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