Commit f4bb2e9c authored by Andy Gospodarek's avatar Andy Gospodarek Committed by David S. Miller

bonding: fix string comparison errors

When a bond contains a device where one name is the subset of another
(eth1 and eth10, for example), one cannot properly set the primary
device or the currently active device.

This was reported and based on work by Takuma Umeya.  I also verified
the problem and tested that this fix resolves it.

V2: A few did not like the the current code or my changes, so I
refactored bonding_store_primary and bonding_store_active_slave to be a
bit cleaner, dropped the use of strnicmp since we did not really need
the comparison to be case insensitive, and formatted the input string
from sysfs so a comparison to IFNAMSIZ could be used.

I also discovered an error in bonding_store_active_slave that would
modify bond->primary_slave rather than bond->curr_active_slave before
forcing the bonding driver to choose a new active slave.

V3: Actually sending the proper patch....
Signed-off-by: default avatarAndy Gospodarek <andy@greyhouse.net>
Reported-by: default avatarTakuma Umeya <tumeya@redhat.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 550fd08c
...@@ -1025,6 +1025,7 @@ static ssize_t bonding_store_primary(struct device *d, ...@@ -1025,6 +1025,7 @@ static ssize_t bonding_store_primary(struct device *d,
int i; int i;
struct slave *slave; struct slave *slave;
struct bonding *bond = to_bond(d); struct bonding *bond = to_bond(d);
char ifname[IFNAMSIZ];
if (!rtnl_trylock()) if (!rtnl_trylock())
return restart_syscall(); return restart_syscall();
...@@ -1035,11 +1036,22 @@ static ssize_t bonding_store_primary(struct device *d, ...@@ -1035,11 +1036,22 @@ static ssize_t bonding_store_primary(struct device *d,
if (!USES_PRIMARY(bond->params.mode)) { if (!USES_PRIMARY(bond->params.mode)) {
pr_info("%s: Unable to set primary slave; %s is in mode %d\n", pr_info("%s: Unable to set primary slave; %s is in mode %d\n",
bond->dev->name, bond->dev->name, bond->params.mode); bond->dev->name, bond->dev->name, bond->params.mode);
} else { goto out;
}
sscanf(buf, "%16s", ifname); /* IFNAMSIZ */
/* check to see if we are clearing primary */
if (!strlen(ifname) || buf[0] == '\n') {
pr_info("%s: Setting primary slave to None.\n",
bond->dev->name);
bond->primary_slave = NULL;
bond_select_active_slave(bond);
goto out;
}
bond_for_each_slave(bond, slave, i) { bond_for_each_slave(bond, slave, i) {
if (strnicmp if (strncmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
(slave->dev->name, buf,
strlen(slave->dev->name)) == 0) {
pr_info("%s: Setting %s as primary slave.\n", pr_info("%s: Setting %s as primary slave.\n",
bond->dev->name, slave->dev->name); bond->dev->name, slave->dev->name);
bond->primary_slave = slave; bond->primary_slave = slave;
...@@ -1049,18 +1061,8 @@ static ssize_t bonding_store_primary(struct device *d, ...@@ -1049,18 +1061,8 @@ static ssize_t bonding_store_primary(struct device *d,
} }
} }
/* if we got here, then we didn't match the name of any slave */ pr_info("%s: Unable to set %.*s as primary slave.\n",
if (strlen(buf) == 0 || buf[0] == '\n') {
pr_info("%s: Setting primary slave to None.\n",
bond->dev->name);
bond->primary_slave = NULL;
bond_select_active_slave(bond);
} else {
pr_info("%s: Unable to set %.*s as primary slave as it is not a slave.\n",
bond->dev->name, (int)strlen(buf) - 1, buf); bond->dev->name, (int)strlen(buf) - 1, buf);
}
}
out: out:
write_unlock_bh(&bond->curr_slave_lock); write_unlock_bh(&bond->curr_slave_lock);
read_unlock(&bond->lock); read_unlock(&bond->lock);
...@@ -1195,6 +1197,7 @@ static ssize_t bonding_store_active_slave(struct device *d, ...@@ -1195,6 +1197,7 @@ static ssize_t bonding_store_active_slave(struct device *d,
struct slave *old_active = NULL; struct slave *old_active = NULL;
struct slave *new_active = NULL; struct slave *new_active = NULL;
struct bonding *bond = to_bond(d); struct bonding *bond = to_bond(d);
char ifname[IFNAMSIZ];
if (!rtnl_trylock()) if (!rtnl_trylock())
return restart_syscall(); return restart_syscall();
...@@ -1203,19 +1206,31 @@ static ssize_t bonding_store_active_slave(struct device *d, ...@@ -1203,19 +1206,31 @@ static ssize_t bonding_store_active_slave(struct device *d,
read_lock(&bond->lock); read_lock(&bond->lock);
write_lock_bh(&bond->curr_slave_lock); write_lock_bh(&bond->curr_slave_lock);
if (!USES_PRIMARY(bond->params.mode)) if (!USES_PRIMARY(bond->params.mode)) {
pr_info("%s: Unable to change active slave; %s is in mode %d\n", pr_info("%s: Unable to change active slave; %s is in mode %d\n",
bond->dev->name, bond->dev->name, bond->params.mode); bond->dev->name, bond->dev->name, bond->params.mode);
else { goto out;
}
sscanf(buf, "%16s", ifname); /* IFNAMSIZ */
/* check to see if we are clearing active */
if (!strlen(ifname) || buf[0] == '\n') {
pr_info("%s: Clearing current active slave.\n",
bond->dev->name);
bond->curr_active_slave = NULL;
bond_select_active_slave(bond);
goto out;
}
bond_for_each_slave(bond, slave, i) { bond_for_each_slave(bond, slave, i) {
if (strnicmp if (strncmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
(slave->dev->name, buf,
strlen(slave->dev->name)) == 0) {
old_active = bond->curr_active_slave; old_active = bond->curr_active_slave;
new_active = slave; new_active = slave;
if (new_active == old_active) { if (new_active == old_active) {
/* do nothing */ /* do nothing */
pr_info("%s: %s is already the current active slave.\n", pr_info("%s: %s is already the current"
" active slave.\n",
bond->dev->name, bond->dev->name,
slave->dev->name); slave->dev->name);
goto out; goto out;
...@@ -1225,13 +1240,17 @@ static ssize_t bonding_store_active_slave(struct device *d, ...@@ -1225,13 +1240,17 @@ static ssize_t bonding_store_active_slave(struct device *d,
(old_active) && (old_active) &&
(new_active->link == BOND_LINK_UP) && (new_active->link == BOND_LINK_UP) &&
IS_UP(new_active->dev)) { IS_UP(new_active->dev)) {
pr_info("%s: Setting %s as active slave.\n", pr_info("%s: Setting %s as active"
" slave.\n",
bond->dev->name, bond->dev->name,
slave->dev->name); slave->dev->name);
bond_change_active_slave(bond, new_active); bond_change_active_slave(bond,
new_active);
} }
else { else {
pr_info("%s: Could not set %s as active slave; either %s is down or the link is down.\n", pr_info("%s: Could not set %s as"
" active slave; either %s is"
" down or the link is down.\n",
bond->dev->name, bond->dev->name,
slave->dev->name, slave->dev->name,
slave->dev->name); slave->dev->name);
...@@ -1241,18 +1260,8 @@ static ssize_t bonding_store_active_slave(struct device *d, ...@@ -1241,18 +1260,8 @@ static ssize_t bonding_store_active_slave(struct device *d,
} }
} }
/* if we got here, then we didn't match the name of any slave */ pr_info("%s: Unable to set %.*s as active slave.\n",
if (strlen(buf) == 0 || buf[0] == '\n') {
pr_info("%s: Setting active slave to None.\n",
bond->dev->name);
bond->primary_slave = NULL;
bond_select_active_slave(bond);
} else {
pr_info("%s: Unable to set %.*s as active slave as it is not a slave.\n",
bond->dev->name, (int)strlen(buf) - 1, buf); bond->dev->name, (int)strlen(buf) - 1, buf);
}
}
out: out:
write_unlock_bh(&bond->curr_slave_lock); write_unlock_bh(&bond->curr_slave_lock);
read_unlock(&bond->lock); read_unlock(&bond->lock);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment