]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/net/link-config.c
Merge pull request #22791 from keszybz/bootctl-invert-order
[thirdparty/systemd.git] / src / udev / net / link-config.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <linux/netdevice.h>
4 #include <netinet/ether.h>
5 #include <unistd.h>
6
7 #include "sd-device.h"
8 #include "sd-netlink.h"
9
10 #include "alloc-util.h"
11 #include "arphrd-util.h"
12 #include "conf-files.h"
13 #include "conf-parser.h"
14 #include "creds-util.h"
15 #include "def.h"
16 #include "device-private.h"
17 #include "device-util.h"
18 #include "ethtool-util.h"
19 #include "fd-util.h"
20 #include "fileio.h"
21 #include "link-config.h"
22 #include "log-link.h"
23 #include "memory-util.h"
24 #include "net-condition.h"
25 #include "netif-sriov.h"
26 #include "netif-util.h"
27 #include "netlink-util.h"
28 #include "parse-util.h"
29 #include "path-lookup.h"
30 #include "path-util.h"
31 #include "proc-cmdline.h"
32 #include "random-util.h"
33 #include "stat-util.h"
34 #include "string-table.h"
35 #include "string-util.h"
36 #include "strv.h"
37 #include "utf8.h"
38
39 struct LinkConfigContext {
40 LIST_HEAD(LinkConfig, configs);
41 int ethtool_fd;
42 bool enable_name_policy;
43 usec_t network_dirs_ts_usec;
44 };
45
46 static LinkConfig* link_config_free(LinkConfig *config) {
47 if (!config)
48 return NULL;
49
50 free(config->filename);
51
52 net_match_clear(&config->match);
53 condition_free_list(config->conditions);
54
55 free(config->description);
56 free(config->name_policy);
57 free(config->name);
58 strv_free(config->alternative_names);
59 free(config->alternative_names_policy);
60 free(config->alias);
61 free(config->wol_password_file);
62 erase_and_free(config->wol_password);
63
64 ordered_hashmap_free_with_destructor(config->sr_iov_by_section, sr_iov_free);
65
66 return mfree(config);
67 }
68
69 DEFINE_TRIVIAL_CLEANUP_FUNC(LinkConfig*, link_config_free);
70
71 static void link_configs_free(LinkConfigContext *ctx) {
72 if (!ctx)
73 return;
74
75 LIST_FOREACH(configs, config, ctx->configs)
76 link_config_free(config);
77 }
78
79 LinkConfigContext *link_config_ctx_free(LinkConfigContext *ctx) {
80 if (!ctx)
81 return NULL;
82
83 safe_close(ctx->ethtool_fd);
84 link_configs_free(ctx);
85 return mfree(ctx);
86 }
87
88 int link_config_ctx_new(LinkConfigContext **ret) {
89 _cleanup_(link_config_ctx_freep) LinkConfigContext *ctx = NULL;
90
91 if (!ret)
92 return -EINVAL;
93
94 ctx = new(LinkConfigContext, 1);
95 if (!ctx)
96 return -ENOMEM;
97
98 *ctx = (LinkConfigContext) {
99 .ethtool_fd = -1,
100 .enable_name_policy = true,
101 };
102
103 *ret = TAKE_PTR(ctx);
104
105 return 0;
106 }
107
108 static int link_parse_wol_password(LinkConfig *config, const char *str) {
109 _cleanup_(erase_and_freep) uint8_t *p = NULL;
110 int r;
111
112 assert(config);
113 assert(str);
114
115 assert_cc(sizeof(struct ether_addr) == SOPASS_MAX);
116
117 p = new(uint8_t, SOPASS_MAX);
118 if (!p)
119 return -ENOMEM;
120
121 /* Reuse parse_ether_addr(), as their formats are equivalent. */
122 r = parse_ether_addr(str, (struct ether_addr*) p);
123 if (r < 0)
124 return r;
125
126 erase_and_free(config->wol_password);
127 config->wol_password = TAKE_PTR(p);
128 return 0;
129 }
130
131 static int link_read_wol_password_from_file(LinkConfig *config) {
132 _cleanup_(erase_and_freep) char *password = NULL;
133 int r;
134
135 assert(config);
136
137 if (!config->wol_password_file)
138 return 0;
139
140 r = read_full_file_full(
141 AT_FDCWD, config->wol_password_file, UINT64_MAX, SIZE_MAX,
142 READ_FULL_FILE_SECURE | READ_FULL_FILE_WARN_WORLD_READABLE | READ_FULL_FILE_CONNECT_SOCKET,
143 NULL, &password, NULL);
144 if (r < 0)
145 return r;
146
147 return link_parse_wol_password(config, password);
148 }
149
150 static int link_read_wol_password_from_cred(LinkConfig *config) {
151 _cleanup_free_ char *base = NULL, *cred_name = NULL;
152 _cleanup_(erase_and_freep) char *password = NULL;
153 int r;
154
155 assert(config);
156 assert(config->filename);
157
158 if (config->wol == UINT32_MAX)
159 return 0; /* WakeOnLan= is not specified. */
160 if (!FLAGS_SET(config->wol, WAKE_MAGICSECURE))
161 return 0; /* secureon is not specified in WakeOnLan=. */
162 if (config->wol_password)
163 return 0; /* WakeOnLanPassword= is specified. */
164 if (config->wol_password_file)
165 return 0; /* a file name is specified in WakeOnLanPassword=, but failed to read it. */
166
167 r = path_extract_filename(config->filename, &base);
168 if (r < 0)
169 return r;
170
171 cred_name = strjoin(base, ".wol.password");
172 if (!cred_name)
173 return -ENOMEM;
174
175 r = read_credential(cred_name, (void**) &password, NULL);
176 if (r == -ENOENT)
177 r = read_credential("wol.password", (void**) &password, NULL);
178 if (r < 0)
179 return r;
180
181 return link_parse_wol_password(config, password);
182 }
183
184 static int link_adjust_wol_options(LinkConfig *config) {
185 int r;
186
187 assert(config);
188
189 r = link_read_wol_password_from_file(config);
190 if (r == -ENOMEM)
191 return log_oom();
192 if (r < 0)
193 log_warning_errno(r, "Failed to read WakeOnLan password from %s, ignoring: %m", config->wol_password_file);
194
195 r = link_read_wol_password_from_cred(config);
196 if (r == -ENOMEM)
197 return log_oom();
198 if (r < 0)
199 log_warning_errno(r, "Failed to read WakeOnLan password from credential, ignoring: %m");
200
201 if (config->wol != UINT32_MAX && config->wol_password)
202 /* Enable WAKE_MAGICSECURE flag when WakeOnLanPassword=. Note that when
203 * WakeOnLanPassword= is set without WakeOnLan=, then ethtool_set_wol() enables
204 * WAKE_MAGICSECURE flag and other flags are not changed. */
205 config->wol |= WAKE_MAGICSECURE;
206
207 return 0;
208 }
209
210 int link_load_one(LinkConfigContext *ctx, const char *filename) {
211 _cleanup_(link_config_freep) LinkConfig *config = NULL;
212 _cleanup_free_ char *name = NULL;
213 const char *dropin_dirname;
214 size_t i;
215 int r;
216
217 assert(ctx);
218 assert(filename);
219
220 r = null_or_empty_path(filename);
221 if (r == -ENOENT)
222 return 0;
223 if (r < 0)
224 return r;
225 if (r > 0) {
226 log_debug("Skipping empty file: %s", filename);
227 return 0;
228 }
229
230 name = strdup(filename);
231 if (!name)
232 return -ENOMEM;
233
234 config = new(LinkConfig, 1);
235 if (!config)
236 return -ENOMEM;
237
238 *config = (LinkConfig) {
239 .filename = TAKE_PTR(name),
240 .mac_address_policy = MAC_ADDRESS_POLICY_NONE,
241 .wol = UINT32_MAX, /* UINT32_MAX means do not change WOL setting. */
242 .duplex = _DUP_INVALID,
243 .port = _NET_DEV_PORT_INVALID,
244 .autonegotiation = -1,
245 .rx_flow_control = -1,
246 .tx_flow_control = -1,
247 .autoneg_flow_control = -1,
248 .txqueuelen = UINT32_MAX,
249 .coalesce.use_adaptive_rx_coalesce = -1,
250 .coalesce.use_adaptive_tx_coalesce = -1,
251 .mdi = ETH_TP_MDI_INVALID,
252 .sr_iov_num_vfs = UINT32_MAX,
253 };
254
255 for (i = 0; i < ELEMENTSOF(config->features); i++)
256 config->features[i] = -1;
257
258 dropin_dirname = strjoina(basename(filename), ".d");
259 r = config_parse_many(
260 STRV_MAKE_CONST(filename),
261 (const char* const*) CONF_PATHS_STRV("systemd/network"),
262 dropin_dirname,
263 "Match\0"
264 "Link\0"
265 "SR-IOV\0",
266 config_item_perf_lookup, link_config_gperf_lookup,
267 CONFIG_PARSE_WARN, config, NULL);
268 if (r < 0)
269 return r;
270
271 if (net_match_is_empty(&config->match) && !config->conditions) {
272 log_warning("%s: No valid settings found in the [Match] section, ignoring file. "
273 "To match all interfaces, add OriginalName=* in the [Match] section.",
274 filename);
275 return 0;
276 }
277
278 if (!condition_test_list(config->conditions, environ, NULL, NULL, NULL)) {
279 log_debug("%s: Conditions do not match the system environment, skipping.", filename);
280 return 0;
281 }
282
283 if (IN_SET(config->mac_address_policy, MAC_ADDRESS_POLICY_PERSISTENT, MAC_ADDRESS_POLICY_RANDOM) &&
284 config->hw_addr.length > 0)
285 log_warning("%s: MACAddress= in [Link] section will be ignored when MACAddressPolicy= "
286 "is set to \"persistent\" or \"random\".",
287 filename);
288
289 r = link_adjust_wol_options(config);
290 if (r < 0)
291 return r;
292
293 r = sr_iov_drop_invalid_sections(config->sr_iov_num_vfs, config->sr_iov_by_section);
294 if (r < 0)
295 return r;
296
297 log_debug("Parsed configuration file %s", filename);
298
299 LIST_PREPEND(configs, ctx->configs, TAKE_PTR(config));
300 return 0;
301 }
302
303 static bool enable_name_policy(void) {
304 bool b;
305
306 return proc_cmdline_get_bool("net.ifnames", &b) <= 0 || b;
307 }
308
309 static int device_unsigned_attribute(sd_device *device, const char *attr, unsigned *type) {
310 const char *s;
311 int r;
312
313 r = sd_device_get_sysattr_value(device, attr, &s);
314 if (r < 0)
315 return log_device_debug_errno(device, r, "Failed to query %s: %m", attr);
316
317 r = safe_atou(s, type);
318 if (r < 0)
319 return log_device_warning_errno(device, r, "Failed to parse %s \"%s\": %m", attr, s);
320
321 log_device_debug(device, "Device has %s=%u", attr, *type);
322 return 0;
323 }
324
325 int link_config_load(LinkConfigContext *ctx) {
326 _cleanup_strv_free_ char **files = NULL;
327 int r;
328
329 link_configs_free(ctx);
330
331 if (!enable_name_policy()) {
332 ctx->enable_name_policy = false;
333 log_info("Network interface NamePolicy= disabled on kernel command line, ignoring.");
334 }
335
336 /* update timestamp */
337 paths_check_timestamp(NETWORK_DIRS, &ctx->network_dirs_ts_usec, true);
338
339 r = conf_files_list_strv(&files, ".link", NULL, 0, NETWORK_DIRS);
340 if (r < 0)
341 return log_error_errno(r, "failed to enumerate link files: %m");
342
343 STRV_FOREACH_BACKWARDS(f, files) {
344 r = link_load_one(ctx, *f);
345 if (r < 0)
346 log_error_errno(r, "Failed to load %s, ignoring: %m", *f);
347 }
348
349 return 0;
350 }
351
352 bool link_config_should_reload(LinkConfigContext *ctx) {
353 return paths_check_timestamp(NETWORK_DIRS, &ctx->network_dirs_ts_usec, false);
354 }
355
356 Link *link_free(Link *link) {
357 if (!link)
358 return NULL;
359
360 sd_device_unref(link->device);
361 free(link->kind);
362 free(link->driver);
363 return mfree(link);
364 }
365
366 int link_new(LinkConfigContext *ctx, sd_netlink **rtnl, sd_device *device, Link **ret) {
367 _cleanup_(link_freep) Link *link = NULL;
368 int r;
369
370 assert(ctx);
371 assert(rtnl);
372 assert(device);
373 assert(ret);
374
375 link = new(Link, 1);
376 if (!link)
377 return -ENOMEM;
378
379 *link = (Link) {
380 .device = sd_device_ref(device),
381 };
382
383 r = sd_device_get_sysname(device, &link->ifname);
384 if (r < 0)
385 return r;
386
387 r = sd_device_get_ifindex(device, &link->ifindex);
388 if (r < 0)
389 return r;
390
391 r = sd_device_get_action(device, &link->action);
392 if (r < 0)
393 return r;
394
395 r = device_unsigned_attribute(device, "name_assign_type", &link->name_assign_type);
396 if (r < 0)
397 log_link_debug_errno(link, r, "Failed to get \"name_assign_type\" attribute, ignoring: %m");
398
399 r = device_unsigned_attribute(device, "addr_assign_type", &link->addr_assign_type);
400 if (r < 0)
401 log_link_debug_errno(link, r, "Failed to get \"addr_assign_type\" attribute, ignoring: %m");
402
403 r = rtnl_get_link_info(rtnl, link->ifindex, &link->iftype, &link->flags,
404 &link->kind, &link->hw_addr, &link->permanent_hw_addr);
405 if (r < 0)
406 return r;
407
408 if (link->hw_addr.length > 0 && link->permanent_hw_addr.length == 0) {
409 r = ethtool_get_permanent_hw_addr(&ctx->ethtool_fd, link->ifname, &link->permanent_hw_addr);
410 if (r < 0)
411 log_link_debug_errno(link, r, "Failed to get permanent hardware address, ignoring: %m");
412 }
413
414 r = ethtool_get_driver(&ctx->ethtool_fd, link->ifname, &link->driver);
415 if (r < 0)
416 log_link_debug_errno(link, r, "Failed to get driver, ignoring: %m");
417
418 *ret = TAKE_PTR(link);
419 return 0;
420 }
421
422 int link_get_config(LinkConfigContext *ctx, Link *link) {
423 int r;
424
425 assert(ctx);
426 assert(link);
427
428 /* Do not configure loopback interfaces by .link files. */
429 if (link->flags & IFF_LOOPBACK)
430 return -ENOENT;
431
432 LIST_FOREACH(configs, config, ctx->configs) {
433 r = net_match_config(
434 &config->match,
435 link->device,
436 &link->hw_addr,
437 &link->permanent_hw_addr,
438 link->driver,
439 link->iftype,
440 link->kind,
441 link->ifname,
442 /* alternative_names = */ NULL,
443 /* wlan_iftype = */ 0,
444 /* ssid = */ NULL,
445 /* bssid = */ NULL);
446 if (r < 0)
447 return r;
448 if (r == 0)
449 continue;
450
451 if (config->match.ifname && !strv_contains(config->match.ifname, "*") && link->name_assign_type == NET_NAME_ENUM)
452 log_link_warning(link, "Config file %s is applied to device based on potentially unpredictable interface name.",
453 config->filename);
454 else
455 log_link_debug(link, "Config file %s is applied", config->filename);
456
457 link->config = config;
458 return 0;
459 }
460
461 return -ENOENT;
462 }
463
464 static int link_apply_ethtool_settings(Link *link, int *ethtool_fd) {
465 LinkConfig *config;
466 const char *name;
467 int r;
468
469 assert(link);
470 assert(link->config);
471 assert(ethtool_fd);
472
473 config = link->config;
474 name = link->ifname;
475
476 r = ethtool_set_glinksettings(ethtool_fd, name,
477 config->autonegotiation, config->advertise,
478 config->speed, config->duplex, config->port, config->mdi);
479 if (r < 0) {
480 if (config->autonegotiation >= 0)
481 log_link_warning_errno(link, r, "Could not %s auto negotiation, ignoring: %m",
482 enable_disable(config->autonegotiation));
483
484 if (!eqzero(config->advertise))
485 log_link_warning_errno(link, r, "Could not set advertise mode, ignoring: %m");
486
487 if (config->speed > 0)
488 log_link_warning_errno(link, r, "Could not set speed to %"PRIu64"Mbps, ignoring: %m",
489 DIV_ROUND_UP(config->speed, 1000000));
490
491 if (config->duplex >= 0)
492 log_link_warning_errno(link, r, "Could not set duplex to %s, ignoring: %m",
493 duplex_to_string(config->duplex));
494
495 if (config->port >= 0)
496 log_link_warning_errno(link, r, "Could not set port to '%s', ignoring: %m",
497 port_to_string(config->port));
498
499 if (config->mdi != ETH_TP_MDI_INVALID)
500 log_link_warning_errno(link, r, "Could not set MDI-X to '%s', ignoring: %m",
501 mdi_to_string(config->mdi));
502 }
503
504 r = ethtool_set_wol(ethtool_fd, name, config->wol, config->wol_password);
505 if (r < 0) {
506 _cleanup_free_ char *str = NULL;
507
508 (void) wol_options_to_string_alloc(config->wol, &str);
509 log_link_warning_errno(link, r, "Could not set WakeOnLan%s%s, ignoring: %m",
510 isempty(str) ? "" : " to ", strempty(str));
511 }
512
513 r = ethtool_set_features(ethtool_fd, name, config->features);
514 if (r < 0)
515 log_link_warning_errno(link, r, "Could not set offload features, ignoring: %m");
516
517 r = ethtool_set_channels(ethtool_fd, name, &config->channels);
518 if (r < 0)
519 log_link_warning_errno(link, r, "Could not set channels, ignoring: %m");
520
521 r = ethtool_set_nic_buffer_size(ethtool_fd, name, &config->ring);
522 if (r < 0)
523 log_link_warning_errno(link, r, "Could not set ring buffer, ignoring: %m");
524
525 r = ethtool_set_flow_control(ethtool_fd, name, config->rx_flow_control, config->tx_flow_control, config->autoneg_flow_control);
526 if (r < 0)
527 log_link_warning_errno(link, r, "Could not set flow control, ignoring: %m");
528
529 r = ethtool_set_nic_coalesce_settings(ethtool_fd, name, &config->coalesce);
530 if (r < 0)
531 log_link_warning_errno(link, r, "Could not set coalesce settings, ignoring: %m");
532
533 return 0;
534 }
535
536 static bool hw_addr_is_valid(Link *link, const struct hw_addr_data *hw_addr) {
537 assert(link);
538 assert(hw_addr);
539
540 switch (link->iftype) {
541 case ARPHRD_ETHER:
542 /* Refuse all zero and all 0xFF. */
543 assert(hw_addr->length == ETH_ALEN);
544 return !ether_addr_is_null(&hw_addr->ether) && !ether_addr_is_broadcast(&hw_addr->ether);
545
546 case ARPHRD_INFINIBAND:
547 /* The last 8 bytes cannot be zero*/
548 assert(hw_addr->length == INFINIBAND_ALEN);
549 return !memeqzero(hw_addr->bytes + INFINIBAND_ALEN - 8, 8);
550
551 default:
552 assert_not_reached();
553 }
554 }
555
556 static int link_generate_new_hw_addr(Link *link, struct hw_addr_data *ret) {
557 struct hw_addr_data hw_addr = HW_ADDR_NULL;
558 bool is_static = false;
559 uint8_t *p;
560 size_t len;
561 int r;
562
563 assert(link);
564 assert(link->config);
565 assert(link->device);
566 assert(ret);
567
568 if (link->hw_addr.length == 0)
569 goto finalize;
570
571 if (link->config->mac_address_policy == MAC_ADDRESS_POLICY_NONE) {
572 log_link_debug(link, "Using static MAC address.");
573 hw_addr = link->config->hw_addr;
574 is_static = true;
575 goto finalize;
576 }
577
578 if (!IN_SET(link->iftype, ARPHRD_ETHER, ARPHRD_INFINIBAND))
579 goto finalize;
580
581 switch (link->addr_assign_type) {
582 case NET_ADDR_SET:
583 log_link_debug(link, "MAC address on the device already set by userspace.");
584 goto finalize;
585 case NET_ADDR_STOLEN:
586 log_link_debug(link, "MAC address on the device already set based on another device.");
587 goto finalize;
588 case NET_ADDR_RANDOM:
589 case NET_ADDR_PERM:
590 break;
591 default:
592 log_link_warning(link, "Unknown addr_assign_type %u, ignoring", link->addr_assign_type);
593 goto finalize;
594 }
595
596 if ((link->config->mac_address_policy == MAC_ADDRESS_POLICY_RANDOM) == (link->addr_assign_type == NET_ADDR_RANDOM)) {
597 log_link_debug(link, "MAC address on the device already matches policy \"%s\".",
598 mac_address_policy_to_string(link->config->mac_address_policy));
599 goto finalize;
600 }
601
602 hw_addr = (struct hw_addr_data) {
603 .length = arphrd_to_hw_addr_len(link->iftype),
604 };
605
606 switch (link->iftype) {
607 case ARPHRD_ETHER:
608 p = hw_addr.bytes;
609 len = hw_addr.length;
610 break;
611 case ARPHRD_INFINIBAND:
612 p = hw_addr.bytes + INFINIBAND_ALEN - 8;
613 len = 8;
614 break;
615 default:
616 assert_not_reached();
617 }
618
619 if (link->config->mac_address_policy == MAC_ADDRESS_POLICY_RANDOM)
620 /* We require genuine randomness here, since we want to make sure we won't collide with other
621 * systems booting up at the very same time. */
622 for (;;) {
623 r = genuine_random_bytes(p, len, 0);
624 if (r < 0)
625 return log_link_warning_errno(link, r, "Failed to acquire random data to generate MAC address: %m");
626
627 if (hw_addr_is_valid(link, &hw_addr))
628 break;
629 }
630
631 else {
632 uint64_t result;
633
634 r = net_get_unique_predictable_data(link->device,
635 naming_scheme_has(NAMING_STABLE_VIRTUAL_MACS),
636 &result);
637 if (r < 0)
638 return log_link_warning_errno(link, r, "Could not generate persistent MAC address: %m");
639
640 assert(len <= sizeof(result));
641 memcpy(p, &result, len);
642 if (!hw_addr_is_valid(link, &hw_addr))
643 return log_link_warning_errno(link, SYNTHETIC_ERRNO(EINVAL),
644 "Could not generate valid persistent MAC address: %m");
645 }
646
647 finalize:
648
649 r = net_verify_hardware_address(link->ifname, is_static, link->iftype, &link->hw_addr, &hw_addr);
650 if (r < 0)
651 return r;
652
653 if (hw_addr_equal(&link->hw_addr, &hw_addr)) {
654 *ret = HW_ADDR_NULL;
655 return 0;
656 }
657
658 if (hw_addr.length > 0)
659 log_link_debug(link, "Applying %s MAC address: %s",
660 link->config->mac_address_policy == MAC_ADDRESS_POLICY_NONE ? "static" :
661 mac_address_policy_to_string(link->config->mac_address_policy),
662 HW_ADDR_TO_STR(&hw_addr));
663
664 *ret = hw_addr;
665 return 0;
666 }
667
668 static int link_apply_rtnl_settings(Link *link, sd_netlink **rtnl) {
669 struct hw_addr_data hw_addr = {};
670 LinkConfig *config;
671 int r;
672
673 assert(link);
674 assert(link->config);
675 assert(rtnl);
676
677 config = link->config;
678
679 (void) link_generate_new_hw_addr(link, &hw_addr);
680
681 r = rtnl_set_link_properties(rtnl, link->ifindex, config->alias, &hw_addr,
682 config->txqueues, config->rxqueues, config->txqueuelen,
683 config->mtu, config->gso_max_size, config->gso_max_segments);
684 if (r < 0)
685 log_link_warning_errno(link, r,
686 "Could not set Alias=, MACAddress=/MACAddressPolicy=, "
687 "TransmitQueues=, ReceiveQueues=, TransmitQueueLength=, MTUBytes=, "
688 "GenericSegmentOffloadMaxBytes= or GenericSegmentOffloadMaxSegments=, "
689 "ignoring: %m");
690
691 return 0;
692 }
693
694 static int link_generate_new_name(Link *link, bool enable_name_policy) {
695 LinkConfig *config;
696 sd_device *device;
697
698 assert(link);
699 assert(link->config);
700 assert(link->device);
701
702 config = link->config;
703 device = link->device;
704
705 if (link->action == SD_DEVICE_MOVE) {
706 log_link_debug(link, "Skipping to apply Name= and NamePolicy= on '%s' uevent.",
707 device_action_to_string(link->action));
708 goto no_rename;
709 }
710
711 if (IN_SET(link->name_assign_type, NET_NAME_USER, NET_NAME_RENAMED) &&
712 !naming_scheme_has(NAMING_ALLOW_RERENAMES)) {
713 log_link_debug(link, "Device already has a name given by userspace, not renaming.");
714 goto no_rename;
715 }
716
717 if (enable_name_policy && config->name_policy)
718 for (NamePolicy *policy = config->name_policy; *policy != _NAMEPOLICY_INVALID; policy++) {
719 const char *new_name = NULL;
720
721 switch (*policy) {
722 case NAMEPOLICY_KERNEL:
723 if (link->name_assign_type != NET_NAME_PREDICTABLE)
724 continue;
725
726 /* The kernel claims to have given a predictable name, keep it. */
727 log_link_debug(link, "Policy *%s*: keeping predictable kernel name",
728 name_policy_to_string(*policy));
729 goto no_rename;
730 case NAMEPOLICY_KEEP:
731 if (!IN_SET(link->name_assign_type, NET_NAME_USER, NET_NAME_RENAMED))
732 continue;
733
734 log_link_debug(link, "Policy *%s*: keeping existing userspace name",
735 name_policy_to_string(*policy));
736 goto no_rename;
737 case NAMEPOLICY_DATABASE:
738 (void) sd_device_get_property_value(device, "ID_NET_NAME_FROM_DATABASE", &new_name);
739 break;
740 case NAMEPOLICY_ONBOARD:
741 (void) sd_device_get_property_value(device, "ID_NET_NAME_ONBOARD", &new_name);
742 break;
743 case NAMEPOLICY_SLOT:
744 (void) sd_device_get_property_value(device, "ID_NET_NAME_SLOT", &new_name);
745 break;
746 case NAMEPOLICY_PATH:
747 (void) sd_device_get_property_value(device, "ID_NET_NAME_PATH", &new_name);
748 break;
749 case NAMEPOLICY_MAC:
750 (void) sd_device_get_property_value(device, "ID_NET_NAME_MAC", &new_name);
751 break;
752 default:
753 assert_not_reached();
754 }
755 if (ifname_valid(new_name)) {
756 log_link_debug(link, "Policy *%s* yields \"%s\".", name_policy_to_string(*policy), new_name);
757 link->new_name = new_name;
758 return 0;
759 }
760 }
761
762 if (link->config->name) {
763 log_link_debug(link, "Policies didn't yield a name, using specified Name=%s.", link->config->name);
764 link->new_name = link->config->name;
765 return 0;
766 }
767
768 log_link_debug(link, "Policies didn't yield a name and Name= is not given, not renaming.");
769 no_rename:
770 link->new_name = link->ifname;
771 return 0;
772 }
773
774 static int link_apply_alternative_names(Link *link, sd_netlink **rtnl) {
775 _cleanup_strv_free_ char **altnames = NULL, **current_altnames = NULL;
776 LinkConfig *config;
777 sd_device *device;
778 int r;
779
780 assert(link);
781 assert(link->config);
782 assert(link->device);
783 assert(rtnl);
784
785 config = link->config;
786 device = link->device;
787
788 if (config->alternative_names) {
789 altnames = strv_copy(config->alternative_names);
790 if (!altnames)
791 return log_oom();
792 }
793
794 if (config->alternative_names_policy)
795 for (NamePolicy *p = config->alternative_names_policy; *p != _NAMEPOLICY_INVALID; p++) {
796 const char *n = NULL;
797
798 switch (*p) {
799 case NAMEPOLICY_DATABASE:
800 (void) sd_device_get_property_value(device, "ID_NET_NAME_FROM_DATABASE", &n);
801 break;
802 case NAMEPOLICY_ONBOARD:
803 (void) sd_device_get_property_value(device, "ID_NET_NAME_ONBOARD", &n);
804 break;
805 case NAMEPOLICY_SLOT:
806 (void) sd_device_get_property_value(device, "ID_NET_NAME_SLOT", &n);
807 break;
808 case NAMEPOLICY_PATH:
809 (void) sd_device_get_property_value(device, "ID_NET_NAME_PATH", &n);
810 break;
811 case NAMEPOLICY_MAC:
812 (void) sd_device_get_property_value(device, "ID_NET_NAME_MAC", &n);
813 break;
814 default:
815 assert_not_reached();
816 }
817 if (!isempty(n)) {
818 r = strv_extend(&altnames, n);
819 if (r < 0)
820 return log_oom();
821 }
822 }
823
824 if (link->new_name)
825 strv_remove(altnames, link->new_name);
826 strv_remove(altnames, link->ifname);
827
828 r = rtnl_get_link_alternative_names(rtnl, link->ifindex, &current_altnames);
829 if (r < 0)
830 log_link_debug_errno(link, r, "Failed to get alternative names, ignoring: %m");
831
832 STRV_FOREACH(p, current_altnames)
833 strv_remove(altnames, *p);
834
835 strv_uniq(altnames);
836 strv_sort(altnames);
837 r = rtnl_set_link_alternative_names(rtnl, link->ifindex, altnames);
838 if (r < 0)
839 log_link_full_errno(link, r == -EOPNOTSUPP ? LOG_DEBUG : LOG_WARNING, r,
840 "Could not set AlternativeName= or apply AlternativeNamesPolicy=, ignoring: %m");
841
842 return 0;
843 }
844
845 static int sr_iov_configure(Link *link, sd_netlink **rtnl, SRIOV *sr_iov) {
846 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
847 int r;
848
849 assert(link);
850 assert(rtnl);
851 assert(link->ifindex > 0);
852
853 if (!*rtnl) {
854 r = sd_netlink_open(rtnl);
855 if (r < 0)
856 return r;
857 }
858
859 r = sd_rtnl_message_new_link(*rtnl, &req, RTM_SETLINK, link->ifindex);
860 if (r < 0)
861 return r;
862
863 r = sr_iov_set_netlink_message(sr_iov, req);
864 if (r < 0)
865 return r;
866
867 r = sd_netlink_call(*rtnl, req, 0, NULL);
868 if (r < 0)
869 return r;
870
871 return 0;
872 }
873
874 static int link_apply_sr_iov_config(Link *link, sd_netlink **rtnl) {
875 SRIOV *sr_iov;
876 uint32_t n;
877 int r;
878
879 assert(link);
880 assert(link->config);
881 assert(link->device);
882
883 r = sr_iov_set_num_vfs(link->device, link->config->sr_iov_num_vfs, link->config->sr_iov_by_section);
884 if (r < 0)
885 log_link_warning_errno(link, r, "Failed to set the number of SR-IOV virtual functions, ignoring: %m");
886
887 if (ordered_hashmap_isempty(link->config->sr_iov_by_section))
888 return 0;
889
890 r = sr_iov_get_num_vfs(link->device, &n);
891 if (r < 0) {
892 log_link_warning_errno(link, r, "Failed to get the number of SR-IOV virtual functions, ignoring [SR-IOV] sections: %m");
893 return 0;
894 }
895 if (n == 0) {
896 log_link_warning(link, "No SR-IOV virtual function exists, ignoring [SR-IOV] sections: %m");
897 return 0;
898 }
899
900 ORDERED_HASHMAP_FOREACH(sr_iov, link->config->sr_iov_by_section) {
901 if (sr_iov->vf >= n) {
902 log_link_warning(link, "SR-IOV virtual function %"PRIu32" does not exist, ignoring.", sr_iov->vf);
903 continue;
904 }
905
906 r = sr_iov_configure(link, rtnl, sr_iov);
907 if (r < 0)
908 log_link_warning_errno(link, r,
909 "Failed to configure SR-IOV virtual function %"PRIu32", ignoring: %m",
910 sr_iov->vf);
911 }
912
913 return 0;
914 }
915
916 int link_apply_config(LinkConfigContext *ctx, sd_netlink **rtnl, Link *link) {
917 int r;
918
919 assert(ctx);
920 assert(rtnl);
921 assert(link);
922
923 if (!IN_SET(link->action, SD_DEVICE_ADD, SD_DEVICE_BIND, SD_DEVICE_MOVE)) {
924 log_link_debug(link, "Skipping to apply .link settings on '%s' uevent.",
925 device_action_to_string(link->action));
926
927 link->new_name = link->ifname;
928 return 0;
929 }
930
931 r = link_apply_ethtool_settings(link, &ctx->ethtool_fd);
932 if (r < 0)
933 return r;
934
935 r = link_apply_rtnl_settings(link, rtnl);
936 if (r < 0)
937 return r;
938
939 r = link_generate_new_name(link, ctx->enable_name_policy);
940 if (r < 0)
941 return r;
942
943 r = link_apply_alternative_names(link, rtnl);
944 if (r < 0)
945 return r;
946
947 r = link_apply_sr_iov_config(link, rtnl);
948 if (r < 0)
949 return r;
950
951 return 0;
952 }
953
954 int config_parse_ifalias(
955 const char *unit,
956 const char *filename,
957 unsigned line,
958 const char *section,
959 unsigned section_line,
960 const char *lvalue,
961 int ltype,
962 const char *rvalue,
963 void *data,
964 void *userdata) {
965
966 char **s = data;
967
968 assert(filename);
969 assert(lvalue);
970 assert(rvalue);
971 assert(data);
972
973 if (!isempty(rvalue)) {
974 *s = mfree(*s);
975 return 0;
976 }
977
978 if (!ascii_is_valid(rvalue)) {
979 log_syntax(unit, LOG_WARNING, filename, line, 0,
980 "Interface alias is not ASCII clean, ignoring assignment: %s", rvalue);
981 return 0;
982 }
983
984 if (strlen(rvalue) >= IFALIASZ) {
985 log_syntax(unit, LOG_WARNING, filename, line, 0,
986 "Interface alias is too long, ignoring assignment: %s", rvalue);
987 return 0;
988 }
989
990 return free_and_strdup_warn(s, rvalue);
991 }
992
993 int config_parse_rx_tx_queues(
994 const char *unit,
995 const char *filename,
996 unsigned line,
997 const char *section,
998 unsigned section_line,
999 const char *lvalue,
1000 int ltype,
1001 const char *rvalue,
1002 void *data,
1003 void *userdata) {
1004
1005 uint32_t k, *v = data;
1006 int r;
1007
1008 if (isempty(rvalue)) {
1009 *v = 0;
1010 return 0;
1011 }
1012
1013 r = safe_atou32(rvalue, &k);
1014 if (r < 0) {
1015 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse %s=, ignoring assignment: %s.", lvalue, rvalue);
1016 return 0;
1017 }
1018 if (k == 0 || k > 4096) {
1019 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid %s=, ignoring assignment: %s.", lvalue, rvalue);
1020 return 0;
1021 }
1022
1023 *v = k;
1024 return 0;
1025 }
1026
1027 int config_parse_txqueuelen(
1028 const char *unit,
1029 const char *filename,
1030 unsigned line,
1031 const char *section,
1032 unsigned section_line,
1033 const char *lvalue,
1034 int ltype,
1035 const char *rvalue,
1036 void *data,
1037 void *userdata) {
1038
1039 uint32_t k, *v = data;
1040 int r;
1041
1042 if (isempty(rvalue)) {
1043 *v = UINT32_MAX;
1044 return 0;
1045 }
1046
1047 r = safe_atou32(rvalue, &k);
1048 if (r < 0) {
1049 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse %s=, ignoring assignment: %s.", lvalue, rvalue);
1050 return 0;
1051 }
1052 if (k == UINT32_MAX) {
1053 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid %s=, ignoring assignment: %s.", lvalue, rvalue);
1054 return 0;
1055 }
1056
1057 *v = k;
1058 return 0;
1059 }
1060
1061 int config_parse_wol_password(
1062 const char *unit,
1063 const char *filename,
1064 unsigned line,
1065 const char *section,
1066 unsigned section_line,
1067 const char *lvalue,
1068 int ltype,
1069 const char *rvalue,
1070 void *data,
1071 void *userdata) {
1072
1073 LinkConfig *config = userdata;
1074 int r;
1075
1076 assert(filename);
1077 assert(lvalue);
1078 assert(rvalue);
1079 assert(userdata);
1080
1081 if (isempty(rvalue)) {
1082 config->wol_password = erase_and_free(config->wol_password);
1083 config->wol_password_file = mfree(config->wol_password_file);
1084 return 0;
1085 }
1086
1087 if (path_is_absolute(rvalue) && path_is_safe(rvalue)) {
1088 config->wol_password = erase_and_free(config->wol_password);
1089 return free_and_strdup_warn(&config->wol_password_file, rvalue);
1090 }
1091
1092 warn_file_is_world_accessible(filename, NULL, unit, line);
1093
1094 r = link_parse_wol_password(config, rvalue);
1095 if (r == -ENOMEM)
1096 return log_oom();
1097 if (r < 0) {
1098 log_syntax(unit, LOG_WARNING, filename, line, r,
1099 "Failed to parse %s=, ignoring assignment: %s.", lvalue, rvalue);
1100 return 0;
1101 }
1102
1103 config->wol_password_file = mfree(config->wol_password_file);
1104 return 0;
1105 }
1106
1107 static const char* const mac_address_policy_table[_MAC_ADDRESS_POLICY_MAX] = {
1108 [MAC_ADDRESS_POLICY_PERSISTENT] = "persistent",
1109 [MAC_ADDRESS_POLICY_RANDOM] = "random",
1110 [MAC_ADDRESS_POLICY_NONE] = "none",
1111 };
1112
1113 DEFINE_STRING_TABLE_LOOKUP(mac_address_policy, MACAddressPolicy);
1114 DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT(
1115 config_parse_mac_address_policy,
1116 mac_address_policy,
1117 MACAddressPolicy,
1118 MAC_ADDRESS_POLICY_NONE,
1119 "Failed to parse MAC address policy");
1120
1121 DEFINE_CONFIG_PARSE_ENUMV(config_parse_name_policy, name_policy, NamePolicy,
1122 _NAMEPOLICY_INVALID,
1123 "Failed to parse interface name policy");
1124
1125 DEFINE_CONFIG_PARSE_ENUMV(config_parse_alternative_names_policy, alternative_names_policy, NamePolicy,
1126 _NAMEPOLICY_INVALID,
1127 "Failed to parse alternative names policy");