Skip to content

Commit c341683

Browse files
fix: Prioritize find link info by permanent MAC address, with fallback 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]>
1 parent 560173b commit c341683

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

library/network_connections.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -226,14 +226,26 @@ def link_info_find(cls, refresh=False, mac=None, ifname=None):
226226
if mac is not None:
227227
mac = Util.mac_norm(mac)
228228
for linkinfo in cls.link_infos(refresh).values():
229-
if mac is not None and mac not in [
230-
linkinfo.get("perm-address", None),
231-
linkinfo.get("address", None),
232-
]:
233-
continue
234-
if ifname is not None and ifname != linkinfo.get("ifname", None):
235-
continue
236-
return linkinfo
229+
perm_address = linkinfo.get("perm-address", None)
230+
current_address = linkinfo.get("address", None)
231+
232+
# Match by perm-address (prioritized)
233+
if mac is not None and perm_address not in [None, "00:00:00:00:00:00"]:
234+
if mac == perm_address:
235+
return linkinfo
236+
237+
# Fallback to match by address
238+
if mac is not None and (perm_address in [None, "00:00:00:00:00:00"]):
239+
if mac == current_address:
240+
matched_by_address = linkinfo # Save for potential fallback
241+
242+
if ifname is not None and ifname == linkinfo.get("ifname", None):
243+
return linkinfo
244+
245+
# Return fallback match by address if no perm-address match found
246+
if "matched_by_address" in locals():
247+
return matched_by_address
248+
237249
return None
238250

239251

0 commit comments

Comments
 (0)