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