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