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