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