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