xref: /openbmc/linux/net/core/rtnetlink.c (revision f534f6581ec084fe94d6759f7672bd009794b07e)
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>
56dca56c30SJiri Pirko #include <net/devlink.h>
57cc7f5022SIdo Schimmel #if IS_ENABLED(CONFIG_IPV6)
58cc7f5022SIdo Schimmel #include <net/addrconf.h>
59cc7f5022SIdo Schimmel #endif
601da177e4SLinus Torvalds 
616264f58cSJakub Kicinski #include "dev.h"
626264f58cSJakub Kicinski 
63a428afe8SNikolay Aleksandrov #define RTNL_MAX_TYPE		50
64160656d7SIdo Schimmel #define RTNL_SLAVE_MAX_TYPE	43
65ccf8dbcdSKees Cook 
66e0d087afSEric Dumazet struct rtnl_link {
67e2849863SThomas Graf 	rtnl_doit_func		doit;
68e2849863SThomas Graf 	rtnl_dumpit_func	dumpit;
69e4202511SFlorian Westphal 	struct module		*owner;
7062256f98SFlorian Westphal 	unsigned int		flags;
71addf9b90SFlorian Westphal 	struct rcu_head		rcu;
72e2849863SThomas Graf };
73e2849863SThomas Graf 
746756ae4bSStephen Hemminger static DEFINE_MUTEX(rtnl_mutex);
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds void rtnl_lock(void)
771da177e4SLinus Torvalds {
786756ae4bSStephen Hemminger 	mutex_lock(&rtnl_mutex);
791da177e4SLinus Torvalds }
80e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_lock);
811da177e4SLinus Torvalds 
8279ffdfc6SKirill Tkhai int rtnl_lock_killable(void)
8379ffdfc6SKirill Tkhai {
8479ffdfc6SKirill Tkhai 	return mutex_lock_killable(&rtnl_mutex);
8579ffdfc6SKirill Tkhai }
8679ffdfc6SKirill Tkhai EXPORT_SYMBOL(rtnl_lock_killable);
8779ffdfc6SKirill Tkhai 
881b5c5493SEric Dumazet static struct sk_buff *defer_kfree_skb_list;
891b5c5493SEric Dumazet void rtnl_kfree_skbs(struct sk_buff *head, struct sk_buff *tail)
901b5c5493SEric Dumazet {
911b5c5493SEric Dumazet 	if (head && tail) {
921b5c5493SEric Dumazet 		tail->next = defer_kfree_skb_list;
931b5c5493SEric Dumazet 		defer_kfree_skb_list = head;
941b5c5493SEric Dumazet 	}
951b5c5493SEric Dumazet }
961b5c5493SEric Dumazet EXPORT_SYMBOL(rtnl_kfree_skbs);
971b5c5493SEric Dumazet 
986756ae4bSStephen Hemminger void __rtnl_unlock(void)
991da177e4SLinus Torvalds {
1001b5c5493SEric Dumazet 	struct sk_buff *head = defer_kfree_skb_list;
1011b5c5493SEric Dumazet 
1021b5c5493SEric Dumazet 	defer_kfree_skb_list = NULL;
1031b5c5493SEric Dumazet 
1040b5c21bbSJohannes Berg 	/* Ensure that we didn't actually add any TODO item when __rtnl_unlock()
1050b5c21bbSJohannes Berg 	 * is used. In some places, e.g. in cfg80211, we have code that will do
1060b5c21bbSJohannes Berg 	 * something like
1070b5c21bbSJohannes Berg 	 *   rtnl_lock()
1080b5c21bbSJohannes Berg 	 *   wiphy_lock()
1090b5c21bbSJohannes Berg 	 *   ...
1100b5c21bbSJohannes Berg 	 *   rtnl_unlock()
1110b5c21bbSJohannes Berg 	 *
1120b5c21bbSJohannes Berg 	 * and because netdev_run_todo() acquires the RTNL for items on the list
1130b5c21bbSJohannes Berg 	 * we could cause a situation such as this:
1140b5c21bbSJohannes Berg 	 * Thread 1			Thread 2
1150b5c21bbSJohannes Berg 	 *				  rtnl_lock()
1160b5c21bbSJohannes Berg 	 *				  unregister_netdevice()
1170b5c21bbSJohannes Berg 	 *				  __rtnl_unlock()
1180b5c21bbSJohannes Berg 	 * rtnl_lock()
1190b5c21bbSJohannes Berg 	 * wiphy_lock()
1200b5c21bbSJohannes Berg 	 * rtnl_unlock()
1210b5c21bbSJohannes Berg 	 *   netdev_run_todo()
1220b5c21bbSJohannes Berg 	 *     __rtnl_unlock()
1230b5c21bbSJohannes Berg 	 *
1240b5c21bbSJohannes Berg 	 *     // list not empty now
1250b5c21bbSJohannes Berg 	 *     // because of thread 2
1260b5c21bbSJohannes Berg 	 *				  rtnl_lock()
1270b5c21bbSJohannes Berg 	 *     while (!list_empty(...))
1280b5c21bbSJohannes Berg 	 *       rtnl_lock()
1290b5c21bbSJohannes Berg 	 *				  wiphy_lock()
1300b5c21bbSJohannes Berg 	 * **** DEADLOCK ****
1310b5c21bbSJohannes Berg 	 *
1320b5c21bbSJohannes Berg 	 * However, usage of __rtnl_unlock() is rare, and so we can ensure that
1330b5c21bbSJohannes Berg 	 * it's not used in cases where something is added to do the list.
1340b5c21bbSJohannes Berg 	 */
1350b5c21bbSJohannes Berg 	WARN_ON(!list_empty(&net_todo_list));
1360b5c21bbSJohannes Berg 
1376756ae4bSStephen Hemminger 	mutex_unlock(&rtnl_mutex);
1381b5c5493SEric Dumazet 
1391b5c5493SEric Dumazet 	while (head) {
1401b5c5493SEric Dumazet 		struct sk_buff *next = head->next;
1411b5c5493SEric Dumazet 
1421b5c5493SEric Dumazet 		kfree_skb(head);
1431b5c5493SEric Dumazet 		cond_resched();
1441b5c5493SEric Dumazet 		head = next;
1451b5c5493SEric Dumazet 	}
1461da177e4SLinus Torvalds }
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds void rtnl_unlock(void)
1491da177e4SLinus Torvalds {
15058ec3b4dSHerbert Xu 	/* This fellow will unlock it for us. */
1511da177e4SLinus Torvalds 	netdev_run_todo();
1521da177e4SLinus Torvalds }
153e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unlock);
1541da177e4SLinus Torvalds 
1556756ae4bSStephen Hemminger int rtnl_trylock(void)
1566756ae4bSStephen Hemminger {
1576756ae4bSStephen Hemminger 	return mutex_trylock(&rtnl_mutex);
1586756ae4bSStephen Hemminger }
159e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_trylock);
1606756ae4bSStephen Hemminger 
161c9c1014bSPatrick McHardy int rtnl_is_locked(void)
162c9c1014bSPatrick McHardy {
163c9c1014bSPatrick McHardy 	return mutex_is_locked(&rtnl_mutex);
164c9c1014bSPatrick McHardy }
165e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_is_locked);
166c9c1014bSPatrick McHardy 
1676f99528eSVlad Buslov bool refcount_dec_and_rtnl_lock(refcount_t *r)
1686f99528eSVlad Buslov {
1696f99528eSVlad Buslov 	return refcount_dec_and_mutex_lock(r, &rtnl_mutex);
1706f99528eSVlad Buslov }
1716f99528eSVlad Buslov EXPORT_SYMBOL(refcount_dec_and_rtnl_lock);
1726f99528eSVlad Buslov 
173a898def2SPaul E. McKenney #ifdef CONFIG_PROVE_LOCKING
1740cbf3343SYaowei Bai bool lockdep_rtnl_is_held(void)
175a898def2SPaul E. McKenney {
176a898def2SPaul E. McKenney 	return lockdep_is_held(&rtnl_mutex);
177a898def2SPaul E. McKenney }
178a898def2SPaul E. McKenney EXPORT_SYMBOL(lockdep_rtnl_is_held);
179a898def2SPaul E. McKenney #endif /* #ifdef CONFIG_PROVE_LOCKING */
180a898def2SPaul E. McKenney 
18151e13685SJakub Kicinski static struct rtnl_link __rcu *__rcu *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
182e2849863SThomas Graf 
183e2849863SThomas Graf static inline int rtm_msgindex(int msgtype)
184e2849863SThomas Graf {
185e2849863SThomas Graf 	int msgindex = msgtype - RTM_BASE;
186e2849863SThomas Graf 
187e2849863SThomas Graf 	/*
188e2849863SThomas Graf 	 * msgindex < 0 implies someone tried to register a netlink
189e2849863SThomas Graf 	 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
190e2849863SThomas Graf 	 * the message type has not been added to linux/rtnetlink.h
191e2849863SThomas Graf 	 */
192e2849863SThomas Graf 	BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
193e2849863SThomas Graf 
194e2849863SThomas Graf 	return msgindex;
195e2849863SThomas Graf }
196e2849863SThomas Graf 
197addf9b90SFlorian Westphal static struct rtnl_link *rtnl_get_link(int protocol, int msgtype)
198addf9b90SFlorian Westphal {
19951e13685SJakub Kicinski 	struct rtnl_link __rcu **tab;
200addf9b90SFlorian Westphal 
201addf9b90SFlorian Westphal 	if (protocol >= ARRAY_SIZE(rtnl_msg_handlers))
202addf9b90SFlorian Westphal 		protocol = PF_UNSPEC;
203addf9b90SFlorian Westphal 
204addf9b90SFlorian Westphal 	tab = rcu_dereference_rtnl(rtnl_msg_handlers[protocol]);
205addf9b90SFlorian Westphal 	if (!tab)
206addf9b90SFlorian Westphal 		tab = rcu_dereference_rtnl(rtnl_msg_handlers[PF_UNSPEC]);
207addf9b90SFlorian Westphal 
20851e13685SJakub Kicinski 	return rcu_dereference_rtnl(tab[msgtype]);
209addf9b90SFlorian Westphal }
210addf9b90SFlorian Westphal 
211e4202511SFlorian Westphal static int rtnl_register_internal(struct module *owner,
212e4202511SFlorian Westphal 				  int protocol, int msgtype,
213c7ac8679SGreg Rose 				  rtnl_doit_func doit, rtnl_dumpit_func dumpit,
214b97bac64SFlorian Westphal 				  unsigned int flags)
215e2849863SThomas Graf {
216b0e9fe1bSFlorian Westphal 	struct rtnl_link *link, *old;
217b0e9fe1bSFlorian Westphal 	struct rtnl_link __rcu **tab;
218e2849863SThomas Graf 	int msgindex;
219addf9b90SFlorian Westphal 	int ret = -ENOBUFS;
220e2849863SThomas Graf 
22125239ceeSPatrick McHardy 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
222e2849863SThomas Graf 	msgindex = rtm_msgindex(msgtype);
223e2849863SThomas Graf 
224addf9b90SFlorian Westphal 	rtnl_lock();
22551e13685SJakub Kicinski 	tab = rtnl_dereference(rtnl_msg_handlers[protocol]);
226e2849863SThomas Graf 	if (tab == NULL) {
227addf9b90SFlorian Westphal 		tab = kcalloc(RTM_NR_MSGTYPES, sizeof(void *), GFP_KERNEL);
228addf9b90SFlorian Westphal 		if (!tab)
229addf9b90SFlorian Westphal 			goto unlock;
230e2849863SThomas Graf 
231addf9b90SFlorian Westphal 		/* ensures we see the 0 stores */
2326853dd48SFlorian Westphal 		rcu_assign_pointer(rtnl_msg_handlers[protocol], tab);
233e2849863SThomas Graf 	}
234e2849863SThomas Graf 
235addf9b90SFlorian Westphal 	old = rtnl_dereference(tab[msgindex]);
236addf9b90SFlorian Westphal 	if (old) {
237addf9b90SFlorian Westphal 		link = kmemdup(old, sizeof(*old), GFP_KERNEL);
238addf9b90SFlorian Westphal 		if (!link)
239addf9b90SFlorian Westphal 			goto unlock;
240addf9b90SFlorian Westphal 	} else {
241addf9b90SFlorian Westphal 		link = kzalloc(sizeof(*link), GFP_KERNEL);
242addf9b90SFlorian Westphal 		if (!link)
243addf9b90SFlorian Westphal 			goto unlock;
244addf9b90SFlorian Westphal 	}
245e2849863SThomas Graf 
246e4202511SFlorian Westphal 	WARN_ON(link->owner && link->owner != owner);
247e4202511SFlorian Westphal 	link->owner = owner;
248e4202511SFlorian Westphal 
249addf9b90SFlorian Westphal 	WARN_ON(doit && link->doit && link->doit != doit);
250addf9b90SFlorian Westphal 	if (doit)
251addf9b90SFlorian Westphal 		link->doit = doit;
252addf9b90SFlorian Westphal 	WARN_ON(dumpit && link->dumpit && link->dumpit != dumpit);
253addf9b90SFlorian Westphal 	if (dumpit)
254addf9b90SFlorian Westphal 		link->dumpit = dumpit;
255addf9b90SFlorian Westphal 
256a6cec0bcSNikolay Aleksandrov 	WARN_ON(rtnl_msgtype_kind(msgtype) != RTNL_KIND_DEL &&
257a6cec0bcSNikolay Aleksandrov 		(flags & RTNL_FLAG_BULK_DEL_SUPPORTED));
258addf9b90SFlorian Westphal 	link->flags |= flags;
259addf9b90SFlorian Westphal 
260addf9b90SFlorian Westphal 	/* publish protocol:msgtype */
261addf9b90SFlorian Westphal 	rcu_assign_pointer(tab[msgindex], link);
262addf9b90SFlorian Westphal 	ret = 0;
263addf9b90SFlorian Westphal 	if (old)
264addf9b90SFlorian Westphal 		kfree_rcu(old, rcu);
265addf9b90SFlorian Westphal unlock:
266addf9b90SFlorian Westphal 	rtnl_unlock();
267addf9b90SFlorian Westphal 	return ret;
268e2849863SThomas Graf }
269e4202511SFlorian Westphal 
270e4202511SFlorian Westphal /**
271e4202511SFlorian Westphal  * rtnl_register_module - Register a rtnetlink message type
272e4202511SFlorian Westphal  *
273e4202511SFlorian Westphal  * @owner: module registering the hook (THIS_MODULE)
274e4202511SFlorian Westphal  * @protocol: Protocol family or PF_UNSPEC
275e4202511SFlorian Westphal  * @msgtype: rtnetlink message type
276e4202511SFlorian Westphal  * @doit: Function pointer called for each request message
277e4202511SFlorian Westphal  * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
278d467d0bcSZheng Yongjun  * @flags: rtnl_link_flags to modify behaviour of doit/dumpit functions
279e4202511SFlorian Westphal  *
280e4202511SFlorian Westphal  * Like rtnl_register, but for use by removable modules.
281e4202511SFlorian Westphal  */
282e4202511SFlorian Westphal int rtnl_register_module(struct module *owner,
283e4202511SFlorian Westphal 			 int protocol, int msgtype,
284e4202511SFlorian Westphal 			 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
285e4202511SFlorian Westphal 			 unsigned int flags)
286e4202511SFlorian Westphal {
287e4202511SFlorian Westphal 	return rtnl_register_internal(owner, protocol, msgtype,
288e4202511SFlorian Westphal 				      doit, dumpit, flags);
289e4202511SFlorian Westphal }
290e4202511SFlorian Westphal EXPORT_SYMBOL_GPL(rtnl_register_module);
291e4202511SFlorian Westphal 
292e4202511SFlorian Westphal /**
29316feebcfSFlorian Westphal  * rtnl_register - Register a rtnetlink message type
294e4202511SFlorian Westphal  * @protocol: Protocol family or PF_UNSPEC
295e4202511SFlorian Westphal  * @msgtype: rtnetlink message type
296e4202511SFlorian Westphal  * @doit: Function pointer called for each request message
297e4202511SFlorian Westphal  * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
298d467d0bcSZheng Yongjun  * @flags: rtnl_link_flags to modify behaviour of doit/dumpit functions
299e4202511SFlorian Westphal  *
300e4202511SFlorian Westphal  * Registers the specified function pointers (at least one of them has
301e4202511SFlorian Westphal  * to be non-NULL) to be called whenever a request message for the
302e4202511SFlorian Westphal  * specified protocol family and message type is received.
303e4202511SFlorian Westphal  *
304e4202511SFlorian Westphal  * The special protocol family PF_UNSPEC may be used to define fallback
305e4202511SFlorian Westphal  * function pointers for the case when no entry for the specific protocol
306e4202511SFlorian Westphal  * family exists.
307e2849863SThomas Graf  */
308e2849863SThomas Graf void rtnl_register(int protocol, int msgtype,
309c7ac8679SGreg Rose 		   rtnl_doit_func doit, rtnl_dumpit_func dumpit,
310b97bac64SFlorian Westphal 		   unsigned int flags)
311e2849863SThomas Graf {
31216feebcfSFlorian Westphal 	int err;
31316feebcfSFlorian Westphal 
31416feebcfSFlorian Westphal 	err = rtnl_register_internal(NULL, protocol, msgtype, doit, dumpit,
31516feebcfSFlorian Westphal 				     flags);
31616feebcfSFlorian Westphal 	if (err)
31716feebcfSFlorian Westphal 		pr_err("Unable to register rtnetlink message handler, "
31816feebcfSFlorian Westphal 		       "protocol = %d, message type = %d\n", protocol, msgtype);
319e2849863SThomas Graf }
320e2849863SThomas Graf 
321e2849863SThomas Graf /**
322e2849863SThomas Graf  * rtnl_unregister - Unregister a rtnetlink message type
323e2849863SThomas Graf  * @protocol: Protocol family or PF_UNSPEC
324e2849863SThomas Graf  * @msgtype: rtnetlink message type
325e2849863SThomas Graf  *
326e2849863SThomas Graf  * Returns 0 on success or a negative error code.
327e2849863SThomas Graf  */
328e2849863SThomas Graf int rtnl_unregister(int protocol, int msgtype)
329e2849863SThomas Graf {
33051e13685SJakub Kicinski 	struct rtnl_link __rcu **tab;
33151e13685SJakub Kicinski 	struct rtnl_link *link;
332e2849863SThomas Graf 	int msgindex;
333e2849863SThomas Graf 
33425239ceeSPatrick McHardy 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
335e2849863SThomas Graf 	msgindex = rtm_msgindex(msgtype);
336e2849863SThomas Graf 
3376853dd48SFlorian Westphal 	rtnl_lock();
338addf9b90SFlorian Westphal 	tab = rtnl_dereference(rtnl_msg_handlers[protocol]);
339addf9b90SFlorian Westphal 	if (!tab) {
3406853dd48SFlorian Westphal 		rtnl_unlock();
341e2849863SThomas Graf 		return -ENOENT;
3426853dd48SFlorian Westphal 	}
343e2849863SThomas Graf 
34451e13685SJakub Kicinski 	link = rtnl_dereference(tab[msgindex]);
3454fc29989SYajun Deng 	RCU_INIT_POINTER(tab[msgindex], NULL);
3466853dd48SFlorian Westphal 	rtnl_unlock();
347e2849863SThomas Graf 
348addf9b90SFlorian Westphal 	kfree_rcu(link, rcu);
349addf9b90SFlorian Westphal 
350e2849863SThomas Graf 	return 0;
351e2849863SThomas Graf }
352e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister);
353e2849863SThomas Graf 
354e2849863SThomas Graf /**
355e2849863SThomas Graf  * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
356e2849863SThomas Graf  * @protocol : Protocol family or PF_UNSPEC
357e2849863SThomas Graf  *
358e2849863SThomas Graf  * Identical to calling rtnl_unregster() for all registered message types
359e2849863SThomas Graf  * of a certain protocol family.
360e2849863SThomas Graf  */
361e2849863SThomas Graf void rtnl_unregister_all(int protocol)
362e2849863SThomas Graf {
36351e13685SJakub Kicinski 	struct rtnl_link __rcu **tab;
36451e13685SJakub Kicinski 	struct rtnl_link *link;
365addf9b90SFlorian Westphal 	int msgindex;
366019a3169SFlorian Westphal 
36725239ceeSPatrick McHardy 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
368e2849863SThomas Graf 
369019a3169SFlorian Westphal 	rtnl_lock();
37051e13685SJakub Kicinski 	tab = rtnl_dereference(rtnl_msg_handlers[protocol]);
371f707ef61SSabrina Dubroca 	if (!tab) {
372f707ef61SSabrina Dubroca 		rtnl_unlock();
373f707ef61SSabrina Dubroca 		return;
374f707ef61SSabrina Dubroca 	}
3756853dd48SFlorian Westphal 	RCU_INIT_POINTER(rtnl_msg_handlers[protocol], NULL);
376addf9b90SFlorian Westphal 	for (msgindex = 0; msgindex < RTM_NR_MSGTYPES; msgindex++) {
37751e13685SJakub Kicinski 		link = rtnl_dereference(tab[msgindex]);
378addf9b90SFlorian Westphal 		if (!link)
379addf9b90SFlorian Westphal 			continue;
380addf9b90SFlorian Westphal 
3814fc29989SYajun Deng 		RCU_INIT_POINTER(tab[msgindex], NULL);
382addf9b90SFlorian Westphal 		kfree_rcu(link, rcu);
383addf9b90SFlorian Westphal 	}
384019a3169SFlorian Westphal 	rtnl_unlock();
385019a3169SFlorian Westphal 
3866853dd48SFlorian Westphal 	synchronize_net();
3876853dd48SFlorian Westphal 
388addf9b90SFlorian Westphal 	kfree(tab);
389e2849863SThomas Graf }
390e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister_all);
3911da177e4SLinus Torvalds 
39238f7b870SPatrick McHardy static LIST_HEAD(link_ops);
39338f7b870SPatrick McHardy 
394c63044f0SEric Dumazet static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
395c63044f0SEric Dumazet {
396c63044f0SEric Dumazet 	const struct rtnl_link_ops *ops;
397c63044f0SEric Dumazet 
398c63044f0SEric Dumazet 	list_for_each_entry(ops, &link_ops, list) {
399c63044f0SEric Dumazet 		if (!strcmp(ops->kind, kind))
400c63044f0SEric Dumazet 			return ops;
401c63044f0SEric Dumazet 	}
402c63044f0SEric Dumazet 	return NULL;
403c63044f0SEric Dumazet }
404c63044f0SEric Dumazet 
40538f7b870SPatrick McHardy /**
40638f7b870SPatrick McHardy  * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
40738f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to register
40838f7b870SPatrick McHardy  *
40938f7b870SPatrick McHardy  * The caller must hold the rtnl_mutex. This function should be used
41038f7b870SPatrick McHardy  * by drivers that create devices during module initialization. It
41138f7b870SPatrick McHardy  * must be called before registering the devices.
41238f7b870SPatrick McHardy  *
41338f7b870SPatrick McHardy  * Returns 0 on success or a negative error code.
41438f7b870SPatrick McHardy  */
41538f7b870SPatrick McHardy int __rtnl_link_register(struct rtnl_link_ops *ops)
41638f7b870SPatrick McHardy {
417c63044f0SEric Dumazet 	if (rtnl_link_ops_get(ops->kind))
418c63044f0SEric Dumazet 		return -EEXIST;
419c63044f0SEric Dumazet 
4208c713dc9SJohannes Berg 	/* The check for alloc/setup is here because if ops
421b0ab2fabSJiri Pirko 	 * does not have that filled up, it is not possible
422b0ab2fabSJiri Pirko 	 * to use the ops for creating device. So do not
423b0ab2fabSJiri Pirko 	 * fill up dellink as well. That disables rtnl_dellink.
424b0ab2fabSJiri Pirko 	 */
4258c713dc9SJohannes Berg 	if ((ops->alloc || ops->setup) && !ops->dellink)
42623289a37SEric Dumazet 		ops->dellink = unregister_netdevice_queue;
4272d85cba2SPatrick McHardy 
42838f7b870SPatrick McHardy 	list_add_tail(&ops->list, &link_ops);
42938f7b870SPatrick McHardy 	return 0;
43038f7b870SPatrick McHardy }
43138f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_register);
43238f7b870SPatrick McHardy 
43338f7b870SPatrick McHardy /**
43438f7b870SPatrick McHardy  * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
43538f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to register
43638f7b870SPatrick McHardy  *
43738f7b870SPatrick McHardy  * Returns 0 on success or a negative error code.
43838f7b870SPatrick McHardy  */
43938f7b870SPatrick McHardy int rtnl_link_register(struct rtnl_link_ops *ops)
44038f7b870SPatrick McHardy {
44138f7b870SPatrick McHardy 	int err;
44238f7b870SPatrick McHardy 
443ccf8dbcdSKees Cook 	/* Sanity-check max sizes to avoid stack buffer overflow. */
444ccf8dbcdSKees Cook 	if (WARN_ON(ops->maxtype > RTNL_MAX_TYPE ||
445ccf8dbcdSKees Cook 		    ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE))
446ccf8dbcdSKees Cook 		return -EINVAL;
447ccf8dbcdSKees Cook 
44838f7b870SPatrick McHardy 	rtnl_lock();
44938f7b870SPatrick McHardy 	err = __rtnl_link_register(ops);
45038f7b870SPatrick McHardy 	rtnl_unlock();
45138f7b870SPatrick McHardy 	return err;
45238f7b870SPatrick McHardy }
45338f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_register);
45438f7b870SPatrick McHardy 
455669f87baSPavel Emelyanov static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
456669f87baSPavel Emelyanov {
457669f87baSPavel Emelyanov 	struct net_device *dev;
45823289a37SEric Dumazet 	LIST_HEAD(list_kill);
45923289a37SEric Dumazet 
460669f87baSPavel Emelyanov 	for_each_netdev(net, dev) {
46123289a37SEric Dumazet 		if (dev->rtnl_link_ops == ops)
46223289a37SEric Dumazet 			ops->dellink(dev, &list_kill);
463669f87baSPavel Emelyanov 	}
46423289a37SEric Dumazet 	unregister_netdevice_many(&list_kill);
465669f87baSPavel Emelyanov }
466669f87baSPavel Emelyanov 
46738f7b870SPatrick McHardy /**
46838f7b870SPatrick McHardy  * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
46938f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to unregister
47038f7b870SPatrick McHardy  *
471554873e5SKirill Tkhai  * The caller must hold the rtnl_mutex and guarantee net_namespace_list
472554873e5SKirill Tkhai  * integrity (hold pernet_ops_rwsem for writing to close the race
473554873e5SKirill Tkhai  * with setup_net() and cleanup_net()).
47438f7b870SPatrick McHardy  */
47538f7b870SPatrick McHardy void __rtnl_link_unregister(struct rtnl_link_ops *ops)
47638f7b870SPatrick McHardy {
477881d966bSEric W. Biederman 	struct net *net;
4782d85cba2SPatrick McHardy 
479881d966bSEric W. Biederman 	for_each_net(net) {
480669f87baSPavel Emelyanov 		__rtnl_kill_links(net, ops);
481881d966bSEric W. Biederman 	}
48238f7b870SPatrick McHardy 	list_del(&ops->list);
48338f7b870SPatrick McHardy }
48438f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
48538f7b870SPatrick McHardy 
486200b916fSCong Wang /* Return with the rtnl_lock held when there are no network
487200b916fSCong Wang  * devices unregistering in any network namespace.
488200b916fSCong Wang  */
489200b916fSCong Wang static void rtnl_lock_unregistering_all(void)
490200b916fSCong Wang {
491200b916fSCong Wang 	struct net *net;
492200b916fSCong Wang 	bool unregistering;
493ff960a73SPeter Zijlstra 	DEFINE_WAIT_FUNC(wait, woken_wake_function);
494200b916fSCong Wang 
495ff960a73SPeter Zijlstra 	add_wait_queue(&netdev_unregistering_wq, &wait);
496200b916fSCong Wang 	for (;;) {
497200b916fSCong Wang 		unregistering = false;
498200b916fSCong Wang 		rtnl_lock();
499f0b07bb1SKirill Tkhai 		/* We held write locked pernet_ops_rwsem, and parallel
500f0b07bb1SKirill Tkhai 		 * setup_net() and cleanup_net() are not possible.
501f0b07bb1SKirill Tkhai 		 */
502200b916fSCong Wang 		for_each_net(net) {
503ede6c39cSEric Dumazet 			if (atomic_read(&net->dev_unreg_count) > 0) {
504200b916fSCong Wang 				unregistering = true;
505200b916fSCong Wang 				break;
506200b916fSCong Wang 			}
507200b916fSCong Wang 		}
508200b916fSCong Wang 		if (!unregistering)
509200b916fSCong Wang 			break;
510200b916fSCong Wang 		__rtnl_unlock();
511ff960a73SPeter Zijlstra 
512ff960a73SPeter Zijlstra 		wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
513200b916fSCong Wang 	}
514ff960a73SPeter Zijlstra 	remove_wait_queue(&netdev_unregistering_wq, &wait);
515200b916fSCong Wang }
516200b916fSCong Wang 
51738f7b870SPatrick McHardy /**
51838f7b870SPatrick McHardy  * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
51938f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to unregister
52038f7b870SPatrick McHardy  */
52138f7b870SPatrick McHardy void rtnl_link_unregister(struct rtnl_link_ops *ops)
52238f7b870SPatrick McHardy {
5238518e9bbSKirill Tkhai 	/* Close the race with setup_net() and cleanup_net() */
5244420bf21SKirill Tkhai 	down_write(&pernet_ops_rwsem);
525200b916fSCong Wang 	rtnl_lock_unregistering_all();
52638f7b870SPatrick McHardy 	__rtnl_link_unregister(ops);
52738f7b870SPatrick McHardy 	rtnl_unlock();
5284420bf21SKirill Tkhai 	up_write(&pernet_ops_rwsem);
52938f7b870SPatrick McHardy }
53038f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_unregister);
53138f7b870SPatrick McHardy 
532ba7d49b1SJiri Pirko static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
533ba7d49b1SJiri Pirko {
534ba7d49b1SJiri Pirko 	struct net_device *master_dev;
535ba7d49b1SJiri Pirko 	const struct rtnl_link_ops *ops;
5368515ae38SFlorian Westphal 	size_t size = 0;
537ba7d49b1SJiri Pirko 
5388515ae38SFlorian Westphal 	rcu_read_lock();
5398515ae38SFlorian Westphal 
5408515ae38SFlorian Westphal 	master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
541ba7d49b1SJiri Pirko 	if (!master_dev)
5428515ae38SFlorian Westphal 		goto out;
5438515ae38SFlorian Westphal 
544ba7d49b1SJiri Pirko 	ops = master_dev->rtnl_link_ops;
5456049f253SFernando Luis Vazquez Cao 	if (!ops || !ops->get_slave_size)
5468515ae38SFlorian Westphal 		goto out;
547ba7d49b1SJiri Pirko 	/* IFLA_INFO_SLAVE_DATA + nested data */
5488515ae38SFlorian Westphal 	size = nla_total_size(sizeof(struct nlattr)) +
549ba7d49b1SJiri Pirko 	       ops->get_slave_size(master_dev, dev);
5508515ae38SFlorian Westphal 
5518515ae38SFlorian Westphal out:
5528515ae38SFlorian Westphal 	rcu_read_unlock();
5538515ae38SFlorian Westphal 	return size;
554ba7d49b1SJiri Pirko }
555ba7d49b1SJiri Pirko 
55638f7b870SPatrick McHardy static size_t rtnl_link_get_size(const struct net_device *dev)
55738f7b870SPatrick McHardy {
55838f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
55938f7b870SPatrick McHardy 	size_t size;
56038f7b870SPatrick McHardy 
56138f7b870SPatrick McHardy 	if (!ops)
56238f7b870SPatrick McHardy 		return 0;
56338f7b870SPatrick McHardy 
564369cf77aSThomas Graf 	size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
565369cf77aSThomas Graf 	       nla_total_size(strlen(ops->kind) + 1);  /* IFLA_INFO_KIND */
56638f7b870SPatrick McHardy 
56738f7b870SPatrick McHardy 	if (ops->get_size)
56838f7b870SPatrick McHardy 		/* IFLA_INFO_DATA + nested data */
569369cf77aSThomas Graf 		size += nla_total_size(sizeof(struct nlattr)) +
57038f7b870SPatrick McHardy 			ops->get_size(dev);
57138f7b870SPatrick McHardy 
57238f7b870SPatrick McHardy 	if (ops->get_xstats_size)
573369cf77aSThomas Graf 		/* IFLA_INFO_XSTATS */
574369cf77aSThomas Graf 		size += nla_total_size(ops->get_xstats_size(dev));
57538f7b870SPatrick McHardy 
576ba7d49b1SJiri Pirko 	size += rtnl_link_get_slave_info_data_size(dev);
577ba7d49b1SJiri Pirko 
57838f7b870SPatrick McHardy 	return size;
57938f7b870SPatrick McHardy }
58038f7b870SPatrick McHardy 
581f8ff182cSThomas Graf static LIST_HEAD(rtnl_af_ops);
582f8ff182cSThomas Graf 
583f8ff182cSThomas Graf static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
584f8ff182cSThomas Graf {
585f8ff182cSThomas Graf 	const struct rtnl_af_ops *ops;
586f8ff182cSThomas Graf 
587a100243dSCong Wang 	ASSERT_RTNL();
588a100243dSCong Wang 
589a100243dSCong Wang 	list_for_each_entry(ops, &rtnl_af_ops, list) {
590f8ff182cSThomas Graf 		if (ops->family == family)
591f8ff182cSThomas Graf 			return ops;
592f8ff182cSThomas Graf 	}
593f8ff182cSThomas Graf 
594f8ff182cSThomas Graf 	return NULL;
595f8ff182cSThomas Graf }
596f8ff182cSThomas Graf 
597f8ff182cSThomas Graf /**
598f8ff182cSThomas Graf  * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
599f8ff182cSThomas Graf  * @ops: struct rtnl_af_ops * to register
600f8ff182cSThomas Graf  *
601f8ff182cSThomas Graf  * Returns 0 on success or a negative error code.
602f8ff182cSThomas Graf  */
6033678a9d8Sstephen hemminger void rtnl_af_register(struct rtnl_af_ops *ops)
604f8ff182cSThomas Graf {
605f8ff182cSThomas Graf 	rtnl_lock();
6065fa85a09SFlorian Westphal 	list_add_tail_rcu(&ops->list, &rtnl_af_ops);
607f8ff182cSThomas Graf 	rtnl_unlock();
608f8ff182cSThomas Graf }
609f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_register);
610f8ff182cSThomas Graf 
611f8ff182cSThomas Graf /**
612f8ff182cSThomas Graf  * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
613f8ff182cSThomas Graf  * @ops: struct rtnl_af_ops * to unregister
614f8ff182cSThomas Graf  */
615f8ff182cSThomas Graf void rtnl_af_unregister(struct rtnl_af_ops *ops)
616f8ff182cSThomas Graf {
617f8ff182cSThomas Graf 	rtnl_lock();
6185fa85a09SFlorian Westphal 	list_del_rcu(&ops->list);
619f8ff182cSThomas Graf 	rtnl_unlock();
6205fa85a09SFlorian Westphal 
6215fa85a09SFlorian Westphal 	synchronize_rcu();
622f8ff182cSThomas Graf }
623f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_unregister);
624f8ff182cSThomas Graf 
625b1974ed0SArad, Ronen static size_t rtnl_link_get_af_size(const struct net_device *dev,
626b1974ed0SArad, Ronen 				    u32 ext_filter_mask)
627f8ff182cSThomas Graf {
628f8ff182cSThomas Graf 	struct rtnl_af_ops *af_ops;
629f8ff182cSThomas Graf 	size_t size;
630f8ff182cSThomas Graf 
631f8ff182cSThomas Graf 	/* IFLA_AF_SPEC */
632f8ff182cSThomas Graf 	size = nla_total_size(sizeof(struct nlattr));
633f8ff182cSThomas Graf 
6345fa85a09SFlorian Westphal 	rcu_read_lock();
6355fa85a09SFlorian Westphal 	list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
636f8ff182cSThomas Graf 		if (af_ops->get_link_af_size) {
637f8ff182cSThomas Graf 			/* AF_* + nested data */
638f8ff182cSThomas Graf 			size += nla_total_size(sizeof(struct nlattr)) +
639b1974ed0SArad, Ronen 				af_ops->get_link_af_size(dev, ext_filter_mask);
640f8ff182cSThomas Graf 		}
641f8ff182cSThomas Graf 	}
6425fa85a09SFlorian Westphal 	rcu_read_unlock();
643f8ff182cSThomas Graf 
644f8ff182cSThomas Graf 	return size;
645f8ff182cSThomas Graf }
646f8ff182cSThomas Graf 
647ba7d49b1SJiri Pirko static bool rtnl_have_link_slave_info(const struct net_device *dev)
648ba7d49b1SJiri Pirko {
649ba7d49b1SJiri Pirko 	struct net_device *master_dev;
6504c82a95eSFlorian Westphal 	bool ret = false;
651ba7d49b1SJiri Pirko 
6524c82a95eSFlorian Westphal 	rcu_read_lock();
6534c82a95eSFlorian Westphal 
6544c82a95eSFlorian Westphal 	master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
655813f020cSJiri Pirko 	if (master_dev && master_dev->rtnl_link_ops)
6564c82a95eSFlorian Westphal 		ret = true;
6574c82a95eSFlorian Westphal 	rcu_read_unlock();
6584c82a95eSFlorian Westphal 	return ret;
659ba7d49b1SJiri Pirko }
660ba7d49b1SJiri Pirko 
661ba7d49b1SJiri Pirko static int rtnl_link_slave_info_fill(struct sk_buff *skb,
662ba7d49b1SJiri Pirko 				     const struct net_device *dev)
663ba7d49b1SJiri Pirko {
664ba7d49b1SJiri Pirko 	struct net_device *master_dev;
665ba7d49b1SJiri Pirko 	const struct rtnl_link_ops *ops;
666ba7d49b1SJiri Pirko 	struct nlattr *slave_data;
667ba7d49b1SJiri Pirko 	int err;
668ba7d49b1SJiri Pirko 
669ba7d49b1SJiri Pirko 	master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
670ba7d49b1SJiri Pirko 	if (!master_dev)
671ba7d49b1SJiri Pirko 		return 0;
672ba7d49b1SJiri Pirko 	ops = master_dev->rtnl_link_ops;
673ba7d49b1SJiri Pirko 	if (!ops)
674ba7d49b1SJiri Pirko 		return 0;
675ba7d49b1SJiri Pirko 	if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
676ba7d49b1SJiri Pirko 		return -EMSGSIZE;
677ba7d49b1SJiri Pirko 	if (ops->fill_slave_info) {
678ae0be8deSMichal Kubecek 		slave_data = nla_nest_start_noflag(skb, IFLA_INFO_SLAVE_DATA);
679ba7d49b1SJiri Pirko 		if (!slave_data)
680ba7d49b1SJiri Pirko 			return -EMSGSIZE;
681ba7d49b1SJiri Pirko 		err = ops->fill_slave_info(skb, master_dev, dev);
682ba7d49b1SJiri Pirko 		if (err < 0)
683ba7d49b1SJiri Pirko 			goto err_cancel_slave_data;
684ba7d49b1SJiri Pirko 		nla_nest_end(skb, slave_data);
685ba7d49b1SJiri Pirko 	}
686ba7d49b1SJiri Pirko 	return 0;
687ba7d49b1SJiri Pirko 
688ba7d49b1SJiri Pirko err_cancel_slave_data:
689ba7d49b1SJiri Pirko 	nla_nest_cancel(skb, slave_data);
690ba7d49b1SJiri Pirko 	return err;
691ba7d49b1SJiri Pirko }
692ba7d49b1SJiri Pirko 
693ba7d49b1SJiri Pirko static int rtnl_link_info_fill(struct sk_buff *skb,
694ba7d49b1SJiri Pirko 			       const struct net_device *dev)
69538f7b870SPatrick McHardy {
69638f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
697ba7d49b1SJiri Pirko 	struct nlattr *data;
698ba7d49b1SJiri Pirko 	int err;
699ba7d49b1SJiri Pirko 
700ba7d49b1SJiri Pirko 	if (!ops)
701ba7d49b1SJiri Pirko 		return 0;
702ba7d49b1SJiri Pirko 	if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
703ba7d49b1SJiri Pirko 		return -EMSGSIZE;
704ba7d49b1SJiri Pirko 	if (ops->fill_xstats) {
705ba7d49b1SJiri Pirko 		err = ops->fill_xstats(skb, dev);
706ba7d49b1SJiri Pirko 		if (err < 0)
707ba7d49b1SJiri Pirko 			return err;
708ba7d49b1SJiri Pirko 	}
709ba7d49b1SJiri Pirko 	if (ops->fill_info) {
710ae0be8deSMichal Kubecek 		data = nla_nest_start_noflag(skb, IFLA_INFO_DATA);
711ba7d49b1SJiri Pirko 		if (data == NULL)
712ba7d49b1SJiri Pirko 			return -EMSGSIZE;
713ba7d49b1SJiri Pirko 		err = ops->fill_info(skb, dev);
714ba7d49b1SJiri Pirko 		if (err < 0)
715ba7d49b1SJiri Pirko 			goto err_cancel_data;
716ba7d49b1SJiri Pirko 		nla_nest_end(skb, data);
717ba7d49b1SJiri Pirko 	}
718ba7d49b1SJiri Pirko 	return 0;
719ba7d49b1SJiri Pirko 
720ba7d49b1SJiri Pirko err_cancel_data:
721ba7d49b1SJiri Pirko 	nla_nest_cancel(skb, data);
722ba7d49b1SJiri Pirko 	return err;
723ba7d49b1SJiri Pirko }
724ba7d49b1SJiri Pirko 
725ba7d49b1SJiri Pirko static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
726ba7d49b1SJiri Pirko {
727ba7d49b1SJiri Pirko 	struct nlattr *linkinfo;
72838f7b870SPatrick McHardy 	int err = -EMSGSIZE;
72938f7b870SPatrick McHardy 
730ae0be8deSMichal Kubecek 	linkinfo = nla_nest_start_noflag(skb, IFLA_LINKINFO);
73138f7b870SPatrick McHardy 	if (linkinfo == NULL)
73238f7b870SPatrick McHardy 		goto out;
73338f7b870SPatrick McHardy 
734ba7d49b1SJiri Pirko 	err = rtnl_link_info_fill(skb, dev);
73538f7b870SPatrick McHardy 	if (err < 0)
73638f7b870SPatrick McHardy 		goto err_cancel_link;
737ba7d49b1SJiri Pirko 
738ba7d49b1SJiri Pirko 	err = rtnl_link_slave_info_fill(skb, dev);
73938f7b870SPatrick McHardy 	if (err < 0)
740ba7d49b1SJiri Pirko 		goto err_cancel_link;
74138f7b870SPatrick McHardy 
74238f7b870SPatrick McHardy 	nla_nest_end(skb, linkinfo);
74338f7b870SPatrick McHardy 	return 0;
74438f7b870SPatrick McHardy 
74538f7b870SPatrick McHardy err_cancel_link:
74638f7b870SPatrick McHardy 	nla_nest_cancel(skb, linkinfo);
74738f7b870SPatrick McHardy out:
74838f7b870SPatrick McHardy 	return err;
74938f7b870SPatrick McHardy }
75038f7b870SPatrick McHardy 
75195c96174SEric Dumazet int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
7521da177e4SLinus Torvalds {
75397c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
7541da177e4SLinus Torvalds 
755cfdf0d9aSYajun Deng 	return nlmsg_notify(rtnl, skb, pid, group, echo, GFP_KERNEL);
7561da177e4SLinus Torvalds }
7571da177e4SLinus Torvalds 
75897c53cacSDenis V. Lunev int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
7592942e900SThomas Graf {
76097c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
76197c53cacSDenis V. Lunev 
7622942e900SThomas Graf 	return nlmsg_unicast(rtnl, skb, pid);
7632942e900SThomas Graf }
764e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unicast);
7652942e900SThomas Graf 
7661ce85fe4SPablo Neira Ayuso void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
7671d997f10SHangbin Liu 		 const struct nlmsghdr *nlh, gfp_t flags)
76897676b6bSThomas Graf {
76997c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
77097676b6bSThomas Graf 
771f9b282b3SYajun Deng 	nlmsg_notify(rtnl, skb, pid, group, nlmsg_report(nlh), flags);
77297676b6bSThomas Graf }
773e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_notify);
77497676b6bSThomas Graf 
77597c53cacSDenis V. Lunev void rtnl_set_sk_err(struct net *net, u32 group, int error)
77697676b6bSThomas Graf {
77797c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
77897c53cacSDenis V. Lunev 
77997676b6bSThomas Graf 	netlink_set_err(rtnl, 0, group, error);
78097676b6bSThomas Graf }
781e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_set_sk_err);
78297676b6bSThomas Graf 
7831da177e4SLinus Torvalds int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
7841da177e4SLinus Torvalds {
7852d7202bfSThomas Graf 	struct nlattr *mx;
7862d7202bfSThomas Graf 	int i, valid = 0;
7871da177e4SLinus Torvalds 
788c22a133aSDavid Ahern 	/* nothing is dumped for dst_default_metrics, so just skip the loop */
789c22a133aSDavid Ahern 	if (metrics == dst_default_metrics.metrics)
790c22a133aSDavid Ahern 		return 0;
791c22a133aSDavid Ahern 
792ae0be8deSMichal Kubecek 	mx = nla_nest_start_noflag(skb, RTA_METRICS);
7932d7202bfSThomas Graf 	if (mx == NULL)
7942d7202bfSThomas Graf 		return -ENOBUFS;
7952d7202bfSThomas Graf 
7961da177e4SLinus Torvalds 	for (i = 0; i < RTAX_MAX; i++) {
7972d7202bfSThomas Graf 		if (metrics[i]) {
798ea697639SDaniel Borkmann 			if (i == RTAX_CC_ALGO - 1) {
799ea697639SDaniel Borkmann 				char tmp[TCP_CA_NAME_MAX], *name;
800ea697639SDaniel Borkmann 
801ea697639SDaniel Borkmann 				name = tcp_ca_get_name_by_key(metrics[i], tmp);
802ea697639SDaniel Borkmann 				if (!name)
803ea697639SDaniel Borkmann 					continue;
804ea697639SDaniel Borkmann 				if (nla_put_string(skb, i + 1, name))
805ea697639SDaniel Borkmann 					goto nla_put_failure;
806c3a8d947SDaniel Borkmann 			} else if (i == RTAX_FEATURES - 1) {
807c3a8d947SDaniel Borkmann 				u32 user_features = metrics[i] & RTAX_FEATURE_MASK;
808c3a8d947SDaniel Borkmann 
809f8edcd12SPhil Sutter 				if (!user_features)
810f8edcd12SPhil Sutter 					continue;
811c3a8d947SDaniel Borkmann 				BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK);
812c3a8d947SDaniel Borkmann 				if (nla_put_u32(skb, i + 1, user_features))
813c3a8d947SDaniel Borkmann 					goto nla_put_failure;
814ea697639SDaniel Borkmann 			} else {
815a6574349SDavid S. Miller 				if (nla_put_u32(skb, i + 1, metrics[i]))
816a6574349SDavid S. Miller 					goto nla_put_failure;
8171da177e4SLinus Torvalds 			}
818ea697639SDaniel Borkmann 			valid++;
819ea697639SDaniel Borkmann 		}
8202d7202bfSThomas Graf 	}
8211da177e4SLinus Torvalds 
822a57d27fcSDavid S. Miller 	if (!valid) {
823a57d27fcSDavid S. Miller 		nla_nest_cancel(skb, mx);
824a57d27fcSDavid S. Miller 		return 0;
825a57d27fcSDavid S. Miller 	}
8262d7202bfSThomas Graf 
8272d7202bfSThomas Graf 	return nla_nest_end(skb, mx);
8282d7202bfSThomas Graf 
8292d7202bfSThomas Graf nla_put_failure:
830bc3ed28cSThomas Graf 	nla_nest_cancel(skb, mx);
831bc3ed28cSThomas Graf 	return -EMSGSIZE;
8321da177e4SLinus Torvalds }
833e0d087afSEric Dumazet EXPORT_SYMBOL(rtnetlink_put_metrics);
8341da177e4SLinus Torvalds 
835e3703b3dSThomas Graf int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
83687a50699SDavid S. Miller 		       long expires, u32 error)
837e3703b3dSThomas Graf {
838e3703b3dSThomas Graf 	struct rta_cacheinfo ci = {
839e3703b3dSThomas Graf 		.rta_error = error,
840e3703b3dSThomas Graf 		.rta_id =  id,
841e3703b3dSThomas Graf 	};
842e3703b3dSThomas Graf 
8433940746dSDavid Ahern 	if (dst) {
8443940746dSDavid Ahern 		ci.rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse);
8453940746dSDavid Ahern 		ci.rta_used = dst->__use;
846bc9d3a9fSThomas Gleixner 		ci.rta_clntref = rcuref_read(&dst->__rcuref);
8473940746dSDavid Ahern 	}
8488253947eSLi Wei 	if (expires) {
8498253947eSLi Wei 		unsigned long clock;
850e3703b3dSThomas Graf 
8518253947eSLi Wei 		clock = jiffies_to_clock_t(abs(expires));
8528253947eSLi Wei 		clock = min_t(unsigned long, clock, INT_MAX);
8538253947eSLi Wei 		ci.rta_expires = (expires > 0) ? clock : -clock;
8548253947eSLi Wei 	}
855e3703b3dSThomas Graf 	return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
856e3703b3dSThomas Graf }
857e3703b3dSThomas Graf EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
8581da177e4SLinus Torvalds 
85993b2d4a2SDavid S. Miller static void set_operstate(struct net_device *dev, unsigned char transition)
860b00055aaSStefan Rompf {
861b00055aaSStefan Rompf 	unsigned char operstate = dev->operstate;
862b00055aaSStefan Rompf 
863b00055aaSStefan Rompf 	switch (transition) {
864b00055aaSStefan Rompf 	case IF_OPER_UP:
865b00055aaSStefan Rompf 		if ((operstate == IF_OPER_DORMANT ||
866eec517cdSAndrew Lunn 		     operstate == IF_OPER_TESTING ||
867b00055aaSStefan Rompf 		     operstate == IF_OPER_UNKNOWN) &&
868eec517cdSAndrew Lunn 		    !netif_dormant(dev) && !netif_testing(dev))
869b00055aaSStefan Rompf 			operstate = IF_OPER_UP;
870b00055aaSStefan Rompf 		break;
871b00055aaSStefan Rompf 
872eec517cdSAndrew Lunn 	case IF_OPER_TESTING:
873abbc7928SJuhee Kang 		if (netif_oper_up(dev))
874eec517cdSAndrew Lunn 			operstate = IF_OPER_TESTING;
875eec517cdSAndrew Lunn 		break;
876eec517cdSAndrew Lunn 
877b00055aaSStefan Rompf 	case IF_OPER_DORMANT:
878abbc7928SJuhee Kang 		if (netif_oper_up(dev))
879b00055aaSStefan Rompf 			operstate = IF_OPER_DORMANT;
880b00055aaSStefan Rompf 		break;
8813ff50b79SStephen Hemminger 	}
882b00055aaSStefan Rompf 
883b00055aaSStefan Rompf 	if (dev->operstate != operstate) {
884fd888e85SSebastian Andrzej Siewior 		write_lock(&dev_base_lock);
885b00055aaSStefan Rompf 		dev->operstate = operstate;
886fd888e85SSebastian Andrzej Siewior 		write_unlock(&dev_base_lock);
887b00055aaSStefan Rompf 		netdev_state_change(dev);
88893b2d4a2SDavid S. Miller 	}
889b00055aaSStefan Rompf }
890b00055aaSStefan Rompf 
891b1beb681SJiri Benc static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
892b1beb681SJiri Benc {
893b1beb681SJiri Benc 	return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
894b1beb681SJiri Benc 	       (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
895b1beb681SJiri Benc }
896b1beb681SJiri Benc 
8973729d502SPatrick McHardy static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
8983729d502SPatrick McHardy 					   const struct ifinfomsg *ifm)
8993729d502SPatrick McHardy {
9003729d502SPatrick McHardy 	unsigned int flags = ifm->ifi_flags;
9013729d502SPatrick McHardy 
9023729d502SPatrick McHardy 	/* bugwards compatibility: ifi_change == 0 is treated as ~0 */
9033729d502SPatrick McHardy 	if (ifm->ifi_change)
9043729d502SPatrick McHardy 		flags = (flags & ifm->ifi_change) |
905b1beb681SJiri Benc 			(rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
9063729d502SPatrick McHardy 
9073729d502SPatrick McHardy 	return flags;
9083729d502SPatrick McHardy }
9093729d502SPatrick McHardy 
910b60c5115SThomas Graf static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
911be1f3c2cSBen Hutchings 				 const struct rtnl_link_stats64 *b)
9121da177e4SLinus Torvalds {
913b60c5115SThomas Graf 	a->rx_packets = b->rx_packets;
914b60c5115SThomas Graf 	a->tx_packets = b->tx_packets;
915b60c5115SThomas Graf 	a->rx_bytes = b->rx_bytes;
916b60c5115SThomas Graf 	a->tx_bytes = b->tx_bytes;
917b60c5115SThomas Graf 	a->rx_errors = b->rx_errors;
918b60c5115SThomas Graf 	a->tx_errors = b->tx_errors;
919b60c5115SThomas Graf 	a->rx_dropped = b->rx_dropped;
920b60c5115SThomas Graf 	a->tx_dropped = b->tx_dropped;
921b60c5115SThomas Graf 
922b60c5115SThomas Graf 	a->multicast = b->multicast;
923b60c5115SThomas Graf 	a->collisions = b->collisions;
924b60c5115SThomas Graf 
925b60c5115SThomas Graf 	a->rx_length_errors = b->rx_length_errors;
926b60c5115SThomas Graf 	a->rx_over_errors = b->rx_over_errors;
927b60c5115SThomas Graf 	a->rx_crc_errors = b->rx_crc_errors;
928b60c5115SThomas Graf 	a->rx_frame_errors = b->rx_frame_errors;
929b60c5115SThomas Graf 	a->rx_fifo_errors = b->rx_fifo_errors;
930b60c5115SThomas Graf 	a->rx_missed_errors = b->rx_missed_errors;
931b60c5115SThomas Graf 
932b60c5115SThomas Graf 	a->tx_aborted_errors = b->tx_aborted_errors;
933b60c5115SThomas Graf 	a->tx_carrier_errors = b->tx_carrier_errors;
934b60c5115SThomas Graf 	a->tx_fifo_errors = b->tx_fifo_errors;
935b60c5115SThomas Graf 	a->tx_heartbeat_errors = b->tx_heartbeat_errors;
936b60c5115SThomas Graf 	a->tx_window_errors = b->tx_window_errors;
937b60c5115SThomas Graf 
938b60c5115SThomas Graf 	a->rx_compressed = b->rx_compressed;
939b60c5115SThomas Graf 	a->tx_compressed = b->tx_compressed;
9406e7333d3SJarod Wilson 
9416e7333d3SJarod Wilson 	a->rx_nohandler = b->rx_nohandler;
94210708f37SJan Engelhardt }
94310708f37SJan Engelhardt 
944c02db8c6SChris Wright /* All VF info */
945115c9b81SGreg Rose static inline int rtnl_vfinfo_size(const struct net_device *dev,
946115c9b81SGreg Rose 				   u32 ext_filter_mask)
947ebc08a6fSWilliams, Mitch A {
9489af15c38SPhil Sutter 	if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) {
949c02db8c6SChris Wright 		int num_vfs = dev_num_vf(dev->dev.parent);
9507e75f74aSSabrina Dubroca 		size_t size = nla_total_size(0);
951045de01aSScott Feldman 		size += num_vfs *
9527e75f74aSSabrina Dubroca 			(nla_total_size(0) +
9537e75f74aSSabrina Dubroca 			 nla_total_size(sizeof(struct ifla_vf_mac)) +
95475345f88SDenis Kirjanov 			 nla_total_size(sizeof(struct ifla_vf_broadcast)) +
9557e75f74aSSabrina Dubroca 			 nla_total_size(sizeof(struct ifla_vf_vlan)) +
9567e75f74aSSabrina Dubroca 			 nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
95779aab093SMoshe Shemesh 			 nla_total_size(MAX_VLAN_LIST_LEN *
95879aab093SMoshe Shemesh 					sizeof(struct ifla_vf_vlan_info)) +
959ed616689SSucheta Chakraborty 			 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
9607e75f74aSSabrina Dubroca 			 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
961945a3676SJiri Benc 			 nla_total_size(sizeof(struct ifla_vf_rate)) +
96201a3d796SVlad Zolotarov 			 nla_total_size(sizeof(struct ifla_vf_link_state)) +
9633b766cd8SEran Ben Elisha 			 nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
964fa0e21faSEdwin Peer 			 nla_total_size(sizeof(struct ifla_vf_trust)));
965fa0e21faSEdwin Peer 		if (~ext_filter_mask & RTEXT_FILTER_SKIP_STATS) {
966fa0e21faSEdwin Peer 			size += num_vfs *
967fa0e21faSEdwin Peer 				(nla_total_size(0) + /* nest IFLA_VF_STATS */
9683b766cd8SEran Ben Elisha 				 /* IFLA_VF_STATS_RX_PACKETS */
969343a6d8eSNicolas Dichtel 				 nla_total_size_64bit(sizeof(__u64)) +
9703b766cd8SEran Ben Elisha 				 /* IFLA_VF_STATS_TX_PACKETS */
971343a6d8eSNicolas Dichtel 				 nla_total_size_64bit(sizeof(__u64)) +
9723b766cd8SEran Ben Elisha 				 /* IFLA_VF_STATS_RX_BYTES */
973343a6d8eSNicolas Dichtel 				 nla_total_size_64bit(sizeof(__u64)) +
9743b766cd8SEran Ben Elisha 				 /* IFLA_VF_STATS_TX_BYTES */
975343a6d8eSNicolas Dichtel 				 nla_total_size_64bit(sizeof(__u64)) +
9763b766cd8SEran Ben Elisha 				 /* IFLA_VF_STATS_BROADCAST */
977343a6d8eSNicolas Dichtel 				 nla_total_size_64bit(sizeof(__u64)) +
9783b766cd8SEran Ben Elisha 				 /* IFLA_VF_STATS_MULTICAST */
979343a6d8eSNicolas Dichtel 				 nla_total_size_64bit(sizeof(__u64)) +
980c5a9f6f0SEugenia Emantayev 				 /* IFLA_VF_STATS_RX_DROPPED */
981c5a9f6f0SEugenia Emantayev 				 nla_total_size_64bit(sizeof(__u64)) +
982c5a9f6f0SEugenia Emantayev 				 /* IFLA_VF_STATS_TX_DROPPED */
983fa0e21faSEdwin Peer 				 nla_total_size_64bit(sizeof(__u64)));
984fa0e21faSEdwin Peer 		}
985c02db8c6SChris Wright 		return size;
986c02db8c6SChris Wright 	} else
987ebc08a6fSWilliams, Mitch A 		return 0;
988ebc08a6fSWilliams, Mitch A }
989ebc08a6fSWilliams, Mitch A 
990c53864fdSDavid Gibson static size_t rtnl_port_size(const struct net_device *dev,
991c53864fdSDavid Gibson 			     u32 ext_filter_mask)
99257b61080SScott Feldman {
99357b61080SScott Feldman 	size_t port_size = nla_total_size(4)		/* PORT_VF */
99457b61080SScott Feldman 		+ nla_total_size(PORT_PROFILE_MAX)	/* PORT_PROFILE */
99557b61080SScott Feldman 		+ nla_total_size(PORT_UUID_MAX)		/* PORT_INSTANCE_UUID */
99657b61080SScott Feldman 		+ nla_total_size(PORT_UUID_MAX)		/* PORT_HOST_UUID */
99757b61080SScott Feldman 		+ nla_total_size(1)			/* PROT_VDP_REQUEST */
99857b61080SScott Feldman 		+ nla_total_size(2);			/* PORT_VDP_RESPONSE */
99957b61080SScott Feldman 	size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
100057b61080SScott Feldman 	size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
100157b61080SScott Feldman 		+ port_size;
100257b61080SScott Feldman 	size_t port_self_size = nla_total_size(sizeof(struct nlattr))
100357b61080SScott Feldman 		+ port_size;
100457b61080SScott Feldman 
1005c53864fdSDavid Gibson 	if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
1006c53864fdSDavid Gibson 	    !(ext_filter_mask & RTEXT_FILTER_VF))
100757b61080SScott Feldman 		return 0;
100857b61080SScott Feldman 	if (dev_num_vf(dev->dev.parent))
100957b61080SScott Feldman 		return port_self_size + vf_ports_size +
101057b61080SScott Feldman 			vf_port_size * dev_num_vf(dev->dev.parent);
101157b61080SScott Feldman 	else
101257b61080SScott Feldman 		return port_self_size;
101357b61080SScott Feldman }
101457b61080SScott Feldman 
1015b5cdae32SDavid S. Miller static size_t rtnl_xdp_size(void)
1016d1fdd913SBrenden Blanco {
1017b3cfaa31SSabrina Dubroca 	size_t xdp_size = nla_total_size(0) +	/* nest IFLA_XDP */
101858038695SMartin KaFai Lau 			  nla_total_size(1) +	/* XDP_ATTACHED */
1019a25717d2SJakub Kicinski 			  nla_total_size(4) +	/* XDP_PROG_ID (or 1st mode) */
10204f91da26SJakub Kicinski 			  nla_total_size(4);	/* XDP_<mode>_PROG_ID */
1021d1fdd913SBrenden Blanco 
1022d1fdd913SBrenden Blanco 	return xdp_size;
1023d1fdd913SBrenden Blanco }
1024d1fdd913SBrenden Blanco 
102588f4fb0cSJiri Pirko static size_t rtnl_prop_list_size(const struct net_device *dev)
102688f4fb0cSJiri Pirko {
102788f4fb0cSJiri Pirko 	struct netdev_name_node *name_node;
102888f4fb0cSJiri Pirko 	size_t size;
102988f4fb0cSJiri Pirko 
103088f4fb0cSJiri Pirko 	if (list_empty(&dev->name_node->list))
103188f4fb0cSJiri Pirko 		return 0;
103288f4fb0cSJiri Pirko 	size = nla_total_size(0);
103388f4fb0cSJiri Pirko 	list_for_each_entry(name_node, &dev->name_node->list, list)
103488f4fb0cSJiri Pirko 		size += nla_total_size(ALTIFNAMSIZ);
103588f4fb0cSJiri Pirko 	return size;
103688f4fb0cSJiri Pirko }
103788f4fb0cSJiri Pirko 
1038829eb208SRoopa Prabhu static size_t rtnl_proto_down_size(const struct net_device *dev)
1039829eb208SRoopa Prabhu {
1040829eb208SRoopa Prabhu 	size_t size = nla_total_size(1);
1041829eb208SRoopa Prabhu 
1042829eb208SRoopa Prabhu 	if (dev->proto_down_reason)
1043829eb208SRoopa Prabhu 		size += nla_total_size(0) + nla_total_size(4);
1044829eb208SRoopa Prabhu 
1045829eb208SRoopa Prabhu 	return size;
1046829eb208SRoopa Prabhu }
1047829eb208SRoopa Prabhu 
1048dca56c30SJiri Pirko static size_t rtnl_devlink_port_size(const struct net_device *dev)
1049dca56c30SJiri Pirko {
1050dca56c30SJiri Pirko 	size_t size = nla_total_size(0); /* nest IFLA_DEVLINK_PORT */
1051dca56c30SJiri Pirko 
1052dca56c30SJiri Pirko 	if (dev->devlink_port)
1053dca56c30SJiri Pirko 		size += devlink_nl_port_handle_size(dev->devlink_port);
1054dca56c30SJiri Pirko 
1055dca56c30SJiri Pirko 	return size;
1056dca56c30SJiri Pirko }
1057dca56c30SJiri Pirko 
1058115c9b81SGreg Rose static noinline size_t if_nlmsg_size(const struct net_device *dev,
1059115c9b81SGreg Rose 				     u32 ext_filter_mask)
1060339bf98fSThomas Graf {
1061339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
1062339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
10630b815a1aSStephen Hemminger 	       + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
1064339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
1065270cb4d0SNicolas Dichtel 	       + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap))
1066339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rtnl_link_stats))
106735c58459SDavid S. Miller 	       + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
1068339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
1069339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
1070339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_TXQLEN */
1071339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_WEIGHT */
1072339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MTU */
1073339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_LINK */
1074339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MASTER */
10759a57247fSJiri Pirko 	       + nla_total_size(1) /* IFLA_CARRIER */
1076edbc0bb3SBen Greear 	       + nla_total_size(4) /* IFLA_PROMISCUITY */
10777e6e1b57SNicolas Dichtel 	       + nla_total_size(4) /* IFLA_ALLMULTI */
107876ff5cc9SJiri Pirko 	       + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
107976ff5cc9SJiri Pirko 	       + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
10806919756cSTobias Klauser 	       + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
10816919756cSTobias Klauser 	       + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
1082eac1b93cSCoco Li 	       + nla_total_size(4) /* IFLA_GRO_MAX_SIZE */
10839eefedd5SXin Long 	       + nla_total_size(4) /* IFLA_GSO_IPV4_MAX_SIZE */
10849eefedd5SXin Long 	       + nla_total_size(4) /* IFLA_GRO_IPV4_MAX_SIZE */
108589527be8SEric Dumazet 	       + nla_total_size(4) /* IFLA_TSO_MAX_SIZE */
108689527be8SEric Dumazet 	       + nla_total_size(4) /* IFLA_TSO_MAX_SEGS */
1087339bf98fSThomas Graf 	       + nla_total_size(1) /* IFLA_OPERSTATE */
108838f7b870SPatrick McHardy 	       + nla_total_size(1) /* IFLA_LINKMODE */
10892d3b479dSdavid decotigny 	       + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
1090d37512a2SNicolas Dichtel 	       + nla_total_size(4) /* IFLA_LINK_NETNSID */
1091db833d40SSerhey Popovych 	       + nla_total_size(4) /* IFLA_GROUP */
1092115c9b81SGreg Rose 	       + nla_total_size(ext_filter_mask
1093115c9b81SGreg Rose 			        & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
1094115c9b81SGreg Rose 	       + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
1095c53864fdSDavid Gibson 	       + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
1096f8ff182cSThomas Graf 	       + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
1097b1974ed0SArad, Ronen 	       + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
109882f28412SJiri Pirko 	       + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
109988d6378bSAnuradha Karuppiah 	       + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
1100c57c7a95SNicolas Dichtel 	       + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
1101b5cdae32SDavid S. Miller 	       + rtnl_xdp_size() /* IFLA_XDP */
11023d3ea5afSVlad Yasevich 	       + nla_total_size(4)  /* IFLA_EVENT */
11036621dd29SNicolas Dichtel 	       + nla_total_size(4)  /* IFLA_NEW_NETNSID */
110438e01b30SNicolas Dichtel 	       + nla_total_size(4)  /* IFLA_NEW_IFINDEX */
1105829eb208SRoopa Prabhu 	       + rtnl_proto_down_size(dev)  /* proto down */
11067e4a8d5aSChristian Brauner 	       + nla_total_size(4)  /* IFLA_TARGET_NETNSID */
1107b2d3bcfaSDavid Decotigny 	       + nla_total_size(4)  /* IFLA_CARRIER_UP_COUNT */
1108b2d3bcfaSDavid Decotigny 	       + nla_total_size(4)  /* IFLA_CARRIER_DOWN_COUNT */
11093e7a50ceSStephen Hemminger 	       + nla_total_size(4)  /* IFLA_MIN_MTU */
11103e7a50ceSStephen Hemminger 	       + nla_total_size(4)  /* IFLA_MAX_MTU */
111188f4fb0cSJiri Pirko 	       + rtnl_prop_list_size(dev)
1112f74877a5SMichal Kubecek 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_PERM_ADDRESS */
1113dca56c30SJiri Pirko 	       + rtnl_devlink_port_size(dev)
111479e1ad14SJiri Benc 	       + 0;
1115339bf98fSThomas Graf }
1116339bf98fSThomas Graf 
111757b61080SScott Feldman static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
111857b61080SScott Feldman {
111957b61080SScott Feldman 	struct nlattr *vf_ports;
112057b61080SScott Feldman 	struct nlattr *vf_port;
112157b61080SScott Feldman 	int vf;
112257b61080SScott Feldman 	int err;
112357b61080SScott Feldman 
1124ae0be8deSMichal Kubecek 	vf_ports = nla_nest_start_noflag(skb, IFLA_VF_PORTS);
112557b61080SScott Feldman 	if (!vf_ports)
112657b61080SScott Feldman 		return -EMSGSIZE;
112757b61080SScott Feldman 
112857b61080SScott Feldman 	for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
1129ae0be8deSMichal Kubecek 		vf_port = nla_nest_start_noflag(skb, IFLA_VF_PORT);
11308ca94183SScott Feldman 		if (!vf_port)
11318ca94183SScott Feldman 			goto nla_put_failure;
1132a6574349SDavid S. Miller 		if (nla_put_u32(skb, IFLA_PORT_VF, vf))
1133a6574349SDavid S. Miller 			goto nla_put_failure;
113457b61080SScott Feldman 		err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
11358ca94183SScott Feldman 		if (err == -EMSGSIZE)
11368ca94183SScott Feldman 			goto nla_put_failure;
113757b61080SScott Feldman 		if (err) {
113857b61080SScott Feldman 			nla_nest_cancel(skb, vf_port);
113957b61080SScott Feldman 			continue;
114057b61080SScott Feldman 		}
114157b61080SScott Feldman 		nla_nest_end(skb, vf_port);
114257b61080SScott Feldman 	}
114357b61080SScott Feldman 
114457b61080SScott Feldman 	nla_nest_end(skb, vf_ports);
114557b61080SScott Feldman 
114657b61080SScott Feldman 	return 0;
11478ca94183SScott Feldman 
11488ca94183SScott Feldman nla_put_failure:
11498ca94183SScott Feldman 	nla_nest_cancel(skb, vf_ports);
11508ca94183SScott Feldman 	return -EMSGSIZE;
115157b61080SScott Feldman }
115257b61080SScott Feldman 
115357b61080SScott Feldman static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
115457b61080SScott Feldman {
115557b61080SScott Feldman 	struct nlattr *port_self;
115657b61080SScott Feldman 	int err;
115757b61080SScott Feldman 
1158ae0be8deSMichal Kubecek 	port_self = nla_nest_start_noflag(skb, IFLA_PORT_SELF);
115957b61080SScott Feldman 	if (!port_self)
116057b61080SScott Feldman 		return -EMSGSIZE;
116157b61080SScott Feldman 
116257b61080SScott Feldman 	err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
116357b61080SScott Feldman 	if (err) {
116457b61080SScott Feldman 		nla_nest_cancel(skb, port_self);
11658ca94183SScott Feldman 		return (err == -EMSGSIZE) ? err : 0;
116657b61080SScott Feldman 	}
116757b61080SScott Feldman 
116857b61080SScott Feldman 	nla_nest_end(skb, port_self);
116957b61080SScott Feldman 
117057b61080SScott Feldman 	return 0;
117157b61080SScott Feldman }
117257b61080SScott Feldman 
1173c53864fdSDavid Gibson static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
1174c53864fdSDavid Gibson 			  u32 ext_filter_mask)
117557b61080SScott Feldman {
117657b61080SScott Feldman 	int err;
117757b61080SScott Feldman 
1178c53864fdSDavid Gibson 	if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
1179c53864fdSDavid Gibson 	    !(ext_filter_mask & RTEXT_FILTER_VF))
118057b61080SScott Feldman 		return 0;
118157b61080SScott Feldman 
118257b61080SScott Feldman 	err = rtnl_port_self_fill(skb, dev);
118357b61080SScott Feldman 	if (err)
118457b61080SScott Feldman 		return err;
118557b61080SScott Feldman 
118657b61080SScott Feldman 	if (dev_num_vf(dev->dev.parent)) {
118757b61080SScott Feldman 		err = rtnl_vf_ports_fill(skb, dev);
118857b61080SScott Feldman 		if (err)
118957b61080SScott Feldman 			return err;
119057b61080SScott Feldman 	}
119157b61080SScott Feldman 
119257b61080SScott Feldman 	return 0;
119357b61080SScott Feldman }
119457b61080SScott Feldman 
119566cae9edSJiri Pirko static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
119666cae9edSJiri Pirko {
119766cae9edSJiri Pirko 	int err;
119802637fceSJiri Pirko 	struct netdev_phys_item_id ppid;
119966cae9edSJiri Pirko 
120066cae9edSJiri Pirko 	err = dev_get_phys_port_id(dev, &ppid);
120166cae9edSJiri Pirko 	if (err) {
120266cae9edSJiri Pirko 		if (err == -EOPNOTSUPP)
120366cae9edSJiri Pirko 			return 0;
120466cae9edSJiri Pirko 		return err;
120566cae9edSJiri Pirko 	}
120666cae9edSJiri Pirko 
120766cae9edSJiri Pirko 	if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
120866cae9edSJiri Pirko 		return -EMSGSIZE;
120966cae9edSJiri Pirko 
121066cae9edSJiri Pirko 	return 0;
121166cae9edSJiri Pirko }
121266cae9edSJiri Pirko 
1213db24a904SDavid Ahern static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1214db24a904SDavid Ahern {
1215db24a904SDavid Ahern 	char name[IFNAMSIZ];
1216db24a904SDavid Ahern 	int err;
1217db24a904SDavid Ahern 
1218db24a904SDavid Ahern 	err = dev_get_phys_port_name(dev, name, sizeof(name));
1219db24a904SDavid Ahern 	if (err) {
1220db24a904SDavid Ahern 		if (err == -EOPNOTSUPP)
1221db24a904SDavid Ahern 			return 0;
1222db24a904SDavid Ahern 		return err;
1223db24a904SDavid Ahern 	}
1224db24a904SDavid Ahern 
122577ef033bSMichal Schmidt 	if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
1226db24a904SDavid Ahern 		return -EMSGSIZE;
1227db24a904SDavid Ahern 
1228db24a904SDavid Ahern 	return 0;
1229db24a904SDavid Ahern }
1230db24a904SDavid Ahern 
123182f28412SJiri Pirko static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
123282f28412SJiri Pirko {
1233bccb3025SFlorian Fainelli 	struct netdev_phys_item_id ppid = { };
123482f28412SJiri Pirko 	int err;
123582f28412SJiri Pirko 
1236bccb3025SFlorian Fainelli 	err = dev_get_port_parent_id(dev, &ppid, false);
123782f28412SJiri Pirko 	if (err) {
123882f28412SJiri Pirko 		if (err == -EOPNOTSUPP)
123982f28412SJiri Pirko 			return 0;
124082f28412SJiri Pirko 		return err;
124182f28412SJiri Pirko 	}
124282f28412SJiri Pirko 
1243bccb3025SFlorian Fainelli 	if (nla_put(skb, IFLA_PHYS_SWITCH_ID, ppid.id_len, ppid.id))
124482f28412SJiri Pirko 		return -EMSGSIZE;
124582f28412SJiri Pirko 
124682f28412SJiri Pirko 	return 0;
124782f28412SJiri Pirko }
124882f28412SJiri Pirko 
1249b22b941bSHannes Frederic Sowa static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1250b22b941bSHannes Frederic Sowa 					      struct net_device *dev)
1251b22b941bSHannes Frederic Sowa {
1252550bce59SRoopa Prabhu 	struct rtnl_link_stats64 *sp;
1253b22b941bSHannes Frederic Sowa 	struct nlattr *attr;
1254b22b941bSHannes Frederic Sowa 
125558414d32SNicolas Dichtel 	attr = nla_reserve_64bit(skb, IFLA_STATS64,
125658414d32SNicolas Dichtel 				 sizeof(struct rtnl_link_stats64), IFLA_PAD);
1257b22b941bSHannes Frederic Sowa 	if (!attr)
1258b22b941bSHannes Frederic Sowa 		return -EMSGSIZE;
1259b22b941bSHannes Frederic Sowa 
1260550bce59SRoopa Prabhu 	sp = nla_data(attr);
1261550bce59SRoopa Prabhu 	dev_get_stats(dev, sp);
1262550bce59SRoopa Prabhu 
1263550bce59SRoopa Prabhu 	attr = nla_reserve(skb, IFLA_STATS,
1264550bce59SRoopa Prabhu 			   sizeof(struct rtnl_link_stats));
1265550bce59SRoopa Prabhu 	if (!attr)
1266550bce59SRoopa Prabhu 		return -EMSGSIZE;
1267550bce59SRoopa Prabhu 
1268550bce59SRoopa Prabhu 	copy_rtnl_link_stats(nla_data(attr), sp);
1269b22b941bSHannes Frederic Sowa 
1270b22b941bSHannes Frederic Sowa 	return 0;
1271b22b941bSHannes Frederic Sowa }
1272b22b941bSHannes Frederic Sowa 
1273b22b941bSHannes Frederic Sowa static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1274b22b941bSHannes Frederic Sowa 					       struct net_device *dev,
1275b22b941bSHannes Frederic Sowa 					       int vfs_num,
1276fa0e21faSEdwin Peer 					       struct nlattr *vfinfo,
1277fa0e21faSEdwin Peer 					       u32 ext_filter_mask)
1278b22b941bSHannes Frederic Sowa {
1279b22b941bSHannes Frederic Sowa 	struct ifla_vf_rss_query_en vf_rss_query_en;
128079aab093SMoshe Shemesh 	struct nlattr *vf, *vfstats, *vfvlanlist;
1281b22b941bSHannes Frederic Sowa 	struct ifla_vf_link_state vf_linkstate;
128279aab093SMoshe Shemesh 	struct ifla_vf_vlan_info vf_vlan_info;
1283b22b941bSHannes Frederic Sowa 	struct ifla_vf_spoofchk vf_spoofchk;
1284b22b941bSHannes Frederic Sowa 	struct ifla_vf_tx_rate vf_tx_rate;
1285b22b941bSHannes Frederic Sowa 	struct ifla_vf_stats vf_stats;
1286b22b941bSHannes Frederic Sowa 	struct ifla_vf_trust vf_trust;
1287b22b941bSHannes Frederic Sowa 	struct ifla_vf_vlan vf_vlan;
1288b22b941bSHannes Frederic Sowa 	struct ifla_vf_rate vf_rate;
1289b22b941bSHannes Frederic Sowa 	struct ifla_vf_mac vf_mac;
129075345f88SDenis Kirjanov 	struct ifla_vf_broadcast vf_broadcast;
1291b22b941bSHannes Frederic Sowa 	struct ifla_vf_info ivi;
129230aad417SDanit Goldberg 	struct ifla_vf_guid node_guid;
129330aad417SDanit Goldberg 	struct ifla_vf_guid port_guid;
1294b22b941bSHannes Frederic Sowa 
12950eed9cf5SMintz, Yuval 	memset(&ivi, 0, sizeof(ivi));
12960eed9cf5SMintz, Yuval 
1297b22b941bSHannes Frederic Sowa 	/* Not all SR-IOV capable drivers support the
1298b22b941bSHannes Frederic Sowa 	 * spoofcheck and "RSS query enable" query.  Preset to
1299b22b941bSHannes Frederic Sowa 	 * -1 so the user space tool can detect that the driver
1300b22b941bSHannes Frederic Sowa 	 * didn't report anything.
1301b22b941bSHannes Frederic Sowa 	 */
1302b22b941bSHannes Frederic Sowa 	ivi.spoofchk = -1;
1303b22b941bSHannes Frederic Sowa 	ivi.rss_query_en = -1;
1304b22b941bSHannes Frederic Sowa 	ivi.trusted = -1;
1305b22b941bSHannes Frederic Sowa 	/* The default value for VF link state is "auto"
1306b22b941bSHannes Frederic Sowa 	 * IFLA_VF_LINK_STATE_AUTO which equals zero
1307b22b941bSHannes Frederic Sowa 	 */
1308b22b941bSHannes Frederic Sowa 	ivi.linkstate = 0;
130979aab093SMoshe Shemesh 	/* VLAN Protocol by default is 802.1Q */
131079aab093SMoshe Shemesh 	ivi.vlan_proto = htons(ETH_P_8021Q);
1311b22b941bSHannes Frederic Sowa 	if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1312b22b941bSHannes Frederic Sowa 		return 0;
1313b22b941bSHannes Frederic Sowa 
1314775f4f05SDan Carpenter 	memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
13159fbf082fSLeon Romanovsky 	memset(&node_guid, 0, sizeof(node_guid));
13169fbf082fSLeon Romanovsky 	memset(&port_guid, 0, sizeof(port_guid));
1317775f4f05SDan Carpenter 
1318b22b941bSHannes Frederic Sowa 	vf_mac.vf =
1319b22b941bSHannes Frederic Sowa 		vf_vlan.vf =
132079aab093SMoshe Shemesh 		vf_vlan_info.vf =
1321b22b941bSHannes Frederic Sowa 		vf_rate.vf =
1322b22b941bSHannes Frederic Sowa 		vf_tx_rate.vf =
1323b22b941bSHannes Frederic Sowa 		vf_spoofchk.vf =
1324b22b941bSHannes Frederic Sowa 		vf_linkstate.vf =
1325b22b941bSHannes Frederic Sowa 		vf_rss_query_en.vf =
13269aed6ae0SDanit Goldberg 		vf_trust.vf =
13279aed6ae0SDanit Goldberg 		node_guid.vf =
13289aed6ae0SDanit Goldberg 		port_guid.vf = ivi.vf;
1329b22b941bSHannes Frederic Sowa 
1330b22b941bSHannes Frederic Sowa 	memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
133175345f88SDenis Kirjanov 	memcpy(vf_broadcast.broadcast, dev->broadcast, dev->addr_len);
1332b22b941bSHannes Frederic Sowa 	vf_vlan.vlan = ivi.vlan;
1333b22b941bSHannes Frederic Sowa 	vf_vlan.qos = ivi.qos;
133479aab093SMoshe Shemesh 	vf_vlan_info.vlan = ivi.vlan;
133579aab093SMoshe Shemesh 	vf_vlan_info.qos = ivi.qos;
133679aab093SMoshe Shemesh 	vf_vlan_info.vlan_proto = ivi.vlan_proto;
1337b22b941bSHannes Frederic Sowa 	vf_tx_rate.rate = ivi.max_tx_rate;
1338b22b941bSHannes Frederic Sowa 	vf_rate.min_tx_rate = ivi.min_tx_rate;
1339b22b941bSHannes Frederic Sowa 	vf_rate.max_tx_rate = ivi.max_tx_rate;
1340b22b941bSHannes Frederic Sowa 	vf_spoofchk.setting = ivi.spoofchk;
1341b22b941bSHannes Frederic Sowa 	vf_linkstate.link_state = ivi.linkstate;
1342b22b941bSHannes Frederic Sowa 	vf_rss_query_en.setting = ivi.rss_query_en;
1343b22b941bSHannes Frederic Sowa 	vf_trust.setting = ivi.trusted;
1344ae0be8deSMichal Kubecek 	vf = nla_nest_start_noflag(skb, IFLA_VF_INFO);
134579aab093SMoshe Shemesh 	if (!vf)
134679aab093SMoshe Shemesh 		goto nla_put_vfinfo_failure;
1347b22b941bSHannes Frederic Sowa 	if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
134875345f88SDenis Kirjanov 	    nla_put(skb, IFLA_VF_BROADCAST, sizeof(vf_broadcast), &vf_broadcast) ||
1349b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1350b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1351b22b941bSHannes Frederic Sowa 		    &vf_rate) ||
1352b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1353b22b941bSHannes Frederic Sowa 		    &vf_tx_rate) ||
1354b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1355b22b941bSHannes Frederic Sowa 		    &vf_spoofchk) ||
1356b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1357b22b941bSHannes Frederic Sowa 		    &vf_linkstate) ||
1358b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1359b22b941bSHannes Frederic Sowa 		    sizeof(vf_rss_query_en),
1360b22b941bSHannes Frederic Sowa 		    &vf_rss_query_en) ||
1361b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_TRUST,
1362b22b941bSHannes Frederic Sowa 		    sizeof(vf_trust), &vf_trust))
136379aab093SMoshe Shemesh 		goto nla_put_vf_failure;
136430aad417SDanit Goldberg 
136530aad417SDanit Goldberg 	if (dev->netdev_ops->ndo_get_vf_guid &&
136630aad417SDanit Goldberg 	    !dev->netdev_ops->ndo_get_vf_guid(dev, vfs_num, &node_guid,
136730aad417SDanit Goldberg 					      &port_guid)) {
136830aad417SDanit Goldberg 		if (nla_put(skb, IFLA_VF_IB_NODE_GUID, sizeof(node_guid),
136930aad417SDanit Goldberg 			    &node_guid) ||
137030aad417SDanit Goldberg 		    nla_put(skb, IFLA_VF_IB_PORT_GUID, sizeof(port_guid),
137130aad417SDanit Goldberg 			    &port_guid))
137230aad417SDanit Goldberg 			goto nla_put_vf_failure;
137330aad417SDanit Goldberg 	}
1374ae0be8deSMichal Kubecek 	vfvlanlist = nla_nest_start_noflag(skb, IFLA_VF_VLAN_LIST);
137579aab093SMoshe Shemesh 	if (!vfvlanlist)
137679aab093SMoshe Shemesh 		goto nla_put_vf_failure;
137779aab093SMoshe Shemesh 	if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
137879aab093SMoshe Shemesh 		    &vf_vlan_info)) {
137979aab093SMoshe Shemesh 		nla_nest_cancel(skb, vfvlanlist);
138079aab093SMoshe Shemesh 		goto nla_put_vf_failure;
138179aab093SMoshe Shemesh 	}
138279aab093SMoshe Shemesh 	nla_nest_end(skb, vfvlanlist);
1383fa0e21faSEdwin Peer 	if (~ext_filter_mask & RTEXT_FILTER_SKIP_STATS) {
1384b22b941bSHannes Frederic Sowa 		memset(&vf_stats, 0, sizeof(vf_stats));
1385b22b941bSHannes Frederic Sowa 		if (dev->netdev_ops->ndo_get_vf_stats)
1386b22b941bSHannes Frederic Sowa 			dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1387b22b941bSHannes Frederic Sowa 							  &vf_stats);
1388ae0be8deSMichal Kubecek 		vfstats = nla_nest_start_noflag(skb, IFLA_VF_STATS);
138979aab093SMoshe Shemesh 		if (!vfstats)
139079aab093SMoshe Shemesh 			goto nla_put_vf_failure;
1391343a6d8eSNicolas Dichtel 		if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1392343a6d8eSNicolas Dichtel 				      vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1393343a6d8eSNicolas Dichtel 		    nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1394343a6d8eSNicolas Dichtel 				      vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1395343a6d8eSNicolas Dichtel 		    nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1396343a6d8eSNicolas Dichtel 				      vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1397343a6d8eSNicolas Dichtel 		    nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1398343a6d8eSNicolas Dichtel 				      vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1399343a6d8eSNicolas Dichtel 		    nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1400343a6d8eSNicolas Dichtel 				      vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1401343a6d8eSNicolas Dichtel 		    nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
1402c5a9f6f0SEugenia Emantayev 				      vf_stats.multicast, IFLA_VF_STATS_PAD) ||
1403c5a9f6f0SEugenia Emantayev 		    nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED,
1404c5a9f6f0SEugenia Emantayev 				      vf_stats.rx_dropped, IFLA_VF_STATS_PAD) ||
1405c5a9f6f0SEugenia Emantayev 		    nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED,
1406c5a9f6f0SEugenia Emantayev 				      vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) {
140779aab093SMoshe Shemesh 			nla_nest_cancel(skb, vfstats);
140879aab093SMoshe Shemesh 			goto nla_put_vf_failure;
140979aab093SMoshe Shemesh 		}
1410b22b941bSHannes Frederic Sowa 		nla_nest_end(skb, vfstats);
1411fa0e21faSEdwin Peer 	}
1412b22b941bSHannes Frederic Sowa 	nla_nest_end(skb, vf);
1413b22b941bSHannes Frederic Sowa 	return 0;
141479aab093SMoshe Shemesh 
141579aab093SMoshe Shemesh nla_put_vf_failure:
141679aab093SMoshe Shemesh 	nla_nest_cancel(skb, vf);
141779aab093SMoshe Shemesh nla_put_vfinfo_failure:
141879aab093SMoshe Shemesh 	nla_nest_cancel(skb, vfinfo);
141979aab093SMoshe Shemesh 	return -EMSGSIZE;
1420b22b941bSHannes Frederic Sowa }
1421b22b941bSHannes Frederic Sowa 
1422250fc3dfSFlorian Westphal static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb,
1423250fc3dfSFlorian Westphal 					   struct net_device *dev,
1424250fc3dfSFlorian Westphal 					   u32 ext_filter_mask)
1425250fc3dfSFlorian Westphal {
1426250fc3dfSFlorian Westphal 	struct nlattr *vfinfo;
1427250fc3dfSFlorian Westphal 	int i, num_vfs;
1428250fc3dfSFlorian Westphal 
1429250fc3dfSFlorian Westphal 	if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0))
1430250fc3dfSFlorian Westphal 		return 0;
1431250fc3dfSFlorian Westphal 
1432250fc3dfSFlorian Westphal 	num_vfs = dev_num_vf(dev->dev.parent);
1433250fc3dfSFlorian Westphal 	if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs))
1434250fc3dfSFlorian Westphal 		return -EMSGSIZE;
1435250fc3dfSFlorian Westphal 
1436250fc3dfSFlorian Westphal 	if (!dev->netdev_ops->ndo_get_vf_config)
1437250fc3dfSFlorian Westphal 		return 0;
1438250fc3dfSFlorian Westphal 
1439ae0be8deSMichal Kubecek 	vfinfo = nla_nest_start_noflag(skb, IFLA_VFINFO_LIST);
1440250fc3dfSFlorian Westphal 	if (!vfinfo)
1441250fc3dfSFlorian Westphal 		return -EMSGSIZE;
1442250fc3dfSFlorian Westphal 
1443250fc3dfSFlorian Westphal 	for (i = 0; i < num_vfs; i++) {
1444fa0e21faSEdwin Peer 		if (rtnl_fill_vfinfo(skb, dev, i, vfinfo, ext_filter_mask))
1445250fc3dfSFlorian Westphal 			return -EMSGSIZE;
1446250fc3dfSFlorian Westphal 	}
1447250fc3dfSFlorian Westphal 
1448250fc3dfSFlorian Westphal 	nla_nest_end(skb, vfinfo);
1449250fc3dfSFlorian Westphal 	return 0;
1450250fc3dfSFlorian Westphal }
1451250fc3dfSFlorian Westphal 
1452b22b941bSHannes Frederic Sowa static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1453b22b941bSHannes Frederic Sowa {
14545f8e4474SKangjie Lu 	struct rtnl_link_ifmap map;
14555f8e4474SKangjie Lu 
14565f8e4474SKangjie Lu 	memset(&map, 0, sizeof(map));
14575f8e4474SKangjie Lu 	map.mem_start   = dev->mem_start;
14585f8e4474SKangjie Lu 	map.mem_end     = dev->mem_end;
14595f8e4474SKangjie Lu 	map.base_addr   = dev->base_addr;
14605f8e4474SKangjie Lu 	map.irq         = dev->irq;
14615f8e4474SKangjie Lu 	map.dma         = dev->dma;
14625f8e4474SKangjie Lu 	map.port        = dev->if_port;
14635f8e4474SKangjie Lu 
1464270cb4d0SNicolas Dichtel 	if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
1465b22b941bSHannes Frederic Sowa 		return -EMSGSIZE;
1466b22b941bSHannes Frederic Sowa 
1467b22b941bSHannes Frederic Sowa 	return 0;
1468b22b941bSHannes Frederic Sowa }
1469b22b941bSHannes Frederic Sowa 
1470a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_skb(struct net_device *dev)
1471d67b9cd2SDaniel Borkmann {
147258038695SMartin KaFai Lau 	const struct bpf_prog *generic_xdp_prog;
1473d67b9cd2SDaniel Borkmann 
1474d67b9cd2SDaniel Borkmann 	ASSERT_RTNL();
1475d67b9cd2SDaniel Borkmann 
147658038695SMartin KaFai Lau 	generic_xdp_prog = rtnl_dereference(dev->xdp_prog);
1477a25717d2SJakub Kicinski 	if (!generic_xdp_prog)
1478a25717d2SJakub Kicinski 		return 0;
1479a25717d2SJakub Kicinski 	return generic_xdp_prog->aux->id;
148058038695SMartin KaFai Lau }
1481ce158e58SJakub Kicinski 
1482a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_drv(struct net_device *dev)
1483a25717d2SJakub Kicinski {
14847f0a8382SAndrii Nakryiko 	return dev_xdp_prog_id(dev, XDP_MODE_DRV);
1485a25717d2SJakub Kicinski }
1486118b4aa2SJakub Kicinski 
1487a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_hw(struct net_device *dev)
1488a25717d2SJakub Kicinski {
14897f0a8382SAndrii Nakryiko 	return dev_xdp_prog_id(dev, XDP_MODE_HW);
1490a25717d2SJakub Kicinski }
1491a25717d2SJakub Kicinski 
1492a25717d2SJakub Kicinski static int rtnl_xdp_report_one(struct sk_buff *skb, struct net_device *dev,
1493a25717d2SJakub Kicinski 			       u32 *prog_id, u8 *mode, u8 tgt_mode, u32 attr,
1494a25717d2SJakub Kicinski 			       u32 (*get_prog_id)(struct net_device *dev))
1495a25717d2SJakub Kicinski {
1496a25717d2SJakub Kicinski 	u32 curr_id;
1497a25717d2SJakub Kicinski 	int err;
1498a25717d2SJakub Kicinski 
1499a25717d2SJakub Kicinski 	curr_id = get_prog_id(dev);
1500a25717d2SJakub Kicinski 	if (!curr_id)
1501a25717d2SJakub Kicinski 		return 0;
1502a25717d2SJakub Kicinski 
1503a25717d2SJakub Kicinski 	*prog_id = curr_id;
1504a25717d2SJakub Kicinski 	err = nla_put_u32(skb, attr, curr_id);
1505a25717d2SJakub Kicinski 	if (err)
1506a25717d2SJakub Kicinski 		return err;
1507a25717d2SJakub Kicinski 
1508a25717d2SJakub Kicinski 	if (*mode != XDP_ATTACHED_NONE)
1509a25717d2SJakub Kicinski 		*mode = XDP_ATTACHED_MULTI;
1510a25717d2SJakub Kicinski 	else
1511a25717d2SJakub Kicinski 		*mode = tgt_mode;
1512a25717d2SJakub Kicinski 
1513a25717d2SJakub Kicinski 	return 0;
1514d67b9cd2SDaniel Borkmann }
1515d67b9cd2SDaniel Borkmann 
1516d1fdd913SBrenden Blanco static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1517d1fdd913SBrenden Blanco {
1518d1fdd913SBrenden Blanco 	struct nlattr *xdp;
151958038695SMartin KaFai Lau 	u32 prog_id;
1520d1fdd913SBrenden Blanco 	int err;
15214f91da26SJakub Kicinski 	u8 mode;
1522d1fdd913SBrenden Blanco 
1523ae0be8deSMichal Kubecek 	xdp = nla_nest_start_noflag(skb, IFLA_XDP);
1524d1fdd913SBrenden Blanco 	if (!xdp)
1525d1fdd913SBrenden Blanco 		return -EMSGSIZE;
1526b5cdae32SDavid S. Miller 
1527a25717d2SJakub Kicinski 	prog_id = 0;
1528a25717d2SJakub Kicinski 	mode = XDP_ATTACHED_NONE;
1529202aabe8SJakub Kicinski 	err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_SKB,
1530202aabe8SJakub Kicinski 				  IFLA_XDP_SKB_PROG_ID, rtnl_xdp_prog_skb);
1531202aabe8SJakub Kicinski 	if (err)
1532a25717d2SJakub Kicinski 		goto err_cancel;
1533202aabe8SJakub Kicinski 	err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_DRV,
1534202aabe8SJakub Kicinski 				  IFLA_XDP_DRV_PROG_ID, rtnl_xdp_prog_drv);
1535202aabe8SJakub Kicinski 	if (err)
1536a25717d2SJakub Kicinski 		goto err_cancel;
1537202aabe8SJakub Kicinski 	err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_HW,
1538202aabe8SJakub Kicinski 				  IFLA_XDP_HW_PROG_ID, rtnl_xdp_prog_hw);
1539202aabe8SJakub Kicinski 	if (err)
1540a25717d2SJakub Kicinski 		goto err_cancel;
1541a25717d2SJakub Kicinski 
15424f91da26SJakub Kicinski 	err = nla_put_u8(skb, IFLA_XDP_ATTACHED, mode);
1543d1fdd913SBrenden Blanco 	if (err)
1544d1fdd913SBrenden Blanco 		goto err_cancel;
1545d1fdd913SBrenden Blanco 
1546a25717d2SJakub Kicinski 	if (prog_id && mode != XDP_ATTACHED_MULTI) {
154758038695SMartin KaFai Lau 		err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id);
154858038695SMartin KaFai Lau 		if (err)
154958038695SMartin KaFai Lau 			goto err_cancel;
155058038695SMartin KaFai Lau 	}
155158038695SMartin KaFai Lau 
1552d1fdd913SBrenden Blanco 	nla_nest_end(skb, xdp);
1553d1fdd913SBrenden Blanco 	return 0;
1554d1fdd913SBrenden Blanco 
1555d1fdd913SBrenden Blanco err_cancel:
1556d1fdd913SBrenden Blanco 	nla_nest_cancel(skb, xdp);
1557d1fdd913SBrenden Blanco 	return err;
1558d1fdd913SBrenden Blanco }
1559d1fdd913SBrenden Blanco 
15603d3ea5afSVlad Yasevich static u32 rtnl_get_event(unsigned long event)
15613d3ea5afSVlad Yasevich {
15623d3ea5afSVlad Yasevich 	u32 rtnl_event_type = IFLA_EVENT_NONE;
15633d3ea5afSVlad Yasevich 
15643d3ea5afSVlad Yasevich 	switch (event) {
15653d3ea5afSVlad Yasevich 	case NETDEV_REBOOT:
15663d3ea5afSVlad Yasevich 		rtnl_event_type = IFLA_EVENT_REBOOT;
15673d3ea5afSVlad Yasevich 		break;
15683d3ea5afSVlad Yasevich 	case NETDEV_FEAT_CHANGE:
15693d3ea5afSVlad Yasevich 		rtnl_event_type = IFLA_EVENT_FEATURES;
15703d3ea5afSVlad Yasevich 		break;
15713d3ea5afSVlad Yasevich 	case NETDEV_BONDING_FAILOVER:
15723d3ea5afSVlad Yasevich 		rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER;
15733d3ea5afSVlad Yasevich 		break;
15743d3ea5afSVlad Yasevich 	case NETDEV_NOTIFY_PEERS:
15753d3ea5afSVlad Yasevich 		rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS;
15763d3ea5afSVlad Yasevich 		break;
15773d3ea5afSVlad Yasevich 	case NETDEV_RESEND_IGMP:
15783d3ea5afSVlad Yasevich 		rtnl_event_type = IFLA_EVENT_IGMP_RESEND;
15793d3ea5afSVlad Yasevich 		break;
15803d3ea5afSVlad Yasevich 	case NETDEV_CHANGEINFODATA:
15813d3ea5afSVlad Yasevich 		rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS;
15823d3ea5afSVlad Yasevich 		break;
15833d3ea5afSVlad Yasevich 	default:
15843d3ea5afSVlad Yasevich 		break;
15853d3ea5afSVlad Yasevich 	}
15863d3ea5afSVlad Yasevich 
15873d3ea5afSVlad Yasevich 	return rtnl_event_type;
15883d3ea5afSVlad Yasevich }
15893d3ea5afSVlad Yasevich 
159079110a04SFlorian Westphal static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev)
159179110a04SFlorian Westphal {
159279110a04SFlorian Westphal 	const struct net_device *upper_dev;
159379110a04SFlorian Westphal 	int ret = 0;
159479110a04SFlorian Westphal 
159579110a04SFlorian Westphal 	rcu_read_lock();
159679110a04SFlorian Westphal 
159779110a04SFlorian Westphal 	upper_dev = netdev_master_upper_dev_get_rcu(dev);
159879110a04SFlorian Westphal 	if (upper_dev)
159979110a04SFlorian Westphal 		ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex);
160079110a04SFlorian Westphal 
160179110a04SFlorian Westphal 	rcu_read_unlock();
160279110a04SFlorian Westphal 	return ret;
160379110a04SFlorian Westphal }
160479110a04SFlorian Westphal 
1605feadc4b6SSabrina Dubroca static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev,
1606feadc4b6SSabrina Dubroca 			  bool force)
160779110a04SFlorian Westphal {
160879110a04SFlorian Westphal 	int ifindex = dev_get_iflink(dev);
160979110a04SFlorian Westphal 
1610feadc4b6SSabrina Dubroca 	if (force || dev->ifindex != ifindex)
161179110a04SFlorian Westphal 		return nla_put_u32(skb, IFLA_LINK, ifindex);
1612feadc4b6SSabrina Dubroca 
1613feadc4b6SSabrina Dubroca 	return 0;
161479110a04SFlorian Westphal }
161579110a04SFlorian Westphal 
16166c557001SFlorian Westphal static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb,
16176c557001SFlorian Westphal 					      struct net_device *dev)
16186c557001SFlorian Westphal {
16196c557001SFlorian Westphal 	char buf[IFALIASZ];
16206c557001SFlorian Westphal 	int ret;
16216c557001SFlorian Westphal 
16226c557001SFlorian Westphal 	ret = dev_get_alias(dev, buf, sizeof(buf));
16236c557001SFlorian Westphal 	return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0;
16246c557001SFlorian Westphal }
16256c557001SFlorian Westphal 
1626b1e66b9aSFlorian Westphal static int rtnl_fill_link_netnsid(struct sk_buff *skb,
162779e1ad14SJiri Benc 				  const struct net_device *dev,
1628d4e4fdf9SGuillaume Nault 				  struct net *src_net, gfp_t gfp)
1629b1e66b9aSFlorian Westphal {
1630feadc4b6SSabrina Dubroca 	bool put_iflink = false;
1631feadc4b6SSabrina Dubroca 
1632b1e66b9aSFlorian Westphal 	if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) {
1633b1e66b9aSFlorian Westphal 		struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1634b1e66b9aSFlorian Westphal 
1635b1e66b9aSFlorian Westphal 		if (!net_eq(dev_net(dev), link_net)) {
1636d4e4fdf9SGuillaume Nault 			int id = peernet2id_alloc(src_net, link_net, gfp);
1637b1e66b9aSFlorian Westphal 
1638b1e66b9aSFlorian Westphal 			if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1639b1e66b9aSFlorian Westphal 				return -EMSGSIZE;
1640feadc4b6SSabrina Dubroca 
1641feadc4b6SSabrina Dubroca 			put_iflink = true;
1642b1e66b9aSFlorian Westphal 		}
1643b1e66b9aSFlorian Westphal 	}
1644b1e66b9aSFlorian Westphal 
1645feadc4b6SSabrina Dubroca 	return nla_put_iflink(skb, dev, put_iflink);
1646b1e66b9aSFlorian Westphal }
1647b1e66b9aSFlorian Westphal 
1648070cbf5bSFlorian Westphal static int rtnl_fill_link_af(struct sk_buff *skb,
1649070cbf5bSFlorian Westphal 			     const struct net_device *dev,
1650070cbf5bSFlorian Westphal 			     u32 ext_filter_mask)
1651070cbf5bSFlorian Westphal {
1652070cbf5bSFlorian Westphal 	const struct rtnl_af_ops *af_ops;
1653070cbf5bSFlorian Westphal 	struct nlattr *af_spec;
1654070cbf5bSFlorian Westphal 
1655ae0be8deSMichal Kubecek 	af_spec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
1656070cbf5bSFlorian Westphal 	if (!af_spec)
1657070cbf5bSFlorian Westphal 		return -EMSGSIZE;
1658070cbf5bSFlorian Westphal 
16595fa85a09SFlorian Westphal 	list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
1660070cbf5bSFlorian Westphal 		struct nlattr *af;
1661070cbf5bSFlorian Westphal 		int err;
1662070cbf5bSFlorian Westphal 
1663070cbf5bSFlorian Westphal 		if (!af_ops->fill_link_af)
1664070cbf5bSFlorian Westphal 			continue;
1665070cbf5bSFlorian Westphal 
1666ae0be8deSMichal Kubecek 		af = nla_nest_start_noflag(skb, af_ops->family);
1667070cbf5bSFlorian Westphal 		if (!af)
1668070cbf5bSFlorian Westphal 			return -EMSGSIZE;
1669070cbf5bSFlorian Westphal 
1670070cbf5bSFlorian Westphal 		err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
1671070cbf5bSFlorian Westphal 		/*
1672070cbf5bSFlorian Westphal 		 * Caller may return ENODATA to indicate that there
1673070cbf5bSFlorian Westphal 		 * was no data to be dumped. This is not an error, it
1674070cbf5bSFlorian Westphal 		 * means we should trim the attribute header and
1675070cbf5bSFlorian Westphal 		 * continue.
1676070cbf5bSFlorian Westphal 		 */
1677070cbf5bSFlorian Westphal 		if (err == -ENODATA)
1678070cbf5bSFlorian Westphal 			nla_nest_cancel(skb, af);
1679070cbf5bSFlorian Westphal 		else if (err < 0)
1680070cbf5bSFlorian Westphal 			return -EMSGSIZE;
1681070cbf5bSFlorian Westphal 
1682070cbf5bSFlorian Westphal 		nla_nest_end(skb, af);
1683070cbf5bSFlorian Westphal 	}
1684070cbf5bSFlorian Westphal 
1685070cbf5bSFlorian Westphal 	nla_nest_end(skb, af_spec);
1686070cbf5bSFlorian Westphal 	return 0;
1687070cbf5bSFlorian Westphal }
1688070cbf5bSFlorian Westphal 
168988f4fb0cSJiri Pirko static int rtnl_fill_alt_ifnames(struct sk_buff *skb,
169088f4fb0cSJiri Pirko 				 const struct net_device *dev)
169188f4fb0cSJiri Pirko {
169288f4fb0cSJiri Pirko 	struct netdev_name_node *name_node;
169388f4fb0cSJiri Pirko 	int count = 0;
169488f4fb0cSJiri Pirko 
169588f4fb0cSJiri Pirko 	list_for_each_entry(name_node, &dev->name_node->list, list) {
169688f4fb0cSJiri Pirko 		if (nla_put_string(skb, IFLA_ALT_IFNAME, name_node->name))
169788f4fb0cSJiri Pirko 			return -EMSGSIZE;
169888f4fb0cSJiri Pirko 		count++;
169988f4fb0cSJiri Pirko 	}
170088f4fb0cSJiri Pirko 	return count;
170188f4fb0cSJiri Pirko }
170288f4fb0cSJiri Pirko 
170388f4fb0cSJiri Pirko static int rtnl_fill_prop_list(struct sk_buff *skb,
170488f4fb0cSJiri Pirko 			       const struct net_device *dev)
170588f4fb0cSJiri Pirko {
170688f4fb0cSJiri Pirko 	struct nlattr *prop_list;
170788f4fb0cSJiri Pirko 	int ret;
170888f4fb0cSJiri Pirko 
170988f4fb0cSJiri Pirko 	prop_list = nla_nest_start(skb, IFLA_PROP_LIST);
171088f4fb0cSJiri Pirko 	if (!prop_list)
171188f4fb0cSJiri Pirko 		return -EMSGSIZE;
171288f4fb0cSJiri Pirko 
171388f4fb0cSJiri Pirko 	ret = rtnl_fill_alt_ifnames(skb, dev);
171488f4fb0cSJiri Pirko 	if (ret <= 0)
171588f4fb0cSJiri Pirko 		goto nest_cancel;
171688f4fb0cSJiri Pirko 
171788f4fb0cSJiri Pirko 	nla_nest_end(skb, prop_list);
171888f4fb0cSJiri Pirko 	return 0;
171988f4fb0cSJiri Pirko 
172088f4fb0cSJiri Pirko nest_cancel:
172188f4fb0cSJiri Pirko 	nla_nest_cancel(skb, prop_list);
172288f4fb0cSJiri Pirko 	return ret;
172388f4fb0cSJiri Pirko }
172488f4fb0cSJiri Pirko 
1725829eb208SRoopa Prabhu static int rtnl_fill_proto_down(struct sk_buff *skb,
1726829eb208SRoopa Prabhu 				const struct net_device *dev)
1727829eb208SRoopa Prabhu {
1728829eb208SRoopa Prabhu 	struct nlattr *pr;
1729829eb208SRoopa Prabhu 	u32 preason;
1730829eb208SRoopa Prabhu 
1731829eb208SRoopa Prabhu 	if (nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down))
1732829eb208SRoopa Prabhu 		goto nla_put_failure;
1733829eb208SRoopa Prabhu 
1734829eb208SRoopa Prabhu 	preason = dev->proto_down_reason;
1735829eb208SRoopa Prabhu 	if (!preason)
1736829eb208SRoopa Prabhu 		return 0;
1737829eb208SRoopa Prabhu 
1738829eb208SRoopa Prabhu 	pr = nla_nest_start(skb, IFLA_PROTO_DOWN_REASON);
1739829eb208SRoopa Prabhu 	if (!pr)
1740829eb208SRoopa Prabhu 		return -EMSGSIZE;
1741829eb208SRoopa Prabhu 
1742829eb208SRoopa Prabhu 	if (nla_put_u32(skb, IFLA_PROTO_DOWN_REASON_VALUE, preason)) {
1743829eb208SRoopa Prabhu 		nla_nest_cancel(skb, pr);
1744829eb208SRoopa Prabhu 		goto nla_put_failure;
1745829eb208SRoopa Prabhu 	}
1746829eb208SRoopa Prabhu 
1747829eb208SRoopa Prabhu 	nla_nest_end(skb, pr);
1748829eb208SRoopa Prabhu 	return 0;
1749829eb208SRoopa Prabhu 
1750829eb208SRoopa Prabhu nla_put_failure:
1751829eb208SRoopa Prabhu 	return -EMSGSIZE;
1752829eb208SRoopa Prabhu }
1753829eb208SRoopa Prabhu 
1754dca56c30SJiri Pirko static int rtnl_fill_devlink_port(struct sk_buff *skb,
1755dca56c30SJiri Pirko 				  const struct net_device *dev)
1756dca56c30SJiri Pirko {
1757dca56c30SJiri Pirko 	struct nlattr *devlink_port_nest;
1758dca56c30SJiri Pirko 	int ret;
1759dca56c30SJiri Pirko 
1760dca56c30SJiri Pirko 	devlink_port_nest = nla_nest_start(skb, IFLA_DEVLINK_PORT);
1761dca56c30SJiri Pirko 	if (!devlink_port_nest)
1762dca56c30SJiri Pirko 		return -EMSGSIZE;
1763dca56c30SJiri Pirko 
1764dca56c30SJiri Pirko 	if (dev->devlink_port) {
1765dca56c30SJiri Pirko 		ret = devlink_nl_port_handle_fill(skb, dev->devlink_port);
1766dca56c30SJiri Pirko 		if (ret < 0)
1767dca56c30SJiri Pirko 			goto nest_cancel;
1768dca56c30SJiri Pirko 	}
1769dca56c30SJiri Pirko 
1770dca56c30SJiri Pirko 	nla_nest_end(skb, devlink_port_nest);
1771dca56c30SJiri Pirko 	return 0;
1772dca56c30SJiri Pirko 
1773dca56c30SJiri Pirko nest_cancel:
1774dca56c30SJiri Pirko 	nla_nest_cancel(skb, devlink_port_nest);
1775dca56c30SJiri Pirko 	return ret;
1776dca56c30SJiri Pirko }
1777dca56c30SJiri Pirko 
177879e1ad14SJiri Benc static int rtnl_fill_ifinfo(struct sk_buff *skb,
177979e1ad14SJiri Benc 			    struct net_device *dev, struct net *src_net,
1780575c3e2aSPatrick McHardy 			    int type, u32 pid, u32 seq, u32 change,
17813d3ea5afSVlad Yasevich 			    unsigned int flags, u32 ext_filter_mask,
178238e01b30SNicolas Dichtel 			    u32 event, int *new_nsid, int new_ifindex,
1783d4e4fdf9SGuillaume Nault 			    int tgt_netnsid, gfp_t gfp)
1784b60c5115SThomas Graf {
1785b60c5115SThomas Graf 	struct ifinfomsg *ifm;
17861da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
17875891cd5eSEric Dumazet 	struct Qdisc *qdisc;
17881da177e4SLinus Torvalds 
17892907c35fSEric Dumazet 	ASSERT_RTNL();
1790b60c5115SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1791b60c5115SThomas Graf 	if (nlh == NULL)
179226932566SPatrick McHardy 		return -EMSGSIZE;
17931da177e4SLinus Torvalds 
1794b60c5115SThomas Graf 	ifm = nlmsg_data(nlh);
1795b60c5115SThomas Graf 	ifm->ifi_family = AF_UNSPEC;
1796b60c5115SThomas Graf 	ifm->__ifi_pad = 0;
1797b60c5115SThomas Graf 	ifm->ifi_type = dev->type;
1798b60c5115SThomas Graf 	ifm->ifi_index = dev->ifindex;
1799b60c5115SThomas Graf 	ifm->ifi_flags = dev_get_flags(dev);
1800b60c5115SThomas Graf 	ifm->ifi_change = change;
18011da177e4SLinus Torvalds 
18027e4a8d5aSChristian Brauner 	if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_TARGET_NETNSID, tgt_netnsid))
180379e1ad14SJiri Benc 		goto nla_put_failure;
180479e1ad14SJiri Benc 
18055891cd5eSEric Dumazet 	qdisc = rtnl_dereference(dev->qdisc);
1806a6574349SDavid S. Miller 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1807a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1808a6574349SDavid S. Miller 	    nla_put_u8(skb, IFLA_OPERSTATE,
1809a6574349SDavid S. Miller 		       netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1810a6574349SDavid S. Miller 	    nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1811a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
18123e7a50ceSStephen Hemminger 	    nla_put_u32(skb, IFLA_MIN_MTU, dev->min_mtu) ||
18133e7a50ceSStephen Hemminger 	    nla_put_u32(skb, IFLA_MAX_MTU, dev->max_mtu) ||
1814a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_GROUP, dev->group) ||
1815edbc0bb3SBen Greear 	    nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
18167e6e1b57SNicolas Dichtel 	    nla_put_u32(skb, IFLA_ALLMULTI, dev->allmulti) ||
181776ff5cc9SJiri Pirko 	    nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
1818c70ce028SEric Dumazet 	    nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1819c70ce028SEric Dumazet 	    nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
1820eac1b93cSCoco Li 	    nla_put_u32(skb, IFLA_GRO_MAX_SIZE, dev->gro_max_size) ||
18219eefedd5SXin Long 	    nla_put_u32(skb, IFLA_GSO_IPV4_MAX_SIZE, dev->gso_ipv4_max_size) ||
18229eefedd5SXin Long 	    nla_put_u32(skb, IFLA_GRO_IPV4_MAX_SIZE, dev->gro_ipv4_max_size) ||
182389527be8SEric Dumazet 	    nla_put_u32(skb, IFLA_TSO_MAX_SIZE, dev->tso_max_size) ||
182489527be8SEric Dumazet 	    nla_put_u32(skb, IFLA_TSO_MAX_SEGS, dev->tso_max_segs) ||
18251d69c2b3SMark A. Greer #ifdef CONFIG_RPS
182676ff5cc9SJiri Pirko 	    nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
18271d69c2b3SMark A. Greer #endif
182879110a04SFlorian Westphal 	    put_master_ifindex(skb, dev) ||
18299a57247fSJiri Pirko 	    nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
18305891cd5eSEric Dumazet 	    (qdisc &&
18315891cd5eSEric Dumazet 	     nla_put_string(skb, IFLA_QDISC, qdisc->ops->id)) ||
18326c557001SFlorian Westphal 	    nla_put_ifalias(skb, dev) ||
18332d3b479dSdavid decotigny 	    nla_put_u32(skb, IFLA_CARRIER_CHANGES,
1834b2d3bcfaSDavid Decotigny 			atomic_read(&dev->carrier_up_count) +
1835b2d3bcfaSDavid Decotigny 			atomic_read(&dev->carrier_down_count)) ||
1836b2d3bcfaSDavid Decotigny 	    nla_put_u32(skb, IFLA_CARRIER_UP_COUNT,
1837b2d3bcfaSDavid Decotigny 			atomic_read(&dev->carrier_up_count)) ||
1838b2d3bcfaSDavid Decotigny 	    nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT,
1839b2d3bcfaSDavid Decotigny 			atomic_read(&dev->carrier_down_count)))
1840a6574349SDavid S. Miller 		goto nla_put_failure;
18410b815a1aSStephen Hemminger 
1842829eb208SRoopa Prabhu 	if (rtnl_fill_proto_down(skb, dev))
1843829eb208SRoopa Prabhu 		goto nla_put_failure;
1844829eb208SRoopa Prabhu 
18453d3ea5afSVlad Yasevich 	if (event != IFLA_EVENT_NONE) {
18463d3ea5afSVlad Yasevich 		if (nla_put_u32(skb, IFLA_EVENT, event))
18473d3ea5afSVlad Yasevich 			goto nla_put_failure;
18483d3ea5afSVlad Yasevich 	}
18493d3ea5afSVlad Yasevich 
1850b22b941bSHannes Frederic Sowa 	if (rtnl_fill_link_ifmap(skb, dev))
1851a6574349SDavid S. Miller 		goto nla_put_failure;
18521da177e4SLinus Torvalds 
18531da177e4SLinus Torvalds 	if (dev->addr_len) {
1854a6574349SDavid S. Miller 		if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1855a6574349SDavid S. Miller 		    nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1856a6574349SDavid S. Miller 			goto nla_put_failure;
18571da177e4SLinus Torvalds 	}
18581da177e4SLinus Torvalds 
185966cae9edSJiri Pirko 	if (rtnl_phys_port_id_fill(skb, dev))
186066cae9edSJiri Pirko 		goto nla_put_failure;
186166cae9edSJiri Pirko 
1862db24a904SDavid Ahern 	if (rtnl_phys_port_name_fill(skb, dev))
1863db24a904SDavid Ahern 		goto nla_put_failure;
1864db24a904SDavid Ahern 
186582f28412SJiri Pirko 	if (rtnl_phys_switch_id_fill(skb, dev))
186682f28412SJiri Pirko 		goto nla_put_failure;
186782f28412SJiri Pirko 
1868b22b941bSHannes Frederic Sowa 	if (rtnl_fill_stats(skb, dev))
1869b60c5115SThomas Graf 		goto nla_put_failure;
1870b60c5115SThomas Graf 
1871250fc3dfSFlorian Westphal 	if (rtnl_fill_vf(skb, dev, ext_filter_mask))
1872a6574349SDavid S. Miller 		goto nla_put_failure;
187357b61080SScott Feldman 
1874c53864fdSDavid Gibson 	if (rtnl_port_fill(skb, dev, ext_filter_mask))
187557b61080SScott Feldman 		goto nla_put_failure;
187657b61080SScott Feldman 
1877d1fdd913SBrenden Blanco 	if (rtnl_xdp_fill(skb, dev))
1878d1fdd913SBrenden Blanco 		goto nla_put_failure;
1879d1fdd913SBrenden Blanco 
1880ba7d49b1SJiri Pirko 	if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
188138f7b870SPatrick McHardy 		if (rtnl_link_fill(skb, dev) < 0)
188238f7b870SPatrick McHardy 			goto nla_put_failure;
188338f7b870SPatrick McHardy 	}
188438f7b870SPatrick McHardy 
1885d4e4fdf9SGuillaume Nault 	if (rtnl_fill_link_netnsid(skb, dev, src_net, gfp))
1886d37512a2SNicolas Dichtel 		goto nla_put_failure;
1887d37512a2SNicolas Dichtel 
18886621dd29SNicolas Dichtel 	if (new_nsid &&
18896621dd29SNicolas Dichtel 	    nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0)
18906621dd29SNicolas Dichtel 		goto nla_put_failure;
189138e01b30SNicolas Dichtel 	if (new_ifindex &&
189238e01b30SNicolas Dichtel 	    nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0)
189338e01b30SNicolas Dichtel 		goto nla_put_failure;
189438e01b30SNicolas Dichtel 
1895f74877a5SMichal Kubecek 	if (memchr_inv(dev->perm_addr, '\0', dev->addr_len) &&
1896f74877a5SMichal Kubecek 	    nla_put(skb, IFLA_PERM_ADDRESS, dev->addr_len, dev->perm_addr))
1897f74877a5SMichal Kubecek 		goto nla_put_failure;
18986621dd29SNicolas Dichtel 
18995fa85a09SFlorian Westphal 	rcu_read_lock();
1900070cbf5bSFlorian Westphal 	if (rtnl_fill_link_af(skb, dev, ext_filter_mask))
19015fa85a09SFlorian Westphal 		goto nla_put_failure_rcu;
19025fa85a09SFlorian Westphal 	rcu_read_unlock();
1903f8ff182cSThomas Graf 
190488f4fb0cSJiri Pirko 	if (rtnl_fill_prop_list(skb, dev))
190588f4fb0cSJiri Pirko 		goto nla_put_failure;
190688f4fb0cSJiri Pirko 
190700e77ed8SJohannes Berg 	if (dev->dev.parent &&
190800e77ed8SJohannes Berg 	    nla_put_string(skb, IFLA_PARENT_DEV_NAME,
190900e77ed8SJohannes Berg 			   dev_name(dev->dev.parent)))
191000e77ed8SJohannes Berg 		goto nla_put_failure;
191100e77ed8SJohannes Berg 
191200e77ed8SJohannes Berg 	if (dev->dev.parent && dev->dev.parent->bus &&
191300e77ed8SJohannes Berg 	    nla_put_string(skb, IFLA_PARENT_DEV_BUS_NAME,
191400e77ed8SJohannes Berg 			   dev->dev.parent->bus->name))
191500e77ed8SJohannes Berg 		goto nla_put_failure;
191600e77ed8SJohannes Berg 
1917dca56c30SJiri Pirko 	if (rtnl_fill_devlink_port(skb, dev))
1918dca56c30SJiri Pirko 		goto nla_put_failure;
1919dca56c30SJiri Pirko 
1920053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
1921053c095aSJohannes Berg 	return 0;
1922b60c5115SThomas Graf 
19235fa85a09SFlorian Westphal nla_put_failure_rcu:
19245fa85a09SFlorian Westphal 	rcu_read_unlock();
1925b60c5115SThomas Graf nla_put_failure:
192626932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
192726932566SPatrick McHardy 	return -EMSGSIZE;
19281da177e4SLinus Torvalds }
19291da177e4SLinus Torvalds 
1930f7b12606SJiri Pirko static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1931f7b12606SJiri Pirko 	[IFLA_IFNAME]		= { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1932f7b12606SJiri Pirko 	[IFLA_ADDRESS]		= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1933f7b12606SJiri Pirko 	[IFLA_BROADCAST]	= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1934f7b12606SJiri Pirko 	[IFLA_MAP]		= { .len = sizeof(struct rtnl_link_ifmap) },
1935f7b12606SJiri Pirko 	[IFLA_MTU]		= { .type = NLA_U32 },
1936f7b12606SJiri Pirko 	[IFLA_LINK]		= { .type = NLA_U32 },
1937f7b12606SJiri Pirko 	[IFLA_MASTER]		= { .type = NLA_U32 },
1938f7b12606SJiri Pirko 	[IFLA_CARRIER]		= { .type = NLA_U8 },
1939f7b12606SJiri Pirko 	[IFLA_TXQLEN]		= { .type = NLA_U32 },
1940f7b12606SJiri Pirko 	[IFLA_WEIGHT]		= { .type = NLA_U32 },
1941f7b12606SJiri Pirko 	[IFLA_OPERSTATE]	= { .type = NLA_U8 },
1942f7b12606SJiri Pirko 	[IFLA_LINKMODE]		= { .type = NLA_U8 },
1943f7b12606SJiri Pirko 	[IFLA_LINKINFO]		= { .type = NLA_NESTED },
1944f7b12606SJiri Pirko 	[IFLA_NET_NS_PID]	= { .type = NLA_U32 },
1945f7b12606SJiri Pirko 	[IFLA_NET_NS_FD]	= { .type = NLA_U32 },
19462459b4c6SNicolas Dichtel 	/* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
19472459b4c6SNicolas Dichtel 	 * allow 0-length string (needed to remove an alias).
19482459b4c6SNicolas Dichtel 	 */
19492459b4c6SNicolas Dichtel 	[IFLA_IFALIAS]	        = { .type = NLA_BINARY, .len = IFALIASZ - 1 },
1950f7b12606SJiri Pirko 	[IFLA_VFINFO_LIST]	= {. type = NLA_NESTED },
1951f7b12606SJiri Pirko 	[IFLA_VF_PORTS]		= { .type = NLA_NESTED },
1952f7b12606SJiri Pirko 	[IFLA_PORT_SELF]	= { .type = NLA_NESTED },
1953f7b12606SJiri Pirko 	[IFLA_AF_SPEC]		= { .type = NLA_NESTED },
1954f7b12606SJiri Pirko 	[IFLA_EXT_MASK]		= { .type = NLA_U32 },
1955f7b12606SJiri Pirko 	[IFLA_PROMISCUITY]	= { .type = NLA_U32 },
1956f7b12606SJiri Pirko 	[IFLA_NUM_TX_QUEUES]	= { .type = NLA_U32 },
1957f7b12606SJiri Pirko 	[IFLA_NUM_RX_QUEUES]	= { .type = NLA_U32 },
195846e6b992SStephen Hemminger 	[IFLA_GSO_MAX_SEGS]	= { .type = NLA_U32 },
195946e6b992SStephen Hemminger 	[IFLA_GSO_MAX_SIZE]	= { .type = NLA_U32 },
196002637fceSJiri Pirko 	[IFLA_PHYS_PORT_ID]	= { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
19612d3b479dSdavid decotigny 	[IFLA_CARRIER_CHANGES]	= { .type = NLA_U32 },  /* ignored */
196282f28412SJiri Pirko 	[IFLA_PHYS_SWITCH_ID]	= { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1963317f4810SNicolas Dichtel 	[IFLA_LINK_NETNSID]	= { .type = NLA_S32 },
196488d6378bSAnuradha Karuppiah 	[IFLA_PROTO_DOWN]	= { .type = NLA_U8 },
1965d1fdd913SBrenden Blanco 	[IFLA_XDP]		= { .type = NLA_NESTED },
19663d3ea5afSVlad Yasevich 	[IFLA_EVENT]		= { .type = NLA_U32 },
1967db833d40SSerhey Popovych 	[IFLA_GROUP]		= { .type = NLA_U32 },
19687e4a8d5aSChristian Brauner 	[IFLA_TARGET_NETNSID]	= { .type = NLA_S32 },
1969b2d3bcfaSDavid Decotigny 	[IFLA_CARRIER_UP_COUNT]	= { .type = NLA_U32 },
1970b2d3bcfaSDavid Decotigny 	[IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 },
19713e7a50ceSStephen Hemminger 	[IFLA_MIN_MTU]		= { .type = NLA_U32 },
19723e7a50ceSStephen Hemminger 	[IFLA_MAX_MTU]		= { .type = NLA_U32 },
197336fbf1e5SJiri Pirko 	[IFLA_PROP_LIST]	= { .type = NLA_NESTED },
197436fbf1e5SJiri Pirko 	[IFLA_ALT_IFNAME]	= { .type = NLA_STRING,
197536fbf1e5SJiri Pirko 				    .len = ALTIFNAMSIZ - 1 },
1976f74877a5SMichal Kubecek 	[IFLA_PERM_ADDRESS]	= { .type = NLA_REJECT },
1977829eb208SRoopa Prabhu 	[IFLA_PROTO_DOWN_REASON] = { .type = NLA_NESTED },
19787e4a5131SAndrei Vagin 	[IFLA_NEW_IFINDEX]	= NLA_POLICY_MIN(NLA_S32, 1),
197988b71053SJohannes Berg 	[IFLA_PARENT_DEV_NAME]	= { .type = NLA_NUL_STRING },
1980eac1b93cSCoco Li 	[IFLA_GRO_MAX_SIZE]	= { .type = NLA_U32 },
198189527be8SEric Dumazet 	[IFLA_TSO_MAX_SIZE]	= { .type = NLA_REJECT },
198289527be8SEric Dumazet 	[IFLA_TSO_MAX_SEGS]	= { .type = NLA_REJECT },
19837e6e1b57SNicolas Dichtel 	[IFLA_ALLMULTI]		= { .type = NLA_REJECT },
19849eefedd5SXin Long 	[IFLA_GSO_IPV4_MAX_SIZE]	= { .type = NLA_U32 },
19859eefedd5SXin Long 	[IFLA_GRO_IPV4_MAX_SIZE]	= { .type = NLA_U32 },
1986f7b12606SJiri Pirko };
1987f7b12606SJiri Pirko 
1988f7b12606SJiri Pirko static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1989f7b12606SJiri Pirko 	[IFLA_INFO_KIND]	= { .type = NLA_STRING },
1990f7b12606SJiri Pirko 	[IFLA_INFO_DATA]	= { .type = NLA_NESTED },
1991f7b12606SJiri Pirko 	[IFLA_INFO_SLAVE_KIND]	= { .type = NLA_STRING },
1992f7b12606SJiri Pirko 	[IFLA_INFO_SLAVE_DATA]	= { .type = NLA_NESTED },
1993f7b12606SJiri Pirko };
1994f7b12606SJiri Pirko 
1995f7b12606SJiri Pirko static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1996364d5716SDaniel Borkmann 	[IFLA_VF_MAC]		= { .len = sizeof(struct ifla_vf_mac) },
199775345f88SDenis Kirjanov 	[IFLA_VF_BROADCAST]	= { .type = NLA_REJECT },
1998364d5716SDaniel Borkmann 	[IFLA_VF_VLAN]		= { .len = sizeof(struct ifla_vf_vlan) },
199979aab093SMoshe Shemesh 	[IFLA_VF_VLAN_LIST]     = { .type = NLA_NESTED },
2000364d5716SDaniel Borkmann 	[IFLA_VF_TX_RATE]	= { .len = sizeof(struct ifla_vf_tx_rate) },
2001364d5716SDaniel Borkmann 	[IFLA_VF_SPOOFCHK]	= { .len = sizeof(struct ifla_vf_spoofchk) },
2002364d5716SDaniel Borkmann 	[IFLA_VF_RATE]		= { .len = sizeof(struct ifla_vf_rate) },
2003364d5716SDaniel Borkmann 	[IFLA_VF_LINK_STATE]	= { .len = sizeof(struct ifla_vf_link_state) },
200401a3d796SVlad Zolotarov 	[IFLA_VF_RSS_QUERY_EN]	= { .len = sizeof(struct ifla_vf_rss_query_en) },
20053b766cd8SEran Ben Elisha 	[IFLA_VF_STATS]		= { .type = NLA_NESTED },
2006dd461d6aSHiroshi Shimamoto 	[IFLA_VF_TRUST]		= { .len = sizeof(struct ifla_vf_trust) },
2007cc8e27ccSEli Cohen 	[IFLA_VF_IB_NODE_GUID]	= { .len = sizeof(struct ifla_vf_guid) },
2008cc8e27ccSEli Cohen 	[IFLA_VF_IB_PORT_GUID]	= { .len = sizeof(struct ifla_vf_guid) },
20093b766cd8SEran Ben Elisha };
20103b766cd8SEran Ben Elisha 
2011f7b12606SJiri Pirko static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
2012f7b12606SJiri Pirko 	[IFLA_PORT_VF]		= { .type = NLA_U32 },
2013f7b12606SJiri Pirko 	[IFLA_PORT_PROFILE]	= { .type = NLA_STRING,
2014f7b12606SJiri Pirko 				    .len = PORT_PROFILE_MAX },
2015f7b12606SJiri Pirko 	[IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
2016f7b12606SJiri Pirko 				      .len = PORT_UUID_MAX },
2017f7b12606SJiri Pirko 	[IFLA_PORT_HOST_UUID]	= { .type = NLA_STRING,
2018f7b12606SJiri Pirko 				    .len = PORT_UUID_MAX },
2019f7b12606SJiri Pirko 	[IFLA_PORT_REQUEST]	= { .type = NLA_U8, },
2020f7b12606SJiri Pirko 	[IFLA_PORT_RESPONSE]	= { .type = NLA_U16, },
2021025331dfSDaniel Borkmann 
2022025331dfSDaniel Borkmann 	/* Unused, but we need to keep it here since user space could
2023025331dfSDaniel Borkmann 	 * fill it. It's also broken with regard to NLA_BINARY use in
2024025331dfSDaniel Borkmann 	 * combination with structs.
2025025331dfSDaniel Borkmann 	 */
2026025331dfSDaniel Borkmann 	[IFLA_PORT_VSI_TYPE]	= { .type = NLA_BINARY,
2027025331dfSDaniel Borkmann 				    .len = sizeof(struct ifla_port_vsi) },
2028f7b12606SJiri Pirko };
2029f7b12606SJiri Pirko 
2030d1fdd913SBrenden Blanco static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
203192234c8fSToke Høiland-Jørgensen 	[IFLA_XDP_UNSPEC]	= { .strict_start_type = IFLA_XDP_EXPECTED_FD },
2032d1fdd913SBrenden Blanco 	[IFLA_XDP_FD]		= { .type = NLA_S32 },
203392234c8fSToke Høiland-Jørgensen 	[IFLA_XDP_EXPECTED_FD]	= { .type = NLA_S32 },
2034d1fdd913SBrenden Blanco 	[IFLA_XDP_ATTACHED]	= { .type = NLA_U8 },
203585de8576SDaniel Borkmann 	[IFLA_XDP_FLAGS]	= { .type = NLA_U32 },
203658038695SMartin KaFai Lau 	[IFLA_XDP_PROG_ID]	= { .type = NLA_U32 },
2037d1fdd913SBrenden Blanco };
2038d1fdd913SBrenden Blanco 
2039dc599f76SDavid Ahern static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
2040dc599f76SDavid Ahern {
2041dc599f76SDavid Ahern 	const struct rtnl_link_ops *ops = NULL;
2042dc599f76SDavid Ahern 	struct nlattr *linfo[IFLA_INFO_MAX + 1];
2043dc599f76SDavid Ahern 
20448cb08174SJohannes Berg 	if (nla_parse_nested_deprecated(linfo, IFLA_INFO_MAX, nla, ifla_info_policy, NULL) < 0)
2045dc599f76SDavid Ahern 		return NULL;
2046dc599f76SDavid Ahern 
2047dc599f76SDavid Ahern 	if (linfo[IFLA_INFO_KIND]) {
2048dc599f76SDavid Ahern 		char kind[MODULE_NAME_LEN];
2049dc599f76SDavid Ahern 
2050872f6903SFrancis Laniel 		nla_strscpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
2051dc599f76SDavid Ahern 		ops = rtnl_link_ops_get(kind);
2052dc599f76SDavid Ahern 	}
2053dc599f76SDavid Ahern 
2054dc599f76SDavid Ahern 	return ops;
2055dc599f76SDavid Ahern }
2056dc599f76SDavid Ahern 
2057dc599f76SDavid Ahern static bool link_master_filtered(struct net_device *dev, int master_idx)
2058dc599f76SDavid Ahern {
2059dc599f76SDavid Ahern 	struct net_device *master;
2060dc599f76SDavid Ahern 
2061dc599f76SDavid Ahern 	if (!master_idx)
2062dc599f76SDavid Ahern 		return false;
2063dc599f76SDavid Ahern 
2064dc599f76SDavid Ahern 	master = netdev_master_upper_dev_get(dev);
2065d3432bf1SLahav Schlesinger 
2066d3432bf1SLahav Schlesinger 	/* 0 is already used to denote IFLA_MASTER wasn't passed, therefore need
2067d3432bf1SLahav Schlesinger 	 * another invalid value for ifindex to denote "no master".
2068d3432bf1SLahav Schlesinger 	 */
2069d3432bf1SLahav Schlesinger 	if (master_idx == -1)
2070d3432bf1SLahav Schlesinger 		return !!master;
2071d3432bf1SLahav Schlesinger 
2072dc599f76SDavid Ahern 	if (!master || master->ifindex != master_idx)
2073dc599f76SDavid Ahern 		return true;
2074dc599f76SDavid Ahern 
2075dc599f76SDavid Ahern 	return false;
2076dc599f76SDavid Ahern }
2077dc599f76SDavid Ahern 
2078dc599f76SDavid Ahern static bool link_kind_filtered(const struct net_device *dev,
2079dc599f76SDavid Ahern 			       const struct rtnl_link_ops *kind_ops)
2080dc599f76SDavid Ahern {
2081dc599f76SDavid Ahern 	if (kind_ops && dev->rtnl_link_ops != kind_ops)
2082dc599f76SDavid Ahern 		return true;
2083dc599f76SDavid Ahern 
2084dc599f76SDavid Ahern 	return false;
2085dc599f76SDavid Ahern }
2086dc599f76SDavid Ahern 
2087dc599f76SDavid Ahern static bool link_dump_filtered(struct net_device *dev,
2088dc599f76SDavid Ahern 			       int master_idx,
2089dc599f76SDavid Ahern 			       const struct rtnl_link_ops *kind_ops)
2090dc599f76SDavid Ahern {
2091dc599f76SDavid Ahern 	if (link_master_filtered(dev, master_idx) ||
2092dc599f76SDavid Ahern 	    link_kind_filtered(dev, kind_ops))
2093dc599f76SDavid Ahern 		return true;
2094dc599f76SDavid Ahern 
2095dc599f76SDavid Ahern 	return false;
2096dc599f76SDavid Ahern }
2097dc599f76SDavid Ahern 
2098c383edc4SChristian Brauner /**
2099c383edc4SChristian Brauner  * rtnl_get_net_ns_capable - Get netns if sufficiently privileged.
2100c383edc4SChristian Brauner  * @sk: netlink socket
2101c383edc4SChristian Brauner  * @netnsid: network namespace identifier
2102c383edc4SChristian Brauner  *
2103c383edc4SChristian Brauner  * Returns the network namespace identified by netnsid on success or an error
2104c383edc4SChristian Brauner  * pointer on failure.
2105c383edc4SChristian Brauner  */
2106c383edc4SChristian Brauner struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid)
210779e1ad14SJiri Benc {
210879e1ad14SJiri Benc 	struct net *net;
210979e1ad14SJiri Benc 
2110f428fe4aSAndrei Vagin 	net = get_net_ns_by_id(sock_net(sk), netnsid);
211179e1ad14SJiri Benc 	if (!net)
211279e1ad14SJiri Benc 		return ERR_PTR(-EINVAL);
211379e1ad14SJiri Benc 
211479e1ad14SJiri Benc 	/* For now, the caller is required to have CAP_NET_ADMIN in
211579e1ad14SJiri Benc 	 * the user namespace owning the target net ns.
211679e1ad14SJiri Benc 	 */
2117f428fe4aSAndrei Vagin 	if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) {
211879e1ad14SJiri Benc 		put_net(net);
211979e1ad14SJiri Benc 		return ERR_PTR(-EACCES);
212079e1ad14SJiri Benc 	}
212179e1ad14SJiri Benc 	return net;
212279e1ad14SJiri Benc }
2123c383edc4SChristian Brauner EXPORT_SYMBOL_GPL(rtnl_get_net_ns_capable);
212479e1ad14SJiri Benc 
2125905cf0abSDavid Ahern static int rtnl_valid_dump_ifinfo_req(const struct nlmsghdr *nlh,
2126905cf0abSDavid Ahern 				      bool strict_check, struct nlattr **tb,
2127905cf0abSDavid Ahern 				      struct netlink_ext_ack *extack)
2128905cf0abSDavid Ahern {
2129905cf0abSDavid Ahern 	int hdrlen;
2130905cf0abSDavid Ahern 
2131905cf0abSDavid Ahern 	if (strict_check) {
2132905cf0abSDavid Ahern 		struct ifinfomsg *ifm;
2133905cf0abSDavid Ahern 
2134905cf0abSDavid Ahern 		if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
2135905cf0abSDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid header for link dump");
2136905cf0abSDavid Ahern 			return -EINVAL;
2137905cf0abSDavid Ahern 		}
2138905cf0abSDavid Ahern 
2139905cf0abSDavid Ahern 		ifm = nlmsg_data(nlh);
2140905cf0abSDavid Ahern 		if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
2141905cf0abSDavid Ahern 		    ifm->ifi_change) {
2142905cf0abSDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid values in header for link dump request");
2143905cf0abSDavid Ahern 			return -EINVAL;
2144905cf0abSDavid Ahern 		}
2145905cf0abSDavid Ahern 		if (ifm->ifi_index) {
2146905cf0abSDavid Ahern 			NL_SET_ERR_MSG(extack, "Filter by device index not supported for link dumps");
2147905cf0abSDavid Ahern 			return -EINVAL;
2148905cf0abSDavid Ahern 		}
2149905cf0abSDavid Ahern 
21508cb08174SJohannes Berg 		return nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb,
21518cb08174SJohannes Berg 						     IFLA_MAX, ifla_policy,
21528cb08174SJohannes Berg 						     extack);
2153905cf0abSDavid Ahern 	}
2154905cf0abSDavid Ahern 
2155905cf0abSDavid Ahern 	/* A hack to preserve kernel<->userspace interface.
2156905cf0abSDavid Ahern 	 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
2157905cf0abSDavid Ahern 	 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
2158905cf0abSDavid Ahern 	 * what iproute2 < v3.9.0 used.
2159905cf0abSDavid Ahern 	 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
2160905cf0abSDavid Ahern 	 * attribute, its netlink message is shorter than struct ifinfomsg.
2161905cf0abSDavid Ahern 	 */
2162905cf0abSDavid Ahern 	hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2163905cf0abSDavid Ahern 		 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2164905cf0abSDavid Ahern 
21658cb08174SJohannes Berg 	return nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy,
21668cb08174SJohannes Berg 				      extack);
2167905cf0abSDavid Ahern }
2168905cf0abSDavid Ahern 
2169b60c5115SThomas Graf static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
21701da177e4SLinus Torvalds {
2171905cf0abSDavid Ahern 	struct netlink_ext_ack *extack = cb->extack;
2172905cf0abSDavid Ahern 	const struct nlmsghdr *nlh = cb->nlh;
21733b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
217479e1ad14SJiri Benc 	struct net *tgt_net = net;
21757c28bd0bSEric Dumazet 	int h, s_h;
21767c28bd0bSEric Dumazet 	int idx = 0, s_idx;
21771da177e4SLinus Torvalds 	struct net_device *dev;
21787c28bd0bSEric Dumazet 	struct hlist_head *head;
2179115c9b81SGreg Rose 	struct nlattr *tb[IFLA_MAX+1];
2180115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
2181dc599f76SDavid Ahern 	const struct rtnl_link_ops *kind_ops = NULL;
2182dc599f76SDavid Ahern 	unsigned int flags = NLM_F_MULTI;
2183dc599f76SDavid Ahern 	int master_idx = 0;
218479e1ad14SJiri Benc 	int netnsid = -1;
2185905cf0abSDavid Ahern 	int err, i;
21861da177e4SLinus Torvalds 
21877c28bd0bSEric Dumazet 	s_h = cb->args[0];
21887c28bd0bSEric Dumazet 	s_idx = cb->args[1];
21897c28bd0bSEric Dumazet 
2190905cf0abSDavid Ahern 	err = rtnl_valid_dump_ifinfo_req(nlh, cb->strict_check, tb, extack);
2191905cf0abSDavid Ahern 	if (err < 0) {
2192905cf0abSDavid Ahern 		if (cb->strict_check)
2193905cf0abSDavid Ahern 			return err;
2194e5eca6d4SMichal Schmidt 
2195905cf0abSDavid Ahern 		goto walk_entries;
219679e1ad14SJiri Benc 	}
219779e1ad14SJiri Benc 
2198905cf0abSDavid Ahern 	for (i = 0; i <= IFLA_MAX; ++i) {
2199905cf0abSDavid Ahern 		if (!tb[i])
2200905cf0abSDavid Ahern 			continue;
2201dc599f76SDavid Ahern 
2202905cf0abSDavid Ahern 		/* new attributes should only be added with strict checking */
2203905cf0abSDavid Ahern 		switch (i) {
2204905cf0abSDavid Ahern 		case IFLA_TARGET_NETNSID:
2205905cf0abSDavid Ahern 			netnsid = nla_get_s32(tb[i]);
2206905cf0abSDavid Ahern 			tgt_net = rtnl_get_net_ns_capable(skb->sk, netnsid);
2207905cf0abSDavid Ahern 			if (IS_ERR(tgt_net)) {
2208905cf0abSDavid Ahern 				NL_SET_ERR_MSG(extack, "Invalid target network namespace id");
2209905cf0abSDavid Ahern 				return PTR_ERR(tgt_net);
2210905cf0abSDavid Ahern 			}
2211905cf0abSDavid Ahern 			break;
2212905cf0abSDavid Ahern 		case IFLA_EXT_MASK:
2213905cf0abSDavid Ahern 			ext_filter_mask = nla_get_u32(tb[i]);
2214905cf0abSDavid Ahern 			break;
2215905cf0abSDavid Ahern 		case IFLA_MASTER:
2216905cf0abSDavid Ahern 			master_idx = nla_get_u32(tb[i]);
2217905cf0abSDavid Ahern 			break;
2218905cf0abSDavid Ahern 		case IFLA_LINKINFO:
2219905cf0abSDavid Ahern 			kind_ops = linkinfo_to_kind_ops(tb[i]);
2220905cf0abSDavid Ahern 			break;
2221905cf0abSDavid Ahern 		default:
2222905cf0abSDavid Ahern 			if (cb->strict_check) {
2223905cf0abSDavid Ahern 				NL_SET_ERR_MSG(extack, "Unsupported attribute in link dump request");
2224905cf0abSDavid Ahern 				return -EINVAL;
2225905cf0abSDavid Ahern 			}
2226905cf0abSDavid Ahern 		}
2227905cf0abSDavid Ahern 	}
2228dc599f76SDavid Ahern 
2229dc599f76SDavid Ahern 	if (master_idx || kind_ops)
2230dc599f76SDavid Ahern 		flags |= NLM_F_DUMP_FILTERED;
2231115c9b81SGreg Rose 
2232905cf0abSDavid Ahern walk_entries:
22337c28bd0bSEric Dumazet 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
22347562f876SPavel Emelianov 		idx = 0;
223579e1ad14SJiri Benc 		head = &tgt_net->dev_index_head[h];
2236cac5e65eSEric Dumazet 		hlist_for_each_entry(dev, head, index_hlist) {
2237dc599f76SDavid Ahern 			if (link_dump_filtered(dev, master_idx, kind_ops))
22383f0ae05dSZhang Shengju 				goto cont;
22391da177e4SLinus Torvalds 			if (idx < s_idx)
22407562f876SPavel Emelianov 				goto cont;
224179e1ad14SJiri Benc 			err = rtnl_fill_ifinfo(skb, dev, net,
224279e1ad14SJiri Benc 					       RTM_NEWLINK,
224315e47304SEric W. Biederman 					       NETLINK_CB(cb->skb).portid,
2244905cf0abSDavid Ahern 					       nlh->nlmsg_seq, 0, flags,
224538e01b30SNicolas Dichtel 					       ext_filter_mask, 0, NULL, 0,
2246d4e4fdf9SGuillaume Nault 					       netnsid, GFP_KERNEL);
2247973462bbSDavid Gibson 
2248f6c5775fSDavid Ahern 			if (err < 0) {
2249f6c5775fSDavid Ahern 				if (likely(skb->len))
22507c28bd0bSEric Dumazet 					goto out;
22514e985adaSThomas Graf 
2252f6c5775fSDavid Ahern 				goto out_err;
2253f6c5775fSDavid Ahern 			}
22547562f876SPavel Emelianov cont:
22557562f876SPavel Emelianov 			idx++;
22561da177e4SLinus Torvalds 		}
22577c28bd0bSEric Dumazet 	}
22587c28bd0bSEric Dumazet out:
2259f6c5775fSDavid Ahern 	err = skb->len;
2260f6c5775fSDavid Ahern out_err:
22617c28bd0bSEric Dumazet 	cb->args[1] = idx;
22627c28bd0bSEric Dumazet 	cb->args[0] = h;
2263a9ecb0cbSzhang kai 	cb->seq = tgt_net->dev_base_seq;
2264d0225784SJakub Sitnicki 	nl_dump_check_consistent(cb, nlmsg_hdr(skb));
226579e1ad14SJiri Benc 	if (netnsid >= 0)
226679e1ad14SJiri Benc 		put_net(tgt_net);
22671da177e4SLinus Torvalds 
2268f6c5775fSDavid Ahern 	return err;
22691da177e4SLinus Torvalds }
22701da177e4SLinus Torvalds 
2271*f534f658SJakub Kicinski int rtnl_nla_parse_ifinfomsg(struct nlattr **tb, const struct nlattr *nla_peer,
2272fceb6435SJohannes Berg 			     struct netlink_ext_ack *exterr)
2273f7b12606SJiri Pirko {
2274*f534f658SJakub Kicinski 	const struct ifinfomsg *ifmp;
2275*f534f658SJakub Kicinski 	const struct nlattr *attrs;
2276*f534f658SJakub Kicinski 	size_t len;
2277*f534f658SJakub Kicinski 
2278*f534f658SJakub Kicinski 	ifmp = nla_data(nla_peer);
2279*f534f658SJakub Kicinski 	attrs = nla_data(nla_peer) + sizeof(struct ifinfomsg);
2280*f534f658SJakub Kicinski 	len = nla_len(nla_peer) - sizeof(struct ifinfomsg);
2281*f534f658SJakub Kicinski 
2282*f534f658SJakub Kicinski 	if (ifmp->ifi_index < 0) {
2283*f534f658SJakub Kicinski 		NL_SET_ERR_MSG_ATTR(exterr, nla_peer,
2284*f534f658SJakub Kicinski 				    "ifindex can't be negative");
2285*f534f658SJakub Kicinski 		return -EINVAL;
2286*f534f658SJakub Kicinski 	}
2287*f534f658SJakub Kicinski 
2288*f534f658SJakub Kicinski 	return nla_parse_deprecated(tb, IFLA_MAX, attrs, len, ifla_policy,
22898cb08174SJohannes Berg 				    exterr);
2290f7b12606SJiri Pirko }
2291*f534f658SJakub Kicinski EXPORT_SYMBOL(rtnl_nla_parse_ifinfomsg);
229257b61080SScott Feldman 
229381adee47SEric W. Biederman struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
229481adee47SEric W. Biederman {
229581adee47SEric W. Biederman 	struct net *net;
229681adee47SEric W. Biederman 	/* Examine the link attributes and figure out which
229781adee47SEric W. Biederman 	 * network namespace we are talking about.
229881adee47SEric W. Biederman 	 */
229981adee47SEric W. Biederman 	if (tb[IFLA_NET_NS_PID])
230081adee47SEric W. Biederman 		net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
2301f0630529SEric W. Biederman 	else if (tb[IFLA_NET_NS_FD])
2302f0630529SEric W. Biederman 		net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
230381adee47SEric W. Biederman 	else
230481adee47SEric W. Biederman 		net = get_net(src_net);
230581adee47SEric W. Biederman 	return net;
230681adee47SEric W. Biederman }
230781adee47SEric W. Biederman EXPORT_SYMBOL(rtnl_link_get_net);
230881adee47SEric W. Biederman 
23097c4f63baSChristian Brauner /* Figure out which network namespace we are talking about by
23107c4f63baSChristian Brauner  * examining the link attributes in the following order:
23117c4f63baSChristian Brauner  *
23127c4f63baSChristian Brauner  * 1. IFLA_NET_NS_PID
23137c4f63baSChristian Brauner  * 2. IFLA_NET_NS_FD
23147e4a8d5aSChristian Brauner  * 3. IFLA_TARGET_NETNSID
23157c4f63baSChristian Brauner  */
23167c4f63baSChristian Brauner static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net,
23177c4f63baSChristian Brauner 					       struct nlattr *tb[])
23187c4f63baSChristian Brauner {
23197c4f63baSChristian Brauner 	struct net *net;
23207c4f63baSChristian Brauner 
23217c4f63baSChristian Brauner 	if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])
23227c4f63baSChristian Brauner 		return rtnl_link_get_net(src_net, tb);
23237c4f63baSChristian Brauner 
23247e4a8d5aSChristian Brauner 	if (!tb[IFLA_TARGET_NETNSID])
23257c4f63baSChristian Brauner 		return get_net(src_net);
23267c4f63baSChristian Brauner 
23277e4a8d5aSChristian Brauner 	net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_TARGET_NETNSID]));
23287c4f63baSChristian Brauner 	if (!net)
23297c4f63baSChristian Brauner 		return ERR_PTR(-EINVAL);
23307c4f63baSChristian Brauner 
23317c4f63baSChristian Brauner 	return net;
23327c4f63baSChristian Brauner }
23337c4f63baSChristian Brauner 
23347c4f63baSChristian Brauner static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb,
23357c4f63baSChristian Brauner 					     struct net *src_net,
23367c4f63baSChristian Brauner 					     struct nlattr *tb[], int cap)
23377c4f63baSChristian Brauner {
23387c4f63baSChristian Brauner 	struct net *net;
23397c4f63baSChristian Brauner 
23407c4f63baSChristian Brauner 	net = rtnl_link_get_net_by_nlattr(src_net, tb);
23417c4f63baSChristian Brauner 	if (IS_ERR(net))
23427c4f63baSChristian Brauner 		return net;
23437c4f63baSChristian Brauner 
23447c4f63baSChristian Brauner 	if (!netlink_ns_capable(skb, net->user_ns, cap)) {
23457c4f63baSChristian Brauner 		put_net(net);
23467c4f63baSChristian Brauner 		return ERR_PTR(-EPERM);
23477c4f63baSChristian Brauner 	}
23487c4f63baSChristian Brauner 
23497c4f63baSChristian Brauner 	return net;
23507c4f63baSChristian Brauner }
23517c4f63baSChristian Brauner 
23524ff66caeSChristian Brauner /* Verify that rtnetlink requests do not pass additional properties
23534ff66caeSChristian Brauner  * potentially referring to different network namespaces.
23544ff66caeSChristian Brauner  */
23554ff66caeSChristian Brauner static int rtnl_ensure_unique_netns(struct nlattr *tb[],
23564ff66caeSChristian Brauner 				    struct netlink_ext_ack *extack,
23574ff66caeSChristian Brauner 				    bool netns_id_only)
23584ff66caeSChristian Brauner {
23594ff66caeSChristian Brauner 
23604ff66caeSChristian Brauner 	if (netns_id_only) {
23614ff66caeSChristian Brauner 		if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD])
23624ff66caeSChristian Brauner 			return 0;
23634ff66caeSChristian Brauner 
23644ff66caeSChristian Brauner 		NL_SET_ERR_MSG(extack, "specified netns attribute not supported");
23654ff66caeSChristian Brauner 		return -EOPNOTSUPP;
23664ff66caeSChristian Brauner 	}
23674ff66caeSChristian Brauner 
23687e4a8d5aSChristian Brauner 	if (tb[IFLA_TARGET_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]))
23694ff66caeSChristian Brauner 		goto invalid_attr;
23704ff66caeSChristian Brauner 
23717e4a8d5aSChristian Brauner 	if (tb[IFLA_NET_NS_PID] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_FD]))
23724ff66caeSChristian Brauner 		goto invalid_attr;
23734ff66caeSChristian Brauner 
23747e4a8d5aSChristian Brauner 	if (tb[IFLA_NET_NS_FD] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_PID]))
23754ff66caeSChristian Brauner 		goto invalid_attr;
23764ff66caeSChristian Brauner 
23774ff66caeSChristian Brauner 	return 0;
23784ff66caeSChristian Brauner 
23794ff66caeSChristian Brauner invalid_attr:
23804ff66caeSChristian Brauner 	NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified");
23814ff66caeSChristian Brauner 	return -EINVAL;
23824ff66caeSChristian Brauner }
23834ff66caeSChristian Brauner 
2384a14857c2SBin Chen static	int rtnl_set_vf_rate(struct net_device *dev, int vf, int min_tx_rate,
2385a14857c2SBin Chen 			     int max_tx_rate)
2386a14857c2SBin Chen {
2387a14857c2SBin Chen 	const struct net_device_ops *ops = dev->netdev_ops;
2388a14857c2SBin Chen 
2389a14857c2SBin Chen 	if (!ops->ndo_set_vf_rate)
2390a14857c2SBin Chen 		return -EOPNOTSUPP;
2391a14857c2SBin Chen 	if (max_tx_rate && max_tx_rate < min_tx_rate)
2392a14857c2SBin Chen 		return -EINVAL;
2393a14857c2SBin Chen 
2394a14857c2SBin Chen 	return ops->ndo_set_vf_rate(dev, vf, min_tx_rate, max_tx_rate);
2395a14857c2SBin Chen }
2396a14857c2SBin Chen 
23978679c31eSRocco Yue static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[],
23988679c31eSRocco Yue 			    struct netlink_ext_ack *extack)
23991840bb13SThomas Graf {
24001840bb13SThomas Graf 	if (tb[IFLA_ADDRESS] &&
24011840bb13SThomas Graf 	    nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
24021840bb13SThomas Graf 		return -EINVAL;
24031840bb13SThomas Graf 
24041840bb13SThomas Graf 	if (tb[IFLA_BROADCAST] &&
24051840bb13SThomas Graf 	    nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
24061840bb13SThomas Graf 		return -EINVAL;
2407fef5b228SXin Long 
2408fef5b228SXin Long 	if (tb[IFLA_GSO_MAX_SIZE] &&
2409fef5b228SXin Long 	    nla_get_u32(tb[IFLA_GSO_MAX_SIZE]) > dev->tso_max_size) {
2410fef5b228SXin Long 		NL_SET_ERR_MSG(extack, "too big gso_max_size");
2411fef5b228SXin Long 		return -EINVAL;
2412fef5b228SXin Long 	}
2413fef5b228SXin Long 
2414fef5b228SXin Long 	if (tb[IFLA_GSO_MAX_SEGS] &&
2415fef5b228SXin Long 	    (nla_get_u32(tb[IFLA_GSO_MAX_SEGS]) > GSO_MAX_SEGS ||
2416fef5b228SXin Long 	     nla_get_u32(tb[IFLA_GSO_MAX_SEGS]) > dev->tso_max_segs)) {
2417fef5b228SXin Long 		NL_SET_ERR_MSG(extack, "too big gso_max_segs");
2418fef5b228SXin Long 		return -EINVAL;
2419fef5b228SXin Long 	}
2420fef5b228SXin Long 
242165d6914eSXin Long 	if (tb[IFLA_GRO_MAX_SIZE] &&
242265d6914eSXin Long 	    nla_get_u32(tb[IFLA_GRO_MAX_SIZE]) > GRO_MAX_SIZE) {
242365d6914eSXin Long 		NL_SET_ERR_MSG(extack, "too big gro_max_size");
242465d6914eSXin Long 		return -EINVAL;
242565d6914eSXin Long 	}
242665d6914eSXin Long 
2427fef5b228SXin Long 	if (tb[IFLA_GSO_IPV4_MAX_SIZE] &&
2428fef5b228SXin Long 	    nla_get_u32(tb[IFLA_GSO_IPV4_MAX_SIZE]) > dev->tso_max_size) {
2429fef5b228SXin Long 		NL_SET_ERR_MSG(extack, "too big gso_ipv4_max_size");
2430fef5b228SXin Long 		return -EINVAL;
2431fef5b228SXin Long 	}
243265d6914eSXin Long 
243365d6914eSXin Long 	if (tb[IFLA_GRO_IPV4_MAX_SIZE] &&
243465d6914eSXin Long 	    nla_get_u32(tb[IFLA_GRO_IPV4_MAX_SIZE]) > GRO_MAX_SIZE) {
243565d6914eSXin Long 		NL_SET_ERR_MSG(extack, "too big gro_ipv4_max_size");
243665d6914eSXin Long 		return -EINVAL;
243765d6914eSXin Long 	}
24381840bb13SThomas Graf 
2439cf7afbfeSThomas Graf 	if (tb[IFLA_AF_SPEC]) {
2440cf7afbfeSThomas Graf 		struct nlattr *af;
2441cf7afbfeSThomas Graf 		int rem, err;
2442cf7afbfeSThomas Graf 
2443cf7afbfeSThomas Graf 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2444cf7afbfeSThomas Graf 			const struct rtnl_af_ops *af_ops;
2445cf7afbfeSThomas Graf 
24465fa85a09SFlorian Westphal 			af_ops = rtnl_af_lookup(nla_type(af));
2447a100243dSCong Wang 			if (!af_ops)
2448cf7afbfeSThomas Graf 				return -EAFNOSUPPORT;
2449cf7afbfeSThomas Graf 
2450a100243dSCong Wang 			if (!af_ops->set_link_af)
2451cf7afbfeSThomas Graf 				return -EOPNOTSUPP;
2452cf7afbfeSThomas Graf 
2453cf7afbfeSThomas Graf 			if (af_ops->validate_link_af) {
24548679c31eSRocco Yue 				err = af_ops->validate_link_af(dev, af, extack);
2455a100243dSCong Wang 				if (err < 0)
2456cf7afbfeSThomas Graf 					return err;
2457cf7afbfeSThomas Graf 			}
2458cf7afbfeSThomas Graf 		}
2459cf7afbfeSThomas Graf 	}
2460cf7afbfeSThomas Graf 
24611840bb13SThomas Graf 	return 0;
24621840bb13SThomas Graf }
24631840bb13SThomas Graf 
2464cc8e27ccSEli Cohen static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
2465cc8e27ccSEli Cohen 				  int guid_type)
2466cc8e27ccSEli Cohen {
2467cc8e27ccSEli Cohen 	const struct net_device_ops *ops = dev->netdev_ops;
2468cc8e27ccSEli Cohen 
2469cc8e27ccSEli Cohen 	return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
2470cc8e27ccSEli Cohen }
2471cc8e27ccSEli Cohen 
2472cc8e27ccSEli Cohen static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
2473cc8e27ccSEli Cohen {
2474cc8e27ccSEli Cohen 	if (dev->type != ARPHRD_INFINIBAND)
2475cc8e27ccSEli Cohen 		return -EOPNOTSUPP;
2476cc8e27ccSEli Cohen 
2477cc8e27ccSEli Cohen 	return handle_infiniband_guid(dev, ivt, guid_type);
2478cc8e27ccSEli Cohen }
2479cc8e27ccSEli Cohen 
24804f7d2cdfSDaniel Borkmann static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
2481c02db8c6SChris Wright {
2482c02db8c6SChris Wright 	const struct net_device_ops *ops = dev->netdev_ops;
24834f7d2cdfSDaniel Borkmann 	int err = -EINVAL;
2484c02db8c6SChris Wright 
24854f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_MAC]) {
24864f7d2cdfSDaniel Borkmann 		struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
24874f7d2cdfSDaniel Borkmann 
2488ff08ddbaSDan Carpenter 		if (ivm->vf >= INT_MAX)
2489ff08ddbaSDan Carpenter 			return -EINVAL;
2490c02db8c6SChris Wright 		err = -EOPNOTSUPP;
2491c02db8c6SChris Wright 		if (ops->ndo_set_vf_mac)
2492c02db8c6SChris Wright 			err = ops->ndo_set_vf_mac(dev, ivm->vf,
2493c02db8c6SChris Wright 						  ivm->mac);
24944f7d2cdfSDaniel Borkmann 		if (err < 0)
24954f7d2cdfSDaniel Borkmann 			return err;
2496c02db8c6SChris Wright 	}
24974f7d2cdfSDaniel Borkmann 
24984f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_VLAN]) {
24994f7d2cdfSDaniel Borkmann 		struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
25004f7d2cdfSDaniel Borkmann 
2501ff08ddbaSDan Carpenter 		if (ivv->vf >= INT_MAX)
2502ff08ddbaSDan Carpenter 			return -EINVAL;
2503c02db8c6SChris Wright 		err = -EOPNOTSUPP;
2504c02db8c6SChris Wright 		if (ops->ndo_set_vf_vlan)
25054f7d2cdfSDaniel Borkmann 			err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
250679aab093SMoshe Shemesh 						   ivv->qos,
250779aab093SMoshe Shemesh 						   htons(ETH_P_8021Q));
250879aab093SMoshe Shemesh 		if (err < 0)
250979aab093SMoshe Shemesh 			return err;
251079aab093SMoshe Shemesh 	}
251179aab093SMoshe Shemesh 
251279aab093SMoshe Shemesh 	if (tb[IFLA_VF_VLAN_LIST]) {
251379aab093SMoshe Shemesh 		struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
251479aab093SMoshe Shemesh 		struct nlattr *attr;
251579aab093SMoshe Shemesh 		int rem, len = 0;
251679aab093SMoshe Shemesh 
251779aab093SMoshe Shemesh 		err = -EOPNOTSUPP;
251879aab093SMoshe Shemesh 		if (!ops->ndo_set_vf_vlan)
251979aab093SMoshe Shemesh 			return err;
252079aab093SMoshe Shemesh 
252179aab093SMoshe Shemesh 		nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
252279aab093SMoshe Shemesh 			if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
252379aab093SMoshe Shemesh 			    nla_len(attr) < NLA_HDRLEN) {
252479aab093SMoshe Shemesh 				return -EINVAL;
252579aab093SMoshe Shemesh 			}
252679aab093SMoshe Shemesh 			if (len >= MAX_VLAN_LIST_LEN)
252779aab093SMoshe Shemesh 				return -EOPNOTSUPP;
252879aab093SMoshe Shemesh 			ivvl[len] = nla_data(attr);
252979aab093SMoshe Shemesh 
253079aab093SMoshe Shemesh 			len++;
253179aab093SMoshe Shemesh 		}
2532fa34cd94SArnd Bergmann 		if (len == 0)
2533fa34cd94SArnd Bergmann 			return -EINVAL;
2534fa34cd94SArnd Bergmann 
2535ff08ddbaSDan Carpenter 		if (ivvl[0]->vf >= INT_MAX)
2536ff08ddbaSDan Carpenter 			return -EINVAL;
253779aab093SMoshe Shemesh 		err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
253879aab093SMoshe Shemesh 					   ivvl[0]->qos, ivvl[0]->vlan_proto);
25394f7d2cdfSDaniel Borkmann 		if (err < 0)
25404f7d2cdfSDaniel Borkmann 			return err;
2541c02db8c6SChris Wright 	}
25424f7d2cdfSDaniel Borkmann 
25434f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_TX_RATE]) {
25444f7d2cdfSDaniel Borkmann 		struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
2545ed616689SSucheta Chakraborty 		struct ifla_vf_info ivf;
25464f7d2cdfSDaniel Borkmann 
2547ff08ddbaSDan Carpenter 		if (ivt->vf >= INT_MAX)
2548ff08ddbaSDan Carpenter 			return -EINVAL;
2549c02db8c6SChris Wright 		err = -EOPNOTSUPP;
2550ed616689SSucheta Chakraborty 		if (ops->ndo_get_vf_config)
25514f7d2cdfSDaniel Borkmann 			err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
25524f7d2cdfSDaniel Borkmann 		if (err < 0)
25534f7d2cdfSDaniel Borkmann 			return err;
25544f7d2cdfSDaniel Borkmann 
2555a14857c2SBin Chen 		err = rtnl_set_vf_rate(dev, ivt->vf,
2556a14857c2SBin Chen 				       ivf.min_tx_rate, ivt->rate);
25574f7d2cdfSDaniel Borkmann 		if (err < 0)
25584f7d2cdfSDaniel Borkmann 			return err;
2559c02db8c6SChris Wright 	}
25604f7d2cdfSDaniel Borkmann 
25614f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_RATE]) {
25624f7d2cdfSDaniel Borkmann 		struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
25634f7d2cdfSDaniel Borkmann 
2564ff08ddbaSDan Carpenter 		if (ivt->vf >= INT_MAX)
2565ff08ddbaSDan Carpenter 			return -EINVAL;
2566a14857c2SBin Chen 
2567a14857c2SBin Chen 		err = rtnl_set_vf_rate(dev, ivt->vf,
2568a14857c2SBin Chen 				       ivt->min_tx_rate, ivt->max_tx_rate);
25694f7d2cdfSDaniel Borkmann 		if (err < 0)
25704f7d2cdfSDaniel Borkmann 			return err;
2571ed616689SSucheta Chakraborty 	}
25724f7d2cdfSDaniel Borkmann 
25734f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_SPOOFCHK]) {
25744f7d2cdfSDaniel Borkmann 		struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
25754f7d2cdfSDaniel Borkmann 
2576ff08ddbaSDan Carpenter 		if (ivs->vf >= INT_MAX)
2577ff08ddbaSDan Carpenter 			return -EINVAL;
25785f8444a3SGreg Rose 		err = -EOPNOTSUPP;
25795f8444a3SGreg Rose 		if (ops->ndo_set_vf_spoofchk)
25805f8444a3SGreg Rose 			err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
25815f8444a3SGreg Rose 						       ivs->setting);
25824f7d2cdfSDaniel Borkmann 		if (err < 0)
25834f7d2cdfSDaniel Borkmann 			return err;
25845f8444a3SGreg Rose 	}
25854f7d2cdfSDaniel Borkmann 
25864f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_LINK_STATE]) {
25874f7d2cdfSDaniel Borkmann 		struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
25884f7d2cdfSDaniel Borkmann 
2589ff08ddbaSDan Carpenter 		if (ivl->vf >= INT_MAX)
2590ff08ddbaSDan Carpenter 			return -EINVAL;
25911d8faf48SRony Efraim 		err = -EOPNOTSUPP;
25921d8faf48SRony Efraim 		if (ops->ndo_set_vf_link_state)
25931d8faf48SRony Efraim 			err = ops->ndo_set_vf_link_state(dev, ivl->vf,
25941d8faf48SRony Efraim 							 ivl->link_state);
25954f7d2cdfSDaniel Borkmann 		if (err < 0)
25964f7d2cdfSDaniel Borkmann 			return err;
25971d8faf48SRony Efraim 	}
25984f7d2cdfSDaniel Borkmann 
25994f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_RSS_QUERY_EN]) {
260001a3d796SVlad Zolotarov 		struct ifla_vf_rss_query_en *ivrssq_en;
260101a3d796SVlad Zolotarov 
260201a3d796SVlad Zolotarov 		err = -EOPNOTSUPP;
26034f7d2cdfSDaniel Borkmann 		ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
2604ff08ddbaSDan Carpenter 		if (ivrssq_en->vf >= INT_MAX)
2605ff08ddbaSDan Carpenter 			return -EINVAL;
260601a3d796SVlad Zolotarov 		if (ops->ndo_set_vf_rss_query_en)
26074f7d2cdfSDaniel Borkmann 			err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
260801a3d796SVlad Zolotarov 							   ivrssq_en->setting);
26094f7d2cdfSDaniel Borkmann 		if (err < 0)
26104f7d2cdfSDaniel Borkmann 			return err;
261101a3d796SVlad Zolotarov 	}
26124f7d2cdfSDaniel Borkmann 
2613dd461d6aSHiroshi Shimamoto 	if (tb[IFLA_VF_TRUST]) {
2614dd461d6aSHiroshi Shimamoto 		struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
2615dd461d6aSHiroshi Shimamoto 
2616ff08ddbaSDan Carpenter 		if (ivt->vf >= INT_MAX)
2617ff08ddbaSDan Carpenter 			return -EINVAL;
2618dd461d6aSHiroshi Shimamoto 		err = -EOPNOTSUPP;
2619dd461d6aSHiroshi Shimamoto 		if (ops->ndo_set_vf_trust)
2620dd461d6aSHiroshi Shimamoto 			err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
2621dd461d6aSHiroshi Shimamoto 		if (err < 0)
2622dd461d6aSHiroshi Shimamoto 			return err;
2623dd461d6aSHiroshi Shimamoto 	}
2624dd461d6aSHiroshi Shimamoto 
2625cc8e27ccSEli Cohen 	if (tb[IFLA_VF_IB_NODE_GUID]) {
2626cc8e27ccSEli Cohen 		struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
2627cc8e27ccSEli Cohen 
2628ff08ddbaSDan Carpenter 		if (ivt->vf >= INT_MAX)
2629ff08ddbaSDan Carpenter 			return -EINVAL;
2630cc8e27ccSEli Cohen 		if (!ops->ndo_set_vf_guid)
2631cc8e27ccSEli Cohen 			return -EOPNOTSUPP;
2632cc8e27ccSEli Cohen 		return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
2633cc8e27ccSEli Cohen 	}
2634cc8e27ccSEli Cohen 
2635cc8e27ccSEli Cohen 	if (tb[IFLA_VF_IB_PORT_GUID]) {
2636cc8e27ccSEli Cohen 		struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
2637cc8e27ccSEli Cohen 
2638ff08ddbaSDan Carpenter 		if (ivt->vf >= INT_MAX)
2639ff08ddbaSDan Carpenter 			return -EINVAL;
2640cc8e27ccSEli Cohen 		if (!ops->ndo_set_vf_guid)
2641cc8e27ccSEli Cohen 			return -EOPNOTSUPP;
2642cc8e27ccSEli Cohen 
2643cc8e27ccSEli Cohen 		return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
2644cc8e27ccSEli Cohen 	}
2645cc8e27ccSEli Cohen 
2646c02db8c6SChris Wright 	return err;
2647c02db8c6SChris Wright }
2648c02db8c6SChris Wright 
264933eaf2a6SDavid Ahern static int do_set_master(struct net_device *dev, int ifindex,
265033eaf2a6SDavid Ahern 			 struct netlink_ext_ack *extack)
2651fbaec0eaSJiri Pirko {
2652898e5061SJiri Pirko 	struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
2653fbaec0eaSJiri Pirko 	const struct net_device_ops *ops;
2654fbaec0eaSJiri Pirko 	int err;
2655fbaec0eaSJiri Pirko 
2656898e5061SJiri Pirko 	if (upper_dev) {
2657898e5061SJiri Pirko 		if (upper_dev->ifindex == ifindex)
2658fbaec0eaSJiri Pirko 			return 0;
2659898e5061SJiri Pirko 		ops = upper_dev->netdev_ops;
2660fbaec0eaSJiri Pirko 		if (ops->ndo_del_slave) {
2661898e5061SJiri Pirko 			err = ops->ndo_del_slave(upper_dev, dev);
2662fbaec0eaSJiri Pirko 			if (err)
2663fbaec0eaSJiri Pirko 				return err;
2664fbaec0eaSJiri Pirko 		} else {
2665fbaec0eaSJiri Pirko 			return -EOPNOTSUPP;
2666fbaec0eaSJiri Pirko 		}
2667fbaec0eaSJiri Pirko 	}
2668fbaec0eaSJiri Pirko 
2669fbaec0eaSJiri Pirko 	if (ifindex) {
2670898e5061SJiri Pirko 		upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
2671898e5061SJiri Pirko 		if (!upper_dev)
2672fbaec0eaSJiri Pirko 			return -EINVAL;
2673898e5061SJiri Pirko 		ops = upper_dev->netdev_ops;
2674fbaec0eaSJiri Pirko 		if (ops->ndo_add_slave) {
267533eaf2a6SDavid Ahern 			err = ops->ndo_add_slave(upper_dev, dev, extack);
2676fbaec0eaSJiri Pirko 			if (err)
2677fbaec0eaSJiri Pirko 				return err;
2678fbaec0eaSJiri Pirko 		} else {
2679fbaec0eaSJiri Pirko 			return -EOPNOTSUPP;
2680fbaec0eaSJiri Pirko 		}
2681fbaec0eaSJiri Pirko 	}
2682fbaec0eaSJiri Pirko 	return 0;
2683fbaec0eaSJiri Pirko }
2684fbaec0eaSJiri Pirko 
2685829eb208SRoopa Prabhu static const struct nla_policy ifla_proto_down_reason_policy[IFLA_PROTO_DOWN_REASON_VALUE + 1] = {
2686829eb208SRoopa Prabhu 	[IFLA_PROTO_DOWN_REASON_MASK]	= { .type = NLA_U32 },
2687829eb208SRoopa Prabhu 	[IFLA_PROTO_DOWN_REASON_VALUE]	= { .type = NLA_U32 },
2688829eb208SRoopa Prabhu };
2689829eb208SRoopa Prabhu 
2690829eb208SRoopa Prabhu static int do_set_proto_down(struct net_device *dev,
2691829eb208SRoopa Prabhu 			     struct nlattr *nl_proto_down,
2692829eb208SRoopa Prabhu 			     struct nlattr *nl_proto_down_reason,
2693829eb208SRoopa Prabhu 			     struct netlink_ext_ack *extack)
2694829eb208SRoopa Prabhu {
2695829eb208SRoopa Prabhu 	struct nlattr *pdreason[IFLA_PROTO_DOWN_REASON_MAX + 1];
2696829eb208SRoopa Prabhu 	unsigned long mask = 0;
2697829eb208SRoopa Prabhu 	u32 value;
2698829eb208SRoopa Prabhu 	bool proto_down;
2699829eb208SRoopa Prabhu 	int err;
2700829eb208SRoopa Prabhu 
27012106efdaSJakub Kicinski 	if (!(dev->priv_flags & IFF_CHANGE_PROTO_DOWN)) {
2702829eb208SRoopa Prabhu 		NL_SET_ERR_MSG(extack,  "Protodown not supported by device");
2703829eb208SRoopa Prabhu 		return -EOPNOTSUPP;
2704829eb208SRoopa Prabhu 	}
2705829eb208SRoopa Prabhu 
2706829eb208SRoopa Prabhu 	if (nl_proto_down_reason) {
2707829eb208SRoopa Prabhu 		err = nla_parse_nested_deprecated(pdreason,
2708829eb208SRoopa Prabhu 						  IFLA_PROTO_DOWN_REASON_MAX,
2709829eb208SRoopa Prabhu 						  nl_proto_down_reason,
2710829eb208SRoopa Prabhu 						  ifla_proto_down_reason_policy,
2711829eb208SRoopa Prabhu 						  NULL);
2712829eb208SRoopa Prabhu 		if (err < 0)
2713829eb208SRoopa Prabhu 			return err;
2714829eb208SRoopa Prabhu 
2715829eb208SRoopa Prabhu 		if (!pdreason[IFLA_PROTO_DOWN_REASON_VALUE]) {
2716829eb208SRoopa Prabhu 			NL_SET_ERR_MSG(extack, "Invalid protodown reason value");
2717829eb208SRoopa Prabhu 			return -EINVAL;
2718829eb208SRoopa Prabhu 		}
2719829eb208SRoopa Prabhu 
2720829eb208SRoopa Prabhu 		value = nla_get_u32(pdreason[IFLA_PROTO_DOWN_REASON_VALUE]);
2721829eb208SRoopa Prabhu 
2722829eb208SRoopa Prabhu 		if (pdreason[IFLA_PROTO_DOWN_REASON_MASK])
2723829eb208SRoopa Prabhu 			mask = nla_get_u32(pdreason[IFLA_PROTO_DOWN_REASON_MASK]);
2724829eb208SRoopa Prabhu 
2725829eb208SRoopa Prabhu 		dev_change_proto_down_reason(dev, mask, value);
2726829eb208SRoopa Prabhu 	}
2727829eb208SRoopa Prabhu 
2728829eb208SRoopa Prabhu 	if (nl_proto_down) {
2729829eb208SRoopa Prabhu 		proto_down = nla_get_u8(nl_proto_down);
2730829eb208SRoopa Prabhu 
2731d467d0bcSZheng Yongjun 		/* Don't turn off protodown if there are active reasons */
2732829eb208SRoopa Prabhu 		if (!proto_down && dev->proto_down_reason) {
2733829eb208SRoopa Prabhu 			NL_SET_ERR_MSG(extack, "Cannot clear protodown, active reasons");
2734829eb208SRoopa Prabhu 			return -EBUSY;
2735829eb208SRoopa Prabhu 		}
2736829eb208SRoopa Prabhu 		err = dev_change_proto_down(dev,
2737829eb208SRoopa Prabhu 					    proto_down);
2738829eb208SRoopa Prabhu 		if (err)
2739829eb208SRoopa Prabhu 			return err;
2740829eb208SRoopa Prabhu 	}
2741829eb208SRoopa Prabhu 
2742829eb208SRoopa Prabhu 	return 0;
2743829eb208SRoopa Prabhu }
2744829eb208SRoopa Prabhu 
274590c325e3SNicolas Dichtel #define DO_SETLINK_MODIFIED	0x01
2746ba998906SNicolas Dichtel /* notify flag means notify + modified. */
2747ba998906SNicolas Dichtel #define DO_SETLINK_NOTIFY	0x03
274890f62cf3SEric W. Biederman static int do_setlink(const struct sk_buff *skb,
274990f62cf3SEric W. Biederman 		      struct net_device *dev, struct ifinfomsg *ifm,
2750ddf9f970SJakub Kicinski 		      struct netlink_ext_ack *extack,
27515ea08b52SFlorent Fourcot 		      struct nlattr **tb, int status)
27520157f60cSPatrick McHardy {
2753d314774cSStephen Hemminger 	const struct net_device_ops *ops = dev->netdev_ops;
27545ea08b52SFlorent Fourcot 	char ifname[IFNAMSIZ];
27550157f60cSPatrick McHardy 	int err;
27560157f60cSPatrick McHardy 
27575ea08b52SFlorent Fourcot 	if (tb[IFLA_IFNAME])
27585ea08b52SFlorent Fourcot 		nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
27595ea08b52SFlorent Fourcot 	else
27605ea08b52SFlorent Fourcot 		ifname[0] = '\0';
27615ea08b52SFlorent Fourcot 
27627e4a8d5aSChristian Brauner 	if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_TARGET_NETNSID]) {
27635ea08b52SFlorent Fourcot 		const char *pat = ifname[0] ? ifname : NULL;
2764eeb85a14SAndrei Vagin 		struct net *net;
2765eeb85a14SAndrei Vagin 		int new_ifindex;
2766eeb85a14SAndrei Vagin 
2767eeb85a14SAndrei Vagin 		net = rtnl_link_get_net_capable(skb, dev_net(dev),
27687c4f63baSChristian Brauner 						tb, CAP_NET_ADMIN);
2769d8a5ec67SEric W. Biederman 		if (IS_ERR(net)) {
2770d8a5ec67SEric W. Biederman 			err = PTR_ERR(net);
2771d8a5ec67SEric W. Biederman 			goto errout;
2772d8a5ec67SEric W. Biederman 		}
27737c4f63baSChristian Brauner 
2774eeb85a14SAndrei Vagin 		if (tb[IFLA_NEW_IFINDEX])
2775eeb85a14SAndrei Vagin 			new_ifindex = nla_get_s32(tb[IFLA_NEW_IFINDEX]);
2776eeb85a14SAndrei Vagin 		else
2777eeb85a14SAndrei Vagin 			new_ifindex = 0;
2778eeb85a14SAndrei Vagin 
277996a6b93bSAndrey Ignatov 		err = __dev_change_net_namespace(dev, net, pat, new_ifindex);
2780d8a5ec67SEric W. Biederman 		put_net(net);
2781d8a5ec67SEric W. Biederman 		if (err)
2782d8a5ec67SEric W. Biederman 			goto errout;
278390c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
2784d8a5ec67SEric W. Biederman 	}
2785d8a5ec67SEric W. Biederman 
27860157f60cSPatrick McHardy 	if (tb[IFLA_MAP]) {
27870157f60cSPatrick McHardy 		struct rtnl_link_ifmap *u_map;
27880157f60cSPatrick McHardy 		struct ifmap k_map;
27890157f60cSPatrick McHardy 
2790d314774cSStephen Hemminger 		if (!ops->ndo_set_config) {
27910157f60cSPatrick McHardy 			err = -EOPNOTSUPP;
27920157f60cSPatrick McHardy 			goto errout;
27930157f60cSPatrick McHardy 		}
27940157f60cSPatrick McHardy 
27950157f60cSPatrick McHardy 		if (!netif_device_present(dev)) {
27960157f60cSPatrick McHardy 			err = -ENODEV;
27970157f60cSPatrick McHardy 			goto errout;
27980157f60cSPatrick McHardy 		}
27990157f60cSPatrick McHardy 
28000157f60cSPatrick McHardy 		u_map = nla_data(tb[IFLA_MAP]);
28010157f60cSPatrick McHardy 		k_map.mem_start = (unsigned long) u_map->mem_start;
28020157f60cSPatrick McHardy 		k_map.mem_end = (unsigned long) u_map->mem_end;
28030157f60cSPatrick McHardy 		k_map.base_addr = (unsigned short) u_map->base_addr;
28040157f60cSPatrick McHardy 		k_map.irq = (unsigned char) u_map->irq;
28050157f60cSPatrick McHardy 		k_map.dma = (unsigned char) u_map->dma;
28060157f60cSPatrick McHardy 		k_map.port = (unsigned char) u_map->port;
28070157f60cSPatrick McHardy 
2808d314774cSStephen Hemminger 		err = ops->ndo_set_config(dev, &k_map);
28090157f60cSPatrick McHardy 		if (err < 0)
28100157f60cSPatrick McHardy 			goto errout;
28110157f60cSPatrick McHardy 
2812ba998906SNicolas Dichtel 		status |= DO_SETLINK_NOTIFY;
28130157f60cSPatrick McHardy 	}
28140157f60cSPatrick McHardy 
28150157f60cSPatrick McHardy 	if (tb[IFLA_ADDRESS]) {
28160157f60cSPatrick McHardy 		struct sockaddr *sa;
28170157f60cSPatrick McHardy 		int len;
28180157f60cSPatrick McHardy 
2819153711f9SWANG Cong 		len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
2820153711f9SWANG Cong 						  sizeof(*sa));
28210157f60cSPatrick McHardy 		sa = kmalloc(len, GFP_KERNEL);
28220157f60cSPatrick McHardy 		if (!sa) {
28230157f60cSPatrick McHardy 			err = -ENOMEM;
28240157f60cSPatrick McHardy 			goto errout;
28250157f60cSPatrick McHardy 		}
28260157f60cSPatrick McHardy 		sa->sa_family = dev->type;
28270157f60cSPatrick McHardy 		memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
28280157f60cSPatrick McHardy 		       dev->addr_len);
28293b23a32aSCong Wang 		err = dev_set_mac_address_user(dev, sa, extack);
28300157f60cSPatrick McHardy 		kfree(sa);
28310157f60cSPatrick McHardy 		if (err)
28320157f60cSPatrick McHardy 			goto errout;
283390c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
28340157f60cSPatrick McHardy 	}
28350157f60cSPatrick McHardy 
28360157f60cSPatrick McHardy 	if (tb[IFLA_MTU]) {
28377a4c53beSStephen Hemminger 		err = dev_set_mtu_ext(dev, nla_get_u32(tb[IFLA_MTU]), extack);
28380157f60cSPatrick McHardy 		if (err < 0)
28390157f60cSPatrick McHardy 			goto errout;
284090c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
28410157f60cSPatrick McHardy 	}
28420157f60cSPatrick McHardy 
2843cbda10faSVlad Dogaru 	if (tb[IFLA_GROUP]) {
2844cbda10faSVlad Dogaru 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
2845ba998906SNicolas Dichtel 		status |= DO_SETLINK_NOTIFY;
2846cbda10faSVlad Dogaru 	}
2847cbda10faSVlad Dogaru 
28480157f60cSPatrick McHardy 	/*
28490157f60cSPatrick McHardy 	 * Interface selected by interface index but interface
28500157f60cSPatrick McHardy 	 * name provided implies that a name change has been
28510157f60cSPatrick McHardy 	 * requested.
28520157f60cSPatrick McHardy 	 */
28530157f60cSPatrick McHardy 	if (ifm->ifi_index > 0 && ifname[0]) {
28540157f60cSPatrick McHardy 		err = dev_change_name(dev, ifname);
28550157f60cSPatrick McHardy 		if (err < 0)
28560157f60cSPatrick McHardy 			goto errout;
285790c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
28580157f60cSPatrick McHardy 	}
28590157f60cSPatrick McHardy 
28600b815a1aSStephen Hemminger 	if (tb[IFLA_IFALIAS]) {
28610b815a1aSStephen Hemminger 		err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
28620b815a1aSStephen Hemminger 				    nla_len(tb[IFLA_IFALIAS]));
28630b815a1aSStephen Hemminger 		if (err < 0)
28640b815a1aSStephen Hemminger 			goto errout;
2865ba998906SNicolas Dichtel 		status |= DO_SETLINK_NOTIFY;
28660b815a1aSStephen Hemminger 	}
28670b815a1aSStephen Hemminger 
28680157f60cSPatrick McHardy 	if (tb[IFLA_BROADCAST]) {
28690157f60cSPatrick McHardy 		nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
2870e7c3273eSJiri Pirko 		call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
28710157f60cSPatrick McHardy 	}
28720157f60cSPatrick McHardy 
2873fbaec0eaSJiri Pirko 	if (tb[IFLA_MASTER]) {
287433eaf2a6SDavid Ahern 		err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
2875fbaec0eaSJiri Pirko 		if (err)
2876fbaec0eaSJiri Pirko 			goto errout;
287790c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
2878fbaec0eaSJiri Pirko 	}
2879fbaec0eaSJiri Pirko 
2880a4abfa62SPhil Sutter 	if (ifm->ifi_flags || ifm->ifi_change) {
2881a4abfa62SPhil Sutter 		err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
2882a4abfa62SPhil Sutter 				       extack);
2883a4abfa62SPhil Sutter 		if (err < 0)
2884a4abfa62SPhil Sutter 			goto errout;
2885a4abfa62SPhil Sutter 	}
2886a4abfa62SPhil Sutter 
28879a57247fSJiri Pirko 	if (tb[IFLA_CARRIER]) {
28889a57247fSJiri Pirko 		err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
28899a57247fSJiri Pirko 		if (err)
28909a57247fSJiri Pirko 			goto errout;
289190c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
28929a57247fSJiri Pirko 	}
28939a57247fSJiri Pirko 
28945d1180fcSNicolas Dichtel 	if (tb[IFLA_TXQLEN]) {
28950cd29503SAlexey Dobriyan 		unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]);
28965d1180fcSNicolas Dichtel 
28976a643ddbSCong Wang 		err = dev_change_tx_queue_len(dev, value);
28986a643ddbSCong Wang 		if (err)
289908294a26SJason Wang 			goto errout;
29002d7f669bSXin Long 		status |= DO_SETLINK_MODIFIED;
290108294a26SJason Wang 	}
29020157f60cSPatrick McHardy 
290346e6b992SStephen Hemminger 	if (tb[IFLA_GSO_MAX_SIZE]) {
290446e6b992SStephen Hemminger 		u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]);
290546e6b992SStephen Hemminger 
290646e6b992SStephen Hemminger 		if (dev->gso_max_size ^ max_size) {
290746e6b992SStephen Hemminger 			netif_set_gso_max_size(dev, max_size);
290846e6b992SStephen Hemminger 			status |= DO_SETLINK_MODIFIED;
290946e6b992SStephen Hemminger 		}
291046e6b992SStephen Hemminger 	}
291146e6b992SStephen Hemminger 
291246e6b992SStephen Hemminger 	if (tb[IFLA_GSO_MAX_SEGS]) {
291346e6b992SStephen Hemminger 		u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
291446e6b992SStephen Hemminger 
291546e6b992SStephen Hemminger 		if (dev->gso_max_segs ^ max_segs) {
29166d872df3SEric Dumazet 			netif_set_gso_max_segs(dev, max_segs);
291746e6b992SStephen Hemminger 			status |= DO_SETLINK_MODIFIED;
291846e6b992SStephen Hemminger 		}
291946e6b992SStephen Hemminger 	}
292046e6b992SStephen Hemminger 
2921eac1b93cSCoco Li 	if (tb[IFLA_GRO_MAX_SIZE]) {
2922eac1b93cSCoco Li 		u32 gro_max_size = nla_get_u32(tb[IFLA_GRO_MAX_SIZE]);
2923eac1b93cSCoco Li 
2924eac1b93cSCoco Li 		if (dev->gro_max_size ^ gro_max_size) {
2925eac1b93cSCoco Li 			netif_set_gro_max_size(dev, gro_max_size);
2926eac1b93cSCoco Li 			status |= DO_SETLINK_MODIFIED;
2927eac1b93cSCoco Li 		}
2928eac1b93cSCoco Li 	}
2929eac1b93cSCoco Li 
29309eefedd5SXin Long 	if (tb[IFLA_GSO_IPV4_MAX_SIZE]) {
29319eefedd5SXin Long 		u32 max_size = nla_get_u32(tb[IFLA_GSO_IPV4_MAX_SIZE]);
29329eefedd5SXin Long 
29339eefedd5SXin Long 		if (dev->gso_ipv4_max_size ^ max_size) {
29349eefedd5SXin Long 			netif_set_gso_ipv4_max_size(dev, max_size);
29359eefedd5SXin Long 			status |= DO_SETLINK_MODIFIED;
29369eefedd5SXin Long 		}
29379eefedd5SXin Long 	}
29389eefedd5SXin Long 
29399eefedd5SXin Long 	if (tb[IFLA_GRO_IPV4_MAX_SIZE]) {
29409eefedd5SXin Long 		u32 gro_max_size = nla_get_u32(tb[IFLA_GRO_IPV4_MAX_SIZE]);
29419eefedd5SXin Long 
29429eefedd5SXin Long 		if (dev->gro_ipv4_max_size ^ gro_max_size) {
29439eefedd5SXin Long 			netif_set_gro_ipv4_max_size(dev, gro_max_size);
29449eefedd5SXin Long 			status |= DO_SETLINK_MODIFIED;
29459eefedd5SXin Long 		}
29469eefedd5SXin Long 	}
29479eefedd5SXin Long 
29480157f60cSPatrick McHardy 	if (tb[IFLA_OPERSTATE])
294993b2d4a2SDavid S. Miller 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
29500157f60cSPatrick McHardy 
29510157f60cSPatrick McHardy 	if (tb[IFLA_LINKMODE]) {
29521889b0e7SNicolas Dichtel 		unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
29531889b0e7SNicolas Dichtel 
2954fd888e85SSebastian Andrzej Siewior 		write_lock(&dev_base_lock);
29551889b0e7SNicolas Dichtel 		if (dev->link_mode ^ value)
2956ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
29571889b0e7SNicolas Dichtel 		dev->link_mode = value;
2958fd888e85SSebastian Andrzej Siewior 		write_unlock(&dev_base_lock);
29590157f60cSPatrick McHardy 	}
29600157f60cSPatrick McHardy 
2961c02db8c6SChris Wright 	if (tb[IFLA_VFINFO_LIST]) {
29624f7d2cdfSDaniel Borkmann 		struct nlattr *vfinfo[IFLA_VF_MAX + 1];
2963c02db8c6SChris Wright 		struct nlattr *attr;
2964c02db8c6SChris Wright 		int rem;
29654f7d2cdfSDaniel Borkmann 
2966c02db8c6SChris Wright 		nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
29674f7d2cdfSDaniel Borkmann 			if (nla_type(attr) != IFLA_VF_INFO ||
29684f7d2cdfSDaniel Borkmann 			    nla_len(attr) < NLA_HDRLEN) {
2969253683bbSDavid Howells 				err = -EINVAL;
2970c02db8c6SChris Wright 				goto errout;
2971253683bbSDavid Howells 			}
29728cb08174SJohannes Berg 			err = nla_parse_nested_deprecated(vfinfo, IFLA_VF_MAX,
29738cb08174SJohannes Berg 							  attr,
29748cb08174SJohannes Berg 							  ifla_vf_policy,
29758cb08174SJohannes Berg 							  NULL);
29764f7d2cdfSDaniel Borkmann 			if (err < 0)
29774f7d2cdfSDaniel Borkmann 				goto errout;
29784f7d2cdfSDaniel Borkmann 			err = do_setvfinfo(dev, vfinfo);
2979ebc08a6fSWilliams, Mitch A 			if (err < 0)
2980ebc08a6fSWilliams, Mitch A 				goto errout;
2981ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
2982ebc08a6fSWilliams, Mitch A 		}
2983ebc08a6fSWilliams, Mitch A 	}
29840157f60cSPatrick McHardy 	err = 0;
29850157f60cSPatrick McHardy 
298657b61080SScott Feldman 	if (tb[IFLA_VF_PORTS]) {
298757b61080SScott Feldman 		struct nlattr *port[IFLA_PORT_MAX+1];
298857b61080SScott Feldman 		struct nlattr *attr;
298957b61080SScott Feldman 		int vf;
299057b61080SScott Feldman 		int rem;
299157b61080SScott Feldman 
299257b61080SScott Feldman 		err = -EOPNOTSUPP;
299357b61080SScott Feldman 		if (!ops->ndo_set_vf_port)
299457b61080SScott Feldman 			goto errout;
299557b61080SScott Feldman 
299657b61080SScott Feldman 		nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
2997035d210fSDaniel Borkmann 			if (nla_type(attr) != IFLA_VF_PORT ||
2998035d210fSDaniel Borkmann 			    nla_len(attr) < NLA_HDRLEN) {
2999035d210fSDaniel Borkmann 				err = -EINVAL;
3000035d210fSDaniel Borkmann 				goto errout;
3001035d210fSDaniel Borkmann 			}
30028cb08174SJohannes Berg 			err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
30038cb08174SJohannes Berg 							  attr,
30048cb08174SJohannes Berg 							  ifla_port_policy,
30058cb08174SJohannes Berg 							  NULL);
300657b61080SScott Feldman 			if (err < 0)
300757b61080SScott Feldman 				goto errout;
300857b61080SScott Feldman 			if (!port[IFLA_PORT_VF]) {
300957b61080SScott Feldman 				err = -EOPNOTSUPP;
301057b61080SScott Feldman 				goto errout;
301157b61080SScott Feldman 			}
301257b61080SScott Feldman 			vf = nla_get_u32(port[IFLA_PORT_VF]);
301357b61080SScott Feldman 			err = ops->ndo_set_vf_port(dev, vf, port);
301457b61080SScott Feldman 			if (err < 0)
301557b61080SScott Feldman 				goto errout;
3016ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
301757b61080SScott Feldman 		}
301857b61080SScott Feldman 	}
301957b61080SScott Feldman 	err = 0;
302057b61080SScott Feldman 
302157b61080SScott Feldman 	if (tb[IFLA_PORT_SELF]) {
302257b61080SScott Feldman 		struct nlattr *port[IFLA_PORT_MAX+1];
302357b61080SScott Feldman 
30248cb08174SJohannes Berg 		err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
30258cb08174SJohannes Berg 						  tb[IFLA_PORT_SELF],
30268cb08174SJohannes Berg 						  ifla_port_policy, NULL);
302757b61080SScott Feldman 		if (err < 0)
302857b61080SScott Feldman 			goto errout;
302957b61080SScott Feldman 
303057b61080SScott Feldman 		err = -EOPNOTSUPP;
303157b61080SScott Feldman 		if (ops->ndo_set_vf_port)
303257b61080SScott Feldman 			err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
303357b61080SScott Feldman 		if (err < 0)
303457b61080SScott Feldman 			goto errout;
3035ba998906SNicolas Dichtel 		status |= DO_SETLINK_NOTIFY;
303657b61080SScott Feldman 	}
3037f8ff182cSThomas Graf 
3038f8ff182cSThomas Graf 	if (tb[IFLA_AF_SPEC]) {
3039f8ff182cSThomas Graf 		struct nlattr *af;
3040f8ff182cSThomas Graf 		int rem;
3041f8ff182cSThomas Graf 
3042f8ff182cSThomas Graf 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
3043f8ff182cSThomas Graf 			const struct rtnl_af_ops *af_ops;
3044f8ff182cSThomas Graf 
3045058c8d59SGustavo A. R. Silva 			BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af))));
3046f8ff182cSThomas Graf 
30473583a4e8SStephen Hemminger 			err = af_ops->set_link_af(dev, af, extack);
3048a100243dSCong Wang 			if (err < 0)
3049f8ff182cSThomas Graf 				goto errout;
3050f8ff182cSThomas Graf 
3051ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
3052f8ff182cSThomas Graf 		}
3053f8ff182cSThomas Graf 	}
305457b61080SScott Feldman 	err = 0;
305557b61080SScott Feldman 
3056829eb208SRoopa Prabhu 	if (tb[IFLA_PROTO_DOWN] || tb[IFLA_PROTO_DOWN_REASON]) {
3057829eb208SRoopa Prabhu 		err = do_set_proto_down(dev, tb[IFLA_PROTO_DOWN],
3058829eb208SRoopa Prabhu 					tb[IFLA_PROTO_DOWN_REASON], extack);
305988d6378bSAnuradha Karuppiah 		if (err)
306088d6378bSAnuradha Karuppiah 			goto errout;
306188d6378bSAnuradha Karuppiah 		status |= DO_SETLINK_NOTIFY;
306288d6378bSAnuradha Karuppiah 	}
306388d6378bSAnuradha Karuppiah 
3064d1fdd913SBrenden Blanco 	if (tb[IFLA_XDP]) {
3065d1fdd913SBrenden Blanco 		struct nlattr *xdp[IFLA_XDP_MAX + 1];
306685de8576SDaniel Borkmann 		u32 xdp_flags = 0;
3067d1fdd913SBrenden Blanco 
30688cb08174SJohannes Berg 		err = nla_parse_nested_deprecated(xdp, IFLA_XDP_MAX,
30698cb08174SJohannes Berg 						  tb[IFLA_XDP],
3070fceb6435SJohannes Berg 						  ifla_xdp_policy, NULL);
3071d1fdd913SBrenden Blanco 		if (err < 0)
3072d1fdd913SBrenden Blanco 			goto errout;
3073d1fdd913SBrenden Blanco 
307458038695SMartin KaFai Lau 		if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
3075262d8625SBrenden Blanco 			err = -EINVAL;
3076262d8625SBrenden Blanco 			goto errout;
3077262d8625SBrenden Blanco 		}
307885de8576SDaniel Borkmann 
307985de8576SDaniel Borkmann 		if (xdp[IFLA_XDP_FLAGS]) {
308085de8576SDaniel Borkmann 			xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
308185de8576SDaniel Borkmann 			if (xdp_flags & ~XDP_FLAGS_MASK) {
308285de8576SDaniel Borkmann 				err = -EINVAL;
308385de8576SDaniel Borkmann 				goto errout;
308485de8576SDaniel Borkmann 			}
3085ee5d032fSJakub Kicinski 			if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) {
30860489df9aSDaniel Borkmann 				err = -EINVAL;
30870489df9aSDaniel Borkmann 				goto errout;
30880489df9aSDaniel Borkmann 			}
308985de8576SDaniel Borkmann 		}
309085de8576SDaniel Borkmann 
3091d1fdd913SBrenden Blanco 		if (xdp[IFLA_XDP_FD]) {
309292234c8fSToke Høiland-Jørgensen 			int expected_fd = -1;
309392234c8fSToke Høiland-Jørgensen 
309492234c8fSToke Høiland-Jørgensen 			if (xdp_flags & XDP_FLAGS_REPLACE) {
309592234c8fSToke Høiland-Jørgensen 				if (!xdp[IFLA_XDP_EXPECTED_FD]) {
309692234c8fSToke Høiland-Jørgensen 					err = -EINVAL;
309792234c8fSToke Høiland-Jørgensen 					goto errout;
309892234c8fSToke Høiland-Jørgensen 				}
309992234c8fSToke Høiland-Jørgensen 				expected_fd =
310092234c8fSToke Høiland-Jørgensen 					nla_get_s32(xdp[IFLA_XDP_EXPECTED_FD]);
310192234c8fSToke Høiland-Jørgensen 			}
310292234c8fSToke Høiland-Jørgensen 
3103ddf9f970SJakub Kicinski 			err = dev_change_xdp_fd(dev, extack,
310485de8576SDaniel Borkmann 						nla_get_s32(xdp[IFLA_XDP_FD]),
310592234c8fSToke Høiland-Jørgensen 						expected_fd,
310685de8576SDaniel Borkmann 						xdp_flags);
3107d1fdd913SBrenden Blanco 			if (err)
3108d1fdd913SBrenden Blanco 				goto errout;
3109d1fdd913SBrenden Blanco 			status |= DO_SETLINK_NOTIFY;
3110d1fdd913SBrenden Blanco 		}
3111d1fdd913SBrenden Blanco 	}
3112d1fdd913SBrenden Blanco 
31130157f60cSPatrick McHardy errout:
3114ba998906SNicolas Dichtel 	if (status & DO_SETLINK_MODIFIED) {
311564ff90ccSXin Long 		if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY)
3116ba998906SNicolas Dichtel 			netdev_state_change(dev);
3117ba998906SNicolas Dichtel 
3118ba998906SNicolas Dichtel 		if (err < 0)
3119e87cc472SJoe 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",
3120e87cc472SJoe Perches 					     dev->name);
3121ba998906SNicolas Dichtel 	}
31220157f60cSPatrick McHardy 
31230157f60cSPatrick McHardy 	return err;
31240157f60cSPatrick McHardy }
31250157f60cSPatrick McHardy 
3126cc6090e9SJiri Pirko static struct net_device *rtnl_dev_get(struct net *net,
31275ea08b52SFlorent Fourcot 				       struct nlattr *tb[])
3128cc6090e9SJiri Pirko {
31295ea08b52SFlorent Fourcot 	char ifname[ALTIFNAMSIZ];
3130cc6090e9SJiri Pirko 
31315ea08b52SFlorent Fourcot 	if (tb[IFLA_IFNAME])
31325ea08b52SFlorent Fourcot 		nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
31335ea08b52SFlorent Fourcot 	else if (tb[IFLA_ALT_IFNAME])
31345ea08b52SFlorent Fourcot 		nla_strscpy(ifname, tb[IFLA_ALT_IFNAME], ALTIFNAMSIZ);
3135cc6090e9SJiri Pirko 	else
3136cc6090e9SJiri Pirko 		return NULL;
3137cc6090e9SJiri Pirko 
3138cc6090e9SJiri Pirko 	return __dev_get_by_name(net, ifname);
3139cc6090e9SJiri Pirko }
3140cc6090e9SJiri Pirko 
3141c21ef3e3SDavid Ahern static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3142c21ef3e3SDavid Ahern 			struct netlink_ext_ack *extack)
3143da5e0494SThomas Graf {
31443b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
3145da5e0494SThomas Graf 	struct ifinfomsg *ifm;
3146da5e0494SThomas Graf 	struct net_device *dev;
31470157f60cSPatrick McHardy 	int err;
3148da5e0494SThomas Graf 	struct nlattr *tb[IFLA_MAX+1];
31491da177e4SLinus Torvalds 
31508cb08174SJohannes Berg 	err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
31518cb08174SJohannes Berg 				     ifla_policy, extack);
3152da5e0494SThomas Graf 	if (err < 0)
3153da5e0494SThomas Graf 		goto errout;
31541da177e4SLinus Torvalds 
31554ff66caeSChristian Brauner 	err = rtnl_ensure_unique_netns(tb, extack, false);
31564ff66caeSChristian Brauner 	if (err < 0)
31574ff66caeSChristian Brauner 		goto errout;
31584ff66caeSChristian Brauner 
31591da177e4SLinus Torvalds 	err = -EINVAL;
3160da5e0494SThomas Graf 	ifm = nlmsg_data(nlh);
316151055be8SPatrick McHardy 	if (ifm->ifi_index > 0)
3162a3d12891SEric Dumazet 		dev = __dev_get_by_index(net, ifm->ifi_index);
316376c9ac0eSJiri Pirko 	else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
31645ea08b52SFlorent Fourcot 		dev = rtnl_dev_get(net, tb);
3165da5e0494SThomas Graf 	else
3166da5e0494SThomas Graf 		goto errout;
31671da177e4SLinus Torvalds 
3168da5e0494SThomas Graf 	if (dev == NULL) {
3169da5e0494SThomas Graf 		err = -ENODEV;
3170da5e0494SThomas Graf 		goto errout;
3171da5e0494SThomas Graf 	}
31721da177e4SLinus Torvalds 
317389da780aSXin Long 	err = validate_linkmsg(dev, tb, extack);
317489da780aSXin Long 	if (err < 0)
317589da780aSXin Long 		goto errout;
317689da780aSXin Long 
31775ea08b52SFlorent Fourcot 	err = do_setlink(skb, dev, ifm, extack, tb, 0);
3178da5e0494SThomas Graf errout:
31791da177e4SLinus Torvalds 	return err;
31801da177e4SLinus Torvalds }
31811da177e4SLinus Torvalds 
318266400d54SWANG Cong static int rtnl_group_dellink(const struct net *net, int group)
318366400d54SWANG Cong {
318466400d54SWANG Cong 	struct net_device *dev, *aux;
318566400d54SWANG Cong 	LIST_HEAD(list_kill);
318666400d54SWANG Cong 	bool found = false;
318766400d54SWANG Cong 
318866400d54SWANG Cong 	if (!group)
318966400d54SWANG Cong 		return -EPERM;
319066400d54SWANG Cong 
319166400d54SWANG Cong 	for_each_netdev(net, dev) {
319266400d54SWANG Cong 		if (dev->group == group) {
319366400d54SWANG Cong 			const struct rtnl_link_ops *ops;
319466400d54SWANG Cong 
319566400d54SWANG Cong 			found = true;
319666400d54SWANG Cong 			ops = dev->rtnl_link_ops;
319766400d54SWANG Cong 			if (!ops || !ops->dellink)
319866400d54SWANG Cong 				return -EOPNOTSUPP;
319966400d54SWANG Cong 		}
320066400d54SWANG Cong 	}
320166400d54SWANG Cong 
320266400d54SWANG Cong 	if (!found)
320366400d54SWANG Cong 		return -ENODEV;
320466400d54SWANG Cong 
320566400d54SWANG Cong 	for_each_netdev_safe(net, dev, aux) {
320666400d54SWANG Cong 		if (dev->group == group) {
320766400d54SWANG Cong 			const struct rtnl_link_ops *ops;
320866400d54SWANG Cong 
320966400d54SWANG Cong 			ops = dev->rtnl_link_ops;
321066400d54SWANG Cong 			ops->dellink(dev, &list_kill);
321166400d54SWANG Cong 		}
321266400d54SWANG Cong 	}
321366400d54SWANG Cong 	unregister_netdevice_many(&list_kill);
321466400d54SWANG Cong 
321566400d54SWANG Cong 	return 0;
321666400d54SWANG Cong }
321766400d54SWANG Cong 
3218f3a63cceSHangbin Liu int rtnl_delete_link(struct net_device *dev, u32 portid, const struct nlmsghdr *nlh)
3219614732eaSThomas Graf {
3220614732eaSThomas Graf 	const struct rtnl_link_ops *ops;
3221614732eaSThomas Graf 	LIST_HEAD(list_kill);
3222614732eaSThomas Graf 
3223614732eaSThomas Graf 	ops = dev->rtnl_link_ops;
3224614732eaSThomas Graf 	if (!ops || !ops->dellink)
3225614732eaSThomas Graf 		return -EOPNOTSUPP;
3226614732eaSThomas Graf 
3227614732eaSThomas Graf 	ops->dellink(dev, &list_kill);
3228f3a63cceSHangbin Liu 	unregister_netdevice_many_notify(&list_kill, portid, nlh);
3229614732eaSThomas Graf 
3230614732eaSThomas Graf 	return 0;
3231614732eaSThomas Graf }
3232614732eaSThomas Graf EXPORT_SYMBOL_GPL(rtnl_delete_link);
3233614732eaSThomas Graf 
3234c21ef3e3SDavid Ahern static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
3235c21ef3e3SDavid Ahern 			struct netlink_ext_ack *extack)
323638f7b870SPatrick McHardy {
32373b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
3238f3a63cceSHangbin Liu 	u32 portid = NETLINK_CB(skb).portid;
3239b61ad68aSChristian Brauner 	struct net *tgt_net = net;
3240b61ad68aSChristian Brauner 	struct net_device *dev = NULL;
324138f7b870SPatrick McHardy 	struct ifinfomsg *ifm;
324238f7b870SPatrick McHardy 	struct nlattr *tb[IFLA_MAX+1];
324338f7b870SPatrick McHardy 	int err;
3244b61ad68aSChristian Brauner 	int netnsid = -1;
324538f7b870SPatrick McHardy 
32468cb08174SJohannes Berg 	err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
32478cb08174SJohannes Berg 				     ifla_policy, extack);
324838f7b870SPatrick McHardy 	if (err < 0)
324938f7b870SPatrick McHardy 		return err;
325038f7b870SPatrick McHardy 
32514ff66caeSChristian Brauner 	err = rtnl_ensure_unique_netns(tb, extack, true);
32524ff66caeSChristian Brauner 	if (err < 0)
32534ff66caeSChristian Brauner 		return err;
32544ff66caeSChristian Brauner 
32557e4a8d5aSChristian Brauner 	if (tb[IFLA_TARGET_NETNSID]) {
32567e4a8d5aSChristian Brauner 		netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
3257c383edc4SChristian Brauner 		tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
3258b61ad68aSChristian Brauner 		if (IS_ERR(tgt_net))
3259b61ad68aSChristian Brauner 			return PTR_ERR(tgt_net);
3260b61ad68aSChristian Brauner 	}
3261b61ad68aSChristian Brauner 
3262b61ad68aSChristian Brauner 	err = -EINVAL;
326338f7b870SPatrick McHardy 	ifm = nlmsg_data(nlh);
326438f7b870SPatrick McHardy 	if (ifm->ifi_index > 0)
3265b61ad68aSChristian Brauner 		dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
326676c9ac0eSJiri Pirko 	else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
32675ea08b52SFlorent Fourcot 		dev = rtnl_dev_get(net, tb);
326866400d54SWANG Cong 	else if (tb[IFLA_GROUP])
3269b61ad68aSChristian Brauner 		err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP]));
327038f7b870SPatrick McHardy 	else
3271b61ad68aSChristian Brauner 		goto out;
327238f7b870SPatrick McHardy 
3273b61ad68aSChristian Brauner 	if (!dev) {
3274dee04163SFlorent Fourcot 		if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME] || ifm->ifi_index > 0)
3275b61ad68aSChristian Brauner 			err = -ENODEV;
327638f7b870SPatrick McHardy 
3277b61ad68aSChristian Brauner 		goto out;
3278b61ad68aSChristian Brauner 	}
3279b61ad68aSChristian Brauner 
3280f3a63cceSHangbin Liu 	err = rtnl_delete_link(dev, portid, nlh);
3281b61ad68aSChristian Brauner 
3282b61ad68aSChristian Brauner out:
3283b61ad68aSChristian Brauner 	if (netnsid >= 0)
3284b61ad68aSChristian Brauner 		put_net(tgt_net);
3285b61ad68aSChristian Brauner 
3286b61ad68aSChristian Brauner 	return err;
328738f7b870SPatrick McHardy }
328838f7b870SPatrick McHardy 
32891d997f10SHangbin Liu int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm,
32901d997f10SHangbin Liu 			u32 portid, const struct nlmsghdr *nlh)
32913729d502SPatrick McHardy {
32923729d502SPatrick McHardy 	unsigned int old_flags;
32933729d502SPatrick McHardy 	int err;
32943729d502SPatrick McHardy 
32953729d502SPatrick McHardy 	old_flags = dev->flags;
32963729d502SPatrick McHardy 	if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
32976d040321SPetr Machata 		err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
32986d040321SPetr Machata 					 NULL);
32993729d502SPatrick McHardy 		if (err < 0)
33003729d502SPatrick McHardy 			return err;
33013729d502SPatrick McHardy 	}
33023729d502SPatrick McHardy 
33038d356b89SRoopa Prabhu 	if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) {
33041d997f10SHangbin Liu 		__dev_notify_flags(dev, old_flags, (old_flags ^ dev->flags), portid, nlh);
33058d356b89SRoopa Prabhu 	} else {
33063729d502SPatrick McHardy 		dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
33071d997f10SHangbin Liu 		__dev_notify_flags(dev, old_flags, ~0U, portid, nlh);
33088d356b89SRoopa Prabhu 	}
33093729d502SPatrick McHardy 	return 0;
33103729d502SPatrick McHardy }
33113729d502SPatrick McHardy EXPORT_SYMBOL(rtnl_configure_link);
33123729d502SPatrick McHardy 
3313d0522f1cSDavid Ahern struct net_device *rtnl_create_link(struct net *net, const char *ifname,
3314d0522f1cSDavid Ahern 				    unsigned char name_assign_type,
3315d0522f1cSDavid Ahern 				    const struct rtnl_link_ops *ops,
3316d0522f1cSDavid Ahern 				    struct nlattr *tb[],
3317d0522f1cSDavid Ahern 				    struct netlink_ext_ack *extack)
3318e7199288SPavel Emelianov {
3319e7199288SPavel Emelianov 	struct net_device *dev;
3320d40156aaSJiri Pirko 	unsigned int num_tx_queues = 1;
3321d40156aaSJiri Pirko 	unsigned int num_rx_queues = 1;
3322b0ad3c17SXin Long 	int err;
3323e7199288SPavel Emelianov 
332476ff5cc9SJiri Pirko 	if (tb[IFLA_NUM_TX_QUEUES])
332576ff5cc9SJiri Pirko 		num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
332676ff5cc9SJiri Pirko 	else if (ops->get_num_tx_queues)
3327d40156aaSJiri Pirko 		num_tx_queues = ops->get_num_tx_queues();
332876ff5cc9SJiri Pirko 
332976ff5cc9SJiri Pirko 	if (tb[IFLA_NUM_RX_QUEUES])
333076ff5cc9SJiri Pirko 		num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
333176ff5cc9SJiri Pirko 	else if (ops->get_num_rx_queues)
3332d40156aaSJiri Pirko 		num_rx_queues = ops->get_num_rx_queues();
3333efacb309Sstephen hemminger 
3334d0522f1cSDavid Ahern 	if (num_tx_queues < 1 || num_tx_queues > 4096) {
3335d0522f1cSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid number of transmit queues");
33360e1d6ecaSEric Dumazet 		return ERR_PTR(-EINVAL);
3337d0522f1cSDavid Ahern 	}
33380e1d6ecaSEric Dumazet 
3339d0522f1cSDavid Ahern 	if (num_rx_queues < 1 || num_rx_queues > 4096) {
3340d0522f1cSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid number of receive queues");
33410e1d6ecaSEric Dumazet 		return ERR_PTR(-EINVAL);
3342d0522f1cSDavid Ahern 	}
33430e1d6ecaSEric Dumazet 
33448c713dc9SJohannes Berg 	if (ops->alloc) {
33458c713dc9SJohannes Berg 		dev = ops->alloc(tb, ifname, name_assign_type,
33468c713dc9SJohannes Berg 				 num_tx_queues, num_rx_queues);
33478c713dc9SJohannes Berg 		if (IS_ERR(dev))
33488c713dc9SJohannes Berg 			return dev;
33498c713dc9SJohannes Berg 	} else {
33508c713dc9SJohannes Berg 		dev = alloc_netdev_mqs(ops->priv_size, ifname,
33518c713dc9SJohannes Berg 				       name_assign_type, ops->setup,
33528c713dc9SJohannes Berg 				       num_tx_queues, num_rx_queues);
33538c713dc9SJohannes Berg 	}
33548c713dc9SJohannes Berg 
3355e7199288SPavel Emelianov 	if (!dev)
3356d1892e4eSTobias Klauser 		return ERR_PTR(-ENOMEM);
3357e7199288SPavel Emelianov 
3358b0ad3c17SXin Long 	err = validate_linkmsg(dev, tb, extack);
3359b0ad3c17SXin Long 	if (err < 0) {
3360b0ad3c17SXin Long 		free_netdev(dev);
3361b0ad3c17SXin Long 		return ERR_PTR(err);
3362b0ad3c17SXin Long 	}
3363b0ad3c17SXin Long 
336481adee47SEric W. Biederman 	dev_net_set(dev, net);
336581adee47SEric W. Biederman 	dev->rtnl_link_ops = ops;
33663729d502SPatrick McHardy 	dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
336781adee47SEric W. Biederman 
3368d836f5c6SEric Dumazet 	if (tb[IFLA_MTU]) {
3369d836f5c6SEric Dumazet 		u32 mtu = nla_get_u32(tb[IFLA_MTU]);
3370d836f5c6SEric Dumazet 
3371d836f5c6SEric Dumazet 		err = dev_validate_mtu(dev, mtu, extack);
3372d836f5c6SEric Dumazet 		if (err) {
3373d836f5c6SEric Dumazet 			free_netdev(dev);
3374d836f5c6SEric Dumazet 			return ERR_PTR(err);
3375d836f5c6SEric Dumazet 		}
3376d836f5c6SEric Dumazet 		dev->mtu = mtu;
3377d836f5c6SEric Dumazet 	}
33782afb9b53SJiri Pirko 	if (tb[IFLA_ADDRESS]) {
3379efd38f75SJakub Kicinski 		__dev_addr_set(dev, nla_data(tb[IFLA_ADDRESS]),
3380e7199288SPavel Emelianov 			       nla_len(tb[IFLA_ADDRESS]));
33812afb9b53SJiri Pirko 		dev->addr_assign_type = NET_ADDR_SET;
33822afb9b53SJiri Pirko 	}
3383e7199288SPavel Emelianov 	if (tb[IFLA_BROADCAST])
3384e7199288SPavel Emelianov 		memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
3385e7199288SPavel Emelianov 				nla_len(tb[IFLA_BROADCAST]));
3386e7199288SPavel Emelianov 	if (tb[IFLA_TXQLEN])
3387e7199288SPavel Emelianov 		dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
3388e7199288SPavel Emelianov 	if (tb[IFLA_OPERSTATE])
338993b2d4a2SDavid S. Miller 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
3390e7199288SPavel Emelianov 	if (tb[IFLA_LINKMODE])
3391e7199288SPavel Emelianov 		dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
3392ffa934f1SPatrick McHardy 	if (tb[IFLA_GROUP])
3393ffa934f1SPatrick McHardy 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
339446e6b992SStephen Hemminger 	if (tb[IFLA_GSO_MAX_SIZE])
339546e6b992SStephen Hemminger 		netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE]));
339646e6b992SStephen Hemminger 	if (tb[IFLA_GSO_MAX_SEGS])
33976d872df3SEric Dumazet 		netif_set_gso_max_segs(dev, nla_get_u32(tb[IFLA_GSO_MAX_SEGS]));
3398eac1b93cSCoco Li 	if (tb[IFLA_GRO_MAX_SIZE])
3399eac1b93cSCoco Li 		netif_set_gro_max_size(dev, nla_get_u32(tb[IFLA_GRO_MAX_SIZE]));
34009eefedd5SXin Long 	if (tb[IFLA_GSO_IPV4_MAX_SIZE])
34019eefedd5SXin Long 		netif_set_gso_ipv4_max_size(dev, nla_get_u32(tb[IFLA_GSO_IPV4_MAX_SIZE]));
34029eefedd5SXin Long 	if (tb[IFLA_GRO_IPV4_MAX_SIZE])
34039eefedd5SXin Long 		netif_set_gro_ipv4_max_size(dev, nla_get_u32(tb[IFLA_GRO_IPV4_MAX_SIZE]));
3404e7199288SPavel Emelianov 
3405e7199288SPavel Emelianov 	return dev;
3406e7199288SPavel Emelianov }
3407e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_create_link);
3408e7199288SPavel Emelianov 
340990f62cf3SEric W. Biederman static int rtnl_group_changelink(const struct sk_buff *skb,
341090f62cf3SEric W. Biederman 		struct net *net, int group,
3411e7ed828fSVlad Dogaru 		struct ifinfomsg *ifm,
3412ddf9f970SJakub Kicinski 		struct netlink_ext_ack *extack,
3413e7ed828fSVlad Dogaru 		struct nlattr **tb)
3414e7ed828fSVlad Dogaru {
3415d079535dSWANG Cong 	struct net_device *dev, *aux;
3416e7ed828fSVlad Dogaru 	int err;
3417e7ed828fSVlad Dogaru 
3418d079535dSWANG Cong 	for_each_netdev_safe(net, dev, aux) {
3419e7ed828fSVlad Dogaru 		if (dev->group == group) {
342089da780aSXin Long 			err = validate_linkmsg(dev, tb, extack);
342189da780aSXin Long 			if (err < 0)
342289da780aSXin Long 				return err;
34235ea08b52SFlorent Fourcot 			err = do_setlink(skb, dev, ifm, extack, tb, 0);
3424e7ed828fSVlad Dogaru 			if (err < 0)
3425e7ed828fSVlad Dogaru 				return err;
3426e7ed828fSVlad Dogaru 		}
3427e7ed828fSVlad Dogaru 	}
3428e7ed828fSVlad Dogaru 
3429e7ed828fSVlad Dogaru 	return 0;
3430e7ed828fSVlad Dogaru }
3431e7ed828fSVlad Dogaru 
343263105e83SJakub Kicinski static int rtnl_newlink_create(struct sk_buff *skb, struct ifinfomsg *ifm,
343363105e83SJakub Kicinski 			       const struct rtnl_link_ops *ops,
3434d88e136cSHangbin Liu 			       const struct nlmsghdr *nlh,
343563105e83SJakub Kicinski 			       struct nlattr **tb, struct nlattr **data,
343602839cc8SJakub Kicinski 			       struct netlink_ext_ack *extack)
343702839cc8SJakub Kicinski {
343802839cc8SJakub Kicinski 	unsigned char name_assign_type = NET_NAME_USER;
343902839cc8SJakub Kicinski 	struct net *net = sock_net(skb->sk);
3440d88e136cSHangbin Liu 	u32 portid = NETLINK_CB(skb).portid;
344102839cc8SJakub Kicinski 	struct net *dest_net, *link_net;
344202839cc8SJakub Kicinski 	struct net_device *dev;
344302839cc8SJakub Kicinski 	char ifname[IFNAMSIZ];
344402839cc8SJakub Kicinski 	int err;
344502839cc8SJakub Kicinski 
344602839cc8SJakub Kicinski 	if (!ops->alloc && !ops->setup)
344702839cc8SJakub Kicinski 		return -EOPNOTSUPP;
344802839cc8SJakub Kicinski 
344902839cc8SJakub Kicinski 	if (tb[IFLA_IFNAME]) {
345002839cc8SJakub Kicinski 		nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
345102839cc8SJakub Kicinski 	} else {
345202839cc8SJakub Kicinski 		snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
345302839cc8SJakub Kicinski 		name_assign_type = NET_NAME_ENUM;
345402839cc8SJakub Kicinski 	}
345502839cc8SJakub Kicinski 
345602839cc8SJakub Kicinski 	dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN);
345702839cc8SJakub Kicinski 	if (IS_ERR(dest_net))
345802839cc8SJakub Kicinski 		return PTR_ERR(dest_net);
345902839cc8SJakub Kicinski 
346002839cc8SJakub Kicinski 	if (tb[IFLA_LINK_NETNSID]) {
346102839cc8SJakub Kicinski 		int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
346202839cc8SJakub Kicinski 
346302839cc8SJakub Kicinski 		link_net = get_net_ns_by_id(dest_net, id);
346402839cc8SJakub Kicinski 		if (!link_net) {
346502839cc8SJakub Kicinski 			NL_SET_ERR_MSG(extack, "Unknown network namespace id");
346602839cc8SJakub Kicinski 			err =  -EINVAL;
346702839cc8SJakub Kicinski 			goto out;
346802839cc8SJakub Kicinski 		}
346902839cc8SJakub Kicinski 		err = -EPERM;
347002839cc8SJakub Kicinski 		if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
347102839cc8SJakub Kicinski 			goto out;
347202839cc8SJakub Kicinski 	} else {
347302839cc8SJakub Kicinski 		link_net = NULL;
347402839cc8SJakub Kicinski 	}
347502839cc8SJakub Kicinski 
347602839cc8SJakub Kicinski 	dev = rtnl_create_link(link_net ? : dest_net, ifname,
347702839cc8SJakub Kicinski 			       name_assign_type, ops, tb, extack);
347802839cc8SJakub Kicinski 	if (IS_ERR(dev)) {
347902839cc8SJakub Kicinski 		err = PTR_ERR(dev);
348002839cc8SJakub Kicinski 		goto out;
348102839cc8SJakub Kicinski 	}
348202839cc8SJakub Kicinski 
348302839cc8SJakub Kicinski 	dev->ifindex = ifm->ifi_index;
348402839cc8SJakub Kicinski 
348502839cc8SJakub Kicinski 	if (ops->newlink)
348602839cc8SJakub Kicinski 		err = ops->newlink(link_net ? : net, dev, tb, data, extack);
348702839cc8SJakub Kicinski 	else
348802839cc8SJakub Kicinski 		err = register_netdevice(dev);
348902839cc8SJakub Kicinski 	if (err < 0) {
349002839cc8SJakub Kicinski 		free_netdev(dev);
349102839cc8SJakub Kicinski 		goto out;
349202839cc8SJakub Kicinski 	}
349302839cc8SJakub Kicinski 
3494d88e136cSHangbin Liu 	err = rtnl_configure_link(dev, ifm, portid, nlh);
349502839cc8SJakub Kicinski 	if (err < 0)
349602839cc8SJakub Kicinski 		goto out_unregister;
349702839cc8SJakub Kicinski 	if (link_net) {
349802839cc8SJakub Kicinski 		err = dev_change_net_namespace(dev, dest_net, ifname);
349902839cc8SJakub Kicinski 		if (err < 0)
350002839cc8SJakub Kicinski 			goto out_unregister;
350102839cc8SJakub Kicinski 	}
350202839cc8SJakub Kicinski 	if (tb[IFLA_MASTER]) {
350302839cc8SJakub Kicinski 		err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
350402839cc8SJakub Kicinski 		if (err)
350502839cc8SJakub Kicinski 			goto out_unregister;
350602839cc8SJakub Kicinski 	}
350702839cc8SJakub Kicinski out:
350802839cc8SJakub Kicinski 	if (link_net)
350902839cc8SJakub Kicinski 		put_net(link_net);
351002839cc8SJakub Kicinski 	put_net(dest_net);
351102839cc8SJakub Kicinski 	return err;
351202839cc8SJakub Kicinski out_unregister:
351302839cc8SJakub Kicinski 	if (ops->newlink) {
351402839cc8SJakub Kicinski 		LIST_HEAD(list_kill);
351502839cc8SJakub Kicinski 
351602839cc8SJakub Kicinski 		ops->dellink(dev, &list_kill);
351702839cc8SJakub Kicinski 		unregister_netdevice_many(&list_kill);
351802839cc8SJakub Kicinski 	} else {
351902839cc8SJakub Kicinski 		unregister_netdevice(dev);
352002839cc8SJakub Kicinski 	}
352102839cc8SJakub Kicinski 	goto out;
352202839cc8SJakub Kicinski }
352363105e83SJakub Kicinski 
3524c92bf26cSJakub Kicinski struct rtnl_newlink_tbs {
3525c92bf26cSJakub Kicinski 	struct nlattr *tb[IFLA_MAX + 1];
3526c92bf26cSJakub Kicinski 	struct nlattr *attr[RTNL_MAX_TYPE + 1];
3527420d0318SJakub Kicinski 	struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1];
3528c92bf26cSJakub Kicinski };
3529c92bf26cSJakub Kicinski 
3530c92bf26cSJakub Kicinski static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3531c92bf26cSJakub Kicinski 			  struct rtnl_newlink_tbs *tbs,
3532c92bf26cSJakub Kicinski 			  struct netlink_ext_ack *extack)
3533c92bf26cSJakub Kicinski {
3534420d0318SJakub Kicinski 	struct nlattr *linkinfo[IFLA_INFO_MAX + 1];
3535c92bf26cSJakub Kicinski 	struct nlattr ** const tb = tbs->tb;
3536c6f6f244SEric Dumazet 	const struct rtnl_link_ops *m_ops;
3537c6f6f244SEric Dumazet 	struct net_device *master_dev;
35383b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
353938f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops;
3540420d0318SJakub Kicinski 	struct nlattr **slave_data;
3541420d0318SJakub Kicinski 	char kind[MODULE_NAME_LEN];
3542420d0318SJakub Kicinski 	struct net_device *dev;
3543420d0318SJakub Kicinski 	struct ifinfomsg *ifm;
3544420d0318SJakub Kicinski 	struct nlattr **data;
3545ef2a7c90SFlorent Fourcot 	bool link_specified;
354638f7b870SPatrick McHardy 	int err;
354738f7b870SPatrick McHardy 
354895a5afcaSJohannes Berg #ifdef CONFIG_MODULES
354938f7b870SPatrick McHardy replay:
35508072f085SThomas Graf #endif
35518cb08174SJohannes Berg 	err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
35528cb08174SJohannes Berg 				     ifla_policy, extack);
355338f7b870SPatrick McHardy 	if (err < 0)
355438f7b870SPatrick McHardy 		return err;
355538f7b870SPatrick McHardy 
35564ff66caeSChristian Brauner 	err = rtnl_ensure_unique_netns(tb, extack, false);
35574ff66caeSChristian Brauner 	if (err < 0)
35584ff66caeSChristian Brauner 		return err;
35594ff66caeSChristian Brauner 
356038f7b870SPatrick McHardy 	ifm = nlmsg_data(nlh);
3561ef2a7c90SFlorent Fourcot 	if (ifm->ifi_index > 0) {
3562ef2a7c90SFlorent Fourcot 		link_specified = true;
3563881d966bSEric W. Biederman 		dev = __dev_get_by_index(net, ifm->ifi_index);
3564ef2a7c90SFlorent Fourcot 	} else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) {
3565ef2a7c90SFlorent Fourcot 		link_specified = true;
35665ea08b52SFlorent Fourcot 		dev = rtnl_dev_get(net, tb);
3567ef2a7c90SFlorent Fourcot 	} else {
3568ef2a7c90SFlorent Fourcot 		link_specified = false;
356938f7b870SPatrick McHardy 		dev = NULL;
3570ef2a7c90SFlorent Fourcot 	}
357138f7b870SPatrick McHardy 
3572c6f6f244SEric Dumazet 	master_dev = NULL;
3573c6f6f244SEric Dumazet 	m_ops = NULL;
3574ba7d49b1SJiri Pirko 	if (dev) {
3575ba7d49b1SJiri Pirko 		master_dev = netdev_master_upper_dev_get(dev);
3576ba7d49b1SJiri Pirko 		if (master_dev)
3577ba7d49b1SJiri Pirko 			m_ops = master_dev->rtnl_link_ops;
3578ba7d49b1SJiri Pirko 	}
3579ba7d49b1SJiri Pirko 
358038f7b870SPatrick McHardy 	if (tb[IFLA_LINKINFO]) {
35818cb08174SJohannes Berg 		err = nla_parse_nested_deprecated(linkinfo, IFLA_INFO_MAX,
35828cb08174SJohannes Berg 						  tb[IFLA_LINKINFO],
35838cb08174SJohannes Berg 						  ifla_info_policy, NULL);
358438f7b870SPatrick McHardy 		if (err < 0)
358538f7b870SPatrick McHardy 			return err;
358638f7b870SPatrick McHardy 	} else
358738f7b870SPatrick McHardy 		memset(linkinfo, 0, sizeof(linkinfo));
358838f7b870SPatrick McHardy 
358938f7b870SPatrick McHardy 	if (linkinfo[IFLA_INFO_KIND]) {
3590872f6903SFrancis Laniel 		nla_strscpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
359138f7b870SPatrick McHardy 		ops = rtnl_link_ops_get(kind);
359238f7b870SPatrick McHardy 	} else {
359338f7b870SPatrick McHardy 		kind[0] = '\0';
359438f7b870SPatrick McHardy 		ops = NULL;
359538f7b870SPatrick McHardy 	}
359638f7b870SPatrick McHardy 
3597420d0318SJakub Kicinski 	data = NULL;
359838f7b870SPatrick McHardy 	if (ops) {
3599ccf8dbcdSKees Cook 		if (ops->maxtype > RTNL_MAX_TYPE)
3600ccf8dbcdSKees Cook 			return -EINVAL;
3601ccf8dbcdSKees Cook 
360238f7b870SPatrick McHardy 		if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
3603c92bf26cSJakub Kicinski 			err = nla_parse_nested_deprecated(tbs->attr, ops->maxtype,
360438f7b870SPatrick McHardy 							  linkinfo[IFLA_INFO_DATA],
360568d57f3bSDavid Ahern 							  ops->policy, extack);
360638f7b870SPatrick McHardy 			if (err < 0)
360738f7b870SPatrick McHardy 				return err;
3608c92bf26cSJakub Kicinski 			data = tbs->attr;
360938f7b870SPatrick McHardy 		}
361038f7b870SPatrick McHardy 		if (ops->validate) {
3611a8b8a889SMatthias Schiffer 			err = ops->validate(tb, data, extack);
361238f7b870SPatrick McHardy 			if (err < 0)
361338f7b870SPatrick McHardy 				return err;
361438f7b870SPatrick McHardy 		}
361538f7b870SPatrick McHardy 	}
361638f7b870SPatrick McHardy 
3617420d0318SJakub Kicinski 	slave_data = NULL;
3618ba7d49b1SJiri Pirko 	if (m_ops) {
3619af066ed3SPrashant Bhole 		if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE)
3620ccf8dbcdSKees Cook 			return -EINVAL;
3621ccf8dbcdSKees Cook 
3622ba7d49b1SJiri Pirko 		if (m_ops->slave_maxtype &&
3623ba7d49b1SJiri Pirko 		    linkinfo[IFLA_INFO_SLAVE_DATA]) {
3624c92bf26cSJakub Kicinski 			err = nla_parse_nested_deprecated(tbs->slave_attr,
36258cb08174SJohannes Berg 							  m_ops->slave_maxtype,
3626ba7d49b1SJiri Pirko 							  linkinfo[IFLA_INFO_SLAVE_DATA],
36278cb08174SJohannes Berg 							  m_ops->slave_policy,
36288cb08174SJohannes Berg 							  extack);
3629ba7d49b1SJiri Pirko 			if (err < 0)
3630ba7d49b1SJiri Pirko 				return err;
3631c92bf26cSJakub Kicinski 			slave_data = tbs->slave_attr;
3632ba7d49b1SJiri Pirko 		}
3633ba7d49b1SJiri Pirko 	}
3634ba7d49b1SJiri Pirko 
363538f7b870SPatrick McHardy 	if (dev) {
363690c325e3SNicolas Dichtel 		int status = 0;
363738f7b870SPatrick McHardy 
363838f7b870SPatrick McHardy 		if (nlh->nlmsg_flags & NLM_F_EXCL)
363938f7b870SPatrick McHardy 			return -EEXIST;
364038f7b870SPatrick McHardy 		if (nlh->nlmsg_flags & NLM_F_REPLACE)
364138f7b870SPatrick McHardy 			return -EOPNOTSUPP;
364238f7b870SPatrick McHardy 
364389da780aSXin Long 		err = validate_linkmsg(dev, tb, extack);
364489da780aSXin Long 		if (err < 0)
364589da780aSXin Long 			return err;
364689da780aSXin Long 
364738f7b870SPatrick McHardy 		if (linkinfo[IFLA_INFO_DATA]) {
364838f7b870SPatrick McHardy 			if (!ops || ops != dev->rtnl_link_ops ||
364938f7b870SPatrick McHardy 			    !ops->changelink)
365038f7b870SPatrick McHardy 				return -EOPNOTSUPP;
365138f7b870SPatrick McHardy 
3652ad744b22SMatthias Schiffer 			err = ops->changelink(dev, tb, data, extack);
365338f7b870SPatrick McHardy 			if (err < 0)
365438f7b870SPatrick McHardy 				return err;
3655ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
365638f7b870SPatrick McHardy 		}
365738f7b870SPatrick McHardy 
3658ba7d49b1SJiri Pirko 		if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
3659ba7d49b1SJiri Pirko 			if (!m_ops || !m_ops->slave_changelink)
3660ba7d49b1SJiri Pirko 				return -EOPNOTSUPP;
3661ba7d49b1SJiri Pirko 
3662420d0318SJakub Kicinski 			err = m_ops->slave_changelink(master_dev, dev, tb,
3663420d0318SJakub Kicinski 						      slave_data, extack);
3664ba7d49b1SJiri Pirko 			if (err < 0)
3665ba7d49b1SJiri Pirko 				return err;
3666ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
3667ba7d49b1SJiri Pirko 		}
3668ba7d49b1SJiri Pirko 
36695ea08b52SFlorent Fourcot 		return do_setlink(skb, dev, ifm, extack, tb, status);
367038f7b870SPatrick McHardy 	}
367138f7b870SPatrick McHardy 
3672ffa934f1SPatrick McHardy 	if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
3673ef2a7c90SFlorent Fourcot 		/* No dev found and NLM_F_CREATE not set. Requested dev does not exist,
3674ef2a7c90SFlorent Fourcot 		 * or it's for a group
3675ef2a7c90SFlorent Fourcot 		*/
3676ef2a7c90SFlorent Fourcot 		if (link_specified)
3677ef2a7c90SFlorent Fourcot 			return -ENODEV;
3678ef2a7c90SFlorent Fourcot 		if (tb[IFLA_GROUP])
367990f62cf3SEric W. Biederman 			return rtnl_group_changelink(skb, net,
3680ffa934f1SPatrick McHardy 						nla_get_u32(tb[IFLA_GROUP]),
3681ddf9f970SJakub Kicinski 						ifm, extack, tb);
36826f37c9f9SFlorent Fourcot 		return -ENODEV;
3683ffa934f1SPatrick McHardy 	}
368438f7b870SPatrick McHardy 
3685160ca014STheuns Verwoerd 	if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
368638f7b870SPatrick McHardy 		return -EOPNOTSUPP;
368738f7b870SPatrick McHardy 
368838f7b870SPatrick McHardy 	if (!ops) {
368995a5afcaSJohannes Berg #ifdef CONFIG_MODULES
369038f7b870SPatrick McHardy 		if (kind[0]) {
369138f7b870SPatrick McHardy 			__rtnl_unlock();
369238f7b870SPatrick McHardy 			request_module("rtnl-link-%s", kind);
369338f7b870SPatrick McHardy 			rtnl_lock();
369438f7b870SPatrick McHardy 			ops = rtnl_link_ops_get(kind);
369538f7b870SPatrick McHardy 			if (ops)
369638f7b870SPatrick McHardy 				goto replay;
369738f7b870SPatrick McHardy 		}
369838f7b870SPatrick McHardy #endif
369968d57f3bSDavid Ahern 		NL_SET_ERR_MSG(extack, "Unknown device type");
370038f7b870SPatrick McHardy 		return -EOPNOTSUPP;
370138f7b870SPatrick McHardy 	}
370238f7b870SPatrick McHardy 
3703d88e136cSHangbin Liu 	return rtnl_newlink_create(skb, ifm, ops, nlh, tb, data, extack);
370463105e83SJakub Kicinski }
370563105e83SJakub Kicinski 
3706a2939745SJakub Kicinski static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3707a2939745SJakub Kicinski 			struct netlink_ext_ack *extack)
3708a2939745SJakub Kicinski {
3709c92bf26cSJakub Kicinski 	struct rtnl_newlink_tbs *tbs;
3710a2939745SJakub Kicinski 	int ret;
3711a2939745SJakub Kicinski 
3712c92bf26cSJakub Kicinski 	tbs = kmalloc(sizeof(*tbs), GFP_KERNEL);
3713c92bf26cSJakub Kicinski 	if (!tbs)
3714a2939745SJakub Kicinski 		return -ENOMEM;
3715a2939745SJakub Kicinski 
3716c92bf26cSJakub Kicinski 	ret = __rtnl_newlink(skb, nlh, tbs, extack);
3717c92bf26cSJakub Kicinski 	kfree(tbs);
3718a2939745SJakub Kicinski 	return ret;
3719a2939745SJakub Kicinski }
3720a2939745SJakub Kicinski 
37219b3757b0SJakub Kicinski static int rtnl_valid_getlink_req(struct sk_buff *skb,
37229b3757b0SJakub Kicinski 				  const struct nlmsghdr *nlh,
37239b3757b0SJakub Kicinski 				  struct nlattr **tb,
37249b3757b0SJakub Kicinski 				  struct netlink_ext_ack *extack)
37259b3757b0SJakub Kicinski {
37269b3757b0SJakub Kicinski 	struct ifinfomsg *ifm;
37279b3757b0SJakub Kicinski 	int i, err;
37289b3757b0SJakub Kicinski 
37299b3757b0SJakub Kicinski 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
37309b3757b0SJakub Kicinski 		NL_SET_ERR_MSG(extack, "Invalid header for get link");
37319b3757b0SJakub Kicinski 		return -EINVAL;
37329b3757b0SJakub Kicinski 	}
37339b3757b0SJakub Kicinski 
37349b3757b0SJakub Kicinski 	if (!netlink_strict_get_check(skb))
37358cb08174SJohannes Berg 		return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
37368cb08174SJohannes Berg 					      ifla_policy, extack);
37379b3757b0SJakub Kicinski 
37389b3757b0SJakub Kicinski 	ifm = nlmsg_data(nlh);
37399b3757b0SJakub Kicinski 	if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
37409b3757b0SJakub Kicinski 	    ifm->ifi_change) {
37419b3757b0SJakub Kicinski 		NL_SET_ERR_MSG(extack, "Invalid values in header for get link request");
37429b3757b0SJakub Kicinski 		return -EINVAL;
37439b3757b0SJakub Kicinski 	}
37449b3757b0SJakub Kicinski 
37458cb08174SJohannes Berg 	err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFLA_MAX,
37468cb08174SJohannes Berg 					    ifla_policy, extack);
37479b3757b0SJakub Kicinski 	if (err)
37489b3757b0SJakub Kicinski 		return err;
37499b3757b0SJakub Kicinski 
37509b3757b0SJakub Kicinski 	for (i = 0; i <= IFLA_MAX; i++) {
37519b3757b0SJakub Kicinski 		if (!tb[i])
37529b3757b0SJakub Kicinski 			continue;
37539b3757b0SJakub Kicinski 
37549b3757b0SJakub Kicinski 		switch (i) {
37559b3757b0SJakub Kicinski 		case IFLA_IFNAME:
375676c9ac0eSJiri Pirko 		case IFLA_ALT_IFNAME:
37579b3757b0SJakub Kicinski 		case IFLA_EXT_MASK:
37589b3757b0SJakub Kicinski 		case IFLA_TARGET_NETNSID:
37599b3757b0SJakub Kicinski 			break;
37609b3757b0SJakub Kicinski 		default:
37619b3757b0SJakub Kicinski 			NL_SET_ERR_MSG(extack, "Unsupported attribute in get link request");
37629b3757b0SJakub Kicinski 			return -EINVAL;
37639b3757b0SJakub Kicinski 		}
37649b3757b0SJakub Kicinski 	}
37659b3757b0SJakub Kicinski 
37669b3757b0SJakub Kicinski 	return 0;
37679b3757b0SJakub Kicinski }
37689b3757b0SJakub Kicinski 
3769c21ef3e3SDavid Ahern static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3770c21ef3e3SDavid Ahern 			struct netlink_ext_ack *extack)
3771711e2c33SJean Tourrilhes {
37723b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
377379e1ad14SJiri Benc 	struct net *tgt_net = net;
3774b60c5115SThomas Graf 	struct ifinfomsg *ifm;
3775b60c5115SThomas Graf 	struct nlattr *tb[IFLA_MAX+1];
3776b60c5115SThomas Graf 	struct net_device *dev = NULL;
3777b60c5115SThomas Graf 	struct sk_buff *nskb;
377879e1ad14SJiri Benc 	int netnsid = -1;
3779339bf98fSThomas Graf 	int err;
3780115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
3781711e2c33SJean Tourrilhes 
37829b3757b0SJakub Kicinski 	err = rtnl_valid_getlink_req(skb, nlh, tb, extack);
3783b60c5115SThomas Graf 	if (err < 0)
37849918f230SEric Sesterhenn 		return err;
3785b60c5115SThomas Graf 
37864ff66caeSChristian Brauner 	err = rtnl_ensure_unique_netns(tb, extack, true);
37874ff66caeSChristian Brauner 	if (err < 0)
37884ff66caeSChristian Brauner 		return err;
37894ff66caeSChristian Brauner 
37907e4a8d5aSChristian Brauner 	if (tb[IFLA_TARGET_NETNSID]) {
37917e4a8d5aSChristian Brauner 		netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
3792c383edc4SChristian Brauner 		tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
379379e1ad14SJiri Benc 		if (IS_ERR(tgt_net))
379479e1ad14SJiri Benc 			return PTR_ERR(tgt_net);
379579e1ad14SJiri Benc 	}
379679e1ad14SJiri Benc 
3797115c9b81SGreg Rose 	if (tb[IFLA_EXT_MASK])
3798115c9b81SGreg Rose 		ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3799115c9b81SGreg Rose 
380079e1ad14SJiri Benc 	err = -EINVAL;
3801b60c5115SThomas Graf 	ifm = nlmsg_data(nlh);
3802a3d12891SEric Dumazet 	if (ifm->ifi_index > 0)
380379e1ad14SJiri Benc 		dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
380476c9ac0eSJiri Pirko 	else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
38055ea08b52SFlorent Fourcot 		dev = rtnl_dev_get(tgt_net, tb);
3806a3d12891SEric Dumazet 	else
380779e1ad14SJiri Benc 		goto out;
3808b60c5115SThomas Graf 
380979e1ad14SJiri Benc 	err = -ENODEV;
3810a3d12891SEric Dumazet 	if (dev == NULL)
381179e1ad14SJiri Benc 		goto out;
3812a3d12891SEric Dumazet 
381379e1ad14SJiri Benc 	err = -ENOBUFS;
3814115c9b81SGreg Rose 	nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
3815a3d12891SEric Dumazet 	if (nskb == NULL)
381679e1ad14SJiri Benc 		goto out;
3817711e2c33SJean Tourrilhes 
381879e1ad14SJiri Benc 	err = rtnl_fill_ifinfo(nskb, dev, net,
381979e1ad14SJiri Benc 			       RTM_NEWLINK, NETLINK_CB(skb).portid,
382079e1ad14SJiri Benc 			       nlh->nlmsg_seq, 0, 0, ext_filter_mask,
3821d4e4fdf9SGuillaume Nault 			       0, NULL, 0, netnsid, GFP_KERNEL);
382226932566SPatrick McHardy 	if (err < 0) {
382326932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in if_nlmsg_size */
382426932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
382526932566SPatrick McHardy 		kfree_skb(nskb);
3826a3d12891SEric Dumazet 	} else
382715e47304SEric W. Biederman 		err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
382879e1ad14SJiri Benc out:
382979e1ad14SJiri Benc 	if (netnsid >= 0)
383079e1ad14SJiri Benc 		put_net(tgt_net);
3831b60c5115SThomas Graf 
3832711e2c33SJean Tourrilhes 	return err;
3833711e2c33SJean Tourrilhes }
3834711e2c33SJean Tourrilhes 
383536fbf1e5SJiri Pirko static int rtnl_alt_ifname(int cmd, struct net_device *dev, struct nlattr *attr,
383636fbf1e5SJiri Pirko 			   bool *changed, struct netlink_ext_ack *extack)
383736fbf1e5SJiri Pirko {
383836fbf1e5SJiri Pirko 	char *alt_ifname;
3839155fb43bSJakub Kicinski 	size_t size;
384036fbf1e5SJiri Pirko 	int err;
384136fbf1e5SJiri Pirko 
384236fbf1e5SJiri Pirko 	err = nla_validate(attr, attr->nla_len, IFLA_MAX, ifla_policy, extack);
384336fbf1e5SJiri Pirko 	if (err)
384436fbf1e5SJiri Pirko 		return err;
384536fbf1e5SJiri Pirko 
3846155fb43bSJakub Kicinski 	if (cmd == RTM_NEWLINKPROP) {
3847155fb43bSJakub Kicinski 		size = rtnl_prop_list_size(dev);
3848155fb43bSJakub Kicinski 		size += nla_total_size(ALTIFNAMSIZ);
3849155fb43bSJakub Kicinski 		if (size >= U16_MAX) {
3850155fb43bSJakub Kicinski 			NL_SET_ERR_MSG(extack,
3851155fb43bSJakub Kicinski 				       "effective property list too long");
3852155fb43bSJakub Kicinski 			return -EINVAL;
3853155fb43bSJakub Kicinski 		}
3854155fb43bSJakub Kicinski 	}
3855155fb43bSJakub Kicinski 
38565d26cff5SJakub Kicinski 	alt_ifname = nla_strdup(attr, GFP_KERNEL_ACCOUNT);
385736fbf1e5SJiri Pirko 	if (!alt_ifname)
385836fbf1e5SJiri Pirko 		return -ENOMEM;
385944bfa9c5SEric Dumazet 
386044bfa9c5SEric Dumazet 	if (cmd == RTM_NEWLINKPROP) {
386136fbf1e5SJiri Pirko 		err = netdev_name_node_alt_create(dev, alt_ifname);
386244bfa9c5SEric Dumazet 		if (!err)
386344bfa9c5SEric Dumazet 			alt_ifname = NULL;
386436fbf1e5SJiri Pirko 	} else if (cmd == RTM_DELLINKPROP) {
386536fbf1e5SJiri Pirko 		err = netdev_name_node_alt_destroy(dev, alt_ifname);
386636fbf1e5SJiri Pirko 	} else {
386744bfa9c5SEric Dumazet 		WARN_ON_ONCE(1);
386844bfa9c5SEric Dumazet 		err = -EINVAL;
386936fbf1e5SJiri Pirko 	}
387036fbf1e5SJiri Pirko 
387144bfa9c5SEric Dumazet 	kfree(alt_ifname);
387244bfa9c5SEric Dumazet 	if (!err)
387336fbf1e5SJiri Pirko 		*changed = true;
387444bfa9c5SEric Dumazet 	return err;
387536fbf1e5SJiri Pirko }
387636fbf1e5SJiri Pirko 
387736fbf1e5SJiri Pirko static int rtnl_linkprop(int cmd, struct sk_buff *skb, struct nlmsghdr *nlh,
387836fbf1e5SJiri Pirko 			 struct netlink_ext_ack *extack)
387936fbf1e5SJiri Pirko {
388036fbf1e5SJiri Pirko 	struct net *net = sock_net(skb->sk);
388136fbf1e5SJiri Pirko 	struct nlattr *tb[IFLA_MAX + 1];
388236fbf1e5SJiri Pirko 	struct net_device *dev;
388336fbf1e5SJiri Pirko 	struct ifinfomsg *ifm;
388436fbf1e5SJiri Pirko 	bool changed = false;
388536fbf1e5SJiri Pirko 	struct nlattr *attr;
388636fbf1e5SJiri Pirko 	int err, rem;
388736fbf1e5SJiri Pirko 
388836fbf1e5SJiri Pirko 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
388936fbf1e5SJiri Pirko 	if (err)
389036fbf1e5SJiri Pirko 		return err;
389136fbf1e5SJiri Pirko 
389236fbf1e5SJiri Pirko 	err = rtnl_ensure_unique_netns(tb, extack, true);
389336fbf1e5SJiri Pirko 	if (err)
389436fbf1e5SJiri Pirko 		return err;
389536fbf1e5SJiri Pirko 
389636fbf1e5SJiri Pirko 	ifm = nlmsg_data(nlh);
3897cc6090e9SJiri Pirko 	if (ifm->ifi_index > 0)
389836fbf1e5SJiri Pirko 		dev = __dev_get_by_index(net, ifm->ifi_index);
389976c9ac0eSJiri Pirko 	else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
39005ea08b52SFlorent Fourcot 		dev = rtnl_dev_get(net, tb);
3901cc6090e9SJiri Pirko 	else
390236fbf1e5SJiri Pirko 		return -EINVAL;
390336fbf1e5SJiri Pirko 
390436fbf1e5SJiri Pirko 	if (!dev)
390536fbf1e5SJiri Pirko 		return -ENODEV;
390636fbf1e5SJiri Pirko 
390736fbf1e5SJiri Pirko 	if (!tb[IFLA_PROP_LIST])
390836fbf1e5SJiri Pirko 		return 0;
390936fbf1e5SJiri Pirko 
391036fbf1e5SJiri Pirko 	nla_for_each_nested(attr, tb[IFLA_PROP_LIST], rem) {
391136fbf1e5SJiri Pirko 		switch (nla_type(attr)) {
391236fbf1e5SJiri Pirko 		case IFLA_ALT_IFNAME:
391336fbf1e5SJiri Pirko 			err = rtnl_alt_ifname(cmd, dev, attr, &changed, extack);
391436fbf1e5SJiri Pirko 			if (err)
391536fbf1e5SJiri Pirko 				return err;
391636fbf1e5SJiri Pirko 			break;
391736fbf1e5SJiri Pirko 		}
391836fbf1e5SJiri Pirko 	}
391936fbf1e5SJiri Pirko 
392036fbf1e5SJiri Pirko 	if (changed)
392136fbf1e5SJiri Pirko 		netdev_state_change(dev);
392236fbf1e5SJiri Pirko 	return 0;
392336fbf1e5SJiri Pirko }
392436fbf1e5SJiri Pirko 
392536fbf1e5SJiri Pirko static int rtnl_newlinkprop(struct sk_buff *skb, struct nlmsghdr *nlh,
392636fbf1e5SJiri Pirko 			    struct netlink_ext_ack *extack)
392736fbf1e5SJiri Pirko {
392836fbf1e5SJiri Pirko 	return rtnl_linkprop(RTM_NEWLINKPROP, skb, nlh, extack);
392936fbf1e5SJiri Pirko }
393036fbf1e5SJiri Pirko 
393136fbf1e5SJiri Pirko static int rtnl_dellinkprop(struct sk_buff *skb, struct nlmsghdr *nlh,
393236fbf1e5SJiri Pirko 			    struct netlink_ext_ack *extack)
393336fbf1e5SJiri Pirko {
393436fbf1e5SJiri Pirko 	return rtnl_linkprop(RTM_DELLINKPROP, skb, nlh, extack);
393536fbf1e5SJiri Pirko }
393636fbf1e5SJiri Pirko 
3937ebfe3c51SDi Zhu static u32 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
3938c7ac8679SGreg Rose {
3939115c9b81SGreg Rose 	struct net *net = sock_net(skb->sk);
3940ebfe3c51SDi Zhu 	size_t min_ifinfo_dump_size = 0;
3941115c9b81SGreg Rose 	struct nlattr *tb[IFLA_MAX+1];
3942115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
3943ebfe3c51SDi Zhu 	struct net_device *dev;
3944e5eca6d4SMichal Schmidt 	int hdrlen;
3945115c9b81SGreg Rose 
3946e5eca6d4SMichal Schmidt 	/* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
3947e5eca6d4SMichal Schmidt 	hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
3948e5eca6d4SMichal Schmidt 		 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
3949e5eca6d4SMichal Schmidt 
39508cb08174SJohannes Berg 	if (nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
3951115c9b81SGreg Rose 		if (tb[IFLA_EXT_MASK])
3952115c9b81SGreg Rose 			ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3953a4b64fbeSEric Dumazet 	}
3954115c9b81SGreg Rose 
3955115c9b81SGreg Rose 	if (!ext_filter_mask)
3956115c9b81SGreg Rose 		return NLMSG_GOODSIZE;
3957115c9b81SGreg Rose 	/*
3958115c9b81SGreg Rose 	 * traverse the list of net devices and compute the minimum
3959115c9b81SGreg Rose 	 * buffer size based upon the filter mask.
3960115c9b81SGreg Rose 	 */
39616853dd48SFlorian Westphal 	rcu_read_lock();
39626853dd48SFlorian Westphal 	for_each_netdev_rcu(net, dev) {
3963ebfe3c51SDi Zhu 		min_ifinfo_dump_size = max(min_ifinfo_dump_size,
3964ebfe3c51SDi Zhu 					   if_nlmsg_size(dev, ext_filter_mask));
3965115c9b81SGreg Rose 	}
39666853dd48SFlorian Westphal 	rcu_read_unlock();
3967115c9b81SGreg Rose 
396893af2056SZhang Shengju 	return nlmsg_total_size(min_ifinfo_dump_size);
3969c7ac8679SGreg Rose }
3970c7ac8679SGreg Rose 
397142bad1daSAdrian Bunk static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
39721da177e4SLinus Torvalds {
39731da177e4SLinus Torvalds 	int idx;
39741da177e4SLinus Torvalds 	int s_idx = cb->family;
397587ccbb1fSChristian Brauner 	int type = cb->nlh->nlmsg_type - RTM_BASE;
3976c63586dcSDavid Ahern 	int ret = 0;
39771da177e4SLinus Torvalds 
39781da177e4SLinus Torvalds 	if (s_idx == 0)
39791da177e4SLinus Torvalds 		s_idx = 1;
39806853dd48SFlorian Westphal 
398125239ceeSPatrick McHardy 	for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
398251e13685SJakub Kicinski 		struct rtnl_link __rcu **tab;
3983addf9b90SFlorian Westphal 		struct rtnl_link *link;
39846853dd48SFlorian Westphal 		rtnl_dumpit_func dumpit;
39856853dd48SFlorian Westphal 
39861da177e4SLinus Torvalds 		if (idx < s_idx || idx == PF_PACKET)
39871da177e4SLinus Torvalds 			continue;
39886853dd48SFlorian Westphal 
3989addf9b90SFlorian Westphal 		if (type < 0 || type >= RTM_NR_MSGTYPES)
39901da177e4SLinus Torvalds 			continue;
39916853dd48SFlorian Westphal 
3992addf9b90SFlorian Westphal 		tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]);
3993addf9b90SFlorian Westphal 		if (!tab)
3994addf9b90SFlorian Westphal 			continue;
3995addf9b90SFlorian Westphal 
399651e13685SJakub Kicinski 		link = rcu_dereference_rtnl(tab[type]);
3997addf9b90SFlorian Westphal 		if (!link)
3998addf9b90SFlorian Westphal 			continue;
3999addf9b90SFlorian Westphal 
4000addf9b90SFlorian Westphal 		dumpit = link->dumpit;
40016853dd48SFlorian Westphal 		if (!dumpit)
40026853dd48SFlorian Westphal 			continue;
40036853dd48SFlorian Westphal 
40040465277fSNicolas Dichtel 		if (idx > s_idx) {
40051da177e4SLinus Torvalds 			memset(&cb->args[0], 0, sizeof(cb->args));
40060465277fSNicolas Dichtel 			cb->prev_seq = 0;
40070465277fSNicolas Dichtel 			cb->seq = 0;
40080465277fSNicolas Dichtel 		}
4009c63586dcSDavid Ahern 		ret = dumpit(skb, cb);
40105e1acb4aSAlexey Kodanev 		if (ret)
40111da177e4SLinus Torvalds 			break;
40121da177e4SLinus Torvalds 	}
40131da177e4SLinus Torvalds 	cb->family = idx;
40141da177e4SLinus Torvalds 
4015c63586dcSDavid Ahern 	return skb->len ? : ret;
40161da177e4SLinus Torvalds }
40171da177e4SLinus Torvalds 
4018395eea6cSMahesh Bandewar struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
40193d3ea5afSVlad Yasevich 				       unsigned int change,
402038e01b30SNicolas Dichtel 				       u32 event, gfp_t flags, int *new_nsid,
402159d3efd2SMartin Willi 				       int new_ifindex, u32 portid,
402259d3efd2SMartin Willi 				       const struct nlmsghdr *nlh)
40231da177e4SLinus Torvalds {
4024c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
40251da177e4SLinus Torvalds 	struct sk_buff *skb;
40260ec6d3f4SThomas Graf 	int err = -ENOBUFS;
402759d3efd2SMartin Willi 	u32 seq = 0;
40281da177e4SLinus Torvalds 
402950af5969Sluo penghao 	skb = nlmsg_new(if_nlmsg_size(dev, 0), flags);
40300ec6d3f4SThomas Graf 	if (skb == NULL)
40310ec6d3f4SThomas Graf 		goto errout;
40321da177e4SLinus Torvalds 
403359d3efd2SMartin Willi 	if (nlmsg_report(nlh))
403459d3efd2SMartin Willi 		seq = nlmsg_seq(nlh);
403559d3efd2SMartin Willi 	else
403659d3efd2SMartin Willi 		portid = 0;
403759d3efd2SMartin Willi 
403879e1ad14SJiri Benc 	err = rtnl_fill_ifinfo(skb, dev, dev_net(dev),
40391d997f10SHangbin Liu 			       type, portid, seq, change, 0, 0, event,
4040d4e4fdf9SGuillaume Nault 			       new_nsid, new_ifindex, -1, flags);
404126932566SPatrick McHardy 	if (err < 0) {
404226932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in if_nlmsg_size() */
404326932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
404426932566SPatrick McHardy 		kfree_skb(skb);
404526932566SPatrick McHardy 		goto errout;
404626932566SPatrick McHardy 	}
4047395eea6cSMahesh Bandewar 	return skb;
40480ec6d3f4SThomas Graf errout:
40490ec6d3f4SThomas Graf 	if (err < 0)
40504b3da706SEric W. Biederman 		rtnl_set_sk_err(net, RTNLGRP_LINK, err);
4051395eea6cSMahesh Bandewar 	return NULL;
4052395eea6cSMahesh Bandewar }
4053395eea6cSMahesh Bandewar 
40541d997f10SHangbin Liu void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags,
40551d997f10SHangbin Liu 		       u32 portid, const struct nlmsghdr *nlh)
4056395eea6cSMahesh Bandewar {
4057395eea6cSMahesh Bandewar 	struct net *net = dev_net(dev);
4058395eea6cSMahesh Bandewar 
40591d997f10SHangbin Liu 	rtnl_notify(skb, net, portid, RTNLGRP_LINK, nlh, flags);
4060395eea6cSMahesh Bandewar }
4061395eea6cSMahesh Bandewar 
40623d3ea5afSVlad Yasevich static void rtmsg_ifinfo_event(int type, struct net_device *dev,
40633d3ea5afSVlad Yasevich 			       unsigned int change, u32 event,
40641d997f10SHangbin Liu 			       gfp_t flags, int *new_nsid, int new_ifindex,
40651d997f10SHangbin Liu 			       u32 portid, const struct nlmsghdr *nlh)
4066395eea6cSMahesh Bandewar {
4067395eea6cSMahesh Bandewar 	struct sk_buff *skb;
4068395eea6cSMahesh Bandewar 
4069ed2a80abSNicolas Dichtel 	if (dev->reg_state != NETREG_REGISTERED)
4070ed2a80abSNicolas Dichtel 		return;
4071ed2a80abSNicolas Dichtel 
407238e01b30SNicolas Dichtel 	skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid,
407359d3efd2SMartin Willi 				     new_ifindex, portid, nlh);
4074395eea6cSMahesh Bandewar 	if (skb)
40751d997f10SHangbin Liu 		rtmsg_ifinfo_send(skb, dev, flags, portid, nlh);
40761da177e4SLinus Torvalds }
40773d3ea5afSVlad Yasevich 
40783d3ea5afSVlad Yasevich void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
40791d997f10SHangbin Liu 		  gfp_t flags, u32 portid, const struct nlmsghdr *nlh)
40803d3ea5afSVlad Yasevich {
408138e01b30SNicolas Dichtel 	rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
40821d997f10SHangbin Liu 			   NULL, 0, portid, nlh);
40833d3ea5afSVlad Yasevich }
40841da177e4SLinus Torvalds 
40856621dd29SNicolas Dichtel void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change,
408638e01b30SNicolas Dichtel 			 gfp_t flags, int *new_nsid, int new_ifindex)
40876621dd29SNicolas Dichtel {
40886621dd29SNicolas Dichtel 	rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
40891d997f10SHangbin Liu 			   new_nsid, new_ifindex, 0, NULL);
40906621dd29SNicolas Dichtel }
40916621dd29SNicolas Dichtel 
4092d83b0603SJohn Fastabend static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
4093d83b0603SJohn Fastabend 				   struct net_device *dev,
40941e53d5bbSHubert Sokolowski 				   u8 *addr, u16 vid, u32 pid, u32 seq,
40951c104a6bSNicolas Dichtel 				   int type, unsigned int flags,
4096b3379041SHubert Sokolowski 				   int nlflags, u16 ndm_state)
4097d83b0603SJohn Fastabend {
4098d83b0603SJohn Fastabend 	struct nlmsghdr *nlh;
4099d83b0603SJohn Fastabend 	struct ndmsg *ndm;
4100d83b0603SJohn Fastabend 
41011c104a6bSNicolas Dichtel 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
4102d83b0603SJohn Fastabend 	if (!nlh)
4103d83b0603SJohn Fastabend 		return -EMSGSIZE;
4104d83b0603SJohn Fastabend 
4105d83b0603SJohn Fastabend 	ndm = nlmsg_data(nlh);
4106d83b0603SJohn Fastabend 	ndm->ndm_family  = AF_BRIDGE;
4107d83b0603SJohn Fastabend 	ndm->ndm_pad1	 = 0;
4108d83b0603SJohn Fastabend 	ndm->ndm_pad2    = 0;
4109d83b0603SJohn Fastabend 	ndm->ndm_flags	 = flags;
4110d83b0603SJohn Fastabend 	ndm->ndm_type	 = 0;
4111d83b0603SJohn Fastabend 	ndm->ndm_ifindex = dev->ifindex;
4112b3379041SHubert Sokolowski 	ndm->ndm_state   = ndm_state;
4113d83b0603SJohn Fastabend 
4114aa540695SEric Dumazet 	if (nla_put(skb, NDA_LLADDR, dev->addr_len, addr))
4115d83b0603SJohn Fastabend 		goto nla_put_failure;
41161e53d5bbSHubert Sokolowski 	if (vid)
41171e53d5bbSHubert Sokolowski 		if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
41181e53d5bbSHubert Sokolowski 			goto nla_put_failure;
4119d83b0603SJohn Fastabend 
4120053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
4121053c095aSJohannes Berg 	return 0;
4122d83b0603SJohn Fastabend 
4123d83b0603SJohn Fastabend nla_put_failure:
4124d83b0603SJohn Fastabend 	nlmsg_cancel(skb, nlh);
4125d83b0603SJohn Fastabend 	return -EMSGSIZE;
4126d83b0603SJohn Fastabend }
4127d83b0603SJohn Fastabend 
4128aa540695SEric Dumazet static inline size_t rtnl_fdb_nlmsg_size(const struct net_device *dev)
41293ff661c3SJohn Fastabend {
4130f82ef3e1SSabrina Dubroca 	return NLMSG_ALIGN(sizeof(struct ndmsg)) +
4131aa540695SEric Dumazet 	       nla_total_size(dev->addr_len) +	/* NDA_LLADDR */
4132f82ef3e1SSabrina Dubroca 	       nla_total_size(sizeof(u16)) +	/* NDA_VLAN */
4133f82ef3e1SSabrina Dubroca 	       0;
41343ff661c3SJohn Fastabend }
41353ff661c3SJohn Fastabend 
4136b3379041SHubert Sokolowski static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
4137b3379041SHubert Sokolowski 			    u16 ndm_state)
41383ff661c3SJohn Fastabend {
41393ff661c3SJohn Fastabend 	struct net *net = dev_net(dev);
41403ff661c3SJohn Fastabend 	struct sk_buff *skb;
41413ff661c3SJohn Fastabend 	int err = -ENOBUFS;
41423ff661c3SJohn Fastabend 
4143aa540695SEric Dumazet 	skb = nlmsg_new(rtnl_fdb_nlmsg_size(dev), GFP_ATOMIC);
41443ff661c3SJohn Fastabend 	if (!skb)
41453ff661c3SJohn Fastabend 		goto errout;
41463ff661c3SJohn Fastabend 
41471e53d5bbSHubert Sokolowski 	err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
4148b3379041SHubert Sokolowski 				      0, 0, type, NTF_SELF, 0, ndm_state);
41493ff661c3SJohn Fastabend 	if (err < 0) {
41503ff661c3SJohn Fastabend 		kfree_skb(skb);
41513ff661c3SJohn Fastabend 		goto errout;
41523ff661c3SJohn Fastabend 	}
41533ff661c3SJohn Fastabend 
41543ff661c3SJohn Fastabend 	rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
41553ff661c3SJohn Fastabend 	return;
41563ff661c3SJohn Fastabend errout:
41573ff661c3SJohn Fastabend 	rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
41583ff661c3SJohn Fastabend }
41593ff661c3SJohn Fastabend 
4160a986967eSBart Van Assche /*
4161090096bfSVlad Yasevich  * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
4162090096bfSVlad Yasevich  */
4163090096bfSVlad Yasevich int ndo_dflt_fdb_add(struct ndmsg *ndm,
4164090096bfSVlad Yasevich 		     struct nlattr *tb[],
4165090096bfSVlad Yasevich 		     struct net_device *dev,
4166f6f6424bSJiri Pirko 		     const unsigned char *addr, u16 vid,
4167090096bfSVlad Yasevich 		     u16 flags)
4168090096bfSVlad Yasevich {
4169090096bfSVlad Yasevich 	int err = -EINVAL;
4170090096bfSVlad Yasevich 
4171090096bfSVlad Yasevich 	/* If aging addresses are supported device will need to
4172090096bfSVlad Yasevich 	 * implement its own handler for this.
4173090096bfSVlad Yasevich 	 */
4174090096bfSVlad Yasevich 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
417578ecc890SVladimir Oltean 		netdev_info(dev, "default FDB implementation only supports local addresses\n");
4176090096bfSVlad Yasevich 		return err;
4177090096bfSVlad Yasevich 	}
4178090096bfSVlad Yasevich 
4179a35ec8e3SHans J. Schultz 	if (tb[NDA_FLAGS_EXT]) {
4180a35ec8e3SHans J. Schultz 		netdev_info(dev, "invalid flags given to default FDB implementation\n");
4181a35ec8e3SHans J. Schultz 		return err;
4182a35ec8e3SHans J. Schultz 	}
4183a35ec8e3SHans J. Schultz 
418465891feaSOr Gerlitz 	if (vid) {
418523ac0b42SVladimir Oltean 		netdev_info(dev, "vlans aren't supported yet for dev_uc|mc_add()\n");
418665891feaSOr Gerlitz 		return err;
418765891feaSOr Gerlitz 	}
418865891feaSOr Gerlitz 
4189090096bfSVlad Yasevich 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
4190090096bfSVlad Yasevich 		err = dev_uc_add_excl(dev, addr);
4191090096bfSVlad Yasevich 	else if (is_multicast_ether_addr(addr))
4192090096bfSVlad Yasevich 		err = dev_mc_add_excl(dev, addr);
4193090096bfSVlad Yasevich 
4194090096bfSVlad Yasevich 	/* Only return duplicate errors if NLM_F_EXCL is set */
4195090096bfSVlad Yasevich 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
4196090096bfSVlad Yasevich 		err = 0;
4197090096bfSVlad Yasevich 
4198090096bfSVlad Yasevich 	return err;
4199090096bfSVlad Yasevich }
4200090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_add);
4201090096bfSVlad Yasevich 
4202b88d12e4SFlorian Westphal static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid,
4203b88d12e4SFlorian Westphal 			 struct netlink_ext_ack *extack)
4204f6f6424bSJiri Pirko {
4205f6f6424bSJiri Pirko 	u16 vid = 0;
4206f6f6424bSJiri Pirko 
4207f6f6424bSJiri Pirko 	if (vlan_attr) {
4208f6f6424bSJiri Pirko 		if (nla_len(vlan_attr) != sizeof(u16)) {
4209b88d12e4SFlorian Westphal 			NL_SET_ERR_MSG(extack, "invalid vlan attribute size");
4210f6f6424bSJiri Pirko 			return -EINVAL;
4211f6f6424bSJiri Pirko 		}
4212f6f6424bSJiri Pirko 
4213f6f6424bSJiri Pirko 		vid = nla_get_u16(vlan_attr);
4214f6f6424bSJiri Pirko 
4215f6f6424bSJiri Pirko 		if (!vid || vid >= VLAN_VID_MASK) {
4216b88d12e4SFlorian Westphal 			NL_SET_ERR_MSG(extack, "invalid vlan id");
4217f6f6424bSJiri Pirko 			return -EINVAL;
4218f6f6424bSJiri Pirko 		}
4219f6f6424bSJiri Pirko 	}
4220f6f6424bSJiri Pirko 	*p_vid = vid;
4221f6f6424bSJiri Pirko 	return 0;
4222f6f6424bSJiri Pirko }
4223f6f6424bSJiri Pirko 
4224c21ef3e3SDavid Ahern static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
4225c21ef3e3SDavid Ahern 			struct netlink_ext_ack *extack)
422677162022SJohn Fastabend {
422777162022SJohn Fastabend 	struct net *net = sock_net(skb->sk);
422877162022SJohn Fastabend 	struct ndmsg *ndm;
422977162022SJohn Fastabend 	struct nlattr *tb[NDA_MAX+1];
423077162022SJohn Fastabend 	struct net_device *dev;
423177162022SJohn Fastabend 	u8 *addr;
4232f6f6424bSJiri Pirko 	u16 vid;
423377162022SJohn Fastabend 	int err;
423477162022SJohn Fastabend 
42358cb08174SJohannes Berg 	err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL,
42368cb08174SJohannes Berg 				     extack);
423777162022SJohn Fastabend 	if (err < 0)
423877162022SJohn Fastabend 		return err;
423977162022SJohn Fastabend 
424077162022SJohn Fastabend 	ndm = nlmsg_data(nlh);
424177162022SJohn Fastabend 	if (ndm->ndm_ifindex == 0) {
4242b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "invalid ifindex");
424377162022SJohn Fastabend 		return -EINVAL;
424477162022SJohn Fastabend 	}
424577162022SJohn Fastabend 
424677162022SJohn Fastabend 	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
424777162022SJohn Fastabend 	if (dev == NULL) {
4248b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "unknown ifindex");
424977162022SJohn Fastabend 		return -ENODEV;
425077162022SJohn Fastabend 	}
425177162022SJohn Fastabend 
425277162022SJohn Fastabend 	if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
4253b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "invalid address");
425477162022SJohn Fastabend 		return -EINVAL;
425577162022SJohn Fastabend 	}
425677162022SJohn Fastabend 
4257da715775SIdo Schimmel 	if (dev->type != ARPHRD_ETHER) {
4258da715775SIdo Schimmel 		NL_SET_ERR_MSG(extack, "FDB add only supported for Ethernet devices");
4259da715775SIdo Schimmel 		return -EINVAL;
4260da715775SIdo Schimmel 	}
4261da715775SIdo Schimmel 
426277162022SJohn Fastabend 	addr = nla_data(tb[NDA_LLADDR]);
426377162022SJohn Fastabend 
4264b88d12e4SFlorian Westphal 	err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
4265f6f6424bSJiri Pirko 	if (err)
4266f6f6424bSJiri Pirko 		return err;
4267f6f6424bSJiri Pirko 
426877162022SJohn Fastabend 	err = -EOPNOTSUPP;
426977162022SJohn Fastabend 
427077162022SJohn Fastabend 	/* Support fdb on master device the net/bridge default case */
427177162022SJohn Fastabend 	if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
42722e92a2d0SJulian Wiedmann 	    netif_is_bridge_port(dev)) {
4273898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4274898e5061SJiri Pirko 		const struct net_device_ops *ops = br_dev->netdev_ops;
4275898e5061SJiri Pirko 
4276f6f6424bSJiri Pirko 		err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
427787b0984eSPetr Machata 				       nlh->nlmsg_flags, extack);
427877162022SJohn Fastabend 		if (err)
427977162022SJohn Fastabend 			goto out;
428077162022SJohn Fastabend 		else
428177162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_MASTER;
428277162022SJohn Fastabend 	}
428377162022SJohn Fastabend 
428477162022SJohn Fastabend 	/* Embedded bridge, macvlan, and any other device support */
4285090096bfSVlad Yasevich 	if ((ndm->ndm_flags & NTF_SELF)) {
4286090096bfSVlad Yasevich 		if (dev->netdev_ops->ndo_fdb_add)
4287090096bfSVlad Yasevich 			err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
4288f6f6424bSJiri Pirko 							   vid,
428987b0984eSPetr Machata 							   nlh->nlmsg_flags,
429087b0984eSPetr Machata 							   extack);
4291090096bfSVlad Yasevich 		else
4292f6f6424bSJiri Pirko 			err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
429377162022SJohn Fastabend 					       nlh->nlmsg_flags);
429477162022SJohn Fastabend 
42953ff661c3SJohn Fastabend 		if (!err) {
4296b3379041SHubert Sokolowski 			rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
4297b3379041SHubert Sokolowski 					ndm->ndm_state);
429877162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_SELF;
429977162022SJohn Fastabend 		}
43003ff661c3SJohn Fastabend 	}
430177162022SJohn Fastabend out:
430277162022SJohn Fastabend 	return err;
430377162022SJohn Fastabend }
430477162022SJohn Fastabend 
4305a986967eSBart Van Assche /*
4306090096bfSVlad Yasevich  * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
4307090096bfSVlad Yasevich  */
4308090096bfSVlad Yasevich int ndo_dflt_fdb_del(struct ndmsg *ndm,
4309090096bfSVlad Yasevich 		     struct nlattr *tb[],
4310090096bfSVlad Yasevich 		     struct net_device *dev,
4311f6f6424bSJiri Pirko 		     const unsigned char *addr, u16 vid)
4312090096bfSVlad Yasevich {
4313c8a89c4aSAlexander Duyck 	int err = -EINVAL;
4314090096bfSVlad Yasevich 
4315090096bfSVlad Yasevich 	/* If aging addresses are supported device will need to
4316090096bfSVlad Yasevich 	 * implement its own handler for this.
4317090096bfSVlad Yasevich 	 */
431864535993SSridhar Samudrala 	if (!(ndm->ndm_state & NUD_PERMANENT)) {
431978ecc890SVladimir Oltean 		netdev_info(dev, "default FDB implementation only supports local addresses\n");
4320c8a89c4aSAlexander Duyck 		return err;
4321090096bfSVlad Yasevich 	}
4322090096bfSVlad Yasevich 
4323090096bfSVlad Yasevich 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
4324090096bfSVlad Yasevich 		err = dev_uc_del(dev, addr);
4325090096bfSVlad Yasevich 	else if (is_multicast_ether_addr(addr))
4326090096bfSVlad Yasevich 		err = dev_mc_del(dev, addr);
4327090096bfSVlad Yasevich 
4328090096bfSVlad Yasevich 	return err;
4329090096bfSVlad Yasevich }
4330090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_del);
4331090096bfSVlad Yasevich 
43329e834259SNikolay Aleksandrov static const struct nla_policy fdb_del_bulk_policy[NDA_MAX + 1] = {
43339e834259SNikolay Aleksandrov 	[NDA_VLAN]	= { .type = NLA_U16 },
43349e834259SNikolay Aleksandrov 	[NDA_IFINDEX]	= NLA_POLICY_MIN(NLA_S32, 1),
4335ea2c0f9eSNikolay Aleksandrov 	[NDA_NDM_STATE_MASK]	= { .type = NLA_U16  },
4336ea2c0f9eSNikolay Aleksandrov 	[NDA_NDM_FLAGS_MASK]	= { .type = NLA_U8 },
43379e834259SNikolay Aleksandrov };
43389e834259SNikolay Aleksandrov 
4339c21ef3e3SDavid Ahern static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
4340c21ef3e3SDavid Ahern 			struct netlink_ext_ack *extack)
434177162022SJohn Fastabend {
43429e834259SNikolay Aleksandrov 	bool del_bulk = !!(nlh->nlmsg_flags & NLM_F_BULK);
434377162022SJohn Fastabend 	struct net *net = sock_net(skb->sk);
43449e834259SNikolay Aleksandrov 	const struct net_device_ops *ops;
434577162022SJohn Fastabend 	struct ndmsg *ndm;
43461690be63SVlad Yasevich 	struct nlattr *tb[NDA_MAX+1];
434777162022SJohn Fastabend 	struct net_device *dev;
43489e834259SNikolay Aleksandrov 	__u8 *addr = NULL;
43497d311801SColin Ian King 	int err;
4350f6f6424bSJiri Pirko 	u16 vid;
435177162022SJohn Fastabend 
435290f62cf3SEric W. Biederman 	if (!netlink_capable(skb, CAP_NET_ADMIN))
43531690be63SVlad Yasevich 		return -EPERM;
43541690be63SVlad Yasevich 
43559e834259SNikolay Aleksandrov 	if (!del_bulk) {
43569e834259SNikolay Aleksandrov 		err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX,
43579e834259SNikolay Aleksandrov 					     NULL, extack);
43589e834259SNikolay Aleksandrov 	} else {
43599e834259SNikolay Aleksandrov 		err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX,
43609e834259SNikolay Aleksandrov 				  fdb_del_bulk_policy, extack);
43619e834259SNikolay Aleksandrov 	}
43621690be63SVlad Yasevich 	if (err < 0)
43631690be63SVlad Yasevich 		return err;
436477162022SJohn Fastabend 
436577162022SJohn Fastabend 	ndm = nlmsg_data(nlh);
436677162022SJohn Fastabend 	if (ndm->ndm_ifindex == 0) {
4367b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "invalid ifindex");
436877162022SJohn Fastabend 		return -EINVAL;
436977162022SJohn Fastabend 	}
437077162022SJohn Fastabend 
437177162022SJohn Fastabend 	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
437277162022SJohn Fastabend 	if (dev == NULL) {
4373b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "unknown ifindex");
437477162022SJohn Fastabend 		return -ENODEV;
437577162022SJohn Fastabend 	}
437677162022SJohn Fastabend 
43779e834259SNikolay Aleksandrov 	if (!del_bulk) {
43781690be63SVlad Yasevich 		if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
4379b88d12e4SFlorian Westphal 			NL_SET_ERR_MSG(extack, "invalid address");
438077162022SJohn Fastabend 			return -EINVAL;
438177162022SJohn Fastabend 		}
43829e834259SNikolay Aleksandrov 		addr = nla_data(tb[NDA_LLADDR]);
43839e834259SNikolay Aleksandrov 	}
438477162022SJohn Fastabend 
4385da715775SIdo Schimmel 	if (dev->type != ARPHRD_ETHER) {
4386da715775SIdo Schimmel 		NL_SET_ERR_MSG(extack, "FDB delete only supported for Ethernet devices");
4387da715775SIdo Schimmel 		return -EINVAL;
4388da715775SIdo Schimmel 	}
4389da715775SIdo Schimmel 
4390b88d12e4SFlorian Westphal 	err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
4391f6f6424bSJiri Pirko 	if (err)
4392f6f6424bSJiri Pirko 		return err;
4393f6f6424bSJiri Pirko 
439477162022SJohn Fastabend 	err = -EOPNOTSUPP;
439577162022SJohn Fastabend 
439677162022SJohn Fastabend 	/* Support fdb on master device the net/bridge default case */
439777162022SJohn Fastabend 	if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
43982e92a2d0SJulian Wiedmann 	    netif_is_bridge_port(dev)) {
4399898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
440077162022SJohn Fastabend 
44019e834259SNikolay Aleksandrov 		ops = br_dev->netdev_ops;
44029e834259SNikolay Aleksandrov 		if (!del_bulk) {
4403898e5061SJiri Pirko 			if (ops->ndo_fdb_del)
4404ca4567f1SAlaa Mohamed 				err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid, extack);
44059e834259SNikolay Aleksandrov 		} else {
44069e834259SNikolay Aleksandrov 			if (ops->ndo_fdb_del_bulk)
44079e834259SNikolay Aleksandrov 				err = ops->ndo_fdb_del_bulk(ndm, tb, dev, vid,
44089e834259SNikolay Aleksandrov 							    extack);
44099e834259SNikolay Aleksandrov 		}
441077162022SJohn Fastabend 
441177162022SJohn Fastabend 		if (err)
441277162022SJohn Fastabend 			goto out;
441377162022SJohn Fastabend 		else
441477162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_MASTER;
441577162022SJohn Fastabend 	}
441677162022SJohn Fastabend 
441777162022SJohn Fastabend 	/* Embedded bridge, macvlan, and any other device support */
4418090096bfSVlad Yasevich 	if (ndm->ndm_flags & NTF_SELF) {
44199e834259SNikolay Aleksandrov 		ops = dev->netdev_ops;
44209e834259SNikolay Aleksandrov 		if (!del_bulk) {
44219e834259SNikolay Aleksandrov 			if (ops->ndo_fdb_del)
4422ca4567f1SAlaa Mohamed 				err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid, extack);
4423090096bfSVlad Yasevich 			else
4424f6f6424bSJiri Pirko 				err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
44259e834259SNikolay Aleksandrov 		} else {
44269e834259SNikolay Aleksandrov 			/* in case err was cleared by NTF_MASTER call */
44279e834259SNikolay Aleksandrov 			err = -EOPNOTSUPP;
44289e834259SNikolay Aleksandrov 			if (ops->ndo_fdb_del_bulk)
44299e834259SNikolay Aleksandrov 				err = ops->ndo_fdb_del_bulk(ndm, tb, dev, vid,
44309e834259SNikolay Aleksandrov 							    extack);
44319e834259SNikolay Aleksandrov 		}
443277162022SJohn Fastabend 
44333ff661c3SJohn Fastabend 		if (!err) {
44349e834259SNikolay Aleksandrov 			if (!del_bulk)
4435b3379041SHubert Sokolowski 				rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
4436b3379041SHubert Sokolowski 						ndm->ndm_state);
443777162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_SELF;
443877162022SJohn Fastabend 		}
44393ff661c3SJohn Fastabend 	}
444077162022SJohn Fastabend out:
444177162022SJohn Fastabend 	return err;
444277162022SJohn Fastabend }
444377162022SJohn Fastabend 
4444d83b0603SJohn Fastabend static int nlmsg_populate_fdb(struct sk_buff *skb,
4445d83b0603SJohn Fastabend 			      struct netlink_callback *cb,
4446d83b0603SJohn Fastabend 			      struct net_device *dev,
4447d83b0603SJohn Fastabend 			      int *idx,
4448d83b0603SJohn Fastabend 			      struct netdev_hw_addr_list *list)
4449d83b0603SJohn Fastabend {
4450d83b0603SJohn Fastabend 	struct netdev_hw_addr *ha;
4451d83b0603SJohn Fastabend 	int err;
445215e47304SEric W. Biederman 	u32 portid, seq;
4453d83b0603SJohn Fastabend 
445415e47304SEric W. Biederman 	portid = NETLINK_CB(cb->skb).portid;
4455d83b0603SJohn Fastabend 	seq = cb->nlh->nlmsg_seq;
4456d83b0603SJohn Fastabend 
4457d83b0603SJohn Fastabend 	list_for_each_entry(ha, &list->list, list) {
4458d297653dSRoopa Prabhu 		if (*idx < cb->args[2])
4459d83b0603SJohn Fastabend 			goto skip;
4460d83b0603SJohn Fastabend 
44611e53d5bbSHubert Sokolowski 		err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
4462a7a558feSJohn Fastabend 					      portid, seq,
44631c104a6bSNicolas Dichtel 					      RTM_NEWNEIGH, NTF_SELF,
4464b3379041SHubert Sokolowski 					      NLM_F_MULTI, NUD_PERMANENT);
4465d83b0603SJohn Fastabend 		if (err < 0)
4466d83b0603SJohn Fastabend 			return err;
4467d83b0603SJohn Fastabend skip:
4468d83b0603SJohn Fastabend 		*idx += 1;
4469d83b0603SJohn Fastabend 	}
4470d83b0603SJohn Fastabend 	return 0;
4471d83b0603SJohn Fastabend }
4472d83b0603SJohn Fastabend 
4473d83b0603SJohn Fastabend /**
44742c53040fSBen Hutchings  * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
4475a986967eSBart Van Assche  * @skb: socket buffer to store message in
4476a986967eSBart Van Assche  * @cb: netlink callback
4477d83b0603SJohn Fastabend  * @dev: netdevice
4478a986967eSBart Van Assche  * @filter_dev: ignored
4479a986967eSBart Van Assche  * @idx: the number of FDB table entries dumped is added to *@idx
4480d83b0603SJohn Fastabend  *
4481d83b0603SJohn Fastabend  * Default netdevice operation to dump the existing unicast address list.
448291f3e7b1SJohn Fastabend  * Returns number of addresses from list put in skb.
4483d83b0603SJohn Fastabend  */
4484d83b0603SJohn Fastabend int ndo_dflt_fdb_dump(struct sk_buff *skb,
4485d83b0603SJohn Fastabend 		      struct netlink_callback *cb,
4486d83b0603SJohn Fastabend 		      struct net_device *dev,
44875d5eacb3SJamal Hadi Salim 		      struct net_device *filter_dev,
4488d297653dSRoopa Prabhu 		      int *idx)
4489d83b0603SJohn Fastabend {
4490d83b0603SJohn Fastabend 	int err;
4491d83b0603SJohn Fastabend 
449268883893SEric Dumazet 	if (dev->type != ARPHRD_ETHER)
449368883893SEric Dumazet 		return -EINVAL;
449468883893SEric Dumazet 
4495d83b0603SJohn Fastabend 	netif_addr_lock_bh(dev);
4496d297653dSRoopa Prabhu 	err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
4497d83b0603SJohn Fastabend 	if (err)
4498d83b0603SJohn Fastabend 		goto out;
44992934c9dbSZhang Shengju 	err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
4500d83b0603SJohn Fastabend out:
4501d83b0603SJohn Fastabend 	netif_addr_unlock_bh(dev);
4502d297653dSRoopa Prabhu 	return err;
4503d83b0603SJohn Fastabend }
4504d83b0603SJohn Fastabend EXPORT_SYMBOL(ndo_dflt_fdb_dump);
4505d83b0603SJohn Fastabend 
45068c6e137fSDavid Ahern static int valid_fdb_dump_strict(const struct nlmsghdr *nlh,
45078c6e137fSDavid Ahern 				 int *br_idx, int *brport_idx,
45088c6e137fSDavid Ahern 				 struct netlink_ext_ack *extack)
45098c6e137fSDavid Ahern {
45108c6e137fSDavid Ahern 	struct nlattr *tb[NDA_MAX + 1];
45118c6e137fSDavid Ahern 	struct ndmsg *ndm;
45128c6e137fSDavid Ahern 	int err, i;
45138c6e137fSDavid Ahern 
45148c6e137fSDavid Ahern 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
45158c6e137fSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid header for fdb dump request");
45168c6e137fSDavid Ahern 		return -EINVAL;
45178c6e137fSDavid Ahern 	}
45188c6e137fSDavid Ahern 
45198c6e137fSDavid Ahern 	ndm = nlmsg_data(nlh);
45208c6e137fSDavid Ahern 	if (ndm->ndm_pad1  || ndm->ndm_pad2  || ndm->ndm_state ||
45218c6e137fSDavid Ahern 	    ndm->ndm_flags || ndm->ndm_type) {
45228b73018fSNikolay Aleksandrov 		NL_SET_ERR_MSG(extack, "Invalid values in header for fdb dump request");
45238c6e137fSDavid Ahern 		return -EINVAL;
45248c6e137fSDavid Ahern 	}
45258c6e137fSDavid Ahern 
45268cb08174SJohannes Berg 	err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
45278cb08174SJohannes Berg 					    NDA_MAX, NULL, extack);
45288c6e137fSDavid Ahern 	if (err < 0)
45298c6e137fSDavid Ahern 		return err;
45308c6e137fSDavid Ahern 
45318c6e137fSDavid Ahern 	*brport_idx = ndm->ndm_ifindex;
45328c6e137fSDavid Ahern 	for (i = 0; i <= NDA_MAX; ++i) {
45338c6e137fSDavid Ahern 		if (!tb[i])
45348c6e137fSDavid Ahern 			continue;
45358c6e137fSDavid Ahern 
45368c6e137fSDavid Ahern 		switch (i) {
45378c6e137fSDavid Ahern 		case NDA_IFINDEX:
45388c6e137fSDavid Ahern 			if (nla_len(tb[i]) != sizeof(u32)) {
45398c6e137fSDavid Ahern 				NL_SET_ERR_MSG(extack, "Invalid IFINDEX attribute in fdb dump request");
45408c6e137fSDavid Ahern 				return -EINVAL;
45418c6e137fSDavid Ahern 			}
45428c6e137fSDavid Ahern 			*brport_idx = nla_get_u32(tb[NDA_IFINDEX]);
45438c6e137fSDavid Ahern 			break;
45448c6e137fSDavid Ahern 		case NDA_MASTER:
45458c6e137fSDavid Ahern 			if (nla_len(tb[i]) != sizeof(u32)) {
45468c6e137fSDavid Ahern 				NL_SET_ERR_MSG(extack, "Invalid MASTER attribute in fdb dump request");
45478c6e137fSDavid Ahern 				return -EINVAL;
45488c6e137fSDavid Ahern 			}
45498c6e137fSDavid Ahern 			*br_idx = nla_get_u32(tb[NDA_MASTER]);
45508c6e137fSDavid Ahern 			break;
45518c6e137fSDavid Ahern 		default:
45528c6e137fSDavid Ahern 			NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb dump request");
45538c6e137fSDavid Ahern 			return -EINVAL;
45548c6e137fSDavid Ahern 		}
45558c6e137fSDavid Ahern 	}
45568c6e137fSDavid Ahern 
45578c6e137fSDavid Ahern 	return 0;
45588c6e137fSDavid Ahern }
45598c6e137fSDavid Ahern 
45608dfbda19SDavid Ahern static int valid_fdb_dump_legacy(const struct nlmsghdr *nlh,
45618dfbda19SDavid Ahern 				 int *br_idx, int *brport_idx,
45628dfbda19SDavid Ahern 				 struct netlink_ext_ack *extack)
45638dfbda19SDavid Ahern {
45648dfbda19SDavid Ahern 	struct nlattr *tb[IFLA_MAX+1];
45658dfbda19SDavid Ahern 	int err;
45668dfbda19SDavid Ahern 
45678dfbda19SDavid Ahern 	/* A hack to preserve kernel<->userspace interface.
45688dfbda19SDavid Ahern 	 * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0.
45698dfbda19SDavid Ahern 	 * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails.
45708dfbda19SDavid Ahern 	 * So, check for ndmsg with an optional u32 attribute (not used here).
45718dfbda19SDavid Ahern 	 * Fortunately these sizes don't conflict with the size of ifinfomsg
45728dfbda19SDavid Ahern 	 * with an optional attribute.
45738dfbda19SDavid Ahern 	 */
45748dfbda19SDavid Ahern 	if (nlmsg_len(nlh) != sizeof(struct ndmsg) &&
45758dfbda19SDavid Ahern 	    (nlmsg_len(nlh) != sizeof(struct ndmsg) +
45768dfbda19SDavid Ahern 	     nla_attr_size(sizeof(u32)))) {
45774565d7e5SDavid Ahern 		struct ifinfomsg *ifm;
45784565d7e5SDavid Ahern 
45798cb08174SJohannes Berg 		err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
45808cb08174SJohannes Berg 					     tb, IFLA_MAX, ifla_policy,
45818cb08174SJohannes Berg 					     extack);
45828dfbda19SDavid Ahern 		if (err < 0) {
45838dfbda19SDavid Ahern 			return -EINVAL;
45848dfbda19SDavid Ahern 		} else if (err == 0) {
45858dfbda19SDavid Ahern 			if (tb[IFLA_MASTER])
45868dfbda19SDavid Ahern 				*br_idx = nla_get_u32(tb[IFLA_MASTER]);
45878dfbda19SDavid Ahern 		}
45888dfbda19SDavid Ahern 
45894565d7e5SDavid Ahern 		ifm = nlmsg_data(nlh);
45908dfbda19SDavid Ahern 		*brport_idx = ifm->ifi_index;
45918dfbda19SDavid Ahern 	}
45928dfbda19SDavid Ahern 	return 0;
45938dfbda19SDavid Ahern }
45948dfbda19SDavid Ahern 
459577162022SJohn Fastabend static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
459677162022SJohn Fastabend {
459777162022SJohn Fastabend 	struct net_device *dev;
45985e6d2435SJamal Hadi Salim 	struct net_device *br_dev = NULL;
45995e6d2435SJamal Hadi Salim 	const struct net_device_ops *ops = NULL;
46005e6d2435SJamal Hadi Salim 	const struct net_device_ops *cops = NULL;
46015e6d2435SJamal Hadi Salim 	struct net *net = sock_net(skb->sk);
4602d297653dSRoopa Prabhu 	struct hlist_head *head;
46035e6d2435SJamal Hadi Salim 	int brport_idx = 0;
46045e6d2435SJamal Hadi Salim 	int br_idx = 0;
4605d297653dSRoopa Prabhu 	int h, s_h;
4606d297653dSRoopa Prabhu 	int idx = 0, s_idx;
4607d297653dSRoopa Prabhu 	int err = 0;
4608d297653dSRoopa Prabhu 	int fidx = 0;
460977162022SJohn Fastabend 
46108c6e137fSDavid Ahern 	if (cb->strict_check)
46118c6e137fSDavid Ahern 		err = valid_fdb_dump_strict(cb->nlh, &br_idx, &brport_idx,
46128c6e137fSDavid Ahern 					    cb->extack);
46138c6e137fSDavid Ahern 	else
46148dfbda19SDavid Ahern 		err = valid_fdb_dump_legacy(cb->nlh, &br_idx, &brport_idx,
46158dfbda19SDavid Ahern 					    cb->extack);
46168dfbda19SDavid Ahern 	if (err < 0)
46178dfbda19SDavid Ahern 		return err;
46185e6d2435SJamal Hadi Salim 
46195e6d2435SJamal Hadi Salim 	if (br_idx) {
46205e6d2435SJamal Hadi Salim 		br_dev = __dev_get_by_index(net, br_idx);
46215e6d2435SJamal Hadi Salim 		if (!br_dev)
46225e6d2435SJamal Hadi Salim 			return -ENODEV;
46235e6d2435SJamal Hadi Salim 
4624898e5061SJiri Pirko 		ops = br_dev->netdev_ops;
46255e6d2435SJamal Hadi Salim 	}
46265e6d2435SJamal Hadi Salim 
4627d297653dSRoopa Prabhu 	s_h = cb->args[0];
4628d297653dSRoopa Prabhu 	s_idx = cb->args[1];
4629d297653dSRoopa Prabhu 
4630d297653dSRoopa Prabhu 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4631d297653dSRoopa Prabhu 		idx = 0;
4632d297653dSRoopa Prabhu 		head = &net->dev_index_head[h];
4633d297653dSRoopa Prabhu 		hlist_for_each_entry(dev, head, index_hlist) {
4634d297653dSRoopa Prabhu 
46355e6d2435SJamal Hadi Salim 			if (brport_idx && (dev->ifindex != brport_idx))
46365e6d2435SJamal Hadi Salim 				continue;
46375e6d2435SJamal Hadi Salim 
46385e6d2435SJamal Hadi Salim 			if (!br_idx) { /* user did not specify a specific bridge */
46392e92a2d0SJulian Wiedmann 				if (netif_is_bridge_port(dev)) {
46405e6d2435SJamal Hadi Salim 					br_dev = netdev_master_upper_dev_get(dev);
46415e6d2435SJamal Hadi Salim 					cops = br_dev->netdev_ops;
46425e6d2435SJamal Hadi Salim 				}
46435e6d2435SJamal Hadi Salim 			} else {
46445e6d2435SJamal Hadi Salim 				if (dev != br_dev &&
46452e92a2d0SJulian Wiedmann 				    !netif_is_bridge_port(dev))
46465e6d2435SJamal Hadi Salim 					continue;
46475e6d2435SJamal Hadi Salim 
46485e6d2435SJamal Hadi Salim 				if (br_dev != netdev_master_upper_dev_get(dev) &&
4649254ec036SKyungrok Chung 				    !netif_is_bridge_master(dev))
46505e6d2435SJamal Hadi Salim 					continue;
46515e6d2435SJamal Hadi Salim 				cops = ops;
46525e6d2435SJamal Hadi Salim 			}
46535e6d2435SJamal Hadi Salim 
4654d297653dSRoopa Prabhu 			if (idx < s_idx)
4655d297653dSRoopa Prabhu 				goto cont;
4656d297653dSRoopa Prabhu 
46572e92a2d0SJulian Wiedmann 			if (netif_is_bridge_port(dev)) {
4658d297653dSRoopa Prabhu 				if (cops && cops->ndo_fdb_dump) {
4659d297653dSRoopa Prabhu 					err = cops->ndo_fdb_dump(skb, cb,
4660d297653dSRoopa Prabhu 								br_dev, dev,
4661d297653dSRoopa Prabhu 								&fidx);
4662d297653dSRoopa Prabhu 					if (err == -EMSGSIZE)
4663d297653dSRoopa Prabhu 						goto out;
466477162022SJohn Fastabend 				}
4665d297653dSRoopa Prabhu 			}
466677162022SJohn Fastabend 
46675e6d2435SJamal Hadi Salim 			if (dev->netdev_ops->ndo_fdb_dump)
4668d297653dSRoopa Prabhu 				err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
4669d297653dSRoopa Prabhu 								    dev, NULL,
4670d297653dSRoopa Prabhu 								    &fidx);
46716cb69742SHubert Sokolowski 			else
4672d297653dSRoopa Prabhu 				err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
4673d297653dSRoopa Prabhu 							&fidx);
4674d297653dSRoopa Prabhu 			if (err == -EMSGSIZE)
4675d297653dSRoopa Prabhu 				goto out;
46765e6d2435SJamal Hadi Salim 
46775e6d2435SJamal Hadi Salim 			cops = NULL;
4678d297653dSRoopa Prabhu 
4679d297653dSRoopa Prabhu 			/* reset fdb offset to 0 for rest of the interfaces */
4680d297653dSRoopa Prabhu 			cb->args[2] = 0;
4681d297653dSRoopa Prabhu 			fidx = 0;
4682d297653dSRoopa Prabhu cont:
4683d297653dSRoopa Prabhu 			idx++;
4684d297653dSRoopa Prabhu 		}
468577162022SJohn Fastabend 	}
468677162022SJohn Fastabend 
4687d297653dSRoopa Prabhu out:
4688d297653dSRoopa Prabhu 	cb->args[0] = h;
4689d297653dSRoopa Prabhu 	cb->args[1] = idx;
4690d297653dSRoopa Prabhu 	cb->args[2] = fidx;
4691d297653dSRoopa Prabhu 
469277162022SJohn Fastabend 	return skb->len;
469377162022SJohn Fastabend }
469477162022SJohn Fastabend 
46955b2f94b2SRoopa Prabhu static int valid_fdb_get_strict(const struct nlmsghdr *nlh,
46965b2f94b2SRoopa Prabhu 				struct nlattr **tb, u8 *ndm_flags,
46975b2f94b2SRoopa Prabhu 				int *br_idx, int *brport_idx, u8 **addr,
46985b2f94b2SRoopa Prabhu 				u16 *vid, struct netlink_ext_ack *extack)
46995b2f94b2SRoopa Prabhu {
47005b2f94b2SRoopa Prabhu 	struct ndmsg *ndm;
47015b2f94b2SRoopa Prabhu 	int err, i;
47025b2f94b2SRoopa Prabhu 
47035b2f94b2SRoopa Prabhu 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
47045b2f94b2SRoopa Prabhu 		NL_SET_ERR_MSG(extack, "Invalid header for fdb get request");
47055b2f94b2SRoopa Prabhu 		return -EINVAL;
47065b2f94b2SRoopa Prabhu 	}
47075b2f94b2SRoopa Prabhu 
47085b2f94b2SRoopa Prabhu 	ndm = nlmsg_data(nlh);
47095b2f94b2SRoopa Prabhu 	if (ndm->ndm_pad1  || ndm->ndm_pad2  || ndm->ndm_state ||
47105b2f94b2SRoopa Prabhu 	    ndm->ndm_type) {
47115b2f94b2SRoopa Prabhu 		NL_SET_ERR_MSG(extack, "Invalid values in header for fdb get request");
47125b2f94b2SRoopa Prabhu 		return -EINVAL;
47135b2f94b2SRoopa Prabhu 	}
47145b2f94b2SRoopa Prabhu 
47155b2f94b2SRoopa Prabhu 	if (ndm->ndm_flags & ~(NTF_MASTER | NTF_SELF)) {
47165b2f94b2SRoopa Prabhu 		NL_SET_ERR_MSG(extack, "Invalid flags in header for fdb get request");
47175b2f94b2SRoopa Prabhu 		return -EINVAL;
47185b2f94b2SRoopa Prabhu 	}
47195b2f94b2SRoopa Prabhu 
47208cb08174SJohannes Berg 	err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
47218cb08174SJohannes Berg 					    NDA_MAX, nda_policy, extack);
47225b2f94b2SRoopa Prabhu 	if (err < 0)
47235b2f94b2SRoopa Prabhu 		return err;
47245b2f94b2SRoopa Prabhu 
47255b2f94b2SRoopa Prabhu 	*ndm_flags = ndm->ndm_flags;
47265b2f94b2SRoopa Prabhu 	*brport_idx = ndm->ndm_ifindex;
47275b2f94b2SRoopa Prabhu 	for (i = 0; i <= NDA_MAX; ++i) {
47285b2f94b2SRoopa Prabhu 		if (!tb[i])
47295b2f94b2SRoopa Prabhu 			continue;
47305b2f94b2SRoopa Prabhu 
47315b2f94b2SRoopa Prabhu 		switch (i) {
47325b2f94b2SRoopa Prabhu 		case NDA_MASTER:
47335b2f94b2SRoopa Prabhu 			*br_idx = nla_get_u32(tb[i]);
47345b2f94b2SRoopa Prabhu 			break;
47355b2f94b2SRoopa Prabhu 		case NDA_LLADDR:
47365b2f94b2SRoopa Prabhu 			if (nla_len(tb[i]) != ETH_ALEN) {
47375b2f94b2SRoopa Prabhu 				NL_SET_ERR_MSG(extack, "Invalid address in fdb get request");
47385b2f94b2SRoopa Prabhu 				return -EINVAL;
47395b2f94b2SRoopa Prabhu 			}
47405b2f94b2SRoopa Prabhu 			*addr = nla_data(tb[i]);
47415b2f94b2SRoopa Prabhu 			break;
47425b2f94b2SRoopa Prabhu 		case NDA_VLAN:
47435b2f94b2SRoopa Prabhu 			err = fdb_vid_parse(tb[i], vid, extack);
47445b2f94b2SRoopa Prabhu 			if (err)
47455b2f94b2SRoopa Prabhu 				return err;
47465b2f94b2SRoopa Prabhu 			break;
47475b2f94b2SRoopa Prabhu 		case NDA_VNI:
47485b2f94b2SRoopa Prabhu 			break;
47495b2f94b2SRoopa Prabhu 		default:
47505b2f94b2SRoopa Prabhu 			NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb get request");
47515b2f94b2SRoopa Prabhu 			return -EINVAL;
47525b2f94b2SRoopa Prabhu 		}
47535b2f94b2SRoopa Prabhu 	}
47545b2f94b2SRoopa Prabhu 
47555b2f94b2SRoopa Prabhu 	return 0;
47565b2f94b2SRoopa Prabhu }
47575b2f94b2SRoopa Prabhu 
47585b2f94b2SRoopa Prabhu static int rtnl_fdb_get(struct sk_buff *in_skb, struct nlmsghdr *nlh,
47595b2f94b2SRoopa Prabhu 			struct netlink_ext_ack *extack)
47605b2f94b2SRoopa Prabhu {
47615b2f94b2SRoopa Prabhu 	struct net_device *dev = NULL, *br_dev = NULL;
47625b2f94b2SRoopa Prabhu 	const struct net_device_ops *ops = NULL;
47635b2f94b2SRoopa Prabhu 	struct net *net = sock_net(in_skb->sk);
47645b2f94b2SRoopa Prabhu 	struct nlattr *tb[NDA_MAX + 1];
47655b2f94b2SRoopa Prabhu 	struct sk_buff *skb;
47665b2f94b2SRoopa Prabhu 	int brport_idx = 0;
47675b2f94b2SRoopa Prabhu 	u8 ndm_flags = 0;
47685b2f94b2SRoopa Prabhu 	int br_idx = 0;
47695b2f94b2SRoopa Prabhu 	u8 *addr = NULL;
47705b2f94b2SRoopa Prabhu 	u16 vid = 0;
47715b2f94b2SRoopa Prabhu 	int err;
47725b2f94b2SRoopa Prabhu 
47735b2f94b2SRoopa Prabhu 	err = valid_fdb_get_strict(nlh, tb, &ndm_flags, &br_idx,
47745b2f94b2SRoopa Prabhu 				   &brport_idx, &addr, &vid, extack);
47755b2f94b2SRoopa Prabhu 	if (err < 0)
47765b2f94b2SRoopa Prabhu 		return err;
47775b2f94b2SRoopa Prabhu 
4778f989d03eSNikolay Aleksandrov 	if (!addr) {
4779f989d03eSNikolay Aleksandrov 		NL_SET_ERR_MSG(extack, "Missing lookup address for fdb get request");
4780f989d03eSNikolay Aleksandrov 		return -EINVAL;
4781f989d03eSNikolay Aleksandrov 	}
4782f989d03eSNikolay Aleksandrov 
47835b2f94b2SRoopa Prabhu 	if (brport_idx) {
47845b2f94b2SRoopa Prabhu 		dev = __dev_get_by_index(net, brport_idx);
47855b2f94b2SRoopa Prabhu 		if (!dev) {
47865b2f94b2SRoopa Prabhu 			NL_SET_ERR_MSG(extack, "Unknown device ifindex");
47875b2f94b2SRoopa Prabhu 			return -ENODEV;
47885b2f94b2SRoopa Prabhu 		}
47895b2f94b2SRoopa Prabhu 	}
47905b2f94b2SRoopa Prabhu 
47915b2f94b2SRoopa Prabhu 	if (br_idx) {
47925b2f94b2SRoopa Prabhu 		if (dev) {
47935b2f94b2SRoopa Prabhu 			NL_SET_ERR_MSG(extack, "Master and device are mutually exclusive");
47945b2f94b2SRoopa Prabhu 			return -EINVAL;
47955b2f94b2SRoopa Prabhu 		}
47965b2f94b2SRoopa Prabhu 
47975b2f94b2SRoopa Prabhu 		br_dev = __dev_get_by_index(net, br_idx);
47985b2f94b2SRoopa Prabhu 		if (!br_dev) {
47995b2f94b2SRoopa Prabhu 			NL_SET_ERR_MSG(extack, "Invalid master ifindex");
48005b2f94b2SRoopa Prabhu 			return -EINVAL;
48015b2f94b2SRoopa Prabhu 		}
48025b2f94b2SRoopa Prabhu 		ops = br_dev->netdev_ops;
48035b2f94b2SRoopa Prabhu 	}
48045b2f94b2SRoopa Prabhu 
48055b2f94b2SRoopa Prabhu 	if (dev) {
48065b2f94b2SRoopa Prabhu 		if (!ndm_flags || (ndm_flags & NTF_MASTER)) {
48072e92a2d0SJulian Wiedmann 			if (!netif_is_bridge_port(dev)) {
48085b2f94b2SRoopa Prabhu 				NL_SET_ERR_MSG(extack, "Device is not a bridge port");
48095b2f94b2SRoopa Prabhu 				return -EINVAL;
48105b2f94b2SRoopa Prabhu 			}
48115b2f94b2SRoopa Prabhu 			br_dev = netdev_master_upper_dev_get(dev);
48125b2f94b2SRoopa Prabhu 			if (!br_dev) {
48135b2f94b2SRoopa Prabhu 				NL_SET_ERR_MSG(extack, "Master of device not found");
48145b2f94b2SRoopa Prabhu 				return -EINVAL;
48155b2f94b2SRoopa Prabhu 			}
48165b2f94b2SRoopa Prabhu 			ops = br_dev->netdev_ops;
48175b2f94b2SRoopa Prabhu 		} else {
48185b2f94b2SRoopa Prabhu 			if (!(ndm_flags & NTF_SELF)) {
48195b2f94b2SRoopa Prabhu 				NL_SET_ERR_MSG(extack, "Missing NTF_SELF");
48205b2f94b2SRoopa Prabhu 				return -EINVAL;
48215b2f94b2SRoopa Prabhu 			}
48225b2f94b2SRoopa Prabhu 			ops = dev->netdev_ops;
48235b2f94b2SRoopa Prabhu 		}
48245b2f94b2SRoopa Prabhu 	}
48255b2f94b2SRoopa Prabhu 
48265b2f94b2SRoopa Prabhu 	if (!br_dev && !dev) {
48275b2f94b2SRoopa Prabhu 		NL_SET_ERR_MSG(extack, "No device specified");
48285b2f94b2SRoopa Prabhu 		return -ENODEV;
48295b2f94b2SRoopa Prabhu 	}
48305b2f94b2SRoopa Prabhu 
48315b2f94b2SRoopa Prabhu 	if (!ops || !ops->ndo_fdb_get) {
48325b2f94b2SRoopa Prabhu 		NL_SET_ERR_MSG(extack, "Fdb get operation not supported by device");
48335b2f94b2SRoopa Prabhu 		return -EOPNOTSUPP;
48345b2f94b2SRoopa Prabhu 	}
48355b2f94b2SRoopa Prabhu 
48365b2f94b2SRoopa Prabhu 	skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
48375b2f94b2SRoopa Prabhu 	if (!skb)
48385b2f94b2SRoopa Prabhu 		return -ENOBUFS;
48395b2f94b2SRoopa Prabhu 
48405b2f94b2SRoopa Prabhu 	if (br_dev)
48415b2f94b2SRoopa Prabhu 		dev = br_dev;
48425b2f94b2SRoopa Prabhu 	err = ops->ndo_fdb_get(skb, tb, dev, addr, vid,
48435b2f94b2SRoopa Prabhu 			       NETLINK_CB(in_skb).portid,
48445b2f94b2SRoopa Prabhu 			       nlh->nlmsg_seq, extack);
48455b2f94b2SRoopa Prabhu 	if (err)
48465b2f94b2SRoopa Prabhu 		goto out;
48475b2f94b2SRoopa Prabhu 
48485b2f94b2SRoopa Prabhu 	return rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
48495b2f94b2SRoopa Prabhu out:
48505b2f94b2SRoopa Prabhu 	kfree_skb(skb);
48515b2f94b2SRoopa Prabhu 	return err;
48525b2f94b2SRoopa Prabhu }
48535b2f94b2SRoopa Prabhu 
48542c3c031cSScott Feldman static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
48552c3c031cSScott Feldman 			       unsigned int attrnum, unsigned int flag)
48562c3c031cSScott Feldman {
48572c3c031cSScott Feldman 	if (mask & flag)
48582c3c031cSScott Feldman 		return nla_put_u8(skb, attrnum, !!(flags & flag));
48592c3c031cSScott Feldman 	return 0;
48602c3c031cSScott Feldman }
48612c3c031cSScott Feldman 
4862815cccbfSJohn Fastabend int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
48632c3c031cSScott Feldman 			    struct net_device *dev, u16 mode,
48647d4f8d87SScott Feldman 			    u32 flags, u32 mask, int nlflags,
48657d4f8d87SScott Feldman 			    u32 filter_mask,
48667d4f8d87SScott Feldman 			    int (*vlan_fill)(struct sk_buff *skb,
48677d4f8d87SScott Feldman 					     struct net_device *dev,
48687d4f8d87SScott Feldman 					     u32 filter_mask))
4869815cccbfSJohn Fastabend {
4870815cccbfSJohn Fastabend 	struct nlmsghdr *nlh;
4871815cccbfSJohn Fastabend 	struct ifinfomsg *ifm;
4872815cccbfSJohn Fastabend 	struct nlattr *br_afspec;
48732c3c031cSScott Feldman 	struct nlattr *protinfo;
4874815cccbfSJohn Fastabend 	u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
4875898e5061SJiri Pirko 	struct net_device *br_dev = netdev_master_upper_dev_get(dev);
48767d4f8d87SScott Feldman 	int err = 0;
4877815cccbfSJohn Fastabend 
487846c264daSNicolas Dichtel 	nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
4879815cccbfSJohn Fastabend 	if (nlh == NULL)
4880815cccbfSJohn Fastabend 		return -EMSGSIZE;
4881815cccbfSJohn Fastabend 
4882815cccbfSJohn Fastabend 	ifm = nlmsg_data(nlh);
4883815cccbfSJohn Fastabend 	ifm->ifi_family = AF_BRIDGE;
4884815cccbfSJohn Fastabend 	ifm->__ifi_pad = 0;
4885815cccbfSJohn Fastabend 	ifm->ifi_type = dev->type;
4886815cccbfSJohn Fastabend 	ifm->ifi_index = dev->ifindex;
4887815cccbfSJohn Fastabend 	ifm->ifi_flags = dev_get_flags(dev);
4888815cccbfSJohn Fastabend 	ifm->ifi_change = 0;
4889815cccbfSJohn Fastabend 
4890815cccbfSJohn Fastabend 
4891815cccbfSJohn Fastabend 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
4892815cccbfSJohn Fastabend 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
4893815cccbfSJohn Fastabend 	    nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
4894898e5061SJiri Pirko 	    (br_dev &&
4895898e5061SJiri Pirko 	     nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
4896815cccbfSJohn Fastabend 	    (dev->addr_len &&
4897815cccbfSJohn Fastabend 	     nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
4898a54acb3aSNicolas Dichtel 	    (dev->ifindex != dev_get_iflink(dev) &&
4899a54acb3aSNicolas Dichtel 	     nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
4900815cccbfSJohn Fastabend 		goto nla_put_failure;
4901815cccbfSJohn Fastabend 
4902ae0be8deSMichal Kubecek 	br_afspec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
4903815cccbfSJohn Fastabend 	if (!br_afspec)
4904815cccbfSJohn Fastabend 		goto nla_put_failure;
4905815cccbfSJohn Fastabend 
49061d460b98SRoopa Prabhu 	if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
4907815cccbfSJohn Fastabend 		nla_nest_cancel(skb, br_afspec);
4908815cccbfSJohn Fastabend 		goto nla_put_failure;
4909815cccbfSJohn Fastabend 	}
49101d460b98SRoopa Prabhu 
49111d460b98SRoopa Prabhu 	if (mode != BRIDGE_MODE_UNDEF) {
49121d460b98SRoopa Prabhu 		if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
49131d460b98SRoopa Prabhu 			nla_nest_cancel(skb, br_afspec);
49141d460b98SRoopa Prabhu 			goto nla_put_failure;
49151d460b98SRoopa Prabhu 		}
49161d460b98SRoopa Prabhu 	}
49177d4f8d87SScott Feldman 	if (vlan_fill) {
49187d4f8d87SScott Feldman 		err = vlan_fill(skb, dev, filter_mask);
49197d4f8d87SScott Feldman 		if (err) {
49207d4f8d87SScott Feldman 			nla_nest_cancel(skb, br_afspec);
49217d4f8d87SScott Feldman 			goto nla_put_failure;
49227d4f8d87SScott Feldman 		}
49237d4f8d87SScott Feldman 	}
4924815cccbfSJohn Fastabend 	nla_nest_end(skb, br_afspec);
4925815cccbfSJohn Fastabend 
4926ae0be8deSMichal Kubecek 	protinfo = nla_nest_start(skb, IFLA_PROTINFO);
49272c3c031cSScott Feldman 	if (!protinfo)
49282c3c031cSScott Feldman 		goto nla_put_failure;
49292c3c031cSScott Feldman 
49302c3c031cSScott Feldman 	if (brport_nla_put_flag(skb, flags, mask,
49312c3c031cSScott Feldman 				IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
49322c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
49332c3c031cSScott Feldman 				IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
49342c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
49352c3c031cSScott Feldman 				IFLA_BRPORT_FAST_LEAVE,
49362c3c031cSScott Feldman 				BR_MULTICAST_FAST_LEAVE) ||
49372c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
49382c3c031cSScott Feldman 				IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
49392c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
49402c3c031cSScott Feldman 				IFLA_BRPORT_LEARNING, BR_LEARNING) ||
49412c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
49422c3c031cSScott Feldman 				IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
49432c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
49442c3c031cSScott Feldman 				IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
49452c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
4946583cb0b4SJulian Wiedmann 				IFLA_BRPORT_PROXYARP, BR_PROXYARP) ||
4947583cb0b4SJulian Wiedmann 	    brport_nla_put_flag(skb, flags, mask,
4948583cb0b4SJulian Wiedmann 				IFLA_BRPORT_MCAST_FLOOD, BR_MCAST_FLOOD) ||
4949583cb0b4SJulian Wiedmann 	    brport_nla_put_flag(skb, flags, mask,
4950583cb0b4SJulian Wiedmann 				IFLA_BRPORT_BCAST_FLOOD, BR_BCAST_FLOOD)) {
49512c3c031cSScott Feldman 		nla_nest_cancel(skb, protinfo);
49522c3c031cSScott Feldman 		goto nla_put_failure;
49532c3c031cSScott Feldman 	}
49542c3c031cSScott Feldman 
49552c3c031cSScott Feldman 	nla_nest_end(skb, protinfo);
49562c3c031cSScott Feldman 
4957053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
4958053c095aSJohannes Berg 	return 0;
4959815cccbfSJohn Fastabend nla_put_failure:
4960815cccbfSJohn Fastabend 	nlmsg_cancel(skb, nlh);
49617d4f8d87SScott Feldman 	return err ? err : -EMSGSIZE;
4962815cccbfSJohn Fastabend }
49637d4f8d87SScott Feldman EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
4964815cccbfSJohn Fastabend 
49652d011be8SDavid Ahern static int valid_bridge_getlink_req(const struct nlmsghdr *nlh,
49662d011be8SDavid Ahern 				    bool strict_check, u32 *filter_mask,
49672d011be8SDavid Ahern 				    struct netlink_ext_ack *extack)
49682d011be8SDavid Ahern {
49692d011be8SDavid Ahern 	struct nlattr *tb[IFLA_MAX+1];
49702d011be8SDavid Ahern 	int err, i;
49712d011be8SDavid Ahern 
49722d011be8SDavid Ahern 	if (strict_check) {
49732d011be8SDavid Ahern 		struct ifinfomsg *ifm;
49742d011be8SDavid Ahern 
49752d011be8SDavid Ahern 		if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
49762d011be8SDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid header for bridge link dump");
49772d011be8SDavid Ahern 			return -EINVAL;
49782d011be8SDavid Ahern 		}
49792d011be8SDavid Ahern 
49802d011be8SDavid Ahern 		ifm = nlmsg_data(nlh);
49812d011be8SDavid Ahern 		if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
49822d011be8SDavid Ahern 		    ifm->ifi_change || ifm->ifi_index) {
49832d011be8SDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid values in header for bridge link dump request");
49842d011be8SDavid Ahern 			return -EINVAL;
49852d011be8SDavid Ahern 		}
49862d011be8SDavid Ahern 
49878cb08174SJohannes Berg 		err = nlmsg_parse_deprecated_strict(nlh,
49888cb08174SJohannes Berg 						    sizeof(struct ifinfomsg),
49898cb08174SJohannes Berg 						    tb, IFLA_MAX, ifla_policy,
49908cb08174SJohannes Berg 						    extack);
49912d011be8SDavid Ahern 	} else {
49928cb08174SJohannes Berg 		err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
49938cb08174SJohannes Berg 					     tb, IFLA_MAX, ifla_policy,
49948cb08174SJohannes Berg 					     extack);
49952d011be8SDavid Ahern 	}
49962d011be8SDavid Ahern 	if (err < 0)
49972d011be8SDavid Ahern 		return err;
49982d011be8SDavid Ahern 
49992d011be8SDavid Ahern 	/* new attributes should only be added with strict checking */
50002d011be8SDavid Ahern 	for (i = 0; i <= IFLA_MAX; ++i) {
50012d011be8SDavid Ahern 		if (!tb[i])
50022d011be8SDavid Ahern 			continue;
50032d011be8SDavid Ahern 
50042d011be8SDavid Ahern 		switch (i) {
50052d011be8SDavid Ahern 		case IFLA_EXT_MASK:
50062d011be8SDavid Ahern 			*filter_mask = nla_get_u32(tb[i]);
50072d011be8SDavid Ahern 			break;
50082d011be8SDavid Ahern 		default:
50092d011be8SDavid Ahern 			if (strict_check) {
50102d011be8SDavid Ahern 				NL_SET_ERR_MSG(extack, "Unsupported attribute in bridge link dump request");
50112d011be8SDavid Ahern 				return -EINVAL;
50122d011be8SDavid Ahern 			}
50132d011be8SDavid Ahern 		}
50142d011be8SDavid Ahern 	}
50152d011be8SDavid Ahern 
50162d011be8SDavid Ahern 	return 0;
50172d011be8SDavid Ahern }
50182d011be8SDavid Ahern 
5019e5a55a89SJohn Fastabend static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
5020e5a55a89SJohn Fastabend {
50212d011be8SDavid Ahern 	const struct nlmsghdr *nlh = cb->nlh;
5022e5a55a89SJohn Fastabend 	struct net *net = sock_net(skb->sk);
5023e5a55a89SJohn Fastabend 	struct net_device *dev;
5024e5a55a89SJohn Fastabend 	int idx = 0;
5025e5a55a89SJohn Fastabend 	u32 portid = NETLINK_CB(cb->skb).portid;
50262d011be8SDavid Ahern 	u32 seq = nlh->nlmsg_seq;
50276cbdceebSVlad Yasevich 	u32 filter_mask = 0;
5028d64f69b0SRoopa Prabhu 	int err;
50296cbdceebSVlad Yasevich 
50302d011be8SDavid Ahern 	err = valid_bridge_getlink_req(nlh, cb->strict_check, &filter_mask,
50312d011be8SDavid Ahern 				       cb->extack);
50322d011be8SDavid Ahern 	if (err < 0 && cb->strict_check)
50332d011be8SDavid Ahern 		return err;
5034e5a55a89SJohn Fastabend 
5035e5a55a89SJohn Fastabend 	rcu_read_lock();
5036e5a55a89SJohn Fastabend 	for_each_netdev_rcu(net, dev) {
5037e5a55a89SJohn Fastabend 		const struct net_device_ops *ops = dev->netdev_ops;
5038898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
5039e5a55a89SJohn Fastabend 
5040898e5061SJiri Pirko 		if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
5041d64f69b0SRoopa Prabhu 			if (idx >= cb->args[0]) {
5042d64f69b0SRoopa Prabhu 				err = br_dev->netdev_ops->ndo_bridge_getlink(
5043d64f69b0SRoopa Prabhu 						skb, portid, seq, dev,
5044d64f69b0SRoopa Prabhu 						filter_mask, NLM_F_MULTI);
5045f6c5775fSDavid Ahern 				if (err < 0 && err != -EOPNOTSUPP) {
5046f6c5775fSDavid Ahern 					if (likely(skb->len))
5047e5a55a89SJohn Fastabend 						break;
5048f6c5775fSDavid Ahern 
5049f6c5775fSDavid Ahern 					goto out_err;
5050f6c5775fSDavid Ahern 				}
5051d64f69b0SRoopa Prabhu 			}
5052e5a55a89SJohn Fastabend 			idx++;
5053e5a55a89SJohn Fastabend 		}
5054e5a55a89SJohn Fastabend 
5055e5a55a89SJohn Fastabend 		if (ops->ndo_bridge_getlink) {
5056d64f69b0SRoopa Prabhu 			if (idx >= cb->args[0]) {
5057d64f69b0SRoopa Prabhu 				err = ops->ndo_bridge_getlink(skb, portid,
5058d64f69b0SRoopa Prabhu 							      seq, dev,
505946c264daSNicolas Dichtel 							      filter_mask,
5060d64f69b0SRoopa Prabhu 							      NLM_F_MULTI);
5061f6c5775fSDavid Ahern 				if (err < 0 && err != -EOPNOTSUPP) {
5062f6c5775fSDavid Ahern 					if (likely(skb->len))
5063e5a55a89SJohn Fastabend 						break;
5064f6c5775fSDavid Ahern 
5065f6c5775fSDavid Ahern 					goto out_err;
5066f6c5775fSDavid Ahern 				}
5067d64f69b0SRoopa Prabhu 			}
5068e5a55a89SJohn Fastabend 			idx++;
5069e5a55a89SJohn Fastabend 		}
5070e5a55a89SJohn Fastabend 	}
5071f6c5775fSDavid Ahern 	err = skb->len;
5072f6c5775fSDavid Ahern out_err:
5073e5a55a89SJohn Fastabend 	rcu_read_unlock();
5074e5a55a89SJohn Fastabend 	cb->args[0] = idx;
5075e5a55a89SJohn Fastabend 
5076f6c5775fSDavid Ahern 	return err;
5077e5a55a89SJohn Fastabend }
5078e5a55a89SJohn Fastabend 
50792469ffd7SJohn Fastabend static inline size_t bridge_nlmsg_size(void)
50802469ffd7SJohn Fastabend {
50812469ffd7SJohn Fastabend 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
50822469ffd7SJohn Fastabend 		+ nla_total_size(IFNAMSIZ)	/* IFLA_IFNAME */
50832469ffd7SJohn Fastabend 		+ nla_total_size(MAX_ADDR_LEN)	/* IFLA_ADDRESS */
50842469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_MASTER */
50852469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_MTU */
50862469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_LINK */
50872469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_OPERSTATE */
50882469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u8))	/* IFLA_PROTINFO */
50892469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(struct nlattr))	/* IFLA_AF_SPEC */
50902469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u16))	/* IFLA_BRIDGE_FLAGS */
50912469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u16));	/* IFLA_BRIDGE_MODE */
50922469ffd7SJohn Fastabend }
50932469ffd7SJohn Fastabend 
509402dba438SRoopa Prabhu static int rtnl_bridge_notify(struct net_device *dev)
50952469ffd7SJohn Fastabend {
50962469ffd7SJohn Fastabend 	struct net *net = dev_net(dev);
50972469ffd7SJohn Fastabend 	struct sk_buff *skb;
50982469ffd7SJohn Fastabend 	int err = -EOPNOTSUPP;
50992469ffd7SJohn Fastabend 
510002dba438SRoopa Prabhu 	if (!dev->netdev_ops->ndo_bridge_getlink)
510102dba438SRoopa Prabhu 		return 0;
510202dba438SRoopa Prabhu 
51032469ffd7SJohn Fastabend 	skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
51042469ffd7SJohn Fastabend 	if (!skb) {
51052469ffd7SJohn Fastabend 		err = -ENOMEM;
51062469ffd7SJohn Fastabend 		goto errout;
51072469ffd7SJohn Fastabend 	}
51082469ffd7SJohn Fastabend 
510946c264daSNicolas Dichtel 	err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
5110c38e01b8SJohn Fastabend 	if (err < 0)
5111c38e01b8SJohn Fastabend 		goto errout;
51122469ffd7SJohn Fastabend 
5113d2e381c4SIdo Schimmel 	/* Notification info is only filled for bridge ports, not the bridge
5114d2e381c4SIdo Schimmel 	 * device itself. Therefore, a zero notification length is valid and
5115d2e381c4SIdo Schimmel 	 * should not result in an error.
5116d2e381c4SIdo Schimmel 	 */
5117d2e381c4SIdo Schimmel 	if (!skb->len)
511859ccaaaaSRoopa Prabhu 		goto errout;
511959ccaaaaSRoopa Prabhu 
51202469ffd7SJohn Fastabend 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
51212469ffd7SJohn Fastabend 	return 0;
51222469ffd7SJohn Fastabend errout:
51232469ffd7SJohn Fastabend 	WARN_ON(err == -EMSGSIZE);
51242469ffd7SJohn Fastabend 	kfree_skb(skb);
512559ccaaaaSRoopa Prabhu 	if (err)
51262469ffd7SJohn Fastabend 		rtnl_set_sk_err(net, RTNLGRP_LINK, err);
51272469ffd7SJohn Fastabend 	return err;
51282469ffd7SJohn Fastabend }
51292469ffd7SJohn Fastabend 
5130c21ef3e3SDavid Ahern static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
5131c21ef3e3SDavid Ahern 			       struct netlink_ext_ack *extack)
5132e5a55a89SJohn Fastabend {
5133e5a55a89SJohn Fastabend 	struct net *net = sock_net(skb->sk);
5134e5a55a89SJohn Fastabend 	struct ifinfomsg *ifm;
5135e5a55a89SJohn Fastabend 	struct net_device *dev;
51362469ffd7SJohn Fastabend 	struct nlattr *br_spec, *attr = NULL;
51372469ffd7SJohn Fastabend 	int rem, err = -EOPNOTSUPP;
51384de8b413SRosen, Rami 	u16 flags = 0;
5139c38e01b8SJohn Fastabend 	bool have_flags = false;
5140e5a55a89SJohn Fastabend 
5141e5a55a89SJohn Fastabend 	if (nlmsg_len(nlh) < sizeof(*ifm))
5142e5a55a89SJohn Fastabend 		return -EINVAL;
5143e5a55a89SJohn Fastabend 
5144e5a55a89SJohn Fastabend 	ifm = nlmsg_data(nlh);
5145e5a55a89SJohn Fastabend 	if (ifm->ifi_family != AF_BRIDGE)
5146e5a55a89SJohn Fastabend 		return -EPFNOSUPPORT;
5147e5a55a89SJohn Fastabend 
5148e5a55a89SJohn Fastabend 	dev = __dev_get_by_index(net, ifm->ifi_index);
5149e5a55a89SJohn Fastabend 	if (!dev) {
5150b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "unknown ifindex");
5151e5a55a89SJohn Fastabend 		return -ENODEV;
5152e5a55a89SJohn Fastabend 	}
5153e5a55a89SJohn Fastabend 
51542469ffd7SJohn Fastabend 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
51552469ffd7SJohn Fastabend 	if (br_spec) {
51562469ffd7SJohn Fastabend 		nla_for_each_nested(attr, br_spec, rem) {
5157d73ef2d6SLin Ma 			if (nla_type(attr) == IFLA_BRIDGE_FLAGS && !have_flags) {
51586e8d1c55SThomas Graf 				if (nla_len(attr) < sizeof(flags))
51596e8d1c55SThomas Graf 					return -EINVAL;
51606e8d1c55SThomas Graf 
5161c38e01b8SJohn Fastabend 				have_flags = true;
51622469ffd7SJohn Fastabend 				flags = nla_get_u16(attr);
5163d73ef2d6SLin Ma 			}
5164d73ef2d6SLin Ma 
5165d73ef2d6SLin Ma 			if (nla_type(attr) == IFLA_BRIDGE_MODE) {
5166d73ef2d6SLin Ma 				if (nla_len(attr) < sizeof(u16))
5167d73ef2d6SLin Ma 					return -EINVAL;
51682469ffd7SJohn Fastabend 			}
51692469ffd7SJohn Fastabend 		}
51702469ffd7SJohn Fastabend 	}
51712469ffd7SJohn Fastabend 
51722469ffd7SJohn Fastabend 	if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
5173898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
5174898e5061SJiri Pirko 
5175898e5061SJiri Pirko 		if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
51762469ffd7SJohn Fastabend 			err = -EOPNOTSUPP;
5177e5a55a89SJohn Fastabend 			goto out;
5178e5a55a89SJohn Fastabend 		}
5179e5a55a89SJohn Fastabend 
51802fd527b7SPetr Machata 		err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags,
51812fd527b7SPetr Machata 							     extack);
51822469ffd7SJohn Fastabend 		if (err)
51832469ffd7SJohn Fastabend 			goto out;
51842469ffd7SJohn Fastabend 
51852469ffd7SJohn Fastabend 		flags &= ~BRIDGE_FLAGS_MASTER;
51862469ffd7SJohn Fastabend 	}
51872469ffd7SJohn Fastabend 
51882469ffd7SJohn Fastabend 	if ((flags & BRIDGE_FLAGS_SELF)) {
51892469ffd7SJohn Fastabend 		if (!dev->netdev_ops->ndo_bridge_setlink)
51902469ffd7SJohn Fastabend 			err = -EOPNOTSUPP;
51912469ffd7SJohn Fastabend 		else
5192add511b3SRoopa Prabhu 			err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
51932fd527b7SPetr Machata 								  flags,
51942fd527b7SPetr Machata 								  extack);
519502dba438SRoopa Prabhu 		if (!err) {
51962469ffd7SJohn Fastabend 			flags &= ~BRIDGE_FLAGS_SELF;
519702dba438SRoopa Prabhu 
519802dba438SRoopa Prabhu 			/* Generate event to notify upper layer of bridge
519902dba438SRoopa Prabhu 			 * change
520002dba438SRoopa Prabhu 			 */
520102dba438SRoopa Prabhu 			err = rtnl_bridge_notify(dev);
520202dba438SRoopa Prabhu 		}
52032469ffd7SJohn Fastabend 	}
52042469ffd7SJohn Fastabend 
5205c38e01b8SJohn Fastabend 	if (have_flags)
52062469ffd7SJohn Fastabend 		memcpy(nla_data(attr), &flags, sizeof(flags));
5207e5a55a89SJohn Fastabend out:
5208e5a55a89SJohn Fastabend 	return err;
5209e5a55a89SJohn Fastabend }
5210e5a55a89SJohn Fastabend 
5211c21ef3e3SDavid Ahern static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
5212c21ef3e3SDavid Ahern 			       struct netlink_ext_ack *extack)
5213407af329SVlad Yasevich {
5214407af329SVlad Yasevich 	struct net *net = sock_net(skb->sk);
5215407af329SVlad Yasevich 	struct ifinfomsg *ifm;
5216407af329SVlad Yasevich 	struct net_device *dev;
5217407af329SVlad Yasevich 	struct nlattr *br_spec, *attr = NULL;
5218407af329SVlad Yasevich 	int rem, err = -EOPNOTSUPP;
52194de8b413SRosen, Rami 	u16 flags = 0;
5220407af329SVlad Yasevich 	bool have_flags = false;
5221407af329SVlad Yasevich 
5222407af329SVlad Yasevich 	if (nlmsg_len(nlh) < sizeof(*ifm))
5223407af329SVlad Yasevich 		return -EINVAL;
5224407af329SVlad Yasevich 
5225407af329SVlad Yasevich 	ifm = nlmsg_data(nlh);
5226407af329SVlad Yasevich 	if (ifm->ifi_family != AF_BRIDGE)
5227407af329SVlad Yasevich 		return -EPFNOSUPPORT;
5228407af329SVlad Yasevich 
5229407af329SVlad Yasevich 	dev = __dev_get_by_index(net, ifm->ifi_index);
5230407af329SVlad Yasevich 	if (!dev) {
5231b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "unknown ifindex");
5232407af329SVlad Yasevich 		return -ENODEV;
5233407af329SVlad Yasevich 	}
5234407af329SVlad Yasevich 
5235407af329SVlad Yasevich 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
5236407af329SVlad Yasevich 	if (br_spec) {
5237407af329SVlad Yasevich 		nla_for_each_nested(attr, br_spec, rem) {
5238407af329SVlad Yasevich 			if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
52396e8d1c55SThomas Graf 				if (nla_len(attr) < sizeof(flags))
52406e8d1c55SThomas Graf 					return -EINVAL;
52416e8d1c55SThomas Graf 
5242407af329SVlad Yasevich 				have_flags = true;
5243407af329SVlad Yasevich 				flags = nla_get_u16(attr);
5244407af329SVlad Yasevich 				break;
5245407af329SVlad Yasevich 			}
5246407af329SVlad Yasevich 		}
5247407af329SVlad Yasevich 	}
5248407af329SVlad Yasevich 
5249407af329SVlad Yasevich 	if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
5250407af329SVlad Yasevich 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
5251407af329SVlad Yasevich 
5252407af329SVlad Yasevich 		if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
5253407af329SVlad Yasevich 			err = -EOPNOTSUPP;
5254407af329SVlad Yasevich 			goto out;
5255407af329SVlad Yasevich 		}
5256407af329SVlad Yasevich 
5257add511b3SRoopa Prabhu 		err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
5258407af329SVlad Yasevich 		if (err)
5259407af329SVlad Yasevich 			goto out;
5260407af329SVlad Yasevich 
5261407af329SVlad Yasevich 		flags &= ~BRIDGE_FLAGS_MASTER;
5262407af329SVlad Yasevich 	}
5263407af329SVlad Yasevich 
5264407af329SVlad Yasevich 	if ((flags & BRIDGE_FLAGS_SELF)) {
5265407af329SVlad Yasevich 		if (!dev->netdev_ops->ndo_bridge_dellink)
5266407af329SVlad Yasevich 			err = -EOPNOTSUPP;
5267407af329SVlad Yasevich 		else
5268add511b3SRoopa Prabhu 			err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
5269add511b3SRoopa Prabhu 								  flags);
5270407af329SVlad Yasevich 
527102dba438SRoopa Prabhu 		if (!err) {
5272407af329SVlad Yasevich 			flags &= ~BRIDGE_FLAGS_SELF;
527302dba438SRoopa Prabhu 
527402dba438SRoopa Prabhu 			/* Generate event to notify upper layer of bridge
527502dba438SRoopa Prabhu 			 * change
527602dba438SRoopa Prabhu 			 */
527702dba438SRoopa Prabhu 			err = rtnl_bridge_notify(dev);
527802dba438SRoopa Prabhu 		}
5279407af329SVlad Yasevich 	}
5280407af329SVlad Yasevich 
5281407af329SVlad Yasevich 	if (have_flags)
5282407af329SVlad Yasevich 		memcpy(nla_data(attr), &flags, sizeof(flags));
5283407af329SVlad Yasevich out:
5284407af329SVlad Yasevich 	return err;
5285407af329SVlad Yasevich }
5286407af329SVlad Yasevich 
5287e8872a25SNikolay Aleksandrov static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
5288e8872a25SNikolay Aleksandrov {
5289e8872a25SNikolay Aleksandrov 	return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
5290e8872a25SNikolay Aleksandrov 	       (!idxattr || idxattr == attrid);
5291e8872a25SNikolay Aleksandrov }
5292e8872a25SNikolay Aleksandrov 
5293f6e0fb81SPetr Machata static bool
5294f6e0fb81SPetr Machata rtnl_offload_xstats_have_ndo(const struct net_device *dev, int attr_id)
529569ae6ad2SNogah Frankel {
5296f6e0fb81SPetr Machata 	return dev->netdev_ops &&
5297f6e0fb81SPetr Machata 	       dev->netdev_ops->ndo_has_offload_stats &&
5298f6e0fb81SPetr Machata 	       dev->netdev_ops->ndo_get_offload_stats &&
5299f6e0fb81SPetr Machata 	       dev->netdev_ops->ndo_has_offload_stats(dev, attr_id);
530069ae6ad2SNogah Frankel }
530169ae6ad2SNogah Frankel 
5302f6e0fb81SPetr Machata static unsigned int
5303f6e0fb81SPetr Machata rtnl_offload_xstats_get_size_ndo(const struct net_device *dev, int attr_id)
5304f6e0fb81SPetr Machata {
5305f6e0fb81SPetr Machata 	return rtnl_offload_xstats_have_ndo(dev, attr_id) ?
5306f6e0fb81SPetr Machata 	       sizeof(struct rtnl_link_stats64) : 0;
5307f6e0fb81SPetr Machata }
5308f6e0fb81SPetr Machata 
5309f6e0fb81SPetr Machata static int
5310f6e0fb81SPetr Machata rtnl_offload_xstats_fill_ndo(struct net_device *dev, int attr_id,
5311f6e0fb81SPetr Machata 			     struct sk_buff *skb)
5312f6e0fb81SPetr Machata {
5313f6e0fb81SPetr Machata 	unsigned int size = rtnl_offload_xstats_get_size_ndo(dev, attr_id);
5314f6e0fb81SPetr Machata 	struct nlattr *attr = NULL;
5315f6e0fb81SPetr Machata 	void *attr_data;
5316f6e0fb81SPetr Machata 	int err;
5317f6e0fb81SPetr Machata 
5318f6e0fb81SPetr Machata 	if (!size)
5319f6e0fb81SPetr Machata 		return -ENODATA;
5320f6e0fb81SPetr Machata 
5321f6e0fb81SPetr Machata 	attr = nla_reserve_64bit(skb, attr_id, size,
5322f6e0fb81SPetr Machata 				 IFLA_OFFLOAD_XSTATS_UNSPEC);
5323f6e0fb81SPetr Machata 	if (!attr)
5324f6e0fb81SPetr Machata 		return -EMSGSIZE;
5325f6e0fb81SPetr Machata 
5326f6e0fb81SPetr Machata 	attr_data = nla_data(attr);
5327f6e0fb81SPetr Machata 	memset(attr_data, 0, size);
5328f6e0fb81SPetr Machata 
5329f6e0fb81SPetr Machata 	err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev, attr_data);
5330f6e0fb81SPetr Machata 	if (err)
5331f6e0fb81SPetr Machata 		return err;
5332f6e0fb81SPetr Machata 
533369ae6ad2SNogah Frankel 	return 0;
533469ae6ad2SNogah Frankel }
533569ae6ad2SNogah Frankel 
53360e7788fdSPetr Machata static unsigned int
53370e7788fdSPetr Machata rtnl_offload_xstats_get_size_stats(const struct net_device *dev,
53380e7788fdSPetr Machata 				   enum netdev_offload_xstats_type type)
53390e7788fdSPetr Machata {
53400e7788fdSPetr Machata 	bool enabled = netdev_offload_xstats_enabled(dev, type);
53410e7788fdSPetr Machata 
53420e7788fdSPetr Machata 	return enabled ? sizeof(struct rtnl_hw_stats64) : 0;
53430e7788fdSPetr Machata }
53440e7788fdSPetr Machata 
53450e7788fdSPetr Machata struct rtnl_offload_xstats_request_used {
53460e7788fdSPetr Machata 	bool request;
53470e7788fdSPetr Machata 	bool used;
53480e7788fdSPetr Machata };
53490e7788fdSPetr Machata 
53500e7788fdSPetr Machata static int
53510e7788fdSPetr Machata rtnl_offload_xstats_get_stats(struct net_device *dev,
53520e7788fdSPetr Machata 			      enum netdev_offload_xstats_type type,
53530e7788fdSPetr Machata 			      struct rtnl_offload_xstats_request_used *ru,
53540e7788fdSPetr Machata 			      struct rtnl_hw_stats64 *stats,
53550e7788fdSPetr Machata 			      struct netlink_ext_ack *extack)
53560e7788fdSPetr Machata {
53570e7788fdSPetr Machata 	bool request;
53580e7788fdSPetr Machata 	bool used;
53590e7788fdSPetr Machata 	int err;
53600e7788fdSPetr Machata 
53610e7788fdSPetr Machata 	request = netdev_offload_xstats_enabled(dev, type);
53620e7788fdSPetr Machata 	if (!request) {
53630e7788fdSPetr Machata 		used = false;
53640e7788fdSPetr Machata 		goto out;
53650e7788fdSPetr Machata 	}
53660e7788fdSPetr Machata 
53670e7788fdSPetr Machata 	err = netdev_offload_xstats_get(dev, type, stats, &used, extack);
53680e7788fdSPetr Machata 	if (err)
53690e7788fdSPetr Machata 		return err;
53700e7788fdSPetr Machata 
53710e7788fdSPetr Machata out:
53720e7788fdSPetr Machata 	if (ru) {
53730e7788fdSPetr Machata 		ru->request = request;
53740e7788fdSPetr Machata 		ru->used = used;
53750e7788fdSPetr Machata 	}
53760e7788fdSPetr Machata 	return 0;
53770e7788fdSPetr Machata }
53780e7788fdSPetr Machata 
53790e7788fdSPetr Machata static int
53800e7788fdSPetr Machata rtnl_offload_xstats_fill_hw_s_info_one(struct sk_buff *skb, int attr_id,
53810e7788fdSPetr Machata 				       struct rtnl_offload_xstats_request_used *ru)
53820e7788fdSPetr Machata {
53830e7788fdSPetr Machata 	struct nlattr *nest;
53840e7788fdSPetr Machata 
53850e7788fdSPetr Machata 	nest = nla_nest_start(skb, attr_id);
53860e7788fdSPetr Machata 	if (!nest)
53870e7788fdSPetr Machata 		return -EMSGSIZE;
53880e7788fdSPetr Machata 
53890e7788fdSPetr Machata 	if (nla_put_u8(skb, IFLA_OFFLOAD_XSTATS_HW_S_INFO_REQUEST, ru->request))
53900e7788fdSPetr Machata 		goto nla_put_failure;
53910e7788fdSPetr Machata 
53920e7788fdSPetr Machata 	if (nla_put_u8(skb, IFLA_OFFLOAD_XSTATS_HW_S_INFO_USED, ru->used))
53930e7788fdSPetr Machata 		goto nla_put_failure;
53940e7788fdSPetr Machata 
53950e7788fdSPetr Machata 	nla_nest_end(skb, nest);
53960e7788fdSPetr Machata 	return 0;
53970e7788fdSPetr Machata 
53980e7788fdSPetr Machata nla_put_failure:
53990e7788fdSPetr Machata 	nla_nest_cancel(skb, nest);
54000e7788fdSPetr Machata 	return -EMSGSIZE;
54010e7788fdSPetr Machata }
54020e7788fdSPetr Machata 
54030e7788fdSPetr Machata static int
54040e7788fdSPetr Machata rtnl_offload_xstats_fill_hw_s_info(struct sk_buff *skb, struct net_device *dev,
54050e7788fdSPetr Machata 				   struct netlink_ext_ack *extack)
54060e7788fdSPetr Machata {
54070e7788fdSPetr Machata 	enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3;
54080e7788fdSPetr Machata 	struct rtnl_offload_xstats_request_used ru_l3;
54090e7788fdSPetr Machata 	struct nlattr *nest;
54100e7788fdSPetr Machata 	int err;
54110e7788fdSPetr Machata 
54120e7788fdSPetr Machata 	err = rtnl_offload_xstats_get_stats(dev, t_l3, &ru_l3, NULL, extack);
54130e7788fdSPetr Machata 	if (err)
54140e7788fdSPetr Machata 		return err;
54150e7788fdSPetr Machata 
54160e7788fdSPetr Machata 	nest = nla_nest_start(skb, IFLA_OFFLOAD_XSTATS_HW_S_INFO);
54170e7788fdSPetr Machata 	if (!nest)
54180e7788fdSPetr Machata 		return -EMSGSIZE;
54190e7788fdSPetr Machata 
54200e7788fdSPetr Machata 	if (rtnl_offload_xstats_fill_hw_s_info_one(skb,
54210e7788fdSPetr Machata 						   IFLA_OFFLOAD_XSTATS_L3_STATS,
54220e7788fdSPetr Machata 						   &ru_l3))
54230e7788fdSPetr Machata 		goto nla_put_failure;
54240e7788fdSPetr Machata 
54250e7788fdSPetr Machata 	nla_nest_end(skb, nest);
54260e7788fdSPetr Machata 	return 0;
54270e7788fdSPetr Machata 
54280e7788fdSPetr Machata nla_put_failure:
54290e7788fdSPetr Machata 	nla_nest_cancel(skb, nest);
54300e7788fdSPetr Machata 	return -EMSGSIZE;
54310e7788fdSPetr Machata }
54320e7788fdSPetr Machata 
54336b524a1dSPetr Machata static int rtnl_offload_xstats_fill(struct sk_buff *skb, struct net_device *dev,
543405415bccSPetr Machata 				    int *prividx, u32 off_filter_mask,
543505415bccSPetr Machata 				    struct netlink_ext_ack *extack)
543669ae6ad2SNogah Frankel {
54370e7788fdSPetr Machata 	enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3;
54380e7788fdSPetr Machata 	int attr_id_hw_s_info = IFLA_OFFLOAD_XSTATS_HW_S_INFO;
54390e7788fdSPetr Machata 	int attr_id_l3_stats = IFLA_OFFLOAD_XSTATS_L3_STATS;
5440f6e0fb81SPetr Machata 	int attr_id_cpu_hit = IFLA_OFFLOAD_XSTATS_CPU_HIT;
5441f6e0fb81SPetr Machata 	bool have_data = false;
544269ae6ad2SNogah Frankel 	int err;
544369ae6ad2SNogah Frankel 
544446efc97bSPetr Machata 	if (*prividx <= attr_id_cpu_hit &&
544546efc97bSPetr Machata 	    (off_filter_mask &
544646efc97bSPetr Machata 	     IFLA_STATS_FILTER_BIT(attr_id_cpu_hit))) {
5447f6e0fb81SPetr Machata 		err = rtnl_offload_xstats_fill_ndo(dev, attr_id_cpu_hit, skb);
5448f6e0fb81SPetr Machata 		if (!err) {
5449f6e0fb81SPetr Machata 			have_data = true;
5450f6e0fb81SPetr Machata 		} else if (err != -ENODATA) {
5451f6e0fb81SPetr Machata 			*prividx = attr_id_cpu_hit;
5452f6e0fb81SPetr Machata 			return err;
5453f6e0fb81SPetr Machata 		}
545469ae6ad2SNogah Frankel 	}
545569ae6ad2SNogah Frankel 
54560e7788fdSPetr Machata 	if (*prividx <= attr_id_hw_s_info &&
54570e7788fdSPetr Machata 	    (off_filter_mask & IFLA_STATS_FILTER_BIT(attr_id_hw_s_info))) {
54580e7788fdSPetr Machata 		*prividx = attr_id_hw_s_info;
54590e7788fdSPetr Machata 
54600e7788fdSPetr Machata 		err = rtnl_offload_xstats_fill_hw_s_info(skb, dev, extack);
54610e7788fdSPetr Machata 		if (err)
54620e7788fdSPetr Machata 			return err;
54630e7788fdSPetr Machata 
54640e7788fdSPetr Machata 		have_data = true;
54650e7788fdSPetr Machata 		*prividx = 0;
54660e7788fdSPetr Machata 	}
54670e7788fdSPetr Machata 
54680e7788fdSPetr Machata 	if (*prividx <= attr_id_l3_stats &&
54690e7788fdSPetr Machata 	    (off_filter_mask & IFLA_STATS_FILTER_BIT(attr_id_l3_stats))) {
54700e7788fdSPetr Machata 		unsigned int size_l3;
54710e7788fdSPetr Machata 		struct nlattr *attr;
54720e7788fdSPetr Machata 
54730e7788fdSPetr Machata 		*prividx = attr_id_l3_stats;
54740e7788fdSPetr Machata 
54750e7788fdSPetr Machata 		size_l3 = rtnl_offload_xstats_get_size_stats(dev, t_l3);
547623cfe941SPetr Machata 		if (!size_l3)
547723cfe941SPetr Machata 			goto skip_l3_stats;
54780e7788fdSPetr Machata 		attr = nla_reserve_64bit(skb, attr_id_l3_stats, size_l3,
54790e7788fdSPetr Machata 					 IFLA_OFFLOAD_XSTATS_UNSPEC);
54800e7788fdSPetr Machata 		if (!attr)
54810e7788fdSPetr Machata 			return -EMSGSIZE;
54820e7788fdSPetr Machata 
54830e7788fdSPetr Machata 		err = rtnl_offload_xstats_get_stats(dev, t_l3, NULL,
54840e7788fdSPetr Machata 						    nla_data(attr), extack);
54850e7788fdSPetr Machata 		if (err)
54860e7788fdSPetr Machata 			return err;
54870e7788fdSPetr Machata 
54880e7788fdSPetr Machata 		have_data = true;
548923cfe941SPetr Machata skip_l3_stats:
54900e7788fdSPetr Machata 		*prividx = 0;
54910e7788fdSPetr Machata 	}
54920e7788fdSPetr Machata 
5493f6e0fb81SPetr Machata 	if (!have_data)
549469ae6ad2SNogah Frankel 		return -ENODATA;
549569ae6ad2SNogah Frankel 
549669ae6ad2SNogah Frankel 	*prividx = 0;
549769ae6ad2SNogah Frankel 	return 0;
549869ae6ad2SNogah Frankel }
549969ae6ad2SNogah Frankel 
55000e7788fdSPetr Machata static unsigned int
55010e7788fdSPetr Machata rtnl_offload_xstats_get_size_hw_s_info_one(const struct net_device *dev,
55020e7788fdSPetr Machata 					   enum netdev_offload_xstats_type type)
55030e7788fdSPetr Machata {
55040e7788fdSPetr Machata 	bool enabled = netdev_offload_xstats_enabled(dev, type);
55050e7788fdSPetr Machata 
55060e7788fdSPetr Machata 	return nla_total_size(0) +
55070e7788fdSPetr Machata 		/* IFLA_OFFLOAD_XSTATS_HW_S_INFO_REQUEST */
55080e7788fdSPetr Machata 		nla_total_size(sizeof(u8)) +
55090e7788fdSPetr Machata 		/* IFLA_OFFLOAD_XSTATS_HW_S_INFO_USED */
55100e7788fdSPetr Machata 		(enabled ? nla_total_size(sizeof(u8)) : 0) +
55110e7788fdSPetr Machata 		0;
55120e7788fdSPetr Machata }
55130e7788fdSPetr Machata 
55140e7788fdSPetr Machata static unsigned int
55150e7788fdSPetr Machata rtnl_offload_xstats_get_size_hw_s_info(const struct net_device *dev)
55160e7788fdSPetr Machata {
55170e7788fdSPetr Machata 	enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3;
55180e7788fdSPetr Machata 
55190e7788fdSPetr Machata 	return nla_total_size(0) +
55200e7788fdSPetr Machata 		/* IFLA_OFFLOAD_XSTATS_L3_STATS */
55210e7788fdSPetr Machata 		rtnl_offload_xstats_get_size_hw_s_info_one(dev, t_l3) +
55220e7788fdSPetr Machata 		0;
55230e7788fdSPetr Machata }
55240e7788fdSPetr Machata 
552546efc97bSPetr Machata static int rtnl_offload_xstats_get_size(const struct net_device *dev,
552646efc97bSPetr Machata 					u32 off_filter_mask)
552769ae6ad2SNogah Frankel {
55280e7788fdSPetr Machata 	enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3;
5529f6e0fb81SPetr Machata 	int attr_id_cpu_hit = IFLA_OFFLOAD_XSTATS_CPU_HIT;
553069ae6ad2SNogah Frankel 	int nla_size = 0;
553169ae6ad2SNogah Frankel 	int size;
553269ae6ad2SNogah Frankel 
553346efc97bSPetr Machata 	if (off_filter_mask &
553446efc97bSPetr Machata 	    IFLA_STATS_FILTER_BIT(attr_id_cpu_hit)) {
5535f6e0fb81SPetr Machata 		size = rtnl_offload_xstats_get_size_ndo(dev, attr_id_cpu_hit);
553669ae6ad2SNogah Frankel 		nla_size += nla_total_size_64bit(size);
553746efc97bSPetr Machata 	}
553869ae6ad2SNogah Frankel 
55390e7788fdSPetr Machata 	if (off_filter_mask &
55400e7788fdSPetr Machata 	    IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_HW_S_INFO))
55410e7788fdSPetr Machata 		nla_size += rtnl_offload_xstats_get_size_hw_s_info(dev);
55420e7788fdSPetr Machata 
55430e7788fdSPetr Machata 	if (off_filter_mask &
55440e7788fdSPetr Machata 	    IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_L3_STATS)) {
55450e7788fdSPetr Machata 		size = rtnl_offload_xstats_get_size_stats(dev, t_l3);
55460e7788fdSPetr Machata 		nla_size += nla_total_size_64bit(size);
55470e7788fdSPetr Machata 	}
55480e7788fdSPetr Machata 
554969ae6ad2SNogah Frankel 	if (nla_size != 0)
555069ae6ad2SNogah Frankel 		nla_size += nla_total_size(0);
555169ae6ad2SNogah Frankel 
555269ae6ad2SNogah Frankel 	return nla_size;
555369ae6ad2SNogah Frankel }
555469ae6ad2SNogah Frankel 
555546efc97bSPetr Machata struct rtnl_stats_dump_filters {
555646efc97bSPetr Machata 	/* mask[0] filters outer attributes. Then individual nests have their
555746efc97bSPetr Machata 	 * filtering mask at the index of the nested attribute.
555846efc97bSPetr Machata 	 */
555946efc97bSPetr Machata 	u32 mask[IFLA_STATS_MAX + 1];
556046efc97bSPetr Machata };
556146efc97bSPetr Machata 
556210c9ead9SRoopa Prabhu static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
556310c9ead9SRoopa Prabhu 			       int type, u32 pid, u32 seq, u32 change,
556446efc97bSPetr Machata 			       unsigned int flags,
556546efc97bSPetr Machata 			       const struct rtnl_stats_dump_filters *filters,
556605415bccSPetr Machata 			       int *idxattr, int *prividx,
556705415bccSPetr Machata 			       struct netlink_ext_ack *extack)
556810c9ead9SRoopa Prabhu {
556946efc97bSPetr Machata 	unsigned int filter_mask = filters->mask[0];
557010c9ead9SRoopa Prabhu 	struct if_stats_msg *ifsm;
557110c9ead9SRoopa Prabhu 	struct nlmsghdr *nlh;
557210c9ead9SRoopa Prabhu 	struct nlattr *attr;
5573e8872a25SNikolay Aleksandrov 	int s_prividx = *prividx;
557469ae6ad2SNogah Frankel 	int err;
557510c9ead9SRoopa Prabhu 
557610c9ead9SRoopa Prabhu 	ASSERT_RTNL();
557710c9ead9SRoopa Prabhu 
557810c9ead9SRoopa Prabhu 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
557910c9ead9SRoopa Prabhu 	if (!nlh)
558010c9ead9SRoopa Prabhu 		return -EMSGSIZE;
558110c9ead9SRoopa Prabhu 
558210c9ead9SRoopa Prabhu 	ifsm = nlmsg_data(nlh);
5583ce024f42SNikolay Aleksandrov 	ifsm->family = PF_UNSPEC;
5584ce024f42SNikolay Aleksandrov 	ifsm->pad1 = 0;
5585ce024f42SNikolay Aleksandrov 	ifsm->pad2 = 0;
558610c9ead9SRoopa Prabhu 	ifsm->ifindex = dev->ifindex;
558710c9ead9SRoopa Prabhu 	ifsm->filter_mask = filter_mask;
558810c9ead9SRoopa Prabhu 
5589e8872a25SNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
559010c9ead9SRoopa Prabhu 		struct rtnl_link_stats64 *sp;
559110c9ead9SRoopa Prabhu 
559258414d32SNicolas Dichtel 		attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
559358414d32SNicolas Dichtel 					 sizeof(struct rtnl_link_stats64),
559458414d32SNicolas Dichtel 					 IFLA_STATS_UNSPEC);
5595216e6906SPetr Machata 		if (!attr) {
5596216e6906SPetr Machata 			err = -EMSGSIZE;
559710c9ead9SRoopa Prabhu 			goto nla_put_failure;
5598216e6906SPetr Machata 		}
559910c9ead9SRoopa Prabhu 
560010c9ead9SRoopa Prabhu 		sp = nla_data(attr);
560110c9ead9SRoopa Prabhu 		dev_get_stats(dev, sp);
560210c9ead9SRoopa Prabhu 	}
560310c9ead9SRoopa Prabhu 
560497a47facSNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
560597a47facSNikolay Aleksandrov 		const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
560697a47facSNikolay Aleksandrov 
560797a47facSNikolay Aleksandrov 		if (ops && ops->fill_linkxstats) {
560897a47facSNikolay Aleksandrov 			*idxattr = IFLA_STATS_LINK_XSTATS;
5609ae0be8deSMichal Kubecek 			attr = nla_nest_start_noflag(skb,
561097a47facSNikolay Aleksandrov 						     IFLA_STATS_LINK_XSTATS);
5611216e6906SPetr Machata 			if (!attr) {
5612216e6906SPetr Machata 				err = -EMSGSIZE;
561397a47facSNikolay Aleksandrov 				goto nla_put_failure;
5614216e6906SPetr Machata 			}
561597a47facSNikolay Aleksandrov 
561680e73cc5SNikolay Aleksandrov 			err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
561780e73cc5SNikolay Aleksandrov 			nla_nest_end(skb, attr);
561880e73cc5SNikolay Aleksandrov 			if (err)
561980e73cc5SNikolay Aleksandrov 				goto nla_put_failure;
562080e73cc5SNikolay Aleksandrov 			*idxattr = 0;
562180e73cc5SNikolay Aleksandrov 		}
562280e73cc5SNikolay Aleksandrov 	}
562380e73cc5SNikolay Aleksandrov 
562480e73cc5SNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
562580e73cc5SNikolay Aleksandrov 			     *idxattr)) {
562680e73cc5SNikolay Aleksandrov 		const struct rtnl_link_ops *ops = NULL;
562780e73cc5SNikolay Aleksandrov 		const struct net_device *master;
562880e73cc5SNikolay Aleksandrov 
562980e73cc5SNikolay Aleksandrov 		master = netdev_master_upper_dev_get(dev);
563080e73cc5SNikolay Aleksandrov 		if (master)
563180e73cc5SNikolay Aleksandrov 			ops = master->rtnl_link_ops;
563280e73cc5SNikolay Aleksandrov 		if (ops && ops->fill_linkxstats) {
563380e73cc5SNikolay Aleksandrov 			*idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
5634ae0be8deSMichal Kubecek 			attr = nla_nest_start_noflag(skb,
563580e73cc5SNikolay Aleksandrov 						     IFLA_STATS_LINK_XSTATS_SLAVE);
5636216e6906SPetr Machata 			if (!attr) {
5637216e6906SPetr Machata 				err = -EMSGSIZE;
563880e73cc5SNikolay Aleksandrov 				goto nla_put_failure;
5639216e6906SPetr Machata 			}
564080e73cc5SNikolay Aleksandrov 
564180e73cc5SNikolay Aleksandrov 			err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
564297a47facSNikolay Aleksandrov 			nla_nest_end(skb, attr);
564397a47facSNikolay Aleksandrov 			if (err)
564497a47facSNikolay Aleksandrov 				goto nla_put_failure;
564597a47facSNikolay Aleksandrov 			*idxattr = 0;
564697a47facSNikolay Aleksandrov 		}
564797a47facSNikolay Aleksandrov 	}
564897a47facSNikolay Aleksandrov 
564969ae6ad2SNogah Frankel 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
565069ae6ad2SNogah Frankel 			     *idxattr)) {
565146efc97bSPetr Machata 		u32 off_filter_mask;
565246efc97bSPetr Machata 
565346efc97bSPetr Machata 		off_filter_mask = filters->mask[IFLA_STATS_LINK_OFFLOAD_XSTATS];
565469ae6ad2SNogah Frankel 		*idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
5655ae0be8deSMichal Kubecek 		attr = nla_nest_start_noflag(skb,
5656ae0be8deSMichal Kubecek 					     IFLA_STATS_LINK_OFFLOAD_XSTATS);
5657216e6906SPetr Machata 		if (!attr) {
5658216e6906SPetr Machata 			err = -EMSGSIZE;
565969ae6ad2SNogah Frankel 			goto nla_put_failure;
5660216e6906SPetr Machata 		}
566169ae6ad2SNogah Frankel 
566246efc97bSPetr Machata 		err = rtnl_offload_xstats_fill(skb, dev, prividx,
566305415bccSPetr Machata 					       off_filter_mask, extack);
566469ae6ad2SNogah Frankel 		if (err == -ENODATA)
566569ae6ad2SNogah Frankel 			nla_nest_cancel(skb, attr);
566669ae6ad2SNogah Frankel 		else
566769ae6ad2SNogah Frankel 			nla_nest_end(skb, attr);
566869ae6ad2SNogah Frankel 
566969ae6ad2SNogah Frankel 		if (err && err != -ENODATA)
567069ae6ad2SNogah Frankel 			goto nla_put_failure;
567169ae6ad2SNogah Frankel 		*idxattr = 0;
567269ae6ad2SNogah Frankel 	}
567369ae6ad2SNogah Frankel 
5674aefb4d4aSRobert Shearman 	if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
5675aefb4d4aSRobert Shearman 		struct rtnl_af_ops *af_ops;
5676aefb4d4aSRobert Shearman 
5677aefb4d4aSRobert Shearman 		*idxattr = IFLA_STATS_AF_SPEC;
5678ae0be8deSMichal Kubecek 		attr = nla_nest_start_noflag(skb, IFLA_STATS_AF_SPEC);
5679216e6906SPetr Machata 		if (!attr) {
5680216e6906SPetr Machata 			err = -EMSGSIZE;
5681aefb4d4aSRobert Shearman 			goto nla_put_failure;
5682216e6906SPetr Machata 		}
5683aefb4d4aSRobert Shearman 
56845fa85a09SFlorian Westphal 		rcu_read_lock();
56855fa85a09SFlorian Westphal 		list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
5686aefb4d4aSRobert Shearman 			if (af_ops->fill_stats_af) {
5687aefb4d4aSRobert Shearman 				struct nlattr *af;
5688aefb4d4aSRobert Shearman 
5689ae0be8deSMichal Kubecek 				af = nla_nest_start_noflag(skb,
5690ae0be8deSMichal Kubecek 							   af_ops->family);
56915fa85a09SFlorian Westphal 				if (!af) {
56925fa85a09SFlorian Westphal 					rcu_read_unlock();
569357d29a29STom Rix 					err = -EMSGSIZE;
5694aefb4d4aSRobert Shearman 					goto nla_put_failure;
56955fa85a09SFlorian Westphal 				}
5696aefb4d4aSRobert Shearman 				err = af_ops->fill_stats_af(skb, dev);
5697aefb4d4aSRobert Shearman 
56985fa85a09SFlorian Westphal 				if (err == -ENODATA) {
5699aefb4d4aSRobert Shearman 					nla_nest_cancel(skb, af);
57005fa85a09SFlorian Westphal 				} else if (err < 0) {
57015fa85a09SFlorian Westphal 					rcu_read_unlock();
5702aefb4d4aSRobert Shearman 					goto nla_put_failure;
57035fa85a09SFlorian Westphal 				}
5704aefb4d4aSRobert Shearman 
5705aefb4d4aSRobert Shearman 				nla_nest_end(skb, af);
5706aefb4d4aSRobert Shearman 			}
5707aefb4d4aSRobert Shearman 		}
57085fa85a09SFlorian Westphal 		rcu_read_unlock();
5709aefb4d4aSRobert Shearman 
5710aefb4d4aSRobert Shearman 		nla_nest_end(skb, attr);
5711aefb4d4aSRobert Shearman 
5712aefb4d4aSRobert Shearman 		*idxattr = 0;
5713aefb4d4aSRobert Shearman 	}
5714aefb4d4aSRobert Shearman 
571510c9ead9SRoopa Prabhu 	nlmsg_end(skb, nlh);
571610c9ead9SRoopa Prabhu 
571710c9ead9SRoopa Prabhu 	return 0;
571810c9ead9SRoopa Prabhu 
571910c9ead9SRoopa Prabhu nla_put_failure:
5720e8872a25SNikolay Aleksandrov 	/* not a multi message or no progress mean a real error */
5721e8872a25SNikolay Aleksandrov 	if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
572210c9ead9SRoopa Prabhu 		nlmsg_cancel(skb, nlh);
5723e8872a25SNikolay Aleksandrov 	else
5724e8872a25SNikolay Aleksandrov 		nlmsg_end(skb, nlh);
572510c9ead9SRoopa Prabhu 
5726216e6906SPetr Machata 	return err;
572710c9ead9SRoopa Prabhu }
572810c9ead9SRoopa Prabhu 
572910c9ead9SRoopa Prabhu static size_t if_nlmsg_stats_size(const struct net_device *dev,
573046efc97bSPetr Machata 				  const struct rtnl_stats_dump_filters *filters)
573110c9ead9SRoopa Prabhu {
5732d3436799SEric Dumazet 	size_t size = NLMSG_ALIGN(sizeof(struct if_stats_msg));
573346efc97bSPetr Machata 	unsigned int filter_mask = filters->mask[0];
573410c9ead9SRoopa Prabhu 
5735e8872a25SNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
573610c9ead9SRoopa Prabhu 		size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
573710c9ead9SRoopa Prabhu 
573897a47facSNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
573997a47facSNikolay Aleksandrov 		const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
574080e73cc5SNikolay Aleksandrov 		int attr = IFLA_STATS_LINK_XSTATS;
574197a47facSNikolay Aleksandrov 
574297a47facSNikolay Aleksandrov 		if (ops && ops->get_linkxstats_size) {
574380e73cc5SNikolay Aleksandrov 			size += nla_total_size(ops->get_linkxstats_size(dev,
574480e73cc5SNikolay Aleksandrov 									attr));
574597a47facSNikolay Aleksandrov 			/* for IFLA_STATS_LINK_XSTATS */
574697a47facSNikolay Aleksandrov 			size += nla_total_size(0);
574797a47facSNikolay Aleksandrov 		}
574897a47facSNikolay Aleksandrov 	}
574997a47facSNikolay Aleksandrov 
575080e73cc5SNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
575180e73cc5SNikolay Aleksandrov 		struct net_device *_dev = (struct net_device *)dev;
575280e73cc5SNikolay Aleksandrov 		const struct rtnl_link_ops *ops = NULL;
575380e73cc5SNikolay Aleksandrov 		const struct net_device *master;
575480e73cc5SNikolay Aleksandrov 
575580e73cc5SNikolay Aleksandrov 		/* netdev_master_upper_dev_get can't take const */
575680e73cc5SNikolay Aleksandrov 		master = netdev_master_upper_dev_get(_dev);
575780e73cc5SNikolay Aleksandrov 		if (master)
575880e73cc5SNikolay Aleksandrov 			ops = master->rtnl_link_ops;
575980e73cc5SNikolay Aleksandrov 		if (ops && ops->get_linkxstats_size) {
576080e73cc5SNikolay Aleksandrov 			int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
576180e73cc5SNikolay Aleksandrov 
576280e73cc5SNikolay Aleksandrov 			size += nla_total_size(ops->get_linkxstats_size(dev,
576380e73cc5SNikolay Aleksandrov 									attr));
576480e73cc5SNikolay Aleksandrov 			/* for IFLA_STATS_LINK_XSTATS_SLAVE */
576580e73cc5SNikolay Aleksandrov 			size += nla_total_size(0);
576680e73cc5SNikolay Aleksandrov 		}
576780e73cc5SNikolay Aleksandrov 	}
576880e73cc5SNikolay Aleksandrov 
576946efc97bSPetr Machata 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0)) {
577046efc97bSPetr Machata 		u32 off_filter_mask;
577146efc97bSPetr Machata 
577246efc97bSPetr Machata 		off_filter_mask = filters->mask[IFLA_STATS_LINK_OFFLOAD_XSTATS];
577346efc97bSPetr Machata 		size += rtnl_offload_xstats_get_size(dev, off_filter_mask);
577446efc97bSPetr Machata 	}
577569ae6ad2SNogah Frankel 
5776aefb4d4aSRobert Shearman 	if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
5777aefb4d4aSRobert Shearman 		struct rtnl_af_ops *af_ops;
5778aefb4d4aSRobert Shearman 
5779aefb4d4aSRobert Shearman 		/* for IFLA_STATS_AF_SPEC */
5780aefb4d4aSRobert Shearman 		size += nla_total_size(0);
5781aefb4d4aSRobert Shearman 
57825fa85a09SFlorian Westphal 		rcu_read_lock();
57835fa85a09SFlorian Westphal 		list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
5784aefb4d4aSRobert Shearman 			if (af_ops->get_stats_af_size) {
5785aefb4d4aSRobert Shearman 				size += nla_total_size(
5786aefb4d4aSRobert Shearman 					af_ops->get_stats_af_size(dev));
5787aefb4d4aSRobert Shearman 
5788aefb4d4aSRobert Shearman 				/* for AF_* */
5789aefb4d4aSRobert Shearman 				size += nla_total_size(0);
5790aefb4d4aSRobert Shearman 			}
5791aefb4d4aSRobert Shearman 		}
57925fa85a09SFlorian Westphal 		rcu_read_unlock();
5793aefb4d4aSRobert Shearman 	}
5794aefb4d4aSRobert Shearman 
579510c9ead9SRoopa Prabhu 	return size;
579610c9ead9SRoopa Prabhu }
579710c9ead9SRoopa Prabhu 
579846efc97bSPetr Machata #define RTNL_STATS_OFFLOAD_XSTATS_VALID ((1 << __IFLA_OFFLOAD_XSTATS_MAX) - 1)
579946efc97bSPetr Machata 
580046efc97bSPetr Machata static const struct nla_policy
580146efc97bSPetr Machata rtnl_stats_get_policy_filters[IFLA_STATS_MAX + 1] = {
580246efc97bSPetr Machata 	[IFLA_STATS_LINK_OFFLOAD_XSTATS] =
580346efc97bSPetr Machata 		    NLA_POLICY_MASK(NLA_U32, RTNL_STATS_OFFLOAD_XSTATS_VALID),
580446efc97bSPetr Machata };
580546efc97bSPetr Machata 
580646efc97bSPetr Machata static const struct nla_policy
580746efc97bSPetr Machata rtnl_stats_get_policy[IFLA_STATS_GETSET_MAX + 1] = {
580846efc97bSPetr Machata 	[IFLA_STATS_GET_FILTERS] =
580946efc97bSPetr Machata 		    NLA_POLICY_NESTED(rtnl_stats_get_policy_filters),
581046efc97bSPetr Machata };
581146efc97bSPetr Machata 
581203ba3566SPetr Machata static const struct nla_policy
581303ba3566SPetr Machata ifla_stats_set_policy[IFLA_STATS_GETSET_MAX + 1] = {
58145fd0b838SPetr Machata 	[IFLA_STATS_SET_OFFLOAD_XSTATS_L3_STATS] = NLA_POLICY_MAX(NLA_U8, 1),
581503ba3566SPetr Machata };
581603ba3566SPetr Machata 
581746efc97bSPetr Machata static int rtnl_stats_get_parse_filters(struct nlattr *ifla_filters,
581846efc97bSPetr Machata 					struct rtnl_stats_dump_filters *filters,
581946efc97bSPetr Machata 					struct netlink_ext_ack *extack)
582046efc97bSPetr Machata {
582146efc97bSPetr Machata 	struct nlattr *tb[IFLA_STATS_MAX + 1];
582246efc97bSPetr Machata 	int err;
582346efc97bSPetr Machata 	int at;
582446efc97bSPetr Machata 
582546efc97bSPetr Machata 	err = nla_parse_nested(tb, IFLA_STATS_MAX, ifla_filters,
582646efc97bSPetr Machata 			       rtnl_stats_get_policy_filters, extack);
582746efc97bSPetr Machata 	if (err < 0)
582846efc97bSPetr Machata 		return err;
582946efc97bSPetr Machata 
583046efc97bSPetr Machata 	for (at = 1; at <= IFLA_STATS_MAX; at++) {
583146efc97bSPetr Machata 		if (tb[at]) {
583246efc97bSPetr Machata 			if (!(filters->mask[0] & IFLA_STATS_FILTER_BIT(at))) {
583346efc97bSPetr Machata 				NL_SET_ERR_MSG(extack, "Filtered attribute not enabled in filter_mask");
583446efc97bSPetr Machata 				return -EINVAL;
583546efc97bSPetr Machata 			}
583646efc97bSPetr Machata 			filters->mask[at] = nla_get_u32(tb[at]);
583746efc97bSPetr Machata 		}
583846efc97bSPetr Machata 	}
583946efc97bSPetr Machata 
584046efc97bSPetr Machata 	return 0;
584146efc97bSPetr Machata }
584246efc97bSPetr Machata 
584346efc97bSPetr Machata static int rtnl_stats_get_parse(const struct nlmsghdr *nlh,
584446efc97bSPetr Machata 				u32 filter_mask,
584546efc97bSPetr Machata 				struct rtnl_stats_dump_filters *filters,
584646efc97bSPetr Machata 				struct netlink_ext_ack *extack)
584746efc97bSPetr Machata {
584846efc97bSPetr Machata 	struct nlattr *tb[IFLA_STATS_GETSET_MAX + 1];
584946efc97bSPetr Machata 	int err;
585046efc97bSPetr Machata 	int i;
585146efc97bSPetr Machata 
585246efc97bSPetr Machata 	filters->mask[0] = filter_mask;
585346efc97bSPetr Machata 	for (i = 1; i < ARRAY_SIZE(filters->mask); i++)
585446efc97bSPetr Machata 		filters->mask[i] = -1U;
585546efc97bSPetr Machata 
585646efc97bSPetr Machata 	err = nlmsg_parse(nlh, sizeof(struct if_stats_msg), tb,
585746efc97bSPetr Machata 			  IFLA_STATS_GETSET_MAX, rtnl_stats_get_policy, extack);
585846efc97bSPetr Machata 	if (err < 0)
585946efc97bSPetr Machata 		return err;
586046efc97bSPetr Machata 
586146efc97bSPetr Machata 	if (tb[IFLA_STATS_GET_FILTERS]) {
586246efc97bSPetr Machata 		err = rtnl_stats_get_parse_filters(tb[IFLA_STATS_GET_FILTERS],
586346efc97bSPetr Machata 						   filters, extack);
586446efc97bSPetr Machata 		if (err)
586546efc97bSPetr Machata 			return err;
586646efc97bSPetr Machata 	}
586746efc97bSPetr Machata 
586846efc97bSPetr Machata 	return 0;
586946efc97bSPetr Machata }
587046efc97bSPetr Machata 
587151bc860dSJakub Kicinski static int rtnl_valid_stats_req(const struct nlmsghdr *nlh, bool strict_check,
587251bc860dSJakub Kicinski 				bool is_dump, struct netlink_ext_ack *extack)
587351bc860dSJakub Kicinski {
587451bc860dSJakub Kicinski 	struct if_stats_msg *ifsm;
587551bc860dSJakub Kicinski 
587669f23a09SEric Dumazet 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifsm))) {
587751bc860dSJakub Kicinski 		NL_SET_ERR_MSG(extack, "Invalid header for stats dump");
587851bc860dSJakub Kicinski 		return -EINVAL;
587951bc860dSJakub Kicinski 	}
588051bc860dSJakub Kicinski 
588151bc860dSJakub Kicinski 	if (!strict_check)
588251bc860dSJakub Kicinski 		return 0;
588351bc860dSJakub Kicinski 
588451bc860dSJakub Kicinski 	ifsm = nlmsg_data(nlh);
588551bc860dSJakub Kicinski 
588651bc860dSJakub Kicinski 	/* only requests using strict checks can pass data to influence
588751bc860dSJakub Kicinski 	 * the dump. The legacy exception is filter_mask.
588851bc860dSJakub Kicinski 	 */
588951bc860dSJakub Kicinski 	if (ifsm->pad1 || ifsm->pad2 || (is_dump && ifsm->ifindex)) {
589051bc860dSJakub Kicinski 		NL_SET_ERR_MSG(extack, "Invalid values in header for stats dump request");
589151bc860dSJakub Kicinski 		return -EINVAL;
589251bc860dSJakub Kicinski 	}
58936300acb2SJakub Kicinski 	if (ifsm->filter_mask >= IFLA_STATS_FILTER_BIT(IFLA_STATS_MAX + 1)) {
58946300acb2SJakub Kicinski 		NL_SET_ERR_MSG(extack, "Invalid stats requested through filter mask");
58956300acb2SJakub Kicinski 		return -EINVAL;
58966300acb2SJakub Kicinski 	}
589751bc860dSJakub Kicinski 
589851bc860dSJakub Kicinski 	return 0;
589951bc860dSJakub Kicinski }
590051bc860dSJakub Kicinski 
5901c21ef3e3SDavid Ahern static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
5902c21ef3e3SDavid Ahern 			  struct netlink_ext_ack *extack)
590310c9ead9SRoopa Prabhu {
590446efc97bSPetr Machata 	struct rtnl_stats_dump_filters filters;
590510c9ead9SRoopa Prabhu 	struct net *net = sock_net(skb->sk);
590610c9ead9SRoopa Prabhu 	struct net_device *dev = NULL;
5907e8872a25SNikolay Aleksandrov 	int idxattr = 0, prividx = 0;
5908e8872a25SNikolay Aleksandrov 	struct if_stats_msg *ifsm;
590910c9ead9SRoopa Prabhu 	struct sk_buff *nskb;
591010c9ead9SRoopa Prabhu 	int err;
591110c9ead9SRoopa Prabhu 
591251bc860dSJakub Kicinski 	err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb),
591351bc860dSJakub Kicinski 				   false, extack);
591451bc860dSJakub Kicinski 	if (err)
591551bc860dSJakub Kicinski 		return err;
59164775cc1fSMathias Krause 
591710c9ead9SRoopa Prabhu 	ifsm = nlmsg_data(nlh);
591810c9ead9SRoopa Prabhu 	if (ifsm->ifindex > 0)
591910c9ead9SRoopa Prabhu 		dev = __dev_get_by_index(net, ifsm->ifindex);
592010c9ead9SRoopa Prabhu 	else
592110c9ead9SRoopa Prabhu 		return -EINVAL;
592210c9ead9SRoopa Prabhu 
592310c9ead9SRoopa Prabhu 	if (!dev)
592410c9ead9SRoopa Prabhu 		return -ENODEV;
592510c9ead9SRoopa Prabhu 
592646efc97bSPetr Machata 	if (!ifsm->filter_mask) {
592722b67d17SPetr Machata 		NL_SET_ERR_MSG(extack, "Filter mask must be set for stats get");
592810c9ead9SRoopa Prabhu 		return -EINVAL;
592922b67d17SPetr Machata 	}
593010c9ead9SRoopa Prabhu 
593146efc97bSPetr Machata 	err = rtnl_stats_get_parse(nlh, ifsm->filter_mask, &filters, extack);
593246efc97bSPetr Machata 	if (err)
593346efc97bSPetr Machata 		return err;
593446efc97bSPetr Machata 
593546efc97bSPetr Machata 	nskb = nlmsg_new(if_nlmsg_stats_size(dev, &filters), GFP_KERNEL);
593610c9ead9SRoopa Prabhu 	if (!nskb)
593710c9ead9SRoopa Prabhu 		return -ENOBUFS;
593810c9ead9SRoopa Prabhu 
593910c9ead9SRoopa Prabhu 	err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
594010c9ead9SRoopa Prabhu 				  NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
594105415bccSPetr Machata 				  0, &filters, &idxattr, &prividx, extack);
594210c9ead9SRoopa Prabhu 	if (err < 0) {
594310c9ead9SRoopa Prabhu 		/* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
594410c9ead9SRoopa Prabhu 		WARN_ON(err == -EMSGSIZE);
594510c9ead9SRoopa Prabhu 		kfree_skb(nskb);
594610c9ead9SRoopa Prabhu 	} else {
594710c9ead9SRoopa Prabhu 		err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
594810c9ead9SRoopa Prabhu 	}
594910c9ead9SRoopa Prabhu 
595010c9ead9SRoopa Prabhu 	return err;
595110c9ead9SRoopa Prabhu }
595210c9ead9SRoopa Prabhu 
595310c9ead9SRoopa Prabhu static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
595410c9ead9SRoopa Prabhu {
5955841891ecSDavid Ahern 	struct netlink_ext_ack *extack = cb->extack;
5956e8872a25SNikolay Aleksandrov 	int h, s_h, err, s_idx, s_idxattr, s_prividx;
595746efc97bSPetr Machata 	struct rtnl_stats_dump_filters filters;
595810c9ead9SRoopa Prabhu 	struct net *net = sock_net(skb->sk);
595910c9ead9SRoopa Prabhu 	unsigned int flags = NLM_F_MULTI;
5960e8872a25SNikolay Aleksandrov 	struct if_stats_msg *ifsm;
5961e8872a25SNikolay Aleksandrov 	struct hlist_head *head;
5962e8872a25SNikolay Aleksandrov 	struct net_device *dev;
5963e8872a25SNikolay Aleksandrov 	int idx = 0;
596410c9ead9SRoopa Prabhu 
596510c9ead9SRoopa Prabhu 	s_h = cb->args[0];
596610c9ead9SRoopa Prabhu 	s_idx = cb->args[1];
5967e8872a25SNikolay Aleksandrov 	s_idxattr = cb->args[2];
5968e8872a25SNikolay Aleksandrov 	s_prividx = cb->args[3];
596910c9ead9SRoopa Prabhu 
597010c9ead9SRoopa Prabhu 	cb->seq = net->dev_base_seq;
597110c9ead9SRoopa Prabhu 
597251bc860dSJakub Kicinski 	err = rtnl_valid_stats_req(cb->nlh, cb->strict_check, true, extack);
597351bc860dSJakub Kicinski 	if (err)
597451bc860dSJakub Kicinski 		return err;
59754775cc1fSMathias Krause 
597610c9ead9SRoopa Prabhu 	ifsm = nlmsg_data(cb->nlh);
597746efc97bSPetr Machata 	if (!ifsm->filter_mask) {
5978841891ecSDavid Ahern 		NL_SET_ERR_MSG(extack, "Filter mask must be set for stats dump");
5979841891ecSDavid Ahern 		return -EINVAL;
5980841891ecSDavid Ahern 	}
598110c9ead9SRoopa Prabhu 
598246efc97bSPetr Machata 	err = rtnl_stats_get_parse(cb->nlh, ifsm->filter_mask, &filters,
598346efc97bSPetr Machata 				   extack);
598446efc97bSPetr Machata 	if (err)
598546efc97bSPetr Machata 		return err;
598646efc97bSPetr Machata 
598710c9ead9SRoopa Prabhu 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
598810c9ead9SRoopa Prabhu 		idx = 0;
598910c9ead9SRoopa Prabhu 		head = &net->dev_index_head[h];
599010c9ead9SRoopa Prabhu 		hlist_for_each_entry(dev, head, index_hlist) {
599110c9ead9SRoopa Prabhu 			if (idx < s_idx)
599210c9ead9SRoopa Prabhu 				goto cont;
599310c9ead9SRoopa Prabhu 			err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
599410c9ead9SRoopa Prabhu 						  NETLINK_CB(cb->skb).portid,
599510c9ead9SRoopa Prabhu 						  cb->nlh->nlmsg_seq, 0,
599646efc97bSPetr Machata 						  flags, &filters,
599705415bccSPetr Machata 						  &s_idxattr, &s_prividx,
599805415bccSPetr Machata 						  extack);
599910c9ead9SRoopa Prabhu 			/* If we ran out of room on the first message,
600010c9ead9SRoopa Prabhu 			 * we're in trouble
600110c9ead9SRoopa Prabhu 			 */
600210c9ead9SRoopa Prabhu 			WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
600310c9ead9SRoopa Prabhu 
600410c9ead9SRoopa Prabhu 			if (err < 0)
600510c9ead9SRoopa Prabhu 				goto out;
6006e8872a25SNikolay Aleksandrov 			s_prividx = 0;
6007e8872a25SNikolay Aleksandrov 			s_idxattr = 0;
600810c9ead9SRoopa Prabhu 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
600910c9ead9SRoopa Prabhu cont:
601010c9ead9SRoopa Prabhu 			idx++;
601110c9ead9SRoopa Prabhu 		}
601210c9ead9SRoopa Prabhu 	}
601310c9ead9SRoopa Prabhu out:
6014e8872a25SNikolay Aleksandrov 	cb->args[3] = s_prividx;
6015e8872a25SNikolay Aleksandrov 	cb->args[2] = s_idxattr;
601610c9ead9SRoopa Prabhu 	cb->args[1] = idx;
601710c9ead9SRoopa Prabhu 	cb->args[0] = h;
601810c9ead9SRoopa Prabhu 
601910c9ead9SRoopa Prabhu 	return skb->len;
602010c9ead9SRoopa Prabhu }
602110c9ead9SRoopa Prabhu 
60225fd0b838SPetr Machata void rtnl_offload_xstats_notify(struct net_device *dev)
60235fd0b838SPetr Machata {
60245fd0b838SPetr Machata 	struct rtnl_stats_dump_filters response_filters = {};
60255fd0b838SPetr Machata 	struct net *net = dev_net(dev);
60265fd0b838SPetr Machata 	int idxattr = 0, prividx = 0;
60275fd0b838SPetr Machata 	struct sk_buff *skb;
60285fd0b838SPetr Machata 	int err = -ENOBUFS;
60295fd0b838SPetr Machata 
60305fd0b838SPetr Machata 	ASSERT_RTNL();
60315fd0b838SPetr Machata 
60325fd0b838SPetr Machata 	response_filters.mask[0] |=
60335fd0b838SPetr Machata 		IFLA_STATS_FILTER_BIT(IFLA_STATS_LINK_OFFLOAD_XSTATS);
60345fd0b838SPetr Machata 	response_filters.mask[IFLA_STATS_LINK_OFFLOAD_XSTATS] |=
60355fd0b838SPetr Machata 		IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_HW_S_INFO);
60365fd0b838SPetr Machata 
60375fd0b838SPetr Machata 	skb = nlmsg_new(if_nlmsg_stats_size(dev, &response_filters),
60385fd0b838SPetr Machata 			GFP_KERNEL);
60395fd0b838SPetr Machata 	if (!skb)
60405fd0b838SPetr Machata 		goto errout;
60415fd0b838SPetr Machata 
60425fd0b838SPetr Machata 	err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS, 0, 0, 0, 0,
60435fd0b838SPetr Machata 				  &response_filters, &idxattr, &prividx, NULL);
60445fd0b838SPetr Machata 	if (err < 0) {
60455fd0b838SPetr Machata 		kfree_skb(skb);
60465fd0b838SPetr Machata 		goto errout;
60475fd0b838SPetr Machata 	}
60485fd0b838SPetr Machata 
60495fd0b838SPetr Machata 	rtnl_notify(skb, net, 0, RTNLGRP_STATS, NULL, GFP_KERNEL);
60505fd0b838SPetr Machata 	return;
60515fd0b838SPetr Machata 
60525fd0b838SPetr Machata errout:
60535fd0b838SPetr Machata 	rtnl_set_sk_err(net, RTNLGRP_STATS, err);
60545fd0b838SPetr Machata }
60555fd0b838SPetr Machata EXPORT_SYMBOL(rtnl_offload_xstats_notify);
60565fd0b838SPetr Machata 
605703ba3566SPetr Machata static int rtnl_stats_set(struct sk_buff *skb, struct nlmsghdr *nlh,
605803ba3566SPetr Machata 			  struct netlink_ext_ack *extack)
605903ba3566SPetr Machata {
60605fd0b838SPetr Machata 	enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3;
606103ba3566SPetr Machata 	struct rtnl_stats_dump_filters response_filters = {};
606203ba3566SPetr Machata 	struct nlattr *tb[IFLA_STATS_GETSET_MAX + 1];
606303ba3566SPetr Machata 	struct net *net = sock_net(skb->sk);
606403ba3566SPetr Machata 	struct net_device *dev = NULL;
606503ba3566SPetr Machata 	struct if_stats_msg *ifsm;
60665fd0b838SPetr Machata 	bool notify = false;
606703ba3566SPetr Machata 	int err;
606803ba3566SPetr Machata 
606903ba3566SPetr Machata 	err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb),
607003ba3566SPetr Machata 				   false, extack);
607103ba3566SPetr Machata 	if (err)
607203ba3566SPetr Machata 		return err;
607303ba3566SPetr Machata 
607403ba3566SPetr Machata 	ifsm = nlmsg_data(nlh);
607503ba3566SPetr Machata 	if (ifsm->family != AF_UNSPEC) {
607603ba3566SPetr Machata 		NL_SET_ERR_MSG(extack, "Address family should be AF_UNSPEC");
607703ba3566SPetr Machata 		return -EINVAL;
607803ba3566SPetr Machata 	}
607903ba3566SPetr Machata 
608003ba3566SPetr Machata 	if (ifsm->ifindex > 0)
608103ba3566SPetr Machata 		dev = __dev_get_by_index(net, ifsm->ifindex);
608203ba3566SPetr Machata 	else
608303ba3566SPetr Machata 		return -EINVAL;
608403ba3566SPetr Machata 
608503ba3566SPetr Machata 	if (!dev)
608603ba3566SPetr Machata 		return -ENODEV;
608703ba3566SPetr Machata 
608803ba3566SPetr Machata 	if (ifsm->filter_mask) {
608903ba3566SPetr Machata 		NL_SET_ERR_MSG(extack, "Filter mask must be 0 for stats set");
609003ba3566SPetr Machata 		return -EINVAL;
609103ba3566SPetr Machata 	}
609203ba3566SPetr Machata 
609303ba3566SPetr Machata 	err = nlmsg_parse(nlh, sizeof(*ifsm), tb, IFLA_STATS_GETSET_MAX,
609403ba3566SPetr Machata 			  ifla_stats_set_policy, extack);
609503ba3566SPetr Machata 	if (err < 0)
609603ba3566SPetr Machata 		return err;
609703ba3566SPetr Machata 
60985fd0b838SPetr Machata 	if (tb[IFLA_STATS_SET_OFFLOAD_XSTATS_L3_STATS]) {
60995fd0b838SPetr Machata 		u8 req = nla_get_u8(tb[IFLA_STATS_SET_OFFLOAD_XSTATS_L3_STATS]);
610003ba3566SPetr Machata 
61015fd0b838SPetr Machata 		if (req)
61025fd0b838SPetr Machata 			err = netdev_offload_xstats_enable(dev, t_l3, extack);
61035fd0b838SPetr Machata 		else
61045fd0b838SPetr Machata 			err = netdev_offload_xstats_disable(dev, t_l3);
61055fd0b838SPetr Machata 
61065fd0b838SPetr Machata 		if (!err)
61075fd0b838SPetr Machata 			notify = true;
61085fd0b838SPetr Machata 		else if (err != -EALREADY)
61095fd0b838SPetr Machata 			return err;
61105fd0b838SPetr Machata 
61115fd0b838SPetr Machata 		response_filters.mask[0] |=
61125fd0b838SPetr Machata 			IFLA_STATS_FILTER_BIT(IFLA_STATS_LINK_OFFLOAD_XSTATS);
61135fd0b838SPetr Machata 		response_filters.mask[IFLA_STATS_LINK_OFFLOAD_XSTATS] |=
61145fd0b838SPetr Machata 			IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_HW_S_INFO);
611503ba3566SPetr Machata 	}
611603ba3566SPetr Machata 
61175fd0b838SPetr Machata 	if (notify)
61185fd0b838SPetr Machata 		rtnl_offload_xstats_notify(dev);
61195fd0b838SPetr Machata 
61205fd0b838SPetr Machata 	return 0;
612103ba3566SPetr Machata }
612203ba3566SPetr Machata 
6123cc7f5022SIdo Schimmel static int rtnl_mdb_valid_dump_req(const struct nlmsghdr *nlh,
6124cc7f5022SIdo Schimmel 				   struct netlink_ext_ack *extack)
6125cc7f5022SIdo Schimmel {
6126cc7f5022SIdo Schimmel 	struct br_port_msg *bpm;
6127cc7f5022SIdo Schimmel 
6128cc7f5022SIdo Schimmel 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*bpm))) {
6129cc7f5022SIdo Schimmel 		NL_SET_ERR_MSG(extack, "Invalid header for mdb dump request");
6130cc7f5022SIdo Schimmel 		return -EINVAL;
6131cc7f5022SIdo Schimmel 	}
6132cc7f5022SIdo Schimmel 
6133cc7f5022SIdo Schimmel 	bpm = nlmsg_data(nlh);
6134cc7f5022SIdo Schimmel 	if (bpm->ifindex) {
6135cc7f5022SIdo Schimmel 		NL_SET_ERR_MSG(extack, "Filtering by device index is not supported for mdb dump request");
6136cc7f5022SIdo Schimmel 		return -EINVAL;
6137cc7f5022SIdo Schimmel 	}
6138cc7f5022SIdo Schimmel 	if (nlmsg_attrlen(nlh, sizeof(*bpm))) {
6139cc7f5022SIdo Schimmel 		NL_SET_ERR_MSG(extack, "Invalid data after header in mdb dump request");
6140cc7f5022SIdo Schimmel 		return -EINVAL;
6141cc7f5022SIdo Schimmel 	}
6142cc7f5022SIdo Schimmel 
6143cc7f5022SIdo Schimmel 	return 0;
6144cc7f5022SIdo Schimmel }
6145cc7f5022SIdo Schimmel 
6146cc7f5022SIdo Schimmel struct rtnl_mdb_dump_ctx {
6147cc7f5022SIdo Schimmel 	long idx;
6148cc7f5022SIdo Schimmel };
6149cc7f5022SIdo Schimmel 
6150cc7f5022SIdo Schimmel static int rtnl_mdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
6151cc7f5022SIdo Schimmel {
6152cc7f5022SIdo Schimmel 	struct rtnl_mdb_dump_ctx *ctx = (void *)cb->ctx;
6153cc7f5022SIdo Schimmel 	struct net *net = sock_net(skb->sk);
6154cc7f5022SIdo Schimmel 	struct net_device *dev;
6155cc7f5022SIdo Schimmel 	int idx, s_idx;
6156cc7f5022SIdo Schimmel 	int err;
6157cc7f5022SIdo Schimmel 
6158cc7f5022SIdo Schimmel 	NL_ASSERT_DUMP_CTX_FITS(struct rtnl_mdb_dump_ctx);
6159cc7f5022SIdo Schimmel 
6160cc7f5022SIdo Schimmel 	if (cb->strict_check) {
6161cc7f5022SIdo Schimmel 		err = rtnl_mdb_valid_dump_req(cb->nlh, cb->extack);
6162cc7f5022SIdo Schimmel 		if (err)
6163cc7f5022SIdo Schimmel 			return err;
6164cc7f5022SIdo Schimmel 	}
6165cc7f5022SIdo Schimmel 
6166cc7f5022SIdo Schimmel 	s_idx = ctx->idx;
6167cc7f5022SIdo Schimmel 	idx = 0;
6168cc7f5022SIdo Schimmel 
6169cc7f5022SIdo Schimmel 	for_each_netdev(net, dev) {
6170cc7f5022SIdo Schimmel 		if (idx < s_idx)
6171cc7f5022SIdo Schimmel 			goto skip;
6172cc7f5022SIdo Schimmel 		if (!dev->netdev_ops->ndo_mdb_dump)
6173cc7f5022SIdo Schimmel 			goto skip;
6174cc7f5022SIdo Schimmel 
6175cc7f5022SIdo Schimmel 		err = dev->netdev_ops->ndo_mdb_dump(dev, skb, cb);
6176cc7f5022SIdo Schimmel 		if (err == -EMSGSIZE)
6177cc7f5022SIdo Schimmel 			goto out;
6178cc7f5022SIdo Schimmel 		/* Moving on to next device, reset markers and sequence
6179cc7f5022SIdo Schimmel 		 * counters since they are all maintained per-device.
6180cc7f5022SIdo Schimmel 		 */
6181cc7f5022SIdo Schimmel 		memset(cb->ctx, 0, sizeof(cb->ctx));
6182cc7f5022SIdo Schimmel 		cb->prev_seq = 0;
6183cc7f5022SIdo Schimmel 		cb->seq = 0;
6184cc7f5022SIdo Schimmel skip:
6185cc7f5022SIdo Schimmel 		idx++;
6186cc7f5022SIdo Schimmel 	}
6187cc7f5022SIdo Schimmel 
6188cc7f5022SIdo Schimmel out:
6189cc7f5022SIdo Schimmel 	ctx->idx = idx;
6190cc7f5022SIdo Schimmel 	return skb->len;
6191cc7f5022SIdo Schimmel }
6192cc7f5022SIdo Schimmel 
6193cc7f5022SIdo Schimmel static int rtnl_validate_mdb_entry(const struct nlattr *attr,
6194cc7f5022SIdo Schimmel 				   struct netlink_ext_ack *extack)
6195cc7f5022SIdo Schimmel {
6196cc7f5022SIdo Schimmel 	struct br_mdb_entry *entry = nla_data(attr);
6197cc7f5022SIdo Schimmel 
6198cc7f5022SIdo Schimmel 	if (nla_len(attr) != sizeof(struct br_mdb_entry)) {
6199cc7f5022SIdo Schimmel 		NL_SET_ERR_MSG_ATTR(extack, attr, "Invalid attribute length");
6200cc7f5022SIdo Schimmel 		return -EINVAL;
6201cc7f5022SIdo Schimmel 	}
6202cc7f5022SIdo Schimmel 
6203cc7f5022SIdo Schimmel 	if (entry->ifindex == 0) {
6204cc7f5022SIdo Schimmel 		NL_SET_ERR_MSG(extack, "Zero entry ifindex is not allowed");
6205cc7f5022SIdo Schimmel 		return -EINVAL;
6206cc7f5022SIdo Schimmel 	}
6207cc7f5022SIdo Schimmel 
6208cc7f5022SIdo Schimmel 	if (entry->addr.proto == htons(ETH_P_IP)) {
6209da654c80SIdo Schimmel 		if (!ipv4_is_multicast(entry->addr.u.ip4) &&
6210da654c80SIdo Schimmel 		    !ipv4_is_zeronet(entry->addr.u.ip4)) {
6211da654c80SIdo Schimmel 			NL_SET_ERR_MSG(extack, "IPv4 entry group address is not multicast or 0.0.0.0");
6212cc7f5022SIdo Schimmel 			return -EINVAL;
6213cc7f5022SIdo Schimmel 		}
6214cc7f5022SIdo Schimmel 		if (ipv4_is_local_multicast(entry->addr.u.ip4)) {
6215cc7f5022SIdo Schimmel 			NL_SET_ERR_MSG(extack, "IPv4 entry group address is local multicast");
6216cc7f5022SIdo Schimmel 			return -EINVAL;
6217cc7f5022SIdo Schimmel 		}
6218cc7f5022SIdo Schimmel #if IS_ENABLED(CONFIG_IPV6)
6219cc7f5022SIdo Schimmel 	} else if (entry->addr.proto == htons(ETH_P_IPV6)) {
6220cc7f5022SIdo Schimmel 		if (ipv6_addr_is_ll_all_nodes(&entry->addr.u.ip6)) {
6221cc7f5022SIdo Schimmel 			NL_SET_ERR_MSG(extack, "IPv6 entry group address is link-local all nodes");
6222cc7f5022SIdo Schimmel 			return -EINVAL;
6223cc7f5022SIdo Schimmel 		}
6224cc7f5022SIdo Schimmel #endif
6225cc7f5022SIdo Schimmel 	} else if (entry->addr.proto == 0) {
6226cc7f5022SIdo Schimmel 		/* L2 mdb */
6227cc7f5022SIdo Schimmel 		if (!is_multicast_ether_addr(entry->addr.u.mac_addr)) {
6228cc7f5022SIdo Schimmel 			NL_SET_ERR_MSG(extack, "L2 entry group is not multicast");
6229cc7f5022SIdo Schimmel 			return -EINVAL;
6230cc7f5022SIdo Schimmel 		}
6231cc7f5022SIdo Schimmel 	} else {
6232cc7f5022SIdo Schimmel 		NL_SET_ERR_MSG(extack, "Unknown entry protocol");
6233cc7f5022SIdo Schimmel 		return -EINVAL;
6234cc7f5022SIdo Schimmel 	}
6235cc7f5022SIdo Schimmel 
6236cc7f5022SIdo Schimmel 	if (entry->state != MDB_PERMANENT && entry->state != MDB_TEMPORARY) {
6237cc7f5022SIdo Schimmel 		NL_SET_ERR_MSG(extack, "Unknown entry state");
6238cc7f5022SIdo Schimmel 		return -EINVAL;
6239cc7f5022SIdo Schimmel 	}
6240cc7f5022SIdo Schimmel 	if (entry->vid >= VLAN_VID_MASK) {
6241cc7f5022SIdo Schimmel 		NL_SET_ERR_MSG(extack, "Invalid entry VLAN id");
6242cc7f5022SIdo Schimmel 		return -EINVAL;
6243cc7f5022SIdo Schimmel 	}
6244cc7f5022SIdo Schimmel 
6245cc7f5022SIdo Schimmel 	return 0;
6246cc7f5022SIdo Schimmel }
6247cc7f5022SIdo Schimmel 
6248cc7f5022SIdo Schimmel static const struct nla_policy mdba_policy[MDBA_SET_ENTRY_MAX + 1] = {
6249cc7f5022SIdo Schimmel 	[MDBA_SET_ENTRY_UNSPEC] = { .strict_start_type = MDBA_SET_ENTRY_ATTRS + 1 },
6250cc7f5022SIdo Schimmel 	[MDBA_SET_ENTRY] = NLA_POLICY_VALIDATE_FN(NLA_BINARY,
6251cc7f5022SIdo Schimmel 						  rtnl_validate_mdb_entry,
6252cc7f5022SIdo Schimmel 						  sizeof(struct br_mdb_entry)),
6253cc7f5022SIdo Schimmel 	[MDBA_SET_ENTRY_ATTRS] = { .type = NLA_NESTED },
6254cc7f5022SIdo Schimmel };
6255cc7f5022SIdo Schimmel 
6256cc7f5022SIdo Schimmel static int rtnl_mdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
6257cc7f5022SIdo Schimmel 			struct netlink_ext_ack *extack)
6258cc7f5022SIdo Schimmel {
6259cc7f5022SIdo Schimmel 	struct nlattr *tb[MDBA_SET_ENTRY_MAX + 1];
6260cc7f5022SIdo Schimmel 	struct net *net = sock_net(skb->sk);
6261cc7f5022SIdo Schimmel 	struct br_port_msg *bpm;
6262cc7f5022SIdo Schimmel 	struct net_device *dev;
6263cc7f5022SIdo Schimmel 	int err;
6264cc7f5022SIdo Schimmel 
6265cc7f5022SIdo Schimmel 	err = nlmsg_parse_deprecated(nlh, sizeof(*bpm), tb,
6266cc7f5022SIdo Schimmel 				     MDBA_SET_ENTRY_MAX, mdba_policy, extack);
6267cc7f5022SIdo Schimmel 	if (err)
6268cc7f5022SIdo Schimmel 		return err;
6269cc7f5022SIdo Schimmel 
6270cc7f5022SIdo Schimmel 	bpm = nlmsg_data(nlh);
6271cc7f5022SIdo Schimmel 	if (!bpm->ifindex) {
6272cc7f5022SIdo Schimmel 		NL_SET_ERR_MSG(extack, "Invalid ifindex");
6273cc7f5022SIdo Schimmel 		return -EINVAL;
6274cc7f5022SIdo Schimmel 	}
6275cc7f5022SIdo Schimmel 
6276cc7f5022SIdo Schimmel 	dev = __dev_get_by_index(net, bpm->ifindex);
6277cc7f5022SIdo Schimmel 	if (!dev) {
6278cc7f5022SIdo Schimmel 		NL_SET_ERR_MSG(extack, "Device doesn't exist");
6279cc7f5022SIdo Schimmel 		return -ENODEV;
6280cc7f5022SIdo Schimmel 	}
6281cc7f5022SIdo Schimmel 
6282cc7f5022SIdo Schimmel 	if (NL_REQ_ATTR_CHECK(extack, NULL, tb, MDBA_SET_ENTRY)) {
6283cc7f5022SIdo Schimmel 		NL_SET_ERR_MSG(extack, "Missing MDBA_SET_ENTRY attribute");
6284cc7f5022SIdo Schimmel 		return -EINVAL;
6285cc7f5022SIdo Schimmel 	}
6286cc7f5022SIdo Schimmel 
6287cc7f5022SIdo Schimmel 	if (!dev->netdev_ops->ndo_mdb_add) {
6288cc7f5022SIdo Schimmel 		NL_SET_ERR_MSG(extack, "Device does not support MDB operations");
6289cc7f5022SIdo Schimmel 		return -EOPNOTSUPP;
6290cc7f5022SIdo Schimmel 	}
6291cc7f5022SIdo Schimmel 
6292cc7f5022SIdo Schimmel 	return dev->netdev_ops->ndo_mdb_add(dev, tb, nlh->nlmsg_flags, extack);
6293cc7f5022SIdo Schimmel }
6294cc7f5022SIdo Schimmel 
6295cc7f5022SIdo Schimmel static int rtnl_mdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
6296cc7f5022SIdo Schimmel 			struct netlink_ext_ack *extack)
6297cc7f5022SIdo Schimmel {
6298cc7f5022SIdo Schimmel 	struct nlattr *tb[MDBA_SET_ENTRY_MAX + 1];
6299cc7f5022SIdo Schimmel 	struct net *net = sock_net(skb->sk);
6300cc7f5022SIdo Schimmel 	struct br_port_msg *bpm;
6301cc7f5022SIdo Schimmel 	struct net_device *dev;
6302cc7f5022SIdo Schimmel 	int err;
6303cc7f5022SIdo Schimmel 
6304cc7f5022SIdo Schimmel 	err = nlmsg_parse_deprecated(nlh, sizeof(*bpm), tb,
6305cc7f5022SIdo Schimmel 				     MDBA_SET_ENTRY_MAX, mdba_policy, extack);
6306cc7f5022SIdo Schimmel 	if (err)
6307cc7f5022SIdo Schimmel 		return err;
6308cc7f5022SIdo Schimmel 
6309cc7f5022SIdo Schimmel 	bpm = nlmsg_data(nlh);
6310cc7f5022SIdo Schimmel 	if (!bpm->ifindex) {
6311cc7f5022SIdo Schimmel 		NL_SET_ERR_MSG(extack, "Invalid ifindex");
6312cc7f5022SIdo Schimmel 		return -EINVAL;
6313cc7f5022SIdo Schimmel 	}
6314cc7f5022SIdo Schimmel 
6315cc7f5022SIdo Schimmel 	dev = __dev_get_by_index(net, bpm->ifindex);
6316cc7f5022SIdo Schimmel 	if (!dev) {
6317cc7f5022SIdo Schimmel 		NL_SET_ERR_MSG(extack, "Device doesn't exist");
6318cc7f5022SIdo Schimmel 		return -ENODEV;
6319cc7f5022SIdo Schimmel 	}
6320cc7f5022SIdo Schimmel 
6321cc7f5022SIdo Schimmel 	if (NL_REQ_ATTR_CHECK(extack, NULL, tb, MDBA_SET_ENTRY)) {
6322cc7f5022SIdo Schimmel 		NL_SET_ERR_MSG(extack, "Missing MDBA_SET_ENTRY attribute");
6323cc7f5022SIdo Schimmel 		return -EINVAL;
6324cc7f5022SIdo Schimmel 	}
6325cc7f5022SIdo Schimmel 
6326cc7f5022SIdo Schimmel 	if (!dev->netdev_ops->ndo_mdb_del) {
6327cc7f5022SIdo Schimmel 		NL_SET_ERR_MSG(extack, "Device does not support MDB operations");
6328cc7f5022SIdo Schimmel 		return -EOPNOTSUPP;
6329cc7f5022SIdo Schimmel 	}
6330cc7f5022SIdo Schimmel 
6331cc7f5022SIdo Schimmel 	return dev->netdev_ops->ndo_mdb_del(dev, tb, extack);
6332cc7f5022SIdo Schimmel }
6333cc7f5022SIdo Schimmel 
63341da177e4SLinus Torvalds /* Process one rtnetlink message. */
63351da177e4SLinus Torvalds 
63362d4bc933SJohannes Berg static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
63372d4bc933SJohannes Berg 			     struct netlink_ext_ack *extack)
63381da177e4SLinus Torvalds {
63393b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
6340addf9b90SFlorian Westphal 	struct rtnl_link *link;
634112dc5c2cSNikolay Aleksandrov 	enum rtnl_kinds kind;
6342e4202511SFlorian Westphal 	struct module *owner;
63436853dd48SFlorian Westphal 	int err = -EOPNOTSUPP;
6344e2849863SThomas Graf 	rtnl_doit_func doit;
634562256f98SFlorian Westphal 	unsigned int flags;
63461da177e4SLinus Torvalds 	int family;
63471da177e4SLinus Torvalds 	int type;
63481da177e4SLinus Torvalds 
63491da177e4SLinus Torvalds 	type = nlh->nlmsg_type;
63501da177e4SLinus Torvalds 	if (type > RTM_MAX)
6351038890feSThomas Graf 		return -EOPNOTSUPP;
63521da177e4SLinus Torvalds 
63531da177e4SLinus Torvalds 	type -= RTM_BASE;
63541da177e4SLinus Torvalds 
63551da177e4SLinus Torvalds 	/* All the messages must have at least 1 byte length */
6356573ce260SHong zhi guo 	if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
63571da177e4SLinus Torvalds 		return 0;
63581da177e4SLinus Torvalds 
6359573ce260SHong zhi guo 	family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
63602e9ea3e3SNikolay Aleksandrov 	kind = rtnl_msgtype_kind(type);
63611da177e4SLinus Torvalds 
636212dc5c2cSNikolay Aleksandrov 	if (kind != RTNL_KIND_GET && !netlink_net_capable(skb, CAP_NET_ADMIN))
63631d00a4ebSThomas Graf 		return -EPERM;
63641da177e4SLinus Torvalds 
63656853dd48SFlorian Westphal 	rcu_read_lock();
636612dc5c2cSNikolay Aleksandrov 	if (kind == RTNL_KIND_GET && (nlh->nlmsg_flags & NLM_F_DUMP)) {
636797c53cacSDenis V. Lunev 		struct sock *rtnl;
6368e2849863SThomas Graf 		rtnl_dumpit_func dumpit;
6369ebfe3c51SDi Zhu 		u32 min_dump_alloc = 0;
63701da177e4SLinus Torvalds 
6371addf9b90SFlorian Westphal 		link = rtnl_get_link(family, type);
6372addf9b90SFlorian Westphal 		if (!link || !link->dumpit) {
63736853dd48SFlorian Westphal 			family = PF_UNSPEC;
6374addf9b90SFlorian Westphal 			link = rtnl_get_link(family, type);
6375addf9b90SFlorian Westphal 			if (!link || !link->dumpit)
63766853dd48SFlorian Westphal 				goto err_unlock;
63776853dd48SFlorian Westphal 		}
6378e4202511SFlorian Westphal 		owner = link->owner;
6379addf9b90SFlorian Westphal 		dumpit = link->dumpit;
63806853dd48SFlorian Westphal 
63815c2bb9b6SFlorian Westphal 		if (type == RTM_GETLINK - RTM_BASE)
6382e1fa6d21SFlorian Westphal 			min_dump_alloc = rtnl_calcit(skb, nlh);
63831da177e4SLinus Torvalds 
6384e4202511SFlorian Westphal 		err = 0;
6385e4202511SFlorian Westphal 		/* need to do this before rcu_read_unlock() */
6386e4202511SFlorian Westphal 		if (!try_module_get(owner))
6387e4202511SFlorian Westphal 			err = -EPROTONOSUPPORT;
6388e4202511SFlorian Westphal 
63896853dd48SFlorian Westphal 		rcu_read_unlock();
63906853dd48SFlorian Westphal 
639197c53cacSDenis V. Lunev 		rtnl = net->rtnl;
6392e4202511SFlorian Westphal 		if (err == 0) {
639380d326faSPablo Neira Ayuso 			struct netlink_dump_control c = {
639480d326faSPablo Neira Ayuso 				.dump		= dumpit,
639580d326faSPablo Neira Ayuso 				.min_dump_alloc	= min_dump_alloc,
6396e4202511SFlorian Westphal 				.module		= owner,
639780d326faSPablo Neira Ayuso 			};
639880d326faSPablo Neira Ayuso 			err = netlink_dump_start(rtnl, skb, nlh, &c);
6399e4202511SFlorian Westphal 			/* netlink_dump_start() will keep a reference on
6400e4202511SFlorian Westphal 			 * module if dump is still in progress.
6401e4202511SFlorian Westphal 			 */
6402e4202511SFlorian Westphal 			module_put(owner);
640380d326faSPablo Neira Ayuso 		}
64042907c35fSEric Dumazet 		return err;
64051da177e4SLinus Torvalds 	}
64061da177e4SLinus Torvalds 
6407addf9b90SFlorian Westphal 	link = rtnl_get_link(family, type);
6408addf9b90SFlorian Westphal 	if (!link || !link->doit) {
64098caa38b5SFlorian Westphal 		family = PF_UNSPEC;
6410addf9b90SFlorian Westphal 		link = rtnl_get_link(PF_UNSPEC, type);
6411addf9b90SFlorian Westphal 		if (!link || !link->doit)
6412addf9b90SFlorian Westphal 			goto out_unlock;
64138caa38b5SFlorian Westphal 	}
64148caa38b5SFlorian Westphal 
6415e4202511SFlorian Westphal 	owner = link->owner;
6416e4202511SFlorian Westphal 	if (!try_module_get(owner)) {
6417e4202511SFlorian Westphal 		err = -EPROTONOSUPPORT;
6418e4202511SFlorian Westphal 		goto out_unlock;
6419e4202511SFlorian Westphal 	}
6420e4202511SFlorian Westphal 
6421addf9b90SFlorian Westphal 	flags = link->flags;
6422a6cec0bcSNikolay Aleksandrov 	if (kind == RTNL_KIND_DEL && (nlh->nlmsg_flags & NLM_F_BULK) &&
6423a6cec0bcSNikolay Aleksandrov 	    !(flags & RTNL_FLAG_BULK_DEL_SUPPORTED)) {
6424a6cec0bcSNikolay Aleksandrov 		NL_SET_ERR_MSG(extack, "Bulk delete is not supported");
64255b22f627SZhengchao Shao 		module_put(owner);
6426a6cec0bcSNikolay Aleksandrov 		goto err_unlock;
6427a6cec0bcSNikolay Aleksandrov 	}
6428a6cec0bcSNikolay Aleksandrov 
642962256f98SFlorian Westphal 	if (flags & RTNL_FLAG_DOIT_UNLOCKED) {
6430addf9b90SFlorian Westphal 		doit = link->doit;
643162256f98SFlorian Westphal 		rcu_read_unlock();
643262256f98SFlorian Westphal 		if (doit)
643362256f98SFlorian Westphal 			err = doit(skb, nlh, extack);
6434e4202511SFlorian Westphal 		module_put(owner);
643562256f98SFlorian Westphal 		return err;
643662256f98SFlorian Westphal 	}
64376853dd48SFlorian Westphal 	rcu_read_unlock();
64386853dd48SFlorian Westphal 
64390cc09020SFlorian Westphal 	rtnl_lock();
6440addf9b90SFlorian Westphal 	link = rtnl_get_link(family, type);
6441addf9b90SFlorian Westphal 	if (link && link->doit)
6442addf9b90SFlorian Westphal 		err = link->doit(skb, nlh, extack);
64430cc09020SFlorian Westphal 	rtnl_unlock();
6444addf9b90SFlorian Westphal 
6445e4202511SFlorian Westphal 	module_put(owner);
6446e4202511SFlorian Westphal 
6447addf9b90SFlorian Westphal 	return err;
6448addf9b90SFlorian Westphal 
6449addf9b90SFlorian Westphal out_unlock:
6450addf9b90SFlorian Westphal 	rcu_read_unlock();
64510cc09020SFlorian Westphal 	return err;
64520cc09020SFlorian Westphal 
64530cc09020SFlorian Westphal err_unlock:
64546853dd48SFlorian Westphal 	rcu_read_unlock();
64550cc09020SFlorian Westphal 	return -EOPNOTSUPP;
64561da177e4SLinus Torvalds }
64571da177e4SLinus Torvalds 
6458cd40b7d3SDenis V. Lunev static void rtnetlink_rcv(struct sk_buff *skb)
64591da177e4SLinus Torvalds {
6460cd40b7d3SDenis V. Lunev 	netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
64611da177e4SLinus Torvalds }
64621da177e4SLinus Torvalds 
64635f729eaaSJulien Gomes static int rtnetlink_bind(struct net *net, int group)
64645f729eaaSJulien Gomes {
64655f729eaaSJulien Gomes 	switch (group) {
64665f729eaaSJulien Gomes 	case RTNLGRP_IPV4_MROUTE_R:
64675f729eaaSJulien Gomes 	case RTNLGRP_IPV6_MROUTE_R:
64685f729eaaSJulien Gomes 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
64695f729eaaSJulien Gomes 			return -EPERM;
64705f729eaaSJulien Gomes 		break;
64715f729eaaSJulien Gomes 	}
64725f729eaaSJulien Gomes 	return 0;
64735f729eaaSJulien Gomes }
64745f729eaaSJulien Gomes 
64751da177e4SLinus Torvalds static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
64761da177e4SLinus Torvalds {
6477351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
6478e9dc8653SEric W. Biederman 
64791da177e4SLinus Torvalds 	switch (event) {
64805138e86fSVlad Yasevich 	case NETDEV_REBOOT:
64818a212589SXin Long 	case NETDEV_CHANGEMTU:
64823753654eSDavid Ahern 	case NETDEV_CHANGEADDR:
64835138e86fSVlad Yasevich 	case NETDEV_CHANGENAME:
64845138e86fSVlad Yasevich 	case NETDEV_FEAT_CHANGE:
64855138e86fSVlad Yasevich 	case NETDEV_BONDING_FAILOVER:
6486e6e66594SXin Long 	case NETDEV_POST_TYPE_CHANGE:
64875138e86fSVlad Yasevich 	case NETDEV_NOTIFY_PEERS:
6488dc709f37SXin Long 	case NETDEV_CHANGEUPPER:
64895138e86fSVlad Yasevich 	case NETDEV_RESEND_IGMP:
64905138e86fSVlad Yasevich 	case NETDEV_CHANGEINFODATA:
6491eeda3fb9SXin Long 	case NETDEV_CHANGELOWERSTATE:
6492ebdcf045SXin Long 	case NETDEV_CHANGE_TX_QUEUE_LEN:
64933d3ea5afSVlad Yasevich 		rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
64941d997f10SHangbin Liu 				   GFP_KERNEL, NULL, 0, 0, NULL);
64951da177e4SLinus Torvalds 		break;
64961da177e4SLinus Torvalds 	default:
64971da177e4SLinus Torvalds 		break;
64981da177e4SLinus Torvalds 	}
64991da177e4SLinus Torvalds 	return NOTIFY_DONE;
65001da177e4SLinus Torvalds }
65011da177e4SLinus Torvalds 
65021da177e4SLinus Torvalds static struct notifier_block rtnetlink_dev_notifier = {
65031da177e4SLinus Torvalds 	.notifier_call	= rtnetlink_event,
65041da177e4SLinus Torvalds };
65051da177e4SLinus Torvalds 
650697c53cacSDenis V. Lunev 
65072c8c1e72SAlexey Dobriyan static int __net_init rtnetlink_net_init(struct net *net)
650897c53cacSDenis V. Lunev {
650997c53cacSDenis V. Lunev 	struct sock *sk;
6510a31f2d17SPablo Neira Ayuso 	struct netlink_kernel_cfg cfg = {
6511a31f2d17SPablo Neira Ayuso 		.groups		= RTNLGRP_MAX,
6512a31f2d17SPablo Neira Ayuso 		.input		= rtnetlink_rcv,
6513a31f2d17SPablo Neira Ayuso 		.cb_mutex	= &rtnl_mutex,
65149785e10aSPablo Neira Ayuso 		.flags		= NL_CFG_F_NONROOT_RECV,
65155f729eaaSJulien Gomes 		.bind		= rtnetlink_bind,
6516a31f2d17SPablo Neira Ayuso 	};
6517a31f2d17SPablo Neira Ayuso 
65189f00d977SPablo Neira Ayuso 	sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
651997c53cacSDenis V. Lunev 	if (!sk)
652097c53cacSDenis V. Lunev 		return -ENOMEM;
652197c53cacSDenis V. Lunev 	net->rtnl = sk;
652297c53cacSDenis V. Lunev 	return 0;
652397c53cacSDenis V. Lunev }
652497c53cacSDenis V. Lunev 
65252c8c1e72SAlexey Dobriyan static void __net_exit rtnetlink_net_exit(struct net *net)
652697c53cacSDenis V. Lunev {
6527b7c6ba6eSDenis V. Lunev 	netlink_kernel_release(net->rtnl);
652897c53cacSDenis V. Lunev 	net->rtnl = NULL;
652997c53cacSDenis V. Lunev }
653097c53cacSDenis V. Lunev 
653197c53cacSDenis V. Lunev static struct pernet_operations rtnetlink_net_ops = {
653297c53cacSDenis V. Lunev 	.init = rtnetlink_net_init,
653397c53cacSDenis V. Lunev 	.exit = rtnetlink_net_exit,
653497c53cacSDenis V. Lunev };
653597c53cacSDenis V. Lunev 
65361da177e4SLinus Torvalds void __init rtnetlink_init(void)
65371da177e4SLinus Torvalds {
653897c53cacSDenis V. Lunev 	if (register_pernet_subsys(&rtnetlink_net_ops))
65391da177e4SLinus Torvalds 		panic("rtnetlink_init: cannot initialize rtnetlink\n");
654097c53cacSDenis V. Lunev 
65411da177e4SLinus Torvalds 	register_netdevice_notifier(&rtnetlink_dev_notifier);
6542340d17fcSThomas Graf 
6543c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
6544b97bac64SFlorian Westphal 		      rtnl_dump_ifinfo, 0);
6545b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0);
6546b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0);
6547b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0);
6548687ad8ccSThomas Graf 
6549b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0);
6550b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0);
6551b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0);
655277162022SJohn Fastabend 
655336fbf1e5SJiri Pirko 	rtnl_register(PF_UNSPEC, RTM_NEWLINKPROP, rtnl_newlinkprop, NULL, 0);
655436fbf1e5SJiri Pirko 	rtnl_register(PF_UNSPEC, RTM_DELLINKPROP, rtnl_dellinkprop, NULL, 0);
655536fbf1e5SJiri Pirko 
6556b97bac64SFlorian Westphal 	rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
65579e834259SNikolay Aleksandrov 	rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL,
65589e834259SNikolay Aleksandrov 		      RTNL_FLAG_BULK_DEL_SUPPORTED);
65595b2f94b2SRoopa Prabhu 	rtnl_register(PF_BRIDGE, RTM_GETNEIGH, rtnl_fdb_get, rtnl_fdb_dump, 0);
6560e5a55a89SJohn Fastabend 
6561b97bac64SFlorian Westphal 	rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0);
6562b97bac64SFlorian Westphal 	rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0);
6563b97bac64SFlorian Westphal 	rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0);
656410c9ead9SRoopa Prabhu 
656510c9ead9SRoopa Prabhu 	rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,
6566b97bac64SFlorian Westphal 		      0);
656703ba3566SPetr Machata 	rtnl_register(PF_UNSPEC, RTM_SETSTATS, rtnl_stats_set, NULL, 0);
6568cc7f5022SIdo Schimmel 
6569cc7f5022SIdo Schimmel 	rtnl_register(PF_BRIDGE, RTM_GETMDB, NULL, rtnl_mdb_dump, 0);
6570cc7f5022SIdo Schimmel 	rtnl_register(PF_BRIDGE, RTM_NEWMDB, rtnl_mdb_add, NULL, 0);
6571cc7f5022SIdo Schimmel 	rtnl_register(PF_BRIDGE, RTM_DELMDB, rtnl_mdb_del, NULL, 0);
65721da177e4SLinus Torvalds }
6573