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