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