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