]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/net/link-config.c
network: make Name= in [Match] support alternative names of interfaces
[thirdparty/systemd.git] / src / udev / net / link-config.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <linux/netdevice.h>
4 #include <netinet/ether.h>
5
6 #include "sd-device.h"
7 #include "sd-netlink.h"
8
9 #include "alloc-util.h"
10 #include "conf-files.h"
11 #include "conf-parser.h"
12 #include "def.h"
13 #include "device-util.h"
14 #include "ethtool-util.h"
15 #include "fd-util.h"
16 #include "link-config.h"
17 #include "log.h"
18 #include "memory-util.h"
19 #include "naming-scheme.h"
20 #include "netlink-util.h"
21 #include "network-internal.h"
22 #include "parse-util.h"
23 #include "path-util.h"
24 #include "proc-cmdline.h"
25 #include "random-util.h"
26 #include "stat-util.h"
27 #include "string-table.h"
28 #include "string-util.h"
29 #include "strv.h"
30
31 struct link_config_ctx {
32 LIST_HEAD(link_config, links);
33
34 int ethtool_fd;
35
36 bool enable_name_policy;
37
38 sd_netlink *rtnl;
39
40 usec_t network_dirs_ts_usec;
41 };
42
43 static void link_config_free(link_config *link) {
44 if (!link)
45 return;
46
47 free(link->filename);
48
49 set_free_free(link->match_mac);
50 strv_free(link->match_path);
51 strv_free(link->match_driver);
52 strv_free(link->match_type);
53 strv_free(link->match_name);
54 strv_free(link->match_property);
55 condition_free_list(link->conditions);
56
57 free(link->description);
58 free(link->mac);
59 free(link->name_policy);
60 free(link->name);
61 strv_free(link->alternative_names);
62 free(link->alias);
63
64 free(link);
65 }
66
67 DEFINE_TRIVIAL_CLEANUP_FUNC(link_config*, link_config_free);
68
69 static void link_configs_free(link_config_ctx *ctx) {
70 link_config *link, *link_next;
71
72 if (!ctx)
73 return;
74
75 LIST_FOREACH_SAFE(links, link, link_next, ctx->links)
76 link_config_free(link);
77 }
78
79 void link_config_ctx_free(link_config_ctx *ctx) {
80 if (!ctx)
81 return;
82
83 safe_close(ctx->ethtool_fd);
84
85 sd_netlink_unref(ctx->rtnl);
86
87 link_configs_free(ctx);
88
89 free(ctx);
90
91 return;
92 }
93
94 int 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
110 *ret = TAKE_PTR(ctx);
111
112 return 0;
113 }
114
115 int link_load_one(link_config_ctx *ctx, const char *filename) {
116 _cleanup_(link_config_freep) link_config *link = NULL;
117 _cleanup_fclose_ FILE *file = NULL;
118 _cleanup_free_ char *name = NULL;
119 size_t i;
120 int r;
121
122 assert(ctx);
123 assert(filename);
124
125 file = fopen(filename, "re");
126 if (!file)
127 return errno == ENOENT ? 0 : -errno;
128
129 if (null_or_empty_fd(fileno(file))) {
130 log_debug("Skipping empty file: %s", filename);
131 return 0;
132 }
133
134 name = strdup(filename);
135 if (!name)
136 return -ENOMEM;
137
138 link = new(link_config, 1);
139 if (!link)
140 return -ENOMEM;
141
142 *link = (link_config) {
143 .filename = TAKE_PTR(name),
144 .mac_address_policy = _MAC_ADDRESS_POLICY_INVALID,
145 .wol = _WOL_INVALID,
146 .duplex = _DUP_INVALID,
147 .port = _NET_DEV_PORT_INVALID,
148 .autonegotiation = -1,
149 };
150
151 for (i = 0; i < ELEMENTSOF(link->features); i++)
152 link->features[i] = -1;
153
154 r = config_parse(NULL, filename, file,
155 "Match\0Link\0",
156 config_item_perf_lookup, link_config_gperf_lookup,
157 CONFIG_PARSE_WARN, link);
158 if (r < 0)
159 return r;
160
161 if (link->speed > UINT_MAX)
162 return -ERANGE;
163
164 if (set_isempty(link->match_mac) && strv_isempty(link->match_path) &&
165 strv_isempty(link->match_driver) && strv_isempty(link->match_type) &&
166 strv_isempty(link->match_name) && strv_isempty(link->match_property) && !link->conditions)
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
172 if (!condition_test_list(link->conditions, NULL, NULL, NULL)) {
173 log_debug("%s: Conditions do not match the system environment, skipping.", filename);
174 return 0;
175 }
176
177 log_debug("Parsed configuration file %s", filename);
178
179 LIST_PREPEND(links, ctx->links, TAKE_PTR(link));
180 return 0;
181 }
182
183 static bool enable_name_policy(void) {
184 bool b;
185
186 return proc_cmdline_get_bool("net.ifnames", &b) <= 0 || b;
187 }
188
189 static int link_unsigned_attribute(sd_device *device, const char *attr, unsigned *type) {
190 const char *s;
191 int r;
192
193 r = sd_device_get_sysattr_value(device, attr, &s);
194 if (r < 0)
195 return log_device_debug_errno(device, r, "Failed to query %s: %m", attr);
196
197 r = safe_atou(s, type);
198 if (r < 0)
199 return log_device_warning_errno(device, r, "Failed to parse %s \"%s\": %m", attr, s);
200
201 log_device_debug(device, "Device has %s=%u", attr, *type);
202 return 0;
203 }
204
205 int link_config_load(link_config_ctx *ctx) {
206 _cleanup_strv_free_ char **files;
207 char **f;
208 int r;
209
210 link_configs_free(ctx);
211
212 if (!enable_name_policy()) {
213 ctx->enable_name_policy = false;
214 log_info("Network interface NamePolicy= disabled on kernel command line, ignoring.");
215 }
216
217 /* update timestamp */
218 paths_check_timestamp(NETWORK_DIRS, &ctx->network_dirs_ts_usec, true);
219
220 r = conf_files_list_strv(&files, ".link", NULL, 0, NETWORK_DIRS);
221 if (r < 0)
222 return log_error_errno(r, "failed to enumerate link files: %m");
223
224 STRV_FOREACH_BACKWARDS(f, files) {
225 r = link_load_one(ctx, *f);
226 if (r < 0)
227 log_error_errno(r, "Failed to load %s, ignoring: %m", *f);
228 }
229
230 return 0;
231 }
232
233 bool link_config_should_reload(link_config_ctx *ctx) {
234 return paths_check_timestamp(NETWORK_DIRS, &ctx->network_dirs_ts_usec, false);
235 }
236
237 int link_config_get(link_config_ctx *ctx, sd_device *device, link_config **ret) {
238 link_config *link;
239
240 assert(ctx);
241 assert(device);
242 assert(ret);
243
244 LIST_FOREACH(links, link, ctx->links) {
245 if (net_match_config(link->match_mac, link->match_path, link->match_driver,
246 link->match_type, link->match_name, link->match_property, NULL, NULL, NULL,
247 device, NULL, NULL, NULL, 0, NULL, NULL)) {
248 if (link->match_name && !strv_contains(link->match_name, "*")) {
249 unsigned name_assign_type = NET_NAME_UNKNOWN;
250
251 (void) link_unsigned_attribute(device, "name_assign_type", &name_assign_type);
252
253 if (name_assign_type == NET_NAME_ENUM) {
254 log_device_warning(device, "Config file %s applies to device based on potentially unpredictable interface name",
255 link->filename);
256 *ret = link;
257
258 return 0;
259 } else if (name_assign_type == NET_NAME_RENAMED) {
260 log_device_warning(device, "Config file %s matches device based on renamed interface name, ignoring",
261 link->filename);
262
263 continue;
264 }
265 }
266
267 log_device_debug(device, "Config file %s is applied", link->filename);
268
269 *ret = link;
270 return 0;
271 }
272 }
273
274 *ret = NULL;
275 return -ENOENT;
276 }
277
278 static int get_mac(sd_device *device, MACAddressPolicy policy, struct ether_addr *mac) {
279 unsigned addr_type;
280 bool want_random = policy == MAC_ADDRESS_POLICY_RANDOM;
281 int r;
282
283 assert(IN_SET(policy, MAC_ADDRESS_POLICY_RANDOM, MAC_ADDRESS_POLICY_PERSISTENT));
284
285 r = link_unsigned_attribute(device, "addr_assign_type", &addr_type);
286 if (r < 0)
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 }
299
300 if (want_random == (addr_type == NET_ADDR_RANDOM))
301 return log_device_debug(device, "MAC on the device already matches policy *%s*",
302 mac_address_policy_to_string(policy));
303
304 if (want_random) {
305 log_device_debug(device, "Using random bytes to generate MAC");
306 random_bytes(mac->ether_addr_octet, ETH_ALEN);
307 } else {
308 uint64_t result;
309
310 r = net_get_unique_predictable_data(device,
311 naming_scheme_has(NAMING_STABLE_VIRTUAL_MACS),
312 &result);
313 if (r < 0)
314 return log_device_warning_errno(device, r, "Could not generate persistent MAC: %m");
315
316 log_device_debug(device, "Using generated persistent MAC address");
317 assert_cc(ETH_ALEN <= sizeof(result));
318 memcpy(mac->ether_addr_octet, &result, ETH_ALEN);
319 }
320
321 /* see eth_random_addr in the kernel */
322 mac->ether_addr_octet[0] &= 0xfe; /* clear multicast bit */
323 mac->ether_addr_octet[0] |= 0x02; /* set local assignment bit (IEEE802) */
324 return 1;
325 }
326
327 int link_config_apply(link_config_ctx *ctx, link_config *config,
328 sd_device *device, const char **name) {
329 struct ether_addr generated_mac;
330 struct ether_addr *mac = NULL;
331 const char *new_name = NULL;
332 const char *old_name;
333 unsigned speed, name_type = NET_NAME_UNKNOWN;
334 NamePolicy policy;
335 int r, ifindex;
336
337 assert(ctx);
338 assert(config);
339 assert(device);
340 assert(name);
341
342 r = sd_device_get_sysname(device, &old_name);
343 if (r < 0)
344 return r;
345
346 r = ethtool_set_glinksettings(&ctx->ethtool_fd, old_name,
347 config->autonegotiation, config->advertise,
348 config->speed, config->duplex, config->port);
349 if (r < 0) {
350
351 if (config->port != _NET_DEV_PORT_INVALID)
352 log_warning_errno(r, "Could not set port (%s) of %s: %m", port_to_string(config->port), old_name);
353
354 if (!eqzero(config->advertise))
355 log_warning_errno(r, "Could not set advertise mode: %m"); /* TODO: include modes in the log message. */
356
357 if (config->speed) {
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
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));
368 }
369
370 r = ethtool_set_wol(&ctx->ethtool_fd, old_name, config->wol);
371 if (r < 0)
372 log_warning_errno(r, "Could not set WakeOnLan of %s to %s: %m",
373 old_name, wol_to_string(config->wol));
374
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
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
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
391 r = sd_device_get_ifindex(device, &ifindex);
392 if (r < 0)
393 return log_device_warning_errno(device, r, "Could not find ifindex: %m");
394
395 (void) link_unsigned_attribute(device, "name_assign_type", &name_type);
396
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
403 if (ctx->enable_name_policy && config->name_policy)
404 for (NamePolicy *p = config->name_policy; !new_name && *p != _NAMEPOLICY_INVALID; p++) {
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;
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;
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");
440 }
441 }
442
443 if (new_name)
444 log_device_debug(device, "Policy *%s* yields \"%s\".", name_policy_to_string(policy), new_name);
445 else if (config->name) {
446 new_name = config->name;
447 log_device_debug(device, "Policies didn't yield a name, using specified Name=%s.", new_name);
448 } else
449 log_device_debug(device, "Policies didn't yield a name and Name= is not given, not renaming.");
450 no_rename:
451
452 if (IN_SET(config->mac_address_policy, MAC_ADDRESS_POLICY_PERSISTENT, MAC_ADDRESS_POLICY_RANDOM)) {
453 if (get_mac(device, config->mac_address_policy, &generated_mac) > 0)
454 mac = &generated_mac;
455 } else
456 mac = config->mac;
457
458 r = rtnl_set_link_properties(&ctx->rtnl, ifindex, config->alias, mac, config->mtu);
459 if (r < 0)
460 return log_warning_errno(r, "Could not set Alias=, MACAddress= or MTU= on %s: %m", old_name);
461
462 r = rtnl_set_link_alternative_names(&ctx->rtnl, ifindex, config->alternative_names);
463 if (r < 0)
464 return log_warning_errno(r, "Could not set AlternativeName= on %s: %m", old_name);
465
466 *name = new_name;
467
468 return 0;
469 }
470
471 int link_get_driver(link_config_ctx *ctx, sd_device *device, char **ret) {
472 const char *name;
473 char *driver = NULL;
474 int r;
475
476 r = sd_device_get_sysname(device, &name);
477 if (r < 0)
478 return r;
479
480 r = ethtool_get_driver(&ctx->ethtool_fd, name, &driver);
481 if (r < 0)
482 return r;
483
484 *ret = driver;
485 return 0;
486 }
487
488 static const char* const mac_address_policy_table[_MAC_ADDRESS_POLICY_MAX] = {
489 [MAC_ADDRESS_POLICY_PERSISTENT] = "persistent",
490 [MAC_ADDRESS_POLICY_RANDOM] = "random",
491 [MAC_ADDRESS_POLICY_NONE] = "none",
492 };
493
494 DEFINE_STRING_TABLE_LOOKUP(mac_address_policy, MACAddressPolicy);
495 DEFINE_CONFIG_PARSE_ENUM(config_parse_mac_address_policy, mac_address_policy, MACAddressPolicy,
496 "Failed to parse MAC address policy");
497
498 static const char* const name_policy_table[_NAMEPOLICY_MAX] = {
499 [NAMEPOLICY_KERNEL] = "kernel",
500 [NAMEPOLICY_KEEP] = "keep",
501 [NAMEPOLICY_DATABASE] = "database",
502 [NAMEPOLICY_ONBOARD] = "onboard",
503 [NAMEPOLICY_SLOT] = "slot",
504 [NAMEPOLICY_PATH] = "path",
505 [NAMEPOLICY_MAC] = "mac",
506 };
507
508 DEFINE_STRING_TABLE_LOOKUP(name_policy, NamePolicy);
509 DEFINE_CONFIG_PARSE_ENUMV(config_parse_name_policy, name_policy, NamePolicy,
510 _NAMEPOLICY_INVALID,
511 "Failed to parse interface name policy");