Skip to content

Commit e53c89f

Browse files
fix: Refine MAC validation using interface name
When a user provides both an interface name and a MAC address, the current validation process retrieves sysfs link info separately using the interface name and the MAC address, then compares the results. If the information doesn't match, an error is raised. However, this approach may trigger false alarms because retrieving the link info by MAC might return data that only matches the current MAC instead of the permanent MAC. Since the interface name is unique within the kernel, a more robust validation method is to fetch the MAC address using the interface name and then compare it directly with the user-provided MAC address. Signed-off-by: Wen Liang <[email protected]>
1 parent 57b4c00 commit e53c89f

File tree

1 file changed

+34
-7
lines changed

1 file changed

+34
-7
lines changed

library/network_connections.py

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,16 @@ def _link_read_address(ifname):
167167
c = SysUtil._sysctl_read("/sys/class/net/" + ifname + "/address")
168168
return Util.mac_norm(c.strip())
169169

170+
@staticmethod
171+
def _link_read_bond_port_perm_hwaddr(ifname):
172+
filename = os.path.join(
173+
"/sys/class/net", ifname, "bonding_slave", "perm_hwaddr"
174+
)
175+
if not os.path.exists(filename):
176+
return None
177+
c = SysUtil._sysctl_read(filename)
178+
return Util.mac_norm(c.strip())
179+
170180
@staticmethod
171181
def _link_read_permaddress(ifname):
172182
return ethtool.get_perm_addr(ifname)
@@ -187,6 +197,13 @@ def _link_infos_fetch():
187197
"ifname": ifname,
188198
"address": SysUtil._link_read_address(ifname),
189199
"perm-address": SysUtil._link_read_permaddress(ifname),
200+
# When an interface is added as a port of a bonding device, its MAC
201+
# address might change, we need to retrieve and preserve the original
202+
# MAC address to ensure the user-provided interface name and MAC match
203+
# correctly.
204+
"bond-port-perm-hwaddr": SysUtil._link_read_bond_port_perm_hwaddr(
205+
ifname
206+
),
190207
}
191208
return links
192209

@@ -2180,13 +2197,23 @@ def run_prepare(self):
21802197
"infiniband interface exists"
21812198
% (connection["interface_name"]),
21822199
)
2183-
if li_mac and li_ifname and li_mac != li_ifname:
2184-
self.log_fatal(
2185-
idx,
2186-
"profile specifies interface_name '%s' and mac '%s' but no "
2187-
"such interface exists"
2188-
% (connection["interface_name"], connection["mac"]),
2189-
)
2200+
elif connection["mac"]:
2201+
perm_address = li_ifname.get("perm-address", NULL_MAC)
2202+
current_address = li_ifname.get("address", NULL_MAC)
2203+
bond_port_perm_hwaddr = li_ifname.get(
2204+
"bond-port-perm-hwaddr", NULL_MAC
2205+
)
2206+
if (perm_address not in {NULL_MAC, connection["mac"]}) or (
2207+
perm_address == NULL_MAC
2208+
and connection["mac"]
2209+
not in {current_address, bond_port_perm_hwaddr}
2210+
):
2211+
self.log_fatal(
2212+
idx,
2213+
"profile specifies interface_name '%s' and mac '%s' "
2214+
"but no such interface exists"
2215+
% (connection["interface_name"], connection["mac"]),
2216+
)
21902217

21912218
def start_transaction(self):
21922219
"""Hook before making changes"""

0 commit comments

Comments
 (0)