1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include <linux/netdevice.h>
4 #include <netinet/ether.h>
8 #include "sd-netlink.h"
10 #include "alloc-util.h"
11 #include "conf-files.h"
12 #include "conf-parser.h"
14 #include "device-private.h"
15 #include "device-util.h"
16 #include "ethtool-util.h"
18 #include "link-config.h"
20 #include "memory-util.h"
21 #include "net-condition.h"
22 #include "netif-naming-scheme.h"
23 #include "netlink-util.h"
24 #include "network-util.h"
25 #include "parse-util.h"
26 #include "path-lookup.h"
27 #include "path-util.h"
28 #include "proc-cmdline.h"
29 #include "random-util.h"
30 #include "stat-util.h"
31 #include "string-table.h"
32 #include "string-util.h"
36 struct link_config_ctx
{
37 LIST_HEAD(link_config
, links
);
41 bool enable_name_policy
;
45 usec_t network_dirs_ts_usec
;
48 static link_config
* link_config_free(link_config
*link
) {
54 net_match_clear(&link
->match
);
55 condition_free_list(link
->conditions
);
57 free(link
->description
);
59 free(link
->name_policy
);
61 strv_free(link
->alternative_names
);
62 free(link
->alternative_names_policy
);
68 DEFINE_TRIVIAL_CLEANUP_FUNC(link_config
*, link_config_free
);
70 static void link_configs_free(link_config_ctx
*ctx
) {
71 link_config
*link
, *link_next
;
76 LIST_FOREACH_SAFE(links
, link
, link_next
, ctx
->links
)
77 link_config_free(link
);
80 link_config_ctx
* link_config_ctx_free(link_config_ctx
*ctx
) {
84 safe_close(ctx
->ethtool_fd
);
85 sd_netlink_unref(ctx
->rtnl
);
86 link_configs_free(ctx
);
90 int link_config_ctx_new(link_config_ctx
**ret
) {
91 _cleanup_(link_config_ctx_freep
) link_config_ctx
*ctx
= NULL
;
96 ctx
= new0(link_config_ctx
, 1);
100 LIST_HEAD_INIT(ctx
->links
);
102 ctx
->ethtool_fd
= -1;
104 ctx
->enable_name_policy
= true;
106 *ret
= TAKE_PTR(ctx
);
111 int link_load_one(link_config_ctx
*ctx
, const char *filename
) {
112 _cleanup_(link_config_freep
) link_config
*link
= NULL
;
113 _cleanup_free_
char *name
= NULL
;
114 const char *dropin_dirname
;
121 r
= null_or_empty_path(filename
);
127 log_debug("Skipping empty file: %s", filename
);
131 name
= strdup(filename
);
135 link
= new(link_config
, 1);
139 *link
= (link_config
) {
140 .filename
= TAKE_PTR(name
),
141 .mac_address_policy
= _MAC_ADDRESS_POLICY_INVALID
,
143 .duplex
= _DUP_INVALID
,
144 .port
= _NET_DEV_PORT_INVALID
,
145 .autonegotiation
= -1,
146 .rx_flow_control
= -1,
147 .tx_flow_control
= -1,
148 .autoneg_flow_control
= -1,
149 .txqueuelen
= UINT32_MAX
,
152 for (i
= 0; i
< ELEMENTSOF(link
->features
); i
++)
153 link
->features
[i
] = -1;
155 dropin_dirname
= strjoina(basename(filename
), ".d");
156 r
= config_parse_many(
157 STRV_MAKE_CONST(filename
),
158 (const char* const*) CONF_PATHS_STRV("systemd/network"),
161 config_item_perf_lookup
, link_config_gperf_lookup
,
162 CONFIG_PARSE_WARN
, link
, NULL
);
166 if (net_match_is_empty(&link
->match
) && !link
->conditions
) {
167 log_warning("%s: No valid settings found in the [Match] section, ignoring file. "
168 "To match all interfaces, add OriginalName=* in the [Match] section.",
173 if (!condition_test_list(link
->conditions
, environ
, NULL
, NULL
, NULL
)) {
174 log_debug("%s: Conditions do not match the system environment, skipping.", filename
);
178 if (IN_SET(link
->mac_address_policy
, MAC_ADDRESS_POLICY_PERSISTENT
, MAC_ADDRESS_POLICY_RANDOM
) && link
->mac
) {
179 log_warning("%s: MACAddress= in [Link] section will be ignored when MACAddressPolicy= "
180 "is set to \"persistent\" or \"random\".",
182 link
->mac
= mfree(link
->mac
);
185 log_debug("Parsed configuration file %s", filename
);
187 LIST_PREPEND(links
, ctx
->links
, TAKE_PTR(link
));
191 static bool enable_name_policy(void) {
194 return proc_cmdline_get_bool("net.ifnames", &b
) <= 0 || b
;
197 static int link_unsigned_attribute(sd_device
*device
, const char *attr
, unsigned *type
) {
201 r
= sd_device_get_sysattr_value(device
, attr
, &s
);
203 return log_device_debug_errno(device
, r
, "Failed to query %s: %m", attr
);
205 r
= safe_atou(s
, type
);
207 return log_device_warning_errno(device
, r
, "Failed to parse %s \"%s\": %m", attr
, s
);
209 log_device_debug(device
, "Device has %s=%u", attr
, *type
);
213 int link_config_load(link_config_ctx
*ctx
) {
214 _cleanup_strv_free_
char **files
= NULL
;
218 link_configs_free(ctx
);
220 if (!enable_name_policy()) {
221 ctx
->enable_name_policy
= false;
222 log_info("Network interface NamePolicy= disabled on kernel command line, ignoring.");
225 /* update timestamp */
226 paths_check_timestamp(NETWORK_DIRS
, &ctx
->network_dirs_ts_usec
, true);
228 r
= conf_files_list_strv(&files
, ".link", NULL
, 0, NETWORK_DIRS
);
230 return log_error_errno(r
, "failed to enumerate link files: %m");
232 STRV_FOREACH_BACKWARDS(f
, files
) {
233 r
= link_load_one(ctx
, *f
);
235 log_error_errno(r
, "Failed to load %s, ignoring: %m", *f
);
241 bool link_config_should_reload(link_config_ctx
*ctx
) {
242 return paths_check_timestamp(NETWORK_DIRS
, &ctx
->network_dirs_ts_usec
, false);
245 int link_config_get(link_config_ctx
*ctx
, sd_device
*device
, link_config
**ret
) {
246 unsigned name_assign_type
= NET_NAME_UNKNOWN
;
247 struct ether_addr permanent_mac
= {};
248 unsigned short iftype
= 0;
257 r
= sd_device_get_sysname(device
, &name
);
261 r
= sd_device_get_ifindex(device
, &ifindex
);
265 r
= rtnl_get_link_iftype(&ctx
->rtnl
, ifindex
, &iftype
);
269 r
= ethtool_get_permanent_macaddr(&ctx
->ethtool_fd
, name
, &permanent_mac
);
271 log_device_debug_errno(device
, r
, "Failed to get permanent MAC address, ignoring: %m");
273 (void) link_unsigned_attribute(device
, "name_assign_type", &name_assign_type
);
275 LIST_FOREACH(links
, link
, ctx
->links
) {
276 if (net_match_config(&link
->match
, device
, NULL
, &permanent_mac
, NULL
, iftype
, NULL
, NULL
, 0, NULL
, NULL
)) {
277 if (link
->match
.ifname
&& !strv_contains(link
->match
.ifname
, "*") && name_assign_type
== NET_NAME_ENUM
)
278 log_device_warning(device
, "Config file %s is applied to device based on potentially unpredictable interface name.",
281 log_device_debug(device
, "Config file %s is applied", link
->filename
);
291 static int link_config_apply_ethtool_settings(int *ethtool_fd
, const link_config
*config
, sd_device
*device
) {
299 r
= sd_device_get_sysname(device
, &name
);
301 return log_device_error_errno(device
, r
, "Failed to get sysname: %m");
303 r
= ethtool_set_glinksettings(ethtool_fd
, name
,
304 config
->autonegotiation
, config
->advertise
,
305 config
->speed
, config
->duplex
, config
->port
);
307 if (config
->port
!= _NET_DEV_PORT_INVALID
)
308 log_device_warning_errno(device
, r
, "Could not set port '%s', ignoring: %m", port_to_string(config
->port
));
310 if (!eqzero(config
->advertise
))
311 log_device_warning_errno(device
, r
, "Could not set advertise mode, ignoring: %m"); /* TODO: include modes in the log message. */
314 unsigned speed
= DIV_ROUND_UP(config
->speed
, 1000000);
315 if (r
== -EOPNOTSUPP
) {
316 r
= ethtool_set_speed(ethtool_fd
, name
, speed
, config
->duplex
);
318 log_device_warning_errno(device
, r
, "Could not set speed to %uMbps, ignoring: %m", speed
);
322 if (config
->duplex
!= _DUP_INVALID
)
323 log_device_warning_errno(device
, r
, "Could not set duplex to %s, ignoring: %m", duplex_to_string(config
->duplex
));
326 r
= ethtool_set_wol(ethtool_fd
, name
, config
->wol
);
328 log_device_warning_errno(device
, r
, "Could not set WakeOnLan to %s, ignoring: %m", wol_to_string(config
->wol
));
330 r
= ethtool_set_features(ethtool_fd
, name
, config
->features
);
332 log_device_warning_errno(device
, r
, "Could not set offload features, ignoring: %m");
334 if (config
->channels
.rx_count_set
|| config
->channels
.tx_count_set
|| config
->channels
.other_count_set
|| config
->channels
.combined_count_set
) {
335 r
= ethtool_set_channels(ethtool_fd
, name
, &config
->channels
);
337 log_device_warning_errno(device
, r
, "Could not set channels, ignoring: %m");
340 if (config
->ring
.rx_pending_set
|| config
->ring
.rx_mini_pending_set
|| config
->ring
.rx_jumbo_pending_set
|| config
->ring
.tx_pending_set
) {
341 r
= ethtool_set_nic_buffer_size(ethtool_fd
, name
, &config
->ring
);
343 log_device_warning_errno(device
, r
, "Could not set ring buffer, ignoring: %m");
346 if (config
->rx_flow_control
>= 0 || config
->tx_flow_control
>= 0 || config
->autoneg_flow_control
>= 0) {
347 r
= ethtool_set_flow_control(ethtool_fd
, name
, config
->rx_flow_control
, config
->tx_flow_control
, config
->autoneg_flow_control
);
349 log_device_warning_errno(device
, r
, "Could not set flow control, ignoring: %m");
355 static int get_mac(sd_device
*device
, MACAddressPolicy policy
, struct ether_addr
*mac
) {
357 bool want_random
= policy
== MAC_ADDRESS_POLICY_RANDOM
;
360 assert(IN_SET(policy
, MAC_ADDRESS_POLICY_RANDOM
, MAC_ADDRESS_POLICY_PERSISTENT
));
362 r
= link_unsigned_attribute(device
, "addr_assign_type", &addr_type
);
367 return log_device_debug(device
, "MAC on the device already set by userspace");
368 case NET_ADDR_STOLEN
:
369 return log_device_debug(device
, "MAC on the device already set based on another device");
370 case NET_ADDR_RANDOM
:
374 log_device_warning(device
, "Unknown addr_assign_type %u, ignoring", addr_type
);
378 if (want_random
== (addr_type
== NET_ADDR_RANDOM
))
379 return log_device_debug(device
, "MAC on the device already matches policy *%s*",
380 mac_address_policy_to_string(policy
));
383 log_device_debug(device
, "Using random bytes to generate MAC");
385 /* We require genuine randomness here, since we want to make sure we won't collide with other
386 * systems booting up at the very same time. We do allow RDRAND however, since this is not
387 * cryptographic key material. */
388 r
= genuine_random_bytes(mac
->ether_addr_octet
, ETH_ALEN
, RANDOM_ALLOW_RDRAND
);
390 return log_device_error_errno(device
, r
, "Failed to acquire random data to generate MAC: %m");
394 r
= net_get_unique_predictable_data(device
,
395 naming_scheme_has(NAMING_STABLE_VIRTUAL_MACS
),
398 return log_device_warning_errno(device
, r
, "Could not generate persistent MAC: %m");
400 log_device_debug(device
, "Using generated persistent MAC address");
401 assert_cc(ETH_ALEN
<= sizeof(result
));
402 memcpy(mac
->ether_addr_octet
, &result
, ETH_ALEN
);
405 /* see eth_random_addr in the kernel */
406 mac
->ether_addr_octet
[0] &= 0xfe; /* clear multicast bit */
407 mac
->ether_addr_octet
[0] |= 0x02; /* set local assignment bit (IEEE802) */
411 static int link_config_apply_rtnl_settings(sd_netlink
**rtnl
, const link_config
*config
, sd_device
*device
) {
412 struct ether_addr generated_mac
, *mac
= NULL
;
419 r
= sd_device_get_ifindex(device
, &ifindex
);
421 return log_device_error_errno(device
, r
, "Could not find ifindex: %m");
423 if (IN_SET(config
->mac_address_policy
, MAC_ADDRESS_POLICY_PERSISTENT
, MAC_ADDRESS_POLICY_RANDOM
)) {
424 if (get_mac(device
, config
->mac_address_policy
, &generated_mac
) > 0)
425 mac
= &generated_mac
;
429 r
= rtnl_set_link_properties(rtnl
, ifindex
, config
->alias
, mac
,
430 config
->txqueues
, config
->rxqueues
, config
->txqueuelen
,
431 config
->mtu
, config
->gso_max_size
, config
->gso_max_segments
);
433 log_device_warning_errno(device
, r
,
434 "Could not set Alias=, MACAddress=, "
435 "TransmitQueues=, ReceiveQueues=, TransmitQueueLength=, MTU=, "
436 "GenericSegmentOffloadMaxBytes= or GenericSegmentOffloadMaxSegments=, "
442 static int link_config_generate_new_name(const link_config_ctx
*ctx
, const link_config
*config
, sd_device
*device
, const char **ret_name
) {
443 unsigned name_type
= NET_NAME_UNKNOWN
;
451 (void) link_unsigned_attribute(device
, "name_assign_type", &name_type
);
453 if (IN_SET(name_type
, NET_NAME_USER
, NET_NAME_RENAMED
)
454 && !naming_scheme_has(NAMING_ALLOW_RERENAMES
)) {
455 log_device_debug(device
, "Device already has a name given by userspace, not renaming.");
459 if (ctx
->enable_name_policy
&& config
->name_policy
)
460 for (NamePolicy
*p
= config
->name_policy
; *p
!= _NAMEPOLICY_INVALID
; p
++) {
461 const char *new_name
= NULL
;
462 NamePolicy policy
= *p
;
465 case NAMEPOLICY_KERNEL
:
466 if (name_type
!= NET_NAME_PREDICTABLE
)
469 /* The kernel claims to have given a predictable name, keep it. */
470 log_device_debug(device
, "Policy *%s*: keeping predictable kernel name",
471 name_policy_to_string(policy
));
473 case NAMEPOLICY_KEEP
:
474 if (!IN_SET(name_type
, NET_NAME_USER
, NET_NAME_RENAMED
))
477 log_device_debug(device
, "Policy *%s*: keeping existing userspace name",
478 name_policy_to_string(policy
));
480 case NAMEPOLICY_DATABASE
:
481 (void) sd_device_get_property_value(device
, "ID_NET_NAME_FROM_DATABASE", &new_name
);
483 case NAMEPOLICY_ONBOARD
:
484 (void) sd_device_get_property_value(device
, "ID_NET_NAME_ONBOARD", &new_name
);
486 case NAMEPOLICY_SLOT
:
487 (void) sd_device_get_property_value(device
, "ID_NET_NAME_SLOT", &new_name
);
489 case NAMEPOLICY_PATH
:
490 (void) sd_device_get_property_value(device
, "ID_NET_NAME_PATH", &new_name
);
493 (void) sd_device_get_property_value(device
, "ID_NET_NAME_MAC", &new_name
);
496 assert_not_reached("invalid policy");
498 if (ifname_valid(new_name
)) {
499 log_device_debug(device
, "Policy *%s* yields \"%s\".", name_policy_to_string(policy
), new_name
);
500 *ret_name
= new_name
;
506 log_device_debug(device
, "Policies didn't yield a name, using specified Name=%s.", config
->name
);
507 *ret_name
= config
->name
;
511 log_device_debug(device
, "Policies didn't yield a name and Name= is not given, not renaming.");
513 r
= sd_device_get_sysname(device
, ret_name
);
515 return log_device_error_errno(device
, r
, "Failed to get sysname: %m");
520 static int link_config_apply_alternative_names(sd_netlink
**rtnl
, const link_config
*config
, sd_device
*device
, const char *new_name
) {
521 _cleanup_strv_free_
char **altnames
= NULL
, **current_altnames
= NULL
;
522 const char *current_name
;
529 r
= sd_device_get_sysname(device
, ¤t_name
);
531 return log_device_error_errno(device
, r
, "Failed to get sysname: %m");
533 r
= sd_device_get_ifindex(device
, &ifindex
);
535 return log_device_error_errno(device
, r
, "Could not find ifindex: %m");
537 if (config
->alternative_names
) {
538 altnames
= strv_copy(config
->alternative_names
);
543 if (config
->alternative_names_policy
)
544 for (NamePolicy
*p
= config
->alternative_names_policy
; *p
!= _NAMEPOLICY_INVALID
; p
++) {
545 const char *n
= NULL
;
548 case NAMEPOLICY_DATABASE
:
549 (void) sd_device_get_property_value(device
, "ID_NET_NAME_FROM_DATABASE", &n
);
551 case NAMEPOLICY_ONBOARD
:
552 (void) sd_device_get_property_value(device
, "ID_NET_NAME_ONBOARD", &n
);
554 case NAMEPOLICY_SLOT
:
555 (void) sd_device_get_property_value(device
, "ID_NET_NAME_SLOT", &n
);
557 case NAMEPOLICY_PATH
:
558 (void) sd_device_get_property_value(device
, "ID_NET_NAME_PATH", &n
);
561 (void) sd_device_get_property_value(device
, "ID_NET_NAME_MAC", &n
);
564 assert_not_reached("invalid policy");
567 r
= strv_extend(&altnames
, n
);
574 strv_remove(altnames
, new_name
);
575 strv_remove(altnames
, current_name
);
577 r
= rtnl_get_link_alternative_names(rtnl
, ifindex
, ¤t_altnames
);
579 log_device_debug_errno(device
, r
, "Failed to get alternative names, ignoring: %m");
582 STRV_FOREACH(p
, current_altnames
)
583 strv_remove(altnames
, *p
);
587 r
= rtnl_set_link_alternative_names(rtnl
, ifindex
, altnames
);
589 log_device_full_errno(device
, r
== -EOPNOTSUPP
? LOG_DEBUG
: LOG_WARNING
, r
,
590 "Could not set AlternativeName= or apply AlternativeNamesPolicy=, ignoring: %m");
595 int link_config_apply(link_config_ctx
*ctx
, const link_config
*config
, sd_device
*device
, const char **ret_name
) {
596 const char *new_name
;
597 sd_device_action_t a
;
605 r
= sd_device_get_action(device
, &a
);
607 return log_device_error_errno(device
, r
, "Failed to get ACTION= property: %m");
609 if (!IN_SET(a
, SD_DEVICE_ADD
, SD_DEVICE_BIND
, SD_DEVICE_MOVE
)) {
610 log_device_debug(device
, "Skipping to apply .link settings on '%s' uevent.", device_action_to_string(a
));
612 r
= sd_device_get_sysname(device
, ret_name
);
614 return log_device_error_errno(device
, r
, "Failed to get sysname: %m");
619 r
= link_config_apply_ethtool_settings(&ctx
->ethtool_fd
, config
, device
);
623 r
= link_config_apply_rtnl_settings(&ctx
->rtnl
, config
, device
);
627 if (a
== SD_DEVICE_MOVE
) {
628 log_device_debug(device
, "Skipping to apply Name= and NamePolicy= on '%s' uevent.", device_action_to_string(a
));
630 r
= sd_device_get_sysname(device
, &new_name
);
632 return log_device_error_errno(device
, r
, "Failed to get sysname: %m");
634 r
= link_config_generate_new_name(ctx
, config
, device
, &new_name
);
639 r
= link_config_apply_alternative_names(&ctx
->rtnl
, config
, device
, new_name
);
643 *ret_name
= new_name
;
647 int link_get_driver(link_config_ctx
*ctx
, sd_device
*device
, char **ret
) {
652 r
= sd_device_get_sysname(device
, &name
);
656 r
= ethtool_get_driver(&ctx
->ethtool_fd
, name
, &driver
);
664 int config_parse_ifalias(
666 const char *filename
,
669 unsigned section_line
,
683 if (!isempty(rvalue
)) {
688 if (!ascii_is_valid(rvalue
)) {
689 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
690 "Interface alias is not ASCII clean, ignoring assignment: %s", rvalue
);
694 if (strlen(rvalue
) >= IFALIASZ
) {
695 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
696 "Interface alias is too long, ignoring assignment: %s", rvalue
);
700 return free_and_strdup_warn(s
, rvalue
);
703 int config_parse_rx_tx_queues(
705 const char *filename
,
708 unsigned section_line
,
715 uint32_t k
, *v
= data
;
718 if (isempty(rvalue
)) {
723 r
= safe_atou32(rvalue
, &k
);
725 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Failed to parse %s=, ignoring assignment: %s.", lvalue
, rvalue
);
728 if (k
== 0 || k
> 4096) {
729 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Invalid %s=, ignoring assignment: %s.", lvalue
, rvalue
);
737 int config_parse_txqueuelen(
739 const char *filename
,
742 unsigned section_line
,
749 uint32_t k
, *v
= data
;
752 if (isempty(rvalue
)) {
757 r
= safe_atou32(rvalue
, &k
);
759 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Failed to parse %s=, ignoring assignment: %s.", lvalue
, rvalue
);
762 if (k
== UINT32_MAX
) {
763 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Invalid %s=, ignoring assignment: %s.", lvalue
, rvalue
);
771 static const char* const mac_address_policy_table
[_MAC_ADDRESS_POLICY_MAX
] = {
772 [MAC_ADDRESS_POLICY_PERSISTENT
] = "persistent",
773 [MAC_ADDRESS_POLICY_RANDOM
] = "random",
774 [MAC_ADDRESS_POLICY_NONE
] = "none",
777 DEFINE_STRING_TABLE_LOOKUP(mac_address_policy
, MACAddressPolicy
);
778 DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT(
779 config_parse_mac_address_policy
,
782 MAC_ADDRESS_POLICY_NONE
,
783 "Failed to parse MAC address policy");
785 static const char* const name_policy_table
[_NAMEPOLICY_MAX
] = {
786 [NAMEPOLICY_KERNEL
] = "kernel",
787 [NAMEPOLICY_KEEP
] = "keep",
788 [NAMEPOLICY_DATABASE
] = "database",
789 [NAMEPOLICY_ONBOARD
] = "onboard",
790 [NAMEPOLICY_SLOT
] = "slot",
791 [NAMEPOLICY_PATH
] = "path",
792 [NAMEPOLICY_MAC
] = "mac",
795 DEFINE_STRING_TABLE_LOOKUP(name_policy
, NamePolicy
);
796 DEFINE_CONFIG_PARSE_ENUMV(config_parse_name_policy
, name_policy
, NamePolicy
,
798 "Failed to parse interface name policy");
800 static const char* const alternative_names_policy_table
[_NAMEPOLICY_MAX
] = {
801 [NAMEPOLICY_DATABASE
] = "database",
802 [NAMEPOLICY_ONBOARD
] = "onboard",
803 [NAMEPOLICY_SLOT
] = "slot",
804 [NAMEPOLICY_PATH
] = "path",
805 [NAMEPOLICY_MAC
] = "mac",
808 DEFINE_STRING_TABLE_LOOKUP(alternative_names_policy
, NamePolicy
);
809 DEFINE_CONFIG_PARSE_ENUMV(config_parse_alternative_names_policy
, alternative_names_policy
, NamePolicy
,
811 "Failed to parse alternative names policy");