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