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