xref: /linux/net/core/rtnetlink.c (revision 55b078f031f556ea18963bd808e79a1dfaa4be44)
1 /*
2  * INET		An implementation of the TCP/IP protocol suite for the LINUX
3  *		operating system.  INET is implemented using the  BSD Socket
4  *		interface as the means of communication with the user level.
5  *
6  *		Routing netlink socket interface: protocol independent part.
7  *
8  * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
9  *
10  *		This program is free software; you can redistribute it and/or
11  *		modify it under the terms of the GNU General Public License
12  *		as published by the Free Software Foundation; either version
13  *		2 of the License, or (at your option) any later version.
14  *
15  *	Fixes:
16  *	Vitaly E. Lavrov		RTA_OK arithmetics was wrong.
17  */
18 
19 #include <linux/bitops.h>
20 #include <linux/errno.h>
21 #include <linux/module.h>
22 #include <linux/types.h>
23 #include <linux/socket.h>
24 #include <linux/kernel.h>
25 #include <linux/timer.h>
26 #include <linux/string.h>
27 #include <linux/sockios.h>
28 #include <linux/net.h>
29 #include <linux/fcntl.h>
30 #include <linux/mm.h>
31 #include <linux/slab.h>
32 #include <linux/interrupt.h>
33 #include <linux/capability.h>
34 #include <linux/skbuff.h>
35 #include <linux/init.h>
36 #include <linux/security.h>
37 #include <linux/mutex.h>
38 #include <linux/if_addr.h>
39 #include <linux/if_bridge.h>
40 #include <linux/if_vlan.h>
41 #include <linux/pci.h>
42 #include <linux/etherdevice.h>
43 #include <linux/bpf.h>
44 
45 #include <linux/uaccess.h>
46 
47 #include <linux/inet.h>
48 #include <linux/netdevice.h>
49 #include <net/switchdev.h>
50 #include <net/ip.h>
51 #include <net/protocol.h>
52 #include <net/arp.h>
53 #include <net/route.h>
54 #include <net/udp.h>
55 #include <net/tcp.h>
56 #include <net/sock.h>
57 #include <net/pkt_sched.h>
58 #include <net/fib_rules.h>
59 #include <net/rtnetlink.h>
60 #include <net/net_namespace.h>
61 
62 #define RTNL_MAX_TYPE		49
63 #define RTNL_SLAVE_MAX_TYPE	36
64 
65 struct rtnl_link {
66 	rtnl_doit_func		doit;
67 	rtnl_dumpit_func	dumpit;
68 	struct module		*owner;
69 	unsigned int		flags;
70 	struct rcu_head		rcu;
71 };
72 
73 static DEFINE_MUTEX(rtnl_mutex);
74 
75 void rtnl_lock(void)
76 {
77 	mutex_lock(&rtnl_mutex);
78 }
79 EXPORT_SYMBOL(rtnl_lock);
80 
81 int rtnl_lock_killable(void)
82 {
83 	return mutex_lock_killable(&rtnl_mutex);
84 }
85 EXPORT_SYMBOL(rtnl_lock_killable);
86 
87 static struct sk_buff *defer_kfree_skb_list;
88 void rtnl_kfree_skbs(struct sk_buff *head, struct sk_buff *tail)
89 {
90 	if (head && tail) {
91 		tail->next = defer_kfree_skb_list;
92 		defer_kfree_skb_list = head;
93 	}
94 }
95 EXPORT_SYMBOL(rtnl_kfree_skbs);
96 
97 void __rtnl_unlock(void)
98 {
99 	struct sk_buff *head = defer_kfree_skb_list;
100 
101 	defer_kfree_skb_list = NULL;
102 
103 	mutex_unlock(&rtnl_mutex);
104 
105 	while (head) {
106 		struct sk_buff *next = head->next;
107 
108 		kfree_skb(head);
109 		cond_resched();
110 		head = next;
111 	}
112 }
113 
114 void rtnl_unlock(void)
115 {
116 	/* This fellow will unlock it for us. */
117 	netdev_run_todo();
118 }
119 EXPORT_SYMBOL(rtnl_unlock);
120 
121 int rtnl_trylock(void)
122 {
123 	return mutex_trylock(&rtnl_mutex);
124 }
125 EXPORT_SYMBOL(rtnl_trylock);
126 
127 int rtnl_is_locked(void)
128 {
129 	return mutex_is_locked(&rtnl_mutex);
130 }
131 EXPORT_SYMBOL(rtnl_is_locked);
132 
133 bool refcount_dec_and_rtnl_lock(refcount_t *r)
134 {
135 	return refcount_dec_and_mutex_lock(r, &rtnl_mutex);
136 }
137 EXPORT_SYMBOL(refcount_dec_and_rtnl_lock);
138 
139 #ifdef CONFIG_PROVE_LOCKING
140 bool lockdep_rtnl_is_held(void)
141 {
142 	return lockdep_is_held(&rtnl_mutex);
143 }
144 EXPORT_SYMBOL(lockdep_rtnl_is_held);
145 #endif /* #ifdef CONFIG_PROVE_LOCKING */
146 
147 static struct rtnl_link *__rcu *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
148 
149 static inline int rtm_msgindex(int msgtype)
150 {
151 	int msgindex = msgtype - RTM_BASE;
152 
153 	/*
154 	 * msgindex < 0 implies someone tried to register a netlink
155 	 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
156 	 * the message type has not been added to linux/rtnetlink.h
157 	 */
158 	BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
159 
160 	return msgindex;
161 }
162 
163 static struct rtnl_link *rtnl_get_link(int protocol, int msgtype)
164 {
165 	struct rtnl_link **tab;
166 
167 	if (protocol >= ARRAY_SIZE(rtnl_msg_handlers))
168 		protocol = PF_UNSPEC;
169 
170 	tab = rcu_dereference_rtnl(rtnl_msg_handlers[protocol]);
171 	if (!tab)
172 		tab = rcu_dereference_rtnl(rtnl_msg_handlers[PF_UNSPEC]);
173 
174 	return tab[msgtype];
175 }
176 
177 static int rtnl_register_internal(struct module *owner,
178 				  int protocol, int msgtype,
179 				  rtnl_doit_func doit, rtnl_dumpit_func dumpit,
180 				  unsigned int flags)
181 {
182 	struct rtnl_link *link, *old;
183 	struct rtnl_link __rcu **tab;
184 	int msgindex;
185 	int ret = -ENOBUFS;
186 
187 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
188 	msgindex = rtm_msgindex(msgtype);
189 
190 	rtnl_lock();
191 	tab = rtnl_msg_handlers[protocol];
192 	if (tab == NULL) {
193 		tab = kcalloc(RTM_NR_MSGTYPES, sizeof(void *), GFP_KERNEL);
194 		if (!tab)
195 			goto unlock;
196 
197 		/* ensures we see the 0 stores */
198 		rcu_assign_pointer(rtnl_msg_handlers[protocol], tab);
199 	}
200 
201 	old = rtnl_dereference(tab[msgindex]);
202 	if (old) {
203 		link = kmemdup(old, sizeof(*old), GFP_KERNEL);
204 		if (!link)
205 			goto unlock;
206 	} else {
207 		link = kzalloc(sizeof(*link), GFP_KERNEL);
208 		if (!link)
209 			goto unlock;
210 	}
211 
212 	WARN_ON(link->owner && link->owner != owner);
213 	link->owner = owner;
214 
215 	WARN_ON(doit && link->doit && link->doit != doit);
216 	if (doit)
217 		link->doit = doit;
218 	WARN_ON(dumpit && link->dumpit && link->dumpit != dumpit);
219 	if (dumpit)
220 		link->dumpit = dumpit;
221 
222 	link->flags |= flags;
223 
224 	/* publish protocol:msgtype */
225 	rcu_assign_pointer(tab[msgindex], link);
226 	ret = 0;
227 	if (old)
228 		kfree_rcu(old, rcu);
229 unlock:
230 	rtnl_unlock();
231 	return ret;
232 }
233 
234 /**
235  * rtnl_register_module - Register a rtnetlink message type
236  *
237  * @owner: module registering the hook (THIS_MODULE)
238  * @protocol: Protocol family or PF_UNSPEC
239  * @msgtype: rtnetlink message type
240  * @doit: Function pointer called for each request message
241  * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
242  * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
243  *
244  * Like rtnl_register, but for use by removable modules.
245  */
246 int rtnl_register_module(struct module *owner,
247 			 int protocol, int msgtype,
248 			 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
249 			 unsigned int flags)
250 {
251 	return rtnl_register_internal(owner, protocol, msgtype,
252 				      doit, dumpit, flags);
253 }
254 EXPORT_SYMBOL_GPL(rtnl_register_module);
255 
256 /**
257  * rtnl_register - Register a rtnetlink message type
258  * @protocol: Protocol family or PF_UNSPEC
259  * @msgtype: rtnetlink message type
260  * @doit: Function pointer called for each request message
261  * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
262  * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
263  *
264  * Registers the specified function pointers (at least one of them has
265  * to be non-NULL) to be called whenever a request message for the
266  * specified protocol family and message type is received.
267  *
268  * The special protocol family PF_UNSPEC may be used to define fallback
269  * function pointers for the case when no entry for the specific protocol
270  * family exists.
271  */
272 void rtnl_register(int protocol, int msgtype,
273 		   rtnl_doit_func doit, rtnl_dumpit_func dumpit,
274 		   unsigned int flags)
275 {
276 	int err;
277 
278 	err = rtnl_register_internal(NULL, protocol, msgtype, doit, dumpit,
279 				     flags);
280 	if (err)
281 		pr_err("Unable to register rtnetlink message handler, "
282 		       "protocol = %d, message type = %d\n", protocol, msgtype);
283 }
284 
285 /**
286  * rtnl_unregister - Unregister a rtnetlink message type
287  * @protocol: Protocol family or PF_UNSPEC
288  * @msgtype: rtnetlink message type
289  *
290  * Returns 0 on success or a negative error code.
291  */
292 int rtnl_unregister(int protocol, int msgtype)
293 {
294 	struct rtnl_link **tab, *link;
295 	int msgindex;
296 
297 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
298 	msgindex = rtm_msgindex(msgtype);
299 
300 	rtnl_lock();
301 	tab = rtnl_dereference(rtnl_msg_handlers[protocol]);
302 	if (!tab) {
303 		rtnl_unlock();
304 		return -ENOENT;
305 	}
306 
307 	link = tab[msgindex];
308 	rcu_assign_pointer(tab[msgindex], NULL);
309 	rtnl_unlock();
310 
311 	kfree_rcu(link, rcu);
312 
313 	return 0;
314 }
315 EXPORT_SYMBOL_GPL(rtnl_unregister);
316 
317 /**
318  * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
319  * @protocol : Protocol family or PF_UNSPEC
320  *
321  * Identical to calling rtnl_unregster() for all registered message types
322  * of a certain protocol family.
323  */
324 void rtnl_unregister_all(int protocol)
325 {
326 	struct rtnl_link **tab, *link;
327 	int msgindex;
328 
329 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
330 
331 	rtnl_lock();
332 	tab = rtnl_msg_handlers[protocol];
333 	if (!tab) {
334 		rtnl_unlock();
335 		return;
336 	}
337 	RCU_INIT_POINTER(rtnl_msg_handlers[protocol], NULL);
338 	for (msgindex = 0; msgindex < RTM_NR_MSGTYPES; msgindex++) {
339 		link = tab[msgindex];
340 		if (!link)
341 			continue;
342 
343 		rcu_assign_pointer(tab[msgindex], NULL);
344 		kfree_rcu(link, rcu);
345 	}
346 	rtnl_unlock();
347 
348 	synchronize_net();
349 
350 	kfree(tab);
351 }
352 EXPORT_SYMBOL_GPL(rtnl_unregister_all);
353 
354 static LIST_HEAD(link_ops);
355 
356 static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
357 {
358 	const struct rtnl_link_ops *ops;
359 
360 	list_for_each_entry(ops, &link_ops, list) {
361 		if (!strcmp(ops->kind, kind))
362 			return ops;
363 	}
364 	return NULL;
365 }
366 
367 /**
368  * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
369  * @ops: struct rtnl_link_ops * to register
370  *
371  * The caller must hold the rtnl_mutex. This function should be used
372  * by drivers that create devices during module initialization. It
373  * must be called before registering the devices.
374  *
375  * Returns 0 on success or a negative error code.
376  */
377 int __rtnl_link_register(struct rtnl_link_ops *ops)
378 {
379 	if (rtnl_link_ops_get(ops->kind))
380 		return -EEXIST;
381 
382 	/* The check for setup is here because if ops
383 	 * does not have that filled up, it is not possible
384 	 * to use the ops for creating device. So do not
385 	 * fill up dellink as well. That disables rtnl_dellink.
386 	 */
387 	if (ops->setup && !ops->dellink)
388 		ops->dellink = unregister_netdevice_queue;
389 
390 	list_add_tail(&ops->list, &link_ops);
391 	return 0;
392 }
393 EXPORT_SYMBOL_GPL(__rtnl_link_register);
394 
395 /**
396  * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
397  * @ops: struct rtnl_link_ops * to register
398  *
399  * Returns 0 on success or a negative error code.
400  */
401 int rtnl_link_register(struct rtnl_link_ops *ops)
402 {
403 	int err;
404 
405 	/* Sanity-check max sizes to avoid stack buffer overflow. */
406 	if (WARN_ON(ops->maxtype > RTNL_MAX_TYPE ||
407 		    ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE))
408 		return -EINVAL;
409 
410 	rtnl_lock();
411 	err = __rtnl_link_register(ops);
412 	rtnl_unlock();
413 	return err;
414 }
415 EXPORT_SYMBOL_GPL(rtnl_link_register);
416 
417 static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
418 {
419 	struct net_device *dev;
420 	LIST_HEAD(list_kill);
421 
422 	for_each_netdev(net, dev) {
423 		if (dev->rtnl_link_ops == ops)
424 			ops->dellink(dev, &list_kill);
425 	}
426 	unregister_netdevice_many(&list_kill);
427 }
428 
429 /**
430  * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
431  * @ops: struct rtnl_link_ops * to unregister
432  *
433  * The caller must hold the rtnl_mutex and guarantee net_namespace_list
434  * integrity (hold pernet_ops_rwsem for writing to close the race
435  * with setup_net() and cleanup_net()).
436  */
437 void __rtnl_link_unregister(struct rtnl_link_ops *ops)
438 {
439 	struct net *net;
440 
441 	for_each_net(net) {
442 		__rtnl_kill_links(net, ops);
443 	}
444 	list_del(&ops->list);
445 }
446 EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
447 
448 /* Return with the rtnl_lock held when there are no network
449  * devices unregistering in any network namespace.
450  */
451 static void rtnl_lock_unregistering_all(void)
452 {
453 	struct net *net;
454 	bool unregistering;
455 	DEFINE_WAIT_FUNC(wait, woken_wake_function);
456 
457 	add_wait_queue(&netdev_unregistering_wq, &wait);
458 	for (;;) {
459 		unregistering = false;
460 		rtnl_lock();
461 		/* We held write locked pernet_ops_rwsem, and parallel
462 		 * setup_net() and cleanup_net() are not possible.
463 		 */
464 		for_each_net(net) {
465 			if (net->dev_unreg_count > 0) {
466 				unregistering = true;
467 				break;
468 			}
469 		}
470 		if (!unregistering)
471 			break;
472 		__rtnl_unlock();
473 
474 		wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
475 	}
476 	remove_wait_queue(&netdev_unregistering_wq, &wait);
477 }
478 
479 /**
480  * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
481  * @ops: struct rtnl_link_ops * to unregister
482  */
483 void rtnl_link_unregister(struct rtnl_link_ops *ops)
484 {
485 	/* Close the race with setup_net() and cleanup_net() */
486 	down_write(&pernet_ops_rwsem);
487 	rtnl_lock_unregistering_all();
488 	__rtnl_link_unregister(ops);
489 	rtnl_unlock();
490 	up_write(&pernet_ops_rwsem);
491 }
492 EXPORT_SYMBOL_GPL(rtnl_link_unregister);
493 
494 static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
495 {
496 	struct net_device *master_dev;
497 	const struct rtnl_link_ops *ops;
498 	size_t size = 0;
499 
500 	rcu_read_lock();
501 
502 	master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
503 	if (!master_dev)
504 		goto out;
505 
506 	ops = master_dev->rtnl_link_ops;
507 	if (!ops || !ops->get_slave_size)
508 		goto out;
509 	/* IFLA_INFO_SLAVE_DATA + nested data */
510 	size = nla_total_size(sizeof(struct nlattr)) +
511 	       ops->get_slave_size(master_dev, dev);
512 
513 out:
514 	rcu_read_unlock();
515 	return size;
516 }
517 
518 static size_t rtnl_link_get_size(const struct net_device *dev)
519 {
520 	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
521 	size_t size;
522 
523 	if (!ops)
524 		return 0;
525 
526 	size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
527 	       nla_total_size(strlen(ops->kind) + 1);  /* IFLA_INFO_KIND */
528 
529 	if (ops->get_size)
530 		/* IFLA_INFO_DATA + nested data */
531 		size += nla_total_size(sizeof(struct nlattr)) +
532 			ops->get_size(dev);
533 
534 	if (ops->get_xstats_size)
535 		/* IFLA_INFO_XSTATS */
536 		size += nla_total_size(ops->get_xstats_size(dev));
537 
538 	size += rtnl_link_get_slave_info_data_size(dev);
539 
540 	return size;
541 }
542 
543 static LIST_HEAD(rtnl_af_ops);
544 
545 static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
546 {
547 	const struct rtnl_af_ops *ops;
548 
549 	list_for_each_entry_rcu(ops, &rtnl_af_ops, list) {
550 		if (ops->family == family)
551 			return ops;
552 	}
553 
554 	return NULL;
555 }
556 
557 /**
558  * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
559  * @ops: struct rtnl_af_ops * to register
560  *
561  * Returns 0 on success or a negative error code.
562  */
563 void rtnl_af_register(struct rtnl_af_ops *ops)
564 {
565 	rtnl_lock();
566 	list_add_tail_rcu(&ops->list, &rtnl_af_ops);
567 	rtnl_unlock();
568 }
569 EXPORT_SYMBOL_GPL(rtnl_af_register);
570 
571 /**
572  * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
573  * @ops: struct rtnl_af_ops * to unregister
574  */
575 void rtnl_af_unregister(struct rtnl_af_ops *ops)
576 {
577 	rtnl_lock();
578 	list_del_rcu(&ops->list);
579 	rtnl_unlock();
580 
581 	synchronize_rcu();
582 }
583 EXPORT_SYMBOL_GPL(rtnl_af_unregister);
584 
585 static size_t rtnl_link_get_af_size(const struct net_device *dev,
586 				    u32 ext_filter_mask)
587 {
588 	struct rtnl_af_ops *af_ops;
589 	size_t size;
590 
591 	/* IFLA_AF_SPEC */
592 	size = nla_total_size(sizeof(struct nlattr));
593 
594 	rcu_read_lock();
595 	list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
596 		if (af_ops->get_link_af_size) {
597 			/* AF_* + nested data */
598 			size += nla_total_size(sizeof(struct nlattr)) +
599 				af_ops->get_link_af_size(dev, ext_filter_mask);
600 		}
601 	}
602 	rcu_read_unlock();
603 
604 	return size;
605 }
606 
607 static bool rtnl_have_link_slave_info(const struct net_device *dev)
608 {
609 	struct net_device *master_dev;
610 	bool ret = false;
611 
612 	rcu_read_lock();
613 
614 	master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
615 	if (master_dev && master_dev->rtnl_link_ops)
616 		ret = true;
617 	rcu_read_unlock();
618 	return ret;
619 }
620 
621 static int rtnl_link_slave_info_fill(struct sk_buff *skb,
622 				     const struct net_device *dev)
623 {
624 	struct net_device *master_dev;
625 	const struct rtnl_link_ops *ops;
626 	struct nlattr *slave_data;
627 	int err;
628 
629 	master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
630 	if (!master_dev)
631 		return 0;
632 	ops = master_dev->rtnl_link_ops;
633 	if (!ops)
634 		return 0;
635 	if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
636 		return -EMSGSIZE;
637 	if (ops->fill_slave_info) {
638 		slave_data = nla_nest_start(skb, IFLA_INFO_SLAVE_DATA);
639 		if (!slave_data)
640 			return -EMSGSIZE;
641 		err = ops->fill_slave_info(skb, master_dev, dev);
642 		if (err < 0)
643 			goto err_cancel_slave_data;
644 		nla_nest_end(skb, slave_data);
645 	}
646 	return 0;
647 
648 err_cancel_slave_data:
649 	nla_nest_cancel(skb, slave_data);
650 	return err;
651 }
652 
653 static int rtnl_link_info_fill(struct sk_buff *skb,
654 			       const struct net_device *dev)
655 {
656 	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
657 	struct nlattr *data;
658 	int err;
659 
660 	if (!ops)
661 		return 0;
662 	if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
663 		return -EMSGSIZE;
664 	if (ops->fill_xstats) {
665 		err = ops->fill_xstats(skb, dev);
666 		if (err < 0)
667 			return err;
668 	}
669 	if (ops->fill_info) {
670 		data = nla_nest_start(skb, IFLA_INFO_DATA);
671 		if (data == NULL)
672 			return -EMSGSIZE;
673 		err = ops->fill_info(skb, dev);
674 		if (err < 0)
675 			goto err_cancel_data;
676 		nla_nest_end(skb, data);
677 	}
678 	return 0;
679 
680 err_cancel_data:
681 	nla_nest_cancel(skb, data);
682 	return err;
683 }
684 
685 static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
686 {
687 	struct nlattr *linkinfo;
688 	int err = -EMSGSIZE;
689 
690 	linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
691 	if (linkinfo == NULL)
692 		goto out;
693 
694 	err = rtnl_link_info_fill(skb, dev);
695 	if (err < 0)
696 		goto err_cancel_link;
697 
698 	err = rtnl_link_slave_info_fill(skb, dev);
699 	if (err < 0)
700 		goto err_cancel_link;
701 
702 	nla_nest_end(skb, linkinfo);
703 	return 0;
704 
705 err_cancel_link:
706 	nla_nest_cancel(skb, linkinfo);
707 out:
708 	return err;
709 }
710 
711 int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
712 {
713 	struct sock *rtnl = net->rtnl;
714 	int err = 0;
715 
716 	NETLINK_CB(skb).dst_group = group;
717 	if (echo)
718 		refcount_inc(&skb->users);
719 	netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
720 	if (echo)
721 		err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
722 	return err;
723 }
724 
725 int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
726 {
727 	struct sock *rtnl = net->rtnl;
728 
729 	return nlmsg_unicast(rtnl, skb, pid);
730 }
731 EXPORT_SYMBOL(rtnl_unicast);
732 
733 void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
734 		 struct nlmsghdr *nlh, gfp_t flags)
735 {
736 	struct sock *rtnl = net->rtnl;
737 	int report = 0;
738 
739 	if (nlh)
740 		report = nlmsg_report(nlh);
741 
742 	nlmsg_notify(rtnl, skb, pid, group, report, flags);
743 }
744 EXPORT_SYMBOL(rtnl_notify);
745 
746 void rtnl_set_sk_err(struct net *net, u32 group, int error)
747 {
748 	struct sock *rtnl = net->rtnl;
749 
750 	netlink_set_err(rtnl, 0, group, error);
751 }
752 EXPORT_SYMBOL(rtnl_set_sk_err);
753 
754 int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
755 {
756 	struct nlattr *mx;
757 	int i, valid = 0;
758 
759 	mx = nla_nest_start(skb, RTA_METRICS);
760 	if (mx == NULL)
761 		return -ENOBUFS;
762 
763 	for (i = 0; i < RTAX_MAX; i++) {
764 		if (metrics[i]) {
765 			if (i == RTAX_CC_ALGO - 1) {
766 				char tmp[TCP_CA_NAME_MAX], *name;
767 
768 				name = tcp_ca_get_name_by_key(metrics[i], tmp);
769 				if (!name)
770 					continue;
771 				if (nla_put_string(skb, i + 1, name))
772 					goto nla_put_failure;
773 			} else if (i == RTAX_FEATURES - 1) {
774 				u32 user_features = metrics[i] & RTAX_FEATURE_MASK;
775 
776 				if (!user_features)
777 					continue;
778 				BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK);
779 				if (nla_put_u32(skb, i + 1, user_features))
780 					goto nla_put_failure;
781 			} else {
782 				if (nla_put_u32(skb, i + 1, metrics[i]))
783 					goto nla_put_failure;
784 			}
785 			valid++;
786 		}
787 	}
788 
789 	if (!valid) {
790 		nla_nest_cancel(skb, mx);
791 		return 0;
792 	}
793 
794 	return nla_nest_end(skb, mx);
795 
796 nla_put_failure:
797 	nla_nest_cancel(skb, mx);
798 	return -EMSGSIZE;
799 }
800 EXPORT_SYMBOL(rtnetlink_put_metrics);
801 
802 int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
803 		       long expires, u32 error)
804 {
805 	struct rta_cacheinfo ci = {
806 		.rta_error = error,
807 		.rta_id =  id,
808 	};
809 
810 	if (dst) {
811 		ci.rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse);
812 		ci.rta_used = dst->__use;
813 		ci.rta_clntref = atomic_read(&dst->__refcnt);
814 	}
815 	if (expires) {
816 		unsigned long clock;
817 
818 		clock = jiffies_to_clock_t(abs(expires));
819 		clock = min_t(unsigned long, clock, INT_MAX);
820 		ci.rta_expires = (expires > 0) ? clock : -clock;
821 	}
822 	return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
823 }
824 EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
825 
826 static void set_operstate(struct net_device *dev, unsigned char transition)
827 {
828 	unsigned char operstate = dev->operstate;
829 
830 	switch (transition) {
831 	case IF_OPER_UP:
832 		if ((operstate == IF_OPER_DORMANT ||
833 		     operstate == IF_OPER_UNKNOWN) &&
834 		    !netif_dormant(dev))
835 			operstate = IF_OPER_UP;
836 		break;
837 
838 	case IF_OPER_DORMANT:
839 		if (operstate == IF_OPER_UP ||
840 		    operstate == IF_OPER_UNKNOWN)
841 			operstate = IF_OPER_DORMANT;
842 		break;
843 	}
844 
845 	if (dev->operstate != operstate) {
846 		write_lock_bh(&dev_base_lock);
847 		dev->operstate = operstate;
848 		write_unlock_bh(&dev_base_lock);
849 		netdev_state_change(dev);
850 	}
851 }
852 
853 static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
854 {
855 	return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
856 	       (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
857 }
858 
859 static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
860 					   const struct ifinfomsg *ifm)
861 {
862 	unsigned int flags = ifm->ifi_flags;
863 
864 	/* bugwards compatibility: ifi_change == 0 is treated as ~0 */
865 	if (ifm->ifi_change)
866 		flags = (flags & ifm->ifi_change) |
867 			(rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
868 
869 	return flags;
870 }
871 
872 static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
873 				 const struct rtnl_link_stats64 *b)
874 {
875 	a->rx_packets = b->rx_packets;
876 	a->tx_packets = b->tx_packets;
877 	a->rx_bytes = b->rx_bytes;
878 	a->tx_bytes = b->tx_bytes;
879 	a->rx_errors = b->rx_errors;
880 	a->tx_errors = b->tx_errors;
881 	a->rx_dropped = b->rx_dropped;
882 	a->tx_dropped = b->tx_dropped;
883 
884 	a->multicast = b->multicast;
885 	a->collisions = b->collisions;
886 
887 	a->rx_length_errors = b->rx_length_errors;
888 	a->rx_over_errors = b->rx_over_errors;
889 	a->rx_crc_errors = b->rx_crc_errors;
890 	a->rx_frame_errors = b->rx_frame_errors;
891 	a->rx_fifo_errors = b->rx_fifo_errors;
892 	a->rx_missed_errors = b->rx_missed_errors;
893 
894 	a->tx_aborted_errors = b->tx_aborted_errors;
895 	a->tx_carrier_errors = b->tx_carrier_errors;
896 	a->tx_fifo_errors = b->tx_fifo_errors;
897 	a->tx_heartbeat_errors = b->tx_heartbeat_errors;
898 	a->tx_window_errors = b->tx_window_errors;
899 
900 	a->rx_compressed = b->rx_compressed;
901 	a->tx_compressed = b->tx_compressed;
902 
903 	a->rx_nohandler = b->rx_nohandler;
904 }
905 
906 /* All VF info */
907 static inline int rtnl_vfinfo_size(const struct net_device *dev,
908 				   u32 ext_filter_mask)
909 {
910 	if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) {
911 		int num_vfs = dev_num_vf(dev->dev.parent);
912 		size_t size = nla_total_size(0);
913 		size += num_vfs *
914 			(nla_total_size(0) +
915 			 nla_total_size(sizeof(struct ifla_vf_mac)) +
916 			 nla_total_size(sizeof(struct ifla_vf_vlan)) +
917 			 nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
918 			 nla_total_size(MAX_VLAN_LIST_LEN *
919 					sizeof(struct ifla_vf_vlan_info)) +
920 			 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
921 			 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
922 			 nla_total_size(sizeof(struct ifla_vf_rate)) +
923 			 nla_total_size(sizeof(struct ifla_vf_link_state)) +
924 			 nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
925 			 nla_total_size(0) + /* nest IFLA_VF_STATS */
926 			 /* IFLA_VF_STATS_RX_PACKETS */
927 			 nla_total_size_64bit(sizeof(__u64)) +
928 			 /* IFLA_VF_STATS_TX_PACKETS */
929 			 nla_total_size_64bit(sizeof(__u64)) +
930 			 /* IFLA_VF_STATS_RX_BYTES */
931 			 nla_total_size_64bit(sizeof(__u64)) +
932 			 /* IFLA_VF_STATS_TX_BYTES */
933 			 nla_total_size_64bit(sizeof(__u64)) +
934 			 /* IFLA_VF_STATS_BROADCAST */
935 			 nla_total_size_64bit(sizeof(__u64)) +
936 			 /* IFLA_VF_STATS_MULTICAST */
937 			 nla_total_size_64bit(sizeof(__u64)) +
938 			 /* IFLA_VF_STATS_RX_DROPPED */
939 			 nla_total_size_64bit(sizeof(__u64)) +
940 			 /* IFLA_VF_STATS_TX_DROPPED */
941 			 nla_total_size_64bit(sizeof(__u64)) +
942 			 nla_total_size(sizeof(struct ifla_vf_trust)));
943 		return size;
944 	} else
945 		return 0;
946 }
947 
948 static size_t rtnl_port_size(const struct net_device *dev,
949 			     u32 ext_filter_mask)
950 {
951 	size_t port_size = nla_total_size(4)		/* PORT_VF */
952 		+ nla_total_size(PORT_PROFILE_MAX)	/* PORT_PROFILE */
953 		+ nla_total_size(PORT_UUID_MAX)		/* PORT_INSTANCE_UUID */
954 		+ nla_total_size(PORT_UUID_MAX)		/* PORT_HOST_UUID */
955 		+ nla_total_size(1)			/* PROT_VDP_REQUEST */
956 		+ nla_total_size(2);			/* PORT_VDP_RESPONSE */
957 	size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
958 	size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
959 		+ port_size;
960 	size_t port_self_size = nla_total_size(sizeof(struct nlattr))
961 		+ port_size;
962 
963 	if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
964 	    !(ext_filter_mask & RTEXT_FILTER_VF))
965 		return 0;
966 	if (dev_num_vf(dev->dev.parent))
967 		return port_self_size + vf_ports_size +
968 			vf_port_size * dev_num_vf(dev->dev.parent);
969 	else
970 		return port_self_size;
971 }
972 
973 static size_t rtnl_xdp_size(void)
974 {
975 	size_t xdp_size = nla_total_size(0) +	/* nest IFLA_XDP */
976 			  nla_total_size(1) +	/* XDP_ATTACHED */
977 			  nla_total_size(4) +	/* XDP_PROG_ID (or 1st mode) */
978 			  nla_total_size(4);	/* XDP_<mode>_PROG_ID */
979 
980 	return xdp_size;
981 }
982 
983 static noinline size_t if_nlmsg_size(const struct net_device *dev,
984 				     u32 ext_filter_mask)
985 {
986 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
987 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
988 	       + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
989 	       + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
990 	       + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap))
991 	       + nla_total_size(sizeof(struct rtnl_link_stats))
992 	       + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
993 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
994 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
995 	       + nla_total_size(4) /* IFLA_TXQLEN */
996 	       + nla_total_size(4) /* IFLA_WEIGHT */
997 	       + nla_total_size(4) /* IFLA_MTU */
998 	       + nla_total_size(4) /* IFLA_LINK */
999 	       + nla_total_size(4) /* IFLA_MASTER */
1000 	       + nla_total_size(1) /* IFLA_CARRIER */
1001 	       + nla_total_size(4) /* IFLA_PROMISCUITY */
1002 	       + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
1003 	       + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
1004 	       + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
1005 	       + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
1006 	       + nla_total_size(1) /* IFLA_OPERSTATE */
1007 	       + nla_total_size(1) /* IFLA_LINKMODE */
1008 	       + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
1009 	       + nla_total_size(4) /* IFLA_LINK_NETNSID */
1010 	       + nla_total_size(4) /* IFLA_GROUP */
1011 	       + nla_total_size(ext_filter_mask
1012 			        & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
1013 	       + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
1014 	       + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
1015 	       + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
1016 	       + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
1017 	       + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
1018 	       + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
1019 	       + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
1020 	       + rtnl_xdp_size() /* IFLA_XDP */
1021 	       + nla_total_size(4)  /* IFLA_EVENT */
1022 	       + nla_total_size(4)  /* IFLA_NEW_NETNSID */
1023 	       + nla_total_size(4)  /* IFLA_NEW_IFINDEX */
1024 	       + nla_total_size(1)  /* IFLA_PROTO_DOWN */
1025 	       + nla_total_size(4)  /* IFLA_TARGET_NETNSID */
1026 	       + nla_total_size(4)  /* IFLA_CARRIER_UP_COUNT */
1027 	       + nla_total_size(4)  /* IFLA_CARRIER_DOWN_COUNT */
1028 	       + nla_total_size(4)  /* IFLA_MIN_MTU */
1029 	       + nla_total_size(4)  /* IFLA_MAX_MTU */
1030 	       + 0;
1031 }
1032 
1033 static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
1034 {
1035 	struct nlattr *vf_ports;
1036 	struct nlattr *vf_port;
1037 	int vf;
1038 	int err;
1039 
1040 	vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
1041 	if (!vf_ports)
1042 		return -EMSGSIZE;
1043 
1044 	for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
1045 		vf_port = nla_nest_start(skb, IFLA_VF_PORT);
1046 		if (!vf_port)
1047 			goto nla_put_failure;
1048 		if (nla_put_u32(skb, IFLA_PORT_VF, vf))
1049 			goto nla_put_failure;
1050 		err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
1051 		if (err == -EMSGSIZE)
1052 			goto nla_put_failure;
1053 		if (err) {
1054 			nla_nest_cancel(skb, vf_port);
1055 			continue;
1056 		}
1057 		nla_nest_end(skb, vf_port);
1058 	}
1059 
1060 	nla_nest_end(skb, vf_ports);
1061 
1062 	return 0;
1063 
1064 nla_put_failure:
1065 	nla_nest_cancel(skb, vf_ports);
1066 	return -EMSGSIZE;
1067 }
1068 
1069 static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
1070 {
1071 	struct nlattr *port_self;
1072 	int err;
1073 
1074 	port_self = nla_nest_start(skb, IFLA_PORT_SELF);
1075 	if (!port_self)
1076 		return -EMSGSIZE;
1077 
1078 	err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
1079 	if (err) {
1080 		nla_nest_cancel(skb, port_self);
1081 		return (err == -EMSGSIZE) ? err : 0;
1082 	}
1083 
1084 	nla_nest_end(skb, port_self);
1085 
1086 	return 0;
1087 }
1088 
1089 static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
1090 			  u32 ext_filter_mask)
1091 {
1092 	int err;
1093 
1094 	if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
1095 	    !(ext_filter_mask & RTEXT_FILTER_VF))
1096 		return 0;
1097 
1098 	err = rtnl_port_self_fill(skb, dev);
1099 	if (err)
1100 		return err;
1101 
1102 	if (dev_num_vf(dev->dev.parent)) {
1103 		err = rtnl_vf_ports_fill(skb, dev);
1104 		if (err)
1105 			return err;
1106 	}
1107 
1108 	return 0;
1109 }
1110 
1111 static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
1112 {
1113 	int err;
1114 	struct netdev_phys_item_id ppid;
1115 
1116 	err = dev_get_phys_port_id(dev, &ppid);
1117 	if (err) {
1118 		if (err == -EOPNOTSUPP)
1119 			return 0;
1120 		return err;
1121 	}
1122 
1123 	if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
1124 		return -EMSGSIZE;
1125 
1126 	return 0;
1127 }
1128 
1129 static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1130 {
1131 	char name[IFNAMSIZ];
1132 	int err;
1133 
1134 	err = dev_get_phys_port_name(dev, name, sizeof(name));
1135 	if (err) {
1136 		if (err == -EOPNOTSUPP)
1137 			return 0;
1138 		return err;
1139 	}
1140 
1141 	if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
1142 		return -EMSGSIZE;
1143 
1144 	return 0;
1145 }
1146 
1147 static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
1148 {
1149 	int err;
1150 	struct switchdev_attr attr = {
1151 		.orig_dev = dev,
1152 		.id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
1153 		.flags = SWITCHDEV_F_NO_RECURSE,
1154 	};
1155 
1156 	err = switchdev_port_attr_get(dev, &attr);
1157 	if (err) {
1158 		if (err == -EOPNOTSUPP)
1159 			return 0;
1160 		return err;
1161 	}
1162 
1163 	if (nla_put(skb, IFLA_PHYS_SWITCH_ID, attr.u.ppid.id_len,
1164 		    attr.u.ppid.id))
1165 		return -EMSGSIZE;
1166 
1167 	return 0;
1168 }
1169 
1170 static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1171 					      struct net_device *dev)
1172 {
1173 	struct rtnl_link_stats64 *sp;
1174 	struct nlattr *attr;
1175 
1176 	attr = nla_reserve_64bit(skb, IFLA_STATS64,
1177 				 sizeof(struct rtnl_link_stats64), IFLA_PAD);
1178 	if (!attr)
1179 		return -EMSGSIZE;
1180 
1181 	sp = nla_data(attr);
1182 	dev_get_stats(dev, sp);
1183 
1184 	attr = nla_reserve(skb, IFLA_STATS,
1185 			   sizeof(struct rtnl_link_stats));
1186 	if (!attr)
1187 		return -EMSGSIZE;
1188 
1189 	copy_rtnl_link_stats(nla_data(attr), sp);
1190 
1191 	return 0;
1192 }
1193 
1194 static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1195 					       struct net_device *dev,
1196 					       int vfs_num,
1197 					       struct nlattr *vfinfo)
1198 {
1199 	struct ifla_vf_rss_query_en vf_rss_query_en;
1200 	struct nlattr *vf, *vfstats, *vfvlanlist;
1201 	struct ifla_vf_link_state vf_linkstate;
1202 	struct ifla_vf_vlan_info vf_vlan_info;
1203 	struct ifla_vf_spoofchk vf_spoofchk;
1204 	struct ifla_vf_tx_rate vf_tx_rate;
1205 	struct ifla_vf_stats vf_stats;
1206 	struct ifla_vf_trust vf_trust;
1207 	struct ifla_vf_vlan vf_vlan;
1208 	struct ifla_vf_rate vf_rate;
1209 	struct ifla_vf_mac vf_mac;
1210 	struct ifla_vf_info ivi;
1211 
1212 	memset(&ivi, 0, sizeof(ivi));
1213 
1214 	/* Not all SR-IOV capable drivers support the
1215 	 * spoofcheck and "RSS query enable" query.  Preset to
1216 	 * -1 so the user space tool can detect that the driver
1217 	 * didn't report anything.
1218 	 */
1219 	ivi.spoofchk = -1;
1220 	ivi.rss_query_en = -1;
1221 	ivi.trusted = -1;
1222 	/* The default value for VF link state is "auto"
1223 	 * IFLA_VF_LINK_STATE_AUTO which equals zero
1224 	 */
1225 	ivi.linkstate = 0;
1226 	/* VLAN Protocol by default is 802.1Q */
1227 	ivi.vlan_proto = htons(ETH_P_8021Q);
1228 	if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1229 		return 0;
1230 
1231 	memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
1232 
1233 	vf_mac.vf =
1234 		vf_vlan.vf =
1235 		vf_vlan_info.vf =
1236 		vf_rate.vf =
1237 		vf_tx_rate.vf =
1238 		vf_spoofchk.vf =
1239 		vf_linkstate.vf =
1240 		vf_rss_query_en.vf =
1241 		vf_trust.vf = ivi.vf;
1242 
1243 	memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1244 	vf_vlan.vlan = ivi.vlan;
1245 	vf_vlan.qos = ivi.qos;
1246 	vf_vlan_info.vlan = ivi.vlan;
1247 	vf_vlan_info.qos = ivi.qos;
1248 	vf_vlan_info.vlan_proto = ivi.vlan_proto;
1249 	vf_tx_rate.rate = ivi.max_tx_rate;
1250 	vf_rate.min_tx_rate = ivi.min_tx_rate;
1251 	vf_rate.max_tx_rate = ivi.max_tx_rate;
1252 	vf_spoofchk.setting = ivi.spoofchk;
1253 	vf_linkstate.link_state = ivi.linkstate;
1254 	vf_rss_query_en.setting = ivi.rss_query_en;
1255 	vf_trust.setting = ivi.trusted;
1256 	vf = nla_nest_start(skb, IFLA_VF_INFO);
1257 	if (!vf)
1258 		goto nla_put_vfinfo_failure;
1259 	if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1260 	    nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1261 	    nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1262 		    &vf_rate) ||
1263 	    nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1264 		    &vf_tx_rate) ||
1265 	    nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1266 		    &vf_spoofchk) ||
1267 	    nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1268 		    &vf_linkstate) ||
1269 	    nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1270 		    sizeof(vf_rss_query_en),
1271 		    &vf_rss_query_en) ||
1272 	    nla_put(skb, IFLA_VF_TRUST,
1273 		    sizeof(vf_trust), &vf_trust))
1274 		goto nla_put_vf_failure;
1275 	vfvlanlist = nla_nest_start(skb, IFLA_VF_VLAN_LIST);
1276 	if (!vfvlanlist)
1277 		goto nla_put_vf_failure;
1278 	if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
1279 		    &vf_vlan_info)) {
1280 		nla_nest_cancel(skb, vfvlanlist);
1281 		goto nla_put_vf_failure;
1282 	}
1283 	nla_nest_end(skb, vfvlanlist);
1284 	memset(&vf_stats, 0, sizeof(vf_stats));
1285 	if (dev->netdev_ops->ndo_get_vf_stats)
1286 		dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1287 						&vf_stats);
1288 	vfstats = nla_nest_start(skb, IFLA_VF_STATS);
1289 	if (!vfstats)
1290 		goto nla_put_vf_failure;
1291 	if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1292 			      vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1293 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1294 			      vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1295 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1296 			      vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1297 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1298 			      vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1299 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1300 			      vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1301 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
1302 			      vf_stats.multicast, IFLA_VF_STATS_PAD) ||
1303 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED,
1304 			      vf_stats.rx_dropped, IFLA_VF_STATS_PAD) ||
1305 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED,
1306 			      vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) {
1307 		nla_nest_cancel(skb, vfstats);
1308 		goto nla_put_vf_failure;
1309 	}
1310 	nla_nest_end(skb, vfstats);
1311 	nla_nest_end(skb, vf);
1312 	return 0;
1313 
1314 nla_put_vf_failure:
1315 	nla_nest_cancel(skb, vf);
1316 nla_put_vfinfo_failure:
1317 	nla_nest_cancel(skb, vfinfo);
1318 	return -EMSGSIZE;
1319 }
1320 
1321 static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb,
1322 					   struct net_device *dev,
1323 					   u32 ext_filter_mask)
1324 {
1325 	struct nlattr *vfinfo;
1326 	int i, num_vfs;
1327 
1328 	if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0))
1329 		return 0;
1330 
1331 	num_vfs = dev_num_vf(dev->dev.parent);
1332 	if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs))
1333 		return -EMSGSIZE;
1334 
1335 	if (!dev->netdev_ops->ndo_get_vf_config)
1336 		return 0;
1337 
1338 	vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1339 	if (!vfinfo)
1340 		return -EMSGSIZE;
1341 
1342 	for (i = 0; i < num_vfs; i++) {
1343 		if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
1344 			return -EMSGSIZE;
1345 	}
1346 
1347 	nla_nest_end(skb, vfinfo);
1348 	return 0;
1349 }
1350 
1351 static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1352 {
1353 	struct rtnl_link_ifmap map;
1354 
1355 	memset(&map, 0, sizeof(map));
1356 	map.mem_start   = dev->mem_start;
1357 	map.mem_end     = dev->mem_end;
1358 	map.base_addr   = dev->base_addr;
1359 	map.irq         = dev->irq;
1360 	map.dma         = dev->dma;
1361 	map.port        = dev->if_port;
1362 
1363 	if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
1364 		return -EMSGSIZE;
1365 
1366 	return 0;
1367 }
1368 
1369 static u32 rtnl_xdp_prog_skb(struct net_device *dev)
1370 {
1371 	const struct bpf_prog *generic_xdp_prog;
1372 
1373 	ASSERT_RTNL();
1374 
1375 	generic_xdp_prog = rtnl_dereference(dev->xdp_prog);
1376 	if (!generic_xdp_prog)
1377 		return 0;
1378 	return generic_xdp_prog->aux->id;
1379 }
1380 
1381 static u32 rtnl_xdp_prog_drv(struct net_device *dev)
1382 {
1383 	return __dev_xdp_query(dev, dev->netdev_ops->ndo_bpf, XDP_QUERY_PROG);
1384 }
1385 
1386 static u32 rtnl_xdp_prog_hw(struct net_device *dev)
1387 {
1388 	return __dev_xdp_query(dev, dev->netdev_ops->ndo_bpf,
1389 			       XDP_QUERY_PROG_HW);
1390 }
1391 
1392 static int rtnl_xdp_report_one(struct sk_buff *skb, struct net_device *dev,
1393 			       u32 *prog_id, u8 *mode, u8 tgt_mode, u32 attr,
1394 			       u32 (*get_prog_id)(struct net_device *dev))
1395 {
1396 	u32 curr_id;
1397 	int err;
1398 
1399 	curr_id = get_prog_id(dev);
1400 	if (!curr_id)
1401 		return 0;
1402 
1403 	*prog_id = curr_id;
1404 	err = nla_put_u32(skb, attr, curr_id);
1405 	if (err)
1406 		return err;
1407 
1408 	if (*mode != XDP_ATTACHED_NONE)
1409 		*mode = XDP_ATTACHED_MULTI;
1410 	else
1411 		*mode = tgt_mode;
1412 
1413 	return 0;
1414 }
1415 
1416 static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1417 {
1418 	struct nlattr *xdp;
1419 	u32 prog_id;
1420 	int err;
1421 	u8 mode;
1422 
1423 	xdp = nla_nest_start(skb, IFLA_XDP);
1424 	if (!xdp)
1425 		return -EMSGSIZE;
1426 
1427 	prog_id = 0;
1428 	mode = XDP_ATTACHED_NONE;
1429 	err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_SKB,
1430 				  IFLA_XDP_SKB_PROG_ID, rtnl_xdp_prog_skb);
1431 	if (err)
1432 		goto err_cancel;
1433 	err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_DRV,
1434 				  IFLA_XDP_DRV_PROG_ID, rtnl_xdp_prog_drv);
1435 	if (err)
1436 		goto err_cancel;
1437 	err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_HW,
1438 				  IFLA_XDP_HW_PROG_ID, rtnl_xdp_prog_hw);
1439 	if (err)
1440 		goto err_cancel;
1441 
1442 	err = nla_put_u8(skb, IFLA_XDP_ATTACHED, mode);
1443 	if (err)
1444 		goto err_cancel;
1445 
1446 	if (prog_id && mode != XDP_ATTACHED_MULTI) {
1447 		err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id);
1448 		if (err)
1449 			goto err_cancel;
1450 	}
1451 
1452 	nla_nest_end(skb, xdp);
1453 	return 0;
1454 
1455 err_cancel:
1456 	nla_nest_cancel(skb, xdp);
1457 	return err;
1458 }
1459 
1460 static u32 rtnl_get_event(unsigned long event)
1461 {
1462 	u32 rtnl_event_type = IFLA_EVENT_NONE;
1463 
1464 	switch (event) {
1465 	case NETDEV_REBOOT:
1466 		rtnl_event_type = IFLA_EVENT_REBOOT;
1467 		break;
1468 	case NETDEV_FEAT_CHANGE:
1469 		rtnl_event_type = IFLA_EVENT_FEATURES;
1470 		break;
1471 	case NETDEV_BONDING_FAILOVER:
1472 		rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER;
1473 		break;
1474 	case NETDEV_NOTIFY_PEERS:
1475 		rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS;
1476 		break;
1477 	case NETDEV_RESEND_IGMP:
1478 		rtnl_event_type = IFLA_EVENT_IGMP_RESEND;
1479 		break;
1480 	case NETDEV_CHANGEINFODATA:
1481 		rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS;
1482 		break;
1483 	default:
1484 		break;
1485 	}
1486 
1487 	return rtnl_event_type;
1488 }
1489 
1490 static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev)
1491 {
1492 	const struct net_device *upper_dev;
1493 	int ret = 0;
1494 
1495 	rcu_read_lock();
1496 
1497 	upper_dev = netdev_master_upper_dev_get_rcu(dev);
1498 	if (upper_dev)
1499 		ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex);
1500 
1501 	rcu_read_unlock();
1502 	return ret;
1503 }
1504 
1505 static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev)
1506 {
1507 	int ifindex = dev_get_iflink(dev);
1508 
1509 	if (dev->ifindex == ifindex)
1510 		return 0;
1511 
1512 	return nla_put_u32(skb, IFLA_LINK, ifindex);
1513 }
1514 
1515 static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb,
1516 					      struct net_device *dev)
1517 {
1518 	char buf[IFALIASZ];
1519 	int ret;
1520 
1521 	ret = dev_get_alias(dev, buf, sizeof(buf));
1522 	return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0;
1523 }
1524 
1525 static int rtnl_fill_link_netnsid(struct sk_buff *skb,
1526 				  const struct net_device *dev,
1527 				  struct net *src_net)
1528 {
1529 	if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) {
1530 		struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1531 
1532 		if (!net_eq(dev_net(dev), link_net)) {
1533 			int id = peernet2id_alloc(src_net, link_net);
1534 
1535 			if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1536 				return -EMSGSIZE;
1537 		}
1538 	}
1539 
1540 	return 0;
1541 }
1542 
1543 static int rtnl_fill_link_af(struct sk_buff *skb,
1544 			     const struct net_device *dev,
1545 			     u32 ext_filter_mask)
1546 {
1547 	const struct rtnl_af_ops *af_ops;
1548 	struct nlattr *af_spec;
1549 
1550 	af_spec = nla_nest_start(skb, IFLA_AF_SPEC);
1551 	if (!af_spec)
1552 		return -EMSGSIZE;
1553 
1554 	list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
1555 		struct nlattr *af;
1556 		int err;
1557 
1558 		if (!af_ops->fill_link_af)
1559 			continue;
1560 
1561 		af = nla_nest_start(skb, af_ops->family);
1562 		if (!af)
1563 			return -EMSGSIZE;
1564 
1565 		err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
1566 		/*
1567 		 * Caller may return ENODATA to indicate that there
1568 		 * was no data to be dumped. This is not an error, it
1569 		 * means we should trim the attribute header and
1570 		 * continue.
1571 		 */
1572 		if (err == -ENODATA)
1573 			nla_nest_cancel(skb, af);
1574 		else if (err < 0)
1575 			return -EMSGSIZE;
1576 
1577 		nla_nest_end(skb, af);
1578 	}
1579 
1580 	nla_nest_end(skb, af_spec);
1581 	return 0;
1582 }
1583 
1584 static int rtnl_fill_ifinfo(struct sk_buff *skb,
1585 			    struct net_device *dev, struct net *src_net,
1586 			    int type, u32 pid, u32 seq, u32 change,
1587 			    unsigned int flags, u32 ext_filter_mask,
1588 			    u32 event, int *new_nsid, int new_ifindex,
1589 			    int tgt_netnsid)
1590 {
1591 	struct ifinfomsg *ifm;
1592 	struct nlmsghdr *nlh;
1593 
1594 	ASSERT_RTNL();
1595 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1596 	if (nlh == NULL)
1597 		return -EMSGSIZE;
1598 
1599 	ifm = nlmsg_data(nlh);
1600 	ifm->ifi_family = AF_UNSPEC;
1601 	ifm->__ifi_pad = 0;
1602 	ifm->ifi_type = dev->type;
1603 	ifm->ifi_index = dev->ifindex;
1604 	ifm->ifi_flags = dev_get_flags(dev);
1605 	ifm->ifi_change = change;
1606 
1607 	if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_TARGET_NETNSID, tgt_netnsid))
1608 		goto nla_put_failure;
1609 
1610 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1611 	    nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1612 	    nla_put_u8(skb, IFLA_OPERSTATE,
1613 		       netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1614 	    nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1615 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1616 	    nla_put_u32(skb, IFLA_MIN_MTU, dev->min_mtu) ||
1617 	    nla_put_u32(skb, IFLA_MAX_MTU, dev->max_mtu) ||
1618 	    nla_put_u32(skb, IFLA_GROUP, dev->group) ||
1619 	    nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
1620 	    nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
1621 	    nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1622 	    nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
1623 #ifdef CONFIG_RPS
1624 	    nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
1625 #endif
1626 	    nla_put_iflink(skb, dev) ||
1627 	    put_master_ifindex(skb, dev) ||
1628 	    nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
1629 	    (dev->qdisc &&
1630 	     nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
1631 	    nla_put_ifalias(skb, dev) ||
1632 	    nla_put_u32(skb, IFLA_CARRIER_CHANGES,
1633 			atomic_read(&dev->carrier_up_count) +
1634 			atomic_read(&dev->carrier_down_count)) ||
1635 	    nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down) ||
1636 	    nla_put_u32(skb, IFLA_CARRIER_UP_COUNT,
1637 			atomic_read(&dev->carrier_up_count)) ||
1638 	    nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT,
1639 			atomic_read(&dev->carrier_down_count)))
1640 		goto nla_put_failure;
1641 
1642 	if (event != IFLA_EVENT_NONE) {
1643 		if (nla_put_u32(skb, IFLA_EVENT, event))
1644 			goto nla_put_failure;
1645 	}
1646 
1647 	if (rtnl_fill_link_ifmap(skb, dev))
1648 		goto nla_put_failure;
1649 
1650 	if (dev->addr_len) {
1651 		if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1652 		    nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1653 			goto nla_put_failure;
1654 	}
1655 
1656 	if (rtnl_phys_port_id_fill(skb, dev))
1657 		goto nla_put_failure;
1658 
1659 	if (rtnl_phys_port_name_fill(skb, dev))
1660 		goto nla_put_failure;
1661 
1662 	if (rtnl_phys_switch_id_fill(skb, dev))
1663 		goto nla_put_failure;
1664 
1665 	if (rtnl_fill_stats(skb, dev))
1666 		goto nla_put_failure;
1667 
1668 	if (rtnl_fill_vf(skb, dev, ext_filter_mask))
1669 		goto nla_put_failure;
1670 
1671 	if (rtnl_port_fill(skb, dev, ext_filter_mask))
1672 		goto nla_put_failure;
1673 
1674 	if (rtnl_xdp_fill(skb, dev))
1675 		goto nla_put_failure;
1676 
1677 	if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
1678 		if (rtnl_link_fill(skb, dev) < 0)
1679 			goto nla_put_failure;
1680 	}
1681 
1682 	if (rtnl_fill_link_netnsid(skb, dev, src_net))
1683 		goto nla_put_failure;
1684 
1685 	if (new_nsid &&
1686 	    nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0)
1687 		goto nla_put_failure;
1688 	if (new_ifindex &&
1689 	    nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0)
1690 		goto nla_put_failure;
1691 
1692 
1693 	rcu_read_lock();
1694 	if (rtnl_fill_link_af(skb, dev, ext_filter_mask))
1695 		goto nla_put_failure_rcu;
1696 	rcu_read_unlock();
1697 
1698 	nlmsg_end(skb, nlh);
1699 	return 0;
1700 
1701 nla_put_failure_rcu:
1702 	rcu_read_unlock();
1703 nla_put_failure:
1704 	nlmsg_cancel(skb, nlh);
1705 	return -EMSGSIZE;
1706 }
1707 
1708 static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1709 	[IFLA_IFNAME]		= { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1710 	[IFLA_ADDRESS]		= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1711 	[IFLA_BROADCAST]	= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1712 	[IFLA_MAP]		= { .len = sizeof(struct rtnl_link_ifmap) },
1713 	[IFLA_MTU]		= { .type = NLA_U32 },
1714 	[IFLA_LINK]		= { .type = NLA_U32 },
1715 	[IFLA_MASTER]		= { .type = NLA_U32 },
1716 	[IFLA_CARRIER]		= { .type = NLA_U8 },
1717 	[IFLA_TXQLEN]		= { .type = NLA_U32 },
1718 	[IFLA_WEIGHT]		= { .type = NLA_U32 },
1719 	[IFLA_OPERSTATE]	= { .type = NLA_U8 },
1720 	[IFLA_LINKMODE]		= { .type = NLA_U8 },
1721 	[IFLA_LINKINFO]		= { .type = NLA_NESTED },
1722 	[IFLA_NET_NS_PID]	= { .type = NLA_U32 },
1723 	[IFLA_NET_NS_FD]	= { .type = NLA_U32 },
1724 	/* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
1725 	 * allow 0-length string (needed to remove an alias).
1726 	 */
1727 	[IFLA_IFALIAS]	        = { .type = NLA_BINARY, .len = IFALIASZ - 1 },
1728 	[IFLA_VFINFO_LIST]	= {. type = NLA_NESTED },
1729 	[IFLA_VF_PORTS]		= { .type = NLA_NESTED },
1730 	[IFLA_PORT_SELF]	= { .type = NLA_NESTED },
1731 	[IFLA_AF_SPEC]		= { .type = NLA_NESTED },
1732 	[IFLA_EXT_MASK]		= { .type = NLA_U32 },
1733 	[IFLA_PROMISCUITY]	= { .type = NLA_U32 },
1734 	[IFLA_NUM_TX_QUEUES]	= { .type = NLA_U32 },
1735 	[IFLA_NUM_RX_QUEUES]	= { .type = NLA_U32 },
1736 	[IFLA_GSO_MAX_SEGS]	= { .type = NLA_U32 },
1737 	[IFLA_GSO_MAX_SIZE]	= { .type = NLA_U32 },
1738 	[IFLA_PHYS_PORT_ID]	= { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1739 	[IFLA_CARRIER_CHANGES]	= { .type = NLA_U32 },  /* ignored */
1740 	[IFLA_PHYS_SWITCH_ID]	= { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1741 	[IFLA_LINK_NETNSID]	= { .type = NLA_S32 },
1742 	[IFLA_PROTO_DOWN]	= { .type = NLA_U8 },
1743 	[IFLA_XDP]		= { .type = NLA_NESTED },
1744 	[IFLA_EVENT]		= { .type = NLA_U32 },
1745 	[IFLA_GROUP]		= { .type = NLA_U32 },
1746 	[IFLA_TARGET_NETNSID]	= { .type = NLA_S32 },
1747 	[IFLA_CARRIER_UP_COUNT]	= { .type = NLA_U32 },
1748 	[IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 },
1749 	[IFLA_MIN_MTU]		= { .type = NLA_U32 },
1750 	[IFLA_MAX_MTU]		= { .type = NLA_U32 },
1751 };
1752 
1753 static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1754 	[IFLA_INFO_KIND]	= { .type = NLA_STRING },
1755 	[IFLA_INFO_DATA]	= { .type = NLA_NESTED },
1756 	[IFLA_INFO_SLAVE_KIND]	= { .type = NLA_STRING },
1757 	[IFLA_INFO_SLAVE_DATA]	= { .type = NLA_NESTED },
1758 };
1759 
1760 static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1761 	[IFLA_VF_MAC]		= { .len = sizeof(struct ifla_vf_mac) },
1762 	[IFLA_VF_VLAN]		= { .len = sizeof(struct ifla_vf_vlan) },
1763 	[IFLA_VF_VLAN_LIST]     = { .type = NLA_NESTED },
1764 	[IFLA_VF_TX_RATE]	= { .len = sizeof(struct ifla_vf_tx_rate) },
1765 	[IFLA_VF_SPOOFCHK]	= { .len = sizeof(struct ifla_vf_spoofchk) },
1766 	[IFLA_VF_RATE]		= { .len = sizeof(struct ifla_vf_rate) },
1767 	[IFLA_VF_LINK_STATE]	= { .len = sizeof(struct ifla_vf_link_state) },
1768 	[IFLA_VF_RSS_QUERY_EN]	= { .len = sizeof(struct ifla_vf_rss_query_en) },
1769 	[IFLA_VF_STATS]		= { .type = NLA_NESTED },
1770 	[IFLA_VF_TRUST]		= { .len = sizeof(struct ifla_vf_trust) },
1771 	[IFLA_VF_IB_NODE_GUID]	= { .len = sizeof(struct ifla_vf_guid) },
1772 	[IFLA_VF_IB_PORT_GUID]	= { .len = sizeof(struct ifla_vf_guid) },
1773 };
1774 
1775 static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1776 	[IFLA_PORT_VF]		= { .type = NLA_U32 },
1777 	[IFLA_PORT_PROFILE]	= { .type = NLA_STRING,
1778 				    .len = PORT_PROFILE_MAX },
1779 	[IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1780 				      .len = PORT_UUID_MAX },
1781 	[IFLA_PORT_HOST_UUID]	= { .type = NLA_STRING,
1782 				    .len = PORT_UUID_MAX },
1783 	[IFLA_PORT_REQUEST]	= { .type = NLA_U8, },
1784 	[IFLA_PORT_RESPONSE]	= { .type = NLA_U16, },
1785 
1786 	/* Unused, but we need to keep it here since user space could
1787 	 * fill it. It's also broken with regard to NLA_BINARY use in
1788 	 * combination with structs.
1789 	 */
1790 	[IFLA_PORT_VSI_TYPE]	= { .type = NLA_BINARY,
1791 				    .len = sizeof(struct ifla_port_vsi) },
1792 };
1793 
1794 static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
1795 	[IFLA_XDP_FD]		= { .type = NLA_S32 },
1796 	[IFLA_XDP_ATTACHED]	= { .type = NLA_U8 },
1797 	[IFLA_XDP_FLAGS]	= { .type = NLA_U32 },
1798 	[IFLA_XDP_PROG_ID]	= { .type = NLA_U32 },
1799 };
1800 
1801 static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1802 {
1803 	const struct rtnl_link_ops *ops = NULL;
1804 	struct nlattr *linfo[IFLA_INFO_MAX + 1];
1805 
1806 	if (nla_parse_nested(linfo, IFLA_INFO_MAX, nla,
1807 			     ifla_info_policy, NULL) < 0)
1808 		return NULL;
1809 
1810 	if (linfo[IFLA_INFO_KIND]) {
1811 		char kind[MODULE_NAME_LEN];
1812 
1813 		nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1814 		ops = rtnl_link_ops_get(kind);
1815 	}
1816 
1817 	return ops;
1818 }
1819 
1820 static bool link_master_filtered(struct net_device *dev, int master_idx)
1821 {
1822 	struct net_device *master;
1823 
1824 	if (!master_idx)
1825 		return false;
1826 
1827 	master = netdev_master_upper_dev_get(dev);
1828 	if (!master || master->ifindex != master_idx)
1829 		return true;
1830 
1831 	return false;
1832 }
1833 
1834 static bool link_kind_filtered(const struct net_device *dev,
1835 			       const struct rtnl_link_ops *kind_ops)
1836 {
1837 	if (kind_ops && dev->rtnl_link_ops != kind_ops)
1838 		return true;
1839 
1840 	return false;
1841 }
1842 
1843 static bool link_dump_filtered(struct net_device *dev,
1844 			       int master_idx,
1845 			       const struct rtnl_link_ops *kind_ops)
1846 {
1847 	if (link_master_filtered(dev, master_idx) ||
1848 	    link_kind_filtered(dev, kind_ops))
1849 		return true;
1850 
1851 	return false;
1852 }
1853 
1854 /**
1855  * rtnl_get_net_ns_capable - Get netns if sufficiently privileged.
1856  * @sk: netlink socket
1857  * @netnsid: network namespace identifier
1858  *
1859  * Returns the network namespace identified by netnsid on success or an error
1860  * pointer on failure.
1861  */
1862 struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid)
1863 {
1864 	struct net *net;
1865 
1866 	net = get_net_ns_by_id(sock_net(sk), netnsid);
1867 	if (!net)
1868 		return ERR_PTR(-EINVAL);
1869 
1870 	/* For now, the caller is required to have CAP_NET_ADMIN in
1871 	 * the user namespace owning the target net ns.
1872 	 */
1873 	if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) {
1874 		put_net(net);
1875 		return ERR_PTR(-EACCES);
1876 	}
1877 	return net;
1878 }
1879 EXPORT_SYMBOL_GPL(rtnl_get_net_ns_capable);
1880 
1881 static int rtnl_valid_dump_ifinfo_req(const struct nlmsghdr *nlh,
1882 				      bool strict_check, struct nlattr **tb,
1883 				      struct netlink_ext_ack *extack)
1884 {
1885 	int hdrlen;
1886 
1887 	if (strict_check) {
1888 		struct ifinfomsg *ifm;
1889 
1890 		if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
1891 			NL_SET_ERR_MSG(extack, "Invalid header for link dump");
1892 			return -EINVAL;
1893 		}
1894 
1895 		ifm = nlmsg_data(nlh);
1896 		if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
1897 		    ifm->ifi_change) {
1898 			NL_SET_ERR_MSG(extack, "Invalid values in header for link dump request");
1899 			return -EINVAL;
1900 		}
1901 		if (ifm->ifi_index) {
1902 			NL_SET_ERR_MSG(extack, "Filter by device index not supported for link dumps");
1903 			return -EINVAL;
1904 		}
1905 
1906 		return nlmsg_parse_strict(nlh, sizeof(*ifm), tb, IFLA_MAX,
1907 					  ifla_policy, extack);
1908 	}
1909 
1910 	/* A hack to preserve kernel<->userspace interface.
1911 	 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1912 	 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1913 	 * what iproute2 < v3.9.0 used.
1914 	 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1915 	 * attribute, its netlink message is shorter than struct ifinfomsg.
1916 	 */
1917 	hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
1918 		 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1919 
1920 	return nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, extack);
1921 }
1922 
1923 static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
1924 {
1925 	struct netlink_ext_ack *extack = cb->extack;
1926 	const struct nlmsghdr *nlh = cb->nlh;
1927 	struct net *net = sock_net(skb->sk);
1928 	struct net *tgt_net = net;
1929 	int h, s_h;
1930 	int idx = 0, s_idx;
1931 	struct net_device *dev;
1932 	struct hlist_head *head;
1933 	struct nlattr *tb[IFLA_MAX+1];
1934 	u32 ext_filter_mask = 0;
1935 	const struct rtnl_link_ops *kind_ops = NULL;
1936 	unsigned int flags = NLM_F_MULTI;
1937 	int master_idx = 0;
1938 	int netnsid = -1;
1939 	int err, i;
1940 
1941 	s_h = cb->args[0];
1942 	s_idx = cb->args[1];
1943 
1944 	err = rtnl_valid_dump_ifinfo_req(nlh, cb->strict_check, tb, extack);
1945 	if (err < 0) {
1946 		if (cb->strict_check)
1947 			return err;
1948 
1949 		goto walk_entries;
1950 	}
1951 
1952 	for (i = 0; i <= IFLA_MAX; ++i) {
1953 		if (!tb[i])
1954 			continue;
1955 
1956 		/* new attributes should only be added with strict checking */
1957 		switch (i) {
1958 		case IFLA_TARGET_NETNSID:
1959 			netnsid = nla_get_s32(tb[i]);
1960 			tgt_net = rtnl_get_net_ns_capable(skb->sk, netnsid);
1961 			if (IS_ERR(tgt_net)) {
1962 				NL_SET_ERR_MSG(extack, "Invalid target network namespace id");
1963 				return PTR_ERR(tgt_net);
1964 			}
1965 			break;
1966 		case IFLA_EXT_MASK:
1967 			ext_filter_mask = nla_get_u32(tb[i]);
1968 			break;
1969 		case IFLA_MASTER:
1970 			master_idx = nla_get_u32(tb[i]);
1971 			break;
1972 		case IFLA_LINKINFO:
1973 			kind_ops = linkinfo_to_kind_ops(tb[i]);
1974 			break;
1975 		default:
1976 			if (cb->strict_check) {
1977 				NL_SET_ERR_MSG(extack, "Unsupported attribute in link dump request");
1978 				return -EINVAL;
1979 			}
1980 		}
1981 	}
1982 
1983 	if (master_idx || kind_ops)
1984 		flags |= NLM_F_DUMP_FILTERED;
1985 
1986 walk_entries:
1987 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1988 		idx = 0;
1989 		head = &tgt_net->dev_index_head[h];
1990 		hlist_for_each_entry(dev, head, index_hlist) {
1991 			if (link_dump_filtered(dev, master_idx, kind_ops))
1992 				goto cont;
1993 			if (idx < s_idx)
1994 				goto cont;
1995 			err = rtnl_fill_ifinfo(skb, dev, net,
1996 					       RTM_NEWLINK,
1997 					       NETLINK_CB(cb->skb).portid,
1998 					       nlh->nlmsg_seq, 0, flags,
1999 					       ext_filter_mask, 0, NULL, 0,
2000 					       netnsid);
2001 
2002 			if (err < 0) {
2003 				if (likely(skb->len))
2004 					goto out;
2005 
2006 				goto out_err;
2007 			}
2008 cont:
2009 			idx++;
2010 		}
2011 	}
2012 out:
2013 	err = skb->len;
2014 out_err:
2015 	cb->args[1] = idx;
2016 	cb->args[0] = h;
2017 	cb->seq = net->dev_base_seq;
2018 	nl_dump_check_consistent(cb, nlmsg_hdr(skb));
2019 	if (netnsid >= 0)
2020 		put_net(tgt_net);
2021 
2022 	return err;
2023 }
2024 
2025 int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len,
2026 			struct netlink_ext_ack *exterr)
2027 {
2028 	return nla_parse(tb, IFLA_MAX, head, len, ifla_policy, exterr);
2029 }
2030 EXPORT_SYMBOL(rtnl_nla_parse_ifla);
2031 
2032 struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
2033 {
2034 	struct net *net;
2035 	/* Examine the link attributes and figure out which
2036 	 * network namespace we are talking about.
2037 	 */
2038 	if (tb[IFLA_NET_NS_PID])
2039 		net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
2040 	else if (tb[IFLA_NET_NS_FD])
2041 		net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
2042 	else
2043 		net = get_net(src_net);
2044 	return net;
2045 }
2046 EXPORT_SYMBOL(rtnl_link_get_net);
2047 
2048 /* Figure out which network namespace we are talking about by
2049  * examining the link attributes in the following order:
2050  *
2051  * 1. IFLA_NET_NS_PID
2052  * 2. IFLA_NET_NS_FD
2053  * 3. IFLA_TARGET_NETNSID
2054  */
2055 static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net,
2056 					       struct nlattr *tb[])
2057 {
2058 	struct net *net;
2059 
2060 	if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])
2061 		return rtnl_link_get_net(src_net, tb);
2062 
2063 	if (!tb[IFLA_TARGET_NETNSID])
2064 		return get_net(src_net);
2065 
2066 	net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_TARGET_NETNSID]));
2067 	if (!net)
2068 		return ERR_PTR(-EINVAL);
2069 
2070 	return net;
2071 }
2072 
2073 static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb,
2074 					     struct net *src_net,
2075 					     struct nlattr *tb[], int cap)
2076 {
2077 	struct net *net;
2078 
2079 	net = rtnl_link_get_net_by_nlattr(src_net, tb);
2080 	if (IS_ERR(net))
2081 		return net;
2082 
2083 	if (!netlink_ns_capable(skb, net->user_ns, cap)) {
2084 		put_net(net);
2085 		return ERR_PTR(-EPERM);
2086 	}
2087 
2088 	return net;
2089 }
2090 
2091 /* Verify that rtnetlink requests do not pass additional properties
2092  * potentially referring to different network namespaces.
2093  */
2094 static int rtnl_ensure_unique_netns(struct nlattr *tb[],
2095 				    struct netlink_ext_ack *extack,
2096 				    bool netns_id_only)
2097 {
2098 
2099 	if (netns_id_only) {
2100 		if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD])
2101 			return 0;
2102 
2103 		NL_SET_ERR_MSG(extack, "specified netns attribute not supported");
2104 		return -EOPNOTSUPP;
2105 	}
2106 
2107 	if (tb[IFLA_TARGET_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]))
2108 		goto invalid_attr;
2109 
2110 	if (tb[IFLA_NET_NS_PID] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_FD]))
2111 		goto invalid_attr;
2112 
2113 	if (tb[IFLA_NET_NS_FD] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_PID]))
2114 		goto invalid_attr;
2115 
2116 	return 0;
2117 
2118 invalid_attr:
2119 	NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified");
2120 	return -EINVAL;
2121 }
2122 
2123 static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
2124 {
2125 	if (dev) {
2126 		if (tb[IFLA_ADDRESS] &&
2127 		    nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
2128 			return -EINVAL;
2129 
2130 		if (tb[IFLA_BROADCAST] &&
2131 		    nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
2132 			return -EINVAL;
2133 	}
2134 
2135 	if (tb[IFLA_AF_SPEC]) {
2136 		struct nlattr *af;
2137 		int rem, err;
2138 
2139 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2140 			const struct rtnl_af_ops *af_ops;
2141 
2142 			rcu_read_lock();
2143 			af_ops = rtnl_af_lookup(nla_type(af));
2144 			if (!af_ops) {
2145 				rcu_read_unlock();
2146 				return -EAFNOSUPPORT;
2147 			}
2148 
2149 			if (!af_ops->set_link_af) {
2150 				rcu_read_unlock();
2151 				return -EOPNOTSUPP;
2152 			}
2153 
2154 			if (af_ops->validate_link_af) {
2155 				err = af_ops->validate_link_af(dev, af);
2156 				if (err < 0) {
2157 					rcu_read_unlock();
2158 					return err;
2159 				}
2160 			}
2161 
2162 			rcu_read_unlock();
2163 		}
2164 	}
2165 
2166 	return 0;
2167 }
2168 
2169 static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
2170 				  int guid_type)
2171 {
2172 	const struct net_device_ops *ops = dev->netdev_ops;
2173 
2174 	return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
2175 }
2176 
2177 static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
2178 {
2179 	if (dev->type != ARPHRD_INFINIBAND)
2180 		return -EOPNOTSUPP;
2181 
2182 	return handle_infiniband_guid(dev, ivt, guid_type);
2183 }
2184 
2185 static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
2186 {
2187 	const struct net_device_ops *ops = dev->netdev_ops;
2188 	int err = -EINVAL;
2189 
2190 	if (tb[IFLA_VF_MAC]) {
2191 		struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
2192 
2193 		err = -EOPNOTSUPP;
2194 		if (ops->ndo_set_vf_mac)
2195 			err = ops->ndo_set_vf_mac(dev, ivm->vf,
2196 						  ivm->mac);
2197 		if (err < 0)
2198 			return err;
2199 	}
2200 
2201 	if (tb[IFLA_VF_VLAN]) {
2202 		struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
2203 
2204 		err = -EOPNOTSUPP;
2205 		if (ops->ndo_set_vf_vlan)
2206 			err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
2207 						   ivv->qos,
2208 						   htons(ETH_P_8021Q));
2209 		if (err < 0)
2210 			return err;
2211 	}
2212 
2213 	if (tb[IFLA_VF_VLAN_LIST]) {
2214 		struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
2215 		struct nlattr *attr;
2216 		int rem, len = 0;
2217 
2218 		err = -EOPNOTSUPP;
2219 		if (!ops->ndo_set_vf_vlan)
2220 			return err;
2221 
2222 		nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
2223 			if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
2224 			    nla_len(attr) < NLA_HDRLEN) {
2225 				return -EINVAL;
2226 			}
2227 			if (len >= MAX_VLAN_LIST_LEN)
2228 				return -EOPNOTSUPP;
2229 			ivvl[len] = nla_data(attr);
2230 
2231 			len++;
2232 		}
2233 		if (len == 0)
2234 			return -EINVAL;
2235 
2236 		err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
2237 					   ivvl[0]->qos, ivvl[0]->vlan_proto);
2238 		if (err < 0)
2239 			return err;
2240 	}
2241 
2242 	if (tb[IFLA_VF_TX_RATE]) {
2243 		struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
2244 		struct ifla_vf_info ivf;
2245 
2246 		err = -EOPNOTSUPP;
2247 		if (ops->ndo_get_vf_config)
2248 			err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
2249 		if (err < 0)
2250 			return err;
2251 
2252 		err = -EOPNOTSUPP;
2253 		if (ops->ndo_set_vf_rate)
2254 			err = ops->ndo_set_vf_rate(dev, ivt->vf,
2255 						   ivf.min_tx_rate,
2256 						   ivt->rate);
2257 		if (err < 0)
2258 			return err;
2259 	}
2260 
2261 	if (tb[IFLA_VF_RATE]) {
2262 		struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
2263 
2264 		err = -EOPNOTSUPP;
2265 		if (ops->ndo_set_vf_rate)
2266 			err = ops->ndo_set_vf_rate(dev, ivt->vf,
2267 						   ivt->min_tx_rate,
2268 						   ivt->max_tx_rate);
2269 		if (err < 0)
2270 			return err;
2271 	}
2272 
2273 	if (tb[IFLA_VF_SPOOFCHK]) {
2274 		struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
2275 
2276 		err = -EOPNOTSUPP;
2277 		if (ops->ndo_set_vf_spoofchk)
2278 			err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
2279 						       ivs->setting);
2280 		if (err < 0)
2281 			return err;
2282 	}
2283 
2284 	if (tb[IFLA_VF_LINK_STATE]) {
2285 		struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
2286 
2287 		err = -EOPNOTSUPP;
2288 		if (ops->ndo_set_vf_link_state)
2289 			err = ops->ndo_set_vf_link_state(dev, ivl->vf,
2290 							 ivl->link_state);
2291 		if (err < 0)
2292 			return err;
2293 	}
2294 
2295 	if (tb[IFLA_VF_RSS_QUERY_EN]) {
2296 		struct ifla_vf_rss_query_en *ivrssq_en;
2297 
2298 		err = -EOPNOTSUPP;
2299 		ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
2300 		if (ops->ndo_set_vf_rss_query_en)
2301 			err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
2302 							   ivrssq_en->setting);
2303 		if (err < 0)
2304 			return err;
2305 	}
2306 
2307 	if (tb[IFLA_VF_TRUST]) {
2308 		struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
2309 
2310 		err = -EOPNOTSUPP;
2311 		if (ops->ndo_set_vf_trust)
2312 			err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
2313 		if (err < 0)
2314 			return err;
2315 	}
2316 
2317 	if (tb[IFLA_VF_IB_NODE_GUID]) {
2318 		struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
2319 
2320 		if (!ops->ndo_set_vf_guid)
2321 			return -EOPNOTSUPP;
2322 
2323 		return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
2324 	}
2325 
2326 	if (tb[IFLA_VF_IB_PORT_GUID]) {
2327 		struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
2328 
2329 		if (!ops->ndo_set_vf_guid)
2330 			return -EOPNOTSUPP;
2331 
2332 		return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
2333 	}
2334 
2335 	return err;
2336 }
2337 
2338 static int do_set_master(struct net_device *dev, int ifindex,
2339 			 struct netlink_ext_ack *extack)
2340 {
2341 	struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
2342 	const struct net_device_ops *ops;
2343 	int err;
2344 
2345 	if (upper_dev) {
2346 		if (upper_dev->ifindex == ifindex)
2347 			return 0;
2348 		ops = upper_dev->netdev_ops;
2349 		if (ops->ndo_del_slave) {
2350 			err = ops->ndo_del_slave(upper_dev, dev);
2351 			if (err)
2352 				return err;
2353 		} else {
2354 			return -EOPNOTSUPP;
2355 		}
2356 	}
2357 
2358 	if (ifindex) {
2359 		upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
2360 		if (!upper_dev)
2361 			return -EINVAL;
2362 		ops = upper_dev->netdev_ops;
2363 		if (ops->ndo_add_slave) {
2364 			err = ops->ndo_add_slave(upper_dev, dev, extack);
2365 			if (err)
2366 				return err;
2367 		} else {
2368 			return -EOPNOTSUPP;
2369 		}
2370 	}
2371 	return 0;
2372 }
2373 
2374 #define DO_SETLINK_MODIFIED	0x01
2375 /* notify flag means notify + modified. */
2376 #define DO_SETLINK_NOTIFY	0x03
2377 static int do_setlink(const struct sk_buff *skb,
2378 		      struct net_device *dev, struct ifinfomsg *ifm,
2379 		      struct netlink_ext_ack *extack,
2380 		      struct nlattr **tb, char *ifname, int status)
2381 {
2382 	const struct net_device_ops *ops = dev->netdev_ops;
2383 	int err;
2384 
2385 	err = validate_linkmsg(dev, tb);
2386 	if (err < 0)
2387 		return err;
2388 
2389 	if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_TARGET_NETNSID]) {
2390 		struct net *net = rtnl_link_get_net_capable(skb, dev_net(dev),
2391 							    tb, CAP_NET_ADMIN);
2392 		if (IS_ERR(net)) {
2393 			err = PTR_ERR(net);
2394 			goto errout;
2395 		}
2396 
2397 		err = dev_change_net_namespace(dev, net, ifname);
2398 		put_net(net);
2399 		if (err)
2400 			goto errout;
2401 		status |= DO_SETLINK_MODIFIED;
2402 	}
2403 
2404 	if (tb[IFLA_MAP]) {
2405 		struct rtnl_link_ifmap *u_map;
2406 		struct ifmap k_map;
2407 
2408 		if (!ops->ndo_set_config) {
2409 			err = -EOPNOTSUPP;
2410 			goto errout;
2411 		}
2412 
2413 		if (!netif_device_present(dev)) {
2414 			err = -ENODEV;
2415 			goto errout;
2416 		}
2417 
2418 		u_map = nla_data(tb[IFLA_MAP]);
2419 		k_map.mem_start = (unsigned long) u_map->mem_start;
2420 		k_map.mem_end = (unsigned long) u_map->mem_end;
2421 		k_map.base_addr = (unsigned short) u_map->base_addr;
2422 		k_map.irq = (unsigned char) u_map->irq;
2423 		k_map.dma = (unsigned char) u_map->dma;
2424 		k_map.port = (unsigned char) u_map->port;
2425 
2426 		err = ops->ndo_set_config(dev, &k_map);
2427 		if (err < 0)
2428 			goto errout;
2429 
2430 		status |= DO_SETLINK_NOTIFY;
2431 	}
2432 
2433 	if (tb[IFLA_ADDRESS]) {
2434 		struct sockaddr *sa;
2435 		int len;
2436 
2437 		len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
2438 						  sizeof(*sa));
2439 		sa = kmalloc(len, GFP_KERNEL);
2440 		if (!sa) {
2441 			err = -ENOMEM;
2442 			goto errout;
2443 		}
2444 		sa->sa_family = dev->type;
2445 		memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
2446 		       dev->addr_len);
2447 		err = dev_set_mac_address(dev, sa);
2448 		kfree(sa);
2449 		if (err)
2450 			goto errout;
2451 		status |= DO_SETLINK_MODIFIED;
2452 	}
2453 
2454 	if (tb[IFLA_MTU]) {
2455 		err = dev_set_mtu_ext(dev, nla_get_u32(tb[IFLA_MTU]), extack);
2456 		if (err < 0)
2457 			goto errout;
2458 		status |= DO_SETLINK_MODIFIED;
2459 	}
2460 
2461 	if (tb[IFLA_GROUP]) {
2462 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
2463 		status |= DO_SETLINK_NOTIFY;
2464 	}
2465 
2466 	/*
2467 	 * Interface selected by interface index but interface
2468 	 * name provided implies that a name change has been
2469 	 * requested.
2470 	 */
2471 	if (ifm->ifi_index > 0 && ifname[0]) {
2472 		err = dev_change_name(dev, ifname);
2473 		if (err < 0)
2474 			goto errout;
2475 		status |= DO_SETLINK_MODIFIED;
2476 	}
2477 
2478 	if (tb[IFLA_IFALIAS]) {
2479 		err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
2480 				    nla_len(tb[IFLA_IFALIAS]));
2481 		if (err < 0)
2482 			goto errout;
2483 		status |= DO_SETLINK_NOTIFY;
2484 	}
2485 
2486 	if (tb[IFLA_BROADCAST]) {
2487 		nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
2488 		call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
2489 	}
2490 
2491 	if (ifm->ifi_flags || ifm->ifi_change) {
2492 		err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
2493 		if (err < 0)
2494 			goto errout;
2495 	}
2496 
2497 	if (tb[IFLA_MASTER]) {
2498 		err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
2499 		if (err)
2500 			goto errout;
2501 		status |= DO_SETLINK_MODIFIED;
2502 	}
2503 
2504 	if (tb[IFLA_CARRIER]) {
2505 		err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
2506 		if (err)
2507 			goto errout;
2508 		status |= DO_SETLINK_MODIFIED;
2509 	}
2510 
2511 	if (tb[IFLA_TXQLEN]) {
2512 		unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]);
2513 
2514 		err = dev_change_tx_queue_len(dev, value);
2515 		if (err)
2516 			goto errout;
2517 		status |= DO_SETLINK_MODIFIED;
2518 	}
2519 
2520 	if (tb[IFLA_GSO_MAX_SIZE]) {
2521 		u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]);
2522 
2523 		if (max_size > GSO_MAX_SIZE) {
2524 			err = -EINVAL;
2525 			goto errout;
2526 		}
2527 
2528 		if (dev->gso_max_size ^ max_size) {
2529 			netif_set_gso_max_size(dev, max_size);
2530 			status |= DO_SETLINK_MODIFIED;
2531 		}
2532 	}
2533 
2534 	if (tb[IFLA_GSO_MAX_SEGS]) {
2535 		u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
2536 
2537 		if (max_segs > GSO_MAX_SEGS) {
2538 			err = -EINVAL;
2539 			goto errout;
2540 		}
2541 
2542 		if (dev->gso_max_segs ^ max_segs) {
2543 			dev->gso_max_segs = max_segs;
2544 			status |= DO_SETLINK_MODIFIED;
2545 		}
2546 	}
2547 
2548 	if (tb[IFLA_OPERSTATE])
2549 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
2550 
2551 	if (tb[IFLA_LINKMODE]) {
2552 		unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
2553 
2554 		write_lock_bh(&dev_base_lock);
2555 		if (dev->link_mode ^ value)
2556 			status |= DO_SETLINK_NOTIFY;
2557 		dev->link_mode = value;
2558 		write_unlock_bh(&dev_base_lock);
2559 	}
2560 
2561 	if (tb[IFLA_VFINFO_LIST]) {
2562 		struct nlattr *vfinfo[IFLA_VF_MAX + 1];
2563 		struct nlattr *attr;
2564 		int rem;
2565 
2566 		nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
2567 			if (nla_type(attr) != IFLA_VF_INFO ||
2568 			    nla_len(attr) < NLA_HDRLEN) {
2569 				err = -EINVAL;
2570 				goto errout;
2571 			}
2572 			err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr,
2573 					       ifla_vf_policy, NULL);
2574 			if (err < 0)
2575 				goto errout;
2576 			err = do_setvfinfo(dev, vfinfo);
2577 			if (err < 0)
2578 				goto errout;
2579 			status |= DO_SETLINK_NOTIFY;
2580 		}
2581 	}
2582 	err = 0;
2583 
2584 	if (tb[IFLA_VF_PORTS]) {
2585 		struct nlattr *port[IFLA_PORT_MAX+1];
2586 		struct nlattr *attr;
2587 		int vf;
2588 		int rem;
2589 
2590 		err = -EOPNOTSUPP;
2591 		if (!ops->ndo_set_vf_port)
2592 			goto errout;
2593 
2594 		nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
2595 			if (nla_type(attr) != IFLA_VF_PORT ||
2596 			    nla_len(attr) < NLA_HDRLEN) {
2597 				err = -EINVAL;
2598 				goto errout;
2599 			}
2600 			err = nla_parse_nested(port, IFLA_PORT_MAX, attr,
2601 					       ifla_port_policy, NULL);
2602 			if (err < 0)
2603 				goto errout;
2604 			if (!port[IFLA_PORT_VF]) {
2605 				err = -EOPNOTSUPP;
2606 				goto errout;
2607 			}
2608 			vf = nla_get_u32(port[IFLA_PORT_VF]);
2609 			err = ops->ndo_set_vf_port(dev, vf, port);
2610 			if (err < 0)
2611 				goto errout;
2612 			status |= DO_SETLINK_NOTIFY;
2613 		}
2614 	}
2615 	err = 0;
2616 
2617 	if (tb[IFLA_PORT_SELF]) {
2618 		struct nlattr *port[IFLA_PORT_MAX+1];
2619 
2620 		err = nla_parse_nested(port, IFLA_PORT_MAX,
2621 				       tb[IFLA_PORT_SELF], ifla_port_policy,
2622 				       NULL);
2623 		if (err < 0)
2624 			goto errout;
2625 
2626 		err = -EOPNOTSUPP;
2627 		if (ops->ndo_set_vf_port)
2628 			err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
2629 		if (err < 0)
2630 			goto errout;
2631 		status |= DO_SETLINK_NOTIFY;
2632 	}
2633 
2634 	if (tb[IFLA_AF_SPEC]) {
2635 		struct nlattr *af;
2636 		int rem;
2637 
2638 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2639 			const struct rtnl_af_ops *af_ops;
2640 
2641 			rcu_read_lock();
2642 
2643 			BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af))));
2644 
2645 			err = af_ops->set_link_af(dev, af);
2646 			if (err < 0) {
2647 				rcu_read_unlock();
2648 				goto errout;
2649 			}
2650 
2651 			rcu_read_unlock();
2652 			status |= DO_SETLINK_NOTIFY;
2653 		}
2654 	}
2655 	err = 0;
2656 
2657 	if (tb[IFLA_PROTO_DOWN]) {
2658 		err = dev_change_proto_down(dev,
2659 					    nla_get_u8(tb[IFLA_PROTO_DOWN]));
2660 		if (err)
2661 			goto errout;
2662 		status |= DO_SETLINK_NOTIFY;
2663 	}
2664 
2665 	if (tb[IFLA_XDP]) {
2666 		struct nlattr *xdp[IFLA_XDP_MAX + 1];
2667 		u32 xdp_flags = 0;
2668 
2669 		err = nla_parse_nested(xdp, IFLA_XDP_MAX, tb[IFLA_XDP],
2670 				       ifla_xdp_policy, NULL);
2671 		if (err < 0)
2672 			goto errout;
2673 
2674 		if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
2675 			err = -EINVAL;
2676 			goto errout;
2677 		}
2678 
2679 		if (xdp[IFLA_XDP_FLAGS]) {
2680 			xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
2681 			if (xdp_flags & ~XDP_FLAGS_MASK) {
2682 				err = -EINVAL;
2683 				goto errout;
2684 			}
2685 			if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) {
2686 				err = -EINVAL;
2687 				goto errout;
2688 			}
2689 		}
2690 
2691 		if (xdp[IFLA_XDP_FD]) {
2692 			err = dev_change_xdp_fd(dev, extack,
2693 						nla_get_s32(xdp[IFLA_XDP_FD]),
2694 						xdp_flags);
2695 			if (err)
2696 				goto errout;
2697 			status |= DO_SETLINK_NOTIFY;
2698 		}
2699 	}
2700 
2701 errout:
2702 	if (status & DO_SETLINK_MODIFIED) {
2703 		if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY)
2704 			netdev_state_change(dev);
2705 
2706 		if (err < 0)
2707 			net_warn_ratelimited("A link change request failed with some changes committed already. Interface %s may have been left with an inconsistent configuration, please check.\n",
2708 					     dev->name);
2709 	}
2710 
2711 	return err;
2712 }
2713 
2714 static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2715 			struct netlink_ext_ack *extack)
2716 {
2717 	struct net *net = sock_net(skb->sk);
2718 	struct ifinfomsg *ifm;
2719 	struct net_device *dev;
2720 	int err;
2721 	struct nlattr *tb[IFLA_MAX+1];
2722 	char ifname[IFNAMSIZ];
2723 
2724 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy,
2725 			  extack);
2726 	if (err < 0)
2727 		goto errout;
2728 
2729 	err = rtnl_ensure_unique_netns(tb, extack, false);
2730 	if (err < 0)
2731 		goto errout;
2732 
2733 	if (tb[IFLA_IFNAME])
2734 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2735 	else
2736 		ifname[0] = '\0';
2737 
2738 	err = -EINVAL;
2739 	ifm = nlmsg_data(nlh);
2740 	if (ifm->ifi_index > 0)
2741 		dev = __dev_get_by_index(net, ifm->ifi_index);
2742 	else if (tb[IFLA_IFNAME])
2743 		dev = __dev_get_by_name(net, ifname);
2744 	else
2745 		goto errout;
2746 
2747 	if (dev == NULL) {
2748 		err = -ENODEV;
2749 		goto errout;
2750 	}
2751 
2752 	err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0);
2753 errout:
2754 	return err;
2755 }
2756 
2757 static int rtnl_group_dellink(const struct net *net, int group)
2758 {
2759 	struct net_device *dev, *aux;
2760 	LIST_HEAD(list_kill);
2761 	bool found = false;
2762 
2763 	if (!group)
2764 		return -EPERM;
2765 
2766 	for_each_netdev(net, dev) {
2767 		if (dev->group == group) {
2768 			const struct rtnl_link_ops *ops;
2769 
2770 			found = true;
2771 			ops = dev->rtnl_link_ops;
2772 			if (!ops || !ops->dellink)
2773 				return -EOPNOTSUPP;
2774 		}
2775 	}
2776 
2777 	if (!found)
2778 		return -ENODEV;
2779 
2780 	for_each_netdev_safe(net, dev, aux) {
2781 		if (dev->group == group) {
2782 			const struct rtnl_link_ops *ops;
2783 
2784 			ops = dev->rtnl_link_ops;
2785 			ops->dellink(dev, &list_kill);
2786 		}
2787 	}
2788 	unregister_netdevice_many(&list_kill);
2789 
2790 	return 0;
2791 }
2792 
2793 int rtnl_delete_link(struct net_device *dev)
2794 {
2795 	const struct rtnl_link_ops *ops;
2796 	LIST_HEAD(list_kill);
2797 
2798 	ops = dev->rtnl_link_ops;
2799 	if (!ops || !ops->dellink)
2800 		return -EOPNOTSUPP;
2801 
2802 	ops->dellink(dev, &list_kill);
2803 	unregister_netdevice_many(&list_kill);
2804 
2805 	return 0;
2806 }
2807 EXPORT_SYMBOL_GPL(rtnl_delete_link);
2808 
2809 static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
2810 			struct netlink_ext_ack *extack)
2811 {
2812 	struct net *net = sock_net(skb->sk);
2813 	struct net *tgt_net = net;
2814 	struct net_device *dev = NULL;
2815 	struct ifinfomsg *ifm;
2816 	char ifname[IFNAMSIZ];
2817 	struct nlattr *tb[IFLA_MAX+1];
2818 	int err;
2819 	int netnsid = -1;
2820 
2821 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
2822 	if (err < 0)
2823 		return err;
2824 
2825 	err = rtnl_ensure_unique_netns(tb, extack, true);
2826 	if (err < 0)
2827 		return err;
2828 
2829 	if (tb[IFLA_IFNAME])
2830 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2831 
2832 	if (tb[IFLA_TARGET_NETNSID]) {
2833 		netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
2834 		tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
2835 		if (IS_ERR(tgt_net))
2836 			return PTR_ERR(tgt_net);
2837 	}
2838 
2839 	err = -EINVAL;
2840 	ifm = nlmsg_data(nlh);
2841 	if (ifm->ifi_index > 0)
2842 		dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
2843 	else if (tb[IFLA_IFNAME])
2844 		dev = __dev_get_by_name(tgt_net, ifname);
2845 	else if (tb[IFLA_GROUP])
2846 		err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP]));
2847 	else
2848 		goto out;
2849 
2850 	if (!dev) {
2851 		if (tb[IFLA_IFNAME] || ifm->ifi_index > 0)
2852 			err = -ENODEV;
2853 
2854 		goto out;
2855 	}
2856 
2857 	err = rtnl_delete_link(dev);
2858 
2859 out:
2860 	if (netnsid >= 0)
2861 		put_net(tgt_net);
2862 
2863 	return err;
2864 }
2865 
2866 int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
2867 {
2868 	unsigned int old_flags;
2869 	int err;
2870 
2871 	old_flags = dev->flags;
2872 	if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
2873 		err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
2874 		if (err < 0)
2875 			return err;
2876 	}
2877 
2878 	if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) {
2879 		__dev_notify_flags(dev, old_flags, (old_flags ^ dev->flags));
2880 	} else {
2881 		dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
2882 		__dev_notify_flags(dev, old_flags, ~0U);
2883 	}
2884 	return 0;
2885 }
2886 EXPORT_SYMBOL(rtnl_configure_link);
2887 
2888 struct net_device *rtnl_create_link(struct net *net,
2889 	const char *ifname, unsigned char name_assign_type,
2890 	const struct rtnl_link_ops *ops, struct nlattr *tb[])
2891 {
2892 	struct net_device *dev;
2893 	unsigned int num_tx_queues = 1;
2894 	unsigned int num_rx_queues = 1;
2895 
2896 	if (tb[IFLA_NUM_TX_QUEUES])
2897 		num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
2898 	else if (ops->get_num_tx_queues)
2899 		num_tx_queues = ops->get_num_tx_queues();
2900 
2901 	if (tb[IFLA_NUM_RX_QUEUES])
2902 		num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
2903 	else if (ops->get_num_rx_queues)
2904 		num_rx_queues = ops->get_num_rx_queues();
2905 
2906 	if (num_tx_queues < 1 || num_tx_queues > 4096)
2907 		return ERR_PTR(-EINVAL);
2908 
2909 	if (num_rx_queues < 1 || num_rx_queues > 4096)
2910 		return ERR_PTR(-EINVAL);
2911 
2912 	dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
2913 			       ops->setup, num_tx_queues, num_rx_queues);
2914 	if (!dev)
2915 		return ERR_PTR(-ENOMEM);
2916 
2917 	dev_net_set(dev, net);
2918 	dev->rtnl_link_ops = ops;
2919 	dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
2920 
2921 	if (tb[IFLA_MTU])
2922 		dev->mtu = nla_get_u32(tb[IFLA_MTU]);
2923 	if (tb[IFLA_ADDRESS]) {
2924 		memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
2925 				nla_len(tb[IFLA_ADDRESS]));
2926 		dev->addr_assign_type = NET_ADDR_SET;
2927 	}
2928 	if (tb[IFLA_BROADCAST])
2929 		memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
2930 				nla_len(tb[IFLA_BROADCAST]));
2931 	if (tb[IFLA_TXQLEN])
2932 		dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
2933 	if (tb[IFLA_OPERSTATE])
2934 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
2935 	if (tb[IFLA_LINKMODE])
2936 		dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
2937 	if (tb[IFLA_GROUP])
2938 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
2939 	if (tb[IFLA_GSO_MAX_SIZE])
2940 		netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE]));
2941 	if (tb[IFLA_GSO_MAX_SEGS])
2942 		dev->gso_max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
2943 
2944 	return dev;
2945 }
2946 EXPORT_SYMBOL(rtnl_create_link);
2947 
2948 static int rtnl_group_changelink(const struct sk_buff *skb,
2949 		struct net *net, int group,
2950 		struct ifinfomsg *ifm,
2951 		struct netlink_ext_ack *extack,
2952 		struct nlattr **tb)
2953 {
2954 	struct net_device *dev, *aux;
2955 	int err;
2956 
2957 	for_each_netdev_safe(net, dev, aux) {
2958 		if (dev->group == group) {
2959 			err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0);
2960 			if (err < 0)
2961 				return err;
2962 		}
2963 	}
2964 
2965 	return 0;
2966 }
2967 
2968 static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2969 			struct netlink_ext_ack *extack)
2970 {
2971 	struct net *net = sock_net(skb->sk);
2972 	const struct rtnl_link_ops *ops;
2973 	const struct rtnl_link_ops *m_ops = NULL;
2974 	struct net_device *dev;
2975 	struct net_device *master_dev = NULL;
2976 	struct ifinfomsg *ifm;
2977 	char kind[MODULE_NAME_LEN];
2978 	char ifname[IFNAMSIZ];
2979 	struct nlattr *tb[IFLA_MAX+1];
2980 	struct nlattr *linkinfo[IFLA_INFO_MAX+1];
2981 	unsigned char name_assign_type = NET_NAME_USER;
2982 	int err;
2983 
2984 #ifdef CONFIG_MODULES
2985 replay:
2986 #endif
2987 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
2988 	if (err < 0)
2989 		return err;
2990 
2991 	err = rtnl_ensure_unique_netns(tb, extack, false);
2992 	if (err < 0)
2993 		return err;
2994 
2995 	if (tb[IFLA_IFNAME])
2996 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2997 	else
2998 		ifname[0] = '\0';
2999 
3000 	ifm = nlmsg_data(nlh);
3001 	if (ifm->ifi_index > 0)
3002 		dev = __dev_get_by_index(net, ifm->ifi_index);
3003 	else {
3004 		if (ifname[0])
3005 			dev = __dev_get_by_name(net, ifname);
3006 		else
3007 			dev = NULL;
3008 	}
3009 
3010 	if (dev) {
3011 		master_dev = netdev_master_upper_dev_get(dev);
3012 		if (master_dev)
3013 			m_ops = master_dev->rtnl_link_ops;
3014 	}
3015 
3016 	err = validate_linkmsg(dev, tb);
3017 	if (err < 0)
3018 		return err;
3019 
3020 	if (tb[IFLA_LINKINFO]) {
3021 		err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
3022 				       tb[IFLA_LINKINFO], ifla_info_policy,
3023 				       NULL);
3024 		if (err < 0)
3025 			return err;
3026 	} else
3027 		memset(linkinfo, 0, sizeof(linkinfo));
3028 
3029 	if (linkinfo[IFLA_INFO_KIND]) {
3030 		nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
3031 		ops = rtnl_link_ops_get(kind);
3032 	} else {
3033 		kind[0] = '\0';
3034 		ops = NULL;
3035 	}
3036 
3037 	if (1) {
3038 		struct nlattr *attr[RTNL_MAX_TYPE + 1];
3039 		struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1];
3040 		struct nlattr **data = NULL;
3041 		struct nlattr **slave_data = NULL;
3042 		struct net *dest_net, *link_net = NULL;
3043 
3044 		if (ops) {
3045 			if (ops->maxtype > RTNL_MAX_TYPE)
3046 				return -EINVAL;
3047 
3048 			if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
3049 				err = nla_parse_nested(attr, ops->maxtype,
3050 						       linkinfo[IFLA_INFO_DATA],
3051 						       ops->policy, NULL);
3052 				if (err < 0)
3053 					return err;
3054 				data = attr;
3055 			}
3056 			if (ops->validate) {
3057 				err = ops->validate(tb, data, extack);
3058 				if (err < 0)
3059 					return err;
3060 			}
3061 		}
3062 
3063 		if (m_ops) {
3064 			if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE)
3065 				return -EINVAL;
3066 
3067 			if (m_ops->slave_maxtype &&
3068 			    linkinfo[IFLA_INFO_SLAVE_DATA]) {
3069 				err = nla_parse_nested(slave_attr,
3070 						       m_ops->slave_maxtype,
3071 						       linkinfo[IFLA_INFO_SLAVE_DATA],
3072 						       m_ops->slave_policy,
3073 						       NULL);
3074 				if (err < 0)
3075 					return err;
3076 				slave_data = slave_attr;
3077 			}
3078 		}
3079 
3080 		if (dev) {
3081 			int status = 0;
3082 
3083 			if (nlh->nlmsg_flags & NLM_F_EXCL)
3084 				return -EEXIST;
3085 			if (nlh->nlmsg_flags & NLM_F_REPLACE)
3086 				return -EOPNOTSUPP;
3087 
3088 			if (linkinfo[IFLA_INFO_DATA]) {
3089 				if (!ops || ops != dev->rtnl_link_ops ||
3090 				    !ops->changelink)
3091 					return -EOPNOTSUPP;
3092 
3093 				err = ops->changelink(dev, tb, data, extack);
3094 				if (err < 0)
3095 					return err;
3096 				status |= DO_SETLINK_NOTIFY;
3097 			}
3098 
3099 			if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
3100 				if (!m_ops || !m_ops->slave_changelink)
3101 					return -EOPNOTSUPP;
3102 
3103 				err = m_ops->slave_changelink(master_dev, dev,
3104 							      tb, slave_data,
3105 							      extack);
3106 				if (err < 0)
3107 					return err;
3108 				status |= DO_SETLINK_NOTIFY;
3109 			}
3110 
3111 			return do_setlink(skb, dev, ifm, extack, tb, ifname,
3112 					  status);
3113 		}
3114 
3115 		if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
3116 			if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
3117 				return rtnl_group_changelink(skb, net,
3118 						nla_get_u32(tb[IFLA_GROUP]),
3119 						ifm, extack, tb);
3120 			return -ENODEV;
3121 		}
3122 
3123 		if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
3124 			return -EOPNOTSUPP;
3125 
3126 		if (!ops) {
3127 #ifdef CONFIG_MODULES
3128 			if (kind[0]) {
3129 				__rtnl_unlock();
3130 				request_module("rtnl-link-%s", kind);
3131 				rtnl_lock();
3132 				ops = rtnl_link_ops_get(kind);
3133 				if (ops)
3134 					goto replay;
3135 			}
3136 #endif
3137 			return -EOPNOTSUPP;
3138 		}
3139 
3140 		if (!ops->setup)
3141 			return -EOPNOTSUPP;
3142 
3143 		if (!ifname[0]) {
3144 			snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
3145 			name_assign_type = NET_NAME_ENUM;
3146 		}
3147 
3148 		dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN);
3149 		if (IS_ERR(dest_net))
3150 			return PTR_ERR(dest_net);
3151 
3152 		if (tb[IFLA_LINK_NETNSID]) {
3153 			int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
3154 
3155 			link_net = get_net_ns_by_id(dest_net, id);
3156 			if (!link_net) {
3157 				err =  -EINVAL;
3158 				goto out;
3159 			}
3160 			err = -EPERM;
3161 			if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
3162 				goto out;
3163 		}
3164 
3165 		dev = rtnl_create_link(link_net ? : dest_net, ifname,
3166 				       name_assign_type, ops, tb);
3167 		if (IS_ERR(dev)) {
3168 			err = PTR_ERR(dev);
3169 			goto out;
3170 		}
3171 
3172 		dev->ifindex = ifm->ifi_index;
3173 
3174 		if (ops->newlink) {
3175 			err = ops->newlink(link_net ? : net, dev, tb, data,
3176 					   extack);
3177 			/* Drivers should call free_netdev() in ->destructor
3178 			 * and unregister it on failure after registration
3179 			 * so that device could be finally freed in rtnl_unlock.
3180 			 */
3181 			if (err < 0) {
3182 				/* If device is not registered at all, free it now */
3183 				if (dev->reg_state == NETREG_UNINITIALIZED)
3184 					free_netdev(dev);
3185 				goto out;
3186 			}
3187 		} else {
3188 			err = register_netdevice(dev);
3189 			if (err < 0) {
3190 				free_netdev(dev);
3191 				goto out;
3192 			}
3193 		}
3194 		err = rtnl_configure_link(dev, ifm);
3195 		if (err < 0)
3196 			goto out_unregister;
3197 		if (link_net) {
3198 			err = dev_change_net_namespace(dev, dest_net, ifname);
3199 			if (err < 0)
3200 				goto out_unregister;
3201 		}
3202 		if (tb[IFLA_MASTER]) {
3203 			err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]),
3204 					    extack);
3205 			if (err)
3206 				goto out_unregister;
3207 		}
3208 out:
3209 		if (link_net)
3210 			put_net(link_net);
3211 		put_net(dest_net);
3212 		return err;
3213 out_unregister:
3214 		if (ops->newlink) {
3215 			LIST_HEAD(list_kill);
3216 
3217 			ops->dellink(dev, &list_kill);
3218 			unregister_netdevice_many(&list_kill);
3219 		} else {
3220 			unregister_netdevice(dev);
3221 		}
3222 		goto out;
3223 	}
3224 }
3225 
3226 static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3227 			struct netlink_ext_ack *extack)
3228 {
3229 	struct net *net = sock_net(skb->sk);
3230 	struct net *tgt_net = net;
3231 	struct ifinfomsg *ifm;
3232 	char ifname[IFNAMSIZ];
3233 	struct nlattr *tb[IFLA_MAX+1];
3234 	struct net_device *dev = NULL;
3235 	struct sk_buff *nskb;
3236 	int netnsid = -1;
3237 	int err;
3238 	u32 ext_filter_mask = 0;
3239 
3240 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
3241 	if (err < 0)
3242 		return err;
3243 
3244 	err = rtnl_ensure_unique_netns(tb, extack, true);
3245 	if (err < 0)
3246 		return err;
3247 
3248 	if (tb[IFLA_TARGET_NETNSID]) {
3249 		netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
3250 		tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
3251 		if (IS_ERR(tgt_net))
3252 			return PTR_ERR(tgt_net);
3253 	}
3254 
3255 	if (tb[IFLA_IFNAME])
3256 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
3257 
3258 	if (tb[IFLA_EXT_MASK])
3259 		ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3260 
3261 	err = -EINVAL;
3262 	ifm = nlmsg_data(nlh);
3263 	if (ifm->ifi_index > 0)
3264 		dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
3265 	else if (tb[IFLA_IFNAME])
3266 		dev = __dev_get_by_name(tgt_net, ifname);
3267 	else
3268 		goto out;
3269 
3270 	err = -ENODEV;
3271 	if (dev == NULL)
3272 		goto out;
3273 
3274 	err = -ENOBUFS;
3275 	nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
3276 	if (nskb == NULL)
3277 		goto out;
3278 
3279 	err = rtnl_fill_ifinfo(nskb, dev, net,
3280 			       RTM_NEWLINK, NETLINK_CB(skb).portid,
3281 			       nlh->nlmsg_seq, 0, 0, ext_filter_mask,
3282 			       0, NULL, 0, netnsid);
3283 	if (err < 0) {
3284 		/* -EMSGSIZE implies BUG in if_nlmsg_size */
3285 		WARN_ON(err == -EMSGSIZE);
3286 		kfree_skb(nskb);
3287 	} else
3288 		err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
3289 out:
3290 	if (netnsid >= 0)
3291 		put_net(tgt_net);
3292 
3293 	return err;
3294 }
3295 
3296 static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
3297 {
3298 	struct net *net = sock_net(skb->sk);
3299 	struct net_device *dev;
3300 	struct nlattr *tb[IFLA_MAX+1];
3301 	u32 ext_filter_mask = 0;
3302 	u16 min_ifinfo_dump_size = 0;
3303 	int hdrlen;
3304 
3305 	/* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
3306 	hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
3307 		 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
3308 
3309 	if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
3310 		if (tb[IFLA_EXT_MASK])
3311 			ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3312 	}
3313 
3314 	if (!ext_filter_mask)
3315 		return NLMSG_GOODSIZE;
3316 	/*
3317 	 * traverse the list of net devices and compute the minimum
3318 	 * buffer size based upon the filter mask.
3319 	 */
3320 	rcu_read_lock();
3321 	for_each_netdev_rcu(net, dev) {
3322 		min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
3323 					     if_nlmsg_size(dev,
3324 						           ext_filter_mask));
3325 	}
3326 	rcu_read_unlock();
3327 
3328 	return nlmsg_total_size(min_ifinfo_dump_size);
3329 }
3330 
3331 static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
3332 {
3333 	int idx;
3334 	int s_idx = cb->family;
3335 	int type = cb->nlh->nlmsg_type - RTM_BASE;
3336 	int ret = 0;
3337 
3338 	if (s_idx == 0)
3339 		s_idx = 1;
3340 
3341 	for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
3342 		struct rtnl_link **tab;
3343 		struct rtnl_link *link;
3344 		rtnl_dumpit_func dumpit;
3345 
3346 		if (idx < s_idx || idx == PF_PACKET)
3347 			continue;
3348 
3349 		if (type < 0 || type >= RTM_NR_MSGTYPES)
3350 			continue;
3351 
3352 		tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]);
3353 		if (!tab)
3354 			continue;
3355 
3356 		link = tab[type];
3357 		if (!link)
3358 			continue;
3359 
3360 		dumpit = link->dumpit;
3361 		if (!dumpit)
3362 			continue;
3363 
3364 		if (idx > s_idx) {
3365 			memset(&cb->args[0], 0, sizeof(cb->args));
3366 			cb->prev_seq = 0;
3367 			cb->seq = 0;
3368 		}
3369 		ret = dumpit(skb, cb);
3370 		if (ret)
3371 			break;
3372 	}
3373 	cb->family = idx;
3374 
3375 	return skb->len ? : ret;
3376 }
3377 
3378 struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
3379 				       unsigned int change,
3380 				       u32 event, gfp_t flags, int *new_nsid,
3381 				       int new_ifindex)
3382 {
3383 	struct net *net = dev_net(dev);
3384 	struct sk_buff *skb;
3385 	int err = -ENOBUFS;
3386 	size_t if_info_size;
3387 
3388 	skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
3389 	if (skb == NULL)
3390 		goto errout;
3391 
3392 	err = rtnl_fill_ifinfo(skb, dev, dev_net(dev),
3393 			       type, 0, 0, change, 0, 0, event,
3394 			       new_nsid, new_ifindex, -1);
3395 	if (err < 0) {
3396 		/* -EMSGSIZE implies BUG in if_nlmsg_size() */
3397 		WARN_ON(err == -EMSGSIZE);
3398 		kfree_skb(skb);
3399 		goto errout;
3400 	}
3401 	return skb;
3402 errout:
3403 	if (err < 0)
3404 		rtnl_set_sk_err(net, RTNLGRP_LINK, err);
3405 	return NULL;
3406 }
3407 
3408 void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
3409 {
3410 	struct net *net = dev_net(dev);
3411 
3412 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
3413 }
3414 
3415 static void rtmsg_ifinfo_event(int type, struct net_device *dev,
3416 			       unsigned int change, u32 event,
3417 			       gfp_t flags, int *new_nsid, int new_ifindex)
3418 {
3419 	struct sk_buff *skb;
3420 
3421 	if (dev->reg_state != NETREG_REGISTERED)
3422 		return;
3423 
3424 	skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid,
3425 				     new_ifindex);
3426 	if (skb)
3427 		rtmsg_ifinfo_send(skb, dev, flags);
3428 }
3429 
3430 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
3431 		  gfp_t flags)
3432 {
3433 	rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
3434 			   NULL, 0);
3435 }
3436 
3437 void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change,
3438 			 gfp_t flags, int *new_nsid, int new_ifindex)
3439 {
3440 	rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
3441 			   new_nsid, new_ifindex);
3442 }
3443 
3444 static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
3445 				   struct net_device *dev,
3446 				   u8 *addr, u16 vid, u32 pid, u32 seq,
3447 				   int type, unsigned int flags,
3448 				   int nlflags, u16 ndm_state)
3449 {
3450 	struct nlmsghdr *nlh;
3451 	struct ndmsg *ndm;
3452 
3453 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
3454 	if (!nlh)
3455 		return -EMSGSIZE;
3456 
3457 	ndm = nlmsg_data(nlh);
3458 	ndm->ndm_family  = AF_BRIDGE;
3459 	ndm->ndm_pad1	 = 0;
3460 	ndm->ndm_pad2    = 0;
3461 	ndm->ndm_flags	 = flags;
3462 	ndm->ndm_type	 = 0;
3463 	ndm->ndm_ifindex = dev->ifindex;
3464 	ndm->ndm_state   = ndm_state;
3465 
3466 	if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
3467 		goto nla_put_failure;
3468 	if (vid)
3469 		if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
3470 			goto nla_put_failure;
3471 
3472 	nlmsg_end(skb, nlh);
3473 	return 0;
3474 
3475 nla_put_failure:
3476 	nlmsg_cancel(skb, nlh);
3477 	return -EMSGSIZE;
3478 }
3479 
3480 static inline size_t rtnl_fdb_nlmsg_size(void)
3481 {
3482 	return NLMSG_ALIGN(sizeof(struct ndmsg)) +
3483 	       nla_total_size(ETH_ALEN) +	/* NDA_LLADDR */
3484 	       nla_total_size(sizeof(u16)) +	/* NDA_VLAN */
3485 	       0;
3486 }
3487 
3488 static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
3489 			    u16 ndm_state)
3490 {
3491 	struct net *net = dev_net(dev);
3492 	struct sk_buff *skb;
3493 	int err = -ENOBUFS;
3494 
3495 	skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
3496 	if (!skb)
3497 		goto errout;
3498 
3499 	err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
3500 				      0, 0, type, NTF_SELF, 0, ndm_state);
3501 	if (err < 0) {
3502 		kfree_skb(skb);
3503 		goto errout;
3504 	}
3505 
3506 	rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
3507 	return;
3508 errout:
3509 	rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
3510 }
3511 
3512 /**
3513  * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
3514  */
3515 int ndo_dflt_fdb_add(struct ndmsg *ndm,
3516 		     struct nlattr *tb[],
3517 		     struct net_device *dev,
3518 		     const unsigned char *addr, u16 vid,
3519 		     u16 flags)
3520 {
3521 	int err = -EINVAL;
3522 
3523 	/* If aging addresses are supported device will need to
3524 	 * implement its own handler for this.
3525 	 */
3526 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
3527 		pr_info("%s: FDB only supports static addresses\n", dev->name);
3528 		return err;
3529 	}
3530 
3531 	if (vid) {
3532 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
3533 		return err;
3534 	}
3535 
3536 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3537 		err = dev_uc_add_excl(dev, addr);
3538 	else if (is_multicast_ether_addr(addr))
3539 		err = dev_mc_add_excl(dev, addr);
3540 
3541 	/* Only return duplicate errors if NLM_F_EXCL is set */
3542 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
3543 		err = 0;
3544 
3545 	return err;
3546 }
3547 EXPORT_SYMBOL(ndo_dflt_fdb_add);
3548 
3549 static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid,
3550 			 struct netlink_ext_ack *extack)
3551 {
3552 	u16 vid = 0;
3553 
3554 	if (vlan_attr) {
3555 		if (nla_len(vlan_attr) != sizeof(u16)) {
3556 			NL_SET_ERR_MSG(extack, "invalid vlan attribute size");
3557 			return -EINVAL;
3558 		}
3559 
3560 		vid = nla_get_u16(vlan_attr);
3561 
3562 		if (!vid || vid >= VLAN_VID_MASK) {
3563 			NL_SET_ERR_MSG(extack, "invalid vlan id");
3564 			return -EINVAL;
3565 		}
3566 	}
3567 	*p_vid = vid;
3568 	return 0;
3569 }
3570 
3571 static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
3572 			struct netlink_ext_ack *extack)
3573 {
3574 	struct net *net = sock_net(skb->sk);
3575 	struct ndmsg *ndm;
3576 	struct nlattr *tb[NDA_MAX+1];
3577 	struct net_device *dev;
3578 	u8 *addr;
3579 	u16 vid;
3580 	int err;
3581 
3582 	err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
3583 	if (err < 0)
3584 		return err;
3585 
3586 	ndm = nlmsg_data(nlh);
3587 	if (ndm->ndm_ifindex == 0) {
3588 		NL_SET_ERR_MSG(extack, "invalid ifindex");
3589 		return -EINVAL;
3590 	}
3591 
3592 	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3593 	if (dev == NULL) {
3594 		NL_SET_ERR_MSG(extack, "unknown ifindex");
3595 		return -ENODEV;
3596 	}
3597 
3598 	if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
3599 		NL_SET_ERR_MSG(extack, "invalid address");
3600 		return -EINVAL;
3601 	}
3602 
3603 	if (dev->type != ARPHRD_ETHER) {
3604 		NL_SET_ERR_MSG(extack, "FDB add only supported for Ethernet devices");
3605 		return -EINVAL;
3606 	}
3607 
3608 	addr = nla_data(tb[NDA_LLADDR]);
3609 
3610 	err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
3611 	if (err)
3612 		return err;
3613 
3614 	err = -EOPNOTSUPP;
3615 
3616 	/* Support fdb on master device the net/bridge default case */
3617 	if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3618 	    (dev->priv_flags & IFF_BRIDGE_PORT)) {
3619 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3620 		const struct net_device_ops *ops = br_dev->netdev_ops;
3621 
3622 		err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
3623 				       nlh->nlmsg_flags);
3624 		if (err)
3625 			goto out;
3626 		else
3627 			ndm->ndm_flags &= ~NTF_MASTER;
3628 	}
3629 
3630 	/* Embedded bridge, macvlan, and any other device support */
3631 	if ((ndm->ndm_flags & NTF_SELF)) {
3632 		if (dev->netdev_ops->ndo_fdb_add)
3633 			err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
3634 							   vid,
3635 							   nlh->nlmsg_flags);
3636 		else
3637 			err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
3638 					       nlh->nlmsg_flags);
3639 
3640 		if (!err) {
3641 			rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
3642 					ndm->ndm_state);
3643 			ndm->ndm_flags &= ~NTF_SELF;
3644 		}
3645 	}
3646 out:
3647 	return err;
3648 }
3649 
3650 /**
3651  * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
3652  */
3653 int ndo_dflt_fdb_del(struct ndmsg *ndm,
3654 		     struct nlattr *tb[],
3655 		     struct net_device *dev,
3656 		     const unsigned char *addr, u16 vid)
3657 {
3658 	int err = -EINVAL;
3659 
3660 	/* If aging addresses are supported device will need to
3661 	 * implement its own handler for this.
3662 	 */
3663 	if (!(ndm->ndm_state & NUD_PERMANENT)) {
3664 		pr_info("%s: FDB only supports static addresses\n", dev->name);
3665 		return err;
3666 	}
3667 
3668 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3669 		err = dev_uc_del(dev, addr);
3670 	else if (is_multicast_ether_addr(addr))
3671 		err = dev_mc_del(dev, addr);
3672 
3673 	return err;
3674 }
3675 EXPORT_SYMBOL(ndo_dflt_fdb_del);
3676 
3677 static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
3678 			struct netlink_ext_ack *extack)
3679 {
3680 	struct net *net = sock_net(skb->sk);
3681 	struct ndmsg *ndm;
3682 	struct nlattr *tb[NDA_MAX+1];
3683 	struct net_device *dev;
3684 	int err = -EINVAL;
3685 	__u8 *addr;
3686 	u16 vid;
3687 
3688 	if (!netlink_capable(skb, CAP_NET_ADMIN))
3689 		return -EPERM;
3690 
3691 	err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
3692 	if (err < 0)
3693 		return err;
3694 
3695 	ndm = nlmsg_data(nlh);
3696 	if (ndm->ndm_ifindex == 0) {
3697 		NL_SET_ERR_MSG(extack, "invalid ifindex");
3698 		return -EINVAL;
3699 	}
3700 
3701 	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3702 	if (dev == NULL) {
3703 		NL_SET_ERR_MSG(extack, "unknown ifindex");
3704 		return -ENODEV;
3705 	}
3706 
3707 	if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
3708 		NL_SET_ERR_MSG(extack, "invalid address");
3709 		return -EINVAL;
3710 	}
3711 
3712 	if (dev->type != ARPHRD_ETHER) {
3713 		NL_SET_ERR_MSG(extack, "FDB delete only supported for Ethernet devices");
3714 		return -EINVAL;
3715 	}
3716 
3717 	addr = nla_data(tb[NDA_LLADDR]);
3718 
3719 	err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
3720 	if (err)
3721 		return err;
3722 
3723 	err = -EOPNOTSUPP;
3724 
3725 	/* Support fdb on master device the net/bridge default case */
3726 	if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3727 	    (dev->priv_flags & IFF_BRIDGE_PORT)) {
3728 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3729 		const struct net_device_ops *ops = br_dev->netdev_ops;
3730 
3731 		if (ops->ndo_fdb_del)
3732 			err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
3733 
3734 		if (err)
3735 			goto out;
3736 		else
3737 			ndm->ndm_flags &= ~NTF_MASTER;
3738 	}
3739 
3740 	/* Embedded bridge, macvlan, and any other device support */
3741 	if (ndm->ndm_flags & NTF_SELF) {
3742 		if (dev->netdev_ops->ndo_fdb_del)
3743 			err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
3744 							   vid);
3745 		else
3746 			err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
3747 
3748 		if (!err) {
3749 			rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
3750 					ndm->ndm_state);
3751 			ndm->ndm_flags &= ~NTF_SELF;
3752 		}
3753 	}
3754 out:
3755 	return err;
3756 }
3757 
3758 static int nlmsg_populate_fdb(struct sk_buff *skb,
3759 			      struct netlink_callback *cb,
3760 			      struct net_device *dev,
3761 			      int *idx,
3762 			      struct netdev_hw_addr_list *list)
3763 {
3764 	struct netdev_hw_addr *ha;
3765 	int err;
3766 	u32 portid, seq;
3767 
3768 	portid = NETLINK_CB(cb->skb).portid;
3769 	seq = cb->nlh->nlmsg_seq;
3770 
3771 	list_for_each_entry(ha, &list->list, list) {
3772 		if (*idx < cb->args[2])
3773 			goto skip;
3774 
3775 		err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
3776 					      portid, seq,
3777 					      RTM_NEWNEIGH, NTF_SELF,
3778 					      NLM_F_MULTI, NUD_PERMANENT);
3779 		if (err < 0)
3780 			return err;
3781 skip:
3782 		*idx += 1;
3783 	}
3784 	return 0;
3785 }
3786 
3787 /**
3788  * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
3789  * @nlh: netlink message header
3790  * @dev: netdevice
3791  *
3792  * Default netdevice operation to dump the existing unicast address list.
3793  * Returns number of addresses from list put in skb.
3794  */
3795 int ndo_dflt_fdb_dump(struct sk_buff *skb,
3796 		      struct netlink_callback *cb,
3797 		      struct net_device *dev,
3798 		      struct net_device *filter_dev,
3799 		      int *idx)
3800 {
3801 	int err;
3802 
3803 	netif_addr_lock_bh(dev);
3804 	err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
3805 	if (err)
3806 		goto out;
3807 	err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
3808 out:
3809 	netif_addr_unlock_bh(dev);
3810 	return err;
3811 }
3812 EXPORT_SYMBOL(ndo_dflt_fdb_dump);
3813 
3814 static int valid_fdb_dump_strict(const struct nlmsghdr *nlh,
3815 				 int *br_idx, int *brport_idx,
3816 				 struct netlink_ext_ack *extack)
3817 {
3818 	struct nlattr *tb[NDA_MAX + 1];
3819 	struct ndmsg *ndm;
3820 	int err, i;
3821 
3822 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
3823 		NL_SET_ERR_MSG(extack, "Invalid header for fdb dump request");
3824 		return -EINVAL;
3825 	}
3826 
3827 	ndm = nlmsg_data(nlh);
3828 	if (ndm->ndm_pad1  || ndm->ndm_pad2  || ndm->ndm_state ||
3829 	    ndm->ndm_flags || ndm->ndm_type) {
3830 		NL_SET_ERR_MSG(extack, "Invalid values in header for fbd dump request");
3831 		return -EINVAL;
3832 	}
3833 
3834 	err = nlmsg_parse_strict(nlh, sizeof(struct ndmsg), tb, NDA_MAX,
3835 				 NULL, extack);
3836 	if (err < 0)
3837 		return err;
3838 
3839 	*brport_idx = ndm->ndm_ifindex;
3840 	for (i = 0; i <= NDA_MAX; ++i) {
3841 		if (!tb[i])
3842 			continue;
3843 
3844 		switch (i) {
3845 		case NDA_IFINDEX:
3846 			if (nla_len(tb[i]) != sizeof(u32)) {
3847 				NL_SET_ERR_MSG(extack, "Invalid IFINDEX attribute in fdb dump request");
3848 				return -EINVAL;
3849 			}
3850 			*brport_idx = nla_get_u32(tb[NDA_IFINDEX]);
3851 			break;
3852 		case NDA_MASTER:
3853 			if (nla_len(tb[i]) != sizeof(u32)) {
3854 				NL_SET_ERR_MSG(extack, "Invalid MASTER attribute in fdb dump request");
3855 				return -EINVAL;
3856 			}
3857 			*br_idx = nla_get_u32(tb[NDA_MASTER]);
3858 			break;
3859 		default:
3860 			NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb dump request");
3861 			return -EINVAL;
3862 		}
3863 	}
3864 
3865 	return 0;
3866 }
3867 
3868 static int valid_fdb_dump_legacy(const struct nlmsghdr *nlh,
3869 				 int *br_idx, int *brport_idx,
3870 				 struct netlink_ext_ack *extack)
3871 {
3872 	struct nlattr *tb[IFLA_MAX+1];
3873 	int err;
3874 
3875 	/* A hack to preserve kernel<->userspace interface.
3876 	 * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0.
3877 	 * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails.
3878 	 * So, check for ndmsg with an optional u32 attribute (not used here).
3879 	 * Fortunately these sizes don't conflict with the size of ifinfomsg
3880 	 * with an optional attribute.
3881 	 */
3882 	if (nlmsg_len(nlh) != sizeof(struct ndmsg) &&
3883 	    (nlmsg_len(nlh) != sizeof(struct ndmsg) +
3884 	     nla_attr_size(sizeof(u32)))) {
3885 		struct ifinfomsg *ifm;
3886 
3887 		err = nlmsg_parse(nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX,
3888 				  ifla_policy, extack);
3889 		if (err < 0) {
3890 			return -EINVAL;
3891 		} else if (err == 0) {
3892 			if (tb[IFLA_MASTER])
3893 				*br_idx = nla_get_u32(tb[IFLA_MASTER]);
3894 		}
3895 
3896 		ifm = nlmsg_data(nlh);
3897 		*brport_idx = ifm->ifi_index;
3898 	}
3899 	return 0;
3900 }
3901 
3902 static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
3903 {
3904 	struct net_device *dev;
3905 	struct net_device *br_dev = NULL;
3906 	const struct net_device_ops *ops = NULL;
3907 	const struct net_device_ops *cops = NULL;
3908 	struct net *net = sock_net(skb->sk);
3909 	struct hlist_head *head;
3910 	int brport_idx = 0;
3911 	int br_idx = 0;
3912 	int h, s_h;
3913 	int idx = 0, s_idx;
3914 	int err = 0;
3915 	int fidx = 0;
3916 
3917 	if (cb->strict_check)
3918 		err = valid_fdb_dump_strict(cb->nlh, &br_idx, &brport_idx,
3919 					    cb->extack);
3920 	else
3921 		err = valid_fdb_dump_legacy(cb->nlh, &br_idx, &brport_idx,
3922 					    cb->extack);
3923 	if (err < 0)
3924 		return err;
3925 
3926 	if (br_idx) {
3927 		br_dev = __dev_get_by_index(net, br_idx);
3928 		if (!br_dev)
3929 			return -ENODEV;
3930 
3931 		ops = br_dev->netdev_ops;
3932 	}
3933 
3934 	s_h = cb->args[0];
3935 	s_idx = cb->args[1];
3936 
3937 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
3938 		idx = 0;
3939 		head = &net->dev_index_head[h];
3940 		hlist_for_each_entry(dev, head, index_hlist) {
3941 
3942 			if (brport_idx && (dev->ifindex != brport_idx))
3943 				continue;
3944 
3945 			if (!br_idx) { /* user did not specify a specific bridge */
3946 				if (dev->priv_flags & IFF_BRIDGE_PORT) {
3947 					br_dev = netdev_master_upper_dev_get(dev);
3948 					cops = br_dev->netdev_ops;
3949 				}
3950 			} else {
3951 				if (dev != br_dev &&
3952 				    !(dev->priv_flags & IFF_BRIDGE_PORT))
3953 					continue;
3954 
3955 				if (br_dev != netdev_master_upper_dev_get(dev) &&
3956 				    !(dev->priv_flags & IFF_EBRIDGE))
3957 					continue;
3958 				cops = ops;
3959 			}
3960 
3961 			if (idx < s_idx)
3962 				goto cont;
3963 
3964 			if (dev->priv_flags & IFF_BRIDGE_PORT) {
3965 				if (cops && cops->ndo_fdb_dump) {
3966 					err = cops->ndo_fdb_dump(skb, cb,
3967 								br_dev, dev,
3968 								&fidx);
3969 					if (err == -EMSGSIZE)
3970 						goto out;
3971 				}
3972 			}
3973 
3974 			if (dev->netdev_ops->ndo_fdb_dump)
3975 				err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
3976 								    dev, NULL,
3977 								    &fidx);
3978 			else
3979 				err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
3980 							&fidx);
3981 			if (err == -EMSGSIZE)
3982 				goto out;
3983 
3984 			cops = NULL;
3985 
3986 			/* reset fdb offset to 0 for rest of the interfaces */
3987 			cb->args[2] = 0;
3988 			fidx = 0;
3989 cont:
3990 			idx++;
3991 		}
3992 	}
3993 
3994 out:
3995 	cb->args[0] = h;
3996 	cb->args[1] = idx;
3997 	cb->args[2] = fidx;
3998 
3999 	return skb->len;
4000 }
4001 
4002 static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
4003 			       unsigned int attrnum, unsigned int flag)
4004 {
4005 	if (mask & flag)
4006 		return nla_put_u8(skb, attrnum, !!(flags & flag));
4007 	return 0;
4008 }
4009 
4010 int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
4011 			    struct net_device *dev, u16 mode,
4012 			    u32 flags, u32 mask, int nlflags,
4013 			    u32 filter_mask,
4014 			    int (*vlan_fill)(struct sk_buff *skb,
4015 					     struct net_device *dev,
4016 					     u32 filter_mask))
4017 {
4018 	struct nlmsghdr *nlh;
4019 	struct ifinfomsg *ifm;
4020 	struct nlattr *br_afspec;
4021 	struct nlattr *protinfo;
4022 	u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
4023 	struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4024 	int err = 0;
4025 
4026 	nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
4027 	if (nlh == NULL)
4028 		return -EMSGSIZE;
4029 
4030 	ifm = nlmsg_data(nlh);
4031 	ifm->ifi_family = AF_BRIDGE;
4032 	ifm->__ifi_pad = 0;
4033 	ifm->ifi_type = dev->type;
4034 	ifm->ifi_index = dev->ifindex;
4035 	ifm->ifi_flags = dev_get_flags(dev);
4036 	ifm->ifi_change = 0;
4037 
4038 
4039 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
4040 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
4041 	    nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
4042 	    (br_dev &&
4043 	     nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
4044 	    (dev->addr_len &&
4045 	     nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
4046 	    (dev->ifindex != dev_get_iflink(dev) &&
4047 	     nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
4048 		goto nla_put_failure;
4049 
4050 	br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
4051 	if (!br_afspec)
4052 		goto nla_put_failure;
4053 
4054 	if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
4055 		nla_nest_cancel(skb, br_afspec);
4056 		goto nla_put_failure;
4057 	}
4058 
4059 	if (mode != BRIDGE_MODE_UNDEF) {
4060 		if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
4061 			nla_nest_cancel(skb, br_afspec);
4062 			goto nla_put_failure;
4063 		}
4064 	}
4065 	if (vlan_fill) {
4066 		err = vlan_fill(skb, dev, filter_mask);
4067 		if (err) {
4068 			nla_nest_cancel(skb, br_afspec);
4069 			goto nla_put_failure;
4070 		}
4071 	}
4072 	nla_nest_end(skb, br_afspec);
4073 
4074 	protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
4075 	if (!protinfo)
4076 		goto nla_put_failure;
4077 
4078 	if (brport_nla_put_flag(skb, flags, mask,
4079 				IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
4080 	    brport_nla_put_flag(skb, flags, mask,
4081 				IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
4082 	    brport_nla_put_flag(skb, flags, mask,
4083 				IFLA_BRPORT_FAST_LEAVE,
4084 				BR_MULTICAST_FAST_LEAVE) ||
4085 	    brport_nla_put_flag(skb, flags, mask,
4086 				IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
4087 	    brport_nla_put_flag(skb, flags, mask,
4088 				IFLA_BRPORT_LEARNING, BR_LEARNING) ||
4089 	    brport_nla_put_flag(skb, flags, mask,
4090 				IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
4091 	    brport_nla_put_flag(skb, flags, mask,
4092 				IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
4093 	    brport_nla_put_flag(skb, flags, mask,
4094 				IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
4095 		nla_nest_cancel(skb, protinfo);
4096 		goto nla_put_failure;
4097 	}
4098 
4099 	nla_nest_end(skb, protinfo);
4100 
4101 	nlmsg_end(skb, nlh);
4102 	return 0;
4103 nla_put_failure:
4104 	nlmsg_cancel(skb, nlh);
4105 	return err ? err : -EMSGSIZE;
4106 }
4107 EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
4108 
4109 static int valid_bridge_getlink_req(const struct nlmsghdr *nlh,
4110 				    bool strict_check, u32 *filter_mask,
4111 				    struct netlink_ext_ack *extack)
4112 {
4113 	struct nlattr *tb[IFLA_MAX+1];
4114 	int err, i;
4115 
4116 	if (strict_check) {
4117 		struct ifinfomsg *ifm;
4118 
4119 		if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
4120 			NL_SET_ERR_MSG(extack, "Invalid header for bridge link dump");
4121 			return -EINVAL;
4122 		}
4123 
4124 		ifm = nlmsg_data(nlh);
4125 		if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
4126 		    ifm->ifi_change || ifm->ifi_index) {
4127 			NL_SET_ERR_MSG(extack, "Invalid values in header for bridge link dump request");
4128 			return -EINVAL;
4129 		}
4130 
4131 		err = nlmsg_parse_strict(nlh, sizeof(struct ifinfomsg), tb,
4132 					 IFLA_MAX, ifla_policy, extack);
4133 	} else {
4134 		err = nlmsg_parse(nlh, sizeof(struct ifinfomsg), tb,
4135 				  IFLA_MAX, ifla_policy, extack);
4136 	}
4137 	if (err < 0)
4138 		return err;
4139 
4140 	/* new attributes should only be added with strict checking */
4141 	for (i = 0; i <= IFLA_MAX; ++i) {
4142 		if (!tb[i])
4143 			continue;
4144 
4145 		switch (i) {
4146 		case IFLA_EXT_MASK:
4147 			*filter_mask = nla_get_u32(tb[i]);
4148 			break;
4149 		default:
4150 			if (strict_check) {
4151 				NL_SET_ERR_MSG(extack, "Unsupported attribute in bridge link dump request");
4152 				return -EINVAL;
4153 			}
4154 		}
4155 	}
4156 
4157 	return 0;
4158 }
4159 
4160 static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
4161 {
4162 	const struct nlmsghdr *nlh = cb->nlh;
4163 	struct net *net = sock_net(skb->sk);
4164 	struct net_device *dev;
4165 	int idx = 0;
4166 	u32 portid = NETLINK_CB(cb->skb).portid;
4167 	u32 seq = nlh->nlmsg_seq;
4168 	u32 filter_mask = 0;
4169 	int err;
4170 
4171 	err = valid_bridge_getlink_req(nlh, cb->strict_check, &filter_mask,
4172 				       cb->extack);
4173 	if (err < 0 && cb->strict_check)
4174 		return err;
4175 
4176 	rcu_read_lock();
4177 	for_each_netdev_rcu(net, dev) {
4178 		const struct net_device_ops *ops = dev->netdev_ops;
4179 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4180 
4181 		if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
4182 			if (idx >= cb->args[0]) {
4183 				err = br_dev->netdev_ops->ndo_bridge_getlink(
4184 						skb, portid, seq, dev,
4185 						filter_mask, NLM_F_MULTI);
4186 				if (err < 0 && err != -EOPNOTSUPP) {
4187 					if (likely(skb->len))
4188 						break;
4189 
4190 					goto out_err;
4191 				}
4192 			}
4193 			idx++;
4194 		}
4195 
4196 		if (ops->ndo_bridge_getlink) {
4197 			if (idx >= cb->args[0]) {
4198 				err = ops->ndo_bridge_getlink(skb, portid,
4199 							      seq, dev,
4200 							      filter_mask,
4201 							      NLM_F_MULTI);
4202 				if (err < 0 && err != -EOPNOTSUPP) {
4203 					if (likely(skb->len))
4204 						break;
4205 
4206 					goto out_err;
4207 				}
4208 			}
4209 			idx++;
4210 		}
4211 	}
4212 	err = skb->len;
4213 out_err:
4214 	rcu_read_unlock();
4215 	cb->args[0] = idx;
4216 
4217 	return err;
4218 }
4219 
4220 static inline size_t bridge_nlmsg_size(void)
4221 {
4222 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
4223 		+ nla_total_size(IFNAMSIZ)	/* IFLA_IFNAME */
4224 		+ nla_total_size(MAX_ADDR_LEN)	/* IFLA_ADDRESS */
4225 		+ nla_total_size(sizeof(u32))	/* IFLA_MASTER */
4226 		+ nla_total_size(sizeof(u32))	/* IFLA_MTU */
4227 		+ nla_total_size(sizeof(u32))	/* IFLA_LINK */
4228 		+ nla_total_size(sizeof(u32))	/* IFLA_OPERSTATE */
4229 		+ nla_total_size(sizeof(u8))	/* IFLA_PROTINFO */
4230 		+ nla_total_size(sizeof(struct nlattr))	/* IFLA_AF_SPEC */
4231 		+ nla_total_size(sizeof(u16))	/* IFLA_BRIDGE_FLAGS */
4232 		+ nla_total_size(sizeof(u16));	/* IFLA_BRIDGE_MODE */
4233 }
4234 
4235 static int rtnl_bridge_notify(struct net_device *dev)
4236 {
4237 	struct net *net = dev_net(dev);
4238 	struct sk_buff *skb;
4239 	int err = -EOPNOTSUPP;
4240 
4241 	if (!dev->netdev_ops->ndo_bridge_getlink)
4242 		return 0;
4243 
4244 	skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
4245 	if (!skb) {
4246 		err = -ENOMEM;
4247 		goto errout;
4248 	}
4249 
4250 	err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
4251 	if (err < 0)
4252 		goto errout;
4253 
4254 	if (!skb->len)
4255 		goto errout;
4256 
4257 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
4258 	return 0;
4259 errout:
4260 	WARN_ON(err == -EMSGSIZE);
4261 	kfree_skb(skb);
4262 	if (err)
4263 		rtnl_set_sk_err(net, RTNLGRP_LINK, err);
4264 	return err;
4265 }
4266 
4267 static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
4268 			       struct netlink_ext_ack *extack)
4269 {
4270 	struct net *net = sock_net(skb->sk);
4271 	struct ifinfomsg *ifm;
4272 	struct net_device *dev;
4273 	struct nlattr *br_spec, *attr = NULL;
4274 	int rem, err = -EOPNOTSUPP;
4275 	u16 flags = 0;
4276 	bool have_flags = false;
4277 
4278 	if (nlmsg_len(nlh) < sizeof(*ifm))
4279 		return -EINVAL;
4280 
4281 	ifm = nlmsg_data(nlh);
4282 	if (ifm->ifi_family != AF_BRIDGE)
4283 		return -EPFNOSUPPORT;
4284 
4285 	dev = __dev_get_by_index(net, ifm->ifi_index);
4286 	if (!dev) {
4287 		NL_SET_ERR_MSG(extack, "unknown ifindex");
4288 		return -ENODEV;
4289 	}
4290 
4291 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4292 	if (br_spec) {
4293 		nla_for_each_nested(attr, br_spec, rem) {
4294 			if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
4295 				if (nla_len(attr) < sizeof(flags))
4296 					return -EINVAL;
4297 
4298 				have_flags = true;
4299 				flags = nla_get_u16(attr);
4300 				break;
4301 			}
4302 		}
4303 	}
4304 
4305 	if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
4306 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4307 
4308 		if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
4309 			err = -EOPNOTSUPP;
4310 			goto out;
4311 		}
4312 
4313 		err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags);
4314 		if (err)
4315 			goto out;
4316 
4317 		flags &= ~BRIDGE_FLAGS_MASTER;
4318 	}
4319 
4320 	if ((flags & BRIDGE_FLAGS_SELF)) {
4321 		if (!dev->netdev_ops->ndo_bridge_setlink)
4322 			err = -EOPNOTSUPP;
4323 		else
4324 			err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
4325 								  flags);
4326 		if (!err) {
4327 			flags &= ~BRIDGE_FLAGS_SELF;
4328 
4329 			/* Generate event to notify upper layer of bridge
4330 			 * change
4331 			 */
4332 			err = rtnl_bridge_notify(dev);
4333 		}
4334 	}
4335 
4336 	if (have_flags)
4337 		memcpy(nla_data(attr), &flags, sizeof(flags));
4338 out:
4339 	return err;
4340 }
4341 
4342 static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
4343 			       struct netlink_ext_ack *extack)
4344 {
4345 	struct net *net = sock_net(skb->sk);
4346 	struct ifinfomsg *ifm;
4347 	struct net_device *dev;
4348 	struct nlattr *br_spec, *attr = NULL;
4349 	int rem, err = -EOPNOTSUPP;
4350 	u16 flags = 0;
4351 	bool have_flags = false;
4352 
4353 	if (nlmsg_len(nlh) < sizeof(*ifm))
4354 		return -EINVAL;
4355 
4356 	ifm = nlmsg_data(nlh);
4357 	if (ifm->ifi_family != AF_BRIDGE)
4358 		return -EPFNOSUPPORT;
4359 
4360 	dev = __dev_get_by_index(net, ifm->ifi_index);
4361 	if (!dev) {
4362 		NL_SET_ERR_MSG(extack, "unknown ifindex");
4363 		return -ENODEV;
4364 	}
4365 
4366 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4367 	if (br_spec) {
4368 		nla_for_each_nested(attr, br_spec, rem) {
4369 			if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
4370 				if (nla_len(attr) < sizeof(flags))
4371 					return -EINVAL;
4372 
4373 				have_flags = true;
4374 				flags = nla_get_u16(attr);
4375 				break;
4376 			}
4377 		}
4378 	}
4379 
4380 	if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
4381 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4382 
4383 		if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
4384 			err = -EOPNOTSUPP;
4385 			goto out;
4386 		}
4387 
4388 		err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
4389 		if (err)
4390 			goto out;
4391 
4392 		flags &= ~BRIDGE_FLAGS_MASTER;
4393 	}
4394 
4395 	if ((flags & BRIDGE_FLAGS_SELF)) {
4396 		if (!dev->netdev_ops->ndo_bridge_dellink)
4397 			err = -EOPNOTSUPP;
4398 		else
4399 			err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
4400 								  flags);
4401 
4402 		if (!err) {
4403 			flags &= ~BRIDGE_FLAGS_SELF;
4404 
4405 			/* Generate event to notify upper layer of bridge
4406 			 * change
4407 			 */
4408 			err = rtnl_bridge_notify(dev);
4409 		}
4410 	}
4411 
4412 	if (have_flags)
4413 		memcpy(nla_data(attr), &flags, sizeof(flags));
4414 out:
4415 	return err;
4416 }
4417 
4418 static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
4419 {
4420 	return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
4421 	       (!idxattr || idxattr == attrid);
4422 }
4423 
4424 #define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
4425 static int rtnl_get_offload_stats_attr_size(int attr_id)
4426 {
4427 	switch (attr_id) {
4428 	case IFLA_OFFLOAD_XSTATS_CPU_HIT:
4429 		return sizeof(struct rtnl_link_stats64);
4430 	}
4431 
4432 	return 0;
4433 }
4434 
4435 static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev,
4436 				  int *prividx)
4437 {
4438 	struct nlattr *attr = NULL;
4439 	int attr_id, size;
4440 	void *attr_data;
4441 	int err;
4442 
4443 	if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
4444 	      dev->netdev_ops->ndo_get_offload_stats))
4445 		return -ENODATA;
4446 
4447 	for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
4448 	     attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
4449 		if (attr_id < *prividx)
4450 			continue;
4451 
4452 		size = rtnl_get_offload_stats_attr_size(attr_id);
4453 		if (!size)
4454 			continue;
4455 
4456 		if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
4457 			continue;
4458 
4459 		attr = nla_reserve_64bit(skb, attr_id, size,
4460 					 IFLA_OFFLOAD_XSTATS_UNSPEC);
4461 		if (!attr)
4462 			goto nla_put_failure;
4463 
4464 		attr_data = nla_data(attr);
4465 		memset(attr_data, 0, size);
4466 		err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev,
4467 							     attr_data);
4468 		if (err)
4469 			goto get_offload_stats_failure;
4470 	}
4471 
4472 	if (!attr)
4473 		return -ENODATA;
4474 
4475 	*prividx = 0;
4476 	return 0;
4477 
4478 nla_put_failure:
4479 	err = -EMSGSIZE;
4480 get_offload_stats_failure:
4481 	*prividx = attr_id;
4482 	return err;
4483 }
4484 
4485 static int rtnl_get_offload_stats_size(const struct net_device *dev)
4486 {
4487 	int nla_size = 0;
4488 	int attr_id;
4489 	int size;
4490 
4491 	if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
4492 	      dev->netdev_ops->ndo_get_offload_stats))
4493 		return 0;
4494 
4495 	for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
4496 	     attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
4497 		if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
4498 			continue;
4499 		size = rtnl_get_offload_stats_attr_size(attr_id);
4500 		nla_size += nla_total_size_64bit(size);
4501 	}
4502 
4503 	if (nla_size != 0)
4504 		nla_size += nla_total_size(0);
4505 
4506 	return nla_size;
4507 }
4508 
4509 static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
4510 			       int type, u32 pid, u32 seq, u32 change,
4511 			       unsigned int flags, unsigned int filter_mask,
4512 			       int *idxattr, int *prividx)
4513 {
4514 	struct if_stats_msg *ifsm;
4515 	struct nlmsghdr *nlh;
4516 	struct nlattr *attr;
4517 	int s_prividx = *prividx;
4518 	int err;
4519 
4520 	ASSERT_RTNL();
4521 
4522 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
4523 	if (!nlh)
4524 		return -EMSGSIZE;
4525 
4526 	ifsm = nlmsg_data(nlh);
4527 	ifsm->family = PF_UNSPEC;
4528 	ifsm->pad1 = 0;
4529 	ifsm->pad2 = 0;
4530 	ifsm->ifindex = dev->ifindex;
4531 	ifsm->filter_mask = filter_mask;
4532 
4533 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
4534 		struct rtnl_link_stats64 *sp;
4535 
4536 		attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
4537 					 sizeof(struct rtnl_link_stats64),
4538 					 IFLA_STATS_UNSPEC);
4539 		if (!attr)
4540 			goto nla_put_failure;
4541 
4542 		sp = nla_data(attr);
4543 		dev_get_stats(dev, sp);
4544 	}
4545 
4546 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
4547 		const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
4548 
4549 		if (ops && ops->fill_linkxstats) {
4550 			*idxattr = IFLA_STATS_LINK_XSTATS;
4551 			attr = nla_nest_start(skb,
4552 					      IFLA_STATS_LINK_XSTATS);
4553 			if (!attr)
4554 				goto nla_put_failure;
4555 
4556 			err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
4557 			nla_nest_end(skb, attr);
4558 			if (err)
4559 				goto nla_put_failure;
4560 			*idxattr = 0;
4561 		}
4562 	}
4563 
4564 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
4565 			     *idxattr)) {
4566 		const struct rtnl_link_ops *ops = NULL;
4567 		const struct net_device *master;
4568 
4569 		master = netdev_master_upper_dev_get(dev);
4570 		if (master)
4571 			ops = master->rtnl_link_ops;
4572 		if (ops && ops->fill_linkxstats) {
4573 			*idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
4574 			attr = nla_nest_start(skb,
4575 					      IFLA_STATS_LINK_XSTATS_SLAVE);
4576 			if (!attr)
4577 				goto nla_put_failure;
4578 
4579 			err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
4580 			nla_nest_end(skb, attr);
4581 			if (err)
4582 				goto nla_put_failure;
4583 			*idxattr = 0;
4584 		}
4585 	}
4586 
4587 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
4588 			     *idxattr)) {
4589 		*idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
4590 		attr = nla_nest_start(skb, IFLA_STATS_LINK_OFFLOAD_XSTATS);
4591 		if (!attr)
4592 			goto nla_put_failure;
4593 
4594 		err = rtnl_get_offload_stats(skb, dev, prividx);
4595 		if (err == -ENODATA)
4596 			nla_nest_cancel(skb, attr);
4597 		else
4598 			nla_nest_end(skb, attr);
4599 
4600 		if (err && err != -ENODATA)
4601 			goto nla_put_failure;
4602 		*idxattr = 0;
4603 	}
4604 
4605 	if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
4606 		struct rtnl_af_ops *af_ops;
4607 
4608 		*idxattr = IFLA_STATS_AF_SPEC;
4609 		attr = nla_nest_start(skb, IFLA_STATS_AF_SPEC);
4610 		if (!attr)
4611 			goto nla_put_failure;
4612 
4613 		rcu_read_lock();
4614 		list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
4615 			if (af_ops->fill_stats_af) {
4616 				struct nlattr *af;
4617 				int err;
4618 
4619 				af = nla_nest_start(skb, af_ops->family);
4620 				if (!af) {
4621 					rcu_read_unlock();
4622 					goto nla_put_failure;
4623 				}
4624 				err = af_ops->fill_stats_af(skb, dev);
4625 
4626 				if (err == -ENODATA) {
4627 					nla_nest_cancel(skb, af);
4628 				} else if (err < 0) {
4629 					rcu_read_unlock();
4630 					goto nla_put_failure;
4631 				}
4632 
4633 				nla_nest_end(skb, af);
4634 			}
4635 		}
4636 		rcu_read_unlock();
4637 
4638 		nla_nest_end(skb, attr);
4639 
4640 		*idxattr = 0;
4641 	}
4642 
4643 	nlmsg_end(skb, nlh);
4644 
4645 	return 0;
4646 
4647 nla_put_failure:
4648 	/* not a multi message or no progress mean a real error */
4649 	if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
4650 		nlmsg_cancel(skb, nlh);
4651 	else
4652 		nlmsg_end(skb, nlh);
4653 
4654 	return -EMSGSIZE;
4655 }
4656 
4657 static size_t if_nlmsg_stats_size(const struct net_device *dev,
4658 				  u32 filter_mask)
4659 {
4660 	size_t size = 0;
4661 
4662 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
4663 		size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
4664 
4665 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
4666 		const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
4667 		int attr = IFLA_STATS_LINK_XSTATS;
4668 
4669 		if (ops && ops->get_linkxstats_size) {
4670 			size += nla_total_size(ops->get_linkxstats_size(dev,
4671 									attr));
4672 			/* for IFLA_STATS_LINK_XSTATS */
4673 			size += nla_total_size(0);
4674 		}
4675 	}
4676 
4677 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
4678 		struct net_device *_dev = (struct net_device *)dev;
4679 		const struct rtnl_link_ops *ops = NULL;
4680 		const struct net_device *master;
4681 
4682 		/* netdev_master_upper_dev_get can't take const */
4683 		master = netdev_master_upper_dev_get(_dev);
4684 		if (master)
4685 			ops = master->rtnl_link_ops;
4686 		if (ops && ops->get_linkxstats_size) {
4687 			int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
4688 
4689 			size += nla_total_size(ops->get_linkxstats_size(dev,
4690 									attr));
4691 			/* for IFLA_STATS_LINK_XSTATS_SLAVE */
4692 			size += nla_total_size(0);
4693 		}
4694 	}
4695 
4696 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0))
4697 		size += rtnl_get_offload_stats_size(dev);
4698 
4699 	if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
4700 		struct rtnl_af_ops *af_ops;
4701 
4702 		/* for IFLA_STATS_AF_SPEC */
4703 		size += nla_total_size(0);
4704 
4705 		rcu_read_lock();
4706 		list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
4707 			if (af_ops->get_stats_af_size) {
4708 				size += nla_total_size(
4709 					af_ops->get_stats_af_size(dev));
4710 
4711 				/* for AF_* */
4712 				size += nla_total_size(0);
4713 			}
4714 		}
4715 		rcu_read_unlock();
4716 	}
4717 
4718 	return size;
4719 }
4720 
4721 static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
4722 			  struct netlink_ext_ack *extack)
4723 {
4724 	struct net *net = sock_net(skb->sk);
4725 	struct net_device *dev = NULL;
4726 	int idxattr = 0, prividx = 0;
4727 	struct if_stats_msg *ifsm;
4728 	struct sk_buff *nskb;
4729 	u32 filter_mask;
4730 	int err;
4731 
4732 	if (nlmsg_len(nlh) < sizeof(*ifsm))
4733 		return -EINVAL;
4734 
4735 	ifsm = nlmsg_data(nlh);
4736 	if (ifsm->ifindex > 0)
4737 		dev = __dev_get_by_index(net, ifsm->ifindex);
4738 	else
4739 		return -EINVAL;
4740 
4741 	if (!dev)
4742 		return -ENODEV;
4743 
4744 	filter_mask = ifsm->filter_mask;
4745 	if (!filter_mask)
4746 		return -EINVAL;
4747 
4748 	nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
4749 	if (!nskb)
4750 		return -ENOBUFS;
4751 
4752 	err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
4753 				  NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
4754 				  0, filter_mask, &idxattr, &prividx);
4755 	if (err < 0) {
4756 		/* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
4757 		WARN_ON(err == -EMSGSIZE);
4758 		kfree_skb(nskb);
4759 	} else {
4760 		err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
4761 	}
4762 
4763 	return err;
4764 }
4765 
4766 static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
4767 {
4768 	struct netlink_ext_ack *extack = cb->extack;
4769 	int h, s_h, err, s_idx, s_idxattr, s_prividx;
4770 	struct net *net = sock_net(skb->sk);
4771 	unsigned int flags = NLM_F_MULTI;
4772 	struct if_stats_msg *ifsm;
4773 	struct hlist_head *head;
4774 	struct net_device *dev;
4775 	u32 filter_mask = 0;
4776 	int idx = 0;
4777 
4778 	s_h = cb->args[0];
4779 	s_idx = cb->args[1];
4780 	s_idxattr = cb->args[2];
4781 	s_prividx = cb->args[3];
4782 
4783 	cb->seq = net->dev_base_seq;
4784 
4785 	if (nlmsg_len(cb->nlh) < sizeof(*ifsm)) {
4786 		NL_SET_ERR_MSG(extack, "Invalid header for stats dump");
4787 		return -EINVAL;
4788 	}
4789 
4790 	ifsm = nlmsg_data(cb->nlh);
4791 
4792 	/* only requests using strict checks can pass data to influence
4793 	 * the dump. The legacy exception is filter_mask.
4794 	 */
4795 	if (cb->strict_check) {
4796 		if (ifsm->pad1 || ifsm->pad2 || ifsm->ifindex) {
4797 			NL_SET_ERR_MSG(extack, "Invalid values in header for stats dump request");
4798 			return -EINVAL;
4799 		}
4800 		if (nlmsg_attrlen(cb->nlh, sizeof(*ifsm))) {
4801 			NL_SET_ERR_MSG(extack, "Invalid attributes after stats header");
4802 			return -EINVAL;
4803 		}
4804 	}
4805 
4806 	filter_mask = ifsm->filter_mask;
4807 	if (!filter_mask) {
4808 		NL_SET_ERR_MSG(extack, "Filter mask must be set for stats dump");
4809 		return -EINVAL;
4810 	}
4811 
4812 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4813 		idx = 0;
4814 		head = &net->dev_index_head[h];
4815 		hlist_for_each_entry(dev, head, index_hlist) {
4816 			if (idx < s_idx)
4817 				goto cont;
4818 			err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
4819 						  NETLINK_CB(cb->skb).portid,
4820 						  cb->nlh->nlmsg_seq, 0,
4821 						  flags, filter_mask,
4822 						  &s_idxattr, &s_prividx);
4823 			/* If we ran out of room on the first message,
4824 			 * we're in trouble
4825 			 */
4826 			WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
4827 
4828 			if (err < 0)
4829 				goto out;
4830 			s_prividx = 0;
4831 			s_idxattr = 0;
4832 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
4833 cont:
4834 			idx++;
4835 		}
4836 	}
4837 out:
4838 	cb->args[3] = s_prividx;
4839 	cb->args[2] = s_idxattr;
4840 	cb->args[1] = idx;
4841 	cb->args[0] = h;
4842 
4843 	return skb->len;
4844 }
4845 
4846 /* Process one rtnetlink message. */
4847 
4848 static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
4849 			     struct netlink_ext_ack *extack)
4850 {
4851 	struct net *net = sock_net(skb->sk);
4852 	struct rtnl_link *link;
4853 	struct module *owner;
4854 	int err = -EOPNOTSUPP;
4855 	rtnl_doit_func doit;
4856 	unsigned int flags;
4857 	int kind;
4858 	int family;
4859 	int type;
4860 
4861 	type = nlh->nlmsg_type;
4862 	if (type > RTM_MAX)
4863 		return -EOPNOTSUPP;
4864 
4865 	type -= RTM_BASE;
4866 
4867 	/* All the messages must have at least 1 byte length */
4868 	if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
4869 		return 0;
4870 
4871 	family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
4872 	kind = type&3;
4873 
4874 	if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
4875 		return -EPERM;
4876 
4877 	rcu_read_lock();
4878 	if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
4879 		struct sock *rtnl;
4880 		rtnl_dumpit_func dumpit;
4881 		u16 min_dump_alloc = 0;
4882 
4883 		link = rtnl_get_link(family, type);
4884 		if (!link || !link->dumpit) {
4885 			family = PF_UNSPEC;
4886 			link = rtnl_get_link(family, type);
4887 			if (!link || !link->dumpit)
4888 				goto err_unlock;
4889 		}
4890 		owner = link->owner;
4891 		dumpit = link->dumpit;
4892 
4893 		if (type == RTM_GETLINK - RTM_BASE)
4894 			min_dump_alloc = rtnl_calcit(skb, nlh);
4895 
4896 		err = 0;
4897 		/* need to do this before rcu_read_unlock() */
4898 		if (!try_module_get(owner))
4899 			err = -EPROTONOSUPPORT;
4900 
4901 		rcu_read_unlock();
4902 
4903 		rtnl = net->rtnl;
4904 		if (err == 0) {
4905 			struct netlink_dump_control c = {
4906 				.dump		= dumpit,
4907 				.min_dump_alloc	= min_dump_alloc,
4908 				.module		= owner,
4909 			};
4910 			err = netlink_dump_start(rtnl, skb, nlh, &c);
4911 			/* netlink_dump_start() will keep a reference on
4912 			 * module if dump is still in progress.
4913 			 */
4914 			module_put(owner);
4915 		}
4916 		return err;
4917 	}
4918 
4919 	link = rtnl_get_link(family, type);
4920 	if (!link || !link->doit) {
4921 		family = PF_UNSPEC;
4922 		link = rtnl_get_link(PF_UNSPEC, type);
4923 		if (!link || !link->doit)
4924 			goto out_unlock;
4925 	}
4926 
4927 	owner = link->owner;
4928 	if (!try_module_get(owner)) {
4929 		err = -EPROTONOSUPPORT;
4930 		goto out_unlock;
4931 	}
4932 
4933 	flags = link->flags;
4934 	if (flags & RTNL_FLAG_DOIT_UNLOCKED) {
4935 		doit = link->doit;
4936 		rcu_read_unlock();
4937 		if (doit)
4938 			err = doit(skb, nlh, extack);
4939 		module_put(owner);
4940 		return err;
4941 	}
4942 	rcu_read_unlock();
4943 
4944 	rtnl_lock();
4945 	link = rtnl_get_link(family, type);
4946 	if (link && link->doit)
4947 		err = link->doit(skb, nlh, extack);
4948 	rtnl_unlock();
4949 
4950 	module_put(owner);
4951 
4952 	return err;
4953 
4954 out_unlock:
4955 	rcu_read_unlock();
4956 	return err;
4957 
4958 err_unlock:
4959 	rcu_read_unlock();
4960 	return -EOPNOTSUPP;
4961 }
4962 
4963 static void rtnetlink_rcv(struct sk_buff *skb)
4964 {
4965 	netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
4966 }
4967 
4968 static int rtnetlink_bind(struct net *net, int group)
4969 {
4970 	switch (group) {
4971 	case RTNLGRP_IPV4_MROUTE_R:
4972 	case RTNLGRP_IPV6_MROUTE_R:
4973 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
4974 			return -EPERM;
4975 		break;
4976 	}
4977 	return 0;
4978 }
4979 
4980 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
4981 {
4982 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
4983 
4984 	switch (event) {
4985 	case NETDEV_REBOOT:
4986 	case NETDEV_CHANGEMTU:
4987 	case NETDEV_CHANGEADDR:
4988 	case NETDEV_CHANGENAME:
4989 	case NETDEV_FEAT_CHANGE:
4990 	case NETDEV_BONDING_FAILOVER:
4991 	case NETDEV_POST_TYPE_CHANGE:
4992 	case NETDEV_NOTIFY_PEERS:
4993 	case NETDEV_CHANGEUPPER:
4994 	case NETDEV_RESEND_IGMP:
4995 	case NETDEV_CHANGEINFODATA:
4996 	case NETDEV_CHANGELOWERSTATE:
4997 	case NETDEV_CHANGE_TX_QUEUE_LEN:
4998 		rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
4999 				   GFP_KERNEL, NULL, 0);
5000 		break;
5001 	default:
5002 		break;
5003 	}
5004 	return NOTIFY_DONE;
5005 }
5006 
5007 static struct notifier_block rtnetlink_dev_notifier = {
5008 	.notifier_call	= rtnetlink_event,
5009 };
5010 
5011 
5012 static int __net_init rtnetlink_net_init(struct net *net)
5013 {
5014 	struct sock *sk;
5015 	struct netlink_kernel_cfg cfg = {
5016 		.groups		= RTNLGRP_MAX,
5017 		.input		= rtnetlink_rcv,
5018 		.cb_mutex	= &rtnl_mutex,
5019 		.flags		= NL_CFG_F_NONROOT_RECV,
5020 		.bind		= rtnetlink_bind,
5021 	};
5022 
5023 	sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
5024 	if (!sk)
5025 		return -ENOMEM;
5026 	net->rtnl = sk;
5027 	return 0;
5028 }
5029 
5030 static void __net_exit rtnetlink_net_exit(struct net *net)
5031 {
5032 	netlink_kernel_release(net->rtnl);
5033 	net->rtnl = NULL;
5034 }
5035 
5036 static struct pernet_operations rtnetlink_net_ops = {
5037 	.init = rtnetlink_net_init,
5038 	.exit = rtnetlink_net_exit,
5039 };
5040 
5041 void __init rtnetlink_init(void)
5042 {
5043 	if (register_pernet_subsys(&rtnetlink_net_ops))
5044 		panic("rtnetlink_init: cannot initialize rtnetlink\n");
5045 
5046 	register_netdevice_notifier(&rtnetlink_dev_notifier);
5047 
5048 	rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
5049 		      rtnl_dump_ifinfo, 0);
5050 	rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0);
5051 	rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0);
5052 	rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0);
5053 
5054 	rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0);
5055 	rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0);
5056 	rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0);
5057 
5058 	rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
5059 	rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0);
5060 	rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, 0);
5061 
5062 	rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0);
5063 	rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0);
5064 	rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0);
5065 
5066 	rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,
5067 		      0);
5068 }
5069