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