Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-2.6
[safe/jmp/linux-2.6] / drivers / net / bonding / bond_main.c
index 17a4611..ecfaf14 100644 (file)
@@ -35,7 +35,6 @@
 
 #include <linux/kernel.h>
 #include <linux/module.h>
-#include <linux/sched.h>
 #include <linux/types.h>
 #include <linux/fcntl.h>
 #include <linux/interrupt.h>
@@ -61,6 +60,7 @@
 #include <linux/errno.h>
 #include <linux/netdevice.h>
 #include <linux/inetdevice.h>
+#include <linux/igmp.h>
 #include <linux/etherdevice.h>
 #include <linux/skbuff.h>
 #include <net/sock.h>
@@ -74,7 +74,9 @@
 #include <linux/ethtool.h>
 #include <linux/if_vlan.h>
 #include <linux/if_bonding.h>
+#include <linux/jiffies.h>
 #include <net/route.h>
+#include <net/net_namespace.h>
 #include "bonding.h"
 #include "bond_3ad.h"
 #include "bond_alb.h"
@@ -97,6 +99,7 @@ static char *xmit_hash_policy = NULL;
 static int arp_interval = BOND_LINK_ARP_INTERV;
 static char *arp_ip_target[BOND_MAX_ARP_TARGETS] = { NULL, };
 static char *arp_validate = NULL;
+static int fail_over_mac = 0;
 struct bond_params bonding_defaults;
 
 module_param(max_bonds, int, 0);
@@ -130,6 +133,8 @@ module_param_array(arp_ip_target, charp, NULL, 0);
 MODULE_PARM_DESC(arp_ip_target, "arp targets in n.n.n.n form");
 module_param(arp_validate, charp, 0);
 MODULE_PARM_DESC(arp_validate, "validate src/dst of ARP probes: none (default), active, backup or all");
+module_param(fail_over_mac, int, 0);
+MODULE_PARM_DESC(fail_over_mac, "For active-backup, do not set all slaves to the same MAC.  0 of off (default), 1 for on.");
 
 /*----------------------------- Global variables ----------------------------*/
 
@@ -143,7 +148,7 @@ static struct proc_dir_entry *bond_proc_dir = NULL;
 #endif
 
 extern struct rw_semaphore bonding_rwsem;
-static u32 arp_target[BOND_MAX_ARP_TARGETS] = { 0, } ;
+static __be32 arp_target[BOND_MAX_ARP_TARGETS] = { 0, } ;
 static int arp_ip_count        = 0;
 static int bond_mode   = BOND_MODE_ROUNDROBIN;
 static int xmit_hashtype= BOND_XMIT_POLICY_LAYER2;
@@ -170,6 +175,7 @@ struct bond_parm_tbl bond_mode_tbl[] = {
 struct bond_parm_tbl xmit_hashtype_tbl[] = {
 {      "layer2",               BOND_XMIT_POLICY_LAYER2},
 {      "layer3+4",             BOND_XMIT_POLICY_LAYER34},
+{      "layer2+3",             BOND_XMIT_POLICY_LAYER23},
 {      NULL,                   -1},
 };
 
@@ -184,10 +190,11 @@ struct bond_parm_tbl arp_validate_tbl[] = {
 /*-------------------------- Forward declarations ---------------------------*/
 
 static void bond_send_gratuitous_arp(struct bonding *bond);
+static void bond_deinit(struct net_device *bond_dev);
 
 /*---------------------------- General routines -----------------------------*/
 
-const char *bond_mode_name(int mode)
+static const char *bond_mode_name(int mode)
 {
        switch (mode) {
        case BOND_MODE_ROUNDROBIN :
@@ -376,7 +383,7 @@ struct vlan_entry *bond_next_vlan(struct bonding *bond, struct vlan_entry *curr)
  */
 int bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb, struct net_device *slave_dev)
 {
-       unsigned short vlan_id;
+       unsigned short uninitialized_var(vlan_id);
 
        if (!list_empty(&bond->vlan_list) &&
            !(slave_dev->features & NETIF_F_HW_VLAN_TX) &&
@@ -489,9 +496,9 @@ static void bond_vlan_rx_kill_vid(struct net_device *bond_dev, uint16_t vid)
                        /* Save and then restore vlan_dev in the grp array,
                         * since the slave's driver might clear it.
                         */
-                       vlan_dev = bond->vlgrp->vlan_devices[vid];
+                       vlan_dev = vlan_group_get_device(bond->vlgrp, vid);
                        slave_dev->vlan_rx_kill_vid(slave_dev, vid);
-                       bond->vlgrp->vlan_devices[vid] = vlan_dev;
+                       vlan_group_set_device(bond->vlgrp, vid, vlan_dev);
                }
        }
 
@@ -551,9 +558,9 @@ static void bond_del_vlans_from_slave(struct bonding *bond, struct net_device *s
                /* Save and then restore vlan_dev in the grp array,
                 * since the slave's driver might clear it.
                 */
-               vlan_dev = bond->vlgrp->vlan_devices[vlan->vlan_id];
+               vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
                slave_dev->vlan_rx_kill_vid(slave_dev, vlan->vlan_id);
-               bond->vlgrp->vlan_devices[vlan->vlan_id] = vlan_dev;
+               vlan_group_set_device(bond->vlgrp, vlan->vlan_id, vlan_dev);
        }
 
 unreg:
@@ -613,38 +620,20 @@ down:
 static int bond_update_speed_duplex(struct slave *slave)
 {
        struct net_device *slave_dev = slave->dev;
-       static int (* ioctl)(struct net_device *, struct ifreq *, int);
-       struct ifreq ifr;
        struct ethtool_cmd etool;
+       int res;
 
        /* Fake speed and duplex */
        slave->speed = SPEED_100;
        slave->duplex = DUPLEX_FULL;
 
-       if (slave_dev->ethtool_ops) {
-               int res;
-
-               if (!slave_dev->ethtool_ops->get_settings) {
-                       return -1;
-               }
-
-               res = slave_dev->ethtool_ops->get_settings(slave_dev, &etool);
-               if (res < 0) {
-                       return -1;
-               }
-
-               goto verify;
-       }
+       if (!slave_dev->ethtool_ops || !slave_dev->ethtool_ops->get_settings)
+               return -1;
 
-       ioctl = slave_dev->do_ioctl;
-       strncpy(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
-       etool.cmd = ETHTOOL_GSET;
-       ifr.ifr_data = (char*)&etool;
-       if (!ioctl || (IOCTL(slave_dev, &ifr, SIOCETHTOOL) < 0)) {
+       res = slave_dev->ethtool_ops->get_settings(slave_dev, &etool);
+       if (res < 0)
                return -1;
-       }
 
-verify:
        switch (etool.speed) {
        case SPEED_10:
        case SPEED_100:
@@ -690,7 +679,6 @@ static int bond_check_dev_link(struct bonding *bond, struct net_device *slave_de
        static int (* ioctl)(struct net_device *, struct ifreq *, int);
        struct ifreq ifr;
        struct mii_ioctl_data *mii;
-       struct ethtool_value etool;
 
        if (bond->params.use_carrier) {
                return netif_carrier_ok(slave_dev) ? BMSR_LSTATUS : 0;
@@ -721,9 +709,10 @@ static int bond_check_dev_link(struct bonding *bond, struct net_device *slave_de
                }
        }
 
-       /* try SIOCETHTOOL ioctl, some drivers cache ETHTOOL_GLINK */
-       /* for a period of time so we attempt to get link status   */
-       /* from it last if the above MII ioctls fail...            */
+       /*
+        * Some drivers cache ETHTOOL_GLINK for a period of time so we only
+        * attempt to get link status from it if the above MII ioctls fail.
+        */
        if (slave_dev->ethtool_ops) {
                if (slave_dev->ethtool_ops->get_link) {
                        u32 link;
@@ -734,23 +723,9 @@ static int bond_check_dev_link(struct bonding *bond, struct net_device *slave_de
                }
        }
 
-       if (ioctl) {
-               strncpy(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
-               etool.cmd = ETHTOOL_GLINK;
-               ifr.ifr_data = (char*)&etool;
-               if (IOCTL(slave_dev, &ifr, SIOCETHTOOL) == 0) {
-                       if (etool.data == 1) {
-                               return BMSR_LSTATUS;
-                       } else {
-                               dprintk("SIOCETHTOOL shows link down\n");
-                               return 0;
-                       }
-               }
-       }
-
        /*
         * If reporting, report that either there's no dev->do_ioctl,
-        * or both SIOCGMIIREG and SIOCETHTOOL failed (meaning that we
+        * or both SIOCGMIIREG and get_link failed (meaning that we
         * cannot report link status).  If not reporting, pretend
         * we're ok.
         */
@@ -862,6 +837,28 @@ static void bond_mc_delete(struct bonding *bond, void *addr, int alen)
        }
 }
 
+
+/*
+ * Retrieve the list of registered multicast addresses for the bonding
+ * device and retransmit an IGMP JOIN request to the current active
+ * slave.
+ */
+static void bond_resend_igmp_join_requests(struct bonding *bond)
+{
+       struct in_device *in_dev;
+       struct ip_mc_list *im;
+
+       rcu_read_lock();
+       in_dev = __in_dev_get_rcu(bond->dev);
+       if (in_dev) {
+               for (im = in_dev->mc_list; im; im = im->next) {
+                       ip_mc_rejoin_group(im);
+               }
+       }
+
+       rcu_read_unlock();
+}
+
 /*
  * Totally destroys the mc_list in bond
  */
@@ -875,6 +872,7 @@ static void bond_mc_list_destroy(struct bonding *bond)
                kfree(dmi);
                dmi = bond->mc_list;
        }
+        bond->mc_list = NULL;
 }
 
 /*
@@ -968,6 +966,7 @@ static void bond_mc_swap(struct bonding *bond, struct slave *new_active, struct
                for (dmi = bond->dev->mc_list; dmi; dmi = dmi->next) {
                        dev_mc_add(new_active->dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
                }
+               bond_resend_igmp_join_requests(bond);
        }
 }
 
@@ -1103,7 +1102,21 @@ void bond_change_active_slave(struct bonding *bond, struct slave *new_active)
                if (new_active) {
                        bond_set_slave_active_flags(new_active);
                }
-               bond_send_gratuitous_arp(bond);
+
+               /* when bonding does not set the slave MAC address, the bond MAC
+                * address is the one of the active slave.
+                */
+               if (new_active && bond->params.fail_over_mac)
+                       memcpy(bond->dev->dev_addr,  new_active->dev->dev_addr,
+                               new_active->dev->addr_len);
+               if (bond->curr_active_slave &&
+                       test_bit(__LINK_STATE_LINKWATCH_PENDING,
+                                       &bond->curr_active_slave->dev->state)) {
+                       dprintk("delaying gratuitous arp on %s\n",
+                               bond->curr_active_slave->dev->name);
+                       bond->send_grat_arp = 1;
+               } else
+                       bond_send_gratuitous_arp(bond);
        }
 }
 
@@ -1200,7 +1213,8 @@ static void bond_detach_slave(struct bonding *bond, struct slave *slave)
 
 /*---------------------------------- IOCTL ----------------------------------*/
 
-int bond_sethwaddr(struct net_device *bond_dev, struct net_device *slave_dev)
+static int bond_sethwaddr(struct net_device *bond_dev,
+                         struct net_device *slave_dev)
 {
        dprintk("bond_dev=%p\n", bond_dev);
        dprintk("slave_dev=%p\n", slave_dev);
@@ -1209,49 +1223,60 @@ int bond_sethwaddr(struct net_device *bond_dev, struct net_device *slave_dev)
        return 0;
 }
 
-#define BOND_INTERSECT_FEATURES \
-       (NETIF_F_SG | NETIF_F_ALL_CSUM | NETIF_F_TSO | NETIF_F_UFO)
+#define BOND_VLAN_FEATURES \
+       (NETIF_F_VLAN_CHALLENGED | NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_TX | \
+        NETIF_F_HW_VLAN_FILTER)
 
 /* 
  * Compute the common dev->feature set available to all slaves.  Some
- * feature bits are managed elsewhere, so preserve feature bits set on
- * master device that are not part of the examined set.
+ * feature bits are managed elsewhere, so preserve those feature bits
+ * on the master device.
  */
 static int bond_compute_features(struct bonding *bond)
 {
-       unsigned long features = BOND_INTERSECT_FEATURES;
        struct slave *slave;
        struct net_device *bond_dev = bond->dev;
-       unsigned short max_hard_header_len = ETH_HLEN;
+       unsigned long features = bond_dev->features;
+       unsigned short max_hard_header_len = max((u16)ETH_HLEN,
+                                               bond_dev->hard_header_len);
        int i;
 
+       features &= ~(NETIF_F_ALL_CSUM | BOND_VLAN_FEATURES);
+       features |= NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA |
+                   NETIF_F_GSO_MASK | NETIF_F_NO_CSUM;
+
        bond_for_each_slave(bond, slave, i) {
-               features &= (slave->dev->features & BOND_INTERSECT_FEATURES);
+               features = netdev_compute_features(features,
+                                                  slave->dev->features);
                if (slave->dev->hard_header_len > max_hard_header_len)
                        max_hard_header_len = slave->dev->hard_header_len;
        }
 
-       if ((features & NETIF_F_SG) && 
-           !(features & NETIF_F_ALL_CSUM))
-               features &= ~NETIF_F_SG;
-
-       /* 
-        * features will include NETIF_F_TSO (NETIF_F_UFO) iff all 
-        * slave devices support NETIF_F_TSO (NETIF_F_UFO), which 
-        * implies that all slaves also support scatter-gather 
-        * (NETIF_F_SG), which implies that features also includes 
-        * NETIF_F_SG. So no need to check whether we have an  
-        * illegal combination of NETIF_F_{TSO,UFO} and 
-        * !NETIF_F_SG 
-        */
-
-       features |= (bond_dev->features & ~BOND_INTERSECT_FEATURES);
+       features |= (bond_dev->features & BOND_VLAN_FEATURES);
        bond_dev->features = features;
        bond_dev->hard_header_len = max_hard_header_len;
 
        return 0;
 }
 
+
+static void bond_setup_by_slave(struct net_device *bond_dev,
+                               struct net_device *slave_dev)
+{
+       struct bonding *bond = bond_dev->priv;
+
+       bond_dev->neigh_setup           = slave_dev->neigh_setup;
+       bond_dev->header_ops            = slave_dev->header_ops;
+
+       bond_dev->type              = slave_dev->type;
+       bond_dev->hard_header_len   = slave_dev->hard_header_len;
+       bond_dev->addr_len          = slave_dev->addr_len;
+
+       memcpy(bond_dev->broadcast, slave_dev->broadcast,
+               slave_dev->addr_len);
+       bond->setup_by_slave = 1;
+}
+
 /* enslave device <slave> to bond device <master> */
 int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
 {
@@ -1272,8 +1297,9 @@ int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
 
        /* bond must be initialized by bond_open() before enslaving */
        if (!(bond_dev->flags & IFF_UP)) {
-               dprintk("Error, master_dev is not up\n");
-               return -EPERM;
+               printk(KERN_WARNING DRV_NAME
+                       " %s: master_dev is not up in bond_enslave\n",
+                       bond_dev->name);
        }
 
        /* already enslaved */
@@ -1326,24 +1352,50 @@ int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
                goto err_undo_flags;
        }
 
+       /* set bonding device ether type by slave - bonding netdevices are
+        * created with ether_setup, so when the slave type is not ARPHRD_ETHER
+        * there is a need to override some of the type dependent attribs/funcs.
+        *
+        * bond ether type mutual exclusion - don't allow slaves of dissimilar
+        * ether type (eg ARPHRD_ETHER and ARPHRD_INFINIBAND) share the same bond
+        */
+       if (bond->slave_cnt == 0) {
+               if (slave_dev->type != ARPHRD_ETHER)
+                       bond_setup_by_slave(bond_dev, slave_dev);
+       } else if (bond_dev->type != slave_dev->type) {
+               printk(KERN_ERR DRV_NAME ": %s ether type (%d) is different "
+                       "from other slaves (%d), can not enslave it.\n",
+                       slave_dev->name,
+                       slave_dev->type, bond_dev->type);
+                       res = -EINVAL;
+                       goto err_undo_flags;
+       }
+
        if (slave_dev->set_mac_address == NULL) {
-               printk(KERN_ERR DRV_NAME
-                       ": %s: Error: The slave device you specified does "
-                       "not support setting the MAC address. "
-                       "Your kernel likely does not support slave "
-                       "devices.\n", bond_dev->name);
-               res = -EOPNOTSUPP;
-               goto err_undo_flags;
+               if (bond->slave_cnt == 0) {
+                       printk(KERN_WARNING DRV_NAME
+                              ": %s: Warning: The first slave device "
+                              "specified does not support setting the MAC "
+                              "address. Enabling the fail_over_mac option.",
+                              bond_dev->name);
+                       bond->params.fail_over_mac = 1;
+               } else if (!bond->params.fail_over_mac) {
+                       printk(KERN_ERR DRV_NAME
+                               ": %s: Error: The slave device specified "
+                               "does not support setting the MAC address, "
+                               "but fail_over_mac is not enabled.\n"
+                               , bond_dev->name);
+                       res = -EOPNOTSUPP;
+                       goto err_undo_flags;
+               }
        }
 
-       new_slave = kmalloc(sizeof(struct slave), GFP_KERNEL);
+       new_slave = kzalloc(sizeof(struct slave), GFP_KERNEL);
        if (!new_slave) {
                res = -ENOMEM;
                goto err_undo_flags;
        }
 
-       memset(new_slave, 0, sizeof(struct slave));
-
        /* save slave's original flags before calling
         * netdev_set_master and dev_open
         */
@@ -1356,18 +1408,25 @@ int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
         */
        memcpy(new_slave->perm_hwaddr, slave_dev->dev_addr, ETH_ALEN);
 
-       /*
-        * Set slave to master's mac address.  The application already
-        * set the master's mac address to that of the first slave
-        */
-       memcpy(addr.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
-       addr.sa_family = slave_dev->type;
-       res = dev_set_mac_address(slave_dev, &addr);
-       if (res) {
-               dprintk("Error %d calling set_mac_address\n", res);
-               goto err_free;
+       if (!bond->params.fail_over_mac) {
+               /*
+                * Set slave to master's mac address.  The application already
+                * set the master's mac address to that of the first slave
+                */
+               memcpy(addr.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
+               addr.sa_family = slave_dev->type;
+               res = dev_set_mac_address(slave_dev, &addr);
+               if (res) {
+                       dprintk("Error %d calling set_mac_address\n", res);
+                       goto err_free;
+               }
        }
 
+       res = netdev_set_master(slave_dev, bond_dev);
+       if (res) {
+               dprintk("Error %d calling netdev_set_master\n", res);
+               goto err_close;
+       }
        /* open the slave since the application closed it */
        res = dev_open(slave_dev);
        if (res) {
@@ -1375,12 +1434,6 @@ int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
                goto err_restore_mac;
        }
 
-       res = netdev_set_master(slave_dev, bond_dev);
-       if (res) {
-               dprintk("Error %d calling netdev_set_master\n", res);
-               goto err_close;
-       }
-
        new_slave->dev = slave_dev;
        slave_dev->priv_flags |= IFF_BONDING;
 
@@ -1411,10 +1464,12 @@ int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
                        dev_set_allmulti(slave_dev, 1);
                }
 
+               netif_tx_lock_bh(bond_dev);
                /* upload master's mc_list to new slave */
                for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
                        dev_mc_add (slave_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
                }
+               netif_tx_unlock_bh(bond_dev);
        }
 
        if (bond->params.mode == BOND_MODE_8023AD) {
@@ -1540,15 +1595,7 @@ int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
        case BOND_MODE_TLB:
        case BOND_MODE_ALB:
                new_slave->state = BOND_STATE_ACTIVE;
-               if ((!bond->curr_active_slave) &&
-                   (new_slave->link != BOND_LINK_DOWN)) {
-                       /* first slave or no active slave yet, and this link
-                        * is OK, so make this interface the active one
-                        */
-                       bond_change_active_slave(bond, new_slave);
-               } else {
-                       bond_set_slave_inactive_flags(new_slave);
-               }
+               bond_set_slave_inactive_flags(new_slave);
                break;
        default:
                dprintk("This slave is always active in trunk mode\n");
@@ -1591,9 +1638,11 @@ err_close:
        dev_close(slave_dev);
 
 err_restore_mac:
-       memcpy(addr.sa_data, new_slave->perm_hwaddr, ETH_ALEN);
-       addr.sa_family = slave_dev->type;
-       dev_set_mac_address(slave_dev, &addr);
+       if (!bond->params.fail_over_mac) {
+               memcpy(addr.sa_data, new_slave->perm_hwaddr, ETH_ALEN);
+               addr.sa_family = slave_dev->type;
+               dev_set_mac_address(slave_dev, &addr);
+       }
 
 err_free:
        kfree(new_slave);
@@ -1621,6 +1670,7 @@ int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
        struct slave *slave, *oldcurrent;
        struct sockaddr addr;
        int mac_addr_differ;
+       DECLARE_MAC_BUF(mac);
 
        /* slave is not a slave or master is not master of this slave */
        if (!(slave_dev->flags & IFF_SLAVE) ||
@@ -1648,19 +1698,13 @@ int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
                                 ETH_ALEN);
        if (!mac_addr_differ && (bond->slave_cnt > 1)) {
                printk(KERN_WARNING DRV_NAME
-                      ": %s: Warning: the permanent HWaddr of %s "
-                      "- %02X:%02X:%02X:%02X:%02X:%02X - is "
-                      "still in use by %s. Set the HWaddr of "
-                      "%s to a different address to avoid "
-                      "conflicts.\n",
+                      ": %s: Warning: the permanent HWaddr of %s - "
+                      "%s - is still in use by %s. "
+                      "Set the HWaddr of %s to a different address "
+                      "to avoid conflicts.\n",
                       bond_dev->name,
                       slave_dev->name,
-                      slave->perm_hwaddr[0],
-                      slave->perm_hwaddr[1],
-                      slave->perm_hwaddr[2],
-                      slave->perm_hwaddr[3],
-                      slave->perm_hwaddr[4],
-                      slave->perm_hwaddr[5],
+                      print_mac(mac, slave->perm_hwaddr),
                       bond_dev->name,
                       slave_dev->name);
        }
@@ -1704,12 +1748,28 @@ int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
                 * has been cleared (if our_slave == old_current),
                 * but before a new active slave is selected.
                 */
+               write_unlock_bh(&bond->lock);
                bond_alb_deinit_slave(bond, slave);
+               write_lock_bh(&bond->lock);
        }
 
-       if (oldcurrent == slave)
+       if (oldcurrent == slave) {
+               /*
+                * Note that we hold RTNL over this sequence, so there
+                * is no concern that another slave add/remove event
+                * will interfere.
+                */
+               write_unlock_bh(&bond->lock);
+               read_lock(&bond->lock);
+               write_lock_bh(&bond->curr_slave_lock);
+
                bond_select_active_slave(bond);
 
+               write_unlock_bh(&bond->curr_slave_lock);
+               read_unlock(&bond->lock);
+               write_lock_bh(&bond->lock);
+       }
+
        if (bond->slave_cnt == 0) {
                bond_set_carrier(bond);
 
@@ -1763,7 +1823,9 @@ int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
                }
 
                /* flush master's mc_list from slave */
+               netif_tx_lock_bh(bond_dev);
                bond_mc_list_flush(bond_dev, slave_dev);
+               netif_tx_unlock_bh(bond_dev);
        }
 
        netdev_set_master(slave_dev, NULL);
@@ -1771,10 +1833,12 @@ int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
        /* close slave before restoring its mac address */
        dev_close(slave_dev);
 
-       /* restore original ("permanent") mac address */
-       memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
-       addr.sa_family = slave_dev->type;
-       dev_set_mac_address(slave_dev, &addr);
+       if (!bond->params.fail_over_mac) {
+               /* restore original ("permanent") mac address */
+               memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
+               addr.sa_family = slave_dev->type;
+               dev_set_mac_address(slave_dev, &addr);
+       }
 
        slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
                                   IFF_SLAVE_INACTIVE | IFF_BONDING |
@@ -1786,6 +1850,35 @@ int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
 }
 
 /*
+* Destroy a bonding device.
+* Must be under rtnl_lock when this function is called.
+*/
+void bond_destroy(struct bonding *bond)
+{
+       bond_deinit(bond->dev);
+       bond_destroy_sysfs_entry(bond);
+       unregister_netdevice(bond->dev);
+}
+
+/*
+* First release a slave and than destroy the bond if no more slaves iare left.
+* Must be under rtnl_lock when this function is called.
+*/
+int  bond_release_and_destroy(struct net_device *bond_dev, struct net_device *slave_dev)
+{
+       struct bonding *bond = bond_dev->priv;
+       int ret;
+
+       ret = bond_release(bond_dev, slave_dev);
+       if ((ret == 0) && (bond->slave_cnt == 0)) {
+               printk(KERN_INFO DRV_NAME ": %s: destroying bond %s.\n",
+                      bond_dev->name, bond_dev->name);
+               bond_destroy(bond);
+       }
+       return ret;
+}
+
+/*
  * This function releases all slaves.
  */
 static int bond_release_all(struct net_device *bond_dev)
@@ -1818,6 +1911,12 @@ static int bond_release_all(struct net_device *bond_dev)
                slave_dev = slave->dev;
                bond_detach_slave(bond, slave);
 
+               /* now that the slave is detached, unlock and perform
+                * all the undo steps that should not be called from
+                * within a lock.
+                */
+               write_unlock_bh(&bond->lock);
+
                if ((bond->params.mode == BOND_MODE_TLB) ||
                    (bond->params.mode == BOND_MODE_ALB)) {
                        /* must be called only after the slave
@@ -1828,12 +1927,6 @@ static int bond_release_all(struct net_device *bond_dev)
 
                bond_compute_features(bond);
 
-               /* now that the slave is detached, unlock and perform
-                * all the undo steps that should not be called from
-                * within a lock.
-                */
-               write_unlock_bh(&bond->lock);
-
                bond_destroy_slave_symlinks(bond_dev, slave_dev);
                bond_del_vlans_from_slave(bond, slave_dev);
 
@@ -1853,7 +1946,9 @@ static int bond_release_all(struct net_device *bond_dev)
                        }
 
                        /* flush master's mc_list from slave */
+                       netif_tx_lock_bh(bond_dev);
                        bond_mc_list_flush(bond_dev, slave_dev);
+                       netif_tx_unlock_bh(bond_dev);
                }
 
                netdev_set_master(slave_dev, NULL);
@@ -1861,10 +1956,12 @@ static int bond_release_all(struct net_device *bond_dev)
                /* close slave before restoring its mac address */
                dev_close(slave_dev);
 
-               /* restore original ("permanent") mac address*/
-               memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
-               addr.sa_family = slave_dev->type;
-               dev_set_mac_address(slave_dev, &addr);
+               if (!bond->params.fail_over_mac) {
+                       /* restore original ("permanent") mac address*/
+                       memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
+                       addr.sa_family = slave_dev->type;
+                       dev_set_mac_address(slave_dev, &addr);
+               }
 
                slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
                                           IFF_SLAVE_INACTIVE);
@@ -1932,16 +2029,19 @@ static int bond_ioctl_change_active(struct net_device *bond_dev, struct net_devi
                return -EINVAL;
        }
 
-       write_lock_bh(&bond->lock);
+       read_lock(&bond->lock);
 
+       read_lock(&bond->curr_slave_lock);
        old_active = bond->curr_active_slave;
+       read_unlock(&bond->curr_slave_lock);
+
        new_active = bond_get_slave_by_dev(bond, slave_dev);
 
        /*
         * Changing to the current active: do nothing; return success.
         */
        if (new_active && (new_active == old_active)) {
-               write_unlock_bh(&bond->lock);
+               read_unlock(&bond->lock);
                return 0;
        }
 
@@ -1949,12 +2049,14 @@ static int bond_ioctl_change_active(struct net_device *bond_dev, struct net_devi
            (old_active) &&
            (new_active->link == BOND_LINK_UP) &&
            IS_UP(new_active->dev)) {
+               write_lock_bh(&bond->curr_slave_lock);
                bond_change_active_slave(bond, new_active);
+               write_unlock_bh(&bond->curr_slave_lock);
        } else {
                res = -EINVAL;
        }
 
-       write_unlock_bh(&bond->lock);
+       read_unlock(&bond->lock);
 
        return res;
 }
@@ -1966,9 +2068,9 @@ static int bond_info_query(struct net_device *bond_dev, struct ifbond *info)
        info->bond_mode = bond->params.mode;
        info->miimon = bond->params.miimon;
 
-       read_lock_bh(&bond->lock);
+       read_lock(&bond->lock);
        info->num_slaves = bond->slave_cnt;
-       read_unlock_bh(&bond->lock);
+       read_unlock(&bond->lock);
 
        return 0;
 }
@@ -1983,7 +2085,7 @@ static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *in
                return -ENODEV;
        }
 
-       read_lock_bh(&bond->lock);
+       read_lock(&bond->lock);
 
        bond_for_each_slave(bond, slave, i) {
                if (i == (int)info->slave_id) {
@@ -1992,7 +2094,7 @@ static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *in
                }
        }
 
-       read_unlock_bh(&bond->lock);
+       read_unlock(&bond->lock);
 
        if (found) {
                strcpy(info->slave_name, slave->dev->name);
@@ -2008,26 +2110,25 @@ static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *in
 
 /*-------------------------------- Monitoring -------------------------------*/
 
-/* this function is called regularly to monitor each slave's link. */
-void bond_mii_monitor(struct net_device *bond_dev)
+/*
+ * if !have_locks, return nonzero if a failover is necessary.  if
+ * have_locks, do whatever failover activities are needed.
+ *
+ * This is to separate the inspection and failover steps for locking
+ * purposes; failover requires rtnl, but acquiring it for every
+ * inspection is undesirable, so a wrapper first does inspection, and
+ * the acquires the necessary locks and calls again to perform
+ * failover if needed.  Since all locks are dropped, a complete
+ * restart is needed between calls.
+ */
+static int __bond_mii_monitor(struct bonding *bond, int have_locks)
 {
-       struct bonding *bond = bond_dev->priv;
        struct slave *slave, *oldcurrent;
        int do_failover = 0;
-       int delta_in_ticks;
        int i;
 
-       read_lock(&bond->lock);
-
-       delta_in_ticks = (bond->params.miimon * HZ) / 1000;
-
-       if (bond->kill_timers) {
+       if (bond->slave_cnt == 0)
                goto out;
-       }
-
-       if (bond->slave_cnt == 0) {
-               goto re_arm;
-       }
 
        /* we will try to read the link status of each of our slaves, and
         * set their IFF_RUNNING flag appropriately. For each slave not
@@ -2035,6 +2136,17 @@ void bond_mii_monitor(struct net_device *bond_dev)
         * program could monitor the link itself if needed.
         */
 
+       if (bond->send_grat_arp) {
+               if (bond->curr_active_slave && test_bit(__LINK_STATE_LINKWATCH_PENDING,
+                               &bond->curr_active_slave->dev->state))
+                       dprintk("Needs to send gratuitous arp but not yet\n");
+               else {
+                       dprintk("sending delayed gratuitous arp on on %s\n",
+                               bond->curr_active_slave->dev->name);
+                       bond_send_gratuitous_arp(bond);
+                       bond->send_grat_arp = 0;
+               }
+       }
        read_lock(&bond->curr_slave_lock);
        oldcurrent = bond->curr_active_slave;
        read_unlock(&bond->curr_slave_lock);
@@ -2050,7 +2162,11 @@ void bond_mii_monitor(struct net_device *bond_dev)
                switch (slave->link) {
                case BOND_LINK_UP:      /* the link was up */
                        if (link_state == BMSR_LSTATUS) {
-                               /* link stays up, nothing more to do */
+                               if (!oldcurrent) {
+                                       if (!have_locks)
+                                               return 1;
+                                       do_failover = 1;
+                               }
                                break;
                        } else { /* link going down */
                                slave->link  = BOND_LINK_FAIL;
@@ -2065,7 +2181,7 @@ void bond_mii_monitor(struct net_device *bond_dev)
                                               ": %s: link status down for %s "
                                               "interface %s, disabling it in "
                                               "%d ms.\n",
-                                              bond_dev->name,
+                                              bond->dev->name,
                                               IS_UP(slave_dev)
                                               ? ((bond->params.mode == BOND_MODE_ACTIVEBACKUP)
                                                  ? ((slave == oldcurrent)
@@ -2083,6 +2199,9 @@ void bond_mii_monitor(struct net_device *bond_dev)
                        if (link_state != BMSR_LSTATUS) {
                                /* link stays down */
                                if (slave->delay <= 0) {
+                                       if (!have_locks)
+                                               return 1;
+
                                        /* link down for too long time */
                                        slave->link = BOND_LINK_DOWN;
 
@@ -2098,7 +2217,7 @@ void bond_mii_monitor(struct net_device *bond_dev)
                                               ": %s: link status definitely "
                                               "down for interface %s, "
                                               "disabling it\n",
-                                              bond_dev->name,
+                                              bond->dev->name,
                                               slave_dev->name);
 
                                        /* notify ad that the link status has changed */
@@ -2124,7 +2243,7 @@ void bond_mii_monitor(struct net_device *bond_dev)
                                printk(KERN_INFO DRV_NAME
                                       ": %s: link status up again after %d "
                                       "ms for interface %s.\n",
-                                      bond_dev->name,
+                                      bond->dev->name,
                                       (bond->params.downdelay - slave->delay) * bond->params.miimon,
                                       slave_dev->name);
                        }
@@ -2144,7 +2263,7 @@ void bond_mii_monitor(struct net_device *bond_dev)
                                               ": %s: link status up for "
                                               "interface %s, enabling it "
                                               "in %d ms.\n",
-                                              bond_dev->name,
+                                              bond->dev->name,
                                               slave_dev->name,
                                               bond->params.updelay * bond->params.miimon);
                                }
@@ -2160,12 +2279,15 @@ void bond_mii_monitor(struct net_device *bond_dev)
                                printk(KERN_INFO DRV_NAME
                                       ": %s: link status down again after %d "
                                       "ms for interface %s.\n",
-                                      bond_dev->name,
+                                      bond->dev->name,
                                       (bond->params.updelay - slave->delay) * bond->params.miimon,
                                       slave_dev->name);
                        } else {
                                /* link stays up */
                                if (slave->delay == 0) {
+                                       if (!have_locks)
+                                               return 1;
+
                                        /* now the link has been up for long time enough */
                                        slave->link = BOND_LINK_UP;
                                        slave->jiffies = jiffies;
@@ -2184,7 +2306,7 @@ void bond_mii_monitor(struct net_device *bond_dev)
                                        printk(KERN_INFO DRV_NAME
                                               ": %s: link status definitely "
                                               "up for interface %s.\n",
-                                              bond_dev->name,
+                                              bond->dev->name,
                                               slave_dev->name);
 
                                        /* notify ad that the link status has changed */
@@ -2210,7 +2332,7 @@ void bond_mii_monitor(struct net_device *bond_dev)
                        /* Should not happen */
                        printk(KERN_ERR DRV_NAME
                               ": %s: Error: %s Illegal value (link=%d)\n",
-                              bond_dev->name,
+                              bond->dev->name,
                               slave->dev->name,
                               slave->link);
                        goto out;
@@ -2231,24 +2353,56 @@ void bond_mii_monitor(struct net_device *bond_dev)
        } /* end of for */
 
        if (do_failover) {
-               write_lock(&bond->curr_slave_lock);
+               ASSERT_RTNL();
+
+               write_lock_bh(&bond->curr_slave_lock);
 
                bond_select_active_slave(bond);
 
-               write_unlock(&bond->curr_slave_lock);
+               write_unlock_bh(&bond->curr_slave_lock);
+
        } else
                bond_set_carrier(bond);
 
-re_arm:
-       if (bond->params.miimon) {
-               mod_timer(&bond->mii_timer, jiffies + delta_in_ticks);
-       }
 out:
-       read_unlock(&bond->lock);
+       return 0;
 }
 
+/*
+ * bond_mii_monitor
+ *
+ * Really a wrapper that splits the mii monitor into two phases: an
+ * inspection, then (if inspection indicates something needs to be
+ * done) an acquisition of appropriate locks followed by another pass
+ * to implement whatever link state changes are indicated.
+ */
+void bond_mii_monitor(struct work_struct *work)
+{
+       struct bonding *bond = container_of(work, struct bonding,
+                                           mii_work.work);
+       unsigned long delay;
+
+       read_lock(&bond->lock);
+       if (bond->kill_timers) {
+               read_unlock(&bond->lock);
+               return;
+       }
+       if (__bond_mii_monitor(bond, 0)) {
+               read_unlock(&bond->lock);
+               rtnl_lock();
+               read_lock(&bond->lock);
+               __bond_mii_monitor(bond, 1);
+               read_unlock(&bond->lock);
+               rtnl_unlock();  /* might sleep, hold no other locks */
+               read_lock(&bond->lock);
+       }
+
+       delay = ((bond->params.miimon * HZ) / 1000) ? : 1;
+       read_unlock(&bond->lock);
+       queue_delayed_work(bond->wq, &bond->mii_work, delay);
+}
 
-static u32 bond_glean_dev_ip(struct net_device *dev)
+static __be32 bond_glean_dev_ip(struct net_device *dev)
 {
        struct in_device *idev;
        struct in_ifaddr *ifa;
@@ -2291,7 +2445,7 @@ static int bond_has_ip(struct bonding *bond)
        return 0;
 }
 
-static int bond_has_this_ip(struct bonding *bond, u32 ip)
+static int bond_has_this_ip(struct bonding *bond, __be32 ip)
 {
        struct vlan_entry *vlan, *vlan_next;
 
@@ -2315,7 +2469,7 @@ static int bond_has_this_ip(struct bonding *bond, u32 ip)
  * switches in VLAN mode (especially if ports are configured as
  * "native" to a VLAN) might not pass non-tagged frames.
  */
-static void bond_arp_send(struct net_device *slave_dev, int arp_op, u32 dest_ip, u32 src_ip, unsigned short vlan_id)
+static void bond_arp_send(struct net_device *slave_dev, int arp_op, __be32 dest_ip, __be32 src_ip, unsigned short vlan_id)
 {
        struct sk_buff *skb;
 
@@ -2343,7 +2497,7 @@ static void bond_arp_send(struct net_device *slave_dev, int arp_op, u32 dest_ip,
 static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
 {
        int i, vlan_id, rv;
-       u32 *targets = bond->params.arp_targets;
+       __be32 *targets = bond->params.arp_targets;
        struct vlan_entry *vlan, *vlan_next;
        struct net_device *vlan_dev;
        struct flowi fl;
@@ -2369,7 +2523,7 @@ static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
                fl.fl4_dst = targets[i];
                fl.fl4_tos = RTO_ONLINK;
 
-               rv = ip_route_output_key(&rt, &fl);
+               rv = ip_route_output_key(&init_net, &rt, &fl);
                if (rv) {
                        if (net_ratelimit()) {
                                printk(KERN_WARNING DRV_NAME
@@ -2393,7 +2547,7 @@ static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
                vlan_id = 0;
                list_for_each_entry_safe(vlan, vlan_next, &bond->vlan_list,
                                         vlan_list) {
-                       vlan_dev = bond->vlgrp->vlan_devices[vlan->vlan_id];
+                       vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
                        if (vlan_dev == rt->u.dst.dev) {
                                vlan_id = vlan->vlan_id;
                                dprintk("basa: vlan match on %s %d\n",
@@ -2436,11 +2590,11 @@ static void bond_send_gratuitous_arp(struct bonding *bond)
 
        if (bond->master_ip) {
                bond_arp_send(slave->dev, ARPOP_REPLY, bond->master_ip,
-                                 bond->master_ip, 0);
+                               bond->master_ip, 0);
        }
 
        list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
-               vlan_dev = bond->vlgrp->vlan_devices[vlan->vlan_id];
+               vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
                if (vlan->vlan_ip) {
                        bond_arp_send(slave->dev, ARPOP_REPLY, vlan->vlan_ip,
                                      vlan->vlan_ip, vlan->vlan_id);
@@ -2448,10 +2602,10 @@ static void bond_send_gratuitous_arp(struct bonding *bond)
        }
 }
 
-static void bond_validate_arp(struct bonding *bond, struct slave *slave, u32 sip, u32 tip)
+static void bond_validate_arp(struct bonding *bond, struct slave *slave, __be32 sip, __be32 tip)
 {
        int i;
-       u32 *targets = bond->params.arp_targets;
+       __be32 *targets = bond->params.arp_targets;
 
        targets = bond->params.arp_targets;
        for (i = 0; (i < BOND_MAX_ARP_TARGETS) && targets[i]; i++) {
@@ -2473,7 +2627,10 @@ static int bond_arp_rcv(struct sk_buff *skb, struct net_device *dev, struct pack
        struct slave *slave;
        struct bonding *bond;
        unsigned char *arp_ptr;
-       u32 sip, tip;
+       __be32 sip, tip;
+
+       if (dev_net(dev) != &init_net)
+               goto out;
 
        if (!(dev->priv_flags & IFF_BONDING) || !(dev->flags & IFF_MASTER))
                goto out;
@@ -2489,13 +2646,10 @@ static int bond_arp_rcv(struct sk_buff *skb, struct net_device *dev, struct pack
        if (!slave || !slave_do_arp_validate(bond, slave))
                goto out_unlock;
 
-       /* ARP header, plus 2 device addresses, plus 2 IP addresses.  */
-       if (!pskb_may_pull(skb, (sizeof(struct arphdr) +
-                                (2 * dev->addr_len) +
-                                (2 * sizeof(u32)))))
+       if (!pskb_may_pull(skb, arp_hdr_len(dev)))
                goto out_unlock;
 
-       arp = skb->nh.arph;
+       arp = arp_hdr(skb);
        if (arp->ar_hln != dev->addr_len ||
            skb->pkt_type == PACKET_OTHERHOST ||
            skb->pkt_type == PACKET_LOOPBACK ||
@@ -2542,9 +2696,10 @@ out:
  * arp is transmitted to generate traffic. see activebackup_arp_monitor for
  * arp monitoring in active backup mode.
  */
-void bond_loadbalance_arp_mon(struct net_device *bond_dev)
+void bond_loadbalance_arp_mon(struct work_struct *work)
 {
-       struct bonding *bond = bond_dev->priv;
+       struct bonding *bond = container_of(work, struct bonding,
+                                           arp_work.work);
        struct slave *slave, *oldcurrent;
        int do_failover = 0;
        int delta_in_ticks;
@@ -2576,8 +2731,8 @@ void bond_loadbalance_arp_mon(struct net_device *bond_dev)
         */
        bond_for_each_slave(bond, slave, i) {
                if (slave->link != BOND_LINK_UP) {
-                       if (((jiffies - slave->dev->trans_start) <= delta_in_ticks) &&
-                           ((jiffies - slave->dev->last_rx) <= delta_in_ticks)) {
+                       if (time_before_eq(jiffies, slave->dev->trans_start + delta_in_ticks) &&
+                           time_before_eq(jiffies, slave->dev->last_rx + delta_in_ticks)) {
 
                                slave->link  = BOND_LINK_UP;
                                slave->state = BOND_STATE_ACTIVE;
@@ -2591,13 +2746,13 @@ void bond_loadbalance_arp_mon(struct net_device *bond_dev)
                                        printk(KERN_INFO DRV_NAME
                                               ": %s: link status definitely "
                                               "up for interface %s, ",
-                                              bond_dev->name,
+                                              bond->dev->name,
                                               slave->dev->name);
                                        do_failover = 1;
                                } else {
                                        printk(KERN_INFO DRV_NAME
                                               ": %s: interface %s is now up\n",
-                                              bond_dev->name,
+                                              bond->dev->name,
                                               slave->dev->name);
                                }
                        }
@@ -2608,8 +2763,8 @@ void bond_loadbalance_arp_mon(struct net_device *bond_dev)
                         * when the source ip is 0, so don't take the link down
                         * if we don't know our ip yet
                         */
-                       if (((jiffies - slave->dev->trans_start) >= (2*delta_in_ticks)) ||
-                           (((jiffies - slave->dev->last_rx) >= (2*delta_in_ticks)) &&
+                       if (time_after_eq(jiffies, slave->dev->trans_start + 2*delta_in_ticks) ||
+                           (time_after_eq(jiffies, slave->dev->last_rx + 2*delta_in_ticks) &&
                             bond_has_ip(bond))) {
 
                                slave->link  = BOND_LINK_DOWN;
@@ -2621,7 +2776,7 @@ void bond_loadbalance_arp_mon(struct net_device *bond_dev)
 
                                printk(KERN_INFO DRV_NAME
                                       ": %s: interface %s is now down.\n",
-                                      bond_dev->name,
+                                      bond->dev->name,
                                       slave->dev->name);
 
                                if (slave == oldcurrent) {
@@ -2643,17 +2798,16 @@ void bond_loadbalance_arp_mon(struct net_device *bond_dev)
        }
 
        if (do_failover) {
-               write_lock(&bond->curr_slave_lock);
+               write_lock_bh(&bond->curr_slave_lock);
 
                bond_select_active_slave(bond);
 
-               write_unlock(&bond->curr_slave_lock);
+               write_unlock_bh(&bond->curr_slave_lock);
        }
 
 re_arm:
-       if (bond->params.arp_interval) {
-               mod_timer(&bond->arp_timer, jiffies + delta_in_ticks);
-       }
+       if (bond->params.arp_interval)
+               queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
 out:
        read_unlock(&bond->lock);
 }
@@ -2673,9 +2827,10 @@ out:
  * may have received.
  * see loadbalance_arp_monitor for arp monitoring in load balancing mode
  */
-void bond_activebackup_arp_mon(struct net_device *bond_dev)
+void bond_activebackup_arp_mon(struct work_struct *work)
 {
-       struct bonding *bond = bond_dev->priv;
+       struct bonding *bond = container_of(work, struct bonding,
+                                           arp_work.work);
        struct slave *slave;
        int delta_in_ticks;
        int i;
@@ -2699,15 +2854,15 @@ void bond_activebackup_arp_mon(struct net_device *bond_dev)
         */
        bond_for_each_slave(bond, slave, i) {
                if (slave->link != BOND_LINK_UP) {
-                       if ((jiffies - slave_last_rx(bond, slave)) <=
-                            delta_in_ticks) {
+                       if (time_before_eq(jiffies,
+                           slave_last_rx(bond, slave) + delta_in_ticks)) {
 
                                slave->link = BOND_LINK_UP;
 
-                               write_lock(&bond->curr_slave_lock);
+                               write_lock_bh(&bond->curr_slave_lock);
 
                                if ((!bond->curr_active_slave) &&
-                                   ((jiffies - slave->dev->trans_start) <= delta_in_ticks)) {
+                                   time_before_eq(jiffies, slave->dev->trans_start + delta_in_ticks)) {
                                        bond_change_active_slave(bond, slave);
                                        bond->current_arp_slave = NULL;
                                } else if (bond->curr_active_slave != slave) {
@@ -2727,25 +2882,25 @@ void bond_activebackup_arp_mon(struct net_device *bond_dev)
                                        printk(KERN_INFO DRV_NAME
                                               ": %s: %s is up and now the "
                                               "active interface\n",
-                                              bond_dev->name,
+                                              bond->dev->name,
                                               slave->dev->name);
                                        netif_carrier_on(bond->dev);
                                } else {
                                        printk(KERN_INFO DRV_NAME
                                               ": %s: backup interface %s is "
                                               "now up\n",
-                                              bond_dev->name,
+                                              bond->dev->name,
                                               slave->dev->name);
                                }
 
-                               write_unlock(&bond->curr_slave_lock);
+                               write_unlock_bh(&bond->curr_slave_lock);
                        }
                } else {
                        read_lock(&bond->curr_slave_lock);
 
                        if ((slave != bond->curr_active_slave) &&
                            (!bond->current_arp_slave) &&
-                           (((jiffies - slave_last_rx(bond, slave)) >= 3*delta_in_ticks) &&
+                           (time_after_eq(jiffies, slave_last_rx(bond, slave) + 3*delta_in_ticks) &&
                             bond_has_ip(bond))) {
                                /* a backup slave has gone down; three times
                                 * the delta allows the current slave to be
@@ -2770,7 +2925,7 @@ void bond_activebackup_arp_mon(struct net_device *bond_dev)
 
                                printk(KERN_INFO DRV_NAME
                                       ": %s: backup interface %s is now down\n",
-                                      bond_dev->name,
+                                      bond->dev->name,
                                       slave->dev->name);
                        } else {
                                read_unlock(&bond->curr_slave_lock);
@@ -2791,10 +2946,10 @@ void bond_activebackup_arp_mon(struct net_device *bond_dev)
                 * before being taken out. if a primary is being used, check
                 * if it is up and needs to take over as the curr_active_slave
                 */
-               if ((((jiffies - slave->dev->trans_start) >= (2*delta_in_ticks)) ||
-           (((jiffies - slave_last_rx(bond, slave)) >= (2*delta_in_ticks)) &&
-            bond_has_ip(bond))) &&
-                   ((jiffies - slave->jiffies) >= 2*delta_in_ticks)) {
+               if ((time_after_eq(jiffies, slave->dev->trans_start + 2*delta_in_ticks) ||
+                       (time_after_eq(jiffies, slave_last_rx(bond, slave) + 2*delta_in_ticks) &&
+                        bond_has_ip(bond))) &&
+                       time_after_eq(jiffies, slave->jiffies + 2*delta_in_ticks)) {
 
                        slave->link  = BOND_LINK_DOWN;
 
@@ -2805,15 +2960,15 @@ void bond_activebackup_arp_mon(struct net_device *bond_dev)
                        printk(KERN_INFO DRV_NAME
                               ": %s: link status down for active interface "
                               "%s, disabling it\n",
-                              bond_dev->name,
+                              bond->dev->name,
                               slave->dev->name);
 
-                       write_lock(&bond->curr_slave_lock);
+                       write_lock_bh(&bond->curr_slave_lock);
 
                        bond_select_active_slave(bond);
                        slave = bond->curr_active_slave;
 
-                       write_unlock(&bond->curr_slave_lock);
+                       write_unlock_bh(&bond->curr_slave_lock);
 
                        bond->current_arp_slave = slave;
 
@@ -2827,14 +2982,14 @@ void bond_activebackup_arp_mon(struct net_device *bond_dev)
                        printk(KERN_INFO DRV_NAME
                               ": %s: changing from interface %s to primary "
                               "interface %s\n",
-                              bond_dev->name,
+                              bond->dev->name,
                               slave->dev->name,
                               bond->primary_slave->dev->name);
 
                        /* primary is up so switch to it */
-                       write_lock(&bond->curr_slave_lock);
+                       write_lock_bh(&bond->curr_slave_lock);
                        bond_change_active_slave(bond, bond->primary_slave);
-                       write_unlock(&bond->curr_slave_lock);
+                       write_unlock_bh(&bond->curr_slave_lock);
 
                        slave = bond->primary_slave;
                        slave->jiffies = jiffies;
@@ -2891,7 +3046,7 @@ void bond_activebackup_arp_mon(struct net_device *bond_dev)
                                        printk(KERN_INFO DRV_NAME
                                               ": %s: backup interface %s is "
                                               "now down.\n",
-                                              bond_dev->name,
+                                              bond->dev->name,
                                               slave->dev->name);
                                }
                        }
@@ -2900,7 +3055,7 @@ void bond_activebackup_arp_mon(struct net_device *bond_dev)
 
 re_arm:
        if (bond->params.arp_interval) {
-               mod_timer(&bond->arp_timer, jiffies + delta_in_ticks);
+               queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
        }
 out:
        read_unlock(&bond->lock);
@@ -2921,7 +3076,7 @@ static void *bond_info_seq_start(struct seq_file *seq, loff_t *pos)
 
        /* make sure the bond won't be taken away */
        read_lock(&dev_base_lock);
-       read_lock_bh(&bond->lock);
+       read_lock(&bond->lock);
 
        if (*pos == 0) {
                return SEQ_START_TOKEN;
@@ -2955,7 +3110,7 @@ static void bond_info_seq_stop(struct seq_file *seq, void *v)
 {
        struct bonding *bond = seq->private;
 
-       read_unlock_bh(&bond->lock);
+       read_unlock(&bond->lock);
        read_unlock(&dev_base_lock);
 }
 
@@ -2970,9 +3125,15 @@ static void bond_info_show_master(struct seq_file *seq)
        curr = bond->curr_active_slave;
        read_unlock(&bond->curr_slave_lock);
 
-       seq_printf(seq, "Bonding Mode: %s\n",
+       seq_printf(seq, "Bonding Mode: %s",
                   bond_mode_name(bond->params.mode));
 
+       if (bond->params.mode == BOND_MODE_ACTIVEBACKUP &&
+           bond->params.fail_over_mac)
+               seq_printf(seq, " (fail_over_mac)");
+
+       seq_printf(seq, "\n");
+
        if (bond->params.mode == BOND_MODE_XOR ||
                bond->params.mode == BOND_MODE_8023AD) {
                seq_printf(seq, "Transmit Hash Policy: %s (%d)\n",
@@ -3020,6 +3181,7 @@ static void bond_info_show_master(struct seq_file *seq)
 
        if (bond->params.mode == BOND_MODE_8023AD) {
                struct ad_info ad_info;
+               DECLARE_MAC_BUF(mac);
 
                seq_puts(seq, "\n802.3ad info\n");
                seq_printf(seq, "LACP rate: %s\n",
@@ -3039,13 +3201,8 @@ static void bond_info_show_master(struct seq_file *seq)
                                   ad_info.actor_key);
                        seq_printf(seq, "\tPartner Key: %d\n",
                                   ad_info.partner_key);
-                       seq_printf(seq, "\tPartner Mac Address: %02x:%02x:%02x:%02x:%02x:%02x\n",
-                                  ad_info.partner_system[0],
-                                  ad_info.partner_system[1],
-                                  ad_info.partner_system[2],
-                                  ad_info.partner_system[3],
-                                  ad_info.partner_system[4],
-                                  ad_info.partner_system[5]);
+                       seq_printf(seq, "\tPartner Mac Address: %s\n",
+                                  print_mac(mac, ad_info.partner_system));
                }
        }
 }
@@ -3053,6 +3210,7 @@ static void bond_info_show_master(struct seq_file *seq)
 static void bond_info_show_slave(struct seq_file *seq, const struct slave *slave)
 {
        struct bonding *bond = seq->private;
+       DECLARE_MAC_BUF(mac);
 
        seq_printf(seq, "\nSlave Interface: %s\n", slave->dev->name);
        seq_printf(seq, "MII Status: %s\n",
@@ -3061,10 +3219,8 @@ static void bond_info_show_slave(struct seq_file *seq, const struct slave *slave
                   slave->link_failure_count);
 
        seq_printf(seq,
-                  "Permanent HW addr: %02x:%02x:%02x:%02x:%02x:%02x\n",
-                  slave->perm_hwaddr[0], slave->perm_hwaddr[1],
-                  slave->perm_hwaddr[2], slave->perm_hwaddr[3],
-                  slave->perm_hwaddr[4], slave->perm_hwaddr[5]);
+                  "Permanent HW addr: %s\n",
+                  print_mac(mac, slave->perm_hwaddr));
 
        if (bond->params.mode == BOND_MODE_8023AD) {
                const struct aggregator *agg
@@ -3115,7 +3271,7 @@ static int bond_info_open(struct inode *inode, struct file *file)
        return res;
 }
 
-static struct file_operations bond_info_fops = {
+static const struct file_operations bond_info_fops = {
        .owner   = THIS_MODULE,
        .open    = bond_info_open,
        .read    = seq_read,
@@ -3162,7 +3318,7 @@ static void bond_create_proc_dir(void)
 {
        int len = strlen(DRV_NAME);
 
-       for (bond_proc_dir = proc_net->subdir; bond_proc_dir;
+       for (bond_proc_dir = init_net.proc_net->subdir; bond_proc_dir;
             bond_proc_dir = bond_proc_dir->next) {
                if ((bond_proc_dir->namelen == len) &&
                    !memcmp(bond_proc_dir->name, DRV_NAME, len)) {
@@ -3171,7 +3327,7 @@ static void bond_create_proc_dir(void)
        }
 
        if (!bond_proc_dir) {
-               bond_proc_dir = proc_mkdir(DRV_NAME, proc_net);
+               bond_proc_dir = proc_mkdir(DRV_NAME, init_net.proc_net);
                if (bond_proc_dir) {
                        bond_proc_dir->owner = THIS_MODULE;
                } else {
@@ -3206,7 +3362,7 @@ static void bond_destroy_proc_dir(void)
                        bond_proc_dir->owner = NULL;
                }
        } else {
-               remove_proc_entry(DRV_NAME, proc_net);
+               remove_proc_entry(DRV_NAME, init_net.proc_net);
                bond_proc_dir = NULL;
        }
 }
@@ -3238,9 +3394,7 @@ static int bond_master_netdev_event(unsigned long event, struct net_device *bond
        case NETDEV_CHANGENAME:
                return bond_event_changename(event_bond);
        case NETDEV_UNREGISTER:
-               /*
-                * TODO: remove a bond from the list?
-                */
+               bond_release_all(event_bond->dev);
                break;
        default:
                break;
@@ -3257,7 +3411,10 @@ static int bond_slave_netdev_event(unsigned long event, struct net_device *slave
        switch (event) {
        case NETDEV_UNREGISTER:
                if (bond_dev) {
-                       bond_release(bond_dev, slave_dev);
+                       if (bond->setup_by_slave)
+                               bond_release_and_destroy(bond_dev, slave_dev);
+                       else
+                               bond_release(bond_dev, slave_dev);
                }
                break;
        case NETDEV_CHANGE:
@@ -3313,6 +3470,9 @@ static int bond_netdev_event(struct notifier_block *this, unsigned long event, v
 {
        struct net_device *event_dev = (struct net_device *)ptr;
 
+       if (dev_net(event_dev) != &init_net)
+               return NOTIFY_DONE;
+
        dprintk("event_dev: %s, event: %lx\n",
                (event_dev ? event_dev->name : "None"),
                event);
@@ -3348,6 +3508,9 @@ static int bond_inetaddr_event(struct notifier_block *this, unsigned long event,
        struct bonding *bond, *bond_next;
        struct vlan_entry *vlan, *vlan_next;
 
+       if (dev_net(ifa->ifa_dev->dev) != &init_net)
+               return NOTIFY_DONE;
+
        list_for_each_entry_safe(bond, bond_next, &bond_dev_list, bond_list) {
                if (bond->dev == event_dev) {
                        switch (event) {
@@ -3367,7 +3530,7 @@ static int bond_inetaddr_event(struct notifier_block *this, unsigned long event,
 
                list_for_each_entry_safe(vlan, vlan_next, &bond->vlan_list,
                                         vlan_list) {
-                       vlan_dev = bond->vlgrp->vlan_devices[vlan->vlan_id];
+                       vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
                        if (vlan_dev == event_dev) {
                                switch (event) {
                                case NETDEV_UP:
@@ -3419,21 +3582,45 @@ void bond_register_arp(struct bonding *bond)
 {
        struct packet_type *pt = &bond->arp_mon_pt;
 
+       if (pt->type)
+               return;
+
        pt->type = htons(ETH_P_ARP);
-       pt->dev = NULL; /*bond->dev;XXX*/
+       pt->dev = bond->dev;
        pt->func = bond_arp_rcv;
        dev_add_pack(pt);
 }
 
 void bond_unregister_arp(struct bonding *bond)
 {
-       dev_remove_pack(&bond->arp_mon_pt);
+       struct packet_type *pt = &bond->arp_mon_pt;
+
+       dev_remove_pack(pt);
+       pt->type = 0;
 }
 
 /*---------------------------- Hashing Policies -----------------------------*/
 
 /*
- * Hash for the the output device based upon layer 3 and layer 4 data. If
+ * Hash for the output device based upon layer 2 and layer 3 data. If
+ * the packet is not IP mimic bond_xmit_hash_policy_l2()
+ */
+static int bond_xmit_hash_policy_l23(struct sk_buff *skb,
+                                    struct net_device *bond_dev, int count)
+{
+       struct ethhdr *data = (struct ethhdr *)skb->data;
+       struct iphdr *iph = ip_hdr(skb);
+
+       if (skb->protocol == __constant_htons(ETH_P_IP)) {
+               return ((ntohl(iph->saddr ^ iph->daddr) & 0xffff) ^
+                       (data->h_dest[5] ^ bond_dev->dev_addr[5])) % count;
+       }
+
+       return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
+}
+
+/*
+ * Hash for the output device based upon layer 3 and layer 4 data. If
  * the packet is a frag or not TCP or UDP, just use layer 3 data.  If it is
  * altogether not IP, mimic bond_xmit_hash_policy_l2()
  */
@@ -3441,15 +3628,15 @@ static int bond_xmit_hash_policy_l34(struct sk_buff *skb,
                                    struct net_device *bond_dev, int count)
 {
        struct ethhdr *data = (struct ethhdr *)skb->data;
-       struct iphdr *iph = skb->nh.iph;
-       u16 *layer4hdr = (u16 *)((u32 *)iph + iph->ihl);
+       struct iphdr *iph = ip_hdr(skb);
+       __be16 *layer4hdr = (__be16 *)((u32 *)iph + iph->ihl);
        int layer4_xor = 0;
 
        if (skb->protocol == __constant_htons(ETH_P_IP)) {
                if (!(iph->frag_off & __constant_htons(IP_MF|IP_OFFSET)) &&
                    (iph->protocol == IPPROTO_TCP ||
                     iph->protocol == IPPROTO_UDP)) {
-                       layer4_xor = htons((*layer4hdr ^ *(layer4hdr + 1)));
+                       layer4_xor = ntohs((*layer4hdr ^ *(layer4hdr + 1)));
                }
                return (layer4_xor ^
                        ((ntohl(iph->saddr ^ iph->daddr)) & 0xffff)) % count;
@@ -3475,15 +3662,11 @@ static int bond_xmit_hash_policy_l2(struct sk_buff *skb,
 static int bond_open(struct net_device *bond_dev)
 {
        struct bonding *bond = bond_dev->priv;
-       struct timer_list *mii_timer = &bond->mii_timer;
-       struct timer_list *arp_timer = &bond->arp_timer;
 
        bond->kill_timers = 0;
 
        if ((bond->params.mode == BOND_MODE_TLB) ||
            (bond->params.mode == BOND_MODE_ALB)) {
-               struct timer_list *alb_timer = &(BOND_ALB_INFO(bond).alb_timer);
-
                /* bond_alb_initialize must be called before the timer
                 * is started.
                 */
@@ -3492,44 +3675,31 @@ static int bond_open(struct net_device *bond_dev)
                        return -1;
                }
 
-               init_timer(alb_timer);
-               alb_timer->expires  = jiffies + 1;
-               alb_timer->data     = (unsigned long)bond;
-               alb_timer->function = (void *)&bond_alb_monitor;
-               add_timer(alb_timer);
+               INIT_DELAYED_WORK(&bond->alb_work, bond_alb_monitor);
+               queue_delayed_work(bond->wq, &bond->alb_work, 0);
        }
 
        if (bond->params.miimon) {  /* link check interval, in milliseconds. */
-               init_timer(mii_timer);
-               mii_timer->expires  = jiffies + 1;
-               mii_timer->data     = (unsigned long)bond_dev;
-               mii_timer->function = (void *)&bond_mii_monitor;
-               add_timer(mii_timer);
+               INIT_DELAYED_WORK(&bond->mii_work, bond_mii_monitor);
+               queue_delayed_work(bond->wq, &bond->mii_work, 0);
        }
 
        if (bond->params.arp_interval) {  /* arp interval, in milliseconds. */
-               init_timer(arp_timer);
-               arp_timer->expires  = jiffies + 1;
-               arp_timer->data     = (unsigned long)bond_dev;
-               if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
-                       arp_timer->function = (void *)&bond_activebackup_arp_mon;
-               } else {
-                       arp_timer->function = (void *)&bond_loadbalance_arp_mon;
-               }
+               if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
+                       INIT_DELAYED_WORK(&bond->arp_work,
+                                         bond_activebackup_arp_mon);
+               else
+                       INIT_DELAYED_WORK(&bond->arp_work,
+                                         bond_loadbalance_arp_mon);
+
+               queue_delayed_work(bond->wq, &bond->arp_work, 0);
                if (bond->params.arp_validate)
                        bond_register_arp(bond);
-
-               add_timer(arp_timer);
        }
 
        if (bond->params.mode == BOND_MODE_8023AD) {
-               struct timer_list *ad_timer = &(BOND_AD_INFO(bond).ad_timer);
-               init_timer(ad_timer);
-               ad_timer->expires  = jiffies + 1;
-               ad_timer->data     = (unsigned long)bond;
-               ad_timer->function = (void *)&bond_3ad_state_machine_handler;
-               add_timer(ad_timer);
-
+               INIT_DELAYED_WORK(&bond->ad_work, bond_3ad_state_machine_handler);
+               queue_delayed_work(bond->wq, &bond->ad_work, 0);
                /* register to receive LACPDUs */
                bond_register_lacpdu(bond);
        }
@@ -3557,25 +3727,21 @@ static int bond_close(struct net_device *bond_dev)
 
        write_unlock_bh(&bond->lock);
 
-       /* del_timer_sync must run without holding the bond->lock
-        * because a running timer might be trying to hold it too
-        */
-
        if (bond->params.miimon) {  /* link check interval, in milliseconds. */
-               del_timer_sync(&bond->mii_timer);
+               cancel_delayed_work(&bond->mii_work);
        }
 
        if (bond->params.arp_interval) {  /* arp interval, in milliseconds. */
-               del_timer_sync(&bond->arp_timer);
+               cancel_delayed_work(&bond->arp_work);
        }
 
        switch (bond->params.mode) {
        case BOND_MODE_8023AD:
-               del_timer_sync(&(BOND_AD_INFO(bond).ad_timer));
+               cancel_delayed_work(&bond->ad_work);
                break;
        case BOND_MODE_TLB:
        case BOND_MODE_ALB:
-               del_timer_sync(&(BOND_ALB_INFO(bond).alb_timer));
+               cancel_delayed_work(&bond->alb_work);
                break;
        default:
                break;
@@ -3597,43 +3763,45 @@ static struct net_device_stats *bond_get_stats(struct net_device *bond_dev)
 {
        struct bonding *bond = bond_dev->priv;
        struct net_device_stats *stats = &(bond->stats), *sstats;
+       struct net_device_stats local_stats;
        struct slave *slave;
        int i;
 
-       memset(stats, 0, sizeof(struct net_device_stats));
+       memset(&local_stats, 0, sizeof(struct net_device_stats));
 
        read_lock_bh(&bond->lock);
 
        bond_for_each_slave(bond, slave, i) {
                sstats = slave->dev->get_stats(slave->dev);
+               local_stats.rx_packets += sstats->rx_packets;
+               local_stats.rx_bytes += sstats->rx_bytes;
+               local_stats.rx_errors += sstats->rx_errors;
+               local_stats.rx_dropped += sstats->rx_dropped;
 
-               stats->rx_packets += sstats->rx_packets;
-               stats->rx_bytes += sstats->rx_bytes;
-               stats->rx_errors += sstats->rx_errors;
-               stats->rx_dropped += sstats->rx_dropped;
-
-               stats->tx_packets += sstats->tx_packets;
-               stats->tx_bytes += sstats->tx_bytes;
-               stats->tx_errors += sstats->tx_errors;
-               stats->tx_dropped += sstats->tx_dropped;
+               local_stats.tx_packets += sstats->tx_packets;
+               local_stats.tx_bytes += sstats->tx_bytes;
+               local_stats.tx_errors += sstats->tx_errors;
+               local_stats.tx_dropped += sstats->tx_dropped;
 
-               stats->multicast += sstats->multicast;
-               stats->collisions += sstats->collisions;
+               local_stats.multicast += sstats->multicast;
+               local_stats.collisions += sstats->collisions;
 
-               stats->rx_length_errors += sstats->rx_length_errors;
-               stats->rx_over_errors += sstats->rx_over_errors;
-               stats->rx_crc_errors += sstats->rx_crc_errors;
-               stats->rx_frame_errors += sstats->rx_frame_errors;
-               stats->rx_fifo_errors += sstats->rx_fifo_errors;
-               stats->rx_missed_errors += sstats->rx_missed_errors;
+               local_stats.rx_length_errors += sstats->rx_length_errors;
+               local_stats.rx_over_errors += sstats->rx_over_errors;
+               local_stats.rx_crc_errors += sstats->rx_crc_errors;
+               local_stats.rx_frame_errors += sstats->rx_frame_errors;
+               local_stats.rx_fifo_errors += sstats->rx_fifo_errors;
+               local_stats.rx_missed_errors += sstats->rx_missed_errors;
 
-               stats->tx_aborted_errors += sstats->tx_aborted_errors;
-               stats->tx_carrier_errors += sstats->tx_carrier_errors;
-               stats->tx_fifo_errors += sstats->tx_fifo_errors;
-               stats->tx_heartbeat_errors += sstats->tx_heartbeat_errors;
-               stats->tx_window_errors += sstats->tx_window_errors;
+               local_stats.tx_aborted_errors += sstats->tx_aborted_errors;
+               local_stats.tx_carrier_errors += sstats->tx_carrier_errors;
+               local_stats.tx_fifo_errors += sstats->tx_fifo_errors;
+               local_stats.tx_heartbeat_errors += sstats->tx_heartbeat_errors;
+               local_stats.tx_window_errors += sstats->tx_window_errors;
        }
 
+       memcpy(stats, &local_stats, sizeof(struct net_device_stats));
+
        read_unlock_bh(&bond->lock);
 
        return stats;
@@ -3673,13 +3841,13 @@ static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd
                if (mii->reg_num == 1) {
                        struct bonding *bond = bond_dev->priv;
                        mii->val_out = 0;
-                       read_lock_bh(&bond->lock);
+                       read_lock(&bond->lock);
                        read_lock(&bond->curr_slave_lock);
-                       if (bond->curr_active_slave) {
+                       if (netif_carrier_ok(bond->dev)) {
                                mii->val_out = BMSR_LSTATUS;
                        }
                        read_unlock(&bond->curr_slave_lock);
-                       read_unlock_bh(&bond->lock);
+                       read_unlock(&bond->lock);
                }
 
                return 0;
@@ -3725,7 +3893,7 @@ static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd
        }
 
        down_write(&(bonding_rwsem));
-       slave_dev = dev_get_by_name(ifr->ifr_slave);
+       slave_dev = dev_get_by_name(&init_net, ifr->ifr_slave);
 
        dprintk("slave_dev=%p: \n", slave_dev);
 
@@ -3766,8 +3934,6 @@ static void bond_set_multicast_list(struct net_device *bond_dev)
        struct bonding *bond = bond_dev->priv;
        struct dev_mc_list *dmi;
 
-       write_lock_bh(&bond->lock);
-
        /*
         * Do promisc before checking multicast_mode
         */
@@ -3788,6 +3954,8 @@ static void bond_set_multicast_list(struct net_device *bond_dev)
                bond_set_allmulti(bond, -1);
        }
 
+       read_lock(&bond->lock);
+
        bond->flags = bond_dev->flags;
 
        /* looking for addresses to add to slaves' mc list */
@@ -3808,7 +3976,7 @@ static void bond_set_multicast_list(struct net_device *bond_dev)
        bond_mc_list_destroy(bond);
        bond_mc_list_copy(bond_dev->mc_list, bond, GFP_ATOMIC);
 
-       write_unlock_bh(&bond->lock);
+       read_unlock(&bond->lock);
 }
 
 /*
@@ -3896,6 +4064,13 @@ static int bond_set_mac_address(struct net_device *bond_dev, void *addr)
 
        dprintk("bond=%p, name=%s\n", bond, (bond_dev ? bond_dev->name : "None"));
 
+       /*
+        * If fail_over_mac is enabled, do nothing and return success.
+        * Returning an error causes ifenslave to fail.
+        */
+       if (bond->params.fail_over_mac)
+               return 0;
+
        if (!is_valid_ether_addr(sa->sa_data)) {
                return -EADDRNOTAVAIL;
        }
@@ -3964,8 +4139,7 @@ static int bond_xmit_roundrobin(struct sk_buff *skb, struct net_device *bond_dev
 {
        struct bonding *bond = bond_dev->priv;
        struct slave *slave, *start_at;
-       int i;
-       int res = 1;
+       int i, slave_no, res = 1;
 
        read_lock(&bond->lock);
 
@@ -3973,29 +4147,29 @@ static int bond_xmit_roundrobin(struct sk_buff *skb, struct net_device *bond_dev
                goto out;
        }
 
-       read_lock(&bond->curr_slave_lock);
-       slave = start_at = bond->curr_active_slave;
-       read_unlock(&bond->curr_slave_lock);
+       /*
+        * Concurrent TX may collide on rr_tx_counter; we accept that
+        * as being rare enough not to justify using an atomic op here
+        */
+       slave_no = bond->rr_tx_counter++ % bond->slave_cnt;
 
-       if (!slave) {
-               goto out;
+       bond_for_each_slave(bond, slave, i) {
+               slave_no--;
+               if (slave_no < 0) {
+                       break;
+               }
        }
 
+       start_at = slave;
        bond_for_each_slave_from(bond, slave, i, start_at) {
                if (IS_UP(slave->dev) &&
                    (slave->link == BOND_LINK_UP) &&
                    (slave->state == BOND_STATE_ACTIVE)) {
                        res = bond_dev_queue_xmit(bond, skb, slave->dev);
-
-                       write_lock(&bond->curr_slave_lock);
-                       bond->curr_active_slave = slave->next;
-                       write_unlock(&bond->curr_slave_lock);
-
                        break;
                }
        }
 
-
 out:
        if (res) {
                /* no suitable interface, frame not sent */
@@ -4005,42 +4179,6 @@ out:
        return 0;
 }
 
-static void bond_activebackup_xmit_copy(struct sk_buff *skb,
-                                        struct bonding *bond,
-                                        struct slave *slave)
-{
-       struct sk_buff *skb2 = skb_copy(skb, GFP_ATOMIC);
-       struct ethhdr *eth_data;
-       u8 *hwaddr;
-       int res;
-
-       if (!skb2) {
-               printk(KERN_ERR DRV_NAME ": Error: "
-                      "bond_activebackup_xmit_copy(): skb_copy() failed\n");
-               return;
-       }
-
-       skb2->mac.raw = (unsigned char *)skb2->data;
-       eth_data = eth_hdr(skb2);
-
-       /* Pick an appropriate source MAC address
-        *      -- use slave's perm MAC addr, unless used by bond
-        *      -- otherwise, borrow active slave's perm MAC addr
-        *         since that will not be used
-        */
-       hwaddr = slave->perm_hwaddr;
-       if (!memcmp(eth_data->h_source, hwaddr, ETH_ALEN))
-               hwaddr = bond->curr_active_slave->perm_hwaddr;
-
-       /* Set source MAC address appropriately */
-       memcpy(eth_data->h_source, hwaddr, ETH_ALEN);
-
-       res = bond_dev_queue_xmit(bond, skb2, slave->dev);
-       if (res)
-               dev_kfree_skb(skb2);
-
-       return;
-}
 
 /*
  * in active-backup mode, we know that bond->curr_active_slave is always valid if
@@ -4061,21 +4199,6 @@ static int bond_xmit_activebackup(struct sk_buff *skb, struct net_device *bond_d
        if (!bond->curr_active_slave)
                goto out;
 
-       /* Xmit IGMP frames on all slaves to ensure rapid fail-over
-          for multicast traffic on snooping switches */
-       if (skb->protocol == __constant_htons(ETH_P_IP) &&
-           skb->nh.iph->protocol == IPPROTO_IGMP) {
-               struct slave *slave, *active_slave;
-               int i;
-
-               active_slave = bond->curr_active_slave;
-               bond_for_each_slave_from_to(bond, slave, i, active_slave->next,
-                                           active_slave->prev)
-                       if (IS_UP(slave->dev) &&
-                           (slave->link == BOND_LINK_UP))
-                               bond_activebackup_xmit_copy(skb, bond, slave);
-       }
-
        res = bond_dev_queue_xmit(bond, skb, bond->curr_active_slave->dev);
 
 out:
@@ -4201,6 +4324,22 @@ out:
 
 /*------------------------- Device initialization ---------------------------*/
 
+static void bond_set_xmit_hash_policy(struct bonding *bond)
+{
+       switch (bond->params.xmit_policy) {
+       case BOND_XMIT_POLICY_LAYER23:
+               bond->xmit_hash_policy = bond_xmit_hash_policy_l23;
+               break;
+       case BOND_XMIT_POLICY_LAYER34:
+               bond->xmit_hash_policy = bond_xmit_hash_policy_l34;
+               break;
+       case BOND_XMIT_POLICY_LAYER2:
+       default:
+               bond->xmit_hash_policy = bond_xmit_hash_policy_l2;
+               break;
+       }
+}
+
 /*
  * set bond mode specific net device operations
  */
@@ -4217,10 +4356,7 @@ void bond_set_mode_ops(struct bonding *bond, int mode)
                break;
        case BOND_MODE_XOR:
                bond_dev->hard_start_xmit = bond_xmit_xor;
-               if (bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER34)
-                       bond->xmit_hash_policy = bond_xmit_hash_policy_l34;
-               else
-                       bond->xmit_hash_policy = bond_xmit_hash_policy_l2;
+               bond_set_xmit_hash_policy(bond);
                break;
        case BOND_MODE_BROADCAST:
                bond_dev->hard_start_xmit = bond_xmit_broadcast;
@@ -4228,10 +4364,7 @@ void bond_set_mode_ops(struct bonding *bond, int mode)
        case BOND_MODE_8023AD:
                bond_set_master_3ad_flags(bond);
                bond_dev->hard_start_xmit = bond_3ad_xmit_xor;
-               if (bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER34)
-                       bond->xmit_hash_policy = bond_xmit_hash_policy_l34;
-               else
-                       bond->xmit_hash_policy = bond_xmit_hash_policy_l2;
+               bond_set_xmit_hash_policy(bond);
                break;
        case BOND_MODE_ALB:
                bond_set_master_alb_flags(bond);
@@ -4259,10 +4392,6 @@ static void bond_ethtool_get_drvinfo(struct net_device *bond_dev,
 }
 
 static const struct ethtool_ops bond_ethtool_ops = {
-       .get_tx_csum            = ethtool_op_get_tx_csum,
-       .get_tso                = ethtool_op_get_tso,
-       .get_ufo                = ethtool_op_get_ufo,
-       .get_sg                 = ethtool_op_get_sg,
        .get_drvinfo            = bond_ethtool_get_drvinfo,
 };
 
@@ -4282,12 +4411,18 @@ static int bond_init(struct net_device *bond_dev, struct bond_params *params)
 
        bond->params = *params; /* copy params struct */
 
+       bond->wq = create_singlethread_workqueue(bond_dev->name);
+       if (!bond->wq)
+               return -ENOMEM;
+
        /* Initialize pointers */
        bond->first_slave = NULL;
        bond->curr_active_slave = NULL;
        bond->current_arp_slave = NULL;
        bond->primary_slave = NULL;
        bond->dev = bond_dev;
+       bond->send_grat_arp = 0;
+       bond->setup_by_slave = 0;
        INIT_LIST_HEAD(&bond->vlan_list);
 
        /* Initialize the device entry points */
@@ -4299,6 +4434,7 @@ static int bond_init(struct net_device *bond_dev, struct bond_params *params)
        bond_dev->set_multicast_list = bond_set_multicast_list;
        bond_dev->change_mtu = bond_change_mtu;
        bond_dev->set_mac_address = bond_set_mac_address;
+       bond_dev->validate_addr = NULL;
 
        bond_set_mode_ops(bond, bond->params.mode);
 
@@ -4336,7 +4472,6 @@ static int bond_init(struct net_device *bond_dev, struct bond_params *params)
 #ifdef CONFIG_PROC_FS
        bond_create_proc_entry(bond);
 #endif
-
        list_add_tail(&bond->bond_list, &bond_dev_list);
 
        return 0;
@@ -4345,7 +4480,7 @@ static int bond_init(struct net_device *bond_dev, struct bond_params *params)
 /* De-initialize device specific data.
  * Caller must hold rtnl_lock.
  */
-void bond_deinit(struct net_device *bond_dev)
+static void bond_deinit(struct net_device *bond_dev)
 {
        struct bonding *bond = bond_dev->priv;
 
@@ -4356,6 +4491,27 @@ void bond_deinit(struct net_device *bond_dev)
 #endif
 }
 
+static void bond_work_cancel_all(struct bonding *bond)
+{
+       write_lock_bh(&bond->lock);
+       bond->kill_timers = 1;
+       write_unlock_bh(&bond->lock);
+
+       if (bond->params.miimon && delayed_work_pending(&bond->mii_work))
+               cancel_delayed_work(&bond->mii_work);
+
+       if (bond->params.arp_interval && delayed_work_pending(&bond->arp_work))
+               cancel_delayed_work(&bond->arp_work);
+
+       if (bond->params.mode == BOND_MODE_ALB &&
+           delayed_work_pending(&bond->alb_work))
+               cancel_delayed_work(&bond->alb_work);
+
+       if (bond->params.mode == BOND_MODE_8023AD &&
+           delayed_work_pending(&bond->ad_work))
+               cancel_delayed_work(&bond->ad_work);
+}
+
 /* Unregister and free all bond devices.
  * Caller must hold rtnl_lock.
  */
@@ -4366,11 +4522,13 @@ static void bond_free_all(void)
        list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list) {
                struct net_device *bond_dev = bond->dev;
 
+               bond_work_cancel_all(bond);
+               netif_tx_lock_bh(bond_dev);
                bond_mc_list_destroy(bond);
+               netif_tx_unlock_bh(bond_dev);
                /* Release the bonded slaves */
                bond_release_all(bond_dev);
-               unregister_netdevice(bond_dev);
-               bond_deinit(bond_dev);
+               bond_destroy(bond);
        }
 
 #ifdef CONFIG_PROC_FS
@@ -4382,19 +4540,32 @@ static void bond_free_all(void)
 
 /*
  * Convert string input module parms.  Accept either the
- * number of the mode or its string name.
+ * number of the mode or its string name.  A bit complicated because
+ * some mode names are substrings of other names, and calls from sysfs
+ * may have whitespace in the name (trailing newlines, for example).
  */
-int bond_parse_parm(char *mode_arg, struct bond_parm_tbl *tbl)
+int bond_parse_parm(const char *buf, struct bond_parm_tbl *tbl)
 {
-       int i;
+       int mode = -1, i, rv;
+       char *p, modestr[BOND_MAX_MODENAME_LEN + 1] = { 0, };
+
+       for (p = (char *)buf; *p; p++)
+               if (!(isdigit(*p) || isspace(*p)))
+                       break;
+
+       if (*p)
+               rv = sscanf(buf, "%20s", modestr);
+       else
+               rv = sscanf(buf, "%d", &mode);
+
+       if (!rv)
+               return -1;
 
        for (i = 0; tbl[i].modename; i++) {
-               if ((isdigit(*mode_arg) &&
-                    tbl[i].mode == simple_strtol(mode_arg, NULL, 0)) ||
-                   (strncmp(mode_arg, tbl[i].modename,
-                            strlen(tbl[i].modename)) == 0)) {
+               if (mode == tbl[i].mode)
+                       return tbl[i].mode;
+               if (strcmp(modestr, tbl[i].modename) == 0)
                        return tbl[i].mode;
-               }
        }
 
        return -1;
@@ -4592,7 +4763,7 @@ static int bond_check_params(struct bond_params *params)
                               arp_ip_target[arp_ip_count]);
                        arp_interval = 0;
                } else {
-                       u32 ip = in_aton(arp_ip_target[arp_ip_count]);
+                       __be32 ip = in_aton(arp_ip_target[arp_ip_count]);
                        arp_target[arp_ip_count] = ip;
                }
        }
@@ -4670,6 +4841,11 @@ static int bond_check_params(struct bond_params *params)
                primary = NULL;
        }
 
+       if (fail_over_mac && (bond_mode != BOND_MODE_ACTIVEBACKUP))
+               printk(KERN_WARNING DRV_NAME
+                      ": Warning: fail_over_mac only affects "
+                      "active-backup mode.\n");
+
        /* fill params struct with the proper values */
        params->mode = bond_mode;
        params->xmit_policy = xmit_hashtype;
@@ -4681,6 +4857,7 @@ static int bond_check_params(struct bond_params *params)
        params->use_carrier = use_carrier;
        params->lacp_fast = lacp_fast;
        params->primary[0] = 0;
+       params->fail_over_mac = fail_over_mac;
 
        if (primary) {
                strncpy(params->primary, primary, IFNAMSIZ);
@@ -4695,16 +4872,33 @@ static int bond_check_params(struct bond_params *params)
 static struct lock_class_key bonding_netdev_xmit_lock_key;
 
 /* Create a new bond based on the specified name and bonding parameters.
+ * If name is NULL, obtain a suitable "bond%d" name for us.
  * Caller must NOT hold rtnl_lock; we need to release it here before we
  * set up our sysfs entries.
  */
 int bond_create(char *name, struct bond_params *params, struct bonding **newbond)
 {
        struct net_device *bond_dev;
+       struct bonding *bond, *nxt;
        int res;
 
        rtnl_lock();
-       bond_dev = alloc_netdev(sizeof(struct bonding), name, ether_setup);
+       down_write(&bonding_rwsem);
+
+       /* Check to see if the bond already exists. */
+       if (name) {
+               list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list)
+                       if (strnicmp(bond->dev->name, name, IFNAMSIZ) == 0) {
+                               printk(KERN_ERR DRV_NAME
+                              ": cannot add bond %s; it already exists\n",
+                                      name);
+                               res = -EPERM;
+                               goto out_rtnl;
+                       }
+       }
+
+       bond_dev = alloc_netdev(sizeof(struct bonding), name ? name : "",
+                               ether_setup);
        if (!bond_dev) {
                printk(KERN_ERR DRV_NAME
                       ": %s: eek! can't alloc netdev!\n",
@@ -4713,6 +4907,12 @@ int bond_create(char *name, struct bond_params *params, struct bonding **newbond
                goto out_rtnl;
        }
 
+       if (!name) {
+               res = dev_alloc_name(bond_dev, "bond%d");
+               if (res < 0)
+                       goto out_netdev;
+       }
+
        /* bond_init() must be called after dev_alloc_name() (for the
         * /proc files), but before register_netdevice(), because we
         * need to set function pointers.
@@ -4723,8 +4923,6 @@ int bond_create(char *name, struct bond_params *params, struct bonding **newbond
                goto out_netdev;
        }
 
-       SET_MODULE_OWNER(bond_dev);
-
        res = register_netdevice(bond_dev);
        if (res < 0) {
                goto out_bond;
@@ -4737,16 +4935,24 @@ int bond_create(char *name, struct bond_params *params, struct bonding **newbond
 
        netif_carrier_off(bond_dev);
 
+       up_write(&bonding_rwsem);
        rtnl_unlock(); /* allows sysfs registration of net device */
        res = bond_create_sysfs_entry(bond_dev->priv);
-       goto done;
+       if (res < 0) {
+               rtnl_lock();
+               down_write(&bonding_rwsem);
+               goto out_bond;
+       }
+
+       return 0;
+
 out_bond:
        bond_deinit(bond_dev);
 out_netdev:
        free_netdev(bond_dev);
 out_rtnl:
+       up_write(&bonding_rwsem);
        rtnl_unlock();
-done:
        return res;
 }
 
@@ -4754,7 +4960,7 @@ static int __init bonding_init(void)
 {
        int i;
        int res;
-       char new_bond_name[8];  /* Enough room for 999 bonds at init. */
+       struct bonding *bond, *nxt;
 
        printk(KERN_INFO "%s", version);
 
@@ -4766,9 +4972,11 @@ static int __init bonding_init(void)
 #ifdef CONFIG_PROC_FS
        bond_create_proc_dir();
 #endif
+
+       init_rwsem(&bonding_rwsem);
+
        for (i = 0; i < max_bonds; i++) {
-               sprintf(new_bond_name, "bond%d",i);
-               res = bond_create(new_bond_name,&bonding_defaults, NULL);
+               res = bond_create(NULL, &bonding_defaults, NULL);
                if (res)
                        goto err;
        }
@@ -4782,6 +4990,11 @@ static int __init bonding_init(void)
 
        goto out;
 err:
+       list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list) {
+               bond_work_cancel_all(bond);
+               destroy_workqueue(bond->wq);
+       }
+
        rtnl_lock();
        bond_free_all();
        bond_destroy_sysfs();