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