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