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