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