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