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