bonding: change bond_create_proc_entry() to return void
[pandora-kernel.git] / drivers / net / bonding / bond_main.c
1 /*
2  * originally based on the dummy device.
3  *
4  * Copyright 1999, Thomas Davis, tadavis@lbl.gov.
5  * Licensed under the GPL. Based on dummy.c, and eql.c devices.
6  *
7  * bonding.c: an Ethernet Bonding driver
8  *
9  * This is useful to talk to a Cisco EtherChannel compatible equipment:
10  *      Cisco 5500
11  *      Sun Trunking (Solaris)
12  *      Alteon AceDirector Trunks
13  *      Linux Bonding
14  *      and probably many L2 switches ...
15  *
16  * How it works:
17  *    ifconfig bond0 ipaddress netmask up
18  *      will setup a network device, with an ip address.  No mac address
19  *      will be assigned at this time.  The hw mac address will come from
20  *      the first slave bonded to the channel.  All slaves will then use
21  *      this hw mac address.
22  *
23  *    ifconfig bond0 down
24  *         will release all slaves, marking them as down.
25  *
26  *    ifenslave bond0 eth0
27  *      will attach eth0 to bond0 as a slave.  eth0 hw mac address will either
28  *      a: be used as initial mac address
29  *      b: if a hw mac address already is there, eth0's hw mac address
30  *         will then be set from bond0.
31  *
32  */
33
34 #include <linux/kernel.h>
35 #include <linux/module.h>
36 #include <linux/types.h>
37 #include <linux/fcntl.h>
38 #include <linux/interrupt.h>
39 #include <linux/ptrace.h>
40 #include <linux/ioport.h>
41 #include <linux/in.h>
42 #include <net/ip.h>
43 #include <linux/ip.h>
44 #include <linux/tcp.h>
45 #include <linux/udp.h>
46 #include <linux/slab.h>
47 #include <linux/string.h>
48 #include <linux/init.h>
49 #include <linux/timer.h>
50 #include <linux/socket.h>
51 #include <linux/ctype.h>
52 #include <linux/inet.h>
53 #include <linux/bitops.h>
54 #include <linux/io.h>
55 #include <asm/system.h>
56 #include <asm/dma.h>
57 #include <linux/uaccess.h>
58 #include <linux/errno.h>
59 #include <linux/netdevice.h>
60 #include <linux/inetdevice.h>
61 #include <linux/igmp.h>
62 #include <linux/etherdevice.h>
63 #include <linux/skbuff.h>
64 #include <net/sock.h>
65 #include <linux/rtnetlink.h>
66 #include <linux/proc_fs.h>
67 #include <linux/seq_file.h>
68 #include <linux/smp.h>
69 #include <linux/if_ether.h>
70 #include <net/arp.h>
71 #include <linux/mii.h>
72 #include <linux/ethtool.h>
73 #include <linux/if_vlan.h>
74 #include <linux/if_bonding.h>
75 #include <linux/jiffies.h>
76 #include <net/route.h>
77 #include <net/net_namespace.h>
78 #include "bonding.h"
79 #include "bond_3ad.h"
80 #include "bond_alb.h"
81
82 /*---------------------------- Module parameters ----------------------------*/
83
84 /* monitor all links that often (in milliseconds). <=0 disables monitoring */
85 #define BOND_LINK_MON_INTERV    0
86 #define BOND_LINK_ARP_INTERV    0
87
88 static int max_bonds    = BOND_DEFAULT_MAX_BONDS;
89 static int num_grat_arp = 1;
90 static int num_unsol_na = 1;
91 static int miimon       = BOND_LINK_MON_INTERV;
92 static int updelay;
93 static int downdelay;
94 static int use_carrier  = 1;
95 static char *mode;
96 static char *primary;
97 static char *primary_reselect;
98 static char *lacp_rate;
99 static char *ad_select;
100 static char *xmit_hash_policy;
101 static int arp_interval = BOND_LINK_ARP_INTERV;
102 static char *arp_ip_target[BOND_MAX_ARP_TARGETS];
103 static char *arp_validate;
104 static char *fail_over_mac;
105 static struct bond_params bonding_defaults;
106
107 module_param(max_bonds, int, 0);
108 MODULE_PARM_DESC(max_bonds, "Max number of bonded devices");
109 module_param(num_grat_arp, int, 0644);
110 MODULE_PARM_DESC(num_grat_arp, "Number of gratuitous ARP packets to send on failover event");
111 module_param(num_unsol_na, int, 0644);
112 MODULE_PARM_DESC(num_unsol_na, "Number of unsolicited IPv6 Neighbor Advertisements packets to send on failover event");
113 module_param(miimon, int, 0);
114 MODULE_PARM_DESC(miimon, "Link check interval in milliseconds");
115 module_param(updelay, int, 0);
116 MODULE_PARM_DESC(updelay, "Delay before considering link up, in milliseconds");
117 module_param(downdelay, int, 0);
118 MODULE_PARM_DESC(downdelay, "Delay before considering link down, "
119                             "in milliseconds");
120 module_param(use_carrier, int, 0);
121 MODULE_PARM_DESC(use_carrier, "Use netif_carrier_ok (vs MII ioctls) in miimon; "
122                               "0 for off, 1 for on (default)");
123 module_param(mode, charp, 0);
124 MODULE_PARM_DESC(mode, "Mode of operation : 0 for balance-rr, "
125                        "1 for active-backup, 2 for balance-xor, "
126                        "3 for broadcast, 4 for 802.3ad, 5 for balance-tlb, "
127                        "6 for balance-alb");
128 module_param(primary, charp, 0);
129 MODULE_PARM_DESC(primary, "Primary network device to use");
130 module_param(primary_reselect, charp, 0);
131 MODULE_PARM_DESC(primary_reselect, "Reselect primary slave "
132                                    "once it comes up; "
133                                    "0 for always (default), "
134                                    "1 for only if speed of primary is "
135                                    "better, "
136                                    "2 for only on active slave "
137                                    "failure");
138 module_param(lacp_rate, charp, 0);
139 MODULE_PARM_DESC(lacp_rate, "LACPDU tx rate to request from 802.3ad partner "
140                             "(slow/fast)");
141 module_param(ad_select, charp, 0);
142 MODULE_PARM_DESC(ad_select, "803.ad aggregation selection logic: stable (0, default), bandwidth (1), count (2)");
143 module_param(xmit_hash_policy, charp, 0);
144 MODULE_PARM_DESC(xmit_hash_policy, "XOR hashing method: 0 for layer 2 (default)"
145                                    ", 1 for layer 3+4");
146 module_param(arp_interval, int, 0);
147 MODULE_PARM_DESC(arp_interval, "arp interval in milliseconds");
148 module_param_array(arp_ip_target, charp, NULL, 0);
149 MODULE_PARM_DESC(arp_ip_target, "arp targets in n.n.n.n form");
150 module_param(arp_validate, charp, 0);
151 MODULE_PARM_DESC(arp_validate, "validate src/dst of ARP probes: none (default), active, backup or all");
152 module_param(fail_over_mac, charp, 0);
153 MODULE_PARM_DESC(fail_over_mac, "For active-backup, do not set all slaves to the same MAC.  none (default), active or follow");
154
155 /*----------------------------- Global variables ----------------------------*/
156
157 static const char * const version =
158         DRV_DESCRIPTION ": v" DRV_VERSION " (" DRV_RELDATE ")\n";
159
160 LIST_HEAD(bond_dev_list);
161
162 #ifdef CONFIG_PROC_FS
163 static struct proc_dir_entry *bond_proc_dir;
164 #endif
165
166 static __be32 arp_target[BOND_MAX_ARP_TARGETS];
167 static int arp_ip_count;
168 static int bond_mode    = BOND_MODE_ROUNDROBIN;
169 static int xmit_hashtype = BOND_XMIT_POLICY_LAYER2;
170 static int lacp_fast;
171
172
173 const struct bond_parm_tbl bond_lacp_tbl[] = {
174 {       "slow",         AD_LACP_SLOW},
175 {       "fast",         AD_LACP_FAST},
176 {       NULL,           -1},
177 };
178
179 const struct bond_parm_tbl bond_mode_tbl[] = {
180 {       "balance-rr",           BOND_MODE_ROUNDROBIN},
181 {       "active-backup",        BOND_MODE_ACTIVEBACKUP},
182 {       "balance-xor",          BOND_MODE_XOR},
183 {       "broadcast",            BOND_MODE_BROADCAST},
184 {       "802.3ad",              BOND_MODE_8023AD},
185 {       "balance-tlb",          BOND_MODE_TLB},
186 {       "balance-alb",          BOND_MODE_ALB},
187 {       NULL,                   -1},
188 };
189
190 const struct bond_parm_tbl xmit_hashtype_tbl[] = {
191 {       "layer2",               BOND_XMIT_POLICY_LAYER2},
192 {       "layer3+4",             BOND_XMIT_POLICY_LAYER34},
193 {       "layer2+3",             BOND_XMIT_POLICY_LAYER23},
194 {       NULL,                   -1},
195 };
196
197 const struct bond_parm_tbl arp_validate_tbl[] = {
198 {       "none",                 BOND_ARP_VALIDATE_NONE},
199 {       "active",               BOND_ARP_VALIDATE_ACTIVE},
200 {       "backup",               BOND_ARP_VALIDATE_BACKUP},
201 {       "all",                  BOND_ARP_VALIDATE_ALL},
202 {       NULL,                   -1},
203 };
204
205 const struct bond_parm_tbl fail_over_mac_tbl[] = {
206 {       "none",                 BOND_FOM_NONE},
207 {       "active",               BOND_FOM_ACTIVE},
208 {       "follow",               BOND_FOM_FOLLOW},
209 {       NULL,                   -1},
210 };
211
212 const struct bond_parm_tbl pri_reselect_tbl[] = {
213 {       "always",               BOND_PRI_RESELECT_ALWAYS},
214 {       "better",               BOND_PRI_RESELECT_BETTER},
215 {       "failure",              BOND_PRI_RESELECT_FAILURE},
216 {       NULL,                   -1},
217 };
218
219 struct bond_parm_tbl ad_select_tbl[] = {
220 {       "stable",       BOND_AD_STABLE},
221 {       "bandwidth",    BOND_AD_BANDWIDTH},
222 {       "count",        BOND_AD_COUNT},
223 {       NULL,           -1},
224 };
225
226 /*-------------------------- Forward declarations ---------------------------*/
227
228 static void bond_send_gratuitous_arp(struct bonding *bond);
229 static int bond_init(struct net_device *bond_dev);
230 static void bond_deinit(struct net_device *bond_dev);
231
232 /*---------------------------- General routines -----------------------------*/
233
234 static const char *bond_mode_name(int mode)
235 {
236         static const char *names[] = {
237                 [BOND_MODE_ROUNDROBIN] = "load balancing (round-robin)",
238                 [BOND_MODE_ACTIVEBACKUP] = "fault-tolerance (active-backup)",
239                 [BOND_MODE_XOR] = "load balancing (xor)",
240                 [BOND_MODE_BROADCAST] = "fault-tolerance (broadcast)",
241                 [BOND_MODE_8023AD] = "IEEE 802.3ad Dynamic link aggregation",
242                 [BOND_MODE_TLB] = "transmit load balancing",
243                 [BOND_MODE_ALB] = "adaptive load balancing",
244         };
245
246         if (mode < 0 || mode > BOND_MODE_ALB)
247                 return "unknown";
248
249         return names[mode];
250 }
251
252 /*---------------------------------- VLAN -----------------------------------*/
253
254 /**
255  * bond_add_vlan - add a new vlan id on bond
256  * @bond: bond that got the notification
257  * @vlan_id: the vlan id to add
258  *
259  * Returns -ENOMEM if allocation failed.
260  */
261 static int bond_add_vlan(struct bonding *bond, unsigned short vlan_id)
262 {
263         struct vlan_entry *vlan;
264
265         pr_debug("bond: %s, vlan id %d\n",
266                 (bond ? bond->dev->name : "None"), vlan_id);
267
268         vlan = kzalloc(sizeof(struct vlan_entry), GFP_KERNEL);
269         if (!vlan)
270                 return -ENOMEM;
271
272         INIT_LIST_HEAD(&vlan->vlan_list);
273         vlan->vlan_id = vlan_id;
274
275         write_lock_bh(&bond->lock);
276
277         list_add_tail(&vlan->vlan_list, &bond->vlan_list);
278
279         write_unlock_bh(&bond->lock);
280
281         pr_debug("added VLAN ID %d on bond %s\n", vlan_id, bond->dev->name);
282
283         return 0;
284 }
285
286 /**
287  * bond_del_vlan - delete a vlan id from bond
288  * @bond: bond that got the notification
289  * @vlan_id: the vlan id to delete
290  *
291  * returns -ENODEV if @vlan_id was not found in @bond.
292  */
293 static int bond_del_vlan(struct bonding *bond, unsigned short vlan_id)
294 {
295         struct vlan_entry *vlan;
296         int res = -ENODEV;
297
298         pr_debug("bond: %s, vlan id %d\n", bond->dev->name, vlan_id);
299
300         write_lock_bh(&bond->lock);
301
302         list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
303                 if (vlan->vlan_id == vlan_id) {
304                         list_del(&vlan->vlan_list);
305
306                         if (bond_is_lb(bond))
307                                 bond_alb_clear_vlan(bond, vlan_id);
308
309                         pr_debug("removed VLAN ID %d from bond %s\n", vlan_id,
310                                 bond->dev->name);
311
312                         kfree(vlan);
313
314                         if (list_empty(&bond->vlan_list) &&
315                             (bond->slave_cnt == 0)) {
316                                 /* Last VLAN removed and no slaves, so
317                                  * restore block on adding VLANs. This will
318                                  * be removed once new slaves that are not
319                                  * VLAN challenged will be added.
320                                  */
321                                 bond->dev->features |= NETIF_F_VLAN_CHALLENGED;
322                         }
323
324                         res = 0;
325                         goto out;
326                 }
327         }
328
329         pr_debug("couldn't find VLAN ID %d in bond %s\n", vlan_id,
330                 bond->dev->name);
331
332 out:
333         write_unlock_bh(&bond->lock);
334         return res;
335 }
336
337 /**
338  * bond_has_challenged_slaves
339  * @bond: the bond we're working on
340  *
341  * Searches the slave list. Returns 1 if a vlan challenged slave
342  * was found, 0 otherwise.
343  *
344  * Assumes bond->lock is held.
345  */
346 static int bond_has_challenged_slaves(struct bonding *bond)
347 {
348         struct slave *slave;
349         int i;
350
351         bond_for_each_slave(bond, slave, i) {
352                 if (slave->dev->features & NETIF_F_VLAN_CHALLENGED) {
353                         pr_debug("found VLAN challenged slave - %s\n",
354                                 slave->dev->name);
355                         return 1;
356                 }
357         }
358
359         pr_debug("no VLAN challenged slaves found\n");
360         return 0;
361 }
362
363 /**
364  * bond_next_vlan - safely skip to the next item in the vlans list.
365  * @bond: the bond we're working on
366  * @curr: item we're advancing from
367  *
368  * Returns %NULL if list is empty, bond->next_vlan if @curr is %NULL,
369  * or @curr->next otherwise (even if it is @curr itself again).
370  *
371  * Caller must hold bond->lock
372  */
373 struct vlan_entry *bond_next_vlan(struct bonding *bond, struct vlan_entry *curr)
374 {
375         struct vlan_entry *next, *last;
376
377         if (list_empty(&bond->vlan_list))
378                 return NULL;
379
380         if (!curr) {
381                 next = list_entry(bond->vlan_list.next,
382                                   struct vlan_entry, vlan_list);
383         } else {
384                 last = list_entry(bond->vlan_list.prev,
385                                   struct vlan_entry, vlan_list);
386                 if (last == curr) {
387                         next = list_entry(bond->vlan_list.next,
388                                           struct vlan_entry, vlan_list);
389                 } else {
390                         next = list_entry(curr->vlan_list.next,
391                                           struct vlan_entry, vlan_list);
392                 }
393         }
394
395         return next;
396 }
397
398 /**
399  * bond_dev_queue_xmit - Prepare skb for xmit.
400  *
401  * @bond: bond device that got this skb for tx.
402  * @skb: hw accel VLAN tagged skb to transmit
403  * @slave_dev: slave that is supposed to xmit this skbuff
404  *
405  * When the bond gets an skb to transmit that is
406  * already hardware accelerated VLAN tagged, and it
407  * needs to relay this skb to a slave that is not
408  * hw accel capable, the skb needs to be "unaccelerated",
409  * i.e. strip the hwaccel tag and re-insert it as part
410  * of the payload.
411  */
412 int bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb,
413                         struct net_device *slave_dev)
414 {
415         unsigned short uninitialized_var(vlan_id);
416
417         if (!list_empty(&bond->vlan_list) &&
418             !(slave_dev->features & NETIF_F_HW_VLAN_TX) &&
419             vlan_get_tag(skb, &vlan_id) == 0) {
420                 skb->dev = slave_dev;
421                 skb = vlan_put_tag(skb, vlan_id);
422                 if (!skb) {
423                         /* vlan_put_tag() frees the skb in case of error,
424                          * so return success here so the calling functions
425                          * won't attempt to free is again.
426                          */
427                         return 0;
428                 }
429         } else {
430                 skb->dev = slave_dev;
431         }
432
433         skb->priority = 1;
434         dev_queue_xmit(skb);
435
436         return 0;
437 }
438
439 /*
440  * In the following 3 functions, bond_vlan_rx_register(), bond_vlan_rx_add_vid
441  * and bond_vlan_rx_kill_vid, We don't protect the slave list iteration with a
442  * lock because:
443  * a. This operation is performed in IOCTL context,
444  * b. The operation is protected by the RTNL semaphore in the 8021q code,
445  * c. Holding a lock with BH disabled while directly calling a base driver
446  *    entry point is generally a BAD idea.
447  *
448  * The design of synchronization/protection for this operation in the 8021q
449  * module is good for one or more VLAN devices over a single physical device
450  * and cannot be extended for a teaming solution like bonding, so there is a
451  * potential race condition here where a net device from the vlan group might
452  * be referenced (either by a base driver or the 8021q code) while it is being
453  * removed from the system. However, it turns out we're not making matters
454  * worse, and if it works for regular VLAN usage it will work here too.
455 */
456
457 /**
458  * bond_vlan_rx_register - Propagates registration to slaves
459  * @bond_dev: bonding net device that got called
460  * @grp: vlan group being registered
461  */
462 static void bond_vlan_rx_register(struct net_device *bond_dev,
463                                   struct vlan_group *grp)
464 {
465         struct bonding *bond = netdev_priv(bond_dev);
466         struct slave *slave;
467         int i;
468
469         bond->vlgrp = grp;
470
471         bond_for_each_slave(bond, slave, i) {
472                 struct net_device *slave_dev = slave->dev;
473                 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
474
475                 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
476                     slave_ops->ndo_vlan_rx_register) {
477                         slave_ops->ndo_vlan_rx_register(slave_dev, grp);
478                 }
479         }
480 }
481
482 /**
483  * bond_vlan_rx_add_vid - Propagates adding an id to slaves
484  * @bond_dev: bonding net device that got called
485  * @vid: vlan id being added
486  */
487 static void bond_vlan_rx_add_vid(struct net_device *bond_dev, uint16_t vid)
488 {
489         struct bonding *bond = netdev_priv(bond_dev);
490         struct slave *slave;
491         int i, res;
492
493         bond_for_each_slave(bond, slave, i) {
494                 struct net_device *slave_dev = slave->dev;
495                 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
496
497                 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
498                     slave_ops->ndo_vlan_rx_add_vid) {
499                         slave_ops->ndo_vlan_rx_add_vid(slave_dev, vid);
500                 }
501         }
502
503         res = bond_add_vlan(bond, vid);
504         if (res) {
505                 pr_err(DRV_NAME
506                        ": %s: Error: Failed to add vlan id %d\n",
507                        bond_dev->name, vid);
508         }
509 }
510
511 /**
512  * bond_vlan_rx_kill_vid - Propagates deleting an id to slaves
513  * @bond_dev: bonding net device that got called
514  * @vid: vlan id being removed
515  */
516 static void bond_vlan_rx_kill_vid(struct net_device *bond_dev, uint16_t vid)
517 {
518         struct bonding *bond = netdev_priv(bond_dev);
519         struct slave *slave;
520         struct net_device *vlan_dev;
521         int i, res;
522
523         bond_for_each_slave(bond, slave, i) {
524                 struct net_device *slave_dev = slave->dev;
525                 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
526
527                 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
528                     slave_ops->ndo_vlan_rx_kill_vid) {
529                         /* Save and then restore vlan_dev in the grp array,
530                          * since the slave's driver might clear it.
531                          */
532                         vlan_dev = vlan_group_get_device(bond->vlgrp, vid);
533                         slave_ops->ndo_vlan_rx_kill_vid(slave_dev, vid);
534                         vlan_group_set_device(bond->vlgrp, vid, vlan_dev);
535                 }
536         }
537
538         res = bond_del_vlan(bond, vid);
539         if (res) {
540                 pr_err(DRV_NAME
541                        ": %s: Error: Failed to remove vlan id %d\n",
542                        bond_dev->name, vid);
543         }
544 }
545
546 static void bond_add_vlans_on_slave(struct bonding *bond, struct net_device *slave_dev)
547 {
548         struct vlan_entry *vlan;
549         const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
550
551         write_lock_bh(&bond->lock);
552
553         if (list_empty(&bond->vlan_list))
554                 goto out;
555
556         if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
557             slave_ops->ndo_vlan_rx_register)
558                 slave_ops->ndo_vlan_rx_register(slave_dev, bond->vlgrp);
559
560         if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
561             !(slave_ops->ndo_vlan_rx_add_vid))
562                 goto out;
563
564         list_for_each_entry(vlan, &bond->vlan_list, vlan_list)
565                 slave_ops->ndo_vlan_rx_add_vid(slave_dev, vlan->vlan_id);
566
567 out:
568         write_unlock_bh(&bond->lock);
569 }
570
571 static void bond_del_vlans_from_slave(struct bonding *bond,
572                                       struct net_device *slave_dev)
573 {
574         const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
575         struct vlan_entry *vlan;
576         struct net_device *vlan_dev;
577
578         write_lock_bh(&bond->lock);
579
580         if (list_empty(&bond->vlan_list))
581                 goto out;
582
583         if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
584             !(slave_ops->ndo_vlan_rx_kill_vid))
585                 goto unreg;
586
587         list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
588                 /* Save and then restore vlan_dev in the grp array,
589                  * since the slave's driver might clear it.
590                  */
591                 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
592                 slave_ops->ndo_vlan_rx_kill_vid(slave_dev, vlan->vlan_id);
593                 vlan_group_set_device(bond->vlgrp, vlan->vlan_id, vlan_dev);
594         }
595
596 unreg:
597         if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
598             slave_ops->ndo_vlan_rx_register)
599                 slave_ops->ndo_vlan_rx_register(slave_dev, NULL);
600
601 out:
602         write_unlock_bh(&bond->lock);
603 }
604
605 /*------------------------------- Link status -------------------------------*/
606
607 /*
608  * Set the carrier state for the master according to the state of its
609  * slaves.  If any slaves are up, the master is up.  In 802.3ad mode,
610  * do special 802.3ad magic.
611  *
612  * Returns zero if carrier state does not change, nonzero if it does.
613  */
614 static int bond_set_carrier(struct bonding *bond)
615 {
616         struct slave *slave;
617         int i;
618
619         if (bond->slave_cnt == 0)
620                 goto down;
621
622         if (bond->params.mode == BOND_MODE_8023AD)
623                 return bond_3ad_set_carrier(bond);
624
625         bond_for_each_slave(bond, slave, i) {
626                 if (slave->link == BOND_LINK_UP) {
627                         if (!netif_carrier_ok(bond->dev)) {
628                                 netif_carrier_on(bond->dev);
629                                 return 1;
630                         }
631                         return 0;
632                 }
633         }
634
635 down:
636         if (netif_carrier_ok(bond->dev)) {
637                 netif_carrier_off(bond->dev);
638                 return 1;
639         }
640         return 0;
641 }
642
643 /*
644  * Get link speed and duplex from the slave's base driver
645  * using ethtool. If for some reason the call fails or the
646  * values are invalid, fake speed and duplex to 100/Full
647  * and return error.
648  */
649 static int bond_update_speed_duplex(struct slave *slave)
650 {
651         struct net_device *slave_dev = slave->dev;
652         struct ethtool_cmd etool;
653         int res;
654
655         /* Fake speed and duplex */
656         slave->speed = SPEED_100;
657         slave->duplex = DUPLEX_FULL;
658
659         if (!slave_dev->ethtool_ops || !slave_dev->ethtool_ops->get_settings)
660                 return -1;
661
662         res = slave_dev->ethtool_ops->get_settings(slave_dev, &etool);
663         if (res < 0)
664                 return -1;
665
666         switch (etool.speed) {
667         case SPEED_10:
668         case SPEED_100:
669         case SPEED_1000:
670         case SPEED_10000:
671                 break;
672         default:
673                 return -1;
674         }
675
676         switch (etool.duplex) {
677         case DUPLEX_FULL:
678         case DUPLEX_HALF:
679                 break;
680         default:
681                 return -1;
682         }
683
684         slave->speed = etool.speed;
685         slave->duplex = etool.duplex;
686
687         return 0;
688 }
689
690 /*
691  * if <dev> supports MII link status reporting, check its link status.
692  *
693  * We either do MII/ETHTOOL ioctls, or check netif_carrier_ok(),
694  * depending upon the setting of the use_carrier parameter.
695  *
696  * Return either BMSR_LSTATUS, meaning that the link is up (or we
697  * can't tell and just pretend it is), or 0, meaning that the link is
698  * down.
699  *
700  * If reporting is non-zero, instead of faking link up, return -1 if
701  * both ETHTOOL and MII ioctls fail (meaning the device does not
702  * support them).  If use_carrier is set, return whatever it says.
703  * It'd be nice if there was a good way to tell if a driver supports
704  * netif_carrier, but there really isn't.
705  */
706 static int bond_check_dev_link(struct bonding *bond,
707                                struct net_device *slave_dev, int reporting)
708 {
709         const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
710         static int (*ioctl)(struct net_device *, struct ifreq *, int);
711         struct ifreq ifr;
712         struct mii_ioctl_data *mii;
713
714         if (!reporting && !netif_running(slave_dev))
715                 return 0;
716
717         if (bond->params.use_carrier)
718                 return netif_carrier_ok(slave_dev) ? BMSR_LSTATUS : 0;
719
720         /* Try to get link status using Ethtool first. */
721         if (slave_dev->ethtool_ops) {
722                 if (slave_dev->ethtool_ops->get_link) {
723                         u32 link;
724
725                         link = slave_dev->ethtool_ops->get_link(slave_dev);
726
727                         return link ? BMSR_LSTATUS : 0;
728                 }
729         }
730
731         /* Ethtool can't be used, fallback to MII ioctls. */
732         ioctl = slave_ops->ndo_do_ioctl;
733         if (ioctl) {
734                 /* TODO: set pointer to correct ioctl on a per team member */
735                 /*       bases to make this more efficient. that is, once  */
736                 /*       we determine the correct ioctl, we will always    */
737                 /*       call it and not the others for that team          */
738                 /*       member.                                           */
739
740                 /*
741                  * We cannot assume that SIOCGMIIPHY will also read a
742                  * register; not all network drivers (e.g., e100)
743                  * support that.
744                  */
745
746                 /* Yes, the mii is overlaid on the ifreq.ifr_ifru */
747                 strncpy(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
748                 mii = if_mii(&ifr);
749                 if (IOCTL(slave_dev, &ifr, SIOCGMIIPHY) == 0) {
750                         mii->reg_num = MII_BMSR;
751                         if (IOCTL(slave_dev, &ifr, SIOCGMIIREG) == 0)
752                                 return mii->val_out & BMSR_LSTATUS;
753                 }
754         }
755
756         /*
757          * If reporting, report that either there's no dev->do_ioctl,
758          * or both SIOCGMIIREG and get_link failed (meaning that we
759          * cannot report link status).  If not reporting, pretend
760          * we're ok.
761          */
762         return reporting ? -1 : BMSR_LSTATUS;
763 }
764
765 /*----------------------------- Multicast list ------------------------------*/
766
767 /*
768  * Returns 0 if dmi1 and dmi2 are the same, non-0 otherwise
769  */
770 static inline int bond_is_dmi_same(const struct dev_mc_list *dmi1,
771                                    const struct dev_mc_list *dmi2)
772 {
773         return memcmp(dmi1->dmi_addr, dmi2->dmi_addr, dmi1->dmi_addrlen) == 0 &&
774                         dmi1->dmi_addrlen == dmi2->dmi_addrlen;
775 }
776
777 /*
778  * returns dmi entry if found, NULL otherwise
779  */
780 static struct dev_mc_list *bond_mc_list_find_dmi(struct dev_mc_list *dmi,
781                                                  struct dev_mc_list *mc_list)
782 {
783         struct dev_mc_list *idmi;
784
785         for (idmi = mc_list; idmi; idmi = idmi->next) {
786                 if (bond_is_dmi_same(dmi, idmi))
787                         return idmi;
788         }
789
790         return NULL;
791 }
792
793 /*
794  * Push the promiscuity flag down to appropriate slaves
795  */
796 static int bond_set_promiscuity(struct bonding *bond, int inc)
797 {
798         int err = 0;
799         if (USES_PRIMARY(bond->params.mode)) {
800                 /* write lock already acquired */
801                 if (bond->curr_active_slave) {
802                         err = dev_set_promiscuity(bond->curr_active_slave->dev,
803                                                   inc);
804                 }
805         } else {
806                 struct slave *slave;
807                 int i;
808                 bond_for_each_slave(bond, slave, i) {
809                         err = dev_set_promiscuity(slave->dev, inc);
810                         if (err)
811                                 return err;
812                 }
813         }
814         return err;
815 }
816
817 /*
818  * Push the allmulti flag down to all slaves
819  */
820 static int bond_set_allmulti(struct bonding *bond, int inc)
821 {
822         int err = 0;
823         if (USES_PRIMARY(bond->params.mode)) {
824                 /* write lock already acquired */
825                 if (bond->curr_active_slave) {
826                         err = dev_set_allmulti(bond->curr_active_slave->dev,
827                                                inc);
828                 }
829         } else {
830                 struct slave *slave;
831                 int i;
832                 bond_for_each_slave(bond, slave, i) {
833                         err = dev_set_allmulti(slave->dev, inc);
834                         if (err)
835                                 return err;
836                 }
837         }
838         return err;
839 }
840
841 /*
842  * Add a Multicast address to slaves
843  * according to mode
844  */
845 static void bond_mc_add(struct bonding *bond, void *addr, int alen)
846 {
847         if (USES_PRIMARY(bond->params.mode)) {
848                 /* write lock already acquired */
849                 if (bond->curr_active_slave)
850                         dev_mc_add(bond->curr_active_slave->dev, addr, alen, 0);
851         } else {
852                 struct slave *slave;
853                 int i;
854
855                 bond_for_each_slave(bond, slave, i)
856                         dev_mc_add(slave->dev, addr, alen, 0);
857         }
858 }
859
860 /*
861  * Remove a multicast address from slave
862  * according to mode
863  */
864 static void bond_mc_delete(struct bonding *bond, void *addr, int alen)
865 {
866         if (USES_PRIMARY(bond->params.mode)) {
867                 /* write lock already acquired */
868                 if (bond->curr_active_slave)
869                         dev_mc_delete(bond->curr_active_slave->dev, addr,
870                                       alen, 0);
871         } else {
872                 struct slave *slave;
873                 int i;
874                 bond_for_each_slave(bond, slave, i) {
875                         dev_mc_delete(slave->dev, addr, alen, 0);
876                 }
877         }
878 }
879
880
881 /*
882  * Retrieve the list of registered multicast addresses for the bonding
883  * device and retransmit an IGMP JOIN request to the current active
884  * slave.
885  */
886 static void bond_resend_igmp_join_requests(struct bonding *bond)
887 {
888         struct in_device *in_dev;
889         struct ip_mc_list *im;
890
891         rcu_read_lock();
892         in_dev = __in_dev_get_rcu(bond->dev);
893         if (in_dev) {
894                 for (im = in_dev->mc_list; im; im = im->next)
895                         ip_mc_rejoin_group(im);
896         }
897
898         rcu_read_unlock();
899 }
900
901 /*
902  * Totally destroys the mc_list in bond
903  */
904 static void bond_mc_list_destroy(struct bonding *bond)
905 {
906         struct dev_mc_list *dmi;
907
908         dmi = bond->mc_list;
909         while (dmi) {
910                 bond->mc_list = dmi->next;
911                 kfree(dmi);
912                 dmi = bond->mc_list;
913         }
914
915         bond->mc_list = NULL;
916 }
917
918 /*
919  * Copy all the Multicast addresses from src to the bonding device dst
920  */
921 static int bond_mc_list_copy(struct dev_mc_list *mc_list, struct bonding *bond,
922                              gfp_t gfp_flag)
923 {
924         struct dev_mc_list *dmi, *new_dmi;
925
926         for (dmi = mc_list; dmi; dmi = dmi->next) {
927                 new_dmi = kmalloc(sizeof(struct dev_mc_list), gfp_flag);
928
929                 if (!new_dmi) {
930                         /* FIXME: Potential memory leak !!! */
931                         return -ENOMEM;
932                 }
933
934                 new_dmi->next = bond->mc_list;
935                 bond->mc_list = new_dmi;
936                 new_dmi->dmi_addrlen = dmi->dmi_addrlen;
937                 memcpy(new_dmi->dmi_addr, dmi->dmi_addr, dmi->dmi_addrlen);
938                 new_dmi->dmi_users = dmi->dmi_users;
939                 new_dmi->dmi_gusers = dmi->dmi_gusers;
940         }
941
942         return 0;
943 }
944
945 /*
946  * flush all members of flush->mc_list from device dev->mc_list
947  */
948 static void bond_mc_list_flush(struct net_device *bond_dev,
949                                struct net_device *slave_dev)
950 {
951         struct bonding *bond = netdev_priv(bond_dev);
952         struct dev_mc_list *dmi;
953
954         for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next)
955                 dev_mc_delete(slave_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
956
957         if (bond->params.mode == BOND_MODE_8023AD) {
958                 /* del lacpdu mc addr from mc list */
959                 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
960
961                 dev_mc_delete(slave_dev, lacpdu_multicast, ETH_ALEN, 0);
962         }
963 }
964
965 /*--------------------------- Active slave change ---------------------------*/
966
967 /*
968  * Update the mc list and multicast-related flags for the new and
969  * old active slaves (if any) according to the multicast mode, and
970  * promiscuous flags unconditionally.
971  */
972 static void bond_mc_swap(struct bonding *bond, struct slave *new_active,
973                          struct slave *old_active)
974 {
975         struct dev_mc_list *dmi;
976
977         if (!USES_PRIMARY(bond->params.mode))
978                 /* nothing to do -  mc list is already up-to-date on
979                  * all slaves
980                  */
981                 return;
982
983         if (old_active) {
984                 if (bond->dev->flags & IFF_PROMISC)
985                         dev_set_promiscuity(old_active->dev, -1);
986
987                 if (bond->dev->flags & IFF_ALLMULTI)
988                         dev_set_allmulti(old_active->dev, -1);
989
990                 for (dmi = bond->dev->mc_list; dmi; dmi = dmi->next)
991                         dev_mc_delete(old_active->dev, dmi->dmi_addr,
992                                       dmi->dmi_addrlen, 0);
993         }
994
995         if (new_active) {
996                 /* FIXME: Signal errors upstream. */
997                 if (bond->dev->flags & IFF_PROMISC)
998                         dev_set_promiscuity(new_active->dev, 1);
999
1000                 if (bond->dev->flags & IFF_ALLMULTI)
1001                         dev_set_allmulti(new_active->dev, 1);
1002
1003                 for (dmi = bond->dev->mc_list; dmi; dmi = dmi->next)
1004                         dev_mc_add(new_active->dev, dmi->dmi_addr,
1005                                    dmi->dmi_addrlen, 0);
1006                 bond_resend_igmp_join_requests(bond);
1007         }
1008 }
1009
1010 /*
1011  * bond_do_fail_over_mac
1012  *
1013  * Perform special MAC address swapping for fail_over_mac settings
1014  *
1015  * Called with RTNL, bond->lock for read, curr_slave_lock for write_bh.
1016  */
1017 static void bond_do_fail_over_mac(struct bonding *bond,
1018                                   struct slave *new_active,
1019                                   struct slave *old_active)
1020         __releases(&bond->curr_slave_lock)
1021         __releases(&bond->lock)
1022         __acquires(&bond->lock)
1023         __acquires(&bond->curr_slave_lock)
1024 {
1025         u8 tmp_mac[ETH_ALEN];
1026         struct sockaddr saddr;
1027         int rv;
1028
1029         switch (bond->params.fail_over_mac) {
1030         case BOND_FOM_ACTIVE:
1031                 if (new_active)
1032                         memcpy(bond->dev->dev_addr,  new_active->dev->dev_addr,
1033                                new_active->dev->addr_len);
1034                 break;
1035         case BOND_FOM_FOLLOW:
1036                 /*
1037                  * if new_active && old_active, swap them
1038                  * if just old_active, do nothing (going to no active slave)
1039                  * if just new_active, set new_active to bond's MAC
1040                  */
1041                 if (!new_active)
1042                         return;
1043
1044                 write_unlock_bh(&bond->curr_slave_lock);
1045                 read_unlock(&bond->lock);
1046
1047                 if (old_active) {
1048                         memcpy(tmp_mac, new_active->dev->dev_addr, ETH_ALEN);
1049                         memcpy(saddr.sa_data, old_active->dev->dev_addr,
1050                                ETH_ALEN);
1051                         saddr.sa_family = new_active->dev->type;
1052                 } else {
1053                         memcpy(saddr.sa_data, bond->dev->dev_addr, ETH_ALEN);
1054                         saddr.sa_family = bond->dev->type;
1055                 }
1056
1057                 rv = dev_set_mac_address(new_active->dev, &saddr);
1058                 if (rv) {
1059                         pr_err(DRV_NAME
1060                                ": %s: Error %d setting MAC of slave %s\n",
1061                                bond->dev->name, -rv, new_active->dev->name);
1062                         goto out;
1063                 }
1064
1065                 if (!old_active)
1066                         goto out;
1067
1068                 memcpy(saddr.sa_data, tmp_mac, ETH_ALEN);
1069                 saddr.sa_family = old_active->dev->type;
1070
1071                 rv = dev_set_mac_address(old_active->dev, &saddr);
1072                 if (rv)
1073                         pr_err(DRV_NAME
1074                                ": %s: Error %d setting MAC of slave %s\n",
1075                                bond->dev->name, -rv, new_active->dev->name);
1076 out:
1077                 read_lock(&bond->lock);
1078                 write_lock_bh(&bond->curr_slave_lock);
1079                 break;
1080         default:
1081                 pr_err(DRV_NAME
1082                        ": %s: bond_do_fail_over_mac impossible: bad policy %d\n",
1083                        bond->dev->name, bond->params.fail_over_mac);
1084                 break;
1085         }
1086
1087 }
1088
1089 static bool bond_should_change_active(struct bonding *bond)
1090 {
1091         struct slave *prim = bond->primary_slave;
1092         struct slave *curr = bond->curr_active_slave;
1093
1094         if (!prim || !curr || curr->link != BOND_LINK_UP)
1095                 return true;
1096         if (bond->force_primary) {
1097                 bond->force_primary = false;
1098                 return true;
1099         }
1100         if (bond->params.primary_reselect == BOND_PRI_RESELECT_BETTER &&
1101             (prim->speed < curr->speed ||
1102              (prim->speed == curr->speed && prim->duplex <= curr->duplex)))
1103                 return false;
1104         if (bond->params.primary_reselect == BOND_PRI_RESELECT_FAILURE)
1105                 return false;
1106         return true;
1107 }
1108
1109 /**
1110  * find_best_interface - select the best available slave to be the active one
1111  * @bond: our bonding struct
1112  *
1113  * Warning: Caller must hold curr_slave_lock for writing.
1114  */
1115 static struct slave *bond_find_best_slave(struct bonding *bond)
1116 {
1117         struct slave *new_active, *old_active;
1118         struct slave *bestslave = NULL;
1119         int mintime = bond->params.updelay;
1120         int i;
1121
1122         new_active = bond->curr_active_slave;
1123
1124         if (!new_active) { /* there were no active slaves left */
1125                 if (bond->slave_cnt > 0)   /* found one slave */
1126                         new_active = bond->first_slave;
1127                 else
1128                         return NULL; /* still no slave, return NULL */
1129         }
1130
1131         if ((bond->primary_slave) &&
1132             bond->primary_slave->link == BOND_LINK_UP &&
1133             bond_should_change_active(bond)) {
1134                 new_active = bond->primary_slave;
1135         }
1136
1137         /* remember where to stop iterating over the slaves */
1138         old_active = new_active;
1139
1140         bond_for_each_slave_from(bond, new_active, i, old_active) {
1141                 if (new_active->link == BOND_LINK_UP) {
1142                         return new_active;
1143                 } else if (new_active->link == BOND_LINK_BACK &&
1144                            IS_UP(new_active->dev)) {
1145                         /* link up, but waiting for stabilization */
1146                         if (new_active->delay < mintime) {
1147                                 mintime = new_active->delay;
1148                                 bestslave = new_active;
1149                         }
1150                 }
1151         }
1152
1153         return bestslave;
1154 }
1155
1156 /**
1157  * change_active_interface - change the active slave into the specified one
1158  * @bond: our bonding struct
1159  * @new: the new slave to make the active one
1160  *
1161  * Set the new slave to the bond's settings and unset them on the old
1162  * curr_active_slave.
1163  * Setting include flags, mc-list, promiscuity, allmulti, etc.
1164  *
1165  * If @new's link state is %BOND_LINK_BACK we'll set it to %BOND_LINK_UP,
1166  * because it is apparently the best available slave we have, even though its
1167  * updelay hasn't timed out yet.
1168  *
1169  * If new_active is not NULL, caller must hold bond->lock for read and
1170  * curr_slave_lock for write_bh.
1171  */
1172 void bond_change_active_slave(struct bonding *bond, struct slave *new_active)
1173 {
1174         struct slave *old_active = bond->curr_active_slave;
1175
1176         if (old_active == new_active)
1177                 return;
1178
1179         if (new_active) {
1180                 new_active->jiffies = jiffies;
1181
1182                 if (new_active->link == BOND_LINK_BACK) {
1183                         if (USES_PRIMARY(bond->params.mode)) {
1184                                 pr_info(DRV_NAME
1185                                        ": %s: making interface %s the new "
1186                                        "active one %d ms earlier.\n",
1187                                        bond->dev->name, new_active->dev->name,
1188                                        (bond->params.updelay - new_active->delay) * bond->params.miimon);
1189                         }
1190
1191                         new_active->delay = 0;
1192                         new_active->link = BOND_LINK_UP;
1193
1194                         if (bond->params.mode == BOND_MODE_8023AD)
1195                                 bond_3ad_handle_link_change(new_active, BOND_LINK_UP);
1196
1197                         if (bond_is_lb(bond))
1198                                 bond_alb_handle_link_change(bond, new_active, BOND_LINK_UP);
1199                 } else {
1200                         if (USES_PRIMARY(bond->params.mode)) {
1201                                 pr_info(DRV_NAME
1202                                        ": %s: making interface %s the new "
1203                                        "active one.\n",
1204                                        bond->dev->name, new_active->dev->name);
1205                         }
1206                 }
1207         }
1208
1209         if (USES_PRIMARY(bond->params.mode))
1210                 bond_mc_swap(bond, new_active, old_active);
1211
1212         if (bond_is_lb(bond)) {
1213                 bond_alb_handle_active_change(bond, new_active);
1214                 if (old_active)
1215                         bond_set_slave_inactive_flags(old_active);
1216                 if (new_active)
1217                         bond_set_slave_active_flags(new_active);
1218         } else {
1219                 bond->curr_active_slave = new_active;
1220         }
1221
1222         if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
1223                 if (old_active)
1224                         bond_set_slave_inactive_flags(old_active);
1225
1226                 if (new_active) {
1227                         bond_set_slave_active_flags(new_active);
1228
1229                         if (bond->params.fail_over_mac)
1230                                 bond_do_fail_over_mac(bond, new_active,
1231                                                       old_active);
1232
1233                         bond->send_grat_arp = bond->params.num_grat_arp;
1234                         bond_send_gratuitous_arp(bond);
1235
1236                         bond->send_unsol_na = bond->params.num_unsol_na;
1237                         bond_send_unsolicited_na(bond);
1238
1239                         write_unlock_bh(&bond->curr_slave_lock);
1240                         read_unlock(&bond->lock);
1241
1242                         netdev_bonding_change(bond->dev, NETDEV_BONDING_FAILOVER);
1243
1244                         read_lock(&bond->lock);
1245                         write_lock_bh(&bond->curr_slave_lock);
1246                 }
1247         }
1248 }
1249
1250 /**
1251  * bond_select_active_slave - select a new active slave, if needed
1252  * @bond: our bonding struct
1253  *
1254  * This functions should be called when one of the following occurs:
1255  * - The old curr_active_slave has been released or lost its link.
1256  * - The primary_slave has got its link back.
1257  * - A slave has got its link back and there's no old curr_active_slave.
1258  *
1259  * Caller must hold bond->lock for read and curr_slave_lock for write_bh.
1260  */
1261 void bond_select_active_slave(struct bonding *bond)
1262 {
1263         struct slave *best_slave;
1264         int rv;
1265
1266         best_slave = bond_find_best_slave(bond);
1267         if (best_slave != bond->curr_active_slave) {
1268                 bond_change_active_slave(bond, best_slave);
1269                 rv = bond_set_carrier(bond);
1270                 if (!rv)
1271                         return;
1272
1273                 if (netif_carrier_ok(bond->dev)) {
1274                         pr_info(DRV_NAME
1275                                ": %s: first active interface up!\n",
1276                                bond->dev->name);
1277                 } else {
1278                         pr_info(DRV_NAME ": %s: "
1279                                "now running without any active interface !\n",
1280                                bond->dev->name);
1281                 }
1282         }
1283 }
1284
1285 /*--------------------------- slave list handling ---------------------------*/
1286
1287 /*
1288  * This function attaches the slave to the end of list.
1289  *
1290  * bond->lock held for writing by caller.
1291  */
1292 static void bond_attach_slave(struct bonding *bond, struct slave *new_slave)
1293 {
1294         if (bond->first_slave == NULL) { /* attaching the first slave */
1295                 new_slave->next = new_slave;
1296                 new_slave->prev = new_slave;
1297                 bond->first_slave = new_slave;
1298         } else {
1299                 new_slave->next = bond->first_slave;
1300                 new_slave->prev = bond->first_slave->prev;
1301                 new_slave->next->prev = new_slave;
1302                 new_slave->prev->next = new_slave;
1303         }
1304
1305         bond->slave_cnt++;
1306 }
1307
1308 /*
1309  * This function detaches the slave from the list.
1310  * WARNING: no check is made to verify if the slave effectively
1311  * belongs to <bond>.
1312  * Nothing is freed on return, structures are just unchained.
1313  * If any slave pointer in bond was pointing to <slave>,
1314  * it should be changed by the calling function.
1315  *
1316  * bond->lock held for writing by caller.
1317  */
1318 static void bond_detach_slave(struct bonding *bond, struct slave *slave)
1319 {
1320         if (slave->next)
1321                 slave->next->prev = slave->prev;
1322
1323         if (slave->prev)
1324                 slave->prev->next = slave->next;
1325
1326         if (bond->first_slave == slave) { /* slave is the first slave */
1327                 if (bond->slave_cnt > 1) { /* there are more slave */
1328                         bond->first_slave = slave->next;
1329                 } else {
1330                         bond->first_slave = NULL; /* slave was the last one */
1331                 }
1332         }
1333
1334         slave->next = NULL;
1335         slave->prev = NULL;
1336         bond->slave_cnt--;
1337 }
1338
1339 /*---------------------------------- IOCTL ----------------------------------*/
1340
1341 static int bond_sethwaddr(struct net_device *bond_dev,
1342                           struct net_device *slave_dev)
1343 {
1344         pr_debug("bond_dev=%p\n", bond_dev);
1345         pr_debug("slave_dev=%p\n", slave_dev);
1346         pr_debug("slave_dev->addr_len=%d\n", slave_dev->addr_len);
1347         memcpy(bond_dev->dev_addr, slave_dev->dev_addr, slave_dev->addr_len);
1348         return 0;
1349 }
1350
1351 #define BOND_VLAN_FEATURES \
1352         (NETIF_F_VLAN_CHALLENGED | NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_TX | \
1353          NETIF_F_HW_VLAN_FILTER)
1354
1355 /*
1356  * Compute the common dev->feature set available to all slaves.  Some
1357  * feature bits are managed elsewhere, so preserve those feature bits
1358  * on the master device.
1359  */
1360 static int bond_compute_features(struct bonding *bond)
1361 {
1362         struct slave *slave;
1363         struct net_device *bond_dev = bond->dev;
1364         unsigned long features = bond_dev->features;
1365         unsigned long vlan_features = 0;
1366         unsigned short max_hard_header_len = max((u16)ETH_HLEN,
1367                                                 bond_dev->hard_header_len);
1368         int i;
1369
1370         features &= ~(NETIF_F_ALL_CSUM | BOND_VLAN_FEATURES);
1371         features |=  NETIF_F_GSO_MASK | NETIF_F_NO_CSUM;
1372
1373         if (!bond->first_slave)
1374                 goto done;
1375
1376         features &= ~NETIF_F_ONE_FOR_ALL;
1377
1378         vlan_features = bond->first_slave->dev->vlan_features;
1379         bond_for_each_slave(bond, slave, i) {
1380                 features = netdev_increment_features(features,
1381                                                      slave->dev->features,
1382                                                      NETIF_F_ONE_FOR_ALL);
1383                 vlan_features = netdev_increment_features(vlan_features,
1384                                                         slave->dev->vlan_features,
1385                                                         NETIF_F_ONE_FOR_ALL);
1386                 if (slave->dev->hard_header_len > max_hard_header_len)
1387                         max_hard_header_len = slave->dev->hard_header_len;
1388         }
1389
1390 done:
1391         features |= (bond_dev->features & BOND_VLAN_FEATURES);
1392         bond_dev->features = netdev_fix_features(features, NULL);
1393         bond_dev->vlan_features = netdev_fix_features(vlan_features, NULL);
1394         bond_dev->hard_header_len = max_hard_header_len;
1395
1396         return 0;
1397 }
1398
1399 static void bond_setup_by_slave(struct net_device *bond_dev,
1400                                 struct net_device *slave_dev)
1401 {
1402         struct bonding *bond = netdev_priv(bond_dev);
1403
1404         bond_dev->header_ops        = slave_dev->header_ops;
1405
1406         bond_dev->type              = slave_dev->type;
1407         bond_dev->hard_header_len   = slave_dev->hard_header_len;
1408         bond_dev->addr_len          = slave_dev->addr_len;
1409
1410         memcpy(bond_dev->broadcast, slave_dev->broadcast,
1411                 slave_dev->addr_len);
1412         bond->setup_by_slave = 1;
1413 }
1414
1415 /* enslave device <slave> to bond device <master> */
1416 int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
1417 {
1418         struct bonding *bond = netdev_priv(bond_dev);
1419         const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
1420         struct slave *new_slave = NULL;
1421         struct dev_mc_list *dmi;
1422         struct sockaddr addr;
1423         int link_reporting;
1424         int old_features = bond_dev->features;
1425         int res = 0;
1426
1427         if (!bond->params.use_carrier && slave_dev->ethtool_ops == NULL &&
1428                 slave_ops->ndo_do_ioctl == NULL) {
1429                 pr_warning(DRV_NAME
1430                        ": %s: Warning: no link monitoring support for %s\n",
1431                        bond_dev->name, slave_dev->name);
1432         }
1433
1434         /* bond must be initialized by bond_open() before enslaving */
1435         if (!(bond_dev->flags & IFF_UP)) {
1436                 pr_warning(DRV_NAME
1437                         " %s: master_dev is not up in bond_enslave\n",
1438                         bond_dev->name);
1439         }
1440
1441         /* already enslaved */
1442         if (slave_dev->flags & IFF_SLAVE) {
1443                 pr_debug("Error, Device was already enslaved\n");
1444                 return -EBUSY;
1445         }
1446
1447         /* vlan challenged mutual exclusion */
1448         /* no need to lock since we're protected by rtnl_lock */
1449         if (slave_dev->features & NETIF_F_VLAN_CHALLENGED) {
1450                 pr_debug("%s: NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
1451                 if (!list_empty(&bond->vlan_list)) {
1452                         pr_err(DRV_NAME
1453                                ": %s: Error: cannot enslave VLAN "
1454                                "challenged slave %s on VLAN enabled "
1455                                "bond %s\n", bond_dev->name, slave_dev->name,
1456                                bond_dev->name);
1457                         return -EPERM;
1458                 } else {
1459                         pr_warning(DRV_NAME
1460                                ": %s: Warning: enslaved VLAN challenged "
1461                                "slave %s. Adding VLANs will be blocked as "
1462                                "long as %s is part of bond %s\n",
1463                                bond_dev->name, slave_dev->name, slave_dev->name,
1464                                bond_dev->name);
1465                         bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1466                 }
1467         } else {
1468                 pr_debug("%s: ! NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
1469                 if (bond->slave_cnt == 0) {
1470                         /* First slave, and it is not VLAN challenged,
1471                          * so remove the block of adding VLANs over the bond.
1472                          */
1473                         bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
1474                 }
1475         }
1476
1477         /*
1478          * Old ifenslave binaries are no longer supported.  These can
1479          * be identified with moderate accuracy by the state of the slave:
1480          * the current ifenslave will set the interface down prior to
1481          * enslaving it; the old ifenslave will not.
1482          */
1483         if ((slave_dev->flags & IFF_UP)) {
1484                 pr_err(DRV_NAME ": %s is up. "
1485                        "This may be due to an out of date ifenslave.\n",
1486                        slave_dev->name);
1487                 res = -EPERM;
1488                 goto err_undo_flags;
1489         }
1490
1491         /* set bonding device ether type by slave - bonding netdevices are
1492          * created with ether_setup, so when the slave type is not ARPHRD_ETHER
1493          * there is a need to override some of the type dependent attribs/funcs.
1494          *
1495          * bond ether type mutual exclusion - don't allow slaves of dissimilar
1496          * ether type (eg ARPHRD_ETHER and ARPHRD_INFINIBAND) share the same bond
1497          */
1498         if (bond->slave_cnt == 0) {
1499                 if (bond_dev->type != slave_dev->type) {
1500                         pr_debug("%s: change device type from %d to %d\n",
1501                                 bond_dev->name, bond_dev->type, slave_dev->type);
1502
1503                         netdev_bonding_change(bond_dev, NETDEV_BONDING_OLDTYPE);
1504
1505                         if (slave_dev->type != ARPHRD_ETHER)
1506                                 bond_setup_by_slave(bond_dev, slave_dev);
1507                         else
1508                                 ether_setup(bond_dev);
1509
1510                         netdev_bonding_change(bond_dev, NETDEV_BONDING_NEWTYPE);
1511                 }
1512         } else if (bond_dev->type != slave_dev->type) {
1513                 pr_err(DRV_NAME ": %s ether type (%d) is different "
1514                         "from other slaves (%d), can not enslave it.\n",
1515                         slave_dev->name,
1516                         slave_dev->type, bond_dev->type);
1517                         res = -EINVAL;
1518                         goto err_undo_flags;
1519         }
1520
1521         if (slave_ops->ndo_set_mac_address == NULL) {
1522                 if (bond->slave_cnt == 0) {
1523                         pr_warning(DRV_NAME
1524                                ": %s: Warning: The first slave device "
1525                                "specified does not support setting the MAC "
1526                                "address. Setting fail_over_mac to active.",
1527                                bond_dev->name);
1528                         bond->params.fail_over_mac = BOND_FOM_ACTIVE;
1529                 } else if (bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
1530                         pr_err(DRV_NAME
1531                                 ": %s: Error: The slave device specified "
1532                                 "does not support setting the MAC address, "
1533                                 "but fail_over_mac is not set to active.\n"
1534                                 , bond_dev->name);
1535                         res = -EOPNOTSUPP;
1536                         goto err_undo_flags;
1537                 }
1538         }
1539
1540         new_slave = kzalloc(sizeof(struct slave), GFP_KERNEL);
1541         if (!new_slave) {
1542                 res = -ENOMEM;
1543                 goto err_undo_flags;
1544         }
1545
1546         /* save slave's original flags before calling
1547          * netdev_set_master and dev_open
1548          */
1549         new_slave->original_flags = slave_dev->flags;
1550
1551         /*
1552          * Save slave's original ("permanent") mac address for modes
1553          * that need it, and for restoring it upon release, and then
1554          * set it to the master's address
1555          */
1556         memcpy(new_slave->perm_hwaddr, slave_dev->dev_addr, ETH_ALEN);
1557
1558         if (!bond->params.fail_over_mac) {
1559                 /*
1560                  * Set slave to master's mac address.  The application already
1561                  * set the master's mac address to that of the first slave
1562                  */
1563                 memcpy(addr.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
1564                 addr.sa_family = slave_dev->type;
1565                 res = dev_set_mac_address(slave_dev, &addr);
1566                 if (res) {
1567                         pr_debug("Error %d calling set_mac_address\n", res);
1568                         goto err_free;
1569                 }
1570         }
1571
1572         res = netdev_set_master(slave_dev, bond_dev);
1573         if (res) {
1574                 pr_debug("Error %d calling netdev_set_master\n", res);
1575                 goto err_restore_mac;
1576         }
1577         /* open the slave since the application closed it */
1578         res = dev_open(slave_dev);
1579         if (res) {
1580                 pr_debug("Opening slave %s failed\n", slave_dev->name);
1581                 goto err_unset_master;
1582         }
1583
1584         new_slave->dev = slave_dev;
1585         slave_dev->priv_flags |= IFF_BONDING;
1586
1587         if (bond_is_lb(bond)) {
1588                 /* bond_alb_init_slave() must be called before all other stages since
1589                  * it might fail and we do not want to have to undo everything
1590                  */
1591                 res = bond_alb_init_slave(bond, new_slave);
1592                 if (res)
1593                         goto err_close;
1594         }
1595
1596         /* If the mode USES_PRIMARY, then the new slave gets the
1597          * master's promisc (and mc) settings only if it becomes the
1598          * curr_active_slave, and that is taken care of later when calling
1599          * bond_change_active()
1600          */
1601         if (!USES_PRIMARY(bond->params.mode)) {
1602                 /* set promiscuity level to new slave */
1603                 if (bond_dev->flags & IFF_PROMISC) {
1604                         res = dev_set_promiscuity(slave_dev, 1);
1605                         if (res)
1606                                 goto err_close;
1607                 }
1608
1609                 /* set allmulti level to new slave */
1610                 if (bond_dev->flags & IFF_ALLMULTI) {
1611                         res = dev_set_allmulti(slave_dev, 1);
1612                         if (res)
1613                                 goto err_close;
1614                 }
1615
1616                 netif_addr_lock_bh(bond_dev);
1617                 /* upload master's mc_list to new slave */
1618                 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next)
1619                         dev_mc_add(slave_dev, dmi->dmi_addr,
1620                                    dmi->dmi_addrlen, 0);
1621                 netif_addr_unlock_bh(bond_dev);
1622         }
1623
1624         if (bond->params.mode == BOND_MODE_8023AD) {
1625                 /* add lacpdu mc addr to mc list */
1626                 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
1627
1628                 dev_mc_add(slave_dev, lacpdu_multicast, ETH_ALEN, 0);
1629         }
1630
1631         bond_add_vlans_on_slave(bond, slave_dev);
1632
1633         write_lock_bh(&bond->lock);
1634
1635         bond_attach_slave(bond, new_slave);
1636
1637         new_slave->delay = 0;
1638         new_slave->link_failure_count = 0;
1639
1640         bond_compute_features(bond);
1641
1642         write_unlock_bh(&bond->lock);
1643
1644         read_lock(&bond->lock);
1645
1646         new_slave->last_arp_rx = jiffies;
1647
1648         if (bond->params.miimon && !bond->params.use_carrier) {
1649                 link_reporting = bond_check_dev_link(bond, slave_dev, 1);
1650
1651                 if ((link_reporting == -1) && !bond->params.arp_interval) {
1652                         /*
1653                          * miimon is set but a bonded network driver
1654                          * does not support ETHTOOL/MII and
1655                          * arp_interval is not set.  Note: if
1656                          * use_carrier is enabled, we will never go
1657                          * here (because netif_carrier is always
1658                          * supported); thus, we don't need to change
1659                          * the messages for netif_carrier.
1660                          */
1661                         pr_warning(DRV_NAME
1662                                ": %s: Warning: MII and ETHTOOL support not "
1663                                "available for interface %s, and "
1664                                "arp_interval/arp_ip_target module parameters "
1665                                "not specified, thus bonding will not detect "
1666                                "link failures! see bonding.txt for details.\n",
1667                                bond_dev->name, slave_dev->name);
1668                 } else if (link_reporting == -1) {
1669                         /* unable get link status using mii/ethtool */
1670                         pr_warning(DRV_NAME
1671                                ": %s: Warning: can't get link status from "
1672                                "interface %s; the network driver associated "
1673                                "with this interface does not support MII or "
1674                                "ETHTOOL link status reporting, thus miimon "
1675                                "has no effect on this interface.\n",
1676                                bond_dev->name, slave_dev->name);
1677                 }
1678         }
1679
1680         /* check for initial state */
1681         if (!bond->params.miimon ||
1682             (bond_check_dev_link(bond, slave_dev, 0) == BMSR_LSTATUS)) {
1683                 if (bond->params.updelay) {
1684                         pr_debug("Initial state of slave_dev is "
1685                                 "BOND_LINK_BACK\n");
1686                         new_slave->link  = BOND_LINK_BACK;
1687                         new_slave->delay = bond->params.updelay;
1688                 } else {
1689                         pr_debug("Initial state of slave_dev is "
1690                                 "BOND_LINK_UP\n");
1691                         new_slave->link  = BOND_LINK_UP;
1692                 }
1693                 new_slave->jiffies = jiffies;
1694         } else {
1695                 pr_debug("Initial state of slave_dev is "
1696                         "BOND_LINK_DOWN\n");
1697                 new_slave->link  = BOND_LINK_DOWN;
1698         }
1699
1700         if (bond_update_speed_duplex(new_slave) &&
1701             (new_slave->link != BOND_LINK_DOWN)) {
1702                 pr_warning(DRV_NAME
1703                        ": %s: Warning: failed to get speed and duplex from %s, "
1704                        "assumed to be 100Mb/sec and Full.\n",
1705                        bond_dev->name, new_slave->dev->name);
1706
1707                 if (bond->params.mode == BOND_MODE_8023AD) {
1708                         pr_warning(DRV_NAME
1709                                ": %s: Warning: Operation of 802.3ad mode requires ETHTOOL "
1710                                "support in base driver for proper aggregator "
1711                                "selection.\n", bond_dev->name);
1712                 }
1713         }
1714
1715         if (USES_PRIMARY(bond->params.mode) && bond->params.primary[0]) {
1716                 /* if there is a primary slave, remember it */
1717                 if (strcmp(bond->params.primary, new_slave->dev->name) == 0) {
1718                         bond->primary_slave = new_slave;
1719                         bond->force_primary = true;
1720                 }
1721         }
1722
1723         write_lock_bh(&bond->curr_slave_lock);
1724
1725         switch (bond->params.mode) {
1726         case BOND_MODE_ACTIVEBACKUP:
1727                 bond_set_slave_inactive_flags(new_slave);
1728                 bond_select_active_slave(bond);
1729                 break;
1730         case BOND_MODE_8023AD:
1731                 /* in 802.3ad mode, the internal mechanism
1732                  * will activate the slaves in the selected
1733                  * aggregator
1734                  */
1735                 bond_set_slave_inactive_flags(new_slave);
1736                 /* if this is the first slave */
1737                 if (bond->slave_cnt == 1) {
1738                         SLAVE_AD_INFO(new_slave).id = 1;
1739                         /* Initialize AD with the number of times that the AD timer is called in 1 second
1740                          * can be called only after the mac address of the bond is set
1741                          */
1742                         bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL,
1743                                             bond->params.lacp_fast);
1744                 } else {
1745                         SLAVE_AD_INFO(new_slave).id =
1746                                 SLAVE_AD_INFO(new_slave->prev).id + 1;
1747                 }
1748
1749                 bond_3ad_bind_slave(new_slave);
1750                 break;
1751         case BOND_MODE_TLB:
1752         case BOND_MODE_ALB:
1753                 new_slave->state = BOND_STATE_ACTIVE;
1754                 bond_set_slave_inactive_flags(new_slave);
1755                 bond_select_active_slave(bond);
1756                 break;
1757         default:
1758                 pr_debug("This slave is always active in trunk mode\n");
1759
1760                 /* always active in trunk mode */
1761                 new_slave->state = BOND_STATE_ACTIVE;
1762
1763                 /* In trunking mode there is little meaning to curr_active_slave
1764                  * anyway (it holds no special properties of the bond device),
1765                  * so we can change it without calling change_active_interface()
1766                  */
1767                 if (!bond->curr_active_slave)
1768                         bond->curr_active_slave = new_slave;
1769
1770                 break;
1771         } /* switch(bond_mode) */
1772
1773         write_unlock_bh(&bond->curr_slave_lock);
1774
1775         bond_set_carrier(bond);
1776
1777         read_unlock(&bond->lock);
1778
1779         res = bond_create_slave_symlinks(bond_dev, slave_dev);
1780         if (res)
1781                 goto err_close;
1782
1783         pr_info(DRV_NAME
1784                ": %s: enslaving %s as a%s interface with a%s link.\n",
1785                bond_dev->name, slave_dev->name,
1786                new_slave->state == BOND_STATE_ACTIVE ? "n active" : " backup",
1787                new_slave->link != BOND_LINK_DOWN ? "n up" : " down");
1788
1789         /* enslave is successful */
1790         return 0;
1791
1792 /* Undo stages on error */
1793 err_close:
1794         dev_close(slave_dev);
1795
1796 err_unset_master:
1797         netdev_set_master(slave_dev, NULL);
1798
1799 err_restore_mac:
1800         if (!bond->params.fail_over_mac) {
1801                 /* XXX TODO - fom follow mode needs to change master's
1802                  * MAC if this slave's MAC is in use by the bond, or at
1803                  * least print a warning.
1804                  */
1805                 memcpy(addr.sa_data, new_slave->perm_hwaddr, ETH_ALEN);
1806                 addr.sa_family = slave_dev->type;
1807                 dev_set_mac_address(slave_dev, &addr);
1808         }
1809
1810 err_free:
1811         kfree(new_slave);
1812
1813 err_undo_flags:
1814         bond_dev->features = old_features;
1815
1816         return res;
1817 }
1818
1819 /*
1820  * Try to release the slave device <slave> from the bond device <master>
1821  * It is legal to access curr_active_slave without a lock because all the function
1822  * is write-locked.
1823  *
1824  * The rules for slave state should be:
1825  *   for Active/Backup:
1826  *     Active stays on all backups go down
1827  *   for Bonded connections:
1828  *     The first up interface should be left on and all others downed.
1829  */
1830 int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
1831 {
1832         struct bonding *bond = netdev_priv(bond_dev);
1833         struct slave *slave, *oldcurrent;
1834         struct sockaddr addr;
1835
1836         /* slave is not a slave or master is not master of this slave */
1837         if (!(slave_dev->flags & IFF_SLAVE) ||
1838             (slave_dev->master != bond_dev)) {
1839                 pr_err(DRV_NAME
1840                        ": %s: Error: cannot release %s.\n",
1841                        bond_dev->name, slave_dev->name);
1842                 return -EINVAL;
1843         }
1844
1845         write_lock_bh(&bond->lock);
1846
1847         slave = bond_get_slave_by_dev(bond, slave_dev);
1848         if (!slave) {
1849                 /* not a slave of this bond */
1850                 pr_info(DRV_NAME
1851                        ": %s: %s not enslaved\n",
1852                        bond_dev->name, slave_dev->name);
1853                 write_unlock_bh(&bond->lock);
1854                 return -EINVAL;
1855         }
1856
1857         if (!bond->params.fail_over_mac) {
1858                 if (!compare_ether_addr(bond_dev->dev_addr, slave->perm_hwaddr)
1859                     && bond->slave_cnt > 1)
1860                         pr_warning(DRV_NAME
1861                                ": %s: Warning: the permanent HWaddr of %s - "
1862                                "%pM - is still in use by %s. "
1863                                "Set the HWaddr of %s to a different address "
1864                                "to avoid conflicts.\n",
1865                                bond_dev->name, slave_dev->name,
1866                                slave->perm_hwaddr,
1867                                bond_dev->name, slave_dev->name);
1868         }
1869
1870         /* Inform AD package of unbinding of slave. */
1871         if (bond->params.mode == BOND_MODE_8023AD) {
1872                 /* must be called before the slave is
1873                  * detached from the list
1874                  */
1875                 bond_3ad_unbind_slave(slave);
1876         }
1877
1878         pr_info(DRV_NAME
1879                ": %s: releasing %s interface %s\n",
1880                bond_dev->name,
1881                (slave->state == BOND_STATE_ACTIVE)
1882                ? "active" : "backup",
1883                slave_dev->name);
1884
1885         oldcurrent = bond->curr_active_slave;
1886
1887         bond->current_arp_slave = NULL;
1888
1889         /* release the slave from its bond */
1890         bond_detach_slave(bond, slave);
1891
1892         bond_compute_features(bond);
1893
1894         if (bond->primary_slave == slave)
1895                 bond->primary_slave = NULL;
1896
1897         if (oldcurrent == slave)
1898                 bond_change_active_slave(bond, NULL);
1899
1900         if (bond_is_lb(bond)) {
1901                 /* Must be called only after the slave has been
1902                  * detached from the list and the curr_active_slave
1903                  * has been cleared (if our_slave == old_current),
1904                  * but before a new active slave is selected.
1905                  */
1906                 write_unlock_bh(&bond->lock);
1907                 bond_alb_deinit_slave(bond, slave);
1908                 write_lock_bh(&bond->lock);
1909         }
1910
1911         if (oldcurrent == slave) {
1912                 /*
1913                  * Note that we hold RTNL over this sequence, so there
1914                  * is no concern that another slave add/remove event
1915                  * will interfere.
1916                  */
1917                 write_unlock_bh(&bond->lock);
1918                 read_lock(&bond->lock);
1919                 write_lock_bh(&bond->curr_slave_lock);
1920
1921                 bond_select_active_slave(bond);
1922
1923                 write_unlock_bh(&bond->curr_slave_lock);
1924                 read_unlock(&bond->lock);
1925                 write_lock_bh(&bond->lock);
1926         }
1927
1928         if (bond->slave_cnt == 0) {
1929                 bond_set_carrier(bond);
1930
1931                 /* if the last slave was removed, zero the mac address
1932                  * of the master so it will be set by the application
1933                  * to the mac address of the first slave
1934                  */
1935                 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
1936
1937                 if (list_empty(&bond->vlan_list)) {
1938                         bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1939                 } else {
1940                         pr_warning(DRV_NAME
1941                                ": %s: Warning: clearing HW address of %s while it "
1942                                "still has VLANs.\n",
1943                                bond_dev->name, bond_dev->name);
1944                         pr_warning(DRV_NAME
1945                                ": %s: When re-adding slaves, make sure the bond's "
1946                                "HW address matches its VLANs'.\n",
1947                                bond_dev->name);
1948                 }
1949         } else if ((bond_dev->features & NETIF_F_VLAN_CHALLENGED) &&
1950                    !bond_has_challenged_slaves(bond)) {
1951                 pr_info(DRV_NAME
1952                        ": %s: last VLAN challenged slave %s "
1953                        "left bond %s. VLAN blocking is removed\n",
1954                        bond_dev->name, slave_dev->name, bond_dev->name);
1955                 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
1956         }
1957
1958         write_unlock_bh(&bond->lock);
1959
1960         /* must do this from outside any spinlocks */
1961         bond_destroy_slave_symlinks(bond_dev, slave_dev);
1962
1963         bond_del_vlans_from_slave(bond, slave_dev);
1964
1965         /* If the mode USES_PRIMARY, then we should only remove its
1966          * promisc and mc settings if it was the curr_active_slave, but that was
1967          * already taken care of above when we detached the slave
1968          */
1969         if (!USES_PRIMARY(bond->params.mode)) {
1970                 /* unset promiscuity level from slave */
1971                 if (bond_dev->flags & IFF_PROMISC)
1972                         dev_set_promiscuity(slave_dev, -1);
1973
1974                 /* unset allmulti level from slave */
1975                 if (bond_dev->flags & IFF_ALLMULTI)
1976                         dev_set_allmulti(slave_dev, -1);
1977
1978                 /* flush master's mc_list from slave */
1979                 netif_addr_lock_bh(bond_dev);
1980                 bond_mc_list_flush(bond_dev, slave_dev);
1981                 netif_addr_unlock_bh(bond_dev);
1982         }
1983
1984         netdev_set_master(slave_dev, NULL);
1985
1986         /* close slave before restoring its mac address */
1987         dev_close(slave_dev);
1988
1989         if (bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
1990                 /* restore original ("permanent") mac address */
1991                 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
1992                 addr.sa_family = slave_dev->type;
1993                 dev_set_mac_address(slave_dev, &addr);
1994         }
1995
1996         slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
1997                                    IFF_SLAVE_INACTIVE | IFF_BONDING |
1998                                    IFF_SLAVE_NEEDARP);
1999
2000         kfree(slave);
2001
2002         return 0;  /* deletion OK */
2003 }
2004
2005 /*
2006 * Destroy a bonding device.
2007 * Must be under rtnl_lock when this function is called.
2008 */
2009 static void bond_uninit(struct net_device *bond_dev)
2010 {
2011         struct bonding *bond = netdev_priv(bond_dev);
2012
2013         bond_deinit(bond_dev);
2014         bond_destroy_sysfs_entry(bond);
2015
2016         if (bond->wq)
2017                 destroy_workqueue(bond->wq);
2018
2019         netif_addr_lock_bh(bond_dev);
2020         bond_mc_list_destroy(bond);
2021         netif_addr_unlock_bh(bond_dev);
2022 }
2023
2024 /*
2025 * First release a slave and than destroy the bond if no more slaves are left.
2026 * Must be under rtnl_lock when this function is called.
2027 */
2028 int  bond_release_and_destroy(struct net_device *bond_dev,
2029                               struct net_device *slave_dev)
2030 {
2031         struct bonding *bond = netdev_priv(bond_dev);
2032         int ret;
2033
2034         ret = bond_release(bond_dev, slave_dev);
2035         if ((ret == 0) && (bond->slave_cnt == 0)) {
2036                 pr_info(DRV_NAME ": %s: destroying bond %s.\n",
2037                        bond_dev->name, bond_dev->name);
2038                 unregister_netdevice(bond_dev);
2039         }
2040         return ret;
2041 }
2042
2043 /*
2044  * This function releases all slaves.
2045  */
2046 static int bond_release_all(struct net_device *bond_dev)
2047 {
2048         struct bonding *bond = netdev_priv(bond_dev);
2049         struct slave *slave;
2050         struct net_device *slave_dev;
2051         struct sockaddr addr;
2052
2053         write_lock_bh(&bond->lock);
2054
2055         netif_carrier_off(bond_dev);
2056
2057         if (bond->slave_cnt == 0)
2058                 goto out;
2059
2060         bond->current_arp_slave = NULL;
2061         bond->primary_slave = NULL;
2062         bond_change_active_slave(bond, NULL);
2063
2064         while ((slave = bond->first_slave) != NULL) {
2065                 /* Inform AD package of unbinding of slave
2066                  * before slave is detached from the list.
2067                  */
2068                 if (bond->params.mode == BOND_MODE_8023AD)
2069                         bond_3ad_unbind_slave(slave);
2070
2071                 slave_dev = slave->dev;
2072                 bond_detach_slave(bond, slave);
2073
2074                 /* now that the slave is detached, unlock and perform
2075                  * all the undo steps that should not be called from
2076                  * within a lock.
2077                  */
2078                 write_unlock_bh(&bond->lock);
2079
2080                 if (bond_is_lb(bond)) {
2081                         /* must be called only after the slave
2082                          * has been detached from the list
2083                          */
2084                         bond_alb_deinit_slave(bond, slave);
2085                 }
2086
2087                 bond_compute_features(bond);
2088
2089                 bond_destroy_slave_symlinks(bond_dev, slave_dev);
2090                 bond_del_vlans_from_slave(bond, slave_dev);
2091
2092                 /* If the mode USES_PRIMARY, then we should only remove its
2093                  * promisc and mc settings if it was the curr_active_slave, but that was
2094                  * already taken care of above when we detached the slave
2095                  */
2096                 if (!USES_PRIMARY(bond->params.mode)) {
2097                         /* unset promiscuity level from slave */
2098                         if (bond_dev->flags & IFF_PROMISC)
2099                                 dev_set_promiscuity(slave_dev, -1);
2100
2101                         /* unset allmulti level from slave */
2102                         if (bond_dev->flags & IFF_ALLMULTI)
2103                                 dev_set_allmulti(slave_dev, -1);
2104
2105                         /* flush master's mc_list from slave */
2106                         netif_addr_lock_bh(bond_dev);
2107                         bond_mc_list_flush(bond_dev, slave_dev);
2108                         netif_addr_unlock_bh(bond_dev);
2109                 }
2110
2111                 netdev_set_master(slave_dev, NULL);
2112
2113                 /* close slave before restoring its mac address */
2114                 dev_close(slave_dev);
2115
2116                 if (!bond->params.fail_over_mac) {
2117                         /* restore original ("permanent") mac address*/
2118                         memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
2119                         addr.sa_family = slave_dev->type;
2120                         dev_set_mac_address(slave_dev, &addr);
2121                 }
2122
2123                 slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
2124                                            IFF_SLAVE_INACTIVE);
2125
2126                 kfree(slave);
2127
2128                 /* re-acquire the lock before getting the next slave */
2129                 write_lock_bh(&bond->lock);
2130         }
2131
2132         /* zero the mac address of the master so it will be
2133          * set by the application to the mac address of the
2134          * first slave
2135          */
2136         memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
2137
2138         if (list_empty(&bond->vlan_list))
2139                 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
2140         else {
2141                 pr_warning(DRV_NAME
2142                        ": %s: Warning: clearing HW address of %s while it "
2143                        "still has VLANs.\n",
2144                        bond_dev->name, bond_dev->name);
2145                 pr_warning(DRV_NAME
2146                        ": %s: When re-adding slaves, make sure the bond's "
2147                        "HW address matches its VLANs'.\n",
2148                        bond_dev->name);
2149         }
2150
2151         pr_info(DRV_NAME
2152                ": %s: released all slaves\n",
2153                bond_dev->name);
2154
2155 out:
2156         write_unlock_bh(&bond->lock);
2157
2158         return 0;
2159 }
2160
2161 /*
2162  * This function changes the active slave to slave <slave_dev>.
2163  * It returns -EINVAL in the following cases.
2164  *  - <slave_dev> is not found in the list.
2165  *  - There is not active slave now.
2166  *  - <slave_dev> is already active.
2167  *  - The link state of <slave_dev> is not BOND_LINK_UP.
2168  *  - <slave_dev> is not running.
2169  * In these cases, this function does nothing.
2170  * In the other cases, current_slave pointer is changed and 0 is returned.
2171  */
2172 static int bond_ioctl_change_active(struct net_device *bond_dev, struct net_device *slave_dev)
2173 {
2174         struct bonding *bond = netdev_priv(bond_dev);
2175         struct slave *old_active = NULL;
2176         struct slave *new_active = NULL;
2177         int res = 0;
2178
2179         if (!USES_PRIMARY(bond->params.mode))
2180                 return -EINVAL;
2181
2182         /* Verify that master_dev is indeed the master of slave_dev */
2183         if (!(slave_dev->flags & IFF_SLAVE) || (slave_dev->master != bond_dev))
2184                 return -EINVAL;
2185
2186         read_lock(&bond->lock);
2187
2188         read_lock(&bond->curr_slave_lock);
2189         old_active = bond->curr_active_slave;
2190         read_unlock(&bond->curr_slave_lock);
2191
2192         new_active = bond_get_slave_by_dev(bond, slave_dev);
2193
2194         /*
2195          * Changing to the current active: do nothing; return success.
2196          */
2197         if (new_active && (new_active == old_active)) {
2198                 read_unlock(&bond->lock);
2199                 return 0;
2200         }
2201
2202         if ((new_active) &&
2203             (old_active) &&
2204             (new_active->link == BOND_LINK_UP) &&
2205             IS_UP(new_active->dev)) {
2206                 write_lock_bh(&bond->curr_slave_lock);
2207                 bond_change_active_slave(bond, new_active);
2208                 write_unlock_bh(&bond->curr_slave_lock);
2209         } else
2210                 res = -EINVAL;
2211
2212         read_unlock(&bond->lock);
2213
2214         return res;
2215 }
2216
2217 static int bond_info_query(struct net_device *bond_dev, struct ifbond *info)
2218 {
2219         struct bonding *bond = netdev_priv(bond_dev);
2220
2221         info->bond_mode = bond->params.mode;
2222         info->miimon = bond->params.miimon;
2223
2224         read_lock(&bond->lock);
2225         info->num_slaves = bond->slave_cnt;
2226         read_unlock(&bond->lock);
2227
2228         return 0;
2229 }
2230
2231 static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info)
2232 {
2233         struct bonding *bond = netdev_priv(bond_dev);
2234         struct slave *slave;
2235         int i, res = -ENODEV;
2236
2237         read_lock(&bond->lock);
2238
2239         bond_for_each_slave(bond, slave, i) {
2240                 if (i == (int)info->slave_id) {
2241                         res = 0;
2242                         strcpy(info->slave_name, slave->dev->name);
2243                         info->link = slave->link;
2244                         info->state = slave->state;
2245                         info->link_failure_count = slave->link_failure_count;
2246                         break;
2247                 }
2248         }
2249
2250         read_unlock(&bond->lock);
2251
2252         return res;
2253 }
2254
2255 /*-------------------------------- Monitoring -------------------------------*/
2256
2257
2258 static int bond_miimon_inspect(struct bonding *bond)
2259 {
2260         struct slave *slave;
2261         int i, link_state, commit = 0;
2262         bool ignore_updelay;
2263
2264         ignore_updelay = !bond->curr_active_slave ? true : false;
2265
2266         bond_for_each_slave(bond, slave, i) {
2267                 slave->new_link = BOND_LINK_NOCHANGE;
2268
2269                 link_state = bond_check_dev_link(bond, slave->dev, 0);
2270
2271                 switch (slave->link) {
2272                 case BOND_LINK_UP:
2273                         if (link_state)
2274                                 continue;
2275
2276                         slave->link = BOND_LINK_FAIL;
2277                         slave->delay = bond->params.downdelay;
2278                         if (slave->delay) {
2279                                 pr_info(DRV_NAME
2280                                        ": %s: link status down for %s"
2281                                        "interface %s, disabling it in %d ms.\n",
2282                                        bond->dev->name,
2283                                        (bond->params.mode ==
2284                                         BOND_MODE_ACTIVEBACKUP) ?
2285                                        ((slave->state == BOND_STATE_ACTIVE) ?
2286                                         "active " : "backup ") : "",
2287                                        slave->dev->name,
2288                                        bond->params.downdelay * bond->params.miimon);
2289                         }
2290                         /*FALLTHRU*/
2291                 case BOND_LINK_FAIL:
2292                         if (link_state) {
2293                                 /*
2294                                  * recovered before downdelay expired
2295                                  */
2296                                 slave->link = BOND_LINK_UP;
2297                                 slave->jiffies = jiffies;
2298                                 pr_info(DRV_NAME
2299                                        ": %s: link status up again after %d "
2300                                        "ms for interface %s.\n",
2301                                        bond->dev->name,
2302                                        (bond->params.downdelay - slave->delay) *
2303                                        bond->params.miimon,
2304                                        slave->dev->name);
2305                                 continue;
2306                         }
2307
2308                         if (slave->delay <= 0) {
2309                                 slave->new_link = BOND_LINK_DOWN;
2310                                 commit++;
2311                                 continue;
2312                         }
2313
2314                         slave->delay--;
2315                         break;
2316
2317                 case BOND_LINK_DOWN:
2318                         if (!link_state)
2319                                 continue;
2320
2321                         slave->link = BOND_LINK_BACK;
2322                         slave->delay = bond->params.updelay;
2323
2324                         if (slave->delay) {
2325                                 pr_info(DRV_NAME
2326                                        ": %s: link status up for "
2327                                        "interface %s, enabling it in %d ms.\n",
2328                                        bond->dev->name, slave->dev->name,
2329                                        ignore_updelay ? 0 :
2330                                        bond->params.updelay *
2331                                        bond->params.miimon);
2332                         }
2333                         /*FALLTHRU*/
2334                 case BOND_LINK_BACK:
2335                         if (!link_state) {
2336                                 slave->link = BOND_LINK_DOWN;
2337                                 pr_info(DRV_NAME
2338                                        ": %s: link status down again after %d "
2339                                        "ms for interface %s.\n",
2340                                        bond->dev->name,
2341                                        (bond->params.updelay - slave->delay) *
2342                                        bond->params.miimon,
2343                                        slave->dev->name);
2344
2345                                 continue;
2346                         }
2347
2348                         if (ignore_updelay)
2349                                 slave->delay = 0;
2350
2351                         if (slave->delay <= 0) {
2352                                 slave->new_link = BOND_LINK_UP;
2353                                 commit++;
2354                                 ignore_updelay = false;
2355                                 continue;
2356                         }
2357
2358                         slave->delay--;
2359                         break;
2360                 }
2361         }
2362
2363         return commit;
2364 }
2365
2366 static void bond_miimon_commit(struct bonding *bond)
2367 {
2368         struct slave *slave;
2369         int i;
2370
2371         bond_for_each_slave(bond, slave, i) {
2372                 switch (slave->new_link) {
2373                 case BOND_LINK_NOCHANGE:
2374                         continue;
2375
2376                 case BOND_LINK_UP:
2377                         slave->link = BOND_LINK_UP;
2378                         slave->jiffies = jiffies;
2379
2380                         if (bond->params.mode == BOND_MODE_8023AD) {
2381                                 /* prevent it from being the active one */
2382                                 slave->state = BOND_STATE_BACKUP;
2383                         } else if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
2384                                 /* make it immediately active */
2385                                 slave->state = BOND_STATE_ACTIVE;
2386                         } else if (slave != bond->primary_slave) {
2387                                 /* prevent it from being the active one */
2388                                 slave->state = BOND_STATE_BACKUP;
2389                         }
2390
2391                         pr_info(DRV_NAME
2392                                ": %s: link status definitely "
2393                                "up for interface %s.\n",
2394                                bond->dev->name, slave->dev->name);
2395
2396                         /* notify ad that the link status has changed */
2397                         if (bond->params.mode == BOND_MODE_8023AD)
2398                                 bond_3ad_handle_link_change(slave, BOND_LINK_UP);
2399
2400                         if (bond_is_lb(bond))
2401                                 bond_alb_handle_link_change(bond, slave,
2402                                                             BOND_LINK_UP);
2403
2404                         if (!bond->curr_active_slave ||
2405                             (slave == bond->primary_slave))
2406                                 goto do_failover;
2407
2408                         continue;
2409
2410                 case BOND_LINK_DOWN:
2411                         if (slave->link_failure_count < UINT_MAX)
2412                                 slave->link_failure_count++;
2413
2414                         slave->link = BOND_LINK_DOWN;
2415
2416                         if (bond->params.mode == BOND_MODE_ACTIVEBACKUP ||
2417                             bond->params.mode == BOND_MODE_8023AD)
2418                                 bond_set_slave_inactive_flags(slave);
2419
2420                         pr_info(DRV_NAME
2421                                ": %s: link status definitely down for "
2422                                "interface %s, disabling it\n",
2423                                bond->dev->name, slave->dev->name);
2424
2425                         if (bond->params.mode == BOND_MODE_8023AD)
2426                                 bond_3ad_handle_link_change(slave,
2427                                                             BOND_LINK_DOWN);
2428
2429                         if (bond_is_lb(bond))
2430                                 bond_alb_handle_link_change(bond, slave,
2431                                                             BOND_LINK_DOWN);
2432
2433                         if (slave == bond->curr_active_slave)
2434                                 goto do_failover;
2435
2436                         continue;
2437
2438                 default:
2439                         pr_err(DRV_NAME
2440                                ": %s: invalid new link %d on slave %s\n",
2441                                bond->dev->name, slave->new_link,
2442                                slave->dev->name);
2443                         slave->new_link = BOND_LINK_NOCHANGE;
2444
2445                         continue;
2446                 }
2447
2448 do_failover:
2449                 ASSERT_RTNL();
2450                 write_lock_bh(&bond->curr_slave_lock);
2451                 bond_select_active_slave(bond);
2452                 write_unlock_bh(&bond->curr_slave_lock);
2453         }
2454
2455         bond_set_carrier(bond);
2456 }
2457
2458 /*
2459  * bond_mii_monitor
2460  *
2461  * Really a wrapper that splits the mii monitor into two phases: an
2462  * inspection, then (if inspection indicates something needs to be done)
2463  * an acquisition of appropriate locks followed by a commit phase to
2464  * implement whatever link state changes are indicated.
2465  */
2466 void bond_mii_monitor(struct work_struct *work)
2467 {
2468         struct bonding *bond = container_of(work, struct bonding,
2469                                             mii_work.work);
2470
2471         read_lock(&bond->lock);
2472         if (bond->kill_timers)
2473                 goto out;
2474
2475         if (bond->slave_cnt == 0)
2476                 goto re_arm;
2477
2478         if (bond->send_grat_arp) {
2479                 read_lock(&bond->curr_slave_lock);
2480                 bond_send_gratuitous_arp(bond);
2481                 read_unlock(&bond->curr_slave_lock);
2482         }
2483
2484         if (bond->send_unsol_na) {
2485                 read_lock(&bond->curr_slave_lock);
2486                 bond_send_unsolicited_na(bond);
2487                 read_unlock(&bond->curr_slave_lock);
2488         }
2489
2490         if (bond_miimon_inspect(bond)) {
2491                 read_unlock(&bond->lock);
2492                 rtnl_lock();
2493                 read_lock(&bond->lock);
2494
2495                 bond_miimon_commit(bond);
2496
2497                 read_unlock(&bond->lock);
2498                 rtnl_unlock();  /* might sleep, hold no other locks */
2499                 read_lock(&bond->lock);
2500         }
2501
2502 re_arm:
2503         if (bond->params.miimon)
2504                 queue_delayed_work(bond->wq, &bond->mii_work,
2505                                    msecs_to_jiffies(bond->params.miimon));
2506 out:
2507         read_unlock(&bond->lock);
2508 }
2509
2510 static __be32 bond_glean_dev_ip(struct net_device *dev)
2511 {
2512         struct in_device *idev;
2513         struct in_ifaddr *ifa;
2514         __be32 addr = 0;
2515
2516         if (!dev)
2517                 return 0;
2518
2519         rcu_read_lock();
2520         idev = __in_dev_get_rcu(dev);
2521         if (!idev)
2522                 goto out;
2523
2524         ifa = idev->ifa_list;
2525         if (!ifa)
2526                 goto out;
2527
2528         addr = ifa->ifa_local;
2529 out:
2530         rcu_read_unlock();
2531         return addr;
2532 }
2533
2534 static int bond_has_this_ip(struct bonding *bond, __be32 ip)
2535 {
2536         struct vlan_entry *vlan;
2537
2538         if (ip == bond->master_ip)
2539                 return 1;
2540
2541         list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
2542                 if (ip == vlan->vlan_ip)
2543                         return 1;
2544         }
2545
2546         return 0;
2547 }
2548
2549 /*
2550  * We go to the (large) trouble of VLAN tagging ARP frames because
2551  * switches in VLAN mode (especially if ports are configured as
2552  * "native" to a VLAN) might not pass non-tagged frames.
2553  */
2554 static void bond_arp_send(struct net_device *slave_dev, int arp_op, __be32 dest_ip, __be32 src_ip, unsigned short vlan_id)
2555 {
2556         struct sk_buff *skb;
2557
2558         pr_debug("arp %d on slave %s: dst %x src %x vid %d\n", arp_op,
2559                slave_dev->name, dest_ip, src_ip, vlan_id);
2560
2561         skb = arp_create(arp_op, ETH_P_ARP, dest_ip, slave_dev, src_ip,
2562                          NULL, slave_dev->dev_addr, NULL);
2563
2564         if (!skb) {
2565                 pr_err(DRV_NAME ": ARP packet allocation failed\n");
2566                 return;
2567         }
2568         if (vlan_id) {
2569                 skb = vlan_put_tag(skb, vlan_id);
2570                 if (!skb) {
2571                         pr_err(DRV_NAME ": failed to insert VLAN tag\n");
2572                         return;
2573                 }
2574         }
2575         arp_xmit(skb);
2576 }
2577
2578
2579 static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
2580 {
2581         int i, vlan_id, rv;
2582         __be32 *targets = bond->params.arp_targets;
2583         struct vlan_entry *vlan;
2584         struct net_device *vlan_dev;
2585         struct flowi fl;
2586         struct rtable *rt;
2587
2588         for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
2589                 if (!targets[i])
2590                         break;
2591                 pr_debug("basa: target %x\n", targets[i]);
2592                 if (list_empty(&bond->vlan_list)) {
2593                         pr_debug("basa: empty vlan: arp_send\n");
2594                         bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2595                                       bond->master_ip, 0);
2596                         continue;
2597                 }
2598
2599                 /*
2600                  * If VLANs are configured, we do a route lookup to
2601                  * determine which VLAN interface would be used, so we
2602                  * can tag the ARP with the proper VLAN tag.
2603                  */
2604                 memset(&fl, 0, sizeof(fl));
2605                 fl.fl4_dst = targets[i];
2606                 fl.fl4_tos = RTO_ONLINK;
2607
2608                 rv = ip_route_output_key(&init_net, &rt, &fl);
2609                 if (rv) {
2610                         if (net_ratelimit()) {
2611                                 pr_warning(DRV_NAME
2612                              ": %s: no route to arp_ip_target %pI4\n",
2613                                        bond->dev->name, &fl.fl4_dst);
2614                         }
2615                         continue;
2616                 }
2617
2618                 /*
2619                  * This target is not on a VLAN
2620                  */
2621                 if (rt->u.dst.dev == bond->dev) {
2622                         ip_rt_put(rt);
2623                         pr_debug("basa: rtdev == bond->dev: arp_send\n");
2624                         bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2625                                       bond->master_ip, 0);
2626                         continue;
2627                 }
2628
2629                 vlan_id = 0;
2630                 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
2631                         vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
2632                         if (vlan_dev == rt->u.dst.dev) {
2633                                 vlan_id = vlan->vlan_id;
2634                                 pr_debug("basa: vlan match on %s %d\n",
2635                                        vlan_dev->name, vlan_id);
2636                                 break;
2637                         }
2638                 }
2639
2640                 if (vlan_id) {
2641                         ip_rt_put(rt);
2642                         bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2643                                       vlan->vlan_ip, vlan_id);
2644                         continue;
2645                 }
2646
2647                 if (net_ratelimit()) {
2648                         pr_warning(DRV_NAME
2649                ": %s: no path to arp_ip_target %pI4 via rt.dev %s\n",
2650                                bond->dev->name, &fl.fl4_dst,
2651                                rt->u.dst.dev ? rt->u.dst.dev->name : "NULL");
2652                 }
2653                 ip_rt_put(rt);
2654         }
2655 }
2656
2657 /*
2658  * Kick out a gratuitous ARP for an IP on the bonding master plus one
2659  * for each VLAN above us.
2660  *
2661  * Caller must hold curr_slave_lock for read or better
2662  */
2663 static void bond_send_gratuitous_arp(struct bonding *bond)
2664 {
2665         struct slave *slave = bond->curr_active_slave;
2666         struct vlan_entry *vlan;
2667         struct net_device *vlan_dev;
2668
2669         pr_debug("bond_send_grat_arp: bond %s slave %s\n", bond->dev->name,
2670                                 slave ? slave->dev->name : "NULL");
2671
2672         if (!slave || !bond->send_grat_arp ||
2673             test_bit(__LINK_STATE_LINKWATCH_PENDING, &slave->dev->state))
2674                 return;
2675
2676         bond->send_grat_arp--;
2677
2678         if (bond->master_ip) {
2679                 bond_arp_send(slave->dev, ARPOP_REPLY, bond->master_ip,
2680                                 bond->master_ip, 0);
2681         }
2682
2683         list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
2684                 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
2685                 if (vlan->vlan_ip) {
2686                         bond_arp_send(slave->dev, ARPOP_REPLY, vlan->vlan_ip,
2687                                       vlan->vlan_ip, vlan->vlan_id);
2688                 }
2689         }
2690 }
2691
2692 static void bond_validate_arp(struct bonding *bond, struct slave *slave, __be32 sip, __be32 tip)
2693 {
2694         int i;
2695         __be32 *targets = bond->params.arp_targets;
2696
2697         for (i = 0; (i < BOND_MAX_ARP_TARGETS) && targets[i]; i++) {
2698                 pr_debug("bva: sip %pI4 tip %pI4 t[%d] %pI4 bhti(tip) %d\n",
2699                         &sip, &tip, i, &targets[i], bond_has_this_ip(bond, tip));
2700                 if (sip == targets[i]) {
2701                         if (bond_has_this_ip(bond, tip))
2702                                 slave->last_arp_rx = jiffies;
2703                         return;
2704                 }
2705         }
2706 }
2707
2708 static int bond_arp_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
2709 {
2710         struct arphdr *arp;
2711         struct slave *slave;
2712         struct bonding *bond;
2713         unsigned char *arp_ptr;
2714         __be32 sip, tip;
2715
2716         if (dev_net(dev) != &init_net)
2717                 goto out;
2718
2719         if (!(dev->priv_flags & IFF_BONDING) || !(dev->flags & IFF_MASTER))
2720                 goto out;
2721
2722         bond = netdev_priv(dev);
2723         read_lock(&bond->lock);
2724
2725         pr_debug("bond_arp_rcv: bond %s skb->dev %s orig_dev %s\n",
2726                 bond->dev->name, skb->dev ? skb->dev->name : "NULL",
2727                 orig_dev ? orig_dev->name : "NULL");
2728
2729         slave = bond_get_slave_by_dev(bond, orig_dev);
2730         if (!slave || !slave_do_arp_validate(bond, slave))
2731                 goto out_unlock;
2732
2733         if (!pskb_may_pull(skb, arp_hdr_len(dev)))
2734                 goto out_unlock;
2735
2736         arp = arp_hdr(skb);
2737         if (arp->ar_hln != dev->addr_len ||
2738             skb->pkt_type == PACKET_OTHERHOST ||
2739             skb->pkt_type == PACKET_LOOPBACK ||
2740             arp->ar_hrd != htons(ARPHRD_ETHER) ||
2741             arp->ar_pro != htons(ETH_P_IP) ||
2742             arp->ar_pln != 4)
2743                 goto out_unlock;
2744
2745         arp_ptr = (unsigned char *)(arp + 1);
2746         arp_ptr += dev->addr_len;
2747         memcpy(&sip, arp_ptr, 4);
2748         arp_ptr += 4 + dev->addr_len;
2749         memcpy(&tip, arp_ptr, 4);
2750
2751         pr_debug("bond_arp_rcv: %s %s/%d av %d sv %d sip %pI4 tip %pI4\n",
2752                 bond->dev->name, slave->dev->name, slave->state,
2753                 bond->params.arp_validate, slave_do_arp_validate(bond, slave),
2754                 &sip, &tip);
2755
2756         /*
2757          * Backup slaves won't see the ARP reply, but do come through
2758          * here for each ARP probe (so we swap the sip/tip to validate
2759          * the probe).  In a "redundant switch, common router" type of
2760          * configuration, the ARP probe will (hopefully) travel from
2761          * the active, through one switch, the router, then the other
2762          * switch before reaching the backup.
2763          */
2764         if (slave->state == BOND_STATE_ACTIVE)
2765                 bond_validate_arp(bond, slave, sip, tip);
2766         else
2767                 bond_validate_arp(bond, slave, tip, sip);
2768
2769 out_unlock:
2770         read_unlock(&bond->lock);
2771 out:
2772         dev_kfree_skb(skb);
2773         return NET_RX_SUCCESS;
2774 }
2775
2776 /*
2777  * this function is called regularly to monitor each slave's link
2778  * ensuring that traffic is being sent and received when arp monitoring
2779  * is used in load-balancing mode. if the adapter has been dormant, then an
2780  * arp is transmitted to generate traffic. see activebackup_arp_monitor for
2781  * arp monitoring in active backup mode.
2782  */
2783 void bond_loadbalance_arp_mon(struct work_struct *work)
2784 {
2785         struct bonding *bond = container_of(work, struct bonding,
2786                                             arp_work.work);
2787         struct slave *slave, *oldcurrent;
2788         int do_failover = 0;
2789         int delta_in_ticks;
2790         int i;
2791
2792         read_lock(&bond->lock);
2793
2794         delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
2795
2796         if (bond->kill_timers)
2797                 goto out;
2798
2799         if (bond->slave_cnt == 0)
2800                 goto re_arm;
2801
2802         read_lock(&bond->curr_slave_lock);
2803         oldcurrent = bond->curr_active_slave;
2804         read_unlock(&bond->curr_slave_lock);
2805
2806         /* see if any of the previous devices are up now (i.e. they have
2807          * xmt and rcv traffic). the curr_active_slave does not come into
2808          * the picture unless it is null. also, slave->jiffies is not needed
2809          * here because we send an arp on each slave and give a slave as
2810          * long as it needs to get the tx/rx within the delta.
2811          * TODO: what about up/down delay in arp mode? it wasn't here before
2812          *       so it can wait
2813          */
2814         bond_for_each_slave(bond, slave, i) {
2815                 if (slave->link != BOND_LINK_UP) {
2816                         if (time_before_eq(jiffies, dev_trans_start(slave->dev) + delta_in_ticks) &&
2817                             time_before_eq(jiffies, slave->dev->last_rx + delta_in_ticks)) {
2818
2819                                 slave->link  = BOND_LINK_UP;
2820                                 slave->state = BOND_STATE_ACTIVE;
2821
2822                                 /* primary_slave has no meaning in round-robin
2823                                  * mode. the window of a slave being up and
2824                                  * curr_active_slave being null after enslaving
2825                                  * is closed.
2826                                  */
2827                                 if (!oldcurrent) {
2828                                         pr_info(DRV_NAME
2829                                                ": %s: link status definitely "
2830                                                "up for interface %s, ",
2831                                                bond->dev->name,
2832                                                slave->dev->name);
2833                                         do_failover = 1;
2834                                 } else {
2835                                         pr_info(DRV_NAME
2836                                                ": %s: interface %s is now up\n",
2837                                                bond->dev->name,
2838                                                slave->dev->name);
2839                                 }
2840                         }
2841                 } else {
2842                         /* slave->link == BOND_LINK_UP */
2843
2844                         /* not all switches will respond to an arp request
2845                          * when the source ip is 0, so don't take the link down
2846                          * if we don't know our ip yet
2847                          */
2848                         if (time_after_eq(jiffies, dev_trans_start(slave->dev) + 2*delta_in_ticks) ||
2849                             (time_after_eq(jiffies, slave->dev->last_rx + 2*delta_in_ticks))) {
2850
2851                                 slave->link  = BOND_LINK_DOWN;
2852                                 slave->state = BOND_STATE_BACKUP;
2853
2854                                 if (slave->link_failure_count < UINT_MAX)
2855                                         slave->link_failure_count++;
2856
2857                                 pr_info(DRV_NAME
2858                                        ": %s: interface %s is now down.\n",
2859                                        bond->dev->name,
2860                                        slave->dev->name);
2861
2862                                 if (slave == oldcurrent)
2863                                         do_failover = 1;
2864                         }
2865                 }
2866
2867                 /* note: if switch is in round-robin mode, all links
2868                  * must tx arp to ensure all links rx an arp - otherwise
2869                  * links may oscillate or not come up at all; if switch is
2870                  * in something like xor mode, there is nothing we can
2871                  * do - all replies will be rx'ed on same link causing slaves
2872                  * to be unstable during low/no traffic periods
2873                  */
2874                 if (IS_UP(slave->dev))
2875                         bond_arp_send_all(bond, slave);
2876         }
2877
2878         if (do_failover) {
2879                 write_lock_bh(&bond->curr_slave_lock);
2880
2881                 bond_select_active_slave(bond);
2882
2883                 write_unlock_bh(&bond->curr_slave_lock);
2884         }
2885
2886 re_arm:
2887         if (bond->params.arp_interval)
2888                 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
2889 out:
2890         read_unlock(&bond->lock);
2891 }
2892
2893 /*
2894  * Called to inspect slaves for active-backup mode ARP monitor link state
2895  * changes.  Sets new_link in slaves to specify what action should take
2896  * place for the slave.  Returns 0 if no changes are found, >0 if changes
2897  * to link states must be committed.
2898  *
2899  * Called with bond->lock held for read.
2900  */
2901 static int bond_ab_arp_inspect(struct bonding *bond, int delta_in_ticks)
2902 {
2903         struct slave *slave;
2904         int i, commit = 0;
2905
2906         bond_for_each_slave(bond, slave, i) {
2907                 slave->new_link = BOND_LINK_NOCHANGE;
2908
2909                 if (slave->link != BOND_LINK_UP) {
2910                         if (time_before_eq(jiffies, slave_last_rx(bond, slave) +
2911                                            delta_in_ticks)) {
2912                                 slave->new_link = BOND_LINK_UP;
2913                                 commit++;
2914                         }
2915
2916                         continue;
2917                 }
2918
2919                 /*
2920                  * Give slaves 2*delta after being enslaved or made
2921                  * active.  This avoids bouncing, as the last receive
2922                  * times need a full ARP monitor cycle to be updated.
2923                  */
2924                 if (!time_after_eq(jiffies, slave->jiffies +
2925                                    2 * delta_in_ticks))
2926                         continue;
2927
2928                 /*
2929                  * Backup slave is down if:
2930                  * - No current_arp_slave AND
2931                  * - more than 3*delta since last receive AND
2932                  * - the bond has an IP address
2933                  *
2934                  * Note: a non-null current_arp_slave indicates
2935                  * the curr_active_slave went down and we are
2936                  * searching for a new one; under this condition
2937                  * we only take the curr_active_slave down - this
2938                  * gives each slave a chance to tx/rx traffic
2939                  * before being taken out
2940                  */
2941                 if (slave->state == BOND_STATE_BACKUP &&
2942                     !bond->current_arp_slave &&
2943                     time_after(jiffies, slave_last_rx(bond, slave) +
2944                                3 * delta_in_ticks)) {
2945                         slave->new_link = BOND_LINK_DOWN;
2946                         commit++;
2947                 }
2948
2949                 /*
2950                  * Active slave is down if:
2951                  * - more than 2*delta since transmitting OR
2952                  * - (more than 2*delta since receive AND
2953                  *    the bond has an IP address)
2954                  */
2955                 if ((slave->state == BOND_STATE_ACTIVE) &&
2956                     (time_after_eq(jiffies, dev_trans_start(slave->dev) +
2957                                     2 * delta_in_ticks) ||
2958                       (time_after_eq(jiffies, slave_last_rx(bond, slave)
2959                                      + 2 * delta_in_ticks)))) {
2960                         slave->new_link = BOND_LINK_DOWN;
2961                         commit++;
2962                 }
2963         }
2964
2965         return commit;
2966 }
2967
2968 /*
2969  * Called to commit link state changes noted by inspection step of
2970  * active-backup mode ARP monitor.
2971  *
2972  * Called with RTNL and bond->lock for read.
2973  */
2974 static void bond_ab_arp_commit(struct bonding *bond, int delta_in_ticks)
2975 {
2976         struct slave *slave;
2977         int i;
2978
2979         bond_for_each_slave(bond, slave, i) {
2980                 switch (slave->new_link) {
2981                 case BOND_LINK_NOCHANGE:
2982                         continue;
2983
2984                 case BOND_LINK_UP:
2985                         if ((!bond->curr_active_slave &&
2986                              time_before_eq(jiffies,
2987                                             dev_trans_start(slave->dev) +
2988                                             delta_in_ticks)) ||
2989                             bond->curr_active_slave != slave) {
2990                                 slave->link = BOND_LINK_UP;
2991                                 bond->current_arp_slave = NULL;
2992
2993                                 pr_info(DRV_NAME
2994                         &nbs