xref: /openbmc/linux/net/core/rtnetlink.c (revision f74877a5457d34d604dba6dbbb13c4c05bac8b93)
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 ||
832b00055aaSStefan Rompf 		     operstate == IF_OPER_UNKNOWN) &&
833b00055aaSStefan Rompf 		    !netif_dormant(dev))
834b00055aaSStefan Rompf 			operstate = IF_OPER_UP;
835b00055aaSStefan Rompf 		break;
836b00055aaSStefan Rompf 
837b00055aaSStefan Rompf 	case IF_OPER_DORMANT:
838b00055aaSStefan Rompf 		if (operstate == IF_OPER_UP ||
839b00055aaSStefan Rompf 		    operstate == IF_OPER_UNKNOWN)
840b00055aaSStefan Rompf 			operstate = IF_OPER_DORMANT;
841b00055aaSStefan Rompf 		break;
8423ff50b79SStephen Hemminger 	}
843b00055aaSStefan Rompf 
844b00055aaSStefan Rompf 	if (dev->operstate != operstate) {
845b00055aaSStefan Rompf 		write_lock_bh(&dev_base_lock);
846b00055aaSStefan Rompf 		dev->operstate = operstate;
847b00055aaSStefan Rompf 		write_unlock_bh(&dev_base_lock);
848b00055aaSStefan Rompf 		netdev_state_change(dev);
84993b2d4a2SDavid S. Miller 	}
850b00055aaSStefan Rompf }
851b00055aaSStefan Rompf 
852b1beb681SJiri Benc static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
853b1beb681SJiri Benc {
854b1beb681SJiri Benc 	return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
855b1beb681SJiri Benc 	       (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
856b1beb681SJiri Benc }
857b1beb681SJiri Benc 
8583729d502SPatrick McHardy static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
8593729d502SPatrick McHardy 					   const struct ifinfomsg *ifm)
8603729d502SPatrick McHardy {
8613729d502SPatrick McHardy 	unsigned int flags = ifm->ifi_flags;
8623729d502SPatrick McHardy 
8633729d502SPatrick McHardy 	/* bugwards compatibility: ifi_change == 0 is treated as ~0 */
8643729d502SPatrick McHardy 	if (ifm->ifi_change)
8653729d502SPatrick McHardy 		flags = (flags & ifm->ifi_change) |
866b1beb681SJiri Benc 			(rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
8673729d502SPatrick McHardy 
8683729d502SPatrick McHardy 	return flags;
8693729d502SPatrick McHardy }
8703729d502SPatrick McHardy 
871b60c5115SThomas Graf static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
872be1f3c2cSBen Hutchings 				 const struct rtnl_link_stats64 *b)
8731da177e4SLinus Torvalds {
874b60c5115SThomas Graf 	a->rx_packets = b->rx_packets;
875b60c5115SThomas Graf 	a->tx_packets = b->tx_packets;
876b60c5115SThomas Graf 	a->rx_bytes = b->rx_bytes;
877b60c5115SThomas Graf 	a->tx_bytes = b->tx_bytes;
878b60c5115SThomas Graf 	a->rx_errors = b->rx_errors;
879b60c5115SThomas Graf 	a->tx_errors = b->tx_errors;
880b60c5115SThomas Graf 	a->rx_dropped = b->rx_dropped;
881b60c5115SThomas Graf 	a->tx_dropped = b->tx_dropped;
882b60c5115SThomas Graf 
883b60c5115SThomas Graf 	a->multicast = b->multicast;
884b60c5115SThomas Graf 	a->collisions = b->collisions;
885b60c5115SThomas Graf 
886b60c5115SThomas Graf 	a->rx_length_errors = b->rx_length_errors;
887b60c5115SThomas Graf 	a->rx_over_errors = b->rx_over_errors;
888b60c5115SThomas Graf 	a->rx_crc_errors = b->rx_crc_errors;
889b60c5115SThomas Graf 	a->rx_frame_errors = b->rx_frame_errors;
890b60c5115SThomas Graf 	a->rx_fifo_errors = b->rx_fifo_errors;
891b60c5115SThomas Graf 	a->rx_missed_errors = b->rx_missed_errors;
892b60c5115SThomas Graf 
893b60c5115SThomas Graf 	a->tx_aborted_errors = b->tx_aborted_errors;
894b60c5115SThomas Graf 	a->tx_carrier_errors = b->tx_carrier_errors;
895b60c5115SThomas Graf 	a->tx_fifo_errors = b->tx_fifo_errors;
896b60c5115SThomas Graf 	a->tx_heartbeat_errors = b->tx_heartbeat_errors;
897b60c5115SThomas Graf 	a->tx_window_errors = b->tx_window_errors;
898b60c5115SThomas Graf 
899b60c5115SThomas Graf 	a->rx_compressed = b->rx_compressed;
900b60c5115SThomas Graf 	a->tx_compressed = b->tx_compressed;
9016e7333d3SJarod Wilson 
9026e7333d3SJarod Wilson 	a->rx_nohandler = b->rx_nohandler;
90310708f37SJan Engelhardt }
90410708f37SJan Engelhardt 
905c02db8c6SChris Wright /* All VF info */
906115c9b81SGreg Rose static inline int rtnl_vfinfo_size(const struct net_device *dev,
907115c9b81SGreg Rose 				   u32 ext_filter_mask)
908ebc08a6fSWilliams, Mitch A {
9099af15c38SPhil Sutter 	if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) {
910c02db8c6SChris Wright 		int num_vfs = dev_num_vf(dev->dev.parent);
9117e75f74aSSabrina Dubroca 		size_t size = nla_total_size(0);
912045de01aSScott Feldman 		size += num_vfs *
9137e75f74aSSabrina Dubroca 			(nla_total_size(0) +
9147e75f74aSSabrina Dubroca 			 nla_total_size(sizeof(struct ifla_vf_mac)) +
91575345f88SDenis Kirjanov 			 nla_total_size(sizeof(struct ifla_vf_broadcast)) +
9167e75f74aSSabrina Dubroca 			 nla_total_size(sizeof(struct ifla_vf_vlan)) +
9177e75f74aSSabrina Dubroca 			 nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
91879aab093SMoshe Shemesh 			 nla_total_size(MAX_VLAN_LIST_LEN *
91979aab093SMoshe Shemesh 					sizeof(struct ifla_vf_vlan_info)) +
920ed616689SSucheta Chakraborty 			 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
9217e75f74aSSabrina Dubroca 			 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
922945a3676SJiri Benc 			 nla_total_size(sizeof(struct ifla_vf_rate)) +
92301a3d796SVlad Zolotarov 			 nla_total_size(sizeof(struct ifla_vf_link_state)) +
9243b766cd8SEran Ben Elisha 			 nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
9257e75f74aSSabrina Dubroca 			 nla_total_size(0) + /* nest IFLA_VF_STATS */
9263b766cd8SEran Ben Elisha 			 /* IFLA_VF_STATS_RX_PACKETS */
927343a6d8eSNicolas Dichtel 			 nla_total_size_64bit(sizeof(__u64)) +
9283b766cd8SEran Ben Elisha 			 /* IFLA_VF_STATS_TX_PACKETS */
929343a6d8eSNicolas Dichtel 			 nla_total_size_64bit(sizeof(__u64)) +
9303b766cd8SEran Ben Elisha 			 /* IFLA_VF_STATS_RX_BYTES */
931343a6d8eSNicolas Dichtel 			 nla_total_size_64bit(sizeof(__u64)) +
9323b766cd8SEran Ben Elisha 			 /* IFLA_VF_STATS_TX_BYTES */
933343a6d8eSNicolas Dichtel 			 nla_total_size_64bit(sizeof(__u64)) +
9343b766cd8SEran Ben Elisha 			 /* IFLA_VF_STATS_BROADCAST */
935343a6d8eSNicolas Dichtel 			 nla_total_size_64bit(sizeof(__u64)) +
9363b766cd8SEran Ben Elisha 			 /* IFLA_VF_STATS_MULTICAST */
937343a6d8eSNicolas Dichtel 			 nla_total_size_64bit(sizeof(__u64)) +
938c5a9f6f0SEugenia Emantayev 			 /* IFLA_VF_STATS_RX_DROPPED */
939c5a9f6f0SEugenia Emantayev 			 nla_total_size_64bit(sizeof(__u64)) +
940c5a9f6f0SEugenia Emantayev 			 /* IFLA_VF_STATS_TX_DROPPED */
941c5a9f6f0SEugenia Emantayev 			 nla_total_size_64bit(sizeof(__u64)) +
942dd461d6aSHiroshi Shimamoto 			 nla_total_size(sizeof(struct ifla_vf_trust)));
943c02db8c6SChris Wright 		return size;
944c02db8c6SChris Wright 	} else
945ebc08a6fSWilliams, Mitch A 		return 0;
946ebc08a6fSWilliams, Mitch A }
947ebc08a6fSWilliams, Mitch A 
948c53864fdSDavid Gibson static size_t rtnl_port_size(const struct net_device *dev,
949c53864fdSDavid Gibson 			     u32 ext_filter_mask)
95057b61080SScott Feldman {
95157b61080SScott Feldman 	size_t port_size = nla_total_size(4)		/* PORT_VF */
95257b61080SScott Feldman 		+ nla_total_size(PORT_PROFILE_MAX)	/* PORT_PROFILE */
95357b61080SScott Feldman 		+ nla_total_size(PORT_UUID_MAX)		/* PORT_INSTANCE_UUID */
95457b61080SScott Feldman 		+ nla_total_size(PORT_UUID_MAX)		/* PORT_HOST_UUID */
95557b61080SScott Feldman 		+ nla_total_size(1)			/* PROT_VDP_REQUEST */
95657b61080SScott Feldman 		+ nla_total_size(2);			/* PORT_VDP_RESPONSE */
95757b61080SScott Feldman 	size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
95857b61080SScott Feldman 	size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
95957b61080SScott Feldman 		+ port_size;
96057b61080SScott Feldman 	size_t port_self_size = nla_total_size(sizeof(struct nlattr))
96157b61080SScott Feldman 		+ port_size;
96257b61080SScott Feldman 
963c53864fdSDavid Gibson 	if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
964c53864fdSDavid Gibson 	    !(ext_filter_mask & RTEXT_FILTER_VF))
96557b61080SScott Feldman 		return 0;
96657b61080SScott Feldman 	if (dev_num_vf(dev->dev.parent))
96757b61080SScott Feldman 		return port_self_size + vf_ports_size +
96857b61080SScott Feldman 			vf_port_size * dev_num_vf(dev->dev.parent);
96957b61080SScott Feldman 	else
97057b61080SScott Feldman 		return port_self_size;
97157b61080SScott Feldman }
97257b61080SScott Feldman 
973b5cdae32SDavid S. Miller static size_t rtnl_xdp_size(void)
974d1fdd913SBrenden Blanco {
975b3cfaa31SSabrina Dubroca 	size_t xdp_size = nla_total_size(0) +	/* nest IFLA_XDP */
97658038695SMartin KaFai Lau 			  nla_total_size(1) +	/* XDP_ATTACHED */
977a25717d2SJakub Kicinski 			  nla_total_size(4) +	/* XDP_PROG_ID (or 1st mode) */
9784f91da26SJakub Kicinski 			  nla_total_size(4);	/* XDP_<mode>_PROG_ID */
979d1fdd913SBrenden Blanco 
980d1fdd913SBrenden Blanco 	return xdp_size;
981d1fdd913SBrenden Blanco }
982d1fdd913SBrenden Blanco 
98388f4fb0cSJiri Pirko static size_t rtnl_prop_list_size(const struct net_device *dev)
98488f4fb0cSJiri Pirko {
98588f4fb0cSJiri Pirko 	struct netdev_name_node *name_node;
98688f4fb0cSJiri Pirko 	size_t size;
98788f4fb0cSJiri Pirko 
98888f4fb0cSJiri Pirko 	if (list_empty(&dev->name_node->list))
98988f4fb0cSJiri Pirko 		return 0;
99088f4fb0cSJiri Pirko 	size = nla_total_size(0);
99188f4fb0cSJiri Pirko 	list_for_each_entry(name_node, &dev->name_node->list, list)
99288f4fb0cSJiri Pirko 		size += nla_total_size(ALTIFNAMSIZ);
99388f4fb0cSJiri Pirko 	return size;
99488f4fb0cSJiri Pirko }
99588f4fb0cSJiri Pirko 
996115c9b81SGreg Rose static noinline size_t if_nlmsg_size(const struct net_device *dev,
997115c9b81SGreg Rose 				     u32 ext_filter_mask)
998339bf98fSThomas Graf {
999339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
1000339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
10010b815a1aSStephen Hemminger 	       + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
1002339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
1003270cb4d0SNicolas Dichtel 	       + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap))
1004339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rtnl_link_stats))
100535c58459SDavid S. Miller 	       + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
1006339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
1007339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
1008339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_TXQLEN */
1009339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_WEIGHT */
1010339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MTU */
1011339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_LINK */
1012339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MASTER */
10139a57247fSJiri Pirko 	       + nla_total_size(1) /* IFLA_CARRIER */
1014edbc0bb3SBen Greear 	       + nla_total_size(4) /* IFLA_PROMISCUITY */
101576ff5cc9SJiri Pirko 	       + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
101676ff5cc9SJiri Pirko 	       + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
10176919756cSTobias Klauser 	       + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
10186919756cSTobias Klauser 	       + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
1019339bf98fSThomas Graf 	       + nla_total_size(1) /* IFLA_OPERSTATE */
102038f7b870SPatrick McHardy 	       + nla_total_size(1) /* IFLA_LINKMODE */
10212d3b479dSdavid decotigny 	       + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
1022d37512a2SNicolas Dichtel 	       + nla_total_size(4) /* IFLA_LINK_NETNSID */
1023db833d40SSerhey Popovych 	       + nla_total_size(4) /* IFLA_GROUP */
1024115c9b81SGreg Rose 	       + nla_total_size(ext_filter_mask
1025115c9b81SGreg Rose 			        & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
1026115c9b81SGreg Rose 	       + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
1027c53864fdSDavid Gibson 	       + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
1028f8ff182cSThomas Graf 	       + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
1029b1974ed0SArad, Ronen 	       + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
103082f28412SJiri Pirko 	       + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
103188d6378bSAnuradha Karuppiah 	       + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
1032c57c7a95SNicolas Dichtel 	       + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
1033b5cdae32SDavid S. Miller 	       + rtnl_xdp_size() /* IFLA_XDP */
10343d3ea5afSVlad Yasevich 	       + nla_total_size(4)  /* IFLA_EVENT */
10356621dd29SNicolas Dichtel 	       + nla_total_size(4)  /* IFLA_NEW_NETNSID */
103638e01b30SNicolas Dichtel 	       + nla_total_size(4)  /* IFLA_NEW_IFINDEX */
103703ac738dSColin Ian King 	       + nla_total_size(1)  /* IFLA_PROTO_DOWN */
10387e4a8d5aSChristian Brauner 	       + nla_total_size(4)  /* IFLA_TARGET_NETNSID */
1039b2d3bcfaSDavid Decotigny 	       + nla_total_size(4)  /* IFLA_CARRIER_UP_COUNT */
1040b2d3bcfaSDavid Decotigny 	       + nla_total_size(4)  /* IFLA_CARRIER_DOWN_COUNT */
10413e7a50ceSStephen Hemminger 	       + nla_total_size(4)  /* IFLA_MIN_MTU */
10423e7a50ceSStephen Hemminger 	       + nla_total_size(4)  /* IFLA_MAX_MTU */
104388f4fb0cSJiri Pirko 	       + rtnl_prop_list_size(dev)
1044*f74877a5SMichal Kubecek 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_PERM_ADDRESS */
104579e1ad14SJiri Benc 	       + 0;
1046339bf98fSThomas Graf }
1047339bf98fSThomas Graf 
104857b61080SScott Feldman static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
104957b61080SScott Feldman {
105057b61080SScott Feldman 	struct nlattr *vf_ports;
105157b61080SScott Feldman 	struct nlattr *vf_port;
105257b61080SScott Feldman 	int vf;
105357b61080SScott Feldman 	int err;
105457b61080SScott Feldman 
1055ae0be8deSMichal Kubecek 	vf_ports = nla_nest_start_noflag(skb, IFLA_VF_PORTS);
105657b61080SScott Feldman 	if (!vf_ports)
105757b61080SScott Feldman 		return -EMSGSIZE;
105857b61080SScott Feldman 
105957b61080SScott Feldman 	for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
1060ae0be8deSMichal Kubecek 		vf_port = nla_nest_start_noflag(skb, IFLA_VF_PORT);
10618ca94183SScott Feldman 		if (!vf_port)
10628ca94183SScott Feldman 			goto nla_put_failure;
1063a6574349SDavid S. Miller 		if (nla_put_u32(skb, IFLA_PORT_VF, vf))
1064a6574349SDavid S. Miller 			goto nla_put_failure;
106557b61080SScott Feldman 		err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
10668ca94183SScott Feldman 		if (err == -EMSGSIZE)
10678ca94183SScott Feldman 			goto nla_put_failure;
106857b61080SScott Feldman 		if (err) {
106957b61080SScott Feldman 			nla_nest_cancel(skb, vf_port);
107057b61080SScott Feldman 			continue;
107157b61080SScott Feldman 		}
107257b61080SScott Feldman 		nla_nest_end(skb, vf_port);
107357b61080SScott Feldman 	}
107457b61080SScott Feldman 
107557b61080SScott Feldman 	nla_nest_end(skb, vf_ports);
107657b61080SScott Feldman 
107757b61080SScott Feldman 	return 0;
10788ca94183SScott Feldman 
10798ca94183SScott Feldman nla_put_failure:
10808ca94183SScott Feldman 	nla_nest_cancel(skb, vf_ports);
10818ca94183SScott Feldman 	return -EMSGSIZE;
108257b61080SScott Feldman }
108357b61080SScott Feldman 
108457b61080SScott Feldman static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
108557b61080SScott Feldman {
108657b61080SScott Feldman 	struct nlattr *port_self;
108757b61080SScott Feldman 	int err;
108857b61080SScott Feldman 
1089ae0be8deSMichal Kubecek 	port_self = nla_nest_start_noflag(skb, IFLA_PORT_SELF);
109057b61080SScott Feldman 	if (!port_self)
109157b61080SScott Feldman 		return -EMSGSIZE;
109257b61080SScott Feldman 
109357b61080SScott Feldman 	err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
109457b61080SScott Feldman 	if (err) {
109557b61080SScott Feldman 		nla_nest_cancel(skb, port_self);
10968ca94183SScott Feldman 		return (err == -EMSGSIZE) ? err : 0;
109757b61080SScott Feldman 	}
109857b61080SScott Feldman 
109957b61080SScott Feldman 	nla_nest_end(skb, port_self);
110057b61080SScott Feldman 
110157b61080SScott Feldman 	return 0;
110257b61080SScott Feldman }
110357b61080SScott Feldman 
1104c53864fdSDavid Gibson static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
1105c53864fdSDavid Gibson 			  u32 ext_filter_mask)
110657b61080SScott Feldman {
110757b61080SScott Feldman 	int err;
110857b61080SScott Feldman 
1109c53864fdSDavid Gibson 	if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
1110c53864fdSDavid Gibson 	    !(ext_filter_mask & RTEXT_FILTER_VF))
111157b61080SScott Feldman 		return 0;
111257b61080SScott Feldman 
111357b61080SScott Feldman 	err = rtnl_port_self_fill(skb, dev);
111457b61080SScott Feldman 	if (err)
111557b61080SScott Feldman 		return err;
111657b61080SScott Feldman 
111757b61080SScott Feldman 	if (dev_num_vf(dev->dev.parent)) {
111857b61080SScott Feldman 		err = rtnl_vf_ports_fill(skb, dev);
111957b61080SScott Feldman 		if (err)
112057b61080SScott Feldman 			return err;
112157b61080SScott Feldman 	}
112257b61080SScott Feldman 
112357b61080SScott Feldman 	return 0;
112457b61080SScott Feldman }
112557b61080SScott Feldman 
112666cae9edSJiri Pirko static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
112766cae9edSJiri Pirko {
112866cae9edSJiri Pirko 	int err;
112902637fceSJiri Pirko 	struct netdev_phys_item_id ppid;
113066cae9edSJiri Pirko 
113166cae9edSJiri Pirko 	err = dev_get_phys_port_id(dev, &ppid);
113266cae9edSJiri Pirko 	if (err) {
113366cae9edSJiri Pirko 		if (err == -EOPNOTSUPP)
113466cae9edSJiri Pirko 			return 0;
113566cae9edSJiri Pirko 		return err;
113666cae9edSJiri Pirko 	}
113766cae9edSJiri Pirko 
113866cae9edSJiri Pirko 	if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
113966cae9edSJiri Pirko 		return -EMSGSIZE;
114066cae9edSJiri Pirko 
114166cae9edSJiri Pirko 	return 0;
114266cae9edSJiri Pirko }
114366cae9edSJiri Pirko 
1144db24a904SDavid Ahern static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1145db24a904SDavid Ahern {
1146db24a904SDavid Ahern 	char name[IFNAMSIZ];
1147db24a904SDavid Ahern 	int err;
1148db24a904SDavid Ahern 
1149db24a904SDavid Ahern 	err = dev_get_phys_port_name(dev, name, sizeof(name));
1150db24a904SDavid Ahern 	if (err) {
1151db24a904SDavid Ahern 		if (err == -EOPNOTSUPP)
1152db24a904SDavid Ahern 			return 0;
1153db24a904SDavid Ahern 		return err;
1154db24a904SDavid Ahern 	}
1155db24a904SDavid Ahern 
115677ef033bSMichal Schmidt 	if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
1157db24a904SDavid Ahern 		return -EMSGSIZE;
1158db24a904SDavid Ahern 
1159db24a904SDavid Ahern 	return 0;
1160db24a904SDavid Ahern }
1161db24a904SDavid Ahern 
116282f28412SJiri Pirko static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
116382f28412SJiri Pirko {
1164bccb3025SFlorian Fainelli 	struct netdev_phys_item_id ppid = { };
116582f28412SJiri Pirko 	int err;
116682f28412SJiri Pirko 
1167bccb3025SFlorian Fainelli 	err = dev_get_port_parent_id(dev, &ppid, false);
116882f28412SJiri Pirko 	if (err) {
116982f28412SJiri Pirko 		if (err == -EOPNOTSUPP)
117082f28412SJiri Pirko 			return 0;
117182f28412SJiri Pirko 		return err;
117282f28412SJiri Pirko 	}
117382f28412SJiri Pirko 
1174bccb3025SFlorian Fainelli 	if (nla_put(skb, IFLA_PHYS_SWITCH_ID, ppid.id_len, ppid.id))
117582f28412SJiri Pirko 		return -EMSGSIZE;
117682f28412SJiri Pirko 
117782f28412SJiri Pirko 	return 0;
117882f28412SJiri Pirko }
117982f28412SJiri Pirko 
1180b22b941bSHannes Frederic Sowa static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1181b22b941bSHannes Frederic Sowa 					      struct net_device *dev)
1182b22b941bSHannes Frederic Sowa {
1183550bce59SRoopa Prabhu 	struct rtnl_link_stats64 *sp;
1184b22b941bSHannes Frederic Sowa 	struct nlattr *attr;
1185b22b941bSHannes Frederic Sowa 
118658414d32SNicolas Dichtel 	attr = nla_reserve_64bit(skb, IFLA_STATS64,
118758414d32SNicolas Dichtel 				 sizeof(struct rtnl_link_stats64), IFLA_PAD);
1188b22b941bSHannes Frederic Sowa 	if (!attr)
1189b22b941bSHannes Frederic Sowa 		return -EMSGSIZE;
1190b22b941bSHannes Frederic Sowa 
1191550bce59SRoopa Prabhu 	sp = nla_data(attr);
1192550bce59SRoopa Prabhu 	dev_get_stats(dev, sp);
1193550bce59SRoopa Prabhu 
1194550bce59SRoopa Prabhu 	attr = nla_reserve(skb, IFLA_STATS,
1195550bce59SRoopa Prabhu 			   sizeof(struct rtnl_link_stats));
1196550bce59SRoopa Prabhu 	if (!attr)
1197550bce59SRoopa Prabhu 		return -EMSGSIZE;
1198550bce59SRoopa Prabhu 
1199550bce59SRoopa Prabhu 	copy_rtnl_link_stats(nla_data(attr), sp);
1200b22b941bSHannes Frederic Sowa 
1201b22b941bSHannes Frederic Sowa 	return 0;
1202b22b941bSHannes Frederic Sowa }
1203b22b941bSHannes Frederic Sowa 
1204b22b941bSHannes Frederic Sowa static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1205b22b941bSHannes Frederic Sowa 					       struct net_device *dev,
1206b22b941bSHannes Frederic Sowa 					       int vfs_num,
1207b22b941bSHannes Frederic Sowa 					       struct nlattr *vfinfo)
1208b22b941bSHannes Frederic Sowa {
1209b22b941bSHannes Frederic Sowa 	struct ifla_vf_rss_query_en vf_rss_query_en;
121079aab093SMoshe Shemesh 	struct nlattr *vf, *vfstats, *vfvlanlist;
1211b22b941bSHannes Frederic Sowa 	struct ifla_vf_link_state vf_linkstate;
121279aab093SMoshe Shemesh 	struct ifla_vf_vlan_info vf_vlan_info;
1213b22b941bSHannes Frederic Sowa 	struct ifla_vf_spoofchk vf_spoofchk;
1214b22b941bSHannes Frederic Sowa 	struct ifla_vf_tx_rate vf_tx_rate;
1215b22b941bSHannes Frederic Sowa 	struct ifla_vf_stats vf_stats;
1216b22b941bSHannes Frederic Sowa 	struct ifla_vf_trust vf_trust;
1217b22b941bSHannes Frederic Sowa 	struct ifla_vf_vlan vf_vlan;
1218b22b941bSHannes Frederic Sowa 	struct ifla_vf_rate vf_rate;
1219b22b941bSHannes Frederic Sowa 	struct ifla_vf_mac vf_mac;
122075345f88SDenis Kirjanov 	struct ifla_vf_broadcast vf_broadcast;
1221b22b941bSHannes Frederic Sowa 	struct ifla_vf_info ivi;
122230aad417SDanit Goldberg 	struct ifla_vf_guid node_guid;
122330aad417SDanit Goldberg 	struct ifla_vf_guid port_guid;
1224b22b941bSHannes Frederic Sowa 
12250eed9cf5SMintz, Yuval 	memset(&ivi, 0, sizeof(ivi));
12260eed9cf5SMintz, Yuval 
1227b22b941bSHannes Frederic Sowa 	/* Not all SR-IOV capable drivers support the
1228b22b941bSHannes Frederic Sowa 	 * spoofcheck and "RSS query enable" query.  Preset to
1229b22b941bSHannes Frederic Sowa 	 * -1 so the user space tool can detect that the driver
1230b22b941bSHannes Frederic Sowa 	 * didn't report anything.
1231b22b941bSHannes Frederic Sowa 	 */
1232b22b941bSHannes Frederic Sowa 	ivi.spoofchk = -1;
1233b22b941bSHannes Frederic Sowa 	ivi.rss_query_en = -1;
1234b22b941bSHannes Frederic Sowa 	ivi.trusted = -1;
1235b22b941bSHannes Frederic Sowa 	/* The default value for VF link state is "auto"
1236b22b941bSHannes Frederic Sowa 	 * IFLA_VF_LINK_STATE_AUTO which equals zero
1237b22b941bSHannes Frederic Sowa 	 */
1238b22b941bSHannes Frederic Sowa 	ivi.linkstate = 0;
123979aab093SMoshe Shemesh 	/* VLAN Protocol by default is 802.1Q */
124079aab093SMoshe Shemesh 	ivi.vlan_proto = htons(ETH_P_8021Q);
1241b22b941bSHannes Frederic Sowa 	if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1242b22b941bSHannes Frederic Sowa 		return 0;
1243b22b941bSHannes Frederic Sowa 
1244775f4f05SDan Carpenter 	memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
1245775f4f05SDan Carpenter 
1246b22b941bSHannes Frederic Sowa 	vf_mac.vf =
1247b22b941bSHannes Frederic Sowa 		vf_vlan.vf =
124879aab093SMoshe Shemesh 		vf_vlan_info.vf =
1249b22b941bSHannes Frederic Sowa 		vf_rate.vf =
1250b22b941bSHannes Frederic Sowa 		vf_tx_rate.vf =
1251b22b941bSHannes Frederic Sowa 		vf_spoofchk.vf =
1252b22b941bSHannes Frederic Sowa 		vf_linkstate.vf =
1253b22b941bSHannes Frederic Sowa 		vf_rss_query_en.vf =
12549aed6ae0SDanit Goldberg 		vf_trust.vf =
12559aed6ae0SDanit Goldberg 		node_guid.vf =
12569aed6ae0SDanit Goldberg 		port_guid.vf = ivi.vf;
1257b22b941bSHannes Frederic Sowa 
1258b22b941bSHannes Frederic Sowa 	memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
125975345f88SDenis Kirjanov 	memcpy(vf_broadcast.broadcast, dev->broadcast, dev->addr_len);
1260b22b941bSHannes Frederic Sowa 	vf_vlan.vlan = ivi.vlan;
1261b22b941bSHannes Frederic Sowa 	vf_vlan.qos = ivi.qos;
126279aab093SMoshe Shemesh 	vf_vlan_info.vlan = ivi.vlan;
126379aab093SMoshe Shemesh 	vf_vlan_info.qos = ivi.qos;
126479aab093SMoshe Shemesh 	vf_vlan_info.vlan_proto = ivi.vlan_proto;
1265b22b941bSHannes Frederic Sowa 	vf_tx_rate.rate = ivi.max_tx_rate;
1266b22b941bSHannes Frederic Sowa 	vf_rate.min_tx_rate = ivi.min_tx_rate;
1267b22b941bSHannes Frederic Sowa 	vf_rate.max_tx_rate = ivi.max_tx_rate;
1268b22b941bSHannes Frederic Sowa 	vf_spoofchk.setting = ivi.spoofchk;
1269b22b941bSHannes Frederic Sowa 	vf_linkstate.link_state = ivi.linkstate;
1270b22b941bSHannes Frederic Sowa 	vf_rss_query_en.setting = ivi.rss_query_en;
1271b22b941bSHannes Frederic Sowa 	vf_trust.setting = ivi.trusted;
1272ae0be8deSMichal Kubecek 	vf = nla_nest_start_noflag(skb, IFLA_VF_INFO);
127379aab093SMoshe Shemesh 	if (!vf)
127479aab093SMoshe Shemesh 		goto nla_put_vfinfo_failure;
1275b22b941bSHannes Frederic Sowa 	if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
127675345f88SDenis Kirjanov 	    nla_put(skb, IFLA_VF_BROADCAST, sizeof(vf_broadcast), &vf_broadcast) ||
1277b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1278b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1279b22b941bSHannes Frederic Sowa 		    &vf_rate) ||
1280b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1281b22b941bSHannes Frederic Sowa 		    &vf_tx_rate) ||
1282b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1283b22b941bSHannes Frederic Sowa 		    &vf_spoofchk) ||
1284b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1285b22b941bSHannes Frederic Sowa 		    &vf_linkstate) ||
1286b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1287b22b941bSHannes Frederic Sowa 		    sizeof(vf_rss_query_en),
1288b22b941bSHannes Frederic Sowa 		    &vf_rss_query_en) ||
1289b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_TRUST,
1290b22b941bSHannes Frederic Sowa 		    sizeof(vf_trust), &vf_trust))
129179aab093SMoshe Shemesh 		goto nla_put_vf_failure;
129230aad417SDanit Goldberg 
129330aad417SDanit Goldberg 	memset(&node_guid, 0, sizeof(node_guid));
129430aad417SDanit Goldberg 	memset(&port_guid, 0, sizeof(port_guid));
129530aad417SDanit Goldberg 	if (dev->netdev_ops->ndo_get_vf_guid &&
129630aad417SDanit Goldberg 	    !dev->netdev_ops->ndo_get_vf_guid(dev, vfs_num, &node_guid,
129730aad417SDanit Goldberg 					      &port_guid)) {
129830aad417SDanit Goldberg 		if (nla_put(skb, IFLA_VF_IB_NODE_GUID, sizeof(node_guid),
129930aad417SDanit Goldberg 			    &node_guid) ||
130030aad417SDanit Goldberg 		    nla_put(skb, IFLA_VF_IB_PORT_GUID, sizeof(port_guid),
130130aad417SDanit Goldberg 			    &port_guid))
130230aad417SDanit Goldberg 			goto nla_put_vf_failure;
130330aad417SDanit Goldberg 	}
1304ae0be8deSMichal Kubecek 	vfvlanlist = nla_nest_start_noflag(skb, IFLA_VF_VLAN_LIST);
130579aab093SMoshe Shemesh 	if (!vfvlanlist)
130679aab093SMoshe Shemesh 		goto nla_put_vf_failure;
130779aab093SMoshe Shemesh 	if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
130879aab093SMoshe Shemesh 		    &vf_vlan_info)) {
130979aab093SMoshe Shemesh 		nla_nest_cancel(skb, vfvlanlist);
131079aab093SMoshe Shemesh 		goto nla_put_vf_failure;
131179aab093SMoshe Shemesh 	}
131279aab093SMoshe Shemesh 	nla_nest_end(skb, vfvlanlist);
1313b22b941bSHannes Frederic Sowa 	memset(&vf_stats, 0, sizeof(vf_stats));
1314b22b941bSHannes Frederic Sowa 	if (dev->netdev_ops->ndo_get_vf_stats)
1315b22b941bSHannes Frederic Sowa 		dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1316b22b941bSHannes Frederic Sowa 						&vf_stats);
1317ae0be8deSMichal Kubecek 	vfstats = nla_nest_start_noflag(skb, IFLA_VF_STATS);
131879aab093SMoshe Shemesh 	if (!vfstats)
131979aab093SMoshe Shemesh 		goto nla_put_vf_failure;
1320343a6d8eSNicolas Dichtel 	if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1321343a6d8eSNicolas Dichtel 			      vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1322343a6d8eSNicolas Dichtel 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1323343a6d8eSNicolas Dichtel 			      vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1324343a6d8eSNicolas Dichtel 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1325343a6d8eSNicolas Dichtel 			      vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1326343a6d8eSNicolas Dichtel 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1327343a6d8eSNicolas Dichtel 			      vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1328343a6d8eSNicolas Dichtel 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1329343a6d8eSNicolas Dichtel 			      vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1330343a6d8eSNicolas Dichtel 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
1331c5a9f6f0SEugenia Emantayev 			      vf_stats.multicast, IFLA_VF_STATS_PAD) ||
1332c5a9f6f0SEugenia Emantayev 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED,
1333c5a9f6f0SEugenia Emantayev 			      vf_stats.rx_dropped, IFLA_VF_STATS_PAD) ||
1334c5a9f6f0SEugenia Emantayev 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED,
1335c5a9f6f0SEugenia Emantayev 			      vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) {
133679aab093SMoshe Shemesh 		nla_nest_cancel(skb, vfstats);
133779aab093SMoshe Shemesh 		goto nla_put_vf_failure;
133879aab093SMoshe Shemesh 	}
1339b22b941bSHannes Frederic Sowa 	nla_nest_end(skb, vfstats);
1340b22b941bSHannes Frederic Sowa 	nla_nest_end(skb, vf);
1341b22b941bSHannes Frederic Sowa 	return 0;
134279aab093SMoshe Shemesh 
134379aab093SMoshe Shemesh nla_put_vf_failure:
134479aab093SMoshe Shemesh 	nla_nest_cancel(skb, vf);
134579aab093SMoshe Shemesh nla_put_vfinfo_failure:
134679aab093SMoshe Shemesh 	nla_nest_cancel(skb, vfinfo);
134779aab093SMoshe Shemesh 	return -EMSGSIZE;
1348b22b941bSHannes Frederic Sowa }
1349b22b941bSHannes Frederic Sowa 
1350250fc3dfSFlorian Westphal static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb,
1351250fc3dfSFlorian Westphal 					   struct net_device *dev,
1352250fc3dfSFlorian Westphal 					   u32 ext_filter_mask)
1353250fc3dfSFlorian Westphal {
1354250fc3dfSFlorian Westphal 	struct nlattr *vfinfo;
1355250fc3dfSFlorian Westphal 	int i, num_vfs;
1356250fc3dfSFlorian Westphal 
1357250fc3dfSFlorian Westphal 	if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0))
1358250fc3dfSFlorian Westphal 		return 0;
1359250fc3dfSFlorian Westphal 
1360250fc3dfSFlorian Westphal 	num_vfs = dev_num_vf(dev->dev.parent);
1361250fc3dfSFlorian Westphal 	if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs))
1362250fc3dfSFlorian Westphal 		return -EMSGSIZE;
1363250fc3dfSFlorian Westphal 
1364250fc3dfSFlorian Westphal 	if (!dev->netdev_ops->ndo_get_vf_config)
1365250fc3dfSFlorian Westphal 		return 0;
1366250fc3dfSFlorian Westphal 
1367ae0be8deSMichal Kubecek 	vfinfo = nla_nest_start_noflag(skb, IFLA_VFINFO_LIST);
1368250fc3dfSFlorian Westphal 	if (!vfinfo)
1369250fc3dfSFlorian Westphal 		return -EMSGSIZE;
1370250fc3dfSFlorian Westphal 
1371250fc3dfSFlorian Westphal 	for (i = 0; i < num_vfs; i++) {
1372250fc3dfSFlorian Westphal 		if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
1373250fc3dfSFlorian Westphal 			return -EMSGSIZE;
1374250fc3dfSFlorian Westphal 	}
1375250fc3dfSFlorian Westphal 
1376250fc3dfSFlorian Westphal 	nla_nest_end(skb, vfinfo);
1377250fc3dfSFlorian Westphal 	return 0;
1378250fc3dfSFlorian Westphal }
1379250fc3dfSFlorian Westphal 
1380b22b941bSHannes Frederic Sowa static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1381b22b941bSHannes Frederic Sowa {
13825f8e4474SKangjie Lu 	struct rtnl_link_ifmap map;
13835f8e4474SKangjie Lu 
13845f8e4474SKangjie Lu 	memset(&map, 0, sizeof(map));
13855f8e4474SKangjie Lu 	map.mem_start   = dev->mem_start;
13865f8e4474SKangjie Lu 	map.mem_end     = dev->mem_end;
13875f8e4474SKangjie Lu 	map.base_addr   = dev->base_addr;
13885f8e4474SKangjie Lu 	map.irq         = dev->irq;
13895f8e4474SKangjie Lu 	map.dma         = dev->dma;
13905f8e4474SKangjie Lu 	map.port        = dev->if_port;
13915f8e4474SKangjie Lu 
1392270cb4d0SNicolas Dichtel 	if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
1393b22b941bSHannes Frederic Sowa 		return -EMSGSIZE;
1394b22b941bSHannes Frederic Sowa 
1395b22b941bSHannes Frederic Sowa 	return 0;
1396b22b941bSHannes Frederic Sowa }
1397b22b941bSHannes Frederic Sowa 
1398a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_skb(struct net_device *dev)
1399d67b9cd2SDaniel Borkmann {
140058038695SMartin KaFai Lau 	const struct bpf_prog *generic_xdp_prog;
1401d67b9cd2SDaniel Borkmann 
1402d67b9cd2SDaniel Borkmann 	ASSERT_RTNL();
1403d67b9cd2SDaniel Borkmann 
140458038695SMartin KaFai Lau 	generic_xdp_prog = rtnl_dereference(dev->xdp_prog);
1405a25717d2SJakub Kicinski 	if (!generic_xdp_prog)
1406a25717d2SJakub Kicinski 		return 0;
1407a25717d2SJakub Kicinski 	return generic_xdp_prog->aux->id;
140858038695SMartin KaFai Lau }
1409ce158e58SJakub Kicinski 
1410a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_drv(struct net_device *dev)
1411a25717d2SJakub Kicinski {
1412a25717d2SJakub Kicinski 	return __dev_xdp_query(dev, dev->netdev_ops->ndo_bpf, XDP_QUERY_PROG);
1413a25717d2SJakub Kicinski }
1414118b4aa2SJakub Kicinski 
1415a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_hw(struct net_device *dev)
1416a25717d2SJakub Kicinski {
1417a25717d2SJakub Kicinski 	return __dev_xdp_query(dev, dev->netdev_ops->ndo_bpf,
1418a25717d2SJakub Kicinski 			       XDP_QUERY_PROG_HW);
1419a25717d2SJakub Kicinski }
1420a25717d2SJakub Kicinski 
1421a25717d2SJakub Kicinski static int rtnl_xdp_report_one(struct sk_buff *skb, struct net_device *dev,
1422a25717d2SJakub Kicinski 			       u32 *prog_id, u8 *mode, u8 tgt_mode, u32 attr,
1423a25717d2SJakub Kicinski 			       u32 (*get_prog_id)(struct net_device *dev))
1424a25717d2SJakub Kicinski {
1425a25717d2SJakub Kicinski 	u32 curr_id;
1426a25717d2SJakub Kicinski 	int err;
1427a25717d2SJakub Kicinski 
1428a25717d2SJakub Kicinski 	curr_id = get_prog_id(dev);
1429a25717d2SJakub Kicinski 	if (!curr_id)
1430a25717d2SJakub Kicinski 		return 0;
1431a25717d2SJakub Kicinski 
1432a25717d2SJakub Kicinski 	*prog_id = curr_id;
1433a25717d2SJakub Kicinski 	err = nla_put_u32(skb, attr, curr_id);
1434a25717d2SJakub Kicinski 	if (err)
1435a25717d2SJakub Kicinski 		return err;
1436a25717d2SJakub Kicinski 
1437a25717d2SJakub Kicinski 	if (*mode != XDP_ATTACHED_NONE)
1438a25717d2SJakub Kicinski 		*mode = XDP_ATTACHED_MULTI;
1439a25717d2SJakub Kicinski 	else
1440a25717d2SJakub Kicinski 		*mode = tgt_mode;
1441a25717d2SJakub Kicinski 
1442a25717d2SJakub Kicinski 	return 0;
1443d67b9cd2SDaniel Borkmann }
1444d67b9cd2SDaniel Borkmann 
1445d1fdd913SBrenden Blanco static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1446d1fdd913SBrenden Blanco {
1447d1fdd913SBrenden Blanco 	struct nlattr *xdp;
144858038695SMartin KaFai Lau 	u32 prog_id;
1449d1fdd913SBrenden Blanco 	int err;
14504f91da26SJakub Kicinski 	u8 mode;
1451d1fdd913SBrenden Blanco 
1452ae0be8deSMichal Kubecek 	xdp = nla_nest_start_noflag(skb, IFLA_XDP);
1453d1fdd913SBrenden Blanco 	if (!xdp)
1454d1fdd913SBrenden Blanco 		return -EMSGSIZE;
1455b5cdae32SDavid S. Miller 
1456a25717d2SJakub Kicinski 	prog_id = 0;
1457a25717d2SJakub Kicinski 	mode = XDP_ATTACHED_NONE;
1458202aabe8SJakub Kicinski 	err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_SKB,
1459202aabe8SJakub Kicinski 				  IFLA_XDP_SKB_PROG_ID, rtnl_xdp_prog_skb);
1460202aabe8SJakub Kicinski 	if (err)
1461a25717d2SJakub Kicinski 		goto err_cancel;
1462202aabe8SJakub Kicinski 	err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_DRV,
1463202aabe8SJakub Kicinski 				  IFLA_XDP_DRV_PROG_ID, rtnl_xdp_prog_drv);
1464202aabe8SJakub Kicinski 	if (err)
1465a25717d2SJakub Kicinski 		goto err_cancel;
1466202aabe8SJakub Kicinski 	err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_HW,
1467202aabe8SJakub Kicinski 				  IFLA_XDP_HW_PROG_ID, rtnl_xdp_prog_hw);
1468202aabe8SJakub Kicinski 	if (err)
1469a25717d2SJakub Kicinski 		goto err_cancel;
1470a25717d2SJakub Kicinski 
14714f91da26SJakub Kicinski 	err = nla_put_u8(skb, IFLA_XDP_ATTACHED, mode);
1472d1fdd913SBrenden Blanco 	if (err)
1473d1fdd913SBrenden Blanco 		goto err_cancel;
1474d1fdd913SBrenden Blanco 
1475a25717d2SJakub Kicinski 	if (prog_id && mode != XDP_ATTACHED_MULTI) {
147658038695SMartin KaFai Lau 		err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id);
147758038695SMartin KaFai Lau 		if (err)
147858038695SMartin KaFai Lau 			goto err_cancel;
147958038695SMartin KaFai Lau 	}
148058038695SMartin KaFai Lau 
1481d1fdd913SBrenden Blanco 	nla_nest_end(skb, xdp);
1482d1fdd913SBrenden Blanco 	return 0;
1483d1fdd913SBrenden Blanco 
1484d1fdd913SBrenden Blanco err_cancel:
1485d1fdd913SBrenden Blanco 	nla_nest_cancel(skb, xdp);
1486d1fdd913SBrenden Blanco 	return err;
1487d1fdd913SBrenden Blanco }
1488d1fdd913SBrenden Blanco 
14893d3ea5afSVlad Yasevich static u32 rtnl_get_event(unsigned long event)
14903d3ea5afSVlad Yasevich {
14913d3ea5afSVlad Yasevich 	u32 rtnl_event_type = IFLA_EVENT_NONE;
14923d3ea5afSVlad Yasevich 
14933d3ea5afSVlad Yasevich 	switch (event) {
14943d3ea5afSVlad Yasevich 	case NETDEV_REBOOT:
14953d3ea5afSVlad Yasevich 		rtnl_event_type = IFLA_EVENT_REBOOT;
14963d3ea5afSVlad Yasevich 		break;
14973d3ea5afSVlad Yasevich 	case NETDEV_FEAT_CHANGE:
14983d3ea5afSVlad Yasevich 		rtnl_event_type = IFLA_EVENT_FEATURES;
14993d3ea5afSVlad Yasevich 		break;
15003d3ea5afSVlad Yasevich 	case NETDEV_BONDING_FAILOVER:
15013d3ea5afSVlad Yasevich 		rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER;
15023d3ea5afSVlad Yasevich 		break;
15033d3ea5afSVlad Yasevich 	case NETDEV_NOTIFY_PEERS:
15043d3ea5afSVlad Yasevich 		rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS;
15053d3ea5afSVlad Yasevich 		break;
15063d3ea5afSVlad Yasevich 	case NETDEV_RESEND_IGMP:
15073d3ea5afSVlad Yasevich 		rtnl_event_type = IFLA_EVENT_IGMP_RESEND;
15083d3ea5afSVlad Yasevich 		break;
15093d3ea5afSVlad Yasevich 	case NETDEV_CHANGEINFODATA:
15103d3ea5afSVlad Yasevich 		rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS;
15113d3ea5afSVlad Yasevich 		break;
15123d3ea5afSVlad Yasevich 	default:
15133d3ea5afSVlad Yasevich 		break;
15143d3ea5afSVlad Yasevich 	}
15153d3ea5afSVlad Yasevich 
15163d3ea5afSVlad Yasevich 	return rtnl_event_type;
15173d3ea5afSVlad Yasevich }
15183d3ea5afSVlad Yasevich 
151979110a04SFlorian Westphal static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev)
152079110a04SFlorian Westphal {
152179110a04SFlorian Westphal 	const struct net_device *upper_dev;
152279110a04SFlorian Westphal 	int ret = 0;
152379110a04SFlorian Westphal 
152479110a04SFlorian Westphal 	rcu_read_lock();
152579110a04SFlorian Westphal 
152679110a04SFlorian Westphal 	upper_dev = netdev_master_upper_dev_get_rcu(dev);
152779110a04SFlorian Westphal 	if (upper_dev)
152879110a04SFlorian Westphal 		ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex);
152979110a04SFlorian Westphal 
153079110a04SFlorian Westphal 	rcu_read_unlock();
153179110a04SFlorian Westphal 	return ret;
153279110a04SFlorian Westphal }
153379110a04SFlorian Westphal 
1534feadc4b6SSabrina Dubroca static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev,
1535feadc4b6SSabrina Dubroca 			  bool force)
153679110a04SFlorian Westphal {
153779110a04SFlorian Westphal 	int ifindex = dev_get_iflink(dev);
153879110a04SFlorian Westphal 
1539feadc4b6SSabrina Dubroca 	if (force || dev->ifindex != ifindex)
154079110a04SFlorian Westphal 		return nla_put_u32(skb, IFLA_LINK, ifindex);
1541feadc4b6SSabrina Dubroca 
1542feadc4b6SSabrina Dubroca 	return 0;
154379110a04SFlorian Westphal }
154479110a04SFlorian Westphal 
15456c557001SFlorian Westphal static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb,
15466c557001SFlorian Westphal 					      struct net_device *dev)
15476c557001SFlorian Westphal {
15486c557001SFlorian Westphal 	char buf[IFALIASZ];
15496c557001SFlorian Westphal 	int ret;
15506c557001SFlorian Westphal 
15516c557001SFlorian Westphal 	ret = dev_get_alias(dev, buf, sizeof(buf));
15526c557001SFlorian Westphal 	return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0;
15536c557001SFlorian Westphal }
15546c557001SFlorian Westphal 
1555b1e66b9aSFlorian Westphal static int rtnl_fill_link_netnsid(struct sk_buff *skb,
155679e1ad14SJiri Benc 				  const struct net_device *dev,
1557d4e4fdf9SGuillaume Nault 				  struct net *src_net, gfp_t gfp)
1558b1e66b9aSFlorian Westphal {
1559feadc4b6SSabrina Dubroca 	bool put_iflink = false;
1560feadc4b6SSabrina Dubroca 
1561b1e66b9aSFlorian Westphal 	if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) {
1562b1e66b9aSFlorian Westphal 		struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1563b1e66b9aSFlorian Westphal 
1564b1e66b9aSFlorian Westphal 		if (!net_eq(dev_net(dev), link_net)) {
1565d4e4fdf9SGuillaume Nault 			int id = peernet2id_alloc(src_net, link_net, gfp);
1566b1e66b9aSFlorian Westphal 
1567b1e66b9aSFlorian Westphal 			if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1568b1e66b9aSFlorian Westphal 				return -EMSGSIZE;
1569feadc4b6SSabrina Dubroca 
1570feadc4b6SSabrina Dubroca 			put_iflink = true;
1571b1e66b9aSFlorian Westphal 		}
1572b1e66b9aSFlorian Westphal 	}
1573b1e66b9aSFlorian Westphal 
1574feadc4b6SSabrina Dubroca 	return nla_put_iflink(skb, dev, put_iflink);
1575b1e66b9aSFlorian Westphal }
1576b1e66b9aSFlorian Westphal 
1577070cbf5bSFlorian Westphal static int rtnl_fill_link_af(struct sk_buff *skb,
1578070cbf5bSFlorian Westphal 			     const struct net_device *dev,
1579070cbf5bSFlorian Westphal 			     u32 ext_filter_mask)
1580070cbf5bSFlorian Westphal {
1581070cbf5bSFlorian Westphal 	const struct rtnl_af_ops *af_ops;
1582070cbf5bSFlorian Westphal 	struct nlattr *af_spec;
1583070cbf5bSFlorian Westphal 
1584ae0be8deSMichal Kubecek 	af_spec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
1585070cbf5bSFlorian Westphal 	if (!af_spec)
1586070cbf5bSFlorian Westphal 		return -EMSGSIZE;
1587070cbf5bSFlorian Westphal 
15885fa85a09SFlorian Westphal 	list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
1589070cbf5bSFlorian Westphal 		struct nlattr *af;
1590070cbf5bSFlorian Westphal 		int err;
1591070cbf5bSFlorian Westphal 
1592070cbf5bSFlorian Westphal 		if (!af_ops->fill_link_af)
1593070cbf5bSFlorian Westphal 			continue;
1594070cbf5bSFlorian Westphal 
1595ae0be8deSMichal Kubecek 		af = nla_nest_start_noflag(skb, af_ops->family);
1596070cbf5bSFlorian Westphal 		if (!af)
1597070cbf5bSFlorian Westphal 			return -EMSGSIZE;
1598070cbf5bSFlorian Westphal 
1599070cbf5bSFlorian Westphal 		err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
1600070cbf5bSFlorian Westphal 		/*
1601070cbf5bSFlorian Westphal 		 * Caller may return ENODATA to indicate that there
1602070cbf5bSFlorian Westphal 		 * was no data to be dumped. This is not an error, it
1603070cbf5bSFlorian Westphal 		 * means we should trim the attribute header and
1604070cbf5bSFlorian Westphal 		 * continue.
1605070cbf5bSFlorian Westphal 		 */
1606070cbf5bSFlorian Westphal 		if (err == -ENODATA)
1607070cbf5bSFlorian Westphal 			nla_nest_cancel(skb, af);
1608070cbf5bSFlorian Westphal 		else if (err < 0)
1609070cbf5bSFlorian Westphal 			return -EMSGSIZE;
1610070cbf5bSFlorian Westphal 
1611070cbf5bSFlorian Westphal 		nla_nest_end(skb, af);
1612070cbf5bSFlorian Westphal 	}
1613070cbf5bSFlorian Westphal 
1614070cbf5bSFlorian Westphal 	nla_nest_end(skb, af_spec);
1615070cbf5bSFlorian Westphal 	return 0;
1616070cbf5bSFlorian Westphal }
1617070cbf5bSFlorian Westphal 
161888f4fb0cSJiri Pirko static int rtnl_fill_alt_ifnames(struct sk_buff *skb,
161988f4fb0cSJiri Pirko 				 const struct net_device *dev)
162088f4fb0cSJiri Pirko {
162188f4fb0cSJiri Pirko 	struct netdev_name_node *name_node;
162288f4fb0cSJiri Pirko 	int count = 0;
162388f4fb0cSJiri Pirko 
162488f4fb0cSJiri Pirko 	list_for_each_entry(name_node, &dev->name_node->list, list) {
162588f4fb0cSJiri Pirko 		if (nla_put_string(skb, IFLA_ALT_IFNAME, name_node->name))
162688f4fb0cSJiri Pirko 			return -EMSGSIZE;
162788f4fb0cSJiri Pirko 		count++;
162888f4fb0cSJiri Pirko 	}
162988f4fb0cSJiri Pirko 	return count;
163088f4fb0cSJiri Pirko }
163188f4fb0cSJiri Pirko 
163288f4fb0cSJiri Pirko static int rtnl_fill_prop_list(struct sk_buff *skb,
163388f4fb0cSJiri Pirko 			       const struct net_device *dev)
163488f4fb0cSJiri Pirko {
163588f4fb0cSJiri Pirko 	struct nlattr *prop_list;
163688f4fb0cSJiri Pirko 	int ret;
163788f4fb0cSJiri Pirko 
163888f4fb0cSJiri Pirko 	prop_list = nla_nest_start(skb, IFLA_PROP_LIST);
163988f4fb0cSJiri Pirko 	if (!prop_list)
164088f4fb0cSJiri Pirko 		return -EMSGSIZE;
164188f4fb0cSJiri Pirko 
164288f4fb0cSJiri Pirko 	ret = rtnl_fill_alt_ifnames(skb, dev);
164388f4fb0cSJiri Pirko 	if (ret <= 0)
164488f4fb0cSJiri Pirko 		goto nest_cancel;
164588f4fb0cSJiri Pirko 
164688f4fb0cSJiri Pirko 	nla_nest_end(skb, prop_list);
164788f4fb0cSJiri Pirko 	return 0;
164888f4fb0cSJiri Pirko 
164988f4fb0cSJiri Pirko nest_cancel:
165088f4fb0cSJiri Pirko 	nla_nest_cancel(skb, prop_list);
165188f4fb0cSJiri Pirko 	return ret;
165288f4fb0cSJiri Pirko }
165388f4fb0cSJiri Pirko 
165479e1ad14SJiri Benc static int rtnl_fill_ifinfo(struct sk_buff *skb,
165579e1ad14SJiri Benc 			    struct net_device *dev, struct net *src_net,
1656575c3e2aSPatrick McHardy 			    int type, u32 pid, u32 seq, u32 change,
16573d3ea5afSVlad Yasevich 			    unsigned int flags, u32 ext_filter_mask,
165838e01b30SNicolas Dichtel 			    u32 event, int *new_nsid, int new_ifindex,
1659d4e4fdf9SGuillaume Nault 			    int tgt_netnsid, gfp_t gfp)
1660b60c5115SThomas Graf {
1661b60c5115SThomas Graf 	struct ifinfomsg *ifm;
16621da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
16631da177e4SLinus Torvalds 
16642907c35fSEric Dumazet 	ASSERT_RTNL();
1665b60c5115SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1666b60c5115SThomas Graf 	if (nlh == NULL)
166726932566SPatrick McHardy 		return -EMSGSIZE;
16681da177e4SLinus Torvalds 
1669b60c5115SThomas Graf 	ifm = nlmsg_data(nlh);
1670b60c5115SThomas Graf 	ifm->ifi_family = AF_UNSPEC;
1671b60c5115SThomas Graf 	ifm->__ifi_pad = 0;
1672b60c5115SThomas Graf 	ifm->ifi_type = dev->type;
1673b60c5115SThomas Graf 	ifm->ifi_index = dev->ifindex;
1674b60c5115SThomas Graf 	ifm->ifi_flags = dev_get_flags(dev);
1675b60c5115SThomas Graf 	ifm->ifi_change = change;
16761da177e4SLinus Torvalds 
16777e4a8d5aSChristian Brauner 	if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_TARGET_NETNSID, tgt_netnsid))
167879e1ad14SJiri Benc 		goto nla_put_failure;
167979e1ad14SJiri Benc 
1680a6574349SDavid S. Miller 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1681a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1682a6574349SDavid S. Miller 	    nla_put_u8(skb, IFLA_OPERSTATE,
1683a6574349SDavid S. Miller 		       netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1684a6574349SDavid S. Miller 	    nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1685a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
16863e7a50ceSStephen Hemminger 	    nla_put_u32(skb, IFLA_MIN_MTU, dev->min_mtu) ||
16873e7a50ceSStephen Hemminger 	    nla_put_u32(skb, IFLA_MAX_MTU, dev->max_mtu) ||
1688a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_GROUP, dev->group) ||
1689edbc0bb3SBen Greear 	    nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
169076ff5cc9SJiri Pirko 	    nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
1691c70ce028SEric Dumazet 	    nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1692c70ce028SEric Dumazet 	    nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
16931d69c2b3SMark A. Greer #ifdef CONFIG_RPS
169476ff5cc9SJiri Pirko 	    nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
16951d69c2b3SMark A. Greer #endif
169679110a04SFlorian Westphal 	    put_master_ifindex(skb, dev) ||
16979a57247fSJiri Pirko 	    nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
1698a6574349SDavid S. Miller 	    (dev->qdisc &&
1699a6574349SDavid S. Miller 	     nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
17006c557001SFlorian Westphal 	    nla_put_ifalias(skb, dev) ||
17012d3b479dSdavid decotigny 	    nla_put_u32(skb, IFLA_CARRIER_CHANGES,
1702b2d3bcfaSDavid Decotigny 			atomic_read(&dev->carrier_up_count) +
1703b2d3bcfaSDavid Decotigny 			atomic_read(&dev->carrier_down_count)) ||
1704b2d3bcfaSDavid Decotigny 	    nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down) ||
1705b2d3bcfaSDavid Decotigny 	    nla_put_u32(skb, IFLA_CARRIER_UP_COUNT,
1706b2d3bcfaSDavid Decotigny 			atomic_read(&dev->carrier_up_count)) ||
1707b2d3bcfaSDavid Decotigny 	    nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT,
1708b2d3bcfaSDavid Decotigny 			atomic_read(&dev->carrier_down_count)))
1709a6574349SDavid S. Miller 		goto nla_put_failure;
17100b815a1aSStephen Hemminger 
17113d3ea5afSVlad Yasevich 	if (event != IFLA_EVENT_NONE) {
17123d3ea5afSVlad Yasevich 		if (nla_put_u32(skb, IFLA_EVENT, event))
17133d3ea5afSVlad Yasevich 			goto nla_put_failure;
17143d3ea5afSVlad Yasevich 	}
17153d3ea5afSVlad Yasevich 
1716b22b941bSHannes Frederic Sowa 	if (rtnl_fill_link_ifmap(skb, dev))
1717a6574349SDavid S. Miller 		goto nla_put_failure;
17181da177e4SLinus Torvalds 
17191da177e4SLinus Torvalds 	if (dev->addr_len) {
1720a6574349SDavid S. Miller 		if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1721a6574349SDavid S. Miller 		    nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1722a6574349SDavid S. Miller 			goto nla_put_failure;
17231da177e4SLinus Torvalds 	}
17241da177e4SLinus Torvalds 
172566cae9edSJiri Pirko 	if (rtnl_phys_port_id_fill(skb, dev))
172666cae9edSJiri Pirko 		goto nla_put_failure;
172766cae9edSJiri Pirko 
1728db24a904SDavid Ahern 	if (rtnl_phys_port_name_fill(skb, dev))
1729db24a904SDavid Ahern 		goto nla_put_failure;
1730db24a904SDavid Ahern 
173182f28412SJiri Pirko 	if (rtnl_phys_switch_id_fill(skb, dev))
173282f28412SJiri Pirko 		goto nla_put_failure;
173382f28412SJiri Pirko 
1734b22b941bSHannes Frederic Sowa 	if (rtnl_fill_stats(skb, dev))
1735b60c5115SThomas Graf 		goto nla_put_failure;
1736b60c5115SThomas Graf 
1737250fc3dfSFlorian Westphal 	if (rtnl_fill_vf(skb, dev, ext_filter_mask))
1738a6574349SDavid S. Miller 		goto nla_put_failure;
173957b61080SScott Feldman 
1740c53864fdSDavid Gibson 	if (rtnl_port_fill(skb, dev, ext_filter_mask))
174157b61080SScott Feldman 		goto nla_put_failure;
174257b61080SScott Feldman 
1743d1fdd913SBrenden Blanco 	if (rtnl_xdp_fill(skb, dev))
1744d1fdd913SBrenden Blanco 		goto nla_put_failure;
1745d1fdd913SBrenden Blanco 
1746ba7d49b1SJiri Pirko 	if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
174738f7b870SPatrick McHardy 		if (rtnl_link_fill(skb, dev) < 0)
174838f7b870SPatrick McHardy 			goto nla_put_failure;
174938f7b870SPatrick McHardy 	}
175038f7b870SPatrick McHardy 
1751d4e4fdf9SGuillaume Nault 	if (rtnl_fill_link_netnsid(skb, dev, src_net, gfp))
1752d37512a2SNicolas Dichtel 		goto nla_put_failure;
1753d37512a2SNicolas Dichtel 
17546621dd29SNicolas Dichtel 	if (new_nsid &&
17556621dd29SNicolas Dichtel 	    nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0)
17566621dd29SNicolas Dichtel 		goto nla_put_failure;
175738e01b30SNicolas Dichtel 	if (new_ifindex &&
175838e01b30SNicolas Dichtel 	    nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0)
175938e01b30SNicolas Dichtel 		goto nla_put_failure;
176038e01b30SNicolas Dichtel 
1761*f74877a5SMichal Kubecek 	if (memchr_inv(dev->perm_addr, '\0', dev->addr_len) &&
1762*f74877a5SMichal Kubecek 	    nla_put(skb, IFLA_PERM_ADDRESS, dev->addr_len, dev->perm_addr))
1763*f74877a5SMichal Kubecek 		goto nla_put_failure;
17646621dd29SNicolas Dichtel 
17655fa85a09SFlorian Westphal 	rcu_read_lock();
1766070cbf5bSFlorian Westphal 	if (rtnl_fill_link_af(skb, dev, ext_filter_mask))
17675fa85a09SFlorian Westphal 		goto nla_put_failure_rcu;
17685fa85a09SFlorian Westphal 	rcu_read_unlock();
1769f8ff182cSThomas Graf 
177088f4fb0cSJiri Pirko 	if (rtnl_fill_prop_list(skb, dev))
177188f4fb0cSJiri Pirko 		goto nla_put_failure;
177288f4fb0cSJiri Pirko 
1773053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
1774053c095aSJohannes Berg 	return 0;
1775b60c5115SThomas Graf 
17765fa85a09SFlorian Westphal nla_put_failure_rcu:
17775fa85a09SFlorian Westphal 	rcu_read_unlock();
1778b60c5115SThomas Graf nla_put_failure:
177926932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
178026932566SPatrick McHardy 	return -EMSGSIZE;
17811da177e4SLinus Torvalds }
17821da177e4SLinus Torvalds 
1783f7b12606SJiri Pirko static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1784f7b12606SJiri Pirko 	[IFLA_IFNAME]		= { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1785f7b12606SJiri Pirko 	[IFLA_ADDRESS]		= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1786f7b12606SJiri Pirko 	[IFLA_BROADCAST]	= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1787f7b12606SJiri Pirko 	[IFLA_MAP]		= { .len = sizeof(struct rtnl_link_ifmap) },
1788f7b12606SJiri Pirko 	[IFLA_MTU]		= { .type = NLA_U32 },
1789f7b12606SJiri Pirko 	[IFLA_LINK]		= { .type = NLA_U32 },
1790f7b12606SJiri Pirko 	[IFLA_MASTER]		= { .type = NLA_U32 },
1791f7b12606SJiri Pirko 	[IFLA_CARRIER]		= { .type = NLA_U8 },
1792f7b12606SJiri Pirko 	[IFLA_TXQLEN]		= { .type = NLA_U32 },
1793f7b12606SJiri Pirko 	[IFLA_WEIGHT]		= { .type = NLA_U32 },
1794f7b12606SJiri Pirko 	[IFLA_OPERSTATE]	= { .type = NLA_U8 },
1795f7b12606SJiri Pirko 	[IFLA_LINKMODE]		= { .type = NLA_U8 },
1796f7b12606SJiri Pirko 	[IFLA_LINKINFO]		= { .type = NLA_NESTED },
1797f7b12606SJiri Pirko 	[IFLA_NET_NS_PID]	= { .type = NLA_U32 },
1798f7b12606SJiri Pirko 	[IFLA_NET_NS_FD]	= { .type = NLA_U32 },
17992459b4c6SNicolas Dichtel 	/* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
18002459b4c6SNicolas Dichtel 	 * allow 0-length string (needed to remove an alias).
18012459b4c6SNicolas Dichtel 	 */
18022459b4c6SNicolas Dichtel 	[IFLA_IFALIAS]	        = { .type = NLA_BINARY, .len = IFALIASZ - 1 },
1803f7b12606SJiri Pirko 	[IFLA_VFINFO_LIST]	= {. type = NLA_NESTED },
1804f7b12606SJiri Pirko 	[IFLA_VF_PORTS]		= { .type = NLA_NESTED },
1805f7b12606SJiri Pirko 	[IFLA_PORT_SELF]	= { .type = NLA_NESTED },
1806f7b12606SJiri Pirko 	[IFLA_AF_SPEC]		= { .type = NLA_NESTED },
1807f7b12606SJiri Pirko 	[IFLA_EXT_MASK]		= { .type = NLA_U32 },
1808f7b12606SJiri Pirko 	[IFLA_PROMISCUITY]	= { .type = NLA_U32 },
1809f7b12606SJiri Pirko 	[IFLA_NUM_TX_QUEUES]	= { .type = NLA_U32 },
1810f7b12606SJiri Pirko 	[IFLA_NUM_RX_QUEUES]	= { .type = NLA_U32 },
181146e6b992SStephen Hemminger 	[IFLA_GSO_MAX_SEGS]	= { .type = NLA_U32 },
181246e6b992SStephen Hemminger 	[IFLA_GSO_MAX_SIZE]	= { .type = NLA_U32 },
181302637fceSJiri Pirko 	[IFLA_PHYS_PORT_ID]	= { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
18142d3b479dSdavid decotigny 	[IFLA_CARRIER_CHANGES]	= { .type = NLA_U32 },  /* ignored */
181582f28412SJiri Pirko 	[IFLA_PHYS_SWITCH_ID]	= { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1816317f4810SNicolas Dichtel 	[IFLA_LINK_NETNSID]	= { .type = NLA_S32 },
181788d6378bSAnuradha Karuppiah 	[IFLA_PROTO_DOWN]	= { .type = NLA_U8 },
1818d1fdd913SBrenden Blanco 	[IFLA_XDP]		= { .type = NLA_NESTED },
18193d3ea5afSVlad Yasevich 	[IFLA_EVENT]		= { .type = NLA_U32 },
1820db833d40SSerhey Popovych 	[IFLA_GROUP]		= { .type = NLA_U32 },
18217e4a8d5aSChristian Brauner 	[IFLA_TARGET_NETNSID]	= { .type = NLA_S32 },
1822b2d3bcfaSDavid Decotigny 	[IFLA_CARRIER_UP_COUNT]	= { .type = NLA_U32 },
1823b2d3bcfaSDavid Decotigny 	[IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 },
18243e7a50ceSStephen Hemminger 	[IFLA_MIN_MTU]		= { .type = NLA_U32 },
18253e7a50ceSStephen Hemminger 	[IFLA_MAX_MTU]		= { .type = NLA_U32 },
182636fbf1e5SJiri Pirko 	[IFLA_PROP_LIST]	= { .type = NLA_NESTED },
182736fbf1e5SJiri Pirko 	[IFLA_ALT_IFNAME]	= { .type = NLA_STRING,
182836fbf1e5SJiri Pirko 				    .len = ALTIFNAMSIZ - 1 },
1829*f74877a5SMichal Kubecek 	[IFLA_PERM_ADDRESS]	= { .type = NLA_REJECT },
1830f7b12606SJiri Pirko };
1831f7b12606SJiri Pirko 
1832f7b12606SJiri Pirko static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1833f7b12606SJiri Pirko 	[IFLA_INFO_KIND]	= { .type = NLA_STRING },
1834f7b12606SJiri Pirko 	[IFLA_INFO_DATA]	= { .type = NLA_NESTED },
1835f7b12606SJiri Pirko 	[IFLA_INFO_SLAVE_KIND]	= { .type = NLA_STRING },
1836f7b12606SJiri Pirko 	[IFLA_INFO_SLAVE_DATA]	= { .type = NLA_NESTED },
1837f7b12606SJiri Pirko };
1838f7b12606SJiri Pirko 
1839f7b12606SJiri Pirko static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1840364d5716SDaniel Borkmann 	[IFLA_VF_MAC]		= { .len = sizeof(struct ifla_vf_mac) },
184175345f88SDenis Kirjanov 	[IFLA_VF_BROADCAST]	= { .type = NLA_REJECT },
1842364d5716SDaniel Borkmann 	[IFLA_VF_VLAN]		= { .len = sizeof(struct ifla_vf_vlan) },
184379aab093SMoshe Shemesh 	[IFLA_VF_VLAN_LIST]     = { .type = NLA_NESTED },
1844364d5716SDaniel Borkmann 	[IFLA_VF_TX_RATE]	= { .len = sizeof(struct ifla_vf_tx_rate) },
1845364d5716SDaniel Borkmann 	[IFLA_VF_SPOOFCHK]	= { .len = sizeof(struct ifla_vf_spoofchk) },
1846364d5716SDaniel Borkmann 	[IFLA_VF_RATE]		= { .len = sizeof(struct ifla_vf_rate) },
1847364d5716SDaniel Borkmann 	[IFLA_VF_LINK_STATE]	= { .len = sizeof(struct ifla_vf_link_state) },
184801a3d796SVlad Zolotarov 	[IFLA_VF_RSS_QUERY_EN]	= { .len = sizeof(struct ifla_vf_rss_query_en) },
18493b766cd8SEran Ben Elisha 	[IFLA_VF_STATS]		= { .type = NLA_NESTED },
1850dd461d6aSHiroshi Shimamoto 	[IFLA_VF_TRUST]		= { .len = sizeof(struct ifla_vf_trust) },
1851cc8e27ccSEli Cohen 	[IFLA_VF_IB_NODE_GUID]	= { .len = sizeof(struct ifla_vf_guid) },
1852cc8e27ccSEli Cohen 	[IFLA_VF_IB_PORT_GUID]	= { .len = sizeof(struct ifla_vf_guid) },
18533b766cd8SEran Ben Elisha };
18543b766cd8SEran Ben Elisha 
1855f7b12606SJiri Pirko static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1856f7b12606SJiri Pirko 	[IFLA_PORT_VF]		= { .type = NLA_U32 },
1857f7b12606SJiri Pirko 	[IFLA_PORT_PROFILE]	= { .type = NLA_STRING,
1858f7b12606SJiri Pirko 				    .len = PORT_PROFILE_MAX },
1859f7b12606SJiri Pirko 	[IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1860f7b12606SJiri Pirko 				      .len = PORT_UUID_MAX },
1861f7b12606SJiri Pirko 	[IFLA_PORT_HOST_UUID]	= { .type = NLA_STRING,
1862f7b12606SJiri Pirko 				    .len = PORT_UUID_MAX },
1863f7b12606SJiri Pirko 	[IFLA_PORT_REQUEST]	= { .type = NLA_U8, },
1864f7b12606SJiri Pirko 	[IFLA_PORT_RESPONSE]	= { .type = NLA_U16, },
1865025331dfSDaniel Borkmann 
1866025331dfSDaniel Borkmann 	/* Unused, but we need to keep it here since user space could
1867025331dfSDaniel Borkmann 	 * fill it. It's also broken with regard to NLA_BINARY use in
1868025331dfSDaniel Borkmann 	 * combination with structs.
1869025331dfSDaniel Borkmann 	 */
1870025331dfSDaniel Borkmann 	[IFLA_PORT_VSI_TYPE]	= { .type = NLA_BINARY,
1871025331dfSDaniel Borkmann 				    .len = sizeof(struct ifla_port_vsi) },
1872f7b12606SJiri Pirko };
1873f7b12606SJiri Pirko 
1874d1fdd913SBrenden Blanco static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
1875d1fdd913SBrenden Blanco 	[IFLA_XDP_FD]		= { .type = NLA_S32 },
1876d1fdd913SBrenden Blanco 	[IFLA_XDP_ATTACHED]	= { .type = NLA_U8 },
187785de8576SDaniel Borkmann 	[IFLA_XDP_FLAGS]	= { .type = NLA_U32 },
187858038695SMartin KaFai Lau 	[IFLA_XDP_PROG_ID]	= { .type = NLA_U32 },
1879d1fdd913SBrenden Blanco };
1880d1fdd913SBrenden Blanco 
1881dc599f76SDavid Ahern static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1882dc599f76SDavid Ahern {
1883dc599f76SDavid Ahern 	const struct rtnl_link_ops *ops = NULL;
1884dc599f76SDavid Ahern 	struct nlattr *linfo[IFLA_INFO_MAX + 1];
1885dc599f76SDavid Ahern 
18868cb08174SJohannes Berg 	if (nla_parse_nested_deprecated(linfo, IFLA_INFO_MAX, nla, ifla_info_policy, NULL) < 0)
1887dc599f76SDavid Ahern 		return NULL;
1888dc599f76SDavid Ahern 
1889dc599f76SDavid Ahern 	if (linfo[IFLA_INFO_KIND]) {
1890dc599f76SDavid Ahern 		char kind[MODULE_NAME_LEN];
1891dc599f76SDavid Ahern 
1892dc599f76SDavid Ahern 		nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1893dc599f76SDavid Ahern 		ops = rtnl_link_ops_get(kind);
1894dc599f76SDavid Ahern 	}
1895dc599f76SDavid Ahern 
1896dc599f76SDavid Ahern 	return ops;
1897dc599f76SDavid Ahern }
1898dc599f76SDavid Ahern 
1899dc599f76SDavid Ahern static bool link_master_filtered(struct net_device *dev, int master_idx)
1900dc599f76SDavid Ahern {
1901dc599f76SDavid Ahern 	struct net_device *master;
1902dc599f76SDavid Ahern 
1903dc599f76SDavid Ahern 	if (!master_idx)
1904dc599f76SDavid Ahern 		return false;
1905dc599f76SDavid Ahern 
1906dc599f76SDavid Ahern 	master = netdev_master_upper_dev_get(dev);
1907dc599f76SDavid Ahern 	if (!master || master->ifindex != master_idx)
1908dc599f76SDavid Ahern 		return true;
1909dc599f76SDavid Ahern 
1910dc599f76SDavid Ahern 	return false;
1911dc599f76SDavid Ahern }
1912dc599f76SDavid Ahern 
1913dc599f76SDavid Ahern static bool link_kind_filtered(const struct net_device *dev,
1914dc599f76SDavid Ahern 			       const struct rtnl_link_ops *kind_ops)
1915dc599f76SDavid Ahern {
1916dc599f76SDavid Ahern 	if (kind_ops && dev->rtnl_link_ops != kind_ops)
1917dc599f76SDavid Ahern 		return true;
1918dc599f76SDavid Ahern 
1919dc599f76SDavid Ahern 	return false;
1920dc599f76SDavid Ahern }
1921dc599f76SDavid Ahern 
1922dc599f76SDavid Ahern static bool link_dump_filtered(struct net_device *dev,
1923dc599f76SDavid Ahern 			       int master_idx,
1924dc599f76SDavid Ahern 			       const struct rtnl_link_ops *kind_ops)
1925dc599f76SDavid Ahern {
1926dc599f76SDavid Ahern 	if (link_master_filtered(dev, master_idx) ||
1927dc599f76SDavid Ahern 	    link_kind_filtered(dev, kind_ops))
1928dc599f76SDavid Ahern 		return true;
1929dc599f76SDavid Ahern 
1930dc599f76SDavid Ahern 	return false;
1931dc599f76SDavid Ahern }
1932dc599f76SDavid Ahern 
1933c383edc4SChristian Brauner /**
1934c383edc4SChristian Brauner  * rtnl_get_net_ns_capable - Get netns if sufficiently privileged.
1935c383edc4SChristian Brauner  * @sk: netlink socket
1936c383edc4SChristian Brauner  * @netnsid: network namespace identifier
1937c383edc4SChristian Brauner  *
1938c383edc4SChristian Brauner  * Returns the network namespace identified by netnsid on success or an error
1939c383edc4SChristian Brauner  * pointer on failure.
1940c383edc4SChristian Brauner  */
1941c383edc4SChristian Brauner struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid)
194279e1ad14SJiri Benc {
194379e1ad14SJiri Benc 	struct net *net;
194479e1ad14SJiri Benc 
1945f428fe4aSAndrei Vagin 	net = get_net_ns_by_id(sock_net(sk), netnsid);
194679e1ad14SJiri Benc 	if (!net)
194779e1ad14SJiri Benc 		return ERR_PTR(-EINVAL);
194879e1ad14SJiri Benc 
194979e1ad14SJiri Benc 	/* For now, the caller is required to have CAP_NET_ADMIN in
195079e1ad14SJiri Benc 	 * the user namespace owning the target net ns.
195179e1ad14SJiri Benc 	 */
1952f428fe4aSAndrei Vagin 	if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) {
195379e1ad14SJiri Benc 		put_net(net);
195479e1ad14SJiri Benc 		return ERR_PTR(-EACCES);
195579e1ad14SJiri Benc 	}
195679e1ad14SJiri Benc 	return net;
195779e1ad14SJiri Benc }
1958c383edc4SChristian Brauner EXPORT_SYMBOL_GPL(rtnl_get_net_ns_capable);
195979e1ad14SJiri Benc 
1960905cf0abSDavid Ahern static int rtnl_valid_dump_ifinfo_req(const struct nlmsghdr *nlh,
1961905cf0abSDavid Ahern 				      bool strict_check, struct nlattr **tb,
1962905cf0abSDavid Ahern 				      struct netlink_ext_ack *extack)
1963905cf0abSDavid Ahern {
1964905cf0abSDavid Ahern 	int hdrlen;
1965905cf0abSDavid Ahern 
1966905cf0abSDavid Ahern 	if (strict_check) {
1967905cf0abSDavid Ahern 		struct ifinfomsg *ifm;
1968905cf0abSDavid Ahern 
1969905cf0abSDavid Ahern 		if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
1970905cf0abSDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid header for link dump");
1971905cf0abSDavid Ahern 			return -EINVAL;
1972905cf0abSDavid Ahern 		}
1973905cf0abSDavid Ahern 
1974905cf0abSDavid Ahern 		ifm = nlmsg_data(nlh);
1975905cf0abSDavid Ahern 		if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
1976905cf0abSDavid Ahern 		    ifm->ifi_change) {
1977905cf0abSDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid values in header for link dump request");
1978905cf0abSDavid Ahern 			return -EINVAL;
1979905cf0abSDavid Ahern 		}
1980905cf0abSDavid Ahern 		if (ifm->ifi_index) {
1981905cf0abSDavid Ahern 			NL_SET_ERR_MSG(extack, "Filter by device index not supported for link dumps");
1982905cf0abSDavid Ahern 			return -EINVAL;
1983905cf0abSDavid Ahern 		}
1984905cf0abSDavid Ahern 
19858cb08174SJohannes Berg 		return nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb,
19868cb08174SJohannes Berg 						     IFLA_MAX, ifla_policy,
19878cb08174SJohannes Berg 						     extack);
1988905cf0abSDavid Ahern 	}
1989905cf0abSDavid Ahern 
1990905cf0abSDavid Ahern 	/* A hack to preserve kernel<->userspace interface.
1991905cf0abSDavid Ahern 	 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1992905cf0abSDavid Ahern 	 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1993905cf0abSDavid Ahern 	 * what iproute2 < v3.9.0 used.
1994905cf0abSDavid Ahern 	 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1995905cf0abSDavid Ahern 	 * attribute, its netlink message is shorter than struct ifinfomsg.
1996905cf0abSDavid Ahern 	 */
1997905cf0abSDavid Ahern 	hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
1998905cf0abSDavid Ahern 		 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1999905cf0abSDavid Ahern 
20008cb08174SJohannes Berg 	return nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy,
20018cb08174SJohannes Berg 				      extack);
2002905cf0abSDavid Ahern }
2003905cf0abSDavid Ahern 
2004b60c5115SThomas Graf static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
20051da177e4SLinus Torvalds {
2006905cf0abSDavid Ahern 	struct netlink_ext_ack *extack = cb->extack;
2007905cf0abSDavid Ahern 	const struct nlmsghdr *nlh = cb->nlh;
20083b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
200979e1ad14SJiri Benc 	struct net *tgt_net = net;
20107c28bd0bSEric Dumazet 	int h, s_h;
20117c28bd0bSEric Dumazet 	int idx = 0, s_idx;
20121da177e4SLinus Torvalds 	struct net_device *dev;
20137c28bd0bSEric Dumazet 	struct hlist_head *head;
2014115c9b81SGreg Rose 	struct nlattr *tb[IFLA_MAX+1];
2015115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
2016dc599f76SDavid Ahern 	const struct rtnl_link_ops *kind_ops = NULL;
2017dc599f76SDavid Ahern 	unsigned int flags = NLM_F_MULTI;
2018dc599f76SDavid Ahern 	int master_idx = 0;
201979e1ad14SJiri Benc 	int netnsid = -1;
2020905cf0abSDavid Ahern 	int err, i;
20211da177e4SLinus Torvalds 
20227c28bd0bSEric Dumazet 	s_h = cb->args[0];
20237c28bd0bSEric Dumazet 	s_idx = cb->args[1];
20247c28bd0bSEric Dumazet 
2025905cf0abSDavid Ahern 	err = rtnl_valid_dump_ifinfo_req(nlh, cb->strict_check, tb, extack);
2026905cf0abSDavid Ahern 	if (err < 0) {
2027905cf0abSDavid Ahern 		if (cb->strict_check)
2028905cf0abSDavid Ahern 			return err;
2029e5eca6d4SMichal Schmidt 
2030905cf0abSDavid Ahern 		goto walk_entries;
203179e1ad14SJiri Benc 	}
203279e1ad14SJiri Benc 
2033905cf0abSDavid Ahern 	for (i = 0; i <= IFLA_MAX; ++i) {
2034905cf0abSDavid Ahern 		if (!tb[i])
2035905cf0abSDavid Ahern 			continue;
2036dc599f76SDavid Ahern 
2037905cf0abSDavid Ahern 		/* new attributes should only be added with strict checking */
2038905cf0abSDavid Ahern 		switch (i) {
2039905cf0abSDavid Ahern 		case IFLA_TARGET_NETNSID:
2040905cf0abSDavid Ahern 			netnsid = nla_get_s32(tb[i]);
2041905cf0abSDavid Ahern 			tgt_net = rtnl_get_net_ns_capable(skb->sk, netnsid);
2042905cf0abSDavid Ahern 			if (IS_ERR(tgt_net)) {
2043905cf0abSDavid Ahern 				NL_SET_ERR_MSG(extack, "Invalid target network namespace id");
2044905cf0abSDavid Ahern 				return PTR_ERR(tgt_net);
2045905cf0abSDavid Ahern 			}
2046905cf0abSDavid Ahern 			break;
2047905cf0abSDavid Ahern 		case IFLA_EXT_MASK:
2048905cf0abSDavid Ahern 			ext_filter_mask = nla_get_u32(tb[i]);
2049905cf0abSDavid Ahern 			break;
2050905cf0abSDavid Ahern 		case IFLA_MASTER:
2051905cf0abSDavid Ahern 			master_idx = nla_get_u32(tb[i]);
2052905cf0abSDavid Ahern 			break;
2053905cf0abSDavid Ahern 		case IFLA_LINKINFO:
2054905cf0abSDavid Ahern 			kind_ops = linkinfo_to_kind_ops(tb[i]);
2055905cf0abSDavid Ahern 			break;
2056905cf0abSDavid Ahern 		default:
2057905cf0abSDavid Ahern 			if (cb->strict_check) {
2058905cf0abSDavid Ahern 				NL_SET_ERR_MSG(extack, "Unsupported attribute in link dump request");
2059905cf0abSDavid Ahern 				return -EINVAL;
2060905cf0abSDavid Ahern 			}
2061905cf0abSDavid Ahern 		}
2062905cf0abSDavid Ahern 	}
2063dc599f76SDavid Ahern 
2064dc599f76SDavid Ahern 	if (master_idx || kind_ops)
2065dc599f76SDavid Ahern 		flags |= NLM_F_DUMP_FILTERED;
2066115c9b81SGreg Rose 
2067905cf0abSDavid Ahern walk_entries:
20687c28bd0bSEric Dumazet 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
20697562f876SPavel Emelianov 		idx = 0;
207079e1ad14SJiri Benc 		head = &tgt_net->dev_index_head[h];
2071cac5e65eSEric Dumazet 		hlist_for_each_entry(dev, head, index_hlist) {
2072dc599f76SDavid Ahern 			if (link_dump_filtered(dev, master_idx, kind_ops))
20733f0ae05dSZhang Shengju 				goto cont;
20741da177e4SLinus Torvalds 			if (idx < s_idx)
20757562f876SPavel Emelianov 				goto cont;
207679e1ad14SJiri Benc 			err = rtnl_fill_ifinfo(skb, dev, net,
207779e1ad14SJiri Benc 					       RTM_NEWLINK,
207815e47304SEric W. Biederman 					       NETLINK_CB(cb->skb).portid,
2079905cf0abSDavid Ahern 					       nlh->nlmsg_seq, 0, flags,
208038e01b30SNicolas Dichtel 					       ext_filter_mask, 0, NULL, 0,
2081d4e4fdf9SGuillaume Nault 					       netnsid, GFP_KERNEL);
2082973462bbSDavid Gibson 
2083f6c5775fSDavid Ahern 			if (err < 0) {
2084f6c5775fSDavid Ahern 				if (likely(skb->len))
20857c28bd0bSEric Dumazet 					goto out;
20864e985adaSThomas Graf 
2087f6c5775fSDavid Ahern 				goto out_err;
2088f6c5775fSDavid Ahern 			}
20897562f876SPavel Emelianov cont:
20907562f876SPavel Emelianov 			idx++;
20911da177e4SLinus Torvalds 		}
20927c28bd0bSEric Dumazet 	}
20937c28bd0bSEric Dumazet out:
2094f6c5775fSDavid Ahern 	err = skb->len;
2095f6c5775fSDavid Ahern out_err:
20967c28bd0bSEric Dumazet 	cb->args[1] = idx;
20977c28bd0bSEric Dumazet 	cb->args[0] = h;
2098d0225784SJakub Sitnicki 	cb->seq = net->dev_base_seq;
2099d0225784SJakub Sitnicki 	nl_dump_check_consistent(cb, nlmsg_hdr(skb));
210079e1ad14SJiri Benc 	if (netnsid >= 0)
210179e1ad14SJiri Benc 		put_net(tgt_net);
21021da177e4SLinus Torvalds 
2103f6c5775fSDavid Ahern 	return err;
21041da177e4SLinus Torvalds }
21051da177e4SLinus Torvalds 
2106fceb6435SJohannes Berg int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len,
2107fceb6435SJohannes Berg 			struct netlink_ext_ack *exterr)
2108f7b12606SJiri Pirko {
21098cb08174SJohannes Berg 	return nla_parse_deprecated(tb, IFLA_MAX, head, len, ifla_policy,
21108cb08174SJohannes Berg 				    exterr);
2111f7b12606SJiri Pirko }
2112f7b12606SJiri Pirko EXPORT_SYMBOL(rtnl_nla_parse_ifla);
211357b61080SScott Feldman 
211481adee47SEric W. Biederman struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
211581adee47SEric W. Biederman {
211681adee47SEric W. Biederman 	struct net *net;
211781adee47SEric W. Biederman 	/* Examine the link attributes and figure out which
211881adee47SEric W. Biederman 	 * network namespace we are talking about.
211981adee47SEric W. Biederman 	 */
212081adee47SEric W. Biederman 	if (tb[IFLA_NET_NS_PID])
212181adee47SEric W. Biederman 		net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
2122f0630529SEric W. Biederman 	else if (tb[IFLA_NET_NS_FD])
2123f0630529SEric W. Biederman 		net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
212481adee47SEric W. Biederman 	else
212581adee47SEric W. Biederman 		net = get_net(src_net);
212681adee47SEric W. Biederman 	return net;
212781adee47SEric W. Biederman }
212881adee47SEric W. Biederman EXPORT_SYMBOL(rtnl_link_get_net);
212981adee47SEric W. Biederman 
21307c4f63baSChristian Brauner /* Figure out which network namespace we are talking about by
21317c4f63baSChristian Brauner  * examining the link attributes in the following order:
21327c4f63baSChristian Brauner  *
21337c4f63baSChristian Brauner  * 1. IFLA_NET_NS_PID
21347c4f63baSChristian Brauner  * 2. IFLA_NET_NS_FD
21357e4a8d5aSChristian Brauner  * 3. IFLA_TARGET_NETNSID
21367c4f63baSChristian Brauner  */
21377c4f63baSChristian Brauner static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net,
21387c4f63baSChristian Brauner 					       struct nlattr *tb[])
21397c4f63baSChristian Brauner {
21407c4f63baSChristian Brauner 	struct net *net;
21417c4f63baSChristian Brauner 
21427c4f63baSChristian Brauner 	if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])
21437c4f63baSChristian Brauner 		return rtnl_link_get_net(src_net, tb);
21447c4f63baSChristian Brauner 
21457e4a8d5aSChristian Brauner 	if (!tb[IFLA_TARGET_NETNSID])
21467c4f63baSChristian Brauner 		return get_net(src_net);
21477c4f63baSChristian Brauner 
21487e4a8d5aSChristian Brauner 	net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_TARGET_NETNSID]));
21497c4f63baSChristian Brauner 	if (!net)
21507c4f63baSChristian Brauner 		return ERR_PTR(-EINVAL);
21517c4f63baSChristian Brauner 
21527c4f63baSChristian Brauner 	return net;
21537c4f63baSChristian Brauner }
21547c4f63baSChristian Brauner 
21557c4f63baSChristian Brauner static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb,
21567c4f63baSChristian Brauner 					     struct net *src_net,
21577c4f63baSChristian Brauner 					     struct nlattr *tb[], int cap)
21587c4f63baSChristian Brauner {
21597c4f63baSChristian Brauner 	struct net *net;
21607c4f63baSChristian Brauner 
21617c4f63baSChristian Brauner 	net = rtnl_link_get_net_by_nlattr(src_net, tb);
21627c4f63baSChristian Brauner 	if (IS_ERR(net))
21637c4f63baSChristian Brauner 		return net;
21647c4f63baSChristian Brauner 
21657c4f63baSChristian Brauner 	if (!netlink_ns_capable(skb, net->user_ns, cap)) {
21667c4f63baSChristian Brauner 		put_net(net);
21677c4f63baSChristian Brauner 		return ERR_PTR(-EPERM);
21687c4f63baSChristian Brauner 	}
21697c4f63baSChristian Brauner 
21707c4f63baSChristian Brauner 	return net;
21717c4f63baSChristian Brauner }
21727c4f63baSChristian Brauner 
21734ff66caeSChristian Brauner /* Verify that rtnetlink requests do not pass additional properties
21744ff66caeSChristian Brauner  * potentially referring to different network namespaces.
21754ff66caeSChristian Brauner  */
21764ff66caeSChristian Brauner static int rtnl_ensure_unique_netns(struct nlattr *tb[],
21774ff66caeSChristian Brauner 				    struct netlink_ext_ack *extack,
21784ff66caeSChristian Brauner 				    bool netns_id_only)
21794ff66caeSChristian Brauner {
21804ff66caeSChristian Brauner 
21814ff66caeSChristian Brauner 	if (netns_id_only) {
21824ff66caeSChristian Brauner 		if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD])
21834ff66caeSChristian Brauner 			return 0;
21844ff66caeSChristian Brauner 
21854ff66caeSChristian Brauner 		NL_SET_ERR_MSG(extack, "specified netns attribute not supported");
21864ff66caeSChristian Brauner 		return -EOPNOTSUPP;
21874ff66caeSChristian Brauner 	}
21884ff66caeSChristian Brauner 
21897e4a8d5aSChristian Brauner 	if (tb[IFLA_TARGET_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]))
21904ff66caeSChristian Brauner 		goto invalid_attr;
21914ff66caeSChristian Brauner 
21927e4a8d5aSChristian Brauner 	if (tb[IFLA_NET_NS_PID] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_FD]))
21934ff66caeSChristian Brauner 		goto invalid_attr;
21944ff66caeSChristian Brauner 
21957e4a8d5aSChristian Brauner 	if (tb[IFLA_NET_NS_FD] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_PID]))
21964ff66caeSChristian Brauner 		goto invalid_attr;
21974ff66caeSChristian Brauner 
21984ff66caeSChristian Brauner 	return 0;
21994ff66caeSChristian Brauner 
22004ff66caeSChristian Brauner invalid_attr:
22014ff66caeSChristian Brauner 	NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified");
22024ff66caeSChristian Brauner 	return -EINVAL;
22034ff66caeSChristian Brauner }
22044ff66caeSChristian Brauner 
22051840bb13SThomas Graf static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
22061840bb13SThomas Graf {
22071840bb13SThomas Graf 	if (dev) {
22081840bb13SThomas Graf 		if (tb[IFLA_ADDRESS] &&
22091840bb13SThomas Graf 		    nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
22101840bb13SThomas Graf 			return -EINVAL;
22111840bb13SThomas Graf 
22121840bb13SThomas Graf 		if (tb[IFLA_BROADCAST] &&
22131840bb13SThomas Graf 		    nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
22141840bb13SThomas Graf 			return -EINVAL;
22151840bb13SThomas Graf 	}
22161840bb13SThomas Graf 
2217cf7afbfeSThomas Graf 	if (tb[IFLA_AF_SPEC]) {
2218cf7afbfeSThomas Graf 		struct nlattr *af;
2219cf7afbfeSThomas Graf 		int rem, err;
2220cf7afbfeSThomas Graf 
2221cf7afbfeSThomas Graf 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2222cf7afbfeSThomas Graf 			const struct rtnl_af_ops *af_ops;
2223cf7afbfeSThomas Graf 
22245fa85a09SFlorian Westphal 			rcu_read_lock();
22255fa85a09SFlorian Westphal 			af_ops = rtnl_af_lookup(nla_type(af));
22265fa85a09SFlorian Westphal 			if (!af_ops) {
22275fa85a09SFlorian Westphal 				rcu_read_unlock();
2228cf7afbfeSThomas Graf 				return -EAFNOSUPPORT;
22295fa85a09SFlorian Westphal 			}
2230cf7afbfeSThomas Graf 
22315fa85a09SFlorian Westphal 			if (!af_ops->set_link_af) {
22325fa85a09SFlorian Westphal 				rcu_read_unlock();
2233cf7afbfeSThomas Graf 				return -EOPNOTSUPP;
22345fa85a09SFlorian Westphal 			}
2235cf7afbfeSThomas Graf 
2236cf7afbfeSThomas Graf 			if (af_ops->validate_link_af) {
22376d3a9a68SKurt Van Dijck 				err = af_ops->validate_link_af(dev, af);
22385fa85a09SFlorian Westphal 				if (err < 0) {
22395fa85a09SFlorian Westphal 					rcu_read_unlock();
2240cf7afbfeSThomas Graf 					return err;
2241cf7afbfeSThomas Graf 				}
2242cf7afbfeSThomas Graf 			}
22435fa85a09SFlorian Westphal 
22445fa85a09SFlorian Westphal 			rcu_read_unlock();
22455fa85a09SFlorian Westphal 		}
2246cf7afbfeSThomas Graf 	}
2247cf7afbfeSThomas Graf 
22481840bb13SThomas Graf 	return 0;
22491840bb13SThomas Graf }
22501840bb13SThomas Graf 
2251cc8e27ccSEli Cohen static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
2252cc8e27ccSEli Cohen 				  int guid_type)
2253cc8e27ccSEli Cohen {
2254cc8e27ccSEli Cohen 	const struct net_device_ops *ops = dev->netdev_ops;
2255cc8e27ccSEli Cohen 
2256cc8e27ccSEli Cohen 	return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
2257cc8e27ccSEli Cohen }
2258cc8e27ccSEli Cohen 
2259cc8e27ccSEli Cohen static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
2260cc8e27ccSEli Cohen {
2261cc8e27ccSEli Cohen 	if (dev->type != ARPHRD_INFINIBAND)
2262cc8e27ccSEli Cohen 		return -EOPNOTSUPP;
2263cc8e27ccSEli Cohen 
2264cc8e27ccSEli Cohen 	return handle_infiniband_guid(dev, ivt, guid_type);
2265cc8e27ccSEli Cohen }
2266cc8e27ccSEli Cohen 
22674f7d2cdfSDaniel Borkmann static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
2268c02db8c6SChris Wright {
2269c02db8c6SChris Wright 	const struct net_device_ops *ops = dev->netdev_ops;
22704f7d2cdfSDaniel Borkmann 	int err = -EINVAL;
2271c02db8c6SChris Wright 
22724f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_MAC]) {
22734f7d2cdfSDaniel Borkmann 		struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
22744f7d2cdfSDaniel Borkmann 
2275ff08ddbaSDan Carpenter 		if (ivm->vf >= INT_MAX)
2276ff08ddbaSDan Carpenter 			return -EINVAL;
2277c02db8c6SChris Wright 		err = -EOPNOTSUPP;
2278c02db8c6SChris Wright 		if (ops->ndo_set_vf_mac)
2279c02db8c6SChris Wright 			err = ops->ndo_set_vf_mac(dev, ivm->vf,
2280c02db8c6SChris Wright 						  ivm->mac);
22814f7d2cdfSDaniel Borkmann 		if (err < 0)
22824f7d2cdfSDaniel Borkmann 			return err;
2283c02db8c6SChris Wright 	}
22844f7d2cdfSDaniel Borkmann 
22854f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_VLAN]) {
22864f7d2cdfSDaniel Borkmann 		struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
22874f7d2cdfSDaniel Borkmann 
2288ff08ddbaSDan Carpenter 		if (ivv->vf >= INT_MAX)
2289ff08ddbaSDan Carpenter 			return -EINVAL;
2290c02db8c6SChris Wright 		err = -EOPNOTSUPP;
2291c02db8c6SChris Wright 		if (ops->ndo_set_vf_vlan)
22924f7d2cdfSDaniel Borkmann 			err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
229379aab093SMoshe Shemesh 						   ivv->qos,
229479aab093SMoshe Shemesh 						   htons(ETH_P_8021Q));
229579aab093SMoshe Shemesh 		if (err < 0)
229679aab093SMoshe Shemesh 			return err;
229779aab093SMoshe Shemesh 	}
229879aab093SMoshe Shemesh 
229979aab093SMoshe Shemesh 	if (tb[IFLA_VF_VLAN_LIST]) {
230079aab093SMoshe Shemesh 		struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
230179aab093SMoshe Shemesh 		struct nlattr *attr;
230279aab093SMoshe Shemesh 		int rem, len = 0;
230379aab093SMoshe Shemesh 
230479aab093SMoshe Shemesh 		err = -EOPNOTSUPP;
230579aab093SMoshe Shemesh 		if (!ops->ndo_set_vf_vlan)
230679aab093SMoshe Shemesh 			return err;
230779aab093SMoshe Shemesh 
230879aab093SMoshe Shemesh 		nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
230979aab093SMoshe Shemesh 			if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
231079aab093SMoshe Shemesh 			    nla_len(attr) < NLA_HDRLEN) {
231179aab093SMoshe Shemesh 				return -EINVAL;
231279aab093SMoshe Shemesh 			}
231379aab093SMoshe Shemesh 			if (len >= MAX_VLAN_LIST_LEN)
231479aab093SMoshe Shemesh 				return -EOPNOTSUPP;
231579aab093SMoshe Shemesh 			ivvl[len] = nla_data(attr);
231679aab093SMoshe Shemesh 
231779aab093SMoshe Shemesh 			len++;
231879aab093SMoshe Shemesh 		}
2319fa34cd94SArnd Bergmann 		if (len == 0)
2320fa34cd94SArnd Bergmann 			return -EINVAL;
2321fa34cd94SArnd Bergmann 
2322ff08ddbaSDan Carpenter 		if (ivvl[0]->vf >= INT_MAX)
2323ff08ddbaSDan Carpenter 			return -EINVAL;
232479aab093SMoshe Shemesh 		err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
232579aab093SMoshe Shemesh 					   ivvl[0]->qos, ivvl[0]->vlan_proto);
23264f7d2cdfSDaniel Borkmann 		if (err < 0)
23274f7d2cdfSDaniel Borkmann 			return err;
2328c02db8c6SChris Wright 	}
23294f7d2cdfSDaniel Borkmann 
23304f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_TX_RATE]) {
23314f7d2cdfSDaniel Borkmann 		struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
2332ed616689SSucheta Chakraborty 		struct ifla_vf_info ivf;
23334f7d2cdfSDaniel Borkmann 
2334ff08ddbaSDan Carpenter 		if (ivt->vf >= INT_MAX)
2335ff08ddbaSDan Carpenter 			return -EINVAL;
2336c02db8c6SChris Wright 		err = -EOPNOTSUPP;
2337ed616689SSucheta Chakraborty 		if (ops->ndo_get_vf_config)
23384f7d2cdfSDaniel Borkmann 			err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
23394f7d2cdfSDaniel Borkmann 		if (err < 0)
23404f7d2cdfSDaniel Borkmann 			return err;
23414f7d2cdfSDaniel Borkmann 
2342ed616689SSucheta Chakraborty 		err = -EOPNOTSUPP;
2343ed616689SSucheta Chakraborty 		if (ops->ndo_set_vf_rate)
2344ed616689SSucheta Chakraborty 			err = ops->ndo_set_vf_rate(dev, ivt->vf,
2345ed616689SSucheta Chakraborty 						   ivf.min_tx_rate,
2346c02db8c6SChris Wright 						   ivt->rate);
23474f7d2cdfSDaniel Borkmann 		if (err < 0)
23484f7d2cdfSDaniel Borkmann 			return err;
2349c02db8c6SChris Wright 	}
23504f7d2cdfSDaniel Borkmann 
23514f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_RATE]) {
23524f7d2cdfSDaniel Borkmann 		struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
23534f7d2cdfSDaniel Borkmann 
2354ff08ddbaSDan Carpenter 		if (ivt->vf >= INT_MAX)
2355ff08ddbaSDan Carpenter 			return -EINVAL;
2356ed616689SSucheta Chakraborty 		err = -EOPNOTSUPP;
2357ed616689SSucheta Chakraborty 		if (ops->ndo_set_vf_rate)
2358ed616689SSucheta Chakraborty 			err = ops->ndo_set_vf_rate(dev, ivt->vf,
2359ed616689SSucheta Chakraborty 						   ivt->min_tx_rate,
2360ed616689SSucheta Chakraborty 						   ivt->max_tx_rate);
23614f7d2cdfSDaniel Borkmann 		if (err < 0)
23624f7d2cdfSDaniel Borkmann 			return err;
2363ed616689SSucheta Chakraborty 	}
23644f7d2cdfSDaniel Borkmann 
23654f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_SPOOFCHK]) {
23664f7d2cdfSDaniel Borkmann 		struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
23674f7d2cdfSDaniel Borkmann 
2368ff08ddbaSDan Carpenter 		if (ivs->vf >= INT_MAX)
2369ff08ddbaSDan Carpenter 			return -EINVAL;
23705f8444a3SGreg Rose 		err = -EOPNOTSUPP;
23715f8444a3SGreg Rose 		if (ops->ndo_set_vf_spoofchk)
23725f8444a3SGreg Rose 			err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
23735f8444a3SGreg Rose 						       ivs->setting);
23744f7d2cdfSDaniel Borkmann 		if (err < 0)
23754f7d2cdfSDaniel Borkmann 			return err;
23765f8444a3SGreg Rose 	}
23774f7d2cdfSDaniel Borkmann 
23784f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_LINK_STATE]) {
23794f7d2cdfSDaniel Borkmann 		struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
23804f7d2cdfSDaniel Borkmann 
2381ff08ddbaSDan Carpenter 		if (ivl->vf >= INT_MAX)
2382ff08ddbaSDan Carpenter 			return -EINVAL;
23831d8faf48SRony Efraim 		err = -EOPNOTSUPP;
23841d8faf48SRony Efraim 		if (ops->ndo_set_vf_link_state)
23851d8faf48SRony Efraim 			err = ops->ndo_set_vf_link_state(dev, ivl->vf,
23861d8faf48SRony Efraim 							 ivl->link_state);
23874f7d2cdfSDaniel Borkmann 		if (err < 0)
23884f7d2cdfSDaniel Borkmann 			return err;
23891d8faf48SRony Efraim 	}
23904f7d2cdfSDaniel Borkmann 
23914f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_RSS_QUERY_EN]) {
239201a3d796SVlad Zolotarov 		struct ifla_vf_rss_query_en *ivrssq_en;
239301a3d796SVlad Zolotarov 
239401a3d796SVlad Zolotarov 		err = -EOPNOTSUPP;
23954f7d2cdfSDaniel Borkmann 		ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
2396ff08ddbaSDan Carpenter 		if (ivrssq_en->vf >= INT_MAX)
2397ff08ddbaSDan Carpenter 			return -EINVAL;
239801a3d796SVlad Zolotarov 		if (ops->ndo_set_vf_rss_query_en)
23994f7d2cdfSDaniel Borkmann 			err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
240001a3d796SVlad Zolotarov 							   ivrssq_en->setting);
24014f7d2cdfSDaniel Borkmann 		if (err < 0)
24024f7d2cdfSDaniel Borkmann 			return err;
240301a3d796SVlad Zolotarov 	}
24044f7d2cdfSDaniel Borkmann 
2405dd461d6aSHiroshi Shimamoto 	if (tb[IFLA_VF_TRUST]) {
2406dd461d6aSHiroshi Shimamoto 		struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
2407dd461d6aSHiroshi Shimamoto 
2408ff08ddbaSDan Carpenter 		if (ivt->vf >= INT_MAX)
2409ff08ddbaSDan Carpenter 			return -EINVAL;
2410dd461d6aSHiroshi Shimamoto 		err = -EOPNOTSUPP;
2411dd461d6aSHiroshi Shimamoto 		if (ops->ndo_set_vf_trust)
2412dd461d6aSHiroshi Shimamoto 			err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
2413dd461d6aSHiroshi Shimamoto 		if (err < 0)
2414dd461d6aSHiroshi Shimamoto 			return err;
2415dd461d6aSHiroshi Shimamoto 	}
2416dd461d6aSHiroshi Shimamoto 
2417cc8e27ccSEli Cohen 	if (tb[IFLA_VF_IB_NODE_GUID]) {
2418cc8e27ccSEli Cohen 		struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
2419cc8e27ccSEli Cohen 
2420ff08ddbaSDan Carpenter 		if (ivt->vf >= INT_MAX)
2421ff08ddbaSDan Carpenter 			return -EINVAL;
2422cc8e27ccSEli Cohen 		if (!ops->ndo_set_vf_guid)
2423cc8e27ccSEli Cohen 			return -EOPNOTSUPP;
2424cc8e27ccSEli Cohen 		return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
2425cc8e27ccSEli Cohen 	}
2426cc8e27ccSEli Cohen 
2427cc8e27ccSEli Cohen 	if (tb[IFLA_VF_IB_PORT_GUID]) {
2428cc8e27ccSEli Cohen 		struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
2429cc8e27ccSEli Cohen 
2430ff08ddbaSDan Carpenter 		if (ivt->vf >= INT_MAX)
2431ff08ddbaSDan Carpenter 			return -EINVAL;
2432cc8e27ccSEli Cohen 		if (!ops->ndo_set_vf_guid)
2433cc8e27ccSEli Cohen 			return -EOPNOTSUPP;
2434cc8e27ccSEli Cohen 
2435cc8e27ccSEli Cohen 		return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
2436cc8e27ccSEli Cohen 	}
2437cc8e27ccSEli Cohen 
2438c02db8c6SChris Wright 	return err;
2439c02db8c6SChris Wright }
2440c02db8c6SChris Wright 
244133eaf2a6SDavid Ahern static int do_set_master(struct net_device *dev, int ifindex,
244233eaf2a6SDavid Ahern 			 struct netlink_ext_ack *extack)
2443fbaec0eaSJiri Pirko {
2444898e5061SJiri Pirko 	struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
2445fbaec0eaSJiri Pirko 	const struct net_device_ops *ops;
2446fbaec0eaSJiri Pirko 	int err;
2447fbaec0eaSJiri Pirko 
2448898e5061SJiri Pirko 	if (upper_dev) {
2449898e5061SJiri Pirko 		if (upper_dev->ifindex == ifindex)
2450fbaec0eaSJiri Pirko 			return 0;
2451898e5061SJiri Pirko 		ops = upper_dev->netdev_ops;
2452fbaec0eaSJiri Pirko 		if (ops->ndo_del_slave) {
2453898e5061SJiri Pirko 			err = ops->ndo_del_slave(upper_dev, dev);
2454fbaec0eaSJiri Pirko 			if (err)
2455fbaec0eaSJiri Pirko 				return err;
2456ab92d68fSTaehee Yoo 			netdev_update_lockdep_key(dev);
2457fbaec0eaSJiri Pirko 		} else {
2458fbaec0eaSJiri Pirko 			return -EOPNOTSUPP;
2459fbaec0eaSJiri Pirko 		}
2460fbaec0eaSJiri Pirko 	}
2461fbaec0eaSJiri Pirko 
2462fbaec0eaSJiri Pirko 	if (ifindex) {
2463898e5061SJiri Pirko 		upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
2464898e5061SJiri Pirko 		if (!upper_dev)
2465fbaec0eaSJiri Pirko 			return -EINVAL;
2466898e5061SJiri Pirko 		ops = upper_dev->netdev_ops;
2467fbaec0eaSJiri Pirko 		if (ops->ndo_add_slave) {
246833eaf2a6SDavid Ahern 			err = ops->ndo_add_slave(upper_dev, dev, extack);
2469fbaec0eaSJiri Pirko 			if (err)
2470fbaec0eaSJiri Pirko 				return err;
2471fbaec0eaSJiri Pirko 		} else {
2472fbaec0eaSJiri Pirko 			return -EOPNOTSUPP;
2473fbaec0eaSJiri Pirko 		}
2474fbaec0eaSJiri Pirko 	}
2475fbaec0eaSJiri Pirko 	return 0;
2476fbaec0eaSJiri Pirko }
2477fbaec0eaSJiri Pirko 
247890c325e3SNicolas Dichtel #define DO_SETLINK_MODIFIED	0x01
2479ba998906SNicolas Dichtel /* notify flag means notify + modified. */
2480ba998906SNicolas Dichtel #define DO_SETLINK_NOTIFY	0x03
248190f62cf3SEric W. Biederman static int do_setlink(const struct sk_buff *skb,
248290f62cf3SEric W. Biederman 		      struct net_device *dev, struct ifinfomsg *ifm,
2483ddf9f970SJakub Kicinski 		      struct netlink_ext_ack *extack,
248490c325e3SNicolas Dichtel 		      struct nlattr **tb, char *ifname, int status)
24850157f60cSPatrick McHardy {
2486d314774cSStephen Hemminger 	const struct net_device_ops *ops = dev->netdev_ops;
24870157f60cSPatrick McHardy 	int err;
24880157f60cSPatrick McHardy 
2489644c7eebSEric Dumazet 	err = validate_linkmsg(dev, tb);
2490644c7eebSEric Dumazet 	if (err < 0)
2491644c7eebSEric Dumazet 		return err;
2492644c7eebSEric Dumazet 
24937e4a8d5aSChristian Brauner 	if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_TARGET_NETNSID]) {
24947c4f63baSChristian Brauner 		struct net *net = rtnl_link_get_net_capable(skb, dev_net(dev),
24957c4f63baSChristian Brauner 							    tb, CAP_NET_ADMIN);
2496d8a5ec67SEric W. Biederman 		if (IS_ERR(net)) {
2497d8a5ec67SEric W. Biederman 			err = PTR_ERR(net);
2498d8a5ec67SEric W. Biederman 			goto errout;
2499d8a5ec67SEric W. Biederman 		}
25007c4f63baSChristian Brauner 
2501d8a5ec67SEric W. Biederman 		err = dev_change_net_namespace(dev, net, ifname);
2502d8a5ec67SEric W. Biederman 		put_net(net);
2503d8a5ec67SEric W. Biederman 		if (err)
2504d8a5ec67SEric W. Biederman 			goto errout;
250590c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
2506d8a5ec67SEric W. Biederman 	}
2507d8a5ec67SEric W. Biederman 
25080157f60cSPatrick McHardy 	if (tb[IFLA_MAP]) {
25090157f60cSPatrick McHardy 		struct rtnl_link_ifmap *u_map;
25100157f60cSPatrick McHardy 		struct ifmap k_map;
25110157f60cSPatrick McHardy 
2512d314774cSStephen Hemminger 		if (!ops->ndo_set_config) {
25130157f60cSPatrick McHardy 			err = -EOPNOTSUPP;
25140157f60cSPatrick McHardy 			goto errout;
25150157f60cSPatrick McHardy 		}
25160157f60cSPatrick McHardy 
25170157f60cSPatrick McHardy 		if (!netif_device_present(dev)) {
25180157f60cSPatrick McHardy 			err = -ENODEV;
25190157f60cSPatrick McHardy 			goto errout;
25200157f60cSPatrick McHardy 		}
25210157f60cSPatrick McHardy 
25220157f60cSPatrick McHardy 		u_map = nla_data(tb[IFLA_MAP]);
25230157f60cSPatrick McHardy 		k_map.mem_start = (unsigned long) u_map->mem_start;
25240157f60cSPatrick McHardy 		k_map.mem_end = (unsigned long) u_map->mem_end;
25250157f60cSPatrick McHardy 		k_map.base_addr = (unsigned short) u_map->base_addr;
25260157f60cSPatrick McHardy 		k_map.irq = (unsigned char) u_map->irq;
25270157f60cSPatrick McHardy 		k_map.dma = (unsigned char) u_map->dma;
25280157f60cSPatrick McHardy 		k_map.port = (unsigned char) u_map->port;
25290157f60cSPatrick McHardy 
2530d314774cSStephen Hemminger 		err = ops->ndo_set_config(dev, &k_map);
25310157f60cSPatrick McHardy 		if (err < 0)
25320157f60cSPatrick McHardy 			goto errout;
25330157f60cSPatrick McHardy 
2534ba998906SNicolas Dichtel 		status |= DO_SETLINK_NOTIFY;
25350157f60cSPatrick McHardy 	}
25360157f60cSPatrick McHardy 
25370157f60cSPatrick McHardy 	if (tb[IFLA_ADDRESS]) {
25380157f60cSPatrick McHardy 		struct sockaddr *sa;
25390157f60cSPatrick McHardy 		int len;
25400157f60cSPatrick McHardy 
2541153711f9SWANG Cong 		len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
2542153711f9SWANG Cong 						  sizeof(*sa));
25430157f60cSPatrick McHardy 		sa = kmalloc(len, GFP_KERNEL);
25440157f60cSPatrick McHardy 		if (!sa) {
25450157f60cSPatrick McHardy 			err = -ENOMEM;
25460157f60cSPatrick McHardy 			goto errout;
25470157f60cSPatrick McHardy 		}
25480157f60cSPatrick McHardy 		sa->sa_family = dev->type;
25490157f60cSPatrick McHardy 		memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
25500157f60cSPatrick McHardy 		       dev->addr_len);
25513a37a963SPetr Machata 		err = dev_set_mac_address(dev, sa, extack);
25520157f60cSPatrick McHardy 		kfree(sa);
25530157f60cSPatrick McHardy 		if (err)
25540157f60cSPatrick McHardy 			goto errout;
255590c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
25560157f60cSPatrick McHardy 	}
25570157f60cSPatrick McHardy 
25580157f60cSPatrick McHardy 	if (tb[IFLA_MTU]) {
25597a4c53beSStephen Hemminger 		err = dev_set_mtu_ext(dev, nla_get_u32(tb[IFLA_MTU]), extack);
25600157f60cSPatrick McHardy 		if (err < 0)
25610157f60cSPatrick McHardy 			goto errout;
256290c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
25630157f60cSPatrick McHardy 	}
25640157f60cSPatrick McHardy 
2565cbda10faSVlad Dogaru 	if (tb[IFLA_GROUP]) {
2566cbda10faSVlad Dogaru 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
2567ba998906SNicolas Dichtel 		status |= DO_SETLINK_NOTIFY;
2568cbda10faSVlad Dogaru 	}
2569cbda10faSVlad Dogaru 
25700157f60cSPatrick McHardy 	/*
25710157f60cSPatrick McHardy 	 * Interface selected by interface index but interface
25720157f60cSPatrick McHardy 	 * name provided implies that a name change has been
25730157f60cSPatrick McHardy 	 * requested.
25740157f60cSPatrick McHardy 	 */
25750157f60cSPatrick McHardy 	if (ifm->ifi_index > 0 && ifname[0]) {
25760157f60cSPatrick McHardy 		err = dev_change_name(dev, ifname);
25770157f60cSPatrick McHardy 		if (err < 0)
25780157f60cSPatrick McHardy 			goto errout;
257990c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
25800157f60cSPatrick McHardy 	}
25810157f60cSPatrick McHardy 
25820b815a1aSStephen Hemminger 	if (tb[IFLA_IFALIAS]) {
25830b815a1aSStephen Hemminger 		err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
25840b815a1aSStephen Hemminger 				    nla_len(tb[IFLA_IFALIAS]));
25850b815a1aSStephen Hemminger 		if (err < 0)
25860b815a1aSStephen Hemminger 			goto errout;
2587ba998906SNicolas Dichtel 		status |= DO_SETLINK_NOTIFY;
25880b815a1aSStephen Hemminger 	}
25890b815a1aSStephen Hemminger 
25900157f60cSPatrick McHardy 	if (tb[IFLA_BROADCAST]) {
25910157f60cSPatrick McHardy 		nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
2592e7c3273eSJiri Pirko 		call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
25930157f60cSPatrick McHardy 	}
25940157f60cSPatrick McHardy 
25950157f60cSPatrick McHardy 	if (ifm->ifi_flags || ifm->ifi_change) {
2596567c5e13SPetr Machata 		err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
2597567c5e13SPetr Machata 				       extack);
25985f9021cfSJohannes Berg 		if (err < 0)
25995f9021cfSJohannes Berg 			goto errout;
26000157f60cSPatrick McHardy 	}
26010157f60cSPatrick McHardy 
2602fbaec0eaSJiri Pirko 	if (tb[IFLA_MASTER]) {
260333eaf2a6SDavid Ahern 		err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
2604fbaec0eaSJiri Pirko 		if (err)
2605fbaec0eaSJiri Pirko 			goto errout;
260690c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
2607fbaec0eaSJiri Pirko 	}
2608fbaec0eaSJiri Pirko 
26099a57247fSJiri Pirko 	if (tb[IFLA_CARRIER]) {
26109a57247fSJiri Pirko 		err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
26119a57247fSJiri Pirko 		if (err)
26129a57247fSJiri Pirko 			goto errout;
261390c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
26149a57247fSJiri Pirko 	}
26159a57247fSJiri Pirko 
26165d1180fcSNicolas Dichtel 	if (tb[IFLA_TXQLEN]) {
26170cd29503SAlexey Dobriyan 		unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]);
26185d1180fcSNicolas Dichtel 
26196a643ddbSCong Wang 		err = dev_change_tx_queue_len(dev, value);
26206a643ddbSCong Wang 		if (err)
262108294a26SJason Wang 			goto errout;
26222d7f669bSXin Long 		status |= DO_SETLINK_MODIFIED;
262308294a26SJason Wang 	}
26240157f60cSPatrick McHardy 
262546e6b992SStephen Hemminger 	if (tb[IFLA_GSO_MAX_SIZE]) {
262646e6b992SStephen Hemminger 		u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]);
262746e6b992SStephen Hemminger 
262846e6b992SStephen Hemminger 		if (max_size > GSO_MAX_SIZE) {
262946e6b992SStephen Hemminger 			err = -EINVAL;
263046e6b992SStephen Hemminger 			goto errout;
263146e6b992SStephen Hemminger 		}
263246e6b992SStephen Hemminger 
263346e6b992SStephen Hemminger 		if (dev->gso_max_size ^ max_size) {
263446e6b992SStephen Hemminger 			netif_set_gso_max_size(dev, max_size);
263546e6b992SStephen Hemminger 			status |= DO_SETLINK_MODIFIED;
263646e6b992SStephen Hemminger 		}
263746e6b992SStephen Hemminger 	}
263846e6b992SStephen Hemminger 
263946e6b992SStephen Hemminger 	if (tb[IFLA_GSO_MAX_SEGS]) {
264046e6b992SStephen Hemminger 		u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
264146e6b992SStephen Hemminger 
264246e6b992SStephen Hemminger 		if (max_segs > GSO_MAX_SEGS) {
264346e6b992SStephen Hemminger 			err = -EINVAL;
264446e6b992SStephen Hemminger 			goto errout;
264546e6b992SStephen Hemminger 		}
264646e6b992SStephen Hemminger 
264746e6b992SStephen Hemminger 		if (dev->gso_max_segs ^ max_segs) {
264846e6b992SStephen Hemminger 			dev->gso_max_segs = max_segs;
264946e6b992SStephen Hemminger 			status |= DO_SETLINK_MODIFIED;
265046e6b992SStephen Hemminger 		}
265146e6b992SStephen Hemminger 	}
265246e6b992SStephen Hemminger 
26530157f60cSPatrick McHardy 	if (tb[IFLA_OPERSTATE])
265493b2d4a2SDavid S. Miller 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
26550157f60cSPatrick McHardy 
26560157f60cSPatrick McHardy 	if (tb[IFLA_LINKMODE]) {
26571889b0e7SNicolas Dichtel 		unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
26581889b0e7SNicolas Dichtel 
26590157f60cSPatrick McHardy 		write_lock_bh(&dev_base_lock);
26601889b0e7SNicolas Dichtel 		if (dev->link_mode ^ value)
2661ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
26621889b0e7SNicolas Dichtel 		dev->link_mode = value;
266393b2d4a2SDavid S. Miller 		write_unlock_bh(&dev_base_lock);
26640157f60cSPatrick McHardy 	}
26650157f60cSPatrick McHardy 
2666c02db8c6SChris Wright 	if (tb[IFLA_VFINFO_LIST]) {
26674f7d2cdfSDaniel Borkmann 		struct nlattr *vfinfo[IFLA_VF_MAX + 1];
2668c02db8c6SChris Wright 		struct nlattr *attr;
2669c02db8c6SChris Wright 		int rem;
26704f7d2cdfSDaniel Borkmann 
2671c02db8c6SChris Wright 		nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
26724f7d2cdfSDaniel Borkmann 			if (nla_type(attr) != IFLA_VF_INFO ||
26734f7d2cdfSDaniel Borkmann 			    nla_len(attr) < NLA_HDRLEN) {
2674253683bbSDavid Howells 				err = -EINVAL;
2675c02db8c6SChris Wright 				goto errout;
2676253683bbSDavid Howells 			}
26778cb08174SJohannes Berg 			err = nla_parse_nested_deprecated(vfinfo, IFLA_VF_MAX,
26788cb08174SJohannes Berg 							  attr,
26798cb08174SJohannes Berg 							  ifla_vf_policy,
26808cb08174SJohannes Berg 							  NULL);
26814f7d2cdfSDaniel Borkmann 			if (err < 0)
26824f7d2cdfSDaniel Borkmann 				goto errout;
26834f7d2cdfSDaniel Borkmann 			err = do_setvfinfo(dev, vfinfo);
2684ebc08a6fSWilliams, Mitch A 			if (err < 0)
2685ebc08a6fSWilliams, Mitch A 				goto errout;
2686ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
2687ebc08a6fSWilliams, Mitch A 		}
2688ebc08a6fSWilliams, Mitch A 	}
26890157f60cSPatrick McHardy 	err = 0;
26900157f60cSPatrick McHardy 
269157b61080SScott Feldman 	if (tb[IFLA_VF_PORTS]) {
269257b61080SScott Feldman 		struct nlattr *port[IFLA_PORT_MAX+1];
269357b61080SScott Feldman 		struct nlattr *attr;
269457b61080SScott Feldman 		int vf;
269557b61080SScott Feldman 		int rem;
269657b61080SScott Feldman 
269757b61080SScott Feldman 		err = -EOPNOTSUPP;
269857b61080SScott Feldman 		if (!ops->ndo_set_vf_port)
269957b61080SScott Feldman 			goto errout;
270057b61080SScott Feldman 
270157b61080SScott Feldman 		nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
2702035d210fSDaniel Borkmann 			if (nla_type(attr) != IFLA_VF_PORT ||
2703035d210fSDaniel Borkmann 			    nla_len(attr) < NLA_HDRLEN) {
2704035d210fSDaniel Borkmann 				err = -EINVAL;
2705035d210fSDaniel Borkmann 				goto errout;
2706035d210fSDaniel Borkmann 			}
27078cb08174SJohannes Berg 			err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
27088cb08174SJohannes Berg 							  attr,
27098cb08174SJohannes Berg 							  ifla_port_policy,
27108cb08174SJohannes Berg 							  NULL);
271157b61080SScott Feldman 			if (err < 0)
271257b61080SScott Feldman 				goto errout;
271357b61080SScott Feldman 			if (!port[IFLA_PORT_VF]) {
271457b61080SScott Feldman 				err = -EOPNOTSUPP;
271557b61080SScott Feldman 				goto errout;
271657b61080SScott Feldman 			}
271757b61080SScott Feldman 			vf = nla_get_u32(port[IFLA_PORT_VF]);
271857b61080SScott Feldman 			err = ops->ndo_set_vf_port(dev, vf, port);
271957b61080SScott Feldman 			if (err < 0)
272057b61080SScott Feldman 				goto errout;
2721ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
272257b61080SScott Feldman 		}
272357b61080SScott Feldman 	}
272457b61080SScott Feldman 	err = 0;
272557b61080SScott Feldman 
272657b61080SScott Feldman 	if (tb[IFLA_PORT_SELF]) {
272757b61080SScott Feldman 		struct nlattr *port[IFLA_PORT_MAX+1];
272857b61080SScott Feldman 
27298cb08174SJohannes Berg 		err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
27308cb08174SJohannes Berg 						  tb[IFLA_PORT_SELF],
27318cb08174SJohannes Berg 						  ifla_port_policy, NULL);
273257b61080SScott Feldman 		if (err < 0)
273357b61080SScott Feldman 			goto errout;
273457b61080SScott Feldman 
273557b61080SScott Feldman 		err = -EOPNOTSUPP;
273657b61080SScott Feldman 		if (ops->ndo_set_vf_port)
273757b61080SScott Feldman 			err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
273857b61080SScott Feldman 		if (err < 0)
273957b61080SScott Feldman 			goto errout;
2740ba998906SNicolas Dichtel 		status |= DO_SETLINK_NOTIFY;
274157b61080SScott Feldman 	}
2742f8ff182cSThomas Graf 
2743f8ff182cSThomas Graf 	if (tb[IFLA_AF_SPEC]) {
2744f8ff182cSThomas Graf 		struct nlattr *af;
2745f8ff182cSThomas Graf 		int rem;
2746f8ff182cSThomas Graf 
2747f8ff182cSThomas Graf 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2748f8ff182cSThomas Graf 			const struct rtnl_af_ops *af_ops;
2749f8ff182cSThomas Graf 
27505fa85a09SFlorian Westphal 			rcu_read_lock();
27515fa85a09SFlorian Westphal 
2752058c8d59SGustavo A. R. Silva 			BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af))));
2753f8ff182cSThomas Graf 
2754cf7afbfeSThomas Graf 			err = af_ops->set_link_af(dev, af);
27555fa85a09SFlorian Westphal 			if (err < 0) {
27565fa85a09SFlorian Westphal 				rcu_read_unlock();
2757f8ff182cSThomas Graf 				goto errout;
27585fa85a09SFlorian Westphal 			}
2759f8ff182cSThomas Graf 
27605fa85a09SFlorian Westphal 			rcu_read_unlock();
2761ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
2762f8ff182cSThomas Graf 		}
2763f8ff182cSThomas Graf 	}
276457b61080SScott Feldman 	err = 0;
276557b61080SScott Feldman 
276688d6378bSAnuradha Karuppiah 	if (tb[IFLA_PROTO_DOWN]) {
276788d6378bSAnuradha Karuppiah 		err = dev_change_proto_down(dev,
276888d6378bSAnuradha Karuppiah 					    nla_get_u8(tb[IFLA_PROTO_DOWN]));
276988d6378bSAnuradha Karuppiah 		if (err)
277088d6378bSAnuradha Karuppiah 			goto errout;
277188d6378bSAnuradha Karuppiah 		status |= DO_SETLINK_NOTIFY;
277288d6378bSAnuradha Karuppiah 	}
277388d6378bSAnuradha Karuppiah 
2774d1fdd913SBrenden Blanco 	if (tb[IFLA_XDP]) {
2775d1fdd913SBrenden Blanco 		struct nlattr *xdp[IFLA_XDP_MAX + 1];
277685de8576SDaniel Borkmann 		u32 xdp_flags = 0;
2777d1fdd913SBrenden Blanco 
27788cb08174SJohannes Berg 		err = nla_parse_nested_deprecated(xdp, IFLA_XDP_MAX,
27798cb08174SJohannes Berg 						  tb[IFLA_XDP],
2780fceb6435SJohannes Berg 						  ifla_xdp_policy, NULL);
2781d1fdd913SBrenden Blanco 		if (err < 0)
2782d1fdd913SBrenden Blanco 			goto errout;
2783d1fdd913SBrenden Blanco 
278458038695SMartin KaFai Lau 		if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
2785262d8625SBrenden Blanco 			err = -EINVAL;
2786262d8625SBrenden Blanco 			goto errout;
2787262d8625SBrenden Blanco 		}
278885de8576SDaniel Borkmann 
278985de8576SDaniel Borkmann 		if (xdp[IFLA_XDP_FLAGS]) {
279085de8576SDaniel Borkmann 			xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
279185de8576SDaniel Borkmann 			if (xdp_flags & ~XDP_FLAGS_MASK) {
279285de8576SDaniel Borkmann 				err = -EINVAL;
279385de8576SDaniel Borkmann 				goto errout;
279485de8576SDaniel Borkmann 			}
2795ee5d032fSJakub Kicinski 			if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) {
27960489df9aSDaniel Borkmann 				err = -EINVAL;
27970489df9aSDaniel Borkmann 				goto errout;
27980489df9aSDaniel Borkmann 			}
279985de8576SDaniel Borkmann 		}
280085de8576SDaniel Borkmann 
2801d1fdd913SBrenden Blanco 		if (xdp[IFLA_XDP_FD]) {
2802ddf9f970SJakub Kicinski 			err = dev_change_xdp_fd(dev, extack,
280385de8576SDaniel Borkmann 						nla_get_s32(xdp[IFLA_XDP_FD]),
280485de8576SDaniel Borkmann 						xdp_flags);
2805d1fdd913SBrenden Blanco 			if (err)
2806d1fdd913SBrenden Blanco 				goto errout;
2807d1fdd913SBrenden Blanco 			status |= DO_SETLINK_NOTIFY;
2808d1fdd913SBrenden Blanco 		}
2809d1fdd913SBrenden Blanco 	}
2810d1fdd913SBrenden Blanco 
28110157f60cSPatrick McHardy errout:
2812ba998906SNicolas Dichtel 	if (status & DO_SETLINK_MODIFIED) {
281364ff90ccSXin Long 		if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY)
2814ba998906SNicolas Dichtel 			netdev_state_change(dev);
2815ba998906SNicolas Dichtel 
2816ba998906SNicolas Dichtel 		if (err < 0)
2817e87cc472SJoe 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",
2818e87cc472SJoe Perches 					     dev->name);
2819ba998906SNicolas Dichtel 	}
28200157f60cSPatrick McHardy 
28210157f60cSPatrick McHardy 	return err;
28220157f60cSPatrick McHardy }
28230157f60cSPatrick McHardy 
2824cc6090e9SJiri Pirko static struct net_device *rtnl_dev_get(struct net *net,
2825cc6090e9SJiri Pirko 				       struct nlattr *ifname_attr,
282676c9ac0eSJiri Pirko 				       struct nlattr *altifname_attr,
2827cc6090e9SJiri Pirko 				       char *ifname)
2828cc6090e9SJiri Pirko {
282976c9ac0eSJiri Pirko 	char buffer[ALTIFNAMSIZ];
2830cc6090e9SJiri Pirko 
2831cc6090e9SJiri Pirko 	if (!ifname) {
2832cc6090e9SJiri Pirko 		ifname = buffer;
2833cc6090e9SJiri Pirko 		if (ifname_attr)
2834cc6090e9SJiri Pirko 			nla_strlcpy(ifname, ifname_attr, IFNAMSIZ);
283576c9ac0eSJiri Pirko 		else if (altifname_attr)
283676c9ac0eSJiri Pirko 			nla_strlcpy(ifname, altifname_attr, ALTIFNAMSIZ);
2837cc6090e9SJiri Pirko 		else
2838cc6090e9SJiri Pirko 			return NULL;
2839cc6090e9SJiri Pirko 	}
2840cc6090e9SJiri Pirko 
2841cc6090e9SJiri Pirko 	return __dev_get_by_name(net, ifname);
2842cc6090e9SJiri Pirko }
2843cc6090e9SJiri Pirko 
2844c21ef3e3SDavid Ahern static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2845c21ef3e3SDavid Ahern 			struct netlink_ext_ack *extack)
2846da5e0494SThomas Graf {
28473b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
2848da5e0494SThomas Graf 	struct ifinfomsg *ifm;
2849da5e0494SThomas Graf 	struct net_device *dev;
28500157f60cSPatrick McHardy 	int err;
2851da5e0494SThomas Graf 	struct nlattr *tb[IFLA_MAX+1];
28521da177e4SLinus Torvalds 	char ifname[IFNAMSIZ];
28531da177e4SLinus Torvalds 
28548cb08174SJohannes Berg 	err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
28558cb08174SJohannes Berg 				     ifla_policy, extack);
2856da5e0494SThomas Graf 	if (err < 0)
2857da5e0494SThomas Graf 		goto errout;
28581da177e4SLinus Torvalds 
28594ff66caeSChristian Brauner 	err = rtnl_ensure_unique_netns(tb, extack, false);
28604ff66caeSChristian Brauner 	if (err < 0)
28614ff66caeSChristian Brauner 		goto errout;
28624ff66caeSChristian Brauner 
28635176f91eSThomas Graf 	if (tb[IFLA_IFNAME])
28645176f91eSThomas Graf 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
286578e5b891SPatrick McHardy 	else
286678e5b891SPatrick McHardy 		ifname[0] = '\0';
28671da177e4SLinus Torvalds 
28681da177e4SLinus Torvalds 	err = -EINVAL;
2869da5e0494SThomas Graf 	ifm = nlmsg_data(nlh);
287051055be8SPatrick McHardy 	if (ifm->ifi_index > 0)
2871a3d12891SEric Dumazet 		dev = __dev_get_by_index(net, ifm->ifi_index);
287276c9ac0eSJiri Pirko 	else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
287376c9ac0eSJiri Pirko 		dev = rtnl_dev_get(net, NULL, tb[IFLA_ALT_IFNAME], ifname);
2874da5e0494SThomas Graf 	else
2875da5e0494SThomas Graf 		goto errout;
28761da177e4SLinus Torvalds 
2877da5e0494SThomas Graf 	if (dev == NULL) {
2878da5e0494SThomas Graf 		err = -ENODEV;
2879da5e0494SThomas Graf 		goto errout;
2880da5e0494SThomas Graf 	}
28811da177e4SLinus Torvalds 
2882ddf9f970SJakub Kicinski 	err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0);
2883da5e0494SThomas Graf errout:
28841da177e4SLinus Torvalds 	return err;
28851da177e4SLinus Torvalds }
28861da177e4SLinus Torvalds 
288766400d54SWANG Cong static int rtnl_group_dellink(const struct net *net, int group)
288866400d54SWANG Cong {
288966400d54SWANG Cong 	struct net_device *dev, *aux;
289066400d54SWANG Cong 	LIST_HEAD(list_kill);
289166400d54SWANG Cong 	bool found = false;
289266400d54SWANG Cong 
289366400d54SWANG Cong 	if (!group)
289466400d54SWANG Cong 		return -EPERM;
289566400d54SWANG Cong 
289666400d54SWANG Cong 	for_each_netdev(net, dev) {
289766400d54SWANG Cong 		if (dev->group == group) {
289866400d54SWANG Cong 			const struct rtnl_link_ops *ops;
289966400d54SWANG Cong 
290066400d54SWANG Cong 			found = true;
290166400d54SWANG Cong 			ops = dev->rtnl_link_ops;
290266400d54SWANG Cong 			if (!ops || !ops->dellink)
290366400d54SWANG Cong 				return -EOPNOTSUPP;
290466400d54SWANG Cong 		}
290566400d54SWANG Cong 	}
290666400d54SWANG Cong 
290766400d54SWANG Cong 	if (!found)
290866400d54SWANG Cong 		return -ENODEV;
290966400d54SWANG Cong 
291066400d54SWANG Cong 	for_each_netdev_safe(net, dev, aux) {
291166400d54SWANG Cong 		if (dev->group == group) {
291266400d54SWANG Cong 			const struct rtnl_link_ops *ops;
291366400d54SWANG Cong 
291466400d54SWANG Cong 			ops = dev->rtnl_link_ops;
291566400d54SWANG Cong 			ops->dellink(dev, &list_kill);
291666400d54SWANG Cong 		}
291766400d54SWANG Cong 	}
291866400d54SWANG Cong 	unregister_netdevice_many(&list_kill);
291966400d54SWANG Cong 
292066400d54SWANG Cong 	return 0;
292166400d54SWANG Cong }
292266400d54SWANG Cong 
2923614732eaSThomas Graf int rtnl_delete_link(struct net_device *dev)
2924614732eaSThomas Graf {
2925614732eaSThomas Graf 	const struct rtnl_link_ops *ops;
2926614732eaSThomas Graf 	LIST_HEAD(list_kill);
2927614732eaSThomas Graf 
2928614732eaSThomas Graf 	ops = dev->rtnl_link_ops;
2929614732eaSThomas Graf 	if (!ops || !ops->dellink)
2930614732eaSThomas Graf 		return -EOPNOTSUPP;
2931614732eaSThomas Graf 
2932614732eaSThomas Graf 	ops->dellink(dev, &list_kill);
2933614732eaSThomas Graf 	unregister_netdevice_many(&list_kill);
2934614732eaSThomas Graf 
2935614732eaSThomas Graf 	return 0;
2936614732eaSThomas Graf }
2937614732eaSThomas Graf EXPORT_SYMBOL_GPL(rtnl_delete_link);
2938614732eaSThomas Graf 
2939c21ef3e3SDavid Ahern static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
2940c21ef3e3SDavid Ahern 			struct netlink_ext_ack *extack)
294138f7b870SPatrick McHardy {
29423b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
2943b61ad68aSChristian Brauner 	struct net *tgt_net = net;
2944b61ad68aSChristian Brauner 	struct net_device *dev = NULL;
294538f7b870SPatrick McHardy 	struct ifinfomsg *ifm;
294638f7b870SPatrick McHardy 	struct nlattr *tb[IFLA_MAX+1];
294738f7b870SPatrick McHardy 	int err;
2948b61ad68aSChristian Brauner 	int netnsid = -1;
294938f7b870SPatrick McHardy 
29508cb08174SJohannes Berg 	err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
29518cb08174SJohannes Berg 				     ifla_policy, extack);
295238f7b870SPatrick McHardy 	if (err < 0)
295338f7b870SPatrick McHardy 		return err;
295438f7b870SPatrick McHardy 
29554ff66caeSChristian Brauner 	err = rtnl_ensure_unique_netns(tb, extack, true);
29564ff66caeSChristian Brauner 	if (err < 0)
29574ff66caeSChristian Brauner 		return err;
29584ff66caeSChristian Brauner 
29597e4a8d5aSChristian Brauner 	if (tb[IFLA_TARGET_NETNSID]) {
29607e4a8d5aSChristian Brauner 		netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
2961c383edc4SChristian Brauner 		tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
2962b61ad68aSChristian Brauner 		if (IS_ERR(tgt_net))
2963b61ad68aSChristian Brauner 			return PTR_ERR(tgt_net);
2964b61ad68aSChristian Brauner 	}
2965b61ad68aSChristian Brauner 
2966b61ad68aSChristian Brauner 	err = -EINVAL;
296738f7b870SPatrick McHardy 	ifm = nlmsg_data(nlh);
296838f7b870SPatrick McHardy 	if (ifm->ifi_index > 0)
2969b61ad68aSChristian Brauner 		dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
297076c9ac0eSJiri Pirko 	else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
297176c9ac0eSJiri Pirko 		dev = rtnl_dev_get(net, tb[IFLA_IFNAME],
297276c9ac0eSJiri Pirko 				   tb[IFLA_ALT_IFNAME], NULL);
297366400d54SWANG Cong 	else if (tb[IFLA_GROUP])
2974b61ad68aSChristian Brauner 		err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP]));
297538f7b870SPatrick McHardy 	else
2976b61ad68aSChristian Brauner 		goto out;
297738f7b870SPatrick McHardy 
2978b61ad68aSChristian Brauner 	if (!dev) {
2979b61ad68aSChristian Brauner 		if (tb[IFLA_IFNAME] || ifm->ifi_index > 0)
2980b61ad68aSChristian Brauner 			err = -ENODEV;
298138f7b870SPatrick McHardy 
2982b61ad68aSChristian Brauner 		goto out;
2983b61ad68aSChristian Brauner 	}
2984b61ad68aSChristian Brauner 
2985b61ad68aSChristian Brauner 	err = rtnl_delete_link(dev);
2986b61ad68aSChristian Brauner 
2987b61ad68aSChristian Brauner out:
2988b61ad68aSChristian Brauner 	if (netnsid >= 0)
2989b61ad68aSChristian Brauner 		put_net(tgt_net);
2990b61ad68aSChristian Brauner 
2991b61ad68aSChristian Brauner 	return err;
299238f7b870SPatrick McHardy }
299338f7b870SPatrick McHardy 
29943729d502SPatrick McHardy int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
29953729d502SPatrick McHardy {
29963729d502SPatrick McHardy 	unsigned int old_flags;
29973729d502SPatrick McHardy 	int err;
29983729d502SPatrick McHardy 
29993729d502SPatrick McHardy 	old_flags = dev->flags;
30003729d502SPatrick McHardy 	if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
30016d040321SPetr Machata 		err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
30026d040321SPetr Machata 					 NULL);
30033729d502SPatrick McHardy 		if (err < 0)
30043729d502SPatrick McHardy 			return err;
30053729d502SPatrick McHardy 	}
30063729d502SPatrick McHardy 
30078d356b89SRoopa Prabhu 	if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) {
300856a49d70SRoopa Prabhu 		__dev_notify_flags(dev, old_flags, (old_flags ^ dev->flags));
30098d356b89SRoopa Prabhu 	} else {
30103729d502SPatrick McHardy 		dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
3011a528c219SNicolas Dichtel 		__dev_notify_flags(dev, old_flags, ~0U);
30128d356b89SRoopa Prabhu 	}
30133729d502SPatrick McHardy 	return 0;
30143729d502SPatrick McHardy }
30153729d502SPatrick McHardy EXPORT_SYMBOL(rtnl_configure_link);
30163729d502SPatrick McHardy 
3017d0522f1cSDavid Ahern struct net_device *rtnl_create_link(struct net *net, const char *ifname,
3018d0522f1cSDavid Ahern 				    unsigned char name_assign_type,
3019d0522f1cSDavid Ahern 				    const struct rtnl_link_ops *ops,
3020d0522f1cSDavid Ahern 				    struct nlattr *tb[],
3021d0522f1cSDavid Ahern 				    struct netlink_ext_ack *extack)
3022e7199288SPavel Emelianov {
3023e7199288SPavel Emelianov 	struct net_device *dev;
3024d40156aaSJiri Pirko 	unsigned int num_tx_queues = 1;
3025d40156aaSJiri Pirko 	unsigned int num_rx_queues = 1;
3026e7199288SPavel Emelianov 
302776ff5cc9SJiri Pirko 	if (tb[IFLA_NUM_TX_QUEUES])
302876ff5cc9SJiri Pirko 		num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
302976ff5cc9SJiri Pirko 	else if (ops->get_num_tx_queues)
3030d40156aaSJiri Pirko 		num_tx_queues = ops->get_num_tx_queues();
303176ff5cc9SJiri Pirko 
303276ff5cc9SJiri Pirko 	if (tb[IFLA_NUM_RX_QUEUES])
303376ff5cc9SJiri Pirko 		num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
303476ff5cc9SJiri Pirko 	else if (ops->get_num_rx_queues)
3035d40156aaSJiri Pirko 		num_rx_queues = ops->get_num_rx_queues();
3036efacb309Sstephen hemminger 
3037d0522f1cSDavid Ahern 	if (num_tx_queues < 1 || num_tx_queues > 4096) {
3038d0522f1cSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid number of transmit queues");
30390e1d6ecaSEric Dumazet 		return ERR_PTR(-EINVAL);
3040d0522f1cSDavid Ahern 	}
30410e1d6ecaSEric Dumazet 
3042d0522f1cSDavid Ahern 	if (num_rx_queues < 1 || num_rx_queues > 4096) {
3043d0522f1cSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid number of receive queues");
30440e1d6ecaSEric Dumazet 		return ERR_PTR(-EINVAL);
3045d0522f1cSDavid Ahern 	}
30460e1d6ecaSEric Dumazet 
30475517750fSTom Gundersen 	dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
3048c835a677STom Gundersen 			       ops->setup, num_tx_queues, num_rx_queues);
3049e7199288SPavel Emelianov 	if (!dev)
3050d1892e4eSTobias Klauser 		return ERR_PTR(-ENOMEM);
3051e7199288SPavel Emelianov 
305281adee47SEric W. Biederman 	dev_net_set(dev, net);
305381adee47SEric W. Biederman 	dev->rtnl_link_ops = ops;
30543729d502SPatrick McHardy 	dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
305581adee47SEric W. Biederman 
3056e7199288SPavel Emelianov 	if (tb[IFLA_MTU])
3057e7199288SPavel Emelianov 		dev->mtu = nla_get_u32(tb[IFLA_MTU]);
30582afb9b53SJiri Pirko 	if (tb[IFLA_ADDRESS]) {
3059e7199288SPavel Emelianov 		memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
3060e7199288SPavel Emelianov 				nla_len(tb[IFLA_ADDRESS]));
30612afb9b53SJiri Pirko 		dev->addr_assign_type = NET_ADDR_SET;
30622afb9b53SJiri Pirko 	}
3063e7199288SPavel Emelianov 	if (tb[IFLA_BROADCAST])
3064e7199288SPavel Emelianov 		memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
3065e7199288SPavel Emelianov 				nla_len(tb[IFLA_BROADCAST]));
3066e7199288SPavel Emelianov 	if (tb[IFLA_TXQLEN])
3067e7199288SPavel Emelianov 		dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
3068e7199288SPavel Emelianov 	if (tb[IFLA_OPERSTATE])
306993b2d4a2SDavid S. Miller 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
3070e7199288SPavel Emelianov 	if (tb[IFLA_LINKMODE])
3071e7199288SPavel Emelianov 		dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
3072ffa934f1SPatrick McHardy 	if (tb[IFLA_GROUP])
3073ffa934f1SPatrick McHardy 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
307446e6b992SStephen Hemminger 	if (tb[IFLA_GSO_MAX_SIZE])
307546e6b992SStephen Hemminger 		netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE]));
307646e6b992SStephen Hemminger 	if (tb[IFLA_GSO_MAX_SEGS])
3077a0b586faSStephen Hemminger 		dev->gso_max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
3078e7199288SPavel Emelianov 
3079e7199288SPavel Emelianov 	return dev;
3080e7199288SPavel Emelianov }
3081e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_create_link);
3082e7199288SPavel Emelianov 
308390f62cf3SEric W. Biederman static int rtnl_group_changelink(const struct sk_buff *skb,
308490f62cf3SEric W. Biederman 		struct net *net, int group,
3085e7ed828fSVlad Dogaru 		struct ifinfomsg *ifm,
3086ddf9f970SJakub Kicinski 		struct netlink_ext_ack *extack,
3087e7ed828fSVlad Dogaru 		struct nlattr **tb)
3088e7ed828fSVlad Dogaru {
3089d079535dSWANG Cong 	struct net_device *dev, *aux;
3090e7ed828fSVlad Dogaru 	int err;
3091e7ed828fSVlad Dogaru 
3092d079535dSWANG Cong 	for_each_netdev_safe(net, dev, aux) {
3093e7ed828fSVlad Dogaru 		if (dev->group == group) {
3094ddf9f970SJakub Kicinski 			err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0);
3095e7ed828fSVlad Dogaru 			if (err < 0)
3096e7ed828fSVlad Dogaru 				return err;
3097e7ed828fSVlad Dogaru 		}
3098e7ed828fSVlad Dogaru 	}
3099e7ed828fSVlad Dogaru 
3100e7ed828fSVlad Dogaru 	return 0;
3101e7ed828fSVlad Dogaru }
3102e7ed828fSVlad Dogaru 
3103a2939745SJakub Kicinski static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3104a2939745SJakub Kicinski 			  struct nlattr **attr, struct netlink_ext_ack *extack)
310538f7b870SPatrick McHardy {
3106420d0318SJakub Kicinski 	struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1];
3107420d0318SJakub Kicinski 	unsigned char name_assign_type = NET_NAME_USER;
3108420d0318SJakub Kicinski 	struct nlattr *linkinfo[IFLA_INFO_MAX + 1];
3109420d0318SJakub Kicinski 	const struct rtnl_link_ops *m_ops = NULL;
3110420d0318SJakub Kicinski 	struct net_device *master_dev = NULL;
31113b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
311238f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops;
311338f7b870SPatrick McHardy 	struct nlattr *tb[IFLA_MAX + 1];
3114420d0318SJakub Kicinski 	struct net *dest_net, *link_net;
3115420d0318SJakub Kicinski 	struct nlattr **slave_data;
3116420d0318SJakub Kicinski 	char kind[MODULE_NAME_LEN];
3117420d0318SJakub Kicinski 	struct net_device *dev;
3118420d0318SJakub Kicinski 	struct ifinfomsg *ifm;
3119420d0318SJakub Kicinski 	char ifname[IFNAMSIZ];
3120420d0318SJakub Kicinski 	struct nlattr **data;
312138f7b870SPatrick McHardy 	int err;
312238f7b870SPatrick McHardy 
312395a5afcaSJohannes Berg #ifdef CONFIG_MODULES
312438f7b870SPatrick McHardy replay:
31258072f085SThomas Graf #endif
31268cb08174SJohannes Berg 	err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
31278cb08174SJohannes Berg 				     ifla_policy, extack);
312838f7b870SPatrick McHardy 	if (err < 0)
312938f7b870SPatrick McHardy 		return err;
313038f7b870SPatrick McHardy 
31314ff66caeSChristian Brauner 	err = rtnl_ensure_unique_netns(tb, extack, false);
31324ff66caeSChristian Brauner 	if (err < 0)
31334ff66caeSChristian Brauner 		return err;
31344ff66caeSChristian Brauner 
313538f7b870SPatrick McHardy 	if (tb[IFLA_IFNAME])
313638f7b870SPatrick McHardy 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
313738f7b870SPatrick McHardy 	else
313838f7b870SPatrick McHardy 		ifname[0] = '\0';
313938f7b870SPatrick McHardy 
314038f7b870SPatrick McHardy 	ifm = nlmsg_data(nlh);
314138f7b870SPatrick McHardy 	if (ifm->ifi_index > 0)
3142881d966bSEric W. Biederman 		dev = __dev_get_by_index(net, ifm->ifi_index);
314376c9ac0eSJiri Pirko 	else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
314476c9ac0eSJiri Pirko 		dev = rtnl_dev_get(net, NULL, tb[IFLA_ALT_IFNAME], ifname);
314538f7b870SPatrick McHardy 	else
314638f7b870SPatrick McHardy 		dev = NULL;
314738f7b870SPatrick McHardy 
3148ba7d49b1SJiri Pirko 	if (dev) {
3149ba7d49b1SJiri Pirko 		master_dev = netdev_master_upper_dev_get(dev);
3150ba7d49b1SJiri Pirko 		if (master_dev)
3151ba7d49b1SJiri Pirko 			m_ops = master_dev->rtnl_link_ops;
3152ba7d49b1SJiri Pirko 	}
3153ba7d49b1SJiri Pirko 
3154e0d087afSEric Dumazet 	err = validate_linkmsg(dev, tb);
3155e0d087afSEric Dumazet 	if (err < 0)
31561840bb13SThomas Graf 		return err;
31571840bb13SThomas Graf 
315838f7b870SPatrick McHardy 	if (tb[IFLA_LINKINFO]) {
31598cb08174SJohannes Berg 		err = nla_parse_nested_deprecated(linkinfo, IFLA_INFO_MAX,
31608cb08174SJohannes Berg 						  tb[IFLA_LINKINFO],
31618cb08174SJohannes Berg 						  ifla_info_policy, NULL);
316238f7b870SPatrick McHardy 		if (err < 0)
316338f7b870SPatrick McHardy 			return err;
316438f7b870SPatrick McHardy 	} else
316538f7b870SPatrick McHardy 		memset(linkinfo, 0, sizeof(linkinfo));
316638f7b870SPatrick McHardy 
316738f7b870SPatrick McHardy 	if (linkinfo[IFLA_INFO_KIND]) {
316838f7b870SPatrick McHardy 		nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
316938f7b870SPatrick McHardy 		ops = rtnl_link_ops_get(kind);
317038f7b870SPatrick McHardy 	} else {
317138f7b870SPatrick McHardy 		kind[0] = '\0';
317238f7b870SPatrick McHardy 		ops = NULL;
317338f7b870SPatrick McHardy 	}
317438f7b870SPatrick McHardy 
3175420d0318SJakub Kicinski 	data = NULL;
317638f7b870SPatrick McHardy 	if (ops) {
3177ccf8dbcdSKees Cook 		if (ops->maxtype > RTNL_MAX_TYPE)
3178ccf8dbcdSKees Cook 			return -EINVAL;
3179ccf8dbcdSKees Cook 
318038f7b870SPatrick McHardy 		if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
31818cb08174SJohannes Berg 			err = nla_parse_nested_deprecated(attr, ops->maxtype,
318238f7b870SPatrick McHardy 							  linkinfo[IFLA_INFO_DATA],
318368d57f3bSDavid Ahern 							  ops->policy, extack);
318438f7b870SPatrick McHardy 			if (err < 0)
318538f7b870SPatrick McHardy 				return err;
318638f7b870SPatrick McHardy 			data = attr;
318738f7b870SPatrick McHardy 		}
318838f7b870SPatrick McHardy 		if (ops->validate) {
3189a8b8a889SMatthias Schiffer 			err = ops->validate(tb, data, extack);
319038f7b870SPatrick McHardy 			if (err < 0)
319138f7b870SPatrick McHardy 				return err;
319238f7b870SPatrick McHardy 		}
319338f7b870SPatrick McHardy 	}
319438f7b870SPatrick McHardy 
3195420d0318SJakub Kicinski 	slave_data = NULL;
3196ba7d49b1SJiri Pirko 	if (m_ops) {
3197af066ed3SPrashant Bhole 		if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE)
3198ccf8dbcdSKees Cook 			return -EINVAL;
3199ccf8dbcdSKees Cook 
3200ba7d49b1SJiri Pirko 		if (m_ops->slave_maxtype &&
3201ba7d49b1SJiri Pirko 		    linkinfo[IFLA_INFO_SLAVE_DATA]) {
32028cb08174SJohannes Berg 			err = nla_parse_nested_deprecated(slave_attr,
32038cb08174SJohannes Berg 							  m_ops->slave_maxtype,
3204ba7d49b1SJiri Pirko 							  linkinfo[IFLA_INFO_SLAVE_DATA],
32058cb08174SJohannes Berg 							  m_ops->slave_policy,
32068cb08174SJohannes Berg 							  extack);
3207ba7d49b1SJiri Pirko 			if (err < 0)
3208ba7d49b1SJiri Pirko 				return err;
3209ba7d49b1SJiri Pirko 			slave_data = slave_attr;
3210ba7d49b1SJiri Pirko 		}
3211ba7d49b1SJiri Pirko 	}
3212ba7d49b1SJiri Pirko 
321338f7b870SPatrick McHardy 	if (dev) {
321490c325e3SNicolas Dichtel 		int status = 0;
321538f7b870SPatrick McHardy 
321638f7b870SPatrick McHardy 		if (nlh->nlmsg_flags & NLM_F_EXCL)
321738f7b870SPatrick McHardy 			return -EEXIST;
321838f7b870SPatrick McHardy 		if (nlh->nlmsg_flags & NLM_F_REPLACE)
321938f7b870SPatrick McHardy 			return -EOPNOTSUPP;
322038f7b870SPatrick McHardy 
322138f7b870SPatrick McHardy 		if (linkinfo[IFLA_INFO_DATA]) {
322238f7b870SPatrick McHardy 			if (!ops || ops != dev->rtnl_link_ops ||
322338f7b870SPatrick McHardy 			    !ops->changelink)
322438f7b870SPatrick McHardy 				return -EOPNOTSUPP;
322538f7b870SPatrick McHardy 
3226ad744b22SMatthias Schiffer 			err = ops->changelink(dev, tb, data, extack);
322738f7b870SPatrick McHardy 			if (err < 0)
322838f7b870SPatrick McHardy 				return err;
3229ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
323038f7b870SPatrick McHardy 		}
323138f7b870SPatrick McHardy 
3232ba7d49b1SJiri Pirko 		if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
3233ba7d49b1SJiri Pirko 			if (!m_ops || !m_ops->slave_changelink)
3234ba7d49b1SJiri Pirko 				return -EOPNOTSUPP;
3235ba7d49b1SJiri Pirko 
3236420d0318SJakub Kicinski 			err = m_ops->slave_changelink(master_dev, dev, tb,
3237420d0318SJakub Kicinski 						      slave_data, extack);
3238ba7d49b1SJiri Pirko 			if (err < 0)
3239ba7d49b1SJiri Pirko 				return err;
3240ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
3241ba7d49b1SJiri Pirko 		}
3242ba7d49b1SJiri Pirko 
3243420d0318SJakub Kicinski 		return do_setlink(skb, dev, ifm, extack, tb, ifname, status);
324438f7b870SPatrick McHardy 	}
324538f7b870SPatrick McHardy 
3246ffa934f1SPatrick McHardy 	if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
3247ffa934f1SPatrick McHardy 		if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
324890f62cf3SEric W. Biederman 			return rtnl_group_changelink(skb, net,
3249ffa934f1SPatrick McHardy 						nla_get_u32(tb[IFLA_GROUP]),
3250ddf9f970SJakub Kicinski 						ifm, extack, tb);
325138f7b870SPatrick McHardy 		return -ENODEV;
3252ffa934f1SPatrick McHardy 	}
325338f7b870SPatrick McHardy 
3254160ca014STheuns Verwoerd 	if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
325538f7b870SPatrick McHardy 		return -EOPNOTSUPP;
325638f7b870SPatrick McHardy 
325738f7b870SPatrick McHardy 	if (!ops) {
325895a5afcaSJohannes Berg #ifdef CONFIG_MODULES
325938f7b870SPatrick McHardy 		if (kind[0]) {
326038f7b870SPatrick McHardy 			__rtnl_unlock();
326138f7b870SPatrick McHardy 			request_module("rtnl-link-%s", kind);
326238f7b870SPatrick McHardy 			rtnl_lock();
326338f7b870SPatrick McHardy 			ops = rtnl_link_ops_get(kind);
326438f7b870SPatrick McHardy 			if (ops)
326538f7b870SPatrick McHardy 				goto replay;
326638f7b870SPatrick McHardy 		}
326738f7b870SPatrick McHardy #endif
326868d57f3bSDavid Ahern 		NL_SET_ERR_MSG(extack, "Unknown device type");
326938f7b870SPatrick McHardy 		return -EOPNOTSUPP;
327038f7b870SPatrick McHardy 	}
327138f7b870SPatrick McHardy 
3272b0ab2fabSJiri Pirko 	if (!ops->setup)
3273b0ab2fabSJiri Pirko 		return -EOPNOTSUPP;
3274b0ab2fabSJiri Pirko 
32755517750fSTom Gundersen 	if (!ifname[0]) {
327638f7b870SPatrick McHardy 		snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
32775517750fSTom Gundersen 		name_assign_type = NET_NAME_ENUM;
32785517750fSTom Gundersen 	}
327938f7b870SPatrick McHardy 
32805bb8ed07SChristian Brauner 	dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN);
328113ad1774SEric W. Biederman 	if (IS_ERR(dest_net))
328213ad1774SEric W. Biederman 		return PTR_ERR(dest_net);
328313ad1774SEric W. Biederman 
3284317f4810SNicolas Dichtel 	if (tb[IFLA_LINK_NETNSID]) {
3285317f4810SNicolas Dichtel 		int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
3286317f4810SNicolas Dichtel 
3287317f4810SNicolas Dichtel 		link_net = get_net_ns_by_id(dest_net, id);
3288317f4810SNicolas Dichtel 		if (!link_net) {
328968d57f3bSDavid Ahern 			NL_SET_ERR_MSG(extack, "Unknown network namespace id");
3290317f4810SNicolas Dichtel 			err =  -EINVAL;
3291317f4810SNicolas Dichtel 			goto out;
3292317f4810SNicolas Dichtel 		}
329306615bedSEric W. Biederman 		err = -EPERM;
329406615bedSEric W. Biederman 		if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
329506615bedSEric W. Biederman 			goto out;
3296420d0318SJakub Kicinski 	} else {
3297420d0318SJakub Kicinski 		link_net = NULL;
3298317f4810SNicolas Dichtel 	}
3299317f4810SNicolas Dichtel 
3300317f4810SNicolas Dichtel 	dev = rtnl_create_link(link_net ? : dest_net, ifname,
3301d0522f1cSDavid Ahern 			       name_assign_type, ops, tb, extack);
33029c7dafbfSPavel Emelyanov 	if (IS_ERR(dev)) {
3303e7199288SPavel Emelianov 		err = PTR_ERR(dev);
33049c7dafbfSPavel Emelyanov 		goto out;
33059c7dafbfSPavel Emelyanov 	}
33069c7dafbfSPavel Emelyanov 
33079c7dafbfSPavel Emelyanov 	dev->ifindex = ifm->ifi_index;
33089c7dafbfSPavel Emelyanov 
33090e0eee24SCong Wang 	if (ops->newlink) {
3310420d0318SJakub Kicinski 		err = ops->newlink(link_net ? : net, dev, tb, data, extack);
33110e0eee24SCong Wang 		/* Drivers should call free_netdev() in ->destructor
3312e51fb152SCong Wang 		 * and unregister it on failure after registration
3313e51fb152SCong Wang 		 * so that device could be finally freed in rtnl_unlock.
33140e0eee24SCong Wang 		 */
3315e51fb152SCong Wang 		if (err < 0) {
3316e51fb152SCong Wang 			/* If device is not registered at all, free it now */
3317e51fb152SCong Wang 			if (dev->reg_state == NETREG_UNINITIALIZED)
3318e51fb152SCong Wang 				free_netdev(dev);
33190e0eee24SCong Wang 			goto out;
3320e51fb152SCong Wang 		}
33210e0eee24SCong Wang 	} else {
33222d85cba2SPatrick McHardy 		err = register_netdevice(dev);
3323fce9b9beSDan Carpenter 		if (err < 0) {
332438f7b870SPatrick McHardy 			free_netdev(dev);
33253729d502SPatrick McHardy 			goto out;
3326fce9b9beSDan Carpenter 		}
33270e0eee24SCong Wang 	}
33283729d502SPatrick McHardy 	err = rtnl_configure_link(dev, ifm);
332943638900SDavid S. Miller 	if (err < 0)
333043638900SDavid S. Miller 		goto out_unregister;
333143638900SDavid S. Miller 	if (link_net) {
333243638900SDavid S. Miller 		err = dev_change_net_namespace(dev, dest_net, ifname);
333343638900SDavid S. Miller 		if (err < 0)
333443638900SDavid S. Miller 			goto out_unregister;
333543638900SDavid S. Miller 	}
3336160ca014STheuns Verwoerd 	if (tb[IFLA_MASTER]) {
3337420d0318SJakub Kicinski 		err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
3338160ca014STheuns Verwoerd 		if (err)
3339160ca014STheuns Verwoerd 			goto out_unregister;
3340160ca014STheuns Verwoerd 	}
334143638900SDavid S. Miller out:
334243638900SDavid S. Miller 	if (link_net)
334343638900SDavid S. Miller 		put_net(link_net);
334443638900SDavid S. Miller 	put_net(dest_net);
334543638900SDavid S. Miller 	return err;
334643638900SDavid S. Miller out_unregister:
33477afb8886SWANG Cong 	if (ops->newlink) {
33487afb8886SWANG Cong 		LIST_HEAD(list_kill);
33497afb8886SWANG Cong 
33507afb8886SWANG Cong 		ops->dellink(dev, &list_kill);
33517afb8886SWANG Cong 		unregister_netdevice_many(&list_kill);
33527afb8886SWANG Cong 	} else {
33533729d502SPatrick McHardy 		unregister_netdevice(dev);
33547afb8886SWANG Cong 	}
3355317f4810SNicolas Dichtel 	goto out;
3356317f4810SNicolas Dichtel }
335738f7b870SPatrick McHardy 
3358a2939745SJakub Kicinski static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3359a2939745SJakub Kicinski 			struct netlink_ext_ack *extack)
3360a2939745SJakub Kicinski {
3361a2939745SJakub Kicinski 	struct nlattr **attr;
3362a2939745SJakub Kicinski 	int ret;
3363a2939745SJakub Kicinski 
3364a2939745SJakub Kicinski 	attr = kmalloc_array(RTNL_MAX_TYPE + 1, sizeof(*attr), GFP_KERNEL);
3365a2939745SJakub Kicinski 	if (!attr)
3366a2939745SJakub Kicinski 		return -ENOMEM;
3367a2939745SJakub Kicinski 
3368a2939745SJakub Kicinski 	ret = __rtnl_newlink(skb, nlh, attr, extack);
3369a2939745SJakub Kicinski 	kfree(attr);
3370a2939745SJakub Kicinski 	return ret;
3371a2939745SJakub Kicinski }
3372a2939745SJakub Kicinski 
33739b3757b0SJakub Kicinski static int rtnl_valid_getlink_req(struct sk_buff *skb,
33749b3757b0SJakub Kicinski 				  const struct nlmsghdr *nlh,
33759b3757b0SJakub Kicinski 				  struct nlattr **tb,
33769b3757b0SJakub Kicinski 				  struct netlink_ext_ack *extack)
33779b3757b0SJakub Kicinski {
33789b3757b0SJakub Kicinski 	struct ifinfomsg *ifm;
33799b3757b0SJakub Kicinski 	int i, err;
33809b3757b0SJakub Kicinski 
33819b3757b0SJakub Kicinski 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
33829b3757b0SJakub Kicinski 		NL_SET_ERR_MSG(extack, "Invalid header for get link");
33839b3757b0SJakub Kicinski 		return -EINVAL;
33849b3757b0SJakub Kicinski 	}
33859b3757b0SJakub Kicinski 
33869b3757b0SJakub Kicinski 	if (!netlink_strict_get_check(skb))
33878cb08174SJohannes Berg 		return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
33888cb08174SJohannes Berg 					      ifla_policy, extack);
33899b3757b0SJakub Kicinski 
33909b3757b0SJakub Kicinski 	ifm = nlmsg_data(nlh);
33919b3757b0SJakub Kicinski 	if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
33929b3757b0SJakub Kicinski 	    ifm->ifi_change) {
33939b3757b0SJakub Kicinski 		NL_SET_ERR_MSG(extack, "Invalid values in header for get link request");
33949b3757b0SJakub Kicinski 		return -EINVAL;
33959b3757b0SJakub Kicinski 	}
33969b3757b0SJakub Kicinski 
33978cb08174SJohannes Berg 	err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFLA_MAX,
33988cb08174SJohannes Berg 					    ifla_policy, extack);
33999b3757b0SJakub Kicinski 	if (err)
34009b3757b0SJakub Kicinski 		return err;
34019b3757b0SJakub Kicinski 
34029b3757b0SJakub Kicinski 	for (i = 0; i <= IFLA_MAX; i++) {
34039b3757b0SJakub Kicinski 		if (!tb[i])
34049b3757b0SJakub Kicinski 			continue;
34059b3757b0SJakub Kicinski 
34069b3757b0SJakub Kicinski 		switch (i) {
34079b3757b0SJakub Kicinski 		case IFLA_IFNAME:
340876c9ac0eSJiri Pirko 		case IFLA_ALT_IFNAME:
34099b3757b0SJakub Kicinski 		case IFLA_EXT_MASK:
34109b3757b0SJakub Kicinski 		case IFLA_TARGET_NETNSID:
34119b3757b0SJakub Kicinski 			break;
34129b3757b0SJakub Kicinski 		default:
34139b3757b0SJakub Kicinski 			NL_SET_ERR_MSG(extack, "Unsupported attribute in get link request");
34149b3757b0SJakub Kicinski 			return -EINVAL;
34159b3757b0SJakub Kicinski 		}
34169b3757b0SJakub Kicinski 	}
34179b3757b0SJakub Kicinski 
34189b3757b0SJakub Kicinski 	return 0;
34199b3757b0SJakub Kicinski }
34209b3757b0SJakub Kicinski 
3421c21ef3e3SDavid Ahern static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3422c21ef3e3SDavid Ahern 			struct netlink_ext_ack *extack)
3423711e2c33SJean Tourrilhes {
34243b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
342579e1ad14SJiri Benc 	struct net *tgt_net = net;
3426b60c5115SThomas Graf 	struct ifinfomsg *ifm;
3427b60c5115SThomas Graf 	struct nlattr *tb[IFLA_MAX+1];
3428b60c5115SThomas Graf 	struct net_device *dev = NULL;
3429b60c5115SThomas Graf 	struct sk_buff *nskb;
343079e1ad14SJiri Benc 	int netnsid = -1;
3431339bf98fSThomas Graf 	int err;
3432115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
3433711e2c33SJean Tourrilhes 
34349b3757b0SJakub Kicinski 	err = rtnl_valid_getlink_req(skb, nlh, tb, extack);
3435b60c5115SThomas Graf 	if (err < 0)
34369918f230SEric Sesterhenn 		return err;
3437b60c5115SThomas Graf 
34384ff66caeSChristian Brauner 	err = rtnl_ensure_unique_netns(tb, extack, true);
34394ff66caeSChristian Brauner 	if (err < 0)
34404ff66caeSChristian Brauner 		return err;
34414ff66caeSChristian Brauner 
34427e4a8d5aSChristian Brauner 	if (tb[IFLA_TARGET_NETNSID]) {
34437e4a8d5aSChristian Brauner 		netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
3444c383edc4SChristian Brauner 		tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
344579e1ad14SJiri Benc 		if (IS_ERR(tgt_net))
344679e1ad14SJiri Benc 			return PTR_ERR(tgt_net);
344779e1ad14SJiri Benc 	}
344879e1ad14SJiri Benc 
3449115c9b81SGreg Rose 	if (tb[IFLA_EXT_MASK])
3450115c9b81SGreg Rose 		ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3451115c9b81SGreg Rose 
345279e1ad14SJiri Benc 	err = -EINVAL;
3453b60c5115SThomas Graf 	ifm = nlmsg_data(nlh);
3454a3d12891SEric Dumazet 	if (ifm->ifi_index > 0)
345579e1ad14SJiri Benc 		dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
345676c9ac0eSJiri Pirko 	else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
345776c9ac0eSJiri Pirko 		dev = rtnl_dev_get(tgt_net, tb[IFLA_IFNAME],
345876c9ac0eSJiri Pirko 				   tb[IFLA_ALT_IFNAME], NULL);
3459a3d12891SEric Dumazet 	else
346079e1ad14SJiri Benc 		goto out;
3461b60c5115SThomas Graf 
346279e1ad14SJiri Benc 	err = -ENODEV;
3463a3d12891SEric Dumazet 	if (dev == NULL)
346479e1ad14SJiri Benc 		goto out;
3465a3d12891SEric Dumazet 
346679e1ad14SJiri Benc 	err = -ENOBUFS;
3467115c9b81SGreg Rose 	nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
3468a3d12891SEric Dumazet 	if (nskb == NULL)
346979e1ad14SJiri Benc 		goto out;
3470711e2c33SJean Tourrilhes 
347179e1ad14SJiri Benc 	err = rtnl_fill_ifinfo(nskb, dev, net,
347279e1ad14SJiri Benc 			       RTM_NEWLINK, NETLINK_CB(skb).portid,
347379e1ad14SJiri Benc 			       nlh->nlmsg_seq, 0, 0, ext_filter_mask,
3474d4e4fdf9SGuillaume Nault 			       0, NULL, 0, netnsid, GFP_KERNEL);
347526932566SPatrick McHardy 	if (err < 0) {
347626932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in if_nlmsg_size */
347726932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
347826932566SPatrick McHardy 		kfree_skb(nskb);
3479a3d12891SEric Dumazet 	} else
348015e47304SEric W. Biederman 		err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
348179e1ad14SJiri Benc out:
348279e1ad14SJiri Benc 	if (netnsid >= 0)
348379e1ad14SJiri Benc 		put_net(tgt_net);
3484b60c5115SThomas Graf 
3485711e2c33SJean Tourrilhes 	return err;
3486711e2c33SJean Tourrilhes }
3487711e2c33SJean Tourrilhes 
348836fbf1e5SJiri Pirko static int rtnl_alt_ifname(int cmd, struct net_device *dev, struct nlattr *attr,
348936fbf1e5SJiri Pirko 			   bool *changed, struct netlink_ext_ack *extack)
349036fbf1e5SJiri Pirko {
349136fbf1e5SJiri Pirko 	char *alt_ifname;
349236fbf1e5SJiri Pirko 	int err;
349336fbf1e5SJiri Pirko 
349436fbf1e5SJiri Pirko 	err = nla_validate(attr, attr->nla_len, IFLA_MAX, ifla_policy, extack);
349536fbf1e5SJiri Pirko 	if (err)
349636fbf1e5SJiri Pirko 		return err;
349736fbf1e5SJiri Pirko 
349836fbf1e5SJiri Pirko 	alt_ifname = nla_data(attr);
349936fbf1e5SJiri Pirko 	if (cmd == RTM_NEWLINKPROP) {
350036fbf1e5SJiri Pirko 		alt_ifname = kstrdup(alt_ifname, GFP_KERNEL);
350136fbf1e5SJiri Pirko 		if (!alt_ifname)
350236fbf1e5SJiri Pirko 			return -ENOMEM;
350336fbf1e5SJiri Pirko 		err = netdev_name_node_alt_create(dev, alt_ifname);
350436fbf1e5SJiri Pirko 		if (err) {
350536fbf1e5SJiri Pirko 			kfree(alt_ifname);
350636fbf1e5SJiri Pirko 			return err;
350736fbf1e5SJiri Pirko 		}
350836fbf1e5SJiri Pirko 	} else if (cmd == RTM_DELLINKPROP) {
350936fbf1e5SJiri Pirko 		err = netdev_name_node_alt_destroy(dev, alt_ifname);
351036fbf1e5SJiri Pirko 		if (err)
351136fbf1e5SJiri Pirko 			return err;
351236fbf1e5SJiri Pirko 	} else {
351336fbf1e5SJiri Pirko 		WARN_ON(1);
351436fbf1e5SJiri Pirko 		return 0;
351536fbf1e5SJiri Pirko 	}
351636fbf1e5SJiri Pirko 
351736fbf1e5SJiri Pirko 	*changed = true;
351836fbf1e5SJiri Pirko 	return 0;
351936fbf1e5SJiri Pirko }
352036fbf1e5SJiri Pirko 
352136fbf1e5SJiri Pirko static int rtnl_linkprop(int cmd, struct sk_buff *skb, struct nlmsghdr *nlh,
352236fbf1e5SJiri Pirko 			 struct netlink_ext_ack *extack)
352336fbf1e5SJiri Pirko {
352436fbf1e5SJiri Pirko 	struct net *net = sock_net(skb->sk);
352536fbf1e5SJiri Pirko 	struct nlattr *tb[IFLA_MAX + 1];
352636fbf1e5SJiri Pirko 	struct net_device *dev;
352736fbf1e5SJiri Pirko 	struct ifinfomsg *ifm;
352836fbf1e5SJiri Pirko 	bool changed = false;
352936fbf1e5SJiri Pirko 	struct nlattr *attr;
353036fbf1e5SJiri Pirko 	int err, rem;
353136fbf1e5SJiri Pirko 
353236fbf1e5SJiri Pirko 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
353336fbf1e5SJiri Pirko 	if (err)
353436fbf1e5SJiri Pirko 		return err;
353536fbf1e5SJiri Pirko 
353636fbf1e5SJiri Pirko 	err = rtnl_ensure_unique_netns(tb, extack, true);
353736fbf1e5SJiri Pirko 	if (err)
353836fbf1e5SJiri Pirko 		return err;
353936fbf1e5SJiri Pirko 
354036fbf1e5SJiri Pirko 	ifm = nlmsg_data(nlh);
3541cc6090e9SJiri Pirko 	if (ifm->ifi_index > 0)
354236fbf1e5SJiri Pirko 		dev = __dev_get_by_index(net, ifm->ifi_index);
354376c9ac0eSJiri Pirko 	else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
354476c9ac0eSJiri Pirko 		dev = rtnl_dev_get(net, tb[IFLA_IFNAME],
354576c9ac0eSJiri Pirko 				   tb[IFLA_ALT_IFNAME], NULL);
3546cc6090e9SJiri Pirko 	else
354736fbf1e5SJiri Pirko 		return -EINVAL;
354836fbf1e5SJiri Pirko 
354936fbf1e5SJiri Pirko 	if (!dev)
355036fbf1e5SJiri Pirko 		return -ENODEV;
355136fbf1e5SJiri Pirko 
355236fbf1e5SJiri Pirko 	if (!tb[IFLA_PROP_LIST])
355336fbf1e5SJiri Pirko 		return 0;
355436fbf1e5SJiri Pirko 
355536fbf1e5SJiri Pirko 	nla_for_each_nested(attr, tb[IFLA_PROP_LIST], rem) {
355636fbf1e5SJiri Pirko 		switch (nla_type(attr)) {
355736fbf1e5SJiri Pirko 		case IFLA_ALT_IFNAME:
355836fbf1e5SJiri Pirko 			err = rtnl_alt_ifname(cmd, dev, attr, &changed, extack);
355936fbf1e5SJiri Pirko 			if (err)
356036fbf1e5SJiri Pirko 				return err;
356136fbf1e5SJiri Pirko 			break;
356236fbf1e5SJiri Pirko 		}
356336fbf1e5SJiri Pirko 	}
356436fbf1e5SJiri Pirko 
356536fbf1e5SJiri Pirko 	if (changed)
356636fbf1e5SJiri Pirko 		netdev_state_change(dev);
356736fbf1e5SJiri Pirko 	return 0;
356836fbf1e5SJiri Pirko }
356936fbf1e5SJiri Pirko 
357036fbf1e5SJiri Pirko static int rtnl_newlinkprop(struct sk_buff *skb, struct nlmsghdr *nlh,
357136fbf1e5SJiri Pirko 			    struct netlink_ext_ack *extack)
357236fbf1e5SJiri Pirko {
357336fbf1e5SJiri Pirko 	return rtnl_linkprop(RTM_NEWLINKPROP, skb, nlh, extack);
357436fbf1e5SJiri Pirko }
357536fbf1e5SJiri Pirko 
357636fbf1e5SJiri Pirko static int rtnl_dellinkprop(struct sk_buff *skb, struct nlmsghdr *nlh,
357736fbf1e5SJiri Pirko 			    struct netlink_ext_ack *extack)
357836fbf1e5SJiri Pirko {
357936fbf1e5SJiri Pirko 	return rtnl_linkprop(RTM_DELLINKPROP, skb, nlh, extack);
358036fbf1e5SJiri Pirko }
358136fbf1e5SJiri Pirko 
3582115c9b81SGreg Rose static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
3583c7ac8679SGreg Rose {
3584115c9b81SGreg Rose 	struct net *net = sock_net(skb->sk);
3585115c9b81SGreg Rose 	struct net_device *dev;
3586115c9b81SGreg Rose 	struct nlattr *tb[IFLA_MAX+1];
3587115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
3588115c9b81SGreg Rose 	u16 min_ifinfo_dump_size = 0;
3589e5eca6d4SMichal Schmidt 	int hdrlen;
3590115c9b81SGreg Rose 
3591e5eca6d4SMichal Schmidt 	/* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
3592e5eca6d4SMichal Schmidt 	hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
3593e5eca6d4SMichal Schmidt 		 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
3594e5eca6d4SMichal Schmidt 
35958cb08174SJohannes Berg 	if (nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
3596115c9b81SGreg Rose 		if (tb[IFLA_EXT_MASK])
3597115c9b81SGreg Rose 			ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3598a4b64fbeSEric Dumazet 	}
3599115c9b81SGreg Rose 
3600115c9b81SGreg Rose 	if (!ext_filter_mask)
3601115c9b81SGreg Rose 		return NLMSG_GOODSIZE;
3602115c9b81SGreg Rose 	/*
3603115c9b81SGreg Rose 	 * traverse the list of net devices and compute the minimum
3604115c9b81SGreg Rose 	 * buffer size based upon the filter mask.
3605115c9b81SGreg Rose 	 */
36066853dd48SFlorian Westphal 	rcu_read_lock();
36076853dd48SFlorian Westphal 	for_each_netdev_rcu(net, dev) {
3608115c9b81SGreg Rose 		min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
3609115c9b81SGreg Rose 					     if_nlmsg_size(dev,
3610115c9b81SGreg Rose 						           ext_filter_mask));
3611115c9b81SGreg Rose 	}
36126853dd48SFlorian Westphal 	rcu_read_unlock();
3613115c9b81SGreg Rose 
361493af2056SZhang Shengju 	return nlmsg_total_size(min_ifinfo_dump_size);
3615c7ac8679SGreg Rose }
3616c7ac8679SGreg Rose 
361742bad1daSAdrian Bunk static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
36181da177e4SLinus Torvalds {
36191da177e4SLinus Torvalds 	int idx;
36201da177e4SLinus Torvalds 	int s_idx = cb->family;
362187ccbb1fSChristian Brauner 	int type = cb->nlh->nlmsg_type - RTM_BASE;
3622c63586dcSDavid Ahern 	int ret = 0;
36231da177e4SLinus Torvalds 
36241da177e4SLinus Torvalds 	if (s_idx == 0)
36251da177e4SLinus Torvalds 		s_idx = 1;
36266853dd48SFlorian Westphal 
362725239ceeSPatrick McHardy 	for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
3628addf9b90SFlorian Westphal 		struct rtnl_link **tab;
3629addf9b90SFlorian Westphal 		struct rtnl_link *link;
36306853dd48SFlorian Westphal 		rtnl_dumpit_func dumpit;
36316853dd48SFlorian Westphal 
36321da177e4SLinus Torvalds 		if (idx < s_idx || idx == PF_PACKET)
36331da177e4SLinus Torvalds 			continue;
36346853dd48SFlorian Westphal 
3635addf9b90SFlorian Westphal 		if (type < 0 || type >= RTM_NR_MSGTYPES)
36361da177e4SLinus Torvalds 			continue;
36376853dd48SFlorian Westphal 
3638addf9b90SFlorian Westphal 		tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]);
3639addf9b90SFlorian Westphal 		if (!tab)
3640addf9b90SFlorian Westphal 			continue;
3641addf9b90SFlorian Westphal 
3642addf9b90SFlorian Westphal 		link = tab[type];
3643addf9b90SFlorian Westphal 		if (!link)
3644addf9b90SFlorian Westphal 			continue;
3645addf9b90SFlorian Westphal 
3646addf9b90SFlorian Westphal 		dumpit = link->dumpit;
36476853dd48SFlorian Westphal 		if (!dumpit)
36486853dd48SFlorian Westphal 			continue;
36496853dd48SFlorian Westphal 
36500465277fSNicolas Dichtel 		if (idx > s_idx) {
36511da177e4SLinus Torvalds 			memset(&cb->args[0], 0, sizeof(cb->args));
36520465277fSNicolas Dichtel 			cb->prev_seq = 0;
36530465277fSNicolas Dichtel 			cb->seq = 0;
36540465277fSNicolas Dichtel 		}
3655c63586dcSDavid Ahern 		ret = dumpit(skb, cb);
36565e1acb4aSAlexey Kodanev 		if (ret)
36571da177e4SLinus Torvalds 			break;
36581da177e4SLinus Torvalds 	}
36591da177e4SLinus Torvalds 	cb->family = idx;
36601da177e4SLinus Torvalds 
3661c63586dcSDavid Ahern 	return skb->len ? : ret;
36621da177e4SLinus Torvalds }
36631da177e4SLinus Torvalds 
3664395eea6cSMahesh Bandewar struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
36653d3ea5afSVlad Yasevich 				       unsigned int change,
366638e01b30SNicolas Dichtel 				       u32 event, gfp_t flags, int *new_nsid,
366738e01b30SNicolas Dichtel 				       int new_ifindex)
36681da177e4SLinus Torvalds {
3669c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
36701da177e4SLinus Torvalds 	struct sk_buff *skb;
36710ec6d3f4SThomas Graf 	int err = -ENOBUFS;
3672c7ac8679SGreg Rose 	size_t if_info_size;
36731da177e4SLinus Torvalds 
36747f294054SAlexei Starovoitov 	skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
36750ec6d3f4SThomas Graf 	if (skb == NULL)
36760ec6d3f4SThomas Graf 		goto errout;
36771da177e4SLinus Torvalds 
367879e1ad14SJiri Benc 	err = rtnl_fill_ifinfo(skb, dev, dev_net(dev),
367979e1ad14SJiri Benc 			       type, 0, 0, change, 0, 0, event,
3680d4e4fdf9SGuillaume Nault 			       new_nsid, new_ifindex, -1, flags);
368126932566SPatrick McHardy 	if (err < 0) {
368226932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in if_nlmsg_size() */
368326932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
368426932566SPatrick McHardy 		kfree_skb(skb);
368526932566SPatrick McHardy 		goto errout;
368626932566SPatrick McHardy 	}
3687395eea6cSMahesh Bandewar 	return skb;
36880ec6d3f4SThomas Graf errout:
36890ec6d3f4SThomas Graf 	if (err < 0)
36904b3da706SEric W. Biederman 		rtnl_set_sk_err(net, RTNLGRP_LINK, err);
3691395eea6cSMahesh Bandewar 	return NULL;
3692395eea6cSMahesh Bandewar }
3693395eea6cSMahesh Bandewar 
3694395eea6cSMahesh Bandewar void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
3695395eea6cSMahesh Bandewar {
3696395eea6cSMahesh Bandewar 	struct net *net = dev_net(dev);
3697395eea6cSMahesh Bandewar 
3698395eea6cSMahesh Bandewar 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
3699395eea6cSMahesh Bandewar }
3700395eea6cSMahesh Bandewar 
37013d3ea5afSVlad Yasevich static void rtmsg_ifinfo_event(int type, struct net_device *dev,
37023d3ea5afSVlad Yasevich 			       unsigned int change, u32 event,
370338e01b30SNicolas Dichtel 			       gfp_t flags, int *new_nsid, int new_ifindex)
3704395eea6cSMahesh Bandewar {
3705395eea6cSMahesh Bandewar 	struct sk_buff *skb;
3706395eea6cSMahesh Bandewar 
3707ed2a80abSNicolas Dichtel 	if (dev->reg_state != NETREG_REGISTERED)
3708ed2a80abSNicolas Dichtel 		return;
3709ed2a80abSNicolas Dichtel 
371038e01b30SNicolas Dichtel 	skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid,
371138e01b30SNicolas Dichtel 				     new_ifindex);
3712395eea6cSMahesh Bandewar 	if (skb)
3713395eea6cSMahesh Bandewar 		rtmsg_ifinfo_send(skb, dev, flags);
37141da177e4SLinus Torvalds }
37153d3ea5afSVlad Yasevich 
37163d3ea5afSVlad Yasevich void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
37173d3ea5afSVlad Yasevich 		  gfp_t flags)
37183d3ea5afSVlad Yasevich {
371938e01b30SNicolas Dichtel 	rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
372038e01b30SNicolas Dichtel 			   NULL, 0);
37213d3ea5afSVlad Yasevich }
37221da177e4SLinus Torvalds 
37236621dd29SNicolas Dichtel void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change,
372438e01b30SNicolas Dichtel 			 gfp_t flags, int *new_nsid, int new_ifindex)
37256621dd29SNicolas Dichtel {
37266621dd29SNicolas Dichtel 	rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
372738e01b30SNicolas Dichtel 			   new_nsid, new_ifindex);
37286621dd29SNicolas Dichtel }
37296621dd29SNicolas Dichtel 
3730d83b0603SJohn Fastabend static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
3731d83b0603SJohn Fastabend 				   struct net_device *dev,
37321e53d5bbSHubert Sokolowski 				   u8 *addr, u16 vid, u32 pid, u32 seq,
37331c104a6bSNicolas Dichtel 				   int type, unsigned int flags,
3734b3379041SHubert Sokolowski 				   int nlflags, u16 ndm_state)
3735d83b0603SJohn Fastabend {
3736d83b0603SJohn Fastabend 	struct nlmsghdr *nlh;
3737d83b0603SJohn Fastabend 	struct ndmsg *ndm;
3738d83b0603SJohn Fastabend 
37391c104a6bSNicolas Dichtel 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
3740d83b0603SJohn Fastabend 	if (!nlh)
3741d83b0603SJohn Fastabend 		return -EMSGSIZE;
3742d83b0603SJohn Fastabend 
3743d83b0603SJohn Fastabend 	ndm = nlmsg_data(nlh);
3744d83b0603SJohn Fastabend 	ndm->ndm_family  = AF_BRIDGE;
3745d83b0603SJohn Fastabend 	ndm->ndm_pad1	 = 0;
3746d83b0603SJohn Fastabend 	ndm->ndm_pad2    = 0;
3747d83b0603SJohn Fastabend 	ndm->ndm_flags	 = flags;
3748d83b0603SJohn Fastabend 	ndm->ndm_type	 = 0;
3749d83b0603SJohn Fastabend 	ndm->ndm_ifindex = dev->ifindex;
3750b3379041SHubert Sokolowski 	ndm->ndm_state   = ndm_state;
3751d83b0603SJohn Fastabend 
3752d83b0603SJohn Fastabend 	if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
3753d83b0603SJohn Fastabend 		goto nla_put_failure;
37541e53d5bbSHubert Sokolowski 	if (vid)
37551e53d5bbSHubert Sokolowski 		if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
37561e53d5bbSHubert Sokolowski 			goto nla_put_failure;
3757d83b0603SJohn Fastabend 
3758053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
3759053c095aSJohannes Berg 	return 0;
3760d83b0603SJohn Fastabend 
3761d83b0603SJohn Fastabend nla_put_failure:
3762d83b0603SJohn Fastabend 	nlmsg_cancel(skb, nlh);
3763d83b0603SJohn Fastabend 	return -EMSGSIZE;
3764d83b0603SJohn Fastabend }
3765d83b0603SJohn Fastabend 
37663ff661c3SJohn Fastabend static inline size_t rtnl_fdb_nlmsg_size(void)
37673ff661c3SJohn Fastabend {
3768f82ef3e1SSabrina Dubroca 	return NLMSG_ALIGN(sizeof(struct ndmsg)) +
3769f82ef3e1SSabrina Dubroca 	       nla_total_size(ETH_ALEN) +	/* NDA_LLADDR */
3770f82ef3e1SSabrina Dubroca 	       nla_total_size(sizeof(u16)) +	/* NDA_VLAN */
3771f82ef3e1SSabrina Dubroca 	       0;
37723ff661c3SJohn Fastabend }
37733ff661c3SJohn Fastabend 
3774b3379041SHubert Sokolowski static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
3775b3379041SHubert Sokolowski 			    u16 ndm_state)
37763ff661c3SJohn Fastabend {
37773ff661c3SJohn Fastabend 	struct net *net = dev_net(dev);
37783ff661c3SJohn Fastabend 	struct sk_buff *skb;
37793ff661c3SJohn Fastabend 	int err = -ENOBUFS;
37803ff661c3SJohn Fastabend 
37813ff661c3SJohn Fastabend 	skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
37823ff661c3SJohn Fastabend 	if (!skb)
37833ff661c3SJohn Fastabend 		goto errout;
37843ff661c3SJohn Fastabend 
37851e53d5bbSHubert Sokolowski 	err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
3786b3379041SHubert Sokolowski 				      0, 0, type, NTF_SELF, 0, ndm_state);
37873ff661c3SJohn Fastabend 	if (err < 0) {
37883ff661c3SJohn Fastabend 		kfree_skb(skb);
37893ff661c3SJohn Fastabend 		goto errout;
37903ff661c3SJohn Fastabend 	}
37913ff661c3SJohn Fastabend 
37923ff661c3SJohn Fastabend 	rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
37933ff661c3SJohn Fastabend 	return;
37943ff661c3SJohn Fastabend errout:
37953ff661c3SJohn Fastabend 	rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
37963ff661c3SJohn Fastabend }
37973ff661c3SJohn Fastabend 
3798a986967eSBart Van Assche /*
3799090096bfSVlad Yasevich  * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
3800090096bfSVlad Yasevich  */
3801090096bfSVlad Yasevich int ndo_dflt_fdb_add(struct ndmsg *ndm,
3802090096bfSVlad Yasevich 		     struct nlattr *tb[],
3803090096bfSVlad Yasevich 		     struct net_device *dev,
3804f6f6424bSJiri Pirko 		     const unsigned char *addr, u16 vid,
3805090096bfSVlad Yasevich 		     u16 flags)
3806090096bfSVlad Yasevich {
3807090096bfSVlad Yasevich 	int err = -EINVAL;
3808090096bfSVlad Yasevich 
3809090096bfSVlad Yasevich 	/* If aging addresses are supported device will need to
3810090096bfSVlad Yasevich 	 * implement its own handler for this.
3811090096bfSVlad Yasevich 	 */
3812090096bfSVlad Yasevich 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
3813090096bfSVlad Yasevich 		pr_info("%s: FDB only supports static addresses\n", dev->name);
3814090096bfSVlad Yasevich 		return err;
3815090096bfSVlad Yasevich 	}
3816090096bfSVlad Yasevich 
381765891feaSOr Gerlitz 	if (vid) {
381865891feaSOr Gerlitz 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
381965891feaSOr Gerlitz 		return err;
382065891feaSOr Gerlitz 	}
382165891feaSOr Gerlitz 
3822090096bfSVlad Yasevich 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3823090096bfSVlad Yasevich 		err = dev_uc_add_excl(dev, addr);
3824090096bfSVlad Yasevich 	else if (is_multicast_ether_addr(addr))
3825090096bfSVlad Yasevich 		err = dev_mc_add_excl(dev, addr);
3826090096bfSVlad Yasevich 
3827090096bfSVlad Yasevich 	/* Only return duplicate errors if NLM_F_EXCL is set */
3828090096bfSVlad Yasevich 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
3829090096bfSVlad Yasevich 		err = 0;
3830090096bfSVlad Yasevich 
3831090096bfSVlad Yasevich 	return err;
3832090096bfSVlad Yasevich }
3833090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_add);
3834090096bfSVlad Yasevich 
3835b88d12e4SFlorian Westphal static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid,
3836b88d12e4SFlorian Westphal 			 struct netlink_ext_ack *extack)
3837f6f6424bSJiri Pirko {
3838f6f6424bSJiri Pirko 	u16 vid = 0;
3839f6f6424bSJiri Pirko 
3840f6f6424bSJiri Pirko 	if (vlan_attr) {
3841f6f6424bSJiri Pirko 		if (nla_len(vlan_attr) != sizeof(u16)) {
3842b88d12e4SFlorian Westphal 			NL_SET_ERR_MSG(extack, "invalid vlan attribute size");
3843f6f6424bSJiri Pirko 			return -EINVAL;
3844f6f6424bSJiri Pirko 		}
3845f6f6424bSJiri Pirko 
3846f6f6424bSJiri Pirko 		vid = nla_get_u16(vlan_attr);
3847f6f6424bSJiri Pirko 
3848f6f6424bSJiri Pirko 		if (!vid || vid >= VLAN_VID_MASK) {
3849b88d12e4SFlorian Westphal 			NL_SET_ERR_MSG(extack, "invalid vlan id");
3850f6f6424bSJiri Pirko 			return -EINVAL;
3851f6f6424bSJiri Pirko 		}
3852f6f6424bSJiri Pirko 	}
3853f6f6424bSJiri Pirko 	*p_vid = vid;
3854f6f6424bSJiri Pirko 	return 0;
3855f6f6424bSJiri Pirko }
3856f6f6424bSJiri Pirko 
3857c21ef3e3SDavid Ahern static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
3858c21ef3e3SDavid Ahern 			struct netlink_ext_ack *extack)
385977162022SJohn Fastabend {
386077162022SJohn Fastabend 	struct net *net = sock_net(skb->sk);
386177162022SJohn Fastabend 	struct ndmsg *ndm;
386277162022SJohn Fastabend 	struct nlattr *tb[NDA_MAX+1];
386377162022SJohn Fastabend 	struct net_device *dev;
386477162022SJohn Fastabend 	u8 *addr;
3865f6f6424bSJiri Pirko 	u16 vid;
386677162022SJohn Fastabend 	int err;
386777162022SJohn Fastabend 
38688cb08174SJohannes Berg 	err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL,
38698cb08174SJohannes Berg 				     extack);
387077162022SJohn Fastabend 	if (err < 0)
387177162022SJohn Fastabend 		return err;
387277162022SJohn Fastabend 
387377162022SJohn Fastabend 	ndm = nlmsg_data(nlh);
387477162022SJohn Fastabend 	if (ndm->ndm_ifindex == 0) {
3875b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "invalid ifindex");
387677162022SJohn Fastabend 		return -EINVAL;
387777162022SJohn Fastabend 	}
387877162022SJohn Fastabend 
387977162022SJohn Fastabend 	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
388077162022SJohn Fastabend 	if (dev == NULL) {
3881b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "unknown ifindex");
388277162022SJohn Fastabend 		return -ENODEV;
388377162022SJohn Fastabend 	}
388477162022SJohn Fastabend 
388577162022SJohn Fastabend 	if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
3886b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "invalid address");
388777162022SJohn Fastabend 		return -EINVAL;
388877162022SJohn Fastabend 	}
388977162022SJohn Fastabend 
3890da715775SIdo Schimmel 	if (dev->type != ARPHRD_ETHER) {
3891da715775SIdo Schimmel 		NL_SET_ERR_MSG(extack, "FDB add only supported for Ethernet devices");
3892da715775SIdo Schimmel 		return -EINVAL;
3893da715775SIdo Schimmel 	}
3894da715775SIdo Schimmel 
389577162022SJohn Fastabend 	addr = nla_data(tb[NDA_LLADDR]);
389677162022SJohn Fastabend 
3897b88d12e4SFlorian Westphal 	err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
3898f6f6424bSJiri Pirko 	if (err)
3899f6f6424bSJiri Pirko 		return err;
3900f6f6424bSJiri Pirko 
390177162022SJohn Fastabend 	err = -EOPNOTSUPP;
390277162022SJohn Fastabend 
390377162022SJohn Fastabend 	/* Support fdb on master device the net/bridge default case */
390477162022SJohn Fastabend 	if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
390577162022SJohn Fastabend 	    (dev->priv_flags & IFF_BRIDGE_PORT)) {
3906898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3907898e5061SJiri Pirko 		const struct net_device_ops *ops = br_dev->netdev_ops;
3908898e5061SJiri Pirko 
3909f6f6424bSJiri Pirko 		err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
391087b0984eSPetr Machata 				       nlh->nlmsg_flags, extack);
391177162022SJohn Fastabend 		if (err)
391277162022SJohn Fastabend 			goto out;
391377162022SJohn Fastabend 		else
391477162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_MASTER;
391577162022SJohn Fastabend 	}
391677162022SJohn Fastabend 
391777162022SJohn Fastabend 	/* Embedded bridge, macvlan, and any other device support */
3918090096bfSVlad Yasevich 	if ((ndm->ndm_flags & NTF_SELF)) {
3919090096bfSVlad Yasevich 		if (dev->netdev_ops->ndo_fdb_add)
3920090096bfSVlad Yasevich 			err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
3921f6f6424bSJiri Pirko 							   vid,
392287b0984eSPetr Machata 							   nlh->nlmsg_flags,
392387b0984eSPetr Machata 							   extack);
3924090096bfSVlad Yasevich 		else
3925f6f6424bSJiri Pirko 			err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
392677162022SJohn Fastabend 					       nlh->nlmsg_flags);
392777162022SJohn Fastabend 
39283ff661c3SJohn Fastabend 		if (!err) {
3929b3379041SHubert Sokolowski 			rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
3930b3379041SHubert Sokolowski 					ndm->ndm_state);
393177162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_SELF;
393277162022SJohn Fastabend 		}
39333ff661c3SJohn Fastabend 	}
393477162022SJohn Fastabend out:
393577162022SJohn Fastabend 	return err;
393677162022SJohn Fastabend }
393777162022SJohn Fastabend 
3938a986967eSBart Van Assche /*
3939090096bfSVlad Yasevich  * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
3940090096bfSVlad Yasevich  */
3941090096bfSVlad Yasevich int ndo_dflt_fdb_del(struct ndmsg *ndm,
3942090096bfSVlad Yasevich 		     struct nlattr *tb[],
3943090096bfSVlad Yasevich 		     struct net_device *dev,
3944f6f6424bSJiri Pirko 		     const unsigned char *addr, u16 vid)
3945090096bfSVlad Yasevich {
3946c8a89c4aSAlexander Duyck 	int err = -EINVAL;
3947090096bfSVlad Yasevich 
3948090096bfSVlad Yasevich 	/* If aging addresses are supported device will need to
3949090096bfSVlad Yasevich 	 * implement its own handler for this.
3950090096bfSVlad Yasevich 	 */
395164535993SSridhar Samudrala 	if (!(ndm->ndm_state & NUD_PERMANENT)) {
3952090096bfSVlad Yasevich 		pr_info("%s: FDB only supports static addresses\n", dev->name);
3953c8a89c4aSAlexander Duyck 		return err;
3954090096bfSVlad Yasevich 	}
3955090096bfSVlad Yasevich 
3956090096bfSVlad Yasevich 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3957090096bfSVlad Yasevich 		err = dev_uc_del(dev, addr);
3958090096bfSVlad Yasevich 	else if (is_multicast_ether_addr(addr))
3959090096bfSVlad Yasevich 		err = dev_mc_del(dev, addr);
3960090096bfSVlad Yasevich 
3961090096bfSVlad Yasevich 	return err;
3962090096bfSVlad Yasevich }
3963090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_del);
3964090096bfSVlad Yasevich 
3965c21ef3e3SDavid Ahern static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
3966c21ef3e3SDavid Ahern 			struct netlink_ext_ack *extack)
396777162022SJohn Fastabend {
396877162022SJohn Fastabend 	struct net *net = sock_net(skb->sk);
396977162022SJohn Fastabend 	struct ndmsg *ndm;
39701690be63SVlad Yasevich 	struct nlattr *tb[NDA_MAX+1];
397177162022SJohn Fastabend 	struct net_device *dev;
397277162022SJohn Fastabend 	int err = -EINVAL;
397377162022SJohn Fastabend 	__u8 *addr;
3974f6f6424bSJiri Pirko 	u16 vid;
397577162022SJohn Fastabend 
397690f62cf3SEric W. Biederman 	if (!netlink_capable(skb, CAP_NET_ADMIN))
39771690be63SVlad Yasevich 		return -EPERM;
39781690be63SVlad Yasevich 
39798cb08174SJohannes Berg 	err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL,
39808cb08174SJohannes Berg 				     extack);
39811690be63SVlad Yasevich 	if (err < 0)
39821690be63SVlad Yasevich 		return err;
398377162022SJohn Fastabend 
398477162022SJohn Fastabend 	ndm = nlmsg_data(nlh);
398577162022SJohn Fastabend 	if (ndm->ndm_ifindex == 0) {
3986b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "invalid ifindex");
398777162022SJohn Fastabend 		return -EINVAL;
398877162022SJohn Fastabend 	}
398977162022SJohn Fastabend 
399077162022SJohn Fastabend 	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
399177162022SJohn Fastabend 	if (dev == NULL) {
3992b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "unknown ifindex");
399377162022SJohn Fastabend 		return -ENODEV;
399477162022SJohn Fastabend 	}
399577162022SJohn Fastabend 
39961690be63SVlad Yasevich 	if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
3997b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "invalid address");
399877162022SJohn Fastabend 		return -EINVAL;
399977162022SJohn Fastabend 	}
400077162022SJohn Fastabend 
4001da715775SIdo Schimmel 	if (dev->type != ARPHRD_ETHER) {
4002da715775SIdo Schimmel 		NL_SET_ERR_MSG(extack, "FDB delete only supported for Ethernet devices");
4003da715775SIdo Schimmel 		return -EINVAL;
4004da715775SIdo Schimmel 	}
4005da715775SIdo Schimmel 
40061690be63SVlad Yasevich 	addr = nla_data(tb[NDA_LLADDR]);
40071690be63SVlad Yasevich 
4008b88d12e4SFlorian Westphal 	err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
4009f6f6424bSJiri Pirko 	if (err)
4010f6f6424bSJiri Pirko 		return err;
4011f6f6424bSJiri Pirko 
401277162022SJohn Fastabend 	err = -EOPNOTSUPP;
401377162022SJohn Fastabend 
401477162022SJohn Fastabend 	/* Support fdb on master device the net/bridge default case */
401577162022SJohn Fastabend 	if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
401677162022SJohn Fastabend 	    (dev->priv_flags & IFF_BRIDGE_PORT)) {
4017898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4018898e5061SJiri Pirko 		const struct net_device_ops *ops = br_dev->netdev_ops;
401977162022SJohn Fastabend 
4020898e5061SJiri Pirko 		if (ops->ndo_fdb_del)
4021f6f6424bSJiri Pirko 			err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
402277162022SJohn Fastabend 
402377162022SJohn Fastabend 		if (err)
402477162022SJohn Fastabend 			goto out;
402577162022SJohn Fastabend 		else
402677162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_MASTER;
402777162022SJohn Fastabend 	}
402877162022SJohn Fastabend 
402977162022SJohn Fastabend 	/* Embedded bridge, macvlan, and any other device support */
4030090096bfSVlad Yasevich 	if (ndm->ndm_flags & NTF_SELF) {
4031090096bfSVlad Yasevich 		if (dev->netdev_ops->ndo_fdb_del)
4032f6f6424bSJiri Pirko 			err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
4033f6f6424bSJiri Pirko 							   vid);
4034090096bfSVlad Yasevich 		else
4035f6f6424bSJiri Pirko 			err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
403677162022SJohn Fastabend 
40373ff661c3SJohn Fastabend 		if (!err) {
4038b3379041SHubert Sokolowski 			rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
4039b3379041SHubert Sokolowski 					ndm->ndm_state);
404077162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_SELF;
404177162022SJohn Fastabend 		}
40423ff661c3SJohn Fastabend 	}
404377162022SJohn Fastabend out:
404477162022SJohn Fastabend 	return err;
404577162022SJohn Fastabend }
404677162022SJohn Fastabend 
4047d83b0603SJohn Fastabend static int nlmsg_populate_fdb(struct sk_buff *skb,
4048d83b0603SJohn Fastabend 			      struct netlink_callback *cb,
4049d83b0603SJohn Fastabend 			      struct net_device *dev,
4050d83b0603SJohn Fastabend 			      int *idx,
4051d83b0603SJohn Fastabend 			      struct netdev_hw_addr_list *list)
4052d83b0603SJohn Fastabend {
4053d83b0603SJohn Fastabend 	struct netdev_hw_addr *ha;
4054d83b0603SJohn Fastabend 	int err;
405515e47304SEric W. Biederman 	u32 portid, seq;
4056d83b0603SJohn Fastabend 
405715e47304SEric W. Biederman 	portid = NETLINK_CB(cb->skb).portid;
4058d83b0603SJohn Fastabend 	seq = cb->nlh->nlmsg_seq;
4059d83b0603SJohn Fastabend 
4060d83b0603SJohn Fastabend 	list_for_each_entry(ha, &list->list, list) {
4061d297653dSRoopa Prabhu 		if (*idx < cb->args[2])
4062d83b0603SJohn Fastabend 			goto skip;
4063d83b0603SJohn Fastabend 
40641e53d5bbSHubert Sokolowski 		err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
4065a7a558feSJohn Fastabend 					      portid, seq,
40661c104a6bSNicolas Dichtel 					      RTM_NEWNEIGH, NTF_SELF,
4067b3379041SHubert Sokolowski 					      NLM_F_MULTI, NUD_PERMANENT);
4068d83b0603SJohn Fastabend 		if (err < 0)
4069d83b0603SJohn Fastabend 			return err;
4070d83b0603SJohn Fastabend skip:
4071d83b0603SJohn Fastabend 		*idx += 1;
4072d83b0603SJohn Fastabend 	}
4073d83b0603SJohn Fastabend 	return 0;
4074d83b0603SJohn Fastabend }
4075d83b0603SJohn Fastabend 
4076d83b0603SJohn Fastabend /**
40772c53040fSBen Hutchings  * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
4078a986967eSBart Van Assche  * @skb: socket buffer to store message in
4079a986967eSBart Van Assche  * @cb: netlink callback
4080d83b0603SJohn Fastabend  * @dev: netdevice
4081a986967eSBart Van Assche  * @filter_dev: ignored
4082a986967eSBart Van Assche  * @idx: the number of FDB table entries dumped is added to *@idx
4083d83b0603SJohn Fastabend  *
4084d83b0603SJohn Fastabend  * Default netdevice operation to dump the existing unicast address list.
408591f3e7b1SJohn Fastabend  * Returns number of addresses from list put in skb.
4086d83b0603SJohn Fastabend  */
4087d83b0603SJohn Fastabend int ndo_dflt_fdb_dump(struct sk_buff *skb,
4088d83b0603SJohn Fastabend 		      struct netlink_callback *cb,
4089d83b0603SJohn Fastabend 		      struct net_device *dev,
40905d5eacb3SJamal Hadi Salim 		      struct net_device *filter_dev,
4091d297653dSRoopa Prabhu 		      int *idx)
4092d83b0603SJohn Fastabend {
4093d83b0603SJohn Fastabend 	int err;
4094d83b0603SJohn Fastabend 
409568883893SEric Dumazet 	if (dev->type != ARPHRD_ETHER)
409668883893SEric Dumazet 		return -EINVAL;
409768883893SEric Dumazet 
4098d83b0603SJohn Fastabend 	netif_addr_lock_bh(dev);
4099d297653dSRoopa Prabhu 	err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
4100d83b0603SJohn Fastabend 	if (err)
4101d83b0603SJohn Fastabend 		goto out;
41022934c9dbSZhang Shengju 	err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
4103d83b0603SJohn Fastabend out:
4104d83b0603SJohn Fastabend 	netif_addr_unlock_bh(dev);
4105d297653dSRoopa Prabhu 	return err;
4106d83b0603SJohn Fastabend }
4107d83b0603SJohn Fastabend EXPORT_SYMBOL(ndo_dflt_fdb_dump);
4108d83b0603SJohn Fastabend 
41098c6e137fSDavid Ahern static int valid_fdb_dump_strict(const struct nlmsghdr *nlh,
41108c6e137fSDavid Ahern 				 int *br_idx, int *brport_idx,
41118c6e137fSDavid Ahern 				 struct netlink_ext_ack *extack)
41128c6e137fSDavid Ahern {
41138c6e137fSDavid Ahern 	struct nlattr *tb[NDA_MAX + 1];
41148c6e137fSDavid Ahern 	struct ndmsg *ndm;
41158c6e137fSDavid Ahern 	int err, i;
41168c6e137fSDavid Ahern 
41178c6e137fSDavid Ahern 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
41188c6e137fSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid header for fdb dump request");
41198c6e137fSDavid Ahern 		return -EINVAL;
41208c6e137fSDavid Ahern 	}
41218c6e137fSDavid Ahern 
41228c6e137fSDavid Ahern 	ndm = nlmsg_data(nlh);
41238c6e137fSDavid Ahern 	if (ndm->ndm_pad1  || ndm->ndm_pad2  || ndm->ndm_state ||
41248c6e137fSDavid Ahern 	    ndm->ndm_flags || ndm->ndm_type) {
41258b73018fSNikolay Aleksandrov 		NL_SET_ERR_MSG(extack, "Invalid values in header for fdb dump request");
41268c6e137fSDavid Ahern 		return -EINVAL;
41278c6e137fSDavid Ahern 	}
41288c6e137fSDavid Ahern 
41298cb08174SJohannes Berg 	err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
41308cb08174SJohannes Berg 					    NDA_MAX, NULL, extack);
41318c6e137fSDavid Ahern 	if (err < 0)
41328c6e137fSDavid Ahern 		return err;
41338c6e137fSDavid Ahern 
41348c6e137fSDavid Ahern 	*brport_idx = ndm->ndm_ifindex;
41358c6e137fSDavid Ahern 	for (i = 0; i <= NDA_MAX; ++i) {
41368c6e137fSDavid Ahern 		if (!tb[i])
41378c6e137fSDavid Ahern 			continue;
41388c6e137fSDavid Ahern 
41398c6e137fSDavid Ahern 		switch (i) {
41408c6e137fSDavid Ahern 		case NDA_IFINDEX:
41418c6e137fSDavid Ahern 			if (nla_len(tb[i]) != sizeof(u32)) {
41428c6e137fSDavid Ahern 				NL_SET_ERR_MSG(extack, "Invalid IFINDEX attribute in fdb dump request");
41438c6e137fSDavid Ahern 				return -EINVAL;
41448c6e137fSDavid Ahern 			}
41458c6e137fSDavid Ahern 			*brport_idx = nla_get_u32(tb[NDA_IFINDEX]);
41468c6e137fSDavid Ahern 			break;
41478c6e137fSDavid Ahern 		case NDA_MASTER:
41488c6e137fSDavid Ahern 			if (nla_len(tb[i]) != sizeof(u32)) {
41498c6e137fSDavid Ahern 				NL_SET_ERR_MSG(extack, "Invalid MASTER attribute in fdb dump request");
41508c6e137fSDavid Ahern 				return -EINVAL;
41518c6e137fSDavid Ahern 			}
41528c6e137fSDavid Ahern 			*br_idx = nla_get_u32(tb[NDA_MASTER]);
41538c6e137fSDavid Ahern 			break;
41548c6e137fSDavid Ahern 		default:
41558c6e137fSDavid Ahern 			NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb dump request");
41568c6e137fSDavid Ahern 			return -EINVAL;
41578c6e137fSDavid Ahern 		}
41588c6e137fSDavid Ahern 	}
41598c6e137fSDavid Ahern 
41608c6e137fSDavid Ahern 	return 0;
41618c6e137fSDavid Ahern }
41628c6e137fSDavid Ahern 
41638dfbda19SDavid Ahern static int valid_fdb_dump_legacy(const struct nlmsghdr *nlh,
41648dfbda19SDavid Ahern 				 int *br_idx, int *brport_idx,
41658dfbda19SDavid Ahern 				 struct netlink_ext_ack *extack)
41668dfbda19SDavid Ahern {
41678dfbda19SDavid Ahern 	struct nlattr *tb[IFLA_MAX+1];
41688dfbda19SDavid Ahern 	int err;
41698dfbda19SDavid Ahern 
41708dfbda19SDavid Ahern 	/* A hack to preserve kernel<->userspace interface.
41718dfbda19SDavid Ahern 	 * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0.
41728dfbda19SDavid Ahern 	 * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails.
41738dfbda19SDavid Ahern 	 * So, check for ndmsg with an optional u32 attribute (not used here).
41748dfbda19SDavid Ahern 	 * Fortunately these sizes don't conflict with the size of ifinfomsg
41758dfbda19SDavid Ahern 	 * with an optional attribute.
41768dfbda19SDavid Ahern 	 */
41778dfbda19SDavid Ahern 	if (nlmsg_len(nlh) != sizeof(struct ndmsg) &&
41788dfbda19SDavid Ahern 	    (nlmsg_len(nlh) != sizeof(struct ndmsg) +
41798dfbda19SDavid Ahern 	     nla_attr_size(sizeof(u32)))) {
41804565d7e5SDavid Ahern 		struct ifinfomsg *ifm;
41814565d7e5SDavid Ahern 
41828cb08174SJohannes Berg 		err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
41838cb08174SJohannes Berg 					     tb, IFLA_MAX, ifla_policy,
41848cb08174SJohannes Berg 					     extack);
41858dfbda19SDavid Ahern 		if (err < 0) {
41868dfbda19SDavid Ahern 			return -EINVAL;
41878dfbda19SDavid Ahern 		} else if (err == 0) {
41888dfbda19SDavid Ahern 			if (tb[IFLA_MASTER])
41898dfbda19SDavid Ahern 				*br_idx = nla_get_u32(tb[IFLA_MASTER]);
41908dfbda19SDavid Ahern 		}
41918dfbda19SDavid Ahern 
41924565d7e5SDavid Ahern 		ifm = nlmsg_data(nlh);
41938dfbda19SDavid Ahern 		*brport_idx = ifm->ifi_index;
41948dfbda19SDavid Ahern 	}
41958dfbda19SDavid Ahern 	return 0;
41968dfbda19SDavid Ahern }
41978dfbda19SDavid Ahern 
419877162022SJohn Fastabend static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
419977162022SJohn Fastabend {
420077162022SJohn Fastabend 	struct net_device *dev;
42015e6d2435SJamal Hadi Salim 	struct net_device *br_dev = NULL;
42025e6d2435SJamal Hadi Salim 	const struct net_device_ops *ops = NULL;
42035e6d2435SJamal Hadi Salim 	const struct net_device_ops *cops = NULL;
42045e6d2435SJamal Hadi Salim 	struct net *net = sock_net(skb->sk);
4205d297653dSRoopa Prabhu 	struct hlist_head *head;
42065e6d2435SJamal Hadi Salim 	int brport_idx = 0;
42075e6d2435SJamal Hadi Salim 	int br_idx = 0;
4208d297653dSRoopa Prabhu 	int h, s_h;
4209d297653dSRoopa Prabhu 	int idx = 0, s_idx;
4210d297653dSRoopa Prabhu 	int err = 0;
4211d297653dSRoopa Prabhu 	int fidx = 0;
421277162022SJohn Fastabend 
42138c6e137fSDavid Ahern 	if (cb->strict_check)
42148c6e137fSDavid Ahern 		err = valid_fdb_dump_strict(cb->nlh, &br_idx, &brport_idx,
42158c6e137fSDavid Ahern 					    cb->extack);
42168c6e137fSDavid Ahern 	else
42178dfbda19SDavid Ahern 		err = valid_fdb_dump_legacy(cb->nlh, &br_idx, &brport_idx,
42188dfbda19SDavid Ahern 					    cb->extack);
42198dfbda19SDavid Ahern 	if (err < 0)
42208dfbda19SDavid Ahern 		return err;
42215e6d2435SJamal Hadi Salim 
42225e6d2435SJamal Hadi Salim 	if (br_idx) {
42235e6d2435SJamal Hadi Salim 		br_dev = __dev_get_by_index(net, br_idx);
42245e6d2435SJamal Hadi Salim 		if (!br_dev)
42255e6d2435SJamal Hadi Salim 			return -ENODEV;
42265e6d2435SJamal Hadi Salim 
4227898e5061SJiri Pirko 		ops = br_dev->netdev_ops;
42285e6d2435SJamal Hadi Salim 	}
42295e6d2435SJamal Hadi Salim 
4230d297653dSRoopa Prabhu 	s_h = cb->args[0];
4231d297653dSRoopa Prabhu 	s_idx = cb->args[1];
4232d297653dSRoopa Prabhu 
4233d297653dSRoopa Prabhu 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4234d297653dSRoopa Prabhu 		idx = 0;
4235d297653dSRoopa Prabhu 		head = &net->dev_index_head[h];
4236d297653dSRoopa Prabhu 		hlist_for_each_entry(dev, head, index_hlist) {
4237d297653dSRoopa Prabhu 
42385e6d2435SJamal Hadi Salim 			if (brport_idx && (dev->ifindex != brport_idx))
42395e6d2435SJamal Hadi Salim 				continue;
42405e6d2435SJamal Hadi Salim 
42415e6d2435SJamal Hadi Salim 			if (!br_idx) { /* user did not specify a specific bridge */
42425e6d2435SJamal Hadi Salim 				if (dev->priv_flags & IFF_BRIDGE_PORT) {
42435e6d2435SJamal Hadi Salim 					br_dev = netdev_master_upper_dev_get(dev);
42445e6d2435SJamal Hadi Salim 					cops = br_dev->netdev_ops;
42455e6d2435SJamal Hadi Salim 				}
42465e6d2435SJamal Hadi Salim 			} else {
42475e6d2435SJamal Hadi Salim 				if (dev != br_dev &&
42485e6d2435SJamal Hadi Salim 				    !(dev->priv_flags & IFF_BRIDGE_PORT))
42495e6d2435SJamal Hadi Salim 					continue;
42505e6d2435SJamal Hadi Salim 
42515e6d2435SJamal Hadi Salim 				if (br_dev != netdev_master_upper_dev_get(dev) &&
42525e6d2435SJamal Hadi Salim 				    !(dev->priv_flags & IFF_EBRIDGE))
42535e6d2435SJamal Hadi Salim 					continue;
42545e6d2435SJamal Hadi Salim 				cops = ops;
42555e6d2435SJamal Hadi Salim 			}
42565e6d2435SJamal Hadi Salim 
4257d297653dSRoopa Prabhu 			if (idx < s_idx)
4258d297653dSRoopa Prabhu 				goto cont;
4259d297653dSRoopa Prabhu 
42605e6d2435SJamal Hadi Salim 			if (dev->priv_flags & IFF_BRIDGE_PORT) {
4261d297653dSRoopa Prabhu 				if (cops && cops->ndo_fdb_dump) {
4262d297653dSRoopa Prabhu 					err = cops->ndo_fdb_dump(skb, cb,
4263d297653dSRoopa Prabhu 								br_dev, dev,
4264d297653dSRoopa Prabhu 								&fidx);
4265d297653dSRoopa Prabhu 					if (err == -EMSGSIZE)
4266d297653dSRoopa Prabhu 						goto out;
426777162022SJohn Fastabend 				}
4268d297653dSRoopa Prabhu 			}
426977162022SJohn Fastabend 
42705e6d2435SJamal Hadi Salim 			if (dev->netdev_ops->ndo_fdb_dump)
4271d297653dSRoopa Prabhu 				err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
4272d297653dSRoopa Prabhu 								    dev, NULL,
4273d297653dSRoopa Prabhu 								    &fidx);
42746cb69742SHubert Sokolowski 			else
4275d297653dSRoopa Prabhu 				err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
4276d297653dSRoopa Prabhu 							&fidx);
4277d297653dSRoopa Prabhu 			if (err == -EMSGSIZE)
4278d297653dSRoopa Prabhu 				goto out;
42795e6d2435SJamal Hadi Salim 
42805e6d2435SJamal Hadi Salim 			cops = NULL;
4281d297653dSRoopa Prabhu 
4282d297653dSRoopa Prabhu 			/* reset fdb offset to 0 for rest of the interfaces */
4283d297653dSRoopa Prabhu 			cb->args[2] = 0;
4284d297653dSRoopa Prabhu 			fidx = 0;
4285d297653dSRoopa Prabhu cont:
4286d297653dSRoopa Prabhu 			idx++;
4287d297653dSRoopa Prabhu 		}
428877162022SJohn Fastabend 	}
428977162022SJohn Fastabend 
4290d297653dSRoopa Prabhu out:
4291d297653dSRoopa Prabhu 	cb->args[0] = h;
4292d297653dSRoopa Prabhu 	cb->args[1] = idx;
4293d297653dSRoopa Prabhu 	cb->args[2] = fidx;
4294d297653dSRoopa Prabhu 
429577162022SJohn Fastabend 	return skb->len;
429677162022SJohn Fastabend }
429777162022SJohn Fastabend 
42985b2f94b2SRoopa Prabhu static int valid_fdb_get_strict(const struct nlmsghdr *nlh,
42995b2f94b2SRoopa Prabhu 				struct nlattr **tb, u8 *ndm_flags,
43005b2f94b2SRoopa Prabhu 				int *br_idx, int *brport_idx, u8 **addr,
43015b2f94b2SRoopa Prabhu 				u16 *vid, struct netlink_ext_ack *extack)
43025b2f94b2SRoopa Prabhu {
43035b2f94b2SRoopa Prabhu 	struct ndmsg *ndm;
43045b2f94b2SRoopa Prabhu 	int err, i;
43055b2f94b2SRoopa Prabhu 
43065b2f94b2SRoopa Prabhu 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
43075b2f94b2SRoopa Prabhu 		NL_SET_ERR_MSG(extack, "Invalid header for fdb get request");
43085b2f94b2SRoopa Prabhu 		return -EINVAL;
43095b2f94b2SRoopa Prabhu 	}
43105b2f94b2SRoopa Prabhu 
43115b2f94b2SRoopa Prabhu 	ndm = nlmsg_data(nlh);
43125b2f94b2SRoopa Prabhu 	if (ndm->ndm_pad1  || ndm->ndm_pad2  || ndm->ndm_state ||
43135b2f94b2SRoopa Prabhu 	    ndm->ndm_type) {
43145b2f94b2SRoopa Prabhu 		NL_SET_ERR_MSG(extack, "Invalid values in header for fdb get request");
43155b2f94b2SRoopa Prabhu 		return -EINVAL;
43165b2f94b2SRoopa Prabhu 	}
43175b2f94b2SRoopa Prabhu 
43185b2f94b2SRoopa Prabhu 	if (ndm->ndm_flags & ~(NTF_MASTER | NTF_SELF)) {
43195b2f94b2SRoopa Prabhu 		NL_SET_ERR_MSG(extack, "Invalid flags in header for fdb get request");
43205b2f94b2SRoopa Prabhu 		return -EINVAL;
43215b2f94b2SRoopa Prabhu 	}
43225b2f94b2SRoopa Prabhu 
43238cb08174SJohannes Berg 	err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
43248cb08174SJohannes Berg 					    NDA_MAX, nda_policy, extack);
43255b2f94b2SRoopa Prabhu 	if (err < 0)
43265b2f94b2SRoopa Prabhu 		return err;
43275b2f94b2SRoopa Prabhu 
43285b2f94b2SRoopa Prabhu 	*ndm_flags = ndm->ndm_flags;
43295b2f94b2SRoopa Prabhu 	*brport_idx = ndm->ndm_ifindex;
43305b2f94b2SRoopa Prabhu 	for (i = 0; i <= NDA_MAX; ++i) {
43315b2f94b2SRoopa Prabhu 		if (!tb[i])
43325b2f94b2SRoopa Prabhu 			continue;
43335b2f94b2SRoopa Prabhu 
43345b2f94b2SRoopa Prabhu 		switch (i) {
43355b2f94b2SRoopa Prabhu 		case NDA_MASTER:
43365b2f94b2SRoopa Prabhu 			*br_idx = nla_get_u32(tb[i]);
43375b2f94b2SRoopa Prabhu 			break;
43385b2f94b2SRoopa Prabhu 		case NDA_LLADDR:
43395b2f94b2SRoopa Prabhu 			if (nla_len(tb[i]) != ETH_ALEN) {
43405b2f94b2SRoopa Prabhu 				NL_SET_ERR_MSG(extack, "Invalid address in fdb get request");
43415b2f94b2SRoopa Prabhu 				return -EINVAL;
43425b2f94b2SRoopa Prabhu 			}
43435b2f94b2SRoopa Prabhu 			*addr = nla_data(tb[i]);
43445b2f94b2SRoopa Prabhu 			break;
43455b2f94b2SRoopa Prabhu 		case NDA_VLAN:
43465b2f94b2SRoopa Prabhu 			err = fdb_vid_parse(tb[i], vid, extack);
43475b2f94b2SRoopa Prabhu 			if (err)
43485b2f94b2SRoopa Prabhu 				return err;
43495b2f94b2SRoopa Prabhu 			break;
43505b2f94b2SRoopa Prabhu 		case NDA_VNI:
43515b2f94b2SRoopa Prabhu 			break;
43525b2f94b2SRoopa Prabhu 		default:
43535b2f94b2SRoopa Prabhu 			NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb get request");
43545b2f94b2SRoopa Prabhu 			return -EINVAL;
43555b2f94b2SRoopa Prabhu 		}
43565b2f94b2SRoopa Prabhu 	}
43575b2f94b2SRoopa Prabhu 
43585b2f94b2SRoopa Prabhu 	return 0;
43595b2f94b2SRoopa Prabhu }
43605b2f94b2SRoopa Prabhu 
43615b2f94b2SRoopa Prabhu static int rtnl_fdb_get(struct sk_buff *in_skb, struct nlmsghdr *nlh,
43625b2f94b2SRoopa Prabhu 			struct netlink_ext_ack *extack)
43635b2f94b2SRoopa Prabhu {
43645b2f94b2SRoopa Prabhu 	struct net_device *dev = NULL, *br_dev = NULL;
43655b2f94b2SRoopa Prabhu 	const struct net_device_ops *ops = NULL;
43665b2f94b2SRoopa Prabhu 	struct net *net = sock_net(in_skb->sk);
43675b2f94b2SRoopa Prabhu 	struct nlattr *tb[NDA_MAX + 1];
43685b2f94b2SRoopa Prabhu 	struct sk_buff *skb;
43695b2f94b2SRoopa Prabhu 	int brport_idx = 0;
43705b2f94b2SRoopa Prabhu 	u8 ndm_flags = 0;
43715b2f94b2SRoopa Prabhu 	int br_idx = 0;
43725b2f94b2SRoopa Prabhu 	u8 *addr = NULL;
43735b2f94b2SRoopa Prabhu 	u16 vid = 0;
43745b2f94b2SRoopa Prabhu 	int err;
43755b2f94b2SRoopa Prabhu 
43765b2f94b2SRoopa Prabhu 	err = valid_fdb_get_strict(nlh, tb, &ndm_flags, &br_idx,
43775b2f94b2SRoopa Prabhu 				   &brport_idx, &addr, &vid, extack);
43785b2f94b2SRoopa Prabhu 	if (err < 0)
43795b2f94b2SRoopa Prabhu 		return err;
43805b2f94b2SRoopa Prabhu 
4381f989d03eSNikolay Aleksandrov 	if (!addr) {
4382f989d03eSNikolay Aleksandrov 		NL_SET_ERR_MSG(extack, "Missing lookup address for fdb get request");
4383f989d03eSNikolay Aleksandrov 		return -EINVAL;
4384f989d03eSNikolay Aleksandrov 	}
4385f989d03eSNikolay Aleksandrov 
43865b2f94b2SRoopa Prabhu 	if (brport_idx) {
43875b2f94b2SRoopa Prabhu 		dev = __dev_get_by_index(net, brport_idx);
43885b2f94b2SRoopa Prabhu 		if (!dev) {
43895b2f94b2SRoopa Prabhu 			NL_SET_ERR_MSG(extack, "Unknown device ifindex");
43905b2f94b2SRoopa Prabhu 			return -ENODEV;
43915b2f94b2SRoopa Prabhu 		}
43925b2f94b2SRoopa Prabhu 	}
43935b2f94b2SRoopa Prabhu 
43945b2f94b2SRoopa Prabhu 	if (br_idx) {
43955b2f94b2SRoopa Prabhu 		if (dev) {
43965b2f94b2SRoopa Prabhu 			NL_SET_ERR_MSG(extack, "Master and device are mutually exclusive");
43975b2f94b2SRoopa Prabhu 			return -EINVAL;
43985b2f94b2SRoopa Prabhu 		}
43995b2f94b2SRoopa Prabhu 
44005b2f94b2SRoopa Prabhu 		br_dev = __dev_get_by_index(net, br_idx);
44015b2f94b2SRoopa Prabhu 		if (!br_dev) {
44025b2f94b2SRoopa Prabhu 			NL_SET_ERR_MSG(extack, "Invalid master ifindex");
44035b2f94b2SRoopa Prabhu 			return -EINVAL;
44045b2f94b2SRoopa Prabhu 		}
44055b2f94b2SRoopa Prabhu 		ops = br_dev->netdev_ops;
44065b2f94b2SRoopa Prabhu 	}
44075b2f94b2SRoopa Prabhu 
44085b2f94b2SRoopa Prabhu 	if (dev) {
44095b2f94b2SRoopa Prabhu 		if (!ndm_flags || (ndm_flags & NTF_MASTER)) {
44105b2f94b2SRoopa Prabhu 			if (!(dev->priv_flags & IFF_BRIDGE_PORT)) {
44115b2f94b2SRoopa Prabhu 				NL_SET_ERR_MSG(extack, "Device is not a bridge port");
44125b2f94b2SRoopa Prabhu 				return -EINVAL;
44135b2f94b2SRoopa Prabhu 			}
44145b2f94b2SRoopa Prabhu 			br_dev = netdev_master_upper_dev_get(dev);
44155b2f94b2SRoopa Prabhu 			if (!br_dev) {
44165b2f94b2SRoopa Prabhu 				NL_SET_ERR_MSG(extack, "Master of device not found");
44175b2f94b2SRoopa Prabhu 				return -EINVAL;
44185b2f94b2SRoopa Prabhu 			}
44195b2f94b2SRoopa Prabhu 			ops = br_dev->netdev_ops;
44205b2f94b2SRoopa Prabhu 		} else {
44215b2f94b2SRoopa Prabhu 			if (!(ndm_flags & NTF_SELF)) {
44225b2f94b2SRoopa Prabhu 				NL_SET_ERR_MSG(extack, "Missing NTF_SELF");
44235b2f94b2SRoopa Prabhu 				return -EINVAL;
44245b2f94b2SRoopa Prabhu 			}
44255b2f94b2SRoopa Prabhu 			ops = dev->netdev_ops;
44265b2f94b2SRoopa Prabhu 		}
44275b2f94b2SRoopa Prabhu 	}
44285b2f94b2SRoopa Prabhu 
44295b2f94b2SRoopa Prabhu 	if (!br_dev && !dev) {
44305b2f94b2SRoopa Prabhu 		NL_SET_ERR_MSG(extack, "No device specified");
44315b2f94b2SRoopa Prabhu 		return -ENODEV;
44325b2f94b2SRoopa Prabhu 	}
44335b2f94b2SRoopa Prabhu 
44345b2f94b2SRoopa Prabhu 	if (!ops || !ops->ndo_fdb_get) {
44355b2f94b2SRoopa Prabhu 		NL_SET_ERR_MSG(extack, "Fdb get operation not supported by device");
44365b2f94b2SRoopa Prabhu 		return -EOPNOTSUPP;
44375b2f94b2SRoopa Prabhu 	}
44385b2f94b2SRoopa Prabhu 
44395b2f94b2SRoopa Prabhu 	skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
44405b2f94b2SRoopa Prabhu 	if (!skb)
44415b2f94b2SRoopa Prabhu 		return -ENOBUFS;
44425b2f94b2SRoopa Prabhu 
44435b2f94b2SRoopa Prabhu 	if (br_dev)
44445b2f94b2SRoopa Prabhu 		dev = br_dev;
44455b2f94b2SRoopa Prabhu 	err = ops->ndo_fdb_get(skb, tb, dev, addr, vid,
44465b2f94b2SRoopa Prabhu 			       NETLINK_CB(in_skb).portid,
44475b2f94b2SRoopa Prabhu 			       nlh->nlmsg_seq, extack);
44485b2f94b2SRoopa Prabhu 	if (err)
44495b2f94b2SRoopa Prabhu 		goto out;
44505b2f94b2SRoopa Prabhu 
44515b2f94b2SRoopa Prabhu 	return rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
44525b2f94b2SRoopa Prabhu out:
44535b2f94b2SRoopa Prabhu 	kfree_skb(skb);
44545b2f94b2SRoopa Prabhu 	return err;
44555b2f94b2SRoopa Prabhu }
44565b2f94b2SRoopa Prabhu 
44572c3c031cSScott Feldman static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
44582c3c031cSScott Feldman 			       unsigned int attrnum, unsigned int flag)
44592c3c031cSScott Feldman {
44602c3c031cSScott Feldman 	if (mask & flag)
44612c3c031cSScott Feldman 		return nla_put_u8(skb, attrnum, !!(flags & flag));
44622c3c031cSScott Feldman 	return 0;
44632c3c031cSScott Feldman }
44642c3c031cSScott Feldman 
4465815cccbfSJohn Fastabend int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
44662c3c031cSScott Feldman 			    struct net_device *dev, u16 mode,
44677d4f8d87SScott Feldman 			    u32 flags, u32 mask, int nlflags,
44687d4f8d87SScott Feldman 			    u32 filter_mask,
44697d4f8d87SScott Feldman 			    int (*vlan_fill)(struct sk_buff *skb,
44707d4f8d87SScott Feldman 					     struct net_device *dev,
44717d4f8d87SScott Feldman 					     u32 filter_mask))
4472815cccbfSJohn Fastabend {
4473815cccbfSJohn Fastabend 	struct nlmsghdr *nlh;
4474815cccbfSJohn Fastabend 	struct ifinfomsg *ifm;
4475815cccbfSJohn Fastabend 	struct nlattr *br_afspec;
44762c3c031cSScott Feldman 	struct nlattr *protinfo;
4477815cccbfSJohn Fastabend 	u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
4478898e5061SJiri Pirko 	struct net_device *br_dev = netdev_master_upper_dev_get(dev);
44797d4f8d87SScott Feldman 	int err = 0;
4480815cccbfSJohn Fastabend 
448146c264daSNicolas Dichtel 	nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
4482815cccbfSJohn Fastabend 	if (nlh == NULL)
4483815cccbfSJohn Fastabend 		return -EMSGSIZE;
4484815cccbfSJohn Fastabend 
4485815cccbfSJohn Fastabend 	ifm = nlmsg_data(nlh);
4486815cccbfSJohn Fastabend 	ifm->ifi_family = AF_BRIDGE;
4487815cccbfSJohn Fastabend 	ifm->__ifi_pad = 0;
4488815cccbfSJohn Fastabend 	ifm->ifi_type = dev->type;
4489815cccbfSJohn Fastabend 	ifm->ifi_index = dev->ifindex;
4490815cccbfSJohn Fastabend 	ifm->ifi_flags = dev_get_flags(dev);
4491815cccbfSJohn Fastabend 	ifm->ifi_change = 0;
4492815cccbfSJohn Fastabend 
4493815cccbfSJohn Fastabend 
4494815cccbfSJohn Fastabend 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
4495815cccbfSJohn Fastabend 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
4496815cccbfSJohn Fastabend 	    nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
4497898e5061SJiri Pirko 	    (br_dev &&
4498898e5061SJiri Pirko 	     nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
4499815cccbfSJohn Fastabend 	    (dev->addr_len &&
4500815cccbfSJohn Fastabend 	     nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
4501a54acb3aSNicolas Dichtel 	    (dev->ifindex != dev_get_iflink(dev) &&
4502a54acb3aSNicolas Dichtel 	     nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
4503815cccbfSJohn Fastabend 		goto nla_put_failure;
4504815cccbfSJohn Fastabend 
4505ae0be8deSMichal Kubecek 	br_afspec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
4506815cccbfSJohn Fastabend 	if (!br_afspec)
4507815cccbfSJohn Fastabend 		goto nla_put_failure;
4508815cccbfSJohn Fastabend 
45091d460b98SRoopa Prabhu 	if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
4510815cccbfSJohn Fastabend 		nla_nest_cancel(skb, br_afspec);
4511815cccbfSJohn Fastabend 		goto nla_put_failure;
4512815cccbfSJohn Fastabend 	}
45131d460b98SRoopa Prabhu 
45141d460b98SRoopa Prabhu 	if (mode != BRIDGE_MODE_UNDEF) {
45151d460b98SRoopa Prabhu 		if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
45161d460b98SRoopa Prabhu 			nla_nest_cancel(skb, br_afspec);
45171d460b98SRoopa Prabhu 			goto nla_put_failure;
45181d460b98SRoopa Prabhu 		}
45191d460b98SRoopa Prabhu 	}
45207d4f8d87SScott Feldman 	if (vlan_fill) {
45217d4f8d87SScott Feldman 		err = vlan_fill(skb, dev, filter_mask);
45227d4f8d87SScott Feldman 		if (err) {
45237d4f8d87SScott Feldman 			nla_nest_cancel(skb, br_afspec);
45247d4f8d87SScott Feldman 			goto nla_put_failure;
45257d4f8d87SScott Feldman 		}
45267d4f8d87SScott Feldman 	}
4527815cccbfSJohn Fastabend 	nla_nest_end(skb, br_afspec);
4528815cccbfSJohn Fastabend 
4529ae0be8deSMichal Kubecek 	protinfo = nla_nest_start(skb, IFLA_PROTINFO);
45302c3c031cSScott Feldman 	if (!protinfo)
45312c3c031cSScott Feldman 		goto nla_put_failure;
45322c3c031cSScott Feldman 
45332c3c031cSScott Feldman 	if (brport_nla_put_flag(skb, flags, mask,
45342c3c031cSScott Feldman 				IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
45352c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
45362c3c031cSScott Feldman 				IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
45372c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
45382c3c031cSScott Feldman 				IFLA_BRPORT_FAST_LEAVE,
45392c3c031cSScott Feldman 				BR_MULTICAST_FAST_LEAVE) ||
45402c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
45412c3c031cSScott Feldman 				IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
45422c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
45432c3c031cSScott Feldman 				IFLA_BRPORT_LEARNING, BR_LEARNING) ||
45442c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
45452c3c031cSScott Feldman 				IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
45462c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
45472c3c031cSScott Feldman 				IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
45482c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
45492c3c031cSScott Feldman 				IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
45502c3c031cSScott Feldman 		nla_nest_cancel(skb, protinfo);
45512c3c031cSScott Feldman 		goto nla_put_failure;
45522c3c031cSScott Feldman 	}
45532c3c031cSScott Feldman 
45542c3c031cSScott Feldman 	nla_nest_end(skb, protinfo);
45552c3c031cSScott Feldman 
4556053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
4557053c095aSJohannes Berg 	return 0;
4558815cccbfSJohn Fastabend nla_put_failure:
4559815cccbfSJohn Fastabend 	nlmsg_cancel(skb, nlh);
45607d4f8d87SScott Feldman 	return err ? err : -EMSGSIZE;
4561815cccbfSJohn Fastabend }
45627d4f8d87SScott Feldman EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
4563815cccbfSJohn Fastabend 
45642d011be8SDavid Ahern static int valid_bridge_getlink_req(const struct nlmsghdr *nlh,
45652d011be8SDavid Ahern 				    bool strict_check, u32 *filter_mask,
45662d011be8SDavid Ahern 				    struct netlink_ext_ack *extack)
45672d011be8SDavid Ahern {
45682d011be8SDavid Ahern 	struct nlattr *tb[IFLA_MAX+1];
45692d011be8SDavid Ahern 	int err, i;
45702d011be8SDavid Ahern 
45712d011be8SDavid Ahern 	if (strict_check) {
45722d011be8SDavid Ahern 		struct ifinfomsg *ifm;
45732d011be8SDavid Ahern 
45742d011be8SDavid Ahern 		if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
45752d011be8SDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid header for bridge link dump");
45762d011be8SDavid Ahern 			return -EINVAL;
45772d011be8SDavid Ahern 		}
45782d011be8SDavid Ahern 
45792d011be8SDavid Ahern 		ifm = nlmsg_data(nlh);
45802d011be8SDavid Ahern 		if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
45812d011be8SDavid Ahern 		    ifm->ifi_change || ifm->ifi_index) {
45822d011be8SDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid values in header for bridge link dump request");
45832d011be8SDavid Ahern 			return -EINVAL;
45842d011be8SDavid Ahern 		}
45852d011be8SDavid Ahern 
45868cb08174SJohannes Berg 		err = nlmsg_parse_deprecated_strict(nlh,
45878cb08174SJohannes Berg 						    sizeof(struct ifinfomsg),
45888cb08174SJohannes Berg 						    tb, IFLA_MAX, ifla_policy,
45898cb08174SJohannes Berg 						    extack);
45902d011be8SDavid Ahern 	} else {
45918cb08174SJohannes Berg 		err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
45928cb08174SJohannes Berg 					     tb, IFLA_MAX, ifla_policy,
45938cb08174SJohannes Berg 					     extack);
45942d011be8SDavid Ahern 	}
45952d011be8SDavid Ahern 	if (err < 0)
45962d011be8SDavid Ahern 		return err;
45972d011be8SDavid Ahern 
45982d011be8SDavid Ahern 	/* new attributes should only be added with strict checking */
45992d011be8SDavid Ahern 	for (i = 0; i <= IFLA_MAX; ++i) {
46002d011be8SDavid Ahern 		if (!tb[i])
46012d011be8SDavid Ahern 			continue;
46022d011be8SDavid Ahern 
46032d011be8SDavid Ahern 		switch (i) {
46042d011be8SDavid Ahern 		case IFLA_EXT_MASK:
46052d011be8SDavid Ahern 			*filter_mask = nla_get_u32(tb[i]);
46062d011be8SDavid Ahern 			break;
46072d011be8SDavid Ahern 		default:
46082d011be8SDavid Ahern 			if (strict_check) {
46092d011be8SDavid Ahern 				NL_SET_ERR_MSG(extack, "Unsupported attribute in bridge link dump request");
46102d011be8SDavid Ahern 				return -EINVAL;
46112d011be8SDavid Ahern 			}
46122d011be8SDavid Ahern 		}
46132d011be8SDavid Ahern 	}
46142d011be8SDavid Ahern 
46152d011be8SDavid Ahern 	return 0;
46162d011be8SDavid Ahern }
46172d011be8SDavid Ahern 
4618e5a55a89SJohn Fastabend static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
4619e5a55a89SJohn Fastabend {
46202d011be8SDavid Ahern 	const struct nlmsghdr *nlh = cb->nlh;
4621e5a55a89SJohn Fastabend 	struct net *net = sock_net(skb->sk);
4622e5a55a89SJohn Fastabend 	struct net_device *dev;
4623e5a55a89SJohn Fastabend 	int idx = 0;
4624e5a55a89SJohn Fastabend 	u32 portid = NETLINK_CB(cb->skb).portid;
46252d011be8SDavid Ahern 	u32 seq = nlh->nlmsg_seq;
46266cbdceebSVlad Yasevich 	u32 filter_mask = 0;
4627d64f69b0SRoopa Prabhu 	int err;
46286cbdceebSVlad Yasevich 
46292d011be8SDavid Ahern 	err = valid_bridge_getlink_req(nlh, cb->strict_check, &filter_mask,
46302d011be8SDavid Ahern 				       cb->extack);
46312d011be8SDavid Ahern 	if (err < 0 && cb->strict_check)
46322d011be8SDavid Ahern 		return err;
4633e5a55a89SJohn Fastabend 
4634e5a55a89SJohn Fastabend 	rcu_read_lock();
4635e5a55a89SJohn Fastabend 	for_each_netdev_rcu(net, dev) {
4636e5a55a89SJohn Fastabend 		const struct net_device_ops *ops = dev->netdev_ops;
4637898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4638e5a55a89SJohn Fastabend 
4639898e5061SJiri Pirko 		if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
4640d64f69b0SRoopa Prabhu 			if (idx >= cb->args[0]) {
4641d64f69b0SRoopa Prabhu 				err = br_dev->netdev_ops->ndo_bridge_getlink(
4642d64f69b0SRoopa Prabhu 						skb, portid, seq, dev,
4643d64f69b0SRoopa Prabhu 						filter_mask, NLM_F_MULTI);
4644f6c5775fSDavid Ahern 				if (err < 0 && err != -EOPNOTSUPP) {
4645f6c5775fSDavid Ahern 					if (likely(skb->len))
4646e5a55a89SJohn Fastabend 						break;
4647f6c5775fSDavid Ahern 
4648f6c5775fSDavid Ahern 					goto out_err;
4649f6c5775fSDavid Ahern 				}
4650d64f69b0SRoopa Prabhu 			}
4651e5a55a89SJohn Fastabend 			idx++;
4652e5a55a89SJohn Fastabend 		}
4653e5a55a89SJohn Fastabend 
4654e5a55a89SJohn Fastabend 		if (ops->ndo_bridge_getlink) {
4655d64f69b0SRoopa Prabhu 			if (idx >= cb->args[0]) {
4656d64f69b0SRoopa Prabhu 				err = ops->ndo_bridge_getlink(skb, portid,
4657d64f69b0SRoopa Prabhu 							      seq, dev,
465846c264daSNicolas Dichtel 							      filter_mask,
4659d64f69b0SRoopa Prabhu 							      NLM_F_MULTI);
4660f6c5775fSDavid Ahern 				if (err < 0 && err != -EOPNOTSUPP) {
4661f6c5775fSDavid Ahern 					if (likely(skb->len))
4662e5a55a89SJohn Fastabend 						break;
4663f6c5775fSDavid Ahern 
4664f6c5775fSDavid Ahern 					goto out_err;
4665f6c5775fSDavid Ahern 				}
4666d64f69b0SRoopa Prabhu 			}
4667e5a55a89SJohn Fastabend 			idx++;
4668e5a55a89SJohn Fastabend 		}
4669e5a55a89SJohn Fastabend 	}
4670f6c5775fSDavid Ahern 	err = skb->len;
4671f6c5775fSDavid Ahern out_err:
4672e5a55a89SJohn Fastabend 	rcu_read_unlock();
4673e5a55a89SJohn Fastabend 	cb->args[0] = idx;
4674e5a55a89SJohn Fastabend 
4675f6c5775fSDavid Ahern 	return err;
4676e5a55a89SJohn Fastabend }
4677e5a55a89SJohn Fastabend 
46782469ffd7SJohn Fastabend static inline size_t bridge_nlmsg_size(void)
46792469ffd7SJohn Fastabend {
46802469ffd7SJohn Fastabend 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
46812469ffd7SJohn Fastabend 		+ nla_total_size(IFNAMSIZ)	/* IFLA_IFNAME */
46822469ffd7SJohn Fastabend 		+ nla_total_size(MAX_ADDR_LEN)	/* IFLA_ADDRESS */
46832469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_MASTER */
46842469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_MTU */
46852469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_LINK */
46862469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_OPERSTATE */
46872469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u8))	/* IFLA_PROTINFO */
46882469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(struct nlattr))	/* IFLA_AF_SPEC */
46892469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u16))	/* IFLA_BRIDGE_FLAGS */
46902469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u16));	/* IFLA_BRIDGE_MODE */
46912469ffd7SJohn Fastabend }
46922469ffd7SJohn Fastabend 
469302dba438SRoopa Prabhu static int rtnl_bridge_notify(struct net_device *dev)
46942469ffd7SJohn Fastabend {
46952469ffd7SJohn Fastabend 	struct net *net = dev_net(dev);
46962469ffd7SJohn Fastabend 	struct sk_buff *skb;
46972469ffd7SJohn Fastabend 	int err = -EOPNOTSUPP;
46982469ffd7SJohn Fastabend 
469902dba438SRoopa Prabhu 	if (!dev->netdev_ops->ndo_bridge_getlink)
470002dba438SRoopa Prabhu 		return 0;
470102dba438SRoopa Prabhu 
47022469ffd7SJohn Fastabend 	skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
47032469ffd7SJohn Fastabend 	if (!skb) {
47042469ffd7SJohn Fastabend 		err = -ENOMEM;
47052469ffd7SJohn Fastabend 		goto errout;
47062469ffd7SJohn Fastabend 	}
47072469ffd7SJohn Fastabend 
470846c264daSNicolas Dichtel 	err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
4709c38e01b8SJohn Fastabend 	if (err < 0)
4710c38e01b8SJohn Fastabend 		goto errout;
47112469ffd7SJohn Fastabend 
471259ccaaaaSRoopa Prabhu 	if (!skb->len)
471359ccaaaaSRoopa Prabhu 		goto errout;
471459ccaaaaSRoopa Prabhu 
47152469ffd7SJohn Fastabend 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
47162469ffd7SJohn Fastabend 	return 0;
47172469ffd7SJohn Fastabend errout:
47182469ffd7SJohn Fastabend 	WARN_ON(err == -EMSGSIZE);
47192469ffd7SJohn Fastabend 	kfree_skb(skb);
472059ccaaaaSRoopa Prabhu 	if (err)
47212469ffd7SJohn Fastabend 		rtnl_set_sk_err(net, RTNLGRP_LINK, err);
47222469ffd7SJohn Fastabend 	return err;
47232469ffd7SJohn Fastabend }
47242469ffd7SJohn Fastabend 
4725c21ef3e3SDavid Ahern static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
4726c21ef3e3SDavid Ahern 			       struct netlink_ext_ack *extack)
4727e5a55a89SJohn Fastabend {
4728e5a55a89SJohn Fastabend 	struct net *net = sock_net(skb->sk);
4729e5a55a89SJohn Fastabend 	struct ifinfomsg *ifm;
4730e5a55a89SJohn Fastabend 	struct net_device *dev;
47312469ffd7SJohn Fastabend 	struct nlattr *br_spec, *attr = NULL;
47322469ffd7SJohn Fastabend 	int rem, err = -EOPNOTSUPP;
47334de8b413SRosen, Rami 	u16 flags = 0;
4734c38e01b8SJohn Fastabend 	bool have_flags = false;
4735e5a55a89SJohn Fastabend 
4736e5a55a89SJohn Fastabend 	if (nlmsg_len(nlh) < sizeof(*ifm))
4737e5a55a89SJohn Fastabend 		return -EINVAL;
4738e5a55a89SJohn Fastabend 
4739e5a55a89SJohn Fastabend 	ifm = nlmsg_data(nlh);
4740e5a55a89SJohn Fastabend 	if (ifm->ifi_family != AF_BRIDGE)
4741e5a55a89SJohn Fastabend 		return -EPFNOSUPPORT;
4742e5a55a89SJohn Fastabend 
4743e5a55a89SJohn Fastabend 	dev = __dev_get_by_index(net, ifm->ifi_index);
4744e5a55a89SJohn Fastabend 	if (!dev) {
4745b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "unknown ifindex");
4746e5a55a89SJohn Fastabend 		return -ENODEV;
4747e5a55a89SJohn Fastabend 	}
4748e5a55a89SJohn Fastabend 
47492469ffd7SJohn Fastabend 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
47502469ffd7SJohn Fastabend 	if (br_spec) {
47512469ffd7SJohn Fastabend 		nla_for_each_nested(attr, br_spec, rem) {
47522469ffd7SJohn Fastabend 			if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
47536e8d1c55SThomas Graf 				if (nla_len(attr) < sizeof(flags))
47546e8d1c55SThomas Graf 					return -EINVAL;
47556e8d1c55SThomas Graf 
4756c38e01b8SJohn Fastabend 				have_flags = true;
47572469ffd7SJohn Fastabend 				flags = nla_get_u16(attr);
47582469ffd7SJohn Fastabend 				break;
47592469ffd7SJohn Fastabend 			}
47602469ffd7SJohn Fastabend 		}
47612469ffd7SJohn Fastabend 	}
47622469ffd7SJohn Fastabend 
47632469ffd7SJohn Fastabend 	if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
4764898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4765898e5061SJiri Pirko 
4766898e5061SJiri Pirko 		if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
47672469ffd7SJohn Fastabend 			err = -EOPNOTSUPP;
4768e5a55a89SJohn Fastabend 			goto out;
4769e5a55a89SJohn Fastabend 		}
4770e5a55a89SJohn Fastabend 
47712fd527b7SPetr Machata 		err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags,
47722fd527b7SPetr Machata 							     extack);
47732469ffd7SJohn Fastabend 		if (err)
47742469ffd7SJohn Fastabend 			goto out;
47752469ffd7SJohn Fastabend 
47762469ffd7SJohn Fastabend 		flags &= ~BRIDGE_FLAGS_MASTER;
47772469ffd7SJohn Fastabend 	}
47782469ffd7SJohn Fastabend 
47792469ffd7SJohn Fastabend 	if ((flags & BRIDGE_FLAGS_SELF)) {
47802469ffd7SJohn Fastabend 		if (!dev->netdev_ops->ndo_bridge_setlink)
47812469ffd7SJohn Fastabend 			err = -EOPNOTSUPP;
47822469ffd7SJohn Fastabend 		else
4783add511b3SRoopa Prabhu 			err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
47842fd527b7SPetr Machata 								  flags,
47852fd527b7SPetr Machata 								  extack);
478602dba438SRoopa Prabhu 		if (!err) {
47872469ffd7SJohn Fastabend 			flags &= ~BRIDGE_FLAGS_SELF;
478802dba438SRoopa Prabhu 
478902dba438SRoopa Prabhu 			/* Generate event to notify upper layer of bridge
479002dba438SRoopa Prabhu 			 * change
479102dba438SRoopa Prabhu 			 */
479202dba438SRoopa Prabhu 			err = rtnl_bridge_notify(dev);
479302dba438SRoopa Prabhu 		}
47942469ffd7SJohn Fastabend 	}
47952469ffd7SJohn Fastabend 
4796c38e01b8SJohn Fastabend 	if (have_flags)
47972469ffd7SJohn Fastabend 		memcpy(nla_data(attr), &flags, sizeof(flags));
4798e5a55a89SJohn Fastabend out:
4799e5a55a89SJohn Fastabend 	return err;
4800e5a55a89SJohn Fastabend }
4801e5a55a89SJohn Fastabend 
4802c21ef3e3SDavid Ahern static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
4803c21ef3e3SDavid Ahern 			       struct netlink_ext_ack *extack)
4804407af329SVlad Yasevich {
4805407af329SVlad Yasevich 	struct net *net = sock_net(skb->sk);
4806407af329SVlad Yasevich 	struct ifinfomsg *ifm;
4807407af329SVlad Yasevich 	struct net_device *dev;
4808407af329SVlad Yasevich 	struct nlattr *br_spec, *attr = NULL;
4809407af329SVlad Yasevich 	int rem, err = -EOPNOTSUPP;
48104de8b413SRosen, Rami 	u16 flags = 0;
4811407af329SVlad Yasevich 	bool have_flags = false;
4812407af329SVlad Yasevich 
4813407af329SVlad Yasevich 	if (nlmsg_len(nlh) < sizeof(*ifm))
4814407af329SVlad Yasevich 		return -EINVAL;
4815407af329SVlad Yasevich 
4816407af329SVlad Yasevich 	ifm = nlmsg_data(nlh);
4817407af329SVlad Yasevich 	if (ifm->ifi_family != AF_BRIDGE)
4818407af329SVlad Yasevich 		return -EPFNOSUPPORT;
4819407af329SVlad Yasevich 
4820407af329SVlad Yasevich 	dev = __dev_get_by_index(net, ifm->ifi_index);
4821407af329SVlad Yasevich 	if (!dev) {
4822b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "unknown ifindex");
4823407af329SVlad Yasevich 		return -ENODEV;
4824407af329SVlad Yasevich 	}
4825407af329SVlad Yasevich 
4826407af329SVlad Yasevich 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4827407af329SVlad Yasevich 	if (br_spec) {
4828407af329SVlad Yasevich 		nla_for_each_nested(attr, br_spec, rem) {
4829407af329SVlad Yasevich 			if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
48306e8d1c55SThomas Graf 				if (nla_len(attr) < sizeof(flags))
48316e8d1c55SThomas Graf 					return -EINVAL;
48326e8d1c55SThomas Graf 
4833407af329SVlad Yasevich 				have_flags = true;
4834407af329SVlad Yasevich 				flags = nla_get_u16(attr);
4835407af329SVlad Yasevich 				break;
4836407af329SVlad Yasevich 			}
4837407af329SVlad Yasevich 		}
4838407af329SVlad Yasevich 	}
4839407af329SVlad Yasevich 
4840407af329SVlad Yasevich 	if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
4841407af329SVlad Yasevich 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4842407af329SVlad Yasevich 
4843407af329SVlad Yasevich 		if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
4844407af329SVlad Yasevich 			err = -EOPNOTSUPP;
4845407af329SVlad Yasevich 			goto out;
4846407af329SVlad Yasevich 		}
4847407af329SVlad Yasevich 
4848add511b3SRoopa Prabhu 		err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
4849407af329SVlad Yasevich 		if (err)
4850407af329SVlad Yasevich 			goto out;
4851407af329SVlad Yasevich 
4852407af329SVlad Yasevich 		flags &= ~BRIDGE_FLAGS_MASTER;
4853407af329SVlad Yasevich 	}
4854407af329SVlad Yasevich 
4855407af329SVlad Yasevich 	if ((flags & BRIDGE_FLAGS_SELF)) {
4856407af329SVlad Yasevich 		if (!dev->netdev_ops->ndo_bridge_dellink)
4857407af329SVlad Yasevich 			err = -EOPNOTSUPP;
4858407af329SVlad Yasevich 		else
4859add511b3SRoopa Prabhu 			err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
4860add511b3SRoopa Prabhu 								  flags);
4861407af329SVlad Yasevich 
486202dba438SRoopa Prabhu 		if (!err) {
4863407af329SVlad Yasevich 			flags &= ~BRIDGE_FLAGS_SELF;
486402dba438SRoopa Prabhu 
486502dba438SRoopa Prabhu 			/* Generate event to notify upper layer of bridge
486602dba438SRoopa Prabhu 			 * change
486702dba438SRoopa Prabhu 			 */
486802dba438SRoopa Prabhu 			err = rtnl_bridge_notify(dev);
486902dba438SRoopa Prabhu 		}
4870407af329SVlad Yasevich 	}
4871407af329SVlad Yasevich 
4872407af329SVlad Yasevich 	if (have_flags)
4873407af329SVlad Yasevich 		memcpy(nla_data(attr), &flags, sizeof(flags));
4874407af329SVlad Yasevich out:
4875407af329SVlad Yasevich 	return err;
4876407af329SVlad Yasevich }
4877407af329SVlad Yasevich 
4878e8872a25SNikolay Aleksandrov static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
4879e8872a25SNikolay Aleksandrov {
4880e8872a25SNikolay Aleksandrov 	return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
4881e8872a25SNikolay Aleksandrov 	       (!idxattr || idxattr == attrid);
4882e8872a25SNikolay Aleksandrov }
4883e8872a25SNikolay Aleksandrov 
488469ae6ad2SNogah Frankel #define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
488569ae6ad2SNogah Frankel static int rtnl_get_offload_stats_attr_size(int attr_id)
488669ae6ad2SNogah Frankel {
488769ae6ad2SNogah Frankel 	switch (attr_id) {
488869ae6ad2SNogah Frankel 	case IFLA_OFFLOAD_XSTATS_CPU_HIT:
488969ae6ad2SNogah Frankel 		return sizeof(struct rtnl_link_stats64);
489069ae6ad2SNogah Frankel 	}
489169ae6ad2SNogah Frankel 
489269ae6ad2SNogah Frankel 	return 0;
489369ae6ad2SNogah Frankel }
489469ae6ad2SNogah Frankel 
489569ae6ad2SNogah Frankel static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev,
489669ae6ad2SNogah Frankel 				  int *prividx)
489769ae6ad2SNogah Frankel {
489869ae6ad2SNogah Frankel 	struct nlattr *attr = NULL;
489969ae6ad2SNogah Frankel 	int attr_id, size;
490069ae6ad2SNogah Frankel 	void *attr_data;
490169ae6ad2SNogah Frankel 	int err;
490269ae6ad2SNogah Frankel 
490369ae6ad2SNogah Frankel 	if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
490469ae6ad2SNogah Frankel 	      dev->netdev_ops->ndo_get_offload_stats))
490569ae6ad2SNogah Frankel 		return -ENODATA;
490669ae6ad2SNogah Frankel 
490769ae6ad2SNogah Frankel 	for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
490869ae6ad2SNogah Frankel 	     attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
490969ae6ad2SNogah Frankel 		if (attr_id < *prividx)
491069ae6ad2SNogah Frankel 			continue;
491169ae6ad2SNogah Frankel 
491269ae6ad2SNogah Frankel 		size = rtnl_get_offload_stats_attr_size(attr_id);
491369ae6ad2SNogah Frankel 		if (!size)
491469ae6ad2SNogah Frankel 			continue;
491569ae6ad2SNogah Frankel 
49163df5b3c6SOr Gerlitz 		if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
491769ae6ad2SNogah Frankel 			continue;
491869ae6ad2SNogah Frankel 
491969ae6ad2SNogah Frankel 		attr = nla_reserve_64bit(skb, attr_id, size,
492069ae6ad2SNogah Frankel 					 IFLA_OFFLOAD_XSTATS_UNSPEC);
492169ae6ad2SNogah Frankel 		if (!attr)
492269ae6ad2SNogah Frankel 			goto nla_put_failure;
492369ae6ad2SNogah Frankel 
492469ae6ad2SNogah Frankel 		attr_data = nla_data(attr);
492569ae6ad2SNogah Frankel 		memset(attr_data, 0, size);
492669ae6ad2SNogah Frankel 		err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev,
492769ae6ad2SNogah Frankel 							     attr_data);
492869ae6ad2SNogah Frankel 		if (err)
492969ae6ad2SNogah Frankel 			goto get_offload_stats_failure;
493069ae6ad2SNogah Frankel 	}
493169ae6ad2SNogah Frankel 
493269ae6ad2SNogah Frankel 	if (!attr)
493369ae6ad2SNogah Frankel 		return -ENODATA;
493469ae6ad2SNogah Frankel 
493569ae6ad2SNogah Frankel 	*prividx = 0;
493669ae6ad2SNogah Frankel 	return 0;
493769ae6ad2SNogah Frankel 
493869ae6ad2SNogah Frankel nla_put_failure:
493969ae6ad2SNogah Frankel 	err = -EMSGSIZE;
494069ae6ad2SNogah Frankel get_offload_stats_failure:
494169ae6ad2SNogah Frankel 	*prividx = attr_id;
494269ae6ad2SNogah Frankel 	return err;
494369ae6ad2SNogah Frankel }
494469ae6ad2SNogah Frankel 
494569ae6ad2SNogah Frankel static int rtnl_get_offload_stats_size(const struct net_device *dev)
494669ae6ad2SNogah Frankel {
494769ae6ad2SNogah Frankel 	int nla_size = 0;
494869ae6ad2SNogah Frankel 	int attr_id;
494969ae6ad2SNogah Frankel 	int size;
495069ae6ad2SNogah Frankel 
495169ae6ad2SNogah Frankel 	if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
495269ae6ad2SNogah Frankel 	      dev->netdev_ops->ndo_get_offload_stats))
495369ae6ad2SNogah Frankel 		return 0;
495469ae6ad2SNogah Frankel 
495569ae6ad2SNogah Frankel 	for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
495669ae6ad2SNogah Frankel 	     attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
49573df5b3c6SOr Gerlitz 		if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
495869ae6ad2SNogah Frankel 			continue;
495969ae6ad2SNogah Frankel 		size = rtnl_get_offload_stats_attr_size(attr_id);
496069ae6ad2SNogah Frankel 		nla_size += nla_total_size_64bit(size);
496169ae6ad2SNogah Frankel 	}
496269ae6ad2SNogah Frankel 
496369ae6ad2SNogah Frankel 	if (nla_size != 0)
496469ae6ad2SNogah Frankel 		nla_size += nla_total_size(0);
496569ae6ad2SNogah Frankel 
496669ae6ad2SNogah Frankel 	return nla_size;
496769ae6ad2SNogah Frankel }
496869ae6ad2SNogah Frankel 
496910c9ead9SRoopa Prabhu static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
497010c9ead9SRoopa Prabhu 			       int type, u32 pid, u32 seq, u32 change,
4971e8872a25SNikolay Aleksandrov 			       unsigned int flags, unsigned int filter_mask,
4972e8872a25SNikolay Aleksandrov 			       int *idxattr, int *prividx)
497310c9ead9SRoopa Prabhu {
497410c9ead9SRoopa Prabhu 	struct if_stats_msg *ifsm;
497510c9ead9SRoopa Prabhu 	struct nlmsghdr *nlh;
497610c9ead9SRoopa Prabhu 	struct nlattr *attr;
4977e8872a25SNikolay Aleksandrov 	int s_prividx = *prividx;
497869ae6ad2SNogah Frankel 	int err;
497910c9ead9SRoopa Prabhu 
498010c9ead9SRoopa Prabhu 	ASSERT_RTNL();
498110c9ead9SRoopa Prabhu 
498210c9ead9SRoopa Prabhu 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
498310c9ead9SRoopa Prabhu 	if (!nlh)
498410c9ead9SRoopa Prabhu 		return -EMSGSIZE;
498510c9ead9SRoopa Prabhu 
498610c9ead9SRoopa Prabhu 	ifsm = nlmsg_data(nlh);
4987ce024f42SNikolay Aleksandrov 	ifsm->family = PF_UNSPEC;
4988ce024f42SNikolay Aleksandrov 	ifsm->pad1 = 0;
4989ce024f42SNikolay Aleksandrov 	ifsm->pad2 = 0;
499010c9ead9SRoopa Prabhu 	ifsm->ifindex = dev->ifindex;
499110c9ead9SRoopa Prabhu 	ifsm->filter_mask = filter_mask;
499210c9ead9SRoopa Prabhu 
4993e8872a25SNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
499410c9ead9SRoopa Prabhu 		struct rtnl_link_stats64 *sp;
499510c9ead9SRoopa Prabhu 
499658414d32SNicolas Dichtel 		attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
499758414d32SNicolas Dichtel 					 sizeof(struct rtnl_link_stats64),
499858414d32SNicolas Dichtel 					 IFLA_STATS_UNSPEC);
499910c9ead9SRoopa Prabhu 		if (!attr)
500010c9ead9SRoopa Prabhu 			goto nla_put_failure;
500110c9ead9SRoopa Prabhu 
500210c9ead9SRoopa Prabhu 		sp = nla_data(attr);
500310c9ead9SRoopa Prabhu 		dev_get_stats(dev, sp);
500410c9ead9SRoopa Prabhu 	}
500510c9ead9SRoopa Prabhu 
500697a47facSNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
500797a47facSNikolay Aleksandrov 		const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
500897a47facSNikolay Aleksandrov 
500997a47facSNikolay Aleksandrov 		if (ops && ops->fill_linkxstats) {
501097a47facSNikolay Aleksandrov 			*idxattr = IFLA_STATS_LINK_XSTATS;
5011ae0be8deSMichal Kubecek 			attr = nla_nest_start_noflag(skb,
501297a47facSNikolay Aleksandrov 						     IFLA_STATS_LINK_XSTATS);
501397a47facSNikolay Aleksandrov 			if (!attr)
501497a47facSNikolay Aleksandrov 				goto nla_put_failure;
501597a47facSNikolay Aleksandrov 
501680e73cc5SNikolay Aleksandrov 			err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
501780e73cc5SNikolay Aleksandrov 			nla_nest_end(skb, attr);
501880e73cc5SNikolay Aleksandrov 			if (err)
501980e73cc5SNikolay Aleksandrov 				goto nla_put_failure;
502080e73cc5SNikolay Aleksandrov 			*idxattr = 0;
502180e73cc5SNikolay Aleksandrov 		}
502280e73cc5SNikolay Aleksandrov 	}
502380e73cc5SNikolay Aleksandrov 
502480e73cc5SNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
502580e73cc5SNikolay Aleksandrov 			     *idxattr)) {
502680e73cc5SNikolay Aleksandrov 		const struct rtnl_link_ops *ops = NULL;
502780e73cc5SNikolay Aleksandrov 		const struct net_device *master;
502880e73cc5SNikolay Aleksandrov 
502980e73cc5SNikolay Aleksandrov 		master = netdev_master_upper_dev_get(dev);
503080e73cc5SNikolay Aleksandrov 		if (master)
503180e73cc5SNikolay Aleksandrov 			ops = master->rtnl_link_ops;
503280e73cc5SNikolay Aleksandrov 		if (ops && ops->fill_linkxstats) {
503380e73cc5SNikolay Aleksandrov 			*idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
5034ae0be8deSMichal Kubecek 			attr = nla_nest_start_noflag(skb,
503580e73cc5SNikolay Aleksandrov 						     IFLA_STATS_LINK_XSTATS_SLAVE);
503680e73cc5SNikolay Aleksandrov 			if (!attr)
503780e73cc5SNikolay Aleksandrov 				goto nla_put_failure;
503880e73cc5SNikolay Aleksandrov 
503980e73cc5SNikolay Aleksandrov 			err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
504097a47facSNikolay Aleksandrov 			nla_nest_end(skb, attr);
504197a47facSNikolay Aleksandrov 			if (err)
504297a47facSNikolay Aleksandrov 				goto nla_put_failure;
504397a47facSNikolay Aleksandrov 			*idxattr = 0;
504497a47facSNikolay Aleksandrov 		}
504597a47facSNikolay Aleksandrov 	}
504697a47facSNikolay Aleksandrov 
504769ae6ad2SNogah Frankel 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
504869ae6ad2SNogah Frankel 			     *idxattr)) {
504969ae6ad2SNogah Frankel 		*idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
5050ae0be8deSMichal Kubecek 		attr = nla_nest_start_noflag(skb,
5051ae0be8deSMichal Kubecek 					     IFLA_STATS_LINK_OFFLOAD_XSTATS);
505269ae6ad2SNogah Frankel 		if (!attr)
505369ae6ad2SNogah Frankel 			goto nla_put_failure;
505469ae6ad2SNogah Frankel 
505569ae6ad2SNogah Frankel 		err = rtnl_get_offload_stats(skb, dev, prividx);
505669ae6ad2SNogah Frankel 		if (err == -ENODATA)
505769ae6ad2SNogah Frankel 			nla_nest_cancel(skb, attr);
505869ae6ad2SNogah Frankel 		else
505969ae6ad2SNogah Frankel 			nla_nest_end(skb, attr);
506069ae6ad2SNogah Frankel 
506169ae6ad2SNogah Frankel 		if (err && err != -ENODATA)
506269ae6ad2SNogah Frankel 			goto nla_put_failure;
506369ae6ad2SNogah Frankel 		*idxattr = 0;
506469ae6ad2SNogah Frankel 	}
506569ae6ad2SNogah Frankel 
5066aefb4d4aSRobert Shearman 	if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
5067aefb4d4aSRobert Shearman 		struct rtnl_af_ops *af_ops;
5068aefb4d4aSRobert Shearman 
5069aefb4d4aSRobert Shearman 		*idxattr = IFLA_STATS_AF_SPEC;
5070ae0be8deSMichal Kubecek 		attr = nla_nest_start_noflag(skb, IFLA_STATS_AF_SPEC);
5071aefb4d4aSRobert Shearman 		if (!attr)
5072aefb4d4aSRobert Shearman 			goto nla_put_failure;
5073aefb4d4aSRobert Shearman 
50745fa85a09SFlorian Westphal 		rcu_read_lock();
50755fa85a09SFlorian Westphal 		list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
5076aefb4d4aSRobert Shearman 			if (af_ops->fill_stats_af) {
5077aefb4d4aSRobert Shearman 				struct nlattr *af;
5078aefb4d4aSRobert Shearman 				int err;
5079aefb4d4aSRobert Shearman 
5080ae0be8deSMichal Kubecek 				af = nla_nest_start_noflag(skb,
5081ae0be8deSMichal Kubecek 							   af_ops->family);
50825fa85a09SFlorian Westphal 				if (!af) {
50835fa85a09SFlorian Westphal 					rcu_read_unlock();
5084aefb4d4aSRobert Shearman 					goto nla_put_failure;
50855fa85a09SFlorian Westphal 				}
5086aefb4d4aSRobert Shearman 				err = af_ops->fill_stats_af(skb, dev);
5087aefb4d4aSRobert Shearman 
50885fa85a09SFlorian Westphal 				if (err == -ENODATA) {
5089aefb4d4aSRobert Shearman 					nla_nest_cancel(skb, af);
50905fa85a09SFlorian Westphal 				} else if (err < 0) {
50915fa85a09SFlorian Westphal 					rcu_read_unlock();
5092aefb4d4aSRobert Shearman 					goto nla_put_failure;
50935fa85a09SFlorian Westphal 				}
5094aefb4d4aSRobert Shearman 
5095aefb4d4aSRobert Shearman 				nla_nest_end(skb, af);
5096aefb4d4aSRobert Shearman 			}
5097aefb4d4aSRobert Shearman 		}
50985fa85a09SFlorian Westphal 		rcu_read_unlock();
5099aefb4d4aSRobert Shearman 
5100aefb4d4aSRobert Shearman 		nla_nest_end(skb, attr);
5101aefb4d4aSRobert Shearman 
5102aefb4d4aSRobert Shearman 		*idxattr = 0;
5103aefb4d4aSRobert Shearman 	}
5104aefb4d4aSRobert Shearman 
510510c9ead9SRoopa Prabhu 	nlmsg_end(skb, nlh);
510610c9ead9SRoopa Prabhu 
510710c9ead9SRoopa Prabhu 	return 0;
510810c9ead9SRoopa Prabhu 
510910c9ead9SRoopa Prabhu nla_put_failure:
5110e8872a25SNikolay Aleksandrov 	/* not a multi message or no progress mean a real error */
5111e8872a25SNikolay Aleksandrov 	if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
511210c9ead9SRoopa Prabhu 		nlmsg_cancel(skb, nlh);
5113e8872a25SNikolay Aleksandrov 	else
5114e8872a25SNikolay Aleksandrov 		nlmsg_end(skb, nlh);
511510c9ead9SRoopa Prabhu 
511610c9ead9SRoopa Prabhu 	return -EMSGSIZE;
511710c9ead9SRoopa Prabhu }
511810c9ead9SRoopa Prabhu 
511910c9ead9SRoopa Prabhu static size_t if_nlmsg_stats_size(const struct net_device *dev,
512010c9ead9SRoopa Prabhu 				  u32 filter_mask)
512110c9ead9SRoopa Prabhu {
512210c9ead9SRoopa Prabhu 	size_t size = 0;
512310c9ead9SRoopa Prabhu 
5124e8872a25SNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
512510c9ead9SRoopa Prabhu 		size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
512610c9ead9SRoopa Prabhu 
512797a47facSNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
512897a47facSNikolay Aleksandrov 		const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
512980e73cc5SNikolay Aleksandrov 		int attr = IFLA_STATS_LINK_XSTATS;
513097a47facSNikolay Aleksandrov 
513197a47facSNikolay Aleksandrov 		if (ops && ops->get_linkxstats_size) {
513280e73cc5SNikolay Aleksandrov 			size += nla_total_size(ops->get_linkxstats_size(dev,
513380e73cc5SNikolay Aleksandrov 									attr));
513497a47facSNikolay Aleksandrov 			/* for IFLA_STATS_LINK_XSTATS */
513597a47facSNikolay Aleksandrov 			size += nla_total_size(0);
513697a47facSNikolay Aleksandrov 		}
513797a47facSNikolay Aleksandrov 	}
513897a47facSNikolay Aleksandrov 
513980e73cc5SNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
514080e73cc5SNikolay Aleksandrov 		struct net_device *_dev = (struct net_device *)dev;
514180e73cc5SNikolay Aleksandrov 		const struct rtnl_link_ops *ops = NULL;
514280e73cc5SNikolay Aleksandrov 		const struct net_device *master;
514380e73cc5SNikolay Aleksandrov 
514480e73cc5SNikolay Aleksandrov 		/* netdev_master_upper_dev_get can't take const */
514580e73cc5SNikolay Aleksandrov 		master = netdev_master_upper_dev_get(_dev);
514680e73cc5SNikolay Aleksandrov 		if (master)
514780e73cc5SNikolay Aleksandrov 			ops = master->rtnl_link_ops;
514880e73cc5SNikolay Aleksandrov 		if (ops && ops->get_linkxstats_size) {
514980e73cc5SNikolay Aleksandrov 			int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
515080e73cc5SNikolay Aleksandrov 
515180e73cc5SNikolay Aleksandrov 			size += nla_total_size(ops->get_linkxstats_size(dev,
515280e73cc5SNikolay Aleksandrov 									attr));
515380e73cc5SNikolay Aleksandrov 			/* for IFLA_STATS_LINK_XSTATS_SLAVE */
515480e73cc5SNikolay Aleksandrov 			size += nla_total_size(0);
515580e73cc5SNikolay Aleksandrov 		}
515680e73cc5SNikolay Aleksandrov 	}
515780e73cc5SNikolay Aleksandrov 
515869ae6ad2SNogah Frankel 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0))
515969ae6ad2SNogah Frankel 		size += rtnl_get_offload_stats_size(dev);
516069ae6ad2SNogah Frankel 
5161aefb4d4aSRobert Shearman 	if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
5162aefb4d4aSRobert Shearman 		struct rtnl_af_ops *af_ops;
5163aefb4d4aSRobert Shearman 
5164aefb4d4aSRobert Shearman 		/* for IFLA_STATS_AF_SPEC */
5165aefb4d4aSRobert Shearman 		size += nla_total_size(0);
5166aefb4d4aSRobert Shearman 
51675fa85a09SFlorian Westphal 		rcu_read_lock();
51685fa85a09SFlorian Westphal 		list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
5169aefb4d4aSRobert Shearman 			if (af_ops->get_stats_af_size) {
5170aefb4d4aSRobert Shearman 				size += nla_total_size(
5171aefb4d4aSRobert Shearman 					af_ops->get_stats_af_size(dev));
5172aefb4d4aSRobert Shearman 
5173aefb4d4aSRobert Shearman 				/* for AF_* */
5174aefb4d4aSRobert Shearman 				size += nla_total_size(0);
5175aefb4d4aSRobert Shearman 			}
5176aefb4d4aSRobert Shearman 		}
51775fa85a09SFlorian Westphal 		rcu_read_unlock();
5178aefb4d4aSRobert Shearman 	}
5179aefb4d4aSRobert Shearman 
518010c9ead9SRoopa Prabhu 	return size;
518110c9ead9SRoopa Prabhu }
518210c9ead9SRoopa Prabhu 
518351bc860dSJakub Kicinski static int rtnl_valid_stats_req(const struct nlmsghdr *nlh, bool strict_check,
518451bc860dSJakub Kicinski 				bool is_dump, struct netlink_ext_ack *extack)
518551bc860dSJakub Kicinski {
518651bc860dSJakub Kicinski 	struct if_stats_msg *ifsm;
518751bc860dSJakub Kicinski 
518869f23a09SEric Dumazet 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifsm))) {
518951bc860dSJakub Kicinski 		NL_SET_ERR_MSG(extack, "Invalid header for stats dump");
519051bc860dSJakub Kicinski 		return -EINVAL;
519151bc860dSJakub Kicinski 	}
519251bc860dSJakub Kicinski 
519351bc860dSJakub Kicinski 	if (!strict_check)
519451bc860dSJakub Kicinski 		return 0;
519551bc860dSJakub Kicinski 
519651bc860dSJakub Kicinski 	ifsm = nlmsg_data(nlh);
519751bc860dSJakub Kicinski 
519851bc860dSJakub Kicinski 	/* only requests using strict checks can pass data to influence
519951bc860dSJakub Kicinski 	 * the dump. The legacy exception is filter_mask.
520051bc860dSJakub Kicinski 	 */
520151bc860dSJakub Kicinski 	if (ifsm->pad1 || ifsm->pad2 || (is_dump && ifsm->ifindex)) {
520251bc860dSJakub Kicinski 		NL_SET_ERR_MSG(extack, "Invalid values in header for stats dump request");
520351bc860dSJakub Kicinski 		return -EINVAL;
520451bc860dSJakub Kicinski 	}
520551bc860dSJakub Kicinski 	if (nlmsg_attrlen(nlh, sizeof(*ifsm))) {
520651bc860dSJakub Kicinski 		NL_SET_ERR_MSG(extack, "Invalid attributes after stats header");
520751bc860dSJakub Kicinski 		return -EINVAL;
520851bc860dSJakub Kicinski 	}
52096300acb2SJakub Kicinski 	if (ifsm->filter_mask >= IFLA_STATS_FILTER_BIT(IFLA_STATS_MAX + 1)) {
52106300acb2SJakub Kicinski 		NL_SET_ERR_MSG(extack, "Invalid stats requested through filter mask");
52116300acb2SJakub Kicinski 		return -EINVAL;
52126300acb2SJakub Kicinski 	}
521351bc860dSJakub Kicinski 
521451bc860dSJakub Kicinski 	return 0;
521551bc860dSJakub Kicinski }
521651bc860dSJakub Kicinski 
5217c21ef3e3SDavid Ahern static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
5218c21ef3e3SDavid Ahern 			  struct netlink_ext_ack *extack)
521910c9ead9SRoopa Prabhu {
522010c9ead9SRoopa Prabhu 	struct net *net = sock_net(skb->sk);
522110c9ead9SRoopa Prabhu 	struct net_device *dev = NULL;
5222e8872a25SNikolay Aleksandrov 	int idxattr = 0, prividx = 0;
5223e8872a25SNikolay Aleksandrov 	struct if_stats_msg *ifsm;
522410c9ead9SRoopa Prabhu 	struct sk_buff *nskb;
522510c9ead9SRoopa Prabhu 	u32 filter_mask;
522610c9ead9SRoopa Prabhu 	int err;
522710c9ead9SRoopa Prabhu 
522851bc860dSJakub Kicinski 	err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb),
522951bc860dSJakub Kicinski 				   false, extack);
523051bc860dSJakub Kicinski 	if (err)
523151bc860dSJakub Kicinski 		return err;
52324775cc1fSMathias Krause 
523310c9ead9SRoopa Prabhu 	ifsm = nlmsg_data(nlh);
523410c9ead9SRoopa Prabhu 	if (ifsm->ifindex > 0)
523510c9ead9SRoopa Prabhu 		dev = __dev_get_by_index(net, ifsm->ifindex);
523610c9ead9SRoopa Prabhu 	else
523710c9ead9SRoopa Prabhu 		return -EINVAL;
523810c9ead9SRoopa Prabhu 
523910c9ead9SRoopa Prabhu 	if (!dev)
524010c9ead9SRoopa Prabhu 		return -ENODEV;
524110c9ead9SRoopa Prabhu 
524210c9ead9SRoopa Prabhu 	filter_mask = ifsm->filter_mask;
524310c9ead9SRoopa Prabhu 	if (!filter_mask)
524410c9ead9SRoopa Prabhu 		return -EINVAL;
524510c9ead9SRoopa Prabhu 
524610c9ead9SRoopa Prabhu 	nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
524710c9ead9SRoopa Prabhu 	if (!nskb)
524810c9ead9SRoopa Prabhu 		return -ENOBUFS;
524910c9ead9SRoopa Prabhu 
525010c9ead9SRoopa Prabhu 	err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
525110c9ead9SRoopa Prabhu 				  NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
5252e8872a25SNikolay Aleksandrov 				  0, filter_mask, &idxattr, &prividx);
525310c9ead9SRoopa Prabhu 	if (err < 0) {
525410c9ead9SRoopa Prabhu 		/* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
525510c9ead9SRoopa Prabhu 		WARN_ON(err == -EMSGSIZE);
525610c9ead9SRoopa Prabhu 		kfree_skb(nskb);
525710c9ead9SRoopa Prabhu 	} else {
525810c9ead9SRoopa Prabhu 		err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
525910c9ead9SRoopa Prabhu 	}
526010c9ead9SRoopa Prabhu 
526110c9ead9SRoopa Prabhu 	return err;
526210c9ead9SRoopa Prabhu }
526310c9ead9SRoopa Prabhu 
526410c9ead9SRoopa Prabhu static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
526510c9ead9SRoopa Prabhu {
5266841891ecSDavid Ahern 	struct netlink_ext_ack *extack = cb->extack;
5267e8872a25SNikolay Aleksandrov 	int h, s_h, err, s_idx, s_idxattr, s_prividx;
526810c9ead9SRoopa Prabhu 	struct net *net = sock_net(skb->sk);
526910c9ead9SRoopa Prabhu 	unsigned int flags = NLM_F_MULTI;
5270e8872a25SNikolay Aleksandrov 	struct if_stats_msg *ifsm;
5271e8872a25SNikolay Aleksandrov 	struct hlist_head *head;
5272e8872a25SNikolay Aleksandrov 	struct net_device *dev;
527310c9ead9SRoopa Prabhu 	u32 filter_mask = 0;
5274e8872a25SNikolay Aleksandrov 	int idx = 0;
527510c9ead9SRoopa Prabhu 
527610c9ead9SRoopa Prabhu 	s_h = cb->args[0];
527710c9ead9SRoopa Prabhu 	s_idx = cb->args[1];
5278e8872a25SNikolay Aleksandrov 	s_idxattr = cb->args[2];
5279e8872a25SNikolay Aleksandrov 	s_prividx = cb->args[3];
528010c9ead9SRoopa Prabhu 
528110c9ead9SRoopa Prabhu 	cb->seq = net->dev_base_seq;
528210c9ead9SRoopa Prabhu 
528351bc860dSJakub Kicinski 	err = rtnl_valid_stats_req(cb->nlh, cb->strict_check, true, extack);
528451bc860dSJakub Kicinski 	if (err)
528551bc860dSJakub Kicinski 		return err;
52864775cc1fSMathias Krause 
528710c9ead9SRoopa Prabhu 	ifsm = nlmsg_data(cb->nlh);
5288841891ecSDavid Ahern 	filter_mask = ifsm->filter_mask;
5289841891ecSDavid Ahern 	if (!filter_mask) {
5290841891ecSDavid Ahern 		NL_SET_ERR_MSG(extack, "Filter mask must be set for stats dump");
5291841891ecSDavid Ahern 		return -EINVAL;
5292841891ecSDavid Ahern 	}
529310c9ead9SRoopa Prabhu 
529410c9ead9SRoopa Prabhu 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
529510c9ead9SRoopa Prabhu 		idx = 0;
529610c9ead9SRoopa Prabhu 		head = &net->dev_index_head[h];
529710c9ead9SRoopa Prabhu 		hlist_for_each_entry(dev, head, index_hlist) {
529810c9ead9SRoopa Prabhu 			if (idx < s_idx)
529910c9ead9SRoopa Prabhu 				goto cont;
530010c9ead9SRoopa Prabhu 			err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
530110c9ead9SRoopa Prabhu 						  NETLINK_CB(cb->skb).portid,
530210c9ead9SRoopa Prabhu 						  cb->nlh->nlmsg_seq, 0,
5303e8872a25SNikolay Aleksandrov 						  flags, filter_mask,
5304e8872a25SNikolay Aleksandrov 						  &s_idxattr, &s_prividx);
530510c9ead9SRoopa Prabhu 			/* If we ran out of room on the first message,
530610c9ead9SRoopa Prabhu 			 * we're in trouble
530710c9ead9SRoopa Prabhu 			 */
530810c9ead9SRoopa Prabhu 			WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
530910c9ead9SRoopa Prabhu 
531010c9ead9SRoopa Prabhu 			if (err < 0)
531110c9ead9SRoopa Prabhu 				goto out;
5312e8872a25SNikolay Aleksandrov 			s_prividx = 0;
5313e8872a25SNikolay Aleksandrov 			s_idxattr = 0;
531410c9ead9SRoopa Prabhu 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
531510c9ead9SRoopa Prabhu cont:
531610c9ead9SRoopa Prabhu 			idx++;
531710c9ead9SRoopa Prabhu 		}
531810c9ead9SRoopa Prabhu 	}
531910c9ead9SRoopa Prabhu out:
5320e8872a25SNikolay Aleksandrov 	cb->args[3] = s_prividx;
5321e8872a25SNikolay Aleksandrov 	cb->args[2] = s_idxattr;
532210c9ead9SRoopa Prabhu 	cb->args[1] = idx;
532310c9ead9SRoopa Prabhu 	cb->args[0] = h;
532410c9ead9SRoopa Prabhu 
532510c9ead9SRoopa Prabhu 	return skb->len;
532610c9ead9SRoopa Prabhu }
532710c9ead9SRoopa Prabhu 
53281da177e4SLinus Torvalds /* Process one rtnetlink message. */
53291da177e4SLinus Torvalds 
53302d4bc933SJohannes Berg static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
53312d4bc933SJohannes Berg 			     struct netlink_ext_ack *extack)
53321da177e4SLinus Torvalds {
53333b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
5334addf9b90SFlorian Westphal 	struct rtnl_link *link;
5335e4202511SFlorian Westphal 	struct module *owner;
53366853dd48SFlorian Westphal 	int err = -EOPNOTSUPP;
5337e2849863SThomas Graf 	rtnl_doit_func doit;
533862256f98SFlorian Westphal 	unsigned int flags;
5339617cfc75SAlexander Kuleshov 	int kind;
53401da177e4SLinus Torvalds 	int family;
53411da177e4SLinus Torvalds 	int type;
53421da177e4SLinus Torvalds 
53431da177e4SLinus Torvalds 	type = nlh->nlmsg_type;
53441da177e4SLinus Torvalds 	if (type > RTM_MAX)
5345038890feSThomas Graf 		return -EOPNOTSUPP;
53461da177e4SLinus Torvalds 
53471da177e4SLinus Torvalds 	type -= RTM_BASE;
53481da177e4SLinus Torvalds 
53491da177e4SLinus Torvalds 	/* All the messages must have at least 1 byte length */
5350573ce260SHong zhi guo 	if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
53511da177e4SLinus Torvalds 		return 0;
53521da177e4SLinus Torvalds 
5353573ce260SHong zhi guo 	family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
53541da177e4SLinus Torvalds 	kind = type&3;
53551da177e4SLinus Torvalds 
535690f62cf3SEric W. Biederman 	if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
53571d00a4ebSThomas Graf 		return -EPERM;
53581da177e4SLinus Torvalds 
53596853dd48SFlorian Westphal 	rcu_read_lock();
5360b8f3ab42SDavid S. Miller 	if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
536197c53cacSDenis V. Lunev 		struct sock *rtnl;
5362e2849863SThomas Graf 		rtnl_dumpit_func dumpit;
5363c7ac8679SGreg Rose 		u16 min_dump_alloc = 0;
53641da177e4SLinus Torvalds 
5365addf9b90SFlorian Westphal 		link = rtnl_get_link(family, type);
5366addf9b90SFlorian Westphal 		if (!link || !link->dumpit) {
53676853dd48SFlorian Westphal 			family = PF_UNSPEC;
5368addf9b90SFlorian Westphal 			link = rtnl_get_link(family, type);
5369addf9b90SFlorian Westphal 			if (!link || !link->dumpit)
53706853dd48SFlorian Westphal 				goto err_unlock;
53716853dd48SFlorian Westphal 		}
5372e4202511SFlorian Westphal 		owner = link->owner;
5373addf9b90SFlorian Westphal 		dumpit = link->dumpit;
53746853dd48SFlorian Westphal 
53755c2bb9b6SFlorian Westphal 		if (type == RTM_GETLINK - RTM_BASE)
5376e1fa6d21SFlorian Westphal 			min_dump_alloc = rtnl_calcit(skb, nlh);
53771da177e4SLinus Torvalds 
5378e4202511SFlorian Westphal 		err = 0;
5379e4202511SFlorian Westphal 		/* need to do this before rcu_read_unlock() */
5380e4202511SFlorian Westphal 		if (!try_module_get(owner))
5381e4202511SFlorian Westphal 			err = -EPROTONOSUPPORT;
5382e4202511SFlorian Westphal 
53836853dd48SFlorian Westphal 		rcu_read_unlock();
53846853dd48SFlorian Westphal 
538597c53cacSDenis V. Lunev 		rtnl = net->rtnl;
5386e4202511SFlorian Westphal 		if (err == 0) {
538780d326faSPablo Neira Ayuso 			struct netlink_dump_control c = {
538880d326faSPablo Neira Ayuso 				.dump		= dumpit,
538980d326faSPablo Neira Ayuso 				.min_dump_alloc	= min_dump_alloc,
5390e4202511SFlorian Westphal 				.module		= owner,
539180d326faSPablo Neira Ayuso 			};
539280d326faSPablo Neira Ayuso 			err = netlink_dump_start(rtnl, skb, nlh, &c);
5393e4202511SFlorian Westphal 			/* netlink_dump_start() will keep a reference on
5394e4202511SFlorian Westphal 			 * module if dump is still in progress.
5395e4202511SFlorian Westphal 			 */
5396e4202511SFlorian Westphal 			module_put(owner);
539780d326faSPablo Neira Ayuso 		}
53982907c35fSEric Dumazet 		return err;
53991da177e4SLinus Torvalds 	}
54001da177e4SLinus Torvalds 
5401addf9b90SFlorian Westphal 	link = rtnl_get_link(family, type);
5402addf9b90SFlorian Westphal 	if (!link || !link->doit) {
54038caa38b5SFlorian Westphal 		family = PF_UNSPEC;
5404addf9b90SFlorian Westphal 		link = rtnl_get_link(PF_UNSPEC, type);
5405addf9b90SFlorian Westphal 		if (!link || !link->doit)
5406addf9b90SFlorian Westphal 			goto out_unlock;
54078caa38b5SFlorian Westphal 	}
54088caa38b5SFlorian Westphal 
5409e4202511SFlorian Westphal 	owner = link->owner;
5410e4202511SFlorian Westphal 	if (!try_module_get(owner)) {
5411e4202511SFlorian Westphal 		err = -EPROTONOSUPPORT;
5412e4202511SFlorian Westphal 		goto out_unlock;
5413e4202511SFlorian Westphal 	}
5414e4202511SFlorian Westphal 
5415addf9b90SFlorian Westphal 	flags = link->flags;
541662256f98SFlorian Westphal 	if (flags & RTNL_FLAG_DOIT_UNLOCKED) {
5417addf9b90SFlorian Westphal 		doit = link->doit;
541862256f98SFlorian Westphal 		rcu_read_unlock();
541962256f98SFlorian Westphal 		if (doit)
542062256f98SFlorian Westphal 			err = doit(skb, nlh, extack);
5421e4202511SFlorian Westphal 		module_put(owner);
542262256f98SFlorian Westphal 		return err;
542362256f98SFlorian Westphal 	}
54246853dd48SFlorian Westphal 	rcu_read_unlock();
54256853dd48SFlorian Westphal 
54260cc09020SFlorian Westphal 	rtnl_lock();
5427addf9b90SFlorian Westphal 	link = rtnl_get_link(family, type);
5428addf9b90SFlorian Westphal 	if (link && link->doit)
5429addf9b90SFlorian Westphal 		err = link->doit(skb, nlh, extack);
54300cc09020SFlorian Westphal 	rtnl_unlock();
5431addf9b90SFlorian Westphal 
5432e4202511SFlorian Westphal 	module_put(owner);
5433e4202511SFlorian Westphal 
5434addf9b90SFlorian Westphal 	return err;
5435addf9b90SFlorian Westphal 
5436addf9b90SFlorian Westphal out_unlock:
5437addf9b90SFlorian Westphal 	rcu_read_unlock();
54380cc09020SFlorian Westphal 	return err;
54390cc09020SFlorian Westphal 
54400cc09020SFlorian Westphal err_unlock:
54416853dd48SFlorian Westphal 	rcu_read_unlock();
54420cc09020SFlorian Westphal 	return -EOPNOTSUPP;
54431da177e4SLinus Torvalds }
54441da177e4SLinus Torvalds 
5445cd40b7d3SDenis V. Lunev static void rtnetlink_rcv(struct sk_buff *skb)
54461da177e4SLinus Torvalds {
5447cd40b7d3SDenis V. Lunev 	netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
54481da177e4SLinus Torvalds }
54491da177e4SLinus Torvalds 
54505f729eaaSJulien Gomes static int rtnetlink_bind(struct net *net, int group)
54515f729eaaSJulien Gomes {
54525f729eaaSJulien Gomes 	switch (group) {
54535f729eaaSJulien Gomes 	case RTNLGRP_IPV4_MROUTE_R:
54545f729eaaSJulien Gomes 	case RTNLGRP_IPV6_MROUTE_R:
54555f729eaaSJulien Gomes 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
54565f729eaaSJulien Gomes 			return -EPERM;
54575f729eaaSJulien Gomes 		break;
54585f729eaaSJulien Gomes 	}
54595f729eaaSJulien Gomes 	return 0;
54605f729eaaSJulien Gomes }
54615f729eaaSJulien Gomes 
54621da177e4SLinus Torvalds static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
54631da177e4SLinus Torvalds {
5464351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
5465e9dc8653SEric W. Biederman 
54661da177e4SLinus Torvalds 	switch (event) {
54675138e86fSVlad Yasevich 	case NETDEV_REBOOT:
54688a212589SXin Long 	case NETDEV_CHANGEMTU:
54693753654eSDavid Ahern 	case NETDEV_CHANGEADDR:
54705138e86fSVlad Yasevich 	case NETDEV_CHANGENAME:
54715138e86fSVlad Yasevich 	case NETDEV_FEAT_CHANGE:
54725138e86fSVlad Yasevich 	case NETDEV_BONDING_FAILOVER:
5473e6e66594SXin Long 	case NETDEV_POST_TYPE_CHANGE:
54745138e86fSVlad Yasevich 	case NETDEV_NOTIFY_PEERS:
5475dc709f37SXin Long 	case NETDEV_CHANGEUPPER:
54765138e86fSVlad Yasevich 	case NETDEV_RESEND_IGMP:
54775138e86fSVlad Yasevich 	case NETDEV_CHANGEINFODATA:
5478eeda3fb9SXin Long 	case NETDEV_CHANGELOWERSTATE:
5479ebdcf045SXin Long 	case NETDEV_CHANGE_TX_QUEUE_LEN:
54803d3ea5afSVlad Yasevich 		rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
548138e01b30SNicolas Dichtel 				   GFP_KERNEL, NULL, 0);
54821da177e4SLinus Torvalds 		break;
54831da177e4SLinus Torvalds 	default:
54841da177e4SLinus Torvalds 		break;
54851da177e4SLinus Torvalds 	}
54861da177e4SLinus Torvalds 	return NOTIFY_DONE;
54871da177e4SLinus Torvalds }
54881da177e4SLinus Torvalds 
54891da177e4SLinus Torvalds static struct notifier_block rtnetlink_dev_notifier = {
54901da177e4SLinus Torvalds 	.notifier_call	= rtnetlink_event,
54911da177e4SLinus Torvalds };
54921da177e4SLinus Torvalds 
549397c53cacSDenis V. Lunev 
54942c8c1e72SAlexey Dobriyan static int __net_init rtnetlink_net_init(struct net *net)
549597c53cacSDenis V. Lunev {
549697c53cacSDenis V. Lunev 	struct sock *sk;
5497a31f2d17SPablo Neira Ayuso 	struct netlink_kernel_cfg cfg = {
5498a31f2d17SPablo Neira Ayuso 		.groups		= RTNLGRP_MAX,
5499a31f2d17SPablo Neira Ayuso 		.input		= rtnetlink_rcv,
5500a31f2d17SPablo Neira Ayuso 		.cb_mutex	= &rtnl_mutex,
55019785e10aSPablo Neira Ayuso 		.flags		= NL_CFG_F_NONROOT_RECV,
55025f729eaaSJulien Gomes 		.bind		= rtnetlink_bind,
5503a31f2d17SPablo Neira Ayuso 	};
5504a31f2d17SPablo Neira Ayuso 
55059f00d977SPablo Neira Ayuso 	sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
550697c53cacSDenis V. Lunev 	if (!sk)
550797c53cacSDenis V. Lunev 		return -ENOMEM;
550897c53cacSDenis V. Lunev 	net->rtnl = sk;
550997c53cacSDenis V. Lunev 	return 0;
551097c53cacSDenis V. Lunev }
551197c53cacSDenis V. Lunev 
55122c8c1e72SAlexey Dobriyan static void __net_exit rtnetlink_net_exit(struct net *net)
551397c53cacSDenis V. Lunev {
5514b7c6ba6eSDenis V. Lunev 	netlink_kernel_release(net->rtnl);
551597c53cacSDenis V. Lunev 	net->rtnl = NULL;
551697c53cacSDenis V. Lunev }
551797c53cacSDenis V. Lunev 
551897c53cacSDenis V. Lunev static struct pernet_operations rtnetlink_net_ops = {
551997c53cacSDenis V. Lunev 	.init = rtnetlink_net_init,
552097c53cacSDenis V. Lunev 	.exit = rtnetlink_net_exit,
552197c53cacSDenis V. Lunev };
552297c53cacSDenis V. Lunev 
55231da177e4SLinus Torvalds void __init rtnetlink_init(void)
55241da177e4SLinus Torvalds {
552597c53cacSDenis V. Lunev 	if (register_pernet_subsys(&rtnetlink_net_ops))
55261da177e4SLinus Torvalds 		panic("rtnetlink_init: cannot initialize rtnetlink\n");
552797c53cacSDenis V. Lunev 
55281da177e4SLinus Torvalds 	register_netdevice_notifier(&rtnetlink_dev_notifier);
5529340d17fcSThomas Graf 
5530c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
5531b97bac64SFlorian Westphal 		      rtnl_dump_ifinfo, 0);
5532b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0);
5533b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0);
5534b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0);
5535687ad8ccSThomas Graf 
5536b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0);
5537b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0);
5538b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0);
553977162022SJohn Fastabend 
554036fbf1e5SJiri Pirko 	rtnl_register(PF_UNSPEC, RTM_NEWLINKPROP, rtnl_newlinkprop, NULL, 0);
554136fbf1e5SJiri Pirko 	rtnl_register(PF_UNSPEC, RTM_DELLINKPROP, rtnl_dellinkprop, NULL, 0);
554236fbf1e5SJiri Pirko 
5543b97bac64SFlorian Westphal 	rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
5544b97bac64SFlorian Westphal 	rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0);
55455b2f94b2SRoopa Prabhu 	rtnl_register(PF_BRIDGE, RTM_GETNEIGH, rtnl_fdb_get, rtnl_fdb_dump, 0);
5546e5a55a89SJohn Fastabend 
5547b97bac64SFlorian Westphal 	rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0);
5548b97bac64SFlorian Westphal 	rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0);
5549b97bac64SFlorian Westphal 	rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0);
555010c9ead9SRoopa Prabhu 
555110c9ead9SRoopa Prabhu 	rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,
5552b97bac64SFlorian Westphal 		      0);
55531da177e4SLinus Torvalds }
5554