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