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