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