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