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