linux-bk/drivers/net/bonding/bond_main.c
<<
>>
Prefs
   1/*
   2 * originally based on the dummy device.
   3 *
   4 * Copyright 1999, Thomas Davis, tadavis@lbl.gov.  
   5 * Licensed under the GPL. Based on dummy.c, and eql.c devices.
   6 *
   7 * bonding.c: an Ethernet Bonding driver
   8 *
   9 * This is useful to talk to a Cisco EtherChannel compatible equipment:
  10 *      Cisco 5500
  11 *      Sun Trunking (Solaris)
  12 *      Alteon AceDirector Trunks
  13 *      Linux Bonding
  14 *      and probably many L2 switches ...
  15 *
  16 * How it works:
  17 *    ifconfig bond0 ipaddress netmask up
  18 *      will setup a network device, with an ip address.  No mac address 
  19 *      will be assigned at this time.  The hw mac address will come from 
  20 *      the first slave bonded to the channel.  All slaves will then use 
  21 *      this hw mac address.
  22 *
  23 *    ifconfig bond0 down
  24 *         will release all slaves, marking them as down.
  25 *
  26 *    ifenslave bond0 eth0
  27 *      will attach eth0 to bond0 as a slave.  eth0 hw mac address will either
  28 *      a: be used as initial mac address
  29 *      b: if a hw mac address already is there, eth0's hw mac address 
  30 *         will then be set from bond0.
  31 *
  32 * v0.1 - first working version.
  33 * v0.2 - changed stats to be calculated by summing slaves stats.
  34 *
  35 * Changes:
  36 * Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  37 * - fix leaks on failure at bond_init
  38 *
  39 * 2000/09/30 - Willy Tarreau <willy at meta-x.org>
  40 *     - added trivial code to release a slave device.
  41 *     - fixed security bug (CAP_NET_ADMIN not checked)
  42 *     - implemented MII link monitoring to disable dead links :
  43 *       All MII capable slaves are checked every <miimon> milliseconds
  44 *       (100 ms seems good). This value can be changed by passing it to
  45 *       insmod. A value of zero disables the monitoring (default).
  46 *     - fixed an infinite loop in bond_xmit_roundrobin() when there's no
  47 *       good slave.
  48 *     - made the code hopefully SMP safe
  49 *
  50 * 2000/10/03 - Willy Tarreau <willy at meta-x.org>
  51 *     - optimized slave lists based on relevant suggestions from Thomas Davis
  52 *     - implemented active-backup method to obtain HA with two switches:
  53 *       stay as long as possible on the same active interface, while we
  54 *       also monitor the backup one (MII link status) because we want to know
  55 *       if we are able to switch at any time. ( pass "mode=1" to insmod )
  56 *     - lots of stress testings because we need it to be more robust than the
  57 *       wires ! :->
  58 *
  59 * 2000/10/09 - Willy Tarreau <willy at meta-x.org>
  60 *     - added up and down delays after link state change.
  61 *     - optimized the slaves chaining so that when we run forward, we never
  62 *       repass through the bond itself, but we can find it by searching
  63 *       backwards. Renders the deletion more difficult, but accelerates the
  64 *       scan.
  65 *     - smarter enslaving and releasing.
  66 *     - finer and more robust SMP locking
  67 *
  68 * 2000/10/17 - Willy Tarreau <willy at meta-x.org>
  69 *     - fixed two potential SMP race conditions
  70 *
  71 * 2000/10/18 - Willy Tarreau <willy at meta-x.org>
  72 *     - small fixes to the monitoring FSM in case of zero delays
  73 * 2000/11/01 - Willy Tarreau <willy at meta-x.org>
  74 *     - fixed first slave not automatically used in trunk mode.
  75 * 2000/11/10 : spelling of "EtherChannel" corrected.
  76 * 2000/11/13 : fixed a race condition in case of concurrent accesses to ioctl().
  77 * 2000/12/16 : fixed improper usage of rtnl_exlock_nowait().
  78 *
  79 * 2001/1/3 - Chad N. Tindel <ctindel at ieee dot org>
  80 *     - The bonding driver now simulates MII status monitoring, just like
  81 *       a normal network device.  It will show that the link is down iff
  82 *       every slave in the bond shows that their links are down.  If at least
  83 *       one slave is up, the bond's MII status will appear as up.
  84 *
  85 * 2001/2/7 - Chad N. Tindel <ctindel at ieee dot org>
  86 *     - Applications can now query the bond from user space to get
  87 *       information which may be useful.  They do this by calling
  88 *       the BOND_INFO_QUERY ioctl.  Once the app knows how many slaves
  89 *       are in the bond, it can call the BOND_SLAVE_INFO_QUERY ioctl to
  90 *       get slave specific information (# link failures, etc).  See
  91 *       <linux/if_bonding.h> for more details.  The structs of interest
  92 *       are ifbond and ifslave.
  93 *
  94 * 2001/4/5 - Chad N. Tindel <ctindel at ieee dot org>
  95 *     - Ported to 2.4 Kernel
  96 * 
  97 * 2001/5/2 - Jeffrey E. Mast <jeff at mastfamily dot com>
  98 *     - When a device is detached from a bond, the slave device is no longer
  99 *       left thinking that is has a master.
 100 *
 101 * 2001/5/16 - Jeffrey E. Mast <jeff at mastfamily dot com>
 102 *     - memset did not appropriately initialized the bond rw_locks. Used 
 103 *       rwlock_init to initialize to unlocked state to prevent deadlock when 
 104 *       first attempting a lock
 105 *     - Called SET_MODULE_OWNER for bond device
 106 *
 107 * 2001/5/17 - Tim Anderson <tsa at mvista.com>
 108 *     - 2 paths for releasing for slave release; 1 through ioctl
 109 *       and 2) through close. Both paths need to release the same way.
 110 *     - the free slave in bond release is changing slave status before
 111 *       the free. The netdev_set_master() is intended to change slave state
 112 *       so it should not be done as part of the release process.
 113 *     - Simple rule for slave state at release: only the active in A/B and
 114 *       only one in the trunked case.
 115 *
 116 * 2001/6/01 - Tim Anderson <tsa at mvista.com>
 117 *     - Now call dev_close when releasing a slave so it doesn't screw up
 118 *       out routing table.
 119 *
 120 * 2001/6/01 - Chad N. Tindel <ctindel at ieee dot org>
 121 *     - Added /proc support for getting bond and slave information.
 122 *       Information is in /proc/net/<bond device>/info. 
 123 *     - Changed the locking when calling bond_close to prevent deadlock.
 124 *
 125 * 2001/8/05 - Janice Girouard <girouard at us.ibm.com>
 126 *     - correct problem where refcnt of slave is not incremented in bond_ioctl
 127 *       so the system hangs when halting.
 128 *     - correct locking problem when unable to malloc in bond_enslave.
 129 *     - adding bond_xmit_xor logic.
 130 *     - adding multiple bond device support.
 131 *
 132 * 2001/8/13 - Erik Habbinga <erik_habbinga at hp dot com>
 133 *     - correct locking problem with rtnl_exlock_nowait
 134 *
 135 * 2001/8/23 - Janice Girouard <girouard at us.ibm.com>
 136 *     - bzero initial dev_bonds, to correct oops
 137 *     - convert SIOCDEVPRIVATE to new MII ioctl calls
 138 *
 139 * 2001/9/13 - Takao Indoh <indou dot takao at jp dot fujitsu dot com>
 140 *     - Add the BOND_CHANGE_ACTIVE ioctl implementation
 141 *
 142 * 2001/9/14 - Mark Huth <mhuth at mvista dot com>
 143 *     - Change MII_LINK_READY to not check for end of auto-negotiation,
 144 *       but only for an up link.
 145 *
 146 * 2001/9/20 - Chad N. Tindel <ctindel at ieee dot org>
 147 *     - Add the device field to bonding_t.  Previously the net_device 
 148 *       corresponding to a bond wasn't available from the bonding_t 
 149 *       structure.
 150 *
 151 * 2001/9/25 - Janice Girouard <girouard at us.ibm.com>
 152 *     - add arp_monitor for active backup mode
 153 *
 154 * 2001/10/23 - Takao Indoh <indou dot takao at jp dot fujitsu dot com>
 155 *     - Various memory leak fixes
 156 *
 157 * 2001/11/5 - Mark Huth <mark dot huth at mvista dot com>
 158 *     - Don't take rtnl lock in bond_mii_monitor as it deadlocks under 
 159 *       certain hotswap conditions.  
 160 *       Note:  this same change may be required in bond_arp_monitor ???
 161 *     - Remove possibility of calling bond_sethwaddr with NULL slave_dev ptr 
 162 *     - Handle hot swap ethernet interface deregistration events to remove
 163 *       kernel oops following hot swap of enslaved interface
 164 *
 165 * 2002/1/2 - Chad N. Tindel <ctindel at ieee dot org>
 166 *     - Restore original slave flags at release time.
 167 *
 168 * 2002/02/18 - Erik Habbinga <erik_habbinga at hp dot com>
 169 *     - bond_release(): calling kfree on our_slave after call to
 170 *       bond_restore_slave_flags, not before
 171 *     - bond_enslave(): saving slave flags into original_flags before
 172 *       call to netdev_set_master, so the IFF_SLAVE flag doesn't end
 173 *       up in original_flags
 174 *
 175 * 2002/04/05 - Mark Smith <mark.smith at comdev dot cc> and
 176 *              Steve Mead <steve.mead at comdev dot cc>
 177 *     - Port Gleb Natapov's multicast support patchs from 2.4.12
 178 *       to 2.4.18 adding support for multicast.
 179 *
 180 * 2002/06/10 - Tony Cureington <tony.cureington * hp_com>
 181 *     - corrected uninitialized pointer (ifr.ifr_data) in bond_check_dev_link;
 182 *       actually changed function to use MIIPHY, then MIIREG, and finally
 183 *       ETHTOOL to determine the link status
 184 *     - fixed bad ifr_data pointer assignments in bond_ioctl
 185 *     - corrected mode 1 being reported as active-backup in bond_get_info;
 186 *       also added text to distinguish type of load balancing (rr or xor)
 187 *     - change arp_ip_target module param from "1-12s" (array of 12 ptrs)
 188 *       to "s" (a single ptr)
 189 *
 190 * 2002/08/30 - Jay Vosburgh <fubar at us dot ibm dot com>
 191 *     - Removed acquisition of xmit_lock in set_multicast_list; caused
 192 *       deadlock on SMP (lock is held by caller).
 193 *     - Revamped SIOCGMIIPHY, SIOCGMIIREG portion of bond_check_dev_link().
 194 *
 195 * 2002/09/18 - Jay Vosburgh <fubar at us dot ibm dot com>
 196 *     - Fixed up bond_check_dev_link() (and callers): removed some magic
 197 *       numbers, banished local MII_ defines, wrapped ioctl calls to
 198 *       prevent EFAULT errors
 199 *
 200 * 2002/9/30 - Jay Vosburgh <fubar at us dot ibm dot com>
 201 *     - make sure the ip target matches the arp_target before saving the
 202 *       hw address.
 203 *
 204 * 2002/9/30 - Dan Eisner <eisner at 2robots dot com>
 205 *     - make sure my_ip is set before taking down the link, since
 206 *       not all switches respond if the source ip is not set.
 207 *
 208 * 2002/10/8 - Janice Girouard <girouard at us dot ibm dot com>
 209 *     - read in the local ip address when enslaving a device
 210 *     - add primary support
 211 *     - make sure 2*arp_interval has passed when a new device
 212 *       is brought on-line before taking it down.
 213 *
 214 * 2002/09/11 - Philippe De Muyter <phdm at macqel dot be>
 215 *     - Added bond_xmit_broadcast logic.
 216 *     - Added bond_mode() support function.
 217 *
 218 * 2002/10/26 - Laurent Deniel <laurent.deniel at free.fr>
 219 *     - allow to register multicast addresses only on active slave
 220 *       (useful in active-backup mode)
 221 *     - add multicast module parameter
 222 *     - fix deletion of multicast groups after unloading module
 223 *
 224 * 2002/11/06 - Kameshwara Rayaprolu <kameshwara.rao * wipro_com>
 225 *     - Changes to prevent panic from closing the device twice; if we close 
 226 *       the device in bond_release, we must set the original_flags to down 
 227 *       so it won't be closed again by the network layer.
 228 *
 229 * 2002/11/07 - Tony Cureington <tony.cureington * hp_com>
 230 *     - Fix arp_target_hw_addr memory leak
 231 *     - Created activebackup_arp_monitor function to handle arp monitoring 
 232 *       in active backup mode - the bond_arp_monitor had several problems... 
 233 *       such as allowing slaves to tx arps sequentially without any delay 
 234 *       for a response
 235 *     - Renamed bond_arp_monitor to loadbalance_arp_monitor and re-wrote
 236 *       this function to just handle arp monitoring in load-balancing mode;
 237 *       it is a lot more compact now
 238 *     - Changes to ensure one and only one slave transmits in active-backup 
 239 *       mode
 240 *     - Robustesize parameters; warn users about bad combinations of 
 241 *       parameters; also if miimon is specified and a network driver does 
 242 *       not support MII or ETHTOOL, inform the user of this
 243 *     - Changes to support link_failure_count when in arp monitoring mode
 244 *     - Fix up/down delay reported in /proc
 245 *     - Added version; log version; make version available from "modinfo -d"
 246 *     - Fixed problem in bond_check_dev_link - if the first IOCTL (SIOCGMIIPH)
 247 *       failed, the ETHTOOL ioctl never got a chance
 248 *
 249 * 2002/11/16 - Laurent Deniel <laurent.deniel at free.fr>
 250 *     - fix multicast handling in activebackup_arp_monitor
 251 *     - remove one unnecessary and confusing current_slave == slave test 
 252 *       in activebackup_arp_monitor
 253 *
 254 *  2002/11/17 - Laurent Deniel <laurent.deniel at free.fr>
 255 *     - fix bond_slave_info_query when slave_id = num_slaves
 256 *
 257 *  2002/11/19 - Janice Girouard <girouard at us dot ibm dot com>
 258 *     - correct ifr_data reference.  Update ifr_data reference
 259 *       to mii_ioctl_data struct values to avoid confusion.
 260 *
 261 *  2002/11/22 - Bert Barbe <bert.barbe at oracle dot com>
 262 *      - Add support for multiple arp_ip_target
 263 *
 264 *  2002/12/13 - Jay Vosburgh <fubar at us dot ibm dot com>
 265 *      - Changed to allow text strings for mode and multicast, e.g.,
 266 *        insmod bonding mode=active-backup.  The numbers still work.
 267 *        One change: an invalid choice will cause module load failure,
 268 *        rather than the previous behavior of just picking one.
 269 *      - Minor cleanups; got rid of dup ctype stuff, atoi function
 270 * 
 271 * 2003/02/07 - Jay Vosburgh <fubar at us dot ibm dot com>
 272 *      - Added use_carrier module parameter that causes miimon to
 273 *        use netif_carrier_ok() test instead of MII/ETHTOOL ioctls.
 274 *      - Minor cleanups; consolidated ioctl calls to one function.
 275 *
 276 * 2003/02/07 - Tony Cureington <tony.cureington * hp_com>
 277 *      - Fix bond_mii_monitor() logic error that could result in
 278 *        bonding round-robin mode ignoring links after failover/recovery
 279 *
 280 * 2003/03/17 - Jay Vosburgh <fubar at us dot ibm dot com>
 281 *      - kmalloc fix (GFP_KERNEL to GFP_ATOMIC) reported by
 282 *        Shmulik dot Hen at intel.com.
 283 *      - Based on discussion on mailing list, changed use of
 284 *        update_slave_cnt(), created wrapper functions for adding/removing
 285 *        slaves, changed bond_xmit_xor() to check slave_cnt instead of
 286 *        checking slave and slave->dev (which only worked by accident).
 287 *      - Misc code cleanup: get arp_send() prototype from header file,
 288 *        add max_bonds to bonding.txt.
 289 *
 290 * 2003/03/18 - Tsippy Mendelson <tsippy.mendelson at intel dot com> and
 291 *              Shmulik Hen <shmulik.hen at intel dot com>
 292 *      - Make sure only bond_attach_slave() and bond_detach_slave() can
 293 *        manipulate the slave list, including slave_cnt, even when in
 294 *        bond_release_all().
 295 *      - Fixed hang in bond_release() with traffic running:
 296 *        netdev_set_master() must not be called from within the bond lock.
 297 *
 298 * 2003/03/18 - Tsippy Mendelson <tsippy.mendelson at intel dot com> and
 299 *              Shmulik Hen <shmulik.hen at intel dot com>
 300 *      - Fixed hang in bond_enslave() with traffic running:
 301 *        netdev_set_master() must not be called from within the bond lock.
 302 *
 303 * 2003/03/18 - Amir Noam <amir.noam at intel dot com>
 304 *      - Added support for getting slave's speed and duplex via ethtool.
 305 *        Needed for 802.3ad and other future modes.
 306 *
 307 * 2003/03/18 - Tsippy Mendelson <tsippy.mendelson at intel dot com> and
 308 *              Shmulik Hen <shmulik.hen at intel dot com>
 309 *      - Enable support of modes that need to use the unique mac address of
 310 *        each slave.
 311 *        * bond_enslave(): Moved setting the slave's mac address, and
 312 *          openning it, from the application to the driver. This breaks
 313 *          backward comaptibility with old versions of ifenslave that open
 314 *           the slave before enalsving it !!!.
 315 *        * bond_release(): The driver also takes care of closing the slave
 316 *          and restoring its original mac address.
 317 *      - Removed the code that restores all base driver's flags.
 318 *        Flags are automatically restored once all undo stages are done
 319 *        properly.
 320 *      - Block possibility of enslaving before the master is up. This
 321 *        prevents putting the system in an unstable state.
 322 *
 323 * 2003/03/18 - Amir Noam <amir.noam at intel dot com>,
 324 *              Tsippy Mendelson <tsippy.mendelson at intel dot com> and
 325 *              Shmulik Hen <shmulik.hen at intel dot com>
 326 *      - Added support for IEEE 802.3ad Dynamic link aggregation mode.
 327 *
 328 * 2003/05/01 - Amir Noam <amir.noam at intel dot com>
 329 *      - Added ABI version control to restore compatibility between
 330 *        new/old ifenslave and new/old bonding.
 331 *
 332 * 2003/05/01 - Shmulik Hen <shmulik.hen at intel dot com>
 333 *      - Fixed bug in bond_release_all(): save old value of current_slave
 334 *        before setting it to NULL.
 335 *      - Changed driver versioning scheme to include version number instead
 336 *        of release date (that is already in another field). There are 3
 337 *        fields X.Y.Z where:
 338 *              X - Major version - big behavior changes
 339 *              Y - Minor version - addition of features
 340 *              Z - Extra version - minor changes and bug fixes
 341 *        The current version is 1.0.0 as a base line.
 342 *
 343 * 2003/05/01 - Tsippy Mendelson <tsippy.mendelson at intel dot com> and
 344 *              Amir Noam <amir.noam at intel dot com>
 345 *      - Added support for lacp_rate module param.
 346 *      - Code beautification and style changes (mainly in comments).
 347 *        new version - 1.0.1
 348 *
 349 * 2003/05/01 - Shmulik Hen <shmulik.hen at intel dot com>
 350 *      - Based on discussion on mailing list, changed locking scheme
 351 *        to use lock/unlock or lock_bh/unlock_bh appropriately instead
 352 *        of lock_irqsave/unlock_irqrestore. The new scheme helps exposing
 353 *        hidden bugs and solves system hangs that occurred due to the fact
 354 *        that holding lock_irqsave doesn't prevent softirqs from running.
 355 *        This also increases total throughput since interrupts are not
 356 *        blocked on each transmitted packets or monitor timeout.
 357 *        new version - 2.0.0
 358 *
 359 * 2003/05/01 - Shmulik Hen <shmulik.hen at intel dot com>
 360 *      - Added support for Transmit load balancing mode.
 361 *      - Concentrate all assignments of current_slave to a single point
 362 *        so specific modes can take actions when the primary adapter is
 363 *        changed.
 364 *      - Take the updelay parameter into consideration during bond_enslave
 365 *        since some adapters loose their link during setting the device.
 366 *      - Renamed bond_3ad_link_status_changed() to
 367 *        bond_3ad_handle_link_change() for compatibility with TLB.
 368 *        new version - 2.1.0
 369 *
 370 * 2003/05/01 - Tsippy Mendelson <tsippy.mendelson at intel dot com>
 371 *      - Added support for Adaptive load balancing mode which is
 372 *        equivalent to Transmit load balancing + Receive load balancing.
 373 *        new version - 2.2.0
 374 *
 375 * 2003/05/15 - Jay Vosburgh <fubar at us dot ibm dot com>
 376 *      - Applied fix to activebackup_arp_monitor posted to bonding-devel
 377 *        by Tony Cureington <tony.cureington * hp_com>.  Fixes ARP
 378 *        monitor endless failover bug.  Version to 2.2.10
 379 *
 380 * 2003/05/20 - Amir Noam <amir.noam at intel dot com>
 381 *      - Fixed bug in ABI version control - Don't commit to a specific
 382 *        ABI version if receiving unsupported ioctl commands.
 383 *
 384 * 2003/05/22 - Jay Vosburgh <fubar at us dot ibm dot com>
 385 *      - Fix ifenslave -c causing bond to loose existing routes;
 386 *        added bond_set_mac_address() that doesn't require the
 387 *        bond to be down.
 388 *      - In conjunction with fix for ifenslave -c, in
 389 *        bond_change_active(), changing to the already active slave
 390 *        is no longer an error (it successfully does nothing).
 391 *
 392 * 2003/06/30 - Amir Noam <amir.noam at intel dot com>
 393 *      - Fixed bond_change_active() for ALB/TLB modes.
 394 *        Version to 2.2.14.
 395 *
 396 * 2003/07/29 - Amir Noam <amir.noam at intel dot com>
 397 *      - Fixed ARP monitoring bug.
 398 *        Version to 2.2.15.
 399 *
 400 * 2003/07/31 - Willy Tarreau <willy at ods dot org>
 401 *      - Fixed kernel panic when using ARP monitoring without
 402 *        setting bond's IP address.
 403 *        Version to 2.2.16.
 404 *
 405 * 2003/08/06 - Amir Noam <amir.noam at intel dot com>
 406 *      - Back port from 2.6: use alloc_netdev(); fix /proc handling;
 407 *        made stats a part of bond struct so no need to allocate
 408 *        and free it separately; use standard list operations instead
 409 *        of pre-allocated array of bonds.
 410 *        Version to 2.3.0.
 411 *
 412 * 2003/08/07 - Jay Vosburgh <fubar at us dot ibm dot com>,
 413 *             Amir Noam <amir.noam at intel dot com> and
 414 *             Shmulik Hen <shmulik.hen at intel dot com>
 415 *      - Propagating master's settings: Distinguish between modes that
 416 *        use a primary slave from those that don't, and propagate settings
 417 *        accordingly; Consolidate change_active opeartions and add
 418 *        reselect_active and find_best opeartions; Decouple promiscuous
 419 *        handling from the multicast mode setting; Add support for changing
 420 *        HW address and MTU with proper unwind; Consolidate procfs code,
 421 *        add CHANGENAME handler; Enhance netdev notification handling.
 422 *        Version to 2.4.0.
 423 *
 424 * 2003/09/15 - Stephen Hemminger <shemminger at osdl dot org>,
 425 *             Amir Noam <amir.noam at intel dot com>
 426 *      - Convert /proc to seq_file interface.
 427 *        Change /proc/net/bondX/info to /proc/net/bonding/bondX.
 428 *        Set version to 2.4.1.
 429 */
 430
 431#include <linux/config.h>
 432#include <linux/kernel.h>
 433#include <linux/module.h>
 434#include <linux/sched.h>
 435#include <linux/types.h>
 436#include <linux/fcntl.h>
 437#include <linux/interrupt.h>
 438#include <linux/ptrace.h>
 439#include <linux/ioport.h>
 440#include <linux/in.h>
 441#include <linux/ip.h>
 442#include <linux/slab.h>
 443#include <linux/string.h>
 444#include <linux/init.h>
 445#include <linux/timer.h>
 446#include <linux/socket.h>
 447#include <linux/ctype.h>
 448#include <linux/inet.h>
 449#include <asm/system.h>
 450#include <asm/bitops.h>
 451#include <asm/io.h>
 452#include <asm/dma.h>
 453#include <asm/uaccess.h>
 454#include <linux/errno.h>
 455
 456#include <linux/netdevice.h>
 457#include <linux/inetdevice.h>
 458#include <linux/etherdevice.h>
 459#include <linux/skbuff.h>
 460#include <net/sock.h>
 461#include <linux/rtnetlink.h>
 462#include <linux/proc_fs.h>
 463#include <linux/seq_file.h>
 464
 465#include <linux/if_bonding.h>
 466#include <linux/smp.h>
 467#include <linux/if_ether.h>
 468#include <net/arp.h>
 469#include <linux/mii.h>
 470#include <linux/ethtool.h>
 471#include "bonding.h"
 472#include "bond_3ad.h"
 473#include "bond_alb.h"
 474
 475#define DRV_VERSION     "2.4.1"
 476#define DRV_RELDATE     "September 15, 2003"
 477#define DRV_NAME        "bonding"
 478#define DRV_DESCRIPTION "Ethernet Channel Bonding Driver"
 479
 480static const char *version =
 481DRV_NAME ".c:v" DRV_VERSION " (" DRV_RELDATE ")\n";
 482
 483/* monitor all links that often (in milliseconds). <=0 disables monitoring */
 484#ifndef BOND_LINK_MON_INTERV
 485#define BOND_LINK_MON_INTERV    0
 486#endif
 487
 488#ifndef BOND_LINK_ARP_INTERV
 489#define BOND_LINK_ARP_INTERV    0
 490#endif
 491
 492#ifndef MAX_ARP_IP_TARGETS
 493#define MAX_ARP_IP_TARGETS 16
 494#endif
 495
 496#define USES_PRIMARY(mode) \
 497                (((mode) == BOND_MODE_ACTIVEBACKUP) || \
 498                 ((mode) == BOND_MODE_TLB) || \
 499                 ((mode) == BOND_MODE_ALB))
 500
 501struct bond_parm_tbl {
 502        char *modename;
 503        int mode;
 504};
 505
 506static int arp_interval = BOND_LINK_ARP_INTERV;
 507static char *arp_ip_target[MAX_ARP_IP_TARGETS] = { NULL, };
 508static u32 arp_target[MAX_ARP_IP_TARGETS] = { 0, } ;
 509static int arp_ip_count = 0;
 510static u32 my_ip = 0;
 511char *arp_target_hw_addr = NULL;
 512
 513static char *primary= NULL;
 514
 515static int app_abi_ver = 0;
 516static int orig_app_abi_ver = -1; /* This is used to save the first ABI version
 517                                   * we receive from the application. Once set,
 518                                   * it won't be changed, and the module will
 519                                   * refuse to enslave/release interfaces if the
 520                                   * command comes from an application using
 521                                   * another ABI version.
 522                                   */
 523
 524static int max_bonds    = BOND_DEFAULT_MAX_BONDS;
 525static int miimon       = BOND_LINK_MON_INTERV;
 526static int use_carrier  = 1;
 527static int bond_mode    = BOND_MODE_ROUNDROBIN;
 528static int updelay      = 0;
 529static int downdelay    = 0;
 530
 531static char *mode       = NULL;
 532
 533static struct bond_parm_tbl bond_mode_tbl[] = {
 534{       "balance-rr",           BOND_MODE_ROUNDROBIN},
 535{       "active-backup",        BOND_MODE_ACTIVEBACKUP},
 536{       "balance-xor",          BOND_MODE_XOR},
 537{       "broadcast",            BOND_MODE_BROADCAST},
 538{       "802.3ad",              BOND_MODE_8023AD},
 539{       "balance-tlb",          BOND_MODE_TLB},
 540{       "balance-alb",          BOND_MODE_ALB},
 541{       NULL,                   -1},
 542};
 543
 544static int multicast_mode       = BOND_MULTICAST_ALL;
 545static char *multicast          = NULL;
 546
 547static struct bond_parm_tbl bond_mc_tbl[] = {
 548{       "disabled",             BOND_MULTICAST_DISABLED},
 549{       "active",               BOND_MULTICAST_ACTIVE},
 550{       "all",                  BOND_MULTICAST_ALL},
 551{       NULL,                   -1},
 552};
 553
 554static int lacp_fast            = 0;
 555static char *lacp_rate          = NULL;
 556
 557static struct bond_parm_tbl bond_lacp_tbl[] = {
 558{       "slow",         AD_LACP_SLOW},
 559{       "fast",         AD_LACP_FAST},
 560{       NULL,           -1},
 561};
 562
 563static LIST_HEAD(bond_dev_list);
 564#ifdef CONFIG_PROC_FS
 565static struct proc_dir_entry *bond_proc_dir = NULL;
 566#endif
 567
 568MODULE_PARM(max_bonds, "i");
 569MODULE_PARM_DESC(max_bonds, "Max number of bonded devices");
 570MODULE_PARM(miimon, "i");
 571MODULE_PARM_DESC(miimon, "Link check interval in milliseconds");
 572MODULE_PARM(use_carrier, "i");
 573MODULE_PARM_DESC(use_carrier, "Use netif_carrier_ok (vs MII ioctls) in miimon; 0 for off, 1 for on (default)");
 574MODULE_PARM(mode, "s");
 575MODULE_PARM_DESC(mode, "Mode of operation : 0 for round robin, 1 for active-backup, 2 for xor");
 576MODULE_PARM(arp_interval, "i");
 577MODULE_PARM_DESC(arp_interval, "arp interval in milliseconds");
 578MODULE_PARM(arp_ip_target, "1-" __MODULE_STRING(MAX_ARP_IP_TARGETS) "s");
 579MODULE_PARM_DESC(arp_ip_target, "arp targets in n.n.n.n form");
 580MODULE_PARM(updelay, "i");
 581MODULE_PARM_DESC(updelay, "Delay before considering link up, in milliseconds");
 582MODULE_PARM(downdelay, "i");
 583MODULE_PARM_DESC(downdelay, "Delay before considering link down, in milliseconds");
 584MODULE_PARM(primary, "s");
 585MODULE_PARM_DESC(primary, "Primary network device to use");
 586MODULE_PARM(multicast, "s");
 587MODULE_PARM_DESC(multicast, "Mode for multicast support : 0 for none, 1 for active slave, 2 for all slaves (default)");
 588MODULE_PARM(lacp_rate, "s");
 589MODULE_PARM_DESC(lacp_rate, "LACPDU tx rate to request from 802.3ad partner (slow/fast)");
 590
 591static int bond_xmit_roundrobin(struct sk_buff *skb, struct net_device *dev);
 592static int bond_xmit_xor(struct sk_buff *skb, struct net_device *dev);
 593static int bond_xmit_activebackup(struct sk_buff *skb, struct net_device *dev);
 594static struct net_device_stats *bond_get_stats(struct net_device *dev);
 595static void bond_mii_monitor(struct net_device *dev);
 596static void loadbalance_arp_monitor(struct net_device *dev);
 597static void activebackup_arp_monitor(struct net_device *dev);
 598static void bond_mc_list_destroy(struct bonding *bond);
 599static void bond_mc_add(bonding_t *bond, void *addr, int alen);
 600static void bond_mc_delete(bonding_t *bond, void *addr, int alen);
 601static int bond_mc_list_copy (struct dev_mc_list *src, struct bonding *dst, int gpf_flag);
 602static inline int dmi_same(struct dev_mc_list *dmi1, struct dev_mc_list *dmi2);
 603static void bond_set_promiscuity(bonding_t *bond, int inc);
 604static void bond_set_allmulti(bonding_t *bond, int inc);
 605static struct dev_mc_list* bond_mc_list_find_dmi(struct dev_mc_list *dmi, struct dev_mc_list *mc_list);
 606static void bond_mc_update(bonding_t *bond, slave_t *new, slave_t *old);
 607static int bond_enslave(struct net_device *master, struct net_device *slave);
 608static int bond_release(struct net_device *master, struct net_device *slave);
 609static int bond_release_all(struct net_device *master);
 610static int bond_sethwaddr(struct net_device *master, struct net_device *slave);
 611static void change_active_interface(struct bonding *bond, struct slave *new);
 612static void reselect_active_interface(struct bonding *bond);
 613static struct slave *find_best_interface(struct bonding *bond);
 614
 615
 616/* #define BONDING_DEBUG 1 */
 617#ifdef BONDING_DEBUG
 618#define dprintk(x...) printk(x...)
 619#else /* BONDING_DEBUG */
 620#define dprintk(x...) do {} while (0)
 621#endif /* BONDING_DEBUG */
 622
 623/* several macros */
 624
 625static void arp_send_all(slave_t *slave)
 626{       
 627        int i; 
 628
 629        for (i = 0; (i<MAX_ARP_IP_TARGETS) && arp_target[i]; i++) { 
 630                arp_send(ARPOP_REQUEST, ETH_P_ARP, arp_target[i], slave->dev, 
 631                         my_ip, arp_target_hw_addr, slave->dev->dev_addr,
 632                         arp_target_hw_addr); 
 633        } 
 634}
 635 
 636
 637static const char *
 638bond_mode_name(void)
 639{
 640        switch (bond_mode) {
 641        case BOND_MODE_ROUNDROBIN :
 642                return "load balancing (round-robin)";
 643        case BOND_MODE_ACTIVEBACKUP :
 644                return "fault-tolerance (active-backup)";
 645        case BOND_MODE_XOR :
 646                return "load balancing (xor)";
 647        case BOND_MODE_BROADCAST :
 648                return "fault-tolerance (broadcast)";
 649        case BOND_MODE_8023AD:
 650                return "IEEE 802.3ad Dynamic link aggregation";
 651        case BOND_MODE_TLB:
 652                return "transmit load balancing";
 653        case BOND_MODE_ALB:
 654                return "adaptive load balancing";
 655        default:
 656                return "unknown";
 657        }
 658}
 659
 660static const char *
 661multicast_mode_name(void)
 662{
 663        switch(multicast_mode) {
 664        case BOND_MULTICAST_DISABLED :
 665                return "disabled";
 666        case BOND_MULTICAST_ACTIVE :
 667                return "active slave only";
 668        case BOND_MULTICAST_ALL :
 669                return "all slaves";
 670        default :
 671                return "unknown";
 672        }
 673}
 674
 675void bond_set_slave_inactive_flags(slave_t *slave)
 676{
 677        slave->state = BOND_STATE_BACKUP;
 678        slave->dev->flags |= IFF_NOARP;
 679}
 680
 681void bond_set_slave_active_flags(slave_t *slave)
 682{
 683        slave->state = BOND_STATE_ACTIVE;
 684        slave->dev->flags &= ~IFF_NOARP;
 685}
 686
 687/*
 688 * This function counts and verifies the the number of attached
 689 * slaves, checking the count against the expected value (given that incr
 690 * is either 1 or -1, for add or removal of a slave).  Only
 691 * bond_xmit_xor() uses the slave_cnt value, but this is still a good
 692 * consistency check.
 693 */
 694static inline void
 695update_slave_cnt(bonding_t *bond, int incr)
 696{
 697        slave_t *slave = NULL;
 698        int expect = bond->slave_cnt + incr;
 699
 700        bond->slave_cnt = 0;
 701        for (slave = bond->prev; slave != (slave_t*)bond;
 702             slave = slave->prev) {
 703                bond->slave_cnt++;
 704        }
 705
 706        if (expect != bond->slave_cnt)
 707                BUG();
 708}
 709
 710/* 
 711 * This function detaches the slave <slave> from the list <bond>.
 712 * WARNING: no check is made to verify if the slave effectively
 713 * belongs to <bond>. It returns <slave> in case it's needed.
 714 * Nothing is freed on return, structures are just unchained.
 715 * If the bond->current_slave pointer was pointing to <slave>,
 716 * it should be changed by the calling function.
 717 *
 718 * bond->lock held for writing by caller.
 719 */
 720static slave_t *
 721bond_detach_slave(bonding_t *bond, slave_t *slave)
 722{
 723        if ((bond == NULL) || (slave == NULL) ||
 724           ((void *)bond == (void *)slave)) {
 725                printk(KERN_ERR
 726                        "bond_detach_slave(): trying to detach "
 727                        "slave %p from bond %p\n", bond, slave);
 728                return slave;
 729        }
 730
 731        if (bond->next == slave) {  /* is the slave at the head ? */
 732                if (bond->prev == slave) {  /* is the slave alone ? */
 733                        bond->prev = bond->next = (slave_t *)bond;
 734                } else { /* not alone */
 735                        bond->next        = slave->next;
 736                        slave->next->prev = (slave_t *)bond;
 737                        bond->prev->next  = slave->next;
 738                }
 739        } else {
 740                slave->prev->next = slave->next;
 741                if (bond->prev == slave) {  /* is this slave the last one ? */
 742                        bond->prev = slave->prev;
 743                } else {
 744                        slave->next->prev = slave->prev;
 745                }
 746        }
 747
 748        update_slave_cnt(bond, -1);
 749
 750        return slave;
 751}
 752
 753/*
 754 * This function attaches the slave <slave> to the list <bond>.
 755 *
 756 * bond->lock held for writing by caller.
 757 */
 758static void
 759bond_attach_slave(struct bonding *bond, struct slave *new_slave)
 760{
 761        /* 
 762         * queue to the end of the slaves list, make the first element its
 763         * successor, the last one its predecessor, and make it the bond's
 764         * predecessor. 
 765         *
 766         * Just to clarify, so future bonding driver hackers don't go through
 767         * the same confusion stage I did trying to figure this out, the
 768         * slaves are stored in a double linked circular list, sortof.
 769         * In the ->next direction, the last slave points to the first slave,
 770         * bypassing bond; only the slaves are in the ->next direction.
 771         * In the ->prev direction, however, the first slave points to bond
 772         * and bond points to the last slave.
 773         *
 774         * It looks like a circle with a little bubble hanging off one side
 775         * in the ->prev direction only.
 776         *
 777         * When going through the list once, its best to start at bond->prev
 778         * and go in the ->prev direction, testing for bond.  Doing this
 779         * in the ->next direction doesn't work.  Trust me, I know this now.
 780         * :)  -mts 2002.03.14
 781         */
 782        new_slave->prev       = bond->prev;
 783        new_slave->prev->next = new_slave;
 784        bond->prev            = new_slave;
 785        new_slave->next       = bond->next;
 786
 787        update_slave_cnt(bond, 1);
 788}
 789
 790
 791/*
 792 * Less bad way to call ioctl from within the kernel; this needs to be
 793 * done some other way to get the call out of interrupt context.
 794 * Needs "ioctl" variable to be supplied by calling context.
 795 */
 796#define IOCTL(dev, arg, cmd) ({         \
 797        int ret;                        \
 798        mm_segment_t fs = get_fs();     \
 799        set_fs(get_ds());               \
 800        ret = ioctl(dev, arg, cmd);     \
 801        set_fs(fs);                     \
 802        ret; })
 803
 804/*
 805 * Get link speed and duplex from the slave's base driver
 806 * using ethtool. If for some reason the call fails or the
 807 * values are invalid, fake speed and duplex to 100/Full
 808 * and return error.
 809 */
 810static int bond_update_speed_duplex(struct slave *slave)
 811{
 812        struct net_device *dev = slave->dev;
 813        static int (* ioctl)(struct net_device *, struct ifreq *, int);
 814        struct ifreq ifr;
 815        struct ethtool_cmd etool;
 816
 817        ioctl = dev->do_ioctl;
 818        if (ioctl) {
 819                etool.cmd = ETHTOOL_GSET;
 820                ifr.ifr_data = (char*)&etool;
 821                if (IOCTL(dev, &ifr, SIOCETHTOOL) == 0) {
 822                        slave->speed = etool.speed;
 823                        slave->duplex = etool.duplex;
 824                } else {
 825                        goto err_out;
 826                }
 827        } else {
 828                goto err_out;
 829        }
 830
 831        switch (slave->speed) {
 832                case SPEED_10:
 833                case SPEED_100:
 834                case SPEED_1000:
 835                        break;
 836                default:
 837                        goto err_out;
 838        }
 839
 840        switch (slave->duplex) {
 841                case DUPLEX_FULL:
 842                case DUPLEX_HALF:
 843                        break;
 844                default:
 845                        goto err_out;
 846        }
 847
 848        return 0;
 849
 850err_out:
 851        /* Fake speed and duplex */
 852        slave->speed = SPEED_100;
 853        slave->duplex = DUPLEX_FULL;
 854        return -1;
 855}
 856
 857/* 
 858 * if <dev> supports MII link status reporting, check its link status.
 859 *
 860 * We either do MII/ETHTOOL ioctls, or check netif_carrier_ok(),
 861 * depening upon the setting of the use_carrier parameter.
 862 *
 863 * Return either BMSR_LSTATUS, meaning that the link is up (or we
 864 * can't tell and just pretend it is), or 0, meaning that the link is
 865 * down.
 866 *
 867 * If reporting is non-zero, instead of faking link up, return -1 if
 868 * both ETHTOOL and MII ioctls fail (meaning the device does not
 869 * support them).  If use_carrier is set, return whatever it says.
 870 * It'd be nice if there was a good way to tell if a driver supports
 871 * netif_carrier, but there really isn't.
 872 */
 873static int
 874bond_check_dev_link(struct net_device *dev, int reporting)
 875{
 876        static int (* ioctl)(struct net_device *, struct ifreq *, int);
 877        struct ifreq ifr;
 878        struct mii_ioctl_data *mii;
 879        struct ethtool_value etool;
 880
 881        if (use_carrier) {
 882                return netif_carrier_ok(dev) ? BMSR_LSTATUS : 0;
 883        }
 884
 885        ioctl = dev->do_ioctl;
 886        if (ioctl) {
 887                /* TODO: set pointer to correct ioctl on a per team member */
 888                /*       bases to make this more efficient. that is, once  */
 889                /*       we determine the correct ioctl, we will always    */
 890                /*       call it and not the others for that team          */
 891                /*       member.                                           */
 892
 893                /*
 894                 * We cannot assume that SIOCGMIIPHY will also read a
 895                 * register; not all network drivers (e.g., e100)
 896                 * support that.
 897                 */
 898
 899                /* Yes, the mii is overlaid on the ifreq.ifr_ifru */
 900                mii = (struct mii_ioctl_data *)&ifr.ifr_data;
 901                if (IOCTL(dev, &ifr, SIOCGMIIPHY) == 0) {
 902                        mii->reg_num = MII_BMSR;
 903                        if (IOCTL(dev, &ifr, SIOCGMIIREG) == 0) {
 904                                return mii->val_out & BMSR_LSTATUS;
 905                        }
 906                }
 907
 908                /* try SIOCETHTOOL ioctl, some drivers cache ETHTOOL_GLINK */
 909                /* for a period of time so we attempt to get link status   */
 910                /* from it last if the above MII ioctls fail...            */
 911                etool.cmd = ETHTOOL_GLINK;
 912                ifr.ifr_data = (char*)&etool;
 913                if (IOCTL(dev, &ifr, SIOCETHTOOL) == 0) {
 914                        if (etool.data == 1) {
 915                                return BMSR_LSTATUS;
 916                        } else { 
 917#ifdef BONDING_DEBUG
 918                                printk(KERN_INFO 
 919                                        ":: SIOCETHTOOL shows link down \n");
 920#endif
 921                                return 0;
 922                        } 
 923                }
 924
 925        }
 926 
 927        /*
 928         * If reporting, report that either there's no dev->do_ioctl,
 929         * or both SIOCGMIIREG and SIOCETHTOOL failed (meaning that we
 930         * cannot report link status).  If not reporting, pretend
 931         * we're ok.
 932         */
 933        return reporting ? -1 : BMSR_LSTATUS;
 934}
 935
 936static u16 bond_check_mii_link(bonding_t *bond)
 937{
 938        int has_active_interface = 0;
 939
 940        read_lock_bh(&bond->lock);
 941        read_lock(&bond->ptrlock);
 942        has_active_interface = (bond->current_slave != NULL);
 943        read_unlock(&bond->ptrlock);
 944        read_unlock_bh(&bond->lock);
 945
 946        return (has_active_interface ? BMSR_LSTATUS : 0);
 947}
 948
 949/* register to receive lacpdus on a bond */
 950static void bond_register_lacpdu(struct bonding *bond)
 951{
 952        struct packet_type* pk_type = &(BOND_AD_INFO(bond).ad_pkt_type);
 953
 954        /* initialize packet type */
 955        pk_type->type = PKT_TYPE_LACPDU;
 956        pk_type->dev = bond->device;
 957        pk_type->func = bond_3ad_lacpdu_recv;
 958
 959        dev_add_pack(pk_type);
 960}
 961
 962/* unregister to receive lacpdus on a bond */
 963static void bond_unregister_lacpdu(struct bonding *bond)
 964{
 965        dev_remove_pack(&(BOND_AD_INFO(bond).ad_pkt_type));
 966}
 967
 968static int bond_open(struct net_device *dev)
 969{
 970        struct bonding *bond = (struct bonding *)(dev->priv);
 971        struct timer_list *timer = &((struct bonding *)(dev->priv))->mii_timer;
 972        struct timer_list *arp_timer = &((struct bonding *)(dev->priv))->arp_timer;
 973
 974        if ((bond_mode == BOND_MODE_TLB) ||
 975            (bond_mode == BOND_MODE_ALB)) {
 976                struct timer_list *alb_timer = &(BOND_ALB_INFO(bond).alb_timer);
 977
 978                /* bond_alb_initialize must be called before the timer
 979                 * is started.
 980                 */
 981                if (bond_alb_initialize(bond, (bond_mode == BOND_MODE_ALB))) {
 982                        /* something went wrong - fail the open operation */
 983                        return -1;
 984                }
 985
 986                init_timer(alb_timer);
 987                alb_timer->expires  = jiffies + 1;
 988                alb_timer->data     = (unsigned long)bond;
 989                alb_timer->function = (void *)&bond_alb_monitor;
 990                add_timer(alb_timer);
 991        }
 992
 993        if (miimon > 0) {  /* link check interval, in milliseconds. */
 994                init_timer(timer);
 995                timer->expires  = jiffies + (miimon * HZ / 1000);
 996                timer->data     = (unsigned long)dev;
 997                timer->function = (void *)&bond_mii_monitor;
 998                add_timer(timer);
 999        }
1000
1001        if (arp_interval> 0) {  /* arp interval, in milliseconds. */
1002                init_timer(arp_timer);
1003                arp_timer->expires  = jiffies + (arp_interval * HZ / 1000);
1004                arp_timer->data     = (unsigned long)dev;
1005                if (bond_mode == BOND_MODE_ACTIVEBACKUP) {
1006                        arp_timer->function = (void *)&activebackup_arp_monitor;
1007                } else {
1008                        arp_timer->function = (void *)&loadbalance_arp_monitor;
1009                }
1010                add_timer(arp_timer);
1011        }
1012
1013        if (bond_mode == BOND_MODE_8023AD) {
1014                struct timer_list *ad_timer = &(BOND_AD_INFO(bond).ad_timer);
1015                init_timer(ad_timer);
1016                ad_timer->expires  = jiffies + (AD_TIMER_INTERVAL * HZ / 1000);
1017                ad_timer->data     = (unsigned long)bond;
1018                ad_timer->function = (void *)&bond_3ad_state_machine_handler;
1019                add_timer(ad_timer);
1020
1021                /* register to receive LACPDUs */
1022                bond_register_lacpdu(bond);
1023        }
1024
1025        return 0;
1026}
1027
1028static int bond_close(struct net_device *master)
1029{
1030        bonding_t *bond = (struct bonding *) master->priv;
1031
1032        write_lock_bh(&bond->lock);
1033
1034        if (miimon > 0) {  /* link check interval, in milliseconds. */
1035                del_timer(&bond->mii_timer);
1036        }
1037        if (arp_interval> 0) {  /* arp interval, in milliseconds. */
1038                del_timer(&bond->arp_timer);
1039                if (arp_target_hw_addr != NULL) {
1040                        kfree(arp_target_hw_addr); 
1041                        arp_target_hw_addr = NULL;
1042                }
1043        }
1044
1045        if (bond_mode == BOND_MODE_8023AD) {
1046                del_timer_sync(&(BOND_AD_INFO(bond).ad_timer));
1047
1048                /* Unregister the receive of LACPDUs */
1049                bond_unregister_lacpdu(bond);
1050        }
1051
1052        bond_mc_list_destroy (bond);
1053
1054        write_unlock_bh(&bond->lock);
1055
1056        /* Release the bonded slaves */
1057        bond_release_all(master);
1058
1059        if ((bond_mode == BOND_MODE_TLB) ||
1060            (bond_mode == BOND_MODE_ALB)) {
1061                del_timer_sync(&(BOND_ALB_INFO(bond).alb_timer));
1062
1063                bond_alb_deinitialize(bond);
1064        }
1065
1066        return 0;
1067}
1068
1069/* 
1070 * flush all members of flush->mc_list from device dev->mc_list
1071 */
1072static void bond_mc_list_flush(struct net_device *dev, struct net_device *flush)
1073{ 
1074        struct dev_mc_list *dmi; 
1075 
1076        for (dmi = flush->mc_list; dmi != NULL; dmi = dmi->next) 
1077                dev_mc_delete(dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
1078
1079        if (bond_mode == BOND_MODE_8023AD) {
1080                /* del lacpdu mc addr from mc list */
1081                u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
1082
1083                dev_mc_delete(dev, lacpdu_multicast, ETH_ALEN, 0);
1084        }
1085}
1086
1087/*
1088 * Totally destroys the mc_list in bond
1089 */
1090static void bond_mc_list_destroy(struct bonding *bond)
1091{
1092        struct dev_mc_list *dmi;
1093
1094        dmi = bond->mc_list; 
1095        while (dmi) { 
1096                bond->mc_list = dmi->next; 
1097                kfree(dmi); 
1098                dmi = bond->mc_list; 
1099        }
1100}
1101
1102/*
1103 * Add a Multicast address to every slave in the bonding group
1104 */
1105static void bond_mc_add(bonding_t *bond, void *addr, int alen)
1106{ 
1107        slave_t *slave;
1108        switch (multicast_mode) {
1109        case BOND_MULTICAST_ACTIVE :
1110                /* write lock already acquired */
1111                if (bond->current_slave != NULL)
1112                        dev_mc_add(bond->current_slave->dev, addr, alen, 0);
1113                break;
1114        case BOND_MULTICAST_ALL :
1115                for (slave = bond->prev; slave != (slave_t*)bond; slave = slave->prev)
1116                        dev_mc_add(slave->dev, addr, alen, 0);
1117                break;
1118        case BOND_MULTICAST_DISABLED :
1119                break;
1120        }
1121} 
1122
1123/*
1124 * Remove a multicast address from every slave in the bonding group
1125 */
1126static void bond_mc_delete(bonding_t *bond, void *addr, int alen)
1127{ 
1128        slave_t *slave; 
1129        switch (multicast_mode) {
1130        case BOND_MULTICAST_ACTIVE :
1131                /* write lock already acquired */
1132                if (bond->current_slave != NULL)
1133                        dev_mc_delete(bond->current_slave->dev, addr, alen, 0);
1134                break;
1135        case BOND_MULTICAST_ALL :
1136                for (slave = bond->prev; slave != (slave_t*)bond; slave = slave->prev)
1137                        dev_mc_delete(slave->dev, addr, alen, 0);
1138                break;
1139        case BOND_MULTICAST_DISABLED :
1140                break;
1141        }
1142} 
1143
1144/*
1145 * Copy all the Multicast addresses from src to the bonding device dst
1146 */
1147static int bond_mc_list_copy (struct dev_mc_list *src, struct bonding *dst,
1148 int gpf_flag)
1149{
1150        struct dev_mc_list *dmi, *new_dmi;
1151
1152        for (dmi = src; dmi != NULL; dmi = dmi->next) { 
1153                new_dmi = kmalloc(sizeof(struct dev_mc_list), gpf_flag);
1154
1155                if (new_dmi == NULL) {
1156                        return -ENOMEM; 
1157                }
1158
1159                new_dmi->next = dst->mc_list; 
1160                dst->mc_list = new_dmi;
1161
1162                new_dmi->dmi_addrlen = dmi->dmi_addrlen; 
1163                memcpy(new_dmi->dmi_addr, dmi->dmi_addr, dmi->dmi_addrlen); 
1164                new_dmi->dmi_users = dmi->dmi_users;
1165                new_dmi->dmi_gusers = dmi->dmi_gusers; 
1166        } 
1167        return 0;
1168}
1169
1170/*
1171 * Returns 0 if dmi1 and dmi2 are the same, non-0 otherwise
1172 */
1173static inline int dmi_same(struct dev_mc_list *dmi1, struct dev_mc_list *dmi2)
1174{ 
1175        return memcmp(dmi1->dmi_addr, dmi2->dmi_addr, dmi1->dmi_addrlen) == 0 &&
1176         dmi1->dmi_addrlen == dmi2->dmi_addrlen;
1177} 
1178
1179/*
1180 * Push the promiscuity flag down to appropriate slaves
1181 */
1182static void bond_set_promiscuity(bonding_t *bond, int inc)
1183{ 
1184        slave_t *slave; 
1185
1186        if (USES_PRIMARY(bond_mode)) {
1187                if (bond->current_slave) {
1188                        dev_set_promiscuity(bond->current_slave->dev, inc);
1189                }
1190
1191        } else { 
1192                for (slave = bond->prev; slave != (slave_t*)bond;
1193                     slave = slave->prev) {
1194                        dev_set_promiscuity(slave->dev, inc);
1195                }
1196        }
1197} 
1198
1199/*
1200 * Push the allmulti flag down to all slaves
1201 */
1202static void bond_set_allmulti(bonding_t *bond, int inc)
1203{ 
1204        slave_t *slave; 
1205        switch (multicast_mode) {
1206        case BOND_MULTICAST_ACTIVE : 
1207                /* write lock already acquired */
1208                if (bond->current_slave != NULL)
1209                        dev_set_allmulti(bond->current_slave->dev, inc);
1210                break;
1211        case BOND_MULTICAST_ALL :
1212                for (slave = bond->prev; slave != (slave_t*)bond; slave = slave->prev)
1213                        dev_set_allmulti(slave->dev, inc);
1214                break;
1215        case BOND_MULTICAST_DISABLED :
1216                break;
1217        }
1218} 
1219
1220/* 
1221 * returns dmi entry if found, NULL otherwise 
1222 */
1223static struct dev_mc_list* bond_mc_list_find_dmi(struct dev_mc_list *dmi,
1224 struct dev_mc_list *mc_list)
1225{ 
1226        struct dev_mc_list *idmi;
1227
1228        for (idmi = mc_list; idmi != NULL; idmi = idmi->next) {
1229                if (dmi_same(dmi, idmi)) {
1230                        return idmi; 
1231                }
1232        }
1233        return NULL;
1234} 
1235
1236static void set_multicast_list(struct net_device *master)
1237{
1238        bonding_t *bond = master->priv;
1239        struct dev_mc_list *dmi;
1240
1241        write_lock_bh(&bond->lock);
1242
1243        /*
1244         * Do promisc before checking multicast_mode
1245         */
1246        if ( (master->flags & IFF_PROMISC) && !(bond->flags & IFF_PROMISC) )
1247                bond_set_promiscuity(bond, 1); 
1248
1249        if ( !(master->flags & IFF_PROMISC) && (bond->flags & IFF_PROMISC) ) 
1250                bond_set_promiscuity(bond, -1); 
1251
1252        if (multicast_mode == BOND_MULTICAST_DISABLED) {
1253                bond->flags = master->flags;
1254                write_unlock_bh(&bond->lock);
1255                return;
1256        }
1257
1258        /* set allmulti flag to slaves */ 
1259        if ( (master->flags & IFF_ALLMULTI) && !(bond->flags & IFF_ALLMULTI) ) 
1260                bond_set_allmulti(bond, 1); 
1261
1262        if ( !(master->flags & IFF_ALLMULTI) && (bond->flags & IFF_ALLMULTI) )
1263                bond_set_allmulti(bond, -1); 
1264
1265        bond->flags = master->flags; 
1266
1267        /* looking for addresses to add to slaves' mc list */ 
1268        for (dmi = master->mc_list; dmi != NULL; dmi = dmi->next) { 
1269                if (bond_mc_list_find_dmi(dmi, bond->mc_list) == NULL) 
1270                 bond_mc_add(bond, dmi->dmi_addr, dmi->dmi_addrlen); 
1271        } 
1272
1273        /* looking for addresses to delete from slaves' list */ 
1274        for (dmi = bond->mc_list; dmi != NULL; dmi = dmi->next) { 
1275                if (bond_mc_list_find_dmi(dmi, master->mc_list) == NULL) 
1276                 bond_mc_delete(bond, dmi->dmi_addr, dmi->dmi_addrlen); 
1277        }
1278
1279
1280        /* save master's multicast list */ 
1281        bond_mc_list_destroy (bond);
1282        bond_mc_list_copy (master->mc_list, bond, GFP_ATOMIC);
1283
1284        write_unlock_bh(&bond->lock);
1285}
1286
1287/*
1288 * Update the mc list and multicast-related flags for the new and 
1289 * old active slaves (if any) according to the multicast mode, and
1290 * promiscuous flags unconditionally.
1291 */
1292static void bond_mc_update(bonding_t *bond, slave_t *new, slave_t *old)
1293{
1294        struct dev_mc_list *dmi;
1295
1296        if (USES_PRIMARY(bond_mode)) {
1297                if (bond->device->flags & IFF_PROMISC) {
1298                        if (old)
1299                                dev_set_promiscuity(old->dev, -1);
1300                        if (new)
1301                                dev_set_promiscuity(new->dev, 1);
1302                }
1303        }
1304
1305        switch(multicast_mode) {
1306        case BOND_MULTICAST_ACTIVE :            
1307                if (bond->device->flags & IFF_ALLMULTI) {
1308                        if (old)
1309                                dev_set_allmulti(old->dev, -1);
1310                        if (new)
1311                                dev_set_allmulti(new->dev, 1);
1312                }
1313                /* first remove all mc addresses from old slave if any,
1314                   and _then_ add them to new active slave */
1315                if (old) {
1316                        for (dmi = bond->device->mc_list; dmi != NULL; dmi = dmi->next)
1317                                dev_mc_delete(old->dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
1318                }
1319                if (new) {
1320                        for (dmi = bond->device->mc_list; dmi != NULL; dmi = dmi->next)
1321                                dev_mc_add(new->dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
1322                }
1323                break;
1324        case BOND_MULTICAST_ALL :
1325                /* nothing to do: mc list is already up-to-date on all slaves */
1326                break;
1327        case BOND_MULTICAST_DISABLED :
1328                break;
1329        }
1330}
1331
1332/* enslave device <slave> to bond device <master> */
1333static int bond_enslave(struct net_device *master_dev, 
1334                        struct net_device *slave_dev)
1335{
1336        bonding_t *bond = NULL;
1337        slave_t *new_slave = NULL;
1338        unsigned long rflags = 0;
1339        int err = 0;
1340        struct dev_mc_list *dmi;
1341        struct in_ifaddr **ifap;
1342        struct in_ifaddr *ifa;
1343        int link_reporting;
1344        struct sockaddr addr;
1345
1346        if (master_dev == NULL || slave_dev == NULL) {
1347                return -ENODEV;
1348        }
1349        bond = (struct bonding *) master_dev->priv;
1350
1351        if (slave_dev->do_ioctl == NULL) {
1352                printk(KERN_DEBUG
1353                        "Warning : no link monitoring support for %s\n",
1354                        slave_dev->name);
1355        }
1356
1357
1358        /* bond must be initialized by bond_open() before enslaving */
1359        if (!(master_dev->flags & IFF_UP)) {
1360#ifdef BONDING_DEBUG
1361                printk(KERN_CRIT "Error, master_dev is not up\n");
1362#endif
1363                return -EPERM;
1364        }
1365
1366        /* already enslaved */
1367        if (master_dev->flags & IFF_SLAVE || slave_dev->flags & IFF_SLAVE) {
1368#ifdef BONDING_DEBUG
1369                printk(KERN_CRIT "Error, Device was already enslaved\n");
1370#endif
1371                return -EBUSY;
1372        }
1373
1374        if (app_abi_ver >= 1) {
1375                /* The application is using an ABI, which requires the
1376                 * slave interface to be closed.
1377                 */
1378                if ((slave_dev->flags & IFF_UP)) {
1379#ifdef BONDING_DEBUG
1380                        printk(KERN_CRIT "Error, slave_dev is up\n");
1381#endif
1382                        return -EPERM;
1383                }
1384
1385                if (slave_dev->set_mac_address == NULL) {
1386                        printk(KERN_CRIT
1387                               "The slave device you specified does not support"
1388                               " setting the MAC address.\n");
1389                        printk(KERN_CRIT
1390                               "Your kernel likely does not support slave"
1391                               " devices.\n");
1392
1393                        return -EOPNOTSUPP;
1394                }
1395        } else {
1396                /* The application is not using an ABI, which requires the
1397                 * slave interface to be open.
1398                 */
1399                if (!(slave_dev->flags & IFF_UP)) {
1400#ifdef BONDING_DEBUG
1401                        printk(KERN_CRIT "Error, slave_dev is not running\n");
1402#endif
1403                        return -EINVAL;
1404                }
1405
1406                if ((bond_mode == BOND_MODE_8023AD) ||
1407                    (bond_mode == BOND_MODE_TLB) ||
1408                    (bond_mode == BOND_MODE_ALB)) {
1409                        printk(KERN_ERR
1410                               "bonding: Error: to use %s mode, you must "
1411                               "upgrade ifenslave.\n", bond_mode_name());
1412                        return -EOPNOTSUPP;
1413                }
1414        }
1415
1416        if ((new_slave = kmalloc(sizeof(slave_t), GFP_KERNEL)) == NULL) {
1417                return -ENOMEM;
1418        }
1419        memset(new_slave, 0, sizeof(slave_t));
1420
1421        /* save slave's original flags before calling
1422         * netdev_set_master and dev_open
1423         */
1424        new_slave->original_flags = slave_dev->flags;
1425
1426        if (app_abi_ver >= 1) {
1427                /* save slave's original ("permanent") mac address for
1428                 * modes that needs it, and for restoring it upon release,
1429                 * and then set it to the master's address
1430                 */
1431                memcpy(new_slave->perm_hwaddr, slave_dev->dev_addr, ETH_ALEN);
1432
1433                if (bond->slave_cnt > 0) {
1434                        /* set slave to master's mac address
1435                         * The application already set the master's
1436                         * mac address to that of the first slave
1437                         */
1438                        memcpy(addr.sa_data, master_dev->dev_addr, master_dev->addr_len);
1439                        addr.sa_family = slave_dev->type;
1440                        err = slave_dev->set_mac_address(slave_dev, &addr);
1441                        if (err) {
1442#ifdef BONDING_DEBUG
1443                                printk(KERN_CRIT "Error %d calling set_mac_address\n", err);
1444#endif
1445                                goto err_free;
1446                        }
1447                }
1448
1449                /* open the slave since the application closed it */
1450                err = dev_open(slave_dev);
1451                if (err) {
1452#ifdef BONDING_DEBUG
1453                        printk(KERN_CRIT "Openning slave %s failed\n", slave_dev->name);
1454#endif
1455                        goto err_restore_mac;
1456                }
1457        }
1458
1459        err = netdev_set_master(slave_dev, master_dev);
1460        if (err) {
1461#ifdef BONDING_DEBUG
1462                printk(KERN_CRIT "Error %d calling netdev_set_master\n", err);
1463#endif
1464                if (app_abi_ver < 1) {
1465                        goto err_free;
1466                } else {
1467                        goto err_close;
1468                }
1469        }
1470
1471        new_slave->dev = slave_dev;
1472
1473        if ((bond_mode == BOND_MODE_TLB) ||
1474            (bond_mode == BOND_MODE_ALB)) {
1475                /* bond_alb_init_slave() must be called before all other stages since
1476                 * it might fail and we do not want to have to undo everything
1477                 */
1478                err = bond_alb_init_slave(bond, new_slave);
1479                if (err) {
1480                        goto err_unset_master;
1481                }
1482        }
1483
1484        /* set promiscuity level to new slave */ 
1485        if (master_dev->flags & IFF_PROMISC) {
1486                /* If the mode USES_PRIMARY, then the new slave gets the
1487                 * master's promisc (and mc) settings only if it becomes the
1488                 * current_slave, and that is taken care of later when calling
1489                 * bond_change_active()
1490                 */
1491                if (!USES_PRIMARY(bond_mode)) {
1492                        dev_set_promiscuity(slave_dev, 1); 
1493                }
1494        }
1495 
1496        if (multicast_mode == BOND_MULTICAST_ALL) {
1497                /* set allmulti level to new slave */
1498                if (master_dev->flags & IFF_ALLMULTI) 
1499                        dev_set_allmulti(slave_dev, 1); 
1500                
1501                /* upload master's mc_list to new slave */ 
1502                for (dmi = master_dev->mc_list; dmi != NULL; dmi = dmi->next) 
1503                        dev_mc_add (slave_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
1504        }
1505
1506        if (bond_mode == BOND_MODE_8023AD) {
1507                /* add lacpdu mc addr to mc list */
1508                u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
1509
1510                dev_mc_add(slave_dev, lacpdu_multicast, ETH_ALEN, 0);
1511        }
1512
1513        write_lock_bh(&bond->lock);
1514        
1515        bond_attach_slave(bond, new_slave);
1516        new_slave->delay = 0;
1517        new_slave->link_failure_count = 0;
1518
1519        if (miimon > 0 && !use_carrier) {
1520                link_reporting = bond_check_dev_link(slave_dev, 1);
1521
1522                if ((link_reporting == -1) && (arp_interval == 0)) {
1523                        /*
1524                         * miimon is set but a bonded network driver
1525                         * does not support ETHTOOL/MII and
1526                         * arp_interval is not set.  Note: if
1527                         * use_carrier is enabled, we will never go
1528                         * here (because netif_carrier is always
1529                         * supported); thus, we don't need to change
1530                         * the messages for netif_carrier.
1531                         */ 
1532                        printk(KERN_ERR
1533                                "bond_enslave(): MII and ETHTOOL support not "
1534                                "available for interface %s, and "
1535                                "arp_interval/arp_ip_target module parameters "
1536                                "not specified, thus bonding will not detect "
1537                                "link failures! see bonding.txt for details.\n",
1538                                slave_dev->name);
1539                } else if (link_reporting == -1) {
1540                        /* unable  get link status using mii/ethtool */
1541                        printk(KERN_WARNING 
1542                               "bond_enslave: can't get link status from "
1543                               "interface %s; the network driver associated "
1544                               "with this interface does not support "
1545                               "MII or ETHTOOL link status reporting, thus "
1546                               "miimon has no effect on this interface.\n", 
1547                               slave_dev->name);
1548                }
1549        }
1550
1551        /* check for initial state */
1552        if ((miimon <= 0) ||
1553            (bond_check_dev_link(slave_dev, 0) == BMSR_LSTATUS)) {
1554                if (updelay) {
1555#ifdef BONDING_DEBUG
1556                        printk(KERN_CRIT "Initial state of slave_dev is "
1557                               "BOND_LINK_BACK\n");
1558#endif
1559                        new_slave->link  = BOND_LINK_BACK;
1560                        new_slave->delay = updelay;
1561                }
1562                else {
1563#ifdef BONDING_DEBUG
1564                        printk(KERN_DEBUG "Initial state of slave_dev is "
1565                                "BOND_LINK_UP\n");
1566#endif
1567                        new_slave->link  = BOND_LINK_UP;
1568                }
1569                new_slave->jiffies = jiffies;
1570        }
1571        else {
1572#ifdef BONDING_DEBUG
1573                printk(KERN_CRIT "Initial state of slave_dev is "
1574                        "BOND_LINK_DOWN\n");
1575#endif
1576                new_slave->link  = BOND_LINK_DOWN;
1577        }
1578
1579        if (bond_update_speed_duplex(new_slave) &&
1580            (new_slave->link != BOND_LINK_DOWN)) {
1581
1582                printk(KERN_WARNING
1583                       "bond_enslave(): failed to get speed/duplex from %s, "
1584                       "speed forced to 100Mbps, duplex forced to Full.\n",
1585                       new_slave->dev->name);
1586                if (bond_mode == BOND_MODE_8023AD) {
1587                        printk(KERN_WARNING
1588                               "Operation of 802.3ad mode requires ETHTOOL support "
1589                               "in base driver for proper aggregator selection.\n");
1590                }
1591        }
1592
1593        /* if we're in active-backup mode, we need one and only one active
1594         * interface. The backup interfaces will have their NOARP flag set
1595         * because we need them to be completely deaf and not to respond to
1596         * any ARP request on the network to avoid fooling a switch. Thus,
1597         * since we guarantee that current_slave always point to the last
1598         * usable interface, we just have to verify this interface's flag.
1599         */
1600        if (bond_mode == BOND_MODE_ACTIVEBACKUP) {
1601                if (((bond->current_slave == NULL)
1602                        || (bond->current_slave->dev->flags & IFF_NOARP))
1603                        && (new_slave->link != BOND_LINK_DOWN)) {
1604#ifdef BONDING_DEBUG
1605                        printk(KERN_CRIT "This is the first active slave\n");
1606#endif
1607                        /* first slave or no active slave yet, and this link
1608                           is OK, so make this interface the active one */
1609                        change_active_interface(bond, new_slave);
1610                }
1611                else {
1612#ifdef BONDING_DEBUG
1613                        printk(KERN_CRIT "This is just a backup slave\n");
1614#endif
1615                        bond_set_slave_inactive_flags(new_slave);
1616                }
1617                if (((struct in_device *)slave_dev->ip_ptr) != NULL) {
1618                        read_lock_irqsave(&(((struct in_device *)slave_dev->ip_ptr)->lock), rflags);
1619                        ifap= &(((struct in_device *)slave_dev->ip_ptr)->ifa_list);
1620                        ifa = *ifap;
1621                        if (ifa != NULL)
1622                                my_ip = ifa->ifa_address;
1623                        read_unlock_irqrestore(&(((struct in_device *)slave_dev->ip_ptr)->lock), rflags);
1624                }
1625
1626                /* if there is a primary slave, remember it */
1627                if (primary != NULL) {
1628                        if (strcmp(primary, new_slave->dev->name) == 0) {
1629                                bond->primary_slave = new_slave;
1630                        }
1631                }
1632        } else if (bond_mode == BOND_MODE_8023AD) {
1633                /* in 802.3ad mode, the internal mechanism
1634                 * will activate the slaves in the selected
1635                 * aggregator
1636                 */
1637                bond_set_slave_inactive_flags(new_slave);
1638                /* if this is the first slave */
1639                if (new_slave == bond->next) {
1640                        SLAVE_AD_INFO(new_slave).id = 1;
1641                        /* Initialize AD with the number of times that the AD timer is called in 1 second
1642                         * can be called only after the mac address of the bond is set
1643                         */
1644                        bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL,
1645                                            lacp_fast);
1646                } else {
1647                        SLAVE_AD_INFO(new_slave).id =
1648                        SLAVE_AD_INFO(new_slave->prev).id + 1;
1649                }
1650
1651                bond_3ad_bind_slave(new_slave);
1652        } else if ((bond_mode == BOND_MODE_TLB) ||
1653                   (bond_mode == BOND_MODE_ALB)) {
1654                new_slave->state = BOND_STATE_ACTIVE;
1655                if ((bond->current_slave == NULL) && (new_slave->link != BOND_LINK_DOWN)) {
1656                        /* first slave or no active slave yet, and this link
1657                         * is OK, so make this interface the active one
1658                         */
1659                        change_active_interface(bond, new_slave);
1660                }
1661
1662                /* if there is a primary slave, remember it */
1663                if (primary != NULL) {
1664                        if (strcmp(primary, new_slave->dev->name) == 0) {
1665                                bond->primary_slave = new_slave;
1666                        }
1667                }
1668        } else {
1669#ifdef BONDING_DEBUG
1670                printk(KERN_CRIT "This slave is always active in trunk mode\n");
1671#endif
1672                /* always active in trunk mode */
1673                new_slave->state = BOND_STATE_ACTIVE;
1674
1675                /* In trunking mode there is little meaning to current_slave
1676                 * anyway (it holds no special properties of the bond device),
1677                 * so we can change it without calling change_active_interface()
1678                 */
1679                if (bond->current_slave == NULL) 
1680                        bond->current_slave = new_slave;
1681        }
1682
1683        write_unlock_bh(&bond->lock);
1684
1685        if (app_abi_ver < 1) {
1686                /*
1687                 * !!! This is to support old versions of ifenslave.
1688                 * We can remove this in 2.5 because our ifenslave takes
1689                 * care of this for us.
1690                 * We check to see if the master has a mac address yet.
1691                 * If not, we'll give it the mac address of our slave device.
1692                 */
1693                int ndx = 0;
1694
1695                for (ndx = 0; ndx < slave_dev->addr_len; ndx++) {
1696#ifdef BONDING_DEBUG
1697                        printk(KERN_DEBUG
1698                               "Checking ndx=%d of master_dev->dev_addr\n", ndx);
1699#endif
1700                        if (master_dev->dev_addr[ndx] != 0) {
1701#ifdef BONDING_DEBUG
1702                                printk(KERN_DEBUG
1703                                       "Found non-zero byte at ndx=%d\n", ndx);
1704#endif
1705                                break;
1706                        }
1707                }
1708                if (ndx == slave_dev->addr_len) {
1709                        /*
1710                         * We got all the way through the address and it was
1711                         * all 0's.
1712                         */
1713#ifdef BONDING_DEBUG
1714                        printk(KERN_DEBUG "%s doesn't have a MAC address yet.  ",
1715                               master_dev->name);
1716                        printk(KERN_DEBUG "Going to give assign it from %s.\n",
1717                               slave_dev->name);
1718#endif
1719                        bond_sethwaddr(master_dev, slave_dev);
1720                }
1721        }
1722
1723        printk (KERN_INFO "%s: enslaving %s as a%s interface with a%s link.\n",
1724                master_dev->name, slave_dev->name,
1725                new_slave->state == BOND_STATE_ACTIVE ? "n active" : " backup",
1726                new_slave->link != BOND_LINK_DOWN ? "n up" : " down");
1727
1728        /* enslave is successful */
1729        return 0;
1730
1731/* Undo stages on error */
1732err_unset_master:
1733        netdev_set_master(slave_dev, NULL);
1734
1735err_close:
1736        dev_close(slave_dev);
1737
1738err_restore_mac:
1739        memcpy(addr.sa_data, new_slave->perm_hwaddr, ETH_ALEN);
1740        addr.sa_family = slave_dev->type;
1741        slave_dev->set_mac_address(slave_dev, &addr);
1742
1743err_free:
1744        kfree(new_slave);
1745        return err;
1746}
1747
1748/* 
1749 * This function changes the active slave to slave <slave_dev>.
1750 * It returns -EINVAL in the following cases.
1751 *  - <slave_dev> is not found in the list.
1752 *  - There is not active slave now.
1753 *  - <slave_dev> is already active.
1754 *  - The link state of <slave_dev> is not BOND_LINK_UP.
1755 *  - <slave_dev> is not running.
1756 * In these cases, this fuction does nothing.
1757 * In the other cases, currnt_slave pointer is changed and 0 is returned.
1758 */
1759static int bond_change_active(struct net_device *master_dev, struct net_device *slave_dev)
1760{
1761        bonding_t *bond;
1762        slave_t *slave;
1763        slave_t *oldactive = NULL;
1764        slave_t *newactive = NULL;
1765        int ret = 0;
1766
1767        if (master_dev == NULL || slave_dev == NULL) {
1768                return -ENODEV;
1769        }
1770
1771        /* Verify that master_dev is indeed the master of slave_dev */
1772        if (!(slave_dev->flags & IFF_SLAVE) ||
1773            (slave_dev->master != master_dev)) {
1774
1775                return -EINVAL;
1776        }
1777
1778        bond = (struct bonding *) master_dev->priv;
1779        write_lock_bh(&bond->lock);
1780        slave = (slave_t *)bond;
1781        oldactive = bond->current_slave;
1782
1783        while ((slave = slave->prev) != (slave_t *)bond) {
1784                if(slave_dev == slave->dev) {
1785                        newactive = slave;
1786                        break;
1787                }
1788        }
1789
1790        /*
1791         * Changing to the current active: do nothing; return success.
1792         */
1793        if (newactive && (newactive == oldactive)) {
1794                write_unlock_bh(&bond->lock);
1795                return 0;
1796        }
1797
1798        if ((newactive != NULL)&&
1799            (oldactive != NULL)&&
1800            (newactive->link == BOND_LINK_UP)&&
1801            IS_UP(newactive->dev)) {
1802                change_active_interface(bond, newactive);
1803        } else {
1804                ret = -EINVAL;
1805        }
1806        write_unlock_bh(&bond->lock);
1807        return ret;
1808}
1809
1810/**
1811 * find_best_interface - select the best available slave to be the active one
1812 * @bond: our bonding struct
1813 *
1814 * Warning: Caller must hold ptrlock for writing.
1815 */
1816static struct slave *find_best_interface(struct bonding *bond)
1817{
1818        struct slave *newslave, *oldslave;
1819        struct slave *bestslave = NULL;
1820        int mintime;
1821
1822        newslave = oldslave = bond->current_slave;
1823
1824        if (newslave == NULL) { /* there were no active slaves left */
1825                if (bond->next != (slave_t *)bond) {  /* found one slave */
1826                        newslave = bond->next;
1827                } else {
1828                        return NULL; /* still no slave, return NULL */
1829                }
1830        }
1831
1832        mintime = updelay;
1833
1834        /* first try the primary link; if arping, a link must tx/rx traffic 
1835         * before it can be considered the current_slave - also, we would skip 
1836         * slaves between the current_slave and primary_slave that may be up 
1837         * and able to arp
1838         */
1839        if ((bond->primary_slave != NULL) && (arp_interval == 0)) {
1840                if (IS_UP(bond->primary_slave->dev)) 
1841                        newslave = bond->primary_slave;
1842        }
1843
1844        /* remember where to stop iterating over the slaves */
1845        oldslave = newslave;
1846
1847        do {
1848                if (IS_UP(newslave->dev)) {
1849                        if (newslave->link == BOND_LINK_UP) {
1850                                return newslave;
1851                        }
1852                        else if (newslave->link == BOND_LINK_BACK) {
1853                                /* link up, but waiting for stabilization */
1854                                if (newslave->delay < mintime) {
1855                                        mintime = newslave->delay;
1856                                        bestslave = newslave;
1857                                }
1858                        }
1859                }
1860        } while ((newslave = newslave->next) != oldslave);
1861
1862        return bestslave;
1863}
1864
1865/**
1866 * change_active_interface - change the active slave into the specified one
1867 * @bond: our bonding struct
1868 * @new: the new slave to make the active one
1869 * 
1870 * Set the new slave to the bond's settings and unset them on the old
1871 * current_slave.
1872 * Setting include flags, mc-list, promiscuity, allmulti, etc.
1873 *
1874 * If @new's link state is %BOND_LINK_BACK we'll set it to %BOND_LINK_UP,
1875 * because it is apparently the best available slave we have, even though its
1876 * updelay hasn't timed out yet.
1877 *
1878 * Warning: Caller must hold ptrlock for writing.
1879 */
1880static void change_active_interface(struct bonding *bond, struct slave *new)
1881{
1882        struct slave *old = bond->current_slave;
1883
1884        if (old == new) {
1885                return;
1886        }
1887
1888        if (new) {
1889                if (new->link == BOND_LINK_BACK) {
1890                        if (USES_PRIMARY(bond_mode)) {
1891                                printk (KERN_INFO
1892                                        "%s: making interface %s the new "
1893                                        "active one %d ms earlier.\n",
1894                                        bond->device->name, new->dev->name,
1895                                        (updelay - new->delay) * miimon);
1896                        }
1897
1898                        new->delay = 0;
1899                        new->link = BOND_LINK_UP;
1900                        new->jiffies = jiffies;
1901
1902                        if (bond_mode == BOND_MODE_8023AD) {
1903                                bond_3ad_handle_link_change(new, BOND_LINK_UP);
1904                        }
1905
1906                        if ((bond_mode == BOND_MODE_TLB) ||
1907                            (bond_mode == BOND_MODE_ALB)) {
1908                                bond_alb_handle_link_change(bond, new, BOND_LINK_UP);
1909                        }
1910                } else {
1911                        if (USES_PRIMARY(bond_mode)) {
1912                                printk (KERN_INFO
1913                                        "%s: making interface %s the new active one.\n",
1914                                        bond->device->name, new->dev->name);
1915                        }
1916                }
1917        }
1918
1919        if (bond_mode == BOND_MODE_ACTIVEBACKUP) {
1920                if (old) {
1921                        bond_set_slave_inactive_flags(old);
1922                }
1923
1924                if (new) {
1925                        bond_set_slave_active_flags(new);
1926                }
1927        }
1928
1929        if (USES_PRIMARY(bond_mode)) {
1930                bond_mc_update(bond, new, old);
1931        }
1932
1933        if ((bond_mode == BOND_MODE_TLB) ||
1934            (bond_mode == BOND_MODE_ALB)) {
1935                bond_alb_assign_current_slave(bond, new);
1936        } else {
1937                bond->current_slave = new;
1938        }
1939}
1940
1941/**
1942 * reselect_active_interface - select a new active slave, if needed
1943 * @bond: our bonding struct
1944 *
1945 * This functions shoud be called when one of the following occurs:
1946 * - The old current_slave has been released or lost its link.
1947 * - The primary_slave has got its link back.
1948 * - A slave has got its link back and there's no old current_slave.
1949 *
1950 * Warning: Caller must hold ptrlock for writing.
1951 */
1952static void reselect_active_interface(struct bonding *bond)
1953{
1954        struct slave *best_slave;
1955
1956        best_slave = find_best_interface(bond);
1957
1958        if (best_slave != bond->current_slave) {
1959                change_active_interface(bond, best_slave);
1960        }
1961}
1962
1963/*
1964 * Try to release the slave device <slave> from the bond device <master>
1965 * It is legal to access current_slave without a lock because all the function
1966 * is write-locked.
1967 *
1968 * The rules for slave state should be:
1969 *   for Active/Backup:
1970 *     Active stays on all backups go down
1971 *   for Bonded connections:
1972 *     The first up interface should be left on and all others downed.
1973 */
1974static int bond_release(struct net_device *master, struct net_device *slave)
1975{
1976        bonding_t *bond;
1977        slave_t *our_slave, *old_current;
1978        struct sockaddr addr;
1979        
1980        if (master == NULL || slave == NULL)  {
1981                return -ENODEV;
1982        }
1983
1984        bond = (struct bonding *) master->priv;
1985
1986        /* master already enslaved, or slave not enslaved,
1987           or no slave for this master */
1988        if ((master->flags & IFF_SLAVE) || !(slave->flags & IFF_SLAVE)) {
1989                printk (KERN_DEBUG "%s: cannot release %s.\n", master->name, slave->name);
1990                return -EINVAL;
1991        }
1992
1993        write_lock_bh(&bond->lock);
1994        bond->current_arp_slave = NULL;
1995        our_slave = (slave_t *)bond;
1996        old_current = bond->current_slave;
1997        while ((our_slave = our_slave->prev) != (slave_t *)bond) {
1998                if (our_slave->dev == slave) {
1999                        int mac_addr_differ = memcmp(bond->device->dev_addr,
2000                                                 our_slave->perm_hwaddr,
2001                                                 ETH_ALEN);
2002                        if (!mac_addr_differ && (bond->slave_cnt > 1)) {
2003                                printk(KERN_WARNING "WARNING: the permanent HWaddr of %s "
2004                                "- %02X:%02X:%02X:%02X:%02X:%02X - "
2005                                "is still in use by %s. Set the HWaddr "
2006                                "of %s to a different address "
2007                                "to avoid conflicts.\n",
2008                                       slave->name,
2009                                       our_slave->perm_hwaddr[0],
2010                                       our_slave->perm_hwaddr[1],
2011                                       our_slave->perm_hwaddr[2],
2012                                       our_slave->perm_hwaddr[3],
2013                                       our_slave->perm_hwaddr[4],
2014                                       our_slave->perm_hwaddr[5],
2015                                       bond->device->name,
2016                                       slave->name);
2017                        }
2018
2019                        /* Inform AD package of unbinding of slave. */
2020                        if (bond_mode == BOND_MODE_8023AD) {
2021                                /* must be called before the slave is
2022                                 * detached from the list
2023                                 */
2024                                bond_3ad_unbind_slave(our_slave);
2025                        }
2026
2027                        printk (KERN_INFO "%s: releasing %s interface %s\n",
2028                                master->name,
2029                                (our_slave->state == BOND_STATE_ACTIVE) ? "active" : "backup",
2030                                slave->name);
2031
2032                        /* release the slave from its bond */
2033                        bond_detach_slave(bond, our_slave);
2034
2035                        if (bond->primary_slave == our_slave) {
2036                                bond->primary_slave = NULL;
2037                        }
2038
2039                        if (bond->current_slave == our_slave) {
2040                                change_active_interface(bond, NULL);
2041                                reselect_active_interface(bond);
2042                        }
2043
2044                        if (bond->current_slave == NULL) {
2045                                printk(KERN_INFO
2046                                        "%s: now running without any active interface !\n",
2047                                        master->name);
2048                        }
2049
2050                        if ((bond_mode == BOND_MODE_TLB) ||
2051                            (bond_mode == BOND_MODE_ALB)) {
2052                                /* must be called only after the slave has been
2053                                 * detached from the list and the current_slave
2054                                 * has been replaced (if our_slave == old_current)
2055                                 */
2056                                bond_alb_deinit_slave(bond, our_slave);
2057                        }
2058
2059                        break;
2060                }
2061
2062        }
2063        write_unlock_bh(&bond->lock);
2064        
2065        if (our_slave == (slave_t *)bond) {
2066                /* if we get here, it's because the device was not found */
2067                printk (KERN_INFO "%s: %s not enslaved\n", master->name, slave->name);
2068                return -EINVAL;
2069        }
2070
2071        /* unset promiscuity level from slave */
2072        if (master->flags & IFF_PROMISC) {
2073                /* If the mode USES_PRIMARY, then we should only remove its
2074                 * promisc settings if it was the current_slave, but that was
2075                 * already taken care of above when we detached the slave
2076                 */
2077                if (!USES_PRIMARY(bond_mode)) {
2078                        dev_set_promiscuity(slave, -1); 
2079                }
2080        }
2081
2082        /* undo settings and restore original values */
2083        if (multicast_mode == BOND_MULTICAST_ALL) {
2084                /* flush master's mc_list from slave */ 
2085                bond_mc_list_flush (slave, master); 
2086
2087                /* unset allmulti level from slave */ 
2088                if (master->flags & IFF_ALLMULTI)
2089                        dev_set_allmulti(slave, -1); 
2090        }
2091
2092        netdev_set_master(slave, NULL);
2093
2094        /* close slave before restoring its mac address */
2095        dev_close(slave);
2096
2097        if (app_abi_ver >= 1) {
2098                /* restore original ("permanent") mac address */
2099                memcpy(addr.sa_data, our_slave->perm_hwaddr, ETH_ALEN);
2100                addr.sa_family = slave->type;
2101                slave->set_mac_address(slave, &addr);
2102        }
2103
2104        /* restore the original state of the
2105         * IFF_NOARP flag that might have been
2106         * set by bond_set_slave_inactive_flags()
2107         */
2108        if ((our_slave->original_flags & IFF_NOARP) == 0) {
2109                slave->flags &= ~IFF_NOARP;
2110        }
2111
2112        kfree(our_slave);
2113
2114        /* if the last slave was removed, zero the mac address
2115         * of the master so it will be set by the application
2116         * to the mac address of the first slave
2117         */
2118        if (bond->next == (slave_t*)bond) {
2119                memset(master->dev_addr, 0, master->addr_len);
2120        }
2121
2122        return 0;  /* deletion OK */
2123}
2124
2125/* 
2126 * This function releases all slaves.
2127 */
2128static int bond_release_all(struct net_device *master)
2129{
2130        bonding_t *bond;
2131        slave_t *our_slave, *old_current;
2132        struct net_device *slave_dev;
2133        struct sockaddr addr;
2134        int err = 0;
2135
2136        if (master == NULL)  {
2137                return -ENODEV;
2138        }
2139
2140        if (master->flags & IFF_SLAVE) {
2141                return -EINVAL;
2142        }
2143
2144        bond = (struct bonding *) master->priv;
2145
2146        write_lock_bh(&bond->lock);
2147        if (bond->next == (struct slave *) bond) {
2148                err = -EINVAL;
2149                goto out;
2150        }
2151
2152        old_current = bond->current_slave;
2153        change_active_interface(bond, NULL);
2154        bond->current_arp_slave = NULL;
2155        bond->primary_slave = NULL;
2156
2157        while ((our_slave = bond->prev) != (slave_t *)bond) {
2158                /* Inform AD package of unbinding of slave
2159                 * before slave is detached from the list.
2160                 */
2161                if (bond_mode == BOND_MODE_8023AD) {
2162                        bond_3ad_unbind_slave(our_slave);
2163                }
2164
2165                slave_dev = our_slave->dev;
2166                bond_detach_slave(bond, our_slave);
2167
2168                if ((bond_mode == BOND_MODE_TLB) ||
2169                    (bond_mode == BOND_MODE_ALB)) {
2170                        /* must be called only after the slave
2171                         * has been detached from the list
2172                         */
2173                        bond_alb_deinit_slave(bond, our_slave);
2174                }
2175
2176                /* now that the slave is detached, unlock and perform
2177                 * all the undo steps that should not be called from
2178                 * within a lock.
2179                 */
2180                write_unlock_bh(&bond->lock);
2181
2182                /* unset promiscuity level from slave */
2183                if (master->flags & IFF_PROMISC) {
2184                        if (!USES_PRIMARY(bond_mode)) {
2185                                dev_set_promiscuity(slave_dev, -1); 
2186                        }
2187                }
2188
2189                if (multicast_mode == BOND_MULTICAST_ALL) {
2190                        /* flush master's mc_list from slave */ 
2191                        bond_mc_list_flush (slave_dev, master); 
2192
2193                        /* unset allmulti level from slave */ 
2194                        if (master->flags & IFF_ALLMULTI)
2195                                dev_set_allmulti(slave_dev, -1); 
2196                }
2197
2198                netdev_set_master(slave_dev, NULL);
2199
2200                /* close slave before restoring its mac address */
2201                dev_close(slave_dev);
2202
2203                if (app_abi_ver >= 1) {
2204                        /* restore original ("permanent") mac address*/
2205                        memcpy(addr.sa_data, our_slave->perm_hwaddr, ETH_ALEN);
2206                        addr.sa_family = slave_dev->type;
2207                        slave_dev->set_mac_address(slave_dev, &addr);
2208                }
2209
2210                /* restore the original state of the IFF_NOARP flag that might have
2211                 * been set by bond_set_slave_inactive_flags()
2212                 */
2213                if ((our_slave->original_flags & IFF_NOARP) == 0) {
2214                        slave_dev->flags &= ~IFF_NOARP;
2215                }
2216
2217                kfree(our_slave);
2218
2219                /* re-acquire the lock before getting the next slave */
2220                write_lock_bh(&bond->lock);
2221        }
2222
2223        /* zero the mac address of the master so it will be
2224         * set by the application to the mac address of the
2225         * first slave
2226         */
2227        memset(master->dev_addr, 0, master->addr_len);
2228
2229        printk (KERN_INFO "%s: released all slaves\n", master->name);
2230
2231out:
2232        write_unlock_bh(&bond->lock);
2233
2234        return err;
2235}
2236
2237/* this function is called regularly to monitor each slave's link. */
2238static void bond_mii_monitor(struct net_device *master)
2239{
2240        bonding_t *bond = (struct bonding *) master->priv;
2241        slave_t *slave, *oldcurrent;
2242        int slave_died = 0;
2243        int do_failover = 0;
2244
2245        read_lock(&bond->lock);
2246
2247        /* we will try to read the link status of each of our slaves, and
2248         * set their IFF_RUNNING flag appropriately. For each slave not
2249         * supporting MII status, we won't do anything so that a user-space
2250         * program could monitor the link itself if needed.
2251         */
2252
2253        slave = (slave_t *)bond;
2254
2255        read_lock(&bond->ptrlock);
2256        oldcurrent = bond->current_slave;
2257        read_unlock(&bond->ptrlock);
2258
2259        while ((slave = slave->prev) != (slave_t *)bond) {
2260                struct net_device *dev = slave->dev;
2261                int link_state;
2262                u16 old_speed = slave->speed;
2263                u8 old_duplex = slave->duplex;
2264                
2265                link_state = bond_check_dev_link(dev, 0);
2266
2267                switch (slave->link) {
2268                case BOND_LINK_UP:      /* the link was up */
2269                        if (link_state == BMSR_LSTATUS) {
2270                                /* link stays up, nothing more to do */
2271                                break;
2272                        }
2273                        else { /* link going down */
2274                                slave->link  = BOND_LINK_FAIL;
2275                                slave->delay = downdelay;
2276                                if (slave->link_failure_count < UINT_MAX) {
2277                                        slave->link_failure_count++;
2278                                }
2279                                if (downdelay > 0) {
2280                                        printk (KERN_INFO
2281                                                "%s: link status down for %sinterface "
2282                                                "%s, disabling it in %d ms.\n",
2283                                                master->name,
2284                                                IS_UP(dev)
2285                                                ? ((bond_mode == BOND_MODE_ACTIVEBACKUP)
2286                                                   ? ((slave == oldcurrent)
2287                                                      ? "active " : "backup ")
2288                                                   : "")
2289                                                : "idle ",
2290                                                dev->name,
2291                                                downdelay * miimon);
2292                                        }
2293                        }
2294                        /* no break ! fall through the BOND_LINK_FAIL test to
2295                           ensure proper action to be taken
2296                        */
2297                case BOND_LINK_FAIL:    /* the link has just gone down */
2298                        if (link_state != BMSR_LSTATUS) {
2299                                /* link stays down */
2300                                if (slave->delay <= 0) {
2301                                        /* link down for too long time */
2302                                        slave->link = BOND_LINK_DOWN;
2303                                        /* in active/backup mode, we must
2304                                         * completely disable this interface
2305                                         */
2306                                        if ((bond_mode == BOND_MODE_ACTIVEBACKUP) ||
2307                                            (bond_mode == BOND_MODE_8023AD)) {
2308                                                bond_set_slave_inactive_flags(slave);
2309                                        }
2310
2311                                        printk(KERN_INFO
2312                                                "%s: link status definitely down "
2313                                                "for interface %s, disabling it",
2314                                                master->name,
2315                                                dev->name);
2316
2317                                        /* notify ad that the link status has changed */
2318                                        if (bond_mode == BOND_MODE_8023AD) {
2319                                                bond_3ad_handle_link_change(slave, BOND_LINK_DOWN);
2320                                        }
2321
2322                                        if ((bond_mode == BOND_MODE_TLB) ||
2323                                            (bond_mode == BOND_MODE_ALB)) {
2324                                                bond_alb_handle_link_change(bond, slave, BOND_LINK_DOWN);
2325                                        }
2326
2327                                        if (slave == oldcurrent) {
2328                                                do_failover = 1;
2329                                        }
2330
2331                                        slave_died = 1;
2332                                } else {
2333                                        slave->delay--;
2334                                }
2335                        } else {
2336                                /* link up again */
2337                                slave->link  = BOND_LINK_UP;
2338                                slave->jiffies = jiffies;
2339                                printk(KERN_INFO
2340                                        "%s: link status up again after %d ms "
2341                                        "for interface %s.\n",
2342                                        master->name,
2343                                        (downdelay - slave->delay) * miimon,
2344                                        dev->name);
2345                        }
2346                        break;
2347                case BOND_LINK_DOWN:    /* the link was down */
2348                        if (link_state != BMSR_LSTATUS) {
2349                                /* the link stays down, nothing more to do */
2350                                break;
2351                        } else {        /* link going up */
2352                                slave->link  = BOND_LINK_BACK;
2353                                slave->delay = updelay;
2354                                
2355                                if (updelay > 0) {
2356                                        /* if updelay == 0, no need to
2357                                           advertise about a 0 ms delay */
2358                                        printk (KERN_INFO
2359                                                "%s: link status up for interface"
2360                                                " %s, enabling it in %d ms.\n",
2361                                                master->name,
2362                                                dev->name,
2363                                                updelay * miimon);
2364                                }
2365                        }
2366                        /* no break ! fall through the BOND_LINK_BACK state in
2367                           case there's something to do.
2368                        */
2369                case BOND_LINK_BACK:    /* the link has just come back */
2370                        if (link_state != BMSR_LSTATUS) {
2371                                /* link down again */
2372                                slave->link  = BOND_LINK_DOWN;
2373                                printk(KERN_INFO
2374                                        "%s: link status down again after %d ms "
2375                                        "for interface %s.\n",
2376                                        master->name,
2377                                        (updelay - slave->delay) * miimon,
2378                                        dev->name);
2379                        } else {
2380                                /* link stays up */
2381                                if (slave->delay == 0) {
2382                                        /* now the link has been up for long time enough */
2383                                        slave->link = BOND_LINK_UP;
2384                                        slave->jiffies = jiffies;
2385
2386                                        if (bond_mode == BOND_MODE_8023AD) {
2387                                                /* prevent it from being the active one */
2388                                                slave->state = BOND_STATE_BACKUP;
2389                                        }
2390                                        else if (bond_mode != BOND_MODE_ACTIVEBACKUP) {
2391                                                /* make it immediately active */
2392                                                slave->state = BOND_STATE_ACTIVE;
2393                                        } else if (slave != bond->primary_slave) {
2394                                                /* prevent it from being the active one */
2395                                                slave->state = BOND_STATE_BACKUP;
2396                                        }
2397
2398                                        printk(KERN_INFO
2399                                                "%s: link status definitely up "
2400                                                "for interface %s.\n",
2401                                                master->name,
2402                                                dev->name);
2403        
2404                                        /* notify ad that the link status has changed */
2405                                        if (bond_mode == BOND_MODE_8023AD) {
2406                                                bond_3ad_handle_link_change(slave, BOND_LINK_UP);
2407                                        }
2408
2409                                        if ((bond_mode == BOND_MODE_TLB) ||
2410                                            (bond_mode == BOND_MODE_ALB)) {
2411                                                bond_alb_handle_link_change(bond, slave, BOND_LINK_UP);
2412                                        }
2413
2414                                        if ((oldcurrent == NULL) ||
2415                                            (slave == bond->primary_slave)) {
2416                                                do_failover = 1;
2417                                        }
2418                                } else {
2419                                        slave->delay--;
2420                                }
2421                        }
2422                        break;
2423                } /* end of switch */
2424
2425                bond_update_speed_duplex(slave);
2426
2427                if (bond_mode == BOND_MODE_8023AD) {
2428                        if (old_speed != slave->speed) {
2429                                bond_3ad_adapter_speed_changed(slave);
2430                        }
2431                        if (old_duplex != slave->duplex) {
2432                                bond_3ad_adapter_duplex_changed(slave);
2433                        }
2434                }
2435
2436        } /* end of while */
2437
2438        if (do_failover) {
2439                write_lock(&bond->ptrlock);
2440
2441                reselect_active_interface(bond);
2442                if (oldcurrent && !bond->current_slave) {
2443                        printk(KERN_INFO
2444                                "%s: now running without any active interface !\n",
2445                                master->name);
2446                }
2447
2448                write_unlock(&bond->ptrlock);
2449        }
2450
2451        read_unlock(&bond->lock);
2452        /* re-arm the timer */
2453        mod_timer(&bond->mii_timer, jiffies + (miimon * HZ / 1000));
2454}
2455
2456/* 
2457 * this function is called regularly to monitor each slave's link 
2458 * ensuring that traffic is being sent and received when arp monitoring
2459 * is used in load-balancing mode. if the adapter has been dormant, then an 
2460 * arp is transmitted to generate traffic. see activebackup_arp_monitor for 
2461 * arp monitoring in active backup mode. 
2462 */
2463static void loadbalance_arp_monitor(struct net_device *master)
2464{
2465        bonding_t *bond;
2466        slave_t *slave, *oldcurrent;
2467        int the_delta_in_ticks =  arp_interval * HZ / 1000;
2468        int next_timer = jiffies + (arp_interval * HZ / 1000);
2469        int do_failover = 0;
2470
2471        bond = (struct bonding *) master->priv; 
2472        if (master->priv == NULL) {
2473                mod_timer(&bond->arp_timer, next_timer);
2474                return;
2475        }
2476
2477        /* TODO: investigate why rtnl_shlock_nowait and rtnl_exlock_nowait
2478         * are called below and add comment why they are required... 
2479         */
2480        if ((!IS_UP(master)) || rtnl_shlock_nowait()) {
2481                mod_timer(&bond->arp_timer, next_timer);
2482                return;
2483        }
2484
2485        if (rtnl_exlock_nowait()) {
2486                rtnl_shunlock();
2487                mod_timer(&bond->arp_timer, next_timer);
2488                return;
2489        }
2490
2491        read_lock(&bond->lock);
2492
2493        read_lock(&bond->ptrlock);
2494        oldcurrent = bond->current_slave;
2495        read_unlock(&bond->ptrlock);
2496
2497        /* see if any of the previous devices are up now (i.e. they have
2498         * xmt and rcv traffic). the current_slave does not come into
2499         * the picture unless it is null. also, slave->jiffies is not needed
2500         * here because we send an arp on each slave and give a slave as
2501         * long as it needs to get the tx/rx within the delta.
2502         * TODO: what about up/down delay in arp mode? it wasn't here before
2503         *       so it can wait 
2504         */
2505        slave = (slave_t *)bond;
2506        while ((slave = slave->prev) != (slave_t *)bond)  {
2507
2508                if (slave->link != BOND_LINK_UP) {
2509
2510                        if (((jiffies - slave->dev->trans_start) <= 
2511                                                the_delta_in_ticks) &&  
2512                             ((jiffies - slave->dev->last_rx) <= 
2513                                                the_delta_in_ticks)) {
2514
2515                                slave->link  = BOND_LINK_UP;
2516                                slave->state = BOND_STATE_ACTIVE;
2517
2518                                /* primary_slave has no meaning in round-robin
2519                                 * mode. the window of a slave being up and 
2520                                 * current_slave being null after enslaving
2521                                 * is closed.
2522                                 */
2523                                if (oldcurrent == NULL) {
2524                                        printk(KERN_INFO
2525                                                "%s: link status definitely up "
2526                                                "for interface %s, ",
2527                                                master->name,
2528                                                slave->dev->name);
2529                                        do_failover = 1;
2530                                } else {
2531                                        printk(KERN_INFO
2532                                                "%s: interface %s is now up\n",
2533                                                master->name,
2534                                                slave->dev->name);
2535                                }
2536                        } 
2537                } else {
2538                        /* slave->link == BOND_LINK_UP */
2539
2540                        /* not all switches will respond to an arp request
2541                         * when the source ip is 0, so don't take the link down
2542                         * if we don't know our ip yet
2543                         */
2544                        if (((jiffies - slave->dev->trans_start) >= 
2545                              (2*the_delta_in_ticks)) ||
2546                             (((jiffies - slave->dev->last_rx) >= 
2547                               (2*the_delta_in_ticks)) && my_ip !=0)) {
2548                                slave->link  = BOND_LINK_DOWN;
2549                                slave->state = BOND_STATE_BACKUP;
2550                                if (slave->link_failure_count < UINT_MAX) {
2551                                        slave->link_failure_count++;
2552                                }
2553                                printk(KERN_INFO
2554                                       "%s: interface %s is now down.\n",
2555                                       master->name,
2556                                       slave->dev->name);
2557
2558                                if (slave == oldcurrent) {
2559                                        do_failover = 1;
2560                                }
2561                        }
2562                } 
2563
2564                /* note: if switch is in round-robin mode, all links 
2565                 * must tx arp to ensure all links rx an arp - otherwise
2566                 * links may oscillate or not come up at all; if switch is 
2567                 * in something like xor mode, there is nothing we can 
2568                 * do - all replies will be rx'ed on same link causing slaves 
2569                 * to be unstable during low/no traffic periods
2570                 */
2571                if (IS_UP(slave->dev)) {
2572                        arp_send_all(slave);
2573                }
2574        }
2575
2576        if (do_failover) {
2577                write_lock(&bond->ptrlock);
2578
2579                reselect_active_interface(bond);
2580                if (oldcurrent && !bond->current_slave) {
2581                        printk(KERN_INFO
2582                                "%s: now running without any active interface !\n",
2583                                master->name);
2584                }
2585
2586                write_unlock(&bond->ptrlock);
2587        }
2588
2589        read_unlock(&bond->lock);
2590        rtnl_exunlock();
2591        rtnl_shunlock();
2592
2593        /* re-arm the timer */
2594        mod_timer(&bond->arp_timer, next_timer);
2595}
2596
2597/* 
2598 * When using arp monitoring in active-backup mode, this function is
2599 * called to determine if any backup slaves have went down or a new
2600 * current slave needs to be found.
2601 * The backup slaves never generate traffic, they are considered up by merely 
2602 * receiving traffic. If the current slave goes down, each backup slave will 
2603 * be given the opportunity to tx/rx an arp before being taken down - this 
2604 * prevents all slaves from being taken down due to the current slave not 
2605 * sending any traffic for the backups to receive. The arps are not necessarily
2606 * necessary, any tx and rx traffic will keep the current slave up. While any 
2607 * rx traffic will keep the backup slaves up, the current slave is responsible 
2608 * for generating traffic to keep them up regardless of any other traffic they 
2609 * may have received.
2610 * see loadbalance_arp_monitor for arp monitoring in load balancing mode
2611 */
2612static void activebackup_arp_monitor(struct net_device *master)
2613{
2614        bonding_t *bond;
2615        slave_t *slave;
2616        int the_delta_in_ticks =  arp_interval * HZ / 1000;
2617        int next_timer = jiffies + (arp_interval * HZ / 1000);
2618
2619        bond = (struct bonding *) master->priv; 
2620        if (master->priv == NULL) {
2621                mod_timer(&bond->arp_timer, next_timer);
2622                return;
2623        }
2624
2625        if (!IS_UP(master)) {
2626                mod_timer(&bond->arp_timer, next_timer);
2627                return;
2628        }
2629
2630        read_lock(&bond->lock);
2631
2632        /* determine if any slave has come up or any backup slave has 
2633         * gone down 
2634         * TODO: what about up/down delay in arp mode? it wasn't here before
2635         *       so it can wait 
2636         */
2637        slave = (slave_t *)bond;
2638        while ((slave = slave->prev) != (slave_t *)bond)  {
2639
2640                if (slave->link != BOND_LINK_UP) {
2641                        if ((jiffies - slave->dev->last_rx) <=
2642                            the_delta_in_ticks) {
2643
2644                                slave->link = BOND_LINK_UP;
2645                                write_lock(&bond->ptrlock);
2646                                if ((bond->current_slave == NULL) &&
2647                                    ((jiffies - slave->dev->trans_start) <=
2648                                     the_delta_in_ticks)) {
2649                                        change_active_interface(bond, slave);
2650                                        bond->current_arp_slave = NULL;
2651                                } else if (bond->current_slave != slave) {
2652                                        /* this slave has just come up but we 
2653                                         * already have a current slave; this
2654                                         * can also happen if bond_enslave adds
2655                                         * a new slave that is up while we are 
2656                                         * searching for a new slave
2657                                         */
2658                                        bond_set_slave_inactive_flags(slave);
2659                                        bond->current_arp_slave = NULL;
2660                                }
2661
2662                                if (slave == bond->current_slave) {
2663                                        printk(KERN_INFO
2664                                                "%s: %s is up and now the "
2665                                                "active interface\n",
2666                                                master->name,
2667                                                slave->dev->name);
2668                                } else {
2669                                        printk(KERN_INFO
2670                                                "%s: backup interface %s is "
2671                                                "now up\n",
2672                                                master->name,
2673                                                slave->dev->name);
2674                                }
2675
2676                                write_unlock(&bond->ptrlock);
2677                        }
2678                } else {
2679                        read_lock(&bond->ptrlock);
2680                        if ((slave != bond->current_slave) &&
2681                            (bond->current_arp_slave == NULL) &&
2682                            (((jiffies - slave->dev->last_rx) >=
2683                             3*the_delta_in_ticks) && (my_ip != 0))) {
2684                                /* a backup slave has gone down; three times 
2685                                 * the delta allows the current slave to be 
2686                                 * taken out before the backup slave.
2687                                 * note: a non-null current_arp_slave indicates
2688                                 * the current_slave went down and we are 
2689                                 * searching for a new one; under this 
2690                                 * condition we only take the current_slave 
2691                                 * down - this gives each slave a chance to 
2692                                 * tx/rx traffic before being taken out
2693                                 */
2694                                read_unlock(&bond->ptrlock);
2695                                slave->link  = BOND_LINK_DOWN;
2696                                if (slave->link_failure_count < UINT_MAX) {
2697                                        slave->link_failure_count++;
2698                                }
2699                                bond_set_slave_inactive_flags(slave);
2700                                printk(KERN_INFO
2701                                        "%s: backup interface %s is now down\n",
2702                                        master->name,
2703                                        slave->dev->name);
2704                        } else {
2705                                read_unlock(&bond->ptrlock);
2706                        }
2707                }
2708        }
2709
2710        read_lock(&bond->ptrlock);
2711        slave = bond->current_slave;
2712        read_unlock(&bond->ptrlock);
2713
2714        if (slave != NULL) {
2715
2716                /* if we have sent traffic in the past 2*arp_intervals but
2717                 * haven't xmit and rx traffic in that time interval, select 
2718                 * a different slave. slave->jiffies is only updated when
2719                 * a slave first becomes the current_slave - not necessarily
2720                 * after every arp; this ensures the slave has a full 2*delta 
2721                 * before being taken out. if a primary is being used, check 
2722                 * if it is up and needs to take over as the current_slave
2723                 */
2724                if ((((jiffies - slave->dev->trans_start) >= 
2725                       (2*the_delta_in_ticks)) ||
2726                     (((jiffies - slave->dev->last_rx) >= 
2727                       (2*the_delta_in_ticks)) && (my_ip != 0))) &&
2728                    ((jiffies - slave->jiffies) >= 2*the_delta_in_ticks)) {
2729
2730                        slave->link  = BOND_LINK_DOWN;
2731                        if (slave->link_failure_count < UINT_MAX) {
2732                                slave->link_failure_count++;
2733                        }
2734                        printk(KERN_INFO "%s: link status down for "
2735                                         "active interface %s, disabling it",
2736                               master->name,
2737                               slave->dev->name);
2738                        write_lock(&bond->ptrlock);
2739                        reselect_active_interface(bond);
2740                        slave = bond->current_slave;
2741                        write_unlock(&bond->ptrlock);
2742                        bond->current_arp_slave = slave;
2743                        if (slave != NULL) {
2744                                slave->jiffies = jiffies;
2745                        }
2746
2747                } else if ((bond->primary_slave != NULL) && 
2748                           (bond->primary_slave != slave) && 
2749                           (bond->primary_slave->link == BOND_LINK_UP)) {
2750                        /* at this point, slave is the current_slave */
2751                        printk(KERN_INFO 
2752                               "%s: changing from interface %s to primary "
2753                               "interface %s\n",
2754                               master->name, 
2755                               slave->dev->name, 
2756                               bond->primary_slave->dev->name);
2757                               
2758                        /* primary is up so switch to it */
2759                        write_lock(&bond->ptrlock);
2760                        change_active_interface(bond, bond->primary_slave);
2761                        write_unlock(&bond->ptrlock);
2762                        slave = bond->primary_slave;
2763                        slave->jiffies = jiffies;
2764                } else {
2765                        bond->current_arp_slave = NULL;
2766                }
2767
2768                /* the current slave must tx an arp to ensure backup slaves
2769                 * rx traffic
2770                 */
2771                if ((slave != NULL) && (my_ip != 0)) {
2772                        arp_send_all(slave);
2773                }
2774        }
2775
2776        /* if we don't have a current_slave, search for the next available 
2777         * backup slave from the current_arp_slave and make it the candidate 
2778         * for becoming the current_slave
2779         */
2780        if (slave == NULL) { 
2781
2782                if ((bond->current_arp_slave == NULL) ||
2783                    (bond->current_arp_slave == (slave_t *)bond)) {
2784                        bond->current_arp_slave = bond->prev;
2785                } 
2786
2787                if (bond->current_arp_slave != (slave_t *)bond) {
2788                        bond_set_slave_inactive_flags(bond->current_arp_slave);
2789                        slave = bond->current_arp_slave->next;
2790
2791                        /* search for next candidate */
2792                        do {
2793                                if (IS_UP(slave->dev)) {
2794                                        slave->link = BOND_LINK_BACK;
2795                                        bond_set_slave_active_flags(slave);
2796                                        arp_send_all(slave);
2797                                        slave->jiffies = jiffies;
2798                                        bond->current_arp_slave = slave;
2799                                        break;
2800                                }
2801
2802                                /* if the link state is up at this point, we 
2803                                 * mark it down - this can happen if we have 
2804                                 * simultaneous link failures and 
2805                                 * reselect_active_interface doesn't make this 
2806                                 * one the current slave so it is still marked 
2807                                 * up when it is actually down
2808                                 */
2809                                if (slave->link == BOND_LINK_UP) {
2810                                        slave->link  = BOND_LINK_DOWN;
2811                                        if (slave->link_failure_count < 
2812                                                        UINT_MAX) {
2813                                                slave->link_failure_count++;
2814                                        }
2815
2816                                        bond_set_slave_inactive_flags(slave);
2817                                        printk(KERN_INFO
2818                                                "%s: backup interface "
2819                                                "%s is now down.\n",
2820                                                master->name,
2821                                                slave->dev->name);
2822                                }
2823                        } while ((slave = slave->next) != 
2824                                        bond->current_arp_slave->next);
2825                }
2826        }
2827
2828        read_unlock(&bond->lock);
2829        mod_timer(&bond->arp_timer, next_timer);
2830}
2831
2832static int bond_sethwaddr(struct net_device *master, struct net_device *slave)
2833{
2834#ifdef BONDING_DEBUG
2835        printk(KERN_CRIT "bond_sethwaddr: master=%x\n", (unsigned int)master);
2836        printk(KERN_CRIT "bond_sethwaddr: slave=%x\n", (unsigned int)slave);
2837        printk(KERN_CRIT "bond_sethwaddr: slave->addr_len=%d\n", slave->addr_len);
2838#endif
2839        memcpy(master->dev_addr, slave->dev_addr, slave->addr_len);
2840        return 0;
2841}
2842
2843static int bond_info_query(struct net_device *master, struct ifbond *info)
2844{
2845        bonding_t *bond = (struct bonding *) master->priv;
2846        slave_t *slave;
2847
2848        info->bond_mode = bond_mode;
2849        info->num_slaves = 0;
2850        info->miimon = miimon;
2851
2852        read_lock_bh(&bond->lock);
2853        for (slave = bond->prev; slave != (slave_t *)bond; slave = slave->prev) {
2854                info->num_slaves++;
2855        }
2856        read_unlock_bh(&bond->lock);
2857
2858        return 0;
2859}
2860
2861static int bond_slave_info_query(struct net_device *master, 
2862                                        struct ifslave *info)
2863{
2864        bonding_t *bond = (struct bonding *) master->priv;
2865        slave_t *slave;
2866        int cur_ndx = 0;
2867
2868        if (info->slave_id < 0) {
2869                return -ENODEV;
2870        }
2871
2872        read_lock_bh(&bond->lock);
2873        for (slave = bond->prev; 
2874                 slave != (slave_t *)bond && cur_ndx < info->slave_id; 
2875                 slave = slave->prev) {
2876                cur_ndx++;
2877        }
2878        read_unlock_bh(&bond->lock);
2879
2880        if (slave != (slave_t *)bond) {
2881                strcpy(info->slave_name, slave->dev->name);
2882                info->link = slave->link;
2883                info->state = slave->state;
2884                info->link_failure_count = slave->link_failure_count;
2885        } else {
2886                return -ENODEV;
2887        }
2888
2889        return 0;
2890}
2891
2892static int bond_ethtool_ioctl(struct net_device *master_dev, struct ifreq *ifr)
2893{
2894        void *addr = ifr->ifr_data;
2895        uint32_t cmd;
2896
2897        if (get_user(cmd, (uint32_t *) addr))
2898                return -EFAULT;
2899
2900        switch (cmd) {
2901
2902        case ETHTOOL_GDRVINFO:
2903                {
2904                        struct ethtool_drvinfo info;
2905                        char *endptr;
2906
2907                        if (copy_from_user(&info, addr, sizeof(info)))
2908                                return -EFAULT;
2909
2910                        if (strcmp(info.driver, "ifenslave") == 0) {
2911                                int new_abi_ver;
2912
2913                                new_abi_ver = simple_strtoul(info.fw_version,
2914                                                             &endptr, 0);
2915                                if (*endptr) {
2916                                        printk(KERN_ERR
2917                                               "bonding: Error: got invalid ABI"
2918                                               " version from application\n");
2919
2920                                        return -EINVAL;
2921                                }
2922
2923                                if (orig_app_abi_ver == -1) {
2924                                        orig_app_abi_ver  = new_abi_ver;
2925                                }
2926
2927                                app_abi_ver = new_abi_ver;
2928                        }
2929
2930                        strncpy(info.driver,  DRV_NAME, 32);
2931                        strncpy(info.version, DRV_VERSION, 32);
2932                        snprintf(info.fw_version, 32, "%d", BOND_ABI_VERSION);
2933
2934                        if (copy_to_user(addr, &info, sizeof(info)))
2935                                return -EFAULT;
2936
2937                        return 0;
2938                }
2939                break;
2940        default:
2941                return -EOPNOTSUPP;
2942        }
2943}
2944
2945static int bond_ioctl(struct net_device *master_dev, struct ifreq *ifr, int cmd)
2946{
2947        struct net_device *slave_dev = NULL;
2948        struct ifbond *u_binfo = NULL, k_binfo;
2949        struct ifslave *u_sinfo = NULL, k_sinfo;
2950        struct mii_ioctl_data *mii = NULL;
2951        int prev_abi_ver = orig_app_abi_ver;
2952        int ret = 0;
2953
2954#ifdef BONDING_DEBUG
2955        printk(KERN_INFO "bond_ioctl: master=%s, cmd=%d\n", 
2956                master_dev->name, cmd);
2957#endif
2958
2959        switch (cmd) {
2960        case SIOCETHTOOL:
2961                return bond_ethtool_ioctl(master_dev, ifr);
2962
2963        case SIOCGMIIPHY:
2964                mii = (struct mii_ioctl_data *)&ifr->ifr_data;
2965                if (mii == NULL) {
2966                        return -EINVAL;
2967                }
2968                mii->phy_id = 0;
2969                /* Fall Through */
2970        case SIOCGMIIREG:
2971                /* 
2972                 * We do this again just in case we were called by SIOCGMIIREG
2973                 * instead of SIOCGMIIPHY.
2974                 */
2975                mii = (struct mii_ioctl_data *)&ifr->ifr_data;
2976                if (mii == NULL) {
2977                        return -EINVAL;
2978                }
2979                if (mii->reg_num == 1) {
2980                        mii->val_out = bond_check_mii_link(
2981                                (struct bonding *)master_dev->priv);
2982                }
2983                return 0;
2984        case BOND_INFO_QUERY_OLD:
2985        case SIOCBONDINFOQUERY:
2986                u_binfo = (struct ifbond *)ifr->ifr_data;
2987                if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond))) {
2988                        return -EFAULT;
2989                }
2990                ret = bond_info_query(master_dev, &k_binfo);
2991                if (ret == 0) {
2992                        if (copy_to_user(u_binfo, &k_binfo, sizeof(ifbond))) {
2993                                return -EFAULT;
2994                        }
2995                }
2996                return ret;
2997        case BOND_SLAVE_INFO_QUERY_OLD:
2998        case SIOCBONDSLAVEINFOQUERY:
2999                u_sinfo = (struct ifslave *)ifr->ifr_data;
3000                if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave))) {
3001                        return -EFAULT;
3002                }
3003                ret = bond_slave_info_query(master_dev, &k_sinfo);
3004                if (ret == 0) {
3005                        if (copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave))) {
3006                                return -EFAULT;
3007                        }
3008                }
3009                return ret;
3010        }
3011
3012        if (!capable(CAP_NET_ADMIN)) {
3013                return -EPERM;
3014        }
3015
3016        if (orig_app_abi_ver == -1) {
3017                /* no orig_app_abi_ver was provided yet, so we'll use the
3018                 * current one from now on, even if it's 0
3019                 */
3020                orig_app_abi_ver = app_abi_ver;
3021
3022        } else if (orig_app_abi_ver != app_abi_ver) {
3023                printk(KERN_ERR
3024                       "bonding: Error: already using ifenslave ABI "
3025                       "version %d; to upgrade ifenslave to version %d, "
3026                       "you must first reload bonding.\n",
3027                       orig_app_abi_ver, app_abi_ver);
3028                return -EINVAL;
3029        }
3030
3031        slave_dev = dev_get_by_name(ifr->ifr_slave);
3032
3033#ifdef BONDING_DEBUG
3034        printk(KERN_INFO "slave_dev=%x: \n", (unsigned int)slave_dev);
3035        printk(KERN_INFO "slave_dev->name=%s: \n", slave_dev->name);
3036#endif
3037
3038        if (slave_dev == NULL) {
3039                ret = -ENODEV;
3040        } else {
3041                switch (cmd) {
3042                case BOND_ENSLAVE_OLD:
3043                case SIOCBONDENSLAVE:           
3044                        ret = bond_enslave(master_dev, slave_dev);
3045                        break;
3046                case BOND_RELEASE_OLD:                  
3047                case SIOCBONDRELEASE:   
3048                        ret = bond_release(master_dev, slave_dev); 
3049                        break;
3050                case BOND_SETHWADDR_OLD:
3051                case SIOCBONDSETHWADDR:
3052                        ret = bond_sethwaddr(master_dev, slave_dev);
3053                        break;
3054                case BOND_CHANGE_ACTIVE_OLD:
3055                case SIOCBONDCHANGEACTIVE:
3056                        if (USES_PRIMARY(bond_mode)) {
3057                                ret = bond_change_active(master_dev, slave_dev);
3058                        }
3059                        else {
3060                                ret = -EINVAL;
3061                        }
3062                        break;
3063                default:
3064                        ret = -EOPNOTSUPP;
3065                }
3066                dev_put(slave_dev);
3067        }
3068
3069        if (ret < 0) {
3070                /* The ioctl failed, so there's no point in changing the
3071                 * orig_app_abi_ver. We'll restore it's value just in case
3072                 * we've changed it earlier in this function.
3073                 */
3074                orig_app_abi_ver = prev_abi_ver;
3075        }
3076
3077        return ret;
3078}
3079
3080#ifdef CONFIG_NET_FASTROUTE
3081static int bond_accept_fastpath(struct net_device *dev, struct dst_entry *dst)
3082{
3083        return -1;
3084}
3085#endif
3086
3087/* 
3088 * in broadcast mode, we send everything to all usable interfaces.
3089 */
3090static int bond_xmit_broadcast(struct sk_buff *skb, struct net_device *dev)
3091{
3092        slave_t *slave, *start_at;
3093        struct bonding *bond = (struct bonding *) dev->priv;
3094        struct net_device *device_we_should_send_to = 0;
3095
3096        if (!IS_UP(dev)) { /* bond down */
3097                dev_kfree_skb(skb);
3098                return 0;
3099        }
3100
3101        read_lock(&bond->lock);
3102
3103        read_lock(&bond->ptrlock);
3104        slave = start_at = bond->current_slave;
3105        read_unlock(&bond->ptrlock);
3106
3107        if (slave == NULL) { /* we're at the root, get the first slave */
3108                /* no suitable interface, frame not sent */
3109                read_unlock(&bond->lock);
3110                dev_kfree_skb(skb);
3111                return 0;
3112        }
3113
3114        do {
3115                if (IS_UP(slave->dev)
3116                    && (slave->link == BOND_LINK_UP)
3117                    && (slave->state == BOND_STATE_ACTIVE)) {
3118                        if (device_we_should_send_to) {
3119                                struct sk_buff *skb2;
3120                                if ((skb2 = skb_clone(skb, GFP_ATOMIC)) == NULL) {
3121                                        printk(KERN_ERR "bond_xmit_broadcast: skb_clone() failed\n");
3122                                        continue;
3123                                }
3124
3125                                skb2->dev = device_we_should_send_to;
3126                                skb2->priority = 1;
3127                                dev_queue_xmit(skb2);
3128                        }
3129                        device_we_should_send_to = slave->dev;
3130                }
3131        } while ((slave = slave->next) != start_at);
3132
3133        if (device_we_should_send_to) {
3134                skb->dev = device_we_should_send_to;
3135                skb->priority = 1;
3136                dev_queue_xmit(skb);
3137        } else
3138                dev_kfree_skb(skb);
3139
3140        /* frame sent to all suitable interfaces */
3141        read_unlock(&bond->lock);
3142        return 0;
3143}
3144
3145static int bond_xmit_roundrobin(struct sk_buff *skb, struct net_device *dev)
3146{
3147        slave_t *slave, *start_at;
3148        struct bonding *bond = (struct bonding *) dev->priv;
3149
3150        if (!IS_UP(dev)) { /* bond down */
3151                dev_kfree_skb(skb);
3152                return 0;
3153        }
3154
3155        read_lock(&bond->lock);
3156
3157        read_lock(&bond->ptrlock);
3158        slave = start_at = bond->current_slave;
3159        read_unlock(&bond->ptrlock);
3160
3161        if (slave == NULL) { /* we're at the root, get the first slave */
3162                /* no suitable interface, frame not sent */
3163                dev_kfree_skb(skb);
3164                read_unlock(&bond->lock);
3165                return 0;
3166        }
3167
3168        do {
3169                if (IS_UP(slave->dev)
3170                    && (slave->link == BOND_LINK_UP)
3171                    && (slave->state == BOND_STATE_ACTIVE)) {
3172
3173                        skb->dev = slave->dev;
3174                        skb->priority = 1;
3175                        dev_queue_xmit(skb);
3176
3177                        write_lock(&bond->ptrlock);
3178                        bond->current_slave = slave->next;
3179                        write_unlock(&bond->ptrlock);
3180
3181                        read_unlock(&bond->lock);
3182                        return 0;
3183                }
3184        } while ((slave = slave->next) != start_at);
3185
3186        /* no suitable interface, frame not sent */
3187        dev_kfree_skb(skb);
3188        read_unlock(&bond->lock);
3189        return 0;
3190}
3191
3192/* 
3193 * in XOR mode, we determine the output device by performing xor on
3194 * the source and destination hw adresses.  If this device is not 
3195 * enabled, find the next slave following this xor slave. 
3196 */
3197static int bond_xmit_xor(struct sk_buff *skb, struct net_device *dev)
3198{
3199        slave_t *slave, *start_at;
3200        struct bonding *bond = (struct bonding *) dev->priv;
3201        struct ethhdr *data = (struct ethhdr *)skb->data;
3202        int slave_no;
3203
3204        if (!IS_UP(dev)) { /* bond down */
3205                dev_kfree_skb(skb);
3206                return 0;
3207        }
3208
3209        read_lock(&bond->lock);
3210        slave = bond->prev;
3211
3212        /* we're at the root, get the first slave */
3213        if (bond->slave_cnt == 0) {
3214                /* no suitable interface, frame not sent */
3215                dev_kfree_skb(skb);
3216                read_unlock(&bond->lock);
3217                return 0;
3218        }
3219
3220        slave_no = (data->h_dest[5]^slave->dev->dev_addr[5]) % bond->slave_cnt;
3221
3222        while ( (slave_no > 0) && (slave != (slave_t *)bond) ) {
3223                slave = slave->prev;
3224                slave_no--;
3225        } 
3226        start_at = slave;
3227
3228        do {
3229                if (IS_UP(slave->dev)
3230                    && (slave->link == BOND_LINK_UP)
3231                    && (slave->state == BOND_STATE_ACTIVE)) {
3232
3233                        skb->dev = slave->dev;
3234                        skb->priority = 1;
3235                        dev_queue_xmit(skb);
3236
3237                        read_unlock(&bond->lock);
3238                        return 0;
3239                }
3240        } while ((slave = slave->next) != start_at);
3241
3242        /* no suitable interface, frame not sent */
3243        dev_kfree_skb(skb);
3244        read_unlock(&bond->lock);
3245        return 0;
3246}
3247
3248/* 
3249 * in active-backup mode, we know that bond->current_slave is always valid if
3250 * the bond has a usable interface.
3251 */
3252static int bond_xmit_activebackup(struct sk_buff *skb, struct net_device *dev)
3253{
3254        struct bonding *bond = (struct bonding *) dev->priv;
3255        int ret;
3256
3257        if (!IS_UP(dev)) { /* bond down */
3258                dev_kfree_skb(skb);
3259                return 0;
3260        }
3261
3262        /* if we are sending arp packets, try to at least 
3263           identify our own ip address */
3264        if ( (arp_interval > 0) && (my_ip == 0) &&
3265                (skb->protocol == __constant_htons(ETH_P_ARP) ) ) {
3266                char *the_ip = (((char *)skb->data)) 
3267                                + sizeof(struct ethhdr)  
3268                                + sizeof(struct arphdr) + 
3269                                ETH_ALEN;
3270                memcpy(&my_ip, the_ip, 4);
3271        }
3272
3273        /* if we are sending arp packets and don't know 
3274         * the target hw address, save it so we don't need 
3275         * to use a broadcast address.
3276         * don't do this if in active backup mode because the slaves must 
3277         * receive packets to stay up, and the only ones they receive are 
3278         * broadcasts. 
3279         */
3280        if ( (bond_mode != BOND_MODE_ACTIVEBACKUP) && 
3281             (arp_ip_count == 1) &&
3282             (arp_interval > 0) && (arp_target_hw_addr == NULL) &&
3283             (skb->protocol == __constant_htons(ETH_P_IP) ) ) {
3284                struct ethhdr *eth_hdr = 
3285                        (struct ethhdr *) (((char *)skb->data));
3286                struct iphdr *ip_hdr = (struct iphdr *)(eth_hdr + 1);
3287
3288                if (arp_target[0] == ip_hdr->daddr) {
3289                        arp_target_hw_addr = kmalloc(ETH_ALEN, GFP_KERNEL);
3290                        if (arp_target_hw_addr != NULL)
3291                                memcpy(arp_target_hw_addr, eth_hdr->h_dest, ETH_ALEN);
3292                }
3293        }
3294
3295        read_lock(&bond->lock);
3296
3297        read_lock(&bond->ptrlock);
3298        if (bond->current_slave != NULL) { /* one usable interface */
3299                skb->dev = bond->current_slave->dev;
3300                read_unlock(&bond->ptrlock);
3301                skb->priority = 1;
3302                ret = dev_queue_xmit(skb);
3303                read_unlock(&bond->lock);
3304                return 0;
3305        }
3306        else {
3307                read_unlock(&bond->ptrlock);
3308        }
3309
3310        /* no suitable interface, frame not sent */
3311#ifdef BONDING_DEBUG
3312        printk(KERN_INFO "There was no suitable interface, so we don't transmit\n");
3313#endif
3314        dev_kfree_skb(skb);
3315        read_unlock(&bond->lock);
3316        return 0;
3317}
3318
3319static struct net_device_stats *bond_get_stats(struct net_device *dev)
3320{
3321        bonding_t *bond = dev->priv;
3322        struct net_device_stats *stats = &(bond->stats), *sstats;
3323        slave_t *slave;
3324
3325        memset(stats, 0, sizeof(struct net_device_stats));
3326
3327        read_lock_bh(&bond->lock);
3328
3329        for (slave = bond->prev; slave != (slave_t *)bond; slave = slave->prev) {
3330                sstats = slave->dev->get_stats(slave->dev);
3331 
3332                stats->rx_packets += sstats->rx_packets;
3333                stats->rx_bytes += sstats->rx_bytes;
3334                stats->rx_errors += sstats->rx_errors;
3335                stats->rx_dropped += sstats->rx_dropped;
3336
3337                stats->tx_packets += sstats->tx_packets;
3338                stats->tx_bytes += sstats->tx_bytes;
3339                stats->tx_errors += sstats->tx_errors;
3340                stats->tx_dropped += sstats->tx_dropped;
3341
3342                stats->multicast += sstats->multicast;
3343                stats->collisions += sstats->collisions;
3344
3345                stats->rx_length_errors += sstats->rx_length_errors;
3346                stats->rx_over_errors += sstats->rx_over_errors;
3347                stats->rx_crc_errors += sstats->rx_crc_errors;
3348                stats->rx_frame_errors += sstats->rx_frame_errors;
3349                stats->rx_fifo_errors += sstats->rx_fifo_errors;        
3350                stats->rx_missed_errors += sstats->rx_missed_errors;
3351        
3352                stats->tx_aborted_errors += sstats->tx_aborted_errors;
3353                stats->tx_carrier_errors += sstats->tx_carrier_errors;
3354                stats->tx_fifo_errors += sstats->tx_fifo_errors;
3355                stats->tx_heartbeat_errors += sstats->tx_heartbeat_errors;
3356                stats->tx_window_errors += sstats->tx_window_errors;
3357
3358        }
3359
3360        read_unlock_bh(&bond->lock);
3361        return stats;
3362}
3363
3364#ifdef CONFIG_PROC_FS
3365
3366#define SEQ_START_TOKEN ((void *)1)
3367
3368static void *bond_info_seq_start(struct seq_file *seq, loff_t *pos)
3369{
3370        struct bonding *bond = seq->private;
3371        loff_t off = 0;
3372        struct slave *slave;
3373
3374        /* make sure the bond won't be taken away */
3375        read_lock(&dev_base_lock);
3376        read_lock_bh(&bond->lock);
3377
3378        if (*pos == 0) {
3379                return SEQ_START_TOKEN;
3380        }
3381
3382        for (slave = bond->prev; slave != (slave_t *)bond;
3383             slave = slave->prev) {
3384
3385                if (++off == *pos) {
3386                        return slave;
3387                }
3388        }
3389
3390        return NULL;
3391}
3392
3393static void *bond_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3394{
3395        struct bonding *bond = seq->private;
3396        struct slave *slave = v;
3397
3398        ++*pos;
3399        if (v == SEQ_START_TOKEN) {
3400                slave = bond->prev;
3401        } else {
3402                slave = slave->prev;
3403        }
3404
3405        return (slave == (struct slave *) bond) ? NULL : slave;
3406}
3407
3408static void bond_info_seq_stop(struct seq_file *seq, void *v)
3409{
3410        struct bonding *bond = seq->private;
3411
3412        read_unlock_bh(&bond->lock);
3413        read_unlock(&dev_base_lock);
3414}
3415
3416static void bond_info_show_master(struct seq_file *seq, struct bonding *bond)
3417{
3418        struct slave *curr;
3419
3420        read_lock(&bond->ptrlock);
3421        curr = bond->current_slave;
3422        read_unlock(&bond->ptrlock);
3423
3424        seq_printf(seq, "Bonding Mode: %s\n", bond_mode_name());
3425
3426        if (USES_PRIMARY(bond_mode)) {
3427                if (curr) {
3428                        seq_printf(seq,
3429                                   "Currently Active Slave: %s\n",
3430                                   curr->dev->name);
3431                }
3432        }
3433
3434        seq_printf(seq, "MII Status: %s\n", (curr) ? "up" : "down");
3435        seq_printf(seq, "MII Polling Interval (ms): %d\n", miimon);
3436        seq_printf(seq, "Up Delay (ms): %d\n", updelay * miimon);
3437        seq_printf(seq, "Down Delay (ms): %d\n", downdelay * miimon);
3438        seq_printf(seq, "Multicast Mode: %s\n", multicast_mode_name());
3439
3440        if (bond_mode == BOND_MODE_8023AD) {
3441                struct ad_info ad_info;
3442
3443                seq_puts(seq, "\n802.3ad info\n");
3444
3445                if (bond_3ad_get_active_agg_info(bond, &ad_info)) {
3446                        seq_printf(seq, "bond %s has no active aggregator\n",
3447                                   bond->device->name);
3448                } else {
3449                        seq_printf(seq, "Active Aggregator Info:\n");
3450
3451                        seq_printf(seq, "\tAggregator ID: %d\n",
3452                                   ad_info.aggregator_id);
3453                        seq_printf(seq, "\tNumber of ports: %d\n",
3454                                   ad_info.ports);
3455                        seq_printf(seq, "\tActor Key: %d\n",
3456                                   ad_info.actor_key);
3457                        seq_printf(seq, "\tPartner Key: %d\n",
3458                                   ad_info.partner_key);
3459                        seq_printf(seq, "\tPartner Mac Address: %02x:%02x:%02x:%02x:%02x:%02x\n",
3460                                   ad_info.partner_system[0],
3461                                   ad_info.partner_system[1],
3462                                   ad_info.partner_system[2],
3463                                   ad_info.partner_system[3],
3464                                   ad_info.partner_system[4],
3465                                   ad_info.partner_system[5]);
3466                }
3467        }
3468}
3469
3470static void bond_info_show_slave(struct seq_file *seq, const struct slave *slave)
3471{
3472        seq_printf(seq, "\nSlave Interface: %s\n", slave->dev->name);
3473        seq_printf(seq, "MII Status: %s\n",
3474                   (slave->link == BOND_LINK_UP) ?  "up" : "down");
3475        seq_printf(seq, "Link Failure Count: %d\n",
3476                   slave->link_failure_count);
3477
3478        if (app_abi_ver >= 1) {
3479                seq_printf(seq,
3480                           "Permanent HW addr: %02x:%02x:%02x:%02x:%02x:%02x\n",
3481                           slave->perm_hwaddr[0],
3482                           slave->perm_hwaddr[1],
3483                           slave->perm_hwaddr[2],
3484                           slave->perm_hwaddr[3],
3485                           slave->perm_hwaddr[4],
3486                           slave->perm_hwaddr[5]);
3487        }
3488
3489        if (bond_mode == BOND_MODE_8023AD) {
3490                const struct aggregator *agg
3491                        = SLAVE_AD_INFO(slave).port.aggregator;
3492
3493                if (agg) {
3494                        seq_printf(seq, "Aggregator ID: %d\n",
3495                                   agg->aggregator_identifier);
3496                } else {
3497                        seq_puts(seq, "Aggregator ID: N/A\n");
3498                }
3499        }
3500}
3501
3502static int bond_info_seq_show(struct seq_file *seq, void *v)
3503{
3504        if (v == SEQ_START_TOKEN) {
3505                seq_printf(seq, "%s\n", version);
3506                bond_info_show_master(seq, seq->private);
3507        } else {
3508                bond_info_show_slave(seq, v);
3509        }
3510
3511        return 0;
3512}
3513
3514static struct seq_operations bond_info_seq_ops = {
3515        .start = bond_info_seq_start,
3516        .next  = bond_info_seq_next,
3517        .stop  = bond_info_seq_stop,
3518        .show  = bond_info_seq_show,
3519};
3520
3521static int bond_info_open(struct inode *inode, struct file *file)
3522{
3523        struct seq_file *seq;
3524        struct proc_dir_entry *proc;
3525        int rc;
3526
3527        rc = seq_open(file, &bond_info_seq_ops);
3528        if (!rc) {
3529                /* recover the pointer buried in proc_dir_entry data */
3530                seq = file->private_data;
3531                proc = PDE(inode);
3532                seq->private = proc->data;
3533        }
3534        return rc;
3535}
3536
3537static struct file_operations bond_info_fops = {
3538        .owner   = THIS_MODULE,
3539        .open    = bond_info_open,
3540        .read    = seq_read,
3541        .llseek  = seq_lseek,
3542        .release = seq_release,
3543};
3544
3545static int bond_create_proc_info(struct bonding *bond)
3546{
3547        struct net_device *dev = bond->device;
3548
3549        if (bond_proc_dir) {
3550                bond->bond_proc_file = create_proc_entry(dev->name,
3551                                                         S_IRUGO, 
3552                                                         bond_proc_dir);
3553                if (bond->bond_proc_file == NULL) {
3554                        printk(KERN_WARNING
3555                               "%s: Cannot create /proc/net/bonding/%s\n", 
3556                               dev->name, dev->name);
3557                } else {
3558                        bond->bond_proc_file->data = bond;
3559                        bond->bond_proc_file->proc_fops = &bond_info_fops;
3560                        bond->bond_proc_file->owner = THIS_MODULE;
3561                        memcpy(bond->procdir_name, dev->name, IFNAMSIZ);
3562                }
3563        }
3564
3565        return 0;
3566}
3567
3568static void bond_destroy_proc_info(struct bonding *bond)
3569{
3570        if (bond_proc_dir && bond->bond_proc_file) {
3571                remove_proc_entry(bond->procdir_name, bond_proc_dir);
3572                memset(bond->procdir_name, 0, IFNAMSIZ);
3573                bond->bond_proc_file = NULL;
3574        }
3575}
3576
3577/* Create the bonding directory under /proc/net, if doesn't exist yet.
3578 * Caller must hold rtnl_lock.
3579 */
3580static void bond_create_proc_dir(void)
3581{
3582        int len = strlen(DRV_NAME);
3583
3584        for (bond_proc_dir = proc_net->subdir; bond_proc_dir;
3585             bond_proc_dir = bond_proc_dir->next) {
3586                if ((bond_proc_dir->namelen == len) &&
3587                    !memcmp(bond_proc_dir->name, DRV_NAME, len)) {
3588                        break;
3589                }
3590        }
3591
3592        if (!bond_proc_dir) {
3593                bond_proc_dir = proc_mkdir(DRV_NAME, proc_net);
3594                if (bond_proc_dir) {
3595                        bond_proc_dir->owner = THIS_MODULE;
3596                } else {
3597                        printk(KERN_WARNING DRV_NAME
3598                                ": Warning: cannot create /proc/net/%s\n",
3599                                DRV_NAME);
3600                }
3601        }
3602}
3603
3604/* Destroy the bonding directory under /proc/net, if empty.
3605 * Caller must hold rtnl_lock.
3606 */
3607static void bond_destroy_proc_dir(void)
3608{
3609        struct proc_dir_entry *de;
3610
3611        if (!bond_proc_dir) {
3612                return;
3613        }
3614
3615        /* verify that the /proc dir is empty */
3616        for (de = bond_proc_dir->subdir; de; de = de->next) {
3617                /* ignore . and .. */
3618                if (*(de->name) != '.') {
3619                        break;
3620                }
3621        }
3622
3623        if (de) {
3624                if (bond_proc_dir->owner == THIS_MODULE) {
3625                        bond_proc_dir->owner = NULL;
3626                }
3627        } else {
3628                remove_proc_entry(DRV_NAME, proc_net);
3629                bond_proc_dir = NULL;
3630        }
3631}
3632#endif /* CONFIG_PROC_FS */
3633
3634/*
3635 * Change HW address
3636 *
3637 * Note that many devices must be down to change the HW address, and
3638 * downing the master releases all slaves.  We can make bonds full of
3639 * bonding devices to test this, however.
3640 */
3641static inline int
3642bond_set_mac_address(struct net_device *dev, void *addr)
3643{
3644        struct bonding *bond = dev->priv;
3645        struct sockaddr *sa = addr, tmp_sa;
3646        struct slave *slave;
3647        int error;
3648
3649        dprintk(KERN_INFO "bond_set_mac_address %p %s\n", dev,
3650               dev->name);
3651
3652        if (!is_valid_ether_addr(sa->sa_data)) {
3653                return -EADDRNOTAVAIL;
3654        }
3655
3656        for (slave = bond->prev; slave != (struct slave *)bond;
3657             slave = slave->prev) {
3658                dprintk(KERN_INFO "bond_set_mac: slave %p %s\n", slave,
3659                        slave->dev->name);
3660                if (slave->dev->set_mac_address == NULL) {
3661                        error = -EOPNOTSUPP;
3662                        dprintk(KERN_INFO "bond_set_mac EOPNOTSUPP %s\n",
3663                                slave->dev->name);
3664                        goto unwind;
3665                }
3666
3667                error = slave->dev->set_mac_address(slave->dev, addr);
3668                if (error) {
3669                        /* TODO: consider downing the slave 
3670                         * and retry ?
3671                         * User should expect communications
3672                         * breakage anyway until ARP finish
3673                         * updating, so...
3674                         */
3675                        dprintk(KERN_INFO "bond_set_mac err %d %s\n",
3676                                error, slave->dev->name);
3677                        goto unwind;
3678                }
3679        }
3680
3681        /* success */
3682        memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
3683        return 0;
3684
3685unwind:
3686        memcpy(tmp_sa.sa_data, dev->dev_addr, dev->addr_len);
3687        tmp_sa.sa_family = dev->type;
3688
3689        for (slave = slave->next; slave != bond->next;
3690             slave = slave->next) {
3691                int tmp_error;
3692
3693                tmp_error = slave->dev->set_mac_address(slave->dev, &tmp_sa);
3694                if (tmp_error) {
3695                        dprintk(KERN_INFO "bond_set_mac_address: "
3696                                "unwind err %d dev %s\n",
3697                                tmp_error, slave->dev->name);
3698                }
3699        }
3700
3701        return error;
3702}
3703
3704/*
3705 * Change the MTU of all of a master's slaves to match the master
3706 */
3707static inline int
3708bond_change_mtu(struct net_device *dev, int newmtu)
3709{
3710        bonding_t *bond = dev->priv;
3711        slave_t *slave;
3712        int error;
3713
3714        dprintk(KERN_INFO "CM: b %p nm %d\n", bond, newmtu);
3715        for (slave = bond->prev; slave != (slave_t *)bond;
3716             slave = slave->prev) {
3717                dprintk(KERN_INFO "CM: s %p s->p %p c_m %p\n", slave,
3718                        slave->prev, slave->dev->change_mtu);
3719                if (slave->dev->change_mtu) {
3720                        error = slave->dev->change_mtu(slave->dev, newmtu);
3721                } else {
3722                        slave->dev->mtu = newmtu;
3723                        error = 0;
3724                }
3725
3726                if (error) {
3727                        /* If we failed to set the slave's mtu to the new value
3728                         * we must abort the operation even in ACTIVE_BACKUP
3729                         * mode, because if we allow the backup slaves to have
3730                         * different mtu values than the active slave we'll
3731                         * need to change their mtu when doing a failover. That
3732                         * means changing their mtu from timer context, which
3733                         * is probably not a good idea.
3734                         */
3735                        dprintk(KERN_INFO "bond_change_mtu err %d %s\n",
3736                               error, slave->dev->name);
3737                        goto unwind;
3738                }
3739        }
3740
3741        dev->mtu = newmtu;
3742        return 0;
3743
3744
3745unwind:
3746        for (slave = slave->next; slave != bond->next;
3747             slave = slave->next) {
3748
3749                if (slave->dev->change_mtu) {
3750                        slave->dev->change_mtu(slave->dev, dev->mtu);
3751                } else {
3752                        slave->dev->mtu = dev->mtu;
3753                }
3754        }
3755
3756        return error;
3757}
3758
3759/*
3760 * Change device name
3761 */
3762static inline int bond_event_changename(struct bonding *bond)
3763{
3764#ifdef CONFIG_PROC_FS
3765        bond_destroy_proc_info(bond);
3766        bond_create_proc_info(bond);
3767#endif
3768
3769        return NOTIFY_DONE;
3770}
3771
3772static int bond_master_netdev_event(unsigned long event, struct net_device *event_dev)
3773{
3774        struct bonding *bond, *event_bond = NULL;
3775
3776        list_for_each_entry(bond, &bond_dev_list, bond_list) {
3777                if (bond == event_dev->priv) {
3778                        event_bond = bond;
3779                        break;
3780                }
3781        }
3782
3783        if (event_bond == NULL) {
3784                return NOTIFY_DONE;
3785        }
3786
3787        switch (event) {
3788        case NETDEV_CHANGENAME:
3789                return bond_event_changename(event_bond);
3790        case NETDEV_UNREGISTER:
3791                /*
3792                 * TODO: remove a bond from the list?
3793                 */
3794                break;
3795        default:
3796                break;
3797        }
3798
3799        return NOTIFY_DONE;
3800}
3801
3802static int bond_slave_netdev_event(unsigned long event, struct net_device *event_dev)
3803{
3804        struct net_device *master = event_dev->master;
3805
3806        switch (event) {
3807        case NETDEV_UNREGISTER:
3808                if (master != NULL) {
3809                        bond_release(master, event_dev);
3810                }
3811                break;
3812        case NETDEV_CHANGE:
3813                /*
3814                 * TODO: is this what we get if somebody
3815                 * sets up a hierarchical bond, then rmmod's
3816                 * one of the slave bonding devices?
3817                 */
3818                break;
3819        case NETDEV_DOWN:
3820                /*
3821                 * ... Or is it this?
3822                 */
3823                break;
3824        case NETDEV_CHANGEMTU:
3825                /*
3826                 * TODO: Should slaves be allowed to
3827                 * independently alter their MTU?  For
3828                 * an active-backup bond, slaves need
3829                 * not be the same type of device, so
3830                 * MTUs may vary.  For other modes,
3831                 * slaves arguably should have the
3832                 * same MTUs. To do this, we'd need to
3833                 * take over the slave's change_mtu
3834                 * function for the duration of their
3835                 * servitude.
3836                 */
3837                break;
3838        case NETDEV_CHANGENAME:
3839                /*
3840                 * TODO: handle changing the primary's name
3841                 */
3842                break;
3843        default:
3844                break;
3845        }
3846
3847        return NOTIFY_DONE;
3848}
3849
3850/*
3851 * bond_netdev_event: handle netdev notifier chain events.
3852 *
3853 * This function receives events for the netdev chain.  The caller (an
3854 * ioctl handler calling notifier_call_chain) holds the necessary
3855 * locks for us to safely manipulate the slave devices (RTNL lock,
3856 * dev_probe_lock).
3857 */
3858static int bond_netdev_event(struct notifier_block *this, unsigned long event, void *ptr)
3859{
3860        struct net_device *event_dev = (struct net_device *)ptr;
3861        unsigned short flags;
3862        int res = NOTIFY_DONE;
3863
3864        dprintk(KERN_INFO "bond_netdev_event n_b %p ev %lx ptr %p\n",
3865                this, event, ptr);
3866
3867        flags = event_dev->flags & (IFF_MASTER | IFF_SLAVE);
3868        switch (flags) {
3869        case IFF_MASTER:
3870                res = bond_master_netdev_event(event, event_dev);
3871                break;
3872        case IFF_SLAVE:
3873                res = bond_slave_netdev_event(event, event_dev);
3874                break;
3875        default:
3876                /* A master that is also a slave ? */
3877                break;
3878        }
3879
3880        return res;
3881}
3882
3883static struct notifier_block bond_netdev_notifier = {
3884        .notifier_call = bond_netdev_event,
3885};
3886
3887/* De-initialize device specific data.
3888 * Caller must hold rtnl_lock.
3889 */
3890static inline void bond_deinit(struct net_device *dev)
3891{
3892        struct bonding *bond = dev->priv;
3893
3894        list_del(&bond->bond_list);
3895
3896#ifdef CONFIG_PROC_FS
3897        bond_destroy_proc_info(bond);
3898#endif
3899}
3900
3901/* Unregister and free all bond devices.
3902 * Caller must hold rtnl_lock.
3903 */
3904static void bond_free_all(void)
3905{
3906        struct bonding *bond, *nxt;
3907
3908        list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list) {
3909                struct net_device *dev = bond->device;
3910
3911                unregister_netdevice(dev);
3912                bond_deinit(dev);
3913                free_netdev(dev);
3914        }
3915
3916#ifdef CONFIG_PROC_FS
3917        bond_destroy_proc_dir();
3918#endif
3919}
3920
3921/*
3922 * Does not allocate but creates a /proc entry.
3923 * Allowed to fail.
3924 */
3925static int __init bond_init(struct net_device *dev)
3926{
3927        struct bonding *bond;
3928        int count;
3929
3930#ifdef BONDING_DEBUG
3931        printk (KERN_INFO "Begin bond_init for %s\n", dev->name);
3932#endif
3933        bond = dev->priv;
3934
3935        /* initialize rwlocks */
3936        rwlock_init(&bond->lock);
3937        rwlock_init(&bond->ptrlock);
3938
3939        /* Initialize pointers */
3940        bond->next = bond->prev = (slave_t *)bond;
3941        bond->current_slave = NULL;
3942        bond->current_arp_slave = NULL;
3943        bond->device = dev;
3944
3945        /* Initialize the device structure. */
3946        dev->set_mac_address = bond_set_mac_address;
3947
3948        switch (bond_mode) {
3949        case BOND_MODE_ACTIVEBACKUP:
3950                dev->hard_start_xmit = bond_xmit_activebackup;
3951                break;
3952        case BOND_MODE_ROUNDROBIN:
3953                dev->hard_start_xmit = bond_xmit_roundrobin;
3954                break;
3955        case BOND_MODE_XOR:
3956                dev->hard_start_xmit = bond_xmit_xor;
3957                break;
3958        case BOND_MODE_BROADCAST:
3959                dev->hard_start_xmit = bond_xmit_broadcast;
3960                break;
3961        case BOND_MODE_8023AD:
3962                dev->hard_start_xmit = bond_3ad_xmit_xor;
3963                break;
3964        case BOND_MODE_TLB:
3965        case BOND_MODE_ALB:
3966                dev->hard_start_xmit = bond_alb_xmit;
3967                dev->set_mac_address = bond_alb_set_mac_address;
3968                break;
3969        default:
3970                printk(KERN_ERR "Unknown bonding mode %d\n", bond_mode);
3971                return -EINVAL;
3972        }
3973
3974        dev->get_stats = bond_get_stats;
3975        dev->open = bond_open;
3976        dev->stop = bond_close;
3977        dev->set_multicast_list = set_multicast_list;
3978        dev->do_ioctl = bond_ioctl;
3979        dev->change_mtu = bond_change_mtu;
3980        dev->tx_queue_len = 0;
3981        dev->flags |= IFF_MASTER|IFF_MULTICAST;
3982#ifdef CONFIG_NET_FASTROUTE
3983        dev->accept_fastpath = bond_accept_fastpath;
3984#endif
3985
3986        printk(KERN_INFO "%s registered with", dev->name);
3987        if (miimon > 0) {
3988                printk(" MII link monitoring set to %d ms", miimon);
3989                updelay /= miimon;
3990                downdelay /= miimon;
3991        } else {
3992                printk("out MII link monitoring");
3993        }
3994        printk(", in %s mode.\n", bond_mode_name());
3995
3996        printk(KERN_INFO "%s registered with", dev->name);
3997        if (arp_interval > 0) {
3998                printk(" ARP monitoring set to %d ms with %d target(s):", 
3999                        arp_interval, arp_ip_count);
4000                for (count=0 ; count<arp_ip_count ; count++)
4001                        printk (" %s", arp_ip_target[count]);
4002                printk("\n");
4003        } else {
4004                printk("out ARP monitoring\n");
4005        }
4006 
4007#ifdef CONFIG_PROC_FS
4008        bond_create_proc_info(bond);
4009#endif
4010
4011        list_add_tail(&bond->bond_list, &bond_dev_list);
4012
4013        return 0;
4014}
4015
4016/*
4017static int __init bond_probe(struct net_device *dev)
4018{
4019        bond_init(dev);
4020        return 0;
4021}
4022 */
4023
4024/*
4025 * Convert string input module parms.  Accept either the
4026 * number of the mode or its string name.
4027 */
4028static inline int
4029bond_parse_parm(char *mode_arg, struct bond_parm_tbl *tbl)
4030{
4031        int i;
4032
4033        for (i = 0; tbl[i].modename != NULL; i++) {
4034                if ((isdigit(*mode_arg) &&
4035                    tbl[i].mode == simple_strtol(mode_arg, NULL, 0)) ||
4036                    (0 == strncmp(mode_arg, tbl[i].modename,
4037                                  strlen(tbl[i].modename)))) {
4038                        return tbl[i].mode;
4039                }
4040        }
4041
4042        return -1;
4043}
4044
4045
4046static int __init bonding_init(void)
4047{
4048        int no;
4049        int err;
4050
4051        printk(KERN_INFO "%s", version);
4052
4053        /*
4054         * Convert string parameters.
4055         */
4056        if (mode) {
4057                bond_mode = bond_parse_parm(mode, bond_mode_tbl);
4058                if (bond_mode == -1) {
4059                        printk(KERN_WARNING
4060                               "bonding_init(): Invalid bonding mode \"%s\"\n",
4061                               mode == NULL ? "NULL" : mode);
4062                        return -EINVAL;
4063                }
4064        }
4065
4066        if (USES_PRIMARY(bond_mode)) {
4067                multicast_mode = BOND_MULTICAST_ACTIVE;
4068        } else {
4069                multicast_mode = BOND_MULTICAST_ALL;
4070        }
4071
4072        if (multicast) {
4073                multicast_mode = bond_parse_parm(multicast, bond_mc_tbl);
4074                if (multicast_mode == -1) {
4075                        printk(KERN_WARNING 
4076                       "bonding_init(): Invalid multicast mode \"%s\"\n",
4077                               multicast == NULL ? "NULL" : multicast);
4078                        return -EINVAL;
4079                }
4080        }
4081
4082        if (lacp_rate) {
4083                if (bond_mode != BOND_MODE_8023AD) {
4084                        printk(KERN_WARNING
4085                               "lacp_rate param is irrelevant in mode %s\n",
4086                               bond_mode_name());
4087                } else {
4088                        lacp_fast = bond_parse_parm(lacp_rate, bond_lacp_tbl);
4089                        if (lacp_fast == -1) {
4090                                printk(KERN_WARNING
4091                                       "bonding_init(): Invalid lacp rate "
4092                                       "\"%s\"\n",
4093                                       lacp_rate == NULL ? "NULL" : lacp_rate);
4094
4095                                return -EINVAL;
4096                        }
4097                }
4098        }
4099
4100        if (max_bonds < 1 || max_bonds > INT_MAX) {
4101                printk(KERN_WARNING 
4102                       "bonding_init(): max_bonds (%d) not in range %d-%d, "
4103                       "so it was reset to BOND_DEFAULT_MAX_BONDS (%d)",
4104                       max_bonds, 1, INT_MAX, BOND_DEFAULT_MAX_BONDS);
4105                max_bonds = BOND_DEFAULT_MAX_BONDS;
4106        }
4107
4108        if (miimon < 0) {
4109                printk(KERN_WARNING 
4110                       "bonding_init(): miimon module parameter (%d), "
4111                       "not in range 0-%d, so it was reset to %d\n",
4112                       miimon, INT_MAX, BOND_LINK_MON_INTERV);
4113                miimon = BOND_LINK_MON_INTERV;
4114        }
4115
4116        if (updelay < 0) {
4117                printk(KERN_WARNING 
4118                       "bonding_init(): updelay module parameter (%d), "
4119                       "not in range 0-%d, so it was reset to 0\n",
4120                       updelay, INT_MAX);
4121                updelay = 0;
4122        }
4123
4124        if (downdelay < 0) {
4125                printk(KERN_WARNING 
4126                       "bonding_init(): downdelay module parameter (%d), "
4127                       "not in range 0-%d, so it was reset to 0\n",
4128                       downdelay, INT_MAX);
4129                downdelay = 0;
4130        }
4131
4132        /* reset values for 802.3ad */
4133        if (bond_mode == BOND_MODE_8023AD) {
4134                if (arp_interval != 0) {
4135                        printk(KERN_WARNING "bonding_init(): ARP monitoring"
4136                               "can't be used simultaneously with 802.3ad, "
4137                               "disabling ARP monitoring\n");
4138                        arp_interval = 0;
4139                }
4140
4141                if (miimon == 0) {
4142                        printk(KERN_ERR
4143                               "bonding_init(): miimon must be specified, "
4144                               "otherwise bonding will not detect link failure, "
4145                               "speed and duplex which are essential "
4146                               "for 802.3ad operation\n");
4147                        printk(KERN_ERR "Forcing miimon to 100msec\n");
4148                        miimon = 100;
4149                }
4150
4151                if (multicast_mode != BOND_MULTICAST_ALL) {
4152                        printk(KERN_ERR
4153                               "bonding_init(): Multicast mode must "
4154                               "be set to ALL for 802.3ad\n");
4155                        printk(KERN_ERR "Forcing Multicast mode to ALL\n");
4156                        multicast_mode = BOND_MULTICAST_ALL;
4157                }
4158        }
4159
4160        /* reset values for TLB/ALB */
4161        if ((bond_mode == BOND_MODE_TLB) ||
4162            (bond_mode == BOND_MODE_ALB)) {
4163                if (miimon == 0) {
4164                        printk(KERN_ERR
4165                               "bonding_init(): miimon must be specified, "
4166                               "otherwise bonding will not detect link failure "
4167                               "and link speed which are essential "
4168                               "for TLB/ALB load balancing\n");
4169                        printk(KERN_ERR "Forcing miimon to 100msec\n");
4170                        miimon = 100;
4171                }
4172
4173                if (multicast_mode != BOND_MULTICAST_ACTIVE) {
4174                        printk(KERN_ERR
4175                               "bonding_init(): Multicast mode must "
4176                               "be set to ACTIVE for TLB/ALB\n");
4177                        printk(KERN_ERR "Forcing Multicast mode to ACTIVE\n");
4178                        multicast_mode = BOND_MULTICAST_ACTIVE;
4179                }
4180        }
4181
4182        if (bond_mode == BOND_MODE_ALB) {
4183                printk(KERN_INFO
4184                       "In ALB mode you might experience client disconnections"
4185                       " upon reconnection of a link if the bonding module"
4186                       " updelay parameter (%d msec) is incompatible with the"
4187                       " forwarding delay time of the switch\n", updelay);
4188        }
4189
4190        if (miimon == 0) {
4191                if ((updelay != 0) || (downdelay != 0)) {
4192                        /* just warn the user the up/down delay will have
4193                         * no effect since miimon is zero...
4194                         */
4195                        printk(KERN_WARNING 
4196                               "bonding_init(): miimon module parameter not "
4197                               "set and updelay (%d) or downdelay (%d) module "
4198                               "parameter is set; updelay and downdelay have "
4199                               "no effect unless miimon is set\n",
4200                               updelay, downdelay);
4201                }
4202        } else {
4203                /* don't allow arp monitoring */
4204                if (arp_interval != 0) {
4205                        printk(KERN_WARNING 
4206                               "bonding_init(): miimon (%d) and arp_interval "
4207                               "(%d) can't be used simultaneously, "
4208                               "disabling ARP monitoring\n",
4209                               miimon, arp_interval);
4210                        arp_interval = 0;
4211                }
4212
4213                if ((updelay % miimon) != 0) {
4214                        /* updelay will be rounded in bond_init() when it
4215                         * is divided by miimon, we just inform user here
4216                         */
4217                        printk(KERN_WARNING 
4218                               "bonding_init(): updelay (%d) is not a multiple "
4219                               "of miimon (%d), updelay rounded to %d ms\n",
4220                               updelay, miimon, (updelay / miimon) * miimon);
4221                }
4222
4223                if ((downdelay % miimon) != 0) {
4224                        /* downdelay will be rounded in bond_init() when it
4225                         * is divided by miimon, we just inform user here
4226                         */
4227                        printk(KERN_WARNING 
4228                               "bonding_init(): downdelay (%d) is not a "
4229                               "multiple of miimon (%d), downdelay rounded "
4230                               "to %d ms\n",
4231                               downdelay, miimon, 
4232                               (downdelay / miimon) * miimon);
4233                }
4234        }
4235
4236        if (arp_interval < 0) {
4237                printk(KERN_WARNING 
4238                       "bonding_init(): arp_interval module parameter (%d), "
4239                       "not in range 0-%d, so it was reset to %d\n",
4240                       arp_interval, INT_MAX, BOND_LINK_ARP_INTERV);
4241                arp_interval = BOND_LINK_ARP_INTERV;
4242        }
4243
4244        for (arp_ip_count=0 ;
4245             (arp_ip_count < MAX_ARP_IP_TARGETS) && arp_ip_target[arp_ip_count];
4246              arp_ip_count++ ) {
4247                /* not complete check, but should be good enough to
4248                   catch mistakes */
4249                if (!isdigit(arp_ip_target[arp_ip_count][0])) { 
4250                        printk(KERN_WARNING
4251                               "bonding_init(): bad arp_ip_target module "
4252                               "parameter (%s), ARP monitoring will not be "
4253                               "performed\n",
4254                               arp_ip_target[arp_ip_count]);
4255                        arp_interval = 0;
4256                } else { 
4257                        u32 ip = in_aton(arp_ip_target[arp_ip_count]); 
4258                        arp_target[arp_ip_count] = ip;
4259                }
4260        }
4261
4262
4263        if ( (arp_interval > 0) && (arp_ip_count==0)) {
4264                /* don't allow arping if no arp_ip_target given... */
4265                printk(KERN_WARNING 
4266                       "bonding_init(): arp_interval module parameter "
4267                       "(%d) specified without providing an arp_ip_target "
4268                       "parameter, arp_interval was reset to 0\n",
4269                       arp_interval);
4270                arp_interval = 0;
4271        }
4272
4273        if ((miimon == 0) && (arp_interval == 0)) {
4274                /* miimon and arp_interval not set, we need one so things
4275                 * work as expected, see bonding.txt for details
4276                 */
4277                printk(KERN_ERR 
4278                       "bonding_init(): either miimon or "
4279                       "arp_interval and arp_ip_target module parameters "
4280                       "must be specified, otherwise bonding will not detect "
4281                       "link failures! see bonding.txt for details.\n");
4282        }
4283
4284        if ((primary != NULL) && !USES_PRIMARY(bond_mode)) {
4285                /* currently, using a primary only makes sense
4286                 * in active backup, TLB or ALB modes
4287                 */
4288                printk(KERN_WARNING 
4289                       "bonding_init(): %s primary device specified but has "
4290                       "no effect in %s mode\n",
4291                       primary, bond_mode_name());
4292                primary = NULL;
4293        }
4294
4295        rtnl_lock();
4296
4297#ifdef CONFIG_PROC_FS
4298        bond_create_proc_dir();
4299#endif
4300
4301        err = 0;
4302        for (no = 0; no < max_bonds; no++) {
4303                struct net_device *dev;
4304
4305                dev = alloc_netdev(sizeof(struct bonding), "", ether_setup);
4306                if (!dev) {
4307                        err = -ENOMEM;
4308                        goto out_err;
4309                }
4310
4311                err = dev_alloc_name(dev, "bond%d");
4312                if (err < 0) {
4313                        free_netdev(dev);
4314                        goto out_err;
4315                }
4316
4317                /* bond_init() must be called after dev_alloc_name() (for the
4318                 * /proc files), but before register_netdevice(), because we
4319                 * need to set function pointers.
4320                 */
4321                err = bond_init(dev);
4322                if (err < 0) {
4323                        free_netdev(dev);
4324                        goto out_err;
4325                }
4326
4327                SET_MODULE_OWNER(dev);
4328
4329                err = register_netdevice(dev);
4330                if (err < 0) {
4331                        bond_deinit(dev);
4332                        free_netdev(dev);
4333                        goto out_err;
4334                }
4335        }
4336
4337        rtnl_unlock();
4338        register_netdevice_notifier(&bond_netdev_notifier);
4339
4340        return 0;
4341
4342out_err:
4343        /* free and unregister all bonds that were successfully added */
4344        bond_free_all();
4345
4346        rtnl_unlock();
4347
4348        return err;
4349}
4350
4351static void __exit bonding_exit(void)
4352{
4353        unregister_netdevice_notifier(&bond_netdev_notifier);
4354
4355        rtnl_lock();
4356        bond_free_all();
4357        rtnl_unlock();
4358}
4359
4360module_init(bonding_init);
4361module_exit(bonding_exit);
4362MODULE_LICENSE("GPL");
4363MODULE_DESCRIPTION(DRV_DESCRIPTION ", v" DRV_VERSION);
4364
4365/*
4366 * Local variables:
4367 *  c-indent-level: 8
4368 *  c-basic-offset: 8
4369 *  tab-width: 8
4370 * End:
4371 */
4372
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.