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