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