40ff79175c4aceaaf7514b84fec9971c29473f92
[pandora-kernel.git] / drivers / net / bonding / bond_main.c
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 curr_active_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 curr_active_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 curr_active_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  * 2003/11/20 - Amir Noam <amir.noam at intel dot com>
431  *      - Fix /proc creation/destruction.
432  *
433  * 2003/12/01 - Shmulik Hen <shmulik.hen at intel dot com>
434  *      - Massive cleanup - Set version to 2.5.0
435  *        Code changes:
436  *        o Consolidate format of prints and debug prints.
437  *        o Remove bonding_t/slave_t typedefs and consolidate all casts.
438  *        o Remove dead code and unnecessary checks.
439  *        o Consolidate starting/stopping timers.
440  *        o Consolidate handling of primary module param throughout the code.
441  *        o Removed multicast module param support - all settings are done
442  *          according to mode.
443  *        o Slave list iteration - bond is no longer part of the list,
444  *          added cyclic list iteration macros.
445  *        o Consolidate error handling in all xmit functions.
446  *        Style changes:
447  *        o Consolidate function naming and declarations.
448  *        o Consolidate function params and local variables names.
449  *        o Consolidate return values.
450  *        o Consolidate curly braces.
451  *        o Consolidate conditionals format.
452  *        o Change struct member names and types.
453  *        o Chomp trailing spaces, remove empty lines, fix indentations.
454  *        o Re-organize code according to context.
455  *
456  * 2003/12/30 - Amir Noam <amir.noam at intel dot com>
457  *      - Fixed: Cannot remove and re-enslave the original active slave.
458  *      - Fixed: Releasing the original active slave causes mac address
459  *               duplication.
460  *      - Add support for slaves that use ethtool_ops.
461  *        Set version to 2.5.3.
462  *
463  * 2004/01/05 - Amir Noam <amir.noam at intel dot com>
464  *      - Save bonding parameters per bond instead of using the global values.
465  *        Set version to 2.5.4.
466  *
467  * 2004/01/14 - Shmulik Hen <shmulik.hen at intel dot com>
468  *      - Enhance VLAN support:
469  *        * Add support for VLAN hardware acceleration capable slaves.
470  *        * Add capability to tag self generated packets in ALB/TLB modes.
471  *        Set version to 2.6.0.
472  * 2004/10/29 - Mitch Williams <mitch.a.williams at intel dot com>
473  *      - Fixed bug when unloading module while using 802.3ad.  If
474  *        spinlock debugging is turned on, this causes a stack dump.
475  *        Solution is to move call to dev_remove_pack outside of the
476  *        spinlock.
477  *        Set version to 2.6.1.
478  * 2005/06/05 - Jay Vosburgh <fubar@us.ibm.com>
479  *      - Support for generating gratuitous ARPs in active-backup mode.
480  *        Includes support for VLAN tagging all bonding-generated ARPs
481  *        as needed.  Set version to 2.6.2.
482  * 2005/06/08 - Jason Gabler <jygabler at lbl dot gov>
483  *      - alternate hashing policy support for mode 2
484  *        * Added kernel parameter "xmit_hash_policy" to allow the selection
485  *          of different hashing policies for mode 2.  The original mode 2
486  *          policy is the default, now found in xmit_hash_policy_layer2().
487  *        * Added xmit_hash_policy_layer34()
488  *      - Modified by Jay Vosburgh <fubar@us.ibm.com> to also support mode 4.
489  *        Set version to 2.6.3.
490  * 2005/09/26 - Jay Vosburgh <fubar@us.ibm.com>
491  *      - Removed backwards compatibility for old ifenslaves.  Version 2.6.4.
492  * 2005/09/27 - Mitch Williams <mitch.a.williams at intel dot com>
493  *      - Radheka Godse <radheka.godse at intel dot com>
494  *      - Split out bond creation code to allow for sysfs interface.
495  *      - Removed static declaration on some functions and data items.
496  *      - Added sysfs support, including capability to add/remove/change
497  *         any bond at runtime.
498  *
499  *      - Miscellaneous:
500  *      - Added bonding: <bondname>: prefix to sysfs log messages
501  *      - Added arp_ip_targets to /proc entry
502  *      - Allow ARP target table to have empty entries
503  *      - trivial fix: added missing modes description to modinfo
504  *      - Corrected bug in ALB init where kmalloc is called inside
505  *        a held lock
506  *      - Corrected behavior to maintain bond link when changing
507  *        from arp monitor to miimon and vice versa
508  *      - Added missing bonding: <bondname>: prefix to alb, ad log messages
509  *      - Fixed stack dump warnings seen if changing between miimon
510  *        and arp monitoring when the bond interface is down.
511  *      - Fixed stack dump warnings seen when enslaving an e100
512  *        driver
513  *      - Set version to 3.0.0
514  */
515
516 //#define BONDING_DEBUG 1
517
518 #include <linux/config.h>
519 #include <linux/kernel.h>
520 #include <linux/module.h>
521 #include <linux/sched.h>
522 #include <linux/types.h>
523 #include <linux/fcntl.h>
524 #include <linux/interrupt.h>
525 #include <linux/ptrace.h>
526 #include <linux/ioport.h>
527 #include <linux/in.h>
528 #include <net/ip.h>
529 #include <linux/ip.h>
530 #include <linux/tcp.h>
531 #include <linux/udp.h>
532 #include <linux/slab.h>
533 #include <linux/string.h>
534 #include <linux/init.h>
535 #include <linux/timer.h>
536 #include <linux/socket.h>
537 #include <linux/ctype.h>
538 #include <linux/inet.h>
539 #include <linux/bitops.h>
540 #include <asm/system.h>
541 #include <asm/io.h>
542 #include <asm/dma.h>
543 #include <asm/uaccess.h>
544 #include <linux/errno.h>
545 #include <linux/netdevice.h>
546 #include <linux/inetdevice.h>
547 #include <linux/etherdevice.h>
548 #include <linux/skbuff.h>
549 #include <net/sock.h>
550 #include <linux/rtnetlink.h>
551 #include <linux/proc_fs.h>
552 #include <linux/seq_file.h>
553 #include <linux/smp.h>
554 #include <linux/if_ether.h>
555 #include <net/arp.h>
556 #include <linux/mii.h>
557 #include <linux/ethtool.h>
558 #include <linux/if_vlan.h>
559 #include <linux/if_bonding.h>
560 #include <net/route.h>
561 #include "bonding.h"
562 #include "bond_3ad.h"
563 #include "bond_alb.h"
564
565 /*---------------------------- Module parameters ----------------------------*/
566
567 /* monitor all links that often (in milliseconds). <=0 disables monitoring */
568 #define BOND_LINK_MON_INTERV    0
569 #define BOND_LINK_ARP_INTERV    0
570
571 static int max_bonds    = BOND_DEFAULT_MAX_BONDS;
572 static int miimon       = BOND_LINK_MON_INTERV;
573 static int updelay      = 0;
574 static int downdelay    = 0;
575 static int use_carrier  = 1;
576 static char *mode       = NULL;
577 static char *primary    = NULL;
578 static char *lacp_rate  = NULL;
579 static char *xmit_hash_policy = NULL;
580 static int arp_interval = BOND_LINK_ARP_INTERV;
581 static char *arp_ip_target[BOND_MAX_ARP_TARGETS] = { NULL, };
582 struct bond_params bonding_defaults;
583
584 module_param(max_bonds, int, 0);
585 MODULE_PARM_DESC(max_bonds, "Max number of bonded devices");
586 module_param(miimon, int, 0);
587 MODULE_PARM_DESC(miimon, "Link check interval in milliseconds");
588 module_param(updelay, int, 0);
589 MODULE_PARM_DESC(updelay, "Delay before considering link up, in milliseconds");
590 module_param(downdelay, int, 0);
591 MODULE_PARM_DESC(downdelay, "Delay before considering link down, "
592                             "in milliseconds");
593 module_param(use_carrier, int, 0);
594 MODULE_PARM_DESC(use_carrier, "Use netif_carrier_ok (vs MII ioctls) in miimon; "
595                               "0 for off, 1 for on (default)");
596 module_param(mode, charp, 0);
597 MODULE_PARM_DESC(mode, "Mode of operation : 0 for balance-rr, "
598                        "1 for active-backup, 2 for balance-xor, "
599                        "3 for broadcast, 4 for 802.3ad, 5 for balance-tlb, "
600                        "6 for balance-alb");
601 module_param(primary, charp, 0);
602 MODULE_PARM_DESC(primary, "Primary network device to use");
603 module_param(lacp_rate, charp, 0);
604 MODULE_PARM_DESC(lacp_rate, "LACPDU tx rate to request from 802.3ad partner "
605                             "(slow/fast)");
606 module_param(xmit_hash_policy, charp, 0);
607 MODULE_PARM_DESC(xmit_hash_policy, "XOR hashing method: 0 for layer 2 (default)"
608                                    ", 1 for layer 3+4");
609 module_param(arp_interval, int, 0);
610 MODULE_PARM_DESC(arp_interval, "arp interval in milliseconds");
611 module_param_array(arp_ip_target, charp, NULL, 0);
612 MODULE_PARM_DESC(arp_ip_target, "arp targets in n.n.n.n form");
613
614 /*----------------------------- Global variables ----------------------------*/
615
616 static const char *version =
617         DRV_DESCRIPTION ": v" DRV_VERSION " (" DRV_RELDATE ")\n";
618
619 LIST_HEAD(bond_dev_list);
620
621 #ifdef CONFIG_PROC_FS
622 static struct proc_dir_entry *bond_proc_dir = NULL;
623 #endif
624
625 extern struct rw_semaphore bonding_rwsem;
626 static u32 arp_target[BOND_MAX_ARP_TARGETS] = { 0, } ;
627 static int arp_ip_count = 0;
628 static int bond_mode    = BOND_MODE_ROUNDROBIN;
629 static int xmit_hashtype= BOND_XMIT_POLICY_LAYER2;
630 static int lacp_fast    = 0;
631
632
633 struct bond_parm_tbl bond_lacp_tbl[] = {
634 {       "slow",         AD_LACP_SLOW},
635 {       "fast",         AD_LACP_FAST},
636 {       NULL,           -1},
637 };
638
639 struct bond_parm_tbl bond_mode_tbl[] = {
640 {       "balance-rr",           BOND_MODE_ROUNDROBIN},
641 {       "active-backup",        BOND_MODE_ACTIVEBACKUP},
642 {       "balance-xor",          BOND_MODE_XOR},
643 {       "broadcast",            BOND_MODE_BROADCAST},
644 {       "802.3ad",              BOND_MODE_8023AD},
645 {       "balance-tlb",          BOND_MODE_TLB},
646 {       "balance-alb",          BOND_MODE_ALB},
647 {       NULL,                   -1},
648 };
649
650 struct bond_parm_tbl xmit_hashtype_tbl[] = {
651 {       "layer2",               BOND_XMIT_POLICY_LAYER2},
652 {       "layer3+4",             BOND_XMIT_POLICY_LAYER34},
653 {       NULL,                   -1},
654 };
655
656 /*-------------------------- Forward declarations ---------------------------*/
657
658 static void bond_send_gratuitous_arp(struct bonding *bond);
659
660 /*---------------------------- General routines -----------------------------*/
661
662 const char *bond_mode_name(int mode)
663 {
664         switch (mode) {
665         case BOND_MODE_ROUNDROBIN :
666                 return "load balancing (round-robin)";
667         case BOND_MODE_ACTIVEBACKUP :
668                 return "fault-tolerance (active-backup)";
669         case BOND_MODE_XOR :
670                 return "load balancing (xor)";
671         case BOND_MODE_BROADCAST :
672                 return "fault-tolerance (broadcast)";
673         case BOND_MODE_8023AD:
674                 return "IEEE 802.3ad Dynamic link aggregation";
675         case BOND_MODE_TLB:
676                 return "transmit load balancing";
677         case BOND_MODE_ALB:
678                 return "adaptive load balancing";
679         default:
680                 return "unknown";
681         }
682 }
683
684 /*---------------------------------- VLAN -----------------------------------*/
685
686 /**
687  * bond_add_vlan - add a new vlan id on bond
688  * @bond: bond that got the notification
689  * @vlan_id: the vlan id to add
690  *
691  * Returns -ENOMEM if allocation failed.
692  */
693 static int bond_add_vlan(struct bonding *bond, unsigned short vlan_id)
694 {
695         struct vlan_entry *vlan;
696
697         dprintk("bond: %s, vlan id %d\n",
698                 (bond ? bond->dev->name: "None"), vlan_id);
699
700         vlan = kmalloc(sizeof(struct vlan_entry), GFP_KERNEL);
701         if (!vlan) {
702                 return -ENOMEM;
703         }
704
705         INIT_LIST_HEAD(&vlan->vlan_list);
706         vlan->vlan_id = vlan_id;
707         vlan->vlan_ip = 0;
708
709         write_lock_bh(&bond->lock);
710
711         list_add_tail(&vlan->vlan_list, &bond->vlan_list);
712
713         write_unlock_bh(&bond->lock);
714
715         dprintk("added VLAN ID %d on bond %s\n", vlan_id, bond->dev->name);
716
717         return 0;
718 }
719
720 /**
721  * bond_del_vlan - delete a vlan id from bond
722  * @bond: bond that got the notification
723  * @vlan_id: the vlan id to delete
724  *
725  * returns -ENODEV if @vlan_id was not found in @bond.
726  */
727 static int bond_del_vlan(struct bonding *bond, unsigned short vlan_id)
728 {
729         struct vlan_entry *vlan, *next;
730         int res = -ENODEV;
731
732         dprintk("bond: %s, vlan id %d\n", bond->dev->name, vlan_id);
733
734         write_lock_bh(&bond->lock);
735
736         list_for_each_entry_safe(vlan, next, &bond->vlan_list, vlan_list) {
737                 if (vlan->vlan_id == vlan_id) {
738                         list_del(&vlan->vlan_list);
739
740                         if ((bond->params.mode == BOND_MODE_TLB) ||
741                             (bond->params.mode == BOND_MODE_ALB)) {
742                                 bond_alb_clear_vlan(bond, vlan_id);
743                         }
744
745                         dprintk("removed VLAN ID %d from bond %s\n", vlan_id,
746                                 bond->dev->name);
747
748                         kfree(vlan);
749
750                         if (list_empty(&bond->vlan_list) &&
751                             (bond->slave_cnt == 0)) {
752                                 /* Last VLAN removed and no slaves, so
753                                  * restore block on adding VLANs. This will
754                                  * be removed once new slaves that are not
755                                  * VLAN challenged will be added.
756                                  */
757                                 bond->dev->features |= NETIF_F_VLAN_CHALLENGED;
758                         }
759
760                         res = 0;
761                         goto out;
762                 }
763         }
764
765         dprintk("couldn't find VLAN ID %d in bond %s\n", vlan_id,
766                 bond->dev->name);
767
768 out:
769         write_unlock_bh(&bond->lock);
770         return res;
771 }
772
773 /**
774  * bond_has_challenged_slaves
775  * @bond: the bond we're working on
776  *
777  * Searches the slave list. Returns 1 if a vlan challenged slave
778  * was found, 0 otherwise.
779  *
780  * Assumes bond->lock is held.
781  */
782 static int bond_has_challenged_slaves(struct bonding *bond)
783 {
784         struct slave *slave;
785         int i;
786
787         bond_for_each_slave(bond, slave, i) {
788                 if (slave->dev->features & NETIF_F_VLAN_CHALLENGED) {
789                         dprintk("found VLAN challenged slave - %s\n",
790                                 slave->dev->name);
791                         return 1;
792                 }
793         }
794
795         dprintk("no VLAN challenged slaves found\n");
796         return 0;
797 }
798
799 /**
800  * bond_next_vlan - safely skip to the next item in the vlans list.
801  * @bond: the bond we're working on
802  * @curr: item we're advancing from
803  *
804  * Returns %NULL if list is empty, bond->next_vlan if @curr is %NULL,
805  * or @curr->next otherwise (even if it is @curr itself again).
806  * 
807  * Caller must hold bond->lock
808  */
809 struct vlan_entry *bond_next_vlan(struct bonding *bond, struct vlan_entry *curr)
810 {
811         struct vlan_entry *next, *last;
812
813         if (list_empty(&bond->vlan_list)) {
814                 return NULL;
815         }
816
817         if (!curr) {
818                 next = list_entry(bond->vlan_list.next,
819                                   struct vlan_entry, vlan_list);
820         } else {
821                 last = list_entry(bond->vlan_list.prev,
822                                   struct vlan_entry, vlan_list);
823                 if (last == curr) {
824                         next = list_entry(bond->vlan_list.next,
825                                           struct vlan_entry, vlan_list);
826                 } else {
827                         next = list_entry(curr->vlan_list.next,
828                                           struct vlan_entry, vlan_list);
829                 }
830         }
831
832         return next;
833 }
834
835 /**
836  * bond_dev_queue_xmit - Prepare skb for xmit.
837  * 
838  * @bond: bond device that got this skb for tx.
839  * @skb: hw accel VLAN tagged skb to transmit
840  * @slave_dev: slave that is supposed to xmit this skbuff
841  * 
842  * When the bond gets an skb to transmit that is
843  * already hardware accelerated VLAN tagged, and it
844  * needs to relay this skb to a slave that is not
845  * hw accel capable, the skb needs to be "unaccelerated",
846  * i.e. strip the hwaccel tag and re-insert it as part
847  * of the payload.
848  */
849 int bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb, struct net_device *slave_dev)
850 {
851         unsigned short vlan_id;
852
853         if (!list_empty(&bond->vlan_list) &&
854             !(slave_dev->features & NETIF_F_HW_VLAN_TX) &&
855             vlan_get_tag(skb, &vlan_id) == 0) {
856                 skb->dev = slave_dev;
857                 skb = vlan_put_tag(skb, vlan_id);
858                 if (!skb) {
859                         /* vlan_put_tag() frees the skb in case of error,
860                          * so return success here so the calling functions
861                          * won't attempt to free is again.
862                          */
863                         return 0;
864                 }
865         } else {
866                 skb->dev = slave_dev;
867         }
868
869         skb->priority = 1;
870         dev_queue_xmit(skb);
871
872         return 0;
873 }
874
875 /*
876  * In the following 3 functions, bond_vlan_rx_register(), bond_vlan_rx_add_vid
877  * and bond_vlan_rx_kill_vid, We don't protect the slave list iteration with a
878  * lock because:
879  * a. This operation is performed in IOCTL context,
880  * b. The operation is protected by the RTNL semaphore in the 8021q code,
881  * c. Holding a lock with BH disabled while directly calling a base driver
882  *    entry point is generally a BAD idea.
883  * 
884  * The design of synchronization/protection for this operation in the 8021q
885  * module is good for one or more VLAN devices over a single physical device
886  * and cannot be extended for a teaming solution like bonding, so there is a
887  * potential race condition here where a net device from the vlan group might
888  * be referenced (either by a base driver or the 8021q code) while it is being
889  * removed from the system. However, it turns out we're not making matters
890  * worse, and if it works for regular VLAN usage it will work here too.
891 */
892
893 /**
894  * bond_vlan_rx_register - Propagates registration to slaves
895  * @bond_dev: bonding net device that got called
896  * @grp: vlan group being registered
897  */
898 static void bond_vlan_rx_register(struct net_device *bond_dev, struct vlan_group *grp)
899 {
900         struct bonding *bond = bond_dev->priv;
901         struct slave *slave;
902         int i;
903
904         bond->vlgrp = grp;
905
906         bond_for_each_slave(bond, slave, i) {
907                 struct net_device *slave_dev = slave->dev;
908
909                 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
910                     slave_dev->vlan_rx_register) {
911                         slave_dev->vlan_rx_register(slave_dev, grp);
912                 }
913         }
914 }
915
916 /**
917  * bond_vlan_rx_add_vid - Propagates adding an id to slaves
918  * @bond_dev: bonding net device that got called
919  * @vid: vlan id being added
920  */
921 static void bond_vlan_rx_add_vid(struct net_device *bond_dev, uint16_t vid)
922 {
923         struct bonding *bond = bond_dev->priv;
924         struct slave *slave;
925         int i, res;
926
927         bond_for_each_slave(bond, slave, i) {
928                 struct net_device *slave_dev = slave->dev;
929
930                 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
931                     slave_dev->vlan_rx_add_vid) {
932                         slave_dev->vlan_rx_add_vid(slave_dev, vid);
933                 }
934         }
935
936         res = bond_add_vlan(bond, vid);
937         if (res) {
938                 printk(KERN_ERR DRV_NAME
939                        ": %s: Error: Failed to add vlan id %d\n",
940                        bond_dev->name, vid);
941         }
942 }
943
944 /**
945  * bond_vlan_rx_kill_vid - Propagates deleting an id to slaves
946  * @bond_dev: bonding net device that got called
947  * @vid: vlan id being removed
948  */
949 static void bond_vlan_rx_kill_vid(struct net_device *bond_dev, uint16_t vid)
950 {
951         struct bonding *bond = bond_dev->priv;
952         struct slave *slave;
953         struct net_device *vlan_dev;
954         int i, res;
955
956         bond_for_each_slave(bond, slave, i) {
957                 struct net_device *slave_dev = slave->dev;
958
959                 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
960                     slave_dev->vlan_rx_kill_vid) {
961                         /* Save and then restore vlan_dev in the grp array,
962                          * since the slave's driver might clear it.
963                          */
964                         vlan_dev = bond->vlgrp->vlan_devices[vid];
965                         slave_dev->vlan_rx_kill_vid(slave_dev, vid);
966                         bond->vlgrp->vlan_devices[vid] = vlan_dev;
967                 }
968         }
969
970         res = bond_del_vlan(bond, vid);
971         if (res) {
972                 printk(KERN_ERR DRV_NAME
973                        ": %s: Error: Failed to remove vlan id %d\n",
974                        bond_dev->name, vid);
975         }
976 }
977
978 static void bond_add_vlans_on_slave(struct bonding *bond, struct net_device *slave_dev)
979 {
980         struct vlan_entry *vlan;
981
982         write_lock_bh(&bond->lock);
983
984         if (list_empty(&bond->vlan_list)) {
985                 goto out;
986         }
987
988         if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
989             slave_dev->vlan_rx_register) {
990                 slave_dev->vlan_rx_register(slave_dev, bond->vlgrp);
991         }
992
993         if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
994             !(slave_dev->vlan_rx_add_vid)) {
995                 goto out;
996         }
997
998         list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
999                 slave_dev->vlan_rx_add_vid(slave_dev, vlan->vlan_id);
1000         }
1001
1002 out:
1003         write_unlock_bh(&bond->lock);
1004 }
1005
1006 static void bond_del_vlans_from_slave(struct bonding *bond, struct net_device *slave_dev)
1007 {
1008         struct vlan_entry *vlan;
1009         struct net_device *vlan_dev;
1010
1011         write_lock_bh(&bond->lock);
1012
1013         if (list_empty(&bond->vlan_list)) {
1014                 goto out;
1015         }
1016
1017         if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
1018             !(slave_dev->vlan_rx_kill_vid)) {
1019                 goto unreg;
1020         }
1021
1022         list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
1023                 /* Save and then restore vlan_dev in the grp array,
1024                  * since the slave's driver might clear it.
1025                  */
1026                 vlan_dev = bond->vlgrp->vlan_devices[vlan->vlan_id];
1027                 slave_dev->vlan_rx_kill_vid(slave_dev, vlan->vlan_id);
1028                 bond->vlgrp->vlan_devices[vlan->vlan_id] = vlan_dev;
1029         }
1030
1031 unreg:
1032         if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
1033             slave_dev->vlan_rx_register) {
1034                 slave_dev->vlan_rx_register(slave_dev, NULL);
1035         }
1036
1037 out:
1038         write_unlock_bh(&bond->lock);
1039 }
1040
1041 /*------------------------------- Link status -------------------------------*/
1042
1043 /*
1044  * Get link speed and duplex from the slave's base driver
1045  * using ethtool. If for some reason the call fails or the
1046  * values are invalid, fake speed and duplex to 100/Full
1047  * and return error.
1048  */
1049 static int bond_update_speed_duplex(struct slave *slave)
1050 {
1051         struct net_device *slave_dev = slave->dev;
1052         static int (* ioctl)(struct net_device *, struct ifreq *, int);
1053         struct ifreq ifr;
1054         struct ethtool_cmd etool;
1055
1056         /* Fake speed and duplex */
1057         slave->speed = SPEED_100;
1058         slave->duplex = DUPLEX_FULL;
1059
1060         if (slave_dev->ethtool_ops) {
1061                 u32 res;
1062
1063                 if (!slave_dev->ethtool_ops->get_settings) {
1064                         return -1;
1065                 }
1066
1067                 res = slave_dev->ethtool_ops->get_settings(slave_dev, &etool);
1068                 if (res < 0) {
1069                         return -1;
1070                 }
1071
1072                 goto verify;
1073         }
1074
1075         ioctl = slave_dev->do_ioctl;
1076         strncpy(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
1077         etool.cmd = ETHTOOL_GSET;
1078         ifr.ifr_data = (char*)&etool;
1079         if (!ioctl || (IOCTL(slave_dev, &ifr, SIOCETHTOOL) < 0)) {
1080                 return -1;
1081         }
1082
1083 verify:
1084         switch (etool.speed) {
1085         case SPEED_10:
1086         case SPEED_100:
1087         case SPEED_1000:
1088                 break;
1089         default:
1090                 return -1;
1091         }
1092
1093         switch (etool.duplex) {
1094         case DUPLEX_FULL:
1095         case DUPLEX_HALF:
1096                 break;
1097         default:
1098                 return -1;
1099         }
1100
1101         slave->speed = etool.speed;
1102         slave->duplex = etool.duplex;
1103
1104         return 0;
1105 }
1106
1107 /*
1108  * if <dev> supports MII link status reporting, check its link status.
1109  *
1110  * We either do MII/ETHTOOL ioctls, or check netif_carrier_ok(),
1111  * depening upon the setting of the use_carrier parameter.
1112  *
1113  * Return either BMSR_LSTATUS, meaning that the link is up (or we
1114  * can't tell and just pretend it is), or 0, meaning that the link is
1115  * down.
1116  *
1117  * If reporting is non-zero, instead of faking link up, return -1 if
1118  * both ETHTOOL and MII ioctls fail (meaning the device does not
1119  * support them).  If use_carrier is set, return whatever it says.
1120  * It'd be nice if there was a good way to tell if a driver supports
1121  * netif_carrier, but there really isn't.
1122  */
1123 static int bond_check_dev_link(struct bonding *bond, struct net_device *slave_dev, int reporting)
1124 {
1125         static int (* ioctl)(struct net_device *, struct ifreq *, int);
1126         struct ifreq ifr;
1127         struct mii_ioctl_data *mii;
1128         struct ethtool_value etool;
1129
1130         if (bond->params.use_carrier) {
1131                 return netif_carrier_ok(slave_dev) ? BMSR_LSTATUS : 0;
1132         }
1133
1134         ioctl = slave_dev->do_ioctl;
1135         if (ioctl) {
1136                 /* TODO: set pointer to correct ioctl on a per team member */
1137                 /*       bases to make this more efficient. that is, once  */
1138                 /*       we determine the correct ioctl, we will always    */
1139                 /*       call it and not the others for that team          */
1140                 /*       member.                                           */
1141
1142                 /*
1143                  * We cannot assume that SIOCGMIIPHY will also read a
1144                  * register; not all network drivers (e.g., e100)
1145                  * support that.
1146                  */
1147
1148                 /* Yes, the mii is overlaid on the ifreq.ifr_ifru */
1149                 strncpy(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
1150                 mii = if_mii(&ifr);
1151                 if (IOCTL(slave_dev, &ifr, SIOCGMIIPHY) == 0) {
1152                         mii->reg_num = MII_BMSR;
1153                         if (IOCTL(slave_dev, &ifr, SIOCGMIIREG) == 0) {
1154                                 return (mii->val_out & BMSR_LSTATUS);
1155                         }
1156                 }
1157         }
1158
1159         /* try SIOCETHTOOL ioctl, some drivers cache ETHTOOL_GLINK */
1160         /* for a period of time so we attempt to get link status   */
1161         /* from it last if the above MII ioctls fail...            */
1162         if (slave_dev->ethtool_ops) {
1163                 if (slave_dev->ethtool_ops->get_link) {
1164                         u32 link;
1165
1166                         link = slave_dev->ethtool_ops->get_link(slave_dev);
1167
1168                         return link ? BMSR_LSTATUS : 0;
1169                 }
1170         }
1171
1172         if (ioctl) {
1173                 strncpy(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
1174                 etool.cmd = ETHTOOL_GLINK;
1175                 ifr.ifr_data = (char*)&etool;
1176                 if (IOCTL(slave_dev, &ifr, SIOCETHTOOL) == 0) {
1177                         if (etool.data == 1) {
1178                                 return BMSR_LSTATUS;
1179                         } else {
1180                                 dprintk("SIOCETHTOOL shows link down\n");
1181                                 return 0;
1182                         }
1183                 }
1184         }
1185
1186         /*
1187          * If reporting, report that either there's no dev->do_ioctl,
1188          * or both SIOCGMIIREG and SIOCETHTOOL failed (meaning that we
1189          * cannot report link status).  If not reporting, pretend
1190          * we're ok.
1191          */
1192         return (reporting ? -1 : BMSR_LSTATUS);
1193 }
1194
1195 /*----------------------------- Multicast list ------------------------------*/
1196
1197 /*
1198  * Returns 0 if dmi1 and dmi2 are the same, non-0 otherwise
1199  */
1200 static inline int bond_is_dmi_same(struct dev_mc_list *dmi1, struct dev_mc_list *dmi2)
1201 {
1202         return memcmp(dmi1->dmi_addr, dmi2->dmi_addr, dmi1->dmi_addrlen) == 0 &&
1203                         dmi1->dmi_addrlen == dmi2->dmi_addrlen;
1204 }
1205
1206 /*
1207  * returns dmi entry if found, NULL otherwise
1208  */
1209 static struct dev_mc_list *bond_mc_list_find_dmi(struct dev_mc_list *dmi, struct dev_mc_list *mc_list)
1210 {
1211         struct dev_mc_list *idmi;
1212
1213         for (idmi = mc_list; idmi; idmi = idmi->next) {
1214                 if (bond_is_dmi_same(dmi, idmi)) {
1215                         return idmi;
1216                 }
1217         }
1218
1219         return NULL;
1220 }
1221
1222 /*
1223  * Push the promiscuity flag down to appropriate slaves
1224  */
1225 static void bond_set_promiscuity(struct bonding *bond, int inc)
1226 {
1227         if (USES_PRIMARY(bond->params.mode)) {
1228                 /* write lock already acquired */
1229                 if (bond->curr_active_slave) {
1230                         dev_set_promiscuity(bond->curr_active_slave->dev, inc);
1231                 }
1232         } else {
1233                 struct slave *slave;
1234                 int i;
1235                 bond_for_each_slave(bond, slave, i) {
1236                         dev_set_promiscuity(slave->dev, inc);
1237                 }
1238         }
1239 }
1240
1241 /*
1242  * Push the allmulti flag down to all slaves
1243  */
1244 static void bond_set_allmulti(struct bonding *bond, int inc)
1245 {
1246         if (USES_PRIMARY(bond->params.mode)) {
1247                 /* write lock already acquired */
1248                 if (bond->curr_active_slave) {
1249                         dev_set_allmulti(bond->curr_active_slave->dev, inc);
1250                 }
1251         } else {
1252                 struct slave *slave;
1253                 int i;
1254                 bond_for_each_slave(bond, slave, i) {
1255                         dev_set_allmulti(slave->dev, inc);
1256                 }
1257         }
1258 }
1259
1260 /*
1261  * Add a Multicast address to slaves
1262  * according to mode
1263  */
1264 static void bond_mc_add(struct bonding *bond, void *addr, int alen)
1265 {
1266         if (USES_PRIMARY(bond->params.mode)) {
1267                 /* write lock already acquired */
1268                 if (bond->curr_active_slave) {
1269                         dev_mc_add(bond->curr_active_slave->dev, addr, alen, 0);
1270                 }
1271         } else {
1272                 struct slave *slave;
1273                 int i;
1274                 bond_for_each_slave(bond, slave, i) {
1275                         dev_mc_add(slave->dev, addr, alen, 0);
1276                 }
1277         }
1278 }
1279
1280 /*
1281  * Remove a multicast address from slave
1282  * according to mode
1283  */
1284 static void bond_mc_delete(struct bonding *bond, void *addr, int alen)
1285 {
1286         if (USES_PRIMARY(bond->params.mode)) {
1287                 /* write lock already acquired */
1288                 if (bond->curr_active_slave) {
1289                         dev_mc_delete(bond->curr_active_slave->dev, addr, alen, 0);
1290                 }
1291         } else {
1292                 struct slave *slave;
1293                 int i;
1294                 bond_for_each_slave(bond, slave, i) {
1295                         dev_mc_delete(slave->dev, addr, alen, 0);
1296                 }
1297         }
1298 }
1299
1300 /*
1301  * Totally destroys the mc_list in bond
1302  */
1303 static void bond_mc_list_destroy(struct bonding *bond)
1304 {
1305         struct dev_mc_list *dmi;
1306
1307         dmi = bond->mc_list;
1308         while (dmi) {
1309                 bond->mc_list = dmi->next;
1310                 kfree(dmi);
1311                 dmi = bond->mc_list;
1312         }
1313 }
1314
1315 /*
1316  * Copy all the Multicast addresses from src to the bonding device dst
1317  */
1318 static int bond_mc_list_copy(struct dev_mc_list *mc_list, struct bonding *bond,
1319                              gfp_t gfp_flag)
1320 {
1321         struct dev_mc_list *dmi, *new_dmi;
1322
1323         for (dmi = mc_list; dmi; dmi = dmi->next) {
1324                 new_dmi = kmalloc(sizeof(struct dev_mc_list), gfp_flag);
1325
1326                 if (!new_dmi) {
1327                         /* FIXME: Potential memory leak !!! */
1328                         return -ENOMEM;
1329                 }
1330
1331                 new_dmi->next = bond->mc_list;
1332                 bond->mc_list = new_dmi;
1333                 new_dmi->dmi_addrlen = dmi->dmi_addrlen;
1334                 memcpy(new_dmi->dmi_addr, dmi->dmi_addr, dmi->dmi_addrlen);
1335                 new_dmi->dmi_users = dmi->dmi_users;
1336                 new_dmi->dmi_gusers = dmi->dmi_gusers;
1337         }
1338
1339         return 0;
1340 }
1341
1342 /*
1343  * flush all members of flush->mc_list from device dev->mc_list
1344  */
1345 static void bond_mc_list_flush(struct net_device *bond_dev, struct net_device *slave_dev)
1346 {
1347         struct bonding *bond = bond_dev->priv;
1348         struct dev_mc_list *dmi;
1349
1350         for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
1351                 dev_mc_delete(slave_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
1352         }
1353
1354         if (bond->params.mode == BOND_MODE_8023AD) {
1355                 /* del lacpdu mc addr from mc list */
1356                 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
1357
1358                 dev_mc_delete(slave_dev, lacpdu_multicast, ETH_ALEN, 0);
1359         }
1360 }
1361
1362 /*--------------------------- Active slave change ---------------------------*/
1363
1364 /*
1365  * Update the mc list and multicast-related flags for the new and
1366  * old active slaves (if any) according to the multicast mode, and
1367  * promiscuous flags unconditionally.
1368  */
1369 static void bond_mc_swap(struct bonding *bond, struct slave *new_active, struct slave *old_active)
1370 {
1371         struct dev_mc_list *dmi;
1372
1373         if (!USES_PRIMARY(bond->params.mode)) {
1374                 /* nothing to do -  mc list is already up-to-date on
1375                  * all slaves
1376                  */
1377                 return;
1378         }
1379
1380         if (old_active) {
1381                 if (bond->dev->flags & IFF_PROMISC) {
1382                         dev_set_promiscuity(old_active->dev, -1);
1383                 }
1384
1385                 if (bond->dev->flags & IFF_ALLMULTI) {
1386                         dev_set_allmulti(old_active->dev, -1);
1387                 }
1388
1389                 for (dmi = bond->dev->mc_list; dmi; dmi = dmi->next) {
1390                         dev_mc_delete(old_active->dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
1391                 }
1392         }
1393
1394         if (new_active) {
1395                 if (bond->dev->flags & IFF_PROMISC) {
1396                         dev_set_promiscuity(new_active->dev, 1);
1397                 }
1398
1399                 if (bond->dev->flags & IFF_ALLMULTI) {
1400                         dev_set_allmulti(new_active->dev, 1);
1401                 }
1402
1403                 for (dmi = bond->dev->mc_list; dmi; dmi = dmi->next) {
1404                         dev_mc_add(new_active->dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
1405                 }
1406         }
1407 }
1408
1409 /**
1410  * find_best_interface - select the best available slave to be the active one
1411  * @bond: our bonding struct
1412  *
1413  * Warning: Caller must hold curr_slave_lock for writing.
1414  */
1415 static struct slave *bond_find_best_slave(struct bonding *bond)
1416 {
1417         struct slave *new_active, *old_active;
1418         struct slave *bestslave = NULL;
1419         int mintime = bond->params.updelay;
1420         int i;
1421
1422         new_active = old_active = bond->curr_active_slave;
1423
1424         if (!new_active) { /* there were no active slaves left */
1425                 if (bond->slave_cnt > 0) {  /* found one slave */
1426                         new_active = bond->first_slave;
1427                 } else {
1428                         return NULL; /* still no slave, return NULL */
1429                 }
1430         }
1431
1432         /* first try the primary link; if arping, a link must tx/rx traffic
1433          * before it can be considered the curr_active_slave - also, we would skip
1434          * slaves between the curr_active_slave and primary_slave that may be up
1435          * and able to arp
1436          */
1437         if ((bond->primary_slave) &&
1438             (!bond->params.arp_interval) &&
1439             (IS_UP(bond->primary_slave->dev))) {
1440                 new_active = bond->primary_slave;
1441         }
1442
1443         /* remember where to stop iterating over the slaves */
1444         old_active = new_active;
1445
1446         bond_for_each_slave_from(bond, new_active, i, old_active) {
1447                 if (IS_UP(new_active->dev)) {
1448                         if (new_active->link == BOND_LINK_UP) {
1449                                 return new_active;
1450                         } else if (new_active->link == BOND_LINK_BACK) {
1451                                 /* link up, but waiting for stabilization */
1452                                 if (new_active->delay < mintime) {
1453                                         mintime = new_active->delay;
1454                                         bestslave = new_active;
1455                                 }
1456                         }
1457                 }
1458         }
1459
1460         return bestslave;
1461 }
1462
1463 /**
1464  * change_active_interface - change the active slave into the specified one
1465  * @bond: our bonding struct
1466  * @new: the new slave to make the active one
1467  *
1468  * Set the new slave to the bond's settings and unset them on the old
1469  * curr_active_slave.
1470  * Setting include flags, mc-list, promiscuity, allmulti, etc.
1471  *
1472  * If @new's link state is %BOND_LINK_BACK we'll set it to %BOND_LINK_UP,
1473  * because it is apparently the best available slave we have, even though its
1474  * updelay hasn't timed out yet.
1475  *
1476  * Warning: Caller must hold curr_slave_lock for writing.
1477  */
1478 void bond_change_active_slave(struct bonding *bond, struct slave *new_active)
1479 {
1480         struct slave *old_active = bond->curr_active_slave;
1481
1482         if (old_active == new_active) {
1483                 return;
1484         }
1485
1486         if (new_active) {
1487                 if (new_active->link == BOND_LINK_BACK) {
1488                         if (USES_PRIMARY(bond->params.mode)) {
1489                                 printk(KERN_INFO DRV_NAME
1490                                        ": %s: making interface %s the new "
1491                                        "active one %d ms earlier.\n",
1492                                        bond->dev->name, new_active->dev->name,
1493                                        (bond->params.updelay - new_active->delay) * bond->params.miimon);
1494                         }
1495
1496                         new_active->delay = 0;
1497                         new_active->link = BOND_LINK_UP;
1498                         new_active->jiffies = jiffies;
1499
1500                         if (bond->params.mode == BOND_MODE_8023AD) {
1501                                 bond_3ad_handle_link_change(new_active, BOND_LINK_UP);
1502                         }
1503
1504                         if ((bond->params.mode == BOND_MODE_TLB) ||
1505                             (bond->params.mode == BOND_MODE_ALB)) {
1506                                 bond_alb_handle_link_change(bond, new_active, BOND_LINK_UP);
1507                         }
1508                 } else {
1509                         if (USES_PRIMARY(bond->params.mode)) {
1510                                 printk(KERN_INFO DRV_NAME
1511                                        ": %s: making interface %s the new "
1512                                        "active one.\n",
1513                                        bond->dev->name, new_active->dev->name);
1514                         }
1515                 }
1516         }
1517
1518         if (USES_PRIMARY(bond->params.mode)) {
1519                 bond_mc_swap(bond, new_active, old_active);
1520         }
1521
1522         if ((bond->params.mode == BOND_MODE_TLB) ||
1523             (bond->params.mode == BOND_MODE_ALB)) {
1524                 bond_alb_handle_active_change(bond, new_active);
1525         } else {
1526                 bond->curr_active_slave = new_active;
1527         }
1528
1529         if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
1530                 if (old_active) {
1531                         bond_set_slave_inactive_flags(old_active);
1532                 }
1533
1534                 if (new_active) {
1535                         bond_set_slave_active_flags(new_active);
1536                 }
1537                 bond_send_gratuitous_arp(bond);
1538         }
1539 }
1540
1541 /**
1542  * bond_select_active_slave - select a new active slave, if needed
1543  * @bond: our bonding struct
1544  *
1545  * This functions shoud be called when one of the following occurs:
1546  * - The old curr_active_slave has been released or lost its link.
1547  * - The primary_slave has got its link back.
1548  * - A slave has got its link back and there's no old curr_active_slave.
1549  *
1550  * Warning: Caller must hold curr_slave_lock for writing.
1551  */
1552 void bond_select_active_slave(struct bonding *bond)
1553 {
1554         struct slave *best_slave;
1555
1556         best_slave = bond_find_best_slave(bond);
1557         if (best_slave != bond->curr_active_slave) {
1558                 bond_change_active_slave(bond, best_slave);
1559         }
1560 }
1561
1562 /*--------------------------- slave list handling ---------------------------*/
1563
1564 /*
1565  * This function attaches the slave to the end of list.
1566  *
1567  * bond->lock held for writing by caller.
1568  */
1569 static void bond_attach_slave(struct bonding *bond, struct slave *new_slave)
1570 {
1571         if (bond->first_slave == NULL) { /* attaching the first slave */
1572                 new_slave->next = new_slave;
1573                 new_slave->prev = new_slave;
1574                 bond->first_slave = new_slave;
1575         } else {
1576                 new_slave->next = bond->first_slave;
1577                 new_slave->prev = bond->first_slave->prev;
1578                 new_slave->next->prev = new_slave;
1579                 new_slave->prev->next = new_slave;
1580         }
1581
1582         bond->slave_cnt++;
1583 }
1584
1585 /*
1586  * This function detaches the slave from the list.
1587  * WARNING: no check is made to verify if the slave effectively
1588  * belongs to <bond>.
1589  * Nothing is freed on return, structures are just unchained.
1590  * If any slave pointer in bond was pointing to <slave>,
1591  * it should be changed by the calling function.
1592  *
1593  * bond->lock held for writing by caller.
1594  */
1595 static void bond_detach_slave(struct bonding *bond, struct slave *slave)
1596 {
1597         if (slave->next) {
1598                 slave->next->prev = slave->prev;
1599         }
1600
1601         if (slave->prev) {
1602                 slave->prev->next = slave->next;
1603         }
1604
1605         if (bond->first_slave == slave) { /* slave is the first slave */
1606                 if (bond->slave_cnt > 1) { /* there are more slave */
1607                         bond->first_slave = slave->next;
1608                 } else {
1609                         bond->first_slave = NULL; /* slave was the last one */
1610                 }
1611         }
1612
1613         slave->next = NULL;
1614         slave->prev = NULL;
1615         bond->slave_cnt--;
1616 }
1617
1618 /*---------------------------------- IOCTL ----------------------------------*/
1619
1620 int bond_sethwaddr(struct net_device *bond_dev, struct net_device *slave_dev)
1621 {
1622         dprintk("bond_dev=%p\n", bond_dev);
1623         dprintk("slave_dev=%p\n", slave_dev);
1624         dprintk("slave_dev->addr_len=%d\n", slave_dev->addr_len);
1625         memcpy(bond_dev->dev_addr, slave_dev->dev_addr, slave_dev->addr_len);
1626         return 0;
1627 }
1628
1629 #define BOND_INTERSECT_FEATURES \
1630         (NETIF_F_SG|NETIF_F_IP_CSUM|NETIF_F_NO_CSUM|NETIF_F_HW_CSUM)
1631
1632 /* 
1633  * Compute the common dev->feature set available to all slaves.  Some
1634  * feature bits are managed elsewhere, so preserve feature bits set on
1635  * master device that are not part of the examined set.
1636  */
1637 static int bond_compute_features(struct bonding *bond)
1638 {
1639         unsigned long features = BOND_INTERSECT_FEATURES;
1640         struct slave *slave;
1641         struct net_device *bond_dev = bond->dev;
1642         int i;
1643
1644         bond_for_each_slave(bond, slave, i)
1645                 features &= (slave->dev->features & BOND_INTERSECT_FEATURES);
1646
1647         if ((features & NETIF_F_SG) && 
1648             !(features & (NETIF_F_IP_CSUM |
1649                           NETIF_F_NO_CSUM |
1650                           NETIF_F_HW_CSUM)))
1651                 features &= ~NETIF_F_SG;
1652
1653         features |= (bond_dev->features & ~BOND_INTERSECT_FEATURES);
1654         bond_dev->features = features;
1655
1656         return 0;
1657 }
1658
1659 /* enslave device <slave> to bond device <master> */
1660 int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
1661 {
1662         struct bonding *bond = bond_dev->priv;
1663         struct slave *new_slave = NULL;
1664         struct dev_mc_list *dmi;
1665         struct sockaddr addr;
1666         int link_reporting;
1667         int old_features = bond_dev->features;
1668         int res = 0;
1669
1670         if (!bond->params.use_carrier && slave_dev->ethtool_ops == NULL &&
1671                 slave_dev->do_ioctl == NULL) {
1672                 printk(KERN_WARNING DRV_NAME
1673                        ": %s: Warning: no link monitoring support for %s\n",
1674                        bond_dev->name, slave_dev->name);
1675         }
1676
1677         /* bond must be initialized by bond_open() before enslaving */
1678         if (!(bond_dev->flags & IFF_UP)) {
1679                 dprintk("Error, master_dev is not up\n");
1680                 return -EPERM;
1681         }
1682
1683         /* already enslaved */
1684         if (slave_dev->flags & IFF_SLAVE) {
1685                 dprintk("Error, Device was already enslaved\n");
1686                 return -EBUSY;
1687         }
1688
1689         /* vlan challenged mutual exclusion */
1690         /* no need to lock since we're protected by rtnl_lock */
1691         if (slave_dev->features & NETIF_F_VLAN_CHALLENGED) {
1692                 dprintk("%s: NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
1693                 if (!list_empty(&bond->vlan_list)) {
1694                         printk(KERN_ERR DRV_NAME
1695                                ": %s: Error: cannot enslave VLAN "
1696                                "challenged slave %s on VLAN enabled "
1697                                "bond %s\n", bond_dev->name, slave_dev->name,
1698                                bond_dev->name);
1699                         return -EPERM;
1700                 } else {
1701                         printk(KERN_WARNING DRV_NAME
1702                                ": %s: Warning: enslaved VLAN challenged "
1703                                "slave %s. Adding VLANs will be blocked as "
1704                                "long as %s is part of bond %s\n",
1705                                bond_dev->name, slave_dev->name, slave_dev->name,
1706                                bond_dev->name);
1707                         bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1708                 }
1709         } else {
1710                 dprintk("%s: ! NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
1711                 if (bond->slave_cnt == 0) {
1712                         /* First slave, and it is not VLAN challenged,
1713                          * so remove the block of adding VLANs over the bond.
1714                          */
1715                         bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
1716                 }
1717         }
1718
1719         /*
1720          * Old ifenslave binaries are no longer supported.  These can
1721          * be identified with moderate accurary by the state of the slave:
1722          * the current ifenslave will set the interface down prior to
1723          * enslaving it; the old ifenslave will not.
1724          */
1725         if ((slave_dev->flags & IFF_UP)) {
1726                 printk(KERN_ERR DRV_NAME ": %s is up. "
1727                        "This may be due to an out of date ifenslave.\n",
1728                        slave_dev->name);
1729                 res = -EPERM;
1730                 goto err_undo_flags;
1731         }
1732
1733         if (slave_dev->set_mac_address == NULL) {
1734                 printk(KERN_ERR DRV_NAME
1735                         ": %s: Error: The slave device you specified does "
1736                         "not support setting the MAC address. "
1737                         "Your kernel likely does not support slave "
1738                         "devices.\n", bond_dev->name);
1739                 res = -EOPNOTSUPP;
1740                 goto err_undo_flags;
1741         }
1742
1743         new_slave = kmalloc(sizeof(struct slave), GFP_KERNEL);
1744         if (!new_slave) {
1745                 res = -ENOMEM;
1746                 goto err_undo_flags;
1747         }
1748
1749         memset(new_slave, 0, sizeof(struct slave));
1750
1751         /* save slave's original flags before calling
1752          * netdev_set_master and dev_open
1753          */
1754         new_slave->original_flags = slave_dev->flags;
1755
1756         /*
1757          * Save slave's original ("permanent") mac address for modes
1758          * that need it, and for restoring it upon release, and then
1759          * set it to the master's address
1760          */
1761         memcpy(new_slave->perm_hwaddr, slave_dev->dev_addr, ETH_ALEN);
1762
1763         /*
1764          * Set slave to master's mac address.  The application already
1765          * set the master's mac address to that of the first slave
1766          */
1767         memcpy(addr.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
1768         addr.sa_family = slave_dev->type;
1769         res = dev_set_mac_address(slave_dev, &addr);
1770         if (res) {
1771                 dprintk("Error %d calling set_mac_address\n", res);
1772                 goto err_free;
1773         }
1774
1775         /* open the slave since the application closed it */
1776         res = dev_open(slave_dev);
1777         if (res) {
1778                 dprintk("Openning slave %s failed\n", slave_dev->name);
1779                 goto err_restore_mac;
1780         }
1781
1782         res = netdev_set_master(slave_dev, bond_dev);
1783         if (res) {
1784                 dprintk("Error %d calling netdev_set_master\n", res);
1785                 goto err_close;
1786         }
1787
1788         new_slave->dev = slave_dev;
1789
1790         if ((bond->params.mode == BOND_MODE_TLB) ||
1791             (bond->params.mode == BOND_MODE_ALB)) {
1792                 /* bond_alb_init_slave() must be called before all other stages since
1793                  * it might fail and we do not want to have to undo everything
1794                  */
1795                 res = bond_alb_init_slave(bond, new_slave);
1796                 if (res) {
1797                         goto err_unset_master;
1798                 }
1799         }
1800
1801         /* If the mode USES_PRIMARY, then the new slave gets the
1802          * master's promisc (and mc) settings only if it becomes the
1803          * curr_active_slave, and that is taken care of later when calling
1804          * bond_change_active()
1805          */
1806         if (!USES_PRIMARY(bond->params.mode)) {
1807                 /* set promiscuity level to new slave */
1808                 if (bond_dev->flags & IFF_PROMISC) {
1809                         dev_set_promiscuity(slave_dev, 1);
1810                 }
1811
1812                 /* set allmulti level to new slave */
1813                 if (bond_dev->flags & IFF_ALLMULTI) {
1814                         dev_set_allmulti(slave_dev, 1);
1815                 }
1816
1817                 /* upload master's mc_list to new slave */
1818                 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
1819                         dev_mc_add (slave_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
1820                 }
1821         }
1822
1823         if (bond->params.mode == BOND_MODE_8023AD) {
1824                 /* add lacpdu mc addr to mc list */
1825                 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
1826
1827                 dev_mc_add(slave_dev, lacpdu_multicast, ETH_ALEN, 0);
1828         }
1829
1830         bond_add_vlans_on_slave(bond, slave_dev);
1831
1832         write_lock_bh(&bond->lock);
1833
1834         bond_attach_slave(bond, new_slave);
1835
1836         new_slave->delay = 0;
1837         new_slave->link_failure_count = 0;
1838
1839         bond_compute_features(bond);
1840
1841         if (bond->params.miimon && !bond->params.use_carrier) {
1842                 link_reporting = bond_check_dev_link(bond, slave_dev, 1);
1843
1844                 if ((link_reporting == -1) && !bond->params.arp_interval) {
1845                         /*
1846                          * miimon is set but a bonded network driver
1847                          * does not support ETHTOOL/MII and
1848                          * arp_interval is not set.  Note: if
1849                          * use_carrier is enabled, we will never go
1850                          * here (because netif_carrier is always
1851                          * supported); thus, we don't need to change
1852                          * the messages for netif_carrier.
1853                          */
1854                         printk(KERN_WARNING DRV_NAME
1855                                ": %s: Warning: MII and ETHTOOL support not "
1856                                "available for interface %s, and "
1857                                "arp_interval/arp_ip_target module parameters "
1858                                "not specified, thus bonding will not detect "
1859                                "link failures! see bonding.txt for details.\n",
1860                                bond_dev->name, slave_dev->name);
1861                 } else if (link_reporting == -1) {
1862                         /* unable get link status using mii/ethtool */
1863                         printk(KERN_WARNING DRV_NAME
1864                                ": %s: Warning: can't get link status from "
1865                                "interface %s; the network driver associated "
1866                                "with this interface does not support MII or "
1867                                "ETHTOOL link status reporting, thus miimon "
1868                                "has no effect on this interface.\n",
1869                                bond_dev->name, slave_dev->name);
1870                 }
1871         }
1872
1873         /* check for initial state */
1874         if (!bond->params.miimon ||
1875             (bond_check_dev_link(bond, slave_dev, 0) == BMSR_LSTATUS)) {
1876                 if (bond->params.updelay) {
1877                         dprintk("Initial state of slave_dev is "
1878                                 "BOND_LINK_BACK\n");
1879                         new_slave->link  = BOND_LINK_BACK;
1880                         new_slave->delay = bond->params.updelay;
1881                 } else {
1882                         dprintk("Initial state of slave_dev is "
1883                                 "BOND_LINK_UP\n");
1884                         new_slave->link  = BOND_LINK_UP;
1885                 }
1886                 new_slave->jiffies = jiffies;
1887         } else {
1888                 dprintk("Initial state of slave_dev is "
1889                         "BOND_LINK_DOWN\n");
1890                 new_slave->link  = BOND_LINK_DOWN;
1891         }
1892
1893         if (bond_update_speed_duplex(new_slave) &&
1894             (new_slave->link != BOND_LINK_DOWN)) {
1895                 printk(KERN_WARNING DRV_NAME
1896                        ": %s: Warning: failed to get speed and duplex from %s, "
1897                        "assumed to be 100Mb/sec and Full.\n",
1898                        bond_dev->name, new_slave->dev->name);
1899
1900                 if (bond->params.mode == BOND_MODE_8023AD) {
1901                         printk(KERN_WARNING DRV_NAME
1902                                ": %s: Warning: Operation of 802.3ad mode requires ETHTOOL "
1903                                "support in base driver for proper aggregator "
1904                                "selection.\n", bond_dev->name);
1905                 }
1906         }
1907
1908         if (USES_PRIMARY(bond->params.mode) && bond->params.primary[0]) {
1909                 /* if there is a primary slave, remember it */
1910                 if (strcmp(bond->params.primary, new_slave->dev->name) == 0) {
1911                         bond->primary_slave = new_slave;
1912                 }
1913         }
1914
1915         switch (bond->params.mode) {
1916         case BOND_MODE_ACTIVEBACKUP:
1917                 /* if we're in active-backup mode, we need one and only one active
1918                  * interface. The backup interfaces will have their NOARP flag set
1919                  * because we need them to be completely deaf and not to respond to
1920                  * any ARP request on the network to avoid fooling a switch. Thus,
1921                  * since we guarantee that curr_active_slave always point to the last
1922                  * usable interface, we just have to verify this interface's flag.
1923                  */
1924                 if (((!bond->curr_active_slave) ||
1925                      (bond->curr_active_slave->dev->flags & IFF_NOARP)) &&
1926                     (new_slave->link != BOND_LINK_DOWN)) {
1927                         dprintk("This is the first active slave\n");
1928                         /* first slave or no active slave yet, and this link
1929                            is OK, so make this interface the active one */
1930                         bond_change_active_slave(bond, new_slave);
1931                 } else {
1932                         dprintk("This is just a backup slave\n");
1933                         bond_set_slave_inactive_flags(new_slave);
1934                 }
1935                 break;
1936         case BOND_MODE_8023AD:
1937                 /* in 802.3ad mode, the internal mechanism
1938                  * will activate the slaves in the selected
1939                  * aggregator
1940                  */
1941                 bond_set_slave_inactive_flags(new_slave);
1942                 /* if this is the first slave */
1943                 if (bond->slave_cnt == 1) {
1944                         SLAVE_AD_INFO(new_slave).id = 1;
1945                         /* Initialize AD with the number of times that the AD timer is called in 1 second
1946                          * can be called only after the mac address of the bond is set
1947                          */
1948                         bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL,
1949                                             bond->params.lacp_fast);
1950                 } else {
1951                         SLAVE_AD_INFO(new_slave).id =
1952                                 SLAVE_AD_INFO(new_slave->prev).id + 1;
1953                 }
1954
1955                 bond_3ad_bind_slave(new_slave);
1956                 break;
1957         case BOND_MODE_TLB:
1958         case BOND_MODE_ALB:
1959                 new_slave->state = BOND_STATE_ACTIVE;
1960                 if ((!bond->curr_active_slave) &&
1961                     (new_slave->link != BOND_LINK_DOWN)) {
1962                         /* first slave or no active slave yet, and this link
1963                          * is OK, so make this interface the active one
1964                          */
1965                         bond_change_active_slave(bond, new_slave);
1966                 }
1967                 break;
1968         default:
1969                 dprintk("This slave is always active in trunk mode\n");
1970
1971                 /* always active in trunk mode */
1972                 new_slave->state = BOND_STATE_ACTIVE;
1973
1974                 /* In trunking mode there is little meaning to curr_active_slave
1975                  * anyway (it holds no special properties of the bond device),
1976                  * so we can change it without calling change_active_interface()
1977                  */
1978                 if (!bond->curr_active_slave) {
1979                         bond->curr_active_slave = new_slave;
1980                 }
1981                 break;
1982         } /* switch(bond_mode) */
1983
1984         write_unlock_bh(&bond->lock);
1985
1986         res = bond_create_slave_symlinks(bond_dev, slave_dev);
1987         if (res)
1988                 goto err_unset_master;
1989
1990         printk(KERN_INFO DRV_NAME
1991                ": %s: enslaving %s as a%s interface with a%s link.\n",
1992                bond_dev->name, slave_dev->name,
1993                new_slave->state == BOND_STATE_ACTIVE ? "n active" : " backup",
1994                new_slave->link != BOND_LINK_DOWN ? "n up" : " down");
1995
1996         /* enslave is successful */
1997         return 0;
1998
1999 /* Undo stages on error */
2000 err_unset_master:
2001         netdev_set_master(slave_dev, NULL);
2002
2003 err_close:
2004         dev_close(slave_dev);
2005
2006 err_restore_mac:
2007         memcpy(addr.sa_data, new_slave->perm_hwaddr, ETH_ALEN);
2008         addr.sa_family = slave_dev->type;
2009         dev_set_mac_address(slave_dev, &addr);
2010
2011 err_free:
2012         kfree(new_slave);
2013
2014 err_undo_flags:
2015         bond_dev->features = old_features;
2016  
2017         return res;
2018 }
2019
2020 /*
2021  * Try to release the slave device <slave> from the bond device <master>
2022  * It is legal to access curr_active_slave without a lock because all the function
2023  * is write-locked.
2024  *
2025  * The rules for slave state should be:
2026  *   for Active/Backup:
2027  *     Active stays on all backups go down
2028  *   for Bonded connections:
2029  *     The first up interface should be left on and all others downed.
2030  */
2031 int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
2032 {
2033         struct bonding *bond = bond_dev->priv;
2034         struct slave *slave, *oldcurrent;
2035         struct sockaddr addr;
2036         int mac_addr_differ;
2037
2038         /* slave is not a slave or master is not master of this slave */
2039         if (!(slave_dev->flags & IFF_SLAVE) ||
2040             (slave_dev->master != bond_dev)) {
2041                 printk(KERN_ERR DRV_NAME
2042                        ": %s: Error: cannot release %s.\n",
2043                        bond_dev->name, slave_dev->name);
2044                 return -EINVAL;
2045         }
2046
2047         write_lock_bh(&bond->lock);
2048
2049         slave = bond_get_slave_by_dev(bond, slave_dev);
2050         if (!slave) {
2051                 /* not a slave of this bond */
2052                 printk(KERN_INFO DRV_NAME
2053                        ": %s: %s not enslaved\n",
2054                        bond_dev->name, slave_dev->name);
2055                 return -EINVAL;
2056         }
2057
2058         mac_addr_differ = memcmp(bond_dev->dev_addr,
2059                                  slave->perm_hwaddr,
2060                                  ETH_ALEN);
2061         if (!mac_addr_differ && (bond->slave_cnt > 1)) {
2062                 printk(KERN_WARNING DRV_NAME
2063                        ": %s: Warning: the permanent HWaddr of %s "
2064                        "- %02X:%02X:%02X:%02X:%02X:%02X - is "
2065                        "still in use by %s. Set the HWaddr of "
2066                        "%s to a different address to avoid "
2067                        "conflicts.\n",
2068                        bond_dev->name,
2069                        slave_dev->name,
2070                        slave->perm_hwaddr[0],
2071                        slave->perm_hwaddr[1],
2072                        slave->perm_hwaddr[2],
2073                        slave->perm_hwaddr[3],
2074                        slave->perm_hwaddr[4],
2075                        slave->perm_hwaddr[5],
2076                        bond_dev->name,
2077                        slave_dev->name);
2078         }
2079
2080         /* Inform AD package of unbinding of slave. */
2081         if (bond->params.mode == BOND_MODE_8023AD) {
2082                 /* must be called before the slave is
2083                  * detached from the list
2084                  */
2085                 bond_3ad_unbind_slave(slave);
2086         }
2087
2088         printk(KERN_INFO DRV_NAME
2089                ": %s: releasing %s interface %s\n",
2090                bond_dev->name,
2091                (slave->state == BOND_STATE_ACTIVE)
2092                ? "active" : "backup",
2093                slave_dev->name);
2094
2095         oldcurrent = bond->curr_active_slave;
2096
2097         bond->current_arp_slave = NULL;
2098
2099         /* release the slave from its bond */
2100         bond_detach_slave(bond, slave);
2101
2102         bond_compute_features(bond);
2103
2104         if (bond->primary_slave == slave) {
2105                 bond->primary_slave = NULL;
2106         }
2107
2108         if (oldcurrent == slave) {
2109                 bond_change_active_slave(bond, NULL);
2110         }
2111
2112         if ((bond->params.mode == BOND_MODE_TLB) ||
2113             (bond->params.mode == BOND_MODE_ALB)) {
2114                 /* Must be called only after the slave has been
2115                  * detached from the list and the curr_active_slave
2116                  * has been cleared (if our_slave == old_current),
2117                  * but before a new active slave is selected.
2118                  */
2119                 bond_alb_deinit_slave(bond, slave);
2120         }
2121
2122         if (oldcurrent == slave) {
2123                 bond_select_active_slave(bond);
2124
2125                 if (!bond->curr_active_slave) {
2126                         printk(KERN_INFO DRV_NAME
2127                                ": %s: now running without any active "
2128                                "interface !\n",
2129                                bond_dev->name);
2130                 }
2131         }
2132
2133         if (bond->slave_cnt == 0) {
2134                 /* if the last slave was removed, zero the mac address
2135                  * of the master so it will be set by the application
2136                  * to the mac address of the first slave
2137                  */
2138                 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
2139
2140                 if (list_empty(&bond->vlan_list)) {
2141                         bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
2142                 } else {
2143                         printk(KERN_WARNING DRV_NAME
2144                                ": %s: Warning: clearing HW address of %s while it "
2145                                "still has VLANs.\n",
2146                                bond_dev->name, bond_dev->name);
2147                         printk(KERN_WARNING DRV_NAME
2148                                ": %s: When re-adding slaves, make sure the bond's "
2149                                "HW address matches its VLANs'.\n",
2150                                bond_dev->name);
2151                 }
2152         } else if ((bond_dev->features & NETIF_F_VLAN_CHALLENGED) &&
2153                    !bond_has_challenged_slaves(bond)) {
2154                 printk(KERN_INFO DRV_NAME
2155                        ": %s: last VLAN challenged slave %s "
2156                        "left bond %s. VLAN blocking is removed\n",
2157                        bond_dev->name, slave_dev->name, bond_dev->name);
2158                 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
2159         }
2160
2161         write_unlock_bh(&bond->lock);
2162
2163         /* must do this from outside any spinlocks */
2164         bond_destroy_slave_symlinks(bond_dev, slave_dev);
2165
2166         bond_del_vlans_from_slave(bond, slave_dev);
2167
2168         /* If the mode USES_PRIMARY, then we should only remove its
2169          * promisc and mc settings if it was the curr_active_slave, but that was
2170          * already taken care of above when we detached the slave
2171          */
2172         if (!USES_PRIMARY(bond->params.mode)) {
2173                 /* unset promiscuity level from slave */
2174                 if (bond_dev->flags & IFF_PROMISC) {
2175                         dev_set_promiscuity(slave_dev, -1);
2176                 }
2177
2178                 /* unset allmulti level from slave */
2179                 if (bond_dev->flags & IFF_ALLMULTI) {
2180                         dev_set_allmulti(slave_dev, -1);
2181                 }
2182
2183                 /* flush master's mc_list from slave */
2184                 bond_mc_list_flush(bond_dev, slave_dev);
2185         }
2186
2187         netdev_set_master(slave_dev, NULL);
2188
2189         /* close slave before restoring its mac address */
2190         dev_close(slave_dev);
2191
2192         /* restore original ("permanent") mac address */
2193         memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
2194         addr.sa_family = slave_dev->type;
2195         dev_set_mac_address(slave_dev, &addr);
2196
2197         /* restore the original state of the
2198          * IFF_NOARP flag that might have been
2199          * set by bond_set_slave_inactive_flags()
2200          */
2201         if ((slave->original_flags & IFF_NOARP) == 0) {
2202                 slave_dev->flags &= ~IFF_NOARP;
2203         }
2204
2205         kfree(slave);
2206
2207         return 0;  /* deletion OK */
2208 }
2209
2210 /*
2211  * This function releases all slaves.
2212  */
2213 static int bond_release_all(struct net_device *bond_dev)
2214 {
2215         struct bonding *bond = bond_dev->priv;
2216         struct slave *slave;
2217         struct net_device *slave_dev;
2218         struct sockaddr addr;
2219
2220         write_lock_bh(&bond->lock);
2221
2222         if (bond->slave_cnt == 0) {
2223                 goto out;
2224         }
2225
2226         bond->current_arp_slave = NULL;
2227         bond->primary_slave = NULL;
2228         bond_change_active_slave(bond, NULL);
2229
2230         while ((slave = bond->first_slave) != NULL) {
2231                 /* Inform AD package of unbinding of slave
2232                  * before slave is detached from the list.
2233                  */
2234                 if (bond->params.mode == BOND_MODE_8023AD) {
2235                         bond_3ad_unbind_slave(slave);
2236                 }
2237
2238                 slave_dev = slave->dev;
2239                 bond_detach_slave(bond, slave);
2240
2241                 if ((bond->params.mode == BOND_MODE_TLB) ||
2242                     (bond->params.mode == BOND_MODE_ALB)) {
2243                         /* must be called only after the slave
2244                          * has been detached from the list
2245                          */
2246                         bond_alb_deinit_slave(bond, slave);
2247                 }
2248
2249                 bond_compute_features(bond);
2250
2251                 /* now that the slave is detached, unlock and perform
2252                  * all the undo steps that should not be called from
2253                  * within a lock.
2254                  */
2255                 write_unlock_bh(&bond->lock);
2256
2257                 bond_destroy_slave_symlinks(bond_dev, slave_dev);
2258                 bond_del_vlans_from_slave(bond, slave_dev);
2259
2260                 /* If the mode USES_PRIMARY, then we should only remove its
2261                  * promisc and mc settings if it was the curr_active_slave, but that was
2262                  * already taken care of above when we detached the slave
2263                  */
2264                 if (!USES_PRIMARY(bond->params.mode)) {
2265                         /* unset promiscuity level from slave */
2266                         if (bond_dev->flags & IFF_PROMISC) {
2267                                 dev_set_promiscuity(slave_dev, -1);
2268                         }
2269
2270                         /* unset allmulti level from slave */
2271                         if (bond_dev->flags & IFF_ALLMULTI) {
2272                                 dev_set_allmulti(slave_dev, -1);
2273                         }
2274
2275                         /* flush master's mc_list from slave */
2276                         bond_mc_list_flush(bond_dev, slave_dev);
2277                 }
2278
2279                 netdev_set_master(slave_dev, NULL);
2280
2281                 /* close slave before restoring its mac address */
2282                 dev_close(slave_dev);
2283
2284                 /* restore original ("permanent") mac address*/
2285                 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
2286                 addr.sa_family = slave_dev->type;
2287                 dev_set_mac_address(slave_dev, &addr);
2288
2289                 /* restore the original state of the IFF_NOARP flag that might have
2290                  * been set by bond_set_slave_inactive_flags()
2291                  */
2292                 if ((slave->original_flags & IFF_NOARP) == 0) {
2293                         slave_dev->flags &= ~IFF_NOARP;
2294                 }
2295
2296                 kfree(slave);
2297
2298                 /* re-acquire the lock before getting the next slave */
2299                 write_lock_bh(&bond->lock);
2300         }
2301
2302         /* zero the mac address of the master so it will be
2303          * set by the application to the mac address of the
2304          * first slave
2305          */
2306         memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
2307
2308         if (list_empty(&bond->vlan_list)) {
2309                 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
2310         } else {
2311                 printk(KERN_WARNING DRV_NAME
2312                        ": %s: Warning: clearing HW address of %s while it "
2313                        "still has VLANs.\n",
2314                        bond_dev->name, bond_dev->name);
2315                 printk(KERN_WARNING DRV_NAME
2316                        ": %s: When re-adding slaves, make sure the bond's "
2317                        "HW address matches its VLANs'.\n",
2318                        bond_dev->name);
2319         }
2320
2321         printk(KERN_INFO DRV_NAME
2322                ": %s: released all slaves\n",
2323                bond_dev->name);
2324
2325 out:
2326         write_unlock_bh(&bond->lock);
2327
2328         return 0;
2329 }
2330
2331 /*
2332  * This function changes the active slave to slave <slave_dev>.
2333  * It returns -EINVAL in the following cases.
2334  *  - <slave_dev> is not found in the list.
2335  *  - There is not active slave now.
2336  *  - <slave_dev> is already active.
2337  *  - The link state of <slave_dev> is not BOND_LINK_UP.
2338  *  - <slave_dev> is not running.
2339  * In these cases, this fuction does nothing.
2340  * In the other cases, currnt_slave pointer is changed and 0 is returned.
2341  */
2342 static int bond_ioctl_change_active(struct net_device *bond_dev, struct net_device *slave_dev)
2343 {
2344         struct bonding *bond = bond_dev->priv;
2345         struct slave *old_active = NULL;
2346         struct slave *new_active = NULL;
2347         int res = 0;
2348
2349         if (!USES_PRIMARY(bond->params.mode)) {
2350                 return -EINVAL;
2351         }
2352
2353         /* Verify that master_dev is indeed the master of slave_dev */
2354         if (!(slave_dev->flags & IFF_SLAVE) ||
2355             (slave_dev->master != bond_dev)) {
2356                 return -EINVAL;
2357         }
2358
2359         write_lock_bh(&bond->lock);
2360
2361         old_active = bond->curr_active_slave;
2362         new_active = bond_get_slave_by_dev(bond, slave_dev);
2363
2364         /*
2365          * Changing to the current active: do nothing; return success.
2366          */
2367         if (new_active && (new_active == old_active)) {
2368                 write_unlock_bh(&bond->lock);
2369                 return 0;
2370         }
2371
2372         if ((new_active) &&
2373             (old_active) &&
2374             (new_active->link == BOND_LINK_UP) &&
2375             IS_UP(new_active->dev)) {
2376                 bond_change_active_slave(bond, new_active);
2377         } else {
2378                 res = -EINVAL;
2379         }
2380
2381         write_unlock_bh(&bond->lock);
2382
2383         return res;
2384 }
2385
2386 static int bond_info_query(struct net_device *bond_dev, struct ifbond *info)
2387 {
2388         struct bonding *bond = bond_dev->priv;
2389
2390         info->bond_mode = bond->params.mode;
2391         info->miimon = bond->params.miimon;
2392
2393         read_lock_bh(&bond->lock);
2394         info->num_slaves = bond->slave_cnt;
2395         read_unlock_bh(&bond->lock);
2396
2397         return 0;
2398 }
2399
2400 static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info)
2401 {
2402         struct bonding *bond = bond_dev->priv;
2403         struct slave *slave;
2404         int i, found = 0;
2405
2406         if (info->slave_id < 0) {
2407                 return -ENODEV;
2408         }
2409
2410         read_lock_bh(&bond->lock);
2411
2412         bond_for_each_slave(bond, slave, i) {
2413                 if (i == (int)info->slave_id) {
2414                         found = 1;
2415                         break;
2416                 }
2417         }
2418
2419         read_unlock_bh(&bond->lock);
2420
2421         if (found) {
2422                 strcpy(info->slave_name, slave->dev->name);
2423                 info->link = slave->link;
2424                 info->state = slave->state;
2425                 info->link_failure_count = slave->link_failure_count;
2426         } else {
2427                 return -ENODEV;
2428         }
2429
2430         return 0;
2431 }
2432
2433 /*-------------------------------- Monitoring -------------------------------*/
2434
2435 /* this function is called regularly to monitor each slave's link. */
2436 void bond_mii_monitor(struct net_device *bond_dev)
2437 {
2438         struct bonding *bond = bond_dev->priv;
2439         struct slave *slave, *oldcurrent;
2440         int do_failover = 0;
2441         int delta_in_ticks;
2442         int i;
2443
2444         read_lock(&bond->lock);
2445
2446         delta_in_ticks = (bond->params.miimon * HZ) / 1000;
2447
2448         if (bond->kill_timers) {
2449                 goto out;
2450         }
2451
2452         if (bond->slave_cnt == 0) {
2453                 goto re_arm;
2454         }
2455
2456         /* we will try to read the link status of each of our slaves, and
2457          * set their IFF_RUNNING flag appropriately. For each slave not
2458          * supporting MII status, we won't do anything so that a user-space
2459          * program could monitor the link itself if needed.
2460          */
2461
2462         read_lock(&bond->curr_slave_lock);
2463         oldcurrent = bond->curr_active_slave;
2464         read_unlock(&bond->curr_slave_lock);
2465
2466         bond_for_each_slave(bond, slave, i) {
2467                 struct net_device *slave_dev = slave->dev;
2468                 int link_state;
2469                 u16 old_speed = slave->speed;
2470                 u8 old_duplex = slave->duplex;
2471
2472                 link_state = bond_check_dev_link(bond, slave_dev, 0);
2473
2474                 switch (slave->link) {
2475                 case BOND_LINK_UP:      /* the link was up */
2476                         if (link_state == BMSR_LSTATUS) {
2477                                 /* link stays up, nothing more to do */
2478                                 break;
2479                         } else { /* link going down */
2480                                 slave->link  = BOND_LINK_FAIL;
2481                                 slave->delay = bond->params.downdelay;
2482
2483                                 if (slave->link_failure_count < UINT_MAX) {
2484                                         slave->link_failure_count++;
2485                                 }
2486
2487                                 if (bond->params.downdelay) {
2488                                         printk(KERN_INFO DRV_NAME
2489                                                ": %s: link status down for %s "
2490                                                "interface %s, disabling it in "
2491                                                "%d ms.\n",
2492                                                bond_dev->name,
2493                                                IS_UP(slave_dev)
2494                                                ? ((bond->params.mode == BOND_MODE_ACTIVEBACKUP)
2495                                                   ? ((slave == oldcurrent)
2496                                                      ? "active " : "backup ")
2497                                                   : "")
2498                                                : "idle ",
2499                                                slave_dev->name,
2500                                                bond->params.downdelay * bond->params.miimon);
2501                                 }
2502                         }
2503                         /* no break ! fall through the BOND_LINK_FAIL test to
2504                            ensure proper action to be taken
2505                         */
2506                 case BOND_LINK_FAIL:    /* the link has just gone down */
2507                         if (link_state != BMSR_LSTATUS) {
2508                                 /* link stays down */
2509                                 if (slave->delay <= 0) {
2510                                         /* link down for too long time */
2511                                         slave->link = BOND_LINK_DOWN;
2512
2513                                         /* in active/backup mode, we must
2514                                          * completely disable this interface
2515                                          */
2516                                         if ((bond->params.mode == BOND_MODE_ACTIVEBACKUP) ||
2517                                             (bond->params.mode == BOND_MODE_8023AD)) {
2518                                                 bond_set_slave_inactive_flags(slave);
2519                                         }
2520
2521                                         printk(KERN_INFO DRV_NAME
2522                                                ": %s: link status definitely "
2523                                                "down for interface %s, "
2524                                                "disabling it\n",
2525                                                bond_dev->name,
2526                                                slave_dev->name);
2527
2528                                         /* notify ad that the link status has changed */
2529                                         if (bond->params.mode == BOND_MODE_8023AD) {
2530                                                 bond_3ad_handle_link_change(slave, BOND_LINK_DOWN);
2531                                         }
2532
2533                                         if ((bond->params.mode == BOND_MODE_TLB) ||
2534                                             (bond->params.mode == BOND_MODE_ALB)) {
2535                                                 bond_alb_handle_link_change(bond, slave, BOND_LINK_DOWN);
2536                                         }
2537
2538                                         if (slave == oldcurrent) {
2539                                                 do_failover = 1;
2540                                         }
2541                                 } else {
2542                                         slave->delay--;
2543                                 }
2544                         } else {
2545                                 /* link up again */
2546                                 slave->link  = BOND_LINK_UP;
2547                                 slave->jiffies = jiffies;
2548                                 printk(KERN_INFO DRV_NAME
2549                                        ": %s: link status up again after %d "
2550                                        "ms for interface %s.\n",
2551                                        bond_dev->name,
2552                                        (bond->params.downdelay - slave->delay) * bond->params.miimon,
2553                                        slave_dev->name);
2554                         }
2555                         break;
2556                 case BOND_LINK_DOWN:    /* the link was down */
2557                         if (link_state != BMSR_LSTATUS) {
2558                                 /* the link stays down, nothing more to do */
2559                                 break;
2560                         } else {        /* link going up */
2561                                 slave->link  = BOND_LINK_BACK;
2562                                 slave->delay = bond->params.updelay;
2563
2564                                 if (bond->params.updelay) {
2565                                         /* if updelay == 0, no need to
2566                                            advertise about a 0 ms delay */
2567                                         printk(KERN_INFO DRV_NAME
2568                                                ": %s: link status up for "
2569                                                "interface %s, enabling it "
2570                                                "in %d ms.\n",
2571                                                bond_dev->name,
2572                                                slave_dev->name,
2573                                                bond->params.updelay * bond->params.miimon);
2574                                 }
2575                         }
2576                         /* no break ! fall through the BOND_LINK_BACK state in
2577                            case there's something to do.
2578                         */
2579                 case BOND_LINK_BACK:    /* the link has just come back */
2580                         if (link_state != BMSR_LSTATUS) {
2581                                 /* link down again */
2582                                 slave->link  = BOND_LINK_DOWN;
2583
2584                                 printk(KERN_INFO DRV_NAME
2585                                        ": %s: link status down again after %d "
2586                                        "ms for interface %s.\n",
2587                                        bond_dev->name,
2588                                        (bond->params.updelay - slave->delay) * bond->params.miimon,
2589                                        slave_dev->name);
2590                         } else {
2591                                 /* link stays up */
2592                                 if (slave->delay == 0) {
2593                                         /* now the link has been up for long time enough */
2594                                         slave->link = BOND_LINK_UP;
2595                                         slave->jiffies = jiffies;
2596
2597                                         if (bond->params.mode == BOND_MODE_8023AD) {
2598                                                 /* prevent it from being the active one */
2599                                                 slave->state = BOND_STATE_BACKUP;
2600                                         } else if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
2601                                                 /* make it immediately active */
2602                                                 slave->state = BOND_STATE_ACTIVE;
2603                                         } else if (slave != bond->primary_slave) {
2604                                                 /* prevent it from being the active one */
2605                                                 slave->state = BOND_STATE_BACKUP;
2606                                         }
2607
2608                                         printk(KERN_INFO DRV_NAME
2609                                                ": %s: link status definitely "
2610                                                "up for interface %s.\n",
2611                                                bond_dev->name,
2612                                                slave_dev->name);
2613
2614                                         /* notify ad that the link status has changed */
2615                                         if (bond->params.mode == BOND_MODE_8023AD) {
2616                                                 bond_3ad_handle_link_change(slave, BOND_LINK_UP);
2617                                         }
2618
2619                                         if ((bond->params.mode == BOND_MODE_TLB) ||
2620                                             (bond->params.mode == BOND_MODE_ALB)) {
2621                                                 bond_alb_handle_link_change(bond, slave, BOND_LINK_UP);
2622                                         }
2623
2624                                         if ((!oldcurrent) ||
2625                                             (slave == bond->primary_slave)) {
2626                                                 do_failover = 1;
2627                                         }
2628                                 } else {
2629                                         slave->delay--;
2630                                 }
2631                         }
2632                         break;
2633                 default:
2634                         /* Should not happen */
2635                         printk(KERN_ERR DRV_NAME
2636                                ": %s: Error: %s Illegal value (link=%d)\n",
2637                                bond_dev->name,
2638                                slave->dev->name,
2639                                slave->link);
2640                         goto out;
2641                 } /* end of switch (slave->link) */
2642
2643                 bond_update_speed_duplex(slave);
2644
2645                 if (bond->params.mode == BOND_MODE_8023AD) {
2646                         if (old_speed != slave->speed) {
2647                                 bond_3ad_adapter_speed_changed(slave);
2648                         }
2649
2650                         if (old_duplex != slave->duplex) {
2651                                 bond_3ad_adapter_duplex_changed(slave);
2652                         }
2653                 }
2654
2655         } /* end of for */
2656
2657         if (do_failover) {
2658                 write_lock(&bond->curr_slave_lock);
2659
2660                 bond_select_active_slave(bond);
2661
2662                 if (oldcurrent && !bond->curr_active_slave) {
2663                         printk(KERN_INFO DRV_NAME
2664                                ": %s: now running without any active "
2665                                "interface !\n",
2666                                bond_dev->name);
2667                 }
2668
2669                 write_unlock(&bond->curr_slave_lock);
2670         }
2671
2672 re_arm:
2673         if (bond->params.miimon) {
2674                 mod_timer(&bond->mii_timer, jiffies + delta_in_ticks);
2675         }
2676 out:
2677         read_unlock(&bond->lock);
2678 }
2679
2680
2681 static u32 bond_glean_dev_ip(struct net_device *dev)
2682 {
2683         struct in_device *idev;
2684         struct in_ifaddr *ifa;
2685         u32 addr = 0;
2686
2687         if (!dev)
2688                 return 0;
2689
2690         rcu_read_lock();
2691         idev = __in_dev_get_rcu(dev);
2692         if (!idev)
2693                 goto out;
2694
2695         ifa = idev->ifa_list;
2696         if (!ifa)
2697                 goto out;
2698
2699         addr = ifa->ifa_local;
2700 out:
2701         rcu_read_unlock();
2702         return addr;
2703 }
2704
2705 static int bond_has_ip(struct bonding *bond)
2706 {
2707         struct vlan_entry *vlan, *vlan_next;
2708
2709         if (bond->master_ip)
2710                 return 1;
2711
2712         if (list_empty(&bond->vlan_list))
2713                 return 0;
2714
2715         list_for_each_entry_safe(vlan, vlan_next, &bond->vlan_list,
2716                                  vlan_list) {
2717                 if (vlan->vlan_ip)
2718                         return 1;
2719         }
2720
2721         return 0;
2722 }
2723
2724 /*
2725  * We go to the (large) trouble of VLAN tagging ARP frames because
2726  * switches in VLAN mode (especially if ports are configured as
2727  * "native" to a VLAN) might not pass non-tagged frames.
2728  */
2729 static void bond_arp_send(struct net_device *slave_dev, int arp_op, u32 dest_ip, u32 src_ip, unsigned short vlan_id)
2730 {
2731         struct sk_buff *skb;
2732
2733         dprintk("arp %d on slave %s: dst %x src %x vid %d\n", arp_op,
2734                slave_dev->name, dest_ip, src_ip, vlan_id);
2735                
2736         skb = arp_create(arp_op, ETH_P_ARP, dest_ip, slave_dev, src_ip,
2737                          NULL, slave_dev->dev_addr, NULL);
2738
2739         if (!skb) {
2740                 printk(KERN_ERR DRV_NAME ": ARP packet allocation failed\n");
2741                 return;
2742         }
2743         if (vlan_id) {
2744                 skb = vlan_put_tag(skb, vlan_id);
2745                 if (!skb) {
2746                         printk(KERN_ERR DRV_NAME ": failed to insert VLAN tag\n");
2747                         return;
2748                 }
2749         }
2750         arp_xmit(skb);
2751 }
2752
2753
2754 static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
2755 {
2756         int i, vlan_id, rv;
2757         u32 *targets = bond->params.arp_targets;
2758         struct vlan_entry *vlan, *vlan_next;
2759         struct net_device *vlan_dev;
2760         struct flowi fl;
2761         struct rtable *rt;
2762
2763         for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
2764                 if (!targets[i])
2765                         continue;
2766                 dprintk("basa: target %x\n", targets[i]);
2767                 if (list_empty(&bond->vlan_list)) {
2768                         dprintk("basa: empty vlan: arp_send\n");
2769                         bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2770                                       bond->master_ip, 0);
2771                         continue;
2772                 }
2773
2774                 /*
2775                  * If VLANs are configured, we do a route lookup to
2776                  * determine which VLAN interface would be used, so we
2777                  * can tag the ARP with the proper VLAN tag.
2778                  */
2779                 memset(&fl, 0, sizeof(fl));
2780                 fl.fl4_dst = targets[i];
2781                 fl.fl4_tos = RTO_ONLINK;
2782
2783                 rv = ip_route_output_key(&rt, &fl);
2784                 if (rv) {
2785                         if (net_ratelimit()) {
2786                                 printk(KERN_WARNING DRV_NAME
2787                              ": %s: no route to arp_ip_target %u.%u.%u.%u\n",
2788                                        bond->dev->name, NIPQUAD(fl.fl4_dst));
2789                         }
2790                         continue;
2791                 }
2792
2793                 /*
2794                  * This target is not on a VLAN
2795                  */
2796                 if (rt->u.dst.dev == bond->dev) {
2797                         ip_rt_put(rt);
2798                         dprintk("basa: rtdev == bond->dev: arp_send\n");
2799                         bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2800                                       bond->master_ip, 0);
2801                         continue;
2802                 }
2803
2804                 vlan_id = 0;
2805                 list_for_each_entry_safe(vlan, vlan_next, &bond->vlan_list,
2806                                          vlan_list) {
2807                         vlan_dev = bond->vlgrp->vlan_devices[vlan->vlan_id];
2808                         if (vlan_dev == rt->u.dst.dev) {
2809                                 vlan_id = vlan->vlan_id;
2810                                 dprintk("basa: vlan match on %s %d\n",
2811                                        vlan_dev->name, vlan_id);
2812                                 break;
2813                         }
2814                 }
2815
2816                 if (vlan_id) {
2817                         ip_rt_put(rt);
2818                         bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2819                                       vlan->vlan_ip, vlan_id);
2820                         continue;
2821                 }
2822
2823                 if (net_ratelimit()) {
2824                         printk(KERN_WARNING DRV_NAME
2825                ": %s: no path to arp_ip_target %u.%u.%u.%u via rt.dev %s\n",
2826                                bond->dev->name, NIPQUAD(fl.fl4_dst),
2827                                rt->u.dst.dev ? rt->u.dst.dev->name : "NULL");
2828                 }
2829                 ip_rt_put(rt);
2830         }
2831 }
2832
2833 /*
2834  * Kick out a gratuitous ARP for an IP on the bonding master plus one
2835  * for each VLAN above us.
2836  */
2837 static void bond_send_gratuitous_arp(struct bonding *bond)
2838 {
2839         struct slave *slave = bond->curr_active_slave;
2840         struct vlan_entry *vlan;
2841         struct net_device *vlan_dev;
2842
2843         dprintk("bond_send_grat_arp: bond %s slave %s\n", bond->dev->name,
2844                                 slave ? slave->dev->name : "NULL");
2845         if (!slave)
2846                 return;
2847
2848         if (bond->master_ip) {
2849                 bond_arp_send(slave->dev, ARPOP_REPLY, bond->master_ip,
2850                                   bond->master_ip, 0);
2851         }
2852
2853         list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
2854                 vlan_dev = bond->vlgrp->vlan_devices[vlan->vlan_id];
2855                 if (vlan->vlan_ip) {
2856                         bond_arp_send(slave->dev, ARPOP_REPLY, vlan->vlan_ip,
2857                                       vlan->vlan_ip, vlan->vlan_id);
2858                 }
2859         }
2860 }
2861
2862 /*
2863  * this function is called regularly to monitor each slave's link
2864  * ensuring that traffic is being sent and received when arp monitoring
2865  * is used in load-balancing mode. if the adapter has been dormant, then an
2866  * arp is transmitted to generate traffic. see activebackup_arp_monitor for
2867  * arp monitoring in active backup mode.
2868  */
2869 void bond_loadbalance_arp_mon(struct net_device *bond_dev)
2870 {
2871         struct bonding *bond = bond_dev->priv;
2872         struct slave *slave, *oldcurrent;
2873         int do_failover = 0;
2874         int delta_in_ticks;
2875         int i;
2876
2877         read_lock(&bond->lock);
2878
2879         delta_in_ticks = (bond->params.arp_interval * HZ) / 1000;
2880
2881         if (bond->kill_timers) {
2882                 goto out;
2883         }
2884
2885         if (bond->slave_cnt == 0) {
2886                 goto re_arm;
2887         }
2888
2889         read_lock(&bond->curr_slave_lock);
2890         oldcurrent = bond->curr_active_slave;
2891         read_unlock(&bond->curr_slave_lock);
2892
2893         /* see if any of the previous devices are up now (i.e. they have
2894          * xmt and rcv traffic). the curr_active_slave does not come into
2895          * the picture unless it is null. also, slave->jiffies is not needed
2896          * here because we send an arp on each slave and give a slave as
2897          * long as it needs to get the tx/rx within the delta.
2898          * TODO: what about up/down delay in arp mode? it wasn't here before
2899          *       so it can wait
2900          */
2901         bond_for_each_slave(bond, slave, i) {
2902                 if (slave->link != BOND_LINK_UP) {
2903                         if (((jiffies - slave->dev->trans_start) <= delta_in_ticks) &&
2904                             ((jiffies - slave->dev->last_rx) <= delta_in_ticks)) {
2905
2906                                 slave->link  = BOND_LINK_UP;
2907                                 slave->state = BOND_STATE_ACTIVE;
2908
2909                                 /* primary_slave has no meaning in round-robin
2910                                  * mode. the window of a slave being up and
2911                                  * curr_active_slave being null after enslaving
2912                                  * is closed.
2913                                  */
2914                                 if (!oldcurrent) {
2915                                         printk(KERN_INFO DRV_NAME
2916                                                ": %s: link status definitely "
2917                                                "up for interface %s, ",
2918                                                bond_dev->name,
2919                                                slave->dev->name);
2920                                         do_failover = 1;
2921                                 } else {
2922                                         printk(KERN_INFO DRV_NAME
2923                                                ": %s: interface %s is now up\n",
2924                                                bond_dev->name,
2925                                                slave->dev->name);
2926                                 }
2927                         }
2928                 } else {
2929                         /* slave->link == BOND_LINK_UP */
2930
2931                         /* not all switches will respond to an arp request
2932                          * when the source ip is 0, so don't take the link down
2933                          * if we don't know our ip yet
2934                          */
2935                         if (((jiffies - slave->dev->trans_start) >= (2*delta_in_ticks)) ||
2936                             (((jiffies - slave->dev->last_rx) >= (2*delta_in_ticks)) &&
2937                              bond_has_ip(bond))) {
2938
2939                                 slave->link  = BOND_LINK_DOWN;
2940                                 slave->state = BOND_STATE_BACKUP;
2941
2942                                 if (slave->link_failure_count < UINT_MAX) {
2943                                         slave->link_failure_count++;
2944                                 }
2945
2946                                 printk(KERN_INFO DRV_NAME
2947                                        ": %s: interface %s is now down.\n",
2948                                        bond_dev->name,
2949                                        slave->dev->name);
2950
2951                                 if (slave == oldcurrent) {
2952                                         do_failover = 1;
2953                                 }
2954                         }
2955                 }
2956
2957                 /* note: if switch is in round-robin mode, all links
2958                  * must tx arp to ensure all links rx an arp - otherwise
2959                  * links may oscillate or not come up at all; if switch is
2960                  * in something like xor mode, there is nothing we can
2961                  * do - all replies will be rx'ed on same link causing slaves
2962                  * to be unstable during low/no traffic periods
2963                  */
2964                 if (IS_UP(slave->dev)) {
2965                         bond_arp_send_all(bond, slave);
2966                 }
2967         }
2968
2969         if (do_failover) {
2970                 write_lock(&bond->curr_slave_lock);
2971
2972                 bond_select_active_slave(bond);
2973
2974                 if (oldcurrent && !bond->curr_active_slave) {
2975                         printk(KERN_INFO DRV_NAME
2976                                ": %s: now running without any active "
2977                                "interface !\n",
2978                                bond_dev->name);
2979                 }
2980
2981                 write_unlock(&bond->curr_slave_lock);
2982         }
2983
2984 re_arm:
2985         if (bond->params.arp_interval) {
2986                 mod_timer(&bond->arp_timer, jiffies + delta_in_ticks);
2987         }
2988 out:
2989         read_unlock(&bond->lock);
2990 }
2991
2992 /*
2993  * When using arp monitoring in active-backup mode, this function is
2994  * called to determine if any backup slaves have went down or a new
2995  * current slave needs to be found.
2996  * The backup slaves never generate traffic, they are considered up by merely
2997  * receiving traffic. If the current slave goes down, each backup slave will
2998  * be given the opportunity to tx/rx an arp before being taken down - this
2999  * prevents all slaves from being taken down due to the current slave not
3000  * sending any traffic for the backups to receive. The arps are not necessarily
3001  * necessary, any tx and rx traffic will keep the current slave up. While any
3002  * rx traffic will keep the backup slaves up, the current slave is responsible
3003  * for generating traffic to keep them up regardless of any other traffic they
3004  * may have received.
3005  * see loadbalance_arp_monitor for arp monitoring in load balancing mode
3006  */
3007 void bond_activebackup_arp_mon(struct net_device *bond_dev)
3008 {
3009         struct bonding *bond = bond_dev->priv;
3010         struct slave *slave;
3011         int delta_in_ticks;
3012         int i;
3013
3014         read_lock(&bond->lock);
3015
3016         delta_in_ticks = (bond->params.arp_interval * HZ) / 1000;
3017
3018         if (bond->kill_timers) {
3019                 goto out;
3020         }
3021
3022         if (bond->slave_cnt == 0) {
3023                 goto re_arm;
3024         }
3025
3026         /* determine if any slave has come up or any backup slave has
3027          * gone down
3028          * TODO: what about up/down delay in arp mode? it wasn't here before
3029          *       so it can wait
3030          */
3031         bond_for_each_slave(bond, slave, i) {
3032                 if (slave->link != BOND_LINK_UP) {
3033                         if ((jiffies - slave->dev->last_rx) <= delta_in_ticks) {
3034
3035                                 slave->link = BOND_LINK_UP;
3036
3037                                 write_lock(&bond->curr_slave_lock);
3038
3039                                 if ((!bond->curr_active_slave) &&
3040                                     ((jiffies - slave->dev->trans_start) <= delta_in_ticks)) {
3041                                         bond_change_active_slave(bond, slave);
3042                                         bond->current_arp_slave = NULL;
3043                                 } else if (bond->curr_active_slave != slave) {
3044                                         /* this slave has just come up but we
3045                                          * already have a current slave; this
3046                                          * can also happen if bond_enslave adds
3047                                          * a new slave that is up while we are
3048                                          * searching for a new slave
3049                                          */
3050                                         bond_set_slave_inactive_flags(slave);
3051                                         bond->current_arp_slave = NULL;
3052                                 }
3053
3054                                 if (slave == bond->curr_active_slave) {
3055                                         printk(KERN_INFO DRV_NAME
3056                                                ": %s: %s is up and now the "
3057                                                "active interface\n",
3058                                                bond_dev->name,
3059                                                slave->dev->name);
3060                                 } else {
3061                                         printk(KERN_INFO DRV_NAME
3062                                                ": %s: backup interface %s is "
3063                                                "now up\n",
3064                                                bond_dev->name,
3065                                                slave->dev->name);
3066                                 }
3067
3068                                 write_unlock(&bond->curr_slave_lock);
3069                         }
3070                 } else {
3071                         read_lock(&bond->curr_slave_lock);
3072
3073                         if ((slave != bond->curr_active_slave) &&
3074                             (!bond->current_arp_slave) &&
3075                             (((jiffies - slave->dev->last_rx) >= 3*delta_in_ticks) &&
3076                              bond_has_ip(bond))) {
3077                                 /* a backup slave has gone down; three times
3078                                  * the delta allows the current slave to be
3079                                  * taken out before the backup slave.
3080                                  * note: a non-null current_arp_slave indicates
3081                                  * the curr_active_slave went down and we are
3082                                  * searching for a new one; under this
3083                                  * condition we only take the curr_active_slave
3084                                  * down - this gives each slave a chance to
3085                                  * tx/rx traffic before being taken out
3086                                  */
3087
3088                                 read_unlock(&bond->curr_slave_lock);
3089
3090                                 slave->link  = BOND_LINK_DOWN;
3091
3092                                 if (slave->link_failure_count < UINT_MAX) {
3093                                         slave->link_failure_count++;
3094                                 }
3095
3096                                 bond_set_slave_inactive_flags(slave);
3097
3098                                 printk(KERN_INFO DRV_NAME
3099                                        ": %s: backup interface %s is now down\n",
3100                                        bond_dev->name,
3101                                        slave->dev->name);
3102                         } else {
3103                                 read_unlock(&bond->curr_slave_lock);
3104                         }
3105                 }
3106         }
3107
3108         read_lock(&bond->curr_slave_lock);
3109         slave = bond->curr_active_slave;
3110         read_unlock(&bond->curr_slave_lock);
3111
3112         if (slave) {
3113                 /* if we have sent traffic in the past 2*arp_intervals but
3114                  * haven't xmit and rx traffic in that time interval, select
3115                  * a different slave. slave->jiffies is only updated when
3116                  * a slave first becomes the curr_active_slave - not necessarily
3117                  * after every arp; this ensures the slave has a full 2*delta
3118                  * before being taken out. if a primary is being used, check
3119                  * if it is up and needs to take over as the curr_active_slave
3120                  */
3121                 if ((((jiffies - slave->dev->trans_start) >= (2*delta_in_ticks)) ||
3122             (((jiffies - slave->dev->last_rx) >= (2*delta_in_ticks)) &&
3123              bond_has_ip(bond))) &&
3124                     ((jiffies - slave->jiffies) >= 2*delta_in_ticks)) {
3125
3126                         slave->link  = BOND_LINK_DOWN;
3127
3128                         if (slave->link_failure_count < UINT_MAX) {
3129                                 slave->link_failure_count++;
3130                         }
3131
3132                         printk(KERN_INFO DRV_NAME
3133                                ": %s: link status down for active interface "
3134                                "%s, disabling it\n",
3135                                bond_dev->name,
3136                                slave->dev->name);
3137
3138                         write_lock(&bond->curr_slave_lock);
3139
3140                         bond_select_active_slave(bond);
3141                         slave = bond->curr_active_slave;
3142
3143                         write_unlock(&bond->curr_slave_lock);
3144
3145                         bond->current_arp_slave = slave;
3146
3147                         if (slave) {
3148                                 slave->jiffies = jiffies;
3149                         }
3150                 } else if ((bond->primary_slave) &&
3151                            (bond->primary_slave != slave) &&
3152                            (bond->primary_slave->link == BOND_LINK_UP)) {
3153                         /* at this point, slave is the curr_active_slave */
3154                         printk(KERN_INFO DRV_NAME
3155                                ": %s: changing from interface %s to primary "
3156                                "interface %s\n",
3157                                bond_dev->name,
3158                                slave->dev->name,
3159                                bond->primary_slave->dev->name);
3160
3161                         /* primary is up so switch to it */
3162                         write_lock(&bond->curr_slave_lock);
3163                         bond_change_active_slave(bond, bond->primary_slave);
3164                         write_unlock(&bond->curr_slave_lock);
3165
3166                         slave = bond->primary_slave;
3167                         slave->jiffies = jiffies;
3168                 } else {
3169                         bond->current_arp_slave = NULL;
3170                 }
3171
3172                 /* the current slave must tx an arp to ensure backup slaves
3173                  * rx traffic
3174                  */
3175                 if (slave && bond_has_ip(bond)) {
3176                         bond_arp_send_all(bond, slave);
3177                 }
3178         }
3179
3180         /* if we don't have a curr_active_slave, search for the next available
3181          * backup slave from the current_arp_slave and make it the candidate
3182          * for becoming the curr_active_slave
3183          */
3184         if (!slave) {
3185                 if (!bond->current_arp_slave) {
3186                         bond->current_arp_slave = bond->first_slave;
3187                 }
3188
3189                 if (bond->current_arp_slave) {
3190                         bond_set_slave_inactive_flags(bond->current_arp_slave);
3191
3192                         /* search for next candidate */
3193                         bond_for_each_slave_from(bond, slave, i, bond->current_arp_slave->next) {
3194                                 if (IS_UP(slave->dev)) {
3195                                         slave->link = BOND_LINK_BACK;
3196                                         bond_set_slave_active_flags(slave);
3197                                         bond_arp_send_all(bond, slave);
3198                                         slave->jiffies = jiffies;
3199                                         bond->current_arp_slave = slave;
3200                                         break;
3201                                 }
3202
3203                                 /* if the link state is up at this point, we
3204                                  * mark it down - this can happen if we have
3205                                  * simultaneous link failures and
3206                                  * reselect_active_interface doesn't make this
3207                                  * one the current slave so it is still marked
3208                                  * up when it is actually down
3209                                  */
3210                                 if (slave->link == BOND_LINK_UP) {
3211                                         slave->link  = BOND_LINK_DOWN;
3212                                         if (slave->link_failure_count < UINT_MAX) {
3213                                                 slave->link_failure_count++;
3214                                         }
3215
3216                                         bond_set_slave_inactive_flags(slave);
3217
3218                                         printk(KERN_INFO DRV_NAME
3219                                                ": %s: backup interface %s is "
3220                                                "now down.\n",
3221                                                bond_dev->name,
3222                                                slave->dev->name);
3223                                 }
3224                         }
3225                 }
3226         }
3227
3228 re_arm:
3229         if (bond->params.arp_interval) {
3230                 mod_timer(&bond->arp_timer, jiffies + delta_in_ticks);
3231         }
3232 out:
3233         read_unlock(&bond->lock);
3234 }
3235
3236 /*------------------------------ proc/seq_file-------------------------------*/
3237
3238 #ifdef CONFIG_PROC_FS
3239
3240 #define SEQ_START_TOKEN ((void *)1)
3241
3242 static void *bond_info_seq_start(struct seq_file *seq, loff_t *pos)
3243 {
3244         struct bonding *bond = seq->private;
3245         loff_t off = 0;
3246         struct slave *slave;
3247         int i;
3248
3249         /* make sure the bond won't be taken away */
3250         read_lock(&dev_base_lock);
3251         read_lock_bh(&bond->lock);
3252
3253         if (*pos == 0) {
3254                 return SEQ_START_TOKEN;
3255         }
3256
3257         bond_for_each_slave(bond, slave, i) {
3258                 if (++off == *pos) {
3259                         return slave;
3260                 }
3261         }
3262
3263         return NULL;
3264 }
3265
3266 static void *bond_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3267 {
3268         struct bonding *bond = seq->private;
3269         struct slave *slave = v;
3270
3271         ++*pos;
3272         if (v == SEQ_START_TOKEN) {
3273                 return bond->first_slave;
3274         }
3275
3276         slave = slave->next;
3277
3278         return (slave == bond->first_slave) ? NULL : slave;
3279 }
3280
3281 static void bond_info_seq_stop(struct seq_file *seq, void *v)
3282 {
3283         struct bonding *bond = seq->private;
3284
3285         read_unlock_bh(&bond->lock);
3286         read_unlock(&dev_base_lock);
3287 }
3288
3289 static void bond_info_show_master(struct seq_file *seq)
3290 {
3291         struct bonding *bond = seq->private;
3292         struct slave *curr;
3293         int i;
3294         u32 target;
3295
3296         read_lock(&bond->curr_slave_lock);
3297         curr = bond->curr_active_slave;
3298         read_unlock(&bond->curr_slave_lock);
3299
3300         seq_printf(seq, "Bonding Mode: %s\n",
3301                    bond_mode_name(bond->params.mode));
3302
3303         if (bond->params.mode == BOND_MODE_XOR ||
3304                 bond->params.mode == BOND_MODE_8023AD) {
3305                 seq_printf(seq, "Transmit Hash Policy: %s (%d)\n",
3306                         xmit_hashtype_tbl[bond->params.xmit_policy].modename,
3307                         bond->params.xmit_policy);
3308         }
3309
3310         if (USES_PRIMARY(bond->params.mode)) {
3311                 seq_printf(seq, "Primary Slave: %s\n",
3312                            (bond->primary_slave) ?
3313                            bond->primary_slave->dev->name : "None");
3314
3315                 seq_printf(seq, "Currently Active Slave: %s\n",
3316                            (curr) ? curr->dev->name : "None");
3317         }
3318
3319         seq_printf(seq, "MII Status: %s\n", (curr) ? "up" : "down");
3320         seq_printf(seq, "MII Polling Interval (ms): %d\n", bond->params.miimon);
3321         seq_printf(seq, "Up Delay (ms): %d\n",
3322                    bond->params.updelay * bond->params.miimon);
3323         seq_printf(seq, "Down Delay (ms): %d\n",
3324                    bond->params.downdelay * bond->params.miimon);
3325
3326
3327         /* ARP information */
3328         if(bond->params.arp_interval > 0) {
3329                 int printed=0;
3330                 seq_printf(seq, "ARP Polling Interval (ms): %d\n",
3331                                 bond->params.arp_interval);
3332
3333                 seq_printf(seq, "ARP IP target/s (n.n.n.n form):");
3334
3335                 for(i = 0; (i < BOND_MAX_ARP_TARGETS) ;i++) {
3336                         if (!bond->params.arp_targets[i])
3337                                 continue;
3338                         if (printed)
3339                                 seq_printf(seq, ",");
3340                         target = ntohl(bond->params.arp_targets[i]);
3341                         seq_printf(seq, " %d.%d.%d.%d", HIPQUAD(target));
3342                         printed = 1;
3343                 }
3344                 seq_printf(seq, "\n");
3345         }
3346
3347         if (bond->params.mode == BOND_MODE_8023AD) {
3348                 struct ad_info ad_info;
3349
3350                 seq_puts(seq, "\n802.3ad info\n");
3351                 seq_printf(seq, "LACP rate: %s\n",
3352                            (bond->params.lacp_fast) ? "fast" : "slow");
3353
3354                 if (bond_3ad_get_active_agg_info(bond, &ad_info)) {
3355                         seq_printf(seq, "bond %s has no active aggregator\n",
3356                                    bond->dev->name);
3357                 } else {
3358                         seq_printf(seq, "Active Aggregator Info:\n");
3359
3360                         seq_printf(seq, "\tAggregator ID: %d\n",
3361                                    ad_info.aggregator_id);
3362                         seq_printf(seq, "\tNumber of ports: %d\n",
3363                                    ad_info.ports);
3364                         seq_printf(seq, "\tActor Key: %d\n",
3365                                    ad_info.actor_key);
3366                         seq_printf(seq, "\tPartner Key: %d\n",
3367                                    ad_info.partner_key);
3368                         seq_printf(seq, "\tPartner Mac Address: %02x:%02x:%02x:%02x:%02x:%02x\n",
3369                                    ad_info.partner_system[0],
3370                                    ad_info.partner_system[1],
3371                                    ad_info.partner_system[2],
3372                                    ad_info.partner_system[3],
3373                                    ad_info.partner_system[4],
3374                                    ad_info.partner_system[5]);
3375                 }
3376         }
3377 }
3378
3379 static void bond_info_show_slave(struct seq_file *seq, const struct slave *slave)
3380 {
3381         struct bonding *bond = seq->private;
3382
3383         seq_printf(seq, "\nSlave Interface: %s\n", slave->dev->name);
3384         seq_printf(seq, "MII Status: %s\n",
3385                    (slave->link == BOND_LINK_UP) ?  "up" : "down");
3386         seq_printf(seq, "Link Failure Count: %d\n",
3387                    slave->link_failure_count);
3388
3389         seq_printf(seq,
3390                    "Permanent HW addr: %02x:%02x:%02x:%02x:%02x:%02x\n",
3391                    slave->perm_hwaddr[0], slave->perm_hwaddr[1],
3392                    slave->perm_hwaddr[2], slave->perm_hwaddr[3],
3393                    slave->perm_hwaddr[4], slave->perm_hwaddr[5]);
3394
3395         if (bond->params.mode == BOND_MODE_8023AD) {
3396                 const struct aggregator *agg
3397                         = SLAVE_AD_INFO(slave).port.aggregator;
3398
3399                 if (agg) {
3400                         seq_printf(seq, "Aggregator ID: %d\n",
3401                                    agg->aggregator_identifier);
3402                 } else {
3403                         seq_puts(seq, "Aggregator ID: N/A\n");
3404                 }
3405         }
3406 }
3407
3408 static int bond_info_seq_show(struct seq_file *seq, void *v)
3409 {
3410         if (v == SEQ_START_TOKEN) {
3411                 seq_printf(seq, "%s\n", version);
3412                 bond_info_show_master(seq);
3413         } else {
3414                 bond_info_show_slave(seq, v);
3415         }
3416
3417         return 0;
3418 }
3419
3420 static struct seq_operations bond_info_seq_ops = {
3421         .start = bond_info_seq_start,
3422         .next  = bond_info_seq_next,
3423         .stop  = bond_info_seq_stop,
3424         .show  = bond_info_seq_show,
3425 };
3426
3427 static int bond_info_open(struct inode *inode, struct file *file)
3428 {
3429         struct seq_file *seq;
3430         struct proc_dir_entry *proc;
3431         int res;
3432
3433         res = seq_open(file, &bond_info_seq_ops);
3434         if (!res) {
3435                 /* recover the pointer buried in proc_dir_entry data */
3436                 seq = file->private_data;
3437                 proc = PDE(inode);
3438                 seq->private = proc->data;
3439         }
3440
3441         return res;
3442 }
3443
3444 static struct file_operations bond_info_fops = {
3445         .owner   = THIS_MODULE,
3446         .open    = bond_info_open,
3447         .read    = seq_read,
3448         .llseek  = seq_lseek,
3449         .release = seq_release,
3450 };
3451
3452 static int bond_create_proc_entry(struct bonding *bond)
3453 {
3454         struct net_device *bond_dev = bond->dev;
3455
3456         if (bond_proc_dir) {
3457                 bond->proc_entry = create_proc_entry(bond_dev->name,
3458                                                      S_IRUGO,
3459                                                      bond_proc_dir);
3460                 if (bond->proc_entry == NULL) {
3461                         printk(KERN_WARNING DRV_NAME
3462                                ": Warning: Cannot create /proc/net/%s/%s\n",
3463                                DRV_NAME, bond_dev->name);
3464                 } else {
3465                         bond->proc_entry->data = bond;
3466                         bond->proc_entry->proc_fops = &bond_info_fops;
3467                         bond->proc_entry->owner = THIS_MODULE;
3468                         memcpy(bond->proc_file_name, bond_dev->name, IFNAMSIZ);
3469                 }
3470         }
3471
3472         return 0;
3473 }
3474
3475 static void bond_remove_proc_entry(struct bonding *bond)
3476 {
3477         if (bond_proc_dir && bond->proc_entry) {
3478                 remove_proc_entry(bond->proc_file_name, bond_proc_dir);
3479                 memset(bond->proc_file_name, 0, IFNAMSIZ);
3480                 bond->proc_entry = NULL;
3481         }
3482 }
3483
3484 /* Create the bonding directory under /proc/net, if doesn't exist yet.
3485  * Caller must hold rtnl_lock.
3486  */
3487 static void bond_create_proc_dir(void)
3488 {
3489         int len = strlen(DRV_NAME);
3490
3491         for (bond_proc_dir = proc_net->subdir; bond_proc_dir;
3492              bond_proc_dir = bond_proc_dir->next) {
3493                 if ((bond_proc_dir->namelen == len) &&
3494                     !memcmp(bond_proc_dir->name, DRV_NAME, len)) {
3495                         break;
3496                 }
3497         }
3498
3499         if (!bond_proc_dir) {
3500                 bond_proc_dir = proc_mkdir(DRV_NAME, proc_net);
3501                 if (bond_proc_dir) {
3502                         bond_proc_dir->owner = THIS_MODULE;
3503                 } else {
3504                         printk(KERN_WARNING DRV_NAME
3505                                 ": Warning: cannot create /proc/net/%s\n",
3506                                 DRV_NAME);
3507                 }
3508         }
3509 }
3510
3511 /* Destroy the bonding directory under /proc/net, if empty.
3512  * Caller must hold rtnl_lock.
3513  */
3514 static void bond_destroy_proc_dir(void)
3515 {
3516         struct proc_dir_entry *de;
3517
3518         if (!bond_proc_dir) {
3519                 return;
3520         }
3521
3522         /* verify that the /proc dir is empty */
3523         for (de = bond_proc_dir->subdir; de; de = de->next) {
3524                 /* ignore . and .. */
3525                 if (*(de->name) != '.') {
3526                         break;
3527                 }
3528         }
3529
3530         if (de) {
3531                 if (bond_proc_dir->owner == THIS_MODULE) {
3532                         bond_proc_dir->owner = NULL;
3533                 }
3534         } else {
3535                 remove_proc_entry(DRV_NAME, proc_net);
3536                 bond_proc_dir = NULL;
3537         }
3538 }
3539 #endif /* CONFIG_PROC_FS */
3540
3541 /*-------------------------- netdev event handling --------------------------*/
3542
3543 /*
3544  * Change device name
3545  */
3546 static int bond_event_changename(struct bonding *bond)
3547 {
3548 #ifdef CONFIG_PROC_FS
3549         bond_remove_proc_entry(bond);
3550         bond_create_proc_entry(bond);
3551 #endif
3552         down_write(&(bonding_rwsem));
3553         bond_destroy_sysfs_entry(bond);
3554         bond_create_sysfs_entry(bond);
3555         up_write(&(bonding_rwsem));
3556         return NOTIFY_DONE;
3557 }
3558
3559 static int bond_master_netdev_event(unsigned long event, struct net_device *bond_dev)
3560 {
3561         struct bonding *event_bond = bond_dev->priv;
3562
3563         switch (event) {
3564         case NETDEV_CHANGENAME:
3565                 return bond_event_changename(event_bond);
3566         case NETDEV_UNREGISTER:
3567                 /*
3568                  * TODO: remove a bond from the list?
3569                  */
3570                 break;
3571         default:
3572                 break;
3573         }
3574
3575         return NOTIFY_DONE;
3576 }
3577
3578 static int bond_slave_netdev_event(unsigned long event, struct net_device *slave_dev)
3579 {
3580         struct net_device *bond_dev = slave_dev->master;
3581         struct bonding *bond = bond_dev->priv;
3582
3583         switch (event) {
3584         case NETDEV_UNREGISTER:
3585                 if (bond_dev) {
3586                         bond_release(bond_dev, slave_dev);
3587                 }
3588                 break;
3589         case NETDEV_CHANGE:
3590                 /*
3591                  * TODO: is this what we get if somebody
3592                  * sets up a hierarchical bond, then rmmod's
3593                  * one of the slave bonding devices?
3594                  */
3595                 break;
3596         case NETDEV_DOWN:
3597                 /*
3598                  * ... Or is it this?
3599                  */
3600                 break;
3601         case NETDEV_CHANGEMTU:
3602                 /*
3603                  * TODO: Should slaves be allowed to
3604                  * independently alter their MTU?  For
3605                  * an active-backup bond, slaves need
3606                  * not be the same type of device, so
3607                  * MTUs may vary.  For other modes,
3608                  * slaves arguably should have the
3609                  * same MTUs. To do this, we'd need to
3610                  * take over the slave's change_mtu
3611                  * function for the duration of their
3612                  * servitude.
3613                  */
3614                 break;
3615         case NETDEV_CHANGENAME:
3616                 /*
3617                  * TODO: handle changing the primary's name
3618                  */
3619                 break;
3620         case NETDEV_FEAT_CHANGE:
3621                 bond_compute_features(bond);
3622                 break;
3623         default:
3624                 break;
3625         }
3626
3627         return NOTIFY_DONE;
3628 }
3629
3630 /*
3631  * bond_netdev_event: handle netdev notifier chain events.
3632  *
3633  * This function receives events for the netdev chain.  The caller (an
3634  * ioctl handler calling notifier_call_chain) holds the necessary
3635  * locks for us to safely manipulate the slave devices (RTNL lock,
3636  * dev_probe_lock).
3637  */
3638 static int bond_netdev_event(struct notifier_block *this, unsigned long event, void *ptr)
3639 {
3640         struct net_device *event_dev = (struct net_device *)ptr;
3641
3642         dprintk("event_dev: %s, event: %lx\n",
3643                 (event_dev ? event_dev->name : "None"),
3644                 event);
3645
3646         if (event_dev->flags & IFF_MASTER) {
3647                 dprintk("IFF_MASTER\n");
3648                 return bond_master_netdev_event(event, event_dev);
3649         }
3650
3651         if (event_dev->flags & IFF_SLAVE) {
3652                 dprintk("IFF_SLAVE\n");
3653                 return bond_slave_netdev_event(event, event_dev);
3654         }
3655
3656         return NOTIFY_DONE;
3657 }
3658
3659 /*
3660  * bond_inetaddr_event: handle inetaddr notifier chain events.
3661  *
3662  * We keep track of device IPs primarily to use as source addresses in
3663  * ARP monitor probes (rather than spewing out broadcasts all the time).
3664  *
3665  * We track one IP for the main device (if it has one), plus one per VLAN.
3666  */
3667 static int bond_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
3668 {
3669         struct in_ifaddr *ifa = ptr;
3670         struct net_device *vlan_dev, *event_dev = ifa->ifa_dev->dev;
3671         struct bonding *bond, *bond_next;
3672         struct vlan_entry *vlan, *vlan_next;
3673
3674         list_for_each_entry_safe(bond, bond_next, &bond_dev_list, bond_list) {
3675                 if (bond->dev == event_dev) {
3676                         switch (event) {
3677                         case NETDEV_UP:
3678                                 bond->master_ip = ifa->ifa_local;
3679                                 return NOTIFY_OK;
3680                         case NETDEV_DOWN:
3681                                 bond->master_ip = bond_glean_dev_ip(bond->dev);
3682                                 return NOTIFY_OK;
3683                         default:
3684                                 return NOTIFY_DONE;
3685                         }
3686                 }
3687
3688                 if (list_empty(&bond->vlan_list))
3689                         continue;
3690
3691                 list_for_each_entry_safe(vlan, vlan_next, &bond->vlan_list,
3692                                          vlan_list) {
3693                         vlan_dev = bond->vlgrp->vlan_devices[vlan->vlan_id];
3694                         if (vlan_dev == event_dev) {
3695                                 switch (event) {
3696                                 case NETDEV_UP:
3697                                         vlan->vlan_ip = ifa->ifa_local;
3698                                         return NOTIFY_OK;
3699                                 case NETDEV_DOWN:
3700                                         vlan->vlan_ip =
3701                                                 bond_glean_dev_ip(vlan_dev);
3702                                         return NOTIFY_OK;
3703                                 default:
3704                                         return NOTIFY_DONE;
3705                                 }
3706                         }
3707                 }
3708         }
3709         return NOTIFY_DONE;
3710 }
3711
3712 static struct notifier_block bond_netdev_notifier = {
3713         .notifier_call = bond_netdev_event,
3714 };
3715
3716 static struct notifier_block bond_inetaddr_notifier = {
3717         .notifier_call = bond_inetaddr_event,
3718 };
3719
3720 /*-------------------------- Packet type handling ---------------------------*/
3721
3722 /* register to receive lacpdus on a bond */
3723 static void bond_register_lacpdu(struct bonding *bond)
3724 {
3725         struct packet_type *pk_type = &(BOND_AD_INFO(bond).ad_pkt_type);
3726
3727         /* initialize packet type */
3728         pk_type->type = PKT_TYPE_LACPDU;
3729         pk_type->dev = bond->dev;
3730         pk_type->func = bond_3ad_lacpdu_recv;
3731
3732         dev_add_pack(pk_type);
3733 }
3734
3735 /* unregister to receive lacpdus on a bond */
3736 static void bond_unregister_lacpdu(struct bonding *bond)
3737 {
3738         dev_remove_pack(&(BOND_AD_INFO(bond).ad_pkt_type));
3739 }
3740
3741 /*---------------------------- Hashing Policies -----------------------------*/
3742
3743 /*
3744  * Hash for the the output device based upon layer 3 and layer 4 data. If
3745  * the packet is a frag or not TCP or UDP, just use layer 3 data.  If it is
3746  * altogether not IP, mimic bond_xmit_hash_policy_l2()
3747  */
3748 static int bond_xmit_hash_policy_l34(struct sk_buff *skb,
3749                                     struct net_device *bond_dev, int count)
3750 {
3751         struct ethhdr *data = (struct ethhdr *)skb->data;
3752         struct iphdr *iph = skb->nh.iph;
3753         u16 *layer4hdr = (u16 *)((u32 *)iph + iph->ihl);
3754         int layer4_xor = 0;
3755
3756         if (skb->protocol == __constant_htons(ETH_P_IP)) {
3757                 if (!(iph->frag_off & __constant_htons(IP_MF|IP_OFFSET)) &&
3758                     (iph->protocol == IPPROTO_TCP ||
3759                      iph->protocol == IPPROTO_UDP)) {
3760                         layer4_xor = htons((*layer4hdr ^ *(layer4hdr + 1)));
3761                 }
3762                 return (layer4_xor ^
3763                         ((ntohl(iph->saddr ^ iph->daddr)) & 0xffff)) % count;
3764
3765         }
3766
3767         return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
3768 }
3769
3770 /*
3771  * Hash for the output device based upon layer 2 data
3772  */
3773 static int bond_xmit_hash_policy_l2(struct sk_buff *skb,
3774                                    struct net_device *bond_dev, int count)
3775 {
3776         struct ethhdr *data = (struct ethhdr *)skb->data;
3777
3778         return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
3779 }
3780
3781 /*-------------------------- Device entry points ----------------------------*/
3782
3783 static int bond_open(struct net_device *bond_dev)
3784 {
3785         struct bonding *bond = bond_dev->priv;
3786         struct timer_list *mii_timer = &bond->mii_timer;
3787         struct timer_list *arp_timer = &bond->arp_timer;
3788
3789         bond->kill_timers = 0;
3790
3791         if ((bond->params.mode == BOND_MODE_TLB) ||
3792             (bond->params.mode == BOND_MODE_ALB)) {
3793                 struct timer_list *alb_timer = &(BOND_ALB_INFO(bond).alb_timer);
3794
3795                 /* bond_alb_initialize must be called before the timer
3796                  * is started.
3797                  */
3798                 if (bond_alb_initialize(bond, (bond->params.mode == BOND_MODE_ALB))) {
3799                         /* something went wrong - fail the open operation */
3800                         return -1;
3801                 }
3802
3803                 init_timer(alb_timer);
3804                 alb_timer->expires  = jiffies + 1;
3805                 alb_timer->data     = (unsigned long)bond;
3806                 alb_timer->function = (void *)&bond_alb_monitor;
3807                 add_timer(alb_timer);
3808         }
3809
3810         if (bond->params.miimon) {  /* link check interval, in milliseconds. */
3811                 init_timer(mii_timer);
3812                 mii_timer->expires  = jiffies + 1;
3813                 mii_timer->data     = (unsigned long)bond_dev;
3814                 mii_timer->function = (void *)&bond_mii_monitor;
3815                 add_timer(mii_timer);
3816         }
3817
3818         if (bond->params.arp_interval) {  /* arp interval, in milliseconds. */
3819                 init_timer(arp_timer);
3820                 arp_timer->expires  = jiffies + 1;
3821                 arp_timer->data     = (unsigned long)bond_dev;
3822                 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
3823                         arp_timer->function = (void *)&bond_activebackup_arp_mon;
3824                 } else {
3825                         arp_timer->function = (void *)&bond_loadbalance_arp_mon;
3826                 }
3827                 add_timer(arp_timer);
3828         }
3829
3830         if (bond->params.mode == BOND_MODE_8023AD) {
3831                 struct timer_list *ad_timer = &(BOND_AD_INFO(bond).ad_timer);
3832                 init_timer(ad_timer);
3833                 ad_timer->expires  = jiffies + 1;
3834                 ad_timer->data     = (unsigned long)bond;
3835                 ad_timer->function = (void *)&bond_3ad_state_machine_handler;
3836                 add_timer(ad_timer);
3837
3838                 /* register to receive LACPDUs */
3839                 bond_register_lacpdu(bond);
3840         }
3841
3842         return 0;
3843 }
3844
3845 static int bond_close(struct net_device *bond_dev)
3846 {
3847         struct bonding *bond = bond_dev->priv;
3848
3849         if (bond->params.mode == BOND_MODE_8023AD) {
3850                 /* Unregister the receive of LACPDUs */
3851                 bond_unregister_lacpdu(bond);
3852         }
3853
3854         write_lock_bh(&bond->lock);
3855
3856         bond_mc_list_destroy(bond);
3857
3858         /* signal timers not to re-arm */
3859         bond->kill_timers = 1;
3860
3861         write_unlock_bh(&bond->lock);
3862
3863         /* del_timer_sync must run without holding the bond->lock
3864          * because a running timer might be trying to hold it too
3865          */
3866
3867         if (bond->params.miimon) {  /* link check interval, in milliseconds. */
3868                 del_timer_sync(&bond->mii_timer);
3869         }
3870
3871         if (bond->params.arp_interval) {  /* arp interval, in milliseconds. */
3872                 del_timer_sync(&bond->arp_timer);
3873         }
3874
3875         switch (bond->params.mode) {
3876         case BOND_MODE_8023AD:
3877                 del_timer_sync(&(BOND_AD_INFO(bond).ad_timer));
3878                 break;
3879         case BOND_MODE_TLB:
3880         case BOND_MODE_ALB:
3881                 del_timer_sync(&(BOND_ALB_INFO(bond).alb_timer));
3882                 break;
3883         default:
3884                 break;
3885         }
3886
3887         /* Release the bonded slaves */
3888         bond_release_all(bond_dev);
3889
3890         if ((bond->params.mode == BOND_MODE_TLB) ||
3891             (bond->params.mode == BOND_MODE_ALB)) {
3892                 /* Must be called only after all
3893                  * slaves have been released
3894                  */
3895                 bond_alb_deinitialize(bond);
3896         }
3897
3898         return 0;
3899 }
3900
3901 static struct net_device_stats *bond_get_stats(struct net_device *bond_dev)
3902 {
3903         struct bonding *bond = bond_dev->priv;
3904         struct net_device_stats *stats = &(bond->stats), *sstats;
3905         struct slave *slave;
3906         int i;
3907
3908         memset(stats, 0, sizeof(struct net_device_stats));
3909
3910         read_lock_bh(&bond->lock);
3911
3912         bond_for_each_slave(bond, slave, i) {
3913                 sstats = slave->dev->get_stats(slave->dev);
3914
3915                 stats->rx_packets += sstats->rx_packets;
3916                 stats->rx_bytes += sstats->rx_bytes;
3917                 stats->rx_errors += sstats->rx_errors;
3918                 stats->rx_dropped += sstats->rx_dropped;
3919
3920                 stats->tx_packets += sstats->tx_packets;
3921                 stats->tx_bytes += sstats->tx_bytes;
3922                 stats->tx_errors += sstats->tx_errors;
3923                 stats->tx_dropped += sstats->tx_dropped;
3924
3925                 stats->multicast += sstats->multicast;
3926                 stats->collisions += sstats->collisions;
3927
3928                 stats->rx_length_errors += sstats->rx_length_errors;
3929                 stats->rx_over_errors += sstats->rx_over_errors;
3930                 stats->rx_crc_errors += sstats->rx_crc_errors;
3931                 stats->rx_frame_errors += sstats->rx_frame_errors;
3932                 stats->rx_fifo_errors += sstats->rx_fifo_errors;
3933                 stats->rx_missed_errors += sstats->rx_missed_errors;
3934
3935                 stats->tx_aborted_errors += sstats->tx_aborted_errors;
3936                 stats->tx_carrier_errors += sstats->tx_carrier_errors;
3937                 stats->tx_fifo_errors += sstats->tx_fifo_errors;
3938                 stats->tx_heartbeat_errors += sstats->tx_heartbeat_errors;
3939                 stats->tx_window_errors += sstats->tx_window_errors;
3940         }
3941
3942         read_unlock_bh(&bond->lock);
3943
3944         return stats;
3945 }
3946
3947 static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
3948 {
3949         struct net_device *slave_dev = NULL;
3950         struct ifbond k_binfo;
3951         struct ifbond __user *u_binfo = NULL;
3952         struct ifslave k_sinfo;
3953         struct ifslave __user *u_sinfo = NULL;
3954         struct mii_ioctl_data *mii = NULL;
3955         int res = 0;
3956
3957         dprintk("bond_ioctl: master=%s, cmd=%d\n",
3958                 bond_dev->name, cmd);
3959
3960         switch (cmd) {
3961         case SIOCGMIIPHY:
3962                 mii = if_mii(ifr);
3963                 if (!mii) {
3964                         return -EINVAL;
3965                 }
3966                 mii->phy_id = 0;
3967                 /* Fall Through */
3968         case SIOCGMIIREG:
3969                 /*
3970                  * We do this again just in case we were called by SIOCGMIIREG
3971                  * instead of SIOCGMIIPHY.
3972                  */
3973                 mii = if_mii(ifr);
3974                 if (!mii) {
3975                         return -EINVAL;
3976                 }
3977
3978                 if (mii->reg_num == 1) {
3979                         struct bonding *bond = bond_dev->priv;
3980                         mii->val_out = 0;
3981                         read_lock_bh(&bond->lock);
3982                         read_lock(&bond->curr_slave_lock);
3983                         if (bond->curr_active_slave) {
3984                                 mii->val_out = BMSR_LSTATUS;
3985                         }
3986                         read_unlock(&bond->curr_slave_lock);
3987                         read_unlock_bh(&bond->lock);
3988                 }
3989
3990                 return 0;
3991         case BOND_INFO_QUERY_OLD:
3992         case SIOCBONDINFOQUERY:
3993                 u_binfo = (struct ifbond __user *)ifr->ifr_data;
3994
3995                 if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond))) {
3996                         return -EFAULT;
3997                 }
3998
3999                 res = bond_info_query(bond_dev, &k_binfo);
4000                 if (res == 0) {
4001                         if (copy_to_user(u_binfo, &k_binfo, sizeof(ifbond))) {
4002                                 return -EFAULT;
4003                         }
4004                 }
4005
4006                 return res;
4007         case BOND_SLAVE_INFO_QUERY_OLD:
4008         case SIOCBONDSLAVEINFOQUERY:
4009                 u_sinfo = (struct ifslave __user *)ifr->ifr_data;
4010
4011                 if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave))) {
4012                         return -EFAULT;
4013                 }
4014
4015                 res = bond_slave_info_query(bond_dev, &k_sinfo);
4016                 if (res == 0) {
4017                         if (copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave))) {
4018                                 return -EFAULT;
4019                         }
4020                 }
4021
4022                 return res;
4023         default:
4024                 /* Go on */
4025                 break;
4026         }
4027
4028         if (!capable(CAP_NET_ADMIN)) {
4029                 return -EPERM;
4030         }
4031
4032         down_write(&(bonding_rwsem));
4033         slave_dev = dev_get_by_name(ifr->ifr_slave);
4034
4035         dprintk("slave_dev=%p: \n", slave_dev);
4036
4037         if (!slave_dev) {
4038                 res = -ENODEV;
4039         } else {
4040                 dprintk("slave_dev->name=%s: \n", slave_dev->name);
4041                 switch (cmd) {
4042                 case BOND_ENSLAVE_OLD:
4043                 case SIOCBONDENSLAVE:
4044                         res = bond_enslave(bond_dev, slave_dev);
4045                         break;
4046                 case BOND_RELEASE_OLD:
4047                 case SIOCBONDRELEASE:
4048                         res = bond_release(bond_dev, slave_dev);
4049                         break;
4050                 case BOND_SETHWADDR_OLD:
4051                 case SIOCBONDSETHWADDR:
4052                         res = bond_sethwaddr(bond_dev, slave_dev);
4053                         break;
4054                 case BOND_CHANGE_ACTIVE_OLD:
4055                 case SIOCBONDCHANGEACTIVE:
4056                         res = bond_ioctl_change_active(bond_dev, slave_dev);
4057                         break;
4058                 default:
4059                         res = -EOPNOTSUPP;
4060                 }
4061
4062                 dev_put(slave_dev);
4063         }
4064
4065         up_write(&(bonding_rwsem));
4066         return res;
4067 }
4068
4069 static void bond_set_multicast_list(struct net_device *bond_dev)
4070 {
4071         struct bonding *bond = bond_dev->priv;
4072         struct dev_mc_list *dmi;
4073
4074         write_lock_bh(&bond->lock);
4075
4076         /*
4077          * Do promisc before checking multicast_mode
4078          */
4079         if ((bond_dev->flags & IFF_PROMISC) && !(bond->flags & IFF_PROMISC)) {
4080                 bond_set_promiscuity(bond, 1);
4081         }
4082
4083         if (!(bond_dev->flags & IFF_PROMISC) && (bond->flags & IFF_PROMISC)) {
4084                 bond_set_promiscuity(bond, -1);
4085         }
4086
4087         /* set allmulti flag to slaves */
4088         if ((bond_dev->flags & IFF_ALLMULTI) && !(bond->flags & IFF_ALLMULTI)) {
4089                 bond_set_allmulti(bond, 1);
4090         }
4091
4092         if (!(bond_dev->flags & IFF_ALLMULTI) && (bond->flags & IFF_ALLMULTI)) {
4093                 bond_set_allmulti(bond, -1);
4094         }
4095
4096         bond->flags = bond_dev->flags;
4097
4098         /* looking for addresses to add to slaves' mc list */
4099         for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
4100                 if (!bond_mc_list_find_dmi(dmi, bond->mc_list)) {
4101                         bond_mc_add(bond, dmi->dmi_addr, dmi->dmi_addrlen);
4102                 }
4103         }
4104
4105         /* looking for addresses to delete from slaves' list */
4106         for (dmi = bond->mc_list; dmi; dmi = dmi->next) {
4107                 if (!bond_mc_list_find_dmi(dmi, bond_dev->mc_list)) {
4108                         bond_mc_delete(bond, dmi->dmi_addr, dmi->dmi_addrlen);
4109                 }
4110         }
4111
4112         /* save master's multicast list */
4113         bond_mc_list_destroy(bond);
4114         bond_mc_list_copy(bond_dev->mc_list, bond, GFP_ATOMIC);
4115
4116         write_unlock_bh(&bond->lock);
4117 }
4118
4119 /*
4120  * Change the MTU of all of a master's slaves to match the master
4121  */
4122 static int bond_change_mtu(struct net_device *bond_dev, int new_mtu)
4123 {
4124         struct bonding *bond = bond_dev->priv;
4125         struct slave *slave, *stop_at;
4126         int res = 0;
4127         int i;
4128
4129         dprintk("bond=%p, name=%s, new_mtu=%d\n", bond,
4130                 (bond_dev ? bond_dev->name : "None"), new_mtu);
4131
4132         /* Can't hold bond->lock with bh disabled here since
4133          * some base drivers panic. On the other hand we can't
4134          * hold bond->lock without bh disabled because we'll
4135          * deadlock. The only solution is to rely on the fact
4136          * that we're under rtnl_lock here, and the slaves
4137          * list won't change. This doesn't solve the problem
4138          * of setting the slave's MTU while it is
4139          * transmitting, but the assumption is that the base
4140          * driver can handle that.
4141          *
4142          * TODO: figure out a way to safely iterate the slaves
4143          * list, but without holding a lock around the actual
4144          * call to the base driver.
4145          */
4146
4147         bond_for_each_slave(bond, slave, i) {
4148                 dprintk("s %p s->p %p c_m %p\n", slave,
4149                         slave->prev, slave->dev->change_mtu);
4150
4151                 res = dev_set_mtu(slave->dev, new_mtu);
4152
4153                 if (res) {
4154                         /* If we failed to set the slave's mtu to the new value
4155                          * we must abort the operation even in ACTIVE_BACKUP
4156                          * mode, because if we allow the backup slaves to have
4157                          * different mtu values than the active slave we'll
4158                          * need to change their mtu when doing a failover. That
4159                          * means changing their mtu from timer context, which
4160                          * is probably not a good idea.
4161                          */
4162                         dprintk("err %d %s\n", res, slave->dev->name);
4163                         goto unwind;
4164                 }
4165         }
4166
4167         bond_dev->mtu = new_mtu;
4168
4169         return 0;
4170
4171 unwind:
4172         /* unwind from head to the slave that failed */
4173         stop_at = slave;
4174         bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
4175                 int tmp_res;
4176
4177                 tmp_res = dev_set_mtu(slave->dev, bond_dev->mtu);
4178                 if (tmp_res) {
4179                         dprintk("unwind err %d dev %s\n", tmp_res,
4180                                 slave->dev->name);
4181                 }
4182         }
4183
4184         return res;
4185 }
4186
4187 /*
4188  * Change HW address
4189  *
4190  * Note that many devices must be down to change the HW address, and
4191  * downing the master releases all slaves.  We can make bonds full of
4192  * bonding devices to test this, however.
4193  */
4194 static int bond_set_mac_address(struct net_device *bond_dev, void *addr)
4195 {
4196         struct bonding *bond = bond_dev->priv;
4197         struct sockaddr *sa = addr, tmp_sa;
4198         struct slave *slave, *stop_at;
4199         int res = 0;
4200         int i;
4201
4202         dprintk("bond=%p, name=%s\n", bond, (bond_dev ? bond_dev->name : "None"));
4203
4204         if (!is_valid_ether_addr(sa->sa_data)) {
4205                 return -EADDRNOTAVAIL;
4206         }
4207
4208         /* Can't hold bond->lock with bh disabled here since
4209          * some base drivers panic. On the other hand we can't
4210          * hold bond->lock without bh disabled because we'll
4211          * deadlock. The only solution is to rely on the fact
4212          * that we're under rtnl_lock here, and the slaves
4213          * list won't change. This doesn't solve the problem
4214          * of setting the slave's hw address while it is
4215          * transmitting, but the assumption is that the base
4216          * driver can handle that.
4217          *
4218          * TODO: figure out a way to safely iterate the slaves
4219          * list, but without holding a lock around the actual
4220          * call to the base driver.
4221          */
4222
4223         bond_for_each_slave(bond, slave, i) {
4224                 dprintk("slave %p %s\n", slave, slave->dev->name);
4225
4226                 if (slave->dev->set_mac_address == NULL) {
4227                         res = -EOPNOTSUPP;
4228                         dprintk("EOPNOTSUPP %s\n", slave->dev->name);
4229                         goto unwind;
4230                 }
4231
4232                 res = dev_set_mac_address(slave->dev, addr);
4233                 if (res) {
4234                         /* TODO: consider downing the slave
4235                          * and retry ?
4236                          * User should expect communications
4237                          * breakage anyway until ARP finish
4238                          * updating, so...
4239                          */
4240                         dprintk("err %d %s\n", res, slave->dev->name);
4241                         goto unwind;
4242                 }
4243         }
4244
4245         /* success */
4246         memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
4247         return 0;
4248
4249 unwind:
4250         memcpy(tmp_sa.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
4251         tmp_sa.sa_family = bond_dev->type;
4252
4253         /* unwind from head to the slave that failed */
4254         stop_at = slave;
4255         bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
4256                 int tmp_res;
4257
4258                 tmp_res = dev_set_mac_address(slave->dev, &tmp_sa);
4259                 if (tmp_res) {
4260                         dprintk("unwind err %d dev %s\n", tmp_res,
4261                                 slave->dev->name);
4262                 }
4263         }
4264
4265         return res;
4266 }
4267
4268 static int bond_xmit_roundrobin(struct sk_buff *skb, struct net_device *bond_dev)
4269 {
4270         struct bonding *bond = bond_dev->priv;
4271         struct slave *slave, *start_at;
4272         int i;
4273         int res = 1;
4274
4275         read_lock(&bond->lock);
4276
4277         if (!BOND_IS_OK(bond)) {
4278                 goto out;
4279         }
4280
4281         read_lock(&bond->curr_slave_lock);
4282         slave = start_at = bond->curr_active_slave;
4283         read_unlock(&bond->curr_slave_lock);
4284
4285         if (!slave) {
4286                 goto out;
4287         }
4288
4289         bond_for_each_slave_from(bond, slave, i, start_at) {
4290                 if (IS_UP(slave->dev) &&
4291                     (slave->link == BOND_LINK_UP) &&
4292                     (slave->state == BOND_STATE_ACTIVE)) {
4293                         res = bond_dev_queue_xmit(bond, skb, slave->dev);
4294
4295                         write_lock(&bond->curr_slave_lock);
4296                         bond->curr_active_slave = slave->next;
4297                         write_unlock(&bond->curr_slave_lock);
4298
4299                         break;
4300                 }
4301         }
4302
4303
4304 out:
4305         if (res) {
4306                 /* no suitable interface, frame not sent */
4307                 dev_kfree_skb(skb);
4308         }
4309         read_unlock(&bond->lock);
4310         return 0;
4311 }
4312
4313 static void bond_activebackup_xmit_copy(struct sk_buff *skb,
4314                                         struct bonding *bond,
4315                                         struct slave *slave)
4316 {
4317         struct sk_buff *skb2 = skb_copy(skb, GFP_ATOMIC);
4318         struct ethhdr *eth_data;
4319         u8 *hwaddr;
4320         int res;
4321
4322         if (!skb2) {
4323                 printk(KERN_ERR DRV_NAME ": Error: "
4324                        "bond_activebackup_xmit_copy(): skb_copy() failed\n");
4325                 return;
4326         }
4327
4328         skb2->mac.raw = (unsigned char *)skb2->data;
4329         eth_data = eth_hdr(skb2);
4330
4331         /* Pick an appropriate source MAC address
4332          *      -- use slave's perm MAC addr, unless used by bond
4333          *      -- otherwise, borrow active slave's perm MAC addr
4334          *         since that will not be used
4335          */
4336         hwaddr = slave->perm_hwaddr;
4337         if (!memcmp(eth_data->h_source, hwaddr, ETH_ALEN))
4338                 hwaddr = bond->curr_active_slave->perm_hwaddr;
4339
4340         /* Set source MAC address appropriately */
4341         memcpy(eth_data->h_source, hwaddr, ETH_ALEN);
4342
4343         res = bond_dev_queue_xmit(bond, skb2, slave->dev);
4344         if (res)
4345                 dev_kfree_skb(skb2);
4346
4347         return;
4348 }
4349
4350 /*
4351  * in active-backup mode, we know that bond->curr_active_slave is always valid if
4352  * the bond has a usable interface.
4353  */
4354 static int bond_xmit_activebackup(struct sk_buff *skb, struct net_device *bond_dev)
4355 {
4356         struct bonding *bond = bond_dev->priv;
4357         int res = 1;
4358
4359         read_lock(&bond->lock);
4360         read_lock(&bond->curr_slave_lock);
4361
4362         if (!BOND_IS_OK(bond)) {
4363                 goto out;
4364         }
4365
4366         if (!bond->curr_active_slave)
4367                 goto out;
4368
4369         /* Xmit IGMP frames on all slaves to ensure rapid fail-over
4370            for multicast traffic on snooping switches */
4371         if (skb->protocol == __constant_htons(ETH_P_IP) &&
4372             skb->nh.iph->protocol == IPPROTO_IGMP) {
4373                 struct slave *slave, *active_slave;
4374                 int i;
4375
4376                 active_slave = bond->curr_active_slave;
4377                 bond_for_each_slave_from_to(bond, slave, i, active_slave->next,
4378                                             active_slave->prev)
4379                         if (IS_UP(slave->dev) &&
4380                             (slave->link == BOND_LINK_UP))
4381                                 bond_activebackup_xmit_copy(skb, bond, slave);
4382         }
4383
4384         res = bond_dev_queue_xmit(bond, skb, bond->curr_active_slave->dev);
4385
4386 out:
4387         if (res) {
4388                 /* no suitable interface, frame not sent */
4389                 dev_kfree_skb(skb);
4390         }
4391         read_unlock(&bond->curr_slave_lock);
4392         read_unlock(&bond->lock);
4393         return 0;
4394 }
4395
4396 /*
4397  * In bond_xmit_xor() , we determine the output device by using a pre-
4398  * determined xmit_hash_policy(), If the selected device is not enabled,
4399  * find the next active slave.
4400  */
4401 static int bond_xmit_xor(struct sk_buff *skb, struct net_device *bond_dev)
4402 {
4403         struct bonding *bond = bond_dev->priv;
4404         struct slave *slave, *start_at;
4405         int slave_no;
4406         int i;
4407         int res = 1;
4408
4409         read_lock(&bond->lock);
4410
4411         if (!BOND_IS_OK(bond)) {
4412                 goto out;
4413         }
4414
4415         slave_no = bond->xmit_hash_policy(skb, bond_dev, bond->slave_cnt);
4416
4417         bond_for_each_slave(bond, slave, i) {
4418                 slave_no--;
4419                 if (slave_no < 0) {
4420                         break;
4421                 }
4422         }
4423
4424         start_at = slave;
4425
4426         bond_for_each_slave_from(bond, slave, i, start_at) {
4427                 if (IS_UP(slave->dev) &&
4428                     (slave->link == BOND_LINK_UP) &&
4429                     (slave->state == BOND_STATE_ACTIVE)) {
4430                         res = bond_dev_queue_xmit(bond, skb, slave->dev);
4431                         break;
4432                 }
4433         }
4434
4435 out:
4436         if (res) {
4437                 /* no suitable interface, frame not sent */
4438                 dev_kfree_skb(skb);
4439         }
4440         read_unlock(&bond->lock);
4441         return 0;
4442 }
4443
4444 /*
4445  * in broadcast mode, we send everything to all usable interfaces.
4446  */
4447 static int bond_xmit_broadcast(struct sk_buff *skb, struct net_device *bond_dev)
4448 {
4449         struct bonding *bond = bond_dev->priv;
4450         struct slave *slave, *start_at;
4451         struct net_device *tx_dev = NULL;
4452         int i;
4453         int res = 1;
4454
4455         read_lock(&bond->lock);
4456
4457         if (!BOND_IS_OK(bond)) {
4458                 goto out;
4459         }
4460
4461         read_lock(&bond->curr_slave_lock);
4462         start_at = bond->curr_active_slave;
4463         read_unlock(&bond->curr_slave_lock);
4464
4465         if (!start_at) {
4466                 goto out;
4467         }
4468
4469         bond_for_each_slave_from(bond, slave, i, start_at) {
4470                 if (IS_UP(slave->dev) &&
4471                     (slave->link == BOND_LINK_UP) &&
4472                     (slave->state == BOND_STATE_ACTIVE)) {
4473                         if (tx_dev) {
4474                                 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
4475                                 if (!skb2) {
4476                                         printk(KERN_ERR DRV_NAME
4477                                                ": %s: Error: bond_xmit_broadcast(): "
4478                                                "skb_clone() failed\n",
4479                                                bond_dev->name);
4480                                         continue;
4481                                 }
4482
4483                                 res = bond_dev_queue_xmit(bond, skb2, tx_dev);
4484                                 if (res) {
4485                                         dev_kfree_skb(skb2);
4486                                         continue;
4487                                 }
4488                         }
4489                         tx_dev = slave->dev;
4490                 }
4491         }
4492
4493         if (tx_dev) {
4494                 res = bond_dev_queue_xmit(bond, skb, tx_dev);
4495         }
4496
4497 out:
4498         if (res) {
4499                 /* no suitable interface, frame not sent */
4500                 dev_kfree_skb(skb);
4501         }
4502         /* frame sent to all suitable interfaces */
4503         read_unlock(&bond->lock);
4504         return 0;
4505 }
4506
4507 /*------------------------- Device initialization ---------------------------*/
4508
4509 /*
4510  * set bond mode specific net device operations
4511  */
4512 void bond_set_mode_ops(struct bonding *bond, int mode)
4513 {
4514         struct net_device *bond_dev = bond->dev;
4515
4516         switch (mode) {
4517         case BOND_MODE_ROUNDROBIN:
4518                 bond_dev->hard_start_xmit = bond_xmit_roundrobin;
4519                 break;
4520         case BOND_MODE_ACTIVEBACKUP:
4521                 bond_dev->hard_start_xmit = bond_xmit_activebackup;
4522                 break;
4523         case BOND_MODE_XOR:
4524                 bond_dev->hard_start_xmit = bond_xmit_xor;
4525                 if (bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER34)
4526                         bond->xmit_hash_policy = bond_xmit_hash_policy_l34;
4527                 else
4528                         bond->xmit_hash_policy = bond_xmit_hash_policy_l2;
4529                 break;
4530         case BOND_MODE_BROADCAST:
4531                 bond_dev->hard_start_xmit = bond_xmit_broadcast;
4532                 break;
4533         case BOND_MODE_8023AD:
4534                 bond_dev->hard_start_xmit = bond_3ad_xmit_xor;
4535                 if (bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER34)
4536                         bond->xmit_hash_policy = bond_xmit_hash_policy_l34;
4537                 else
4538                         bond->xmit_hash_policy = bond_xmit_hash_policy_l2;
4539                 break;
4540         case BOND_MODE_TLB:
4541         case BOND_MODE_ALB:
4542                 bond_dev->hard_start_xmit = bond_alb_xmit;
4543                 bond_dev->set_mac_address = bond_alb_set_mac_address;
4544                 break;
4545         default:
4546                 /* Should never happen, mode already checked */
4547                 printk(KERN_ERR DRV_NAME
4548                        ": %s: Error: Unknown bonding mode %d\n",
4549                        bond_dev->name,
4550                        mode);
4551                 break;
4552         }
4553 }
4554
4555 static void bond_ethtool_get_drvinfo(struct net_device *bond_dev,
4556                                     struct ethtool_drvinfo *drvinfo)
4557 {
4558         strncpy(drvinfo->driver, DRV_NAME, 32);
4559         strncpy(drvinfo->version, DRV_VERSION, 32);
4560         snprintf(drvinfo->fw_version, 32, "%d", BOND_ABI_VERSION);
4561 }
4562
4563 static struct ethtool_ops bond_ethtool_ops = {
4564         .get_tx_csum            = ethtool_op_get_tx_csum,
4565         .get_sg                 = ethtool_op_get_sg,
4566         .get_drvinfo            = bond_ethtool_get_drvinfo,
4567 };
4568
4569 /*
4570  * Does not allocate but creates a /proc entry.
4571  * Allowed to fail.
4572  */
4573 static int bond_init(struct net_device *bond_dev, struct bond_params *params)
4574 {
4575         struct bonding *bond = bond_dev->priv;
4576
4577         dprintk("Begin bond_init for %s\n", bond_dev->name);
4578
4579         /* initialize rwlocks */
4580         rwlock_init(&bond->lock);
4581         rwlock_init(&bond->curr_slave_lock);
4582
4583         bond->params = *params; /* copy params struct */
4584
4585         /* Initialize pointers */
4586         bond->first_slave = NULL;
4587         bond->curr_active_slave = NULL;
4588         bond->current_arp_slave = NULL;
4589         bond->primary_slave = NULL;
4590         bond->dev = bond_dev;
4591         INIT_LIST_HEAD(&bond->vlan_list);
4592
4593         /* Initialize the device entry points */
4594         bond_dev->open = bond_open;
4595         bond_dev->stop = bond_close;
4596         bond_dev->get_stats = bond_get_stats;
4597         bond_dev->do_ioctl = bond_do_ioctl;
4598         bond_dev->ethtool_ops = &bond_ethtool_ops;
4599         bond_dev->set_multicast_list = bond_set_multicast_list;
4600         bond_dev->change_mtu = bond_change_mtu;
4601         bond_dev->set_mac_address = bond_set_mac_address;
4602
4603         bond_set_mode_ops(bond, bond->params.mode);
4604
4605         bond_dev->destructor = free_netdev;
4606
4607         /* Initialize the device options */
4608         bond_dev->tx_queue_len = 0;
4609         bond_dev->flags |= IFF_MASTER|IFF_MULTICAST;
4610
4611         /* At first, we block adding VLANs. That's the only way to
4612          * prevent problems that occur when adding VLANs over an
4613          * empty bond. The block will be removed once non-challenged
4614          * slaves are enslaved.
4615          */
4616         bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
4617
4618         /* don't acquire bond device's xmit_lock when 
4619          * transmitting */
4620         bond_dev->features |= NETIF_F_LLTX;
4621
4622         /* By default, we declare the bond to be fully
4623          * VLAN hardware accelerated capable. Special
4624          * care is taken in the various xmit functions
4625          * when there are slaves that are not hw accel
4626          * capable
4627          */
4628         bond_dev->vlan_rx_register = bond_vlan_rx_register;
4629         bond_dev->vlan_rx_add_vid  = bond_vlan_rx_add_vid;
4630         bond_dev->vlan_rx_kill_vid = bond_vlan_rx_kill_vid;
4631         bond_dev->features |= (NETIF_F_HW_VLAN_TX |
4632                                NETIF_F_HW_VLAN_RX |
4633                                NETIF_F_HW_VLAN_FILTER);
4634
4635 #ifdef CONFIG_PROC_FS
4636         bond_create_proc_entry(bond);
4637 #endif
4638
4639         list_add_tail(&bond->bond_list, &bond_dev_list);
4640
4641         return 0;
4642 }
4643
4644 /* De-initialize device specific data.
4645  * Caller must hold rtnl_lock.
4646  */
4647 void bond_deinit(struct net_device *bond_dev)
4648 {
4649         struct bonding *bond = bond_dev->priv;
4650
4651         list_del(&bond->bond_list);
4652
4653 #ifdef CONFIG_PROC_FS
4654         bond_remove_proc_entry(bond);
4655 #endif
4656 }
4657
4658 /* Unregister and free all bond devices.
4659  * Caller must hold rtnl_lock.
4660  */
4661 static void bond_free_all(void)
4662 {
4663         struct bonding *bond, *nxt;
4664
4665         list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list) {
4666                 struct net_device *bond_dev = bond->dev;
4667
4668                 unregister_netdevice(bond_dev);
4669                 bond_deinit(bond_dev);
4670         }
4671
4672 #ifdef CONFIG_PROC_FS
4673         bond_destroy_proc_dir();
4674 #endif
4675 }
4676
4677 /*------------------------- Module initialization ---------------------------*/
4678
4679 /*
4680  * Convert string input module parms.  Accept either the
4681  * number of the mode or its string name.
4682  */
4683 int bond_parse_parm(char *mode_arg, struct bond_parm_tbl *tbl)
4684 {
4685         int i;
4686
4687         for (i = 0; tbl[i].modename; i++) {
4688                 if ((isdigit(*mode_arg) &&
4689                      tbl[i].mode == simple_strtol(mode_arg, NULL, 0)) ||
4690                     (strncmp(mode_arg, tbl[i].modename,
4691                              strlen(tbl[i].modename)) == 0)) {
4692                         return tbl[i].mode;
4693                 }
4694         }
4695
4696         return -1;
4697 }
4698
4699 static int bond_check_params(struct bond_params *params)
4700 {
4701         /*
4702          * Convert string parameters.
4703          */
4704         if (mode) {
4705                 bond_mode = bond_parse_parm(mode, bond_mode_tbl);
4706                 if (bond_mode == -1) {
4707                         printk(KERN_ERR DRV_NAME
4708                                ": Error: Invalid bonding mode \"%s\"\n",
4709                                mode == NULL ? "NULL" : mode);
4710                         return -EINVAL;
4711                 }
4712         }
4713
4714         if (xmit_hash_policy) {
4715                 if ((bond_mode != BOND_MODE_XOR) &&
4716                     (bond_mode != BOND_MODE_8023AD)) {
4717                         printk(KERN_INFO DRV_NAME
4718                                ": xor_mode param is irrelevant in mode %s\n",
4719                                bond_mode_name(bond_mode));
4720                 } else {
4721                         xmit_hashtype = bond_parse_parm(xmit_hash_policy,
4722                                                         xmit_hashtype_tbl);
4723                         if (xmit_hashtype == -1) {
4724                                 printk(KERN_ERR DRV_NAME
4725                                 ": Error: Invalid xmit_hash_policy \"%s\"\n",
4726                                 xmit_hash_policy == NULL ? "NULL" :
4727                                        xmit_hash_policy);
4728                                 return -EINVAL;
4729                         }
4730                 }
4731         }
4732
4733         if (lacp_rate) {
4734                 if (bond_mode != BOND_MODE_8023AD) {
4735                         printk(KERN_INFO DRV_NAME
4736                                ": lacp_rate param is irrelevant in mode %s\n",
4737                                bond_mode_name(bond_mode));
4738                 } else {
4739                         lacp_fast = bond_parse_parm(lacp_rate, bond_lacp_tbl);
4740                         if (lacp_fast == -1) {
4741                                 printk(KERN_ERR DRV_NAME
4742                                        ": Error: Invalid lacp rate \"%s\"\n",
4743                                        lacp_rate == NULL ? "NULL" : lacp_rate);
4744                                 return -EINVAL;
4745                         }
4746                 }
4747         }
4748
4749         if (max_bonds < 1 || max_bonds > INT_MAX) {
4750                 printk(KERN_WARNING DRV_NAME
4751                        ": Warning: max_bonds (%d) not in range %d-%d, so it "
4752                        "was reset to BOND_DEFAULT_MAX_BONDS (%d)\n",
4753                        max_bonds, 1, INT_MAX, BOND_DEFAULT_MAX_BONDS);
4754                 max_bonds = BOND_DEFAULT_MAX_BONDS;
4755         }
4756
4757         if (miimon < 0) {
4758                 printk(KERN_WARNING DRV_NAME
4759                        ": Warning: miimon module parameter (%d), "
4760                        "not in range 0-%d, so it was reset to %d\n",
4761                        miimon, INT_MAX, BOND_LINK_MON_INTERV);
4762                 miimon = BOND_LINK_MON_INTERV;
4763         }
4764
4765         if (updelay < 0) {
4766                 printk(KERN_WARNING DRV_NAME
4767                        ": Warning: updelay module parameter (%d), "
4768                        "not in range 0-%d, so it was reset to 0\n",
4769                        updelay, INT_MAX);
4770                 updelay = 0;
4771         }
4772
4773         if (downdelay < 0) {
4774                 printk(KERN_WARNING DRV_NAME
4775                        ": Warning: downdelay module parameter (%d), "
4776                        "not in range 0-%d, so it was reset to 0\n",
4777                        downdelay, INT_MAX);
4778                 downdelay = 0;
4779         }
4780
4781         if ((use_carrier != 0) && (use_carrier != 1)) {
4782                 printk(KERN_WARNING DRV_NAME
4783                        ": Warning: use_carrier module parameter (%d), "
4784                        "not of valid value (0/1), so it was set to 1\n",
4785                        use_carrier);
4786                 use_carrier = 1;
4787         }
4788
4789         /* reset values for 802.3ad */
4790         if (bond_mode == BOND_MODE_8023AD) {
4791                 if (!miimon) {
4792                         printk(KERN_WARNING DRV_NAME
4793                                ": Warning: miimon must be specified, "
4794                                "otherwise bonding will not detect link "
4795                                "failure, speed and duplex which are "
4796                                "essential for 802.3ad operation\n");
4797                         printk(KERN_WARNING "Forcing miimon to 100msec\n");
4798                         miimon = 100;
4799                 }
4800         }
4801
4802         /* reset values for TLB/ALB */
4803         if ((bond_mode == BOND_MODE_TLB) ||
4804             (bond_mode == BOND_MODE_ALB)) {
4805                 if (!miimon) {
4806                         printk(KERN_WARNING DRV_NAME
4807                                ": Warning: miimon must be specified, "
4808                                "otherwise bonding will not detect link "
4809                                "failure and link speed which are essential "
4810                                "for TLB/ALB load balancing\n");
4811                         printk(KERN_WARNING "Forcing miimon to 100msec\n");
4812                         miimon = 100;
4813                 }
4814         }
4815
4816         if (bond_mode == BOND_MODE_ALB) {
4817                 printk(KERN_NOTICE DRV_NAME
4818                        ": In ALB mode you might experience client "
4819                        "disconnections upon reconnection of a link if the "
4820                        "bonding module updelay parameter (%d msec) is "
4821                        "incompatible with the forwarding delay time of the "
4822                        "switch\n",
4823                        updelay);
4824         }
4825
4826         if (!miimon) {
4827                 if (updelay || downdelay) {
4828                         /* just warn the user the up/down delay will have
4829                          * no effect since miimon is zero...
4830                          */
4831                         printk(KERN_WARNING DRV_NAME
4832                                ": Warning: miimon module parameter not set "
4833                                "and updelay (%d) or downdelay (%d) module "
4834                                "parameter is set; updelay and downdelay have "
4835                                "no effect unless miimon is set\n",
4836                                updelay, downdelay);
4837                 }
4838         } else {
4839                 /* don't allow arp monitoring */
4840                 if (arp_interval) {
4841                         printk(KERN_WARNING DRV_NAME
4842                                ": Warning: miimon (%d) and arp_interval (%d) "
4843                                "can't be used simultaneously, disabling ARP "
4844                                "monitoring\n",
4845                                miimon, arp_interval);
4846                         arp_interval = 0;
4847                 }
4848
4849                 if ((updelay % miimon) != 0) {
4850                         printk(KERN_WARNING DRV_NAME
4851                                ": Warning: updelay (%d) is not a multiple "
4852                                "of miimon (%d), updelay rounded to %d ms\n",
4853                                updelay, miimon, (updelay / miimon) * miimon);
4854                 }
4855
4856                 updelay /= miimon;
4857
4858                 if ((downdelay % miimon) != 0) {
4859                         printk(KERN_WARNING DRV_NAME
4860                                ": Warning: downdelay (%d) is not a multiple "
4861                                "of miimon (%d), downdelay rounded to %d ms\n",
4862                                downdelay, miimon,
4863                                (downdelay / miimon) * miimon);
4864                 }
4865
4866                 downdelay /= miimon;
4867         }
4868
4869         if (arp_interval < 0) {
4870                 printk(KERN_WARNING DRV_NAME
4871                        ": Warning: arp_interval module parameter (%d) "
4872                        ", not in range 0-%d, so it was reset to %d\n",
4873                        arp_interval, INT_MAX, BOND_LINK_ARP_INTERV);
4874                 arp_interval = BOND_LINK_ARP_INTERV;
4875         }
4876
4877         for (arp_ip_count = 0;
4878              (arp_ip_count < BOND_MAX_ARP_TARGETS) && arp_ip_target[arp_ip_count];
4879              arp_ip_count++) {
4880                 /* not complete check, but should be good enough to
4881                    catch mistakes */
4882                 if (!isdigit(arp_ip_target[arp_ip_count][0])) {
4883                         printk(KERN_WARNING DRV_NAME
4884                                ": Warning: bad arp_ip_target module parameter "
4885                                "(%s), ARP monitoring will not be performed\n",
4886                                arp_ip_target[arp_ip_count]);
4887                         arp_interval = 0;
4888                 } else {
4889                         u32 ip = in_aton(arp_ip_target[arp_ip_count]);
4890                         arp_target[arp_ip_count] = ip;
4891                 }
4892         }
4893
4894         if (arp_interval && !arp_ip_count) {
4895                 /* don't allow arping if no arp_ip_target given... */
4896                 printk(KERN_WARNING DRV_NAME
4897                        ": Warning: arp_interval module parameter (%d) "
4898                        "specified without providing an arp_ip_target "
4899                        "parameter, arp_interval was reset to 0\n",
4900                        arp_interval);
4901                 arp_interval = 0;
4902         }
4903
4904         if (miimon) {
4905                 printk(KERN_INFO DRV_NAME
4906                        ": MII link monitoring set to %d ms\n",
4907                        miimon);
4908         } else if (arp_interval) {
4909                 int i;
4910
4911                 printk(KERN_INFO DRV_NAME
4912                        ": ARP monitoring set to %d ms with %d target(s):",
4913                        arp_interval, arp_ip_count);
4914
4915                 for (i = 0; i < arp_ip_count; i++)
4916                         printk (" %s", arp_ip_target[i]);
4917
4918                 printk("\n");
4919
4920         } else {
4921                 /* miimon and arp_interval not set, we need one so things
4922                  * work as expected, see bonding.txt for details
4923                  */
4924                 printk(KERN_WARNING DRV_NAME
4925                        ": Warning: either miimon or arp_interval and "
4926                        "arp_ip_target module parameters must be specified, "
4927                        "otherwise bonding will not detect link failures! see "
4928                        "bonding.txt for details.\n");
4929         }
4930
4931         if (primary && !USES_PRIMARY(bond_mode)) {
4932                 /* currently, using a primary only makes sense
4933                  * in active backup, TLB or ALB modes
4934                  */
4935                 printk(KERN_WARNING DRV_NAME
4936                        ": Warning: %s primary device specified but has no "
4937                        "effect in %s mode\n",
4938                        primary, bond_mode_name(bond_mode));
4939                 primary = NULL;
4940         }
4941
4942         /* fill params struct with the proper values */
4943         params->mode = bond_mode;
4944         params->xmit_policy = xmit_hashtype;
4945         params->miimon = miimon;
4946         params->arp_interval = arp_interval;
4947         params->updelay = updelay;
4948         params->downdelay = downdelay;
4949         params->use_carrier = use_carrier;
4950         params->lacp_fast = lacp_fast;
4951         params->primary[0] = 0;
4952
4953         if (primary) {
4954                 strncpy(params->primary, primary, IFNAMSIZ);
4955                 params->primary[IFNAMSIZ - 1] = 0;
4956         }
4957
4958         memcpy(params->arp_targets, arp_target, sizeof(arp_target));
4959
4960         return 0;
4961 }
4962
4963 /* Create a new bond based on the specified name and bonding parameters.
4964  * Caller must NOT hold rtnl_lock; we need to release it here before we
4965  * set up our sysfs entries.
4966  */
4967 int bond_create(char *name, struct bond_params *params, struct bonding **newbond)
4968 {
4969         struct net_device *bond_dev;
4970         int res;
4971
4972         rtnl_lock();
4973         bond_dev = alloc_netdev(sizeof(struct bonding), name, ether_setup);
4974         if (!bond_dev) {
4975                 printk(KERN_ERR DRV_NAME
4976                        ": %s: eek! can't alloc netdev!\n",
4977                        name);
4978                 res = -ENOMEM;
4979                 goto out_rtnl;
4980         }
4981
4982         /* bond_init() must be called after dev_alloc_name() (for the
4983          * /proc files), but before register_netdevice(), because we
4984          * need to set function pointers.
4985          */
4986
4987         res = bond_init(bond_dev, params);
4988         if (res < 0) {
4989                 goto out_netdev;
4990         }
4991
4992         SET_MODULE_OWNER(bond_dev);
4993
4994         res = register_netdevice(bond_dev);
4995         if (res < 0) {
4996                 goto out_bond;
4997         }
4998         if (newbond)
4999                 *newbond = bond_dev->priv;
5000
5001         rtnl_unlock(); /* allows sysfs registration of net device */
5002         res = bond_create_sysfs_entry(bond_dev->priv);
5003         goto done;
5004 out_bond:
5005         bond_deinit(bond_dev);
5006 out_netdev:
5007         free_netdev(bond_dev);
5008 out_rtnl:
5009         rtnl_unlock();
5010 done:
5011         return res;
5012 }
5013
5014 static int __init bonding_init(void)
5015 {
5016         int i;
5017         int res;
5018         char new_bond_name[8];  /* Enough room for 999 bonds at init. */
5019
5020         printk(KERN_INFO "%s", version);
5021
5022         res = bond_check_params(&bonding_defaults);
5023         if (res) {
5024                 goto out;
5025         }
5026
5027 #ifdef CONFIG_PROC_FS
5028         bond_create_proc_dir();
5029 #endif
5030         for (i = 0; i < max_bonds; i++) {
5031                 sprintf(new_bond_name, "bond%d",i);
5032                 res = bond_create(new_bond_name,&bonding_defaults, NULL);
5033                 if (res)
5034                         goto err;
5035         }
5036
5037         res = bond_create_sysfs();
5038         if (res)
5039                 goto err;
5040
5041         register_netdevice_notifier(&bond_netdev_notifier);
5042         register_inetaddr_notifier(&bond_inetaddr_notifier);
5043
5044         goto out;
5045 err:
5046         rtnl_lock();
5047         bond_free_all();
5048         bond_destroy_sysfs();
5049         rtnl_unlock();
5050 out:
5051         return res;
5052
5053 }
5054
5055 static void __exit bonding_exit(void)
5056 {
5057         unregister_netdevice_notifier(&bond_netdev_notifier);
5058         unregister_inetaddr_notifier(&bond_inetaddr_notifier);
5059
5060         rtnl_lock();
5061         bond_free_all();
5062         bond_destroy_sysfs();
5063         rtnl_unlock();
5064 }
5065
5066 module_init(bonding_init);
5067 module_exit(bonding_exit);
5068 MODULE_LICENSE("GPL");
5069 MODULE_VERSION(DRV_VERSION);
5070 MODULE_DESCRIPTION(DRV_DESCRIPTION ", v" DRV_VERSION);
5071 MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others");
5072 MODULE_SUPPORTED_DEVICE("most ethernet devices");
5073
5074 /*
5075  * Local variables:
5076  *  c-indent-level: 8
5077  *  c-basic-offset: 8
5078  *  tab-width: 8
5079  * End:
5080  */
5081