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