xref: /openbmc/linux/net/core/rtnetlink.c (revision 63105e83987a08f73accfba78602a073bf219b69)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * INET		An implementation of the TCP/IP protocol suite for the LINUX
41da177e4SLinus Torvalds  *		operating system.  INET is implemented using the  BSD Socket
51da177e4SLinus Torvalds  *		interface as the means of communication with the user level.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *		Routing netlink socket interface: protocol independent part.
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  *	Fixes:
12d467d0bcSZheng Yongjun  *	Vitaly E. Lavrov		RTA_OK arithmetic was wrong.
131da177e4SLinus Torvalds  */
141da177e4SLinus Torvalds 
15ee5d032fSJakub Kicinski #include <linux/bitops.h>
161da177e4SLinus Torvalds #include <linux/errno.h>
171da177e4SLinus Torvalds #include <linux/module.h>
181da177e4SLinus Torvalds #include <linux/types.h>
191da177e4SLinus Torvalds #include <linux/socket.h>
201da177e4SLinus Torvalds #include <linux/kernel.h>
211da177e4SLinus Torvalds #include <linux/timer.h>
221da177e4SLinus Torvalds #include <linux/string.h>
231da177e4SLinus Torvalds #include <linux/sockios.h>
241da177e4SLinus Torvalds #include <linux/net.h>
251da177e4SLinus Torvalds #include <linux/fcntl.h>
261da177e4SLinus Torvalds #include <linux/mm.h>
271da177e4SLinus Torvalds #include <linux/slab.h>
281da177e4SLinus Torvalds #include <linux/interrupt.h>
291da177e4SLinus Torvalds #include <linux/capability.h>
301da177e4SLinus Torvalds #include <linux/skbuff.h>
311da177e4SLinus Torvalds #include <linux/init.h>
321da177e4SLinus Torvalds #include <linux/security.h>
336756ae4bSStephen Hemminger #include <linux/mutex.h>
341823730fSThomas Graf #include <linux/if_addr.h>
3577162022SJohn Fastabend #include <linux/if_bridge.h>
36f6f6424bSJiri Pirko #include <linux/if_vlan.h>
37ebc08a6fSWilliams, Mitch A #include <linux/pci.h>
3877162022SJohn Fastabend #include <linux/etherdevice.h>
3958038695SMartin KaFai Lau #include <linux/bpf.h>
401da177e4SLinus Torvalds 
417c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds #include <linux/inet.h>
441da177e4SLinus Torvalds #include <linux/netdevice.h>
451da177e4SLinus Torvalds #include <net/ip.h>
461da177e4SLinus Torvalds #include <net/protocol.h>
471da177e4SLinus Torvalds #include <net/arp.h>
481da177e4SLinus Torvalds #include <net/route.h>
491da177e4SLinus Torvalds #include <net/udp.h>
50ea697639SDaniel Borkmann #include <net/tcp.h>
511da177e4SLinus Torvalds #include <net/sock.h>
521da177e4SLinus Torvalds #include <net/pkt_sched.h>
5314c0b97dSThomas Graf #include <net/fib_rules.h>
54e2849863SThomas Graf #include <net/rtnetlink.h>
5530ffee84SJohannes Berg #include <net/net_namespace.h>
561da177e4SLinus Torvalds 
576264f58cSJakub Kicinski #include "dev.h"
586264f58cSJakub Kicinski 
59a428afe8SNikolay Aleksandrov #define RTNL_MAX_TYPE		50
602dba407fSNikolay Aleksandrov #define RTNL_SLAVE_MAX_TYPE	40
61ccf8dbcdSKees Cook 
62e0d087afSEric Dumazet struct rtnl_link {
63e2849863SThomas Graf 	rtnl_doit_func		doit;
64e2849863SThomas Graf 	rtnl_dumpit_func	dumpit;
65e4202511SFlorian Westphal 	struct module		*owner;
6662256f98SFlorian Westphal 	unsigned int		flags;
67addf9b90SFlorian Westphal 	struct rcu_head		rcu;
68e2849863SThomas Graf };
69e2849863SThomas Graf 
706756ae4bSStephen Hemminger static DEFINE_MUTEX(rtnl_mutex);
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds void rtnl_lock(void)
731da177e4SLinus Torvalds {
746756ae4bSStephen Hemminger 	mutex_lock(&rtnl_mutex);
751da177e4SLinus Torvalds }
76e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_lock);
771da177e4SLinus Torvalds 
7879ffdfc6SKirill Tkhai int rtnl_lock_killable(void)
7979ffdfc6SKirill Tkhai {
8079ffdfc6SKirill Tkhai 	return mutex_lock_killable(&rtnl_mutex);
8179ffdfc6SKirill Tkhai }
8279ffdfc6SKirill Tkhai EXPORT_SYMBOL(rtnl_lock_killable);
8379ffdfc6SKirill Tkhai 
841b5c5493SEric Dumazet static struct sk_buff *defer_kfree_skb_list;
851b5c5493SEric Dumazet void rtnl_kfree_skbs(struct sk_buff *head, struct sk_buff *tail)
861b5c5493SEric Dumazet {
871b5c5493SEric Dumazet 	if (head && tail) {
881b5c5493SEric Dumazet 		tail->next = defer_kfree_skb_list;
891b5c5493SEric Dumazet 		defer_kfree_skb_list = head;
901b5c5493SEric Dumazet 	}
911b5c5493SEric Dumazet }
921b5c5493SEric Dumazet EXPORT_SYMBOL(rtnl_kfree_skbs);
931b5c5493SEric Dumazet 
946756ae4bSStephen Hemminger void __rtnl_unlock(void)
951da177e4SLinus Torvalds {
961b5c5493SEric Dumazet 	struct sk_buff *head = defer_kfree_skb_list;
971b5c5493SEric Dumazet 
981b5c5493SEric Dumazet 	defer_kfree_skb_list = NULL;
991b5c5493SEric Dumazet 
1000b5c21bbSJohannes Berg 	/* Ensure that we didn't actually add any TODO item when __rtnl_unlock()
1010b5c21bbSJohannes Berg 	 * is used. In some places, e.g. in cfg80211, we have code that will do
1020b5c21bbSJohannes Berg 	 * something like
1030b5c21bbSJohannes Berg 	 *   rtnl_lock()
1040b5c21bbSJohannes Berg 	 *   wiphy_lock()
1050b5c21bbSJohannes Berg 	 *   ...
1060b5c21bbSJohannes Berg 	 *   rtnl_unlock()
1070b5c21bbSJohannes Berg 	 *
1080b5c21bbSJohannes Berg 	 * and because netdev_run_todo() acquires the RTNL for items on the list
1090b5c21bbSJohannes Berg 	 * we could cause a situation such as this:
1100b5c21bbSJohannes Berg 	 * Thread 1			Thread 2
1110b5c21bbSJohannes Berg 	 *				  rtnl_lock()
1120b5c21bbSJohannes Berg 	 *				  unregister_netdevice()
1130b5c21bbSJohannes Berg 	 *				  __rtnl_unlock()
1140b5c21bbSJohannes Berg 	 * rtnl_lock()
1150b5c21bbSJohannes Berg 	 * wiphy_lock()
1160b5c21bbSJohannes Berg 	 * rtnl_unlock()
1170b5c21bbSJohannes Berg 	 *   netdev_run_todo()
1180b5c21bbSJohannes Berg 	 *     __rtnl_unlock()
1190b5c21bbSJohannes Berg 	 *
1200b5c21bbSJohannes Berg 	 *     // list not empty now
1210b5c21bbSJohannes Berg 	 *     // because of thread 2
1220b5c21bbSJohannes Berg 	 *				  rtnl_lock()
1230b5c21bbSJohannes Berg 	 *     while (!list_empty(...))
1240b5c21bbSJohannes Berg 	 *       rtnl_lock()
1250b5c21bbSJohannes Berg 	 *				  wiphy_lock()
1260b5c21bbSJohannes Berg 	 * **** DEADLOCK ****
1270b5c21bbSJohannes Berg 	 *
1280b5c21bbSJohannes Berg 	 * However, usage of __rtnl_unlock() is rare, and so we can ensure that
1290b5c21bbSJohannes Berg 	 * it's not used in cases where something is added to do the list.
1300b5c21bbSJohannes Berg 	 */
1310b5c21bbSJohannes Berg 	WARN_ON(!list_empty(&net_todo_list));
1320b5c21bbSJohannes Berg 
1336756ae4bSStephen Hemminger 	mutex_unlock(&rtnl_mutex);
1341b5c5493SEric Dumazet 
1351b5c5493SEric Dumazet 	while (head) {
1361b5c5493SEric Dumazet 		struct sk_buff *next = head->next;
1371b5c5493SEric Dumazet 
1381b5c5493SEric Dumazet 		kfree_skb(head);
1391b5c5493SEric Dumazet 		cond_resched();
1401b5c5493SEric Dumazet 		head = next;
1411b5c5493SEric Dumazet 	}
1421da177e4SLinus Torvalds }
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds void rtnl_unlock(void)
1451da177e4SLinus Torvalds {
14658ec3b4dSHerbert Xu 	/* This fellow will unlock it for us. */
1471da177e4SLinus Torvalds 	netdev_run_todo();
1481da177e4SLinus Torvalds }
149e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unlock);
1501da177e4SLinus Torvalds 
1516756ae4bSStephen Hemminger int rtnl_trylock(void)
1526756ae4bSStephen Hemminger {
1536756ae4bSStephen Hemminger 	return mutex_trylock(&rtnl_mutex);
1546756ae4bSStephen Hemminger }
155e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_trylock);
1566756ae4bSStephen Hemminger 
157c9c1014bSPatrick McHardy int rtnl_is_locked(void)
158c9c1014bSPatrick McHardy {
159c9c1014bSPatrick McHardy 	return mutex_is_locked(&rtnl_mutex);
160c9c1014bSPatrick McHardy }
161e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_is_locked);
162c9c1014bSPatrick McHardy 
1636f99528eSVlad Buslov bool refcount_dec_and_rtnl_lock(refcount_t *r)
1646f99528eSVlad Buslov {
1656f99528eSVlad Buslov 	return refcount_dec_and_mutex_lock(r, &rtnl_mutex);
1666f99528eSVlad Buslov }
1676f99528eSVlad Buslov EXPORT_SYMBOL(refcount_dec_and_rtnl_lock);
1686f99528eSVlad Buslov 
169a898def2SPaul E. McKenney #ifdef CONFIG_PROVE_LOCKING
1700cbf3343SYaowei Bai bool lockdep_rtnl_is_held(void)
171a898def2SPaul E. McKenney {
172a898def2SPaul E. McKenney 	return lockdep_is_held(&rtnl_mutex);
173a898def2SPaul E. McKenney }
174a898def2SPaul E. McKenney EXPORT_SYMBOL(lockdep_rtnl_is_held);
175a898def2SPaul E. McKenney #endif /* #ifdef CONFIG_PROVE_LOCKING */
176a898def2SPaul E. McKenney 
17751e13685SJakub Kicinski static struct rtnl_link __rcu *__rcu *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
178e2849863SThomas Graf 
179e2849863SThomas Graf static inline int rtm_msgindex(int msgtype)
180e2849863SThomas Graf {
181e2849863SThomas Graf 	int msgindex = msgtype - RTM_BASE;
182e2849863SThomas Graf 
183e2849863SThomas Graf 	/*
184e2849863SThomas Graf 	 * msgindex < 0 implies someone tried to register a netlink
185e2849863SThomas Graf 	 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
186e2849863SThomas Graf 	 * the message type has not been added to linux/rtnetlink.h
187e2849863SThomas Graf 	 */
188e2849863SThomas Graf 	BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
189e2849863SThomas Graf 
190e2849863SThomas Graf 	return msgindex;
191e2849863SThomas Graf }
192e2849863SThomas Graf 
193addf9b90SFlorian Westphal static struct rtnl_link *rtnl_get_link(int protocol, int msgtype)
194addf9b90SFlorian Westphal {
19551e13685SJakub Kicinski 	struct rtnl_link __rcu **tab;
196addf9b90SFlorian Westphal 
197addf9b90SFlorian Westphal 	if (protocol >= ARRAY_SIZE(rtnl_msg_handlers))
198addf9b90SFlorian Westphal 		protocol = PF_UNSPEC;
199addf9b90SFlorian Westphal 
200addf9b90SFlorian Westphal 	tab = rcu_dereference_rtnl(rtnl_msg_handlers[protocol]);
201addf9b90SFlorian Westphal 	if (!tab)
202addf9b90SFlorian Westphal 		tab = rcu_dereference_rtnl(rtnl_msg_handlers[PF_UNSPEC]);
203addf9b90SFlorian Westphal 
20451e13685SJakub Kicinski 	return rcu_dereference_rtnl(tab[msgtype]);
205addf9b90SFlorian Westphal }
206addf9b90SFlorian Westphal 
207e4202511SFlorian Westphal static int rtnl_register_internal(struct module *owner,
208e4202511SFlorian Westphal 				  int protocol, int msgtype,
209c7ac8679SGreg Rose 				  rtnl_doit_func doit, rtnl_dumpit_func dumpit,
210b97bac64SFlorian Westphal 				  unsigned int flags)
211e2849863SThomas Graf {
212b0e9fe1bSFlorian Westphal 	struct rtnl_link *link, *old;
213b0e9fe1bSFlorian Westphal 	struct rtnl_link __rcu **tab;
214e2849863SThomas Graf 	int msgindex;
215addf9b90SFlorian Westphal 	int ret = -ENOBUFS;
216e2849863SThomas Graf 
21725239ceeSPatrick McHardy 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
218e2849863SThomas Graf 	msgindex = rtm_msgindex(msgtype);
219e2849863SThomas Graf 
220addf9b90SFlorian Westphal 	rtnl_lock();
22151e13685SJakub Kicinski 	tab = rtnl_dereference(rtnl_msg_handlers[protocol]);
222e2849863SThomas Graf 	if (tab == NULL) {
223addf9b90SFlorian Westphal 		tab = kcalloc(RTM_NR_MSGTYPES, sizeof(void *), GFP_KERNEL);
224addf9b90SFlorian Westphal 		if (!tab)
225addf9b90SFlorian Westphal 			goto unlock;
226e2849863SThomas Graf 
227addf9b90SFlorian Westphal 		/* ensures we see the 0 stores */
2286853dd48SFlorian Westphal 		rcu_assign_pointer(rtnl_msg_handlers[protocol], tab);
229e2849863SThomas Graf 	}
230e2849863SThomas Graf 
231addf9b90SFlorian Westphal 	old = rtnl_dereference(tab[msgindex]);
232addf9b90SFlorian Westphal 	if (old) {
233addf9b90SFlorian Westphal 		link = kmemdup(old, sizeof(*old), GFP_KERNEL);
234addf9b90SFlorian Westphal 		if (!link)
235addf9b90SFlorian Westphal 			goto unlock;
236addf9b90SFlorian Westphal 	} else {
237addf9b90SFlorian Westphal 		link = kzalloc(sizeof(*link), GFP_KERNEL);
238addf9b90SFlorian Westphal 		if (!link)
239addf9b90SFlorian Westphal 			goto unlock;
240addf9b90SFlorian Westphal 	}
241e2849863SThomas Graf 
242e4202511SFlorian Westphal 	WARN_ON(link->owner && link->owner != owner);
243e4202511SFlorian Westphal 	link->owner = owner;
244e4202511SFlorian Westphal 
245addf9b90SFlorian Westphal 	WARN_ON(doit && link->doit && link->doit != doit);
246addf9b90SFlorian Westphal 	if (doit)
247addf9b90SFlorian Westphal 		link->doit = doit;
248addf9b90SFlorian Westphal 	WARN_ON(dumpit && link->dumpit && link->dumpit != dumpit);
249addf9b90SFlorian Westphal 	if (dumpit)
250addf9b90SFlorian Westphal 		link->dumpit = dumpit;
251addf9b90SFlorian Westphal 
252a6cec0bcSNikolay Aleksandrov 	WARN_ON(rtnl_msgtype_kind(msgtype) != RTNL_KIND_DEL &&
253a6cec0bcSNikolay Aleksandrov 		(flags & RTNL_FLAG_BULK_DEL_SUPPORTED));
254addf9b90SFlorian Westphal 	link->flags |= flags;
255addf9b90SFlorian Westphal 
256addf9b90SFlorian Westphal 	/* publish protocol:msgtype */
257addf9b90SFlorian Westphal 	rcu_assign_pointer(tab[msgindex], link);
258addf9b90SFlorian Westphal 	ret = 0;
259addf9b90SFlorian Westphal 	if (old)
260addf9b90SFlorian Westphal 		kfree_rcu(old, rcu);
261addf9b90SFlorian Westphal unlock:
262addf9b90SFlorian Westphal 	rtnl_unlock();
263addf9b90SFlorian Westphal 	return ret;
264e2849863SThomas Graf }
265e4202511SFlorian Westphal 
266e4202511SFlorian Westphal /**
267e4202511SFlorian Westphal  * rtnl_register_module - Register a rtnetlink message type
268e4202511SFlorian Westphal  *
269e4202511SFlorian Westphal  * @owner: module registering the hook (THIS_MODULE)
270e4202511SFlorian Westphal  * @protocol: Protocol family or PF_UNSPEC
271e4202511SFlorian Westphal  * @msgtype: rtnetlink message type
272e4202511SFlorian Westphal  * @doit: Function pointer called for each request message
273e4202511SFlorian Westphal  * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
274d467d0bcSZheng Yongjun  * @flags: rtnl_link_flags to modify behaviour of doit/dumpit functions
275e4202511SFlorian Westphal  *
276e4202511SFlorian Westphal  * Like rtnl_register, but for use by removable modules.
277e4202511SFlorian Westphal  */
278e4202511SFlorian Westphal int rtnl_register_module(struct module *owner,
279e4202511SFlorian Westphal 			 int protocol, int msgtype,
280e4202511SFlorian Westphal 			 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
281e4202511SFlorian Westphal 			 unsigned int flags)
282e4202511SFlorian Westphal {
283e4202511SFlorian Westphal 	return rtnl_register_internal(owner, protocol, msgtype,
284e4202511SFlorian Westphal 				      doit, dumpit, flags);
285e4202511SFlorian Westphal }
286e4202511SFlorian Westphal EXPORT_SYMBOL_GPL(rtnl_register_module);
287e4202511SFlorian Westphal 
288e4202511SFlorian Westphal /**
28916feebcfSFlorian Westphal  * rtnl_register - Register a rtnetlink message type
290e4202511SFlorian Westphal  * @protocol: Protocol family or PF_UNSPEC
291e4202511SFlorian Westphal  * @msgtype: rtnetlink message type
292e4202511SFlorian Westphal  * @doit: Function pointer called for each request message
293e4202511SFlorian Westphal  * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
294d467d0bcSZheng Yongjun  * @flags: rtnl_link_flags to modify behaviour of doit/dumpit functions
295e4202511SFlorian Westphal  *
296e4202511SFlorian Westphal  * Registers the specified function pointers (at least one of them has
297e4202511SFlorian Westphal  * to be non-NULL) to be called whenever a request message for the
298e4202511SFlorian Westphal  * specified protocol family and message type is received.
299e4202511SFlorian Westphal  *
300e4202511SFlorian Westphal  * The special protocol family PF_UNSPEC may be used to define fallback
301e4202511SFlorian Westphal  * function pointers for the case when no entry for the specific protocol
302e4202511SFlorian Westphal  * family exists.
303e2849863SThomas Graf  */
304e2849863SThomas Graf void rtnl_register(int protocol, int msgtype,
305c7ac8679SGreg Rose 		   rtnl_doit_func doit, rtnl_dumpit_func dumpit,
306b97bac64SFlorian Westphal 		   unsigned int flags)
307e2849863SThomas Graf {
30816feebcfSFlorian Westphal 	int err;
30916feebcfSFlorian Westphal 
31016feebcfSFlorian Westphal 	err = rtnl_register_internal(NULL, protocol, msgtype, doit, dumpit,
31116feebcfSFlorian Westphal 				     flags);
31216feebcfSFlorian Westphal 	if (err)
31316feebcfSFlorian Westphal 		pr_err("Unable to register rtnetlink message handler, "
31416feebcfSFlorian Westphal 		       "protocol = %d, message type = %d\n", protocol, msgtype);
315e2849863SThomas Graf }
316e2849863SThomas Graf 
317e2849863SThomas Graf /**
318e2849863SThomas Graf  * rtnl_unregister - Unregister a rtnetlink message type
319e2849863SThomas Graf  * @protocol: Protocol family or PF_UNSPEC
320e2849863SThomas Graf  * @msgtype: rtnetlink message type
321e2849863SThomas Graf  *
322e2849863SThomas Graf  * Returns 0 on success or a negative error code.
323e2849863SThomas Graf  */
324e2849863SThomas Graf int rtnl_unregister(int protocol, int msgtype)
325e2849863SThomas Graf {
32651e13685SJakub Kicinski 	struct rtnl_link __rcu **tab;
32751e13685SJakub Kicinski 	struct rtnl_link *link;
328e2849863SThomas Graf 	int msgindex;
329e2849863SThomas Graf 
33025239ceeSPatrick McHardy 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
331e2849863SThomas Graf 	msgindex = rtm_msgindex(msgtype);
332e2849863SThomas Graf 
3336853dd48SFlorian Westphal 	rtnl_lock();
334addf9b90SFlorian Westphal 	tab = rtnl_dereference(rtnl_msg_handlers[protocol]);
335addf9b90SFlorian Westphal 	if (!tab) {
3366853dd48SFlorian Westphal 		rtnl_unlock();
337e2849863SThomas Graf 		return -ENOENT;
3386853dd48SFlorian Westphal 	}
339e2849863SThomas Graf 
34051e13685SJakub Kicinski 	link = rtnl_dereference(tab[msgindex]);
3414fc29989SYajun Deng 	RCU_INIT_POINTER(tab[msgindex], NULL);
3426853dd48SFlorian Westphal 	rtnl_unlock();
343e2849863SThomas Graf 
344addf9b90SFlorian Westphal 	kfree_rcu(link, rcu);
345addf9b90SFlorian Westphal 
346e2849863SThomas Graf 	return 0;
347e2849863SThomas Graf }
348e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister);
349e2849863SThomas Graf 
350e2849863SThomas Graf /**
351e2849863SThomas Graf  * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
352e2849863SThomas Graf  * @protocol : Protocol family or PF_UNSPEC
353e2849863SThomas Graf  *
354e2849863SThomas Graf  * Identical to calling rtnl_unregster() for all registered message types
355e2849863SThomas Graf  * of a certain protocol family.
356e2849863SThomas Graf  */
357e2849863SThomas Graf void rtnl_unregister_all(int protocol)
358e2849863SThomas Graf {
35951e13685SJakub Kicinski 	struct rtnl_link __rcu **tab;
36051e13685SJakub Kicinski 	struct rtnl_link *link;
361addf9b90SFlorian Westphal 	int msgindex;
362019a3169SFlorian Westphal 
36325239ceeSPatrick McHardy 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
364e2849863SThomas Graf 
365019a3169SFlorian Westphal 	rtnl_lock();
36651e13685SJakub Kicinski 	tab = rtnl_dereference(rtnl_msg_handlers[protocol]);
367f707ef61SSabrina Dubroca 	if (!tab) {
368f707ef61SSabrina Dubroca 		rtnl_unlock();
369f707ef61SSabrina Dubroca 		return;
370f707ef61SSabrina Dubroca 	}
3716853dd48SFlorian Westphal 	RCU_INIT_POINTER(rtnl_msg_handlers[protocol], NULL);
372addf9b90SFlorian Westphal 	for (msgindex = 0; msgindex < RTM_NR_MSGTYPES; msgindex++) {
37351e13685SJakub Kicinski 		link = rtnl_dereference(tab[msgindex]);
374addf9b90SFlorian Westphal 		if (!link)
375addf9b90SFlorian Westphal 			continue;
376addf9b90SFlorian Westphal 
3774fc29989SYajun Deng 		RCU_INIT_POINTER(tab[msgindex], NULL);
378addf9b90SFlorian Westphal 		kfree_rcu(link, rcu);
379addf9b90SFlorian Westphal 	}
380019a3169SFlorian Westphal 	rtnl_unlock();
381019a3169SFlorian Westphal 
3826853dd48SFlorian Westphal 	synchronize_net();
3836853dd48SFlorian Westphal 
384addf9b90SFlorian Westphal 	kfree(tab);
385e2849863SThomas Graf }
386e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister_all);
3871da177e4SLinus Torvalds 
38838f7b870SPatrick McHardy static LIST_HEAD(link_ops);
38938f7b870SPatrick McHardy 
390c63044f0SEric Dumazet static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
391c63044f0SEric Dumazet {
392c63044f0SEric Dumazet 	const struct rtnl_link_ops *ops;
393c63044f0SEric Dumazet 
394c63044f0SEric Dumazet 	list_for_each_entry(ops, &link_ops, list) {
395c63044f0SEric Dumazet 		if (!strcmp(ops->kind, kind))
396c63044f0SEric Dumazet 			return ops;
397c63044f0SEric Dumazet 	}
398c63044f0SEric Dumazet 	return NULL;
399c63044f0SEric Dumazet }
400c63044f0SEric Dumazet 
40138f7b870SPatrick McHardy /**
40238f7b870SPatrick McHardy  * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
40338f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to register
40438f7b870SPatrick McHardy  *
40538f7b870SPatrick McHardy  * The caller must hold the rtnl_mutex. This function should be used
40638f7b870SPatrick McHardy  * by drivers that create devices during module initialization. It
40738f7b870SPatrick McHardy  * must be called before registering the devices.
40838f7b870SPatrick McHardy  *
40938f7b870SPatrick McHardy  * Returns 0 on success or a negative error code.
41038f7b870SPatrick McHardy  */
41138f7b870SPatrick McHardy int __rtnl_link_register(struct rtnl_link_ops *ops)
41238f7b870SPatrick McHardy {
413c63044f0SEric Dumazet 	if (rtnl_link_ops_get(ops->kind))
414c63044f0SEric Dumazet 		return -EEXIST;
415c63044f0SEric Dumazet 
4168c713dc9SJohannes Berg 	/* The check for alloc/setup is here because if ops
417b0ab2fabSJiri Pirko 	 * does not have that filled up, it is not possible
418b0ab2fabSJiri Pirko 	 * to use the ops for creating device. So do not
419b0ab2fabSJiri Pirko 	 * fill up dellink as well. That disables rtnl_dellink.
420b0ab2fabSJiri Pirko 	 */
4218c713dc9SJohannes Berg 	if ((ops->alloc || ops->setup) && !ops->dellink)
42223289a37SEric Dumazet 		ops->dellink = unregister_netdevice_queue;
4232d85cba2SPatrick McHardy 
42438f7b870SPatrick McHardy 	list_add_tail(&ops->list, &link_ops);
42538f7b870SPatrick McHardy 	return 0;
42638f7b870SPatrick McHardy }
42738f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_register);
42838f7b870SPatrick McHardy 
42938f7b870SPatrick McHardy /**
43038f7b870SPatrick McHardy  * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
43138f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to register
43238f7b870SPatrick McHardy  *
43338f7b870SPatrick McHardy  * Returns 0 on success or a negative error code.
43438f7b870SPatrick McHardy  */
43538f7b870SPatrick McHardy int rtnl_link_register(struct rtnl_link_ops *ops)
43638f7b870SPatrick McHardy {
43738f7b870SPatrick McHardy 	int err;
43838f7b870SPatrick McHardy 
439ccf8dbcdSKees Cook 	/* Sanity-check max sizes to avoid stack buffer overflow. */
440ccf8dbcdSKees Cook 	if (WARN_ON(ops->maxtype > RTNL_MAX_TYPE ||
441ccf8dbcdSKees Cook 		    ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE))
442ccf8dbcdSKees Cook 		return -EINVAL;
443ccf8dbcdSKees Cook 
44438f7b870SPatrick McHardy 	rtnl_lock();
44538f7b870SPatrick McHardy 	err = __rtnl_link_register(ops);
44638f7b870SPatrick McHardy 	rtnl_unlock();
44738f7b870SPatrick McHardy 	return err;
44838f7b870SPatrick McHardy }
44938f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_register);
45038f7b870SPatrick McHardy 
451669f87baSPavel Emelyanov static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
452669f87baSPavel Emelyanov {
453669f87baSPavel Emelyanov 	struct net_device *dev;
45423289a37SEric Dumazet 	LIST_HEAD(list_kill);
45523289a37SEric Dumazet 
456669f87baSPavel Emelyanov 	for_each_netdev(net, dev) {
45723289a37SEric Dumazet 		if (dev->rtnl_link_ops == ops)
45823289a37SEric Dumazet 			ops->dellink(dev, &list_kill);
459669f87baSPavel Emelyanov 	}
46023289a37SEric Dumazet 	unregister_netdevice_many(&list_kill);
461669f87baSPavel Emelyanov }
462669f87baSPavel Emelyanov 
46338f7b870SPatrick McHardy /**
46438f7b870SPatrick McHardy  * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
46538f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to unregister
46638f7b870SPatrick McHardy  *
467554873e5SKirill Tkhai  * The caller must hold the rtnl_mutex and guarantee net_namespace_list
468554873e5SKirill Tkhai  * integrity (hold pernet_ops_rwsem for writing to close the race
469554873e5SKirill Tkhai  * with setup_net() and cleanup_net()).
47038f7b870SPatrick McHardy  */
47138f7b870SPatrick McHardy void __rtnl_link_unregister(struct rtnl_link_ops *ops)
47238f7b870SPatrick McHardy {
473881d966bSEric W. Biederman 	struct net *net;
4742d85cba2SPatrick McHardy 
475881d966bSEric W. Biederman 	for_each_net(net) {
476669f87baSPavel Emelyanov 		__rtnl_kill_links(net, ops);
477881d966bSEric W. Biederman 	}
47838f7b870SPatrick McHardy 	list_del(&ops->list);
47938f7b870SPatrick McHardy }
48038f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
48138f7b870SPatrick McHardy 
482200b916fSCong Wang /* Return with the rtnl_lock held when there are no network
483200b916fSCong Wang  * devices unregistering in any network namespace.
484200b916fSCong Wang  */
485200b916fSCong Wang static void rtnl_lock_unregistering_all(void)
486200b916fSCong Wang {
487200b916fSCong Wang 	struct net *net;
488200b916fSCong Wang 	bool unregistering;
489ff960a73SPeter Zijlstra 	DEFINE_WAIT_FUNC(wait, woken_wake_function);
490200b916fSCong Wang 
491ff960a73SPeter Zijlstra 	add_wait_queue(&netdev_unregistering_wq, &wait);
492200b916fSCong Wang 	for (;;) {
493200b916fSCong Wang 		unregistering = false;
494200b916fSCong Wang 		rtnl_lock();
495f0b07bb1SKirill Tkhai 		/* We held write locked pernet_ops_rwsem, and parallel
496f0b07bb1SKirill Tkhai 		 * setup_net() and cleanup_net() are not possible.
497f0b07bb1SKirill Tkhai 		 */
498200b916fSCong Wang 		for_each_net(net) {
499ede6c39cSEric Dumazet 			if (atomic_read(&net->dev_unreg_count) > 0) {
500200b916fSCong Wang 				unregistering = true;
501200b916fSCong Wang 				break;
502200b916fSCong Wang 			}
503200b916fSCong Wang 		}
504200b916fSCong Wang 		if (!unregistering)
505200b916fSCong Wang 			break;
506200b916fSCong Wang 		__rtnl_unlock();
507ff960a73SPeter Zijlstra 
508ff960a73SPeter Zijlstra 		wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
509200b916fSCong Wang 	}
510ff960a73SPeter Zijlstra 	remove_wait_queue(&netdev_unregistering_wq, &wait);
511200b916fSCong Wang }
512200b916fSCong Wang 
51338f7b870SPatrick McHardy /**
51438f7b870SPatrick McHardy  * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
51538f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to unregister
51638f7b870SPatrick McHardy  */
51738f7b870SPatrick McHardy void rtnl_link_unregister(struct rtnl_link_ops *ops)
51838f7b870SPatrick McHardy {
5198518e9bbSKirill Tkhai 	/* Close the race with setup_net() and cleanup_net() */
5204420bf21SKirill Tkhai 	down_write(&pernet_ops_rwsem);
521200b916fSCong Wang 	rtnl_lock_unregistering_all();
52238f7b870SPatrick McHardy 	__rtnl_link_unregister(ops);
52338f7b870SPatrick McHardy 	rtnl_unlock();
5244420bf21SKirill Tkhai 	up_write(&pernet_ops_rwsem);
52538f7b870SPatrick McHardy }
52638f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_unregister);
52738f7b870SPatrick McHardy 
528ba7d49b1SJiri Pirko static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
529ba7d49b1SJiri Pirko {
530ba7d49b1SJiri Pirko 	struct net_device *master_dev;
531ba7d49b1SJiri Pirko 	const struct rtnl_link_ops *ops;
5328515ae38SFlorian Westphal 	size_t size = 0;
533ba7d49b1SJiri Pirko 
5348515ae38SFlorian Westphal 	rcu_read_lock();
5358515ae38SFlorian Westphal 
5368515ae38SFlorian Westphal 	master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
537ba7d49b1SJiri Pirko 	if (!master_dev)
5388515ae38SFlorian Westphal 		goto out;
5398515ae38SFlorian Westphal 
540ba7d49b1SJiri Pirko 	ops = master_dev->rtnl_link_ops;
5416049f253SFernando Luis Vazquez Cao 	if (!ops || !ops->get_slave_size)
5428515ae38SFlorian Westphal 		goto out;
543ba7d49b1SJiri Pirko 	/* IFLA_INFO_SLAVE_DATA + nested data */
5448515ae38SFlorian Westphal 	size = nla_total_size(sizeof(struct nlattr)) +
545ba7d49b1SJiri Pirko 	       ops->get_slave_size(master_dev, dev);
5468515ae38SFlorian Westphal 
5478515ae38SFlorian Westphal out:
5488515ae38SFlorian Westphal 	rcu_read_unlock();
5498515ae38SFlorian Westphal 	return size;
550ba7d49b1SJiri Pirko }
551ba7d49b1SJiri Pirko 
55238f7b870SPatrick McHardy static size_t rtnl_link_get_size(const struct net_device *dev)
55338f7b870SPatrick McHardy {
55438f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
55538f7b870SPatrick McHardy 	size_t size;
55638f7b870SPatrick McHardy 
55738f7b870SPatrick McHardy 	if (!ops)
55838f7b870SPatrick McHardy 		return 0;
55938f7b870SPatrick McHardy 
560369cf77aSThomas Graf 	size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
561369cf77aSThomas Graf 	       nla_total_size(strlen(ops->kind) + 1);  /* IFLA_INFO_KIND */
56238f7b870SPatrick McHardy 
56338f7b870SPatrick McHardy 	if (ops->get_size)
56438f7b870SPatrick McHardy 		/* IFLA_INFO_DATA + nested data */
565369cf77aSThomas Graf 		size += nla_total_size(sizeof(struct nlattr)) +
56638f7b870SPatrick McHardy 			ops->get_size(dev);
56738f7b870SPatrick McHardy 
56838f7b870SPatrick McHardy 	if (ops->get_xstats_size)
569369cf77aSThomas Graf 		/* IFLA_INFO_XSTATS */
570369cf77aSThomas Graf 		size += nla_total_size(ops->get_xstats_size(dev));
57138f7b870SPatrick McHardy 
572ba7d49b1SJiri Pirko 	size += rtnl_link_get_slave_info_data_size(dev);
573ba7d49b1SJiri Pirko 
57438f7b870SPatrick McHardy 	return size;
57538f7b870SPatrick McHardy }
57638f7b870SPatrick McHardy 
577f8ff182cSThomas Graf static LIST_HEAD(rtnl_af_ops);
578f8ff182cSThomas Graf 
579f8ff182cSThomas Graf static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
580f8ff182cSThomas Graf {
581f8ff182cSThomas Graf 	const struct rtnl_af_ops *ops;
582f8ff182cSThomas Graf 
583a100243dSCong Wang 	ASSERT_RTNL();
584a100243dSCong Wang 
585a100243dSCong Wang 	list_for_each_entry(ops, &rtnl_af_ops, list) {
586f8ff182cSThomas Graf 		if (ops->family == family)
587f8ff182cSThomas Graf 			return ops;
588f8ff182cSThomas Graf 	}
589f8ff182cSThomas Graf 
590f8ff182cSThomas Graf 	return NULL;
591f8ff182cSThomas Graf }
592f8ff182cSThomas Graf 
593f8ff182cSThomas Graf /**
594f8ff182cSThomas Graf  * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
595f8ff182cSThomas Graf  * @ops: struct rtnl_af_ops * to register
596f8ff182cSThomas Graf  *
597f8ff182cSThomas Graf  * Returns 0 on success or a negative error code.
598f8ff182cSThomas Graf  */
5993678a9d8Sstephen hemminger void rtnl_af_register(struct rtnl_af_ops *ops)
600f8ff182cSThomas Graf {
601f8ff182cSThomas Graf 	rtnl_lock();
6025fa85a09SFlorian Westphal 	list_add_tail_rcu(&ops->list, &rtnl_af_ops);
603f8ff182cSThomas Graf 	rtnl_unlock();
604f8ff182cSThomas Graf }
605f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_register);
606f8ff182cSThomas Graf 
607f8ff182cSThomas Graf /**
608f8ff182cSThomas Graf  * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
609f8ff182cSThomas Graf  * @ops: struct rtnl_af_ops * to unregister
610f8ff182cSThomas Graf  */
611f8ff182cSThomas Graf void rtnl_af_unregister(struct rtnl_af_ops *ops)
612f8ff182cSThomas Graf {
613f8ff182cSThomas Graf 	rtnl_lock();
6145fa85a09SFlorian Westphal 	list_del_rcu(&ops->list);
615f8ff182cSThomas Graf 	rtnl_unlock();
6165fa85a09SFlorian Westphal 
6175fa85a09SFlorian Westphal 	synchronize_rcu();
618f8ff182cSThomas Graf }
619f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_unregister);
620f8ff182cSThomas Graf 
621b1974ed0SArad, Ronen static size_t rtnl_link_get_af_size(const struct net_device *dev,
622b1974ed0SArad, Ronen 				    u32 ext_filter_mask)
623f8ff182cSThomas Graf {
624f8ff182cSThomas Graf 	struct rtnl_af_ops *af_ops;
625f8ff182cSThomas Graf 	size_t size;
626f8ff182cSThomas Graf 
627f8ff182cSThomas Graf 	/* IFLA_AF_SPEC */
628f8ff182cSThomas Graf 	size = nla_total_size(sizeof(struct nlattr));
629f8ff182cSThomas Graf 
6305fa85a09SFlorian Westphal 	rcu_read_lock();
6315fa85a09SFlorian Westphal 	list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
632f8ff182cSThomas Graf 		if (af_ops->get_link_af_size) {
633f8ff182cSThomas Graf 			/* AF_* + nested data */
634f8ff182cSThomas Graf 			size += nla_total_size(sizeof(struct nlattr)) +
635b1974ed0SArad, Ronen 				af_ops->get_link_af_size(dev, ext_filter_mask);
636f8ff182cSThomas Graf 		}
637f8ff182cSThomas Graf 	}
6385fa85a09SFlorian Westphal 	rcu_read_unlock();
639f8ff182cSThomas Graf 
640f8ff182cSThomas Graf 	return size;
641f8ff182cSThomas Graf }
642f8ff182cSThomas Graf 
643ba7d49b1SJiri Pirko static bool rtnl_have_link_slave_info(const struct net_device *dev)
644ba7d49b1SJiri Pirko {
645ba7d49b1SJiri Pirko 	struct net_device *master_dev;
6464c82a95eSFlorian Westphal 	bool ret = false;
647ba7d49b1SJiri Pirko 
6484c82a95eSFlorian Westphal 	rcu_read_lock();
6494c82a95eSFlorian Westphal 
6504c82a95eSFlorian Westphal 	master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
651813f020cSJiri Pirko 	if (master_dev && master_dev->rtnl_link_ops)
6524c82a95eSFlorian Westphal 		ret = true;
6534c82a95eSFlorian Westphal 	rcu_read_unlock();
6544c82a95eSFlorian Westphal 	return ret;
655ba7d49b1SJiri Pirko }
656ba7d49b1SJiri Pirko 
657ba7d49b1SJiri Pirko static int rtnl_link_slave_info_fill(struct sk_buff *skb,
658ba7d49b1SJiri Pirko 				     const struct net_device *dev)
659ba7d49b1SJiri Pirko {
660ba7d49b1SJiri Pirko 	struct net_device *master_dev;
661ba7d49b1SJiri Pirko 	const struct rtnl_link_ops *ops;
662ba7d49b1SJiri Pirko 	struct nlattr *slave_data;
663ba7d49b1SJiri Pirko 	int err;
664ba7d49b1SJiri Pirko 
665ba7d49b1SJiri Pirko 	master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
666ba7d49b1SJiri Pirko 	if (!master_dev)
667ba7d49b1SJiri Pirko 		return 0;
668ba7d49b1SJiri Pirko 	ops = master_dev->rtnl_link_ops;
669ba7d49b1SJiri Pirko 	if (!ops)
670ba7d49b1SJiri Pirko 		return 0;
671ba7d49b1SJiri Pirko 	if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
672ba7d49b1SJiri Pirko 		return -EMSGSIZE;
673ba7d49b1SJiri Pirko 	if (ops->fill_slave_info) {
674ae0be8deSMichal Kubecek 		slave_data = nla_nest_start_noflag(skb, IFLA_INFO_SLAVE_DATA);
675ba7d49b1SJiri Pirko 		if (!slave_data)
676ba7d49b1SJiri Pirko 			return -EMSGSIZE;
677ba7d49b1SJiri Pirko 		err = ops->fill_slave_info(skb, master_dev, dev);
678ba7d49b1SJiri Pirko 		if (err < 0)
679ba7d49b1SJiri Pirko 			goto err_cancel_slave_data;
680ba7d49b1SJiri Pirko 		nla_nest_end(skb, slave_data);
681ba7d49b1SJiri Pirko 	}
682ba7d49b1SJiri Pirko 	return 0;
683ba7d49b1SJiri Pirko 
684ba7d49b1SJiri Pirko err_cancel_slave_data:
685ba7d49b1SJiri Pirko 	nla_nest_cancel(skb, slave_data);
686ba7d49b1SJiri Pirko 	return err;
687ba7d49b1SJiri Pirko }
688ba7d49b1SJiri Pirko 
689ba7d49b1SJiri Pirko static int rtnl_link_info_fill(struct sk_buff *skb,
690ba7d49b1SJiri Pirko 			       const struct net_device *dev)
69138f7b870SPatrick McHardy {
69238f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
693ba7d49b1SJiri Pirko 	struct nlattr *data;
694ba7d49b1SJiri Pirko 	int err;
695ba7d49b1SJiri Pirko 
696ba7d49b1SJiri Pirko 	if (!ops)
697ba7d49b1SJiri Pirko 		return 0;
698ba7d49b1SJiri Pirko 	if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
699ba7d49b1SJiri Pirko 		return -EMSGSIZE;
700ba7d49b1SJiri Pirko 	if (ops->fill_xstats) {
701ba7d49b1SJiri Pirko 		err = ops->fill_xstats(skb, dev);
702ba7d49b1SJiri Pirko 		if (err < 0)
703ba7d49b1SJiri Pirko 			return err;
704ba7d49b1SJiri Pirko 	}
705ba7d49b1SJiri Pirko 	if (ops->fill_info) {
706ae0be8deSMichal Kubecek 		data = nla_nest_start_noflag(skb, IFLA_INFO_DATA);
707ba7d49b1SJiri Pirko 		if (data == NULL)
708ba7d49b1SJiri Pirko 			return -EMSGSIZE;
709ba7d49b1SJiri Pirko 		err = ops->fill_info(skb, dev);
710ba7d49b1SJiri Pirko 		if (err < 0)
711ba7d49b1SJiri Pirko 			goto err_cancel_data;
712ba7d49b1SJiri Pirko 		nla_nest_end(skb, data);
713ba7d49b1SJiri Pirko 	}
714ba7d49b1SJiri Pirko 	return 0;
715ba7d49b1SJiri Pirko 
716ba7d49b1SJiri Pirko err_cancel_data:
717ba7d49b1SJiri Pirko 	nla_nest_cancel(skb, data);
718ba7d49b1SJiri Pirko 	return err;
719ba7d49b1SJiri Pirko }
720ba7d49b1SJiri Pirko 
721ba7d49b1SJiri Pirko static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
722ba7d49b1SJiri Pirko {
723ba7d49b1SJiri Pirko 	struct nlattr *linkinfo;
72438f7b870SPatrick McHardy 	int err = -EMSGSIZE;
72538f7b870SPatrick McHardy 
726ae0be8deSMichal Kubecek 	linkinfo = nla_nest_start_noflag(skb, IFLA_LINKINFO);
72738f7b870SPatrick McHardy 	if (linkinfo == NULL)
72838f7b870SPatrick McHardy 		goto out;
72938f7b870SPatrick McHardy 
730ba7d49b1SJiri Pirko 	err = rtnl_link_info_fill(skb, dev);
73138f7b870SPatrick McHardy 	if (err < 0)
73238f7b870SPatrick McHardy 		goto err_cancel_link;
733ba7d49b1SJiri Pirko 
734ba7d49b1SJiri Pirko 	err = rtnl_link_slave_info_fill(skb, dev);
73538f7b870SPatrick McHardy 	if (err < 0)
736ba7d49b1SJiri Pirko 		goto err_cancel_link;
73738f7b870SPatrick McHardy 
73838f7b870SPatrick McHardy 	nla_nest_end(skb, linkinfo);
73938f7b870SPatrick McHardy 	return 0;
74038f7b870SPatrick McHardy 
74138f7b870SPatrick McHardy err_cancel_link:
74238f7b870SPatrick McHardy 	nla_nest_cancel(skb, linkinfo);
74338f7b870SPatrick McHardy out:
74438f7b870SPatrick McHardy 	return err;
74538f7b870SPatrick McHardy }
74638f7b870SPatrick McHardy 
74795c96174SEric Dumazet int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
7481da177e4SLinus Torvalds {
74997c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
7501da177e4SLinus Torvalds 
751cfdf0d9aSYajun Deng 	return nlmsg_notify(rtnl, skb, pid, group, echo, GFP_KERNEL);
7521da177e4SLinus Torvalds }
7531da177e4SLinus Torvalds 
75497c53cacSDenis V. Lunev int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
7552942e900SThomas Graf {
75697c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
75797c53cacSDenis V. Lunev 
7582942e900SThomas Graf 	return nlmsg_unicast(rtnl, skb, pid);
7592942e900SThomas Graf }
760e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unicast);
7612942e900SThomas Graf 
7621ce85fe4SPablo Neira Ayuso void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
76397676b6bSThomas Graf 		 struct nlmsghdr *nlh, gfp_t flags)
76497676b6bSThomas Graf {
76597c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
76697676b6bSThomas Graf 
767f9b282b3SYajun Deng 	nlmsg_notify(rtnl, skb, pid, group, nlmsg_report(nlh), flags);
76897676b6bSThomas Graf }
769e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_notify);
77097676b6bSThomas Graf 
77197c53cacSDenis V. Lunev void rtnl_set_sk_err(struct net *net, u32 group, int error)
77297676b6bSThomas Graf {
77397c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
77497c53cacSDenis V. Lunev 
77597676b6bSThomas Graf 	netlink_set_err(rtnl, 0, group, error);
77697676b6bSThomas Graf }
777e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_set_sk_err);
77897676b6bSThomas Graf 
7791da177e4SLinus Torvalds int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
7801da177e4SLinus Torvalds {
7812d7202bfSThomas Graf 	struct nlattr *mx;
7822d7202bfSThomas Graf 	int i, valid = 0;
7831da177e4SLinus Torvalds 
784c22a133aSDavid Ahern 	/* nothing is dumped for dst_default_metrics, so just skip the loop */
785c22a133aSDavid Ahern 	if (metrics == dst_default_metrics.metrics)
786c22a133aSDavid Ahern 		return 0;
787c22a133aSDavid Ahern 
788ae0be8deSMichal Kubecek 	mx = nla_nest_start_noflag(skb, RTA_METRICS);
7892d7202bfSThomas Graf 	if (mx == NULL)
7902d7202bfSThomas Graf 		return -ENOBUFS;
7912d7202bfSThomas Graf 
7921da177e4SLinus Torvalds 	for (i = 0; i < RTAX_MAX; i++) {
7932d7202bfSThomas Graf 		if (metrics[i]) {
794ea697639SDaniel Borkmann 			if (i == RTAX_CC_ALGO - 1) {
795ea697639SDaniel Borkmann 				char tmp[TCP_CA_NAME_MAX], *name;
796ea697639SDaniel Borkmann 
797ea697639SDaniel Borkmann 				name = tcp_ca_get_name_by_key(metrics[i], tmp);
798ea697639SDaniel Borkmann 				if (!name)
799ea697639SDaniel Borkmann 					continue;
800ea697639SDaniel Borkmann 				if (nla_put_string(skb, i + 1, name))
801ea697639SDaniel Borkmann 					goto nla_put_failure;
802c3a8d947SDaniel Borkmann 			} else if (i == RTAX_FEATURES - 1) {
803c3a8d947SDaniel Borkmann 				u32 user_features = metrics[i] & RTAX_FEATURE_MASK;
804c3a8d947SDaniel Borkmann 
805f8edcd12SPhil Sutter 				if (!user_features)
806f8edcd12SPhil Sutter 					continue;
807c3a8d947SDaniel Borkmann 				BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK);
808c3a8d947SDaniel Borkmann 				if (nla_put_u32(skb, i + 1, user_features))
809c3a8d947SDaniel Borkmann 					goto nla_put_failure;
810ea697639SDaniel Borkmann 			} else {
811a6574349SDavid S. Miller 				if (nla_put_u32(skb, i + 1, metrics[i]))
812a6574349SDavid S. Miller 					goto nla_put_failure;
8131da177e4SLinus Torvalds 			}
814ea697639SDaniel Borkmann 			valid++;
815ea697639SDaniel Borkmann 		}
8162d7202bfSThomas Graf 	}
8171da177e4SLinus Torvalds 
818a57d27fcSDavid S. Miller 	if (!valid) {
819a57d27fcSDavid S. Miller 		nla_nest_cancel(skb, mx);
820a57d27fcSDavid S. Miller 		return 0;
821a57d27fcSDavid S. Miller 	}
8222d7202bfSThomas Graf 
8232d7202bfSThomas Graf 	return nla_nest_end(skb, mx);
8242d7202bfSThomas Graf 
8252d7202bfSThomas Graf nla_put_failure:
826bc3ed28cSThomas Graf 	nla_nest_cancel(skb, mx);
827bc3ed28cSThomas Graf 	return -EMSGSIZE;
8281da177e4SLinus Torvalds }
829e0d087afSEric Dumazet EXPORT_SYMBOL(rtnetlink_put_metrics);
8301da177e4SLinus Torvalds 
831e3703b3dSThomas Graf int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
83287a50699SDavid S. Miller 		       long expires, u32 error)
833e3703b3dSThomas Graf {
834e3703b3dSThomas Graf 	struct rta_cacheinfo ci = {
835e3703b3dSThomas Graf 		.rta_error = error,
836e3703b3dSThomas Graf 		.rta_id =  id,
837e3703b3dSThomas Graf 	};
838e3703b3dSThomas Graf 
8393940746dSDavid Ahern 	if (dst) {
8403940746dSDavid Ahern 		ci.rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse);
8413940746dSDavid Ahern 		ci.rta_used = dst->__use;
8423940746dSDavid Ahern 		ci.rta_clntref = atomic_read(&dst->__refcnt);
8433940746dSDavid Ahern 	}
8448253947eSLi Wei 	if (expires) {
8458253947eSLi Wei 		unsigned long clock;
846e3703b3dSThomas Graf 
8478253947eSLi Wei 		clock = jiffies_to_clock_t(abs(expires));
8488253947eSLi Wei 		clock = min_t(unsigned long, clock, INT_MAX);
8498253947eSLi Wei 		ci.rta_expires = (expires > 0) ? clock : -clock;
8508253947eSLi Wei 	}
851e3703b3dSThomas Graf 	return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
852e3703b3dSThomas Graf }
853e3703b3dSThomas Graf EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
8541da177e4SLinus Torvalds 
85593b2d4a2SDavid S. Miller static void set_operstate(struct net_device *dev, unsigned char transition)
856b00055aaSStefan Rompf {
857b00055aaSStefan Rompf 	unsigned char operstate = dev->operstate;
858b00055aaSStefan Rompf 
859b00055aaSStefan Rompf 	switch (transition) {
860b00055aaSStefan Rompf 	case IF_OPER_UP:
861b00055aaSStefan Rompf 		if ((operstate == IF_OPER_DORMANT ||
862eec517cdSAndrew Lunn 		     operstate == IF_OPER_TESTING ||
863b00055aaSStefan Rompf 		     operstate == IF_OPER_UNKNOWN) &&
864eec517cdSAndrew Lunn 		    !netif_dormant(dev) && !netif_testing(dev))
865b00055aaSStefan Rompf 			operstate = IF_OPER_UP;
866b00055aaSStefan Rompf 		break;
867b00055aaSStefan Rompf 
868eec517cdSAndrew Lunn 	case IF_OPER_TESTING:
869eec517cdSAndrew Lunn 		if (operstate == IF_OPER_UP ||
870eec517cdSAndrew Lunn 		    operstate == IF_OPER_UNKNOWN)
871eec517cdSAndrew Lunn 			operstate = IF_OPER_TESTING;
872eec517cdSAndrew Lunn 		break;
873eec517cdSAndrew Lunn 
874b00055aaSStefan Rompf 	case IF_OPER_DORMANT:
875b00055aaSStefan Rompf 		if (operstate == IF_OPER_UP ||
876b00055aaSStefan Rompf 		    operstate == IF_OPER_UNKNOWN)
877b00055aaSStefan Rompf 			operstate = IF_OPER_DORMANT;
878b00055aaSStefan Rompf 		break;
8793ff50b79SStephen Hemminger 	}
880b00055aaSStefan Rompf 
881b00055aaSStefan Rompf 	if (dev->operstate != operstate) {
882fd888e85SSebastian Andrzej Siewior 		write_lock(&dev_base_lock);
883b00055aaSStefan Rompf 		dev->operstate = operstate;
884fd888e85SSebastian Andrzej Siewior 		write_unlock(&dev_base_lock);
885b00055aaSStefan Rompf 		netdev_state_change(dev);
88693b2d4a2SDavid S. Miller 	}
887b00055aaSStefan Rompf }
888b00055aaSStefan Rompf 
889b1beb681SJiri Benc static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
890b1beb681SJiri Benc {
891b1beb681SJiri Benc 	return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
892b1beb681SJiri Benc 	       (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
893b1beb681SJiri Benc }
894b1beb681SJiri Benc 
8953729d502SPatrick McHardy static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
8963729d502SPatrick McHardy 					   const struct ifinfomsg *ifm)
8973729d502SPatrick McHardy {
8983729d502SPatrick McHardy 	unsigned int flags = ifm->ifi_flags;
8993729d502SPatrick McHardy 
9003729d502SPatrick McHardy 	/* bugwards compatibility: ifi_change == 0 is treated as ~0 */
9013729d502SPatrick McHardy 	if (ifm->ifi_change)
9023729d502SPatrick McHardy 		flags = (flags & ifm->ifi_change) |
903b1beb681SJiri Benc 			(rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
9043729d502SPatrick McHardy 
9053729d502SPatrick McHardy 	return flags;
9063729d502SPatrick McHardy }
9073729d502SPatrick McHardy 
908b60c5115SThomas Graf static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
909be1f3c2cSBen Hutchings 				 const struct rtnl_link_stats64 *b)
9101da177e4SLinus Torvalds {
911b60c5115SThomas Graf 	a->rx_packets = b->rx_packets;
912b60c5115SThomas Graf 	a->tx_packets = b->tx_packets;
913b60c5115SThomas Graf 	a->rx_bytes = b->rx_bytes;
914b60c5115SThomas Graf 	a->tx_bytes = b->tx_bytes;
915b60c5115SThomas Graf 	a->rx_errors = b->rx_errors;
916b60c5115SThomas Graf 	a->tx_errors = b->tx_errors;
917b60c5115SThomas Graf 	a->rx_dropped = b->rx_dropped;
918b60c5115SThomas Graf 	a->tx_dropped = b->tx_dropped;
919b60c5115SThomas Graf 
920b60c5115SThomas Graf 	a->multicast = b->multicast;
921b60c5115SThomas Graf 	a->collisions = b->collisions;
922b60c5115SThomas Graf 
923b60c5115SThomas Graf 	a->rx_length_errors = b->rx_length_errors;
924b60c5115SThomas Graf 	a->rx_over_errors = b->rx_over_errors;
925b60c5115SThomas Graf 	a->rx_crc_errors = b->rx_crc_errors;
926b60c5115SThomas Graf 	a->rx_frame_errors = b->rx_frame_errors;
927b60c5115SThomas Graf 	a->rx_fifo_errors = b->rx_fifo_errors;
928b60c5115SThomas Graf 	a->rx_missed_errors = b->rx_missed_errors;
929b60c5115SThomas Graf 
930b60c5115SThomas Graf 	a->tx_aborted_errors = b->tx_aborted_errors;
931b60c5115SThomas Graf 	a->tx_carrier_errors = b->tx_carrier_errors;
932b60c5115SThomas Graf 	a->tx_fifo_errors = b->tx_fifo_errors;
933b60c5115SThomas Graf 	a->tx_heartbeat_errors = b->tx_heartbeat_errors;
934b60c5115SThomas Graf 	a->tx_window_errors = b->tx_window_errors;
935b60c5115SThomas Graf 
936b60c5115SThomas Graf 	a->rx_compressed = b->rx_compressed;
937b60c5115SThomas Graf 	a->tx_compressed = b->tx_compressed;
9386e7333d3SJarod Wilson 
9396e7333d3SJarod Wilson 	a->rx_nohandler = b->rx_nohandler;
94010708f37SJan Engelhardt }
94110708f37SJan Engelhardt 
942c02db8c6SChris Wright /* All VF info */
943115c9b81SGreg Rose static inline int rtnl_vfinfo_size(const struct net_device *dev,
944115c9b81SGreg Rose 				   u32 ext_filter_mask)
945ebc08a6fSWilliams, Mitch A {
9469af15c38SPhil Sutter 	if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) {
947c02db8c6SChris Wright 		int num_vfs = dev_num_vf(dev->dev.parent);
9487e75f74aSSabrina Dubroca 		size_t size = nla_total_size(0);
949045de01aSScott Feldman 		size += num_vfs *
9507e75f74aSSabrina Dubroca 			(nla_total_size(0) +
9517e75f74aSSabrina Dubroca 			 nla_total_size(sizeof(struct ifla_vf_mac)) +
95275345f88SDenis Kirjanov 			 nla_total_size(sizeof(struct ifla_vf_broadcast)) +
9537e75f74aSSabrina Dubroca 			 nla_total_size(sizeof(struct ifla_vf_vlan)) +
9547e75f74aSSabrina Dubroca 			 nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
95579aab093SMoshe Shemesh 			 nla_total_size(MAX_VLAN_LIST_LEN *
95679aab093SMoshe Shemesh 					sizeof(struct ifla_vf_vlan_info)) +
957ed616689SSucheta Chakraborty 			 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
9587e75f74aSSabrina Dubroca 			 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
959945a3676SJiri Benc 			 nla_total_size(sizeof(struct ifla_vf_rate)) +
96001a3d796SVlad Zolotarov 			 nla_total_size(sizeof(struct ifla_vf_link_state)) +
9613b766cd8SEran Ben Elisha 			 nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
9627e75f74aSSabrina Dubroca 			 nla_total_size(0) + /* nest IFLA_VF_STATS */
9633b766cd8SEran Ben Elisha 			 /* IFLA_VF_STATS_RX_PACKETS */
964343a6d8eSNicolas Dichtel 			 nla_total_size_64bit(sizeof(__u64)) +
9653b766cd8SEran Ben Elisha 			 /* IFLA_VF_STATS_TX_PACKETS */
966343a6d8eSNicolas Dichtel 			 nla_total_size_64bit(sizeof(__u64)) +
9673b766cd8SEran Ben Elisha 			 /* IFLA_VF_STATS_RX_BYTES */
968343a6d8eSNicolas Dichtel 			 nla_total_size_64bit(sizeof(__u64)) +
9693b766cd8SEran Ben Elisha 			 /* IFLA_VF_STATS_TX_BYTES */
970343a6d8eSNicolas Dichtel 			 nla_total_size_64bit(sizeof(__u64)) +
9713b766cd8SEran Ben Elisha 			 /* IFLA_VF_STATS_BROADCAST */
972343a6d8eSNicolas Dichtel 			 nla_total_size_64bit(sizeof(__u64)) +
9733b766cd8SEran Ben Elisha 			 /* IFLA_VF_STATS_MULTICAST */
974343a6d8eSNicolas Dichtel 			 nla_total_size_64bit(sizeof(__u64)) +
975c5a9f6f0SEugenia Emantayev 			 /* IFLA_VF_STATS_RX_DROPPED */
976c5a9f6f0SEugenia Emantayev 			 nla_total_size_64bit(sizeof(__u64)) +
977c5a9f6f0SEugenia Emantayev 			 /* IFLA_VF_STATS_TX_DROPPED */
978c5a9f6f0SEugenia Emantayev 			 nla_total_size_64bit(sizeof(__u64)) +
979dd461d6aSHiroshi Shimamoto 			 nla_total_size(sizeof(struct ifla_vf_trust)));
980c02db8c6SChris Wright 		return size;
981c02db8c6SChris Wright 	} else
982ebc08a6fSWilliams, Mitch A 		return 0;
983ebc08a6fSWilliams, Mitch A }
984ebc08a6fSWilliams, Mitch A 
985c53864fdSDavid Gibson static size_t rtnl_port_size(const struct net_device *dev,
986c53864fdSDavid Gibson 			     u32 ext_filter_mask)
98757b61080SScott Feldman {
98857b61080SScott Feldman 	size_t port_size = nla_total_size(4)		/* PORT_VF */
98957b61080SScott Feldman 		+ nla_total_size(PORT_PROFILE_MAX)	/* PORT_PROFILE */
99057b61080SScott Feldman 		+ nla_total_size(PORT_UUID_MAX)		/* PORT_INSTANCE_UUID */
99157b61080SScott Feldman 		+ nla_total_size(PORT_UUID_MAX)		/* PORT_HOST_UUID */
99257b61080SScott Feldman 		+ nla_total_size(1)			/* PROT_VDP_REQUEST */
99357b61080SScott Feldman 		+ nla_total_size(2);			/* PORT_VDP_RESPONSE */
99457b61080SScott Feldman 	size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
99557b61080SScott Feldman 	size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
99657b61080SScott Feldman 		+ port_size;
99757b61080SScott Feldman 	size_t port_self_size = nla_total_size(sizeof(struct nlattr))
99857b61080SScott Feldman 		+ port_size;
99957b61080SScott Feldman 
1000c53864fdSDavid Gibson 	if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
1001c53864fdSDavid Gibson 	    !(ext_filter_mask & RTEXT_FILTER_VF))
100257b61080SScott Feldman 		return 0;
100357b61080SScott Feldman 	if (dev_num_vf(dev->dev.parent))
100457b61080SScott Feldman 		return port_self_size + vf_ports_size +
100557b61080SScott Feldman 			vf_port_size * dev_num_vf(dev->dev.parent);
100657b61080SScott Feldman 	else
100757b61080SScott Feldman 		return port_self_size;
100857b61080SScott Feldman }
100957b61080SScott Feldman 
1010b5cdae32SDavid S. Miller static size_t rtnl_xdp_size(void)
1011d1fdd913SBrenden Blanco {
1012b3cfaa31SSabrina Dubroca 	size_t xdp_size = nla_total_size(0) +	/* nest IFLA_XDP */
101358038695SMartin KaFai Lau 			  nla_total_size(1) +	/* XDP_ATTACHED */
1014a25717d2SJakub Kicinski 			  nla_total_size(4) +	/* XDP_PROG_ID (or 1st mode) */
10154f91da26SJakub Kicinski 			  nla_total_size(4);	/* XDP_<mode>_PROG_ID */
1016d1fdd913SBrenden Blanco 
1017d1fdd913SBrenden Blanco 	return xdp_size;
1018d1fdd913SBrenden Blanco }
1019d1fdd913SBrenden Blanco 
102088f4fb0cSJiri Pirko static size_t rtnl_prop_list_size(const struct net_device *dev)
102188f4fb0cSJiri Pirko {
102288f4fb0cSJiri Pirko 	struct netdev_name_node *name_node;
102388f4fb0cSJiri Pirko 	size_t size;
102488f4fb0cSJiri Pirko 
102588f4fb0cSJiri Pirko 	if (list_empty(&dev->name_node->list))
102688f4fb0cSJiri Pirko 		return 0;
102788f4fb0cSJiri Pirko 	size = nla_total_size(0);
102888f4fb0cSJiri Pirko 	list_for_each_entry(name_node, &dev->name_node->list, list)
102988f4fb0cSJiri Pirko 		size += nla_total_size(ALTIFNAMSIZ);
103088f4fb0cSJiri Pirko 	return size;
103188f4fb0cSJiri Pirko }
103288f4fb0cSJiri Pirko 
1033829eb208SRoopa Prabhu static size_t rtnl_proto_down_size(const struct net_device *dev)
1034829eb208SRoopa Prabhu {
1035829eb208SRoopa Prabhu 	size_t size = nla_total_size(1);
1036829eb208SRoopa Prabhu 
1037829eb208SRoopa Prabhu 	if (dev->proto_down_reason)
1038829eb208SRoopa Prabhu 		size += nla_total_size(0) + nla_total_size(4);
1039829eb208SRoopa Prabhu 
1040829eb208SRoopa Prabhu 	return size;
1041829eb208SRoopa Prabhu }
1042829eb208SRoopa Prabhu 
1043115c9b81SGreg Rose static noinline size_t if_nlmsg_size(const struct net_device *dev,
1044115c9b81SGreg Rose 				     u32 ext_filter_mask)
1045339bf98fSThomas Graf {
1046339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
1047339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
10480b815a1aSStephen Hemminger 	       + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
1049339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
1050270cb4d0SNicolas Dichtel 	       + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap))
1051339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rtnl_link_stats))
105235c58459SDavid S. Miller 	       + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
1053339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
1054339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
1055339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_TXQLEN */
1056339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_WEIGHT */
1057339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MTU */
1058339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_LINK */
1059339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MASTER */
10609a57247fSJiri Pirko 	       + nla_total_size(1) /* IFLA_CARRIER */
1061edbc0bb3SBen Greear 	       + nla_total_size(4) /* IFLA_PROMISCUITY */
106276ff5cc9SJiri Pirko 	       + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
106376ff5cc9SJiri Pirko 	       + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
10646919756cSTobias Klauser 	       + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
10656919756cSTobias Klauser 	       + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
1066eac1b93cSCoco Li 	       + nla_total_size(4) /* IFLA_GRO_MAX_SIZE */
1067339bf98fSThomas Graf 	       + nla_total_size(1) /* IFLA_OPERSTATE */
106838f7b870SPatrick McHardy 	       + nla_total_size(1) /* IFLA_LINKMODE */
10692d3b479dSdavid decotigny 	       + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
1070d37512a2SNicolas Dichtel 	       + nla_total_size(4) /* IFLA_LINK_NETNSID */
1071db833d40SSerhey Popovych 	       + nla_total_size(4) /* IFLA_GROUP */
1072115c9b81SGreg Rose 	       + nla_total_size(ext_filter_mask
1073115c9b81SGreg Rose 			        & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
1074115c9b81SGreg Rose 	       + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
1075c53864fdSDavid Gibson 	       + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
1076f8ff182cSThomas Graf 	       + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
1077b1974ed0SArad, Ronen 	       + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
107882f28412SJiri Pirko 	       + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
107988d6378bSAnuradha Karuppiah 	       + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
1080c57c7a95SNicolas Dichtel 	       + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
1081b5cdae32SDavid S. Miller 	       + rtnl_xdp_size() /* IFLA_XDP */
10823d3ea5afSVlad Yasevich 	       + nla_total_size(4)  /* IFLA_EVENT */
10836621dd29SNicolas Dichtel 	       + nla_total_size(4)  /* IFLA_NEW_NETNSID */
108438e01b30SNicolas Dichtel 	       + nla_total_size(4)  /* IFLA_NEW_IFINDEX */
1085829eb208SRoopa Prabhu 	       + rtnl_proto_down_size(dev)  /* proto down */
10867e4a8d5aSChristian Brauner 	       + nla_total_size(4)  /* IFLA_TARGET_NETNSID */
1087b2d3bcfaSDavid Decotigny 	       + nla_total_size(4)  /* IFLA_CARRIER_UP_COUNT */
1088b2d3bcfaSDavid Decotigny 	       + nla_total_size(4)  /* IFLA_CARRIER_DOWN_COUNT */
10893e7a50ceSStephen Hemminger 	       + nla_total_size(4)  /* IFLA_MIN_MTU */
10903e7a50ceSStephen Hemminger 	       + nla_total_size(4)  /* IFLA_MAX_MTU */
109188f4fb0cSJiri Pirko 	       + rtnl_prop_list_size(dev)
1092f74877a5SMichal Kubecek 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_PERM_ADDRESS */
109379e1ad14SJiri Benc 	       + 0;
1094339bf98fSThomas Graf }
1095339bf98fSThomas Graf 
109657b61080SScott Feldman static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
109757b61080SScott Feldman {
109857b61080SScott Feldman 	struct nlattr *vf_ports;
109957b61080SScott Feldman 	struct nlattr *vf_port;
110057b61080SScott Feldman 	int vf;
110157b61080SScott Feldman 	int err;
110257b61080SScott Feldman 
1103ae0be8deSMichal Kubecek 	vf_ports = nla_nest_start_noflag(skb, IFLA_VF_PORTS);
110457b61080SScott Feldman 	if (!vf_ports)
110557b61080SScott Feldman 		return -EMSGSIZE;
110657b61080SScott Feldman 
110757b61080SScott Feldman 	for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
1108ae0be8deSMichal Kubecek 		vf_port = nla_nest_start_noflag(skb, IFLA_VF_PORT);
11098ca94183SScott Feldman 		if (!vf_port)
11108ca94183SScott Feldman 			goto nla_put_failure;
1111a6574349SDavid S. Miller 		if (nla_put_u32(skb, IFLA_PORT_VF, vf))
1112a6574349SDavid S. Miller 			goto nla_put_failure;
111357b61080SScott Feldman 		err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
11148ca94183SScott Feldman 		if (err == -EMSGSIZE)
11158ca94183SScott Feldman 			goto nla_put_failure;
111657b61080SScott Feldman 		if (err) {
111757b61080SScott Feldman 			nla_nest_cancel(skb, vf_port);
111857b61080SScott Feldman 			continue;
111957b61080SScott Feldman 		}
112057b61080SScott Feldman 		nla_nest_end(skb, vf_port);
112157b61080SScott Feldman 	}
112257b61080SScott Feldman 
112357b61080SScott Feldman 	nla_nest_end(skb, vf_ports);
112457b61080SScott Feldman 
112557b61080SScott Feldman 	return 0;
11268ca94183SScott Feldman 
11278ca94183SScott Feldman nla_put_failure:
11288ca94183SScott Feldman 	nla_nest_cancel(skb, vf_ports);
11298ca94183SScott Feldman 	return -EMSGSIZE;
113057b61080SScott Feldman }
113157b61080SScott Feldman 
113257b61080SScott Feldman static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
113357b61080SScott Feldman {
113457b61080SScott Feldman 	struct nlattr *port_self;
113557b61080SScott Feldman 	int err;
113657b61080SScott Feldman 
1137ae0be8deSMichal Kubecek 	port_self = nla_nest_start_noflag(skb, IFLA_PORT_SELF);
113857b61080SScott Feldman 	if (!port_self)
113957b61080SScott Feldman 		return -EMSGSIZE;
114057b61080SScott Feldman 
114157b61080SScott Feldman 	err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
114257b61080SScott Feldman 	if (err) {
114357b61080SScott Feldman 		nla_nest_cancel(skb, port_self);
11448ca94183SScott Feldman 		return (err == -EMSGSIZE) ? err : 0;
114557b61080SScott Feldman 	}
114657b61080SScott Feldman 
114757b61080SScott Feldman 	nla_nest_end(skb, port_self);
114857b61080SScott Feldman 
114957b61080SScott Feldman 	return 0;
115057b61080SScott Feldman }
115157b61080SScott Feldman 
1152c53864fdSDavid Gibson static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
1153c53864fdSDavid Gibson 			  u32 ext_filter_mask)
115457b61080SScott Feldman {
115557b61080SScott Feldman 	int err;
115657b61080SScott Feldman 
1157c53864fdSDavid Gibson 	if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
1158c53864fdSDavid Gibson 	    !(ext_filter_mask & RTEXT_FILTER_VF))
115957b61080SScott Feldman 		return 0;
116057b61080SScott Feldman 
116157b61080SScott Feldman 	err = rtnl_port_self_fill(skb, dev);
116257b61080SScott Feldman 	if (err)
116357b61080SScott Feldman 		return err;
116457b61080SScott Feldman 
116557b61080SScott Feldman 	if (dev_num_vf(dev->dev.parent)) {
116657b61080SScott Feldman 		err = rtnl_vf_ports_fill(skb, dev);
116757b61080SScott Feldman 		if (err)
116857b61080SScott Feldman 			return err;
116957b61080SScott Feldman 	}
117057b61080SScott Feldman 
117157b61080SScott Feldman 	return 0;
117257b61080SScott Feldman }
117357b61080SScott Feldman 
117466cae9edSJiri Pirko static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
117566cae9edSJiri Pirko {
117666cae9edSJiri Pirko 	int err;
117702637fceSJiri Pirko 	struct netdev_phys_item_id ppid;
117866cae9edSJiri Pirko 
117966cae9edSJiri Pirko 	err = dev_get_phys_port_id(dev, &ppid);
118066cae9edSJiri Pirko 	if (err) {
118166cae9edSJiri Pirko 		if (err == -EOPNOTSUPP)
118266cae9edSJiri Pirko 			return 0;
118366cae9edSJiri Pirko 		return err;
118466cae9edSJiri Pirko 	}
118566cae9edSJiri Pirko 
118666cae9edSJiri Pirko 	if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
118766cae9edSJiri Pirko 		return -EMSGSIZE;
118866cae9edSJiri Pirko 
118966cae9edSJiri Pirko 	return 0;
119066cae9edSJiri Pirko }
119166cae9edSJiri Pirko 
1192db24a904SDavid Ahern static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1193db24a904SDavid Ahern {
1194db24a904SDavid Ahern 	char name[IFNAMSIZ];
1195db24a904SDavid Ahern 	int err;
1196db24a904SDavid Ahern 
1197db24a904SDavid Ahern 	err = dev_get_phys_port_name(dev, name, sizeof(name));
1198db24a904SDavid Ahern 	if (err) {
1199db24a904SDavid Ahern 		if (err == -EOPNOTSUPP)
1200db24a904SDavid Ahern 			return 0;
1201db24a904SDavid Ahern 		return err;
1202db24a904SDavid Ahern 	}
1203db24a904SDavid Ahern 
120477ef033bSMichal Schmidt 	if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
1205db24a904SDavid Ahern 		return -EMSGSIZE;
1206db24a904SDavid Ahern 
1207db24a904SDavid Ahern 	return 0;
1208db24a904SDavid Ahern }
1209db24a904SDavid Ahern 
121082f28412SJiri Pirko static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
121182f28412SJiri Pirko {
1212bccb3025SFlorian Fainelli 	struct netdev_phys_item_id ppid = { };
121382f28412SJiri Pirko 	int err;
121482f28412SJiri Pirko 
1215bccb3025SFlorian Fainelli 	err = dev_get_port_parent_id(dev, &ppid, false);
121682f28412SJiri Pirko 	if (err) {
121782f28412SJiri Pirko 		if (err == -EOPNOTSUPP)
121882f28412SJiri Pirko 			return 0;
121982f28412SJiri Pirko 		return err;
122082f28412SJiri Pirko 	}
122182f28412SJiri Pirko 
1222bccb3025SFlorian Fainelli 	if (nla_put(skb, IFLA_PHYS_SWITCH_ID, ppid.id_len, ppid.id))
122382f28412SJiri Pirko 		return -EMSGSIZE;
122482f28412SJiri Pirko 
122582f28412SJiri Pirko 	return 0;
122682f28412SJiri Pirko }
122782f28412SJiri Pirko 
1228b22b941bSHannes Frederic Sowa static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1229b22b941bSHannes Frederic Sowa 					      struct net_device *dev)
1230b22b941bSHannes Frederic Sowa {
1231550bce59SRoopa Prabhu 	struct rtnl_link_stats64 *sp;
1232b22b941bSHannes Frederic Sowa 	struct nlattr *attr;
1233b22b941bSHannes Frederic Sowa 
123458414d32SNicolas Dichtel 	attr = nla_reserve_64bit(skb, IFLA_STATS64,
123558414d32SNicolas Dichtel 				 sizeof(struct rtnl_link_stats64), IFLA_PAD);
1236b22b941bSHannes Frederic Sowa 	if (!attr)
1237b22b941bSHannes Frederic Sowa 		return -EMSGSIZE;
1238b22b941bSHannes Frederic Sowa 
1239550bce59SRoopa Prabhu 	sp = nla_data(attr);
1240550bce59SRoopa Prabhu 	dev_get_stats(dev, sp);
1241550bce59SRoopa Prabhu 
1242550bce59SRoopa Prabhu 	attr = nla_reserve(skb, IFLA_STATS,
1243550bce59SRoopa Prabhu 			   sizeof(struct rtnl_link_stats));
1244550bce59SRoopa Prabhu 	if (!attr)
1245550bce59SRoopa Prabhu 		return -EMSGSIZE;
1246550bce59SRoopa Prabhu 
1247550bce59SRoopa Prabhu 	copy_rtnl_link_stats(nla_data(attr), sp);
1248b22b941bSHannes Frederic Sowa 
1249b22b941bSHannes Frederic Sowa 	return 0;
1250b22b941bSHannes Frederic Sowa }
1251b22b941bSHannes Frederic Sowa 
1252b22b941bSHannes Frederic Sowa static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1253b22b941bSHannes Frederic Sowa 					       struct net_device *dev,
1254b22b941bSHannes Frederic Sowa 					       int vfs_num,
1255b22b941bSHannes Frederic Sowa 					       struct nlattr *vfinfo)
1256b22b941bSHannes Frederic Sowa {
1257b22b941bSHannes Frederic Sowa 	struct ifla_vf_rss_query_en vf_rss_query_en;
125879aab093SMoshe Shemesh 	struct nlattr *vf, *vfstats, *vfvlanlist;
1259b22b941bSHannes Frederic Sowa 	struct ifla_vf_link_state vf_linkstate;
126079aab093SMoshe Shemesh 	struct ifla_vf_vlan_info vf_vlan_info;
1261b22b941bSHannes Frederic Sowa 	struct ifla_vf_spoofchk vf_spoofchk;
1262b22b941bSHannes Frederic Sowa 	struct ifla_vf_tx_rate vf_tx_rate;
1263b22b941bSHannes Frederic Sowa 	struct ifla_vf_stats vf_stats;
1264b22b941bSHannes Frederic Sowa 	struct ifla_vf_trust vf_trust;
1265b22b941bSHannes Frederic Sowa 	struct ifla_vf_vlan vf_vlan;
1266b22b941bSHannes Frederic Sowa 	struct ifla_vf_rate vf_rate;
1267b22b941bSHannes Frederic Sowa 	struct ifla_vf_mac vf_mac;
126875345f88SDenis Kirjanov 	struct ifla_vf_broadcast vf_broadcast;
1269b22b941bSHannes Frederic Sowa 	struct ifla_vf_info ivi;
127030aad417SDanit Goldberg 	struct ifla_vf_guid node_guid;
127130aad417SDanit Goldberg 	struct ifla_vf_guid port_guid;
1272b22b941bSHannes Frederic Sowa 
12730eed9cf5SMintz, Yuval 	memset(&ivi, 0, sizeof(ivi));
12740eed9cf5SMintz, Yuval 
1275b22b941bSHannes Frederic Sowa 	/* Not all SR-IOV capable drivers support the
1276b22b941bSHannes Frederic Sowa 	 * spoofcheck and "RSS query enable" query.  Preset to
1277b22b941bSHannes Frederic Sowa 	 * -1 so the user space tool can detect that the driver
1278b22b941bSHannes Frederic Sowa 	 * didn't report anything.
1279b22b941bSHannes Frederic Sowa 	 */
1280b22b941bSHannes Frederic Sowa 	ivi.spoofchk = -1;
1281b22b941bSHannes Frederic Sowa 	ivi.rss_query_en = -1;
1282b22b941bSHannes Frederic Sowa 	ivi.trusted = -1;
1283b22b941bSHannes Frederic Sowa 	/* The default value for VF link state is "auto"
1284b22b941bSHannes Frederic Sowa 	 * IFLA_VF_LINK_STATE_AUTO which equals zero
1285b22b941bSHannes Frederic Sowa 	 */
1286b22b941bSHannes Frederic Sowa 	ivi.linkstate = 0;
128779aab093SMoshe Shemesh 	/* VLAN Protocol by default is 802.1Q */
128879aab093SMoshe Shemesh 	ivi.vlan_proto = htons(ETH_P_8021Q);
1289b22b941bSHannes Frederic Sowa 	if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1290b22b941bSHannes Frederic Sowa 		return 0;
1291b22b941bSHannes Frederic Sowa 
1292775f4f05SDan Carpenter 	memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
12939fbf082fSLeon Romanovsky 	memset(&node_guid, 0, sizeof(node_guid));
12949fbf082fSLeon Romanovsky 	memset(&port_guid, 0, sizeof(port_guid));
1295775f4f05SDan Carpenter 
1296b22b941bSHannes Frederic Sowa 	vf_mac.vf =
1297b22b941bSHannes Frederic Sowa 		vf_vlan.vf =
129879aab093SMoshe Shemesh 		vf_vlan_info.vf =
1299b22b941bSHannes Frederic Sowa 		vf_rate.vf =
1300b22b941bSHannes Frederic Sowa 		vf_tx_rate.vf =
1301b22b941bSHannes Frederic Sowa 		vf_spoofchk.vf =
1302b22b941bSHannes Frederic Sowa 		vf_linkstate.vf =
1303b22b941bSHannes Frederic Sowa 		vf_rss_query_en.vf =
13049aed6ae0SDanit Goldberg 		vf_trust.vf =
13059aed6ae0SDanit Goldberg 		node_guid.vf =
13069aed6ae0SDanit Goldberg 		port_guid.vf = ivi.vf;
1307b22b941bSHannes Frederic Sowa 
1308b22b941bSHannes Frederic Sowa 	memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
130975345f88SDenis Kirjanov 	memcpy(vf_broadcast.broadcast, dev->broadcast, dev->addr_len);
1310b22b941bSHannes Frederic Sowa 	vf_vlan.vlan = ivi.vlan;
1311b22b941bSHannes Frederic Sowa 	vf_vlan.qos = ivi.qos;
131279aab093SMoshe Shemesh 	vf_vlan_info.vlan = ivi.vlan;
131379aab093SMoshe Shemesh 	vf_vlan_info.qos = ivi.qos;
131479aab093SMoshe Shemesh 	vf_vlan_info.vlan_proto = ivi.vlan_proto;
1315b22b941bSHannes Frederic Sowa 	vf_tx_rate.rate = ivi.max_tx_rate;
1316b22b941bSHannes Frederic Sowa 	vf_rate.min_tx_rate = ivi.min_tx_rate;
1317b22b941bSHannes Frederic Sowa 	vf_rate.max_tx_rate = ivi.max_tx_rate;
1318b22b941bSHannes Frederic Sowa 	vf_spoofchk.setting = ivi.spoofchk;
1319b22b941bSHannes Frederic Sowa 	vf_linkstate.link_state = ivi.linkstate;
1320b22b941bSHannes Frederic Sowa 	vf_rss_query_en.setting = ivi.rss_query_en;
1321b22b941bSHannes Frederic Sowa 	vf_trust.setting = ivi.trusted;
1322ae0be8deSMichal Kubecek 	vf = nla_nest_start_noflag(skb, IFLA_VF_INFO);
132379aab093SMoshe Shemesh 	if (!vf)
132479aab093SMoshe Shemesh 		goto nla_put_vfinfo_failure;
1325b22b941bSHannes Frederic Sowa 	if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
132675345f88SDenis Kirjanov 	    nla_put(skb, IFLA_VF_BROADCAST, sizeof(vf_broadcast), &vf_broadcast) ||
1327b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1328b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1329b22b941bSHannes Frederic Sowa 		    &vf_rate) ||
1330b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1331b22b941bSHannes Frederic Sowa 		    &vf_tx_rate) ||
1332b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1333b22b941bSHannes Frederic Sowa 		    &vf_spoofchk) ||
1334b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1335b22b941bSHannes Frederic Sowa 		    &vf_linkstate) ||
1336b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1337b22b941bSHannes Frederic Sowa 		    sizeof(vf_rss_query_en),
1338b22b941bSHannes Frederic Sowa 		    &vf_rss_query_en) ||
1339b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_TRUST,
1340b22b941bSHannes Frederic Sowa 		    sizeof(vf_trust), &vf_trust))
134179aab093SMoshe Shemesh 		goto nla_put_vf_failure;
134230aad417SDanit Goldberg 
134330aad417SDanit Goldberg 	if (dev->netdev_ops->ndo_get_vf_guid &&
134430aad417SDanit Goldberg 	    !dev->netdev_ops->ndo_get_vf_guid(dev, vfs_num, &node_guid,
134530aad417SDanit Goldberg 					      &port_guid)) {
134630aad417SDanit Goldberg 		if (nla_put(skb, IFLA_VF_IB_NODE_GUID, sizeof(node_guid),
134730aad417SDanit Goldberg 			    &node_guid) ||
134830aad417SDanit Goldberg 		    nla_put(skb, IFLA_VF_IB_PORT_GUID, sizeof(port_guid),
134930aad417SDanit Goldberg 			    &port_guid))
135030aad417SDanit Goldberg 			goto nla_put_vf_failure;
135130aad417SDanit Goldberg 	}
1352ae0be8deSMichal Kubecek 	vfvlanlist = nla_nest_start_noflag(skb, IFLA_VF_VLAN_LIST);
135379aab093SMoshe Shemesh 	if (!vfvlanlist)
135479aab093SMoshe Shemesh 		goto nla_put_vf_failure;
135579aab093SMoshe Shemesh 	if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
135679aab093SMoshe Shemesh 		    &vf_vlan_info)) {
135779aab093SMoshe Shemesh 		nla_nest_cancel(skb, vfvlanlist);
135879aab093SMoshe Shemesh 		goto nla_put_vf_failure;
135979aab093SMoshe Shemesh 	}
136079aab093SMoshe Shemesh 	nla_nest_end(skb, vfvlanlist);
1361b22b941bSHannes Frederic Sowa 	memset(&vf_stats, 0, sizeof(vf_stats));
1362b22b941bSHannes Frederic Sowa 	if (dev->netdev_ops->ndo_get_vf_stats)
1363b22b941bSHannes Frederic Sowa 		dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1364b22b941bSHannes Frederic Sowa 						&vf_stats);
1365ae0be8deSMichal Kubecek 	vfstats = nla_nest_start_noflag(skb, IFLA_VF_STATS);
136679aab093SMoshe Shemesh 	if (!vfstats)
136779aab093SMoshe Shemesh 		goto nla_put_vf_failure;
1368343a6d8eSNicolas Dichtel 	if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1369343a6d8eSNicolas Dichtel 			      vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1370343a6d8eSNicolas Dichtel 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1371343a6d8eSNicolas Dichtel 			      vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1372343a6d8eSNicolas Dichtel 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1373343a6d8eSNicolas Dichtel 			      vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1374343a6d8eSNicolas Dichtel 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1375343a6d8eSNicolas Dichtel 			      vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1376343a6d8eSNicolas Dichtel 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1377343a6d8eSNicolas Dichtel 			      vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1378343a6d8eSNicolas Dichtel 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
1379c5a9f6f0SEugenia Emantayev 			      vf_stats.multicast, IFLA_VF_STATS_PAD) ||
1380c5a9f6f0SEugenia Emantayev 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED,
1381c5a9f6f0SEugenia Emantayev 			      vf_stats.rx_dropped, IFLA_VF_STATS_PAD) ||
1382c5a9f6f0SEugenia Emantayev 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED,
1383c5a9f6f0SEugenia Emantayev 			      vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) {
138479aab093SMoshe Shemesh 		nla_nest_cancel(skb, vfstats);
138579aab093SMoshe Shemesh 		goto nla_put_vf_failure;
138679aab093SMoshe Shemesh 	}
1387b22b941bSHannes Frederic Sowa 	nla_nest_end(skb, vfstats);
1388b22b941bSHannes Frederic Sowa 	nla_nest_end(skb, vf);
1389b22b941bSHannes Frederic Sowa 	return 0;
139079aab093SMoshe Shemesh 
139179aab093SMoshe Shemesh nla_put_vf_failure:
139279aab093SMoshe Shemesh 	nla_nest_cancel(skb, vf);
139379aab093SMoshe Shemesh nla_put_vfinfo_failure:
139479aab093SMoshe Shemesh 	nla_nest_cancel(skb, vfinfo);
139579aab093SMoshe Shemesh 	return -EMSGSIZE;
1396b22b941bSHannes Frederic Sowa }
1397b22b941bSHannes Frederic Sowa 
1398250fc3dfSFlorian Westphal static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb,
1399250fc3dfSFlorian Westphal 					   struct net_device *dev,
1400250fc3dfSFlorian Westphal 					   u32 ext_filter_mask)
1401250fc3dfSFlorian Westphal {
1402250fc3dfSFlorian Westphal 	struct nlattr *vfinfo;
1403250fc3dfSFlorian Westphal 	int i, num_vfs;
1404250fc3dfSFlorian Westphal 
1405250fc3dfSFlorian Westphal 	if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0))
1406250fc3dfSFlorian Westphal 		return 0;
1407250fc3dfSFlorian Westphal 
1408250fc3dfSFlorian Westphal 	num_vfs = dev_num_vf(dev->dev.parent);
1409250fc3dfSFlorian Westphal 	if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs))
1410250fc3dfSFlorian Westphal 		return -EMSGSIZE;
1411250fc3dfSFlorian Westphal 
1412250fc3dfSFlorian Westphal 	if (!dev->netdev_ops->ndo_get_vf_config)
1413250fc3dfSFlorian Westphal 		return 0;
1414250fc3dfSFlorian Westphal 
1415ae0be8deSMichal Kubecek 	vfinfo = nla_nest_start_noflag(skb, IFLA_VFINFO_LIST);
1416250fc3dfSFlorian Westphal 	if (!vfinfo)
1417250fc3dfSFlorian Westphal 		return -EMSGSIZE;
1418250fc3dfSFlorian Westphal 
1419250fc3dfSFlorian Westphal 	for (i = 0; i < num_vfs; i++) {
1420250fc3dfSFlorian Westphal 		if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
1421250fc3dfSFlorian Westphal 			return -EMSGSIZE;
1422250fc3dfSFlorian Westphal 	}
1423250fc3dfSFlorian Westphal 
1424250fc3dfSFlorian Westphal 	nla_nest_end(skb, vfinfo);
1425250fc3dfSFlorian Westphal 	return 0;
1426250fc3dfSFlorian Westphal }
1427250fc3dfSFlorian Westphal 
1428b22b941bSHannes Frederic Sowa static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1429b22b941bSHannes Frederic Sowa {
14305f8e4474SKangjie Lu 	struct rtnl_link_ifmap map;
14315f8e4474SKangjie Lu 
14325f8e4474SKangjie Lu 	memset(&map, 0, sizeof(map));
14335f8e4474SKangjie Lu 	map.mem_start   = dev->mem_start;
14345f8e4474SKangjie Lu 	map.mem_end     = dev->mem_end;
14355f8e4474SKangjie Lu 	map.base_addr   = dev->base_addr;
14365f8e4474SKangjie Lu 	map.irq         = dev->irq;
14375f8e4474SKangjie Lu 	map.dma         = dev->dma;
14385f8e4474SKangjie Lu 	map.port        = dev->if_port;
14395f8e4474SKangjie Lu 
1440270cb4d0SNicolas Dichtel 	if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
1441b22b941bSHannes Frederic Sowa 		return -EMSGSIZE;
1442b22b941bSHannes Frederic Sowa 
1443b22b941bSHannes Frederic Sowa 	return 0;
1444b22b941bSHannes Frederic Sowa }
1445b22b941bSHannes Frederic Sowa 
1446a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_skb(struct net_device *dev)
1447d67b9cd2SDaniel Borkmann {
144858038695SMartin KaFai Lau 	const struct bpf_prog *generic_xdp_prog;
1449d67b9cd2SDaniel Borkmann 
1450d67b9cd2SDaniel Borkmann 	ASSERT_RTNL();
1451d67b9cd2SDaniel Borkmann 
145258038695SMartin KaFai Lau 	generic_xdp_prog = rtnl_dereference(dev->xdp_prog);
1453a25717d2SJakub Kicinski 	if (!generic_xdp_prog)
1454a25717d2SJakub Kicinski 		return 0;
1455a25717d2SJakub Kicinski 	return generic_xdp_prog->aux->id;
145658038695SMartin KaFai Lau }
1457ce158e58SJakub Kicinski 
1458a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_drv(struct net_device *dev)
1459a25717d2SJakub Kicinski {
14607f0a8382SAndrii Nakryiko 	return dev_xdp_prog_id(dev, XDP_MODE_DRV);
1461a25717d2SJakub Kicinski }
1462118b4aa2SJakub Kicinski 
1463a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_hw(struct net_device *dev)
1464a25717d2SJakub Kicinski {
14657f0a8382SAndrii Nakryiko 	return dev_xdp_prog_id(dev, XDP_MODE_HW);
1466a25717d2SJakub Kicinski }
1467a25717d2SJakub Kicinski 
1468a25717d2SJakub Kicinski static int rtnl_xdp_report_one(struct sk_buff *skb, struct net_device *dev,
1469a25717d2SJakub Kicinski 			       u32 *prog_id, u8 *mode, u8 tgt_mode, u32 attr,
1470a25717d2SJakub Kicinski 			       u32 (*get_prog_id)(struct net_device *dev))
1471a25717d2SJakub Kicinski {
1472a25717d2SJakub Kicinski 	u32 curr_id;
1473a25717d2SJakub Kicinski 	int err;
1474a25717d2SJakub Kicinski 
1475a25717d2SJakub Kicinski 	curr_id = get_prog_id(dev);
1476a25717d2SJakub Kicinski 	if (!curr_id)
1477a25717d2SJakub Kicinski 		return 0;
1478a25717d2SJakub Kicinski 
1479a25717d2SJakub Kicinski 	*prog_id = curr_id;
1480a25717d2SJakub Kicinski 	err = nla_put_u32(skb, attr, curr_id);
1481a25717d2SJakub Kicinski 	if (err)
1482a25717d2SJakub Kicinski 		return err;
1483a25717d2SJakub Kicinski 
1484a25717d2SJakub Kicinski 	if (*mode != XDP_ATTACHED_NONE)
1485a25717d2SJakub Kicinski 		*mode = XDP_ATTACHED_MULTI;
1486a25717d2SJakub Kicinski 	else
1487a25717d2SJakub Kicinski 		*mode = tgt_mode;
1488a25717d2SJakub Kicinski 
1489a25717d2SJakub Kicinski 	return 0;
1490d67b9cd2SDaniel Borkmann }
1491d67b9cd2SDaniel Borkmann 
1492d1fdd913SBrenden Blanco static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1493d1fdd913SBrenden Blanco {
1494d1fdd913SBrenden Blanco 	struct nlattr *xdp;
149558038695SMartin KaFai Lau 	u32 prog_id;
1496d1fdd913SBrenden Blanco 	int err;
14974f91da26SJakub Kicinski 	u8 mode;
1498d1fdd913SBrenden Blanco 
1499ae0be8deSMichal Kubecek 	xdp = nla_nest_start_noflag(skb, IFLA_XDP);
1500d1fdd913SBrenden Blanco 	if (!xdp)
1501d1fdd913SBrenden Blanco 		return -EMSGSIZE;
1502b5cdae32SDavid S. Miller 
1503a25717d2SJakub Kicinski 	prog_id = 0;
1504a25717d2SJakub Kicinski 	mode = XDP_ATTACHED_NONE;
1505202aabe8SJakub Kicinski 	err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_SKB,
1506202aabe8SJakub Kicinski 				  IFLA_XDP_SKB_PROG_ID, rtnl_xdp_prog_skb);
1507202aabe8SJakub Kicinski 	if (err)
1508a25717d2SJakub Kicinski 		goto err_cancel;
1509202aabe8SJakub Kicinski 	err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_DRV,
1510202aabe8SJakub Kicinski 				  IFLA_XDP_DRV_PROG_ID, rtnl_xdp_prog_drv);
1511202aabe8SJakub Kicinski 	if (err)
1512a25717d2SJakub Kicinski 		goto err_cancel;
1513202aabe8SJakub Kicinski 	err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_HW,
1514202aabe8SJakub Kicinski 				  IFLA_XDP_HW_PROG_ID, rtnl_xdp_prog_hw);
1515202aabe8SJakub Kicinski 	if (err)
1516a25717d2SJakub Kicinski 		goto err_cancel;
1517a25717d2SJakub Kicinski 
15184f91da26SJakub Kicinski 	err = nla_put_u8(skb, IFLA_XDP_ATTACHED, mode);
1519d1fdd913SBrenden Blanco 	if (err)
1520d1fdd913SBrenden Blanco 		goto err_cancel;
1521d1fdd913SBrenden Blanco 
1522a25717d2SJakub Kicinski 	if (prog_id && mode != XDP_ATTACHED_MULTI) {
152358038695SMartin KaFai Lau 		err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id);
152458038695SMartin KaFai Lau 		if (err)
152558038695SMartin KaFai Lau 			goto err_cancel;
152658038695SMartin KaFai Lau 	}
152758038695SMartin KaFai Lau 
1528d1fdd913SBrenden Blanco 	nla_nest_end(skb, xdp);
1529d1fdd913SBrenden Blanco 	return 0;
1530d1fdd913SBrenden Blanco 
1531d1fdd913SBrenden Blanco err_cancel:
1532d1fdd913SBrenden Blanco 	nla_nest_cancel(skb, xdp);
1533d1fdd913SBrenden Blanco 	return err;
1534d1fdd913SBrenden Blanco }
1535d1fdd913SBrenden Blanco 
15363d3ea5afSVlad Yasevich static u32 rtnl_get_event(unsigned long event)
15373d3ea5afSVlad Yasevich {
15383d3ea5afSVlad Yasevich 	u32 rtnl_event_type = IFLA_EVENT_NONE;
15393d3ea5afSVlad Yasevich 
15403d3ea5afSVlad Yasevich 	switch (event) {
15413d3ea5afSVlad Yasevich 	case NETDEV_REBOOT:
15423d3ea5afSVlad Yasevich 		rtnl_event_type = IFLA_EVENT_REBOOT;
15433d3ea5afSVlad Yasevich 		break;
15443d3ea5afSVlad Yasevich 	case NETDEV_FEAT_CHANGE:
15453d3ea5afSVlad Yasevich 		rtnl_event_type = IFLA_EVENT_FEATURES;
15463d3ea5afSVlad Yasevich 		break;
15473d3ea5afSVlad Yasevich 	case NETDEV_BONDING_FAILOVER:
15483d3ea5afSVlad Yasevich 		rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER;
15493d3ea5afSVlad Yasevich 		break;
15503d3ea5afSVlad Yasevich 	case NETDEV_NOTIFY_PEERS:
15513d3ea5afSVlad Yasevich 		rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS;
15523d3ea5afSVlad Yasevich 		break;
15533d3ea5afSVlad Yasevich 	case NETDEV_RESEND_IGMP:
15543d3ea5afSVlad Yasevich 		rtnl_event_type = IFLA_EVENT_IGMP_RESEND;
15553d3ea5afSVlad Yasevich 		break;
15563d3ea5afSVlad Yasevich 	case NETDEV_CHANGEINFODATA:
15573d3ea5afSVlad Yasevich 		rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS;
15583d3ea5afSVlad Yasevich 		break;
15593d3ea5afSVlad Yasevich 	default:
15603d3ea5afSVlad Yasevich 		break;
15613d3ea5afSVlad Yasevich 	}
15623d3ea5afSVlad Yasevich 
15633d3ea5afSVlad Yasevich 	return rtnl_event_type;
15643d3ea5afSVlad Yasevich }
15653d3ea5afSVlad Yasevich 
156679110a04SFlorian Westphal static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev)
156779110a04SFlorian Westphal {
156879110a04SFlorian Westphal 	const struct net_device *upper_dev;
156979110a04SFlorian Westphal 	int ret = 0;
157079110a04SFlorian Westphal 
157179110a04SFlorian Westphal 	rcu_read_lock();
157279110a04SFlorian Westphal 
157379110a04SFlorian Westphal 	upper_dev = netdev_master_upper_dev_get_rcu(dev);
157479110a04SFlorian Westphal 	if (upper_dev)
157579110a04SFlorian Westphal 		ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex);
157679110a04SFlorian Westphal 
157779110a04SFlorian Westphal 	rcu_read_unlock();
157879110a04SFlorian Westphal 	return ret;
157979110a04SFlorian Westphal }
158079110a04SFlorian Westphal 
1581feadc4b6SSabrina Dubroca static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev,
1582feadc4b6SSabrina Dubroca 			  bool force)
158379110a04SFlorian Westphal {
158479110a04SFlorian Westphal 	int ifindex = dev_get_iflink(dev);
158579110a04SFlorian Westphal 
1586feadc4b6SSabrina Dubroca 	if (force || dev->ifindex != ifindex)
158779110a04SFlorian Westphal 		return nla_put_u32(skb, IFLA_LINK, ifindex);
1588feadc4b6SSabrina Dubroca 
1589feadc4b6SSabrina Dubroca 	return 0;
159079110a04SFlorian Westphal }
159179110a04SFlorian Westphal 
15926c557001SFlorian Westphal static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb,
15936c557001SFlorian Westphal 					      struct net_device *dev)
15946c557001SFlorian Westphal {
15956c557001SFlorian Westphal 	char buf[IFALIASZ];
15966c557001SFlorian Westphal 	int ret;
15976c557001SFlorian Westphal 
15986c557001SFlorian Westphal 	ret = dev_get_alias(dev, buf, sizeof(buf));
15996c557001SFlorian Westphal 	return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0;
16006c557001SFlorian Westphal }
16016c557001SFlorian Westphal 
1602b1e66b9aSFlorian Westphal static int rtnl_fill_link_netnsid(struct sk_buff *skb,
160379e1ad14SJiri Benc 				  const struct net_device *dev,
1604d4e4fdf9SGuillaume Nault 				  struct net *src_net, gfp_t gfp)
1605b1e66b9aSFlorian Westphal {
1606feadc4b6SSabrina Dubroca 	bool put_iflink = false;
1607feadc4b6SSabrina Dubroca 
1608b1e66b9aSFlorian Westphal 	if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) {
1609b1e66b9aSFlorian Westphal 		struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1610b1e66b9aSFlorian Westphal 
1611b1e66b9aSFlorian Westphal 		if (!net_eq(dev_net(dev), link_net)) {
1612d4e4fdf9SGuillaume Nault 			int id = peernet2id_alloc(src_net, link_net, gfp);
1613b1e66b9aSFlorian Westphal 
1614b1e66b9aSFlorian Westphal 			if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1615b1e66b9aSFlorian Westphal 				return -EMSGSIZE;
1616feadc4b6SSabrina Dubroca 
1617feadc4b6SSabrina Dubroca 			put_iflink = true;
1618b1e66b9aSFlorian Westphal 		}
1619b1e66b9aSFlorian Westphal 	}
1620b1e66b9aSFlorian Westphal 
1621feadc4b6SSabrina Dubroca 	return nla_put_iflink(skb, dev, put_iflink);
1622b1e66b9aSFlorian Westphal }
1623b1e66b9aSFlorian Westphal 
1624070cbf5bSFlorian Westphal static int rtnl_fill_link_af(struct sk_buff *skb,
1625070cbf5bSFlorian Westphal 			     const struct net_device *dev,
1626070cbf5bSFlorian Westphal 			     u32 ext_filter_mask)
1627070cbf5bSFlorian Westphal {
1628070cbf5bSFlorian Westphal 	const struct rtnl_af_ops *af_ops;
1629070cbf5bSFlorian Westphal 	struct nlattr *af_spec;
1630070cbf5bSFlorian Westphal 
1631ae0be8deSMichal Kubecek 	af_spec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
1632070cbf5bSFlorian Westphal 	if (!af_spec)
1633070cbf5bSFlorian Westphal 		return -EMSGSIZE;
1634070cbf5bSFlorian Westphal 
16355fa85a09SFlorian Westphal 	list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
1636070cbf5bSFlorian Westphal 		struct nlattr *af;
1637070cbf5bSFlorian Westphal 		int err;
1638070cbf5bSFlorian Westphal 
1639070cbf5bSFlorian Westphal 		if (!af_ops->fill_link_af)
1640070cbf5bSFlorian Westphal 			continue;
1641070cbf5bSFlorian Westphal 
1642ae0be8deSMichal Kubecek 		af = nla_nest_start_noflag(skb, af_ops->family);
1643070cbf5bSFlorian Westphal 		if (!af)
1644070cbf5bSFlorian Westphal 			return -EMSGSIZE;
1645070cbf5bSFlorian Westphal 
1646070cbf5bSFlorian Westphal 		err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
1647070cbf5bSFlorian Westphal 		/*
1648070cbf5bSFlorian Westphal 		 * Caller may return ENODATA to indicate that there
1649070cbf5bSFlorian Westphal 		 * was no data to be dumped. This is not an error, it
1650070cbf5bSFlorian Westphal 		 * means we should trim the attribute header and
1651070cbf5bSFlorian Westphal 		 * continue.
1652070cbf5bSFlorian Westphal 		 */
1653070cbf5bSFlorian Westphal 		if (err == -ENODATA)
1654070cbf5bSFlorian Westphal 			nla_nest_cancel(skb, af);
1655070cbf5bSFlorian Westphal 		else if (err < 0)
1656070cbf5bSFlorian Westphal 			return -EMSGSIZE;
1657070cbf5bSFlorian Westphal 
1658070cbf5bSFlorian Westphal 		nla_nest_end(skb, af);
1659070cbf5bSFlorian Westphal 	}
1660070cbf5bSFlorian Westphal 
1661070cbf5bSFlorian Westphal 	nla_nest_end(skb, af_spec);
1662070cbf5bSFlorian Westphal 	return 0;
1663070cbf5bSFlorian Westphal }
1664070cbf5bSFlorian Westphal 
166588f4fb0cSJiri Pirko static int rtnl_fill_alt_ifnames(struct sk_buff *skb,
166688f4fb0cSJiri Pirko 				 const struct net_device *dev)
166788f4fb0cSJiri Pirko {
166888f4fb0cSJiri Pirko 	struct netdev_name_node *name_node;
166988f4fb0cSJiri Pirko 	int count = 0;
167088f4fb0cSJiri Pirko 
167188f4fb0cSJiri Pirko 	list_for_each_entry(name_node, &dev->name_node->list, list) {
167288f4fb0cSJiri Pirko 		if (nla_put_string(skb, IFLA_ALT_IFNAME, name_node->name))
167388f4fb0cSJiri Pirko 			return -EMSGSIZE;
167488f4fb0cSJiri Pirko 		count++;
167588f4fb0cSJiri Pirko 	}
167688f4fb0cSJiri Pirko 	return count;
167788f4fb0cSJiri Pirko }
167888f4fb0cSJiri Pirko 
167988f4fb0cSJiri Pirko static int rtnl_fill_prop_list(struct sk_buff *skb,
168088f4fb0cSJiri Pirko 			       const struct net_device *dev)
168188f4fb0cSJiri Pirko {
168288f4fb0cSJiri Pirko 	struct nlattr *prop_list;
168388f4fb0cSJiri Pirko 	int ret;
168488f4fb0cSJiri Pirko 
168588f4fb0cSJiri Pirko 	prop_list = nla_nest_start(skb, IFLA_PROP_LIST);
168688f4fb0cSJiri Pirko 	if (!prop_list)
168788f4fb0cSJiri Pirko 		return -EMSGSIZE;
168888f4fb0cSJiri Pirko 
168988f4fb0cSJiri Pirko 	ret = rtnl_fill_alt_ifnames(skb, dev);
169088f4fb0cSJiri Pirko 	if (ret <= 0)
169188f4fb0cSJiri Pirko 		goto nest_cancel;
169288f4fb0cSJiri Pirko 
169388f4fb0cSJiri Pirko 	nla_nest_end(skb, prop_list);
169488f4fb0cSJiri Pirko 	return 0;
169588f4fb0cSJiri Pirko 
169688f4fb0cSJiri Pirko nest_cancel:
169788f4fb0cSJiri Pirko 	nla_nest_cancel(skb, prop_list);
169888f4fb0cSJiri Pirko 	return ret;
169988f4fb0cSJiri Pirko }
170088f4fb0cSJiri Pirko 
1701829eb208SRoopa Prabhu static int rtnl_fill_proto_down(struct sk_buff *skb,
1702829eb208SRoopa Prabhu 				const struct net_device *dev)
1703829eb208SRoopa Prabhu {
1704829eb208SRoopa Prabhu 	struct nlattr *pr;
1705829eb208SRoopa Prabhu 	u32 preason;
1706829eb208SRoopa Prabhu 
1707829eb208SRoopa Prabhu 	if (nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down))
1708829eb208SRoopa Prabhu 		goto nla_put_failure;
1709829eb208SRoopa Prabhu 
1710829eb208SRoopa Prabhu 	preason = dev->proto_down_reason;
1711829eb208SRoopa Prabhu 	if (!preason)
1712829eb208SRoopa Prabhu 		return 0;
1713829eb208SRoopa Prabhu 
1714829eb208SRoopa Prabhu 	pr = nla_nest_start(skb, IFLA_PROTO_DOWN_REASON);
1715829eb208SRoopa Prabhu 	if (!pr)
1716829eb208SRoopa Prabhu 		return -EMSGSIZE;
1717829eb208SRoopa Prabhu 
1718829eb208SRoopa Prabhu 	if (nla_put_u32(skb, IFLA_PROTO_DOWN_REASON_VALUE, preason)) {
1719829eb208SRoopa Prabhu 		nla_nest_cancel(skb, pr);
1720829eb208SRoopa Prabhu 		goto nla_put_failure;
1721829eb208SRoopa Prabhu 	}
1722829eb208SRoopa Prabhu 
1723829eb208SRoopa Prabhu 	nla_nest_end(skb, pr);
1724829eb208SRoopa Prabhu 	return 0;
1725829eb208SRoopa Prabhu 
1726829eb208SRoopa Prabhu nla_put_failure:
1727829eb208SRoopa Prabhu 	return -EMSGSIZE;
1728829eb208SRoopa Prabhu }
1729829eb208SRoopa Prabhu 
173079e1ad14SJiri Benc static int rtnl_fill_ifinfo(struct sk_buff *skb,
173179e1ad14SJiri Benc 			    struct net_device *dev, struct net *src_net,
1732575c3e2aSPatrick McHardy 			    int type, u32 pid, u32 seq, u32 change,
17333d3ea5afSVlad Yasevich 			    unsigned int flags, u32 ext_filter_mask,
173438e01b30SNicolas Dichtel 			    u32 event, int *new_nsid, int new_ifindex,
1735d4e4fdf9SGuillaume Nault 			    int tgt_netnsid, gfp_t gfp)
1736b60c5115SThomas Graf {
1737b60c5115SThomas Graf 	struct ifinfomsg *ifm;
17381da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
17395891cd5eSEric Dumazet 	struct Qdisc *qdisc;
17401da177e4SLinus Torvalds 
17412907c35fSEric Dumazet 	ASSERT_RTNL();
1742b60c5115SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1743b60c5115SThomas Graf 	if (nlh == NULL)
174426932566SPatrick McHardy 		return -EMSGSIZE;
17451da177e4SLinus Torvalds 
1746b60c5115SThomas Graf 	ifm = nlmsg_data(nlh);
1747b60c5115SThomas Graf 	ifm->ifi_family = AF_UNSPEC;
1748b60c5115SThomas Graf 	ifm->__ifi_pad = 0;
1749b60c5115SThomas Graf 	ifm->ifi_type = dev->type;
1750b60c5115SThomas Graf 	ifm->ifi_index = dev->ifindex;
1751b60c5115SThomas Graf 	ifm->ifi_flags = dev_get_flags(dev);
1752b60c5115SThomas Graf 	ifm->ifi_change = change;
17531da177e4SLinus Torvalds 
17547e4a8d5aSChristian Brauner 	if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_TARGET_NETNSID, tgt_netnsid))
175579e1ad14SJiri Benc 		goto nla_put_failure;
175679e1ad14SJiri Benc 
17575891cd5eSEric Dumazet 	qdisc = rtnl_dereference(dev->qdisc);
1758a6574349SDavid S. Miller 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1759a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1760a6574349SDavid S. Miller 	    nla_put_u8(skb, IFLA_OPERSTATE,
1761a6574349SDavid S. Miller 		       netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1762a6574349SDavid S. Miller 	    nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1763a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
17643e7a50ceSStephen Hemminger 	    nla_put_u32(skb, IFLA_MIN_MTU, dev->min_mtu) ||
17653e7a50ceSStephen Hemminger 	    nla_put_u32(skb, IFLA_MAX_MTU, dev->max_mtu) ||
1766a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_GROUP, dev->group) ||
1767edbc0bb3SBen Greear 	    nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
176876ff5cc9SJiri Pirko 	    nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
1769c70ce028SEric Dumazet 	    nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1770c70ce028SEric Dumazet 	    nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
1771eac1b93cSCoco Li 	    nla_put_u32(skb, IFLA_GRO_MAX_SIZE, dev->gro_max_size) ||
17721d69c2b3SMark A. Greer #ifdef CONFIG_RPS
177376ff5cc9SJiri Pirko 	    nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
17741d69c2b3SMark A. Greer #endif
177579110a04SFlorian Westphal 	    put_master_ifindex(skb, dev) ||
17769a57247fSJiri Pirko 	    nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
17775891cd5eSEric Dumazet 	    (qdisc &&
17785891cd5eSEric Dumazet 	     nla_put_string(skb, IFLA_QDISC, qdisc->ops->id)) ||
17796c557001SFlorian Westphal 	    nla_put_ifalias(skb, dev) ||
17802d3b479dSdavid decotigny 	    nla_put_u32(skb, IFLA_CARRIER_CHANGES,
1781b2d3bcfaSDavid Decotigny 			atomic_read(&dev->carrier_up_count) +
1782b2d3bcfaSDavid Decotigny 			atomic_read(&dev->carrier_down_count)) ||
1783b2d3bcfaSDavid Decotigny 	    nla_put_u32(skb, IFLA_CARRIER_UP_COUNT,
1784b2d3bcfaSDavid Decotigny 			atomic_read(&dev->carrier_up_count)) ||
1785b2d3bcfaSDavid Decotigny 	    nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT,
1786b2d3bcfaSDavid Decotigny 			atomic_read(&dev->carrier_down_count)))
1787a6574349SDavid S. Miller 		goto nla_put_failure;
17880b815a1aSStephen Hemminger 
1789829eb208SRoopa Prabhu 	if (rtnl_fill_proto_down(skb, dev))
1790829eb208SRoopa Prabhu 		goto nla_put_failure;
1791829eb208SRoopa Prabhu 
17923d3ea5afSVlad Yasevich 	if (event != IFLA_EVENT_NONE) {
17933d3ea5afSVlad Yasevich 		if (nla_put_u32(skb, IFLA_EVENT, event))
17943d3ea5afSVlad Yasevich 			goto nla_put_failure;
17953d3ea5afSVlad Yasevich 	}
17963d3ea5afSVlad Yasevich 
1797b22b941bSHannes Frederic Sowa 	if (rtnl_fill_link_ifmap(skb, dev))
1798a6574349SDavid S. Miller 		goto nla_put_failure;
17991da177e4SLinus Torvalds 
18001da177e4SLinus Torvalds 	if (dev->addr_len) {
1801a6574349SDavid S. Miller 		if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1802a6574349SDavid S. Miller 		    nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1803a6574349SDavid S. Miller 			goto nla_put_failure;
18041da177e4SLinus Torvalds 	}
18051da177e4SLinus Torvalds 
180666cae9edSJiri Pirko 	if (rtnl_phys_port_id_fill(skb, dev))
180766cae9edSJiri Pirko 		goto nla_put_failure;
180866cae9edSJiri Pirko 
1809db24a904SDavid Ahern 	if (rtnl_phys_port_name_fill(skb, dev))
1810db24a904SDavid Ahern 		goto nla_put_failure;
1811db24a904SDavid Ahern 
181282f28412SJiri Pirko 	if (rtnl_phys_switch_id_fill(skb, dev))
181382f28412SJiri Pirko 		goto nla_put_failure;
181482f28412SJiri Pirko 
1815b22b941bSHannes Frederic Sowa 	if (rtnl_fill_stats(skb, dev))
1816b60c5115SThomas Graf 		goto nla_put_failure;
1817b60c5115SThomas Graf 
1818250fc3dfSFlorian Westphal 	if (rtnl_fill_vf(skb, dev, ext_filter_mask))
1819a6574349SDavid S. Miller 		goto nla_put_failure;
182057b61080SScott Feldman 
1821c53864fdSDavid Gibson 	if (rtnl_port_fill(skb, dev, ext_filter_mask))
182257b61080SScott Feldman 		goto nla_put_failure;
182357b61080SScott Feldman 
1824d1fdd913SBrenden Blanco 	if (rtnl_xdp_fill(skb, dev))
1825d1fdd913SBrenden Blanco 		goto nla_put_failure;
1826d1fdd913SBrenden Blanco 
1827ba7d49b1SJiri Pirko 	if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
182838f7b870SPatrick McHardy 		if (rtnl_link_fill(skb, dev) < 0)
182938f7b870SPatrick McHardy 			goto nla_put_failure;
183038f7b870SPatrick McHardy 	}
183138f7b870SPatrick McHardy 
1832d4e4fdf9SGuillaume Nault 	if (rtnl_fill_link_netnsid(skb, dev, src_net, gfp))
1833d37512a2SNicolas Dichtel 		goto nla_put_failure;
1834d37512a2SNicolas Dichtel 
18356621dd29SNicolas Dichtel 	if (new_nsid &&
18366621dd29SNicolas Dichtel 	    nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0)
18376621dd29SNicolas Dichtel 		goto nla_put_failure;
183838e01b30SNicolas Dichtel 	if (new_ifindex &&
183938e01b30SNicolas Dichtel 	    nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0)
184038e01b30SNicolas Dichtel 		goto nla_put_failure;
184138e01b30SNicolas Dichtel 
1842f74877a5SMichal Kubecek 	if (memchr_inv(dev->perm_addr, '\0', dev->addr_len) &&
1843f74877a5SMichal Kubecek 	    nla_put(skb, IFLA_PERM_ADDRESS, dev->addr_len, dev->perm_addr))
1844f74877a5SMichal Kubecek 		goto nla_put_failure;
18456621dd29SNicolas Dichtel 
18465fa85a09SFlorian Westphal 	rcu_read_lock();
1847070cbf5bSFlorian Westphal 	if (rtnl_fill_link_af(skb, dev, ext_filter_mask))
18485fa85a09SFlorian Westphal 		goto nla_put_failure_rcu;
18495fa85a09SFlorian Westphal 	rcu_read_unlock();
1850f8ff182cSThomas Graf 
185188f4fb0cSJiri Pirko 	if (rtnl_fill_prop_list(skb, dev))
185288f4fb0cSJiri Pirko 		goto nla_put_failure;
185388f4fb0cSJiri Pirko 
185400e77ed8SJohannes Berg 	if (dev->dev.parent &&
185500e77ed8SJohannes Berg 	    nla_put_string(skb, IFLA_PARENT_DEV_NAME,
185600e77ed8SJohannes Berg 			   dev_name(dev->dev.parent)))
185700e77ed8SJohannes Berg 		goto nla_put_failure;
185800e77ed8SJohannes Berg 
185900e77ed8SJohannes Berg 	if (dev->dev.parent && dev->dev.parent->bus &&
186000e77ed8SJohannes Berg 	    nla_put_string(skb, IFLA_PARENT_DEV_BUS_NAME,
186100e77ed8SJohannes Berg 			   dev->dev.parent->bus->name))
186200e77ed8SJohannes Berg 		goto nla_put_failure;
186300e77ed8SJohannes Berg 
1864053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
1865053c095aSJohannes Berg 	return 0;
1866b60c5115SThomas Graf 
18675fa85a09SFlorian Westphal nla_put_failure_rcu:
18685fa85a09SFlorian Westphal 	rcu_read_unlock();
1869b60c5115SThomas Graf nla_put_failure:
187026932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
187126932566SPatrick McHardy 	return -EMSGSIZE;
18721da177e4SLinus Torvalds }
18731da177e4SLinus Torvalds 
1874f7b12606SJiri Pirko static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1875f7b12606SJiri Pirko 	[IFLA_IFNAME]		= { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1876f7b12606SJiri Pirko 	[IFLA_ADDRESS]		= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1877f7b12606SJiri Pirko 	[IFLA_BROADCAST]	= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1878f7b12606SJiri Pirko 	[IFLA_MAP]		= { .len = sizeof(struct rtnl_link_ifmap) },
1879f7b12606SJiri Pirko 	[IFLA_MTU]		= { .type = NLA_U32 },
1880f7b12606SJiri Pirko 	[IFLA_LINK]		= { .type = NLA_U32 },
1881f7b12606SJiri Pirko 	[IFLA_MASTER]		= { .type = NLA_U32 },
1882f7b12606SJiri Pirko 	[IFLA_CARRIER]		= { .type = NLA_U8 },
1883f7b12606SJiri Pirko 	[IFLA_TXQLEN]		= { .type = NLA_U32 },
1884f7b12606SJiri Pirko 	[IFLA_WEIGHT]		= { .type = NLA_U32 },
1885f7b12606SJiri Pirko 	[IFLA_OPERSTATE]	= { .type = NLA_U8 },
1886f7b12606SJiri Pirko 	[IFLA_LINKMODE]		= { .type = NLA_U8 },
1887f7b12606SJiri Pirko 	[IFLA_LINKINFO]		= { .type = NLA_NESTED },
1888f7b12606SJiri Pirko 	[IFLA_NET_NS_PID]	= { .type = NLA_U32 },
1889f7b12606SJiri Pirko 	[IFLA_NET_NS_FD]	= { .type = NLA_U32 },
18902459b4c6SNicolas Dichtel 	/* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
18912459b4c6SNicolas Dichtel 	 * allow 0-length string (needed to remove an alias).
18922459b4c6SNicolas Dichtel 	 */
18932459b4c6SNicolas Dichtel 	[IFLA_IFALIAS]	        = { .type = NLA_BINARY, .len = IFALIASZ - 1 },
1894f7b12606SJiri Pirko 	[IFLA_VFINFO_LIST]	= {. type = NLA_NESTED },
1895f7b12606SJiri Pirko 	[IFLA_VF_PORTS]		= { .type = NLA_NESTED },
1896f7b12606SJiri Pirko 	[IFLA_PORT_SELF]	= { .type = NLA_NESTED },
1897f7b12606SJiri Pirko 	[IFLA_AF_SPEC]		= { .type = NLA_NESTED },
1898f7b12606SJiri Pirko 	[IFLA_EXT_MASK]		= { .type = NLA_U32 },
1899f7b12606SJiri Pirko 	[IFLA_PROMISCUITY]	= { .type = NLA_U32 },
1900f7b12606SJiri Pirko 	[IFLA_NUM_TX_QUEUES]	= { .type = NLA_U32 },
1901f7b12606SJiri Pirko 	[IFLA_NUM_RX_QUEUES]	= { .type = NLA_U32 },
190246e6b992SStephen Hemminger 	[IFLA_GSO_MAX_SEGS]	= { .type = NLA_U32 },
190346e6b992SStephen Hemminger 	[IFLA_GSO_MAX_SIZE]	= { .type = NLA_U32 },
190402637fceSJiri Pirko 	[IFLA_PHYS_PORT_ID]	= { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
19052d3b479dSdavid decotigny 	[IFLA_CARRIER_CHANGES]	= { .type = NLA_U32 },  /* ignored */
190682f28412SJiri Pirko 	[IFLA_PHYS_SWITCH_ID]	= { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1907317f4810SNicolas Dichtel 	[IFLA_LINK_NETNSID]	= { .type = NLA_S32 },
190888d6378bSAnuradha Karuppiah 	[IFLA_PROTO_DOWN]	= { .type = NLA_U8 },
1909d1fdd913SBrenden Blanco 	[IFLA_XDP]		= { .type = NLA_NESTED },
19103d3ea5afSVlad Yasevich 	[IFLA_EVENT]		= { .type = NLA_U32 },
1911db833d40SSerhey Popovych 	[IFLA_GROUP]		= { .type = NLA_U32 },
19127e4a8d5aSChristian Brauner 	[IFLA_TARGET_NETNSID]	= { .type = NLA_S32 },
1913b2d3bcfaSDavid Decotigny 	[IFLA_CARRIER_UP_COUNT]	= { .type = NLA_U32 },
1914b2d3bcfaSDavid Decotigny 	[IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 },
19153e7a50ceSStephen Hemminger 	[IFLA_MIN_MTU]		= { .type = NLA_U32 },
19163e7a50ceSStephen Hemminger 	[IFLA_MAX_MTU]		= { .type = NLA_U32 },
191736fbf1e5SJiri Pirko 	[IFLA_PROP_LIST]	= { .type = NLA_NESTED },
191836fbf1e5SJiri Pirko 	[IFLA_ALT_IFNAME]	= { .type = NLA_STRING,
191936fbf1e5SJiri Pirko 				    .len = ALTIFNAMSIZ - 1 },
1920f74877a5SMichal Kubecek 	[IFLA_PERM_ADDRESS]	= { .type = NLA_REJECT },
1921829eb208SRoopa Prabhu 	[IFLA_PROTO_DOWN_REASON] = { .type = NLA_NESTED },
19227e4a5131SAndrei Vagin 	[IFLA_NEW_IFINDEX]	= NLA_POLICY_MIN(NLA_S32, 1),
192388b71053SJohannes Berg 	[IFLA_PARENT_DEV_NAME]	= { .type = NLA_NUL_STRING },
1924eac1b93cSCoco Li 	[IFLA_GRO_MAX_SIZE]	= { .type = NLA_U32 },
1925f7b12606SJiri Pirko };
1926f7b12606SJiri Pirko 
1927f7b12606SJiri Pirko static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1928f7b12606SJiri Pirko 	[IFLA_INFO_KIND]	= { .type = NLA_STRING },
1929f7b12606SJiri Pirko 	[IFLA_INFO_DATA]	= { .type = NLA_NESTED },
1930f7b12606SJiri Pirko 	[IFLA_INFO_SLAVE_KIND]	= { .type = NLA_STRING },
1931f7b12606SJiri Pirko 	[IFLA_INFO_SLAVE_DATA]	= { .type = NLA_NESTED },
1932f7b12606SJiri Pirko };
1933f7b12606SJiri Pirko 
1934f7b12606SJiri Pirko static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1935364d5716SDaniel Borkmann 	[IFLA_VF_MAC]		= { .len = sizeof(struct ifla_vf_mac) },
193675345f88SDenis Kirjanov 	[IFLA_VF_BROADCAST]	= { .type = NLA_REJECT },
1937364d5716SDaniel Borkmann 	[IFLA_VF_VLAN]		= { .len = sizeof(struct ifla_vf_vlan) },
193879aab093SMoshe Shemesh 	[IFLA_VF_VLAN_LIST]     = { .type = NLA_NESTED },
1939364d5716SDaniel Borkmann 	[IFLA_VF_TX_RATE]	= { .len = sizeof(struct ifla_vf_tx_rate) },
1940364d5716SDaniel Borkmann 	[IFLA_VF_SPOOFCHK]	= { .len = sizeof(struct ifla_vf_spoofchk) },
1941364d5716SDaniel Borkmann 	[IFLA_VF_RATE]		= { .len = sizeof(struct ifla_vf_rate) },
1942364d5716SDaniel Borkmann 	[IFLA_VF_LINK_STATE]	= { .len = sizeof(struct ifla_vf_link_state) },
194301a3d796SVlad Zolotarov 	[IFLA_VF_RSS_QUERY_EN]	= { .len = sizeof(struct ifla_vf_rss_query_en) },
19443b766cd8SEran Ben Elisha 	[IFLA_VF_STATS]		= { .type = NLA_NESTED },
1945dd461d6aSHiroshi Shimamoto 	[IFLA_VF_TRUST]		= { .len = sizeof(struct ifla_vf_trust) },
1946cc8e27ccSEli Cohen 	[IFLA_VF_IB_NODE_GUID]	= { .len = sizeof(struct ifla_vf_guid) },
1947cc8e27ccSEli Cohen 	[IFLA_VF_IB_PORT_GUID]	= { .len = sizeof(struct ifla_vf_guid) },
19483b766cd8SEran Ben Elisha };
19493b766cd8SEran Ben Elisha 
1950f7b12606SJiri Pirko static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1951f7b12606SJiri Pirko 	[IFLA_PORT_VF]		= { .type = NLA_U32 },
1952f7b12606SJiri Pirko 	[IFLA_PORT_PROFILE]	= { .type = NLA_STRING,
1953f7b12606SJiri Pirko 				    .len = PORT_PROFILE_MAX },
1954f7b12606SJiri Pirko 	[IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1955f7b12606SJiri Pirko 				      .len = PORT_UUID_MAX },
1956f7b12606SJiri Pirko 	[IFLA_PORT_HOST_UUID]	= { .type = NLA_STRING,
1957f7b12606SJiri Pirko 				    .len = PORT_UUID_MAX },
1958f7b12606SJiri Pirko 	[IFLA_PORT_REQUEST]	= { .type = NLA_U8, },
1959f7b12606SJiri Pirko 	[IFLA_PORT_RESPONSE]	= { .type = NLA_U16, },
1960025331dfSDaniel Borkmann 
1961025331dfSDaniel Borkmann 	/* Unused, but we need to keep it here since user space could
1962025331dfSDaniel Borkmann 	 * fill it. It's also broken with regard to NLA_BINARY use in
1963025331dfSDaniel Borkmann 	 * combination with structs.
1964025331dfSDaniel Borkmann 	 */
1965025331dfSDaniel Borkmann 	[IFLA_PORT_VSI_TYPE]	= { .type = NLA_BINARY,
1966025331dfSDaniel Borkmann 				    .len = sizeof(struct ifla_port_vsi) },
1967f7b12606SJiri Pirko };
1968f7b12606SJiri Pirko 
1969d1fdd913SBrenden Blanco static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
197092234c8fSToke Høiland-Jørgensen 	[IFLA_XDP_UNSPEC]	= { .strict_start_type = IFLA_XDP_EXPECTED_FD },
1971d1fdd913SBrenden Blanco 	[IFLA_XDP_FD]		= { .type = NLA_S32 },
197292234c8fSToke Høiland-Jørgensen 	[IFLA_XDP_EXPECTED_FD]	= { .type = NLA_S32 },
1973d1fdd913SBrenden Blanco 	[IFLA_XDP_ATTACHED]	= { .type = NLA_U8 },
197485de8576SDaniel Borkmann 	[IFLA_XDP_FLAGS]	= { .type = NLA_U32 },
197558038695SMartin KaFai Lau 	[IFLA_XDP_PROG_ID]	= { .type = NLA_U32 },
1976d1fdd913SBrenden Blanco };
1977d1fdd913SBrenden Blanco 
1978dc599f76SDavid Ahern static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1979dc599f76SDavid Ahern {
1980dc599f76SDavid Ahern 	const struct rtnl_link_ops *ops = NULL;
1981dc599f76SDavid Ahern 	struct nlattr *linfo[IFLA_INFO_MAX + 1];
1982dc599f76SDavid Ahern 
19838cb08174SJohannes Berg 	if (nla_parse_nested_deprecated(linfo, IFLA_INFO_MAX, nla, ifla_info_policy, NULL) < 0)
1984dc599f76SDavid Ahern 		return NULL;
1985dc599f76SDavid Ahern 
1986dc599f76SDavid Ahern 	if (linfo[IFLA_INFO_KIND]) {
1987dc599f76SDavid Ahern 		char kind[MODULE_NAME_LEN];
1988dc599f76SDavid Ahern 
1989872f6903SFrancis Laniel 		nla_strscpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1990dc599f76SDavid Ahern 		ops = rtnl_link_ops_get(kind);
1991dc599f76SDavid Ahern 	}
1992dc599f76SDavid Ahern 
1993dc599f76SDavid Ahern 	return ops;
1994dc599f76SDavid Ahern }
1995dc599f76SDavid Ahern 
1996dc599f76SDavid Ahern static bool link_master_filtered(struct net_device *dev, int master_idx)
1997dc599f76SDavid Ahern {
1998dc599f76SDavid Ahern 	struct net_device *master;
1999dc599f76SDavid Ahern 
2000dc599f76SDavid Ahern 	if (!master_idx)
2001dc599f76SDavid Ahern 		return false;
2002dc599f76SDavid Ahern 
2003dc599f76SDavid Ahern 	master = netdev_master_upper_dev_get(dev);
2004d3432bf1SLahav Schlesinger 
2005d3432bf1SLahav Schlesinger 	/* 0 is already used to denote IFLA_MASTER wasn't passed, therefore need
2006d3432bf1SLahav Schlesinger 	 * another invalid value for ifindex to denote "no master".
2007d3432bf1SLahav Schlesinger 	 */
2008d3432bf1SLahav Schlesinger 	if (master_idx == -1)
2009d3432bf1SLahav Schlesinger 		return !!master;
2010d3432bf1SLahav Schlesinger 
2011dc599f76SDavid Ahern 	if (!master || master->ifindex != master_idx)
2012dc599f76SDavid Ahern 		return true;
2013dc599f76SDavid Ahern 
2014dc599f76SDavid Ahern 	return false;
2015dc599f76SDavid Ahern }
2016dc599f76SDavid Ahern 
2017dc599f76SDavid Ahern static bool link_kind_filtered(const struct net_device *dev,
2018dc599f76SDavid Ahern 			       const struct rtnl_link_ops *kind_ops)
2019dc599f76SDavid Ahern {
2020dc599f76SDavid Ahern 	if (kind_ops && dev->rtnl_link_ops != kind_ops)
2021dc599f76SDavid Ahern 		return true;
2022dc599f76SDavid Ahern 
2023dc599f76SDavid Ahern 	return false;
2024dc599f76SDavid Ahern }
2025dc599f76SDavid Ahern 
2026dc599f76SDavid Ahern static bool link_dump_filtered(struct net_device *dev,
2027dc599f76SDavid Ahern 			       int master_idx,
2028dc599f76SDavid Ahern 			       const struct rtnl_link_ops *kind_ops)
2029dc599f76SDavid Ahern {
2030dc599f76SDavid Ahern 	if (link_master_filtered(dev, master_idx) ||
2031dc599f76SDavid Ahern 	    link_kind_filtered(dev, kind_ops))
2032dc599f76SDavid Ahern 		return true;
2033dc599f76SDavid Ahern 
2034dc599f76SDavid Ahern 	return false;
2035dc599f76SDavid Ahern }
2036dc599f76SDavid Ahern 
2037c383edc4SChristian Brauner /**
2038c383edc4SChristian Brauner  * rtnl_get_net_ns_capable - Get netns if sufficiently privileged.
2039c383edc4SChristian Brauner  * @sk: netlink socket
2040c383edc4SChristian Brauner  * @netnsid: network namespace identifier
2041c383edc4SChristian Brauner  *
2042c383edc4SChristian Brauner  * Returns the network namespace identified by netnsid on success or an error
2043c383edc4SChristian Brauner  * pointer on failure.
2044c383edc4SChristian Brauner  */
2045c383edc4SChristian Brauner struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid)
204679e1ad14SJiri Benc {
204779e1ad14SJiri Benc 	struct net *net;
204879e1ad14SJiri Benc 
2049f428fe4aSAndrei Vagin 	net = get_net_ns_by_id(sock_net(sk), netnsid);
205079e1ad14SJiri Benc 	if (!net)
205179e1ad14SJiri Benc 		return ERR_PTR(-EINVAL);
205279e1ad14SJiri Benc 
205379e1ad14SJiri Benc 	/* For now, the caller is required to have CAP_NET_ADMIN in
205479e1ad14SJiri Benc 	 * the user namespace owning the target net ns.
205579e1ad14SJiri Benc 	 */
2056f428fe4aSAndrei Vagin 	if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) {
205779e1ad14SJiri Benc 		put_net(net);
205879e1ad14SJiri Benc 		return ERR_PTR(-EACCES);
205979e1ad14SJiri Benc 	}
206079e1ad14SJiri Benc 	return net;
206179e1ad14SJiri Benc }
2062c383edc4SChristian Brauner EXPORT_SYMBOL_GPL(rtnl_get_net_ns_capable);
206379e1ad14SJiri Benc 
2064905cf0abSDavid Ahern static int rtnl_valid_dump_ifinfo_req(const struct nlmsghdr *nlh,
2065905cf0abSDavid Ahern 				      bool strict_check, struct nlattr **tb,
2066905cf0abSDavid Ahern 				      struct netlink_ext_ack *extack)
2067905cf0abSDavid Ahern {
2068905cf0abSDavid Ahern 	int hdrlen;
2069905cf0abSDavid Ahern 
2070905cf0abSDavid Ahern 	if (strict_check) {
2071905cf0abSDavid Ahern 		struct ifinfomsg *ifm;
2072905cf0abSDavid Ahern 
2073905cf0abSDavid Ahern 		if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
2074905cf0abSDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid header for link dump");
2075905cf0abSDavid Ahern 			return -EINVAL;
2076905cf0abSDavid Ahern 		}
2077905cf0abSDavid Ahern 
2078905cf0abSDavid Ahern 		ifm = nlmsg_data(nlh);
2079905cf0abSDavid Ahern 		if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
2080905cf0abSDavid Ahern 		    ifm->ifi_change) {
2081905cf0abSDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid values in header for link dump request");
2082905cf0abSDavid Ahern 			return -EINVAL;
2083905cf0abSDavid Ahern 		}
2084905cf0abSDavid Ahern 		if (ifm->ifi_index) {
2085905cf0abSDavid Ahern 			NL_SET_ERR_MSG(extack, "Filter by device index not supported for link dumps");
2086905cf0abSDavid Ahern 			return -EINVAL;
2087905cf0abSDavid Ahern 		}
2088905cf0abSDavid Ahern 
20898cb08174SJohannes Berg 		return nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb,
20908cb08174SJohannes Berg 						     IFLA_MAX, ifla_policy,
20918cb08174SJohannes Berg 						     extack);
2092905cf0abSDavid Ahern 	}
2093905cf0abSDavid Ahern 
2094905cf0abSDavid Ahern 	/* A hack to preserve kernel<->userspace interface.
2095905cf0abSDavid Ahern 	 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
2096905cf0abSDavid Ahern 	 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
2097905cf0abSDavid Ahern 	 * what iproute2 < v3.9.0 used.
2098905cf0abSDavid Ahern 	 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
2099905cf0abSDavid Ahern 	 * attribute, its netlink message is shorter than struct ifinfomsg.
2100905cf0abSDavid Ahern 	 */
2101905cf0abSDavid Ahern 	hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2102905cf0abSDavid Ahern 		 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2103905cf0abSDavid Ahern 
21048cb08174SJohannes Berg 	return nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy,
21058cb08174SJohannes Berg 				      extack);
2106905cf0abSDavid Ahern }
2107905cf0abSDavid Ahern 
2108b60c5115SThomas Graf static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
21091da177e4SLinus Torvalds {
2110905cf0abSDavid Ahern 	struct netlink_ext_ack *extack = cb->extack;
2111905cf0abSDavid Ahern 	const struct nlmsghdr *nlh = cb->nlh;
21123b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
211379e1ad14SJiri Benc 	struct net *tgt_net = net;
21147c28bd0bSEric Dumazet 	int h, s_h;
21157c28bd0bSEric Dumazet 	int idx = 0, s_idx;
21161da177e4SLinus Torvalds 	struct net_device *dev;
21177c28bd0bSEric Dumazet 	struct hlist_head *head;
2118115c9b81SGreg Rose 	struct nlattr *tb[IFLA_MAX+1];
2119115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
2120dc599f76SDavid Ahern 	const struct rtnl_link_ops *kind_ops = NULL;
2121dc599f76SDavid Ahern 	unsigned int flags = NLM_F_MULTI;
2122dc599f76SDavid Ahern 	int master_idx = 0;
212379e1ad14SJiri Benc 	int netnsid = -1;
2124905cf0abSDavid Ahern 	int err, i;
21251da177e4SLinus Torvalds 
21267c28bd0bSEric Dumazet 	s_h = cb->args[0];
21277c28bd0bSEric Dumazet 	s_idx = cb->args[1];
21287c28bd0bSEric Dumazet 
2129905cf0abSDavid Ahern 	err = rtnl_valid_dump_ifinfo_req(nlh, cb->strict_check, tb, extack);
2130905cf0abSDavid Ahern 	if (err < 0) {
2131905cf0abSDavid Ahern 		if (cb->strict_check)
2132905cf0abSDavid Ahern 			return err;
2133e5eca6d4SMichal Schmidt 
2134905cf0abSDavid Ahern 		goto walk_entries;
213579e1ad14SJiri Benc 	}
213679e1ad14SJiri Benc 
2137905cf0abSDavid Ahern 	for (i = 0; i <= IFLA_MAX; ++i) {
2138905cf0abSDavid Ahern 		if (!tb[i])
2139905cf0abSDavid Ahern 			continue;
2140dc599f76SDavid Ahern 
2141905cf0abSDavid Ahern 		/* new attributes should only be added with strict checking */
2142905cf0abSDavid Ahern 		switch (i) {
2143905cf0abSDavid Ahern 		case IFLA_TARGET_NETNSID:
2144905cf0abSDavid Ahern 			netnsid = nla_get_s32(tb[i]);
2145905cf0abSDavid Ahern 			tgt_net = rtnl_get_net_ns_capable(skb->sk, netnsid);
2146905cf0abSDavid Ahern 			if (IS_ERR(tgt_net)) {
2147905cf0abSDavid Ahern 				NL_SET_ERR_MSG(extack, "Invalid target network namespace id");
2148905cf0abSDavid Ahern 				return PTR_ERR(tgt_net);
2149905cf0abSDavid Ahern 			}
2150905cf0abSDavid Ahern 			break;
2151905cf0abSDavid Ahern 		case IFLA_EXT_MASK:
2152905cf0abSDavid Ahern 			ext_filter_mask = nla_get_u32(tb[i]);
2153905cf0abSDavid Ahern 			break;
2154905cf0abSDavid Ahern 		case IFLA_MASTER:
2155905cf0abSDavid Ahern 			master_idx = nla_get_u32(tb[i]);
2156905cf0abSDavid Ahern 			break;
2157905cf0abSDavid Ahern 		case IFLA_LINKINFO:
2158905cf0abSDavid Ahern 			kind_ops = linkinfo_to_kind_ops(tb[i]);
2159905cf0abSDavid Ahern 			break;
2160905cf0abSDavid Ahern 		default:
2161905cf0abSDavid Ahern 			if (cb->strict_check) {
2162905cf0abSDavid Ahern 				NL_SET_ERR_MSG(extack, "Unsupported attribute in link dump request");
2163905cf0abSDavid Ahern 				return -EINVAL;
2164905cf0abSDavid Ahern 			}
2165905cf0abSDavid Ahern 		}
2166905cf0abSDavid Ahern 	}
2167dc599f76SDavid Ahern 
2168dc599f76SDavid Ahern 	if (master_idx || kind_ops)
2169dc599f76SDavid Ahern 		flags |= NLM_F_DUMP_FILTERED;
2170115c9b81SGreg Rose 
2171905cf0abSDavid Ahern walk_entries:
21727c28bd0bSEric Dumazet 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
21737562f876SPavel Emelianov 		idx = 0;
217479e1ad14SJiri Benc 		head = &tgt_net->dev_index_head[h];
2175cac5e65eSEric Dumazet 		hlist_for_each_entry(dev, head, index_hlist) {
2176dc599f76SDavid Ahern 			if (link_dump_filtered(dev, master_idx, kind_ops))
21773f0ae05dSZhang Shengju 				goto cont;
21781da177e4SLinus Torvalds 			if (idx < s_idx)
21797562f876SPavel Emelianov 				goto cont;
218079e1ad14SJiri Benc 			err = rtnl_fill_ifinfo(skb, dev, net,
218179e1ad14SJiri Benc 					       RTM_NEWLINK,
218215e47304SEric W. Biederman 					       NETLINK_CB(cb->skb).portid,
2183905cf0abSDavid Ahern 					       nlh->nlmsg_seq, 0, flags,
218438e01b30SNicolas Dichtel 					       ext_filter_mask, 0, NULL, 0,
2185d4e4fdf9SGuillaume Nault 					       netnsid, GFP_KERNEL);
2186973462bbSDavid Gibson 
2187f6c5775fSDavid Ahern 			if (err < 0) {
2188f6c5775fSDavid Ahern 				if (likely(skb->len))
21897c28bd0bSEric Dumazet 					goto out;
21904e985adaSThomas Graf 
2191f6c5775fSDavid Ahern 				goto out_err;
2192f6c5775fSDavid Ahern 			}
21937562f876SPavel Emelianov cont:
21947562f876SPavel Emelianov 			idx++;
21951da177e4SLinus Torvalds 		}
21967c28bd0bSEric Dumazet 	}
21977c28bd0bSEric Dumazet out:
2198f6c5775fSDavid Ahern 	err = skb->len;
2199f6c5775fSDavid Ahern out_err:
22007c28bd0bSEric Dumazet 	cb->args[1] = idx;
22017c28bd0bSEric Dumazet 	cb->args[0] = h;
2202a9ecb0cbSzhang kai 	cb->seq = tgt_net->dev_base_seq;
2203d0225784SJakub Sitnicki 	nl_dump_check_consistent(cb, nlmsg_hdr(skb));
220479e1ad14SJiri Benc 	if (netnsid >= 0)
220579e1ad14SJiri Benc 		put_net(tgt_net);
22061da177e4SLinus Torvalds 
2207f6c5775fSDavid Ahern 	return err;
22081da177e4SLinus Torvalds }
22091da177e4SLinus Torvalds 
2210fceb6435SJohannes Berg int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len,
2211fceb6435SJohannes Berg 			struct netlink_ext_ack *exterr)
2212f7b12606SJiri Pirko {
22138cb08174SJohannes Berg 	return nla_parse_deprecated(tb, IFLA_MAX, head, len, ifla_policy,
22148cb08174SJohannes Berg 				    exterr);
2215f7b12606SJiri Pirko }
2216f7b12606SJiri Pirko EXPORT_SYMBOL(rtnl_nla_parse_ifla);
221757b61080SScott Feldman 
221881adee47SEric W. Biederman struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
221981adee47SEric W. Biederman {
222081adee47SEric W. Biederman 	struct net *net;
222181adee47SEric W. Biederman 	/* Examine the link attributes and figure out which
222281adee47SEric W. Biederman 	 * network namespace we are talking about.
222381adee47SEric W. Biederman 	 */
222481adee47SEric W. Biederman 	if (tb[IFLA_NET_NS_PID])
222581adee47SEric W. Biederman 		net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
2226f0630529SEric W. Biederman 	else if (tb[IFLA_NET_NS_FD])
2227f0630529SEric W. Biederman 		net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
222881adee47SEric W. Biederman 	else
222981adee47SEric W. Biederman 		net = get_net(src_net);
223081adee47SEric W. Biederman 	return net;
223181adee47SEric W. Biederman }
223281adee47SEric W. Biederman EXPORT_SYMBOL(rtnl_link_get_net);
223381adee47SEric W. Biederman 
22347c4f63baSChristian Brauner /* Figure out which network namespace we are talking about by
22357c4f63baSChristian Brauner  * examining the link attributes in the following order:
22367c4f63baSChristian Brauner  *
22377c4f63baSChristian Brauner  * 1. IFLA_NET_NS_PID
22387c4f63baSChristian Brauner  * 2. IFLA_NET_NS_FD
22397e4a8d5aSChristian Brauner  * 3. IFLA_TARGET_NETNSID
22407c4f63baSChristian Brauner  */
22417c4f63baSChristian Brauner static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net,
22427c4f63baSChristian Brauner 					       struct nlattr *tb[])
22437c4f63baSChristian Brauner {
22447c4f63baSChristian Brauner 	struct net *net;
22457c4f63baSChristian Brauner 
22467c4f63baSChristian Brauner 	if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])
22477c4f63baSChristian Brauner 		return rtnl_link_get_net(src_net, tb);
22487c4f63baSChristian Brauner 
22497e4a8d5aSChristian Brauner 	if (!tb[IFLA_TARGET_NETNSID])
22507c4f63baSChristian Brauner 		return get_net(src_net);
22517c4f63baSChristian Brauner 
22527e4a8d5aSChristian Brauner 	net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_TARGET_NETNSID]));
22537c4f63baSChristian Brauner 	if (!net)
22547c4f63baSChristian Brauner 		return ERR_PTR(-EINVAL);
22557c4f63baSChristian Brauner 
22567c4f63baSChristian Brauner 	return net;
22577c4f63baSChristian Brauner }
22587c4f63baSChristian Brauner 
22597c4f63baSChristian Brauner static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb,
22607c4f63baSChristian Brauner 					     struct net *src_net,
22617c4f63baSChristian Brauner 					     struct nlattr *tb[], int cap)
22627c4f63baSChristian Brauner {
22637c4f63baSChristian Brauner 	struct net *net;
22647c4f63baSChristian Brauner 
22657c4f63baSChristian Brauner 	net = rtnl_link_get_net_by_nlattr(src_net, tb);
22667c4f63baSChristian Brauner 	if (IS_ERR(net))
22677c4f63baSChristian Brauner 		return net;
22687c4f63baSChristian Brauner 
22697c4f63baSChristian Brauner 	if (!netlink_ns_capable(skb, net->user_ns, cap)) {
22707c4f63baSChristian Brauner 		put_net(net);
22717c4f63baSChristian Brauner 		return ERR_PTR(-EPERM);
22727c4f63baSChristian Brauner 	}
22737c4f63baSChristian Brauner 
22747c4f63baSChristian Brauner 	return net;
22757c4f63baSChristian Brauner }
22767c4f63baSChristian Brauner 
22774ff66caeSChristian Brauner /* Verify that rtnetlink requests do not pass additional properties
22784ff66caeSChristian Brauner  * potentially referring to different network namespaces.
22794ff66caeSChristian Brauner  */
22804ff66caeSChristian Brauner static int rtnl_ensure_unique_netns(struct nlattr *tb[],
22814ff66caeSChristian Brauner 				    struct netlink_ext_ack *extack,
22824ff66caeSChristian Brauner 				    bool netns_id_only)
22834ff66caeSChristian Brauner {
22844ff66caeSChristian Brauner 
22854ff66caeSChristian Brauner 	if (netns_id_only) {
22864ff66caeSChristian Brauner 		if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD])
22874ff66caeSChristian Brauner 			return 0;
22884ff66caeSChristian Brauner 
22894ff66caeSChristian Brauner 		NL_SET_ERR_MSG(extack, "specified netns attribute not supported");
22904ff66caeSChristian Brauner 		return -EOPNOTSUPP;
22914ff66caeSChristian Brauner 	}
22924ff66caeSChristian Brauner 
22937e4a8d5aSChristian Brauner 	if (tb[IFLA_TARGET_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]))
22944ff66caeSChristian Brauner 		goto invalid_attr;
22954ff66caeSChristian Brauner 
22967e4a8d5aSChristian Brauner 	if (tb[IFLA_NET_NS_PID] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_FD]))
22974ff66caeSChristian Brauner 		goto invalid_attr;
22984ff66caeSChristian Brauner 
22997e4a8d5aSChristian Brauner 	if (tb[IFLA_NET_NS_FD] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_PID]))
23004ff66caeSChristian Brauner 		goto invalid_attr;
23014ff66caeSChristian Brauner 
23024ff66caeSChristian Brauner 	return 0;
23034ff66caeSChristian Brauner 
23044ff66caeSChristian Brauner invalid_attr:
23054ff66caeSChristian Brauner 	NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified");
23064ff66caeSChristian Brauner 	return -EINVAL;
23074ff66caeSChristian Brauner }
23084ff66caeSChristian Brauner 
23098679c31eSRocco Yue static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[],
23108679c31eSRocco Yue 			    struct netlink_ext_ack *extack)
23111840bb13SThomas Graf {
23121840bb13SThomas Graf 	if (dev) {
23131840bb13SThomas Graf 		if (tb[IFLA_ADDRESS] &&
23141840bb13SThomas Graf 		    nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
23151840bb13SThomas Graf 			return -EINVAL;
23161840bb13SThomas Graf 
23171840bb13SThomas Graf 		if (tb[IFLA_BROADCAST] &&
23181840bb13SThomas Graf 		    nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
23191840bb13SThomas Graf 			return -EINVAL;
23201840bb13SThomas Graf 	}
23211840bb13SThomas Graf 
2322cf7afbfeSThomas Graf 	if (tb[IFLA_AF_SPEC]) {
2323cf7afbfeSThomas Graf 		struct nlattr *af;
2324cf7afbfeSThomas Graf 		int rem, err;
2325cf7afbfeSThomas Graf 
2326cf7afbfeSThomas Graf 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2327cf7afbfeSThomas Graf 			const struct rtnl_af_ops *af_ops;
2328cf7afbfeSThomas Graf 
23295fa85a09SFlorian Westphal 			af_ops = rtnl_af_lookup(nla_type(af));
2330a100243dSCong Wang 			if (!af_ops)
2331cf7afbfeSThomas Graf 				return -EAFNOSUPPORT;
2332cf7afbfeSThomas Graf 
2333a100243dSCong Wang 			if (!af_ops->set_link_af)
2334cf7afbfeSThomas Graf 				return -EOPNOTSUPP;
2335cf7afbfeSThomas Graf 
2336cf7afbfeSThomas Graf 			if (af_ops->validate_link_af) {
23378679c31eSRocco Yue 				err = af_ops->validate_link_af(dev, af, extack);
2338a100243dSCong Wang 				if (err < 0)
2339cf7afbfeSThomas Graf 					return err;
2340cf7afbfeSThomas Graf 			}
2341cf7afbfeSThomas Graf 		}
2342cf7afbfeSThomas Graf 	}
2343cf7afbfeSThomas Graf 
2344eac1b93cSCoco Li 	if (tb[IFLA_GRO_MAX_SIZE]) {
2345eac1b93cSCoco Li 		u32 gro_max_size = nla_get_u32(tb[IFLA_GRO_MAX_SIZE]);
2346eac1b93cSCoco Li 
2347eac1b93cSCoco Li 		if (gro_max_size > GRO_MAX_SIZE) {
2348eac1b93cSCoco Li 			NL_SET_ERR_MSG(extack, "too big gro_max_size");
2349eac1b93cSCoco Li 			return -EINVAL;
2350eac1b93cSCoco Li 		}
2351eac1b93cSCoco Li 	}
23521840bb13SThomas Graf 	return 0;
23531840bb13SThomas Graf }
23541840bb13SThomas Graf 
2355cc8e27ccSEli Cohen static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
2356cc8e27ccSEli Cohen 				  int guid_type)
2357cc8e27ccSEli Cohen {
2358cc8e27ccSEli Cohen 	const struct net_device_ops *ops = dev->netdev_ops;
2359cc8e27ccSEli Cohen 
2360cc8e27ccSEli Cohen 	return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
2361cc8e27ccSEli Cohen }
2362cc8e27ccSEli Cohen 
2363cc8e27ccSEli Cohen static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
2364cc8e27ccSEli Cohen {
2365cc8e27ccSEli Cohen 	if (dev->type != ARPHRD_INFINIBAND)
2366cc8e27ccSEli Cohen 		return -EOPNOTSUPP;
2367cc8e27ccSEli Cohen 
2368cc8e27ccSEli Cohen 	return handle_infiniband_guid(dev, ivt, guid_type);
2369cc8e27ccSEli Cohen }
2370cc8e27ccSEli Cohen 
23714f7d2cdfSDaniel Borkmann static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
2372c02db8c6SChris Wright {
2373c02db8c6SChris Wright 	const struct net_device_ops *ops = dev->netdev_ops;
23744f7d2cdfSDaniel Borkmann 	int err = -EINVAL;
2375c02db8c6SChris Wright 
23764f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_MAC]) {
23774f7d2cdfSDaniel Borkmann 		struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
23784f7d2cdfSDaniel Borkmann 
2379ff08ddbaSDan Carpenter 		if (ivm->vf >= INT_MAX)
2380ff08ddbaSDan Carpenter 			return -EINVAL;
2381c02db8c6SChris Wright 		err = -EOPNOTSUPP;
2382c02db8c6SChris Wright 		if (ops->ndo_set_vf_mac)
2383c02db8c6SChris Wright 			err = ops->ndo_set_vf_mac(dev, ivm->vf,
2384c02db8c6SChris Wright 						  ivm->mac);
23854f7d2cdfSDaniel Borkmann 		if (err < 0)
23864f7d2cdfSDaniel Borkmann 			return err;
2387c02db8c6SChris Wright 	}
23884f7d2cdfSDaniel Borkmann 
23894f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_VLAN]) {
23904f7d2cdfSDaniel Borkmann 		struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
23914f7d2cdfSDaniel Borkmann 
2392ff08ddbaSDan Carpenter 		if (ivv->vf >= INT_MAX)
2393ff08ddbaSDan Carpenter 			return -EINVAL;
2394c02db8c6SChris Wright 		err = -EOPNOTSUPP;
2395c02db8c6SChris Wright 		if (ops->ndo_set_vf_vlan)
23964f7d2cdfSDaniel Borkmann 			err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
239779aab093SMoshe Shemesh 						   ivv->qos,
239879aab093SMoshe Shemesh 						   htons(ETH_P_8021Q));
239979aab093SMoshe Shemesh 		if (err < 0)
240079aab093SMoshe Shemesh 			return err;
240179aab093SMoshe Shemesh 	}
240279aab093SMoshe Shemesh 
240379aab093SMoshe Shemesh 	if (tb[IFLA_VF_VLAN_LIST]) {
240479aab093SMoshe Shemesh 		struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
240579aab093SMoshe Shemesh 		struct nlattr *attr;
240679aab093SMoshe Shemesh 		int rem, len = 0;
240779aab093SMoshe Shemesh 
240879aab093SMoshe Shemesh 		err = -EOPNOTSUPP;
240979aab093SMoshe Shemesh 		if (!ops->ndo_set_vf_vlan)
241079aab093SMoshe Shemesh 			return err;
241179aab093SMoshe Shemesh 
241279aab093SMoshe Shemesh 		nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
241379aab093SMoshe Shemesh 			if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
241479aab093SMoshe Shemesh 			    nla_len(attr) < NLA_HDRLEN) {
241579aab093SMoshe Shemesh 				return -EINVAL;
241679aab093SMoshe Shemesh 			}
241779aab093SMoshe Shemesh 			if (len >= MAX_VLAN_LIST_LEN)
241879aab093SMoshe Shemesh 				return -EOPNOTSUPP;
241979aab093SMoshe Shemesh 			ivvl[len] = nla_data(attr);
242079aab093SMoshe Shemesh 
242179aab093SMoshe Shemesh 			len++;
242279aab093SMoshe Shemesh 		}
2423fa34cd94SArnd Bergmann 		if (len == 0)
2424fa34cd94SArnd Bergmann 			return -EINVAL;
2425fa34cd94SArnd Bergmann 
2426ff08ddbaSDan Carpenter 		if (ivvl[0]->vf >= INT_MAX)
2427ff08ddbaSDan Carpenter 			return -EINVAL;
242879aab093SMoshe Shemesh 		err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
242979aab093SMoshe Shemesh 					   ivvl[0]->qos, ivvl[0]->vlan_proto);
24304f7d2cdfSDaniel Borkmann 		if (err < 0)
24314f7d2cdfSDaniel Borkmann 			return err;
2432c02db8c6SChris Wright 	}
24334f7d2cdfSDaniel Borkmann 
24344f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_TX_RATE]) {
24354f7d2cdfSDaniel Borkmann 		struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
2436ed616689SSucheta Chakraborty 		struct ifla_vf_info ivf;
24374f7d2cdfSDaniel Borkmann 
2438ff08ddbaSDan Carpenter 		if (ivt->vf >= INT_MAX)
2439ff08ddbaSDan Carpenter 			return -EINVAL;
2440c02db8c6SChris Wright 		err = -EOPNOTSUPP;
2441ed616689SSucheta Chakraborty 		if (ops->ndo_get_vf_config)
24424f7d2cdfSDaniel Borkmann 			err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
24434f7d2cdfSDaniel Borkmann 		if (err < 0)
24444f7d2cdfSDaniel Borkmann 			return err;
24454f7d2cdfSDaniel Borkmann 
2446ed616689SSucheta Chakraborty 		err = -EOPNOTSUPP;
2447ed616689SSucheta Chakraborty 		if (ops->ndo_set_vf_rate)
2448ed616689SSucheta Chakraborty 			err = ops->ndo_set_vf_rate(dev, ivt->vf,
2449ed616689SSucheta Chakraborty 						   ivf.min_tx_rate,
2450c02db8c6SChris Wright 						   ivt->rate);
24514f7d2cdfSDaniel Borkmann 		if (err < 0)
24524f7d2cdfSDaniel Borkmann 			return err;
2453c02db8c6SChris Wright 	}
24544f7d2cdfSDaniel Borkmann 
24554f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_RATE]) {
24564f7d2cdfSDaniel Borkmann 		struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
24574f7d2cdfSDaniel Borkmann 
2458ff08ddbaSDan Carpenter 		if (ivt->vf >= INT_MAX)
2459ff08ddbaSDan Carpenter 			return -EINVAL;
2460ed616689SSucheta Chakraborty 		err = -EOPNOTSUPP;
2461ed616689SSucheta Chakraborty 		if (ops->ndo_set_vf_rate)
2462ed616689SSucheta Chakraborty 			err = ops->ndo_set_vf_rate(dev, ivt->vf,
2463ed616689SSucheta Chakraborty 						   ivt->min_tx_rate,
2464ed616689SSucheta Chakraborty 						   ivt->max_tx_rate);
24654f7d2cdfSDaniel Borkmann 		if (err < 0)
24664f7d2cdfSDaniel Borkmann 			return err;
2467ed616689SSucheta Chakraborty 	}
24684f7d2cdfSDaniel Borkmann 
24694f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_SPOOFCHK]) {
24704f7d2cdfSDaniel Borkmann 		struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
24714f7d2cdfSDaniel Borkmann 
2472ff08ddbaSDan Carpenter 		if (ivs->vf >= INT_MAX)
2473ff08ddbaSDan Carpenter 			return -EINVAL;
24745f8444a3SGreg Rose 		err = -EOPNOTSUPP;
24755f8444a3SGreg Rose 		if (ops->ndo_set_vf_spoofchk)
24765f8444a3SGreg Rose 			err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
24775f8444a3SGreg Rose 						       ivs->setting);
24784f7d2cdfSDaniel Borkmann 		if (err < 0)
24794f7d2cdfSDaniel Borkmann 			return err;
24805f8444a3SGreg Rose 	}
24814f7d2cdfSDaniel Borkmann 
24824f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_LINK_STATE]) {
24834f7d2cdfSDaniel Borkmann 		struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
24844f7d2cdfSDaniel Borkmann 
2485ff08ddbaSDan Carpenter 		if (ivl->vf >= INT_MAX)
2486ff08ddbaSDan Carpenter 			return -EINVAL;
24871d8faf48SRony Efraim 		err = -EOPNOTSUPP;
24881d8faf48SRony Efraim 		if (ops->ndo_set_vf_link_state)
24891d8faf48SRony Efraim 			err = ops->ndo_set_vf_link_state(dev, ivl->vf,
24901d8faf48SRony Efraim 							 ivl->link_state);
24914f7d2cdfSDaniel Borkmann 		if (err < 0)
24924f7d2cdfSDaniel Borkmann 			return err;
24931d8faf48SRony Efraim 	}
24944f7d2cdfSDaniel Borkmann 
24954f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_RSS_QUERY_EN]) {
249601a3d796SVlad Zolotarov 		struct ifla_vf_rss_query_en *ivrssq_en;
249701a3d796SVlad Zolotarov 
249801a3d796SVlad Zolotarov 		err = -EOPNOTSUPP;
24994f7d2cdfSDaniel Borkmann 		ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
2500ff08ddbaSDan Carpenter 		if (ivrssq_en->vf >= INT_MAX)
2501ff08ddbaSDan Carpenter 			return -EINVAL;
250201a3d796SVlad Zolotarov 		if (ops->ndo_set_vf_rss_query_en)
25034f7d2cdfSDaniel Borkmann 			err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
250401a3d796SVlad Zolotarov 							   ivrssq_en->setting);
25054f7d2cdfSDaniel Borkmann 		if (err < 0)
25064f7d2cdfSDaniel Borkmann 			return err;
250701a3d796SVlad Zolotarov 	}
25084f7d2cdfSDaniel Borkmann 
2509dd461d6aSHiroshi Shimamoto 	if (tb[IFLA_VF_TRUST]) {
2510dd461d6aSHiroshi Shimamoto 		struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
2511dd461d6aSHiroshi Shimamoto 
2512ff08ddbaSDan Carpenter 		if (ivt->vf >= INT_MAX)
2513ff08ddbaSDan Carpenter 			return -EINVAL;
2514dd461d6aSHiroshi Shimamoto 		err = -EOPNOTSUPP;
2515dd461d6aSHiroshi Shimamoto 		if (ops->ndo_set_vf_trust)
2516dd461d6aSHiroshi Shimamoto 			err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
2517dd461d6aSHiroshi Shimamoto 		if (err < 0)
2518dd461d6aSHiroshi Shimamoto 			return err;
2519dd461d6aSHiroshi Shimamoto 	}
2520dd461d6aSHiroshi Shimamoto 
2521cc8e27ccSEli Cohen 	if (tb[IFLA_VF_IB_NODE_GUID]) {
2522cc8e27ccSEli Cohen 		struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
2523cc8e27ccSEli Cohen 
2524ff08ddbaSDan Carpenter 		if (ivt->vf >= INT_MAX)
2525ff08ddbaSDan Carpenter 			return -EINVAL;
2526cc8e27ccSEli Cohen 		if (!ops->ndo_set_vf_guid)
2527cc8e27ccSEli Cohen 			return -EOPNOTSUPP;
2528cc8e27ccSEli Cohen 		return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
2529cc8e27ccSEli Cohen 	}
2530cc8e27ccSEli Cohen 
2531cc8e27ccSEli Cohen 	if (tb[IFLA_VF_IB_PORT_GUID]) {
2532cc8e27ccSEli Cohen 		struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
2533cc8e27ccSEli Cohen 
2534ff08ddbaSDan Carpenter 		if (ivt->vf >= INT_MAX)
2535ff08ddbaSDan Carpenter 			return -EINVAL;
2536cc8e27ccSEli Cohen 		if (!ops->ndo_set_vf_guid)
2537cc8e27ccSEli Cohen 			return -EOPNOTSUPP;
2538cc8e27ccSEli Cohen 
2539cc8e27ccSEli Cohen 		return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
2540cc8e27ccSEli Cohen 	}
2541cc8e27ccSEli Cohen 
2542c02db8c6SChris Wright 	return err;
2543c02db8c6SChris Wright }
2544c02db8c6SChris Wright 
254533eaf2a6SDavid Ahern static int do_set_master(struct net_device *dev, int ifindex,
254633eaf2a6SDavid Ahern 			 struct netlink_ext_ack *extack)
2547fbaec0eaSJiri Pirko {
2548898e5061SJiri Pirko 	struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
2549fbaec0eaSJiri Pirko 	const struct net_device_ops *ops;
2550fbaec0eaSJiri Pirko 	int err;
2551fbaec0eaSJiri Pirko 
2552898e5061SJiri Pirko 	if (upper_dev) {
2553898e5061SJiri Pirko 		if (upper_dev->ifindex == ifindex)
2554fbaec0eaSJiri Pirko 			return 0;
2555898e5061SJiri Pirko 		ops = upper_dev->netdev_ops;
2556fbaec0eaSJiri Pirko 		if (ops->ndo_del_slave) {
2557898e5061SJiri Pirko 			err = ops->ndo_del_slave(upper_dev, dev);
2558fbaec0eaSJiri Pirko 			if (err)
2559fbaec0eaSJiri Pirko 				return err;
2560fbaec0eaSJiri Pirko 		} else {
2561fbaec0eaSJiri Pirko 			return -EOPNOTSUPP;
2562fbaec0eaSJiri Pirko 		}
2563fbaec0eaSJiri Pirko 	}
2564fbaec0eaSJiri Pirko 
2565fbaec0eaSJiri Pirko 	if (ifindex) {
2566898e5061SJiri Pirko 		upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
2567898e5061SJiri Pirko 		if (!upper_dev)
2568fbaec0eaSJiri Pirko 			return -EINVAL;
2569898e5061SJiri Pirko 		ops = upper_dev->netdev_ops;
2570fbaec0eaSJiri Pirko 		if (ops->ndo_add_slave) {
257133eaf2a6SDavid Ahern 			err = ops->ndo_add_slave(upper_dev, dev, extack);
2572fbaec0eaSJiri Pirko 			if (err)
2573fbaec0eaSJiri Pirko 				return err;
2574fbaec0eaSJiri Pirko 		} else {
2575fbaec0eaSJiri Pirko 			return -EOPNOTSUPP;
2576fbaec0eaSJiri Pirko 		}
2577fbaec0eaSJiri Pirko 	}
2578fbaec0eaSJiri Pirko 	return 0;
2579fbaec0eaSJiri Pirko }
2580fbaec0eaSJiri Pirko 
2581829eb208SRoopa Prabhu static const struct nla_policy ifla_proto_down_reason_policy[IFLA_PROTO_DOWN_REASON_VALUE + 1] = {
2582829eb208SRoopa Prabhu 	[IFLA_PROTO_DOWN_REASON_MASK]	= { .type = NLA_U32 },
2583829eb208SRoopa Prabhu 	[IFLA_PROTO_DOWN_REASON_VALUE]	= { .type = NLA_U32 },
2584829eb208SRoopa Prabhu };
2585829eb208SRoopa Prabhu 
2586829eb208SRoopa Prabhu static int do_set_proto_down(struct net_device *dev,
2587829eb208SRoopa Prabhu 			     struct nlattr *nl_proto_down,
2588829eb208SRoopa Prabhu 			     struct nlattr *nl_proto_down_reason,
2589829eb208SRoopa Prabhu 			     struct netlink_ext_ack *extack)
2590829eb208SRoopa Prabhu {
2591829eb208SRoopa Prabhu 	struct nlattr *pdreason[IFLA_PROTO_DOWN_REASON_MAX + 1];
2592829eb208SRoopa Prabhu 	unsigned long mask = 0;
2593829eb208SRoopa Prabhu 	u32 value;
2594829eb208SRoopa Prabhu 	bool proto_down;
2595829eb208SRoopa Prabhu 	int err;
2596829eb208SRoopa Prabhu 
25972106efdaSJakub Kicinski 	if (!(dev->priv_flags & IFF_CHANGE_PROTO_DOWN)) {
2598829eb208SRoopa Prabhu 		NL_SET_ERR_MSG(extack,  "Protodown not supported by device");
2599829eb208SRoopa Prabhu 		return -EOPNOTSUPP;
2600829eb208SRoopa Prabhu 	}
2601829eb208SRoopa Prabhu 
2602829eb208SRoopa Prabhu 	if (nl_proto_down_reason) {
2603829eb208SRoopa Prabhu 		err = nla_parse_nested_deprecated(pdreason,
2604829eb208SRoopa Prabhu 						  IFLA_PROTO_DOWN_REASON_MAX,
2605829eb208SRoopa Prabhu 						  nl_proto_down_reason,
2606829eb208SRoopa Prabhu 						  ifla_proto_down_reason_policy,
2607829eb208SRoopa Prabhu 						  NULL);
2608829eb208SRoopa Prabhu 		if (err < 0)
2609829eb208SRoopa Prabhu 			return err;
2610829eb208SRoopa Prabhu 
2611829eb208SRoopa Prabhu 		if (!pdreason[IFLA_PROTO_DOWN_REASON_VALUE]) {
2612829eb208SRoopa Prabhu 			NL_SET_ERR_MSG(extack, "Invalid protodown reason value");
2613829eb208SRoopa Prabhu 			return -EINVAL;
2614829eb208SRoopa Prabhu 		}
2615829eb208SRoopa Prabhu 
2616829eb208SRoopa Prabhu 		value = nla_get_u32(pdreason[IFLA_PROTO_DOWN_REASON_VALUE]);
2617829eb208SRoopa Prabhu 
2618829eb208SRoopa Prabhu 		if (pdreason[IFLA_PROTO_DOWN_REASON_MASK])
2619829eb208SRoopa Prabhu 			mask = nla_get_u32(pdreason[IFLA_PROTO_DOWN_REASON_MASK]);
2620829eb208SRoopa Prabhu 
2621829eb208SRoopa Prabhu 		dev_change_proto_down_reason(dev, mask, value);
2622829eb208SRoopa Prabhu 	}
2623829eb208SRoopa Prabhu 
2624829eb208SRoopa Prabhu 	if (nl_proto_down) {
2625829eb208SRoopa Prabhu 		proto_down = nla_get_u8(nl_proto_down);
2626829eb208SRoopa Prabhu 
2627d467d0bcSZheng Yongjun 		/* Don't turn off protodown if there are active reasons */
2628829eb208SRoopa Prabhu 		if (!proto_down && dev->proto_down_reason) {
2629829eb208SRoopa Prabhu 			NL_SET_ERR_MSG(extack, "Cannot clear protodown, active reasons");
2630829eb208SRoopa Prabhu 			return -EBUSY;
2631829eb208SRoopa Prabhu 		}
2632829eb208SRoopa Prabhu 		err = dev_change_proto_down(dev,
2633829eb208SRoopa Prabhu 					    proto_down);
2634829eb208SRoopa Prabhu 		if (err)
2635829eb208SRoopa Prabhu 			return err;
2636829eb208SRoopa Prabhu 	}
2637829eb208SRoopa Prabhu 
2638829eb208SRoopa Prabhu 	return 0;
2639829eb208SRoopa Prabhu }
2640829eb208SRoopa Prabhu 
264190c325e3SNicolas Dichtel #define DO_SETLINK_MODIFIED	0x01
2642ba998906SNicolas Dichtel /* notify flag means notify + modified. */
2643ba998906SNicolas Dichtel #define DO_SETLINK_NOTIFY	0x03
264490f62cf3SEric W. Biederman static int do_setlink(const struct sk_buff *skb,
264590f62cf3SEric W. Biederman 		      struct net_device *dev, struct ifinfomsg *ifm,
2646ddf9f970SJakub Kicinski 		      struct netlink_ext_ack *extack,
26475ea08b52SFlorent Fourcot 		      struct nlattr **tb, int status)
26480157f60cSPatrick McHardy {
2649d314774cSStephen Hemminger 	const struct net_device_ops *ops = dev->netdev_ops;
26505ea08b52SFlorent Fourcot 	char ifname[IFNAMSIZ];
26510157f60cSPatrick McHardy 	int err;
26520157f60cSPatrick McHardy 
26538679c31eSRocco Yue 	err = validate_linkmsg(dev, tb, extack);
2654644c7eebSEric Dumazet 	if (err < 0)
2655644c7eebSEric Dumazet 		return err;
2656644c7eebSEric Dumazet 
26575ea08b52SFlorent Fourcot 	if (tb[IFLA_IFNAME])
26585ea08b52SFlorent Fourcot 		nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
26595ea08b52SFlorent Fourcot 	else
26605ea08b52SFlorent Fourcot 		ifname[0] = '\0';
26615ea08b52SFlorent Fourcot 
26627e4a8d5aSChristian Brauner 	if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_TARGET_NETNSID]) {
26635ea08b52SFlorent Fourcot 		const char *pat = ifname[0] ? ifname : NULL;
2664eeb85a14SAndrei Vagin 		struct net *net;
2665eeb85a14SAndrei Vagin 		int new_ifindex;
2666eeb85a14SAndrei Vagin 
2667eeb85a14SAndrei Vagin 		net = rtnl_link_get_net_capable(skb, dev_net(dev),
26687c4f63baSChristian Brauner 						tb, CAP_NET_ADMIN);
2669d8a5ec67SEric W. Biederman 		if (IS_ERR(net)) {
2670d8a5ec67SEric W. Biederman 			err = PTR_ERR(net);
2671d8a5ec67SEric W. Biederman 			goto errout;
2672d8a5ec67SEric W. Biederman 		}
26737c4f63baSChristian Brauner 
2674eeb85a14SAndrei Vagin 		if (tb[IFLA_NEW_IFINDEX])
2675eeb85a14SAndrei Vagin 			new_ifindex = nla_get_s32(tb[IFLA_NEW_IFINDEX]);
2676eeb85a14SAndrei Vagin 		else
2677eeb85a14SAndrei Vagin 			new_ifindex = 0;
2678eeb85a14SAndrei Vagin 
267996a6b93bSAndrey Ignatov 		err = __dev_change_net_namespace(dev, net, pat, new_ifindex);
2680d8a5ec67SEric W. Biederman 		put_net(net);
2681d8a5ec67SEric W. Biederman 		if (err)
2682d8a5ec67SEric W. Biederman 			goto errout;
268390c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
2684d8a5ec67SEric W. Biederman 	}
2685d8a5ec67SEric W. Biederman 
26860157f60cSPatrick McHardy 	if (tb[IFLA_MAP]) {
26870157f60cSPatrick McHardy 		struct rtnl_link_ifmap *u_map;
26880157f60cSPatrick McHardy 		struct ifmap k_map;
26890157f60cSPatrick McHardy 
2690d314774cSStephen Hemminger 		if (!ops->ndo_set_config) {
26910157f60cSPatrick McHardy 			err = -EOPNOTSUPP;
26920157f60cSPatrick McHardy 			goto errout;
26930157f60cSPatrick McHardy 		}
26940157f60cSPatrick McHardy 
26950157f60cSPatrick McHardy 		if (!netif_device_present(dev)) {
26960157f60cSPatrick McHardy 			err = -ENODEV;
26970157f60cSPatrick McHardy 			goto errout;
26980157f60cSPatrick McHardy 		}
26990157f60cSPatrick McHardy 
27000157f60cSPatrick McHardy 		u_map = nla_data(tb[IFLA_MAP]);
27010157f60cSPatrick McHardy 		k_map.mem_start = (unsigned long) u_map->mem_start;
27020157f60cSPatrick McHardy 		k_map.mem_end = (unsigned long) u_map->mem_end;
27030157f60cSPatrick McHardy 		k_map.base_addr = (unsigned short) u_map->base_addr;
27040157f60cSPatrick McHardy 		k_map.irq = (unsigned char) u_map->irq;
27050157f60cSPatrick McHardy 		k_map.dma = (unsigned char) u_map->dma;
27060157f60cSPatrick McHardy 		k_map.port = (unsigned char) u_map->port;
27070157f60cSPatrick McHardy 
2708d314774cSStephen Hemminger 		err = ops->ndo_set_config(dev, &k_map);
27090157f60cSPatrick McHardy 		if (err < 0)
27100157f60cSPatrick McHardy 			goto errout;
27110157f60cSPatrick McHardy 
2712ba998906SNicolas Dichtel 		status |= DO_SETLINK_NOTIFY;
27130157f60cSPatrick McHardy 	}
27140157f60cSPatrick McHardy 
27150157f60cSPatrick McHardy 	if (tb[IFLA_ADDRESS]) {
27160157f60cSPatrick McHardy 		struct sockaddr *sa;
27170157f60cSPatrick McHardy 		int len;
27180157f60cSPatrick McHardy 
2719153711f9SWANG Cong 		len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
2720153711f9SWANG Cong 						  sizeof(*sa));
27210157f60cSPatrick McHardy 		sa = kmalloc(len, GFP_KERNEL);
27220157f60cSPatrick McHardy 		if (!sa) {
27230157f60cSPatrick McHardy 			err = -ENOMEM;
27240157f60cSPatrick McHardy 			goto errout;
27250157f60cSPatrick McHardy 		}
27260157f60cSPatrick McHardy 		sa->sa_family = dev->type;
27270157f60cSPatrick McHardy 		memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
27280157f60cSPatrick McHardy 		       dev->addr_len);
27293b23a32aSCong Wang 		err = dev_set_mac_address_user(dev, sa, extack);
27300157f60cSPatrick McHardy 		kfree(sa);
27310157f60cSPatrick McHardy 		if (err)
27320157f60cSPatrick McHardy 			goto errout;
273390c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
27340157f60cSPatrick McHardy 	}
27350157f60cSPatrick McHardy 
27360157f60cSPatrick McHardy 	if (tb[IFLA_MTU]) {
27377a4c53beSStephen Hemminger 		err = dev_set_mtu_ext(dev, nla_get_u32(tb[IFLA_MTU]), extack);
27380157f60cSPatrick McHardy 		if (err < 0)
27390157f60cSPatrick McHardy 			goto errout;
274090c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
27410157f60cSPatrick McHardy 	}
27420157f60cSPatrick McHardy 
2743cbda10faSVlad Dogaru 	if (tb[IFLA_GROUP]) {
2744cbda10faSVlad Dogaru 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
2745ba998906SNicolas Dichtel 		status |= DO_SETLINK_NOTIFY;
2746cbda10faSVlad Dogaru 	}
2747cbda10faSVlad Dogaru 
27480157f60cSPatrick McHardy 	/*
27490157f60cSPatrick McHardy 	 * Interface selected by interface index but interface
27500157f60cSPatrick McHardy 	 * name provided implies that a name change has been
27510157f60cSPatrick McHardy 	 * requested.
27520157f60cSPatrick McHardy 	 */
27530157f60cSPatrick McHardy 	if (ifm->ifi_index > 0 && ifname[0]) {
27540157f60cSPatrick McHardy 		err = dev_change_name(dev, ifname);
27550157f60cSPatrick McHardy 		if (err < 0)
27560157f60cSPatrick McHardy 			goto errout;
275790c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
27580157f60cSPatrick McHardy 	}
27590157f60cSPatrick McHardy 
27600b815a1aSStephen Hemminger 	if (tb[IFLA_IFALIAS]) {
27610b815a1aSStephen Hemminger 		err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
27620b815a1aSStephen Hemminger 				    nla_len(tb[IFLA_IFALIAS]));
27630b815a1aSStephen Hemminger 		if (err < 0)
27640b815a1aSStephen Hemminger 			goto errout;
2765ba998906SNicolas Dichtel 		status |= DO_SETLINK_NOTIFY;
27660b815a1aSStephen Hemminger 	}
27670b815a1aSStephen Hemminger 
27680157f60cSPatrick McHardy 	if (tb[IFLA_BROADCAST]) {
27690157f60cSPatrick McHardy 		nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
2770e7c3273eSJiri Pirko 		call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
27710157f60cSPatrick McHardy 	}
27720157f60cSPatrick McHardy 
27730157f60cSPatrick McHardy 	if (ifm->ifi_flags || ifm->ifi_change) {
2774567c5e13SPetr Machata 		err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
2775567c5e13SPetr Machata 				       extack);
27765f9021cfSJohannes Berg 		if (err < 0)
27775f9021cfSJohannes Berg 			goto errout;
27780157f60cSPatrick McHardy 	}
27790157f60cSPatrick McHardy 
2780fbaec0eaSJiri Pirko 	if (tb[IFLA_MASTER]) {
278133eaf2a6SDavid Ahern 		err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
2782fbaec0eaSJiri Pirko 		if (err)
2783fbaec0eaSJiri Pirko 			goto errout;
278490c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
2785fbaec0eaSJiri Pirko 	}
2786fbaec0eaSJiri Pirko 
27879a57247fSJiri Pirko 	if (tb[IFLA_CARRIER]) {
27889a57247fSJiri Pirko 		err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
27899a57247fSJiri Pirko 		if (err)
27909a57247fSJiri Pirko 			goto errout;
279190c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
27929a57247fSJiri Pirko 	}
27939a57247fSJiri Pirko 
27945d1180fcSNicolas Dichtel 	if (tb[IFLA_TXQLEN]) {
27950cd29503SAlexey Dobriyan 		unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]);
27965d1180fcSNicolas Dichtel 
27976a643ddbSCong Wang 		err = dev_change_tx_queue_len(dev, value);
27986a643ddbSCong Wang 		if (err)
279908294a26SJason Wang 			goto errout;
28002d7f669bSXin Long 		status |= DO_SETLINK_MODIFIED;
280108294a26SJason Wang 	}
28020157f60cSPatrick McHardy 
280346e6b992SStephen Hemminger 	if (tb[IFLA_GSO_MAX_SIZE]) {
280446e6b992SStephen Hemminger 		u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]);
280546e6b992SStephen Hemminger 
280646e6b992SStephen Hemminger 		if (max_size > GSO_MAX_SIZE) {
280746e6b992SStephen Hemminger 			err = -EINVAL;
280846e6b992SStephen Hemminger 			goto errout;
280946e6b992SStephen Hemminger 		}
281046e6b992SStephen Hemminger 
281146e6b992SStephen Hemminger 		if (dev->gso_max_size ^ max_size) {
281246e6b992SStephen Hemminger 			netif_set_gso_max_size(dev, max_size);
281346e6b992SStephen Hemminger 			status |= DO_SETLINK_MODIFIED;
281446e6b992SStephen Hemminger 		}
281546e6b992SStephen Hemminger 	}
281646e6b992SStephen Hemminger 
281746e6b992SStephen Hemminger 	if (tb[IFLA_GSO_MAX_SEGS]) {
281846e6b992SStephen Hemminger 		u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
281946e6b992SStephen Hemminger 
282046e6b992SStephen Hemminger 		if (max_segs > GSO_MAX_SEGS) {
282146e6b992SStephen Hemminger 			err = -EINVAL;
282246e6b992SStephen Hemminger 			goto errout;
282346e6b992SStephen Hemminger 		}
282446e6b992SStephen Hemminger 
282546e6b992SStephen Hemminger 		if (dev->gso_max_segs ^ max_segs) {
28266d872df3SEric Dumazet 			netif_set_gso_max_segs(dev, max_segs);
282746e6b992SStephen Hemminger 			status |= DO_SETLINK_MODIFIED;
282846e6b992SStephen Hemminger 		}
282946e6b992SStephen Hemminger 	}
283046e6b992SStephen Hemminger 
2831eac1b93cSCoco Li 	if (tb[IFLA_GRO_MAX_SIZE]) {
2832eac1b93cSCoco Li 		u32 gro_max_size = nla_get_u32(tb[IFLA_GRO_MAX_SIZE]);
2833eac1b93cSCoco Li 
2834eac1b93cSCoco Li 		if (dev->gro_max_size ^ gro_max_size) {
2835eac1b93cSCoco Li 			netif_set_gro_max_size(dev, gro_max_size);
2836eac1b93cSCoco Li 			status |= DO_SETLINK_MODIFIED;
2837eac1b93cSCoco Li 		}
2838eac1b93cSCoco Li 	}
2839eac1b93cSCoco Li 
28400157f60cSPatrick McHardy 	if (tb[IFLA_OPERSTATE])
284193b2d4a2SDavid S. Miller 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
28420157f60cSPatrick McHardy 
28430157f60cSPatrick McHardy 	if (tb[IFLA_LINKMODE]) {
28441889b0e7SNicolas Dichtel 		unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
28451889b0e7SNicolas Dichtel 
2846fd888e85SSebastian Andrzej Siewior 		write_lock(&dev_base_lock);
28471889b0e7SNicolas Dichtel 		if (dev->link_mode ^ value)
2848ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
28491889b0e7SNicolas Dichtel 		dev->link_mode = value;
2850fd888e85SSebastian Andrzej Siewior 		write_unlock(&dev_base_lock);
28510157f60cSPatrick McHardy 	}
28520157f60cSPatrick McHardy 
2853c02db8c6SChris Wright 	if (tb[IFLA_VFINFO_LIST]) {
28544f7d2cdfSDaniel Borkmann 		struct nlattr *vfinfo[IFLA_VF_MAX + 1];
2855c02db8c6SChris Wright 		struct nlattr *attr;
2856c02db8c6SChris Wright 		int rem;
28574f7d2cdfSDaniel Borkmann 
2858c02db8c6SChris Wright 		nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
28594f7d2cdfSDaniel Borkmann 			if (nla_type(attr) != IFLA_VF_INFO ||
28604f7d2cdfSDaniel Borkmann 			    nla_len(attr) < NLA_HDRLEN) {
2861253683bbSDavid Howells 				err = -EINVAL;
2862c02db8c6SChris Wright 				goto errout;
2863253683bbSDavid Howells 			}
28648cb08174SJohannes Berg 			err = nla_parse_nested_deprecated(vfinfo, IFLA_VF_MAX,
28658cb08174SJohannes Berg 							  attr,
28668cb08174SJohannes Berg 							  ifla_vf_policy,
28678cb08174SJohannes Berg 							  NULL);
28684f7d2cdfSDaniel Borkmann 			if (err < 0)
28694f7d2cdfSDaniel Borkmann 				goto errout;
28704f7d2cdfSDaniel Borkmann 			err = do_setvfinfo(dev, vfinfo);
2871ebc08a6fSWilliams, Mitch A 			if (err < 0)
2872ebc08a6fSWilliams, Mitch A 				goto errout;
2873ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
2874ebc08a6fSWilliams, Mitch A 		}
2875ebc08a6fSWilliams, Mitch A 	}
28760157f60cSPatrick McHardy 	err = 0;
28770157f60cSPatrick McHardy 
287857b61080SScott Feldman 	if (tb[IFLA_VF_PORTS]) {
287957b61080SScott Feldman 		struct nlattr *port[IFLA_PORT_MAX+1];
288057b61080SScott Feldman 		struct nlattr *attr;
288157b61080SScott Feldman 		int vf;
288257b61080SScott Feldman 		int rem;
288357b61080SScott Feldman 
288457b61080SScott Feldman 		err = -EOPNOTSUPP;
288557b61080SScott Feldman 		if (!ops->ndo_set_vf_port)
288657b61080SScott Feldman 			goto errout;
288757b61080SScott Feldman 
288857b61080SScott Feldman 		nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
2889035d210fSDaniel Borkmann 			if (nla_type(attr) != IFLA_VF_PORT ||
2890035d210fSDaniel Borkmann 			    nla_len(attr) < NLA_HDRLEN) {
2891035d210fSDaniel Borkmann 				err = -EINVAL;
2892035d210fSDaniel Borkmann 				goto errout;
2893035d210fSDaniel Borkmann 			}
28948cb08174SJohannes Berg 			err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
28958cb08174SJohannes Berg 							  attr,
28968cb08174SJohannes Berg 							  ifla_port_policy,
28978cb08174SJohannes Berg 							  NULL);
289857b61080SScott Feldman 			if (err < 0)
289957b61080SScott Feldman 				goto errout;
290057b61080SScott Feldman 			if (!port[IFLA_PORT_VF]) {
290157b61080SScott Feldman 				err = -EOPNOTSUPP;
290257b61080SScott Feldman 				goto errout;
290357b61080SScott Feldman 			}
290457b61080SScott Feldman 			vf = nla_get_u32(port[IFLA_PORT_VF]);
290557b61080SScott Feldman 			err = ops->ndo_set_vf_port(dev, vf, port);
290657b61080SScott Feldman 			if (err < 0)
290757b61080SScott Feldman 				goto errout;
2908ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
290957b61080SScott Feldman 		}
291057b61080SScott Feldman 	}
291157b61080SScott Feldman 	err = 0;
291257b61080SScott Feldman 
291357b61080SScott Feldman 	if (tb[IFLA_PORT_SELF]) {
291457b61080SScott Feldman 		struct nlattr *port[IFLA_PORT_MAX+1];
291557b61080SScott Feldman 
29168cb08174SJohannes Berg 		err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
29178cb08174SJohannes Berg 						  tb[IFLA_PORT_SELF],
29188cb08174SJohannes Berg 						  ifla_port_policy, NULL);
291957b61080SScott Feldman 		if (err < 0)
292057b61080SScott Feldman 			goto errout;
292157b61080SScott Feldman 
292257b61080SScott Feldman 		err = -EOPNOTSUPP;
292357b61080SScott Feldman 		if (ops->ndo_set_vf_port)
292457b61080SScott Feldman 			err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
292557b61080SScott Feldman 		if (err < 0)
292657b61080SScott Feldman 			goto errout;
2927ba998906SNicolas Dichtel 		status |= DO_SETLINK_NOTIFY;
292857b61080SScott Feldman 	}
2929f8ff182cSThomas Graf 
2930f8ff182cSThomas Graf 	if (tb[IFLA_AF_SPEC]) {
2931f8ff182cSThomas Graf 		struct nlattr *af;
2932f8ff182cSThomas Graf 		int rem;
2933f8ff182cSThomas Graf 
2934f8ff182cSThomas Graf 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2935f8ff182cSThomas Graf 			const struct rtnl_af_ops *af_ops;
2936f8ff182cSThomas Graf 
2937058c8d59SGustavo A. R. Silva 			BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af))));
2938f8ff182cSThomas Graf 
29393583a4e8SStephen Hemminger 			err = af_ops->set_link_af(dev, af, extack);
2940a100243dSCong Wang 			if (err < 0)
2941f8ff182cSThomas Graf 				goto errout;
2942f8ff182cSThomas Graf 
2943ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
2944f8ff182cSThomas Graf 		}
2945f8ff182cSThomas Graf 	}
294657b61080SScott Feldman 	err = 0;
294757b61080SScott Feldman 
2948829eb208SRoopa Prabhu 	if (tb[IFLA_PROTO_DOWN] || tb[IFLA_PROTO_DOWN_REASON]) {
2949829eb208SRoopa Prabhu 		err = do_set_proto_down(dev, tb[IFLA_PROTO_DOWN],
2950829eb208SRoopa Prabhu 					tb[IFLA_PROTO_DOWN_REASON], extack);
295188d6378bSAnuradha Karuppiah 		if (err)
295288d6378bSAnuradha Karuppiah 			goto errout;
295388d6378bSAnuradha Karuppiah 		status |= DO_SETLINK_NOTIFY;
295488d6378bSAnuradha Karuppiah 	}
295588d6378bSAnuradha Karuppiah 
2956d1fdd913SBrenden Blanco 	if (tb[IFLA_XDP]) {
2957d1fdd913SBrenden Blanco 		struct nlattr *xdp[IFLA_XDP_MAX + 1];
295885de8576SDaniel Borkmann 		u32 xdp_flags = 0;
2959d1fdd913SBrenden Blanco 
29608cb08174SJohannes Berg 		err = nla_parse_nested_deprecated(xdp, IFLA_XDP_MAX,
29618cb08174SJohannes Berg 						  tb[IFLA_XDP],
2962fceb6435SJohannes Berg 						  ifla_xdp_policy, NULL);
2963d1fdd913SBrenden Blanco 		if (err < 0)
2964d1fdd913SBrenden Blanco 			goto errout;
2965d1fdd913SBrenden Blanco 
296658038695SMartin KaFai Lau 		if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
2967262d8625SBrenden Blanco 			err = -EINVAL;
2968262d8625SBrenden Blanco 			goto errout;
2969262d8625SBrenden Blanco 		}
297085de8576SDaniel Borkmann 
297185de8576SDaniel Borkmann 		if (xdp[IFLA_XDP_FLAGS]) {
297285de8576SDaniel Borkmann 			xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
297385de8576SDaniel Borkmann 			if (xdp_flags & ~XDP_FLAGS_MASK) {
297485de8576SDaniel Borkmann 				err = -EINVAL;
297585de8576SDaniel Borkmann 				goto errout;
297685de8576SDaniel Borkmann 			}
2977ee5d032fSJakub Kicinski 			if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) {
29780489df9aSDaniel Borkmann 				err = -EINVAL;
29790489df9aSDaniel Borkmann 				goto errout;
29800489df9aSDaniel Borkmann 			}
298185de8576SDaniel Borkmann 		}
298285de8576SDaniel Borkmann 
2983d1fdd913SBrenden Blanco 		if (xdp[IFLA_XDP_FD]) {
298492234c8fSToke Høiland-Jørgensen 			int expected_fd = -1;
298592234c8fSToke Høiland-Jørgensen 
298692234c8fSToke Høiland-Jørgensen 			if (xdp_flags & XDP_FLAGS_REPLACE) {
298792234c8fSToke Høiland-Jørgensen 				if (!xdp[IFLA_XDP_EXPECTED_FD]) {
298892234c8fSToke Høiland-Jørgensen 					err = -EINVAL;
298992234c8fSToke Høiland-Jørgensen 					goto errout;
299092234c8fSToke Høiland-Jørgensen 				}
299192234c8fSToke Høiland-Jørgensen 				expected_fd =
299292234c8fSToke Høiland-Jørgensen 					nla_get_s32(xdp[IFLA_XDP_EXPECTED_FD]);
299392234c8fSToke Høiland-Jørgensen 			}
299492234c8fSToke Høiland-Jørgensen 
2995ddf9f970SJakub Kicinski 			err = dev_change_xdp_fd(dev, extack,
299685de8576SDaniel Borkmann 						nla_get_s32(xdp[IFLA_XDP_FD]),
299792234c8fSToke Høiland-Jørgensen 						expected_fd,
299885de8576SDaniel Borkmann 						xdp_flags);
2999d1fdd913SBrenden Blanco 			if (err)
3000d1fdd913SBrenden Blanco 				goto errout;
3001d1fdd913SBrenden Blanco 			status |= DO_SETLINK_NOTIFY;
3002d1fdd913SBrenden Blanco 		}
3003d1fdd913SBrenden Blanco 	}
3004d1fdd913SBrenden Blanco 
30050157f60cSPatrick McHardy errout:
3006ba998906SNicolas Dichtel 	if (status & DO_SETLINK_MODIFIED) {
300764ff90ccSXin Long 		if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY)
3008ba998906SNicolas Dichtel 			netdev_state_change(dev);
3009ba998906SNicolas Dichtel 
3010ba998906SNicolas Dichtel 		if (err < 0)
3011e87cc472SJoe Perches 			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",
3012e87cc472SJoe Perches 					     dev->name);
3013ba998906SNicolas Dichtel 	}
30140157f60cSPatrick McHardy 
30150157f60cSPatrick McHardy 	return err;
30160157f60cSPatrick McHardy }
30170157f60cSPatrick McHardy 
3018cc6090e9SJiri Pirko static struct net_device *rtnl_dev_get(struct net *net,
30195ea08b52SFlorent Fourcot 				       struct nlattr *tb[])
3020cc6090e9SJiri Pirko {
30215ea08b52SFlorent Fourcot 	char ifname[ALTIFNAMSIZ];
3022cc6090e9SJiri Pirko 
30235ea08b52SFlorent Fourcot 	if (tb[IFLA_IFNAME])
30245ea08b52SFlorent Fourcot 		nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
30255ea08b52SFlorent Fourcot 	else if (tb[IFLA_ALT_IFNAME])
30265ea08b52SFlorent Fourcot 		nla_strscpy(ifname, tb[IFLA_ALT_IFNAME], ALTIFNAMSIZ);
3027cc6090e9SJiri Pirko 	else
3028cc6090e9SJiri Pirko 		return NULL;
3029cc6090e9SJiri Pirko 
3030cc6090e9SJiri Pirko 	return __dev_get_by_name(net, ifname);
3031cc6090e9SJiri Pirko }
3032cc6090e9SJiri Pirko 
3033c21ef3e3SDavid Ahern static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3034c21ef3e3SDavid Ahern 			struct netlink_ext_ack *extack)
3035da5e0494SThomas Graf {
30363b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
3037da5e0494SThomas Graf 	struct ifinfomsg *ifm;
3038da5e0494SThomas Graf 	struct net_device *dev;
30390157f60cSPatrick McHardy 	int err;
3040da5e0494SThomas Graf 	struct nlattr *tb[IFLA_MAX+1];
30411da177e4SLinus Torvalds 
30428cb08174SJohannes Berg 	err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
30438cb08174SJohannes Berg 				     ifla_policy, extack);
3044da5e0494SThomas Graf 	if (err < 0)
3045da5e0494SThomas Graf 		goto errout;
30461da177e4SLinus Torvalds 
30474ff66caeSChristian Brauner 	err = rtnl_ensure_unique_netns(tb, extack, false);
30484ff66caeSChristian Brauner 	if (err < 0)
30494ff66caeSChristian Brauner 		goto errout;
30504ff66caeSChristian Brauner 
30511da177e4SLinus Torvalds 	err = -EINVAL;
3052da5e0494SThomas Graf 	ifm = nlmsg_data(nlh);
305351055be8SPatrick McHardy 	if (ifm->ifi_index > 0)
3054a3d12891SEric Dumazet 		dev = __dev_get_by_index(net, ifm->ifi_index);
305576c9ac0eSJiri Pirko 	else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
30565ea08b52SFlorent Fourcot 		dev = rtnl_dev_get(net, tb);
3057da5e0494SThomas Graf 	else
3058da5e0494SThomas Graf 		goto errout;
30591da177e4SLinus Torvalds 
3060da5e0494SThomas Graf 	if (dev == NULL) {
3061da5e0494SThomas Graf 		err = -ENODEV;
3062da5e0494SThomas Graf 		goto errout;
3063da5e0494SThomas Graf 	}
30641da177e4SLinus Torvalds 
30655ea08b52SFlorent Fourcot 	err = do_setlink(skb, dev, ifm, extack, tb, 0);
3066da5e0494SThomas Graf errout:
30671da177e4SLinus Torvalds 	return err;
30681da177e4SLinus Torvalds }
30691da177e4SLinus Torvalds 
307066400d54SWANG Cong static int rtnl_group_dellink(const struct net *net, int group)
307166400d54SWANG Cong {
307266400d54SWANG Cong 	struct net_device *dev, *aux;
307366400d54SWANG Cong 	LIST_HEAD(list_kill);
307466400d54SWANG Cong 	bool found = false;
307566400d54SWANG Cong 
307666400d54SWANG Cong 	if (!group)
307766400d54SWANG Cong 		return -EPERM;
307866400d54SWANG Cong 
307966400d54SWANG Cong 	for_each_netdev(net, dev) {
308066400d54SWANG Cong 		if (dev->group == group) {
308166400d54SWANG Cong 			const struct rtnl_link_ops *ops;
308266400d54SWANG Cong 
308366400d54SWANG Cong 			found = true;
308466400d54SWANG Cong 			ops = dev->rtnl_link_ops;
308566400d54SWANG Cong 			if (!ops || !ops->dellink)
308666400d54SWANG Cong 				return -EOPNOTSUPP;
308766400d54SWANG Cong 		}
308866400d54SWANG Cong 	}
308966400d54SWANG Cong 
309066400d54SWANG Cong 	if (!found)
309166400d54SWANG Cong 		return -ENODEV;
309266400d54SWANG Cong 
309366400d54SWANG Cong 	for_each_netdev_safe(net, dev, aux) {
309466400d54SWANG Cong 		if (dev->group == group) {
309566400d54SWANG Cong 			const struct rtnl_link_ops *ops;
309666400d54SWANG Cong 
309766400d54SWANG Cong 			ops = dev->rtnl_link_ops;
309866400d54SWANG Cong 			ops->dellink(dev, &list_kill);
309966400d54SWANG Cong 		}
310066400d54SWANG Cong 	}
310166400d54SWANG Cong 	unregister_netdevice_many(&list_kill);
310266400d54SWANG Cong 
310366400d54SWANG Cong 	return 0;
310466400d54SWANG Cong }
310566400d54SWANG Cong 
3106614732eaSThomas Graf int rtnl_delete_link(struct net_device *dev)
3107614732eaSThomas Graf {
3108614732eaSThomas Graf 	const struct rtnl_link_ops *ops;
3109614732eaSThomas Graf 	LIST_HEAD(list_kill);
3110614732eaSThomas Graf 
3111614732eaSThomas Graf 	ops = dev->rtnl_link_ops;
3112614732eaSThomas Graf 	if (!ops || !ops->dellink)
3113614732eaSThomas Graf 		return -EOPNOTSUPP;
3114614732eaSThomas Graf 
3115614732eaSThomas Graf 	ops->dellink(dev, &list_kill);
3116614732eaSThomas Graf 	unregister_netdevice_many(&list_kill);
3117614732eaSThomas Graf 
3118614732eaSThomas Graf 	return 0;
3119614732eaSThomas Graf }
3120614732eaSThomas Graf EXPORT_SYMBOL_GPL(rtnl_delete_link);
3121614732eaSThomas Graf 
3122c21ef3e3SDavid Ahern static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
3123c21ef3e3SDavid Ahern 			struct netlink_ext_ack *extack)
312438f7b870SPatrick McHardy {
31253b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
3126b61ad68aSChristian Brauner 	struct net *tgt_net = net;
3127b61ad68aSChristian Brauner 	struct net_device *dev = NULL;
312838f7b870SPatrick McHardy 	struct ifinfomsg *ifm;
312938f7b870SPatrick McHardy 	struct nlattr *tb[IFLA_MAX+1];
313038f7b870SPatrick McHardy 	int err;
3131b61ad68aSChristian Brauner 	int netnsid = -1;
313238f7b870SPatrick McHardy 
31338cb08174SJohannes Berg 	err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
31348cb08174SJohannes Berg 				     ifla_policy, extack);
313538f7b870SPatrick McHardy 	if (err < 0)
313638f7b870SPatrick McHardy 		return err;
313738f7b870SPatrick McHardy 
31384ff66caeSChristian Brauner 	err = rtnl_ensure_unique_netns(tb, extack, true);
31394ff66caeSChristian Brauner 	if (err < 0)
31404ff66caeSChristian Brauner 		return err;
31414ff66caeSChristian Brauner 
31427e4a8d5aSChristian Brauner 	if (tb[IFLA_TARGET_NETNSID]) {
31437e4a8d5aSChristian Brauner 		netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
3144c383edc4SChristian Brauner 		tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
3145b61ad68aSChristian Brauner 		if (IS_ERR(tgt_net))
3146b61ad68aSChristian Brauner 			return PTR_ERR(tgt_net);
3147b61ad68aSChristian Brauner 	}
3148b61ad68aSChristian Brauner 
3149b61ad68aSChristian Brauner 	err = -EINVAL;
315038f7b870SPatrick McHardy 	ifm = nlmsg_data(nlh);
315138f7b870SPatrick McHardy 	if (ifm->ifi_index > 0)
3152b61ad68aSChristian Brauner 		dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
315376c9ac0eSJiri Pirko 	else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
31545ea08b52SFlorent Fourcot 		dev = rtnl_dev_get(net, tb);
315566400d54SWANG Cong 	else if (tb[IFLA_GROUP])
3156b61ad68aSChristian Brauner 		err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP]));
315738f7b870SPatrick McHardy 	else
3158b61ad68aSChristian Brauner 		goto out;
315938f7b870SPatrick McHardy 
3160b61ad68aSChristian Brauner 	if (!dev) {
3161dee04163SFlorent Fourcot 		if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME] || ifm->ifi_index > 0)
3162b61ad68aSChristian Brauner 			err = -ENODEV;
316338f7b870SPatrick McHardy 
3164b61ad68aSChristian Brauner 		goto out;
3165b61ad68aSChristian Brauner 	}
3166b61ad68aSChristian Brauner 
3167b61ad68aSChristian Brauner 	err = rtnl_delete_link(dev);
3168b61ad68aSChristian Brauner 
3169b61ad68aSChristian Brauner out:
3170b61ad68aSChristian Brauner 	if (netnsid >= 0)
3171b61ad68aSChristian Brauner 		put_net(tgt_net);
3172b61ad68aSChristian Brauner 
3173b61ad68aSChristian Brauner 	return err;
317438f7b870SPatrick McHardy }
317538f7b870SPatrick McHardy 
31763729d502SPatrick McHardy int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
31773729d502SPatrick McHardy {
31783729d502SPatrick McHardy 	unsigned int old_flags;
31793729d502SPatrick McHardy 	int err;
31803729d502SPatrick McHardy 
31813729d502SPatrick McHardy 	old_flags = dev->flags;
31823729d502SPatrick McHardy 	if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
31836d040321SPetr Machata 		err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
31846d040321SPetr Machata 					 NULL);
31853729d502SPatrick McHardy 		if (err < 0)
31863729d502SPatrick McHardy 			return err;
31873729d502SPatrick McHardy 	}
31883729d502SPatrick McHardy 
31898d356b89SRoopa Prabhu 	if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) {
319056a49d70SRoopa Prabhu 		__dev_notify_flags(dev, old_flags, (old_flags ^ dev->flags));
31918d356b89SRoopa Prabhu 	} else {
31923729d502SPatrick McHardy 		dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
3193a528c219SNicolas Dichtel 		__dev_notify_flags(dev, old_flags, ~0U);
31948d356b89SRoopa Prabhu 	}
31953729d502SPatrick McHardy 	return 0;
31963729d502SPatrick McHardy }
31973729d502SPatrick McHardy EXPORT_SYMBOL(rtnl_configure_link);
31983729d502SPatrick McHardy 
3199d0522f1cSDavid Ahern struct net_device *rtnl_create_link(struct net *net, const char *ifname,
3200d0522f1cSDavid Ahern 				    unsigned char name_assign_type,
3201d0522f1cSDavid Ahern 				    const struct rtnl_link_ops *ops,
3202d0522f1cSDavid Ahern 				    struct nlattr *tb[],
3203d0522f1cSDavid Ahern 				    struct netlink_ext_ack *extack)
3204e7199288SPavel Emelianov {
3205e7199288SPavel Emelianov 	struct net_device *dev;
3206d40156aaSJiri Pirko 	unsigned int num_tx_queues = 1;
3207d40156aaSJiri Pirko 	unsigned int num_rx_queues = 1;
3208e7199288SPavel Emelianov 
320976ff5cc9SJiri Pirko 	if (tb[IFLA_NUM_TX_QUEUES])
321076ff5cc9SJiri Pirko 		num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
321176ff5cc9SJiri Pirko 	else if (ops->get_num_tx_queues)
3212d40156aaSJiri Pirko 		num_tx_queues = ops->get_num_tx_queues();
321376ff5cc9SJiri Pirko 
321476ff5cc9SJiri Pirko 	if (tb[IFLA_NUM_RX_QUEUES])
321576ff5cc9SJiri Pirko 		num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
321676ff5cc9SJiri Pirko 	else if (ops->get_num_rx_queues)
3217d40156aaSJiri Pirko 		num_rx_queues = ops->get_num_rx_queues();
3218efacb309Sstephen hemminger 
3219d0522f1cSDavid Ahern 	if (num_tx_queues < 1 || num_tx_queues > 4096) {
3220d0522f1cSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid number of transmit queues");
32210e1d6ecaSEric Dumazet 		return ERR_PTR(-EINVAL);
3222d0522f1cSDavid Ahern 	}
32230e1d6ecaSEric Dumazet 
3224d0522f1cSDavid Ahern 	if (num_rx_queues < 1 || num_rx_queues > 4096) {
3225d0522f1cSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid number of receive queues");
32260e1d6ecaSEric Dumazet 		return ERR_PTR(-EINVAL);
3227d0522f1cSDavid Ahern 	}
32280e1d6ecaSEric Dumazet 
32298c713dc9SJohannes Berg 	if (ops->alloc) {
32308c713dc9SJohannes Berg 		dev = ops->alloc(tb, ifname, name_assign_type,
32318c713dc9SJohannes Berg 				 num_tx_queues, num_rx_queues);
32328c713dc9SJohannes Berg 		if (IS_ERR(dev))
32338c713dc9SJohannes Berg 			return dev;
32348c713dc9SJohannes Berg 	} else {
32358c713dc9SJohannes Berg 		dev = alloc_netdev_mqs(ops->priv_size, ifname,
32368c713dc9SJohannes Berg 				       name_assign_type, ops->setup,
32378c713dc9SJohannes Berg 				       num_tx_queues, num_rx_queues);
32388c713dc9SJohannes Berg 	}
32398c713dc9SJohannes Berg 
3240e7199288SPavel Emelianov 	if (!dev)
3241d1892e4eSTobias Klauser 		return ERR_PTR(-ENOMEM);
3242e7199288SPavel Emelianov 
324381adee47SEric W. Biederman 	dev_net_set(dev, net);
324481adee47SEric W. Biederman 	dev->rtnl_link_ops = ops;
32453729d502SPatrick McHardy 	dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
324681adee47SEric W. Biederman 
3247d836f5c6SEric Dumazet 	if (tb[IFLA_MTU]) {
3248d836f5c6SEric Dumazet 		u32 mtu = nla_get_u32(tb[IFLA_MTU]);
3249d836f5c6SEric Dumazet 		int err;
3250d836f5c6SEric Dumazet 
3251d836f5c6SEric Dumazet 		err = dev_validate_mtu(dev, mtu, extack);
3252d836f5c6SEric Dumazet 		if (err) {
3253d836f5c6SEric Dumazet 			free_netdev(dev);
3254d836f5c6SEric Dumazet 			return ERR_PTR(err);
3255d836f5c6SEric Dumazet 		}
3256d836f5c6SEric Dumazet 		dev->mtu = mtu;
3257d836f5c6SEric Dumazet 	}
32582afb9b53SJiri Pirko 	if (tb[IFLA_ADDRESS]) {
3259efd38f75SJakub Kicinski 		__dev_addr_set(dev, nla_data(tb[IFLA_ADDRESS]),
3260e7199288SPavel Emelianov 			       nla_len(tb[IFLA_ADDRESS]));
32612afb9b53SJiri Pirko 		dev->addr_assign_type = NET_ADDR_SET;
32622afb9b53SJiri Pirko 	}
3263e7199288SPavel Emelianov 	if (tb[IFLA_BROADCAST])
3264e7199288SPavel Emelianov 		memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
3265e7199288SPavel Emelianov 				nla_len(tb[IFLA_BROADCAST]));
3266e7199288SPavel Emelianov 	if (tb[IFLA_TXQLEN])
3267e7199288SPavel Emelianov 		dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
3268e7199288SPavel Emelianov 	if (tb[IFLA_OPERSTATE])
326993b2d4a2SDavid S. Miller 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
3270e7199288SPavel Emelianov 	if (tb[IFLA_LINKMODE])
3271e7199288SPavel Emelianov 		dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
3272ffa934f1SPatrick McHardy 	if (tb[IFLA_GROUP])
3273ffa934f1SPatrick McHardy 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
327446e6b992SStephen Hemminger 	if (tb[IFLA_GSO_MAX_SIZE])
327546e6b992SStephen Hemminger 		netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE]));
327646e6b992SStephen Hemminger 	if (tb[IFLA_GSO_MAX_SEGS])
32776d872df3SEric Dumazet 		netif_set_gso_max_segs(dev, nla_get_u32(tb[IFLA_GSO_MAX_SEGS]));
3278eac1b93cSCoco Li 	if (tb[IFLA_GRO_MAX_SIZE])
3279eac1b93cSCoco Li 		netif_set_gro_max_size(dev, nla_get_u32(tb[IFLA_GRO_MAX_SIZE]));
3280e7199288SPavel Emelianov 
3281e7199288SPavel Emelianov 	return dev;
3282e7199288SPavel Emelianov }
3283e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_create_link);
3284e7199288SPavel Emelianov 
328590f62cf3SEric W. Biederman static int rtnl_group_changelink(const struct sk_buff *skb,
328690f62cf3SEric W. Biederman 		struct net *net, int group,
3287e7ed828fSVlad Dogaru 		struct ifinfomsg *ifm,
3288ddf9f970SJakub Kicinski 		struct netlink_ext_ack *extack,
3289e7ed828fSVlad Dogaru 		struct nlattr **tb)
3290e7ed828fSVlad Dogaru {
3291d079535dSWANG Cong 	struct net_device *dev, *aux;
3292e7ed828fSVlad Dogaru 	int err;
3293e7ed828fSVlad Dogaru 
3294d079535dSWANG Cong 	for_each_netdev_safe(net, dev, aux) {
3295e7ed828fSVlad Dogaru 		if (dev->group == group) {
32965ea08b52SFlorent Fourcot 			err = do_setlink(skb, dev, ifm, extack, tb, 0);
3297e7ed828fSVlad Dogaru 			if (err < 0)
3298e7ed828fSVlad Dogaru 				return err;
3299e7ed828fSVlad Dogaru 		}
3300e7ed828fSVlad Dogaru 	}
3301e7ed828fSVlad Dogaru 
3302e7ed828fSVlad Dogaru 	return 0;
3303e7ed828fSVlad Dogaru }
3304e7ed828fSVlad Dogaru 
3305*63105e83SJakub Kicinski static int rtnl_newlink_create(struct sk_buff *skb, struct ifinfomsg *ifm,
3306*63105e83SJakub Kicinski 			       const struct rtnl_link_ops *ops,
3307*63105e83SJakub Kicinski 			       struct nlattr **tb, struct nlattr **data,
3308*63105e83SJakub Kicinski 			       struct netlink_ext_ack *extack);
3309*63105e83SJakub Kicinski 
3310c92bf26cSJakub Kicinski struct rtnl_newlink_tbs {
3311c92bf26cSJakub Kicinski 	struct nlattr *tb[IFLA_MAX + 1];
3312c92bf26cSJakub Kicinski 	struct nlattr *attr[RTNL_MAX_TYPE + 1];
3313420d0318SJakub Kicinski 	struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1];
3314c92bf26cSJakub Kicinski };
3315c92bf26cSJakub Kicinski 
3316c92bf26cSJakub Kicinski static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3317c92bf26cSJakub Kicinski 			  struct rtnl_newlink_tbs *tbs,
3318c92bf26cSJakub Kicinski 			  struct netlink_ext_ack *extack)
3319c92bf26cSJakub Kicinski {
3320420d0318SJakub Kicinski 	struct nlattr *linkinfo[IFLA_INFO_MAX + 1];
3321c92bf26cSJakub Kicinski 	struct nlattr ** const tb = tbs->tb;
3322c6f6f244SEric Dumazet 	const struct rtnl_link_ops *m_ops;
3323c6f6f244SEric Dumazet 	struct net_device *master_dev;
33243b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
332538f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops;
3326420d0318SJakub Kicinski 	struct nlattr **slave_data;
3327420d0318SJakub Kicinski 	char kind[MODULE_NAME_LEN];
3328420d0318SJakub Kicinski 	struct net_device *dev;
3329420d0318SJakub Kicinski 	struct ifinfomsg *ifm;
3330420d0318SJakub Kicinski 	struct nlattr **data;
3331ef2a7c90SFlorent Fourcot 	bool link_specified;
333238f7b870SPatrick McHardy 	int err;
333338f7b870SPatrick McHardy 
333495a5afcaSJohannes Berg #ifdef CONFIG_MODULES
333538f7b870SPatrick McHardy replay:
33368072f085SThomas Graf #endif
33378cb08174SJohannes Berg 	err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
33388cb08174SJohannes Berg 				     ifla_policy, extack);
333938f7b870SPatrick McHardy 	if (err < 0)
334038f7b870SPatrick McHardy 		return err;
334138f7b870SPatrick McHardy 
33424ff66caeSChristian Brauner 	err = rtnl_ensure_unique_netns(tb, extack, false);
33434ff66caeSChristian Brauner 	if (err < 0)
33444ff66caeSChristian Brauner 		return err;
33454ff66caeSChristian Brauner 
334638f7b870SPatrick McHardy 	ifm = nlmsg_data(nlh);
3347ef2a7c90SFlorent Fourcot 	if (ifm->ifi_index > 0) {
3348ef2a7c90SFlorent Fourcot 		link_specified = true;
3349881d966bSEric W. Biederman 		dev = __dev_get_by_index(net, ifm->ifi_index);
3350ef2a7c90SFlorent Fourcot 	} else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) {
3351ef2a7c90SFlorent Fourcot 		link_specified = true;
33525ea08b52SFlorent Fourcot 		dev = rtnl_dev_get(net, tb);
3353ef2a7c90SFlorent Fourcot 	} else {
3354ef2a7c90SFlorent Fourcot 		link_specified = false;
335538f7b870SPatrick McHardy 		dev = NULL;
3356ef2a7c90SFlorent Fourcot 	}
335738f7b870SPatrick McHardy 
3358c6f6f244SEric Dumazet 	master_dev = NULL;
3359c6f6f244SEric Dumazet 	m_ops = NULL;
3360ba7d49b1SJiri Pirko 	if (dev) {
3361ba7d49b1SJiri Pirko 		master_dev = netdev_master_upper_dev_get(dev);
3362ba7d49b1SJiri Pirko 		if (master_dev)
3363ba7d49b1SJiri Pirko 			m_ops = master_dev->rtnl_link_ops;
3364ba7d49b1SJiri Pirko 	}
3365ba7d49b1SJiri Pirko 
33668679c31eSRocco Yue 	err = validate_linkmsg(dev, tb, extack);
3367e0d087afSEric Dumazet 	if (err < 0)
33681840bb13SThomas Graf 		return err;
33691840bb13SThomas Graf 
337038f7b870SPatrick McHardy 	if (tb[IFLA_LINKINFO]) {
33718cb08174SJohannes Berg 		err = nla_parse_nested_deprecated(linkinfo, IFLA_INFO_MAX,
33728cb08174SJohannes Berg 						  tb[IFLA_LINKINFO],
33738cb08174SJohannes Berg 						  ifla_info_policy, NULL);
337438f7b870SPatrick McHardy 		if (err < 0)
337538f7b870SPatrick McHardy 			return err;
337638f7b870SPatrick McHardy 	} else
337738f7b870SPatrick McHardy 		memset(linkinfo, 0, sizeof(linkinfo));
337838f7b870SPatrick McHardy 
337938f7b870SPatrick McHardy 	if (linkinfo[IFLA_INFO_KIND]) {
3380872f6903SFrancis Laniel 		nla_strscpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
338138f7b870SPatrick McHardy 		ops = rtnl_link_ops_get(kind);
338238f7b870SPatrick McHardy 	} else {
338338f7b870SPatrick McHardy 		kind[0] = '\0';
338438f7b870SPatrick McHardy 		ops = NULL;
338538f7b870SPatrick McHardy 	}
338638f7b870SPatrick McHardy 
3387420d0318SJakub Kicinski 	data = NULL;
338838f7b870SPatrick McHardy 	if (ops) {
3389ccf8dbcdSKees Cook 		if (ops->maxtype > RTNL_MAX_TYPE)
3390ccf8dbcdSKees Cook 			return -EINVAL;
3391ccf8dbcdSKees Cook 
339238f7b870SPatrick McHardy 		if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
3393c92bf26cSJakub Kicinski 			err = nla_parse_nested_deprecated(tbs->attr, ops->maxtype,
339438f7b870SPatrick McHardy 							  linkinfo[IFLA_INFO_DATA],
339568d57f3bSDavid Ahern 							  ops->policy, extack);
339638f7b870SPatrick McHardy 			if (err < 0)
339738f7b870SPatrick McHardy 				return err;
3398c92bf26cSJakub Kicinski 			data = tbs->attr;
339938f7b870SPatrick McHardy 		}
340038f7b870SPatrick McHardy 		if (ops->validate) {
3401a8b8a889SMatthias Schiffer 			err = ops->validate(tb, data, extack);
340238f7b870SPatrick McHardy 			if (err < 0)
340338f7b870SPatrick McHardy 				return err;
340438f7b870SPatrick McHardy 		}
340538f7b870SPatrick McHardy 	}
340638f7b870SPatrick McHardy 
3407420d0318SJakub Kicinski 	slave_data = NULL;
3408ba7d49b1SJiri Pirko 	if (m_ops) {
3409af066ed3SPrashant Bhole 		if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE)
3410ccf8dbcdSKees Cook 			return -EINVAL;
3411ccf8dbcdSKees Cook 
3412ba7d49b1SJiri Pirko 		if (m_ops->slave_maxtype &&
3413ba7d49b1SJiri Pirko 		    linkinfo[IFLA_INFO_SLAVE_DATA]) {
3414c92bf26cSJakub Kicinski 			err = nla_parse_nested_deprecated(tbs->slave_attr,
34158cb08174SJohannes Berg 							  m_ops->slave_maxtype,
3416ba7d49b1SJiri Pirko 							  linkinfo[IFLA_INFO_SLAVE_DATA],
34178cb08174SJohannes Berg 							  m_ops->slave_policy,
34188cb08174SJohannes Berg 							  extack);
3419ba7d49b1SJiri Pirko 			if (err < 0)
3420ba7d49b1SJiri Pirko 				return err;
3421c92bf26cSJakub Kicinski 			slave_data = tbs->slave_attr;
3422ba7d49b1SJiri Pirko 		}
3423ba7d49b1SJiri Pirko 	}
3424ba7d49b1SJiri Pirko 
342538f7b870SPatrick McHardy 	if (dev) {
342690c325e3SNicolas Dichtel 		int status = 0;
342738f7b870SPatrick McHardy 
342838f7b870SPatrick McHardy 		if (nlh->nlmsg_flags & NLM_F_EXCL)
342938f7b870SPatrick McHardy 			return -EEXIST;
343038f7b870SPatrick McHardy 		if (nlh->nlmsg_flags & NLM_F_REPLACE)
343138f7b870SPatrick McHardy 			return -EOPNOTSUPP;
343238f7b870SPatrick McHardy 
343338f7b870SPatrick McHardy 		if (linkinfo[IFLA_INFO_DATA]) {
343438f7b870SPatrick McHardy 			if (!ops || ops != dev->rtnl_link_ops ||
343538f7b870SPatrick McHardy 			    !ops->changelink)
343638f7b870SPatrick McHardy 				return -EOPNOTSUPP;
343738f7b870SPatrick McHardy 
3438ad744b22SMatthias Schiffer 			err = ops->changelink(dev, tb, data, extack);
343938f7b870SPatrick McHardy 			if (err < 0)
344038f7b870SPatrick McHardy 				return err;
3441ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
344238f7b870SPatrick McHardy 		}
344338f7b870SPatrick McHardy 
3444ba7d49b1SJiri Pirko 		if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
3445ba7d49b1SJiri Pirko 			if (!m_ops || !m_ops->slave_changelink)
3446ba7d49b1SJiri Pirko 				return -EOPNOTSUPP;
3447ba7d49b1SJiri Pirko 
3448420d0318SJakub Kicinski 			err = m_ops->slave_changelink(master_dev, dev, tb,
3449420d0318SJakub Kicinski 						      slave_data, extack);
3450ba7d49b1SJiri Pirko 			if (err < 0)
3451ba7d49b1SJiri Pirko 				return err;
3452ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
3453ba7d49b1SJiri Pirko 		}
3454ba7d49b1SJiri Pirko 
34555ea08b52SFlorent Fourcot 		return do_setlink(skb, dev, ifm, extack, tb, status);
345638f7b870SPatrick McHardy 	}
345738f7b870SPatrick McHardy 
3458ffa934f1SPatrick McHardy 	if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
3459ef2a7c90SFlorent Fourcot 		/* No dev found and NLM_F_CREATE not set. Requested dev does not exist,
3460ef2a7c90SFlorent Fourcot 		 * or it's for a group
3461ef2a7c90SFlorent Fourcot 		*/
3462ef2a7c90SFlorent Fourcot 		if (link_specified)
3463ef2a7c90SFlorent Fourcot 			return -ENODEV;
3464ef2a7c90SFlorent Fourcot 		if (tb[IFLA_GROUP])
346590f62cf3SEric W. Biederman 			return rtnl_group_changelink(skb, net,
3466ffa934f1SPatrick McHardy 						nla_get_u32(tb[IFLA_GROUP]),
3467ddf9f970SJakub Kicinski 						ifm, extack, tb);
34686f37c9f9SFlorent Fourcot 		return -ENODEV;
3469ffa934f1SPatrick McHardy 	}
347038f7b870SPatrick McHardy 
3471160ca014STheuns Verwoerd 	if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
347238f7b870SPatrick McHardy 		return -EOPNOTSUPP;
347338f7b870SPatrick McHardy 
347438f7b870SPatrick McHardy 	if (!ops) {
347595a5afcaSJohannes Berg #ifdef CONFIG_MODULES
347638f7b870SPatrick McHardy 		if (kind[0]) {
347738f7b870SPatrick McHardy 			__rtnl_unlock();
347838f7b870SPatrick McHardy 			request_module("rtnl-link-%s", kind);
347938f7b870SPatrick McHardy 			rtnl_lock();
348038f7b870SPatrick McHardy 			ops = rtnl_link_ops_get(kind);
348138f7b870SPatrick McHardy 			if (ops)
348238f7b870SPatrick McHardy 				goto replay;
348338f7b870SPatrick McHardy 		}
348438f7b870SPatrick McHardy #endif
348568d57f3bSDavid Ahern 		NL_SET_ERR_MSG(extack, "Unknown device type");
348638f7b870SPatrick McHardy 		return -EOPNOTSUPP;
348738f7b870SPatrick McHardy 	}
348838f7b870SPatrick McHardy 
3489*63105e83SJakub Kicinski 	return rtnl_newlink_create(skb, ifm, ops, tb, data, extack);
3490*63105e83SJakub Kicinski }
3491*63105e83SJakub Kicinski 
3492*63105e83SJakub Kicinski static int rtnl_newlink_create(struct sk_buff *skb, struct ifinfomsg *ifm,
3493*63105e83SJakub Kicinski 			       const struct rtnl_link_ops *ops,
3494*63105e83SJakub Kicinski 			       struct nlattr **tb, struct nlattr **data,
3495*63105e83SJakub Kicinski 			       struct netlink_ext_ack *extack)
3496*63105e83SJakub Kicinski {
3497*63105e83SJakub Kicinski 	unsigned char name_assign_type = NET_NAME_USER;
3498*63105e83SJakub Kicinski 	struct net *net = sock_net(skb->sk);
3499*63105e83SJakub Kicinski 	struct net *dest_net, *link_net;
3500*63105e83SJakub Kicinski 	struct net_device *dev;
3501*63105e83SJakub Kicinski 	char ifname[IFNAMSIZ];
3502*63105e83SJakub Kicinski 	int err;
3503*63105e83SJakub Kicinski 
35048c713dc9SJohannes Berg 	if (!ops->alloc && !ops->setup)
3505b0ab2fabSJiri Pirko 		return -EOPNOTSUPP;
3506b0ab2fabSJiri Pirko 
35075ea08b52SFlorent Fourcot 	if (tb[IFLA_IFNAME]) {
35085ea08b52SFlorent Fourcot 		nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
35095ea08b52SFlorent Fourcot 	} else {
351038f7b870SPatrick McHardy 		snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
35115517750fSTom Gundersen 		name_assign_type = NET_NAME_ENUM;
35125517750fSTom Gundersen 	}
351338f7b870SPatrick McHardy 
35145bb8ed07SChristian Brauner 	dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN);
351513ad1774SEric W. Biederman 	if (IS_ERR(dest_net))
351613ad1774SEric W. Biederman 		return PTR_ERR(dest_net);
351713ad1774SEric W. Biederman 
3518317f4810SNicolas Dichtel 	if (tb[IFLA_LINK_NETNSID]) {
3519317f4810SNicolas Dichtel 		int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
3520317f4810SNicolas Dichtel 
3521317f4810SNicolas Dichtel 		link_net = get_net_ns_by_id(dest_net, id);
3522317f4810SNicolas Dichtel 		if (!link_net) {
352368d57f3bSDavid Ahern 			NL_SET_ERR_MSG(extack, "Unknown network namespace id");
3524317f4810SNicolas Dichtel 			err =  -EINVAL;
3525317f4810SNicolas Dichtel 			goto out;
3526317f4810SNicolas Dichtel 		}
352706615bedSEric W. Biederman 		err = -EPERM;
352806615bedSEric W. Biederman 		if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
352906615bedSEric W. Biederman 			goto out;
3530420d0318SJakub Kicinski 	} else {
3531420d0318SJakub Kicinski 		link_net = NULL;
3532317f4810SNicolas Dichtel 	}
3533317f4810SNicolas Dichtel 
3534317f4810SNicolas Dichtel 	dev = rtnl_create_link(link_net ? : dest_net, ifname,
3535d0522f1cSDavid Ahern 			       name_assign_type, ops, tb, extack);
35369c7dafbfSPavel Emelyanov 	if (IS_ERR(dev)) {
3537e7199288SPavel Emelianov 		err = PTR_ERR(dev);
35389c7dafbfSPavel Emelyanov 		goto out;
35399c7dafbfSPavel Emelyanov 	}
35409c7dafbfSPavel Emelyanov 
35419c7dafbfSPavel Emelyanov 	dev->ifindex = ifm->ifi_index;
35429c7dafbfSPavel Emelyanov 
3543c269a24cSJakub Kicinski 	if (ops->newlink)
3544420d0318SJakub Kicinski 		err = ops->newlink(link_net ? : net, dev, tb, data, extack);
3545c269a24cSJakub Kicinski 	else
35462d85cba2SPatrick McHardy 		err = register_netdevice(dev);
3547fce9b9beSDan Carpenter 	if (err < 0) {
354838f7b870SPatrick McHardy 		free_netdev(dev);
35493729d502SPatrick McHardy 		goto out;
3550fce9b9beSDan Carpenter 	}
3551c269a24cSJakub Kicinski 
35523729d502SPatrick McHardy 	err = rtnl_configure_link(dev, ifm);
355343638900SDavid S. Miller 	if (err < 0)
355443638900SDavid S. Miller 		goto out_unregister;
355543638900SDavid S. Miller 	if (link_net) {
35560854fa82SAndrei Vagin 		err = dev_change_net_namespace(dev, dest_net, ifname);
355743638900SDavid S. Miller 		if (err < 0)
355843638900SDavid S. Miller 			goto out_unregister;
355943638900SDavid S. Miller 	}
3560160ca014STheuns Verwoerd 	if (tb[IFLA_MASTER]) {
3561420d0318SJakub Kicinski 		err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
3562160ca014STheuns Verwoerd 		if (err)
3563160ca014STheuns Verwoerd 			goto out_unregister;
3564160ca014STheuns Verwoerd 	}
356543638900SDavid S. Miller out:
356643638900SDavid S. Miller 	if (link_net)
356743638900SDavid S. Miller 		put_net(link_net);
356843638900SDavid S. Miller 	put_net(dest_net);
356943638900SDavid S. Miller 	return err;
357043638900SDavid S. Miller out_unregister:
35717afb8886SWANG Cong 	if (ops->newlink) {
35727afb8886SWANG Cong 		LIST_HEAD(list_kill);
35737afb8886SWANG Cong 
35747afb8886SWANG Cong 		ops->dellink(dev, &list_kill);
35757afb8886SWANG Cong 		unregister_netdevice_many(&list_kill);
35767afb8886SWANG Cong 	} else {
35773729d502SPatrick McHardy 		unregister_netdevice(dev);
35787afb8886SWANG Cong 	}
3579317f4810SNicolas Dichtel 	goto out;
3580317f4810SNicolas Dichtel }
358138f7b870SPatrick McHardy 
3582a2939745SJakub Kicinski static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3583a2939745SJakub Kicinski 			struct netlink_ext_ack *extack)
3584a2939745SJakub Kicinski {
3585c92bf26cSJakub Kicinski 	struct rtnl_newlink_tbs *tbs;
3586a2939745SJakub Kicinski 	int ret;
3587a2939745SJakub Kicinski 
3588c92bf26cSJakub Kicinski 	tbs = kmalloc(sizeof(*tbs), GFP_KERNEL);
3589c92bf26cSJakub Kicinski 	if (!tbs)
3590a2939745SJakub Kicinski 		return -ENOMEM;
3591a2939745SJakub Kicinski 
3592c92bf26cSJakub Kicinski 	ret = __rtnl_newlink(skb, nlh, tbs, extack);
3593c92bf26cSJakub Kicinski 	kfree(tbs);
3594a2939745SJakub Kicinski 	return ret;
3595a2939745SJakub Kicinski }
3596a2939745SJakub Kicinski 
35979b3757b0SJakub Kicinski static int rtnl_valid_getlink_req(struct sk_buff *skb,
35989b3757b0SJakub Kicinski 				  const struct nlmsghdr *nlh,
35999b3757b0SJakub Kicinski 				  struct nlattr **tb,
36009b3757b0SJakub Kicinski 				  struct netlink_ext_ack *extack)
36019b3757b0SJakub Kicinski {
36029b3757b0SJakub Kicinski 	struct ifinfomsg *ifm;
36039b3757b0SJakub Kicinski 	int i, err;
36049b3757b0SJakub Kicinski 
36059b3757b0SJakub Kicinski 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
36069b3757b0SJakub Kicinski 		NL_SET_ERR_MSG(extack, "Invalid header for get link");
36079b3757b0SJakub Kicinski 		return -EINVAL;
36089b3757b0SJakub Kicinski 	}
36099b3757b0SJakub Kicinski 
36109b3757b0SJakub Kicinski 	if (!netlink_strict_get_check(skb))
36118cb08174SJohannes Berg 		return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
36128cb08174SJohannes Berg 					      ifla_policy, extack);
36139b3757b0SJakub Kicinski 
36149b3757b0SJakub Kicinski 	ifm = nlmsg_data(nlh);
36159b3757b0SJakub Kicinski 	if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
36169b3757b0SJakub Kicinski 	    ifm->ifi_change) {
36179b3757b0SJakub Kicinski 		NL_SET_ERR_MSG(extack, "Invalid values in header for get link request");
36189b3757b0SJakub Kicinski 		return -EINVAL;
36199b3757b0SJakub Kicinski 	}
36209b3757b0SJakub Kicinski 
36218cb08174SJohannes Berg 	err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFLA_MAX,
36228cb08174SJohannes Berg 					    ifla_policy, extack);
36239b3757b0SJakub Kicinski 	if (err)
36249b3757b0SJakub Kicinski 		return err;
36259b3757b0SJakub Kicinski 
36269b3757b0SJakub Kicinski 	for (i = 0; i <= IFLA_MAX; i++) {
36279b3757b0SJakub Kicinski 		if (!tb[i])
36289b3757b0SJakub Kicinski 			continue;
36299b3757b0SJakub Kicinski 
36309b3757b0SJakub Kicinski 		switch (i) {
36319b3757b0SJakub Kicinski 		case IFLA_IFNAME:
363276c9ac0eSJiri Pirko 		case IFLA_ALT_IFNAME:
36339b3757b0SJakub Kicinski 		case IFLA_EXT_MASK:
36349b3757b0SJakub Kicinski 		case IFLA_TARGET_NETNSID:
36359b3757b0SJakub Kicinski 			break;
36369b3757b0SJakub Kicinski 		default:
36379b3757b0SJakub Kicinski 			NL_SET_ERR_MSG(extack, "Unsupported attribute in get link request");
36389b3757b0SJakub Kicinski 			return -EINVAL;
36399b3757b0SJakub Kicinski 		}
36409b3757b0SJakub Kicinski 	}
36419b3757b0SJakub Kicinski 
36429b3757b0SJakub Kicinski 	return 0;
36439b3757b0SJakub Kicinski }
36449b3757b0SJakub Kicinski 
3645c21ef3e3SDavid Ahern static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3646c21ef3e3SDavid Ahern 			struct netlink_ext_ack *extack)
3647711e2c33SJean Tourrilhes {
36483b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
364979e1ad14SJiri Benc 	struct net *tgt_net = net;
3650b60c5115SThomas Graf 	struct ifinfomsg *ifm;
3651b60c5115SThomas Graf 	struct nlattr *tb[IFLA_MAX+1];
3652b60c5115SThomas Graf 	struct net_device *dev = NULL;
3653b60c5115SThomas Graf 	struct sk_buff *nskb;
365479e1ad14SJiri Benc 	int netnsid = -1;
3655339bf98fSThomas Graf 	int err;
3656115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
3657711e2c33SJean Tourrilhes 
36589b3757b0SJakub Kicinski 	err = rtnl_valid_getlink_req(skb, nlh, tb, extack);
3659b60c5115SThomas Graf 	if (err < 0)
36609918f230SEric Sesterhenn 		return err;
3661b60c5115SThomas Graf 
36624ff66caeSChristian Brauner 	err = rtnl_ensure_unique_netns(tb, extack, true);
36634ff66caeSChristian Brauner 	if (err < 0)
36644ff66caeSChristian Brauner 		return err;
36654ff66caeSChristian Brauner 
36667e4a8d5aSChristian Brauner 	if (tb[IFLA_TARGET_NETNSID]) {
36677e4a8d5aSChristian Brauner 		netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
3668c383edc4SChristian Brauner 		tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
366979e1ad14SJiri Benc 		if (IS_ERR(tgt_net))
367079e1ad14SJiri Benc 			return PTR_ERR(tgt_net);
367179e1ad14SJiri Benc 	}
367279e1ad14SJiri Benc 
3673115c9b81SGreg Rose 	if (tb[IFLA_EXT_MASK])
3674115c9b81SGreg Rose 		ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3675115c9b81SGreg Rose 
367679e1ad14SJiri Benc 	err = -EINVAL;
3677b60c5115SThomas Graf 	ifm = nlmsg_data(nlh);
3678a3d12891SEric Dumazet 	if (ifm->ifi_index > 0)
367979e1ad14SJiri Benc 		dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
368076c9ac0eSJiri Pirko 	else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
36815ea08b52SFlorent Fourcot 		dev = rtnl_dev_get(tgt_net, tb);
3682a3d12891SEric Dumazet 	else
368379e1ad14SJiri Benc 		goto out;
3684b60c5115SThomas Graf 
368579e1ad14SJiri Benc 	err = -ENODEV;
3686a3d12891SEric Dumazet 	if (dev == NULL)
368779e1ad14SJiri Benc 		goto out;
3688a3d12891SEric Dumazet 
368979e1ad14SJiri Benc 	err = -ENOBUFS;
3690115c9b81SGreg Rose 	nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
3691a3d12891SEric Dumazet 	if (nskb == NULL)
369279e1ad14SJiri Benc 		goto out;
3693711e2c33SJean Tourrilhes 
369479e1ad14SJiri Benc 	err = rtnl_fill_ifinfo(nskb, dev, net,
369579e1ad14SJiri Benc 			       RTM_NEWLINK, NETLINK_CB(skb).portid,
369679e1ad14SJiri Benc 			       nlh->nlmsg_seq, 0, 0, ext_filter_mask,
3697d4e4fdf9SGuillaume Nault 			       0, NULL, 0, netnsid, GFP_KERNEL);
369826932566SPatrick McHardy 	if (err < 0) {
369926932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in if_nlmsg_size */
370026932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
370126932566SPatrick McHardy 		kfree_skb(nskb);
3702a3d12891SEric Dumazet 	} else
370315e47304SEric W. Biederman 		err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
370479e1ad14SJiri Benc out:
370579e1ad14SJiri Benc 	if (netnsid >= 0)
370679e1ad14SJiri Benc 		put_net(tgt_net);
3707b60c5115SThomas Graf 
3708711e2c33SJean Tourrilhes 	return err;
3709711e2c33SJean Tourrilhes }
3710711e2c33SJean Tourrilhes 
371136fbf1e5SJiri Pirko static int rtnl_alt_ifname(int cmd, struct net_device *dev, struct nlattr *attr,
371236fbf1e5SJiri Pirko 			   bool *changed, struct netlink_ext_ack *extack)
371336fbf1e5SJiri Pirko {
371436fbf1e5SJiri Pirko 	char *alt_ifname;
3715155fb43bSJakub Kicinski 	size_t size;
371636fbf1e5SJiri Pirko 	int err;
371736fbf1e5SJiri Pirko 
371836fbf1e5SJiri Pirko 	err = nla_validate(attr, attr->nla_len, IFLA_MAX, ifla_policy, extack);
371936fbf1e5SJiri Pirko 	if (err)
372036fbf1e5SJiri Pirko 		return err;
372136fbf1e5SJiri Pirko 
3722155fb43bSJakub Kicinski 	if (cmd == RTM_NEWLINKPROP) {
3723155fb43bSJakub Kicinski 		size = rtnl_prop_list_size(dev);
3724155fb43bSJakub Kicinski 		size += nla_total_size(ALTIFNAMSIZ);
3725155fb43bSJakub Kicinski 		if (size >= U16_MAX) {
3726155fb43bSJakub Kicinski 			NL_SET_ERR_MSG(extack,
3727155fb43bSJakub Kicinski 				       "effective property list too long");
3728155fb43bSJakub Kicinski 			return -EINVAL;
3729155fb43bSJakub Kicinski 		}
3730155fb43bSJakub Kicinski 	}
3731155fb43bSJakub Kicinski 
37325d26cff5SJakub Kicinski 	alt_ifname = nla_strdup(attr, GFP_KERNEL_ACCOUNT);
373336fbf1e5SJiri Pirko 	if (!alt_ifname)
373436fbf1e5SJiri Pirko 		return -ENOMEM;
373544bfa9c5SEric Dumazet 
373644bfa9c5SEric Dumazet 	if (cmd == RTM_NEWLINKPROP) {
373736fbf1e5SJiri Pirko 		err = netdev_name_node_alt_create(dev, alt_ifname);
373844bfa9c5SEric Dumazet 		if (!err)
373944bfa9c5SEric Dumazet 			alt_ifname = NULL;
374036fbf1e5SJiri Pirko 	} else if (cmd == RTM_DELLINKPROP) {
374136fbf1e5SJiri Pirko 		err = netdev_name_node_alt_destroy(dev, alt_ifname);
374236fbf1e5SJiri Pirko 	} else {
374344bfa9c5SEric Dumazet 		WARN_ON_ONCE(1);
374444bfa9c5SEric Dumazet 		err = -EINVAL;
374536fbf1e5SJiri Pirko 	}
374636fbf1e5SJiri Pirko 
374744bfa9c5SEric Dumazet 	kfree(alt_ifname);
374844bfa9c5SEric Dumazet 	if (!err)
374936fbf1e5SJiri Pirko 		*changed = true;
375044bfa9c5SEric Dumazet 	return err;
375136fbf1e5SJiri Pirko }
375236fbf1e5SJiri Pirko 
375336fbf1e5SJiri Pirko static int rtnl_linkprop(int cmd, struct sk_buff *skb, struct nlmsghdr *nlh,
375436fbf1e5SJiri Pirko 			 struct netlink_ext_ack *extack)
375536fbf1e5SJiri Pirko {
375636fbf1e5SJiri Pirko 	struct net *net = sock_net(skb->sk);
375736fbf1e5SJiri Pirko 	struct nlattr *tb[IFLA_MAX + 1];
375836fbf1e5SJiri Pirko 	struct net_device *dev;
375936fbf1e5SJiri Pirko 	struct ifinfomsg *ifm;
376036fbf1e5SJiri Pirko 	bool changed = false;
376136fbf1e5SJiri Pirko 	struct nlattr *attr;
376236fbf1e5SJiri Pirko 	int err, rem;
376336fbf1e5SJiri Pirko 
376436fbf1e5SJiri Pirko 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
376536fbf1e5SJiri Pirko 	if (err)
376636fbf1e5SJiri Pirko 		return err;
376736fbf1e5SJiri Pirko 
376836fbf1e5SJiri Pirko 	err = rtnl_ensure_unique_netns(tb, extack, true);
376936fbf1e5SJiri Pirko 	if (err)
377036fbf1e5SJiri Pirko 		return err;
377136fbf1e5SJiri Pirko 
377236fbf1e5SJiri Pirko 	ifm = nlmsg_data(nlh);
3773cc6090e9SJiri Pirko 	if (ifm->ifi_index > 0)
377436fbf1e5SJiri Pirko 		dev = __dev_get_by_index(net, ifm->ifi_index);
377576c9ac0eSJiri Pirko 	else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
37765ea08b52SFlorent Fourcot 		dev = rtnl_dev_get(net, tb);
3777cc6090e9SJiri Pirko 	else
377836fbf1e5SJiri Pirko 		return -EINVAL;
377936fbf1e5SJiri Pirko 
378036fbf1e5SJiri Pirko 	if (!dev)
378136fbf1e5SJiri Pirko 		return -ENODEV;
378236fbf1e5SJiri Pirko 
378336fbf1e5SJiri Pirko 	if (!tb[IFLA_PROP_LIST])
378436fbf1e5SJiri Pirko 		return 0;
378536fbf1e5SJiri Pirko 
378636fbf1e5SJiri Pirko 	nla_for_each_nested(attr, tb[IFLA_PROP_LIST], rem) {
378736fbf1e5SJiri Pirko 		switch (nla_type(attr)) {
378836fbf1e5SJiri Pirko 		case IFLA_ALT_IFNAME:
378936fbf1e5SJiri Pirko 			err = rtnl_alt_ifname(cmd, dev, attr, &changed, extack);
379036fbf1e5SJiri Pirko 			if (err)
379136fbf1e5SJiri Pirko 				return err;
379236fbf1e5SJiri Pirko 			break;
379336fbf1e5SJiri Pirko 		}
379436fbf1e5SJiri Pirko 	}
379536fbf1e5SJiri Pirko 
379636fbf1e5SJiri Pirko 	if (changed)
379736fbf1e5SJiri Pirko 		netdev_state_change(dev);
379836fbf1e5SJiri Pirko 	return 0;
379936fbf1e5SJiri Pirko }
380036fbf1e5SJiri Pirko 
380136fbf1e5SJiri Pirko static int rtnl_newlinkprop(struct sk_buff *skb, struct nlmsghdr *nlh,
380236fbf1e5SJiri Pirko 			    struct netlink_ext_ack *extack)
380336fbf1e5SJiri Pirko {
380436fbf1e5SJiri Pirko 	return rtnl_linkprop(RTM_NEWLINKPROP, skb, nlh, extack);
380536fbf1e5SJiri Pirko }
380636fbf1e5SJiri Pirko 
380736fbf1e5SJiri Pirko static int rtnl_dellinkprop(struct sk_buff *skb, struct nlmsghdr *nlh,
380836fbf1e5SJiri Pirko 			    struct netlink_ext_ack *extack)
380936fbf1e5SJiri Pirko {
381036fbf1e5SJiri Pirko 	return rtnl_linkprop(RTM_DELLINKPROP, skb, nlh, extack);
381136fbf1e5SJiri Pirko }
381236fbf1e5SJiri Pirko 
3813ebfe3c51SDi Zhu static u32 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
3814c7ac8679SGreg Rose {
3815115c9b81SGreg Rose 	struct net *net = sock_net(skb->sk);
3816ebfe3c51SDi Zhu 	size_t min_ifinfo_dump_size = 0;
3817115c9b81SGreg Rose 	struct nlattr *tb[IFLA_MAX+1];
3818115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
3819ebfe3c51SDi Zhu 	struct net_device *dev;
3820e5eca6d4SMichal Schmidt 	int hdrlen;
3821115c9b81SGreg Rose 
3822e5eca6d4SMichal Schmidt 	/* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
3823e5eca6d4SMichal Schmidt 	hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
3824e5eca6d4SMichal Schmidt 		 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
3825e5eca6d4SMichal Schmidt 
38268cb08174SJohannes Berg 	if (nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
3827115c9b81SGreg Rose 		if (tb[IFLA_EXT_MASK])
3828115c9b81SGreg Rose 			ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3829a4b64fbeSEric Dumazet 	}
3830115c9b81SGreg Rose 
3831115c9b81SGreg Rose 	if (!ext_filter_mask)
3832115c9b81SGreg Rose 		return NLMSG_GOODSIZE;
3833115c9b81SGreg Rose 	/*
3834115c9b81SGreg Rose 	 * traverse the list of net devices and compute the minimum
3835115c9b81SGreg Rose 	 * buffer size based upon the filter mask.
3836115c9b81SGreg Rose 	 */
38376853dd48SFlorian Westphal 	rcu_read_lock();
38386853dd48SFlorian Westphal 	for_each_netdev_rcu(net, dev) {
3839ebfe3c51SDi Zhu 		min_ifinfo_dump_size = max(min_ifinfo_dump_size,
3840ebfe3c51SDi Zhu 					   if_nlmsg_size(dev, ext_filter_mask));
3841115c9b81SGreg Rose 	}
38426853dd48SFlorian Westphal 	rcu_read_unlock();
3843115c9b81SGreg Rose 
384493af2056SZhang Shengju 	return nlmsg_total_size(min_ifinfo_dump_size);
3845c7ac8679SGreg Rose }
3846c7ac8679SGreg Rose 
384742bad1daSAdrian Bunk static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
38481da177e4SLinus Torvalds {
38491da177e4SLinus Torvalds 	int idx;
38501da177e4SLinus Torvalds 	int s_idx = cb->family;
385187ccbb1fSChristian Brauner 	int type = cb->nlh->nlmsg_type - RTM_BASE;
3852c63586dcSDavid Ahern 	int ret = 0;
38531da177e4SLinus Torvalds 
38541da177e4SLinus Torvalds 	if (s_idx == 0)
38551da177e4SLinus Torvalds 		s_idx = 1;
38566853dd48SFlorian Westphal 
385725239ceeSPatrick McHardy 	for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
385851e13685SJakub Kicinski 		struct rtnl_link __rcu **tab;
3859addf9b90SFlorian Westphal 		struct rtnl_link *link;
38606853dd48SFlorian Westphal 		rtnl_dumpit_func dumpit;
38616853dd48SFlorian Westphal 
38621da177e4SLinus Torvalds 		if (idx < s_idx || idx == PF_PACKET)
38631da177e4SLinus Torvalds 			continue;
38646853dd48SFlorian Westphal 
3865addf9b90SFlorian Westphal 		if (type < 0 || type >= RTM_NR_MSGTYPES)
38661da177e4SLinus Torvalds 			continue;
38676853dd48SFlorian Westphal 
3868addf9b90SFlorian Westphal 		tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]);
3869addf9b90SFlorian Westphal 		if (!tab)
3870addf9b90SFlorian Westphal 			continue;
3871addf9b90SFlorian Westphal 
387251e13685SJakub Kicinski 		link = rcu_dereference_rtnl(tab[type]);
3873addf9b90SFlorian Westphal 		if (!link)
3874addf9b90SFlorian Westphal 			continue;
3875addf9b90SFlorian Westphal 
3876addf9b90SFlorian Westphal 		dumpit = link->dumpit;
38776853dd48SFlorian Westphal 		if (!dumpit)
38786853dd48SFlorian Westphal 			continue;
38796853dd48SFlorian Westphal 
38800465277fSNicolas Dichtel 		if (idx > s_idx) {
38811da177e4SLinus Torvalds 			memset(&cb->args[0], 0, sizeof(cb->args));
38820465277fSNicolas Dichtel 			cb->prev_seq = 0;
38830465277fSNicolas Dichtel 			cb->seq = 0;
38840465277fSNicolas Dichtel 		}
3885c63586dcSDavid Ahern 		ret = dumpit(skb, cb);
38865e1acb4aSAlexey Kodanev 		if (ret)
38871da177e4SLinus Torvalds 			break;
38881da177e4SLinus Torvalds 	}
38891da177e4SLinus Torvalds 	cb->family = idx;
38901da177e4SLinus Torvalds 
3891c63586dcSDavid Ahern 	return skb->len ? : ret;
38921da177e4SLinus Torvalds }
38931da177e4SLinus Torvalds 
3894395eea6cSMahesh Bandewar struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
38953d3ea5afSVlad Yasevich 				       unsigned int change,
389638e01b30SNicolas Dichtel 				       u32 event, gfp_t flags, int *new_nsid,
389738e01b30SNicolas Dichtel 				       int new_ifindex)
38981da177e4SLinus Torvalds {
3899c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
39001da177e4SLinus Torvalds 	struct sk_buff *skb;
39010ec6d3f4SThomas Graf 	int err = -ENOBUFS;
39021da177e4SLinus Torvalds 
390350af5969Sluo penghao 	skb = nlmsg_new(if_nlmsg_size(dev, 0), flags);
39040ec6d3f4SThomas Graf 	if (skb == NULL)
39050ec6d3f4SThomas Graf 		goto errout;
39061da177e4SLinus Torvalds 
390779e1ad14SJiri Benc 	err = rtnl_fill_ifinfo(skb, dev, dev_net(dev),
390879e1ad14SJiri Benc 			       type, 0, 0, change, 0, 0, event,
3909d4e4fdf9SGuillaume Nault 			       new_nsid, new_ifindex, -1, flags);
391026932566SPatrick McHardy 	if (err < 0) {
391126932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in if_nlmsg_size() */
391226932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
391326932566SPatrick McHardy 		kfree_skb(skb);
391426932566SPatrick McHardy 		goto errout;
391526932566SPatrick McHardy 	}
3916395eea6cSMahesh Bandewar 	return skb;
39170ec6d3f4SThomas Graf errout:
39180ec6d3f4SThomas Graf 	if (err < 0)
39194b3da706SEric W. Biederman 		rtnl_set_sk_err(net, RTNLGRP_LINK, err);
3920395eea6cSMahesh Bandewar 	return NULL;
3921395eea6cSMahesh Bandewar }
3922395eea6cSMahesh Bandewar 
3923395eea6cSMahesh Bandewar void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
3924395eea6cSMahesh Bandewar {
3925395eea6cSMahesh Bandewar 	struct net *net = dev_net(dev);
3926395eea6cSMahesh Bandewar 
3927395eea6cSMahesh Bandewar 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
3928395eea6cSMahesh Bandewar }
3929395eea6cSMahesh Bandewar 
39303d3ea5afSVlad Yasevich static void rtmsg_ifinfo_event(int type, struct net_device *dev,
39313d3ea5afSVlad Yasevich 			       unsigned int change, u32 event,
393238e01b30SNicolas Dichtel 			       gfp_t flags, int *new_nsid, int new_ifindex)
3933395eea6cSMahesh Bandewar {
3934395eea6cSMahesh Bandewar 	struct sk_buff *skb;
3935395eea6cSMahesh Bandewar 
3936ed2a80abSNicolas Dichtel 	if (dev->reg_state != NETREG_REGISTERED)
3937ed2a80abSNicolas Dichtel 		return;
3938ed2a80abSNicolas Dichtel 
393938e01b30SNicolas Dichtel 	skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid,
394038e01b30SNicolas Dichtel 				     new_ifindex);
3941395eea6cSMahesh Bandewar 	if (skb)
3942395eea6cSMahesh Bandewar 		rtmsg_ifinfo_send(skb, dev, flags);
39431da177e4SLinus Torvalds }
39443d3ea5afSVlad Yasevich 
39453d3ea5afSVlad Yasevich void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
39463d3ea5afSVlad Yasevich 		  gfp_t flags)
39473d3ea5afSVlad Yasevich {
394838e01b30SNicolas Dichtel 	rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
394938e01b30SNicolas Dichtel 			   NULL, 0);
39503d3ea5afSVlad Yasevich }
39511da177e4SLinus Torvalds 
39526621dd29SNicolas Dichtel void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change,
395338e01b30SNicolas Dichtel 			 gfp_t flags, int *new_nsid, int new_ifindex)
39546621dd29SNicolas Dichtel {
39556621dd29SNicolas Dichtel 	rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
395638e01b30SNicolas Dichtel 			   new_nsid, new_ifindex);
39576621dd29SNicolas Dichtel }
39586621dd29SNicolas Dichtel 
3959d83b0603SJohn Fastabend static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
3960d83b0603SJohn Fastabend 				   struct net_device *dev,
39611e53d5bbSHubert Sokolowski 				   u8 *addr, u16 vid, u32 pid, u32 seq,
39621c104a6bSNicolas Dichtel 				   int type, unsigned int flags,
3963b3379041SHubert Sokolowski 				   int nlflags, u16 ndm_state)
3964d83b0603SJohn Fastabend {
3965d83b0603SJohn Fastabend 	struct nlmsghdr *nlh;
3966d83b0603SJohn Fastabend 	struct ndmsg *ndm;
3967d83b0603SJohn Fastabend 
39681c104a6bSNicolas Dichtel 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
3969d83b0603SJohn Fastabend 	if (!nlh)
3970d83b0603SJohn Fastabend 		return -EMSGSIZE;
3971d83b0603SJohn Fastabend 
3972d83b0603SJohn Fastabend 	ndm = nlmsg_data(nlh);
3973d83b0603SJohn Fastabend 	ndm->ndm_family  = AF_BRIDGE;
3974d83b0603SJohn Fastabend 	ndm->ndm_pad1	 = 0;
3975d83b0603SJohn Fastabend 	ndm->ndm_pad2    = 0;
3976d83b0603SJohn Fastabend 	ndm->ndm_flags	 = flags;
3977d83b0603SJohn Fastabend 	ndm->ndm_type	 = 0;
3978d83b0603SJohn Fastabend 	ndm->ndm_ifindex = dev->ifindex;
3979b3379041SHubert Sokolowski 	ndm->ndm_state   = ndm_state;
3980d83b0603SJohn Fastabend 
3981d83b0603SJohn Fastabend 	if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
3982d83b0603SJohn Fastabend 		goto nla_put_failure;
39831e53d5bbSHubert Sokolowski 	if (vid)
39841e53d5bbSHubert Sokolowski 		if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
39851e53d5bbSHubert Sokolowski 			goto nla_put_failure;
3986d83b0603SJohn Fastabend 
3987053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
3988053c095aSJohannes Berg 	return 0;
3989d83b0603SJohn Fastabend 
3990d83b0603SJohn Fastabend nla_put_failure:
3991d83b0603SJohn Fastabend 	nlmsg_cancel(skb, nlh);
3992d83b0603SJohn Fastabend 	return -EMSGSIZE;
3993d83b0603SJohn Fastabend }
3994d83b0603SJohn Fastabend 
39953ff661c3SJohn Fastabend static inline size_t rtnl_fdb_nlmsg_size(void)
39963ff661c3SJohn Fastabend {
3997f82ef3e1SSabrina Dubroca 	return NLMSG_ALIGN(sizeof(struct ndmsg)) +
3998f82ef3e1SSabrina Dubroca 	       nla_total_size(ETH_ALEN) +	/* NDA_LLADDR */
3999f82ef3e1SSabrina Dubroca 	       nla_total_size(sizeof(u16)) +	/* NDA_VLAN */
4000f82ef3e1SSabrina Dubroca 	       0;
40013ff661c3SJohn Fastabend }
40023ff661c3SJohn Fastabend 
4003b3379041SHubert Sokolowski static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
4004b3379041SHubert Sokolowski 			    u16 ndm_state)
40053ff661c3SJohn Fastabend {
40063ff661c3SJohn Fastabend 	struct net *net = dev_net(dev);
40073ff661c3SJohn Fastabend 	struct sk_buff *skb;
40083ff661c3SJohn Fastabend 	int err = -ENOBUFS;
40093ff661c3SJohn Fastabend 
40103ff661c3SJohn Fastabend 	skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
40113ff661c3SJohn Fastabend 	if (!skb)
40123ff661c3SJohn Fastabend 		goto errout;
40133ff661c3SJohn Fastabend 
40141e53d5bbSHubert Sokolowski 	err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
4015b3379041SHubert Sokolowski 				      0, 0, type, NTF_SELF, 0, ndm_state);
40163ff661c3SJohn Fastabend 	if (err < 0) {
40173ff661c3SJohn Fastabend 		kfree_skb(skb);
40183ff661c3SJohn Fastabend 		goto errout;
40193ff661c3SJohn Fastabend 	}
40203ff661c3SJohn Fastabend 
40213ff661c3SJohn Fastabend 	rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
40223ff661c3SJohn Fastabend 	return;
40233ff661c3SJohn Fastabend errout:
40243ff661c3SJohn Fastabend 	rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
40253ff661c3SJohn Fastabend }
40263ff661c3SJohn Fastabend 
4027a986967eSBart Van Assche /*
4028090096bfSVlad Yasevich  * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
4029090096bfSVlad Yasevich  */
4030090096bfSVlad Yasevich int ndo_dflt_fdb_add(struct ndmsg *ndm,
4031090096bfSVlad Yasevich 		     struct nlattr *tb[],
4032090096bfSVlad Yasevich 		     struct net_device *dev,
4033f6f6424bSJiri Pirko 		     const unsigned char *addr, u16 vid,
4034090096bfSVlad Yasevich 		     u16 flags)
4035090096bfSVlad Yasevich {
4036090096bfSVlad Yasevich 	int err = -EINVAL;
4037090096bfSVlad Yasevich 
4038090096bfSVlad Yasevich 	/* If aging addresses are supported device will need to
4039090096bfSVlad Yasevich 	 * implement its own handler for this.
4040090096bfSVlad Yasevich 	 */
4041090096bfSVlad Yasevich 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
404278ecc890SVladimir Oltean 		netdev_info(dev, "default FDB implementation only supports local addresses\n");
4043090096bfSVlad Yasevich 		return err;
4044090096bfSVlad Yasevich 	}
4045090096bfSVlad Yasevich 
404665891feaSOr Gerlitz 	if (vid) {
404723ac0b42SVladimir Oltean 		netdev_info(dev, "vlans aren't supported yet for dev_uc|mc_add()\n");
404865891feaSOr Gerlitz 		return err;
404965891feaSOr Gerlitz 	}
405065891feaSOr Gerlitz 
4051090096bfSVlad Yasevich 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
4052090096bfSVlad Yasevich 		err = dev_uc_add_excl(dev, addr);
4053090096bfSVlad Yasevich 	else if (is_multicast_ether_addr(addr))
4054090096bfSVlad Yasevich 		err = dev_mc_add_excl(dev, addr);
4055090096bfSVlad Yasevich 
4056090096bfSVlad Yasevich 	/* Only return duplicate errors if NLM_F_EXCL is set */
4057090096bfSVlad Yasevich 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
4058090096bfSVlad Yasevich 		err = 0;
4059090096bfSVlad Yasevich 
4060090096bfSVlad Yasevich 	return err;
4061090096bfSVlad Yasevich }
4062090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_add);
4063090096bfSVlad Yasevich 
4064b88d12e4SFlorian Westphal static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid,
4065b88d12e4SFlorian Westphal 			 struct netlink_ext_ack *extack)
4066f6f6424bSJiri Pirko {
4067f6f6424bSJiri Pirko 	u16 vid = 0;
4068f6f6424bSJiri Pirko 
4069f6f6424bSJiri Pirko 	if (vlan_attr) {
4070f6f6424bSJiri Pirko 		if (nla_len(vlan_attr) != sizeof(u16)) {
4071b88d12e4SFlorian Westphal 			NL_SET_ERR_MSG(extack, "invalid vlan attribute size");
4072f6f6424bSJiri Pirko 			return -EINVAL;
4073f6f6424bSJiri Pirko 		}
4074f6f6424bSJiri Pirko 
4075f6f6424bSJiri Pirko 		vid = nla_get_u16(vlan_attr);
4076f6f6424bSJiri Pirko 
4077f6f6424bSJiri Pirko 		if (!vid || vid >= VLAN_VID_MASK) {
4078b88d12e4SFlorian Westphal 			NL_SET_ERR_MSG(extack, "invalid vlan id");
4079f6f6424bSJiri Pirko 			return -EINVAL;
4080f6f6424bSJiri Pirko 		}
4081f6f6424bSJiri Pirko 	}
4082f6f6424bSJiri Pirko 	*p_vid = vid;
4083f6f6424bSJiri Pirko 	return 0;
4084f6f6424bSJiri Pirko }
4085f6f6424bSJiri Pirko 
4086c21ef3e3SDavid Ahern static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
4087c21ef3e3SDavid Ahern 			struct netlink_ext_ack *extack)
408877162022SJohn Fastabend {
408977162022SJohn Fastabend 	struct net *net = sock_net(skb->sk);
409077162022SJohn Fastabend 	struct ndmsg *ndm;
409177162022SJohn Fastabend 	struct nlattr *tb[NDA_MAX+1];
409277162022SJohn Fastabend 	struct net_device *dev;
409377162022SJohn Fastabend 	u8 *addr;
4094f6f6424bSJiri Pirko 	u16 vid;
409577162022SJohn Fastabend 	int err;
409677162022SJohn Fastabend 
40978cb08174SJohannes Berg 	err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL,
40988cb08174SJohannes Berg 				     extack);
409977162022SJohn Fastabend 	if (err < 0)
410077162022SJohn Fastabend 		return err;
410177162022SJohn Fastabend 
410277162022SJohn Fastabend 	ndm = nlmsg_data(nlh);
410377162022SJohn Fastabend 	if (ndm->ndm_ifindex == 0) {
4104b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "invalid ifindex");
410577162022SJohn Fastabend 		return -EINVAL;
410677162022SJohn Fastabend 	}
410777162022SJohn Fastabend 
410877162022SJohn Fastabend 	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
410977162022SJohn Fastabend 	if (dev == NULL) {
4110b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "unknown ifindex");
411177162022SJohn Fastabend 		return -ENODEV;
411277162022SJohn Fastabend 	}
411377162022SJohn Fastabend 
411477162022SJohn Fastabend 	if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
4115b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "invalid address");
411677162022SJohn Fastabend 		return -EINVAL;
411777162022SJohn Fastabend 	}
411877162022SJohn Fastabend 
4119da715775SIdo Schimmel 	if (dev->type != ARPHRD_ETHER) {
4120da715775SIdo Schimmel 		NL_SET_ERR_MSG(extack, "FDB add only supported for Ethernet devices");
4121da715775SIdo Schimmel 		return -EINVAL;
4122da715775SIdo Schimmel 	}
4123da715775SIdo Schimmel 
412477162022SJohn Fastabend 	addr = nla_data(tb[NDA_LLADDR]);
412577162022SJohn Fastabend 
4126b88d12e4SFlorian Westphal 	err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
4127f6f6424bSJiri Pirko 	if (err)
4128f6f6424bSJiri Pirko 		return err;
4129f6f6424bSJiri Pirko 
413077162022SJohn Fastabend 	err = -EOPNOTSUPP;
413177162022SJohn Fastabend 
413277162022SJohn Fastabend 	/* Support fdb on master device the net/bridge default case */
413377162022SJohn Fastabend 	if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
41342e92a2d0SJulian Wiedmann 	    netif_is_bridge_port(dev)) {
4135898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4136898e5061SJiri Pirko 		const struct net_device_ops *ops = br_dev->netdev_ops;
4137898e5061SJiri Pirko 
4138f6f6424bSJiri Pirko 		err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
413987b0984eSPetr Machata 				       nlh->nlmsg_flags, extack);
414077162022SJohn Fastabend 		if (err)
414177162022SJohn Fastabend 			goto out;
414277162022SJohn Fastabend 		else
414377162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_MASTER;
414477162022SJohn Fastabend 	}
414577162022SJohn Fastabend 
414677162022SJohn Fastabend 	/* Embedded bridge, macvlan, and any other device support */
4147090096bfSVlad Yasevich 	if ((ndm->ndm_flags & NTF_SELF)) {
4148090096bfSVlad Yasevich 		if (dev->netdev_ops->ndo_fdb_add)
4149090096bfSVlad Yasevich 			err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
4150f6f6424bSJiri Pirko 							   vid,
415187b0984eSPetr Machata 							   nlh->nlmsg_flags,
415287b0984eSPetr Machata 							   extack);
4153090096bfSVlad Yasevich 		else
4154f6f6424bSJiri Pirko 			err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
415577162022SJohn Fastabend 					       nlh->nlmsg_flags);
415677162022SJohn Fastabend 
41573ff661c3SJohn Fastabend 		if (!err) {
4158b3379041SHubert Sokolowski 			rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
4159b3379041SHubert Sokolowski 					ndm->ndm_state);
416077162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_SELF;
416177162022SJohn Fastabend 		}
41623ff661c3SJohn Fastabend 	}
416377162022SJohn Fastabend out:
416477162022SJohn Fastabend 	return err;
416577162022SJohn Fastabend }
416677162022SJohn Fastabend 
4167a986967eSBart Van Assche /*
4168090096bfSVlad Yasevich  * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
4169090096bfSVlad Yasevich  */
4170090096bfSVlad Yasevich int ndo_dflt_fdb_del(struct ndmsg *ndm,
4171090096bfSVlad Yasevich 		     struct nlattr *tb[],
4172090096bfSVlad Yasevich 		     struct net_device *dev,
4173f6f6424bSJiri Pirko 		     const unsigned char *addr, u16 vid)
4174090096bfSVlad Yasevich {
4175c8a89c4aSAlexander Duyck 	int err = -EINVAL;
4176090096bfSVlad Yasevich 
4177090096bfSVlad Yasevich 	/* If aging addresses are supported device will need to
4178090096bfSVlad Yasevich 	 * implement its own handler for this.
4179090096bfSVlad Yasevich 	 */
418064535993SSridhar Samudrala 	if (!(ndm->ndm_state & NUD_PERMANENT)) {
418178ecc890SVladimir Oltean 		netdev_info(dev, "default FDB implementation only supports local addresses\n");
4182c8a89c4aSAlexander Duyck 		return err;
4183090096bfSVlad Yasevich 	}
4184090096bfSVlad Yasevich 
4185090096bfSVlad Yasevich 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
4186090096bfSVlad Yasevich 		err = dev_uc_del(dev, addr);
4187090096bfSVlad Yasevich 	else if (is_multicast_ether_addr(addr))
4188090096bfSVlad Yasevich 		err = dev_mc_del(dev, addr);
4189090096bfSVlad Yasevich 
4190090096bfSVlad Yasevich 	return err;
4191090096bfSVlad Yasevich }
4192090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_del);
4193090096bfSVlad Yasevich 
41949e834259SNikolay Aleksandrov static const struct nla_policy fdb_del_bulk_policy[NDA_MAX + 1] = {
41959e834259SNikolay Aleksandrov 	[NDA_VLAN]	= { .type = NLA_U16 },
41969e834259SNikolay Aleksandrov 	[NDA_IFINDEX]	= NLA_POLICY_MIN(NLA_S32, 1),
4197ea2c0f9eSNikolay Aleksandrov 	[NDA_NDM_STATE_MASK]	= { .type = NLA_U16  },
4198ea2c0f9eSNikolay Aleksandrov 	[NDA_NDM_FLAGS_MASK]	= { .type = NLA_U8 },
41999e834259SNikolay Aleksandrov };
42009e834259SNikolay Aleksandrov 
4201c21ef3e3SDavid Ahern static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
4202c21ef3e3SDavid Ahern 			struct netlink_ext_ack *extack)
420377162022SJohn Fastabend {
42049e834259SNikolay Aleksandrov 	bool del_bulk = !!(nlh->nlmsg_flags & NLM_F_BULK);
420577162022SJohn Fastabend 	struct net *net = sock_net(skb->sk);
42069e834259SNikolay Aleksandrov 	const struct net_device_ops *ops;
420777162022SJohn Fastabend 	struct ndmsg *ndm;
42081690be63SVlad Yasevich 	struct nlattr *tb[NDA_MAX+1];
420977162022SJohn Fastabend 	struct net_device *dev;
42109e834259SNikolay Aleksandrov 	__u8 *addr = NULL;
42117d311801SColin Ian King 	int err;
4212f6f6424bSJiri Pirko 	u16 vid;
421377162022SJohn Fastabend 
421490f62cf3SEric W. Biederman 	if (!netlink_capable(skb, CAP_NET_ADMIN))
42151690be63SVlad Yasevich 		return -EPERM;
42161690be63SVlad Yasevich 
42179e834259SNikolay Aleksandrov 	if (!del_bulk) {
42189e834259SNikolay Aleksandrov 		err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX,
42199e834259SNikolay Aleksandrov 					     NULL, extack);
42209e834259SNikolay Aleksandrov 	} else {
42219e834259SNikolay Aleksandrov 		err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX,
42229e834259SNikolay Aleksandrov 				  fdb_del_bulk_policy, extack);
42239e834259SNikolay Aleksandrov 	}
42241690be63SVlad Yasevich 	if (err < 0)
42251690be63SVlad Yasevich 		return err;
422677162022SJohn Fastabend 
422777162022SJohn Fastabend 	ndm = nlmsg_data(nlh);
422877162022SJohn Fastabend 	if (ndm->ndm_ifindex == 0) {
4229b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "invalid ifindex");
423077162022SJohn Fastabend 		return -EINVAL;
423177162022SJohn Fastabend 	}
423277162022SJohn Fastabend 
423377162022SJohn Fastabend 	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
423477162022SJohn Fastabend 	if (dev == NULL) {
4235b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "unknown ifindex");
423677162022SJohn Fastabend 		return -ENODEV;
423777162022SJohn Fastabend 	}
423877162022SJohn Fastabend 
42399e834259SNikolay Aleksandrov 	if (!del_bulk) {
42401690be63SVlad Yasevich 		if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
4241b88d12e4SFlorian Westphal 			NL_SET_ERR_MSG(extack, "invalid address");
424277162022SJohn Fastabend 			return -EINVAL;
424377162022SJohn Fastabend 		}
42449e834259SNikolay Aleksandrov 		addr = nla_data(tb[NDA_LLADDR]);
42459e834259SNikolay Aleksandrov 	}
424677162022SJohn Fastabend 
4247da715775SIdo Schimmel 	if (dev->type != ARPHRD_ETHER) {
4248da715775SIdo Schimmel 		NL_SET_ERR_MSG(extack, "FDB delete only supported for Ethernet devices");
4249da715775SIdo Schimmel 		return -EINVAL;
4250da715775SIdo Schimmel 	}
4251da715775SIdo Schimmel 
4252b88d12e4SFlorian Westphal 	err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
4253f6f6424bSJiri Pirko 	if (err)
4254f6f6424bSJiri Pirko 		return err;
4255f6f6424bSJiri Pirko 
425677162022SJohn Fastabend 	err = -EOPNOTSUPP;
425777162022SJohn Fastabend 
425877162022SJohn Fastabend 	/* Support fdb on master device the net/bridge default case */
425977162022SJohn Fastabend 	if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
42602e92a2d0SJulian Wiedmann 	    netif_is_bridge_port(dev)) {
4261898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
426277162022SJohn Fastabend 
42639e834259SNikolay Aleksandrov 		ops = br_dev->netdev_ops;
42649e834259SNikolay Aleksandrov 		if (!del_bulk) {
4265898e5061SJiri Pirko 			if (ops->ndo_fdb_del)
4266f6f6424bSJiri Pirko 				err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
42679e834259SNikolay Aleksandrov 		} else {
42689e834259SNikolay Aleksandrov 			if (ops->ndo_fdb_del_bulk)
42699e834259SNikolay Aleksandrov 				err = ops->ndo_fdb_del_bulk(ndm, tb, dev, vid,
42709e834259SNikolay Aleksandrov 							    extack);
42719e834259SNikolay Aleksandrov 		}
427277162022SJohn Fastabend 
427377162022SJohn Fastabend 		if (err)
427477162022SJohn Fastabend 			goto out;
427577162022SJohn Fastabend 		else
427677162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_MASTER;
427777162022SJohn Fastabend 	}
427877162022SJohn Fastabend 
427977162022SJohn Fastabend 	/* Embedded bridge, macvlan, and any other device support */
4280090096bfSVlad Yasevich 	if (ndm->ndm_flags & NTF_SELF) {
42819e834259SNikolay Aleksandrov 		ops = dev->netdev_ops;
42829e834259SNikolay Aleksandrov 		if (!del_bulk) {
42839e834259SNikolay Aleksandrov 			if (ops->ndo_fdb_del)
42849e834259SNikolay Aleksandrov 				err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
4285090096bfSVlad Yasevich 			else
4286f6f6424bSJiri Pirko 				err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
42879e834259SNikolay Aleksandrov 		} else {
42889e834259SNikolay Aleksandrov 			/* in case err was cleared by NTF_MASTER call */
42899e834259SNikolay Aleksandrov 			err = -EOPNOTSUPP;
42909e834259SNikolay Aleksandrov 			if (ops->ndo_fdb_del_bulk)
42919e834259SNikolay Aleksandrov 				err = ops->ndo_fdb_del_bulk(ndm, tb, dev, vid,
42929e834259SNikolay Aleksandrov 							    extack);
42939e834259SNikolay Aleksandrov 		}
429477162022SJohn Fastabend 
42953ff661c3SJohn Fastabend 		if (!err) {
42969e834259SNikolay Aleksandrov 			if (!del_bulk)
4297b3379041SHubert Sokolowski 				rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
4298b3379041SHubert Sokolowski 						ndm->ndm_state);
429977162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_SELF;
430077162022SJohn Fastabend 		}
43013ff661c3SJohn Fastabend 	}
430277162022SJohn Fastabend out:
430377162022SJohn Fastabend 	return err;
430477162022SJohn Fastabend }
430577162022SJohn Fastabend 
4306d83b0603SJohn Fastabend static int nlmsg_populate_fdb(struct sk_buff *skb,
4307d83b0603SJohn Fastabend 			      struct netlink_callback *cb,
4308d83b0603SJohn Fastabend 			      struct net_device *dev,
4309d83b0603SJohn Fastabend 			      int *idx,
4310d83b0603SJohn Fastabend 			      struct netdev_hw_addr_list *list)
4311d83b0603SJohn Fastabend {
4312d83b0603SJohn Fastabend 	struct netdev_hw_addr *ha;
4313d83b0603SJohn Fastabend 	int err;
431415e47304SEric W. Biederman 	u32 portid, seq;
4315d83b0603SJohn Fastabend 
431615e47304SEric W. Biederman 	portid = NETLINK_CB(cb->skb).portid;
4317d83b0603SJohn Fastabend 	seq = cb->nlh->nlmsg_seq;
4318d83b0603SJohn Fastabend 
4319d83b0603SJohn Fastabend 	list_for_each_entry(ha, &list->list, list) {
4320d297653dSRoopa Prabhu 		if (*idx < cb->args[2])
4321d83b0603SJohn Fastabend 			goto skip;
4322d83b0603SJohn Fastabend 
43231e53d5bbSHubert Sokolowski 		err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
4324a7a558feSJohn Fastabend 					      portid, seq,
43251c104a6bSNicolas Dichtel 					      RTM_NEWNEIGH, NTF_SELF,
4326b3379041SHubert Sokolowski 					      NLM_F_MULTI, NUD_PERMANENT);
4327d83b0603SJohn Fastabend 		if (err < 0)
4328d83b0603SJohn Fastabend 			return err;
4329d83b0603SJohn Fastabend skip:
4330d83b0603SJohn Fastabend 		*idx += 1;
4331d83b0603SJohn Fastabend 	}
4332d83b0603SJohn Fastabend 	return 0;
4333d83b0603SJohn Fastabend }
4334d83b0603SJohn Fastabend 
4335d83b0603SJohn Fastabend /**
43362c53040fSBen Hutchings  * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
4337a986967eSBart Van Assche  * @skb: socket buffer to store message in
4338a986967eSBart Van Assche  * @cb: netlink callback
4339d83b0603SJohn Fastabend  * @dev: netdevice
4340a986967eSBart Van Assche  * @filter_dev: ignored
4341a986967eSBart Van Assche  * @idx: the number of FDB table entries dumped is added to *@idx
4342d83b0603SJohn Fastabend  *
4343d83b0603SJohn Fastabend  * Default netdevice operation to dump the existing unicast address list.
434491f3e7b1SJohn Fastabend  * Returns number of addresses from list put in skb.
4345d83b0603SJohn Fastabend  */
4346d83b0603SJohn Fastabend int ndo_dflt_fdb_dump(struct sk_buff *skb,
4347d83b0603SJohn Fastabend 		      struct netlink_callback *cb,
4348d83b0603SJohn Fastabend 		      struct net_device *dev,
43495d5eacb3SJamal Hadi Salim 		      struct net_device *filter_dev,
4350d297653dSRoopa Prabhu 		      int *idx)
4351d83b0603SJohn Fastabend {
4352d83b0603SJohn Fastabend 	int err;
4353d83b0603SJohn Fastabend 
435468883893SEric Dumazet 	if (dev->type != ARPHRD_ETHER)
435568883893SEric Dumazet 		return -EINVAL;
435668883893SEric Dumazet 
4357d83b0603SJohn Fastabend 	netif_addr_lock_bh(dev);
4358d297653dSRoopa Prabhu 	err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
4359d83b0603SJohn Fastabend 	if (err)
4360d83b0603SJohn Fastabend 		goto out;
43612934c9dbSZhang Shengju 	err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
4362d83b0603SJohn Fastabend out:
4363d83b0603SJohn Fastabend 	netif_addr_unlock_bh(dev);
4364d297653dSRoopa Prabhu 	return err;
4365d83b0603SJohn Fastabend }
4366d83b0603SJohn Fastabend EXPORT_SYMBOL(ndo_dflt_fdb_dump);
4367d83b0603SJohn Fastabend 
43688c6e137fSDavid Ahern static int valid_fdb_dump_strict(const struct nlmsghdr *nlh,
43698c6e137fSDavid Ahern 				 int *br_idx, int *brport_idx,
43708c6e137fSDavid Ahern 				 struct netlink_ext_ack *extack)
43718c6e137fSDavid Ahern {
43728c6e137fSDavid Ahern 	struct nlattr *tb[NDA_MAX + 1];
43738c6e137fSDavid Ahern 	struct ndmsg *ndm;
43748c6e137fSDavid Ahern 	int err, i;
43758c6e137fSDavid Ahern 
43768c6e137fSDavid Ahern 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
43778c6e137fSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid header for fdb dump request");
43788c6e137fSDavid Ahern 		return -EINVAL;
43798c6e137fSDavid Ahern 	}
43808c6e137fSDavid Ahern 
43818c6e137fSDavid Ahern 	ndm = nlmsg_data(nlh);
43828c6e137fSDavid Ahern 	if (ndm->ndm_pad1  || ndm->ndm_pad2  || ndm->ndm_state ||
43838c6e137fSDavid Ahern 	    ndm->ndm_flags || ndm->ndm_type) {
43848b73018fSNikolay Aleksandrov 		NL_SET_ERR_MSG(extack, "Invalid values in header for fdb dump request");
43858c6e137fSDavid Ahern 		return -EINVAL;
43868c6e137fSDavid Ahern 	}
43878c6e137fSDavid Ahern 
43888cb08174SJohannes Berg 	err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
43898cb08174SJohannes Berg 					    NDA_MAX, NULL, extack);
43908c6e137fSDavid Ahern 	if (err < 0)
43918c6e137fSDavid Ahern 		return err;
43928c6e137fSDavid Ahern 
43938c6e137fSDavid Ahern 	*brport_idx = ndm->ndm_ifindex;
43948c6e137fSDavid Ahern 	for (i = 0; i <= NDA_MAX; ++i) {
43958c6e137fSDavid Ahern 		if (!tb[i])
43968c6e137fSDavid Ahern 			continue;
43978c6e137fSDavid Ahern 
43988c6e137fSDavid Ahern 		switch (i) {
43998c6e137fSDavid Ahern 		case NDA_IFINDEX:
44008c6e137fSDavid Ahern 			if (nla_len(tb[i]) != sizeof(u32)) {
44018c6e137fSDavid Ahern 				NL_SET_ERR_MSG(extack, "Invalid IFINDEX attribute in fdb dump request");
44028c6e137fSDavid Ahern 				return -EINVAL;
44038c6e137fSDavid Ahern 			}
44048c6e137fSDavid Ahern 			*brport_idx = nla_get_u32(tb[NDA_IFINDEX]);
44058c6e137fSDavid Ahern 			break;
44068c6e137fSDavid Ahern 		case NDA_MASTER:
44078c6e137fSDavid Ahern 			if (nla_len(tb[i]) != sizeof(u32)) {
44088c6e137fSDavid Ahern 				NL_SET_ERR_MSG(extack, "Invalid MASTER attribute in fdb dump request");
44098c6e137fSDavid Ahern 				return -EINVAL;
44108c6e137fSDavid Ahern 			}
44118c6e137fSDavid Ahern 			*br_idx = nla_get_u32(tb[NDA_MASTER]);
44128c6e137fSDavid Ahern 			break;
44138c6e137fSDavid Ahern 		default:
44148c6e137fSDavid Ahern 			NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb dump request");
44158c6e137fSDavid Ahern 			return -EINVAL;
44168c6e137fSDavid Ahern 		}
44178c6e137fSDavid Ahern 	}
44188c6e137fSDavid Ahern 
44198c6e137fSDavid Ahern 	return 0;
44208c6e137fSDavid Ahern }
44218c6e137fSDavid Ahern 
44228dfbda19SDavid Ahern static int valid_fdb_dump_legacy(const struct nlmsghdr *nlh,
44238dfbda19SDavid Ahern 				 int *br_idx, int *brport_idx,
44248dfbda19SDavid Ahern 				 struct netlink_ext_ack *extack)
44258dfbda19SDavid Ahern {
44268dfbda19SDavid Ahern 	struct nlattr *tb[IFLA_MAX+1];
44278dfbda19SDavid Ahern 	int err;
44288dfbda19SDavid Ahern 
44298dfbda19SDavid Ahern 	/* A hack to preserve kernel<->userspace interface.
44308dfbda19SDavid Ahern 	 * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0.
44318dfbda19SDavid Ahern 	 * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails.
44328dfbda19SDavid Ahern 	 * So, check for ndmsg with an optional u32 attribute (not used here).
44338dfbda19SDavid Ahern 	 * Fortunately these sizes don't conflict with the size of ifinfomsg
44348dfbda19SDavid Ahern 	 * with an optional attribute.
44358dfbda19SDavid Ahern 	 */
44368dfbda19SDavid Ahern 	if (nlmsg_len(nlh) != sizeof(struct ndmsg) &&
44378dfbda19SDavid Ahern 	    (nlmsg_len(nlh) != sizeof(struct ndmsg) +
44388dfbda19SDavid Ahern 	     nla_attr_size(sizeof(u32)))) {
44394565d7e5SDavid Ahern 		struct ifinfomsg *ifm;
44404565d7e5SDavid Ahern 
44418cb08174SJohannes Berg 		err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
44428cb08174SJohannes Berg 					     tb, IFLA_MAX, ifla_policy,
44438cb08174SJohannes Berg 					     extack);
44448dfbda19SDavid Ahern 		if (err < 0) {
44458dfbda19SDavid Ahern 			return -EINVAL;
44468dfbda19SDavid Ahern 		} else if (err == 0) {
44478dfbda19SDavid Ahern 			if (tb[IFLA_MASTER])
44488dfbda19SDavid Ahern 				*br_idx = nla_get_u32(tb[IFLA_MASTER]);
44498dfbda19SDavid Ahern 		}
44508dfbda19SDavid Ahern 
44514565d7e5SDavid Ahern 		ifm = nlmsg_data(nlh);
44528dfbda19SDavid Ahern 		*brport_idx = ifm->ifi_index;
44538dfbda19SDavid Ahern 	}
44548dfbda19SDavid Ahern 	return 0;
44558dfbda19SDavid Ahern }
44568dfbda19SDavid Ahern 
445777162022SJohn Fastabend static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
445877162022SJohn Fastabend {
445977162022SJohn Fastabend 	struct net_device *dev;
44605e6d2435SJamal Hadi Salim 	struct net_device *br_dev = NULL;
44615e6d2435SJamal Hadi Salim 	const struct net_device_ops *ops = NULL;
44625e6d2435SJamal Hadi Salim 	const struct net_device_ops *cops = NULL;
44635e6d2435SJamal Hadi Salim 	struct net *net = sock_net(skb->sk);
4464d297653dSRoopa Prabhu 	struct hlist_head *head;
44655e6d2435SJamal Hadi Salim 	int brport_idx = 0;
44665e6d2435SJamal Hadi Salim 	int br_idx = 0;
4467d297653dSRoopa Prabhu 	int h, s_h;
4468d297653dSRoopa Prabhu 	int idx = 0, s_idx;
4469d297653dSRoopa Prabhu 	int err = 0;
4470d297653dSRoopa Prabhu 	int fidx = 0;
447177162022SJohn Fastabend 
44728c6e137fSDavid Ahern 	if (cb->strict_check)
44738c6e137fSDavid Ahern 		err = valid_fdb_dump_strict(cb->nlh, &br_idx, &brport_idx,
44748c6e137fSDavid Ahern 					    cb->extack);
44758c6e137fSDavid Ahern 	else
44768dfbda19SDavid Ahern 		err = valid_fdb_dump_legacy(cb->nlh, &br_idx, &brport_idx,
44778dfbda19SDavid Ahern 					    cb->extack);
44788dfbda19SDavid Ahern 	if (err < 0)
44798dfbda19SDavid Ahern 		return err;
44805e6d2435SJamal Hadi Salim 
44815e6d2435SJamal Hadi Salim 	if (br_idx) {
44825e6d2435SJamal Hadi Salim 		br_dev = __dev_get_by_index(net, br_idx);
44835e6d2435SJamal Hadi Salim 		if (!br_dev)
44845e6d2435SJamal Hadi Salim 			return -ENODEV;
44855e6d2435SJamal Hadi Salim 
4486898e5061SJiri Pirko 		ops = br_dev->netdev_ops;
44875e6d2435SJamal Hadi Salim 	}
44885e6d2435SJamal Hadi Salim 
4489d297653dSRoopa Prabhu 	s_h = cb->args[0];
4490d297653dSRoopa Prabhu 	s_idx = cb->args[1];
4491d297653dSRoopa Prabhu 
4492d297653dSRoopa Prabhu 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4493d297653dSRoopa Prabhu 		idx = 0;
4494d297653dSRoopa Prabhu 		head = &net->dev_index_head[h];
4495d297653dSRoopa Prabhu 		hlist_for_each_entry(dev, head, index_hlist) {
4496d297653dSRoopa Prabhu 
44975e6d2435SJamal Hadi Salim 			if (brport_idx && (dev->ifindex != brport_idx))
44985e6d2435SJamal Hadi Salim 				continue;
44995e6d2435SJamal Hadi Salim 
45005e6d2435SJamal Hadi Salim 			if (!br_idx) { /* user did not specify a specific bridge */
45012e92a2d0SJulian Wiedmann 				if (netif_is_bridge_port(dev)) {
45025e6d2435SJamal Hadi Salim 					br_dev = netdev_master_upper_dev_get(dev);
45035e6d2435SJamal Hadi Salim 					cops = br_dev->netdev_ops;
45045e6d2435SJamal Hadi Salim 				}
45055e6d2435SJamal Hadi Salim 			} else {
45065e6d2435SJamal Hadi Salim 				if (dev != br_dev &&
45072e92a2d0SJulian Wiedmann 				    !netif_is_bridge_port(dev))
45085e6d2435SJamal Hadi Salim 					continue;
45095e6d2435SJamal Hadi Salim 
45105e6d2435SJamal Hadi Salim 				if (br_dev != netdev_master_upper_dev_get(dev) &&
4511254ec036SKyungrok Chung 				    !netif_is_bridge_master(dev))
45125e6d2435SJamal Hadi Salim 					continue;
45135e6d2435SJamal Hadi Salim 				cops = ops;
45145e6d2435SJamal Hadi Salim 			}
45155e6d2435SJamal Hadi Salim 
4516d297653dSRoopa Prabhu 			if (idx < s_idx)
4517d297653dSRoopa Prabhu 				goto cont;
4518d297653dSRoopa Prabhu 
45192e92a2d0SJulian Wiedmann 			if (netif_is_bridge_port(dev)) {
4520d297653dSRoopa Prabhu 				if (cops && cops->ndo_fdb_dump) {
4521d297653dSRoopa Prabhu 					err = cops->ndo_fdb_dump(skb, cb,
4522d297653dSRoopa Prabhu 								br_dev, dev,
4523d297653dSRoopa Prabhu 								&fidx);
4524d297653dSRoopa Prabhu 					if (err == -EMSGSIZE)
4525d297653dSRoopa Prabhu 						goto out;
452677162022SJohn Fastabend 				}
4527d297653dSRoopa Prabhu 			}
452877162022SJohn Fastabend 
45295e6d2435SJamal Hadi Salim 			if (dev->netdev_ops->ndo_fdb_dump)
4530d297653dSRoopa Prabhu 				err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
4531d297653dSRoopa Prabhu 								    dev, NULL,
4532d297653dSRoopa Prabhu 								    &fidx);
45336cb69742SHubert Sokolowski 			else
4534d297653dSRoopa Prabhu 				err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
4535d297653dSRoopa Prabhu 							&fidx);
4536d297653dSRoopa Prabhu 			if (err == -EMSGSIZE)
4537d297653dSRoopa Prabhu 				goto out;
45385e6d2435SJamal Hadi Salim 
45395e6d2435SJamal Hadi Salim 			cops = NULL;
4540d297653dSRoopa Prabhu 
4541d297653dSRoopa Prabhu 			/* reset fdb offset to 0 for rest of the interfaces */
4542d297653dSRoopa Prabhu 			cb->args[2] = 0;
4543d297653dSRoopa Prabhu 			fidx = 0;
4544d297653dSRoopa Prabhu cont:
4545d297653dSRoopa Prabhu 			idx++;
4546d297653dSRoopa Prabhu 		}
454777162022SJohn Fastabend 	}
454877162022SJohn Fastabend 
4549d297653dSRoopa Prabhu out:
4550d297653dSRoopa Prabhu 	cb->args[0] = h;
4551d297653dSRoopa Prabhu 	cb->args[1] = idx;
4552d297653dSRoopa Prabhu 	cb->args[2] = fidx;
4553d297653dSRoopa Prabhu 
455477162022SJohn Fastabend 	return skb->len;
455577162022SJohn Fastabend }
455677162022SJohn Fastabend 
45575b2f94b2SRoopa Prabhu static int valid_fdb_get_strict(const struct nlmsghdr *nlh,
45585b2f94b2SRoopa Prabhu 				struct nlattr **tb, u8 *ndm_flags,
45595b2f94b2SRoopa Prabhu 				int *br_idx, int *brport_idx, u8 **addr,
45605b2f94b2SRoopa Prabhu 				u16 *vid, struct netlink_ext_ack *extack)
45615b2f94b2SRoopa Prabhu {
45625b2f94b2SRoopa Prabhu 	struct ndmsg *ndm;
45635b2f94b2SRoopa Prabhu 	int err, i;
45645b2f94b2SRoopa Prabhu 
45655b2f94b2SRoopa Prabhu 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
45665b2f94b2SRoopa Prabhu 		NL_SET_ERR_MSG(extack, "Invalid header for fdb get request");
45675b2f94b2SRoopa Prabhu 		return -EINVAL;
45685b2f94b2SRoopa Prabhu 	}
45695b2f94b2SRoopa Prabhu 
45705b2f94b2SRoopa Prabhu 	ndm = nlmsg_data(nlh);
45715b2f94b2SRoopa Prabhu 	if (ndm->ndm_pad1  || ndm->ndm_pad2  || ndm->ndm_state ||
45725b2f94b2SRoopa Prabhu 	    ndm->ndm_type) {
45735b2f94b2SRoopa Prabhu 		NL_SET_ERR_MSG(extack, "Invalid values in header for fdb get request");
45745b2f94b2SRoopa Prabhu 		return -EINVAL;
45755b2f94b2SRoopa Prabhu 	}
45765b2f94b2SRoopa Prabhu 
45775b2f94b2SRoopa Prabhu 	if (ndm->ndm_flags & ~(NTF_MASTER | NTF_SELF)) {
45785b2f94b2SRoopa Prabhu 		NL_SET_ERR_MSG(extack, "Invalid flags in header for fdb get request");
45795b2f94b2SRoopa Prabhu 		return -EINVAL;
45805b2f94b2SRoopa Prabhu 	}
45815b2f94b2SRoopa Prabhu 
45828cb08174SJohannes Berg 	err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
45838cb08174SJohannes Berg 					    NDA_MAX, nda_policy, extack);
45845b2f94b2SRoopa Prabhu 	if (err < 0)
45855b2f94b2SRoopa Prabhu 		return err;
45865b2f94b2SRoopa Prabhu 
45875b2f94b2SRoopa Prabhu 	*ndm_flags = ndm->ndm_flags;
45885b2f94b2SRoopa Prabhu 	*brport_idx = ndm->ndm_ifindex;
45895b2f94b2SRoopa Prabhu 	for (i = 0; i <= NDA_MAX; ++i) {
45905b2f94b2SRoopa Prabhu 		if (!tb[i])
45915b2f94b2SRoopa Prabhu 			continue;
45925b2f94b2SRoopa Prabhu 
45935b2f94b2SRoopa Prabhu 		switch (i) {
45945b2f94b2SRoopa Prabhu 		case NDA_MASTER:
45955b2f94b2SRoopa Prabhu 			*br_idx = nla_get_u32(tb[i]);
45965b2f94b2SRoopa Prabhu 			break;
45975b2f94b2SRoopa Prabhu 		case NDA_LLADDR:
45985b2f94b2SRoopa Prabhu 			if (nla_len(tb[i]) != ETH_ALEN) {
45995b2f94b2SRoopa Prabhu 				NL_SET_ERR_MSG(extack, "Invalid address in fdb get request");
46005b2f94b2SRoopa Prabhu 				return -EINVAL;
46015b2f94b2SRoopa Prabhu 			}
46025b2f94b2SRoopa Prabhu 			*addr = nla_data(tb[i]);
46035b2f94b2SRoopa Prabhu 			break;
46045b2f94b2SRoopa Prabhu 		case NDA_VLAN:
46055b2f94b2SRoopa Prabhu 			err = fdb_vid_parse(tb[i], vid, extack);
46065b2f94b2SRoopa Prabhu 			if (err)
46075b2f94b2SRoopa Prabhu 				return err;
46085b2f94b2SRoopa Prabhu 			break;
46095b2f94b2SRoopa Prabhu 		case NDA_VNI:
46105b2f94b2SRoopa Prabhu 			break;
46115b2f94b2SRoopa Prabhu 		default:
46125b2f94b2SRoopa Prabhu 			NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb get request");
46135b2f94b2SRoopa Prabhu 			return -EINVAL;
46145b2f94b2SRoopa Prabhu 		}
46155b2f94b2SRoopa Prabhu 	}
46165b2f94b2SRoopa Prabhu 
46175b2f94b2SRoopa Prabhu 	return 0;
46185b2f94b2SRoopa Prabhu }
46195b2f94b2SRoopa Prabhu 
46205b2f94b2SRoopa Prabhu static int rtnl_fdb_get(struct sk_buff *in_skb, struct nlmsghdr *nlh,
46215b2f94b2SRoopa Prabhu 			struct netlink_ext_ack *extack)
46225b2f94b2SRoopa Prabhu {
46235b2f94b2SRoopa Prabhu 	struct net_device *dev = NULL, *br_dev = NULL;
46245b2f94b2SRoopa Prabhu 	const struct net_device_ops *ops = NULL;
46255b2f94b2SRoopa Prabhu 	struct net *net = sock_net(in_skb->sk);
46265b2f94b2SRoopa Prabhu 	struct nlattr *tb[NDA_MAX + 1];
46275b2f94b2SRoopa Prabhu 	struct sk_buff *skb;
46285b2f94b2SRoopa Prabhu 	int brport_idx = 0;
46295b2f94b2SRoopa Prabhu 	u8 ndm_flags = 0;
46305b2f94b2SRoopa Prabhu 	int br_idx = 0;
46315b2f94b2SRoopa Prabhu 	u8 *addr = NULL;
46325b2f94b2SRoopa Prabhu 	u16 vid = 0;
46335b2f94b2SRoopa Prabhu 	int err;
46345b2f94b2SRoopa Prabhu 
46355b2f94b2SRoopa Prabhu 	err = valid_fdb_get_strict(nlh, tb, &ndm_flags, &br_idx,
46365b2f94b2SRoopa Prabhu 				   &brport_idx, &addr, &vid, extack);
46375b2f94b2SRoopa Prabhu 	if (err < 0)
46385b2f94b2SRoopa Prabhu 		return err;
46395b2f94b2SRoopa Prabhu 
4640f989d03eSNikolay Aleksandrov 	if (!addr) {
4641f989d03eSNikolay Aleksandrov 		NL_SET_ERR_MSG(extack, "Missing lookup address for fdb get request");
4642f989d03eSNikolay Aleksandrov 		return -EINVAL;
4643f989d03eSNikolay Aleksandrov 	}
4644f989d03eSNikolay Aleksandrov 
46455b2f94b2SRoopa Prabhu 	if (brport_idx) {
46465b2f94b2SRoopa Prabhu 		dev = __dev_get_by_index(net, brport_idx);
46475b2f94b2SRoopa Prabhu 		if (!dev) {
46485b2f94b2SRoopa Prabhu 			NL_SET_ERR_MSG(extack, "Unknown device ifindex");
46495b2f94b2SRoopa Prabhu 			return -ENODEV;
46505b2f94b2SRoopa Prabhu 		}
46515b2f94b2SRoopa Prabhu 	}
46525b2f94b2SRoopa Prabhu 
46535b2f94b2SRoopa Prabhu 	if (br_idx) {
46545b2f94b2SRoopa Prabhu 		if (dev) {
46555b2f94b2SRoopa Prabhu 			NL_SET_ERR_MSG(extack, "Master and device are mutually exclusive");
46565b2f94b2SRoopa Prabhu 			return -EINVAL;
46575b2f94b2SRoopa Prabhu 		}
46585b2f94b2SRoopa Prabhu 
46595b2f94b2SRoopa Prabhu 		br_dev = __dev_get_by_index(net, br_idx);
46605b2f94b2SRoopa Prabhu 		if (!br_dev) {
46615b2f94b2SRoopa Prabhu 			NL_SET_ERR_MSG(extack, "Invalid master ifindex");
46625b2f94b2SRoopa Prabhu 			return -EINVAL;
46635b2f94b2SRoopa Prabhu 		}
46645b2f94b2SRoopa Prabhu 		ops = br_dev->netdev_ops;
46655b2f94b2SRoopa Prabhu 	}
46665b2f94b2SRoopa Prabhu 
46675b2f94b2SRoopa Prabhu 	if (dev) {
46685b2f94b2SRoopa Prabhu 		if (!ndm_flags || (ndm_flags & NTF_MASTER)) {
46692e92a2d0SJulian Wiedmann 			if (!netif_is_bridge_port(dev)) {
46705b2f94b2SRoopa Prabhu 				NL_SET_ERR_MSG(extack, "Device is not a bridge port");
46715b2f94b2SRoopa Prabhu 				return -EINVAL;
46725b2f94b2SRoopa Prabhu 			}
46735b2f94b2SRoopa Prabhu 			br_dev = netdev_master_upper_dev_get(dev);
46745b2f94b2SRoopa Prabhu 			if (!br_dev) {
46755b2f94b2SRoopa Prabhu 				NL_SET_ERR_MSG(extack, "Master of device not found");
46765b2f94b2SRoopa Prabhu 				return -EINVAL;
46775b2f94b2SRoopa Prabhu 			}
46785b2f94b2SRoopa Prabhu 			ops = br_dev->netdev_ops;
46795b2f94b2SRoopa Prabhu 		} else {
46805b2f94b2SRoopa Prabhu 			if (!(ndm_flags & NTF_SELF)) {
46815b2f94b2SRoopa Prabhu 				NL_SET_ERR_MSG(extack, "Missing NTF_SELF");
46825b2f94b2SRoopa Prabhu 				return -EINVAL;
46835b2f94b2SRoopa Prabhu 			}
46845b2f94b2SRoopa Prabhu 			ops = dev->netdev_ops;
46855b2f94b2SRoopa Prabhu 		}
46865b2f94b2SRoopa Prabhu 	}
46875b2f94b2SRoopa Prabhu 
46885b2f94b2SRoopa Prabhu 	if (!br_dev && !dev) {
46895b2f94b2SRoopa Prabhu 		NL_SET_ERR_MSG(extack, "No device specified");
46905b2f94b2SRoopa Prabhu 		return -ENODEV;
46915b2f94b2SRoopa Prabhu 	}
46925b2f94b2SRoopa Prabhu 
46935b2f94b2SRoopa Prabhu 	if (!ops || !ops->ndo_fdb_get) {
46945b2f94b2SRoopa Prabhu 		NL_SET_ERR_MSG(extack, "Fdb get operation not supported by device");
46955b2f94b2SRoopa Prabhu 		return -EOPNOTSUPP;
46965b2f94b2SRoopa Prabhu 	}
46975b2f94b2SRoopa Prabhu 
46985b2f94b2SRoopa Prabhu 	skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
46995b2f94b2SRoopa Prabhu 	if (!skb)
47005b2f94b2SRoopa Prabhu 		return -ENOBUFS;
47015b2f94b2SRoopa Prabhu 
47025b2f94b2SRoopa Prabhu 	if (br_dev)
47035b2f94b2SRoopa Prabhu 		dev = br_dev;
47045b2f94b2SRoopa Prabhu 	err = ops->ndo_fdb_get(skb, tb, dev, addr, vid,
47055b2f94b2SRoopa Prabhu 			       NETLINK_CB(in_skb).portid,
47065b2f94b2SRoopa Prabhu 			       nlh->nlmsg_seq, extack);
47075b2f94b2SRoopa Prabhu 	if (err)
47085b2f94b2SRoopa Prabhu 		goto out;
47095b2f94b2SRoopa Prabhu 
47105b2f94b2SRoopa Prabhu 	return rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
47115b2f94b2SRoopa Prabhu out:
47125b2f94b2SRoopa Prabhu 	kfree_skb(skb);
47135b2f94b2SRoopa Prabhu 	return err;
47145b2f94b2SRoopa Prabhu }
47155b2f94b2SRoopa Prabhu 
47162c3c031cSScott Feldman static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
47172c3c031cSScott Feldman 			       unsigned int attrnum, unsigned int flag)
47182c3c031cSScott Feldman {
47192c3c031cSScott Feldman 	if (mask & flag)
47202c3c031cSScott Feldman 		return nla_put_u8(skb, attrnum, !!(flags & flag));
47212c3c031cSScott Feldman 	return 0;
47222c3c031cSScott Feldman }
47232c3c031cSScott Feldman 
4724815cccbfSJohn Fastabend int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
47252c3c031cSScott Feldman 			    struct net_device *dev, u16 mode,
47267d4f8d87SScott Feldman 			    u32 flags, u32 mask, int nlflags,
47277d4f8d87SScott Feldman 			    u32 filter_mask,
47287d4f8d87SScott Feldman 			    int (*vlan_fill)(struct sk_buff *skb,
47297d4f8d87SScott Feldman 					     struct net_device *dev,
47307d4f8d87SScott Feldman 					     u32 filter_mask))
4731815cccbfSJohn Fastabend {
4732815cccbfSJohn Fastabend 	struct nlmsghdr *nlh;
4733815cccbfSJohn Fastabend 	struct ifinfomsg *ifm;
4734815cccbfSJohn Fastabend 	struct nlattr *br_afspec;
47352c3c031cSScott Feldman 	struct nlattr *protinfo;
4736815cccbfSJohn Fastabend 	u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
4737898e5061SJiri Pirko 	struct net_device *br_dev = netdev_master_upper_dev_get(dev);
47387d4f8d87SScott Feldman 	int err = 0;
4739815cccbfSJohn Fastabend 
474046c264daSNicolas Dichtel 	nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
4741815cccbfSJohn Fastabend 	if (nlh == NULL)
4742815cccbfSJohn Fastabend 		return -EMSGSIZE;
4743815cccbfSJohn Fastabend 
4744815cccbfSJohn Fastabend 	ifm = nlmsg_data(nlh);
4745815cccbfSJohn Fastabend 	ifm->ifi_family = AF_BRIDGE;
4746815cccbfSJohn Fastabend 	ifm->__ifi_pad = 0;
4747815cccbfSJohn Fastabend 	ifm->ifi_type = dev->type;
4748815cccbfSJohn Fastabend 	ifm->ifi_index = dev->ifindex;
4749815cccbfSJohn Fastabend 	ifm->ifi_flags = dev_get_flags(dev);
4750815cccbfSJohn Fastabend 	ifm->ifi_change = 0;
4751815cccbfSJohn Fastabend 
4752815cccbfSJohn Fastabend 
4753815cccbfSJohn Fastabend 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
4754815cccbfSJohn Fastabend 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
4755815cccbfSJohn Fastabend 	    nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
4756898e5061SJiri Pirko 	    (br_dev &&
4757898e5061SJiri Pirko 	     nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
4758815cccbfSJohn Fastabend 	    (dev->addr_len &&
4759815cccbfSJohn Fastabend 	     nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
4760a54acb3aSNicolas Dichtel 	    (dev->ifindex != dev_get_iflink(dev) &&
4761a54acb3aSNicolas Dichtel 	     nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
4762815cccbfSJohn Fastabend 		goto nla_put_failure;
4763815cccbfSJohn Fastabend 
4764ae0be8deSMichal Kubecek 	br_afspec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
4765815cccbfSJohn Fastabend 	if (!br_afspec)
4766815cccbfSJohn Fastabend 		goto nla_put_failure;
4767815cccbfSJohn Fastabend 
47681d460b98SRoopa Prabhu 	if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
4769815cccbfSJohn Fastabend 		nla_nest_cancel(skb, br_afspec);
4770815cccbfSJohn Fastabend 		goto nla_put_failure;
4771815cccbfSJohn Fastabend 	}
47721d460b98SRoopa Prabhu 
47731d460b98SRoopa Prabhu 	if (mode != BRIDGE_MODE_UNDEF) {
47741d460b98SRoopa Prabhu 		if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
47751d460b98SRoopa Prabhu 			nla_nest_cancel(skb, br_afspec);
47761d460b98SRoopa Prabhu 			goto nla_put_failure;
47771d460b98SRoopa Prabhu 		}
47781d460b98SRoopa Prabhu 	}
47797d4f8d87SScott Feldman 	if (vlan_fill) {
47807d4f8d87SScott Feldman 		err = vlan_fill(skb, dev, filter_mask);
47817d4f8d87SScott Feldman 		if (err) {
47827d4f8d87SScott Feldman 			nla_nest_cancel(skb, br_afspec);
47837d4f8d87SScott Feldman 			goto nla_put_failure;
47847d4f8d87SScott Feldman 		}
47857d4f8d87SScott Feldman 	}
4786815cccbfSJohn Fastabend 	nla_nest_end(skb, br_afspec);
4787815cccbfSJohn Fastabend 
4788ae0be8deSMichal Kubecek 	protinfo = nla_nest_start(skb, IFLA_PROTINFO);
47892c3c031cSScott Feldman 	if (!protinfo)
47902c3c031cSScott Feldman 		goto nla_put_failure;
47912c3c031cSScott Feldman 
47922c3c031cSScott Feldman 	if (brport_nla_put_flag(skb, flags, mask,
47932c3c031cSScott Feldman 				IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
47942c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
47952c3c031cSScott Feldman 				IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
47962c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
47972c3c031cSScott Feldman 				IFLA_BRPORT_FAST_LEAVE,
47982c3c031cSScott Feldman 				BR_MULTICAST_FAST_LEAVE) ||
47992c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
48002c3c031cSScott Feldman 				IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
48012c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
48022c3c031cSScott Feldman 				IFLA_BRPORT_LEARNING, BR_LEARNING) ||
48032c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
48042c3c031cSScott Feldman 				IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
48052c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
48062c3c031cSScott Feldman 				IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
48072c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
4808583cb0b4SJulian Wiedmann 				IFLA_BRPORT_PROXYARP, BR_PROXYARP) ||
4809583cb0b4SJulian Wiedmann 	    brport_nla_put_flag(skb, flags, mask,
4810583cb0b4SJulian Wiedmann 				IFLA_BRPORT_MCAST_FLOOD, BR_MCAST_FLOOD) ||
4811583cb0b4SJulian Wiedmann 	    brport_nla_put_flag(skb, flags, mask,
4812583cb0b4SJulian Wiedmann 				IFLA_BRPORT_BCAST_FLOOD, BR_BCAST_FLOOD)) {
48132c3c031cSScott Feldman 		nla_nest_cancel(skb, protinfo);
48142c3c031cSScott Feldman 		goto nla_put_failure;
48152c3c031cSScott Feldman 	}
48162c3c031cSScott Feldman 
48172c3c031cSScott Feldman 	nla_nest_end(skb, protinfo);
48182c3c031cSScott Feldman 
4819053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
4820053c095aSJohannes Berg 	return 0;
4821815cccbfSJohn Fastabend nla_put_failure:
4822815cccbfSJohn Fastabend 	nlmsg_cancel(skb, nlh);
48237d4f8d87SScott Feldman 	return err ? err : -EMSGSIZE;
4824815cccbfSJohn Fastabend }
48257d4f8d87SScott Feldman EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
4826815cccbfSJohn Fastabend 
48272d011be8SDavid Ahern static int valid_bridge_getlink_req(const struct nlmsghdr *nlh,
48282d011be8SDavid Ahern 				    bool strict_check, u32 *filter_mask,
48292d011be8SDavid Ahern 				    struct netlink_ext_ack *extack)
48302d011be8SDavid Ahern {
48312d011be8SDavid Ahern 	struct nlattr *tb[IFLA_MAX+1];
48322d011be8SDavid Ahern 	int err, i;
48332d011be8SDavid Ahern 
48342d011be8SDavid Ahern 	if (strict_check) {
48352d011be8SDavid Ahern 		struct ifinfomsg *ifm;
48362d011be8SDavid Ahern 
48372d011be8SDavid Ahern 		if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
48382d011be8SDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid header for bridge link dump");
48392d011be8SDavid Ahern 			return -EINVAL;
48402d011be8SDavid Ahern 		}
48412d011be8SDavid Ahern 
48422d011be8SDavid Ahern 		ifm = nlmsg_data(nlh);
48432d011be8SDavid Ahern 		if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
48442d011be8SDavid Ahern 		    ifm->ifi_change || ifm->ifi_index) {
48452d011be8SDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid values in header for bridge link dump request");
48462d011be8SDavid Ahern 			return -EINVAL;
48472d011be8SDavid Ahern 		}
48482d011be8SDavid Ahern 
48498cb08174SJohannes Berg 		err = nlmsg_parse_deprecated_strict(nlh,
48508cb08174SJohannes Berg 						    sizeof(struct ifinfomsg),
48518cb08174SJohannes Berg 						    tb, IFLA_MAX, ifla_policy,
48528cb08174SJohannes Berg 						    extack);
48532d011be8SDavid Ahern 	} else {
48548cb08174SJohannes Berg 		err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
48558cb08174SJohannes Berg 					     tb, IFLA_MAX, ifla_policy,
48568cb08174SJohannes Berg 					     extack);
48572d011be8SDavid Ahern 	}
48582d011be8SDavid Ahern 	if (err < 0)
48592d011be8SDavid Ahern 		return err;
48602d011be8SDavid Ahern 
48612d011be8SDavid Ahern 	/* new attributes should only be added with strict checking */
48622d011be8SDavid Ahern 	for (i = 0; i <= IFLA_MAX; ++i) {
48632d011be8SDavid Ahern 		if (!tb[i])
48642d011be8SDavid Ahern 			continue;
48652d011be8SDavid Ahern 
48662d011be8SDavid Ahern 		switch (i) {
48672d011be8SDavid Ahern 		case IFLA_EXT_MASK:
48682d011be8SDavid Ahern 			*filter_mask = nla_get_u32(tb[i]);
48692d011be8SDavid Ahern 			break;
48702d011be8SDavid Ahern 		default:
48712d011be8SDavid Ahern 			if (strict_check) {
48722d011be8SDavid Ahern 				NL_SET_ERR_MSG(extack, "Unsupported attribute in bridge link dump request");
48732d011be8SDavid Ahern 				return -EINVAL;
48742d011be8SDavid Ahern 			}
48752d011be8SDavid Ahern 		}
48762d011be8SDavid Ahern 	}
48772d011be8SDavid Ahern 
48782d011be8SDavid Ahern 	return 0;
48792d011be8SDavid Ahern }
48802d011be8SDavid Ahern 
4881e5a55a89SJohn Fastabend static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
4882e5a55a89SJohn Fastabend {
48832d011be8SDavid Ahern 	const struct nlmsghdr *nlh = cb->nlh;
4884e5a55a89SJohn Fastabend 	struct net *net = sock_net(skb->sk);
4885e5a55a89SJohn Fastabend 	struct net_device *dev;
4886e5a55a89SJohn Fastabend 	int idx = 0;
4887e5a55a89SJohn Fastabend 	u32 portid = NETLINK_CB(cb->skb).portid;
48882d011be8SDavid Ahern 	u32 seq = nlh->nlmsg_seq;
48896cbdceebSVlad Yasevich 	u32 filter_mask = 0;
4890d64f69b0SRoopa Prabhu 	int err;
48916cbdceebSVlad Yasevich 
48922d011be8SDavid Ahern 	err = valid_bridge_getlink_req(nlh, cb->strict_check, &filter_mask,
48932d011be8SDavid Ahern 				       cb->extack);
48942d011be8SDavid Ahern 	if (err < 0 && cb->strict_check)
48952d011be8SDavid Ahern 		return err;
4896e5a55a89SJohn Fastabend 
4897e5a55a89SJohn Fastabend 	rcu_read_lock();
4898e5a55a89SJohn Fastabend 	for_each_netdev_rcu(net, dev) {
4899e5a55a89SJohn Fastabend 		const struct net_device_ops *ops = dev->netdev_ops;
4900898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4901e5a55a89SJohn Fastabend 
4902898e5061SJiri Pirko 		if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
4903d64f69b0SRoopa Prabhu 			if (idx >= cb->args[0]) {
4904d64f69b0SRoopa Prabhu 				err = br_dev->netdev_ops->ndo_bridge_getlink(
4905d64f69b0SRoopa Prabhu 						skb, portid, seq, dev,
4906d64f69b0SRoopa Prabhu 						filter_mask, NLM_F_MULTI);
4907f6c5775fSDavid Ahern 				if (err < 0 && err != -EOPNOTSUPP) {
4908f6c5775fSDavid Ahern 					if (likely(skb->len))
4909e5a55a89SJohn Fastabend 						break;
4910f6c5775fSDavid Ahern 
4911f6c5775fSDavid Ahern 					goto out_err;
4912f6c5775fSDavid Ahern 				}
4913d64f69b0SRoopa Prabhu 			}
4914e5a55a89SJohn Fastabend 			idx++;
4915e5a55a89SJohn Fastabend 		}
4916e5a55a89SJohn Fastabend 
4917e5a55a89SJohn Fastabend 		if (ops->ndo_bridge_getlink) {
4918d64f69b0SRoopa Prabhu 			if (idx >= cb->args[0]) {
4919d64f69b0SRoopa Prabhu 				err = ops->ndo_bridge_getlink(skb, portid,
4920d64f69b0SRoopa Prabhu 							      seq, dev,
492146c264daSNicolas Dichtel 							      filter_mask,
4922d64f69b0SRoopa Prabhu 							      NLM_F_MULTI);
4923f6c5775fSDavid Ahern 				if (err < 0 && err != -EOPNOTSUPP) {
4924f6c5775fSDavid Ahern 					if (likely(skb->len))
4925e5a55a89SJohn Fastabend 						break;
4926f6c5775fSDavid Ahern 
4927f6c5775fSDavid Ahern 					goto out_err;
4928f6c5775fSDavid Ahern 				}
4929d64f69b0SRoopa Prabhu 			}
4930e5a55a89SJohn Fastabend 			idx++;
4931e5a55a89SJohn Fastabend 		}
4932e5a55a89SJohn Fastabend 	}
4933f6c5775fSDavid Ahern 	err = skb->len;
4934f6c5775fSDavid Ahern out_err:
4935e5a55a89SJohn Fastabend 	rcu_read_unlock();
4936e5a55a89SJohn Fastabend 	cb->args[0] = idx;
4937e5a55a89SJohn Fastabend 
4938f6c5775fSDavid Ahern 	return err;
4939e5a55a89SJohn Fastabend }
4940e5a55a89SJohn Fastabend 
49412469ffd7SJohn Fastabend static inline size_t bridge_nlmsg_size(void)
49422469ffd7SJohn Fastabend {
49432469ffd7SJohn Fastabend 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
49442469ffd7SJohn Fastabend 		+ nla_total_size(IFNAMSIZ)	/* IFLA_IFNAME */
49452469ffd7SJohn Fastabend 		+ nla_total_size(MAX_ADDR_LEN)	/* IFLA_ADDRESS */
49462469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_MASTER */
49472469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_MTU */
49482469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_LINK */
49492469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_OPERSTATE */
49502469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u8))	/* IFLA_PROTINFO */
49512469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(struct nlattr))	/* IFLA_AF_SPEC */
49522469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u16))	/* IFLA_BRIDGE_FLAGS */
49532469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u16));	/* IFLA_BRIDGE_MODE */
49542469ffd7SJohn Fastabend }
49552469ffd7SJohn Fastabend 
495602dba438SRoopa Prabhu static int rtnl_bridge_notify(struct net_device *dev)
49572469ffd7SJohn Fastabend {
49582469ffd7SJohn Fastabend 	struct net *net = dev_net(dev);
49592469ffd7SJohn Fastabend 	struct sk_buff *skb;
49602469ffd7SJohn Fastabend 	int err = -EOPNOTSUPP;
49612469ffd7SJohn Fastabend 
496202dba438SRoopa Prabhu 	if (!dev->netdev_ops->ndo_bridge_getlink)
496302dba438SRoopa Prabhu 		return 0;
496402dba438SRoopa Prabhu 
49652469ffd7SJohn Fastabend 	skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
49662469ffd7SJohn Fastabend 	if (!skb) {
49672469ffd7SJohn Fastabend 		err = -ENOMEM;
49682469ffd7SJohn Fastabend 		goto errout;
49692469ffd7SJohn Fastabend 	}
49702469ffd7SJohn Fastabend 
497146c264daSNicolas Dichtel 	err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
4972c38e01b8SJohn Fastabend 	if (err < 0)
4973c38e01b8SJohn Fastabend 		goto errout;
49742469ffd7SJohn Fastabend 
4975d2e381c4SIdo Schimmel 	/* Notification info is only filled for bridge ports, not the bridge
4976d2e381c4SIdo Schimmel 	 * device itself. Therefore, a zero notification length is valid and
4977d2e381c4SIdo Schimmel 	 * should not result in an error.
4978d2e381c4SIdo Schimmel 	 */
4979d2e381c4SIdo Schimmel 	if (!skb->len)
498059ccaaaaSRoopa Prabhu 		goto errout;
498159ccaaaaSRoopa Prabhu 
49822469ffd7SJohn Fastabend 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
49832469ffd7SJohn Fastabend 	return 0;
49842469ffd7SJohn Fastabend errout:
49852469ffd7SJohn Fastabend 	WARN_ON(err == -EMSGSIZE);
49862469ffd7SJohn Fastabend 	kfree_skb(skb);
498759ccaaaaSRoopa Prabhu 	if (err)
49882469ffd7SJohn Fastabend 		rtnl_set_sk_err(net, RTNLGRP_LINK, err);
49892469ffd7SJohn Fastabend 	return err;
49902469ffd7SJohn Fastabend }
49912469ffd7SJohn Fastabend 
4992c21ef3e3SDavid Ahern static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
4993c21ef3e3SDavid Ahern 			       struct netlink_ext_ack *extack)
4994e5a55a89SJohn Fastabend {
4995e5a55a89SJohn Fastabend 	struct net *net = sock_net(skb->sk);
4996e5a55a89SJohn Fastabend 	struct ifinfomsg *ifm;
4997e5a55a89SJohn Fastabend 	struct net_device *dev;
49982469ffd7SJohn Fastabend 	struct nlattr *br_spec, *attr = NULL;
49992469ffd7SJohn Fastabend 	int rem, err = -EOPNOTSUPP;
50004de8b413SRosen, Rami 	u16 flags = 0;
5001c38e01b8SJohn Fastabend 	bool have_flags = false;
5002e5a55a89SJohn Fastabend 
5003e5a55a89SJohn Fastabend 	if (nlmsg_len(nlh) < sizeof(*ifm))
5004e5a55a89SJohn Fastabend 		return -EINVAL;
5005e5a55a89SJohn Fastabend 
5006e5a55a89SJohn Fastabend 	ifm = nlmsg_data(nlh);
5007e5a55a89SJohn Fastabend 	if (ifm->ifi_family != AF_BRIDGE)
5008e5a55a89SJohn Fastabend 		return -EPFNOSUPPORT;
5009e5a55a89SJohn Fastabend 
5010e5a55a89SJohn Fastabend 	dev = __dev_get_by_index(net, ifm->ifi_index);
5011e5a55a89SJohn Fastabend 	if (!dev) {
5012b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "unknown ifindex");
5013e5a55a89SJohn Fastabend 		return -ENODEV;
5014e5a55a89SJohn Fastabend 	}
5015e5a55a89SJohn Fastabend 
50162469ffd7SJohn Fastabend 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
50172469ffd7SJohn Fastabend 	if (br_spec) {
50182469ffd7SJohn Fastabend 		nla_for_each_nested(attr, br_spec, rem) {
50192469ffd7SJohn Fastabend 			if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
50206e8d1c55SThomas Graf 				if (nla_len(attr) < sizeof(flags))
50216e8d1c55SThomas Graf 					return -EINVAL;
50226e8d1c55SThomas Graf 
5023c38e01b8SJohn Fastabend 				have_flags = true;
50242469ffd7SJohn Fastabend 				flags = nla_get_u16(attr);
50252469ffd7SJohn Fastabend 				break;
50262469ffd7SJohn Fastabend 			}
50272469ffd7SJohn Fastabend 		}
50282469ffd7SJohn Fastabend 	}
50292469ffd7SJohn Fastabend 
50302469ffd7SJohn Fastabend 	if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
5031898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
5032898e5061SJiri Pirko 
5033898e5061SJiri Pirko 		if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
50342469ffd7SJohn Fastabend 			err = -EOPNOTSUPP;
5035e5a55a89SJohn Fastabend 			goto out;
5036e5a55a89SJohn Fastabend 		}
5037e5a55a89SJohn Fastabend 
50382fd527b7SPetr Machata 		err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags,
50392fd527b7SPetr Machata 							     extack);
50402469ffd7SJohn Fastabend 		if (err)
50412469ffd7SJohn Fastabend 			goto out;
50422469ffd7SJohn Fastabend 
50432469ffd7SJohn Fastabend 		flags &= ~BRIDGE_FLAGS_MASTER;
50442469ffd7SJohn Fastabend 	}
50452469ffd7SJohn Fastabend 
50462469ffd7SJohn Fastabend 	if ((flags & BRIDGE_FLAGS_SELF)) {
50472469ffd7SJohn Fastabend 		if (!dev->netdev_ops->ndo_bridge_setlink)
50482469ffd7SJohn Fastabend 			err = -EOPNOTSUPP;
50492469ffd7SJohn Fastabend 		else
5050add511b3SRoopa Prabhu 			err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
50512fd527b7SPetr Machata 								  flags,
50522fd527b7SPetr Machata 								  extack);
505302dba438SRoopa Prabhu 		if (!err) {
50542469ffd7SJohn Fastabend 			flags &= ~BRIDGE_FLAGS_SELF;
505502dba438SRoopa Prabhu 
505602dba438SRoopa Prabhu 			/* Generate event to notify upper layer of bridge
505702dba438SRoopa Prabhu 			 * change
505802dba438SRoopa Prabhu 			 */
505902dba438SRoopa Prabhu 			err = rtnl_bridge_notify(dev);
506002dba438SRoopa Prabhu 		}
50612469ffd7SJohn Fastabend 	}
50622469ffd7SJohn Fastabend 
5063c38e01b8SJohn Fastabend 	if (have_flags)
50642469ffd7SJohn Fastabend 		memcpy(nla_data(attr), &flags, sizeof(flags));
5065e5a55a89SJohn Fastabend out:
5066e5a55a89SJohn Fastabend 	return err;
5067e5a55a89SJohn Fastabend }
5068e5a55a89SJohn Fastabend 
5069c21ef3e3SDavid Ahern static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
5070c21ef3e3SDavid Ahern 			       struct netlink_ext_ack *extack)
5071407af329SVlad Yasevich {
5072407af329SVlad Yasevich 	struct net *net = sock_net(skb->sk);
5073407af329SVlad Yasevich 	struct ifinfomsg *ifm;
5074407af329SVlad Yasevich 	struct net_device *dev;
5075407af329SVlad Yasevich 	struct nlattr *br_spec, *attr = NULL;
5076407af329SVlad Yasevich 	int rem, err = -EOPNOTSUPP;
50774de8b413SRosen, Rami 	u16 flags = 0;
5078407af329SVlad Yasevich 	bool have_flags = false;
5079407af329SVlad Yasevich 
5080407af329SVlad Yasevich 	if (nlmsg_len(nlh) < sizeof(*ifm))
5081407af329SVlad Yasevich 		return -EINVAL;
5082407af329SVlad Yasevich 
5083407af329SVlad Yasevich 	ifm = nlmsg_data(nlh);
5084407af329SVlad Yasevich 	if (ifm->ifi_family != AF_BRIDGE)
5085407af329SVlad Yasevich 		return -EPFNOSUPPORT;
5086407af329SVlad Yasevich 
5087407af329SVlad Yasevich 	dev = __dev_get_by_index(net, ifm->ifi_index);
5088407af329SVlad Yasevich 	if (!dev) {
5089b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "unknown ifindex");
5090407af329SVlad Yasevich 		return -ENODEV;
5091407af329SVlad Yasevich 	}
5092407af329SVlad Yasevich 
5093407af329SVlad Yasevich 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
5094407af329SVlad Yasevich 	if (br_spec) {
5095407af329SVlad Yasevich 		nla_for_each_nested(attr, br_spec, rem) {
5096407af329SVlad Yasevich 			if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
50976e8d1c55SThomas Graf 				if (nla_len(attr) < sizeof(flags))
50986e8d1c55SThomas Graf 					return -EINVAL;
50996e8d1c55SThomas Graf 
5100407af329SVlad Yasevich 				have_flags = true;
5101407af329SVlad Yasevich 				flags = nla_get_u16(attr);
5102407af329SVlad Yasevich 				break;
5103407af329SVlad Yasevich 			}
5104407af329SVlad Yasevich 		}
5105407af329SVlad Yasevich 	}
5106407af329SVlad Yasevich 
5107407af329SVlad Yasevich 	if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
5108407af329SVlad Yasevich 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
5109407af329SVlad Yasevich 
5110407af329SVlad Yasevich 		if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
5111407af329SVlad Yasevich 			err = -EOPNOTSUPP;
5112407af329SVlad Yasevich 			goto out;
5113407af329SVlad Yasevich 		}
5114407af329SVlad Yasevich 
5115add511b3SRoopa Prabhu 		err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
5116407af329SVlad Yasevich 		if (err)
5117407af329SVlad Yasevich 			goto out;
5118407af329SVlad Yasevich 
5119407af329SVlad Yasevich 		flags &= ~BRIDGE_FLAGS_MASTER;
5120407af329SVlad Yasevich 	}
5121407af329SVlad Yasevich 
5122407af329SVlad Yasevich 	if ((flags & BRIDGE_FLAGS_SELF)) {
5123407af329SVlad Yasevich 		if (!dev->netdev_ops->ndo_bridge_dellink)
5124407af329SVlad Yasevich 			err = -EOPNOTSUPP;
5125407af329SVlad Yasevich 		else
5126add511b3SRoopa Prabhu 			err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
5127add511b3SRoopa Prabhu 								  flags);
5128407af329SVlad Yasevich 
512902dba438SRoopa Prabhu 		if (!err) {
5130407af329SVlad Yasevich 			flags &= ~BRIDGE_FLAGS_SELF;
513102dba438SRoopa Prabhu 
513202dba438SRoopa Prabhu 			/* Generate event to notify upper layer of bridge
513302dba438SRoopa Prabhu 			 * change
513402dba438SRoopa Prabhu 			 */
513502dba438SRoopa Prabhu 			err = rtnl_bridge_notify(dev);
513602dba438SRoopa Prabhu 		}
5137407af329SVlad Yasevich 	}
5138407af329SVlad Yasevich 
5139407af329SVlad Yasevich 	if (have_flags)
5140407af329SVlad Yasevich 		memcpy(nla_data(attr), &flags, sizeof(flags));
5141407af329SVlad Yasevich out:
5142407af329SVlad Yasevich 	return err;
5143407af329SVlad Yasevich }
5144407af329SVlad Yasevich 
5145e8872a25SNikolay Aleksandrov static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
5146e8872a25SNikolay Aleksandrov {
5147e8872a25SNikolay Aleksandrov 	return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
5148e8872a25SNikolay Aleksandrov 	       (!idxattr || idxattr == attrid);
5149e8872a25SNikolay Aleksandrov }
5150e8872a25SNikolay Aleksandrov 
5151f6e0fb81SPetr Machata static bool
5152f6e0fb81SPetr Machata rtnl_offload_xstats_have_ndo(const struct net_device *dev, int attr_id)
515369ae6ad2SNogah Frankel {
5154f6e0fb81SPetr Machata 	return dev->netdev_ops &&
5155f6e0fb81SPetr Machata 	       dev->netdev_ops->ndo_has_offload_stats &&
5156f6e0fb81SPetr Machata 	       dev->netdev_ops->ndo_get_offload_stats &&
5157f6e0fb81SPetr Machata 	       dev->netdev_ops->ndo_has_offload_stats(dev, attr_id);
515869ae6ad2SNogah Frankel }
515969ae6ad2SNogah Frankel 
5160f6e0fb81SPetr Machata static unsigned int
5161f6e0fb81SPetr Machata rtnl_offload_xstats_get_size_ndo(const struct net_device *dev, int attr_id)
5162f6e0fb81SPetr Machata {
5163f6e0fb81SPetr Machata 	return rtnl_offload_xstats_have_ndo(dev, attr_id) ?
5164f6e0fb81SPetr Machata 	       sizeof(struct rtnl_link_stats64) : 0;
5165f6e0fb81SPetr Machata }
5166f6e0fb81SPetr Machata 
5167f6e0fb81SPetr Machata static int
5168f6e0fb81SPetr Machata rtnl_offload_xstats_fill_ndo(struct net_device *dev, int attr_id,
5169f6e0fb81SPetr Machata 			     struct sk_buff *skb)
5170f6e0fb81SPetr Machata {
5171f6e0fb81SPetr Machata 	unsigned int size = rtnl_offload_xstats_get_size_ndo(dev, attr_id);
5172f6e0fb81SPetr Machata 	struct nlattr *attr = NULL;
5173f6e0fb81SPetr Machata 	void *attr_data;
5174f6e0fb81SPetr Machata 	int err;
5175f6e0fb81SPetr Machata 
5176f6e0fb81SPetr Machata 	if (!size)
5177f6e0fb81SPetr Machata 		return -ENODATA;
5178f6e0fb81SPetr Machata 
5179f6e0fb81SPetr Machata 	attr = nla_reserve_64bit(skb, attr_id, size,
5180f6e0fb81SPetr Machata 				 IFLA_OFFLOAD_XSTATS_UNSPEC);
5181f6e0fb81SPetr Machata 	if (!attr)
5182f6e0fb81SPetr Machata 		return -EMSGSIZE;
5183f6e0fb81SPetr Machata 
5184f6e0fb81SPetr Machata 	attr_data = nla_data(attr);
5185f6e0fb81SPetr Machata 	memset(attr_data, 0, size);
5186f6e0fb81SPetr Machata 
5187f6e0fb81SPetr Machata 	err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev, attr_data);
5188f6e0fb81SPetr Machata 	if (err)
5189f6e0fb81SPetr Machata 		return err;
5190f6e0fb81SPetr Machata 
519169ae6ad2SNogah Frankel 	return 0;
519269ae6ad2SNogah Frankel }
519369ae6ad2SNogah Frankel 
51940e7788fdSPetr Machata static unsigned int
51950e7788fdSPetr Machata rtnl_offload_xstats_get_size_stats(const struct net_device *dev,
51960e7788fdSPetr Machata 				   enum netdev_offload_xstats_type type)
51970e7788fdSPetr Machata {
51980e7788fdSPetr Machata 	bool enabled = netdev_offload_xstats_enabled(dev, type);
51990e7788fdSPetr Machata 
52000e7788fdSPetr Machata 	return enabled ? sizeof(struct rtnl_hw_stats64) : 0;
52010e7788fdSPetr Machata }
52020e7788fdSPetr Machata 
52030e7788fdSPetr Machata struct rtnl_offload_xstats_request_used {
52040e7788fdSPetr Machata 	bool request;
52050e7788fdSPetr Machata 	bool used;
52060e7788fdSPetr Machata };
52070e7788fdSPetr Machata 
52080e7788fdSPetr Machata static int
52090e7788fdSPetr Machata rtnl_offload_xstats_get_stats(struct net_device *dev,
52100e7788fdSPetr Machata 			      enum netdev_offload_xstats_type type,
52110e7788fdSPetr Machata 			      struct rtnl_offload_xstats_request_used *ru,
52120e7788fdSPetr Machata 			      struct rtnl_hw_stats64 *stats,
52130e7788fdSPetr Machata 			      struct netlink_ext_ack *extack)
52140e7788fdSPetr Machata {
52150e7788fdSPetr Machata 	bool request;
52160e7788fdSPetr Machata 	bool used;
52170e7788fdSPetr Machata 	int err;
52180e7788fdSPetr Machata 
52190e7788fdSPetr Machata 	request = netdev_offload_xstats_enabled(dev, type);
52200e7788fdSPetr Machata 	if (!request) {
52210e7788fdSPetr Machata 		used = false;
52220e7788fdSPetr Machata 		goto out;
52230e7788fdSPetr Machata 	}
52240e7788fdSPetr Machata 
52250e7788fdSPetr Machata 	err = netdev_offload_xstats_get(dev, type, stats, &used, extack);
52260e7788fdSPetr Machata 	if (err)
52270e7788fdSPetr Machata 		return err;
52280e7788fdSPetr Machata 
52290e7788fdSPetr Machata out:
52300e7788fdSPetr Machata 	if (ru) {
52310e7788fdSPetr Machata 		ru->request = request;
52320e7788fdSPetr Machata 		ru->used = used;
52330e7788fdSPetr Machata 	}
52340e7788fdSPetr Machata 	return 0;
52350e7788fdSPetr Machata }
52360e7788fdSPetr Machata 
52370e7788fdSPetr Machata static int
52380e7788fdSPetr Machata rtnl_offload_xstats_fill_hw_s_info_one(struct sk_buff *skb, int attr_id,
52390e7788fdSPetr Machata 				       struct rtnl_offload_xstats_request_used *ru)
52400e7788fdSPetr Machata {
52410e7788fdSPetr Machata 	struct nlattr *nest;
52420e7788fdSPetr Machata 
52430e7788fdSPetr Machata 	nest = nla_nest_start(skb, attr_id);
52440e7788fdSPetr Machata 	if (!nest)
52450e7788fdSPetr Machata 		return -EMSGSIZE;
52460e7788fdSPetr Machata 
52470e7788fdSPetr Machata 	if (nla_put_u8(skb, IFLA_OFFLOAD_XSTATS_HW_S_INFO_REQUEST, ru->request))
52480e7788fdSPetr Machata 		goto nla_put_failure;
52490e7788fdSPetr Machata 
52500e7788fdSPetr Machata 	if (nla_put_u8(skb, IFLA_OFFLOAD_XSTATS_HW_S_INFO_USED, ru->used))
52510e7788fdSPetr Machata 		goto nla_put_failure;
52520e7788fdSPetr Machata 
52530e7788fdSPetr Machata 	nla_nest_end(skb, nest);
52540e7788fdSPetr Machata 	return 0;
52550e7788fdSPetr Machata 
52560e7788fdSPetr Machata nla_put_failure:
52570e7788fdSPetr Machata 	nla_nest_cancel(skb, nest);
52580e7788fdSPetr Machata 	return -EMSGSIZE;
52590e7788fdSPetr Machata }
52600e7788fdSPetr Machata 
52610e7788fdSPetr Machata static int
52620e7788fdSPetr Machata rtnl_offload_xstats_fill_hw_s_info(struct sk_buff *skb, struct net_device *dev,
52630e7788fdSPetr Machata 				   struct netlink_ext_ack *extack)
52640e7788fdSPetr Machata {
52650e7788fdSPetr Machata 	enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3;
52660e7788fdSPetr Machata 	struct rtnl_offload_xstats_request_used ru_l3;
52670e7788fdSPetr Machata 	struct nlattr *nest;
52680e7788fdSPetr Machata 	int err;
52690e7788fdSPetr Machata 
52700e7788fdSPetr Machata 	err = rtnl_offload_xstats_get_stats(dev, t_l3, &ru_l3, NULL, extack);
52710e7788fdSPetr Machata 	if (err)
52720e7788fdSPetr Machata 		return err;
52730e7788fdSPetr Machata 
52740e7788fdSPetr Machata 	nest = nla_nest_start(skb, IFLA_OFFLOAD_XSTATS_HW_S_INFO);
52750e7788fdSPetr Machata 	if (!nest)
52760e7788fdSPetr Machata 		return -EMSGSIZE;
52770e7788fdSPetr Machata 
52780e7788fdSPetr Machata 	if (rtnl_offload_xstats_fill_hw_s_info_one(skb,
52790e7788fdSPetr Machata 						   IFLA_OFFLOAD_XSTATS_L3_STATS,
52800e7788fdSPetr Machata 						   &ru_l3))
52810e7788fdSPetr Machata 		goto nla_put_failure;
52820e7788fdSPetr Machata 
52830e7788fdSPetr Machata 	nla_nest_end(skb, nest);
52840e7788fdSPetr Machata 	return 0;
52850e7788fdSPetr Machata 
52860e7788fdSPetr Machata nla_put_failure:
52870e7788fdSPetr Machata 	nla_nest_cancel(skb, nest);
52880e7788fdSPetr Machata 	return -EMSGSIZE;
52890e7788fdSPetr Machata }
52900e7788fdSPetr Machata 
52916b524a1dSPetr Machata static int rtnl_offload_xstats_fill(struct sk_buff *skb, struct net_device *dev,
529205415bccSPetr Machata 				    int *prividx, u32 off_filter_mask,
529305415bccSPetr Machata 				    struct netlink_ext_ack *extack)
529469ae6ad2SNogah Frankel {
52950e7788fdSPetr Machata 	enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3;
52960e7788fdSPetr Machata 	int attr_id_hw_s_info = IFLA_OFFLOAD_XSTATS_HW_S_INFO;
52970e7788fdSPetr Machata 	int attr_id_l3_stats = IFLA_OFFLOAD_XSTATS_L3_STATS;
5298f6e0fb81SPetr Machata 	int attr_id_cpu_hit = IFLA_OFFLOAD_XSTATS_CPU_HIT;
5299f6e0fb81SPetr Machata 	bool have_data = false;
530069ae6ad2SNogah Frankel 	int err;
530169ae6ad2SNogah Frankel 
530246efc97bSPetr Machata 	if (*prividx <= attr_id_cpu_hit &&
530346efc97bSPetr Machata 	    (off_filter_mask &
530446efc97bSPetr Machata 	     IFLA_STATS_FILTER_BIT(attr_id_cpu_hit))) {
5305f6e0fb81SPetr Machata 		err = rtnl_offload_xstats_fill_ndo(dev, attr_id_cpu_hit, skb);
5306f6e0fb81SPetr Machata 		if (!err) {
5307f6e0fb81SPetr Machata 			have_data = true;
5308f6e0fb81SPetr Machata 		} else if (err != -ENODATA) {
5309f6e0fb81SPetr Machata 			*prividx = attr_id_cpu_hit;
5310f6e0fb81SPetr Machata 			return err;
5311f6e0fb81SPetr Machata 		}
531269ae6ad2SNogah Frankel 	}
531369ae6ad2SNogah Frankel 
53140e7788fdSPetr Machata 	if (*prividx <= attr_id_hw_s_info &&
53150e7788fdSPetr Machata 	    (off_filter_mask & IFLA_STATS_FILTER_BIT(attr_id_hw_s_info))) {
53160e7788fdSPetr Machata 		*prividx = attr_id_hw_s_info;
53170e7788fdSPetr Machata 
53180e7788fdSPetr Machata 		err = rtnl_offload_xstats_fill_hw_s_info(skb, dev, extack);
53190e7788fdSPetr Machata 		if (err)
53200e7788fdSPetr Machata 			return err;
53210e7788fdSPetr Machata 
53220e7788fdSPetr Machata 		have_data = true;
53230e7788fdSPetr Machata 		*prividx = 0;
53240e7788fdSPetr Machata 	}
53250e7788fdSPetr Machata 
53260e7788fdSPetr Machata 	if (*prividx <= attr_id_l3_stats &&
53270e7788fdSPetr Machata 	    (off_filter_mask & IFLA_STATS_FILTER_BIT(attr_id_l3_stats))) {
53280e7788fdSPetr Machata 		unsigned int size_l3;
53290e7788fdSPetr Machata 		struct nlattr *attr;
53300e7788fdSPetr Machata 
53310e7788fdSPetr Machata 		*prividx = attr_id_l3_stats;
53320e7788fdSPetr Machata 
53330e7788fdSPetr Machata 		size_l3 = rtnl_offload_xstats_get_size_stats(dev, t_l3);
533423cfe941SPetr Machata 		if (!size_l3)
533523cfe941SPetr Machata 			goto skip_l3_stats;
53360e7788fdSPetr Machata 		attr = nla_reserve_64bit(skb, attr_id_l3_stats, size_l3,
53370e7788fdSPetr Machata 					 IFLA_OFFLOAD_XSTATS_UNSPEC);
53380e7788fdSPetr Machata 		if (!attr)
53390e7788fdSPetr Machata 			return -EMSGSIZE;
53400e7788fdSPetr Machata 
53410e7788fdSPetr Machata 		err = rtnl_offload_xstats_get_stats(dev, t_l3, NULL,
53420e7788fdSPetr Machata 						    nla_data(attr), extack);
53430e7788fdSPetr Machata 		if (err)
53440e7788fdSPetr Machata 			return err;
53450e7788fdSPetr Machata 
53460e7788fdSPetr Machata 		have_data = true;
534723cfe941SPetr Machata skip_l3_stats:
53480e7788fdSPetr Machata 		*prividx = 0;
53490e7788fdSPetr Machata 	}
53500e7788fdSPetr Machata 
5351f6e0fb81SPetr Machata 	if (!have_data)
535269ae6ad2SNogah Frankel 		return -ENODATA;
535369ae6ad2SNogah Frankel 
535469ae6ad2SNogah Frankel 	*prividx = 0;
535569ae6ad2SNogah Frankel 	return 0;
535669ae6ad2SNogah Frankel }
535769ae6ad2SNogah Frankel 
53580e7788fdSPetr Machata static unsigned int
53590e7788fdSPetr Machata rtnl_offload_xstats_get_size_hw_s_info_one(const struct net_device *dev,
53600e7788fdSPetr Machata 					   enum netdev_offload_xstats_type type)
53610e7788fdSPetr Machata {
53620e7788fdSPetr Machata 	bool enabled = netdev_offload_xstats_enabled(dev, type);
53630e7788fdSPetr Machata 
53640e7788fdSPetr Machata 	return nla_total_size(0) +
53650e7788fdSPetr Machata 		/* IFLA_OFFLOAD_XSTATS_HW_S_INFO_REQUEST */
53660e7788fdSPetr Machata 		nla_total_size(sizeof(u8)) +
53670e7788fdSPetr Machata 		/* IFLA_OFFLOAD_XSTATS_HW_S_INFO_USED */
53680e7788fdSPetr Machata 		(enabled ? nla_total_size(sizeof(u8)) : 0) +
53690e7788fdSPetr Machata 		0;
53700e7788fdSPetr Machata }
53710e7788fdSPetr Machata 
53720e7788fdSPetr Machata static unsigned int
53730e7788fdSPetr Machata rtnl_offload_xstats_get_size_hw_s_info(const struct net_device *dev)
53740e7788fdSPetr Machata {
53750e7788fdSPetr Machata 	enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3;
53760e7788fdSPetr Machata 
53770e7788fdSPetr Machata 	return nla_total_size(0) +
53780e7788fdSPetr Machata 		/* IFLA_OFFLOAD_XSTATS_L3_STATS */
53790e7788fdSPetr Machata 		rtnl_offload_xstats_get_size_hw_s_info_one(dev, t_l3) +
53800e7788fdSPetr Machata 		0;
53810e7788fdSPetr Machata }
53820e7788fdSPetr Machata 
538346efc97bSPetr Machata static int rtnl_offload_xstats_get_size(const struct net_device *dev,
538446efc97bSPetr Machata 					u32 off_filter_mask)
538569ae6ad2SNogah Frankel {
53860e7788fdSPetr Machata 	enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3;
5387f6e0fb81SPetr Machata 	int attr_id_cpu_hit = IFLA_OFFLOAD_XSTATS_CPU_HIT;
538869ae6ad2SNogah Frankel 	int nla_size = 0;
538969ae6ad2SNogah Frankel 	int size;
539069ae6ad2SNogah Frankel 
539146efc97bSPetr Machata 	if (off_filter_mask &
539246efc97bSPetr Machata 	    IFLA_STATS_FILTER_BIT(attr_id_cpu_hit)) {
5393f6e0fb81SPetr Machata 		size = rtnl_offload_xstats_get_size_ndo(dev, attr_id_cpu_hit);
539469ae6ad2SNogah Frankel 		nla_size += nla_total_size_64bit(size);
539546efc97bSPetr Machata 	}
539669ae6ad2SNogah Frankel 
53970e7788fdSPetr Machata 	if (off_filter_mask &
53980e7788fdSPetr Machata 	    IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_HW_S_INFO))
53990e7788fdSPetr Machata 		nla_size += rtnl_offload_xstats_get_size_hw_s_info(dev);
54000e7788fdSPetr Machata 
54010e7788fdSPetr Machata 	if (off_filter_mask &
54020e7788fdSPetr Machata 	    IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_L3_STATS)) {
54030e7788fdSPetr Machata 		size = rtnl_offload_xstats_get_size_stats(dev, t_l3);
54040e7788fdSPetr Machata 		nla_size += nla_total_size_64bit(size);
54050e7788fdSPetr Machata 	}
54060e7788fdSPetr Machata 
540769ae6ad2SNogah Frankel 	if (nla_size != 0)
540869ae6ad2SNogah Frankel 		nla_size += nla_total_size(0);
540969ae6ad2SNogah Frankel 
541069ae6ad2SNogah Frankel 	return nla_size;
541169ae6ad2SNogah Frankel }
541269ae6ad2SNogah Frankel 
541346efc97bSPetr Machata struct rtnl_stats_dump_filters {
541446efc97bSPetr Machata 	/* mask[0] filters outer attributes. Then individual nests have their
541546efc97bSPetr Machata 	 * filtering mask at the index of the nested attribute.
541646efc97bSPetr Machata 	 */
541746efc97bSPetr Machata 	u32 mask[IFLA_STATS_MAX + 1];
541846efc97bSPetr Machata };
541946efc97bSPetr Machata 
542010c9ead9SRoopa Prabhu static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
542110c9ead9SRoopa Prabhu 			       int type, u32 pid, u32 seq, u32 change,
542246efc97bSPetr Machata 			       unsigned int flags,
542346efc97bSPetr Machata 			       const struct rtnl_stats_dump_filters *filters,
542405415bccSPetr Machata 			       int *idxattr, int *prividx,
542505415bccSPetr Machata 			       struct netlink_ext_ack *extack)
542610c9ead9SRoopa Prabhu {
542746efc97bSPetr Machata 	unsigned int filter_mask = filters->mask[0];
542810c9ead9SRoopa Prabhu 	struct if_stats_msg *ifsm;
542910c9ead9SRoopa Prabhu 	struct nlmsghdr *nlh;
543010c9ead9SRoopa Prabhu 	struct nlattr *attr;
5431e8872a25SNikolay Aleksandrov 	int s_prividx = *prividx;
543269ae6ad2SNogah Frankel 	int err;
543310c9ead9SRoopa Prabhu 
543410c9ead9SRoopa Prabhu 	ASSERT_RTNL();
543510c9ead9SRoopa Prabhu 
543610c9ead9SRoopa Prabhu 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
543710c9ead9SRoopa Prabhu 	if (!nlh)
543810c9ead9SRoopa Prabhu 		return -EMSGSIZE;
543910c9ead9SRoopa Prabhu 
544010c9ead9SRoopa Prabhu 	ifsm = nlmsg_data(nlh);
5441ce024f42SNikolay Aleksandrov 	ifsm->family = PF_UNSPEC;
5442ce024f42SNikolay Aleksandrov 	ifsm->pad1 = 0;
5443ce024f42SNikolay Aleksandrov 	ifsm->pad2 = 0;
544410c9ead9SRoopa Prabhu 	ifsm->ifindex = dev->ifindex;
544510c9ead9SRoopa Prabhu 	ifsm->filter_mask = filter_mask;
544610c9ead9SRoopa Prabhu 
5447e8872a25SNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
544810c9ead9SRoopa Prabhu 		struct rtnl_link_stats64 *sp;
544910c9ead9SRoopa Prabhu 
545058414d32SNicolas Dichtel 		attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
545158414d32SNicolas Dichtel 					 sizeof(struct rtnl_link_stats64),
545258414d32SNicolas Dichtel 					 IFLA_STATS_UNSPEC);
5453216e6906SPetr Machata 		if (!attr) {
5454216e6906SPetr Machata 			err = -EMSGSIZE;
545510c9ead9SRoopa Prabhu 			goto nla_put_failure;
5456216e6906SPetr Machata 		}
545710c9ead9SRoopa Prabhu 
545810c9ead9SRoopa Prabhu 		sp = nla_data(attr);
545910c9ead9SRoopa Prabhu 		dev_get_stats(dev, sp);
546010c9ead9SRoopa Prabhu 	}
546110c9ead9SRoopa Prabhu 
546297a47facSNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
546397a47facSNikolay Aleksandrov 		const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
546497a47facSNikolay Aleksandrov 
546597a47facSNikolay Aleksandrov 		if (ops && ops->fill_linkxstats) {
546697a47facSNikolay Aleksandrov 			*idxattr = IFLA_STATS_LINK_XSTATS;
5467ae0be8deSMichal Kubecek 			attr = nla_nest_start_noflag(skb,
546897a47facSNikolay Aleksandrov 						     IFLA_STATS_LINK_XSTATS);
5469216e6906SPetr Machata 			if (!attr) {
5470216e6906SPetr Machata 				err = -EMSGSIZE;
547197a47facSNikolay Aleksandrov 				goto nla_put_failure;
5472216e6906SPetr Machata 			}
547397a47facSNikolay Aleksandrov 
547480e73cc5SNikolay Aleksandrov 			err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
547580e73cc5SNikolay Aleksandrov 			nla_nest_end(skb, attr);
547680e73cc5SNikolay Aleksandrov 			if (err)
547780e73cc5SNikolay Aleksandrov 				goto nla_put_failure;
547880e73cc5SNikolay Aleksandrov 			*idxattr = 0;
547980e73cc5SNikolay Aleksandrov 		}
548080e73cc5SNikolay Aleksandrov 	}
548180e73cc5SNikolay Aleksandrov 
548280e73cc5SNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
548380e73cc5SNikolay Aleksandrov 			     *idxattr)) {
548480e73cc5SNikolay Aleksandrov 		const struct rtnl_link_ops *ops = NULL;
548580e73cc5SNikolay Aleksandrov 		const struct net_device *master;
548680e73cc5SNikolay Aleksandrov 
548780e73cc5SNikolay Aleksandrov 		master = netdev_master_upper_dev_get(dev);
548880e73cc5SNikolay Aleksandrov 		if (master)
548980e73cc5SNikolay Aleksandrov 			ops = master->rtnl_link_ops;
549080e73cc5SNikolay Aleksandrov 		if (ops && ops->fill_linkxstats) {
549180e73cc5SNikolay Aleksandrov 			*idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
5492ae0be8deSMichal Kubecek 			attr = nla_nest_start_noflag(skb,
549380e73cc5SNikolay Aleksandrov 						     IFLA_STATS_LINK_XSTATS_SLAVE);
5494216e6906SPetr Machata 			if (!attr) {
5495216e6906SPetr Machata 				err = -EMSGSIZE;
549680e73cc5SNikolay Aleksandrov 				goto nla_put_failure;
5497216e6906SPetr Machata 			}
549880e73cc5SNikolay Aleksandrov 
549980e73cc5SNikolay Aleksandrov 			err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
550097a47facSNikolay Aleksandrov 			nla_nest_end(skb, attr);
550197a47facSNikolay Aleksandrov 			if (err)
550297a47facSNikolay Aleksandrov 				goto nla_put_failure;
550397a47facSNikolay Aleksandrov 			*idxattr = 0;
550497a47facSNikolay Aleksandrov 		}
550597a47facSNikolay Aleksandrov 	}
550697a47facSNikolay Aleksandrov 
550769ae6ad2SNogah Frankel 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
550869ae6ad2SNogah Frankel 			     *idxattr)) {
550946efc97bSPetr Machata 		u32 off_filter_mask;
551046efc97bSPetr Machata 
551146efc97bSPetr Machata 		off_filter_mask = filters->mask[IFLA_STATS_LINK_OFFLOAD_XSTATS];
551269ae6ad2SNogah Frankel 		*idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
5513ae0be8deSMichal Kubecek 		attr = nla_nest_start_noflag(skb,
5514ae0be8deSMichal Kubecek 					     IFLA_STATS_LINK_OFFLOAD_XSTATS);
5515216e6906SPetr Machata 		if (!attr) {
5516216e6906SPetr Machata 			err = -EMSGSIZE;
551769ae6ad2SNogah Frankel 			goto nla_put_failure;
5518216e6906SPetr Machata 		}
551969ae6ad2SNogah Frankel 
552046efc97bSPetr Machata 		err = rtnl_offload_xstats_fill(skb, dev, prividx,
552105415bccSPetr Machata 					       off_filter_mask, extack);
552269ae6ad2SNogah Frankel 		if (err == -ENODATA)
552369ae6ad2SNogah Frankel 			nla_nest_cancel(skb, attr);
552469ae6ad2SNogah Frankel 		else
552569ae6ad2SNogah Frankel 			nla_nest_end(skb, attr);
552669ae6ad2SNogah Frankel 
552769ae6ad2SNogah Frankel 		if (err && err != -ENODATA)
552869ae6ad2SNogah Frankel 			goto nla_put_failure;
552969ae6ad2SNogah Frankel 		*idxattr = 0;
553069ae6ad2SNogah Frankel 	}
553169ae6ad2SNogah Frankel 
5532aefb4d4aSRobert Shearman 	if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
5533aefb4d4aSRobert Shearman 		struct rtnl_af_ops *af_ops;
5534aefb4d4aSRobert Shearman 
5535aefb4d4aSRobert Shearman 		*idxattr = IFLA_STATS_AF_SPEC;
5536ae0be8deSMichal Kubecek 		attr = nla_nest_start_noflag(skb, IFLA_STATS_AF_SPEC);
5537216e6906SPetr Machata 		if (!attr) {
5538216e6906SPetr Machata 			err = -EMSGSIZE;
5539aefb4d4aSRobert Shearman 			goto nla_put_failure;
5540216e6906SPetr Machata 		}
5541aefb4d4aSRobert Shearman 
55425fa85a09SFlorian Westphal 		rcu_read_lock();
55435fa85a09SFlorian Westphal 		list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
5544aefb4d4aSRobert Shearman 			if (af_ops->fill_stats_af) {
5545aefb4d4aSRobert Shearman 				struct nlattr *af;
5546aefb4d4aSRobert Shearman 
5547ae0be8deSMichal Kubecek 				af = nla_nest_start_noflag(skb,
5548ae0be8deSMichal Kubecek 							   af_ops->family);
55495fa85a09SFlorian Westphal 				if (!af) {
55505fa85a09SFlorian Westphal 					rcu_read_unlock();
555157d29a29STom Rix 					err = -EMSGSIZE;
5552aefb4d4aSRobert Shearman 					goto nla_put_failure;
55535fa85a09SFlorian Westphal 				}
5554aefb4d4aSRobert Shearman 				err = af_ops->fill_stats_af(skb, dev);
5555aefb4d4aSRobert Shearman 
55565fa85a09SFlorian Westphal 				if (err == -ENODATA) {
5557aefb4d4aSRobert Shearman 					nla_nest_cancel(skb, af);
55585fa85a09SFlorian Westphal 				} else if (err < 0) {
55595fa85a09SFlorian Westphal 					rcu_read_unlock();
5560aefb4d4aSRobert Shearman 					goto nla_put_failure;
55615fa85a09SFlorian Westphal 				}
5562aefb4d4aSRobert Shearman 
5563aefb4d4aSRobert Shearman 				nla_nest_end(skb, af);
5564aefb4d4aSRobert Shearman 			}
5565aefb4d4aSRobert Shearman 		}
55665fa85a09SFlorian Westphal 		rcu_read_unlock();
5567aefb4d4aSRobert Shearman 
5568aefb4d4aSRobert Shearman 		nla_nest_end(skb, attr);
5569aefb4d4aSRobert Shearman 
5570aefb4d4aSRobert Shearman 		*idxattr = 0;
5571aefb4d4aSRobert Shearman 	}
5572aefb4d4aSRobert Shearman 
557310c9ead9SRoopa Prabhu 	nlmsg_end(skb, nlh);
557410c9ead9SRoopa Prabhu 
557510c9ead9SRoopa Prabhu 	return 0;
557610c9ead9SRoopa Prabhu 
557710c9ead9SRoopa Prabhu nla_put_failure:
5578e8872a25SNikolay Aleksandrov 	/* not a multi message or no progress mean a real error */
5579e8872a25SNikolay Aleksandrov 	if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
558010c9ead9SRoopa Prabhu 		nlmsg_cancel(skb, nlh);
5581e8872a25SNikolay Aleksandrov 	else
5582e8872a25SNikolay Aleksandrov 		nlmsg_end(skb, nlh);
558310c9ead9SRoopa Prabhu 
5584216e6906SPetr Machata 	return err;
558510c9ead9SRoopa Prabhu }
558610c9ead9SRoopa Prabhu 
558710c9ead9SRoopa Prabhu static size_t if_nlmsg_stats_size(const struct net_device *dev,
558846efc97bSPetr Machata 				  const struct rtnl_stats_dump_filters *filters)
558910c9ead9SRoopa Prabhu {
5590d3436799SEric Dumazet 	size_t size = NLMSG_ALIGN(sizeof(struct if_stats_msg));
559146efc97bSPetr Machata 	unsigned int filter_mask = filters->mask[0];
559210c9ead9SRoopa Prabhu 
5593e8872a25SNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
559410c9ead9SRoopa Prabhu 		size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
559510c9ead9SRoopa Prabhu 
559697a47facSNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
559797a47facSNikolay Aleksandrov 		const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
559880e73cc5SNikolay Aleksandrov 		int attr = IFLA_STATS_LINK_XSTATS;
559997a47facSNikolay Aleksandrov 
560097a47facSNikolay Aleksandrov 		if (ops && ops->get_linkxstats_size) {
560180e73cc5SNikolay Aleksandrov 			size += nla_total_size(ops->get_linkxstats_size(dev,
560280e73cc5SNikolay Aleksandrov 									attr));
560397a47facSNikolay Aleksandrov 			/* for IFLA_STATS_LINK_XSTATS */
560497a47facSNikolay Aleksandrov 			size += nla_total_size(0);
560597a47facSNikolay Aleksandrov 		}
560697a47facSNikolay Aleksandrov 	}
560797a47facSNikolay Aleksandrov 
560880e73cc5SNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
560980e73cc5SNikolay Aleksandrov 		struct net_device *_dev = (struct net_device *)dev;
561080e73cc5SNikolay Aleksandrov 		const struct rtnl_link_ops *ops = NULL;
561180e73cc5SNikolay Aleksandrov 		const struct net_device *master;
561280e73cc5SNikolay Aleksandrov 
561380e73cc5SNikolay Aleksandrov 		/* netdev_master_upper_dev_get can't take const */
561480e73cc5SNikolay Aleksandrov 		master = netdev_master_upper_dev_get(_dev);
561580e73cc5SNikolay Aleksandrov 		if (master)
561680e73cc5SNikolay Aleksandrov 			ops = master->rtnl_link_ops;
561780e73cc5SNikolay Aleksandrov 		if (ops && ops->get_linkxstats_size) {
561880e73cc5SNikolay Aleksandrov 			int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
561980e73cc5SNikolay Aleksandrov 
562080e73cc5SNikolay Aleksandrov 			size += nla_total_size(ops->get_linkxstats_size(dev,
562180e73cc5SNikolay Aleksandrov 									attr));
562280e73cc5SNikolay Aleksandrov 			/* for IFLA_STATS_LINK_XSTATS_SLAVE */
562380e73cc5SNikolay Aleksandrov 			size += nla_total_size(0);
562480e73cc5SNikolay Aleksandrov 		}
562580e73cc5SNikolay Aleksandrov 	}
562680e73cc5SNikolay Aleksandrov 
562746efc97bSPetr Machata 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0)) {
562846efc97bSPetr Machata 		u32 off_filter_mask;
562946efc97bSPetr Machata 
563046efc97bSPetr Machata 		off_filter_mask = filters->mask[IFLA_STATS_LINK_OFFLOAD_XSTATS];
563146efc97bSPetr Machata 		size += rtnl_offload_xstats_get_size(dev, off_filter_mask);
563246efc97bSPetr Machata 	}
563369ae6ad2SNogah Frankel 
5634aefb4d4aSRobert Shearman 	if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
5635aefb4d4aSRobert Shearman 		struct rtnl_af_ops *af_ops;
5636aefb4d4aSRobert Shearman 
5637aefb4d4aSRobert Shearman 		/* for IFLA_STATS_AF_SPEC */
5638aefb4d4aSRobert Shearman 		size += nla_total_size(0);
5639aefb4d4aSRobert Shearman 
56405fa85a09SFlorian Westphal 		rcu_read_lock();
56415fa85a09SFlorian Westphal 		list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
5642aefb4d4aSRobert Shearman 			if (af_ops->get_stats_af_size) {
5643aefb4d4aSRobert Shearman 				size += nla_total_size(
5644aefb4d4aSRobert Shearman 					af_ops->get_stats_af_size(dev));
5645aefb4d4aSRobert Shearman 
5646aefb4d4aSRobert Shearman 				/* for AF_* */
5647aefb4d4aSRobert Shearman 				size += nla_total_size(0);
5648aefb4d4aSRobert Shearman 			}
5649aefb4d4aSRobert Shearman 		}
56505fa85a09SFlorian Westphal 		rcu_read_unlock();
5651aefb4d4aSRobert Shearman 	}
5652aefb4d4aSRobert Shearman 
565310c9ead9SRoopa Prabhu 	return size;
565410c9ead9SRoopa Prabhu }
565510c9ead9SRoopa Prabhu 
565646efc97bSPetr Machata #define RTNL_STATS_OFFLOAD_XSTATS_VALID ((1 << __IFLA_OFFLOAD_XSTATS_MAX) - 1)
565746efc97bSPetr Machata 
565846efc97bSPetr Machata static const struct nla_policy
565946efc97bSPetr Machata rtnl_stats_get_policy_filters[IFLA_STATS_MAX + 1] = {
566046efc97bSPetr Machata 	[IFLA_STATS_LINK_OFFLOAD_XSTATS] =
566146efc97bSPetr Machata 		    NLA_POLICY_MASK(NLA_U32, RTNL_STATS_OFFLOAD_XSTATS_VALID),
566246efc97bSPetr Machata };
566346efc97bSPetr Machata 
566446efc97bSPetr Machata static const struct nla_policy
566546efc97bSPetr Machata rtnl_stats_get_policy[IFLA_STATS_GETSET_MAX + 1] = {
566646efc97bSPetr Machata 	[IFLA_STATS_GET_FILTERS] =
566746efc97bSPetr Machata 		    NLA_POLICY_NESTED(rtnl_stats_get_policy_filters),
566846efc97bSPetr Machata };
566946efc97bSPetr Machata 
567003ba3566SPetr Machata static const struct nla_policy
567103ba3566SPetr Machata ifla_stats_set_policy[IFLA_STATS_GETSET_MAX + 1] = {
56725fd0b838SPetr Machata 	[IFLA_STATS_SET_OFFLOAD_XSTATS_L3_STATS] = NLA_POLICY_MAX(NLA_U8, 1),
567303ba3566SPetr Machata };
567403ba3566SPetr Machata 
567546efc97bSPetr Machata static int rtnl_stats_get_parse_filters(struct nlattr *ifla_filters,
567646efc97bSPetr Machata 					struct rtnl_stats_dump_filters *filters,
567746efc97bSPetr Machata 					struct netlink_ext_ack *extack)
567846efc97bSPetr Machata {
567946efc97bSPetr Machata 	struct nlattr *tb[IFLA_STATS_MAX + 1];
568046efc97bSPetr Machata 	int err;
568146efc97bSPetr Machata 	int at;
568246efc97bSPetr Machata 
568346efc97bSPetr Machata 	err = nla_parse_nested(tb, IFLA_STATS_MAX, ifla_filters,
568446efc97bSPetr Machata 			       rtnl_stats_get_policy_filters, extack);
568546efc97bSPetr Machata 	if (err < 0)
568646efc97bSPetr Machata 		return err;
568746efc97bSPetr Machata 
568846efc97bSPetr Machata 	for (at = 1; at <= IFLA_STATS_MAX; at++) {
568946efc97bSPetr Machata 		if (tb[at]) {
569046efc97bSPetr Machata 			if (!(filters->mask[0] & IFLA_STATS_FILTER_BIT(at))) {
569146efc97bSPetr Machata 				NL_SET_ERR_MSG(extack, "Filtered attribute not enabled in filter_mask");
569246efc97bSPetr Machata 				return -EINVAL;
569346efc97bSPetr Machata 			}
569446efc97bSPetr Machata 			filters->mask[at] = nla_get_u32(tb[at]);
569546efc97bSPetr Machata 		}
569646efc97bSPetr Machata 	}
569746efc97bSPetr Machata 
569846efc97bSPetr Machata 	return 0;
569946efc97bSPetr Machata }
570046efc97bSPetr Machata 
570146efc97bSPetr Machata static int rtnl_stats_get_parse(const struct nlmsghdr *nlh,
570246efc97bSPetr Machata 				u32 filter_mask,
570346efc97bSPetr Machata 				struct rtnl_stats_dump_filters *filters,
570446efc97bSPetr Machata 				struct netlink_ext_ack *extack)
570546efc97bSPetr Machata {
570646efc97bSPetr Machata 	struct nlattr *tb[IFLA_STATS_GETSET_MAX + 1];
570746efc97bSPetr Machata 	int err;
570846efc97bSPetr Machata 	int i;
570946efc97bSPetr Machata 
571046efc97bSPetr Machata 	filters->mask[0] = filter_mask;
571146efc97bSPetr Machata 	for (i = 1; i < ARRAY_SIZE(filters->mask); i++)
571246efc97bSPetr Machata 		filters->mask[i] = -1U;
571346efc97bSPetr Machata 
571446efc97bSPetr Machata 	err = nlmsg_parse(nlh, sizeof(struct if_stats_msg), tb,
571546efc97bSPetr Machata 			  IFLA_STATS_GETSET_MAX, rtnl_stats_get_policy, extack);
571646efc97bSPetr Machata 	if (err < 0)
571746efc97bSPetr Machata 		return err;
571846efc97bSPetr Machata 
571946efc97bSPetr Machata 	if (tb[IFLA_STATS_GET_FILTERS]) {
572046efc97bSPetr Machata 		err = rtnl_stats_get_parse_filters(tb[IFLA_STATS_GET_FILTERS],
572146efc97bSPetr Machata 						   filters, extack);
572246efc97bSPetr Machata 		if (err)
572346efc97bSPetr Machata 			return err;
572446efc97bSPetr Machata 	}
572546efc97bSPetr Machata 
572646efc97bSPetr Machata 	return 0;
572746efc97bSPetr Machata }
572846efc97bSPetr Machata 
572951bc860dSJakub Kicinski static int rtnl_valid_stats_req(const struct nlmsghdr *nlh, bool strict_check,
573051bc860dSJakub Kicinski 				bool is_dump, struct netlink_ext_ack *extack)
573151bc860dSJakub Kicinski {
573251bc860dSJakub Kicinski 	struct if_stats_msg *ifsm;
573351bc860dSJakub Kicinski 
573469f23a09SEric Dumazet 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifsm))) {
573551bc860dSJakub Kicinski 		NL_SET_ERR_MSG(extack, "Invalid header for stats dump");
573651bc860dSJakub Kicinski 		return -EINVAL;
573751bc860dSJakub Kicinski 	}
573851bc860dSJakub Kicinski 
573951bc860dSJakub Kicinski 	if (!strict_check)
574051bc860dSJakub Kicinski 		return 0;
574151bc860dSJakub Kicinski 
574251bc860dSJakub Kicinski 	ifsm = nlmsg_data(nlh);
574351bc860dSJakub Kicinski 
574451bc860dSJakub Kicinski 	/* only requests using strict checks can pass data to influence
574551bc860dSJakub Kicinski 	 * the dump. The legacy exception is filter_mask.
574651bc860dSJakub Kicinski 	 */
574751bc860dSJakub Kicinski 	if (ifsm->pad1 || ifsm->pad2 || (is_dump && ifsm->ifindex)) {
574851bc860dSJakub Kicinski 		NL_SET_ERR_MSG(extack, "Invalid values in header for stats dump request");
574951bc860dSJakub Kicinski 		return -EINVAL;
575051bc860dSJakub Kicinski 	}
57516300acb2SJakub Kicinski 	if (ifsm->filter_mask >= IFLA_STATS_FILTER_BIT(IFLA_STATS_MAX + 1)) {
57526300acb2SJakub Kicinski 		NL_SET_ERR_MSG(extack, "Invalid stats requested through filter mask");
57536300acb2SJakub Kicinski 		return -EINVAL;
57546300acb2SJakub Kicinski 	}
575551bc860dSJakub Kicinski 
575651bc860dSJakub Kicinski 	return 0;
575751bc860dSJakub Kicinski }
575851bc860dSJakub Kicinski 
5759c21ef3e3SDavid Ahern static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
5760c21ef3e3SDavid Ahern 			  struct netlink_ext_ack *extack)
576110c9ead9SRoopa Prabhu {
576246efc97bSPetr Machata 	struct rtnl_stats_dump_filters filters;
576310c9ead9SRoopa Prabhu 	struct net *net = sock_net(skb->sk);
576410c9ead9SRoopa Prabhu 	struct net_device *dev = NULL;
5765e8872a25SNikolay Aleksandrov 	int idxattr = 0, prividx = 0;
5766e8872a25SNikolay Aleksandrov 	struct if_stats_msg *ifsm;
576710c9ead9SRoopa Prabhu 	struct sk_buff *nskb;
576810c9ead9SRoopa Prabhu 	int err;
576910c9ead9SRoopa Prabhu 
577051bc860dSJakub Kicinski 	err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb),
577151bc860dSJakub Kicinski 				   false, extack);
577251bc860dSJakub Kicinski 	if (err)
577351bc860dSJakub Kicinski 		return err;
57744775cc1fSMathias Krause 
577510c9ead9SRoopa Prabhu 	ifsm = nlmsg_data(nlh);
577610c9ead9SRoopa Prabhu 	if (ifsm->ifindex > 0)
577710c9ead9SRoopa Prabhu 		dev = __dev_get_by_index(net, ifsm->ifindex);
577810c9ead9SRoopa Prabhu 	else
577910c9ead9SRoopa Prabhu 		return -EINVAL;
578010c9ead9SRoopa Prabhu 
578110c9ead9SRoopa Prabhu 	if (!dev)
578210c9ead9SRoopa Prabhu 		return -ENODEV;
578310c9ead9SRoopa Prabhu 
578446efc97bSPetr Machata 	if (!ifsm->filter_mask) {
578522b67d17SPetr Machata 		NL_SET_ERR_MSG(extack, "Filter mask must be set for stats get");
578610c9ead9SRoopa Prabhu 		return -EINVAL;
578722b67d17SPetr Machata 	}
578810c9ead9SRoopa Prabhu 
578946efc97bSPetr Machata 	err = rtnl_stats_get_parse(nlh, ifsm->filter_mask, &filters, extack);
579046efc97bSPetr Machata 	if (err)
579146efc97bSPetr Machata 		return err;
579246efc97bSPetr Machata 
579346efc97bSPetr Machata 	nskb = nlmsg_new(if_nlmsg_stats_size(dev, &filters), GFP_KERNEL);
579410c9ead9SRoopa Prabhu 	if (!nskb)
579510c9ead9SRoopa Prabhu 		return -ENOBUFS;
579610c9ead9SRoopa Prabhu 
579710c9ead9SRoopa Prabhu 	err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
579810c9ead9SRoopa Prabhu 				  NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
579905415bccSPetr Machata 				  0, &filters, &idxattr, &prividx, extack);
580010c9ead9SRoopa Prabhu 	if (err < 0) {
580110c9ead9SRoopa Prabhu 		/* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
580210c9ead9SRoopa Prabhu 		WARN_ON(err == -EMSGSIZE);
580310c9ead9SRoopa Prabhu 		kfree_skb(nskb);
580410c9ead9SRoopa Prabhu 	} else {
580510c9ead9SRoopa Prabhu 		err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
580610c9ead9SRoopa Prabhu 	}
580710c9ead9SRoopa Prabhu 
580810c9ead9SRoopa Prabhu 	return err;
580910c9ead9SRoopa Prabhu }
581010c9ead9SRoopa Prabhu 
581110c9ead9SRoopa Prabhu static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
581210c9ead9SRoopa Prabhu {
5813841891ecSDavid Ahern 	struct netlink_ext_ack *extack = cb->extack;
5814e8872a25SNikolay Aleksandrov 	int h, s_h, err, s_idx, s_idxattr, s_prividx;
581546efc97bSPetr Machata 	struct rtnl_stats_dump_filters filters;
581610c9ead9SRoopa Prabhu 	struct net *net = sock_net(skb->sk);
581710c9ead9SRoopa Prabhu 	unsigned int flags = NLM_F_MULTI;
5818e8872a25SNikolay Aleksandrov 	struct if_stats_msg *ifsm;
5819e8872a25SNikolay Aleksandrov 	struct hlist_head *head;
5820e8872a25SNikolay Aleksandrov 	struct net_device *dev;
5821e8872a25SNikolay Aleksandrov 	int idx = 0;
582210c9ead9SRoopa Prabhu 
582310c9ead9SRoopa Prabhu 	s_h = cb->args[0];
582410c9ead9SRoopa Prabhu 	s_idx = cb->args[1];
5825e8872a25SNikolay Aleksandrov 	s_idxattr = cb->args[2];
5826e8872a25SNikolay Aleksandrov 	s_prividx = cb->args[3];
582710c9ead9SRoopa Prabhu 
582810c9ead9SRoopa Prabhu 	cb->seq = net->dev_base_seq;
582910c9ead9SRoopa Prabhu 
583051bc860dSJakub Kicinski 	err = rtnl_valid_stats_req(cb->nlh, cb->strict_check, true, extack);
583151bc860dSJakub Kicinski 	if (err)
583251bc860dSJakub Kicinski 		return err;
58334775cc1fSMathias Krause 
583410c9ead9SRoopa Prabhu 	ifsm = nlmsg_data(cb->nlh);
583546efc97bSPetr Machata 	if (!ifsm->filter_mask) {
5836841891ecSDavid Ahern 		NL_SET_ERR_MSG(extack, "Filter mask must be set for stats dump");
5837841891ecSDavid Ahern 		return -EINVAL;
5838841891ecSDavid Ahern 	}
583910c9ead9SRoopa Prabhu 
584046efc97bSPetr Machata 	err = rtnl_stats_get_parse(cb->nlh, ifsm->filter_mask, &filters,
584146efc97bSPetr Machata 				   extack);
584246efc97bSPetr Machata 	if (err)
584346efc97bSPetr Machata 		return err;
584446efc97bSPetr Machata 
584510c9ead9SRoopa Prabhu 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
584610c9ead9SRoopa Prabhu 		idx = 0;
584710c9ead9SRoopa Prabhu 		head = &net->dev_index_head[h];
584810c9ead9SRoopa Prabhu 		hlist_for_each_entry(dev, head, index_hlist) {
584910c9ead9SRoopa Prabhu 			if (idx < s_idx)
585010c9ead9SRoopa Prabhu 				goto cont;
585110c9ead9SRoopa Prabhu 			err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
585210c9ead9SRoopa Prabhu 						  NETLINK_CB(cb->skb).portid,
585310c9ead9SRoopa Prabhu 						  cb->nlh->nlmsg_seq, 0,
585446efc97bSPetr Machata 						  flags, &filters,
585505415bccSPetr Machata 						  &s_idxattr, &s_prividx,
585605415bccSPetr Machata 						  extack);
585710c9ead9SRoopa Prabhu 			/* If we ran out of room on the first message,
585810c9ead9SRoopa Prabhu 			 * we're in trouble
585910c9ead9SRoopa Prabhu 			 */
586010c9ead9SRoopa Prabhu 			WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
586110c9ead9SRoopa Prabhu 
586210c9ead9SRoopa Prabhu 			if (err < 0)
586310c9ead9SRoopa Prabhu 				goto out;
5864e8872a25SNikolay Aleksandrov 			s_prividx = 0;
5865e8872a25SNikolay Aleksandrov 			s_idxattr = 0;
586610c9ead9SRoopa Prabhu 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
586710c9ead9SRoopa Prabhu cont:
586810c9ead9SRoopa Prabhu 			idx++;
586910c9ead9SRoopa Prabhu 		}
587010c9ead9SRoopa Prabhu 	}
587110c9ead9SRoopa Prabhu out:
5872e8872a25SNikolay Aleksandrov 	cb->args[3] = s_prividx;
5873e8872a25SNikolay Aleksandrov 	cb->args[2] = s_idxattr;
587410c9ead9SRoopa Prabhu 	cb->args[1] = idx;
587510c9ead9SRoopa Prabhu 	cb->args[0] = h;
587610c9ead9SRoopa Prabhu 
587710c9ead9SRoopa Prabhu 	return skb->len;
587810c9ead9SRoopa Prabhu }
587910c9ead9SRoopa Prabhu 
58805fd0b838SPetr Machata void rtnl_offload_xstats_notify(struct net_device *dev)
58815fd0b838SPetr Machata {
58825fd0b838SPetr Machata 	struct rtnl_stats_dump_filters response_filters = {};
58835fd0b838SPetr Machata 	struct net *net = dev_net(dev);
58845fd0b838SPetr Machata 	int idxattr = 0, prividx = 0;
58855fd0b838SPetr Machata 	struct sk_buff *skb;
58865fd0b838SPetr Machata 	int err = -ENOBUFS;
58875fd0b838SPetr Machata 
58885fd0b838SPetr Machata 	ASSERT_RTNL();
58895fd0b838SPetr Machata 
58905fd0b838SPetr Machata 	response_filters.mask[0] |=
58915fd0b838SPetr Machata 		IFLA_STATS_FILTER_BIT(IFLA_STATS_LINK_OFFLOAD_XSTATS);
58925fd0b838SPetr Machata 	response_filters.mask[IFLA_STATS_LINK_OFFLOAD_XSTATS] |=
58935fd0b838SPetr Machata 		IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_HW_S_INFO);
58945fd0b838SPetr Machata 
58955fd0b838SPetr Machata 	skb = nlmsg_new(if_nlmsg_stats_size(dev, &response_filters),
58965fd0b838SPetr Machata 			GFP_KERNEL);
58975fd0b838SPetr Machata 	if (!skb)
58985fd0b838SPetr Machata 		goto errout;
58995fd0b838SPetr Machata 
59005fd0b838SPetr Machata 	err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS, 0, 0, 0, 0,
59015fd0b838SPetr Machata 				  &response_filters, &idxattr, &prividx, NULL);
59025fd0b838SPetr Machata 	if (err < 0) {
59035fd0b838SPetr Machata 		kfree_skb(skb);
59045fd0b838SPetr Machata 		goto errout;
59055fd0b838SPetr Machata 	}
59065fd0b838SPetr Machata 
59075fd0b838SPetr Machata 	rtnl_notify(skb, net, 0, RTNLGRP_STATS, NULL, GFP_KERNEL);
59085fd0b838SPetr Machata 	return;
59095fd0b838SPetr Machata 
59105fd0b838SPetr Machata errout:
59115fd0b838SPetr Machata 	rtnl_set_sk_err(net, RTNLGRP_STATS, err);
59125fd0b838SPetr Machata }
59135fd0b838SPetr Machata EXPORT_SYMBOL(rtnl_offload_xstats_notify);
59145fd0b838SPetr Machata 
591503ba3566SPetr Machata static int rtnl_stats_set(struct sk_buff *skb, struct nlmsghdr *nlh,
591603ba3566SPetr Machata 			  struct netlink_ext_ack *extack)
591703ba3566SPetr Machata {
59185fd0b838SPetr Machata 	enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3;
591903ba3566SPetr Machata 	struct rtnl_stats_dump_filters response_filters = {};
592003ba3566SPetr Machata 	struct nlattr *tb[IFLA_STATS_GETSET_MAX + 1];
592103ba3566SPetr Machata 	struct net *net = sock_net(skb->sk);
592203ba3566SPetr Machata 	struct net_device *dev = NULL;
592303ba3566SPetr Machata 	struct if_stats_msg *ifsm;
59245fd0b838SPetr Machata 	bool notify = false;
592503ba3566SPetr Machata 	int err;
592603ba3566SPetr Machata 
592703ba3566SPetr Machata 	err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb),
592803ba3566SPetr Machata 				   false, extack);
592903ba3566SPetr Machata 	if (err)
593003ba3566SPetr Machata 		return err;
593103ba3566SPetr Machata 
593203ba3566SPetr Machata 	ifsm = nlmsg_data(nlh);
593303ba3566SPetr Machata 	if (ifsm->family != AF_UNSPEC) {
593403ba3566SPetr Machata 		NL_SET_ERR_MSG(extack, "Address family should be AF_UNSPEC");
593503ba3566SPetr Machata 		return -EINVAL;
593603ba3566SPetr Machata 	}
593703ba3566SPetr Machata 
593803ba3566SPetr Machata 	if (ifsm->ifindex > 0)
593903ba3566SPetr Machata 		dev = __dev_get_by_index(net, ifsm->ifindex);
594003ba3566SPetr Machata 	else
594103ba3566SPetr Machata 		return -EINVAL;
594203ba3566SPetr Machata 
594303ba3566SPetr Machata 	if (!dev)
594403ba3566SPetr Machata 		return -ENODEV;
594503ba3566SPetr Machata 
594603ba3566SPetr Machata 	if (ifsm->filter_mask) {
594703ba3566SPetr Machata 		NL_SET_ERR_MSG(extack, "Filter mask must be 0 for stats set");
594803ba3566SPetr Machata 		return -EINVAL;
594903ba3566SPetr Machata 	}
595003ba3566SPetr Machata 
595103ba3566SPetr Machata 	err = nlmsg_parse(nlh, sizeof(*ifsm), tb, IFLA_STATS_GETSET_MAX,
595203ba3566SPetr Machata 			  ifla_stats_set_policy, extack);
595303ba3566SPetr Machata 	if (err < 0)
595403ba3566SPetr Machata 		return err;
595503ba3566SPetr Machata 
59565fd0b838SPetr Machata 	if (tb[IFLA_STATS_SET_OFFLOAD_XSTATS_L3_STATS]) {
59575fd0b838SPetr Machata 		u8 req = nla_get_u8(tb[IFLA_STATS_SET_OFFLOAD_XSTATS_L3_STATS]);
595803ba3566SPetr Machata 
59595fd0b838SPetr Machata 		if (req)
59605fd0b838SPetr Machata 			err = netdev_offload_xstats_enable(dev, t_l3, extack);
59615fd0b838SPetr Machata 		else
59625fd0b838SPetr Machata 			err = netdev_offload_xstats_disable(dev, t_l3);
59635fd0b838SPetr Machata 
59645fd0b838SPetr Machata 		if (!err)
59655fd0b838SPetr Machata 			notify = true;
59665fd0b838SPetr Machata 		else if (err != -EALREADY)
59675fd0b838SPetr Machata 			return err;
59685fd0b838SPetr Machata 
59695fd0b838SPetr Machata 		response_filters.mask[0] |=
59705fd0b838SPetr Machata 			IFLA_STATS_FILTER_BIT(IFLA_STATS_LINK_OFFLOAD_XSTATS);
59715fd0b838SPetr Machata 		response_filters.mask[IFLA_STATS_LINK_OFFLOAD_XSTATS] |=
59725fd0b838SPetr Machata 			IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_HW_S_INFO);
597303ba3566SPetr Machata 	}
597403ba3566SPetr Machata 
59755fd0b838SPetr Machata 	if (notify)
59765fd0b838SPetr Machata 		rtnl_offload_xstats_notify(dev);
59775fd0b838SPetr Machata 
59785fd0b838SPetr Machata 	return 0;
597903ba3566SPetr Machata }
598003ba3566SPetr Machata 
59811da177e4SLinus Torvalds /* Process one rtnetlink message. */
59821da177e4SLinus Torvalds 
59832d4bc933SJohannes Berg static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
59842d4bc933SJohannes Berg 			     struct netlink_ext_ack *extack)
59851da177e4SLinus Torvalds {
59863b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
5987addf9b90SFlorian Westphal 	struct rtnl_link *link;
598812dc5c2cSNikolay Aleksandrov 	enum rtnl_kinds kind;
5989e4202511SFlorian Westphal 	struct module *owner;
59906853dd48SFlorian Westphal 	int err = -EOPNOTSUPP;
5991e2849863SThomas Graf 	rtnl_doit_func doit;
599262256f98SFlorian Westphal 	unsigned int flags;
59931da177e4SLinus Torvalds 	int family;
59941da177e4SLinus Torvalds 	int type;
59951da177e4SLinus Torvalds 
59961da177e4SLinus Torvalds 	type = nlh->nlmsg_type;
59971da177e4SLinus Torvalds 	if (type > RTM_MAX)
5998038890feSThomas Graf 		return -EOPNOTSUPP;
59991da177e4SLinus Torvalds 
60001da177e4SLinus Torvalds 	type -= RTM_BASE;
60011da177e4SLinus Torvalds 
60021da177e4SLinus Torvalds 	/* All the messages must have at least 1 byte length */
6003573ce260SHong zhi guo 	if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
60041da177e4SLinus Torvalds 		return 0;
60051da177e4SLinus Torvalds 
6006573ce260SHong zhi guo 	family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
60072e9ea3e3SNikolay Aleksandrov 	kind = rtnl_msgtype_kind(type);
60081da177e4SLinus Torvalds 
600912dc5c2cSNikolay Aleksandrov 	if (kind != RTNL_KIND_GET && !netlink_net_capable(skb, CAP_NET_ADMIN))
60101d00a4ebSThomas Graf 		return -EPERM;
60111da177e4SLinus Torvalds 
60126853dd48SFlorian Westphal 	rcu_read_lock();
601312dc5c2cSNikolay Aleksandrov 	if (kind == RTNL_KIND_GET && (nlh->nlmsg_flags & NLM_F_DUMP)) {
601497c53cacSDenis V. Lunev 		struct sock *rtnl;
6015e2849863SThomas Graf 		rtnl_dumpit_func dumpit;
6016ebfe3c51SDi Zhu 		u32 min_dump_alloc = 0;
60171da177e4SLinus Torvalds 
6018addf9b90SFlorian Westphal 		link = rtnl_get_link(family, type);
6019addf9b90SFlorian Westphal 		if (!link || !link->dumpit) {
60206853dd48SFlorian Westphal 			family = PF_UNSPEC;
6021addf9b90SFlorian Westphal 			link = rtnl_get_link(family, type);
6022addf9b90SFlorian Westphal 			if (!link || !link->dumpit)
60236853dd48SFlorian Westphal 				goto err_unlock;
60246853dd48SFlorian Westphal 		}
6025e4202511SFlorian Westphal 		owner = link->owner;
6026addf9b90SFlorian Westphal 		dumpit = link->dumpit;
60276853dd48SFlorian Westphal 
60285c2bb9b6SFlorian Westphal 		if (type == RTM_GETLINK - RTM_BASE)
6029e1fa6d21SFlorian Westphal 			min_dump_alloc = rtnl_calcit(skb, nlh);
60301da177e4SLinus Torvalds 
6031e4202511SFlorian Westphal 		err = 0;
6032e4202511SFlorian Westphal 		/* need to do this before rcu_read_unlock() */
6033e4202511SFlorian Westphal 		if (!try_module_get(owner))
6034e4202511SFlorian Westphal 			err = -EPROTONOSUPPORT;
6035e4202511SFlorian Westphal 
60366853dd48SFlorian Westphal 		rcu_read_unlock();
60376853dd48SFlorian Westphal 
603897c53cacSDenis V. Lunev 		rtnl = net->rtnl;
6039e4202511SFlorian Westphal 		if (err == 0) {
604080d326faSPablo Neira Ayuso 			struct netlink_dump_control c = {
604180d326faSPablo Neira Ayuso 				.dump		= dumpit,
604280d326faSPablo Neira Ayuso 				.min_dump_alloc	= min_dump_alloc,
6043e4202511SFlorian Westphal 				.module		= owner,
604480d326faSPablo Neira Ayuso 			};
604580d326faSPablo Neira Ayuso 			err = netlink_dump_start(rtnl, skb, nlh, &c);
6046e4202511SFlorian Westphal 			/* netlink_dump_start() will keep a reference on
6047e4202511SFlorian Westphal 			 * module if dump is still in progress.
6048e4202511SFlorian Westphal 			 */
6049e4202511SFlorian Westphal 			module_put(owner);
605080d326faSPablo Neira Ayuso 		}
60512907c35fSEric Dumazet 		return err;
60521da177e4SLinus Torvalds 	}
60531da177e4SLinus Torvalds 
6054addf9b90SFlorian Westphal 	link = rtnl_get_link(family, type);
6055addf9b90SFlorian Westphal 	if (!link || !link->doit) {
60568caa38b5SFlorian Westphal 		family = PF_UNSPEC;
6057addf9b90SFlorian Westphal 		link = rtnl_get_link(PF_UNSPEC, type);
6058addf9b90SFlorian Westphal 		if (!link || !link->doit)
6059addf9b90SFlorian Westphal 			goto out_unlock;
60608caa38b5SFlorian Westphal 	}
60618caa38b5SFlorian Westphal 
6062e4202511SFlorian Westphal 	owner = link->owner;
6063e4202511SFlorian Westphal 	if (!try_module_get(owner)) {
6064e4202511SFlorian Westphal 		err = -EPROTONOSUPPORT;
6065e4202511SFlorian Westphal 		goto out_unlock;
6066e4202511SFlorian Westphal 	}
6067e4202511SFlorian Westphal 
6068addf9b90SFlorian Westphal 	flags = link->flags;
6069a6cec0bcSNikolay Aleksandrov 	if (kind == RTNL_KIND_DEL && (nlh->nlmsg_flags & NLM_F_BULK) &&
6070a6cec0bcSNikolay Aleksandrov 	    !(flags & RTNL_FLAG_BULK_DEL_SUPPORTED)) {
6071a6cec0bcSNikolay Aleksandrov 		NL_SET_ERR_MSG(extack, "Bulk delete is not supported");
6072a6cec0bcSNikolay Aleksandrov 		goto err_unlock;
6073a6cec0bcSNikolay Aleksandrov 	}
6074a6cec0bcSNikolay Aleksandrov 
607562256f98SFlorian Westphal 	if (flags & RTNL_FLAG_DOIT_UNLOCKED) {
6076addf9b90SFlorian Westphal 		doit = link->doit;
607762256f98SFlorian Westphal 		rcu_read_unlock();
607862256f98SFlorian Westphal 		if (doit)
607962256f98SFlorian Westphal 			err = doit(skb, nlh, extack);
6080e4202511SFlorian Westphal 		module_put(owner);
608162256f98SFlorian Westphal 		return err;
608262256f98SFlorian Westphal 	}
60836853dd48SFlorian Westphal 	rcu_read_unlock();
60846853dd48SFlorian Westphal 
60850cc09020SFlorian Westphal 	rtnl_lock();
6086addf9b90SFlorian Westphal 	link = rtnl_get_link(family, type);
6087addf9b90SFlorian Westphal 	if (link && link->doit)
6088addf9b90SFlorian Westphal 		err = link->doit(skb, nlh, extack);
60890cc09020SFlorian Westphal 	rtnl_unlock();
6090addf9b90SFlorian Westphal 
6091e4202511SFlorian Westphal 	module_put(owner);
6092e4202511SFlorian Westphal 
6093addf9b90SFlorian Westphal 	return err;
6094addf9b90SFlorian Westphal 
6095addf9b90SFlorian Westphal out_unlock:
6096addf9b90SFlorian Westphal 	rcu_read_unlock();
60970cc09020SFlorian Westphal 	return err;
60980cc09020SFlorian Westphal 
60990cc09020SFlorian Westphal err_unlock:
61006853dd48SFlorian Westphal 	rcu_read_unlock();
61010cc09020SFlorian Westphal 	return -EOPNOTSUPP;
61021da177e4SLinus Torvalds }
61031da177e4SLinus Torvalds 
6104cd40b7d3SDenis V. Lunev static void rtnetlink_rcv(struct sk_buff *skb)
61051da177e4SLinus Torvalds {
6106cd40b7d3SDenis V. Lunev 	netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
61071da177e4SLinus Torvalds }
61081da177e4SLinus Torvalds 
61095f729eaaSJulien Gomes static int rtnetlink_bind(struct net *net, int group)
61105f729eaaSJulien Gomes {
61115f729eaaSJulien Gomes 	switch (group) {
61125f729eaaSJulien Gomes 	case RTNLGRP_IPV4_MROUTE_R:
61135f729eaaSJulien Gomes 	case RTNLGRP_IPV6_MROUTE_R:
61145f729eaaSJulien Gomes 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
61155f729eaaSJulien Gomes 			return -EPERM;
61165f729eaaSJulien Gomes 		break;
61175f729eaaSJulien Gomes 	}
61185f729eaaSJulien Gomes 	return 0;
61195f729eaaSJulien Gomes }
61205f729eaaSJulien Gomes 
61211da177e4SLinus Torvalds static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
61221da177e4SLinus Torvalds {
6123351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
6124e9dc8653SEric W. Biederman 
61251da177e4SLinus Torvalds 	switch (event) {
61265138e86fSVlad Yasevich 	case NETDEV_REBOOT:
61278a212589SXin Long 	case NETDEV_CHANGEMTU:
61283753654eSDavid Ahern 	case NETDEV_CHANGEADDR:
61295138e86fSVlad Yasevich 	case NETDEV_CHANGENAME:
61305138e86fSVlad Yasevich 	case NETDEV_FEAT_CHANGE:
61315138e86fSVlad Yasevich 	case NETDEV_BONDING_FAILOVER:
6132e6e66594SXin Long 	case NETDEV_POST_TYPE_CHANGE:
61335138e86fSVlad Yasevich 	case NETDEV_NOTIFY_PEERS:
6134dc709f37SXin Long 	case NETDEV_CHANGEUPPER:
61355138e86fSVlad Yasevich 	case NETDEV_RESEND_IGMP:
61365138e86fSVlad Yasevich 	case NETDEV_CHANGEINFODATA:
6137eeda3fb9SXin Long 	case NETDEV_CHANGELOWERSTATE:
6138ebdcf045SXin Long 	case NETDEV_CHANGE_TX_QUEUE_LEN:
61393d3ea5afSVlad Yasevich 		rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
614038e01b30SNicolas Dichtel 				   GFP_KERNEL, NULL, 0);
61411da177e4SLinus Torvalds 		break;
61421da177e4SLinus Torvalds 	default:
61431da177e4SLinus Torvalds 		break;
61441da177e4SLinus Torvalds 	}
61451da177e4SLinus Torvalds 	return NOTIFY_DONE;
61461da177e4SLinus Torvalds }
61471da177e4SLinus Torvalds 
61481da177e4SLinus Torvalds static struct notifier_block rtnetlink_dev_notifier = {
61491da177e4SLinus Torvalds 	.notifier_call	= rtnetlink_event,
61501da177e4SLinus Torvalds };
61511da177e4SLinus Torvalds 
615297c53cacSDenis V. Lunev 
61532c8c1e72SAlexey Dobriyan static int __net_init rtnetlink_net_init(struct net *net)
615497c53cacSDenis V. Lunev {
615597c53cacSDenis V. Lunev 	struct sock *sk;
6156a31f2d17SPablo Neira Ayuso 	struct netlink_kernel_cfg cfg = {
6157a31f2d17SPablo Neira Ayuso 		.groups		= RTNLGRP_MAX,
6158a31f2d17SPablo Neira Ayuso 		.input		= rtnetlink_rcv,
6159a31f2d17SPablo Neira Ayuso 		.cb_mutex	= &rtnl_mutex,
61609785e10aSPablo Neira Ayuso 		.flags		= NL_CFG_F_NONROOT_RECV,
61615f729eaaSJulien Gomes 		.bind		= rtnetlink_bind,
6162a31f2d17SPablo Neira Ayuso 	};
6163a31f2d17SPablo Neira Ayuso 
61649f00d977SPablo Neira Ayuso 	sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
616597c53cacSDenis V. Lunev 	if (!sk)
616697c53cacSDenis V. Lunev 		return -ENOMEM;
616797c53cacSDenis V. Lunev 	net->rtnl = sk;
616897c53cacSDenis V. Lunev 	return 0;
616997c53cacSDenis V. Lunev }
617097c53cacSDenis V. Lunev 
61712c8c1e72SAlexey Dobriyan static void __net_exit rtnetlink_net_exit(struct net *net)
617297c53cacSDenis V. Lunev {
6173b7c6ba6eSDenis V. Lunev 	netlink_kernel_release(net->rtnl);
617497c53cacSDenis V. Lunev 	net->rtnl = NULL;
617597c53cacSDenis V. Lunev }
617697c53cacSDenis V. Lunev 
617797c53cacSDenis V. Lunev static struct pernet_operations rtnetlink_net_ops = {
617897c53cacSDenis V. Lunev 	.init = rtnetlink_net_init,
617997c53cacSDenis V. Lunev 	.exit = rtnetlink_net_exit,
618097c53cacSDenis V. Lunev };
618197c53cacSDenis V. Lunev 
61821da177e4SLinus Torvalds void __init rtnetlink_init(void)
61831da177e4SLinus Torvalds {
618497c53cacSDenis V. Lunev 	if (register_pernet_subsys(&rtnetlink_net_ops))
61851da177e4SLinus Torvalds 		panic("rtnetlink_init: cannot initialize rtnetlink\n");
618697c53cacSDenis V. Lunev 
61871da177e4SLinus Torvalds 	register_netdevice_notifier(&rtnetlink_dev_notifier);
6188340d17fcSThomas Graf 
6189c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
6190b97bac64SFlorian Westphal 		      rtnl_dump_ifinfo, 0);
6191b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0);
6192b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0);
6193b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0);
6194687ad8ccSThomas Graf 
6195b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0);
6196b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0);
6197b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0);
619877162022SJohn Fastabend 
619936fbf1e5SJiri Pirko 	rtnl_register(PF_UNSPEC, RTM_NEWLINKPROP, rtnl_newlinkprop, NULL, 0);
620036fbf1e5SJiri Pirko 	rtnl_register(PF_UNSPEC, RTM_DELLINKPROP, rtnl_dellinkprop, NULL, 0);
620136fbf1e5SJiri Pirko 
6202b97bac64SFlorian Westphal 	rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
62039e834259SNikolay Aleksandrov 	rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL,
62049e834259SNikolay Aleksandrov 		      RTNL_FLAG_BULK_DEL_SUPPORTED);
62055b2f94b2SRoopa Prabhu 	rtnl_register(PF_BRIDGE, RTM_GETNEIGH, rtnl_fdb_get, rtnl_fdb_dump, 0);
6206e5a55a89SJohn Fastabend 
6207b97bac64SFlorian Westphal 	rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0);
6208b97bac64SFlorian Westphal 	rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0);
6209b97bac64SFlorian Westphal 	rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0);
621010c9ead9SRoopa Prabhu 
621110c9ead9SRoopa Prabhu 	rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,
6212b97bac64SFlorian Westphal 		      0);
621303ba3566SPetr Machata 	rtnl_register(PF_UNSPEC, RTM_SETSTATS, rtnl_stats_set, NULL, 0);
62141da177e4SLinus Torvalds }
6215