Skip to content

Commit

Permalink
fix: Prioritize find link info by permanent MAC address, with fallbac…
Browse files Browse the repository at this point in the history
…k to current address

Updated the link_info_find method to prioritize matching links by
perm-address when it is valid and available. If the perm-address is
unavailable (None or "00:00:00:00:00:00"), the method falls back to
matching by address. Additionally, if ifname is provided, it takes
precedence and returns the corresponding linkinfo immediately.

The change resolves scenarios where multiple network interfaces might
share the same current MAC address (address), leading to potential
ambiguity in link matching. By prioritizing the permanent MAC address
(perm-address), the method provides a more precise and consistent match.
This is particularly crucial in environments with:

- MAC address spoofing or dynamic changes, where the current MAC
  address may not reliably identify the interface.
- Virtual interfaces or VLANs, which often lack a valid perm-address
  and rely on the parent interface's address.
- Ambiguity when multiple interfaces share the same address.

This change improves the robustness of MAC address matching by ensuring
that permanent addresses are prioritized while maintaining a reliable
fallback mechanism for interfaces with no permanent address.

Signed-off-by: Wen Liang <[email protected]>
  • Loading branch information
liangwen12year committed Jan 4, 2025
1 parent 560173b commit b9eb99a
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions library/network_connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,14 +226,26 @@ def link_info_find(cls, refresh=False, mac=None, ifname=None):
if mac is not None:
mac = Util.mac_norm(mac)
for linkinfo in cls.link_infos(refresh).values():
if mac is not None and mac not in [
linkinfo.get("perm-address", None),
linkinfo.get("address", None),
]:
continue
if ifname is not None and ifname != linkinfo.get("ifname", None):
continue
return linkinfo
perm_address = linkinfo.get("perm-address", None)
current_address = linkinfo.get("address", None)

Check warning on line 230 in library/network_connections.py

View check run for this annotation

Codecov / codecov/patch

library/network_connections.py#L229-L230

Added lines #L229 - L230 were not covered by tests

# Match by perm-address (prioritized)
if mac is not None and perm_address not in [None, "00:00:00:00:00:00"]:
if mac == perm_address:
return linkinfo

Check warning on line 235 in library/network_connections.py

View check run for this annotation

Codecov / codecov/patch

library/network_connections.py#L233-L235

Added lines #L233 - L235 were not covered by tests

# Fallback to match by address
if mac is not None and (perm_address in [None, "00:00:00:00:00:00"]):
if mac == current_address:
matched_by_address = linkinfo # Save for potential fallback

Check warning on line 240 in library/network_connections.py

View check run for this annotation

Codecov / codecov/patch

library/network_connections.py#L238-L240

Added lines #L238 - L240 were not covered by tests

if ifname is not None and ifname == linkinfo.get("ifname", None):
return linkinfo

Check warning on line 243 in library/network_connections.py

View check run for this annotation

Codecov / codecov/patch

library/network_connections.py#L242-L243

Added lines #L242 - L243 were not covered by tests

# Return fallback match by address if no perm-address match found
if "matched_by_address" in locals():
return matched_by_address

Check warning on line 247 in library/network_connections.py

View check run for this annotation

Codecov / codecov/patch

library/network_connections.py#L246-L247

Added lines #L246 - L247 were not covered by tests

return None


Expand Down

0 comments on commit b9eb99a

Please sign in to comment.