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