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