1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include <linux/netdevice.h>
4 #include <netinet/ether.h>
7 #include "sd-netlink.h"
9 #include "alloc-util.h"
10 #include "conf-files.h"
11 #include "conf-parser.h"
13 #include "device-util.h"
14 #include "ethtool-util.h"
16 #include "link-config.h"
18 #include "memory-util.h"
19 #include "naming-scheme.h"
20 #include "netlink-util.h"
21 #include "network-internal.h"
22 #include "parse-util.h"
23 #include "path-util.h"
24 #include "proc-cmdline.h"
25 #include "random-util.h"
26 #include "stat-util.h"
27 #include "string-table.h"
28 #include "string-util.h"
31 struct link_config_ctx
{
32 LIST_HEAD(link_config
, links
);
36 bool enable_name_policy
;
40 usec_t network_dirs_ts_usec
;
43 static void link_config_free(link_config
*link
) {
49 set_free_free(link
->match_mac
);
50 strv_free(link
->match_path
);
51 strv_free(link
->match_driver
);
52 strv_free(link
->match_type
);
53 strv_free(link
->match_name
);
54 strv_free(link
->match_property
);
55 condition_free_list(link
->conditions
);
57 free(link
->description
);
59 free(link
->name_policy
);
61 strv_free(link
->alternative_names
);
67 DEFINE_TRIVIAL_CLEANUP_FUNC(link_config
*, link_config_free
);
69 static void link_configs_free(link_config_ctx
*ctx
) {
70 link_config
*link
, *link_next
;
75 LIST_FOREACH_SAFE(links
, link
, link_next
, ctx
->links
)
76 link_config_free(link
);
79 void link_config_ctx_free(link_config_ctx
*ctx
) {
83 safe_close(ctx
->ethtool_fd
);
85 sd_netlink_unref(ctx
->rtnl
);
87 link_configs_free(ctx
);
94 int link_config_ctx_new(link_config_ctx
**ret
) {
95 _cleanup_(link_config_ctx_freep
) link_config_ctx
*ctx
= NULL
;
100 ctx
= new0(link_config_ctx
, 1);
104 LIST_HEAD_INIT(ctx
->links
);
106 ctx
->ethtool_fd
= -1;
108 ctx
->enable_name_policy
= true;
110 *ret
= TAKE_PTR(ctx
);
115 int link_load_one(link_config_ctx
*ctx
, const char *filename
) {
116 _cleanup_(link_config_freep
) link_config
*link
= NULL
;
117 _cleanup_fclose_
FILE *file
= NULL
;
118 _cleanup_free_
char *name
= NULL
;
125 file
= fopen(filename
, "re");
127 return errno
== ENOENT
? 0 : -errno
;
129 if (null_or_empty_fd(fileno(file
))) {
130 log_debug("Skipping empty file: %s", filename
);
134 name
= strdup(filename
);
138 link
= new(link_config
, 1);
142 *link
= (link_config
) {
143 .filename
= TAKE_PTR(name
),
144 .mac_address_policy
= _MAC_ADDRESS_POLICY_INVALID
,
146 .duplex
= _DUP_INVALID
,
147 .port
= _NET_DEV_PORT_INVALID
,
148 .autonegotiation
= -1,
151 for (i
= 0; i
< ELEMENTSOF(link
->features
); i
++)
152 link
->features
[i
] = -1;
154 r
= config_parse(NULL
, filename
, file
,
156 config_item_perf_lookup
, link_config_gperf_lookup
,
157 CONFIG_PARSE_WARN
, link
);
161 if (link
->speed
> UINT_MAX
)
164 if (set_isempty(link
->match_mac
) && strv_isempty(link
->match_path
) &&
165 strv_isempty(link
->match_driver
) && strv_isempty(link
->match_type
) &&
166 strv_isempty(link
->match_name
) && strv_isempty(link
->match_property
) && !link
->conditions
)
167 log_warning("%s: No valid settings found in the [Match] section. "
168 "The file will match all interfaces. "
169 "If that is intended, please add OriginalName=* in the [Match] section.",
172 if (!condition_test_list(link
->conditions
, NULL
, NULL
, NULL
)) {
173 log_debug("%s: Conditions do not match the system environment, skipping.", filename
);
177 log_debug("Parsed configuration file %s", filename
);
179 LIST_PREPEND(links
, ctx
->links
, TAKE_PTR(link
));
183 static bool enable_name_policy(void) {
186 return proc_cmdline_get_bool("net.ifnames", &b
) <= 0 || b
;
189 static int link_unsigned_attribute(sd_device
*device
, const char *attr
, unsigned *type
) {
193 r
= sd_device_get_sysattr_value(device
, attr
, &s
);
195 return log_device_debug_errno(device
, r
, "Failed to query %s: %m", attr
);
197 r
= safe_atou(s
, type
);
199 return log_device_warning_errno(device
, r
, "Failed to parse %s \"%s\": %m", attr
, s
);
201 log_device_debug(device
, "Device has %s=%u", attr
, *type
);
205 int link_config_load(link_config_ctx
*ctx
) {
206 _cleanup_strv_free_
char **files
;
210 link_configs_free(ctx
);
212 if (!enable_name_policy()) {
213 ctx
->enable_name_policy
= false;
214 log_info("Network interface NamePolicy= disabled on kernel command line, ignoring.");
217 /* update timestamp */
218 paths_check_timestamp(NETWORK_DIRS
, &ctx
->network_dirs_ts_usec
, true);
220 r
= conf_files_list_strv(&files
, ".link", NULL
, 0, NETWORK_DIRS
);
222 return log_error_errno(r
, "failed to enumerate link files: %m");
224 STRV_FOREACH_BACKWARDS(f
, files
) {
225 r
= link_load_one(ctx
, *f
);
227 log_error_errno(r
, "Failed to load %s, ignoring: %m", *f
);
233 bool link_config_should_reload(link_config_ctx
*ctx
) {
234 return paths_check_timestamp(NETWORK_DIRS
, &ctx
->network_dirs_ts_usec
, false);
237 int link_config_get(link_config_ctx
*ctx
, sd_device
*device
, link_config
**ret
) {
244 LIST_FOREACH(links
, link
, ctx
->links
) {
245 if (net_match_config(link
->match_mac
, link
->match_path
, link
->match_driver
,
246 link
->match_type
, link
->match_name
, link
->match_property
, NULL
, NULL
, NULL
,
247 device
, NULL
, NULL
, NULL
, 0, NULL
, NULL
)) {
248 if (link
->match_name
&& !strv_contains(link
->match_name
, "*")) {
249 unsigned name_assign_type
= NET_NAME_UNKNOWN
;
251 (void) link_unsigned_attribute(device
, "name_assign_type", &name_assign_type
);
253 if (name_assign_type
== NET_NAME_ENUM
) {
254 log_device_warning(device
, "Config file %s applies to device based on potentially unpredictable interface name",
259 } else if (name_assign_type
== NET_NAME_RENAMED
) {
260 log_device_warning(device
, "Config file %s matches device based on renamed interface name, ignoring",
267 log_device_debug(device
, "Config file %s is applied", link
->filename
);
278 static int get_mac(sd_device
*device
, MACAddressPolicy policy
, struct ether_addr
*mac
) {
280 bool want_random
= policy
== MAC_ADDRESS_POLICY_RANDOM
;
283 assert(IN_SET(policy
, MAC_ADDRESS_POLICY_RANDOM
, MAC_ADDRESS_POLICY_PERSISTENT
));
285 r
= link_unsigned_attribute(device
, "addr_assign_type", &addr_type
);
290 return log_device_debug(device
, "MAC on the device already set by userspace");
291 case NET_ADDR_STOLEN
:
292 return log_device_debug(device
, "MAC on the device already set based on another device");
293 case NET_ADDR_RANDOM
:
297 return log_device_warning(device
, "Unknown addr_assign_type %u, ignoring", addr_type
);
300 if (want_random
== (addr_type
== NET_ADDR_RANDOM
))
301 return log_device_debug(device
, "MAC on the device already matches policy *%s*",
302 mac_address_policy_to_string(policy
));
305 log_device_debug(device
, "Using random bytes to generate MAC");
306 random_bytes(mac
->ether_addr_octet
, ETH_ALEN
);
310 r
= net_get_unique_predictable_data(device
,
311 naming_scheme_has(NAMING_STABLE_VIRTUAL_MACS
),
314 return log_device_warning_errno(device
, r
, "Could not generate persistent MAC: %m");
316 log_device_debug(device
, "Using generated persistent MAC address");
317 assert_cc(ETH_ALEN
<= sizeof(result
));
318 memcpy(mac
->ether_addr_octet
, &result
, ETH_ALEN
);
321 /* see eth_random_addr in the kernel */
322 mac
->ether_addr_octet
[0] &= 0xfe; /* clear multicast bit */
323 mac
->ether_addr_octet
[0] |= 0x02; /* set local assignment bit (IEEE802) */
327 int link_config_apply(link_config_ctx
*ctx
, link_config
*config
,
328 sd_device
*device
, const char **name
) {
329 struct ether_addr generated_mac
;
330 struct ether_addr
*mac
= NULL
;
331 const char *new_name
= NULL
;
332 const char *old_name
;
333 unsigned speed
, name_type
= NET_NAME_UNKNOWN
;
342 r
= sd_device_get_sysname(device
, &old_name
);
346 r
= ethtool_set_glinksettings(&ctx
->ethtool_fd
, old_name
,
347 config
->autonegotiation
, config
->advertise
,
348 config
->speed
, config
->duplex
, config
->port
);
351 if (config
->port
!= _NET_DEV_PORT_INVALID
)
352 log_warning_errno(r
, "Could not set port (%s) of %s: %m", port_to_string(config
->port
), old_name
);
354 if (!eqzero(config
->advertise
))
355 log_warning_errno(r
, "Could not set advertise mode: %m"); /* TODO: include modes in the log message. */
358 speed
= DIV_ROUND_UP(config
->speed
, 1000000);
359 if (r
== -EOPNOTSUPP
) {
360 r
= ethtool_set_speed(&ctx
->ethtool_fd
, old_name
, speed
, config
->duplex
);
362 log_warning_errno(r
, "Could not set speed of %s to %u Mbps: %m", old_name
, speed
);
366 if (config
->duplex
!= _DUP_INVALID
)
367 log_warning_errno(r
, "Could not set duplex of %s to %s: %m", old_name
, duplex_to_string(config
->duplex
));
370 r
= ethtool_set_wol(&ctx
->ethtool_fd
, old_name
, config
->wol
);
372 log_warning_errno(r
, "Could not set WakeOnLan of %s to %s: %m",
373 old_name
, wol_to_string(config
->wol
));
375 r
= ethtool_set_features(&ctx
->ethtool_fd
, old_name
, config
->features
);
377 log_warning_errno(r
, "Could not set offload features of %s: %m", old_name
);
379 if (config
->channels
.rx_count_set
|| config
->channels
.tx_count_set
|| config
->channels
.other_count_set
|| config
->channels
.combined_count_set
) {
380 r
= ethtool_set_channels(&ctx
->ethtool_fd
, old_name
, &config
->channels
);
382 log_warning_errno(r
, "Could not set channels of %s: %m", old_name
);
385 if (config
->ring
.rx_pending_set
|| config
->ring
.tx_pending_set
) {
386 r
= ethtool_set_nic_buffer_size(&ctx
->ethtool_fd
, old_name
, &config
->ring
);
388 log_warning_errno(r
, "Could not set ring buffer of %s: %m", old_name
);
391 r
= sd_device_get_ifindex(device
, &ifindex
);
393 return log_device_warning_errno(device
, r
, "Could not find ifindex: %m");
395 (void) link_unsigned_attribute(device
, "name_assign_type", &name_type
);
397 if (IN_SET(name_type
, NET_NAME_USER
, NET_NAME_RENAMED
)
398 && !naming_scheme_has(NAMING_ALLOW_RERENAMES
)) {
399 log_device_debug(device
, "Device already has a name given by userspace, not renaming.");
403 if (ctx
->enable_name_policy
&& config
->name_policy
)
404 for (NamePolicy
*p
= config
->name_policy
; !new_name
&& *p
!= _NAMEPOLICY_INVALID
; p
++) {
408 case NAMEPOLICY_KERNEL
:
409 if (name_type
!= NET_NAME_PREDICTABLE
)
412 /* The kernel claims to have given a predictable name, keep it. */
413 log_device_debug(device
, "Policy *%s*: keeping predictable kernel name",
414 name_policy_to_string(policy
));
416 case NAMEPOLICY_KEEP
:
417 if (!IN_SET(name_type
, NET_NAME_USER
, NET_NAME_RENAMED
))
420 log_device_debug(device
, "Policy *%s*: keeping existing userspace name",
421 name_policy_to_string(policy
));
423 case NAMEPOLICY_DATABASE
:
424 (void) sd_device_get_property_value(device
, "ID_NET_NAME_FROM_DATABASE", &new_name
);
426 case NAMEPOLICY_ONBOARD
:
427 (void) sd_device_get_property_value(device
, "ID_NET_NAME_ONBOARD", &new_name
);
429 case NAMEPOLICY_SLOT
:
430 (void) sd_device_get_property_value(device
, "ID_NET_NAME_SLOT", &new_name
);
432 case NAMEPOLICY_PATH
:
433 (void) sd_device_get_property_value(device
, "ID_NET_NAME_PATH", &new_name
);
436 (void) sd_device_get_property_value(device
, "ID_NET_NAME_MAC", &new_name
);
439 assert_not_reached("invalid policy");
444 log_device_debug(device
, "Policy *%s* yields \"%s\".", name_policy_to_string(policy
), new_name
);
445 else if (config
->name
) {
446 new_name
= config
->name
;
447 log_device_debug(device
, "Policies didn't yield a name, using specified Name=%s.", new_name
);
449 log_device_debug(device
, "Policies didn't yield a name and Name= is not given, not renaming.");
452 if (IN_SET(config
->mac_address_policy
, MAC_ADDRESS_POLICY_PERSISTENT
, MAC_ADDRESS_POLICY_RANDOM
)) {
453 if (get_mac(device
, config
->mac_address_policy
, &generated_mac
) > 0)
454 mac
= &generated_mac
;
458 r
= rtnl_set_link_properties(&ctx
->rtnl
, ifindex
, config
->alias
, mac
, config
->mtu
);
460 return log_warning_errno(r
, "Could not set Alias=, MACAddress= or MTU= on %s: %m", old_name
);
462 r
= rtnl_set_link_alternative_names(&ctx
->rtnl
, ifindex
, config
->alternative_names
);
464 return log_warning_errno(r
, "Could not set AlternativeName= on %s: %m", old_name
);
471 int link_get_driver(link_config_ctx
*ctx
, sd_device
*device
, char **ret
) {
476 r
= sd_device_get_sysname(device
, &name
);
480 r
= ethtool_get_driver(&ctx
->ethtool_fd
, name
, &driver
);
488 static const char* const mac_address_policy_table
[_MAC_ADDRESS_POLICY_MAX
] = {
489 [MAC_ADDRESS_POLICY_PERSISTENT
] = "persistent",
490 [MAC_ADDRESS_POLICY_RANDOM
] = "random",
491 [MAC_ADDRESS_POLICY_NONE
] = "none",
494 DEFINE_STRING_TABLE_LOOKUP(mac_address_policy
, MACAddressPolicy
);
495 DEFINE_CONFIG_PARSE_ENUM(config_parse_mac_address_policy
, mac_address_policy
, MACAddressPolicy
,
496 "Failed to parse MAC address policy");
498 static const char* const name_policy_table
[_NAMEPOLICY_MAX
] = {
499 [NAMEPOLICY_KERNEL
] = "kernel",
500 [NAMEPOLICY_KEEP
] = "keep",
501 [NAMEPOLICY_DATABASE
] = "database",
502 [NAMEPOLICY_ONBOARD
] = "onboard",
503 [NAMEPOLICY_SLOT
] = "slot",
504 [NAMEPOLICY_PATH
] = "path",
505 [NAMEPOLICY_MAC
] = "mac",
508 DEFINE_STRING_TABLE_LOOKUP(name_policy
, NamePolicy
);
509 DEFINE_CONFIG_PARSE_ENUMV(config_parse_name_policy
, name_policy
, NamePolicy
,
511 "Failed to parse interface name policy");