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