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