Skip to content

Commit 5c3bf6c

Browse files
liuhangbindavem330
authored andcommitted
bonding: assign random address if device address is same as bond
This change addresses a MAC address conflict issue in failover scenarios, similar to the problem described in commit a951bc1 ("bonding: correct the MAC address for 'follow' fail_over_mac policy"). In fail_over_mac=follow mode, the bonding driver expects the formerly active slave to swap MAC addresses with the newly active slave during failover. However, under certain conditions, two slaves may end up with the same MAC address, which breaks this policy: 1) ip link set eth0 master bond0 -> bond0 adopts eth0's MAC address (MAC0). 2) ip link set eth1 master bond0 -> eth1 is added as a backup with its own MAC (MAC1). 3) ip link set eth0 nomaster -> eth0 is released and restores its MAC (MAC0). -> eth1 becomes the active slave, and bond0 assigns MAC0 to eth1. 4) ip link set eth0 master bond0 -> eth0 is re-added to bond0, now both eth0 and eth1 have MAC0. This results in a MAC address conflict and violates the expected behavior of the failover policy. To fix this, we assign a random MAC address to any newly added slave if its current MAC address matches that of the bond. The original (permanent) MAC address is saved and will be restored when the device is released from the bond. This ensures that each slave has a unique MAC address during failover transitions, preserving the integrity of the fail_over_mac=follow policy. Fixes: 3915c1e ("bonding: Add "follow" option to fail_over_mac") Signed-off-by: Hangbin Liu <liuhangbin@gmail.com> Acked-by: Jay Vosburgh <jv@jvosburgh.net> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent cc17b4b commit 5c3bf6c

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

drivers/net/bonding/bond_main.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2118,15 +2118,26 @@ int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev,
21182118
* set the master's mac address to that of the first slave
21192119
*/
21202120
memcpy(ss.__data, bond_dev->dev_addr, bond_dev->addr_len);
2121-
ss.ss_family = slave_dev->type;
2122-
res = dev_set_mac_address(slave_dev, (struct sockaddr *)&ss,
2123-
extack);
2124-
if (res) {
2125-
slave_err(bond_dev, slave_dev, "Error %d calling set_mac_address\n", res);
2126-
goto err_restore_mtu;
2127-
}
2121+
} else if (bond->params.fail_over_mac == BOND_FOM_FOLLOW &&
2122+
BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP &&
2123+
memcmp(slave_dev->dev_addr, bond_dev->dev_addr, bond_dev->addr_len) == 0) {
2124+
/* Set slave to random address to avoid duplicate mac
2125+
* address in later fail over.
2126+
*/
2127+
eth_random_addr(ss.__data);
2128+
} else {
2129+
goto skip_mac_set;
21282130
}
21292131

2132+
ss.ss_family = slave_dev->type;
2133+
res = dev_set_mac_address(slave_dev, (struct sockaddr *)&ss, extack);
2134+
if (res) {
2135+
slave_err(bond_dev, slave_dev, "Error %d calling set_mac_address\n", res);
2136+
goto err_restore_mtu;
2137+
}
2138+
2139+
skip_mac_set:
2140+
21302141
/* set no_addrconf flag before open to prevent IPv6 addrconf */
21312142
slave_dev->priv_flags |= IFF_NO_ADDRCONF;
21322143

0 commit comments

Comments
 (0)