xref: /openbmc/linux/net/core/rtnetlink.c (revision a4abfa627c3865c37e036bccb681619a50d3d93c)
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:
869abbc7928SJuhee Kang 		if (netif_oper_up(dev))
870eec517cdSAndrew Lunn 			operstate = IF_OPER_TESTING;
871eec517cdSAndrew Lunn 		break;
872eec517cdSAndrew Lunn 
873b00055aaSStefan Rompf 	case IF_OPER_DORMANT:
874abbc7928SJuhee Kang 		if (netif_oper_up(dev))
875b00055aaSStefan Rompf 			operstate = IF_OPER_DORMANT;
876b00055aaSStefan Rompf 		break;
8773ff50b79SStephen Hemminger 	}
878b00055aaSStefan Rompf 
879b00055aaSStefan Rompf 	if (dev->operstate != operstate) {
880fd888e85SSebastian Andrzej Siewior 		write_lock(&dev_base_lock);
881b00055aaSStefan Rompf 		dev->operstate = operstate;
882fd888e85SSebastian Andrzej Siewior 		write_unlock(&dev_base_lock);
883b00055aaSStefan Rompf 		netdev_state_change(dev);
88493b2d4a2SDavid S. Miller 	}
885b00055aaSStefan Rompf }
886b00055aaSStefan Rompf 
887b1beb681SJiri Benc static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
888b1beb681SJiri Benc {
889b1beb681SJiri Benc 	return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
890b1beb681SJiri Benc 	       (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
891b1beb681SJiri Benc }
892b1beb681SJiri Benc 
8933729d502SPatrick McHardy static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
8943729d502SPatrick McHardy 					   const struct ifinfomsg *ifm)
8953729d502SPatrick McHardy {
8963729d502SPatrick McHardy 	unsigned int flags = ifm->ifi_flags;
8973729d502SPatrick McHardy 
8983729d502SPatrick McHardy 	/* bugwards compatibility: ifi_change == 0 is treated as ~0 */
8993729d502SPatrick McHardy 	if (ifm->ifi_change)
9003729d502SPatrick McHardy 		flags = (flags & ifm->ifi_change) |
901b1beb681SJiri Benc 			(rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
9023729d502SPatrick McHardy 
9033729d502SPatrick McHardy 	return flags;
9043729d502SPatrick McHardy }
9053729d502SPatrick McHardy 
906b60c5115SThomas Graf static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
907be1f3c2cSBen Hutchings 				 const struct rtnl_link_stats64 *b)
9081da177e4SLinus Torvalds {
909b60c5115SThomas Graf 	a->rx_packets = b->rx_packets;
910b60c5115SThomas Graf 	a->tx_packets = b->tx_packets;
911b60c5115SThomas Graf 	a->rx_bytes = b->rx_bytes;
912b60c5115SThomas Graf 	a->tx_bytes = b->tx_bytes;
913b60c5115SThomas Graf 	a->rx_errors = b->rx_errors;
914b60c5115SThomas Graf 	a->tx_errors = b->tx_errors;
915b60c5115SThomas Graf 	a->rx_dropped = b->rx_dropped;
916b60c5115SThomas Graf 	a->tx_dropped = b->tx_dropped;
917b60c5115SThomas Graf 
918b60c5115SThomas Graf 	a->multicast = b->multicast;
919b60c5115SThomas Graf 	a->collisions = b->collisions;
920b60c5115SThomas Graf 
921b60c5115SThomas Graf 	a->rx_length_errors = b->rx_length_errors;
922b60c5115SThomas Graf 	a->rx_over_errors = b->rx_over_errors;
923b60c5115SThomas Graf 	a->rx_crc_errors = b->rx_crc_errors;
924b60c5115SThomas Graf 	a->rx_frame_errors = b->rx_frame_errors;
925b60c5115SThomas Graf 	a->rx_fifo_errors = b->rx_fifo_errors;
926b60c5115SThomas Graf 	a->rx_missed_errors = b->rx_missed_errors;
927b60c5115SThomas Graf 
928b60c5115SThomas Graf 	a->tx_aborted_errors = b->tx_aborted_errors;
929b60c5115SThomas Graf 	a->tx_carrier_errors = b->tx_carrier_errors;
930b60c5115SThomas Graf 	a->tx_fifo_errors = b->tx_fifo_errors;
931b60c5115SThomas Graf 	a->tx_heartbeat_errors = b->tx_heartbeat_errors;
932b60c5115SThomas Graf 	a->tx_window_errors = b->tx_window_errors;
933b60c5115SThomas Graf 
934b60c5115SThomas Graf 	a->rx_compressed = b->rx_compressed;
935b60c5115SThomas Graf 	a->tx_compressed = b->tx_compressed;
9366e7333d3SJarod Wilson 
9376e7333d3SJarod Wilson 	a->rx_nohandler = b->rx_nohandler;
93810708f37SJan Engelhardt }
93910708f37SJan Engelhardt 
940c02db8c6SChris Wright /* All VF info */
941115c9b81SGreg Rose static inline int rtnl_vfinfo_size(const struct net_device *dev,
942115c9b81SGreg Rose 				   u32 ext_filter_mask)
943ebc08a6fSWilliams, Mitch A {
9449af15c38SPhil Sutter 	if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) {
945c02db8c6SChris Wright 		int num_vfs = dev_num_vf(dev->dev.parent);
9467e75f74aSSabrina Dubroca 		size_t size = nla_total_size(0);
947045de01aSScott Feldman 		size += num_vfs *
9487e75f74aSSabrina Dubroca 			(nla_total_size(0) +
9497e75f74aSSabrina Dubroca 			 nla_total_size(sizeof(struct ifla_vf_mac)) +
95075345f88SDenis Kirjanov 			 nla_total_size(sizeof(struct ifla_vf_broadcast)) +
9517e75f74aSSabrina Dubroca 			 nla_total_size(sizeof(struct ifla_vf_vlan)) +
9527e75f74aSSabrina Dubroca 			 nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
95379aab093SMoshe Shemesh 			 nla_total_size(MAX_VLAN_LIST_LEN *
95479aab093SMoshe Shemesh 					sizeof(struct ifla_vf_vlan_info)) +
955ed616689SSucheta Chakraborty 			 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
9567e75f74aSSabrina Dubroca 			 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
957945a3676SJiri Benc 			 nla_total_size(sizeof(struct ifla_vf_rate)) +
95801a3d796SVlad Zolotarov 			 nla_total_size(sizeof(struct ifla_vf_link_state)) +
9593b766cd8SEran Ben Elisha 			 nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
9607e75f74aSSabrina Dubroca 			 nla_total_size(0) + /* nest IFLA_VF_STATS */
9613b766cd8SEran Ben Elisha 			 /* IFLA_VF_STATS_RX_PACKETS */
962343a6d8eSNicolas Dichtel 			 nla_total_size_64bit(sizeof(__u64)) +
9633b766cd8SEran Ben Elisha 			 /* IFLA_VF_STATS_TX_PACKETS */
964343a6d8eSNicolas Dichtel 			 nla_total_size_64bit(sizeof(__u64)) +
9653b766cd8SEran Ben Elisha 			 /* IFLA_VF_STATS_RX_BYTES */
966343a6d8eSNicolas Dichtel 			 nla_total_size_64bit(sizeof(__u64)) +
9673b766cd8SEran Ben Elisha 			 /* IFLA_VF_STATS_TX_BYTES */
968343a6d8eSNicolas Dichtel 			 nla_total_size_64bit(sizeof(__u64)) +
9693b766cd8SEran Ben Elisha 			 /* IFLA_VF_STATS_BROADCAST */
970343a6d8eSNicolas Dichtel 			 nla_total_size_64bit(sizeof(__u64)) +
9713b766cd8SEran Ben Elisha 			 /* IFLA_VF_STATS_MULTICAST */
972343a6d8eSNicolas Dichtel 			 nla_total_size_64bit(sizeof(__u64)) +
973c5a9f6f0SEugenia Emantayev 			 /* IFLA_VF_STATS_RX_DROPPED */
974c5a9f6f0SEugenia Emantayev 			 nla_total_size_64bit(sizeof(__u64)) +
975c5a9f6f0SEugenia Emantayev 			 /* IFLA_VF_STATS_TX_DROPPED */
976c5a9f6f0SEugenia Emantayev 			 nla_total_size_64bit(sizeof(__u64)) +
977dd461d6aSHiroshi Shimamoto 			 nla_total_size(sizeof(struct ifla_vf_trust)));
978c02db8c6SChris Wright 		return size;
979c02db8c6SChris Wright 	} else
980ebc08a6fSWilliams, Mitch A 		return 0;
981ebc08a6fSWilliams, Mitch A }
982ebc08a6fSWilliams, Mitch A 
983c53864fdSDavid Gibson static size_t rtnl_port_size(const struct net_device *dev,
984c53864fdSDavid Gibson 			     u32 ext_filter_mask)
98557b61080SScott Feldman {
98657b61080SScott Feldman 	size_t port_size = nla_total_size(4)		/* PORT_VF */
98757b61080SScott Feldman 		+ nla_total_size(PORT_PROFILE_MAX)	/* PORT_PROFILE */
98857b61080SScott Feldman 		+ nla_total_size(PORT_UUID_MAX)		/* PORT_INSTANCE_UUID */
98957b61080SScott Feldman 		+ nla_total_size(PORT_UUID_MAX)		/* PORT_HOST_UUID */
99057b61080SScott Feldman 		+ nla_total_size(1)			/* PROT_VDP_REQUEST */
99157b61080SScott Feldman 		+ nla_total_size(2);			/* PORT_VDP_RESPONSE */
99257b61080SScott Feldman 	size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
99357b61080SScott Feldman 	size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
99457b61080SScott Feldman 		+ port_size;
99557b61080SScott Feldman 	size_t port_self_size = nla_total_size(sizeof(struct nlattr))
99657b61080SScott Feldman 		+ port_size;
99757b61080SScott Feldman 
998c53864fdSDavid Gibson 	if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
999c53864fdSDavid Gibson 	    !(ext_filter_mask & RTEXT_FILTER_VF))
100057b61080SScott Feldman 		return 0;
100157b61080SScott Feldman 	if (dev_num_vf(dev->dev.parent))
100257b61080SScott Feldman 		return port_self_size + vf_ports_size +
100357b61080SScott Feldman 			vf_port_size * dev_num_vf(dev->dev.parent);
100457b61080SScott Feldman 	else
100557b61080SScott Feldman 		return port_self_size;
100657b61080SScott Feldman }
100757b61080SScott Feldman 
1008b5cdae32SDavid S. Miller static size_t rtnl_xdp_size(void)
1009d1fdd913SBrenden Blanco {
1010b3cfaa31SSabrina Dubroca 	size_t xdp_size = nla_total_size(0) +	/* nest IFLA_XDP */
101158038695SMartin KaFai Lau 			  nla_total_size(1) +	/* XDP_ATTACHED */
1012a25717d2SJakub Kicinski 			  nla_total_size(4) +	/* XDP_PROG_ID (or 1st mode) */
10134f91da26SJakub Kicinski 			  nla_total_size(4);	/* XDP_<mode>_PROG_ID */
1014d1fdd913SBrenden Blanco 
1015d1fdd913SBrenden Blanco 	return xdp_size;
1016d1fdd913SBrenden Blanco }
1017d1fdd913SBrenden Blanco 
101888f4fb0cSJiri Pirko static size_t rtnl_prop_list_size(const struct net_device *dev)
101988f4fb0cSJiri Pirko {
102088f4fb0cSJiri Pirko 	struct netdev_name_node *name_node;
102188f4fb0cSJiri Pirko 	size_t size;
102288f4fb0cSJiri Pirko 
102388f4fb0cSJiri Pirko 	if (list_empty(&dev->name_node->list))
102488f4fb0cSJiri Pirko 		return 0;
102588f4fb0cSJiri Pirko 	size = nla_total_size(0);
102688f4fb0cSJiri Pirko 	list_for_each_entry(name_node, &dev->name_node->list, list)
102788f4fb0cSJiri Pirko 		size += nla_total_size(ALTIFNAMSIZ);
102888f4fb0cSJiri Pirko 	return size;
102988f4fb0cSJiri Pirko }
103088f4fb0cSJiri Pirko 
1031829eb208SRoopa Prabhu static size_t rtnl_proto_down_size(const struct net_device *dev)
1032829eb208SRoopa Prabhu {
1033829eb208SRoopa Prabhu 	size_t size = nla_total_size(1);
1034829eb208SRoopa Prabhu 
1035829eb208SRoopa Prabhu 	if (dev->proto_down_reason)
1036829eb208SRoopa Prabhu 		size += nla_total_size(0) + nla_total_size(4);
1037829eb208SRoopa Prabhu 
1038829eb208SRoopa Prabhu 	return size;
1039829eb208SRoopa Prabhu }
1040829eb208SRoopa Prabhu 
1041115c9b81SGreg Rose static noinline size_t if_nlmsg_size(const struct net_device *dev,
1042115c9b81SGreg Rose 				     u32 ext_filter_mask)
1043339bf98fSThomas Graf {
1044339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
1045339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
10460b815a1aSStephen Hemminger 	       + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
1047339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
1048270cb4d0SNicolas Dichtel 	       + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap))
1049339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rtnl_link_stats))
105035c58459SDavid S. Miller 	       + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
1051339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
1052339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
1053339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_TXQLEN */
1054339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_WEIGHT */
1055339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MTU */
1056339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_LINK */
1057339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MASTER */
10589a57247fSJiri Pirko 	       + nla_total_size(1) /* IFLA_CARRIER */
1059edbc0bb3SBen Greear 	       + nla_total_size(4) /* IFLA_PROMISCUITY */
10607e6e1b57SNicolas Dichtel 	       + nla_total_size(4) /* IFLA_ALLMULTI */
106176ff5cc9SJiri Pirko 	       + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
106276ff5cc9SJiri Pirko 	       + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
10636919756cSTobias Klauser 	       + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
10646919756cSTobias Klauser 	       + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
1065eac1b93cSCoco Li 	       + nla_total_size(4) /* IFLA_GRO_MAX_SIZE */
106689527be8SEric Dumazet 	       + nla_total_size(4) /* IFLA_TSO_MAX_SIZE */
106789527be8SEric Dumazet 	       + nla_total_size(4) /* IFLA_TSO_MAX_SEGS */
1068339bf98fSThomas Graf 	       + nla_total_size(1) /* IFLA_OPERSTATE */
106938f7b870SPatrick McHardy 	       + nla_total_size(1) /* IFLA_LINKMODE */
10702d3b479dSdavid decotigny 	       + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
1071d37512a2SNicolas Dichtel 	       + nla_total_size(4) /* IFLA_LINK_NETNSID */
1072db833d40SSerhey Popovych 	       + nla_total_size(4) /* IFLA_GROUP */
1073115c9b81SGreg Rose 	       + nla_total_size(ext_filter_mask
1074115c9b81SGreg Rose 			        & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
1075115c9b81SGreg Rose 	       + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
1076c53864fdSDavid Gibson 	       + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
1077f8ff182cSThomas Graf 	       + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
1078b1974ed0SArad, Ronen 	       + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
107982f28412SJiri Pirko 	       + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
108088d6378bSAnuradha Karuppiah 	       + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
1081c57c7a95SNicolas Dichtel 	       + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
1082b5cdae32SDavid S. Miller 	       + rtnl_xdp_size() /* IFLA_XDP */
10833d3ea5afSVlad Yasevich 	       + nla_total_size(4)  /* IFLA_EVENT */
10846621dd29SNicolas Dichtel 	       + nla_total_size(4)  /* IFLA_NEW_NETNSID */
108538e01b30SNicolas Dichtel 	       + nla_total_size(4)  /* IFLA_NEW_IFINDEX */
1086829eb208SRoopa Prabhu 	       + rtnl_proto_down_size(dev)  /* proto down */
10877e4a8d5aSChristian Brauner 	       + nla_total_size(4)  /* IFLA_TARGET_NETNSID */
1088b2d3bcfaSDavid Decotigny 	       + nla_total_size(4)  /* IFLA_CARRIER_UP_COUNT */
1089b2d3bcfaSDavid Decotigny 	       + nla_total_size(4)  /* IFLA_CARRIER_DOWN_COUNT */
10903e7a50ceSStephen Hemminger 	       + nla_total_size(4)  /* IFLA_MIN_MTU */
10913e7a50ceSStephen Hemminger 	       + nla_total_size(4)  /* IFLA_MAX_MTU */
109288f4fb0cSJiri Pirko 	       + rtnl_prop_list_size(dev)
1093f74877a5SMichal Kubecek 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_PERM_ADDRESS */
109479e1ad14SJiri Benc 	       + 0;
1095339bf98fSThomas Graf }
1096339bf98fSThomas Graf 
109757b61080SScott Feldman static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
109857b61080SScott Feldman {
109957b61080SScott Feldman 	struct nlattr *vf_ports;
110057b61080SScott Feldman 	struct nlattr *vf_port;
110157b61080SScott Feldman 	int vf;
110257b61080SScott Feldman 	int err;
110357b61080SScott Feldman 
1104ae0be8deSMichal Kubecek 	vf_ports = nla_nest_start_noflag(skb, IFLA_VF_PORTS);
110557b61080SScott Feldman 	if (!vf_ports)
110657b61080SScott Feldman 		return -EMSGSIZE;
110757b61080SScott Feldman 
110857b61080SScott Feldman 	for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
1109ae0be8deSMichal Kubecek 		vf_port = nla_nest_start_noflag(skb, IFLA_VF_PORT);
11108ca94183SScott Feldman 		if (!vf_port)
11118ca94183SScott Feldman 			goto nla_put_failure;
1112a6574349SDavid S. Miller 		if (nla_put_u32(skb, IFLA_PORT_VF, vf))
1113a6574349SDavid S. Miller 			goto nla_put_failure;
111457b61080SScott Feldman 		err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
11158ca94183SScott Feldman 		if (err == -EMSGSIZE)
11168ca94183SScott Feldman 			goto nla_put_failure;
111757b61080SScott Feldman 		if (err) {
111857b61080SScott Feldman 			nla_nest_cancel(skb, vf_port);
111957b61080SScott Feldman 			continue;
112057b61080SScott Feldman 		}
112157b61080SScott Feldman 		nla_nest_end(skb, vf_port);
112257b61080SScott Feldman 	}
112357b61080SScott Feldman 
112457b61080SScott Feldman 	nla_nest_end(skb, vf_ports);
112557b61080SScott Feldman 
112657b61080SScott Feldman 	return 0;
11278ca94183SScott Feldman 
11288ca94183SScott Feldman nla_put_failure:
11298ca94183SScott Feldman 	nla_nest_cancel(skb, vf_ports);
11308ca94183SScott Feldman 	return -EMSGSIZE;
113157b61080SScott Feldman }
113257b61080SScott Feldman 
113357b61080SScott Feldman static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
113457b61080SScott Feldman {
113557b61080SScott Feldman 	struct nlattr *port_self;
113657b61080SScott Feldman 	int err;
113757b61080SScott Feldman 
1138ae0be8deSMichal Kubecek 	port_self = nla_nest_start_noflag(skb, IFLA_PORT_SELF);
113957b61080SScott Feldman 	if (!port_self)
114057b61080SScott Feldman 		return -EMSGSIZE;
114157b61080SScott Feldman 
114257b61080SScott Feldman 	err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
114357b61080SScott Feldman 	if (err) {
114457b61080SScott Feldman 		nla_nest_cancel(skb, port_self);
11458ca94183SScott Feldman 		return (err == -EMSGSIZE) ? err : 0;
114657b61080SScott Feldman 	}
114757b61080SScott Feldman 
114857b61080SScott Feldman 	nla_nest_end(skb, port_self);
114957b61080SScott Feldman 
115057b61080SScott Feldman 	return 0;
115157b61080SScott Feldman }
115257b61080SScott Feldman 
1153c53864fdSDavid Gibson static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
1154c53864fdSDavid Gibson 			  u32 ext_filter_mask)
115557b61080SScott Feldman {
115657b61080SScott Feldman 	int err;
115757b61080SScott Feldman 
1158c53864fdSDavid Gibson 	if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
1159c53864fdSDavid Gibson 	    !(ext_filter_mask & RTEXT_FILTER_VF))
116057b61080SScott Feldman 		return 0;
116157b61080SScott Feldman 
116257b61080SScott Feldman 	err = rtnl_port_self_fill(skb, dev);
116357b61080SScott Feldman 	if (err)
116457b61080SScott Feldman 		return err;
116557b61080SScott Feldman 
116657b61080SScott Feldman 	if (dev_num_vf(dev->dev.parent)) {
116757b61080SScott Feldman 		err = rtnl_vf_ports_fill(skb, dev);
116857b61080SScott Feldman 		if (err)
116957b61080SScott Feldman 			return err;
117057b61080SScott Feldman 	}
117157b61080SScott Feldman 
117257b61080SScott Feldman 	return 0;
117357b61080SScott Feldman }
117457b61080SScott Feldman 
117566cae9edSJiri Pirko static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
117666cae9edSJiri Pirko {
117766cae9edSJiri Pirko 	int err;
117802637fceSJiri Pirko 	struct netdev_phys_item_id ppid;
117966cae9edSJiri Pirko 
118066cae9edSJiri Pirko 	err = dev_get_phys_port_id(dev, &ppid);
118166cae9edSJiri Pirko 	if (err) {
118266cae9edSJiri Pirko 		if (err == -EOPNOTSUPP)
118366cae9edSJiri Pirko 			return 0;
118466cae9edSJiri Pirko 		return err;
118566cae9edSJiri Pirko 	}
118666cae9edSJiri Pirko 
118766cae9edSJiri Pirko 	if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
118866cae9edSJiri Pirko 		return -EMSGSIZE;
118966cae9edSJiri Pirko 
119066cae9edSJiri Pirko 	return 0;
119166cae9edSJiri Pirko }
119266cae9edSJiri Pirko 
1193db24a904SDavid Ahern static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1194db24a904SDavid Ahern {
1195db24a904SDavid Ahern 	char name[IFNAMSIZ];
1196db24a904SDavid Ahern 	int err;
1197db24a904SDavid Ahern 
1198db24a904SDavid Ahern 	err = dev_get_phys_port_name(dev, name, sizeof(name));
1199db24a904SDavid Ahern 	if (err) {
1200db24a904SDavid Ahern 		if (err == -EOPNOTSUPP)
1201db24a904SDavid Ahern 			return 0;
1202db24a904SDavid Ahern 		return err;
1203db24a904SDavid Ahern 	}
1204db24a904SDavid Ahern 
120577ef033bSMichal Schmidt 	if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
1206db24a904SDavid Ahern 		return -EMSGSIZE;
1207db24a904SDavid Ahern 
1208db24a904SDavid Ahern 	return 0;
1209db24a904SDavid Ahern }
1210db24a904SDavid Ahern 
121182f28412SJiri Pirko static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
121282f28412SJiri Pirko {
1213bccb3025SFlorian Fainelli 	struct netdev_phys_item_id ppid = { };
121482f28412SJiri Pirko 	int err;
121582f28412SJiri Pirko 
1216bccb3025SFlorian Fainelli 	err = dev_get_port_parent_id(dev, &ppid, false);
121782f28412SJiri Pirko 	if (err) {
121882f28412SJiri Pirko 		if (err == -EOPNOTSUPP)
121982f28412SJiri Pirko 			return 0;
122082f28412SJiri Pirko 		return err;
122182f28412SJiri Pirko 	}
122282f28412SJiri Pirko 
1223bccb3025SFlorian Fainelli 	if (nla_put(skb, IFLA_PHYS_SWITCH_ID, ppid.id_len, ppid.id))
122482f28412SJiri Pirko 		return -EMSGSIZE;
122582f28412SJiri Pirko 
122682f28412SJiri Pirko 	return 0;
122782f28412SJiri Pirko }
122882f28412SJiri Pirko 
1229b22b941bSHannes Frederic Sowa static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1230b22b941bSHannes Frederic Sowa 					      struct net_device *dev)
1231b22b941bSHannes Frederic Sowa {
1232550bce59SRoopa Prabhu 	struct rtnl_link_stats64 *sp;
1233b22b941bSHannes Frederic Sowa 	struct nlattr *attr;
1234b22b941bSHannes Frederic Sowa 
123558414d32SNicolas Dichtel 	attr = nla_reserve_64bit(skb, IFLA_STATS64,
123658414d32SNicolas Dichtel 				 sizeof(struct rtnl_link_stats64), IFLA_PAD);
1237b22b941bSHannes Frederic Sowa 	if (!attr)
1238b22b941bSHannes Frederic Sowa 		return -EMSGSIZE;
1239b22b941bSHannes Frederic Sowa 
1240550bce59SRoopa Prabhu 	sp = nla_data(attr);
1241550bce59SRoopa Prabhu 	dev_get_stats(dev, sp);
1242550bce59SRoopa Prabhu 
1243550bce59SRoopa Prabhu 	attr = nla_reserve(skb, IFLA_STATS,
1244550bce59SRoopa Prabhu 			   sizeof(struct rtnl_link_stats));
1245550bce59SRoopa Prabhu 	if (!attr)
1246550bce59SRoopa Prabhu 		return -EMSGSIZE;
1247550bce59SRoopa Prabhu 
1248550bce59SRoopa Prabhu 	copy_rtnl_link_stats(nla_data(attr), sp);
1249b22b941bSHannes Frederic Sowa 
1250b22b941bSHannes Frederic Sowa 	return 0;
1251b22b941bSHannes Frederic Sowa }
1252b22b941bSHannes Frederic Sowa 
1253b22b941bSHannes Frederic Sowa static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1254b22b941bSHannes Frederic Sowa 					       struct net_device *dev,
1255b22b941bSHannes Frederic Sowa 					       int vfs_num,
1256b22b941bSHannes Frederic Sowa 					       struct nlattr *vfinfo)
1257b22b941bSHannes Frederic Sowa {
1258b22b941bSHannes Frederic Sowa 	struct ifla_vf_rss_query_en vf_rss_query_en;
125979aab093SMoshe Shemesh 	struct nlattr *vf, *vfstats, *vfvlanlist;
1260b22b941bSHannes Frederic Sowa 	struct ifla_vf_link_state vf_linkstate;
126179aab093SMoshe Shemesh 	struct ifla_vf_vlan_info vf_vlan_info;
1262b22b941bSHannes Frederic Sowa 	struct ifla_vf_spoofchk vf_spoofchk;
1263b22b941bSHannes Frederic Sowa 	struct ifla_vf_tx_rate vf_tx_rate;
1264b22b941bSHannes Frederic Sowa 	struct ifla_vf_stats vf_stats;
1265b22b941bSHannes Frederic Sowa 	struct ifla_vf_trust vf_trust;
1266b22b941bSHannes Frederic Sowa 	struct ifla_vf_vlan vf_vlan;
1267b22b941bSHannes Frederic Sowa 	struct ifla_vf_rate vf_rate;
1268b22b941bSHannes Frederic Sowa 	struct ifla_vf_mac vf_mac;
126975345f88SDenis Kirjanov 	struct ifla_vf_broadcast vf_broadcast;
1270b22b941bSHannes Frederic Sowa 	struct ifla_vf_info ivi;
127130aad417SDanit Goldberg 	struct ifla_vf_guid node_guid;
127230aad417SDanit Goldberg 	struct ifla_vf_guid port_guid;
1273b22b941bSHannes Frederic Sowa 
12740eed9cf5SMintz, Yuval 	memset(&ivi, 0, sizeof(ivi));
12750eed9cf5SMintz, Yuval 
1276b22b941bSHannes Frederic Sowa 	/* Not all SR-IOV capable drivers support the
1277b22b941bSHannes Frederic Sowa 	 * spoofcheck and "RSS query enable" query.  Preset to
1278b22b941bSHannes Frederic Sowa 	 * -1 so the user space tool can detect that the driver
1279b22b941bSHannes Frederic Sowa 	 * didn't report anything.
1280b22b941bSHannes Frederic Sowa 	 */
1281b22b941bSHannes Frederic Sowa 	ivi.spoofchk = -1;
1282b22b941bSHannes Frederic Sowa 	ivi.rss_query_en = -1;
1283b22b941bSHannes Frederic Sowa 	ivi.trusted = -1;
1284b22b941bSHannes Frederic Sowa 	/* The default value for VF link state is "auto"
1285b22b941bSHannes Frederic Sowa 	 * IFLA_VF_LINK_STATE_AUTO which equals zero
1286b22b941bSHannes Frederic Sowa 	 */
1287b22b941bSHannes Frederic Sowa 	ivi.linkstate = 0;
128879aab093SMoshe Shemesh 	/* VLAN Protocol by default is 802.1Q */
128979aab093SMoshe Shemesh 	ivi.vlan_proto = htons(ETH_P_8021Q);
1290b22b941bSHannes Frederic Sowa 	if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1291b22b941bSHannes Frederic Sowa 		return 0;
1292b22b941bSHannes Frederic Sowa 
1293775f4f05SDan Carpenter 	memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
12949fbf082fSLeon Romanovsky 	memset(&node_guid, 0, sizeof(node_guid));
12959fbf082fSLeon Romanovsky 	memset(&port_guid, 0, sizeof(port_guid));
1296775f4f05SDan Carpenter 
1297b22b941bSHannes Frederic Sowa 	vf_mac.vf =
1298b22b941bSHannes Frederic Sowa 		vf_vlan.vf =
129979aab093SMoshe Shemesh 		vf_vlan_info.vf =
1300b22b941bSHannes Frederic Sowa 		vf_rate.vf =
1301b22b941bSHannes Frederic Sowa 		vf_tx_rate.vf =
1302b22b941bSHannes Frederic Sowa 		vf_spoofchk.vf =
1303b22b941bSHannes Frederic Sowa 		vf_linkstate.vf =
1304b22b941bSHannes Frederic Sowa 		vf_rss_query_en.vf =
13059aed6ae0SDanit Goldberg 		vf_trust.vf =
13069aed6ae0SDanit Goldberg 		node_guid.vf =
13079aed6ae0SDanit Goldberg 		port_guid.vf = ivi.vf;
1308b22b941bSHannes Frederic Sowa 
1309b22b941bSHannes Frederic Sowa 	memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
131075345f88SDenis Kirjanov 	memcpy(vf_broadcast.broadcast, dev->broadcast, dev->addr_len);
1311b22b941bSHannes Frederic Sowa 	vf_vlan.vlan = ivi.vlan;
1312b22b941bSHannes Frederic Sowa 	vf_vlan.qos = ivi.qos;
131379aab093SMoshe Shemesh 	vf_vlan_info.vlan = ivi.vlan;
131479aab093SMoshe Shemesh 	vf_vlan_info.qos = ivi.qos;
131579aab093SMoshe Shemesh 	vf_vlan_info.vlan_proto = ivi.vlan_proto;
1316b22b941bSHannes Frederic Sowa 	vf_tx_rate.rate = ivi.max_tx_rate;
1317b22b941bSHannes Frederic Sowa 	vf_rate.min_tx_rate = ivi.min_tx_rate;
1318b22b941bSHannes Frederic Sowa 	vf_rate.max_tx_rate = ivi.max_tx_rate;
1319b22b941bSHannes Frederic Sowa 	vf_spoofchk.setting = ivi.spoofchk;
1320b22b941bSHannes Frederic Sowa 	vf_linkstate.link_state = ivi.linkstate;
1321b22b941bSHannes Frederic Sowa 	vf_rss_query_en.setting = ivi.rss_query_en;
1322b22b941bSHannes Frederic Sowa 	vf_trust.setting = ivi.trusted;
1323ae0be8deSMichal Kubecek 	vf = nla_nest_start_noflag(skb, IFLA_VF_INFO);
132479aab093SMoshe Shemesh 	if (!vf)
132579aab093SMoshe Shemesh 		goto nla_put_vfinfo_failure;
1326b22b941bSHannes Frederic Sowa 	if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
132775345f88SDenis Kirjanov 	    nla_put(skb, IFLA_VF_BROADCAST, sizeof(vf_broadcast), &vf_broadcast) ||
1328b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1329b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1330b22b941bSHannes Frederic Sowa 		    &vf_rate) ||
1331b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1332b22b941bSHannes Frederic Sowa 		    &vf_tx_rate) ||
1333b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1334b22b941bSHannes Frederic Sowa 		    &vf_spoofchk) ||
1335b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1336b22b941bSHannes Frederic Sowa 		    &vf_linkstate) ||
1337b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1338b22b941bSHannes Frederic Sowa 		    sizeof(vf_rss_query_en),
1339b22b941bSHannes Frederic Sowa 		    &vf_rss_query_en) ||
1340b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_TRUST,
1341b22b941bSHannes Frederic Sowa 		    sizeof(vf_trust), &vf_trust))
134279aab093SMoshe Shemesh 		goto nla_put_vf_failure;
134330aad417SDanit Goldberg 
134430aad417SDanit Goldberg 	if (dev->netdev_ops->ndo_get_vf_guid &&
134530aad417SDanit Goldberg 	    !dev->netdev_ops->ndo_get_vf_guid(dev, vfs_num, &node_guid,
134630aad417SDanit Goldberg 					      &port_guid)) {
134730aad417SDanit Goldberg 		if (nla_put(skb, IFLA_VF_IB_NODE_GUID, sizeof(node_guid),
134830aad417SDanit Goldberg 			    &node_guid) ||
134930aad417SDanit Goldberg 		    nla_put(skb, IFLA_VF_IB_PORT_GUID, sizeof(port_guid),
135030aad417SDanit Goldberg 			    &port_guid))
135130aad417SDanit Goldberg 			goto nla_put_vf_failure;
135230aad417SDanit Goldberg 	}
1353ae0be8deSMichal Kubecek 	vfvlanlist = nla_nest_start_noflag(skb, IFLA_VF_VLAN_LIST);
135479aab093SMoshe Shemesh 	if (!vfvlanlist)
135579aab093SMoshe Shemesh 		goto nla_put_vf_failure;
135679aab093SMoshe Shemesh 	if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
135779aab093SMoshe Shemesh 		    &vf_vlan_info)) {
135879aab093SMoshe Shemesh 		nla_nest_cancel(skb, vfvlanlist);
135979aab093SMoshe Shemesh 		goto nla_put_vf_failure;
136079aab093SMoshe Shemesh 	}
136179aab093SMoshe Shemesh 	nla_nest_end(skb, vfvlanlist);
1362b22b941bSHannes Frederic Sowa 	memset(&vf_stats, 0, sizeof(vf_stats));
1363b22b941bSHannes Frederic Sowa 	if (dev->netdev_ops->ndo_get_vf_stats)
1364b22b941bSHannes Frederic Sowa 		dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1365b22b941bSHannes Frederic Sowa 						&vf_stats);
1366ae0be8deSMichal Kubecek 	vfstats = nla_nest_start_noflag(skb, IFLA_VF_STATS);
136779aab093SMoshe Shemesh 	if (!vfstats)
136879aab093SMoshe Shemesh 		goto nla_put_vf_failure;
1369343a6d8eSNicolas Dichtel 	if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1370343a6d8eSNicolas Dichtel 			      vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1371343a6d8eSNicolas Dichtel 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1372343a6d8eSNicolas Dichtel 			      vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1373343a6d8eSNicolas Dichtel 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1374343a6d8eSNicolas Dichtel 			      vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1375343a6d8eSNicolas Dichtel 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1376343a6d8eSNicolas Dichtel 			      vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1377343a6d8eSNicolas Dichtel 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1378343a6d8eSNicolas Dichtel 			      vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1379343a6d8eSNicolas Dichtel 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
1380c5a9f6f0SEugenia Emantayev 			      vf_stats.multicast, IFLA_VF_STATS_PAD) ||
1381c5a9f6f0SEugenia Emantayev 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED,
1382c5a9f6f0SEugenia Emantayev 			      vf_stats.rx_dropped, IFLA_VF_STATS_PAD) ||
1383c5a9f6f0SEugenia Emantayev 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED,
1384c5a9f6f0SEugenia Emantayev 			      vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) {
138579aab093SMoshe Shemesh 		nla_nest_cancel(skb, vfstats);
138679aab093SMoshe Shemesh 		goto nla_put_vf_failure;
138779aab093SMoshe Shemesh 	}
1388b22b941bSHannes Frederic Sowa 	nla_nest_end(skb, vfstats);
1389b22b941bSHannes Frederic Sowa 	nla_nest_end(skb, vf);
1390b22b941bSHannes Frederic Sowa 	return 0;
139179aab093SMoshe Shemesh 
139279aab093SMoshe Shemesh nla_put_vf_failure:
139379aab093SMoshe Shemesh 	nla_nest_cancel(skb, vf);
139479aab093SMoshe Shemesh nla_put_vfinfo_failure:
139579aab093SMoshe Shemesh 	nla_nest_cancel(skb, vfinfo);
139679aab093SMoshe Shemesh 	return -EMSGSIZE;
1397b22b941bSHannes Frederic Sowa }
1398b22b941bSHannes Frederic Sowa 
1399250fc3dfSFlorian Westphal static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb,
1400250fc3dfSFlorian Westphal 					   struct net_device *dev,
1401250fc3dfSFlorian Westphal 					   u32 ext_filter_mask)
1402250fc3dfSFlorian Westphal {
1403250fc3dfSFlorian Westphal 	struct nlattr *vfinfo;
1404250fc3dfSFlorian Westphal 	int i, num_vfs;
1405250fc3dfSFlorian Westphal 
1406250fc3dfSFlorian Westphal 	if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0))
1407250fc3dfSFlorian Westphal 		return 0;
1408250fc3dfSFlorian Westphal 
1409250fc3dfSFlorian Westphal 	num_vfs = dev_num_vf(dev->dev.parent);
1410250fc3dfSFlorian Westphal 	if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs))
1411250fc3dfSFlorian Westphal 		return -EMSGSIZE;
1412250fc3dfSFlorian Westphal 
1413250fc3dfSFlorian Westphal 	if (!dev->netdev_ops->ndo_get_vf_config)
1414250fc3dfSFlorian Westphal 		return 0;
1415250fc3dfSFlorian Westphal 
1416ae0be8deSMichal Kubecek 	vfinfo = nla_nest_start_noflag(skb, IFLA_VFINFO_LIST);
1417250fc3dfSFlorian Westphal 	if (!vfinfo)
1418250fc3dfSFlorian Westphal 		return -EMSGSIZE;
1419250fc3dfSFlorian Westphal 
1420250fc3dfSFlorian Westphal 	for (i = 0; i < num_vfs; i++) {
1421250fc3dfSFlorian Westphal 		if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
1422250fc3dfSFlorian Westphal 			return -EMSGSIZE;
1423250fc3dfSFlorian Westphal 	}
1424250fc3dfSFlorian Westphal 
1425250fc3dfSFlorian Westphal 	nla_nest_end(skb, vfinfo);
1426250fc3dfSFlorian Westphal 	return 0;
1427250fc3dfSFlorian Westphal }
1428250fc3dfSFlorian Westphal 
1429b22b941bSHannes Frederic Sowa static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1430b22b941bSHannes Frederic Sowa {
14315f8e4474SKangjie Lu 	struct rtnl_link_ifmap map;
14325f8e4474SKangjie Lu 
14335f8e4474SKangjie Lu 	memset(&map, 0, sizeof(map));
14345f8e4474SKangjie Lu 	map.mem_start   = dev->mem_start;
14355f8e4474SKangjie Lu 	map.mem_end     = dev->mem_end;
14365f8e4474SKangjie Lu 	map.base_addr   = dev->base_addr;
14375f8e4474SKangjie Lu 	map.irq         = dev->irq;
14385f8e4474SKangjie Lu 	map.dma         = dev->dma;
14395f8e4474SKangjie Lu 	map.port        = dev->if_port;
14405f8e4474SKangjie Lu 
1441270cb4d0SNicolas Dichtel 	if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
1442b22b941bSHannes Frederic Sowa 		return -EMSGSIZE;
1443b22b941bSHannes Frederic Sowa 
1444b22b941bSHannes Frederic Sowa 	return 0;
1445b22b941bSHannes Frederic Sowa }
1446b22b941bSHannes Frederic Sowa 
1447a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_skb(struct net_device *dev)
1448d67b9cd2SDaniel Borkmann {
144958038695SMartin KaFai Lau 	const struct bpf_prog *generic_xdp_prog;
1450d67b9cd2SDaniel Borkmann 
1451d67b9cd2SDaniel Borkmann 	ASSERT_RTNL();
1452d67b9cd2SDaniel Borkmann 
145358038695SMartin KaFai Lau 	generic_xdp_prog = rtnl_dereference(dev->xdp_prog);
1454a25717d2SJakub Kicinski 	if (!generic_xdp_prog)
1455a25717d2SJakub Kicinski 		return 0;
1456a25717d2SJakub Kicinski 	return generic_xdp_prog->aux->id;
145758038695SMartin KaFai Lau }
1458ce158e58SJakub Kicinski 
1459a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_drv(struct net_device *dev)
1460a25717d2SJakub Kicinski {
14617f0a8382SAndrii Nakryiko 	return dev_xdp_prog_id(dev, XDP_MODE_DRV);
1462a25717d2SJakub Kicinski }
1463118b4aa2SJakub Kicinski 
1464a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_hw(struct net_device *dev)
1465a25717d2SJakub Kicinski {
14667f0a8382SAndrii Nakryiko 	return dev_xdp_prog_id(dev, XDP_MODE_HW);
1467a25717d2SJakub Kicinski }
1468a25717d2SJakub Kicinski 
1469a25717d2SJakub Kicinski static int rtnl_xdp_report_one(struct sk_buff *skb, struct net_device *dev,
1470a25717d2SJakub Kicinski 			       u32 *prog_id, u8 *mode, u8 tgt_mode, u32 attr,
1471a25717d2SJakub Kicinski 			       u32 (*get_prog_id)(struct net_device *dev))
1472a25717d2SJakub Kicinski {
1473a25717d2SJakub Kicinski 	u32 curr_id;
1474a25717d2SJakub Kicinski 	int err;
1475a25717d2SJakub Kicinski 
1476a25717d2SJakub Kicinski 	curr_id = get_prog_id(dev);
1477a25717d2SJakub Kicinski 	if (!curr_id)
1478a25717d2SJakub Kicinski 		return 0;
1479a25717d2SJakub Kicinski 
1480a25717d2SJakub Kicinski 	*prog_id = curr_id;
1481a25717d2SJakub Kicinski 	err = nla_put_u32(skb, attr, curr_id);
1482a25717d2SJakub Kicinski 	if (err)
1483a25717d2SJakub Kicinski 		return err;
1484a25717d2SJakub Kicinski 
1485a25717d2SJakub Kicinski 	if (*mode != XDP_ATTACHED_NONE)
1486a25717d2SJakub Kicinski 		*mode = XDP_ATTACHED_MULTI;
1487a25717d2SJakub Kicinski 	else
1488a25717d2SJakub Kicinski 		*mode = tgt_mode;
1489a25717d2SJakub Kicinski 
1490a25717d2SJakub Kicinski 	return 0;
1491d67b9cd2SDaniel Borkmann }
1492d67b9cd2SDaniel Borkmann 
1493d1fdd913SBrenden Blanco static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1494d1fdd913SBrenden Blanco {
1495d1fdd913SBrenden Blanco 	struct nlattr *xdp;
149658038695SMartin KaFai Lau 	u32 prog_id;
1497d1fdd913SBrenden Blanco 	int err;
14984f91da26SJakub Kicinski 	u8 mode;
1499d1fdd913SBrenden Blanco 
1500ae0be8deSMichal Kubecek 	xdp = nla_nest_start_noflag(skb, IFLA_XDP);
1501d1fdd913SBrenden Blanco 	if (!xdp)
1502d1fdd913SBrenden Blanco 		return -EMSGSIZE;
1503b5cdae32SDavid S. Miller 
1504a25717d2SJakub Kicinski 	prog_id = 0;
1505a25717d2SJakub Kicinski 	mode = XDP_ATTACHED_NONE;
1506202aabe8SJakub Kicinski 	err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_SKB,
1507202aabe8SJakub Kicinski 				  IFLA_XDP_SKB_PROG_ID, rtnl_xdp_prog_skb);
1508202aabe8SJakub Kicinski 	if (err)
1509a25717d2SJakub Kicinski 		goto err_cancel;
1510202aabe8SJakub Kicinski 	err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_DRV,
1511202aabe8SJakub Kicinski 				  IFLA_XDP_DRV_PROG_ID, rtnl_xdp_prog_drv);
1512202aabe8SJakub Kicinski 	if (err)
1513a25717d2SJakub Kicinski 		goto err_cancel;
1514202aabe8SJakub Kicinski 	err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_HW,
1515202aabe8SJakub Kicinski 				  IFLA_XDP_HW_PROG_ID, rtnl_xdp_prog_hw);
1516202aabe8SJakub Kicinski 	if (err)
1517a25717d2SJakub Kicinski 		goto err_cancel;
1518a25717d2SJakub Kicinski 
15194f91da26SJakub Kicinski 	err = nla_put_u8(skb, IFLA_XDP_ATTACHED, mode);
1520d1fdd913SBrenden Blanco 	if (err)
1521d1fdd913SBrenden Blanco 		goto err_cancel;
1522d1fdd913SBrenden Blanco 
1523a25717d2SJakub Kicinski 	if (prog_id && mode != XDP_ATTACHED_MULTI) {
152458038695SMartin KaFai Lau 		err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id);
152558038695SMartin KaFai Lau 		if (err)
152658038695SMartin KaFai Lau 			goto err_cancel;
152758038695SMartin KaFai Lau 	}
152858038695SMartin KaFai Lau 
1529d1fdd913SBrenden Blanco 	nla_nest_end(skb, xdp);
1530d1fdd913SBrenden Blanco 	return 0;
1531d1fdd913SBrenden Blanco 
1532d1fdd913SBrenden Blanco err_cancel:
1533d1fdd913SBrenden Blanco 	nla_nest_cancel(skb, xdp);
1534d1fdd913SBrenden Blanco 	return err;
1535d1fdd913SBrenden Blanco }
1536d1fdd913SBrenden Blanco 
15373d3ea5afSVlad Yasevich static u32 rtnl_get_event(unsigned long event)
15383d3ea5afSVlad Yasevich {
15393d3ea5afSVlad Yasevich 	u32 rtnl_event_type = IFLA_EVENT_NONE;
15403d3ea5afSVlad Yasevich 
15413d3ea5afSVlad Yasevich 	switch (event) {
15423d3ea5afSVlad Yasevich 	case NETDEV_REBOOT:
15433d3ea5afSVlad Yasevich 		rtnl_event_type = IFLA_EVENT_REBOOT;
15443d3ea5afSVlad Yasevich 		break;
15453d3ea5afSVlad Yasevich 	case NETDEV_FEAT_CHANGE:
15463d3ea5afSVlad Yasevich 		rtnl_event_type = IFLA_EVENT_FEATURES;
15473d3ea5afSVlad Yasevich 		break;
15483d3ea5afSVlad Yasevich 	case NETDEV_BONDING_FAILOVER:
15493d3ea5afSVlad Yasevich 		rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER;
15503d3ea5afSVlad Yasevich 		break;
15513d3ea5afSVlad Yasevich 	case NETDEV_NOTIFY_PEERS:
15523d3ea5afSVlad Yasevich 		rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS;
15533d3ea5afSVlad Yasevich 		break;
15543d3ea5afSVlad Yasevich 	case NETDEV_RESEND_IGMP:
15553d3ea5afSVlad Yasevich 		rtnl_event_type = IFLA_EVENT_IGMP_RESEND;
15563d3ea5afSVlad Yasevich 		break;
15573d3ea5afSVlad Yasevich 	case NETDEV_CHANGEINFODATA:
15583d3ea5afSVlad Yasevich 		rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS;
15593d3ea5afSVlad Yasevich 		break;
15603d3ea5afSVlad Yasevich 	default:
15613d3ea5afSVlad Yasevich 		break;
15623d3ea5afSVlad Yasevich 	}
15633d3ea5afSVlad Yasevich 
15643d3ea5afSVlad Yasevich 	return rtnl_event_type;
15653d3ea5afSVlad Yasevich }
15663d3ea5afSVlad Yasevich 
156779110a04SFlorian Westphal static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev)
156879110a04SFlorian Westphal {
156979110a04SFlorian Westphal 	const struct net_device *upper_dev;
157079110a04SFlorian Westphal 	int ret = 0;
157179110a04SFlorian Westphal 
157279110a04SFlorian Westphal 	rcu_read_lock();
157379110a04SFlorian Westphal 
157479110a04SFlorian Westphal 	upper_dev = netdev_master_upper_dev_get_rcu(dev);
157579110a04SFlorian Westphal 	if (upper_dev)
157679110a04SFlorian Westphal 		ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex);
157779110a04SFlorian Westphal 
157879110a04SFlorian Westphal 	rcu_read_unlock();
157979110a04SFlorian Westphal 	return ret;
158079110a04SFlorian Westphal }
158179110a04SFlorian Westphal 
1582feadc4b6SSabrina Dubroca static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev,
1583feadc4b6SSabrina Dubroca 			  bool force)
158479110a04SFlorian Westphal {
158579110a04SFlorian Westphal 	int ifindex = dev_get_iflink(dev);
158679110a04SFlorian Westphal 
1587feadc4b6SSabrina Dubroca 	if (force || dev->ifindex != ifindex)
158879110a04SFlorian Westphal 		return nla_put_u32(skb, IFLA_LINK, ifindex);
1589feadc4b6SSabrina Dubroca 
1590feadc4b6SSabrina Dubroca 	return 0;
159179110a04SFlorian Westphal }
159279110a04SFlorian Westphal 
15936c557001SFlorian Westphal static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb,
15946c557001SFlorian Westphal 					      struct net_device *dev)
15956c557001SFlorian Westphal {
15966c557001SFlorian Westphal 	char buf[IFALIASZ];
15976c557001SFlorian Westphal 	int ret;
15986c557001SFlorian Westphal 
15996c557001SFlorian Westphal 	ret = dev_get_alias(dev, buf, sizeof(buf));
16006c557001SFlorian Westphal 	return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0;
16016c557001SFlorian Westphal }
16026c557001SFlorian Westphal 
1603b1e66b9aSFlorian Westphal static int rtnl_fill_link_netnsid(struct sk_buff *skb,
160479e1ad14SJiri Benc 				  const struct net_device *dev,
1605d4e4fdf9SGuillaume Nault 				  struct net *src_net, gfp_t gfp)
1606b1e66b9aSFlorian Westphal {
1607feadc4b6SSabrina Dubroca 	bool put_iflink = false;
1608feadc4b6SSabrina Dubroca 
1609b1e66b9aSFlorian Westphal 	if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) {
1610b1e66b9aSFlorian Westphal 		struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1611b1e66b9aSFlorian Westphal 
1612b1e66b9aSFlorian Westphal 		if (!net_eq(dev_net(dev), link_net)) {
1613d4e4fdf9SGuillaume Nault 			int id = peernet2id_alloc(src_net, link_net, gfp);
1614b1e66b9aSFlorian Westphal 
1615b1e66b9aSFlorian Westphal 			if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1616b1e66b9aSFlorian Westphal 				return -EMSGSIZE;
1617feadc4b6SSabrina Dubroca 
1618feadc4b6SSabrina Dubroca 			put_iflink = true;
1619b1e66b9aSFlorian Westphal 		}
1620b1e66b9aSFlorian Westphal 	}
1621b1e66b9aSFlorian Westphal 
1622feadc4b6SSabrina Dubroca 	return nla_put_iflink(skb, dev, put_iflink);
1623b1e66b9aSFlorian Westphal }
1624b1e66b9aSFlorian Westphal 
1625070cbf5bSFlorian Westphal static int rtnl_fill_link_af(struct sk_buff *skb,
1626070cbf5bSFlorian Westphal 			     const struct net_device *dev,
1627070cbf5bSFlorian Westphal 			     u32 ext_filter_mask)
1628070cbf5bSFlorian Westphal {
1629070cbf5bSFlorian Westphal 	const struct rtnl_af_ops *af_ops;
1630070cbf5bSFlorian Westphal 	struct nlattr *af_spec;
1631070cbf5bSFlorian Westphal 
1632ae0be8deSMichal Kubecek 	af_spec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
1633070cbf5bSFlorian Westphal 	if (!af_spec)
1634070cbf5bSFlorian Westphal 		return -EMSGSIZE;
1635070cbf5bSFlorian Westphal 
16365fa85a09SFlorian Westphal 	list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
1637070cbf5bSFlorian Westphal 		struct nlattr *af;
1638070cbf5bSFlorian Westphal 		int err;
1639070cbf5bSFlorian Westphal 
1640070cbf5bSFlorian Westphal 		if (!af_ops->fill_link_af)
1641070cbf5bSFlorian Westphal 			continue;
1642070cbf5bSFlorian Westphal 
1643ae0be8deSMichal Kubecek 		af = nla_nest_start_noflag(skb, af_ops->family);
1644070cbf5bSFlorian Westphal 		if (!af)
1645070cbf5bSFlorian Westphal 			return -EMSGSIZE;
1646070cbf5bSFlorian Westphal 
1647070cbf5bSFlorian Westphal 		err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
1648070cbf5bSFlorian Westphal 		/*
1649070cbf5bSFlorian Westphal 		 * Caller may return ENODATA to indicate that there
1650070cbf5bSFlorian Westphal 		 * was no data to be dumped. This is not an error, it
1651070cbf5bSFlorian Westphal 		 * means we should trim the attribute header and
1652070cbf5bSFlorian Westphal 		 * continue.
1653070cbf5bSFlorian Westphal 		 */
1654070cbf5bSFlorian Westphal 		if (err == -ENODATA)
1655070cbf5bSFlorian Westphal 			nla_nest_cancel(skb, af);
1656070cbf5bSFlorian Westphal 		else if (err < 0)
1657070cbf5bSFlorian Westphal 			return -EMSGSIZE;
1658070cbf5bSFlorian Westphal 
1659070cbf5bSFlorian Westphal 		nla_nest_end(skb, af);
1660070cbf5bSFlorian Westphal 	}
1661070cbf5bSFlorian Westphal 
1662070cbf5bSFlorian Westphal 	nla_nest_end(skb, af_spec);
1663070cbf5bSFlorian Westphal 	return 0;
1664070cbf5bSFlorian Westphal }
1665070cbf5bSFlorian Westphal 
166688f4fb0cSJiri Pirko static int rtnl_fill_alt_ifnames(struct sk_buff *skb,
166788f4fb0cSJiri Pirko 				 const struct net_device *dev)
166888f4fb0cSJiri Pirko {
166988f4fb0cSJiri Pirko 	struct netdev_name_node *name_node;
167088f4fb0cSJiri Pirko 	int count = 0;
167188f4fb0cSJiri Pirko 
167288f4fb0cSJiri Pirko 	list_for_each_entry(name_node, &dev->name_node->list, list) {
167388f4fb0cSJiri Pirko 		if (nla_put_string(skb, IFLA_ALT_IFNAME, name_node->name))
167488f4fb0cSJiri Pirko 			return -EMSGSIZE;
167588f4fb0cSJiri Pirko 		count++;
167688f4fb0cSJiri Pirko 	}
167788f4fb0cSJiri Pirko 	return count;
167888f4fb0cSJiri Pirko }
167988f4fb0cSJiri Pirko 
168088f4fb0cSJiri Pirko static int rtnl_fill_prop_list(struct sk_buff *skb,
168188f4fb0cSJiri Pirko 			       const struct net_device *dev)
168288f4fb0cSJiri Pirko {
168388f4fb0cSJiri Pirko 	struct nlattr *prop_list;
168488f4fb0cSJiri Pirko 	int ret;
168588f4fb0cSJiri Pirko 
168688f4fb0cSJiri Pirko 	prop_list = nla_nest_start(skb, IFLA_PROP_LIST);
168788f4fb0cSJiri Pirko 	if (!prop_list)
168888f4fb0cSJiri Pirko 		return -EMSGSIZE;
168988f4fb0cSJiri Pirko 
169088f4fb0cSJiri Pirko 	ret = rtnl_fill_alt_ifnames(skb, dev);
169188f4fb0cSJiri Pirko 	if (ret <= 0)
169288f4fb0cSJiri Pirko 		goto nest_cancel;
169388f4fb0cSJiri Pirko 
169488f4fb0cSJiri Pirko 	nla_nest_end(skb, prop_list);
169588f4fb0cSJiri Pirko 	return 0;
169688f4fb0cSJiri Pirko 
169788f4fb0cSJiri Pirko nest_cancel:
169888f4fb0cSJiri Pirko 	nla_nest_cancel(skb, prop_list);
169988f4fb0cSJiri Pirko 	return ret;
170088f4fb0cSJiri Pirko }
170188f4fb0cSJiri Pirko 
1702829eb208SRoopa Prabhu static int rtnl_fill_proto_down(struct sk_buff *skb,
1703829eb208SRoopa Prabhu 				const struct net_device *dev)
1704829eb208SRoopa Prabhu {
1705829eb208SRoopa Prabhu 	struct nlattr *pr;
1706829eb208SRoopa Prabhu 	u32 preason;
1707829eb208SRoopa Prabhu 
1708829eb208SRoopa Prabhu 	if (nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down))
1709829eb208SRoopa Prabhu 		goto nla_put_failure;
1710829eb208SRoopa Prabhu 
1711829eb208SRoopa Prabhu 	preason = dev->proto_down_reason;
1712829eb208SRoopa Prabhu 	if (!preason)
1713829eb208SRoopa Prabhu 		return 0;
1714829eb208SRoopa Prabhu 
1715829eb208SRoopa Prabhu 	pr = nla_nest_start(skb, IFLA_PROTO_DOWN_REASON);
1716829eb208SRoopa Prabhu 	if (!pr)
1717829eb208SRoopa Prabhu 		return -EMSGSIZE;
1718829eb208SRoopa Prabhu 
1719829eb208SRoopa Prabhu 	if (nla_put_u32(skb, IFLA_PROTO_DOWN_REASON_VALUE, preason)) {
1720829eb208SRoopa Prabhu 		nla_nest_cancel(skb, pr);
1721829eb208SRoopa Prabhu 		goto nla_put_failure;
1722829eb208SRoopa Prabhu 	}
1723829eb208SRoopa Prabhu 
1724829eb208SRoopa Prabhu 	nla_nest_end(skb, pr);
1725829eb208SRoopa Prabhu 	return 0;
1726829eb208SRoopa Prabhu 
1727829eb208SRoopa Prabhu nla_put_failure:
1728829eb208SRoopa Prabhu 	return -EMSGSIZE;
1729829eb208SRoopa Prabhu }
1730829eb208SRoopa Prabhu 
173179e1ad14SJiri Benc static int rtnl_fill_ifinfo(struct sk_buff *skb,
173279e1ad14SJiri Benc 			    struct net_device *dev, struct net *src_net,
1733575c3e2aSPatrick McHardy 			    int type, u32 pid, u32 seq, u32 change,
17343d3ea5afSVlad Yasevich 			    unsigned int flags, u32 ext_filter_mask,
173538e01b30SNicolas Dichtel 			    u32 event, int *new_nsid, int new_ifindex,
1736d4e4fdf9SGuillaume Nault 			    int tgt_netnsid, gfp_t gfp)
1737b60c5115SThomas Graf {
1738b60c5115SThomas Graf 	struct ifinfomsg *ifm;
17391da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
17405891cd5eSEric Dumazet 	struct Qdisc *qdisc;
17411da177e4SLinus Torvalds 
17422907c35fSEric Dumazet 	ASSERT_RTNL();
1743b60c5115SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1744b60c5115SThomas Graf 	if (nlh == NULL)
174526932566SPatrick McHardy 		return -EMSGSIZE;
17461da177e4SLinus Torvalds 
1747b60c5115SThomas Graf 	ifm = nlmsg_data(nlh);
1748b60c5115SThomas Graf 	ifm->ifi_family = AF_UNSPEC;
1749b60c5115SThomas Graf 	ifm->__ifi_pad = 0;
1750b60c5115SThomas Graf 	ifm->ifi_type = dev->type;
1751b60c5115SThomas Graf 	ifm->ifi_index = dev->ifindex;
1752b60c5115SThomas Graf 	ifm->ifi_flags = dev_get_flags(dev);
1753b60c5115SThomas Graf 	ifm->ifi_change = change;
17541da177e4SLinus Torvalds 
17557e4a8d5aSChristian Brauner 	if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_TARGET_NETNSID, tgt_netnsid))
175679e1ad14SJiri Benc 		goto nla_put_failure;
175779e1ad14SJiri Benc 
17585891cd5eSEric Dumazet 	qdisc = rtnl_dereference(dev->qdisc);
1759a6574349SDavid S. Miller 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1760a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1761a6574349SDavid S. Miller 	    nla_put_u8(skb, IFLA_OPERSTATE,
1762a6574349SDavid S. Miller 		       netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1763a6574349SDavid S. Miller 	    nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1764a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
17653e7a50ceSStephen Hemminger 	    nla_put_u32(skb, IFLA_MIN_MTU, dev->min_mtu) ||
17663e7a50ceSStephen Hemminger 	    nla_put_u32(skb, IFLA_MAX_MTU, dev->max_mtu) ||
1767a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_GROUP, dev->group) ||
1768edbc0bb3SBen Greear 	    nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
17697e6e1b57SNicolas Dichtel 	    nla_put_u32(skb, IFLA_ALLMULTI, dev->allmulti) ||
177076ff5cc9SJiri Pirko 	    nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
1771c70ce028SEric Dumazet 	    nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1772c70ce028SEric Dumazet 	    nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
1773eac1b93cSCoco Li 	    nla_put_u32(skb, IFLA_GRO_MAX_SIZE, dev->gro_max_size) ||
177489527be8SEric Dumazet 	    nla_put_u32(skb, IFLA_TSO_MAX_SIZE, dev->tso_max_size) ||
177589527be8SEric Dumazet 	    nla_put_u32(skb, IFLA_TSO_MAX_SEGS, dev->tso_max_segs) ||
17761d69c2b3SMark A. Greer #ifdef CONFIG_RPS
177776ff5cc9SJiri Pirko 	    nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
17781d69c2b3SMark A. Greer #endif
177979110a04SFlorian Westphal 	    put_master_ifindex(skb, dev) ||
17809a57247fSJiri Pirko 	    nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
17815891cd5eSEric Dumazet 	    (qdisc &&
17825891cd5eSEric Dumazet 	     nla_put_string(skb, IFLA_QDISC, qdisc->ops->id)) ||
17836c557001SFlorian Westphal 	    nla_put_ifalias(skb, dev) ||
17842d3b479dSdavid decotigny 	    nla_put_u32(skb, IFLA_CARRIER_CHANGES,
1785b2d3bcfaSDavid Decotigny 			atomic_read(&dev->carrier_up_count) +
1786b2d3bcfaSDavid Decotigny 			atomic_read(&dev->carrier_down_count)) ||
1787b2d3bcfaSDavid Decotigny 	    nla_put_u32(skb, IFLA_CARRIER_UP_COUNT,
1788b2d3bcfaSDavid Decotigny 			atomic_read(&dev->carrier_up_count)) ||
1789b2d3bcfaSDavid Decotigny 	    nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT,
1790b2d3bcfaSDavid Decotigny 			atomic_read(&dev->carrier_down_count)))
1791a6574349SDavid S. Miller 		goto nla_put_failure;
17920b815a1aSStephen Hemminger 
1793829eb208SRoopa Prabhu 	if (rtnl_fill_proto_down(skb, dev))
1794829eb208SRoopa Prabhu 		goto nla_put_failure;
1795829eb208SRoopa Prabhu 
17963d3ea5afSVlad Yasevich 	if (event != IFLA_EVENT_NONE) {
17973d3ea5afSVlad Yasevich 		if (nla_put_u32(skb, IFLA_EVENT, event))
17983d3ea5afSVlad Yasevich 			goto nla_put_failure;
17993d3ea5afSVlad Yasevich 	}
18003d3ea5afSVlad Yasevich 
1801b22b941bSHannes Frederic Sowa 	if (rtnl_fill_link_ifmap(skb, dev))
1802a6574349SDavid S. Miller 		goto nla_put_failure;
18031da177e4SLinus Torvalds 
18041da177e4SLinus Torvalds 	if (dev->addr_len) {
1805a6574349SDavid S. Miller 		if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1806a6574349SDavid S. Miller 		    nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1807a6574349SDavid S. Miller 			goto nla_put_failure;
18081da177e4SLinus Torvalds 	}
18091da177e4SLinus Torvalds 
181066cae9edSJiri Pirko 	if (rtnl_phys_port_id_fill(skb, dev))
181166cae9edSJiri Pirko 		goto nla_put_failure;
181266cae9edSJiri Pirko 
1813db24a904SDavid Ahern 	if (rtnl_phys_port_name_fill(skb, dev))
1814db24a904SDavid Ahern 		goto nla_put_failure;
1815db24a904SDavid Ahern 
181682f28412SJiri Pirko 	if (rtnl_phys_switch_id_fill(skb, dev))
181782f28412SJiri Pirko 		goto nla_put_failure;
181882f28412SJiri Pirko 
1819b22b941bSHannes Frederic Sowa 	if (rtnl_fill_stats(skb, dev))
1820b60c5115SThomas Graf 		goto nla_put_failure;
1821b60c5115SThomas Graf 
1822250fc3dfSFlorian Westphal 	if (rtnl_fill_vf(skb, dev, ext_filter_mask))
1823a6574349SDavid S. Miller 		goto nla_put_failure;
182457b61080SScott Feldman 
1825c53864fdSDavid Gibson 	if (rtnl_port_fill(skb, dev, ext_filter_mask))
182657b61080SScott Feldman 		goto nla_put_failure;
182757b61080SScott Feldman 
1828d1fdd913SBrenden Blanco 	if (rtnl_xdp_fill(skb, dev))
1829d1fdd913SBrenden Blanco 		goto nla_put_failure;
1830d1fdd913SBrenden Blanco 
1831ba7d49b1SJiri Pirko 	if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
183238f7b870SPatrick McHardy 		if (rtnl_link_fill(skb, dev) < 0)
183338f7b870SPatrick McHardy 			goto nla_put_failure;
183438f7b870SPatrick McHardy 	}
183538f7b870SPatrick McHardy 
1836d4e4fdf9SGuillaume Nault 	if (rtnl_fill_link_netnsid(skb, dev, src_net, gfp))
1837d37512a2SNicolas Dichtel 		goto nla_put_failure;
1838d37512a2SNicolas Dichtel 
18396621dd29SNicolas Dichtel 	if (new_nsid &&
18406621dd29SNicolas Dichtel 	    nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0)
18416621dd29SNicolas Dichtel 		goto nla_put_failure;
184238e01b30SNicolas Dichtel 	if (new_ifindex &&
184338e01b30SNicolas Dichtel 	    nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0)
184438e01b30SNicolas Dichtel 		goto nla_put_failure;
184538e01b30SNicolas Dichtel 
1846f74877a5SMichal Kubecek 	if (memchr_inv(dev->perm_addr, '\0', dev->addr_len) &&
1847f74877a5SMichal Kubecek 	    nla_put(skb, IFLA_PERM_ADDRESS, dev->addr_len, dev->perm_addr))
1848f74877a5SMichal Kubecek 		goto nla_put_failure;
18496621dd29SNicolas Dichtel 
18505fa85a09SFlorian Westphal 	rcu_read_lock();
1851070cbf5bSFlorian Westphal 	if (rtnl_fill_link_af(skb, dev, ext_filter_mask))
18525fa85a09SFlorian Westphal 		goto nla_put_failure_rcu;
18535fa85a09SFlorian Westphal 	rcu_read_unlock();
1854f8ff182cSThomas Graf 
185588f4fb0cSJiri Pirko 	if (rtnl_fill_prop_list(skb, dev))
185688f4fb0cSJiri Pirko 		goto nla_put_failure;
185788f4fb0cSJiri Pirko 
185800e77ed8SJohannes Berg 	if (dev->dev.parent &&
185900e77ed8SJohannes Berg 	    nla_put_string(skb, IFLA_PARENT_DEV_NAME,
186000e77ed8SJohannes Berg 			   dev_name(dev->dev.parent)))
186100e77ed8SJohannes Berg 		goto nla_put_failure;
186200e77ed8SJohannes Berg 
186300e77ed8SJohannes Berg 	if (dev->dev.parent && dev->dev.parent->bus &&
186400e77ed8SJohannes Berg 	    nla_put_string(skb, IFLA_PARENT_DEV_BUS_NAME,
186500e77ed8SJohannes Berg 			   dev->dev.parent->bus->name))
186600e77ed8SJohannes Berg 		goto nla_put_failure;
186700e77ed8SJohannes Berg 
1868053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
1869053c095aSJohannes Berg 	return 0;
1870b60c5115SThomas Graf 
18715fa85a09SFlorian Westphal nla_put_failure_rcu:
18725fa85a09SFlorian Westphal 	rcu_read_unlock();
1873b60c5115SThomas Graf nla_put_failure:
187426932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
187526932566SPatrick McHardy 	return -EMSGSIZE;
18761da177e4SLinus Torvalds }
18771da177e4SLinus Torvalds 
1878f7b12606SJiri Pirko static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1879f7b12606SJiri Pirko 	[IFLA_IFNAME]		= { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1880f7b12606SJiri Pirko 	[IFLA_ADDRESS]		= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1881f7b12606SJiri Pirko 	[IFLA_BROADCAST]	= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1882f7b12606SJiri Pirko 	[IFLA_MAP]		= { .len = sizeof(struct rtnl_link_ifmap) },
1883f7b12606SJiri Pirko 	[IFLA_MTU]		= { .type = NLA_U32 },
1884f7b12606SJiri Pirko 	[IFLA_LINK]		= { .type = NLA_U32 },
1885f7b12606SJiri Pirko 	[IFLA_MASTER]		= { .type = NLA_U32 },
1886f7b12606SJiri Pirko 	[IFLA_CARRIER]		= { .type = NLA_U8 },
1887f7b12606SJiri Pirko 	[IFLA_TXQLEN]		= { .type = NLA_U32 },
1888f7b12606SJiri Pirko 	[IFLA_WEIGHT]		= { .type = NLA_U32 },
1889f7b12606SJiri Pirko 	[IFLA_OPERSTATE]	= { .type = NLA_U8 },
1890f7b12606SJiri Pirko 	[IFLA_LINKMODE]		= { .type = NLA_U8 },
1891f7b12606SJiri Pirko 	[IFLA_LINKINFO]		= { .type = NLA_NESTED },
1892f7b12606SJiri Pirko 	[IFLA_NET_NS_PID]	= { .type = NLA_U32 },
1893f7b12606SJiri Pirko 	[IFLA_NET_NS_FD]	= { .type = NLA_U32 },
18942459b4c6SNicolas Dichtel 	/* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
18952459b4c6SNicolas Dichtel 	 * allow 0-length string (needed to remove an alias).
18962459b4c6SNicolas Dichtel 	 */
18972459b4c6SNicolas Dichtel 	[IFLA_IFALIAS]	        = { .type = NLA_BINARY, .len = IFALIASZ - 1 },
1898f7b12606SJiri Pirko 	[IFLA_VFINFO_LIST]	= {. type = NLA_NESTED },
1899f7b12606SJiri Pirko 	[IFLA_VF_PORTS]		= { .type = NLA_NESTED },
1900f7b12606SJiri Pirko 	[IFLA_PORT_SELF]	= { .type = NLA_NESTED },
1901f7b12606SJiri Pirko 	[IFLA_AF_SPEC]		= { .type = NLA_NESTED },
1902f7b12606SJiri Pirko 	[IFLA_EXT_MASK]		= { .type = NLA_U32 },
1903f7b12606SJiri Pirko 	[IFLA_PROMISCUITY]	= { .type = NLA_U32 },
1904f7b12606SJiri Pirko 	[IFLA_NUM_TX_QUEUES]	= { .type = NLA_U32 },
1905f7b12606SJiri Pirko 	[IFLA_NUM_RX_QUEUES]	= { .type = NLA_U32 },
190646e6b992SStephen Hemminger 	[IFLA_GSO_MAX_SEGS]	= { .type = NLA_U32 },
190746e6b992SStephen Hemminger 	[IFLA_GSO_MAX_SIZE]	= { .type = NLA_U32 },
190802637fceSJiri Pirko 	[IFLA_PHYS_PORT_ID]	= { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
19092d3b479dSdavid decotigny 	[IFLA_CARRIER_CHANGES]	= { .type = NLA_U32 },  /* ignored */
191082f28412SJiri Pirko 	[IFLA_PHYS_SWITCH_ID]	= { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1911317f4810SNicolas Dichtel 	[IFLA_LINK_NETNSID]	= { .type = NLA_S32 },
191288d6378bSAnuradha Karuppiah 	[IFLA_PROTO_DOWN]	= { .type = NLA_U8 },
1913d1fdd913SBrenden Blanco 	[IFLA_XDP]		= { .type = NLA_NESTED },
19143d3ea5afSVlad Yasevich 	[IFLA_EVENT]		= { .type = NLA_U32 },
1915db833d40SSerhey Popovych 	[IFLA_GROUP]		= { .type = NLA_U32 },
19167e4a8d5aSChristian Brauner 	[IFLA_TARGET_NETNSID]	= { .type = NLA_S32 },
1917b2d3bcfaSDavid Decotigny 	[IFLA_CARRIER_UP_COUNT]	= { .type = NLA_U32 },
1918b2d3bcfaSDavid Decotigny 	[IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 },
19193e7a50ceSStephen Hemminger 	[IFLA_MIN_MTU]		= { .type = NLA_U32 },
19203e7a50ceSStephen Hemminger 	[IFLA_MAX_MTU]		= { .type = NLA_U32 },
192136fbf1e5SJiri Pirko 	[IFLA_PROP_LIST]	= { .type = NLA_NESTED },
192236fbf1e5SJiri Pirko 	[IFLA_ALT_IFNAME]	= { .type = NLA_STRING,
192336fbf1e5SJiri Pirko 				    .len = ALTIFNAMSIZ - 1 },
1924f74877a5SMichal Kubecek 	[IFLA_PERM_ADDRESS]	= { .type = NLA_REJECT },
1925829eb208SRoopa Prabhu 	[IFLA_PROTO_DOWN_REASON] = { .type = NLA_NESTED },
19267e4a5131SAndrei Vagin 	[IFLA_NEW_IFINDEX]	= NLA_POLICY_MIN(NLA_S32, 1),
192788b71053SJohannes Berg 	[IFLA_PARENT_DEV_NAME]	= { .type = NLA_NUL_STRING },
1928eac1b93cSCoco Li 	[IFLA_GRO_MAX_SIZE]	= { .type = NLA_U32 },
192989527be8SEric Dumazet 	[IFLA_TSO_MAX_SIZE]	= { .type = NLA_REJECT },
193089527be8SEric Dumazet 	[IFLA_TSO_MAX_SEGS]	= { .type = NLA_REJECT },
19317e6e1b57SNicolas Dichtel 	[IFLA_ALLMULTI]		= { .type = NLA_REJECT },
1932f7b12606SJiri Pirko };
1933f7b12606SJiri Pirko 
1934f7b12606SJiri Pirko static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1935f7b12606SJiri Pirko 	[IFLA_INFO_KIND]	= { .type = NLA_STRING },
1936f7b12606SJiri Pirko 	[IFLA_INFO_DATA]	= { .type = NLA_NESTED },
1937f7b12606SJiri Pirko 	[IFLA_INFO_SLAVE_KIND]	= { .type = NLA_STRING },
1938f7b12606SJiri Pirko 	[IFLA_INFO_SLAVE_DATA]	= { .type = NLA_NESTED },
1939f7b12606SJiri Pirko };
1940f7b12606SJiri Pirko 
1941f7b12606SJiri Pirko static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1942364d5716SDaniel Borkmann 	[IFLA_VF_MAC]		= { .len = sizeof(struct ifla_vf_mac) },
194375345f88SDenis Kirjanov 	[IFLA_VF_BROADCAST]	= { .type = NLA_REJECT },
1944364d5716SDaniel Borkmann 	[IFLA_VF_VLAN]		= { .len = sizeof(struct ifla_vf_vlan) },
194579aab093SMoshe Shemesh 	[IFLA_VF_VLAN_LIST]     = { .type = NLA_NESTED },
1946364d5716SDaniel Borkmann 	[IFLA_VF_TX_RATE]	= { .len = sizeof(struct ifla_vf_tx_rate) },
1947364d5716SDaniel Borkmann 	[IFLA_VF_SPOOFCHK]	= { .len = sizeof(struct ifla_vf_spoofchk) },
1948364d5716SDaniel Borkmann 	[IFLA_VF_RATE]		= { .len = sizeof(struct ifla_vf_rate) },
1949364d5716SDaniel Borkmann 	[IFLA_VF_LINK_STATE]	= { .len = sizeof(struct ifla_vf_link_state) },
195001a3d796SVlad Zolotarov 	[IFLA_VF_RSS_QUERY_EN]	= { .len = sizeof(struct ifla_vf_rss_query_en) },
19513b766cd8SEran Ben Elisha 	[IFLA_VF_STATS]		= { .type = NLA_NESTED },
1952dd461d6aSHiroshi Shimamoto 	[IFLA_VF_TRUST]		= { .len = sizeof(struct ifla_vf_trust) },
1953cc8e27ccSEli Cohen 	[IFLA_VF_IB_NODE_GUID]	= { .len = sizeof(struct ifla_vf_guid) },
1954cc8e27ccSEli Cohen 	[IFLA_VF_IB_PORT_GUID]	= { .len = sizeof(struct ifla_vf_guid) },
19553b766cd8SEran Ben Elisha };
19563b766cd8SEran Ben Elisha 
1957f7b12606SJiri Pirko static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1958f7b12606SJiri Pirko 	[IFLA_PORT_VF]		= { .type = NLA_U32 },
1959f7b12606SJiri Pirko 	[IFLA_PORT_PROFILE]	= { .type = NLA_STRING,
1960f7b12606SJiri Pirko 				    .len = PORT_PROFILE_MAX },
1961f7b12606SJiri Pirko 	[IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1962f7b12606SJiri Pirko 				      .len = PORT_UUID_MAX },
1963f7b12606SJiri Pirko 	[IFLA_PORT_HOST_UUID]	= { .type = NLA_STRING,
1964f7b12606SJiri Pirko 				    .len = PORT_UUID_MAX },
1965f7b12606SJiri Pirko 	[IFLA_PORT_REQUEST]	= { .type = NLA_U8, },
1966f7b12606SJiri Pirko 	[IFLA_PORT_RESPONSE]	= { .type = NLA_U16, },
1967025331dfSDaniel Borkmann 
1968025331dfSDaniel Borkmann 	/* Unused, but we need to keep it here since user space could
1969025331dfSDaniel Borkmann 	 * fill it. It's also broken with regard to NLA_BINARY use in
1970025331dfSDaniel Borkmann 	 * combination with structs.
1971025331dfSDaniel Borkmann 	 */
1972025331dfSDaniel Borkmann 	[IFLA_PORT_VSI_TYPE]	= { .type = NLA_BINARY,
1973025331dfSDaniel Borkmann 				    .len = sizeof(struct ifla_port_vsi) },
1974f7b12606SJiri Pirko };
1975f7b12606SJiri Pirko 
1976d1fdd913SBrenden Blanco static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
197792234c8fSToke Høiland-Jørgensen 	[IFLA_XDP_UNSPEC]	= { .strict_start_type = IFLA_XDP_EXPECTED_FD },
1978d1fdd913SBrenden Blanco 	[IFLA_XDP_FD]		= { .type = NLA_S32 },
197992234c8fSToke Høiland-Jørgensen 	[IFLA_XDP_EXPECTED_FD]	= { .type = NLA_S32 },
1980d1fdd913SBrenden Blanco 	[IFLA_XDP_ATTACHED]	= { .type = NLA_U8 },
198185de8576SDaniel Borkmann 	[IFLA_XDP_FLAGS]	= { .type = NLA_U32 },
198258038695SMartin KaFai Lau 	[IFLA_XDP_PROG_ID]	= { .type = NLA_U32 },
1983d1fdd913SBrenden Blanco };
1984d1fdd913SBrenden Blanco 
1985dc599f76SDavid Ahern static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1986dc599f76SDavid Ahern {
1987dc599f76SDavid Ahern 	const struct rtnl_link_ops *ops = NULL;
1988dc599f76SDavid Ahern 	struct nlattr *linfo[IFLA_INFO_MAX + 1];
1989dc599f76SDavid Ahern 
19908cb08174SJohannes Berg 	if (nla_parse_nested_deprecated(linfo, IFLA_INFO_MAX, nla, ifla_info_policy, NULL) < 0)
1991dc599f76SDavid Ahern 		return NULL;
1992dc599f76SDavid Ahern 
1993dc599f76SDavid Ahern 	if (linfo[IFLA_INFO_KIND]) {
1994dc599f76SDavid Ahern 		char kind[MODULE_NAME_LEN];
1995dc599f76SDavid Ahern 
1996872f6903SFrancis Laniel 		nla_strscpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1997dc599f76SDavid Ahern 		ops = rtnl_link_ops_get(kind);
1998dc599f76SDavid Ahern 	}
1999dc599f76SDavid Ahern 
2000dc599f76SDavid Ahern 	return ops;
2001dc599f76SDavid Ahern }
2002dc599f76SDavid Ahern 
2003dc599f76SDavid Ahern static bool link_master_filtered(struct net_device *dev, int master_idx)
2004dc599f76SDavid Ahern {
2005dc599f76SDavid Ahern 	struct net_device *master;
2006dc599f76SDavid Ahern 
2007dc599f76SDavid Ahern 	if (!master_idx)
2008dc599f76SDavid Ahern 		return false;
2009dc599f76SDavid Ahern 
2010dc599f76SDavid Ahern 	master = netdev_master_upper_dev_get(dev);
2011d3432bf1SLahav Schlesinger 
2012d3432bf1SLahav Schlesinger 	/* 0 is already used to denote IFLA_MASTER wasn't passed, therefore need
2013d3432bf1SLahav Schlesinger 	 * another invalid value for ifindex to denote "no master".
2014d3432bf1SLahav Schlesinger 	 */
2015d3432bf1SLahav Schlesinger 	if (master_idx == -1)
2016d3432bf1SLahav Schlesinger 		return !!master;
2017d3432bf1SLahav Schlesinger 
2018dc599f76SDavid Ahern 	if (!master || master->ifindex != master_idx)
2019dc599f76SDavid Ahern 		return true;
2020dc599f76SDavid Ahern 
2021dc599f76SDavid Ahern 	return false;
2022dc599f76SDavid Ahern }
2023dc599f76SDavid Ahern 
2024dc599f76SDavid Ahern static bool link_kind_filtered(const struct net_device *dev,
2025dc599f76SDavid Ahern 			       const struct rtnl_link_ops *kind_ops)
2026dc599f76SDavid Ahern {
2027dc599f76SDavid Ahern 	if (kind_ops && dev->rtnl_link_ops != kind_ops)
2028dc599f76SDavid Ahern 		return true;
2029dc599f76SDavid Ahern 
2030dc599f76SDavid Ahern 	return false;
2031dc599f76SDavid Ahern }
2032dc599f76SDavid Ahern 
2033dc599f76SDavid Ahern static bool link_dump_filtered(struct net_device *dev,
2034dc599f76SDavid Ahern 			       int master_idx,
2035dc599f76SDavid Ahern 			       const struct rtnl_link_ops *kind_ops)
2036dc599f76SDavid Ahern {
2037dc599f76SDavid Ahern 	if (link_master_filtered(dev, master_idx) ||
2038dc599f76SDavid Ahern 	    link_kind_filtered(dev, kind_ops))
2039dc599f76SDavid Ahern 		return true;
2040dc599f76SDavid Ahern 
2041dc599f76SDavid Ahern 	return false;
2042dc599f76SDavid Ahern }
2043dc599f76SDavid Ahern 
2044c383edc4SChristian Brauner /**
2045c383edc4SChristian Brauner  * rtnl_get_net_ns_capable - Get netns if sufficiently privileged.
2046c383edc4SChristian Brauner  * @sk: netlink socket
2047c383edc4SChristian Brauner  * @netnsid: network namespace identifier
2048c383edc4SChristian Brauner  *
2049c383edc4SChristian Brauner  * Returns the network namespace identified by netnsid on success or an error
2050c383edc4SChristian Brauner  * pointer on failure.
2051c383edc4SChristian Brauner  */
2052c383edc4SChristian Brauner struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid)
205379e1ad14SJiri Benc {
205479e1ad14SJiri Benc 	struct net *net;
205579e1ad14SJiri Benc 
2056f428fe4aSAndrei Vagin 	net = get_net_ns_by_id(sock_net(sk), netnsid);
205779e1ad14SJiri Benc 	if (!net)
205879e1ad14SJiri Benc 		return ERR_PTR(-EINVAL);
205979e1ad14SJiri Benc 
206079e1ad14SJiri Benc 	/* For now, the caller is required to have CAP_NET_ADMIN in
206179e1ad14SJiri Benc 	 * the user namespace owning the target net ns.
206279e1ad14SJiri Benc 	 */
2063f428fe4aSAndrei Vagin 	if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) {
206479e1ad14SJiri Benc 		put_net(net);
206579e1ad14SJiri Benc 		return ERR_PTR(-EACCES);
206679e1ad14SJiri Benc 	}
206779e1ad14SJiri Benc 	return net;
206879e1ad14SJiri Benc }
2069c383edc4SChristian Brauner EXPORT_SYMBOL_GPL(rtnl_get_net_ns_capable);
207079e1ad14SJiri Benc 
2071905cf0abSDavid Ahern static int rtnl_valid_dump_ifinfo_req(const struct nlmsghdr *nlh,
2072905cf0abSDavid Ahern 				      bool strict_check, struct nlattr **tb,
2073905cf0abSDavid Ahern 				      struct netlink_ext_ack *extack)
2074905cf0abSDavid Ahern {
2075905cf0abSDavid Ahern 	int hdrlen;
2076905cf0abSDavid Ahern 
2077905cf0abSDavid Ahern 	if (strict_check) {
2078905cf0abSDavid Ahern 		struct ifinfomsg *ifm;
2079905cf0abSDavid Ahern 
2080905cf0abSDavid Ahern 		if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
2081905cf0abSDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid header for link dump");
2082905cf0abSDavid Ahern 			return -EINVAL;
2083905cf0abSDavid Ahern 		}
2084905cf0abSDavid Ahern 
2085905cf0abSDavid Ahern 		ifm = nlmsg_data(nlh);
2086905cf0abSDavid Ahern 		if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
2087905cf0abSDavid Ahern 		    ifm->ifi_change) {
2088905cf0abSDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid values in header for link dump request");
2089905cf0abSDavid Ahern 			return -EINVAL;
2090905cf0abSDavid Ahern 		}
2091905cf0abSDavid Ahern 		if (ifm->ifi_index) {
2092905cf0abSDavid Ahern 			NL_SET_ERR_MSG(extack, "Filter by device index not supported for link dumps");
2093905cf0abSDavid Ahern 			return -EINVAL;
2094905cf0abSDavid Ahern 		}
2095905cf0abSDavid Ahern 
20968cb08174SJohannes Berg 		return nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb,
20978cb08174SJohannes Berg 						     IFLA_MAX, ifla_policy,
20988cb08174SJohannes Berg 						     extack);
2099905cf0abSDavid Ahern 	}
2100905cf0abSDavid Ahern 
2101905cf0abSDavid Ahern 	/* A hack to preserve kernel<->userspace interface.
2102905cf0abSDavid Ahern 	 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
2103905cf0abSDavid Ahern 	 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
2104905cf0abSDavid Ahern 	 * what iproute2 < v3.9.0 used.
2105905cf0abSDavid Ahern 	 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
2106905cf0abSDavid Ahern 	 * attribute, its netlink message is shorter than struct ifinfomsg.
2107905cf0abSDavid Ahern 	 */
2108905cf0abSDavid Ahern 	hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2109905cf0abSDavid Ahern 		 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2110905cf0abSDavid Ahern 
21118cb08174SJohannes Berg 	return nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy,
21128cb08174SJohannes Berg 				      extack);
2113905cf0abSDavid Ahern }
2114905cf0abSDavid Ahern 
2115b60c5115SThomas Graf static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
21161da177e4SLinus Torvalds {
2117905cf0abSDavid Ahern 	struct netlink_ext_ack *extack = cb->extack;
2118905cf0abSDavid Ahern 	const struct nlmsghdr *nlh = cb->nlh;
21193b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
212079e1ad14SJiri Benc 	struct net *tgt_net = net;
21217c28bd0bSEric Dumazet 	int h, s_h;
21227c28bd0bSEric Dumazet 	int idx = 0, s_idx;
21231da177e4SLinus Torvalds 	struct net_device *dev;
21247c28bd0bSEric Dumazet 	struct hlist_head *head;
2125115c9b81SGreg Rose 	struct nlattr *tb[IFLA_MAX+1];
2126115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
2127dc599f76SDavid Ahern 	const struct rtnl_link_ops *kind_ops = NULL;
2128dc599f76SDavid Ahern 	unsigned int flags = NLM_F_MULTI;
2129dc599f76SDavid Ahern 	int master_idx = 0;
213079e1ad14SJiri Benc 	int netnsid = -1;
2131905cf0abSDavid Ahern 	int err, i;
21321da177e4SLinus Torvalds 
21337c28bd0bSEric Dumazet 	s_h = cb->args[0];
21347c28bd0bSEric Dumazet 	s_idx = cb->args[1];
21357c28bd0bSEric Dumazet 
2136905cf0abSDavid Ahern 	err = rtnl_valid_dump_ifinfo_req(nlh, cb->strict_check, tb, extack);
2137905cf0abSDavid Ahern 	if (err < 0) {
2138905cf0abSDavid Ahern 		if (cb->strict_check)
2139905cf0abSDavid Ahern 			return err;
2140e5eca6d4SMichal Schmidt 
2141905cf0abSDavid Ahern 		goto walk_entries;
214279e1ad14SJiri Benc 	}
214379e1ad14SJiri Benc 
2144905cf0abSDavid Ahern 	for (i = 0; i <= IFLA_MAX; ++i) {
2145905cf0abSDavid Ahern 		if (!tb[i])
2146905cf0abSDavid Ahern 			continue;
2147dc599f76SDavid Ahern 
2148905cf0abSDavid Ahern 		/* new attributes should only be added with strict checking */
2149905cf0abSDavid Ahern 		switch (i) {
2150905cf0abSDavid Ahern 		case IFLA_TARGET_NETNSID:
2151905cf0abSDavid Ahern 			netnsid = nla_get_s32(tb[i]);
2152905cf0abSDavid Ahern 			tgt_net = rtnl_get_net_ns_capable(skb->sk, netnsid);
2153905cf0abSDavid Ahern 			if (IS_ERR(tgt_net)) {
2154905cf0abSDavid Ahern 				NL_SET_ERR_MSG(extack, "Invalid target network namespace id");
2155905cf0abSDavid Ahern 				return PTR_ERR(tgt_net);
2156905cf0abSDavid Ahern 			}
2157905cf0abSDavid Ahern 			break;
2158905cf0abSDavid Ahern 		case IFLA_EXT_MASK:
2159905cf0abSDavid Ahern 			ext_filter_mask = nla_get_u32(tb[i]);
2160905cf0abSDavid Ahern 			break;
2161905cf0abSDavid Ahern 		case IFLA_MASTER:
2162905cf0abSDavid Ahern 			master_idx = nla_get_u32(tb[i]);
2163905cf0abSDavid Ahern 			break;
2164905cf0abSDavid Ahern 		case IFLA_LINKINFO:
2165905cf0abSDavid Ahern 			kind_ops = linkinfo_to_kind_ops(tb[i]);
2166905cf0abSDavid Ahern 			break;
2167905cf0abSDavid Ahern 		default:
2168905cf0abSDavid Ahern 			if (cb->strict_check) {
2169905cf0abSDavid Ahern 				NL_SET_ERR_MSG(extack, "Unsupported attribute in link dump request");
2170905cf0abSDavid Ahern 				return -EINVAL;
2171905cf0abSDavid Ahern 			}
2172905cf0abSDavid Ahern 		}
2173905cf0abSDavid Ahern 	}
2174dc599f76SDavid Ahern 
2175dc599f76SDavid Ahern 	if (master_idx || kind_ops)
2176dc599f76SDavid Ahern 		flags |= NLM_F_DUMP_FILTERED;
2177115c9b81SGreg Rose 
2178905cf0abSDavid Ahern walk_entries:
21797c28bd0bSEric Dumazet 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
21807562f876SPavel Emelianov 		idx = 0;
218179e1ad14SJiri Benc 		head = &tgt_net->dev_index_head[h];
2182cac5e65eSEric Dumazet 		hlist_for_each_entry(dev, head, index_hlist) {
2183dc599f76SDavid Ahern 			if (link_dump_filtered(dev, master_idx, kind_ops))
21843f0ae05dSZhang Shengju 				goto cont;
21851da177e4SLinus Torvalds 			if (idx < s_idx)
21867562f876SPavel Emelianov 				goto cont;
218779e1ad14SJiri Benc 			err = rtnl_fill_ifinfo(skb, dev, net,
218879e1ad14SJiri Benc 					       RTM_NEWLINK,
218915e47304SEric W. Biederman 					       NETLINK_CB(cb->skb).portid,
2190905cf0abSDavid Ahern 					       nlh->nlmsg_seq, 0, flags,
219138e01b30SNicolas Dichtel 					       ext_filter_mask, 0, NULL, 0,
2192d4e4fdf9SGuillaume Nault 					       netnsid, GFP_KERNEL);
2193973462bbSDavid Gibson 
2194f6c5775fSDavid Ahern 			if (err < 0) {
2195f6c5775fSDavid Ahern 				if (likely(skb->len))
21967c28bd0bSEric Dumazet 					goto out;
21974e985adaSThomas Graf 
2198f6c5775fSDavid Ahern 				goto out_err;
2199f6c5775fSDavid Ahern 			}
22007562f876SPavel Emelianov cont:
22017562f876SPavel Emelianov 			idx++;
22021da177e4SLinus Torvalds 		}
22037c28bd0bSEric Dumazet 	}
22047c28bd0bSEric Dumazet out:
2205f6c5775fSDavid Ahern 	err = skb->len;
2206f6c5775fSDavid Ahern out_err:
22077c28bd0bSEric Dumazet 	cb->args[1] = idx;
22087c28bd0bSEric Dumazet 	cb->args[0] = h;
2209a9ecb0cbSzhang kai 	cb->seq = tgt_net->dev_base_seq;
2210d0225784SJakub Sitnicki 	nl_dump_check_consistent(cb, nlmsg_hdr(skb));
221179e1ad14SJiri Benc 	if (netnsid >= 0)
221279e1ad14SJiri Benc 		put_net(tgt_net);
22131da177e4SLinus Torvalds 
2214f6c5775fSDavid Ahern 	return err;
22151da177e4SLinus Torvalds }
22161da177e4SLinus Torvalds 
2217fceb6435SJohannes Berg int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len,
2218fceb6435SJohannes Berg 			struct netlink_ext_ack *exterr)
2219f7b12606SJiri Pirko {
22208cb08174SJohannes Berg 	return nla_parse_deprecated(tb, IFLA_MAX, head, len, ifla_policy,
22218cb08174SJohannes Berg 				    exterr);
2222f7b12606SJiri Pirko }
2223f7b12606SJiri Pirko EXPORT_SYMBOL(rtnl_nla_parse_ifla);
222457b61080SScott Feldman 
222581adee47SEric W. Biederman struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
222681adee47SEric W. Biederman {
222781adee47SEric W. Biederman 	struct net *net;
222881adee47SEric W. Biederman 	/* Examine the link attributes and figure out which
222981adee47SEric W. Biederman 	 * network namespace we are talking about.
223081adee47SEric W. Biederman 	 */
223181adee47SEric W. Biederman 	if (tb[IFLA_NET_NS_PID])
223281adee47SEric W. Biederman 		net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
2233f0630529SEric W. Biederman 	else if (tb[IFLA_NET_NS_FD])
2234f0630529SEric W. Biederman 		net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
223581adee47SEric W. Biederman 	else
223681adee47SEric W. Biederman 		net = get_net(src_net);
223781adee47SEric W. Biederman 	return net;
223881adee47SEric W. Biederman }
223981adee47SEric W. Biederman EXPORT_SYMBOL(rtnl_link_get_net);
224081adee47SEric W. Biederman 
22417c4f63baSChristian Brauner /* Figure out which network namespace we are talking about by
22427c4f63baSChristian Brauner  * examining the link attributes in the following order:
22437c4f63baSChristian Brauner  *
22447c4f63baSChristian Brauner  * 1. IFLA_NET_NS_PID
22457c4f63baSChristian Brauner  * 2. IFLA_NET_NS_FD
22467e4a8d5aSChristian Brauner  * 3. IFLA_TARGET_NETNSID
22477c4f63baSChristian Brauner  */
22487c4f63baSChristian Brauner static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net,
22497c4f63baSChristian Brauner 					       struct nlattr *tb[])
22507c4f63baSChristian Brauner {
22517c4f63baSChristian Brauner 	struct net *net;
22527c4f63baSChristian Brauner 
22537c4f63baSChristian Brauner 	if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])
22547c4f63baSChristian Brauner 		return rtnl_link_get_net(src_net, tb);
22557c4f63baSChristian Brauner 
22567e4a8d5aSChristian Brauner 	if (!tb[IFLA_TARGET_NETNSID])
22577c4f63baSChristian Brauner 		return get_net(src_net);
22587c4f63baSChristian Brauner 
22597e4a8d5aSChristian Brauner 	net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_TARGET_NETNSID]));
22607c4f63baSChristian Brauner 	if (!net)
22617c4f63baSChristian Brauner 		return ERR_PTR(-EINVAL);
22627c4f63baSChristian Brauner 
22637c4f63baSChristian Brauner 	return net;
22647c4f63baSChristian Brauner }
22657c4f63baSChristian Brauner 
22667c4f63baSChristian Brauner static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb,
22677c4f63baSChristian Brauner 					     struct net *src_net,
22687c4f63baSChristian Brauner 					     struct nlattr *tb[], int cap)
22697c4f63baSChristian Brauner {
22707c4f63baSChristian Brauner 	struct net *net;
22717c4f63baSChristian Brauner 
22727c4f63baSChristian Brauner 	net = rtnl_link_get_net_by_nlattr(src_net, tb);
22737c4f63baSChristian Brauner 	if (IS_ERR(net))
22747c4f63baSChristian Brauner 		return net;
22757c4f63baSChristian Brauner 
22767c4f63baSChristian Brauner 	if (!netlink_ns_capable(skb, net->user_ns, cap)) {
22777c4f63baSChristian Brauner 		put_net(net);
22787c4f63baSChristian Brauner 		return ERR_PTR(-EPERM);
22797c4f63baSChristian Brauner 	}
22807c4f63baSChristian Brauner 
22817c4f63baSChristian Brauner 	return net;
22827c4f63baSChristian Brauner }
22837c4f63baSChristian Brauner 
22844ff66caeSChristian Brauner /* Verify that rtnetlink requests do not pass additional properties
22854ff66caeSChristian Brauner  * potentially referring to different network namespaces.
22864ff66caeSChristian Brauner  */
22874ff66caeSChristian Brauner static int rtnl_ensure_unique_netns(struct nlattr *tb[],
22884ff66caeSChristian Brauner 				    struct netlink_ext_ack *extack,
22894ff66caeSChristian Brauner 				    bool netns_id_only)
22904ff66caeSChristian Brauner {
22914ff66caeSChristian Brauner 
22924ff66caeSChristian Brauner 	if (netns_id_only) {
22934ff66caeSChristian Brauner 		if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD])
22944ff66caeSChristian Brauner 			return 0;
22954ff66caeSChristian Brauner 
22964ff66caeSChristian Brauner 		NL_SET_ERR_MSG(extack, "specified netns attribute not supported");
22974ff66caeSChristian Brauner 		return -EOPNOTSUPP;
22984ff66caeSChristian Brauner 	}
22994ff66caeSChristian Brauner 
23007e4a8d5aSChristian Brauner 	if (tb[IFLA_TARGET_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]))
23014ff66caeSChristian Brauner 		goto invalid_attr;
23024ff66caeSChristian Brauner 
23037e4a8d5aSChristian Brauner 	if (tb[IFLA_NET_NS_PID] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_FD]))
23044ff66caeSChristian Brauner 		goto invalid_attr;
23054ff66caeSChristian Brauner 
23067e4a8d5aSChristian Brauner 	if (tb[IFLA_NET_NS_FD] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_PID]))
23074ff66caeSChristian Brauner 		goto invalid_attr;
23084ff66caeSChristian Brauner 
23094ff66caeSChristian Brauner 	return 0;
23104ff66caeSChristian Brauner 
23114ff66caeSChristian Brauner invalid_attr:
23124ff66caeSChristian Brauner 	NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified");
23134ff66caeSChristian Brauner 	return -EINVAL;
23144ff66caeSChristian Brauner }
23154ff66caeSChristian Brauner 
2316a14857c2SBin Chen static	int rtnl_set_vf_rate(struct net_device *dev, int vf, int min_tx_rate,
2317a14857c2SBin Chen 			     int max_tx_rate)
2318a14857c2SBin Chen {
2319a14857c2SBin Chen 	const struct net_device_ops *ops = dev->netdev_ops;
2320a14857c2SBin Chen 
2321a14857c2SBin Chen 	if (!ops->ndo_set_vf_rate)
2322a14857c2SBin Chen 		return -EOPNOTSUPP;
2323a14857c2SBin Chen 	if (max_tx_rate && max_tx_rate < min_tx_rate)
2324a14857c2SBin Chen 		return -EINVAL;
2325a14857c2SBin Chen 
2326a14857c2SBin Chen 	return ops->ndo_set_vf_rate(dev, vf, min_tx_rate, max_tx_rate);
2327a14857c2SBin Chen }
2328a14857c2SBin Chen 
23298679c31eSRocco Yue static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[],
23308679c31eSRocco Yue 			    struct netlink_ext_ack *extack)
23311840bb13SThomas Graf {
23321840bb13SThomas Graf 	if (dev) {
23331840bb13SThomas Graf 		if (tb[IFLA_ADDRESS] &&
23341840bb13SThomas Graf 		    nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
23351840bb13SThomas Graf 			return -EINVAL;
23361840bb13SThomas Graf 
23371840bb13SThomas Graf 		if (tb[IFLA_BROADCAST] &&
23381840bb13SThomas Graf 		    nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
23391840bb13SThomas Graf 			return -EINVAL;
23401840bb13SThomas Graf 	}
23411840bb13SThomas Graf 
2342cf7afbfeSThomas Graf 	if (tb[IFLA_AF_SPEC]) {
2343cf7afbfeSThomas Graf 		struct nlattr *af;
2344cf7afbfeSThomas Graf 		int rem, err;
2345cf7afbfeSThomas Graf 
2346cf7afbfeSThomas Graf 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2347cf7afbfeSThomas Graf 			const struct rtnl_af_ops *af_ops;
2348cf7afbfeSThomas Graf 
23495fa85a09SFlorian Westphal 			af_ops = rtnl_af_lookup(nla_type(af));
2350a100243dSCong Wang 			if (!af_ops)
2351cf7afbfeSThomas Graf 				return -EAFNOSUPPORT;
2352cf7afbfeSThomas Graf 
2353a100243dSCong Wang 			if (!af_ops->set_link_af)
2354cf7afbfeSThomas Graf 				return -EOPNOTSUPP;
2355cf7afbfeSThomas Graf 
2356cf7afbfeSThomas Graf 			if (af_ops->validate_link_af) {
23578679c31eSRocco Yue 				err = af_ops->validate_link_af(dev, af, extack);
2358a100243dSCong Wang 				if (err < 0)
2359cf7afbfeSThomas Graf 					return err;
2360cf7afbfeSThomas Graf 			}
2361cf7afbfeSThomas Graf 		}
2362cf7afbfeSThomas Graf 	}
2363cf7afbfeSThomas Graf 
23641840bb13SThomas Graf 	return 0;
23651840bb13SThomas Graf }
23661840bb13SThomas Graf 
2367cc8e27ccSEli Cohen static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
2368cc8e27ccSEli Cohen 				  int guid_type)
2369cc8e27ccSEli Cohen {
2370cc8e27ccSEli Cohen 	const struct net_device_ops *ops = dev->netdev_ops;
2371cc8e27ccSEli Cohen 
2372cc8e27ccSEli Cohen 	return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
2373cc8e27ccSEli Cohen }
2374cc8e27ccSEli Cohen 
2375cc8e27ccSEli Cohen static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
2376cc8e27ccSEli Cohen {
2377cc8e27ccSEli Cohen 	if (dev->type != ARPHRD_INFINIBAND)
2378cc8e27ccSEli Cohen 		return -EOPNOTSUPP;
2379cc8e27ccSEli Cohen 
2380cc8e27ccSEli Cohen 	return handle_infiniband_guid(dev, ivt, guid_type);
2381cc8e27ccSEli Cohen }
2382cc8e27ccSEli Cohen 
23834f7d2cdfSDaniel Borkmann static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
2384c02db8c6SChris Wright {
2385c02db8c6SChris Wright 	const struct net_device_ops *ops = dev->netdev_ops;
23864f7d2cdfSDaniel Borkmann 	int err = -EINVAL;
2387c02db8c6SChris Wright 
23884f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_MAC]) {
23894f7d2cdfSDaniel Borkmann 		struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
23904f7d2cdfSDaniel Borkmann 
2391ff08ddbaSDan Carpenter 		if (ivm->vf >= INT_MAX)
2392ff08ddbaSDan Carpenter 			return -EINVAL;
2393c02db8c6SChris Wright 		err = -EOPNOTSUPP;
2394c02db8c6SChris Wright 		if (ops->ndo_set_vf_mac)
2395c02db8c6SChris Wright 			err = ops->ndo_set_vf_mac(dev, ivm->vf,
2396c02db8c6SChris Wright 						  ivm->mac);
23974f7d2cdfSDaniel Borkmann 		if (err < 0)
23984f7d2cdfSDaniel Borkmann 			return err;
2399c02db8c6SChris Wright 	}
24004f7d2cdfSDaniel Borkmann 
24014f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_VLAN]) {
24024f7d2cdfSDaniel Borkmann 		struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
24034f7d2cdfSDaniel Borkmann 
2404ff08ddbaSDan Carpenter 		if (ivv->vf >= INT_MAX)
2405ff08ddbaSDan Carpenter 			return -EINVAL;
2406c02db8c6SChris Wright 		err = -EOPNOTSUPP;
2407c02db8c6SChris Wright 		if (ops->ndo_set_vf_vlan)
24084f7d2cdfSDaniel Borkmann 			err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
240979aab093SMoshe Shemesh 						   ivv->qos,
241079aab093SMoshe Shemesh 						   htons(ETH_P_8021Q));
241179aab093SMoshe Shemesh 		if (err < 0)
241279aab093SMoshe Shemesh 			return err;
241379aab093SMoshe Shemesh 	}
241479aab093SMoshe Shemesh 
241579aab093SMoshe Shemesh 	if (tb[IFLA_VF_VLAN_LIST]) {
241679aab093SMoshe Shemesh 		struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
241779aab093SMoshe Shemesh 		struct nlattr *attr;
241879aab093SMoshe Shemesh 		int rem, len = 0;
241979aab093SMoshe Shemesh 
242079aab093SMoshe Shemesh 		err = -EOPNOTSUPP;
242179aab093SMoshe Shemesh 		if (!ops->ndo_set_vf_vlan)
242279aab093SMoshe Shemesh 			return err;
242379aab093SMoshe Shemesh 
242479aab093SMoshe Shemesh 		nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
242579aab093SMoshe Shemesh 			if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
242679aab093SMoshe Shemesh 			    nla_len(attr) < NLA_HDRLEN) {
242779aab093SMoshe Shemesh 				return -EINVAL;
242879aab093SMoshe Shemesh 			}
242979aab093SMoshe Shemesh 			if (len >= MAX_VLAN_LIST_LEN)
243079aab093SMoshe Shemesh 				return -EOPNOTSUPP;
243179aab093SMoshe Shemesh 			ivvl[len] = nla_data(attr);
243279aab093SMoshe Shemesh 
243379aab093SMoshe Shemesh 			len++;
243479aab093SMoshe Shemesh 		}
2435fa34cd94SArnd Bergmann 		if (len == 0)
2436fa34cd94SArnd Bergmann 			return -EINVAL;
2437fa34cd94SArnd Bergmann 
2438ff08ddbaSDan Carpenter 		if (ivvl[0]->vf >= INT_MAX)
2439ff08ddbaSDan Carpenter 			return -EINVAL;
244079aab093SMoshe Shemesh 		err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
244179aab093SMoshe Shemesh 					   ivvl[0]->qos, ivvl[0]->vlan_proto);
24424f7d2cdfSDaniel Borkmann 		if (err < 0)
24434f7d2cdfSDaniel Borkmann 			return err;
2444c02db8c6SChris Wright 	}
24454f7d2cdfSDaniel Borkmann 
24464f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_TX_RATE]) {
24474f7d2cdfSDaniel Borkmann 		struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
2448ed616689SSucheta Chakraborty 		struct ifla_vf_info ivf;
24494f7d2cdfSDaniel Borkmann 
2450ff08ddbaSDan Carpenter 		if (ivt->vf >= INT_MAX)
2451ff08ddbaSDan Carpenter 			return -EINVAL;
2452c02db8c6SChris Wright 		err = -EOPNOTSUPP;
2453ed616689SSucheta Chakraborty 		if (ops->ndo_get_vf_config)
24544f7d2cdfSDaniel Borkmann 			err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
24554f7d2cdfSDaniel Borkmann 		if (err < 0)
24564f7d2cdfSDaniel Borkmann 			return err;
24574f7d2cdfSDaniel Borkmann 
2458a14857c2SBin Chen 		err = rtnl_set_vf_rate(dev, ivt->vf,
2459a14857c2SBin Chen 				       ivf.min_tx_rate, ivt->rate);
24604f7d2cdfSDaniel Borkmann 		if (err < 0)
24614f7d2cdfSDaniel Borkmann 			return err;
2462c02db8c6SChris Wright 	}
24634f7d2cdfSDaniel Borkmann 
24644f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_RATE]) {
24654f7d2cdfSDaniel Borkmann 		struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
24664f7d2cdfSDaniel Borkmann 
2467ff08ddbaSDan Carpenter 		if (ivt->vf >= INT_MAX)
2468ff08ddbaSDan Carpenter 			return -EINVAL;
2469a14857c2SBin Chen 
2470a14857c2SBin Chen 		err = rtnl_set_vf_rate(dev, ivt->vf,
2471a14857c2SBin Chen 				       ivt->min_tx_rate, ivt->max_tx_rate);
24724f7d2cdfSDaniel Borkmann 		if (err < 0)
24734f7d2cdfSDaniel Borkmann 			return err;
2474ed616689SSucheta Chakraborty 	}
24754f7d2cdfSDaniel Borkmann 
24764f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_SPOOFCHK]) {
24774f7d2cdfSDaniel Borkmann 		struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
24784f7d2cdfSDaniel Borkmann 
2479ff08ddbaSDan Carpenter 		if (ivs->vf >= INT_MAX)
2480ff08ddbaSDan Carpenter 			return -EINVAL;
24815f8444a3SGreg Rose 		err = -EOPNOTSUPP;
24825f8444a3SGreg Rose 		if (ops->ndo_set_vf_spoofchk)
24835f8444a3SGreg Rose 			err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
24845f8444a3SGreg Rose 						       ivs->setting);
24854f7d2cdfSDaniel Borkmann 		if (err < 0)
24864f7d2cdfSDaniel Borkmann 			return err;
24875f8444a3SGreg Rose 	}
24884f7d2cdfSDaniel Borkmann 
24894f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_LINK_STATE]) {
24904f7d2cdfSDaniel Borkmann 		struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
24914f7d2cdfSDaniel Borkmann 
2492ff08ddbaSDan Carpenter 		if (ivl->vf >= INT_MAX)
2493ff08ddbaSDan Carpenter 			return -EINVAL;
24941d8faf48SRony Efraim 		err = -EOPNOTSUPP;
24951d8faf48SRony Efraim 		if (ops->ndo_set_vf_link_state)
24961d8faf48SRony Efraim 			err = ops->ndo_set_vf_link_state(dev, ivl->vf,
24971d8faf48SRony Efraim 							 ivl->link_state);
24984f7d2cdfSDaniel Borkmann 		if (err < 0)
24994f7d2cdfSDaniel Borkmann 			return err;
25001d8faf48SRony Efraim 	}
25014f7d2cdfSDaniel Borkmann 
25024f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_RSS_QUERY_EN]) {
250301a3d796SVlad Zolotarov 		struct ifla_vf_rss_query_en *ivrssq_en;
250401a3d796SVlad Zolotarov 
250501a3d796SVlad Zolotarov 		err = -EOPNOTSUPP;
25064f7d2cdfSDaniel Borkmann 		ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
2507ff08ddbaSDan Carpenter 		if (ivrssq_en->vf >= INT_MAX)
2508ff08ddbaSDan Carpenter 			return -EINVAL;
250901a3d796SVlad Zolotarov 		if (ops->ndo_set_vf_rss_query_en)
25104f7d2cdfSDaniel Borkmann 			err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
251101a3d796SVlad Zolotarov 							   ivrssq_en->setting);
25124f7d2cdfSDaniel Borkmann 		if (err < 0)
25134f7d2cdfSDaniel Borkmann 			return err;
251401a3d796SVlad Zolotarov 	}
25154f7d2cdfSDaniel Borkmann 
2516dd461d6aSHiroshi Shimamoto 	if (tb[IFLA_VF_TRUST]) {
2517dd461d6aSHiroshi Shimamoto 		struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
2518dd461d6aSHiroshi Shimamoto 
2519ff08ddbaSDan Carpenter 		if (ivt->vf >= INT_MAX)
2520ff08ddbaSDan Carpenter 			return -EINVAL;
2521dd461d6aSHiroshi Shimamoto 		err = -EOPNOTSUPP;
2522dd461d6aSHiroshi Shimamoto 		if (ops->ndo_set_vf_trust)
2523dd461d6aSHiroshi Shimamoto 			err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
2524dd461d6aSHiroshi Shimamoto 		if (err < 0)
2525dd461d6aSHiroshi Shimamoto 			return err;
2526dd461d6aSHiroshi Shimamoto 	}
2527dd461d6aSHiroshi Shimamoto 
2528cc8e27ccSEli Cohen 	if (tb[IFLA_VF_IB_NODE_GUID]) {
2529cc8e27ccSEli Cohen 		struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
2530cc8e27ccSEli Cohen 
2531ff08ddbaSDan Carpenter 		if (ivt->vf >= INT_MAX)
2532ff08ddbaSDan Carpenter 			return -EINVAL;
2533cc8e27ccSEli Cohen 		if (!ops->ndo_set_vf_guid)
2534cc8e27ccSEli Cohen 			return -EOPNOTSUPP;
2535cc8e27ccSEli Cohen 		return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
2536cc8e27ccSEli Cohen 	}
2537cc8e27ccSEli Cohen 
2538cc8e27ccSEli Cohen 	if (tb[IFLA_VF_IB_PORT_GUID]) {
2539cc8e27ccSEli Cohen 		struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
2540cc8e27ccSEli Cohen 
2541ff08ddbaSDan Carpenter 		if (ivt->vf >= INT_MAX)
2542ff08ddbaSDan Carpenter 			return -EINVAL;
2543cc8e27ccSEli Cohen 		if (!ops->ndo_set_vf_guid)
2544cc8e27ccSEli Cohen 			return -EOPNOTSUPP;
2545cc8e27ccSEli Cohen 
2546cc8e27ccSEli Cohen 		return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
2547cc8e27ccSEli Cohen 	}
2548cc8e27ccSEli Cohen 
2549c02db8c6SChris Wright 	return err;
2550c02db8c6SChris Wright }
2551c02db8c6SChris Wright 
255233eaf2a6SDavid Ahern static int do_set_master(struct net_device *dev, int ifindex,
255333eaf2a6SDavid Ahern 			 struct netlink_ext_ack *extack)
2554fbaec0eaSJiri Pirko {
2555898e5061SJiri Pirko 	struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
2556fbaec0eaSJiri Pirko 	const struct net_device_ops *ops;
2557fbaec0eaSJiri Pirko 	int err;
2558fbaec0eaSJiri Pirko 
2559898e5061SJiri Pirko 	if (upper_dev) {
2560898e5061SJiri Pirko 		if (upper_dev->ifindex == ifindex)
2561fbaec0eaSJiri Pirko 			return 0;
2562898e5061SJiri Pirko 		ops = upper_dev->netdev_ops;
2563fbaec0eaSJiri Pirko 		if (ops->ndo_del_slave) {
2564898e5061SJiri Pirko 			err = ops->ndo_del_slave(upper_dev, dev);
2565fbaec0eaSJiri Pirko 			if (err)
2566fbaec0eaSJiri Pirko 				return err;
2567fbaec0eaSJiri Pirko 		} else {
2568fbaec0eaSJiri Pirko 			return -EOPNOTSUPP;
2569fbaec0eaSJiri Pirko 		}
2570fbaec0eaSJiri Pirko 	}
2571fbaec0eaSJiri Pirko 
2572fbaec0eaSJiri Pirko 	if (ifindex) {
2573898e5061SJiri Pirko 		upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
2574898e5061SJiri Pirko 		if (!upper_dev)
2575fbaec0eaSJiri Pirko 			return -EINVAL;
2576898e5061SJiri Pirko 		ops = upper_dev->netdev_ops;
2577fbaec0eaSJiri Pirko 		if (ops->ndo_add_slave) {
257833eaf2a6SDavid Ahern 			err = ops->ndo_add_slave(upper_dev, dev, extack);
2579fbaec0eaSJiri Pirko 			if (err)
2580fbaec0eaSJiri Pirko 				return err;
2581fbaec0eaSJiri Pirko 		} else {
2582fbaec0eaSJiri Pirko 			return -EOPNOTSUPP;
2583fbaec0eaSJiri Pirko 		}
2584fbaec0eaSJiri Pirko 	}
2585fbaec0eaSJiri Pirko 	return 0;
2586fbaec0eaSJiri Pirko }
2587fbaec0eaSJiri Pirko 
2588829eb208SRoopa Prabhu static const struct nla_policy ifla_proto_down_reason_policy[IFLA_PROTO_DOWN_REASON_VALUE + 1] = {
2589829eb208SRoopa Prabhu 	[IFLA_PROTO_DOWN_REASON_MASK]	= { .type = NLA_U32 },
2590829eb208SRoopa Prabhu 	[IFLA_PROTO_DOWN_REASON_VALUE]	= { .type = NLA_U32 },
2591829eb208SRoopa Prabhu };
2592829eb208SRoopa Prabhu 
2593829eb208SRoopa Prabhu static int do_set_proto_down(struct net_device *dev,
2594829eb208SRoopa Prabhu 			     struct nlattr *nl_proto_down,
2595829eb208SRoopa Prabhu 			     struct nlattr *nl_proto_down_reason,
2596829eb208SRoopa Prabhu 			     struct netlink_ext_ack *extack)
2597829eb208SRoopa Prabhu {
2598829eb208SRoopa Prabhu 	struct nlattr *pdreason[IFLA_PROTO_DOWN_REASON_MAX + 1];
2599829eb208SRoopa Prabhu 	unsigned long mask = 0;
2600829eb208SRoopa Prabhu 	u32 value;
2601829eb208SRoopa Prabhu 	bool proto_down;
2602829eb208SRoopa Prabhu 	int err;
2603829eb208SRoopa Prabhu 
26042106efdaSJakub Kicinski 	if (!(dev->priv_flags & IFF_CHANGE_PROTO_DOWN)) {
2605829eb208SRoopa Prabhu 		NL_SET_ERR_MSG(extack,  "Protodown not supported by device");
2606829eb208SRoopa Prabhu 		return -EOPNOTSUPP;
2607829eb208SRoopa Prabhu 	}
2608829eb208SRoopa Prabhu 
2609829eb208SRoopa Prabhu 	if (nl_proto_down_reason) {
2610829eb208SRoopa Prabhu 		err = nla_parse_nested_deprecated(pdreason,
2611829eb208SRoopa Prabhu 						  IFLA_PROTO_DOWN_REASON_MAX,
2612829eb208SRoopa Prabhu 						  nl_proto_down_reason,
2613829eb208SRoopa Prabhu 						  ifla_proto_down_reason_policy,
2614829eb208SRoopa Prabhu 						  NULL);
2615829eb208SRoopa Prabhu 		if (err < 0)
2616829eb208SRoopa Prabhu 			return err;
2617829eb208SRoopa Prabhu 
2618829eb208SRoopa Prabhu 		if (!pdreason[IFLA_PROTO_DOWN_REASON_VALUE]) {
2619829eb208SRoopa Prabhu 			NL_SET_ERR_MSG(extack, "Invalid protodown reason value");
2620829eb208SRoopa Prabhu 			return -EINVAL;
2621829eb208SRoopa Prabhu 		}
2622829eb208SRoopa Prabhu 
2623829eb208SRoopa Prabhu 		value = nla_get_u32(pdreason[IFLA_PROTO_DOWN_REASON_VALUE]);
2624829eb208SRoopa Prabhu 
2625829eb208SRoopa Prabhu 		if (pdreason[IFLA_PROTO_DOWN_REASON_MASK])
2626829eb208SRoopa Prabhu 			mask = nla_get_u32(pdreason[IFLA_PROTO_DOWN_REASON_MASK]);
2627829eb208SRoopa Prabhu 
2628829eb208SRoopa Prabhu 		dev_change_proto_down_reason(dev, mask, value);
2629829eb208SRoopa Prabhu 	}
2630829eb208SRoopa Prabhu 
2631829eb208SRoopa Prabhu 	if (nl_proto_down) {
2632829eb208SRoopa Prabhu 		proto_down = nla_get_u8(nl_proto_down);
2633829eb208SRoopa Prabhu 
2634d467d0bcSZheng Yongjun 		/* Don't turn off protodown if there are active reasons */
2635829eb208SRoopa Prabhu 		if (!proto_down && dev->proto_down_reason) {
2636829eb208SRoopa Prabhu 			NL_SET_ERR_MSG(extack, "Cannot clear protodown, active reasons");
2637829eb208SRoopa Prabhu 			return -EBUSY;
2638829eb208SRoopa Prabhu 		}
2639829eb208SRoopa Prabhu 		err = dev_change_proto_down(dev,
2640829eb208SRoopa Prabhu 					    proto_down);
2641829eb208SRoopa Prabhu 		if (err)
2642829eb208SRoopa Prabhu 			return err;
2643829eb208SRoopa Prabhu 	}
2644829eb208SRoopa Prabhu 
2645829eb208SRoopa Prabhu 	return 0;
2646829eb208SRoopa Prabhu }
2647829eb208SRoopa Prabhu 
264890c325e3SNicolas Dichtel #define DO_SETLINK_MODIFIED	0x01
2649ba998906SNicolas Dichtel /* notify flag means notify + modified. */
2650ba998906SNicolas Dichtel #define DO_SETLINK_NOTIFY	0x03
265190f62cf3SEric W. Biederman static int do_setlink(const struct sk_buff *skb,
265290f62cf3SEric W. Biederman 		      struct net_device *dev, struct ifinfomsg *ifm,
2653ddf9f970SJakub Kicinski 		      struct netlink_ext_ack *extack,
26545ea08b52SFlorent Fourcot 		      struct nlattr **tb, int status)
26550157f60cSPatrick McHardy {
2656d314774cSStephen Hemminger 	const struct net_device_ops *ops = dev->netdev_ops;
26575ea08b52SFlorent Fourcot 	char ifname[IFNAMSIZ];
26580157f60cSPatrick McHardy 	int err;
26590157f60cSPatrick McHardy 
26608679c31eSRocco Yue 	err = validate_linkmsg(dev, tb, extack);
2661644c7eebSEric Dumazet 	if (err < 0)
2662644c7eebSEric Dumazet 		return err;
2663644c7eebSEric Dumazet 
26645ea08b52SFlorent Fourcot 	if (tb[IFLA_IFNAME])
26655ea08b52SFlorent Fourcot 		nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
26665ea08b52SFlorent Fourcot 	else
26675ea08b52SFlorent Fourcot 		ifname[0] = '\0';
26685ea08b52SFlorent Fourcot 
26697e4a8d5aSChristian Brauner 	if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_TARGET_NETNSID]) {
26705ea08b52SFlorent Fourcot 		const char *pat = ifname[0] ? ifname : NULL;
2671eeb85a14SAndrei Vagin 		struct net *net;
2672eeb85a14SAndrei Vagin 		int new_ifindex;
2673eeb85a14SAndrei Vagin 
2674eeb85a14SAndrei Vagin 		net = rtnl_link_get_net_capable(skb, dev_net(dev),
26757c4f63baSChristian Brauner 						tb, CAP_NET_ADMIN);
2676d8a5ec67SEric W. Biederman 		if (IS_ERR(net)) {
2677d8a5ec67SEric W. Biederman 			err = PTR_ERR(net);
2678d8a5ec67SEric W. Biederman 			goto errout;
2679d8a5ec67SEric W. Biederman 		}
26807c4f63baSChristian Brauner 
2681eeb85a14SAndrei Vagin 		if (tb[IFLA_NEW_IFINDEX])
2682eeb85a14SAndrei Vagin 			new_ifindex = nla_get_s32(tb[IFLA_NEW_IFINDEX]);
2683eeb85a14SAndrei Vagin 		else
2684eeb85a14SAndrei Vagin 			new_ifindex = 0;
2685eeb85a14SAndrei Vagin 
268696a6b93bSAndrey Ignatov 		err = __dev_change_net_namespace(dev, net, pat, new_ifindex);
2687d8a5ec67SEric W. Biederman 		put_net(net);
2688d8a5ec67SEric W. Biederman 		if (err)
2689d8a5ec67SEric W. Biederman 			goto errout;
269090c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
2691d8a5ec67SEric W. Biederman 	}
2692d8a5ec67SEric W. Biederman 
26930157f60cSPatrick McHardy 	if (tb[IFLA_MAP]) {
26940157f60cSPatrick McHardy 		struct rtnl_link_ifmap *u_map;
26950157f60cSPatrick McHardy 		struct ifmap k_map;
26960157f60cSPatrick McHardy 
2697d314774cSStephen Hemminger 		if (!ops->ndo_set_config) {
26980157f60cSPatrick McHardy 			err = -EOPNOTSUPP;
26990157f60cSPatrick McHardy 			goto errout;
27000157f60cSPatrick McHardy 		}
27010157f60cSPatrick McHardy 
27020157f60cSPatrick McHardy 		if (!netif_device_present(dev)) {
27030157f60cSPatrick McHardy 			err = -ENODEV;
27040157f60cSPatrick McHardy 			goto errout;
27050157f60cSPatrick McHardy 		}
27060157f60cSPatrick McHardy 
27070157f60cSPatrick McHardy 		u_map = nla_data(tb[IFLA_MAP]);
27080157f60cSPatrick McHardy 		k_map.mem_start = (unsigned long) u_map->mem_start;
27090157f60cSPatrick McHardy 		k_map.mem_end = (unsigned long) u_map->mem_end;
27100157f60cSPatrick McHardy 		k_map.base_addr = (unsigned short) u_map->base_addr;
27110157f60cSPatrick McHardy 		k_map.irq = (unsigned char) u_map->irq;
27120157f60cSPatrick McHardy 		k_map.dma = (unsigned char) u_map->dma;
27130157f60cSPatrick McHardy 		k_map.port = (unsigned char) u_map->port;
27140157f60cSPatrick McHardy 
2715d314774cSStephen Hemminger 		err = ops->ndo_set_config(dev, &k_map);
27160157f60cSPatrick McHardy 		if (err < 0)
27170157f60cSPatrick McHardy 			goto errout;
27180157f60cSPatrick McHardy 
2719ba998906SNicolas Dichtel 		status |= DO_SETLINK_NOTIFY;
27200157f60cSPatrick McHardy 	}
27210157f60cSPatrick McHardy 
27220157f60cSPatrick McHardy 	if (tb[IFLA_ADDRESS]) {
27230157f60cSPatrick McHardy 		struct sockaddr *sa;
27240157f60cSPatrick McHardy 		int len;
27250157f60cSPatrick McHardy 
2726153711f9SWANG Cong 		len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
2727153711f9SWANG Cong 						  sizeof(*sa));
27280157f60cSPatrick McHardy 		sa = kmalloc(len, GFP_KERNEL);
27290157f60cSPatrick McHardy 		if (!sa) {
27300157f60cSPatrick McHardy 			err = -ENOMEM;
27310157f60cSPatrick McHardy 			goto errout;
27320157f60cSPatrick McHardy 		}
27330157f60cSPatrick McHardy 		sa->sa_family = dev->type;
27340157f60cSPatrick McHardy 		memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
27350157f60cSPatrick McHardy 		       dev->addr_len);
27363b23a32aSCong Wang 		err = dev_set_mac_address_user(dev, sa, extack);
27370157f60cSPatrick McHardy 		kfree(sa);
27380157f60cSPatrick McHardy 		if (err)
27390157f60cSPatrick McHardy 			goto errout;
274090c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
27410157f60cSPatrick McHardy 	}
27420157f60cSPatrick McHardy 
27430157f60cSPatrick McHardy 	if (tb[IFLA_MTU]) {
27447a4c53beSStephen Hemminger 		err = dev_set_mtu_ext(dev, nla_get_u32(tb[IFLA_MTU]), extack);
27450157f60cSPatrick McHardy 		if (err < 0)
27460157f60cSPatrick McHardy 			goto errout;
274790c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
27480157f60cSPatrick McHardy 	}
27490157f60cSPatrick McHardy 
2750cbda10faSVlad Dogaru 	if (tb[IFLA_GROUP]) {
2751cbda10faSVlad Dogaru 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
2752ba998906SNicolas Dichtel 		status |= DO_SETLINK_NOTIFY;
2753cbda10faSVlad Dogaru 	}
2754cbda10faSVlad Dogaru 
27550157f60cSPatrick McHardy 	/*
27560157f60cSPatrick McHardy 	 * Interface selected by interface index but interface
27570157f60cSPatrick McHardy 	 * name provided implies that a name change has been
27580157f60cSPatrick McHardy 	 * requested.
27590157f60cSPatrick McHardy 	 */
27600157f60cSPatrick McHardy 	if (ifm->ifi_index > 0 && ifname[0]) {
27610157f60cSPatrick McHardy 		err = dev_change_name(dev, ifname);
27620157f60cSPatrick McHardy 		if (err < 0)
27630157f60cSPatrick McHardy 			goto errout;
276490c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
27650157f60cSPatrick McHardy 	}
27660157f60cSPatrick McHardy 
27670b815a1aSStephen Hemminger 	if (tb[IFLA_IFALIAS]) {
27680b815a1aSStephen Hemminger 		err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
27690b815a1aSStephen Hemminger 				    nla_len(tb[IFLA_IFALIAS]));
27700b815a1aSStephen Hemminger 		if (err < 0)
27710b815a1aSStephen Hemminger 			goto errout;
2772ba998906SNicolas Dichtel 		status |= DO_SETLINK_NOTIFY;
27730b815a1aSStephen Hemminger 	}
27740b815a1aSStephen Hemminger 
27750157f60cSPatrick McHardy 	if (tb[IFLA_BROADCAST]) {
27760157f60cSPatrick McHardy 		nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
2777e7c3273eSJiri Pirko 		call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
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 
2787*a4abfa62SPhil Sutter 	if (ifm->ifi_flags || ifm->ifi_change) {
2788*a4abfa62SPhil Sutter 		err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
2789*a4abfa62SPhil Sutter 				       extack);
2790*a4abfa62SPhil Sutter 		if (err < 0)
2791*a4abfa62SPhil Sutter 			goto errout;
2792*a4abfa62SPhil Sutter 	}
2793*a4abfa62SPhil Sutter 
27949a57247fSJiri Pirko 	if (tb[IFLA_CARRIER]) {
27959a57247fSJiri Pirko 		err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
27969a57247fSJiri Pirko 		if (err)
27979a57247fSJiri Pirko 			goto errout;
279890c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
27999a57247fSJiri Pirko 	}
28009a57247fSJiri Pirko 
28015d1180fcSNicolas Dichtel 	if (tb[IFLA_TXQLEN]) {
28020cd29503SAlexey Dobriyan 		unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]);
28035d1180fcSNicolas Dichtel 
28046a643ddbSCong Wang 		err = dev_change_tx_queue_len(dev, value);
28056a643ddbSCong Wang 		if (err)
280608294a26SJason Wang 			goto errout;
28072d7f669bSXin Long 		status |= DO_SETLINK_MODIFIED;
280808294a26SJason Wang 	}
28090157f60cSPatrick McHardy 
281046e6b992SStephen Hemminger 	if (tb[IFLA_GSO_MAX_SIZE]) {
281146e6b992SStephen Hemminger 		u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]);
281246e6b992SStephen Hemminger 
28137c4e983cSAlexander Duyck 		if (max_size > dev->tso_max_size) {
281446e6b992SStephen Hemminger 			err = -EINVAL;
281546e6b992SStephen Hemminger 			goto errout;
281646e6b992SStephen Hemminger 		}
281746e6b992SStephen Hemminger 
281846e6b992SStephen Hemminger 		if (dev->gso_max_size ^ max_size) {
281946e6b992SStephen Hemminger 			netif_set_gso_max_size(dev, max_size);
282046e6b992SStephen Hemminger 			status |= DO_SETLINK_MODIFIED;
282146e6b992SStephen Hemminger 		}
282246e6b992SStephen Hemminger 	}
282346e6b992SStephen Hemminger 
282446e6b992SStephen Hemminger 	if (tb[IFLA_GSO_MAX_SEGS]) {
282546e6b992SStephen Hemminger 		u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
282646e6b992SStephen Hemminger 
282714d7b812SJakub Kicinski 		if (max_segs > GSO_MAX_SEGS || max_segs > dev->tso_max_segs) {
282846e6b992SStephen Hemminger 			err = -EINVAL;
282946e6b992SStephen Hemminger 			goto errout;
283046e6b992SStephen Hemminger 		}
283146e6b992SStephen Hemminger 
283246e6b992SStephen Hemminger 		if (dev->gso_max_segs ^ max_segs) {
28336d872df3SEric Dumazet 			netif_set_gso_max_segs(dev, max_segs);
283446e6b992SStephen Hemminger 			status |= DO_SETLINK_MODIFIED;
283546e6b992SStephen Hemminger 		}
283646e6b992SStephen Hemminger 	}
283746e6b992SStephen Hemminger 
2838eac1b93cSCoco Li 	if (tb[IFLA_GRO_MAX_SIZE]) {
2839eac1b93cSCoco Li 		u32 gro_max_size = nla_get_u32(tb[IFLA_GRO_MAX_SIZE]);
2840eac1b93cSCoco Li 
2841eac1b93cSCoco Li 		if (dev->gro_max_size ^ gro_max_size) {
2842eac1b93cSCoco Li 			netif_set_gro_max_size(dev, gro_max_size);
2843eac1b93cSCoco Li 			status |= DO_SETLINK_MODIFIED;
2844eac1b93cSCoco Li 		}
2845eac1b93cSCoco Li 	}
2846eac1b93cSCoco Li 
28470157f60cSPatrick McHardy 	if (tb[IFLA_OPERSTATE])
284893b2d4a2SDavid S. Miller 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
28490157f60cSPatrick McHardy 
28500157f60cSPatrick McHardy 	if (tb[IFLA_LINKMODE]) {
28511889b0e7SNicolas Dichtel 		unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
28521889b0e7SNicolas Dichtel 
2853fd888e85SSebastian Andrzej Siewior 		write_lock(&dev_base_lock);
28541889b0e7SNicolas Dichtel 		if (dev->link_mode ^ value)
2855ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
28561889b0e7SNicolas Dichtel 		dev->link_mode = value;
2857fd888e85SSebastian Andrzej Siewior 		write_unlock(&dev_base_lock);
28580157f60cSPatrick McHardy 	}
28590157f60cSPatrick McHardy 
2860c02db8c6SChris Wright 	if (tb[IFLA_VFINFO_LIST]) {
28614f7d2cdfSDaniel Borkmann 		struct nlattr *vfinfo[IFLA_VF_MAX + 1];
2862c02db8c6SChris Wright 		struct nlattr *attr;
2863c02db8c6SChris Wright 		int rem;
28644f7d2cdfSDaniel Borkmann 
2865c02db8c6SChris Wright 		nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
28664f7d2cdfSDaniel Borkmann 			if (nla_type(attr) != IFLA_VF_INFO ||
28674f7d2cdfSDaniel Borkmann 			    nla_len(attr) < NLA_HDRLEN) {
2868253683bbSDavid Howells 				err = -EINVAL;
2869c02db8c6SChris Wright 				goto errout;
2870253683bbSDavid Howells 			}
28718cb08174SJohannes Berg 			err = nla_parse_nested_deprecated(vfinfo, IFLA_VF_MAX,
28728cb08174SJohannes Berg 							  attr,
28738cb08174SJohannes Berg 							  ifla_vf_policy,
28748cb08174SJohannes Berg 							  NULL);
28754f7d2cdfSDaniel Borkmann 			if (err < 0)
28764f7d2cdfSDaniel Borkmann 				goto errout;
28774f7d2cdfSDaniel Borkmann 			err = do_setvfinfo(dev, vfinfo);
2878ebc08a6fSWilliams, Mitch A 			if (err < 0)
2879ebc08a6fSWilliams, Mitch A 				goto errout;
2880ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
2881ebc08a6fSWilliams, Mitch A 		}
2882ebc08a6fSWilliams, Mitch A 	}
28830157f60cSPatrick McHardy 	err = 0;
28840157f60cSPatrick McHardy 
288557b61080SScott Feldman 	if (tb[IFLA_VF_PORTS]) {
288657b61080SScott Feldman 		struct nlattr *port[IFLA_PORT_MAX+1];
288757b61080SScott Feldman 		struct nlattr *attr;
288857b61080SScott Feldman 		int vf;
288957b61080SScott Feldman 		int rem;
289057b61080SScott Feldman 
289157b61080SScott Feldman 		err = -EOPNOTSUPP;
289257b61080SScott Feldman 		if (!ops->ndo_set_vf_port)
289357b61080SScott Feldman 			goto errout;
289457b61080SScott Feldman 
289557b61080SScott Feldman 		nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
2896035d210fSDaniel Borkmann 			if (nla_type(attr) != IFLA_VF_PORT ||
2897035d210fSDaniel Borkmann 			    nla_len(attr) < NLA_HDRLEN) {
2898035d210fSDaniel Borkmann 				err = -EINVAL;
2899035d210fSDaniel Borkmann 				goto errout;
2900035d210fSDaniel Borkmann 			}
29018cb08174SJohannes Berg 			err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
29028cb08174SJohannes Berg 							  attr,
29038cb08174SJohannes Berg 							  ifla_port_policy,
29048cb08174SJohannes Berg 							  NULL);
290557b61080SScott Feldman 			if (err < 0)
290657b61080SScott Feldman 				goto errout;
290757b61080SScott Feldman 			if (!port[IFLA_PORT_VF]) {
290857b61080SScott Feldman 				err = -EOPNOTSUPP;
290957b61080SScott Feldman 				goto errout;
291057b61080SScott Feldman 			}
291157b61080SScott Feldman 			vf = nla_get_u32(port[IFLA_PORT_VF]);
291257b61080SScott Feldman 			err = ops->ndo_set_vf_port(dev, vf, port);
291357b61080SScott Feldman 			if (err < 0)
291457b61080SScott Feldman 				goto errout;
2915ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
291657b61080SScott Feldman 		}
291757b61080SScott Feldman 	}
291857b61080SScott Feldman 	err = 0;
291957b61080SScott Feldman 
292057b61080SScott Feldman 	if (tb[IFLA_PORT_SELF]) {
292157b61080SScott Feldman 		struct nlattr *port[IFLA_PORT_MAX+1];
292257b61080SScott Feldman 
29238cb08174SJohannes Berg 		err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
29248cb08174SJohannes Berg 						  tb[IFLA_PORT_SELF],
29258cb08174SJohannes Berg 						  ifla_port_policy, NULL);
292657b61080SScott Feldman 		if (err < 0)
292757b61080SScott Feldman 			goto errout;
292857b61080SScott Feldman 
292957b61080SScott Feldman 		err = -EOPNOTSUPP;
293057b61080SScott Feldman 		if (ops->ndo_set_vf_port)
293157b61080SScott Feldman 			err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
293257b61080SScott Feldman 		if (err < 0)
293357b61080SScott Feldman 			goto errout;
2934ba998906SNicolas Dichtel 		status |= DO_SETLINK_NOTIFY;
293557b61080SScott Feldman 	}
2936f8ff182cSThomas Graf 
2937f8ff182cSThomas Graf 	if (tb[IFLA_AF_SPEC]) {
2938f8ff182cSThomas Graf 		struct nlattr *af;
2939f8ff182cSThomas Graf 		int rem;
2940f8ff182cSThomas Graf 
2941f8ff182cSThomas Graf 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2942f8ff182cSThomas Graf 			const struct rtnl_af_ops *af_ops;
2943f8ff182cSThomas Graf 
2944058c8d59SGustavo A. R. Silva 			BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af))));
2945f8ff182cSThomas Graf 
29463583a4e8SStephen Hemminger 			err = af_ops->set_link_af(dev, af, extack);
2947a100243dSCong Wang 			if (err < 0)
2948f8ff182cSThomas Graf 				goto errout;
2949f8ff182cSThomas Graf 
2950ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
2951f8ff182cSThomas Graf 		}
2952f8ff182cSThomas Graf 	}
295357b61080SScott Feldman 	err = 0;
295457b61080SScott Feldman 
2955829eb208SRoopa Prabhu 	if (tb[IFLA_PROTO_DOWN] || tb[IFLA_PROTO_DOWN_REASON]) {
2956829eb208SRoopa Prabhu 		err = do_set_proto_down(dev, tb[IFLA_PROTO_DOWN],
2957829eb208SRoopa Prabhu 					tb[IFLA_PROTO_DOWN_REASON], extack);
295888d6378bSAnuradha Karuppiah 		if (err)
295988d6378bSAnuradha Karuppiah 			goto errout;
296088d6378bSAnuradha Karuppiah 		status |= DO_SETLINK_NOTIFY;
296188d6378bSAnuradha Karuppiah 	}
296288d6378bSAnuradha Karuppiah 
2963d1fdd913SBrenden Blanco 	if (tb[IFLA_XDP]) {
2964d1fdd913SBrenden Blanco 		struct nlattr *xdp[IFLA_XDP_MAX + 1];
296585de8576SDaniel Borkmann 		u32 xdp_flags = 0;
2966d1fdd913SBrenden Blanco 
29678cb08174SJohannes Berg 		err = nla_parse_nested_deprecated(xdp, IFLA_XDP_MAX,
29688cb08174SJohannes Berg 						  tb[IFLA_XDP],
2969fceb6435SJohannes Berg 						  ifla_xdp_policy, NULL);
2970d1fdd913SBrenden Blanco 		if (err < 0)
2971d1fdd913SBrenden Blanco 			goto errout;
2972d1fdd913SBrenden Blanco 
297358038695SMartin KaFai Lau 		if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
2974262d8625SBrenden Blanco 			err = -EINVAL;
2975262d8625SBrenden Blanco 			goto errout;
2976262d8625SBrenden Blanco 		}
297785de8576SDaniel Borkmann 
297885de8576SDaniel Borkmann 		if (xdp[IFLA_XDP_FLAGS]) {
297985de8576SDaniel Borkmann 			xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
298085de8576SDaniel Borkmann 			if (xdp_flags & ~XDP_FLAGS_MASK) {
298185de8576SDaniel Borkmann 				err = -EINVAL;
298285de8576SDaniel Borkmann 				goto errout;
298385de8576SDaniel Borkmann 			}
2984ee5d032fSJakub Kicinski 			if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) {
29850489df9aSDaniel Borkmann 				err = -EINVAL;
29860489df9aSDaniel Borkmann 				goto errout;
29870489df9aSDaniel Borkmann 			}
298885de8576SDaniel Borkmann 		}
298985de8576SDaniel Borkmann 
2990d1fdd913SBrenden Blanco 		if (xdp[IFLA_XDP_FD]) {
299192234c8fSToke Høiland-Jørgensen 			int expected_fd = -1;
299292234c8fSToke Høiland-Jørgensen 
299392234c8fSToke Høiland-Jørgensen 			if (xdp_flags & XDP_FLAGS_REPLACE) {
299492234c8fSToke Høiland-Jørgensen 				if (!xdp[IFLA_XDP_EXPECTED_FD]) {
299592234c8fSToke Høiland-Jørgensen 					err = -EINVAL;
299692234c8fSToke Høiland-Jørgensen 					goto errout;
299792234c8fSToke Høiland-Jørgensen 				}
299892234c8fSToke Høiland-Jørgensen 				expected_fd =
299992234c8fSToke Høiland-Jørgensen 					nla_get_s32(xdp[IFLA_XDP_EXPECTED_FD]);
300092234c8fSToke Høiland-Jørgensen 			}
300192234c8fSToke Høiland-Jørgensen 
3002ddf9f970SJakub Kicinski 			err = dev_change_xdp_fd(dev, extack,
300385de8576SDaniel Borkmann 						nla_get_s32(xdp[IFLA_XDP_FD]),
300492234c8fSToke Høiland-Jørgensen 						expected_fd,
300585de8576SDaniel Borkmann 						xdp_flags);
3006d1fdd913SBrenden Blanco 			if (err)
3007d1fdd913SBrenden Blanco 				goto errout;
3008d1fdd913SBrenden Blanco 			status |= DO_SETLINK_NOTIFY;
3009d1fdd913SBrenden Blanco 		}
3010d1fdd913SBrenden Blanco 	}
3011d1fdd913SBrenden Blanco 
30120157f60cSPatrick McHardy errout:
3013ba998906SNicolas Dichtel 	if (status & DO_SETLINK_MODIFIED) {
301464ff90ccSXin Long 		if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY)
3015ba998906SNicolas Dichtel 			netdev_state_change(dev);
3016ba998906SNicolas Dichtel 
3017ba998906SNicolas Dichtel 		if (err < 0)
3018e87cc472SJoe 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",
3019e87cc472SJoe Perches 					     dev->name);
3020ba998906SNicolas Dichtel 	}
30210157f60cSPatrick McHardy 
30220157f60cSPatrick McHardy 	return err;
30230157f60cSPatrick McHardy }
30240157f60cSPatrick McHardy 
3025cc6090e9SJiri Pirko static struct net_device *rtnl_dev_get(struct net *net,
30265ea08b52SFlorent Fourcot 				       struct nlattr *tb[])
3027cc6090e9SJiri Pirko {
30285ea08b52SFlorent Fourcot 	char ifname[ALTIFNAMSIZ];
3029cc6090e9SJiri Pirko 
30305ea08b52SFlorent Fourcot 	if (tb[IFLA_IFNAME])
30315ea08b52SFlorent Fourcot 		nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
30325ea08b52SFlorent Fourcot 	else if (tb[IFLA_ALT_IFNAME])
30335ea08b52SFlorent Fourcot 		nla_strscpy(ifname, tb[IFLA_ALT_IFNAME], ALTIFNAMSIZ);
3034cc6090e9SJiri Pirko 	else
3035cc6090e9SJiri Pirko 		return NULL;
3036cc6090e9SJiri Pirko 
3037cc6090e9SJiri Pirko 	return __dev_get_by_name(net, ifname);
3038cc6090e9SJiri Pirko }
3039cc6090e9SJiri Pirko 
3040c21ef3e3SDavid Ahern static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3041c21ef3e3SDavid Ahern 			struct netlink_ext_ack *extack)
3042da5e0494SThomas Graf {
30433b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
3044da5e0494SThomas Graf 	struct ifinfomsg *ifm;
3045da5e0494SThomas Graf 	struct net_device *dev;
30460157f60cSPatrick McHardy 	int err;
3047da5e0494SThomas Graf 	struct nlattr *tb[IFLA_MAX+1];
30481da177e4SLinus Torvalds 
30498cb08174SJohannes Berg 	err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
30508cb08174SJohannes Berg 				     ifla_policy, extack);
3051da5e0494SThomas Graf 	if (err < 0)
3052da5e0494SThomas Graf 		goto errout;
30531da177e4SLinus Torvalds 
30544ff66caeSChristian Brauner 	err = rtnl_ensure_unique_netns(tb, extack, false);
30554ff66caeSChristian Brauner 	if (err < 0)
30564ff66caeSChristian Brauner 		goto errout;
30574ff66caeSChristian Brauner 
30581da177e4SLinus Torvalds 	err = -EINVAL;
3059da5e0494SThomas Graf 	ifm = nlmsg_data(nlh);
306051055be8SPatrick McHardy 	if (ifm->ifi_index > 0)
3061a3d12891SEric Dumazet 		dev = __dev_get_by_index(net, ifm->ifi_index);
306276c9ac0eSJiri Pirko 	else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
30635ea08b52SFlorent Fourcot 		dev = rtnl_dev_get(net, tb);
3064da5e0494SThomas Graf 	else
3065da5e0494SThomas Graf 		goto errout;
30661da177e4SLinus Torvalds 
3067da5e0494SThomas Graf 	if (dev == NULL) {
3068da5e0494SThomas Graf 		err = -ENODEV;
3069da5e0494SThomas Graf 		goto errout;
3070da5e0494SThomas Graf 	}
30711da177e4SLinus Torvalds 
30725ea08b52SFlorent Fourcot 	err = do_setlink(skb, dev, ifm, extack, tb, 0);
3073da5e0494SThomas Graf errout:
30741da177e4SLinus Torvalds 	return err;
30751da177e4SLinus Torvalds }
30761da177e4SLinus Torvalds 
307766400d54SWANG Cong static int rtnl_group_dellink(const struct net *net, int group)
307866400d54SWANG Cong {
307966400d54SWANG Cong 	struct net_device *dev, *aux;
308066400d54SWANG Cong 	LIST_HEAD(list_kill);
308166400d54SWANG Cong 	bool found = false;
308266400d54SWANG Cong 
308366400d54SWANG Cong 	if (!group)
308466400d54SWANG Cong 		return -EPERM;
308566400d54SWANG Cong 
308666400d54SWANG Cong 	for_each_netdev(net, dev) {
308766400d54SWANG Cong 		if (dev->group == group) {
308866400d54SWANG Cong 			const struct rtnl_link_ops *ops;
308966400d54SWANG Cong 
309066400d54SWANG Cong 			found = true;
309166400d54SWANG Cong 			ops = dev->rtnl_link_ops;
309266400d54SWANG Cong 			if (!ops || !ops->dellink)
309366400d54SWANG Cong 				return -EOPNOTSUPP;
309466400d54SWANG Cong 		}
309566400d54SWANG Cong 	}
309666400d54SWANG Cong 
309766400d54SWANG Cong 	if (!found)
309866400d54SWANG Cong 		return -ENODEV;
309966400d54SWANG Cong 
310066400d54SWANG Cong 	for_each_netdev_safe(net, dev, aux) {
310166400d54SWANG Cong 		if (dev->group == group) {
310266400d54SWANG Cong 			const struct rtnl_link_ops *ops;
310366400d54SWANG Cong 
310466400d54SWANG Cong 			ops = dev->rtnl_link_ops;
310566400d54SWANG Cong 			ops->dellink(dev, &list_kill);
310666400d54SWANG Cong 		}
310766400d54SWANG Cong 	}
310866400d54SWANG Cong 	unregister_netdevice_many(&list_kill);
310966400d54SWANG Cong 
311066400d54SWANG Cong 	return 0;
311166400d54SWANG Cong }
311266400d54SWANG Cong 
3113614732eaSThomas Graf int rtnl_delete_link(struct net_device *dev)
3114614732eaSThomas Graf {
3115614732eaSThomas Graf 	const struct rtnl_link_ops *ops;
3116614732eaSThomas Graf 	LIST_HEAD(list_kill);
3117614732eaSThomas Graf 
3118614732eaSThomas Graf 	ops = dev->rtnl_link_ops;
3119614732eaSThomas Graf 	if (!ops || !ops->dellink)
3120614732eaSThomas Graf 		return -EOPNOTSUPP;
3121614732eaSThomas Graf 
3122614732eaSThomas Graf 	ops->dellink(dev, &list_kill);
3123614732eaSThomas Graf 	unregister_netdevice_many(&list_kill);
3124614732eaSThomas Graf 
3125614732eaSThomas Graf 	return 0;
3126614732eaSThomas Graf }
3127614732eaSThomas Graf EXPORT_SYMBOL_GPL(rtnl_delete_link);
3128614732eaSThomas Graf 
3129c21ef3e3SDavid Ahern static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
3130c21ef3e3SDavid Ahern 			struct netlink_ext_ack *extack)
313138f7b870SPatrick McHardy {
31323b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
3133b61ad68aSChristian Brauner 	struct net *tgt_net = net;
3134b61ad68aSChristian Brauner 	struct net_device *dev = NULL;
313538f7b870SPatrick McHardy 	struct ifinfomsg *ifm;
313638f7b870SPatrick McHardy 	struct nlattr *tb[IFLA_MAX+1];
313738f7b870SPatrick McHardy 	int err;
3138b61ad68aSChristian Brauner 	int netnsid = -1;
313938f7b870SPatrick McHardy 
31408cb08174SJohannes Berg 	err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
31418cb08174SJohannes Berg 				     ifla_policy, extack);
314238f7b870SPatrick McHardy 	if (err < 0)
314338f7b870SPatrick McHardy 		return err;
314438f7b870SPatrick McHardy 
31454ff66caeSChristian Brauner 	err = rtnl_ensure_unique_netns(tb, extack, true);
31464ff66caeSChristian Brauner 	if (err < 0)
31474ff66caeSChristian Brauner 		return err;
31484ff66caeSChristian Brauner 
31497e4a8d5aSChristian Brauner 	if (tb[IFLA_TARGET_NETNSID]) {
31507e4a8d5aSChristian Brauner 		netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
3151c383edc4SChristian Brauner 		tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
3152b61ad68aSChristian Brauner 		if (IS_ERR(tgt_net))
3153b61ad68aSChristian Brauner 			return PTR_ERR(tgt_net);
3154b61ad68aSChristian Brauner 	}
3155b61ad68aSChristian Brauner 
3156b61ad68aSChristian Brauner 	err = -EINVAL;
315738f7b870SPatrick McHardy 	ifm = nlmsg_data(nlh);
315838f7b870SPatrick McHardy 	if (ifm->ifi_index > 0)
3159b61ad68aSChristian Brauner 		dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
316076c9ac0eSJiri Pirko 	else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
31615ea08b52SFlorent Fourcot 		dev = rtnl_dev_get(net, tb);
316266400d54SWANG Cong 	else if (tb[IFLA_GROUP])
3163b61ad68aSChristian Brauner 		err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP]));
316438f7b870SPatrick McHardy 	else
3165b61ad68aSChristian Brauner 		goto out;
316638f7b870SPatrick McHardy 
3167b61ad68aSChristian Brauner 	if (!dev) {
3168dee04163SFlorent Fourcot 		if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME] || ifm->ifi_index > 0)
3169b61ad68aSChristian Brauner 			err = -ENODEV;
317038f7b870SPatrick McHardy 
3171b61ad68aSChristian Brauner 		goto out;
3172b61ad68aSChristian Brauner 	}
3173b61ad68aSChristian Brauner 
3174b61ad68aSChristian Brauner 	err = rtnl_delete_link(dev);
3175b61ad68aSChristian Brauner 
3176b61ad68aSChristian Brauner out:
3177b61ad68aSChristian Brauner 	if (netnsid >= 0)
3178b61ad68aSChristian Brauner 		put_net(tgt_net);
3179b61ad68aSChristian Brauner 
3180b61ad68aSChristian Brauner 	return err;
318138f7b870SPatrick McHardy }
318238f7b870SPatrick McHardy 
31833729d502SPatrick McHardy int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
31843729d502SPatrick McHardy {
31853729d502SPatrick McHardy 	unsigned int old_flags;
31863729d502SPatrick McHardy 	int err;
31873729d502SPatrick McHardy 
31883729d502SPatrick McHardy 	old_flags = dev->flags;
31893729d502SPatrick McHardy 	if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
31906d040321SPetr Machata 		err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
31916d040321SPetr Machata 					 NULL);
31923729d502SPatrick McHardy 		if (err < 0)
31933729d502SPatrick McHardy 			return err;
31943729d502SPatrick McHardy 	}
31953729d502SPatrick McHardy 
31968d356b89SRoopa Prabhu 	if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) {
319756a49d70SRoopa Prabhu 		__dev_notify_flags(dev, old_flags, (old_flags ^ dev->flags));
31988d356b89SRoopa Prabhu 	} else {
31993729d502SPatrick McHardy 		dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
3200a528c219SNicolas Dichtel 		__dev_notify_flags(dev, old_flags, ~0U);
32018d356b89SRoopa Prabhu 	}
32023729d502SPatrick McHardy 	return 0;
32033729d502SPatrick McHardy }
32043729d502SPatrick McHardy EXPORT_SYMBOL(rtnl_configure_link);
32053729d502SPatrick McHardy 
3206d0522f1cSDavid Ahern struct net_device *rtnl_create_link(struct net *net, const char *ifname,
3207d0522f1cSDavid Ahern 				    unsigned char name_assign_type,
3208d0522f1cSDavid Ahern 				    const struct rtnl_link_ops *ops,
3209d0522f1cSDavid Ahern 				    struct nlattr *tb[],
3210d0522f1cSDavid Ahern 				    struct netlink_ext_ack *extack)
3211e7199288SPavel Emelianov {
3212e7199288SPavel Emelianov 	struct net_device *dev;
3213d40156aaSJiri Pirko 	unsigned int num_tx_queues = 1;
3214d40156aaSJiri Pirko 	unsigned int num_rx_queues = 1;
3215e7199288SPavel Emelianov 
321676ff5cc9SJiri Pirko 	if (tb[IFLA_NUM_TX_QUEUES])
321776ff5cc9SJiri Pirko 		num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
321876ff5cc9SJiri Pirko 	else if (ops->get_num_tx_queues)
3219d40156aaSJiri Pirko 		num_tx_queues = ops->get_num_tx_queues();
322076ff5cc9SJiri Pirko 
322176ff5cc9SJiri Pirko 	if (tb[IFLA_NUM_RX_QUEUES])
322276ff5cc9SJiri Pirko 		num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
322376ff5cc9SJiri Pirko 	else if (ops->get_num_rx_queues)
3224d40156aaSJiri Pirko 		num_rx_queues = ops->get_num_rx_queues();
3225efacb309Sstephen hemminger 
3226d0522f1cSDavid Ahern 	if (num_tx_queues < 1 || num_tx_queues > 4096) {
3227d0522f1cSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid number of transmit queues");
32280e1d6ecaSEric Dumazet 		return ERR_PTR(-EINVAL);
3229d0522f1cSDavid Ahern 	}
32300e1d6ecaSEric Dumazet 
3231d0522f1cSDavid Ahern 	if (num_rx_queues < 1 || num_rx_queues > 4096) {
3232d0522f1cSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid number of receive queues");
32330e1d6ecaSEric Dumazet 		return ERR_PTR(-EINVAL);
3234d0522f1cSDavid Ahern 	}
32350e1d6ecaSEric Dumazet 
32368c713dc9SJohannes Berg 	if (ops->alloc) {
32378c713dc9SJohannes Berg 		dev = ops->alloc(tb, ifname, name_assign_type,
32388c713dc9SJohannes Berg 				 num_tx_queues, num_rx_queues);
32398c713dc9SJohannes Berg 		if (IS_ERR(dev))
32408c713dc9SJohannes Berg 			return dev;
32418c713dc9SJohannes Berg 	} else {
32428c713dc9SJohannes Berg 		dev = alloc_netdev_mqs(ops->priv_size, ifname,
32438c713dc9SJohannes Berg 				       name_assign_type, ops->setup,
32448c713dc9SJohannes Berg 				       num_tx_queues, num_rx_queues);
32458c713dc9SJohannes Berg 	}
32468c713dc9SJohannes Berg 
3247e7199288SPavel Emelianov 	if (!dev)
3248d1892e4eSTobias Klauser 		return ERR_PTR(-ENOMEM);
3249e7199288SPavel Emelianov 
325081adee47SEric W. Biederman 	dev_net_set(dev, net);
325181adee47SEric W. Biederman 	dev->rtnl_link_ops = ops;
32523729d502SPatrick McHardy 	dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
325381adee47SEric W. Biederman 
3254d836f5c6SEric Dumazet 	if (tb[IFLA_MTU]) {
3255d836f5c6SEric Dumazet 		u32 mtu = nla_get_u32(tb[IFLA_MTU]);
3256d836f5c6SEric Dumazet 		int err;
3257d836f5c6SEric Dumazet 
3258d836f5c6SEric Dumazet 		err = dev_validate_mtu(dev, mtu, extack);
3259d836f5c6SEric Dumazet 		if (err) {
3260d836f5c6SEric Dumazet 			free_netdev(dev);
3261d836f5c6SEric Dumazet 			return ERR_PTR(err);
3262d836f5c6SEric Dumazet 		}
3263d836f5c6SEric Dumazet 		dev->mtu = mtu;
3264d836f5c6SEric Dumazet 	}
32652afb9b53SJiri Pirko 	if (tb[IFLA_ADDRESS]) {
3266efd38f75SJakub Kicinski 		__dev_addr_set(dev, nla_data(tb[IFLA_ADDRESS]),
3267e7199288SPavel Emelianov 			       nla_len(tb[IFLA_ADDRESS]));
32682afb9b53SJiri Pirko 		dev->addr_assign_type = NET_ADDR_SET;
32692afb9b53SJiri Pirko 	}
3270e7199288SPavel Emelianov 	if (tb[IFLA_BROADCAST])
3271e7199288SPavel Emelianov 		memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
3272e7199288SPavel Emelianov 				nla_len(tb[IFLA_BROADCAST]));
3273e7199288SPavel Emelianov 	if (tb[IFLA_TXQLEN])
3274e7199288SPavel Emelianov 		dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
3275e7199288SPavel Emelianov 	if (tb[IFLA_OPERSTATE])
327693b2d4a2SDavid S. Miller 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
3277e7199288SPavel Emelianov 	if (tb[IFLA_LINKMODE])
3278e7199288SPavel Emelianov 		dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
3279ffa934f1SPatrick McHardy 	if (tb[IFLA_GROUP])
3280ffa934f1SPatrick McHardy 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
328146e6b992SStephen Hemminger 	if (tb[IFLA_GSO_MAX_SIZE])
328246e6b992SStephen Hemminger 		netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE]));
328346e6b992SStephen Hemminger 	if (tb[IFLA_GSO_MAX_SEGS])
32846d872df3SEric Dumazet 		netif_set_gso_max_segs(dev, nla_get_u32(tb[IFLA_GSO_MAX_SEGS]));
3285eac1b93cSCoco Li 	if (tb[IFLA_GRO_MAX_SIZE])
3286eac1b93cSCoco Li 		netif_set_gro_max_size(dev, nla_get_u32(tb[IFLA_GRO_MAX_SIZE]));
3287e7199288SPavel Emelianov 
3288e7199288SPavel Emelianov 	return dev;
3289e7199288SPavel Emelianov }
3290e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_create_link);
3291e7199288SPavel Emelianov 
329290f62cf3SEric W. Biederman static int rtnl_group_changelink(const struct sk_buff *skb,
329390f62cf3SEric W. Biederman 		struct net *net, int group,
3294e7ed828fSVlad Dogaru 		struct ifinfomsg *ifm,
3295ddf9f970SJakub Kicinski 		struct netlink_ext_ack *extack,
3296e7ed828fSVlad Dogaru 		struct nlattr **tb)
3297e7ed828fSVlad Dogaru {
3298d079535dSWANG Cong 	struct net_device *dev, *aux;
3299e7ed828fSVlad Dogaru 	int err;
3300e7ed828fSVlad Dogaru 
3301d079535dSWANG Cong 	for_each_netdev_safe(net, dev, aux) {
3302e7ed828fSVlad Dogaru 		if (dev->group == group) {
33035ea08b52SFlorent Fourcot 			err = do_setlink(skb, dev, ifm, extack, tb, 0);
3304e7ed828fSVlad Dogaru 			if (err < 0)
3305e7ed828fSVlad Dogaru 				return err;
3306e7ed828fSVlad Dogaru 		}
3307e7ed828fSVlad Dogaru 	}
3308e7ed828fSVlad Dogaru 
3309e7ed828fSVlad Dogaru 	return 0;
3310e7ed828fSVlad Dogaru }
3311e7ed828fSVlad Dogaru 
331263105e83SJakub Kicinski static int rtnl_newlink_create(struct sk_buff *skb, struct ifinfomsg *ifm,
331363105e83SJakub Kicinski 			       const struct rtnl_link_ops *ops,
331463105e83SJakub Kicinski 			       struct nlattr **tb, struct nlattr **data,
331502839cc8SJakub Kicinski 			       struct netlink_ext_ack *extack)
331602839cc8SJakub Kicinski {
331702839cc8SJakub Kicinski 	unsigned char name_assign_type = NET_NAME_USER;
331802839cc8SJakub Kicinski 	struct net *net = sock_net(skb->sk);
331902839cc8SJakub Kicinski 	struct net *dest_net, *link_net;
332002839cc8SJakub Kicinski 	struct net_device *dev;
332102839cc8SJakub Kicinski 	char ifname[IFNAMSIZ];
332202839cc8SJakub Kicinski 	int err;
332302839cc8SJakub Kicinski 
332402839cc8SJakub Kicinski 	if (!ops->alloc && !ops->setup)
332502839cc8SJakub Kicinski 		return -EOPNOTSUPP;
332602839cc8SJakub Kicinski 
332702839cc8SJakub Kicinski 	if (tb[IFLA_IFNAME]) {
332802839cc8SJakub Kicinski 		nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
332902839cc8SJakub Kicinski 	} else {
333002839cc8SJakub Kicinski 		snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
333102839cc8SJakub Kicinski 		name_assign_type = NET_NAME_ENUM;
333202839cc8SJakub Kicinski 	}
333302839cc8SJakub Kicinski 
333402839cc8SJakub Kicinski 	dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN);
333502839cc8SJakub Kicinski 	if (IS_ERR(dest_net))
333602839cc8SJakub Kicinski 		return PTR_ERR(dest_net);
333702839cc8SJakub Kicinski 
333802839cc8SJakub Kicinski 	if (tb[IFLA_LINK_NETNSID]) {
333902839cc8SJakub Kicinski 		int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
334002839cc8SJakub Kicinski 
334102839cc8SJakub Kicinski 		link_net = get_net_ns_by_id(dest_net, id);
334202839cc8SJakub Kicinski 		if (!link_net) {
334302839cc8SJakub Kicinski 			NL_SET_ERR_MSG(extack, "Unknown network namespace id");
334402839cc8SJakub Kicinski 			err =  -EINVAL;
334502839cc8SJakub Kicinski 			goto out;
334602839cc8SJakub Kicinski 		}
334702839cc8SJakub Kicinski 		err = -EPERM;
334802839cc8SJakub Kicinski 		if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
334902839cc8SJakub Kicinski 			goto out;
335002839cc8SJakub Kicinski 	} else {
335102839cc8SJakub Kicinski 		link_net = NULL;
335202839cc8SJakub Kicinski 	}
335302839cc8SJakub Kicinski 
335402839cc8SJakub Kicinski 	dev = rtnl_create_link(link_net ? : dest_net, ifname,
335502839cc8SJakub Kicinski 			       name_assign_type, ops, tb, extack);
335602839cc8SJakub Kicinski 	if (IS_ERR(dev)) {
335702839cc8SJakub Kicinski 		err = PTR_ERR(dev);
335802839cc8SJakub Kicinski 		goto out;
335902839cc8SJakub Kicinski 	}
336002839cc8SJakub Kicinski 
336102839cc8SJakub Kicinski 	dev->ifindex = ifm->ifi_index;
336202839cc8SJakub Kicinski 
336302839cc8SJakub Kicinski 	if (ops->newlink)
336402839cc8SJakub Kicinski 		err = ops->newlink(link_net ? : net, dev, tb, data, extack);
336502839cc8SJakub Kicinski 	else
336602839cc8SJakub Kicinski 		err = register_netdevice(dev);
336702839cc8SJakub Kicinski 	if (err < 0) {
336802839cc8SJakub Kicinski 		free_netdev(dev);
336902839cc8SJakub Kicinski 		goto out;
337002839cc8SJakub Kicinski 	}
337102839cc8SJakub Kicinski 
337202839cc8SJakub Kicinski 	err = rtnl_configure_link(dev, ifm);
337302839cc8SJakub Kicinski 	if (err < 0)
337402839cc8SJakub Kicinski 		goto out_unregister;
337502839cc8SJakub Kicinski 	if (link_net) {
337602839cc8SJakub Kicinski 		err = dev_change_net_namespace(dev, dest_net, ifname);
337702839cc8SJakub Kicinski 		if (err < 0)
337802839cc8SJakub Kicinski 			goto out_unregister;
337902839cc8SJakub Kicinski 	}
338002839cc8SJakub Kicinski 	if (tb[IFLA_MASTER]) {
338102839cc8SJakub Kicinski 		err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
338202839cc8SJakub Kicinski 		if (err)
338302839cc8SJakub Kicinski 			goto out_unregister;
338402839cc8SJakub Kicinski 	}
338502839cc8SJakub Kicinski out:
338602839cc8SJakub Kicinski 	if (link_net)
338702839cc8SJakub Kicinski 		put_net(link_net);
338802839cc8SJakub Kicinski 	put_net(dest_net);
338902839cc8SJakub Kicinski 	return err;
339002839cc8SJakub Kicinski out_unregister:
339102839cc8SJakub Kicinski 	if (ops->newlink) {
339202839cc8SJakub Kicinski 		LIST_HEAD(list_kill);
339302839cc8SJakub Kicinski 
339402839cc8SJakub Kicinski 		ops->dellink(dev, &list_kill);
339502839cc8SJakub Kicinski 		unregister_netdevice_many(&list_kill);
339602839cc8SJakub Kicinski 	} else {
339702839cc8SJakub Kicinski 		unregister_netdevice(dev);
339802839cc8SJakub Kicinski 	}
339902839cc8SJakub Kicinski 	goto out;
340002839cc8SJakub Kicinski }
340163105e83SJakub Kicinski 
3402c92bf26cSJakub Kicinski struct rtnl_newlink_tbs {
3403c92bf26cSJakub Kicinski 	struct nlattr *tb[IFLA_MAX + 1];
3404c92bf26cSJakub Kicinski 	struct nlattr *attr[RTNL_MAX_TYPE + 1];
3405420d0318SJakub Kicinski 	struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1];
3406c92bf26cSJakub Kicinski };
3407c92bf26cSJakub Kicinski 
3408c92bf26cSJakub Kicinski static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3409c92bf26cSJakub Kicinski 			  struct rtnl_newlink_tbs *tbs,
3410c92bf26cSJakub Kicinski 			  struct netlink_ext_ack *extack)
3411c92bf26cSJakub Kicinski {
3412420d0318SJakub Kicinski 	struct nlattr *linkinfo[IFLA_INFO_MAX + 1];
3413c92bf26cSJakub Kicinski 	struct nlattr ** const tb = tbs->tb;
3414c6f6f244SEric Dumazet 	const struct rtnl_link_ops *m_ops;
3415c6f6f244SEric Dumazet 	struct net_device *master_dev;
34163b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
341738f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops;
3418420d0318SJakub Kicinski 	struct nlattr **slave_data;
3419420d0318SJakub Kicinski 	char kind[MODULE_NAME_LEN];
3420420d0318SJakub Kicinski 	struct net_device *dev;
3421420d0318SJakub Kicinski 	struct ifinfomsg *ifm;
3422420d0318SJakub Kicinski 	struct nlattr **data;
3423ef2a7c90SFlorent Fourcot 	bool link_specified;
342438f7b870SPatrick McHardy 	int err;
342538f7b870SPatrick McHardy 
342695a5afcaSJohannes Berg #ifdef CONFIG_MODULES
342738f7b870SPatrick McHardy replay:
34288072f085SThomas Graf #endif
34298cb08174SJohannes Berg 	err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
34308cb08174SJohannes Berg 				     ifla_policy, extack);
343138f7b870SPatrick McHardy 	if (err < 0)
343238f7b870SPatrick McHardy 		return err;
343338f7b870SPatrick McHardy 
34344ff66caeSChristian Brauner 	err = rtnl_ensure_unique_netns(tb, extack, false);
34354ff66caeSChristian Brauner 	if (err < 0)
34364ff66caeSChristian Brauner 		return err;
34374ff66caeSChristian Brauner 
343838f7b870SPatrick McHardy 	ifm = nlmsg_data(nlh);
3439ef2a7c90SFlorent Fourcot 	if (ifm->ifi_index > 0) {
3440ef2a7c90SFlorent Fourcot 		link_specified = true;
3441881d966bSEric W. Biederman 		dev = __dev_get_by_index(net, ifm->ifi_index);
3442ef2a7c90SFlorent Fourcot 	} else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) {
3443ef2a7c90SFlorent Fourcot 		link_specified = true;
34445ea08b52SFlorent Fourcot 		dev = rtnl_dev_get(net, tb);
3445ef2a7c90SFlorent Fourcot 	} else {
3446ef2a7c90SFlorent Fourcot 		link_specified = false;
344738f7b870SPatrick McHardy 		dev = NULL;
3448ef2a7c90SFlorent Fourcot 	}
344938f7b870SPatrick McHardy 
3450c6f6f244SEric Dumazet 	master_dev = NULL;
3451c6f6f244SEric Dumazet 	m_ops = NULL;
3452ba7d49b1SJiri Pirko 	if (dev) {
3453ba7d49b1SJiri Pirko 		master_dev = netdev_master_upper_dev_get(dev);
3454ba7d49b1SJiri Pirko 		if (master_dev)
3455ba7d49b1SJiri Pirko 			m_ops = master_dev->rtnl_link_ops;
3456ba7d49b1SJiri Pirko 	}
3457ba7d49b1SJiri Pirko 
34588679c31eSRocco Yue 	err = validate_linkmsg(dev, tb, extack);
3459e0d087afSEric Dumazet 	if (err < 0)
34601840bb13SThomas Graf 		return err;
34611840bb13SThomas Graf 
346238f7b870SPatrick McHardy 	if (tb[IFLA_LINKINFO]) {
34638cb08174SJohannes Berg 		err = nla_parse_nested_deprecated(linkinfo, IFLA_INFO_MAX,
34648cb08174SJohannes Berg 						  tb[IFLA_LINKINFO],
34658cb08174SJohannes Berg 						  ifla_info_policy, NULL);
346638f7b870SPatrick McHardy 		if (err < 0)
346738f7b870SPatrick McHardy 			return err;
346838f7b870SPatrick McHardy 	} else
346938f7b870SPatrick McHardy 		memset(linkinfo, 0, sizeof(linkinfo));
347038f7b870SPatrick McHardy 
347138f7b870SPatrick McHardy 	if (linkinfo[IFLA_INFO_KIND]) {
3472872f6903SFrancis Laniel 		nla_strscpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
347338f7b870SPatrick McHardy 		ops = rtnl_link_ops_get(kind);
347438f7b870SPatrick McHardy 	} else {
347538f7b870SPatrick McHardy 		kind[0] = '\0';
347638f7b870SPatrick McHardy 		ops = NULL;
347738f7b870SPatrick McHardy 	}
347838f7b870SPatrick McHardy 
3479420d0318SJakub Kicinski 	data = NULL;
348038f7b870SPatrick McHardy 	if (ops) {
3481ccf8dbcdSKees Cook 		if (ops->maxtype > RTNL_MAX_TYPE)
3482ccf8dbcdSKees Cook 			return -EINVAL;
3483ccf8dbcdSKees Cook 
348438f7b870SPatrick McHardy 		if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
3485c92bf26cSJakub Kicinski 			err = nla_parse_nested_deprecated(tbs->attr, ops->maxtype,
348638f7b870SPatrick McHardy 							  linkinfo[IFLA_INFO_DATA],
348768d57f3bSDavid Ahern 							  ops->policy, extack);
348838f7b870SPatrick McHardy 			if (err < 0)
348938f7b870SPatrick McHardy 				return err;
3490c92bf26cSJakub Kicinski 			data = tbs->attr;
349138f7b870SPatrick McHardy 		}
349238f7b870SPatrick McHardy 		if (ops->validate) {
3493a8b8a889SMatthias Schiffer 			err = ops->validate(tb, data, extack);
349438f7b870SPatrick McHardy 			if (err < 0)
349538f7b870SPatrick McHardy 				return err;
349638f7b870SPatrick McHardy 		}
349738f7b870SPatrick McHardy 	}
349838f7b870SPatrick McHardy 
3499420d0318SJakub Kicinski 	slave_data = NULL;
3500ba7d49b1SJiri Pirko 	if (m_ops) {
3501af066ed3SPrashant Bhole 		if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE)
3502ccf8dbcdSKees Cook 			return -EINVAL;
3503ccf8dbcdSKees Cook 
3504ba7d49b1SJiri Pirko 		if (m_ops->slave_maxtype &&
3505ba7d49b1SJiri Pirko 		    linkinfo[IFLA_INFO_SLAVE_DATA]) {
3506c92bf26cSJakub Kicinski 			err = nla_parse_nested_deprecated(tbs->slave_attr,
35078cb08174SJohannes Berg 							  m_ops->slave_maxtype,
3508ba7d49b1SJiri Pirko 							  linkinfo[IFLA_INFO_SLAVE_DATA],
35098cb08174SJohannes Berg 							  m_ops->slave_policy,
35108cb08174SJohannes Berg 							  extack);
3511ba7d49b1SJiri Pirko 			if (err < 0)
3512ba7d49b1SJiri Pirko 				return err;
3513c92bf26cSJakub Kicinski 			slave_data = tbs->slave_attr;
3514ba7d49b1SJiri Pirko 		}
3515ba7d49b1SJiri Pirko 	}
3516ba7d49b1SJiri Pirko 
351738f7b870SPatrick McHardy 	if (dev) {
351890c325e3SNicolas Dichtel 		int status = 0;
351938f7b870SPatrick McHardy 
352038f7b870SPatrick McHardy 		if (nlh->nlmsg_flags & NLM_F_EXCL)
352138f7b870SPatrick McHardy 			return -EEXIST;
352238f7b870SPatrick McHardy 		if (nlh->nlmsg_flags & NLM_F_REPLACE)
352338f7b870SPatrick McHardy 			return -EOPNOTSUPP;
352438f7b870SPatrick McHardy 
352538f7b870SPatrick McHardy 		if (linkinfo[IFLA_INFO_DATA]) {
352638f7b870SPatrick McHardy 			if (!ops || ops != dev->rtnl_link_ops ||
352738f7b870SPatrick McHardy 			    !ops->changelink)
352838f7b870SPatrick McHardy 				return -EOPNOTSUPP;
352938f7b870SPatrick McHardy 
3530ad744b22SMatthias Schiffer 			err = ops->changelink(dev, tb, data, extack);
353138f7b870SPatrick McHardy 			if (err < 0)
353238f7b870SPatrick McHardy 				return err;
3533ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
353438f7b870SPatrick McHardy 		}
353538f7b870SPatrick McHardy 
3536ba7d49b1SJiri Pirko 		if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
3537ba7d49b1SJiri Pirko 			if (!m_ops || !m_ops->slave_changelink)
3538ba7d49b1SJiri Pirko 				return -EOPNOTSUPP;
3539ba7d49b1SJiri Pirko 
3540420d0318SJakub Kicinski 			err = m_ops->slave_changelink(master_dev, dev, tb,
3541420d0318SJakub Kicinski 						      slave_data, extack);
3542ba7d49b1SJiri Pirko 			if (err < 0)
3543ba7d49b1SJiri Pirko 				return err;
3544ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
3545ba7d49b1SJiri Pirko 		}
3546ba7d49b1SJiri Pirko 
35475ea08b52SFlorent Fourcot 		return do_setlink(skb, dev, ifm, extack, tb, status);
354838f7b870SPatrick McHardy 	}
354938f7b870SPatrick McHardy 
3550ffa934f1SPatrick McHardy 	if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
3551ef2a7c90SFlorent Fourcot 		/* No dev found and NLM_F_CREATE not set. Requested dev does not exist,
3552ef2a7c90SFlorent Fourcot 		 * or it's for a group
3553ef2a7c90SFlorent Fourcot 		*/
3554ef2a7c90SFlorent Fourcot 		if (link_specified)
3555ef2a7c90SFlorent Fourcot 			return -ENODEV;
3556ef2a7c90SFlorent Fourcot 		if (tb[IFLA_GROUP])
355790f62cf3SEric W. Biederman 			return rtnl_group_changelink(skb, net,
3558ffa934f1SPatrick McHardy 						nla_get_u32(tb[IFLA_GROUP]),
3559ddf9f970SJakub Kicinski 						ifm, extack, tb);
35606f37c9f9SFlorent Fourcot 		return -ENODEV;
3561ffa934f1SPatrick McHardy 	}
356238f7b870SPatrick McHardy 
3563160ca014STheuns Verwoerd 	if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
356438f7b870SPatrick McHardy 		return -EOPNOTSUPP;
356538f7b870SPatrick McHardy 
356638f7b870SPatrick McHardy 	if (!ops) {
356795a5afcaSJohannes Berg #ifdef CONFIG_MODULES
356838f7b870SPatrick McHardy 		if (kind[0]) {
356938f7b870SPatrick McHardy 			__rtnl_unlock();
357038f7b870SPatrick McHardy 			request_module("rtnl-link-%s", kind);
357138f7b870SPatrick McHardy 			rtnl_lock();
357238f7b870SPatrick McHardy 			ops = rtnl_link_ops_get(kind);
357338f7b870SPatrick McHardy 			if (ops)
357438f7b870SPatrick McHardy 				goto replay;
357538f7b870SPatrick McHardy 		}
357638f7b870SPatrick McHardy #endif
357768d57f3bSDavid Ahern 		NL_SET_ERR_MSG(extack, "Unknown device type");
357838f7b870SPatrick McHardy 		return -EOPNOTSUPP;
357938f7b870SPatrick McHardy 	}
358038f7b870SPatrick McHardy 
358163105e83SJakub Kicinski 	return rtnl_newlink_create(skb, ifm, ops, tb, data, extack);
358263105e83SJakub Kicinski }
358363105e83SJakub Kicinski 
3584a2939745SJakub Kicinski static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3585a2939745SJakub Kicinski 			struct netlink_ext_ack *extack)
3586a2939745SJakub Kicinski {
3587c92bf26cSJakub Kicinski 	struct rtnl_newlink_tbs *tbs;
3588a2939745SJakub Kicinski 	int ret;
3589a2939745SJakub Kicinski 
3590c92bf26cSJakub Kicinski 	tbs = kmalloc(sizeof(*tbs), GFP_KERNEL);
3591c92bf26cSJakub Kicinski 	if (!tbs)
3592a2939745SJakub Kicinski 		return -ENOMEM;
3593a2939745SJakub Kicinski 
3594c92bf26cSJakub Kicinski 	ret = __rtnl_newlink(skb, nlh, tbs, extack);
3595c92bf26cSJakub Kicinski 	kfree(tbs);
3596a2939745SJakub Kicinski 	return ret;
3597a2939745SJakub Kicinski }
3598a2939745SJakub Kicinski 
35999b3757b0SJakub Kicinski static int rtnl_valid_getlink_req(struct sk_buff *skb,
36009b3757b0SJakub Kicinski 				  const struct nlmsghdr *nlh,
36019b3757b0SJakub Kicinski 				  struct nlattr **tb,
36029b3757b0SJakub Kicinski 				  struct netlink_ext_ack *extack)
36039b3757b0SJakub Kicinski {
36049b3757b0SJakub Kicinski 	struct ifinfomsg *ifm;
36059b3757b0SJakub Kicinski 	int i, err;
36069b3757b0SJakub Kicinski 
36079b3757b0SJakub Kicinski 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
36089b3757b0SJakub Kicinski 		NL_SET_ERR_MSG(extack, "Invalid header for get link");
36099b3757b0SJakub Kicinski 		return -EINVAL;
36109b3757b0SJakub Kicinski 	}
36119b3757b0SJakub Kicinski 
36129b3757b0SJakub Kicinski 	if (!netlink_strict_get_check(skb))
36138cb08174SJohannes Berg 		return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
36148cb08174SJohannes Berg 					      ifla_policy, extack);
36159b3757b0SJakub Kicinski 
36169b3757b0SJakub Kicinski 	ifm = nlmsg_data(nlh);
36179b3757b0SJakub Kicinski 	if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
36189b3757b0SJakub Kicinski 	    ifm->ifi_change) {
36199b3757b0SJakub Kicinski 		NL_SET_ERR_MSG(extack, "Invalid values in header for get link request");
36209b3757b0SJakub Kicinski 		return -EINVAL;
36219b3757b0SJakub Kicinski 	}
36229b3757b0SJakub Kicinski 
36238cb08174SJohannes Berg 	err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFLA_MAX,
36248cb08174SJohannes Berg 					    ifla_policy, extack);
36259b3757b0SJakub Kicinski 	if (err)
36269b3757b0SJakub Kicinski 		return err;
36279b3757b0SJakub Kicinski 
36289b3757b0SJakub Kicinski 	for (i = 0; i <= IFLA_MAX; i++) {
36299b3757b0SJakub Kicinski 		if (!tb[i])
36309b3757b0SJakub Kicinski 			continue;
36319b3757b0SJakub Kicinski 
36329b3757b0SJakub Kicinski 		switch (i) {
36339b3757b0SJakub Kicinski 		case IFLA_IFNAME:
363476c9ac0eSJiri Pirko 		case IFLA_ALT_IFNAME:
36359b3757b0SJakub Kicinski 		case IFLA_EXT_MASK:
36369b3757b0SJakub Kicinski 		case IFLA_TARGET_NETNSID:
36379b3757b0SJakub Kicinski 			break;
36389b3757b0SJakub Kicinski 		default:
36399b3757b0SJakub Kicinski 			NL_SET_ERR_MSG(extack, "Unsupported attribute in get link request");
36409b3757b0SJakub Kicinski 			return -EINVAL;
36419b3757b0SJakub Kicinski 		}
36429b3757b0SJakub Kicinski 	}
36439b3757b0SJakub Kicinski 
36449b3757b0SJakub Kicinski 	return 0;
36459b3757b0SJakub Kicinski }
36469b3757b0SJakub Kicinski 
3647c21ef3e3SDavid Ahern static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3648c21ef3e3SDavid Ahern 			struct netlink_ext_ack *extack)
3649711e2c33SJean Tourrilhes {
36503b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
365179e1ad14SJiri Benc 	struct net *tgt_net = net;
3652b60c5115SThomas Graf 	struct ifinfomsg *ifm;
3653b60c5115SThomas Graf 	struct nlattr *tb[IFLA_MAX+1];
3654b60c5115SThomas Graf 	struct net_device *dev = NULL;
3655b60c5115SThomas Graf 	struct sk_buff *nskb;
365679e1ad14SJiri Benc 	int netnsid = -1;
3657339bf98fSThomas Graf 	int err;
3658115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
3659711e2c33SJean Tourrilhes 
36609b3757b0SJakub Kicinski 	err = rtnl_valid_getlink_req(skb, nlh, tb, extack);
3661b60c5115SThomas Graf 	if (err < 0)
36629918f230SEric Sesterhenn 		return err;
3663b60c5115SThomas Graf 
36644ff66caeSChristian Brauner 	err = rtnl_ensure_unique_netns(tb, extack, true);
36654ff66caeSChristian Brauner 	if (err < 0)
36664ff66caeSChristian Brauner 		return err;
36674ff66caeSChristian Brauner 
36687e4a8d5aSChristian Brauner 	if (tb[IFLA_TARGET_NETNSID]) {
36697e4a8d5aSChristian Brauner 		netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
3670c383edc4SChristian Brauner 		tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
367179e1ad14SJiri Benc 		if (IS_ERR(tgt_net))
367279e1ad14SJiri Benc 			return PTR_ERR(tgt_net);
367379e1ad14SJiri Benc 	}
367479e1ad14SJiri Benc 
3675115c9b81SGreg Rose 	if (tb[IFLA_EXT_MASK])
3676115c9b81SGreg Rose 		ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3677115c9b81SGreg Rose 
367879e1ad14SJiri Benc 	err = -EINVAL;
3679b60c5115SThomas Graf 	ifm = nlmsg_data(nlh);
3680a3d12891SEric Dumazet 	if (ifm->ifi_index > 0)
368179e1ad14SJiri Benc 		dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
368276c9ac0eSJiri Pirko 	else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
36835ea08b52SFlorent Fourcot 		dev = rtnl_dev_get(tgt_net, tb);
3684a3d12891SEric Dumazet 	else
368579e1ad14SJiri Benc 		goto out;
3686b60c5115SThomas Graf 
368779e1ad14SJiri Benc 	err = -ENODEV;
3688a3d12891SEric Dumazet 	if (dev == NULL)
368979e1ad14SJiri Benc 		goto out;
3690a3d12891SEric Dumazet 
369179e1ad14SJiri Benc 	err = -ENOBUFS;
3692115c9b81SGreg Rose 	nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
3693a3d12891SEric Dumazet 	if (nskb == NULL)
369479e1ad14SJiri Benc 		goto out;
3695711e2c33SJean Tourrilhes 
369679e1ad14SJiri Benc 	err = rtnl_fill_ifinfo(nskb, dev, net,
369779e1ad14SJiri Benc 			       RTM_NEWLINK, NETLINK_CB(skb).portid,
369879e1ad14SJiri Benc 			       nlh->nlmsg_seq, 0, 0, ext_filter_mask,
3699d4e4fdf9SGuillaume Nault 			       0, NULL, 0, netnsid, GFP_KERNEL);
370026932566SPatrick McHardy 	if (err < 0) {
370126932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in if_nlmsg_size */
370226932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
370326932566SPatrick McHardy 		kfree_skb(nskb);
3704a3d12891SEric Dumazet 	} else
370515e47304SEric W. Biederman 		err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
370679e1ad14SJiri Benc out:
370779e1ad14SJiri Benc 	if (netnsid >= 0)
370879e1ad14SJiri Benc 		put_net(tgt_net);
3709b60c5115SThomas Graf 
3710711e2c33SJean Tourrilhes 	return err;
3711711e2c33SJean Tourrilhes }
3712711e2c33SJean Tourrilhes 
371336fbf1e5SJiri Pirko static int rtnl_alt_ifname(int cmd, struct net_device *dev, struct nlattr *attr,
371436fbf1e5SJiri Pirko 			   bool *changed, struct netlink_ext_ack *extack)
371536fbf1e5SJiri Pirko {
371636fbf1e5SJiri Pirko 	char *alt_ifname;
3717155fb43bSJakub Kicinski 	size_t size;
371836fbf1e5SJiri Pirko 	int err;
371936fbf1e5SJiri Pirko 
372036fbf1e5SJiri Pirko 	err = nla_validate(attr, attr->nla_len, IFLA_MAX, ifla_policy, extack);
372136fbf1e5SJiri Pirko 	if (err)
372236fbf1e5SJiri Pirko 		return err;
372336fbf1e5SJiri Pirko 
3724155fb43bSJakub Kicinski 	if (cmd == RTM_NEWLINKPROP) {
3725155fb43bSJakub Kicinski 		size = rtnl_prop_list_size(dev);
3726155fb43bSJakub Kicinski 		size += nla_total_size(ALTIFNAMSIZ);
3727155fb43bSJakub Kicinski 		if (size >= U16_MAX) {
3728155fb43bSJakub Kicinski 			NL_SET_ERR_MSG(extack,
3729155fb43bSJakub Kicinski 				       "effective property list too long");
3730155fb43bSJakub Kicinski 			return -EINVAL;
3731155fb43bSJakub Kicinski 		}
3732155fb43bSJakub Kicinski 	}
3733155fb43bSJakub Kicinski 
37345d26cff5SJakub Kicinski 	alt_ifname = nla_strdup(attr, GFP_KERNEL_ACCOUNT);
373536fbf1e5SJiri Pirko 	if (!alt_ifname)
373636fbf1e5SJiri Pirko 		return -ENOMEM;
373744bfa9c5SEric Dumazet 
373844bfa9c5SEric Dumazet 	if (cmd == RTM_NEWLINKPROP) {
373936fbf1e5SJiri Pirko 		err = netdev_name_node_alt_create(dev, alt_ifname);
374044bfa9c5SEric Dumazet 		if (!err)
374144bfa9c5SEric Dumazet 			alt_ifname = NULL;
374236fbf1e5SJiri Pirko 	} else if (cmd == RTM_DELLINKPROP) {
374336fbf1e5SJiri Pirko 		err = netdev_name_node_alt_destroy(dev, alt_ifname);
374436fbf1e5SJiri Pirko 	} else {
374544bfa9c5SEric Dumazet 		WARN_ON_ONCE(1);
374644bfa9c5SEric Dumazet 		err = -EINVAL;
374736fbf1e5SJiri Pirko 	}
374836fbf1e5SJiri Pirko 
374944bfa9c5SEric Dumazet 	kfree(alt_ifname);
375044bfa9c5SEric Dumazet 	if (!err)
375136fbf1e5SJiri Pirko 		*changed = true;
375244bfa9c5SEric Dumazet 	return err;
375336fbf1e5SJiri Pirko }
375436fbf1e5SJiri Pirko 
375536fbf1e5SJiri Pirko static int rtnl_linkprop(int cmd, struct sk_buff *skb, struct nlmsghdr *nlh,
375636fbf1e5SJiri Pirko 			 struct netlink_ext_ack *extack)
375736fbf1e5SJiri Pirko {
375836fbf1e5SJiri Pirko 	struct net *net = sock_net(skb->sk);
375936fbf1e5SJiri Pirko 	struct nlattr *tb[IFLA_MAX + 1];
376036fbf1e5SJiri Pirko 	struct net_device *dev;
376136fbf1e5SJiri Pirko 	struct ifinfomsg *ifm;
376236fbf1e5SJiri Pirko 	bool changed = false;
376336fbf1e5SJiri Pirko 	struct nlattr *attr;
376436fbf1e5SJiri Pirko 	int err, rem;
376536fbf1e5SJiri Pirko 
376636fbf1e5SJiri Pirko 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
376736fbf1e5SJiri Pirko 	if (err)
376836fbf1e5SJiri Pirko 		return err;
376936fbf1e5SJiri Pirko 
377036fbf1e5SJiri Pirko 	err = rtnl_ensure_unique_netns(tb, extack, true);
377136fbf1e5SJiri Pirko 	if (err)
377236fbf1e5SJiri Pirko 		return err;
377336fbf1e5SJiri Pirko 
377436fbf1e5SJiri Pirko 	ifm = nlmsg_data(nlh);
3775cc6090e9SJiri Pirko 	if (ifm->ifi_index > 0)
377636fbf1e5SJiri Pirko 		dev = __dev_get_by_index(net, ifm->ifi_index);
377776c9ac0eSJiri Pirko 	else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
37785ea08b52SFlorent Fourcot 		dev = rtnl_dev_get(net, tb);
3779cc6090e9SJiri Pirko 	else
378036fbf1e5SJiri Pirko 		return -EINVAL;
378136fbf1e5SJiri Pirko 
378236fbf1e5SJiri Pirko 	if (!dev)
378336fbf1e5SJiri Pirko 		return -ENODEV;
378436fbf1e5SJiri Pirko 
378536fbf1e5SJiri Pirko 	if (!tb[IFLA_PROP_LIST])
378636fbf1e5SJiri Pirko 		return 0;
378736fbf1e5SJiri Pirko 
378836fbf1e5SJiri Pirko 	nla_for_each_nested(attr, tb[IFLA_PROP_LIST], rem) {
378936fbf1e5SJiri Pirko 		switch (nla_type(attr)) {
379036fbf1e5SJiri Pirko 		case IFLA_ALT_IFNAME:
379136fbf1e5SJiri Pirko 			err = rtnl_alt_ifname(cmd, dev, attr, &changed, extack);
379236fbf1e5SJiri Pirko 			if (err)
379336fbf1e5SJiri Pirko 				return err;
379436fbf1e5SJiri Pirko 			break;
379536fbf1e5SJiri Pirko 		}
379636fbf1e5SJiri Pirko 	}
379736fbf1e5SJiri Pirko 
379836fbf1e5SJiri Pirko 	if (changed)
379936fbf1e5SJiri Pirko 		netdev_state_change(dev);
380036fbf1e5SJiri Pirko 	return 0;
380136fbf1e5SJiri Pirko }
380236fbf1e5SJiri Pirko 
380336fbf1e5SJiri Pirko static int rtnl_newlinkprop(struct sk_buff *skb, struct nlmsghdr *nlh,
380436fbf1e5SJiri Pirko 			    struct netlink_ext_ack *extack)
380536fbf1e5SJiri Pirko {
380636fbf1e5SJiri Pirko 	return rtnl_linkprop(RTM_NEWLINKPROP, skb, nlh, extack);
380736fbf1e5SJiri Pirko }
380836fbf1e5SJiri Pirko 
380936fbf1e5SJiri Pirko static int rtnl_dellinkprop(struct sk_buff *skb, struct nlmsghdr *nlh,
381036fbf1e5SJiri Pirko 			    struct netlink_ext_ack *extack)
381136fbf1e5SJiri Pirko {
381236fbf1e5SJiri Pirko 	return rtnl_linkprop(RTM_DELLINKPROP, skb, nlh, extack);
381336fbf1e5SJiri Pirko }
381436fbf1e5SJiri Pirko 
3815ebfe3c51SDi Zhu static u32 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
3816c7ac8679SGreg Rose {
3817115c9b81SGreg Rose 	struct net *net = sock_net(skb->sk);
3818ebfe3c51SDi Zhu 	size_t min_ifinfo_dump_size = 0;
3819115c9b81SGreg Rose 	struct nlattr *tb[IFLA_MAX+1];
3820115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
3821ebfe3c51SDi Zhu 	struct net_device *dev;
3822e5eca6d4SMichal Schmidt 	int hdrlen;
3823115c9b81SGreg Rose 
3824e5eca6d4SMichal Schmidt 	/* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
3825e5eca6d4SMichal Schmidt 	hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
3826e5eca6d4SMichal Schmidt 		 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
3827e5eca6d4SMichal Schmidt 
38288cb08174SJohannes Berg 	if (nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
3829115c9b81SGreg Rose 		if (tb[IFLA_EXT_MASK])
3830115c9b81SGreg Rose 			ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3831a4b64fbeSEric Dumazet 	}
3832115c9b81SGreg Rose 
3833115c9b81SGreg Rose 	if (!ext_filter_mask)
3834115c9b81SGreg Rose 		return NLMSG_GOODSIZE;
3835115c9b81SGreg Rose 	/*
3836115c9b81SGreg Rose 	 * traverse the list of net devices and compute the minimum
3837115c9b81SGreg Rose 	 * buffer size based upon the filter mask.
3838115c9b81SGreg Rose 	 */
38396853dd48SFlorian Westphal 	rcu_read_lock();
38406853dd48SFlorian Westphal 	for_each_netdev_rcu(net, dev) {
3841ebfe3c51SDi Zhu 		min_ifinfo_dump_size = max(min_ifinfo_dump_size,
3842ebfe3c51SDi Zhu 					   if_nlmsg_size(dev, ext_filter_mask));
3843115c9b81SGreg Rose 	}
38446853dd48SFlorian Westphal 	rcu_read_unlock();
3845115c9b81SGreg Rose 
384693af2056SZhang Shengju 	return nlmsg_total_size(min_ifinfo_dump_size);
3847c7ac8679SGreg Rose }
3848c7ac8679SGreg Rose 
384942bad1daSAdrian Bunk static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
38501da177e4SLinus Torvalds {
38511da177e4SLinus Torvalds 	int idx;
38521da177e4SLinus Torvalds 	int s_idx = cb->family;
385387ccbb1fSChristian Brauner 	int type = cb->nlh->nlmsg_type - RTM_BASE;
3854c63586dcSDavid Ahern 	int ret = 0;
38551da177e4SLinus Torvalds 
38561da177e4SLinus Torvalds 	if (s_idx == 0)
38571da177e4SLinus Torvalds 		s_idx = 1;
38586853dd48SFlorian Westphal 
385925239ceeSPatrick McHardy 	for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
386051e13685SJakub Kicinski 		struct rtnl_link __rcu **tab;
3861addf9b90SFlorian Westphal 		struct rtnl_link *link;
38626853dd48SFlorian Westphal 		rtnl_dumpit_func dumpit;
38636853dd48SFlorian Westphal 
38641da177e4SLinus Torvalds 		if (idx < s_idx || idx == PF_PACKET)
38651da177e4SLinus Torvalds 			continue;
38666853dd48SFlorian Westphal 
3867addf9b90SFlorian Westphal 		if (type < 0 || type >= RTM_NR_MSGTYPES)
38681da177e4SLinus Torvalds 			continue;
38696853dd48SFlorian Westphal 
3870addf9b90SFlorian Westphal 		tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]);
3871addf9b90SFlorian Westphal 		if (!tab)
3872addf9b90SFlorian Westphal 			continue;
3873addf9b90SFlorian Westphal 
387451e13685SJakub Kicinski 		link = rcu_dereference_rtnl(tab[type]);
3875addf9b90SFlorian Westphal 		if (!link)
3876addf9b90SFlorian Westphal 			continue;
3877addf9b90SFlorian Westphal 
3878addf9b90SFlorian Westphal 		dumpit = link->dumpit;
38796853dd48SFlorian Westphal 		if (!dumpit)
38806853dd48SFlorian Westphal 			continue;
38816853dd48SFlorian Westphal 
38820465277fSNicolas Dichtel 		if (idx > s_idx) {
38831da177e4SLinus Torvalds 			memset(&cb->args[0], 0, sizeof(cb->args));
38840465277fSNicolas Dichtel 			cb->prev_seq = 0;
38850465277fSNicolas Dichtel 			cb->seq = 0;
38860465277fSNicolas Dichtel 		}
3887c63586dcSDavid Ahern 		ret = dumpit(skb, cb);
38885e1acb4aSAlexey Kodanev 		if (ret)
38891da177e4SLinus Torvalds 			break;
38901da177e4SLinus Torvalds 	}
38911da177e4SLinus Torvalds 	cb->family = idx;
38921da177e4SLinus Torvalds 
3893c63586dcSDavid Ahern 	return skb->len ? : ret;
38941da177e4SLinus Torvalds }
38951da177e4SLinus Torvalds 
3896395eea6cSMahesh Bandewar struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
38973d3ea5afSVlad Yasevich 				       unsigned int change,
389838e01b30SNicolas Dichtel 				       u32 event, gfp_t flags, int *new_nsid,
389938e01b30SNicolas Dichtel 				       int new_ifindex)
39001da177e4SLinus Torvalds {
3901c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
39021da177e4SLinus Torvalds 	struct sk_buff *skb;
39030ec6d3f4SThomas Graf 	int err = -ENOBUFS;
39041da177e4SLinus Torvalds 
390550af5969Sluo penghao 	skb = nlmsg_new(if_nlmsg_size(dev, 0), flags);
39060ec6d3f4SThomas Graf 	if (skb == NULL)
39070ec6d3f4SThomas Graf 		goto errout;
39081da177e4SLinus Torvalds 
390979e1ad14SJiri Benc 	err = rtnl_fill_ifinfo(skb, dev, dev_net(dev),
391079e1ad14SJiri Benc 			       type, 0, 0, change, 0, 0, event,
3911d4e4fdf9SGuillaume Nault 			       new_nsid, new_ifindex, -1, flags);
391226932566SPatrick McHardy 	if (err < 0) {
391326932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in if_nlmsg_size() */
391426932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
391526932566SPatrick McHardy 		kfree_skb(skb);
391626932566SPatrick McHardy 		goto errout;
391726932566SPatrick McHardy 	}
3918395eea6cSMahesh Bandewar 	return skb;
39190ec6d3f4SThomas Graf errout:
39200ec6d3f4SThomas Graf 	if (err < 0)
39214b3da706SEric W. Biederman 		rtnl_set_sk_err(net, RTNLGRP_LINK, err);
3922395eea6cSMahesh Bandewar 	return NULL;
3923395eea6cSMahesh Bandewar }
3924395eea6cSMahesh Bandewar 
3925395eea6cSMahesh Bandewar void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
3926395eea6cSMahesh Bandewar {
3927395eea6cSMahesh Bandewar 	struct net *net = dev_net(dev);
3928395eea6cSMahesh Bandewar 
3929395eea6cSMahesh Bandewar 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
3930395eea6cSMahesh Bandewar }
3931395eea6cSMahesh Bandewar 
39323d3ea5afSVlad Yasevich static void rtmsg_ifinfo_event(int type, struct net_device *dev,
39333d3ea5afSVlad Yasevich 			       unsigned int change, u32 event,
393438e01b30SNicolas Dichtel 			       gfp_t flags, int *new_nsid, int new_ifindex)
3935395eea6cSMahesh Bandewar {
3936395eea6cSMahesh Bandewar 	struct sk_buff *skb;
3937395eea6cSMahesh Bandewar 
3938ed2a80abSNicolas Dichtel 	if (dev->reg_state != NETREG_REGISTERED)
3939ed2a80abSNicolas Dichtel 		return;
3940ed2a80abSNicolas Dichtel 
394138e01b30SNicolas Dichtel 	skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid,
394238e01b30SNicolas Dichtel 				     new_ifindex);
3943395eea6cSMahesh Bandewar 	if (skb)
3944395eea6cSMahesh Bandewar 		rtmsg_ifinfo_send(skb, dev, flags);
39451da177e4SLinus Torvalds }
39463d3ea5afSVlad Yasevich 
39473d3ea5afSVlad Yasevich void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
39483d3ea5afSVlad Yasevich 		  gfp_t flags)
39493d3ea5afSVlad Yasevich {
395038e01b30SNicolas Dichtel 	rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
395138e01b30SNicolas Dichtel 			   NULL, 0);
39523d3ea5afSVlad Yasevich }
39531da177e4SLinus Torvalds 
39546621dd29SNicolas Dichtel void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change,
395538e01b30SNicolas Dichtel 			 gfp_t flags, int *new_nsid, int new_ifindex)
39566621dd29SNicolas Dichtel {
39576621dd29SNicolas Dichtel 	rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
395838e01b30SNicolas Dichtel 			   new_nsid, new_ifindex);
39596621dd29SNicolas Dichtel }
39606621dd29SNicolas Dichtel 
3961d83b0603SJohn Fastabend static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
3962d83b0603SJohn Fastabend 				   struct net_device *dev,
39631e53d5bbSHubert Sokolowski 				   u8 *addr, u16 vid, u32 pid, u32 seq,
39641c104a6bSNicolas Dichtel 				   int type, unsigned int flags,
3965b3379041SHubert Sokolowski 				   int nlflags, u16 ndm_state)
3966d83b0603SJohn Fastabend {
3967d83b0603SJohn Fastabend 	struct nlmsghdr *nlh;
3968d83b0603SJohn Fastabend 	struct ndmsg *ndm;
3969d83b0603SJohn Fastabend 
39701c104a6bSNicolas Dichtel 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
3971d83b0603SJohn Fastabend 	if (!nlh)
3972d83b0603SJohn Fastabend 		return -EMSGSIZE;
3973d83b0603SJohn Fastabend 
3974d83b0603SJohn Fastabend 	ndm = nlmsg_data(nlh);
3975d83b0603SJohn Fastabend 	ndm->ndm_family  = AF_BRIDGE;
3976d83b0603SJohn Fastabend 	ndm->ndm_pad1	 = 0;
3977d83b0603SJohn Fastabend 	ndm->ndm_pad2    = 0;
3978d83b0603SJohn Fastabend 	ndm->ndm_flags	 = flags;
3979d83b0603SJohn Fastabend 	ndm->ndm_type	 = 0;
3980d83b0603SJohn Fastabend 	ndm->ndm_ifindex = dev->ifindex;
3981b3379041SHubert Sokolowski 	ndm->ndm_state   = ndm_state;
3982d83b0603SJohn Fastabend 
3983d83b0603SJohn Fastabend 	if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
3984d83b0603SJohn Fastabend 		goto nla_put_failure;
39851e53d5bbSHubert Sokolowski 	if (vid)
39861e53d5bbSHubert Sokolowski 		if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
39871e53d5bbSHubert Sokolowski 			goto nla_put_failure;
3988d83b0603SJohn Fastabend 
3989053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
3990053c095aSJohannes Berg 	return 0;
3991d83b0603SJohn Fastabend 
3992d83b0603SJohn Fastabend nla_put_failure:
3993d83b0603SJohn Fastabend 	nlmsg_cancel(skb, nlh);
3994d83b0603SJohn Fastabend 	return -EMSGSIZE;
3995d83b0603SJohn Fastabend }
3996d83b0603SJohn Fastabend 
39973ff661c3SJohn Fastabend static inline size_t rtnl_fdb_nlmsg_size(void)
39983ff661c3SJohn Fastabend {
3999f82ef3e1SSabrina Dubroca 	return NLMSG_ALIGN(sizeof(struct ndmsg)) +
4000f82ef3e1SSabrina Dubroca 	       nla_total_size(ETH_ALEN) +	/* NDA_LLADDR */
4001f82ef3e1SSabrina Dubroca 	       nla_total_size(sizeof(u16)) +	/* NDA_VLAN */
4002f82ef3e1SSabrina Dubroca 	       0;
40033ff661c3SJohn Fastabend }
40043ff661c3SJohn Fastabend 
4005b3379041SHubert Sokolowski static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
4006b3379041SHubert Sokolowski 			    u16 ndm_state)
40073ff661c3SJohn Fastabend {
40083ff661c3SJohn Fastabend 	struct net *net = dev_net(dev);
40093ff661c3SJohn Fastabend 	struct sk_buff *skb;
40103ff661c3SJohn Fastabend 	int err = -ENOBUFS;
40113ff661c3SJohn Fastabend 
40123ff661c3SJohn Fastabend 	skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
40133ff661c3SJohn Fastabend 	if (!skb)
40143ff661c3SJohn Fastabend 		goto errout;
40153ff661c3SJohn Fastabend 
40161e53d5bbSHubert Sokolowski 	err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
4017b3379041SHubert Sokolowski 				      0, 0, type, NTF_SELF, 0, ndm_state);
40183ff661c3SJohn Fastabend 	if (err < 0) {
40193ff661c3SJohn Fastabend 		kfree_skb(skb);
40203ff661c3SJohn Fastabend 		goto errout;
40213ff661c3SJohn Fastabend 	}
40223ff661c3SJohn Fastabend 
40233ff661c3SJohn Fastabend 	rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
40243ff661c3SJohn Fastabend 	return;
40253ff661c3SJohn Fastabend errout:
40263ff661c3SJohn Fastabend 	rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
40273ff661c3SJohn Fastabend }
40283ff661c3SJohn Fastabend 
4029a986967eSBart Van Assche /*
4030090096bfSVlad Yasevich  * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
4031090096bfSVlad Yasevich  */
4032090096bfSVlad Yasevich int ndo_dflt_fdb_add(struct ndmsg *ndm,
4033090096bfSVlad Yasevich 		     struct nlattr *tb[],
4034090096bfSVlad Yasevich 		     struct net_device *dev,
4035f6f6424bSJiri Pirko 		     const unsigned char *addr, u16 vid,
4036090096bfSVlad Yasevich 		     u16 flags)
4037090096bfSVlad Yasevich {
4038090096bfSVlad Yasevich 	int err = -EINVAL;
4039090096bfSVlad Yasevich 
4040090096bfSVlad Yasevich 	/* If aging addresses are supported device will need to
4041090096bfSVlad Yasevich 	 * implement its own handler for this.
4042090096bfSVlad Yasevich 	 */
4043090096bfSVlad Yasevich 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
404478ecc890SVladimir Oltean 		netdev_info(dev, "default FDB implementation only supports local addresses\n");
4045090096bfSVlad Yasevich 		return err;
4046090096bfSVlad Yasevich 	}
4047090096bfSVlad Yasevich 
404865891feaSOr Gerlitz 	if (vid) {
404923ac0b42SVladimir Oltean 		netdev_info(dev, "vlans aren't supported yet for dev_uc|mc_add()\n");
405065891feaSOr Gerlitz 		return err;
405165891feaSOr Gerlitz 	}
405265891feaSOr Gerlitz 
4053090096bfSVlad Yasevich 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
4054090096bfSVlad Yasevich 		err = dev_uc_add_excl(dev, addr);
4055090096bfSVlad Yasevich 	else if (is_multicast_ether_addr(addr))
4056090096bfSVlad Yasevich 		err = dev_mc_add_excl(dev, addr);
4057090096bfSVlad Yasevich 
4058090096bfSVlad Yasevich 	/* Only return duplicate errors if NLM_F_EXCL is set */
4059090096bfSVlad Yasevich 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
4060090096bfSVlad Yasevich 		err = 0;
4061090096bfSVlad Yasevich 
4062090096bfSVlad Yasevich 	return err;
4063090096bfSVlad Yasevich }
4064090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_add);
4065090096bfSVlad Yasevich 
4066b88d12e4SFlorian Westphal static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid,
4067b88d12e4SFlorian Westphal 			 struct netlink_ext_ack *extack)
4068f6f6424bSJiri Pirko {
4069f6f6424bSJiri Pirko 	u16 vid = 0;
4070f6f6424bSJiri Pirko 
4071f6f6424bSJiri Pirko 	if (vlan_attr) {
4072f6f6424bSJiri Pirko 		if (nla_len(vlan_attr) != sizeof(u16)) {
4073b88d12e4SFlorian Westphal 			NL_SET_ERR_MSG(extack, "invalid vlan attribute size");
4074f6f6424bSJiri Pirko 			return -EINVAL;
4075f6f6424bSJiri Pirko 		}
4076f6f6424bSJiri Pirko 
4077f6f6424bSJiri Pirko 		vid = nla_get_u16(vlan_attr);
4078f6f6424bSJiri Pirko 
4079f6f6424bSJiri Pirko 		if (!vid || vid >= VLAN_VID_MASK) {
4080b88d12e4SFlorian Westphal 			NL_SET_ERR_MSG(extack, "invalid vlan id");
4081f6f6424bSJiri Pirko 			return -EINVAL;
4082f6f6424bSJiri Pirko 		}
4083f6f6424bSJiri Pirko 	}
4084f6f6424bSJiri Pirko 	*p_vid = vid;
4085f6f6424bSJiri Pirko 	return 0;
4086f6f6424bSJiri Pirko }
4087f6f6424bSJiri Pirko 
4088c21ef3e3SDavid Ahern static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
4089c21ef3e3SDavid Ahern 			struct netlink_ext_ack *extack)
409077162022SJohn Fastabend {
409177162022SJohn Fastabend 	struct net *net = sock_net(skb->sk);
409277162022SJohn Fastabend 	struct ndmsg *ndm;
409377162022SJohn Fastabend 	struct nlattr *tb[NDA_MAX+1];
409477162022SJohn Fastabend 	struct net_device *dev;
409577162022SJohn Fastabend 	u8 *addr;
4096f6f6424bSJiri Pirko 	u16 vid;
409777162022SJohn Fastabend 	int err;
409877162022SJohn Fastabend 
40998cb08174SJohannes Berg 	err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL,
41008cb08174SJohannes Berg 				     extack);
410177162022SJohn Fastabend 	if (err < 0)
410277162022SJohn Fastabend 		return err;
410377162022SJohn Fastabend 
410477162022SJohn Fastabend 	ndm = nlmsg_data(nlh);
410577162022SJohn Fastabend 	if (ndm->ndm_ifindex == 0) {
4106b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "invalid ifindex");
410777162022SJohn Fastabend 		return -EINVAL;
410877162022SJohn Fastabend 	}
410977162022SJohn Fastabend 
411077162022SJohn Fastabend 	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
411177162022SJohn Fastabend 	if (dev == NULL) {
4112b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "unknown ifindex");
411377162022SJohn Fastabend 		return -ENODEV;
411477162022SJohn Fastabend 	}
411577162022SJohn Fastabend 
411677162022SJohn Fastabend 	if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
4117b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "invalid address");
411877162022SJohn Fastabend 		return -EINVAL;
411977162022SJohn Fastabend 	}
412077162022SJohn Fastabend 
4121da715775SIdo Schimmel 	if (dev->type != ARPHRD_ETHER) {
4122da715775SIdo Schimmel 		NL_SET_ERR_MSG(extack, "FDB add only supported for Ethernet devices");
4123da715775SIdo Schimmel 		return -EINVAL;
4124da715775SIdo Schimmel 	}
4125da715775SIdo Schimmel 
412677162022SJohn Fastabend 	addr = nla_data(tb[NDA_LLADDR]);
412777162022SJohn Fastabend 
4128b88d12e4SFlorian Westphal 	err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
4129f6f6424bSJiri Pirko 	if (err)
4130f6f6424bSJiri Pirko 		return err;
4131f6f6424bSJiri Pirko 
413277162022SJohn Fastabend 	err = -EOPNOTSUPP;
413377162022SJohn Fastabend 
413477162022SJohn Fastabend 	/* Support fdb on master device the net/bridge default case */
413577162022SJohn Fastabend 	if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
41362e92a2d0SJulian Wiedmann 	    netif_is_bridge_port(dev)) {
4137898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4138898e5061SJiri Pirko 		const struct net_device_ops *ops = br_dev->netdev_ops;
4139898e5061SJiri Pirko 
4140f6f6424bSJiri Pirko 		err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
414187b0984eSPetr Machata 				       nlh->nlmsg_flags, extack);
414277162022SJohn Fastabend 		if (err)
414377162022SJohn Fastabend 			goto out;
414477162022SJohn Fastabend 		else
414577162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_MASTER;
414677162022SJohn Fastabend 	}
414777162022SJohn Fastabend 
414877162022SJohn Fastabend 	/* Embedded bridge, macvlan, and any other device support */
4149090096bfSVlad Yasevich 	if ((ndm->ndm_flags & NTF_SELF)) {
4150090096bfSVlad Yasevich 		if (dev->netdev_ops->ndo_fdb_add)
4151090096bfSVlad Yasevich 			err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
4152f6f6424bSJiri Pirko 							   vid,
415387b0984eSPetr Machata 							   nlh->nlmsg_flags,
415487b0984eSPetr Machata 							   extack);
4155090096bfSVlad Yasevich 		else
4156f6f6424bSJiri Pirko 			err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
415777162022SJohn Fastabend 					       nlh->nlmsg_flags);
415877162022SJohn Fastabend 
41593ff661c3SJohn Fastabend 		if (!err) {
4160b3379041SHubert Sokolowski 			rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
4161b3379041SHubert Sokolowski 					ndm->ndm_state);
416277162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_SELF;
416377162022SJohn Fastabend 		}
41643ff661c3SJohn Fastabend 	}
416577162022SJohn Fastabend out:
416677162022SJohn Fastabend 	return err;
416777162022SJohn Fastabend }
416877162022SJohn Fastabend 
4169a986967eSBart Van Assche /*
4170090096bfSVlad Yasevich  * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
4171090096bfSVlad Yasevich  */
4172090096bfSVlad Yasevich int ndo_dflt_fdb_del(struct ndmsg *ndm,
4173090096bfSVlad Yasevich 		     struct nlattr *tb[],
4174090096bfSVlad Yasevich 		     struct net_device *dev,
4175f6f6424bSJiri Pirko 		     const unsigned char *addr, u16 vid)
4176090096bfSVlad Yasevich {
4177c8a89c4aSAlexander Duyck 	int err = -EINVAL;
4178090096bfSVlad Yasevich 
4179090096bfSVlad Yasevich 	/* If aging addresses are supported device will need to
4180090096bfSVlad Yasevich 	 * implement its own handler for this.
4181090096bfSVlad Yasevich 	 */
418264535993SSridhar Samudrala 	if (!(ndm->ndm_state & NUD_PERMANENT)) {
418378ecc890SVladimir Oltean 		netdev_info(dev, "default FDB implementation only supports local addresses\n");
4184c8a89c4aSAlexander Duyck 		return err;
4185090096bfSVlad Yasevich 	}
4186090096bfSVlad Yasevich 
4187090096bfSVlad Yasevich 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
4188090096bfSVlad Yasevich 		err = dev_uc_del(dev, addr);
4189090096bfSVlad Yasevich 	else if (is_multicast_ether_addr(addr))
4190090096bfSVlad Yasevich 		err = dev_mc_del(dev, addr);
4191090096bfSVlad Yasevich 
4192090096bfSVlad Yasevich 	return err;
4193090096bfSVlad Yasevich }
4194090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_del);
4195090096bfSVlad Yasevich 
41969e834259SNikolay Aleksandrov static const struct nla_policy fdb_del_bulk_policy[NDA_MAX + 1] = {
41979e834259SNikolay Aleksandrov 	[NDA_VLAN]	= { .type = NLA_U16 },
41989e834259SNikolay Aleksandrov 	[NDA_IFINDEX]	= NLA_POLICY_MIN(NLA_S32, 1),
4199ea2c0f9eSNikolay Aleksandrov 	[NDA_NDM_STATE_MASK]	= { .type = NLA_U16  },
4200ea2c0f9eSNikolay Aleksandrov 	[NDA_NDM_FLAGS_MASK]	= { .type = NLA_U8 },
42019e834259SNikolay Aleksandrov };
42029e834259SNikolay Aleksandrov 
4203c21ef3e3SDavid Ahern static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
4204c21ef3e3SDavid Ahern 			struct netlink_ext_ack *extack)
420577162022SJohn Fastabend {
42069e834259SNikolay Aleksandrov 	bool del_bulk = !!(nlh->nlmsg_flags & NLM_F_BULK);
420777162022SJohn Fastabend 	struct net *net = sock_net(skb->sk);
42089e834259SNikolay Aleksandrov 	const struct net_device_ops *ops;
420977162022SJohn Fastabend 	struct ndmsg *ndm;
42101690be63SVlad Yasevich 	struct nlattr *tb[NDA_MAX+1];
421177162022SJohn Fastabend 	struct net_device *dev;
42129e834259SNikolay Aleksandrov 	__u8 *addr = NULL;
42137d311801SColin Ian King 	int err;
4214f6f6424bSJiri Pirko 	u16 vid;
421577162022SJohn Fastabend 
421690f62cf3SEric W. Biederman 	if (!netlink_capable(skb, CAP_NET_ADMIN))
42171690be63SVlad Yasevich 		return -EPERM;
42181690be63SVlad Yasevich 
42199e834259SNikolay Aleksandrov 	if (!del_bulk) {
42209e834259SNikolay Aleksandrov 		err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX,
42219e834259SNikolay Aleksandrov 					     NULL, extack);
42229e834259SNikolay Aleksandrov 	} else {
42239e834259SNikolay Aleksandrov 		err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX,
42249e834259SNikolay Aleksandrov 				  fdb_del_bulk_policy, extack);
42259e834259SNikolay Aleksandrov 	}
42261690be63SVlad Yasevich 	if (err < 0)
42271690be63SVlad Yasevich 		return err;
422877162022SJohn Fastabend 
422977162022SJohn Fastabend 	ndm = nlmsg_data(nlh);
423077162022SJohn Fastabend 	if (ndm->ndm_ifindex == 0) {
4231b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "invalid ifindex");
423277162022SJohn Fastabend 		return -EINVAL;
423377162022SJohn Fastabend 	}
423477162022SJohn Fastabend 
423577162022SJohn Fastabend 	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
423677162022SJohn Fastabend 	if (dev == NULL) {
4237b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "unknown ifindex");
423877162022SJohn Fastabend 		return -ENODEV;
423977162022SJohn Fastabend 	}
424077162022SJohn Fastabend 
42419e834259SNikolay Aleksandrov 	if (!del_bulk) {
42421690be63SVlad Yasevich 		if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
4243b88d12e4SFlorian Westphal 			NL_SET_ERR_MSG(extack, "invalid address");
424477162022SJohn Fastabend 			return -EINVAL;
424577162022SJohn Fastabend 		}
42469e834259SNikolay Aleksandrov 		addr = nla_data(tb[NDA_LLADDR]);
42479e834259SNikolay Aleksandrov 	}
424877162022SJohn Fastabend 
4249da715775SIdo Schimmel 	if (dev->type != ARPHRD_ETHER) {
4250da715775SIdo Schimmel 		NL_SET_ERR_MSG(extack, "FDB delete only supported for Ethernet devices");
4251da715775SIdo Schimmel 		return -EINVAL;
4252da715775SIdo Schimmel 	}
4253da715775SIdo Schimmel 
4254b88d12e4SFlorian Westphal 	err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
4255f6f6424bSJiri Pirko 	if (err)
4256f6f6424bSJiri Pirko 		return err;
4257f6f6424bSJiri Pirko 
425877162022SJohn Fastabend 	err = -EOPNOTSUPP;
425977162022SJohn Fastabend 
426077162022SJohn Fastabend 	/* Support fdb on master device the net/bridge default case */
426177162022SJohn Fastabend 	if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
42622e92a2d0SJulian Wiedmann 	    netif_is_bridge_port(dev)) {
4263898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
426477162022SJohn Fastabend 
42659e834259SNikolay Aleksandrov 		ops = br_dev->netdev_ops;
42669e834259SNikolay Aleksandrov 		if (!del_bulk) {
4267898e5061SJiri Pirko 			if (ops->ndo_fdb_del)
4268ca4567f1SAlaa Mohamed 				err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid, extack);
42699e834259SNikolay Aleksandrov 		} else {
42709e834259SNikolay Aleksandrov 			if (ops->ndo_fdb_del_bulk)
42719e834259SNikolay Aleksandrov 				err = ops->ndo_fdb_del_bulk(ndm, tb, dev, vid,
42729e834259SNikolay Aleksandrov 							    extack);
42739e834259SNikolay Aleksandrov 		}
427477162022SJohn Fastabend 
427577162022SJohn Fastabend 		if (err)
427677162022SJohn Fastabend 			goto out;
427777162022SJohn Fastabend 		else
427877162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_MASTER;
427977162022SJohn Fastabend 	}
428077162022SJohn Fastabend 
428177162022SJohn Fastabend 	/* Embedded bridge, macvlan, and any other device support */
4282090096bfSVlad Yasevich 	if (ndm->ndm_flags & NTF_SELF) {
42839e834259SNikolay Aleksandrov 		ops = dev->netdev_ops;
42849e834259SNikolay Aleksandrov 		if (!del_bulk) {
42859e834259SNikolay Aleksandrov 			if (ops->ndo_fdb_del)
4286ca4567f1SAlaa Mohamed 				err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid, extack);
4287090096bfSVlad Yasevich 			else
4288f6f6424bSJiri Pirko 				err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
42899e834259SNikolay Aleksandrov 		} else {
42909e834259SNikolay Aleksandrov 			/* in case err was cleared by NTF_MASTER call */
42919e834259SNikolay Aleksandrov 			err = -EOPNOTSUPP;
42929e834259SNikolay Aleksandrov 			if (ops->ndo_fdb_del_bulk)
42939e834259SNikolay Aleksandrov 				err = ops->ndo_fdb_del_bulk(ndm, tb, dev, vid,
42949e834259SNikolay Aleksandrov 							    extack);
42959e834259SNikolay Aleksandrov 		}
429677162022SJohn Fastabend 
42973ff661c3SJohn Fastabend 		if (!err) {
42989e834259SNikolay Aleksandrov 			if (!del_bulk)
4299b3379041SHubert Sokolowski 				rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
4300b3379041SHubert Sokolowski 						ndm->ndm_state);
430177162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_SELF;
430277162022SJohn Fastabend 		}
43033ff661c3SJohn Fastabend 	}
430477162022SJohn Fastabend out:
430577162022SJohn Fastabend 	return err;
430677162022SJohn Fastabend }
430777162022SJohn Fastabend 
4308d83b0603SJohn Fastabend static int nlmsg_populate_fdb(struct sk_buff *skb,
4309d83b0603SJohn Fastabend 			      struct netlink_callback *cb,
4310d83b0603SJohn Fastabend 			      struct net_device *dev,
4311d83b0603SJohn Fastabend 			      int *idx,
4312d83b0603SJohn Fastabend 			      struct netdev_hw_addr_list *list)
4313d83b0603SJohn Fastabend {
4314d83b0603SJohn Fastabend 	struct netdev_hw_addr *ha;
4315d83b0603SJohn Fastabend 	int err;
431615e47304SEric W. Biederman 	u32 portid, seq;
4317d83b0603SJohn Fastabend 
431815e47304SEric W. Biederman 	portid = NETLINK_CB(cb->skb).portid;
4319d83b0603SJohn Fastabend 	seq = cb->nlh->nlmsg_seq;
4320d83b0603SJohn Fastabend 
4321d83b0603SJohn Fastabend 	list_for_each_entry(ha, &list->list, list) {
4322d297653dSRoopa Prabhu 		if (*idx < cb->args[2])
4323d83b0603SJohn Fastabend 			goto skip;
4324d83b0603SJohn Fastabend 
43251e53d5bbSHubert Sokolowski 		err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
4326a7a558feSJohn Fastabend 					      portid, seq,
43271c104a6bSNicolas Dichtel 					      RTM_NEWNEIGH, NTF_SELF,
4328b3379041SHubert Sokolowski 					      NLM_F_MULTI, NUD_PERMANENT);
4329d83b0603SJohn Fastabend 		if (err < 0)
4330d83b0603SJohn Fastabend 			return err;
4331d83b0603SJohn Fastabend skip:
4332d83b0603SJohn Fastabend 		*idx += 1;
4333d83b0603SJohn Fastabend 	}
4334d83b0603SJohn Fastabend 	return 0;
4335d83b0603SJohn Fastabend }
4336d83b0603SJohn Fastabend 
4337d83b0603SJohn Fastabend /**
43382c53040fSBen Hutchings  * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
4339a986967eSBart Van Assche  * @skb: socket buffer to store message in
4340a986967eSBart Van Assche  * @cb: netlink callback
4341d83b0603SJohn Fastabend  * @dev: netdevice
4342a986967eSBart Van Assche  * @filter_dev: ignored
4343a986967eSBart Van Assche  * @idx: the number of FDB table entries dumped is added to *@idx
4344d83b0603SJohn Fastabend  *
4345d83b0603SJohn Fastabend  * Default netdevice operation to dump the existing unicast address list.
434691f3e7b1SJohn Fastabend  * Returns number of addresses from list put in skb.
4347d83b0603SJohn Fastabend  */
4348d83b0603SJohn Fastabend int ndo_dflt_fdb_dump(struct sk_buff *skb,
4349d83b0603SJohn Fastabend 		      struct netlink_callback *cb,
4350d83b0603SJohn Fastabend 		      struct net_device *dev,
43515d5eacb3SJamal Hadi Salim 		      struct net_device *filter_dev,
4352d297653dSRoopa Prabhu 		      int *idx)
4353d83b0603SJohn Fastabend {
4354d83b0603SJohn Fastabend 	int err;
4355d83b0603SJohn Fastabend 
435668883893SEric Dumazet 	if (dev->type != ARPHRD_ETHER)
435768883893SEric Dumazet 		return -EINVAL;
435868883893SEric Dumazet 
4359d83b0603SJohn Fastabend 	netif_addr_lock_bh(dev);
4360d297653dSRoopa Prabhu 	err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
4361d83b0603SJohn Fastabend 	if (err)
4362d83b0603SJohn Fastabend 		goto out;
43632934c9dbSZhang Shengju 	err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
4364d83b0603SJohn Fastabend out:
4365d83b0603SJohn Fastabend 	netif_addr_unlock_bh(dev);
4366d297653dSRoopa Prabhu 	return err;
4367d83b0603SJohn Fastabend }
4368d83b0603SJohn Fastabend EXPORT_SYMBOL(ndo_dflt_fdb_dump);
4369d83b0603SJohn Fastabend 
43708c6e137fSDavid Ahern static int valid_fdb_dump_strict(const struct nlmsghdr *nlh,
43718c6e137fSDavid Ahern 				 int *br_idx, int *brport_idx,
43728c6e137fSDavid Ahern 				 struct netlink_ext_ack *extack)
43738c6e137fSDavid Ahern {
43748c6e137fSDavid Ahern 	struct nlattr *tb[NDA_MAX + 1];
43758c6e137fSDavid Ahern 	struct ndmsg *ndm;
43768c6e137fSDavid Ahern 	int err, i;
43778c6e137fSDavid Ahern 
43788c6e137fSDavid Ahern 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
43798c6e137fSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid header for fdb dump request");
43808c6e137fSDavid Ahern 		return -EINVAL;
43818c6e137fSDavid Ahern 	}
43828c6e137fSDavid Ahern 
43838c6e137fSDavid Ahern 	ndm = nlmsg_data(nlh);
43848c6e137fSDavid Ahern 	if (ndm->ndm_pad1  || ndm->ndm_pad2  || ndm->ndm_state ||
43858c6e137fSDavid Ahern 	    ndm->ndm_flags || ndm->ndm_type) {
43868b73018fSNikolay Aleksandrov 		NL_SET_ERR_MSG(extack, "Invalid values in header for fdb dump request");
43878c6e137fSDavid Ahern 		return -EINVAL;
43888c6e137fSDavid Ahern 	}
43898c6e137fSDavid Ahern 
43908cb08174SJohannes Berg 	err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
43918cb08174SJohannes Berg 					    NDA_MAX, NULL, extack);
43928c6e137fSDavid Ahern 	if (err < 0)
43938c6e137fSDavid Ahern 		return err;
43948c6e137fSDavid Ahern 
43958c6e137fSDavid Ahern 	*brport_idx = ndm->ndm_ifindex;
43968c6e137fSDavid Ahern 	for (i = 0; i <= NDA_MAX; ++i) {
43978c6e137fSDavid Ahern 		if (!tb[i])
43988c6e137fSDavid Ahern 			continue;
43998c6e137fSDavid Ahern 
44008c6e137fSDavid Ahern 		switch (i) {
44018c6e137fSDavid Ahern 		case NDA_IFINDEX:
44028c6e137fSDavid Ahern 			if (nla_len(tb[i]) != sizeof(u32)) {
44038c6e137fSDavid Ahern 				NL_SET_ERR_MSG(extack, "Invalid IFINDEX attribute in fdb dump request");
44048c6e137fSDavid Ahern 				return -EINVAL;
44058c6e137fSDavid Ahern 			}
44068c6e137fSDavid Ahern 			*brport_idx = nla_get_u32(tb[NDA_IFINDEX]);
44078c6e137fSDavid Ahern 			break;
44088c6e137fSDavid Ahern 		case NDA_MASTER:
44098c6e137fSDavid Ahern 			if (nla_len(tb[i]) != sizeof(u32)) {
44108c6e137fSDavid Ahern 				NL_SET_ERR_MSG(extack, "Invalid MASTER attribute in fdb dump request");
44118c6e137fSDavid Ahern 				return -EINVAL;
44128c6e137fSDavid Ahern 			}
44138c6e137fSDavid Ahern 			*br_idx = nla_get_u32(tb[NDA_MASTER]);
44148c6e137fSDavid Ahern 			break;
44158c6e137fSDavid Ahern 		default:
44168c6e137fSDavid Ahern 			NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb dump request");
44178c6e137fSDavid Ahern 			return -EINVAL;
44188c6e137fSDavid Ahern 		}
44198c6e137fSDavid Ahern 	}
44208c6e137fSDavid Ahern 
44218c6e137fSDavid Ahern 	return 0;
44228c6e137fSDavid Ahern }
44238c6e137fSDavid Ahern 
44248dfbda19SDavid Ahern static int valid_fdb_dump_legacy(const struct nlmsghdr *nlh,
44258dfbda19SDavid Ahern 				 int *br_idx, int *brport_idx,
44268dfbda19SDavid Ahern 				 struct netlink_ext_ack *extack)
44278dfbda19SDavid Ahern {
44288dfbda19SDavid Ahern 	struct nlattr *tb[IFLA_MAX+1];
44298dfbda19SDavid Ahern 	int err;
44308dfbda19SDavid Ahern 
44318dfbda19SDavid Ahern 	/* A hack to preserve kernel<->userspace interface.
44328dfbda19SDavid Ahern 	 * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0.
44338dfbda19SDavid Ahern 	 * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails.
44348dfbda19SDavid Ahern 	 * So, check for ndmsg with an optional u32 attribute (not used here).
44358dfbda19SDavid Ahern 	 * Fortunately these sizes don't conflict with the size of ifinfomsg
44368dfbda19SDavid Ahern 	 * with an optional attribute.
44378dfbda19SDavid Ahern 	 */
44388dfbda19SDavid Ahern 	if (nlmsg_len(nlh) != sizeof(struct ndmsg) &&
44398dfbda19SDavid Ahern 	    (nlmsg_len(nlh) != sizeof(struct ndmsg) +
44408dfbda19SDavid Ahern 	     nla_attr_size(sizeof(u32)))) {
44414565d7e5SDavid Ahern 		struct ifinfomsg *ifm;
44424565d7e5SDavid Ahern 
44438cb08174SJohannes Berg 		err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
44448cb08174SJohannes Berg 					     tb, IFLA_MAX, ifla_policy,
44458cb08174SJohannes Berg 					     extack);
44468dfbda19SDavid Ahern 		if (err < 0) {
44478dfbda19SDavid Ahern 			return -EINVAL;
44488dfbda19SDavid Ahern 		} else if (err == 0) {
44498dfbda19SDavid Ahern 			if (tb[IFLA_MASTER])
44508dfbda19SDavid Ahern 				*br_idx = nla_get_u32(tb[IFLA_MASTER]);
44518dfbda19SDavid Ahern 		}
44528dfbda19SDavid Ahern 
44534565d7e5SDavid Ahern 		ifm = nlmsg_data(nlh);
44548dfbda19SDavid Ahern 		*brport_idx = ifm->ifi_index;
44558dfbda19SDavid Ahern 	}
44568dfbda19SDavid Ahern 	return 0;
44578dfbda19SDavid Ahern }
44588dfbda19SDavid Ahern 
445977162022SJohn Fastabend static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
446077162022SJohn Fastabend {
446177162022SJohn Fastabend 	struct net_device *dev;
44625e6d2435SJamal Hadi Salim 	struct net_device *br_dev = NULL;
44635e6d2435SJamal Hadi Salim 	const struct net_device_ops *ops = NULL;
44645e6d2435SJamal Hadi Salim 	const struct net_device_ops *cops = NULL;
44655e6d2435SJamal Hadi Salim 	struct net *net = sock_net(skb->sk);
4466d297653dSRoopa Prabhu 	struct hlist_head *head;
44675e6d2435SJamal Hadi Salim 	int brport_idx = 0;
44685e6d2435SJamal Hadi Salim 	int br_idx = 0;
4469d297653dSRoopa Prabhu 	int h, s_h;
4470d297653dSRoopa Prabhu 	int idx = 0, s_idx;
4471d297653dSRoopa Prabhu 	int err = 0;
4472d297653dSRoopa Prabhu 	int fidx = 0;
447377162022SJohn Fastabend 
44748c6e137fSDavid Ahern 	if (cb->strict_check)
44758c6e137fSDavid Ahern 		err = valid_fdb_dump_strict(cb->nlh, &br_idx, &brport_idx,
44768c6e137fSDavid Ahern 					    cb->extack);
44778c6e137fSDavid Ahern 	else
44788dfbda19SDavid Ahern 		err = valid_fdb_dump_legacy(cb->nlh, &br_idx, &brport_idx,
44798dfbda19SDavid Ahern 					    cb->extack);
44808dfbda19SDavid Ahern 	if (err < 0)
44818dfbda19SDavid Ahern 		return err;
44825e6d2435SJamal Hadi Salim 
44835e6d2435SJamal Hadi Salim 	if (br_idx) {
44845e6d2435SJamal Hadi Salim 		br_dev = __dev_get_by_index(net, br_idx);
44855e6d2435SJamal Hadi Salim 		if (!br_dev)
44865e6d2435SJamal Hadi Salim 			return -ENODEV;
44875e6d2435SJamal Hadi Salim 
4488898e5061SJiri Pirko 		ops = br_dev->netdev_ops;
44895e6d2435SJamal Hadi Salim 	}
44905e6d2435SJamal Hadi Salim 
4491d297653dSRoopa Prabhu 	s_h = cb->args[0];
4492d297653dSRoopa Prabhu 	s_idx = cb->args[1];
4493d297653dSRoopa Prabhu 
4494d297653dSRoopa Prabhu 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4495d297653dSRoopa Prabhu 		idx = 0;
4496d297653dSRoopa Prabhu 		head = &net->dev_index_head[h];
4497d297653dSRoopa Prabhu 		hlist_for_each_entry(dev, head, index_hlist) {
4498d297653dSRoopa Prabhu 
44995e6d2435SJamal Hadi Salim 			if (brport_idx && (dev->ifindex != brport_idx))
45005e6d2435SJamal Hadi Salim 				continue;
45015e6d2435SJamal Hadi Salim 
45025e6d2435SJamal Hadi Salim 			if (!br_idx) { /* user did not specify a specific bridge */
45032e92a2d0SJulian Wiedmann 				if (netif_is_bridge_port(dev)) {
45045e6d2435SJamal Hadi Salim 					br_dev = netdev_master_upper_dev_get(dev);
45055e6d2435SJamal Hadi Salim 					cops = br_dev->netdev_ops;
45065e6d2435SJamal Hadi Salim 				}
45075e6d2435SJamal Hadi Salim 			} else {
45085e6d2435SJamal Hadi Salim 				if (dev != br_dev &&
45092e92a2d0SJulian Wiedmann 				    !netif_is_bridge_port(dev))
45105e6d2435SJamal Hadi Salim 					continue;
45115e6d2435SJamal Hadi Salim 
45125e6d2435SJamal Hadi Salim 				if (br_dev != netdev_master_upper_dev_get(dev) &&
4513254ec036SKyungrok Chung 				    !netif_is_bridge_master(dev))
45145e6d2435SJamal Hadi Salim 					continue;
45155e6d2435SJamal Hadi Salim 				cops = ops;
45165e6d2435SJamal Hadi Salim 			}
45175e6d2435SJamal Hadi Salim 
4518d297653dSRoopa Prabhu 			if (idx < s_idx)
4519d297653dSRoopa Prabhu 				goto cont;
4520d297653dSRoopa Prabhu 
45212e92a2d0SJulian Wiedmann 			if (netif_is_bridge_port(dev)) {
4522d297653dSRoopa Prabhu 				if (cops && cops->ndo_fdb_dump) {
4523d297653dSRoopa Prabhu 					err = cops->ndo_fdb_dump(skb, cb,
4524d297653dSRoopa Prabhu 								br_dev, dev,
4525d297653dSRoopa Prabhu 								&fidx);
4526d297653dSRoopa Prabhu 					if (err == -EMSGSIZE)
4527d297653dSRoopa Prabhu 						goto out;
452877162022SJohn Fastabend 				}
4529d297653dSRoopa Prabhu 			}
453077162022SJohn Fastabend 
45315e6d2435SJamal Hadi Salim 			if (dev->netdev_ops->ndo_fdb_dump)
4532d297653dSRoopa Prabhu 				err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
4533d297653dSRoopa Prabhu 								    dev, NULL,
4534d297653dSRoopa Prabhu 								    &fidx);
45356cb69742SHubert Sokolowski 			else
4536d297653dSRoopa Prabhu 				err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
4537d297653dSRoopa Prabhu 							&fidx);
4538d297653dSRoopa Prabhu 			if (err == -EMSGSIZE)
4539d297653dSRoopa Prabhu 				goto out;
45405e6d2435SJamal Hadi Salim 
45415e6d2435SJamal Hadi Salim 			cops = NULL;
4542d297653dSRoopa Prabhu 
4543d297653dSRoopa Prabhu 			/* reset fdb offset to 0 for rest of the interfaces */
4544d297653dSRoopa Prabhu 			cb->args[2] = 0;
4545d297653dSRoopa Prabhu 			fidx = 0;
4546d297653dSRoopa Prabhu cont:
4547d297653dSRoopa Prabhu 			idx++;
4548d297653dSRoopa Prabhu 		}
454977162022SJohn Fastabend 	}
455077162022SJohn Fastabend 
4551d297653dSRoopa Prabhu out:
4552d297653dSRoopa Prabhu 	cb->args[0] = h;
4553d297653dSRoopa Prabhu 	cb->args[1] = idx;
4554d297653dSRoopa Prabhu 	cb->args[2] = fidx;
4555d297653dSRoopa Prabhu 
455677162022SJohn Fastabend 	return skb->len;
455777162022SJohn Fastabend }
455877162022SJohn Fastabend 
45595b2f94b2SRoopa Prabhu static int valid_fdb_get_strict(const struct nlmsghdr *nlh,
45605b2f94b2SRoopa Prabhu 				struct nlattr **tb, u8 *ndm_flags,
45615b2f94b2SRoopa Prabhu 				int *br_idx, int *brport_idx, u8 **addr,
45625b2f94b2SRoopa Prabhu 				u16 *vid, struct netlink_ext_ack *extack)
45635b2f94b2SRoopa Prabhu {
45645b2f94b2SRoopa Prabhu 	struct ndmsg *ndm;
45655b2f94b2SRoopa Prabhu 	int err, i;
45665b2f94b2SRoopa Prabhu 
45675b2f94b2SRoopa Prabhu 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
45685b2f94b2SRoopa Prabhu 		NL_SET_ERR_MSG(extack, "Invalid header for fdb get request");
45695b2f94b2SRoopa Prabhu 		return -EINVAL;
45705b2f94b2SRoopa Prabhu 	}
45715b2f94b2SRoopa Prabhu 
45725b2f94b2SRoopa Prabhu 	ndm = nlmsg_data(nlh);
45735b2f94b2SRoopa Prabhu 	if (ndm->ndm_pad1  || ndm->ndm_pad2  || ndm->ndm_state ||
45745b2f94b2SRoopa Prabhu 	    ndm->ndm_type) {
45755b2f94b2SRoopa Prabhu 		NL_SET_ERR_MSG(extack, "Invalid values in header for fdb get request");
45765b2f94b2SRoopa Prabhu 		return -EINVAL;
45775b2f94b2SRoopa Prabhu 	}
45785b2f94b2SRoopa Prabhu 
45795b2f94b2SRoopa Prabhu 	if (ndm->ndm_flags & ~(NTF_MASTER | NTF_SELF)) {
45805b2f94b2SRoopa Prabhu 		NL_SET_ERR_MSG(extack, "Invalid flags in header for fdb get request");
45815b2f94b2SRoopa Prabhu 		return -EINVAL;
45825b2f94b2SRoopa Prabhu 	}
45835b2f94b2SRoopa Prabhu 
45848cb08174SJohannes Berg 	err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
45858cb08174SJohannes Berg 					    NDA_MAX, nda_policy, extack);
45865b2f94b2SRoopa Prabhu 	if (err < 0)
45875b2f94b2SRoopa Prabhu 		return err;
45885b2f94b2SRoopa Prabhu 
45895b2f94b2SRoopa Prabhu 	*ndm_flags = ndm->ndm_flags;
45905b2f94b2SRoopa Prabhu 	*brport_idx = ndm->ndm_ifindex;
45915b2f94b2SRoopa Prabhu 	for (i = 0; i <= NDA_MAX; ++i) {
45925b2f94b2SRoopa Prabhu 		if (!tb[i])
45935b2f94b2SRoopa Prabhu 			continue;
45945b2f94b2SRoopa Prabhu 
45955b2f94b2SRoopa Prabhu 		switch (i) {
45965b2f94b2SRoopa Prabhu 		case NDA_MASTER:
45975b2f94b2SRoopa Prabhu 			*br_idx = nla_get_u32(tb[i]);
45985b2f94b2SRoopa Prabhu 			break;
45995b2f94b2SRoopa Prabhu 		case NDA_LLADDR:
46005b2f94b2SRoopa Prabhu 			if (nla_len(tb[i]) != ETH_ALEN) {
46015b2f94b2SRoopa Prabhu 				NL_SET_ERR_MSG(extack, "Invalid address in fdb get request");
46025b2f94b2SRoopa Prabhu 				return -EINVAL;
46035b2f94b2SRoopa Prabhu 			}
46045b2f94b2SRoopa Prabhu 			*addr = nla_data(tb[i]);
46055b2f94b2SRoopa Prabhu 			break;
46065b2f94b2SRoopa Prabhu 		case NDA_VLAN:
46075b2f94b2SRoopa Prabhu 			err = fdb_vid_parse(tb[i], vid, extack);
46085b2f94b2SRoopa Prabhu 			if (err)
46095b2f94b2SRoopa Prabhu 				return err;
46105b2f94b2SRoopa Prabhu 			break;
46115b2f94b2SRoopa Prabhu 		case NDA_VNI:
46125b2f94b2SRoopa Prabhu 			break;
46135b2f94b2SRoopa Prabhu 		default:
46145b2f94b2SRoopa Prabhu 			NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb get request");
46155b2f94b2SRoopa Prabhu 			return -EINVAL;
46165b2f94b2SRoopa Prabhu 		}
46175b2f94b2SRoopa Prabhu 	}
46185b2f94b2SRoopa Prabhu 
46195b2f94b2SRoopa Prabhu 	return 0;
46205b2f94b2SRoopa Prabhu }
46215b2f94b2SRoopa Prabhu 
46225b2f94b2SRoopa Prabhu static int rtnl_fdb_get(struct sk_buff *in_skb, struct nlmsghdr *nlh,
46235b2f94b2SRoopa Prabhu 			struct netlink_ext_ack *extack)
46245b2f94b2SRoopa Prabhu {
46255b2f94b2SRoopa Prabhu 	struct net_device *dev = NULL, *br_dev = NULL;
46265b2f94b2SRoopa Prabhu 	const struct net_device_ops *ops = NULL;
46275b2f94b2SRoopa Prabhu 	struct net *net = sock_net(in_skb->sk);
46285b2f94b2SRoopa Prabhu 	struct nlattr *tb[NDA_MAX + 1];
46295b2f94b2SRoopa Prabhu 	struct sk_buff *skb;
46305b2f94b2SRoopa Prabhu 	int brport_idx = 0;
46315b2f94b2SRoopa Prabhu 	u8 ndm_flags = 0;
46325b2f94b2SRoopa Prabhu 	int br_idx = 0;
46335b2f94b2SRoopa Prabhu 	u8 *addr = NULL;
46345b2f94b2SRoopa Prabhu 	u16 vid = 0;
46355b2f94b2SRoopa Prabhu 	int err;
46365b2f94b2SRoopa Prabhu 
46375b2f94b2SRoopa Prabhu 	err = valid_fdb_get_strict(nlh, tb, &ndm_flags, &br_idx,
46385b2f94b2SRoopa Prabhu 				   &brport_idx, &addr, &vid, extack);
46395b2f94b2SRoopa Prabhu 	if (err < 0)
46405b2f94b2SRoopa Prabhu 		return err;
46415b2f94b2SRoopa Prabhu 
4642f989d03eSNikolay Aleksandrov 	if (!addr) {
4643f989d03eSNikolay Aleksandrov 		NL_SET_ERR_MSG(extack, "Missing lookup address for fdb get request");
4644f989d03eSNikolay Aleksandrov 		return -EINVAL;
4645f989d03eSNikolay Aleksandrov 	}
4646f989d03eSNikolay Aleksandrov 
46475b2f94b2SRoopa Prabhu 	if (brport_idx) {
46485b2f94b2SRoopa Prabhu 		dev = __dev_get_by_index(net, brport_idx);
46495b2f94b2SRoopa Prabhu 		if (!dev) {
46505b2f94b2SRoopa Prabhu 			NL_SET_ERR_MSG(extack, "Unknown device ifindex");
46515b2f94b2SRoopa Prabhu 			return -ENODEV;
46525b2f94b2SRoopa Prabhu 		}
46535b2f94b2SRoopa Prabhu 	}
46545b2f94b2SRoopa Prabhu 
46555b2f94b2SRoopa Prabhu 	if (br_idx) {
46565b2f94b2SRoopa Prabhu 		if (dev) {
46575b2f94b2SRoopa Prabhu 			NL_SET_ERR_MSG(extack, "Master and device are mutually exclusive");
46585b2f94b2SRoopa Prabhu 			return -EINVAL;
46595b2f94b2SRoopa Prabhu 		}
46605b2f94b2SRoopa Prabhu 
46615b2f94b2SRoopa Prabhu 		br_dev = __dev_get_by_index(net, br_idx);
46625b2f94b2SRoopa Prabhu 		if (!br_dev) {
46635b2f94b2SRoopa Prabhu 			NL_SET_ERR_MSG(extack, "Invalid master ifindex");
46645b2f94b2SRoopa Prabhu 			return -EINVAL;
46655b2f94b2SRoopa Prabhu 		}
46665b2f94b2SRoopa Prabhu 		ops = br_dev->netdev_ops;
46675b2f94b2SRoopa Prabhu 	}
46685b2f94b2SRoopa Prabhu 
46695b2f94b2SRoopa Prabhu 	if (dev) {
46705b2f94b2SRoopa Prabhu 		if (!ndm_flags || (ndm_flags & NTF_MASTER)) {
46712e92a2d0SJulian Wiedmann 			if (!netif_is_bridge_port(dev)) {
46725b2f94b2SRoopa Prabhu 				NL_SET_ERR_MSG(extack, "Device is not a bridge port");
46735b2f94b2SRoopa Prabhu 				return -EINVAL;
46745b2f94b2SRoopa Prabhu 			}
46755b2f94b2SRoopa Prabhu 			br_dev = netdev_master_upper_dev_get(dev);
46765b2f94b2SRoopa Prabhu 			if (!br_dev) {
46775b2f94b2SRoopa Prabhu 				NL_SET_ERR_MSG(extack, "Master of device not found");
46785b2f94b2SRoopa Prabhu 				return -EINVAL;
46795b2f94b2SRoopa Prabhu 			}
46805b2f94b2SRoopa Prabhu 			ops = br_dev->netdev_ops;
46815b2f94b2SRoopa Prabhu 		} else {
46825b2f94b2SRoopa Prabhu 			if (!(ndm_flags & NTF_SELF)) {
46835b2f94b2SRoopa Prabhu 				NL_SET_ERR_MSG(extack, "Missing NTF_SELF");
46845b2f94b2SRoopa Prabhu 				return -EINVAL;
46855b2f94b2SRoopa Prabhu 			}
46865b2f94b2SRoopa Prabhu 			ops = dev->netdev_ops;
46875b2f94b2SRoopa Prabhu 		}
46885b2f94b2SRoopa Prabhu 	}
46895b2f94b2SRoopa Prabhu 
46905b2f94b2SRoopa Prabhu 	if (!br_dev && !dev) {
46915b2f94b2SRoopa Prabhu 		NL_SET_ERR_MSG(extack, "No device specified");
46925b2f94b2SRoopa Prabhu 		return -ENODEV;
46935b2f94b2SRoopa Prabhu 	}
46945b2f94b2SRoopa Prabhu 
46955b2f94b2SRoopa Prabhu 	if (!ops || !ops->ndo_fdb_get) {
46965b2f94b2SRoopa Prabhu 		NL_SET_ERR_MSG(extack, "Fdb get operation not supported by device");
46975b2f94b2SRoopa Prabhu 		return -EOPNOTSUPP;
46985b2f94b2SRoopa Prabhu 	}
46995b2f94b2SRoopa Prabhu 
47005b2f94b2SRoopa Prabhu 	skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
47015b2f94b2SRoopa Prabhu 	if (!skb)
47025b2f94b2SRoopa Prabhu 		return -ENOBUFS;
47035b2f94b2SRoopa Prabhu 
47045b2f94b2SRoopa Prabhu 	if (br_dev)
47055b2f94b2SRoopa Prabhu 		dev = br_dev;
47065b2f94b2SRoopa Prabhu 	err = ops->ndo_fdb_get(skb, tb, dev, addr, vid,
47075b2f94b2SRoopa Prabhu 			       NETLINK_CB(in_skb).portid,
47085b2f94b2SRoopa Prabhu 			       nlh->nlmsg_seq, extack);
47095b2f94b2SRoopa Prabhu 	if (err)
47105b2f94b2SRoopa Prabhu 		goto out;
47115b2f94b2SRoopa Prabhu 
47125b2f94b2SRoopa Prabhu 	return rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
47135b2f94b2SRoopa Prabhu out:
47145b2f94b2SRoopa Prabhu 	kfree_skb(skb);
47155b2f94b2SRoopa Prabhu 	return err;
47165b2f94b2SRoopa Prabhu }
47175b2f94b2SRoopa Prabhu 
47182c3c031cSScott Feldman static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
47192c3c031cSScott Feldman 			       unsigned int attrnum, unsigned int flag)
47202c3c031cSScott Feldman {
47212c3c031cSScott Feldman 	if (mask & flag)
47222c3c031cSScott Feldman 		return nla_put_u8(skb, attrnum, !!(flags & flag));
47232c3c031cSScott Feldman 	return 0;
47242c3c031cSScott Feldman }
47252c3c031cSScott Feldman 
4726815cccbfSJohn Fastabend int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
47272c3c031cSScott Feldman 			    struct net_device *dev, u16 mode,
47287d4f8d87SScott Feldman 			    u32 flags, u32 mask, int nlflags,
47297d4f8d87SScott Feldman 			    u32 filter_mask,
47307d4f8d87SScott Feldman 			    int (*vlan_fill)(struct sk_buff *skb,
47317d4f8d87SScott Feldman 					     struct net_device *dev,
47327d4f8d87SScott Feldman 					     u32 filter_mask))
4733815cccbfSJohn Fastabend {
4734815cccbfSJohn Fastabend 	struct nlmsghdr *nlh;
4735815cccbfSJohn Fastabend 	struct ifinfomsg *ifm;
4736815cccbfSJohn Fastabend 	struct nlattr *br_afspec;
47372c3c031cSScott Feldman 	struct nlattr *protinfo;
4738815cccbfSJohn Fastabend 	u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
4739898e5061SJiri Pirko 	struct net_device *br_dev = netdev_master_upper_dev_get(dev);
47407d4f8d87SScott Feldman 	int err = 0;
4741815cccbfSJohn Fastabend 
474246c264daSNicolas Dichtel 	nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
4743815cccbfSJohn Fastabend 	if (nlh == NULL)
4744815cccbfSJohn Fastabend 		return -EMSGSIZE;
4745815cccbfSJohn Fastabend 
4746815cccbfSJohn Fastabend 	ifm = nlmsg_data(nlh);
4747815cccbfSJohn Fastabend 	ifm->ifi_family = AF_BRIDGE;
4748815cccbfSJohn Fastabend 	ifm->__ifi_pad = 0;
4749815cccbfSJohn Fastabend 	ifm->ifi_type = dev->type;
4750815cccbfSJohn Fastabend 	ifm->ifi_index = dev->ifindex;
4751815cccbfSJohn Fastabend 	ifm->ifi_flags = dev_get_flags(dev);
4752815cccbfSJohn Fastabend 	ifm->ifi_change = 0;
4753815cccbfSJohn Fastabend 
4754815cccbfSJohn Fastabend 
4755815cccbfSJohn Fastabend 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
4756815cccbfSJohn Fastabend 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
4757815cccbfSJohn Fastabend 	    nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
4758898e5061SJiri Pirko 	    (br_dev &&
4759898e5061SJiri Pirko 	     nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
4760815cccbfSJohn Fastabend 	    (dev->addr_len &&
4761815cccbfSJohn Fastabend 	     nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
4762a54acb3aSNicolas Dichtel 	    (dev->ifindex != dev_get_iflink(dev) &&
4763a54acb3aSNicolas Dichtel 	     nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
4764815cccbfSJohn Fastabend 		goto nla_put_failure;
4765815cccbfSJohn Fastabend 
4766ae0be8deSMichal Kubecek 	br_afspec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
4767815cccbfSJohn Fastabend 	if (!br_afspec)
4768815cccbfSJohn Fastabend 		goto nla_put_failure;
4769815cccbfSJohn Fastabend 
47701d460b98SRoopa Prabhu 	if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
4771815cccbfSJohn Fastabend 		nla_nest_cancel(skb, br_afspec);
4772815cccbfSJohn Fastabend 		goto nla_put_failure;
4773815cccbfSJohn Fastabend 	}
47741d460b98SRoopa Prabhu 
47751d460b98SRoopa Prabhu 	if (mode != BRIDGE_MODE_UNDEF) {
47761d460b98SRoopa Prabhu 		if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
47771d460b98SRoopa Prabhu 			nla_nest_cancel(skb, br_afspec);
47781d460b98SRoopa Prabhu 			goto nla_put_failure;
47791d460b98SRoopa Prabhu 		}
47801d460b98SRoopa Prabhu 	}
47817d4f8d87SScott Feldman 	if (vlan_fill) {
47827d4f8d87SScott Feldman 		err = vlan_fill(skb, dev, filter_mask);
47837d4f8d87SScott Feldman 		if (err) {
47847d4f8d87SScott Feldman 			nla_nest_cancel(skb, br_afspec);
47857d4f8d87SScott Feldman 			goto nla_put_failure;
47867d4f8d87SScott Feldman 		}
47877d4f8d87SScott Feldman 	}
4788815cccbfSJohn Fastabend 	nla_nest_end(skb, br_afspec);
4789815cccbfSJohn Fastabend 
4790ae0be8deSMichal Kubecek 	protinfo = nla_nest_start(skb, IFLA_PROTINFO);
47912c3c031cSScott Feldman 	if (!protinfo)
47922c3c031cSScott Feldman 		goto nla_put_failure;
47932c3c031cSScott Feldman 
47942c3c031cSScott Feldman 	if (brport_nla_put_flag(skb, flags, mask,
47952c3c031cSScott Feldman 				IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
47962c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
47972c3c031cSScott Feldman 				IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
47982c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
47992c3c031cSScott Feldman 				IFLA_BRPORT_FAST_LEAVE,
48002c3c031cSScott Feldman 				BR_MULTICAST_FAST_LEAVE) ||
48012c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
48022c3c031cSScott Feldman 				IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
48032c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
48042c3c031cSScott Feldman 				IFLA_BRPORT_LEARNING, BR_LEARNING) ||
48052c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
48062c3c031cSScott Feldman 				IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
48072c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
48082c3c031cSScott Feldman 				IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
48092c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
4810583cb0b4SJulian Wiedmann 				IFLA_BRPORT_PROXYARP, BR_PROXYARP) ||
4811583cb0b4SJulian Wiedmann 	    brport_nla_put_flag(skb, flags, mask,
4812583cb0b4SJulian Wiedmann 				IFLA_BRPORT_MCAST_FLOOD, BR_MCAST_FLOOD) ||
4813583cb0b4SJulian Wiedmann 	    brport_nla_put_flag(skb, flags, mask,
4814583cb0b4SJulian Wiedmann 				IFLA_BRPORT_BCAST_FLOOD, BR_BCAST_FLOOD)) {
48152c3c031cSScott Feldman 		nla_nest_cancel(skb, protinfo);
48162c3c031cSScott Feldman 		goto nla_put_failure;
48172c3c031cSScott Feldman 	}
48182c3c031cSScott Feldman 
48192c3c031cSScott Feldman 	nla_nest_end(skb, protinfo);
48202c3c031cSScott Feldman 
4821053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
4822053c095aSJohannes Berg 	return 0;
4823815cccbfSJohn Fastabend nla_put_failure:
4824815cccbfSJohn Fastabend 	nlmsg_cancel(skb, nlh);
48257d4f8d87SScott Feldman 	return err ? err : -EMSGSIZE;
4826815cccbfSJohn Fastabend }
48277d4f8d87SScott Feldman EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
4828815cccbfSJohn Fastabend 
48292d011be8SDavid Ahern static int valid_bridge_getlink_req(const struct nlmsghdr *nlh,
48302d011be8SDavid Ahern 				    bool strict_check, u32 *filter_mask,
48312d011be8SDavid Ahern 				    struct netlink_ext_ack *extack)
48322d011be8SDavid Ahern {
48332d011be8SDavid Ahern 	struct nlattr *tb[IFLA_MAX+1];
48342d011be8SDavid Ahern 	int err, i;
48352d011be8SDavid Ahern 
48362d011be8SDavid Ahern 	if (strict_check) {
48372d011be8SDavid Ahern 		struct ifinfomsg *ifm;
48382d011be8SDavid Ahern 
48392d011be8SDavid Ahern 		if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
48402d011be8SDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid header for bridge link dump");
48412d011be8SDavid Ahern 			return -EINVAL;
48422d011be8SDavid Ahern 		}
48432d011be8SDavid Ahern 
48442d011be8SDavid Ahern 		ifm = nlmsg_data(nlh);
48452d011be8SDavid Ahern 		if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
48462d011be8SDavid Ahern 		    ifm->ifi_change || ifm->ifi_index) {
48472d011be8SDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid values in header for bridge link dump request");
48482d011be8SDavid Ahern 			return -EINVAL;
48492d011be8SDavid Ahern 		}
48502d011be8SDavid Ahern 
48518cb08174SJohannes Berg 		err = nlmsg_parse_deprecated_strict(nlh,
48528cb08174SJohannes Berg 						    sizeof(struct ifinfomsg),
48538cb08174SJohannes Berg 						    tb, IFLA_MAX, ifla_policy,
48548cb08174SJohannes Berg 						    extack);
48552d011be8SDavid Ahern 	} else {
48568cb08174SJohannes Berg 		err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
48578cb08174SJohannes Berg 					     tb, IFLA_MAX, ifla_policy,
48588cb08174SJohannes Berg 					     extack);
48592d011be8SDavid Ahern 	}
48602d011be8SDavid Ahern 	if (err < 0)
48612d011be8SDavid Ahern 		return err;
48622d011be8SDavid Ahern 
48632d011be8SDavid Ahern 	/* new attributes should only be added with strict checking */
48642d011be8SDavid Ahern 	for (i = 0; i <= IFLA_MAX; ++i) {
48652d011be8SDavid Ahern 		if (!tb[i])
48662d011be8SDavid Ahern 			continue;
48672d011be8SDavid Ahern 
48682d011be8SDavid Ahern 		switch (i) {
48692d011be8SDavid Ahern 		case IFLA_EXT_MASK:
48702d011be8SDavid Ahern 			*filter_mask = nla_get_u32(tb[i]);
48712d011be8SDavid Ahern 			break;
48722d011be8SDavid Ahern 		default:
48732d011be8SDavid Ahern 			if (strict_check) {
48742d011be8SDavid Ahern 				NL_SET_ERR_MSG(extack, "Unsupported attribute in bridge link dump request");
48752d011be8SDavid Ahern 				return -EINVAL;
48762d011be8SDavid Ahern 			}
48772d011be8SDavid Ahern 		}
48782d011be8SDavid Ahern 	}
48792d011be8SDavid Ahern 
48802d011be8SDavid Ahern 	return 0;
48812d011be8SDavid Ahern }
48822d011be8SDavid Ahern 
4883e5a55a89SJohn Fastabend static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
4884e5a55a89SJohn Fastabend {
48852d011be8SDavid Ahern 	const struct nlmsghdr *nlh = cb->nlh;
4886e5a55a89SJohn Fastabend 	struct net *net = sock_net(skb->sk);
4887e5a55a89SJohn Fastabend 	struct net_device *dev;
4888e5a55a89SJohn Fastabend 	int idx = 0;
4889e5a55a89SJohn Fastabend 	u32 portid = NETLINK_CB(cb->skb).portid;
48902d011be8SDavid Ahern 	u32 seq = nlh->nlmsg_seq;
48916cbdceebSVlad Yasevich 	u32 filter_mask = 0;
4892d64f69b0SRoopa Prabhu 	int err;
48936cbdceebSVlad Yasevich 
48942d011be8SDavid Ahern 	err = valid_bridge_getlink_req(nlh, cb->strict_check, &filter_mask,
48952d011be8SDavid Ahern 				       cb->extack);
48962d011be8SDavid Ahern 	if (err < 0 && cb->strict_check)
48972d011be8SDavid Ahern 		return err;
4898e5a55a89SJohn Fastabend 
4899e5a55a89SJohn Fastabend 	rcu_read_lock();
4900e5a55a89SJohn Fastabend 	for_each_netdev_rcu(net, dev) {
4901e5a55a89SJohn Fastabend 		const struct net_device_ops *ops = dev->netdev_ops;
4902898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4903e5a55a89SJohn Fastabend 
4904898e5061SJiri Pirko 		if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
4905d64f69b0SRoopa Prabhu 			if (idx >= cb->args[0]) {
4906d64f69b0SRoopa Prabhu 				err = br_dev->netdev_ops->ndo_bridge_getlink(
4907d64f69b0SRoopa Prabhu 						skb, portid, seq, dev,
4908d64f69b0SRoopa Prabhu 						filter_mask, NLM_F_MULTI);
4909f6c5775fSDavid Ahern 				if (err < 0 && err != -EOPNOTSUPP) {
4910f6c5775fSDavid Ahern 					if (likely(skb->len))
4911e5a55a89SJohn Fastabend 						break;
4912f6c5775fSDavid Ahern 
4913f6c5775fSDavid Ahern 					goto out_err;
4914f6c5775fSDavid Ahern 				}
4915d64f69b0SRoopa Prabhu 			}
4916e5a55a89SJohn Fastabend 			idx++;
4917e5a55a89SJohn Fastabend 		}
4918e5a55a89SJohn Fastabend 
4919e5a55a89SJohn Fastabend 		if (ops->ndo_bridge_getlink) {
4920d64f69b0SRoopa Prabhu 			if (idx >= cb->args[0]) {
4921d64f69b0SRoopa Prabhu 				err = ops->ndo_bridge_getlink(skb, portid,
4922d64f69b0SRoopa Prabhu 							      seq, dev,
492346c264daSNicolas Dichtel 							      filter_mask,
4924d64f69b0SRoopa Prabhu 							      NLM_F_MULTI);
4925f6c5775fSDavid Ahern 				if (err < 0 && err != -EOPNOTSUPP) {
4926f6c5775fSDavid Ahern 					if (likely(skb->len))
4927e5a55a89SJohn Fastabend 						break;
4928f6c5775fSDavid Ahern 
4929f6c5775fSDavid Ahern 					goto out_err;
4930f6c5775fSDavid Ahern 				}
4931d64f69b0SRoopa Prabhu 			}
4932e5a55a89SJohn Fastabend 			idx++;
4933e5a55a89SJohn Fastabend 		}
4934e5a55a89SJohn Fastabend 	}
4935f6c5775fSDavid Ahern 	err = skb->len;
4936f6c5775fSDavid Ahern out_err:
4937e5a55a89SJohn Fastabend 	rcu_read_unlock();
4938e5a55a89SJohn Fastabend 	cb->args[0] = idx;
4939e5a55a89SJohn Fastabend 
4940f6c5775fSDavid Ahern 	return err;
4941e5a55a89SJohn Fastabend }
4942e5a55a89SJohn Fastabend 
49432469ffd7SJohn Fastabend static inline size_t bridge_nlmsg_size(void)
49442469ffd7SJohn Fastabend {
49452469ffd7SJohn Fastabend 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
49462469ffd7SJohn Fastabend 		+ nla_total_size(IFNAMSIZ)	/* IFLA_IFNAME */
49472469ffd7SJohn Fastabend 		+ nla_total_size(MAX_ADDR_LEN)	/* IFLA_ADDRESS */
49482469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_MASTER */
49492469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_MTU */
49502469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_LINK */
49512469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_OPERSTATE */
49522469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u8))	/* IFLA_PROTINFO */
49532469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(struct nlattr))	/* IFLA_AF_SPEC */
49542469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u16))	/* IFLA_BRIDGE_FLAGS */
49552469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u16));	/* IFLA_BRIDGE_MODE */
49562469ffd7SJohn Fastabend }
49572469ffd7SJohn Fastabend 
495802dba438SRoopa Prabhu static int rtnl_bridge_notify(struct net_device *dev)
49592469ffd7SJohn Fastabend {
49602469ffd7SJohn Fastabend 	struct net *net = dev_net(dev);
49612469ffd7SJohn Fastabend 	struct sk_buff *skb;
49622469ffd7SJohn Fastabend 	int err = -EOPNOTSUPP;
49632469ffd7SJohn Fastabend 
496402dba438SRoopa Prabhu 	if (!dev->netdev_ops->ndo_bridge_getlink)
496502dba438SRoopa Prabhu 		return 0;
496602dba438SRoopa Prabhu 
49672469ffd7SJohn Fastabend 	skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
49682469ffd7SJohn Fastabend 	if (!skb) {
49692469ffd7SJohn Fastabend 		err = -ENOMEM;
49702469ffd7SJohn Fastabend 		goto errout;
49712469ffd7SJohn Fastabend 	}
49722469ffd7SJohn Fastabend 
497346c264daSNicolas Dichtel 	err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
4974c38e01b8SJohn Fastabend 	if (err < 0)
4975c38e01b8SJohn Fastabend 		goto errout;
49762469ffd7SJohn Fastabend 
4977d2e381c4SIdo Schimmel 	/* Notification info is only filled for bridge ports, not the bridge
4978d2e381c4SIdo Schimmel 	 * device itself. Therefore, a zero notification length is valid and
4979d2e381c4SIdo Schimmel 	 * should not result in an error.
4980d2e381c4SIdo Schimmel 	 */
4981d2e381c4SIdo Schimmel 	if (!skb->len)
498259ccaaaaSRoopa Prabhu 		goto errout;
498359ccaaaaSRoopa Prabhu 
49842469ffd7SJohn Fastabend 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
49852469ffd7SJohn Fastabend 	return 0;
49862469ffd7SJohn Fastabend errout:
49872469ffd7SJohn Fastabend 	WARN_ON(err == -EMSGSIZE);
49882469ffd7SJohn Fastabend 	kfree_skb(skb);
498959ccaaaaSRoopa Prabhu 	if (err)
49902469ffd7SJohn Fastabend 		rtnl_set_sk_err(net, RTNLGRP_LINK, err);
49912469ffd7SJohn Fastabend 	return err;
49922469ffd7SJohn Fastabend }
49932469ffd7SJohn Fastabend 
4994c21ef3e3SDavid Ahern static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
4995c21ef3e3SDavid Ahern 			       struct netlink_ext_ack *extack)
4996e5a55a89SJohn Fastabend {
4997e5a55a89SJohn Fastabend 	struct net *net = sock_net(skb->sk);
4998e5a55a89SJohn Fastabend 	struct ifinfomsg *ifm;
4999e5a55a89SJohn Fastabend 	struct net_device *dev;
50002469ffd7SJohn Fastabend 	struct nlattr *br_spec, *attr = NULL;
50012469ffd7SJohn Fastabend 	int rem, err = -EOPNOTSUPP;
50024de8b413SRosen, Rami 	u16 flags = 0;
5003c38e01b8SJohn Fastabend 	bool have_flags = false;
5004e5a55a89SJohn Fastabend 
5005e5a55a89SJohn Fastabend 	if (nlmsg_len(nlh) < sizeof(*ifm))
5006e5a55a89SJohn Fastabend 		return -EINVAL;
5007e5a55a89SJohn Fastabend 
5008e5a55a89SJohn Fastabend 	ifm = nlmsg_data(nlh);
5009e5a55a89SJohn Fastabend 	if (ifm->ifi_family != AF_BRIDGE)
5010e5a55a89SJohn Fastabend 		return -EPFNOSUPPORT;
5011e5a55a89SJohn Fastabend 
5012e5a55a89SJohn Fastabend 	dev = __dev_get_by_index(net, ifm->ifi_index);
5013e5a55a89SJohn Fastabend 	if (!dev) {
5014b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "unknown ifindex");
5015e5a55a89SJohn Fastabend 		return -ENODEV;
5016e5a55a89SJohn Fastabend 	}
5017e5a55a89SJohn Fastabend 
50182469ffd7SJohn Fastabend 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
50192469ffd7SJohn Fastabend 	if (br_spec) {
50202469ffd7SJohn Fastabend 		nla_for_each_nested(attr, br_spec, rem) {
50212469ffd7SJohn Fastabend 			if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
50226e8d1c55SThomas Graf 				if (nla_len(attr) < sizeof(flags))
50236e8d1c55SThomas Graf 					return -EINVAL;
50246e8d1c55SThomas Graf 
5025c38e01b8SJohn Fastabend 				have_flags = true;
50262469ffd7SJohn Fastabend 				flags = nla_get_u16(attr);
50272469ffd7SJohn Fastabend 				break;
50282469ffd7SJohn Fastabend 			}
50292469ffd7SJohn Fastabend 		}
50302469ffd7SJohn Fastabend 	}
50312469ffd7SJohn Fastabend 
50322469ffd7SJohn Fastabend 	if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
5033898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
5034898e5061SJiri Pirko 
5035898e5061SJiri Pirko 		if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
50362469ffd7SJohn Fastabend 			err = -EOPNOTSUPP;
5037e5a55a89SJohn Fastabend 			goto out;
5038e5a55a89SJohn Fastabend 		}
5039e5a55a89SJohn Fastabend 
50402fd527b7SPetr Machata 		err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags,
50412fd527b7SPetr Machata 							     extack);
50422469ffd7SJohn Fastabend 		if (err)
50432469ffd7SJohn Fastabend 			goto out;
50442469ffd7SJohn Fastabend 
50452469ffd7SJohn Fastabend 		flags &= ~BRIDGE_FLAGS_MASTER;
50462469ffd7SJohn Fastabend 	}
50472469ffd7SJohn Fastabend 
50482469ffd7SJohn Fastabend 	if ((flags & BRIDGE_FLAGS_SELF)) {
50492469ffd7SJohn Fastabend 		if (!dev->netdev_ops->ndo_bridge_setlink)
50502469ffd7SJohn Fastabend 			err = -EOPNOTSUPP;
50512469ffd7SJohn Fastabend 		else
5052add511b3SRoopa Prabhu 			err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
50532fd527b7SPetr Machata 								  flags,
50542fd527b7SPetr Machata 								  extack);
505502dba438SRoopa Prabhu 		if (!err) {
50562469ffd7SJohn Fastabend 			flags &= ~BRIDGE_FLAGS_SELF;
505702dba438SRoopa Prabhu 
505802dba438SRoopa Prabhu 			/* Generate event to notify upper layer of bridge
505902dba438SRoopa Prabhu 			 * change
506002dba438SRoopa Prabhu 			 */
506102dba438SRoopa Prabhu 			err = rtnl_bridge_notify(dev);
506202dba438SRoopa Prabhu 		}
50632469ffd7SJohn Fastabend 	}
50642469ffd7SJohn Fastabend 
5065c38e01b8SJohn Fastabend 	if (have_flags)
50662469ffd7SJohn Fastabend 		memcpy(nla_data(attr), &flags, sizeof(flags));
5067e5a55a89SJohn Fastabend out:
5068e5a55a89SJohn Fastabend 	return err;
5069e5a55a89SJohn Fastabend }
5070e5a55a89SJohn Fastabend 
5071c21ef3e3SDavid Ahern static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
5072c21ef3e3SDavid Ahern 			       struct netlink_ext_ack *extack)
5073407af329SVlad Yasevich {
5074407af329SVlad Yasevich 	struct net *net = sock_net(skb->sk);
5075407af329SVlad Yasevich 	struct ifinfomsg *ifm;
5076407af329SVlad Yasevich 	struct net_device *dev;
5077407af329SVlad Yasevich 	struct nlattr *br_spec, *attr = NULL;
5078407af329SVlad Yasevich 	int rem, err = -EOPNOTSUPP;
50794de8b413SRosen, Rami 	u16 flags = 0;
5080407af329SVlad Yasevich 	bool have_flags = false;
5081407af329SVlad Yasevich 
5082407af329SVlad Yasevich 	if (nlmsg_len(nlh) < sizeof(*ifm))
5083407af329SVlad Yasevich 		return -EINVAL;
5084407af329SVlad Yasevich 
5085407af329SVlad Yasevich 	ifm = nlmsg_data(nlh);
5086407af329SVlad Yasevich 	if (ifm->ifi_family != AF_BRIDGE)
5087407af329SVlad Yasevich 		return -EPFNOSUPPORT;
5088407af329SVlad Yasevich 
5089407af329SVlad Yasevich 	dev = __dev_get_by_index(net, ifm->ifi_index);
5090407af329SVlad Yasevich 	if (!dev) {
5091b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "unknown ifindex");
5092407af329SVlad Yasevich 		return -ENODEV;
5093407af329SVlad Yasevich 	}
5094407af329SVlad Yasevich 
5095407af329SVlad Yasevich 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
5096407af329SVlad Yasevich 	if (br_spec) {
5097407af329SVlad Yasevich 		nla_for_each_nested(attr, br_spec, rem) {
5098407af329SVlad Yasevich 			if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
50996e8d1c55SThomas Graf 				if (nla_len(attr) < sizeof(flags))
51006e8d1c55SThomas Graf 					return -EINVAL;
51016e8d1c55SThomas Graf 
5102407af329SVlad Yasevich 				have_flags = true;
5103407af329SVlad Yasevich 				flags = nla_get_u16(attr);
5104407af329SVlad Yasevich 				break;
5105407af329SVlad Yasevich 			}
5106407af329SVlad Yasevich 		}
5107407af329SVlad Yasevich 	}
5108407af329SVlad Yasevich 
5109407af329SVlad Yasevich 	if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
5110407af329SVlad Yasevich 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
5111407af329SVlad Yasevich 
5112407af329SVlad Yasevich 		if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
5113407af329SVlad Yasevich 			err = -EOPNOTSUPP;
5114407af329SVlad Yasevich 			goto out;
5115407af329SVlad Yasevich 		}
5116407af329SVlad Yasevich 
5117add511b3SRoopa Prabhu 		err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
5118407af329SVlad Yasevich 		if (err)
5119407af329SVlad Yasevich 			goto out;
5120407af329SVlad Yasevich 
5121407af329SVlad Yasevich 		flags &= ~BRIDGE_FLAGS_MASTER;
5122407af329SVlad Yasevich 	}
5123407af329SVlad Yasevich 
5124407af329SVlad Yasevich 	if ((flags & BRIDGE_FLAGS_SELF)) {
5125407af329SVlad Yasevich 		if (!dev->netdev_ops->ndo_bridge_dellink)
5126407af329SVlad Yasevich 			err = -EOPNOTSUPP;
5127407af329SVlad Yasevich 		else
5128add511b3SRoopa Prabhu 			err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
5129add511b3SRoopa Prabhu 								  flags);
5130407af329SVlad Yasevich 
513102dba438SRoopa Prabhu 		if (!err) {
5132407af329SVlad Yasevich 			flags &= ~BRIDGE_FLAGS_SELF;
513302dba438SRoopa Prabhu 
513402dba438SRoopa Prabhu 			/* Generate event to notify upper layer of bridge
513502dba438SRoopa Prabhu 			 * change
513602dba438SRoopa Prabhu 			 */
513702dba438SRoopa Prabhu 			err = rtnl_bridge_notify(dev);
513802dba438SRoopa Prabhu 		}
5139407af329SVlad Yasevich 	}
5140407af329SVlad Yasevich 
5141407af329SVlad Yasevich 	if (have_flags)
5142407af329SVlad Yasevich 		memcpy(nla_data(attr), &flags, sizeof(flags));
5143407af329SVlad Yasevich out:
5144407af329SVlad Yasevich 	return err;
5145407af329SVlad Yasevich }
5146407af329SVlad Yasevich 
5147e8872a25SNikolay Aleksandrov static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
5148e8872a25SNikolay Aleksandrov {
5149e8872a25SNikolay Aleksandrov 	return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
5150e8872a25SNikolay Aleksandrov 	       (!idxattr || idxattr == attrid);
5151e8872a25SNikolay Aleksandrov }
5152e8872a25SNikolay Aleksandrov 
5153f6e0fb81SPetr Machata static bool
5154f6e0fb81SPetr Machata rtnl_offload_xstats_have_ndo(const struct net_device *dev, int attr_id)
515569ae6ad2SNogah Frankel {
5156f6e0fb81SPetr Machata 	return dev->netdev_ops &&
5157f6e0fb81SPetr Machata 	       dev->netdev_ops->ndo_has_offload_stats &&
5158f6e0fb81SPetr Machata 	       dev->netdev_ops->ndo_get_offload_stats &&
5159f6e0fb81SPetr Machata 	       dev->netdev_ops->ndo_has_offload_stats(dev, attr_id);
516069ae6ad2SNogah Frankel }
516169ae6ad2SNogah Frankel 
5162f6e0fb81SPetr Machata static unsigned int
5163f6e0fb81SPetr Machata rtnl_offload_xstats_get_size_ndo(const struct net_device *dev, int attr_id)
5164f6e0fb81SPetr Machata {
5165f6e0fb81SPetr Machata 	return rtnl_offload_xstats_have_ndo(dev, attr_id) ?
5166f6e0fb81SPetr Machata 	       sizeof(struct rtnl_link_stats64) : 0;
5167f6e0fb81SPetr Machata }
5168f6e0fb81SPetr Machata 
5169f6e0fb81SPetr Machata static int
5170f6e0fb81SPetr Machata rtnl_offload_xstats_fill_ndo(struct net_device *dev, int attr_id,
5171f6e0fb81SPetr Machata 			     struct sk_buff *skb)
5172f6e0fb81SPetr Machata {
5173f6e0fb81SPetr Machata 	unsigned int size = rtnl_offload_xstats_get_size_ndo(dev, attr_id);
5174f6e0fb81SPetr Machata 	struct nlattr *attr = NULL;
5175f6e0fb81SPetr Machata 	void *attr_data;
5176f6e0fb81SPetr Machata 	int err;
5177f6e0fb81SPetr Machata 
5178f6e0fb81SPetr Machata 	if (!size)
5179f6e0fb81SPetr Machata 		return -ENODATA;
5180f6e0fb81SPetr Machata 
5181f6e0fb81SPetr Machata 	attr = nla_reserve_64bit(skb, attr_id, size,
5182f6e0fb81SPetr Machata 				 IFLA_OFFLOAD_XSTATS_UNSPEC);
5183f6e0fb81SPetr Machata 	if (!attr)
5184f6e0fb81SPetr Machata 		return -EMSGSIZE;
5185f6e0fb81SPetr Machata 
5186f6e0fb81SPetr Machata 	attr_data = nla_data(attr);
5187f6e0fb81SPetr Machata 	memset(attr_data, 0, size);
5188f6e0fb81SPetr Machata 
5189f6e0fb81SPetr Machata 	err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev, attr_data);
5190f6e0fb81SPetr Machata 	if (err)
5191f6e0fb81SPetr Machata 		return err;
5192f6e0fb81SPetr Machata 
519369ae6ad2SNogah Frankel 	return 0;
519469ae6ad2SNogah Frankel }
519569ae6ad2SNogah Frankel 
51960e7788fdSPetr Machata static unsigned int
51970e7788fdSPetr Machata rtnl_offload_xstats_get_size_stats(const struct net_device *dev,
51980e7788fdSPetr Machata 				   enum netdev_offload_xstats_type type)
51990e7788fdSPetr Machata {
52000e7788fdSPetr Machata 	bool enabled = netdev_offload_xstats_enabled(dev, type);
52010e7788fdSPetr Machata 
52020e7788fdSPetr Machata 	return enabled ? sizeof(struct rtnl_hw_stats64) : 0;
52030e7788fdSPetr Machata }
52040e7788fdSPetr Machata 
52050e7788fdSPetr Machata struct rtnl_offload_xstats_request_used {
52060e7788fdSPetr Machata 	bool request;
52070e7788fdSPetr Machata 	bool used;
52080e7788fdSPetr Machata };
52090e7788fdSPetr Machata 
52100e7788fdSPetr Machata static int
52110e7788fdSPetr Machata rtnl_offload_xstats_get_stats(struct net_device *dev,
52120e7788fdSPetr Machata 			      enum netdev_offload_xstats_type type,
52130e7788fdSPetr Machata 			      struct rtnl_offload_xstats_request_used *ru,
52140e7788fdSPetr Machata 			      struct rtnl_hw_stats64 *stats,
52150e7788fdSPetr Machata 			      struct netlink_ext_ack *extack)
52160e7788fdSPetr Machata {
52170e7788fdSPetr Machata 	bool request;
52180e7788fdSPetr Machata 	bool used;
52190e7788fdSPetr Machata 	int err;
52200e7788fdSPetr Machata 
52210e7788fdSPetr Machata 	request = netdev_offload_xstats_enabled(dev, type);
52220e7788fdSPetr Machata 	if (!request) {
52230e7788fdSPetr Machata 		used = false;
52240e7788fdSPetr Machata 		goto out;
52250e7788fdSPetr Machata 	}
52260e7788fdSPetr Machata 
52270e7788fdSPetr Machata 	err = netdev_offload_xstats_get(dev, type, stats, &used, extack);
52280e7788fdSPetr Machata 	if (err)
52290e7788fdSPetr Machata 		return err;
52300e7788fdSPetr Machata 
52310e7788fdSPetr Machata out:
52320e7788fdSPetr Machata 	if (ru) {
52330e7788fdSPetr Machata 		ru->request = request;
52340e7788fdSPetr Machata 		ru->used = used;
52350e7788fdSPetr Machata 	}
52360e7788fdSPetr Machata 	return 0;
52370e7788fdSPetr Machata }
52380e7788fdSPetr Machata 
52390e7788fdSPetr Machata static int
52400e7788fdSPetr Machata rtnl_offload_xstats_fill_hw_s_info_one(struct sk_buff *skb, int attr_id,
52410e7788fdSPetr Machata 				       struct rtnl_offload_xstats_request_used *ru)
52420e7788fdSPetr Machata {
52430e7788fdSPetr Machata 	struct nlattr *nest;
52440e7788fdSPetr Machata 
52450e7788fdSPetr Machata 	nest = nla_nest_start(skb, attr_id);
52460e7788fdSPetr Machata 	if (!nest)
52470e7788fdSPetr Machata 		return -EMSGSIZE;
52480e7788fdSPetr Machata 
52490e7788fdSPetr Machata 	if (nla_put_u8(skb, IFLA_OFFLOAD_XSTATS_HW_S_INFO_REQUEST, ru->request))
52500e7788fdSPetr Machata 		goto nla_put_failure;
52510e7788fdSPetr Machata 
52520e7788fdSPetr Machata 	if (nla_put_u8(skb, IFLA_OFFLOAD_XSTATS_HW_S_INFO_USED, ru->used))
52530e7788fdSPetr Machata 		goto nla_put_failure;
52540e7788fdSPetr Machata 
52550e7788fdSPetr Machata 	nla_nest_end(skb, nest);
52560e7788fdSPetr Machata 	return 0;
52570e7788fdSPetr Machata 
52580e7788fdSPetr Machata nla_put_failure:
52590e7788fdSPetr Machata 	nla_nest_cancel(skb, nest);
52600e7788fdSPetr Machata 	return -EMSGSIZE;
52610e7788fdSPetr Machata }
52620e7788fdSPetr Machata 
52630e7788fdSPetr Machata static int
52640e7788fdSPetr Machata rtnl_offload_xstats_fill_hw_s_info(struct sk_buff *skb, struct net_device *dev,
52650e7788fdSPetr Machata 				   struct netlink_ext_ack *extack)
52660e7788fdSPetr Machata {
52670e7788fdSPetr Machata 	enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3;
52680e7788fdSPetr Machata 	struct rtnl_offload_xstats_request_used ru_l3;
52690e7788fdSPetr Machata 	struct nlattr *nest;
52700e7788fdSPetr Machata 	int err;
52710e7788fdSPetr Machata 
52720e7788fdSPetr Machata 	err = rtnl_offload_xstats_get_stats(dev, t_l3, &ru_l3, NULL, extack);
52730e7788fdSPetr Machata 	if (err)
52740e7788fdSPetr Machata 		return err;
52750e7788fdSPetr Machata 
52760e7788fdSPetr Machata 	nest = nla_nest_start(skb, IFLA_OFFLOAD_XSTATS_HW_S_INFO);
52770e7788fdSPetr Machata 	if (!nest)
52780e7788fdSPetr Machata 		return -EMSGSIZE;
52790e7788fdSPetr Machata 
52800e7788fdSPetr Machata 	if (rtnl_offload_xstats_fill_hw_s_info_one(skb,
52810e7788fdSPetr Machata 						   IFLA_OFFLOAD_XSTATS_L3_STATS,
52820e7788fdSPetr Machata 						   &ru_l3))
52830e7788fdSPetr Machata 		goto nla_put_failure;
52840e7788fdSPetr Machata 
52850e7788fdSPetr Machata 	nla_nest_end(skb, nest);
52860e7788fdSPetr Machata 	return 0;
52870e7788fdSPetr Machata 
52880e7788fdSPetr Machata nla_put_failure:
52890e7788fdSPetr Machata 	nla_nest_cancel(skb, nest);
52900e7788fdSPetr Machata 	return -EMSGSIZE;
52910e7788fdSPetr Machata }
52920e7788fdSPetr Machata 
52936b524a1dSPetr Machata static int rtnl_offload_xstats_fill(struct sk_buff *skb, struct net_device *dev,
529405415bccSPetr Machata 				    int *prividx, u32 off_filter_mask,
529505415bccSPetr Machata 				    struct netlink_ext_ack *extack)
529669ae6ad2SNogah Frankel {
52970e7788fdSPetr Machata 	enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3;
52980e7788fdSPetr Machata 	int attr_id_hw_s_info = IFLA_OFFLOAD_XSTATS_HW_S_INFO;
52990e7788fdSPetr Machata 	int attr_id_l3_stats = IFLA_OFFLOAD_XSTATS_L3_STATS;
5300f6e0fb81SPetr Machata 	int attr_id_cpu_hit = IFLA_OFFLOAD_XSTATS_CPU_HIT;
5301f6e0fb81SPetr Machata 	bool have_data = false;
530269ae6ad2SNogah Frankel 	int err;
530369ae6ad2SNogah Frankel 
530446efc97bSPetr Machata 	if (*prividx <= attr_id_cpu_hit &&
530546efc97bSPetr Machata 	    (off_filter_mask &
530646efc97bSPetr Machata 	     IFLA_STATS_FILTER_BIT(attr_id_cpu_hit))) {
5307f6e0fb81SPetr Machata 		err = rtnl_offload_xstats_fill_ndo(dev, attr_id_cpu_hit, skb);
5308f6e0fb81SPetr Machata 		if (!err) {
5309f6e0fb81SPetr Machata 			have_data = true;
5310f6e0fb81SPetr Machata 		} else if (err != -ENODATA) {
5311f6e0fb81SPetr Machata 			*prividx = attr_id_cpu_hit;
5312f6e0fb81SPetr Machata 			return err;
5313f6e0fb81SPetr Machata 		}
531469ae6ad2SNogah Frankel 	}
531569ae6ad2SNogah Frankel 
53160e7788fdSPetr Machata 	if (*prividx <= attr_id_hw_s_info &&
53170e7788fdSPetr Machata 	    (off_filter_mask & IFLA_STATS_FILTER_BIT(attr_id_hw_s_info))) {
53180e7788fdSPetr Machata 		*prividx = attr_id_hw_s_info;
53190e7788fdSPetr Machata 
53200e7788fdSPetr Machata 		err = rtnl_offload_xstats_fill_hw_s_info(skb, dev, extack);
53210e7788fdSPetr Machata 		if (err)
53220e7788fdSPetr Machata 			return err;
53230e7788fdSPetr Machata 
53240e7788fdSPetr Machata 		have_data = true;
53250e7788fdSPetr Machata 		*prividx = 0;
53260e7788fdSPetr Machata 	}
53270e7788fdSPetr Machata 
53280e7788fdSPetr Machata 	if (*prividx <= attr_id_l3_stats &&
53290e7788fdSPetr Machata 	    (off_filter_mask & IFLA_STATS_FILTER_BIT(attr_id_l3_stats))) {
53300e7788fdSPetr Machata 		unsigned int size_l3;
53310e7788fdSPetr Machata 		struct nlattr *attr;
53320e7788fdSPetr Machata 
53330e7788fdSPetr Machata 		*prividx = attr_id_l3_stats;
53340e7788fdSPetr Machata 
53350e7788fdSPetr Machata 		size_l3 = rtnl_offload_xstats_get_size_stats(dev, t_l3);
533623cfe941SPetr Machata 		if (!size_l3)
533723cfe941SPetr Machata 			goto skip_l3_stats;
53380e7788fdSPetr Machata 		attr = nla_reserve_64bit(skb, attr_id_l3_stats, size_l3,
53390e7788fdSPetr Machata 					 IFLA_OFFLOAD_XSTATS_UNSPEC);
53400e7788fdSPetr Machata 		if (!attr)
53410e7788fdSPetr Machata 			return -EMSGSIZE;
53420e7788fdSPetr Machata 
53430e7788fdSPetr Machata 		err = rtnl_offload_xstats_get_stats(dev, t_l3, NULL,
53440e7788fdSPetr Machata 						    nla_data(attr), extack);
53450e7788fdSPetr Machata 		if (err)
53460e7788fdSPetr Machata 			return err;
53470e7788fdSPetr Machata 
53480e7788fdSPetr Machata 		have_data = true;
534923cfe941SPetr Machata skip_l3_stats:
53500e7788fdSPetr Machata 		*prividx = 0;
53510e7788fdSPetr Machata 	}
53520e7788fdSPetr Machata 
5353f6e0fb81SPetr Machata 	if (!have_data)
535469ae6ad2SNogah Frankel 		return -ENODATA;
535569ae6ad2SNogah Frankel 
535669ae6ad2SNogah Frankel 	*prividx = 0;
535769ae6ad2SNogah Frankel 	return 0;
535869ae6ad2SNogah Frankel }
535969ae6ad2SNogah Frankel 
53600e7788fdSPetr Machata static unsigned int
53610e7788fdSPetr Machata rtnl_offload_xstats_get_size_hw_s_info_one(const struct net_device *dev,
53620e7788fdSPetr Machata 					   enum netdev_offload_xstats_type type)
53630e7788fdSPetr Machata {
53640e7788fdSPetr Machata 	bool enabled = netdev_offload_xstats_enabled(dev, type);
53650e7788fdSPetr Machata 
53660e7788fdSPetr Machata 	return nla_total_size(0) +
53670e7788fdSPetr Machata 		/* IFLA_OFFLOAD_XSTATS_HW_S_INFO_REQUEST */
53680e7788fdSPetr Machata 		nla_total_size(sizeof(u8)) +
53690e7788fdSPetr Machata 		/* IFLA_OFFLOAD_XSTATS_HW_S_INFO_USED */
53700e7788fdSPetr Machata 		(enabled ? nla_total_size(sizeof(u8)) : 0) +
53710e7788fdSPetr Machata 		0;
53720e7788fdSPetr Machata }
53730e7788fdSPetr Machata 
53740e7788fdSPetr Machata static unsigned int
53750e7788fdSPetr Machata rtnl_offload_xstats_get_size_hw_s_info(const struct net_device *dev)
53760e7788fdSPetr Machata {
53770e7788fdSPetr Machata 	enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3;
53780e7788fdSPetr Machata 
53790e7788fdSPetr Machata 	return nla_total_size(0) +
53800e7788fdSPetr Machata 		/* IFLA_OFFLOAD_XSTATS_L3_STATS */
53810e7788fdSPetr Machata 		rtnl_offload_xstats_get_size_hw_s_info_one(dev, t_l3) +
53820e7788fdSPetr Machata 		0;
53830e7788fdSPetr Machata }
53840e7788fdSPetr Machata 
538546efc97bSPetr Machata static int rtnl_offload_xstats_get_size(const struct net_device *dev,
538646efc97bSPetr Machata 					u32 off_filter_mask)
538769ae6ad2SNogah Frankel {
53880e7788fdSPetr Machata 	enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3;
5389f6e0fb81SPetr Machata 	int attr_id_cpu_hit = IFLA_OFFLOAD_XSTATS_CPU_HIT;
539069ae6ad2SNogah Frankel 	int nla_size = 0;
539169ae6ad2SNogah Frankel 	int size;
539269ae6ad2SNogah Frankel 
539346efc97bSPetr Machata 	if (off_filter_mask &
539446efc97bSPetr Machata 	    IFLA_STATS_FILTER_BIT(attr_id_cpu_hit)) {
5395f6e0fb81SPetr Machata 		size = rtnl_offload_xstats_get_size_ndo(dev, attr_id_cpu_hit);
539669ae6ad2SNogah Frankel 		nla_size += nla_total_size_64bit(size);
539746efc97bSPetr Machata 	}
539869ae6ad2SNogah Frankel 
53990e7788fdSPetr Machata 	if (off_filter_mask &
54000e7788fdSPetr Machata 	    IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_HW_S_INFO))
54010e7788fdSPetr Machata 		nla_size += rtnl_offload_xstats_get_size_hw_s_info(dev);
54020e7788fdSPetr Machata 
54030e7788fdSPetr Machata 	if (off_filter_mask &
54040e7788fdSPetr Machata 	    IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_L3_STATS)) {
54050e7788fdSPetr Machata 		size = rtnl_offload_xstats_get_size_stats(dev, t_l3);
54060e7788fdSPetr Machata 		nla_size += nla_total_size_64bit(size);
54070e7788fdSPetr Machata 	}
54080e7788fdSPetr Machata 
540969ae6ad2SNogah Frankel 	if (nla_size != 0)
541069ae6ad2SNogah Frankel 		nla_size += nla_total_size(0);
541169ae6ad2SNogah Frankel 
541269ae6ad2SNogah Frankel 	return nla_size;
541369ae6ad2SNogah Frankel }
541469ae6ad2SNogah Frankel 
541546efc97bSPetr Machata struct rtnl_stats_dump_filters {
541646efc97bSPetr Machata 	/* mask[0] filters outer attributes. Then individual nests have their
541746efc97bSPetr Machata 	 * filtering mask at the index of the nested attribute.
541846efc97bSPetr Machata 	 */
541946efc97bSPetr Machata 	u32 mask[IFLA_STATS_MAX + 1];
542046efc97bSPetr Machata };
542146efc97bSPetr Machata 
542210c9ead9SRoopa Prabhu static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
542310c9ead9SRoopa Prabhu 			       int type, u32 pid, u32 seq, u32 change,
542446efc97bSPetr Machata 			       unsigned int flags,
542546efc97bSPetr Machata 			       const struct rtnl_stats_dump_filters *filters,
542605415bccSPetr Machata 			       int *idxattr, int *prividx,
542705415bccSPetr Machata 			       struct netlink_ext_ack *extack)
542810c9ead9SRoopa Prabhu {
542946efc97bSPetr Machata 	unsigned int filter_mask = filters->mask[0];
543010c9ead9SRoopa Prabhu 	struct if_stats_msg *ifsm;
543110c9ead9SRoopa Prabhu 	struct nlmsghdr *nlh;
543210c9ead9SRoopa Prabhu 	struct nlattr *attr;
5433e8872a25SNikolay Aleksandrov 	int s_prividx = *prividx;
543469ae6ad2SNogah Frankel 	int err;
543510c9ead9SRoopa Prabhu 
543610c9ead9SRoopa Prabhu 	ASSERT_RTNL();
543710c9ead9SRoopa Prabhu 
543810c9ead9SRoopa Prabhu 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
543910c9ead9SRoopa Prabhu 	if (!nlh)
544010c9ead9SRoopa Prabhu 		return -EMSGSIZE;
544110c9ead9SRoopa Prabhu 
544210c9ead9SRoopa Prabhu 	ifsm = nlmsg_data(nlh);
5443ce024f42SNikolay Aleksandrov 	ifsm->family = PF_UNSPEC;
5444ce024f42SNikolay Aleksandrov 	ifsm->pad1 = 0;
5445ce024f42SNikolay Aleksandrov 	ifsm->pad2 = 0;
544610c9ead9SRoopa Prabhu 	ifsm->ifindex = dev->ifindex;
544710c9ead9SRoopa Prabhu 	ifsm->filter_mask = filter_mask;
544810c9ead9SRoopa Prabhu 
5449e8872a25SNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
545010c9ead9SRoopa Prabhu 		struct rtnl_link_stats64 *sp;
545110c9ead9SRoopa Prabhu 
545258414d32SNicolas Dichtel 		attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
545358414d32SNicolas Dichtel 					 sizeof(struct rtnl_link_stats64),
545458414d32SNicolas Dichtel 					 IFLA_STATS_UNSPEC);
5455216e6906SPetr Machata 		if (!attr) {
5456216e6906SPetr Machata 			err = -EMSGSIZE;
545710c9ead9SRoopa Prabhu 			goto nla_put_failure;
5458216e6906SPetr Machata 		}
545910c9ead9SRoopa Prabhu 
546010c9ead9SRoopa Prabhu 		sp = nla_data(attr);
546110c9ead9SRoopa Prabhu 		dev_get_stats(dev, sp);
546210c9ead9SRoopa Prabhu 	}
546310c9ead9SRoopa Prabhu 
546497a47facSNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
546597a47facSNikolay Aleksandrov 		const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
546697a47facSNikolay Aleksandrov 
546797a47facSNikolay Aleksandrov 		if (ops && ops->fill_linkxstats) {
546897a47facSNikolay Aleksandrov 			*idxattr = IFLA_STATS_LINK_XSTATS;
5469ae0be8deSMichal Kubecek 			attr = nla_nest_start_noflag(skb,
547097a47facSNikolay Aleksandrov 						     IFLA_STATS_LINK_XSTATS);
5471216e6906SPetr Machata 			if (!attr) {
5472216e6906SPetr Machata 				err = -EMSGSIZE;
547397a47facSNikolay Aleksandrov 				goto nla_put_failure;
5474216e6906SPetr Machata 			}
547597a47facSNikolay Aleksandrov 
547680e73cc5SNikolay Aleksandrov 			err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
547780e73cc5SNikolay Aleksandrov 			nla_nest_end(skb, attr);
547880e73cc5SNikolay Aleksandrov 			if (err)
547980e73cc5SNikolay Aleksandrov 				goto nla_put_failure;
548080e73cc5SNikolay Aleksandrov 			*idxattr = 0;
548180e73cc5SNikolay Aleksandrov 		}
548280e73cc5SNikolay Aleksandrov 	}
548380e73cc5SNikolay Aleksandrov 
548480e73cc5SNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
548580e73cc5SNikolay Aleksandrov 			     *idxattr)) {
548680e73cc5SNikolay Aleksandrov 		const struct rtnl_link_ops *ops = NULL;
548780e73cc5SNikolay Aleksandrov 		const struct net_device *master;
548880e73cc5SNikolay Aleksandrov 
548980e73cc5SNikolay Aleksandrov 		master = netdev_master_upper_dev_get(dev);
549080e73cc5SNikolay Aleksandrov 		if (master)
549180e73cc5SNikolay Aleksandrov 			ops = master->rtnl_link_ops;
549280e73cc5SNikolay Aleksandrov 		if (ops && ops->fill_linkxstats) {
549380e73cc5SNikolay Aleksandrov 			*idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
5494ae0be8deSMichal Kubecek 			attr = nla_nest_start_noflag(skb,
549580e73cc5SNikolay Aleksandrov 						     IFLA_STATS_LINK_XSTATS_SLAVE);
5496216e6906SPetr Machata 			if (!attr) {
5497216e6906SPetr Machata 				err = -EMSGSIZE;
549880e73cc5SNikolay Aleksandrov 				goto nla_put_failure;
5499216e6906SPetr Machata 			}
550080e73cc5SNikolay Aleksandrov 
550180e73cc5SNikolay Aleksandrov 			err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
550297a47facSNikolay Aleksandrov 			nla_nest_end(skb, attr);
550397a47facSNikolay Aleksandrov 			if (err)
550497a47facSNikolay Aleksandrov 				goto nla_put_failure;
550597a47facSNikolay Aleksandrov 			*idxattr = 0;
550697a47facSNikolay Aleksandrov 		}
550797a47facSNikolay Aleksandrov 	}
550897a47facSNikolay Aleksandrov 
550969ae6ad2SNogah Frankel 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
551069ae6ad2SNogah Frankel 			     *idxattr)) {
551146efc97bSPetr Machata 		u32 off_filter_mask;
551246efc97bSPetr Machata 
551346efc97bSPetr Machata 		off_filter_mask = filters->mask[IFLA_STATS_LINK_OFFLOAD_XSTATS];
551469ae6ad2SNogah Frankel 		*idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
5515ae0be8deSMichal Kubecek 		attr = nla_nest_start_noflag(skb,
5516ae0be8deSMichal Kubecek 					     IFLA_STATS_LINK_OFFLOAD_XSTATS);
5517216e6906SPetr Machata 		if (!attr) {
5518216e6906SPetr Machata 			err = -EMSGSIZE;
551969ae6ad2SNogah Frankel 			goto nla_put_failure;
5520216e6906SPetr Machata 		}
552169ae6ad2SNogah Frankel 
552246efc97bSPetr Machata 		err = rtnl_offload_xstats_fill(skb, dev, prividx,
552305415bccSPetr Machata 					       off_filter_mask, extack);
552469ae6ad2SNogah Frankel 		if (err == -ENODATA)
552569ae6ad2SNogah Frankel 			nla_nest_cancel(skb, attr);
552669ae6ad2SNogah Frankel 		else
552769ae6ad2SNogah Frankel 			nla_nest_end(skb, attr);
552869ae6ad2SNogah Frankel 
552969ae6ad2SNogah Frankel 		if (err && err != -ENODATA)
553069ae6ad2SNogah Frankel 			goto nla_put_failure;
553169ae6ad2SNogah Frankel 		*idxattr = 0;
553269ae6ad2SNogah Frankel 	}
553369ae6ad2SNogah Frankel 
5534aefb4d4aSRobert Shearman 	if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
5535aefb4d4aSRobert Shearman 		struct rtnl_af_ops *af_ops;
5536aefb4d4aSRobert Shearman 
5537aefb4d4aSRobert Shearman 		*idxattr = IFLA_STATS_AF_SPEC;
5538ae0be8deSMichal Kubecek 		attr = nla_nest_start_noflag(skb, IFLA_STATS_AF_SPEC);
5539216e6906SPetr Machata 		if (!attr) {
5540216e6906SPetr Machata 			err = -EMSGSIZE;
5541aefb4d4aSRobert Shearman 			goto nla_put_failure;
5542216e6906SPetr Machata 		}
5543aefb4d4aSRobert Shearman 
55445fa85a09SFlorian Westphal 		rcu_read_lock();
55455fa85a09SFlorian Westphal 		list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
5546aefb4d4aSRobert Shearman 			if (af_ops->fill_stats_af) {
5547aefb4d4aSRobert Shearman 				struct nlattr *af;
5548aefb4d4aSRobert Shearman 
5549ae0be8deSMichal Kubecek 				af = nla_nest_start_noflag(skb,
5550ae0be8deSMichal Kubecek 							   af_ops->family);
55515fa85a09SFlorian Westphal 				if (!af) {
55525fa85a09SFlorian Westphal 					rcu_read_unlock();
555357d29a29STom Rix 					err = -EMSGSIZE;
5554aefb4d4aSRobert Shearman 					goto nla_put_failure;
55555fa85a09SFlorian Westphal 				}
5556aefb4d4aSRobert Shearman 				err = af_ops->fill_stats_af(skb, dev);
5557aefb4d4aSRobert Shearman 
55585fa85a09SFlorian Westphal 				if (err == -ENODATA) {
5559aefb4d4aSRobert Shearman 					nla_nest_cancel(skb, af);
55605fa85a09SFlorian Westphal 				} else if (err < 0) {
55615fa85a09SFlorian Westphal 					rcu_read_unlock();
5562aefb4d4aSRobert Shearman 					goto nla_put_failure;
55635fa85a09SFlorian Westphal 				}
5564aefb4d4aSRobert Shearman 
5565aefb4d4aSRobert Shearman 				nla_nest_end(skb, af);
5566aefb4d4aSRobert Shearman 			}
5567aefb4d4aSRobert Shearman 		}
55685fa85a09SFlorian Westphal 		rcu_read_unlock();
5569aefb4d4aSRobert Shearman 
5570aefb4d4aSRobert Shearman 		nla_nest_end(skb, attr);
5571aefb4d4aSRobert Shearman 
5572aefb4d4aSRobert Shearman 		*idxattr = 0;
5573aefb4d4aSRobert Shearman 	}
5574aefb4d4aSRobert Shearman 
557510c9ead9SRoopa Prabhu 	nlmsg_end(skb, nlh);
557610c9ead9SRoopa Prabhu 
557710c9ead9SRoopa Prabhu 	return 0;
557810c9ead9SRoopa Prabhu 
557910c9ead9SRoopa Prabhu nla_put_failure:
5580e8872a25SNikolay Aleksandrov 	/* not a multi message or no progress mean a real error */
5581e8872a25SNikolay Aleksandrov 	if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
558210c9ead9SRoopa Prabhu 		nlmsg_cancel(skb, nlh);
5583e8872a25SNikolay Aleksandrov 	else
5584e8872a25SNikolay Aleksandrov 		nlmsg_end(skb, nlh);
558510c9ead9SRoopa Prabhu 
5586216e6906SPetr Machata 	return err;
558710c9ead9SRoopa Prabhu }
558810c9ead9SRoopa Prabhu 
558910c9ead9SRoopa Prabhu static size_t if_nlmsg_stats_size(const struct net_device *dev,
559046efc97bSPetr Machata 				  const struct rtnl_stats_dump_filters *filters)
559110c9ead9SRoopa Prabhu {
5592d3436799SEric Dumazet 	size_t size = NLMSG_ALIGN(sizeof(struct if_stats_msg));
559346efc97bSPetr Machata 	unsigned int filter_mask = filters->mask[0];
559410c9ead9SRoopa Prabhu 
5595e8872a25SNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
559610c9ead9SRoopa Prabhu 		size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
559710c9ead9SRoopa Prabhu 
559897a47facSNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
559997a47facSNikolay Aleksandrov 		const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
560080e73cc5SNikolay Aleksandrov 		int attr = IFLA_STATS_LINK_XSTATS;
560197a47facSNikolay Aleksandrov 
560297a47facSNikolay Aleksandrov 		if (ops && ops->get_linkxstats_size) {
560380e73cc5SNikolay Aleksandrov 			size += nla_total_size(ops->get_linkxstats_size(dev,
560480e73cc5SNikolay Aleksandrov 									attr));
560597a47facSNikolay Aleksandrov 			/* for IFLA_STATS_LINK_XSTATS */
560697a47facSNikolay Aleksandrov 			size += nla_total_size(0);
560797a47facSNikolay Aleksandrov 		}
560897a47facSNikolay Aleksandrov 	}
560997a47facSNikolay Aleksandrov 
561080e73cc5SNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
561180e73cc5SNikolay Aleksandrov 		struct net_device *_dev = (struct net_device *)dev;
561280e73cc5SNikolay Aleksandrov 		const struct rtnl_link_ops *ops = NULL;
561380e73cc5SNikolay Aleksandrov 		const struct net_device *master;
561480e73cc5SNikolay Aleksandrov 
561580e73cc5SNikolay Aleksandrov 		/* netdev_master_upper_dev_get can't take const */
561680e73cc5SNikolay Aleksandrov 		master = netdev_master_upper_dev_get(_dev);
561780e73cc5SNikolay Aleksandrov 		if (master)
561880e73cc5SNikolay Aleksandrov 			ops = master->rtnl_link_ops;
561980e73cc5SNikolay Aleksandrov 		if (ops && ops->get_linkxstats_size) {
562080e73cc5SNikolay Aleksandrov 			int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
562180e73cc5SNikolay Aleksandrov 
562280e73cc5SNikolay Aleksandrov 			size += nla_total_size(ops->get_linkxstats_size(dev,
562380e73cc5SNikolay Aleksandrov 									attr));
562480e73cc5SNikolay Aleksandrov 			/* for IFLA_STATS_LINK_XSTATS_SLAVE */
562580e73cc5SNikolay Aleksandrov 			size += nla_total_size(0);
562680e73cc5SNikolay Aleksandrov 		}
562780e73cc5SNikolay Aleksandrov 	}
562880e73cc5SNikolay Aleksandrov 
562946efc97bSPetr Machata 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0)) {
563046efc97bSPetr Machata 		u32 off_filter_mask;
563146efc97bSPetr Machata 
563246efc97bSPetr Machata 		off_filter_mask = filters->mask[IFLA_STATS_LINK_OFFLOAD_XSTATS];
563346efc97bSPetr Machata 		size += rtnl_offload_xstats_get_size(dev, off_filter_mask);
563446efc97bSPetr Machata 	}
563569ae6ad2SNogah Frankel 
5636aefb4d4aSRobert Shearman 	if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
5637aefb4d4aSRobert Shearman 		struct rtnl_af_ops *af_ops;
5638aefb4d4aSRobert Shearman 
5639aefb4d4aSRobert Shearman 		/* for IFLA_STATS_AF_SPEC */
5640aefb4d4aSRobert Shearman 		size += nla_total_size(0);
5641aefb4d4aSRobert Shearman 
56425fa85a09SFlorian Westphal 		rcu_read_lock();
56435fa85a09SFlorian Westphal 		list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
5644aefb4d4aSRobert Shearman 			if (af_ops->get_stats_af_size) {
5645aefb4d4aSRobert Shearman 				size += nla_total_size(
5646aefb4d4aSRobert Shearman 					af_ops->get_stats_af_size(dev));
5647aefb4d4aSRobert Shearman 
5648aefb4d4aSRobert Shearman 				/* for AF_* */
5649aefb4d4aSRobert Shearman 				size += nla_total_size(0);
5650aefb4d4aSRobert Shearman 			}
5651aefb4d4aSRobert Shearman 		}
56525fa85a09SFlorian Westphal 		rcu_read_unlock();
5653aefb4d4aSRobert Shearman 	}
5654aefb4d4aSRobert Shearman 
565510c9ead9SRoopa Prabhu 	return size;
565610c9ead9SRoopa Prabhu }
565710c9ead9SRoopa Prabhu 
565846efc97bSPetr Machata #define RTNL_STATS_OFFLOAD_XSTATS_VALID ((1 << __IFLA_OFFLOAD_XSTATS_MAX) - 1)
565946efc97bSPetr Machata 
566046efc97bSPetr Machata static const struct nla_policy
566146efc97bSPetr Machata rtnl_stats_get_policy_filters[IFLA_STATS_MAX + 1] = {
566246efc97bSPetr Machata 	[IFLA_STATS_LINK_OFFLOAD_XSTATS] =
566346efc97bSPetr Machata 		    NLA_POLICY_MASK(NLA_U32, RTNL_STATS_OFFLOAD_XSTATS_VALID),
566446efc97bSPetr Machata };
566546efc97bSPetr Machata 
566646efc97bSPetr Machata static const struct nla_policy
566746efc97bSPetr Machata rtnl_stats_get_policy[IFLA_STATS_GETSET_MAX + 1] = {
566846efc97bSPetr Machata 	[IFLA_STATS_GET_FILTERS] =
566946efc97bSPetr Machata 		    NLA_POLICY_NESTED(rtnl_stats_get_policy_filters),
567046efc97bSPetr Machata };
567146efc97bSPetr Machata 
567203ba3566SPetr Machata static const struct nla_policy
567303ba3566SPetr Machata ifla_stats_set_policy[IFLA_STATS_GETSET_MAX + 1] = {
56745fd0b838SPetr Machata 	[IFLA_STATS_SET_OFFLOAD_XSTATS_L3_STATS] = NLA_POLICY_MAX(NLA_U8, 1),
567503ba3566SPetr Machata };
567603ba3566SPetr Machata 
567746efc97bSPetr Machata static int rtnl_stats_get_parse_filters(struct nlattr *ifla_filters,
567846efc97bSPetr Machata 					struct rtnl_stats_dump_filters *filters,
567946efc97bSPetr Machata 					struct netlink_ext_ack *extack)
568046efc97bSPetr Machata {
568146efc97bSPetr Machata 	struct nlattr *tb[IFLA_STATS_MAX + 1];
568246efc97bSPetr Machata 	int err;
568346efc97bSPetr Machata 	int at;
568446efc97bSPetr Machata 
568546efc97bSPetr Machata 	err = nla_parse_nested(tb, IFLA_STATS_MAX, ifla_filters,
568646efc97bSPetr Machata 			       rtnl_stats_get_policy_filters, extack);
568746efc97bSPetr Machata 	if (err < 0)
568846efc97bSPetr Machata 		return err;
568946efc97bSPetr Machata 
569046efc97bSPetr Machata 	for (at = 1; at <= IFLA_STATS_MAX; at++) {
569146efc97bSPetr Machata 		if (tb[at]) {
569246efc97bSPetr Machata 			if (!(filters->mask[0] & IFLA_STATS_FILTER_BIT(at))) {
569346efc97bSPetr Machata 				NL_SET_ERR_MSG(extack, "Filtered attribute not enabled in filter_mask");
569446efc97bSPetr Machata 				return -EINVAL;
569546efc97bSPetr Machata 			}
569646efc97bSPetr Machata 			filters->mask[at] = nla_get_u32(tb[at]);
569746efc97bSPetr Machata 		}
569846efc97bSPetr Machata 	}
569946efc97bSPetr Machata 
570046efc97bSPetr Machata 	return 0;
570146efc97bSPetr Machata }
570246efc97bSPetr Machata 
570346efc97bSPetr Machata static int rtnl_stats_get_parse(const struct nlmsghdr *nlh,
570446efc97bSPetr Machata 				u32 filter_mask,
570546efc97bSPetr Machata 				struct rtnl_stats_dump_filters *filters,
570646efc97bSPetr Machata 				struct netlink_ext_ack *extack)
570746efc97bSPetr Machata {
570846efc97bSPetr Machata 	struct nlattr *tb[IFLA_STATS_GETSET_MAX + 1];
570946efc97bSPetr Machata 	int err;
571046efc97bSPetr Machata 	int i;
571146efc97bSPetr Machata 
571246efc97bSPetr Machata 	filters->mask[0] = filter_mask;
571346efc97bSPetr Machata 	for (i = 1; i < ARRAY_SIZE(filters->mask); i++)
571446efc97bSPetr Machata 		filters->mask[i] = -1U;
571546efc97bSPetr Machata 
571646efc97bSPetr Machata 	err = nlmsg_parse(nlh, sizeof(struct if_stats_msg), tb,
571746efc97bSPetr Machata 			  IFLA_STATS_GETSET_MAX, rtnl_stats_get_policy, extack);
571846efc97bSPetr Machata 	if (err < 0)
571946efc97bSPetr Machata 		return err;
572046efc97bSPetr Machata 
572146efc97bSPetr Machata 	if (tb[IFLA_STATS_GET_FILTERS]) {
572246efc97bSPetr Machata 		err = rtnl_stats_get_parse_filters(tb[IFLA_STATS_GET_FILTERS],
572346efc97bSPetr Machata 						   filters, extack);
572446efc97bSPetr Machata 		if (err)
572546efc97bSPetr Machata 			return err;
572646efc97bSPetr Machata 	}
572746efc97bSPetr Machata 
572846efc97bSPetr Machata 	return 0;
572946efc97bSPetr Machata }
573046efc97bSPetr Machata 
573151bc860dSJakub Kicinski static int rtnl_valid_stats_req(const struct nlmsghdr *nlh, bool strict_check,
573251bc860dSJakub Kicinski 				bool is_dump, struct netlink_ext_ack *extack)
573351bc860dSJakub Kicinski {
573451bc860dSJakub Kicinski 	struct if_stats_msg *ifsm;
573551bc860dSJakub Kicinski 
573669f23a09SEric Dumazet 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifsm))) {
573751bc860dSJakub Kicinski 		NL_SET_ERR_MSG(extack, "Invalid header for stats dump");
573851bc860dSJakub Kicinski 		return -EINVAL;
573951bc860dSJakub Kicinski 	}
574051bc860dSJakub Kicinski 
574151bc860dSJakub Kicinski 	if (!strict_check)
574251bc860dSJakub Kicinski 		return 0;
574351bc860dSJakub Kicinski 
574451bc860dSJakub Kicinski 	ifsm = nlmsg_data(nlh);
574551bc860dSJakub Kicinski 
574651bc860dSJakub Kicinski 	/* only requests using strict checks can pass data to influence
574751bc860dSJakub Kicinski 	 * the dump. The legacy exception is filter_mask.
574851bc860dSJakub Kicinski 	 */
574951bc860dSJakub Kicinski 	if (ifsm->pad1 || ifsm->pad2 || (is_dump && ifsm->ifindex)) {
575051bc860dSJakub Kicinski 		NL_SET_ERR_MSG(extack, "Invalid values in header for stats dump request");
575151bc860dSJakub Kicinski 		return -EINVAL;
575251bc860dSJakub Kicinski 	}
57536300acb2SJakub Kicinski 	if (ifsm->filter_mask >= IFLA_STATS_FILTER_BIT(IFLA_STATS_MAX + 1)) {
57546300acb2SJakub Kicinski 		NL_SET_ERR_MSG(extack, "Invalid stats requested through filter mask");
57556300acb2SJakub Kicinski 		return -EINVAL;
57566300acb2SJakub Kicinski 	}
575751bc860dSJakub Kicinski 
575851bc860dSJakub Kicinski 	return 0;
575951bc860dSJakub Kicinski }
576051bc860dSJakub Kicinski 
5761c21ef3e3SDavid Ahern static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
5762c21ef3e3SDavid Ahern 			  struct netlink_ext_ack *extack)
576310c9ead9SRoopa Prabhu {
576446efc97bSPetr Machata 	struct rtnl_stats_dump_filters filters;
576510c9ead9SRoopa Prabhu 	struct net *net = sock_net(skb->sk);
576610c9ead9SRoopa Prabhu 	struct net_device *dev = NULL;
5767e8872a25SNikolay Aleksandrov 	int idxattr = 0, prividx = 0;
5768e8872a25SNikolay Aleksandrov 	struct if_stats_msg *ifsm;
576910c9ead9SRoopa Prabhu 	struct sk_buff *nskb;
577010c9ead9SRoopa Prabhu 	int err;
577110c9ead9SRoopa Prabhu 
577251bc860dSJakub Kicinski 	err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb),
577351bc860dSJakub Kicinski 				   false, extack);
577451bc860dSJakub Kicinski 	if (err)
577551bc860dSJakub Kicinski 		return err;
57764775cc1fSMathias Krause 
577710c9ead9SRoopa Prabhu 	ifsm = nlmsg_data(nlh);
577810c9ead9SRoopa Prabhu 	if (ifsm->ifindex > 0)
577910c9ead9SRoopa Prabhu 		dev = __dev_get_by_index(net, ifsm->ifindex);
578010c9ead9SRoopa Prabhu 	else
578110c9ead9SRoopa Prabhu 		return -EINVAL;
578210c9ead9SRoopa Prabhu 
578310c9ead9SRoopa Prabhu 	if (!dev)
578410c9ead9SRoopa Prabhu 		return -ENODEV;
578510c9ead9SRoopa Prabhu 
578646efc97bSPetr Machata 	if (!ifsm->filter_mask) {
578722b67d17SPetr Machata 		NL_SET_ERR_MSG(extack, "Filter mask must be set for stats get");
578810c9ead9SRoopa Prabhu 		return -EINVAL;
578922b67d17SPetr Machata 	}
579010c9ead9SRoopa Prabhu 
579146efc97bSPetr Machata 	err = rtnl_stats_get_parse(nlh, ifsm->filter_mask, &filters, extack);
579246efc97bSPetr Machata 	if (err)
579346efc97bSPetr Machata 		return err;
579446efc97bSPetr Machata 
579546efc97bSPetr Machata 	nskb = nlmsg_new(if_nlmsg_stats_size(dev, &filters), GFP_KERNEL);
579610c9ead9SRoopa Prabhu 	if (!nskb)
579710c9ead9SRoopa Prabhu 		return -ENOBUFS;
579810c9ead9SRoopa Prabhu 
579910c9ead9SRoopa Prabhu 	err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
580010c9ead9SRoopa Prabhu 				  NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
580105415bccSPetr Machata 				  0, &filters, &idxattr, &prividx, extack);
580210c9ead9SRoopa Prabhu 	if (err < 0) {
580310c9ead9SRoopa Prabhu 		/* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
580410c9ead9SRoopa Prabhu 		WARN_ON(err == -EMSGSIZE);
580510c9ead9SRoopa Prabhu 		kfree_skb(nskb);
580610c9ead9SRoopa Prabhu 	} else {
580710c9ead9SRoopa Prabhu 		err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
580810c9ead9SRoopa Prabhu 	}
580910c9ead9SRoopa Prabhu 
581010c9ead9SRoopa Prabhu 	return err;
581110c9ead9SRoopa Prabhu }
581210c9ead9SRoopa Prabhu 
581310c9ead9SRoopa Prabhu static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
581410c9ead9SRoopa Prabhu {
5815841891ecSDavid Ahern 	struct netlink_ext_ack *extack = cb->extack;
5816e8872a25SNikolay Aleksandrov 	int h, s_h, err, s_idx, s_idxattr, s_prividx;
581746efc97bSPetr Machata 	struct rtnl_stats_dump_filters filters;
581810c9ead9SRoopa Prabhu 	struct net *net = sock_net(skb->sk);
581910c9ead9SRoopa Prabhu 	unsigned int flags = NLM_F_MULTI;
5820e8872a25SNikolay Aleksandrov 	struct if_stats_msg *ifsm;
5821e8872a25SNikolay Aleksandrov 	struct hlist_head *head;
5822e8872a25SNikolay Aleksandrov 	struct net_device *dev;
5823e8872a25SNikolay Aleksandrov 	int idx = 0;
582410c9ead9SRoopa Prabhu 
582510c9ead9SRoopa Prabhu 	s_h = cb->args[0];
582610c9ead9SRoopa Prabhu 	s_idx = cb->args[1];
5827e8872a25SNikolay Aleksandrov 	s_idxattr = cb->args[2];
5828e8872a25SNikolay Aleksandrov 	s_prividx = cb->args[3];
582910c9ead9SRoopa Prabhu 
583010c9ead9SRoopa Prabhu 	cb->seq = net->dev_base_seq;
583110c9ead9SRoopa Prabhu 
583251bc860dSJakub Kicinski 	err = rtnl_valid_stats_req(cb->nlh, cb->strict_check, true, extack);
583351bc860dSJakub Kicinski 	if (err)
583451bc860dSJakub Kicinski 		return err;
58354775cc1fSMathias Krause 
583610c9ead9SRoopa Prabhu 	ifsm = nlmsg_data(cb->nlh);
583746efc97bSPetr Machata 	if (!ifsm->filter_mask) {
5838841891ecSDavid Ahern 		NL_SET_ERR_MSG(extack, "Filter mask must be set for stats dump");
5839841891ecSDavid Ahern 		return -EINVAL;
5840841891ecSDavid Ahern 	}
584110c9ead9SRoopa Prabhu 
584246efc97bSPetr Machata 	err = rtnl_stats_get_parse(cb->nlh, ifsm->filter_mask, &filters,
584346efc97bSPetr Machata 				   extack);
584446efc97bSPetr Machata 	if (err)
584546efc97bSPetr Machata 		return err;
584646efc97bSPetr Machata 
584710c9ead9SRoopa Prabhu 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
584810c9ead9SRoopa Prabhu 		idx = 0;
584910c9ead9SRoopa Prabhu 		head = &net->dev_index_head[h];
585010c9ead9SRoopa Prabhu 		hlist_for_each_entry(dev, head, index_hlist) {
585110c9ead9SRoopa Prabhu 			if (idx < s_idx)
585210c9ead9SRoopa Prabhu 				goto cont;
585310c9ead9SRoopa Prabhu 			err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
585410c9ead9SRoopa Prabhu 						  NETLINK_CB(cb->skb).portid,
585510c9ead9SRoopa Prabhu 						  cb->nlh->nlmsg_seq, 0,
585646efc97bSPetr Machata 						  flags, &filters,
585705415bccSPetr Machata 						  &s_idxattr, &s_prividx,
585805415bccSPetr Machata 						  extack);
585910c9ead9SRoopa Prabhu 			/* If we ran out of room on the first message,
586010c9ead9SRoopa Prabhu 			 * we're in trouble
586110c9ead9SRoopa Prabhu 			 */
586210c9ead9SRoopa Prabhu 			WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
586310c9ead9SRoopa Prabhu 
586410c9ead9SRoopa Prabhu 			if (err < 0)
586510c9ead9SRoopa Prabhu 				goto out;
5866e8872a25SNikolay Aleksandrov 			s_prividx = 0;
5867e8872a25SNikolay Aleksandrov 			s_idxattr = 0;
586810c9ead9SRoopa Prabhu 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
586910c9ead9SRoopa Prabhu cont:
587010c9ead9SRoopa Prabhu 			idx++;
587110c9ead9SRoopa Prabhu 		}
587210c9ead9SRoopa Prabhu 	}
587310c9ead9SRoopa Prabhu out:
5874e8872a25SNikolay Aleksandrov 	cb->args[3] = s_prividx;
5875e8872a25SNikolay Aleksandrov 	cb->args[2] = s_idxattr;
587610c9ead9SRoopa Prabhu 	cb->args[1] = idx;
587710c9ead9SRoopa Prabhu 	cb->args[0] = h;
587810c9ead9SRoopa Prabhu 
587910c9ead9SRoopa Prabhu 	return skb->len;
588010c9ead9SRoopa Prabhu }
588110c9ead9SRoopa Prabhu 
58825fd0b838SPetr Machata void rtnl_offload_xstats_notify(struct net_device *dev)
58835fd0b838SPetr Machata {
58845fd0b838SPetr Machata 	struct rtnl_stats_dump_filters response_filters = {};
58855fd0b838SPetr Machata 	struct net *net = dev_net(dev);
58865fd0b838SPetr Machata 	int idxattr = 0, prividx = 0;
58875fd0b838SPetr Machata 	struct sk_buff *skb;
58885fd0b838SPetr Machata 	int err = -ENOBUFS;
58895fd0b838SPetr Machata 
58905fd0b838SPetr Machata 	ASSERT_RTNL();
58915fd0b838SPetr Machata 
58925fd0b838SPetr Machata 	response_filters.mask[0] |=
58935fd0b838SPetr Machata 		IFLA_STATS_FILTER_BIT(IFLA_STATS_LINK_OFFLOAD_XSTATS);
58945fd0b838SPetr Machata 	response_filters.mask[IFLA_STATS_LINK_OFFLOAD_XSTATS] |=
58955fd0b838SPetr Machata 		IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_HW_S_INFO);
58965fd0b838SPetr Machata 
58975fd0b838SPetr Machata 	skb = nlmsg_new(if_nlmsg_stats_size(dev, &response_filters),
58985fd0b838SPetr Machata 			GFP_KERNEL);
58995fd0b838SPetr Machata 	if (!skb)
59005fd0b838SPetr Machata 		goto errout;
59015fd0b838SPetr Machata 
59025fd0b838SPetr Machata 	err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS, 0, 0, 0, 0,
59035fd0b838SPetr Machata 				  &response_filters, &idxattr, &prividx, NULL);
59045fd0b838SPetr Machata 	if (err < 0) {
59055fd0b838SPetr Machata 		kfree_skb(skb);
59065fd0b838SPetr Machata 		goto errout;
59075fd0b838SPetr Machata 	}
59085fd0b838SPetr Machata 
59095fd0b838SPetr Machata 	rtnl_notify(skb, net, 0, RTNLGRP_STATS, NULL, GFP_KERNEL);
59105fd0b838SPetr Machata 	return;
59115fd0b838SPetr Machata 
59125fd0b838SPetr Machata errout:
59135fd0b838SPetr Machata 	rtnl_set_sk_err(net, RTNLGRP_STATS, err);
59145fd0b838SPetr Machata }
59155fd0b838SPetr Machata EXPORT_SYMBOL(rtnl_offload_xstats_notify);
59165fd0b838SPetr Machata 
591703ba3566SPetr Machata static int rtnl_stats_set(struct sk_buff *skb, struct nlmsghdr *nlh,
591803ba3566SPetr Machata 			  struct netlink_ext_ack *extack)
591903ba3566SPetr Machata {
59205fd0b838SPetr Machata 	enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3;
592103ba3566SPetr Machata 	struct rtnl_stats_dump_filters response_filters = {};
592203ba3566SPetr Machata 	struct nlattr *tb[IFLA_STATS_GETSET_MAX + 1];
592303ba3566SPetr Machata 	struct net *net = sock_net(skb->sk);
592403ba3566SPetr Machata 	struct net_device *dev = NULL;
592503ba3566SPetr Machata 	struct if_stats_msg *ifsm;
59265fd0b838SPetr Machata 	bool notify = false;
592703ba3566SPetr Machata 	int err;
592803ba3566SPetr Machata 
592903ba3566SPetr Machata 	err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb),
593003ba3566SPetr Machata 				   false, extack);
593103ba3566SPetr Machata 	if (err)
593203ba3566SPetr Machata 		return err;
593303ba3566SPetr Machata 
593403ba3566SPetr Machata 	ifsm = nlmsg_data(nlh);
593503ba3566SPetr Machata 	if (ifsm->family != AF_UNSPEC) {
593603ba3566SPetr Machata 		NL_SET_ERR_MSG(extack, "Address family should be AF_UNSPEC");
593703ba3566SPetr Machata 		return -EINVAL;
593803ba3566SPetr Machata 	}
593903ba3566SPetr Machata 
594003ba3566SPetr Machata 	if (ifsm->ifindex > 0)
594103ba3566SPetr Machata 		dev = __dev_get_by_index(net, ifsm->ifindex);
594203ba3566SPetr Machata 	else
594303ba3566SPetr Machata 		return -EINVAL;
594403ba3566SPetr Machata 
594503ba3566SPetr Machata 	if (!dev)
594603ba3566SPetr Machata 		return -ENODEV;
594703ba3566SPetr Machata 
594803ba3566SPetr Machata 	if (ifsm->filter_mask) {
594903ba3566SPetr Machata 		NL_SET_ERR_MSG(extack, "Filter mask must be 0 for stats set");
595003ba3566SPetr Machata 		return -EINVAL;
595103ba3566SPetr Machata 	}
595203ba3566SPetr Machata 
595303ba3566SPetr Machata 	err = nlmsg_parse(nlh, sizeof(*ifsm), tb, IFLA_STATS_GETSET_MAX,
595403ba3566SPetr Machata 			  ifla_stats_set_policy, extack);
595503ba3566SPetr Machata 	if (err < 0)
595603ba3566SPetr Machata 		return err;
595703ba3566SPetr Machata 
59585fd0b838SPetr Machata 	if (tb[IFLA_STATS_SET_OFFLOAD_XSTATS_L3_STATS]) {
59595fd0b838SPetr Machata 		u8 req = nla_get_u8(tb[IFLA_STATS_SET_OFFLOAD_XSTATS_L3_STATS]);
596003ba3566SPetr Machata 
59615fd0b838SPetr Machata 		if (req)
59625fd0b838SPetr Machata 			err = netdev_offload_xstats_enable(dev, t_l3, extack);
59635fd0b838SPetr Machata 		else
59645fd0b838SPetr Machata 			err = netdev_offload_xstats_disable(dev, t_l3);
59655fd0b838SPetr Machata 
59665fd0b838SPetr Machata 		if (!err)
59675fd0b838SPetr Machata 			notify = true;
59685fd0b838SPetr Machata 		else if (err != -EALREADY)
59695fd0b838SPetr Machata 			return err;
59705fd0b838SPetr Machata 
59715fd0b838SPetr Machata 		response_filters.mask[0] |=
59725fd0b838SPetr Machata 			IFLA_STATS_FILTER_BIT(IFLA_STATS_LINK_OFFLOAD_XSTATS);
59735fd0b838SPetr Machata 		response_filters.mask[IFLA_STATS_LINK_OFFLOAD_XSTATS] |=
59745fd0b838SPetr Machata 			IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_HW_S_INFO);
597503ba3566SPetr Machata 	}
597603ba3566SPetr Machata 
59775fd0b838SPetr Machata 	if (notify)
59785fd0b838SPetr Machata 		rtnl_offload_xstats_notify(dev);
59795fd0b838SPetr Machata 
59805fd0b838SPetr Machata 	return 0;
598103ba3566SPetr Machata }
598203ba3566SPetr Machata 
59831da177e4SLinus Torvalds /* Process one rtnetlink message. */
59841da177e4SLinus Torvalds 
59852d4bc933SJohannes Berg static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
59862d4bc933SJohannes Berg 			     struct netlink_ext_ack *extack)
59871da177e4SLinus Torvalds {
59883b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
5989addf9b90SFlorian Westphal 	struct rtnl_link *link;
599012dc5c2cSNikolay Aleksandrov 	enum rtnl_kinds kind;
5991e4202511SFlorian Westphal 	struct module *owner;
59926853dd48SFlorian Westphal 	int err = -EOPNOTSUPP;
5993e2849863SThomas Graf 	rtnl_doit_func doit;
599462256f98SFlorian Westphal 	unsigned int flags;
59951da177e4SLinus Torvalds 	int family;
59961da177e4SLinus Torvalds 	int type;
59971da177e4SLinus Torvalds 
59981da177e4SLinus Torvalds 	type = nlh->nlmsg_type;
59991da177e4SLinus Torvalds 	if (type > RTM_MAX)
6000038890feSThomas Graf 		return -EOPNOTSUPP;
60011da177e4SLinus Torvalds 
60021da177e4SLinus Torvalds 	type -= RTM_BASE;
60031da177e4SLinus Torvalds 
60041da177e4SLinus Torvalds 	/* All the messages must have at least 1 byte length */
6005573ce260SHong zhi guo 	if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
60061da177e4SLinus Torvalds 		return 0;
60071da177e4SLinus Torvalds 
6008573ce260SHong zhi guo 	family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
60092e9ea3e3SNikolay Aleksandrov 	kind = rtnl_msgtype_kind(type);
60101da177e4SLinus Torvalds 
601112dc5c2cSNikolay Aleksandrov 	if (kind != RTNL_KIND_GET && !netlink_net_capable(skb, CAP_NET_ADMIN))
60121d00a4ebSThomas Graf 		return -EPERM;
60131da177e4SLinus Torvalds 
60146853dd48SFlorian Westphal 	rcu_read_lock();
601512dc5c2cSNikolay Aleksandrov 	if (kind == RTNL_KIND_GET && (nlh->nlmsg_flags & NLM_F_DUMP)) {
601697c53cacSDenis V. Lunev 		struct sock *rtnl;
6017e2849863SThomas Graf 		rtnl_dumpit_func dumpit;
6018ebfe3c51SDi Zhu 		u32 min_dump_alloc = 0;
60191da177e4SLinus Torvalds 
6020addf9b90SFlorian Westphal 		link = rtnl_get_link(family, type);
6021addf9b90SFlorian Westphal 		if (!link || !link->dumpit) {
60226853dd48SFlorian Westphal 			family = PF_UNSPEC;
6023addf9b90SFlorian Westphal 			link = rtnl_get_link(family, type);
6024addf9b90SFlorian Westphal 			if (!link || !link->dumpit)
60256853dd48SFlorian Westphal 				goto err_unlock;
60266853dd48SFlorian Westphal 		}
6027e4202511SFlorian Westphal 		owner = link->owner;
6028addf9b90SFlorian Westphal 		dumpit = link->dumpit;
60296853dd48SFlorian Westphal 
60305c2bb9b6SFlorian Westphal 		if (type == RTM_GETLINK - RTM_BASE)
6031e1fa6d21SFlorian Westphal 			min_dump_alloc = rtnl_calcit(skb, nlh);
60321da177e4SLinus Torvalds 
6033e4202511SFlorian Westphal 		err = 0;
6034e4202511SFlorian Westphal 		/* need to do this before rcu_read_unlock() */
6035e4202511SFlorian Westphal 		if (!try_module_get(owner))
6036e4202511SFlorian Westphal 			err = -EPROTONOSUPPORT;
6037e4202511SFlorian Westphal 
60386853dd48SFlorian Westphal 		rcu_read_unlock();
60396853dd48SFlorian Westphal 
604097c53cacSDenis V. Lunev 		rtnl = net->rtnl;
6041e4202511SFlorian Westphal 		if (err == 0) {
604280d326faSPablo Neira Ayuso 			struct netlink_dump_control c = {
604380d326faSPablo Neira Ayuso 				.dump		= dumpit,
604480d326faSPablo Neira Ayuso 				.min_dump_alloc	= min_dump_alloc,
6045e4202511SFlorian Westphal 				.module		= owner,
604680d326faSPablo Neira Ayuso 			};
604780d326faSPablo Neira Ayuso 			err = netlink_dump_start(rtnl, skb, nlh, &c);
6048e4202511SFlorian Westphal 			/* netlink_dump_start() will keep a reference on
6049e4202511SFlorian Westphal 			 * module if dump is still in progress.
6050e4202511SFlorian Westphal 			 */
6051e4202511SFlorian Westphal 			module_put(owner);
605280d326faSPablo Neira Ayuso 		}
60532907c35fSEric Dumazet 		return err;
60541da177e4SLinus Torvalds 	}
60551da177e4SLinus Torvalds 
6056addf9b90SFlorian Westphal 	link = rtnl_get_link(family, type);
6057addf9b90SFlorian Westphal 	if (!link || !link->doit) {
60588caa38b5SFlorian Westphal 		family = PF_UNSPEC;
6059addf9b90SFlorian Westphal 		link = rtnl_get_link(PF_UNSPEC, type);
6060addf9b90SFlorian Westphal 		if (!link || !link->doit)
6061addf9b90SFlorian Westphal 			goto out_unlock;
60628caa38b5SFlorian Westphal 	}
60638caa38b5SFlorian Westphal 
6064e4202511SFlorian Westphal 	owner = link->owner;
6065e4202511SFlorian Westphal 	if (!try_module_get(owner)) {
6066e4202511SFlorian Westphal 		err = -EPROTONOSUPPORT;
6067e4202511SFlorian Westphal 		goto out_unlock;
6068e4202511SFlorian Westphal 	}
6069e4202511SFlorian Westphal 
6070addf9b90SFlorian Westphal 	flags = link->flags;
6071a6cec0bcSNikolay Aleksandrov 	if (kind == RTNL_KIND_DEL && (nlh->nlmsg_flags & NLM_F_BULK) &&
6072a6cec0bcSNikolay Aleksandrov 	    !(flags & RTNL_FLAG_BULK_DEL_SUPPORTED)) {
6073a6cec0bcSNikolay Aleksandrov 		NL_SET_ERR_MSG(extack, "Bulk delete is not supported");
60745b22f627SZhengchao Shao 		module_put(owner);
6075a6cec0bcSNikolay Aleksandrov 		goto err_unlock;
6076a6cec0bcSNikolay Aleksandrov 	}
6077a6cec0bcSNikolay Aleksandrov 
607862256f98SFlorian Westphal 	if (flags & RTNL_FLAG_DOIT_UNLOCKED) {
6079addf9b90SFlorian Westphal 		doit = link->doit;
608062256f98SFlorian Westphal 		rcu_read_unlock();
608162256f98SFlorian Westphal 		if (doit)
608262256f98SFlorian Westphal 			err = doit(skb, nlh, extack);
6083e4202511SFlorian Westphal 		module_put(owner);
608462256f98SFlorian Westphal 		return err;
608562256f98SFlorian Westphal 	}
60866853dd48SFlorian Westphal 	rcu_read_unlock();
60876853dd48SFlorian Westphal 
60880cc09020SFlorian Westphal 	rtnl_lock();
6089addf9b90SFlorian Westphal 	link = rtnl_get_link(family, type);
6090addf9b90SFlorian Westphal 	if (link && link->doit)
6091addf9b90SFlorian Westphal 		err = link->doit(skb, nlh, extack);
60920cc09020SFlorian Westphal 	rtnl_unlock();
6093addf9b90SFlorian Westphal 
6094e4202511SFlorian Westphal 	module_put(owner);
6095e4202511SFlorian Westphal 
6096addf9b90SFlorian Westphal 	return err;
6097addf9b90SFlorian Westphal 
6098addf9b90SFlorian Westphal out_unlock:
6099addf9b90SFlorian Westphal 	rcu_read_unlock();
61000cc09020SFlorian Westphal 	return err;
61010cc09020SFlorian Westphal 
61020cc09020SFlorian Westphal err_unlock:
61036853dd48SFlorian Westphal 	rcu_read_unlock();
61040cc09020SFlorian Westphal 	return -EOPNOTSUPP;
61051da177e4SLinus Torvalds }
61061da177e4SLinus Torvalds 
6107cd40b7d3SDenis V. Lunev static void rtnetlink_rcv(struct sk_buff *skb)
61081da177e4SLinus Torvalds {
6109cd40b7d3SDenis V. Lunev 	netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
61101da177e4SLinus Torvalds }
61111da177e4SLinus Torvalds 
61125f729eaaSJulien Gomes static int rtnetlink_bind(struct net *net, int group)
61135f729eaaSJulien Gomes {
61145f729eaaSJulien Gomes 	switch (group) {
61155f729eaaSJulien Gomes 	case RTNLGRP_IPV4_MROUTE_R:
61165f729eaaSJulien Gomes 	case RTNLGRP_IPV6_MROUTE_R:
61175f729eaaSJulien Gomes 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
61185f729eaaSJulien Gomes 			return -EPERM;
61195f729eaaSJulien Gomes 		break;
61205f729eaaSJulien Gomes 	}
61215f729eaaSJulien Gomes 	return 0;
61225f729eaaSJulien Gomes }
61235f729eaaSJulien Gomes 
61241da177e4SLinus Torvalds static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
61251da177e4SLinus Torvalds {
6126351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
6127e9dc8653SEric W. Biederman 
61281da177e4SLinus Torvalds 	switch (event) {
61295138e86fSVlad Yasevich 	case NETDEV_REBOOT:
61308a212589SXin Long 	case NETDEV_CHANGEMTU:
61313753654eSDavid Ahern 	case NETDEV_CHANGEADDR:
61325138e86fSVlad Yasevich 	case NETDEV_CHANGENAME:
61335138e86fSVlad Yasevich 	case NETDEV_FEAT_CHANGE:
61345138e86fSVlad Yasevich 	case NETDEV_BONDING_FAILOVER:
6135e6e66594SXin Long 	case NETDEV_POST_TYPE_CHANGE:
61365138e86fSVlad Yasevich 	case NETDEV_NOTIFY_PEERS:
6137dc709f37SXin Long 	case NETDEV_CHANGEUPPER:
61385138e86fSVlad Yasevich 	case NETDEV_RESEND_IGMP:
61395138e86fSVlad Yasevich 	case NETDEV_CHANGEINFODATA:
6140eeda3fb9SXin Long 	case NETDEV_CHANGELOWERSTATE:
6141ebdcf045SXin Long 	case NETDEV_CHANGE_TX_QUEUE_LEN:
61423d3ea5afSVlad Yasevich 		rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
614338e01b30SNicolas Dichtel 				   GFP_KERNEL, NULL, 0);
61441da177e4SLinus Torvalds 		break;
61451da177e4SLinus Torvalds 	default:
61461da177e4SLinus Torvalds 		break;
61471da177e4SLinus Torvalds 	}
61481da177e4SLinus Torvalds 	return NOTIFY_DONE;
61491da177e4SLinus Torvalds }
61501da177e4SLinus Torvalds 
61511da177e4SLinus Torvalds static struct notifier_block rtnetlink_dev_notifier = {
61521da177e4SLinus Torvalds 	.notifier_call	= rtnetlink_event,
61531da177e4SLinus Torvalds };
61541da177e4SLinus Torvalds 
615597c53cacSDenis V. Lunev 
61562c8c1e72SAlexey Dobriyan static int __net_init rtnetlink_net_init(struct net *net)
615797c53cacSDenis V. Lunev {
615897c53cacSDenis V. Lunev 	struct sock *sk;
6159a31f2d17SPablo Neira Ayuso 	struct netlink_kernel_cfg cfg = {
6160a31f2d17SPablo Neira Ayuso 		.groups		= RTNLGRP_MAX,
6161a31f2d17SPablo Neira Ayuso 		.input		= rtnetlink_rcv,
6162a31f2d17SPablo Neira Ayuso 		.cb_mutex	= &rtnl_mutex,
61639785e10aSPablo Neira Ayuso 		.flags		= NL_CFG_F_NONROOT_RECV,
61645f729eaaSJulien Gomes 		.bind		= rtnetlink_bind,
6165a31f2d17SPablo Neira Ayuso 	};
6166a31f2d17SPablo Neira Ayuso 
61679f00d977SPablo Neira Ayuso 	sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
616897c53cacSDenis V. Lunev 	if (!sk)
616997c53cacSDenis V. Lunev 		return -ENOMEM;
617097c53cacSDenis V. Lunev 	net->rtnl = sk;
617197c53cacSDenis V. Lunev 	return 0;
617297c53cacSDenis V. Lunev }
617397c53cacSDenis V. Lunev 
61742c8c1e72SAlexey Dobriyan static void __net_exit rtnetlink_net_exit(struct net *net)
617597c53cacSDenis V. Lunev {
6176b7c6ba6eSDenis V. Lunev 	netlink_kernel_release(net->rtnl);
617797c53cacSDenis V. Lunev 	net->rtnl = NULL;
617897c53cacSDenis V. Lunev }
617997c53cacSDenis V. Lunev 
618097c53cacSDenis V. Lunev static struct pernet_operations rtnetlink_net_ops = {
618197c53cacSDenis V. Lunev 	.init = rtnetlink_net_init,
618297c53cacSDenis V. Lunev 	.exit = rtnetlink_net_exit,
618397c53cacSDenis V. Lunev };
618497c53cacSDenis V. Lunev 
61851da177e4SLinus Torvalds void __init rtnetlink_init(void)
61861da177e4SLinus Torvalds {
618797c53cacSDenis V. Lunev 	if (register_pernet_subsys(&rtnetlink_net_ops))
61881da177e4SLinus Torvalds 		panic("rtnetlink_init: cannot initialize rtnetlink\n");
618997c53cacSDenis V. Lunev 
61901da177e4SLinus Torvalds 	register_netdevice_notifier(&rtnetlink_dev_notifier);
6191340d17fcSThomas Graf 
6192c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
6193b97bac64SFlorian Westphal 		      rtnl_dump_ifinfo, 0);
6194b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0);
6195b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0);
6196b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0);
6197687ad8ccSThomas Graf 
6198b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0);
6199b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0);
6200b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0);
620177162022SJohn Fastabend 
620236fbf1e5SJiri Pirko 	rtnl_register(PF_UNSPEC, RTM_NEWLINKPROP, rtnl_newlinkprop, NULL, 0);
620336fbf1e5SJiri Pirko 	rtnl_register(PF_UNSPEC, RTM_DELLINKPROP, rtnl_dellinkprop, NULL, 0);
620436fbf1e5SJiri Pirko 
6205b97bac64SFlorian Westphal 	rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
62069e834259SNikolay Aleksandrov 	rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL,
62079e834259SNikolay Aleksandrov 		      RTNL_FLAG_BULK_DEL_SUPPORTED);
62085b2f94b2SRoopa Prabhu 	rtnl_register(PF_BRIDGE, RTM_GETNEIGH, rtnl_fdb_get, rtnl_fdb_dump, 0);
6209e5a55a89SJohn Fastabend 
6210b97bac64SFlorian Westphal 	rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0);
6211b97bac64SFlorian Westphal 	rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0);
6212b97bac64SFlorian Westphal 	rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0);
621310c9ead9SRoopa Prabhu 
621410c9ead9SRoopa Prabhu 	rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,
6215b97bac64SFlorian Westphal 		      0);
621603ba3566SPetr Machata 	rtnl_register(PF_UNSPEC, RTM_SETSTATS, rtnl_stats_set, NULL, 0);
62171da177e4SLinus Torvalds }
6218