xref: /linux/drivers/net/team/team_core.c (revision 69050f8d6d075dc01af7a5f2f550a8067510366f)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * drivers/net/team/team.c - Network team device driver
4  * Copyright (c) 2011 Jiri Pirko <jpirko@redhat.com>
5  */
6 
7 #include <linux/ethtool.h>
8 #include <linux/kernel.h>
9 #include <linux/types.h>
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/slab.h>
13 #include <linux/rcupdate.h>
14 #include <linux/errno.h>
15 #include <linux/ctype.h>
16 #include <linux/notifier.h>
17 #include <linux/netdevice.h>
18 #include <linux/netpoll.h>
19 #include <linux/if_vlan.h>
20 #include <linux/if_arp.h>
21 #include <linux/socket.h>
22 #include <linux/etherdevice.h>
23 #include <linux/rtnetlink.h>
24 #include <net/rtnetlink.h>
25 #include <net/genetlink.h>
26 #include <net/netdev_lock.h>
27 #include <net/netlink.h>
28 #include <net/sch_generic.h>
29 #include <linux/if_team.h>
30 
31 #include "team_nl.h"
32 
33 #define DRV_NAME "team"
34 
35 
36 /**********
37  * Helpers
38  **********/
39 
40 static struct team_port *team_port_get_rtnl(const struct net_device *dev)
41 {
42 	struct team_port *port = rtnl_dereference(dev->rx_handler_data);
43 
44 	return netif_is_team_port(dev) ? port : NULL;
45 }
46 
47 /*
48  * Since the ability to change device address for open port device is tested in
49  * team_port_add, this function can be called without control of return value
50  */
51 static int __set_port_dev_addr(struct net_device *port_dev,
52 			       const unsigned char *dev_addr)
53 {
54 	struct sockaddr_storage addr;
55 
56 	memcpy(addr.__data, dev_addr, port_dev->addr_len);
57 	addr.ss_family = port_dev->type;
58 	return dev_set_mac_address(port_dev, &addr, NULL);
59 }
60 
61 static int team_port_set_orig_dev_addr(struct team_port *port)
62 {
63 	return __set_port_dev_addr(port->dev, port->orig.dev_addr);
64 }
65 
66 static int team_port_set_team_dev_addr(struct team *team,
67 				       struct team_port *port)
68 {
69 	return __set_port_dev_addr(port->dev, team->dev->dev_addr);
70 }
71 
72 int team_modeop_port_enter(struct team *team, struct team_port *port)
73 {
74 	return team_port_set_team_dev_addr(team, port);
75 }
76 EXPORT_SYMBOL(team_modeop_port_enter);
77 
78 void team_modeop_port_change_dev_addr(struct team *team,
79 				      struct team_port *port)
80 {
81 	team_port_set_team_dev_addr(team, port);
82 }
83 EXPORT_SYMBOL(team_modeop_port_change_dev_addr);
84 
85 static void team_lower_state_changed(struct team_port *port)
86 {
87 	struct netdev_lag_lower_state_info info;
88 
89 	info.link_up = port->linkup;
90 	info.tx_enabled = team_port_enabled(port);
91 	netdev_lower_state_changed(port->dev, &info);
92 }
93 
94 static void team_refresh_port_linkup(struct team_port *port)
95 {
96 	bool new_linkup = port->user.linkup_enabled ? port->user.linkup :
97 						      port->state.linkup;
98 
99 	if (port->linkup != new_linkup) {
100 		port->linkup = new_linkup;
101 		team_lower_state_changed(port);
102 	}
103 }
104 
105 
106 /*******************
107  * Options handling
108  *******************/
109 
110 struct team_option_inst { /* One for each option instance */
111 	struct list_head list;
112 	struct list_head tmp_list;
113 	struct team_option *option;
114 	struct team_option_inst_info info;
115 	bool changed;
116 	bool removed;
117 };
118 
119 static struct team_option *__team_find_option(struct team *team,
120 					      const char *opt_name)
121 {
122 	struct team_option *option;
123 
124 	list_for_each_entry(option, &team->option_list, list) {
125 		if (strcmp(option->name, opt_name) == 0)
126 			return option;
127 	}
128 	return NULL;
129 }
130 
131 static void __team_option_inst_del(struct team_option_inst *opt_inst)
132 {
133 	list_del(&opt_inst->list);
134 	kfree(opt_inst);
135 }
136 
137 static void __team_option_inst_del_option(struct team *team,
138 					  struct team_option *option)
139 {
140 	struct team_option_inst *opt_inst, *tmp;
141 
142 	list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) {
143 		if (opt_inst->option == option)
144 			__team_option_inst_del(opt_inst);
145 	}
146 }
147 
148 static int __team_option_inst_add(struct team *team, struct team_option *option,
149 				  struct team_port *port)
150 {
151 	struct team_option_inst *opt_inst;
152 	unsigned int array_size;
153 	unsigned int i;
154 
155 	array_size = option->array_size;
156 	if (!array_size)
157 		array_size = 1; /* No array but still need one instance */
158 
159 	for (i = 0; i < array_size; i++) {
160 		opt_inst = kmalloc_obj(*opt_inst, GFP_KERNEL);
161 		if (!opt_inst)
162 			return -ENOMEM;
163 		opt_inst->option = option;
164 		opt_inst->info.port = port;
165 		opt_inst->info.array_index = i;
166 		opt_inst->changed = true;
167 		opt_inst->removed = false;
168 		list_add_tail(&opt_inst->list, &team->option_inst_list);
169 		if (option->init)
170 			option->init(team, &opt_inst->info);
171 
172 	}
173 	return 0;
174 }
175 
176 static int __team_option_inst_add_option(struct team *team,
177 					 struct team_option *option)
178 {
179 	int err;
180 
181 	if (!option->per_port) {
182 		err = __team_option_inst_add(team, option, NULL);
183 		if (err)
184 			goto inst_del_option;
185 	}
186 	return 0;
187 
188 inst_del_option:
189 	__team_option_inst_del_option(team, option);
190 	return err;
191 }
192 
193 static void __team_option_inst_mark_removed_option(struct team *team,
194 						   struct team_option *option)
195 {
196 	struct team_option_inst *opt_inst;
197 
198 	list_for_each_entry(opt_inst, &team->option_inst_list, list) {
199 		if (opt_inst->option == option) {
200 			opt_inst->changed = true;
201 			opt_inst->removed = true;
202 		}
203 	}
204 }
205 
206 static void __team_option_inst_del_port(struct team *team,
207 					struct team_port *port)
208 {
209 	struct team_option_inst *opt_inst, *tmp;
210 
211 	list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) {
212 		if (opt_inst->option->per_port &&
213 		    opt_inst->info.port == port)
214 			__team_option_inst_del(opt_inst);
215 	}
216 }
217 
218 static int __team_option_inst_add_port(struct team *team,
219 				       struct team_port *port)
220 {
221 	struct team_option *option;
222 	int err;
223 
224 	list_for_each_entry(option, &team->option_list, list) {
225 		if (!option->per_port)
226 			continue;
227 		err = __team_option_inst_add(team, option, port);
228 		if (err)
229 			goto inst_del_port;
230 	}
231 	return 0;
232 
233 inst_del_port:
234 	__team_option_inst_del_port(team, port);
235 	return err;
236 }
237 
238 static void __team_option_inst_mark_removed_port(struct team *team,
239 						 struct team_port *port)
240 {
241 	struct team_option_inst *opt_inst;
242 
243 	list_for_each_entry(opt_inst, &team->option_inst_list, list) {
244 		if (opt_inst->info.port == port) {
245 			opt_inst->changed = true;
246 			opt_inst->removed = true;
247 		}
248 	}
249 }
250 
251 static int __team_options_register(struct team *team,
252 				   const struct team_option *option,
253 				   size_t option_count)
254 {
255 	int i;
256 	struct team_option **dst_opts;
257 	int err;
258 
259 	dst_opts = kzalloc_objs(struct team_option *, option_count, GFP_KERNEL);
260 	if (!dst_opts)
261 		return -ENOMEM;
262 	for (i = 0; i < option_count; i++, option++) {
263 		if (__team_find_option(team, option->name)) {
264 			err = -EEXIST;
265 			goto alloc_rollback;
266 		}
267 		dst_opts[i] = kmemdup(option, sizeof(*option), GFP_KERNEL);
268 		if (!dst_opts[i]) {
269 			err = -ENOMEM;
270 			goto alloc_rollback;
271 		}
272 	}
273 
274 	for (i = 0; i < option_count; i++) {
275 		err = __team_option_inst_add_option(team, dst_opts[i]);
276 		if (err)
277 			goto inst_rollback;
278 		list_add_tail(&dst_opts[i]->list, &team->option_list);
279 	}
280 
281 	kfree(dst_opts);
282 	return 0;
283 
284 inst_rollback:
285 	for (i--; i >= 0; i--) {
286 		__team_option_inst_del_option(team, dst_opts[i]);
287 		list_del(&dst_opts[i]->list);
288 	}
289 
290 	i = option_count;
291 alloc_rollback:
292 	for (i--; i >= 0; i--)
293 		kfree(dst_opts[i]);
294 
295 	kfree(dst_opts);
296 	return err;
297 }
298 
299 static void __team_options_mark_removed(struct team *team,
300 					const struct team_option *option,
301 					size_t option_count)
302 {
303 	int i;
304 
305 	for (i = 0; i < option_count; i++, option++) {
306 		struct team_option *del_opt;
307 
308 		del_opt = __team_find_option(team, option->name);
309 		if (del_opt)
310 			__team_option_inst_mark_removed_option(team, del_opt);
311 	}
312 }
313 
314 static void __team_options_unregister(struct team *team,
315 				      const struct team_option *option,
316 				      size_t option_count)
317 {
318 	int i;
319 
320 	for (i = 0; i < option_count; i++, option++) {
321 		struct team_option *del_opt;
322 
323 		del_opt = __team_find_option(team, option->name);
324 		if (del_opt) {
325 			__team_option_inst_del_option(team, del_opt);
326 			list_del(&del_opt->list);
327 			kfree(del_opt);
328 		}
329 	}
330 }
331 
332 static void __team_options_change_check(struct team *team);
333 
334 int team_options_register(struct team *team,
335 			  const struct team_option *option,
336 			  size_t option_count)
337 {
338 	int err;
339 
340 	err = __team_options_register(team, option, option_count);
341 	if (err)
342 		return err;
343 	__team_options_change_check(team);
344 	return 0;
345 }
346 EXPORT_SYMBOL(team_options_register);
347 
348 void team_options_unregister(struct team *team,
349 			     const struct team_option *option,
350 			     size_t option_count)
351 {
352 	__team_options_mark_removed(team, option, option_count);
353 	__team_options_change_check(team);
354 	__team_options_unregister(team, option, option_count);
355 }
356 EXPORT_SYMBOL(team_options_unregister);
357 
358 static int team_option_get(struct team *team,
359 			   struct team_option_inst *opt_inst,
360 			   struct team_gsetter_ctx *ctx)
361 {
362 	if (!opt_inst->option->getter)
363 		return -EOPNOTSUPP;
364 
365 	opt_inst->option->getter(team, ctx);
366 	return 0;
367 }
368 
369 static int team_option_set(struct team *team,
370 			   struct team_option_inst *opt_inst,
371 			   struct team_gsetter_ctx *ctx)
372 {
373 	if (!opt_inst->option->setter)
374 		return -EOPNOTSUPP;
375 	return opt_inst->option->setter(team, ctx);
376 }
377 
378 void team_option_inst_set_change(struct team_option_inst_info *opt_inst_info)
379 {
380 	struct team_option_inst *opt_inst;
381 
382 	opt_inst = container_of(opt_inst_info, struct team_option_inst, info);
383 	opt_inst->changed = true;
384 }
385 EXPORT_SYMBOL(team_option_inst_set_change);
386 
387 void team_options_change_check(struct team *team)
388 {
389 	__team_options_change_check(team);
390 }
391 EXPORT_SYMBOL(team_options_change_check);
392 
393 
394 /****************
395  * Mode handling
396  ****************/
397 
398 static LIST_HEAD(mode_list);
399 static DEFINE_SPINLOCK(mode_list_lock);
400 
401 struct team_mode_item {
402 	struct list_head list;
403 	const struct team_mode *mode;
404 };
405 
406 static struct team_mode_item *__find_mode(const char *kind)
407 {
408 	struct team_mode_item *mitem;
409 
410 	list_for_each_entry(mitem, &mode_list, list) {
411 		if (strcmp(mitem->mode->kind, kind) == 0)
412 			return mitem;
413 	}
414 	return NULL;
415 }
416 
417 static bool is_good_mode_name(const char *name)
418 {
419 	while (*name != '\0') {
420 		if (!isalpha(*name) && !isdigit(*name) && *name != '_')
421 			return false;
422 		name++;
423 	}
424 	return true;
425 }
426 
427 int team_mode_register(const struct team_mode *mode)
428 {
429 	int err = 0;
430 	struct team_mode_item *mitem;
431 
432 	if (!is_good_mode_name(mode->kind) ||
433 	    mode->priv_size > TEAM_MODE_PRIV_SIZE)
434 		return -EINVAL;
435 
436 	mitem = kmalloc_obj(*mitem, GFP_KERNEL);
437 	if (!mitem)
438 		return -ENOMEM;
439 
440 	spin_lock(&mode_list_lock);
441 	if (__find_mode(mode->kind)) {
442 		err = -EEXIST;
443 		kfree(mitem);
444 		goto unlock;
445 	}
446 	mitem->mode = mode;
447 	list_add_tail(&mitem->list, &mode_list);
448 unlock:
449 	spin_unlock(&mode_list_lock);
450 	return err;
451 }
452 EXPORT_SYMBOL(team_mode_register);
453 
454 void team_mode_unregister(const struct team_mode *mode)
455 {
456 	struct team_mode_item *mitem;
457 
458 	spin_lock(&mode_list_lock);
459 	mitem = __find_mode(mode->kind);
460 	if (mitem) {
461 		list_del_init(&mitem->list);
462 		kfree(mitem);
463 	}
464 	spin_unlock(&mode_list_lock);
465 }
466 EXPORT_SYMBOL(team_mode_unregister);
467 
468 static const struct team_mode *team_mode_get(const char *kind)
469 {
470 	struct team_mode_item *mitem;
471 	const struct team_mode *mode = NULL;
472 
473 	if (!try_module_get(THIS_MODULE))
474 		return NULL;
475 
476 	spin_lock(&mode_list_lock);
477 	mitem = __find_mode(kind);
478 	if (!mitem) {
479 		spin_unlock(&mode_list_lock);
480 		request_module("team-mode-%s", kind);
481 		spin_lock(&mode_list_lock);
482 		mitem = __find_mode(kind);
483 	}
484 	if (mitem) {
485 		mode = mitem->mode;
486 		if (!try_module_get(mode->owner))
487 			mode = NULL;
488 	}
489 
490 	spin_unlock(&mode_list_lock);
491 	module_put(THIS_MODULE);
492 	return mode;
493 }
494 
495 static void team_mode_put(const struct team_mode *mode)
496 {
497 	module_put(mode->owner);
498 }
499 
500 static bool team_dummy_transmit(struct team *team, struct sk_buff *skb)
501 {
502 	dev_kfree_skb_any(skb);
503 	return false;
504 }
505 
506 static rx_handler_result_t team_dummy_receive(struct team *team,
507 					      struct team_port *port,
508 					      struct sk_buff *skb)
509 {
510 	return RX_HANDLER_ANOTHER;
511 }
512 
513 static const struct team_mode __team_no_mode = {
514 	.kind		= "*NOMODE*",
515 };
516 
517 static bool team_is_mode_set(struct team *team)
518 {
519 	return team->mode != &__team_no_mode;
520 }
521 
522 static void team_set_no_mode(struct team *team)
523 {
524 	team->user_carrier_enabled = false;
525 	team->mode = &__team_no_mode;
526 }
527 
528 static void team_adjust_ops(struct team *team)
529 {
530 	/*
531 	 * To avoid checks in rx/tx skb paths, ensure here that non-null and
532 	 * correct ops are always set.
533 	 */
534 
535 	if (!team->en_port_count || !team_is_mode_set(team) ||
536 	    !team->mode->ops->transmit)
537 		team->ops.transmit = team_dummy_transmit;
538 	else
539 		team->ops.transmit = team->mode->ops->transmit;
540 
541 	if (!team->en_port_count || !team_is_mode_set(team) ||
542 	    !team->mode->ops->receive)
543 		team->ops.receive = team_dummy_receive;
544 	else
545 		team->ops.receive = team->mode->ops->receive;
546 }
547 
548 /*
549  * We can benefit from the fact that it's ensured no port is present
550  * at the time of mode change. Therefore no packets are in fly so there's no
551  * need to set mode operations in any special way.
552  */
553 static int __team_change_mode(struct team *team,
554 			      const struct team_mode *new_mode)
555 {
556 	/* Check if mode was previously set and do cleanup if so */
557 	if (team_is_mode_set(team)) {
558 		void (*exit_op)(struct team *team) = team->ops.exit;
559 
560 		/* Clear ops area so no callback is called any longer */
561 		memset(&team->ops, 0, sizeof(struct team_mode_ops));
562 		team_adjust_ops(team);
563 
564 		if (exit_op)
565 			exit_op(team);
566 		team_mode_put(team->mode);
567 		team_set_no_mode(team);
568 		/* zero private data area */
569 		memset(&team->mode_priv, 0,
570 		       sizeof(struct team) - offsetof(struct team, mode_priv));
571 	}
572 
573 	if (!new_mode)
574 		return 0;
575 
576 	if (new_mode->ops->init) {
577 		int err;
578 
579 		err = new_mode->ops->init(team);
580 		if (err)
581 			return err;
582 	}
583 
584 	team->mode = new_mode;
585 	memcpy(&team->ops, new_mode->ops, sizeof(struct team_mode_ops));
586 	team_adjust_ops(team);
587 
588 	return 0;
589 }
590 
591 static int team_change_mode(struct team *team, const char *kind)
592 {
593 	const struct team_mode *new_mode;
594 	struct net_device *dev = team->dev;
595 	int err;
596 
597 	if (!list_empty(&team->port_list)) {
598 		netdev_err(dev, "No ports can be present during mode change\n");
599 		return -EBUSY;
600 	}
601 
602 	if (team_is_mode_set(team) && strcmp(team->mode->kind, kind) == 0) {
603 		netdev_err(dev, "Unable to change to the same mode the team is in\n");
604 		return -EINVAL;
605 	}
606 
607 	new_mode = team_mode_get(kind);
608 	if (!new_mode) {
609 		netdev_err(dev, "Mode \"%s\" not found\n", kind);
610 		return -EINVAL;
611 	}
612 
613 	err = __team_change_mode(team, new_mode);
614 	if (err) {
615 		netdev_err(dev, "Failed to change to mode \"%s\"\n", kind);
616 		team_mode_put(new_mode);
617 		return err;
618 	}
619 
620 	netdev_info(dev, "Mode changed to \"%s\"\n", kind);
621 	return 0;
622 }
623 
624 
625 /*********************
626  * Peers notification
627  *********************/
628 
629 static void team_notify_peers_work(struct work_struct *work)
630 {
631 	struct team *team;
632 	int val;
633 
634 	team = container_of(work, struct team, notify_peers.dw.work);
635 
636 	if (!rtnl_trylock()) {
637 		schedule_delayed_work(&team->notify_peers.dw, 0);
638 		return;
639 	}
640 	val = atomic_dec_if_positive(&team->notify_peers.count_pending);
641 	if (val < 0) {
642 		rtnl_unlock();
643 		return;
644 	}
645 	call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, team->dev);
646 	rtnl_unlock();
647 	if (val)
648 		schedule_delayed_work(&team->notify_peers.dw,
649 				      msecs_to_jiffies(team->notify_peers.interval));
650 }
651 
652 static void team_notify_peers(struct team *team)
653 {
654 	if (!team->notify_peers.count || !netif_running(team->dev))
655 		return;
656 	atomic_add(team->notify_peers.count, &team->notify_peers.count_pending);
657 	schedule_delayed_work(&team->notify_peers.dw, 0);
658 }
659 
660 static void team_notify_peers_init(struct team *team)
661 {
662 	INIT_DELAYED_WORK(&team->notify_peers.dw, team_notify_peers_work);
663 }
664 
665 static void team_notify_peers_fini(struct team *team)
666 {
667 	cancel_delayed_work_sync(&team->notify_peers.dw);
668 }
669 
670 
671 /*******************************
672  * Send multicast group rejoins
673  *******************************/
674 
675 static void team_mcast_rejoin_work(struct work_struct *work)
676 {
677 	struct team *team;
678 	int val;
679 
680 	team = container_of(work, struct team, mcast_rejoin.dw.work);
681 
682 	if (!rtnl_trylock()) {
683 		schedule_delayed_work(&team->mcast_rejoin.dw, 0);
684 		return;
685 	}
686 	val = atomic_dec_if_positive(&team->mcast_rejoin.count_pending);
687 	if (val < 0) {
688 		rtnl_unlock();
689 		return;
690 	}
691 	call_netdevice_notifiers(NETDEV_RESEND_IGMP, team->dev);
692 	rtnl_unlock();
693 	if (val)
694 		schedule_delayed_work(&team->mcast_rejoin.dw,
695 				      msecs_to_jiffies(team->mcast_rejoin.interval));
696 }
697 
698 static void team_mcast_rejoin(struct team *team)
699 {
700 	if (!team->mcast_rejoin.count || !netif_running(team->dev))
701 		return;
702 	atomic_add(team->mcast_rejoin.count, &team->mcast_rejoin.count_pending);
703 	schedule_delayed_work(&team->mcast_rejoin.dw, 0);
704 }
705 
706 static void team_mcast_rejoin_init(struct team *team)
707 {
708 	INIT_DELAYED_WORK(&team->mcast_rejoin.dw, team_mcast_rejoin_work);
709 }
710 
711 static void team_mcast_rejoin_fini(struct team *team)
712 {
713 	cancel_delayed_work_sync(&team->mcast_rejoin.dw);
714 }
715 
716 
717 /************************
718  * Rx path frame handler
719  ************************/
720 
721 /* note: already called with rcu_read_lock */
722 static rx_handler_result_t team_handle_frame(struct sk_buff **pskb)
723 {
724 	struct sk_buff *skb = *pskb;
725 	struct team_port *port;
726 	struct team *team;
727 	rx_handler_result_t res;
728 
729 	skb = skb_share_check(skb, GFP_ATOMIC);
730 	if (!skb)
731 		return RX_HANDLER_CONSUMED;
732 
733 	*pskb = skb;
734 
735 	port = team_port_get_rcu(skb->dev);
736 	team = port->team;
737 	if (!team_port_enabled(port)) {
738 		if (is_link_local_ether_addr(eth_hdr(skb)->h_dest))
739 			/* link-local packets are mostly useful when stack receives them
740 			 * with the link they arrive on.
741 			 */
742 			return RX_HANDLER_PASS;
743 		/* allow exact match delivery for disabled ports */
744 		res = RX_HANDLER_EXACT;
745 	} else {
746 		res = team->ops.receive(team, port, skb);
747 	}
748 	if (res == RX_HANDLER_ANOTHER) {
749 		struct team_pcpu_stats *pcpu_stats;
750 
751 		pcpu_stats = this_cpu_ptr(team->pcpu_stats);
752 		u64_stats_update_begin(&pcpu_stats->syncp);
753 		u64_stats_inc(&pcpu_stats->rx_packets);
754 		u64_stats_add(&pcpu_stats->rx_bytes, skb->len);
755 		if (skb->pkt_type == PACKET_MULTICAST)
756 			u64_stats_inc(&pcpu_stats->rx_multicast);
757 		u64_stats_update_end(&pcpu_stats->syncp);
758 
759 		skb->dev = team->dev;
760 	} else if (res == RX_HANDLER_EXACT) {
761 		this_cpu_inc(team->pcpu_stats->rx_nohandler);
762 	} else {
763 		this_cpu_inc(team->pcpu_stats->rx_dropped);
764 	}
765 
766 	return res;
767 }
768 
769 
770 /*************************************
771  * Multiqueue Tx port select override
772  *************************************/
773 
774 static int team_queue_override_init(struct team *team)
775 {
776 	struct list_head *listarr;
777 	unsigned int queue_cnt = team->dev->num_tx_queues - 1;
778 	unsigned int i;
779 
780 	if (!queue_cnt)
781 		return 0;
782 	listarr = kmalloc_objs(struct list_head, queue_cnt, GFP_KERNEL);
783 	if (!listarr)
784 		return -ENOMEM;
785 	team->qom_lists = listarr;
786 	for (i = 0; i < queue_cnt; i++)
787 		INIT_LIST_HEAD(listarr++);
788 	return 0;
789 }
790 
791 static void team_queue_override_fini(struct team *team)
792 {
793 	kfree(team->qom_lists);
794 }
795 
796 static struct list_head *__team_get_qom_list(struct team *team, u16 queue_id)
797 {
798 	return &team->qom_lists[queue_id - 1];
799 }
800 
801 /*
802  * note: already called with rcu_read_lock
803  */
804 static bool team_queue_override_transmit(struct team *team, struct sk_buff *skb)
805 {
806 	struct list_head *qom_list;
807 	struct team_port *port;
808 
809 	if (!team->queue_override_enabled || !skb->queue_mapping)
810 		return false;
811 	qom_list = __team_get_qom_list(team, skb->queue_mapping);
812 	list_for_each_entry_rcu(port, qom_list, qom_list) {
813 		if (!team_dev_queue_xmit(team, port, skb))
814 			return true;
815 	}
816 	return false;
817 }
818 
819 static void __team_queue_override_port_del(struct team *team,
820 					   struct team_port *port)
821 {
822 	if (!port->queue_id)
823 		return;
824 	list_del_rcu(&port->qom_list);
825 }
826 
827 static bool team_queue_override_port_has_gt_prio_than(struct team_port *port,
828 						      struct team_port *cur)
829 {
830 	if (port->priority < cur->priority)
831 		return true;
832 	if (port->priority > cur->priority)
833 		return false;
834 	if (port->index < cur->index)
835 		return true;
836 	return false;
837 }
838 
839 static void __team_queue_override_port_add(struct team *team,
840 					   struct team_port *port)
841 {
842 	struct team_port *cur;
843 	struct list_head *qom_list;
844 	struct list_head *node;
845 
846 	if (!port->queue_id)
847 		return;
848 	qom_list = __team_get_qom_list(team, port->queue_id);
849 	node = qom_list;
850 	list_for_each_entry(cur, qom_list, qom_list) {
851 		if (team_queue_override_port_has_gt_prio_than(port, cur))
852 			break;
853 		node = &cur->qom_list;
854 	}
855 	list_add_tail_rcu(&port->qom_list, node);
856 }
857 
858 static void __team_queue_override_enabled_check(struct team *team)
859 {
860 	struct team_port *port;
861 	bool enabled = false;
862 
863 	list_for_each_entry(port, &team->port_list, list) {
864 		if (port->queue_id) {
865 			enabled = true;
866 			break;
867 		}
868 	}
869 	if (enabled == team->queue_override_enabled)
870 		return;
871 	netdev_dbg(team->dev, "%s queue override\n",
872 		   enabled ? "Enabling" : "Disabling");
873 	team->queue_override_enabled = enabled;
874 }
875 
876 static void team_queue_override_port_prio_changed(struct team *team,
877 						  struct team_port *port)
878 {
879 	if (!port->queue_id || !team_port_enabled(port))
880 		return;
881 	__team_queue_override_port_del(team, port);
882 	__team_queue_override_port_add(team, port);
883 	__team_queue_override_enabled_check(team);
884 }
885 
886 static void team_queue_override_port_change_queue_id(struct team *team,
887 						     struct team_port *port,
888 						     u16 new_queue_id)
889 {
890 	if (team_port_enabled(port)) {
891 		__team_queue_override_port_del(team, port);
892 		port->queue_id = new_queue_id;
893 		__team_queue_override_port_add(team, port);
894 		__team_queue_override_enabled_check(team);
895 	} else {
896 		port->queue_id = new_queue_id;
897 	}
898 }
899 
900 static void team_queue_override_port_add(struct team *team,
901 					 struct team_port *port)
902 {
903 	__team_queue_override_port_add(team, port);
904 	__team_queue_override_enabled_check(team);
905 }
906 
907 static void team_queue_override_port_del(struct team *team,
908 					 struct team_port *port)
909 {
910 	__team_queue_override_port_del(team, port);
911 	__team_queue_override_enabled_check(team);
912 }
913 
914 
915 /****************
916  * Port handling
917  ****************/
918 
919 static bool team_port_find(const struct team *team,
920 			   const struct team_port *port)
921 {
922 	struct team_port *cur;
923 
924 	list_for_each_entry(cur, &team->port_list, list)
925 		if (cur == port)
926 			return true;
927 	return false;
928 }
929 
930 /*
931  * Enable/disable port by adding to enabled port hashlist and setting
932  * port->index (Might be racy so reader could see incorrect ifindex when
933  * processing a flying packet, but that is not a problem). Write guarded
934  * by RTNL.
935  */
936 static void team_port_enable(struct team *team,
937 			     struct team_port *port)
938 {
939 	if (team_port_enabled(port))
940 		return;
941 	port->index = team->en_port_count++;
942 	hlist_add_head_rcu(&port->hlist,
943 			   team_port_index_hash(team, port->index));
944 	team_adjust_ops(team);
945 	team_queue_override_port_add(team, port);
946 	if (team->ops.port_enabled)
947 		team->ops.port_enabled(team, port);
948 	team_notify_peers(team);
949 	team_mcast_rejoin(team);
950 	team_lower_state_changed(port);
951 }
952 
953 static void __reconstruct_port_hlist(struct team *team, int rm_index)
954 {
955 	int i;
956 	struct team_port *port;
957 
958 	for (i = rm_index + 1; i < team->en_port_count; i++) {
959 		port = team_get_port_by_index(team, i);
960 		hlist_del_rcu(&port->hlist);
961 		port->index--;
962 		hlist_add_head_rcu(&port->hlist,
963 				   team_port_index_hash(team, port->index));
964 	}
965 }
966 
967 static void team_port_disable(struct team *team,
968 			      struct team_port *port)
969 {
970 	if (!team_port_enabled(port))
971 		return;
972 	if (team->ops.port_disabled)
973 		team->ops.port_disabled(team, port);
974 	hlist_del_rcu(&port->hlist);
975 	__reconstruct_port_hlist(team, port->index);
976 	port->index = -1;
977 	team->en_port_count--;
978 	team_queue_override_port_del(team, port);
979 	team_adjust_ops(team);
980 	team_lower_state_changed(port);
981 }
982 
983 static int team_port_enter(struct team *team, struct team_port *port)
984 {
985 	int err = 0;
986 
987 	dev_hold(team->dev);
988 	if (team->ops.port_enter) {
989 		err = team->ops.port_enter(team, port);
990 		if (err) {
991 			netdev_err(team->dev, "Device %s failed to enter team mode\n",
992 				   port->dev->name);
993 			goto err_port_enter;
994 		}
995 	}
996 
997 	return 0;
998 
999 err_port_enter:
1000 	dev_put(team->dev);
1001 
1002 	return err;
1003 }
1004 
1005 static void team_port_leave(struct team *team, struct team_port *port)
1006 {
1007 	if (team->ops.port_leave)
1008 		team->ops.port_leave(team, port);
1009 	dev_put(team->dev);
1010 }
1011 
1012 #ifdef CONFIG_NET_POLL_CONTROLLER
1013 static int __team_port_enable_netpoll(struct team_port *port)
1014 {
1015 	struct netpoll *np;
1016 	int err;
1017 
1018 	np = kzalloc_obj(*np, GFP_KERNEL);
1019 	if (!np)
1020 		return -ENOMEM;
1021 
1022 	err = __netpoll_setup(np, port->dev);
1023 	if (err) {
1024 		kfree(np);
1025 		return err;
1026 	}
1027 	port->np = np;
1028 	return err;
1029 }
1030 
1031 static int team_port_enable_netpoll(struct team_port *port)
1032 {
1033 	if (!port->team->dev->npinfo)
1034 		return 0;
1035 
1036 	return __team_port_enable_netpoll(port);
1037 }
1038 
1039 static void team_port_disable_netpoll(struct team_port *port)
1040 {
1041 	struct netpoll *np = port->np;
1042 
1043 	if (!np)
1044 		return;
1045 	port->np = NULL;
1046 
1047 	__netpoll_free(np);
1048 }
1049 #else
1050 static int team_port_enable_netpoll(struct team_port *port)
1051 {
1052 	return 0;
1053 }
1054 static void team_port_disable_netpoll(struct team_port *port)
1055 {
1056 }
1057 #endif
1058 
1059 static int team_upper_dev_link(struct team *team, struct team_port *port,
1060 			       struct netlink_ext_ack *extack)
1061 {
1062 	struct netdev_lag_upper_info lag_upper_info;
1063 	int err;
1064 
1065 	lag_upper_info.tx_type = team->mode->lag_tx_type;
1066 	lag_upper_info.hash_type = NETDEV_LAG_HASH_UNKNOWN;
1067 	err = netdev_master_upper_dev_link(port->dev, team->dev, NULL,
1068 					   &lag_upper_info, extack);
1069 	if (err)
1070 		return err;
1071 	port->dev->priv_flags |= IFF_TEAM_PORT;
1072 	return 0;
1073 }
1074 
1075 static void team_upper_dev_unlink(struct team *team, struct team_port *port)
1076 {
1077 	netdev_upper_dev_unlink(port->dev, team->dev);
1078 	port->dev->priv_flags &= ~IFF_TEAM_PORT;
1079 }
1080 
1081 static void __team_port_change_port_added(struct team_port *port, bool linkup);
1082 static int team_dev_type_check_change(struct net_device *dev,
1083 				      struct net_device *port_dev);
1084 
1085 static int team_port_add(struct team *team, struct net_device *port_dev,
1086 			 struct netlink_ext_ack *extack)
1087 {
1088 	struct net_device *dev = team->dev;
1089 	struct team_port *port;
1090 	char *portname = port_dev->name;
1091 	int err;
1092 
1093 	if (port_dev->flags & IFF_LOOPBACK) {
1094 		NL_SET_ERR_MSG(extack, "Loopback device can't be added as a team port");
1095 		netdev_err(dev, "Device %s is loopback device. Loopback devices can't be added as a team port\n",
1096 			   portname);
1097 		return -EINVAL;
1098 	}
1099 
1100 	if (netif_is_team_port(port_dev)) {
1101 		NL_SET_ERR_MSG(extack, "Device is already a port of a team device");
1102 		netdev_err(dev, "Device %s is already a port "
1103 				"of a team device\n", portname);
1104 		return -EBUSY;
1105 	}
1106 
1107 	if (dev == port_dev) {
1108 		NL_SET_ERR_MSG(extack, "Cannot enslave team device to itself");
1109 		netdev_err(dev, "Cannot enslave team device to itself\n");
1110 		return -EINVAL;
1111 	}
1112 
1113 	if (netdev_has_upper_dev(dev, port_dev)) {
1114 		NL_SET_ERR_MSG(extack, "Device is already an upper device of the team interface");
1115 		netdev_err(dev, "Device %s is already an upper device of the team interface\n",
1116 			   portname);
1117 		return -EBUSY;
1118 	}
1119 
1120 	if (netdev_has_upper_dev(port_dev, dev)) {
1121 		NL_SET_ERR_MSG(extack, "Device is already a lower device of the team interface");
1122 		netdev_err(dev, "Device %s is already a lower device of the team interface\n",
1123 			   portname);
1124 		return -EBUSY;
1125 	}
1126 
1127 	if (port_dev->features & NETIF_F_VLAN_CHALLENGED &&
1128 	    vlan_uses_dev(dev)) {
1129 		NL_SET_ERR_MSG(extack, "Device is VLAN challenged and team device has VLAN set up");
1130 		netdev_err(dev, "Device %s is VLAN challenged and team device has VLAN set up\n",
1131 			   portname);
1132 		return -EPERM;
1133 	}
1134 
1135 	if (port_dev->flags & IFF_UP) {
1136 		NL_SET_ERR_MSG(extack, "Device is up. Set it down before adding it as a team port");
1137 		netdev_err(dev, "Device %s is up. Set it down before adding it as a team port\n",
1138 			   portname);
1139 		return -EBUSY;
1140 	}
1141 
1142 	port = kzalloc(sizeof(struct team_port) + team->mode->port_priv_size,
1143 		       GFP_KERNEL);
1144 	if (!port)
1145 		return -ENOMEM;
1146 
1147 	port->dev = port_dev;
1148 	port->team = team;
1149 	INIT_LIST_HEAD(&port->qom_list);
1150 
1151 	port->orig.mtu = port_dev->mtu;
1152 	/*
1153 	 * MTU assignment will be handled in team_dev_type_check_change
1154 	 * if dev and port_dev are of different types
1155 	 */
1156 	if (dev->type == port_dev->type) {
1157 		err = dev_set_mtu(port_dev, dev->mtu);
1158 		if (err) {
1159 			netdev_dbg(dev, "Error %d calling dev_set_mtu\n", err);
1160 			goto err_set_mtu;
1161 		}
1162 	}
1163 
1164 	memcpy(port->orig.dev_addr, port_dev->dev_addr, port_dev->addr_len);
1165 
1166 	err = team_port_enter(team, port);
1167 	if (err) {
1168 		netdev_err(dev, "Device %s failed to enter team mode\n",
1169 			   portname);
1170 		goto err_port_enter;
1171 	}
1172 
1173 	err = dev_open(port_dev, extack);
1174 	if (err) {
1175 		netdev_dbg(dev, "Device %s opening failed\n",
1176 			   portname);
1177 		goto err_dev_open;
1178 	}
1179 
1180 	err = vlan_vids_add_by_dev(port_dev, dev);
1181 	if (err) {
1182 		netdev_err(dev, "Failed to add vlan ids to device %s\n",
1183 				portname);
1184 		goto err_vids_add;
1185 	}
1186 
1187 	err = team_port_enable_netpoll(port);
1188 	if (err) {
1189 		netdev_err(dev, "Failed to enable netpoll on device %s\n",
1190 			   portname);
1191 		goto err_enable_netpoll;
1192 	}
1193 
1194 	if (!(dev->features & NETIF_F_LRO))
1195 		dev_disable_lro(port_dev);
1196 
1197 	err = netdev_rx_handler_register(port_dev, team_handle_frame,
1198 					 port);
1199 	if (err) {
1200 		netdev_err(dev, "Device %s failed to register rx_handler\n",
1201 			   portname);
1202 		goto err_handler_register;
1203 	}
1204 
1205 	err = team_upper_dev_link(team, port, extack);
1206 	if (err) {
1207 		netdev_err(dev, "Device %s failed to set upper link\n",
1208 			   portname);
1209 		goto err_set_upper_link;
1210 	}
1211 
1212 	err = __team_option_inst_add_port(team, port);
1213 	if (err) {
1214 		netdev_err(dev, "Device %s failed to add per-port options\n",
1215 			   portname);
1216 		goto err_option_port_add;
1217 	}
1218 
1219 	/* set promiscuity level to new slave */
1220 	if (dev->flags & IFF_PROMISC) {
1221 		err = dev_set_promiscuity(port_dev, 1);
1222 		if (err)
1223 			goto err_set_slave_promisc;
1224 	}
1225 
1226 	/* set allmulti level to new slave */
1227 	if (dev->flags & IFF_ALLMULTI) {
1228 		err = dev_set_allmulti(port_dev, 1);
1229 		if (err) {
1230 			if (dev->flags & IFF_PROMISC)
1231 				dev_set_promiscuity(port_dev, -1);
1232 			goto err_set_slave_allmulti;
1233 		}
1234 	}
1235 
1236 	err = team_dev_type_check_change(dev, port_dev);
1237 	if (err)
1238 		goto err_set_dev_type;
1239 
1240 	if (dev->flags & IFF_UP) {
1241 		netif_addr_lock_bh(dev);
1242 		dev_uc_sync_multiple(port_dev, dev);
1243 		dev_mc_sync_multiple(port_dev, dev);
1244 		netif_addr_unlock_bh(dev);
1245 	}
1246 
1247 	port->index = -1;
1248 	list_add_tail_rcu(&port->list, &team->port_list);
1249 	team_port_enable(team, port);
1250 	netdev_compute_master_upper_features(team->dev, true);
1251 	__team_port_change_port_added(port, !!netif_oper_up(port_dev));
1252 	__team_options_change_check(team);
1253 
1254 	netdev_info(dev, "Port device %s added\n", portname);
1255 
1256 	return 0;
1257 
1258 err_set_dev_type:
1259 err_set_slave_allmulti:
1260 err_set_slave_promisc:
1261 	__team_option_inst_del_port(team, port);
1262 
1263 err_option_port_add:
1264 	team_upper_dev_unlink(team, port);
1265 
1266 err_set_upper_link:
1267 	netdev_rx_handler_unregister(port_dev);
1268 
1269 err_handler_register:
1270 	team_port_disable_netpoll(port);
1271 
1272 err_enable_netpoll:
1273 	vlan_vids_del_by_dev(port_dev, dev);
1274 
1275 err_vids_add:
1276 	dev_close(port_dev);
1277 
1278 err_dev_open:
1279 	team_port_leave(team, port);
1280 	team_port_set_orig_dev_addr(port);
1281 
1282 err_port_enter:
1283 	dev_set_mtu(port_dev, port->orig.mtu);
1284 
1285 err_set_mtu:
1286 	kfree(port);
1287 
1288 	return err;
1289 }
1290 
1291 static void __team_port_change_port_removed(struct team_port *port);
1292 
1293 static int team_port_del(struct team *team, struct net_device *port_dev)
1294 {
1295 	struct net_device *dev = team->dev;
1296 	struct team_port *port;
1297 	char *portname = port_dev->name;
1298 
1299 	port = team_port_get_rtnl(port_dev);
1300 	if (!port || !team_port_find(team, port)) {
1301 		netdev_err(dev, "Device %s does not act as a port of this team\n",
1302 			   portname);
1303 		return -ENOENT;
1304 	}
1305 
1306 	team_port_disable(team, port);
1307 	list_del_rcu(&port->list);
1308 
1309 	if (dev->flags & IFF_PROMISC)
1310 		dev_set_promiscuity(port_dev, -1);
1311 	if (dev->flags & IFF_ALLMULTI)
1312 		dev_set_allmulti(port_dev, -1);
1313 
1314 	team_upper_dev_unlink(team, port);
1315 	netdev_rx_handler_unregister(port_dev);
1316 	team_port_disable_netpoll(port);
1317 	vlan_vids_del_by_dev(port_dev, dev);
1318 	if (dev->flags & IFF_UP) {
1319 		dev_uc_unsync(port_dev, dev);
1320 		dev_mc_unsync(port_dev, dev);
1321 	}
1322 	dev_close(port_dev);
1323 	team_port_leave(team, port);
1324 
1325 	__team_option_inst_mark_removed_port(team, port);
1326 	__team_options_change_check(team);
1327 	__team_option_inst_del_port(team, port);
1328 	__team_port_change_port_removed(port);
1329 
1330 	team_port_set_orig_dev_addr(port);
1331 	dev_set_mtu(port_dev, port->orig.mtu);
1332 	kfree_rcu(port, rcu);
1333 	netdev_info(dev, "Port device %s removed\n", portname);
1334 	netdev_compute_master_upper_features(team->dev, true);
1335 
1336 	return 0;
1337 }
1338 
1339 
1340 /*****************
1341  * Net device ops
1342  *****************/
1343 
1344 static void team_mode_option_get(struct team *team, struct team_gsetter_ctx *ctx)
1345 {
1346 	ctx->data.str_val = team->mode->kind;
1347 }
1348 
1349 static int team_mode_option_set(struct team *team, struct team_gsetter_ctx *ctx)
1350 {
1351 	return team_change_mode(team, ctx->data.str_val);
1352 }
1353 
1354 static void team_notify_peers_count_get(struct team *team,
1355 					struct team_gsetter_ctx *ctx)
1356 {
1357 	ctx->data.u32_val = team->notify_peers.count;
1358 }
1359 
1360 static int team_notify_peers_count_set(struct team *team,
1361 				       struct team_gsetter_ctx *ctx)
1362 {
1363 	team->notify_peers.count = ctx->data.u32_val;
1364 	return 0;
1365 }
1366 
1367 static void team_notify_peers_interval_get(struct team *team,
1368 					   struct team_gsetter_ctx *ctx)
1369 {
1370 	ctx->data.u32_val = team->notify_peers.interval;
1371 }
1372 
1373 static int team_notify_peers_interval_set(struct team *team,
1374 					  struct team_gsetter_ctx *ctx)
1375 {
1376 	team->notify_peers.interval = ctx->data.u32_val;
1377 	return 0;
1378 }
1379 
1380 static void team_mcast_rejoin_count_get(struct team *team,
1381 					struct team_gsetter_ctx *ctx)
1382 {
1383 	ctx->data.u32_val = team->mcast_rejoin.count;
1384 }
1385 
1386 static int team_mcast_rejoin_count_set(struct team *team,
1387 				       struct team_gsetter_ctx *ctx)
1388 {
1389 	team->mcast_rejoin.count = ctx->data.u32_val;
1390 	return 0;
1391 }
1392 
1393 static void team_mcast_rejoin_interval_get(struct team *team,
1394 					   struct team_gsetter_ctx *ctx)
1395 {
1396 	ctx->data.u32_val = team->mcast_rejoin.interval;
1397 }
1398 
1399 static int team_mcast_rejoin_interval_set(struct team *team,
1400 					  struct team_gsetter_ctx *ctx)
1401 {
1402 	team->mcast_rejoin.interval = ctx->data.u32_val;
1403 	return 0;
1404 }
1405 
1406 static void team_port_en_option_get(struct team *team,
1407 				    struct team_gsetter_ctx *ctx)
1408 {
1409 	struct team_port *port = ctx->info->port;
1410 
1411 	ctx->data.bool_val = team_port_enabled(port);
1412 }
1413 
1414 static int team_port_en_option_set(struct team *team,
1415 				   struct team_gsetter_ctx *ctx)
1416 {
1417 	struct team_port *port = ctx->info->port;
1418 
1419 	if (ctx->data.bool_val)
1420 		team_port_enable(team, port);
1421 	else
1422 		team_port_disable(team, port);
1423 	return 0;
1424 }
1425 
1426 static void team_user_linkup_option_get(struct team *team,
1427 					struct team_gsetter_ctx *ctx)
1428 {
1429 	struct team_port *port = ctx->info->port;
1430 
1431 	ctx->data.bool_val = port->user.linkup;
1432 }
1433 
1434 static void __team_carrier_check(struct team *team);
1435 
1436 static int team_user_linkup_option_set(struct team *team,
1437 				       struct team_gsetter_ctx *ctx)
1438 {
1439 	struct team_port *port = ctx->info->port;
1440 
1441 	port->user.linkup = ctx->data.bool_val;
1442 	team_refresh_port_linkup(port);
1443 	__team_carrier_check(port->team);
1444 	return 0;
1445 }
1446 
1447 static void team_user_linkup_en_option_get(struct team *team,
1448 					   struct team_gsetter_ctx *ctx)
1449 {
1450 	struct team_port *port = ctx->info->port;
1451 
1452 	ctx->data.bool_val = port->user.linkup_enabled;
1453 }
1454 
1455 static int team_user_linkup_en_option_set(struct team *team,
1456 					  struct team_gsetter_ctx *ctx)
1457 {
1458 	struct team_port *port = ctx->info->port;
1459 
1460 	port->user.linkup_enabled = ctx->data.bool_val;
1461 	team_refresh_port_linkup(port);
1462 	__team_carrier_check(port->team);
1463 	return 0;
1464 }
1465 
1466 static void team_priority_option_get(struct team *team,
1467 				     struct team_gsetter_ctx *ctx)
1468 {
1469 	struct team_port *port = ctx->info->port;
1470 
1471 	ctx->data.s32_val = port->priority;
1472 }
1473 
1474 static int team_priority_option_set(struct team *team,
1475 				    struct team_gsetter_ctx *ctx)
1476 {
1477 	struct team_port *port = ctx->info->port;
1478 	s32 priority = ctx->data.s32_val;
1479 
1480 	if (port->priority == priority)
1481 		return 0;
1482 	port->priority = priority;
1483 	team_queue_override_port_prio_changed(team, port);
1484 	return 0;
1485 }
1486 
1487 static void team_queue_id_option_get(struct team *team,
1488 				     struct team_gsetter_ctx *ctx)
1489 {
1490 	struct team_port *port = ctx->info->port;
1491 
1492 	ctx->data.u32_val = port->queue_id;
1493 }
1494 
1495 static int team_queue_id_option_set(struct team *team,
1496 				    struct team_gsetter_ctx *ctx)
1497 {
1498 	struct team_port *port = ctx->info->port;
1499 	u16 new_queue_id = ctx->data.u32_val;
1500 
1501 	if (port->queue_id == new_queue_id)
1502 		return 0;
1503 	if (new_queue_id >= team->dev->real_num_tx_queues)
1504 		return -EINVAL;
1505 	team_queue_override_port_change_queue_id(team, port, new_queue_id);
1506 	return 0;
1507 }
1508 
1509 static const struct team_option team_options[] = {
1510 	{
1511 		.name = "mode",
1512 		.type = TEAM_OPTION_TYPE_STRING,
1513 		.getter = team_mode_option_get,
1514 		.setter = team_mode_option_set,
1515 	},
1516 	{
1517 		.name = "notify_peers_count",
1518 		.type = TEAM_OPTION_TYPE_U32,
1519 		.getter = team_notify_peers_count_get,
1520 		.setter = team_notify_peers_count_set,
1521 	},
1522 	{
1523 		.name = "notify_peers_interval",
1524 		.type = TEAM_OPTION_TYPE_U32,
1525 		.getter = team_notify_peers_interval_get,
1526 		.setter = team_notify_peers_interval_set,
1527 	},
1528 	{
1529 		.name = "mcast_rejoin_count",
1530 		.type = TEAM_OPTION_TYPE_U32,
1531 		.getter = team_mcast_rejoin_count_get,
1532 		.setter = team_mcast_rejoin_count_set,
1533 	},
1534 	{
1535 		.name = "mcast_rejoin_interval",
1536 		.type = TEAM_OPTION_TYPE_U32,
1537 		.getter = team_mcast_rejoin_interval_get,
1538 		.setter = team_mcast_rejoin_interval_set,
1539 	},
1540 	{
1541 		.name = "enabled",
1542 		.type = TEAM_OPTION_TYPE_BOOL,
1543 		.per_port = true,
1544 		.getter = team_port_en_option_get,
1545 		.setter = team_port_en_option_set,
1546 	},
1547 	{
1548 		.name = "user_linkup",
1549 		.type = TEAM_OPTION_TYPE_BOOL,
1550 		.per_port = true,
1551 		.getter = team_user_linkup_option_get,
1552 		.setter = team_user_linkup_option_set,
1553 	},
1554 	{
1555 		.name = "user_linkup_enabled",
1556 		.type = TEAM_OPTION_TYPE_BOOL,
1557 		.per_port = true,
1558 		.getter = team_user_linkup_en_option_get,
1559 		.setter = team_user_linkup_en_option_set,
1560 	},
1561 	{
1562 		.name = "priority",
1563 		.type = TEAM_OPTION_TYPE_S32,
1564 		.per_port = true,
1565 		.getter = team_priority_option_get,
1566 		.setter = team_priority_option_set,
1567 	},
1568 	{
1569 		.name = "queue_id",
1570 		.type = TEAM_OPTION_TYPE_U32,
1571 		.per_port = true,
1572 		.getter = team_queue_id_option_get,
1573 		.setter = team_queue_id_option_set,
1574 	},
1575 };
1576 
1577 
1578 static int team_init(struct net_device *dev)
1579 {
1580 	struct team *team = netdev_priv(dev);
1581 	int i;
1582 	int err;
1583 
1584 	team->dev = dev;
1585 	team_set_no_mode(team);
1586 	team->notifier_ctx = false;
1587 
1588 	team->pcpu_stats = netdev_alloc_pcpu_stats(struct team_pcpu_stats);
1589 	if (!team->pcpu_stats)
1590 		return -ENOMEM;
1591 
1592 	for (i = 0; i < TEAM_PORT_HASHENTRIES; i++)
1593 		INIT_HLIST_HEAD(&team->en_port_hlist[i]);
1594 	INIT_LIST_HEAD(&team->port_list);
1595 	err = team_queue_override_init(team);
1596 	if (err)
1597 		goto err_team_queue_override_init;
1598 
1599 	team_adjust_ops(team);
1600 
1601 	INIT_LIST_HEAD(&team->option_list);
1602 	INIT_LIST_HEAD(&team->option_inst_list);
1603 
1604 	team_notify_peers_init(team);
1605 	team_mcast_rejoin_init(team);
1606 
1607 	err = team_options_register(team, team_options, ARRAY_SIZE(team_options));
1608 	if (err)
1609 		goto err_options_register;
1610 	netif_carrier_off(dev);
1611 
1612 	netdev_lockdep_set_classes(dev);
1613 
1614 	return 0;
1615 
1616 err_options_register:
1617 	team_mcast_rejoin_fini(team);
1618 	team_notify_peers_fini(team);
1619 	team_queue_override_fini(team);
1620 err_team_queue_override_init:
1621 	free_percpu(team->pcpu_stats);
1622 
1623 	return err;
1624 }
1625 
1626 static void team_uninit(struct net_device *dev)
1627 {
1628 	struct team *team = netdev_priv(dev);
1629 	struct team_port *port;
1630 	struct team_port *tmp;
1631 
1632 	ASSERT_RTNL();
1633 
1634 	list_for_each_entry_safe(port, tmp, &team->port_list, list)
1635 		team_port_del(team, port->dev);
1636 
1637 	__team_change_mode(team, NULL); /* cleanup */
1638 	__team_options_unregister(team, team_options, ARRAY_SIZE(team_options));
1639 	team_mcast_rejoin_fini(team);
1640 	team_notify_peers_fini(team);
1641 	team_queue_override_fini(team);
1642 	netdev_change_features(dev);
1643 }
1644 
1645 static void team_destructor(struct net_device *dev)
1646 {
1647 	struct team *team = netdev_priv(dev);
1648 
1649 	free_percpu(team->pcpu_stats);
1650 }
1651 
1652 static int team_open(struct net_device *dev)
1653 {
1654 	return 0;
1655 }
1656 
1657 static int team_close(struct net_device *dev)
1658 {
1659 	struct team *team = netdev_priv(dev);
1660 	struct team_port *port;
1661 
1662 	list_for_each_entry(port, &team->port_list, list) {
1663 		dev_uc_unsync(port->dev, dev);
1664 		dev_mc_unsync(port->dev, dev);
1665 	}
1666 
1667 	return 0;
1668 }
1669 
1670 /*
1671  * note: already called with rcu_read_lock
1672  */
1673 static netdev_tx_t team_xmit(struct sk_buff *skb, struct net_device *dev)
1674 {
1675 	struct team *team = netdev_priv(dev);
1676 	bool tx_success;
1677 	unsigned int len = skb->len;
1678 
1679 	tx_success = team_queue_override_transmit(team, skb);
1680 	if (!tx_success)
1681 		tx_success = team->ops.transmit(team, skb);
1682 	if (tx_success) {
1683 		struct team_pcpu_stats *pcpu_stats;
1684 
1685 		pcpu_stats = this_cpu_ptr(team->pcpu_stats);
1686 		u64_stats_update_begin(&pcpu_stats->syncp);
1687 		u64_stats_inc(&pcpu_stats->tx_packets);
1688 		u64_stats_add(&pcpu_stats->tx_bytes, len);
1689 		u64_stats_update_end(&pcpu_stats->syncp);
1690 	} else {
1691 		this_cpu_inc(team->pcpu_stats->tx_dropped);
1692 	}
1693 
1694 	return NETDEV_TX_OK;
1695 }
1696 
1697 static u16 team_select_queue(struct net_device *dev, struct sk_buff *skb,
1698 			     struct net_device *sb_dev)
1699 {
1700 	/*
1701 	 * This helper function exists to help dev_pick_tx get the correct
1702 	 * destination queue.  Using a helper function skips a call to
1703 	 * skb_tx_hash and will put the skbs in the queue we expect on their
1704 	 * way down to the team driver.
1705 	 */
1706 	u16 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;
1707 
1708 	/*
1709 	 * Save the original txq to restore before passing to the driver
1710 	 */
1711 	qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb->queue_mapping;
1712 
1713 	if (unlikely(txq >= dev->real_num_tx_queues)) {
1714 		do {
1715 			txq -= dev->real_num_tx_queues;
1716 		} while (txq >= dev->real_num_tx_queues);
1717 	}
1718 	return txq;
1719 }
1720 
1721 static void team_change_rx_flags(struct net_device *dev, int change)
1722 {
1723 	struct team *team = netdev_priv(dev);
1724 	struct team_port *port;
1725 	int inc;
1726 
1727 	ASSERT_RTNL();
1728 
1729 	list_for_each_entry(port, &team->port_list, list) {
1730 		if (change & IFF_PROMISC) {
1731 			inc = dev->flags & IFF_PROMISC ? 1 : -1;
1732 			dev_set_promiscuity(port->dev, inc);
1733 		}
1734 		if (change & IFF_ALLMULTI) {
1735 			inc = dev->flags & IFF_ALLMULTI ? 1 : -1;
1736 			dev_set_allmulti(port->dev, inc);
1737 		}
1738 	}
1739 }
1740 
1741 static void team_set_rx_mode(struct net_device *dev)
1742 {
1743 	struct team *team = netdev_priv(dev);
1744 	struct team_port *port;
1745 
1746 	rcu_read_lock();
1747 	list_for_each_entry_rcu(port, &team->port_list, list) {
1748 		dev_uc_sync_multiple(port->dev, dev);
1749 		dev_mc_sync_multiple(port->dev, dev);
1750 	}
1751 	rcu_read_unlock();
1752 }
1753 
1754 static int team_set_mac_address(struct net_device *dev, void *p)
1755 {
1756 	struct sockaddr *addr = p;
1757 	struct team *team = netdev_priv(dev);
1758 	struct team_port *port;
1759 
1760 	ASSERT_RTNL();
1761 
1762 	if (dev->type == ARPHRD_ETHER && !is_valid_ether_addr(addr->sa_data))
1763 		return -EADDRNOTAVAIL;
1764 	dev_addr_set(dev, addr->sa_data);
1765 	list_for_each_entry(port, &team->port_list, list)
1766 		if (team->ops.port_change_dev_addr)
1767 			team->ops.port_change_dev_addr(team, port);
1768 	return 0;
1769 }
1770 
1771 static int team_change_mtu(struct net_device *dev, int new_mtu)
1772 {
1773 	struct team *team = netdev_priv(dev);
1774 	struct team_port *port;
1775 	int err;
1776 
1777 	ASSERT_RTNL();
1778 
1779 	team->port_mtu_change_allowed = true;
1780 	list_for_each_entry(port, &team->port_list, list) {
1781 		err = dev_set_mtu(port->dev, new_mtu);
1782 		if (err) {
1783 			netdev_err(dev, "Device %s failed to change mtu",
1784 				   port->dev->name);
1785 			goto unwind;
1786 		}
1787 	}
1788 	team->port_mtu_change_allowed = false;
1789 
1790 	WRITE_ONCE(dev->mtu, new_mtu);
1791 
1792 	return 0;
1793 
1794 unwind:
1795 	list_for_each_entry_continue_reverse(port, &team->port_list, list)
1796 		dev_set_mtu(port->dev, dev->mtu);
1797 	team->port_mtu_change_allowed = false;
1798 
1799 	return err;
1800 }
1801 
1802 static void
1803 team_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
1804 {
1805 	struct team *team = netdev_priv(dev);
1806 	struct team_pcpu_stats *p;
1807 	u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
1808 	u32 rx_dropped = 0, tx_dropped = 0, rx_nohandler = 0;
1809 	unsigned int start;
1810 	int i;
1811 
1812 	for_each_possible_cpu(i) {
1813 		p = per_cpu_ptr(team->pcpu_stats, i);
1814 		do {
1815 			start = u64_stats_fetch_begin(&p->syncp);
1816 			rx_packets	= u64_stats_read(&p->rx_packets);
1817 			rx_bytes	= u64_stats_read(&p->rx_bytes);
1818 			rx_multicast	= u64_stats_read(&p->rx_multicast);
1819 			tx_packets	= u64_stats_read(&p->tx_packets);
1820 			tx_bytes	= u64_stats_read(&p->tx_bytes);
1821 		} while (u64_stats_fetch_retry(&p->syncp, start));
1822 
1823 		stats->rx_packets	+= rx_packets;
1824 		stats->rx_bytes		+= rx_bytes;
1825 		stats->multicast	+= rx_multicast;
1826 		stats->tx_packets	+= tx_packets;
1827 		stats->tx_bytes		+= tx_bytes;
1828 		/*
1829 		 * rx_dropped, tx_dropped & rx_nohandler are u32,
1830 		 * updated without syncp protection.
1831 		 */
1832 		rx_dropped	+= READ_ONCE(p->rx_dropped);
1833 		tx_dropped	+= READ_ONCE(p->tx_dropped);
1834 		rx_nohandler	+= READ_ONCE(p->rx_nohandler);
1835 	}
1836 	stats->rx_dropped	= rx_dropped;
1837 	stats->tx_dropped	= tx_dropped;
1838 	stats->rx_nohandler	= rx_nohandler;
1839 }
1840 
1841 static int team_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
1842 {
1843 	struct team *team = netdev_priv(dev);
1844 	struct team_port *port;
1845 	int err;
1846 
1847 	ASSERT_RTNL();
1848 
1849 	list_for_each_entry(port, &team->port_list, list) {
1850 		err = vlan_vid_add(port->dev, proto, vid);
1851 		if (err)
1852 			goto unwind;
1853 	}
1854 
1855 	return 0;
1856 
1857 unwind:
1858 	list_for_each_entry_continue_reverse(port, &team->port_list, list)
1859 		vlan_vid_del(port->dev, proto, vid);
1860 
1861 	return err;
1862 }
1863 
1864 static int team_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, u16 vid)
1865 {
1866 	struct team *team = netdev_priv(dev);
1867 	struct team_port *port;
1868 
1869 	ASSERT_RTNL();
1870 
1871 	list_for_each_entry(port, &team->port_list, list)
1872 		vlan_vid_del(port->dev, proto, vid);
1873 
1874 	return 0;
1875 }
1876 
1877 #ifdef CONFIG_NET_POLL_CONTROLLER
1878 static void team_poll_controller(struct net_device *dev)
1879 {
1880 }
1881 
1882 static void __team_netpoll_cleanup(struct team *team)
1883 {
1884 	struct team_port *port;
1885 
1886 	list_for_each_entry(port, &team->port_list, list)
1887 		team_port_disable_netpoll(port);
1888 }
1889 
1890 static void team_netpoll_cleanup(struct net_device *dev)
1891 {
1892 	struct team *team = netdev_priv(dev);
1893 
1894 	ASSERT_RTNL();
1895 
1896 	__team_netpoll_cleanup(team);
1897 }
1898 
1899 static int team_netpoll_setup(struct net_device *dev)
1900 {
1901 	struct team *team = netdev_priv(dev);
1902 	struct team_port *port;
1903 	int err = 0;
1904 
1905 	ASSERT_RTNL();
1906 
1907 	list_for_each_entry(port, &team->port_list, list) {
1908 		err = __team_port_enable_netpoll(port);
1909 		if (err) {
1910 			__team_netpoll_cleanup(team);
1911 			break;
1912 		}
1913 	}
1914 	return err;
1915 }
1916 #endif
1917 
1918 static int team_add_slave(struct net_device *dev, struct net_device *port_dev,
1919 			  struct netlink_ext_ack *extack)
1920 {
1921 	struct team *team = netdev_priv(dev);
1922 
1923 	ASSERT_RTNL();
1924 
1925 	return team_port_add(team, port_dev, extack);
1926 }
1927 
1928 static int team_del_slave(struct net_device *dev, struct net_device *port_dev)
1929 {
1930 	struct team *team = netdev_priv(dev);
1931 
1932 	ASSERT_RTNL();
1933 
1934 	return team_port_del(team, port_dev);
1935 }
1936 
1937 static netdev_features_t team_fix_features(struct net_device *dev,
1938 					   netdev_features_t features)
1939 {
1940 	struct team_port *port;
1941 	struct team *team = netdev_priv(dev);
1942 	netdev_features_t mask;
1943 
1944 	mask = features;
1945 	features = netdev_base_features(features);
1946 
1947 	rcu_read_lock();
1948 	list_for_each_entry_rcu(port, &team->port_list, list) {
1949 		features = netdev_increment_features(features,
1950 						     port->dev->features,
1951 						     mask);
1952 	}
1953 	rcu_read_unlock();
1954 
1955 	features = netdev_add_tso_features(features, mask);
1956 
1957 	return features;
1958 }
1959 
1960 static int team_change_carrier(struct net_device *dev, bool new_carrier)
1961 {
1962 	struct team *team = netdev_priv(dev);
1963 
1964 	team->user_carrier_enabled = true;
1965 
1966 	if (new_carrier)
1967 		netif_carrier_on(dev);
1968 	else
1969 		netif_carrier_off(dev);
1970 	return 0;
1971 }
1972 
1973 static const struct net_device_ops team_netdev_ops = {
1974 	.ndo_init		= team_init,
1975 	.ndo_uninit		= team_uninit,
1976 	.ndo_open		= team_open,
1977 	.ndo_stop		= team_close,
1978 	.ndo_start_xmit		= team_xmit,
1979 	.ndo_select_queue	= team_select_queue,
1980 	.ndo_change_rx_flags	= team_change_rx_flags,
1981 	.ndo_set_rx_mode	= team_set_rx_mode,
1982 	.ndo_set_mac_address	= team_set_mac_address,
1983 	.ndo_change_mtu		= team_change_mtu,
1984 	.ndo_get_stats64	= team_get_stats64,
1985 	.ndo_vlan_rx_add_vid	= team_vlan_rx_add_vid,
1986 	.ndo_vlan_rx_kill_vid	= team_vlan_rx_kill_vid,
1987 #ifdef CONFIG_NET_POLL_CONTROLLER
1988 	.ndo_poll_controller	= team_poll_controller,
1989 	.ndo_netpoll_setup	= team_netpoll_setup,
1990 	.ndo_netpoll_cleanup	= team_netpoll_cleanup,
1991 #endif
1992 	.ndo_add_slave		= team_add_slave,
1993 	.ndo_del_slave		= team_del_slave,
1994 	.ndo_fix_features	= team_fix_features,
1995 	.ndo_change_carrier     = team_change_carrier,
1996 	.ndo_features_check	= passthru_features_check,
1997 };
1998 
1999 /***********************
2000  * ethtool interface
2001  ***********************/
2002 
2003 static void team_ethtool_get_drvinfo(struct net_device *dev,
2004 				     struct ethtool_drvinfo *drvinfo)
2005 {
2006 	strscpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
2007 }
2008 
2009 static int team_ethtool_get_link_ksettings(struct net_device *dev,
2010 					   struct ethtool_link_ksettings *cmd)
2011 {
2012 	struct team *team= netdev_priv(dev);
2013 	unsigned long speed = 0;
2014 	struct team_port *port;
2015 
2016 	cmd->base.duplex = DUPLEX_UNKNOWN;
2017 	cmd->base.port = PORT_OTHER;
2018 
2019 	rcu_read_lock();
2020 	list_for_each_entry_rcu(port, &team->port_list, list) {
2021 		if (team_port_txable(port)) {
2022 			if (port->state.speed != SPEED_UNKNOWN)
2023 				speed += port->state.speed;
2024 			if (cmd->base.duplex == DUPLEX_UNKNOWN &&
2025 			    port->state.duplex != DUPLEX_UNKNOWN)
2026 				cmd->base.duplex = port->state.duplex;
2027 		}
2028 	}
2029 	rcu_read_unlock();
2030 
2031 	cmd->base.speed = speed ? : SPEED_UNKNOWN;
2032 
2033 	return 0;
2034 }
2035 
2036 static const struct ethtool_ops team_ethtool_ops = {
2037 	.get_drvinfo		= team_ethtool_get_drvinfo,
2038 	.get_link		= ethtool_op_get_link,
2039 	.get_link_ksettings	= team_ethtool_get_link_ksettings,
2040 };
2041 
2042 /***********************
2043  * rt netlink interface
2044  ***********************/
2045 
2046 static void team_setup_by_port(struct net_device *dev,
2047 			       struct net_device *port_dev)
2048 {
2049 	struct team *team = netdev_priv(dev);
2050 
2051 	if (port_dev->type == ARPHRD_ETHER)
2052 		dev->header_ops	= team->header_ops_cache;
2053 	else
2054 		dev->header_ops	= port_dev->header_ops;
2055 	dev->type = port_dev->type;
2056 	dev->hard_header_len = port_dev->hard_header_len;
2057 	dev->needed_headroom = port_dev->needed_headroom;
2058 	dev->addr_len = port_dev->addr_len;
2059 	dev->mtu = port_dev->mtu;
2060 	memcpy(dev->broadcast, port_dev->broadcast, port_dev->addr_len);
2061 	eth_hw_addr_inherit(dev, port_dev);
2062 
2063 	if (port_dev->flags & IFF_POINTOPOINT) {
2064 		dev->flags &= ~(IFF_BROADCAST | IFF_MULTICAST);
2065 		dev->flags |= (IFF_POINTOPOINT | IFF_NOARP);
2066 	} else if ((port_dev->flags & (IFF_BROADCAST | IFF_MULTICAST)) ==
2067 		    (IFF_BROADCAST | IFF_MULTICAST)) {
2068 		dev->flags |= (IFF_BROADCAST | IFF_MULTICAST);
2069 		dev->flags &= ~(IFF_POINTOPOINT | IFF_NOARP);
2070 	}
2071 }
2072 
2073 static int team_dev_type_check_change(struct net_device *dev,
2074 				      struct net_device *port_dev)
2075 {
2076 	struct team *team = netdev_priv(dev);
2077 	char *portname = port_dev->name;
2078 	int err;
2079 
2080 	if (dev->type == port_dev->type)
2081 		return 0;
2082 	if (!list_empty(&team->port_list)) {
2083 		netdev_err(dev, "Device %s is of different type\n", portname);
2084 		return -EBUSY;
2085 	}
2086 	err = call_netdevice_notifiers(NETDEV_PRE_TYPE_CHANGE, dev);
2087 	err = notifier_to_errno(err);
2088 	if (err) {
2089 		netdev_err(dev, "Refused to change device type\n");
2090 		return err;
2091 	}
2092 	dev_uc_flush(dev);
2093 	dev_mc_flush(dev);
2094 	team_setup_by_port(dev, port_dev);
2095 	call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, dev);
2096 	return 0;
2097 }
2098 
2099 static void team_setup(struct net_device *dev)
2100 {
2101 	struct team *team = netdev_priv(dev);
2102 
2103 	ether_setup(dev);
2104 	dev->max_mtu = ETH_MAX_MTU;
2105 	team->header_ops_cache = dev->header_ops;
2106 
2107 	dev->netdev_ops = &team_netdev_ops;
2108 	dev->ethtool_ops = &team_ethtool_ops;
2109 	dev->needs_free_netdev = true;
2110 	dev->priv_destructor = team_destructor;
2111 	dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
2112 	dev->priv_flags |= IFF_NO_QUEUE;
2113 	dev->priv_flags |= IFF_TEAM;
2114 
2115 	/*
2116 	 * Indicate we support unicast address filtering. That way core won't
2117 	 * bring us to promisc mode in case a unicast addr is added.
2118 	 * Let this up to underlay drivers.
2119 	 */
2120 	dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
2121 	dev->lltx = true;
2122 
2123 	/* Don't allow team devices to change network namespaces. */
2124 	dev->netns_immutable = true;
2125 
2126 	dev->features |= NETIF_F_GRO;
2127 
2128 	dev->hw_features = MASTER_UPPER_DEV_VLAN_FEATURES |
2129 			   NETIF_F_HW_VLAN_CTAG_RX |
2130 			   NETIF_F_HW_VLAN_CTAG_FILTER |
2131 			   NETIF_F_HW_VLAN_STAG_RX |
2132 			   NETIF_F_HW_VLAN_STAG_FILTER;
2133 
2134 	dev->hw_features |= NETIF_F_GSO_ENCAP_ALL;
2135 	dev->features |= dev->hw_features;
2136 	dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
2137 }
2138 
2139 static int team_newlink(struct net_device *dev,
2140 			struct rtnl_newlink_params *params,
2141 			struct netlink_ext_ack *extack)
2142 {
2143 	struct nlattr **tb = params->tb;
2144 
2145 	if (tb[IFLA_ADDRESS] == NULL)
2146 		eth_hw_addr_random(dev);
2147 
2148 	return register_netdevice(dev);
2149 }
2150 
2151 static int team_validate(struct nlattr *tb[], struct nlattr *data[],
2152 			 struct netlink_ext_ack *extack)
2153 {
2154 	if (tb[IFLA_ADDRESS]) {
2155 		if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
2156 			return -EINVAL;
2157 		if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
2158 			return -EADDRNOTAVAIL;
2159 	}
2160 	return 0;
2161 }
2162 
2163 static unsigned int team_get_num_tx_queues(void)
2164 {
2165 	return TEAM_DEFAULT_NUM_TX_QUEUES;
2166 }
2167 
2168 static unsigned int team_get_num_rx_queues(void)
2169 {
2170 	return TEAM_DEFAULT_NUM_RX_QUEUES;
2171 }
2172 
2173 static struct rtnl_link_ops team_link_ops __read_mostly = {
2174 	.kind			= DRV_NAME,
2175 	.priv_size		= sizeof(struct team),
2176 	.setup			= team_setup,
2177 	.newlink		= team_newlink,
2178 	.validate		= team_validate,
2179 	.get_num_tx_queues	= team_get_num_tx_queues,
2180 	.get_num_rx_queues	= team_get_num_rx_queues,
2181 };
2182 
2183 
2184 /***********************************
2185  * Generic netlink custom interface
2186  ***********************************/
2187 
2188 static struct genl_family team_nl_family;
2189 
2190 int team_nl_noop_doit(struct sk_buff *skb, struct genl_info *info)
2191 {
2192 	struct sk_buff *msg;
2193 	void *hdr;
2194 	int err;
2195 
2196 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2197 	if (!msg)
2198 		return -ENOMEM;
2199 
2200 	hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
2201 			  &team_nl_family, 0, TEAM_CMD_NOOP);
2202 	if (!hdr) {
2203 		err = -EMSGSIZE;
2204 		goto err_msg_put;
2205 	}
2206 
2207 	genlmsg_end(msg, hdr);
2208 
2209 	return genlmsg_unicast(genl_info_net(info), msg, info->snd_portid);
2210 
2211 err_msg_put:
2212 	nlmsg_free(msg);
2213 
2214 	return err;
2215 }
2216 
2217 /*
2218  * Netlink cmd functions should be locked by following two functions.
2219  * Since dev gets held here, that ensures dev won't disappear in between.
2220  */
2221 static struct team *team_nl_team_get(struct genl_info *info)
2222 {
2223 	struct net *net = genl_info_net(info);
2224 	struct net_device *dev;
2225 	int ifindex;
2226 
2227 	ASSERT_RTNL();
2228 
2229 	if (!info->attrs[TEAM_ATTR_TEAM_IFINDEX])
2230 		return NULL;
2231 
2232 	ifindex = nla_get_u32(info->attrs[TEAM_ATTR_TEAM_IFINDEX]);
2233 	dev = dev_get_by_index(net, ifindex);
2234 	if (!dev || dev->netdev_ops != &team_netdev_ops) {
2235 		dev_put(dev);
2236 		return NULL;
2237 	}
2238 
2239 	return netdev_priv(dev);
2240 }
2241 
2242 static void team_nl_team_put(struct team *team)
2243 {
2244 	dev_put(team->dev);
2245 }
2246 
2247 typedef int team_nl_send_func_t(struct sk_buff *skb,
2248 				struct team *team, u32 portid);
2249 
2250 static int team_nl_send_unicast(struct sk_buff *skb, struct team *team, u32 portid)
2251 {
2252 	return genlmsg_unicast(dev_net(team->dev), skb, portid);
2253 }
2254 
2255 static int team_nl_fill_one_option_get(struct sk_buff *skb, struct team *team,
2256 				       struct team_option_inst *opt_inst)
2257 {
2258 	struct nlattr *option_item;
2259 	struct team_option *option = opt_inst->option;
2260 	struct team_option_inst_info *opt_inst_info = &opt_inst->info;
2261 	struct team_gsetter_ctx ctx;
2262 	int err;
2263 
2264 	ctx.info = opt_inst_info;
2265 	err = team_option_get(team, opt_inst, &ctx);
2266 	if (err)
2267 		return err;
2268 
2269 	option_item = nla_nest_start_noflag(skb, TEAM_ATTR_ITEM_OPTION);
2270 	if (!option_item)
2271 		return -EMSGSIZE;
2272 
2273 	if (nla_put_string(skb, TEAM_ATTR_OPTION_NAME, option->name))
2274 		goto nest_cancel;
2275 	if (opt_inst_info->port &&
2276 	    nla_put_u32(skb, TEAM_ATTR_OPTION_PORT_IFINDEX,
2277 			opt_inst_info->port->dev->ifindex))
2278 		goto nest_cancel;
2279 	if (opt_inst->option->array_size &&
2280 	    nla_put_u32(skb, TEAM_ATTR_OPTION_ARRAY_INDEX,
2281 			opt_inst_info->array_index))
2282 		goto nest_cancel;
2283 
2284 	switch (option->type) {
2285 	case TEAM_OPTION_TYPE_U32:
2286 		if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_U32))
2287 			goto nest_cancel;
2288 		if (nla_put_u32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.u32_val))
2289 			goto nest_cancel;
2290 		break;
2291 	case TEAM_OPTION_TYPE_STRING:
2292 		if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_STRING))
2293 			goto nest_cancel;
2294 		if (nla_put_string(skb, TEAM_ATTR_OPTION_DATA,
2295 				   ctx.data.str_val))
2296 			goto nest_cancel;
2297 		break;
2298 	case TEAM_OPTION_TYPE_BINARY:
2299 		if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_BINARY))
2300 			goto nest_cancel;
2301 		if (nla_put(skb, TEAM_ATTR_OPTION_DATA, ctx.data.bin_val.len,
2302 			    ctx.data.bin_val.ptr))
2303 			goto nest_cancel;
2304 		break;
2305 	case TEAM_OPTION_TYPE_BOOL:
2306 		if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_FLAG))
2307 			goto nest_cancel;
2308 		if (ctx.data.bool_val &&
2309 		    nla_put_flag(skb, TEAM_ATTR_OPTION_DATA))
2310 			goto nest_cancel;
2311 		break;
2312 	case TEAM_OPTION_TYPE_S32:
2313 		if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_S32))
2314 			goto nest_cancel;
2315 		if (nla_put_s32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.s32_val))
2316 			goto nest_cancel;
2317 		break;
2318 	default:
2319 		BUG();
2320 	}
2321 	if (opt_inst->removed && nla_put_flag(skb, TEAM_ATTR_OPTION_REMOVED))
2322 		goto nest_cancel;
2323 	if (opt_inst->changed) {
2324 		if (nla_put_flag(skb, TEAM_ATTR_OPTION_CHANGED))
2325 			goto nest_cancel;
2326 		opt_inst->changed = false;
2327 	}
2328 	nla_nest_end(skb, option_item);
2329 	return 0;
2330 
2331 nest_cancel:
2332 	nla_nest_cancel(skb, option_item);
2333 	return -EMSGSIZE;
2334 }
2335 
2336 static int __send_and_alloc_skb(struct sk_buff **pskb,
2337 				struct team *team, u32 portid,
2338 				team_nl_send_func_t *send_func)
2339 {
2340 	int err;
2341 
2342 	if (*pskb) {
2343 		err = send_func(*pskb, team, portid);
2344 		if (err)
2345 			return err;
2346 	}
2347 	*pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
2348 	if (!*pskb)
2349 		return -ENOMEM;
2350 	return 0;
2351 }
2352 
2353 static int team_nl_send_options_get(struct team *team, u32 portid, u32 seq,
2354 				    int flags, team_nl_send_func_t *send_func,
2355 				    struct list_head *sel_opt_inst_list)
2356 {
2357 	struct nlattr *option_list;
2358 	struct nlmsghdr *nlh;
2359 	void *hdr;
2360 	struct team_option_inst *opt_inst;
2361 	int err;
2362 	struct sk_buff *skb = NULL;
2363 	bool incomplete;
2364 	int i;
2365 
2366 	opt_inst = list_first_entry(sel_opt_inst_list,
2367 				    struct team_option_inst, tmp_list);
2368 
2369 start_again:
2370 	err = __send_and_alloc_skb(&skb, team, portid, send_func);
2371 	if (err)
2372 		return err;
2373 
2374 	hdr = genlmsg_put(skb, portid, seq, &team_nl_family, flags | NLM_F_MULTI,
2375 			  TEAM_CMD_OPTIONS_GET);
2376 	if (!hdr) {
2377 		nlmsg_free(skb);
2378 		return -EMSGSIZE;
2379 	}
2380 
2381 	if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
2382 		goto nla_put_failure;
2383 	option_list = nla_nest_start_noflag(skb, TEAM_ATTR_LIST_OPTION);
2384 	if (!option_list)
2385 		goto nla_put_failure;
2386 
2387 	i = 0;
2388 	incomplete = false;
2389 	list_for_each_entry_from(opt_inst, sel_opt_inst_list, tmp_list) {
2390 		err = team_nl_fill_one_option_get(skb, team, opt_inst);
2391 		if (err) {
2392 			if (err == -EMSGSIZE) {
2393 				if (!i)
2394 					goto errout;
2395 				incomplete = true;
2396 				break;
2397 			}
2398 			goto errout;
2399 		}
2400 		i++;
2401 	}
2402 
2403 	nla_nest_end(skb, option_list);
2404 	genlmsg_end(skb, hdr);
2405 	if (incomplete)
2406 		goto start_again;
2407 
2408 send_done:
2409 	nlh = nlmsg_put(skb, portid, seq, NLMSG_DONE, 0, flags | NLM_F_MULTI);
2410 	if (!nlh) {
2411 		err = __send_and_alloc_skb(&skb, team, portid, send_func);
2412 		if (err)
2413 			return err;
2414 		goto send_done;
2415 	}
2416 
2417 	return send_func(skb, team, portid);
2418 
2419 nla_put_failure:
2420 	err = -EMSGSIZE;
2421 errout:
2422 	nlmsg_free(skb);
2423 	return err;
2424 }
2425 
2426 int team_nl_options_get_doit(struct sk_buff *skb, struct genl_info *info)
2427 {
2428 	struct team *team;
2429 	struct team_option_inst *opt_inst;
2430 	int err;
2431 	LIST_HEAD(sel_opt_inst_list);
2432 
2433 	rtnl_lock();
2434 
2435 	team = team_nl_team_get(info);
2436 	if (!team) {
2437 		err = -EINVAL;
2438 		goto rtnl_unlock;
2439 	}
2440 
2441 	list_for_each_entry(opt_inst, &team->option_inst_list, list)
2442 		list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
2443 	err = team_nl_send_options_get(team, info->snd_portid, info->snd_seq,
2444 				       NLM_F_ACK, team_nl_send_unicast,
2445 				       &sel_opt_inst_list);
2446 
2447 	team_nl_team_put(team);
2448 
2449 rtnl_unlock:
2450 	rtnl_unlock();
2451 
2452 	return err;
2453 }
2454 
2455 static int team_nl_send_event_options_get(struct team *team,
2456 					  struct list_head *sel_opt_inst_list);
2457 
2458 int team_nl_options_set_doit(struct sk_buff *skb, struct genl_info *info)
2459 {
2460 	struct team *team;
2461 	int err = 0;
2462 	int i;
2463 	struct nlattr *nl_option;
2464 
2465 	rtnl_lock();
2466 
2467 	team = team_nl_team_get(info);
2468 	if (!team) {
2469 		err = -EINVAL;
2470 		goto rtnl_unlock;
2471 	}
2472 
2473 	err = -EINVAL;
2474 	if (!info->attrs[TEAM_ATTR_LIST_OPTION]) {
2475 		err = -EINVAL;
2476 		goto team_put;
2477 	}
2478 
2479 	nla_for_each_nested(nl_option, info->attrs[TEAM_ATTR_LIST_OPTION], i) {
2480 		struct nlattr *opt_attrs[TEAM_ATTR_OPTION_MAX + 1];
2481 		struct nlattr *attr;
2482 		struct nlattr *attr_data;
2483 		LIST_HEAD(opt_inst_list);
2484 		enum team_option_type opt_type;
2485 		int opt_port_ifindex = 0; /* != 0 for per-port options */
2486 		u32 opt_array_index = 0;
2487 		bool opt_is_array = false;
2488 		struct team_option_inst *opt_inst;
2489 		char *opt_name;
2490 		bool opt_found = false;
2491 
2492 		if (nla_type(nl_option) != TEAM_ATTR_ITEM_OPTION) {
2493 			err = -EINVAL;
2494 			goto team_put;
2495 		}
2496 		err = nla_parse_nested_deprecated(opt_attrs,
2497 						  TEAM_ATTR_OPTION_MAX,
2498 						  nl_option,
2499 						  team_attr_option_nl_policy,
2500 						  info->extack);
2501 		if (err)
2502 			goto team_put;
2503 		if (!opt_attrs[TEAM_ATTR_OPTION_NAME] ||
2504 		    !opt_attrs[TEAM_ATTR_OPTION_TYPE]) {
2505 			err = -EINVAL;
2506 			goto team_put;
2507 		}
2508 		switch (nla_get_u8(opt_attrs[TEAM_ATTR_OPTION_TYPE])) {
2509 		case NLA_U32:
2510 			opt_type = TEAM_OPTION_TYPE_U32;
2511 			break;
2512 		case NLA_STRING:
2513 			opt_type = TEAM_OPTION_TYPE_STRING;
2514 			break;
2515 		case NLA_BINARY:
2516 			opt_type = TEAM_OPTION_TYPE_BINARY;
2517 			break;
2518 		case NLA_FLAG:
2519 			opt_type = TEAM_OPTION_TYPE_BOOL;
2520 			break;
2521 		case NLA_S32:
2522 			opt_type = TEAM_OPTION_TYPE_S32;
2523 			break;
2524 		default:
2525 			goto team_put;
2526 		}
2527 
2528 		attr_data = opt_attrs[TEAM_ATTR_OPTION_DATA];
2529 		if (opt_type != TEAM_OPTION_TYPE_BOOL && !attr_data) {
2530 			err = -EINVAL;
2531 			goto team_put;
2532 		}
2533 
2534 		opt_name = nla_data(opt_attrs[TEAM_ATTR_OPTION_NAME]);
2535 		attr = opt_attrs[TEAM_ATTR_OPTION_PORT_IFINDEX];
2536 		if (attr)
2537 			opt_port_ifindex = nla_get_u32(attr);
2538 
2539 		attr = opt_attrs[TEAM_ATTR_OPTION_ARRAY_INDEX];
2540 		if (attr) {
2541 			opt_is_array = true;
2542 			opt_array_index = nla_get_u32(attr);
2543 		}
2544 
2545 		list_for_each_entry(opt_inst, &team->option_inst_list, list) {
2546 			struct team_option *option = opt_inst->option;
2547 			struct team_gsetter_ctx ctx;
2548 			struct team_option_inst_info *opt_inst_info;
2549 			int tmp_ifindex;
2550 
2551 			opt_inst_info = &opt_inst->info;
2552 			tmp_ifindex = opt_inst_info->port ?
2553 				      opt_inst_info->port->dev->ifindex : 0;
2554 			if (option->type != opt_type ||
2555 			    strcmp(option->name, opt_name) ||
2556 			    tmp_ifindex != opt_port_ifindex ||
2557 			    (option->array_size && !opt_is_array) ||
2558 			    opt_inst_info->array_index != opt_array_index)
2559 				continue;
2560 			opt_found = true;
2561 			ctx.info = opt_inst_info;
2562 			switch (opt_type) {
2563 			case TEAM_OPTION_TYPE_U32:
2564 				ctx.data.u32_val = nla_get_u32(attr_data);
2565 				break;
2566 			case TEAM_OPTION_TYPE_STRING:
2567 				if (nla_len(attr_data) > TEAM_STRING_MAX_LEN ||
2568 				    !memchr(nla_data(attr_data), '\0',
2569 					    nla_len(attr_data))) {
2570 					err = -EINVAL;
2571 					goto team_put;
2572 				}
2573 				ctx.data.str_val = nla_data(attr_data);
2574 				break;
2575 			case TEAM_OPTION_TYPE_BINARY:
2576 				ctx.data.bin_val.len = nla_len(attr_data);
2577 				ctx.data.bin_val.ptr = nla_data(attr_data);
2578 				break;
2579 			case TEAM_OPTION_TYPE_BOOL:
2580 				ctx.data.bool_val = attr_data ? true : false;
2581 				break;
2582 			case TEAM_OPTION_TYPE_S32:
2583 				ctx.data.s32_val = nla_get_s32(attr_data);
2584 				break;
2585 			default:
2586 				BUG();
2587 			}
2588 			err = team_option_set(team, opt_inst, &ctx);
2589 			if (err)
2590 				goto team_put;
2591 			opt_inst->changed = true;
2592 			list_add(&opt_inst->tmp_list, &opt_inst_list);
2593 		}
2594 		if (!opt_found) {
2595 			err = -ENOENT;
2596 			goto team_put;
2597 		}
2598 
2599 		err = team_nl_send_event_options_get(team, &opt_inst_list);
2600 		if (err)
2601 			break;
2602 	}
2603 
2604 team_put:
2605 	team_nl_team_put(team);
2606 rtnl_unlock:
2607 	rtnl_unlock();
2608 	return err;
2609 }
2610 
2611 static int team_nl_fill_one_port_get(struct sk_buff *skb,
2612 				     struct team_port *port)
2613 {
2614 	struct nlattr *port_item;
2615 
2616 	port_item = nla_nest_start_noflag(skb, TEAM_ATTR_ITEM_PORT);
2617 	if (!port_item)
2618 		goto nest_cancel;
2619 	if (nla_put_u32(skb, TEAM_ATTR_PORT_IFINDEX, port->dev->ifindex))
2620 		goto nest_cancel;
2621 	if (port->changed) {
2622 		if (nla_put_flag(skb, TEAM_ATTR_PORT_CHANGED))
2623 			goto nest_cancel;
2624 		port->changed = false;
2625 	}
2626 	if ((port->removed &&
2627 	     nla_put_flag(skb, TEAM_ATTR_PORT_REMOVED)) ||
2628 	    (port->state.linkup &&
2629 	     nla_put_flag(skb, TEAM_ATTR_PORT_LINKUP)) ||
2630 	    nla_put_u32(skb, TEAM_ATTR_PORT_SPEED, port->state.speed) ||
2631 	    nla_put_u8(skb, TEAM_ATTR_PORT_DUPLEX, port->state.duplex))
2632 		goto nest_cancel;
2633 	nla_nest_end(skb, port_item);
2634 	return 0;
2635 
2636 nest_cancel:
2637 	nla_nest_cancel(skb, port_item);
2638 	return -EMSGSIZE;
2639 }
2640 
2641 static int team_nl_send_port_list_get(struct team *team, u32 portid, u32 seq,
2642 				      int flags, team_nl_send_func_t *send_func,
2643 				      struct team_port *one_port)
2644 {
2645 	struct nlattr *port_list;
2646 	struct nlmsghdr *nlh;
2647 	void *hdr;
2648 	struct team_port *port;
2649 	int err;
2650 	struct sk_buff *skb = NULL;
2651 	bool incomplete;
2652 	int i;
2653 
2654 	port = list_first_entry_or_null(&team->port_list,
2655 					struct team_port, list);
2656 
2657 start_again:
2658 	err = __send_and_alloc_skb(&skb, team, portid, send_func);
2659 	if (err)
2660 		return err;
2661 
2662 	hdr = genlmsg_put(skb, portid, seq, &team_nl_family, flags | NLM_F_MULTI,
2663 			  TEAM_CMD_PORT_LIST_GET);
2664 	if (!hdr) {
2665 		nlmsg_free(skb);
2666 		return -EMSGSIZE;
2667 	}
2668 
2669 	if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
2670 		goto nla_put_failure;
2671 	port_list = nla_nest_start_noflag(skb, TEAM_ATTR_LIST_PORT);
2672 	if (!port_list)
2673 		goto nla_put_failure;
2674 
2675 	i = 0;
2676 	incomplete = false;
2677 
2678 	/* If one port is selected, called wants to send port list containing
2679 	 * only this port. Otherwise go through all listed ports and send all
2680 	 */
2681 	if (one_port) {
2682 		err = team_nl_fill_one_port_get(skb, one_port);
2683 		if (err)
2684 			goto errout;
2685 	} else if (port) {
2686 		list_for_each_entry_from(port, &team->port_list, list) {
2687 			err = team_nl_fill_one_port_get(skb, port);
2688 			if (err) {
2689 				if (err == -EMSGSIZE) {
2690 					if (!i)
2691 						goto errout;
2692 					incomplete = true;
2693 					break;
2694 				}
2695 				goto errout;
2696 			}
2697 			i++;
2698 		}
2699 	}
2700 
2701 	nla_nest_end(skb, port_list);
2702 	genlmsg_end(skb, hdr);
2703 	if (incomplete)
2704 		goto start_again;
2705 
2706 send_done:
2707 	nlh = nlmsg_put(skb, portid, seq, NLMSG_DONE, 0, flags | NLM_F_MULTI);
2708 	if (!nlh) {
2709 		err = __send_and_alloc_skb(&skb, team, portid, send_func);
2710 		if (err)
2711 			return err;
2712 		goto send_done;
2713 	}
2714 
2715 	return send_func(skb, team, portid);
2716 
2717 nla_put_failure:
2718 	err = -EMSGSIZE;
2719 errout:
2720 	nlmsg_free(skb);
2721 	return err;
2722 }
2723 
2724 int team_nl_port_list_get_doit(struct sk_buff *skb,
2725 			       struct genl_info *info)
2726 {
2727 	struct team *team;
2728 	int err;
2729 
2730 	rtnl_lock();
2731 
2732 	team = team_nl_team_get(info);
2733 	if (!team) {
2734 		err = -EINVAL;
2735 		goto rtnl_unlock;
2736 	}
2737 
2738 	err = team_nl_send_port_list_get(team, info->snd_portid, info->snd_seq,
2739 					 NLM_F_ACK, team_nl_send_unicast, NULL);
2740 
2741 	team_nl_team_put(team);
2742 
2743 rtnl_unlock:
2744 	rtnl_unlock();
2745 
2746 	return err;
2747 }
2748 
2749 static const struct genl_multicast_group team_nl_mcgrps[] = {
2750 	{ .name = TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME, },
2751 };
2752 
2753 static struct genl_family team_nl_family __ro_after_init = {
2754 	.name		= TEAM_GENL_NAME,
2755 	.version	= TEAM_GENL_VERSION,
2756 	.maxattr	= ARRAY_SIZE(team_nl_policy) - 1,
2757 	.policy = team_nl_policy,
2758 	.netnsok	= true,
2759 	.module		= THIS_MODULE,
2760 	.small_ops	= team_nl_ops,
2761 	.n_small_ops	= ARRAY_SIZE(team_nl_ops),
2762 	.resv_start_op	= TEAM_CMD_PORT_LIST_GET + 1,
2763 	.mcgrps		= team_nl_mcgrps,
2764 	.n_mcgrps	= ARRAY_SIZE(team_nl_mcgrps),
2765 };
2766 
2767 static int team_nl_send_multicast(struct sk_buff *skb,
2768 				  struct team *team, u32 portid)
2769 {
2770 	return genlmsg_multicast_netns(&team_nl_family, dev_net(team->dev),
2771 				       skb, 0, 0, GFP_KERNEL);
2772 }
2773 
2774 static int team_nl_send_event_options_get(struct team *team,
2775 					  struct list_head *sel_opt_inst_list)
2776 {
2777 	return team_nl_send_options_get(team, 0, 0, 0, team_nl_send_multicast,
2778 					sel_opt_inst_list);
2779 }
2780 
2781 static int team_nl_send_event_port_get(struct team *team,
2782 				       struct team_port *port)
2783 {
2784 	return team_nl_send_port_list_get(team, 0, 0, 0, team_nl_send_multicast,
2785 					  port);
2786 }
2787 
2788 static int __init team_nl_init(void)
2789 {
2790 	return genl_register_family(&team_nl_family);
2791 }
2792 
2793 static void __exit team_nl_fini(void)
2794 {
2795 	genl_unregister_family(&team_nl_family);
2796 }
2797 
2798 
2799 /******************
2800  * Change checkers
2801  ******************/
2802 
2803 static void __team_options_change_check(struct team *team)
2804 {
2805 	int err;
2806 	struct team_option_inst *opt_inst;
2807 	LIST_HEAD(sel_opt_inst_list);
2808 
2809 	list_for_each_entry(opt_inst, &team->option_inst_list, list) {
2810 		if (opt_inst->changed)
2811 			list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
2812 	}
2813 	err = team_nl_send_event_options_get(team, &sel_opt_inst_list);
2814 	if (err && err != -ESRCH)
2815 		netdev_warn(team->dev, "Failed to send options change via netlink (err %d)\n",
2816 			    err);
2817 }
2818 
2819 /* rtnl lock is held */
2820 
2821 static void __team_port_change_send(struct team_port *port, bool linkup)
2822 {
2823 	int err;
2824 
2825 	port->changed = true;
2826 	port->state.linkup = linkup;
2827 	team_refresh_port_linkup(port);
2828 	if (linkup) {
2829 		struct ethtool_link_ksettings ecmd;
2830 
2831 		err = __ethtool_get_link_ksettings(port->dev, &ecmd);
2832 		if (!err) {
2833 			port->state.speed = ecmd.base.speed;
2834 			port->state.duplex = ecmd.base.duplex;
2835 			goto send_event;
2836 		}
2837 	}
2838 	port->state.speed = 0;
2839 	port->state.duplex = 0;
2840 
2841 send_event:
2842 	err = team_nl_send_event_port_get(port->team, port);
2843 	if (err && err != -ESRCH)
2844 		netdev_warn(port->team->dev, "Failed to send port change of device %s via netlink (err %d)\n",
2845 			    port->dev->name, err);
2846 
2847 }
2848 
2849 static void __team_carrier_check(struct team *team)
2850 {
2851 	struct team_port *port;
2852 	bool team_linkup;
2853 
2854 	if (team->user_carrier_enabled)
2855 		return;
2856 
2857 	team_linkup = false;
2858 	list_for_each_entry(port, &team->port_list, list) {
2859 		if (port->linkup) {
2860 			team_linkup = true;
2861 			break;
2862 		}
2863 	}
2864 
2865 	if (team_linkup)
2866 		netif_carrier_on(team->dev);
2867 	else
2868 		netif_carrier_off(team->dev);
2869 }
2870 
2871 static void __team_port_change_check(struct team_port *port, bool linkup)
2872 {
2873 	if (port->state.linkup != linkup)
2874 		__team_port_change_send(port, linkup);
2875 	__team_carrier_check(port->team);
2876 }
2877 
2878 static void __team_port_change_port_added(struct team_port *port, bool linkup)
2879 {
2880 	__team_port_change_send(port, linkup);
2881 	__team_carrier_check(port->team);
2882 }
2883 
2884 static void __team_port_change_port_removed(struct team_port *port)
2885 {
2886 	port->removed = true;
2887 	__team_port_change_send(port, false);
2888 	__team_carrier_check(port->team);
2889 }
2890 
2891 static void team_port_change_check(struct team_port *port, bool linkup)
2892 {
2893 	ASSERT_RTNL();
2894 
2895 	__team_port_change_check(port, linkup);
2896 }
2897 
2898 
2899 /************************************
2900  * Net device notifier event handler
2901  ************************************/
2902 
2903 static int team_device_event(struct notifier_block *unused,
2904 			     unsigned long event, void *ptr)
2905 {
2906 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2907 	struct team_port *port;
2908 
2909 	port = team_port_get_rtnl(dev);
2910 	if (!port)
2911 		return NOTIFY_DONE;
2912 
2913 	switch (event) {
2914 	case NETDEV_UP:
2915 		if (netif_oper_up(dev))
2916 			team_port_change_check(port, true);
2917 		break;
2918 	case NETDEV_DOWN:
2919 		team_port_change_check(port, false);
2920 		break;
2921 	case NETDEV_CHANGE:
2922 		if (netif_running(port->dev))
2923 			team_port_change_check(port,
2924 					       !!netif_oper_up(port->dev));
2925 		break;
2926 	case NETDEV_UNREGISTER:
2927 		team_del_slave(port->team->dev, dev);
2928 		break;
2929 	case NETDEV_FEAT_CHANGE:
2930 		if (!port->team->notifier_ctx) {
2931 			port->team->notifier_ctx = true;
2932 			netdev_compute_master_upper_features(port->team->dev, true);
2933 			port->team->notifier_ctx = false;
2934 		}
2935 		break;
2936 	case NETDEV_PRECHANGEMTU:
2937 		/* Forbid to change mtu of underlaying device */
2938 		if (!port->team->port_mtu_change_allowed)
2939 			return NOTIFY_BAD;
2940 		break;
2941 	case NETDEV_PRE_TYPE_CHANGE:
2942 		/* Forbid to change type of underlaying device */
2943 		return NOTIFY_BAD;
2944 	case NETDEV_RESEND_IGMP:
2945 		/* Propagate to master device */
2946 		call_netdevice_notifiers(event, port->team->dev);
2947 		break;
2948 	}
2949 	return NOTIFY_DONE;
2950 }
2951 
2952 static struct notifier_block team_notifier_block __read_mostly = {
2953 	.notifier_call = team_device_event,
2954 };
2955 
2956 
2957 /***********************
2958  * Module init and exit
2959  ***********************/
2960 
2961 static int __init team_module_init(void)
2962 {
2963 	int err;
2964 
2965 	register_netdevice_notifier(&team_notifier_block);
2966 
2967 	err = rtnl_link_register(&team_link_ops);
2968 	if (err)
2969 		goto err_rtnl_reg;
2970 
2971 	err = team_nl_init();
2972 	if (err)
2973 		goto err_nl_init;
2974 
2975 	return 0;
2976 
2977 err_nl_init:
2978 	rtnl_link_unregister(&team_link_ops);
2979 
2980 err_rtnl_reg:
2981 	unregister_netdevice_notifier(&team_notifier_block);
2982 
2983 	return err;
2984 }
2985 
2986 static void __exit team_module_exit(void)
2987 {
2988 	team_nl_fini();
2989 	rtnl_link_unregister(&team_link_ops);
2990 	unregister_netdevice_notifier(&team_notifier_block);
2991 }
2992 
2993 module_init(team_module_init);
2994 module_exit(team_module_exit);
2995 
2996 MODULE_LICENSE("GPL v2");
2997 MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
2998 MODULE_DESCRIPTION("Ethernet team device driver");
2999 MODULE_ALIAS_RTNL_LINK(DRV_NAME);
3000