xref: /openbmc/linux/net/core/rtnetlink.c (revision eec517cdb4810b3843eb7707971de3164088bff1)
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:
121da177e4SLinus Torvalds  *	Vitaly E. Lavrov		RTA_OK arithmetics was wrong.
131da177e4SLinus Torvalds  */
141da177e4SLinus Torvalds 
15ee5d032fSJakub Kicinski #include <linux/bitops.h>
161da177e4SLinus Torvalds #include <linux/errno.h>
171da177e4SLinus Torvalds #include <linux/module.h>
181da177e4SLinus Torvalds #include <linux/types.h>
191da177e4SLinus Torvalds #include <linux/socket.h>
201da177e4SLinus Torvalds #include <linux/kernel.h>
211da177e4SLinus Torvalds #include <linux/timer.h>
221da177e4SLinus Torvalds #include <linux/string.h>
231da177e4SLinus Torvalds #include <linux/sockios.h>
241da177e4SLinus Torvalds #include <linux/net.h>
251da177e4SLinus Torvalds #include <linux/fcntl.h>
261da177e4SLinus Torvalds #include <linux/mm.h>
271da177e4SLinus Torvalds #include <linux/slab.h>
281da177e4SLinus Torvalds #include <linux/interrupt.h>
291da177e4SLinus Torvalds #include <linux/capability.h>
301da177e4SLinus Torvalds #include <linux/skbuff.h>
311da177e4SLinus Torvalds #include <linux/init.h>
321da177e4SLinus Torvalds #include <linux/security.h>
336756ae4bSStephen Hemminger #include <linux/mutex.h>
341823730fSThomas Graf #include <linux/if_addr.h>
3577162022SJohn Fastabend #include <linux/if_bridge.h>
36f6f6424bSJiri Pirko #include <linux/if_vlan.h>
37ebc08a6fSWilliams, Mitch A #include <linux/pci.h>
3877162022SJohn Fastabend #include <linux/etherdevice.h>
3958038695SMartin KaFai Lau #include <linux/bpf.h>
401da177e4SLinus Torvalds 
417c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds #include <linux/inet.h>
441da177e4SLinus Torvalds #include <linux/netdevice.h>
451da177e4SLinus Torvalds #include <net/ip.h>
461da177e4SLinus Torvalds #include <net/protocol.h>
471da177e4SLinus Torvalds #include <net/arp.h>
481da177e4SLinus Torvalds #include <net/route.h>
491da177e4SLinus Torvalds #include <net/udp.h>
50ea697639SDaniel Borkmann #include <net/tcp.h>
511da177e4SLinus Torvalds #include <net/sock.h>
521da177e4SLinus Torvalds #include <net/pkt_sched.h>
5314c0b97dSThomas Graf #include <net/fib_rules.h>
54e2849863SThomas Graf #include <net/rtnetlink.h>
5530ffee84SJohannes Berg #include <net/net_namespace.h>
561da177e4SLinus Torvalds 
57a428afe8SNikolay Aleksandrov #define RTNL_MAX_TYPE		50
58ccf8dbcdSKees Cook #define RTNL_SLAVE_MAX_TYPE	36
59ccf8dbcdSKees Cook 
60e0d087afSEric Dumazet struct rtnl_link {
61e2849863SThomas Graf 	rtnl_doit_func		doit;
62e2849863SThomas Graf 	rtnl_dumpit_func	dumpit;
63e4202511SFlorian Westphal 	struct module		*owner;
6462256f98SFlorian Westphal 	unsigned int		flags;
65addf9b90SFlorian Westphal 	struct rcu_head		rcu;
66e2849863SThomas Graf };
67e2849863SThomas Graf 
686756ae4bSStephen Hemminger static DEFINE_MUTEX(rtnl_mutex);
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds void rtnl_lock(void)
711da177e4SLinus Torvalds {
726756ae4bSStephen Hemminger 	mutex_lock(&rtnl_mutex);
731da177e4SLinus Torvalds }
74e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_lock);
751da177e4SLinus Torvalds 
7679ffdfc6SKirill Tkhai int rtnl_lock_killable(void)
7779ffdfc6SKirill Tkhai {
7879ffdfc6SKirill Tkhai 	return mutex_lock_killable(&rtnl_mutex);
7979ffdfc6SKirill Tkhai }
8079ffdfc6SKirill Tkhai EXPORT_SYMBOL(rtnl_lock_killable);
8179ffdfc6SKirill Tkhai 
821b5c5493SEric Dumazet static struct sk_buff *defer_kfree_skb_list;
831b5c5493SEric Dumazet void rtnl_kfree_skbs(struct sk_buff *head, struct sk_buff *tail)
841b5c5493SEric Dumazet {
851b5c5493SEric Dumazet 	if (head && tail) {
861b5c5493SEric Dumazet 		tail->next = defer_kfree_skb_list;
871b5c5493SEric Dumazet 		defer_kfree_skb_list = head;
881b5c5493SEric Dumazet 	}
891b5c5493SEric Dumazet }
901b5c5493SEric Dumazet EXPORT_SYMBOL(rtnl_kfree_skbs);
911b5c5493SEric Dumazet 
926756ae4bSStephen Hemminger void __rtnl_unlock(void)
931da177e4SLinus Torvalds {
941b5c5493SEric Dumazet 	struct sk_buff *head = defer_kfree_skb_list;
951b5c5493SEric Dumazet 
961b5c5493SEric Dumazet 	defer_kfree_skb_list = NULL;
971b5c5493SEric Dumazet 
986756ae4bSStephen Hemminger 	mutex_unlock(&rtnl_mutex);
991b5c5493SEric Dumazet 
1001b5c5493SEric Dumazet 	while (head) {
1011b5c5493SEric Dumazet 		struct sk_buff *next = head->next;
1021b5c5493SEric Dumazet 
1031b5c5493SEric Dumazet 		kfree_skb(head);
1041b5c5493SEric Dumazet 		cond_resched();
1051b5c5493SEric Dumazet 		head = next;
1061b5c5493SEric Dumazet 	}
1071da177e4SLinus Torvalds }
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds void rtnl_unlock(void)
1101da177e4SLinus Torvalds {
11158ec3b4dSHerbert Xu 	/* This fellow will unlock it for us. */
1121da177e4SLinus Torvalds 	netdev_run_todo();
1131da177e4SLinus Torvalds }
114e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unlock);
1151da177e4SLinus Torvalds 
1166756ae4bSStephen Hemminger int rtnl_trylock(void)
1176756ae4bSStephen Hemminger {
1186756ae4bSStephen Hemminger 	return mutex_trylock(&rtnl_mutex);
1196756ae4bSStephen Hemminger }
120e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_trylock);
1216756ae4bSStephen Hemminger 
122c9c1014bSPatrick McHardy int rtnl_is_locked(void)
123c9c1014bSPatrick McHardy {
124c9c1014bSPatrick McHardy 	return mutex_is_locked(&rtnl_mutex);
125c9c1014bSPatrick McHardy }
126e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_is_locked);
127c9c1014bSPatrick McHardy 
1286f99528eSVlad Buslov bool refcount_dec_and_rtnl_lock(refcount_t *r)
1296f99528eSVlad Buslov {
1306f99528eSVlad Buslov 	return refcount_dec_and_mutex_lock(r, &rtnl_mutex);
1316f99528eSVlad Buslov }
1326f99528eSVlad Buslov EXPORT_SYMBOL(refcount_dec_and_rtnl_lock);
1336f99528eSVlad Buslov 
134a898def2SPaul E. McKenney #ifdef CONFIG_PROVE_LOCKING
1350cbf3343SYaowei Bai bool lockdep_rtnl_is_held(void)
136a898def2SPaul E. McKenney {
137a898def2SPaul E. McKenney 	return lockdep_is_held(&rtnl_mutex);
138a898def2SPaul E. McKenney }
139a898def2SPaul E. McKenney EXPORT_SYMBOL(lockdep_rtnl_is_held);
140a898def2SPaul E. McKenney #endif /* #ifdef CONFIG_PROVE_LOCKING */
141a898def2SPaul E. McKenney 
142b0e9fe1bSFlorian Westphal static struct rtnl_link *__rcu *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
143e2849863SThomas Graf 
144e2849863SThomas Graf static inline int rtm_msgindex(int msgtype)
145e2849863SThomas Graf {
146e2849863SThomas Graf 	int msgindex = msgtype - RTM_BASE;
147e2849863SThomas Graf 
148e2849863SThomas Graf 	/*
149e2849863SThomas Graf 	 * msgindex < 0 implies someone tried to register a netlink
150e2849863SThomas Graf 	 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
151e2849863SThomas Graf 	 * the message type has not been added to linux/rtnetlink.h
152e2849863SThomas Graf 	 */
153e2849863SThomas Graf 	BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
154e2849863SThomas Graf 
155e2849863SThomas Graf 	return msgindex;
156e2849863SThomas Graf }
157e2849863SThomas Graf 
158addf9b90SFlorian Westphal static struct rtnl_link *rtnl_get_link(int protocol, int msgtype)
159addf9b90SFlorian Westphal {
160addf9b90SFlorian Westphal 	struct rtnl_link **tab;
161addf9b90SFlorian Westphal 
162addf9b90SFlorian Westphal 	if (protocol >= ARRAY_SIZE(rtnl_msg_handlers))
163addf9b90SFlorian Westphal 		protocol = PF_UNSPEC;
164addf9b90SFlorian Westphal 
165addf9b90SFlorian Westphal 	tab = rcu_dereference_rtnl(rtnl_msg_handlers[protocol]);
166addf9b90SFlorian Westphal 	if (!tab)
167addf9b90SFlorian Westphal 		tab = rcu_dereference_rtnl(rtnl_msg_handlers[PF_UNSPEC]);
168addf9b90SFlorian Westphal 
169addf9b90SFlorian Westphal 	return tab[msgtype];
170addf9b90SFlorian Westphal }
171addf9b90SFlorian Westphal 
172e4202511SFlorian Westphal static int rtnl_register_internal(struct module *owner,
173e4202511SFlorian Westphal 				  int protocol, int msgtype,
174c7ac8679SGreg Rose 				  rtnl_doit_func doit, rtnl_dumpit_func dumpit,
175b97bac64SFlorian Westphal 				  unsigned int flags)
176e2849863SThomas Graf {
177b0e9fe1bSFlorian Westphal 	struct rtnl_link *link, *old;
178b0e9fe1bSFlorian Westphal 	struct rtnl_link __rcu **tab;
179e2849863SThomas Graf 	int msgindex;
180addf9b90SFlorian Westphal 	int ret = -ENOBUFS;
181e2849863SThomas Graf 
18225239ceeSPatrick McHardy 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
183e2849863SThomas Graf 	msgindex = rtm_msgindex(msgtype);
184e2849863SThomas Graf 
185addf9b90SFlorian Westphal 	rtnl_lock();
186addf9b90SFlorian Westphal 	tab = rtnl_msg_handlers[protocol];
187e2849863SThomas Graf 	if (tab == NULL) {
188addf9b90SFlorian Westphal 		tab = kcalloc(RTM_NR_MSGTYPES, sizeof(void *), GFP_KERNEL);
189addf9b90SFlorian Westphal 		if (!tab)
190addf9b90SFlorian Westphal 			goto unlock;
191e2849863SThomas Graf 
192addf9b90SFlorian Westphal 		/* ensures we see the 0 stores */
1936853dd48SFlorian Westphal 		rcu_assign_pointer(rtnl_msg_handlers[protocol], tab);
194e2849863SThomas Graf 	}
195e2849863SThomas Graf 
196addf9b90SFlorian Westphal 	old = rtnl_dereference(tab[msgindex]);
197addf9b90SFlorian Westphal 	if (old) {
198addf9b90SFlorian Westphal 		link = kmemdup(old, sizeof(*old), GFP_KERNEL);
199addf9b90SFlorian Westphal 		if (!link)
200addf9b90SFlorian Westphal 			goto unlock;
201addf9b90SFlorian Westphal 	} else {
202addf9b90SFlorian Westphal 		link = kzalloc(sizeof(*link), GFP_KERNEL);
203addf9b90SFlorian Westphal 		if (!link)
204addf9b90SFlorian Westphal 			goto unlock;
205addf9b90SFlorian Westphal 	}
206e2849863SThomas Graf 
207e4202511SFlorian Westphal 	WARN_ON(link->owner && link->owner != owner);
208e4202511SFlorian Westphal 	link->owner = owner;
209e4202511SFlorian Westphal 
210addf9b90SFlorian Westphal 	WARN_ON(doit && link->doit && link->doit != doit);
211addf9b90SFlorian Westphal 	if (doit)
212addf9b90SFlorian Westphal 		link->doit = doit;
213addf9b90SFlorian Westphal 	WARN_ON(dumpit && link->dumpit && link->dumpit != dumpit);
214addf9b90SFlorian Westphal 	if (dumpit)
215addf9b90SFlorian Westphal 		link->dumpit = dumpit;
216addf9b90SFlorian Westphal 
217addf9b90SFlorian Westphal 	link->flags |= flags;
218addf9b90SFlorian Westphal 
219addf9b90SFlorian Westphal 	/* publish protocol:msgtype */
220addf9b90SFlorian Westphal 	rcu_assign_pointer(tab[msgindex], link);
221addf9b90SFlorian Westphal 	ret = 0;
222addf9b90SFlorian Westphal 	if (old)
223addf9b90SFlorian Westphal 		kfree_rcu(old, rcu);
224addf9b90SFlorian Westphal unlock:
225addf9b90SFlorian Westphal 	rtnl_unlock();
226addf9b90SFlorian Westphal 	return ret;
227e2849863SThomas Graf }
228e4202511SFlorian Westphal 
229e4202511SFlorian Westphal /**
230e4202511SFlorian Westphal  * rtnl_register_module - Register a rtnetlink message type
231e4202511SFlorian Westphal  *
232e4202511SFlorian Westphal  * @owner: module registering the hook (THIS_MODULE)
233e4202511SFlorian Westphal  * @protocol: Protocol family or PF_UNSPEC
234e4202511SFlorian Westphal  * @msgtype: rtnetlink message type
235e4202511SFlorian Westphal  * @doit: Function pointer called for each request message
236e4202511SFlorian Westphal  * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
237e4202511SFlorian Westphal  * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
238e4202511SFlorian Westphal  *
239e4202511SFlorian Westphal  * Like rtnl_register, but for use by removable modules.
240e4202511SFlorian Westphal  */
241e4202511SFlorian Westphal int rtnl_register_module(struct module *owner,
242e4202511SFlorian Westphal 			 int protocol, int msgtype,
243e4202511SFlorian Westphal 			 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
244e4202511SFlorian Westphal 			 unsigned int flags)
245e4202511SFlorian Westphal {
246e4202511SFlorian Westphal 	return rtnl_register_internal(owner, protocol, msgtype,
247e4202511SFlorian Westphal 				      doit, dumpit, flags);
248e4202511SFlorian Westphal }
249e4202511SFlorian Westphal EXPORT_SYMBOL_GPL(rtnl_register_module);
250e4202511SFlorian Westphal 
251e4202511SFlorian Westphal /**
25216feebcfSFlorian Westphal  * rtnl_register - Register a rtnetlink message type
253e4202511SFlorian Westphal  * @protocol: Protocol family or PF_UNSPEC
254e4202511SFlorian Westphal  * @msgtype: rtnetlink message type
255e4202511SFlorian Westphal  * @doit: Function pointer called for each request message
256e4202511SFlorian Westphal  * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
257e4202511SFlorian Westphal  * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
258e4202511SFlorian Westphal  *
259e4202511SFlorian Westphal  * Registers the specified function pointers (at least one of them has
260e4202511SFlorian Westphal  * to be non-NULL) to be called whenever a request message for the
261e4202511SFlorian Westphal  * specified protocol family and message type is received.
262e4202511SFlorian Westphal  *
263e4202511SFlorian Westphal  * The special protocol family PF_UNSPEC may be used to define fallback
264e4202511SFlorian Westphal  * function pointers for the case when no entry for the specific protocol
265e4202511SFlorian Westphal  * family exists.
266e2849863SThomas Graf  */
267e2849863SThomas Graf void rtnl_register(int protocol, int msgtype,
268c7ac8679SGreg Rose 		   rtnl_doit_func doit, rtnl_dumpit_func dumpit,
269b97bac64SFlorian Westphal 		   unsigned int flags)
270e2849863SThomas Graf {
27116feebcfSFlorian Westphal 	int err;
27216feebcfSFlorian Westphal 
27316feebcfSFlorian Westphal 	err = rtnl_register_internal(NULL, protocol, msgtype, doit, dumpit,
27416feebcfSFlorian Westphal 				     flags);
27516feebcfSFlorian Westphal 	if (err)
27616feebcfSFlorian Westphal 		pr_err("Unable to register rtnetlink message handler, "
27716feebcfSFlorian Westphal 		       "protocol = %d, message type = %d\n", protocol, msgtype);
278e2849863SThomas Graf }
279e2849863SThomas Graf 
280e2849863SThomas Graf /**
281e2849863SThomas Graf  * rtnl_unregister - Unregister a rtnetlink message type
282e2849863SThomas Graf  * @protocol: Protocol family or PF_UNSPEC
283e2849863SThomas Graf  * @msgtype: rtnetlink message type
284e2849863SThomas Graf  *
285e2849863SThomas Graf  * Returns 0 on success or a negative error code.
286e2849863SThomas Graf  */
287e2849863SThomas Graf int rtnl_unregister(int protocol, int msgtype)
288e2849863SThomas Graf {
289addf9b90SFlorian Westphal 	struct rtnl_link **tab, *link;
290e2849863SThomas Graf 	int msgindex;
291e2849863SThomas Graf 
29225239ceeSPatrick McHardy 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
293e2849863SThomas Graf 	msgindex = rtm_msgindex(msgtype);
294e2849863SThomas Graf 
2956853dd48SFlorian Westphal 	rtnl_lock();
296addf9b90SFlorian Westphal 	tab = rtnl_dereference(rtnl_msg_handlers[protocol]);
297addf9b90SFlorian Westphal 	if (!tab) {
2986853dd48SFlorian Westphal 		rtnl_unlock();
299e2849863SThomas Graf 		return -ENOENT;
3006853dd48SFlorian Westphal 	}
301e2849863SThomas Graf 
302addf9b90SFlorian Westphal 	link = tab[msgindex];
303addf9b90SFlorian Westphal 	rcu_assign_pointer(tab[msgindex], NULL);
3046853dd48SFlorian Westphal 	rtnl_unlock();
305e2849863SThomas Graf 
306addf9b90SFlorian Westphal 	kfree_rcu(link, rcu);
307addf9b90SFlorian Westphal 
308e2849863SThomas Graf 	return 0;
309e2849863SThomas Graf }
310e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister);
311e2849863SThomas Graf 
312e2849863SThomas Graf /**
313e2849863SThomas Graf  * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
314e2849863SThomas Graf  * @protocol : Protocol family or PF_UNSPEC
315e2849863SThomas Graf  *
316e2849863SThomas Graf  * Identical to calling rtnl_unregster() for all registered message types
317e2849863SThomas Graf  * of a certain protocol family.
318e2849863SThomas Graf  */
319e2849863SThomas Graf void rtnl_unregister_all(int protocol)
320e2849863SThomas Graf {
321addf9b90SFlorian Westphal 	struct rtnl_link **tab, *link;
322addf9b90SFlorian Westphal 	int msgindex;
323019a3169SFlorian Westphal 
32425239ceeSPatrick McHardy 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
325e2849863SThomas Graf 
326019a3169SFlorian Westphal 	rtnl_lock();
327addf9b90SFlorian Westphal 	tab = rtnl_msg_handlers[protocol];
328f707ef61SSabrina Dubroca 	if (!tab) {
329f707ef61SSabrina Dubroca 		rtnl_unlock();
330f707ef61SSabrina Dubroca 		return;
331f707ef61SSabrina Dubroca 	}
3326853dd48SFlorian Westphal 	RCU_INIT_POINTER(rtnl_msg_handlers[protocol], NULL);
333addf9b90SFlorian Westphal 	for (msgindex = 0; msgindex < RTM_NR_MSGTYPES; msgindex++) {
334addf9b90SFlorian Westphal 		link = tab[msgindex];
335addf9b90SFlorian Westphal 		if (!link)
336addf9b90SFlorian Westphal 			continue;
337addf9b90SFlorian Westphal 
338addf9b90SFlorian Westphal 		rcu_assign_pointer(tab[msgindex], NULL);
339addf9b90SFlorian Westphal 		kfree_rcu(link, rcu);
340addf9b90SFlorian Westphal 	}
341019a3169SFlorian Westphal 	rtnl_unlock();
342019a3169SFlorian Westphal 
3436853dd48SFlorian Westphal 	synchronize_net();
3446853dd48SFlorian Westphal 
345addf9b90SFlorian Westphal 	kfree(tab);
346e2849863SThomas Graf }
347e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister_all);
3481da177e4SLinus Torvalds 
34938f7b870SPatrick McHardy static LIST_HEAD(link_ops);
35038f7b870SPatrick McHardy 
351c63044f0SEric Dumazet static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
352c63044f0SEric Dumazet {
353c63044f0SEric Dumazet 	const struct rtnl_link_ops *ops;
354c63044f0SEric Dumazet 
355c63044f0SEric Dumazet 	list_for_each_entry(ops, &link_ops, list) {
356c63044f0SEric Dumazet 		if (!strcmp(ops->kind, kind))
357c63044f0SEric Dumazet 			return ops;
358c63044f0SEric Dumazet 	}
359c63044f0SEric Dumazet 	return NULL;
360c63044f0SEric Dumazet }
361c63044f0SEric Dumazet 
36238f7b870SPatrick McHardy /**
36338f7b870SPatrick McHardy  * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
36438f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to register
36538f7b870SPatrick McHardy  *
36638f7b870SPatrick McHardy  * The caller must hold the rtnl_mutex. This function should be used
36738f7b870SPatrick McHardy  * by drivers that create devices during module initialization. It
36838f7b870SPatrick McHardy  * must be called before registering the devices.
36938f7b870SPatrick McHardy  *
37038f7b870SPatrick McHardy  * Returns 0 on success or a negative error code.
37138f7b870SPatrick McHardy  */
37238f7b870SPatrick McHardy int __rtnl_link_register(struct rtnl_link_ops *ops)
37338f7b870SPatrick McHardy {
374c63044f0SEric Dumazet 	if (rtnl_link_ops_get(ops->kind))
375c63044f0SEric Dumazet 		return -EEXIST;
376c63044f0SEric Dumazet 
377b0ab2fabSJiri Pirko 	/* The check for setup is here because if ops
378b0ab2fabSJiri Pirko 	 * does not have that filled up, it is not possible
379b0ab2fabSJiri Pirko 	 * to use the ops for creating device. So do not
380b0ab2fabSJiri Pirko 	 * fill up dellink as well. That disables rtnl_dellink.
381b0ab2fabSJiri Pirko 	 */
382b0ab2fabSJiri Pirko 	if (ops->setup && !ops->dellink)
38323289a37SEric Dumazet 		ops->dellink = unregister_netdevice_queue;
3842d85cba2SPatrick McHardy 
38538f7b870SPatrick McHardy 	list_add_tail(&ops->list, &link_ops);
38638f7b870SPatrick McHardy 	return 0;
38738f7b870SPatrick McHardy }
38838f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_register);
38938f7b870SPatrick McHardy 
39038f7b870SPatrick McHardy /**
39138f7b870SPatrick McHardy  * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
39238f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to register
39338f7b870SPatrick McHardy  *
39438f7b870SPatrick McHardy  * Returns 0 on success or a negative error code.
39538f7b870SPatrick McHardy  */
39638f7b870SPatrick McHardy int rtnl_link_register(struct rtnl_link_ops *ops)
39738f7b870SPatrick McHardy {
39838f7b870SPatrick McHardy 	int err;
39938f7b870SPatrick McHardy 
400ccf8dbcdSKees Cook 	/* Sanity-check max sizes to avoid stack buffer overflow. */
401ccf8dbcdSKees Cook 	if (WARN_ON(ops->maxtype > RTNL_MAX_TYPE ||
402ccf8dbcdSKees Cook 		    ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE))
403ccf8dbcdSKees Cook 		return -EINVAL;
404ccf8dbcdSKees Cook 
40538f7b870SPatrick McHardy 	rtnl_lock();
40638f7b870SPatrick McHardy 	err = __rtnl_link_register(ops);
40738f7b870SPatrick McHardy 	rtnl_unlock();
40838f7b870SPatrick McHardy 	return err;
40938f7b870SPatrick McHardy }
41038f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_register);
41138f7b870SPatrick McHardy 
412669f87baSPavel Emelyanov static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
413669f87baSPavel Emelyanov {
414669f87baSPavel Emelyanov 	struct net_device *dev;
41523289a37SEric Dumazet 	LIST_HEAD(list_kill);
41623289a37SEric Dumazet 
417669f87baSPavel Emelyanov 	for_each_netdev(net, dev) {
41823289a37SEric Dumazet 		if (dev->rtnl_link_ops == ops)
41923289a37SEric Dumazet 			ops->dellink(dev, &list_kill);
420669f87baSPavel Emelyanov 	}
42123289a37SEric Dumazet 	unregister_netdevice_many(&list_kill);
422669f87baSPavel Emelyanov }
423669f87baSPavel Emelyanov 
42438f7b870SPatrick McHardy /**
42538f7b870SPatrick McHardy  * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
42638f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to unregister
42738f7b870SPatrick McHardy  *
428554873e5SKirill Tkhai  * The caller must hold the rtnl_mutex and guarantee net_namespace_list
429554873e5SKirill Tkhai  * integrity (hold pernet_ops_rwsem for writing to close the race
430554873e5SKirill Tkhai  * with setup_net() and cleanup_net()).
43138f7b870SPatrick McHardy  */
43238f7b870SPatrick McHardy void __rtnl_link_unregister(struct rtnl_link_ops *ops)
43338f7b870SPatrick McHardy {
434881d966bSEric W. Biederman 	struct net *net;
4352d85cba2SPatrick McHardy 
436881d966bSEric W. Biederman 	for_each_net(net) {
437669f87baSPavel Emelyanov 		__rtnl_kill_links(net, ops);
438881d966bSEric W. Biederman 	}
43938f7b870SPatrick McHardy 	list_del(&ops->list);
44038f7b870SPatrick McHardy }
44138f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
44238f7b870SPatrick McHardy 
443200b916fSCong Wang /* Return with the rtnl_lock held when there are no network
444200b916fSCong Wang  * devices unregistering in any network namespace.
445200b916fSCong Wang  */
446200b916fSCong Wang static void rtnl_lock_unregistering_all(void)
447200b916fSCong Wang {
448200b916fSCong Wang 	struct net *net;
449200b916fSCong Wang 	bool unregistering;
450ff960a73SPeter Zijlstra 	DEFINE_WAIT_FUNC(wait, woken_wake_function);
451200b916fSCong Wang 
452ff960a73SPeter Zijlstra 	add_wait_queue(&netdev_unregistering_wq, &wait);
453200b916fSCong Wang 	for (;;) {
454200b916fSCong Wang 		unregistering = false;
455200b916fSCong Wang 		rtnl_lock();
456f0b07bb1SKirill Tkhai 		/* We held write locked pernet_ops_rwsem, and parallel
457f0b07bb1SKirill Tkhai 		 * setup_net() and cleanup_net() are not possible.
458f0b07bb1SKirill Tkhai 		 */
459200b916fSCong Wang 		for_each_net(net) {
460200b916fSCong Wang 			if (net->dev_unreg_count > 0) {
461200b916fSCong Wang 				unregistering = true;
462200b916fSCong Wang 				break;
463200b916fSCong Wang 			}
464200b916fSCong Wang 		}
465200b916fSCong Wang 		if (!unregistering)
466200b916fSCong Wang 			break;
467200b916fSCong Wang 		__rtnl_unlock();
468ff960a73SPeter Zijlstra 
469ff960a73SPeter Zijlstra 		wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
470200b916fSCong Wang 	}
471ff960a73SPeter Zijlstra 	remove_wait_queue(&netdev_unregistering_wq, &wait);
472200b916fSCong Wang }
473200b916fSCong Wang 
47438f7b870SPatrick McHardy /**
47538f7b870SPatrick McHardy  * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
47638f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to unregister
47738f7b870SPatrick McHardy  */
47838f7b870SPatrick McHardy void rtnl_link_unregister(struct rtnl_link_ops *ops)
47938f7b870SPatrick McHardy {
4808518e9bbSKirill Tkhai 	/* Close the race with setup_net() and cleanup_net() */
4814420bf21SKirill Tkhai 	down_write(&pernet_ops_rwsem);
482200b916fSCong Wang 	rtnl_lock_unregistering_all();
48338f7b870SPatrick McHardy 	__rtnl_link_unregister(ops);
48438f7b870SPatrick McHardy 	rtnl_unlock();
4854420bf21SKirill Tkhai 	up_write(&pernet_ops_rwsem);
48638f7b870SPatrick McHardy }
48738f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_unregister);
48838f7b870SPatrick McHardy 
489ba7d49b1SJiri Pirko static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
490ba7d49b1SJiri Pirko {
491ba7d49b1SJiri Pirko 	struct net_device *master_dev;
492ba7d49b1SJiri Pirko 	const struct rtnl_link_ops *ops;
4938515ae38SFlorian Westphal 	size_t size = 0;
494ba7d49b1SJiri Pirko 
4958515ae38SFlorian Westphal 	rcu_read_lock();
4968515ae38SFlorian Westphal 
4978515ae38SFlorian Westphal 	master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
498ba7d49b1SJiri Pirko 	if (!master_dev)
4998515ae38SFlorian Westphal 		goto out;
5008515ae38SFlorian Westphal 
501ba7d49b1SJiri Pirko 	ops = master_dev->rtnl_link_ops;
5026049f253SFernando Luis Vazquez Cao 	if (!ops || !ops->get_slave_size)
5038515ae38SFlorian Westphal 		goto out;
504ba7d49b1SJiri Pirko 	/* IFLA_INFO_SLAVE_DATA + nested data */
5058515ae38SFlorian Westphal 	size = nla_total_size(sizeof(struct nlattr)) +
506ba7d49b1SJiri Pirko 	       ops->get_slave_size(master_dev, dev);
5078515ae38SFlorian Westphal 
5088515ae38SFlorian Westphal out:
5098515ae38SFlorian Westphal 	rcu_read_unlock();
5108515ae38SFlorian Westphal 	return size;
511ba7d49b1SJiri Pirko }
512ba7d49b1SJiri Pirko 
51338f7b870SPatrick McHardy static size_t rtnl_link_get_size(const struct net_device *dev)
51438f7b870SPatrick McHardy {
51538f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
51638f7b870SPatrick McHardy 	size_t size;
51738f7b870SPatrick McHardy 
51838f7b870SPatrick McHardy 	if (!ops)
51938f7b870SPatrick McHardy 		return 0;
52038f7b870SPatrick McHardy 
521369cf77aSThomas Graf 	size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
522369cf77aSThomas Graf 	       nla_total_size(strlen(ops->kind) + 1);  /* IFLA_INFO_KIND */
52338f7b870SPatrick McHardy 
52438f7b870SPatrick McHardy 	if (ops->get_size)
52538f7b870SPatrick McHardy 		/* IFLA_INFO_DATA + nested data */
526369cf77aSThomas Graf 		size += nla_total_size(sizeof(struct nlattr)) +
52738f7b870SPatrick McHardy 			ops->get_size(dev);
52838f7b870SPatrick McHardy 
52938f7b870SPatrick McHardy 	if (ops->get_xstats_size)
530369cf77aSThomas Graf 		/* IFLA_INFO_XSTATS */
531369cf77aSThomas Graf 		size += nla_total_size(ops->get_xstats_size(dev));
53238f7b870SPatrick McHardy 
533ba7d49b1SJiri Pirko 	size += rtnl_link_get_slave_info_data_size(dev);
534ba7d49b1SJiri Pirko 
53538f7b870SPatrick McHardy 	return size;
53638f7b870SPatrick McHardy }
53738f7b870SPatrick McHardy 
538f8ff182cSThomas Graf static LIST_HEAD(rtnl_af_ops);
539f8ff182cSThomas Graf 
540f8ff182cSThomas Graf static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
541f8ff182cSThomas Graf {
542f8ff182cSThomas Graf 	const struct rtnl_af_ops *ops;
543f8ff182cSThomas Graf 
5445fa85a09SFlorian Westphal 	list_for_each_entry_rcu(ops, &rtnl_af_ops, list) {
545f8ff182cSThomas Graf 		if (ops->family == family)
546f8ff182cSThomas Graf 			return ops;
547f8ff182cSThomas Graf 	}
548f8ff182cSThomas Graf 
549f8ff182cSThomas Graf 	return NULL;
550f8ff182cSThomas Graf }
551f8ff182cSThomas Graf 
552f8ff182cSThomas Graf /**
553f8ff182cSThomas Graf  * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
554f8ff182cSThomas Graf  * @ops: struct rtnl_af_ops * to register
555f8ff182cSThomas Graf  *
556f8ff182cSThomas Graf  * Returns 0 on success or a negative error code.
557f8ff182cSThomas Graf  */
5583678a9d8Sstephen hemminger void rtnl_af_register(struct rtnl_af_ops *ops)
559f8ff182cSThomas Graf {
560f8ff182cSThomas Graf 	rtnl_lock();
5615fa85a09SFlorian Westphal 	list_add_tail_rcu(&ops->list, &rtnl_af_ops);
562f8ff182cSThomas Graf 	rtnl_unlock();
563f8ff182cSThomas Graf }
564f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_register);
565f8ff182cSThomas Graf 
566f8ff182cSThomas Graf /**
567f8ff182cSThomas Graf  * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
568f8ff182cSThomas Graf  * @ops: struct rtnl_af_ops * to unregister
569f8ff182cSThomas Graf  */
570f8ff182cSThomas Graf void rtnl_af_unregister(struct rtnl_af_ops *ops)
571f8ff182cSThomas Graf {
572f8ff182cSThomas Graf 	rtnl_lock();
5735fa85a09SFlorian Westphal 	list_del_rcu(&ops->list);
574f8ff182cSThomas Graf 	rtnl_unlock();
5755fa85a09SFlorian Westphal 
5765fa85a09SFlorian Westphal 	synchronize_rcu();
577f8ff182cSThomas Graf }
578f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_unregister);
579f8ff182cSThomas Graf 
580b1974ed0SArad, Ronen static size_t rtnl_link_get_af_size(const struct net_device *dev,
581b1974ed0SArad, Ronen 				    u32 ext_filter_mask)
582f8ff182cSThomas Graf {
583f8ff182cSThomas Graf 	struct rtnl_af_ops *af_ops;
584f8ff182cSThomas Graf 	size_t size;
585f8ff182cSThomas Graf 
586f8ff182cSThomas Graf 	/* IFLA_AF_SPEC */
587f8ff182cSThomas Graf 	size = nla_total_size(sizeof(struct nlattr));
588f8ff182cSThomas Graf 
5895fa85a09SFlorian Westphal 	rcu_read_lock();
5905fa85a09SFlorian Westphal 	list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
591f8ff182cSThomas Graf 		if (af_ops->get_link_af_size) {
592f8ff182cSThomas Graf 			/* AF_* + nested data */
593f8ff182cSThomas Graf 			size += nla_total_size(sizeof(struct nlattr)) +
594b1974ed0SArad, Ronen 				af_ops->get_link_af_size(dev, ext_filter_mask);
595f8ff182cSThomas Graf 		}
596f8ff182cSThomas Graf 	}
5975fa85a09SFlorian Westphal 	rcu_read_unlock();
598f8ff182cSThomas Graf 
599f8ff182cSThomas Graf 	return size;
600f8ff182cSThomas Graf }
601f8ff182cSThomas Graf 
602ba7d49b1SJiri Pirko static bool rtnl_have_link_slave_info(const struct net_device *dev)
603ba7d49b1SJiri Pirko {
604ba7d49b1SJiri Pirko 	struct net_device *master_dev;
6054c82a95eSFlorian Westphal 	bool ret = false;
606ba7d49b1SJiri Pirko 
6074c82a95eSFlorian Westphal 	rcu_read_lock();
6084c82a95eSFlorian Westphal 
6094c82a95eSFlorian Westphal 	master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
610813f020cSJiri Pirko 	if (master_dev && master_dev->rtnl_link_ops)
6114c82a95eSFlorian Westphal 		ret = true;
6124c82a95eSFlorian Westphal 	rcu_read_unlock();
6134c82a95eSFlorian Westphal 	return ret;
614ba7d49b1SJiri Pirko }
615ba7d49b1SJiri Pirko 
616ba7d49b1SJiri Pirko static int rtnl_link_slave_info_fill(struct sk_buff *skb,
617ba7d49b1SJiri Pirko 				     const struct net_device *dev)
618ba7d49b1SJiri Pirko {
619ba7d49b1SJiri Pirko 	struct net_device *master_dev;
620ba7d49b1SJiri Pirko 	const struct rtnl_link_ops *ops;
621ba7d49b1SJiri Pirko 	struct nlattr *slave_data;
622ba7d49b1SJiri Pirko 	int err;
623ba7d49b1SJiri Pirko 
624ba7d49b1SJiri Pirko 	master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
625ba7d49b1SJiri Pirko 	if (!master_dev)
626ba7d49b1SJiri Pirko 		return 0;
627ba7d49b1SJiri Pirko 	ops = master_dev->rtnl_link_ops;
628ba7d49b1SJiri Pirko 	if (!ops)
629ba7d49b1SJiri Pirko 		return 0;
630ba7d49b1SJiri Pirko 	if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
631ba7d49b1SJiri Pirko 		return -EMSGSIZE;
632ba7d49b1SJiri Pirko 	if (ops->fill_slave_info) {
633ae0be8deSMichal Kubecek 		slave_data = nla_nest_start_noflag(skb, IFLA_INFO_SLAVE_DATA);
634ba7d49b1SJiri Pirko 		if (!slave_data)
635ba7d49b1SJiri Pirko 			return -EMSGSIZE;
636ba7d49b1SJiri Pirko 		err = ops->fill_slave_info(skb, master_dev, dev);
637ba7d49b1SJiri Pirko 		if (err < 0)
638ba7d49b1SJiri Pirko 			goto err_cancel_slave_data;
639ba7d49b1SJiri Pirko 		nla_nest_end(skb, slave_data);
640ba7d49b1SJiri Pirko 	}
641ba7d49b1SJiri Pirko 	return 0;
642ba7d49b1SJiri Pirko 
643ba7d49b1SJiri Pirko err_cancel_slave_data:
644ba7d49b1SJiri Pirko 	nla_nest_cancel(skb, slave_data);
645ba7d49b1SJiri Pirko 	return err;
646ba7d49b1SJiri Pirko }
647ba7d49b1SJiri Pirko 
648ba7d49b1SJiri Pirko static int rtnl_link_info_fill(struct sk_buff *skb,
649ba7d49b1SJiri Pirko 			       const struct net_device *dev)
65038f7b870SPatrick McHardy {
65138f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
652ba7d49b1SJiri Pirko 	struct nlattr *data;
653ba7d49b1SJiri Pirko 	int err;
654ba7d49b1SJiri Pirko 
655ba7d49b1SJiri Pirko 	if (!ops)
656ba7d49b1SJiri Pirko 		return 0;
657ba7d49b1SJiri Pirko 	if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
658ba7d49b1SJiri Pirko 		return -EMSGSIZE;
659ba7d49b1SJiri Pirko 	if (ops->fill_xstats) {
660ba7d49b1SJiri Pirko 		err = ops->fill_xstats(skb, dev);
661ba7d49b1SJiri Pirko 		if (err < 0)
662ba7d49b1SJiri Pirko 			return err;
663ba7d49b1SJiri Pirko 	}
664ba7d49b1SJiri Pirko 	if (ops->fill_info) {
665ae0be8deSMichal Kubecek 		data = nla_nest_start_noflag(skb, IFLA_INFO_DATA);
666ba7d49b1SJiri Pirko 		if (data == NULL)
667ba7d49b1SJiri Pirko 			return -EMSGSIZE;
668ba7d49b1SJiri Pirko 		err = ops->fill_info(skb, dev);
669ba7d49b1SJiri Pirko 		if (err < 0)
670ba7d49b1SJiri Pirko 			goto err_cancel_data;
671ba7d49b1SJiri Pirko 		nla_nest_end(skb, data);
672ba7d49b1SJiri Pirko 	}
673ba7d49b1SJiri Pirko 	return 0;
674ba7d49b1SJiri Pirko 
675ba7d49b1SJiri Pirko err_cancel_data:
676ba7d49b1SJiri Pirko 	nla_nest_cancel(skb, data);
677ba7d49b1SJiri Pirko 	return err;
678ba7d49b1SJiri Pirko }
679ba7d49b1SJiri Pirko 
680ba7d49b1SJiri Pirko static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
681ba7d49b1SJiri Pirko {
682ba7d49b1SJiri Pirko 	struct nlattr *linkinfo;
68338f7b870SPatrick McHardy 	int err = -EMSGSIZE;
68438f7b870SPatrick McHardy 
685ae0be8deSMichal Kubecek 	linkinfo = nla_nest_start_noflag(skb, IFLA_LINKINFO);
68638f7b870SPatrick McHardy 	if (linkinfo == NULL)
68738f7b870SPatrick McHardy 		goto out;
68838f7b870SPatrick McHardy 
689ba7d49b1SJiri Pirko 	err = rtnl_link_info_fill(skb, dev);
69038f7b870SPatrick McHardy 	if (err < 0)
69138f7b870SPatrick McHardy 		goto err_cancel_link;
692ba7d49b1SJiri Pirko 
693ba7d49b1SJiri Pirko 	err = rtnl_link_slave_info_fill(skb, dev);
69438f7b870SPatrick McHardy 	if (err < 0)
695ba7d49b1SJiri Pirko 		goto err_cancel_link;
69638f7b870SPatrick McHardy 
69738f7b870SPatrick McHardy 	nla_nest_end(skb, linkinfo);
69838f7b870SPatrick McHardy 	return 0;
69938f7b870SPatrick McHardy 
70038f7b870SPatrick McHardy err_cancel_link:
70138f7b870SPatrick McHardy 	nla_nest_cancel(skb, linkinfo);
70238f7b870SPatrick McHardy out:
70338f7b870SPatrick McHardy 	return err;
70438f7b870SPatrick McHardy }
70538f7b870SPatrick McHardy 
70695c96174SEric Dumazet int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
7071da177e4SLinus Torvalds {
70897c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
7091da177e4SLinus Torvalds 	int err = 0;
7101da177e4SLinus Torvalds 
711ac6d439dSPatrick McHardy 	NETLINK_CB(skb).dst_group = group;
7121da177e4SLinus Torvalds 	if (echo)
71363354797SReshetova, Elena 		refcount_inc(&skb->users);
7141da177e4SLinus Torvalds 	netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
7151da177e4SLinus Torvalds 	if (echo)
7161da177e4SLinus Torvalds 		err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
7171da177e4SLinus Torvalds 	return err;
7181da177e4SLinus Torvalds }
7191da177e4SLinus Torvalds 
72097c53cacSDenis V. Lunev int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
7212942e900SThomas Graf {
72297c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
72397c53cacSDenis V. Lunev 
7242942e900SThomas Graf 	return nlmsg_unicast(rtnl, skb, pid);
7252942e900SThomas Graf }
726e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unicast);
7272942e900SThomas Graf 
7281ce85fe4SPablo Neira Ayuso void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
72997676b6bSThomas Graf 		 struct nlmsghdr *nlh, gfp_t flags)
73097676b6bSThomas Graf {
73197c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
73297676b6bSThomas Graf 	int report = 0;
73397676b6bSThomas Graf 
73497676b6bSThomas Graf 	if (nlh)
73597676b6bSThomas Graf 		report = nlmsg_report(nlh);
73697676b6bSThomas Graf 
7371ce85fe4SPablo Neira Ayuso 	nlmsg_notify(rtnl, skb, pid, group, report, flags);
73897676b6bSThomas Graf }
739e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_notify);
74097676b6bSThomas Graf 
74197c53cacSDenis V. Lunev void rtnl_set_sk_err(struct net *net, u32 group, int error)
74297676b6bSThomas Graf {
74397c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
74497c53cacSDenis V. Lunev 
74597676b6bSThomas Graf 	netlink_set_err(rtnl, 0, group, error);
74697676b6bSThomas Graf }
747e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_set_sk_err);
74897676b6bSThomas Graf 
7491da177e4SLinus Torvalds int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
7501da177e4SLinus Torvalds {
7512d7202bfSThomas Graf 	struct nlattr *mx;
7522d7202bfSThomas Graf 	int i, valid = 0;
7531da177e4SLinus Torvalds 
754c22a133aSDavid Ahern 	/* nothing is dumped for dst_default_metrics, so just skip the loop */
755c22a133aSDavid Ahern 	if (metrics == dst_default_metrics.metrics)
756c22a133aSDavid Ahern 		return 0;
757c22a133aSDavid Ahern 
758ae0be8deSMichal Kubecek 	mx = nla_nest_start_noflag(skb, RTA_METRICS);
7592d7202bfSThomas Graf 	if (mx == NULL)
7602d7202bfSThomas Graf 		return -ENOBUFS;
7612d7202bfSThomas Graf 
7621da177e4SLinus Torvalds 	for (i = 0; i < RTAX_MAX; i++) {
7632d7202bfSThomas Graf 		if (metrics[i]) {
764ea697639SDaniel Borkmann 			if (i == RTAX_CC_ALGO - 1) {
765ea697639SDaniel Borkmann 				char tmp[TCP_CA_NAME_MAX], *name;
766ea697639SDaniel Borkmann 
767ea697639SDaniel Borkmann 				name = tcp_ca_get_name_by_key(metrics[i], tmp);
768ea697639SDaniel Borkmann 				if (!name)
769ea697639SDaniel Borkmann 					continue;
770ea697639SDaniel Borkmann 				if (nla_put_string(skb, i + 1, name))
771ea697639SDaniel Borkmann 					goto nla_put_failure;
772c3a8d947SDaniel Borkmann 			} else if (i == RTAX_FEATURES - 1) {
773c3a8d947SDaniel Borkmann 				u32 user_features = metrics[i] & RTAX_FEATURE_MASK;
774c3a8d947SDaniel Borkmann 
775f8edcd12SPhil Sutter 				if (!user_features)
776f8edcd12SPhil Sutter 					continue;
777c3a8d947SDaniel Borkmann 				BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK);
778c3a8d947SDaniel Borkmann 				if (nla_put_u32(skb, i + 1, user_features))
779c3a8d947SDaniel Borkmann 					goto nla_put_failure;
780ea697639SDaniel Borkmann 			} else {
781a6574349SDavid S. Miller 				if (nla_put_u32(skb, i + 1, metrics[i]))
782a6574349SDavid S. Miller 					goto nla_put_failure;
7831da177e4SLinus Torvalds 			}
784ea697639SDaniel Borkmann 			valid++;
785ea697639SDaniel Borkmann 		}
7862d7202bfSThomas Graf 	}
7871da177e4SLinus Torvalds 
788a57d27fcSDavid S. Miller 	if (!valid) {
789a57d27fcSDavid S. Miller 		nla_nest_cancel(skb, mx);
790a57d27fcSDavid S. Miller 		return 0;
791a57d27fcSDavid S. Miller 	}
7922d7202bfSThomas Graf 
7932d7202bfSThomas Graf 	return nla_nest_end(skb, mx);
7942d7202bfSThomas Graf 
7952d7202bfSThomas Graf nla_put_failure:
796bc3ed28cSThomas Graf 	nla_nest_cancel(skb, mx);
797bc3ed28cSThomas Graf 	return -EMSGSIZE;
7981da177e4SLinus Torvalds }
799e0d087afSEric Dumazet EXPORT_SYMBOL(rtnetlink_put_metrics);
8001da177e4SLinus Torvalds 
801e3703b3dSThomas Graf int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
80287a50699SDavid S. Miller 		       long expires, u32 error)
803e3703b3dSThomas Graf {
804e3703b3dSThomas Graf 	struct rta_cacheinfo ci = {
805e3703b3dSThomas Graf 		.rta_error = error,
806e3703b3dSThomas Graf 		.rta_id =  id,
807e3703b3dSThomas Graf 	};
808e3703b3dSThomas Graf 
8093940746dSDavid Ahern 	if (dst) {
8103940746dSDavid Ahern 		ci.rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse);
8113940746dSDavid Ahern 		ci.rta_used = dst->__use;
8123940746dSDavid Ahern 		ci.rta_clntref = atomic_read(&dst->__refcnt);
8133940746dSDavid Ahern 	}
8148253947eSLi Wei 	if (expires) {
8158253947eSLi Wei 		unsigned long clock;
816e3703b3dSThomas Graf 
8178253947eSLi Wei 		clock = jiffies_to_clock_t(abs(expires));
8188253947eSLi Wei 		clock = min_t(unsigned long, clock, INT_MAX);
8198253947eSLi Wei 		ci.rta_expires = (expires > 0) ? clock : -clock;
8208253947eSLi Wei 	}
821e3703b3dSThomas Graf 	return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
822e3703b3dSThomas Graf }
823e3703b3dSThomas Graf EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
8241da177e4SLinus Torvalds 
82593b2d4a2SDavid S. Miller static void set_operstate(struct net_device *dev, unsigned char transition)
826b00055aaSStefan Rompf {
827b00055aaSStefan Rompf 	unsigned char operstate = dev->operstate;
828b00055aaSStefan Rompf 
829b00055aaSStefan Rompf 	switch (transition) {
830b00055aaSStefan Rompf 	case IF_OPER_UP:
831b00055aaSStefan Rompf 		if ((operstate == IF_OPER_DORMANT ||
832*eec517cdSAndrew Lunn 		     operstate == IF_OPER_TESTING ||
833b00055aaSStefan Rompf 		     operstate == IF_OPER_UNKNOWN) &&
834*eec517cdSAndrew Lunn 		    !netif_dormant(dev) && !netif_testing(dev))
835b00055aaSStefan Rompf 			operstate = IF_OPER_UP;
836b00055aaSStefan Rompf 		break;
837b00055aaSStefan Rompf 
838*eec517cdSAndrew Lunn 	case IF_OPER_TESTING:
839*eec517cdSAndrew Lunn 		if (operstate == IF_OPER_UP ||
840*eec517cdSAndrew Lunn 		    operstate == IF_OPER_UNKNOWN)
841*eec517cdSAndrew Lunn 			operstate = IF_OPER_TESTING;
842*eec517cdSAndrew Lunn 		break;
843*eec517cdSAndrew Lunn 
844b00055aaSStefan Rompf 	case IF_OPER_DORMANT:
845b00055aaSStefan Rompf 		if (operstate == IF_OPER_UP ||
846b00055aaSStefan Rompf 		    operstate == IF_OPER_UNKNOWN)
847b00055aaSStefan Rompf 			operstate = IF_OPER_DORMANT;
848b00055aaSStefan Rompf 		break;
8493ff50b79SStephen Hemminger 	}
850b00055aaSStefan Rompf 
851b00055aaSStefan Rompf 	if (dev->operstate != operstate) {
852b00055aaSStefan Rompf 		write_lock_bh(&dev_base_lock);
853b00055aaSStefan Rompf 		dev->operstate = operstate;
854b00055aaSStefan Rompf 		write_unlock_bh(&dev_base_lock);
855b00055aaSStefan Rompf 		netdev_state_change(dev);
85693b2d4a2SDavid S. Miller 	}
857b00055aaSStefan Rompf }
858b00055aaSStefan Rompf 
859b1beb681SJiri Benc static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
860b1beb681SJiri Benc {
861b1beb681SJiri Benc 	return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
862b1beb681SJiri Benc 	       (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
863b1beb681SJiri Benc }
864b1beb681SJiri Benc 
8653729d502SPatrick McHardy static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
8663729d502SPatrick McHardy 					   const struct ifinfomsg *ifm)
8673729d502SPatrick McHardy {
8683729d502SPatrick McHardy 	unsigned int flags = ifm->ifi_flags;
8693729d502SPatrick McHardy 
8703729d502SPatrick McHardy 	/* bugwards compatibility: ifi_change == 0 is treated as ~0 */
8713729d502SPatrick McHardy 	if (ifm->ifi_change)
8723729d502SPatrick McHardy 		flags = (flags & ifm->ifi_change) |
873b1beb681SJiri Benc 			(rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
8743729d502SPatrick McHardy 
8753729d502SPatrick McHardy 	return flags;
8763729d502SPatrick McHardy }
8773729d502SPatrick McHardy 
878b60c5115SThomas Graf static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
879be1f3c2cSBen Hutchings 				 const struct rtnl_link_stats64 *b)
8801da177e4SLinus Torvalds {
881b60c5115SThomas Graf 	a->rx_packets = b->rx_packets;
882b60c5115SThomas Graf 	a->tx_packets = b->tx_packets;
883b60c5115SThomas Graf 	a->rx_bytes = b->rx_bytes;
884b60c5115SThomas Graf 	a->tx_bytes = b->tx_bytes;
885b60c5115SThomas Graf 	a->rx_errors = b->rx_errors;
886b60c5115SThomas Graf 	a->tx_errors = b->tx_errors;
887b60c5115SThomas Graf 	a->rx_dropped = b->rx_dropped;
888b60c5115SThomas Graf 	a->tx_dropped = b->tx_dropped;
889b60c5115SThomas Graf 
890b60c5115SThomas Graf 	a->multicast = b->multicast;
891b60c5115SThomas Graf 	a->collisions = b->collisions;
892b60c5115SThomas Graf 
893b60c5115SThomas Graf 	a->rx_length_errors = b->rx_length_errors;
894b60c5115SThomas Graf 	a->rx_over_errors = b->rx_over_errors;
895b60c5115SThomas Graf 	a->rx_crc_errors = b->rx_crc_errors;
896b60c5115SThomas Graf 	a->rx_frame_errors = b->rx_frame_errors;
897b60c5115SThomas Graf 	a->rx_fifo_errors = b->rx_fifo_errors;
898b60c5115SThomas Graf 	a->rx_missed_errors = b->rx_missed_errors;
899b60c5115SThomas Graf 
900b60c5115SThomas Graf 	a->tx_aborted_errors = b->tx_aborted_errors;
901b60c5115SThomas Graf 	a->tx_carrier_errors = b->tx_carrier_errors;
902b60c5115SThomas Graf 	a->tx_fifo_errors = b->tx_fifo_errors;
903b60c5115SThomas Graf 	a->tx_heartbeat_errors = b->tx_heartbeat_errors;
904b60c5115SThomas Graf 	a->tx_window_errors = b->tx_window_errors;
905b60c5115SThomas Graf 
906b60c5115SThomas Graf 	a->rx_compressed = b->rx_compressed;
907b60c5115SThomas Graf 	a->tx_compressed = b->tx_compressed;
9086e7333d3SJarod Wilson 
9096e7333d3SJarod Wilson 	a->rx_nohandler = b->rx_nohandler;
91010708f37SJan Engelhardt }
91110708f37SJan Engelhardt 
912c02db8c6SChris Wright /* All VF info */
913115c9b81SGreg Rose static inline int rtnl_vfinfo_size(const struct net_device *dev,
914115c9b81SGreg Rose 				   u32 ext_filter_mask)
915ebc08a6fSWilliams, Mitch A {
9169af15c38SPhil Sutter 	if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) {
917c02db8c6SChris Wright 		int num_vfs = dev_num_vf(dev->dev.parent);
9187e75f74aSSabrina Dubroca 		size_t size = nla_total_size(0);
919045de01aSScott Feldman 		size += num_vfs *
9207e75f74aSSabrina Dubroca 			(nla_total_size(0) +
9217e75f74aSSabrina Dubroca 			 nla_total_size(sizeof(struct ifla_vf_mac)) +
92275345f88SDenis Kirjanov 			 nla_total_size(sizeof(struct ifla_vf_broadcast)) +
9237e75f74aSSabrina Dubroca 			 nla_total_size(sizeof(struct ifla_vf_vlan)) +
9247e75f74aSSabrina Dubroca 			 nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
92579aab093SMoshe Shemesh 			 nla_total_size(MAX_VLAN_LIST_LEN *
92679aab093SMoshe Shemesh 					sizeof(struct ifla_vf_vlan_info)) +
927ed616689SSucheta Chakraborty 			 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
9287e75f74aSSabrina Dubroca 			 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
929945a3676SJiri Benc 			 nla_total_size(sizeof(struct ifla_vf_rate)) +
93001a3d796SVlad Zolotarov 			 nla_total_size(sizeof(struct ifla_vf_link_state)) +
9313b766cd8SEran Ben Elisha 			 nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
9327e75f74aSSabrina Dubroca 			 nla_total_size(0) + /* nest IFLA_VF_STATS */
9333b766cd8SEran Ben Elisha 			 /* IFLA_VF_STATS_RX_PACKETS */
934343a6d8eSNicolas Dichtel 			 nla_total_size_64bit(sizeof(__u64)) +
9353b766cd8SEran Ben Elisha 			 /* IFLA_VF_STATS_TX_PACKETS */
936343a6d8eSNicolas Dichtel 			 nla_total_size_64bit(sizeof(__u64)) +
9373b766cd8SEran Ben Elisha 			 /* IFLA_VF_STATS_RX_BYTES */
938343a6d8eSNicolas Dichtel 			 nla_total_size_64bit(sizeof(__u64)) +
9393b766cd8SEran Ben Elisha 			 /* IFLA_VF_STATS_TX_BYTES */
940343a6d8eSNicolas Dichtel 			 nla_total_size_64bit(sizeof(__u64)) +
9413b766cd8SEran Ben Elisha 			 /* IFLA_VF_STATS_BROADCAST */
942343a6d8eSNicolas Dichtel 			 nla_total_size_64bit(sizeof(__u64)) +
9433b766cd8SEran Ben Elisha 			 /* IFLA_VF_STATS_MULTICAST */
944343a6d8eSNicolas Dichtel 			 nla_total_size_64bit(sizeof(__u64)) +
945c5a9f6f0SEugenia Emantayev 			 /* IFLA_VF_STATS_RX_DROPPED */
946c5a9f6f0SEugenia Emantayev 			 nla_total_size_64bit(sizeof(__u64)) +
947c5a9f6f0SEugenia Emantayev 			 /* IFLA_VF_STATS_TX_DROPPED */
948c5a9f6f0SEugenia Emantayev 			 nla_total_size_64bit(sizeof(__u64)) +
949dd461d6aSHiroshi Shimamoto 			 nla_total_size(sizeof(struct ifla_vf_trust)));
950c02db8c6SChris Wright 		return size;
951c02db8c6SChris Wright 	} else
952ebc08a6fSWilliams, Mitch A 		return 0;
953ebc08a6fSWilliams, Mitch A }
954ebc08a6fSWilliams, Mitch A 
955c53864fdSDavid Gibson static size_t rtnl_port_size(const struct net_device *dev,
956c53864fdSDavid Gibson 			     u32 ext_filter_mask)
95757b61080SScott Feldman {
95857b61080SScott Feldman 	size_t port_size = nla_total_size(4)		/* PORT_VF */
95957b61080SScott Feldman 		+ nla_total_size(PORT_PROFILE_MAX)	/* PORT_PROFILE */
96057b61080SScott Feldman 		+ nla_total_size(PORT_UUID_MAX)		/* PORT_INSTANCE_UUID */
96157b61080SScott Feldman 		+ nla_total_size(PORT_UUID_MAX)		/* PORT_HOST_UUID */
96257b61080SScott Feldman 		+ nla_total_size(1)			/* PROT_VDP_REQUEST */
96357b61080SScott Feldman 		+ nla_total_size(2);			/* PORT_VDP_RESPONSE */
96457b61080SScott Feldman 	size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
96557b61080SScott Feldman 	size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
96657b61080SScott Feldman 		+ port_size;
96757b61080SScott Feldman 	size_t port_self_size = nla_total_size(sizeof(struct nlattr))
96857b61080SScott Feldman 		+ port_size;
96957b61080SScott Feldman 
970c53864fdSDavid Gibson 	if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
971c53864fdSDavid Gibson 	    !(ext_filter_mask & RTEXT_FILTER_VF))
97257b61080SScott Feldman 		return 0;
97357b61080SScott Feldman 	if (dev_num_vf(dev->dev.parent))
97457b61080SScott Feldman 		return port_self_size + vf_ports_size +
97557b61080SScott Feldman 			vf_port_size * dev_num_vf(dev->dev.parent);
97657b61080SScott Feldman 	else
97757b61080SScott Feldman 		return port_self_size;
97857b61080SScott Feldman }
97957b61080SScott Feldman 
980b5cdae32SDavid S. Miller static size_t rtnl_xdp_size(void)
981d1fdd913SBrenden Blanco {
982b3cfaa31SSabrina Dubroca 	size_t xdp_size = nla_total_size(0) +	/* nest IFLA_XDP */
98358038695SMartin KaFai Lau 			  nla_total_size(1) +	/* XDP_ATTACHED */
984a25717d2SJakub Kicinski 			  nla_total_size(4) +	/* XDP_PROG_ID (or 1st mode) */
9854f91da26SJakub Kicinski 			  nla_total_size(4);	/* XDP_<mode>_PROG_ID */
986d1fdd913SBrenden Blanco 
987d1fdd913SBrenden Blanco 	return xdp_size;
988d1fdd913SBrenden Blanco }
989d1fdd913SBrenden Blanco 
99088f4fb0cSJiri Pirko static size_t rtnl_prop_list_size(const struct net_device *dev)
99188f4fb0cSJiri Pirko {
99288f4fb0cSJiri Pirko 	struct netdev_name_node *name_node;
99388f4fb0cSJiri Pirko 	size_t size;
99488f4fb0cSJiri Pirko 
99588f4fb0cSJiri Pirko 	if (list_empty(&dev->name_node->list))
99688f4fb0cSJiri Pirko 		return 0;
99788f4fb0cSJiri Pirko 	size = nla_total_size(0);
99888f4fb0cSJiri Pirko 	list_for_each_entry(name_node, &dev->name_node->list, list)
99988f4fb0cSJiri Pirko 		size += nla_total_size(ALTIFNAMSIZ);
100088f4fb0cSJiri Pirko 	return size;
100188f4fb0cSJiri Pirko }
100288f4fb0cSJiri Pirko 
1003115c9b81SGreg Rose static noinline size_t if_nlmsg_size(const struct net_device *dev,
1004115c9b81SGreg Rose 				     u32 ext_filter_mask)
1005339bf98fSThomas Graf {
1006339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
1007339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
10080b815a1aSStephen Hemminger 	       + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
1009339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
1010270cb4d0SNicolas Dichtel 	       + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap))
1011339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rtnl_link_stats))
101235c58459SDavid S. Miller 	       + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
1013339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
1014339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
1015339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_TXQLEN */
1016339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_WEIGHT */
1017339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MTU */
1018339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_LINK */
1019339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MASTER */
10209a57247fSJiri Pirko 	       + nla_total_size(1) /* IFLA_CARRIER */
1021edbc0bb3SBen Greear 	       + nla_total_size(4) /* IFLA_PROMISCUITY */
102276ff5cc9SJiri Pirko 	       + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
102376ff5cc9SJiri Pirko 	       + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
10246919756cSTobias Klauser 	       + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
10256919756cSTobias Klauser 	       + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
1026339bf98fSThomas Graf 	       + nla_total_size(1) /* IFLA_OPERSTATE */
102738f7b870SPatrick McHardy 	       + nla_total_size(1) /* IFLA_LINKMODE */
10282d3b479dSdavid decotigny 	       + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
1029d37512a2SNicolas Dichtel 	       + nla_total_size(4) /* IFLA_LINK_NETNSID */
1030db833d40SSerhey Popovych 	       + nla_total_size(4) /* IFLA_GROUP */
1031115c9b81SGreg Rose 	       + nla_total_size(ext_filter_mask
1032115c9b81SGreg Rose 			        & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
1033115c9b81SGreg Rose 	       + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
1034c53864fdSDavid Gibson 	       + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
1035f8ff182cSThomas Graf 	       + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
1036b1974ed0SArad, Ronen 	       + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
103782f28412SJiri Pirko 	       + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
103888d6378bSAnuradha Karuppiah 	       + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
1039c57c7a95SNicolas Dichtel 	       + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
1040b5cdae32SDavid S. Miller 	       + rtnl_xdp_size() /* IFLA_XDP */
10413d3ea5afSVlad Yasevich 	       + nla_total_size(4)  /* IFLA_EVENT */
10426621dd29SNicolas Dichtel 	       + nla_total_size(4)  /* IFLA_NEW_NETNSID */
104338e01b30SNicolas Dichtel 	       + nla_total_size(4)  /* IFLA_NEW_IFINDEX */
104403ac738dSColin Ian King 	       + nla_total_size(1)  /* IFLA_PROTO_DOWN */
10457e4a8d5aSChristian Brauner 	       + nla_total_size(4)  /* IFLA_TARGET_NETNSID */
1046b2d3bcfaSDavid Decotigny 	       + nla_total_size(4)  /* IFLA_CARRIER_UP_COUNT */
1047b2d3bcfaSDavid Decotigny 	       + nla_total_size(4)  /* IFLA_CARRIER_DOWN_COUNT */
10483e7a50ceSStephen Hemminger 	       + nla_total_size(4)  /* IFLA_MIN_MTU */
10493e7a50ceSStephen Hemminger 	       + nla_total_size(4)  /* IFLA_MAX_MTU */
105088f4fb0cSJiri Pirko 	       + rtnl_prop_list_size(dev)
1051f74877a5SMichal Kubecek 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_PERM_ADDRESS */
105279e1ad14SJiri Benc 	       + 0;
1053339bf98fSThomas Graf }
1054339bf98fSThomas Graf 
105557b61080SScott Feldman static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
105657b61080SScott Feldman {
105757b61080SScott Feldman 	struct nlattr *vf_ports;
105857b61080SScott Feldman 	struct nlattr *vf_port;
105957b61080SScott Feldman 	int vf;
106057b61080SScott Feldman 	int err;
106157b61080SScott Feldman 
1062ae0be8deSMichal Kubecek 	vf_ports = nla_nest_start_noflag(skb, IFLA_VF_PORTS);
106357b61080SScott Feldman 	if (!vf_ports)
106457b61080SScott Feldman 		return -EMSGSIZE;
106557b61080SScott Feldman 
106657b61080SScott Feldman 	for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
1067ae0be8deSMichal Kubecek 		vf_port = nla_nest_start_noflag(skb, IFLA_VF_PORT);
10688ca94183SScott Feldman 		if (!vf_port)
10698ca94183SScott Feldman 			goto nla_put_failure;
1070a6574349SDavid S. Miller 		if (nla_put_u32(skb, IFLA_PORT_VF, vf))
1071a6574349SDavid S. Miller 			goto nla_put_failure;
107257b61080SScott Feldman 		err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
10738ca94183SScott Feldman 		if (err == -EMSGSIZE)
10748ca94183SScott Feldman 			goto nla_put_failure;
107557b61080SScott Feldman 		if (err) {
107657b61080SScott Feldman 			nla_nest_cancel(skb, vf_port);
107757b61080SScott Feldman 			continue;
107857b61080SScott Feldman 		}
107957b61080SScott Feldman 		nla_nest_end(skb, vf_port);
108057b61080SScott Feldman 	}
108157b61080SScott Feldman 
108257b61080SScott Feldman 	nla_nest_end(skb, vf_ports);
108357b61080SScott Feldman 
108457b61080SScott Feldman 	return 0;
10858ca94183SScott Feldman 
10868ca94183SScott Feldman nla_put_failure:
10878ca94183SScott Feldman 	nla_nest_cancel(skb, vf_ports);
10888ca94183SScott Feldman 	return -EMSGSIZE;
108957b61080SScott Feldman }
109057b61080SScott Feldman 
109157b61080SScott Feldman static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
109257b61080SScott Feldman {
109357b61080SScott Feldman 	struct nlattr *port_self;
109457b61080SScott Feldman 	int err;
109557b61080SScott Feldman 
1096ae0be8deSMichal Kubecek 	port_self = nla_nest_start_noflag(skb, IFLA_PORT_SELF);
109757b61080SScott Feldman 	if (!port_self)
109857b61080SScott Feldman 		return -EMSGSIZE;
109957b61080SScott Feldman 
110057b61080SScott Feldman 	err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
110157b61080SScott Feldman 	if (err) {
110257b61080SScott Feldman 		nla_nest_cancel(skb, port_self);
11038ca94183SScott Feldman 		return (err == -EMSGSIZE) ? err : 0;
110457b61080SScott Feldman 	}
110557b61080SScott Feldman 
110657b61080SScott Feldman 	nla_nest_end(skb, port_self);
110757b61080SScott Feldman 
110857b61080SScott Feldman 	return 0;
110957b61080SScott Feldman }
111057b61080SScott Feldman 
1111c53864fdSDavid Gibson static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
1112c53864fdSDavid Gibson 			  u32 ext_filter_mask)
111357b61080SScott Feldman {
111457b61080SScott Feldman 	int err;
111557b61080SScott Feldman 
1116c53864fdSDavid Gibson 	if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
1117c53864fdSDavid Gibson 	    !(ext_filter_mask & RTEXT_FILTER_VF))
111857b61080SScott Feldman 		return 0;
111957b61080SScott Feldman 
112057b61080SScott Feldman 	err = rtnl_port_self_fill(skb, dev);
112157b61080SScott Feldman 	if (err)
112257b61080SScott Feldman 		return err;
112357b61080SScott Feldman 
112457b61080SScott Feldman 	if (dev_num_vf(dev->dev.parent)) {
112557b61080SScott Feldman 		err = rtnl_vf_ports_fill(skb, dev);
112657b61080SScott Feldman 		if (err)
112757b61080SScott Feldman 			return err;
112857b61080SScott Feldman 	}
112957b61080SScott Feldman 
113057b61080SScott Feldman 	return 0;
113157b61080SScott Feldman }
113257b61080SScott Feldman 
113366cae9edSJiri Pirko static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
113466cae9edSJiri Pirko {
113566cae9edSJiri Pirko 	int err;
113602637fceSJiri Pirko 	struct netdev_phys_item_id ppid;
113766cae9edSJiri Pirko 
113866cae9edSJiri Pirko 	err = dev_get_phys_port_id(dev, &ppid);
113966cae9edSJiri Pirko 	if (err) {
114066cae9edSJiri Pirko 		if (err == -EOPNOTSUPP)
114166cae9edSJiri Pirko 			return 0;
114266cae9edSJiri Pirko 		return err;
114366cae9edSJiri Pirko 	}
114466cae9edSJiri Pirko 
114566cae9edSJiri Pirko 	if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
114666cae9edSJiri Pirko 		return -EMSGSIZE;
114766cae9edSJiri Pirko 
114866cae9edSJiri Pirko 	return 0;
114966cae9edSJiri Pirko }
115066cae9edSJiri Pirko 
1151db24a904SDavid Ahern static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1152db24a904SDavid Ahern {
1153db24a904SDavid Ahern 	char name[IFNAMSIZ];
1154db24a904SDavid Ahern 	int err;
1155db24a904SDavid Ahern 
1156db24a904SDavid Ahern 	err = dev_get_phys_port_name(dev, name, sizeof(name));
1157db24a904SDavid Ahern 	if (err) {
1158db24a904SDavid Ahern 		if (err == -EOPNOTSUPP)
1159db24a904SDavid Ahern 			return 0;
1160db24a904SDavid Ahern 		return err;
1161db24a904SDavid Ahern 	}
1162db24a904SDavid Ahern 
116377ef033bSMichal Schmidt 	if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
1164db24a904SDavid Ahern 		return -EMSGSIZE;
1165db24a904SDavid Ahern 
1166db24a904SDavid Ahern 	return 0;
1167db24a904SDavid Ahern }
1168db24a904SDavid Ahern 
116982f28412SJiri Pirko static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
117082f28412SJiri Pirko {
1171bccb3025SFlorian Fainelli 	struct netdev_phys_item_id ppid = { };
117282f28412SJiri Pirko 	int err;
117382f28412SJiri Pirko 
1174bccb3025SFlorian Fainelli 	err = dev_get_port_parent_id(dev, &ppid, false);
117582f28412SJiri Pirko 	if (err) {
117682f28412SJiri Pirko 		if (err == -EOPNOTSUPP)
117782f28412SJiri Pirko 			return 0;
117882f28412SJiri Pirko 		return err;
117982f28412SJiri Pirko 	}
118082f28412SJiri Pirko 
1181bccb3025SFlorian Fainelli 	if (nla_put(skb, IFLA_PHYS_SWITCH_ID, ppid.id_len, ppid.id))
118282f28412SJiri Pirko 		return -EMSGSIZE;
118382f28412SJiri Pirko 
118482f28412SJiri Pirko 	return 0;
118582f28412SJiri Pirko }
118682f28412SJiri Pirko 
1187b22b941bSHannes Frederic Sowa static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1188b22b941bSHannes Frederic Sowa 					      struct net_device *dev)
1189b22b941bSHannes Frederic Sowa {
1190550bce59SRoopa Prabhu 	struct rtnl_link_stats64 *sp;
1191b22b941bSHannes Frederic Sowa 	struct nlattr *attr;
1192b22b941bSHannes Frederic Sowa 
119358414d32SNicolas Dichtel 	attr = nla_reserve_64bit(skb, IFLA_STATS64,
119458414d32SNicolas Dichtel 				 sizeof(struct rtnl_link_stats64), IFLA_PAD);
1195b22b941bSHannes Frederic Sowa 	if (!attr)
1196b22b941bSHannes Frederic Sowa 		return -EMSGSIZE;
1197b22b941bSHannes Frederic Sowa 
1198550bce59SRoopa Prabhu 	sp = nla_data(attr);
1199550bce59SRoopa Prabhu 	dev_get_stats(dev, sp);
1200550bce59SRoopa Prabhu 
1201550bce59SRoopa Prabhu 	attr = nla_reserve(skb, IFLA_STATS,
1202550bce59SRoopa Prabhu 			   sizeof(struct rtnl_link_stats));
1203550bce59SRoopa Prabhu 	if (!attr)
1204550bce59SRoopa Prabhu 		return -EMSGSIZE;
1205550bce59SRoopa Prabhu 
1206550bce59SRoopa Prabhu 	copy_rtnl_link_stats(nla_data(attr), sp);
1207b22b941bSHannes Frederic Sowa 
1208b22b941bSHannes Frederic Sowa 	return 0;
1209b22b941bSHannes Frederic Sowa }
1210b22b941bSHannes Frederic Sowa 
1211b22b941bSHannes Frederic Sowa static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1212b22b941bSHannes Frederic Sowa 					       struct net_device *dev,
1213b22b941bSHannes Frederic Sowa 					       int vfs_num,
1214b22b941bSHannes Frederic Sowa 					       struct nlattr *vfinfo)
1215b22b941bSHannes Frederic Sowa {
1216b22b941bSHannes Frederic Sowa 	struct ifla_vf_rss_query_en vf_rss_query_en;
121779aab093SMoshe Shemesh 	struct nlattr *vf, *vfstats, *vfvlanlist;
1218b22b941bSHannes Frederic Sowa 	struct ifla_vf_link_state vf_linkstate;
121979aab093SMoshe Shemesh 	struct ifla_vf_vlan_info vf_vlan_info;
1220b22b941bSHannes Frederic Sowa 	struct ifla_vf_spoofchk vf_spoofchk;
1221b22b941bSHannes Frederic Sowa 	struct ifla_vf_tx_rate vf_tx_rate;
1222b22b941bSHannes Frederic Sowa 	struct ifla_vf_stats vf_stats;
1223b22b941bSHannes Frederic Sowa 	struct ifla_vf_trust vf_trust;
1224b22b941bSHannes Frederic Sowa 	struct ifla_vf_vlan vf_vlan;
1225b22b941bSHannes Frederic Sowa 	struct ifla_vf_rate vf_rate;
1226b22b941bSHannes Frederic Sowa 	struct ifla_vf_mac vf_mac;
122775345f88SDenis Kirjanov 	struct ifla_vf_broadcast vf_broadcast;
1228b22b941bSHannes Frederic Sowa 	struct ifla_vf_info ivi;
122930aad417SDanit Goldberg 	struct ifla_vf_guid node_guid;
123030aad417SDanit Goldberg 	struct ifla_vf_guid port_guid;
1231b22b941bSHannes Frederic Sowa 
12320eed9cf5SMintz, Yuval 	memset(&ivi, 0, sizeof(ivi));
12330eed9cf5SMintz, Yuval 
1234b22b941bSHannes Frederic Sowa 	/* Not all SR-IOV capable drivers support the
1235b22b941bSHannes Frederic Sowa 	 * spoofcheck and "RSS query enable" query.  Preset to
1236b22b941bSHannes Frederic Sowa 	 * -1 so the user space tool can detect that the driver
1237b22b941bSHannes Frederic Sowa 	 * didn't report anything.
1238b22b941bSHannes Frederic Sowa 	 */
1239b22b941bSHannes Frederic Sowa 	ivi.spoofchk = -1;
1240b22b941bSHannes Frederic Sowa 	ivi.rss_query_en = -1;
1241b22b941bSHannes Frederic Sowa 	ivi.trusted = -1;
1242b22b941bSHannes Frederic Sowa 	/* The default value for VF link state is "auto"
1243b22b941bSHannes Frederic Sowa 	 * IFLA_VF_LINK_STATE_AUTO which equals zero
1244b22b941bSHannes Frederic Sowa 	 */
1245b22b941bSHannes Frederic Sowa 	ivi.linkstate = 0;
124679aab093SMoshe Shemesh 	/* VLAN Protocol by default is 802.1Q */
124779aab093SMoshe Shemesh 	ivi.vlan_proto = htons(ETH_P_8021Q);
1248b22b941bSHannes Frederic Sowa 	if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1249b22b941bSHannes Frederic Sowa 		return 0;
1250b22b941bSHannes Frederic Sowa 
1251775f4f05SDan Carpenter 	memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
12529fbf082fSLeon Romanovsky 	memset(&node_guid, 0, sizeof(node_guid));
12539fbf082fSLeon Romanovsky 	memset(&port_guid, 0, sizeof(port_guid));
1254775f4f05SDan Carpenter 
1255b22b941bSHannes Frederic Sowa 	vf_mac.vf =
1256b22b941bSHannes Frederic Sowa 		vf_vlan.vf =
125779aab093SMoshe Shemesh 		vf_vlan_info.vf =
1258b22b941bSHannes Frederic Sowa 		vf_rate.vf =
1259b22b941bSHannes Frederic Sowa 		vf_tx_rate.vf =
1260b22b941bSHannes Frederic Sowa 		vf_spoofchk.vf =
1261b22b941bSHannes Frederic Sowa 		vf_linkstate.vf =
1262b22b941bSHannes Frederic Sowa 		vf_rss_query_en.vf =
12639aed6ae0SDanit Goldberg 		vf_trust.vf =
12649aed6ae0SDanit Goldberg 		node_guid.vf =
12659aed6ae0SDanit Goldberg 		port_guid.vf = ivi.vf;
1266b22b941bSHannes Frederic Sowa 
1267b22b941bSHannes Frederic Sowa 	memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
126875345f88SDenis Kirjanov 	memcpy(vf_broadcast.broadcast, dev->broadcast, dev->addr_len);
1269b22b941bSHannes Frederic Sowa 	vf_vlan.vlan = ivi.vlan;
1270b22b941bSHannes Frederic Sowa 	vf_vlan.qos = ivi.qos;
127179aab093SMoshe Shemesh 	vf_vlan_info.vlan = ivi.vlan;
127279aab093SMoshe Shemesh 	vf_vlan_info.qos = ivi.qos;
127379aab093SMoshe Shemesh 	vf_vlan_info.vlan_proto = ivi.vlan_proto;
1274b22b941bSHannes Frederic Sowa 	vf_tx_rate.rate = ivi.max_tx_rate;
1275b22b941bSHannes Frederic Sowa 	vf_rate.min_tx_rate = ivi.min_tx_rate;
1276b22b941bSHannes Frederic Sowa 	vf_rate.max_tx_rate = ivi.max_tx_rate;
1277b22b941bSHannes Frederic Sowa 	vf_spoofchk.setting = ivi.spoofchk;
1278b22b941bSHannes Frederic Sowa 	vf_linkstate.link_state = ivi.linkstate;
1279b22b941bSHannes Frederic Sowa 	vf_rss_query_en.setting = ivi.rss_query_en;
1280b22b941bSHannes Frederic Sowa 	vf_trust.setting = ivi.trusted;
1281ae0be8deSMichal Kubecek 	vf = nla_nest_start_noflag(skb, IFLA_VF_INFO);
128279aab093SMoshe Shemesh 	if (!vf)
128379aab093SMoshe Shemesh 		goto nla_put_vfinfo_failure;
1284b22b941bSHannes Frederic Sowa 	if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
128575345f88SDenis Kirjanov 	    nla_put(skb, IFLA_VF_BROADCAST, sizeof(vf_broadcast), &vf_broadcast) ||
1286b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1287b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1288b22b941bSHannes Frederic Sowa 		    &vf_rate) ||
1289b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1290b22b941bSHannes Frederic Sowa 		    &vf_tx_rate) ||
1291b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1292b22b941bSHannes Frederic Sowa 		    &vf_spoofchk) ||
1293b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1294b22b941bSHannes Frederic Sowa 		    &vf_linkstate) ||
1295b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1296b22b941bSHannes Frederic Sowa 		    sizeof(vf_rss_query_en),
1297b22b941bSHannes Frederic Sowa 		    &vf_rss_query_en) ||
1298b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_TRUST,
1299b22b941bSHannes Frederic Sowa 		    sizeof(vf_trust), &vf_trust))
130079aab093SMoshe Shemesh 		goto nla_put_vf_failure;
130130aad417SDanit Goldberg 
130230aad417SDanit Goldberg 	if (dev->netdev_ops->ndo_get_vf_guid &&
130330aad417SDanit Goldberg 	    !dev->netdev_ops->ndo_get_vf_guid(dev, vfs_num, &node_guid,
130430aad417SDanit Goldberg 					      &port_guid)) {
130530aad417SDanit Goldberg 		if (nla_put(skb, IFLA_VF_IB_NODE_GUID, sizeof(node_guid),
130630aad417SDanit Goldberg 			    &node_guid) ||
130730aad417SDanit Goldberg 		    nla_put(skb, IFLA_VF_IB_PORT_GUID, sizeof(port_guid),
130830aad417SDanit Goldberg 			    &port_guid))
130930aad417SDanit Goldberg 			goto nla_put_vf_failure;
131030aad417SDanit Goldberg 	}
1311ae0be8deSMichal Kubecek 	vfvlanlist = nla_nest_start_noflag(skb, IFLA_VF_VLAN_LIST);
131279aab093SMoshe Shemesh 	if (!vfvlanlist)
131379aab093SMoshe Shemesh 		goto nla_put_vf_failure;
131479aab093SMoshe Shemesh 	if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
131579aab093SMoshe Shemesh 		    &vf_vlan_info)) {
131679aab093SMoshe Shemesh 		nla_nest_cancel(skb, vfvlanlist);
131779aab093SMoshe Shemesh 		goto nla_put_vf_failure;
131879aab093SMoshe Shemesh 	}
131979aab093SMoshe Shemesh 	nla_nest_end(skb, vfvlanlist);
1320b22b941bSHannes Frederic Sowa 	memset(&vf_stats, 0, sizeof(vf_stats));
1321b22b941bSHannes Frederic Sowa 	if (dev->netdev_ops->ndo_get_vf_stats)
1322b22b941bSHannes Frederic Sowa 		dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1323b22b941bSHannes Frederic Sowa 						&vf_stats);
1324ae0be8deSMichal Kubecek 	vfstats = nla_nest_start_noflag(skb, IFLA_VF_STATS);
132579aab093SMoshe Shemesh 	if (!vfstats)
132679aab093SMoshe Shemesh 		goto nla_put_vf_failure;
1327343a6d8eSNicolas Dichtel 	if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1328343a6d8eSNicolas Dichtel 			      vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1329343a6d8eSNicolas Dichtel 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1330343a6d8eSNicolas Dichtel 			      vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1331343a6d8eSNicolas Dichtel 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1332343a6d8eSNicolas Dichtel 			      vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1333343a6d8eSNicolas Dichtel 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1334343a6d8eSNicolas Dichtel 			      vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1335343a6d8eSNicolas Dichtel 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1336343a6d8eSNicolas Dichtel 			      vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1337343a6d8eSNicolas Dichtel 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
1338c5a9f6f0SEugenia Emantayev 			      vf_stats.multicast, IFLA_VF_STATS_PAD) ||
1339c5a9f6f0SEugenia Emantayev 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED,
1340c5a9f6f0SEugenia Emantayev 			      vf_stats.rx_dropped, IFLA_VF_STATS_PAD) ||
1341c5a9f6f0SEugenia Emantayev 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED,
1342c5a9f6f0SEugenia Emantayev 			      vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) {
134379aab093SMoshe Shemesh 		nla_nest_cancel(skb, vfstats);
134479aab093SMoshe Shemesh 		goto nla_put_vf_failure;
134579aab093SMoshe Shemesh 	}
1346b22b941bSHannes Frederic Sowa 	nla_nest_end(skb, vfstats);
1347b22b941bSHannes Frederic Sowa 	nla_nest_end(skb, vf);
1348b22b941bSHannes Frederic Sowa 	return 0;
134979aab093SMoshe Shemesh 
135079aab093SMoshe Shemesh nla_put_vf_failure:
135179aab093SMoshe Shemesh 	nla_nest_cancel(skb, vf);
135279aab093SMoshe Shemesh nla_put_vfinfo_failure:
135379aab093SMoshe Shemesh 	nla_nest_cancel(skb, vfinfo);
135479aab093SMoshe Shemesh 	return -EMSGSIZE;
1355b22b941bSHannes Frederic Sowa }
1356b22b941bSHannes Frederic Sowa 
1357250fc3dfSFlorian Westphal static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb,
1358250fc3dfSFlorian Westphal 					   struct net_device *dev,
1359250fc3dfSFlorian Westphal 					   u32 ext_filter_mask)
1360250fc3dfSFlorian Westphal {
1361250fc3dfSFlorian Westphal 	struct nlattr *vfinfo;
1362250fc3dfSFlorian Westphal 	int i, num_vfs;
1363250fc3dfSFlorian Westphal 
1364250fc3dfSFlorian Westphal 	if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0))
1365250fc3dfSFlorian Westphal 		return 0;
1366250fc3dfSFlorian Westphal 
1367250fc3dfSFlorian Westphal 	num_vfs = dev_num_vf(dev->dev.parent);
1368250fc3dfSFlorian Westphal 	if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs))
1369250fc3dfSFlorian Westphal 		return -EMSGSIZE;
1370250fc3dfSFlorian Westphal 
1371250fc3dfSFlorian Westphal 	if (!dev->netdev_ops->ndo_get_vf_config)
1372250fc3dfSFlorian Westphal 		return 0;
1373250fc3dfSFlorian Westphal 
1374ae0be8deSMichal Kubecek 	vfinfo = nla_nest_start_noflag(skb, IFLA_VFINFO_LIST);
1375250fc3dfSFlorian Westphal 	if (!vfinfo)
1376250fc3dfSFlorian Westphal 		return -EMSGSIZE;
1377250fc3dfSFlorian Westphal 
1378250fc3dfSFlorian Westphal 	for (i = 0; i < num_vfs; i++) {
1379250fc3dfSFlorian Westphal 		if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
1380250fc3dfSFlorian Westphal 			return -EMSGSIZE;
1381250fc3dfSFlorian Westphal 	}
1382250fc3dfSFlorian Westphal 
1383250fc3dfSFlorian Westphal 	nla_nest_end(skb, vfinfo);
1384250fc3dfSFlorian Westphal 	return 0;
1385250fc3dfSFlorian Westphal }
1386250fc3dfSFlorian Westphal 
1387b22b941bSHannes Frederic Sowa static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1388b22b941bSHannes Frederic Sowa {
13895f8e4474SKangjie Lu 	struct rtnl_link_ifmap map;
13905f8e4474SKangjie Lu 
13915f8e4474SKangjie Lu 	memset(&map, 0, sizeof(map));
13925f8e4474SKangjie Lu 	map.mem_start   = dev->mem_start;
13935f8e4474SKangjie Lu 	map.mem_end     = dev->mem_end;
13945f8e4474SKangjie Lu 	map.base_addr   = dev->base_addr;
13955f8e4474SKangjie Lu 	map.irq         = dev->irq;
13965f8e4474SKangjie Lu 	map.dma         = dev->dma;
13975f8e4474SKangjie Lu 	map.port        = dev->if_port;
13985f8e4474SKangjie Lu 
1399270cb4d0SNicolas Dichtel 	if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
1400b22b941bSHannes Frederic Sowa 		return -EMSGSIZE;
1401b22b941bSHannes Frederic Sowa 
1402b22b941bSHannes Frederic Sowa 	return 0;
1403b22b941bSHannes Frederic Sowa }
1404b22b941bSHannes Frederic Sowa 
1405a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_skb(struct net_device *dev)
1406d67b9cd2SDaniel Borkmann {
140758038695SMartin KaFai Lau 	const struct bpf_prog *generic_xdp_prog;
1408d67b9cd2SDaniel Borkmann 
1409d67b9cd2SDaniel Borkmann 	ASSERT_RTNL();
1410d67b9cd2SDaniel Borkmann 
141158038695SMartin KaFai Lau 	generic_xdp_prog = rtnl_dereference(dev->xdp_prog);
1412a25717d2SJakub Kicinski 	if (!generic_xdp_prog)
1413a25717d2SJakub Kicinski 		return 0;
1414a25717d2SJakub Kicinski 	return generic_xdp_prog->aux->id;
141558038695SMartin KaFai Lau }
1416ce158e58SJakub Kicinski 
1417a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_drv(struct net_device *dev)
1418a25717d2SJakub Kicinski {
1419a25717d2SJakub Kicinski 	return __dev_xdp_query(dev, dev->netdev_ops->ndo_bpf, XDP_QUERY_PROG);
1420a25717d2SJakub Kicinski }
1421118b4aa2SJakub Kicinski 
1422a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_hw(struct net_device *dev)
1423a25717d2SJakub Kicinski {
1424a25717d2SJakub Kicinski 	return __dev_xdp_query(dev, dev->netdev_ops->ndo_bpf,
1425a25717d2SJakub Kicinski 			       XDP_QUERY_PROG_HW);
1426a25717d2SJakub Kicinski }
1427a25717d2SJakub Kicinski 
1428a25717d2SJakub Kicinski static int rtnl_xdp_report_one(struct sk_buff *skb, struct net_device *dev,
1429a25717d2SJakub Kicinski 			       u32 *prog_id, u8 *mode, u8 tgt_mode, u32 attr,
1430a25717d2SJakub Kicinski 			       u32 (*get_prog_id)(struct net_device *dev))
1431a25717d2SJakub Kicinski {
1432a25717d2SJakub Kicinski 	u32 curr_id;
1433a25717d2SJakub Kicinski 	int err;
1434a25717d2SJakub Kicinski 
1435a25717d2SJakub Kicinski 	curr_id = get_prog_id(dev);
1436a25717d2SJakub Kicinski 	if (!curr_id)
1437a25717d2SJakub Kicinski 		return 0;
1438a25717d2SJakub Kicinski 
1439a25717d2SJakub Kicinski 	*prog_id = curr_id;
1440a25717d2SJakub Kicinski 	err = nla_put_u32(skb, attr, curr_id);
1441a25717d2SJakub Kicinski 	if (err)
1442a25717d2SJakub Kicinski 		return err;
1443a25717d2SJakub Kicinski 
1444a25717d2SJakub Kicinski 	if (*mode != XDP_ATTACHED_NONE)
1445a25717d2SJakub Kicinski 		*mode = XDP_ATTACHED_MULTI;
1446a25717d2SJakub Kicinski 	else
1447a25717d2SJakub Kicinski 		*mode = tgt_mode;
1448a25717d2SJakub Kicinski 
1449a25717d2SJakub Kicinski 	return 0;
1450d67b9cd2SDaniel Borkmann }
1451d67b9cd2SDaniel Borkmann 
1452d1fdd913SBrenden Blanco static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1453d1fdd913SBrenden Blanco {
1454d1fdd913SBrenden Blanco 	struct nlattr *xdp;
145558038695SMartin KaFai Lau 	u32 prog_id;
1456d1fdd913SBrenden Blanco 	int err;
14574f91da26SJakub Kicinski 	u8 mode;
1458d1fdd913SBrenden Blanco 
1459ae0be8deSMichal Kubecek 	xdp = nla_nest_start_noflag(skb, IFLA_XDP);
1460d1fdd913SBrenden Blanco 	if (!xdp)
1461d1fdd913SBrenden Blanco 		return -EMSGSIZE;
1462b5cdae32SDavid S. Miller 
1463a25717d2SJakub Kicinski 	prog_id = 0;
1464a25717d2SJakub Kicinski 	mode = XDP_ATTACHED_NONE;
1465202aabe8SJakub Kicinski 	err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_SKB,
1466202aabe8SJakub Kicinski 				  IFLA_XDP_SKB_PROG_ID, rtnl_xdp_prog_skb);
1467202aabe8SJakub Kicinski 	if (err)
1468a25717d2SJakub Kicinski 		goto err_cancel;
1469202aabe8SJakub Kicinski 	err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_DRV,
1470202aabe8SJakub Kicinski 				  IFLA_XDP_DRV_PROG_ID, rtnl_xdp_prog_drv);
1471202aabe8SJakub Kicinski 	if (err)
1472a25717d2SJakub Kicinski 		goto err_cancel;
1473202aabe8SJakub Kicinski 	err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_HW,
1474202aabe8SJakub Kicinski 				  IFLA_XDP_HW_PROG_ID, rtnl_xdp_prog_hw);
1475202aabe8SJakub Kicinski 	if (err)
1476a25717d2SJakub Kicinski 		goto err_cancel;
1477a25717d2SJakub Kicinski 
14784f91da26SJakub Kicinski 	err = nla_put_u8(skb, IFLA_XDP_ATTACHED, mode);
1479d1fdd913SBrenden Blanco 	if (err)
1480d1fdd913SBrenden Blanco 		goto err_cancel;
1481d1fdd913SBrenden Blanco 
1482a25717d2SJakub Kicinski 	if (prog_id && mode != XDP_ATTACHED_MULTI) {
148358038695SMartin KaFai Lau 		err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id);
148458038695SMartin KaFai Lau 		if (err)
148558038695SMartin KaFai Lau 			goto err_cancel;
148658038695SMartin KaFai Lau 	}
148758038695SMartin KaFai Lau 
1488d1fdd913SBrenden Blanco 	nla_nest_end(skb, xdp);
1489d1fdd913SBrenden Blanco 	return 0;
1490d1fdd913SBrenden Blanco 
1491d1fdd913SBrenden Blanco err_cancel:
1492d1fdd913SBrenden Blanco 	nla_nest_cancel(skb, xdp);
1493d1fdd913SBrenden Blanco 	return err;
1494d1fdd913SBrenden Blanco }
1495d1fdd913SBrenden Blanco 
14963d3ea5afSVlad Yasevich static u32 rtnl_get_event(unsigned long event)
14973d3ea5afSVlad Yasevich {
14983d3ea5afSVlad Yasevich 	u32 rtnl_event_type = IFLA_EVENT_NONE;
14993d3ea5afSVlad Yasevich 
15003d3ea5afSVlad Yasevich 	switch (event) {
15013d3ea5afSVlad Yasevich 	case NETDEV_REBOOT:
15023d3ea5afSVlad Yasevich 		rtnl_event_type = IFLA_EVENT_REBOOT;
15033d3ea5afSVlad Yasevich 		break;
15043d3ea5afSVlad Yasevich 	case NETDEV_FEAT_CHANGE:
15053d3ea5afSVlad Yasevich 		rtnl_event_type = IFLA_EVENT_FEATURES;
15063d3ea5afSVlad Yasevich 		break;
15073d3ea5afSVlad Yasevich 	case NETDEV_BONDING_FAILOVER:
15083d3ea5afSVlad Yasevich 		rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER;
15093d3ea5afSVlad Yasevich 		break;
15103d3ea5afSVlad Yasevich 	case NETDEV_NOTIFY_PEERS:
15113d3ea5afSVlad Yasevich 		rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS;
15123d3ea5afSVlad Yasevich 		break;
15133d3ea5afSVlad Yasevich 	case NETDEV_RESEND_IGMP:
15143d3ea5afSVlad Yasevich 		rtnl_event_type = IFLA_EVENT_IGMP_RESEND;
15153d3ea5afSVlad Yasevich 		break;
15163d3ea5afSVlad Yasevich 	case NETDEV_CHANGEINFODATA:
15173d3ea5afSVlad Yasevich 		rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS;
15183d3ea5afSVlad Yasevich 		break;
15193d3ea5afSVlad Yasevich 	default:
15203d3ea5afSVlad Yasevich 		break;
15213d3ea5afSVlad Yasevich 	}
15223d3ea5afSVlad Yasevich 
15233d3ea5afSVlad Yasevich 	return rtnl_event_type;
15243d3ea5afSVlad Yasevich }
15253d3ea5afSVlad Yasevich 
152679110a04SFlorian Westphal static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev)
152779110a04SFlorian Westphal {
152879110a04SFlorian Westphal 	const struct net_device *upper_dev;
152979110a04SFlorian Westphal 	int ret = 0;
153079110a04SFlorian Westphal 
153179110a04SFlorian Westphal 	rcu_read_lock();
153279110a04SFlorian Westphal 
153379110a04SFlorian Westphal 	upper_dev = netdev_master_upper_dev_get_rcu(dev);
153479110a04SFlorian Westphal 	if (upper_dev)
153579110a04SFlorian Westphal 		ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex);
153679110a04SFlorian Westphal 
153779110a04SFlorian Westphal 	rcu_read_unlock();
153879110a04SFlorian Westphal 	return ret;
153979110a04SFlorian Westphal }
154079110a04SFlorian Westphal 
1541feadc4b6SSabrina Dubroca static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev,
1542feadc4b6SSabrina Dubroca 			  bool force)
154379110a04SFlorian Westphal {
154479110a04SFlorian Westphal 	int ifindex = dev_get_iflink(dev);
154579110a04SFlorian Westphal 
1546feadc4b6SSabrina Dubroca 	if (force || dev->ifindex != ifindex)
154779110a04SFlorian Westphal 		return nla_put_u32(skb, IFLA_LINK, ifindex);
1548feadc4b6SSabrina Dubroca 
1549feadc4b6SSabrina Dubroca 	return 0;
155079110a04SFlorian Westphal }
155179110a04SFlorian Westphal 
15526c557001SFlorian Westphal static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb,
15536c557001SFlorian Westphal 					      struct net_device *dev)
15546c557001SFlorian Westphal {
15556c557001SFlorian Westphal 	char buf[IFALIASZ];
15566c557001SFlorian Westphal 	int ret;
15576c557001SFlorian Westphal 
15586c557001SFlorian Westphal 	ret = dev_get_alias(dev, buf, sizeof(buf));
15596c557001SFlorian Westphal 	return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0;
15606c557001SFlorian Westphal }
15616c557001SFlorian Westphal 
1562b1e66b9aSFlorian Westphal static int rtnl_fill_link_netnsid(struct sk_buff *skb,
156379e1ad14SJiri Benc 				  const struct net_device *dev,
1564d4e4fdf9SGuillaume Nault 				  struct net *src_net, gfp_t gfp)
1565b1e66b9aSFlorian Westphal {
1566feadc4b6SSabrina Dubroca 	bool put_iflink = false;
1567feadc4b6SSabrina Dubroca 
1568b1e66b9aSFlorian Westphal 	if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) {
1569b1e66b9aSFlorian Westphal 		struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1570b1e66b9aSFlorian Westphal 
1571b1e66b9aSFlorian Westphal 		if (!net_eq(dev_net(dev), link_net)) {
1572d4e4fdf9SGuillaume Nault 			int id = peernet2id_alloc(src_net, link_net, gfp);
1573b1e66b9aSFlorian Westphal 
1574b1e66b9aSFlorian Westphal 			if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1575b1e66b9aSFlorian Westphal 				return -EMSGSIZE;
1576feadc4b6SSabrina Dubroca 
1577feadc4b6SSabrina Dubroca 			put_iflink = true;
1578b1e66b9aSFlorian Westphal 		}
1579b1e66b9aSFlorian Westphal 	}
1580b1e66b9aSFlorian Westphal 
1581feadc4b6SSabrina Dubroca 	return nla_put_iflink(skb, dev, put_iflink);
1582b1e66b9aSFlorian Westphal }
1583b1e66b9aSFlorian Westphal 
1584070cbf5bSFlorian Westphal static int rtnl_fill_link_af(struct sk_buff *skb,
1585070cbf5bSFlorian Westphal 			     const struct net_device *dev,
1586070cbf5bSFlorian Westphal 			     u32 ext_filter_mask)
1587070cbf5bSFlorian Westphal {
1588070cbf5bSFlorian Westphal 	const struct rtnl_af_ops *af_ops;
1589070cbf5bSFlorian Westphal 	struct nlattr *af_spec;
1590070cbf5bSFlorian Westphal 
1591ae0be8deSMichal Kubecek 	af_spec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
1592070cbf5bSFlorian Westphal 	if (!af_spec)
1593070cbf5bSFlorian Westphal 		return -EMSGSIZE;
1594070cbf5bSFlorian Westphal 
15955fa85a09SFlorian Westphal 	list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
1596070cbf5bSFlorian Westphal 		struct nlattr *af;
1597070cbf5bSFlorian Westphal 		int err;
1598070cbf5bSFlorian Westphal 
1599070cbf5bSFlorian Westphal 		if (!af_ops->fill_link_af)
1600070cbf5bSFlorian Westphal 			continue;
1601070cbf5bSFlorian Westphal 
1602ae0be8deSMichal Kubecek 		af = nla_nest_start_noflag(skb, af_ops->family);
1603070cbf5bSFlorian Westphal 		if (!af)
1604070cbf5bSFlorian Westphal 			return -EMSGSIZE;
1605070cbf5bSFlorian Westphal 
1606070cbf5bSFlorian Westphal 		err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
1607070cbf5bSFlorian Westphal 		/*
1608070cbf5bSFlorian Westphal 		 * Caller may return ENODATA to indicate that there
1609070cbf5bSFlorian Westphal 		 * was no data to be dumped. This is not an error, it
1610070cbf5bSFlorian Westphal 		 * means we should trim the attribute header and
1611070cbf5bSFlorian Westphal 		 * continue.
1612070cbf5bSFlorian Westphal 		 */
1613070cbf5bSFlorian Westphal 		if (err == -ENODATA)
1614070cbf5bSFlorian Westphal 			nla_nest_cancel(skb, af);
1615070cbf5bSFlorian Westphal 		else if (err < 0)
1616070cbf5bSFlorian Westphal 			return -EMSGSIZE;
1617070cbf5bSFlorian Westphal 
1618070cbf5bSFlorian Westphal 		nla_nest_end(skb, af);
1619070cbf5bSFlorian Westphal 	}
1620070cbf5bSFlorian Westphal 
1621070cbf5bSFlorian Westphal 	nla_nest_end(skb, af_spec);
1622070cbf5bSFlorian Westphal 	return 0;
1623070cbf5bSFlorian Westphal }
1624070cbf5bSFlorian Westphal 
162588f4fb0cSJiri Pirko static int rtnl_fill_alt_ifnames(struct sk_buff *skb,
162688f4fb0cSJiri Pirko 				 const struct net_device *dev)
162788f4fb0cSJiri Pirko {
162888f4fb0cSJiri Pirko 	struct netdev_name_node *name_node;
162988f4fb0cSJiri Pirko 	int count = 0;
163088f4fb0cSJiri Pirko 
163188f4fb0cSJiri Pirko 	list_for_each_entry(name_node, &dev->name_node->list, list) {
163288f4fb0cSJiri Pirko 		if (nla_put_string(skb, IFLA_ALT_IFNAME, name_node->name))
163388f4fb0cSJiri Pirko 			return -EMSGSIZE;
163488f4fb0cSJiri Pirko 		count++;
163588f4fb0cSJiri Pirko 	}
163688f4fb0cSJiri Pirko 	return count;
163788f4fb0cSJiri Pirko }
163888f4fb0cSJiri Pirko 
163988f4fb0cSJiri Pirko static int rtnl_fill_prop_list(struct sk_buff *skb,
164088f4fb0cSJiri Pirko 			       const struct net_device *dev)
164188f4fb0cSJiri Pirko {
164288f4fb0cSJiri Pirko 	struct nlattr *prop_list;
164388f4fb0cSJiri Pirko 	int ret;
164488f4fb0cSJiri Pirko 
164588f4fb0cSJiri Pirko 	prop_list = nla_nest_start(skb, IFLA_PROP_LIST);
164688f4fb0cSJiri Pirko 	if (!prop_list)
164788f4fb0cSJiri Pirko 		return -EMSGSIZE;
164888f4fb0cSJiri Pirko 
164988f4fb0cSJiri Pirko 	ret = rtnl_fill_alt_ifnames(skb, dev);
165088f4fb0cSJiri Pirko 	if (ret <= 0)
165188f4fb0cSJiri Pirko 		goto nest_cancel;
165288f4fb0cSJiri Pirko 
165388f4fb0cSJiri Pirko 	nla_nest_end(skb, prop_list);
165488f4fb0cSJiri Pirko 	return 0;
165588f4fb0cSJiri Pirko 
165688f4fb0cSJiri Pirko nest_cancel:
165788f4fb0cSJiri Pirko 	nla_nest_cancel(skb, prop_list);
165888f4fb0cSJiri Pirko 	return ret;
165988f4fb0cSJiri Pirko }
166088f4fb0cSJiri Pirko 
166179e1ad14SJiri Benc static int rtnl_fill_ifinfo(struct sk_buff *skb,
166279e1ad14SJiri Benc 			    struct net_device *dev, struct net *src_net,
1663575c3e2aSPatrick McHardy 			    int type, u32 pid, u32 seq, u32 change,
16643d3ea5afSVlad Yasevich 			    unsigned int flags, u32 ext_filter_mask,
166538e01b30SNicolas Dichtel 			    u32 event, int *new_nsid, int new_ifindex,
1666d4e4fdf9SGuillaume Nault 			    int tgt_netnsid, gfp_t gfp)
1667b60c5115SThomas Graf {
1668b60c5115SThomas Graf 	struct ifinfomsg *ifm;
16691da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
16701da177e4SLinus Torvalds 
16712907c35fSEric Dumazet 	ASSERT_RTNL();
1672b60c5115SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1673b60c5115SThomas Graf 	if (nlh == NULL)
167426932566SPatrick McHardy 		return -EMSGSIZE;
16751da177e4SLinus Torvalds 
1676b60c5115SThomas Graf 	ifm = nlmsg_data(nlh);
1677b60c5115SThomas Graf 	ifm->ifi_family = AF_UNSPEC;
1678b60c5115SThomas Graf 	ifm->__ifi_pad = 0;
1679b60c5115SThomas Graf 	ifm->ifi_type = dev->type;
1680b60c5115SThomas Graf 	ifm->ifi_index = dev->ifindex;
1681b60c5115SThomas Graf 	ifm->ifi_flags = dev_get_flags(dev);
1682b60c5115SThomas Graf 	ifm->ifi_change = change;
16831da177e4SLinus Torvalds 
16847e4a8d5aSChristian Brauner 	if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_TARGET_NETNSID, tgt_netnsid))
168579e1ad14SJiri Benc 		goto nla_put_failure;
168679e1ad14SJiri Benc 
1687a6574349SDavid S. Miller 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1688a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1689a6574349SDavid S. Miller 	    nla_put_u8(skb, IFLA_OPERSTATE,
1690a6574349SDavid S. Miller 		       netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1691a6574349SDavid S. Miller 	    nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1692a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
16933e7a50ceSStephen Hemminger 	    nla_put_u32(skb, IFLA_MIN_MTU, dev->min_mtu) ||
16943e7a50ceSStephen Hemminger 	    nla_put_u32(skb, IFLA_MAX_MTU, dev->max_mtu) ||
1695a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_GROUP, dev->group) ||
1696edbc0bb3SBen Greear 	    nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
169776ff5cc9SJiri Pirko 	    nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
1698c70ce028SEric Dumazet 	    nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1699c70ce028SEric Dumazet 	    nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
17001d69c2b3SMark A. Greer #ifdef CONFIG_RPS
170176ff5cc9SJiri Pirko 	    nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
17021d69c2b3SMark A. Greer #endif
170379110a04SFlorian Westphal 	    put_master_ifindex(skb, dev) ||
17049a57247fSJiri Pirko 	    nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
1705a6574349SDavid S. Miller 	    (dev->qdisc &&
1706a6574349SDavid S. Miller 	     nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
17076c557001SFlorian Westphal 	    nla_put_ifalias(skb, dev) ||
17082d3b479dSdavid decotigny 	    nla_put_u32(skb, IFLA_CARRIER_CHANGES,
1709b2d3bcfaSDavid Decotigny 			atomic_read(&dev->carrier_up_count) +
1710b2d3bcfaSDavid Decotigny 			atomic_read(&dev->carrier_down_count)) ||
1711b2d3bcfaSDavid Decotigny 	    nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down) ||
1712b2d3bcfaSDavid Decotigny 	    nla_put_u32(skb, IFLA_CARRIER_UP_COUNT,
1713b2d3bcfaSDavid Decotigny 			atomic_read(&dev->carrier_up_count)) ||
1714b2d3bcfaSDavid Decotigny 	    nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT,
1715b2d3bcfaSDavid Decotigny 			atomic_read(&dev->carrier_down_count)))
1716a6574349SDavid S. Miller 		goto nla_put_failure;
17170b815a1aSStephen Hemminger 
17183d3ea5afSVlad Yasevich 	if (event != IFLA_EVENT_NONE) {
17193d3ea5afSVlad Yasevich 		if (nla_put_u32(skb, IFLA_EVENT, event))
17203d3ea5afSVlad Yasevich 			goto nla_put_failure;
17213d3ea5afSVlad Yasevich 	}
17223d3ea5afSVlad Yasevich 
1723b22b941bSHannes Frederic Sowa 	if (rtnl_fill_link_ifmap(skb, dev))
1724a6574349SDavid S. Miller 		goto nla_put_failure;
17251da177e4SLinus Torvalds 
17261da177e4SLinus Torvalds 	if (dev->addr_len) {
1727a6574349SDavid S. Miller 		if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1728a6574349SDavid S. Miller 		    nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1729a6574349SDavid S. Miller 			goto nla_put_failure;
17301da177e4SLinus Torvalds 	}
17311da177e4SLinus Torvalds 
173266cae9edSJiri Pirko 	if (rtnl_phys_port_id_fill(skb, dev))
173366cae9edSJiri Pirko 		goto nla_put_failure;
173466cae9edSJiri Pirko 
1735db24a904SDavid Ahern 	if (rtnl_phys_port_name_fill(skb, dev))
1736db24a904SDavid Ahern 		goto nla_put_failure;
1737db24a904SDavid Ahern 
173882f28412SJiri Pirko 	if (rtnl_phys_switch_id_fill(skb, dev))
173982f28412SJiri Pirko 		goto nla_put_failure;
174082f28412SJiri Pirko 
1741b22b941bSHannes Frederic Sowa 	if (rtnl_fill_stats(skb, dev))
1742b60c5115SThomas Graf 		goto nla_put_failure;
1743b60c5115SThomas Graf 
1744250fc3dfSFlorian Westphal 	if (rtnl_fill_vf(skb, dev, ext_filter_mask))
1745a6574349SDavid S. Miller 		goto nla_put_failure;
174657b61080SScott Feldman 
1747c53864fdSDavid Gibson 	if (rtnl_port_fill(skb, dev, ext_filter_mask))
174857b61080SScott Feldman 		goto nla_put_failure;
174957b61080SScott Feldman 
1750d1fdd913SBrenden Blanco 	if (rtnl_xdp_fill(skb, dev))
1751d1fdd913SBrenden Blanco 		goto nla_put_failure;
1752d1fdd913SBrenden Blanco 
1753ba7d49b1SJiri Pirko 	if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
175438f7b870SPatrick McHardy 		if (rtnl_link_fill(skb, dev) < 0)
175538f7b870SPatrick McHardy 			goto nla_put_failure;
175638f7b870SPatrick McHardy 	}
175738f7b870SPatrick McHardy 
1758d4e4fdf9SGuillaume Nault 	if (rtnl_fill_link_netnsid(skb, dev, src_net, gfp))
1759d37512a2SNicolas Dichtel 		goto nla_put_failure;
1760d37512a2SNicolas Dichtel 
17616621dd29SNicolas Dichtel 	if (new_nsid &&
17626621dd29SNicolas Dichtel 	    nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0)
17636621dd29SNicolas Dichtel 		goto nla_put_failure;
176438e01b30SNicolas Dichtel 	if (new_ifindex &&
176538e01b30SNicolas Dichtel 	    nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0)
176638e01b30SNicolas Dichtel 		goto nla_put_failure;
176738e01b30SNicolas Dichtel 
1768f74877a5SMichal Kubecek 	if (memchr_inv(dev->perm_addr, '\0', dev->addr_len) &&
1769f74877a5SMichal Kubecek 	    nla_put(skb, IFLA_PERM_ADDRESS, dev->addr_len, dev->perm_addr))
1770f74877a5SMichal Kubecek 		goto nla_put_failure;
17716621dd29SNicolas Dichtel 
17725fa85a09SFlorian Westphal 	rcu_read_lock();
1773070cbf5bSFlorian Westphal 	if (rtnl_fill_link_af(skb, dev, ext_filter_mask))
17745fa85a09SFlorian Westphal 		goto nla_put_failure_rcu;
17755fa85a09SFlorian Westphal 	rcu_read_unlock();
1776f8ff182cSThomas Graf 
177788f4fb0cSJiri Pirko 	if (rtnl_fill_prop_list(skb, dev))
177888f4fb0cSJiri Pirko 		goto nla_put_failure;
177988f4fb0cSJiri Pirko 
1780053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
1781053c095aSJohannes Berg 	return 0;
1782b60c5115SThomas Graf 
17835fa85a09SFlorian Westphal nla_put_failure_rcu:
17845fa85a09SFlorian Westphal 	rcu_read_unlock();
1785b60c5115SThomas Graf nla_put_failure:
178626932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
178726932566SPatrick McHardy 	return -EMSGSIZE;
17881da177e4SLinus Torvalds }
17891da177e4SLinus Torvalds 
1790f7b12606SJiri Pirko static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1791f7b12606SJiri Pirko 	[IFLA_IFNAME]		= { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1792f7b12606SJiri Pirko 	[IFLA_ADDRESS]		= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1793f7b12606SJiri Pirko 	[IFLA_BROADCAST]	= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1794f7b12606SJiri Pirko 	[IFLA_MAP]		= { .len = sizeof(struct rtnl_link_ifmap) },
1795f7b12606SJiri Pirko 	[IFLA_MTU]		= { .type = NLA_U32 },
1796f7b12606SJiri Pirko 	[IFLA_LINK]		= { .type = NLA_U32 },
1797f7b12606SJiri Pirko 	[IFLA_MASTER]		= { .type = NLA_U32 },
1798f7b12606SJiri Pirko 	[IFLA_CARRIER]		= { .type = NLA_U8 },
1799f7b12606SJiri Pirko 	[IFLA_TXQLEN]		= { .type = NLA_U32 },
1800f7b12606SJiri Pirko 	[IFLA_WEIGHT]		= { .type = NLA_U32 },
1801f7b12606SJiri Pirko 	[IFLA_OPERSTATE]	= { .type = NLA_U8 },
1802f7b12606SJiri Pirko 	[IFLA_LINKMODE]		= { .type = NLA_U8 },
1803f7b12606SJiri Pirko 	[IFLA_LINKINFO]		= { .type = NLA_NESTED },
1804f7b12606SJiri Pirko 	[IFLA_NET_NS_PID]	= { .type = NLA_U32 },
1805f7b12606SJiri Pirko 	[IFLA_NET_NS_FD]	= { .type = NLA_U32 },
18062459b4c6SNicolas Dichtel 	/* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
18072459b4c6SNicolas Dichtel 	 * allow 0-length string (needed to remove an alias).
18082459b4c6SNicolas Dichtel 	 */
18092459b4c6SNicolas Dichtel 	[IFLA_IFALIAS]	        = { .type = NLA_BINARY, .len = IFALIASZ - 1 },
1810f7b12606SJiri Pirko 	[IFLA_VFINFO_LIST]	= {. type = NLA_NESTED },
1811f7b12606SJiri Pirko 	[IFLA_VF_PORTS]		= { .type = NLA_NESTED },
1812f7b12606SJiri Pirko 	[IFLA_PORT_SELF]	= { .type = NLA_NESTED },
1813f7b12606SJiri Pirko 	[IFLA_AF_SPEC]		= { .type = NLA_NESTED },
1814f7b12606SJiri Pirko 	[IFLA_EXT_MASK]		= { .type = NLA_U32 },
1815f7b12606SJiri Pirko 	[IFLA_PROMISCUITY]	= { .type = NLA_U32 },
1816f7b12606SJiri Pirko 	[IFLA_NUM_TX_QUEUES]	= { .type = NLA_U32 },
1817f7b12606SJiri Pirko 	[IFLA_NUM_RX_QUEUES]	= { .type = NLA_U32 },
181846e6b992SStephen Hemminger 	[IFLA_GSO_MAX_SEGS]	= { .type = NLA_U32 },
181946e6b992SStephen Hemminger 	[IFLA_GSO_MAX_SIZE]	= { .type = NLA_U32 },
182002637fceSJiri Pirko 	[IFLA_PHYS_PORT_ID]	= { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
18212d3b479dSdavid decotigny 	[IFLA_CARRIER_CHANGES]	= { .type = NLA_U32 },  /* ignored */
182282f28412SJiri Pirko 	[IFLA_PHYS_SWITCH_ID]	= { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1823317f4810SNicolas Dichtel 	[IFLA_LINK_NETNSID]	= { .type = NLA_S32 },
182488d6378bSAnuradha Karuppiah 	[IFLA_PROTO_DOWN]	= { .type = NLA_U8 },
1825d1fdd913SBrenden Blanco 	[IFLA_XDP]		= { .type = NLA_NESTED },
18263d3ea5afSVlad Yasevich 	[IFLA_EVENT]		= { .type = NLA_U32 },
1827db833d40SSerhey Popovych 	[IFLA_GROUP]		= { .type = NLA_U32 },
18287e4a8d5aSChristian Brauner 	[IFLA_TARGET_NETNSID]	= { .type = NLA_S32 },
1829b2d3bcfaSDavid Decotigny 	[IFLA_CARRIER_UP_COUNT]	= { .type = NLA_U32 },
1830b2d3bcfaSDavid Decotigny 	[IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 },
18313e7a50ceSStephen Hemminger 	[IFLA_MIN_MTU]		= { .type = NLA_U32 },
18323e7a50ceSStephen Hemminger 	[IFLA_MAX_MTU]		= { .type = NLA_U32 },
183336fbf1e5SJiri Pirko 	[IFLA_PROP_LIST]	= { .type = NLA_NESTED },
183436fbf1e5SJiri Pirko 	[IFLA_ALT_IFNAME]	= { .type = NLA_STRING,
183536fbf1e5SJiri Pirko 				    .len = ALTIFNAMSIZ - 1 },
1836f74877a5SMichal Kubecek 	[IFLA_PERM_ADDRESS]	= { .type = NLA_REJECT },
1837f7b12606SJiri Pirko };
1838f7b12606SJiri Pirko 
1839f7b12606SJiri Pirko static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1840f7b12606SJiri Pirko 	[IFLA_INFO_KIND]	= { .type = NLA_STRING },
1841f7b12606SJiri Pirko 	[IFLA_INFO_DATA]	= { .type = NLA_NESTED },
1842f7b12606SJiri Pirko 	[IFLA_INFO_SLAVE_KIND]	= { .type = NLA_STRING },
1843f7b12606SJiri Pirko 	[IFLA_INFO_SLAVE_DATA]	= { .type = NLA_NESTED },
1844f7b12606SJiri Pirko };
1845f7b12606SJiri Pirko 
1846f7b12606SJiri Pirko static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1847364d5716SDaniel Borkmann 	[IFLA_VF_MAC]		= { .len = sizeof(struct ifla_vf_mac) },
184875345f88SDenis Kirjanov 	[IFLA_VF_BROADCAST]	= { .type = NLA_REJECT },
1849364d5716SDaniel Borkmann 	[IFLA_VF_VLAN]		= { .len = sizeof(struct ifla_vf_vlan) },
185079aab093SMoshe Shemesh 	[IFLA_VF_VLAN_LIST]     = { .type = NLA_NESTED },
1851364d5716SDaniel Borkmann 	[IFLA_VF_TX_RATE]	= { .len = sizeof(struct ifla_vf_tx_rate) },
1852364d5716SDaniel Borkmann 	[IFLA_VF_SPOOFCHK]	= { .len = sizeof(struct ifla_vf_spoofchk) },
1853364d5716SDaniel Borkmann 	[IFLA_VF_RATE]		= { .len = sizeof(struct ifla_vf_rate) },
1854364d5716SDaniel Borkmann 	[IFLA_VF_LINK_STATE]	= { .len = sizeof(struct ifla_vf_link_state) },
185501a3d796SVlad Zolotarov 	[IFLA_VF_RSS_QUERY_EN]	= { .len = sizeof(struct ifla_vf_rss_query_en) },
18563b766cd8SEran Ben Elisha 	[IFLA_VF_STATS]		= { .type = NLA_NESTED },
1857dd461d6aSHiroshi Shimamoto 	[IFLA_VF_TRUST]		= { .len = sizeof(struct ifla_vf_trust) },
1858cc8e27ccSEli Cohen 	[IFLA_VF_IB_NODE_GUID]	= { .len = sizeof(struct ifla_vf_guid) },
1859cc8e27ccSEli Cohen 	[IFLA_VF_IB_PORT_GUID]	= { .len = sizeof(struct ifla_vf_guid) },
18603b766cd8SEran Ben Elisha };
18613b766cd8SEran Ben Elisha 
1862f7b12606SJiri Pirko static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1863f7b12606SJiri Pirko 	[IFLA_PORT_VF]		= { .type = NLA_U32 },
1864f7b12606SJiri Pirko 	[IFLA_PORT_PROFILE]	= { .type = NLA_STRING,
1865f7b12606SJiri Pirko 				    .len = PORT_PROFILE_MAX },
1866f7b12606SJiri Pirko 	[IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1867f7b12606SJiri Pirko 				      .len = PORT_UUID_MAX },
1868f7b12606SJiri Pirko 	[IFLA_PORT_HOST_UUID]	= { .type = NLA_STRING,
1869f7b12606SJiri Pirko 				    .len = PORT_UUID_MAX },
1870f7b12606SJiri Pirko 	[IFLA_PORT_REQUEST]	= { .type = NLA_U8, },
1871f7b12606SJiri Pirko 	[IFLA_PORT_RESPONSE]	= { .type = NLA_U16, },
1872025331dfSDaniel Borkmann 
1873025331dfSDaniel Borkmann 	/* Unused, but we need to keep it here since user space could
1874025331dfSDaniel Borkmann 	 * fill it. It's also broken with regard to NLA_BINARY use in
1875025331dfSDaniel Borkmann 	 * combination with structs.
1876025331dfSDaniel Borkmann 	 */
1877025331dfSDaniel Borkmann 	[IFLA_PORT_VSI_TYPE]	= { .type = NLA_BINARY,
1878025331dfSDaniel Borkmann 				    .len = sizeof(struct ifla_port_vsi) },
1879f7b12606SJiri Pirko };
1880f7b12606SJiri Pirko 
1881d1fdd913SBrenden Blanco static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
188292234c8fSToke Høiland-Jørgensen 	[IFLA_XDP_UNSPEC]	= { .strict_start_type = IFLA_XDP_EXPECTED_FD },
1883d1fdd913SBrenden Blanco 	[IFLA_XDP_FD]		= { .type = NLA_S32 },
188492234c8fSToke Høiland-Jørgensen 	[IFLA_XDP_EXPECTED_FD]	= { .type = NLA_S32 },
1885d1fdd913SBrenden Blanco 	[IFLA_XDP_ATTACHED]	= { .type = NLA_U8 },
188685de8576SDaniel Borkmann 	[IFLA_XDP_FLAGS]	= { .type = NLA_U32 },
188758038695SMartin KaFai Lau 	[IFLA_XDP_PROG_ID]	= { .type = NLA_U32 },
1888d1fdd913SBrenden Blanco };
1889d1fdd913SBrenden Blanco 
1890dc599f76SDavid Ahern static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1891dc599f76SDavid Ahern {
1892dc599f76SDavid Ahern 	const struct rtnl_link_ops *ops = NULL;
1893dc599f76SDavid Ahern 	struct nlattr *linfo[IFLA_INFO_MAX + 1];
1894dc599f76SDavid Ahern 
18958cb08174SJohannes Berg 	if (nla_parse_nested_deprecated(linfo, IFLA_INFO_MAX, nla, ifla_info_policy, NULL) < 0)
1896dc599f76SDavid Ahern 		return NULL;
1897dc599f76SDavid Ahern 
1898dc599f76SDavid Ahern 	if (linfo[IFLA_INFO_KIND]) {
1899dc599f76SDavid Ahern 		char kind[MODULE_NAME_LEN];
1900dc599f76SDavid Ahern 
1901dc599f76SDavid Ahern 		nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1902dc599f76SDavid Ahern 		ops = rtnl_link_ops_get(kind);
1903dc599f76SDavid Ahern 	}
1904dc599f76SDavid Ahern 
1905dc599f76SDavid Ahern 	return ops;
1906dc599f76SDavid Ahern }
1907dc599f76SDavid Ahern 
1908dc599f76SDavid Ahern static bool link_master_filtered(struct net_device *dev, int master_idx)
1909dc599f76SDavid Ahern {
1910dc599f76SDavid Ahern 	struct net_device *master;
1911dc599f76SDavid Ahern 
1912dc599f76SDavid Ahern 	if (!master_idx)
1913dc599f76SDavid Ahern 		return false;
1914dc599f76SDavid Ahern 
1915dc599f76SDavid Ahern 	master = netdev_master_upper_dev_get(dev);
1916dc599f76SDavid Ahern 	if (!master || master->ifindex != master_idx)
1917dc599f76SDavid Ahern 		return true;
1918dc599f76SDavid Ahern 
1919dc599f76SDavid Ahern 	return false;
1920dc599f76SDavid Ahern }
1921dc599f76SDavid Ahern 
1922dc599f76SDavid Ahern static bool link_kind_filtered(const struct net_device *dev,
1923dc599f76SDavid Ahern 			       const struct rtnl_link_ops *kind_ops)
1924dc599f76SDavid Ahern {
1925dc599f76SDavid Ahern 	if (kind_ops && dev->rtnl_link_ops != kind_ops)
1926dc599f76SDavid Ahern 		return true;
1927dc599f76SDavid Ahern 
1928dc599f76SDavid Ahern 	return false;
1929dc599f76SDavid Ahern }
1930dc599f76SDavid Ahern 
1931dc599f76SDavid Ahern static bool link_dump_filtered(struct net_device *dev,
1932dc599f76SDavid Ahern 			       int master_idx,
1933dc599f76SDavid Ahern 			       const struct rtnl_link_ops *kind_ops)
1934dc599f76SDavid Ahern {
1935dc599f76SDavid Ahern 	if (link_master_filtered(dev, master_idx) ||
1936dc599f76SDavid Ahern 	    link_kind_filtered(dev, kind_ops))
1937dc599f76SDavid Ahern 		return true;
1938dc599f76SDavid Ahern 
1939dc599f76SDavid Ahern 	return false;
1940dc599f76SDavid Ahern }
1941dc599f76SDavid Ahern 
1942c383edc4SChristian Brauner /**
1943c383edc4SChristian Brauner  * rtnl_get_net_ns_capable - Get netns if sufficiently privileged.
1944c383edc4SChristian Brauner  * @sk: netlink socket
1945c383edc4SChristian Brauner  * @netnsid: network namespace identifier
1946c383edc4SChristian Brauner  *
1947c383edc4SChristian Brauner  * Returns the network namespace identified by netnsid on success or an error
1948c383edc4SChristian Brauner  * pointer on failure.
1949c383edc4SChristian Brauner  */
1950c383edc4SChristian Brauner struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid)
195179e1ad14SJiri Benc {
195279e1ad14SJiri Benc 	struct net *net;
195379e1ad14SJiri Benc 
1954f428fe4aSAndrei Vagin 	net = get_net_ns_by_id(sock_net(sk), netnsid);
195579e1ad14SJiri Benc 	if (!net)
195679e1ad14SJiri Benc 		return ERR_PTR(-EINVAL);
195779e1ad14SJiri Benc 
195879e1ad14SJiri Benc 	/* For now, the caller is required to have CAP_NET_ADMIN in
195979e1ad14SJiri Benc 	 * the user namespace owning the target net ns.
196079e1ad14SJiri Benc 	 */
1961f428fe4aSAndrei Vagin 	if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) {
196279e1ad14SJiri Benc 		put_net(net);
196379e1ad14SJiri Benc 		return ERR_PTR(-EACCES);
196479e1ad14SJiri Benc 	}
196579e1ad14SJiri Benc 	return net;
196679e1ad14SJiri Benc }
1967c383edc4SChristian Brauner EXPORT_SYMBOL_GPL(rtnl_get_net_ns_capable);
196879e1ad14SJiri Benc 
1969905cf0abSDavid Ahern static int rtnl_valid_dump_ifinfo_req(const struct nlmsghdr *nlh,
1970905cf0abSDavid Ahern 				      bool strict_check, struct nlattr **tb,
1971905cf0abSDavid Ahern 				      struct netlink_ext_ack *extack)
1972905cf0abSDavid Ahern {
1973905cf0abSDavid Ahern 	int hdrlen;
1974905cf0abSDavid Ahern 
1975905cf0abSDavid Ahern 	if (strict_check) {
1976905cf0abSDavid Ahern 		struct ifinfomsg *ifm;
1977905cf0abSDavid Ahern 
1978905cf0abSDavid Ahern 		if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
1979905cf0abSDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid header for link dump");
1980905cf0abSDavid Ahern 			return -EINVAL;
1981905cf0abSDavid Ahern 		}
1982905cf0abSDavid Ahern 
1983905cf0abSDavid Ahern 		ifm = nlmsg_data(nlh);
1984905cf0abSDavid Ahern 		if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
1985905cf0abSDavid Ahern 		    ifm->ifi_change) {
1986905cf0abSDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid values in header for link dump request");
1987905cf0abSDavid Ahern 			return -EINVAL;
1988905cf0abSDavid Ahern 		}
1989905cf0abSDavid Ahern 		if (ifm->ifi_index) {
1990905cf0abSDavid Ahern 			NL_SET_ERR_MSG(extack, "Filter by device index not supported for link dumps");
1991905cf0abSDavid Ahern 			return -EINVAL;
1992905cf0abSDavid Ahern 		}
1993905cf0abSDavid Ahern 
19948cb08174SJohannes Berg 		return nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb,
19958cb08174SJohannes Berg 						     IFLA_MAX, ifla_policy,
19968cb08174SJohannes Berg 						     extack);
1997905cf0abSDavid Ahern 	}
1998905cf0abSDavid Ahern 
1999905cf0abSDavid Ahern 	/* A hack to preserve kernel<->userspace interface.
2000905cf0abSDavid Ahern 	 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
2001905cf0abSDavid Ahern 	 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
2002905cf0abSDavid Ahern 	 * what iproute2 < v3.9.0 used.
2003905cf0abSDavid Ahern 	 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
2004905cf0abSDavid Ahern 	 * attribute, its netlink message is shorter than struct ifinfomsg.
2005905cf0abSDavid Ahern 	 */
2006905cf0abSDavid Ahern 	hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2007905cf0abSDavid Ahern 		 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2008905cf0abSDavid Ahern 
20098cb08174SJohannes Berg 	return nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy,
20108cb08174SJohannes Berg 				      extack);
2011905cf0abSDavid Ahern }
2012905cf0abSDavid Ahern 
2013b60c5115SThomas Graf static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
20141da177e4SLinus Torvalds {
2015905cf0abSDavid Ahern 	struct netlink_ext_ack *extack = cb->extack;
2016905cf0abSDavid Ahern 	const struct nlmsghdr *nlh = cb->nlh;
20173b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
201879e1ad14SJiri Benc 	struct net *tgt_net = net;
20197c28bd0bSEric Dumazet 	int h, s_h;
20207c28bd0bSEric Dumazet 	int idx = 0, s_idx;
20211da177e4SLinus Torvalds 	struct net_device *dev;
20227c28bd0bSEric Dumazet 	struct hlist_head *head;
2023115c9b81SGreg Rose 	struct nlattr *tb[IFLA_MAX+1];
2024115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
2025dc599f76SDavid Ahern 	const struct rtnl_link_ops *kind_ops = NULL;
2026dc599f76SDavid Ahern 	unsigned int flags = NLM_F_MULTI;
2027dc599f76SDavid Ahern 	int master_idx = 0;
202879e1ad14SJiri Benc 	int netnsid = -1;
2029905cf0abSDavid Ahern 	int err, i;
20301da177e4SLinus Torvalds 
20317c28bd0bSEric Dumazet 	s_h = cb->args[0];
20327c28bd0bSEric Dumazet 	s_idx = cb->args[1];
20337c28bd0bSEric Dumazet 
2034905cf0abSDavid Ahern 	err = rtnl_valid_dump_ifinfo_req(nlh, cb->strict_check, tb, extack);
2035905cf0abSDavid Ahern 	if (err < 0) {
2036905cf0abSDavid Ahern 		if (cb->strict_check)
2037905cf0abSDavid Ahern 			return err;
2038e5eca6d4SMichal Schmidt 
2039905cf0abSDavid Ahern 		goto walk_entries;
204079e1ad14SJiri Benc 	}
204179e1ad14SJiri Benc 
2042905cf0abSDavid Ahern 	for (i = 0; i <= IFLA_MAX; ++i) {
2043905cf0abSDavid Ahern 		if (!tb[i])
2044905cf0abSDavid Ahern 			continue;
2045dc599f76SDavid Ahern 
2046905cf0abSDavid Ahern 		/* new attributes should only be added with strict checking */
2047905cf0abSDavid Ahern 		switch (i) {
2048905cf0abSDavid Ahern 		case IFLA_TARGET_NETNSID:
2049905cf0abSDavid Ahern 			netnsid = nla_get_s32(tb[i]);
2050905cf0abSDavid Ahern 			tgt_net = rtnl_get_net_ns_capable(skb->sk, netnsid);
2051905cf0abSDavid Ahern 			if (IS_ERR(tgt_net)) {
2052905cf0abSDavid Ahern 				NL_SET_ERR_MSG(extack, "Invalid target network namespace id");
2053905cf0abSDavid Ahern 				return PTR_ERR(tgt_net);
2054905cf0abSDavid Ahern 			}
2055905cf0abSDavid Ahern 			break;
2056905cf0abSDavid Ahern 		case IFLA_EXT_MASK:
2057905cf0abSDavid Ahern 			ext_filter_mask = nla_get_u32(tb[i]);
2058905cf0abSDavid Ahern 			break;
2059905cf0abSDavid Ahern 		case IFLA_MASTER:
2060905cf0abSDavid Ahern 			master_idx = nla_get_u32(tb[i]);
2061905cf0abSDavid Ahern 			break;
2062905cf0abSDavid Ahern 		case IFLA_LINKINFO:
2063905cf0abSDavid Ahern 			kind_ops = linkinfo_to_kind_ops(tb[i]);
2064905cf0abSDavid Ahern 			break;
2065905cf0abSDavid Ahern 		default:
2066905cf0abSDavid Ahern 			if (cb->strict_check) {
2067905cf0abSDavid Ahern 				NL_SET_ERR_MSG(extack, "Unsupported attribute in link dump request");
2068905cf0abSDavid Ahern 				return -EINVAL;
2069905cf0abSDavid Ahern 			}
2070905cf0abSDavid Ahern 		}
2071905cf0abSDavid Ahern 	}
2072dc599f76SDavid Ahern 
2073dc599f76SDavid Ahern 	if (master_idx || kind_ops)
2074dc599f76SDavid Ahern 		flags |= NLM_F_DUMP_FILTERED;
2075115c9b81SGreg Rose 
2076905cf0abSDavid Ahern walk_entries:
20777c28bd0bSEric Dumazet 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
20787562f876SPavel Emelianov 		idx = 0;
207979e1ad14SJiri Benc 		head = &tgt_net->dev_index_head[h];
2080cac5e65eSEric Dumazet 		hlist_for_each_entry(dev, head, index_hlist) {
2081dc599f76SDavid Ahern 			if (link_dump_filtered(dev, master_idx, kind_ops))
20823f0ae05dSZhang Shengju 				goto cont;
20831da177e4SLinus Torvalds 			if (idx < s_idx)
20847562f876SPavel Emelianov 				goto cont;
208579e1ad14SJiri Benc 			err = rtnl_fill_ifinfo(skb, dev, net,
208679e1ad14SJiri Benc 					       RTM_NEWLINK,
208715e47304SEric W. Biederman 					       NETLINK_CB(cb->skb).portid,
2088905cf0abSDavid Ahern 					       nlh->nlmsg_seq, 0, flags,
208938e01b30SNicolas Dichtel 					       ext_filter_mask, 0, NULL, 0,
2090d4e4fdf9SGuillaume Nault 					       netnsid, GFP_KERNEL);
2091973462bbSDavid Gibson 
2092f6c5775fSDavid Ahern 			if (err < 0) {
2093f6c5775fSDavid Ahern 				if (likely(skb->len))
20947c28bd0bSEric Dumazet 					goto out;
20954e985adaSThomas Graf 
2096f6c5775fSDavid Ahern 				goto out_err;
2097f6c5775fSDavid Ahern 			}
20987562f876SPavel Emelianov cont:
20997562f876SPavel Emelianov 			idx++;
21001da177e4SLinus Torvalds 		}
21017c28bd0bSEric Dumazet 	}
21027c28bd0bSEric Dumazet out:
2103f6c5775fSDavid Ahern 	err = skb->len;
2104f6c5775fSDavid Ahern out_err:
21057c28bd0bSEric Dumazet 	cb->args[1] = idx;
21067c28bd0bSEric Dumazet 	cb->args[0] = h;
2107d0225784SJakub Sitnicki 	cb->seq = net->dev_base_seq;
2108d0225784SJakub Sitnicki 	nl_dump_check_consistent(cb, nlmsg_hdr(skb));
210979e1ad14SJiri Benc 	if (netnsid >= 0)
211079e1ad14SJiri Benc 		put_net(tgt_net);
21111da177e4SLinus Torvalds 
2112f6c5775fSDavid Ahern 	return err;
21131da177e4SLinus Torvalds }
21141da177e4SLinus Torvalds 
2115fceb6435SJohannes Berg int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len,
2116fceb6435SJohannes Berg 			struct netlink_ext_ack *exterr)
2117f7b12606SJiri Pirko {
21188cb08174SJohannes Berg 	return nla_parse_deprecated(tb, IFLA_MAX, head, len, ifla_policy,
21198cb08174SJohannes Berg 				    exterr);
2120f7b12606SJiri Pirko }
2121f7b12606SJiri Pirko EXPORT_SYMBOL(rtnl_nla_parse_ifla);
212257b61080SScott Feldman 
212381adee47SEric W. Biederman struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
212481adee47SEric W. Biederman {
212581adee47SEric W. Biederman 	struct net *net;
212681adee47SEric W. Biederman 	/* Examine the link attributes and figure out which
212781adee47SEric W. Biederman 	 * network namespace we are talking about.
212881adee47SEric W. Biederman 	 */
212981adee47SEric W. Biederman 	if (tb[IFLA_NET_NS_PID])
213081adee47SEric W. Biederman 		net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
2131f0630529SEric W. Biederman 	else if (tb[IFLA_NET_NS_FD])
2132f0630529SEric W. Biederman 		net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
213381adee47SEric W. Biederman 	else
213481adee47SEric W. Biederman 		net = get_net(src_net);
213581adee47SEric W. Biederman 	return net;
213681adee47SEric W. Biederman }
213781adee47SEric W. Biederman EXPORT_SYMBOL(rtnl_link_get_net);
213881adee47SEric W. Biederman 
21397c4f63baSChristian Brauner /* Figure out which network namespace we are talking about by
21407c4f63baSChristian Brauner  * examining the link attributes in the following order:
21417c4f63baSChristian Brauner  *
21427c4f63baSChristian Brauner  * 1. IFLA_NET_NS_PID
21437c4f63baSChristian Brauner  * 2. IFLA_NET_NS_FD
21447e4a8d5aSChristian Brauner  * 3. IFLA_TARGET_NETNSID
21457c4f63baSChristian Brauner  */
21467c4f63baSChristian Brauner static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net,
21477c4f63baSChristian Brauner 					       struct nlattr *tb[])
21487c4f63baSChristian Brauner {
21497c4f63baSChristian Brauner 	struct net *net;
21507c4f63baSChristian Brauner 
21517c4f63baSChristian Brauner 	if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])
21527c4f63baSChristian Brauner 		return rtnl_link_get_net(src_net, tb);
21537c4f63baSChristian Brauner 
21547e4a8d5aSChristian Brauner 	if (!tb[IFLA_TARGET_NETNSID])
21557c4f63baSChristian Brauner 		return get_net(src_net);
21567c4f63baSChristian Brauner 
21577e4a8d5aSChristian Brauner 	net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_TARGET_NETNSID]));
21587c4f63baSChristian Brauner 	if (!net)
21597c4f63baSChristian Brauner 		return ERR_PTR(-EINVAL);
21607c4f63baSChristian Brauner 
21617c4f63baSChristian Brauner 	return net;
21627c4f63baSChristian Brauner }
21637c4f63baSChristian Brauner 
21647c4f63baSChristian Brauner static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb,
21657c4f63baSChristian Brauner 					     struct net *src_net,
21667c4f63baSChristian Brauner 					     struct nlattr *tb[], int cap)
21677c4f63baSChristian Brauner {
21687c4f63baSChristian Brauner 	struct net *net;
21697c4f63baSChristian Brauner 
21707c4f63baSChristian Brauner 	net = rtnl_link_get_net_by_nlattr(src_net, tb);
21717c4f63baSChristian Brauner 	if (IS_ERR(net))
21727c4f63baSChristian Brauner 		return net;
21737c4f63baSChristian Brauner 
21747c4f63baSChristian Brauner 	if (!netlink_ns_capable(skb, net->user_ns, cap)) {
21757c4f63baSChristian Brauner 		put_net(net);
21767c4f63baSChristian Brauner 		return ERR_PTR(-EPERM);
21777c4f63baSChristian Brauner 	}
21787c4f63baSChristian Brauner 
21797c4f63baSChristian Brauner 	return net;
21807c4f63baSChristian Brauner }
21817c4f63baSChristian Brauner 
21824ff66caeSChristian Brauner /* Verify that rtnetlink requests do not pass additional properties
21834ff66caeSChristian Brauner  * potentially referring to different network namespaces.
21844ff66caeSChristian Brauner  */
21854ff66caeSChristian Brauner static int rtnl_ensure_unique_netns(struct nlattr *tb[],
21864ff66caeSChristian Brauner 				    struct netlink_ext_ack *extack,
21874ff66caeSChristian Brauner 				    bool netns_id_only)
21884ff66caeSChristian Brauner {
21894ff66caeSChristian Brauner 
21904ff66caeSChristian Brauner 	if (netns_id_only) {
21914ff66caeSChristian Brauner 		if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD])
21924ff66caeSChristian Brauner 			return 0;
21934ff66caeSChristian Brauner 
21944ff66caeSChristian Brauner 		NL_SET_ERR_MSG(extack, "specified netns attribute not supported");
21954ff66caeSChristian Brauner 		return -EOPNOTSUPP;
21964ff66caeSChristian Brauner 	}
21974ff66caeSChristian Brauner 
21987e4a8d5aSChristian Brauner 	if (tb[IFLA_TARGET_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]))
21994ff66caeSChristian Brauner 		goto invalid_attr;
22004ff66caeSChristian Brauner 
22017e4a8d5aSChristian Brauner 	if (tb[IFLA_NET_NS_PID] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_FD]))
22024ff66caeSChristian Brauner 		goto invalid_attr;
22034ff66caeSChristian Brauner 
22047e4a8d5aSChristian Brauner 	if (tb[IFLA_NET_NS_FD] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_PID]))
22054ff66caeSChristian Brauner 		goto invalid_attr;
22064ff66caeSChristian Brauner 
22074ff66caeSChristian Brauner 	return 0;
22084ff66caeSChristian Brauner 
22094ff66caeSChristian Brauner invalid_attr:
22104ff66caeSChristian Brauner 	NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified");
22114ff66caeSChristian Brauner 	return -EINVAL;
22124ff66caeSChristian Brauner }
22134ff66caeSChristian Brauner 
22141840bb13SThomas Graf static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
22151840bb13SThomas Graf {
22161840bb13SThomas Graf 	if (dev) {
22171840bb13SThomas Graf 		if (tb[IFLA_ADDRESS] &&
22181840bb13SThomas Graf 		    nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
22191840bb13SThomas Graf 			return -EINVAL;
22201840bb13SThomas Graf 
22211840bb13SThomas Graf 		if (tb[IFLA_BROADCAST] &&
22221840bb13SThomas Graf 		    nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
22231840bb13SThomas Graf 			return -EINVAL;
22241840bb13SThomas Graf 	}
22251840bb13SThomas Graf 
2226cf7afbfeSThomas Graf 	if (tb[IFLA_AF_SPEC]) {
2227cf7afbfeSThomas Graf 		struct nlattr *af;
2228cf7afbfeSThomas Graf 		int rem, err;
2229cf7afbfeSThomas Graf 
2230cf7afbfeSThomas Graf 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2231cf7afbfeSThomas Graf 			const struct rtnl_af_ops *af_ops;
2232cf7afbfeSThomas Graf 
22335fa85a09SFlorian Westphal 			rcu_read_lock();
22345fa85a09SFlorian Westphal 			af_ops = rtnl_af_lookup(nla_type(af));
22355fa85a09SFlorian Westphal 			if (!af_ops) {
22365fa85a09SFlorian Westphal 				rcu_read_unlock();
2237cf7afbfeSThomas Graf 				return -EAFNOSUPPORT;
22385fa85a09SFlorian Westphal 			}
2239cf7afbfeSThomas Graf 
22405fa85a09SFlorian Westphal 			if (!af_ops->set_link_af) {
22415fa85a09SFlorian Westphal 				rcu_read_unlock();
2242cf7afbfeSThomas Graf 				return -EOPNOTSUPP;
22435fa85a09SFlorian Westphal 			}
2244cf7afbfeSThomas Graf 
2245cf7afbfeSThomas Graf 			if (af_ops->validate_link_af) {
22466d3a9a68SKurt Van Dijck 				err = af_ops->validate_link_af(dev, af);
22475fa85a09SFlorian Westphal 				if (err < 0) {
22485fa85a09SFlorian Westphal 					rcu_read_unlock();
2249cf7afbfeSThomas Graf 					return err;
2250cf7afbfeSThomas Graf 				}
2251cf7afbfeSThomas Graf 			}
22525fa85a09SFlorian Westphal 
22535fa85a09SFlorian Westphal 			rcu_read_unlock();
22545fa85a09SFlorian Westphal 		}
2255cf7afbfeSThomas Graf 	}
2256cf7afbfeSThomas Graf 
22571840bb13SThomas Graf 	return 0;
22581840bb13SThomas Graf }
22591840bb13SThomas Graf 
2260cc8e27ccSEli Cohen static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
2261cc8e27ccSEli Cohen 				  int guid_type)
2262cc8e27ccSEli Cohen {
2263cc8e27ccSEli Cohen 	const struct net_device_ops *ops = dev->netdev_ops;
2264cc8e27ccSEli Cohen 
2265cc8e27ccSEli Cohen 	return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
2266cc8e27ccSEli Cohen }
2267cc8e27ccSEli Cohen 
2268cc8e27ccSEli Cohen static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
2269cc8e27ccSEli Cohen {
2270cc8e27ccSEli Cohen 	if (dev->type != ARPHRD_INFINIBAND)
2271cc8e27ccSEli Cohen 		return -EOPNOTSUPP;
2272cc8e27ccSEli Cohen 
2273cc8e27ccSEli Cohen 	return handle_infiniband_guid(dev, ivt, guid_type);
2274cc8e27ccSEli Cohen }
2275cc8e27ccSEli Cohen 
22764f7d2cdfSDaniel Borkmann static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
2277c02db8c6SChris Wright {
2278c02db8c6SChris Wright 	const struct net_device_ops *ops = dev->netdev_ops;
22794f7d2cdfSDaniel Borkmann 	int err = -EINVAL;
2280c02db8c6SChris Wright 
22814f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_MAC]) {
22824f7d2cdfSDaniel Borkmann 		struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
22834f7d2cdfSDaniel Borkmann 
2284ff08ddbaSDan Carpenter 		if (ivm->vf >= INT_MAX)
2285ff08ddbaSDan Carpenter 			return -EINVAL;
2286c02db8c6SChris Wright 		err = -EOPNOTSUPP;
2287c02db8c6SChris Wright 		if (ops->ndo_set_vf_mac)
2288c02db8c6SChris Wright 			err = ops->ndo_set_vf_mac(dev, ivm->vf,
2289c02db8c6SChris Wright 						  ivm->mac);
22904f7d2cdfSDaniel Borkmann 		if (err < 0)
22914f7d2cdfSDaniel Borkmann 			return err;
2292c02db8c6SChris Wright 	}
22934f7d2cdfSDaniel Borkmann 
22944f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_VLAN]) {
22954f7d2cdfSDaniel Borkmann 		struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
22964f7d2cdfSDaniel Borkmann 
2297ff08ddbaSDan Carpenter 		if (ivv->vf >= INT_MAX)
2298ff08ddbaSDan Carpenter 			return -EINVAL;
2299c02db8c6SChris Wright 		err = -EOPNOTSUPP;
2300c02db8c6SChris Wright 		if (ops->ndo_set_vf_vlan)
23014f7d2cdfSDaniel Borkmann 			err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
230279aab093SMoshe Shemesh 						   ivv->qos,
230379aab093SMoshe Shemesh 						   htons(ETH_P_8021Q));
230479aab093SMoshe Shemesh 		if (err < 0)
230579aab093SMoshe Shemesh 			return err;
230679aab093SMoshe Shemesh 	}
230779aab093SMoshe Shemesh 
230879aab093SMoshe Shemesh 	if (tb[IFLA_VF_VLAN_LIST]) {
230979aab093SMoshe Shemesh 		struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
231079aab093SMoshe Shemesh 		struct nlattr *attr;
231179aab093SMoshe Shemesh 		int rem, len = 0;
231279aab093SMoshe Shemesh 
231379aab093SMoshe Shemesh 		err = -EOPNOTSUPP;
231479aab093SMoshe Shemesh 		if (!ops->ndo_set_vf_vlan)
231579aab093SMoshe Shemesh 			return err;
231679aab093SMoshe Shemesh 
231779aab093SMoshe Shemesh 		nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
231879aab093SMoshe Shemesh 			if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
231979aab093SMoshe Shemesh 			    nla_len(attr) < NLA_HDRLEN) {
232079aab093SMoshe Shemesh 				return -EINVAL;
232179aab093SMoshe Shemesh 			}
232279aab093SMoshe Shemesh 			if (len >= MAX_VLAN_LIST_LEN)
232379aab093SMoshe Shemesh 				return -EOPNOTSUPP;
232479aab093SMoshe Shemesh 			ivvl[len] = nla_data(attr);
232579aab093SMoshe Shemesh 
232679aab093SMoshe Shemesh 			len++;
232779aab093SMoshe Shemesh 		}
2328fa34cd94SArnd Bergmann 		if (len == 0)
2329fa34cd94SArnd Bergmann 			return -EINVAL;
2330fa34cd94SArnd Bergmann 
2331ff08ddbaSDan Carpenter 		if (ivvl[0]->vf >= INT_MAX)
2332ff08ddbaSDan Carpenter 			return -EINVAL;
233379aab093SMoshe Shemesh 		err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
233479aab093SMoshe Shemesh 					   ivvl[0]->qos, ivvl[0]->vlan_proto);
23354f7d2cdfSDaniel Borkmann 		if (err < 0)
23364f7d2cdfSDaniel Borkmann 			return err;
2337c02db8c6SChris Wright 	}
23384f7d2cdfSDaniel Borkmann 
23394f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_TX_RATE]) {
23404f7d2cdfSDaniel Borkmann 		struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
2341ed616689SSucheta Chakraborty 		struct ifla_vf_info ivf;
23424f7d2cdfSDaniel Borkmann 
2343ff08ddbaSDan Carpenter 		if (ivt->vf >= INT_MAX)
2344ff08ddbaSDan Carpenter 			return -EINVAL;
2345c02db8c6SChris Wright 		err = -EOPNOTSUPP;
2346ed616689SSucheta Chakraborty 		if (ops->ndo_get_vf_config)
23474f7d2cdfSDaniel Borkmann 			err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
23484f7d2cdfSDaniel Borkmann 		if (err < 0)
23494f7d2cdfSDaniel Borkmann 			return err;
23504f7d2cdfSDaniel Borkmann 
2351ed616689SSucheta Chakraborty 		err = -EOPNOTSUPP;
2352ed616689SSucheta Chakraborty 		if (ops->ndo_set_vf_rate)
2353ed616689SSucheta Chakraborty 			err = ops->ndo_set_vf_rate(dev, ivt->vf,
2354ed616689SSucheta Chakraborty 						   ivf.min_tx_rate,
2355c02db8c6SChris Wright 						   ivt->rate);
23564f7d2cdfSDaniel Borkmann 		if (err < 0)
23574f7d2cdfSDaniel Borkmann 			return err;
2358c02db8c6SChris Wright 	}
23594f7d2cdfSDaniel Borkmann 
23604f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_RATE]) {
23614f7d2cdfSDaniel Borkmann 		struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
23624f7d2cdfSDaniel Borkmann 
2363ff08ddbaSDan Carpenter 		if (ivt->vf >= INT_MAX)
2364ff08ddbaSDan Carpenter 			return -EINVAL;
2365ed616689SSucheta Chakraborty 		err = -EOPNOTSUPP;
2366ed616689SSucheta Chakraborty 		if (ops->ndo_set_vf_rate)
2367ed616689SSucheta Chakraborty 			err = ops->ndo_set_vf_rate(dev, ivt->vf,
2368ed616689SSucheta Chakraborty 						   ivt->min_tx_rate,
2369ed616689SSucheta Chakraborty 						   ivt->max_tx_rate);
23704f7d2cdfSDaniel Borkmann 		if (err < 0)
23714f7d2cdfSDaniel Borkmann 			return err;
2372ed616689SSucheta Chakraborty 	}
23734f7d2cdfSDaniel Borkmann 
23744f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_SPOOFCHK]) {
23754f7d2cdfSDaniel Borkmann 		struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
23764f7d2cdfSDaniel Borkmann 
2377ff08ddbaSDan Carpenter 		if (ivs->vf >= INT_MAX)
2378ff08ddbaSDan Carpenter 			return -EINVAL;
23795f8444a3SGreg Rose 		err = -EOPNOTSUPP;
23805f8444a3SGreg Rose 		if (ops->ndo_set_vf_spoofchk)
23815f8444a3SGreg Rose 			err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
23825f8444a3SGreg Rose 						       ivs->setting);
23834f7d2cdfSDaniel Borkmann 		if (err < 0)
23844f7d2cdfSDaniel Borkmann 			return err;
23855f8444a3SGreg Rose 	}
23864f7d2cdfSDaniel Borkmann 
23874f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_LINK_STATE]) {
23884f7d2cdfSDaniel Borkmann 		struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
23894f7d2cdfSDaniel Borkmann 
2390ff08ddbaSDan Carpenter 		if (ivl->vf >= INT_MAX)
2391ff08ddbaSDan Carpenter 			return -EINVAL;
23921d8faf48SRony Efraim 		err = -EOPNOTSUPP;
23931d8faf48SRony Efraim 		if (ops->ndo_set_vf_link_state)
23941d8faf48SRony Efraim 			err = ops->ndo_set_vf_link_state(dev, ivl->vf,
23951d8faf48SRony Efraim 							 ivl->link_state);
23964f7d2cdfSDaniel Borkmann 		if (err < 0)
23974f7d2cdfSDaniel Borkmann 			return err;
23981d8faf48SRony Efraim 	}
23994f7d2cdfSDaniel Borkmann 
24004f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_RSS_QUERY_EN]) {
240101a3d796SVlad Zolotarov 		struct ifla_vf_rss_query_en *ivrssq_en;
240201a3d796SVlad Zolotarov 
240301a3d796SVlad Zolotarov 		err = -EOPNOTSUPP;
24044f7d2cdfSDaniel Borkmann 		ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
2405ff08ddbaSDan Carpenter 		if (ivrssq_en->vf >= INT_MAX)
2406ff08ddbaSDan Carpenter 			return -EINVAL;
240701a3d796SVlad Zolotarov 		if (ops->ndo_set_vf_rss_query_en)
24084f7d2cdfSDaniel Borkmann 			err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
240901a3d796SVlad Zolotarov 							   ivrssq_en->setting);
24104f7d2cdfSDaniel Borkmann 		if (err < 0)
24114f7d2cdfSDaniel Borkmann 			return err;
241201a3d796SVlad Zolotarov 	}
24134f7d2cdfSDaniel Borkmann 
2414dd461d6aSHiroshi Shimamoto 	if (tb[IFLA_VF_TRUST]) {
2415dd461d6aSHiroshi Shimamoto 		struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
2416dd461d6aSHiroshi Shimamoto 
2417ff08ddbaSDan Carpenter 		if (ivt->vf >= INT_MAX)
2418ff08ddbaSDan Carpenter 			return -EINVAL;
2419dd461d6aSHiroshi Shimamoto 		err = -EOPNOTSUPP;
2420dd461d6aSHiroshi Shimamoto 		if (ops->ndo_set_vf_trust)
2421dd461d6aSHiroshi Shimamoto 			err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
2422dd461d6aSHiroshi Shimamoto 		if (err < 0)
2423dd461d6aSHiroshi Shimamoto 			return err;
2424dd461d6aSHiroshi Shimamoto 	}
2425dd461d6aSHiroshi Shimamoto 
2426cc8e27ccSEli Cohen 	if (tb[IFLA_VF_IB_NODE_GUID]) {
2427cc8e27ccSEli Cohen 		struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
2428cc8e27ccSEli Cohen 
2429ff08ddbaSDan Carpenter 		if (ivt->vf >= INT_MAX)
2430ff08ddbaSDan Carpenter 			return -EINVAL;
2431cc8e27ccSEli Cohen 		if (!ops->ndo_set_vf_guid)
2432cc8e27ccSEli Cohen 			return -EOPNOTSUPP;
2433cc8e27ccSEli Cohen 		return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
2434cc8e27ccSEli Cohen 	}
2435cc8e27ccSEli Cohen 
2436cc8e27ccSEli Cohen 	if (tb[IFLA_VF_IB_PORT_GUID]) {
2437cc8e27ccSEli Cohen 		struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
2438cc8e27ccSEli Cohen 
2439ff08ddbaSDan Carpenter 		if (ivt->vf >= INT_MAX)
2440ff08ddbaSDan Carpenter 			return -EINVAL;
2441cc8e27ccSEli Cohen 		if (!ops->ndo_set_vf_guid)
2442cc8e27ccSEli Cohen 			return -EOPNOTSUPP;
2443cc8e27ccSEli Cohen 
2444cc8e27ccSEli Cohen 		return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
2445cc8e27ccSEli Cohen 	}
2446cc8e27ccSEli Cohen 
2447c02db8c6SChris Wright 	return err;
2448c02db8c6SChris Wright }
2449c02db8c6SChris Wright 
245033eaf2a6SDavid Ahern static int do_set_master(struct net_device *dev, int ifindex,
245133eaf2a6SDavid Ahern 			 struct netlink_ext_ack *extack)
2452fbaec0eaSJiri Pirko {
2453898e5061SJiri Pirko 	struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
2454fbaec0eaSJiri Pirko 	const struct net_device_ops *ops;
2455fbaec0eaSJiri Pirko 	int err;
2456fbaec0eaSJiri Pirko 
2457898e5061SJiri Pirko 	if (upper_dev) {
2458898e5061SJiri Pirko 		if (upper_dev->ifindex == ifindex)
2459fbaec0eaSJiri Pirko 			return 0;
2460898e5061SJiri Pirko 		ops = upper_dev->netdev_ops;
2461fbaec0eaSJiri Pirko 		if (ops->ndo_del_slave) {
2462898e5061SJiri Pirko 			err = ops->ndo_del_slave(upper_dev, dev);
2463fbaec0eaSJiri Pirko 			if (err)
2464fbaec0eaSJiri Pirko 				return err;
2465ab92d68fSTaehee Yoo 			netdev_update_lockdep_key(dev);
2466fbaec0eaSJiri Pirko 		} else {
2467fbaec0eaSJiri Pirko 			return -EOPNOTSUPP;
2468fbaec0eaSJiri Pirko 		}
2469fbaec0eaSJiri Pirko 	}
2470fbaec0eaSJiri Pirko 
2471fbaec0eaSJiri Pirko 	if (ifindex) {
2472898e5061SJiri Pirko 		upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
2473898e5061SJiri Pirko 		if (!upper_dev)
2474fbaec0eaSJiri Pirko 			return -EINVAL;
2475898e5061SJiri Pirko 		ops = upper_dev->netdev_ops;
2476fbaec0eaSJiri Pirko 		if (ops->ndo_add_slave) {
247733eaf2a6SDavid Ahern 			err = ops->ndo_add_slave(upper_dev, dev, extack);
2478fbaec0eaSJiri Pirko 			if (err)
2479fbaec0eaSJiri Pirko 				return err;
2480fbaec0eaSJiri Pirko 		} else {
2481fbaec0eaSJiri Pirko 			return -EOPNOTSUPP;
2482fbaec0eaSJiri Pirko 		}
2483fbaec0eaSJiri Pirko 	}
2484fbaec0eaSJiri Pirko 	return 0;
2485fbaec0eaSJiri Pirko }
2486fbaec0eaSJiri Pirko 
248790c325e3SNicolas Dichtel #define DO_SETLINK_MODIFIED	0x01
2488ba998906SNicolas Dichtel /* notify flag means notify + modified. */
2489ba998906SNicolas Dichtel #define DO_SETLINK_NOTIFY	0x03
249090f62cf3SEric W. Biederman static int do_setlink(const struct sk_buff *skb,
249190f62cf3SEric W. Biederman 		      struct net_device *dev, struct ifinfomsg *ifm,
2492ddf9f970SJakub Kicinski 		      struct netlink_ext_ack *extack,
249390c325e3SNicolas Dichtel 		      struct nlattr **tb, char *ifname, int status)
24940157f60cSPatrick McHardy {
2495d314774cSStephen Hemminger 	const struct net_device_ops *ops = dev->netdev_ops;
24960157f60cSPatrick McHardy 	int err;
24970157f60cSPatrick McHardy 
2498644c7eebSEric Dumazet 	err = validate_linkmsg(dev, tb);
2499644c7eebSEric Dumazet 	if (err < 0)
2500644c7eebSEric Dumazet 		return err;
2501644c7eebSEric Dumazet 
25027e4a8d5aSChristian Brauner 	if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_TARGET_NETNSID]) {
25037c4f63baSChristian Brauner 		struct net *net = rtnl_link_get_net_capable(skb, dev_net(dev),
25047c4f63baSChristian Brauner 							    tb, CAP_NET_ADMIN);
2505d8a5ec67SEric W. Biederman 		if (IS_ERR(net)) {
2506d8a5ec67SEric W. Biederman 			err = PTR_ERR(net);
2507d8a5ec67SEric W. Biederman 			goto errout;
2508d8a5ec67SEric W. Biederman 		}
25097c4f63baSChristian Brauner 
2510d8a5ec67SEric W. Biederman 		err = dev_change_net_namespace(dev, net, ifname);
2511d8a5ec67SEric W. Biederman 		put_net(net);
2512d8a5ec67SEric W. Biederman 		if (err)
2513d8a5ec67SEric W. Biederman 			goto errout;
251490c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
2515d8a5ec67SEric W. Biederman 	}
2516d8a5ec67SEric W. Biederman 
25170157f60cSPatrick McHardy 	if (tb[IFLA_MAP]) {
25180157f60cSPatrick McHardy 		struct rtnl_link_ifmap *u_map;
25190157f60cSPatrick McHardy 		struct ifmap k_map;
25200157f60cSPatrick McHardy 
2521d314774cSStephen Hemminger 		if (!ops->ndo_set_config) {
25220157f60cSPatrick McHardy 			err = -EOPNOTSUPP;
25230157f60cSPatrick McHardy 			goto errout;
25240157f60cSPatrick McHardy 		}
25250157f60cSPatrick McHardy 
25260157f60cSPatrick McHardy 		if (!netif_device_present(dev)) {
25270157f60cSPatrick McHardy 			err = -ENODEV;
25280157f60cSPatrick McHardy 			goto errout;
25290157f60cSPatrick McHardy 		}
25300157f60cSPatrick McHardy 
25310157f60cSPatrick McHardy 		u_map = nla_data(tb[IFLA_MAP]);
25320157f60cSPatrick McHardy 		k_map.mem_start = (unsigned long) u_map->mem_start;
25330157f60cSPatrick McHardy 		k_map.mem_end = (unsigned long) u_map->mem_end;
25340157f60cSPatrick McHardy 		k_map.base_addr = (unsigned short) u_map->base_addr;
25350157f60cSPatrick McHardy 		k_map.irq = (unsigned char) u_map->irq;
25360157f60cSPatrick McHardy 		k_map.dma = (unsigned char) u_map->dma;
25370157f60cSPatrick McHardy 		k_map.port = (unsigned char) u_map->port;
25380157f60cSPatrick McHardy 
2539d314774cSStephen Hemminger 		err = ops->ndo_set_config(dev, &k_map);
25400157f60cSPatrick McHardy 		if (err < 0)
25410157f60cSPatrick McHardy 			goto errout;
25420157f60cSPatrick McHardy 
2543ba998906SNicolas Dichtel 		status |= DO_SETLINK_NOTIFY;
25440157f60cSPatrick McHardy 	}
25450157f60cSPatrick McHardy 
25460157f60cSPatrick McHardy 	if (tb[IFLA_ADDRESS]) {
25470157f60cSPatrick McHardy 		struct sockaddr *sa;
25480157f60cSPatrick McHardy 		int len;
25490157f60cSPatrick McHardy 
2550153711f9SWANG Cong 		len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
2551153711f9SWANG Cong 						  sizeof(*sa));
25520157f60cSPatrick McHardy 		sa = kmalloc(len, GFP_KERNEL);
25530157f60cSPatrick McHardy 		if (!sa) {
25540157f60cSPatrick McHardy 			err = -ENOMEM;
25550157f60cSPatrick McHardy 			goto errout;
25560157f60cSPatrick McHardy 		}
25570157f60cSPatrick McHardy 		sa->sa_family = dev->type;
25580157f60cSPatrick McHardy 		memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
25590157f60cSPatrick McHardy 		       dev->addr_len);
25603a37a963SPetr Machata 		err = dev_set_mac_address(dev, sa, extack);
25610157f60cSPatrick McHardy 		kfree(sa);
25620157f60cSPatrick McHardy 		if (err)
25630157f60cSPatrick McHardy 			goto errout;
256490c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
25650157f60cSPatrick McHardy 	}
25660157f60cSPatrick McHardy 
25670157f60cSPatrick McHardy 	if (tb[IFLA_MTU]) {
25687a4c53beSStephen Hemminger 		err = dev_set_mtu_ext(dev, nla_get_u32(tb[IFLA_MTU]), extack);
25690157f60cSPatrick McHardy 		if (err < 0)
25700157f60cSPatrick McHardy 			goto errout;
257190c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
25720157f60cSPatrick McHardy 	}
25730157f60cSPatrick McHardy 
2574cbda10faSVlad Dogaru 	if (tb[IFLA_GROUP]) {
2575cbda10faSVlad Dogaru 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
2576ba998906SNicolas Dichtel 		status |= DO_SETLINK_NOTIFY;
2577cbda10faSVlad Dogaru 	}
2578cbda10faSVlad Dogaru 
25790157f60cSPatrick McHardy 	/*
25800157f60cSPatrick McHardy 	 * Interface selected by interface index but interface
25810157f60cSPatrick McHardy 	 * name provided implies that a name change has been
25820157f60cSPatrick McHardy 	 * requested.
25830157f60cSPatrick McHardy 	 */
25840157f60cSPatrick McHardy 	if (ifm->ifi_index > 0 && ifname[0]) {
25850157f60cSPatrick McHardy 		err = dev_change_name(dev, ifname);
25860157f60cSPatrick McHardy 		if (err < 0)
25870157f60cSPatrick McHardy 			goto errout;
258890c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
25890157f60cSPatrick McHardy 	}
25900157f60cSPatrick McHardy 
25910b815a1aSStephen Hemminger 	if (tb[IFLA_IFALIAS]) {
25920b815a1aSStephen Hemminger 		err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
25930b815a1aSStephen Hemminger 				    nla_len(tb[IFLA_IFALIAS]));
25940b815a1aSStephen Hemminger 		if (err < 0)
25950b815a1aSStephen Hemminger 			goto errout;
2596ba998906SNicolas Dichtel 		status |= DO_SETLINK_NOTIFY;
25970b815a1aSStephen Hemminger 	}
25980b815a1aSStephen Hemminger 
25990157f60cSPatrick McHardy 	if (tb[IFLA_BROADCAST]) {
26000157f60cSPatrick McHardy 		nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
2601e7c3273eSJiri Pirko 		call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
26020157f60cSPatrick McHardy 	}
26030157f60cSPatrick McHardy 
26040157f60cSPatrick McHardy 	if (ifm->ifi_flags || ifm->ifi_change) {
2605567c5e13SPetr Machata 		err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
2606567c5e13SPetr Machata 				       extack);
26075f9021cfSJohannes Berg 		if (err < 0)
26085f9021cfSJohannes Berg 			goto errout;
26090157f60cSPatrick McHardy 	}
26100157f60cSPatrick McHardy 
2611fbaec0eaSJiri Pirko 	if (tb[IFLA_MASTER]) {
261233eaf2a6SDavid Ahern 		err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
2613fbaec0eaSJiri Pirko 		if (err)
2614fbaec0eaSJiri Pirko 			goto errout;
261590c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
2616fbaec0eaSJiri Pirko 	}
2617fbaec0eaSJiri Pirko 
26189a57247fSJiri Pirko 	if (tb[IFLA_CARRIER]) {
26199a57247fSJiri Pirko 		err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
26209a57247fSJiri Pirko 		if (err)
26219a57247fSJiri Pirko 			goto errout;
262290c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
26239a57247fSJiri Pirko 	}
26249a57247fSJiri Pirko 
26255d1180fcSNicolas Dichtel 	if (tb[IFLA_TXQLEN]) {
26260cd29503SAlexey Dobriyan 		unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]);
26275d1180fcSNicolas Dichtel 
26286a643ddbSCong Wang 		err = dev_change_tx_queue_len(dev, value);
26296a643ddbSCong Wang 		if (err)
263008294a26SJason Wang 			goto errout;
26312d7f669bSXin Long 		status |= DO_SETLINK_MODIFIED;
263208294a26SJason Wang 	}
26330157f60cSPatrick McHardy 
263446e6b992SStephen Hemminger 	if (tb[IFLA_GSO_MAX_SIZE]) {
263546e6b992SStephen Hemminger 		u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]);
263646e6b992SStephen Hemminger 
263746e6b992SStephen Hemminger 		if (max_size > GSO_MAX_SIZE) {
263846e6b992SStephen Hemminger 			err = -EINVAL;
263946e6b992SStephen Hemminger 			goto errout;
264046e6b992SStephen Hemminger 		}
264146e6b992SStephen Hemminger 
264246e6b992SStephen Hemminger 		if (dev->gso_max_size ^ max_size) {
264346e6b992SStephen Hemminger 			netif_set_gso_max_size(dev, max_size);
264446e6b992SStephen Hemminger 			status |= DO_SETLINK_MODIFIED;
264546e6b992SStephen Hemminger 		}
264646e6b992SStephen Hemminger 	}
264746e6b992SStephen Hemminger 
264846e6b992SStephen Hemminger 	if (tb[IFLA_GSO_MAX_SEGS]) {
264946e6b992SStephen Hemminger 		u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
265046e6b992SStephen Hemminger 
265146e6b992SStephen Hemminger 		if (max_segs > GSO_MAX_SEGS) {
265246e6b992SStephen Hemminger 			err = -EINVAL;
265346e6b992SStephen Hemminger 			goto errout;
265446e6b992SStephen Hemminger 		}
265546e6b992SStephen Hemminger 
265646e6b992SStephen Hemminger 		if (dev->gso_max_segs ^ max_segs) {
265746e6b992SStephen Hemminger 			dev->gso_max_segs = max_segs;
265846e6b992SStephen Hemminger 			status |= DO_SETLINK_MODIFIED;
265946e6b992SStephen Hemminger 		}
266046e6b992SStephen Hemminger 	}
266146e6b992SStephen Hemminger 
26620157f60cSPatrick McHardy 	if (tb[IFLA_OPERSTATE])
266393b2d4a2SDavid S. Miller 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
26640157f60cSPatrick McHardy 
26650157f60cSPatrick McHardy 	if (tb[IFLA_LINKMODE]) {
26661889b0e7SNicolas Dichtel 		unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
26671889b0e7SNicolas Dichtel 
26680157f60cSPatrick McHardy 		write_lock_bh(&dev_base_lock);
26691889b0e7SNicolas Dichtel 		if (dev->link_mode ^ value)
2670ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
26711889b0e7SNicolas Dichtel 		dev->link_mode = value;
267293b2d4a2SDavid S. Miller 		write_unlock_bh(&dev_base_lock);
26730157f60cSPatrick McHardy 	}
26740157f60cSPatrick McHardy 
2675c02db8c6SChris Wright 	if (tb[IFLA_VFINFO_LIST]) {
26764f7d2cdfSDaniel Borkmann 		struct nlattr *vfinfo[IFLA_VF_MAX + 1];
2677c02db8c6SChris Wright 		struct nlattr *attr;
2678c02db8c6SChris Wright 		int rem;
26794f7d2cdfSDaniel Borkmann 
2680c02db8c6SChris Wright 		nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
26814f7d2cdfSDaniel Borkmann 			if (nla_type(attr) != IFLA_VF_INFO ||
26824f7d2cdfSDaniel Borkmann 			    nla_len(attr) < NLA_HDRLEN) {
2683253683bbSDavid Howells 				err = -EINVAL;
2684c02db8c6SChris Wright 				goto errout;
2685253683bbSDavid Howells 			}
26868cb08174SJohannes Berg 			err = nla_parse_nested_deprecated(vfinfo, IFLA_VF_MAX,
26878cb08174SJohannes Berg 							  attr,
26888cb08174SJohannes Berg 							  ifla_vf_policy,
26898cb08174SJohannes Berg 							  NULL);
26904f7d2cdfSDaniel Borkmann 			if (err < 0)
26914f7d2cdfSDaniel Borkmann 				goto errout;
26924f7d2cdfSDaniel Borkmann 			err = do_setvfinfo(dev, vfinfo);
2693ebc08a6fSWilliams, Mitch A 			if (err < 0)
2694ebc08a6fSWilliams, Mitch A 				goto errout;
2695ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
2696ebc08a6fSWilliams, Mitch A 		}
2697ebc08a6fSWilliams, Mitch A 	}
26980157f60cSPatrick McHardy 	err = 0;
26990157f60cSPatrick McHardy 
270057b61080SScott Feldman 	if (tb[IFLA_VF_PORTS]) {
270157b61080SScott Feldman 		struct nlattr *port[IFLA_PORT_MAX+1];
270257b61080SScott Feldman 		struct nlattr *attr;
270357b61080SScott Feldman 		int vf;
270457b61080SScott Feldman 		int rem;
270557b61080SScott Feldman 
270657b61080SScott Feldman 		err = -EOPNOTSUPP;
270757b61080SScott Feldman 		if (!ops->ndo_set_vf_port)
270857b61080SScott Feldman 			goto errout;
270957b61080SScott Feldman 
271057b61080SScott Feldman 		nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
2711035d210fSDaniel Borkmann 			if (nla_type(attr) != IFLA_VF_PORT ||
2712035d210fSDaniel Borkmann 			    nla_len(attr) < NLA_HDRLEN) {
2713035d210fSDaniel Borkmann 				err = -EINVAL;
2714035d210fSDaniel Borkmann 				goto errout;
2715035d210fSDaniel Borkmann 			}
27168cb08174SJohannes Berg 			err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
27178cb08174SJohannes Berg 							  attr,
27188cb08174SJohannes Berg 							  ifla_port_policy,
27198cb08174SJohannes Berg 							  NULL);
272057b61080SScott Feldman 			if (err < 0)
272157b61080SScott Feldman 				goto errout;
272257b61080SScott Feldman 			if (!port[IFLA_PORT_VF]) {
272357b61080SScott Feldman 				err = -EOPNOTSUPP;
272457b61080SScott Feldman 				goto errout;
272557b61080SScott Feldman 			}
272657b61080SScott Feldman 			vf = nla_get_u32(port[IFLA_PORT_VF]);
272757b61080SScott Feldman 			err = ops->ndo_set_vf_port(dev, vf, port);
272857b61080SScott Feldman 			if (err < 0)
272957b61080SScott Feldman 				goto errout;
2730ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
273157b61080SScott Feldman 		}
273257b61080SScott Feldman 	}
273357b61080SScott Feldman 	err = 0;
273457b61080SScott Feldman 
273557b61080SScott Feldman 	if (tb[IFLA_PORT_SELF]) {
273657b61080SScott Feldman 		struct nlattr *port[IFLA_PORT_MAX+1];
273757b61080SScott Feldman 
27388cb08174SJohannes Berg 		err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
27398cb08174SJohannes Berg 						  tb[IFLA_PORT_SELF],
27408cb08174SJohannes Berg 						  ifla_port_policy, NULL);
274157b61080SScott Feldman 		if (err < 0)
274257b61080SScott Feldman 			goto errout;
274357b61080SScott Feldman 
274457b61080SScott Feldman 		err = -EOPNOTSUPP;
274557b61080SScott Feldman 		if (ops->ndo_set_vf_port)
274657b61080SScott Feldman 			err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
274757b61080SScott Feldman 		if (err < 0)
274857b61080SScott Feldman 			goto errout;
2749ba998906SNicolas Dichtel 		status |= DO_SETLINK_NOTIFY;
275057b61080SScott Feldman 	}
2751f8ff182cSThomas Graf 
2752f8ff182cSThomas Graf 	if (tb[IFLA_AF_SPEC]) {
2753f8ff182cSThomas Graf 		struct nlattr *af;
2754f8ff182cSThomas Graf 		int rem;
2755f8ff182cSThomas Graf 
2756f8ff182cSThomas Graf 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2757f8ff182cSThomas Graf 			const struct rtnl_af_ops *af_ops;
2758f8ff182cSThomas Graf 
27595fa85a09SFlorian Westphal 			rcu_read_lock();
27605fa85a09SFlorian Westphal 
2761058c8d59SGustavo A. R. Silva 			BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af))));
2762f8ff182cSThomas Graf 
2763cf7afbfeSThomas Graf 			err = af_ops->set_link_af(dev, af);
27645fa85a09SFlorian Westphal 			if (err < 0) {
27655fa85a09SFlorian Westphal 				rcu_read_unlock();
2766f8ff182cSThomas Graf 				goto errout;
27675fa85a09SFlorian Westphal 			}
2768f8ff182cSThomas Graf 
27695fa85a09SFlorian Westphal 			rcu_read_unlock();
2770ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
2771f8ff182cSThomas Graf 		}
2772f8ff182cSThomas Graf 	}
277357b61080SScott Feldman 	err = 0;
277457b61080SScott Feldman 
277588d6378bSAnuradha Karuppiah 	if (tb[IFLA_PROTO_DOWN]) {
277688d6378bSAnuradha Karuppiah 		err = dev_change_proto_down(dev,
277788d6378bSAnuradha Karuppiah 					    nla_get_u8(tb[IFLA_PROTO_DOWN]));
277888d6378bSAnuradha Karuppiah 		if (err)
277988d6378bSAnuradha Karuppiah 			goto errout;
278088d6378bSAnuradha Karuppiah 		status |= DO_SETLINK_NOTIFY;
278188d6378bSAnuradha Karuppiah 	}
278288d6378bSAnuradha Karuppiah 
2783d1fdd913SBrenden Blanco 	if (tb[IFLA_XDP]) {
2784d1fdd913SBrenden Blanco 		struct nlattr *xdp[IFLA_XDP_MAX + 1];
278585de8576SDaniel Borkmann 		u32 xdp_flags = 0;
2786d1fdd913SBrenden Blanco 
27878cb08174SJohannes Berg 		err = nla_parse_nested_deprecated(xdp, IFLA_XDP_MAX,
27888cb08174SJohannes Berg 						  tb[IFLA_XDP],
2789fceb6435SJohannes Berg 						  ifla_xdp_policy, NULL);
2790d1fdd913SBrenden Blanco 		if (err < 0)
2791d1fdd913SBrenden Blanco 			goto errout;
2792d1fdd913SBrenden Blanco 
279358038695SMartin KaFai Lau 		if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
2794262d8625SBrenden Blanco 			err = -EINVAL;
2795262d8625SBrenden Blanco 			goto errout;
2796262d8625SBrenden Blanco 		}
279785de8576SDaniel Borkmann 
279885de8576SDaniel Borkmann 		if (xdp[IFLA_XDP_FLAGS]) {
279985de8576SDaniel Borkmann 			xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
280085de8576SDaniel Borkmann 			if (xdp_flags & ~XDP_FLAGS_MASK) {
280185de8576SDaniel Borkmann 				err = -EINVAL;
280285de8576SDaniel Borkmann 				goto errout;
280385de8576SDaniel Borkmann 			}
2804ee5d032fSJakub Kicinski 			if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) {
28050489df9aSDaniel Borkmann 				err = -EINVAL;
28060489df9aSDaniel Borkmann 				goto errout;
28070489df9aSDaniel Borkmann 			}
280885de8576SDaniel Borkmann 		}
280985de8576SDaniel Borkmann 
2810d1fdd913SBrenden Blanco 		if (xdp[IFLA_XDP_FD]) {
281192234c8fSToke Høiland-Jørgensen 			int expected_fd = -1;
281292234c8fSToke Høiland-Jørgensen 
281392234c8fSToke Høiland-Jørgensen 			if (xdp_flags & XDP_FLAGS_REPLACE) {
281492234c8fSToke Høiland-Jørgensen 				if (!xdp[IFLA_XDP_EXPECTED_FD]) {
281592234c8fSToke Høiland-Jørgensen 					err = -EINVAL;
281692234c8fSToke Høiland-Jørgensen 					goto errout;
281792234c8fSToke Høiland-Jørgensen 				}
281892234c8fSToke Høiland-Jørgensen 				expected_fd =
281992234c8fSToke Høiland-Jørgensen 					nla_get_s32(xdp[IFLA_XDP_EXPECTED_FD]);
282092234c8fSToke Høiland-Jørgensen 			}
282192234c8fSToke Høiland-Jørgensen 
2822ddf9f970SJakub Kicinski 			err = dev_change_xdp_fd(dev, extack,
282385de8576SDaniel Borkmann 						nla_get_s32(xdp[IFLA_XDP_FD]),
282492234c8fSToke Høiland-Jørgensen 						expected_fd,
282585de8576SDaniel Borkmann 						xdp_flags);
2826d1fdd913SBrenden Blanco 			if (err)
2827d1fdd913SBrenden Blanco 				goto errout;
2828d1fdd913SBrenden Blanco 			status |= DO_SETLINK_NOTIFY;
2829d1fdd913SBrenden Blanco 		}
2830d1fdd913SBrenden Blanco 	}
2831d1fdd913SBrenden Blanco 
28320157f60cSPatrick McHardy errout:
2833ba998906SNicolas Dichtel 	if (status & DO_SETLINK_MODIFIED) {
283464ff90ccSXin Long 		if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY)
2835ba998906SNicolas Dichtel 			netdev_state_change(dev);
2836ba998906SNicolas Dichtel 
2837ba998906SNicolas Dichtel 		if (err < 0)
2838e87cc472SJoe 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",
2839e87cc472SJoe Perches 					     dev->name);
2840ba998906SNicolas Dichtel 	}
28410157f60cSPatrick McHardy 
28420157f60cSPatrick McHardy 	return err;
28430157f60cSPatrick McHardy }
28440157f60cSPatrick McHardy 
2845cc6090e9SJiri Pirko static struct net_device *rtnl_dev_get(struct net *net,
2846cc6090e9SJiri Pirko 				       struct nlattr *ifname_attr,
284776c9ac0eSJiri Pirko 				       struct nlattr *altifname_attr,
2848cc6090e9SJiri Pirko 				       char *ifname)
2849cc6090e9SJiri Pirko {
285076c9ac0eSJiri Pirko 	char buffer[ALTIFNAMSIZ];
2851cc6090e9SJiri Pirko 
2852cc6090e9SJiri Pirko 	if (!ifname) {
2853cc6090e9SJiri Pirko 		ifname = buffer;
2854cc6090e9SJiri Pirko 		if (ifname_attr)
2855cc6090e9SJiri Pirko 			nla_strlcpy(ifname, ifname_attr, IFNAMSIZ);
285676c9ac0eSJiri Pirko 		else if (altifname_attr)
285776c9ac0eSJiri Pirko 			nla_strlcpy(ifname, altifname_attr, ALTIFNAMSIZ);
2858cc6090e9SJiri Pirko 		else
2859cc6090e9SJiri Pirko 			return NULL;
2860cc6090e9SJiri Pirko 	}
2861cc6090e9SJiri Pirko 
2862cc6090e9SJiri Pirko 	return __dev_get_by_name(net, ifname);
2863cc6090e9SJiri Pirko }
2864cc6090e9SJiri Pirko 
2865c21ef3e3SDavid Ahern static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2866c21ef3e3SDavid Ahern 			struct netlink_ext_ack *extack)
2867da5e0494SThomas Graf {
28683b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
2869da5e0494SThomas Graf 	struct ifinfomsg *ifm;
2870da5e0494SThomas Graf 	struct net_device *dev;
28710157f60cSPatrick McHardy 	int err;
2872da5e0494SThomas Graf 	struct nlattr *tb[IFLA_MAX+1];
28731da177e4SLinus Torvalds 	char ifname[IFNAMSIZ];
28741da177e4SLinus Torvalds 
28758cb08174SJohannes Berg 	err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
28768cb08174SJohannes Berg 				     ifla_policy, extack);
2877da5e0494SThomas Graf 	if (err < 0)
2878da5e0494SThomas Graf 		goto errout;
28791da177e4SLinus Torvalds 
28804ff66caeSChristian Brauner 	err = rtnl_ensure_unique_netns(tb, extack, false);
28814ff66caeSChristian Brauner 	if (err < 0)
28824ff66caeSChristian Brauner 		goto errout;
28834ff66caeSChristian Brauner 
28845176f91eSThomas Graf 	if (tb[IFLA_IFNAME])
28855176f91eSThomas Graf 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
288678e5b891SPatrick McHardy 	else
288778e5b891SPatrick McHardy 		ifname[0] = '\0';
28881da177e4SLinus Torvalds 
28891da177e4SLinus Torvalds 	err = -EINVAL;
2890da5e0494SThomas Graf 	ifm = nlmsg_data(nlh);
289151055be8SPatrick McHardy 	if (ifm->ifi_index > 0)
2892a3d12891SEric Dumazet 		dev = __dev_get_by_index(net, ifm->ifi_index);
289376c9ac0eSJiri Pirko 	else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
289476c9ac0eSJiri Pirko 		dev = rtnl_dev_get(net, NULL, tb[IFLA_ALT_IFNAME], ifname);
2895da5e0494SThomas Graf 	else
2896da5e0494SThomas Graf 		goto errout;
28971da177e4SLinus Torvalds 
2898da5e0494SThomas Graf 	if (dev == NULL) {
2899da5e0494SThomas Graf 		err = -ENODEV;
2900da5e0494SThomas Graf 		goto errout;
2901da5e0494SThomas Graf 	}
29021da177e4SLinus Torvalds 
2903ddf9f970SJakub Kicinski 	err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0);
2904da5e0494SThomas Graf errout:
29051da177e4SLinus Torvalds 	return err;
29061da177e4SLinus Torvalds }
29071da177e4SLinus Torvalds 
290866400d54SWANG Cong static int rtnl_group_dellink(const struct net *net, int group)
290966400d54SWANG Cong {
291066400d54SWANG Cong 	struct net_device *dev, *aux;
291166400d54SWANG Cong 	LIST_HEAD(list_kill);
291266400d54SWANG Cong 	bool found = false;
291366400d54SWANG Cong 
291466400d54SWANG Cong 	if (!group)
291566400d54SWANG Cong 		return -EPERM;
291666400d54SWANG Cong 
291766400d54SWANG Cong 	for_each_netdev(net, dev) {
291866400d54SWANG Cong 		if (dev->group == group) {
291966400d54SWANG Cong 			const struct rtnl_link_ops *ops;
292066400d54SWANG Cong 
292166400d54SWANG Cong 			found = true;
292266400d54SWANG Cong 			ops = dev->rtnl_link_ops;
292366400d54SWANG Cong 			if (!ops || !ops->dellink)
292466400d54SWANG Cong 				return -EOPNOTSUPP;
292566400d54SWANG Cong 		}
292666400d54SWANG Cong 	}
292766400d54SWANG Cong 
292866400d54SWANG Cong 	if (!found)
292966400d54SWANG Cong 		return -ENODEV;
293066400d54SWANG Cong 
293166400d54SWANG Cong 	for_each_netdev_safe(net, dev, aux) {
293266400d54SWANG Cong 		if (dev->group == group) {
293366400d54SWANG Cong 			const struct rtnl_link_ops *ops;
293466400d54SWANG Cong 
293566400d54SWANG Cong 			ops = dev->rtnl_link_ops;
293666400d54SWANG Cong 			ops->dellink(dev, &list_kill);
293766400d54SWANG Cong 		}
293866400d54SWANG Cong 	}
293966400d54SWANG Cong 	unregister_netdevice_many(&list_kill);
294066400d54SWANG Cong 
294166400d54SWANG Cong 	return 0;
294266400d54SWANG Cong }
294366400d54SWANG Cong 
2944614732eaSThomas Graf int rtnl_delete_link(struct net_device *dev)
2945614732eaSThomas Graf {
2946614732eaSThomas Graf 	const struct rtnl_link_ops *ops;
2947614732eaSThomas Graf 	LIST_HEAD(list_kill);
2948614732eaSThomas Graf 
2949614732eaSThomas Graf 	ops = dev->rtnl_link_ops;
2950614732eaSThomas Graf 	if (!ops || !ops->dellink)
2951614732eaSThomas Graf 		return -EOPNOTSUPP;
2952614732eaSThomas Graf 
2953614732eaSThomas Graf 	ops->dellink(dev, &list_kill);
2954614732eaSThomas Graf 	unregister_netdevice_many(&list_kill);
2955614732eaSThomas Graf 
2956614732eaSThomas Graf 	return 0;
2957614732eaSThomas Graf }
2958614732eaSThomas Graf EXPORT_SYMBOL_GPL(rtnl_delete_link);
2959614732eaSThomas Graf 
2960c21ef3e3SDavid Ahern static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
2961c21ef3e3SDavid Ahern 			struct netlink_ext_ack *extack)
296238f7b870SPatrick McHardy {
29633b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
2964b61ad68aSChristian Brauner 	struct net *tgt_net = net;
2965b61ad68aSChristian Brauner 	struct net_device *dev = NULL;
296638f7b870SPatrick McHardy 	struct ifinfomsg *ifm;
296738f7b870SPatrick McHardy 	struct nlattr *tb[IFLA_MAX+1];
296838f7b870SPatrick McHardy 	int err;
2969b61ad68aSChristian Brauner 	int netnsid = -1;
297038f7b870SPatrick McHardy 
29718cb08174SJohannes Berg 	err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
29728cb08174SJohannes Berg 				     ifla_policy, extack);
297338f7b870SPatrick McHardy 	if (err < 0)
297438f7b870SPatrick McHardy 		return err;
297538f7b870SPatrick McHardy 
29764ff66caeSChristian Brauner 	err = rtnl_ensure_unique_netns(tb, extack, true);
29774ff66caeSChristian Brauner 	if (err < 0)
29784ff66caeSChristian Brauner 		return err;
29794ff66caeSChristian Brauner 
29807e4a8d5aSChristian Brauner 	if (tb[IFLA_TARGET_NETNSID]) {
29817e4a8d5aSChristian Brauner 		netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
2982c383edc4SChristian Brauner 		tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
2983b61ad68aSChristian Brauner 		if (IS_ERR(tgt_net))
2984b61ad68aSChristian Brauner 			return PTR_ERR(tgt_net);
2985b61ad68aSChristian Brauner 	}
2986b61ad68aSChristian Brauner 
2987b61ad68aSChristian Brauner 	err = -EINVAL;
298838f7b870SPatrick McHardy 	ifm = nlmsg_data(nlh);
298938f7b870SPatrick McHardy 	if (ifm->ifi_index > 0)
2990b61ad68aSChristian Brauner 		dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
299176c9ac0eSJiri Pirko 	else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
299276c9ac0eSJiri Pirko 		dev = rtnl_dev_get(net, tb[IFLA_IFNAME],
299376c9ac0eSJiri Pirko 				   tb[IFLA_ALT_IFNAME], NULL);
299466400d54SWANG Cong 	else if (tb[IFLA_GROUP])
2995b61ad68aSChristian Brauner 		err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP]));
299638f7b870SPatrick McHardy 	else
2997b61ad68aSChristian Brauner 		goto out;
299838f7b870SPatrick McHardy 
2999b61ad68aSChristian Brauner 	if (!dev) {
3000b61ad68aSChristian Brauner 		if (tb[IFLA_IFNAME] || ifm->ifi_index > 0)
3001b61ad68aSChristian Brauner 			err = -ENODEV;
300238f7b870SPatrick McHardy 
3003b61ad68aSChristian Brauner 		goto out;
3004b61ad68aSChristian Brauner 	}
3005b61ad68aSChristian Brauner 
3006b61ad68aSChristian Brauner 	err = rtnl_delete_link(dev);
3007b61ad68aSChristian Brauner 
3008b61ad68aSChristian Brauner out:
3009b61ad68aSChristian Brauner 	if (netnsid >= 0)
3010b61ad68aSChristian Brauner 		put_net(tgt_net);
3011b61ad68aSChristian Brauner 
3012b61ad68aSChristian Brauner 	return err;
301338f7b870SPatrick McHardy }
301438f7b870SPatrick McHardy 
30153729d502SPatrick McHardy int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
30163729d502SPatrick McHardy {
30173729d502SPatrick McHardy 	unsigned int old_flags;
30183729d502SPatrick McHardy 	int err;
30193729d502SPatrick McHardy 
30203729d502SPatrick McHardy 	old_flags = dev->flags;
30213729d502SPatrick McHardy 	if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
30226d040321SPetr Machata 		err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
30236d040321SPetr Machata 					 NULL);
30243729d502SPatrick McHardy 		if (err < 0)
30253729d502SPatrick McHardy 			return err;
30263729d502SPatrick McHardy 	}
30273729d502SPatrick McHardy 
30288d356b89SRoopa Prabhu 	if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) {
302956a49d70SRoopa Prabhu 		__dev_notify_flags(dev, old_flags, (old_flags ^ dev->flags));
30308d356b89SRoopa Prabhu 	} else {
30313729d502SPatrick McHardy 		dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
3032a528c219SNicolas Dichtel 		__dev_notify_flags(dev, old_flags, ~0U);
30338d356b89SRoopa Prabhu 	}
30343729d502SPatrick McHardy 	return 0;
30353729d502SPatrick McHardy }
30363729d502SPatrick McHardy EXPORT_SYMBOL(rtnl_configure_link);
30373729d502SPatrick McHardy 
3038d0522f1cSDavid Ahern struct net_device *rtnl_create_link(struct net *net, const char *ifname,
3039d0522f1cSDavid Ahern 				    unsigned char name_assign_type,
3040d0522f1cSDavid Ahern 				    const struct rtnl_link_ops *ops,
3041d0522f1cSDavid Ahern 				    struct nlattr *tb[],
3042d0522f1cSDavid Ahern 				    struct netlink_ext_ack *extack)
3043e7199288SPavel Emelianov {
3044e7199288SPavel Emelianov 	struct net_device *dev;
3045d40156aaSJiri Pirko 	unsigned int num_tx_queues = 1;
3046d40156aaSJiri Pirko 	unsigned int num_rx_queues = 1;
3047e7199288SPavel Emelianov 
304876ff5cc9SJiri Pirko 	if (tb[IFLA_NUM_TX_QUEUES])
304976ff5cc9SJiri Pirko 		num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
305076ff5cc9SJiri Pirko 	else if (ops->get_num_tx_queues)
3051d40156aaSJiri Pirko 		num_tx_queues = ops->get_num_tx_queues();
305276ff5cc9SJiri Pirko 
305376ff5cc9SJiri Pirko 	if (tb[IFLA_NUM_RX_QUEUES])
305476ff5cc9SJiri Pirko 		num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
305576ff5cc9SJiri Pirko 	else if (ops->get_num_rx_queues)
3056d40156aaSJiri Pirko 		num_rx_queues = ops->get_num_rx_queues();
3057efacb309Sstephen hemminger 
3058d0522f1cSDavid Ahern 	if (num_tx_queues < 1 || num_tx_queues > 4096) {
3059d0522f1cSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid number of transmit queues");
30600e1d6ecaSEric Dumazet 		return ERR_PTR(-EINVAL);
3061d0522f1cSDavid Ahern 	}
30620e1d6ecaSEric Dumazet 
3063d0522f1cSDavid Ahern 	if (num_rx_queues < 1 || num_rx_queues > 4096) {
3064d0522f1cSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid number of receive queues");
30650e1d6ecaSEric Dumazet 		return ERR_PTR(-EINVAL);
3066d0522f1cSDavid Ahern 	}
30670e1d6ecaSEric Dumazet 
30685517750fSTom Gundersen 	dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
3069c835a677STom Gundersen 			       ops->setup, num_tx_queues, num_rx_queues);
3070e7199288SPavel Emelianov 	if (!dev)
3071d1892e4eSTobias Klauser 		return ERR_PTR(-ENOMEM);
3072e7199288SPavel Emelianov 
307381adee47SEric W. Biederman 	dev_net_set(dev, net);
307481adee47SEric W. Biederman 	dev->rtnl_link_ops = ops;
30753729d502SPatrick McHardy 	dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
307681adee47SEric W. Biederman 
3077d836f5c6SEric Dumazet 	if (tb[IFLA_MTU]) {
3078d836f5c6SEric Dumazet 		u32 mtu = nla_get_u32(tb[IFLA_MTU]);
3079d836f5c6SEric Dumazet 		int err;
3080d836f5c6SEric Dumazet 
3081d836f5c6SEric Dumazet 		err = dev_validate_mtu(dev, mtu, extack);
3082d836f5c6SEric Dumazet 		if (err) {
3083d836f5c6SEric Dumazet 			free_netdev(dev);
3084d836f5c6SEric Dumazet 			return ERR_PTR(err);
3085d836f5c6SEric Dumazet 		}
3086d836f5c6SEric Dumazet 		dev->mtu = mtu;
3087d836f5c6SEric Dumazet 	}
30882afb9b53SJiri Pirko 	if (tb[IFLA_ADDRESS]) {
3089e7199288SPavel Emelianov 		memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
3090e7199288SPavel Emelianov 				nla_len(tb[IFLA_ADDRESS]));
30912afb9b53SJiri Pirko 		dev->addr_assign_type = NET_ADDR_SET;
30922afb9b53SJiri Pirko 	}
3093e7199288SPavel Emelianov 	if (tb[IFLA_BROADCAST])
3094e7199288SPavel Emelianov 		memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
3095e7199288SPavel Emelianov 				nla_len(tb[IFLA_BROADCAST]));
3096e7199288SPavel Emelianov 	if (tb[IFLA_TXQLEN])
3097e7199288SPavel Emelianov 		dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
3098e7199288SPavel Emelianov 	if (tb[IFLA_OPERSTATE])
309993b2d4a2SDavid S. Miller 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
3100e7199288SPavel Emelianov 	if (tb[IFLA_LINKMODE])
3101e7199288SPavel Emelianov 		dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
3102ffa934f1SPatrick McHardy 	if (tb[IFLA_GROUP])
3103ffa934f1SPatrick McHardy 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
310446e6b992SStephen Hemminger 	if (tb[IFLA_GSO_MAX_SIZE])
310546e6b992SStephen Hemminger 		netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE]));
310646e6b992SStephen Hemminger 	if (tb[IFLA_GSO_MAX_SEGS])
3107a0b586faSStephen Hemminger 		dev->gso_max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
3108e7199288SPavel Emelianov 
3109e7199288SPavel Emelianov 	return dev;
3110e7199288SPavel Emelianov }
3111e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_create_link);
3112e7199288SPavel Emelianov 
311390f62cf3SEric W. Biederman static int rtnl_group_changelink(const struct sk_buff *skb,
311490f62cf3SEric W. Biederman 		struct net *net, int group,
3115e7ed828fSVlad Dogaru 		struct ifinfomsg *ifm,
3116ddf9f970SJakub Kicinski 		struct netlink_ext_ack *extack,
3117e7ed828fSVlad Dogaru 		struct nlattr **tb)
3118e7ed828fSVlad Dogaru {
3119d079535dSWANG Cong 	struct net_device *dev, *aux;
3120e7ed828fSVlad Dogaru 	int err;
3121e7ed828fSVlad Dogaru 
3122d079535dSWANG Cong 	for_each_netdev_safe(net, dev, aux) {
3123e7ed828fSVlad Dogaru 		if (dev->group == group) {
3124ddf9f970SJakub Kicinski 			err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0);
3125e7ed828fSVlad Dogaru 			if (err < 0)
3126e7ed828fSVlad Dogaru 				return err;
3127e7ed828fSVlad Dogaru 		}
3128e7ed828fSVlad Dogaru 	}
3129e7ed828fSVlad Dogaru 
3130e7ed828fSVlad Dogaru 	return 0;
3131e7ed828fSVlad Dogaru }
3132e7ed828fSVlad Dogaru 
3133a2939745SJakub Kicinski static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3134a2939745SJakub Kicinski 			  struct nlattr **attr, struct netlink_ext_ack *extack)
313538f7b870SPatrick McHardy {
3136420d0318SJakub Kicinski 	struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1];
3137420d0318SJakub Kicinski 	unsigned char name_assign_type = NET_NAME_USER;
3138420d0318SJakub Kicinski 	struct nlattr *linkinfo[IFLA_INFO_MAX + 1];
3139420d0318SJakub Kicinski 	const struct rtnl_link_ops *m_ops = NULL;
3140420d0318SJakub Kicinski 	struct net_device *master_dev = NULL;
31413b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
314238f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops;
314338f7b870SPatrick McHardy 	struct nlattr *tb[IFLA_MAX + 1];
3144420d0318SJakub Kicinski 	struct net *dest_net, *link_net;
3145420d0318SJakub Kicinski 	struct nlattr **slave_data;
3146420d0318SJakub Kicinski 	char kind[MODULE_NAME_LEN];
3147420d0318SJakub Kicinski 	struct net_device *dev;
3148420d0318SJakub Kicinski 	struct ifinfomsg *ifm;
3149420d0318SJakub Kicinski 	char ifname[IFNAMSIZ];
3150420d0318SJakub Kicinski 	struct nlattr **data;
315138f7b870SPatrick McHardy 	int err;
315238f7b870SPatrick McHardy 
315395a5afcaSJohannes Berg #ifdef CONFIG_MODULES
315438f7b870SPatrick McHardy replay:
31558072f085SThomas Graf #endif
31568cb08174SJohannes Berg 	err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
31578cb08174SJohannes Berg 				     ifla_policy, extack);
315838f7b870SPatrick McHardy 	if (err < 0)
315938f7b870SPatrick McHardy 		return err;
316038f7b870SPatrick McHardy 
31614ff66caeSChristian Brauner 	err = rtnl_ensure_unique_netns(tb, extack, false);
31624ff66caeSChristian Brauner 	if (err < 0)
31634ff66caeSChristian Brauner 		return err;
31644ff66caeSChristian Brauner 
316538f7b870SPatrick McHardy 	if (tb[IFLA_IFNAME])
316638f7b870SPatrick McHardy 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
316738f7b870SPatrick McHardy 	else
316838f7b870SPatrick McHardy 		ifname[0] = '\0';
316938f7b870SPatrick McHardy 
317038f7b870SPatrick McHardy 	ifm = nlmsg_data(nlh);
317138f7b870SPatrick McHardy 	if (ifm->ifi_index > 0)
3172881d966bSEric W. Biederman 		dev = __dev_get_by_index(net, ifm->ifi_index);
317376c9ac0eSJiri Pirko 	else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
317476c9ac0eSJiri Pirko 		dev = rtnl_dev_get(net, NULL, tb[IFLA_ALT_IFNAME], ifname);
317538f7b870SPatrick McHardy 	else
317638f7b870SPatrick McHardy 		dev = NULL;
317738f7b870SPatrick McHardy 
3178ba7d49b1SJiri Pirko 	if (dev) {
3179ba7d49b1SJiri Pirko 		master_dev = netdev_master_upper_dev_get(dev);
3180ba7d49b1SJiri Pirko 		if (master_dev)
3181ba7d49b1SJiri Pirko 			m_ops = master_dev->rtnl_link_ops;
3182ba7d49b1SJiri Pirko 	}
3183ba7d49b1SJiri Pirko 
3184e0d087afSEric Dumazet 	err = validate_linkmsg(dev, tb);
3185e0d087afSEric Dumazet 	if (err < 0)
31861840bb13SThomas Graf 		return err;
31871840bb13SThomas Graf 
318838f7b870SPatrick McHardy 	if (tb[IFLA_LINKINFO]) {
31898cb08174SJohannes Berg 		err = nla_parse_nested_deprecated(linkinfo, IFLA_INFO_MAX,
31908cb08174SJohannes Berg 						  tb[IFLA_LINKINFO],
31918cb08174SJohannes Berg 						  ifla_info_policy, NULL);
319238f7b870SPatrick McHardy 		if (err < 0)
319338f7b870SPatrick McHardy 			return err;
319438f7b870SPatrick McHardy 	} else
319538f7b870SPatrick McHardy 		memset(linkinfo, 0, sizeof(linkinfo));
319638f7b870SPatrick McHardy 
319738f7b870SPatrick McHardy 	if (linkinfo[IFLA_INFO_KIND]) {
319838f7b870SPatrick McHardy 		nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
319938f7b870SPatrick McHardy 		ops = rtnl_link_ops_get(kind);
320038f7b870SPatrick McHardy 	} else {
320138f7b870SPatrick McHardy 		kind[0] = '\0';
320238f7b870SPatrick McHardy 		ops = NULL;
320338f7b870SPatrick McHardy 	}
320438f7b870SPatrick McHardy 
3205420d0318SJakub Kicinski 	data = NULL;
320638f7b870SPatrick McHardy 	if (ops) {
3207ccf8dbcdSKees Cook 		if (ops->maxtype > RTNL_MAX_TYPE)
3208ccf8dbcdSKees Cook 			return -EINVAL;
3209ccf8dbcdSKees Cook 
321038f7b870SPatrick McHardy 		if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
32118cb08174SJohannes Berg 			err = nla_parse_nested_deprecated(attr, ops->maxtype,
321238f7b870SPatrick McHardy 							  linkinfo[IFLA_INFO_DATA],
321368d57f3bSDavid Ahern 							  ops->policy, extack);
321438f7b870SPatrick McHardy 			if (err < 0)
321538f7b870SPatrick McHardy 				return err;
321638f7b870SPatrick McHardy 			data = attr;
321738f7b870SPatrick McHardy 		}
321838f7b870SPatrick McHardy 		if (ops->validate) {
3219a8b8a889SMatthias Schiffer 			err = ops->validate(tb, data, extack);
322038f7b870SPatrick McHardy 			if (err < 0)
322138f7b870SPatrick McHardy 				return err;
322238f7b870SPatrick McHardy 		}
322338f7b870SPatrick McHardy 	}
322438f7b870SPatrick McHardy 
3225420d0318SJakub Kicinski 	slave_data = NULL;
3226ba7d49b1SJiri Pirko 	if (m_ops) {
3227af066ed3SPrashant Bhole 		if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE)
3228ccf8dbcdSKees Cook 			return -EINVAL;
3229ccf8dbcdSKees Cook 
3230ba7d49b1SJiri Pirko 		if (m_ops->slave_maxtype &&
3231ba7d49b1SJiri Pirko 		    linkinfo[IFLA_INFO_SLAVE_DATA]) {
32328cb08174SJohannes Berg 			err = nla_parse_nested_deprecated(slave_attr,
32338cb08174SJohannes Berg 							  m_ops->slave_maxtype,
3234ba7d49b1SJiri Pirko 							  linkinfo[IFLA_INFO_SLAVE_DATA],
32358cb08174SJohannes Berg 							  m_ops->slave_policy,
32368cb08174SJohannes Berg 							  extack);
3237ba7d49b1SJiri Pirko 			if (err < 0)
3238ba7d49b1SJiri Pirko 				return err;
3239ba7d49b1SJiri Pirko 			slave_data = slave_attr;
3240ba7d49b1SJiri Pirko 		}
3241ba7d49b1SJiri Pirko 	}
3242ba7d49b1SJiri Pirko 
324338f7b870SPatrick McHardy 	if (dev) {
324490c325e3SNicolas Dichtel 		int status = 0;
324538f7b870SPatrick McHardy 
324638f7b870SPatrick McHardy 		if (nlh->nlmsg_flags & NLM_F_EXCL)
324738f7b870SPatrick McHardy 			return -EEXIST;
324838f7b870SPatrick McHardy 		if (nlh->nlmsg_flags & NLM_F_REPLACE)
324938f7b870SPatrick McHardy 			return -EOPNOTSUPP;
325038f7b870SPatrick McHardy 
325138f7b870SPatrick McHardy 		if (linkinfo[IFLA_INFO_DATA]) {
325238f7b870SPatrick McHardy 			if (!ops || ops != dev->rtnl_link_ops ||
325338f7b870SPatrick McHardy 			    !ops->changelink)
325438f7b870SPatrick McHardy 				return -EOPNOTSUPP;
325538f7b870SPatrick McHardy 
3256ad744b22SMatthias Schiffer 			err = ops->changelink(dev, tb, data, extack);
325738f7b870SPatrick McHardy 			if (err < 0)
325838f7b870SPatrick McHardy 				return err;
3259ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
326038f7b870SPatrick McHardy 		}
326138f7b870SPatrick McHardy 
3262ba7d49b1SJiri Pirko 		if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
3263ba7d49b1SJiri Pirko 			if (!m_ops || !m_ops->slave_changelink)
3264ba7d49b1SJiri Pirko 				return -EOPNOTSUPP;
3265ba7d49b1SJiri Pirko 
3266420d0318SJakub Kicinski 			err = m_ops->slave_changelink(master_dev, dev, tb,
3267420d0318SJakub Kicinski 						      slave_data, extack);
3268ba7d49b1SJiri Pirko 			if (err < 0)
3269ba7d49b1SJiri Pirko 				return err;
3270ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
3271ba7d49b1SJiri Pirko 		}
3272ba7d49b1SJiri Pirko 
3273420d0318SJakub Kicinski 		return do_setlink(skb, dev, ifm, extack, tb, ifname, status);
327438f7b870SPatrick McHardy 	}
327538f7b870SPatrick McHardy 
3276ffa934f1SPatrick McHardy 	if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
3277ffa934f1SPatrick McHardy 		if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
327890f62cf3SEric W. Biederman 			return rtnl_group_changelink(skb, net,
3279ffa934f1SPatrick McHardy 						nla_get_u32(tb[IFLA_GROUP]),
3280ddf9f970SJakub Kicinski 						ifm, extack, tb);
328138f7b870SPatrick McHardy 		return -ENODEV;
3282ffa934f1SPatrick McHardy 	}
328338f7b870SPatrick McHardy 
3284160ca014STheuns Verwoerd 	if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
328538f7b870SPatrick McHardy 		return -EOPNOTSUPP;
328638f7b870SPatrick McHardy 
328738f7b870SPatrick McHardy 	if (!ops) {
328895a5afcaSJohannes Berg #ifdef CONFIG_MODULES
328938f7b870SPatrick McHardy 		if (kind[0]) {
329038f7b870SPatrick McHardy 			__rtnl_unlock();
329138f7b870SPatrick McHardy 			request_module("rtnl-link-%s", kind);
329238f7b870SPatrick McHardy 			rtnl_lock();
329338f7b870SPatrick McHardy 			ops = rtnl_link_ops_get(kind);
329438f7b870SPatrick McHardy 			if (ops)
329538f7b870SPatrick McHardy 				goto replay;
329638f7b870SPatrick McHardy 		}
329738f7b870SPatrick McHardy #endif
329868d57f3bSDavid Ahern 		NL_SET_ERR_MSG(extack, "Unknown device type");
329938f7b870SPatrick McHardy 		return -EOPNOTSUPP;
330038f7b870SPatrick McHardy 	}
330138f7b870SPatrick McHardy 
3302b0ab2fabSJiri Pirko 	if (!ops->setup)
3303b0ab2fabSJiri Pirko 		return -EOPNOTSUPP;
3304b0ab2fabSJiri Pirko 
33055517750fSTom Gundersen 	if (!ifname[0]) {
330638f7b870SPatrick McHardy 		snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
33075517750fSTom Gundersen 		name_assign_type = NET_NAME_ENUM;
33085517750fSTom Gundersen 	}
330938f7b870SPatrick McHardy 
33105bb8ed07SChristian Brauner 	dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN);
331113ad1774SEric W. Biederman 	if (IS_ERR(dest_net))
331213ad1774SEric W. Biederman 		return PTR_ERR(dest_net);
331313ad1774SEric W. Biederman 
3314317f4810SNicolas Dichtel 	if (tb[IFLA_LINK_NETNSID]) {
3315317f4810SNicolas Dichtel 		int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
3316317f4810SNicolas Dichtel 
3317317f4810SNicolas Dichtel 		link_net = get_net_ns_by_id(dest_net, id);
3318317f4810SNicolas Dichtel 		if (!link_net) {
331968d57f3bSDavid Ahern 			NL_SET_ERR_MSG(extack, "Unknown network namespace id");
3320317f4810SNicolas Dichtel 			err =  -EINVAL;
3321317f4810SNicolas Dichtel 			goto out;
3322317f4810SNicolas Dichtel 		}
332306615bedSEric W. Biederman 		err = -EPERM;
332406615bedSEric W. Biederman 		if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
332506615bedSEric W. Biederman 			goto out;
3326420d0318SJakub Kicinski 	} else {
3327420d0318SJakub Kicinski 		link_net = NULL;
3328317f4810SNicolas Dichtel 	}
3329317f4810SNicolas Dichtel 
3330317f4810SNicolas Dichtel 	dev = rtnl_create_link(link_net ? : dest_net, ifname,
3331d0522f1cSDavid Ahern 			       name_assign_type, ops, tb, extack);
33329c7dafbfSPavel Emelyanov 	if (IS_ERR(dev)) {
3333e7199288SPavel Emelianov 		err = PTR_ERR(dev);
33349c7dafbfSPavel Emelyanov 		goto out;
33359c7dafbfSPavel Emelyanov 	}
33369c7dafbfSPavel Emelyanov 
33379c7dafbfSPavel Emelyanov 	dev->ifindex = ifm->ifi_index;
33389c7dafbfSPavel Emelyanov 
33390e0eee24SCong Wang 	if (ops->newlink) {
3340420d0318SJakub Kicinski 		err = ops->newlink(link_net ? : net, dev, tb, data, extack);
33410e0eee24SCong Wang 		/* Drivers should call free_netdev() in ->destructor
3342e51fb152SCong Wang 		 * and unregister it on failure after registration
3343e51fb152SCong Wang 		 * so that device could be finally freed in rtnl_unlock.
33440e0eee24SCong Wang 		 */
3345e51fb152SCong Wang 		if (err < 0) {
3346e51fb152SCong Wang 			/* If device is not registered at all, free it now */
3347e51fb152SCong Wang 			if (dev->reg_state == NETREG_UNINITIALIZED)
3348e51fb152SCong Wang 				free_netdev(dev);
33490e0eee24SCong Wang 			goto out;
3350e51fb152SCong Wang 		}
33510e0eee24SCong Wang 	} else {
33522d85cba2SPatrick McHardy 		err = register_netdevice(dev);
3353fce9b9beSDan Carpenter 		if (err < 0) {
335438f7b870SPatrick McHardy 			free_netdev(dev);
33553729d502SPatrick McHardy 			goto out;
3356fce9b9beSDan Carpenter 		}
33570e0eee24SCong Wang 	}
33583729d502SPatrick McHardy 	err = rtnl_configure_link(dev, ifm);
335943638900SDavid S. Miller 	if (err < 0)
336043638900SDavid S. Miller 		goto out_unregister;
336143638900SDavid S. Miller 	if (link_net) {
336243638900SDavid S. Miller 		err = dev_change_net_namespace(dev, dest_net, ifname);
336343638900SDavid S. Miller 		if (err < 0)
336443638900SDavid S. Miller 			goto out_unregister;
336543638900SDavid S. Miller 	}
3366160ca014STheuns Verwoerd 	if (tb[IFLA_MASTER]) {
3367420d0318SJakub Kicinski 		err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
3368160ca014STheuns Verwoerd 		if (err)
3369160ca014STheuns Verwoerd 			goto out_unregister;
3370160ca014STheuns Verwoerd 	}
337143638900SDavid S. Miller out:
337243638900SDavid S. Miller 	if (link_net)
337343638900SDavid S. Miller 		put_net(link_net);
337443638900SDavid S. Miller 	put_net(dest_net);
337543638900SDavid S. Miller 	return err;
337643638900SDavid S. Miller out_unregister:
33777afb8886SWANG Cong 	if (ops->newlink) {
33787afb8886SWANG Cong 		LIST_HEAD(list_kill);
33797afb8886SWANG Cong 
33807afb8886SWANG Cong 		ops->dellink(dev, &list_kill);
33817afb8886SWANG Cong 		unregister_netdevice_many(&list_kill);
33827afb8886SWANG Cong 	} else {
33833729d502SPatrick McHardy 		unregister_netdevice(dev);
33847afb8886SWANG Cong 	}
3385317f4810SNicolas Dichtel 	goto out;
3386317f4810SNicolas Dichtel }
338738f7b870SPatrick McHardy 
3388a2939745SJakub Kicinski static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3389a2939745SJakub Kicinski 			struct netlink_ext_ack *extack)
3390a2939745SJakub Kicinski {
3391a2939745SJakub Kicinski 	struct nlattr **attr;
3392a2939745SJakub Kicinski 	int ret;
3393a2939745SJakub Kicinski 
3394a2939745SJakub Kicinski 	attr = kmalloc_array(RTNL_MAX_TYPE + 1, sizeof(*attr), GFP_KERNEL);
3395a2939745SJakub Kicinski 	if (!attr)
3396a2939745SJakub Kicinski 		return -ENOMEM;
3397a2939745SJakub Kicinski 
3398a2939745SJakub Kicinski 	ret = __rtnl_newlink(skb, nlh, attr, extack);
3399a2939745SJakub Kicinski 	kfree(attr);
3400a2939745SJakub Kicinski 	return ret;
3401a2939745SJakub Kicinski }
3402a2939745SJakub Kicinski 
34039b3757b0SJakub Kicinski static int rtnl_valid_getlink_req(struct sk_buff *skb,
34049b3757b0SJakub Kicinski 				  const struct nlmsghdr *nlh,
34059b3757b0SJakub Kicinski 				  struct nlattr **tb,
34069b3757b0SJakub Kicinski 				  struct netlink_ext_ack *extack)
34079b3757b0SJakub Kicinski {
34089b3757b0SJakub Kicinski 	struct ifinfomsg *ifm;
34099b3757b0SJakub Kicinski 	int i, err;
34109b3757b0SJakub Kicinski 
34119b3757b0SJakub Kicinski 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
34129b3757b0SJakub Kicinski 		NL_SET_ERR_MSG(extack, "Invalid header for get link");
34139b3757b0SJakub Kicinski 		return -EINVAL;
34149b3757b0SJakub Kicinski 	}
34159b3757b0SJakub Kicinski 
34169b3757b0SJakub Kicinski 	if (!netlink_strict_get_check(skb))
34178cb08174SJohannes Berg 		return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
34188cb08174SJohannes Berg 					      ifla_policy, extack);
34199b3757b0SJakub Kicinski 
34209b3757b0SJakub Kicinski 	ifm = nlmsg_data(nlh);
34219b3757b0SJakub Kicinski 	if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
34229b3757b0SJakub Kicinski 	    ifm->ifi_change) {
34239b3757b0SJakub Kicinski 		NL_SET_ERR_MSG(extack, "Invalid values in header for get link request");
34249b3757b0SJakub Kicinski 		return -EINVAL;
34259b3757b0SJakub Kicinski 	}
34269b3757b0SJakub Kicinski 
34278cb08174SJohannes Berg 	err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFLA_MAX,
34288cb08174SJohannes Berg 					    ifla_policy, extack);
34299b3757b0SJakub Kicinski 	if (err)
34309b3757b0SJakub Kicinski 		return err;
34319b3757b0SJakub Kicinski 
34329b3757b0SJakub Kicinski 	for (i = 0; i <= IFLA_MAX; i++) {
34339b3757b0SJakub Kicinski 		if (!tb[i])
34349b3757b0SJakub Kicinski 			continue;
34359b3757b0SJakub Kicinski 
34369b3757b0SJakub Kicinski 		switch (i) {
34379b3757b0SJakub Kicinski 		case IFLA_IFNAME:
343876c9ac0eSJiri Pirko 		case IFLA_ALT_IFNAME:
34399b3757b0SJakub Kicinski 		case IFLA_EXT_MASK:
34409b3757b0SJakub Kicinski 		case IFLA_TARGET_NETNSID:
34419b3757b0SJakub Kicinski 			break;
34429b3757b0SJakub Kicinski 		default:
34439b3757b0SJakub Kicinski 			NL_SET_ERR_MSG(extack, "Unsupported attribute in get link request");
34449b3757b0SJakub Kicinski 			return -EINVAL;
34459b3757b0SJakub Kicinski 		}
34469b3757b0SJakub Kicinski 	}
34479b3757b0SJakub Kicinski 
34489b3757b0SJakub Kicinski 	return 0;
34499b3757b0SJakub Kicinski }
34509b3757b0SJakub Kicinski 
3451c21ef3e3SDavid Ahern static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3452c21ef3e3SDavid Ahern 			struct netlink_ext_ack *extack)
3453711e2c33SJean Tourrilhes {
34543b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
345579e1ad14SJiri Benc 	struct net *tgt_net = net;
3456b60c5115SThomas Graf 	struct ifinfomsg *ifm;
3457b60c5115SThomas Graf 	struct nlattr *tb[IFLA_MAX+1];
3458b60c5115SThomas Graf 	struct net_device *dev = NULL;
3459b60c5115SThomas Graf 	struct sk_buff *nskb;
346079e1ad14SJiri Benc 	int netnsid = -1;
3461339bf98fSThomas Graf 	int err;
3462115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
3463711e2c33SJean Tourrilhes 
34649b3757b0SJakub Kicinski 	err = rtnl_valid_getlink_req(skb, nlh, tb, extack);
3465b60c5115SThomas Graf 	if (err < 0)
34669918f230SEric Sesterhenn 		return err;
3467b60c5115SThomas Graf 
34684ff66caeSChristian Brauner 	err = rtnl_ensure_unique_netns(tb, extack, true);
34694ff66caeSChristian Brauner 	if (err < 0)
34704ff66caeSChristian Brauner 		return err;
34714ff66caeSChristian Brauner 
34727e4a8d5aSChristian Brauner 	if (tb[IFLA_TARGET_NETNSID]) {
34737e4a8d5aSChristian Brauner 		netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
3474c383edc4SChristian Brauner 		tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
347579e1ad14SJiri Benc 		if (IS_ERR(tgt_net))
347679e1ad14SJiri Benc 			return PTR_ERR(tgt_net);
347779e1ad14SJiri Benc 	}
347879e1ad14SJiri Benc 
3479115c9b81SGreg Rose 	if (tb[IFLA_EXT_MASK])
3480115c9b81SGreg Rose 		ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3481115c9b81SGreg Rose 
348279e1ad14SJiri Benc 	err = -EINVAL;
3483b60c5115SThomas Graf 	ifm = nlmsg_data(nlh);
3484a3d12891SEric Dumazet 	if (ifm->ifi_index > 0)
348579e1ad14SJiri Benc 		dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
348676c9ac0eSJiri Pirko 	else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
348776c9ac0eSJiri Pirko 		dev = rtnl_dev_get(tgt_net, tb[IFLA_IFNAME],
348876c9ac0eSJiri Pirko 				   tb[IFLA_ALT_IFNAME], NULL);
3489a3d12891SEric Dumazet 	else
349079e1ad14SJiri Benc 		goto out;
3491b60c5115SThomas Graf 
349279e1ad14SJiri Benc 	err = -ENODEV;
3493a3d12891SEric Dumazet 	if (dev == NULL)
349479e1ad14SJiri Benc 		goto out;
3495a3d12891SEric Dumazet 
349679e1ad14SJiri Benc 	err = -ENOBUFS;
3497115c9b81SGreg Rose 	nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
3498a3d12891SEric Dumazet 	if (nskb == NULL)
349979e1ad14SJiri Benc 		goto out;
3500711e2c33SJean Tourrilhes 
350179e1ad14SJiri Benc 	err = rtnl_fill_ifinfo(nskb, dev, net,
350279e1ad14SJiri Benc 			       RTM_NEWLINK, NETLINK_CB(skb).portid,
350379e1ad14SJiri Benc 			       nlh->nlmsg_seq, 0, 0, ext_filter_mask,
3504d4e4fdf9SGuillaume Nault 			       0, NULL, 0, netnsid, GFP_KERNEL);
350526932566SPatrick McHardy 	if (err < 0) {
350626932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in if_nlmsg_size */
350726932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
350826932566SPatrick McHardy 		kfree_skb(nskb);
3509a3d12891SEric Dumazet 	} else
351015e47304SEric W. Biederman 		err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
351179e1ad14SJiri Benc out:
351279e1ad14SJiri Benc 	if (netnsid >= 0)
351379e1ad14SJiri Benc 		put_net(tgt_net);
3514b60c5115SThomas Graf 
3515711e2c33SJean Tourrilhes 	return err;
3516711e2c33SJean Tourrilhes }
3517711e2c33SJean Tourrilhes 
351836fbf1e5SJiri Pirko static int rtnl_alt_ifname(int cmd, struct net_device *dev, struct nlattr *attr,
351936fbf1e5SJiri Pirko 			   bool *changed, struct netlink_ext_ack *extack)
352036fbf1e5SJiri Pirko {
352136fbf1e5SJiri Pirko 	char *alt_ifname;
352236fbf1e5SJiri Pirko 	int err;
352336fbf1e5SJiri Pirko 
352436fbf1e5SJiri Pirko 	err = nla_validate(attr, attr->nla_len, IFLA_MAX, ifla_policy, extack);
352536fbf1e5SJiri Pirko 	if (err)
352636fbf1e5SJiri Pirko 		return err;
352736fbf1e5SJiri Pirko 
352844bfa9c5SEric Dumazet 	alt_ifname = nla_strdup(attr, GFP_KERNEL);
352936fbf1e5SJiri Pirko 	if (!alt_ifname)
353036fbf1e5SJiri Pirko 		return -ENOMEM;
353144bfa9c5SEric Dumazet 
353244bfa9c5SEric Dumazet 	if (cmd == RTM_NEWLINKPROP) {
353336fbf1e5SJiri Pirko 		err = netdev_name_node_alt_create(dev, alt_ifname);
353444bfa9c5SEric Dumazet 		if (!err)
353544bfa9c5SEric Dumazet 			alt_ifname = NULL;
353636fbf1e5SJiri Pirko 	} else if (cmd == RTM_DELLINKPROP) {
353736fbf1e5SJiri Pirko 		err = netdev_name_node_alt_destroy(dev, alt_ifname);
353836fbf1e5SJiri Pirko 	} else {
353944bfa9c5SEric Dumazet 		WARN_ON_ONCE(1);
354044bfa9c5SEric Dumazet 		err = -EINVAL;
354136fbf1e5SJiri Pirko 	}
354236fbf1e5SJiri Pirko 
354344bfa9c5SEric Dumazet 	kfree(alt_ifname);
354444bfa9c5SEric Dumazet 	if (!err)
354536fbf1e5SJiri Pirko 		*changed = true;
354644bfa9c5SEric Dumazet 	return err;
354736fbf1e5SJiri Pirko }
354836fbf1e5SJiri Pirko 
354936fbf1e5SJiri Pirko static int rtnl_linkprop(int cmd, struct sk_buff *skb, struct nlmsghdr *nlh,
355036fbf1e5SJiri Pirko 			 struct netlink_ext_ack *extack)
355136fbf1e5SJiri Pirko {
355236fbf1e5SJiri Pirko 	struct net *net = sock_net(skb->sk);
355336fbf1e5SJiri Pirko 	struct nlattr *tb[IFLA_MAX + 1];
355436fbf1e5SJiri Pirko 	struct net_device *dev;
355536fbf1e5SJiri Pirko 	struct ifinfomsg *ifm;
355636fbf1e5SJiri Pirko 	bool changed = false;
355736fbf1e5SJiri Pirko 	struct nlattr *attr;
355836fbf1e5SJiri Pirko 	int err, rem;
355936fbf1e5SJiri Pirko 
356036fbf1e5SJiri Pirko 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
356136fbf1e5SJiri Pirko 	if (err)
356236fbf1e5SJiri Pirko 		return err;
356336fbf1e5SJiri Pirko 
356436fbf1e5SJiri Pirko 	err = rtnl_ensure_unique_netns(tb, extack, true);
356536fbf1e5SJiri Pirko 	if (err)
356636fbf1e5SJiri Pirko 		return err;
356736fbf1e5SJiri Pirko 
356836fbf1e5SJiri Pirko 	ifm = nlmsg_data(nlh);
3569cc6090e9SJiri Pirko 	if (ifm->ifi_index > 0)
357036fbf1e5SJiri Pirko 		dev = __dev_get_by_index(net, ifm->ifi_index);
357176c9ac0eSJiri Pirko 	else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
357276c9ac0eSJiri Pirko 		dev = rtnl_dev_get(net, tb[IFLA_IFNAME],
357376c9ac0eSJiri Pirko 				   tb[IFLA_ALT_IFNAME], NULL);
3574cc6090e9SJiri Pirko 	else
357536fbf1e5SJiri Pirko 		return -EINVAL;
357636fbf1e5SJiri Pirko 
357736fbf1e5SJiri Pirko 	if (!dev)
357836fbf1e5SJiri Pirko 		return -ENODEV;
357936fbf1e5SJiri Pirko 
358036fbf1e5SJiri Pirko 	if (!tb[IFLA_PROP_LIST])
358136fbf1e5SJiri Pirko 		return 0;
358236fbf1e5SJiri Pirko 
358336fbf1e5SJiri Pirko 	nla_for_each_nested(attr, tb[IFLA_PROP_LIST], rem) {
358436fbf1e5SJiri Pirko 		switch (nla_type(attr)) {
358536fbf1e5SJiri Pirko 		case IFLA_ALT_IFNAME:
358636fbf1e5SJiri Pirko 			err = rtnl_alt_ifname(cmd, dev, attr, &changed, extack);
358736fbf1e5SJiri Pirko 			if (err)
358836fbf1e5SJiri Pirko 				return err;
358936fbf1e5SJiri Pirko 			break;
359036fbf1e5SJiri Pirko 		}
359136fbf1e5SJiri Pirko 	}
359236fbf1e5SJiri Pirko 
359336fbf1e5SJiri Pirko 	if (changed)
359436fbf1e5SJiri Pirko 		netdev_state_change(dev);
359536fbf1e5SJiri Pirko 	return 0;
359636fbf1e5SJiri Pirko }
359736fbf1e5SJiri Pirko 
359836fbf1e5SJiri Pirko static int rtnl_newlinkprop(struct sk_buff *skb, struct nlmsghdr *nlh,
359936fbf1e5SJiri Pirko 			    struct netlink_ext_ack *extack)
360036fbf1e5SJiri Pirko {
360136fbf1e5SJiri Pirko 	return rtnl_linkprop(RTM_NEWLINKPROP, skb, nlh, extack);
360236fbf1e5SJiri Pirko }
360336fbf1e5SJiri Pirko 
360436fbf1e5SJiri Pirko static int rtnl_dellinkprop(struct sk_buff *skb, struct nlmsghdr *nlh,
360536fbf1e5SJiri Pirko 			    struct netlink_ext_ack *extack)
360636fbf1e5SJiri Pirko {
360736fbf1e5SJiri Pirko 	return rtnl_linkprop(RTM_DELLINKPROP, skb, nlh, extack);
360836fbf1e5SJiri Pirko }
360936fbf1e5SJiri Pirko 
3610115c9b81SGreg Rose static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
3611c7ac8679SGreg Rose {
3612115c9b81SGreg Rose 	struct net *net = sock_net(skb->sk);
3613115c9b81SGreg Rose 	struct net_device *dev;
3614115c9b81SGreg Rose 	struct nlattr *tb[IFLA_MAX+1];
3615115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
3616115c9b81SGreg Rose 	u16 min_ifinfo_dump_size = 0;
3617e5eca6d4SMichal Schmidt 	int hdrlen;
3618115c9b81SGreg Rose 
3619e5eca6d4SMichal Schmidt 	/* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
3620e5eca6d4SMichal Schmidt 	hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
3621e5eca6d4SMichal Schmidt 		 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
3622e5eca6d4SMichal Schmidt 
36238cb08174SJohannes Berg 	if (nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
3624115c9b81SGreg Rose 		if (tb[IFLA_EXT_MASK])
3625115c9b81SGreg Rose 			ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3626a4b64fbeSEric Dumazet 	}
3627115c9b81SGreg Rose 
3628115c9b81SGreg Rose 	if (!ext_filter_mask)
3629115c9b81SGreg Rose 		return NLMSG_GOODSIZE;
3630115c9b81SGreg Rose 	/*
3631115c9b81SGreg Rose 	 * traverse the list of net devices and compute the minimum
3632115c9b81SGreg Rose 	 * buffer size based upon the filter mask.
3633115c9b81SGreg Rose 	 */
36346853dd48SFlorian Westphal 	rcu_read_lock();
36356853dd48SFlorian Westphal 	for_each_netdev_rcu(net, dev) {
3636115c9b81SGreg Rose 		min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
3637115c9b81SGreg Rose 					     if_nlmsg_size(dev,
3638115c9b81SGreg Rose 						           ext_filter_mask));
3639115c9b81SGreg Rose 	}
36406853dd48SFlorian Westphal 	rcu_read_unlock();
3641115c9b81SGreg Rose 
364293af2056SZhang Shengju 	return nlmsg_total_size(min_ifinfo_dump_size);
3643c7ac8679SGreg Rose }
3644c7ac8679SGreg Rose 
364542bad1daSAdrian Bunk static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
36461da177e4SLinus Torvalds {
36471da177e4SLinus Torvalds 	int idx;
36481da177e4SLinus Torvalds 	int s_idx = cb->family;
364987ccbb1fSChristian Brauner 	int type = cb->nlh->nlmsg_type - RTM_BASE;
3650c63586dcSDavid Ahern 	int ret = 0;
36511da177e4SLinus Torvalds 
36521da177e4SLinus Torvalds 	if (s_idx == 0)
36531da177e4SLinus Torvalds 		s_idx = 1;
36546853dd48SFlorian Westphal 
365525239ceeSPatrick McHardy 	for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
3656addf9b90SFlorian Westphal 		struct rtnl_link **tab;
3657addf9b90SFlorian Westphal 		struct rtnl_link *link;
36586853dd48SFlorian Westphal 		rtnl_dumpit_func dumpit;
36596853dd48SFlorian Westphal 
36601da177e4SLinus Torvalds 		if (idx < s_idx || idx == PF_PACKET)
36611da177e4SLinus Torvalds 			continue;
36626853dd48SFlorian Westphal 
3663addf9b90SFlorian Westphal 		if (type < 0 || type >= RTM_NR_MSGTYPES)
36641da177e4SLinus Torvalds 			continue;
36656853dd48SFlorian Westphal 
3666addf9b90SFlorian Westphal 		tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]);
3667addf9b90SFlorian Westphal 		if (!tab)
3668addf9b90SFlorian Westphal 			continue;
3669addf9b90SFlorian Westphal 
3670addf9b90SFlorian Westphal 		link = tab[type];
3671addf9b90SFlorian Westphal 		if (!link)
3672addf9b90SFlorian Westphal 			continue;
3673addf9b90SFlorian Westphal 
3674addf9b90SFlorian Westphal 		dumpit = link->dumpit;
36756853dd48SFlorian Westphal 		if (!dumpit)
36766853dd48SFlorian Westphal 			continue;
36776853dd48SFlorian Westphal 
36780465277fSNicolas Dichtel 		if (idx > s_idx) {
36791da177e4SLinus Torvalds 			memset(&cb->args[0], 0, sizeof(cb->args));
36800465277fSNicolas Dichtel 			cb->prev_seq = 0;
36810465277fSNicolas Dichtel 			cb->seq = 0;
36820465277fSNicolas Dichtel 		}
3683c63586dcSDavid Ahern 		ret = dumpit(skb, cb);
36845e1acb4aSAlexey Kodanev 		if (ret)
36851da177e4SLinus Torvalds 			break;
36861da177e4SLinus Torvalds 	}
36871da177e4SLinus Torvalds 	cb->family = idx;
36881da177e4SLinus Torvalds 
3689c63586dcSDavid Ahern 	return skb->len ? : ret;
36901da177e4SLinus Torvalds }
36911da177e4SLinus Torvalds 
3692395eea6cSMahesh Bandewar struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
36933d3ea5afSVlad Yasevich 				       unsigned int change,
369438e01b30SNicolas Dichtel 				       u32 event, gfp_t flags, int *new_nsid,
369538e01b30SNicolas Dichtel 				       int new_ifindex)
36961da177e4SLinus Torvalds {
3697c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
36981da177e4SLinus Torvalds 	struct sk_buff *skb;
36990ec6d3f4SThomas Graf 	int err = -ENOBUFS;
3700c7ac8679SGreg Rose 	size_t if_info_size;
37011da177e4SLinus Torvalds 
37027f294054SAlexei Starovoitov 	skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
37030ec6d3f4SThomas Graf 	if (skb == NULL)
37040ec6d3f4SThomas Graf 		goto errout;
37051da177e4SLinus Torvalds 
370679e1ad14SJiri Benc 	err = rtnl_fill_ifinfo(skb, dev, dev_net(dev),
370779e1ad14SJiri Benc 			       type, 0, 0, change, 0, 0, event,
3708d4e4fdf9SGuillaume Nault 			       new_nsid, new_ifindex, -1, flags);
370926932566SPatrick McHardy 	if (err < 0) {
371026932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in if_nlmsg_size() */
371126932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
371226932566SPatrick McHardy 		kfree_skb(skb);
371326932566SPatrick McHardy 		goto errout;
371426932566SPatrick McHardy 	}
3715395eea6cSMahesh Bandewar 	return skb;
37160ec6d3f4SThomas Graf errout:
37170ec6d3f4SThomas Graf 	if (err < 0)
37184b3da706SEric W. Biederman 		rtnl_set_sk_err(net, RTNLGRP_LINK, err);
3719395eea6cSMahesh Bandewar 	return NULL;
3720395eea6cSMahesh Bandewar }
3721395eea6cSMahesh Bandewar 
3722395eea6cSMahesh Bandewar void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
3723395eea6cSMahesh Bandewar {
3724395eea6cSMahesh Bandewar 	struct net *net = dev_net(dev);
3725395eea6cSMahesh Bandewar 
3726395eea6cSMahesh Bandewar 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
3727395eea6cSMahesh Bandewar }
3728395eea6cSMahesh Bandewar 
37293d3ea5afSVlad Yasevich static void rtmsg_ifinfo_event(int type, struct net_device *dev,
37303d3ea5afSVlad Yasevich 			       unsigned int change, u32 event,
373138e01b30SNicolas Dichtel 			       gfp_t flags, int *new_nsid, int new_ifindex)
3732395eea6cSMahesh Bandewar {
3733395eea6cSMahesh Bandewar 	struct sk_buff *skb;
3734395eea6cSMahesh Bandewar 
3735ed2a80abSNicolas Dichtel 	if (dev->reg_state != NETREG_REGISTERED)
3736ed2a80abSNicolas Dichtel 		return;
3737ed2a80abSNicolas Dichtel 
373838e01b30SNicolas Dichtel 	skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid,
373938e01b30SNicolas Dichtel 				     new_ifindex);
3740395eea6cSMahesh Bandewar 	if (skb)
3741395eea6cSMahesh Bandewar 		rtmsg_ifinfo_send(skb, dev, flags);
37421da177e4SLinus Torvalds }
37433d3ea5afSVlad Yasevich 
37443d3ea5afSVlad Yasevich void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
37453d3ea5afSVlad Yasevich 		  gfp_t flags)
37463d3ea5afSVlad Yasevich {
374738e01b30SNicolas Dichtel 	rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
374838e01b30SNicolas Dichtel 			   NULL, 0);
37493d3ea5afSVlad Yasevich }
37501da177e4SLinus Torvalds 
37516621dd29SNicolas Dichtel void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change,
375238e01b30SNicolas Dichtel 			 gfp_t flags, int *new_nsid, int new_ifindex)
37536621dd29SNicolas Dichtel {
37546621dd29SNicolas Dichtel 	rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
375538e01b30SNicolas Dichtel 			   new_nsid, new_ifindex);
37566621dd29SNicolas Dichtel }
37576621dd29SNicolas Dichtel 
3758d83b0603SJohn Fastabend static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
3759d83b0603SJohn Fastabend 				   struct net_device *dev,
37601e53d5bbSHubert Sokolowski 				   u8 *addr, u16 vid, u32 pid, u32 seq,
37611c104a6bSNicolas Dichtel 				   int type, unsigned int flags,
3762b3379041SHubert Sokolowski 				   int nlflags, u16 ndm_state)
3763d83b0603SJohn Fastabend {
3764d83b0603SJohn Fastabend 	struct nlmsghdr *nlh;
3765d83b0603SJohn Fastabend 	struct ndmsg *ndm;
3766d83b0603SJohn Fastabend 
37671c104a6bSNicolas Dichtel 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
3768d83b0603SJohn Fastabend 	if (!nlh)
3769d83b0603SJohn Fastabend 		return -EMSGSIZE;
3770d83b0603SJohn Fastabend 
3771d83b0603SJohn Fastabend 	ndm = nlmsg_data(nlh);
3772d83b0603SJohn Fastabend 	ndm->ndm_family  = AF_BRIDGE;
3773d83b0603SJohn Fastabend 	ndm->ndm_pad1	 = 0;
3774d83b0603SJohn Fastabend 	ndm->ndm_pad2    = 0;
3775d83b0603SJohn Fastabend 	ndm->ndm_flags	 = flags;
3776d83b0603SJohn Fastabend 	ndm->ndm_type	 = 0;
3777d83b0603SJohn Fastabend 	ndm->ndm_ifindex = dev->ifindex;
3778b3379041SHubert Sokolowski 	ndm->ndm_state   = ndm_state;
3779d83b0603SJohn Fastabend 
3780d83b0603SJohn Fastabend 	if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
3781d83b0603SJohn Fastabend 		goto nla_put_failure;
37821e53d5bbSHubert Sokolowski 	if (vid)
37831e53d5bbSHubert Sokolowski 		if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
37841e53d5bbSHubert Sokolowski 			goto nla_put_failure;
3785d83b0603SJohn Fastabend 
3786053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
3787053c095aSJohannes Berg 	return 0;
3788d83b0603SJohn Fastabend 
3789d83b0603SJohn Fastabend nla_put_failure:
3790d83b0603SJohn Fastabend 	nlmsg_cancel(skb, nlh);
3791d83b0603SJohn Fastabend 	return -EMSGSIZE;
3792d83b0603SJohn Fastabend }
3793d83b0603SJohn Fastabend 
37943ff661c3SJohn Fastabend static inline size_t rtnl_fdb_nlmsg_size(void)
37953ff661c3SJohn Fastabend {
3796f82ef3e1SSabrina Dubroca 	return NLMSG_ALIGN(sizeof(struct ndmsg)) +
3797f82ef3e1SSabrina Dubroca 	       nla_total_size(ETH_ALEN) +	/* NDA_LLADDR */
3798f82ef3e1SSabrina Dubroca 	       nla_total_size(sizeof(u16)) +	/* NDA_VLAN */
3799f82ef3e1SSabrina Dubroca 	       0;
38003ff661c3SJohn Fastabend }
38013ff661c3SJohn Fastabend 
3802b3379041SHubert Sokolowski static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
3803b3379041SHubert Sokolowski 			    u16 ndm_state)
38043ff661c3SJohn Fastabend {
38053ff661c3SJohn Fastabend 	struct net *net = dev_net(dev);
38063ff661c3SJohn Fastabend 	struct sk_buff *skb;
38073ff661c3SJohn Fastabend 	int err = -ENOBUFS;
38083ff661c3SJohn Fastabend 
38093ff661c3SJohn Fastabend 	skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
38103ff661c3SJohn Fastabend 	if (!skb)
38113ff661c3SJohn Fastabend 		goto errout;
38123ff661c3SJohn Fastabend 
38131e53d5bbSHubert Sokolowski 	err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
3814b3379041SHubert Sokolowski 				      0, 0, type, NTF_SELF, 0, ndm_state);
38153ff661c3SJohn Fastabend 	if (err < 0) {
38163ff661c3SJohn Fastabend 		kfree_skb(skb);
38173ff661c3SJohn Fastabend 		goto errout;
38183ff661c3SJohn Fastabend 	}
38193ff661c3SJohn Fastabend 
38203ff661c3SJohn Fastabend 	rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
38213ff661c3SJohn Fastabend 	return;
38223ff661c3SJohn Fastabend errout:
38233ff661c3SJohn Fastabend 	rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
38243ff661c3SJohn Fastabend }
38253ff661c3SJohn Fastabend 
3826a986967eSBart Van Assche /*
3827090096bfSVlad Yasevich  * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
3828090096bfSVlad Yasevich  */
3829090096bfSVlad Yasevich int ndo_dflt_fdb_add(struct ndmsg *ndm,
3830090096bfSVlad Yasevich 		     struct nlattr *tb[],
3831090096bfSVlad Yasevich 		     struct net_device *dev,
3832f6f6424bSJiri Pirko 		     const unsigned char *addr, u16 vid,
3833090096bfSVlad Yasevich 		     u16 flags)
3834090096bfSVlad Yasevich {
3835090096bfSVlad Yasevich 	int err = -EINVAL;
3836090096bfSVlad Yasevich 
3837090096bfSVlad Yasevich 	/* If aging addresses are supported device will need to
3838090096bfSVlad Yasevich 	 * implement its own handler for this.
3839090096bfSVlad Yasevich 	 */
3840090096bfSVlad Yasevich 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
3841090096bfSVlad Yasevich 		pr_info("%s: FDB only supports static addresses\n", dev->name);
3842090096bfSVlad Yasevich 		return err;
3843090096bfSVlad Yasevich 	}
3844090096bfSVlad Yasevich 
384565891feaSOr Gerlitz 	if (vid) {
384665891feaSOr Gerlitz 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
384765891feaSOr Gerlitz 		return err;
384865891feaSOr Gerlitz 	}
384965891feaSOr Gerlitz 
3850090096bfSVlad Yasevich 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3851090096bfSVlad Yasevich 		err = dev_uc_add_excl(dev, addr);
3852090096bfSVlad Yasevich 	else if (is_multicast_ether_addr(addr))
3853090096bfSVlad Yasevich 		err = dev_mc_add_excl(dev, addr);
3854090096bfSVlad Yasevich 
3855090096bfSVlad Yasevich 	/* Only return duplicate errors if NLM_F_EXCL is set */
3856090096bfSVlad Yasevich 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
3857090096bfSVlad Yasevich 		err = 0;
3858090096bfSVlad Yasevich 
3859090096bfSVlad Yasevich 	return err;
3860090096bfSVlad Yasevich }
3861090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_add);
3862090096bfSVlad Yasevich 
3863b88d12e4SFlorian Westphal static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid,
3864b88d12e4SFlorian Westphal 			 struct netlink_ext_ack *extack)
3865f6f6424bSJiri Pirko {
3866f6f6424bSJiri Pirko 	u16 vid = 0;
3867f6f6424bSJiri Pirko 
3868f6f6424bSJiri Pirko 	if (vlan_attr) {
3869f6f6424bSJiri Pirko 		if (nla_len(vlan_attr) != sizeof(u16)) {
3870b88d12e4SFlorian Westphal 			NL_SET_ERR_MSG(extack, "invalid vlan attribute size");
3871f6f6424bSJiri Pirko 			return -EINVAL;
3872f6f6424bSJiri Pirko 		}
3873f6f6424bSJiri Pirko 
3874f6f6424bSJiri Pirko 		vid = nla_get_u16(vlan_attr);
3875f6f6424bSJiri Pirko 
3876f6f6424bSJiri Pirko 		if (!vid || vid >= VLAN_VID_MASK) {
3877b88d12e4SFlorian Westphal 			NL_SET_ERR_MSG(extack, "invalid vlan id");
3878f6f6424bSJiri Pirko 			return -EINVAL;
3879f6f6424bSJiri Pirko 		}
3880f6f6424bSJiri Pirko 	}
3881f6f6424bSJiri Pirko 	*p_vid = vid;
3882f6f6424bSJiri Pirko 	return 0;
3883f6f6424bSJiri Pirko }
3884f6f6424bSJiri Pirko 
3885c21ef3e3SDavid Ahern static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
3886c21ef3e3SDavid Ahern 			struct netlink_ext_ack *extack)
388777162022SJohn Fastabend {
388877162022SJohn Fastabend 	struct net *net = sock_net(skb->sk);
388977162022SJohn Fastabend 	struct ndmsg *ndm;
389077162022SJohn Fastabend 	struct nlattr *tb[NDA_MAX+1];
389177162022SJohn Fastabend 	struct net_device *dev;
389277162022SJohn Fastabend 	u8 *addr;
3893f6f6424bSJiri Pirko 	u16 vid;
389477162022SJohn Fastabend 	int err;
389577162022SJohn Fastabend 
38968cb08174SJohannes Berg 	err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL,
38978cb08174SJohannes Berg 				     extack);
389877162022SJohn Fastabend 	if (err < 0)
389977162022SJohn Fastabend 		return err;
390077162022SJohn Fastabend 
390177162022SJohn Fastabend 	ndm = nlmsg_data(nlh);
390277162022SJohn Fastabend 	if (ndm->ndm_ifindex == 0) {
3903b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "invalid ifindex");
390477162022SJohn Fastabend 		return -EINVAL;
390577162022SJohn Fastabend 	}
390677162022SJohn Fastabend 
390777162022SJohn Fastabend 	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
390877162022SJohn Fastabend 	if (dev == NULL) {
3909b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "unknown ifindex");
391077162022SJohn Fastabend 		return -ENODEV;
391177162022SJohn Fastabend 	}
391277162022SJohn Fastabend 
391377162022SJohn Fastabend 	if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
3914b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "invalid address");
391577162022SJohn Fastabend 		return -EINVAL;
391677162022SJohn Fastabend 	}
391777162022SJohn Fastabend 
3918da715775SIdo Schimmel 	if (dev->type != ARPHRD_ETHER) {
3919da715775SIdo Schimmel 		NL_SET_ERR_MSG(extack, "FDB add only supported for Ethernet devices");
3920da715775SIdo Schimmel 		return -EINVAL;
3921da715775SIdo Schimmel 	}
3922da715775SIdo Schimmel 
392377162022SJohn Fastabend 	addr = nla_data(tb[NDA_LLADDR]);
392477162022SJohn Fastabend 
3925b88d12e4SFlorian Westphal 	err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
3926f6f6424bSJiri Pirko 	if (err)
3927f6f6424bSJiri Pirko 		return err;
3928f6f6424bSJiri Pirko 
392977162022SJohn Fastabend 	err = -EOPNOTSUPP;
393077162022SJohn Fastabend 
393177162022SJohn Fastabend 	/* Support fdb on master device the net/bridge default case */
393277162022SJohn Fastabend 	if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
39332e92a2d0SJulian Wiedmann 	    netif_is_bridge_port(dev)) {
3934898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3935898e5061SJiri Pirko 		const struct net_device_ops *ops = br_dev->netdev_ops;
3936898e5061SJiri Pirko 
3937f6f6424bSJiri Pirko 		err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
393887b0984eSPetr Machata 				       nlh->nlmsg_flags, extack);
393977162022SJohn Fastabend 		if (err)
394077162022SJohn Fastabend 			goto out;
394177162022SJohn Fastabend 		else
394277162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_MASTER;
394377162022SJohn Fastabend 	}
394477162022SJohn Fastabend 
394577162022SJohn Fastabend 	/* Embedded bridge, macvlan, and any other device support */
3946090096bfSVlad Yasevich 	if ((ndm->ndm_flags & NTF_SELF)) {
3947090096bfSVlad Yasevich 		if (dev->netdev_ops->ndo_fdb_add)
3948090096bfSVlad Yasevich 			err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
3949f6f6424bSJiri Pirko 							   vid,
395087b0984eSPetr Machata 							   nlh->nlmsg_flags,
395187b0984eSPetr Machata 							   extack);
3952090096bfSVlad Yasevich 		else
3953f6f6424bSJiri Pirko 			err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
395477162022SJohn Fastabend 					       nlh->nlmsg_flags);
395577162022SJohn Fastabend 
39563ff661c3SJohn Fastabend 		if (!err) {
3957b3379041SHubert Sokolowski 			rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
3958b3379041SHubert Sokolowski 					ndm->ndm_state);
395977162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_SELF;
396077162022SJohn Fastabend 		}
39613ff661c3SJohn Fastabend 	}
396277162022SJohn Fastabend out:
396377162022SJohn Fastabend 	return err;
396477162022SJohn Fastabend }
396577162022SJohn Fastabend 
3966a986967eSBart Van Assche /*
3967090096bfSVlad Yasevich  * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
3968090096bfSVlad Yasevich  */
3969090096bfSVlad Yasevich int ndo_dflt_fdb_del(struct ndmsg *ndm,
3970090096bfSVlad Yasevich 		     struct nlattr *tb[],
3971090096bfSVlad Yasevich 		     struct net_device *dev,
3972f6f6424bSJiri Pirko 		     const unsigned char *addr, u16 vid)
3973090096bfSVlad Yasevich {
3974c8a89c4aSAlexander Duyck 	int err = -EINVAL;
3975090096bfSVlad Yasevich 
3976090096bfSVlad Yasevich 	/* If aging addresses are supported device will need to
3977090096bfSVlad Yasevich 	 * implement its own handler for this.
3978090096bfSVlad Yasevich 	 */
397964535993SSridhar Samudrala 	if (!(ndm->ndm_state & NUD_PERMANENT)) {
3980090096bfSVlad Yasevich 		pr_info("%s: FDB only supports static addresses\n", dev->name);
3981c8a89c4aSAlexander Duyck 		return err;
3982090096bfSVlad Yasevich 	}
3983090096bfSVlad Yasevich 
3984090096bfSVlad Yasevich 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3985090096bfSVlad Yasevich 		err = dev_uc_del(dev, addr);
3986090096bfSVlad Yasevich 	else if (is_multicast_ether_addr(addr))
3987090096bfSVlad Yasevich 		err = dev_mc_del(dev, addr);
3988090096bfSVlad Yasevich 
3989090096bfSVlad Yasevich 	return err;
3990090096bfSVlad Yasevich }
3991090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_del);
3992090096bfSVlad Yasevich 
3993c21ef3e3SDavid Ahern static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
3994c21ef3e3SDavid Ahern 			struct netlink_ext_ack *extack)
399577162022SJohn Fastabend {
399677162022SJohn Fastabend 	struct net *net = sock_net(skb->sk);
399777162022SJohn Fastabend 	struct ndmsg *ndm;
39981690be63SVlad Yasevich 	struct nlattr *tb[NDA_MAX+1];
399977162022SJohn Fastabend 	struct net_device *dev;
400077162022SJohn Fastabend 	int err = -EINVAL;
400177162022SJohn Fastabend 	__u8 *addr;
4002f6f6424bSJiri Pirko 	u16 vid;
400377162022SJohn Fastabend 
400490f62cf3SEric W. Biederman 	if (!netlink_capable(skb, CAP_NET_ADMIN))
40051690be63SVlad Yasevich 		return -EPERM;
40061690be63SVlad Yasevich 
40078cb08174SJohannes Berg 	err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL,
40088cb08174SJohannes Berg 				     extack);
40091690be63SVlad Yasevich 	if (err < 0)
40101690be63SVlad Yasevich 		return err;
401177162022SJohn Fastabend 
401277162022SJohn Fastabend 	ndm = nlmsg_data(nlh);
401377162022SJohn Fastabend 	if (ndm->ndm_ifindex == 0) {
4014b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "invalid ifindex");
401577162022SJohn Fastabend 		return -EINVAL;
401677162022SJohn Fastabend 	}
401777162022SJohn Fastabend 
401877162022SJohn Fastabend 	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
401977162022SJohn Fastabend 	if (dev == NULL) {
4020b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "unknown ifindex");
402177162022SJohn Fastabend 		return -ENODEV;
402277162022SJohn Fastabend 	}
402377162022SJohn Fastabend 
40241690be63SVlad Yasevich 	if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
4025b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "invalid address");
402677162022SJohn Fastabend 		return -EINVAL;
402777162022SJohn Fastabend 	}
402877162022SJohn Fastabend 
4029da715775SIdo Schimmel 	if (dev->type != ARPHRD_ETHER) {
4030da715775SIdo Schimmel 		NL_SET_ERR_MSG(extack, "FDB delete only supported for Ethernet devices");
4031da715775SIdo Schimmel 		return -EINVAL;
4032da715775SIdo Schimmel 	}
4033da715775SIdo Schimmel 
40341690be63SVlad Yasevich 	addr = nla_data(tb[NDA_LLADDR]);
40351690be63SVlad Yasevich 
4036b88d12e4SFlorian Westphal 	err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
4037f6f6424bSJiri Pirko 	if (err)
4038f6f6424bSJiri Pirko 		return err;
4039f6f6424bSJiri Pirko 
404077162022SJohn Fastabend 	err = -EOPNOTSUPP;
404177162022SJohn Fastabend 
404277162022SJohn Fastabend 	/* Support fdb on master device the net/bridge default case */
404377162022SJohn Fastabend 	if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
40442e92a2d0SJulian Wiedmann 	    netif_is_bridge_port(dev)) {
4045898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4046898e5061SJiri Pirko 		const struct net_device_ops *ops = br_dev->netdev_ops;
404777162022SJohn Fastabend 
4048898e5061SJiri Pirko 		if (ops->ndo_fdb_del)
4049f6f6424bSJiri Pirko 			err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
405077162022SJohn Fastabend 
405177162022SJohn Fastabend 		if (err)
405277162022SJohn Fastabend 			goto out;
405377162022SJohn Fastabend 		else
405477162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_MASTER;
405577162022SJohn Fastabend 	}
405677162022SJohn Fastabend 
405777162022SJohn Fastabend 	/* Embedded bridge, macvlan, and any other device support */
4058090096bfSVlad Yasevich 	if (ndm->ndm_flags & NTF_SELF) {
4059090096bfSVlad Yasevich 		if (dev->netdev_ops->ndo_fdb_del)
4060f6f6424bSJiri Pirko 			err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
4061f6f6424bSJiri Pirko 							   vid);
4062090096bfSVlad Yasevich 		else
4063f6f6424bSJiri Pirko 			err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
406477162022SJohn Fastabend 
40653ff661c3SJohn Fastabend 		if (!err) {
4066b3379041SHubert Sokolowski 			rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
4067b3379041SHubert Sokolowski 					ndm->ndm_state);
406877162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_SELF;
406977162022SJohn Fastabend 		}
40703ff661c3SJohn Fastabend 	}
407177162022SJohn Fastabend out:
407277162022SJohn Fastabend 	return err;
407377162022SJohn Fastabend }
407477162022SJohn Fastabend 
4075d83b0603SJohn Fastabend static int nlmsg_populate_fdb(struct sk_buff *skb,
4076d83b0603SJohn Fastabend 			      struct netlink_callback *cb,
4077d83b0603SJohn Fastabend 			      struct net_device *dev,
4078d83b0603SJohn Fastabend 			      int *idx,
4079d83b0603SJohn Fastabend 			      struct netdev_hw_addr_list *list)
4080d83b0603SJohn Fastabend {
4081d83b0603SJohn Fastabend 	struct netdev_hw_addr *ha;
4082d83b0603SJohn Fastabend 	int err;
408315e47304SEric W. Biederman 	u32 portid, seq;
4084d83b0603SJohn Fastabend 
408515e47304SEric W. Biederman 	portid = NETLINK_CB(cb->skb).portid;
4086d83b0603SJohn Fastabend 	seq = cb->nlh->nlmsg_seq;
4087d83b0603SJohn Fastabend 
4088d83b0603SJohn Fastabend 	list_for_each_entry(ha, &list->list, list) {
4089d297653dSRoopa Prabhu 		if (*idx < cb->args[2])
4090d83b0603SJohn Fastabend 			goto skip;
4091d83b0603SJohn Fastabend 
40921e53d5bbSHubert Sokolowski 		err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
4093a7a558feSJohn Fastabend 					      portid, seq,
40941c104a6bSNicolas Dichtel 					      RTM_NEWNEIGH, NTF_SELF,
4095b3379041SHubert Sokolowski 					      NLM_F_MULTI, NUD_PERMANENT);
4096d83b0603SJohn Fastabend 		if (err < 0)
4097d83b0603SJohn Fastabend 			return err;
4098d83b0603SJohn Fastabend skip:
4099d83b0603SJohn Fastabend 		*idx += 1;
4100d83b0603SJohn Fastabend 	}
4101d83b0603SJohn Fastabend 	return 0;
4102d83b0603SJohn Fastabend }
4103d83b0603SJohn Fastabend 
4104d83b0603SJohn Fastabend /**
41052c53040fSBen Hutchings  * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
4106a986967eSBart Van Assche  * @skb: socket buffer to store message in
4107a986967eSBart Van Assche  * @cb: netlink callback
4108d83b0603SJohn Fastabend  * @dev: netdevice
4109a986967eSBart Van Assche  * @filter_dev: ignored
4110a986967eSBart Van Assche  * @idx: the number of FDB table entries dumped is added to *@idx
4111d83b0603SJohn Fastabend  *
4112d83b0603SJohn Fastabend  * Default netdevice operation to dump the existing unicast address list.
411391f3e7b1SJohn Fastabend  * Returns number of addresses from list put in skb.
4114d83b0603SJohn Fastabend  */
4115d83b0603SJohn Fastabend int ndo_dflt_fdb_dump(struct sk_buff *skb,
4116d83b0603SJohn Fastabend 		      struct netlink_callback *cb,
4117d83b0603SJohn Fastabend 		      struct net_device *dev,
41185d5eacb3SJamal Hadi Salim 		      struct net_device *filter_dev,
4119d297653dSRoopa Prabhu 		      int *idx)
4120d83b0603SJohn Fastabend {
4121d83b0603SJohn Fastabend 	int err;
4122d83b0603SJohn Fastabend 
412368883893SEric Dumazet 	if (dev->type != ARPHRD_ETHER)
412468883893SEric Dumazet 		return -EINVAL;
412568883893SEric Dumazet 
4126d83b0603SJohn Fastabend 	netif_addr_lock_bh(dev);
4127d297653dSRoopa Prabhu 	err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
4128d83b0603SJohn Fastabend 	if (err)
4129d83b0603SJohn Fastabend 		goto out;
41302934c9dbSZhang Shengju 	err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
4131d83b0603SJohn Fastabend out:
4132d83b0603SJohn Fastabend 	netif_addr_unlock_bh(dev);
4133d297653dSRoopa Prabhu 	return err;
4134d83b0603SJohn Fastabend }
4135d83b0603SJohn Fastabend EXPORT_SYMBOL(ndo_dflt_fdb_dump);
4136d83b0603SJohn Fastabend 
41378c6e137fSDavid Ahern static int valid_fdb_dump_strict(const struct nlmsghdr *nlh,
41388c6e137fSDavid Ahern 				 int *br_idx, int *brport_idx,
41398c6e137fSDavid Ahern 				 struct netlink_ext_ack *extack)
41408c6e137fSDavid Ahern {
41418c6e137fSDavid Ahern 	struct nlattr *tb[NDA_MAX + 1];
41428c6e137fSDavid Ahern 	struct ndmsg *ndm;
41438c6e137fSDavid Ahern 	int err, i;
41448c6e137fSDavid Ahern 
41458c6e137fSDavid Ahern 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
41468c6e137fSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid header for fdb dump request");
41478c6e137fSDavid Ahern 		return -EINVAL;
41488c6e137fSDavid Ahern 	}
41498c6e137fSDavid Ahern 
41508c6e137fSDavid Ahern 	ndm = nlmsg_data(nlh);
41518c6e137fSDavid Ahern 	if (ndm->ndm_pad1  || ndm->ndm_pad2  || ndm->ndm_state ||
41528c6e137fSDavid Ahern 	    ndm->ndm_flags || ndm->ndm_type) {
41538b73018fSNikolay Aleksandrov 		NL_SET_ERR_MSG(extack, "Invalid values in header for fdb dump request");
41548c6e137fSDavid Ahern 		return -EINVAL;
41558c6e137fSDavid Ahern 	}
41568c6e137fSDavid Ahern 
41578cb08174SJohannes Berg 	err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
41588cb08174SJohannes Berg 					    NDA_MAX, NULL, extack);
41598c6e137fSDavid Ahern 	if (err < 0)
41608c6e137fSDavid Ahern 		return err;
41618c6e137fSDavid Ahern 
41628c6e137fSDavid Ahern 	*brport_idx = ndm->ndm_ifindex;
41638c6e137fSDavid Ahern 	for (i = 0; i <= NDA_MAX; ++i) {
41648c6e137fSDavid Ahern 		if (!tb[i])
41658c6e137fSDavid Ahern 			continue;
41668c6e137fSDavid Ahern 
41678c6e137fSDavid Ahern 		switch (i) {
41688c6e137fSDavid Ahern 		case NDA_IFINDEX:
41698c6e137fSDavid Ahern 			if (nla_len(tb[i]) != sizeof(u32)) {
41708c6e137fSDavid Ahern 				NL_SET_ERR_MSG(extack, "Invalid IFINDEX attribute in fdb dump request");
41718c6e137fSDavid Ahern 				return -EINVAL;
41728c6e137fSDavid Ahern 			}
41738c6e137fSDavid Ahern 			*brport_idx = nla_get_u32(tb[NDA_IFINDEX]);
41748c6e137fSDavid Ahern 			break;
41758c6e137fSDavid Ahern 		case NDA_MASTER:
41768c6e137fSDavid Ahern 			if (nla_len(tb[i]) != sizeof(u32)) {
41778c6e137fSDavid Ahern 				NL_SET_ERR_MSG(extack, "Invalid MASTER attribute in fdb dump request");
41788c6e137fSDavid Ahern 				return -EINVAL;
41798c6e137fSDavid Ahern 			}
41808c6e137fSDavid Ahern 			*br_idx = nla_get_u32(tb[NDA_MASTER]);
41818c6e137fSDavid Ahern 			break;
41828c6e137fSDavid Ahern 		default:
41838c6e137fSDavid Ahern 			NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb dump request");
41848c6e137fSDavid Ahern 			return -EINVAL;
41858c6e137fSDavid Ahern 		}
41868c6e137fSDavid Ahern 	}
41878c6e137fSDavid Ahern 
41888c6e137fSDavid Ahern 	return 0;
41898c6e137fSDavid Ahern }
41908c6e137fSDavid Ahern 
41918dfbda19SDavid Ahern static int valid_fdb_dump_legacy(const struct nlmsghdr *nlh,
41928dfbda19SDavid Ahern 				 int *br_idx, int *brport_idx,
41938dfbda19SDavid Ahern 				 struct netlink_ext_ack *extack)
41948dfbda19SDavid Ahern {
41958dfbda19SDavid Ahern 	struct nlattr *tb[IFLA_MAX+1];
41968dfbda19SDavid Ahern 	int err;
41978dfbda19SDavid Ahern 
41988dfbda19SDavid Ahern 	/* A hack to preserve kernel<->userspace interface.
41998dfbda19SDavid Ahern 	 * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0.
42008dfbda19SDavid Ahern 	 * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails.
42018dfbda19SDavid Ahern 	 * So, check for ndmsg with an optional u32 attribute (not used here).
42028dfbda19SDavid Ahern 	 * Fortunately these sizes don't conflict with the size of ifinfomsg
42038dfbda19SDavid Ahern 	 * with an optional attribute.
42048dfbda19SDavid Ahern 	 */
42058dfbda19SDavid Ahern 	if (nlmsg_len(nlh) != sizeof(struct ndmsg) &&
42068dfbda19SDavid Ahern 	    (nlmsg_len(nlh) != sizeof(struct ndmsg) +
42078dfbda19SDavid Ahern 	     nla_attr_size(sizeof(u32)))) {
42084565d7e5SDavid Ahern 		struct ifinfomsg *ifm;
42094565d7e5SDavid Ahern 
42108cb08174SJohannes Berg 		err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
42118cb08174SJohannes Berg 					     tb, IFLA_MAX, ifla_policy,
42128cb08174SJohannes Berg 					     extack);
42138dfbda19SDavid Ahern 		if (err < 0) {
42148dfbda19SDavid Ahern 			return -EINVAL;
42158dfbda19SDavid Ahern 		} else if (err == 0) {
42168dfbda19SDavid Ahern 			if (tb[IFLA_MASTER])
42178dfbda19SDavid Ahern 				*br_idx = nla_get_u32(tb[IFLA_MASTER]);
42188dfbda19SDavid Ahern 		}
42198dfbda19SDavid Ahern 
42204565d7e5SDavid Ahern 		ifm = nlmsg_data(nlh);
42218dfbda19SDavid Ahern 		*brport_idx = ifm->ifi_index;
42228dfbda19SDavid Ahern 	}
42238dfbda19SDavid Ahern 	return 0;
42248dfbda19SDavid Ahern }
42258dfbda19SDavid Ahern 
422677162022SJohn Fastabend static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
422777162022SJohn Fastabend {
422877162022SJohn Fastabend 	struct net_device *dev;
42295e6d2435SJamal Hadi Salim 	struct net_device *br_dev = NULL;
42305e6d2435SJamal Hadi Salim 	const struct net_device_ops *ops = NULL;
42315e6d2435SJamal Hadi Salim 	const struct net_device_ops *cops = NULL;
42325e6d2435SJamal Hadi Salim 	struct net *net = sock_net(skb->sk);
4233d297653dSRoopa Prabhu 	struct hlist_head *head;
42345e6d2435SJamal Hadi Salim 	int brport_idx = 0;
42355e6d2435SJamal Hadi Salim 	int br_idx = 0;
4236d297653dSRoopa Prabhu 	int h, s_h;
4237d297653dSRoopa Prabhu 	int idx = 0, s_idx;
4238d297653dSRoopa Prabhu 	int err = 0;
4239d297653dSRoopa Prabhu 	int fidx = 0;
424077162022SJohn Fastabend 
42418c6e137fSDavid Ahern 	if (cb->strict_check)
42428c6e137fSDavid Ahern 		err = valid_fdb_dump_strict(cb->nlh, &br_idx, &brport_idx,
42438c6e137fSDavid Ahern 					    cb->extack);
42448c6e137fSDavid Ahern 	else
42458dfbda19SDavid Ahern 		err = valid_fdb_dump_legacy(cb->nlh, &br_idx, &brport_idx,
42468dfbda19SDavid Ahern 					    cb->extack);
42478dfbda19SDavid Ahern 	if (err < 0)
42488dfbda19SDavid Ahern 		return err;
42495e6d2435SJamal Hadi Salim 
42505e6d2435SJamal Hadi Salim 	if (br_idx) {
42515e6d2435SJamal Hadi Salim 		br_dev = __dev_get_by_index(net, br_idx);
42525e6d2435SJamal Hadi Salim 		if (!br_dev)
42535e6d2435SJamal Hadi Salim 			return -ENODEV;
42545e6d2435SJamal Hadi Salim 
4255898e5061SJiri Pirko 		ops = br_dev->netdev_ops;
42565e6d2435SJamal Hadi Salim 	}
42575e6d2435SJamal Hadi Salim 
4258d297653dSRoopa Prabhu 	s_h = cb->args[0];
4259d297653dSRoopa Prabhu 	s_idx = cb->args[1];
4260d297653dSRoopa Prabhu 
4261d297653dSRoopa Prabhu 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4262d297653dSRoopa Prabhu 		idx = 0;
4263d297653dSRoopa Prabhu 		head = &net->dev_index_head[h];
4264d297653dSRoopa Prabhu 		hlist_for_each_entry(dev, head, index_hlist) {
4265d297653dSRoopa Prabhu 
42665e6d2435SJamal Hadi Salim 			if (brport_idx && (dev->ifindex != brport_idx))
42675e6d2435SJamal Hadi Salim 				continue;
42685e6d2435SJamal Hadi Salim 
42695e6d2435SJamal Hadi Salim 			if (!br_idx) { /* user did not specify a specific bridge */
42702e92a2d0SJulian Wiedmann 				if (netif_is_bridge_port(dev)) {
42715e6d2435SJamal Hadi Salim 					br_dev = netdev_master_upper_dev_get(dev);
42725e6d2435SJamal Hadi Salim 					cops = br_dev->netdev_ops;
42735e6d2435SJamal Hadi Salim 				}
42745e6d2435SJamal Hadi Salim 			} else {
42755e6d2435SJamal Hadi Salim 				if (dev != br_dev &&
42762e92a2d0SJulian Wiedmann 				    !netif_is_bridge_port(dev))
42775e6d2435SJamal Hadi Salim 					continue;
42785e6d2435SJamal Hadi Salim 
42795e6d2435SJamal Hadi Salim 				if (br_dev != netdev_master_upper_dev_get(dev) &&
42805e6d2435SJamal Hadi Salim 				    !(dev->priv_flags & IFF_EBRIDGE))
42815e6d2435SJamal Hadi Salim 					continue;
42825e6d2435SJamal Hadi Salim 				cops = ops;
42835e6d2435SJamal Hadi Salim 			}
42845e6d2435SJamal Hadi Salim 
4285d297653dSRoopa Prabhu 			if (idx < s_idx)
4286d297653dSRoopa Prabhu 				goto cont;
4287d297653dSRoopa Prabhu 
42882e92a2d0SJulian Wiedmann 			if (netif_is_bridge_port(dev)) {
4289d297653dSRoopa Prabhu 				if (cops && cops->ndo_fdb_dump) {
4290d297653dSRoopa Prabhu 					err = cops->ndo_fdb_dump(skb, cb,
4291d297653dSRoopa Prabhu 								br_dev, dev,
4292d297653dSRoopa Prabhu 								&fidx);
4293d297653dSRoopa Prabhu 					if (err == -EMSGSIZE)
4294d297653dSRoopa Prabhu 						goto out;
429577162022SJohn Fastabend 				}
4296d297653dSRoopa Prabhu 			}
429777162022SJohn Fastabend 
42985e6d2435SJamal Hadi Salim 			if (dev->netdev_ops->ndo_fdb_dump)
4299d297653dSRoopa Prabhu 				err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
4300d297653dSRoopa Prabhu 								    dev, NULL,
4301d297653dSRoopa Prabhu 								    &fidx);
43026cb69742SHubert Sokolowski 			else
4303d297653dSRoopa Prabhu 				err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
4304d297653dSRoopa Prabhu 							&fidx);
4305d297653dSRoopa Prabhu 			if (err == -EMSGSIZE)
4306d297653dSRoopa Prabhu 				goto out;
43075e6d2435SJamal Hadi Salim 
43085e6d2435SJamal Hadi Salim 			cops = NULL;
4309d297653dSRoopa Prabhu 
4310d297653dSRoopa Prabhu 			/* reset fdb offset to 0 for rest of the interfaces */
4311d297653dSRoopa Prabhu 			cb->args[2] = 0;
4312d297653dSRoopa Prabhu 			fidx = 0;
4313d297653dSRoopa Prabhu cont:
4314d297653dSRoopa Prabhu 			idx++;
4315d297653dSRoopa Prabhu 		}
431677162022SJohn Fastabend 	}
431777162022SJohn Fastabend 
4318d297653dSRoopa Prabhu out:
4319d297653dSRoopa Prabhu 	cb->args[0] = h;
4320d297653dSRoopa Prabhu 	cb->args[1] = idx;
4321d297653dSRoopa Prabhu 	cb->args[2] = fidx;
4322d297653dSRoopa Prabhu 
432377162022SJohn Fastabend 	return skb->len;
432477162022SJohn Fastabend }
432577162022SJohn Fastabend 
43265b2f94b2SRoopa Prabhu static int valid_fdb_get_strict(const struct nlmsghdr *nlh,
43275b2f94b2SRoopa Prabhu 				struct nlattr **tb, u8 *ndm_flags,
43285b2f94b2SRoopa Prabhu 				int *br_idx, int *brport_idx, u8 **addr,
43295b2f94b2SRoopa Prabhu 				u16 *vid, struct netlink_ext_ack *extack)
43305b2f94b2SRoopa Prabhu {
43315b2f94b2SRoopa Prabhu 	struct ndmsg *ndm;
43325b2f94b2SRoopa Prabhu 	int err, i;
43335b2f94b2SRoopa Prabhu 
43345b2f94b2SRoopa Prabhu 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
43355b2f94b2SRoopa Prabhu 		NL_SET_ERR_MSG(extack, "Invalid header for fdb get request");
43365b2f94b2SRoopa Prabhu 		return -EINVAL;
43375b2f94b2SRoopa Prabhu 	}
43385b2f94b2SRoopa Prabhu 
43395b2f94b2SRoopa Prabhu 	ndm = nlmsg_data(nlh);
43405b2f94b2SRoopa Prabhu 	if (ndm->ndm_pad1  || ndm->ndm_pad2  || ndm->ndm_state ||
43415b2f94b2SRoopa Prabhu 	    ndm->ndm_type) {
43425b2f94b2SRoopa Prabhu 		NL_SET_ERR_MSG(extack, "Invalid values in header for fdb get request");
43435b2f94b2SRoopa Prabhu 		return -EINVAL;
43445b2f94b2SRoopa Prabhu 	}
43455b2f94b2SRoopa Prabhu 
43465b2f94b2SRoopa Prabhu 	if (ndm->ndm_flags & ~(NTF_MASTER | NTF_SELF)) {
43475b2f94b2SRoopa Prabhu 		NL_SET_ERR_MSG(extack, "Invalid flags in header for fdb get request");
43485b2f94b2SRoopa Prabhu 		return -EINVAL;
43495b2f94b2SRoopa Prabhu 	}
43505b2f94b2SRoopa Prabhu 
43518cb08174SJohannes Berg 	err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
43528cb08174SJohannes Berg 					    NDA_MAX, nda_policy, extack);
43535b2f94b2SRoopa Prabhu 	if (err < 0)
43545b2f94b2SRoopa Prabhu 		return err;
43555b2f94b2SRoopa Prabhu 
43565b2f94b2SRoopa Prabhu 	*ndm_flags = ndm->ndm_flags;
43575b2f94b2SRoopa Prabhu 	*brport_idx = ndm->ndm_ifindex;
43585b2f94b2SRoopa Prabhu 	for (i = 0; i <= NDA_MAX; ++i) {
43595b2f94b2SRoopa Prabhu 		if (!tb[i])
43605b2f94b2SRoopa Prabhu 			continue;
43615b2f94b2SRoopa Prabhu 
43625b2f94b2SRoopa Prabhu 		switch (i) {
43635b2f94b2SRoopa Prabhu 		case NDA_MASTER:
43645b2f94b2SRoopa Prabhu 			*br_idx = nla_get_u32(tb[i]);
43655b2f94b2SRoopa Prabhu 			break;
43665b2f94b2SRoopa Prabhu 		case NDA_LLADDR:
43675b2f94b2SRoopa Prabhu 			if (nla_len(tb[i]) != ETH_ALEN) {
43685b2f94b2SRoopa Prabhu 				NL_SET_ERR_MSG(extack, "Invalid address in fdb get request");
43695b2f94b2SRoopa Prabhu 				return -EINVAL;
43705b2f94b2SRoopa Prabhu 			}
43715b2f94b2SRoopa Prabhu 			*addr = nla_data(tb[i]);
43725b2f94b2SRoopa Prabhu 			break;
43735b2f94b2SRoopa Prabhu 		case NDA_VLAN:
43745b2f94b2SRoopa Prabhu 			err = fdb_vid_parse(tb[i], vid, extack);
43755b2f94b2SRoopa Prabhu 			if (err)
43765b2f94b2SRoopa Prabhu 				return err;
43775b2f94b2SRoopa Prabhu 			break;
43785b2f94b2SRoopa Prabhu 		case NDA_VNI:
43795b2f94b2SRoopa Prabhu 			break;
43805b2f94b2SRoopa Prabhu 		default:
43815b2f94b2SRoopa Prabhu 			NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb get request");
43825b2f94b2SRoopa Prabhu 			return -EINVAL;
43835b2f94b2SRoopa Prabhu 		}
43845b2f94b2SRoopa Prabhu 	}
43855b2f94b2SRoopa Prabhu 
43865b2f94b2SRoopa Prabhu 	return 0;
43875b2f94b2SRoopa Prabhu }
43885b2f94b2SRoopa Prabhu 
43895b2f94b2SRoopa Prabhu static int rtnl_fdb_get(struct sk_buff *in_skb, struct nlmsghdr *nlh,
43905b2f94b2SRoopa Prabhu 			struct netlink_ext_ack *extack)
43915b2f94b2SRoopa Prabhu {
43925b2f94b2SRoopa Prabhu 	struct net_device *dev = NULL, *br_dev = NULL;
43935b2f94b2SRoopa Prabhu 	const struct net_device_ops *ops = NULL;
43945b2f94b2SRoopa Prabhu 	struct net *net = sock_net(in_skb->sk);
43955b2f94b2SRoopa Prabhu 	struct nlattr *tb[NDA_MAX + 1];
43965b2f94b2SRoopa Prabhu 	struct sk_buff *skb;
43975b2f94b2SRoopa Prabhu 	int brport_idx = 0;
43985b2f94b2SRoopa Prabhu 	u8 ndm_flags = 0;
43995b2f94b2SRoopa Prabhu 	int br_idx = 0;
44005b2f94b2SRoopa Prabhu 	u8 *addr = NULL;
44015b2f94b2SRoopa Prabhu 	u16 vid = 0;
44025b2f94b2SRoopa Prabhu 	int err;
44035b2f94b2SRoopa Prabhu 
44045b2f94b2SRoopa Prabhu 	err = valid_fdb_get_strict(nlh, tb, &ndm_flags, &br_idx,
44055b2f94b2SRoopa Prabhu 				   &brport_idx, &addr, &vid, extack);
44065b2f94b2SRoopa Prabhu 	if (err < 0)
44075b2f94b2SRoopa Prabhu 		return err;
44085b2f94b2SRoopa Prabhu 
4409f989d03eSNikolay Aleksandrov 	if (!addr) {
4410f989d03eSNikolay Aleksandrov 		NL_SET_ERR_MSG(extack, "Missing lookup address for fdb get request");
4411f989d03eSNikolay Aleksandrov 		return -EINVAL;
4412f989d03eSNikolay Aleksandrov 	}
4413f989d03eSNikolay Aleksandrov 
44145b2f94b2SRoopa Prabhu 	if (brport_idx) {
44155b2f94b2SRoopa Prabhu 		dev = __dev_get_by_index(net, brport_idx);
44165b2f94b2SRoopa Prabhu 		if (!dev) {
44175b2f94b2SRoopa Prabhu 			NL_SET_ERR_MSG(extack, "Unknown device ifindex");
44185b2f94b2SRoopa Prabhu 			return -ENODEV;
44195b2f94b2SRoopa Prabhu 		}
44205b2f94b2SRoopa Prabhu 	}
44215b2f94b2SRoopa Prabhu 
44225b2f94b2SRoopa Prabhu 	if (br_idx) {
44235b2f94b2SRoopa Prabhu 		if (dev) {
44245b2f94b2SRoopa Prabhu 			NL_SET_ERR_MSG(extack, "Master and device are mutually exclusive");
44255b2f94b2SRoopa Prabhu 			return -EINVAL;
44265b2f94b2SRoopa Prabhu 		}
44275b2f94b2SRoopa Prabhu 
44285b2f94b2SRoopa Prabhu 		br_dev = __dev_get_by_index(net, br_idx);
44295b2f94b2SRoopa Prabhu 		if (!br_dev) {
44305b2f94b2SRoopa Prabhu 			NL_SET_ERR_MSG(extack, "Invalid master ifindex");
44315b2f94b2SRoopa Prabhu 			return -EINVAL;
44325b2f94b2SRoopa Prabhu 		}
44335b2f94b2SRoopa Prabhu 		ops = br_dev->netdev_ops;
44345b2f94b2SRoopa Prabhu 	}
44355b2f94b2SRoopa Prabhu 
44365b2f94b2SRoopa Prabhu 	if (dev) {
44375b2f94b2SRoopa Prabhu 		if (!ndm_flags || (ndm_flags & NTF_MASTER)) {
44382e92a2d0SJulian Wiedmann 			if (!netif_is_bridge_port(dev)) {
44395b2f94b2SRoopa Prabhu 				NL_SET_ERR_MSG(extack, "Device is not a bridge port");
44405b2f94b2SRoopa Prabhu 				return -EINVAL;
44415b2f94b2SRoopa Prabhu 			}
44425b2f94b2SRoopa Prabhu 			br_dev = netdev_master_upper_dev_get(dev);
44435b2f94b2SRoopa Prabhu 			if (!br_dev) {
44445b2f94b2SRoopa Prabhu 				NL_SET_ERR_MSG(extack, "Master of device not found");
44455b2f94b2SRoopa Prabhu 				return -EINVAL;
44465b2f94b2SRoopa Prabhu 			}
44475b2f94b2SRoopa Prabhu 			ops = br_dev->netdev_ops;
44485b2f94b2SRoopa Prabhu 		} else {
44495b2f94b2SRoopa Prabhu 			if (!(ndm_flags & NTF_SELF)) {
44505b2f94b2SRoopa Prabhu 				NL_SET_ERR_MSG(extack, "Missing NTF_SELF");
44515b2f94b2SRoopa Prabhu 				return -EINVAL;
44525b2f94b2SRoopa Prabhu 			}
44535b2f94b2SRoopa Prabhu 			ops = dev->netdev_ops;
44545b2f94b2SRoopa Prabhu 		}
44555b2f94b2SRoopa Prabhu 	}
44565b2f94b2SRoopa Prabhu 
44575b2f94b2SRoopa Prabhu 	if (!br_dev && !dev) {
44585b2f94b2SRoopa Prabhu 		NL_SET_ERR_MSG(extack, "No device specified");
44595b2f94b2SRoopa Prabhu 		return -ENODEV;
44605b2f94b2SRoopa Prabhu 	}
44615b2f94b2SRoopa Prabhu 
44625b2f94b2SRoopa Prabhu 	if (!ops || !ops->ndo_fdb_get) {
44635b2f94b2SRoopa Prabhu 		NL_SET_ERR_MSG(extack, "Fdb get operation not supported by device");
44645b2f94b2SRoopa Prabhu 		return -EOPNOTSUPP;
44655b2f94b2SRoopa Prabhu 	}
44665b2f94b2SRoopa Prabhu 
44675b2f94b2SRoopa Prabhu 	skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
44685b2f94b2SRoopa Prabhu 	if (!skb)
44695b2f94b2SRoopa Prabhu 		return -ENOBUFS;
44705b2f94b2SRoopa Prabhu 
44715b2f94b2SRoopa Prabhu 	if (br_dev)
44725b2f94b2SRoopa Prabhu 		dev = br_dev;
44735b2f94b2SRoopa Prabhu 	err = ops->ndo_fdb_get(skb, tb, dev, addr, vid,
44745b2f94b2SRoopa Prabhu 			       NETLINK_CB(in_skb).portid,
44755b2f94b2SRoopa Prabhu 			       nlh->nlmsg_seq, extack);
44765b2f94b2SRoopa Prabhu 	if (err)
44775b2f94b2SRoopa Prabhu 		goto out;
44785b2f94b2SRoopa Prabhu 
44795b2f94b2SRoopa Prabhu 	return rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
44805b2f94b2SRoopa Prabhu out:
44815b2f94b2SRoopa Prabhu 	kfree_skb(skb);
44825b2f94b2SRoopa Prabhu 	return err;
44835b2f94b2SRoopa Prabhu }
44845b2f94b2SRoopa Prabhu 
44852c3c031cSScott Feldman static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
44862c3c031cSScott Feldman 			       unsigned int attrnum, unsigned int flag)
44872c3c031cSScott Feldman {
44882c3c031cSScott Feldman 	if (mask & flag)
44892c3c031cSScott Feldman 		return nla_put_u8(skb, attrnum, !!(flags & flag));
44902c3c031cSScott Feldman 	return 0;
44912c3c031cSScott Feldman }
44922c3c031cSScott Feldman 
4493815cccbfSJohn Fastabend int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
44942c3c031cSScott Feldman 			    struct net_device *dev, u16 mode,
44957d4f8d87SScott Feldman 			    u32 flags, u32 mask, int nlflags,
44967d4f8d87SScott Feldman 			    u32 filter_mask,
44977d4f8d87SScott Feldman 			    int (*vlan_fill)(struct sk_buff *skb,
44987d4f8d87SScott Feldman 					     struct net_device *dev,
44997d4f8d87SScott Feldman 					     u32 filter_mask))
4500815cccbfSJohn Fastabend {
4501815cccbfSJohn Fastabend 	struct nlmsghdr *nlh;
4502815cccbfSJohn Fastabend 	struct ifinfomsg *ifm;
4503815cccbfSJohn Fastabend 	struct nlattr *br_afspec;
45042c3c031cSScott Feldman 	struct nlattr *protinfo;
4505815cccbfSJohn Fastabend 	u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
4506898e5061SJiri Pirko 	struct net_device *br_dev = netdev_master_upper_dev_get(dev);
45077d4f8d87SScott Feldman 	int err = 0;
4508815cccbfSJohn Fastabend 
450946c264daSNicolas Dichtel 	nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
4510815cccbfSJohn Fastabend 	if (nlh == NULL)
4511815cccbfSJohn Fastabend 		return -EMSGSIZE;
4512815cccbfSJohn Fastabend 
4513815cccbfSJohn Fastabend 	ifm = nlmsg_data(nlh);
4514815cccbfSJohn Fastabend 	ifm->ifi_family = AF_BRIDGE;
4515815cccbfSJohn Fastabend 	ifm->__ifi_pad = 0;
4516815cccbfSJohn Fastabend 	ifm->ifi_type = dev->type;
4517815cccbfSJohn Fastabend 	ifm->ifi_index = dev->ifindex;
4518815cccbfSJohn Fastabend 	ifm->ifi_flags = dev_get_flags(dev);
4519815cccbfSJohn Fastabend 	ifm->ifi_change = 0;
4520815cccbfSJohn Fastabend 
4521815cccbfSJohn Fastabend 
4522815cccbfSJohn Fastabend 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
4523815cccbfSJohn Fastabend 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
4524815cccbfSJohn Fastabend 	    nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
4525898e5061SJiri Pirko 	    (br_dev &&
4526898e5061SJiri Pirko 	     nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
4527815cccbfSJohn Fastabend 	    (dev->addr_len &&
4528815cccbfSJohn Fastabend 	     nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
4529a54acb3aSNicolas Dichtel 	    (dev->ifindex != dev_get_iflink(dev) &&
4530a54acb3aSNicolas Dichtel 	     nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
4531815cccbfSJohn Fastabend 		goto nla_put_failure;
4532815cccbfSJohn Fastabend 
4533ae0be8deSMichal Kubecek 	br_afspec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
4534815cccbfSJohn Fastabend 	if (!br_afspec)
4535815cccbfSJohn Fastabend 		goto nla_put_failure;
4536815cccbfSJohn Fastabend 
45371d460b98SRoopa Prabhu 	if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
4538815cccbfSJohn Fastabend 		nla_nest_cancel(skb, br_afspec);
4539815cccbfSJohn Fastabend 		goto nla_put_failure;
4540815cccbfSJohn Fastabend 	}
45411d460b98SRoopa Prabhu 
45421d460b98SRoopa Prabhu 	if (mode != BRIDGE_MODE_UNDEF) {
45431d460b98SRoopa Prabhu 		if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
45441d460b98SRoopa Prabhu 			nla_nest_cancel(skb, br_afspec);
45451d460b98SRoopa Prabhu 			goto nla_put_failure;
45461d460b98SRoopa Prabhu 		}
45471d460b98SRoopa Prabhu 	}
45487d4f8d87SScott Feldman 	if (vlan_fill) {
45497d4f8d87SScott Feldman 		err = vlan_fill(skb, dev, filter_mask);
45507d4f8d87SScott Feldman 		if (err) {
45517d4f8d87SScott Feldman 			nla_nest_cancel(skb, br_afspec);
45527d4f8d87SScott Feldman 			goto nla_put_failure;
45537d4f8d87SScott Feldman 		}
45547d4f8d87SScott Feldman 	}
4555815cccbfSJohn Fastabend 	nla_nest_end(skb, br_afspec);
4556815cccbfSJohn Fastabend 
4557ae0be8deSMichal Kubecek 	protinfo = nla_nest_start(skb, IFLA_PROTINFO);
45582c3c031cSScott Feldman 	if (!protinfo)
45592c3c031cSScott Feldman 		goto nla_put_failure;
45602c3c031cSScott Feldman 
45612c3c031cSScott Feldman 	if (brport_nla_put_flag(skb, flags, mask,
45622c3c031cSScott Feldman 				IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
45632c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
45642c3c031cSScott Feldman 				IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
45652c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
45662c3c031cSScott Feldman 				IFLA_BRPORT_FAST_LEAVE,
45672c3c031cSScott Feldman 				BR_MULTICAST_FAST_LEAVE) ||
45682c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
45692c3c031cSScott Feldman 				IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
45702c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
45712c3c031cSScott Feldman 				IFLA_BRPORT_LEARNING, BR_LEARNING) ||
45722c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
45732c3c031cSScott Feldman 				IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
45742c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
45752c3c031cSScott Feldman 				IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
45762c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
4577583cb0b4SJulian Wiedmann 				IFLA_BRPORT_PROXYARP, BR_PROXYARP) ||
4578583cb0b4SJulian Wiedmann 	    brport_nla_put_flag(skb, flags, mask,
4579583cb0b4SJulian Wiedmann 				IFLA_BRPORT_MCAST_FLOOD, BR_MCAST_FLOOD) ||
4580583cb0b4SJulian Wiedmann 	    brport_nla_put_flag(skb, flags, mask,
4581583cb0b4SJulian Wiedmann 				IFLA_BRPORT_BCAST_FLOOD, BR_BCAST_FLOOD)) {
45822c3c031cSScott Feldman 		nla_nest_cancel(skb, protinfo);
45832c3c031cSScott Feldman 		goto nla_put_failure;
45842c3c031cSScott Feldman 	}
45852c3c031cSScott Feldman 
45862c3c031cSScott Feldman 	nla_nest_end(skb, protinfo);
45872c3c031cSScott Feldman 
4588053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
4589053c095aSJohannes Berg 	return 0;
4590815cccbfSJohn Fastabend nla_put_failure:
4591815cccbfSJohn Fastabend 	nlmsg_cancel(skb, nlh);
45927d4f8d87SScott Feldman 	return err ? err : -EMSGSIZE;
4593815cccbfSJohn Fastabend }
45947d4f8d87SScott Feldman EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
4595815cccbfSJohn Fastabend 
45962d011be8SDavid Ahern static int valid_bridge_getlink_req(const struct nlmsghdr *nlh,
45972d011be8SDavid Ahern 				    bool strict_check, u32 *filter_mask,
45982d011be8SDavid Ahern 				    struct netlink_ext_ack *extack)
45992d011be8SDavid Ahern {
46002d011be8SDavid Ahern 	struct nlattr *tb[IFLA_MAX+1];
46012d011be8SDavid Ahern 	int err, i;
46022d011be8SDavid Ahern 
46032d011be8SDavid Ahern 	if (strict_check) {
46042d011be8SDavid Ahern 		struct ifinfomsg *ifm;
46052d011be8SDavid Ahern 
46062d011be8SDavid Ahern 		if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
46072d011be8SDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid header for bridge link dump");
46082d011be8SDavid Ahern 			return -EINVAL;
46092d011be8SDavid Ahern 		}
46102d011be8SDavid Ahern 
46112d011be8SDavid Ahern 		ifm = nlmsg_data(nlh);
46122d011be8SDavid Ahern 		if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
46132d011be8SDavid Ahern 		    ifm->ifi_change || ifm->ifi_index) {
46142d011be8SDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid values in header for bridge link dump request");
46152d011be8SDavid Ahern 			return -EINVAL;
46162d011be8SDavid Ahern 		}
46172d011be8SDavid Ahern 
46188cb08174SJohannes Berg 		err = nlmsg_parse_deprecated_strict(nlh,
46198cb08174SJohannes Berg 						    sizeof(struct ifinfomsg),
46208cb08174SJohannes Berg 						    tb, IFLA_MAX, ifla_policy,
46218cb08174SJohannes Berg 						    extack);
46222d011be8SDavid Ahern 	} else {
46238cb08174SJohannes Berg 		err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
46248cb08174SJohannes Berg 					     tb, IFLA_MAX, ifla_policy,
46258cb08174SJohannes Berg 					     extack);
46262d011be8SDavid Ahern 	}
46272d011be8SDavid Ahern 	if (err < 0)
46282d011be8SDavid Ahern 		return err;
46292d011be8SDavid Ahern 
46302d011be8SDavid Ahern 	/* new attributes should only be added with strict checking */
46312d011be8SDavid Ahern 	for (i = 0; i <= IFLA_MAX; ++i) {
46322d011be8SDavid Ahern 		if (!tb[i])
46332d011be8SDavid Ahern 			continue;
46342d011be8SDavid Ahern 
46352d011be8SDavid Ahern 		switch (i) {
46362d011be8SDavid Ahern 		case IFLA_EXT_MASK:
46372d011be8SDavid Ahern 			*filter_mask = nla_get_u32(tb[i]);
46382d011be8SDavid Ahern 			break;
46392d011be8SDavid Ahern 		default:
46402d011be8SDavid Ahern 			if (strict_check) {
46412d011be8SDavid Ahern 				NL_SET_ERR_MSG(extack, "Unsupported attribute in bridge link dump request");
46422d011be8SDavid Ahern 				return -EINVAL;
46432d011be8SDavid Ahern 			}
46442d011be8SDavid Ahern 		}
46452d011be8SDavid Ahern 	}
46462d011be8SDavid Ahern 
46472d011be8SDavid Ahern 	return 0;
46482d011be8SDavid Ahern }
46492d011be8SDavid Ahern 
4650e5a55a89SJohn Fastabend static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
4651e5a55a89SJohn Fastabend {
46522d011be8SDavid Ahern 	const struct nlmsghdr *nlh = cb->nlh;
4653e5a55a89SJohn Fastabend 	struct net *net = sock_net(skb->sk);
4654e5a55a89SJohn Fastabend 	struct net_device *dev;
4655e5a55a89SJohn Fastabend 	int idx = 0;
4656e5a55a89SJohn Fastabend 	u32 portid = NETLINK_CB(cb->skb).portid;
46572d011be8SDavid Ahern 	u32 seq = nlh->nlmsg_seq;
46586cbdceebSVlad Yasevich 	u32 filter_mask = 0;
4659d64f69b0SRoopa Prabhu 	int err;
46606cbdceebSVlad Yasevich 
46612d011be8SDavid Ahern 	err = valid_bridge_getlink_req(nlh, cb->strict_check, &filter_mask,
46622d011be8SDavid Ahern 				       cb->extack);
46632d011be8SDavid Ahern 	if (err < 0 && cb->strict_check)
46642d011be8SDavid Ahern 		return err;
4665e5a55a89SJohn Fastabend 
4666e5a55a89SJohn Fastabend 	rcu_read_lock();
4667e5a55a89SJohn Fastabend 	for_each_netdev_rcu(net, dev) {
4668e5a55a89SJohn Fastabend 		const struct net_device_ops *ops = dev->netdev_ops;
4669898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4670e5a55a89SJohn Fastabend 
4671898e5061SJiri Pirko 		if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
4672d64f69b0SRoopa Prabhu 			if (idx >= cb->args[0]) {
4673d64f69b0SRoopa Prabhu 				err = br_dev->netdev_ops->ndo_bridge_getlink(
4674d64f69b0SRoopa Prabhu 						skb, portid, seq, dev,
4675d64f69b0SRoopa Prabhu 						filter_mask, NLM_F_MULTI);
4676f6c5775fSDavid Ahern 				if (err < 0 && err != -EOPNOTSUPP) {
4677f6c5775fSDavid Ahern 					if (likely(skb->len))
4678e5a55a89SJohn Fastabend 						break;
4679f6c5775fSDavid Ahern 
4680f6c5775fSDavid Ahern 					goto out_err;
4681f6c5775fSDavid Ahern 				}
4682d64f69b0SRoopa Prabhu 			}
4683e5a55a89SJohn Fastabend 			idx++;
4684e5a55a89SJohn Fastabend 		}
4685e5a55a89SJohn Fastabend 
4686e5a55a89SJohn Fastabend 		if (ops->ndo_bridge_getlink) {
4687d64f69b0SRoopa Prabhu 			if (idx >= cb->args[0]) {
4688d64f69b0SRoopa Prabhu 				err = ops->ndo_bridge_getlink(skb, portid,
4689d64f69b0SRoopa Prabhu 							      seq, dev,
469046c264daSNicolas Dichtel 							      filter_mask,
4691d64f69b0SRoopa Prabhu 							      NLM_F_MULTI);
4692f6c5775fSDavid Ahern 				if (err < 0 && err != -EOPNOTSUPP) {
4693f6c5775fSDavid Ahern 					if (likely(skb->len))
4694e5a55a89SJohn Fastabend 						break;
4695f6c5775fSDavid Ahern 
4696f6c5775fSDavid Ahern 					goto out_err;
4697f6c5775fSDavid Ahern 				}
4698d64f69b0SRoopa Prabhu 			}
4699e5a55a89SJohn Fastabend 			idx++;
4700e5a55a89SJohn Fastabend 		}
4701e5a55a89SJohn Fastabend 	}
4702f6c5775fSDavid Ahern 	err = skb->len;
4703f6c5775fSDavid Ahern out_err:
4704e5a55a89SJohn Fastabend 	rcu_read_unlock();
4705e5a55a89SJohn Fastabend 	cb->args[0] = idx;
4706e5a55a89SJohn Fastabend 
4707f6c5775fSDavid Ahern 	return err;
4708e5a55a89SJohn Fastabend }
4709e5a55a89SJohn Fastabend 
47102469ffd7SJohn Fastabend static inline size_t bridge_nlmsg_size(void)
47112469ffd7SJohn Fastabend {
47122469ffd7SJohn Fastabend 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
47132469ffd7SJohn Fastabend 		+ nla_total_size(IFNAMSIZ)	/* IFLA_IFNAME */
47142469ffd7SJohn Fastabend 		+ nla_total_size(MAX_ADDR_LEN)	/* IFLA_ADDRESS */
47152469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_MASTER */
47162469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_MTU */
47172469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_LINK */
47182469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_OPERSTATE */
47192469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u8))	/* IFLA_PROTINFO */
47202469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(struct nlattr))	/* IFLA_AF_SPEC */
47212469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u16))	/* IFLA_BRIDGE_FLAGS */
47222469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u16));	/* IFLA_BRIDGE_MODE */
47232469ffd7SJohn Fastabend }
47242469ffd7SJohn Fastabend 
472502dba438SRoopa Prabhu static int rtnl_bridge_notify(struct net_device *dev)
47262469ffd7SJohn Fastabend {
47272469ffd7SJohn Fastabend 	struct net *net = dev_net(dev);
47282469ffd7SJohn Fastabend 	struct sk_buff *skb;
47292469ffd7SJohn Fastabend 	int err = -EOPNOTSUPP;
47302469ffd7SJohn Fastabend 
473102dba438SRoopa Prabhu 	if (!dev->netdev_ops->ndo_bridge_getlink)
473202dba438SRoopa Prabhu 		return 0;
473302dba438SRoopa Prabhu 
47342469ffd7SJohn Fastabend 	skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
47352469ffd7SJohn Fastabend 	if (!skb) {
47362469ffd7SJohn Fastabend 		err = -ENOMEM;
47372469ffd7SJohn Fastabend 		goto errout;
47382469ffd7SJohn Fastabend 	}
47392469ffd7SJohn Fastabend 
474046c264daSNicolas Dichtel 	err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
4741c38e01b8SJohn Fastabend 	if (err < 0)
4742c38e01b8SJohn Fastabend 		goto errout;
47432469ffd7SJohn Fastabend 
474459ccaaaaSRoopa Prabhu 	if (!skb->len)
474559ccaaaaSRoopa Prabhu 		goto errout;
474659ccaaaaSRoopa Prabhu 
47472469ffd7SJohn Fastabend 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
47482469ffd7SJohn Fastabend 	return 0;
47492469ffd7SJohn Fastabend errout:
47502469ffd7SJohn Fastabend 	WARN_ON(err == -EMSGSIZE);
47512469ffd7SJohn Fastabend 	kfree_skb(skb);
475259ccaaaaSRoopa Prabhu 	if (err)
47532469ffd7SJohn Fastabend 		rtnl_set_sk_err(net, RTNLGRP_LINK, err);
47542469ffd7SJohn Fastabend 	return err;
47552469ffd7SJohn Fastabend }
47562469ffd7SJohn Fastabend 
4757c21ef3e3SDavid Ahern static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
4758c21ef3e3SDavid Ahern 			       struct netlink_ext_ack *extack)
4759e5a55a89SJohn Fastabend {
4760e5a55a89SJohn Fastabend 	struct net *net = sock_net(skb->sk);
4761e5a55a89SJohn Fastabend 	struct ifinfomsg *ifm;
4762e5a55a89SJohn Fastabend 	struct net_device *dev;
47632469ffd7SJohn Fastabend 	struct nlattr *br_spec, *attr = NULL;
47642469ffd7SJohn Fastabend 	int rem, err = -EOPNOTSUPP;
47654de8b413SRosen, Rami 	u16 flags = 0;
4766c38e01b8SJohn Fastabend 	bool have_flags = false;
4767e5a55a89SJohn Fastabend 
4768e5a55a89SJohn Fastabend 	if (nlmsg_len(nlh) < sizeof(*ifm))
4769e5a55a89SJohn Fastabend 		return -EINVAL;
4770e5a55a89SJohn Fastabend 
4771e5a55a89SJohn Fastabend 	ifm = nlmsg_data(nlh);
4772e5a55a89SJohn Fastabend 	if (ifm->ifi_family != AF_BRIDGE)
4773e5a55a89SJohn Fastabend 		return -EPFNOSUPPORT;
4774e5a55a89SJohn Fastabend 
4775e5a55a89SJohn Fastabend 	dev = __dev_get_by_index(net, ifm->ifi_index);
4776e5a55a89SJohn Fastabend 	if (!dev) {
4777b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "unknown ifindex");
4778e5a55a89SJohn Fastabend 		return -ENODEV;
4779e5a55a89SJohn Fastabend 	}
4780e5a55a89SJohn Fastabend 
47812469ffd7SJohn Fastabend 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
47822469ffd7SJohn Fastabend 	if (br_spec) {
47832469ffd7SJohn Fastabend 		nla_for_each_nested(attr, br_spec, rem) {
47842469ffd7SJohn Fastabend 			if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
47856e8d1c55SThomas Graf 				if (nla_len(attr) < sizeof(flags))
47866e8d1c55SThomas Graf 					return -EINVAL;
47876e8d1c55SThomas Graf 
4788c38e01b8SJohn Fastabend 				have_flags = true;
47892469ffd7SJohn Fastabend 				flags = nla_get_u16(attr);
47902469ffd7SJohn Fastabend 				break;
47912469ffd7SJohn Fastabend 			}
47922469ffd7SJohn Fastabend 		}
47932469ffd7SJohn Fastabend 	}
47942469ffd7SJohn Fastabend 
47952469ffd7SJohn Fastabend 	if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
4796898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4797898e5061SJiri Pirko 
4798898e5061SJiri Pirko 		if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
47992469ffd7SJohn Fastabend 			err = -EOPNOTSUPP;
4800e5a55a89SJohn Fastabend 			goto out;
4801e5a55a89SJohn Fastabend 		}
4802e5a55a89SJohn Fastabend 
48032fd527b7SPetr Machata 		err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags,
48042fd527b7SPetr Machata 							     extack);
48052469ffd7SJohn Fastabend 		if (err)
48062469ffd7SJohn Fastabend 			goto out;
48072469ffd7SJohn Fastabend 
48082469ffd7SJohn Fastabend 		flags &= ~BRIDGE_FLAGS_MASTER;
48092469ffd7SJohn Fastabend 	}
48102469ffd7SJohn Fastabend 
48112469ffd7SJohn Fastabend 	if ((flags & BRIDGE_FLAGS_SELF)) {
48122469ffd7SJohn Fastabend 		if (!dev->netdev_ops->ndo_bridge_setlink)
48132469ffd7SJohn Fastabend 			err = -EOPNOTSUPP;
48142469ffd7SJohn Fastabend 		else
4815add511b3SRoopa Prabhu 			err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
48162fd527b7SPetr Machata 								  flags,
48172fd527b7SPetr Machata 								  extack);
481802dba438SRoopa Prabhu 		if (!err) {
48192469ffd7SJohn Fastabend 			flags &= ~BRIDGE_FLAGS_SELF;
482002dba438SRoopa Prabhu 
482102dba438SRoopa Prabhu 			/* Generate event to notify upper layer of bridge
482202dba438SRoopa Prabhu 			 * change
482302dba438SRoopa Prabhu 			 */
482402dba438SRoopa Prabhu 			err = rtnl_bridge_notify(dev);
482502dba438SRoopa Prabhu 		}
48262469ffd7SJohn Fastabend 	}
48272469ffd7SJohn Fastabend 
4828c38e01b8SJohn Fastabend 	if (have_flags)
48292469ffd7SJohn Fastabend 		memcpy(nla_data(attr), &flags, sizeof(flags));
4830e5a55a89SJohn Fastabend out:
4831e5a55a89SJohn Fastabend 	return err;
4832e5a55a89SJohn Fastabend }
4833e5a55a89SJohn Fastabend 
4834c21ef3e3SDavid Ahern static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
4835c21ef3e3SDavid Ahern 			       struct netlink_ext_ack *extack)
4836407af329SVlad Yasevich {
4837407af329SVlad Yasevich 	struct net *net = sock_net(skb->sk);
4838407af329SVlad Yasevich 	struct ifinfomsg *ifm;
4839407af329SVlad Yasevich 	struct net_device *dev;
4840407af329SVlad Yasevich 	struct nlattr *br_spec, *attr = NULL;
4841407af329SVlad Yasevich 	int rem, err = -EOPNOTSUPP;
48424de8b413SRosen, Rami 	u16 flags = 0;
4843407af329SVlad Yasevich 	bool have_flags = false;
4844407af329SVlad Yasevich 
4845407af329SVlad Yasevich 	if (nlmsg_len(nlh) < sizeof(*ifm))
4846407af329SVlad Yasevich 		return -EINVAL;
4847407af329SVlad Yasevich 
4848407af329SVlad Yasevich 	ifm = nlmsg_data(nlh);
4849407af329SVlad Yasevich 	if (ifm->ifi_family != AF_BRIDGE)
4850407af329SVlad Yasevich 		return -EPFNOSUPPORT;
4851407af329SVlad Yasevich 
4852407af329SVlad Yasevich 	dev = __dev_get_by_index(net, ifm->ifi_index);
4853407af329SVlad Yasevich 	if (!dev) {
4854b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "unknown ifindex");
4855407af329SVlad Yasevich 		return -ENODEV;
4856407af329SVlad Yasevich 	}
4857407af329SVlad Yasevich 
4858407af329SVlad Yasevich 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4859407af329SVlad Yasevich 	if (br_spec) {
4860407af329SVlad Yasevich 		nla_for_each_nested(attr, br_spec, rem) {
4861407af329SVlad Yasevich 			if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
48626e8d1c55SThomas Graf 				if (nla_len(attr) < sizeof(flags))
48636e8d1c55SThomas Graf 					return -EINVAL;
48646e8d1c55SThomas Graf 
4865407af329SVlad Yasevich 				have_flags = true;
4866407af329SVlad Yasevich 				flags = nla_get_u16(attr);
4867407af329SVlad Yasevich 				break;
4868407af329SVlad Yasevich 			}
4869407af329SVlad Yasevich 		}
4870407af329SVlad Yasevich 	}
4871407af329SVlad Yasevich 
4872407af329SVlad Yasevich 	if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
4873407af329SVlad Yasevich 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4874407af329SVlad Yasevich 
4875407af329SVlad Yasevich 		if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
4876407af329SVlad Yasevich 			err = -EOPNOTSUPP;
4877407af329SVlad Yasevich 			goto out;
4878407af329SVlad Yasevich 		}
4879407af329SVlad Yasevich 
4880add511b3SRoopa Prabhu 		err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
4881407af329SVlad Yasevich 		if (err)
4882407af329SVlad Yasevich 			goto out;
4883407af329SVlad Yasevich 
4884407af329SVlad Yasevich 		flags &= ~BRIDGE_FLAGS_MASTER;
4885407af329SVlad Yasevich 	}
4886407af329SVlad Yasevich 
4887407af329SVlad Yasevich 	if ((flags & BRIDGE_FLAGS_SELF)) {
4888407af329SVlad Yasevich 		if (!dev->netdev_ops->ndo_bridge_dellink)
4889407af329SVlad Yasevich 			err = -EOPNOTSUPP;
4890407af329SVlad Yasevich 		else
4891add511b3SRoopa Prabhu 			err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
4892add511b3SRoopa Prabhu 								  flags);
4893407af329SVlad Yasevich 
489402dba438SRoopa Prabhu 		if (!err) {
4895407af329SVlad Yasevich 			flags &= ~BRIDGE_FLAGS_SELF;
489602dba438SRoopa Prabhu 
489702dba438SRoopa Prabhu 			/* Generate event to notify upper layer of bridge
489802dba438SRoopa Prabhu 			 * change
489902dba438SRoopa Prabhu 			 */
490002dba438SRoopa Prabhu 			err = rtnl_bridge_notify(dev);
490102dba438SRoopa Prabhu 		}
4902407af329SVlad Yasevich 	}
4903407af329SVlad Yasevich 
4904407af329SVlad Yasevich 	if (have_flags)
4905407af329SVlad Yasevich 		memcpy(nla_data(attr), &flags, sizeof(flags));
4906407af329SVlad Yasevich out:
4907407af329SVlad Yasevich 	return err;
4908407af329SVlad Yasevich }
4909407af329SVlad Yasevich 
4910e8872a25SNikolay Aleksandrov static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
4911e8872a25SNikolay Aleksandrov {
4912e8872a25SNikolay Aleksandrov 	return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
4913e8872a25SNikolay Aleksandrov 	       (!idxattr || idxattr == attrid);
4914e8872a25SNikolay Aleksandrov }
4915e8872a25SNikolay Aleksandrov 
491669ae6ad2SNogah Frankel #define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
491769ae6ad2SNogah Frankel static int rtnl_get_offload_stats_attr_size(int attr_id)
491869ae6ad2SNogah Frankel {
491969ae6ad2SNogah Frankel 	switch (attr_id) {
492069ae6ad2SNogah Frankel 	case IFLA_OFFLOAD_XSTATS_CPU_HIT:
492169ae6ad2SNogah Frankel 		return sizeof(struct rtnl_link_stats64);
492269ae6ad2SNogah Frankel 	}
492369ae6ad2SNogah Frankel 
492469ae6ad2SNogah Frankel 	return 0;
492569ae6ad2SNogah Frankel }
492669ae6ad2SNogah Frankel 
492769ae6ad2SNogah Frankel static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev,
492869ae6ad2SNogah Frankel 				  int *prividx)
492969ae6ad2SNogah Frankel {
493069ae6ad2SNogah Frankel 	struct nlattr *attr = NULL;
493169ae6ad2SNogah Frankel 	int attr_id, size;
493269ae6ad2SNogah Frankel 	void *attr_data;
493369ae6ad2SNogah Frankel 	int err;
493469ae6ad2SNogah Frankel 
493569ae6ad2SNogah Frankel 	if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
493669ae6ad2SNogah Frankel 	      dev->netdev_ops->ndo_get_offload_stats))
493769ae6ad2SNogah Frankel 		return -ENODATA;
493869ae6ad2SNogah Frankel 
493969ae6ad2SNogah Frankel 	for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
494069ae6ad2SNogah Frankel 	     attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
494169ae6ad2SNogah Frankel 		if (attr_id < *prividx)
494269ae6ad2SNogah Frankel 			continue;
494369ae6ad2SNogah Frankel 
494469ae6ad2SNogah Frankel 		size = rtnl_get_offload_stats_attr_size(attr_id);
494569ae6ad2SNogah Frankel 		if (!size)
494669ae6ad2SNogah Frankel 			continue;
494769ae6ad2SNogah Frankel 
49483df5b3c6SOr Gerlitz 		if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
494969ae6ad2SNogah Frankel 			continue;
495069ae6ad2SNogah Frankel 
495169ae6ad2SNogah Frankel 		attr = nla_reserve_64bit(skb, attr_id, size,
495269ae6ad2SNogah Frankel 					 IFLA_OFFLOAD_XSTATS_UNSPEC);
495369ae6ad2SNogah Frankel 		if (!attr)
495469ae6ad2SNogah Frankel 			goto nla_put_failure;
495569ae6ad2SNogah Frankel 
495669ae6ad2SNogah Frankel 		attr_data = nla_data(attr);
495769ae6ad2SNogah Frankel 		memset(attr_data, 0, size);
495869ae6ad2SNogah Frankel 		err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev,
495969ae6ad2SNogah Frankel 							     attr_data);
496069ae6ad2SNogah Frankel 		if (err)
496169ae6ad2SNogah Frankel 			goto get_offload_stats_failure;
496269ae6ad2SNogah Frankel 	}
496369ae6ad2SNogah Frankel 
496469ae6ad2SNogah Frankel 	if (!attr)
496569ae6ad2SNogah Frankel 		return -ENODATA;
496669ae6ad2SNogah Frankel 
496769ae6ad2SNogah Frankel 	*prividx = 0;
496869ae6ad2SNogah Frankel 	return 0;
496969ae6ad2SNogah Frankel 
497069ae6ad2SNogah Frankel nla_put_failure:
497169ae6ad2SNogah Frankel 	err = -EMSGSIZE;
497269ae6ad2SNogah Frankel get_offload_stats_failure:
497369ae6ad2SNogah Frankel 	*prividx = attr_id;
497469ae6ad2SNogah Frankel 	return err;
497569ae6ad2SNogah Frankel }
497669ae6ad2SNogah Frankel 
497769ae6ad2SNogah Frankel static int rtnl_get_offload_stats_size(const struct net_device *dev)
497869ae6ad2SNogah Frankel {
497969ae6ad2SNogah Frankel 	int nla_size = 0;
498069ae6ad2SNogah Frankel 	int attr_id;
498169ae6ad2SNogah Frankel 	int size;
498269ae6ad2SNogah Frankel 
498369ae6ad2SNogah Frankel 	if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
498469ae6ad2SNogah Frankel 	      dev->netdev_ops->ndo_get_offload_stats))
498569ae6ad2SNogah Frankel 		return 0;
498669ae6ad2SNogah Frankel 
498769ae6ad2SNogah Frankel 	for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
498869ae6ad2SNogah Frankel 	     attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
49893df5b3c6SOr Gerlitz 		if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
499069ae6ad2SNogah Frankel 			continue;
499169ae6ad2SNogah Frankel 		size = rtnl_get_offload_stats_attr_size(attr_id);
499269ae6ad2SNogah Frankel 		nla_size += nla_total_size_64bit(size);
499369ae6ad2SNogah Frankel 	}
499469ae6ad2SNogah Frankel 
499569ae6ad2SNogah Frankel 	if (nla_size != 0)
499669ae6ad2SNogah Frankel 		nla_size += nla_total_size(0);
499769ae6ad2SNogah Frankel 
499869ae6ad2SNogah Frankel 	return nla_size;
499969ae6ad2SNogah Frankel }
500069ae6ad2SNogah Frankel 
500110c9ead9SRoopa Prabhu static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
500210c9ead9SRoopa Prabhu 			       int type, u32 pid, u32 seq, u32 change,
5003e8872a25SNikolay Aleksandrov 			       unsigned int flags, unsigned int filter_mask,
5004e8872a25SNikolay Aleksandrov 			       int *idxattr, int *prividx)
500510c9ead9SRoopa Prabhu {
500610c9ead9SRoopa Prabhu 	struct if_stats_msg *ifsm;
500710c9ead9SRoopa Prabhu 	struct nlmsghdr *nlh;
500810c9ead9SRoopa Prabhu 	struct nlattr *attr;
5009e8872a25SNikolay Aleksandrov 	int s_prividx = *prividx;
501069ae6ad2SNogah Frankel 	int err;
501110c9ead9SRoopa Prabhu 
501210c9ead9SRoopa Prabhu 	ASSERT_RTNL();
501310c9ead9SRoopa Prabhu 
501410c9ead9SRoopa Prabhu 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
501510c9ead9SRoopa Prabhu 	if (!nlh)
501610c9ead9SRoopa Prabhu 		return -EMSGSIZE;
501710c9ead9SRoopa Prabhu 
501810c9ead9SRoopa Prabhu 	ifsm = nlmsg_data(nlh);
5019ce024f42SNikolay Aleksandrov 	ifsm->family = PF_UNSPEC;
5020ce024f42SNikolay Aleksandrov 	ifsm->pad1 = 0;
5021ce024f42SNikolay Aleksandrov 	ifsm->pad2 = 0;
502210c9ead9SRoopa Prabhu 	ifsm->ifindex = dev->ifindex;
502310c9ead9SRoopa Prabhu 	ifsm->filter_mask = filter_mask;
502410c9ead9SRoopa Prabhu 
5025e8872a25SNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
502610c9ead9SRoopa Prabhu 		struct rtnl_link_stats64 *sp;
502710c9ead9SRoopa Prabhu 
502858414d32SNicolas Dichtel 		attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
502958414d32SNicolas Dichtel 					 sizeof(struct rtnl_link_stats64),
503058414d32SNicolas Dichtel 					 IFLA_STATS_UNSPEC);
503110c9ead9SRoopa Prabhu 		if (!attr)
503210c9ead9SRoopa Prabhu 			goto nla_put_failure;
503310c9ead9SRoopa Prabhu 
503410c9ead9SRoopa Prabhu 		sp = nla_data(attr);
503510c9ead9SRoopa Prabhu 		dev_get_stats(dev, sp);
503610c9ead9SRoopa Prabhu 	}
503710c9ead9SRoopa Prabhu 
503897a47facSNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
503997a47facSNikolay Aleksandrov 		const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
504097a47facSNikolay Aleksandrov 
504197a47facSNikolay Aleksandrov 		if (ops && ops->fill_linkxstats) {
504297a47facSNikolay Aleksandrov 			*idxattr = IFLA_STATS_LINK_XSTATS;
5043ae0be8deSMichal Kubecek 			attr = nla_nest_start_noflag(skb,
504497a47facSNikolay Aleksandrov 						     IFLA_STATS_LINK_XSTATS);
504597a47facSNikolay Aleksandrov 			if (!attr)
504697a47facSNikolay Aleksandrov 				goto nla_put_failure;
504797a47facSNikolay Aleksandrov 
504880e73cc5SNikolay Aleksandrov 			err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
504980e73cc5SNikolay Aleksandrov 			nla_nest_end(skb, attr);
505080e73cc5SNikolay Aleksandrov 			if (err)
505180e73cc5SNikolay Aleksandrov 				goto nla_put_failure;
505280e73cc5SNikolay Aleksandrov 			*idxattr = 0;
505380e73cc5SNikolay Aleksandrov 		}
505480e73cc5SNikolay Aleksandrov 	}
505580e73cc5SNikolay Aleksandrov 
505680e73cc5SNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
505780e73cc5SNikolay Aleksandrov 			     *idxattr)) {
505880e73cc5SNikolay Aleksandrov 		const struct rtnl_link_ops *ops = NULL;
505980e73cc5SNikolay Aleksandrov 		const struct net_device *master;
506080e73cc5SNikolay Aleksandrov 
506180e73cc5SNikolay Aleksandrov 		master = netdev_master_upper_dev_get(dev);
506280e73cc5SNikolay Aleksandrov 		if (master)
506380e73cc5SNikolay Aleksandrov 			ops = master->rtnl_link_ops;
506480e73cc5SNikolay Aleksandrov 		if (ops && ops->fill_linkxstats) {
506580e73cc5SNikolay Aleksandrov 			*idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
5066ae0be8deSMichal Kubecek 			attr = nla_nest_start_noflag(skb,
506780e73cc5SNikolay Aleksandrov 						     IFLA_STATS_LINK_XSTATS_SLAVE);
506880e73cc5SNikolay Aleksandrov 			if (!attr)
506980e73cc5SNikolay Aleksandrov 				goto nla_put_failure;
507080e73cc5SNikolay Aleksandrov 
507180e73cc5SNikolay Aleksandrov 			err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
507297a47facSNikolay Aleksandrov 			nla_nest_end(skb, attr);
507397a47facSNikolay Aleksandrov 			if (err)
507497a47facSNikolay Aleksandrov 				goto nla_put_failure;
507597a47facSNikolay Aleksandrov 			*idxattr = 0;
507697a47facSNikolay Aleksandrov 		}
507797a47facSNikolay Aleksandrov 	}
507897a47facSNikolay Aleksandrov 
507969ae6ad2SNogah Frankel 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
508069ae6ad2SNogah Frankel 			     *idxattr)) {
508169ae6ad2SNogah Frankel 		*idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
5082ae0be8deSMichal Kubecek 		attr = nla_nest_start_noflag(skb,
5083ae0be8deSMichal Kubecek 					     IFLA_STATS_LINK_OFFLOAD_XSTATS);
508469ae6ad2SNogah Frankel 		if (!attr)
508569ae6ad2SNogah Frankel 			goto nla_put_failure;
508669ae6ad2SNogah Frankel 
508769ae6ad2SNogah Frankel 		err = rtnl_get_offload_stats(skb, dev, prividx);
508869ae6ad2SNogah Frankel 		if (err == -ENODATA)
508969ae6ad2SNogah Frankel 			nla_nest_cancel(skb, attr);
509069ae6ad2SNogah Frankel 		else
509169ae6ad2SNogah Frankel 			nla_nest_end(skb, attr);
509269ae6ad2SNogah Frankel 
509369ae6ad2SNogah Frankel 		if (err && err != -ENODATA)
509469ae6ad2SNogah Frankel 			goto nla_put_failure;
509569ae6ad2SNogah Frankel 		*idxattr = 0;
509669ae6ad2SNogah Frankel 	}
509769ae6ad2SNogah Frankel 
5098aefb4d4aSRobert Shearman 	if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
5099aefb4d4aSRobert Shearman 		struct rtnl_af_ops *af_ops;
5100aefb4d4aSRobert Shearman 
5101aefb4d4aSRobert Shearman 		*idxattr = IFLA_STATS_AF_SPEC;
5102ae0be8deSMichal Kubecek 		attr = nla_nest_start_noflag(skb, IFLA_STATS_AF_SPEC);
5103aefb4d4aSRobert Shearman 		if (!attr)
5104aefb4d4aSRobert Shearman 			goto nla_put_failure;
5105aefb4d4aSRobert Shearman 
51065fa85a09SFlorian Westphal 		rcu_read_lock();
51075fa85a09SFlorian Westphal 		list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
5108aefb4d4aSRobert Shearman 			if (af_ops->fill_stats_af) {
5109aefb4d4aSRobert Shearman 				struct nlattr *af;
5110aefb4d4aSRobert Shearman 				int err;
5111aefb4d4aSRobert Shearman 
5112ae0be8deSMichal Kubecek 				af = nla_nest_start_noflag(skb,
5113ae0be8deSMichal Kubecek 							   af_ops->family);
51145fa85a09SFlorian Westphal 				if (!af) {
51155fa85a09SFlorian Westphal 					rcu_read_unlock();
5116aefb4d4aSRobert Shearman 					goto nla_put_failure;
51175fa85a09SFlorian Westphal 				}
5118aefb4d4aSRobert Shearman 				err = af_ops->fill_stats_af(skb, dev);
5119aefb4d4aSRobert Shearman 
51205fa85a09SFlorian Westphal 				if (err == -ENODATA) {
5121aefb4d4aSRobert Shearman 					nla_nest_cancel(skb, af);
51225fa85a09SFlorian Westphal 				} else if (err < 0) {
51235fa85a09SFlorian Westphal 					rcu_read_unlock();
5124aefb4d4aSRobert Shearman 					goto nla_put_failure;
51255fa85a09SFlorian Westphal 				}
5126aefb4d4aSRobert Shearman 
5127aefb4d4aSRobert Shearman 				nla_nest_end(skb, af);
5128aefb4d4aSRobert Shearman 			}
5129aefb4d4aSRobert Shearman 		}
51305fa85a09SFlorian Westphal 		rcu_read_unlock();
5131aefb4d4aSRobert Shearman 
5132aefb4d4aSRobert Shearman 		nla_nest_end(skb, attr);
5133aefb4d4aSRobert Shearman 
5134aefb4d4aSRobert Shearman 		*idxattr = 0;
5135aefb4d4aSRobert Shearman 	}
5136aefb4d4aSRobert Shearman 
513710c9ead9SRoopa Prabhu 	nlmsg_end(skb, nlh);
513810c9ead9SRoopa Prabhu 
513910c9ead9SRoopa Prabhu 	return 0;
514010c9ead9SRoopa Prabhu 
514110c9ead9SRoopa Prabhu nla_put_failure:
5142e8872a25SNikolay Aleksandrov 	/* not a multi message or no progress mean a real error */
5143e8872a25SNikolay Aleksandrov 	if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
514410c9ead9SRoopa Prabhu 		nlmsg_cancel(skb, nlh);
5145e8872a25SNikolay Aleksandrov 	else
5146e8872a25SNikolay Aleksandrov 		nlmsg_end(skb, nlh);
514710c9ead9SRoopa Prabhu 
514810c9ead9SRoopa Prabhu 	return -EMSGSIZE;
514910c9ead9SRoopa Prabhu }
515010c9ead9SRoopa Prabhu 
515110c9ead9SRoopa Prabhu static size_t if_nlmsg_stats_size(const struct net_device *dev,
515210c9ead9SRoopa Prabhu 				  u32 filter_mask)
515310c9ead9SRoopa Prabhu {
515410c9ead9SRoopa Prabhu 	size_t size = 0;
515510c9ead9SRoopa Prabhu 
5156e8872a25SNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
515710c9ead9SRoopa Prabhu 		size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
515810c9ead9SRoopa Prabhu 
515997a47facSNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
516097a47facSNikolay Aleksandrov 		const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
516180e73cc5SNikolay Aleksandrov 		int attr = IFLA_STATS_LINK_XSTATS;
516297a47facSNikolay Aleksandrov 
516397a47facSNikolay Aleksandrov 		if (ops && ops->get_linkxstats_size) {
516480e73cc5SNikolay Aleksandrov 			size += nla_total_size(ops->get_linkxstats_size(dev,
516580e73cc5SNikolay Aleksandrov 									attr));
516697a47facSNikolay Aleksandrov 			/* for IFLA_STATS_LINK_XSTATS */
516797a47facSNikolay Aleksandrov 			size += nla_total_size(0);
516897a47facSNikolay Aleksandrov 		}
516997a47facSNikolay Aleksandrov 	}
517097a47facSNikolay Aleksandrov 
517180e73cc5SNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
517280e73cc5SNikolay Aleksandrov 		struct net_device *_dev = (struct net_device *)dev;
517380e73cc5SNikolay Aleksandrov 		const struct rtnl_link_ops *ops = NULL;
517480e73cc5SNikolay Aleksandrov 		const struct net_device *master;
517580e73cc5SNikolay Aleksandrov 
517680e73cc5SNikolay Aleksandrov 		/* netdev_master_upper_dev_get can't take const */
517780e73cc5SNikolay Aleksandrov 		master = netdev_master_upper_dev_get(_dev);
517880e73cc5SNikolay Aleksandrov 		if (master)
517980e73cc5SNikolay Aleksandrov 			ops = master->rtnl_link_ops;
518080e73cc5SNikolay Aleksandrov 		if (ops && ops->get_linkxstats_size) {
518180e73cc5SNikolay Aleksandrov 			int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
518280e73cc5SNikolay Aleksandrov 
518380e73cc5SNikolay Aleksandrov 			size += nla_total_size(ops->get_linkxstats_size(dev,
518480e73cc5SNikolay Aleksandrov 									attr));
518580e73cc5SNikolay Aleksandrov 			/* for IFLA_STATS_LINK_XSTATS_SLAVE */
518680e73cc5SNikolay Aleksandrov 			size += nla_total_size(0);
518780e73cc5SNikolay Aleksandrov 		}
518880e73cc5SNikolay Aleksandrov 	}
518980e73cc5SNikolay Aleksandrov 
519069ae6ad2SNogah Frankel 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0))
519169ae6ad2SNogah Frankel 		size += rtnl_get_offload_stats_size(dev);
519269ae6ad2SNogah Frankel 
5193aefb4d4aSRobert Shearman 	if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
5194aefb4d4aSRobert Shearman 		struct rtnl_af_ops *af_ops;
5195aefb4d4aSRobert Shearman 
5196aefb4d4aSRobert Shearman 		/* for IFLA_STATS_AF_SPEC */
5197aefb4d4aSRobert Shearman 		size += nla_total_size(0);
5198aefb4d4aSRobert Shearman 
51995fa85a09SFlorian Westphal 		rcu_read_lock();
52005fa85a09SFlorian Westphal 		list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
5201aefb4d4aSRobert Shearman 			if (af_ops->get_stats_af_size) {
5202aefb4d4aSRobert Shearman 				size += nla_total_size(
5203aefb4d4aSRobert Shearman 					af_ops->get_stats_af_size(dev));
5204aefb4d4aSRobert Shearman 
5205aefb4d4aSRobert Shearman 				/* for AF_* */
5206aefb4d4aSRobert Shearman 				size += nla_total_size(0);
5207aefb4d4aSRobert Shearman 			}
5208aefb4d4aSRobert Shearman 		}
52095fa85a09SFlorian Westphal 		rcu_read_unlock();
5210aefb4d4aSRobert Shearman 	}
5211aefb4d4aSRobert Shearman 
521210c9ead9SRoopa Prabhu 	return size;
521310c9ead9SRoopa Prabhu }
521410c9ead9SRoopa Prabhu 
521551bc860dSJakub Kicinski static int rtnl_valid_stats_req(const struct nlmsghdr *nlh, bool strict_check,
521651bc860dSJakub Kicinski 				bool is_dump, struct netlink_ext_ack *extack)
521751bc860dSJakub Kicinski {
521851bc860dSJakub Kicinski 	struct if_stats_msg *ifsm;
521951bc860dSJakub Kicinski 
522069f23a09SEric Dumazet 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifsm))) {
522151bc860dSJakub Kicinski 		NL_SET_ERR_MSG(extack, "Invalid header for stats dump");
522251bc860dSJakub Kicinski 		return -EINVAL;
522351bc860dSJakub Kicinski 	}
522451bc860dSJakub Kicinski 
522551bc860dSJakub Kicinski 	if (!strict_check)
522651bc860dSJakub Kicinski 		return 0;
522751bc860dSJakub Kicinski 
522851bc860dSJakub Kicinski 	ifsm = nlmsg_data(nlh);
522951bc860dSJakub Kicinski 
523051bc860dSJakub Kicinski 	/* only requests using strict checks can pass data to influence
523151bc860dSJakub Kicinski 	 * the dump. The legacy exception is filter_mask.
523251bc860dSJakub Kicinski 	 */
523351bc860dSJakub Kicinski 	if (ifsm->pad1 || ifsm->pad2 || (is_dump && ifsm->ifindex)) {
523451bc860dSJakub Kicinski 		NL_SET_ERR_MSG(extack, "Invalid values in header for stats dump request");
523551bc860dSJakub Kicinski 		return -EINVAL;
523651bc860dSJakub Kicinski 	}
523751bc860dSJakub Kicinski 	if (nlmsg_attrlen(nlh, sizeof(*ifsm))) {
523851bc860dSJakub Kicinski 		NL_SET_ERR_MSG(extack, "Invalid attributes after stats header");
523951bc860dSJakub Kicinski 		return -EINVAL;
524051bc860dSJakub Kicinski 	}
52416300acb2SJakub Kicinski 	if (ifsm->filter_mask >= IFLA_STATS_FILTER_BIT(IFLA_STATS_MAX + 1)) {
52426300acb2SJakub Kicinski 		NL_SET_ERR_MSG(extack, "Invalid stats requested through filter mask");
52436300acb2SJakub Kicinski 		return -EINVAL;
52446300acb2SJakub Kicinski 	}
524551bc860dSJakub Kicinski 
524651bc860dSJakub Kicinski 	return 0;
524751bc860dSJakub Kicinski }
524851bc860dSJakub Kicinski 
5249c21ef3e3SDavid Ahern static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
5250c21ef3e3SDavid Ahern 			  struct netlink_ext_ack *extack)
525110c9ead9SRoopa Prabhu {
525210c9ead9SRoopa Prabhu 	struct net *net = sock_net(skb->sk);
525310c9ead9SRoopa Prabhu 	struct net_device *dev = NULL;
5254e8872a25SNikolay Aleksandrov 	int idxattr = 0, prividx = 0;
5255e8872a25SNikolay Aleksandrov 	struct if_stats_msg *ifsm;
525610c9ead9SRoopa Prabhu 	struct sk_buff *nskb;
525710c9ead9SRoopa Prabhu 	u32 filter_mask;
525810c9ead9SRoopa Prabhu 	int err;
525910c9ead9SRoopa Prabhu 
526051bc860dSJakub Kicinski 	err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb),
526151bc860dSJakub Kicinski 				   false, extack);
526251bc860dSJakub Kicinski 	if (err)
526351bc860dSJakub Kicinski 		return err;
52644775cc1fSMathias Krause 
526510c9ead9SRoopa Prabhu 	ifsm = nlmsg_data(nlh);
526610c9ead9SRoopa Prabhu 	if (ifsm->ifindex > 0)
526710c9ead9SRoopa Prabhu 		dev = __dev_get_by_index(net, ifsm->ifindex);
526810c9ead9SRoopa Prabhu 	else
526910c9ead9SRoopa Prabhu 		return -EINVAL;
527010c9ead9SRoopa Prabhu 
527110c9ead9SRoopa Prabhu 	if (!dev)
527210c9ead9SRoopa Prabhu 		return -ENODEV;
527310c9ead9SRoopa Prabhu 
527410c9ead9SRoopa Prabhu 	filter_mask = ifsm->filter_mask;
527510c9ead9SRoopa Prabhu 	if (!filter_mask)
527610c9ead9SRoopa Prabhu 		return -EINVAL;
527710c9ead9SRoopa Prabhu 
527810c9ead9SRoopa Prabhu 	nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
527910c9ead9SRoopa Prabhu 	if (!nskb)
528010c9ead9SRoopa Prabhu 		return -ENOBUFS;
528110c9ead9SRoopa Prabhu 
528210c9ead9SRoopa Prabhu 	err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
528310c9ead9SRoopa Prabhu 				  NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
5284e8872a25SNikolay Aleksandrov 				  0, filter_mask, &idxattr, &prividx);
528510c9ead9SRoopa Prabhu 	if (err < 0) {
528610c9ead9SRoopa Prabhu 		/* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
528710c9ead9SRoopa Prabhu 		WARN_ON(err == -EMSGSIZE);
528810c9ead9SRoopa Prabhu 		kfree_skb(nskb);
528910c9ead9SRoopa Prabhu 	} else {
529010c9ead9SRoopa Prabhu 		err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
529110c9ead9SRoopa Prabhu 	}
529210c9ead9SRoopa Prabhu 
529310c9ead9SRoopa Prabhu 	return err;
529410c9ead9SRoopa Prabhu }
529510c9ead9SRoopa Prabhu 
529610c9ead9SRoopa Prabhu static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
529710c9ead9SRoopa Prabhu {
5298841891ecSDavid Ahern 	struct netlink_ext_ack *extack = cb->extack;
5299e8872a25SNikolay Aleksandrov 	int h, s_h, err, s_idx, s_idxattr, s_prividx;
530010c9ead9SRoopa Prabhu 	struct net *net = sock_net(skb->sk);
530110c9ead9SRoopa Prabhu 	unsigned int flags = NLM_F_MULTI;
5302e8872a25SNikolay Aleksandrov 	struct if_stats_msg *ifsm;
5303e8872a25SNikolay Aleksandrov 	struct hlist_head *head;
5304e8872a25SNikolay Aleksandrov 	struct net_device *dev;
530510c9ead9SRoopa Prabhu 	u32 filter_mask = 0;
5306e8872a25SNikolay Aleksandrov 	int idx = 0;
530710c9ead9SRoopa Prabhu 
530810c9ead9SRoopa Prabhu 	s_h = cb->args[0];
530910c9ead9SRoopa Prabhu 	s_idx = cb->args[1];
5310e8872a25SNikolay Aleksandrov 	s_idxattr = cb->args[2];
5311e8872a25SNikolay Aleksandrov 	s_prividx = cb->args[3];
531210c9ead9SRoopa Prabhu 
531310c9ead9SRoopa Prabhu 	cb->seq = net->dev_base_seq;
531410c9ead9SRoopa Prabhu 
531551bc860dSJakub Kicinski 	err = rtnl_valid_stats_req(cb->nlh, cb->strict_check, true, extack);
531651bc860dSJakub Kicinski 	if (err)
531751bc860dSJakub Kicinski 		return err;
53184775cc1fSMathias Krause 
531910c9ead9SRoopa Prabhu 	ifsm = nlmsg_data(cb->nlh);
5320841891ecSDavid Ahern 	filter_mask = ifsm->filter_mask;
5321841891ecSDavid Ahern 	if (!filter_mask) {
5322841891ecSDavid Ahern 		NL_SET_ERR_MSG(extack, "Filter mask must be set for stats dump");
5323841891ecSDavid Ahern 		return -EINVAL;
5324841891ecSDavid Ahern 	}
532510c9ead9SRoopa Prabhu 
532610c9ead9SRoopa Prabhu 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
532710c9ead9SRoopa Prabhu 		idx = 0;
532810c9ead9SRoopa Prabhu 		head = &net->dev_index_head[h];
532910c9ead9SRoopa Prabhu 		hlist_for_each_entry(dev, head, index_hlist) {
533010c9ead9SRoopa Prabhu 			if (idx < s_idx)
533110c9ead9SRoopa Prabhu 				goto cont;
533210c9ead9SRoopa Prabhu 			err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
533310c9ead9SRoopa Prabhu 						  NETLINK_CB(cb->skb).portid,
533410c9ead9SRoopa Prabhu 						  cb->nlh->nlmsg_seq, 0,
5335e8872a25SNikolay Aleksandrov 						  flags, filter_mask,
5336e8872a25SNikolay Aleksandrov 						  &s_idxattr, &s_prividx);
533710c9ead9SRoopa Prabhu 			/* If we ran out of room on the first message,
533810c9ead9SRoopa Prabhu 			 * we're in trouble
533910c9ead9SRoopa Prabhu 			 */
534010c9ead9SRoopa Prabhu 			WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
534110c9ead9SRoopa Prabhu 
534210c9ead9SRoopa Prabhu 			if (err < 0)
534310c9ead9SRoopa Prabhu 				goto out;
5344e8872a25SNikolay Aleksandrov 			s_prividx = 0;
5345e8872a25SNikolay Aleksandrov 			s_idxattr = 0;
534610c9ead9SRoopa Prabhu 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
534710c9ead9SRoopa Prabhu cont:
534810c9ead9SRoopa Prabhu 			idx++;
534910c9ead9SRoopa Prabhu 		}
535010c9ead9SRoopa Prabhu 	}
535110c9ead9SRoopa Prabhu out:
5352e8872a25SNikolay Aleksandrov 	cb->args[3] = s_prividx;
5353e8872a25SNikolay Aleksandrov 	cb->args[2] = s_idxattr;
535410c9ead9SRoopa Prabhu 	cb->args[1] = idx;
535510c9ead9SRoopa Prabhu 	cb->args[0] = h;
535610c9ead9SRoopa Prabhu 
535710c9ead9SRoopa Prabhu 	return skb->len;
535810c9ead9SRoopa Prabhu }
535910c9ead9SRoopa Prabhu 
53601da177e4SLinus Torvalds /* Process one rtnetlink message. */
53611da177e4SLinus Torvalds 
53622d4bc933SJohannes Berg static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
53632d4bc933SJohannes Berg 			     struct netlink_ext_ack *extack)
53641da177e4SLinus Torvalds {
53653b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
5366addf9b90SFlorian Westphal 	struct rtnl_link *link;
5367e4202511SFlorian Westphal 	struct module *owner;
53686853dd48SFlorian Westphal 	int err = -EOPNOTSUPP;
5369e2849863SThomas Graf 	rtnl_doit_func doit;
537062256f98SFlorian Westphal 	unsigned int flags;
5371617cfc75SAlexander Kuleshov 	int kind;
53721da177e4SLinus Torvalds 	int family;
53731da177e4SLinus Torvalds 	int type;
53741da177e4SLinus Torvalds 
53751da177e4SLinus Torvalds 	type = nlh->nlmsg_type;
53761da177e4SLinus Torvalds 	if (type > RTM_MAX)
5377038890feSThomas Graf 		return -EOPNOTSUPP;
53781da177e4SLinus Torvalds 
53791da177e4SLinus Torvalds 	type -= RTM_BASE;
53801da177e4SLinus Torvalds 
53811da177e4SLinus Torvalds 	/* All the messages must have at least 1 byte length */
5382573ce260SHong zhi guo 	if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
53831da177e4SLinus Torvalds 		return 0;
53841da177e4SLinus Torvalds 
5385573ce260SHong zhi guo 	family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
53861da177e4SLinus Torvalds 	kind = type&3;
53871da177e4SLinus Torvalds 
538890f62cf3SEric W. Biederman 	if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
53891d00a4ebSThomas Graf 		return -EPERM;
53901da177e4SLinus Torvalds 
53916853dd48SFlorian Westphal 	rcu_read_lock();
5392b8f3ab42SDavid S. Miller 	if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
539397c53cacSDenis V. Lunev 		struct sock *rtnl;
5394e2849863SThomas Graf 		rtnl_dumpit_func dumpit;
5395c7ac8679SGreg Rose 		u16 min_dump_alloc = 0;
53961da177e4SLinus Torvalds 
5397addf9b90SFlorian Westphal 		link = rtnl_get_link(family, type);
5398addf9b90SFlorian Westphal 		if (!link || !link->dumpit) {
53996853dd48SFlorian Westphal 			family = PF_UNSPEC;
5400addf9b90SFlorian Westphal 			link = rtnl_get_link(family, type);
5401addf9b90SFlorian Westphal 			if (!link || !link->dumpit)
54026853dd48SFlorian Westphal 				goto err_unlock;
54036853dd48SFlorian Westphal 		}
5404e4202511SFlorian Westphal 		owner = link->owner;
5405addf9b90SFlorian Westphal 		dumpit = link->dumpit;
54066853dd48SFlorian Westphal 
54075c2bb9b6SFlorian Westphal 		if (type == RTM_GETLINK - RTM_BASE)
5408e1fa6d21SFlorian Westphal 			min_dump_alloc = rtnl_calcit(skb, nlh);
54091da177e4SLinus Torvalds 
5410e4202511SFlorian Westphal 		err = 0;
5411e4202511SFlorian Westphal 		/* need to do this before rcu_read_unlock() */
5412e4202511SFlorian Westphal 		if (!try_module_get(owner))
5413e4202511SFlorian Westphal 			err = -EPROTONOSUPPORT;
5414e4202511SFlorian Westphal 
54156853dd48SFlorian Westphal 		rcu_read_unlock();
54166853dd48SFlorian Westphal 
541797c53cacSDenis V. Lunev 		rtnl = net->rtnl;
5418e4202511SFlorian Westphal 		if (err == 0) {
541980d326faSPablo Neira Ayuso 			struct netlink_dump_control c = {
542080d326faSPablo Neira Ayuso 				.dump		= dumpit,
542180d326faSPablo Neira Ayuso 				.min_dump_alloc	= min_dump_alloc,
5422e4202511SFlorian Westphal 				.module		= owner,
542380d326faSPablo Neira Ayuso 			};
542480d326faSPablo Neira Ayuso 			err = netlink_dump_start(rtnl, skb, nlh, &c);
5425e4202511SFlorian Westphal 			/* netlink_dump_start() will keep a reference on
5426e4202511SFlorian Westphal 			 * module if dump is still in progress.
5427e4202511SFlorian Westphal 			 */
5428e4202511SFlorian Westphal 			module_put(owner);
542980d326faSPablo Neira Ayuso 		}
54302907c35fSEric Dumazet 		return err;
54311da177e4SLinus Torvalds 	}
54321da177e4SLinus Torvalds 
5433addf9b90SFlorian Westphal 	link = rtnl_get_link(family, type);
5434addf9b90SFlorian Westphal 	if (!link || !link->doit) {
54358caa38b5SFlorian Westphal 		family = PF_UNSPEC;
5436addf9b90SFlorian Westphal 		link = rtnl_get_link(PF_UNSPEC, type);
5437addf9b90SFlorian Westphal 		if (!link || !link->doit)
5438addf9b90SFlorian Westphal 			goto out_unlock;
54398caa38b5SFlorian Westphal 	}
54408caa38b5SFlorian Westphal 
5441e4202511SFlorian Westphal 	owner = link->owner;
5442e4202511SFlorian Westphal 	if (!try_module_get(owner)) {
5443e4202511SFlorian Westphal 		err = -EPROTONOSUPPORT;
5444e4202511SFlorian Westphal 		goto out_unlock;
5445e4202511SFlorian Westphal 	}
5446e4202511SFlorian Westphal 
5447addf9b90SFlorian Westphal 	flags = link->flags;
544862256f98SFlorian Westphal 	if (flags & RTNL_FLAG_DOIT_UNLOCKED) {
5449addf9b90SFlorian Westphal 		doit = link->doit;
545062256f98SFlorian Westphal 		rcu_read_unlock();
545162256f98SFlorian Westphal 		if (doit)
545262256f98SFlorian Westphal 			err = doit(skb, nlh, extack);
5453e4202511SFlorian Westphal 		module_put(owner);
545462256f98SFlorian Westphal 		return err;
545562256f98SFlorian Westphal 	}
54566853dd48SFlorian Westphal 	rcu_read_unlock();
54576853dd48SFlorian Westphal 
54580cc09020SFlorian Westphal 	rtnl_lock();
5459addf9b90SFlorian Westphal 	link = rtnl_get_link(family, type);
5460addf9b90SFlorian Westphal 	if (link && link->doit)
5461addf9b90SFlorian Westphal 		err = link->doit(skb, nlh, extack);
54620cc09020SFlorian Westphal 	rtnl_unlock();
5463addf9b90SFlorian Westphal 
5464e4202511SFlorian Westphal 	module_put(owner);
5465e4202511SFlorian Westphal 
5466addf9b90SFlorian Westphal 	return err;
5467addf9b90SFlorian Westphal 
5468addf9b90SFlorian Westphal out_unlock:
5469addf9b90SFlorian Westphal 	rcu_read_unlock();
54700cc09020SFlorian Westphal 	return err;
54710cc09020SFlorian Westphal 
54720cc09020SFlorian Westphal err_unlock:
54736853dd48SFlorian Westphal 	rcu_read_unlock();
54740cc09020SFlorian Westphal 	return -EOPNOTSUPP;
54751da177e4SLinus Torvalds }
54761da177e4SLinus Torvalds 
5477cd40b7d3SDenis V. Lunev static void rtnetlink_rcv(struct sk_buff *skb)
54781da177e4SLinus Torvalds {
5479cd40b7d3SDenis V. Lunev 	netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
54801da177e4SLinus Torvalds }
54811da177e4SLinus Torvalds 
54825f729eaaSJulien Gomes static int rtnetlink_bind(struct net *net, int group)
54835f729eaaSJulien Gomes {
54845f729eaaSJulien Gomes 	switch (group) {
54855f729eaaSJulien Gomes 	case RTNLGRP_IPV4_MROUTE_R:
54865f729eaaSJulien Gomes 	case RTNLGRP_IPV6_MROUTE_R:
54875f729eaaSJulien Gomes 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
54885f729eaaSJulien Gomes 			return -EPERM;
54895f729eaaSJulien Gomes 		break;
54905f729eaaSJulien Gomes 	}
54915f729eaaSJulien Gomes 	return 0;
54925f729eaaSJulien Gomes }
54935f729eaaSJulien Gomes 
54941da177e4SLinus Torvalds static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
54951da177e4SLinus Torvalds {
5496351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
5497e9dc8653SEric W. Biederman 
54981da177e4SLinus Torvalds 	switch (event) {
54995138e86fSVlad Yasevich 	case NETDEV_REBOOT:
55008a212589SXin Long 	case NETDEV_CHANGEMTU:
55013753654eSDavid Ahern 	case NETDEV_CHANGEADDR:
55025138e86fSVlad Yasevich 	case NETDEV_CHANGENAME:
55035138e86fSVlad Yasevich 	case NETDEV_FEAT_CHANGE:
55045138e86fSVlad Yasevich 	case NETDEV_BONDING_FAILOVER:
5505e6e66594SXin Long 	case NETDEV_POST_TYPE_CHANGE:
55065138e86fSVlad Yasevich 	case NETDEV_NOTIFY_PEERS:
5507dc709f37SXin Long 	case NETDEV_CHANGEUPPER:
55085138e86fSVlad Yasevich 	case NETDEV_RESEND_IGMP:
55095138e86fSVlad Yasevich 	case NETDEV_CHANGEINFODATA:
5510eeda3fb9SXin Long 	case NETDEV_CHANGELOWERSTATE:
5511ebdcf045SXin Long 	case NETDEV_CHANGE_TX_QUEUE_LEN:
55123d3ea5afSVlad Yasevich 		rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
551338e01b30SNicolas Dichtel 				   GFP_KERNEL, NULL, 0);
55141da177e4SLinus Torvalds 		break;
55151da177e4SLinus Torvalds 	default:
55161da177e4SLinus Torvalds 		break;
55171da177e4SLinus Torvalds 	}
55181da177e4SLinus Torvalds 	return NOTIFY_DONE;
55191da177e4SLinus Torvalds }
55201da177e4SLinus Torvalds 
55211da177e4SLinus Torvalds static struct notifier_block rtnetlink_dev_notifier = {
55221da177e4SLinus Torvalds 	.notifier_call	= rtnetlink_event,
55231da177e4SLinus Torvalds };
55241da177e4SLinus Torvalds 
552597c53cacSDenis V. Lunev 
55262c8c1e72SAlexey Dobriyan static int __net_init rtnetlink_net_init(struct net *net)
552797c53cacSDenis V. Lunev {
552897c53cacSDenis V. Lunev 	struct sock *sk;
5529a31f2d17SPablo Neira Ayuso 	struct netlink_kernel_cfg cfg = {
5530a31f2d17SPablo Neira Ayuso 		.groups		= RTNLGRP_MAX,
5531a31f2d17SPablo Neira Ayuso 		.input		= rtnetlink_rcv,
5532a31f2d17SPablo Neira Ayuso 		.cb_mutex	= &rtnl_mutex,
55339785e10aSPablo Neira Ayuso 		.flags		= NL_CFG_F_NONROOT_RECV,
55345f729eaaSJulien Gomes 		.bind		= rtnetlink_bind,
5535a31f2d17SPablo Neira Ayuso 	};
5536a31f2d17SPablo Neira Ayuso 
55379f00d977SPablo Neira Ayuso 	sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
553897c53cacSDenis V. Lunev 	if (!sk)
553997c53cacSDenis V. Lunev 		return -ENOMEM;
554097c53cacSDenis V. Lunev 	net->rtnl = sk;
554197c53cacSDenis V. Lunev 	return 0;
554297c53cacSDenis V. Lunev }
554397c53cacSDenis V. Lunev 
55442c8c1e72SAlexey Dobriyan static void __net_exit rtnetlink_net_exit(struct net *net)
554597c53cacSDenis V. Lunev {
5546b7c6ba6eSDenis V. Lunev 	netlink_kernel_release(net->rtnl);
554797c53cacSDenis V. Lunev 	net->rtnl = NULL;
554897c53cacSDenis V. Lunev }
554997c53cacSDenis V. Lunev 
555097c53cacSDenis V. Lunev static struct pernet_operations rtnetlink_net_ops = {
555197c53cacSDenis V. Lunev 	.init = rtnetlink_net_init,
555297c53cacSDenis V. Lunev 	.exit = rtnetlink_net_exit,
555397c53cacSDenis V. Lunev };
555497c53cacSDenis V. Lunev 
55551da177e4SLinus Torvalds void __init rtnetlink_init(void)
55561da177e4SLinus Torvalds {
555797c53cacSDenis V. Lunev 	if (register_pernet_subsys(&rtnetlink_net_ops))
55581da177e4SLinus Torvalds 		panic("rtnetlink_init: cannot initialize rtnetlink\n");
555997c53cacSDenis V. Lunev 
55601da177e4SLinus Torvalds 	register_netdevice_notifier(&rtnetlink_dev_notifier);
5561340d17fcSThomas Graf 
5562c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
5563b97bac64SFlorian Westphal 		      rtnl_dump_ifinfo, 0);
5564b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0);
5565b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0);
5566b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0);
5567687ad8ccSThomas Graf 
5568b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0);
5569b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0);
5570b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0);
557177162022SJohn Fastabend 
557236fbf1e5SJiri Pirko 	rtnl_register(PF_UNSPEC, RTM_NEWLINKPROP, rtnl_newlinkprop, NULL, 0);
557336fbf1e5SJiri Pirko 	rtnl_register(PF_UNSPEC, RTM_DELLINKPROP, rtnl_dellinkprop, NULL, 0);
557436fbf1e5SJiri Pirko 
5575b97bac64SFlorian Westphal 	rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
5576b97bac64SFlorian Westphal 	rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0);
55775b2f94b2SRoopa Prabhu 	rtnl_register(PF_BRIDGE, RTM_GETNEIGH, rtnl_fdb_get, rtnl_fdb_dump, 0);
5578e5a55a89SJohn Fastabend 
5579b97bac64SFlorian Westphal 	rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0);
5580b97bac64SFlorian Westphal 	rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0);
5581b97bac64SFlorian Westphal 	rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0);
558210c9ead9SRoopa Prabhu 
558310c9ead9SRoopa Prabhu 	rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,
5584b97bac64SFlorian Westphal 		      0);
55851da177e4SLinus Torvalds }
5586