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