Merge pull request #21277 from yuwata/permanent-hw-addr

network, udev: several improvement of permanent hardware address handling
This commit is contained in:
Luca Boccassi
2021-11-15 10:57:30 +00:00
committed by GitHub
10 changed files with 119 additions and 76 deletions
+43 -42
View 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;
@@ -347,8 +347,8 @@ typedef struct LinkInfo {
char *ssid;
struct ether_addr bssid;
bool has_mac_address:1;
bool has_permanent_mac_address:1;
bool has_hw_address:1;
bool has_permanent_hw_address:1;
bool has_tx_queues:1;
bool has_rx_queues:1;
bool has_stats64:1;
@@ -552,15 +552,15 @@ static int decode_link(sd_netlink_message *m, LinkInfo *info, char **patterns, b
info->ifindex = ifindex;
info->alternative_names = TAKE_PTR(altnames);
info->has_mac_address =
info->has_hw_address =
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 =
(netlink_message_read_hw_addr(m, IFLA_PERM_ADDRESS, &info->permanent_hw_address) >= 0 ||
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);
@@ -1457,6 +1457,34 @@ static int dump_statistics(Table *table, const LinkInfo *info) {
return 0;
}
static int dump_hw_address(Table *table, sd_hwdb *hwdb, const char *field, const struct hw_addr_data *addr) {
_cleanup_free_ char *description = NULL;
int r;
assert(table);
assert(field);
assert(addr);
if (addr->length == ETH_ALEN)
(void) ieee_oui(hwdb, &addr->ether, &description);
r = table_add_many(table,
TABLE_EMPTY,
TABLE_STRING, field);
if (r < 0)
return table_log_add_error(r);
r = table_add_cell_stringf(table, NULL, "%s%s%s%s",
HW_ADDR_TO_STR(addr),
description ? " (" : "",
strempty(description),
description ? ")" : "");
if (r < 0)
return table_log_add_error(r);
return 0;
}
static OutputFlags get_output_flags(void) {
return
arg_all * OUTPUT_SHOW_ALL |
@@ -1676,43 +1704,16 @@ static int link_status_one(
return table_log_add_error(r);
}
if (info->has_mac_address) {
_cleanup_free_ char *description = NULL;
if (info->hw_address.length == ETH_ALEN)
(void) ieee_oui(hwdb, &info->hw_address.ether, &description);
r = table_add_many(table,
TABLE_EMPTY,
TABLE_STRING, "HW Address:");
if (info->has_hw_address) {
r = dump_hw_address(table, hwdb, "Hardware Address:", &info->hw_address);
if (r < 0)
return table_log_add_error(r);
r = table_add_cell_stringf(table, NULL, "%s%s%s%s",
HW_ADDR_TO_STR(&info->hw_address),
description ? " (" : "",
strempty(description),
description ? ")" : "");
if (r < 0)
return table_log_add_error(r);
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, "HW Permanent 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) {
+20 -6
View File
@@ -10,6 +10,7 @@
#include <unistd.h>
#include "alloc-util.h"
#include "arphrd-util.h"
#include "batadv.h"
#include "bond.h"
#include "bridge.h"
@@ -1196,8 +1197,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,15 +2401,28 @@ 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);
if (r < 0)
log_link_debug_errno(link, r, "Permanent MAC address not found for new device, continuing without: %m");
log_link_debug(link, "Saved new link: ifindex=%i, iftype=%s(%u), kind=%s",
link->ifindex, strna(arphrd_to_name(link->iftype)), link->iftype, strna(link->kind));
r = netlink_message_read_hw_addr(message, IFLA_PERM_ADDRESS, &link->permanent_hw_addr);
if (r < 0) {
if (r != -ENODATA)
log_link_debug_errno(link, r, "Failed to read IFLA_PERM_ADDRESS attribute, ignoring: %m");
if (netlink_message_read_hw_addr(message, IFLA_ADDRESS, NULL) >= 0) {
/* Fallback to ethtool, if the link has a hardware address. */
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 hardware address not found, continuing without: %m");
}
}
if (link->permanent_hw_addr.length > 0)
log_link_debug(link, "Saved permanent hardware address: %s", HW_ADDR_TO_STR(&link->permanent_hw_addr));
r = ethtool_get_driver(&manager->ethtool_fd, link->ifname, &link->driver);
if (r < 0)
log_link_debug_errno(link, r, "Failed to get driver, continuing without: %m");
log_link_debug(link, "Link %d added", link->ifindex);
*ret = TAKE_PTR(link);
return 0;
}
+1 -1
View 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;