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