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