source: src/linux/xscale/linux-2.6.23/drivers/net/bonding/bond_main.c @ 10881

Last change on this file since 10881 was 10881, checked in by BrainSlayer, 5 years ago

new bonding driver

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