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