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