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