]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
ethtool-util: introduce ethtool_get_permanent_hw_addr()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 4 Nov 2021 17:13:37 +0000 (02:13 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 12 Nov 2021 23:53:33 +0000 (08:53 +0900)
And replaces all ethtool_get_permanent_macaddr() with it.

src/network/networkctl.c
src/network/networkd-link.c
src/network/networkd-link.h
src/shared/ethtool-util.c
src/shared/ethtool-util.h
src/udev/net/link-config.c

index abf8e234b062ad6c2b1e64ae2cbfff29b60562de..14319007aa4dac33ba2b957e55d5169f9ca3c78f 100644 (file)
@@ -273,7 +273,7 @@ typedef struct LinkInfo {
         int ifindex;
         unsigned short iftype;
         struct hw_addr_data hw_address;
-        struct ether_addr permanent_mac_address;
+        struct hw_addr_data permanent_hw_address;
         uint32_t master;
         uint32_t mtu;
         uint32_t min_mtu;
@@ -348,7 +348,7 @@ typedef struct LinkInfo {
         struct ether_addr bssid;
 
         bool has_hw_address:1;
-        bool has_permanent_mac_address:1;
+        bool has_permanent_hw_address:1;
         bool has_tx_queues:1;
         bool has_rx_queues:1;
         bool has_stats64:1;
@@ -556,11 +556,10 @@ static int decode_link(sd_netlink_message *m, LinkInfo *info, char **patterns, b
                 netlink_message_read_hw_addr(m, IFLA_ADDRESS, &info->hw_address) >= 0 &&
                 info->hw_address.length > 0;
 
-        info->has_permanent_mac_address =
-                ethtool_get_permanent_macaddr(NULL, info->name, &info->permanent_mac_address) >= 0 &&
-                !ether_addr_is_null(&info->permanent_mac_address) &&
-                (info->hw_address.length != sizeof(struct ether_addr) ||
-                 memcmp(&info->permanent_mac_address, info->hw_address.bytes, sizeof(struct ether_addr)) != 0);
+        info->has_permanent_hw_address =
+                ethtool_get_permanent_hw_addr(NULL, info->name, &info->permanent_hw_address) >= 0 &&
+                !hw_addr_is_null(&info->permanent_hw_address) &&
+                !hw_addr_equal(&info->permanent_hw_address, &info->hw_address);
 
         (void) sd_netlink_message_read_u32(m, IFLA_MTU, &info->mtu);
         (void) sd_netlink_message_read_u32(m, IFLA_MIN_MTU, &info->min_mtu);
@@ -1710,23 +1709,10 @@ static int link_status_one(
                         return r;
         }
 
-        if (info->has_permanent_mac_address) {
-                _cleanup_free_ char *description = NULL;
-
-                (void) ieee_oui(hwdb, &info->permanent_mac_address, &description);
-
-                r = table_add_many(table,
-                                   TABLE_EMPTY,
-                                   TABLE_STRING, "Permanent Hardware Address:");
+        if (info->has_permanent_hw_address) {
+                r = dump_hw_address(table, hwdb, "Permanent Hardware Address:", &info->permanent_hw_address);
                 if (r < 0)
-                        return table_log_add_error(r);
-                r = table_add_cell_stringf(table, NULL, "%s%s%s%s",
-                                           ETHER_ADDR_TO_STR(&info->permanent_mac_address),
-                                           description ? " (" : "",
-                                           strempty(description),
-                                           description ? ")" : "");
-                if (r < 0)
-                        return table_log_add_error(r);
+                        return r;
         }
 
         if (info->mtu > 0) {
index 5136a8820e3be7c7251cf8352fb459926c866dd2..b4a818be9b0b18c43d45a9b7dcf28a1b8ea6ca9f 100644 (file)
@@ -1196,8 +1196,8 @@ static int link_get_network(Link *link, Network **ret) {
                 r = net_match_config(
                                 &network->match,
                                 link->sd_device,
-                                &link->hw_addr.ether,
-                                &link->permanent_mac,
+                                link->hw_addr.length == ETH_ALEN ? &link->hw_addr.ether : NULL,
+                                link->permanent_hw_addr.length == ETH_ALEN ? &link->permanent_hw_addr.ether : NULL,
                                 link->driver,
                                 link->iftype,
                                 link->ifname,
@@ -2400,9 +2400,9 @@ static int link_new(Manager *manager, sd_netlink_message *message, Link **ret) {
         if (r < 0)
                 return log_link_debug_errno(link, r, "Failed to manage link by its interface name: %m");
 
-        r = ethtool_get_permanent_macaddr(&manager->ethtool_fd, link->ifname, &link->permanent_mac);
+        r = ethtool_get_permanent_hw_addr(&manager->ethtool_fd, link->ifname, &link->permanent_hw_addr);
         if (r < 0)
-                log_link_debug_errno(link, r, "Permanent MAC address not found for new device, continuing without: %m");
+                log_link_debug_errno(link, r, "Permanent hardware address not found, continuing without: %m");
 
         r = ethtool_get_driver(&manager->ethtool_fd, link->ifname, &link->driver);
         if (r < 0)
index d02c4fbd1e324a0e7a7a2de23a07cb07c81bba54..7006f3df65e541088df02ee7ddd7ca96fdb4735c 100644 (file)
@@ -55,7 +55,7 @@ typedef struct Link {
         char *state_file;
         struct hw_addr_data hw_addr;
         struct hw_addr_data bcast_addr;
-        struct ether_addr permanent_mac;
+        struct hw_addr_data permanent_hw_addr;
         struct in6_addr ipv6ll_address;
         uint32_t mtu;
         uint32_t min_mtu;
index 333e5a487977c2792571238f04c6be10def51fac..8afb51328e5285c03dd1b9de7ffbd8b27c343caa 100644 (file)
@@ -339,14 +339,14 @@ int ethtool_get_link_info(
         return 0;
 }
 
-int ethtool_get_permanent_macaddr(int *ethtool_fd, const char *ifname, struct ether_addr *ret) {
+int ethtool_get_permanent_hw_addr(int *ethtool_fd, const char *ifname, struct hw_addr_data *ret) {
         _cleanup_close_ int fd = -1;
         struct {
                 struct ethtool_perm_addr addr;
-                uint8_t space[MAX_ADDR_LEN];
+                uint8_t space[HW_ADDR_MAX_SIZE];
         } epaddr = {
                 .addr.cmd = ETHTOOL_GPERMADDR,
-                .addr.size = MAX_ADDR_LEN,
+                .addr.size = HW_ADDR_MAX_SIZE,
         };
         struct ifreq ifr = {
                 .ifr_data = (caddr_t) &epaddr,
@@ -367,17 +367,14 @@ int ethtool_get_permanent_macaddr(int *ethtool_fd, const char *ifname, struct et
         if (ioctl(*ethtool_fd, SIOCETHTOOL, &ifr) < 0)
                 return -errno;
 
-        if (epaddr.addr.size != 6)
-                return -EOPNOTSUPP;
+        if (epaddr.addr.size == 0)
+                return -ENODATA;
 
-#pragma GCC diagnostic push
-#if HAVE_ZERO_LENGTH_BOUNDS
-#  pragma GCC diagnostic ignored "-Wzero-length-bounds"
-#endif
-        for (size_t i = 0; i < epaddr.addr.size; i++)
-                ret->ether_addr_octet[i] = epaddr.addr.data[i];
-#pragma GCC diagnostic pop
+        if (epaddr.addr.size > HW_ADDR_MAX_SIZE)
+                return -EINVAL;
 
+        ret->length = epaddr.addr.size;
+        memcpy(ret->bytes, epaddr.addr.data, epaddr.addr.size);
         return 0;
 }
 
index c57ff1a2b5abcc231df26fb3d52a6f5ea8b1812a..8ca221bc090d736f2f206a2018e042376c8fb4c7 100644 (file)
@@ -6,6 +6,7 @@
 #include <linux/ethtool.h>
 
 #include "conf-parser.h"
+#include "ether-addr-util.h"
 
 #define N_ADVERTISE 3
 
@@ -163,7 +164,7 @@ int ethtool_get_driver(int *ethtool_fd, const char *ifname, char **ret);
 int ethtool_get_link_info(int *ethtool_fd, const char *ifname,
                           int *ret_autonegotiation, uint64_t *ret_speed,
                           Duplex *ret_duplex, NetDevPort *ret_port);
-int ethtool_get_permanent_macaddr(int *ethtool_fd, const char *ifname, struct ether_addr *ret);
+int ethtool_get_permanent_hw_addr(int *ethtool_fd, const char *ifname, struct hw_addr_data *ret);
 int ethtool_set_wol(int *ethtool_fd, const char *ifname, uint32_t wolopts, const uint8_t password[SOPASS_MAX]);
 int ethtool_set_nic_buffer_size(int *ethtool_fd, const char *ifname, const netdev_ring_param *ring);
 int ethtool_set_features(int *ethtool_fd, const char *ifname, const int features[static _NET_DEV_FEAT_MAX]);
index f91d2b6df1cbf1e216b560f5d7e067957dcfd0a0..966c2060d71eebb36a42a2f7acff3c5b8c5ad419 100644 (file)
@@ -349,7 +349,7 @@ bool link_config_should_reload(LinkConfigContext *ctx) {
 
 int link_config_get(LinkConfigContext *ctx, sd_netlink **rtnl, sd_device *device, LinkConfig **ret) {
         unsigned name_assign_type = NET_NAME_UNKNOWN;
-        struct ether_addr permanent_mac = {};
+        struct hw_addr_data permanent_hw_addr = {};
         unsigned short iftype;
         LinkConfig *link;
         const char *name;
@@ -377,14 +377,16 @@ int link_config_get(LinkConfigContext *ctx, sd_netlink **rtnl, sd_device *device
         if (flags & IFF_LOOPBACK)
                 return -ENOENT;
 
-        r = ethtool_get_permanent_macaddr(&ctx->ethtool_fd, name, &permanent_mac);
+        r = ethtool_get_permanent_hw_addr(&ctx->ethtool_fd, name, &permanent_hw_addr);
         if (r < 0)
-                log_device_debug_errno(device, r, "Failed to get permanent MAC address, ignoring: %m");
+                log_device_debug_errno(device, r, "Failed to get permanent hardware address, ignoring: %m");
 
         (void) link_unsigned_attribute(device, "name_assign_type", &name_assign_type);
 
         LIST_FOREACH(links, link, ctx->links) {
-                r = net_match_config(&link->match, device, NULL, &permanent_mac, NULL, iftype, NULL, NULL, 0, NULL, NULL);
+                r = net_match_config(&link->match, device, NULL,
+                                     permanent_hw_addr.length == ETH_ALEN ? &permanent_hw_addr.ether : NULL,
+                                     NULL, iftype, NULL, NULL, 0, NULL, NULL);
                 if (r < 0)
                         return r;
                 if (r == 0)