diff --git a/man/systemd.link.xml b/man/systemd.link.xml index 0f9e988ba0..2209e96037 100644 --- a/man/systemd.link.xml +++ b/man/systemd.link.xml @@ -356,12 +356,24 @@ + + AlternativeNamesPolicy= + + A space-separated list of policies by which the interface's alternative names + should be set. Each of the policies may fail, and all successful policies are used. The + available policies are database, onboard, + slot, path, and mac. If the + kernel does not support the alternative names, then this setting will be ignored. + + + AlternativeName= The alternative interface name to use. This option can be specified multiple times. If the empty string is assigned to this option, the list is reset, and all prior assignments - have no effect. + have no effect. If the kernel does not support the alternative names, then this setting will + be ignored. diff --git a/network/99-default.link b/network/99-default.link index 347d4b72d2..dc7a42bf58 100644 --- a/network/99-default.link +++ b/network/99-default.link @@ -12,4 +12,5 @@ OriginalName=* [Link] NamePolicy=keep kernel database onboard slot path +AlternativeNamesPolicy=database onboard slot path MACAddressPolicy=persistent diff --git a/src/udev/net/link-config-gperf.gperf b/src/udev/net/link-config-gperf.gperf index e0212bbc18..a1105fa674 100644 --- a/src/udev/net/link-config-gperf.gperf +++ b/src/udev/net/link-config-gperf.gperf @@ -19,40 +19,41 @@ struct ConfigPerfItem; %struct-type %includes %% -Match.MACAddress, config_parse_hwaddrs, 0, offsetof(link_config, match_mac) -Match.OriginalName, config_parse_match_ifnames, 0, offsetof(link_config, match_name) -Match.Path, config_parse_match_strv, 0, offsetof(link_config, match_path) -Match.Driver, config_parse_match_strv, 0, offsetof(link_config, match_driver) -Match.Type, config_parse_match_strv, 0, offsetof(link_config, match_type) -Match.Property, config_parse_match_property, 0, offsetof(link_config, match_property) -Match.Host, config_parse_net_condition, CONDITION_HOST, offsetof(link_config, conditions) -Match.Virtualization, config_parse_net_condition, CONDITION_VIRTUALIZATION, offsetof(link_config, conditions) -Match.KernelCommandLine, config_parse_net_condition, CONDITION_KERNEL_COMMAND_LINE, offsetof(link_config, conditions) -Match.KernelVersion, config_parse_net_condition, CONDITION_KERNEL_VERSION, offsetof(link_config, conditions) -Match.Architecture, config_parse_net_condition, CONDITION_ARCHITECTURE, offsetof(link_config, conditions) -Link.Description, config_parse_string, 0, offsetof(link_config, description) -Link.MACAddressPolicy, config_parse_mac_address_policy, 0, offsetof(link_config, mac_address_policy) -Link.MACAddress, config_parse_hwaddr, 0, offsetof(link_config, mac) -Link.NamePolicy, config_parse_name_policy, 0, offsetof(link_config, name_policy) -Link.Name, config_parse_ifname, 0, offsetof(link_config, name) -Link.AlternativeName, config_parse_ifnames, 1, offsetof(link_config, alternative_names) -Link.Alias, config_parse_ifalias, 0, offsetof(link_config, alias) -Link.MTUBytes, config_parse_mtu, AF_UNSPEC, offsetof(link_config, mtu) -Link.BitsPerSecond, config_parse_si_size, 0, offsetof(link_config, speed) -Link.Duplex, config_parse_duplex, 0, offsetof(link_config, duplex) -Link.AutoNegotiation, config_parse_tristate, 0, offsetof(link_config, autonegotiation) -Link.WakeOnLan, config_parse_wol, 0, offsetof(link_config, wol) -Link.Port, config_parse_port, 0, offsetof(link_config, port) -Link.GenericSegmentationOffload, config_parse_tristate, 0, offsetof(link_config, features[NET_DEV_FEAT_GSO]) -Link.TCPSegmentationOffload, config_parse_tristate, 0, offsetof(link_config, features[NET_DEV_FEAT_TSO]) -Link.TCP6SegmentationOffload, config_parse_tristate, 0, offsetof(link_config, features[NET_DEV_FEAT_TSO6]) -Link.UDPSegmentationOffload, config_parse_warn_compat, DISABLED_LEGACY, 0 -Link.GenericReceiveOffload, config_parse_tristate, 0, offsetof(link_config, features[NET_DEV_FEAT_GRO]) -Link.LargeReceiveOffload, config_parse_tristate, 0, offsetof(link_config, features[NET_DEV_FEAT_LRO]) -Link.RxChannels, config_parse_channel, 0, offsetof(link_config, channels) -Link.TxChannels, config_parse_channel, 0, offsetof(link_config, channels) -Link.OtherChannels, config_parse_channel, 0, offsetof(link_config, channels) -Link.CombinedChannels, config_parse_channel, 0, offsetof(link_config, channels) -Link.Advertise, config_parse_advertise, 0, offsetof(link_config, advertise) -Link.RxBufferSize, config_parse_nic_buffer_size, 0, offsetof(link_config, ring) -Link.TxBufferSize, config_parse_nic_buffer_size, 0, offsetof(link_config, ring) +Match.MACAddress, config_parse_hwaddrs, 0, offsetof(link_config, match_mac) +Match.OriginalName, config_parse_match_ifnames, 0, offsetof(link_config, match_name) +Match.Path, config_parse_match_strv, 0, offsetof(link_config, match_path) +Match.Driver, config_parse_match_strv, 0, offsetof(link_config, match_driver) +Match.Type, config_parse_match_strv, 0, offsetof(link_config, match_type) +Match.Property, config_parse_match_property, 0, offsetof(link_config, match_property) +Match.Host, config_parse_net_condition, CONDITION_HOST, offsetof(link_config, conditions) +Match.Virtualization, config_parse_net_condition, CONDITION_VIRTUALIZATION, offsetof(link_config, conditions) +Match.KernelCommandLine, config_parse_net_condition, CONDITION_KERNEL_COMMAND_LINE, offsetof(link_config, conditions) +Match.KernelVersion, config_parse_net_condition, CONDITION_KERNEL_VERSION, offsetof(link_config, conditions) +Match.Architecture, config_parse_net_condition, CONDITION_ARCHITECTURE, offsetof(link_config, conditions) +Link.Description, config_parse_string, 0, offsetof(link_config, description) +Link.MACAddressPolicy, config_parse_mac_address_policy, 0, offsetof(link_config, mac_address_policy) +Link.MACAddress, config_parse_hwaddr, 0, offsetof(link_config, mac) +Link.NamePolicy, config_parse_name_policy, 0, offsetof(link_config, name_policy) +Link.Name, config_parse_ifname, 0, offsetof(link_config, name) +Link.AlternativeName, config_parse_ifnames, 1, offsetof(link_config, alternative_names) +Link.AlternativeNamesPolicy, config_parse_alternative_names_policy, 0, offsetof(link_config, alternative_names_policy) +Link.Alias, config_parse_ifalias, 0, offsetof(link_config, alias) +Link.MTUBytes, config_parse_mtu, AF_UNSPEC, offsetof(link_config, mtu) +Link.BitsPerSecond, config_parse_si_size, 0, offsetof(link_config, speed) +Link.Duplex, config_parse_duplex, 0, offsetof(link_config, duplex) +Link.AutoNegotiation, config_parse_tristate, 0, offsetof(link_config, autonegotiation) +Link.WakeOnLan, config_parse_wol, 0, offsetof(link_config, wol) +Link.Port, config_parse_port, 0, offsetof(link_config, port) +Link.GenericSegmentationOffload, config_parse_tristate, 0, offsetof(link_config, features[NET_DEV_FEAT_GSO]) +Link.TCPSegmentationOffload, config_parse_tristate, 0, offsetof(link_config, features[NET_DEV_FEAT_TSO]) +Link.TCP6SegmentationOffload, config_parse_tristate, 0, offsetof(link_config, features[NET_DEV_FEAT_TSO6]) +Link.UDPSegmentationOffload, config_parse_warn_compat, DISABLED_LEGACY, 0 +Link.GenericReceiveOffload, config_parse_tristate, 0, offsetof(link_config, features[NET_DEV_FEAT_GRO]) +Link.LargeReceiveOffload, config_parse_tristate, 0, offsetof(link_config, features[NET_DEV_FEAT_LRO]) +Link.RxChannels, config_parse_channel, 0, offsetof(link_config, channels) +Link.TxChannels, config_parse_channel, 0, offsetof(link_config, channels) +Link.OtherChannels, config_parse_channel, 0, offsetof(link_config, channels) +Link.CombinedChannels, config_parse_channel, 0, offsetof(link_config, channels) +Link.Advertise, config_parse_advertise, 0, offsetof(link_config, advertise) +Link.RxBufferSize, config_parse_nic_buffer_size, 0, offsetof(link_config, ring) +Link.TxBufferSize, config_parse_nic_buffer_size, 0, offsetof(link_config, ring) diff --git a/src/udev/net/link-config.c b/src/udev/net/link-config.c index 8d89f1af10..19cecf1895 100644 --- a/src/udev/net/link-config.c +++ b/src/udev/net/link-config.c @@ -59,6 +59,7 @@ static void link_config_free(link_config *link) { free(link->name_policy); free(link->name); strv_free(link->alternative_names); + free(link->alternative_names_policy); free(link->alias); free(link); @@ -326,6 +327,7 @@ static int get_mac(sd_device *device, MACAddressPolicy policy, struct ether_addr int link_config_apply(link_config_ctx *ctx, link_config *config, sd_device *device, const char **name) { + _cleanup_strv_free_ char **altnames = NULL; struct ether_addr generated_mac; struct ether_addr *mac = NULL; const char *new_name = NULL; @@ -401,7 +403,7 @@ int link_config_apply(link_config_ctx *ctx, link_config *config, } if (ctx->enable_name_policy && config->name_policy) - for (NamePolicy *p = config->name_policy; !new_name && *p != _NAMEPOLICY_INVALID; p++) { + for (NamePolicy *p = config->name_policy; *p != _NAMEPOLICY_INVALID; p++) { policy = *p; switch (policy) { @@ -438,6 +440,8 @@ int link_config_apply(link_config_ctx *ctx, link_config *config, default: assert_not_reached("invalid policy"); } + if (ifname_valid(new_name)) + break; } if (new_name) @@ -459,9 +463,52 @@ int link_config_apply(link_config_ctx *ctx, link_config *config, if (r < 0) return log_warning_errno(r, "Could not set Alias=, MACAddress= or MTU= on %s: %m", old_name); - r = rtnl_set_link_alternative_names(&ctx->rtnl, ifindex, config->alternative_names); - if (r < 0) - return log_warning_errno(r, "Could not set AlternativeName= on %s: %m", old_name); + if (config->alternative_names) { + altnames = strv_copy(config->alternative_names); + if (!altnames) + return log_oom(); + } + + if (config->alternative_names_policy) + for (NamePolicy *p = config->alternative_names_policy; *p != _NAMEPOLICY_INVALID; p++) { + const char *n; + + switch (*p) { + case NAMEPOLICY_DATABASE: + (void) sd_device_get_property_value(device, "ID_NET_NAME_FROM_DATABASE", &n); + break; + case NAMEPOLICY_ONBOARD: + (void) sd_device_get_property_value(device, "ID_NET_NAME_ONBOARD", &n); + break; + case NAMEPOLICY_SLOT: + (void) sd_device_get_property_value(device, "ID_NET_NAME_SLOT", &n); + break; + case NAMEPOLICY_PATH: + (void) sd_device_get_property_value(device, "ID_NET_NAME_PATH", &n); + break; + case NAMEPOLICY_MAC: + (void) sd_device_get_property_value(device, "ID_NET_NAME_MAC", &n); + break; + default: + assert_not_reached("invalid policy"); + } + if (!isempty(n)) { + r = strv_extend(&altnames, n); + if (r < 0) + return log_oom(); + } + } + + if (new_name) + strv_remove(altnames, new_name); + strv_remove(altnames, old_name); + strv_uniq(altnames); + + r = rtnl_set_link_alternative_names(&ctx->rtnl, ifindex, altnames); + if (r == -EOPNOTSUPP) + log_debug_errno(r, "Could not set AlternativeName= or apply AlternativeNamesPolicy= on %s, ignoring: %m", old_name); + else if (r < 0) + return log_warning_errno(r, "Could not set AlternativeName= or apply AlternativeNamesPolicy= on %s: %m", old_name); *name = new_name; @@ -509,3 +556,16 @@ DEFINE_STRING_TABLE_LOOKUP(name_policy, NamePolicy); DEFINE_CONFIG_PARSE_ENUMV(config_parse_name_policy, name_policy, NamePolicy, _NAMEPOLICY_INVALID, "Failed to parse interface name policy"); + +static const char* const alternative_names_policy_table[_NAMEPOLICY_MAX] = { + [NAMEPOLICY_DATABASE] = "database", + [NAMEPOLICY_ONBOARD] = "onboard", + [NAMEPOLICY_SLOT] = "slot", + [NAMEPOLICY_PATH] = "path", + [NAMEPOLICY_MAC] = "mac", +}; + +DEFINE_STRING_TABLE_LOOKUP(alternative_names_policy, NamePolicy); +DEFINE_CONFIG_PARSE_ENUMV(config_parse_alternative_names_policy, alternative_names_policy, NamePolicy, + _NAMEPOLICY_INVALID, + "Failed to parse alternative names policy"); diff --git a/src/udev/net/link-config.h b/src/udev/net/link-config.h index b5073548ab..1c3a3f7fd4 100644 --- a/src/udev/net/link-config.h +++ b/src/udev/net/link-config.h @@ -47,6 +47,7 @@ struct link_config { struct ether_addr *mac; MACAddressPolicy mac_address_policy; NamePolicy *name_policy; + NamePolicy *alternative_names_policy; char *name; char **alternative_names; char *alias; @@ -79,6 +80,9 @@ int link_get_driver(link_config_ctx *ctx, sd_device *device, char **ret); const char *name_policy_to_string(NamePolicy p) _const_; NamePolicy name_policy_from_string(const char *p) _pure_; +const char *alternative_names_policy_to_string(NamePolicy p) _const_; +NamePolicy alternative_names_policy_from_string(const char *p) _pure_; + const char *mac_address_policy_to_string(MACAddressPolicy p) _const_; MACAddressPolicy mac_address_policy_from_string(const char *p) _pure_; @@ -87,3 +91,4 @@ const struct ConfigPerfItem* link_config_gperf_lookup(const char *key, GPERF_LEN CONFIG_PARSER_PROTOTYPE(config_parse_mac_address_policy); CONFIG_PARSER_PROTOTYPE(config_parse_name_policy); +CONFIG_PARSER_PROTOTYPE(config_parse_alternative_names_policy); diff --git a/src/udev/udev-builtin-net_id.c b/src/udev/udev-builtin-net_id.c index e12364acee..da3fcc97e8 100644 --- a/src/udev/udev-builtin-net_id.c +++ b/src/udev/udev-builtin-net_id.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include "alloc-util.h" @@ -56,22 +57,22 @@ struct netnames { bool mac_valid; sd_device *pcidev; - char pci_slot[IFNAMSIZ]; - char pci_path[IFNAMSIZ]; - char pci_onboard[IFNAMSIZ]; + char pci_slot[ALTIFNAMSIZ]; + char pci_path[ALTIFNAMSIZ]; + char pci_onboard[ALTIFNAMSIZ]; const char *pci_onboard_label; - char usb_ports[IFNAMSIZ]; - char bcma_core[IFNAMSIZ]; - char ccw_busid[IFNAMSIZ]; - char vio_slot[IFNAMSIZ]; - char platform_path[IFNAMSIZ]; - char netdevsim_path[IFNAMSIZ]; + char usb_ports[ALTIFNAMSIZ]; + char bcma_core[ALTIFNAMSIZ]; + char ccw_busid[ALTIFNAMSIZ]; + char vio_slot[ALTIFNAMSIZ]; + char platform_path[ALTIFNAMSIZ]; + char netdevsim_path[ALTIFNAMSIZ]; }; struct virtfn_info { sd_device *physfn_pcidev; - char suffix[IFNAMSIZ]; + char suffix[ALTIFNAMSIZ]; }; /* skip intermediate virtio devices */ @@ -104,7 +105,7 @@ static int get_virtfn_info(sd_device *dev, struct netnames *names, struct virtfn _cleanup_free_ char *virtfn_pci_syspath = NULL; struct dirent *dent; _cleanup_closedir_ DIR *dir = NULL; - char suffix[IFNAMSIZ]; + char suffix[ALTIFNAMSIZ]; int r; assert(dev); @@ -819,7 +820,7 @@ static int builtin_net_id(sd_device *dev, int argc, char *argv[], bool test) { r = names_mac(dev, &names); if (r >= 0 && names.mac_valid) { - char str[IFNAMSIZ]; + char str[ALTIFNAMSIZ]; xsprintf(str, "%sx%02x%02x%02x%02x%02x%02x", prefix, names.mac[0], names.mac[1], names.mac[2], @@ -831,7 +832,7 @@ static int builtin_net_id(sd_device *dev, int argc, char *argv[], bool test) { /* get path names for Linux on System z network devices */ if (names_ccw(dev, &names) >= 0 && names.type == NET_CCW) { - char str[IFNAMSIZ]; + char str[ALTIFNAMSIZ]; if (snprintf_ok(str, sizeof str, "%s%s", prefix, names.ccw_busid)) udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str); @@ -840,7 +841,7 @@ static int builtin_net_id(sd_device *dev, int argc, char *argv[], bool test) { /* get ibmveth/ibmvnic slot-based names. */ if (names_vio(dev, &names) >= 0 && names.type == NET_VIO) { - char str[IFNAMSIZ]; + char str[ALTIFNAMSIZ]; if (snprintf_ok(str, sizeof str, "%s%s", prefix, names.vio_slot)) udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", str); @@ -849,7 +850,7 @@ static int builtin_net_id(sd_device *dev, int argc, char *argv[], bool test) { /* get ACPI path names for ARM64 platform devices */ if (names_platform(dev, &names, test) >= 0 && names.type == NET_PLATFORM) { - char str[IFNAMSIZ]; + char str[ALTIFNAMSIZ]; if (snprintf_ok(str, sizeof str, "%s%s", prefix, names.platform_path)) udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str); @@ -858,7 +859,7 @@ static int builtin_net_id(sd_device *dev, int argc, char *argv[], bool test) { /* get netdevsim path names */ if (names_netdevsim(dev, &names) >= 0 && names.type == NET_NETDEVSIM) { - char str[IFNAMSIZ]; + char str[ALTIFNAMSIZ]; if (snprintf_ok(str, sizeof str, "%s%s", prefix, names.netdevsim_path)) udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str); @@ -872,7 +873,7 @@ static int builtin_net_id(sd_device *dev, int argc, char *argv[], bool test) { /* plain PCI device */ if (names.type == NET_PCI) { - char str[IFNAMSIZ]; + char str[ALTIFNAMSIZ]; if (names.pci_onboard[0] && snprintf_ok(str, sizeof str, "%s%s", prefix, names.pci_onboard)) @@ -896,7 +897,7 @@ static int builtin_net_id(sd_device *dev, int argc, char *argv[], bool test) { /* USB device */ if (names_usb(dev, &names) >= 0 && names.type == NET_USB) { - char str[IFNAMSIZ]; + char str[ALTIFNAMSIZ]; if (names.pci_path[0] && snprintf_ok(str, sizeof str, "%s%s%s", prefix, names.pci_path, names.usb_ports)) @@ -910,7 +911,7 @@ static int builtin_net_id(sd_device *dev, int argc, char *argv[], bool test) { /* Broadcom bus */ if (names_bcma(dev, &names) >= 0 && names.type == NET_BCMA) { - char str[IFNAMSIZ]; + char str[ALTIFNAMSIZ]; if (names.pci_path[0] && snprintf_ok(str, sizeof str, "%s%s%s", prefix, names.pci_path, names.bcma_core)) diff --git a/test/fuzz/fuzz-link-parser/directives.link b/test/fuzz/fuzz-link-parser/directives.link index 5c26129b70..abf21f678a 100644 --- a/test/fuzz/fuzz-link-parser/directives.link +++ b/test/fuzz/fuzz-link-parser/directives.link @@ -16,6 +16,7 @@ MACAddressPolicy= MACAddress= NamePolicy= Name= +AlternativeNamesPolicy= AlternativeName= Alias= MTUBytes=