xref: /openbmc/linux/net/core/rtnetlink.c (revision e4202511480da5f8e6870d8f6ecbb821aeaa8caf)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * INET		An implementation of the TCP/IP protocol suite for the LINUX
31da177e4SLinus Torvalds  *		operating system.  INET is implemented using the  BSD Socket
41da177e4SLinus Torvalds  *		interface as the means of communication with the user level.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *		Routing netlink socket interface: protocol independent part.
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  *		This program is free software; you can redistribute it and/or
111da177e4SLinus Torvalds  *		modify it under the terms of the GNU General Public License
121da177e4SLinus Torvalds  *		as published by the Free Software Foundation; either version
131da177e4SLinus Torvalds  *		2 of the License, or (at your option) any later version.
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  *	Fixes:
161da177e4SLinus Torvalds  *	Vitaly E. Lavrov		RTA_OK arithmetics was wrong.
171da177e4SLinus Torvalds  */
181da177e4SLinus Torvalds 
19ee5d032fSJakub Kicinski #include <linux/bitops.h>
201da177e4SLinus Torvalds #include <linux/errno.h>
211da177e4SLinus Torvalds #include <linux/module.h>
221da177e4SLinus Torvalds #include <linux/types.h>
231da177e4SLinus Torvalds #include <linux/socket.h>
241da177e4SLinus Torvalds #include <linux/kernel.h>
251da177e4SLinus Torvalds #include <linux/timer.h>
261da177e4SLinus Torvalds #include <linux/string.h>
271da177e4SLinus Torvalds #include <linux/sockios.h>
281da177e4SLinus Torvalds #include <linux/net.h>
291da177e4SLinus Torvalds #include <linux/fcntl.h>
301da177e4SLinus Torvalds #include <linux/mm.h>
311da177e4SLinus Torvalds #include <linux/slab.h>
321da177e4SLinus Torvalds #include <linux/interrupt.h>
331da177e4SLinus Torvalds #include <linux/capability.h>
341da177e4SLinus Torvalds #include <linux/skbuff.h>
351da177e4SLinus Torvalds #include <linux/init.h>
361da177e4SLinus Torvalds #include <linux/security.h>
376756ae4bSStephen Hemminger #include <linux/mutex.h>
381823730fSThomas Graf #include <linux/if_addr.h>
3977162022SJohn Fastabend #include <linux/if_bridge.h>
40f6f6424bSJiri Pirko #include <linux/if_vlan.h>
41ebc08a6fSWilliams, Mitch A #include <linux/pci.h>
4277162022SJohn Fastabend #include <linux/etherdevice.h>
4358038695SMartin KaFai Lau #include <linux/bpf.h>
441da177e4SLinus Torvalds 
457c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds #include <linux/inet.h>
481da177e4SLinus Torvalds #include <linux/netdevice.h>
4982f28412SJiri Pirko #include <net/switchdev.h>
501da177e4SLinus Torvalds #include <net/ip.h>
511da177e4SLinus Torvalds #include <net/protocol.h>
521da177e4SLinus Torvalds #include <net/arp.h>
531da177e4SLinus Torvalds #include <net/route.h>
541da177e4SLinus Torvalds #include <net/udp.h>
55ea697639SDaniel Borkmann #include <net/tcp.h>
561da177e4SLinus Torvalds #include <net/sock.h>
571da177e4SLinus Torvalds #include <net/pkt_sched.h>
5814c0b97dSThomas Graf #include <net/fib_rules.h>
59e2849863SThomas Graf #include <net/rtnetlink.h>
6030ffee84SJohannes Berg #include <net/net_namespace.h>
611da177e4SLinus Torvalds 
62e0d087afSEric Dumazet struct rtnl_link {
63e2849863SThomas Graf 	rtnl_doit_func		doit;
64e2849863SThomas Graf 	rtnl_dumpit_func	dumpit;
65*e4202511SFlorian Westphal 	struct module		*owner;
6662256f98SFlorian Westphal 	unsigned int		flags;
67addf9b90SFlorian Westphal 	struct rcu_head		rcu;
68e2849863SThomas Graf };
69e2849863SThomas Graf 
706756ae4bSStephen Hemminger static DEFINE_MUTEX(rtnl_mutex);
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds void rtnl_lock(void)
731da177e4SLinus Torvalds {
746756ae4bSStephen Hemminger 	mutex_lock(&rtnl_mutex);
751da177e4SLinus Torvalds }
76e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_lock);
771da177e4SLinus Torvalds 
781b5c5493SEric Dumazet static struct sk_buff *defer_kfree_skb_list;
791b5c5493SEric Dumazet void rtnl_kfree_skbs(struct sk_buff *head, struct sk_buff *tail)
801b5c5493SEric Dumazet {
811b5c5493SEric Dumazet 	if (head && tail) {
821b5c5493SEric Dumazet 		tail->next = defer_kfree_skb_list;
831b5c5493SEric Dumazet 		defer_kfree_skb_list = head;
841b5c5493SEric Dumazet 	}
851b5c5493SEric Dumazet }
861b5c5493SEric Dumazet EXPORT_SYMBOL(rtnl_kfree_skbs);
871b5c5493SEric Dumazet 
886756ae4bSStephen Hemminger void __rtnl_unlock(void)
891da177e4SLinus Torvalds {
901b5c5493SEric Dumazet 	struct sk_buff *head = defer_kfree_skb_list;
911b5c5493SEric Dumazet 
921b5c5493SEric Dumazet 	defer_kfree_skb_list = NULL;
931b5c5493SEric Dumazet 
946756ae4bSStephen Hemminger 	mutex_unlock(&rtnl_mutex);
951b5c5493SEric Dumazet 
961b5c5493SEric Dumazet 	while (head) {
971b5c5493SEric Dumazet 		struct sk_buff *next = head->next;
981b5c5493SEric Dumazet 
991b5c5493SEric Dumazet 		kfree_skb(head);
1001b5c5493SEric Dumazet 		cond_resched();
1011b5c5493SEric Dumazet 		head = next;
1021b5c5493SEric Dumazet 	}
1031da177e4SLinus Torvalds }
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds void rtnl_unlock(void)
1061da177e4SLinus Torvalds {
10758ec3b4dSHerbert Xu 	/* This fellow will unlock it for us. */
1081da177e4SLinus Torvalds 	netdev_run_todo();
1091da177e4SLinus Torvalds }
110e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unlock);
1111da177e4SLinus Torvalds 
1126756ae4bSStephen Hemminger int rtnl_trylock(void)
1136756ae4bSStephen Hemminger {
1146756ae4bSStephen Hemminger 	return mutex_trylock(&rtnl_mutex);
1156756ae4bSStephen Hemminger }
116e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_trylock);
1176756ae4bSStephen Hemminger 
118c9c1014bSPatrick McHardy int rtnl_is_locked(void)
119c9c1014bSPatrick McHardy {
120c9c1014bSPatrick McHardy 	return mutex_is_locked(&rtnl_mutex);
121c9c1014bSPatrick McHardy }
122e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_is_locked);
123c9c1014bSPatrick McHardy 
124a898def2SPaul E. McKenney #ifdef CONFIG_PROVE_LOCKING
1250cbf3343SYaowei Bai bool lockdep_rtnl_is_held(void)
126a898def2SPaul E. McKenney {
127a898def2SPaul E. McKenney 	return lockdep_is_held(&rtnl_mutex);
128a898def2SPaul E. McKenney }
129a898def2SPaul E. McKenney EXPORT_SYMBOL(lockdep_rtnl_is_held);
130a898def2SPaul E. McKenney #endif /* #ifdef CONFIG_PROVE_LOCKING */
131a898def2SPaul E. McKenney 
132addf9b90SFlorian Westphal static struct rtnl_link __rcu **rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
133e2849863SThomas Graf 
134e2849863SThomas Graf static inline int rtm_msgindex(int msgtype)
135e2849863SThomas Graf {
136e2849863SThomas Graf 	int msgindex = msgtype - RTM_BASE;
137e2849863SThomas Graf 
138e2849863SThomas Graf 	/*
139e2849863SThomas Graf 	 * msgindex < 0 implies someone tried to register a netlink
140e2849863SThomas Graf 	 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
141e2849863SThomas Graf 	 * the message type has not been added to linux/rtnetlink.h
142e2849863SThomas Graf 	 */
143e2849863SThomas Graf 	BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
144e2849863SThomas Graf 
145e2849863SThomas Graf 	return msgindex;
146e2849863SThomas Graf }
147e2849863SThomas Graf 
148addf9b90SFlorian Westphal static struct rtnl_link *rtnl_get_link(int protocol, int msgtype)
149addf9b90SFlorian Westphal {
150addf9b90SFlorian Westphal 	struct rtnl_link **tab;
151addf9b90SFlorian Westphal 
152addf9b90SFlorian Westphal 	if (protocol >= ARRAY_SIZE(rtnl_msg_handlers))
153addf9b90SFlorian Westphal 		protocol = PF_UNSPEC;
154addf9b90SFlorian Westphal 
155addf9b90SFlorian Westphal 	tab = rcu_dereference_rtnl(rtnl_msg_handlers[protocol]);
156addf9b90SFlorian Westphal 	if (!tab)
157addf9b90SFlorian Westphal 		tab = rcu_dereference_rtnl(rtnl_msg_handlers[PF_UNSPEC]);
158addf9b90SFlorian Westphal 
159addf9b90SFlorian Westphal 	return tab[msgtype];
160addf9b90SFlorian Westphal }
161addf9b90SFlorian Westphal 
162*e4202511SFlorian Westphal static int rtnl_register_internal(struct module *owner,
163*e4202511SFlorian Westphal 				  int protocol, int msgtype,
164c7ac8679SGreg Rose 				  rtnl_doit_func doit, rtnl_dumpit_func dumpit,
165b97bac64SFlorian Westphal 				  unsigned int flags)
166e2849863SThomas Graf {
167addf9b90SFlorian Westphal 	struct rtnl_link **tab, *link, *old;
168e2849863SThomas Graf 	int msgindex;
169addf9b90SFlorian Westphal 	int ret = -ENOBUFS;
170e2849863SThomas Graf 
17125239ceeSPatrick McHardy 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
172e2849863SThomas Graf 	msgindex = rtm_msgindex(msgtype);
173e2849863SThomas Graf 
174addf9b90SFlorian Westphal 	rtnl_lock();
175addf9b90SFlorian Westphal 	tab = rtnl_msg_handlers[protocol];
176e2849863SThomas Graf 	if (tab == NULL) {
177addf9b90SFlorian Westphal 		tab = kcalloc(RTM_NR_MSGTYPES, sizeof(void *), GFP_KERNEL);
178addf9b90SFlorian Westphal 		if (!tab)
179addf9b90SFlorian Westphal 			goto unlock;
180e2849863SThomas Graf 
181addf9b90SFlorian Westphal 		/* ensures we see the 0 stores */
1826853dd48SFlorian Westphal 		rcu_assign_pointer(rtnl_msg_handlers[protocol], tab);
183e2849863SThomas Graf 	}
184e2849863SThomas Graf 
185addf9b90SFlorian Westphal 	old = rtnl_dereference(tab[msgindex]);
186addf9b90SFlorian Westphal 	if (old) {
187addf9b90SFlorian Westphal 		link = kmemdup(old, sizeof(*old), GFP_KERNEL);
188addf9b90SFlorian Westphal 		if (!link)
189addf9b90SFlorian Westphal 			goto unlock;
190addf9b90SFlorian Westphal 	} else {
191addf9b90SFlorian Westphal 		link = kzalloc(sizeof(*link), GFP_KERNEL);
192addf9b90SFlorian Westphal 		if (!link)
193addf9b90SFlorian Westphal 			goto unlock;
194addf9b90SFlorian Westphal 	}
195e2849863SThomas Graf 
196*e4202511SFlorian Westphal 	WARN_ON(link->owner && link->owner != owner);
197*e4202511SFlorian Westphal 	link->owner = owner;
198*e4202511SFlorian Westphal 
199addf9b90SFlorian Westphal 	WARN_ON(doit && link->doit && link->doit != doit);
200addf9b90SFlorian Westphal 	if (doit)
201addf9b90SFlorian Westphal 		link->doit = doit;
202addf9b90SFlorian Westphal 	WARN_ON(dumpit && link->dumpit && link->dumpit != dumpit);
203addf9b90SFlorian Westphal 	if (dumpit)
204addf9b90SFlorian Westphal 		link->dumpit = dumpit;
205addf9b90SFlorian Westphal 
206addf9b90SFlorian Westphal 	link->flags |= flags;
207addf9b90SFlorian Westphal 
208addf9b90SFlorian Westphal 	/* publish protocol:msgtype */
209addf9b90SFlorian Westphal 	rcu_assign_pointer(tab[msgindex], link);
210addf9b90SFlorian Westphal 	ret = 0;
211addf9b90SFlorian Westphal 	if (old)
212addf9b90SFlorian Westphal 		kfree_rcu(old, rcu);
213addf9b90SFlorian Westphal unlock:
214addf9b90SFlorian Westphal 	rtnl_unlock();
215addf9b90SFlorian Westphal 	return ret;
216e2849863SThomas Graf }
217*e4202511SFlorian Westphal 
218*e4202511SFlorian Westphal /**
219*e4202511SFlorian Westphal  * rtnl_register_module - Register a rtnetlink message type
220*e4202511SFlorian Westphal  *
221*e4202511SFlorian Westphal  * @owner: module registering the hook (THIS_MODULE)
222*e4202511SFlorian Westphal  * @protocol: Protocol family or PF_UNSPEC
223*e4202511SFlorian Westphal  * @msgtype: rtnetlink message type
224*e4202511SFlorian Westphal  * @doit: Function pointer called for each request message
225*e4202511SFlorian Westphal  * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
226*e4202511SFlorian Westphal  * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
227*e4202511SFlorian Westphal  *
228*e4202511SFlorian Westphal  * Like rtnl_register, but for use by removable modules.
229*e4202511SFlorian Westphal  */
230*e4202511SFlorian Westphal int rtnl_register_module(struct module *owner,
231*e4202511SFlorian Westphal 			 int protocol, int msgtype,
232*e4202511SFlorian Westphal 			 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
233*e4202511SFlorian Westphal 			 unsigned int flags)
234*e4202511SFlorian Westphal {
235*e4202511SFlorian Westphal 	return rtnl_register_internal(owner, protocol, msgtype,
236*e4202511SFlorian Westphal 				      doit, dumpit, flags);
237*e4202511SFlorian Westphal }
238*e4202511SFlorian Westphal EXPORT_SYMBOL_GPL(rtnl_register_module);
239*e4202511SFlorian Westphal 
240*e4202511SFlorian Westphal /**
241*e4202511SFlorian Westphal  * __rtnl_register - Register a rtnetlink message type
242*e4202511SFlorian Westphal  * @protocol: Protocol family or PF_UNSPEC
243*e4202511SFlorian Westphal  * @msgtype: rtnetlink message type
244*e4202511SFlorian Westphal  * @doit: Function pointer called for each request message
245*e4202511SFlorian Westphal  * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
246*e4202511SFlorian Westphal  * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
247*e4202511SFlorian Westphal  *
248*e4202511SFlorian Westphal  * Registers the specified function pointers (at least one of them has
249*e4202511SFlorian Westphal  * to be non-NULL) to be called whenever a request message for the
250*e4202511SFlorian Westphal  * specified protocol family and message type is received.
251*e4202511SFlorian Westphal  *
252*e4202511SFlorian Westphal  * The special protocol family PF_UNSPEC may be used to define fallback
253*e4202511SFlorian Westphal  * function pointers for the case when no entry for the specific protocol
254*e4202511SFlorian Westphal  * family exists.
255*e4202511SFlorian Westphal  *
256*e4202511SFlorian Westphal  * Returns 0 on success or a negative error code.
257*e4202511SFlorian Westphal  */
258*e4202511SFlorian Westphal int __rtnl_register(int protocol, int msgtype,
259*e4202511SFlorian Westphal 		    rtnl_doit_func doit, rtnl_dumpit_func dumpit,
260*e4202511SFlorian Westphal 		    unsigned int flags)
261*e4202511SFlorian Westphal {
262*e4202511SFlorian Westphal 	return rtnl_register_internal(NULL, protocol, msgtype,
263*e4202511SFlorian Westphal 				      doit, dumpit, flags);
264*e4202511SFlorian Westphal }
265e2849863SThomas Graf EXPORT_SYMBOL_GPL(__rtnl_register);
266e2849863SThomas Graf 
267e2849863SThomas Graf /**
268e2849863SThomas Graf  * rtnl_register - Register a rtnetlink message type
269e2849863SThomas Graf  *
270e2849863SThomas Graf  * Identical to __rtnl_register() but panics on failure. This is useful
271e2849863SThomas Graf  * as failure of this function is very unlikely, it can only happen due
272e2849863SThomas Graf  * to lack of memory when allocating the chain to store all message
273e2849863SThomas Graf  * handlers for a protocol. Meant for use in init functions where lack
27425985edcSLucas De Marchi  * of memory implies no sense in continuing.
275e2849863SThomas Graf  */
276e2849863SThomas Graf void rtnl_register(int protocol, int msgtype,
277c7ac8679SGreg Rose 		   rtnl_doit_func doit, rtnl_dumpit_func dumpit,
278b97bac64SFlorian Westphal 		   unsigned int flags)
279e2849863SThomas Graf {
280b97bac64SFlorian Westphal 	if (__rtnl_register(protocol, msgtype, doit, dumpit, flags) < 0)
281e2849863SThomas Graf 		panic("Unable to register rtnetlink message handler, "
282e2849863SThomas Graf 		      "protocol = %d, message type = %d\n",
283e2849863SThomas Graf 		      protocol, msgtype);
284e2849863SThomas Graf }
285e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_register);
286e2849863SThomas Graf 
287e2849863SThomas Graf /**
288e2849863SThomas Graf  * rtnl_unregister - Unregister a rtnetlink message type
289e2849863SThomas Graf  * @protocol: Protocol family or PF_UNSPEC
290e2849863SThomas Graf  * @msgtype: rtnetlink message type
291e2849863SThomas Graf  *
292e2849863SThomas Graf  * Returns 0 on success or a negative error code.
293e2849863SThomas Graf  */
294e2849863SThomas Graf int rtnl_unregister(int protocol, int msgtype)
295e2849863SThomas Graf {
296addf9b90SFlorian Westphal 	struct rtnl_link **tab, *link;
297e2849863SThomas Graf 	int msgindex;
298e2849863SThomas Graf 
29925239ceeSPatrick McHardy 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
300e2849863SThomas Graf 	msgindex = rtm_msgindex(msgtype);
301e2849863SThomas Graf 
3026853dd48SFlorian Westphal 	rtnl_lock();
303addf9b90SFlorian Westphal 	tab = rtnl_dereference(rtnl_msg_handlers[protocol]);
304addf9b90SFlorian Westphal 	if (!tab) {
3056853dd48SFlorian Westphal 		rtnl_unlock();
306e2849863SThomas Graf 		return -ENOENT;
3076853dd48SFlorian Westphal 	}
308e2849863SThomas Graf 
309addf9b90SFlorian Westphal 	link = tab[msgindex];
310addf9b90SFlorian Westphal 	rcu_assign_pointer(tab[msgindex], NULL);
3116853dd48SFlorian Westphal 	rtnl_unlock();
312e2849863SThomas Graf 
313addf9b90SFlorian Westphal 	kfree_rcu(link, rcu);
314addf9b90SFlorian Westphal 
315e2849863SThomas Graf 	return 0;
316e2849863SThomas Graf }
317e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister);
318e2849863SThomas Graf 
319e2849863SThomas Graf /**
320e2849863SThomas Graf  * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
321e2849863SThomas Graf  * @protocol : Protocol family or PF_UNSPEC
322e2849863SThomas Graf  *
323e2849863SThomas Graf  * Identical to calling rtnl_unregster() for all registered message types
324e2849863SThomas Graf  * of a certain protocol family.
325e2849863SThomas Graf  */
326e2849863SThomas Graf void rtnl_unregister_all(int protocol)
327e2849863SThomas Graf {
328addf9b90SFlorian Westphal 	struct rtnl_link **tab, *link;
329addf9b90SFlorian Westphal 	int msgindex;
330019a3169SFlorian Westphal 
33125239ceeSPatrick McHardy 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
332e2849863SThomas Graf 
333019a3169SFlorian Westphal 	rtnl_lock();
334addf9b90SFlorian Westphal 	tab = rtnl_msg_handlers[protocol];
3356853dd48SFlorian Westphal 	RCU_INIT_POINTER(rtnl_msg_handlers[protocol], NULL);
336addf9b90SFlorian Westphal 	for (msgindex = 0; msgindex < RTM_NR_MSGTYPES; msgindex++) {
337addf9b90SFlorian Westphal 		link = tab[msgindex];
338addf9b90SFlorian Westphal 		if (!link)
339addf9b90SFlorian Westphal 			continue;
340addf9b90SFlorian Westphal 
341addf9b90SFlorian Westphal 		rcu_assign_pointer(tab[msgindex], NULL);
342addf9b90SFlorian Westphal 		kfree_rcu(link, rcu);
343addf9b90SFlorian Westphal 	}
344019a3169SFlorian Westphal 	rtnl_unlock();
345019a3169SFlorian Westphal 
3466853dd48SFlorian Westphal 	synchronize_net();
3476853dd48SFlorian Westphal 
348addf9b90SFlorian Westphal 	kfree(tab);
349e2849863SThomas Graf }
350e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister_all);
3511da177e4SLinus Torvalds 
35238f7b870SPatrick McHardy static LIST_HEAD(link_ops);
35338f7b870SPatrick McHardy 
354c63044f0SEric Dumazet static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
355c63044f0SEric Dumazet {
356c63044f0SEric Dumazet 	const struct rtnl_link_ops *ops;
357c63044f0SEric Dumazet 
358c63044f0SEric Dumazet 	list_for_each_entry(ops, &link_ops, list) {
359c63044f0SEric Dumazet 		if (!strcmp(ops->kind, kind))
360c63044f0SEric Dumazet 			return ops;
361c63044f0SEric Dumazet 	}
362c63044f0SEric Dumazet 	return NULL;
363c63044f0SEric Dumazet }
364c63044f0SEric Dumazet 
36538f7b870SPatrick McHardy /**
36638f7b870SPatrick McHardy  * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
36738f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to register
36838f7b870SPatrick McHardy  *
36938f7b870SPatrick McHardy  * The caller must hold the rtnl_mutex. This function should be used
37038f7b870SPatrick McHardy  * by drivers that create devices during module initialization. It
37138f7b870SPatrick McHardy  * must be called before registering the devices.
37238f7b870SPatrick McHardy  *
37338f7b870SPatrick McHardy  * Returns 0 on success or a negative error code.
37438f7b870SPatrick McHardy  */
37538f7b870SPatrick McHardy int __rtnl_link_register(struct rtnl_link_ops *ops)
37638f7b870SPatrick McHardy {
377c63044f0SEric Dumazet 	if (rtnl_link_ops_get(ops->kind))
378c63044f0SEric Dumazet 		return -EEXIST;
379c63044f0SEric Dumazet 
380b0ab2fabSJiri Pirko 	/* The check for setup is here because if ops
381b0ab2fabSJiri Pirko 	 * does not have that filled up, it is not possible
382b0ab2fabSJiri Pirko 	 * to use the ops for creating device. So do not
383b0ab2fabSJiri Pirko 	 * fill up dellink as well. That disables rtnl_dellink.
384b0ab2fabSJiri Pirko 	 */
385b0ab2fabSJiri Pirko 	if (ops->setup && !ops->dellink)
38623289a37SEric Dumazet 		ops->dellink = unregister_netdevice_queue;
3872d85cba2SPatrick McHardy 
38838f7b870SPatrick McHardy 	list_add_tail(&ops->list, &link_ops);
38938f7b870SPatrick McHardy 	return 0;
39038f7b870SPatrick McHardy }
39138f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_register);
39238f7b870SPatrick McHardy 
39338f7b870SPatrick McHardy /**
39438f7b870SPatrick McHardy  * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
39538f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to register
39638f7b870SPatrick McHardy  *
39738f7b870SPatrick McHardy  * Returns 0 on success or a negative error code.
39838f7b870SPatrick McHardy  */
39938f7b870SPatrick McHardy int rtnl_link_register(struct rtnl_link_ops *ops)
40038f7b870SPatrick McHardy {
40138f7b870SPatrick McHardy 	int err;
40238f7b870SPatrick McHardy 
40338f7b870SPatrick McHardy 	rtnl_lock();
40438f7b870SPatrick McHardy 	err = __rtnl_link_register(ops);
40538f7b870SPatrick McHardy 	rtnl_unlock();
40638f7b870SPatrick McHardy 	return err;
40738f7b870SPatrick McHardy }
40838f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_register);
40938f7b870SPatrick McHardy 
410669f87baSPavel Emelyanov static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
411669f87baSPavel Emelyanov {
412669f87baSPavel Emelyanov 	struct net_device *dev;
41323289a37SEric Dumazet 	LIST_HEAD(list_kill);
41423289a37SEric Dumazet 
415669f87baSPavel Emelyanov 	for_each_netdev(net, dev) {
41623289a37SEric Dumazet 		if (dev->rtnl_link_ops == ops)
41723289a37SEric Dumazet 			ops->dellink(dev, &list_kill);
418669f87baSPavel Emelyanov 	}
41923289a37SEric Dumazet 	unregister_netdevice_many(&list_kill);
420669f87baSPavel Emelyanov }
421669f87baSPavel Emelyanov 
42238f7b870SPatrick McHardy /**
42338f7b870SPatrick McHardy  * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
42438f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to unregister
42538f7b870SPatrick McHardy  *
4262d85cba2SPatrick McHardy  * The caller must hold the rtnl_mutex.
42738f7b870SPatrick McHardy  */
42838f7b870SPatrick McHardy void __rtnl_link_unregister(struct rtnl_link_ops *ops)
42938f7b870SPatrick McHardy {
430881d966bSEric W. Biederman 	struct net *net;
4312d85cba2SPatrick McHardy 
432881d966bSEric W. Biederman 	for_each_net(net) {
433669f87baSPavel Emelyanov 		__rtnl_kill_links(net, ops);
434881d966bSEric W. Biederman 	}
43538f7b870SPatrick McHardy 	list_del(&ops->list);
43638f7b870SPatrick McHardy }
43738f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
43838f7b870SPatrick McHardy 
439200b916fSCong Wang /* Return with the rtnl_lock held when there are no network
440200b916fSCong Wang  * devices unregistering in any network namespace.
441200b916fSCong Wang  */
442200b916fSCong Wang static void rtnl_lock_unregistering_all(void)
443200b916fSCong Wang {
444200b916fSCong Wang 	struct net *net;
445200b916fSCong Wang 	bool unregistering;
446ff960a73SPeter Zijlstra 	DEFINE_WAIT_FUNC(wait, woken_wake_function);
447200b916fSCong Wang 
448ff960a73SPeter Zijlstra 	add_wait_queue(&netdev_unregistering_wq, &wait);
449200b916fSCong Wang 	for (;;) {
450200b916fSCong Wang 		unregistering = false;
451200b916fSCong Wang 		rtnl_lock();
452200b916fSCong Wang 		for_each_net(net) {
453200b916fSCong Wang 			if (net->dev_unreg_count > 0) {
454200b916fSCong Wang 				unregistering = true;
455200b916fSCong Wang 				break;
456200b916fSCong Wang 			}
457200b916fSCong Wang 		}
458200b916fSCong Wang 		if (!unregistering)
459200b916fSCong Wang 			break;
460200b916fSCong Wang 		__rtnl_unlock();
461ff960a73SPeter Zijlstra 
462ff960a73SPeter Zijlstra 		wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
463200b916fSCong Wang 	}
464ff960a73SPeter Zijlstra 	remove_wait_queue(&netdev_unregistering_wq, &wait);
465200b916fSCong Wang }
466200b916fSCong Wang 
46738f7b870SPatrick McHardy /**
46838f7b870SPatrick McHardy  * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
46938f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to unregister
47038f7b870SPatrick McHardy  */
47138f7b870SPatrick McHardy void rtnl_link_unregister(struct rtnl_link_ops *ops)
47238f7b870SPatrick McHardy {
473200b916fSCong Wang 	/* Close the race with cleanup_net() */
474200b916fSCong Wang 	mutex_lock(&net_mutex);
475200b916fSCong Wang 	rtnl_lock_unregistering_all();
47638f7b870SPatrick McHardy 	__rtnl_link_unregister(ops);
47738f7b870SPatrick McHardy 	rtnl_unlock();
478200b916fSCong Wang 	mutex_unlock(&net_mutex);
47938f7b870SPatrick McHardy }
48038f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_unregister);
48138f7b870SPatrick McHardy 
482ba7d49b1SJiri Pirko static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
483ba7d49b1SJiri Pirko {
484ba7d49b1SJiri Pirko 	struct net_device *master_dev;
485ba7d49b1SJiri Pirko 	const struct rtnl_link_ops *ops;
4868515ae38SFlorian Westphal 	size_t size = 0;
487ba7d49b1SJiri Pirko 
4888515ae38SFlorian Westphal 	rcu_read_lock();
4898515ae38SFlorian Westphal 
4908515ae38SFlorian Westphal 	master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
491ba7d49b1SJiri Pirko 	if (!master_dev)
4928515ae38SFlorian Westphal 		goto out;
4938515ae38SFlorian Westphal 
494ba7d49b1SJiri Pirko 	ops = master_dev->rtnl_link_ops;
4956049f253SFernando Luis Vazquez Cao 	if (!ops || !ops->get_slave_size)
4968515ae38SFlorian Westphal 		goto out;
497ba7d49b1SJiri Pirko 	/* IFLA_INFO_SLAVE_DATA + nested data */
4988515ae38SFlorian Westphal 	size = nla_total_size(sizeof(struct nlattr)) +
499ba7d49b1SJiri Pirko 	       ops->get_slave_size(master_dev, dev);
5008515ae38SFlorian Westphal 
5018515ae38SFlorian Westphal out:
5028515ae38SFlorian Westphal 	rcu_read_unlock();
5038515ae38SFlorian Westphal 	return size;
504ba7d49b1SJiri Pirko }
505ba7d49b1SJiri Pirko 
50638f7b870SPatrick McHardy static size_t rtnl_link_get_size(const struct net_device *dev)
50738f7b870SPatrick McHardy {
50838f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
50938f7b870SPatrick McHardy 	size_t size;
51038f7b870SPatrick McHardy 
51138f7b870SPatrick McHardy 	if (!ops)
51238f7b870SPatrick McHardy 		return 0;
51338f7b870SPatrick McHardy 
514369cf77aSThomas Graf 	size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
515369cf77aSThomas Graf 	       nla_total_size(strlen(ops->kind) + 1);  /* IFLA_INFO_KIND */
51638f7b870SPatrick McHardy 
51738f7b870SPatrick McHardy 	if (ops->get_size)
51838f7b870SPatrick McHardy 		/* IFLA_INFO_DATA + nested data */
519369cf77aSThomas Graf 		size += nla_total_size(sizeof(struct nlattr)) +
52038f7b870SPatrick McHardy 			ops->get_size(dev);
52138f7b870SPatrick McHardy 
52238f7b870SPatrick McHardy 	if (ops->get_xstats_size)
523369cf77aSThomas Graf 		/* IFLA_INFO_XSTATS */
524369cf77aSThomas Graf 		size += nla_total_size(ops->get_xstats_size(dev));
52538f7b870SPatrick McHardy 
526ba7d49b1SJiri Pirko 	size += rtnl_link_get_slave_info_data_size(dev);
527ba7d49b1SJiri Pirko 
52838f7b870SPatrick McHardy 	return size;
52938f7b870SPatrick McHardy }
53038f7b870SPatrick McHardy 
531f8ff182cSThomas Graf static LIST_HEAD(rtnl_af_ops);
532f8ff182cSThomas Graf 
533f8ff182cSThomas Graf static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
534f8ff182cSThomas Graf {
535f8ff182cSThomas Graf 	const struct rtnl_af_ops *ops;
536f8ff182cSThomas Graf 
5375fa85a09SFlorian Westphal 	list_for_each_entry_rcu(ops, &rtnl_af_ops, list) {
538f8ff182cSThomas Graf 		if (ops->family == family)
539f8ff182cSThomas Graf 			return ops;
540f8ff182cSThomas Graf 	}
541f8ff182cSThomas Graf 
542f8ff182cSThomas Graf 	return NULL;
543f8ff182cSThomas Graf }
544f8ff182cSThomas Graf 
545f8ff182cSThomas Graf /**
546f8ff182cSThomas Graf  * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
547f8ff182cSThomas Graf  * @ops: struct rtnl_af_ops * to register
548f8ff182cSThomas Graf  *
549f8ff182cSThomas Graf  * Returns 0 on success or a negative error code.
550f8ff182cSThomas Graf  */
5513678a9d8Sstephen hemminger void rtnl_af_register(struct rtnl_af_ops *ops)
552f8ff182cSThomas Graf {
553f8ff182cSThomas Graf 	rtnl_lock();
5545fa85a09SFlorian Westphal 	list_add_tail_rcu(&ops->list, &rtnl_af_ops);
555f8ff182cSThomas Graf 	rtnl_unlock();
556f8ff182cSThomas Graf }
557f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_register);
558f8ff182cSThomas Graf 
559f8ff182cSThomas Graf /**
560f8ff182cSThomas Graf  * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
561f8ff182cSThomas Graf  * @ops: struct rtnl_af_ops * to unregister
562f8ff182cSThomas Graf  */
563f8ff182cSThomas Graf void rtnl_af_unregister(struct rtnl_af_ops *ops)
564f8ff182cSThomas Graf {
565f8ff182cSThomas Graf 	rtnl_lock();
5665fa85a09SFlorian Westphal 	list_del_rcu(&ops->list);
567f8ff182cSThomas Graf 	rtnl_unlock();
5685fa85a09SFlorian Westphal 
5695fa85a09SFlorian Westphal 	synchronize_rcu();
570f8ff182cSThomas Graf }
571f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_unregister);
572f8ff182cSThomas Graf 
573b1974ed0SArad, Ronen static size_t rtnl_link_get_af_size(const struct net_device *dev,
574b1974ed0SArad, Ronen 				    u32 ext_filter_mask)
575f8ff182cSThomas Graf {
576f8ff182cSThomas Graf 	struct rtnl_af_ops *af_ops;
577f8ff182cSThomas Graf 	size_t size;
578f8ff182cSThomas Graf 
579f8ff182cSThomas Graf 	/* IFLA_AF_SPEC */
580f8ff182cSThomas Graf 	size = nla_total_size(sizeof(struct nlattr));
581f8ff182cSThomas Graf 
5825fa85a09SFlorian Westphal 	rcu_read_lock();
5835fa85a09SFlorian Westphal 	list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
584f8ff182cSThomas Graf 		if (af_ops->get_link_af_size) {
585f8ff182cSThomas Graf 			/* AF_* + nested data */
586f8ff182cSThomas Graf 			size += nla_total_size(sizeof(struct nlattr)) +
587b1974ed0SArad, Ronen 				af_ops->get_link_af_size(dev, ext_filter_mask);
588f8ff182cSThomas Graf 		}
589f8ff182cSThomas Graf 	}
5905fa85a09SFlorian Westphal 	rcu_read_unlock();
591f8ff182cSThomas Graf 
592f8ff182cSThomas Graf 	return size;
593f8ff182cSThomas Graf }
594f8ff182cSThomas Graf 
595ba7d49b1SJiri Pirko static bool rtnl_have_link_slave_info(const struct net_device *dev)
596ba7d49b1SJiri Pirko {
597ba7d49b1SJiri Pirko 	struct net_device *master_dev;
5984c82a95eSFlorian Westphal 	bool ret = false;
599ba7d49b1SJiri Pirko 
6004c82a95eSFlorian Westphal 	rcu_read_lock();
6014c82a95eSFlorian Westphal 
6024c82a95eSFlorian Westphal 	master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
603813f020cSJiri Pirko 	if (master_dev && master_dev->rtnl_link_ops)
6044c82a95eSFlorian Westphal 		ret = true;
6054c82a95eSFlorian Westphal 	rcu_read_unlock();
6064c82a95eSFlorian Westphal 	return ret;
607ba7d49b1SJiri Pirko }
608ba7d49b1SJiri Pirko 
609ba7d49b1SJiri Pirko static int rtnl_link_slave_info_fill(struct sk_buff *skb,
610ba7d49b1SJiri Pirko 				     const struct net_device *dev)
611ba7d49b1SJiri Pirko {
612ba7d49b1SJiri Pirko 	struct net_device *master_dev;
613ba7d49b1SJiri Pirko 	const struct rtnl_link_ops *ops;
614ba7d49b1SJiri Pirko 	struct nlattr *slave_data;
615ba7d49b1SJiri Pirko 	int err;
616ba7d49b1SJiri Pirko 
617ba7d49b1SJiri Pirko 	master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
618ba7d49b1SJiri Pirko 	if (!master_dev)
619ba7d49b1SJiri Pirko 		return 0;
620ba7d49b1SJiri Pirko 	ops = master_dev->rtnl_link_ops;
621ba7d49b1SJiri Pirko 	if (!ops)
622ba7d49b1SJiri Pirko 		return 0;
623ba7d49b1SJiri Pirko 	if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
624ba7d49b1SJiri Pirko 		return -EMSGSIZE;
625ba7d49b1SJiri Pirko 	if (ops->fill_slave_info) {
626ba7d49b1SJiri Pirko 		slave_data = nla_nest_start(skb, IFLA_INFO_SLAVE_DATA);
627ba7d49b1SJiri Pirko 		if (!slave_data)
628ba7d49b1SJiri Pirko 			return -EMSGSIZE;
629ba7d49b1SJiri Pirko 		err = ops->fill_slave_info(skb, master_dev, dev);
630ba7d49b1SJiri Pirko 		if (err < 0)
631ba7d49b1SJiri Pirko 			goto err_cancel_slave_data;
632ba7d49b1SJiri Pirko 		nla_nest_end(skb, slave_data);
633ba7d49b1SJiri Pirko 	}
634ba7d49b1SJiri Pirko 	return 0;
635ba7d49b1SJiri Pirko 
636ba7d49b1SJiri Pirko err_cancel_slave_data:
637ba7d49b1SJiri Pirko 	nla_nest_cancel(skb, slave_data);
638ba7d49b1SJiri Pirko 	return err;
639ba7d49b1SJiri Pirko }
640ba7d49b1SJiri Pirko 
641ba7d49b1SJiri Pirko static int rtnl_link_info_fill(struct sk_buff *skb,
642ba7d49b1SJiri Pirko 			       const struct net_device *dev)
64338f7b870SPatrick McHardy {
64438f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
645ba7d49b1SJiri Pirko 	struct nlattr *data;
646ba7d49b1SJiri Pirko 	int err;
647ba7d49b1SJiri Pirko 
648ba7d49b1SJiri Pirko 	if (!ops)
649ba7d49b1SJiri Pirko 		return 0;
650ba7d49b1SJiri Pirko 	if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
651ba7d49b1SJiri Pirko 		return -EMSGSIZE;
652ba7d49b1SJiri Pirko 	if (ops->fill_xstats) {
653ba7d49b1SJiri Pirko 		err = ops->fill_xstats(skb, dev);
654ba7d49b1SJiri Pirko 		if (err < 0)
655ba7d49b1SJiri Pirko 			return err;
656ba7d49b1SJiri Pirko 	}
657ba7d49b1SJiri Pirko 	if (ops->fill_info) {
658ba7d49b1SJiri Pirko 		data = nla_nest_start(skb, IFLA_INFO_DATA);
659ba7d49b1SJiri Pirko 		if (data == NULL)
660ba7d49b1SJiri Pirko 			return -EMSGSIZE;
661ba7d49b1SJiri Pirko 		err = ops->fill_info(skb, dev);
662ba7d49b1SJiri Pirko 		if (err < 0)
663ba7d49b1SJiri Pirko 			goto err_cancel_data;
664ba7d49b1SJiri Pirko 		nla_nest_end(skb, data);
665ba7d49b1SJiri Pirko 	}
666ba7d49b1SJiri Pirko 	return 0;
667ba7d49b1SJiri Pirko 
668ba7d49b1SJiri Pirko err_cancel_data:
669ba7d49b1SJiri Pirko 	nla_nest_cancel(skb, data);
670ba7d49b1SJiri Pirko 	return err;
671ba7d49b1SJiri Pirko }
672ba7d49b1SJiri Pirko 
673ba7d49b1SJiri Pirko static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
674ba7d49b1SJiri Pirko {
675ba7d49b1SJiri Pirko 	struct nlattr *linkinfo;
67638f7b870SPatrick McHardy 	int err = -EMSGSIZE;
67738f7b870SPatrick McHardy 
67838f7b870SPatrick McHardy 	linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
67938f7b870SPatrick McHardy 	if (linkinfo == NULL)
68038f7b870SPatrick McHardy 		goto out;
68138f7b870SPatrick McHardy 
682ba7d49b1SJiri Pirko 	err = rtnl_link_info_fill(skb, dev);
68338f7b870SPatrick McHardy 	if (err < 0)
68438f7b870SPatrick McHardy 		goto err_cancel_link;
685ba7d49b1SJiri Pirko 
686ba7d49b1SJiri Pirko 	err = rtnl_link_slave_info_fill(skb, dev);
68738f7b870SPatrick McHardy 	if (err < 0)
688ba7d49b1SJiri Pirko 		goto err_cancel_link;
68938f7b870SPatrick McHardy 
69038f7b870SPatrick McHardy 	nla_nest_end(skb, linkinfo);
69138f7b870SPatrick McHardy 	return 0;
69238f7b870SPatrick McHardy 
69338f7b870SPatrick McHardy err_cancel_link:
69438f7b870SPatrick McHardy 	nla_nest_cancel(skb, linkinfo);
69538f7b870SPatrick McHardy out:
69638f7b870SPatrick McHardy 	return err;
69738f7b870SPatrick McHardy }
69838f7b870SPatrick McHardy 
69995c96174SEric Dumazet int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
7001da177e4SLinus Torvalds {
70197c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
7021da177e4SLinus Torvalds 	int err = 0;
7031da177e4SLinus Torvalds 
704ac6d439dSPatrick McHardy 	NETLINK_CB(skb).dst_group = group;
7051da177e4SLinus Torvalds 	if (echo)
70663354797SReshetova, Elena 		refcount_inc(&skb->users);
7071da177e4SLinus Torvalds 	netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
7081da177e4SLinus Torvalds 	if (echo)
7091da177e4SLinus Torvalds 		err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
7101da177e4SLinus Torvalds 	return err;
7111da177e4SLinus Torvalds }
7121da177e4SLinus Torvalds 
71397c53cacSDenis V. Lunev int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
7142942e900SThomas Graf {
71597c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
71697c53cacSDenis V. Lunev 
7172942e900SThomas Graf 	return nlmsg_unicast(rtnl, skb, pid);
7182942e900SThomas Graf }
719e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unicast);
7202942e900SThomas Graf 
7211ce85fe4SPablo Neira Ayuso void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
72297676b6bSThomas Graf 		 struct nlmsghdr *nlh, gfp_t flags)
72397676b6bSThomas Graf {
72497c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
72597676b6bSThomas Graf 	int report = 0;
72697676b6bSThomas Graf 
72797676b6bSThomas Graf 	if (nlh)
72897676b6bSThomas Graf 		report = nlmsg_report(nlh);
72997676b6bSThomas Graf 
7301ce85fe4SPablo Neira Ayuso 	nlmsg_notify(rtnl, skb, pid, group, report, flags);
73197676b6bSThomas Graf }
732e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_notify);
73397676b6bSThomas Graf 
73497c53cacSDenis V. Lunev void rtnl_set_sk_err(struct net *net, u32 group, int error)
73597676b6bSThomas Graf {
73697c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
73797c53cacSDenis V. Lunev 
73897676b6bSThomas Graf 	netlink_set_err(rtnl, 0, group, error);
73997676b6bSThomas Graf }
740e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_set_sk_err);
74197676b6bSThomas Graf 
7421da177e4SLinus Torvalds int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
7431da177e4SLinus Torvalds {
7442d7202bfSThomas Graf 	struct nlattr *mx;
7452d7202bfSThomas Graf 	int i, valid = 0;
7461da177e4SLinus Torvalds 
7472d7202bfSThomas Graf 	mx = nla_nest_start(skb, RTA_METRICS);
7482d7202bfSThomas Graf 	if (mx == NULL)
7492d7202bfSThomas Graf 		return -ENOBUFS;
7502d7202bfSThomas Graf 
7511da177e4SLinus Torvalds 	for (i = 0; i < RTAX_MAX; i++) {
7522d7202bfSThomas Graf 		if (metrics[i]) {
753ea697639SDaniel Borkmann 			if (i == RTAX_CC_ALGO - 1) {
754ea697639SDaniel Borkmann 				char tmp[TCP_CA_NAME_MAX], *name;
755ea697639SDaniel Borkmann 
756ea697639SDaniel Borkmann 				name = tcp_ca_get_name_by_key(metrics[i], tmp);
757ea697639SDaniel Borkmann 				if (!name)
758ea697639SDaniel Borkmann 					continue;
759ea697639SDaniel Borkmann 				if (nla_put_string(skb, i + 1, name))
760ea697639SDaniel Borkmann 					goto nla_put_failure;
761c3a8d947SDaniel Borkmann 			} else if (i == RTAX_FEATURES - 1) {
762c3a8d947SDaniel Borkmann 				u32 user_features = metrics[i] & RTAX_FEATURE_MASK;
763c3a8d947SDaniel Borkmann 
764f8edcd12SPhil Sutter 				if (!user_features)
765f8edcd12SPhil Sutter 					continue;
766c3a8d947SDaniel Borkmann 				BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK);
767c3a8d947SDaniel Borkmann 				if (nla_put_u32(skb, i + 1, user_features))
768c3a8d947SDaniel Borkmann 					goto nla_put_failure;
769ea697639SDaniel Borkmann 			} else {
770a6574349SDavid S. Miller 				if (nla_put_u32(skb, i + 1, metrics[i]))
771a6574349SDavid S. Miller 					goto nla_put_failure;
7721da177e4SLinus Torvalds 			}
773ea697639SDaniel Borkmann 			valid++;
774ea697639SDaniel Borkmann 		}
7752d7202bfSThomas Graf 	}
7761da177e4SLinus Torvalds 
777a57d27fcSDavid S. Miller 	if (!valid) {
778a57d27fcSDavid S. Miller 		nla_nest_cancel(skb, mx);
779a57d27fcSDavid S. Miller 		return 0;
780a57d27fcSDavid S. Miller 	}
7812d7202bfSThomas Graf 
7822d7202bfSThomas Graf 	return nla_nest_end(skb, mx);
7832d7202bfSThomas Graf 
7842d7202bfSThomas Graf nla_put_failure:
785bc3ed28cSThomas Graf 	nla_nest_cancel(skb, mx);
786bc3ed28cSThomas Graf 	return -EMSGSIZE;
7871da177e4SLinus Torvalds }
788e0d087afSEric Dumazet EXPORT_SYMBOL(rtnetlink_put_metrics);
7891da177e4SLinus Torvalds 
790e3703b3dSThomas Graf int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
79187a50699SDavid S. Miller 		       long expires, u32 error)
792e3703b3dSThomas Graf {
793e3703b3dSThomas Graf 	struct rta_cacheinfo ci = {
794a399a805SEric Dumazet 		.rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse),
795e3703b3dSThomas Graf 		.rta_used = dst->__use,
796e3703b3dSThomas Graf 		.rta_clntref = atomic_read(&(dst->__refcnt)),
797e3703b3dSThomas Graf 		.rta_error = error,
798e3703b3dSThomas Graf 		.rta_id =  id,
799e3703b3dSThomas Graf 	};
800e3703b3dSThomas Graf 
8018253947eSLi Wei 	if (expires) {
8028253947eSLi Wei 		unsigned long clock;
803e3703b3dSThomas Graf 
8048253947eSLi Wei 		clock = jiffies_to_clock_t(abs(expires));
8058253947eSLi Wei 		clock = min_t(unsigned long, clock, INT_MAX);
8068253947eSLi Wei 		ci.rta_expires = (expires > 0) ? clock : -clock;
8078253947eSLi Wei 	}
808e3703b3dSThomas Graf 	return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
809e3703b3dSThomas Graf }
810e3703b3dSThomas Graf EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
8111da177e4SLinus Torvalds 
81293b2d4a2SDavid S. Miller static void set_operstate(struct net_device *dev, unsigned char transition)
813b00055aaSStefan Rompf {
814b00055aaSStefan Rompf 	unsigned char operstate = dev->operstate;
815b00055aaSStefan Rompf 
816b00055aaSStefan Rompf 	switch (transition) {
817b00055aaSStefan Rompf 	case IF_OPER_UP:
818b00055aaSStefan Rompf 		if ((operstate == IF_OPER_DORMANT ||
819b00055aaSStefan Rompf 		     operstate == IF_OPER_UNKNOWN) &&
820b00055aaSStefan Rompf 		    !netif_dormant(dev))
821b00055aaSStefan Rompf 			operstate = IF_OPER_UP;
822b00055aaSStefan Rompf 		break;
823b00055aaSStefan Rompf 
824b00055aaSStefan Rompf 	case IF_OPER_DORMANT:
825b00055aaSStefan Rompf 		if (operstate == IF_OPER_UP ||
826b00055aaSStefan Rompf 		    operstate == IF_OPER_UNKNOWN)
827b00055aaSStefan Rompf 			operstate = IF_OPER_DORMANT;
828b00055aaSStefan Rompf 		break;
8293ff50b79SStephen Hemminger 	}
830b00055aaSStefan Rompf 
831b00055aaSStefan Rompf 	if (dev->operstate != operstate) {
832b00055aaSStefan Rompf 		write_lock_bh(&dev_base_lock);
833b00055aaSStefan Rompf 		dev->operstate = operstate;
834b00055aaSStefan Rompf 		write_unlock_bh(&dev_base_lock);
835b00055aaSStefan Rompf 		netdev_state_change(dev);
83693b2d4a2SDavid S. Miller 	}
837b00055aaSStefan Rompf }
838b00055aaSStefan Rompf 
839b1beb681SJiri Benc static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
840b1beb681SJiri Benc {
841b1beb681SJiri Benc 	return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
842b1beb681SJiri Benc 	       (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
843b1beb681SJiri Benc }
844b1beb681SJiri Benc 
8453729d502SPatrick McHardy static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
8463729d502SPatrick McHardy 					   const struct ifinfomsg *ifm)
8473729d502SPatrick McHardy {
8483729d502SPatrick McHardy 	unsigned int flags = ifm->ifi_flags;
8493729d502SPatrick McHardy 
8503729d502SPatrick McHardy 	/* bugwards compatibility: ifi_change == 0 is treated as ~0 */
8513729d502SPatrick McHardy 	if (ifm->ifi_change)
8523729d502SPatrick McHardy 		flags = (flags & ifm->ifi_change) |
853b1beb681SJiri Benc 			(rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
8543729d502SPatrick McHardy 
8553729d502SPatrick McHardy 	return flags;
8563729d502SPatrick McHardy }
8573729d502SPatrick McHardy 
858b60c5115SThomas Graf static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
859be1f3c2cSBen Hutchings 				 const struct rtnl_link_stats64 *b)
8601da177e4SLinus Torvalds {
861b60c5115SThomas Graf 	a->rx_packets = b->rx_packets;
862b60c5115SThomas Graf 	a->tx_packets = b->tx_packets;
863b60c5115SThomas Graf 	a->rx_bytes = b->rx_bytes;
864b60c5115SThomas Graf 	a->tx_bytes = b->tx_bytes;
865b60c5115SThomas Graf 	a->rx_errors = b->rx_errors;
866b60c5115SThomas Graf 	a->tx_errors = b->tx_errors;
867b60c5115SThomas Graf 	a->rx_dropped = b->rx_dropped;
868b60c5115SThomas Graf 	a->tx_dropped = b->tx_dropped;
869b60c5115SThomas Graf 
870b60c5115SThomas Graf 	a->multicast = b->multicast;
871b60c5115SThomas Graf 	a->collisions = b->collisions;
872b60c5115SThomas Graf 
873b60c5115SThomas Graf 	a->rx_length_errors = b->rx_length_errors;
874b60c5115SThomas Graf 	a->rx_over_errors = b->rx_over_errors;
875b60c5115SThomas Graf 	a->rx_crc_errors = b->rx_crc_errors;
876b60c5115SThomas Graf 	a->rx_frame_errors = b->rx_frame_errors;
877b60c5115SThomas Graf 	a->rx_fifo_errors = b->rx_fifo_errors;
878b60c5115SThomas Graf 	a->rx_missed_errors = b->rx_missed_errors;
879b60c5115SThomas Graf 
880b60c5115SThomas Graf 	a->tx_aborted_errors = b->tx_aborted_errors;
881b60c5115SThomas Graf 	a->tx_carrier_errors = b->tx_carrier_errors;
882b60c5115SThomas Graf 	a->tx_fifo_errors = b->tx_fifo_errors;
883b60c5115SThomas Graf 	a->tx_heartbeat_errors = b->tx_heartbeat_errors;
884b60c5115SThomas Graf 	a->tx_window_errors = b->tx_window_errors;
885b60c5115SThomas Graf 
886b60c5115SThomas Graf 	a->rx_compressed = b->rx_compressed;
887b60c5115SThomas Graf 	a->tx_compressed = b->tx_compressed;
8886e7333d3SJarod Wilson 
8896e7333d3SJarod Wilson 	a->rx_nohandler = b->rx_nohandler;
89010708f37SJan Engelhardt }
89110708f37SJan Engelhardt 
892c02db8c6SChris Wright /* All VF info */
893115c9b81SGreg Rose static inline int rtnl_vfinfo_size(const struct net_device *dev,
894115c9b81SGreg Rose 				   u32 ext_filter_mask)
895ebc08a6fSWilliams, Mitch A {
8969af15c38SPhil Sutter 	if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) {
897c02db8c6SChris Wright 		int num_vfs = dev_num_vf(dev->dev.parent);
8987e75f74aSSabrina Dubroca 		size_t size = nla_total_size(0);
899045de01aSScott Feldman 		size += num_vfs *
9007e75f74aSSabrina Dubroca 			(nla_total_size(0) +
9017e75f74aSSabrina Dubroca 			 nla_total_size(sizeof(struct ifla_vf_mac)) +
9027e75f74aSSabrina Dubroca 			 nla_total_size(sizeof(struct ifla_vf_vlan)) +
9037e75f74aSSabrina Dubroca 			 nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
90479aab093SMoshe Shemesh 			 nla_total_size(MAX_VLAN_LIST_LEN *
90579aab093SMoshe Shemesh 					sizeof(struct ifla_vf_vlan_info)) +
906ed616689SSucheta Chakraborty 			 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
9077e75f74aSSabrina Dubroca 			 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
908945a3676SJiri Benc 			 nla_total_size(sizeof(struct ifla_vf_rate)) +
90901a3d796SVlad Zolotarov 			 nla_total_size(sizeof(struct ifla_vf_link_state)) +
9103b766cd8SEran Ben Elisha 			 nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
9117e75f74aSSabrina Dubroca 			 nla_total_size(0) + /* nest IFLA_VF_STATS */
9123b766cd8SEran Ben Elisha 			 /* IFLA_VF_STATS_RX_PACKETS */
913343a6d8eSNicolas Dichtel 			 nla_total_size_64bit(sizeof(__u64)) +
9143b766cd8SEran Ben Elisha 			 /* IFLA_VF_STATS_TX_PACKETS */
915343a6d8eSNicolas Dichtel 			 nla_total_size_64bit(sizeof(__u64)) +
9163b766cd8SEran Ben Elisha 			 /* IFLA_VF_STATS_RX_BYTES */
917343a6d8eSNicolas Dichtel 			 nla_total_size_64bit(sizeof(__u64)) +
9183b766cd8SEran Ben Elisha 			 /* IFLA_VF_STATS_TX_BYTES */
919343a6d8eSNicolas Dichtel 			 nla_total_size_64bit(sizeof(__u64)) +
9203b766cd8SEran Ben Elisha 			 /* IFLA_VF_STATS_BROADCAST */
921343a6d8eSNicolas Dichtel 			 nla_total_size_64bit(sizeof(__u64)) +
9223b766cd8SEran Ben Elisha 			 /* IFLA_VF_STATS_MULTICAST */
923343a6d8eSNicolas Dichtel 			 nla_total_size_64bit(sizeof(__u64)) +
924dd461d6aSHiroshi Shimamoto 			 nla_total_size(sizeof(struct ifla_vf_trust)));
925c02db8c6SChris Wright 		return size;
926c02db8c6SChris Wright 	} else
927ebc08a6fSWilliams, Mitch A 		return 0;
928ebc08a6fSWilliams, Mitch A }
929ebc08a6fSWilliams, Mitch A 
930c53864fdSDavid Gibson static size_t rtnl_port_size(const struct net_device *dev,
931c53864fdSDavid Gibson 			     u32 ext_filter_mask)
93257b61080SScott Feldman {
93357b61080SScott Feldman 	size_t port_size = nla_total_size(4)		/* PORT_VF */
93457b61080SScott Feldman 		+ nla_total_size(PORT_PROFILE_MAX)	/* PORT_PROFILE */
93557b61080SScott Feldman 		+ nla_total_size(PORT_UUID_MAX)		/* PORT_INSTANCE_UUID */
93657b61080SScott Feldman 		+ nla_total_size(PORT_UUID_MAX)		/* PORT_HOST_UUID */
93757b61080SScott Feldman 		+ nla_total_size(1)			/* PROT_VDP_REQUEST */
93857b61080SScott Feldman 		+ nla_total_size(2);			/* PORT_VDP_RESPONSE */
93957b61080SScott Feldman 	size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
94057b61080SScott Feldman 	size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
94157b61080SScott Feldman 		+ port_size;
94257b61080SScott Feldman 	size_t port_self_size = nla_total_size(sizeof(struct nlattr))
94357b61080SScott Feldman 		+ port_size;
94457b61080SScott Feldman 
945c53864fdSDavid Gibson 	if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
946c53864fdSDavid Gibson 	    !(ext_filter_mask & RTEXT_FILTER_VF))
94757b61080SScott Feldman 		return 0;
94857b61080SScott Feldman 	if (dev_num_vf(dev->dev.parent))
94957b61080SScott Feldman 		return port_self_size + vf_ports_size +
95057b61080SScott Feldman 			vf_port_size * dev_num_vf(dev->dev.parent);
95157b61080SScott Feldman 	else
95257b61080SScott Feldman 		return port_self_size;
95357b61080SScott Feldman }
95457b61080SScott Feldman 
955b5cdae32SDavid S. Miller static size_t rtnl_xdp_size(void)
956d1fdd913SBrenden Blanco {
957b3cfaa31SSabrina Dubroca 	size_t xdp_size = nla_total_size(0) +	/* nest IFLA_XDP */
95858038695SMartin KaFai Lau 			  nla_total_size(1) +	/* XDP_ATTACHED */
95958038695SMartin KaFai Lau 			  nla_total_size(4);	/* XDP_PROG_ID */
960d1fdd913SBrenden Blanco 
961d1fdd913SBrenden Blanco 	return xdp_size;
962d1fdd913SBrenden Blanco }
963d1fdd913SBrenden Blanco 
964115c9b81SGreg Rose static noinline size_t if_nlmsg_size(const struct net_device *dev,
965115c9b81SGreg Rose 				     u32 ext_filter_mask)
966339bf98fSThomas Graf {
967339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
968339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
9690b815a1aSStephen Hemminger 	       + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
970339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
971270cb4d0SNicolas Dichtel 	       + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap))
972339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rtnl_link_stats))
97335c58459SDavid S. Miller 	       + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
974339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
975339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
976339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_TXQLEN */
977339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_WEIGHT */
978339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MTU */
979339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_LINK */
980339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MASTER */
9819a57247fSJiri Pirko 	       + nla_total_size(1) /* IFLA_CARRIER */
982edbc0bb3SBen Greear 	       + nla_total_size(4) /* IFLA_PROMISCUITY */
98376ff5cc9SJiri Pirko 	       + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
98476ff5cc9SJiri Pirko 	       + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
9856919756cSTobias Klauser 	       + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
9866919756cSTobias Klauser 	       + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
987339bf98fSThomas Graf 	       + nla_total_size(1) /* IFLA_OPERSTATE */
98838f7b870SPatrick McHardy 	       + nla_total_size(1) /* IFLA_LINKMODE */
9892d3b479dSdavid decotigny 	       + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
990d37512a2SNicolas Dichtel 	       + nla_total_size(4) /* IFLA_LINK_NETNSID */
991db833d40SSerhey Popovych 	       + nla_total_size(4) /* IFLA_GROUP */
992115c9b81SGreg Rose 	       + nla_total_size(ext_filter_mask
993115c9b81SGreg Rose 			        & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
994115c9b81SGreg Rose 	       + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
995c53864fdSDavid Gibson 	       + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
996f8ff182cSThomas Graf 	       + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
997b1974ed0SArad, Ronen 	       + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
99882f28412SJiri Pirko 	       + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
99988d6378bSAnuradha Karuppiah 	       + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
1000c57c7a95SNicolas Dichtel 	       + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
1001b5cdae32SDavid S. Miller 	       + rtnl_xdp_size() /* IFLA_XDP */
10023d3ea5afSVlad Yasevich 	       + nla_total_size(4)  /* IFLA_EVENT */
10036621dd29SNicolas Dichtel 	       + nla_total_size(4)  /* IFLA_NEW_NETNSID */
100403ac738dSColin Ian King 	       + nla_total_size(1)  /* IFLA_PROTO_DOWN */
100579e1ad14SJiri Benc 	       + nla_total_size(4)  /* IFLA_IF_NETNSID */
100679e1ad14SJiri Benc 	       + 0;
1007339bf98fSThomas Graf }
1008339bf98fSThomas Graf 
100957b61080SScott Feldman static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
101057b61080SScott Feldman {
101157b61080SScott Feldman 	struct nlattr *vf_ports;
101257b61080SScott Feldman 	struct nlattr *vf_port;
101357b61080SScott Feldman 	int vf;
101457b61080SScott Feldman 	int err;
101557b61080SScott Feldman 
101657b61080SScott Feldman 	vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
101757b61080SScott Feldman 	if (!vf_ports)
101857b61080SScott Feldman 		return -EMSGSIZE;
101957b61080SScott Feldman 
102057b61080SScott Feldman 	for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
102157b61080SScott Feldman 		vf_port = nla_nest_start(skb, IFLA_VF_PORT);
10228ca94183SScott Feldman 		if (!vf_port)
10238ca94183SScott Feldman 			goto nla_put_failure;
1024a6574349SDavid S. Miller 		if (nla_put_u32(skb, IFLA_PORT_VF, vf))
1025a6574349SDavid S. Miller 			goto nla_put_failure;
102657b61080SScott Feldman 		err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
10278ca94183SScott Feldman 		if (err == -EMSGSIZE)
10288ca94183SScott Feldman 			goto nla_put_failure;
102957b61080SScott Feldman 		if (err) {
103057b61080SScott Feldman 			nla_nest_cancel(skb, vf_port);
103157b61080SScott Feldman 			continue;
103257b61080SScott Feldman 		}
103357b61080SScott Feldman 		nla_nest_end(skb, vf_port);
103457b61080SScott Feldman 	}
103557b61080SScott Feldman 
103657b61080SScott Feldman 	nla_nest_end(skb, vf_ports);
103757b61080SScott Feldman 
103857b61080SScott Feldman 	return 0;
10398ca94183SScott Feldman 
10408ca94183SScott Feldman nla_put_failure:
10418ca94183SScott Feldman 	nla_nest_cancel(skb, vf_ports);
10428ca94183SScott Feldman 	return -EMSGSIZE;
104357b61080SScott Feldman }
104457b61080SScott Feldman 
104557b61080SScott Feldman static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
104657b61080SScott Feldman {
104757b61080SScott Feldman 	struct nlattr *port_self;
104857b61080SScott Feldman 	int err;
104957b61080SScott Feldman 
105057b61080SScott Feldman 	port_self = nla_nest_start(skb, IFLA_PORT_SELF);
105157b61080SScott Feldman 	if (!port_self)
105257b61080SScott Feldman 		return -EMSGSIZE;
105357b61080SScott Feldman 
105457b61080SScott Feldman 	err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
105557b61080SScott Feldman 	if (err) {
105657b61080SScott Feldman 		nla_nest_cancel(skb, port_self);
10578ca94183SScott Feldman 		return (err == -EMSGSIZE) ? err : 0;
105857b61080SScott Feldman 	}
105957b61080SScott Feldman 
106057b61080SScott Feldman 	nla_nest_end(skb, port_self);
106157b61080SScott Feldman 
106257b61080SScott Feldman 	return 0;
106357b61080SScott Feldman }
106457b61080SScott Feldman 
1065c53864fdSDavid Gibson static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
1066c53864fdSDavid Gibson 			  u32 ext_filter_mask)
106757b61080SScott Feldman {
106857b61080SScott Feldman 	int err;
106957b61080SScott Feldman 
1070c53864fdSDavid Gibson 	if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
1071c53864fdSDavid Gibson 	    !(ext_filter_mask & RTEXT_FILTER_VF))
107257b61080SScott Feldman 		return 0;
107357b61080SScott Feldman 
107457b61080SScott Feldman 	err = rtnl_port_self_fill(skb, dev);
107557b61080SScott Feldman 	if (err)
107657b61080SScott Feldman 		return err;
107757b61080SScott Feldman 
107857b61080SScott Feldman 	if (dev_num_vf(dev->dev.parent)) {
107957b61080SScott Feldman 		err = rtnl_vf_ports_fill(skb, dev);
108057b61080SScott Feldman 		if (err)
108157b61080SScott Feldman 			return err;
108257b61080SScott Feldman 	}
108357b61080SScott Feldman 
108457b61080SScott Feldman 	return 0;
108557b61080SScott Feldman }
108657b61080SScott Feldman 
108766cae9edSJiri Pirko static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
108866cae9edSJiri Pirko {
108966cae9edSJiri Pirko 	int err;
109002637fceSJiri Pirko 	struct netdev_phys_item_id ppid;
109166cae9edSJiri Pirko 
109266cae9edSJiri Pirko 	err = dev_get_phys_port_id(dev, &ppid);
109366cae9edSJiri Pirko 	if (err) {
109466cae9edSJiri Pirko 		if (err == -EOPNOTSUPP)
109566cae9edSJiri Pirko 			return 0;
109666cae9edSJiri Pirko 		return err;
109766cae9edSJiri Pirko 	}
109866cae9edSJiri Pirko 
109966cae9edSJiri Pirko 	if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
110066cae9edSJiri Pirko 		return -EMSGSIZE;
110166cae9edSJiri Pirko 
110266cae9edSJiri Pirko 	return 0;
110366cae9edSJiri Pirko }
110466cae9edSJiri Pirko 
1105db24a904SDavid Ahern static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1106db24a904SDavid Ahern {
1107db24a904SDavid Ahern 	char name[IFNAMSIZ];
1108db24a904SDavid Ahern 	int err;
1109db24a904SDavid Ahern 
1110db24a904SDavid Ahern 	err = dev_get_phys_port_name(dev, name, sizeof(name));
1111db24a904SDavid Ahern 	if (err) {
1112db24a904SDavid Ahern 		if (err == -EOPNOTSUPP)
1113db24a904SDavid Ahern 			return 0;
1114db24a904SDavid Ahern 		return err;
1115db24a904SDavid Ahern 	}
1116db24a904SDavid Ahern 
111777ef033bSMichal Schmidt 	if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
1118db24a904SDavid Ahern 		return -EMSGSIZE;
1119db24a904SDavid Ahern 
1120db24a904SDavid Ahern 	return 0;
1121db24a904SDavid Ahern }
1122db24a904SDavid Ahern 
112382f28412SJiri Pirko static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
112482f28412SJiri Pirko {
112582f28412SJiri Pirko 	int err;
1126f8e20a9fSScott Feldman 	struct switchdev_attr attr = {
11276ff64f6fSIdo Schimmel 		.orig_dev = dev,
11281f868398SJiri Pirko 		.id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
1129f8e20a9fSScott Feldman 		.flags = SWITCHDEV_F_NO_RECURSE,
1130f8e20a9fSScott Feldman 	};
113182f28412SJiri Pirko 
1132f8e20a9fSScott Feldman 	err = switchdev_port_attr_get(dev, &attr);
113382f28412SJiri Pirko 	if (err) {
113482f28412SJiri Pirko 		if (err == -EOPNOTSUPP)
113582f28412SJiri Pirko 			return 0;
113682f28412SJiri Pirko 		return err;
113782f28412SJiri Pirko 	}
113882f28412SJiri Pirko 
113942275bd8SScott Feldman 	if (nla_put(skb, IFLA_PHYS_SWITCH_ID, attr.u.ppid.id_len,
114042275bd8SScott Feldman 		    attr.u.ppid.id))
114182f28412SJiri Pirko 		return -EMSGSIZE;
114282f28412SJiri Pirko 
114382f28412SJiri Pirko 	return 0;
114482f28412SJiri Pirko }
114582f28412SJiri Pirko 
1146b22b941bSHannes Frederic Sowa static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1147b22b941bSHannes Frederic Sowa 					      struct net_device *dev)
1148b22b941bSHannes Frederic Sowa {
1149550bce59SRoopa Prabhu 	struct rtnl_link_stats64 *sp;
1150b22b941bSHannes Frederic Sowa 	struct nlattr *attr;
1151b22b941bSHannes Frederic Sowa 
115258414d32SNicolas Dichtel 	attr = nla_reserve_64bit(skb, IFLA_STATS64,
115358414d32SNicolas Dichtel 				 sizeof(struct rtnl_link_stats64), IFLA_PAD);
1154b22b941bSHannes Frederic Sowa 	if (!attr)
1155b22b941bSHannes Frederic Sowa 		return -EMSGSIZE;
1156b22b941bSHannes Frederic Sowa 
1157550bce59SRoopa Prabhu 	sp = nla_data(attr);
1158550bce59SRoopa Prabhu 	dev_get_stats(dev, sp);
1159550bce59SRoopa Prabhu 
1160550bce59SRoopa Prabhu 	attr = nla_reserve(skb, IFLA_STATS,
1161550bce59SRoopa Prabhu 			   sizeof(struct rtnl_link_stats));
1162550bce59SRoopa Prabhu 	if (!attr)
1163550bce59SRoopa Prabhu 		return -EMSGSIZE;
1164550bce59SRoopa Prabhu 
1165550bce59SRoopa Prabhu 	copy_rtnl_link_stats(nla_data(attr), sp);
1166b22b941bSHannes Frederic Sowa 
1167b22b941bSHannes Frederic Sowa 	return 0;
1168b22b941bSHannes Frederic Sowa }
1169b22b941bSHannes Frederic Sowa 
1170b22b941bSHannes Frederic Sowa static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1171b22b941bSHannes Frederic Sowa 					       struct net_device *dev,
1172b22b941bSHannes Frederic Sowa 					       int vfs_num,
1173b22b941bSHannes Frederic Sowa 					       struct nlattr *vfinfo)
1174b22b941bSHannes Frederic Sowa {
1175b22b941bSHannes Frederic Sowa 	struct ifla_vf_rss_query_en vf_rss_query_en;
117679aab093SMoshe Shemesh 	struct nlattr *vf, *vfstats, *vfvlanlist;
1177b22b941bSHannes Frederic Sowa 	struct ifla_vf_link_state vf_linkstate;
117879aab093SMoshe Shemesh 	struct ifla_vf_vlan_info vf_vlan_info;
1179b22b941bSHannes Frederic Sowa 	struct ifla_vf_spoofchk vf_spoofchk;
1180b22b941bSHannes Frederic Sowa 	struct ifla_vf_tx_rate vf_tx_rate;
1181b22b941bSHannes Frederic Sowa 	struct ifla_vf_stats vf_stats;
1182b22b941bSHannes Frederic Sowa 	struct ifla_vf_trust vf_trust;
1183b22b941bSHannes Frederic Sowa 	struct ifla_vf_vlan vf_vlan;
1184b22b941bSHannes Frederic Sowa 	struct ifla_vf_rate vf_rate;
1185b22b941bSHannes Frederic Sowa 	struct ifla_vf_mac vf_mac;
1186b22b941bSHannes Frederic Sowa 	struct ifla_vf_info ivi;
1187b22b941bSHannes Frederic Sowa 
11880eed9cf5SMintz, Yuval 	memset(&ivi, 0, sizeof(ivi));
11890eed9cf5SMintz, Yuval 
1190b22b941bSHannes Frederic Sowa 	/* Not all SR-IOV capable drivers support the
1191b22b941bSHannes Frederic Sowa 	 * spoofcheck and "RSS query enable" query.  Preset to
1192b22b941bSHannes Frederic Sowa 	 * -1 so the user space tool can detect that the driver
1193b22b941bSHannes Frederic Sowa 	 * didn't report anything.
1194b22b941bSHannes Frederic Sowa 	 */
1195b22b941bSHannes Frederic Sowa 	ivi.spoofchk = -1;
1196b22b941bSHannes Frederic Sowa 	ivi.rss_query_en = -1;
1197b22b941bSHannes Frederic Sowa 	ivi.trusted = -1;
1198b22b941bSHannes Frederic Sowa 	/* The default value for VF link state is "auto"
1199b22b941bSHannes Frederic Sowa 	 * IFLA_VF_LINK_STATE_AUTO which equals zero
1200b22b941bSHannes Frederic Sowa 	 */
1201b22b941bSHannes Frederic Sowa 	ivi.linkstate = 0;
120279aab093SMoshe Shemesh 	/* VLAN Protocol by default is 802.1Q */
120379aab093SMoshe Shemesh 	ivi.vlan_proto = htons(ETH_P_8021Q);
1204b22b941bSHannes Frederic Sowa 	if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1205b22b941bSHannes Frederic Sowa 		return 0;
1206b22b941bSHannes Frederic Sowa 
1207775f4f05SDan Carpenter 	memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
1208775f4f05SDan Carpenter 
1209b22b941bSHannes Frederic Sowa 	vf_mac.vf =
1210b22b941bSHannes Frederic Sowa 		vf_vlan.vf =
121179aab093SMoshe Shemesh 		vf_vlan_info.vf =
1212b22b941bSHannes Frederic Sowa 		vf_rate.vf =
1213b22b941bSHannes Frederic Sowa 		vf_tx_rate.vf =
1214b22b941bSHannes Frederic Sowa 		vf_spoofchk.vf =
1215b22b941bSHannes Frederic Sowa 		vf_linkstate.vf =
1216b22b941bSHannes Frederic Sowa 		vf_rss_query_en.vf =
1217b22b941bSHannes Frederic Sowa 		vf_trust.vf = ivi.vf;
1218b22b941bSHannes Frederic Sowa 
1219b22b941bSHannes Frederic Sowa 	memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1220b22b941bSHannes Frederic Sowa 	vf_vlan.vlan = ivi.vlan;
1221b22b941bSHannes Frederic Sowa 	vf_vlan.qos = ivi.qos;
122279aab093SMoshe Shemesh 	vf_vlan_info.vlan = ivi.vlan;
122379aab093SMoshe Shemesh 	vf_vlan_info.qos = ivi.qos;
122479aab093SMoshe Shemesh 	vf_vlan_info.vlan_proto = ivi.vlan_proto;
1225b22b941bSHannes Frederic Sowa 	vf_tx_rate.rate = ivi.max_tx_rate;
1226b22b941bSHannes Frederic Sowa 	vf_rate.min_tx_rate = ivi.min_tx_rate;
1227b22b941bSHannes Frederic Sowa 	vf_rate.max_tx_rate = ivi.max_tx_rate;
1228b22b941bSHannes Frederic Sowa 	vf_spoofchk.setting = ivi.spoofchk;
1229b22b941bSHannes Frederic Sowa 	vf_linkstate.link_state = ivi.linkstate;
1230b22b941bSHannes Frederic Sowa 	vf_rss_query_en.setting = ivi.rss_query_en;
1231b22b941bSHannes Frederic Sowa 	vf_trust.setting = ivi.trusted;
1232b22b941bSHannes Frederic Sowa 	vf = nla_nest_start(skb, IFLA_VF_INFO);
123379aab093SMoshe Shemesh 	if (!vf)
123479aab093SMoshe Shemesh 		goto nla_put_vfinfo_failure;
1235b22b941bSHannes Frederic Sowa 	if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1236b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1237b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1238b22b941bSHannes Frederic Sowa 		    &vf_rate) ||
1239b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1240b22b941bSHannes Frederic Sowa 		    &vf_tx_rate) ||
1241b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1242b22b941bSHannes Frederic Sowa 		    &vf_spoofchk) ||
1243b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1244b22b941bSHannes Frederic Sowa 		    &vf_linkstate) ||
1245b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1246b22b941bSHannes Frederic Sowa 		    sizeof(vf_rss_query_en),
1247b22b941bSHannes Frederic Sowa 		    &vf_rss_query_en) ||
1248b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_TRUST,
1249b22b941bSHannes Frederic Sowa 		    sizeof(vf_trust), &vf_trust))
125079aab093SMoshe Shemesh 		goto nla_put_vf_failure;
125179aab093SMoshe Shemesh 	vfvlanlist = nla_nest_start(skb, IFLA_VF_VLAN_LIST);
125279aab093SMoshe Shemesh 	if (!vfvlanlist)
125379aab093SMoshe Shemesh 		goto nla_put_vf_failure;
125479aab093SMoshe Shemesh 	if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
125579aab093SMoshe Shemesh 		    &vf_vlan_info)) {
125679aab093SMoshe Shemesh 		nla_nest_cancel(skb, vfvlanlist);
125779aab093SMoshe Shemesh 		goto nla_put_vf_failure;
125879aab093SMoshe Shemesh 	}
125979aab093SMoshe Shemesh 	nla_nest_end(skb, vfvlanlist);
1260b22b941bSHannes Frederic Sowa 	memset(&vf_stats, 0, sizeof(vf_stats));
1261b22b941bSHannes Frederic Sowa 	if (dev->netdev_ops->ndo_get_vf_stats)
1262b22b941bSHannes Frederic Sowa 		dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1263b22b941bSHannes Frederic Sowa 						&vf_stats);
1264b22b941bSHannes Frederic Sowa 	vfstats = nla_nest_start(skb, IFLA_VF_STATS);
126579aab093SMoshe Shemesh 	if (!vfstats)
126679aab093SMoshe Shemesh 		goto nla_put_vf_failure;
1267343a6d8eSNicolas Dichtel 	if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1268343a6d8eSNicolas Dichtel 			      vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1269343a6d8eSNicolas Dichtel 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1270343a6d8eSNicolas Dichtel 			      vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1271343a6d8eSNicolas Dichtel 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1272343a6d8eSNicolas Dichtel 			      vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1273343a6d8eSNicolas Dichtel 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1274343a6d8eSNicolas Dichtel 			      vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1275343a6d8eSNicolas Dichtel 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1276343a6d8eSNicolas Dichtel 			      vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1277343a6d8eSNicolas Dichtel 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
127879aab093SMoshe Shemesh 			      vf_stats.multicast, IFLA_VF_STATS_PAD)) {
127979aab093SMoshe Shemesh 		nla_nest_cancel(skb, vfstats);
128079aab093SMoshe Shemesh 		goto nla_put_vf_failure;
128179aab093SMoshe Shemesh 	}
1282b22b941bSHannes Frederic Sowa 	nla_nest_end(skb, vfstats);
1283b22b941bSHannes Frederic Sowa 	nla_nest_end(skb, vf);
1284b22b941bSHannes Frederic Sowa 	return 0;
128579aab093SMoshe Shemesh 
128679aab093SMoshe Shemesh nla_put_vf_failure:
128779aab093SMoshe Shemesh 	nla_nest_cancel(skb, vf);
128879aab093SMoshe Shemesh nla_put_vfinfo_failure:
128979aab093SMoshe Shemesh 	nla_nest_cancel(skb, vfinfo);
129079aab093SMoshe Shemesh 	return -EMSGSIZE;
1291b22b941bSHannes Frederic Sowa }
1292b22b941bSHannes Frederic Sowa 
1293250fc3dfSFlorian Westphal static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb,
1294250fc3dfSFlorian Westphal 					   struct net_device *dev,
1295250fc3dfSFlorian Westphal 					   u32 ext_filter_mask)
1296250fc3dfSFlorian Westphal {
1297250fc3dfSFlorian Westphal 	struct nlattr *vfinfo;
1298250fc3dfSFlorian Westphal 	int i, num_vfs;
1299250fc3dfSFlorian Westphal 
1300250fc3dfSFlorian Westphal 	if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0))
1301250fc3dfSFlorian Westphal 		return 0;
1302250fc3dfSFlorian Westphal 
1303250fc3dfSFlorian Westphal 	num_vfs = dev_num_vf(dev->dev.parent);
1304250fc3dfSFlorian Westphal 	if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs))
1305250fc3dfSFlorian Westphal 		return -EMSGSIZE;
1306250fc3dfSFlorian Westphal 
1307250fc3dfSFlorian Westphal 	if (!dev->netdev_ops->ndo_get_vf_config)
1308250fc3dfSFlorian Westphal 		return 0;
1309250fc3dfSFlorian Westphal 
1310250fc3dfSFlorian Westphal 	vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1311250fc3dfSFlorian Westphal 	if (!vfinfo)
1312250fc3dfSFlorian Westphal 		return -EMSGSIZE;
1313250fc3dfSFlorian Westphal 
1314250fc3dfSFlorian Westphal 	for (i = 0; i < num_vfs; i++) {
1315250fc3dfSFlorian Westphal 		if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
1316250fc3dfSFlorian Westphal 			return -EMSGSIZE;
1317250fc3dfSFlorian Westphal 	}
1318250fc3dfSFlorian Westphal 
1319250fc3dfSFlorian Westphal 	nla_nest_end(skb, vfinfo);
1320250fc3dfSFlorian Westphal 	return 0;
1321250fc3dfSFlorian Westphal }
1322250fc3dfSFlorian Westphal 
1323b22b941bSHannes Frederic Sowa static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1324b22b941bSHannes Frederic Sowa {
13255f8e4474SKangjie Lu 	struct rtnl_link_ifmap map;
13265f8e4474SKangjie Lu 
13275f8e4474SKangjie Lu 	memset(&map, 0, sizeof(map));
13285f8e4474SKangjie Lu 	map.mem_start   = dev->mem_start;
13295f8e4474SKangjie Lu 	map.mem_end     = dev->mem_end;
13305f8e4474SKangjie Lu 	map.base_addr   = dev->base_addr;
13315f8e4474SKangjie Lu 	map.irq         = dev->irq;
13325f8e4474SKangjie Lu 	map.dma         = dev->dma;
13335f8e4474SKangjie Lu 	map.port        = dev->if_port;
13345f8e4474SKangjie Lu 
1335270cb4d0SNicolas Dichtel 	if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
1336b22b941bSHannes Frederic Sowa 		return -EMSGSIZE;
1337b22b941bSHannes Frederic Sowa 
1338b22b941bSHannes Frederic Sowa 	return 0;
1339b22b941bSHannes Frederic Sowa }
1340b22b941bSHannes Frederic Sowa 
134158038695SMartin KaFai Lau static u8 rtnl_xdp_attached_mode(struct net_device *dev, u32 *prog_id)
1342d67b9cd2SDaniel Borkmann {
1343d67b9cd2SDaniel Borkmann 	const struct net_device_ops *ops = dev->netdev_ops;
134458038695SMartin KaFai Lau 	const struct bpf_prog *generic_xdp_prog;
1345d67b9cd2SDaniel Borkmann 
1346d67b9cd2SDaniel Borkmann 	ASSERT_RTNL();
1347d67b9cd2SDaniel Borkmann 
134858038695SMartin KaFai Lau 	*prog_id = 0;
134958038695SMartin KaFai Lau 	generic_xdp_prog = rtnl_dereference(dev->xdp_prog);
135058038695SMartin KaFai Lau 	if (generic_xdp_prog) {
135158038695SMartin KaFai Lau 		*prog_id = generic_xdp_prog->aux->id;
1352d67b9cd2SDaniel Borkmann 		return XDP_ATTACHED_SKB;
135358038695SMartin KaFai Lau 	}
1354f4e63525SJakub Kicinski 	if (!ops->ndo_bpf)
1355d67b9cd2SDaniel Borkmann 		return XDP_ATTACHED_NONE;
1356ce158e58SJakub Kicinski 
1357f4e63525SJakub Kicinski 	return __dev_xdp_attached(dev, ops->ndo_bpf, prog_id);
1358d67b9cd2SDaniel Borkmann }
1359d67b9cd2SDaniel Borkmann 
1360d1fdd913SBrenden Blanco static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1361d1fdd913SBrenden Blanco {
1362d1fdd913SBrenden Blanco 	struct nlattr *xdp;
136358038695SMartin KaFai Lau 	u32 prog_id;
1364d1fdd913SBrenden Blanco 	int err;
1365d1fdd913SBrenden Blanco 
1366d1fdd913SBrenden Blanco 	xdp = nla_nest_start(skb, IFLA_XDP);
1367d1fdd913SBrenden Blanco 	if (!xdp)
1368d1fdd913SBrenden Blanco 		return -EMSGSIZE;
1369b5cdae32SDavid S. Miller 
1370d67b9cd2SDaniel Borkmann 	err = nla_put_u8(skb, IFLA_XDP_ATTACHED,
137158038695SMartin KaFai Lau 			 rtnl_xdp_attached_mode(dev, &prog_id));
1372d1fdd913SBrenden Blanco 	if (err)
1373d1fdd913SBrenden Blanco 		goto err_cancel;
1374d1fdd913SBrenden Blanco 
137558038695SMartin KaFai Lau 	if (prog_id) {
137658038695SMartin KaFai Lau 		err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id);
137758038695SMartin KaFai Lau 		if (err)
137858038695SMartin KaFai Lau 			goto err_cancel;
137958038695SMartin KaFai Lau 	}
138058038695SMartin KaFai Lau 
1381d1fdd913SBrenden Blanco 	nla_nest_end(skb, xdp);
1382d1fdd913SBrenden Blanco 	return 0;
1383d1fdd913SBrenden Blanco 
1384d1fdd913SBrenden Blanco err_cancel:
1385d1fdd913SBrenden Blanco 	nla_nest_cancel(skb, xdp);
1386d1fdd913SBrenden Blanco 	return err;
1387d1fdd913SBrenden Blanco }
1388d1fdd913SBrenden Blanco 
13893d3ea5afSVlad Yasevich static u32 rtnl_get_event(unsigned long event)
13903d3ea5afSVlad Yasevich {
13913d3ea5afSVlad Yasevich 	u32 rtnl_event_type = IFLA_EVENT_NONE;
13923d3ea5afSVlad Yasevich 
13933d3ea5afSVlad Yasevich 	switch (event) {
13943d3ea5afSVlad Yasevich 	case NETDEV_REBOOT:
13953d3ea5afSVlad Yasevich 		rtnl_event_type = IFLA_EVENT_REBOOT;
13963d3ea5afSVlad Yasevich 		break;
13973d3ea5afSVlad Yasevich 	case NETDEV_FEAT_CHANGE:
13983d3ea5afSVlad Yasevich 		rtnl_event_type = IFLA_EVENT_FEATURES;
13993d3ea5afSVlad Yasevich 		break;
14003d3ea5afSVlad Yasevich 	case NETDEV_BONDING_FAILOVER:
14013d3ea5afSVlad Yasevich 		rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER;
14023d3ea5afSVlad Yasevich 		break;
14033d3ea5afSVlad Yasevich 	case NETDEV_NOTIFY_PEERS:
14043d3ea5afSVlad Yasevich 		rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS;
14053d3ea5afSVlad Yasevich 		break;
14063d3ea5afSVlad Yasevich 	case NETDEV_RESEND_IGMP:
14073d3ea5afSVlad Yasevich 		rtnl_event_type = IFLA_EVENT_IGMP_RESEND;
14083d3ea5afSVlad Yasevich 		break;
14093d3ea5afSVlad Yasevich 	case NETDEV_CHANGEINFODATA:
14103d3ea5afSVlad Yasevich 		rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS;
14113d3ea5afSVlad Yasevich 		break;
14123d3ea5afSVlad Yasevich 	default:
14133d3ea5afSVlad Yasevich 		break;
14143d3ea5afSVlad Yasevich 	}
14153d3ea5afSVlad Yasevich 
14163d3ea5afSVlad Yasevich 	return rtnl_event_type;
14173d3ea5afSVlad Yasevich }
14183d3ea5afSVlad Yasevich 
141979110a04SFlorian Westphal static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev)
142079110a04SFlorian Westphal {
142179110a04SFlorian Westphal 	const struct net_device *upper_dev;
142279110a04SFlorian Westphal 	int ret = 0;
142379110a04SFlorian Westphal 
142479110a04SFlorian Westphal 	rcu_read_lock();
142579110a04SFlorian Westphal 
142679110a04SFlorian Westphal 	upper_dev = netdev_master_upper_dev_get_rcu(dev);
142779110a04SFlorian Westphal 	if (upper_dev)
142879110a04SFlorian Westphal 		ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex);
142979110a04SFlorian Westphal 
143079110a04SFlorian Westphal 	rcu_read_unlock();
143179110a04SFlorian Westphal 	return ret;
143279110a04SFlorian Westphal }
143379110a04SFlorian Westphal 
143479110a04SFlorian Westphal static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev)
143579110a04SFlorian Westphal {
143679110a04SFlorian Westphal 	int ifindex = dev_get_iflink(dev);
143779110a04SFlorian Westphal 
143879110a04SFlorian Westphal 	if (dev->ifindex == ifindex)
143979110a04SFlorian Westphal 		return 0;
144079110a04SFlorian Westphal 
144179110a04SFlorian Westphal 	return nla_put_u32(skb, IFLA_LINK, ifindex);
144279110a04SFlorian Westphal }
144379110a04SFlorian Westphal 
14446c557001SFlorian Westphal static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb,
14456c557001SFlorian Westphal 					      struct net_device *dev)
14466c557001SFlorian Westphal {
14476c557001SFlorian Westphal 	char buf[IFALIASZ];
14486c557001SFlorian Westphal 	int ret;
14496c557001SFlorian Westphal 
14506c557001SFlorian Westphal 	ret = dev_get_alias(dev, buf, sizeof(buf));
14516c557001SFlorian Westphal 	return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0;
14526c557001SFlorian Westphal }
14536c557001SFlorian Westphal 
1454b1e66b9aSFlorian Westphal static int rtnl_fill_link_netnsid(struct sk_buff *skb,
145579e1ad14SJiri Benc 				  const struct net_device *dev,
145679e1ad14SJiri Benc 				  struct net *src_net)
1457b1e66b9aSFlorian Westphal {
1458b1e66b9aSFlorian Westphal 	if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) {
1459b1e66b9aSFlorian Westphal 		struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1460b1e66b9aSFlorian Westphal 
1461b1e66b9aSFlorian Westphal 		if (!net_eq(dev_net(dev), link_net)) {
146279e1ad14SJiri Benc 			int id = peernet2id_alloc(src_net, link_net);
1463b1e66b9aSFlorian Westphal 
1464b1e66b9aSFlorian Westphal 			if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1465b1e66b9aSFlorian Westphal 				return -EMSGSIZE;
1466b1e66b9aSFlorian Westphal 		}
1467b1e66b9aSFlorian Westphal 	}
1468b1e66b9aSFlorian Westphal 
1469b1e66b9aSFlorian Westphal 	return 0;
1470b1e66b9aSFlorian Westphal }
1471b1e66b9aSFlorian Westphal 
1472070cbf5bSFlorian Westphal static int rtnl_fill_link_af(struct sk_buff *skb,
1473070cbf5bSFlorian Westphal 			     const struct net_device *dev,
1474070cbf5bSFlorian Westphal 			     u32 ext_filter_mask)
1475070cbf5bSFlorian Westphal {
1476070cbf5bSFlorian Westphal 	const struct rtnl_af_ops *af_ops;
1477070cbf5bSFlorian Westphal 	struct nlattr *af_spec;
1478070cbf5bSFlorian Westphal 
1479070cbf5bSFlorian Westphal 	af_spec = nla_nest_start(skb, IFLA_AF_SPEC);
1480070cbf5bSFlorian Westphal 	if (!af_spec)
1481070cbf5bSFlorian Westphal 		return -EMSGSIZE;
1482070cbf5bSFlorian Westphal 
14835fa85a09SFlorian Westphal 	list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
1484070cbf5bSFlorian Westphal 		struct nlattr *af;
1485070cbf5bSFlorian Westphal 		int err;
1486070cbf5bSFlorian Westphal 
1487070cbf5bSFlorian Westphal 		if (!af_ops->fill_link_af)
1488070cbf5bSFlorian Westphal 			continue;
1489070cbf5bSFlorian Westphal 
1490070cbf5bSFlorian Westphal 		af = nla_nest_start(skb, af_ops->family);
1491070cbf5bSFlorian Westphal 		if (!af)
1492070cbf5bSFlorian Westphal 			return -EMSGSIZE;
1493070cbf5bSFlorian Westphal 
1494070cbf5bSFlorian Westphal 		err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
1495070cbf5bSFlorian Westphal 		/*
1496070cbf5bSFlorian Westphal 		 * Caller may return ENODATA to indicate that there
1497070cbf5bSFlorian Westphal 		 * was no data to be dumped. This is not an error, it
1498070cbf5bSFlorian Westphal 		 * means we should trim the attribute header and
1499070cbf5bSFlorian Westphal 		 * continue.
1500070cbf5bSFlorian Westphal 		 */
1501070cbf5bSFlorian Westphal 		if (err == -ENODATA)
1502070cbf5bSFlorian Westphal 			nla_nest_cancel(skb, af);
1503070cbf5bSFlorian Westphal 		else if (err < 0)
1504070cbf5bSFlorian Westphal 			return -EMSGSIZE;
1505070cbf5bSFlorian Westphal 
1506070cbf5bSFlorian Westphal 		nla_nest_end(skb, af);
1507070cbf5bSFlorian Westphal 	}
1508070cbf5bSFlorian Westphal 
1509070cbf5bSFlorian Westphal 	nla_nest_end(skb, af_spec);
1510070cbf5bSFlorian Westphal 	return 0;
1511070cbf5bSFlorian Westphal }
1512070cbf5bSFlorian Westphal 
151379e1ad14SJiri Benc static int rtnl_fill_ifinfo(struct sk_buff *skb,
151479e1ad14SJiri Benc 			    struct net_device *dev, struct net *src_net,
1515575c3e2aSPatrick McHardy 			    int type, u32 pid, u32 seq, u32 change,
15163d3ea5afSVlad Yasevich 			    unsigned int flags, u32 ext_filter_mask,
151779e1ad14SJiri Benc 			    u32 event, int *new_nsid, int tgt_netnsid)
1518b60c5115SThomas Graf {
1519b60c5115SThomas Graf 	struct ifinfomsg *ifm;
15201da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
15211da177e4SLinus Torvalds 
15222907c35fSEric Dumazet 	ASSERT_RTNL();
1523b60c5115SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1524b60c5115SThomas Graf 	if (nlh == NULL)
152526932566SPatrick McHardy 		return -EMSGSIZE;
15261da177e4SLinus Torvalds 
1527b60c5115SThomas Graf 	ifm = nlmsg_data(nlh);
1528b60c5115SThomas Graf 	ifm->ifi_family = AF_UNSPEC;
1529b60c5115SThomas Graf 	ifm->__ifi_pad = 0;
1530b60c5115SThomas Graf 	ifm->ifi_type = dev->type;
1531b60c5115SThomas Graf 	ifm->ifi_index = dev->ifindex;
1532b60c5115SThomas Graf 	ifm->ifi_flags = dev_get_flags(dev);
1533b60c5115SThomas Graf 	ifm->ifi_change = change;
15341da177e4SLinus Torvalds 
153579e1ad14SJiri Benc 	if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_IF_NETNSID, tgt_netnsid))
153679e1ad14SJiri Benc 		goto nla_put_failure;
153779e1ad14SJiri Benc 
1538a6574349SDavid S. Miller 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1539a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1540a6574349SDavid S. Miller 	    nla_put_u8(skb, IFLA_OPERSTATE,
1541a6574349SDavid S. Miller 		       netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1542a6574349SDavid S. Miller 	    nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1543a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1544a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_GROUP, dev->group) ||
1545edbc0bb3SBen Greear 	    nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
154676ff5cc9SJiri Pirko 	    nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
1547c70ce028SEric Dumazet 	    nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1548c70ce028SEric Dumazet 	    nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
15491d69c2b3SMark A. Greer #ifdef CONFIG_RPS
155076ff5cc9SJiri Pirko 	    nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
15511d69c2b3SMark A. Greer #endif
155279110a04SFlorian Westphal 	    nla_put_iflink(skb, dev) ||
155379110a04SFlorian Westphal 	    put_master_ifindex(skb, dev) ||
15549a57247fSJiri Pirko 	    nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
1555a6574349SDavid S. Miller 	    (dev->qdisc &&
1556a6574349SDavid S. Miller 	     nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
15576c557001SFlorian Westphal 	    nla_put_ifalias(skb, dev) ||
15582d3b479dSdavid decotigny 	    nla_put_u32(skb, IFLA_CARRIER_CHANGES,
155988d6378bSAnuradha Karuppiah 			atomic_read(&dev->carrier_changes)) ||
156088d6378bSAnuradha Karuppiah 	    nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down))
1561a6574349SDavid S. Miller 		goto nla_put_failure;
15620b815a1aSStephen Hemminger 
15633d3ea5afSVlad Yasevich 	if (event != IFLA_EVENT_NONE) {
15643d3ea5afSVlad Yasevich 		if (nla_put_u32(skb, IFLA_EVENT, event))
15653d3ea5afSVlad Yasevich 			goto nla_put_failure;
15663d3ea5afSVlad Yasevich 	}
15673d3ea5afSVlad Yasevich 
1568b22b941bSHannes Frederic Sowa 	if (rtnl_fill_link_ifmap(skb, dev))
1569a6574349SDavid S. Miller 		goto nla_put_failure;
15701da177e4SLinus Torvalds 
15711da177e4SLinus Torvalds 	if (dev->addr_len) {
1572a6574349SDavid S. Miller 		if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1573a6574349SDavid S. Miller 		    nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1574a6574349SDavid S. Miller 			goto nla_put_failure;
15751da177e4SLinus Torvalds 	}
15761da177e4SLinus Torvalds 
157766cae9edSJiri Pirko 	if (rtnl_phys_port_id_fill(skb, dev))
157866cae9edSJiri Pirko 		goto nla_put_failure;
157966cae9edSJiri Pirko 
1580db24a904SDavid Ahern 	if (rtnl_phys_port_name_fill(skb, dev))
1581db24a904SDavid Ahern 		goto nla_put_failure;
1582db24a904SDavid Ahern 
158382f28412SJiri Pirko 	if (rtnl_phys_switch_id_fill(skb, dev))
158482f28412SJiri Pirko 		goto nla_put_failure;
158582f28412SJiri Pirko 
1586b22b941bSHannes Frederic Sowa 	if (rtnl_fill_stats(skb, dev))
1587b60c5115SThomas Graf 		goto nla_put_failure;
1588b60c5115SThomas Graf 
1589250fc3dfSFlorian Westphal 	if (rtnl_fill_vf(skb, dev, ext_filter_mask))
1590a6574349SDavid S. Miller 		goto nla_put_failure;
159157b61080SScott Feldman 
1592c53864fdSDavid Gibson 	if (rtnl_port_fill(skb, dev, ext_filter_mask))
159357b61080SScott Feldman 		goto nla_put_failure;
159457b61080SScott Feldman 
1595d1fdd913SBrenden Blanco 	if (rtnl_xdp_fill(skb, dev))
1596d1fdd913SBrenden Blanco 		goto nla_put_failure;
1597d1fdd913SBrenden Blanco 
1598ba7d49b1SJiri Pirko 	if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
159938f7b870SPatrick McHardy 		if (rtnl_link_fill(skb, dev) < 0)
160038f7b870SPatrick McHardy 			goto nla_put_failure;
160138f7b870SPatrick McHardy 	}
160238f7b870SPatrick McHardy 
160379e1ad14SJiri Benc 	if (rtnl_fill_link_netnsid(skb, dev, src_net))
1604d37512a2SNicolas Dichtel 		goto nla_put_failure;
1605d37512a2SNicolas Dichtel 
16066621dd29SNicolas Dichtel 	if (new_nsid &&
16076621dd29SNicolas Dichtel 	    nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0)
16086621dd29SNicolas Dichtel 		goto nla_put_failure;
16096621dd29SNicolas Dichtel 
16105fa85a09SFlorian Westphal 	rcu_read_lock();
1611070cbf5bSFlorian Westphal 	if (rtnl_fill_link_af(skb, dev, ext_filter_mask))
16125fa85a09SFlorian Westphal 		goto nla_put_failure_rcu;
16135fa85a09SFlorian Westphal 	rcu_read_unlock();
1614f8ff182cSThomas Graf 
1615053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
1616053c095aSJohannes Berg 	return 0;
1617b60c5115SThomas Graf 
16185fa85a09SFlorian Westphal nla_put_failure_rcu:
16195fa85a09SFlorian Westphal 	rcu_read_unlock();
1620b60c5115SThomas Graf nla_put_failure:
162126932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
162226932566SPatrick McHardy 	return -EMSGSIZE;
16231da177e4SLinus Torvalds }
16241da177e4SLinus Torvalds 
1625f7b12606SJiri Pirko static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1626f7b12606SJiri Pirko 	[IFLA_IFNAME]		= { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1627f7b12606SJiri Pirko 	[IFLA_ADDRESS]		= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1628f7b12606SJiri Pirko 	[IFLA_BROADCAST]	= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1629f7b12606SJiri Pirko 	[IFLA_MAP]		= { .len = sizeof(struct rtnl_link_ifmap) },
1630f7b12606SJiri Pirko 	[IFLA_MTU]		= { .type = NLA_U32 },
1631f7b12606SJiri Pirko 	[IFLA_LINK]		= { .type = NLA_U32 },
1632f7b12606SJiri Pirko 	[IFLA_MASTER]		= { .type = NLA_U32 },
1633f7b12606SJiri Pirko 	[IFLA_CARRIER]		= { .type = NLA_U8 },
1634f7b12606SJiri Pirko 	[IFLA_TXQLEN]		= { .type = NLA_U32 },
1635f7b12606SJiri Pirko 	[IFLA_WEIGHT]		= { .type = NLA_U32 },
1636f7b12606SJiri Pirko 	[IFLA_OPERSTATE]	= { .type = NLA_U8 },
1637f7b12606SJiri Pirko 	[IFLA_LINKMODE]		= { .type = NLA_U8 },
1638f7b12606SJiri Pirko 	[IFLA_LINKINFO]		= { .type = NLA_NESTED },
1639f7b12606SJiri Pirko 	[IFLA_NET_NS_PID]	= { .type = NLA_U32 },
1640f7b12606SJiri Pirko 	[IFLA_NET_NS_FD]	= { .type = NLA_U32 },
16412459b4c6SNicolas Dichtel 	/* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
16422459b4c6SNicolas Dichtel 	 * allow 0-length string (needed to remove an alias).
16432459b4c6SNicolas Dichtel 	 */
16442459b4c6SNicolas Dichtel 	[IFLA_IFALIAS]	        = { .type = NLA_BINARY, .len = IFALIASZ - 1 },
1645f7b12606SJiri Pirko 	[IFLA_VFINFO_LIST]	= {. type = NLA_NESTED },
1646f7b12606SJiri Pirko 	[IFLA_VF_PORTS]		= { .type = NLA_NESTED },
1647f7b12606SJiri Pirko 	[IFLA_PORT_SELF]	= { .type = NLA_NESTED },
1648f7b12606SJiri Pirko 	[IFLA_AF_SPEC]		= { .type = NLA_NESTED },
1649f7b12606SJiri Pirko 	[IFLA_EXT_MASK]		= { .type = NLA_U32 },
1650f7b12606SJiri Pirko 	[IFLA_PROMISCUITY]	= { .type = NLA_U32 },
1651f7b12606SJiri Pirko 	[IFLA_NUM_TX_QUEUES]	= { .type = NLA_U32 },
1652f7b12606SJiri Pirko 	[IFLA_NUM_RX_QUEUES]	= { .type = NLA_U32 },
165302637fceSJiri Pirko 	[IFLA_PHYS_PORT_ID]	= { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
16542d3b479dSdavid decotigny 	[IFLA_CARRIER_CHANGES]	= { .type = NLA_U32 },  /* ignored */
165582f28412SJiri Pirko 	[IFLA_PHYS_SWITCH_ID]	= { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1656317f4810SNicolas Dichtel 	[IFLA_LINK_NETNSID]	= { .type = NLA_S32 },
165788d6378bSAnuradha Karuppiah 	[IFLA_PROTO_DOWN]	= { .type = NLA_U8 },
1658d1fdd913SBrenden Blanco 	[IFLA_XDP]		= { .type = NLA_NESTED },
16593d3ea5afSVlad Yasevich 	[IFLA_EVENT]		= { .type = NLA_U32 },
1660db833d40SSerhey Popovych 	[IFLA_GROUP]		= { .type = NLA_U32 },
166179e1ad14SJiri Benc 	[IFLA_IF_NETNSID]	= { .type = NLA_S32 },
1662f7b12606SJiri Pirko };
1663f7b12606SJiri Pirko 
1664f7b12606SJiri Pirko static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1665f7b12606SJiri Pirko 	[IFLA_INFO_KIND]	= { .type = NLA_STRING },
1666f7b12606SJiri Pirko 	[IFLA_INFO_DATA]	= { .type = NLA_NESTED },
1667f7b12606SJiri Pirko 	[IFLA_INFO_SLAVE_KIND]	= { .type = NLA_STRING },
1668f7b12606SJiri Pirko 	[IFLA_INFO_SLAVE_DATA]	= { .type = NLA_NESTED },
1669f7b12606SJiri Pirko };
1670f7b12606SJiri Pirko 
1671f7b12606SJiri Pirko static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1672364d5716SDaniel Borkmann 	[IFLA_VF_MAC]		= { .len = sizeof(struct ifla_vf_mac) },
1673364d5716SDaniel Borkmann 	[IFLA_VF_VLAN]		= { .len = sizeof(struct ifla_vf_vlan) },
167479aab093SMoshe Shemesh 	[IFLA_VF_VLAN_LIST]     = { .type = NLA_NESTED },
1675364d5716SDaniel Borkmann 	[IFLA_VF_TX_RATE]	= { .len = sizeof(struct ifla_vf_tx_rate) },
1676364d5716SDaniel Borkmann 	[IFLA_VF_SPOOFCHK]	= { .len = sizeof(struct ifla_vf_spoofchk) },
1677364d5716SDaniel Borkmann 	[IFLA_VF_RATE]		= { .len = sizeof(struct ifla_vf_rate) },
1678364d5716SDaniel Borkmann 	[IFLA_VF_LINK_STATE]	= { .len = sizeof(struct ifla_vf_link_state) },
167901a3d796SVlad Zolotarov 	[IFLA_VF_RSS_QUERY_EN]	= { .len = sizeof(struct ifla_vf_rss_query_en) },
16803b766cd8SEran Ben Elisha 	[IFLA_VF_STATS]		= { .type = NLA_NESTED },
1681dd461d6aSHiroshi Shimamoto 	[IFLA_VF_TRUST]		= { .len = sizeof(struct ifla_vf_trust) },
1682cc8e27ccSEli Cohen 	[IFLA_VF_IB_NODE_GUID]	= { .len = sizeof(struct ifla_vf_guid) },
1683cc8e27ccSEli Cohen 	[IFLA_VF_IB_PORT_GUID]	= { .len = sizeof(struct ifla_vf_guid) },
16843b766cd8SEran Ben Elisha };
16853b766cd8SEran Ben Elisha 
1686f7b12606SJiri Pirko static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1687f7b12606SJiri Pirko 	[IFLA_PORT_VF]		= { .type = NLA_U32 },
1688f7b12606SJiri Pirko 	[IFLA_PORT_PROFILE]	= { .type = NLA_STRING,
1689f7b12606SJiri Pirko 				    .len = PORT_PROFILE_MAX },
1690f7b12606SJiri Pirko 	[IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1691f7b12606SJiri Pirko 				      .len = PORT_UUID_MAX },
1692f7b12606SJiri Pirko 	[IFLA_PORT_HOST_UUID]	= { .type = NLA_STRING,
1693f7b12606SJiri Pirko 				    .len = PORT_UUID_MAX },
1694f7b12606SJiri Pirko 	[IFLA_PORT_REQUEST]	= { .type = NLA_U8, },
1695f7b12606SJiri Pirko 	[IFLA_PORT_RESPONSE]	= { .type = NLA_U16, },
1696025331dfSDaniel Borkmann 
1697025331dfSDaniel Borkmann 	/* Unused, but we need to keep it here since user space could
1698025331dfSDaniel Borkmann 	 * fill it. It's also broken with regard to NLA_BINARY use in
1699025331dfSDaniel Borkmann 	 * combination with structs.
1700025331dfSDaniel Borkmann 	 */
1701025331dfSDaniel Borkmann 	[IFLA_PORT_VSI_TYPE]	= { .type = NLA_BINARY,
1702025331dfSDaniel Borkmann 				    .len = sizeof(struct ifla_port_vsi) },
1703f7b12606SJiri Pirko };
1704f7b12606SJiri Pirko 
1705d1fdd913SBrenden Blanco static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
1706d1fdd913SBrenden Blanco 	[IFLA_XDP_FD]		= { .type = NLA_S32 },
1707d1fdd913SBrenden Blanco 	[IFLA_XDP_ATTACHED]	= { .type = NLA_U8 },
170885de8576SDaniel Borkmann 	[IFLA_XDP_FLAGS]	= { .type = NLA_U32 },
170958038695SMartin KaFai Lau 	[IFLA_XDP_PROG_ID]	= { .type = NLA_U32 },
1710d1fdd913SBrenden Blanco };
1711d1fdd913SBrenden Blanco 
1712dc599f76SDavid Ahern static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1713dc599f76SDavid Ahern {
1714dc599f76SDavid Ahern 	const struct rtnl_link_ops *ops = NULL;
1715dc599f76SDavid Ahern 	struct nlattr *linfo[IFLA_INFO_MAX + 1];
1716dc599f76SDavid Ahern 
1717fceb6435SJohannes Berg 	if (nla_parse_nested(linfo, IFLA_INFO_MAX, nla,
1718fceb6435SJohannes Berg 			     ifla_info_policy, NULL) < 0)
1719dc599f76SDavid Ahern 		return NULL;
1720dc599f76SDavid Ahern 
1721dc599f76SDavid Ahern 	if (linfo[IFLA_INFO_KIND]) {
1722dc599f76SDavid Ahern 		char kind[MODULE_NAME_LEN];
1723dc599f76SDavid Ahern 
1724dc599f76SDavid Ahern 		nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1725dc599f76SDavid Ahern 		ops = rtnl_link_ops_get(kind);
1726dc599f76SDavid Ahern 	}
1727dc599f76SDavid Ahern 
1728dc599f76SDavid Ahern 	return ops;
1729dc599f76SDavid Ahern }
1730dc599f76SDavid Ahern 
1731dc599f76SDavid Ahern static bool link_master_filtered(struct net_device *dev, int master_idx)
1732dc599f76SDavid Ahern {
1733dc599f76SDavid Ahern 	struct net_device *master;
1734dc599f76SDavid Ahern 
1735dc599f76SDavid Ahern 	if (!master_idx)
1736dc599f76SDavid Ahern 		return false;
1737dc599f76SDavid Ahern 
1738dc599f76SDavid Ahern 	master = netdev_master_upper_dev_get(dev);
1739dc599f76SDavid Ahern 	if (!master || master->ifindex != master_idx)
1740dc599f76SDavid Ahern 		return true;
1741dc599f76SDavid Ahern 
1742dc599f76SDavid Ahern 	return false;
1743dc599f76SDavid Ahern }
1744dc599f76SDavid Ahern 
1745dc599f76SDavid Ahern static bool link_kind_filtered(const struct net_device *dev,
1746dc599f76SDavid Ahern 			       const struct rtnl_link_ops *kind_ops)
1747dc599f76SDavid Ahern {
1748dc599f76SDavid Ahern 	if (kind_ops && dev->rtnl_link_ops != kind_ops)
1749dc599f76SDavid Ahern 		return true;
1750dc599f76SDavid Ahern 
1751dc599f76SDavid Ahern 	return false;
1752dc599f76SDavid Ahern }
1753dc599f76SDavid Ahern 
1754dc599f76SDavid Ahern static bool link_dump_filtered(struct net_device *dev,
1755dc599f76SDavid Ahern 			       int master_idx,
1756dc599f76SDavid Ahern 			       const struct rtnl_link_ops *kind_ops)
1757dc599f76SDavid Ahern {
1758dc599f76SDavid Ahern 	if (link_master_filtered(dev, master_idx) ||
1759dc599f76SDavid Ahern 	    link_kind_filtered(dev, kind_ops))
1760dc599f76SDavid Ahern 		return true;
1761dc599f76SDavid Ahern 
1762dc599f76SDavid Ahern 	return false;
1763dc599f76SDavid Ahern }
1764dc599f76SDavid Ahern 
176579e1ad14SJiri Benc static struct net *get_target_net(struct sk_buff *skb, int netnsid)
176679e1ad14SJiri Benc {
176779e1ad14SJiri Benc 	struct net *net;
176879e1ad14SJiri Benc 
176979e1ad14SJiri Benc 	net = get_net_ns_by_id(sock_net(skb->sk), netnsid);
177079e1ad14SJiri Benc 	if (!net)
177179e1ad14SJiri Benc 		return ERR_PTR(-EINVAL);
177279e1ad14SJiri Benc 
177379e1ad14SJiri Benc 	/* For now, the caller is required to have CAP_NET_ADMIN in
177479e1ad14SJiri Benc 	 * the user namespace owning the target net ns.
177579e1ad14SJiri Benc 	 */
177679e1ad14SJiri Benc 	if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
177779e1ad14SJiri Benc 		put_net(net);
177879e1ad14SJiri Benc 		return ERR_PTR(-EACCES);
177979e1ad14SJiri Benc 	}
178079e1ad14SJiri Benc 	return net;
178179e1ad14SJiri Benc }
178279e1ad14SJiri Benc 
1783b60c5115SThomas Graf static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
17841da177e4SLinus Torvalds {
17853b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
178679e1ad14SJiri Benc 	struct net *tgt_net = net;
17877c28bd0bSEric Dumazet 	int h, s_h;
17887c28bd0bSEric Dumazet 	int idx = 0, s_idx;
17891da177e4SLinus Torvalds 	struct net_device *dev;
17907c28bd0bSEric Dumazet 	struct hlist_head *head;
1791115c9b81SGreg Rose 	struct nlattr *tb[IFLA_MAX+1];
1792115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
1793dc599f76SDavid Ahern 	const struct rtnl_link_ops *kind_ops = NULL;
1794dc599f76SDavid Ahern 	unsigned int flags = NLM_F_MULTI;
1795dc599f76SDavid Ahern 	int master_idx = 0;
179679e1ad14SJiri Benc 	int netnsid = -1;
1797973462bbSDavid Gibson 	int err;
1798e5eca6d4SMichal Schmidt 	int hdrlen;
17991da177e4SLinus Torvalds 
18007c28bd0bSEric Dumazet 	s_h = cb->args[0];
18017c28bd0bSEric Dumazet 	s_idx = cb->args[1];
18027c28bd0bSEric Dumazet 
1803e5eca6d4SMichal Schmidt 	/* A hack to preserve kernel<->userspace interface.
1804e5eca6d4SMichal Schmidt 	 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1805e5eca6d4SMichal Schmidt 	 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1806e5eca6d4SMichal Schmidt 	 * what iproute2 < v3.9.0 used.
1807e5eca6d4SMichal Schmidt 	 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1808e5eca6d4SMichal Schmidt 	 * attribute, its netlink message is shorter than struct ifinfomsg.
1809e5eca6d4SMichal Schmidt 	 */
1810e5eca6d4SMichal Schmidt 	hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ?
1811e5eca6d4SMichal Schmidt 		 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1812e5eca6d4SMichal Schmidt 
1813fceb6435SJohannes Berg 	if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX,
1814fceb6435SJohannes Berg 			ifla_policy, NULL) >= 0) {
181579e1ad14SJiri Benc 		if (tb[IFLA_IF_NETNSID]) {
181679e1ad14SJiri Benc 			netnsid = nla_get_s32(tb[IFLA_IF_NETNSID]);
181779e1ad14SJiri Benc 			tgt_net = get_target_net(skb, netnsid);
181879e1ad14SJiri Benc 			if (IS_ERR(tgt_net)) {
181979e1ad14SJiri Benc 				tgt_net = net;
182079e1ad14SJiri Benc 				netnsid = -1;
182179e1ad14SJiri Benc 			}
182279e1ad14SJiri Benc 		}
182379e1ad14SJiri Benc 
1824115c9b81SGreg Rose 		if (tb[IFLA_EXT_MASK])
1825115c9b81SGreg Rose 			ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1826dc599f76SDavid Ahern 
1827dc599f76SDavid Ahern 		if (tb[IFLA_MASTER])
1828dc599f76SDavid Ahern 			master_idx = nla_get_u32(tb[IFLA_MASTER]);
1829dc599f76SDavid Ahern 
1830dc599f76SDavid Ahern 		if (tb[IFLA_LINKINFO])
1831dc599f76SDavid Ahern 			kind_ops = linkinfo_to_kind_ops(tb[IFLA_LINKINFO]);
1832dc599f76SDavid Ahern 
1833dc599f76SDavid Ahern 		if (master_idx || kind_ops)
1834dc599f76SDavid Ahern 			flags |= NLM_F_DUMP_FILTERED;
1835a4b64fbeSEric Dumazet 	}
1836115c9b81SGreg Rose 
18377c28bd0bSEric Dumazet 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
18387562f876SPavel Emelianov 		idx = 0;
183979e1ad14SJiri Benc 		head = &tgt_net->dev_index_head[h];
1840cac5e65eSEric Dumazet 		hlist_for_each_entry(dev, head, index_hlist) {
1841dc599f76SDavid Ahern 			if (link_dump_filtered(dev, master_idx, kind_ops))
18423f0ae05dSZhang Shengju 				goto cont;
18431da177e4SLinus Torvalds 			if (idx < s_idx)
18447562f876SPavel Emelianov 				goto cont;
184579e1ad14SJiri Benc 			err = rtnl_fill_ifinfo(skb, dev, net,
184679e1ad14SJiri Benc 					       RTM_NEWLINK,
184715e47304SEric W. Biederman 					       NETLINK_CB(cb->skb).portid,
18487c28bd0bSEric Dumazet 					       cb->nlh->nlmsg_seq, 0,
1849dc599f76SDavid Ahern 					       flags,
185079e1ad14SJiri Benc 					       ext_filter_mask, 0, NULL,
185179e1ad14SJiri Benc 					       netnsid);
1852973462bbSDavid Gibson 
1853f6c5775fSDavid Ahern 			if (err < 0) {
1854f6c5775fSDavid Ahern 				if (likely(skb->len))
18557c28bd0bSEric Dumazet 					goto out;
18564e985adaSThomas Graf 
1857f6c5775fSDavid Ahern 				goto out_err;
1858f6c5775fSDavid Ahern 			}
18597562f876SPavel Emelianov cont:
18607562f876SPavel Emelianov 			idx++;
18611da177e4SLinus Torvalds 		}
18627c28bd0bSEric Dumazet 	}
18637c28bd0bSEric Dumazet out:
1864f6c5775fSDavid Ahern 	err = skb->len;
1865f6c5775fSDavid Ahern out_err:
18667c28bd0bSEric Dumazet 	cb->args[1] = idx;
18677c28bd0bSEric Dumazet 	cb->args[0] = h;
1868d0225784SJakub Sitnicki 	cb->seq = net->dev_base_seq;
1869d0225784SJakub Sitnicki 	nl_dump_check_consistent(cb, nlmsg_hdr(skb));
187079e1ad14SJiri Benc 	if (netnsid >= 0)
187179e1ad14SJiri Benc 		put_net(tgt_net);
18721da177e4SLinus Torvalds 
1873f6c5775fSDavid Ahern 	return err;
18741da177e4SLinus Torvalds }
18751da177e4SLinus Torvalds 
1876fceb6435SJohannes Berg int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len,
1877fceb6435SJohannes Berg 			struct netlink_ext_ack *exterr)
1878f7b12606SJiri Pirko {
1879fceb6435SJohannes Berg 	return nla_parse(tb, IFLA_MAX, head, len, ifla_policy, exterr);
1880f7b12606SJiri Pirko }
1881f7b12606SJiri Pirko EXPORT_SYMBOL(rtnl_nla_parse_ifla);
188257b61080SScott Feldman 
188381adee47SEric W. Biederman struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
188481adee47SEric W. Biederman {
188581adee47SEric W. Biederman 	struct net *net;
188681adee47SEric W. Biederman 	/* Examine the link attributes and figure out which
188781adee47SEric W. Biederman 	 * network namespace we are talking about.
188881adee47SEric W. Biederman 	 */
188981adee47SEric W. Biederman 	if (tb[IFLA_NET_NS_PID])
189081adee47SEric W. Biederman 		net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
1891f0630529SEric W. Biederman 	else if (tb[IFLA_NET_NS_FD])
1892f0630529SEric W. Biederman 		net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
189381adee47SEric W. Biederman 	else
189481adee47SEric W. Biederman 		net = get_net(src_net);
189581adee47SEric W. Biederman 	return net;
189681adee47SEric W. Biederman }
189781adee47SEric W. Biederman EXPORT_SYMBOL(rtnl_link_get_net);
189881adee47SEric W. Biederman 
18991840bb13SThomas Graf static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
19001840bb13SThomas Graf {
19011840bb13SThomas Graf 	if (dev) {
19021840bb13SThomas Graf 		if (tb[IFLA_ADDRESS] &&
19031840bb13SThomas Graf 		    nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
19041840bb13SThomas Graf 			return -EINVAL;
19051840bb13SThomas Graf 
19061840bb13SThomas Graf 		if (tb[IFLA_BROADCAST] &&
19071840bb13SThomas Graf 		    nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
19081840bb13SThomas Graf 			return -EINVAL;
19091840bb13SThomas Graf 	}
19101840bb13SThomas Graf 
1911cf7afbfeSThomas Graf 	if (tb[IFLA_AF_SPEC]) {
1912cf7afbfeSThomas Graf 		struct nlattr *af;
1913cf7afbfeSThomas Graf 		int rem, err;
1914cf7afbfeSThomas Graf 
1915cf7afbfeSThomas Graf 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1916cf7afbfeSThomas Graf 			const struct rtnl_af_ops *af_ops;
1917cf7afbfeSThomas Graf 
19185fa85a09SFlorian Westphal 			rcu_read_lock();
19195fa85a09SFlorian Westphal 			af_ops = rtnl_af_lookup(nla_type(af));
19205fa85a09SFlorian Westphal 			if (!af_ops) {
19215fa85a09SFlorian Westphal 				rcu_read_unlock();
1922cf7afbfeSThomas Graf 				return -EAFNOSUPPORT;
19235fa85a09SFlorian Westphal 			}
1924cf7afbfeSThomas Graf 
19255fa85a09SFlorian Westphal 			if (!af_ops->set_link_af) {
19265fa85a09SFlorian Westphal 				rcu_read_unlock();
1927cf7afbfeSThomas Graf 				return -EOPNOTSUPP;
19285fa85a09SFlorian Westphal 			}
1929cf7afbfeSThomas Graf 
1930cf7afbfeSThomas Graf 			if (af_ops->validate_link_af) {
19316d3a9a68SKurt Van Dijck 				err = af_ops->validate_link_af(dev, af);
19325fa85a09SFlorian Westphal 				if (err < 0) {
19335fa85a09SFlorian Westphal 					rcu_read_unlock();
1934cf7afbfeSThomas Graf 					return err;
1935cf7afbfeSThomas Graf 				}
1936cf7afbfeSThomas Graf 			}
19375fa85a09SFlorian Westphal 
19385fa85a09SFlorian Westphal 			rcu_read_unlock();
19395fa85a09SFlorian Westphal 		}
1940cf7afbfeSThomas Graf 	}
1941cf7afbfeSThomas Graf 
19421840bb13SThomas Graf 	return 0;
19431840bb13SThomas Graf }
19441840bb13SThomas Graf 
1945cc8e27ccSEli Cohen static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
1946cc8e27ccSEli Cohen 				  int guid_type)
1947cc8e27ccSEli Cohen {
1948cc8e27ccSEli Cohen 	const struct net_device_ops *ops = dev->netdev_ops;
1949cc8e27ccSEli Cohen 
1950cc8e27ccSEli Cohen 	return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
1951cc8e27ccSEli Cohen }
1952cc8e27ccSEli Cohen 
1953cc8e27ccSEli Cohen static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
1954cc8e27ccSEli Cohen {
1955cc8e27ccSEli Cohen 	if (dev->type != ARPHRD_INFINIBAND)
1956cc8e27ccSEli Cohen 		return -EOPNOTSUPP;
1957cc8e27ccSEli Cohen 
1958cc8e27ccSEli Cohen 	return handle_infiniband_guid(dev, ivt, guid_type);
1959cc8e27ccSEli Cohen }
1960cc8e27ccSEli Cohen 
19614f7d2cdfSDaniel Borkmann static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
1962c02db8c6SChris Wright {
1963c02db8c6SChris Wright 	const struct net_device_ops *ops = dev->netdev_ops;
19644f7d2cdfSDaniel Borkmann 	int err = -EINVAL;
1965c02db8c6SChris Wright 
19664f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_MAC]) {
19674f7d2cdfSDaniel Borkmann 		struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
19684f7d2cdfSDaniel Borkmann 
1969c02db8c6SChris Wright 		err = -EOPNOTSUPP;
1970c02db8c6SChris Wright 		if (ops->ndo_set_vf_mac)
1971c02db8c6SChris Wright 			err = ops->ndo_set_vf_mac(dev, ivm->vf,
1972c02db8c6SChris Wright 						  ivm->mac);
19734f7d2cdfSDaniel Borkmann 		if (err < 0)
19744f7d2cdfSDaniel Borkmann 			return err;
1975c02db8c6SChris Wright 	}
19764f7d2cdfSDaniel Borkmann 
19774f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_VLAN]) {
19784f7d2cdfSDaniel Borkmann 		struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
19794f7d2cdfSDaniel Borkmann 
1980c02db8c6SChris Wright 		err = -EOPNOTSUPP;
1981c02db8c6SChris Wright 		if (ops->ndo_set_vf_vlan)
19824f7d2cdfSDaniel Borkmann 			err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
198379aab093SMoshe Shemesh 						   ivv->qos,
198479aab093SMoshe Shemesh 						   htons(ETH_P_8021Q));
198579aab093SMoshe Shemesh 		if (err < 0)
198679aab093SMoshe Shemesh 			return err;
198779aab093SMoshe Shemesh 	}
198879aab093SMoshe Shemesh 
198979aab093SMoshe Shemesh 	if (tb[IFLA_VF_VLAN_LIST]) {
199079aab093SMoshe Shemesh 		struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
199179aab093SMoshe Shemesh 		struct nlattr *attr;
199279aab093SMoshe Shemesh 		int rem, len = 0;
199379aab093SMoshe Shemesh 
199479aab093SMoshe Shemesh 		err = -EOPNOTSUPP;
199579aab093SMoshe Shemesh 		if (!ops->ndo_set_vf_vlan)
199679aab093SMoshe Shemesh 			return err;
199779aab093SMoshe Shemesh 
199879aab093SMoshe Shemesh 		nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
199979aab093SMoshe Shemesh 			if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
200079aab093SMoshe Shemesh 			    nla_len(attr) < NLA_HDRLEN) {
200179aab093SMoshe Shemesh 				return -EINVAL;
200279aab093SMoshe Shemesh 			}
200379aab093SMoshe Shemesh 			if (len >= MAX_VLAN_LIST_LEN)
200479aab093SMoshe Shemesh 				return -EOPNOTSUPP;
200579aab093SMoshe Shemesh 			ivvl[len] = nla_data(attr);
200679aab093SMoshe Shemesh 
200779aab093SMoshe Shemesh 			len++;
200879aab093SMoshe Shemesh 		}
2009fa34cd94SArnd Bergmann 		if (len == 0)
2010fa34cd94SArnd Bergmann 			return -EINVAL;
2011fa34cd94SArnd Bergmann 
201279aab093SMoshe Shemesh 		err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
201379aab093SMoshe Shemesh 					   ivvl[0]->qos, ivvl[0]->vlan_proto);
20144f7d2cdfSDaniel Borkmann 		if (err < 0)
20154f7d2cdfSDaniel Borkmann 			return err;
2016c02db8c6SChris Wright 	}
20174f7d2cdfSDaniel Borkmann 
20184f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_TX_RATE]) {
20194f7d2cdfSDaniel Borkmann 		struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
2020ed616689SSucheta Chakraborty 		struct ifla_vf_info ivf;
20214f7d2cdfSDaniel Borkmann 
2022c02db8c6SChris Wright 		err = -EOPNOTSUPP;
2023ed616689SSucheta Chakraborty 		if (ops->ndo_get_vf_config)
20244f7d2cdfSDaniel Borkmann 			err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
20254f7d2cdfSDaniel Borkmann 		if (err < 0)
20264f7d2cdfSDaniel Borkmann 			return err;
20274f7d2cdfSDaniel Borkmann 
2028ed616689SSucheta Chakraborty 		err = -EOPNOTSUPP;
2029ed616689SSucheta Chakraborty 		if (ops->ndo_set_vf_rate)
2030ed616689SSucheta Chakraborty 			err = ops->ndo_set_vf_rate(dev, ivt->vf,
2031ed616689SSucheta Chakraborty 						   ivf.min_tx_rate,
2032c02db8c6SChris Wright 						   ivt->rate);
20334f7d2cdfSDaniel Borkmann 		if (err < 0)
20344f7d2cdfSDaniel Borkmann 			return err;
2035c02db8c6SChris Wright 	}
20364f7d2cdfSDaniel Borkmann 
20374f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_RATE]) {
20384f7d2cdfSDaniel Borkmann 		struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
20394f7d2cdfSDaniel Borkmann 
2040ed616689SSucheta Chakraborty 		err = -EOPNOTSUPP;
2041ed616689SSucheta Chakraborty 		if (ops->ndo_set_vf_rate)
2042ed616689SSucheta Chakraborty 			err = ops->ndo_set_vf_rate(dev, ivt->vf,
2043ed616689SSucheta Chakraborty 						   ivt->min_tx_rate,
2044ed616689SSucheta Chakraborty 						   ivt->max_tx_rate);
20454f7d2cdfSDaniel Borkmann 		if (err < 0)
20464f7d2cdfSDaniel Borkmann 			return err;
2047ed616689SSucheta Chakraborty 	}
20484f7d2cdfSDaniel Borkmann 
20494f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_SPOOFCHK]) {
20504f7d2cdfSDaniel Borkmann 		struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
20514f7d2cdfSDaniel Borkmann 
20525f8444a3SGreg Rose 		err = -EOPNOTSUPP;
20535f8444a3SGreg Rose 		if (ops->ndo_set_vf_spoofchk)
20545f8444a3SGreg Rose 			err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
20555f8444a3SGreg Rose 						       ivs->setting);
20564f7d2cdfSDaniel Borkmann 		if (err < 0)
20574f7d2cdfSDaniel Borkmann 			return err;
20585f8444a3SGreg Rose 	}
20594f7d2cdfSDaniel Borkmann 
20604f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_LINK_STATE]) {
20614f7d2cdfSDaniel Borkmann 		struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
20624f7d2cdfSDaniel Borkmann 
20631d8faf48SRony Efraim 		err = -EOPNOTSUPP;
20641d8faf48SRony Efraim 		if (ops->ndo_set_vf_link_state)
20651d8faf48SRony Efraim 			err = ops->ndo_set_vf_link_state(dev, ivl->vf,
20661d8faf48SRony Efraim 							 ivl->link_state);
20674f7d2cdfSDaniel Borkmann 		if (err < 0)
20684f7d2cdfSDaniel Borkmann 			return err;
20691d8faf48SRony Efraim 	}
20704f7d2cdfSDaniel Borkmann 
20714f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_RSS_QUERY_EN]) {
207201a3d796SVlad Zolotarov 		struct ifla_vf_rss_query_en *ivrssq_en;
207301a3d796SVlad Zolotarov 
207401a3d796SVlad Zolotarov 		err = -EOPNOTSUPP;
20754f7d2cdfSDaniel Borkmann 		ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
207601a3d796SVlad Zolotarov 		if (ops->ndo_set_vf_rss_query_en)
20774f7d2cdfSDaniel Borkmann 			err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
207801a3d796SVlad Zolotarov 							   ivrssq_en->setting);
20794f7d2cdfSDaniel Borkmann 		if (err < 0)
20804f7d2cdfSDaniel Borkmann 			return err;
208101a3d796SVlad Zolotarov 	}
20824f7d2cdfSDaniel Borkmann 
2083dd461d6aSHiroshi Shimamoto 	if (tb[IFLA_VF_TRUST]) {
2084dd461d6aSHiroshi Shimamoto 		struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
2085dd461d6aSHiroshi Shimamoto 
2086dd461d6aSHiroshi Shimamoto 		err = -EOPNOTSUPP;
2087dd461d6aSHiroshi Shimamoto 		if (ops->ndo_set_vf_trust)
2088dd461d6aSHiroshi Shimamoto 			err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
2089dd461d6aSHiroshi Shimamoto 		if (err < 0)
2090dd461d6aSHiroshi Shimamoto 			return err;
2091dd461d6aSHiroshi Shimamoto 	}
2092dd461d6aSHiroshi Shimamoto 
2093cc8e27ccSEli Cohen 	if (tb[IFLA_VF_IB_NODE_GUID]) {
2094cc8e27ccSEli Cohen 		struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
2095cc8e27ccSEli Cohen 
2096cc8e27ccSEli Cohen 		if (!ops->ndo_set_vf_guid)
2097cc8e27ccSEli Cohen 			return -EOPNOTSUPP;
2098cc8e27ccSEli Cohen 
2099cc8e27ccSEli Cohen 		return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
2100cc8e27ccSEli Cohen 	}
2101cc8e27ccSEli Cohen 
2102cc8e27ccSEli Cohen 	if (tb[IFLA_VF_IB_PORT_GUID]) {
2103cc8e27ccSEli Cohen 		struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
2104cc8e27ccSEli Cohen 
2105cc8e27ccSEli Cohen 		if (!ops->ndo_set_vf_guid)
2106cc8e27ccSEli Cohen 			return -EOPNOTSUPP;
2107cc8e27ccSEli Cohen 
2108cc8e27ccSEli Cohen 		return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
2109cc8e27ccSEli Cohen 	}
2110cc8e27ccSEli Cohen 
2111c02db8c6SChris Wright 	return err;
2112c02db8c6SChris Wright }
2113c02db8c6SChris Wright 
211433eaf2a6SDavid Ahern static int do_set_master(struct net_device *dev, int ifindex,
211533eaf2a6SDavid Ahern 			 struct netlink_ext_ack *extack)
2116fbaec0eaSJiri Pirko {
2117898e5061SJiri Pirko 	struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
2118fbaec0eaSJiri Pirko 	const struct net_device_ops *ops;
2119fbaec0eaSJiri Pirko 	int err;
2120fbaec0eaSJiri Pirko 
2121898e5061SJiri Pirko 	if (upper_dev) {
2122898e5061SJiri Pirko 		if (upper_dev->ifindex == ifindex)
2123fbaec0eaSJiri Pirko 			return 0;
2124898e5061SJiri Pirko 		ops = upper_dev->netdev_ops;
2125fbaec0eaSJiri Pirko 		if (ops->ndo_del_slave) {
2126898e5061SJiri Pirko 			err = ops->ndo_del_slave(upper_dev, dev);
2127fbaec0eaSJiri Pirko 			if (err)
2128fbaec0eaSJiri Pirko 				return err;
2129fbaec0eaSJiri Pirko 		} else {
2130fbaec0eaSJiri Pirko 			return -EOPNOTSUPP;
2131fbaec0eaSJiri Pirko 		}
2132fbaec0eaSJiri Pirko 	}
2133fbaec0eaSJiri Pirko 
2134fbaec0eaSJiri Pirko 	if (ifindex) {
2135898e5061SJiri Pirko 		upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
2136898e5061SJiri Pirko 		if (!upper_dev)
2137fbaec0eaSJiri Pirko 			return -EINVAL;
2138898e5061SJiri Pirko 		ops = upper_dev->netdev_ops;
2139fbaec0eaSJiri Pirko 		if (ops->ndo_add_slave) {
214033eaf2a6SDavid Ahern 			err = ops->ndo_add_slave(upper_dev, dev, extack);
2141fbaec0eaSJiri Pirko 			if (err)
2142fbaec0eaSJiri Pirko 				return err;
2143fbaec0eaSJiri Pirko 		} else {
2144fbaec0eaSJiri Pirko 			return -EOPNOTSUPP;
2145fbaec0eaSJiri Pirko 		}
2146fbaec0eaSJiri Pirko 	}
2147fbaec0eaSJiri Pirko 	return 0;
2148fbaec0eaSJiri Pirko }
2149fbaec0eaSJiri Pirko 
215090c325e3SNicolas Dichtel #define DO_SETLINK_MODIFIED	0x01
2151ba998906SNicolas Dichtel /* notify flag means notify + modified. */
2152ba998906SNicolas Dichtel #define DO_SETLINK_NOTIFY	0x03
215390f62cf3SEric W. Biederman static int do_setlink(const struct sk_buff *skb,
215490f62cf3SEric W. Biederman 		      struct net_device *dev, struct ifinfomsg *ifm,
2155ddf9f970SJakub Kicinski 		      struct netlink_ext_ack *extack,
215690c325e3SNicolas Dichtel 		      struct nlattr **tb, char *ifname, int status)
21570157f60cSPatrick McHardy {
2158d314774cSStephen Hemminger 	const struct net_device_ops *ops = dev->netdev_ops;
21590157f60cSPatrick McHardy 	int err;
21600157f60cSPatrick McHardy 
2161f0630529SEric W. Biederman 	if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
216281adee47SEric W. Biederman 		struct net *net = rtnl_link_get_net(dev_net(dev), tb);
2163d8a5ec67SEric W. Biederman 		if (IS_ERR(net)) {
2164d8a5ec67SEric W. Biederman 			err = PTR_ERR(net);
2165d8a5ec67SEric W. Biederman 			goto errout;
2166d8a5ec67SEric W. Biederman 		}
216790f62cf3SEric W. Biederman 		if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
2168e0ebde0eSNicolas Dichtel 			put_net(net);
2169b51642f6SEric W. Biederman 			err = -EPERM;
2170b51642f6SEric W. Biederman 			goto errout;
2171b51642f6SEric W. Biederman 		}
2172d8a5ec67SEric W. Biederman 		err = dev_change_net_namespace(dev, net, ifname);
2173d8a5ec67SEric W. Biederman 		put_net(net);
2174d8a5ec67SEric W. Biederman 		if (err)
2175d8a5ec67SEric W. Biederman 			goto errout;
217690c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
2177d8a5ec67SEric W. Biederman 	}
2178d8a5ec67SEric W. Biederman 
21790157f60cSPatrick McHardy 	if (tb[IFLA_MAP]) {
21800157f60cSPatrick McHardy 		struct rtnl_link_ifmap *u_map;
21810157f60cSPatrick McHardy 		struct ifmap k_map;
21820157f60cSPatrick McHardy 
2183d314774cSStephen Hemminger 		if (!ops->ndo_set_config) {
21840157f60cSPatrick McHardy 			err = -EOPNOTSUPP;
21850157f60cSPatrick McHardy 			goto errout;
21860157f60cSPatrick McHardy 		}
21870157f60cSPatrick McHardy 
21880157f60cSPatrick McHardy 		if (!netif_device_present(dev)) {
21890157f60cSPatrick McHardy 			err = -ENODEV;
21900157f60cSPatrick McHardy 			goto errout;
21910157f60cSPatrick McHardy 		}
21920157f60cSPatrick McHardy 
21930157f60cSPatrick McHardy 		u_map = nla_data(tb[IFLA_MAP]);
21940157f60cSPatrick McHardy 		k_map.mem_start = (unsigned long) u_map->mem_start;
21950157f60cSPatrick McHardy 		k_map.mem_end = (unsigned long) u_map->mem_end;
21960157f60cSPatrick McHardy 		k_map.base_addr = (unsigned short) u_map->base_addr;
21970157f60cSPatrick McHardy 		k_map.irq = (unsigned char) u_map->irq;
21980157f60cSPatrick McHardy 		k_map.dma = (unsigned char) u_map->dma;
21990157f60cSPatrick McHardy 		k_map.port = (unsigned char) u_map->port;
22000157f60cSPatrick McHardy 
2201d314774cSStephen Hemminger 		err = ops->ndo_set_config(dev, &k_map);
22020157f60cSPatrick McHardy 		if (err < 0)
22030157f60cSPatrick McHardy 			goto errout;
22040157f60cSPatrick McHardy 
2205ba998906SNicolas Dichtel 		status |= DO_SETLINK_NOTIFY;
22060157f60cSPatrick McHardy 	}
22070157f60cSPatrick McHardy 
22080157f60cSPatrick McHardy 	if (tb[IFLA_ADDRESS]) {
22090157f60cSPatrick McHardy 		struct sockaddr *sa;
22100157f60cSPatrick McHardy 		int len;
22110157f60cSPatrick McHardy 
2212153711f9SWANG Cong 		len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
2213153711f9SWANG Cong 						  sizeof(*sa));
22140157f60cSPatrick McHardy 		sa = kmalloc(len, GFP_KERNEL);
22150157f60cSPatrick McHardy 		if (!sa) {
22160157f60cSPatrick McHardy 			err = -ENOMEM;
22170157f60cSPatrick McHardy 			goto errout;
22180157f60cSPatrick McHardy 		}
22190157f60cSPatrick McHardy 		sa->sa_family = dev->type;
22200157f60cSPatrick McHardy 		memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
22210157f60cSPatrick McHardy 		       dev->addr_len);
2222e7c3273eSJiri Pirko 		err = dev_set_mac_address(dev, sa);
22230157f60cSPatrick McHardy 		kfree(sa);
22240157f60cSPatrick McHardy 		if (err)
22250157f60cSPatrick McHardy 			goto errout;
222690c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
22270157f60cSPatrick McHardy 	}
22280157f60cSPatrick McHardy 
22290157f60cSPatrick McHardy 	if (tb[IFLA_MTU]) {
22300157f60cSPatrick McHardy 		err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
22310157f60cSPatrick McHardy 		if (err < 0)
22320157f60cSPatrick McHardy 			goto errout;
223390c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
22340157f60cSPatrick McHardy 	}
22350157f60cSPatrick McHardy 
2236cbda10faSVlad Dogaru 	if (tb[IFLA_GROUP]) {
2237cbda10faSVlad Dogaru 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
2238ba998906SNicolas Dichtel 		status |= DO_SETLINK_NOTIFY;
2239cbda10faSVlad Dogaru 	}
2240cbda10faSVlad Dogaru 
22410157f60cSPatrick McHardy 	/*
22420157f60cSPatrick McHardy 	 * Interface selected by interface index but interface
22430157f60cSPatrick McHardy 	 * name provided implies that a name change has been
22440157f60cSPatrick McHardy 	 * requested.
22450157f60cSPatrick McHardy 	 */
22460157f60cSPatrick McHardy 	if (ifm->ifi_index > 0 && ifname[0]) {
22470157f60cSPatrick McHardy 		err = dev_change_name(dev, ifname);
22480157f60cSPatrick McHardy 		if (err < 0)
22490157f60cSPatrick McHardy 			goto errout;
225090c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
22510157f60cSPatrick McHardy 	}
22520157f60cSPatrick McHardy 
22530b815a1aSStephen Hemminger 	if (tb[IFLA_IFALIAS]) {
22540b815a1aSStephen Hemminger 		err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
22550b815a1aSStephen Hemminger 				    nla_len(tb[IFLA_IFALIAS]));
22560b815a1aSStephen Hemminger 		if (err < 0)
22570b815a1aSStephen Hemminger 			goto errout;
2258ba998906SNicolas Dichtel 		status |= DO_SETLINK_NOTIFY;
22590b815a1aSStephen Hemminger 	}
22600b815a1aSStephen Hemminger 
22610157f60cSPatrick McHardy 	if (tb[IFLA_BROADCAST]) {
22620157f60cSPatrick McHardy 		nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
2263e7c3273eSJiri Pirko 		call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
22640157f60cSPatrick McHardy 	}
22650157f60cSPatrick McHardy 
22660157f60cSPatrick McHardy 	if (ifm->ifi_flags || ifm->ifi_change) {
22673729d502SPatrick McHardy 		err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
22685f9021cfSJohannes Berg 		if (err < 0)
22695f9021cfSJohannes Berg 			goto errout;
22700157f60cSPatrick McHardy 	}
22710157f60cSPatrick McHardy 
2272fbaec0eaSJiri Pirko 	if (tb[IFLA_MASTER]) {
227333eaf2a6SDavid Ahern 		err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
2274fbaec0eaSJiri Pirko 		if (err)
2275fbaec0eaSJiri Pirko 			goto errout;
227690c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
2277fbaec0eaSJiri Pirko 	}
2278fbaec0eaSJiri Pirko 
22799a57247fSJiri Pirko 	if (tb[IFLA_CARRIER]) {
22809a57247fSJiri Pirko 		err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
22819a57247fSJiri Pirko 		if (err)
22829a57247fSJiri Pirko 			goto errout;
228390c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
22849a57247fSJiri Pirko 	}
22859a57247fSJiri Pirko 
22865d1180fcSNicolas Dichtel 	if (tb[IFLA_TXQLEN]) {
22870cd29503SAlexey Dobriyan 		unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]);
22880cd29503SAlexey Dobriyan 		unsigned int orig_len = dev->tx_queue_len;
22895d1180fcSNicolas Dichtel 
229008294a26SJason Wang 		if (dev->tx_queue_len ^ value) {
22915d1180fcSNicolas Dichtel 			dev->tx_queue_len = value;
229208294a26SJason Wang 			err = call_netdevice_notifiers(
229308294a26SJason Wang 			      NETDEV_CHANGE_TX_QUEUE_LEN, dev);
229408294a26SJason Wang 			err = notifier_to_errno(err);
229508294a26SJason Wang 			if (err) {
229608294a26SJason Wang 				dev->tx_queue_len = orig_len;
229708294a26SJason Wang 				goto errout;
229808294a26SJason Wang 			}
22992d7f669bSXin Long 			status |= DO_SETLINK_MODIFIED;
230008294a26SJason Wang 		}
23015d1180fcSNicolas Dichtel 	}
23020157f60cSPatrick McHardy 
23030157f60cSPatrick McHardy 	if (tb[IFLA_OPERSTATE])
230493b2d4a2SDavid S. Miller 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
23050157f60cSPatrick McHardy 
23060157f60cSPatrick McHardy 	if (tb[IFLA_LINKMODE]) {
23071889b0e7SNicolas Dichtel 		unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
23081889b0e7SNicolas Dichtel 
23090157f60cSPatrick McHardy 		write_lock_bh(&dev_base_lock);
23101889b0e7SNicolas Dichtel 		if (dev->link_mode ^ value)
2311ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
23121889b0e7SNicolas Dichtel 		dev->link_mode = value;
231393b2d4a2SDavid S. Miller 		write_unlock_bh(&dev_base_lock);
23140157f60cSPatrick McHardy 	}
23150157f60cSPatrick McHardy 
2316c02db8c6SChris Wright 	if (tb[IFLA_VFINFO_LIST]) {
23174f7d2cdfSDaniel Borkmann 		struct nlattr *vfinfo[IFLA_VF_MAX + 1];
2318c02db8c6SChris Wright 		struct nlattr *attr;
2319c02db8c6SChris Wright 		int rem;
23204f7d2cdfSDaniel Borkmann 
2321c02db8c6SChris Wright 		nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
23224f7d2cdfSDaniel Borkmann 			if (nla_type(attr) != IFLA_VF_INFO ||
23234f7d2cdfSDaniel Borkmann 			    nla_len(attr) < NLA_HDRLEN) {
2324253683bbSDavid Howells 				err = -EINVAL;
2325c02db8c6SChris Wright 				goto errout;
2326253683bbSDavid Howells 			}
23274f7d2cdfSDaniel Borkmann 			err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr,
2328fceb6435SJohannes Berg 					       ifla_vf_policy, NULL);
23294f7d2cdfSDaniel Borkmann 			if (err < 0)
23304f7d2cdfSDaniel Borkmann 				goto errout;
23314f7d2cdfSDaniel Borkmann 			err = do_setvfinfo(dev, vfinfo);
2332ebc08a6fSWilliams, Mitch A 			if (err < 0)
2333ebc08a6fSWilliams, Mitch A 				goto errout;
2334ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
2335ebc08a6fSWilliams, Mitch A 		}
2336ebc08a6fSWilliams, Mitch A 	}
23370157f60cSPatrick McHardy 	err = 0;
23380157f60cSPatrick McHardy 
233957b61080SScott Feldman 	if (tb[IFLA_VF_PORTS]) {
234057b61080SScott Feldman 		struct nlattr *port[IFLA_PORT_MAX+1];
234157b61080SScott Feldman 		struct nlattr *attr;
234257b61080SScott Feldman 		int vf;
234357b61080SScott Feldman 		int rem;
234457b61080SScott Feldman 
234557b61080SScott Feldman 		err = -EOPNOTSUPP;
234657b61080SScott Feldman 		if (!ops->ndo_set_vf_port)
234757b61080SScott Feldman 			goto errout;
234857b61080SScott Feldman 
234957b61080SScott Feldman 		nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
2350035d210fSDaniel Borkmann 			if (nla_type(attr) != IFLA_VF_PORT ||
2351035d210fSDaniel Borkmann 			    nla_len(attr) < NLA_HDRLEN) {
2352035d210fSDaniel Borkmann 				err = -EINVAL;
2353035d210fSDaniel Borkmann 				goto errout;
2354035d210fSDaniel Borkmann 			}
2355035d210fSDaniel Borkmann 			err = nla_parse_nested(port, IFLA_PORT_MAX, attr,
2356fceb6435SJohannes Berg 					       ifla_port_policy, NULL);
235757b61080SScott Feldman 			if (err < 0)
235857b61080SScott Feldman 				goto errout;
235957b61080SScott Feldman 			if (!port[IFLA_PORT_VF]) {
236057b61080SScott Feldman 				err = -EOPNOTSUPP;
236157b61080SScott Feldman 				goto errout;
236257b61080SScott Feldman 			}
236357b61080SScott Feldman 			vf = nla_get_u32(port[IFLA_PORT_VF]);
236457b61080SScott Feldman 			err = ops->ndo_set_vf_port(dev, vf, port);
236557b61080SScott Feldman 			if (err < 0)
236657b61080SScott Feldman 				goto errout;
2367ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
236857b61080SScott Feldman 		}
236957b61080SScott Feldman 	}
237057b61080SScott Feldman 	err = 0;
237157b61080SScott Feldman 
237257b61080SScott Feldman 	if (tb[IFLA_PORT_SELF]) {
237357b61080SScott Feldman 		struct nlattr *port[IFLA_PORT_MAX+1];
237457b61080SScott Feldman 
237557b61080SScott Feldman 		err = nla_parse_nested(port, IFLA_PORT_MAX,
2376fceb6435SJohannes Berg 				       tb[IFLA_PORT_SELF], ifla_port_policy,
2377fceb6435SJohannes Berg 				       NULL);
237857b61080SScott Feldman 		if (err < 0)
237957b61080SScott Feldman 			goto errout;
238057b61080SScott Feldman 
238157b61080SScott Feldman 		err = -EOPNOTSUPP;
238257b61080SScott Feldman 		if (ops->ndo_set_vf_port)
238357b61080SScott Feldman 			err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
238457b61080SScott Feldman 		if (err < 0)
238557b61080SScott Feldman 			goto errout;
2386ba998906SNicolas Dichtel 		status |= DO_SETLINK_NOTIFY;
238757b61080SScott Feldman 	}
2388f8ff182cSThomas Graf 
2389f8ff182cSThomas Graf 	if (tb[IFLA_AF_SPEC]) {
2390f8ff182cSThomas Graf 		struct nlattr *af;
2391f8ff182cSThomas Graf 		int rem;
2392f8ff182cSThomas Graf 
2393f8ff182cSThomas Graf 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2394f8ff182cSThomas Graf 			const struct rtnl_af_ops *af_ops;
2395f8ff182cSThomas Graf 
23965fa85a09SFlorian Westphal 			rcu_read_lock();
23975fa85a09SFlorian Westphal 
2398058c8d59SGustavo A. R. Silva 			BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af))));
2399f8ff182cSThomas Graf 
2400cf7afbfeSThomas Graf 			err = af_ops->set_link_af(dev, af);
24015fa85a09SFlorian Westphal 			if (err < 0) {
24025fa85a09SFlorian Westphal 				rcu_read_unlock();
2403f8ff182cSThomas Graf 				goto errout;
24045fa85a09SFlorian Westphal 			}
2405f8ff182cSThomas Graf 
24065fa85a09SFlorian Westphal 			rcu_read_unlock();
2407ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
2408f8ff182cSThomas Graf 		}
2409f8ff182cSThomas Graf 	}
241057b61080SScott Feldman 	err = 0;
241157b61080SScott Feldman 
241288d6378bSAnuradha Karuppiah 	if (tb[IFLA_PROTO_DOWN]) {
241388d6378bSAnuradha Karuppiah 		err = dev_change_proto_down(dev,
241488d6378bSAnuradha Karuppiah 					    nla_get_u8(tb[IFLA_PROTO_DOWN]));
241588d6378bSAnuradha Karuppiah 		if (err)
241688d6378bSAnuradha Karuppiah 			goto errout;
241788d6378bSAnuradha Karuppiah 		status |= DO_SETLINK_NOTIFY;
241888d6378bSAnuradha Karuppiah 	}
241988d6378bSAnuradha Karuppiah 
2420d1fdd913SBrenden Blanco 	if (tb[IFLA_XDP]) {
2421d1fdd913SBrenden Blanco 		struct nlattr *xdp[IFLA_XDP_MAX + 1];
242285de8576SDaniel Borkmann 		u32 xdp_flags = 0;
2423d1fdd913SBrenden Blanco 
2424d1fdd913SBrenden Blanco 		err = nla_parse_nested(xdp, IFLA_XDP_MAX, tb[IFLA_XDP],
2425fceb6435SJohannes Berg 				       ifla_xdp_policy, NULL);
2426d1fdd913SBrenden Blanco 		if (err < 0)
2427d1fdd913SBrenden Blanco 			goto errout;
2428d1fdd913SBrenden Blanco 
242958038695SMartin KaFai Lau 		if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
2430262d8625SBrenden Blanco 			err = -EINVAL;
2431262d8625SBrenden Blanco 			goto errout;
2432262d8625SBrenden Blanco 		}
243385de8576SDaniel Borkmann 
243485de8576SDaniel Borkmann 		if (xdp[IFLA_XDP_FLAGS]) {
243585de8576SDaniel Borkmann 			xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
243685de8576SDaniel Borkmann 			if (xdp_flags & ~XDP_FLAGS_MASK) {
243785de8576SDaniel Borkmann 				err = -EINVAL;
243885de8576SDaniel Borkmann 				goto errout;
243985de8576SDaniel Borkmann 			}
2440ee5d032fSJakub Kicinski 			if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) {
24410489df9aSDaniel Borkmann 				err = -EINVAL;
24420489df9aSDaniel Borkmann 				goto errout;
24430489df9aSDaniel Borkmann 			}
244485de8576SDaniel Borkmann 		}
244585de8576SDaniel Borkmann 
2446d1fdd913SBrenden Blanco 		if (xdp[IFLA_XDP_FD]) {
2447ddf9f970SJakub Kicinski 			err = dev_change_xdp_fd(dev, extack,
244885de8576SDaniel Borkmann 						nla_get_s32(xdp[IFLA_XDP_FD]),
244985de8576SDaniel Borkmann 						xdp_flags);
2450d1fdd913SBrenden Blanco 			if (err)
2451d1fdd913SBrenden Blanco 				goto errout;
2452d1fdd913SBrenden Blanco 			status |= DO_SETLINK_NOTIFY;
2453d1fdd913SBrenden Blanco 		}
2454d1fdd913SBrenden Blanco 	}
2455d1fdd913SBrenden Blanco 
24560157f60cSPatrick McHardy errout:
2457ba998906SNicolas Dichtel 	if (status & DO_SETLINK_MODIFIED) {
245864ff90ccSXin Long 		if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY)
2459ba998906SNicolas Dichtel 			netdev_state_change(dev);
2460ba998906SNicolas Dichtel 
2461ba998906SNicolas Dichtel 		if (err < 0)
2462e87cc472SJoe 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",
2463e87cc472SJoe Perches 					     dev->name);
2464ba998906SNicolas Dichtel 	}
24650157f60cSPatrick McHardy 
24660157f60cSPatrick McHardy 	return err;
24670157f60cSPatrick McHardy }
24680157f60cSPatrick McHardy 
2469c21ef3e3SDavid Ahern static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2470c21ef3e3SDavid Ahern 			struct netlink_ext_ack *extack)
2471da5e0494SThomas Graf {
24723b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
2473da5e0494SThomas Graf 	struct ifinfomsg *ifm;
2474da5e0494SThomas Graf 	struct net_device *dev;
24750157f60cSPatrick McHardy 	int err;
2476da5e0494SThomas Graf 	struct nlattr *tb[IFLA_MAX+1];
24771da177e4SLinus Torvalds 	char ifname[IFNAMSIZ];
24781da177e4SLinus Torvalds 
2479c21ef3e3SDavid Ahern 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy,
2480c21ef3e3SDavid Ahern 			  extack);
2481da5e0494SThomas Graf 	if (err < 0)
2482da5e0494SThomas Graf 		goto errout;
24831da177e4SLinus Torvalds 
248479e1ad14SJiri Benc 	if (tb[IFLA_IF_NETNSID])
248579e1ad14SJiri Benc 		return -EOPNOTSUPP;
248679e1ad14SJiri Benc 
24875176f91eSThomas Graf 	if (tb[IFLA_IFNAME])
24885176f91eSThomas Graf 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
248978e5b891SPatrick McHardy 	else
249078e5b891SPatrick McHardy 		ifname[0] = '\0';
24911da177e4SLinus Torvalds 
24921da177e4SLinus Torvalds 	err = -EINVAL;
2493da5e0494SThomas Graf 	ifm = nlmsg_data(nlh);
249451055be8SPatrick McHardy 	if (ifm->ifi_index > 0)
2495a3d12891SEric Dumazet 		dev = __dev_get_by_index(net, ifm->ifi_index);
2496da5e0494SThomas Graf 	else if (tb[IFLA_IFNAME])
2497a3d12891SEric Dumazet 		dev = __dev_get_by_name(net, ifname);
2498da5e0494SThomas Graf 	else
2499da5e0494SThomas Graf 		goto errout;
25001da177e4SLinus Torvalds 
2501da5e0494SThomas Graf 	if (dev == NULL) {
2502da5e0494SThomas Graf 		err = -ENODEV;
2503da5e0494SThomas Graf 		goto errout;
2504da5e0494SThomas Graf 	}
25051da177e4SLinus Torvalds 
2506e0d087afSEric Dumazet 	err = validate_linkmsg(dev, tb);
2507e0d087afSEric Dumazet 	if (err < 0)
2508a3d12891SEric Dumazet 		goto errout;
2509da5e0494SThomas Graf 
2510ddf9f970SJakub Kicinski 	err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0);
2511da5e0494SThomas Graf errout:
25121da177e4SLinus Torvalds 	return err;
25131da177e4SLinus Torvalds }
25141da177e4SLinus Torvalds 
251566400d54SWANG Cong static int rtnl_group_dellink(const struct net *net, int group)
251666400d54SWANG Cong {
251766400d54SWANG Cong 	struct net_device *dev, *aux;
251866400d54SWANG Cong 	LIST_HEAD(list_kill);
251966400d54SWANG Cong 	bool found = false;
252066400d54SWANG Cong 
252166400d54SWANG Cong 	if (!group)
252266400d54SWANG Cong 		return -EPERM;
252366400d54SWANG Cong 
252466400d54SWANG Cong 	for_each_netdev(net, dev) {
252566400d54SWANG Cong 		if (dev->group == group) {
252666400d54SWANG Cong 			const struct rtnl_link_ops *ops;
252766400d54SWANG Cong 
252866400d54SWANG Cong 			found = true;
252966400d54SWANG Cong 			ops = dev->rtnl_link_ops;
253066400d54SWANG Cong 			if (!ops || !ops->dellink)
253166400d54SWANG Cong 				return -EOPNOTSUPP;
253266400d54SWANG Cong 		}
253366400d54SWANG Cong 	}
253466400d54SWANG Cong 
253566400d54SWANG Cong 	if (!found)
253666400d54SWANG Cong 		return -ENODEV;
253766400d54SWANG Cong 
253866400d54SWANG Cong 	for_each_netdev_safe(net, dev, aux) {
253966400d54SWANG Cong 		if (dev->group == group) {
254066400d54SWANG Cong 			const struct rtnl_link_ops *ops;
254166400d54SWANG Cong 
254266400d54SWANG Cong 			ops = dev->rtnl_link_ops;
254366400d54SWANG Cong 			ops->dellink(dev, &list_kill);
254466400d54SWANG Cong 		}
254566400d54SWANG Cong 	}
254666400d54SWANG Cong 	unregister_netdevice_many(&list_kill);
254766400d54SWANG Cong 
254866400d54SWANG Cong 	return 0;
254966400d54SWANG Cong }
255066400d54SWANG Cong 
2551614732eaSThomas Graf int rtnl_delete_link(struct net_device *dev)
2552614732eaSThomas Graf {
2553614732eaSThomas Graf 	const struct rtnl_link_ops *ops;
2554614732eaSThomas Graf 	LIST_HEAD(list_kill);
2555614732eaSThomas Graf 
2556614732eaSThomas Graf 	ops = dev->rtnl_link_ops;
2557614732eaSThomas Graf 	if (!ops || !ops->dellink)
2558614732eaSThomas Graf 		return -EOPNOTSUPP;
2559614732eaSThomas Graf 
2560614732eaSThomas Graf 	ops->dellink(dev, &list_kill);
2561614732eaSThomas Graf 	unregister_netdevice_many(&list_kill);
2562614732eaSThomas Graf 
2563614732eaSThomas Graf 	return 0;
2564614732eaSThomas Graf }
2565614732eaSThomas Graf EXPORT_SYMBOL_GPL(rtnl_delete_link);
2566614732eaSThomas Graf 
2567c21ef3e3SDavid Ahern static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
2568c21ef3e3SDavid Ahern 			struct netlink_ext_ack *extack)
256938f7b870SPatrick McHardy {
25703b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
257138f7b870SPatrick McHardy 	struct net_device *dev;
257238f7b870SPatrick McHardy 	struct ifinfomsg *ifm;
257338f7b870SPatrick McHardy 	char ifname[IFNAMSIZ];
257438f7b870SPatrick McHardy 	struct nlattr *tb[IFLA_MAX+1];
257538f7b870SPatrick McHardy 	int err;
257638f7b870SPatrick McHardy 
2577c21ef3e3SDavid Ahern 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
257838f7b870SPatrick McHardy 	if (err < 0)
257938f7b870SPatrick McHardy 		return err;
258038f7b870SPatrick McHardy 
258179e1ad14SJiri Benc 	if (tb[IFLA_IF_NETNSID])
258279e1ad14SJiri Benc 		return -EOPNOTSUPP;
258379e1ad14SJiri Benc 
258438f7b870SPatrick McHardy 	if (tb[IFLA_IFNAME])
258538f7b870SPatrick McHardy 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
258638f7b870SPatrick McHardy 
258738f7b870SPatrick McHardy 	ifm = nlmsg_data(nlh);
258838f7b870SPatrick McHardy 	if (ifm->ifi_index > 0)
2589881d966bSEric W. Biederman 		dev = __dev_get_by_index(net, ifm->ifi_index);
259038f7b870SPatrick McHardy 	else if (tb[IFLA_IFNAME])
2591881d966bSEric W. Biederman 		dev = __dev_get_by_name(net, ifname);
259266400d54SWANG Cong 	else if (tb[IFLA_GROUP])
259366400d54SWANG Cong 		return rtnl_group_dellink(net, nla_get_u32(tb[IFLA_GROUP]));
259438f7b870SPatrick McHardy 	else
259538f7b870SPatrick McHardy 		return -EINVAL;
259638f7b870SPatrick McHardy 
259738f7b870SPatrick McHardy 	if (!dev)
259838f7b870SPatrick McHardy 		return -ENODEV;
259938f7b870SPatrick McHardy 
2600614732eaSThomas Graf 	return rtnl_delete_link(dev);
260138f7b870SPatrick McHardy }
260238f7b870SPatrick McHardy 
26033729d502SPatrick McHardy int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
26043729d502SPatrick McHardy {
26053729d502SPatrick McHardy 	unsigned int old_flags;
26063729d502SPatrick McHardy 	int err;
26073729d502SPatrick McHardy 
26083729d502SPatrick McHardy 	old_flags = dev->flags;
26093729d502SPatrick McHardy 	if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
26103729d502SPatrick McHardy 		err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
26113729d502SPatrick McHardy 		if (err < 0)
26123729d502SPatrick McHardy 			return err;
26133729d502SPatrick McHardy 	}
26143729d502SPatrick McHardy 
26153729d502SPatrick McHardy 	dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
26163729d502SPatrick McHardy 
2617a528c219SNicolas Dichtel 	__dev_notify_flags(dev, old_flags, ~0U);
26183729d502SPatrick McHardy 	return 0;
26193729d502SPatrick McHardy }
26203729d502SPatrick McHardy EXPORT_SYMBOL(rtnl_configure_link);
26213729d502SPatrick McHardy 
2622c0713563SRami Rosen struct net_device *rtnl_create_link(struct net *net,
262378ebb0d0SThomas Graf 	const char *ifname, unsigned char name_assign_type,
26245517750fSTom Gundersen 	const struct rtnl_link_ops *ops, struct nlattr *tb[])
2625e7199288SPavel Emelianov {
2626e7199288SPavel Emelianov 	struct net_device *dev;
2627d40156aaSJiri Pirko 	unsigned int num_tx_queues = 1;
2628d40156aaSJiri Pirko 	unsigned int num_rx_queues = 1;
2629e7199288SPavel Emelianov 
263076ff5cc9SJiri Pirko 	if (tb[IFLA_NUM_TX_QUEUES])
263176ff5cc9SJiri Pirko 		num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
263276ff5cc9SJiri Pirko 	else if (ops->get_num_tx_queues)
2633d40156aaSJiri Pirko 		num_tx_queues = ops->get_num_tx_queues();
263476ff5cc9SJiri Pirko 
263576ff5cc9SJiri Pirko 	if (tb[IFLA_NUM_RX_QUEUES])
263676ff5cc9SJiri Pirko 		num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
263776ff5cc9SJiri Pirko 	else if (ops->get_num_rx_queues)
2638d40156aaSJiri Pirko 		num_rx_queues = ops->get_num_rx_queues();
2639efacb309Sstephen hemminger 
26405517750fSTom Gundersen 	dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
2641c835a677STom Gundersen 			       ops->setup, num_tx_queues, num_rx_queues);
2642e7199288SPavel Emelianov 	if (!dev)
2643d1892e4eSTobias Klauser 		return ERR_PTR(-ENOMEM);
2644e7199288SPavel Emelianov 
264581adee47SEric W. Biederman 	dev_net_set(dev, net);
264681adee47SEric W. Biederman 	dev->rtnl_link_ops = ops;
26473729d502SPatrick McHardy 	dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
264881adee47SEric W. Biederman 
2649e7199288SPavel Emelianov 	if (tb[IFLA_MTU])
2650e7199288SPavel Emelianov 		dev->mtu = nla_get_u32(tb[IFLA_MTU]);
26512afb9b53SJiri Pirko 	if (tb[IFLA_ADDRESS]) {
2652e7199288SPavel Emelianov 		memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
2653e7199288SPavel Emelianov 				nla_len(tb[IFLA_ADDRESS]));
26542afb9b53SJiri Pirko 		dev->addr_assign_type = NET_ADDR_SET;
26552afb9b53SJiri Pirko 	}
2656e7199288SPavel Emelianov 	if (tb[IFLA_BROADCAST])
2657e7199288SPavel Emelianov 		memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
2658e7199288SPavel Emelianov 				nla_len(tb[IFLA_BROADCAST]));
2659e7199288SPavel Emelianov 	if (tb[IFLA_TXQLEN])
2660e7199288SPavel Emelianov 		dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
2661e7199288SPavel Emelianov 	if (tb[IFLA_OPERSTATE])
266293b2d4a2SDavid S. Miller 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
2663e7199288SPavel Emelianov 	if (tb[IFLA_LINKMODE])
2664e7199288SPavel Emelianov 		dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
2665ffa934f1SPatrick McHardy 	if (tb[IFLA_GROUP])
2666ffa934f1SPatrick McHardy 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
2667e7199288SPavel Emelianov 
2668e7199288SPavel Emelianov 	return dev;
2669e7199288SPavel Emelianov }
2670e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_create_link);
2671e7199288SPavel Emelianov 
267290f62cf3SEric W. Biederman static int rtnl_group_changelink(const struct sk_buff *skb,
267390f62cf3SEric W. Biederman 		struct net *net, int group,
2674e7ed828fSVlad Dogaru 		struct ifinfomsg *ifm,
2675ddf9f970SJakub Kicinski 		struct netlink_ext_ack *extack,
2676e7ed828fSVlad Dogaru 		struct nlattr **tb)
2677e7ed828fSVlad Dogaru {
2678d079535dSWANG Cong 	struct net_device *dev, *aux;
2679e7ed828fSVlad Dogaru 	int err;
2680e7ed828fSVlad Dogaru 
2681d079535dSWANG Cong 	for_each_netdev_safe(net, dev, aux) {
2682e7ed828fSVlad Dogaru 		if (dev->group == group) {
2683ddf9f970SJakub Kicinski 			err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0);
2684e7ed828fSVlad Dogaru 			if (err < 0)
2685e7ed828fSVlad Dogaru 				return err;
2686e7ed828fSVlad Dogaru 		}
2687e7ed828fSVlad Dogaru 	}
2688e7ed828fSVlad Dogaru 
2689e7ed828fSVlad Dogaru 	return 0;
2690e7ed828fSVlad Dogaru }
2691e7ed828fSVlad Dogaru 
2692c21ef3e3SDavid Ahern static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2693c21ef3e3SDavid Ahern 			struct netlink_ext_ack *extack)
269438f7b870SPatrick McHardy {
26953b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
269638f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops;
2697ba7d49b1SJiri Pirko 	const struct rtnl_link_ops *m_ops = NULL;
269838f7b870SPatrick McHardy 	struct net_device *dev;
2699ba7d49b1SJiri Pirko 	struct net_device *master_dev = NULL;
270038f7b870SPatrick McHardy 	struct ifinfomsg *ifm;
270138f7b870SPatrick McHardy 	char kind[MODULE_NAME_LEN];
270238f7b870SPatrick McHardy 	char ifname[IFNAMSIZ];
270338f7b870SPatrick McHardy 	struct nlattr *tb[IFLA_MAX+1];
270438f7b870SPatrick McHardy 	struct nlattr *linkinfo[IFLA_INFO_MAX+1];
27055517750fSTom Gundersen 	unsigned char name_assign_type = NET_NAME_USER;
270638f7b870SPatrick McHardy 	int err;
270738f7b870SPatrick McHardy 
270895a5afcaSJohannes Berg #ifdef CONFIG_MODULES
270938f7b870SPatrick McHardy replay:
27108072f085SThomas Graf #endif
2711c21ef3e3SDavid Ahern 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
271238f7b870SPatrick McHardy 	if (err < 0)
271338f7b870SPatrick McHardy 		return err;
271438f7b870SPatrick McHardy 
271579e1ad14SJiri Benc 	if (tb[IFLA_IF_NETNSID])
271679e1ad14SJiri Benc 		return -EOPNOTSUPP;
271779e1ad14SJiri Benc 
271838f7b870SPatrick McHardy 	if (tb[IFLA_IFNAME])
271938f7b870SPatrick McHardy 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
272038f7b870SPatrick McHardy 	else
272138f7b870SPatrick McHardy 		ifname[0] = '\0';
272238f7b870SPatrick McHardy 
272338f7b870SPatrick McHardy 	ifm = nlmsg_data(nlh);
272438f7b870SPatrick McHardy 	if (ifm->ifi_index > 0)
2725881d966bSEric W. Biederman 		dev = __dev_get_by_index(net, ifm->ifi_index);
2726e7ed828fSVlad Dogaru 	else {
2727e7ed828fSVlad Dogaru 		if (ifname[0])
2728881d966bSEric W. Biederman 			dev = __dev_get_by_name(net, ifname);
272938f7b870SPatrick McHardy 		else
273038f7b870SPatrick McHardy 			dev = NULL;
2731e7ed828fSVlad Dogaru 	}
273238f7b870SPatrick McHardy 
2733ba7d49b1SJiri Pirko 	if (dev) {
2734ba7d49b1SJiri Pirko 		master_dev = netdev_master_upper_dev_get(dev);
2735ba7d49b1SJiri Pirko 		if (master_dev)
2736ba7d49b1SJiri Pirko 			m_ops = master_dev->rtnl_link_ops;
2737ba7d49b1SJiri Pirko 	}
2738ba7d49b1SJiri Pirko 
2739e0d087afSEric Dumazet 	err = validate_linkmsg(dev, tb);
2740e0d087afSEric Dumazet 	if (err < 0)
27411840bb13SThomas Graf 		return err;
27421840bb13SThomas Graf 
274338f7b870SPatrick McHardy 	if (tb[IFLA_LINKINFO]) {
274438f7b870SPatrick McHardy 		err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
2745fceb6435SJohannes Berg 				       tb[IFLA_LINKINFO], ifla_info_policy,
2746fceb6435SJohannes Berg 				       NULL);
274738f7b870SPatrick McHardy 		if (err < 0)
274838f7b870SPatrick McHardy 			return err;
274938f7b870SPatrick McHardy 	} else
275038f7b870SPatrick McHardy 		memset(linkinfo, 0, sizeof(linkinfo));
275138f7b870SPatrick McHardy 
275238f7b870SPatrick McHardy 	if (linkinfo[IFLA_INFO_KIND]) {
275338f7b870SPatrick McHardy 		nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
275438f7b870SPatrick McHardy 		ops = rtnl_link_ops_get(kind);
275538f7b870SPatrick McHardy 	} else {
275638f7b870SPatrick McHardy 		kind[0] = '\0';
275738f7b870SPatrick McHardy 		ops = NULL;
275838f7b870SPatrick McHardy 	}
275938f7b870SPatrick McHardy 
276038f7b870SPatrick McHardy 	if (1) {
27614e10fd5bSSasha Levin 		struct nlattr *attr[ops ? ops->maxtype + 1 : 1];
27624e10fd5bSSasha Levin 		struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 1];
2763ba7d49b1SJiri Pirko 		struct nlattr **data = NULL;
2764ba7d49b1SJiri Pirko 		struct nlattr **slave_data = NULL;
2765317f4810SNicolas Dichtel 		struct net *dest_net, *link_net = NULL;
276638f7b870SPatrick McHardy 
276738f7b870SPatrick McHardy 		if (ops) {
276838f7b870SPatrick McHardy 			if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
276938f7b870SPatrick McHardy 				err = nla_parse_nested(attr, ops->maxtype,
277038f7b870SPatrick McHardy 						       linkinfo[IFLA_INFO_DATA],
2771fceb6435SJohannes Berg 						       ops->policy, NULL);
277238f7b870SPatrick McHardy 				if (err < 0)
277338f7b870SPatrick McHardy 					return err;
277438f7b870SPatrick McHardy 				data = attr;
277538f7b870SPatrick McHardy 			}
277638f7b870SPatrick McHardy 			if (ops->validate) {
2777a8b8a889SMatthias Schiffer 				err = ops->validate(tb, data, extack);
277838f7b870SPatrick McHardy 				if (err < 0)
277938f7b870SPatrick McHardy 					return err;
278038f7b870SPatrick McHardy 			}
278138f7b870SPatrick McHardy 		}
278238f7b870SPatrick McHardy 
2783ba7d49b1SJiri Pirko 		if (m_ops) {
2784ba7d49b1SJiri Pirko 			if (m_ops->slave_maxtype &&
2785ba7d49b1SJiri Pirko 			    linkinfo[IFLA_INFO_SLAVE_DATA]) {
2786ba7d49b1SJiri Pirko 				err = nla_parse_nested(slave_attr,
2787ba7d49b1SJiri Pirko 						       m_ops->slave_maxtype,
2788ba7d49b1SJiri Pirko 						       linkinfo[IFLA_INFO_SLAVE_DATA],
2789fceb6435SJohannes Berg 						       m_ops->slave_policy,
2790fceb6435SJohannes Berg 						       NULL);
2791ba7d49b1SJiri Pirko 				if (err < 0)
2792ba7d49b1SJiri Pirko 					return err;
2793ba7d49b1SJiri Pirko 				slave_data = slave_attr;
2794ba7d49b1SJiri Pirko 			}
2795ba7d49b1SJiri Pirko 		}
2796ba7d49b1SJiri Pirko 
279738f7b870SPatrick McHardy 		if (dev) {
279890c325e3SNicolas Dichtel 			int status = 0;
279938f7b870SPatrick McHardy 
280038f7b870SPatrick McHardy 			if (nlh->nlmsg_flags & NLM_F_EXCL)
280138f7b870SPatrick McHardy 				return -EEXIST;
280238f7b870SPatrick McHardy 			if (nlh->nlmsg_flags & NLM_F_REPLACE)
280338f7b870SPatrick McHardy 				return -EOPNOTSUPP;
280438f7b870SPatrick McHardy 
280538f7b870SPatrick McHardy 			if (linkinfo[IFLA_INFO_DATA]) {
280638f7b870SPatrick McHardy 				if (!ops || ops != dev->rtnl_link_ops ||
280738f7b870SPatrick McHardy 				    !ops->changelink)
280838f7b870SPatrick McHardy 					return -EOPNOTSUPP;
280938f7b870SPatrick McHardy 
2810ad744b22SMatthias Schiffer 				err = ops->changelink(dev, tb, data, extack);
281138f7b870SPatrick McHardy 				if (err < 0)
281238f7b870SPatrick McHardy 					return err;
2813ba998906SNicolas Dichtel 				status |= DO_SETLINK_NOTIFY;
281438f7b870SPatrick McHardy 			}
281538f7b870SPatrick McHardy 
2816ba7d49b1SJiri Pirko 			if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
2817ba7d49b1SJiri Pirko 				if (!m_ops || !m_ops->slave_changelink)
2818ba7d49b1SJiri Pirko 					return -EOPNOTSUPP;
2819ba7d49b1SJiri Pirko 
2820ba7d49b1SJiri Pirko 				err = m_ops->slave_changelink(master_dev, dev,
282117dd0ec4SMatthias Schiffer 							      tb, slave_data,
282217dd0ec4SMatthias Schiffer 							      extack);
2823ba7d49b1SJiri Pirko 				if (err < 0)
2824ba7d49b1SJiri Pirko 					return err;
2825ba998906SNicolas Dichtel 				status |= DO_SETLINK_NOTIFY;
2826ba7d49b1SJiri Pirko 			}
2827ba7d49b1SJiri Pirko 
2828ddf9f970SJakub Kicinski 			return do_setlink(skb, dev, ifm, extack, tb, ifname,
2829ddf9f970SJakub Kicinski 					  status);
283038f7b870SPatrick McHardy 		}
283138f7b870SPatrick McHardy 
2832ffa934f1SPatrick McHardy 		if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
2833ffa934f1SPatrick McHardy 			if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
283490f62cf3SEric W. Biederman 				return rtnl_group_changelink(skb, net,
2835ffa934f1SPatrick McHardy 						nla_get_u32(tb[IFLA_GROUP]),
2836ddf9f970SJakub Kicinski 						ifm, extack, tb);
283738f7b870SPatrick McHardy 			return -ENODEV;
2838ffa934f1SPatrick McHardy 		}
283938f7b870SPatrick McHardy 
2840160ca014STheuns Verwoerd 		if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
284138f7b870SPatrick McHardy 			return -EOPNOTSUPP;
284238f7b870SPatrick McHardy 
284338f7b870SPatrick McHardy 		if (!ops) {
284495a5afcaSJohannes Berg #ifdef CONFIG_MODULES
284538f7b870SPatrick McHardy 			if (kind[0]) {
284638f7b870SPatrick McHardy 				__rtnl_unlock();
284738f7b870SPatrick McHardy 				request_module("rtnl-link-%s", kind);
284838f7b870SPatrick McHardy 				rtnl_lock();
284938f7b870SPatrick McHardy 				ops = rtnl_link_ops_get(kind);
285038f7b870SPatrick McHardy 				if (ops)
285138f7b870SPatrick McHardy 					goto replay;
285238f7b870SPatrick McHardy 			}
285338f7b870SPatrick McHardy #endif
285438f7b870SPatrick McHardy 			return -EOPNOTSUPP;
285538f7b870SPatrick McHardy 		}
285638f7b870SPatrick McHardy 
2857b0ab2fabSJiri Pirko 		if (!ops->setup)
2858b0ab2fabSJiri Pirko 			return -EOPNOTSUPP;
2859b0ab2fabSJiri Pirko 
28605517750fSTom Gundersen 		if (!ifname[0]) {
286138f7b870SPatrick McHardy 			snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
28625517750fSTom Gundersen 			name_assign_type = NET_NAME_ENUM;
28635517750fSTom Gundersen 		}
286438f7b870SPatrick McHardy 
286581adee47SEric W. Biederman 		dest_net = rtnl_link_get_net(net, tb);
286613ad1774SEric W. Biederman 		if (IS_ERR(dest_net))
286713ad1774SEric W. Biederman 			return PTR_ERR(dest_net);
286813ad1774SEric W. Biederman 
2869505ce415SEric W. Biederman 		err = -EPERM;
2870505ce415SEric W. Biederman 		if (!netlink_ns_capable(skb, dest_net->user_ns, CAP_NET_ADMIN))
2871505ce415SEric W. Biederman 			goto out;
2872505ce415SEric W. Biederman 
2873317f4810SNicolas Dichtel 		if (tb[IFLA_LINK_NETNSID]) {
2874317f4810SNicolas Dichtel 			int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
2875317f4810SNicolas Dichtel 
2876317f4810SNicolas Dichtel 			link_net = get_net_ns_by_id(dest_net, id);
2877317f4810SNicolas Dichtel 			if (!link_net) {
2878317f4810SNicolas Dichtel 				err =  -EINVAL;
2879317f4810SNicolas Dichtel 				goto out;
2880317f4810SNicolas Dichtel 			}
288106615bedSEric W. Biederman 			err = -EPERM;
288206615bedSEric W. Biederman 			if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
288306615bedSEric W. Biederman 				goto out;
2884317f4810SNicolas Dichtel 		}
2885317f4810SNicolas Dichtel 
2886317f4810SNicolas Dichtel 		dev = rtnl_create_link(link_net ? : dest_net, ifname,
2887317f4810SNicolas Dichtel 				       name_assign_type, ops, tb);
28889c7dafbfSPavel Emelyanov 		if (IS_ERR(dev)) {
2889e7199288SPavel Emelianov 			err = PTR_ERR(dev);
28909c7dafbfSPavel Emelyanov 			goto out;
28919c7dafbfSPavel Emelyanov 		}
28929c7dafbfSPavel Emelyanov 
28939c7dafbfSPavel Emelyanov 		dev->ifindex = ifm->ifi_index;
28949c7dafbfSPavel Emelyanov 
28950e0eee24SCong Wang 		if (ops->newlink) {
28967a3f4a18SMatthias Schiffer 			err = ops->newlink(link_net ? : net, dev, tb, data,
28977a3f4a18SMatthias Schiffer 					   extack);
28980e0eee24SCong Wang 			/* Drivers should call free_netdev() in ->destructor
2899e51fb152SCong Wang 			 * and unregister it on failure after registration
2900e51fb152SCong Wang 			 * so that device could be finally freed in rtnl_unlock.
29010e0eee24SCong Wang 			 */
2902e51fb152SCong Wang 			if (err < 0) {
2903e51fb152SCong Wang 				/* If device is not registered at all, free it now */
2904e51fb152SCong Wang 				if (dev->reg_state == NETREG_UNINITIALIZED)
2905e51fb152SCong Wang 					free_netdev(dev);
29060e0eee24SCong Wang 				goto out;
2907e51fb152SCong Wang 			}
29080e0eee24SCong Wang 		} else {
29092d85cba2SPatrick McHardy 			err = register_netdevice(dev);
2910fce9b9beSDan Carpenter 			if (err < 0) {
291138f7b870SPatrick McHardy 				free_netdev(dev);
29123729d502SPatrick McHardy 				goto out;
2913fce9b9beSDan Carpenter 			}
29140e0eee24SCong Wang 		}
29153729d502SPatrick McHardy 		err = rtnl_configure_link(dev, ifm);
291643638900SDavid S. Miller 		if (err < 0)
291743638900SDavid S. Miller 			goto out_unregister;
291843638900SDavid S. Miller 		if (link_net) {
291943638900SDavid S. Miller 			err = dev_change_net_namespace(dev, dest_net, ifname);
292043638900SDavid S. Miller 			if (err < 0)
292143638900SDavid S. Miller 				goto out_unregister;
292243638900SDavid S. Miller 		}
2923160ca014STheuns Verwoerd 		if (tb[IFLA_MASTER]) {
292433eaf2a6SDavid Ahern 			err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]),
292533eaf2a6SDavid Ahern 					    extack);
2926160ca014STheuns Verwoerd 			if (err)
2927160ca014STheuns Verwoerd 				goto out_unregister;
2928160ca014STheuns Verwoerd 		}
292943638900SDavid S. Miller out:
293043638900SDavid S. Miller 		if (link_net)
293143638900SDavid S. Miller 			put_net(link_net);
293243638900SDavid S. Miller 		put_net(dest_net);
293343638900SDavid S. Miller 		return err;
293443638900SDavid S. Miller out_unregister:
29357afb8886SWANG Cong 		if (ops->newlink) {
29367afb8886SWANG Cong 			LIST_HEAD(list_kill);
29377afb8886SWANG Cong 
29387afb8886SWANG Cong 			ops->dellink(dev, &list_kill);
29397afb8886SWANG Cong 			unregister_netdevice_many(&list_kill);
29407afb8886SWANG Cong 		} else {
29413729d502SPatrick McHardy 			unregister_netdevice(dev);
29427afb8886SWANG Cong 		}
2943317f4810SNicolas Dichtel 		goto out;
2944317f4810SNicolas Dichtel 	}
294538f7b870SPatrick McHardy }
294638f7b870SPatrick McHardy 
2947c21ef3e3SDavid Ahern static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2948c21ef3e3SDavid Ahern 			struct netlink_ext_ack *extack)
2949711e2c33SJean Tourrilhes {
29503b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
295179e1ad14SJiri Benc 	struct net *tgt_net = net;
2952b60c5115SThomas Graf 	struct ifinfomsg *ifm;
2953a3d12891SEric Dumazet 	char ifname[IFNAMSIZ];
2954b60c5115SThomas Graf 	struct nlattr *tb[IFLA_MAX+1];
2955b60c5115SThomas Graf 	struct net_device *dev = NULL;
2956b60c5115SThomas Graf 	struct sk_buff *nskb;
295779e1ad14SJiri Benc 	int netnsid = -1;
2958339bf98fSThomas Graf 	int err;
2959115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
2960711e2c33SJean Tourrilhes 
2961c21ef3e3SDavid Ahern 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
2962b60c5115SThomas Graf 	if (err < 0)
29639918f230SEric Sesterhenn 		return err;
2964b60c5115SThomas Graf 
296579e1ad14SJiri Benc 	if (tb[IFLA_IF_NETNSID]) {
296679e1ad14SJiri Benc 		netnsid = nla_get_s32(tb[IFLA_IF_NETNSID]);
296779e1ad14SJiri Benc 		tgt_net = get_target_net(skb, netnsid);
296879e1ad14SJiri Benc 		if (IS_ERR(tgt_net))
296979e1ad14SJiri Benc 			return PTR_ERR(tgt_net);
297079e1ad14SJiri Benc 	}
297179e1ad14SJiri Benc 
2972a3d12891SEric Dumazet 	if (tb[IFLA_IFNAME])
2973a3d12891SEric Dumazet 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2974a3d12891SEric Dumazet 
2975115c9b81SGreg Rose 	if (tb[IFLA_EXT_MASK])
2976115c9b81SGreg Rose 		ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2977115c9b81SGreg Rose 
297879e1ad14SJiri Benc 	err = -EINVAL;
2979b60c5115SThomas Graf 	ifm = nlmsg_data(nlh);
2980a3d12891SEric Dumazet 	if (ifm->ifi_index > 0)
298179e1ad14SJiri Benc 		dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
2982a3d12891SEric Dumazet 	else if (tb[IFLA_IFNAME])
298379e1ad14SJiri Benc 		dev = __dev_get_by_name(tgt_net, ifname);
2984a3d12891SEric Dumazet 	else
298579e1ad14SJiri Benc 		goto out;
2986b60c5115SThomas Graf 
298779e1ad14SJiri Benc 	err = -ENODEV;
2988a3d12891SEric Dumazet 	if (dev == NULL)
298979e1ad14SJiri Benc 		goto out;
2990a3d12891SEric Dumazet 
299179e1ad14SJiri Benc 	err = -ENOBUFS;
2992115c9b81SGreg Rose 	nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
2993a3d12891SEric Dumazet 	if (nskb == NULL)
299479e1ad14SJiri Benc 		goto out;
2995711e2c33SJean Tourrilhes 
299679e1ad14SJiri Benc 	err = rtnl_fill_ifinfo(nskb, dev, net,
299779e1ad14SJiri Benc 			       RTM_NEWLINK, NETLINK_CB(skb).portid,
299879e1ad14SJiri Benc 			       nlh->nlmsg_seq, 0, 0, ext_filter_mask,
299979e1ad14SJiri Benc 			       0, NULL, netnsid);
300026932566SPatrick McHardy 	if (err < 0) {
300126932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in if_nlmsg_size */
300226932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
300326932566SPatrick McHardy 		kfree_skb(nskb);
3004a3d12891SEric Dumazet 	} else
300515e47304SEric W. Biederman 		err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
300679e1ad14SJiri Benc out:
300779e1ad14SJiri Benc 	if (netnsid >= 0)
300879e1ad14SJiri Benc 		put_net(tgt_net);
3009b60c5115SThomas Graf 
3010711e2c33SJean Tourrilhes 	return err;
3011711e2c33SJean Tourrilhes }
3012711e2c33SJean Tourrilhes 
3013115c9b81SGreg Rose static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
3014c7ac8679SGreg Rose {
3015115c9b81SGreg Rose 	struct net *net = sock_net(skb->sk);
3016115c9b81SGreg Rose 	struct net_device *dev;
3017115c9b81SGreg Rose 	struct nlattr *tb[IFLA_MAX+1];
3018115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
3019115c9b81SGreg Rose 	u16 min_ifinfo_dump_size = 0;
3020e5eca6d4SMichal Schmidt 	int hdrlen;
3021115c9b81SGreg Rose 
3022e5eca6d4SMichal Schmidt 	/* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
3023e5eca6d4SMichal Schmidt 	hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
3024e5eca6d4SMichal Schmidt 		 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
3025e5eca6d4SMichal Schmidt 
3026fceb6435SJohannes Berg 	if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
3027115c9b81SGreg Rose 		if (tb[IFLA_EXT_MASK])
3028115c9b81SGreg Rose 			ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3029a4b64fbeSEric Dumazet 	}
3030115c9b81SGreg Rose 
3031115c9b81SGreg Rose 	if (!ext_filter_mask)
3032115c9b81SGreg Rose 		return NLMSG_GOODSIZE;
3033115c9b81SGreg Rose 	/*
3034115c9b81SGreg Rose 	 * traverse the list of net devices and compute the minimum
3035115c9b81SGreg Rose 	 * buffer size based upon the filter mask.
3036115c9b81SGreg Rose 	 */
30376853dd48SFlorian Westphal 	rcu_read_lock();
30386853dd48SFlorian Westphal 	for_each_netdev_rcu(net, dev) {
3039115c9b81SGreg Rose 		min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
3040115c9b81SGreg Rose 					     if_nlmsg_size(dev,
3041115c9b81SGreg Rose 						           ext_filter_mask));
3042115c9b81SGreg Rose 	}
30436853dd48SFlorian Westphal 	rcu_read_unlock();
3044115c9b81SGreg Rose 
304593af2056SZhang Shengju 	return nlmsg_total_size(min_ifinfo_dump_size);
3046c7ac8679SGreg Rose }
3047c7ac8679SGreg Rose 
304842bad1daSAdrian Bunk static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
30491da177e4SLinus Torvalds {
30501da177e4SLinus Torvalds 	int idx;
30511da177e4SLinus Torvalds 	int s_idx = cb->family;
30521da177e4SLinus Torvalds 
30531da177e4SLinus Torvalds 	if (s_idx == 0)
30541da177e4SLinus Torvalds 		s_idx = 1;
30556853dd48SFlorian Westphal 
305625239ceeSPatrick McHardy 	for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
3057addf9b90SFlorian Westphal 		struct rtnl_link **tab;
30581da177e4SLinus Torvalds 		int type = cb->nlh->nlmsg_type-RTM_BASE;
3059addf9b90SFlorian Westphal 		struct rtnl_link *link;
30606853dd48SFlorian Westphal 		rtnl_dumpit_func dumpit;
30616853dd48SFlorian Westphal 
30621da177e4SLinus Torvalds 		if (idx < s_idx || idx == PF_PACKET)
30631da177e4SLinus Torvalds 			continue;
30646853dd48SFlorian Westphal 
3065addf9b90SFlorian Westphal 		if (type < 0 || type >= RTM_NR_MSGTYPES)
30661da177e4SLinus Torvalds 			continue;
30676853dd48SFlorian Westphal 
3068addf9b90SFlorian Westphal 		tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]);
3069addf9b90SFlorian Westphal 		if (!tab)
3070addf9b90SFlorian Westphal 			continue;
3071addf9b90SFlorian Westphal 
3072addf9b90SFlorian Westphal 		link = tab[type];
3073addf9b90SFlorian Westphal 		if (!link)
3074addf9b90SFlorian Westphal 			continue;
3075addf9b90SFlorian Westphal 
3076addf9b90SFlorian Westphal 		dumpit = link->dumpit;
30776853dd48SFlorian Westphal 		if (!dumpit)
30786853dd48SFlorian Westphal 			continue;
30796853dd48SFlorian Westphal 
30800465277fSNicolas Dichtel 		if (idx > s_idx) {
30811da177e4SLinus Torvalds 			memset(&cb->args[0], 0, sizeof(cb->args));
30820465277fSNicolas Dichtel 			cb->prev_seq = 0;
30830465277fSNicolas Dichtel 			cb->seq = 0;
30840465277fSNicolas Dichtel 		}
30856853dd48SFlorian Westphal 		if (dumpit(skb, cb))
30861da177e4SLinus Torvalds 			break;
30871da177e4SLinus Torvalds 	}
30881da177e4SLinus Torvalds 	cb->family = idx;
30891da177e4SLinus Torvalds 
30901da177e4SLinus Torvalds 	return skb->len;
30911da177e4SLinus Torvalds }
30921da177e4SLinus Torvalds 
3093395eea6cSMahesh Bandewar struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
30943d3ea5afSVlad Yasevich 				       unsigned int change,
30956621dd29SNicolas Dichtel 				       u32 event, gfp_t flags, int *new_nsid)
30961da177e4SLinus Torvalds {
3097c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
30981da177e4SLinus Torvalds 	struct sk_buff *skb;
30990ec6d3f4SThomas Graf 	int err = -ENOBUFS;
3100c7ac8679SGreg Rose 	size_t if_info_size;
31011da177e4SLinus Torvalds 
31027f294054SAlexei Starovoitov 	skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
31030ec6d3f4SThomas Graf 	if (skb == NULL)
31040ec6d3f4SThomas Graf 		goto errout;
31051da177e4SLinus Torvalds 
310679e1ad14SJiri Benc 	err = rtnl_fill_ifinfo(skb, dev, dev_net(dev),
310779e1ad14SJiri Benc 			       type, 0, 0, change, 0, 0, event,
310879e1ad14SJiri Benc 			       new_nsid, -1);
310926932566SPatrick McHardy 	if (err < 0) {
311026932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in if_nlmsg_size() */
311126932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
311226932566SPatrick McHardy 		kfree_skb(skb);
311326932566SPatrick McHardy 		goto errout;
311426932566SPatrick McHardy 	}
3115395eea6cSMahesh Bandewar 	return skb;
31160ec6d3f4SThomas Graf errout:
31170ec6d3f4SThomas Graf 	if (err < 0)
31184b3da706SEric W. Biederman 		rtnl_set_sk_err(net, RTNLGRP_LINK, err);
3119395eea6cSMahesh Bandewar 	return NULL;
3120395eea6cSMahesh Bandewar }
3121395eea6cSMahesh Bandewar 
3122395eea6cSMahesh Bandewar void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
3123395eea6cSMahesh Bandewar {
3124395eea6cSMahesh Bandewar 	struct net *net = dev_net(dev);
3125395eea6cSMahesh Bandewar 
3126395eea6cSMahesh Bandewar 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
3127395eea6cSMahesh Bandewar }
3128395eea6cSMahesh Bandewar 
31293d3ea5afSVlad Yasevich static void rtmsg_ifinfo_event(int type, struct net_device *dev,
31303d3ea5afSVlad Yasevich 			       unsigned int change, u32 event,
31316621dd29SNicolas Dichtel 			       gfp_t flags, int *new_nsid)
3132395eea6cSMahesh Bandewar {
3133395eea6cSMahesh Bandewar 	struct sk_buff *skb;
3134395eea6cSMahesh Bandewar 
3135ed2a80abSNicolas Dichtel 	if (dev->reg_state != NETREG_REGISTERED)
3136ed2a80abSNicolas Dichtel 		return;
3137ed2a80abSNicolas Dichtel 
31386621dd29SNicolas Dichtel 	skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid);
3139395eea6cSMahesh Bandewar 	if (skb)
3140395eea6cSMahesh Bandewar 		rtmsg_ifinfo_send(skb, dev, flags);
31411da177e4SLinus Torvalds }
31423d3ea5afSVlad Yasevich 
31433d3ea5afSVlad Yasevich void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
31443d3ea5afSVlad Yasevich 		  gfp_t flags)
31453d3ea5afSVlad Yasevich {
31466621dd29SNicolas Dichtel 	rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags, NULL);
31473d3ea5afSVlad Yasevich }
31481da177e4SLinus Torvalds 
31496621dd29SNicolas Dichtel void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change,
31506621dd29SNicolas Dichtel 			 gfp_t flags, int *new_nsid)
31516621dd29SNicolas Dichtel {
31526621dd29SNicolas Dichtel 	rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
31536621dd29SNicolas Dichtel 			   new_nsid);
31546621dd29SNicolas Dichtel }
31556621dd29SNicolas Dichtel 
3156d83b0603SJohn Fastabend static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
3157d83b0603SJohn Fastabend 				   struct net_device *dev,
31581e53d5bbSHubert Sokolowski 				   u8 *addr, u16 vid, u32 pid, u32 seq,
31591c104a6bSNicolas Dichtel 				   int type, unsigned int flags,
3160b3379041SHubert Sokolowski 				   int nlflags, u16 ndm_state)
3161d83b0603SJohn Fastabend {
3162d83b0603SJohn Fastabend 	struct nlmsghdr *nlh;
3163d83b0603SJohn Fastabend 	struct ndmsg *ndm;
3164d83b0603SJohn Fastabend 
31651c104a6bSNicolas Dichtel 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
3166d83b0603SJohn Fastabend 	if (!nlh)
3167d83b0603SJohn Fastabend 		return -EMSGSIZE;
3168d83b0603SJohn Fastabend 
3169d83b0603SJohn Fastabend 	ndm = nlmsg_data(nlh);
3170d83b0603SJohn Fastabend 	ndm->ndm_family  = AF_BRIDGE;
3171d83b0603SJohn Fastabend 	ndm->ndm_pad1	 = 0;
3172d83b0603SJohn Fastabend 	ndm->ndm_pad2    = 0;
3173d83b0603SJohn Fastabend 	ndm->ndm_flags	 = flags;
3174d83b0603SJohn Fastabend 	ndm->ndm_type	 = 0;
3175d83b0603SJohn Fastabend 	ndm->ndm_ifindex = dev->ifindex;
3176b3379041SHubert Sokolowski 	ndm->ndm_state   = ndm_state;
3177d83b0603SJohn Fastabend 
3178d83b0603SJohn Fastabend 	if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
3179d83b0603SJohn Fastabend 		goto nla_put_failure;
31801e53d5bbSHubert Sokolowski 	if (vid)
31811e53d5bbSHubert Sokolowski 		if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
31821e53d5bbSHubert Sokolowski 			goto nla_put_failure;
3183d83b0603SJohn Fastabend 
3184053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
3185053c095aSJohannes Berg 	return 0;
3186d83b0603SJohn Fastabend 
3187d83b0603SJohn Fastabend nla_put_failure:
3188d83b0603SJohn Fastabend 	nlmsg_cancel(skb, nlh);
3189d83b0603SJohn Fastabend 	return -EMSGSIZE;
3190d83b0603SJohn Fastabend }
3191d83b0603SJohn Fastabend 
31923ff661c3SJohn Fastabend static inline size_t rtnl_fdb_nlmsg_size(void)
31933ff661c3SJohn Fastabend {
3194f82ef3e1SSabrina Dubroca 	return NLMSG_ALIGN(sizeof(struct ndmsg)) +
3195f82ef3e1SSabrina Dubroca 	       nla_total_size(ETH_ALEN) +	/* NDA_LLADDR */
3196f82ef3e1SSabrina Dubroca 	       nla_total_size(sizeof(u16)) +	/* NDA_VLAN */
3197f82ef3e1SSabrina Dubroca 	       0;
31983ff661c3SJohn Fastabend }
31993ff661c3SJohn Fastabend 
3200b3379041SHubert Sokolowski static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
3201b3379041SHubert Sokolowski 			    u16 ndm_state)
32023ff661c3SJohn Fastabend {
32033ff661c3SJohn Fastabend 	struct net *net = dev_net(dev);
32043ff661c3SJohn Fastabend 	struct sk_buff *skb;
32053ff661c3SJohn Fastabend 	int err = -ENOBUFS;
32063ff661c3SJohn Fastabend 
32073ff661c3SJohn Fastabend 	skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
32083ff661c3SJohn Fastabend 	if (!skb)
32093ff661c3SJohn Fastabend 		goto errout;
32103ff661c3SJohn Fastabend 
32111e53d5bbSHubert Sokolowski 	err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
3212b3379041SHubert Sokolowski 				      0, 0, type, NTF_SELF, 0, ndm_state);
32133ff661c3SJohn Fastabend 	if (err < 0) {
32143ff661c3SJohn Fastabend 		kfree_skb(skb);
32153ff661c3SJohn Fastabend 		goto errout;
32163ff661c3SJohn Fastabend 	}
32173ff661c3SJohn Fastabend 
32183ff661c3SJohn Fastabend 	rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
32193ff661c3SJohn Fastabend 	return;
32203ff661c3SJohn Fastabend errout:
32213ff661c3SJohn Fastabend 	rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
32223ff661c3SJohn Fastabend }
32233ff661c3SJohn Fastabend 
3224090096bfSVlad Yasevich /**
3225090096bfSVlad Yasevich  * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
3226090096bfSVlad Yasevich  */
3227090096bfSVlad Yasevich int ndo_dflt_fdb_add(struct ndmsg *ndm,
3228090096bfSVlad Yasevich 		     struct nlattr *tb[],
3229090096bfSVlad Yasevich 		     struct net_device *dev,
3230f6f6424bSJiri Pirko 		     const unsigned char *addr, u16 vid,
3231090096bfSVlad Yasevich 		     u16 flags)
3232090096bfSVlad Yasevich {
3233090096bfSVlad Yasevich 	int err = -EINVAL;
3234090096bfSVlad Yasevich 
3235090096bfSVlad Yasevich 	/* If aging addresses are supported device will need to
3236090096bfSVlad Yasevich 	 * implement its own handler for this.
3237090096bfSVlad Yasevich 	 */
3238090096bfSVlad Yasevich 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
3239090096bfSVlad Yasevich 		pr_info("%s: FDB only supports static addresses\n", dev->name);
3240090096bfSVlad Yasevich 		return err;
3241090096bfSVlad Yasevich 	}
3242090096bfSVlad Yasevich 
324365891feaSOr Gerlitz 	if (vid) {
324465891feaSOr Gerlitz 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
324565891feaSOr Gerlitz 		return err;
324665891feaSOr Gerlitz 	}
324765891feaSOr Gerlitz 
3248090096bfSVlad Yasevich 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3249090096bfSVlad Yasevich 		err = dev_uc_add_excl(dev, addr);
3250090096bfSVlad Yasevich 	else if (is_multicast_ether_addr(addr))
3251090096bfSVlad Yasevich 		err = dev_mc_add_excl(dev, addr);
3252090096bfSVlad Yasevich 
3253090096bfSVlad Yasevich 	/* Only return duplicate errors if NLM_F_EXCL is set */
3254090096bfSVlad Yasevich 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
3255090096bfSVlad Yasevich 		err = 0;
3256090096bfSVlad Yasevich 
3257090096bfSVlad Yasevich 	return err;
3258090096bfSVlad Yasevich }
3259090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_add);
3260090096bfSVlad Yasevich 
3261b88d12e4SFlorian Westphal static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid,
3262b88d12e4SFlorian Westphal 			 struct netlink_ext_ack *extack)
3263f6f6424bSJiri Pirko {
3264f6f6424bSJiri Pirko 	u16 vid = 0;
3265f6f6424bSJiri Pirko 
3266f6f6424bSJiri Pirko 	if (vlan_attr) {
3267f6f6424bSJiri Pirko 		if (nla_len(vlan_attr) != sizeof(u16)) {
3268b88d12e4SFlorian Westphal 			NL_SET_ERR_MSG(extack, "invalid vlan attribute size");
3269f6f6424bSJiri Pirko 			return -EINVAL;
3270f6f6424bSJiri Pirko 		}
3271f6f6424bSJiri Pirko 
3272f6f6424bSJiri Pirko 		vid = nla_get_u16(vlan_attr);
3273f6f6424bSJiri Pirko 
3274f6f6424bSJiri Pirko 		if (!vid || vid >= VLAN_VID_MASK) {
3275b88d12e4SFlorian Westphal 			NL_SET_ERR_MSG(extack, "invalid vlan id");
3276f6f6424bSJiri Pirko 			return -EINVAL;
3277f6f6424bSJiri Pirko 		}
3278f6f6424bSJiri Pirko 	}
3279f6f6424bSJiri Pirko 	*p_vid = vid;
3280f6f6424bSJiri Pirko 	return 0;
3281f6f6424bSJiri Pirko }
3282f6f6424bSJiri Pirko 
3283c21ef3e3SDavid Ahern static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
3284c21ef3e3SDavid Ahern 			struct netlink_ext_ack *extack)
328577162022SJohn Fastabend {
328677162022SJohn Fastabend 	struct net *net = sock_net(skb->sk);
328777162022SJohn Fastabend 	struct ndmsg *ndm;
328877162022SJohn Fastabend 	struct nlattr *tb[NDA_MAX+1];
328977162022SJohn Fastabend 	struct net_device *dev;
329077162022SJohn Fastabend 	u8 *addr;
3291f6f6424bSJiri Pirko 	u16 vid;
329277162022SJohn Fastabend 	int err;
329377162022SJohn Fastabend 
3294c21ef3e3SDavid Ahern 	err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
329577162022SJohn Fastabend 	if (err < 0)
329677162022SJohn Fastabend 		return err;
329777162022SJohn Fastabend 
329877162022SJohn Fastabend 	ndm = nlmsg_data(nlh);
329977162022SJohn Fastabend 	if (ndm->ndm_ifindex == 0) {
3300b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "invalid ifindex");
330177162022SJohn Fastabend 		return -EINVAL;
330277162022SJohn Fastabend 	}
330377162022SJohn Fastabend 
330477162022SJohn Fastabend 	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
330577162022SJohn Fastabend 	if (dev == NULL) {
3306b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "unknown ifindex");
330777162022SJohn Fastabend 		return -ENODEV;
330877162022SJohn Fastabend 	}
330977162022SJohn Fastabend 
331077162022SJohn Fastabend 	if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
3311b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "invalid address");
331277162022SJohn Fastabend 		return -EINVAL;
331377162022SJohn Fastabend 	}
331477162022SJohn Fastabend 
331577162022SJohn Fastabend 	addr = nla_data(tb[NDA_LLADDR]);
331677162022SJohn Fastabend 
3317b88d12e4SFlorian Westphal 	err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
3318f6f6424bSJiri Pirko 	if (err)
3319f6f6424bSJiri Pirko 		return err;
3320f6f6424bSJiri Pirko 
332177162022SJohn Fastabend 	err = -EOPNOTSUPP;
332277162022SJohn Fastabend 
332377162022SJohn Fastabend 	/* Support fdb on master device the net/bridge default case */
332477162022SJohn Fastabend 	if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
332577162022SJohn Fastabend 	    (dev->priv_flags & IFF_BRIDGE_PORT)) {
3326898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3327898e5061SJiri Pirko 		const struct net_device_ops *ops = br_dev->netdev_ops;
3328898e5061SJiri Pirko 
3329f6f6424bSJiri Pirko 		err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
3330f6f6424bSJiri Pirko 				       nlh->nlmsg_flags);
333177162022SJohn Fastabend 		if (err)
333277162022SJohn Fastabend 			goto out;
333377162022SJohn Fastabend 		else
333477162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_MASTER;
333577162022SJohn Fastabend 	}
333677162022SJohn Fastabend 
333777162022SJohn Fastabend 	/* Embedded bridge, macvlan, and any other device support */
3338090096bfSVlad Yasevich 	if ((ndm->ndm_flags & NTF_SELF)) {
3339090096bfSVlad Yasevich 		if (dev->netdev_ops->ndo_fdb_add)
3340090096bfSVlad Yasevich 			err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
3341f6f6424bSJiri Pirko 							   vid,
3342090096bfSVlad Yasevich 							   nlh->nlmsg_flags);
3343090096bfSVlad Yasevich 		else
3344f6f6424bSJiri Pirko 			err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
334577162022SJohn Fastabend 					       nlh->nlmsg_flags);
334677162022SJohn Fastabend 
33473ff661c3SJohn Fastabend 		if (!err) {
3348b3379041SHubert Sokolowski 			rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
3349b3379041SHubert Sokolowski 					ndm->ndm_state);
335077162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_SELF;
335177162022SJohn Fastabend 		}
33523ff661c3SJohn Fastabend 	}
335377162022SJohn Fastabend out:
335477162022SJohn Fastabend 	return err;
335577162022SJohn Fastabend }
335677162022SJohn Fastabend 
3357090096bfSVlad Yasevich /**
3358090096bfSVlad Yasevich  * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
3359090096bfSVlad Yasevich  */
3360090096bfSVlad Yasevich int ndo_dflt_fdb_del(struct ndmsg *ndm,
3361090096bfSVlad Yasevich 		     struct nlattr *tb[],
3362090096bfSVlad Yasevich 		     struct net_device *dev,
3363f6f6424bSJiri Pirko 		     const unsigned char *addr, u16 vid)
3364090096bfSVlad Yasevich {
3365c8a89c4aSAlexander Duyck 	int err = -EINVAL;
3366090096bfSVlad Yasevich 
3367090096bfSVlad Yasevich 	/* If aging addresses are supported device will need to
3368090096bfSVlad Yasevich 	 * implement its own handler for this.
3369090096bfSVlad Yasevich 	 */
337064535993SSridhar Samudrala 	if (!(ndm->ndm_state & NUD_PERMANENT)) {
3371090096bfSVlad Yasevich 		pr_info("%s: FDB only supports static addresses\n", dev->name);
3372c8a89c4aSAlexander Duyck 		return err;
3373090096bfSVlad Yasevich 	}
3374090096bfSVlad Yasevich 
3375090096bfSVlad Yasevich 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3376090096bfSVlad Yasevich 		err = dev_uc_del(dev, addr);
3377090096bfSVlad Yasevich 	else if (is_multicast_ether_addr(addr))
3378090096bfSVlad Yasevich 		err = dev_mc_del(dev, addr);
3379090096bfSVlad Yasevich 
3380090096bfSVlad Yasevich 	return err;
3381090096bfSVlad Yasevich }
3382090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_del);
3383090096bfSVlad Yasevich 
3384c21ef3e3SDavid Ahern static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
3385c21ef3e3SDavid Ahern 			struct netlink_ext_ack *extack)
338677162022SJohn Fastabend {
338777162022SJohn Fastabend 	struct net *net = sock_net(skb->sk);
338877162022SJohn Fastabend 	struct ndmsg *ndm;
33891690be63SVlad Yasevich 	struct nlattr *tb[NDA_MAX+1];
339077162022SJohn Fastabend 	struct net_device *dev;
339177162022SJohn Fastabend 	int err = -EINVAL;
339277162022SJohn Fastabend 	__u8 *addr;
3393f6f6424bSJiri Pirko 	u16 vid;
339477162022SJohn Fastabend 
339590f62cf3SEric W. Biederman 	if (!netlink_capable(skb, CAP_NET_ADMIN))
33961690be63SVlad Yasevich 		return -EPERM;
33971690be63SVlad Yasevich 
3398c21ef3e3SDavid Ahern 	err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
33991690be63SVlad Yasevich 	if (err < 0)
34001690be63SVlad Yasevich 		return err;
340177162022SJohn Fastabend 
340277162022SJohn Fastabend 	ndm = nlmsg_data(nlh);
340377162022SJohn Fastabend 	if (ndm->ndm_ifindex == 0) {
3404b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "invalid ifindex");
340577162022SJohn Fastabend 		return -EINVAL;
340677162022SJohn Fastabend 	}
340777162022SJohn Fastabend 
340877162022SJohn Fastabend 	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
340977162022SJohn Fastabend 	if (dev == NULL) {
3410b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "unknown ifindex");
341177162022SJohn Fastabend 		return -ENODEV;
341277162022SJohn Fastabend 	}
341377162022SJohn Fastabend 
34141690be63SVlad Yasevich 	if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
3415b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "invalid address");
341677162022SJohn Fastabend 		return -EINVAL;
341777162022SJohn Fastabend 	}
341877162022SJohn Fastabend 
34191690be63SVlad Yasevich 	addr = nla_data(tb[NDA_LLADDR]);
34201690be63SVlad Yasevich 
3421b88d12e4SFlorian Westphal 	err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
3422f6f6424bSJiri Pirko 	if (err)
3423f6f6424bSJiri Pirko 		return err;
3424f6f6424bSJiri Pirko 
342577162022SJohn Fastabend 	err = -EOPNOTSUPP;
342677162022SJohn Fastabend 
342777162022SJohn Fastabend 	/* Support fdb on master device the net/bridge default case */
342877162022SJohn Fastabend 	if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
342977162022SJohn Fastabend 	    (dev->priv_flags & IFF_BRIDGE_PORT)) {
3430898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3431898e5061SJiri Pirko 		const struct net_device_ops *ops = br_dev->netdev_ops;
343277162022SJohn Fastabend 
3433898e5061SJiri Pirko 		if (ops->ndo_fdb_del)
3434f6f6424bSJiri Pirko 			err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
343577162022SJohn Fastabend 
343677162022SJohn Fastabend 		if (err)
343777162022SJohn Fastabend 			goto out;
343877162022SJohn Fastabend 		else
343977162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_MASTER;
344077162022SJohn Fastabend 	}
344177162022SJohn Fastabend 
344277162022SJohn Fastabend 	/* Embedded bridge, macvlan, and any other device support */
3443090096bfSVlad Yasevich 	if (ndm->ndm_flags & NTF_SELF) {
3444090096bfSVlad Yasevich 		if (dev->netdev_ops->ndo_fdb_del)
3445f6f6424bSJiri Pirko 			err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
3446f6f6424bSJiri Pirko 							   vid);
3447090096bfSVlad Yasevich 		else
3448f6f6424bSJiri Pirko 			err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
344977162022SJohn Fastabend 
34503ff661c3SJohn Fastabend 		if (!err) {
3451b3379041SHubert Sokolowski 			rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
3452b3379041SHubert Sokolowski 					ndm->ndm_state);
345377162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_SELF;
345477162022SJohn Fastabend 		}
34553ff661c3SJohn Fastabend 	}
345677162022SJohn Fastabend out:
345777162022SJohn Fastabend 	return err;
345877162022SJohn Fastabend }
345977162022SJohn Fastabend 
3460d83b0603SJohn Fastabend static int nlmsg_populate_fdb(struct sk_buff *skb,
3461d83b0603SJohn Fastabend 			      struct netlink_callback *cb,
3462d83b0603SJohn Fastabend 			      struct net_device *dev,
3463d83b0603SJohn Fastabend 			      int *idx,
3464d83b0603SJohn Fastabend 			      struct netdev_hw_addr_list *list)
3465d83b0603SJohn Fastabend {
3466d83b0603SJohn Fastabend 	struct netdev_hw_addr *ha;
3467d83b0603SJohn Fastabend 	int err;
346815e47304SEric W. Biederman 	u32 portid, seq;
3469d83b0603SJohn Fastabend 
347015e47304SEric W. Biederman 	portid = NETLINK_CB(cb->skb).portid;
3471d83b0603SJohn Fastabend 	seq = cb->nlh->nlmsg_seq;
3472d83b0603SJohn Fastabend 
3473d83b0603SJohn Fastabend 	list_for_each_entry(ha, &list->list, list) {
3474d297653dSRoopa Prabhu 		if (*idx < cb->args[2])
3475d83b0603SJohn Fastabend 			goto skip;
3476d83b0603SJohn Fastabend 
34771e53d5bbSHubert Sokolowski 		err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
3478a7a558feSJohn Fastabend 					      portid, seq,
34791c104a6bSNicolas Dichtel 					      RTM_NEWNEIGH, NTF_SELF,
3480b3379041SHubert Sokolowski 					      NLM_F_MULTI, NUD_PERMANENT);
3481d83b0603SJohn Fastabend 		if (err < 0)
3482d83b0603SJohn Fastabend 			return err;
3483d83b0603SJohn Fastabend skip:
3484d83b0603SJohn Fastabend 		*idx += 1;
3485d83b0603SJohn Fastabend 	}
3486d83b0603SJohn Fastabend 	return 0;
3487d83b0603SJohn Fastabend }
3488d83b0603SJohn Fastabend 
3489d83b0603SJohn Fastabend /**
34902c53040fSBen Hutchings  * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
3491d83b0603SJohn Fastabend  * @nlh: netlink message header
3492d83b0603SJohn Fastabend  * @dev: netdevice
3493d83b0603SJohn Fastabend  *
3494d83b0603SJohn Fastabend  * Default netdevice operation to dump the existing unicast address list.
349591f3e7b1SJohn Fastabend  * Returns number of addresses from list put in skb.
3496d83b0603SJohn Fastabend  */
3497d83b0603SJohn Fastabend int ndo_dflt_fdb_dump(struct sk_buff *skb,
3498d83b0603SJohn Fastabend 		      struct netlink_callback *cb,
3499d83b0603SJohn Fastabend 		      struct net_device *dev,
35005d5eacb3SJamal Hadi Salim 		      struct net_device *filter_dev,
3501d297653dSRoopa Prabhu 		      int *idx)
3502d83b0603SJohn Fastabend {
3503d83b0603SJohn Fastabend 	int err;
3504d83b0603SJohn Fastabend 
3505d83b0603SJohn Fastabend 	netif_addr_lock_bh(dev);
3506d297653dSRoopa Prabhu 	err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
3507d83b0603SJohn Fastabend 	if (err)
3508d83b0603SJohn Fastabend 		goto out;
35092934c9dbSZhang Shengju 	err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
3510d83b0603SJohn Fastabend out:
3511d83b0603SJohn Fastabend 	netif_addr_unlock_bh(dev);
3512d297653dSRoopa Prabhu 	return err;
3513d83b0603SJohn Fastabend }
3514d83b0603SJohn Fastabend EXPORT_SYMBOL(ndo_dflt_fdb_dump);
3515d83b0603SJohn Fastabend 
351677162022SJohn Fastabend static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
351777162022SJohn Fastabend {
351877162022SJohn Fastabend 	struct net_device *dev;
35195e6d2435SJamal Hadi Salim 	struct nlattr *tb[IFLA_MAX+1];
35205e6d2435SJamal Hadi Salim 	struct net_device *br_dev = NULL;
35215e6d2435SJamal Hadi Salim 	const struct net_device_ops *ops = NULL;
35225e6d2435SJamal Hadi Salim 	const struct net_device_ops *cops = NULL;
35235e6d2435SJamal Hadi Salim 	struct ifinfomsg *ifm = nlmsg_data(cb->nlh);
35245e6d2435SJamal Hadi Salim 	struct net *net = sock_net(skb->sk);
3525d297653dSRoopa Prabhu 	struct hlist_head *head;
35265e6d2435SJamal Hadi Salim 	int brport_idx = 0;
35275e6d2435SJamal Hadi Salim 	int br_idx = 0;
3528d297653dSRoopa Prabhu 	int h, s_h;
3529d297653dSRoopa Prabhu 	int idx = 0, s_idx;
3530d297653dSRoopa Prabhu 	int err = 0;
3531d297653dSRoopa Prabhu 	int fidx = 0;
353277162022SJohn Fastabend 
35330ff50e83SAlexander Potapenko 	err = nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb,
35340ff50e83SAlexander Potapenko 			  IFLA_MAX, ifla_policy, NULL);
35350ff50e83SAlexander Potapenko 	if (err < 0) {
35360ff50e83SAlexander Potapenko 		return -EINVAL;
35370ff50e83SAlexander Potapenko 	} else if (err == 0) {
35385e6d2435SJamal Hadi Salim 		if (tb[IFLA_MASTER])
35395e6d2435SJamal Hadi Salim 			br_idx = nla_get_u32(tb[IFLA_MASTER]);
35405e6d2435SJamal Hadi Salim 	}
354177162022SJohn Fastabend 
35425e6d2435SJamal Hadi Salim 	brport_idx = ifm->ifi_index;
35435e6d2435SJamal Hadi Salim 
35445e6d2435SJamal Hadi Salim 	if (br_idx) {
35455e6d2435SJamal Hadi Salim 		br_dev = __dev_get_by_index(net, br_idx);
35465e6d2435SJamal Hadi Salim 		if (!br_dev)
35475e6d2435SJamal Hadi Salim 			return -ENODEV;
35485e6d2435SJamal Hadi Salim 
3549898e5061SJiri Pirko 		ops = br_dev->netdev_ops;
35505e6d2435SJamal Hadi Salim 	}
35515e6d2435SJamal Hadi Salim 
3552d297653dSRoopa Prabhu 	s_h = cb->args[0];
3553d297653dSRoopa Prabhu 	s_idx = cb->args[1];
3554d297653dSRoopa Prabhu 
3555d297653dSRoopa Prabhu 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
3556d297653dSRoopa Prabhu 		idx = 0;
3557d297653dSRoopa Prabhu 		head = &net->dev_index_head[h];
3558d297653dSRoopa Prabhu 		hlist_for_each_entry(dev, head, index_hlist) {
3559d297653dSRoopa Prabhu 
35605e6d2435SJamal Hadi Salim 			if (brport_idx && (dev->ifindex != brport_idx))
35615e6d2435SJamal Hadi Salim 				continue;
35625e6d2435SJamal Hadi Salim 
35635e6d2435SJamal Hadi Salim 			if (!br_idx) { /* user did not specify a specific bridge */
35645e6d2435SJamal Hadi Salim 				if (dev->priv_flags & IFF_BRIDGE_PORT) {
35655e6d2435SJamal Hadi Salim 					br_dev = netdev_master_upper_dev_get(dev);
35665e6d2435SJamal Hadi Salim 					cops = br_dev->netdev_ops;
35675e6d2435SJamal Hadi Salim 				}
35685e6d2435SJamal Hadi Salim 			} else {
35695e6d2435SJamal Hadi Salim 				if (dev != br_dev &&
35705e6d2435SJamal Hadi Salim 				    !(dev->priv_flags & IFF_BRIDGE_PORT))
35715e6d2435SJamal Hadi Salim 					continue;
35725e6d2435SJamal Hadi Salim 
35735e6d2435SJamal Hadi Salim 				if (br_dev != netdev_master_upper_dev_get(dev) &&
35745e6d2435SJamal Hadi Salim 				    !(dev->priv_flags & IFF_EBRIDGE))
35755e6d2435SJamal Hadi Salim 					continue;
35765e6d2435SJamal Hadi Salim 				cops = ops;
35775e6d2435SJamal Hadi Salim 			}
35785e6d2435SJamal Hadi Salim 
3579d297653dSRoopa Prabhu 			if (idx < s_idx)
3580d297653dSRoopa Prabhu 				goto cont;
3581d297653dSRoopa Prabhu 
35825e6d2435SJamal Hadi Salim 			if (dev->priv_flags & IFF_BRIDGE_PORT) {
3583d297653dSRoopa Prabhu 				if (cops && cops->ndo_fdb_dump) {
3584d297653dSRoopa Prabhu 					err = cops->ndo_fdb_dump(skb, cb,
3585d297653dSRoopa Prabhu 								br_dev, dev,
3586d297653dSRoopa Prabhu 								&fidx);
3587d297653dSRoopa Prabhu 					if (err == -EMSGSIZE)
3588d297653dSRoopa Prabhu 						goto out;
358977162022SJohn Fastabend 				}
3590d297653dSRoopa Prabhu 			}
359177162022SJohn Fastabend 
35925e6d2435SJamal Hadi Salim 			if (dev->netdev_ops->ndo_fdb_dump)
3593d297653dSRoopa Prabhu 				err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
3594d297653dSRoopa Prabhu 								    dev, NULL,
3595d297653dSRoopa Prabhu 								    &fidx);
35966cb69742SHubert Sokolowski 			else
3597d297653dSRoopa Prabhu 				err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
3598d297653dSRoopa Prabhu 							&fidx);
3599d297653dSRoopa Prabhu 			if (err == -EMSGSIZE)
3600d297653dSRoopa Prabhu 				goto out;
36015e6d2435SJamal Hadi Salim 
36025e6d2435SJamal Hadi Salim 			cops = NULL;
3603d297653dSRoopa Prabhu 
3604d297653dSRoopa Prabhu 			/* reset fdb offset to 0 for rest of the interfaces */
3605d297653dSRoopa Prabhu 			cb->args[2] = 0;
3606d297653dSRoopa Prabhu 			fidx = 0;
3607d297653dSRoopa Prabhu cont:
3608d297653dSRoopa Prabhu 			idx++;
3609d297653dSRoopa Prabhu 		}
361077162022SJohn Fastabend 	}
361177162022SJohn Fastabend 
3612d297653dSRoopa Prabhu out:
3613d297653dSRoopa Prabhu 	cb->args[0] = h;
3614d297653dSRoopa Prabhu 	cb->args[1] = idx;
3615d297653dSRoopa Prabhu 	cb->args[2] = fidx;
3616d297653dSRoopa Prabhu 
361777162022SJohn Fastabend 	return skb->len;
361877162022SJohn Fastabend }
361977162022SJohn Fastabend 
36202c3c031cSScott Feldman static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
36212c3c031cSScott Feldman 			       unsigned int attrnum, unsigned int flag)
36222c3c031cSScott Feldman {
36232c3c031cSScott Feldman 	if (mask & flag)
36242c3c031cSScott Feldman 		return nla_put_u8(skb, attrnum, !!(flags & flag));
36252c3c031cSScott Feldman 	return 0;
36262c3c031cSScott Feldman }
36272c3c031cSScott Feldman 
3628815cccbfSJohn Fastabend int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
36292c3c031cSScott Feldman 			    struct net_device *dev, u16 mode,
36307d4f8d87SScott Feldman 			    u32 flags, u32 mask, int nlflags,
36317d4f8d87SScott Feldman 			    u32 filter_mask,
36327d4f8d87SScott Feldman 			    int (*vlan_fill)(struct sk_buff *skb,
36337d4f8d87SScott Feldman 					     struct net_device *dev,
36347d4f8d87SScott Feldman 					     u32 filter_mask))
3635815cccbfSJohn Fastabend {
3636815cccbfSJohn Fastabend 	struct nlmsghdr *nlh;
3637815cccbfSJohn Fastabend 	struct ifinfomsg *ifm;
3638815cccbfSJohn Fastabend 	struct nlattr *br_afspec;
36392c3c031cSScott Feldman 	struct nlattr *protinfo;
3640815cccbfSJohn Fastabend 	u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
3641898e5061SJiri Pirko 	struct net_device *br_dev = netdev_master_upper_dev_get(dev);
36427d4f8d87SScott Feldman 	int err = 0;
3643815cccbfSJohn Fastabend 
364446c264daSNicolas Dichtel 	nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
3645815cccbfSJohn Fastabend 	if (nlh == NULL)
3646815cccbfSJohn Fastabend 		return -EMSGSIZE;
3647815cccbfSJohn Fastabend 
3648815cccbfSJohn Fastabend 	ifm = nlmsg_data(nlh);
3649815cccbfSJohn Fastabend 	ifm->ifi_family = AF_BRIDGE;
3650815cccbfSJohn Fastabend 	ifm->__ifi_pad = 0;
3651815cccbfSJohn Fastabend 	ifm->ifi_type = dev->type;
3652815cccbfSJohn Fastabend 	ifm->ifi_index = dev->ifindex;
3653815cccbfSJohn Fastabend 	ifm->ifi_flags = dev_get_flags(dev);
3654815cccbfSJohn Fastabend 	ifm->ifi_change = 0;
3655815cccbfSJohn Fastabend 
3656815cccbfSJohn Fastabend 
3657815cccbfSJohn Fastabend 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
3658815cccbfSJohn Fastabend 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
3659815cccbfSJohn Fastabend 	    nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
3660898e5061SJiri Pirko 	    (br_dev &&
3661898e5061SJiri Pirko 	     nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
3662815cccbfSJohn Fastabend 	    (dev->addr_len &&
3663815cccbfSJohn Fastabend 	     nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
3664a54acb3aSNicolas Dichtel 	    (dev->ifindex != dev_get_iflink(dev) &&
3665a54acb3aSNicolas Dichtel 	     nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
3666815cccbfSJohn Fastabend 		goto nla_put_failure;
3667815cccbfSJohn Fastabend 
3668815cccbfSJohn Fastabend 	br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
3669815cccbfSJohn Fastabend 	if (!br_afspec)
3670815cccbfSJohn Fastabend 		goto nla_put_failure;
3671815cccbfSJohn Fastabend 
36721d460b98SRoopa Prabhu 	if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
3673815cccbfSJohn Fastabend 		nla_nest_cancel(skb, br_afspec);
3674815cccbfSJohn Fastabend 		goto nla_put_failure;
3675815cccbfSJohn Fastabend 	}
36761d460b98SRoopa Prabhu 
36771d460b98SRoopa Prabhu 	if (mode != BRIDGE_MODE_UNDEF) {
36781d460b98SRoopa Prabhu 		if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
36791d460b98SRoopa Prabhu 			nla_nest_cancel(skb, br_afspec);
36801d460b98SRoopa Prabhu 			goto nla_put_failure;
36811d460b98SRoopa Prabhu 		}
36821d460b98SRoopa Prabhu 	}
36837d4f8d87SScott Feldman 	if (vlan_fill) {
36847d4f8d87SScott Feldman 		err = vlan_fill(skb, dev, filter_mask);
36857d4f8d87SScott Feldman 		if (err) {
36867d4f8d87SScott Feldman 			nla_nest_cancel(skb, br_afspec);
36877d4f8d87SScott Feldman 			goto nla_put_failure;
36887d4f8d87SScott Feldman 		}
36897d4f8d87SScott Feldman 	}
3690815cccbfSJohn Fastabend 	nla_nest_end(skb, br_afspec);
3691815cccbfSJohn Fastabend 
36922c3c031cSScott Feldman 	protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
36932c3c031cSScott Feldman 	if (!protinfo)
36942c3c031cSScott Feldman 		goto nla_put_failure;
36952c3c031cSScott Feldman 
36962c3c031cSScott Feldman 	if (brport_nla_put_flag(skb, flags, mask,
36972c3c031cSScott Feldman 				IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
36982c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
36992c3c031cSScott Feldman 				IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
37002c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
37012c3c031cSScott Feldman 				IFLA_BRPORT_FAST_LEAVE,
37022c3c031cSScott Feldman 				BR_MULTICAST_FAST_LEAVE) ||
37032c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
37042c3c031cSScott Feldman 				IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
37052c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
37062c3c031cSScott Feldman 				IFLA_BRPORT_LEARNING, BR_LEARNING) ||
37072c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
37082c3c031cSScott Feldman 				IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
37092c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
37102c3c031cSScott Feldman 				IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
37112c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
37122c3c031cSScott Feldman 				IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
37132c3c031cSScott Feldman 		nla_nest_cancel(skb, protinfo);
37142c3c031cSScott Feldman 		goto nla_put_failure;
37152c3c031cSScott Feldman 	}
37162c3c031cSScott Feldman 
37172c3c031cSScott Feldman 	nla_nest_end(skb, protinfo);
37182c3c031cSScott Feldman 
3719053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
3720053c095aSJohannes Berg 	return 0;
3721815cccbfSJohn Fastabend nla_put_failure:
3722815cccbfSJohn Fastabend 	nlmsg_cancel(skb, nlh);
37237d4f8d87SScott Feldman 	return err ? err : -EMSGSIZE;
3724815cccbfSJohn Fastabend }
37257d4f8d87SScott Feldman EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
3726815cccbfSJohn Fastabend 
3727e5a55a89SJohn Fastabend static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
3728e5a55a89SJohn Fastabend {
3729e5a55a89SJohn Fastabend 	struct net *net = sock_net(skb->sk);
3730e5a55a89SJohn Fastabend 	struct net_device *dev;
3731e5a55a89SJohn Fastabend 	int idx = 0;
3732e5a55a89SJohn Fastabend 	u32 portid = NETLINK_CB(cb->skb).portid;
3733e5a55a89SJohn Fastabend 	u32 seq = cb->nlh->nlmsg_seq;
37346cbdceebSVlad Yasevich 	u32 filter_mask = 0;
3735d64f69b0SRoopa Prabhu 	int err;
37366cbdceebSVlad Yasevich 
3737aa68c20fSThomas Graf 	if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) {
3738aa68c20fSThomas Graf 		struct nlattr *extfilt;
3739aa68c20fSThomas Graf 
37403e805ad2SAsbjoern Sloth Toennesen 		extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
37416cbdceebSVlad Yasevich 					  IFLA_EXT_MASK);
3742aa68c20fSThomas Graf 		if (extfilt) {
3743aa68c20fSThomas Graf 			if (nla_len(extfilt) < sizeof(filter_mask))
3744aa68c20fSThomas Graf 				return -EINVAL;
3745aa68c20fSThomas Graf 
37466cbdceebSVlad Yasevich 			filter_mask = nla_get_u32(extfilt);
3747aa68c20fSThomas Graf 		}
3748aa68c20fSThomas Graf 	}
3749e5a55a89SJohn Fastabend 
3750e5a55a89SJohn Fastabend 	rcu_read_lock();
3751e5a55a89SJohn Fastabend 	for_each_netdev_rcu(net, dev) {
3752e5a55a89SJohn Fastabend 		const struct net_device_ops *ops = dev->netdev_ops;
3753898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3754e5a55a89SJohn Fastabend 
3755898e5061SJiri Pirko 		if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
3756d64f69b0SRoopa Prabhu 			if (idx >= cb->args[0]) {
3757d64f69b0SRoopa Prabhu 				err = br_dev->netdev_ops->ndo_bridge_getlink(
3758d64f69b0SRoopa Prabhu 						skb, portid, seq, dev,
3759d64f69b0SRoopa Prabhu 						filter_mask, NLM_F_MULTI);
3760f6c5775fSDavid Ahern 				if (err < 0 && err != -EOPNOTSUPP) {
3761f6c5775fSDavid Ahern 					if (likely(skb->len))
3762e5a55a89SJohn Fastabend 						break;
3763f6c5775fSDavid Ahern 
3764f6c5775fSDavid Ahern 					goto out_err;
3765f6c5775fSDavid Ahern 				}
3766d64f69b0SRoopa Prabhu 			}
3767e5a55a89SJohn Fastabend 			idx++;
3768e5a55a89SJohn Fastabend 		}
3769e5a55a89SJohn Fastabend 
3770e5a55a89SJohn Fastabend 		if (ops->ndo_bridge_getlink) {
3771d64f69b0SRoopa Prabhu 			if (idx >= cb->args[0]) {
3772d64f69b0SRoopa Prabhu 				err = ops->ndo_bridge_getlink(skb, portid,
3773d64f69b0SRoopa Prabhu 							      seq, dev,
377446c264daSNicolas Dichtel 							      filter_mask,
3775d64f69b0SRoopa Prabhu 							      NLM_F_MULTI);
3776f6c5775fSDavid Ahern 				if (err < 0 && err != -EOPNOTSUPP) {
3777f6c5775fSDavid Ahern 					if (likely(skb->len))
3778e5a55a89SJohn Fastabend 						break;
3779f6c5775fSDavid Ahern 
3780f6c5775fSDavid Ahern 					goto out_err;
3781f6c5775fSDavid Ahern 				}
3782d64f69b0SRoopa Prabhu 			}
3783e5a55a89SJohn Fastabend 			idx++;
3784e5a55a89SJohn Fastabend 		}
3785e5a55a89SJohn Fastabend 	}
3786f6c5775fSDavid Ahern 	err = skb->len;
3787f6c5775fSDavid Ahern out_err:
3788e5a55a89SJohn Fastabend 	rcu_read_unlock();
3789e5a55a89SJohn Fastabend 	cb->args[0] = idx;
3790e5a55a89SJohn Fastabend 
3791f6c5775fSDavid Ahern 	return err;
3792e5a55a89SJohn Fastabend }
3793e5a55a89SJohn Fastabend 
37942469ffd7SJohn Fastabend static inline size_t bridge_nlmsg_size(void)
37952469ffd7SJohn Fastabend {
37962469ffd7SJohn Fastabend 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
37972469ffd7SJohn Fastabend 		+ nla_total_size(IFNAMSIZ)	/* IFLA_IFNAME */
37982469ffd7SJohn Fastabend 		+ nla_total_size(MAX_ADDR_LEN)	/* IFLA_ADDRESS */
37992469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_MASTER */
38002469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_MTU */
38012469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_LINK */
38022469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_OPERSTATE */
38032469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u8))	/* IFLA_PROTINFO */
38042469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(struct nlattr))	/* IFLA_AF_SPEC */
38052469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u16))	/* IFLA_BRIDGE_FLAGS */
38062469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u16));	/* IFLA_BRIDGE_MODE */
38072469ffd7SJohn Fastabend }
38082469ffd7SJohn Fastabend 
380902dba438SRoopa Prabhu static int rtnl_bridge_notify(struct net_device *dev)
38102469ffd7SJohn Fastabend {
38112469ffd7SJohn Fastabend 	struct net *net = dev_net(dev);
38122469ffd7SJohn Fastabend 	struct sk_buff *skb;
38132469ffd7SJohn Fastabend 	int err = -EOPNOTSUPP;
38142469ffd7SJohn Fastabend 
381502dba438SRoopa Prabhu 	if (!dev->netdev_ops->ndo_bridge_getlink)
381602dba438SRoopa Prabhu 		return 0;
381702dba438SRoopa Prabhu 
38182469ffd7SJohn Fastabend 	skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
38192469ffd7SJohn Fastabend 	if (!skb) {
38202469ffd7SJohn Fastabend 		err = -ENOMEM;
38212469ffd7SJohn Fastabend 		goto errout;
38222469ffd7SJohn Fastabend 	}
38232469ffd7SJohn Fastabend 
382446c264daSNicolas Dichtel 	err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
3825c38e01b8SJohn Fastabend 	if (err < 0)
3826c38e01b8SJohn Fastabend 		goto errout;
38272469ffd7SJohn Fastabend 
382859ccaaaaSRoopa Prabhu 	if (!skb->len)
382959ccaaaaSRoopa Prabhu 		goto errout;
383059ccaaaaSRoopa Prabhu 
38312469ffd7SJohn Fastabend 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
38322469ffd7SJohn Fastabend 	return 0;
38332469ffd7SJohn Fastabend errout:
38342469ffd7SJohn Fastabend 	WARN_ON(err == -EMSGSIZE);
38352469ffd7SJohn Fastabend 	kfree_skb(skb);
383659ccaaaaSRoopa Prabhu 	if (err)
38372469ffd7SJohn Fastabend 		rtnl_set_sk_err(net, RTNLGRP_LINK, err);
38382469ffd7SJohn Fastabend 	return err;
38392469ffd7SJohn Fastabend }
38402469ffd7SJohn Fastabend 
3841c21ef3e3SDavid Ahern static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3842c21ef3e3SDavid Ahern 			       struct netlink_ext_ack *extack)
3843e5a55a89SJohn Fastabend {
3844e5a55a89SJohn Fastabend 	struct net *net = sock_net(skb->sk);
3845e5a55a89SJohn Fastabend 	struct ifinfomsg *ifm;
3846e5a55a89SJohn Fastabend 	struct net_device *dev;
38472469ffd7SJohn Fastabend 	struct nlattr *br_spec, *attr = NULL;
38482469ffd7SJohn Fastabend 	int rem, err = -EOPNOTSUPP;
38494de8b413SRosen, Rami 	u16 flags = 0;
3850c38e01b8SJohn Fastabend 	bool have_flags = false;
3851e5a55a89SJohn Fastabend 
3852e5a55a89SJohn Fastabend 	if (nlmsg_len(nlh) < sizeof(*ifm))
3853e5a55a89SJohn Fastabend 		return -EINVAL;
3854e5a55a89SJohn Fastabend 
3855e5a55a89SJohn Fastabend 	ifm = nlmsg_data(nlh);
3856e5a55a89SJohn Fastabend 	if (ifm->ifi_family != AF_BRIDGE)
3857e5a55a89SJohn Fastabend 		return -EPFNOSUPPORT;
3858e5a55a89SJohn Fastabend 
3859e5a55a89SJohn Fastabend 	dev = __dev_get_by_index(net, ifm->ifi_index);
3860e5a55a89SJohn Fastabend 	if (!dev) {
3861b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "unknown ifindex");
3862e5a55a89SJohn Fastabend 		return -ENODEV;
3863e5a55a89SJohn Fastabend 	}
3864e5a55a89SJohn Fastabend 
38652469ffd7SJohn Fastabend 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
38662469ffd7SJohn Fastabend 	if (br_spec) {
38672469ffd7SJohn Fastabend 		nla_for_each_nested(attr, br_spec, rem) {
38682469ffd7SJohn Fastabend 			if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
38696e8d1c55SThomas Graf 				if (nla_len(attr) < sizeof(flags))
38706e8d1c55SThomas Graf 					return -EINVAL;
38716e8d1c55SThomas Graf 
3872c38e01b8SJohn Fastabend 				have_flags = true;
38732469ffd7SJohn Fastabend 				flags = nla_get_u16(attr);
38742469ffd7SJohn Fastabend 				break;
38752469ffd7SJohn Fastabend 			}
38762469ffd7SJohn Fastabend 		}
38772469ffd7SJohn Fastabend 	}
38782469ffd7SJohn Fastabend 
38792469ffd7SJohn Fastabend 	if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
3880898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3881898e5061SJiri Pirko 
3882898e5061SJiri Pirko 		if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
38832469ffd7SJohn Fastabend 			err = -EOPNOTSUPP;
3884e5a55a89SJohn Fastabend 			goto out;
3885e5a55a89SJohn Fastabend 		}
3886e5a55a89SJohn Fastabend 
3887add511b3SRoopa Prabhu 		err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags);
38882469ffd7SJohn Fastabend 		if (err)
38892469ffd7SJohn Fastabend 			goto out;
38902469ffd7SJohn Fastabend 
38912469ffd7SJohn Fastabend 		flags &= ~BRIDGE_FLAGS_MASTER;
38922469ffd7SJohn Fastabend 	}
38932469ffd7SJohn Fastabend 
38942469ffd7SJohn Fastabend 	if ((flags & BRIDGE_FLAGS_SELF)) {
38952469ffd7SJohn Fastabend 		if (!dev->netdev_ops->ndo_bridge_setlink)
38962469ffd7SJohn Fastabend 			err = -EOPNOTSUPP;
38972469ffd7SJohn Fastabend 		else
3898add511b3SRoopa Prabhu 			err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
3899add511b3SRoopa Prabhu 								  flags);
390002dba438SRoopa Prabhu 		if (!err) {
39012469ffd7SJohn Fastabend 			flags &= ~BRIDGE_FLAGS_SELF;
390202dba438SRoopa Prabhu 
390302dba438SRoopa Prabhu 			/* Generate event to notify upper layer of bridge
390402dba438SRoopa Prabhu 			 * change
390502dba438SRoopa Prabhu 			 */
390602dba438SRoopa Prabhu 			err = rtnl_bridge_notify(dev);
390702dba438SRoopa Prabhu 		}
39082469ffd7SJohn Fastabend 	}
39092469ffd7SJohn Fastabend 
3910c38e01b8SJohn Fastabend 	if (have_flags)
39112469ffd7SJohn Fastabend 		memcpy(nla_data(attr), &flags, sizeof(flags));
3912e5a55a89SJohn Fastabend out:
3913e5a55a89SJohn Fastabend 	return err;
3914e5a55a89SJohn Fastabend }
3915e5a55a89SJohn Fastabend 
3916c21ef3e3SDavid Ahern static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
3917c21ef3e3SDavid Ahern 			       struct netlink_ext_ack *extack)
3918407af329SVlad Yasevich {
3919407af329SVlad Yasevich 	struct net *net = sock_net(skb->sk);
3920407af329SVlad Yasevich 	struct ifinfomsg *ifm;
3921407af329SVlad Yasevich 	struct net_device *dev;
3922407af329SVlad Yasevich 	struct nlattr *br_spec, *attr = NULL;
3923407af329SVlad Yasevich 	int rem, err = -EOPNOTSUPP;
39244de8b413SRosen, Rami 	u16 flags = 0;
3925407af329SVlad Yasevich 	bool have_flags = false;
3926407af329SVlad Yasevich 
3927407af329SVlad Yasevich 	if (nlmsg_len(nlh) < sizeof(*ifm))
3928407af329SVlad Yasevich 		return -EINVAL;
3929407af329SVlad Yasevich 
3930407af329SVlad Yasevich 	ifm = nlmsg_data(nlh);
3931407af329SVlad Yasevich 	if (ifm->ifi_family != AF_BRIDGE)
3932407af329SVlad Yasevich 		return -EPFNOSUPPORT;
3933407af329SVlad Yasevich 
3934407af329SVlad Yasevich 	dev = __dev_get_by_index(net, ifm->ifi_index);
3935407af329SVlad Yasevich 	if (!dev) {
3936b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "unknown ifindex");
3937407af329SVlad Yasevich 		return -ENODEV;
3938407af329SVlad Yasevich 	}
3939407af329SVlad Yasevich 
3940407af329SVlad Yasevich 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3941407af329SVlad Yasevich 	if (br_spec) {
3942407af329SVlad Yasevich 		nla_for_each_nested(attr, br_spec, rem) {
3943407af329SVlad Yasevich 			if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
39446e8d1c55SThomas Graf 				if (nla_len(attr) < sizeof(flags))
39456e8d1c55SThomas Graf 					return -EINVAL;
39466e8d1c55SThomas Graf 
3947407af329SVlad Yasevich 				have_flags = true;
3948407af329SVlad Yasevich 				flags = nla_get_u16(attr);
3949407af329SVlad Yasevich 				break;
3950407af329SVlad Yasevich 			}
3951407af329SVlad Yasevich 		}
3952407af329SVlad Yasevich 	}
3953407af329SVlad Yasevich 
3954407af329SVlad Yasevich 	if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
3955407af329SVlad Yasevich 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3956407af329SVlad Yasevich 
3957407af329SVlad Yasevich 		if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
3958407af329SVlad Yasevich 			err = -EOPNOTSUPP;
3959407af329SVlad Yasevich 			goto out;
3960407af329SVlad Yasevich 		}
3961407af329SVlad Yasevich 
3962add511b3SRoopa Prabhu 		err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
3963407af329SVlad Yasevich 		if (err)
3964407af329SVlad Yasevich 			goto out;
3965407af329SVlad Yasevich 
3966407af329SVlad Yasevich 		flags &= ~BRIDGE_FLAGS_MASTER;
3967407af329SVlad Yasevich 	}
3968407af329SVlad Yasevich 
3969407af329SVlad Yasevich 	if ((flags & BRIDGE_FLAGS_SELF)) {
3970407af329SVlad Yasevich 		if (!dev->netdev_ops->ndo_bridge_dellink)
3971407af329SVlad Yasevich 			err = -EOPNOTSUPP;
3972407af329SVlad Yasevich 		else
3973add511b3SRoopa Prabhu 			err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
3974add511b3SRoopa Prabhu 								  flags);
3975407af329SVlad Yasevich 
397602dba438SRoopa Prabhu 		if (!err) {
3977407af329SVlad Yasevich 			flags &= ~BRIDGE_FLAGS_SELF;
397802dba438SRoopa Prabhu 
397902dba438SRoopa Prabhu 			/* Generate event to notify upper layer of bridge
398002dba438SRoopa Prabhu 			 * change
398102dba438SRoopa Prabhu 			 */
398202dba438SRoopa Prabhu 			err = rtnl_bridge_notify(dev);
398302dba438SRoopa Prabhu 		}
3984407af329SVlad Yasevich 	}
3985407af329SVlad Yasevich 
3986407af329SVlad Yasevich 	if (have_flags)
3987407af329SVlad Yasevich 		memcpy(nla_data(attr), &flags, sizeof(flags));
3988407af329SVlad Yasevich out:
3989407af329SVlad Yasevich 	return err;
3990407af329SVlad Yasevich }
3991407af329SVlad Yasevich 
3992e8872a25SNikolay Aleksandrov static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
3993e8872a25SNikolay Aleksandrov {
3994e8872a25SNikolay Aleksandrov 	return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
3995e8872a25SNikolay Aleksandrov 	       (!idxattr || idxattr == attrid);
3996e8872a25SNikolay Aleksandrov }
3997e8872a25SNikolay Aleksandrov 
399869ae6ad2SNogah Frankel #define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
399969ae6ad2SNogah Frankel static int rtnl_get_offload_stats_attr_size(int attr_id)
400069ae6ad2SNogah Frankel {
400169ae6ad2SNogah Frankel 	switch (attr_id) {
400269ae6ad2SNogah Frankel 	case IFLA_OFFLOAD_XSTATS_CPU_HIT:
400369ae6ad2SNogah Frankel 		return sizeof(struct rtnl_link_stats64);
400469ae6ad2SNogah Frankel 	}
400569ae6ad2SNogah Frankel 
400669ae6ad2SNogah Frankel 	return 0;
400769ae6ad2SNogah Frankel }
400869ae6ad2SNogah Frankel 
400969ae6ad2SNogah Frankel static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev,
401069ae6ad2SNogah Frankel 				  int *prividx)
401169ae6ad2SNogah Frankel {
401269ae6ad2SNogah Frankel 	struct nlattr *attr = NULL;
401369ae6ad2SNogah Frankel 	int attr_id, size;
401469ae6ad2SNogah Frankel 	void *attr_data;
401569ae6ad2SNogah Frankel 	int err;
401669ae6ad2SNogah Frankel 
401769ae6ad2SNogah Frankel 	if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
401869ae6ad2SNogah Frankel 	      dev->netdev_ops->ndo_get_offload_stats))
401969ae6ad2SNogah Frankel 		return -ENODATA;
402069ae6ad2SNogah Frankel 
402169ae6ad2SNogah Frankel 	for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
402269ae6ad2SNogah Frankel 	     attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
402369ae6ad2SNogah Frankel 		if (attr_id < *prividx)
402469ae6ad2SNogah Frankel 			continue;
402569ae6ad2SNogah Frankel 
402669ae6ad2SNogah Frankel 		size = rtnl_get_offload_stats_attr_size(attr_id);
402769ae6ad2SNogah Frankel 		if (!size)
402869ae6ad2SNogah Frankel 			continue;
402969ae6ad2SNogah Frankel 
40303df5b3c6SOr Gerlitz 		if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
403169ae6ad2SNogah Frankel 			continue;
403269ae6ad2SNogah Frankel 
403369ae6ad2SNogah Frankel 		attr = nla_reserve_64bit(skb, attr_id, size,
403469ae6ad2SNogah Frankel 					 IFLA_OFFLOAD_XSTATS_UNSPEC);
403569ae6ad2SNogah Frankel 		if (!attr)
403669ae6ad2SNogah Frankel 			goto nla_put_failure;
403769ae6ad2SNogah Frankel 
403869ae6ad2SNogah Frankel 		attr_data = nla_data(attr);
403969ae6ad2SNogah Frankel 		memset(attr_data, 0, size);
404069ae6ad2SNogah Frankel 		err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev,
404169ae6ad2SNogah Frankel 							     attr_data);
404269ae6ad2SNogah Frankel 		if (err)
404369ae6ad2SNogah Frankel 			goto get_offload_stats_failure;
404469ae6ad2SNogah Frankel 	}
404569ae6ad2SNogah Frankel 
404669ae6ad2SNogah Frankel 	if (!attr)
404769ae6ad2SNogah Frankel 		return -ENODATA;
404869ae6ad2SNogah Frankel 
404969ae6ad2SNogah Frankel 	*prividx = 0;
405069ae6ad2SNogah Frankel 	return 0;
405169ae6ad2SNogah Frankel 
405269ae6ad2SNogah Frankel nla_put_failure:
405369ae6ad2SNogah Frankel 	err = -EMSGSIZE;
405469ae6ad2SNogah Frankel get_offload_stats_failure:
405569ae6ad2SNogah Frankel 	*prividx = attr_id;
405669ae6ad2SNogah Frankel 	return err;
405769ae6ad2SNogah Frankel }
405869ae6ad2SNogah Frankel 
405969ae6ad2SNogah Frankel static int rtnl_get_offload_stats_size(const struct net_device *dev)
406069ae6ad2SNogah Frankel {
406169ae6ad2SNogah Frankel 	int nla_size = 0;
406269ae6ad2SNogah Frankel 	int attr_id;
406369ae6ad2SNogah Frankel 	int size;
406469ae6ad2SNogah Frankel 
406569ae6ad2SNogah Frankel 	if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
406669ae6ad2SNogah Frankel 	      dev->netdev_ops->ndo_get_offload_stats))
406769ae6ad2SNogah Frankel 		return 0;
406869ae6ad2SNogah Frankel 
406969ae6ad2SNogah Frankel 	for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
407069ae6ad2SNogah Frankel 	     attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
40713df5b3c6SOr Gerlitz 		if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
407269ae6ad2SNogah Frankel 			continue;
407369ae6ad2SNogah Frankel 		size = rtnl_get_offload_stats_attr_size(attr_id);
407469ae6ad2SNogah Frankel 		nla_size += nla_total_size_64bit(size);
407569ae6ad2SNogah Frankel 	}
407669ae6ad2SNogah Frankel 
407769ae6ad2SNogah Frankel 	if (nla_size != 0)
407869ae6ad2SNogah Frankel 		nla_size += nla_total_size(0);
407969ae6ad2SNogah Frankel 
408069ae6ad2SNogah Frankel 	return nla_size;
408169ae6ad2SNogah Frankel }
408269ae6ad2SNogah Frankel 
408310c9ead9SRoopa Prabhu static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
408410c9ead9SRoopa Prabhu 			       int type, u32 pid, u32 seq, u32 change,
4085e8872a25SNikolay Aleksandrov 			       unsigned int flags, unsigned int filter_mask,
4086e8872a25SNikolay Aleksandrov 			       int *idxattr, int *prividx)
408710c9ead9SRoopa Prabhu {
408810c9ead9SRoopa Prabhu 	struct if_stats_msg *ifsm;
408910c9ead9SRoopa Prabhu 	struct nlmsghdr *nlh;
409010c9ead9SRoopa Prabhu 	struct nlattr *attr;
4091e8872a25SNikolay Aleksandrov 	int s_prividx = *prividx;
409269ae6ad2SNogah Frankel 	int err;
409310c9ead9SRoopa Prabhu 
409410c9ead9SRoopa Prabhu 	ASSERT_RTNL();
409510c9ead9SRoopa Prabhu 
409610c9ead9SRoopa Prabhu 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
409710c9ead9SRoopa Prabhu 	if (!nlh)
409810c9ead9SRoopa Prabhu 		return -EMSGSIZE;
409910c9ead9SRoopa Prabhu 
410010c9ead9SRoopa Prabhu 	ifsm = nlmsg_data(nlh);
4101ce024f42SNikolay Aleksandrov 	ifsm->family = PF_UNSPEC;
4102ce024f42SNikolay Aleksandrov 	ifsm->pad1 = 0;
4103ce024f42SNikolay Aleksandrov 	ifsm->pad2 = 0;
410410c9ead9SRoopa Prabhu 	ifsm->ifindex = dev->ifindex;
410510c9ead9SRoopa Prabhu 	ifsm->filter_mask = filter_mask;
410610c9ead9SRoopa Prabhu 
4107e8872a25SNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
410810c9ead9SRoopa Prabhu 		struct rtnl_link_stats64 *sp;
410910c9ead9SRoopa Prabhu 
411058414d32SNicolas Dichtel 		attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
411158414d32SNicolas Dichtel 					 sizeof(struct rtnl_link_stats64),
411258414d32SNicolas Dichtel 					 IFLA_STATS_UNSPEC);
411310c9ead9SRoopa Prabhu 		if (!attr)
411410c9ead9SRoopa Prabhu 			goto nla_put_failure;
411510c9ead9SRoopa Prabhu 
411610c9ead9SRoopa Prabhu 		sp = nla_data(attr);
411710c9ead9SRoopa Prabhu 		dev_get_stats(dev, sp);
411810c9ead9SRoopa Prabhu 	}
411910c9ead9SRoopa Prabhu 
412097a47facSNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
412197a47facSNikolay Aleksandrov 		const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
412297a47facSNikolay Aleksandrov 
412397a47facSNikolay Aleksandrov 		if (ops && ops->fill_linkxstats) {
412497a47facSNikolay Aleksandrov 			*idxattr = IFLA_STATS_LINK_XSTATS;
412597a47facSNikolay Aleksandrov 			attr = nla_nest_start(skb,
412697a47facSNikolay Aleksandrov 					      IFLA_STATS_LINK_XSTATS);
412797a47facSNikolay Aleksandrov 			if (!attr)
412897a47facSNikolay Aleksandrov 				goto nla_put_failure;
412997a47facSNikolay Aleksandrov 
413080e73cc5SNikolay Aleksandrov 			err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
413180e73cc5SNikolay Aleksandrov 			nla_nest_end(skb, attr);
413280e73cc5SNikolay Aleksandrov 			if (err)
413380e73cc5SNikolay Aleksandrov 				goto nla_put_failure;
413480e73cc5SNikolay Aleksandrov 			*idxattr = 0;
413580e73cc5SNikolay Aleksandrov 		}
413680e73cc5SNikolay Aleksandrov 	}
413780e73cc5SNikolay Aleksandrov 
413880e73cc5SNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
413980e73cc5SNikolay Aleksandrov 			     *idxattr)) {
414080e73cc5SNikolay Aleksandrov 		const struct rtnl_link_ops *ops = NULL;
414180e73cc5SNikolay Aleksandrov 		const struct net_device *master;
414280e73cc5SNikolay Aleksandrov 
414380e73cc5SNikolay Aleksandrov 		master = netdev_master_upper_dev_get(dev);
414480e73cc5SNikolay Aleksandrov 		if (master)
414580e73cc5SNikolay Aleksandrov 			ops = master->rtnl_link_ops;
414680e73cc5SNikolay Aleksandrov 		if (ops && ops->fill_linkxstats) {
414780e73cc5SNikolay Aleksandrov 			*idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
414880e73cc5SNikolay Aleksandrov 			attr = nla_nest_start(skb,
414980e73cc5SNikolay Aleksandrov 					      IFLA_STATS_LINK_XSTATS_SLAVE);
415080e73cc5SNikolay Aleksandrov 			if (!attr)
415180e73cc5SNikolay Aleksandrov 				goto nla_put_failure;
415280e73cc5SNikolay Aleksandrov 
415380e73cc5SNikolay Aleksandrov 			err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
415497a47facSNikolay Aleksandrov 			nla_nest_end(skb, attr);
415597a47facSNikolay Aleksandrov 			if (err)
415697a47facSNikolay Aleksandrov 				goto nla_put_failure;
415797a47facSNikolay Aleksandrov 			*idxattr = 0;
415897a47facSNikolay Aleksandrov 		}
415997a47facSNikolay Aleksandrov 	}
416097a47facSNikolay Aleksandrov 
416169ae6ad2SNogah Frankel 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
416269ae6ad2SNogah Frankel 			     *idxattr)) {
416369ae6ad2SNogah Frankel 		*idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
416469ae6ad2SNogah Frankel 		attr = nla_nest_start(skb, IFLA_STATS_LINK_OFFLOAD_XSTATS);
416569ae6ad2SNogah Frankel 		if (!attr)
416669ae6ad2SNogah Frankel 			goto nla_put_failure;
416769ae6ad2SNogah Frankel 
416869ae6ad2SNogah Frankel 		err = rtnl_get_offload_stats(skb, dev, prividx);
416969ae6ad2SNogah Frankel 		if (err == -ENODATA)
417069ae6ad2SNogah Frankel 			nla_nest_cancel(skb, attr);
417169ae6ad2SNogah Frankel 		else
417269ae6ad2SNogah Frankel 			nla_nest_end(skb, attr);
417369ae6ad2SNogah Frankel 
417469ae6ad2SNogah Frankel 		if (err && err != -ENODATA)
417569ae6ad2SNogah Frankel 			goto nla_put_failure;
417669ae6ad2SNogah Frankel 		*idxattr = 0;
417769ae6ad2SNogah Frankel 	}
417869ae6ad2SNogah Frankel 
4179aefb4d4aSRobert Shearman 	if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
4180aefb4d4aSRobert Shearman 		struct rtnl_af_ops *af_ops;
4181aefb4d4aSRobert Shearman 
4182aefb4d4aSRobert Shearman 		*idxattr = IFLA_STATS_AF_SPEC;
4183aefb4d4aSRobert Shearman 		attr = nla_nest_start(skb, IFLA_STATS_AF_SPEC);
4184aefb4d4aSRobert Shearman 		if (!attr)
4185aefb4d4aSRobert Shearman 			goto nla_put_failure;
4186aefb4d4aSRobert Shearman 
41875fa85a09SFlorian Westphal 		rcu_read_lock();
41885fa85a09SFlorian Westphal 		list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
4189aefb4d4aSRobert Shearman 			if (af_ops->fill_stats_af) {
4190aefb4d4aSRobert Shearman 				struct nlattr *af;
4191aefb4d4aSRobert Shearman 				int err;
4192aefb4d4aSRobert Shearman 
4193aefb4d4aSRobert Shearman 				af = nla_nest_start(skb, af_ops->family);
41945fa85a09SFlorian Westphal 				if (!af) {
41955fa85a09SFlorian Westphal 					rcu_read_unlock();
4196aefb4d4aSRobert Shearman 					goto nla_put_failure;
41975fa85a09SFlorian Westphal 				}
4198aefb4d4aSRobert Shearman 				err = af_ops->fill_stats_af(skb, dev);
4199aefb4d4aSRobert Shearman 
42005fa85a09SFlorian Westphal 				if (err == -ENODATA) {
4201aefb4d4aSRobert Shearman 					nla_nest_cancel(skb, af);
42025fa85a09SFlorian Westphal 				} else if (err < 0) {
42035fa85a09SFlorian Westphal 					rcu_read_unlock();
4204aefb4d4aSRobert Shearman 					goto nla_put_failure;
42055fa85a09SFlorian Westphal 				}
4206aefb4d4aSRobert Shearman 
4207aefb4d4aSRobert Shearman 				nla_nest_end(skb, af);
4208aefb4d4aSRobert Shearman 			}
4209aefb4d4aSRobert Shearman 		}
42105fa85a09SFlorian Westphal 		rcu_read_unlock();
4211aefb4d4aSRobert Shearman 
4212aefb4d4aSRobert Shearman 		nla_nest_end(skb, attr);
4213aefb4d4aSRobert Shearman 
4214aefb4d4aSRobert Shearman 		*idxattr = 0;
4215aefb4d4aSRobert Shearman 	}
4216aefb4d4aSRobert Shearman 
421710c9ead9SRoopa Prabhu 	nlmsg_end(skb, nlh);
421810c9ead9SRoopa Prabhu 
421910c9ead9SRoopa Prabhu 	return 0;
422010c9ead9SRoopa Prabhu 
422110c9ead9SRoopa Prabhu nla_put_failure:
4222e8872a25SNikolay Aleksandrov 	/* not a multi message or no progress mean a real error */
4223e8872a25SNikolay Aleksandrov 	if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
422410c9ead9SRoopa Prabhu 		nlmsg_cancel(skb, nlh);
4225e8872a25SNikolay Aleksandrov 	else
4226e8872a25SNikolay Aleksandrov 		nlmsg_end(skb, nlh);
422710c9ead9SRoopa Prabhu 
422810c9ead9SRoopa Prabhu 	return -EMSGSIZE;
422910c9ead9SRoopa Prabhu }
423010c9ead9SRoopa Prabhu 
423110c9ead9SRoopa Prabhu static size_t if_nlmsg_stats_size(const struct net_device *dev,
423210c9ead9SRoopa Prabhu 				  u32 filter_mask)
423310c9ead9SRoopa Prabhu {
423410c9ead9SRoopa Prabhu 	size_t size = 0;
423510c9ead9SRoopa Prabhu 
4236e8872a25SNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
423710c9ead9SRoopa Prabhu 		size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
423810c9ead9SRoopa Prabhu 
423997a47facSNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
424097a47facSNikolay Aleksandrov 		const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
424180e73cc5SNikolay Aleksandrov 		int attr = IFLA_STATS_LINK_XSTATS;
424297a47facSNikolay Aleksandrov 
424397a47facSNikolay Aleksandrov 		if (ops && ops->get_linkxstats_size) {
424480e73cc5SNikolay Aleksandrov 			size += nla_total_size(ops->get_linkxstats_size(dev,
424580e73cc5SNikolay Aleksandrov 									attr));
424697a47facSNikolay Aleksandrov 			/* for IFLA_STATS_LINK_XSTATS */
424797a47facSNikolay Aleksandrov 			size += nla_total_size(0);
424897a47facSNikolay Aleksandrov 		}
424997a47facSNikolay Aleksandrov 	}
425097a47facSNikolay Aleksandrov 
425180e73cc5SNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
425280e73cc5SNikolay Aleksandrov 		struct net_device *_dev = (struct net_device *)dev;
425380e73cc5SNikolay Aleksandrov 		const struct rtnl_link_ops *ops = NULL;
425480e73cc5SNikolay Aleksandrov 		const struct net_device *master;
425580e73cc5SNikolay Aleksandrov 
425680e73cc5SNikolay Aleksandrov 		/* netdev_master_upper_dev_get can't take const */
425780e73cc5SNikolay Aleksandrov 		master = netdev_master_upper_dev_get(_dev);
425880e73cc5SNikolay Aleksandrov 		if (master)
425980e73cc5SNikolay Aleksandrov 			ops = master->rtnl_link_ops;
426080e73cc5SNikolay Aleksandrov 		if (ops && ops->get_linkxstats_size) {
426180e73cc5SNikolay Aleksandrov 			int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
426280e73cc5SNikolay Aleksandrov 
426380e73cc5SNikolay Aleksandrov 			size += nla_total_size(ops->get_linkxstats_size(dev,
426480e73cc5SNikolay Aleksandrov 									attr));
426580e73cc5SNikolay Aleksandrov 			/* for IFLA_STATS_LINK_XSTATS_SLAVE */
426680e73cc5SNikolay Aleksandrov 			size += nla_total_size(0);
426780e73cc5SNikolay Aleksandrov 		}
426880e73cc5SNikolay Aleksandrov 	}
426980e73cc5SNikolay Aleksandrov 
427069ae6ad2SNogah Frankel 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0))
427169ae6ad2SNogah Frankel 		size += rtnl_get_offload_stats_size(dev);
427269ae6ad2SNogah Frankel 
4273aefb4d4aSRobert Shearman 	if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
4274aefb4d4aSRobert Shearman 		struct rtnl_af_ops *af_ops;
4275aefb4d4aSRobert Shearman 
4276aefb4d4aSRobert Shearman 		/* for IFLA_STATS_AF_SPEC */
4277aefb4d4aSRobert Shearman 		size += nla_total_size(0);
4278aefb4d4aSRobert Shearman 
42795fa85a09SFlorian Westphal 		rcu_read_lock();
42805fa85a09SFlorian Westphal 		list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
4281aefb4d4aSRobert Shearman 			if (af_ops->get_stats_af_size) {
4282aefb4d4aSRobert Shearman 				size += nla_total_size(
4283aefb4d4aSRobert Shearman 					af_ops->get_stats_af_size(dev));
4284aefb4d4aSRobert Shearman 
4285aefb4d4aSRobert Shearman 				/* for AF_* */
4286aefb4d4aSRobert Shearman 				size += nla_total_size(0);
4287aefb4d4aSRobert Shearman 			}
4288aefb4d4aSRobert Shearman 		}
42895fa85a09SFlorian Westphal 		rcu_read_unlock();
4290aefb4d4aSRobert Shearman 	}
4291aefb4d4aSRobert Shearman 
429210c9ead9SRoopa Prabhu 	return size;
429310c9ead9SRoopa Prabhu }
429410c9ead9SRoopa Prabhu 
4295c21ef3e3SDavid Ahern static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
4296c21ef3e3SDavid Ahern 			  struct netlink_ext_ack *extack)
429710c9ead9SRoopa Prabhu {
429810c9ead9SRoopa Prabhu 	struct net *net = sock_net(skb->sk);
429910c9ead9SRoopa Prabhu 	struct net_device *dev = NULL;
4300e8872a25SNikolay Aleksandrov 	int idxattr = 0, prividx = 0;
4301e8872a25SNikolay Aleksandrov 	struct if_stats_msg *ifsm;
430210c9ead9SRoopa Prabhu 	struct sk_buff *nskb;
430310c9ead9SRoopa Prabhu 	u32 filter_mask;
430410c9ead9SRoopa Prabhu 	int err;
430510c9ead9SRoopa Prabhu 
43064775cc1fSMathias Krause 	if (nlmsg_len(nlh) < sizeof(*ifsm))
43074775cc1fSMathias Krause 		return -EINVAL;
43084775cc1fSMathias Krause 
430910c9ead9SRoopa Prabhu 	ifsm = nlmsg_data(nlh);
431010c9ead9SRoopa Prabhu 	if (ifsm->ifindex > 0)
431110c9ead9SRoopa Prabhu 		dev = __dev_get_by_index(net, ifsm->ifindex);
431210c9ead9SRoopa Prabhu 	else
431310c9ead9SRoopa Prabhu 		return -EINVAL;
431410c9ead9SRoopa Prabhu 
431510c9ead9SRoopa Prabhu 	if (!dev)
431610c9ead9SRoopa Prabhu 		return -ENODEV;
431710c9ead9SRoopa Prabhu 
431810c9ead9SRoopa Prabhu 	filter_mask = ifsm->filter_mask;
431910c9ead9SRoopa Prabhu 	if (!filter_mask)
432010c9ead9SRoopa Prabhu 		return -EINVAL;
432110c9ead9SRoopa Prabhu 
432210c9ead9SRoopa Prabhu 	nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
432310c9ead9SRoopa Prabhu 	if (!nskb)
432410c9ead9SRoopa Prabhu 		return -ENOBUFS;
432510c9ead9SRoopa Prabhu 
432610c9ead9SRoopa Prabhu 	err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
432710c9ead9SRoopa Prabhu 				  NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
4328e8872a25SNikolay Aleksandrov 				  0, filter_mask, &idxattr, &prividx);
432910c9ead9SRoopa Prabhu 	if (err < 0) {
433010c9ead9SRoopa Prabhu 		/* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
433110c9ead9SRoopa Prabhu 		WARN_ON(err == -EMSGSIZE);
433210c9ead9SRoopa Prabhu 		kfree_skb(nskb);
433310c9ead9SRoopa Prabhu 	} else {
433410c9ead9SRoopa Prabhu 		err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
433510c9ead9SRoopa Prabhu 	}
433610c9ead9SRoopa Prabhu 
433710c9ead9SRoopa Prabhu 	return err;
433810c9ead9SRoopa Prabhu }
433910c9ead9SRoopa Prabhu 
434010c9ead9SRoopa Prabhu static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
434110c9ead9SRoopa Prabhu {
4342e8872a25SNikolay Aleksandrov 	int h, s_h, err, s_idx, s_idxattr, s_prividx;
434310c9ead9SRoopa Prabhu 	struct net *net = sock_net(skb->sk);
434410c9ead9SRoopa Prabhu 	unsigned int flags = NLM_F_MULTI;
4345e8872a25SNikolay Aleksandrov 	struct if_stats_msg *ifsm;
4346e8872a25SNikolay Aleksandrov 	struct hlist_head *head;
4347e8872a25SNikolay Aleksandrov 	struct net_device *dev;
434810c9ead9SRoopa Prabhu 	u32 filter_mask = 0;
4349e8872a25SNikolay Aleksandrov 	int idx = 0;
435010c9ead9SRoopa Prabhu 
435110c9ead9SRoopa Prabhu 	s_h = cb->args[0];
435210c9ead9SRoopa Prabhu 	s_idx = cb->args[1];
4353e8872a25SNikolay Aleksandrov 	s_idxattr = cb->args[2];
4354e8872a25SNikolay Aleksandrov 	s_prividx = cb->args[3];
435510c9ead9SRoopa Prabhu 
435610c9ead9SRoopa Prabhu 	cb->seq = net->dev_base_seq;
435710c9ead9SRoopa Prabhu 
43584775cc1fSMathias Krause 	if (nlmsg_len(cb->nlh) < sizeof(*ifsm))
43594775cc1fSMathias Krause 		return -EINVAL;
43604775cc1fSMathias Krause 
436110c9ead9SRoopa Prabhu 	ifsm = nlmsg_data(cb->nlh);
436210c9ead9SRoopa Prabhu 	filter_mask = ifsm->filter_mask;
436310c9ead9SRoopa Prabhu 	if (!filter_mask)
436410c9ead9SRoopa Prabhu 		return -EINVAL;
436510c9ead9SRoopa Prabhu 
436610c9ead9SRoopa Prabhu 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
436710c9ead9SRoopa Prabhu 		idx = 0;
436810c9ead9SRoopa Prabhu 		head = &net->dev_index_head[h];
436910c9ead9SRoopa Prabhu 		hlist_for_each_entry(dev, head, index_hlist) {
437010c9ead9SRoopa Prabhu 			if (idx < s_idx)
437110c9ead9SRoopa Prabhu 				goto cont;
437210c9ead9SRoopa Prabhu 			err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
437310c9ead9SRoopa Prabhu 						  NETLINK_CB(cb->skb).portid,
437410c9ead9SRoopa Prabhu 						  cb->nlh->nlmsg_seq, 0,
4375e8872a25SNikolay Aleksandrov 						  flags, filter_mask,
4376e8872a25SNikolay Aleksandrov 						  &s_idxattr, &s_prividx);
437710c9ead9SRoopa Prabhu 			/* If we ran out of room on the first message,
437810c9ead9SRoopa Prabhu 			 * we're in trouble
437910c9ead9SRoopa Prabhu 			 */
438010c9ead9SRoopa Prabhu 			WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
438110c9ead9SRoopa Prabhu 
438210c9ead9SRoopa Prabhu 			if (err < 0)
438310c9ead9SRoopa Prabhu 				goto out;
4384e8872a25SNikolay Aleksandrov 			s_prividx = 0;
4385e8872a25SNikolay Aleksandrov 			s_idxattr = 0;
438610c9ead9SRoopa Prabhu 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
438710c9ead9SRoopa Prabhu cont:
438810c9ead9SRoopa Prabhu 			idx++;
438910c9ead9SRoopa Prabhu 		}
439010c9ead9SRoopa Prabhu 	}
439110c9ead9SRoopa Prabhu out:
4392e8872a25SNikolay Aleksandrov 	cb->args[3] = s_prividx;
4393e8872a25SNikolay Aleksandrov 	cb->args[2] = s_idxattr;
439410c9ead9SRoopa Prabhu 	cb->args[1] = idx;
439510c9ead9SRoopa Prabhu 	cb->args[0] = h;
439610c9ead9SRoopa Prabhu 
439710c9ead9SRoopa Prabhu 	return skb->len;
439810c9ead9SRoopa Prabhu }
439910c9ead9SRoopa Prabhu 
44001da177e4SLinus Torvalds /* Process one rtnetlink message. */
44011da177e4SLinus Torvalds 
44022d4bc933SJohannes Berg static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
44032d4bc933SJohannes Berg 			     struct netlink_ext_ack *extack)
44041da177e4SLinus Torvalds {
44053b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
4406addf9b90SFlorian Westphal 	struct rtnl_link *link;
4407*e4202511SFlorian Westphal 	struct module *owner;
44086853dd48SFlorian Westphal 	int err = -EOPNOTSUPP;
4409e2849863SThomas Graf 	rtnl_doit_func doit;
441062256f98SFlorian Westphal 	unsigned int flags;
4411617cfc75SAlexander Kuleshov 	int kind;
44121da177e4SLinus Torvalds 	int family;
44131da177e4SLinus Torvalds 	int type;
44141da177e4SLinus Torvalds 
44151da177e4SLinus Torvalds 	type = nlh->nlmsg_type;
44161da177e4SLinus Torvalds 	if (type > RTM_MAX)
4417038890feSThomas Graf 		return -EOPNOTSUPP;
44181da177e4SLinus Torvalds 
44191da177e4SLinus Torvalds 	type -= RTM_BASE;
44201da177e4SLinus Torvalds 
44211da177e4SLinus Torvalds 	/* All the messages must have at least 1 byte length */
4422573ce260SHong zhi guo 	if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
44231da177e4SLinus Torvalds 		return 0;
44241da177e4SLinus Torvalds 
4425573ce260SHong zhi guo 	family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
44261da177e4SLinus Torvalds 	kind = type&3;
44271da177e4SLinus Torvalds 
442890f62cf3SEric W. Biederman 	if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
44291d00a4ebSThomas Graf 		return -EPERM;
44301da177e4SLinus Torvalds 
44316853dd48SFlorian Westphal 	rcu_read_lock();
4432b8f3ab42SDavid S. Miller 	if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
443397c53cacSDenis V. Lunev 		struct sock *rtnl;
4434e2849863SThomas Graf 		rtnl_dumpit_func dumpit;
4435c7ac8679SGreg Rose 		u16 min_dump_alloc = 0;
44361da177e4SLinus Torvalds 
4437addf9b90SFlorian Westphal 		link = rtnl_get_link(family, type);
4438addf9b90SFlorian Westphal 		if (!link || !link->dumpit) {
44396853dd48SFlorian Westphal 			family = PF_UNSPEC;
4440addf9b90SFlorian Westphal 			link = rtnl_get_link(family, type);
4441addf9b90SFlorian Westphal 			if (!link || !link->dumpit)
44426853dd48SFlorian Westphal 				goto err_unlock;
44436853dd48SFlorian Westphal 		}
4444*e4202511SFlorian Westphal 		owner = link->owner;
4445addf9b90SFlorian Westphal 		dumpit = link->dumpit;
44466853dd48SFlorian Westphal 
44475c2bb9b6SFlorian Westphal 		if (type == RTM_GETLINK - RTM_BASE)
4448e1fa6d21SFlorian Westphal 			min_dump_alloc = rtnl_calcit(skb, nlh);
44491da177e4SLinus Torvalds 
4450*e4202511SFlorian Westphal 		err = 0;
4451*e4202511SFlorian Westphal 		/* need to do this before rcu_read_unlock() */
4452*e4202511SFlorian Westphal 		if (!try_module_get(owner))
4453*e4202511SFlorian Westphal 			err = -EPROTONOSUPPORT;
4454*e4202511SFlorian Westphal 
44556853dd48SFlorian Westphal 		rcu_read_unlock();
44566853dd48SFlorian Westphal 
445797c53cacSDenis V. Lunev 		rtnl = net->rtnl;
4458*e4202511SFlorian Westphal 		if (err == 0) {
445980d326faSPablo Neira Ayuso 			struct netlink_dump_control c = {
446080d326faSPablo Neira Ayuso 				.dump		= dumpit,
446180d326faSPablo Neira Ayuso 				.min_dump_alloc	= min_dump_alloc,
4462*e4202511SFlorian Westphal 				.module		= owner,
446380d326faSPablo Neira Ayuso 			};
446480d326faSPablo Neira Ayuso 			err = netlink_dump_start(rtnl, skb, nlh, &c);
4465*e4202511SFlorian Westphal 			/* netlink_dump_start() will keep a reference on
4466*e4202511SFlorian Westphal 			 * module if dump is still in progress.
4467*e4202511SFlorian Westphal 			 */
4468*e4202511SFlorian Westphal 			module_put(owner);
446980d326faSPablo Neira Ayuso 		}
44702907c35fSEric Dumazet 		return err;
44711da177e4SLinus Torvalds 	}
44721da177e4SLinus Torvalds 
4473addf9b90SFlorian Westphal 	link = rtnl_get_link(family, type);
4474addf9b90SFlorian Westphal 	if (!link || !link->doit) {
44758caa38b5SFlorian Westphal 		family = PF_UNSPEC;
4476addf9b90SFlorian Westphal 		link = rtnl_get_link(PF_UNSPEC, type);
4477addf9b90SFlorian Westphal 		if (!link || !link->doit)
4478addf9b90SFlorian Westphal 			goto out_unlock;
44798caa38b5SFlorian Westphal 	}
44808caa38b5SFlorian Westphal 
4481*e4202511SFlorian Westphal 	owner = link->owner;
4482*e4202511SFlorian Westphal 	if (!try_module_get(owner)) {
4483*e4202511SFlorian Westphal 		err = -EPROTONOSUPPORT;
4484*e4202511SFlorian Westphal 		goto out_unlock;
4485*e4202511SFlorian Westphal 	}
4486*e4202511SFlorian Westphal 
4487addf9b90SFlorian Westphal 	flags = link->flags;
448862256f98SFlorian Westphal 	if (flags & RTNL_FLAG_DOIT_UNLOCKED) {
4489addf9b90SFlorian Westphal 		doit = link->doit;
449062256f98SFlorian Westphal 		rcu_read_unlock();
449162256f98SFlorian Westphal 		if (doit)
449262256f98SFlorian Westphal 			err = doit(skb, nlh, extack);
4493*e4202511SFlorian Westphal 		module_put(owner);
449462256f98SFlorian Westphal 		return err;
449562256f98SFlorian Westphal 	}
44966853dd48SFlorian Westphal 	rcu_read_unlock();
44976853dd48SFlorian Westphal 
44980cc09020SFlorian Westphal 	rtnl_lock();
4499addf9b90SFlorian Westphal 	link = rtnl_get_link(family, type);
4500addf9b90SFlorian Westphal 	if (link && link->doit)
4501addf9b90SFlorian Westphal 		err = link->doit(skb, nlh, extack);
45020cc09020SFlorian Westphal 	rtnl_unlock();
4503addf9b90SFlorian Westphal 
4504*e4202511SFlorian Westphal 	module_put(owner);
4505*e4202511SFlorian Westphal 
4506addf9b90SFlorian Westphal 	return err;
4507addf9b90SFlorian Westphal 
4508addf9b90SFlorian Westphal out_unlock:
4509addf9b90SFlorian Westphal 	rcu_read_unlock();
45100cc09020SFlorian Westphal 	return err;
45110cc09020SFlorian Westphal 
45120cc09020SFlorian Westphal err_unlock:
45136853dd48SFlorian Westphal 	rcu_read_unlock();
45140cc09020SFlorian Westphal 	return -EOPNOTSUPP;
45151da177e4SLinus Torvalds }
45161da177e4SLinus Torvalds 
4517cd40b7d3SDenis V. Lunev static void rtnetlink_rcv(struct sk_buff *skb)
45181da177e4SLinus Torvalds {
4519cd40b7d3SDenis V. Lunev 	netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
45201da177e4SLinus Torvalds }
45211da177e4SLinus Torvalds 
45225f729eaaSJulien Gomes static int rtnetlink_bind(struct net *net, int group)
45235f729eaaSJulien Gomes {
45245f729eaaSJulien Gomes 	switch (group) {
45255f729eaaSJulien Gomes 	case RTNLGRP_IPV4_MROUTE_R:
45265f729eaaSJulien Gomes 	case RTNLGRP_IPV6_MROUTE_R:
45275f729eaaSJulien Gomes 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
45285f729eaaSJulien Gomes 			return -EPERM;
45295f729eaaSJulien Gomes 		break;
45305f729eaaSJulien Gomes 	}
45315f729eaaSJulien Gomes 	return 0;
45325f729eaaSJulien Gomes }
45335f729eaaSJulien Gomes 
45341da177e4SLinus Torvalds static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
45351da177e4SLinus Torvalds {
4536351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
4537e9dc8653SEric W. Biederman 
45381da177e4SLinus Torvalds 	switch (event) {
45395138e86fSVlad Yasevich 	case NETDEV_REBOOT:
45408a212589SXin Long 	case NETDEV_CHANGEMTU:
45413753654eSDavid Ahern 	case NETDEV_CHANGEADDR:
45425138e86fSVlad Yasevich 	case NETDEV_CHANGENAME:
45435138e86fSVlad Yasevich 	case NETDEV_FEAT_CHANGE:
45445138e86fSVlad Yasevich 	case NETDEV_BONDING_FAILOVER:
4545e6e66594SXin Long 	case NETDEV_POST_TYPE_CHANGE:
45465138e86fSVlad Yasevich 	case NETDEV_NOTIFY_PEERS:
4547dc709f37SXin Long 	case NETDEV_CHANGEUPPER:
45485138e86fSVlad Yasevich 	case NETDEV_RESEND_IGMP:
45495138e86fSVlad Yasevich 	case NETDEV_CHANGEINFODATA:
4550eeda3fb9SXin Long 	case NETDEV_CHANGELOWERSTATE:
4551ebdcf045SXin Long 	case NETDEV_CHANGE_TX_QUEUE_LEN:
45523d3ea5afSVlad Yasevich 		rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
45536621dd29SNicolas Dichtel 				   GFP_KERNEL, NULL);
45541da177e4SLinus Torvalds 		break;
45551da177e4SLinus Torvalds 	default:
45561da177e4SLinus Torvalds 		break;
45571da177e4SLinus Torvalds 	}
45581da177e4SLinus Torvalds 	return NOTIFY_DONE;
45591da177e4SLinus Torvalds }
45601da177e4SLinus Torvalds 
45611da177e4SLinus Torvalds static struct notifier_block rtnetlink_dev_notifier = {
45621da177e4SLinus Torvalds 	.notifier_call	= rtnetlink_event,
45631da177e4SLinus Torvalds };
45641da177e4SLinus Torvalds 
456597c53cacSDenis V. Lunev 
45662c8c1e72SAlexey Dobriyan static int __net_init rtnetlink_net_init(struct net *net)
456797c53cacSDenis V. Lunev {
456897c53cacSDenis V. Lunev 	struct sock *sk;
4569a31f2d17SPablo Neira Ayuso 	struct netlink_kernel_cfg cfg = {
4570a31f2d17SPablo Neira Ayuso 		.groups		= RTNLGRP_MAX,
4571a31f2d17SPablo Neira Ayuso 		.input		= rtnetlink_rcv,
4572a31f2d17SPablo Neira Ayuso 		.cb_mutex	= &rtnl_mutex,
45739785e10aSPablo Neira Ayuso 		.flags		= NL_CFG_F_NONROOT_RECV,
45745f729eaaSJulien Gomes 		.bind		= rtnetlink_bind,
4575a31f2d17SPablo Neira Ayuso 	};
4576a31f2d17SPablo Neira Ayuso 
45779f00d977SPablo Neira Ayuso 	sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
457897c53cacSDenis V. Lunev 	if (!sk)
457997c53cacSDenis V. Lunev 		return -ENOMEM;
458097c53cacSDenis V. Lunev 	net->rtnl = sk;
458197c53cacSDenis V. Lunev 	return 0;
458297c53cacSDenis V. Lunev }
458397c53cacSDenis V. Lunev 
45842c8c1e72SAlexey Dobriyan static void __net_exit rtnetlink_net_exit(struct net *net)
458597c53cacSDenis V. Lunev {
4586b7c6ba6eSDenis V. Lunev 	netlink_kernel_release(net->rtnl);
458797c53cacSDenis V. Lunev 	net->rtnl = NULL;
458897c53cacSDenis V. Lunev }
458997c53cacSDenis V. Lunev 
459097c53cacSDenis V. Lunev static struct pernet_operations rtnetlink_net_ops = {
459197c53cacSDenis V. Lunev 	.init = rtnetlink_net_init,
459297c53cacSDenis V. Lunev 	.exit = rtnetlink_net_exit,
459397c53cacSDenis V. Lunev };
459497c53cacSDenis V. Lunev 
45951da177e4SLinus Torvalds void __init rtnetlink_init(void)
45961da177e4SLinus Torvalds {
459797c53cacSDenis V. Lunev 	if (register_pernet_subsys(&rtnetlink_net_ops))
45981da177e4SLinus Torvalds 		panic("rtnetlink_init: cannot initialize rtnetlink\n");
459997c53cacSDenis V. Lunev 
46001da177e4SLinus Torvalds 	register_netdevice_notifier(&rtnetlink_dev_notifier);
4601340d17fcSThomas Graf 
4602c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
4603b97bac64SFlorian Westphal 		      rtnl_dump_ifinfo, 0);
4604b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0);
4605b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0);
4606b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0);
4607687ad8ccSThomas Graf 
4608b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0);
4609b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0);
4610b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0);
461177162022SJohn Fastabend 
4612b97bac64SFlorian Westphal 	rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
4613b97bac64SFlorian Westphal 	rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0);
4614b97bac64SFlorian Westphal 	rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, 0);
4615e5a55a89SJohn Fastabend 
4616b97bac64SFlorian Westphal 	rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0);
4617b97bac64SFlorian Westphal 	rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0);
4618b97bac64SFlorian Westphal 	rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0);
461910c9ead9SRoopa Prabhu 
462010c9ead9SRoopa Prabhu 	rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,
4621b97bac64SFlorian Westphal 		      0);
46221da177e4SLinus Torvalds }
4623