From 1f86a3fe52c71af7f46381bf45c2efe580a19dcc Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sat, 6 Nov 2021 12:15:12 +0900 Subject: [PATCH 1/2] ether-addr-util: introduce several helper functions --- src/basic/ether-addr-util.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/basic/ether-addr-util.h b/src/basic/ether-addr-util.h index 794fc55bb8..a7f42ccd1a 100644 --- a/src/basic/ether-addr-util.h +++ b/src/basic/ether-addr-util.h @@ -6,6 +6,8 @@ #include #include "hash-funcs.h" +#include "macro.h" +#include "memory-util.h" /* This is MAX_ADDR_LEN as defined in linux/netdevice.h, but net/if_arp.h * defines a macro of the same name with a much lower size. */ @@ -60,6 +62,26 @@ static inline bool ether_addr_is_null(const struct ether_addr *addr) { return ether_addr_equal(addr, ÐER_ADDR_NULL); } +static inline bool ether_addr_is_broadcast(const struct ether_addr *addr) { + assert(addr); + return memeqbyte(0xff, addr->ether_addr_octet, ETH_ALEN); +} + +static inline bool ether_addr_is_multicast(const struct ether_addr *addr) { + assert(addr); + return FLAGS_SET(addr->ether_addr_octet[0], 0x01); +} + +static inline bool ether_addr_is_unicast(const struct ether_addr *addr) { + return !ether_addr_is_multicast(addr); +} + +static inline bool ether_addr_is_local(const struct ether_addr *addr) { + /* Determine if the Ethernet address is locally-assigned one (IEEE 802) */ + assert(addr); + return !FLAGS_SET(addr->ether_addr_octet[0], 0x02); +} + int ether_addr_from_string(const char *s, struct ether_addr *ret); extern const struct hash_ops ether_addr_hash_ops; From de0f1579847a69f51db4e00062eafc37df7456fe Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sat, 6 Nov 2021 14:12:30 +0900 Subject: [PATCH 2/2] ether-addr-util: make hw_addr_is_null() return true also for all zero address This fixes the following error: --- systemd-networkd[1449490]: ip6tnl0: Saved hardware address: 00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00 systemd-networkd[1449490]: ip6tnl0: Failed to manage link by its new hardware address, ignoring: File exists --- --- src/basic/ether-addr-util.h | 3 ++- src/network/networkctl.c | 2 +- src/network/networkd-link.c | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/basic/ether-addr-util.h b/src/basic/ether-addr-util.h index a7f42ccd1a..c5cfdb4233 100644 --- a/src/basic/ether-addr-util.h +++ b/src/basic/ether-addr-util.h @@ -37,7 +37,8 @@ static inline bool hw_addr_equal(const struct hw_addr_data *a, const struct hw_a return hw_addr_compare(a, b) == 0; } static inline bool hw_addr_is_null(const struct hw_addr_data *addr) { - return hw_addr_equal(addr, &HW_ADDR_NULL); + assert(addr); + return addr->length == 0 || memeqzero(addr->bytes, addr->length); } extern const struct hash_ops hw_addr_hash_ops; diff --git a/src/network/networkctl.c b/src/network/networkctl.c index 6dd05145c9..bb6e0f6b5b 100644 --- a/src/network/networkctl.c +++ b/src/network/networkctl.c @@ -553,7 +553,7 @@ static int decode_link(sd_netlink_message *m, LinkInfo *info, char **patterns, b info->has_mac_address = netlink_message_read_hw_addr(m, IFLA_ADDRESS, &info->hw_address) >= 0 && - !hw_addr_is_null(&info->hw_address); + info->hw_address.length > 0; info->has_permanent_mac_address = ethtool_get_permanent_macaddr(NULL, info->name, &info->permanent_mac_address) >= 0 && diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c index ab2d42a482..5136a8820e 100644 --- a/src/network/networkd-link.c +++ b/src/network/networkd-link.c @@ -2036,7 +2036,7 @@ static int link_update_hardware_address(Link *link, sd_netlink_message *message) if (hw_addr_equal(&link->hw_addr, &addr)) return 0; - if (hw_addr_is_null(&link->hw_addr)) + if (link->hw_addr.length == 0) log_link_debug(link, "Saved hardware address: %s", HW_ADDR_TO_STR(&addr)); else { log_link_debug(link, "Hardware address is changed: %s → %s",