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