xref: /openbmc/linux/net/core/rtnetlink.c (revision 76c9ac0ee878f6693d398d3a95ccaf85e1f597a6)
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)
104479e1ad14SJiri Benc 	       + 0;
1045339bf98fSThomas Graf }
1046339bf98fSThomas Graf 
104757b61080SScott Feldman static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
104857b61080SScott Feldman {
104957b61080SScott Feldman 	struct nlattr *vf_ports;
105057b61080SScott Feldman 	struct nlattr *vf_port;
105157b61080SScott Feldman 	int vf;
105257b61080SScott Feldman 	int err;
105357b61080SScott Feldman 
1054ae0be8deSMichal Kubecek 	vf_ports = nla_nest_start_noflag(skb, IFLA_VF_PORTS);
105557b61080SScott Feldman 	if (!vf_ports)
105657b61080SScott Feldman 		return -EMSGSIZE;
105757b61080SScott Feldman 
105857b61080SScott Feldman 	for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
1059ae0be8deSMichal Kubecek 		vf_port = nla_nest_start_noflag(skb, IFLA_VF_PORT);
10608ca94183SScott Feldman 		if (!vf_port)
10618ca94183SScott Feldman 			goto nla_put_failure;
1062a6574349SDavid S. Miller 		if (nla_put_u32(skb, IFLA_PORT_VF, vf))
1063a6574349SDavid S. Miller 			goto nla_put_failure;
106457b61080SScott Feldman 		err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
10658ca94183SScott Feldman 		if (err == -EMSGSIZE)
10668ca94183SScott Feldman 			goto nla_put_failure;
106757b61080SScott Feldman 		if (err) {
106857b61080SScott Feldman 			nla_nest_cancel(skb, vf_port);
106957b61080SScott Feldman 			continue;
107057b61080SScott Feldman 		}
107157b61080SScott Feldman 		nla_nest_end(skb, vf_port);
107257b61080SScott Feldman 	}
107357b61080SScott Feldman 
107457b61080SScott Feldman 	nla_nest_end(skb, vf_ports);
107557b61080SScott Feldman 
107657b61080SScott Feldman 	return 0;
10778ca94183SScott Feldman 
10788ca94183SScott Feldman nla_put_failure:
10798ca94183SScott Feldman 	nla_nest_cancel(skb, vf_ports);
10808ca94183SScott Feldman 	return -EMSGSIZE;
108157b61080SScott Feldman }
108257b61080SScott Feldman 
108357b61080SScott Feldman static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
108457b61080SScott Feldman {
108557b61080SScott Feldman 	struct nlattr *port_self;
108657b61080SScott Feldman 	int err;
108757b61080SScott Feldman 
1088ae0be8deSMichal Kubecek 	port_self = nla_nest_start_noflag(skb, IFLA_PORT_SELF);
108957b61080SScott Feldman 	if (!port_self)
109057b61080SScott Feldman 		return -EMSGSIZE;
109157b61080SScott Feldman 
109257b61080SScott Feldman 	err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
109357b61080SScott Feldman 	if (err) {
109457b61080SScott Feldman 		nla_nest_cancel(skb, port_self);
10958ca94183SScott Feldman 		return (err == -EMSGSIZE) ? err : 0;
109657b61080SScott Feldman 	}
109757b61080SScott Feldman 
109857b61080SScott Feldman 	nla_nest_end(skb, port_self);
109957b61080SScott Feldman 
110057b61080SScott Feldman 	return 0;
110157b61080SScott Feldman }
110257b61080SScott Feldman 
1103c53864fdSDavid Gibson static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
1104c53864fdSDavid Gibson 			  u32 ext_filter_mask)
110557b61080SScott Feldman {
110657b61080SScott Feldman 	int err;
110757b61080SScott Feldman 
1108c53864fdSDavid Gibson 	if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
1109c53864fdSDavid Gibson 	    !(ext_filter_mask & RTEXT_FILTER_VF))
111057b61080SScott Feldman 		return 0;
111157b61080SScott Feldman 
111257b61080SScott Feldman 	err = rtnl_port_self_fill(skb, dev);
111357b61080SScott Feldman 	if (err)
111457b61080SScott Feldman 		return err;
111557b61080SScott Feldman 
111657b61080SScott Feldman 	if (dev_num_vf(dev->dev.parent)) {
111757b61080SScott Feldman 		err = rtnl_vf_ports_fill(skb, dev);
111857b61080SScott Feldman 		if (err)
111957b61080SScott Feldman 			return err;
112057b61080SScott Feldman 	}
112157b61080SScott Feldman 
112257b61080SScott Feldman 	return 0;
112357b61080SScott Feldman }
112457b61080SScott Feldman 
112566cae9edSJiri Pirko static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
112666cae9edSJiri Pirko {
112766cae9edSJiri Pirko 	int err;
112802637fceSJiri Pirko 	struct netdev_phys_item_id ppid;
112966cae9edSJiri Pirko 
113066cae9edSJiri Pirko 	err = dev_get_phys_port_id(dev, &ppid);
113166cae9edSJiri Pirko 	if (err) {
113266cae9edSJiri Pirko 		if (err == -EOPNOTSUPP)
113366cae9edSJiri Pirko 			return 0;
113466cae9edSJiri Pirko 		return err;
113566cae9edSJiri Pirko 	}
113666cae9edSJiri Pirko 
113766cae9edSJiri Pirko 	if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
113866cae9edSJiri Pirko 		return -EMSGSIZE;
113966cae9edSJiri Pirko 
114066cae9edSJiri Pirko 	return 0;
114166cae9edSJiri Pirko }
114266cae9edSJiri Pirko 
1143db24a904SDavid Ahern static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1144db24a904SDavid Ahern {
1145db24a904SDavid Ahern 	char name[IFNAMSIZ];
1146db24a904SDavid Ahern 	int err;
1147db24a904SDavid Ahern 
1148db24a904SDavid Ahern 	err = dev_get_phys_port_name(dev, name, sizeof(name));
1149db24a904SDavid Ahern 	if (err) {
1150db24a904SDavid Ahern 		if (err == -EOPNOTSUPP)
1151db24a904SDavid Ahern 			return 0;
1152db24a904SDavid Ahern 		return err;
1153db24a904SDavid Ahern 	}
1154db24a904SDavid Ahern 
115577ef033bSMichal Schmidt 	if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
1156db24a904SDavid Ahern 		return -EMSGSIZE;
1157db24a904SDavid Ahern 
1158db24a904SDavid Ahern 	return 0;
1159db24a904SDavid Ahern }
1160db24a904SDavid Ahern 
116182f28412SJiri Pirko static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
116282f28412SJiri Pirko {
1163bccb3025SFlorian Fainelli 	struct netdev_phys_item_id ppid = { };
116482f28412SJiri Pirko 	int err;
116582f28412SJiri Pirko 
1166bccb3025SFlorian Fainelli 	err = dev_get_port_parent_id(dev, &ppid, false);
116782f28412SJiri Pirko 	if (err) {
116882f28412SJiri Pirko 		if (err == -EOPNOTSUPP)
116982f28412SJiri Pirko 			return 0;
117082f28412SJiri Pirko 		return err;
117182f28412SJiri Pirko 	}
117282f28412SJiri Pirko 
1173bccb3025SFlorian Fainelli 	if (nla_put(skb, IFLA_PHYS_SWITCH_ID, ppid.id_len, ppid.id))
117482f28412SJiri Pirko 		return -EMSGSIZE;
117582f28412SJiri Pirko 
117682f28412SJiri Pirko 	return 0;
117782f28412SJiri Pirko }
117882f28412SJiri Pirko 
1179b22b941bSHannes Frederic Sowa static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1180b22b941bSHannes Frederic Sowa 					      struct net_device *dev)
1181b22b941bSHannes Frederic Sowa {
1182550bce59SRoopa Prabhu 	struct rtnl_link_stats64 *sp;
1183b22b941bSHannes Frederic Sowa 	struct nlattr *attr;
1184b22b941bSHannes Frederic Sowa 
118558414d32SNicolas Dichtel 	attr = nla_reserve_64bit(skb, IFLA_STATS64,
118658414d32SNicolas Dichtel 				 sizeof(struct rtnl_link_stats64), IFLA_PAD);
1187b22b941bSHannes Frederic Sowa 	if (!attr)
1188b22b941bSHannes Frederic Sowa 		return -EMSGSIZE;
1189b22b941bSHannes Frederic Sowa 
1190550bce59SRoopa Prabhu 	sp = nla_data(attr);
1191550bce59SRoopa Prabhu 	dev_get_stats(dev, sp);
1192550bce59SRoopa Prabhu 
1193550bce59SRoopa Prabhu 	attr = nla_reserve(skb, IFLA_STATS,
1194550bce59SRoopa Prabhu 			   sizeof(struct rtnl_link_stats));
1195550bce59SRoopa Prabhu 	if (!attr)
1196550bce59SRoopa Prabhu 		return -EMSGSIZE;
1197550bce59SRoopa Prabhu 
1198550bce59SRoopa Prabhu 	copy_rtnl_link_stats(nla_data(attr), sp);
1199b22b941bSHannes Frederic Sowa 
1200b22b941bSHannes Frederic Sowa 	return 0;
1201b22b941bSHannes Frederic Sowa }
1202b22b941bSHannes Frederic Sowa 
1203b22b941bSHannes Frederic Sowa static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1204b22b941bSHannes Frederic Sowa 					       struct net_device *dev,
1205b22b941bSHannes Frederic Sowa 					       int vfs_num,
1206b22b941bSHannes Frederic Sowa 					       struct nlattr *vfinfo)
1207b22b941bSHannes Frederic Sowa {
1208b22b941bSHannes Frederic Sowa 	struct ifla_vf_rss_query_en vf_rss_query_en;
120979aab093SMoshe Shemesh 	struct nlattr *vf, *vfstats, *vfvlanlist;
1210b22b941bSHannes Frederic Sowa 	struct ifla_vf_link_state vf_linkstate;
121179aab093SMoshe Shemesh 	struct ifla_vf_vlan_info vf_vlan_info;
1212b22b941bSHannes Frederic Sowa 	struct ifla_vf_spoofchk vf_spoofchk;
1213b22b941bSHannes Frederic Sowa 	struct ifla_vf_tx_rate vf_tx_rate;
1214b22b941bSHannes Frederic Sowa 	struct ifla_vf_stats vf_stats;
1215b22b941bSHannes Frederic Sowa 	struct ifla_vf_trust vf_trust;
1216b22b941bSHannes Frederic Sowa 	struct ifla_vf_vlan vf_vlan;
1217b22b941bSHannes Frederic Sowa 	struct ifla_vf_rate vf_rate;
1218b22b941bSHannes Frederic Sowa 	struct ifla_vf_mac vf_mac;
121975345f88SDenis Kirjanov 	struct ifla_vf_broadcast vf_broadcast;
1220b22b941bSHannes Frederic Sowa 	struct ifla_vf_info ivi;
1221b22b941bSHannes Frederic Sowa 
12220eed9cf5SMintz, Yuval 	memset(&ivi, 0, sizeof(ivi));
12230eed9cf5SMintz, Yuval 
1224b22b941bSHannes Frederic Sowa 	/* Not all SR-IOV capable drivers support the
1225b22b941bSHannes Frederic Sowa 	 * spoofcheck and "RSS query enable" query.  Preset to
1226b22b941bSHannes Frederic Sowa 	 * -1 so the user space tool can detect that the driver
1227b22b941bSHannes Frederic Sowa 	 * didn't report anything.
1228b22b941bSHannes Frederic Sowa 	 */
1229b22b941bSHannes Frederic Sowa 	ivi.spoofchk = -1;
1230b22b941bSHannes Frederic Sowa 	ivi.rss_query_en = -1;
1231b22b941bSHannes Frederic Sowa 	ivi.trusted = -1;
1232b22b941bSHannes Frederic Sowa 	/* The default value for VF link state is "auto"
1233b22b941bSHannes Frederic Sowa 	 * IFLA_VF_LINK_STATE_AUTO which equals zero
1234b22b941bSHannes Frederic Sowa 	 */
1235b22b941bSHannes Frederic Sowa 	ivi.linkstate = 0;
123679aab093SMoshe Shemesh 	/* VLAN Protocol by default is 802.1Q */
123779aab093SMoshe Shemesh 	ivi.vlan_proto = htons(ETH_P_8021Q);
1238b22b941bSHannes Frederic Sowa 	if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1239b22b941bSHannes Frederic Sowa 		return 0;
1240b22b941bSHannes Frederic Sowa 
1241775f4f05SDan Carpenter 	memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
1242775f4f05SDan Carpenter 
1243b22b941bSHannes Frederic Sowa 	vf_mac.vf =
1244b22b941bSHannes Frederic Sowa 		vf_vlan.vf =
124579aab093SMoshe Shemesh 		vf_vlan_info.vf =
1246b22b941bSHannes Frederic Sowa 		vf_rate.vf =
1247b22b941bSHannes Frederic Sowa 		vf_tx_rate.vf =
1248b22b941bSHannes Frederic Sowa 		vf_spoofchk.vf =
1249b22b941bSHannes Frederic Sowa 		vf_linkstate.vf =
1250b22b941bSHannes Frederic Sowa 		vf_rss_query_en.vf =
1251b22b941bSHannes Frederic Sowa 		vf_trust.vf = ivi.vf;
1252b22b941bSHannes Frederic Sowa 
1253b22b941bSHannes Frederic Sowa 	memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
125475345f88SDenis Kirjanov 	memcpy(vf_broadcast.broadcast, dev->broadcast, dev->addr_len);
1255b22b941bSHannes Frederic Sowa 	vf_vlan.vlan = ivi.vlan;
1256b22b941bSHannes Frederic Sowa 	vf_vlan.qos = ivi.qos;
125779aab093SMoshe Shemesh 	vf_vlan_info.vlan = ivi.vlan;
125879aab093SMoshe Shemesh 	vf_vlan_info.qos = ivi.qos;
125979aab093SMoshe Shemesh 	vf_vlan_info.vlan_proto = ivi.vlan_proto;
1260b22b941bSHannes Frederic Sowa 	vf_tx_rate.rate = ivi.max_tx_rate;
1261b22b941bSHannes Frederic Sowa 	vf_rate.min_tx_rate = ivi.min_tx_rate;
1262b22b941bSHannes Frederic Sowa 	vf_rate.max_tx_rate = ivi.max_tx_rate;
1263b22b941bSHannes Frederic Sowa 	vf_spoofchk.setting = ivi.spoofchk;
1264b22b941bSHannes Frederic Sowa 	vf_linkstate.link_state = ivi.linkstate;
1265b22b941bSHannes Frederic Sowa 	vf_rss_query_en.setting = ivi.rss_query_en;
1266b22b941bSHannes Frederic Sowa 	vf_trust.setting = ivi.trusted;
1267ae0be8deSMichal Kubecek 	vf = nla_nest_start_noflag(skb, IFLA_VF_INFO);
126879aab093SMoshe Shemesh 	if (!vf)
126979aab093SMoshe Shemesh 		goto nla_put_vfinfo_failure;
1270b22b941bSHannes Frederic Sowa 	if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
127175345f88SDenis Kirjanov 	    nla_put(skb, IFLA_VF_BROADCAST, sizeof(vf_broadcast), &vf_broadcast) ||
1272b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1273b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1274b22b941bSHannes Frederic Sowa 		    &vf_rate) ||
1275b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1276b22b941bSHannes Frederic Sowa 		    &vf_tx_rate) ||
1277b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1278b22b941bSHannes Frederic Sowa 		    &vf_spoofchk) ||
1279b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1280b22b941bSHannes Frederic Sowa 		    &vf_linkstate) ||
1281b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1282b22b941bSHannes Frederic Sowa 		    sizeof(vf_rss_query_en),
1283b22b941bSHannes Frederic Sowa 		    &vf_rss_query_en) ||
1284b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_TRUST,
1285b22b941bSHannes Frederic Sowa 		    sizeof(vf_trust), &vf_trust))
128679aab093SMoshe Shemesh 		goto nla_put_vf_failure;
1287ae0be8deSMichal Kubecek 	vfvlanlist = nla_nest_start_noflag(skb, IFLA_VF_VLAN_LIST);
128879aab093SMoshe Shemesh 	if (!vfvlanlist)
128979aab093SMoshe Shemesh 		goto nla_put_vf_failure;
129079aab093SMoshe Shemesh 	if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
129179aab093SMoshe Shemesh 		    &vf_vlan_info)) {
129279aab093SMoshe Shemesh 		nla_nest_cancel(skb, vfvlanlist);
129379aab093SMoshe Shemesh 		goto nla_put_vf_failure;
129479aab093SMoshe Shemesh 	}
129579aab093SMoshe Shemesh 	nla_nest_end(skb, vfvlanlist);
1296b22b941bSHannes Frederic Sowa 	memset(&vf_stats, 0, sizeof(vf_stats));
1297b22b941bSHannes Frederic Sowa 	if (dev->netdev_ops->ndo_get_vf_stats)
1298b22b941bSHannes Frederic Sowa 		dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1299b22b941bSHannes Frederic Sowa 						&vf_stats);
1300ae0be8deSMichal Kubecek 	vfstats = nla_nest_start_noflag(skb, IFLA_VF_STATS);
130179aab093SMoshe Shemesh 	if (!vfstats)
130279aab093SMoshe Shemesh 		goto nla_put_vf_failure;
1303343a6d8eSNicolas Dichtel 	if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1304343a6d8eSNicolas Dichtel 			      vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1305343a6d8eSNicolas Dichtel 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1306343a6d8eSNicolas Dichtel 			      vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1307343a6d8eSNicolas Dichtel 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1308343a6d8eSNicolas Dichtel 			      vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1309343a6d8eSNicolas Dichtel 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1310343a6d8eSNicolas Dichtel 			      vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1311343a6d8eSNicolas Dichtel 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1312343a6d8eSNicolas Dichtel 			      vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1313343a6d8eSNicolas Dichtel 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
1314c5a9f6f0SEugenia Emantayev 			      vf_stats.multicast, IFLA_VF_STATS_PAD) ||
1315c5a9f6f0SEugenia Emantayev 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED,
1316c5a9f6f0SEugenia Emantayev 			      vf_stats.rx_dropped, IFLA_VF_STATS_PAD) ||
1317c5a9f6f0SEugenia Emantayev 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED,
1318c5a9f6f0SEugenia Emantayev 			      vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) {
131979aab093SMoshe Shemesh 		nla_nest_cancel(skb, vfstats);
132079aab093SMoshe Shemesh 		goto nla_put_vf_failure;
132179aab093SMoshe Shemesh 	}
1322b22b941bSHannes Frederic Sowa 	nla_nest_end(skb, vfstats);
1323b22b941bSHannes Frederic Sowa 	nla_nest_end(skb, vf);
1324b22b941bSHannes Frederic Sowa 	return 0;
132579aab093SMoshe Shemesh 
132679aab093SMoshe Shemesh nla_put_vf_failure:
132779aab093SMoshe Shemesh 	nla_nest_cancel(skb, vf);
132879aab093SMoshe Shemesh nla_put_vfinfo_failure:
132979aab093SMoshe Shemesh 	nla_nest_cancel(skb, vfinfo);
133079aab093SMoshe Shemesh 	return -EMSGSIZE;
1331b22b941bSHannes Frederic Sowa }
1332b22b941bSHannes Frederic Sowa 
1333250fc3dfSFlorian Westphal static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb,
1334250fc3dfSFlorian Westphal 					   struct net_device *dev,
1335250fc3dfSFlorian Westphal 					   u32 ext_filter_mask)
1336250fc3dfSFlorian Westphal {
1337250fc3dfSFlorian Westphal 	struct nlattr *vfinfo;
1338250fc3dfSFlorian Westphal 	int i, num_vfs;
1339250fc3dfSFlorian Westphal 
1340250fc3dfSFlorian Westphal 	if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0))
1341250fc3dfSFlorian Westphal 		return 0;
1342250fc3dfSFlorian Westphal 
1343250fc3dfSFlorian Westphal 	num_vfs = dev_num_vf(dev->dev.parent);
1344250fc3dfSFlorian Westphal 	if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs))
1345250fc3dfSFlorian Westphal 		return -EMSGSIZE;
1346250fc3dfSFlorian Westphal 
1347250fc3dfSFlorian Westphal 	if (!dev->netdev_ops->ndo_get_vf_config)
1348250fc3dfSFlorian Westphal 		return 0;
1349250fc3dfSFlorian Westphal 
1350ae0be8deSMichal Kubecek 	vfinfo = nla_nest_start_noflag(skb, IFLA_VFINFO_LIST);
1351250fc3dfSFlorian Westphal 	if (!vfinfo)
1352250fc3dfSFlorian Westphal 		return -EMSGSIZE;
1353250fc3dfSFlorian Westphal 
1354250fc3dfSFlorian Westphal 	for (i = 0; i < num_vfs; i++) {
1355250fc3dfSFlorian Westphal 		if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
1356250fc3dfSFlorian Westphal 			return -EMSGSIZE;
1357250fc3dfSFlorian Westphal 	}
1358250fc3dfSFlorian Westphal 
1359250fc3dfSFlorian Westphal 	nla_nest_end(skb, vfinfo);
1360250fc3dfSFlorian Westphal 	return 0;
1361250fc3dfSFlorian Westphal }
1362250fc3dfSFlorian Westphal 
1363b22b941bSHannes Frederic Sowa static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1364b22b941bSHannes Frederic Sowa {
13655f8e4474SKangjie Lu 	struct rtnl_link_ifmap map;
13665f8e4474SKangjie Lu 
13675f8e4474SKangjie Lu 	memset(&map, 0, sizeof(map));
13685f8e4474SKangjie Lu 	map.mem_start   = dev->mem_start;
13695f8e4474SKangjie Lu 	map.mem_end     = dev->mem_end;
13705f8e4474SKangjie Lu 	map.base_addr   = dev->base_addr;
13715f8e4474SKangjie Lu 	map.irq         = dev->irq;
13725f8e4474SKangjie Lu 	map.dma         = dev->dma;
13735f8e4474SKangjie Lu 	map.port        = dev->if_port;
13745f8e4474SKangjie Lu 
1375270cb4d0SNicolas Dichtel 	if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
1376b22b941bSHannes Frederic Sowa 		return -EMSGSIZE;
1377b22b941bSHannes Frederic Sowa 
1378b22b941bSHannes Frederic Sowa 	return 0;
1379b22b941bSHannes Frederic Sowa }
1380b22b941bSHannes Frederic Sowa 
1381a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_skb(struct net_device *dev)
1382d67b9cd2SDaniel Borkmann {
138358038695SMartin KaFai Lau 	const struct bpf_prog *generic_xdp_prog;
1384d67b9cd2SDaniel Borkmann 
1385d67b9cd2SDaniel Borkmann 	ASSERT_RTNL();
1386d67b9cd2SDaniel Borkmann 
138758038695SMartin KaFai Lau 	generic_xdp_prog = rtnl_dereference(dev->xdp_prog);
1388a25717d2SJakub Kicinski 	if (!generic_xdp_prog)
1389a25717d2SJakub Kicinski 		return 0;
1390a25717d2SJakub Kicinski 	return generic_xdp_prog->aux->id;
139158038695SMartin KaFai Lau }
1392ce158e58SJakub Kicinski 
1393a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_drv(struct net_device *dev)
1394a25717d2SJakub Kicinski {
1395a25717d2SJakub Kicinski 	return __dev_xdp_query(dev, dev->netdev_ops->ndo_bpf, XDP_QUERY_PROG);
1396a25717d2SJakub Kicinski }
1397118b4aa2SJakub Kicinski 
1398a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_hw(struct net_device *dev)
1399a25717d2SJakub Kicinski {
1400a25717d2SJakub Kicinski 	return __dev_xdp_query(dev, dev->netdev_ops->ndo_bpf,
1401a25717d2SJakub Kicinski 			       XDP_QUERY_PROG_HW);
1402a25717d2SJakub Kicinski }
1403a25717d2SJakub Kicinski 
1404a25717d2SJakub Kicinski static int rtnl_xdp_report_one(struct sk_buff *skb, struct net_device *dev,
1405a25717d2SJakub Kicinski 			       u32 *prog_id, u8 *mode, u8 tgt_mode, u32 attr,
1406a25717d2SJakub Kicinski 			       u32 (*get_prog_id)(struct net_device *dev))
1407a25717d2SJakub Kicinski {
1408a25717d2SJakub Kicinski 	u32 curr_id;
1409a25717d2SJakub Kicinski 	int err;
1410a25717d2SJakub Kicinski 
1411a25717d2SJakub Kicinski 	curr_id = get_prog_id(dev);
1412a25717d2SJakub Kicinski 	if (!curr_id)
1413a25717d2SJakub Kicinski 		return 0;
1414a25717d2SJakub Kicinski 
1415a25717d2SJakub Kicinski 	*prog_id = curr_id;
1416a25717d2SJakub Kicinski 	err = nla_put_u32(skb, attr, curr_id);
1417a25717d2SJakub Kicinski 	if (err)
1418a25717d2SJakub Kicinski 		return err;
1419a25717d2SJakub Kicinski 
1420a25717d2SJakub Kicinski 	if (*mode != XDP_ATTACHED_NONE)
1421a25717d2SJakub Kicinski 		*mode = XDP_ATTACHED_MULTI;
1422a25717d2SJakub Kicinski 	else
1423a25717d2SJakub Kicinski 		*mode = tgt_mode;
1424a25717d2SJakub Kicinski 
1425a25717d2SJakub Kicinski 	return 0;
1426d67b9cd2SDaniel Borkmann }
1427d67b9cd2SDaniel Borkmann 
1428d1fdd913SBrenden Blanco static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1429d1fdd913SBrenden Blanco {
1430d1fdd913SBrenden Blanco 	struct nlattr *xdp;
143158038695SMartin KaFai Lau 	u32 prog_id;
1432d1fdd913SBrenden Blanco 	int err;
14334f91da26SJakub Kicinski 	u8 mode;
1434d1fdd913SBrenden Blanco 
1435ae0be8deSMichal Kubecek 	xdp = nla_nest_start_noflag(skb, IFLA_XDP);
1436d1fdd913SBrenden Blanco 	if (!xdp)
1437d1fdd913SBrenden Blanco 		return -EMSGSIZE;
1438b5cdae32SDavid S. Miller 
1439a25717d2SJakub Kicinski 	prog_id = 0;
1440a25717d2SJakub Kicinski 	mode = XDP_ATTACHED_NONE;
1441202aabe8SJakub Kicinski 	err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_SKB,
1442202aabe8SJakub Kicinski 				  IFLA_XDP_SKB_PROG_ID, rtnl_xdp_prog_skb);
1443202aabe8SJakub Kicinski 	if (err)
1444a25717d2SJakub Kicinski 		goto err_cancel;
1445202aabe8SJakub Kicinski 	err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_DRV,
1446202aabe8SJakub Kicinski 				  IFLA_XDP_DRV_PROG_ID, rtnl_xdp_prog_drv);
1447202aabe8SJakub Kicinski 	if (err)
1448a25717d2SJakub Kicinski 		goto err_cancel;
1449202aabe8SJakub Kicinski 	err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_HW,
1450202aabe8SJakub Kicinski 				  IFLA_XDP_HW_PROG_ID, rtnl_xdp_prog_hw);
1451202aabe8SJakub Kicinski 	if (err)
1452a25717d2SJakub Kicinski 		goto err_cancel;
1453a25717d2SJakub Kicinski 
14544f91da26SJakub Kicinski 	err = nla_put_u8(skb, IFLA_XDP_ATTACHED, mode);
1455d1fdd913SBrenden Blanco 	if (err)
1456d1fdd913SBrenden Blanco 		goto err_cancel;
1457d1fdd913SBrenden Blanco 
1458a25717d2SJakub Kicinski 	if (prog_id && mode != XDP_ATTACHED_MULTI) {
145958038695SMartin KaFai Lau 		err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id);
146058038695SMartin KaFai Lau 		if (err)
146158038695SMartin KaFai Lau 			goto err_cancel;
146258038695SMartin KaFai Lau 	}
146358038695SMartin KaFai Lau 
1464d1fdd913SBrenden Blanco 	nla_nest_end(skb, xdp);
1465d1fdd913SBrenden Blanco 	return 0;
1466d1fdd913SBrenden Blanco 
1467d1fdd913SBrenden Blanco err_cancel:
1468d1fdd913SBrenden Blanco 	nla_nest_cancel(skb, xdp);
1469d1fdd913SBrenden Blanco 	return err;
1470d1fdd913SBrenden Blanco }
1471d1fdd913SBrenden Blanco 
14723d3ea5afSVlad Yasevich static u32 rtnl_get_event(unsigned long event)
14733d3ea5afSVlad Yasevich {
14743d3ea5afSVlad Yasevich 	u32 rtnl_event_type = IFLA_EVENT_NONE;
14753d3ea5afSVlad Yasevich 
14763d3ea5afSVlad Yasevich 	switch (event) {
14773d3ea5afSVlad Yasevich 	case NETDEV_REBOOT:
14783d3ea5afSVlad Yasevich 		rtnl_event_type = IFLA_EVENT_REBOOT;
14793d3ea5afSVlad Yasevich 		break;
14803d3ea5afSVlad Yasevich 	case NETDEV_FEAT_CHANGE:
14813d3ea5afSVlad Yasevich 		rtnl_event_type = IFLA_EVENT_FEATURES;
14823d3ea5afSVlad Yasevich 		break;
14833d3ea5afSVlad Yasevich 	case NETDEV_BONDING_FAILOVER:
14843d3ea5afSVlad Yasevich 		rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER;
14853d3ea5afSVlad Yasevich 		break;
14863d3ea5afSVlad Yasevich 	case NETDEV_NOTIFY_PEERS:
14873d3ea5afSVlad Yasevich 		rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS;
14883d3ea5afSVlad Yasevich 		break;
14893d3ea5afSVlad Yasevich 	case NETDEV_RESEND_IGMP:
14903d3ea5afSVlad Yasevich 		rtnl_event_type = IFLA_EVENT_IGMP_RESEND;
14913d3ea5afSVlad Yasevich 		break;
14923d3ea5afSVlad Yasevich 	case NETDEV_CHANGEINFODATA:
14933d3ea5afSVlad Yasevich 		rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS;
14943d3ea5afSVlad Yasevich 		break;
14953d3ea5afSVlad Yasevich 	default:
14963d3ea5afSVlad Yasevich 		break;
14973d3ea5afSVlad Yasevich 	}
14983d3ea5afSVlad Yasevich 
14993d3ea5afSVlad Yasevich 	return rtnl_event_type;
15003d3ea5afSVlad Yasevich }
15013d3ea5afSVlad Yasevich 
150279110a04SFlorian Westphal static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev)
150379110a04SFlorian Westphal {
150479110a04SFlorian Westphal 	const struct net_device *upper_dev;
150579110a04SFlorian Westphal 	int ret = 0;
150679110a04SFlorian Westphal 
150779110a04SFlorian Westphal 	rcu_read_lock();
150879110a04SFlorian Westphal 
150979110a04SFlorian Westphal 	upper_dev = netdev_master_upper_dev_get_rcu(dev);
151079110a04SFlorian Westphal 	if (upper_dev)
151179110a04SFlorian Westphal 		ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex);
151279110a04SFlorian Westphal 
151379110a04SFlorian Westphal 	rcu_read_unlock();
151479110a04SFlorian Westphal 	return ret;
151579110a04SFlorian Westphal }
151679110a04SFlorian Westphal 
1517feadc4b6SSabrina Dubroca static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev,
1518feadc4b6SSabrina Dubroca 			  bool force)
151979110a04SFlorian Westphal {
152079110a04SFlorian Westphal 	int ifindex = dev_get_iflink(dev);
152179110a04SFlorian Westphal 
1522feadc4b6SSabrina Dubroca 	if (force || dev->ifindex != ifindex)
152379110a04SFlorian Westphal 		return nla_put_u32(skb, IFLA_LINK, ifindex);
1524feadc4b6SSabrina Dubroca 
1525feadc4b6SSabrina Dubroca 	return 0;
152679110a04SFlorian Westphal }
152779110a04SFlorian Westphal 
15286c557001SFlorian Westphal static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb,
15296c557001SFlorian Westphal 					      struct net_device *dev)
15306c557001SFlorian Westphal {
15316c557001SFlorian Westphal 	char buf[IFALIASZ];
15326c557001SFlorian Westphal 	int ret;
15336c557001SFlorian Westphal 
15346c557001SFlorian Westphal 	ret = dev_get_alias(dev, buf, sizeof(buf));
15356c557001SFlorian Westphal 	return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0;
15366c557001SFlorian Westphal }
15376c557001SFlorian Westphal 
1538b1e66b9aSFlorian Westphal static int rtnl_fill_link_netnsid(struct sk_buff *skb,
153979e1ad14SJiri Benc 				  const struct net_device *dev,
154079e1ad14SJiri Benc 				  struct net *src_net)
1541b1e66b9aSFlorian Westphal {
1542feadc4b6SSabrina Dubroca 	bool put_iflink = false;
1543feadc4b6SSabrina Dubroca 
1544b1e66b9aSFlorian Westphal 	if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) {
1545b1e66b9aSFlorian Westphal 		struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1546b1e66b9aSFlorian Westphal 
1547b1e66b9aSFlorian Westphal 		if (!net_eq(dev_net(dev), link_net)) {
154879e1ad14SJiri Benc 			int id = peernet2id_alloc(src_net, link_net);
1549b1e66b9aSFlorian Westphal 
1550b1e66b9aSFlorian Westphal 			if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1551b1e66b9aSFlorian Westphal 				return -EMSGSIZE;
1552feadc4b6SSabrina Dubroca 
1553feadc4b6SSabrina Dubroca 			put_iflink = true;
1554b1e66b9aSFlorian Westphal 		}
1555b1e66b9aSFlorian Westphal 	}
1556b1e66b9aSFlorian Westphal 
1557feadc4b6SSabrina Dubroca 	return nla_put_iflink(skb, dev, put_iflink);
1558b1e66b9aSFlorian Westphal }
1559b1e66b9aSFlorian Westphal 
1560070cbf5bSFlorian Westphal static int rtnl_fill_link_af(struct sk_buff *skb,
1561070cbf5bSFlorian Westphal 			     const struct net_device *dev,
1562070cbf5bSFlorian Westphal 			     u32 ext_filter_mask)
1563070cbf5bSFlorian Westphal {
1564070cbf5bSFlorian Westphal 	const struct rtnl_af_ops *af_ops;
1565070cbf5bSFlorian Westphal 	struct nlattr *af_spec;
1566070cbf5bSFlorian Westphal 
1567ae0be8deSMichal Kubecek 	af_spec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
1568070cbf5bSFlorian Westphal 	if (!af_spec)
1569070cbf5bSFlorian Westphal 		return -EMSGSIZE;
1570070cbf5bSFlorian Westphal 
15715fa85a09SFlorian Westphal 	list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
1572070cbf5bSFlorian Westphal 		struct nlattr *af;
1573070cbf5bSFlorian Westphal 		int err;
1574070cbf5bSFlorian Westphal 
1575070cbf5bSFlorian Westphal 		if (!af_ops->fill_link_af)
1576070cbf5bSFlorian Westphal 			continue;
1577070cbf5bSFlorian Westphal 
1578ae0be8deSMichal Kubecek 		af = nla_nest_start_noflag(skb, af_ops->family);
1579070cbf5bSFlorian Westphal 		if (!af)
1580070cbf5bSFlorian Westphal 			return -EMSGSIZE;
1581070cbf5bSFlorian Westphal 
1582070cbf5bSFlorian Westphal 		err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
1583070cbf5bSFlorian Westphal 		/*
1584070cbf5bSFlorian Westphal 		 * Caller may return ENODATA to indicate that there
1585070cbf5bSFlorian Westphal 		 * was no data to be dumped. This is not an error, it
1586070cbf5bSFlorian Westphal 		 * means we should trim the attribute header and
1587070cbf5bSFlorian Westphal 		 * continue.
1588070cbf5bSFlorian Westphal 		 */
1589070cbf5bSFlorian Westphal 		if (err == -ENODATA)
1590070cbf5bSFlorian Westphal 			nla_nest_cancel(skb, af);
1591070cbf5bSFlorian Westphal 		else if (err < 0)
1592070cbf5bSFlorian Westphal 			return -EMSGSIZE;
1593070cbf5bSFlorian Westphal 
1594070cbf5bSFlorian Westphal 		nla_nest_end(skb, af);
1595070cbf5bSFlorian Westphal 	}
1596070cbf5bSFlorian Westphal 
1597070cbf5bSFlorian Westphal 	nla_nest_end(skb, af_spec);
1598070cbf5bSFlorian Westphal 	return 0;
1599070cbf5bSFlorian Westphal }
1600070cbf5bSFlorian Westphal 
160188f4fb0cSJiri Pirko static int rtnl_fill_alt_ifnames(struct sk_buff *skb,
160288f4fb0cSJiri Pirko 				 const struct net_device *dev)
160388f4fb0cSJiri Pirko {
160488f4fb0cSJiri Pirko 	struct netdev_name_node *name_node;
160588f4fb0cSJiri Pirko 	int count = 0;
160688f4fb0cSJiri Pirko 
160788f4fb0cSJiri Pirko 	list_for_each_entry(name_node, &dev->name_node->list, list) {
160888f4fb0cSJiri Pirko 		if (nla_put_string(skb, IFLA_ALT_IFNAME, name_node->name))
160988f4fb0cSJiri Pirko 			return -EMSGSIZE;
161088f4fb0cSJiri Pirko 		count++;
161188f4fb0cSJiri Pirko 	}
161288f4fb0cSJiri Pirko 	return count;
161388f4fb0cSJiri Pirko }
161488f4fb0cSJiri Pirko 
161588f4fb0cSJiri Pirko static int rtnl_fill_prop_list(struct sk_buff *skb,
161688f4fb0cSJiri Pirko 			       const struct net_device *dev)
161788f4fb0cSJiri Pirko {
161888f4fb0cSJiri Pirko 	struct nlattr *prop_list;
161988f4fb0cSJiri Pirko 	int ret;
162088f4fb0cSJiri Pirko 
162188f4fb0cSJiri Pirko 	prop_list = nla_nest_start(skb, IFLA_PROP_LIST);
162288f4fb0cSJiri Pirko 	if (!prop_list)
162388f4fb0cSJiri Pirko 		return -EMSGSIZE;
162488f4fb0cSJiri Pirko 
162588f4fb0cSJiri Pirko 	ret = rtnl_fill_alt_ifnames(skb, dev);
162688f4fb0cSJiri Pirko 	if (ret <= 0)
162788f4fb0cSJiri Pirko 		goto nest_cancel;
162888f4fb0cSJiri Pirko 
162988f4fb0cSJiri Pirko 	nla_nest_end(skb, prop_list);
163088f4fb0cSJiri Pirko 	return 0;
163188f4fb0cSJiri Pirko 
163288f4fb0cSJiri Pirko nest_cancel:
163388f4fb0cSJiri Pirko 	nla_nest_cancel(skb, prop_list);
163488f4fb0cSJiri Pirko 	return ret;
163588f4fb0cSJiri Pirko }
163688f4fb0cSJiri Pirko 
163779e1ad14SJiri Benc static int rtnl_fill_ifinfo(struct sk_buff *skb,
163879e1ad14SJiri Benc 			    struct net_device *dev, struct net *src_net,
1639575c3e2aSPatrick McHardy 			    int type, u32 pid, u32 seq, u32 change,
16403d3ea5afSVlad Yasevich 			    unsigned int flags, u32 ext_filter_mask,
164138e01b30SNicolas Dichtel 			    u32 event, int *new_nsid, int new_ifindex,
164238e01b30SNicolas Dichtel 			    int tgt_netnsid)
1643b60c5115SThomas Graf {
1644b60c5115SThomas Graf 	struct ifinfomsg *ifm;
16451da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
16461da177e4SLinus Torvalds 
16472907c35fSEric Dumazet 	ASSERT_RTNL();
1648b60c5115SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1649b60c5115SThomas Graf 	if (nlh == NULL)
165026932566SPatrick McHardy 		return -EMSGSIZE;
16511da177e4SLinus Torvalds 
1652b60c5115SThomas Graf 	ifm = nlmsg_data(nlh);
1653b60c5115SThomas Graf 	ifm->ifi_family = AF_UNSPEC;
1654b60c5115SThomas Graf 	ifm->__ifi_pad = 0;
1655b60c5115SThomas Graf 	ifm->ifi_type = dev->type;
1656b60c5115SThomas Graf 	ifm->ifi_index = dev->ifindex;
1657b60c5115SThomas Graf 	ifm->ifi_flags = dev_get_flags(dev);
1658b60c5115SThomas Graf 	ifm->ifi_change = change;
16591da177e4SLinus Torvalds 
16607e4a8d5aSChristian Brauner 	if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_TARGET_NETNSID, tgt_netnsid))
166179e1ad14SJiri Benc 		goto nla_put_failure;
166279e1ad14SJiri Benc 
1663a6574349SDavid S. Miller 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1664a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1665a6574349SDavid S. Miller 	    nla_put_u8(skb, IFLA_OPERSTATE,
1666a6574349SDavid S. Miller 		       netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1667a6574349SDavid S. Miller 	    nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1668a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
16693e7a50ceSStephen Hemminger 	    nla_put_u32(skb, IFLA_MIN_MTU, dev->min_mtu) ||
16703e7a50ceSStephen Hemminger 	    nla_put_u32(skb, IFLA_MAX_MTU, dev->max_mtu) ||
1671a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_GROUP, dev->group) ||
1672edbc0bb3SBen Greear 	    nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
167376ff5cc9SJiri Pirko 	    nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
1674c70ce028SEric Dumazet 	    nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1675c70ce028SEric Dumazet 	    nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
16761d69c2b3SMark A. Greer #ifdef CONFIG_RPS
167776ff5cc9SJiri Pirko 	    nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
16781d69c2b3SMark A. Greer #endif
167979110a04SFlorian Westphal 	    put_master_ifindex(skb, dev) ||
16809a57247fSJiri Pirko 	    nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
1681a6574349SDavid S. Miller 	    (dev->qdisc &&
1682a6574349SDavid S. Miller 	     nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
16836c557001SFlorian Westphal 	    nla_put_ifalias(skb, dev) ||
16842d3b479dSdavid decotigny 	    nla_put_u32(skb, IFLA_CARRIER_CHANGES,
1685b2d3bcfaSDavid Decotigny 			atomic_read(&dev->carrier_up_count) +
1686b2d3bcfaSDavid Decotigny 			atomic_read(&dev->carrier_down_count)) ||
1687b2d3bcfaSDavid Decotigny 	    nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down) ||
1688b2d3bcfaSDavid Decotigny 	    nla_put_u32(skb, IFLA_CARRIER_UP_COUNT,
1689b2d3bcfaSDavid Decotigny 			atomic_read(&dev->carrier_up_count)) ||
1690b2d3bcfaSDavid Decotigny 	    nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT,
1691b2d3bcfaSDavid Decotigny 			atomic_read(&dev->carrier_down_count)))
1692a6574349SDavid S. Miller 		goto nla_put_failure;
16930b815a1aSStephen Hemminger 
16943d3ea5afSVlad Yasevich 	if (event != IFLA_EVENT_NONE) {
16953d3ea5afSVlad Yasevich 		if (nla_put_u32(skb, IFLA_EVENT, event))
16963d3ea5afSVlad Yasevich 			goto nla_put_failure;
16973d3ea5afSVlad Yasevich 	}
16983d3ea5afSVlad Yasevich 
1699b22b941bSHannes Frederic Sowa 	if (rtnl_fill_link_ifmap(skb, dev))
1700a6574349SDavid S. Miller 		goto nla_put_failure;
17011da177e4SLinus Torvalds 
17021da177e4SLinus Torvalds 	if (dev->addr_len) {
1703a6574349SDavid S. Miller 		if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1704a6574349SDavid S. Miller 		    nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1705a6574349SDavid S. Miller 			goto nla_put_failure;
17061da177e4SLinus Torvalds 	}
17071da177e4SLinus Torvalds 
170866cae9edSJiri Pirko 	if (rtnl_phys_port_id_fill(skb, dev))
170966cae9edSJiri Pirko 		goto nla_put_failure;
171066cae9edSJiri Pirko 
1711db24a904SDavid Ahern 	if (rtnl_phys_port_name_fill(skb, dev))
1712db24a904SDavid Ahern 		goto nla_put_failure;
1713db24a904SDavid Ahern 
171482f28412SJiri Pirko 	if (rtnl_phys_switch_id_fill(skb, dev))
171582f28412SJiri Pirko 		goto nla_put_failure;
171682f28412SJiri Pirko 
1717b22b941bSHannes Frederic Sowa 	if (rtnl_fill_stats(skb, dev))
1718b60c5115SThomas Graf 		goto nla_put_failure;
1719b60c5115SThomas Graf 
1720250fc3dfSFlorian Westphal 	if (rtnl_fill_vf(skb, dev, ext_filter_mask))
1721a6574349SDavid S. Miller 		goto nla_put_failure;
172257b61080SScott Feldman 
1723c53864fdSDavid Gibson 	if (rtnl_port_fill(skb, dev, ext_filter_mask))
172457b61080SScott Feldman 		goto nla_put_failure;
172557b61080SScott Feldman 
1726d1fdd913SBrenden Blanco 	if (rtnl_xdp_fill(skb, dev))
1727d1fdd913SBrenden Blanco 		goto nla_put_failure;
1728d1fdd913SBrenden Blanco 
1729ba7d49b1SJiri Pirko 	if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
173038f7b870SPatrick McHardy 		if (rtnl_link_fill(skb, dev) < 0)
173138f7b870SPatrick McHardy 			goto nla_put_failure;
173238f7b870SPatrick McHardy 	}
173338f7b870SPatrick McHardy 
173479e1ad14SJiri Benc 	if (rtnl_fill_link_netnsid(skb, dev, src_net))
1735d37512a2SNicolas Dichtel 		goto nla_put_failure;
1736d37512a2SNicolas Dichtel 
17376621dd29SNicolas Dichtel 	if (new_nsid &&
17386621dd29SNicolas Dichtel 	    nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0)
17396621dd29SNicolas Dichtel 		goto nla_put_failure;
174038e01b30SNicolas Dichtel 	if (new_ifindex &&
174138e01b30SNicolas Dichtel 	    nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0)
174238e01b30SNicolas Dichtel 		goto nla_put_failure;
174338e01b30SNicolas Dichtel 
17446621dd29SNicolas Dichtel 
17455fa85a09SFlorian Westphal 	rcu_read_lock();
1746070cbf5bSFlorian Westphal 	if (rtnl_fill_link_af(skb, dev, ext_filter_mask))
17475fa85a09SFlorian Westphal 		goto nla_put_failure_rcu;
17485fa85a09SFlorian Westphal 	rcu_read_unlock();
1749f8ff182cSThomas Graf 
175088f4fb0cSJiri Pirko 	if (rtnl_fill_prop_list(skb, dev))
175188f4fb0cSJiri Pirko 		goto nla_put_failure;
175288f4fb0cSJiri Pirko 
1753053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
1754053c095aSJohannes Berg 	return 0;
1755b60c5115SThomas Graf 
17565fa85a09SFlorian Westphal nla_put_failure_rcu:
17575fa85a09SFlorian Westphal 	rcu_read_unlock();
1758b60c5115SThomas Graf nla_put_failure:
175926932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
176026932566SPatrick McHardy 	return -EMSGSIZE;
17611da177e4SLinus Torvalds }
17621da177e4SLinus Torvalds 
1763f7b12606SJiri Pirko static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1764f7b12606SJiri Pirko 	[IFLA_IFNAME]		= { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1765f7b12606SJiri Pirko 	[IFLA_ADDRESS]		= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1766f7b12606SJiri Pirko 	[IFLA_BROADCAST]	= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1767f7b12606SJiri Pirko 	[IFLA_MAP]		= { .len = sizeof(struct rtnl_link_ifmap) },
1768f7b12606SJiri Pirko 	[IFLA_MTU]		= { .type = NLA_U32 },
1769f7b12606SJiri Pirko 	[IFLA_LINK]		= { .type = NLA_U32 },
1770f7b12606SJiri Pirko 	[IFLA_MASTER]		= { .type = NLA_U32 },
1771f7b12606SJiri Pirko 	[IFLA_CARRIER]		= { .type = NLA_U8 },
1772f7b12606SJiri Pirko 	[IFLA_TXQLEN]		= { .type = NLA_U32 },
1773f7b12606SJiri Pirko 	[IFLA_WEIGHT]		= { .type = NLA_U32 },
1774f7b12606SJiri Pirko 	[IFLA_OPERSTATE]	= { .type = NLA_U8 },
1775f7b12606SJiri Pirko 	[IFLA_LINKMODE]		= { .type = NLA_U8 },
1776f7b12606SJiri Pirko 	[IFLA_LINKINFO]		= { .type = NLA_NESTED },
1777f7b12606SJiri Pirko 	[IFLA_NET_NS_PID]	= { .type = NLA_U32 },
1778f7b12606SJiri Pirko 	[IFLA_NET_NS_FD]	= { .type = NLA_U32 },
17792459b4c6SNicolas Dichtel 	/* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
17802459b4c6SNicolas Dichtel 	 * allow 0-length string (needed to remove an alias).
17812459b4c6SNicolas Dichtel 	 */
17822459b4c6SNicolas Dichtel 	[IFLA_IFALIAS]	        = { .type = NLA_BINARY, .len = IFALIASZ - 1 },
1783f7b12606SJiri Pirko 	[IFLA_VFINFO_LIST]	= {. type = NLA_NESTED },
1784f7b12606SJiri Pirko 	[IFLA_VF_PORTS]		= { .type = NLA_NESTED },
1785f7b12606SJiri Pirko 	[IFLA_PORT_SELF]	= { .type = NLA_NESTED },
1786f7b12606SJiri Pirko 	[IFLA_AF_SPEC]		= { .type = NLA_NESTED },
1787f7b12606SJiri Pirko 	[IFLA_EXT_MASK]		= { .type = NLA_U32 },
1788f7b12606SJiri Pirko 	[IFLA_PROMISCUITY]	= { .type = NLA_U32 },
1789f7b12606SJiri Pirko 	[IFLA_NUM_TX_QUEUES]	= { .type = NLA_U32 },
1790f7b12606SJiri Pirko 	[IFLA_NUM_RX_QUEUES]	= { .type = NLA_U32 },
179146e6b992SStephen Hemminger 	[IFLA_GSO_MAX_SEGS]	= { .type = NLA_U32 },
179246e6b992SStephen Hemminger 	[IFLA_GSO_MAX_SIZE]	= { .type = NLA_U32 },
179302637fceSJiri Pirko 	[IFLA_PHYS_PORT_ID]	= { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
17942d3b479dSdavid decotigny 	[IFLA_CARRIER_CHANGES]	= { .type = NLA_U32 },  /* ignored */
179582f28412SJiri Pirko 	[IFLA_PHYS_SWITCH_ID]	= { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1796317f4810SNicolas Dichtel 	[IFLA_LINK_NETNSID]	= { .type = NLA_S32 },
179788d6378bSAnuradha Karuppiah 	[IFLA_PROTO_DOWN]	= { .type = NLA_U8 },
1798d1fdd913SBrenden Blanco 	[IFLA_XDP]		= { .type = NLA_NESTED },
17993d3ea5afSVlad Yasevich 	[IFLA_EVENT]		= { .type = NLA_U32 },
1800db833d40SSerhey Popovych 	[IFLA_GROUP]		= { .type = NLA_U32 },
18017e4a8d5aSChristian Brauner 	[IFLA_TARGET_NETNSID]	= { .type = NLA_S32 },
1802b2d3bcfaSDavid Decotigny 	[IFLA_CARRIER_UP_COUNT]	= { .type = NLA_U32 },
1803b2d3bcfaSDavid Decotigny 	[IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 },
18043e7a50ceSStephen Hemminger 	[IFLA_MIN_MTU]		= { .type = NLA_U32 },
18053e7a50ceSStephen Hemminger 	[IFLA_MAX_MTU]		= { .type = NLA_U32 },
180636fbf1e5SJiri Pirko 	[IFLA_PROP_LIST]	= { .type = NLA_NESTED },
180736fbf1e5SJiri Pirko 	[IFLA_ALT_IFNAME]	= { .type = NLA_STRING,
180836fbf1e5SJiri Pirko 				    .len = ALTIFNAMSIZ - 1 },
1809f7b12606SJiri Pirko };
1810f7b12606SJiri Pirko 
1811f7b12606SJiri Pirko static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1812f7b12606SJiri Pirko 	[IFLA_INFO_KIND]	= { .type = NLA_STRING },
1813f7b12606SJiri Pirko 	[IFLA_INFO_DATA]	= { .type = NLA_NESTED },
1814f7b12606SJiri Pirko 	[IFLA_INFO_SLAVE_KIND]	= { .type = NLA_STRING },
1815f7b12606SJiri Pirko 	[IFLA_INFO_SLAVE_DATA]	= { .type = NLA_NESTED },
1816f7b12606SJiri Pirko };
1817f7b12606SJiri Pirko 
1818f7b12606SJiri Pirko static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1819364d5716SDaniel Borkmann 	[IFLA_VF_MAC]		= { .len = sizeof(struct ifla_vf_mac) },
182075345f88SDenis Kirjanov 	[IFLA_VF_BROADCAST]	= { .type = NLA_REJECT },
1821364d5716SDaniel Borkmann 	[IFLA_VF_VLAN]		= { .len = sizeof(struct ifla_vf_vlan) },
182279aab093SMoshe Shemesh 	[IFLA_VF_VLAN_LIST]     = { .type = NLA_NESTED },
1823364d5716SDaniel Borkmann 	[IFLA_VF_TX_RATE]	= { .len = sizeof(struct ifla_vf_tx_rate) },
1824364d5716SDaniel Borkmann 	[IFLA_VF_SPOOFCHK]	= { .len = sizeof(struct ifla_vf_spoofchk) },
1825364d5716SDaniel Borkmann 	[IFLA_VF_RATE]		= { .len = sizeof(struct ifla_vf_rate) },
1826364d5716SDaniel Borkmann 	[IFLA_VF_LINK_STATE]	= { .len = sizeof(struct ifla_vf_link_state) },
182701a3d796SVlad Zolotarov 	[IFLA_VF_RSS_QUERY_EN]	= { .len = sizeof(struct ifla_vf_rss_query_en) },
18283b766cd8SEran Ben Elisha 	[IFLA_VF_STATS]		= { .type = NLA_NESTED },
1829dd461d6aSHiroshi Shimamoto 	[IFLA_VF_TRUST]		= { .len = sizeof(struct ifla_vf_trust) },
1830cc8e27ccSEli Cohen 	[IFLA_VF_IB_NODE_GUID]	= { .len = sizeof(struct ifla_vf_guid) },
1831cc8e27ccSEli Cohen 	[IFLA_VF_IB_PORT_GUID]	= { .len = sizeof(struct ifla_vf_guid) },
18323b766cd8SEran Ben Elisha };
18333b766cd8SEran Ben Elisha 
1834f7b12606SJiri Pirko static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1835f7b12606SJiri Pirko 	[IFLA_PORT_VF]		= { .type = NLA_U32 },
1836f7b12606SJiri Pirko 	[IFLA_PORT_PROFILE]	= { .type = NLA_STRING,
1837f7b12606SJiri Pirko 				    .len = PORT_PROFILE_MAX },
1838f7b12606SJiri Pirko 	[IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1839f7b12606SJiri Pirko 				      .len = PORT_UUID_MAX },
1840f7b12606SJiri Pirko 	[IFLA_PORT_HOST_UUID]	= { .type = NLA_STRING,
1841f7b12606SJiri Pirko 				    .len = PORT_UUID_MAX },
1842f7b12606SJiri Pirko 	[IFLA_PORT_REQUEST]	= { .type = NLA_U8, },
1843f7b12606SJiri Pirko 	[IFLA_PORT_RESPONSE]	= { .type = NLA_U16, },
1844025331dfSDaniel Borkmann 
1845025331dfSDaniel Borkmann 	/* Unused, but we need to keep it here since user space could
1846025331dfSDaniel Borkmann 	 * fill it. It's also broken with regard to NLA_BINARY use in
1847025331dfSDaniel Borkmann 	 * combination with structs.
1848025331dfSDaniel Borkmann 	 */
1849025331dfSDaniel Borkmann 	[IFLA_PORT_VSI_TYPE]	= { .type = NLA_BINARY,
1850025331dfSDaniel Borkmann 				    .len = sizeof(struct ifla_port_vsi) },
1851f7b12606SJiri Pirko };
1852f7b12606SJiri Pirko 
1853d1fdd913SBrenden Blanco static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
1854d1fdd913SBrenden Blanco 	[IFLA_XDP_FD]		= { .type = NLA_S32 },
1855d1fdd913SBrenden Blanco 	[IFLA_XDP_ATTACHED]	= { .type = NLA_U8 },
185685de8576SDaniel Borkmann 	[IFLA_XDP_FLAGS]	= { .type = NLA_U32 },
185758038695SMartin KaFai Lau 	[IFLA_XDP_PROG_ID]	= { .type = NLA_U32 },
1858d1fdd913SBrenden Blanco };
1859d1fdd913SBrenden Blanco 
1860dc599f76SDavid Ahern static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1861dc599f76SDavid Ahern {
1862dc599f76SDavid Ahern 	const struct rtnl_link_ops *ops = NULL;
1863dc599f76SDavid Ahern 	struct nlattr *linfo[IFLA_INFO_MAX + 1];
1864dc599f76SDavid Ahern 
18658cb08174SJohannes Berg 	if (nla_parse_nested_deprecated(linfo, IFLA_INFO_MAX, nla, ifla_info_policy, NULL) < 0)
1866dc599f76SDavid Ahern 		return NULL;
1867dc599f76SDavid Ahern 
1868dc599f76SDavid Ahern 	if (linfo[IFLA_INFO_KIND]) {
1869dc599f76SDavid Ahern 		char kind[MODULE_NAME_LEN];
1870dc599f76SDavid Ahern 
1871dc599f76SDavid Ahern 		nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1872dc599f76SDavid Ahern 		ops = rtnl_link_ops_get(kind);
1873dc599f76SDavid Ahern 	}
1874dc599f76SDavid Ahern 
1875dc599f76SDavid Ahern 	return ops;
1876dc599f76SDavid Ahern }
1877dc599f76SDavid Ahern 
1878dc599f76SDavid Ahern static bool link_master_filtered(struct net_device *dev, int master_idx)
1879dc599f76SDavid Ahern {
1880dc599f76SDavid Ahern 	struct net_device *master;
1881dc599f76SDavid Ahern 
1882dc599f76SDavid Ahern 	if (!master_idx)
1883dc599f76SDavid Ahern 		return false;
1884dc599f76SDavid Ahern 
1885dc599f76SDavid Ahern 	master = netdev_master_upper_dev_get(dev);
1886dc599f76SDavid Ahern 	if (!master || master->ifindex != master_idx)
1887dc599f76SDavid Ahern 		return true;
1888dc599f76SDavid Ahern 
1889dc599f76SDavid Ahern 	return false;
1890dc599f76SDavid Ahern }
1891dc599f76SDavid Ahern 
1892dc599f76SDavid Ahern static bool link_kind_filtered(const struct net_device *dev,
1893dc599f76SDavid Ahern 			       const struct rtnl_link_ops *kind_ops)
1894dc599f76SDavid Ahern {
1895dc599f76SDavid Ahern 	if (kind_ops && dev->rtnl_link_ops != kind_ops)
1896dc599f76SDavid Ahern 		return true;
1897dc599f76SDavid Ahern 
1898dc599f76SDavid Ahern 	return false;
1899dc599f76SDavid Ahern }
1900dc599f76SDavid Ahern 
1901dc599f76SDavid Ahern static bool link_dump_filtered(struct net_device *dev,
1902dc599f76SDavid Ahern 			       int master_idx,
1903dc599f76SDavid Ahern 			       const struct rtnl_link_ops *kind_ops)
1904dc599f76SDavid Ahern {
1905dc599f76SDavid Ahern 	if (link_master_filtered(dev, master_idx) ||
1906dc599f76SDavid Ahern 	    link_kind_filtered(dev, kind_ops))
1907dc599f76SDavid Ahern 		return true;
1908dc599f76SDavid Ahern 
1909dc599f76SDavid Ahern 	return false;
1910dc599f76SDavid Ahern }
1911dc599f76SDavid Ahern 
1912c383edc4SChristian Brauner /**
1913c383edc4SChristian Brauner  * rtnl_get_net_ns_capable - Get netns if sufficiently privileged.
1914c383edc4SChristian Brauner  * @sk: netlink socket
1915c383edc4SChristian Brauner  * @netnsid: network namespace identifier
1916c383edc4SChristian Brauner  *
1917c383edc4SChristian Brauner  * Returns the network namespace identified by netnsid on success or an error
1918c383edc4SChristian Brauner  * pointer on failure.
1919c383edc4SChristian Brauner  */
1920c383edc4SChristian Brauner struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid)
192179e1ad14SJiri Benc {
192279e1ad14SJiri Benc 	struct net *net;
192379e1ad14SJiri Benc 
1924f428fe4aSAndrei Vagin 	net = get_net_ns_by_id(sock_net(sk), netnsid);
192579e1ad14SJiri Benc 	if (!net)
192679e1ad14SJiri Benc 		return ERR_PTR(-EINVAL);
192779e1ad14SJiri Benc 
192879e1ad14SJiri Benc 	/* For now, the caller is required to have CAP_NET_ADMIN in
192979e1ad14SJiri Benc 	 * the user namespace owning the target net ns.
193079e1ad14SJiri Benc 	 */
1931f428fe4aSAndrei Vagin 	if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) {
193279e1ad14SJiri Benc 		put_net(net);
193379e1ad14SJiri Benc 		return ERR_PTR(-EACCES);
193479e1ad14SJiri Benc 	}
193579e1ad14SJiri Benc 	return net;
193679e1ad14SJiri Benc }
1937c383edc4SChristian Brauner EXPORT_SYMBOL_GPL(rtnl_get_net_ns_capable);
193879e1ad14SJiri Benc 
1939905cf0abSDavid Ahern static int rtnl_valid_dump_ifinfo_req(const struct nlmsghdr *nlh,
1940905cf0abSDavid Ahern 				      bool strict_check, struct nlattr **tb,
1941905cf0abSDavid Ahern 				      struct netlink_ext_ack *extack)
1942905cf0abSDavid Ahern {
1943905cf0abSDavid Ahern 	int hdrlen;
1944905cf0abSDavid Ahern 
1945905cf0abSDavid Ahern 	if (strict_check) {
1946905cf0abSDavid Ahern 		struct ifinfomsg *ifm;
1947905cf0abSDavid Ahern 
1948905cf0abSDavid Ahern 		if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
1949905cf0abSDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid header for link dump");
1950905cf0abSDavid Ahern 			return -EINVAL;
1951905cf0abSDavid Ahern 		}
1952905cf0abSDavid Ahern 
1953905cf0abSDavid Ahern 		ifm = nlmsg_data(nlh);
1954905cf0abSDavid Ahern 		if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
1955905cf0abSDavid Ahern 		    ifm->ifi_change) {
1956905cf0abSDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid values in header for link dump request");
1957905cf0abSDavid Ahern 			return -EINVAL;
1958905cf0abSDavid Ahern 		}
1959905cf0abSDavid Ahern 		if (ifm->ifi_index) {
1960905cf0abSDavid Ahern 			NL_SET_ERR_MSG(extack, "Filter by device index not supported for link dumps");
1961905cf0abSDavid Ahern 			return -EINVAL;
1962905cf0abSDavid Ahern 		}
1963905cf0abSDavid Ahern 
19648cb08174SJohannes Berg 		return nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb,
19658cb08174SJohannes Berg 						     IFLA_MAX, ifla_policy,
19668cb08174SJohannes Berg 						     extack);
1967905cf0abSDavid Ahern 	}
1968905cf0abSDavid Ahern 
1969905cf0abSDavid Ahern 	/* A hack to preserve kernel<->userspace interface.
1970905cf0abSDavid Ahern 	 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1971905cf0abSDavid Ahern 	 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1972905cf0abSDavid Ahern 	 * what iproute2 < v3.9.0 used.
1973905cf0abSDavid Ahern 	 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1974905cf0abSDavid Ahern 	 * attribute, its netlink message is shorter than struct ifinfomsg.
1975905cf0abSDavid Ahern 	 */
1976905cf0abSDavid Ahern 	hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
1977905cf0abSDavid Ahern 		 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1978905cf0abSDavid Ahern 
19798cb08174SJohannes Berg 	return nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy,
19808cb08174SJohannes Berg 				      extack);
1981905cf0abSDavid Ahern }
1982905cf0abSDavid Ahern 
1983b60c5115SThomas Graf static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
19841da177e4SLinus Torvalds {
1985905cf0abSDavid Ahern 	struct netlink_ext_ack *extack = cb->extack;
1986905cf0abSDavid Ahern 	const struct nlmsghdr *nlh = cb->nlh;
19873b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
198879e1ad14SJiri Benc 	struct net *tgt_net = net;
19897c28bd0bSEric Dumazet 	int h, s_h;
19907c28bd0bSEric Dumazet 	int idx = 0, s_idx;
19911da177e4SLinus Torvalds 	struct net_device *dev;
19927c28bd0bSEric Dumazet 	struct hlist_head *head;
1993115c9b81SGreg Rose 	struct nlattr *tb[IFLA_MAX+1];
1994115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
1995dc599f76SDavid Ahern 	const struct rtnl_link_ops *kind_ops = NULL;
1996dc599f76SDavid Ahern 	unsigned int flags = NLM_F_MULTI;
1997dc599f76SDavid Ahern 	int master_idx = 0;
199879e1ad14SJiri Benc 	int netnsid = -1;
1999905cf0abSDavid Ahern 	int err, i;
20001da177e4SLinus Torvalds 
20017c28bd0bSEric Dumazet 	s_h = cb->args[0];
20027c28bd0bSEric Dumazet 	s_idx = cb->args[1];
20037c28bd0bSEric Dumazet 
2004905cf0abSDavid Ahern 	err = rtnl_valid_dump_ifinfo_req(nlh, cb->strict_check, tb, extack);
2005905cf0abSDavid Ahern 	if (err < 0) {
2006905cf0abSDavid Ahern 		if (cb->strict_check)
2007905cf0abSDavid Ahern 			return err;
2008e5eca6d4SMichal Schmidt 
2009905cf0abSDavid Ahern 		goto walk_entries;
201079e1ad14SJiri Benc 	}
201179e1ad14SJiri Benc 
2012905cf0abSDavid Ahern 	for (i = 0; i <= IFLA_MAX; ++i) {
2013905cf0abSDavid Ahern 		if (!tb[i])
2014905cf0abSDavid Ahern 			continue;
2015dc599f76SDavid Ahern 
2016905cf0abSDavid Ahern 		/* new attributes should only be added with strict checking */
2017905cf0abSDavid Ahern 		switch (i) {
2018905cf0abSDavid Ahern 		case IFLA_TARGET_NETNSID:
2019905cf0abSDavid Ahern 			netnsid = nla_get_s32(tb[i]);
2020905cf0abSDavid Ahern 			tgt_net = rtnl_get_net_ns_capable(skb->sk, netnsid);
2021905cf0abSDavid Ahern 			if (IS_ERR(tgt_net)) {
2022905cf0abSDavid Ahern 				NL_SET_ERR_MSG(extack, "Invalid target network namespace id");
2023905cf0abSDavid Ahern 				return PTR_ERR(tgt_net);
2024905cf0abSDavid Ahern 			}
2025905cf0abSDavid Ahern 			break;
2026905cf0abSDavid Ahern 		case IFLA_EXT_MASK:
2027905cf0abSDavid Ahern 			ext_filter_mask = nla_get_u32(tb[i]);
2028905cf0abSDavid Ahern 			break;
2029905cf0abSDavid Ahern 		case IFLA_MASTER:
2030905cf0abSDavid Ahern 			master_idx = nla_get_u32(tb[i]);
2031905cf0abSDavid Ahern 			break;
2032905cf0abSDavid Ahern 		case IFLA_LINKINFO:
2033905cf0abSDavid Ahern 			kind_ops = linkinfo_to_kind_ops(tb[i]);
2034905cf0abSDavid Ahern 			break;
2035905cf0abSDavid Ahern 		default:
2036905cf0abSDavid Ahern 			if (cb->strict_check) {
2037905cf0abSDavid Ahern 				NL_SET_ERR_MSG(extack, "Unsupported attribute in link dump request");
2038905cf0abSDavid Ahern 				return -EINVAL;
2039905cf0abSDavid Ahern 			}
2040905cf0abSDavid Ahern 		}
2041905cf0abSDavid Ahern 	}
2042dc599f76SDavid Ahern 
2043dc599f76SDavid Ahern 	if (master_idx || kind_ops)
2044dc599f76SDavid Ahern 		flags |= NLM_F_DUMP_FILTERED;
2045115c9b81SGreg Rose 
2046905cf0abSDavid Ahern walk_entries:
20477c28bd0bSEric Dumazet 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
20487562f876SPavel Emelianov 		idx = 0;
204979e1ad14SJiri Benc 		head = &tgt_net->dev_index_head[h];
2050cac5e65eSEric Dumazet 		hlist_for_each_entry(dev, head, index_hlist) {
2051dc599f76SDavid Ahern 			if (link_dump_filtered(dev, master_idx, kind_ops))
20523f0ae05dSZhang Shengju 				goto cont;
20531da177e4SLinus Torvalds 			if (idx < s_idx)
20547562f876SPavel Emelianov 				goto cont;
205579e1ad14SJiri Benc 			err = rtnl_fill_ifinfo(skb, dev, net,
205679e1ad14SJiri Benc 					       RTM_NEWLINK,
205715e47304SEric W. Biederman 					       NETLINK_CB(cb->skb).portid,
2058905cf0abSDavid Ahern 					       nlh->nlmsg_seq, 0, flags,
205938e01b30SNicolas Dichtel 					       ext_filter_mask, 0, NULL, 0,
206079e1ad14SJiri Benc 					       netnsid);
2061973462bbSDavid Gibson 
2062f6c5775fSDavid Ahern 			if (err < 0) {
2063f6c5775fSDavid Ahern 				if (likely(skb->len))
20647c28bd0bSEric Dumazet 					goto out;
20654e985adaSThomas Graf 
2066f6c5775fSDavid Ahern 				goto out_err;
2067f6c5775fSDavid Ahern 			}
20687562f876SPavel Emelianov cont:
20697562f876SPavel Emelianov 			idx++;
20701da177e4SLinus Torvalds 		}
20717c28bd0bSEric Dumazet 	}
20727c28bd0bSEric Dumazet out:
2073f6c5775fSDavid Ahern 	err = skb->len;
2074f6c5775fSDavid Ahern out_err:
20757c28bd0bSEric Dumazet 	cb->args[1] = idx;
20767c28bd0bSEric Dumazet 	cb->args[0] = h;
2077d0225784SJakub Sitnicki 	cb->seq = net->dev_base_seq;
2078d0225784SJakub Sitnicki 	nl_dump_check_consistent(cb, nlmsg_hdr(skb));
207979e1ad14SJiri Benc 	if (netnsid >= 0)
208079e1ad14SJiri Benc 		put_net(tgt_net);
20811da177e4SLinus Torvalds 
2082f6c5775fSDavid Ahern 	return err;
20831da177e4SLinus Torvalds }
20841da177e4SLinus Torvalds 
2085fceb6435SJohannes Berg int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len,
2086fceb6435SJohannes Berg 			struct netlink_ext_ack *exterr)
2087f7b12606SJiri Pirko {
20888cb08174SJohannes Berg 	return nla_parse_deprecated(tb, IFLA_MAX, head, len, ifla_policy,
20898cb08174SJohannes Berg 				    exterr);
2090f7b12606SJiri Pirko }
2091f7b12606SJiri Pirko EXPORT_SYMBOL(rtnl_nla_parse_ifla);
209257b61080SScott Feldman 
209381adee47SEric W. Biederman struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
209481adee47SEric W. Biederman {
209581adee47SEric W. Biederman 	struct net *net;
209681adee47SEric W. Biederman 	/* Examine the link attributes and figure out which
209781adee47SEric W. Biederman 	 * network namespace we are talking about.
209881adee47SEric W. Biederman 	 */
209981adee47SEric W. Biederman 	if (tb[IFLA_NET_NS_PID])
210081adee47SEric W. Biederman 		net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
2101f0630529SEric W. Biederman 	else if (tb[IFLA_NET_NS_FD])
2102f0630529SEric W. Biederman 		net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
210381adee47SEric W. Biederman 	else
210481adee47SEric W. Biederman 		net = get_net(src_net);
210581adee47SEric W. Biederman 	return net;
210681adee47SEric W. Biederman }
210781adee47SEric W. Biederman EXPORT_SYMBOL(rtnl_link_get_net);
210881adee47SEric W. Biederman 
21097c4f63baSChristian Brauner /* Figure out which network namespace we are talking about by
21107c4f63baSChristian Brauner  * examining the link attributes in the following order:
21117c4f63baSChristian Brauner  *
21127c4f63baSChristian Brauner  * 1. IFLA_NET_NS_PID
21137c4f63baSChristian Brauner  * 2. IFLA_NET_NS_FD
21147e4a8d5aSChristian Brauner  * 3. IFLA_TARGET_NETNSID
21157c4f63baSChristian Brauner  */
21167c4f63baSChristian Brauner static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net,
21177c4f63baSChristian Brauner 					       struct nlattr *tb[])
21187c4f63baSChristian Brauner {
21197c4f63baSChristian Brauner 	struct net *net;
21207c4f63baSChristian Brauner 
21217c4f63baSChristian Brauner 	if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])
21227c4f63baSChristian Brauner 		return rtnl_link_get_net(src_net, tb);
21237c4f63baSChristian Brauner 
21247e4a8d5aSChristian Brauner 	if (!tb[IFLA_TARGET_NETNSID])
21257c4f63baSChristian Brauner 		return get_net(src_net);
21267c4f63baSChristian Brauner 
21277e4a8d5aSChristian Brauner 	net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_TARGET_NETNSID]));
21287c4f63baSChristian Brauner 	if (!net)
21297c4f63baSChristian Brauner 		return ERR_PTR(-EINVAL);
21307c4f63baSChristian Brauner 
21317c4f63baSChristian Brauner 	return net;
21327c4f63baSChristian Brauner }
21337c4f63baSChristian Brauner 
21347c4f63baSChristian Brauner static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb,
21357c4f63baSChristian Brauner 					     struct net *src_net,
21367c4f63baSChristian Brauner 					     struct nlattr *tb[], int cap)
21377c4f63baSChristian Brauner {
21387c4f63baSChristian Brauner 	struct net *net;
21397c4f63baSChristian Brauner 
21407c4f63baSChristian Brauner 	net = rtnl_link_get_net_by_nlattr(src_net, tb);
21417c4f63baSChristian Brauner 	if (IS_ERR(net))
21427c4f63baSChristian Brauner 		return net;
21437c4f63baSChristian Brauner 
21447c4f63baSChristian Brauner 	if (!netlink_ns_capable(skb, net->user_ns, cap)) {
21457c4f63baSChristian Brauner 		put_net(net);
21467c4f63baSChristian Brauner 		return ERR_PTR(-EPERM);
21477c4f63baSChristian Brauner 	}
21487c4f63baSChristian Brauner 
21497c4f63baSChristian Brauner 	return net;
21507c4f63baSChristian Brauner }
21517c4f63baSChristian Brauner 
21524ff66caeSChristian Brauner /* Verify that rtnetlink requests do not pass additional properties
21534ff66caeSChristian Brauner  * potentially referring to different network namespaces.
21544ff66caeSChristian Brauner  */
21554ff66caeSChristian Brauner static int rtnl_ensure_unique_netns(struct nlattr *tb[],
21564ff66caeSChristian Brauner 				    struct netlink_ext_ack *extack,
21574ff66caeSChristian Brauner 				    bool netns_id_only)
21584ff66caeSChristian Brauner {
21594ff66caeSChristian Brauner 
21604ff66caeSChristian Brauner 	if (netns_id_only) {
21614ff66caeSChristian Brauner 		if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD])
21624ff66caeSChristian Brauner 			return 0;
21634ff66caeSChristian Brauner 
21644ff66caeSChristian Brauner 		NL_SET_ERR_MSG(extack, "specified netns attribute not supported");
21654ff66caeSChristian Brauner 		return -EOPNOTSUPP;
21664ff66caeSChristian Brauner 	}
21674ff66caeSChristian Brauner 
21687e4a8d5aSChristian Brauner 	if (tb[IFLA_TARGET_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]))
21694ff66caeSChristian Brauner 		goto invalid_attr;
21704ff66caeSChristian Brauner 
21717e4a8d5aSChristian Brauner 	if (tb[IFLA_NET_NS_PID] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_FD]))
21724ff66caeSChristian Brauner 		goto invalid_attr;
21734ff66caeSChristian Brauner 
21747e4a8d5aSChristian Brauner 	if (tb[IFLA_NET_NS_FD] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_PID]))
21754ff66caeSChristian Brauner 		goto invalid_attr;
21764ff66caeSChristian Brauner 
21774ff66caeSChristian Brauner 	return 0;
21784ff66caeSChristian Brauner 
21794ff66caeSChristian Brauner invalid_attr:
21804ff66caeSChristian Brauner 	NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified");
21814ff66caeSChristian Brauner 	return -EINVAL;
21824ff66caeSChristian Brauner }
21834ff66caeSChristian Brauner 
21841840bb13SThomas Graf static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
21851840bb13SThomas Graf {
21861840bb13SThomas Graf 	if (dev) {
21871840bb13SThomas Graf 		if (tb[IFLA_ADDRESS] &&
21881840bb13SThomas Graf 		    nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
21891840bb13SThomas Graf 			return -EINVAL;
21901840bb13SThomas Graf 
21911840bb13SThomas Graf 		if (tb[IFLA_BROADCAST] &&
21921840bb13SThomas Graf 		    nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
21931840bb13SThomas Graf 			return -EINVAL;
21941840bb13SThomas Graf 	}
21951840bb13SThomas Graf 
2196cf7afbfeSThomas Graf 	if (tb[IFLA_AF_SPEC]) {
2197cf7afbfeSThomas Graf 		struct nlattr *af;
2198cf7afbfeSThomas Graf 		int rem, err;
2199cf7afbfeSThomas Graf 
2200cf7afbfeSThomas Graf 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2201cf7afbfeSThomas Graf 			const struct rtnl_af_ops *af_ops;
2202cf7afbfeSThomas Graf 
22035fa85a09SFlorian Westphal 			rcu_read_lock();
22045fa85a09SFlorian Westphal 			af_ops = rtnl_af_lookup(nla_type(af));
22055fa85a09SFlorian Westphal 			if (!af_ops) {
22065fa85a09SFlorian Westphal 				rcu_read_unlock();
2207cf7afbfeSThomas Graf 				return -EAFNOSUPPORT;
22085fa85a09SFlorian Westphal 			}
2209cf7afbfeSThomas Graf 
22105fa85a09SFlorian Westphal 			if (!af_ops->set_link_af) {
22115fa85a09SFlorian Westphal 				rcu_read_unlock();
2212cf7afbfeSThomas Graf 				return -EOPNOTSUPP;
22135fa85a09SFlorian Westphal 			}
2214cf7afbfeSThomas Graf 
2215cf7afbfeSThomas Graf 			if (af_ops->validate_link_af) {
22166d3a9a68SKurt Van Dijck 				err = af_ops->validate_link_af(dev, af);
22175fa85a09SFlorian Westphal 				if (err < 0) {
22185fa85a09SFlorian Westphal 					rcu_read_unlock();
2219cf7afbfeSThomas Graf 					return err;
2220cf7afbfeSThomas Graf 				}
2221cf7afbfeSThomas Graf 			}
22225fa85a09SFlorian Westphal 
22235fa85a09SFlorian Westphal 			rcu_read_unlock();
22245fa85a09SFlorian Westphal 		}
2225cf7afbfeSThomas Graf 	}
2226cf7afbfeSThomas Graf 
22271840bb13SThomas Graf 	return 0;
22281840bb13SThomas Graf }
22291840bb13SThomas Graf 
2230cc8e27ccSEli Cohen static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
2231cc8e27ccSEli Cohen 				  int guid_type)
2232cc8e27ccSEli Cohen {
2233cc8e27ccSEli Cohen 	const struct net_device_ops *ops = dev->netdev_ops;
2234cc8e27ccSEli Cohen 
2235cc8e27ccSEli Cohen 	return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
2236cc8e27ccSEli Cohen }
2237cc8e27ccSEli Cohen 
2238cc8e27ccSEli Cohen static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
2239cc8e27ccSEli Cohen {
2240cc8e27ccSEli Cohen 	if (dev->type != ARPHRD_INFINIBAND)
2241cc8e27ccSEli Cohen 		return -EOPNOTSUPP;
2242cc8e27ccSEli Cohen 
2243cc8e27ccSEli Cohen 	return handle_infiniband_guid(dev, ivt, guid_type);
2244cc8e27ccSEli Cohen }
2245cc8e27ccSEli Cohen 
22464f7d2cdfSDaniel Borkmann static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
2247c02db8c6SChris Wright {
2248c02db8c6SChris Wright 	const struct net_device_ops *ops = dev->netdev_ops;
22494f7d2cdfSDaniel Borkmann 	int err = -EINVAL;
2250c02db8c6SChris Wright 
22514f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_MAC]) {
22524f7d2cdfSDaniel Borkmann 		struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
22534f7d2cdfSDaniel Borkmann 
2254c02db8c6SChris Wright 		err = -EOPNOTSUPP;
2255c02db8c6SChris Wright 		if (ops->ndo_set_vf_mac)
2256c02db8c6SChris Wright 			err = ops->ndo_set_vf_mac(dev, ivm->vf,
2257c02db8c6SChris Wright 						  ivm->mac);
22584f7d2cdfSDaniel Borkmann 		if (err < 0)
22594f7d2cdfSDaniel Borkmann 			return err;
2260c02db8c6SChris Wright 	}
22614f7d2cdfSDaniel Borkmann 
22624f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_VLAN]) {
22634f7d2cdfSDaniel Borkmann 		struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
22644f7d2cdfSDaniel Borkmann 
2265c02db8c6SChris Wright 		err = -EOPNOTSUPP;
2266c02db8c6SChris Wright 		if (ops->ndo_set_vf_vlan)
22674f7d2cdfSDaniel Borkmann 			err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
226879aab093SMoshe Shemesh 						   ivv->qos,
226979aab093SMoshe Shemesh 						   htons(ETH_P_8021Q));
227079aab093SMoshe Shemesh 		if (err < 0)
227179aab093SMoshe Shemesh 			return err;
227279aab093SMoshe Shemesh 	}
227379aab093SMoshe Shemesh 
227479aab093SMoshe Shemesh 	if (tb[IFLA_VF_VLAN_LIST]) {
227579aab093SMoshe Shemesh 		struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
227679aab093SMoshe Shemesh 		struct nlattr *attr;
227779aab093SMoshe Shemesh 		int rem, len = 0;
227879aab093SMoshe Shemesh 
227979aab093SMoshe Shemesh 		err = -EOPNOTSUPP;
228079aab093SMoshe Shemesh 		if (!ops->ndo_set_vf_vlan)
228179aab093SMoshe Shemesh 			return err;
228279aab093SMoshe Shemesh 
228379aab093SMoshe Shemesh 		nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
228479aab093SMoshe Shemesh 			if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
228579aab093SMoshe Shemesh 			    nla_len(attr) < NLA_HDRLEN) {
228679aab093SMoshe Shemesh 				return -EINVAL;
228779aab093SMoshe Shemesh 			}
228879aab093SMoshe Shemesh 			if (len >= MAX_VLAN_LIST_LEN)
228979aab093SMoshe Shemesh 				return -EOPNOTSUPP;
229079aab093SMoshe Shemesh 			ivvl[len] = nla_data(attr);
229179aab093SMoshe Shemesh 
229279aab093SMoshe Shemesh 			len++;
229379aab093SMoshe Shemesh 		}
2294fa34cd94SArnd Bergmann 		if (len == 0)
2295fa34cd94SArnd Bergmann 			return -EINVAL;
2296fa34cd94SArnd Bergmann 
229779aab093SMoshe Shemesh 		err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
229879aab093SMoshe Shemesh 					   ivvl[0]->qos, ivvl[0]->vlan_proto);
22994f7d2cdfSDaniel Borkmann 		if (err < 0)
23004f7d2cdfSDaniel Borkmann 			return err;
2301c02db8c6SChris Wright 	}
23024f7d2cdfSDaniel Borkmann 
23034f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_TX_RATE]) {
23044f7d2cdfSDaniel Borkmann 		struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
2305ed616689SSucheta Chakraborty 		struct ifla_vf_info ivf;
23064f7d2cdfSDaniel Borkmann 
2307c02db8c6SChris Wright 		err = -EOPNOTSUPP;
2308ed616689SSucheta Chakraborty 		if (ops->ndo_get_vf_config)
23094f7d2cdfSDaniel Borkmann 			err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
23104f7d2cdfSDaniel Borkmann 		if (err < 0)
23114f7d2cdfSDaniel Borkmann 			return err;
23124f7d2cdfSDaniel Borkmann 
2313ed616689SSucheta Chakraborty 		err = -EOPNOTSUPP;
2314ed616689SSucheta Chakraborty 		if (ops->ndo_set_vf_rate)
2315ed616689SSucheta Chakraborty 			err = ops->ndo_set_vf_rate(dev, ivt->vf,
2316ed616689SSucheta Chakraborty 						   ivf.min_tx_rate,
2317c02db8c6SChris Wright 						   ivt->rate);
23184f7d2cdfSDaniel Borkmann 		if (err < 0)
23194f7d2cdfSDaniel Borkmann 			return err;
2320c02db8c6SChris Wright 	}
23214f7d2cdfSDaniel Borkmann 
23224f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_RATE]) {
23234f7d2cdfSDaniel Borkmann 		struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
23244f7d2cdfSDaniel Borkmann 
2325ed616689SSucheta Chakraborty 		err = -EOPNOTSUPP;
2326ed616689SSucheta Chakraborty 		if (ops->ndo_set_vf_rate)
2327ed616689SSucheta Chakraborty 			err = ops->ndo_set_vf_rate(dev, ivt->vf,
2328ed616689SSucheta Chakraborty 						   ivt->min_tx_rate,
2329ed616689SSucheta Chakraborty 						   ivt->max_tx_rate);
23304f7d2cdfSDaniel Borkmann 		if (err < 0)
23314f7d2cdfSDaniel Borkmann 			return err;
2332ed616689SSucheta Chakraborty 	}
23334f7d2cdfSDaniel Borkmann 
23344f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_SPOOFCHK]) {
23354f7d2cdfSDaniel Borkmann 		struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
23364f7d2cdfSDaniel Borkmann 
23375f8444a3SGreg Rose 		err = -EOPNOTSUPP;
23385f8444a3SGreg Rose 		if (ops->ndo_set_vf_spoofchk)
23395f8444a3SGreg Rose 			err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
23405f8444a3SGreg Rose 						       ivs->setting);
23414f7d2cdfSDaniel Borkmann 		if (err < 0)
23424f7d2cdfSDaniel Borkmann 			return err;
23435f8444a3SGreg Rose 	}
23444f7d2cdfSDaniel Borkmann 
23454f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_LINK_STATE]) {
23464f7d2cdfSDaniel Borkmann 		struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
23474f7d2cdfSDaniel Borkmann 
23481d8faf48SRony Efraim 		err = -EOPNOTSUPP;
23491d8faf48SRony Efraim 		if (ops->ndo_set_vf_link_state)
23501d8faf48SRony Efraim 			err = ops->ndo_set_vf_link_state(dev, ivl->vf,
23511d8faf48SRony Efraim 							 ivl->link_state);
23524f7d2cdfSDaniel Borkmann 		if (err < 0)
23534f7d2cdfSDaniel Borkmann 			return err;
23541d8faf48SRony Efraim 	}
23554f7d2cdfSDaniel Borkmann 
23564f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_RSS_QUERY_EN]) {
235701a3d796SVlad Zolotarov 		struct ifla_vf_rss_query_en *ivrssq_en;
235801a3d796SVlad Zolotarov 
235901a3d796SVlad Zolotarov 		err = -EOPNOTSUPP;
23604f7d2cdfSDaniel Borkmann 		ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
236101a3d796SVlad Zolotarov 		if (ops->ndo_set_vf_rss_query_en)
23624f7d2cdfSDaniel Borkmann 			err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
236301a3d796SVlad Zolotarov 							   ivrssq_en->setting);
23644f7d2cdfSDaniel Borkmann 		if (err < 0)
23654f7d2cdfSDaniel Borkmann 			return err;
236601a3d796SVlad Zolotarov 	}
23674f7d2cdfSDaniel Borkmann 
2368dd461d6aSHiroshi Shimamoto 	if (tb[IFLA_VF_TRUST]) {
2369dd461d6aSHiroshi Shimamoto 		struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
2370dd461d6aSHiroshi Shimamoto 
2371dd461d6aSHiroshi Shimamoto 		err = -EOPNOTSUPP;
2372dd461d6aSHiroshi Shimamoto 		if (ops->ndo_set_vf_trust)
2373dd461d6aSHiroshi Shimamoto 			err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
2374dd461d6aSHiroshi Shimamoto 		if (err < 0)
2375dd461d6aSHiroshi Shimamoto 			return err;
2376dd461d6aSHiroshi Shimamoto 	}
2377dd461d6aSHiroshi Shimamoto 
2378cc8e27ccSEli Cohen 	if (tb[IFLA_VF_IB_NODE_GUID]) {
2379cc8e27ccSEli Cohen 		struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
2380cc8e27ccSEli Cohen 
2381cc8e27ccSEli Cohen 		if (!ops->ndo_set_vf_guid)
2382cc8e27ccSEli Cohen 			return -EOPNOTSUPP;
2383cc8e27ccSEli Cohen 
2384cc8e27ccSEli Cohen 		return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
2385cc8e27ccSEli Cohen 	}
2386cc8e27ccSEli Cohen 
2387cc8e27ccSEli Cohen 	if (tb[IFLA_VF_IB_PORT_GUID]) {
2388cc8e27ccSEli Cohen 		struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
2389cc8e27ccSEli Cohen 
2390cc8e27ccSEli Cohen 		if (!ops->ndo_set_vf_guid)
2391cc8e27ccSEli Cohen 			return -EOPNOTSUPP;
2392cc8e27ccSEli Cohen 
2393cc8e27ccSEli Cohen 		return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
2394cc8e27ccSEli Cohen 	}
2395cc8e27ccSEli Cohen 
2396c02db8c6SChris Wright 	return err;
2397c02db8c6SChris Wright }
2398c02db8c6SChris Wright 
239933eaf2a6SDavid Ahern static int do_set_master(struct net_device *dev, int ifindex,
240033eaf2a6SDavid Ahern 			 struct netlink_ext_ack *extack)
2401fbaec0eaSJiri Pirko {
2402898e5061SJiri Pirko 	struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
2403fbaec0eaSJiri Pirko 	const struct net_device_ops *ops;
2404fbaec0eaSJiri Pirko 	int err;
2405fbaec0eaSJiri Pirko 
2406898e5061SJiri Pirko 	if (upper_dev) {
2407898e5061SJiri Pirko 		if (upper_dev->ifindex == ifindex)
2408fbaec0eaSJiri Pirko 			return 0;
2409898e5061SJiri Pirko 		ops = upper_dev->netdev_ops;
2410fbaec0eaSJiri Pirko 		if (ops->ndo_del_slave) {
2411898e5061SJiri Pirko 			err = ops->ndo_del_slave(upper_dev, dev);
2412fbaec0eaSJiri Pirko 			if (err)
2413fbaec0eaSJiri Pirko 				return err;
2414fbaec0eaSJiri Pirko 		} else {
2415fbaec0eaSJiri Pirko 			return -EOPNOTSUPP;
2416fbaec0eaSJiri Pirko 		}
2417fbaec0eaSJiri Pirko 	}
2418fbaec0eaSJiri Pirko 
2419fbaec0eaSJiri Pirko 	if (ifindex) {
2420898e5061SJiri Pirko 		upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
2421898e5061SJiri Pirko 		if (!upper_dev)
2422fbaec0eaSJiri Pirko 			return -EINVAL;
2423898e5061SJiri Pirko 		ops = upper_dev->netdev_ops;
2424fbaec0eaSJiri Pirko 		if (ops->ndo_add_slave) {
242533eaf2a6SDavid Ahern 			err = ops->ndo_add_slave(upper_dev, dev, extack);
2426fbaec0eaSJiri Pirko 			if (err)
2427fbaec0eaSJiri Pirko 				return err;
2428fbaec0eaSJiri Pirko 		} else {
2429fbaec0eaSJiri Pirko 			return -EOPNOTSUPP;
2430fbaec0eaSJiri Pirko 		}
2431fbaec0eaSJiri Pirko 	}
2432fbaec0eaSJiri Pirko 	return 0;
2433fbaec0eaSJiri Pirko }
2434fbaec0eaSJiri Pirko 
243590c325e3SNicolas Dichtel #define DO_SETLINK_MODIFIED	0x01
2436ba998906SNicolas Dichtel /* notify flag means notify + modified. */
2437ba998906SNicolas Dichtel #define DO_SETLINK_NOTIFY	0x03
243890f62cf3SEric W. Biederman static int do_setlink(const struct sk_buff *skb,
243990f62cf3SEric W. Biederman 		      struct net_device *dev, struct ifinfomsg *ifm,
2440ddf9f970SJakub Kicinski 		      struct netlink_ext_ack *extack,
244190c325e3SNicolas Dichtel 		      struct nlattr **tb, char *ifname, int status)
24420157f60cSPatrick McHardy {
2443d314774cSStephen Hemminger 	const struct net_device_ops *ops = dev->netdev_ops;
24440157f60cSPatrick McHardy 	int err;
24450157f60cSPatrick McHardy 
2446644c7eebSEric Dumazet 	err = validate_linkmsg(dev, tb);
2447644c7eebSEric Dumazet 	if (err < 0)
2448644c7eebSEric Dumazet 		return err;
2449644c7eebSEric Dumazet 
24507e4a8d5aSChristian Brauner 	if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_TARGET_NETNSID]) {
24517c4f63baSChristian Brauner 		struct net *net = rtnl_link_get_net_capable(skb, dev_net(dev),
24527c4f63baSChristian Brauner 							    tb, CAP_NET_ADMIN);
2453d8a5ec67SEric W. Biederman 		if (IS_ERR(net)) {
2454d8a5ec67SEric W. Biederman 			err = PTR_ERR(net);
2455d8a5ec67SEric W. Biederman 			goto errout;
2456d8a5ec67SEric W. Biederman 		}
24577c4f63baSChristian Brauner 
2458d8a5ec67SEric W. Biederman 		err = dev_change_net_namespace(dev, net, ifname);
2459d8a5ec67SEric W. Biederman 		put_net(net);
2460d8a5ec67SEric W. Biederman 		if (err)
2461d8a5ec67SEric W. Biederman 			goto errout;
246290c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
2463d8a5ec67SEric W. Biederman 	}
2464d8a5ec67SEric W. Biederman 
24650157f60cSPatrick McHardy 	if (tb[IFLA_MAP]) {
24660157f60cSPatrick McHardy 		struct rtnl_link_ifmap *u_map;
24670157f60cSPatrick McHardy 		struct ifmap k_map;
24680157f60cSPatrick McHardy 
2469d314774cSStephen Hemminger 		if (!ops->ndo_set_config) {
24700157f60cSPatrick McHardy 			err = -EOPNOTSUPP;
24710157f60cSPatrick McHardy 			goto errout;
24720157f60cSPatrick McHardy 		}
24730157f60cSPatrick McHardy 
24740157f60cSPatrick McHardy 		if (!netif_device_present(dev)) {
24750157f60cSPatrick McHardy 			err = -ENODEV;
24760157f60cSPatrick McHardy 			goto errout;
24770157f60cSPatrick McHardy 		}
24780157f60cSPatrick McHardy 
24790157f60cSPatrick McHardy 		u_map = nla_data(tb[IFLA_MAP]);
24800157f60cSPatrick McHardy 		k_map.mem_start = (unsigned long) u_map->mem_start;
24810157f60cSPatrick McHardy 		k_map.mem_end = (unsigned long) u_map->mem_end;
24820157f60cSPatrick McHardy 		k_map.base_addr = (unsigned short) u_map->base_addr;
24830157f60cSPatrick McHardy 		k_map.irq = (unsigned char) u_map->irq;
24840157f60cSPatrick McHardy 		k_map.dma = (unsigned char) u_map->dma;
24850157f60cSPatrick McHardy 		k_map.port = (unsigned char) u_map->port;
24860157f60cSPatrick McHardy 
2487d314774cSStephen Hemminger 		err = ops->ndo_set_config(dev, &k_map);
24880157f60cSPatrick McHardy 		if (err < 0)
24890157f60cSPatrick McHardy 			goto errout;
24900157f60cSPatrick McHardy 
2491ba998906SNicolas Dichtel 		status |= DO_SETLINK_NOTIFY;
24920157f60cSPatrick McHardy 	}
24930157f60cSPatrick McHardy 
24940157f60cSPatrick McHardy 	if (tb[IFLA_ADDRESS]) {
24950157f60cSPatrick McHardy 		struct sockaddr *sa;
24960157f60cSPatrick McHardy 		int len;
24970157f60cSPatrick McHardy 
2498153711f9SWANG Cong 		len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
2499153711f9SWANG Cong 						  sizeof(*sa));
25000157f60cSPatrick McHardy 		sa = kmalloc(len, GFP_KERNEL);
25010157f60cSPatrick McHardy 		if (!sa) {
25020157f60cSPatrick McHardy 			err = -ENOMEM;
25030157f60cSPatrick McHardy 			goto errout;
25040157f60cSPatrick McHardy 		}
25050157f60cSPatrick McHardy 		sa->sa_family = dev->type;
25060157f60cSPatrick McHardy 		memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
25070157f60cSPatrick McHardy 		       dev->addr_len);
25083a37a963SPetr Machata 		err = dev_set_mac_address(dev, sa, extack);
25090157f60cSPatrick McHardy 		kfree(sa);
25100157f60cSPatrick McHardy 		if (err)
25110157f60cSPatrick McHardy 			goto errout;
251290c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
25130157f60cSPatrick McHardy 	}
25140157f60cSPatrick McHardy 
25150157f60cSPatrick McHardy 	if (tb[IFLA_MTU]) {
25167a4c53beSStephen Hemminger 		err = dev_set_mtu_ext(dev, nla_get_u32(tb[IFLA_MTU]), extack);
25170157f60cSPatrick McHardy 		if (err < 0)
25180157f60cSPatrick McHardy 			goto errout;
251990c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
25200157f60cSPatrick McHardy 	}
25210157f60cSPatrick McHardy 
2522cbda10faSVlad Dogaru 	if (tb[IFLA_GROUP]) {
2523cbda10faSVlad Dogaru 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
2524ba998906SNicolas Dichtel 		status |= DO_SETLINK_NOTIFY;
2525cbda10faSVlad Dogaru 	}
2526cbda10faSVlad Dogaru 
25270157f60cSPatrick McHardy 	/*
25280157f60cSPatrick McHardy 	 * Interface selected by interface index but interface
25290157f60cSPatrick McHardy 	 * name provided implies that a name change has been
25300157f60cSPatrick McHardy 	 * requested.
25310157f60cSPatrick McHardy 	 */
25320157f60cSPatrick McHardy 	if (ifm->ifi_index > 0 && ifname[0]) {
25330157f60cSPatrick McHardy 		err = dev_change_name(dev, ifname);
25340157f60cSPatrick McHardy 		if (err < 0)
25350157f60cSPatrick McHardy 			goto errout;
253690c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
25370157f60cSPatrick McHardy 	}
25380157f60cSPatrick McHardy 
25390b815a1aSStephen Hemminger 	if (tb[IFLA_IFALIAS]) {
25400b815a1aSStephen Hemminger 		err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
25410b815a1aSStephen Hemminger 				    nla_len(tb[IFLA_IFALIAS]));
25420b815a1aSStephen Hemminger 		if (err < 0)
25430b815a1aSStephen Hemminger 			goto errout;
2544ba998906SNicolas Dichtel 		status |= DO_SETLINK_NOTIFY;
25450b815a1aSStephen Hemminger 	}
25460b815a1aSStephen Hemminger 
25470157f60cSPatrick McHardy 	if (tb[IFLA_BROADCAST]) {
25480157f60cSPatrick McHardy 		nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
2549e7c3273eSJiri Pirko 		call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
25500157f60cSPatrick McHardy 	}
25510157f60cSPatrick McHardy 
25520157f60cSPatrick McHardy 	if (ifm->ifi_flags || ifm->ifi_change) {
2553567c5e13SPetr Machata 		err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
2554567c5e13SPetr Machata 				       extack);
25555f9021cfSJohannes Berg 		if (err < 0)
25565f9021cfSJohannes Berg 			goto errout;
25570157f60cSPatrick McHardy 	}
25580157f60cSPatrick McHardy 
2559fbaec0eaSJiri Pirko 	if (tb[IFLA_MASTER]) {
256033eaf2a6SDavid Ahern 		err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
2561fbaec0eaSJiri Pirko 		if (err)
2562fbaec0eaSJiri Pirko 			goto errout;
256390c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
2564fbaec0eaSJiri Pirko 	}
2565fbaec0eaSJiri Pirko 
25669a57247fSJiri Pirko 	if (tb[IFLA_CARRIER]) {
25679a57247fSJiri Pirko 		err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
25689a57247fSJiri Pirko 		if (err)
25699a57247fSJiri Pirko 			goto errout;
257090c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
25719a57247fSJiri Pirko 	}
25729a57247fSJiri Pirko 
25735d1180fcSNicolas Dichtel 	if (tb[IFLA_TXQLEN]) {
25740cd29503SAlexey Dobriyan 		unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]);
25755d1180fcSNicolas Dichtel 
25766a643ddbSCong Wang 		err = dev_change_tx_queue_len(dev, value);
25776a643ddbSCong Wang 		if (err)
257808294a26SJason Wang 			goto errout;
25792d7f669bSXin Long 		status |= DO_SETLINK_MODIFIED;
258008294a26SJason Wang 	}
25810157f60cSPatrick McHardy 
258246e6b992SStephen Hemminger 	if (tb[IFLA_GSO_MAX_SIZE]) {
258346e6b992SStephen Hemminger 		u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]);
258446e6b992SStephen Hemminger 
258546e6b992SStephen Hemminger 		if (max_size > GSO_MAX_SIZE) {
258646e6b992SStephen Hemminger 			err = -EINVAL;
258746e6b992SStephen Hemminger 			goto errout;
258846e6b992SStephen Hemminger 		}
258946e6b992SStephen Hemminger 
259046e6b992SStephen Hemminger 		if (dev->gso_max_size ^ max_size) {
259146e6b992SStephen Hemminger 			netif_set_gso_max_size(dev, max_size);
259246e6b992SStephen Hemminger 			status |= DO_SETLINK_MODIFIED;
259346e6b992SStephen Hemminger 		}
259446e6b992SStephen Hemminger 	}
259546e6b992SStephen Hemminger 
259646e6b992SStephen Hemminger 	if (tb[IFLA_GSO_MAX_SEGS]) {
259746e6b992SStephen Hemminger 		u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
259846e6b992SStephen Hemminger 
259946e6b992SStephen Hemminger 		if (max_segs > GSO_MAX_SEGS) {
260046e6b992SStephen Hemminger 			err = -EINVAL;
260146e6b992SStephen Hemminger 			goto errout;
260246e6b992SStephen Hemminger 		}
260346e6b992SStephen Hemminger 
260446e6b992SStephen Hemminger 		if (dev->gso_max_segs ^ max_segs) {
260546e6b992SStephen Hemminger 			dev->gso_max_segs = max_segs;
260646e6b992SStephen Hemminger 			status |= DO_SETLINK_MODIFIED;
260746e6b992SStephen Hemminger 		}
260846e6b992SStephen Hemminger 	}
260946e6b992SStephen Hemminger 
26100157f60cSPatrick McHardy 	if (tb[IFLA_OPERSTATE])
261193b2d4a2SDavid S. Miller 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
26120157f60cSPatrick McHardy 
26130157f60cSPatrick McHardy 	if (tb[IFLA_LINKMODE]) {
26141889b0e7SNicolas Dichtel 		unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
26151889b0e7SNicolas Dichtel 
26160157f60cSPatrick McHardy 		write_lock_bh(&dev_base_lock);
26171889b0e7SNicolas Dichtel 		if (dev->link_mode ^ value)
2618ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
26191889b0e7SNicolas Dichtel 		dev->link_mode = value;
262093b2d4a2SDavid S. Miller 		write_unlock_bh(&dev_base_lock);
26210157f60cSPatrick McHardy 	}
26220157f60cSPatrick McHardy 
2623c02db8c6SChris Wright 	if (tb[IFLA_VFINFO_LIST]) {
26244f7d2cdfSDaniel Borkmann 		struct nlattr *vfinfo[IFLA_VF_MAX + 1];
2625c02db8c6SChris Wright 		struct nlattr *attr;
2626c02db8c6SChris Wright 		int rem;
26274f7d2cdfSDaniel Borkmann 
2628c02db8c6SChris Wright 		nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
26294f7d2cdfSDaniel Borkmann 			if (nla_type(attr) != IFLA_VF_INFO ||
26304f7d2cdfSDaniel Borkmann 			    nla_len(attr) < NLA_HDRLEN) {
2631253683bbSDavid Howells 				err = -EINVAL;
2632c02db8c6SChris Wright 				goto errout;
2633253683bbSDavid Howells 			}
26348cb08174SJohannes Berg 			err = nla_parse_nested_deprecated(vfinfo, IFLA_VF_MAX,
26358cb08174SJohannes Berg 							  attr,
26368cb08174SJohannes Berg 							  ifla_vf_policy,
26378cb08174SJohannes Berg 							  NULL);
26384f7d2cdfSDaniel Borkmann 			if (err < 0)
26394f7d2cdfSDaniel Borkmann 				goto errout;
26404f7d2cdfSDaniel Borkmann 			err = do_setvfinfo(dev, vfinfo);
2641ebc08a6fSWilliams, Mitch A 			if (err < 0)
2642ebc08a6fSWilliams, Mitch A 				goto errout;
2643ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
2644ebc08a6fSWilliams, Mitch A 		}
2645ebc08a6fSWilliams, Mitch A 	}
26460157f60cSPatrick McHardy 	err = 0;
26470157f60cSPatrick McHardy 
264857b61080SScott Feldman 	if (tb[IFLA_VF_PORTS]) {
264957b61080SScott Feldman 		struct nlattr *port[IFLA_PORT_MAX+1];
265057b61080SScott Feldman 		struct nlattr *attr;
265157b61080SScott Feldman 		int vf;
265257b61080SScott Feldman 		int rem;
265357b61080SScott Feldman 
265457b61080SScott Feldman 		err = -EOPNOTSUPP;
265557b61080SScott Feldman 		if (!ops->ndo_set_vf_port)
265657b61080SScott Feldman 			goto errout;
265757b61080SScott Feldman 
265857b61080SScott Feldman 		nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
2659035d210fSDaniel Borkmann 			if (nla_type(attr) != IFLA_VF_PORT ||
2660035d210fSDaniel Borkmann 			    nla_len(attr) < NLA_HDRLEN) {
2661035d210fSDaniel Borkmann 				err = -EINVAL;
2662035d210fSDaniel Borkmann 				goto errout;
2663035d210fSDaniel Borkmann 			}
26648cb08174SJohannes Berg 			err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
26658cb08174SJohannes Berg 							  attr,
26668cb08174SJohannes Berg 							  ifla_port_policy,
26678cb08174SJohannes Berg 							  NULL);
266857b61080SScott Feldman 			if (err < 0)
266957b61080SScott Feldman 				goto errout;
267057b61080SScott Feldman 			if (!port[IFLA_PORT_VF]) {
267157b61080SScott Feldman 				err = -EOPNOTSUPP;
267257b61080SScott Feldman 				goto errout;
267357b61080SScott Feldman 			}
267457b61080SScott Feldman 			vf = nla_get_u32(port[IFLA_PORT_VF]);
267557b61080SScott Feldman 			err = ops->ndo_set_vf_port(dev, vf, port);
267657b61080SScott Feldman 			if (err < 0)
267757b61080SScott Feldman 				goto errout;
2678ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
267957b61080SScott Feldman 		}
268057b61080SScott Feldman 	}
268157b61080SScott Feldman 	err = 0;
268257b61080SScott Feldman 
268357b61080SScott Feldman 	if (tb[IFLA_PORT_SELF]) {
268457b61080SScott Feldman 		struct nlattr *port[IFLA_PORT_MAX+1];
268557b61080SScott Feldman 
26868cb08174SJohannes Berg 		err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
26878cb08174SJohannes Berg 						  tb[IFLA_PORT_SELF],
26888cb08174SJohannes Berg 						  ifla_port_policy, NULL);
268957b61080SScott Feldman 		if (err < 0)
269057b61080SScott Feldman 			goto errout;
269157b61080SScott Feldman 
269257b61080SScott Feldman 		err = -EOPNOTSUPP;
269357b61080SScott Feldman 		if (ops->ndo_set_vf_port)
269457b61080SScott Feldman 			err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
269557b61080SScott Feldman 		if (err < 0)
269657b61080SScott Feldman 			goto errout;
2697ba998906SNicolas Dichtel 		status |= DO_SETLINK_NOTIFY;
269857b61080SScott Feldman 	}
2699f8ff182cSThomas Graf 
2700f8ff182cSThomas Graf 	if (tb[IFLA_AF_SPEC]) {
2701f8ff182cSThomas Graf 		struct nlattr *af;
2702f8ff182cSThomas Graf 		int rem;
2703f8ff182cSThomas Graf 
2704f8ff182cSThomas Graf 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2705f8ff182cSThomas Graf 			const struct rtnl_af_ops *af_ops;
2706f8ff182cSThomas Graf 
27075fa85a09SFlorian Westphal 			rcu_read_lock();
27085fa85a09SFlorian Westphal 
2709058c8d59SGustavo A. R. Silva 			BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af))));
2710f8ff182cSThomas Graf 
2711cf7afbfeSThomas Graf 			err = af_ops->set_link_af(dev, af);
27125fa85a09SFlorian Westphal 			if (err < 0) {
27135fa85a09SFlorian Westphal 				rcu_read_unlock();
2714f8ff182cSThomas Graf 				goto errout;
27155fa85a09SFlorian Westphal 			}
2716f8ff182cSThomas Graf 
27175fa85a09SFlorian Westphal 			rcu_read_unlock();
2718ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
2719f8ff182cSThomas Graf 		}
2720f8ff182cSThomas Graf 	}
272157b61080SScott Feldman 	err = 0;
272257b61080SScott Feldman 
272388d6378bSAnuradha Karuppiah 	if (tb[IFLA_PROTO_DOWN]) {
272488d6378bSAnuradha Karuppiah 		err = dev_change_proto_down(dev,
272588d6378bSAnuradha Karuppiah 					    nla_get_u8(tb[IFLA_PROTO_DOWN]));
272688d6378bSAnuradha Karuppiah 		if (err)
272788d6378bSAnuradha Karuppiah 			goto errout;
272888d6378bSAnuradha Karuppiah 		status |= DO_SETLINK_NOTIFY;
272988d6378bSAnuradha Karuppiah 	}
273088d6378bSAnuradha Karuppiah 
2731d1fdd913SBrenden Blanco 	if (tb[IFLA_XDP]) {
2732d1fdd913SBrenden Blanco 		struct nlattr *xdp[IFLA_XDP_MAX + 1];
273385de8576SDaniel Borkmann 		u32 xdp_flags = 0;
2734d1fdd913SBrenden Blanco 
27358cb08174SJohannes Berg 		err = nla_parse_nested_deprecated(xdp, IFLA_XDP_MAX,
27368cb08174SJohannes Berg 						  tb[IFLA_XDP],
2737fceb6435SJohannes Berg 						  ifla_xdp_policy, NULL);
2738d1fdd913SBrenden Blanco 		if (err < 0)
2739d1fdd913SBrenden Blanco 			goto errout;
2740d1fdd913SBrenden Blanco 
274158038695SMartin KaFai Lau 		if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
2742262d8625SBrenden Blanco 			err = -EINVAL;
2743262d8625SBrenden Blanco 			goto errout;
2744262d8625SBrenden Blanco 		}
274585de8576SDaniel Borkmann 
274685de8576SDaniel Borkmann 		if (xdp[IFLA_XDP_FLAGS]) {
274785de8576SDaniel Borkmann 			xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
274885de8576SDaniel Borkmann 			if (xdp_flags & ~XDP_FLAGS_MASK) {
274985de8576SDaniel Borkmann 				err = -EINVAL;
275085de8576SDaniel Borkmann 				goto errout;
275185de8576SDaniel Borkmann 			}
2752ee5d032fSJakub Kicinski 			if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) {
27530489df9aSDaniel Borkmann 				err = -EINVAL;
27540489df9aSDaniel Borkmann 				goto errout;
27550489df9aSDaniel Borkmann 			}
275685de8576SDaniel Borkmann 		}
275785de8576SDaniel Borkmann 
2758d1fdd913SBrenden Blanco 		if (xdp[IFLA_XDP_FD]) {
2759ddf9f970SJakub Kicinski 			err = dev_change_xdp_fd(dev, extack,
276085de8576SDaniel Borkmann 						nla_get_s32(xdp[IFLA_XDP_FD]),
276185de8576SDaniel Borkmann 						xdp_flags);
2762d1fdd913SBrenden Blanco 			if (err)
2763d1fdd913SBrenden Blanco 				goto errout;
2764d1fdd913SBrenden Blanco 			status |= DO_SETLINK_NOTIFY;
2765d1fdd913SBrenden Blanco 		}
2766d1fdd913SBrenden Blanco 	}
2767d1fdd913SBrenden Blanco 
27680157f60cSPatrick McHardy errout:
2769ba998906SNicolas Dichtel 	if (status & DO_SETLINK_MODIFIED) {
277064ff90ccSXin Long 		if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY)
2771ba998906SNicolas Dichtel 			netdev_state_change(dev);
2772ba998906SNicolas Dichtel 
2773ba998906SNicolas Dichtel 		if (err < 0)
2774e87cc472SJoe 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",
2775e87cc472SJoe Perches 					     dev->name);
2776ba998906SNicolas Dichtel 	}
27770157f60cSPatrick McHardy 
27780157f60cSPatrick McHardy 	return err;
27790157f60cSPatrick McHardy }
27800157f60cSPatrick McHardy 
2781cc6090e9SJiri Pirko static struct net_device *rtnl_dev_get(struct net *net,
2782cc6090e9SJiri Pirko 				       struct nlattr *ifname_attr,
2783*76c9ac0eSJiri Pirko 				       struct nlattr *altifname_attr,
2784cc6090e9SJiri Pirko 				       char *ifname)
2785cc6090e9SJiri Pirko {
2786*76c9ac0eSJiri Pirko 	char buffer[ALTIFNAMSIZ];
2787cc6090e9SJiri Pirko 
2788cc6090e9SJiri Pirko 	if (!ifname) {
2789cc6090e9SJiri Pirko 		ifname = buffer;
2790cc6090e9SJiri Pirko 		if (ifname_attr)
2791cc6090e9SJiri Pirko 			nla_strlcpy(ifname, ifname_attr, IFNAMSIZ);
2792*76c9ac0eSJiri Pirko 		else if (altifname_attr)
2793*76c9ac0eSJiri Pirko 			nla_strlcpy(ifname, altifname_attr, ALTIFNAMSIZ);
2794cc6090e9SJiri Pirko 		else
2795cc6090e9SJiri Pirko 			return NULL;
2796cc6090e9SJiri Pirko 	}
2797cc6090e9SJiri Pirko 
2798cc6090e9SJiri Pirko 	return __dev_get_by_name(net, ifname);
2799cc6090e9SJiri Pirko }
2800cc6090e9SJiri Pirko 
2801c21ef3e3SDavid Ahern static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2802c21ef3e3SDavid Ahern 			struct netlink_ext_ack *extack)
2803da5e0494SThomas Graf {
28043b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
2805da5e0494SThomas Graf 	struct ifinfomsg *ifm;
2806da5e0494SThomas Graf 	struct net_device *dev;
28070157f60cSPatrick McHardy 	int err;
2808da5e0494SThomas Graf 	struct nlattr *tb[IFLA_MAX+1];
28091da177e4SLinus Torvalds 	char ifname[IFNAMSIZ];
28101da177e4SLinus Torvalds 
28118cb08174SJohannes Berg 	err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
28128cb08174SJohannes Berg 				     ifla_policy, extack);
2813da5e0494SThomas Graf 	if (err < 0)
2814da5e0494SThomas Graf 		goto errout;
28151da177e4SLinus Torvalds 
28164ff66caeSChristian Brauner 	err = rtnl_ensure_unique_netns(tb, extack, false);
28174ff66caeSChristian Brauner 	if (err < 0)
28184ff66caeSChristian Brauner 		goto errout;
28194ff66caeSChristian Brauner 
28205176f91eSThomas Graf 	if (tb[IFLA_IFNAME])
28215176f91eSThomas Graf 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
282278e5b891SPatrick McHardy 	else
282378e5b891SPatrick McHardy 		ifname[0] = '\0';
28241da177e4SLinus Torvalds 
28251da177e4SLinus Torvalds 	err = -EINVAL;
2826da5e0494SThomas Graf 	ifm = nlmsg_data(nlh);
282751055be8SPatrick McHardy 	if (ifm->ifi_index > 0)
2828a3d12891SEric Dumazet 		dev = __dev_get_by_index(net, ifm->ifi_index);
2829*76c9ac0eSJiri Pirko 	else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
2830*76c9ac0eSJiri Pirko 		dev = rtnl_dev_get(net, NULL, tb[IFLA_ALT_IFNAME], ifname);
2831da5e0494SThomas Graf 	else
2832da5e0494SThomas Graf 		goto errout;
28331da177e4SLinus Torvalds 
2834da5e0494SThomas Graf 	if (dev == NULL) {
2835da5e0494SThomas Graf 		err = -ENODEV;
2836da5e0494SThomas Graf 		goto errout;
2837da5e0494SThomas Graf 	}
28381da177e4SLinus Torvalds 
2839ddf9f970SJakub Kicinski 	err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0);
2840da5e0494SThomas Graf errout:
28411da177e4SLinus Torvalds 	return err;
28421da177e4SLinus Torvalds }
28431da177e4SLinus Torvalds 
284466400d54SWANG Cong static int rtnl_group_dellink(const struct net *net, int group)
284566400d54SWANG Cong {
284666400d54SWANG Cong 	struct net_device *dev, *aux;
284766400d54SWANG Cong 	LIST_HEAD(list_kill);
284866400d54SWANG Cong 	bool found = false;
284966400d54SWANG Cong 
285066400d54SWANG Cong 	if (!group)
285166400d54SWANG Cong 		return -EPERM;
285266400d54SWANG Cong 
285366400d54SWANG Cong 	for_each_netdev(net, dev) {
285466400d54SWANG Cong 		if (dev->group == group) {
285566400d54SWANG Cong 			const struct rtnl_link_ops *ops;
285666400d54SWANG Cong 
285766400d54SWANG Cong 			found = true;
285866400d54SWANG Cong 			ops = dev->rtnl_link_ops;
285966400d54SWANG Cong 			if (!ops || !ops->dellink)
286066400d54SWANG Cong 				return -EOPNOTSUPP;
286166400d54SWANG Cong 		}
286266400d54SWANG Cong 	}
286366400d54SWANG Cong 
286466400d54SWANG Cong 	if (!found)
286566400d54SWANG Cong 		return -ENODEV;
286666400d54SWANG Cong 
286766400d54SWANG Cong 	for_each_netdev_safe(net, dev, aux) {
286866400d54SWANG Cong 		if (dev->group == group) {
286966400d54SWANG Cong 			const struct rtnl_link_ops *ops;
287066400d54SWANG Cong 
287166400d54SWANG Cong 			ops = dev->rtnl_link_ops;
287266400d54SWANG Cong 			ops->dellink(dev, &list_kill);
287366400d54SWANG Cong 		}
287466400d54SWANG Cong 	}
287566400d54SWANG Cong 	unregister_netdevice_many(&list_kill);
287666400d54SWANG Cong 
287766400d54SWANG Cong 	return 0;
287866400d54SWANG Cong }
287966400d54SWANG Cong 
2880614732eaSThomas Graf int rtnl_delete_link(struct net_device *dev)
2881614732eaSThomas Graf {
2882614732eaSThomas Graf 	const struct rtnl_link_ops *ops;
2883614732eaSThomas Graf 	LIST_HEAD(list_kill);
2884614732eaSThomas Graf 
2885614732eaSThomas Graf 	ops = dev->rtnl_link_ops;
2886614732eaSThomas Graf 	if (!ops || !ops->dellink)
2887614732eaSThomas Graf 		return -EOPNOTSUPP;
2888614732eaSThomas Graf 
2889614732eaSThomas Graf 	ops->dellink(dev, &list_kill);
2890614732eaSThomas Graf 	unregister_netdevice_many(&list_kill);
2891614732eaSThomas Graf 
2892614732eaSThomas Graf 	return 0;
2893614732eaSThomas Graf }
2894614732eaSThomas Graf EXPORT_SYMBOL_GPL(rtnl_delete_link);
2895614732eaSThomas Graf 
2896c21ef3e3SDavid Ahern static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
2897c21ef3e3SDavid Ahern 			struct netlink_ext_ack *extack)
289838f7b870SPatrick McHardy {
28993b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
2900b61ad68aSChristian Brauner 	struct net *tgt_net = net;
2901b61ad68aSChristian Brauner 	struct net_device *dev = NULL;
290238f7b870SPatrick McHardy 	struct ifinfomsg *ifm;
290338f7b870SPatrick McHardy 	struct nlattr *tb[IFLA_MAX+1];
290438f7b870SPatrick McHardy 	int err;
2905b61ad68aSChristian Brauner 	int netnsid = -1;
290638f7b870SPatrick McHardy 
29078cb08174SJohannes Berg 	err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
29088cb08174SJohannes Berg 				     ifla_policy, extack);
290938f7b870SPatrick McHardy 	if (err < 0)
291038f7b870SPatrick McHardy 		return err;
291138f7b870SPatrick McHardy 
29124ff66caeSChristian Brauner 	err = rtnl_ensure_unique_netns(tb, extack, true);
29134ff66caeSChristian Brauner 	if (err < 0)
29144ff66caeSChristian Brauner 		return err;
29154ff66caeSChristian Brauner 
29167e4a8d5aSChristian Brauner 	if (tb[IFLA_TARGET_NETNSID]) {
29177e4a8d5aSChristian Brauner 		netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
2918c383edc4SChristian Brauner 		tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
2919b61ad68aSChristian Brauner 		if (IS_ERR(tgt_net))
2920b61ad68aSChristian Brauner 			return PTR_ERR(tgt_net);
2921b61ad68aSChristian Brauner 	}
2922b61ad68aSChristian Brauner 
2923b61ad68aSChristian Brauner 	err = -EINVAL;
292438f7b870SPatrick McHardy 	ifm = nlmsg_data(nlh);
292538f7b870SPatrick McHardy 	if (ifm->ifi_index > 0)
2926b61ad68aSChristian Brauner 		dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
2927*76c9ac0eSJiri Pirko 	else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
2928*76c9ac0eSJiri Pirko 		dev = rtnl_dev_get(net, tb[IFLA_IFNAME],
2929*76c9ac0eSJiri Pirko 				   tb[IFLA_ALT_IFNAME], NULL);
293066400d54SWANG Cong 	else if (tb[IFLA_GROUP])
2931b61ad68aSChristian Brauner 		err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP]));
293238f7b870SPatrick McHardy 	else
2933b61ad68aSChristian Brauner 		goto out;
293438f7b870SPatrick McHardy 
2935b61ad68aSChristian Brauner 	if (!dev) {
2936b61ad68aSChristian Brauner 		if (tb[IFLA_IFNAME] || ifm->ifi_index > 0)
2937b61ad68aSChristian Brauner 			err = -ENODEV;
293838f7b870SPatrick McHardy 
2939b61ad68aSChristian Brauner 		goto out;
2940b61ad68aSChristian Brauner 	}
2941b61ad68aSChristian Brauner 
2942b61ad68aSChristian Brauner 	err = rtnl_delete_link(dev);
2943b61ad68aSChristian Brauner 
2944b61ad68aSChristian Brauner out:
2945b61ad68aSChristian Brauner 	if (netnsid >= 0)
2946b61ad68aSChristian Brauner 		put_net(tgt_net);
2947b61ad68aSChristian Brauner 
2948b61ad68aSChristian Brauner 	return err;
294938f7b870SPatrick McHardy }
295038f7b870SPatrick McHardy 
29513729d502SPatrick McHardy int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
29523729d502SPatrick McHardy {
29533729d502SPatrick McHardy 	unsigned int old_flags;
29543729d502SPatrick McHardy 	int err;
29553729d502SPatrick McHardy 
29563729d502SPatrick McHardy 	old_flags = dev->flags;
29573729d502SPatrick McHardy 	if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
29586d040321SPetr Machata 		err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
29596d040321SPetr Machata 					 NULL);
29603729d502SPatrick McHardy 		if (err < 0)
29613729d502SPatrick McHardy 			return err;
29623729d502SPatrick McHardy 	}
29633729d502SPatrick McHardy 
29648d356b89SRoopa Prabhu 	if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) {
296556a49d70SRoopa Prabhu 		__dev_notify_flags(dev, old_flags, (old_flags ^ dev->flags));
29668d356b89SRoopa Prabhu 	} else {
29673729d502SPatrick McHardy 		dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
2968a528c219SNicolas Dichtel 		__dev_notify_flags(dev, old_flags, ~0U);
29698d356b89SRoopa Prabhu 	}
29703729d502SPatrick McHardy 	return 0;
29713729d502SPatrick McHardy }
29723729d502SPatrick McHardy EXPORT_SYMBOL(rtnl_configure_link);
29733729d502SPatrick McHardy 
2974d0522f1cSDavid Ahern struct net_device *rtnl_create_link(struct net *net, const char *ifname,
2975d0522f1cSDavid Ahern 				    unsigned char name_assign_type,
2976d0522f1cSDavid Ahern 				    const struct rtnl_link_ops *ops,
2977d0522f1cSDavid Ahern 				    struct nlattr *tb[],
2978d0522f1cSDavid Ahern 				    struct netlink_ext_ack *extack)
2979e7199288SPavel Emelianov {
2980e7199288SPavel Emelianov 	struct net_device *dev;
2981d40156aaSJiri Pirko 	unsigned int num_tx_queues = 1;
2982d40156aaSJiri Pirko 	unsigned int num_rx_queues = 1;
2983e7199288SPavel Emelianov 
298476ff5cc9SJiri Pirko 	if (tb[IFLA_NUM_TX_QUEUES])
298576ff5cc9SJiri Pirko 		num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
298676ff5cc9SJiri Pirko 	else if (ops->get_num_tx_queues)
2987d40156aaSJiri Pirko 		num_tx_queues = ops->get_num_tx_queues();
298876ff5cc9SJiri Pirko 
298976ff5cc9SJiri Pirko 	if (tb[IFLA_NUM_RX_QUEUES])
299076ff5cc9SJiri Pirko 		num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
299176ff5cc9SJiri Pirko 	else if (ops->get_num_rx_queues)
2992d40156aaSJiri Pirko 		num_rx_queues = ops->get_num_rx_queues();
2993efacb309Sstephen hemminger 
2994d0522f1cSDavid Ahern 	if (num_tx_queues < 1 || num_tx_queues > 4096) {
2995d0522f1cSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid number of transmit queues");
29960e1d6ecaSEric Dumazet 		return ERR_PTR(-EINVAL);
2997d0522f1cSDavid Ahern 	}
29980e1d6ecaSEric Dumazet 
2999d0522f1cSDavid Ahern 	if (num_rx_queues < 1 || num_rx_queues > 4096) {
3000d0522f1cSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid number of receive queues");
30010e1d6ecaSEric Dumazet 		return ERR_PTR(-EINVAL);
3002d0522f1cSDavid Ahern 	}
30030e1d6ecaSEric Dumazet 
30045517750fSTom Gundersen 	dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
3005c835a677STom Gundersen 			       ops->setup, num_tx_queues, num_rx_queues);
3006e7199288SPavel Emelianov 	if (!dev)
3007d1892e4eSTobias Klauser 		return ERR_PTR(-ENOMEM);
3008e7199288SPavel Emelianov 
300981adee47SEric W. Biederman 	dev_net_set(dev, net);
301081adee47SEric W. Biederman 	dev->rtnl_link_ops = ops;
30113729d502SPatrick McHardy 	dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
301281adee47SEric W. Biederman 
3013e7199288SPavel Emelianov 	if (tb[IFLA_MTU])
3014e7199288SPavel Emelianov 		dev->mtu = nla_get_u32(tb[IFLA_MTU]);
30152afb9b53SJiri Pirko 	if (tb[IFLA_ADDRESS]) {
3016e7199288SPavel Emelianov 		memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
3017e7199288SPavel Emelianov 				nla_len(tb[IFLA_ADDRESS]));
30182afb9b53SJiri Pirko 		dev->addr_assign_type = NET_ADDR_SET;
30192afb9b53SJiri Pirko 	}
3020e7199288SPavel Emelianov 	if (tb[IFLA_BROADCAST])
3021e7199288SPavel Emelianov 		memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
3022e7199288SPavel Emelianov 				nla_len(tb[IFLA_BROADCAST]));
3023e7199288SPavel Emelianov 	if (tb[IFLA_TXQLEN])
3024e7199288SPavel Emelianov 		dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
3025e7199288SPavel Emelianov 	if (tb[IFLA_OPERSTATE])
302693b2d4a2SDavid S. Miller 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
3027e7199288SPavel Emelianov 	if (tb[IFLA_LINKMODE])
3028e7199288SPavel Emelianov 		dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
3029ffa934f1SPatrick McHardy 	if (tb[IFLA_GROUP])
3030ffa934f1SPatrick McHardy 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
303146e6b992SStephen Hemminger 	if (tb[IFLA_GSO_MAX_SIZE])
303246e6b992SStephen Hemminger 		netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE]));
303346e6b992SStephen Hemminger 	if (tb[IFLA_GSO_MAX_SEGS])
3034a0b586faSStephen Hemminger 		dev->gso_max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
3035e7199288SPavel Emelianov 
3036e7199288SPavel Emelianov 	return dev;
3037e7199288SPavel Emelianov }
3038e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_create_link);
3039e7199288SPavel Emelianov 
304090f62cf3SEric W. Biederman static int rtnl_group_changelink(const struct sk_buff *skb,
304190f62cf3SEric W. Biederman 		struct net *net, int group,
3042e7ed828fSVlad Dogaru 		struct ifinfomsg *ifm,
3043ddf9f970SJakub Kicinski 		struct netlink_ext_ack *extack,
3044e7ed828fSVlad Dogaru 		struct nlattr **tb)
3045e7ed828fSVlad Dogaru {
3046d079535dSWANG Cong 	struct net_device *dev, *aux;
3047e7ed828fSVlad Dogaru 	int err;
3048e7ed828fSVlad Dogaru 
3049d079535dSWANG Cong 	for_each_netdev_safe(net, dev, aux) {
3050e7ed828fSVlad Dogaru 		if (dev->group == group) {
3051ddf9f970SJakub Kicinski 			err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0);
3052e7ed828fSVlad Dogaru 			if (err < 0)
3053e7ed828fSVlad Dogaru 				return err;
3054e7ed828fSVlad Dogaru 		}
3055e7ed828fSVlad Dogaru 	}
3056e7ed828fSVlad Dogaru 
3057e7ed828fSVlad Dogaru 	return 0;
3058e7ed828fSVlad Dogaru }
3059e7ed828fSVlad Dogaru 
3060a2939745SJakub Kicinski static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3061a2939745SJakub Kicinski 			  struct nlattr **attr, struct netlink_ext_ack *extack)
306238f7b870SPatrick McHardy {
3063420d0318SJakub Kicinski 	struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1];
3064420d0318SJakub Kicinski 	unsigned char name_assign_type = NET_NAME_USER;
3065420d0318SJakub Kicinski 	struct nlattr *linkinfo[IFLA_INFO_MAX + 1];
3066420d0318SJakub Kicinski 	const struct rtnl_link_ops *m_ops = NULL;
3067420d0318SJakub Kicinski 	struct net_device *master_dev = NULL;
30683b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
306938f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops;
307038f7b870SPatrick McHardy 	struct nlattr *tb[IFLA_MAX + 1];
3071420d0318SJakub Kicinski 	struct net *dest_net, *link_net;
3072420d0318SJakub Kicinski 	struct nlattr **slave_data;
3073420d0318SJakub Kicinski 	char kind[MODULE_NAME_LEN];
3074420d0318SJakub Kicinski 	struct net_device *dev;
3075420d0318SJakub Kicinski 	struct ifinfomsg *ifm;
3076420d0318SJakub Kicinski 	char ifname[IFNAMSIZ];
3077420d0318SJakub Kicinski 	struct nlattr **data;
307838f7b870SPatrick McHardy 	int err;
307938f7b870SPatrick McHardy 
308095a5afcaSJohannes Berg #ifdef CONFIG_MODULES
308138f7b870SPatrick McHardy replay:
30828072f085SThomas Graf #endif
30838cb08174SJohannes Berg 	err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
30848cb08174SJohannes Berg 				     ifla_policy, extack);
308538f7b870SPatrick McHardy 	if (err < 0)
308638f7b870SPatrick McHardy 		return err;
308738f7b870SPatrick McHardy 
30884ff66caeSChristian Brauner 	err = rtnl_ensure_unique_netns(tb, extack, false);
30894ff66caeSChristian Brauner 	if (err < 0)
30904ff66caeSChristian Brauner 		return err;
30914ff66caeSChristian Brauner 
309238f7b870SPatrick McHardy 	if (tb[IFLA_IFNAME])
309338f7b870SPatrick McHardy 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
309438f7b870SPatrick McHardy 	else
309538f7b870SPatrick McHardy 		ifname[0] = '\0';
309638f7b870SPatrick McHardy 
309738f7b870SPatrick McHardy 	ifm = nlmsg_data(nlh);
309838f7b870SPatrick McHardy 	if (ifm->ifi_index > 0)
3099881d966bSEric W. Biederman 		dev = __dev_get_by_index(net, ifm->ifi_index);
3100*76c9ac0eSJiri Pirko 	else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3101*76c9ac0eSJiri Pirko 		dev = rtnl_dev_get(net, NULL, tb[IFLA_ALT_IFNAME], ifname);
310238f7b870SPatrick McHardy 	else
310338f7b870SPatrick McHardy 		dev = NULL;
310438f7b870SPatrick McHardy 
3105ba7d49b1SJiri Pirko 	if (dev) {
3106ba7d49b1SJiri Pirko 		master_dev = netdev_master_upper_dev_get(dev);
3107ba7d49b1SJiri Pirko 		if (master_dev)
3108ba7d49b1SJiri Pirko 			m_ops = master_dev->rtnl_link_ops;
3109ba7d49b1SJiri Pirko 	}
3110ba7d49b1SJiri Pirko 
3111e0d087afSEric Dumazet 	err = validate_linkmsg(dev, tb);
3112e0d087afSEric Dumazet 	if (err < 0)
31131840bb13SThomas Graf 		return err;
31141840bb13SThomas Graf 
311538f7b870SPatrick McHardy 	if (tb[IFLA_LINKINFO]) {
31168cb08174SJohannes Berg 		err = nla_parse_nested_deprecated(linkinfo, IFLA_INFO_MAX,
31178cb08174SJohannes Berg 						  tb[IFLA_LINKINFO],
31188cb08174SJohannes Berg 						  ifla_info_policy, NULL);
311938f7b870SPatrick McHardy 		if (err < 0)
312038f7b870SPatrick McHardy 			return err;
312138f7b870SPatrick McHardy 	} else
312238f7b870SPatrick McHardy 		memset(linkinfo, 0, sizeof(linkinfo));
312338f7b870SPatrick McHardy 
312438f7b870SPatrick McHardy 	if (linkinfo[IFLA_INFO_KIND]) {
312538f7b870SPatrick McHardy 		nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
312638f7b870SPatrick McHardy 		ops = rtnl_link_ops_get(kind);
312738f7b870SPatrick McHardy 	} else {
312838f7b870SPatrick McHardy 		kind[0] = '\0';
312938f7b870SPatrick McHardy 		ops = NULL;
313038f7b870SPatrick McHardy 	}
313138f7b870SPatrick McHardy 
3132420d0318SJakub Kicinski 	data = NULL;
313338f7b870SPatrick McHardy 	if (ops) {
3134ccf8dbcdSKees Cook 		if (ops->maxtype > RTNL_MAX_TYPE)
3135ccf8dbcdSKees Cook 			return -EINVAL;
3136ccf8dbcdSKees Cook 
313738f7b870SPatrick McHardy 		if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
31388cb08174SJohannes Berg 			err = nla_parse_nested_deprecated(attr, ops->maxtype,
313938f7b870SPatrick McHardy 							  linkinfo[IFLA_INFO_DATA],
314068d57f3bSDavid Ahern 							  ops->policy, extack);
314138f7b870SPatrick McHardy 			if (err < 0)
314238f7b870SPatrick McHardy 				return err;
314338f7b870SPatrick McHardy 			data = attr;
314438f7b870SPatrick McHardy 		}
314538f7b870SPatrick McHardy 		if (ops->validate) {
3146a8b8a889SMatthias Schiffer 			err = ops->validate(tb, data, extack);
314738f7b870SPatrick McHardy 			if (err < 0)
314838f7b870SPatrick McHardy 				return err;
314938f7b870SPatrick McHardy 		}
315038f7b870SPatrick McHardy 	}
315138f7b870SPatrick McHardy 
3152420d0318SJakub Kicinski 	slave_data = NULL;
3153ba7d49b1SJiri Pirko 	if (m_ops) {
3154af066ed3SPrashant Bhole 		if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE)
3155ccf8dbcdSKees Cook 			return -EINVAL;
3156ccf8dbcdSKees Cook 
3157ba7d49b1SJiri Pirko 		if (m_ops->slave_maxtype &&
3158ba7d49b1SJiri Pirko 		    linkinfo[IFLA_INFO_SLAVE_DATA]) {
31598cb08174SJohannes Berg 			err = nla_parse_nested_deprecated(slave_attr,
31608cb08174SJohannes Berg 							  m_ops->slave_maxtype,
3161ba7d49b1SJiri Pirko 							  linkinfo[IFLA_INFO_SLAVE_DATA],
31628cb08174SJohannes Berg 							  m_ops->slave_policy,
31638cb08174SJohannes Berg 							  extack);
3164ba7d49b1SJiri Pirko 			if (err < 0)
3165ba7d49b1SJiri Pirko 				return err;
3166ba7d49b1SJiri Pirko 			slave_data = slave_attr;
3167ba7d49b1SJiri Pirko 		}
3168ba7d49b1SJiri Pirko 	}
3169ba7d49b1SJiri Pirko 
317038f7b870SPatrick McHardy 	if (dev) {
317190c325e3SNicolas Dichtel 		int status = 0;
317238f7b870SPatrick McHardy 
317338f7b870SPatrick McHardy 		if (nlh->nlmsg_flags & NLM_F_EXCL)
317438f7b870SPatrick McHardy 			return -EEXIST;
317538f7b870SPatrick McHardy 		if (nlh->nlmsg_flags & NLM_F_REPLACE)
317638f7b870SPatrick McHardy 			return -EOPNOTSUPP;
317738f7b870SPatrick McHardy 
317838f7b870SPatrick McHardy 		if (linkinfo[IFLA_INFO_DATA]) {
317938f7b870SPatrick McHardy 			if (!ops || ops != dev->rtnl_link_ops ||
318038f7b870SPatrick McHardy 			    !ops->changelink)
318138f7b870SPatrick McHardy 				return -EOPNOTSUPP;
318238f7b870SPatrick McHardy 
3183ad744b22SMatthias Schiffer 			err = ops->changelink(dev, tb, data, extack);
318438f7b870SPatrick McHardy 			if (err < 0)
318538f7b870SPatrick McHardy 				return err;
3186ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
318738f7b870SPatrick McHardy 		}
318838f7b870SPatrick McHardy 
3189ba7d49b1SJiri Pirko 		if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
3190ba7d49b1SJiri Pirko 			if (!m_ops || !m_ops->slave_changelink)
3191ba7d49b1SJiri Pirko 				return -EOPNOTSUPP;
3192ba7d49b1SJiri Pirko 
3193420d0318SJakub Kicinski 			err = m_ops->slave_changelink(master_dev, dev, tb,
3194420d0318SJakub Kicinski 						      slave_data, extack);
3195ba7d49b1SJiri Pirko 			if (err < 0)
3196ba7d49b1SJiri Pirko 				return err;
3197ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
3198ba7d49b1SJiri Pirko 		}
3199ba7d49b1SJiri Pirko 
3200420d0318SJakub Kicinski 		return do_setlink(skb, dev, ifm, extack, tb, ifname, status);
320138f7b870SPatrick McHardy 	}
320238f7b870SPatrick McHardy 
3203ffa934f1SPatrick McHardy 	if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
3204ffa934f1SPatrick McHardy 		if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
320590f62cf3SEric W. Biederman 			return rtnl_group_changelink(skb, net,
3206ffa934f1SPatrick McHardy 						nla_get_u32(tb[IFLA_GROUP]),
3207ddf9f970SJakub Kicinski 						ifm, extack, tb);
320838f7b870SPatrick McHardy 		return -ENODEV;
3209ffa934f1SPatrick McHardy 	}
321038f7b870SPatrick McHardy 
3211160ca014STheuns Verwoerd 	if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
321238f7b870SPatrick McHardy 		return -EOPNOTSUPP;
321338f7b870SPatrick McHardy 
321438f7b870SPatrick McHardy 	if (!ops) {
321595a5afcaSJohannes Berg #ifdef CONFIG_MODULES
321638f7b870SPatrick McHardy 		if (kind[0]) {
321738f7b870SPatrick McHardy 			__rtnl_unlock();
321838f7b870SPatrick McHardy 			request_module("rtnl-link-%s", kind);
321938f7b870SPatrick McHardy 			rtnl_lock();
322038f7b870SPatrick McHardy 			ops = rtnl_link_ops_get(kind);
322138f7b870SPatrick McHardy 			if (ops)
322238f7b870SPatrick McHardy 				goto replay;
322338f7b870SPatrick McHardy 		}
322438f7b870SPatrick McHardy #endif
322568d57f3bSDavid Ahern 		NL_SET_ERR_MSG(extack, "Unknown device type");
322638f7b870SPatrick McHardy 		return -EOPNOTSUPP;
322738f7b870SPatrick McHardy 	}
322838f7b870SPatrick McHardy 
3229b0ab2fabSJiri Pirko 	if (!ops->setup)
3230b0ab2fabSJiri Pirko 		return -EOPNOTSUPP;
3231b0ab2fabSJiri Pirko 
32325517750fSTom Gundersen 	if (!ifname[0]) {
323338f7b870SPatrick McHardy 		snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
32345517750fSTom Gundersen 		name_assign_type = NET_NAME_ENUM;
32355517750fSTom Gundersen 	}
323638f7b870SPatrick McHardy 
32375bb8ed07SChristian Brauner 	dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN);
323813ad1774SEric W. Biederman 	if (IS_ERR(dest_net))
323913ad1774SEric W. Biederman 		return PTR_ERR(dest_net);
324013ad1774SEric W. Biederman 
3241317f4810SNicolas Dichtel 	if (tb[IFLA_LINK_NETNSID]) {
3242317f4810SNicolas Dichtel 		int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
3243317f4810SNicolas Dichtel 
3244317f4810SNicolas Dichtel 		link_net = get_net_ns_by_id(dest_net, id);
3245317f4810SNicolas Dichtel 		if (!link_net) {
324668d57f3bSDavid Ahern 			NL_SET_ERR_MSG(extack, "Unknown network namespace id");
3247317f4810SNicolas Dichtel 			err =  -EINVAL;
3248317f4810SNicolas Dichtel 			goto out;
3249317f4810SNicolas Dichtel 		}
325006615bedSEric W. Biederman 		err = -EPERM;
325106615bedSEric W. Biederman 		if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
325206615bedSEric W. Biederman 			goto out;
3253420d0318SJakub Kicinski 	} else {
3254420d0318SJakub Kicinski 		link_net = NULL;
3255317f4810SNicolas Dichtel 	}
3256317f4810SNicolas Dichtel 
3257317f4810SNicolas Dichtel 	dev = rtnl_create_link(link_net ? : dest_net, ifname,
3258d0522f1cSDavid Ahern 			       name_assign_type, ops, tb, extack);
32599c7dafbfSPavel Emelyanov 	if (IS_ERR(dev)) {
3260e7199288SPavel Emelianov 		err = PTR_ERR(dev);
32619c7dafbfSPavel Emelyanov 		goto out;
32629c7dafbfSPavel Emelyanov 	}
32639c7dafbfSPavel Emelyanov 
32649c7dafbfSPavel Emelyanov 	dev->ifindex = ifm->ifi_index;
32659c7dafbfSPavel Emelyanov 
32660e0eee24SCong Wang 	if (ops->newlink) {
3267420d0318SJakub Kicinski 		err = ops->newlink(link_net ? : net, dev, tb, data, extack);
32680e0eee24SCong Wang 		/* Drivers should call free_netdev() in ->destructor
3269e51fb152SCong Wang 		 * and unregister it on failure after registration
3270e51fb152SCong Wang 		 * so that device could be finally freed in rtnl_unlock.
32710e0eee24SCong Wang 		 */
3272e51fb152SCong Wang 		if (err < 0) {
3273e51fb152SCong Wang 			/* If device is not registered at all, free it now */
3274e51fb152SCong Wang 			if (dev->reg_state == NETREG_UNINITIALIZED)
3275e51fb152SCong Wang 				free_netdev(dev);
32760e0eee24SCong Wang 			goto out;
3277e51fb152SCong Wang 		}
32780e0eee24SCong Wang 	} else {
32792d85cba2SPatrick McHardy 		err = register_netdevice(dev);
3280fce9b9beSDan Carpenter 		if (err < 0) {
328138f7b870SPatrick McHardy 			free_netdev(dev);
32823729d502SPatrick McHardy 			goto out;
3283fce9b9beSDan Carpenter 		}
32840e0eee24SCong Wang 	}
32853729d502SPatrick McHardy 	err = rtnl_configure_link(dev, ifm);
328643638900SDavid S. Miller 	if (err < 0)
328743638900SDavid S. Miller 		goto out_unregister;
328843638900SDavid S. Miller 	if (link_net) {
328943638900SDavid S. Miller 		err = dev_change_net_namespace(dev, dest_net, ifname);
329043638900SDavid S. Miller 		if (err < 0)
329143638900SDavid S. Miller 			goto out_unregister;
329243638900SDavid S. Miller 	}
3293160ca014STheuns Verwoerd 	if (tb[IFLA_MASTER]) {
3294420d0318SJakub Kicinski 		err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
3295160ca014STheuns Verwoerd 		if (err)
3296160ca014STheuns Verwoerd 			goto out_unregister;
3297160ca014STheuns Verwoerd 	}
329843638900SDavid S. Miller out:
329943638900SDavid S. Miller 	if (link_net)
330043638900SDavid S. Miller 		put_net(link_net);
330143638900SDavid S. Miller 	put_net(dest_net);
330243638900SDavid S. Miller 	return err;
330343638900SDavid S. Miller out_unregister:
33047afb8886SWANG Cong 	if (ops->newlink) {
33057afb8886SWANG Cong 		LIST_HEAD(list_kill);
33067afb8886SWANG Cong 
33077afb8886SWANG Cong 		ops->dellink(dev, &list_kill);
33087afb8886SWANG Cong 		unregister_netdevice_many(&list_kill);
33097afb8886SWANG Cong 	} else {
33103729d502SPatrick McHardy 		unregister_netdevice(dev);
33117afb8886SWANG Cong 	}
3312317f4810SNicolas Dichtel 	goto out;
3313317f4810SNicolas Dichtel }
331438f7b870SPatrick McHardy 
3315a2939745SJakub Kicinski static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3316a2939745SJakub Kicinski 			struct netlink_ext_ack *extack)
3317a2939745SJakub Kicinski {
3318a2939745SJakub Kicinski 	struct nlattr **attr;
3319a2939745SJakub Kicinski 	int ret;
3320a2939745SJakub Kicinski 
3321a2939745SJakub Kicinski 	attr = kmalloc_array(RTNL_MAX_TYPE + 1, sizeof(*attr), GFP_KERNEL);
3322a2939745SJakub Kicinski 	if (!attr)
3323a2939745SJakub Kicinski 		return -ENOMEM;
3324a2939745SJakub Kicinski 
3325a2939745SJakub Kicinski 	ret = __rtnl_newlink(skb, nlh, attr, extack);
3326a2939745SJakub Kicinski 	kfree(attr);
3327a2939745SJakub Kicinski 	return ret;
3328a2939745SJakub Kicinski }
3329a2939745SJakub Kicinski 
33309b3757b0SJakub Kicinski static int rtnl_valid_getlink_req(struct sk_buff *skb,
33319b3757b0SJakub Kicinski 				  const struct nlmsghdr *nlh,
33329b3757b0SJakub Kicinski 				  struct nlattr **tb,
33339b3757b0SJakub Kicinski 				  struct netlink_ext_ack *extack)
33349b3757b0SJakub Kicinski {
33359b3757b0SJakub Kicinski 	struct ifinfomsg *ifm;
33369b3757b0SJakub Kicinski 	int i, err;
33379b3757b0SJakub Kicinski 
33389b3757b0SJakub Kicinski 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
33399b3757b0SJakub Kicinski 		NL_SET_ERR_MSG(extack, "Invalid header for get link");
33409b3757b0SJakub Kicinski 		return -EINVAL;
33419b3757b0SJakub Kicinski 	}
33429b3757b0SJakub Kicinski 
33439b3757b0SJakub Kicinski 	if (!netlink_strict_get_check(skb))
33448cb08174SJohannes Berg 		return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
33458cb08174SJohannes Berg 					      ifla_policy, extack);
33469b3757b0SJakub Kicinski 
33479b3757b0SJakub Kicinski 	ifm = nlmsg_data(nlh);
33489b3757b0SJakub Kicinski 	if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
33499b3757b0SJakub Kicinski 	    ifm->ifi_change) {
33509b3757b0SJakub Kicinski 		NL_SET_ERR_MSG(extack, "Invalid values in header for get link request");
33519b3757b0SJakub Kicinski 		return -EINVAL;
33529b3757b0SJakub Kicinski 	}
33539b3757b0SJakub Kicinski 
33548cb08174SJohannes Berg 	err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFLA_MAX,
33558cb08174SJohannes Berg 					    ifla_policy, extack);
33569b3757b0SJakub Kicinski 	if (err)
33579b3757b0SJakub Kicinski 		return err;
33589b3757b0SJakub Kicinski 
33599b3757b0SJakub Kicinski 	for (i = 0; i <= IFLA_MAX; i++) {
33609b3757b0SJakub Kicinski 		if (!tb[i])
33619b3757b0SJakub Kicinski 			continue;
33629b3757b0SJakub Kicinski 
33639b3757b0SJakub Kicinski 		switch (i) {
33649b3757b0SJakub Kicinski 		case IFLA_IFNAME:
3365*76c9ac0eSJiri Pirko 		case IFLA_ALT_IFNAME:
33669b3757b0SJakub Kicinski 		case IFLA_EXT_MASK:
33679b3757b0SJakub Kicinski 		case IFLA_TARGET_NETNSID:
33689b3757b0SJakub Kicinski 			break;
33699b3757b0SJakub Kicinski 		default:
33709b3757b0SJakub Kicinski 			NL_SET_ERR_MSG(extack, "Unsupported attribute in get link request");
33719b3757b0SJakub Kicinski 			return -EINVAL;
33729b3757b0SJakub Kicinski 		}
33739b3757b0SJakub Kicinski 	}
33749b3757b0SJakub Kicinski 
33759b3757b0SJakub Kicinski 	return 0;
33769b3757b0SJakub Kicinski }
33779b3757b0SJakub Kicinski 
3378c21ef3e3SDavid Ahern static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3379c21ef3e3SDavid Ahern 			struct netlink_ext_ack *extack)
3380711e2c33SJean Tourrilhes {
33813b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
338279e1ad14SJiri Benc 	struct net *tgt_net = net;
3383b60c5115SThomas Graf 	struct ifinfomsg *ifm;
3384b60c5115SThomas Graf 	struct nlattr *tb[IFLA_MAX+1];
3385b60c5115SThomas Graf 	struct net_device *dev = NULL;
3386b60c5115SThomas Graf 	struct sk_buff *nskb;
338779e1ad14SJiri Benc 	int netnsid = -1;
3388339bf98fSThomas Graf 	int err;
3389115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
3390711e2c33SJean Tourrilhes 
33919b3757b0SJakub Kicinski 	err = rtnl_valid_getlink_req(skb, nlh, tb, extack);
3392b60c5115SThomas Graf 	if (err < 0)
33939918f230SEric Sesterhenn 		return err;
3394b60c5115SThomas Graf 
33954ff66caeSChristian Brauner 	err = rtnl_ensure_unique_netns(tb, extack, true);
33964ff66caeSChristian Brauner 	if (err < 0)
33974ff66caeSChristian Brauner 		return err;
33984ff66caeSChristian Brauner 
33997e4a8d5aSChristian Brauner 	if (tb[IFLA_TARGET_NETNSID]) {
34007e4a8d5aSChristian Brauner 		netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
3401c383edc4SChristian Brauner 		tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
340279e1ad14SJiri Benc 		if (IS_ERR(tgt_net))
340379e1ad14SJiri Benc 			return PTR_ERR(tgt_net);
340479e1ad14SJiri Benc 	}
340579e1ad14SJiri Benc 
3406115c9b81SGreg Rose 	if (tb[IFLA_EXT_MASK])
3407115c9b81SGreg Rose 		ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3408115c9b81SGreg Rose 
340979e1ad14SJiri Benc 	err = -EINVAL;
3410b60c5115SThomas Graf 	ifm = nlmsg_data(nlh);
3411a3d12891SEric Dumazet 	if (ifm->ifi_index > 0)
341279e1ad14SJiri Benc 		dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
3413*76c9ac0eSJiri Pirko 	else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3414*76c9ac0eSJiri Pirko 		dev = rtnl_dev_get(tgt_net, tb[IFLA_IFNAME],
3415*76c9ac0eSJiri Pirko 				   tb[IFLA_ALT_IFNAME], NULL);
3416a3d12891SEric Dumazet 	else
341779e1ad14SJiri Benc 		goto out;
3418b60c5115SThomas Graf 
341979e1ad14SJiri Benc 	err = -ENODEV;
3420a3d12891SEric Dumazet 	if (dev == NULL)
342179e1ad14SJiri Benc 		goto out;
3422a3d12891SEric Dumazet 
342379e1ad14SJiri Benc 	err = -ENOBUFS;
3424115c9b81SGreg Rose 	nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
3425a3d12891SEric Dumazet 	if (nskb == NULL)
342679e1ad14SJiri Benc 		goto out;
3427711e2c33SJean Tourrilhes 
342879e1ad14SJiri Benc 	err = rtnl_fill_ifinfo(nskb, dev, net,
342979e1ad14SJiri Benc 			       RTM_NEWLINK, NETLINK_CB(skb).portid,
343079e1ad14SJiri Benc 			       nlh->nlmsg_seq, 0, 0, ext_filter_mask,
343138e01b30SNicolas Dichtel 			       0, NULL, 0, netnsid);
343226932566SPatrick McHardy 	if (err < 0) {
343326932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in if_nlmsg_size */
343426932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
343526932566SPatrick McHardy 		kfree_skb(nskb);
3436a3d12891SEric Dumazet 	} else
343715e47304SEric W. Biederman 		err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
343879e1ad14SJiri Benc out:
343979e1ad14SJiri Benc 	if (netnsid >= 0)
344079e1ad14SJiri Benc 		put_net(tgt_net);
3441b60c5115SThomas Graf 
3442711e2c33SJean Tourrilhes 	return err;
3443711e2c33SJean Tourrilhes }
3444711e2c33SJean Tourrilhes 
344536fbf1e5SJiri Pirko static int rtnl_alt_ifname(int cmd, struct net_device *dev, struct nlattr *attr,
344636fbf1e5SJiri Pirko 			   bool *changed, struct netlink_ext_ack *extack)
344736fbf1e5SJiri Pirko {
344836fbf1e5SJiri Pirko 	char *alt_ifname;
344936fbf1e5SJiri Pirko 	int err;
345036fbf1e5SJiri Pirko 
345136fbf1e5SJiri Pirko 	err = nla_validate(attr, attr->nla_len, IFLA_MAX, ifla_policy, extack);
345236fbf1e5SJiri Pirko 	if (err)
345336fbf1e5SJiri Pirko 		return err;
345436fbf1e5SJiri Pirko 
345536fbf1e5SJiri Pirko 	alt_ifname = nla_data(attr);
345636fbf1e5SJiri Pirko 	if (cmd == RTM_NEWLINKPROP) {
345736fbf1e5SJiri Pirko 		alt_ifname = kstrdup(alt_ifname, GFP_KERNEL);
345836fbf1e5SJiri Pirko 		if (!alt_ifname)
345936fbf1e5SJiri Pirko 			return -ENOMEM;
346036fbf1e5SJiri Pirko 		err = netdev_name_node_alt_create(dev, alt_ifname);
346136fbf1e5SJiri Pirko 		if (err) {
346236fbf1e5SJiri Pirko 			kfree(alt_ifname);
346336fbf1e5SJiri Pirko 			return err;
346436fbf1e5SJiri Pirko 		}
346536fbf1e5SJiri Pirko 	} else if (cmd == RTM_DELLINKPROP) {
346636fbf1e5SJiri Pirko 		err = netdev_name_node_alt_destroy(dev, alt_ifname);
346736fbf1e5SJiri Pirko 		if (err)
346836fbf1e5SJiri Pirko 			return err;
346936fbf1e5SJiri Pirko 	} else {
347036fbf1e5SJiri Pirko 		WARN_ON(1);
347136fbf1e5SJiri Pirko 		return 0;
347236fbf1e5SJiri Pirko 	}
347336fbf1e5SJiri Pirko 
347436fbf1e5SJiri Pirko 	*changed = true;
347536fbf1e5SJiri Pirko 	return 0;
347636fbf1e5SJiri Pirko }
347736fbf1e5SJiri Pirko 
347836fbf1e5SJiri Pirko static int rtnl_linkprop(int cmd, struct sk_buff *skb, struct nlmsghdr *nlh,
347936fbf1e5SJiri Pirko 			 struct netlink_ext_ack *extack)
348036fbf1e5SJiri Pirko {
348136fbf1e5SJiri Pirko 	struct net *net = sock_net(skb->sk);
348236fbf1e5SJiri Pirko 	struct nlattr *tb[IFLA_MAX + 1];
348336fbf1e5SJiri Pirko 	struct net_device *dev;
348436fbf1e5SJiri Pirko 	struct ifinfomsg *ifm;
348536fbf1e5SJiri Pirko 	bool changed = false;
348636fbf1e5SJiri Pirko 	struct nlattr *attr;
348736fbf1e5SJiri Pirko 	int err, rem;
348836fbf1e5SJiri Pirko 
348936fbf1e5SJiri Pirko 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
349036fbf1e5SJiri Pirko 	if (err)
349136fbf1e5SJiri Pirko 		return err;
349236fbf1e5SJiri Pirko 
349336fbf1e5SJiri Pirko 	err = rtnl_ensure_unique_netns(tb, extack, true);
349436fbf1e5SJiri Pirko 	if (err)
349536fbf1e5SJiri Pirko 		return err;
349636fbf1e5SJiri Pirko 
349736fbf1e5SJiri Pirko 	ifm = nlmsg_data(nlh);
3498cc6090e9SJiri Pirko 	if (ifm->ifi_index > 0)
349936fbf1e5SJiri Pirko 		dev = __dev_get_by_index(net, ifm->ifi_index);
3500*76c9ac0eSJiri Pirko 	else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3501*76c9ac0eSJiri Pirko 		dev = rtnl_dev_get(net, tb[IFLA_IFNAME],
3502*76c9ac0eSJiri Pirko 				   tb[IFLA_ALT_IFNAME], NULL);
3503cc6090e9SJiri Pirko 	else
350436fbf1e5SJiri Pirko 		return -EINVAL;
350536fbf1e5SJiri Pirko 
350636fbf1e5SJiri Pirko 	if (!dev)
350736fbf1e5SJiri Pirko 		return -ENODEV;
350836fbf1e5SJiri Pirko 
350936fbf1e5SJiri Pirko 	if (!tb[IFLA_PROP_LIST])
351036fbf1e5SJiri Pirko 		return 0;
351136fbf1e5SJiri Pirko 
351236fbf1e5SJiri Pirko 	nla_for_each_nested(attr, tb[IFLA_PROP_LIST], rem) {
351336fbf1e5SJiri Pirko 		switch (nla_type(attr)) {
351436fbf1e5SJiri Pirko 		case IFLA_ALT_IFNAME:
351536fbf1e5SJiri Pirko 			err = rtnl_alt_ifname(cmd, dev, attr, &changed, extack);
351636fbf1e5SJiri Pirko 			if (err)
351736fbf1e5SJiri Pirko 				return err;
351836fbf1e5SJiri Pirko 			break;
351936fbf1e5SJiri Pirko 		}
352036fbf1e5SJiri Pirko 	}
352136fbf1e5SJiri Pirko 
352236fbf1e5SJiri Pirko 	if (changed)
352336fbf1e5SJiri Pirko 		netdev_state_change(dev);
352436fbf1e5SJiri Pirko 	return 0;
352536fbf1e5SJiri Pirko }
352636fbf1e5SJiri Pirko 
352736fbf1e5SJiri Pirko static int rtnl_newlinkprop(struct sk_buff *skb, struct nlmsghdr *nlh,
352836fbf1e5SJiri Pirko 			    struct netlink_ext_ack *extack)
352936fbf1e5SJiri Pirko {
353036fbf1e5SJiri Pirko 	return rtnl_linkprop(RTM_NEWLINKPROP, skb, nlh, extack);
353136fbf1e5SJiri Pirko }
353236fbf1e5SJiri Pirko 
353336fbf1e5SJiri Pirko static int rtnl_dellinkprop(struct sk_buff *skb, struct nlmsghdr *nlh,
353436fbf1e5SJiri Pirko 			    struct netlink_ext_ack *extack)
353536fbf1e5SJiri Pirko {
353636fbf1e5SJiri Pirko 	return rtnl_linkprop(RTM_DELLINKPROP, skb, nlh, extack);
353736fbf1e5SJiri Pirko }
353836fbf1e5SJiri Pirko 
3539115c9b81SGreg Rose static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
3540c7ac8679SGreg Rose {
3541115c9b81SGreg Rose 	struct net *net = sock_net(skb->sk);
3542115c9b81SGreg Rose 	struct net_device *dev;
3543115c9b81SGreg Rose 	struct nlattr *tb[IFLA_MAX+1];
3544115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
3545115c9b81SGreg Rose 	u16 min_ifinfo_dump_size = 0;
3546e5eca6d4SMichal Schmidt 	int hdrlen;
3547115c9b81SGreg Rose 
3548e5eca6d4SMichal Schmidt 	/* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
3549e5eca6d4SMichal Schmidt 	hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
3550e5eca6d4SMichal Schmidt 		 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
3551e5eca6d4SMichal Schmidt 
35528cb08174SJohannes Berg 	if (nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
3553115c9b81SGreg Rose 		if (tb[IFLA_EXT_MASK])
3554115c9b81SGreg Rose 			ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3555a4b64fbeSEric Dumazet 	}
3556115c9b81SGreg Rose 
3557115c9b81SGreg Rose 	if (!ext_filter_mask)
3558115c9b81SGreg Rose 		return NLMSG_GOODSIZE;
3559115c9b81SGreg Rose 	/*
3560115c9b81SGreg Rose 	 * traverse the list of net devices and compute the minimum
3561115c9b81SGreg Rose 	 * buffer size based upon the filter mask.
3562115c9b81SGreg Rose 	 */
35636853dd48SFlorian Westphal 	rcu_read_lock();
35646853dd48SFlorian Westphal 	for_each_netdev_rcu(net, dev) {
3565115c9b81SGreg Rose 		min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
3566115c9b81SGreg Rose 					     if_nlmsg_size(dev,
3567115c9b81SGreg Rose 						           ext_filter_mask));
3568115c9b81SGreg Rose 	}
35696853dd48SFlorian Westphal 	rcu_read_unlock();
3570115c9b81SGreg Rose 
357193af2056SZhang Shengju 	return nlmsg_total_size(min_ifinfo_dump_size);
3572c7ac8679SGreg Rose }
3573c7ac8679SGreg Rose 
357442bad1daSAdrian Bunk static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
35751da177e4SLinus Torvalds {
35761da177e4SLinus Torvalds 	int idx;
35771da177e4SLinus Torvalds 	int s_idx = cb->family;
357887ccbb1fSChristian Brauner 	int type = cb->nlh->nlmsg_type - RTM_BASE;
3579c63586dcSDavid Ahern 	int ret = 0;
35801da177e4SLinus Torvalds 
35811da177e4SLinus Torvalds 	if (s_idx == 0)
35821da177e4SLinus Torvalds 		s_idx = 1;
35836853dd48SFlorian Westphal 
358425239ceeSPatrick McHardy 	for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
3585addf9b90SFlorian Westphal 		struct rtnl_link **tab;
3586addf9b90SFlorian Westphal 		struct rtnl_link *link;
35876853dd48SFlorian Westphal 		rtnl_dumpit_func dumpit;
35886853dd48SFlorian Westphal 
35891da177e4SLinus Torvalds 		if (idx < s_idx || idx == PF_PACKET)
35901da177e4SLinus Torvalds 			continue;
35916853dd48SFlorian Westphal 
3592addf9b90SFlorian Westphal 		if (type < 0 || type >= RTM_NR_MSGTYPES)
35931da177e4SLinus Torvalds 			continue;
35946853dd48SFlorian Westphal 
3595addf9b90SFlorian Westphal 		tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]);
3596addf9b90SFlorian Westphal 		if (!tab)
3597addf9b90SFlorian Westphal 			continue;
3598addf9b90SFlorian Westphal 
3599addf9b90SFlorian Westphal 		link = tab[type];
3600addf9b90SFlorian Westphal 		if (!link)
3601addf9b90SFlorian Westphal 			continue;
3602addf9b90SFlorian Westphal 
3603addf9b90SFlorian Westphal 		dumpit = link->dumpit;
36046853dd48SFlorian Westphal 		if (!dumpit)
36056853dd48SFlorian Westphal 			continue;
36066853dd48SFlorian Westphal 
36070465277fSNicolas Dichtel 		if (idx > s_idx) {
36081da177e4SLinus Torvalds 			memset(&cb->args[0], 0, sizeof(cb->args));
36090465277fSNicolas Dichtel 			cb->prev_seq = 0;
36100465277fSNicolas Dichtel 			cb->seq = 0;
36110465277fSNicolas Dichtel 		}
3612c63586dcSDavid Ahern 		ret = dumpit(skb, cb);
36135e1acb4aSAlexey Kodanev 		if (ret)
36141da177e4SLinus Torvalds 			break;
36151da177e4SLinus Torvalds 	}
36161da177e4SLinus Torvalds 	cb->family = idx;
36171da177e4SLinus Torvalds 
3618c63586dcSDavid Ahern 	return skb->len ? : ret;
36191da177e4SLinus Torvalds }
36201da177e4SLinus Torvalds 
3621395eea6cSMahesh Bandewar struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
36223d3ea5afSVlad Yasevich 				       unsigned int change,
362338e01b30SNicolas Dichtel 				       u32 event, gfp_t flags, int *new_nsid,
362438e01b30SNicolas Dichtel 				       int new_ifindex)
36251da177e4SLinus Torvalds {
3626c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
36271da177e4SLinus Torvalds 	struct sk_buff *skb;
36280ec6d3f4SThomas Graf 	int err = -ENOBUFS;
3629c7ac8679SGreg Rose 	size_t if_info_size;
36301da177e4SLinus Torvalds 
36317f294054SAlexei Starovoitov 	skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
36320ec6d3f4SThomas Graf 	if (skb == NULL)
36330ec6d3f4SThomas Graf 		goto errout;
36341da177e4SLinus Torvalds 
363579e1ad14SJiri Benc 	err = rtnl_fill_ifinfo(skb, dev, dev_net(dev),
363679e1ad14SJiri Benc 			       type, 0, 0, change, 0, 0, event,
363738e01b30SNicolas Dichtel 			       new_nsid, new_ifindex, -1);
363826932566SPatrick McHardy 	if (err < 0) {
363926932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in if_nlmsg_size() */
364026932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
364126932566SPatrick McHardy 		kfree_skb(skb);
364226932566SPatrick McHardy 		goto errout;
364326932566SPatrick McHardy 	}
3644395eea6cSMahesh Bandewar 	return skb;
36450ec6d3f4SThomas Graf errout:
36460ec6d3f4SThomas Graf 	if (err < 0)
36474b3da706SEric W. Biederman 		rtnl_set_sk_err(net, RTNLGRP_LINK, err);
3648395eea6cSMahesh Bandewar 	return NULL;
3649395eea6cSMahesh Bandewar }
3650395eea6cSMahesh Bandewar 
3651395eea6cSMahesh Bandewar void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
3652395eea6cSMahesh Bandewar {
3653395eea6cSMahesh Bandewar 	struct net *net = dev_net(dev);
3654395eea6cSMahesh Bandewar 
3655395eea6cSMahesh Bandewar 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
3656395eea6cSMahesh Bandewar }
3657395eea6cSMahesh Bandewar 
36583d3ea5afSVlad Yasevich static void rtmsg_ifinfo_event(int type, struct net_device *dev,
36593d3ea5afSVlad Yasevich 			       unsigned int change, u32 event,
366038e01b30SNicolas Dichtel 			       gfp_t flags, int *new_nsid, int new_ifindex)
3661395eea6cSMahesh Bandewar {
3662395eea6cSMahesh Bandewar 	struct sk_buff *skb;
3663395eea6cSMahesh Bandewar 
3664ed2a80abSNicolas Dichtel 	if (dev->reg_state != NETREG_REGISTERED)
3665ed2a80abSNicolas Dichtel 		return;
3666ed2a80abSNicolas Dichtel 
366738e01b30SNicolas Dichtel 	skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid,
366838e01b30SNicolas Dichtel 				     new_ifindex);
3669395eea6cSMahesh Bandewar 	if (skb)
3670395eea6cSMahesh Bandewar 		rtmsg_ifinfo_send(skb, dev, flags);
36711da177e4SLinus Torvalds }
36723d3ea5afSVlad Yasevich 
36733d3ea5afSVlad Yasevich void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
36743d3ea5afSVlad Yasevich 		  gfp_t flags)
36753d3ea5afSVlad Yasevich {
367638e01b30SNicolas Dichtel 	rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
367738e01b30SNicolas Dichtel 			   NULL, 0);
36783d3ea5afSVlad Yasevich }
36791da177e4SLinus Torvalds 
36806621dd29SNicolas Dichtel void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change,
368138e01b30SNicolas Dichtel 			 gfp_t flags, int *new_nsid, int new_ifindex)
36826621dd29SNicolas Dichtel {
36836621dd29SNicolas Dichtel 	rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
368438e01b30SNicolas Dichtel 			   new_nsid, new_ifindex);
36856621dd29SNicolas Dichtel }
36866621dd29SNicolas Dichtel 
3687d83b0603SJohn Fastabend static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
3688d83b0603SJohn Fastabend 				   struct net_device *dev,
36891e53d5bbSHubert Sokolowski 				   u8 *addr, u16 vid, u32 pid, u32 seq,
36901c104a6bSNicolas Dichtel 				   int type, unsigned int flags,
3691b3379041SHubert Sokolowski 				   int nlflags, u16 ndm_state)
3692d83b0603SJohn Fastabend {
3693d83b0603SJohn Fastabend 	struct nlmsghdr *nlh;
3694d83b0603SJohn Fastabend 	struct ndmsg *ndm;
3695d83b0603SJohn Fastabend 
36961c104a6bSNicolas Dichtel 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
3697d83b0603SJohn Fastabend 	if (!nlh)
3698d83b0603SJohn Fastabend 		return -EMSGSIZE;
3699d83b0603SJohn Fastabend 
3700d83b0603SJohn Fastabend 	ndm = nlmsg_data(nlh);
3701d83b0603SJohn Fastabend 	ndm->ndm_family  = AF_BRIDGE;
3702d83b0603SJohn Fastabend 	ndm->ndm_pad1	 = 0;
3703d83b0603SJohn Fastabend 	ndm->ndm_pad2    = 0;
3704d83b0603SJohn Fastabend 	ndm->ndm_flags	 = flags;
3705d83b0603SJohn Fastabend 	ndm->ndm_type	 = 0;
3706d83b0603SJohn Fastabend 	ndm->ndm_ifindex = dev->ifindex;
3707b3379041SHubert Sokolowski 	ndm->ndm_state   = ndm_state;
3708d83b0603SJohn Fastabend 
3709d83b0603SJohn Fastabend 	if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
3710d83b0603SJohn Fastabend 		goto nla_put_failure;
37111e53d5bbSHubert Sokolowski 	if (vid)
37121e53d5bbSHubert Sokolowski 		if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
37131e53d5bbSHubert Sokolowski 			goto nla_put_failure;
3714d83b0603SJohn Fastabend 
3715053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
3716053c095aSJohannes Berg 	return 0;
3717d83b0603SJohn Fastabend 
3718d83b0603SJohn Fastabend nla_put_failure:
3719d83b0603SJohn Fastabend 	nlmsg_cancel(skb, nlh);
3720d83b0603SJohn Fastabend 	return -EMSGSIZE;
3721d83b0603SJohn Fastabend }
3722d83b0603SJohn Fastabend 
37233ff661c3SJohn Fastabend static inline size_t rtnl_fdb_nlmsg_size(void)
37243ff661c3SJohn Fastabend {
3725f82ef3e1SSabrina Dubroca 	return NLMSG_ALIGN(sizeof(struct ndmsg)) +
3726f82ef3e1SSabrina Dubroca 	       nla_total_size(ETH_ALEN) +	/* NDA_LLADDR */
3727f82ef3e1SSabrina Dubroca 	       nla_total_size(sizeof(u16)) +	/* NDA_VLAN */
3728f82ef3e1SSabrina Dubroca 	       0;
37293ff661c3SJohn Fastabend }
37303ff661c3SJohn Fastabend 
3731b3379041SHubert Sokolowski static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
3732b3379041SHubert Sokolowski 			    u16 ndm_state)
37333ff661c3SJohn Fastabend {
37343ff661c3SJohn Fastabend 	struct net *net = dev_net(dev);
37353ff661c3SJohn Fastabend 	struct sk_buff *skb;
37363ff661c3SJohn Fastabend 	int err = -ENOBUFS;
37373ff661c3SJohn Fastabend 
37383ff661c3SJohn Fastabend 	skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
37393ff661c3SJohn Fastabend 	if (!skb)
37403ff661c3SJohn Fastabend 		goto errout;
37413ff661c3SJohn Fastabend 
37421e53d5bbSHubert Sokolowski 	err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
3743b3379041SHubert Sokolowski 				      0, 0, type, NTF_SELF, 0, ndm_state);
37443ff661c3SJohn Fastabend 	if (err < 0) {
37453ff661c3SJohn Fastabend 		kfree_skb(skb);
37463ff661c3SJohn Fastabend 		goto errout;
37473ff661c3SJohn Fastabend 	}
37483ff661c3SJohn Fastabend 
37493ff661c3SJohn Fastabend 	rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
37503ff661c3SJohn Fastabend 	return;
37513ff661c3SJohn Fastabend errout:
37523ff661c3SJohn Fastabend 	rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
37533ff661c3SJohn Fastabend }
37543ff661c3SJohn Fastabend 
3755a986967eSBart Van Assche /*
3756090096bfSVlad Yasevich  * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
3757090096bfSVlad Yasevich  */
3758090096bfSVlad Yasevich int ndo_dflt_fdb_add(struct ndmsg *ndm,
3759090096bfSVlad Yasevich 		     struct nlattr *tb[],
3760090096bfSVlad Yasevich 		     struct net_device *dev,
3761f6f6424bSJiri Pirko 		     const unsigned char *addr, u16 vid,
3762090096bfSVlad Yasevich 		     u16 flags)
3763090096bfSVlad Yasevich {
3764090096bfSVlad Yasevich 	int err = -EINVAL;
3765090096bfSVlad Yasevich 
3766090096bfSVlad Yasevich 	/* If aging addresses are supported device will need to
3767090096bfSVlad Yasevich 	 * implement its own handler for this.
3768090096bfSVlad Yasevich 	 */
3769090096bfSVlad Yasevich 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
3770090096bfSVlad Yasevich 		pr_info("%s: FDB only supports static addresses\n", dev->name);
3771090096bfSVlad Yasevich 		return err;
3772090096bfSVlad Yasevich 	}
3773090096bfSVlad Yasevich 
377465891feaSOr Gerlitz 	if (vid) {
377565891feaSOr Gerlitz 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
377665891feaSOr Gerlitz 		return err;
377765891feaSOr Gerlitz 	}
377865891feaSOr Gerlitz 
3779090096bfSVlad Yasevich 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3780090096bfSVlad Yasevich 		err = dev_uc_add_excl(dev, addr);
3781090096bfSVlad Yasevich 	else if (is_multicast_ether_addr(addr))
3782090096bfSVlad Yasevich 		err = dev_mc_add_excl(dev, addr);
3783090096bfSVlad Yasevich 
3784090096bfSVlad Yasevich 	/* Only return duplicate errors if NLM_F_EXCL is set */
3785090096bfSVlad Yasevich 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
3786090096bfSVlad Yasevich 		err = 0;
3787090096bfSVlad Yasevich 
3788090096bfSVlad Yasevich 	return err;
3789090096bfSVlad Yasevich }
3790090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_add);
3791090096bfSVlad Yasevich 
3792b88d12e4SFlorian Westphal static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid,
3793b88d12e4SFlorian Westphal 			 struct netlink_ext_ack *extack)
3794f6f6424bSJiri Pirko {
3795f6f6424bSJiri Pirko 	u16 vid = 0;
3796f6f6424bSJiri Pirko 
3797f6f6424bSJiri Pirko 	if (vlan_attr) {
3798f6f6424bSJiri Pirko 		if (nla_len(vlan_attr) != sizeof(u16)) {
3799b88d12e4SFlorian Westphal 			NL_SET_ERR_MSG(extack, "invalid vlan attribute size");
3800f6f6424bSJiri Pirko 			return -EINVAL;
3801f6f6424bSJiri Pirko 		}
3802f6f6424bSJiri Pirko 
3803f6f6424bSJiri Pirko 		vid = nla_get_u16(vlan_attr);
3804f6f6424bSJiri Pirko 
3805f6f6424bSJiri Pirko 		if (!vid || vid >= VLAN_VID_MASK) {
3806b88d12e4SFlorian Westphal 			NL_SET_ERR_MSG(extack, "invalid vlan id");
3807f6f6424bSJiri Pirko 			return -EINVAL;
3808f6f6424bSJiri Pirko 		}
3809f6f6424bSJiri Pirko 	}
3810f6f6424bSJiri Pirko 	*p_vid = vid;
3811f6f6424bSJiri Pirko 	return 0;
3812f6f6424bSJiri Pirko }
3813f6f6424bSJiri Pirko 
3814c21ef3e3SDavid Ahern static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
3815c21ef3e3SDavid Ahern 			struct netlink_ext_ack *extack)
381677162022SJohn Fastabend {
381777162022SJohn Fastabend 	struct net *net = sock_net(skb->sk);
381877162022SJohn Fastabend 	struct ndmsg *ndm;
381977162022SJohn Fastabend 	struct nlattr *tb[NDA_MAX+1];
382077162022SJohn Fastabend 	struct net_device *dev;
382177162022SJohn Fastabend 	u8 *addr;
3822f6f6424bSJiri Pirko 	u16 vid;
382377162022SJohn Fastabend 	int err;
382477162022SJohn Fastabend 
38258cb08174SJohannes Berg 	err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL,
38268cb08174SJohannes Berg 				     extack);
382777162022SJohn Fastabend 	if (err < 0)
382877162022SJohn Fastabend 		return err;
382977162022SJohn Fastabend 
383077162022SJohn Fastabend 	ndm = nlmsg_data(nlh);
383177162022SJohn Fastabend 	if (ndm->ndm_ifindex == 0) {
3832b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "invalid ifindex");
383377162022SJohn Fastabend 		return -EINVAL;
383477162022SJohn Fastabend 	}
383577162022SJohn Fastabend 
383677162022SJohn Fastabend 	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
383777162022SJohn Fastabend 	if (dev == NULL) {
3838b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "unknown ifindex");
383977162022SJohn Fastabend 		return -ENODEV;
384077162022SJohn Fastabend 	}
384177162022SJohn Fastabend 
384277162022SJohn Fastabend 	if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
3843b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "invalid address");
384477162022SJohn Fastabend 		return -EINVAL;
384577162022SJohn Fastabend 	}
384677162022SJohn Fastabend 
3847da715775SIdo Schimmel 	if (dev->type != ARPHRD_ETHER) {
3848da715775SIdo Schimmel 		NL_SET_ERR_MSG(extack, "FDB add only supported for Ethernet devices");
3849da715775SIdo Schimmel 		return -EINVAL;
3850da715775SIdo Schimmel 	}
3851da715775SIdo Schimmel 
385277162022SJohn Fastabend 	addr = nla_data(tb[NDA_LLADDR]);
385377162022SJohn Fastabend 
3854b88d12e4SFlorian Westphal 	err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
3855f6f6424bSJiri Pirko 	if (err)
3856f6f6424bSJiri Pirko 		return err;
3857f6f6424bSJiri Pirko 
385877162022SJohn Fastabend 	err = -EOPNOTSUPP;
385977162022SJohn Fastabend 
386077162022SJohn Fastabend 	/* Support fdb on master device the net/bridge default case */
386177162022SJohn Fastabend 	if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
386277162022SJohn Fastabend 	    (dev->priv_flags & IFF_BRIDGE_PORT)) {
3863898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3864898e5061SJiri Pirko 		const struct net_device_ops *ops = br_dev->netdev_ops;
3865898e5061SJiri Pirko 
3866f6f6424bSJiri Pirko 		err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
386787b0984eSPetr Machata 				       nlh->nlmsg_flags, extack);
386877162022SJohn Fastabend 		if (err)
386977162022SJohn Fastabend 			goto out;
387077162022SJohn Fastabend 		else
387177162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_MASTER;
387277162022SJohn Fastabend 	}
387377162022SJohn Fastabend 
387477162022SJohn Fastabend 	/* Embedded bridge, macvlan, and any other device support */
3875090096bfSVlad Yasevich 	if ((ndm->ndm_flags & NTF_SELF)) {
3876090096bfSVlad Yasevich 		if (dev->netdev_ops->ndo_fdb_add)
3877090096bfSVlad Yasevich 			err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
3878f6f6424bSJiri Pirko 							   vid,
387987b0984eSPetr Machata 							   nlh->nlmsg_flags,
388087b0984eSPetr Machata 							   extack);
3881090096bfSVlad Yasevich 		else
3882f6f6424bSJiri Pirko 			err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
388377162022SJohn Fastabend 					       nlh->nlmsg_flags);
388477162022SJohn Fastabend 
38853ff661c3SJohn Fastabend 		if (!err) {
3886b3379041SHubert Sokolowski 			rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
3887b3379041SHubert Sokolowski 					ndm->ndm_state);
388877162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_SELF;
388977162022SJohn Fastabend 		}
38903ff661c3SJohn Fastabend 	}
389177162022SJohn Fastabend out:
389277162022SJohn Fastabend 	return err;
389377162022SJohn Fastabend }
389477162022SJohn Fastabend 
3895a986967eSBart Van Assche /*
3896090096bfSVlad Yasevich  * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
3897090096bfSVlad Yasevich  */
3898090096bfSVlad Yasevich int ndo_dflt_fdb_del(struct ndmsg *ndm,
3899090096bfSVlad Yasevich 		     struct nlattr *tb[],
3900090096bfSVlad Yasevich 		     struct net_device *dev,
3901f6f6424bSJiri Pirko 		     const unsigned char *addr, u16 vid)
3902090096bfSVlad Yasevich {
3903c8a89c4aSAlexander Duyck 	int err = -EINVAL;
3904090096bfSVlad Yasevich 
3905090096bfSVlad Yasevich 	/* If aging addresses are supported device will need to
3906090096bfSVlad Yasevich 	 * implement its own handler for this.
3907090096bfSVlad Yasevich 	 */
390864535993SSridhar Samudrala 	if (!(ndm->ndm_state & NUD_PERMANENT)) {
3909090096bfSVlad Yasevich 		pr_info("%s: FDB only supports static addresses\n", dev->name);
3910c8a89c4aSAlexander Duyck 		return err;
3911090096bfSVlad Yasevich 	}
3912090096bfSVlad Yasevich 
3913090096bfSVlad Yasevich 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3914090096bfSVlad Yasevich 		err = dev_uc_del(dev, addr);
3915090096bfSVlad Yasevich 	else if (is_multicast_ether_addr(addr))
3916090096bfSVlad Yasevich 		err = dev_mc_del(dev, addr);
3917090096bfSVlad Yasevich 
3918090096bfSVlad Yasevich 	return err;
3919090096bfSVlad Yasevich }
3920090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_del);
3921090096bfSVlad Yasevich 
3922c21ef3e3SDavid Ahern static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
3923c21ef3e3SDavid Ahern 			struct netlink_ext_ack *extack)
392477162022SJohn Fastabend {
392577162022SJohn Fastabend 	struct net *net = sock_net(skb->sk);
392677162022SJohn Fastabend 	struct ndmsg *ndm;
39271690be63SVlad Yasevich 	struct nlattr *tb[NDA_MAX+1];
392877162022SJohn Fastabend 	struct net_device *dev;
392977162022SJohn Fastabend 	int err = -EINVAL;
393077162022SJohn Fastabend 	__u8 *addr;
3931f6f6424bSJiri Pirko 	u16 vid;
393277162022SJohn Fastabend 
393390f62cf3SEric W. Biederman 	if (!netlink_capable(skb, CAP_NET_ADMIN))
39341690be63SVlad Yasevich 		return -EPERM;
39351690be63SVlad Yasevich 
39368cb08174SJohannes Berg 	err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL,
39378cb08174SJohannes Berg 				     extack);
39381690be63SVlad Yasevich 	if (err < 0)
39391690be63SVlad Yasevich 		return err;
394077162022SJohn Fastabend 
394177162022SJohn Fastabend 	ndm = nlmsg_data(nlh);
394277162022SJohn Fastabend 	if (ndm->ndm_ifindex == 0) {
3943b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "invalid ifindex");
394477162022SJohn Fastabend 		return -EINVAL;
394577162022SJohn Fastabend 	}
394677162022SJohn Fastabend 
394777162022SJohn Fastabend 	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
394877162022SJohn Fastabend 	if (dev == NULL) {
3949b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "unknown ifindex");
395077162022SJohn Fastabend 		return -ENODEV;
395177162022SJohn Fastabend 	}
395277162022SJohn Fastabend 
39531690be63SVlad Yasevich 	if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
3954b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "invalid address");
395577162022SJohn Fastabend 		return -EINVAL;
395677162022SJohn Fastabend 	}
395777162022SJohn Fastabend 
3958da715775SIdo Schimmel 	if (dev->type != ARPHRD_ETHER) {
3959da715775SIdo Schimmel 		NL_SET_ERR_MSG(extack, "FDB delete only supported for Ethernet devices");
3960da715775SIdo Schimmel 		return -EINVAL;
3961da715775SIdo Schimmel 	}
3962da715775SIdo Schimmel 
39631690be63SVlad Yasevich 	addr = nla_data(tb[NDA_LLADDR]);
39641690be63SVlad Yasevich 
3965b88d12e4SFlorian Westphal 	err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
3966f6f6424bSJiri Pirko 	if (err)
3967f6f6424bSJiri Pirko 		return err;
3968f6f6424bSJiri Pirko 
396977162022SJohn Fastabend 	err = -EOPNOTSUPP;
397077162022SJohn Fastabend 
397177162022SJohn Fastabend 	/* Support fdb on master device the net/bridge default case */
397277162022SJohn Fastabend 	if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
397377162022SJohn Fastabend 	    (dev->priv_flags & IFF_BRIDGE_PORT)) {
3974898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3975898e5061SJiri Pirko 		const struct net_device_ops *ops = br_dev->netdev_ops;
397677162022SJohn Fastabend 
3977898e5061SJiri Pirko 		if (ops->ndo_fdb_del)
3978f6f6424bSJiri Pirko 			err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
397977162022SJohn Fastabend 
398077162022SJohn Fastabend 		if (err)
398177162022SJohn Fastabend 			goto out;
398277162022SJohn Fastabend 		else
398377162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_MASTER;
398477162022SJohn Fastabend 	}
398577162022SJohn Fastabend 
398677162022SJohn Fastabend 	/* Embedded bridge, macvlan, and any other device support */
3987090096bfSVlad Yasevich 	if (ndm->ndm_flags & NTF_SELF) {
3988090096bfSVlad Yasevich 		if (dev->netdev_ops->ndo_fdb_del)
3989f6f6424bSJiri Pirko 			err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
3990f6f6424bSJiri Pirko 							   vid);
3991090096bfSVlad Yasevich 		else
3992f6f6424bSJiri Pirko 			err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
399377162022SJohn Fastabend 
39943ff661c3SJohn Fastabend 		if (!err) {
3995b3379041SHubert Sokolowski 			rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
3996b3379041SHubert Sokolowski 					ndm->ndm_state);
399777162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_SELF;
399877162022SJohn Fastabend 		}
39993ff661c3SJohn Fastabend 	}
400077162022SJohn Fastabend out:
400177162022SJohn Fastabend 	return err;
400277162022SJohn Fastabend }
400377162022SJohn Fastabend 
4004d83b0603SJohn Fastabend static int nlmsg_populate_fdb(struct sk_buff *skb,
4005d83b0603SJohn Fastabend 			      struct netlink_callback *cb,
4006d83b0603SJohn Fastabend 			      struct net_device *dev,
4007d83b0603SJohn Fastabend 			      int *idx,
4008d83b0603SJohn Fastabend 			      struct netdev_hw_addr_list *list)
4009d83b0603SJohn Fastabend {
4010d83b0603SJohn Fastabend 	struct netdev_hw_addr *ha;
4011d83b0603SJohn Fastabend 	int err;
401215e47304SEric W. Biederman 	u32 portid, seq;
4013d83b0603SJohn Fastabend 
401415e47304SEric W. Biederman 	portid = NETLINK_CB(cb->skb).portid;
4015d83b0603SJohn Fastabend 	seq = cb->nlh->nlmsg_seq;
4016d83b0603SJohn Fastabend 
4017d83b0603SJohn Fastabend 	list_for_each_entry(ha, &list->list, list) {
4018d297653dSRoopa Prabhu 		if (*idx < cb->args[2])
4019d83b0603SJohn Fastabend 			goto skip;
4020d83b0603SJohn Fastabend 
40211e53d5bbSHubert Sokolowski 		err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
4022a7a558feSJohn Fastabend 					      portid, seq,
40231c104a6bSNicolas Dichtel 					      RTM_NEWNEIGH, NTF_SELF,
4024b3379041SHubert Sokolowski 					      NLM_F_MULTI, NUD_PERMANENT);
4025d83b0603SJohn Fastabend 		if (err < 0)
4026d83b0603SJohn Fastabend 			return err;
4027d83b0603SJohn Fastabend skip:
4028d83b0603SJohn Fastabend 		*idx += 1;
4029d83b0603SJohn Fastabend 	}
4030d83b0603SJohn Fastabend 	return 0;
4031d83b0603SJohn Fastabend }
4032d83b0603SJohn Fastabend 
4033d83b0603SJohn Fastabend /**
40342c53040fSBen Hutchings  * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
4035a986967eSBart Van Assche  * @skb: socket buffer to store message in
4036a986967eSBart Van Assche  * @cb: netlink callback
4037d83b0603SJohn Fastabend  * @dev: netdevice
4038a986967eSBart Van Assche  * @filter_dev: ignored
4039a986967eSBart Van Assche  * @idx: the number of FDB table entries dumped is added to *@idx
4040d83b0603SJohn Fastabend  *
4041d83b0603SJohn Fastabend  * Default netdevice operation to dump the existing unicast address list.
404291f3e7b1SJohn Fastabend  * Returns number of addresses from list put in skb.
4043d83b0603SJohn Fastabend  */
4044d83b0603SJohn Fastabend int ndo_dflt_fdb_dump(struct sk_buff *skb,
4045d83b0603SJohn Fastabend 		      struct netlink_callback *cb,
4046d83b0603SJohn Fastabend 		      struct net_device *dev,
40475d5eacb3SJamal Hadi Salim 		      struct net_device *filter_dev,
4048d297653dSRoopa Prabhu 		      int *idx)
4049d83b0603SJohn Fastabend {
4050d83b0603SJohn Fastabend 	int err;
4051d83b0603SJohn Fastabend 
405268883893SEric Dumazet 	if (dev->type != ARPHRD_ETHER)
405368883893SEric Dumazet 		return -EINVAL;
405468883893SEric Dumazet 
4055d83b0603SJohn Fastabend 	netif_addr_lock_bh(dev);
4056d297653dSRoopa Prabhu 	err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
4057d83b0603SJohn Fastabend 	if (err)
4058d83b0603SJohn Fastabend 		goto out;
40592934c9dbSZhang Shengju 	err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
4060d83b0603SJohn Fastabend out:
4061d83b0603SJohn Fastabend 	netif_addr_unlock_bh(dev);
4062d297653dSRoopa Prabhu 	return err;
4063d83b0603SJohn Fastabend }
4064d83b0603SJohn Fastabend EXPORT_SYMBOL(ndo_dflt_fdb_dump);
4065d83b0603SJohn Fastabend 
40668c6e137fSDavid Ahern static int valid_fdb_dump_strict(const struct nlmsghdr *nlh,
40678c6e137fSDavid Ahern 				 int *br_idx, int *brport_idx,
40688c6e137fSDavid Ahern 				 struct netlink_ext_ack *extack)
40698c6e137fSDavid Ahern {
40708c6e137fSDavid Ahern 	struct nlattr *tb[NDA_MAX + 1];
40718c6e137fSDavid Ahern 	struct ndmsg *ndm;
40728c6e137fSDavid Ahern 	int err, i;
40738c6e137fSDavid Ahern 
40748c6e137fSDavid Ahern 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
40758c6e137fSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid header for fdb dump request");
40768c6e137fSDavid Ahern 		return -EINVAL;
40778c6e137fSDavid Ahern 	}
40788c6e137fSDavid Ahern 
40798c6e137fSDavid Ahern 	ndm = nlmsg_data(nlh);
40808c6e137fSDavid Ahern 	if (ndm->ndm_pad1  || ndm->ndm_pad2  || ndm->ndm_state ||
40818c6e137fSDavid Ahern 	    ndm->ndm_flags || ndm->ndm_type) {
40828c6e137fSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid values in header for fbd dump request");
40838c6e137fSDavid Ahern 		return -EINVAL;
40848c6e137fSDavid Ahern 	}
40858c6e137fSDavid Ahern 
40868cb08174SJohannes Berg 	err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
40878cb08174SJohannes Berg 					    NDA_MAX, NULL, extack);
40888c6e137fSDavid Ahern 	if (err < 0)
40898c6e137fSDavid Ahern 		return err;
40908c6e137fSDavid Ahern 
40918c6e137fSDavid Ahern 	*brport_idx = ndm->ndm_ifindex;
40928c6e137fSDavid Ahern 	for (i = 0; i <= NDA_MAX; ++i) {
40938c6e137fSDavid Ahern 		if (!tb[i])
40948c6e137fSDavid Ahern 			continue;
40958c6e137fSDavid Ahern 
40968c6e137fSDavid Ahern 		switch (i) {
40978c6e137fSDavid Ahern 		case NDA_IFINDEX:
40988c6e137fSDavid Ahern 			if (nla_len(tb[i]) != sizeof(u32)) {
40998c6e137fSDavid Ahern 				NL_SET_ERR_MSG(extack, "Invalid IFINDEX attribute in fdb dump request");
41008c6e137fSDavid Ahern 				return -EINVAL;
41018c6e137fSDavid Ahern 			}
41028c6e137fSDavid Ahern 			*brport_idx = nla_get_u32(tb[NDA_IFINDEX]);
41038c6e137fSDavid Ahern 			break;
41048c6e137fSDavid Ahern 		case NDA_MASTER:
41058c6e137fSDavid Ahern 			if (nla_len(tb[i]) != sizeof(u32)) {
41068c6e137fSDavid Ahern 				NL_SET_ERR_MSG(extack, "Invalid MASTER attribute in fdb dump request");
41078c6e137fSDavid Ahern 				return -EINVAL;
41088c6e137fSDavid Ahern 			}
41098c6e137fSDavid Ahern 			*br_idx = nla_get_u32(tb[NDA_MASTER]);
41108c6e137fSDavid Ahern 			break;
41118c6e137fSDavid Ahern 		default:
41128c6e137fSDavid Ahern 			NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb dump request");
41138c6e137fSDavid Ahern 			return -EINVAL;
41148c6e137fSDavid Ahern 		}
41158c6e137fSDavid Ahern 	}
41168c6e137fSDavid Ahern 
41178c6e137fSDavid Ahern 	return 0;
41188c6e137fSDavid Ahern }
41198c6e137fSDavid Ahern 
41208dfbda19SDavid Ahern static int valid_fdb_dump_legacy(const struct nlmsghdr *nlh,
41218dfbda19SDavid Ahern 				 int *br_idx, int *brport_idx,
41228dfbda19SDavid Ahern 				 struct netlink_ext_ack *extack)
41238dfbda19SDavid Ahern {
41248dfbda19SDavid Ahern 	struct nlattr *tb[IFLA_MAX+1];
41258dfbda19SDavid Ahern 	int err;
41268dfbda19SDavid Ahern 
41278dfbda19SDavid Ahern 	/* A hack to preserve kernel<->userspace interface.
41288dfbda19SDavid Ahern 	 * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0.
41298dfbda19SDavid Ahern 	 * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails.
41308dfbda19SDavid Ahern 	 * So, check for ndmsg with an optional u32 attribute (not used here).
41318dfbda19SDavid Ahern 	 * Fortunately these sizes don't conflict with the size of ifinfomsg
41328dfbda19SDavid Ahern 	 * with an optional attribute.
41338dfbda19SDavid Ahern 	 */
41348dfbda19SDavid Ahern 	if (nlmsg_len(nlh) != sizeof(struct ndmsg) &&
41358dfbda19SDavid Ahern 	    (nlmsg_len(nlh) != sizeof(struct ndmsg) +
41368dfbda19SDavid Ahern 	     nla_attr_size(sizeof(u32)))) {
41374565d7e5SDavid Ahern 		struct ifinfomsg *ifm;
41384565d7e5SDavid Ahern 
41398cb08174SJohannes Berg 		err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
41408cb08174SJohannes Berg 					     tb, IFLA_MAX, ifla_policy,
41418cb08174SJohannes Berg 					     extack);
41428dfbda19SDavid Ahern 		if (err < 0) {
41438dfbda19SDavid Ahern 			return -EINVAL;
41448dfbda19SDavid Ahern 		} else if (err == 0) {
41458dfbda19SDavid Ahern 			if (tb[IFLA_MASTER])
41468dfbda19SDavid Ahern 				*br_idx = nla_get_u32(tb[IFLA_MASTER]);
41478dfbda19SDavid Ahern 		}
41488dfbda19SDavid Ahern 
41494565d7e5SDavid Ahern 		ifm = nlmsg_data(nlh);
41508dfbda19SDavid Ahern 		*brport_idx = ifm->ifi_index;
41518dfbda19SDavid Ahern 	}
41528dfbda19SDavid Ahern 	return 0;
41538dfbda19SDavid Ahern }
41548dfbda19SDavid Ahern 
415577162022SJohn Fastabend static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
415677162022SJohn Fastabend {
415777162022SJohn Fastabend 	struct net_device *dev;
41585e6d2435SJamal Hadi Salim 	struct net_device *br_dev = NULL;
41595e6d2435SJamal Hadi Salim 	const struct net_device_ops *ops = NULL;
41605e6d2435SJamal Hadi Salim 	const struct net_device_ops *cops = NULL;
41615e6d2435SJamal Hadi Salim 	struct net *net = sock_net(skb->sk);
4162d297653dSRoopa Prabhu 	struct hlist_head *head;
41635e6d2435SJamal Hadi Salim 	int brport_idx = 0;
41645e6d2435SJamal Hadi Salim 	int br_idx = 0;
4165d297653dSRoopa Prabhu 	int h, s_h;
4166d297653dSRoopa Prabhu 	int idx = 0, s_idx;
4167d297653dSRoopa Prabhu 	int err = 0;
4168d297653dSRoopa Prabhu 	int fidx = 0;
416977162022SJohn Fastabend 
41708c6e137fSDavid Ahern 	if (cb->strict_check)
41718c6e137fSDavid Ahern 		err = valid_fdb_dump_strict(cb->nlh, &br_idx, &brport_idx,
41728c6e137fSDavid Ahern 					    cb->extack);
41738c6e137fSDavid Ahern 	else
41748dfbda19SDavid Ahern 		err = valid_fdb_dump_legacy(cb->nlh, &br_idx, &brport_idx,
41758dfbda19SDavid Ahern 					    cb->extack);
41768dfbda19SDavid Ahern 	if (err < 0)
41778dfbda19SDavid Ahern 		return err;
41785e6d2435SJamal Hadi Salim 
41795e6d2435SJamal Hadi Salim 	if (br_idx) {
41805e6d2435SJamal Hadi Salim 		br_dev = __dev_get_by_index(net, br_idx);
41815e6d2435SJamal Hadi Salim 		if (!br_dev)
41825e6d2435SJamal Hadi Salim 			return -ENODEV;
41835e6d2435SJamal Hadi Salim 
4184898e5061SJiri Pirko 		ops = br_dev->netdev_ops;
41855e6d2435SJamal Hadi Salim 	}
41865e6d2435SJamal Hadi Salim 
4187d297653dSRoopa Prabhu 	s_h = cb->args[0];
4188d297653dSRoopa Prabhu 	s_idx = cb->args[1];
4189d297653dSRoopa Prabhu 
4190d297653dSRoopa Prabhu 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4191d297653dSRoopa Prabhu 		idx = 0;
4192d297653dSRoopa Prabhu 		head = &net->dev_index_head[h];
4193d297653dSRoopa Prabhu 		hlist_for_each_entry(dev, head, index_hlist) {
4194d297653dSRoopa Prabhu 
41955e6d2435SJamal Hadi Salim 			if (brport_idx && (dev->ifindex != brport_idx))
41965e6d2435SJamal Hadi Salim 				continue;
41975e6d2435SJamal Hadi Salim 
41985e6d2435SJamal Hadi Salim 			if (!br_idx) { /* user did not specify a specific bridge */
41995e6d2435SJamal Hadi Salim 				if (dev->priv_flags & IFF_BRIDGE_PORT) {
42005e6d2435SJamal Hadi Salim 					br_dev = netdev_master_upper_dev_get(dev);
42015e6d2435SJamal Hadi Salim 					cops = br_dev->netdev_ops;
42025e6d2435SJamal Hadi Salim 				}
42035e6d2435SJamal Hadi Salim 			} else {
42045e6d2435SJamal Hadi Salim 				if (dev != br_dev &&
42055e6d2435SJamal Hadi Salim 				    !(dev->priv_flags & IFF_BRIDGE_PORT))
42065e6d2435SJamal Hadi Salim 					continue;
42075e6d2435SJamal Hadi Salim 
42085e6d2435SJamal Hadi Salim 				if (br_dev != netdev_master_upper_dev_get(dev) &&
42095e6d2435SJamal Hadi Salim 				    !(dev->priv_flags & IFF_EBRIDGE))
42105e6d2435SJamal Hadi Salim 					continue;
42115e6d2435SJamal Hadi Salim 				cops = ops;
42125e6d2435SJamal Hadi Salim 			}
42135e6d2435SJamal Hadi Salim 
4214d297653dSRoopa Prabhu 			if (idx < s_idx)
4215d297653dSRoopa Prabhu 				goto cont;
4216d297653dSRoopa Prabhu 
42175e6d2435SJamal Hadi Salim 			if (dev->priv_flags & IFF_BRIDGE_PORT) {
4218d297653dSRoopa Prabhu 				if (cops && cops->ndo_fdb_dump) {
4219d297653dSRoopa Prabhu 					err = cops->ndo_fdb_dump(skb, cb,
4220d297653dSRoopa Prabhu 								br_dev, dev,
4221d297653dSRoopa Prabhu 								&fidx);
4222d297653dSRoopa Prabhu 					if (err == -EMSGSIZE)
4223d297653dSRoopa Prabhu 						goto out;
422477162022SJohn Fastabend 				}
4225d297653dSRoopa Prabhu 			}
422677162022SJohn Fastabend 
42275e6d2435SJamal Hadi Salim 			if (dev->netdev_ops->ndo_fdb_dump)
4228d297653dSRoopa Prabhu 				err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
4229d297653dSRoopa Prabhu 								    dev, NULL,
4230d297653dSRoopa Prabhu 								    &fidx);
42316cb69742SHubert Sokolowski 			else
4232d297653dSRoopa Prabhu 				err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
4233d297653dSRoopa Prabhu 							&fidx);
4234d297653dSRoopa Prabhu 			if (err == -EMSGSIZE)
4235d297653dSRoopa Prabhu 				goto out;
42365e6d2435SJamal Hadi Salim 
42375e6d2435SJamal Hadi Salim 			cops = NULL;
4238d297653dSRoopa Prabhu 
4239d297653dSRoopa Prabhu 			/* reset fdb offset to 0 for rest of the interfaces */
4240d297653dSRoopa Prabhu 			cb->args[2] = 0;
4241d297653dSRoopa Prabhu 			fidx = 0;
4242d297653dSRoopa Prabhu cont:
4243d297653dSRoopa Prabhu 			idx++;
4244d297653dSRoopa Prabhu 		}
424577162022SJohn Fastabend 	}
424677162022SJohn Fastabend 
4247d297653dSRoopa Prabhu out:
4248d297653dSRoopa Prabhu 	cb->args[0] = h;
4249d297653dSRoopa Prabhu 	cb->args[1] = idx;
4250d297653dSRoopa Prabhu 	cb->args[2] = fidx;
4251d297653dSRoopa Prabhu 
425277162022SJohn Fastabend 	return skb->len;
425377162022SJohn Fastabend }
425477162022SJohn Fastabend 
42555b2f94b2SRoopa Prabhu static int valid_fdb_get_strict(const struct nlmsghdr *nlh,
42565b2f94b2SRoopa Prabhu 				struct nlattr **tb, u8 *ndm_flags,
42575b2f94b2SRoopa Prabhu 				int *br_idx, int *brport_idx, u8 **addr,
42585b2f94b2SRoopa Prabhu 				u16 *vid, struct netlink_ext_ack *extack)
42595b2f94b2SRoopa Prabhu {
42605b2f94b2SRoopa Prabhu 	struct ndmsg *ndm;
42615b2f94b2SRoopa Prabhu 	int err, i;
42625b2f94b2SRoopa Prabhu 
42635b2f94b2SRoopa Prabhu 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
42645b2f94b2SRoopa Prabhu 		NL_SET_ERR_MSG(extack, "Invalid header for fdb get request");
42655b2f94b2SRoopa Prabhu 		return -EINVAL;
42665b2f94b2SRoopa Prabhu 	}
42675b2f94b2SRoopa Prabhu 
42685b2f94b2SRoopa Prabhu 	ndm = nlmsg_data(nlh);
42695b2f94b2SRoopa Prabhu 	if (ndm->ndm_pad1  || ndm->ndm_pad2  || ndm->ndm_state ||
42705b2f94b2SRoopa Prabhu 	    ndm->ndm_type) {
42715b2f94b2SRoopa Prabhu 		NL_SET_ERR_MSG(extack, "Invalid values in header for fdb get request");
42725b2f94b2SRoopa Prabhu 		return -EINVAL;
42735b2f94b2SRoopa Prabhu 	}
42745b2f94b2SRoopa Prabhu 
42755b2f94b2SRoopa Prabhu 	if (ndm->ndm_flags & ~(NTF_MASTER | NTF_SELF)) {
42765b2f94b2SRoopa Prabhu 		NL_SET_ERR_MSG(extack, "Invalid flags in header for fdb get request");
42775b2f94b2SRoopa Prabhu 		return -EINVAL;
42785b2f94b2SRoopa Prabhu 	}
42795b2f94b2SRoopa Prabhu 
42808cb08174SJohannes Berg 	err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
42818cb08174SJohannes Berg 					    NDA_MAX, nda_policy, extack);
42825b2f94b2SRoopa Prabhu 	if (err < 0)
42835b2f94b2SRoopa Prabhu 		return err;
42845b2f94b2SRoopa Prabhu 
42855b2f94b2SRoopa Prabhu 	*ndm_flags = ndm->ndm_flags;
42865b2f94b2SRoopa Prabhu 	*brport_idx = ndm->ndm_ifindex;
42875b2f94b2SRoopa Prabhu 	for (i = 0; i <= NDA_MAX; ++i) {
42885b2f94b2SRoopa Prabhu 		if (!tb[i])
42895b2f94b2SRoopa Prabhu 			continue;
42905b2f94b2SRoopa Prabhu 
42915b2f94b2SRoopa Prabhu 		switch (i) {
42925b2f94b2SRoopa Prabhu 		case NDA_MASTER:
42935b2f94b2SRoopa Prabhu 			*br_idx = nla_get_u32(tb[i]);
42945b2f94b2SRoopa Prabhu 			break;
42955b2f94b2SRoopa Prabhu 		case NDA_LLADDR:
42965b2f94b2SRoopa Prabhu 			if (nla_len(tb[i]) != ETH_ALEN) {
42975b2f94b2SRoopa Prabhu 				NL_SET_ERR_MSG(extack, "Invalid address in fdb get request");
42985b2f94b2SRoopa Prabhu 				return -EINVAL;
42995b2f94b2SRoopa Prabhu 			}
43005b2f94b2SRoopa Prabhu 			*addr = nla_data(tb[i]);
43015b2f94b2SRoopa Prabhu 			break;
43025b2f94b2SRoopa Prabhu 		case NDA_VLAN:
43035b2f94b2SRoopa Prabhu 			err = fdb_vid_parse(tb[i], vid, extack);
43045b2f94b2SRoopa Prabhu 			if (err)
43055b2f94b2SRoopa Prabhu 				return err;
43065b2f94b2SRoopa Prabhu 			break;
43075b2f94b2SRoopa Prabhu 		case NDA_VNI:
43085b2f94b2SRoopa Prabhu 			break;
43095b2f94b2SRoopa Prabhu 		default:
43105b2f94b2SRoopa Prabhu 			NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb get request");
43115b2f94b2SRoopa Prabhu 			return -EINVAL;
43125b2f94b2SRoopa Prabhu 		}
43135b2f94b2SRoopa Prabhu 	}
43145b2f94b2SRoopa Prabhu 
43155b2f94b2SRoopa Prabhu 	return 0;
43165b2f94b2SRoopa Prabhu }
43175b2f94b2SRoopa Prabhu 
43185b2f94b2SRoopa Prabhu static int rtnl_fdb_get(struct sk_buff *in_skb, struct nlmsghdr *nlh,
43195b2f94b2SRoopa Prabhu 			struct netlink_ext_ack *extack)
43205b2f94b2SRoopa Prabhu {
43215b2f94b2SRoopa Prabhu 	struct net_device *dev = NULL, *br_dev = NULL;
43225b2f94b2SRoopa Prabhu 	const struct net_device_ops *ops = NULL;
43235b2f94b2SRoopa Prabhu 	struct net *net = sock_net(in_skb->sk);
43245b2f94b2SRoopa Prabhu 	struct nlattr *tb[NDA_MAX + 1];
43255b2f94b2SRoopa Prabhu 	struct sk_buff *skb;
43265b2f94b2SRoopa Prabhu 	int brport_idx = 0;
43275b2f94b2SRoopa Prabhu 	u8 ndm_flags = 0;
43285b2f94b2SRoopa Prabhu 	int br_idx = 0;
43295b2f94b2SRoopa Prabhu 	u8 *addr = NULL;
43305b2f94b2SRoopa Prabhu 	u16 vid = 0;
43315b2f94b2SRoopa Prabhu 	int err;
43325b2f94b2SRoopa Prabhu 
43335b2f94b2SRoopa Prabhu 	err = valid_fdb_get_strict(nlh, tb, &ndm_flags, &br_idx,
43345b2f94b2SRoopa Prabhu 				   &brport_idx, &addr, &vid, extack);
43355b2f94b2SRoopa Prabhu 	if (err < 0)
43365b2f94b2SRoopa Prabhu 		return err;
43375b2f94b2SRoopa Prabhu 
4338f989d03eSNikolay Aleksandrov 	if (!addr) {
4339f989d03eSNikolay Aleksandrov 		NL_SET_ERR_MSG(extack, "Missing lookup address for fdb get request");
4340f989d03eSNikolay Aleksandrov 		return -EINVAL;
4341f989d03eSNikolay Aleksandrov 	}
4342f989d03eSNikolay Aleksandrov 
43435b2f94b2SRoopa Prabhu 	if (brport_idx) {
43445b2f94b2SRoopa Prabhu 		dev = __dev_get_by_index(net, brport_idx);
43455b2f94b2SRoopa Prabhu 		if (!dev) {
43465b2f94b2SRoopa Prabhu 			NL_SET_ERR_MSG(extack, "Unknown device ifindex");
43475b2f94b2SRoopa Prabhu 			return -ENODEV;
43485b2f94b2SRoopa Prabhu 		}
43495b2f94b2SRoopa Prabhu 	}
43505b2f94b2SRoopa Prabhu 
43515b2f94b2SRoopa Prabhu 	if (br_idx) {
43525b2f94b2SRoopa Prabhu 		if (dev) {
43535b2f94b2SRoopa Prabhu 			NL_SET_ERR_MSG(extack, "Master and device are mutually exclusive");
43545b2f94b2SRoopa Prabhu 			return -EINVAL;
43555b2f94b2SRoopa Prabhu 		}
43565b2f94b2SRoopa Prabhu 
43575b2f94b2SRoopa Prabhu 		br_dev = __dev_get_by_index(net, br_idx);
43585b2f94b2SRoopa Prabhu 		if (!br_dev) {
43595b2f94b2SRoopa Prabhu 			NL_SET_ERR_MSG(extack, "Invalid master ifindex");
43605b2f94b2SRoopa Prabhu 			return -EINVAL;
43615b2f94b2SRoopa Prabhu 		}
43625b2f94b2SRoopa Prabhu 		ops = br_dev->netdev_ops;
43635b2f94b2SRoopa Prabhu 	}
43645b2f94b2SRoopa Prabhu 
43655b2f94b2SRoopa Prabhu 	if (dev) {
43665b2f94b2SRoopa Prabhu 		if (!ndm_flags || (ndm_flags & NTF_MASTER)) {
43675b2f94b2SRoopa Prabhu 			if (!(dev->priv_flags & IFF_BRIDGE_PORT)) {
43685b2f94b2SRoopa Prabhu 				NL_SET_ERR_MSG(extack, "Device is not a bridge port");
43695b2f94b2SRoopa Prabhu 				return -EINVAL;
43705b2f94b2SRoopa Prabhu 			}
43715b2f94b2SRoopa Prabhu 			br_dev = netdev_master_upper_dev_get(dev);
43725b2f94b2SRoopa Prabhu 			if (!br_dev) {
43735b2f94b2SRoopa Prabhu 				NL_SET_ERR_MSG(extack, "Master of device not found");
43745b2f94b2SRoopa Prabhu 				return -EINVAL;
43755b2f94b2SRoopa Prabhu 			}
43765b2f94b2SRoopa Prabhu 			ops = br_dev->netdev_ops;
43775b2f94b2SRoopa Prabhu 		} else {
43785b2f94b2SRoopa Prabhu 			if (!(ndm_flags & NTF_SELF)) {
43795b2f94b2SRoopa Prabhu 				NL_SET_ERR_MSG(extack, "Missing NTF_SELF");
43805b2f94b2SRoopa Prabhu 				return -EINVAL;
43815b2f94b2SRoopa Prabhu 			}
43825b2f94b2SRoopa Prabhu 			ops = dev->netdev_ops;
43835b2f94b2SRoopa Prabhu 		}
43845b2f94b2SRoopa Prabhu 	}
43855b2f94b2SRoopa Prabhu 
43865b2f94b2SRoopa Prabhu 	if (!br_dev && !dev) {
43875b2f94b2SRoopa Prabhu 		NL_SET_ERR_MSG(extack, "No device specified");
43885b2f94b2SRoopa Prabhu 		return -ENODEV;
43895b2f94b2SRoopa Prabhu 	}
43905b2f94b2SRoopa Prabhu 
43915b2f94b2SRoopa Prabhu 	if (!ops || !ops->ndo_fdb_get) {
43925b2f94b2SRoopa Prabhu 		NL_SET_ERR_MSG(extack, "Fdb get operation not supported by device");
43935b2f94b2SRoopa Prabhu 		return -EOPNOTSUPP;
43945b2f94b2SRoopa Prabhu 	}
43955b2f94b2SRoopa Prabhu 
43965b2f94b2SRoopa Prabhu 	skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
43975b2f94b2SRoopa Prabhu 	if (!skb)
43985b2f94b2SRoopa Prabhu 		return -ENOBUFS;
43995b2f94b2SRoopa Prabhu 
44005b2f94b2SRoopa Prabhu 	if (br_dev)
44015b2f94b2SRoopa Prabhu 		dev = br_dev;
44025b2f94b2SRoopa Prabhu 	err = ops->ndo_fdb_get(skb, tb, dev, addr, vid,
44035b2f94b2SRoopa Prabhu 			       NETLINK_CB(in_skb).portid,
44045b2f94b2SRoopa Prabhu 			       nlh->nlmsg_seq, extack);
44055b2f94b2SRoopa Prabhu 	if (err)
44065b2f94b2SRoopa Prabhu 		goto out;
44075b2f94b2SRoopa Prabhu 
44085b2f94b2SRoopa Prabhu 	return rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
44095b2f94b2SRoopa Prabhu out:
44105b2f94b2SRoopa Prabhu 	kfree_skb(skb);
44115b2f94b2SRoopa Prabhu 	return err;
44125b2f94b2SRoopa Prabhu }
44135b2f94b2SRoopa Prabhu 
44142c3c031cSScott Feldman static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
44152c3c031cSScott Feldman 			       unsigned int attrnum, unsigned int flag)
44162c3c031cSScott Feldman {
44172c3c031cSScott Feldman 	if (mask & flag)
44182c3c031cSScott Feldman 		return nla_put_u8(skb, attrnum, !!(flags & flag));
44192c3c031cSScott Feldman 	return 0;
44202c3c031cSScott Feldman }
44212c3c031cSScott Feldman 
4422815cccbfSJohn Fastabend int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
44232c3c031cSScott Feldman 			    struct net_device *dev, u16 mode,
44247d4f8d87SScott Feldman 			    u32 flags, u32 mask, int nlflags,
44257d4f8d87SScott Feldman 			    u32 filter_mask,
44267d4f8d87SScott Feldman 			    int (*vlan_fill)(struct sk_buff *skb,
44277d4f8d87SScott Feldman 					     struct net_device *dev,
44287d4f8d87SScott Feldman 					     u32 filter_mask))
4429815cccbfSJohn Fastabend {
4430815cccbfSJohn Fastabend 	struct nlmsghdr *nlh;
4431815cccbfSJohn Fastabend 	struct ifinfomsg *ifm;
4432815cccbfSJohn Fastabend 	struct nlattr *br_afspec;
44332c3c031cSScott Feldman 	struct nlattr *protinfo;
4434815cccbfSJohn Fastabend 	u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
4435898e5061SJiri Pirko 	struct net_device *br_dev = netdev_master_upper_dev_get(dev);
44367d4f8d87SScott Feldman 	int err = 0;
4437815cccbfSJohn Fastabend 
443846c264daSNicolas Dichtel 	nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
4439815cccbfSJohn Fastabend 	if (nlh == NULL)
4440815cccbfSJohn Fastabend 		return -EMSGSIZE;
4441815cccbfSJohn Fastabend 
4442815cccbfSJohn Fastabend 	ifm = nlmsg_data(nlh);
4443815cccbfSJohn Fastabend 	ifm->ifi_family = AF_BRIDGE;
4444815cccbfSJohn Fastabend 	ifm->__ifi_pad = 0;
4445815cccbfSJohn Fastabend 	ifm->ifi_type = dev->type;
4446815cccbfSJohn Fastabend 	ifm->ifi_index = dev->ifindex;
4447815cccbfSJohn Fastabend 	ifm->ifi_flags = dev_get_flags(dev);
4448815cccbfSJohn Fastabend 	ifm->ifi_change = 0;
4449815cccbfSJohn Fastabend 
4450815cccbfSJohn Fastabend 
4451815cccbfSJohn Fastabend 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
4452815cccbfSJohn Fastabend 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
4453815cccbfSJohn Fastabend 	    nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
4454898e5061SJiri Pirko 	    (br_dev &&
4455898e5061SJiri Pirko 	     nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
4456815cccbfSJohn Fastabend 	    (dev->addr_len &&
4457815cccbfSJohn Fastabend 	     nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
4458a54acb3aSNicolas Dichtel 	    (dev->ifindex != dev_get_iflink(dev) &&
4459a54acb3aSNicolas Dichtel 	     nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
4460815cccbfSJohn Fastabend 		goto nla_put_failure;
4461815cccbfSJohn Fastabend 
4462ae0be8deSMichal Kubecek 	br_afspec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
4463815cccbfSJohn Fastabend 	if (!br_afspec)
4464815cccbfSJohn Fastabend 		goto nla_put_failure;
4465815cccbfSJohn Fastabend 
44661d460b98SRoopa Prabhu 	if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
4467815cccbfSJohn Fastabend 		nla_nest_cancel(skb, br_afspec);
4468815cccbfSJohn Fastabend 		goto nla_put_failure;
4469815cccbfSJohn Fastabend 	}
44701d460b98SRoopa Prabhu 
44711d460b98SRoopa Prabhu 	if (mode != BRIDGE_MODE_UNDEF) {
44721d460b98SRoopa Prabhu 		if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
44731d460b98SRoopa Prabhu 			nla_nest_cancel(skb, br_afspec);
44741d460b98SRoopa Prabhu 			goto nla_put_failure;
44751d460b98SRoopa Prabhu 		}
44761d460b98SRoopa Prabhu 	}
44777d4f8d87SScott Feldman 	if (vlan_fill) {
44787d4f8d87SScott Feldman 		err = vlan_fill(skb, dev, filter_mask);
44797d4f8d87SScott Feldman 		if (err) {
44807d4f8d87SScott Feldman 			nla_nest_cancel(skb, br_afspec);
44817d4f8d87SScott Feldman 			goto nla_put_failure;
44827d4f8d87SScott Feldman 		}
44837d4f8d87SScott Feldman 	}
4484815cccbfSJohn Fastabend 	nla_nest_end(skb, br_afspec);
4485815cccbfSJohn Fastabend 
4486ae0be8deSMichal Kubecek 	protinfo = nla_nest_start(skb, IFLA_PROTINFO);
44872c3c031cSScott Feldman 	if (!protinfo)
44882c3c031cSScott Feldman 		goto nla_put_failure;
44892c3c031cSScott Feldman 
44902c3c031cSScott Feldman 	if (brport_nla_put_flag(skb, flags, mask,
44912c3c031cSScott Feldman 				IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
44922c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
44932c3c031cSScott Feldman 				IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
44942c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
44952c3c031cSScott Feldman 				IFLA_BRPORT_FAST_LEAVE,
44962c3c031cSScott Feldman 				BR_MULTICAST_FAST_LEAVE) ||
44972c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
44982c3c031cSScott Feldman 				IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
44992c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
45002c3c031cSScott Feldman 				IFLA_BRPORT_LEARNING, BR_LEARNING) ||
45012c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
45022c3c031cSScott Feldman 				IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
45032c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
45042c3c031cSScott Feldman 				IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
45052c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
45062c3c031cSScott Feldman 				IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
45072c3c031cSScott Feldman 		nla_nest_cancel(skb, protinfo);
45082c3c031cSScott Feldman 		goto nla_put_failure;
45092c3c031cSScott Feldman 	}
45102c3c031cSScott Feldman 
45112c3c031cSScott Feldman 	nla_nest_end(skb, protinfo);
45122c3c031cSScott Feldman 
4513053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
4514053c095aSJohannes Berg 	return 0;
4515815cccbfSJohn Fastabend nla_put_failure:
4516815cccbfSJohn Fastabend 	nlmsg_cancel(skb, nlh);
45177d4f8d87SScott Feldman 	return err ? err : -EMSGSIZE;
4518815cccbfSJohn Fastabend }
45197d4f8d87SScott Feldman EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
4520815cccbfSJohn Fastabend 
45212d011be8SDavid Ahern static int valid_bridge_getlink_req(const struct nlmsghdr *nlh,
45222d011be8SDavid Ahern 				    bool strict_check, u32 *filter_mask,
45232d011be8SDavid Ahern 				    struct netlink_ext_ack *extack)
45242d011be8SDavid Ahern {
45252d011be8SDavid Ahern 	struct nlattr *tb[IFLA_MAX+1];
45262d011be8SDavid Ahern 	int err, i;
45272d011be8SDavid Ahern 
45282d011be8SDavid Ahern 	if (strict_check) {
45292d011be8SDavid Ahern 		struct ifinfomsg *ifm;
45302d011be8SDavid Ahern 
45312d011be8SDavid Ahern 		if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
45322d011be8SDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid header for bridge link dump");
45332d011be8SDavid Ahern 			return -EINVAL;
45342d011be8SDavid Ahern 		}
45352d011be8SDavid Ahern 
45362d011be8SDavid Ahern 		ifm = nlmsg_data(nlh);
45372d011be8SDavid Ahern 		if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
45382d011be8SDavid Ahern 		    ifm->ifi_change || ifm->ifi_index) {
45392d011be8SDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid values in header for bridge link dump request");
45402d011be8SDavid Ahern 			return -EINVAL;
45412d011be8SDavid Ahern 		}
45422d011be8SDavid Ahern 
45438cb08174SJohannes Berg 		err = nlmsg_parse_deprecated_strict(nlh,
45448cb08174SJohannes Berg 						    sizeof(struct ifinfomsg),
45458cb08174SJohannes Berg 						    tb, IFLA_MAX, ifla_policy,
45468cb08174SJohannes Berg 						    extack);
45472d011be8SDavid Ahern 	} else {
45488cb08174SJohannes Berg 		err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
45498cb08174SJohannes Berg 					     tb, IFLA_MAX, ifla_policy,
45508cb08174SJohannes Berg 					     extack);
45512d011be8SDavid Ahern 	}
45522d011be8SDavid Ahern 	if (err < 0)
45532d011be8SDavid Ahern 		return err;
45542d011be8SDavid Ahern 
45552d011be8SDavid Ahern 	/* new attributes should only be added with strict checking */
45562d011be8SDavid Ahern 	for (i = 0; i <= IFLA_MAX; ++i) {
45572d011be8SDavid Ahern 		if (!tb[i])
45582d011be8SDavid Ahern 			continue;
45592d011be8SDavid Ahern 
45602d011be8SDavid Ahern 		switch (i) {
45612d011be8SDavid Ahern 		case IFLA_EXT_MASK:
45622d011be8SDavid Ahern 			*filter_mask = nla_get_u32(tb[i]);
45632d011be8SDavid Ahern 			break;
45642d011be8SDavid Ahern 		default:
45652d011be8SDavid Ahern 			if (strict_check) {
45662d011be8SDavid Ahern 				NL_SET_ERR_MSG(extack, "Unsupported attribute in bridge link dump request");
45672d011be8SDavid Ahern 				return -EINVAL;
45682d011be8SDavid Ahern 			}
45692d011be8SDavid Ahern 		}
45702d011be8SDavid Ahern 	}
45712d011be8SDavid Ahern 
45722d011be8SDavid Ahern 	return 0;
45732d011be8SDavid Ahern }
45742d011be8SDavid Ahern 
4575e5a55a89SJohn Fastabend static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
4576e5a55a89SJohn Fastabend {
45772d011be8SDavid Ahern 	const struct nlmsghdr *nlh = cb->nlh;
4578e5a55a89SJohn Fastabend 	struct net *net = sock_net(skb->sk);
4579e5a55a89SJohn Fastabend 	struct net_device *dev;
4580e5a55a89SJohn Fastabend 	int idx = 0;
4581e5a55a89SJohn Fastabend 	u32 portid = NETLINK_CB(cb->skb).portid;
45822d011be8SDavid Ahern 	u32 seq = nlh->nlmsg_seq;
45836cbdceebSVlad Yasevich 	u32 filter_mask = 0;
4584d64f69b0SRoopa Prabhu 	int err;
45856cbdceebSVlad Yasevich 
45862d011be8SDavid Ahern 	err = valid_bridge_getlink_req(nlh, cb->strict_check, &filter_mask,
45872d011be8SDavid Ahern 				       cb->extack);
45882d011be8SDavid Ahern 	if (err < 0 && cb->strict_check)
45892d011be8SDavid Ahern 		return err;
4590e5a55a89SJohn Fastabend 
4591e5a55a89SJohn Fastabend 	rcu_read_lock();
4592e5a55a89SJohn Fastabend 	for_each_netdev_rcu(net, dev) {
4593e5a55a89SJohn Fastabend 		const struct net_device_ops *ops = dev->netdev_ops;
4594898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4595e5a55a89SJohn Fastabend 
4596898e5061SJiri Pirko 		if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
4597d64f69b0SRoopa Prabhu 			if (idx >= cb->args[0]) {
4598d64f69b0SRoopa Prabhu 				err = br_dev->netdev_ops->ndo_bridge_getlink(
4599d64f69b0SRoopa Prabhu 						skb, portid, seq, dev,
4600d64f69b0SRoopa Prabhu 						filter_mask, NLM_F_MULTI);
4601f6c5775fSDavid Ahern 				if (err < 0 && err != -EOPNOTSUPP) {
4602f6c5775fSDavid Ahern 					if (likely(skb->len))
4603e5a55a89SJohn Fastabend 						break;
4604f6c5775fSDavid Ahern 
4605f6c5775fSDavid Ahern 					goto out_err;
4606f6c5775fSDavid Ahern 				}
4607d64f69b0SRoopa Prabhu 			}
4608e5a55a89SJohn Fastabend 			idx++;
4609e5a55a89SJohn Fastabend 		}
4610e5a55a89SJohn Fastabend 
4611e5a55a89SJohn Fastabend 		if (ops->ndo_bridge_getlink) {
4612d64f69b0SRoopa Prabhu 			if (idx >= cb->args[0]) {
4613d64f69b0SRoopa Prabhu 				err = ops->ndo_bridge_getlink(skb, portid,
4614d64f69b0SRoopa Prabhu 							      seq, dev,
461546c264daSNicolas Dichtel 							      filter_mask,
4616d64f69b0SRoopa Prabhu 							      NLM_F_MULTI);
4617f6c5775fSDavid Ahern 				if (err < 0 && err != -EOPNOTSUPP) {
4618f6c5775fSDavid Ahern 					if (likely(skb->len))
4619e5a55a89SJohn Fastabend 						break;
4620f6c5775fSDavid Ahern 
4621f6c5775fSDavid Ahern 					goto out_err;
4622f6c5775fSDavid Ahern 				}
4623d64f69b0SRoopa Prabhu 			}
4624e5a55a89SJohn Fastabend 			idx++;
4625e5a55a89SJohn Fastabend 		}
4626e5a55a89SJohn Fastabend 	}
4627f6c5775fSDavid Ahern 	err = skb->len;
4628f6c5775fSDavid Ahern out_err:
4629e5a55a89SJohn Fastabend 	rcu_read_unlock();
4630e5a55a89SJohn Fastabend 	cb->args[0] = idx;
4631e5a55a89SJohn Fastabend 
4632f6c5775fSDavid Ahern 	return err;
4633e5a55a89SJohn Fastabend }
4634e5a55a89SJohn Fastabend 
46352469ffd7SJohn Fastabend static inline size_t bridge_nlmsg_size(void)
46362469ffd7SJohn Fastabend {
46372469ffd7SJohn Fastabend 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
46382469ffd7SJohn Fastabend 		+ nla_total_size(IFNAMSIZ)	/* IFLA_IFNAME */
46392469ffd7SJohn Fastabend 		+ nla_total_size(MAX_ADDR_LEN)	/* IFLA_ADDRESS */
46402469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_MASTER */
46412469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_MTU */
46422469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_LINK */
46432469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_OPERSTATE */
46442469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u8))	/* IFLA_PROTINFO */
46452469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(struct nlattr))	/* IFLA_AF_SPEC */
46462469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u16))	/* IFLA_BRIDGE_FLAGS */
46472469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u16));	/* IFLA_BRIDGE_MODE */
46482469ffd7SJohn Fastabend }
46492469ffd7SJohn Fastabend 
465002dba438SRoopa Prabhu static int rtnl_bridge_notify(struct net_device *dev)
46512469ffd7SJohn Fastabend {
46522469ffd7SJohn Fastabend 	struct net *net = dev_net(dev);
46532469ffd7SJohn Fastabend 	struct sk_buff *skb;
46542469ffd7SJohn Fastabend 	int err = -EOPNOTSUPP;
46552469ffd7SJohn Fastabend 
465602dba438SRoopa Prabhu 	if (!dev->netdev_ops->ndo_bridge_getlink)
465702dba438SRoopa Prabhu 		return 0;
465802dba438SRoopa Prabhu 
46592469ffd7SJohn Fastabend 	skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
46602469ffd7SJohn Fastabend 	if (!skb) {
46612469ffd7SJohn Fastabend 		err = -ENOMEM;
46622469ffd7SJohn Fastabend 		goto errout;
46632469ffd7SJohn Fastabend 	}
46642469ffd7SJohn Fastabend 
466546c264daSNicolas Dichtel 	err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
4666c38e01b8SJohn Fastabend 	if (err < 0)
4667c38e01b8SJohn Fastabend 		goto errout;
46682469ffd7SJohn Fastabend 
466959ccaaaaSRoopa Prabhu 	if (!skb->len)
467059ccaaaaSRoopa Prabhu 		goto errout;
467159ccaaaaSRoopa Prabhu 
46722469ffd7SJohn Fastabend 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
46732469ffd7SJohn Fastabend 	return 0;
46742469ffd7SJohn Fastabend errout:
46752469ffd7SJohn Fastabend 	WARN_ON(err == -EMSGSIZE);
46762469ffd7SJohn Fastabend 	kfree_skb(skb);
467759ccaaaaSRoopa Prabhu 	if (err)
46782469ffd7SJohn Fastabend 		rtnl_set_sk_err(net, RTNLGRP_LINK, err);
46792469ffd7SJohn Fastabend 	return err;
46802469ffd7SJohn Fastabend }
46812469ffd7SJohn Fastabend 
4682c21ef3e3SDavid Ahern static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
4683c21ef3e3SDavid Ahern 			       struct netlink_ext_ack *extack)
4684e5a55a89SJohn Fastabend {
4685e5a55a89SJohn Fastabend 	struct net *net = sock_net(skb->sk);
4686e5a55a89SJohn Fastabend 	struct ifinfomsg *ifm;
4687e5a55a89SJohn Fastabend 	struct net_device *dev;
46882469ffd7SJohn Fastabend 	struct nlattr *br_spec, *attr = NULL;
46892469ffd7SJohn Fastabend 	int rem, err = -EOPNOTSUPP;
46904de8b413SRosen, Rami 	u16 flags = 0;
4691c38e01b8SJohn Fastabend 	bool have_flags = false;
4692e5a55a89SJohn Fastabend 
4693e5a55a89SJohn Fastabend 	if (nlmsg_len(nlh) < sizeof(*ifm))
4694e5a55a89SJohn Fastabend 		return -EINVAL;
4695e5a55a89SJohn Fastabend 
4696e5a55a89SJohn Fastabend 	ifm = nlmsg_data(nlh);
4697e5a55a89SJohn Fastabend 	if (ifm->ifi_family != AF_BRIDGE)
4698e5a55a89SJohn Fastabend 		return -EPFNOSUPPORT;
4699e5a55a89SJohn Fastabend 
4700e5a55a89SJohn Fastabend 	dev = __dev_get_by_index(net, ifm->ifi_index);
4701e5a55a89SJohn Fastabend 	if (!dev) {
4702b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "unknown ifindex");
4703e5a55a89SJohn Fastabend 		return -ENODEV;
4704e5a55a89SJohn Fastabend 	}
4705e5a55a89SJohn Fastabend 
47062469ffd7SJohn Fastabend 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
47072469ffd7SJohn Fastabend 	if (br_spec) {
47082469ffd7SJohn Fastabend 		nla_for_each_nested(attr, br_spec, rem) {
47092469ffd7SJohn Fastabend 			if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
47106e8d1c55SThomas Graf 				if (nla_len(attr) < sizeof(flags))
47116e8d1c55SThomas Graf 					return -EINVAL;
47126e8d1c55SThomas Graf 
4713c38e01b8SJohn Fastabend 				have_flags = true;
47142469ffd7SJohn Fastabend 				flags = nla_get_u16(attr);
47152469ffd7SJohn Fastabend 				break;
47162469ffd7SJohn Fastabend 			}
47172469ffd7SJohn Fastabend 		}
47182469ffd7SJohn Fastabend 	}
47192469ffd7SJohn Fastabend 
47202469ffd7SJohn Fastabend 	if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
4721898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4722898e5061SJiri Pirko 
4723898e5061SJiri Pirko 		if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
47242469ffd7SJohn Fastabend 			err = -EOPNOTSUPP;
4725e5a55a89SJohn Fastabend 			goto out;
4726e5a55a89SJohn Fastabend 		}
4727e5a55a89SJohn Fastabend 
47282fd527b7SPetr Machata 		err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags,
47292fd527b7SPetr Machata 							     extack);
47302469ffd7SJohn Fastabend 		if (err)
47312469ffd7SJohn Fastabend 			goto out;
47322469ffd7SJohn Fastabend 
47332469ffd7SJohn Fastabend 		flags &= ~BRIDGE_FLAGS_MASTER;
47342469ffd7SJohn Fastabend 	}
47352469ffd7SJohn Fastabend 
47362469ffd7SJohn Fastabend 	if ((flags & BRIDGE_FLAGS_SELF)) {
47372469ffd7SJohn Fastabend 		if (!dev->netdev_ops->ndo_bridge_setlink)
47382469ffd7SJohn Fastabend 			err = -EOPNOTSUPP;
47392469ffd7SJohn Fastabend 		else
4740add511b3SRoopa Prabhu 			err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
47412fd527b7SPetr Machata 								  flags,
47422fd527b7SPetr Machata 								  extack);
474302dba438SRoopa Prabhu 		if (!err) {
47442469ffd7SJohn Fastabend 			flags &= ~BRIDGE_FLAGS_SELF;
474502dba438SRoopa Prabhu 
474602dba438SRoopa Prabhu 			/* Generate event to notify upper layer of bridge
474702dba438SRoopa Prabhu 			 * change
474802dba438SRoopa Prabhu 			 */
474902dba438SRoopa Prabhu 			err = rtnl_bridge_notify(dev);
475002dba438SRoopa Prabhu 		}
47512469ffd7SJohn Fastabend 	}
47522469ffd7SJohn Fastabend 
4753c38e01b8SJohn Fastabend 	if (have_flags)
47542469ffd7SJohn Fastabend 		memcpy(nla_data(attr), &flags, sizeof(flags));
4755e5a55a89SJohn Fastabend out:
4756e5a55a89SJohn Fastabend 	return err;
4757e5a55a89SJohn Fastabend }
4758e5a55a89SJohn Fastabend 
4759c21ef3e3SDavid Ahern static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
4760c21ef3e3SDavid Ahern 			       struct netlink_ext_ack *extack)
4761407af329SVlad Yasevich {
4762407af329SVlad Yasevich 	struct net *net = sock_net(skb->sk);
4763407af329SVlad Yasevich 	struct ifinfomsg *ifm;
4764407af329SVlad Yasevich 	struct net_device *dev;
4765407af329SVlad Yasevich 	struct nlattr *br_spec, *attr = NULL;
4766407af329SVlad Yasevich 	int rem, err = -EOPNOTSUPP;
47674de8b413SRosen, Rami 	u16 flags = 0;
4768407af329SVlad Yasevich 	bool have_flags = false;
4769407af329SVlad Yasevich 
4770407af329SVlad Yasevich 	if (nlmsg_len(nlh) < sizeof(*ifm))
4771407af329SVlad Yasevich 		return -EINVAL;
4772407af329SVlad Yasevich 
4773407af329SVlad Yasevich 	ifm = nlmsg_data(nlh);
4774407af329SVlad Yasevich 	if (ifm->ifi_family != AF_BRIDGE)
4775407af329SVlad Yasevich 		return -EPFNOSUPPORT;
4776407af329SVlad Yasevich 
4777407af329SVlad Yasevich 	dev = __dev_get_by_index(net, ifm->ifi_index);
4778407af329SVlad Yasevich 	if (!dev) {
4779b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "unknown ifindex");
4780407af329SVlad Yasevich 		return -ENODEV;
4781407af329SVlad Yasevich 	}
4782407af329SVlad Yasevich 
4783407af329SVlad Yasevich 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4784407af329SVlad Yasevich 	if (br_spec) {
4785407af329SVlad Yasevich 		nla_for_each_nested(attr, br_spec, rem) {
4786407af329SVlad Yasevich 			if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
47876e8d1c55SThomas Graf 				if (nla_len(attr) < sizeof(flags))
47886e8d1c55SThomas Graf 					return -EINVAL;
47896e8d1c55SThomas Graf 
4790407af329SVlad Yasevich 				have_flags = true;
4791407af329SVlad Yasevich 				flags = nla_get_u16(attr);
4792407af329SVlad Yasevich 				break;
4793407af329SVlad Yasevich 			}
4794407af329SVlad Yasevich 		}
4795407af329SVlad Yasevich 	}
4796407af329SVlad Yasevich 
4797407af329SVlad Yasevich 	if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
4798407af329SVlad Yasevich 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4799407af329SVlad Yasevich 
4800407af329SVlad Yasevich 		if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
4801407af329SVlad Yasevich 			err = -EOPNOTSUPP;
4802407af329SVlad Yasevich 			goto out;
4803407af329SVlad Yasevich 		}
4804407af329SVlad Yasevich 
4805add511b3SRoopa Prabhu 		err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
4806407af329SVlad Yasevich 		if (err)
4807407af329SVlad Yasevich 			goto out;
4808407af329SVlad Yasevich 
4809407af329SVlad Yasevich 		flags &= ~BRIDGE_FLAGS_MASTER;
4810407af329SVlad Yasevich 	}
4811407af329SVlad Yasevich 
4812407af329SVlad Yasevich 	if ((flags & BRIDGE_FLAGS_SELF)) {
4813407af329SVlad Yasevich 		if (!dev->netdev_ops->ndo_bridge_dellink)
4814407af329SVlad Yasevich 			err = -EOPNOTSUPP;
4815407af329SVlad Yasevich 		else
4816add511b3SRoopa Prabhu 			err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
4817add511b3SRoopa Prabhu 								  flags);
4818407af329SVlad Yasevich 
481902dba438SRoopa Prabhu 		if (!err) {
4820407af329SVlad Yasevich 			flags &= ~BRIDGE_FLAGS_SELF;
482102dba438SRoopa Prabhu 
482202dba438SRoopa Prabhu 			/* Generate event to notify upper layer of bridge
482302dba438SRoopa Prabhu 			 * change
482402dba438SRoopa Prabhu 			 */
482502dba438SRoopa Prabhu 			err = rtnl_bridge_notify(dev);
482602dba438SRoopa Prabhu 		}
4827407af329SVlad Yasevich 	}
4828407af329SVlad Yasevich 
4829407af329SVlad Yasevich 	if (have_flags)
4830407af329SVlad Yasevich 		memcpy(nla_data(attr), &flags, sizeof(flags));
4831407af329SVlad Yasevich out:
4832407af329SVlad Yasevich 	return err;
4833407af329SVlad Yasevich }
4834407af329SVlad Yasevich 
4835e8872a25SNikolay Aleksandrov static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
4836e8872a25SNikolay Aleksandrov {
4837e8872a25SNikolay Aleksandrov 	return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
4838e8872a25SNikolay Aleksandrov 	       (!idxattr || idxattr == attrid);
4839e8872a25SNikolay Aleksandrov }
4840e8872a25SNikolay Aleksandrov 
484169ae6ad2SNogah Frankel #define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
484269ae6ad2SNogah Frankel static int rtnl_get_offload_stats_attr_size(int attr_id)
484369ae6ad2SNogah Frankel {
484469ae6ad2SNogah Frankel 	switch (attr_id) {
484569ae6ad2SNogah Frankel 	case IFLA_OFFLOAD_XSTATS_CPU_HIT:
484669ae6ad2SNogah Frankel 		return sizeof(struct rtnl_link_stats64);
484769ae6ad2SNogah Frankel 	}
484869ae6ad2SNogah Frankel 
484969ae6ad2SNogah Frankel 	return 0;
485069ae6ad2SNogah Frankel }
485169ae6ad2SNogah Frankel 
485269ae6ad2SNogah Frankel static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev,
485369ae6ad2SNogah Frankel 				  int *prividx)
485469ae6ad2SNogah Frankel {
485569ae6ad2SNogah Frankel 	struct nlattr *attr = NULL;
485669ae6ad2SNogah Frankel 	int attr_id, size;
485769ae6ad2SNogah Frankel 	void *attr_data;
485869ae6ad2SNogah Frankel 	int err;
485969ae6ad2SNogah Frankel 
486069ae6ad2SNogah Frankel 	if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
486169ae6ad2SNogah Frankel 	      dev->netdev_ops->ndo_get_offload_stats))
486269ae6ad2SNogah Frankel 		return -ENODATA;
486369ae6ad2SNogah Frankel 
486469ae6ad2SNogah Frankel 	for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
486569ae6ad2SNogah Frankel 	     attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
486669ae6ad2SNogah Frankel 		if (attr_id < *prividx)
486769ae6ad2SNogah Frankel 			continue;
486869ae6ad2SNogah Frankel 
486969ae6ad2SNogah Frankel 		size = rtnl_get_offload_stats_attr_size(attr_id);
487069ae6ad2SNogah Frankel 		if (!size)
487169ae6ad2SNogah Frankel 			continue;
487269ae6ad2SNogah Frankel 
48733df5b3c6SOr Gerlitz 		if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
487469ae6ad2SNogah Frankel 			continue;
487569ae6ad2SNogah Frankel 
487669ae6ad2SNogah Frankel 		attr = nla_reserve_64bit(skb, attr_id, size,
487769ae6ad2SNogah Frankel 					 IFLA_OFFLOAD_XSTATS_UNSPEC);
487869ae6ad2SNogah Frankel 		if (!attr)
487969ae6ad2SNogah Frankel 			goto nla_put_failure;
488069ae6ad2SNogah Frankel 
488169ae6ad2SNogah Frankel 		attr_data = nla_data(attr);
488269ae6ad2SNogah Frankel 		memset(attr_data, 0, size);
488369ae6ad2SNogah Frankel 		err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev,
488469ae6ad2SNogah Frankel 							     attr_data);
488569ae6ad2SNogah Frankel 		if (err)
488669ae6ad2SNogah Frankel 			goto get_offload_stats_failure;
488769ae6ad2SNogah Frankel 	}
488869ae6ad2SNogah Frankel 
488969ae6ad2SNogah Frankel 	if (!attr)
489069ae6ad2SNogah Frankel 		return -ENODATA;
489169ae6ad2SNogah Frankel 
489269ae6ad2SNogah Frankel 	*prividx = 0;
489369ae6ad2SNogah Frankel 	return 0;
489469ae6ad2SNogah Frankel 
489569ae6ad2SNogah Frankel nla_put_failure:
489669ae6ad2SNogah Frankel 	err = -EMSGSIZE;
489769ae6ad2SNogah Frankel get_offload_stats_failure:
489869ae6ad2SNogah Frankel 	*prividx = attr_id;
489969ae6ad2SNogah Frankel 	return err;
490069ae6ad2SNogah Frankel }
490169ae6ad2SNogah Frankel 
490269ae6ad2SNogah Frankel static int rtnl_get_offload_stats_size(const struct net_device *dev)
490369ae6ad2SNogah Frankel {
490469ae6ad2SNogah Frankel 	int nla_size = 0;
490569ae6ad2SNogah Frankel 	int attr_id;
490669ae6ad2SNogah Frankel 	int size;
490769ae6ad2SNogah Frankel 
490869ae6ad2SNogah Frankel 	if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
490969ae6ad2SNogah Frankel 	      dev->netdev_ops->ndo_get_offload_stats))
491069ae6ad2SNogah Frankel 		return 0;
491169ae6ad2SNogah Frankel 
491269ae6ad2SNogah Frankel 	for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
491369ae6ad2SNogah Frankel 	     attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
49143df5b3c6SOr Gerlitz 		if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
491569ae6ad2SNogah Frankel 			continue;
491669ae6ad2SNogah Frankel 		size = rtnl_get_offload_stats_attr_size(attr_id);
491769ae6ad2SNogah Frankel 		nla_size += nla_total_size_64bit(size);
491869ae6ad2SNogah Frankel 	}
491969ae6ad2SNogah Frankel 
492069ae6ad2SNogah Frankel 	if (nla_size != 0)
492169ae6ad2SNogah Frankel 		nla_size += nla_total_size(0);
492269ae6ad2SNogah Frankel 
492369ae6ad2SNogah Frankel 	return nla_size;
492469ae6ad2SNogah Frankel }
492569ae6ad2SNogah Frankel 
492610c9ead9SRoopa Prabhu static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
492710c9ead9SRoopa Prabhu 			       int type, u32 pid, u32 seq, u32 change,
4928e8872a25SNikolay Aleksandrov 			       unsigned int flags, unsigned int filter_mask,
4929e8872a25SNikolay Aleksandrov 			       int *idxattr, int *prividx)
493010c9ead9SRoopa Prabhu {
493110c9ead9SRoopa Prabhu 	struct if_stats_msg *ifsm;
493210c9ead9SRoopa Prabhu 	struct nlmsghdr *nlh;
493310c9ead9SRoopa Prabhu 	struct nlattr *attr;
4934e8872a25SNikolay Aleksandrov 	int s_prividx = *prividx;
493569ae6ad2SNogah Frankel 	int err;
493610c9ead9SRoopa Prabhu 
493710c9ead9SRoopa Prabhu 	ASSERT_RTNL();
493810c9ead9SRoopa Prabhu 
493910c9ead9SRoopa Prabhu 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
494010c9ead9SRoopa Prabhu 	if (!nlh)
494110c9ead9SRoopa Prabhu 		return -EMSGSIZE;
494210c9ead9SRoopa Prabhu 
494310c9ead9SRoopa Prabhu 	ifsm = nlmsg_data(nlh);
4944ce024f42SNikolay Aleksandrov 	ifsm->family = PF_UNSPEC;
4945ce024f42SNikolay Aleksandrov 	ifsm->pad1 = 0;
4946ce024f42SNikolay Aleksandrov 	ifsm->pad2 = 0;
494710c9ead9SRoopa Prabhu 	ifsm->ifindex = dev->ifindex;
494810c9ead9SRoopa Prabhu 	ifsm->filter_mask = filter_mask;
494910c9ead9SRoopa Prabhu 
4950e8872a25SNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
495110c9ead9SRoopa Prabhu 		struct rtnl_link_stats64 *sp;
495210c9ead9SRoopa Prabhu 
495358414d32SNicolas Dichtel 		attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
495458414d32SNicolas Dichtel 					 sizeof(struct rtnl_link_stats64),
495558414d32SNicolas Dichtel 					 IFLA_STATS_UNSPEC);
495610c9ead9SRoopa Prabhu 		if (!attr)
495710c9ead9SRoopa Prabhu 			goto nla_put_failure;
495810c9ead9SRoopa Prabhu 
495910c9ead9SRoopa Prabhu 		sp = nla_data(attr);
496010c9ead9SRoopa Prabhu 		dev_get_stats(dev, sp);
496110c9ead9SRoopa Prabhu 	}
496210c9ead9SRoopa Prabhu 
496397a47facSNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
496497a47facSNikolay Aleksandrov 		const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
496597a47facSNikolay Aleksandrov 
496697a47facSNikolay Aleksandrov 		if (ops && ops->fill_linkxstats) {
496797a47facSNikolay Aleksandrov 			*idxattr = IFLA_STATS_LINK_XSTATS;
4968ae0be8deSMichal Kubecek 			attr = nla_nest_start_noflag(skb,
496997a47facSNikolay Aleksandrov 						     IFLA_STATS_LINK_XSTATS);
497097a47facSNikolay Aleksandrov 			if (!attr)
497197a47facSNikolay Aleksandrov 				goto nla_put_failure;
497297a47facSNikolay Aleksandrov 
497380e73cc5SNikolay Aleksandrov 			err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
497480e73cc5SNikolay Aleksandrov 			nla_nest_end(skb, attr);
497580e73cc5SNikolay Aleksandrov 			if (err)
497680e73cc5SNikolay Aleksandrov 				goto nla_put_failure;
497780e73cc5SNikolay Aleksandrov 			*idxattr = 0;
497880e73cc5SNikolay Aleksandrov 		}
497980e73cc5SNikolay Aleksandrov 	}
498080e73cc5SNikolay Aleksandrov 
498180e73cc5SNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
498280e73cc5SNikolay Aleksandrov 			     *idxattr)) {
498380e73cc5SNikolay Aleksandrov 		const struct rtnl_link_ops *ops = NULL;
498480e73cc5SNikolay Aleksandrov 		const struct net_device *master;
498580e73cc5SNikolay Aleksandrov 
498680e73cc5SNikolay Aleksandrov 		master = netdev_master_upper_dev_get(dev);
498780e73cc5SNikolay Aleksandrov 		if (master)
498880e73cc5SNikolay Aleksandrov 			ops = master->rtnl_link_ops;
498980e73cc5SNikolay Aleksandrov 		if (ops && ops->fill_linkxstats) {
499080e73cc5SNikolay Aleksandrov 			*idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
4991ae0be8deSMichal Kubecek 			attr = nla_nest_start_noflag(skb,
499280e73cc5SNikolay Aleksandrov 						     IFLA_STATS_LINK_XSTATS_SLAVE);
499380e73cc5SNikolay Aleksandrov 			if (!attr)
499480e73cc5SNikolay Aleksandrov 				goto nla_put_failure;
499580e73cc5SNikolay Aleksandrov 
499680e73cc5SNikolay Aleksandrov 			err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
499797a47facSNikolay Aleksandrov 			nla_nest_end(skb, attr);
499897a47facSNikolay Aleksandrov 			if (err)
499997a47facSNikolay Aleksandrov 				goto nla_put_failure;
500097a47facSNikolay Aleksandrov 			*idxattr = 0;
500197a47facSNikolay Aleksandrov 		}
500297a47facSNikolay Aleksandrov 	}
500397a47facSNikolay Aleksandrov 
500469ae6ad2SNogah Frankel 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
500569ae6ad2SNogah Frankel 			     *idxattr)) {
500669ae6ad2SNogah Frankel 		*idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
5007ae0be8deSMichal Kubecek 		attr = nla_nest_start_noflag(skb,
5008ae0be8deSMichal Kubecek 					     IFLA_STATS_LINK_OFFLOAD_XSTATS);
500969ae6ad2SNogah Frankel 		if (!attr)
501069ae6ad2SNogah Frankel 			goto nla_put_failure;
501169ae6ad2SNogah Frankel 
501269ae6ad2SNogah Frankel 		err = rtnl_get_offload_stats(skb, dev, prividx);
501369ae6ad2SNogah Frankel 		if (err == -ENODATA)
501469ae6ad2SNogah Frankel 			nla_nest_cancel(skb, attr);
501569ae6ad2SNogah Frankel 		else
501669ae6ad2SNogah Frankel 			nla_nest_end(skb, attr);
501769ae6ad2SNogah Frankel 
501869ae6ad2SNogah Frankel 		if (err && err != -ENODATA)
501969ae6ad2SNogah Frankel 			goto nla_put_failure;
502069ae6ad2SNogah Frankel 		*idxattr = 0;
502169ae6ad2SNogah Frankel 	}
502269ae6ad2SNogah Frankel 
5023aefb4d4aSRobert Shearman 	if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
5024aefb4d4aSRobert Shearman 		struct rtnl_af_ops *af_ops;
5025aefb4d4aSRobert Shearman 
5026aefb4d4aSRobert Shearman 		*idxattr = IFLA_STATS_AF_SPEC;
5027ae0be8deSMichal Kubecek 		attr = nla_nest_start_noflag(skb, IFLA_STATS_AF_SPEC);
5028aefb4d4aSRobert Shearman 		if (!attr)
5029aefb4d4aSRobert Shearman 			goto nla_put_failure;
5030aefb4d4aSRobert Shearman 
50315fa85a09SFlorian Westphal 		rcu_read_lock();
50325fa85a09SFlorian Westphal 		list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
5033aefb4d4aSRobert Shearman 			if (af_ops->fill_stats_af) {
5034aefb4d4aSRobert Shearman 				struct nlattr *af;
5035aefb4d4aSRobert Shearman 				int err;
5036aefb4d4aSRobert Shearman 
5037ae0be8deSMichal Kubecek 				af = nla_nest_start_noflag(skb,
5038ae0be8deSMichal Kubecek 							   af_ops->family);
50395fa85a09SFlorian Westphal 				if (!af) {
50405fa85a09SFlorian Westphal 					rcu_read_unlock();
5041aefb4d4aSRobert Shearman 					goto nla_put_failure;
50425fa85a09SFlorian Westphal 				}
5043aefb4d4aSRobert Shearman 				err = af_ops->fill_stats_af(skb, dev);
5044aefb4d4aSRobert Shearman 
50455fa85a09SFlorian Westphal 				if (err == -ENODATA) {
5046aefb4d4aSRobert Shearman 					nla_nest_cancel(skb, af);
50475fa85a09SFlorian Westphal 				} else if (err < 0) {
50485fa85a09SFlorian Westphal 					rcu_read_unlock();
5049aefb4d4aSRobert Shearman 					goto nla_put_failure;
50505fa85a09SFlorian Westphal 				}
5051aefb4d4aSRobert Shearman 
5052aefb4d4aSRobert Shearman 				nla_nest_end(skb, af);
5053aefb4d4aSRobert Shearman 			}
5054aefb4d4aSRobert Shearman 		}
50555fa85a09SFlorian Westphal 		rcu_read_unlock();
5056aefb4d4aSRobert Shearman 
5057aefb4d4aSRobert Shearman 		nla_nest_end(skb, attr);
5058aefb4d4aSRobert Shearman 
5059aefb4d4aSRobert Shearman 		*idxattr = 0;
5060aefb4d4aSRobert Shearman 	}
5061aefb4d4aSRobert Shearman 
506210c9ead9SRoopa Prabhu 	nlmsg_end(skb, nlh);
506310c9ead9SRoopa Prabhu 
506410c9ead9SRoopa Prabhu 	return 0;
506510c9ead9SRoopa Prabhu 
506610c9ead9SRoopa Prabhu nla_put_failure:
5067e8872a25SNikolay Aleksandrov 	/* not a multi message or no progress mean a real error */
5068e8872a25SNikolay Aleksandrov 	if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
506910c9ead9SRoopa Prabhu 		nlmsg_cancel(skb, nlh);
5070e8872a25SNikolay Aleksandrov 	else
5071e8872a25SNikolay Aleksandrov 		nlmsg_end(skb, nlh);
507210c9ead9SRoopa Prabhu 
507310c9ead9SRoopa Prabhu 	return -EMSGSIZE;
507410c9ead9SRoopa Prabhu }
507510c9ead9SRoopa Prabhu 
507610c9ead9SRoopa Prabhu static size_t if_nlmsg_stats_size(const struct net_device *dev,
507710c9ead9SRoopa Prabhu 				  u32 filter_mask)
507810c9ead9SRoopa Prabhu {
507910c9ead9SRoopa Prabhu 	size_t size = 0;
508010c9ead9SRoopa Prabhu 
5081e8872a25SNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
508210c9ead9SRoopa Prabhu 		size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
508310c9ead9SRoopa Prabhu 
508497a47facSNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
508597a47facSNikolay Aleksandrov 		const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
508680e73cc5SNikolay Aleksandrov 		int attr = IFLA_STATS_LINK_XSTATS;
508797a47facSNikolay Aleksandrov 
508897a47facSNikolay Aleksandrov 		if (ops && ops->get_linkxstats_size) {
508980e73cc5SNikolay Aleksandrov 			size += nla_total_size(ops->get_linkxstats_size(dev,
509080e73cc5SNikolay Aleksandrov 									attr));
509197a47facSNikolay Aleksandrov 			/* for IFLA_STATS_LINK_XSTATS */
509297a47facSNikolay Aleksandrov 			size += nla_total_size(0);
509397a47facSNikolay Aleksandrov 		}
509497a47facSNikolay Aleksandrov 	}
509597a47facSNikolay Aleksandrov 
509680e73cc5SNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
509780e73cc5SNikolay Aleksandrov 		struct net_device *_dev = (struct net_device *)dev;
509880e73cc5SNikolay Aleksandrov 		const struct rtnl_link_ops *ops = NULL;
509980e73cc5SNikolay Aleksandrov 		const struct net_device *master;
510080e73cc5SNikolay Aleksandrov 
510180e73cc5SNikolay Aleksandrov 		/* netdev_master_upper_dev_get can't take const */
510280e73cc5SNikolay Aleksandrov 		master = netdev_master_upper_dev_get(_dev);
510380e73cc5SNikolay Aleksandrov 		if (master)
510480e73cc5SNikolay Aleksandrov 			ops = master->rtnl_link_ops;
510580e73cc5SNikolay Aleksandrov 		if (ops && ops->get_linkxstats_size) {
510680e73cc5SNikolay Aleksandrov 			int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
510780e73cc5SNikolay Aleksandrov 
510880e73cc5SNikolay Aleksandrov 			size += nla_total_size(ops->get_linkxstats_size(dev,
510980e73cc5SNikolay Aleksandrov 									attr));
511080e73cc5SNikolay Aleksandrov 			/* for IFLA_STATS_LINK_XSTATS_SLAVE */
511180e73cc5SNikolay Aleksandrov 			size += nla_total_size(0);
511280e73cc5SNikolay Aleksandrov 		}
511380e73cc5SNikolay Aleksandrov 	}
511480e73cc5SNikolay Aleksandrov 
511569ae6ad2SNogah Frankel 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0))
511669ae6ad2SNogah Frankel 		size += rtnl_get_offload_stats_size(dev);
511769ae6ad2SNogah Frankel 
5118aefb4d4aSRobert Shearman 	if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
5119aefb4d4aSRobert Shearman 		struct rtnl_af_ops *af_ops;
5120aefb4d4aSRobert Shearman 
5121aefb4d4aSRobert Shearman 		/* for IFLA_STATS_AF_SPEC */
5122aefb4d4aSRobert Shearman 		size += nla_total_size(0);
5123aefb4d4aSRobert Shearman 
51245fa85a09SFlorian Westphal 		rcu_read_lock();
51255fa85a09SFlorian Westphal 		list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
5126aefb4d4aSRobert Shearman 			if (af_ops->get_stats_af_size) {
5127aefb4d4aSRobert Shearman 				size += nla_total_size(
5128aefb4d4aSRobert Shearman 					af_ops->get_stats_af_size(dev));
5129aefb4d4aSRobert Shearman 
5130aefb4d4aSRobert Shearman 				/* for AF_* */
5131aefb4d4aSRobert Shearman 				size += nla_total_size(0);
5132aefb4d4aSRobert Shearman 			}
5133aefb4d4aSRobert Shearman 		}
51345fa85a09SFlorian Westphal 		rcu_read_unlock();
5135aefb4d4aSRobert Shearman 	}
5136aefb4d4aSRobert Shearman 
513710c9ead9SRoopa Prabhu 	return size;
513810c9ead9SRoopa Prabhu }
513910c9ead9SRoopa Prabhu 
514051bc860dSJakub Kicinski static int rtnl_valid_stats_req(const struct nlmsghdr *nlh, bool strict_check,
514151bc860dSJakub Kicinski 				bool is_dump, struct netlink_ext_ack *extack)
514251bc860dSJakub Kicinski {
514351bc860dSJakub Kicinski 	struct if_stats_msg *ifsm;
514451bc860dSJakub Kicinski 
514569f23a09SEric Dumazet 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifsm))) {
514651bc860dSJakub Kicinski 		NL_SET_ERR_MSG(extack, "Invalid header for stats dump");
514751bc860dSJakub Kicinski 		return -EINVAL;
514851bc860dSJakub Kicinski 	}
514951bc860dSJakub Kicinski 
515051bc860dSJakub Kicinski 	if (!strict_check)
515151bc860dSJakub Kicinski 		return 0;
515251bc860dSJakub Kicinski 
515351bc860dSJakub Kicinski 	ifsm = nlmsg_data(nlh);
515451bc860dSJakub Kicinski 
515551bc860dSJakub Kicinski 	/* only requests using strict checks can pass data to influence
515651bc860dSJakub Kicinski 	 * the dump. The legacy exception is filter_mask.
515751bc860dSJakub Kicinski 	 */
515851bc860dSJakub Kicinski 	if (ifsm->pad1 || ifsm->pad2 || (is_dump && ifsm->ifindex)) {
515951bc860dSJakub Kicinski 		NL_SET_ERR_MSG(extack, "Invalid values in header for stats dump request");
516051bc860dSJakub Kicinski 		return -EINVAL;
516151bc860dSJakub Kicinski 	}
516251bc860dSJakub Kicinski 	if (nlmsg_attrlen(nlh, sizeof(*ifsm))) {
516351bc860dSJakub Kicinski 		NL_SET_ERR_MSG(extack, "Invalid attributes after stats header");
516451bc860dSJakub Kicinski 		return -EINVAL;
516551bc860dSJakub Kicinski 	}
51666300acb2SJakub Kicinski 	if (ifsm->filter_mask >= IFLA_STATS_FILTER_BIT(IFLA_STATS_MAX + 1)) {
51676300acb2SJakub Kicinski 		NL_SET_ERR_MSG(extack, "Invalid stats requested through filter mask");
51686300acb2SJakub Kicinski 		return -EINVAL;
51696300acb2SJakub Kicinski 	}
517051bc860dSJakub Kicinski 
517151bc860dSJakub Kicinski 	return 0;
517251bc860dSJakub Kicinski }
517351bc860dSJakub Kicinski 
5174c21ef3e3SDavid Ahern static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
5175c21ef3e3SDavid Ahern 			  struct netlink_ext_ack *extack)
517610c9ead9SRoopa Prabhu {
517710c9ead9SRoopa Prabhu 	struct net *net = sock_net(skb->sk);
517810c9ead9SRoopa Prabhu 	struct net_device *dev = NULL;
5179e8872a25SNikolay Aleksandrov 	int idxattr = 0, prividx = 0;
5180e8872a25SNikolay Aleksandrov 	struct if_stats_msg *ifsm;
518110c9ead9SRoopa Prabhu 	struct sk_buff *nskb;
518210c9ead9SRoopa Prabhu 	u32 filter_mask;
518310c9ead9SRoopa Prabhu 	int err;
518410c9ead9SRoopa Prabhu 
518551bc860dSJakub Kicinski 	err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb),
518651bc860dSJakub Kicinski 				   false, extack);
518751bc860dSJakub Kicinski 	if (err)
518851bc860dSJakub Kicinski 		return err;
51894775cc1fSMathias Krause 
519010c9ead9SRoopa Prabhu 	ifsm = nlmsg_data(nlh);
519110c9ead9SRoopa Prabhu 	if (ifsm->ifindex > 0)
519210c9ead9SRoopa Prabhu 		dev = __dev_get_by_index(net, ifsm->ifindex);
519310c9ead9SRoopa Prabhu 	else
519410c9ead9SRoopa Prabhu 		return -EINVAL;
519510c9ead9SRoopa Prabhu 
519610c9ead9SRoopa Prabhu 	if (!dev)
519710c9ead9SRoopa Prabhu 		return -ENODEV;
519810c9ead9SRoopa Prabhu 
519910c9ead9SRoopa Prabhu 	filter_mask = ifsm->filter_mask;
520010c9ead9SRoopa Prabhu 	if (!filter_mask)
520110c9ead9SRoopa Prabhu 		return -EINVAL;
520210c9ead9SRoopa Prabhu 
520310c9ead9SRoopa Prabhu 	nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
520410c9ead9SRoopa Prabhu 	if (!nskb)
520510c9ead9SRoopa Prabhu 		return -ENOBUFS;
520610c9ead9SRoopa Prabhu 
520710c9ead9SRoopa Prabhu 	err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
520810c9ead9SRoopa Prabhu 				  NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
5209e8872a25SNikolay Aleksandrov 				  0, filter_mask, &idxattr, &prividx);
521010c9ead9SRoopa Prabhu 	if (err < 0) {
521110c9ead9SRoopa Prabhu 		/* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
521210c9ead9SRoopa Prabhu 		WARN_ON(err == -EMSGSIZE);
521310c9ead9SRoopa Prabhu 		kfree_skb(nskb);
521410c9ead9SRoopa Prabhu 	} else {
521510c9ead9SRoopa Prabhu 		err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
521610c9ead9SRoopa Prabhu 	}
521710c9ead9SRoopa Prabhu 
521810c9ead9SRoopa Prabhu 	return err;
521910c9ead9SRoopa Prabhu }
522010c9ead9SRoopa Prabhu 
522110c9ead9SRoopa Prabhu static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
522210c9ead9SRoopa Prabhu {
5223841891ecSDavid Ahern 	struct netlink_ext_ack *extack = cb->extack;
5224e8872a25SNikolay Aleksandrov 	int h, s_h, err, s_idx, s_idxattr, s_prividx;
522510c9ead9SRoopa Prabhu 	struct net *net = sock_net(skb->sk);
522610c9ead9SRoopa Prabhu 	unsigned int flags = NLM_F_MULTI;
5227e8872a25SNikolay Aleksandrov 	struct if_stats_msg *ifsm;
5228e8872a25SNikolay Aleksandrov 	struct hlist_head *head;
5229e8872a25SNikolay Aleksandrov 	struct net_device *dev;
523010c9ead9SRoopa Prabhu 	u32 filter_mask = 0;
5231e8872a25SNikolay Aleksandrov 	int idx = 0;
523210c9ead9SRoopa Prabhu 
523310c9ead9SRoopa Prabhu 	s_h = cb->args[0];
523410c9ead9SRoopa Prabhu 	s_idx = cb->args[1];
5235e8872a25SNikolay Aleksandrov 	s_idxattr = cb->args[2];
5236e8872a25SNikolay Aleksandrov 	s_prividx = cb->args[3];
523710c9ead9SRoopa Prabhu 
523810c9ead9SRoopa Prabhu 	cb->seq = net->dev_base_seq;
523910c9ead9SRoopa Prabhu 
524051bc860dSJakub Kicinski 	err = rtnl_valid_stats_req(cb->nlh, cb->strict_check, true, extack);
524151bc860dSJakub Kicinski 	if (err)
524251bc860dSJakub Kicinski 		return err;
52434775cc1fSMathias Krause 
524410c9ead9SRoopa Prabhu 	ifsm = nlmsg_data(cb->nlh);
5245841891ecSDavid Ahern 	filter_mask = ifsm->filter_mask;
5246841891ecSDavid Ahern 	if (!filter_mask) {
5247841891ecSDavid Ahern 		NL_SET_ERR_MSG(extack, "Filter mask must be set for stats dump");
5248841891ecSDavid Ahern 		return -EINVAL;
5249841891ecSDavid Ahern 	}
525010c9ead9SRoopa Prabhu 
525110c9ead9SRoopa Prabhu 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
525210c9ead9SRoopa Prabhu 		idx = 0;
525310c9ead9SRoopa Prabhu 		head = &net->dev_index_head[h];
525410c9ead9SRoopa Prabhu 		hlist_for_each_entry(dev, head, index_hlist) {
525510c9ead9SRoopa Prabhu 			if (idx < s_idx)
525610c9ead9SRoopa Prabhu 				goto cont;
525710c9ead9SRoopa Prabhu 			err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
525810c9ead9SRoopa Prabhu 						  NETLINK_CB(cb->skb).portid,
525910c9ead9SRoopa Prabhu 						  cb->nlh->nlmsg_seq, 0,
5260e8872a25SNikolay Aleksandrov 						  flags, filter_mask,
5261e8872a25SNikolay Aleksandrov 						  &s_idxattr, &s_prividx);
526210c9ead9SRoopa Prabhu 			/* If we ran out of room on the first message,
526310c9ead9SRoopa Prabhu 			 * we're in trouble
526410c9ead9SRoopa Prabhu 			 */
526510c9ead9SRoopa Prabhu 			WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
526610c9ead9SRoopa Prabhu 
526710c9ead9SRoopa Prabhu 			if (err < 0)
526810c9ead9SRoopa Prabhu 				goto out;
5269e8872a25SNikolay Aleksandrov 			s_prividx = 0;
5270e8872a25SNikolay Aleksandrov 			s_idxattr = 0;
527110c9ead9SRoopa Prabhu 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
527210c9ead9SRoopa Prabhu cont:
527310c9ead9SRoopa Prabhu 			idx++;
527410c9ead9SRoopa Prabhu 		}
527510c9ead9SRoopa Prabhu 	}
527610c9ead9SRoopa Prabhu out:
5277e8872a25SNikolay Aleksandrov 	cb->args[3] = s_prividx;
5278e8872a25SNikolay Aleksandrov 	cb->args[2] = s_idxattr;
527910c9ead9SRoopa Prabhu 	cb->args[1] = idx;
528010c9ead9SRoopa Prabhu 	cb->args[0] = h;
528110c9ead9SRoopa Prabhu 
528210c9ead9SRoopa Prabhu 	return skb->len;
528310c9ead9SRoopa Prabhu }
528410c9ead9SRoopa Prabhu 
52851da177e4SLinus Torvalds /* Process one rtnetlink message. */
52861da177e4SLinus Torvalds 
52872d4bc933SJohannes Berg static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
52882d4bc933SJohannes Berg 			     struct netlink_ext_ack *extack)
52891da177e4SLinus Torvalds {
52903b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
5291addf9b90SFlorian Westphal 	struct rtnl_link *link;
5292e4202511SFlorian Westphal 	struct module *owner;
52936853dd48SFlorian Westphal 	int err = -EOPNOTSUPP;
5294e2849863SThomas Graf 	rtnl_doit_func doit;
529562256f98SFlorian Westphal 	unsigned int flags;
5296617cfc75SAlexander Kuleshov 	int kind;
52971da177e4SLinus Torvalds 	int family;
52981da177e4SLinus Torvalds 	int type;
52991da177e4SLinus Torvalds 
53001da177e4SLinus Torvalds 	type = nlh->nlmsg_type;
53011da177e4SLinus Torvalds 	if (type > RTM_MAX)
5302038890feSThomas Graf 		return -EOPNOTSUPP;
53031da177e4SLinus Torvalds 
53041da177e4SLinus Torvalds 	type -= RTM_BASE;
53051da177e4SLinus Torvalds 
53061da177e4SLinus Torvalds 	/* All the messages must have at least 1 byte length */
5307573ce260SHong zhi guo 	if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
53081da177e4SLinus Torvalds 		return 0;
53091da177e4SLinus Torvalds 
5310573ce260SHong zhi guo 	family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
53111da177e4SLinus Torvalds 	kind = type&3;
53121da177e4SLinus Torvalds 
531390f62cf3SEric W. Biederman 	if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
53141d00a4ebSThomas Graf 		return -EPERM;
53151da177e4SLinus Torvalds 
53166853dd48SFlorian Westphal 	rcu_read_lock();
5317b8f3ab42SDavid S. Miller 	if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
531897c53cacSDenis V. Lunev 		struct sock *rtnl;
5319e2849863SThomas Graf 		rtnl_dumpit_func dumpit;
5320c7ac8679SGreg Rose 		u16 min_dump_alloc = 0;
53211da177e4SLinus Torvalds 
5322addf9b90SFlorian Westphal 		link = rtnl_get_link(family, type);
5323addf9b90SFlorian Westphal 		if (!link || !link->dumpit) {
53246853dd48SFlorian Westphal 			family = PF_UNSPEC;
5325addf9b90SFlorian Westphal 			link = rtnl_get_link(family, type);
5326addf9b90SFlorian Westphal 			if (!link || !link->dumpit)
53276853dd48SFlorian Westphal 				goto err_unlock;
53286853dd48SFlorian Westphal 		}
5329e4202511SFlorian Westphal 		owner = link->owner;
5330addf9b90SFlorian Westphal 		dumpit = link->dumpit;
53316853dd48SFlorian Westphal 
53325c2bb9b6SFlorian Westphal 		if (type == RTM_GETLINK - RTM_BASE)
5333e1fa6d21SFlorian Westphal 			min_dump_alloc = rtnl_calcit(skb, nlh);
53341da177e4SLinus Torvalds 
5335e4202511SFlorian Westphal 		err = 0;
5336e4202511SFlorian Westphal 		/* need to do this before rcu_read_unlock() */
5337e4202511SFlorian Westphal 		if (!try_module_get(owner))
5338e4202511SFlorian Westphal 			err = -EPROTONOSUPPORT;
5339e4202511SFlorian Westphal 
53406853dd48SFlorian Westphal 		rcu_read_unlock();
53416853dd48SFlorian Westphal 
534297c53cacSDenis V. Lunev 		rtnl = net->rtnl;
5343e4202511SFlorian Westphal 		if (err == 0) {
534480d326faSPablo Neira Ayuso 			struct netlink_dump_control c = {
534580d326faSPablo Neira Ayuso 				.dump		= dumpit,
534680d326faSPablo Neira Ayuso 				.min_dump_alloc	= min_dump_alloc,
5347e4202511SFlorian Westphal 				.module		= owner,
534880d326faSPablo Neira Ayuso 			};
534980d326faSPablo Neira Ayuso 			err = netlink_dump_start(rtnl, skb, nlh, &c);
5350e4202511SFlorian Westphal 			/* netlink_dump_start() will keep a reference on
5351e4202511SFlorian Westphal 			 * module if dump is still in progress.
5352e4202511SFlorian Westphal 			 */
5353e4202511SFlorian Westphal 			module_put(owner);
535480d326faSPablo Neira Ayuso 		}
53552907c35fSEric Dumazet 		return err;
53561da177e4SLinus Torvalds 	}
53571da177e4SLinus Torvalds 
5358addf9b90SFlorian Westphal 	link = rtnl_get_link(family, type);
5359addf9b90SFlorian Westphal 	if (!link || !link->doit) {
53608caa38b5SFlorian Westphal 		family = PF_UNSPEC;
5361addf9b90SFlorian Westphal 		link = rtnl_get_link(PF_UNSPEC, type);
5362addf9b90SFlorian Westphal 		if (!link || !link->doit)
5363addf9b90SFlorian Westphal 			goto out_unlock;
53648caa38b5SFlorian Westphal 	}
53658caa38b5SFlorian Westphal 
5366e4202511SFlorian Westphal 	owner = link->owner;
5367e4202511SFlorian Westphal 	if (!try_module_get(owner)) {
5368e4202511SFlorian Westphal 		err = -EPROTONOSUPPORT;
5369e4202511SFlorian Westphal 		goto out_unlock;
5370e4202511SFlorian Westphal 	}
5371e4202511SFlorian Westphal 
5372addf9b90SFlorian Westphal 	flags = link->flags;
537362256f98SFlorian Westphal 	if (flags & RTNL_FLAG_DOIT_UNLOCKED) {
5374addf9b90SFlorian Westphal 		doit = link->doit;
537562256f98SFlorian Westphal 		rcu_read_unlock();
537662256f98SFlorian Westphal 		if (doit)
537762256f98SFlorian Westphal 			err = doit(skb, nlh, extack);
5378e4202511SFlorian Westphal 		module_put(owner);
537962256f98SFlorian Westphal 		return err;
538062256f98SFlorian Westphal 	}
53816853dd48SFlorian Westphal 	rcu_read_unlock();
53826853dd48SFlorian Westphal 
53830cc09020SFlorian Westphal 	rtnl_lock();
5384addf9b90SFlorian Westphal 	link = rtnl_get_link(family, type);
5385addf9b90SFlorian Westphal 	if (link && link->doit)
5386addf9b90SFlorian Westphal 		err = link->doit(skb, nlh, extack);
53870cc09020SFlorian Westphal 	rtnl_unlock();
5388addf9b90SFlorian Westphal 
5389e4202511SFlorian Westphal 	module_put(owner);
5390e4202511SFlorian Westphal 
5391addf9b90SFlorian Westphal 	return err;
5392addf9b90SFlorian Westphal 
5393addf9b90SFlorian Westphal out_unlock:
5394addf9b90SFlorian Westphal 	rcu_read_unlock();
53950cc09020SFlorian Westphal 	return err;
53960cc09020SFlorian Westphal 
53970cc09020SFlorian Westphal err_unlock:
53986853dd48SFlorian Westphal 	rcu_read_unlock();
53990cc09020SFlorian Westphal 	return -EOPNOTSUPP;
54001da177e4SLinus Torvalds }
54011da177e4SLinus Torvalds 
5402cd40b7d3SDenis V. Lunev static void rtnetlink_rcv(struct sk_buff *skb)
54031da177e4SLinus Torvalds {
5404cd40b7d3SDenis V. Lunev 	netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
54051da177e4SLinus Torvalds }
54061da177e4SLinus Torvalds 
54075f729eaaSJulien Gomes static int rtnetlink_bind(struct net *net, int group)
54085f729eaaSJulien Gomes {
54095f729eaaSJulien Gomes 	switch (group) {
54105f729eaaSJulien Gomes 	case RTNLGRP_IPV4_MROUTE_R:
54115f729eaaSJulien Gomes 	case RTNLGRP_IPV6_MROUTE_R:
54125f729eaaSJulien Gomes 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
54135f729eaaSJulien Gomes 			return -EPERM;
54145f729eaaSJulien Gomes 		break;
54155f729eaaSJulien Gomes 	}
54165f729eaaSJulien Gomes 	return 0;
54175f729eaaSJulien Gomes }
54185f729eaaSJulien Gomes 
54191da177e4SLinus Torvalds static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
54201da177e4SLinus Torvalds {
5421351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
5422e9dc8653SEric W. Biederman 
54231da177e4SLinus Torvalds 	switch (event) {
54245138e86fSVlad Yasevich 	case NETDEV_REBOOT:
54258a212589SXin Long 	case NETDEV_CHANGEMTU:
54263753654eSDavid Ahern 	case NETDEV_CHANGEADDR:
54275138e86fSVlad Yasevich 	case NETDEV_CHANGENAME:
54285138e86fSVlad Yasevich 	case NETDEV_FEAT_CHANGE:
54295138e86fSVlad Yasevich 	case NETDEV_BONDING_FAILOVER:
5430e6e66594SXin Long 	case NETDEV_POST_TYPE_CHANGE:
54315138e86fSVlad Yasevich 	case NETDEV_NOTIFY_PEERS:
5432dc709f37SXin Long 	case NETDEV_CHANGEUPPER:
54335138e86fSVlad Yasevich 	case NETDEV_RESEND_IGMP:
54345138e86fSVlad Yasevich 	case NETDEV_CHANGEINFODATA:
5435eeda3fb9SXin Long 	case NETDEV_CHANGELOWERSTATE:
5436ebdcf045SXin Long 	case NETDEV_CHANGE_TX_QUEUE_LEN:
54373d3ea5afSVlad Yasevich 		rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
543838e01b30SNicolas Dichtel 				   GFP_KERNEL, NULL, 0);
54391da177e4SLinus Torvalds 		break;
54401da177e4SLinus Torvalds 	default:
54411da177e4SLinus Torvalds 		break;
54421da177e4SLinus Torvalds 	}
54431da177e4SLinus Torvalds 	return NOTIFY_DONE;
54441da177e4SLinus Torvalds }
54451da177e4SLinus Torvalds 
54461da177e4SLinus Torvalds static struct notifier_block rtnetlink_dev_notifier = {
54471da177e4SLinus Torvalds 	.notifier_call	= rtnetlink_event,
54481da177e4SLinus Torvalds };
54491da177e4SLinus Torvalds 
545097c53cacSDenis V. Lunev 
54512c8c1e72SAlexey Dobriyan static int __net_init rtnetlink_net_init(struct net *net)
545297c53cacSDenis V. Lunev {
545397c53cacSDenis V. Lunev 	struct sock *sk;
5454a31f2d17SPablo Neira Ayuso 	struct netlink_kernel_cfg cfg = {
5455a31f2d17SPablo Neira Ayuso 		.groups		= RTNLGRP_MAX,
5456a31f2d17SPablo Neira Ayuso 		.input		= rtnetlink_rcv,
5457a31f2d17SPablo Neira Ayuso 		.cb_mutex	= &rtnl_mutex,
54589785e10aSPablo Neira Ayuso 		.flags		= NL_CFG_F_NONROOT_RECV,
54595f729eaaSJulien Gomes 		.bind		= rtnetlink_bind,
5460a31f2d17SPablo Neira Ayuso 	};
5461a31f2d17SPablo Neira Ayuso 
54629f00d977SPablo Neira Ayuso 	sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
546397c53cacSDenis V. Lunev 	if (!sk)
546497c53cacSDenis V. Lunev 		return -ENOMEM;
546597c53cacSDenis V. Lunev 	net->rtnl = sk;
546697c53cacSDenis V. Lunev 	return 0;
546797c53cacSDenis V. Lunev }
546897c53cacSDenis V. Lunev 
54692c8c1e72SAlexey Dobriyan static void __net_exit rtnetlink_net_exit(struct net *net)
547097c53cacSDenis V. Lunev {
5471b7c6ba6eSDenis V. Lunev 	netlink_kernel_release(net->rtnl);
547297c53cacSDenis V. Lunev 	net->rtnl = NULL;
547397c53cacSDenis V. Lunev }
547497c53cacSDenis V. Lunev 
547597c53cacSDenis V. Lunev static struct pernet_operations rtnetlink_net_ops = {
547697c53cacSDenis V. Lunev 	.init = rtnetlink_net_init,
547797c53cacSDenis V. Lunev 	.exit = rtnetlink_net_exit,
547897c53cacSDenis V. Lunev };
547997c53cacSDenis V. Lunev 
54801da177e4SLinus Torvalds void __init rtnetlink_init(void)
54811da177e4SLinus Torvalds {
548297c53cacSDenis V. Lunev 	if (register_pernet_subsys(&rtnetlink_net_ops))
54831da177e4SLinus Torvalds 		panic("rtnetlink_init: cannot initialize rtnetlink\n");
548497c53cacSDenis V. Lunev 
54851da177e4SLinus Torvalds 	register_netdevice_notifier(&rtnetlink_dev_notifier);
5486340d17fcSThomas Graf 
5487c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
5488b97bac64SFlorian Westphal 		      rtnl_dump_ifinfo, 0);
5489b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0);
5490b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0);
5491b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0);
5492687ad8ccSThomas Graf 
5493b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0);
5494b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0);
5495b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0);
549677162022SJohn Fastabend 
549736fbf1e5SJiri Pirko 	rtnl_register(PF_UNSPEC, RTM_NEWLINKPROP, rtnl_newlinkprop, NULL, 0);
549836fbf1e5SJiri Pirko 	rtnl_register(PF_UNSPEC, RTM_DELLINKPROP, rtnl_dellinkprop, NULL, 0);
549936fbf1e5SJiri Pirko 
5500b97bac64SFlorian Westphal 	rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
5501b97bac64SFlorian Westphal 	rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0);
55025b2f94b2SRoopa Prabhu 	rtnl_register(PF_BRIDGE, RTM_GETNEIGH, rtnl_fdb_get, rtnl_fdb_dump, 0);
5503e5a55a89SJohn Fastabend 
5504b97bac64SFlorian Westphal 	rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0);
5505b97bac64SFlorian Westphal 	rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0);
5506b97bac64SFlorian Westphal 	rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0);
550710c9ead9SRoopa Prabhu 
550810c9ead9SRoopa Prabhu 	rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,
5509b97bac64SFlorian Westphal 		      0);
55101da177e4SLinus Torvalds }
5511