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