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