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