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