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