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