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