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