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