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