xref: /openbmc/linux/net/core/rtnetlink.c (revision b2d3bcfa26a7a8de41f358a6cae8b848673b3c6e)
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;
65e4202511SFlorian 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 
132b0e9fe1bSFlorian 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 
162e4202511SFlorian Westphal static int rtnl_register_internal(struct module *owner,
163e4202511SFlorian Westphal 				  int protocol, int msgtype,
164c7ac8679SGreg Rose 				  rtnl_doit_func doit, rtnl_dumpit_func dumpit,
165b97bac64SFlorian Westphal 				  unsigned int flags)
166e2849863SThomas Graf {
167b0e9fe1bSFlorian Westphal 	struct rtnl_link *link, *old;
168b0e9fe1bSFlorian Westphal 	struct rtnl_link __rcu **tab;
169e2849863SThomas Graf 	int msgindex;
170addf9b90SFlorian Westphal 	int ret = -ENOBUFS;
171e2849863SThomas Graf 
17225239ceeSPatrick McHardy 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
173e2849863SThomas Graf 	msgindex = rtm_msgindex(msgtype);
174e2849863SThomas Graf 
175addf9b90SFlorian Westphal 	rtnl_lock();
176addf9b90SFlorian Westphal 	tab = rtnl_msg_handlers[protocol];
177e2849863SThomas Graf 	if (tab == NULL) {
178addf9b90SFlorian Westphal 		tab = kcalloc(RTM_NR_MSGTYPES, sizeof(void *), GFP_KERNEL);
179addf9b90SFlorian Westphal 		if (!tab)
180addf9b90SFlorian Westphal 			goto unlock;
181e2849863SThomas Graf 
182addf9b90SFlorian Westphal 		/* ensures we see the 0 stores */
1836853dd48SFlorian Westphal 		rcu_assign_pointer(rtnl_msg_handlers[protocol], tab);
184e2849863SThomas Graf 	}
185e2849863SThomas Graf 
186addf9b90SFlorian Westphal 	old = rtnl_dereference(tab[msgindex]);
187addf9b90SFlorian Westphal 	if (old) {
188addf9b90SFlorian Westphal 		link = kmemdup(old, sizeof(*old), GFP_KERNEL);
189addf9b90SFlorian Westphal 		if (!link)
190addf9b90SFlorian Westphal 			goto unlock;
191addf9b90SFlorian Westphal 	} else {
192addf9b90SFlorian Westphal 		link = kzalloc(sizeof(*link), GFP_KERNEL);
193addf9b90SFlorian Westphal 		if (!link)
194addf9b90SFlorian Westphal 			goto unlock;
195addf9b90SFlorian Westphal 	}
196e2849863SThomas Graf 
197e4202511SFlorian Westphal 	WARN_ON(link->owner && link->owner != owner);
198e4202511SFlorian Westphal 	link->owner = owner;
199e4202511SFlorian Westphal 
200addf9b90SFlorian Westphal 	WARN_ON(doit && link->doit && link->doit != doit);
201addf9b90SFlorian Westphal 	if (doit)
202addf9b90SFlorian Westphal 		link->doit = doit;
203addf9b90SFlorian Westphal 	WARN_ON(dumpit && link->dumpit && link->dumpit != dumpit);
204addf9b90SFlorian Westphal 	if (dumpit)
205addf9b90SFlorian Westphal 		link->dumpit = dumpit;
206addf9b90SFlorian Westphal 
207addf9b90SFlorian Westphal 	link->flags |= flags;
208addf9b90SFlorian Westphal 
209addf9b90SFlorian Westphal 	/* publish protocol:msgtype */
210addf9b90SFlorian Westphal 	rcu_assign_pointer(tab[msgindex], link);
211addf9b90SFlorian Westphal 	ret = 0;
212addf9b90SFlorian Westphal 	if (old)
213addf9b90SFlorian Westphal 		kfree_rcu(old, rcu);
214addf9b90SFlorian Westphal unlock:
215addf9b90SFlorian Westphal 	rtnl_unlock();
216addf9b90SFlorian Westphal 	return ret;
217e2849863SThomas Graf }
218e4202511SFlorian Westphal 
219e4202511SFlorian Westphal /**
220e4202511SFlorian Westphal  * rtnl_register_module - Register a rtnetlink message type
221e4202511SFlorian Westphal  *
222e4202511SFlorian Westphal  * @owner: module registering the hook (THIS_MODULE)
223e4202511SFlorian Westphal  * @protocol: Protocol family or PF_UNSPEC
224e4202511SFlorian Westphal  * @msgtype: rtnetlink message type
225e4202511SFlorian Westphal  * @doit: Function pointer called for each request message
226e4202511SFlorian Westphal  * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
227e4202511SFlorian Westphal  * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
228e4202511SFlorian Westphal  *
229e4202511SFlorian Westphal  * Like rtnl_register, but for use by removable modules.
230e4202511SFlorian Westphal  */
231e4202511SFlorian Westphal int rtnl_register_module(struct module *owner,
232e4202511SFlorian Westphal 			 int protocol, int msgtype,
233e4202511SFlorian Westphal 			 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
234e4202511SFlorian Westphal 			 unsigned int flags)
235e4202511SFlorian Westphal {
236e4202511SFlorian Westphal 	return rtnl_register_internal(owner, protocol, msgtype,
237e4202511SFlorian Westphal 				      doit, dumpit, flags);
238e4202511SFlorian Westphal }
239e4202511SFlorian Westphal EXPORT_SYMBOL_GPL(rtnl_register_module);
240e4202511SFlorian Westphal 
241e4202511SFlorian Westphal /**
24216feebcfSFlorian Westphal  * rtnl_register - Register a rtnetlink message type
243e4202511SFlorian Westphal  * @protocol: Protocol family or PF_UNSPEC
244e4202511SFlorian Westphal  * @msgtype: rtnetlink message type
245e4202511SFlorian Westphal  * @doit: Function pointer called for each request message
246e4202511SFlorian Westphal  * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
247e4202511SFlorian Westphal  * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
248e4202511SFlorian Westphal  *
249e4202511SFlorian Westphal  * Registers the specified function pointers (at least one of them has
250e4202511SFlorian Westphal  * to be non-NULL) to be called whenever a request message for the
251e4202511SFlorian Westphal  * specified protocol family and message type is received.
252e4202511SFlorian Westphal  *
253e4202511SFlorian Westphal  * The special protocol family PF_UNSPEC may be used to define fallback
254e4202511SFlorian Westphal  * function pointers for the case when no entry for the specific protocol
255e4202511SFlorian Westphal  * family exists.
256e2849863SThomas Graf  */
257e2849863SThomas Graf void rtnl_register(int protocol, int msgtype,
258c7ac8679SGreg Rose 		   rtnl_doit_func doit, rtnl_dumpit_func dumpit,
259b97bac64SFlorian Westphal 		   unsigned int flags)
260e2849863SThomas Graf {
26116feebcfSFlorian Westphal 	int err;
26216feebcfSFlorian Westphal 
26316feebcfSFlorian Westphal 	err = rtnl_register_internal(NULL, protocol, msgtype, doit, dumpit,
26416feebcfSFlorian Westphal 				     flags);
26516feebcfSFlorian Westphal 	if (err)
26616feebcfSFlorian Westphal 		pr_err("Unable to register rtnetlink message handler, "
26716feebcfSFlorian Westphal 		       "protocol = %d, message type = %d\n", protocol, msgtype);
268e2849863SThomas Graf }
269e2849863SThomas Graf 
270e2849863SThomas Graf /**
271e2849863SThomas Graf  * rtnl_unregister - Unregister a rtnetlink message type
272e2849863SThomas Graf  * @protocol: Protocol family or PF_UNSPEC
273e2849863SThomas Graf  * @msgtype: rtnetlink message type
274e2849863SThomas Graf  *
275e2849863SThomas Graf  * Returns 0 on success or a negative error code.
276e2849863SThomas Graf  */
277e2849863SThomas Graf int rtnl_unregister(int protocol, int msgtype)
278e2849863SThomas Graf {
279addf9b90SFlorian Westphal 	struct rtnl_link **tab, *link;
280e2849863SThomas Graf 	int msgindex;
281e2849863SThomas Graf 
28225239ceeSPatrick McHardy 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
283e2849863SThomas Graf 	msgindex = rtm_msgindex(msgtype);
284e2849863SThomas Graf 
2856853dd48SFlorian Westphal 	rtnl_lock();
286addf9b90SFlorian Westphal 	tab = rtnl_dereference(rtnl_msg_handlers[protocol]);
287addf9b90SFlorian Westphal 	if (!tab) {
2886853dd48SFlorian Westphal 		rtnl_unlock();
289e2849863SThomas Graf 		return -ENOENT;
2906853dd48SFlorian Westphal 	}
291e2849863SThomas Graf 
292addf9b90SFlorian Westphal 	link = tab[msgindex];
293addf9b90SFlorian Westphal 	rcu_assign_pointer(tab[msgindex], NULL);
2946853dd48SFlorian Westphal 	rtnl_unlock();
295e2849863SThomas Graf 
296addf9b90SFlorian Westphal 	kfree_rcu(link, rcu);
297addf9b90SFlorian Westphal 
298e2849863SThomas Graf 	return 0;
299e2849863SThomas Graf }
300e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister);
301e2849863SThomas Graf 
302e2849863SThomas Graf /**
303e2849863SThomas Graf  * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
304e2849863SThomas Graf  * @protocol : Protocol family or PF_UNSPEC
305e2849863SThomas Graf  *
306e2849863SThomas Graf  * Identical to calling rtnl_unregster() for all registered message types
307e2849863SThomas Graf  * of a certain protocol family.
308e2849863SThomas Graf  */
309e2849863SThomas Graf void rtnl_unregister_all(int protocol)
310e2849863SThomas Graf {
311addf9b90SFlorian Westphal 	struct rtnl_link **tab, *link;
312addf9b90SFlorian Westphal 	int msgindex;
313019a3169SFlorian Westphal 
31425239ceeSPatrick McHardy 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
315e2849863SThomas Graf 
316019a3169SFlorian Westphal 	rtnl_lock();
317addf9b90SFlorian Westphal 	tab = rtnl_msg_handlers[protocol];
3186853dd48SFlorian Westphal 	RCU_INIT_POINTER(rtnl_msg_handlers[protocol], NULL);
319addf9b90SFlorian Westphal 	for (msgindex = 0; msgindex < RTM_NR_MSGTYPES; msgindex++) {
320addf9b90SFlorian Westphal 		link = tab[msgindex];
321addf9b90SFlorian Westphal 		if (!link)
322addf9b90SFlorian Westphal 			continue;
323addf9b90SFlorian Westphal 
324addf9b90SFlorian Westphal 		rcu_assign_pointer(tab[msgindex], NULL);
325addf9b90SFlorian Westphal 		kfree_rcu(link, rcu);
326addf9b90SFlorian Westphal 	}
327019a3169SFlorian Westphal 	rtnl_unlock();
328019a3169SFlorian Westphal 
3296853dd48SFlorian Westphal 	synchronize_net();
3306853dd48SFlorian Westphal 
331addf9b90SFlorian Westphal 	kfree(tab);
332e2849863SThomas Graf }
333e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister_all);
3341da177e4SLinus Torvalds 
33538f7b870SPatrick McHardy static LIST_HEAD(link_ops);
33638f7b870SPatrick McHardy 
337c63044f0SEric Dumazet static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
338c63044f0SEric Dumazet {
339c63044f0SEric Dumazet 	const struct rtnl_link_ops *ops;
340c63044f0SEric Dumazet 
341c63044f0SEric Dumazet 	list_for_each_entry(ops, &link_ops, list) {
342c63044f0SEric Dumazet 		if (!strcmp(ops->kind, kind))
343c63044f0SEric Dumazet 			return ops;
344c63044f0SEric Dumazet 	}
345c63044f0SEric Dumazet 	return NULL;
346c63044f0SEric Dumazet }
347c63044f0SEric Dumazet 
34838f7b870SPatrick McHardy /**
34938f7b870SPatrick McHardy  * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
35038f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to register
35138f7b870SPatrick McHardy  *
35238f7b870SPatrick McHardy  * The caller must hold the rtnl_mutex. This function should be used
35338f7b870SPatrick McHardy  * by drivers that create devices during module initialization. It
35438f7b870SPatrick McHardy  * must be called before registering the devices.
35538f7b870SPatrick McHardy  *
35638f7b870SPatrick McHardy  * Returns 0 on success or a negative error code.
35738f7b870SPatrick McHardy  */
35838f7b870SPatrick McHardy int __rtnl_link_register(struct rtnl_link_ops *ops)
35938f7b870SPatrick McHardy {
360c63044f0SEric Dumazet 	if (rtnl_link_ops_get(ops->kind))
361c63044f0SEric Dumazet 		return -EEXIST;
362c63044f0SEric Dumazet 
363b0ab2fabSJiri Pirko 	/* The check for setup is here because if ops
364b0ab2fabSJiri Pirko 	 * does not have that filled up, it is not possible
365b0ab2fabSJiri Pirko 	 * to use the ops for creating device. So do not
366b0ab2fabSJiri Pirko 	 * fill up dellink as well. That disables rtnl_dellink.
367b0ab2fabSJiri Pirko 	 */
368b0ab2fabSJiri Pirko 	if (ops->setup && !ops->dellink)
36923289a37SEric Dumazet 		ops->dellink = unregister_netdevice_queue;
3702d85cba2SPatrick McHardy 
37138f7b870SPatrick McHardy 	list_add_tail(&ops->list, &link_ops);
37238f7b870SPatrick McHardy 	return 0;
37338f7b870SPatrick McHardy }
37438f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_register);
37538f7b870SPatrick McHardy 
37638f7b870SPatrick McHardy /**
37738f7b870SPatrick McHardy  * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
37838f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to register
37938f7b870SPatrick McHardy  *
38038f7b870SPatrick McHardy  * Returns 0 on success or a negative error code.
38138f7b870SPatrick McHardy  */
38238f7b870SPatrick McHardy int rtnl_link_register(struct rtnl_link_ops *ops)
38338f7b870SPatrick McHardy {
38438f7b870SPatrick McHardy 	int err;
38538f7b870SPatrick McHardy 
38638f7b870SPatrick McHardy 	rtnl_lock();
38738f7b870SPatrick McHardy 	err = __rtnl_link_register(ops);
38838f7b870SPatrick McHardy 	rtnl_unlock();
38938f7b870SPatrick McHardy 	return err;
39038f7b870SPatrick McHardy }
39138f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_register);
39238f7b870SPatrick McHardy 
393669f87baSPavel Emelyanov static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
394669f87baSPavel Emelyanov {
395669f87baSPavel Emelyanov 	struct net_device *dev;
39623289a37SEric Dumazet 	LIST_HEAD(list_kill);
39723289a37SEric Dumazet 
398669f87baSPavel Emelyanov 	for_each_netdev(net, dev) {
39923289a37SEric Dumazet 		if (dev->rtnl_link_ops == ops)
40023289a37SEric Dumazet 			ops->dellink(dev, &list_kill);
401669f87baSPavel Emelyanov 	}
40223289a37SEric Dumazet 	unregister_netdevice_many(&list_kill);
403669f87baSPavel Emelyanov }
404669f87baSPavel Emelyanov 
40538f7b870SPatrick McHardy /**
40638f7b870SPatrick McHardy  * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
40738f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to unregister
40838f7b870SPatrick McHardy  *
4092d85cba2SPatrick McHardy  * The caller must hold the rtnl_mutex.
41038f7b870SPatrick McHardy  */
41138f7b870SPatrick McHardy void __rtnl_link_unregister(struct rtnl_link_ops *ops)
41238f7b870SPatrick McHardy {
413881d966bSEric W. Biederman 	struct net *net;
4142d85cba2SPatrick McHardy 
415881d966bSEric W. Biederman 	for_each_net(net) {
416669f87baSPavel Emelyanov 		__rtnl_kill_links(net, ops);
417881d966bSEric W. Biederman 	}
41838f7b870SPatrick McHardy 	list_del(&ops->list);
41938f7b870SPatrick McHardy }
42038f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
42138f7b870SPatrick McHardy 
422200b916fSCong Wang /* Return with the rtnl_lock held when there are no network
423200b916fSCong Wang  * devices unregistering in any network namespace.
424200b916fSCong Wang  */
425200b916fSCong Wang static void rtnl_lock_unregistering_all(void)
426200b916fSCong Wang {
427200b916fSCong Wang 	struct net *net;
428200b916fSCong Wang 	bool unregistering;
429ff960a73SPeter Zijlstra 	DEFINE_WAIT_FUNC(wait, woken_wake_function);
430200b916fSCong Wang 
431ff960a73SPeter Zijlstra 	add_wait_queue(&netdev_unregistering_wq, &wait);
432200b916fSCong Wang 	for (;;) {
433200b916fSCong Wang 		unregistering = false;
434200b916fSCong Wang 		rtnl_lock();
435200b916fSCong Wang 		for_each_net(net) {
436200b916fSCong Wang 			if (net->dev_unreg_count > 0) {
437200b916fSCong Wang 				unregistering = true;
438200b916fSCong Wang 				break;
439200b916fSCong Wang 			}
440200b916fSCong Wang 		}
441200b916fSCong Wang 		if (!unregistering)
442200b916fSCong Wang 			break;
443200b916fSCong Wang 		__rtnl_unlock();
444ff960a73SPeter Zijlstra 
445ff960a73SPeter Zijlstra 		wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
446200b916fSCong Wang 	}
447ff960a73SPeter Zijlstra 	remove_wait_queue(&netdev_unregistering_wq, &wait);
448200b916fSCong Wang }
449200b916fSCong Wang 
45038f7b870SPatrick McHardy /**
45138f7b870SPatrick McHardy  * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
45238f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to unregister
45338f7b870SPatrick McHardy  */
45438f7b870SPatrick McHardy void rtnl_link_unregister(struct rtnl_link_ops *ops)
45538f7b870SPatrick McHardy {
456200b916fSCong Wang 	/* Close the race with cleanup_net() */
457200b916fSCong Wang 	mutex_lock(&net_mutex);
458200b916fSCong Wang 	rtnl_lock_unregistering_all();
45938f7b870SPatrick McHardy 	__rtnl_link_unregister(ops);
46038f7b870SPatrick McHardy 	rtnl_unlock();
461200b916fSCong Wang 	mutex_unlock(&net_mutex);
46238f7b870SPatrick McHardy }
46338f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_unregister);
46438f7b870SPatrick McHardy 
465ba7d49b1SJiri Pirko static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
466ba7d49b1SJiri Pirko {
467ba7d49b1SJiri Pirko 	struct net_device *master_dev;
468ba7d49b1SJiri Pirko 	const struct rtnl_link_ops *ops;
4698515ae38SFlorian Westphal 	size_t size = 0;
470ba7d49b1SJiri Pirko 
4718515ae38SFlorian Westphal 	rcu_read_lock();
4728515ae38SFlorian Westphal 
4738515ae38SFlorian Westphal 	master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
474ba7d49b1SJiri Pirko 	if (!master_dev)
4758515ae38SFlorian Westphal 		goto out;
4768515ae38SFlorian Westphal 
477ba7d49b1SJiri Pirko 	ops = master_dev->rtnl_link_ops;
4786049f253SFernando Luis Vazquez Cao 	if (!ops || !ops->get_slave_size)
4798515ae38SFlorian Westphal 		goto out;
480ba7d49b1SJiri Pirko 	/* IFLA_INFO_SLAVE_DATA + nested data */
4818515ae38SFlorian Westphal 	size = nla_total_size(sizeof(struct nlattr)) +
482ba7d49b1SJiri Pirko 	       ops->get_slave_size(master_dev, dev);
4838515ae38SFlorian Westphal 
4848515ae38SFlorian Westphal out:
4858515ae38SFlorian Westphal 	rcu_read_unlock();
4868515ae38SFlorian Westphal 	return size;
487ba7d49b1SJiri Pirko }
488ba7d49b1SJiri Pirko 
48938f7b870SPatrick McHardy static size_t rtnl_link_get_size(const struct net_device *dev)
49038f7b870SPatrick McHardy {
49138f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
49238f7b870SPatrick McHardy 	size_t size;
49338f7b870SPatrick McHardy 
49438f7b870SPatrick McHardy 	if (!ops)
49538f7b870SPatrick McHardy 		return 0;
49638f7b870SPatrick McHardy 
497369cf77aSThomas Graf 	size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
498369cf77aSThomas Graf 	       nla_total_size(strlen(ops->kind) + 1);  /* IFLA_INFO_KIND */
49938f7b870SPatrick McHardy 
50038f7b870SPatrick McHardy 	if (ops->get_size)
50138f7b870SPatrick McHardy 		/* IFLA_INFO_DATA + nested data */
502369cf77aSThomas Graf 		size += nla_total_size(sizeof(struct nlattr)) +
50338f7b870SPatrick McHardy 			ops->get_size(dev);
50438f7b870SPatrick McHardy 
50538f7b870SPatrick McHardy 	if (ops->get_xstats_size)
506369cf77aSThomas Graf 		/* IFLA_INFO_XSTATS */
507369cf77aSThomas Graf 		size += nla_total_size(ops->get_xstats_size(dev));
50838f7b870SPatrick McHardy 
509ba7d49b1SJiri Pirko 	size += rtnl_link_get_slave_info_data_size(dev);
510ba7d49b1SJiri Pirko 
51138f7b870SPatrick McHardy 	return size;
51238f7b870SPatrick McHardy }
51338f7b870SPatrick McHardy 
514f8ff182cSThomas Graf static LIST_HEAD(rtnl_af_ops);
515f8ff182cSThomas Graf 
516f8ff182cSThomas Graf static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
517f8ff182cSThomas Graf {
518f8ff182cSThomas Graf 	const struct rtnl_af_ops *ops;
519f8ff182cSThomas Graf 
5205fa85a09SFlorian Westphal 	list_for_each_entry_rcu(ops, &rtnl_af_ops, list) {
521f8ff182cSThomas Graf 		if (ops->family == family)
522f8ff182cSThomas Graf 			return ops;
523f8ff182cSThomas Graf 	}
524f8ff182cSThomas Graf 
525f8ff182cSThomas Graf 	return NULL;
526f8ff182cSThomas Graf }
527f8ff182cSThomas Graf 
528f8ff182cSThomas Graf /**
529f8ff182cSThomas Graf  * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
530f8ff182cSThomas Graf  * @ops: struct rtnl_af_ops * to register
531f8ff182cSThomas Graf  *
532f8ff182cSThomas Graf  * Returns 0 on success or a negative error code.
533f8ff182cSThomas Graf  */
5343678a9d8Sstephen hemminger void rtnl_af_register(struct rtnl_af_ops *ops)
535f8ff182cSThomas Graf {
536f8ff182cSThomas Graf 	rtnl_lock();
5375fa85a09SFlorian Westphal 	list_add_tail_rcu(&ops->list, &rtnl_af_ops);
538f8ff182cSThomas Graf 	rtnl_unlock();
539f8ff182cSThomas Graf }
540f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_register);
541f8ff182cSThomas Graf 
542f8ff182cSThomas Graf /**
543f8ff182cSThomas Graf  * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
544f8ff182cSThomas Graf  * @ops: struct rtnl_af_ops * to unregister
545f8ff182cSThomas Graf  */
546f8ff182cSThomas Graf void rtnl_af_unregister(struct rtnl_af_ops *ops)
547f8ff182cSThomas Graf {
548f8ff182cSThomas Graf 	rtnl_lock();
5495fa85a09SFlorian Westphal 	list_del_rcu(&ops->list);
550f8ff182cSThomas Graf 	rtnl_unlock();
5515fa85a09SFlorian Westphal 
5525fa85a09SFlorian Westphal 	synchronize_rcu();
553f8ff182cSThomas Graf }
554f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_unregister);
555f8ff182cSThomas Graf 
556b1974ed0SArad, Ronen static size_t rtnl_link_get_af_size(const struct net_device *dev,
557b1974ed0SArad, Ronen 				    u32 ext_filter_mask)
558f8ff182cSThomas Graf {
559f8ff182cSThomas Graf 	struct rtnl_af_ops *af_ops;
560f8ff182cSThomas Graf 	size_t size;
561f8ff182cSThomas Graf 
562f8ff182cSThomas Graf 	/* IFLA_AF_SPEC */
563f8ff182cSThomas Graf 	size = nla_total_size(sizeof(struct nlattr));
564f8ff182cSThomas Graf 
5655fa85a09SFlorian Westphal 	rcu_read_lock();
5665fa85a09SFlorian Westphal 	list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
567f8ff182cSThomas Graf 		if (af_ops->get_link_af_size) {
568f8ff182cSThomas Graf 			/* AF_* + nested data */
569f8ff182cSThomas Graf 			size += nla_total_size(sizeof(struct nlattr)) +
570b1974ed0SArad, Ronen 				af_ops->get_link_af_size(dev, ext_filter_mask);
571f8ff182cSThomas Graf 		}
572f8ff182cSThomas Graf 	}
5735fa85a09SFlorian Westphal 	rcu_read_unlock();
574f8ff182cSThomas Graf 
575f8ff182cSThomas Graf 	return size;
576f8ff182cSThomas Graf }
577f8ff182cSThomas Graf 
578ba7d49b1SJiri Pirko static bool rtnl_have_link_slave_info(const struct net_device *dev)
579ba7d49b1SJiri Pirko {
580ba7d49b1SJiri Pirko 	struct net_device *master_dev;
5814c82a95eSFlorian Westphal 	bool ret = false;
582ba7d49b1SJiri Pirko 
5834c82a95eSFlorian Westphal 	rcu_read_lock();
5844c82a95eSFlorian Westphal 
5854c82a95eSFlorian Westphal 	master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
586813f020cSJiri Pirko 	if (master_dev && master_dev->rtnl_link_ops)
5874c82a95eSFlorian Westphal 		ret = true;
5884c82a95eSFlorian Westphal 	rcu_read_unlock();
5894c82a95eSFlorian Westphal 	return ret;
590ba7d49b1SJiri Pirko }
591ba7d49b1SJiri Pirko 
592ba7d49b1SJiri Pirko static int rtnl_link_slave_info_fill(struct sk_buff *skb,
593ba7d49b1SJiri Pirko 				     const struct net_device *dev)
594ba7d49b1SJiri Pirko {
595ba7d49b1SJiri Pirko 	struct net_device *master_dev;
596ba7d49b1SJiri Pirko 	const struct rtnl_link_ops *ops;
597ba7d49b1SJiri Pirko 	struct nlattr *slave_data;
598ba7d49b1SJiri Pirko 	int err;
599ba7d49b1SJiri Pirko 
600ba7d49b1SJiri Pirko 	master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
601ba7d49b1SJiri Pirko 	if (!master_dev)
602ba7d49b1SJiri Pirko 		return 0;
603ba7d49b1SJiri Pirko 	ops = master_dev->rtnl_link_ops;
604ba7d49b1SJiri Pirko 	if (!ops)
605ba7d49b1SJiri Pirko 		return 0;
606ba7d49b1SJiri Pirko 	if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
607ba7d49b1SJiri Pirko 		return -EMSGSIZE;
608ba7d49b1SJiri Pirko 	if (ops->fill_slave_info) {
609ba7d49b1SJiri Pirko 		slave_data = nla_nest_start(skb, IFLA_INFO_SLAVE_DATA);
610ba7d49b1SJiri Pirko 		if (!slave_data)
611ba7d49b1SJiri Pirko 			return -EMSGSIZE;
612ba7d49b1SJiri Pirko 		err = ops->fill_slave_info(skb, master_dev, dev);
613ba7d49b1SJiri Pirko 		if (err < 0)
614ba7d49b1SJiri Pirko 			goto err_cancel_slave_data;
615ba7d49b1SJiri Pirko 		nla_nest_end(skb, slave_data);
616ba7d49b1SJiri Pirko 	}
617ba7d49b1SJiri Pirko 	return 0;
618ba7d49b1SJiri Pirko 
619ba7d49b1SJiri Pirko err_cancel_slave_data:
620ba7d49b1SJiri Pirko 	nla_nest_cancel(skb, slave_data);
621ba7d49b1SJiri Pirko 	return err;
622ba7d49b1SJiri Pirko }
623ba7d49b1SJiri Pirko 
624ba7d49b1SJiri Pirko static int rtnl_link_info_fill(struct sk_buff *skb,
625ba7d49b1SJiri Pirko 			       const struct net_device *dev)
62638f7b870SPatrick McHardy {
62738f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
628ba7d49b1SJiri Pirko 	struct nlattr *data;
629ba7d49b1SJiri Pirko 	int err;
630ba7d49b1SJiri Pirko 
631ba7d49b1SJiri Pirko 	if (!ops)
632ba7d49b1SJiri Pirko 		return 0;
633ba7d49b1SJiri Pirko 	if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
634ba7d49b1SJiri Pirko 		return -EMSGSIZE;
635ba7d49b1SJiri Pirko 	if (ops->fill_xstats) {
636ba7d49b1SJiri Pirko 		err = ops->fill_xstats(skb, dev);
637ba7d49b1SJiri Pirko 		if (err < 0)
638ba7d49b1SJiri Pirko 			return err;
639ba7d49b1SJiri Pirko 	}
640ba7d49b1SJiri Pirko 	if (ops->fill_info) {
641ba7d49b1SJiri Pirko 		data = nla_nest_start(skb, IFLA_INFO_DATA);
642ba7d49b1SJiri Pirko 		if (data == NULL)
643ba7d49b1SJiri Pirko 			return -EMSGSIZE;
644ba7d49b1SJiri Pirko 		err = ops->fill_info(skb, dev);
645ba7d49b1SJiri Pirko 		if (err < 0)
646ba7d49b1SJiri Pirko 			goto err_cancel_data;
647ba7d49b1SJiri Pirko 		nla_nest_end(skb, data);
648ba7d49b1SJiri Pirko 	}
649ba7d49b1SJiri Pirko 	return 0;
650ba7d49b1SJiri Pirko 
651ba7d49b1SJiri Pirko err_cancel_data:
652ba7d49b1SJiri Pirko 	nla_nest_cancel(skb, data);
653ba7d49b1SJiri Pirko 	return err;
654ba7d49b1SJiri Pirko }
655ba7d49b1SJiri Pirko 
656ba7d49b1SJiri Pirko static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
657ba7d49b1SJiri Pirko {
658ba7d49b1SJiri Pirko 	struct nlattr *linkinfo;
65938f7b870SPatrick McHardy 	int err = -EMSGSIZE;
66038f7b870SPatrick McHardy 
66138f7b870SPatrick McHardy 	linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
66238f7b870SPatrick McHardy 	if (linkinfo == NULL)
66338f7b870SPatrick McHardy 		goto out;
66438f7b870SPatrick McHardy 
665ba7d49b1SJiri Pirko 	err = rtnl_link_info_fill(skb, dev);
66638f7b870SPatrick McHardy 	if (err < 0)
66738f7b870SPatrick McHardy 		goto err_cancel_link;
668ba7d49b1SJiri Pirko 
669ba7d49b1SJiri Pirko 	err = rtnl_link_slave_info_fill(skb, dev);
67038f7b870SPatrick McHardy 	if (err < 0)
671ba7d49b1SJiri Pirko 		goto err_cancel_link;
67238f7b870SPatrick McHardy 
67338f7b870SPatrick McHardy 	nla_nest_end(skb, linkinfo);
67438f7b870SPatrick McHardy 	return 0;
67538f7b870SPatrick McHardy 
67638f7b870SPatrick McHardy err_cancel_link:
67738f7b870SPatrick McHardy 	nla_nest_cancel(skb, linkinfo);
67838f7b870SPatrick McHardy out:
67938f7b870SPatrick McHardy 	return err;
68038f7b870SPatrick McHardy }
68138f7b870SPatrick McHardy 
68295c96174SEric Dumazet int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
6831da177e4SLinus Torvalds {
68497c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
6851da177e4SLinus Torvalds 	int err = 0;
6861da177e4SLinus Torvalds 
687ac6d439dSPatrick McHardy 	NETLINK_CB(skb).dst_group = group;
6881da177e4SLinus Torvalds 	if (echo)
68963354797SReshetova, Elena 		refcount_inc(&skb->users);
6901da177e4SLinus Torvalds 	netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
6911da177e4SLinus Torvalds 	if (echo)
6921da177e4SLinus Torvalds 		err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
6931da177e4SLinus Torvalds 	return err;
6941da177e4SLinus Torvalds }
6951da177e4SLinus Torvalds 
69697c53cacSDenis V. Lunev int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
6972942e900SThomas Graf {
69897c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
69997c53cacSDenis V. Lunev 
7002942e900SThomas Graf 	return nlmsg_unicast(rtnl, skb, pid);
7012942e900SThomas Graf }
702e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unicast);
7032942e900SThomas Graf 
7041ce85fe4SPablo Neira Ayuso void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
70597676b6bSThomas Graf 		 struct nlmsghdr *nlh, gfp_t flags)
70697676b6bSThomas Graf {
70797c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
70897676b6bSThomas Graf 	int report = 0;
70997676b6bSThomas Graf 
71097676b6bSThomas Graf 	if (nlh)
71197676b6bSThomas Graf 		report = nlmsg_report(nlh);
71297676b6bSThomas Graf 
7131ce85fe4SPablo Neira Ayuso 	nlmsg_notify(rtnl, skb, pid, group, report, flags);
71497676b6bSThomas Graf }
715e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_notify);
71697676b6bSThomas Graf 
71797c53cacSDenis V. Lunev void rtnl_set_sk_err(struct net *net, u32 group, int error)
71897676b6bSThomas Graf {
71997c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
72097c53cacSDenis V. Lunev 
72197676b6bSThomas Graf 	netlink_set_err(rtnl, 0, group, error);
72297676b6bSThomas Graf }
723e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_set_sk_err);
72497676b6bSThomas Graf 
7251da177e4SLinus Torvalds int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
7261da177e4SLinus Torvalds {
7272d7202bfSThomas Graf 	struct nlattr *mx;
7282d7202bfSThomas Graf 	int i, valid = 0;
7291da177e4SLinus Torvalds 
7302d7202bfSThomas Graf 	mx = nla_nest_start(skb, RTA_METRICS);
7312d7202bfSThomas Graf 	if (mx == NULL)
7322d7202bfSThomas Graf 		return -ENOBUFS;
7332d7202bfSThomas Graf 
7341da177e4SLinus Torvalds 	for (i = 0; i < RTAX_MAX; i++) {
7352d7202bfSThomas Graf 		if (metrics[i]) {
736ea697639SDaniel Borkmann 			if (i == RTAX_CC_ALGO - 1) {
737ea697639SDaniel Borkmann 				char tmp[TCP_CA_NAME_MAX], *name;
738ea697639SDaniel Borkmann 
739ea697639SDaniel Borkmann 				name = tcp_ca_get_name_by_key(metrics[i], tmp);
740ea697639SDaniel Borkmann 				if (!name)
741ea697639SDaniel Borkmann 					continue;
742ea697639SDaniel Borkmann 				if (nla_put_string(skb, i + 1, name))
743ea697639SDaniel Borkmann 					goto nla_put_failure;
744c3a8d947SDaniel Borkmann 			} else if (i == RTAX_FEATURES - 1) {
745c3a8d947SDaniel Borkmann 				u32 user_features = metrics[i] & RTAX_FEATURE_MASK;
746c3a8d947SDaniel Borkmann 
747f8edcd12SPhil Sutter 				if (!user_features)
748f8edcd12SPhil Sutter 					continue;
749c3a8d947SDaniel Borkmann 				BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK);
750c3a8d947SDaniel Borkmann 				if (nla_put_u32(skb, i + 1, user_features))
751c3a8d947SDaniel Borkmann 					goto nla_put_failure;
752ea697639SDaniel Borkmann 			} else {
753a6574349SDavid S. Miller 				if (nla_put_u32(skb, i + 1, metrics[i]))
754a6574349SDavid S. Miller 					goto nla_put_failure;
7551da177e4SLinus Torvalds 			}
756ea697639SDaniel Borkmann 			valid++;
757ea697639SDaniel Borkmann 		}
7582d7202bfSThomas Graf 	}
7591da177e4SLinus Torvalds 
760a57d27fcSDavid S. Miller 	if (!valid) {
761a57d27fcSDavid S. Miller 		nla_nest_cancel(skb, mx);
762a57d27fcSDavid S. Miller 		return 0;
763a57d27fcSDavid S. Miller 	}
7642d7202bfSThomas Graf 
7652d7202bfSThomas Graf 	return nla_nest_end(skb, mx);
7662d7202bfSThomas Graf 
7672d7202bfSThomas Graf nla_put_failure:
768bc3ed28cSThomas Graf 	nla_nest_cancel(skb, mx);
769bc3ed28cSThomas Graf 	return -EMSGSIZE;
7701da177e4SLinus Torvalds }
771e0d087afSEric Dumazet EXPORT_SYMBOL(rtnetlink_put_metrics);
7721da177e4SLinus Torvalds 
773e3703b3dSThomas Graf int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
77487a50699SDavid S. Miller 		       long expires, u32 error)
775e3703b3dSThomas Graf {
776e3703b3dSThomas Graf 	struct rta_cacheinfo ci = {
777a399a805SEric Dumazet 		.rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse),
778e3703b3dSThomas Graf 		.rta_used = dst->__use,
779e3703b3dSThomas Graf 		.rta_clntref = atomic_read(&(dst->__refcnt)),
780e3703b3dSThomas Graf 		.rta_error = error,
781e3703b3dSThomas Graf 		.rta_id =  id,
782e3703b3dSThomas Graf 	};
783e3703b3dSThomas Graf 
7848253947eSLi Wei 	if (expires) {
7858253947eSLi Wei 		unsigned long clock;
786e3703b3dSThomas Graf 
7878253947eSLi Wei 		clock = jiffies_to_clock_t(abs(expires));
7888253947eSLi Wei 		clock = min_t(unsigned long, clock, INT_MAX);
7898253947eSLi Wei 		ci.rta_expires = (expires > 0) ? clock : -clock;
7908253947eSLi Wei 	}
791e3703b3dSThomas Graf 	return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
792e3703b3dSThomas Graf }
793e3703b3dSThomas Graf EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
7941da177e4SLinus Torvalds 
79593b2d4a2SDavid S. Miller static void set_operstate(struct net_device *dev, unsigned char transition)
796b00055aaSStefan Rompf {
797b00055aaSStefan Rompf 	unsigned char operstate = dev->operstate;
798b00055aaSStefan Rompf 
799b00055aaSStefan Rompf 	switch (transition) {
800b00055aaSStefan Rompf 	case IF_OPER_UP:
801b00055aaSStefan Rompf 		if ((operstate == IF_OPER_DORMANT ||
802b00055aaSStefan Rompf 		     operstate == IF_OPER_UNKNOWN) &&
803b00055aaSStefan Rompf 		    !netif_dormant(dev))
804b00055aaSStefan Rompf 			operstate = IF_OPER_UP;
805b00055aaSStefan Rompf 		break;
806b00055aaSStefan Rompf 
807b00055aaSStefan Rompf 	case IF_OPER_DORMANT:
808b00055aaSStefan Rompf 		if (operstate == IF_OPER_UP ||
809b00055aaSStefan Rompf 		    operstate == IF_OPER_UNKNOWN)
810b00055aaSStefan Rompf 			operstate = IF_OPER_DORMANT;
811b00055aaSStefan Rompf 		break;
8123ff50b79SStephen Hemminger 	}
813b00055aaSStefan Rompf 
814b00055aaSStefan Rompf 	if (dev->operstate != operstate) {
815b00055aaSStefan Rompf 		write_lock_bh(&dev_base_lock);
816b00055aaSStefan Rompf 		dev->operstate = operstate;
817b00055aaSStefan Rompf 		write_unlock_bh(&dev_base_lock);
818b00055aaSStefan Rompf 		netdev_state_change(dev);
81993b2d4a2SDavid S. Miller 	}
820b00055aaSStefan Rompf }
821b00055aaSStefan Rompf 
822b1beb681SJiri Benc static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
823b1beb681SJiri Benc {
824b1beb681SJiri Benc 	return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
825b1beb681SJiri Benc 	       (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
826b1beb681SJiri Benc }
827b1beb681SJiri Benc 
8283729d502SPatrick McHardy static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
8293729d502SPatrick McHardy 					   const struct ifinfomsg *ifm)
8303729d502SPatrick McHardy {
8313729d502SPatrick McHardy 	unsigned int flags = ifm->ifi_flags;
8323729d502SPatrick McHardy 
8333729d502SPatrick McHardy 	/* bugwards compatibility: ifi_change == 0 is treated as ~0 */
8343729d502SPatrick McHardy 	if (ifm->ifi_change)
8353729d502SPatrick McHardy 		flags = (flags & ifm->ifi_change) |
836b1beb681SJiri Benc 			(rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
8373729d502SPatrick McHardy 
8383729d502SPatrick McHardy 	return flags;
8393729d502SPatrick McHardy }
8403729d502SPatrick McHardy 
841b60c5115SThomas Graf static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
842be1f3c2cSBen Hutchings 				 const struct rtnl_link_stats64 *b)
8431da177e4SLinus Torvalds {
844b60c5115SThomas Graf 	a->rx_packets = b->rx_packets;
845b60c5115SThomas Graf 	a->tx_packets = b->tx_packets;
846b60c5115SThomas Graf 	a->rx_bytes = b->rx_bytes;
847b60c5115SThomas Graf 	a->tx_bytes = b->tx_bytes;
848b60c5115SThomas Graf 	a->rx_errors = b->rx_errors;
849b60c5115SThomas Graf 	a->tx_errors = b->tx_errors;
850b60c5115SThomas Graf 	a->rx_dropped = b->rx_dropped;
851b60c5115SThomas Graf 	a->tx_dropped = b->tx_dropped;
852b60c5115SThomas Graf 
853b60c5115SThomas Graf 	a->multicast = b->multicast;
854b60c5115SThomas Graf 	a->collisions = b->collisions;
855b60c5115SThomas Graf 
856b60c5115SThomas Graf 	a->rx_length_errors = b->rx_length_errors;
857b60c5115SThomas Graf 	a->rx_over_errors = b->rx_over_errors;
858b60c5115SThomas Graf 	a->rx_crc_errors = b->rx_crc_errors;
859b60c5115SThomas Graf 	a->rx_frame_errors = b->rx_frame_errors;
860b60c5115SThomas Graf 	a->rx_fifo_errors = b->rx_fifo_errors;
861b60c5115SThomas Graf 	a->rx_missed_errors = b->rx_missed_errors;
862b60c5115SThomas Graf 
863b60c5115SThomas Graf 	a->tx_aborted_errors = b->tx_aborted_errors;
864b60c5115SThomas Graf 	a->tx_carrier_errors = b->tx_carrier_errors;
865b60c5115SThomas Graf 	a->tx_fifo_errors = b->tx_fifo_errors;
866b60c5115SThomas Graf 	a->tx_heartbeat_errors = b->tx_heartbeat_errors;
867b60c5115SThomas Graf 	a->tx_window_errors = b->tx_window_errors;
868b60c5115SThomas Graf 
869b60c5115SThomas Graf 	a->rx_compressed = b->rx_compressed;
870b60c5115SThomas Graf 	a->tx_compressed = b->tx_compressed;
8716e7333d3SJarod Wilson 
8726e7333d3SJarod Wilson 	a->rx_nohandler = b->rx_nohandler;
87310708f37SJan Engelhardt }
87410708f37SJan Engelhardt 
875c02db8c6SChris Wright /* All VF info */
876115c9b81SGreg Rose static inline int rtnl_vfinfo_size(const struct net_device *dev,
877115c9b81SGreg Rose 				   u32 ext_filter_mask)
878ebc08a6fSWilliams, Mitch A {
8799af15c38SPhil Sutter 	if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) {
880c02db8c6SChris Wright 		int num_vfs = dev_num_vf(dev->dev.parent);
8817e75f74aSSabrina Dubroca 		size_t size = nla_total_size(0);
882045de01aSScott Feldman 		size += num_vfs *
8837e75f74aSSabrina Dubroca 			(nla_total_size(0) +
8847e75f74aSSabrina Dubroca 			 nla_total_size(sizeof(struct ifla_vf_mac)) +
8857e75f74aSSabrina Dubroca 			 nla_total_size(sizeof(struct ifla_vf_vlan)) +
8867e75f74aSSabrina Dubroca 			 nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
88779aab093SMoshe Shemesh 			 nla_total_size(MAX_VLAN_LIST_LEN *
88879aab093SMoshe Shemesh 					sizeof(struct ifla_vf_vlan_info)) +
889ed616689SSucheta Chakraborty 			 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
8907e75f74aSSabrina Dubroca 			 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
891945a3676SJiri Benc 			 nla_total_size(sizeof(struct ifla_vf_rate)) +
89201a3d796SVlad Zolotarov 			 nla_total_size(sizeof(struct ifla_vf_link_state)) +
8933b766cd8SEran Ben Elisha 			 nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
8947e75f74aSSabrina Dubroca 			 nla_total_size(0) + /* nest IFLA_VF_STATS */
8953b766cd8SEran Ben Elisha 			 /* IFLA_VF_STATS_RX_PACKETS */
896343a6d8eSNicolas Dichtel 			 nla_total_size_64bit(sizeof(__u64)) +
8973b766cd8SEran Ben Elisha 			 /* IFLA_VF_STATS_TX_PACKETS */
898343a6d8eSNicolas Dichtel 			 nla_total_size_64bit(sizeof(__u64)) +
8993b766cd8SEran Ben Elisha 			 /* IFLA_VF_STATS_RX_BYTES */
900343a6d8eSNicolas Dichtel 			 nla_total_size_64bit(sizeof(__u64)) +
9013b766cd8SEran Ben Elisha 			 /* IFLA_VF_STATS_TX_BYTES */
902343a6d8eSNicolas Dichtel 			 nla_total_size_64bit(sizeof(__u64)) +
9033b766cd8SEran Ben Elisha 			 /* IFLA_VF_STATS_BROADCAST */
904343a6d8eSNicolas Dichtel 			 nla_total_size_64bit(sizeof(__u64)) +
9053b766cd8SEran Ben Elisha 			 /* IFLA_VF_STATS_MULTICAST */
906343a6d8eSNicolas Dichtel 			 nla_total_size_64bit(sizeof(__u64)) +
907c5a9f6f0SEugenia Emantayev 			 /* IFLA_VF_STATS_RX_DROPPED */
908c5a9f6f0SEugenia Emantayev 			 nla_total_size_64bit(sizeof(__u64)) +
909c5a9f6f0SEugenia Emantayev 			 /* IFLA_VF_STATS_TX_DROPPED */
910c5a9f6f0SEugenia Emantayev 			 nla_total_size_64bit(sizeof(__u64)) +
911dd461d6aSHiroshi Shimamoto 			 nla_total_size(sizeof(struct ifla_vf_trust)));
912c02db8c6SChris Wright 		return size;
913c02db8c6SChris Wright 	} else
914ebc08a6fSWilliams, Mitch A 		return 0;
915ebc08a6fSWilliams, Mitch A }
916ebc08a6fSWilliams, Mitch A 
917c53864fdSDavid Gibson static size_t rtnl_port_size(const struct net_device *dev,
918c53864fdSDavid Gibson 			     u32 ext_filter_mask)
91957b61080SScott Feldman {
92057b61080SScott Feldman 	size_t port_size = nla_total_size(4)		/* PORT_VF */
92157b61080SScott Feldman 		+ nla_total_size(PORT_PROFILE_MAX)	/* PORT_PROFILE */
92257b61080SScott Feldman 		+ nla_total_size(PORT_UUID_MAX)		/* PORT_INSTANCE_UUID */
92357b61080SScott Feldman 		+ nla_total_size(PORT_UUID_MAX)		/* PORT_HOST_UUID */
92457b61080SScott Feldman 		+ nla_total_size(1)			/* PROT_VDP_REQUEST */
92557b61080SScott Feldman 		+ nla_total_size(2);			/* PORT_VDP_RESPONSE */
92657b61080SScott Feldman 	size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
92757b61080SScott Feldman 	size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
92857b61080SScott Feldman 		+ port_size;
92957b61080SScott Feldman 	size_t port_self_size = nla_total_size(sizeof(struct nlattr))
93057b61080SScott Feldman 		+ port_size;
93157b61080SScott Feldman 
932c53864fdSDavid Gibson 	if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
933c53864fdSDavid Gibson 	    !(ext_filter_mask & RTEXT_FILTER_VF))
93457b61080SScott Feldman 		return 0;
93557b61080SScott Feldman 	if (dev_num_vf(dev->dev.parent))
93657b61080SScott Feldman 		return port_self_size + vf_ports_size +
93757b61080SScott Feldman 			vf_port_size * dev_num_vf(dev->dev.parent);
93857b61080SScott Feldman 	else
93957b61080SScott Feldman 		return port_self_size;
94057b61080SScott Feldman }
94157b61080SScott Feldman 
942b5cdae32SDavid S. Miller static size_t rtnl_xdp_size(void)
943d1fdd913SBrenden Blanco {
944b3cfaa31SSabrina Dubroca 	size_t xdp_size = nla_total_size(0) +	/* nest IFLA_XDP */
94558038695SMartin KaFai Lau 			  nla_total_size(1) +	/* XDP_ATTACHED */
94658038695SMartin KaFai Lau 			  nla_total_size(4);	/* XDP_PROG_ID */
947d1fdd913SBrenden Blanco 
948d1fdd913SBrenden Blanco 	return xdp_size;
949d1fdd913SBrenden Blanco }
950d1fdd913SBrenden Blanco 
951115c9b81SGreg Rose static noinline size_t if_nlmsg_size(const struct net_device *dev,
952115c9b81SGreg Rose 				     u32 ext_filter_mask)
953339bf98fSThomas Graf {
954339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
955339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
9560b815a1aSStephen Hemminger 	       + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
957339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
958270cb4d0SNicolas Dichtel 	       + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap))
959339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rtnl_link_stats))
96035c58459SDavid S. Miller 	       + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
961339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
962339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
963339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_TXQLEN */
964339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_WEIGHT */
965339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MTU */
966339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_LINK */
967339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MASTER */
9689a57247fSJiri Pirko 	       + nla_total_size(1) /* IFLA_CARRIER */
969edbc0bb3SBen Greear 	       + nla_total_size(4) /* IFLA_PROMISCUITY */
97076ff5cc9SJiri Pirko 	       + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
97176ff5cc9SJiri Pirko 	       + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
9726919756cSTobias Klauser 	       + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
9736919756cSTobias Klauser 	       + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
974339bf98fSThomas Graf 	       + nla_total_size(1) /* IFLA_OPERSTATE */
97538f7b870SPatrick McHardy 	       + nla_total_size(1) /* IFLA_LINKMODE */
9762d3b479dSdavid decotigny 	       + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
977d37512a2SNicolas Dichtel 	       + nla_total_size(4) /* IFLA_LINK_NETNSID */
978db833d40SSerhey Popovych 	       + nla_total_size(4) /* IFLA_GROUP */
979115c9b81SGreg Rose 	       + nla_total_size(ext_filter_mask
980115c9b81SGreg Rose 			        & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
981115c9b81SGreg Rose 	       + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
982c53864fdSDavid Gibson 	       + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
983f8ff182cSThomas Graf 	       + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
984b1974ed0SArad, Ronen 	       + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
98582f28412SJiri Pirko 	       + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
98688d6378bSAnuradha Karuppiah 	       + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
987c57c7a95SNicolas Dichtel 	       + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
988b5cdae32SDavid S. Miller 	       + rtnl_xdp_size() /* IFLA_XDP */
9893d3ea5afSVlad Yasevich 	       + nla_total_size(4)  /* IFLA_EVENT */
9906621dd29SNicolas Dichtel 	       + nla_total_size(4)  /* IFLA_NEW_NETNSID */
99103ac738dSColin Ian King 	       + nla_total_size(1)  /* IFLA_PROTO_DOWN */
99279e1ad14SJiri Benc 	       + nla_total_size(4)  /* IFLA_IF_NETNSID */
993*b2d3bcfaSDavid Decotigny 	       + nla_total_size(4)  /* IFLA_CARRIER_UP_COUNT */
994*b2d3bcfaSDavid Decotigny 	       + nla_total_size(4)  /* IFLA_CARRIER_DOWN_COUNT */
99579e1ad14SJiri Benc 	       + 0;
996339bf98fSThomas Graf }
997339bf98fSThomas Graf 
99857b61080SScott Feldman static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
99957b61080SScott Feldman {
100057b61080SScott Feldman 	struct nlattr *vf_ports;
100157b61080SScott Feldman 	struct nlattr *vf_port;
100257b61080SScott Feldman 	int vf;
100357b61080SScott Feldman 	int err;
100457b61080SScott Feldman 
100557b61080SScott Feldman 	vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
100657b61080SScott Feldman 	if (!vf_ports)
100757b61080SScott Feldman 		return -EMSGSIZE;
100857b61080SScott Feldman 
100957b61080SScott Feldman 	for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
101057b61080SScott Feldman 		vf_port = nla_nest_start(skb, IFLA_VF_PORT);
10118ca94183SScott Feldman 		if (!vf_port)
10128ca94183SScott Feldman 			goto nla_put_failure;
1013a6574349SDavid S. Miller 		if (nla_put_u32(skb, IFLA_PORT_VF, vf))
1014a6574349SDavid S. Miller 			goto nla_put_failure;
101557b61080SScott Feldman 		err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
10168ca94183SScott Feldman 		if (err == -EMSGSIZE)
10178ca94183SScott Feldman 			goto nla_put_failure;
101857b61080SScott Feldman 		if (err) {
101957b61080SScott Feldman 			nla_nest_cancel(skb, vf_port);
102057b61080SScott Feldman 			continue;
102157b61080SScott Feldman 		}
102257b61080SScott Feldman 		nla_nest_end(skb, vf_port);
102357b61080SScott Feldman 	}
102457b61080SScott Feldman 
102557b61080SScott Feldman 	nla_nest_end(skb, vf_ports);
102657b61080SScott Feldman 
102757b61080SScott Feldman 	return 0;
10288ca94183SScott Feldman 
10298ca94183SScott Feldman nla_put_failure:
10308ca94183SScott Feldman 	nla_nest_cancel(skb, vf_ports);
10318ca94183SScott Feldman 	return -EMSGSIZE;
103257b61080SScott Feldman }
103357b61080SScott Feldman 
103457b61080SScott Feldman static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
103557b61080SScott Feldman {
103657b61080SScott Feldman 	struct nlattr *port_self;
103757b61080SScott Feldman 	int err;
103857b61080SScott Feldman 
103957b61080SScott Feldman 	port_self = nla_nest_start(skb, IFLA_PORT_SELF);
104057b61080SScott Feldman 	if (!port_self)
104157b61080SScott Feldman 		return -EMSGSIZE;
104257b61080SScott Feldman 
104357b61080SScott Feldman 	err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
104457b61080SScott Feldman 	if (err) {
104557b61080SScott Feldman 		nla_nest_cancel(skb, port_self);
10468ca94183SScott Feldman 		return (err == -EMSGSIZE) ? err : 0;
104757b61080SScott Feldman 	}
104857b61080SScott Feldman 
104957b61080SScott Feldman 	nla_nest_end(skb, port_self);
105057b61080SScott Feldman 
105157b61080SScott Feldman 	return 0;
105257b61080SScott Feldman }
105357b61080SScott Feldman 
1054c53864fdSDavid Gibson static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
1055c53864fdSDavid Gibson 			  u32 ext_filter_mask)
105657b61080SScott Feldman {
105757b61080SScott Feldman 	int err;
105857b61080SScott Feldman 
1059c53864fdSDavid Gibson 	if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
1060c53864fdSDavid Gibson 	    !(ext_filter_mask & RTEXT_FILTER_VF))
106157b61080SScott Feldman 		return 0;
106257b61080SScott Feldman 
106357b61080SScott Feldman 	err = rtnl_port_self_fill(skb, dev);
106457b61080SScott Feldman 	if (err)
106557b61080SScott Feldman 		return err;
106657b61080SScott Feldman 
106757b61080SScott Feldman 	if (dev_num_vf(dev->dev.parent)) {
106857b61080SScott Feldman 		err = rtnl_vf_ports_fill(skb, dev);
106957b61080SScott Feldman 		if (err)
107057b61080SScott Feldman 			return err;
107157b61080SScott Feldman 	}
107257b61080SScott Feldman 
107357b61080SScott Feldman 	return 0;
107457b61080SScott Feldman }
107557b61080SScott Feldman 
107666cae9edSJiri Pirko static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
107766cae9edSJiri Pirko {
107866cae9edSJiri Pirko 	int err;
107902637fceSJiri Pirko 	struct netdev_phys_item_id ppid;
108066cae9edSJiri Pirko 
108166cae9edSJiri Pirko 	err = dev_get_phys_port_id(dev, &ppid);
108266cae9edSJiri Pirko 	if (err) {
108366cae9edSJiri Pirko 		if (err == -EOPNOTSUPP)
108466cae9edSJiri Pirko 			return 0;
108566cae9edSJiri Pirko 		return err;
108666cae9edSJiri Pirko 	}
108766cae9edSJiri Pirko 
108866cae9edSJiri Pirko 	if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
108966cae9edSJiri Pirko 		return -EMSGSIZE;
109066cae9edSJiri Pirko 
109166cae9edSJiri Pirko 	return 0;
109266cae9edSJiri Pirko }
109366cae9edSJiri Pirko 
1094db24a904SDavid Ahern static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1095db24a904SDavid Ahern {
1096db24a904SDavid Ahern 	char name[IFNAMSIZ];
1097db24a904SDavid Ahern 	int err;
1098db24a904SDavid Ahern 
1099db24a904SDavid Ahern 	err = dev_get_phys_port_name(dev, name, sizeof(name));
1100db24a904SDavid Ahern 	if (err) {
1101db24a904SDavid Ahern 		if (err == -EOPNOTSUPP)
1102db24a904SDavid Ahern 			return 0;
1103db24a904SDavid Ahern 		return err;
1104db24a904SDavid Ahern 	}
1105db24a904SDavid Ahern 
110677ef033bSMichal Schmidt 	if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
1107db24a904SDavid Ahern 		return -EMSGSIZE;
1108db24a904SDavid Ahern 
1109db24a904SDavid Ahern 	return 0;
1110db24a904SDavid Ahern }
1111db24a904SDavid Ahern 
111282f28412SJiri Pirko static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
111382f28412SJiri Pirko {
111482f28412SJiri Pirko 	int err;
1115f8e20a9fSScott Feldman 	struct switchdev_attr attr = {
11166ff64f6fSIdo Schimmel 		.orig_dev = dev,
11171f868398SJiri Pirko 		.id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
1118f8e20a9fSScott Feldman 		.flags = SWITCHDEV_F_NO_RECURSE,
1119f8e20a9fSScott Feldman 	};
112082f28412SJiri Pirko 
1121f8e20a9fSScott Feldman 	err = switchdev_port_attr_get(dev, &attr);
112282f28412SJiri Pirko 	if (err) {
112382f28412SJiri Pirko 		if (err == -EOPNOTSUPP)
112482f28412SJiri Pirko 			return 0;
112582f28412SJiri Pirko 		return err;
112682f28412SJiri Pirko 	}
112782f28412SJiri Pirko 
112842275bd8SScott Feldman 	if (nla_put(skb, IFLA_PHYS_SWITCH_ID, attr.u.ppid.id_len,
112942275bd8SScott Feldman 		    attr.u.ppid.id))
113082f28412SJiri Pirko 		return -EMSGSIZE;
113182f28412SJiri Pirko 
113282f28412SJiri Pirko 	return 0;
113382f28412SJiri Pirko }
113482f28412SJiri Pirko 
1135b22b941bSHannes Frederic Sowa static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1136b22b941bSHannes Frederic Sowa 					      struct net_device *dev)
1137b22b941bSHannes Frederic Sowa {
1138550bce59SRoopa Prabhu 	struct rtnl_link_stats64 *sp;
1139b22b941bSHannes Frederic Sowa 	struct nlattr *attr;
1140b22b941bSHannes Frederic Sowa 
114158414d32SNicolas Dichtel 	attr = nla_reserve_64bit(skb, IFLA_STATS64,
114258414d32SNicolas Dichtel 				 sizeof(struct rtnl_link_stats64), IFLA_PAD);
1143b22b941bSHannes Frederic Sowa 	if (!attr)
1144b22b941bSHannes Frederic Sowa 		return -EMSGSIZE;
1145b22b941bSHannes Frederic Sowa 
1146550bce59SRoopa Prabhu 	sp = nla_data(attr);
1147550bce59SRoopa Prabhu 	dev_get_stats(dev, sp);
1148550bce59SRoopa Prabhu 
1149550bce59SRoopa Prabhu 	attr = nla_reserve(skb, IFLA_STATS,
1150550bce59SRoopa Prabhu 			   sizeof(struct rtnl_link_stats));
1151550bce59SRoopa Prabhu 	if (!attr)
1152550bce59SRoopa Prabhu 		return -EMSGSIZE;
1153550bce59SRoopa Prabhu 
1154550bce59SRoopa Prabhu 	copy_rtnl_link_stats(nla_data(attr), sp);
1155b22b941bSHannes Frederic Sowa 
1156b22b941bSHannes Frederic Sowa 	return 0;
1157b22b941bSHannes Frederic Sowa }
1158b22b941bSHannes Frederic Sowa 
1159b22b941bSHannes Frederic Sowa static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1160b22b941bSHannes Frederic Sowa 					       struct net_device *dev,
1161b22b941bSHannes Frederic Sowa 					       int vfs_num,
1162b22b941bSHannes Frederic Sowa 					       struct nlattr *vfinfo)
1163b22b941bSHannes Frederic Sowa {
1164b22b941bSHannes Frederic Sowa 	struct ifla_vf_rss_query_en vf_rss_query_en;
116579aab093SMoshe Shemesh 	struct nlattr *vf, *vfstats, *vfvlanlist;
1166b22b941bSHannes Frederic Sowa 	struct ifla_vf_link_state vf_linkstate;
116779aab093SMoshe Shemesh 	struct ifla_vf_vlan_info vf_vlan_info;
1168b22b941bSHannes Frederic Sowa 	struct ifla_vf_spoofchk vf_spoofchk;
1169b22b941bSHannes Frederic Sowa 	struct ifla_vf_tx_rate vf_tx_rate;
1170b22b941bSHannes Frederic Sowa 	struct ifla_vf_stats vf_stats;
1171b22b941bSHannes Frederic Sowa 	struct ifla_vf_trust vf_trust;
1172b22b941bSHannes Frederic Sowa 	struct ifla_vf_vlan vf_vlan;
1173b22b941bSHannes Frederic Sowa 	struct ifla_vf_rate vf_rate;
1174b22b941bSHannes Frederic Sowa 	struct ifla_vf_mac vf_mac;
1175b22b941bSHannes Frederic Sowa 	struct ifla_vf_info ivi;
1176b22b941bSHannes Frederic Sowa 
11770eed9cf5SMintz, Yuval 	memset(&ivi, 0, sizeof(ivi));
11780eed9cf5SMintz, Yuval 
1179b22b941bSHannes Frederic Sowa 	/* Not all SR-IOV capable drivers support the
1180b22b941bSHannes Frederic Sowa 	 * spoofcheck and "RSS query enable" query.  Preset to
1181b22b941bSHannes Frederic Sowa 	 * -1 so the user space tool can detect that the driver
1182b22b941bSHannes Frederic Sowa 	 * didn't report anything.
1183b22b941bSHannes Frederic Sowa 	 */
1184b22b941bSHannes Frederic Sowa 	ivi.spoofchk = -1;
1185b22b941bSHannes Frederic Sowa 	ivi.rss_query_en = -1;
1186b22b941bSHannes Frederic Sowa 	ivi.trusted = -1;
1187b22b941bSHannes Frederic Sowa 	/* The default value for VF link state is "auto"
1188b22b941bSHannes Frederic Sowa 	 * IFLA_VF_LINK_STATE_AUTO which equals zero
1189b22b941bSHannes Frederic Sowa 	 */
1190b22b941bSHannes Frederic Sowa 	ivi.linkstate = 0;
119179aab093SMoshe Shemesh 	/* VLAN Protocol by default is 802.1Q */
119279aab093SMoshe Shemesh 	ivi.vlan_proto = htons(ETH_P_8021Q);
1193b22b941bSHannes Frederic Sowa 	if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1194b22b941bSHannes Frederic Sowa 		return 0;
1195b22b941bSHannes Frederic Sowa 
1196775f4f05SDan Carpenter 	memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
1197775f4f05SDan Carpenter 
1198b22b941bSHannes Frederic Sowa 	vf_mac.vf =
1199b22b941bSHannes Frederic Sowa 		vf_vlan.vf =
120079aab093SMoshe Shemesh 		vf_vlan_info.vf =
1201b22b941bSHannes Frederic Sowa 		vf_rate.vf =
1202b22b941bSHannes Frederic Sowa 		vf_tx_rate.vf =
1203b22b941bSHannes Frederic Sowa 		vf_spoofchk.vf =
1204b22b941bSHannes Frederic Sowa 		vf_linkstate.vf =
1205b22b941bSHannes Frederic Sowa 		vf_rss_query_en.vf =
1206b22b941bSHannes Frederic Sowa 		vf_trust.vf = ivi.vf;
1207b22b941bSHannes Frederic Sowa 
1208b22b941bSHannes Frederic Sowa 	memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1209b22b941bSHannes Frederic Sowa 	vf_vlan.vlan = ivi.vlan;
1210b22b941bSHannes Frederic Sowa 	vf_vlan.qos = ivi.qos;
121179aab093SMoshe Shemesh 	vf_vlan_info.vlan = ivi.vlan;
121279aab093SMoshe Shemesh 	vf_vlan_info.qos = ivi.qos;
121379aab093SMoshe Shemesh 	vf_vlan_info.vlan_proto = ivi.vlan_proto;
1214b22b941bSHannes Frederic Sowa 	vf_tx_rate.rate = ivi.max_tx_rate;
1215b22b941bSHannes Frederic Sowa 	vf_rate.min_tx_rate = ivi.min_tx_rate;
1216b22b941bSHannes Frederic Sowa 	vf_rate.max_tx_rate = ivi.max_tx_rate;
1217b22b941bSHannes Frederic Sowa 	vf_spoofchk.setting = ivi.spoofchk;
1218b22b941bSHannes Frederic Sowa 	vf_linkstate.link_state = ivi.linkstate;
1219b22b941bSHannes Frederic Sowa 	vf_rss_query_en.setting = ivi.rss_query_en;
1220b22b941bSHannes Frederic Sowa 	vf_trust.setting = ivi.trusted;
1221b22b941bSHannes Frederic Sowa 	vf = nla_nest_start(skb, IFLA_VF_INFO);
122279aab093SMoshe Shemesh 	if (!vf)
122379aab093SMoshe Shemesh 		goto nla_put_vfinfo_failure;
1224b22b941bSHannes Frederic Sowa 	if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1225b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1226b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1227b22b941bSHannes Frederic Sowa 		    &vf_rate) ||
1228b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1229b22b941bSHannes Frederic Sowa 		    &vf_tx_rate) ||
1230b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1231b22b941bSHannes Frederic Sowa 		    &vf_spoofchk) ||
1232b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1233b22b941bSHannes Frederic Sowa 		    &vf_linkstate) ||
1234b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1235b22b941bSHannes Frederic Sowa 		    sizeof(vf_rss_query_en),
1236b22b941bSHannes Frederic Sowa 		    &vf_rss_query_en) ||
1237b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_TRUST,
1238b22b941bSHannes Frederic Sowa 		    sizeof(vf_trust), &vf_trust))
123979aab093SMoshe Shemesh 		goto nla_put_vf_failure;
124079aab093SMoshe Shemesh 	vfvlanlist = nla_nest_start(skb, IFLA_VF_VLAN_LIST);
124179aab093SMoshe Shemesh 	if (!vfvlanlist)
124279aab093SMoshe Shemesh 		goto nla_put_vf_failure;
124379aab093SMoshe Shemesh 	if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
124479aab093SMoshe Shemesh 		    &vf_vlan_info)) {
124579aab093SMoshe Shemesh 		nla_nest_cancel(skb, vfvlanlist);
124679aab093SMoshe Shemesh 		goto nla_put_vf_failure;
124779aab093SMoshe Shemesh 	}
124879aab093SMoshe Shemesh 	nla_nest_end(skb, vfvlanlist);
1249b22b941bSHannes Frederic Sowa 	memset(&vf_stats, 0, sizeof(vf_stats));
1250b22b941bSHannes Frederic Sowa 	if (dev->netdev_ops->ndo_get_vf_stats)
1251b22b941bSHannes Frederic Sowa 		dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1252b22b941bSHannes Frederic Sowa 						&vf_stats);
1253b22b941bSHannes Frederic Sowa 	vfstats = nla_nest_start(skb, IFLA_VF_STATS);
125479aab093SMoshe Shemesh 	if (!vfstats)
125579aab093SMoshe Shemesh 		goto nla_put_vf_failure;
1256343a6d8eSNicolas Dichtel 	if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1257343a6d8eSNicolas Dichtel 			      vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1258343a6d8eSNicolas Dichtel 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1259343a6d8eSNicolas Dichtel 			      vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1260343a6d8eSNicolas Dichtel 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1261343a6d8eSNicolas Dichtel 			      vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1262343a6d8eSNicolas Dichtel 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1263343a6d8eSNicolas Dichtel 			      vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1264343a6d8eSNicolas Dichtel 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1265343a6d8eSNicolas Dichtel 			      vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1266343a6d8eSNicolas Dichtel 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
1267c5a9f6f0SEugenia Emantayev 			      vf_stats.multicast, IFLA_VF_STATS_PAD) ||
1268c5a9f6f0SEugenia Emantayev 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED,
1269c5a9f6f0SEugenia Emantayev 			      vf_stats.rx_dropped, IFLA_VF_STATS_PAD) ||
1270c5a9f6f0SEugenia Emantayev 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED,
1271c5a9f6f0SEugenia Emantayev 			      vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) {
127279aab093SMoshe Shemesh 		nla_nest_cancel(skb, vfstats);
127379aab093SMoshe Shemesh 		goto nla_put_vf_failure;
127479aab093SMoshe Shemesh 	}
1275b22b941bSHannes Frederic Sowa 	nla_nest_end(skb, vfstats);
1276b22b941bSHannes Frederic Sowa 	nla_nest_end(skb, vf);
1277b22b941bSHannes Frederic Sowa 	return 0;
127879aab093SMoshe Shemesh 
127979aab093SMoshe Shemesh nla_put_vf_failure:
128079aab093SMoshe Shemesh 	nla_nest_cancel(skb, vf);
128179aab093SMoshe Shemesh nla_put_vfinfo_failure:
128279aab093SMoshe Shemesh 	nla_nest_cancel(skb, vfinfo);
128379aab093SMoshe Shemesh 	return -EMSGSIZE;
1284b22b941bSHannes Frederic Sowa }
1285b22b941bSHannes Frederic Sowa 
1286250fc3dfSFlorian Westphal static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb,
1287250fc3dfSFlorian Westphal 					   struct net_device *dev,
1288250fc3dfSFlorian Westphal 					   u32 ext_filter_mask)
1289250fc3dfSFlorian Westphal {
1290250fc3dfSFlorian Westphal 	struct nlattr *vfinfo;
1291250fc3dfSFlorian Westphal 	int i, num_vfs;
1292250fc3dfSFlorian Westphal 
1293250fc3dfSFlorian Westphal 	if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0))
1294250fc3dfSFlorian Westphal 		return 0;
1295250fc3dfSFlorian Westphal 
1296250fc3dfSFlorian Westphal 	num_vfs = dev_num_vf(dev->dev.parent);
1297250fc3dfSFlorian Westphal 	if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs))
1298250fc3dfSFlorian Westphal 		return -EMSGSIZE;
1299250fc3dfSFlorian Westphal 
1300250fc3dfSFlorian Westphal 	if (!dev->netdev_ops->ndo_get_vf_config)
1301250fc3dfSFlorian Westphal 		return 0;
1302250fc3dfSFlorian Westphal 
1303250fc3dfSFlorian Westphal 	vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1304250fc3dfSFlorian Westphal 	if (!vfinfo)
1305250fc3dfSFlorian Westphal 		return -EMSGSIZE;
1306250fc3dfSFlorian Westphal 
1307250fc3dfSFlorian Westphal 	for (i = 0; i < num_vfs; i++) {
1308250fc3dfSFlorian Westphal 		if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
1309250fc3dfSFlorian Westphal 			return -EMSGSIZE;
1310250fc3dfSFlorian Westphal 	}
1311250fc3dfSFlorian Westphal 
1312250fc3dfSFlorian Westphal 	nla_nest_end(skb, vfinfo);
1313250fc3dfSFlorian Westphal 	return 0;
1314250fc3dfSFlorian Westphal }
1315250fc3dfSFlorian Westphal 
1316b22b941bSHannes Frederic Sowa static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1317b22b941bSHannes Frederic Sowa {
13185f8e4474SKangjie Lu 	struct rtnl_link_ifmap map;
13195f8e4474SKangjie Lu 
13205f8e4474SKangjie Lu 	memset(&map, 0, sizeof(map));
13215f8e4474SKangjie Lu 	map.mem_start   = dev->mem_start;
13225f8e4474SKangjie Lu 	map.mem_end     = dev->mem_end;
13235f8e4474SKangjie Lu 	map.base_addr   = dev->base_addr;
13245f8e4474SKangjie Lu 	map.irq         = dev->irq;
13255f8e4474SKangjie Lu 	map.dma         = dev->dma;
13265f8e4474SKangjie Lu 	map.port        = dev->if_port;
13275f8e4474SKangjie Lu 
1328270cb4d0SNicolas Dichtel 	if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
1329b22b941bSHannes Frederic Sowa 		return -EMSGSIZE;
1330b22b941bSHannes Frederic Sowa 
1331b22b941bSHannes Frederic Sowa 	return 0;
1332b22b941bSHannes Frederic Sowa }
1333b22b941bSHannes Frederic Sowa 
133458038695SMartin KaFai Lau static u8 rtnl_xdp_attached_mode(struct net_device *dev, u32 *prog_id)
1335d67b9cd2SDaniel Borkmann {
1336d67b9cd2SDaniel Borkmann 	const struct net_device_ops *ops = dev->netdev_ops;
133758038695SMartin KaFai Lau 	const struct bpf_prog *generic_xdp_prog;
1338118b4aa2SJakub Kicinski 	struct netdev_bpf xdp;
1339d67b9cd2SDaniel Borkmann 
1340d67b9cd2SDaniel Borkmann 	ASSERT_RTNL();
1341d67b9cd2SDaniel Borkmann 
134258038695SMartin KaFai Lau 	*prog_id = 0;
134358038695SMartin KaFai Lau 	generic_xdp_prog = rtnl_dereference(dev->xdp_prog);
134458038695SMartin KaFai Lau 	if (generic_xdp_prog) {
134558038695SMartin KaFai Lau 		*prog_id = generic_xdp_prog->aux->id;
1346d67b9cd2SDaniel Borkmann 		return XDP_ATTACHED_SKB;
134758038695SMartin KaFai Lau 	}
1348f4e63525SJakub Kicinski 	if (!ops->ndo_bpf)
1349d67b9cd2SDaniel Borkmann 		return XDP_ATTACHED_NONE;
1350ce158e58SJakub Kicinski 
1351118b4aa2SJakub Kicinski 	__dev_xdp_query(dev, ops->ndo_bpf, &xdp);
1352118b4aa2SJakub Kicinski 	*prog_id = xdp.prog_id;
1353118b4aa2SJakub Kicinski 
1354118b4aa2SJakub Kicinski 	return xdp.prog_attached;
1355d67b9cd2SDaniel Borkmann }
1356d67b9cd2SDaniel Borkmann 
1357d1fdd913SBrenden Blanco static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1358d1fdd913SBrenden Blanco {
1359d1fdd913SBrenden Blanco 	struct nlattr *xdp;
136058038695SMartin KaFai Lau 	u32 prog_id;
1361d1fdd913SBrenden Blanco 	int err;
1362d1fdd913SBrenden Blanco 
1363d1fdd913SBrenden Blanco 	xdp = nla_nest_start(skb, IFLA_XDP);
1364d1fdd913SBrenden Blanco 	if (!xdp)
1365d1fdd913SBrenden Blanco 		return -EMSGSIZE;
1366b5cdae32SDavid S. Miller 
1367d67b9cd2SDaniel Borkmann 	err = nla_put_u8(skb, IFLA_XDP_ATTACHED,
136858038695SMartin KaFai Lau 			 rtnl_xdp_attached_mode(dev, &prog_id));
1369d1fdd913SBrenden Blanco 	if (err)
1370d1fdd913SBrenden Blanco 		goto err_cancel;
1371d1fdd913SBrenden Blanco 
137258038695SMartin KaFai Lau 	if (prog_id) {
137358038695SMartin KaFai Lau 		err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id);
137458038695SMartin KaFai Lau 		if (err)
137558038695SMartin KaFai Lau 			goto err_cancel;
137658038695SMartin KaFai Lau 	}
137758038695SMartin KaFai Lau 
1378d1fdd913SBrenden Blanco 	nla_nest_end(skb, xdp);
1379d1fdd913SBrenden Blanco 	return 0;
1380d1fdd913SBrenden Blanco 
1381d1fdd913SBrenden Blanco err_cancel:
1382d1fdd913SBrenden Blanco 	nla_nest_cancel(skb, xdp);
1383d1fdd913SBrenden Blanco 	return err;
1384d1fdd913SBrenden Blanco }
1385d1fdd913SBrenden Blanco 
13863d3ea5afSVlad Yasevich static u32 rtnl_get_event(unsigned long event)
13873d3ea5afSVlad Yasevich {
13883d3ea5afSVlad Yasevich 	u32 rtnl_event_type = IFLA_EVENT_NONE;
13893d3ea5afSVlad Yasevich 
13903d3ea5afSVlad Yasevich 	switch (event) {
13913d3ea5afSVlad Yasevich 	case NETDEV_REBOOT:
13923d3ea5afSVlad Yasevich 		rtnl_event_type = IFLA_EVENT_REBOOT;
13933d3ea5afSVlad Yasevich 		break;
13943d3ea5afSVlad Yasevich 	case NETDEV_FEAT_CHANGE:
13953d3ea5afSVlad Yasevich 		rtnl_event_type = IFLA_EVENT_FEATURES;
13963d3ea5afSVlad Yasevich 		break;
13973d3ea5afSVlad Yasevich 	case NETDEV_BONDING_FAILOVER:
13983d3ea5afSVlad Yasevich 		rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER;
13993d3ea5afSVlad Yasevich 		break;
14003d3ea5afSVlad Yasevich 	case NETDEV_NOTIFY_PEERS:
14013d3ea5afSVlad Yasevich 		rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS;
14023d3ea5afSVlad Yasevich 		break;
14033d3ea5afSVlad Yasevich 	case NETDEV_RESEND_IGMP:
14043d3ea5afSVlad Yasevich 		rtnl_event_type = IFLA_EVENT_IGMP_RESEND;
14053d3ea5afSVlad Yasevich 		break;
14063d3ea5afSVlad Yasevich 	case NETDEV_CHANGEINFODATA:
14073d3ea5afSVlad Yasevich 		rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS;
14083d3ea5afSVlad Yasevich 		break;
14093d3ea5afSVlad Yasevich 	default:
14103d3ea5afSVlad Yasevich 		break;
14113d3ea5afSVlad Yasevich 	}
14123d3ea5afSVlad Yasevich 
14133d3ea5afSVlad Yasevich 	return rtnl_event_type;
14143d3ea5afSVlad Yasevich }
14153d3ea5afSVlad Yasevich 
141679110a04SFlorian Westphal static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev)
141779110a04SFlorian Westphal {
141879110a04SFlorian Westphal 	const struct net_device *upper_dev;
141979110a04SFlorian Westphal 	int ret = 0;
142079110a04SFlorian Westphal 
142179110a04SFlorian Westphal 	rcu_read_lock();
142279110a04SFlorian Westphal 
142379110a04SFlorian Westphal 	upper_dev = netdev_master_upper_dev_get_rcu(dev);
142479110a04SFlorian Westphal 	if (upper_dev)
142579110a04SFlorian Westphal 		ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex);
142679110a04SFlorian Westphal 
142779110a04SFlorian Westphal 	rcu_read_unlock();
142879110a04SFlorian Westphal 	return ret;
142979110a04SFlorian Westphal }
143079110a04SFlorian Westphal 
143179110a04SFlorian Westphal static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev)
143279110a04SFlorian Westphal {
143379110a04SFlorian Westphal 	int ifindex = dev_get_iflink(dev);
143479110a04SFlorian Westphal 
143579110a04SFlorian Westphal 	if (dev->ifindex == ifindex)
143679110a04SFlorian Westphal 		return 0;
143779110a04SFlorian Westphal 
143879110a04SFlorian Westphal 	return nla_put_u32(skb, IFLA_LINK, ifindex);
143979110a04SFlorian Westphal }
144079110a04SFlorian Westphal 
14416c557001SFlorian Westphal static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb,
14426c557001SFlorian Westphal 					      struct net_device *dev)
14436c557001SFlorian Westphal {
14446c557001SFlorian Westphal 	char buf[IFALIASZ];
14456c557001SFlorian Westphal 	int ret;
14466c557001SFlorian Westphal 
14476c557001SFlorian Westphal 	ret = dev_get_alias(dev, buf, sizeof(buf));
14486c557001SFlorian Westphal 	return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0;
14496c557001SFlorian Westphal }
14506c557001SFlorian Westphal 
1451b1e66b9aSFlorian Westphal static int rtnl_fill_link_netnsid(struct sk_buff *skb,
145279e1ad14SJiri Benc 				  const struct net_device *dev,
145379e1ad14SJiri Benc 				  struct net *src_net)
1454b1e66b9aSFlorian Westphal {
1455b1e66b9aSFlorian Westphal 	if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) {
1456b1e66b9aSFlorian Westphal 		struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1457b1e66b9aSFlorian Westphal 
1458b1e66b9aSFlorian Westphal 		if (!net_eq(dev_net(dev), link_net)) {
145979e1ad14SJiri Benc 			int id = peernet2id_alloc(src_net, link_net);
1460b1e66b9aSFlorian Westphal 
1461b1e66b9aSFlorian Westphal 			if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1462b1e66b9aSFlorian Westphal 				return -EMSGSIZE;
1463b1e66b9aSFlorian Westphal 		}
1464b1e66b9aSFlorian Westphal 	}
1465b1e66b9aSFlorian Westphal 
1466b1e66b9aSFlorian Westphal 	return 0;
1467b1e66b9aSFlorian Westphal }
1468b1e66b9aSFlorian Westphal 
1469070cbf5bSFlorian Westphal static int rtnl_fill_link_af(struct sk_buff *skb,
1470070cbf5bSFlorian Westphal 			     const struct net_device *dev,
1471070cbf5bSFlorian Westphal 			     u32 ext_filter_mask)
1472070cbf5bSFlorian Westphal {
1473070cbf5bSFlorian Westphal 	const struct rtnl_af_ops *af_ops;
1474070cbf5bSFlorian Westphal 	struct nlattr *af_spec;
1475070cbf5bSFlorian Westphal 
1476070cbf5bSFlorian Westphal 	af_spec = nla_nest_start(skb, IFLA_AF_SPEC);
1477070cbf5bSFlorian Westphal 	if (!af_spec)
1478070cbf5bSFlorian Westphal 		return -EMSGSIZE;
1479070cbf5bSFlorian Westphal 
14805fa85a09SFlorian Westphal 	list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
1481070cbf5bSFlorian Westphal 		struct nlattr *af;
1482070cbf5bSFlorian Westphal 		int err;
1483070cbf5bSFlorian Westphal 
1484070cbf5bSFlorian Westphal 		if (!af_ops->fill_link_af)
1485070cbf5bSFlorian Westphal 			continue;
1486070cbf5bSFlorian Westphal 
1487070cbf5bSFlorian Westphal 		af = nla_nest_start(skb, af_ops->family);
1488070cbf5bSFlorian Westphal 		if (!af)
1489070cbf5bSFlorian Westphal 			return -EMSGSIZE;
1490070cbf5bSFlorian Westphal 
1491070cbf5bSFlorian Westphal 		err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
1492070cbf5bSFlorian Westphal 		/*
1493070cbf5bSFlorian Westphal 		 * Caller may return ENODATA to indicate that there
1494070cbf5bSFlorian Westphal 		 * was no data to be dumped. This is not an error, it
1495070cbf5bSFlorian Westphal 		 * means we should trim the attribute header and
1496070cbf5bSFlorian Westphal 		 * continue.
1497070cbf5bSFlorian Westphal 		 */
1498070cbf5bSFlorian Westphal 		if (err == -ENODATA)
1499070cbf5bSFlorian Westphal 			nla_nest_cancel(skb, af);
1500070cbf5bSFlorian Westphal 		else if (err < 0)
1501070cbf5bSFlorian Westphal 			return -EMSGSIZE;
1502070cbf5bSFlorian Westphal 
1503070cbf5bSFlorian Westphal 		nla_nest_end(skb, af);
1504070cbf5bSFlorian Westphal 	}
1505070cbf5bSFlorian Westphal 
1506070cbf5bSFlorian Westphal 	nla_nest_end(skb, af_spec);
1507070cbf5bSFlorian Westphal 	return 0;
1508070cbf5bSFlorian Westphal }
1509070cbf5bSFlorian Westphal 
151079e1ad14SJiri Benc static int rtnl_fill_ifinfo(struct sk_buff *skb,
151179e1ad14SJiri Benc 			    struct net_device *dev, struct net *src_net,
1512575c3e2aSPatrick McHardy 			    int type, u32 pid, u32 seq, u32 change,
15133d3ea5afSVlad Yasevich 			    unsigned int flags, u32 ext_filter_mask,
151479e1ad14SJiri Benc 			    u32 event, int *new_nsid, int tgt_netnsid)
1515b60c5115SThomas Graf {
1516b60c5115SThomas Graf 	struct ifinfomsg *ifm;
15171da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
15181da177e4SLinus Torvalds 
15192907c35fSEric Dumazet 	ASSERT_RTNL();
1520b60c5115SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1521b60c5115SThomas Graf 	if (nlh == NULL)
152226932566SPatrick McHardy 		return -EMSGSIZE;
15231da177e4SLinus Torvalds 
1524b60c5115SThomas Graf 	ifm = nlmsg_data(nlh);
1525b60c5115SThomas Graf 	ifm->ifi_family = AF_UNSPEC;
1526b60c5115SThomas Graf 	ifm->__ifi_pad = 0;
1527b60c5115SThomas Graf 	ifm->ifi_type = dev->type;
1528b60c5115SThomas Graf 	ifm->ifi_index = dev->ifindex;
1529b60c5115SThomas Graf 	ifm->ifi_flags = dev_get_flags(dev);
1530b60c5115SThomas Graf 	ifm->ifi_change = change;
15311da177e4SLinus Torvalds 
153279e1ad14SJiri Benc 	if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_IF_NETNSID, tgt_netnsid))
153379e1ad14SJiri Benc 		goto nla_put_failure;
153479e1ad14SJiri Benc 
1535a6574349SDavid S. Miller 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1536a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1537a6574349SDavid S. Miller 	    nla_put_u8(skb, IFLA_OPERSTATE,
1538a6574349SDavid S. Miller 		       netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1539a6574349SDavid S. Miller 	    nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1540a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1541a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_GROUP, dev->group) ||
1542edbc0bb3SBen Greear 	    nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
154376ff5cc9SJiri Pirko 	    nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
1544c70ce028SEric Dumazet 	    nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1545c70ce028SEric Dumazet 	    nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
15461d69c2b3SMark A. Greer #ifdef CONFIG_RPS
154776ff5cc9SJiri Pirko 	    nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
15481d69c2b3SMark A. Greer #endif
154979110a04SFlorian Westphal 	    nla_put_iflink(skb, dev) ||
155079110a04SFlorian Westphal 	    put_master_ifindex(skb, dev) ||
15519a57247fSJiri Pirko 	    nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
1552a6574349SDavid S. Miller 	    (dev->qdisc &&
1553a6574349SDavid S. Miller 	     nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
15546c557001SFlorian Westphal 	    nla_put_ifalias(skb, dev) ||
15552d3b479dSdavid decotigny 	    nla_put_u32(skb, IFLA_CARRIER_CHANGES,
1556*b2d3bcfaSDavid Decotigny 			atomic_read(&dev->carrier_up_count) +
1557*b2d3bcfaSDavid Decotigny 			atomic_read(&dev->carrier_down_count)) ||
1558*b2d3bcfaSDavid Decotigny 	    nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down) ||
1559*b2d3bcfaSDavid Decotigny 	    nla_put_u32(skb, IFLA_CARRIER_UP_COUNT,
1560*b2d3bcfaSDavid Decotigny 			atomic_read(&dev->carrier_up_count)) ||
1561*b2d3bcfaSDavid Decotigny 	    nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT,
1562*b2d3bcfaSDavid Decotigny 			atomic_read(&dev->carrier_down_count)))
1563a6574349SDavid S. Miller 		goto nla_put_failure;
15640b815a1aSStephen Hemminger 
15653d3ea5afSVlad Yasevich 	if (event != IFLA_EVENT_NONE) {
15663d3ea5afSVlad Yasevich 		if (nla_put_u32(skb, IFLA_EVENT, event))
15673d3ea5afSVlad Yasevich 			goto nla_put_failure;
15683d3ea5afSVlad Yasevich 	}
15693d3ea5afSVlad Yasevich 
1570b22b941bSHannes Frederic Sowa 	if (rtnl_fill_link_ifmap(skb, dev))
1571a6574349SDavid S. Miller 		goto nla_put_failure;
15721da177e4SLinus Torvalds 
15731da177e4SLinus Torvalds 	if (dev->addr_len) {
1574a6574349SDavid S. Miller 		if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1575a6574349SDavid S. Miller 		    nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1576a6574349SDavid S. Miller 			goto nla_put_failure;
15771da177e4SLinus Torvalds 	}
15781da177e4SLinus Torvalds 
157966cae9edSJiri Pirko 	if (rtnl_phys_port_id_fill(skb, dev))
158066cae9edSJiri Pirko 		goto nla_put_failure;
158166cae9edSJiri Pirko 
1582db24a904SDavid Ahern 	if (rtnl_phys_port_name_fill(skb, dev))
1583db24a904SDavid Ahern 		goto nla_put_failure;
1584db24a904SDavid Ahern 
158582f28412SJiri Pirko 	if (rtnl_phys_switch_id_fill(skb, dev))
158682f28412SJiri Pirko 		goto nla_put_failure;
158782f28412SJiri Pirko 
1588b22b941bSHannes Frederic Sowa 	if (rtnl_fill_stats(skb, dev))
1589b60c5115SThomas Graf 		goto nla_put_failure;
1590b60c5115SThomas Graf 
1591250fc3dfSFlorian Westphal 	if (rtnl_fill_vf(skb, dev, ext_filter_mask))
1592a6574349SDavid S. Miller 		goto nla_put_failure;
159357b61080SScott Feldman 
1594c53864fdSDavid Gibson 	if (rtnl_port_fill(skb, dev, ext_filter_mask))
159557b61080SScott Feldman 		goto nla_put_failure;
159657b61080SScott Feldman 
1597d1fdd913SBrenden Blanco 	if (rtnl_xdp_fill(skb, dev))
1598d1fdd913SBrenden Blanco 		goto nla_put_failure;
1599d1fdd913SBrenden Blanco 
1600ba7d49b1SJiri Pirko 	if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
160138f7b870SPatrick McHardy 		if (rtnl_link_fill(skb, dev) < 0)
160238f7b870SPatrick McHardy 			goto nla_put_failure;
160338f7b870SPatrick McHardy 	}
160438f7b870SPatrick McHardy 
160579e1ad14SJiri Benc 	if (rtnl_fill_link_netnsid(skb, dev, src_net))
1606d37512a2SNicolas Dichtel 		goto nla_put_failure;
1607d37512a2SNicolas Dichtel 
16086621dd29SNicolas Dichtel 	if (new_nsid &&
16096621dd29SNicolas Dichtel 	    nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0)
16106621dd29SNicolas Dichtel 		goto nla_put_failure;
16116621dd29SNicolas Dichtel 
16125fa85a09SFlorian Westphal 	rcu_read_lock();
1613070cbf5bSFlorian Westphal 	if (rtnl_fill_link_af(skb, dev, ext_filter_mask))
16145fa85a09SFlorian Westphal 		goto nla_put_failure_rcu;
16155fa85a09SFlorian Westphal 	rcu_read_unlock();
1616f8ff182cSThomas Graf 
1617053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
1618053c095aSJohannes Berg 	return 0;
1619b60c5115SThomas Graf 
16205fa85a09SFlorian Westphal nla_put_failure_rcu:
16215fa85a09SFlorian Westphal 	rcu_read_unlock();
1622b60c5115SThomas Graf nla_put_failure:
162326932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
162426932566SPatrick McHardy 	return -EMSGSIZE;
16251da177e4SLinus Torvalds }
16261da177e4SLinus Torvalds 
1627f7b12606SJiri Pirko static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1628f7b12606SJiri Pirko 	[IFLA_IFNAME]		= { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1629f7b12606SJiri Pirko 	[IFLA_ADDRESS]		= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1630f7b12606SJiri Pirko 	[IFLA_BROADCAST]	= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1631f7b12606SJiri Pirko 	[IFLA_MAP]		= { .len = sizeof(struct rtnl_link_ifmap) },
1632f7b12606SJiri Pirko 	[IFLA_MTU]		= { .type = NLA_U32 },
1633f7b12606SJiri Pirko 	[IFLA_LINK]		= { .type = NLA_U32 },
1634f7b12606SJiri Pirko 	[IFLA_MASTER]		= { .type = NLA_U32 },
1635f7b12606SJiri Pirko 	[IFLA_CARRIER]		= { .type = NLA_U8 },
1636f7b12606SJiri Pirko 	[IFLA_TXQLEN]		= { .type = NLA_U32 },
1637f7b12606SJiri Pirko 	[IFLA_WEIGHT]		= { .type = NLA_U32 },
1638f7b12606SJiri Pirko 	[IFLA_OPERSTATE]	= { .type = NLA_U8 },
1639f7b12606SJiri Pirko 	[IFLA_LINKMODE]		= { .type = NLA_U8 },
1640f7b12606SJiri Pirko 	[IFLA_LINKINFO]		= { .type = NLA_NESTED },
1641f7b12606SJiri Pirko 	[IFLA_NET_NS_PID]	= { .type = NLA_U32 },
1642f7b12606SJiri Pirko 	[IFLA_NET_NS_FD]	= { .type = NLA_U32 },
16432459b4c6SNicolas Dichtel 	/* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
16442459b4c6SNicolas Dichtel 	 * allow 0-length string (needed to remove an alias).
16452459b4c6SNicolas Dichtel 	 */
16462459b4c6SNicolas Dichtel 	[IFLA_IFALIAS]	        = { .type = NLA_BINARY, .len = IFALIASZ - 1 },
1647f7b12606SJiri Pirko 	[IFLA_VFINFO_LIST]	= {. type = NLA_NESTED },
1648f7b12606SJiri Pirko 	[IFLA_VF_PORTS]		= { .type = NLA_NESTED },
1649f7b12606SJiri Pirko 	[IFLA_PORT_SELF]	= { .type = NLA_NESTED },
1650f7b12606SJiri Pirko 	[IFLA_AF_SPEC]		= { .type = NLA_NESTED },
1651f7b12606SJiri Pirko 	[IFLA_EXT_MASK]		= { .type = NLA_U32 },
1652f7b12606SJiri Pirko 	[IFLA_PROMISCUITY]	= { .type = NLA_U32 },
1653f7b12606SJiri Pirko 	[IFLA_NUM_TX_QUEUES]	= { .type = NLA_U32 },
1654f7b12606SJiri Pirko 	[IFLA_NUM_RX_QUEUES]	= { .type = NLA_U32 },
165546e6b992SStephen Hemminger 	[IFLA_GSO_MAX_SEGS]	= { .type = NLA_U32 },
165646e6b992SStephen Hemminger 	[IFLA_GSO_MAX_SIZE]	= { .type = NLA_U32 },
165702637fceSJiri Pirko 	[IFLA_PHYS_PORT_ID]	= { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
16582d3b479dSdavid decotigny 	[IFLA_CARRIER_CHANGES]	= { .type = NLA_U32 },  /* ignored */
165982f28412SJiri Pirko 	[IFLA_PHYS_SWITCH_ID]	= { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1660317f4810SNicolas Dichtel 	[IFLA_LINK_NETNSID]	= { .type = NLA_S32 },
166188d6378bSAnuradha Karuppiah 	[IFLA_PROTO_DOWN]	= { .type = NLA_U8 },
1662d1fdd913SBrenden Blanco 	[IFLA_XDP]		= { .type = NLA_NESTED },
16633d3ea5afSVlad Yasevich 	[IFLA_EVENT]		= { .type = NLA_U32 },
1664db833d40SSerhey Popovych 	[IFLA_GROUP]		= { .type = NLA_U32 },
166579e1ad14SJiri Benc 	[IFLA_IF_NETNSID]	= { .type = NLA_S32 },
1666*b2d3bcfaSDavid Decotigny 	[IFLA_CARRIER_UP_COUNT]	= { .type = NLA_U32 },
1667*b2d3bcfaSDavid Decotigny 	[IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 },
1668f7b12606SJiri Pirko };
1669f7b12606SJiri Pirko 
1670f7b12606SJiri Pirko static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1671f7b12606SJiri Pirko 	[IFLA_INFO_KIND]	= { .type = NLA_STRING },
1672f7b12606SJiri Pirko 	[IFLA_INFO_DATA]	= { .type = NLA_NESTED },
1673f7b12606SJiri Pirko 	[IFLA_INFO_SLAVE_KIND]	= { .type = NLA_STRING },
1674f7b12606SJiri Pirko 	[IFLA_INFO_SLAVE_DATA]	= { .type = NLA_NESTED },
1675f7b12606SJiri Pirko };
1676f7b12606SJiri Pirko 
1677f7b12606SJiri Pirko static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1678364d5716SDaniel Borkmann 	[IFLA_VF_MAC]		= { .len = sizeof(struct ifla_vf_mac) },
1679364d5716SDaniel Borkmann 	[IFLA_VF_VLAN]		= { .len = sizeof(struct ifla_vf_vlan) },
168079aab093SMoshe Shemesh 	[IFLA_VF_VLAN_LIST]     = { .type = NLA_NESTED },
1681364d5716SDaniel Borkmann 	[IFLA_VF_TX_RATE]	= { .len = sizeof(struct ifla_vf_tx_rate) },
1682364d5716SDaniel Borkmann 	[IFLA_VF_SPOOFCHK]	= { .len = sizeof(struct ifla_vf_spoofchk) },
1683364d5716SDaniel Borkmann 	[IFLA_VF_RATE]		= { .len = sizeof(struct ifla_vf_rate) },
1684364d5716SDaniel Borkmann 	[IFLA_VF_LINK_STATE]	= { .len = sizeof(struct ifla_vf_link_state) },
168501a3d796SVlad Zolotarov 	[IFLA_VF_RSS_QUERY_EN]	= { .len = sizeof(struct ifla_vf_rss_query_en) },
16863b766cd8SEran Ben Elisha 	[IFLA_VF_STATS]		= { .type = NLA_NESTED },
1687dd461d6aSHiroshi Shimamoto 	[IFLA_VF_TRUST]		= { .len = sizeof(struct ifla_vf_trust) },
1688cc8e27ccSEli Cohen 	[IFLA_VF_IB_NODE_GUID]	= { .len = sizeof(struct ifla_vf_guid) },
1689cc8e27ccSEli Cohen 	[IFLA_VF_IB_PORT_GUID]	= { .len = sizeof(struct ifla_vf_guid) },
16903b766cd8SEran Ben Elisha };
16913b766cd8SEran Ben Elisha 
1692f7b12606SJiri Pirko static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1693f7b12606SJiri Pirko 	[IFLA_PORT_VF]		= { .type = NLA_U32 },
1694f7b12606SJiri Pirko 	[IFLA_PORT_PROFILE]	= { .type = NLA_STRING,
1695f7b12606SJiri Pirko 				    .len = PORT_PROFILE_MAX },
1696f7b12606SJiri Pirko 	[IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1697f7b12606SJiri Pirko 				      .len = PORT_UUID_MAX },
1698f7b12606SJiri Pirko 	[IFLA_PORT_HOST_UUID]	= { .type = NLA_STRING,
1699f7b12606SJiri Pirko 				    .len = PORT_UUID_MAX },
1700f7b12606SJiri Pirko 	[IFLA_PORT_REQUEST]	= { .type = NLA_U8, },
1701f7b12606SJiri Pirko 	[IFLA_PORT_RESPONSE]	= { .type = NLA_U16, },
1702025331dfSDaniel Borkmann 
1703025331dfSDaniel Borkmann 	/* Unused, but we need to keep it here since user space could
1704025331dfSDaniel Borkmann 	 * fill it. It's also broken with regard to NLA_BINARY use in
1705025331dfSDaniel Borkmann 	 * combination with structs.
1706025331dfSDaniel Borkmann 	 */
1707025331dfSDaniel Borkmann 	[IFLA_PORT_VSI_TYPE]	= { .type = NLA_BINARY,
1708025331dfSDaniel Borkmann 				    .len = sizeof(struct ifla_port_vsi) },
1709f7b12606SJiri Pirko };
1710f7b12606SJiri Pirko 
1711d1fdd913SBrenden Blanco static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
1712d1fdd913SBrenden Blanco 	[IFLA_XDP_FD]		= { .type = NLA_S32 },
1713d1fdd913SBrenden Blanco 	[IFLA_XDP_ATTACHED]	= { .type = NLA_U8 },
171485de8576SDaniel Borkmann 	[IFLA_XDP_FLAGS]	= { .type = NLA_U32 },
171558038695SMartin KaFai Lau 	[IFLA_XDP_PROG_ID]	= { .type = NLA_U32 },
1716d1fdd913SBrenden Blanco };
1717d1fdd913SBrenden Blanco 
1718dc599f76SDavid Ahern static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1719dc599f76SDavid Ahern {
1720dc599f76SDavid Ahern 	const struct rtnl_link_ops *ops = NULL;
1721dc599f76SDavid Ahern 	struct nlattr *linfo[IFLA_INFO_MAX + 1];
1722dc599f76SDavid Ahern 
1723fceb6435SJohannes Berg 	if (nla_parse_nested(linfo, IFLA_INFO_MAX, nla,
1724fceb6435SJohannes Berg 			     ifla_info_policy, NULL) < 0)
1725dc599f76SDavid Ahern 		return NULL;
1726dc599f76SDavid Ahern 
1727dc599f76SDavid Ahern 	if (linfo[IFLA_INFO_KIND]) {
1728dc599f76SDavid Ahern 		char kind[MODULE_NAME_LEN];
1729dc599f76SDavid Ahern 
1730dc599f76SDavid Ahern 		nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1731dc599f76SDavid Ahern 		ops = rtnl_link_ops_get(kind);
1732dc599f76SDavid Ahern 	}
1733dc599f76SDavid Ahern 
1734dc599f76SDavid Ahern 	return ops;
1735dc599f76SDavid Ahern }
1736dc599f76SDavid Ahern 
1737dc599f76SDavid Ahern static bool link_master_filtered(struct net_device *dev, int master_idx)
1738dc599f76SDavid Ahern {
1739dc599f76SDavid Ahern 	struct net_device *master;
1740dc599f76SDavid Ahern 
1741dc599f76SDavid Ahern 	if (!master_idx)
1742dc599f76SDavid Ahern 		return false;
1743dc599f76SDavid Ahern 
1744dc599f76SDavid Ahern 	master = netdev_master_upper_dev_get(dev);
1745dc599f76SDavid Ahern 	if (!master || master->ifindex != master_idx)
1746dc599f76SDavid Ahern 		return true;
1747dc599f76SDavid Ahern 
1748dc599f76SDavid Ahern 	return false;
1749dc599f76SDavid Ahern }
1750dc599f76SDavid Ahern 
1751dc599f76SDavid Ahern static bool link_kind_filtered(const struct net_device *dev,
1752dc599f76SDavid Ahern 			       const struct rtnl_link_ops *kind_ops)
1753dc599f76SDavid Ahern {
1754dc599f76SDavid Ahern 	if (kind_ops && dev->rtnl_link_ops != kind_ops)
1755dc599f76SDavid Ahern 		return true;
1756dc599f76SDavid Ahern 
1757dc599f76SDavid Ahern 	return false;
1758dc599f76SDavid Ahern }
1759dc599f76SDavid Ahern 
1760dc599f76SDavid Ahern static bool link_dump_filtered(struct net_device *dev,
1761dc599f76SDavid Ahern 			       int master_idx,
1762dc599f76SDavid Ahern 			       const struct rtnl_link_ops *kind_ops)
1763dc599f76SDavid Ahern {
1764dc599f76SDavid Ahern 	if (link_master_filtered(dev, master_idx) ||
1765dc599f76SDavid Ahern 	    link_kind_filtered(dev, kind_ops))
1766dc599f76SDavid Ahern 		return true;
1767dc599f76SDavid Ahern 
1768dc599f76SDavid Ahern 	return false;
1769dc599f76SDavid Ahern }
1770dc599f76SDavid Ahern 
1771f428fe4aSAndrei Vagin static struct net *get_target_net(struct sock *sk, int netnsid)
177279e1ad14SJiri Benc {
177379e1ad14SJiri Benc 	struct net *net;
177479e1ad14SJiri Benc 
1775f428fe4aSAndrei Vagin 	net = get_net_ns_by_id(sock_net(sk), netnsid);
177679e1ad14SJiri Benc 	if (!net)
177779e1ad14SJiri Benc 		return ERR_PTR(-EINVAL);
177879e1ad14SJiri Benc 
177979e1ad14SJiri Benc 	/* For now, the caller is required to have CAP_NET_ADMIN in
178079e1ad14SJiri Benc 	 * the user namespace owning the target net ns.
178179e1ad14SJiri Benc 	 */
1782f428fe4aSAndrei Vagin 	if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) {
178379e1ad14SJiri Benc 		put_net(net);
178479e1ad14SJiri Benc 		return ERR_PTR(-EACCES);
178579e1ad14SJiri Benc 	}
178679e1ad14SJiri Benc 	return net;
178779e1ad14SJiri Benc }
178879e1ad14SJiri Benc 
1789b60c5115SThomas Graf static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
17901da177e4SLinus Torvalds {
17913b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
179279e1ad14SJiri Benc 	struct net *tgt_net = net;
17937c28bd0bSEric Dumazet 	int h, s_h;
17947c28bd0bSEric Dumazet 	int idx = 0, s_idx;
17951da177e4SLinus Torvalds 	struct net_device *dev;
17967c28bd0bSEric Dumazet 	struct hlist_head *head;
1797115c9b81SGreg Rose 	struct nlattr *tb[IFLA_MAX+1];
1798115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
1799dc599f76SDavid Ahern 	const struct rtnl_link_ops *kind_ops = NULL;
1800dc599f76SDavid Ahern 	unsigned int flags = NLM_F_MULTI;
1801dc599f76SDavid Ahern 	int master_idx = 0;
180279e1ad14SJiri Benc 	int netnsid = -1;
1803973462bbSDavid Gibson 	int err;
1804e5eca6d4SMichal Schmidt 	int hdrlen;
18051da177e4SLinus Torvalds 
18067c28bd0bSEric Dumazet 	s_h = cb->args[0];
18077c28bd0bSEric Dumazet 	s_idx = cb->args[1];
18087c28bd0bSEric Dumazet 
1809e5eca6d4SMichal Schmidt 	/* A hack to preserve kernel<->userspace interface.
1810e5eca6d4SMichal Schmidt 	 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1811e5eca6d4SMichal Schmidt 	 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1812e5eca6d4SMichal Schmidt 	 * what iproute2 < v3.9.0 used.
1813e5eca6d4SMichal Schmidt 	 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1814e5eca6d4SMichal Schmidt 	 * attribute, its netlink message is shorter than struct ifinfomsg.
1815e5eca6d4SMichal Schmidt 	 */
1816e5eca6d4SMichal Schmidt 	hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ?
1817e5eca6d4SMichal Schmidt 		 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1818e5eca6d4SMichal Schmidt 
1819fceb6435SJohannes Berg 	if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX,
1820fceb6435SJohannes Berg 			ifla_policy, NULL) >= 0) {
182179e1ad14SJiri Benc 		if (tb[IFLA_IF_NETNSID]) {
182279e1ad14SJiri Benc 			netnsid = nla_get_s32(tb[IFLA_IF_NETNSID]);
1823f428fe4aSAndrei Vagin 			tgt_net = get_target_net(skb->sk, netnsid);
182479e1ad14SJiri Benc 			if (IS_ERR(tgt_net)) {
182579e1ad14SJiri Benc 				tgt_net = net;
182679e1ad14SJiri Benc 				netnsid = -1;
182779e1ad14SJiri Benc 			}
182879e1ad14SJiri Benc 		}
182979e1ad14SJiri Benc 
1830115c9b81SGreg Rose 		if (tb[IFLA_EXT_MASK])
1831115c9b81SGreg Rose 			ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1832dc599f76SDavid Ahern 
1833dc599f76SDavid Ahern 		if (tb[IFLA_MASTER])
1834dc599f76SDavid Ahern 			master_idx = nla_get_u32(tb[IFLA_MASTER]);
1835dc599f76SDavid Ahern 
1836dc599f76SDavid Ahern 		if (tb[IFLA_LINKINFO])
1837dc599f76SDavid Ahern 			kind_ops = linkinfo_to_kind_ops(tb[IFLA_LINKINFO]);
1838dc599f76SDavid Ahern 
1839dc599f76SDavid Ahern 		if (master_idx || kind_ops)
1840dc599f76SDavid Ahern 			flags |= NLM_F_DUMP_FILTERED;
1841a4b64fbeSEric Dumazet 	}
1842115c9b81SGreg Rose 
18437c28bd0bSEric Dumazet 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
18447562f876SPavel Emelianov 		idx = 0;
184579e1ad14SJiri Benc 		head = &tgt_net->dev_index_head[h];
1846cac5e65eSEric Dumazet 		hlist_for_each_entry(dev, head, index_hlist) {
1847dc599f76SDavid Ahern 			if (link_dump_filtered(dev, master_idx, kind_ops))
18483f0ae05dSZhang Shengju 				goto cont;
18491da177e4SLinus Torvalds 			if (idx < s_idx)
18507562f876SPavel Emelianov 				goto cont;
185179e1ad14SJiri Benc 			err = rtnl_fill_ifinfo(skb, dev, net,
185279e1ad14SJiri Benc 					       RTM_NEWLINK,
185315e47304SEric W. Biederman 					       NETLINK_CB(cb->skb).portid,
18547c28bd0bSEric Dumazet 					       cb->nlh->nlmsg_seq, 0,
1855dc599f76SDavid Ahern 					       flags,
185679e1ad14SJiri Benc 					       ext_filter_mask, 0, NULL,
185779e1ad14SJiri Benc 					       netnsid);
1858973462bbSDavid Gibson 
1859f6c5775fSDavid Ahern 			if (err < 0) {
1860f6c5775fSDavid Ahern 				if (likely(skb->len))
18617c28bd0bSEric Dumazet 					goto out;
18624e985adaSThomas Graf 
1863f6c5775fSDavid Ahern 				goto out_err;
1864f6c5775fSDavid Ahern 			}
18657562f876SPavel Emelianov cont:
18667562f876SPavel Emelianov 			idx++;
18671da177e4SLinus Torvalds 		}
18687c28bd0bSEric Dumazet 	}
18697c28bd0bSEric Dumazet out:
1870f6c5775fSDavid Ahern 	err = skb->len;
1871f6c5775fSDavid Ahern out_err:
18727c28bd0bSEric Dumazet 	cb->args[1] = idx;
18737c28bd0bSEric Dumazet 	cb->args[0] = h;
1874d0225784SJakub Sitnicki 	cb->seq = net->dev_base_seq;
1875d0225784SJakub Sitnicki 	nl_dump_check_consistent(cb, nlmsg_hdr(skb));
187679e1ad14SJiri Benc 	if (netnsid >= 0)
187779e1ad14SJiri Benc 		put_net(tgt_net);
18781da177e4SLinus Torvalds 
1879f6c5775fSDavid Ahern 	return err;
18801da177e4SLinus Torvalds }
18811da177e4SLinus Torvalds 
1882fceb6435SJohannes Berg int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len,
1883fceb6435SJohannes Berg 			struct netlink_ext_ack *exterr)
1884f7b12606SJiri Pirko {
1885fceb6435SJohannes Berg 	return nla_parse(tb, IFLA_MAX, head, len, ifla_policy, exterr);
1886f7b12606SJiri Pirko }
1887f7b12606SJiri Pirko EXPORT_SYMBOL(rtnl_nla_parse_ifla);
188857b61080SScott Feldman 
188981adee47SEric W. Biederman struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
189081adee47SEric W. Biederman {
189181adee47SEric W. Biederman 	struct net *net;
189281adee47SEric W. Biederman 	/* Examine the link attributes and figure out which
189381adee47SEric W. Biederman 	 * network namespace we are talking about.
189481adee47SEric W. Biederman 	 */
189581adee47SEric W. Biederman 	if (tb[IFLA_NET_NS_PID])
189681adee47SEric W. Biederman 		net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
1897f0630529SEric W. Biederman 	else if (tb[IFLA_NET_NS_FD])
1898f0630529SEric W. Biederman 		net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
189981adee47SEric W. Biederman 	else
190081adee47SEric W. Biederman 		net = get_net(src_net);
190181adee47SEric W. Biederman 	return net;
190281adee47SEric W. Biederman }
190381adee47SEric W. Biederman EXPORT_SYMBOL(rtnl_link_get_net);
190481adee47SEric W. Biederman 
19051840bb13SThomas Graf static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
19061840bb13SThomas Graf {
19071840bb13SThomas Graf 	if (dev) {
19081840bb13SThomas Graf 		if (tb[IFLA_ADDRESS] &&
19091840bb13SThomas Graf 		    nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
19101840bb13SThomas Graf 			return -EINVAL;
19111840bb13SThomas Graf 
19121840bb13SThomas Graf 		if (tb[IFLA_BROADCAST] &&
19131840bb13SThomas Graf 		    nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
19141840bb13SThomas Graf 			return -EINVAL;
19151840bb13SThomas Graf 	}
19161840bb13SThomas Graf 
1917cf7afbfeSThomas Graf 	if (tb[IFLA_AF_SPEC]) {
1918cf7afbfeSThomas Graf 		struct nlattr *af;
1919cf7afbfeSThomas Graf 		int rem, err;
1920cf7afbfeSThomas Graf 
1921cf7afbfeSThomas Graf 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1922cf7afbfeSThomas Graf 			const struct rtnl_af_ops *af_ops;
1923cf7afbfeSThomas Graf 
19245fa85a09SFlorian Westphal 			rcu_read_lock();
19255fa85a09SFlorian Westphal 			af_ops = rtnl_af_lookup(nla_type(af));
19265fa85a09SFlorian Westphal 			if (!af_ops) {
19275fa85a09SFlorian Westphal 				rcu_read_unlock();
1928cf7afbfeSThomas Graf 				return -EAFNOSUPPORT;
19295fa85a09SFlorian Westphal 			}
1930cf7afbfeSThomas Graf 
19315fa85a09SFlorian Westphal 			if (!af_ops->set_link_af) {
19325fa85a09SFlorian Westphal 				rcu_read_unlock();
1933cf7afbfeSThomas Graf 				return -EOPNOTSUPP;
19345fa85a09SFlorian Westphal 			}
1935cf7afbfeSThomas Graf 
1936cf7afbfeSThomas Graf 			if (af_ops->validate_link_af) {
19376d3a9a68SKurt Van Dijck 				err = af_ops->validate_link_af(dev, af);
19385fa85a09SFlorian Westphal 				if (err < 0) {
19395fa85a09SFlorian Westphal 					rcu_read_unlock();
1940cf7afbfeSThomas Graf 					return err;
1941cf7afbfeSThomas Graf 				}
1942cf7afbfeSThomas Graf 			}
19435fa85a09SFlorian Westphal 
19445fa85a09SFlorian Westphal 			rcu_read_unlock();
19455fa85a09SFlorian Westphal 		}
1946cf7afbfeSThomas Graf 	}
1947cf7afbfeSThomas Graf 
19481840bb13SThomas Graf 	return 0;
19491840bb13SThomas Graf }
19501840bb13SThomas Graf 
1951cc8e27ccSEli Cohen static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
1952cc8e27ccSEli Cohen 				  int guid_type)
1953cc8e27ccSEli Cohen {
1954cc8e27ccSEli Cohen 	const struct net_device_ops *ops = dev->netdev_ops;
1955cc8e27ccSEli Cohen 
1956cc8e27ccSEli Cohen 	return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
1957cc8e27ccSEli Cohen }
1958cc8e27ccSEli Cohen 
1959cc8e27ccSEli Cohen static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
1960cc8e27ccSEli Cohen {
1961cc8e27ccSEli Cohen 	if (dev->type != ARPHRD_INFINIBAND)
1962cc8e27ccSEli Cohen 		return -EOPNOTSUPP;
1963cc8e27ccSEli Cohen 
1964cc8e27ccSEli Cohen 	return handle_infiniband_guid(dev, ivt, guid_type);
1965cc8e27ccSEli Cohen }
1966cc8e27ccSEli Cohen 
19674f7d2cdfSDaniel Borkmann static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
1968c02db8c6SChris Wright {
1969c02db8c6SChris Wright 	const struct net_device_ops *ops = dev->netdev_ops;
19704f7d2cdfSDaniel Borkmann 	int err = -EINVAL;
1971c02db8c6SChris Wright 
19724f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_MAC]) {
19734f7d2cdfSDaniel Borkmann 		struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
19744f7d2cdfSDaniel Borkmann 
1975c02db8c6SChris Wright 		err = -EOPNOTSUPP;
1976c02db8c6SChris Wright 		if (ops->ndo_set_vf_mac)
1977c02db8c6SChris Wright 			err = ops->ndo_set_vf_mac(dev, ivm->vf,
1978c02db8c6SChris Wright 						  ivm->mac);
19794f7d2cdfSDaniel Borkmann 		if (err < 0)
19804f7d2cdfSDaniel Borkmann 			return err;
1981c02db8c6SChris Wright 	}
19824f7d2cdfSDaniel Borkmann 
19834f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_VLAN]) {
19844f7d2cdfSDaniel Borkmann 		struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
19854f7d2cdfSDaniel Borkmann 
1986c02db8c6SChris Wright 		err = -EOPNOTSUPP;
1987c02db8c6SChris Wright 		if (ops->ndo_set_vf_vlan)
19884f7d2cdfSDaniel Borkmann 			err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
198979aab093SMoshe Shemesh 						   ivv->qos,
199079aab093SMoshe Shemesh 						   htons(ETH_P_8021Q));
199179aab093SMoshe Shemesh 		if (err < 0)
199279aab093SMoshe Shemesh 			return err;
199379aab093SMoshe Shemesh 	}
199479aab093SMoshe Shemesh 
199579aab093SMoshe Shemesh 	if (tb[IFLA_VF_VLAN_LIST]) {
199679aab093SMoshe Shemesh 		struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
199779aab093SMoshe Shemesh 		struct nlattr *attr;
199879aab093SMoshe Shemesh 		int rem, len = 0;
199979aab093SMoshe Shemesh 
200079aab093SMoshe Shemesh 		err = -EOPNOTSUPP;
200179aab093SMoshe Shemesh 		if (!ops->ndo_set_vf_vlan)
200279aab093SMoshe Shemesh 			return err;
200379aab093SMoshe Shemesh 
200479aab093SMoshe Shemesh 		nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
200579aab093SMoshe Shemesh 			if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
200679aab093SMoshe Shemesh 			    nla_len(attr) < NLA_HDRLEN) {
200779aab093SMoshe Shemesh 				return -EINVAL;
200879aab093SMoshe Shemesh 			}
200979aab093SMoshe Shemesh 			if (len >= MAX_VLAN_LIST_LEN)
201079aab093SMoshe Shemesh 				return -EOPNOTSUPP;
201179aab093SMoshe Shemesh 			ivvl[len] = nla_data(attr);
201279aab093SMoshe Shemesh 
201379aab093SMoshe Shemesh 			len++;
201479aab093SMoshe Shemesh 		}
2015fa34cd94SArnd Bergmann 		if (len == 0)
2016fa34cd94SArnd Bergmann 			return -EINVAL;
2017fa34cd94SArnd Bergmann 
201879aab093SMoshe Shemesh 		err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
201979aab093SMoshe Shemesh 					   ivvl[0]->qos, ivvl[0]->vlan_proto);
20204f7d2cdfSDaniel Borkmann 		if (err < 0)
20214f7d2cdfSDaniel Borkmann 			return err;
2022c02db8c6SChris Wright 	}
20234f7d2cdfSDaniel Borkmann 
20244f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_TX_RATE]) {
20254f7d2cdfSDaniel Borkmann 		struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
2026ed616689SSucheta Chakraborty 		struct ifla_vf_info ivf;
20274f7d2cdfSDaniel Borkmann 
2028c02db8c6SChris Wright 		err = -EOPNOTSUPP;
2029ed616689SSucheta Chakraborty 		if (ops->ndo_get_vf_config)
20304f7d2cdfSDaniel Borkmann 			err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
20314f7d2cdfSDaniel Borkmann 		if (err < 0)
20324f7d2cdfSDaniel Borkmann 			return err;
20334f7d2cdfSDaniel Borkmann 
2034ed616689SSucheta Chakraborty 		err = -EOPNOTSUPP;
2035ed616689SSucheta Chakraborty 		if (ops->ndo_set_vf_rate)
2036ed616689SSucheta Chakraborty 			err = ops->ndo_set_vf_rate(dev, ivt->vf,
2037ed616689SSucheta Chakraborty 						   ivf.min_tx_rate,
2038c02db8c6SChris Wright 						   ivt->rate);
20394f7d2cdfSDaniel Borkmann 		if (err < 0)
20404f7d2cdfSDaniel Borkmann 			return err;
2041c02db8c6SChris Wright 	}
20424f7d2cdfSDaniel Borkmann 
20434f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_RATE]) {
20444f7d2cdfSDaniel Borkmann 		struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
20454f7d2cdfSDaniel Borkmann 
2046ed616689SSucheta Chakraborty 		err = -EOPNOTSUPP;
2047ed616689SSucheta Chakraborty 		if (ops->ndo_set_vf_rate)
2048ed616689SSucheta Chakraborty 			err = ops->ndo_set_vf_rate(dev, ivt->vf,
2049ed616689SSucheta Chakraborty 						   ivt->min_tx_rate,
2050ed616689SSucheta Chakraborty 						   ivt->max_tx_rate);
20514f7d2cdfSDaniel Borkmann 		if (err < 0)
20524f7d2cdfSDaniel Borkmann 			return err;
2053ed616689SSucheta Chakraborty 	}
20544f7d2cdfSDaniel Borkmann 
20554f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_SPOOFCHK]) {
20564f7d2cdfSDaniel Borkmann 		struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
20574f7d2cdfSDaniel Borkmann 
20585f8444a3SGreg Rose 		err = -EOPNOTSUPP;
20595f8444a3SGreg Rose 		if (ops->ndo_set_vf_spoofchk)
20605f8444a3SGreg Rose 			err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
20615f8444a3SGreg Rose 						       ivs->setting);
20624f7d2cdfSDaniel Borkmann 		if (err < 0)
20634f7d2cdfSDaniel Borkmann 			return err;
20645f8444a3SGreg Rose 	}
20654f7d2cdfSDaniel Borkmann 
20664f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_LINK_STATE]) {
20674f7d2cdfSDaniel Borkmann 		struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
20684f7d2cdfSDaniel Borkmann 
20691d8faf48SRony Efraim 		err = -EOPNOTSUPP;
20701d8faf48SRony Efraim 		if (ops->ndo_set_vf_link_state)
20711d8faf48SRony Efraim 			err = ops->ndo_set_vf_link_state(dev, ivl->vf,
20721d8faf48SRony Efraim 							 ivl->link_state);
20734f7d2cdfSDaniel Borkmann 		if (err < 0)
20744f7d2cdfSDaniel Borkmann 			return err;
20751d8faf48SRony Efraim 	}
20764f7d2cdfSDaniel Borkmann 
20774f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_RSS_QUERY_EN]) {
207801a3d796SVlad Zolotarov 		struct ifla_vf_rss_query_en *ivrssq_en;
207901a3d796SVlad Zolotarov 
208001a3d796SVlad Zolotarov 		err = -EOPNOTSUPP;
20814f7d2cdfSDaniel Borkmann 		ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
208201a3d796SVlad Zolotarov 		if (ops->ndo_set_vf_rss_query_en)
20834f7d2cdfSDaniel Borkmann 			err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
208401a3d796SVlad Zolotarov 							   ivrssq_en->setting);
20854f7d2cdfSDaniel Borkmann 		if (err < 0)
20864f7d2cdfSDaniel Borkmann 			return err;
208701a3d796SVlad Zolotarov 	}
20884f7d2cdfSDaniel Borkmann 
2089dd461d6aSHiroshi Shimamoto 	if (tb[IFLA_VF_TRUST]) {
2090dd461d6aSHiroshi Shimamoto 		struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
2091dd461d6aSHiroshi Shimamoto 
2092dd461d6aSHiroshi Shimamoto 		err = -EOPNOTSUPP;
2093dd461d6aSHiroshi Shimamoto 		if (ops->ndo_set_vf_trust)
2094dd461d6aSHiroshi Shimamoto 			err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
2095dd461d6aSHiroshi Shimamoto 		if (err < 0)
2096dd461d6aSHiroshi Shimamoto 			return err;
2097dd461d6aSHiroshi Shimamoto 	}
2098dd461d6aSHiroshi Shimamoto 
2099cc8e27ccSEli Cohen 	if (tb[IFLA_VF_IB_NODE_GUID]) {
2100cc8e27ccSEli Cohen 		struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
2101cc8e27ccSEli Cohen 
2102cc8e27ccSEli Cohen 		if (!ops->ndo_set_vf_guid)
2103cc8e27ccSEli Cohen 			return -EOPNOTSUPP;
2104cc8e27ccSEli Cohen 
2105cc8e27ccSEli Cohen 		return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
2106cc8e27ccSEli Cohen 	}
2107cc8e27ccSEli Cohen 
2108cc8e27ccSEli Cohen 	if (tb[IFLA_VF_IB_PORT_GUID]) {
2109cc8e27ccSEli Cohen 		struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
2110cc8e27ccSEli Cohen 
2111cc8e27ccSEli Cohen 		if (!ops->ndo_set_vf_guid)
2112cc8e27ccSEli Cohen 			return -EOPNOTSUPP;
2113cc8e27ccSEli Cohen 
2114cc8e27ccSEli Cohen 		return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
2115cc8e27ccSEli Cohen 	}
2116cc8e27ccSEli Cohen 
2117c02db8c6SChris Wright 	return err;
2118c02db8c6SChris Wright }
2119c02db8c6SChris Wright 
212033eaf2a6SDavid Ahern static int do_set_master(struct net_device *dev, int ifindex,
212133eaf2a6SDavid Ahern 			 struct netlink_ext_ack *extack)
2122fbaec0eaSJiri Pirko {
2123898e5061SJiri Pirko 	struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
2124fbaec0eaSJiri Pirko 	const struct net_device_ops *ops;
2125fbaec0eaSJiri Pirko 	int err;
2126fbaec0eaSJiri Pirko 
2127898e5061SJiri Pirko 	if (upper_dev) {
2128898e5061SJiri Pirko 		if (upper_dev->ifindex == ifindex)
2129fbaec0eaSJiri Pirko 			return 0;
2130898e5061SJiri Pirko 		ops = upper_dev->netdev_ops;
2131fbaec0eaSJiri Pirko 		if (ops->ndo_del_slave) {
2132898e5061SJiri Pirko 			err = ops->ndo_del_slave(upper_dev, dev);
2133fbaec0eaSJiri Pirko 			if (err)
2134fbaec0eaSJiri Pirko 				return err;
2135fbaec0eaSJiri Pirko 		} else {
2136fbaec0eaSJiri Pirko 			return -EOPNOTSUPP;
2137fbaec0eaSJiri Pirko 		}
2138fbaec0eaSJiri Pirko 	}
2139fbaec0eaSJiri Pirko 
2140fbaec0eaSJiri Pirko 	if (ifindex) {
2141898e5061SJiri Pirko 		upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
2142898e5061SJiri Pirko 		if (!upper_dev)
2143fbaec0eaSJiri Pirko 			return -EINVAL;
2144898e5061SJiri Pirko 		ops = upper_dev->netdev_ops;
2145fbaec0eaSJiri Pirko 		if (ops->ndo_add_slave) {
214633eaf2a6SDavid Ahern 			err = ops->ndo_add_slave(upper_dev, dev, extack);
2147fbaec0eaSJiri Pirko 			if (err)
2148fbaec0eaSJiri Pirko 				return err;
2149fbaec0eaSJiri Pirko 		} else {
2150fbaec0eaSJiri Pirko 			return -EOPNOTSUPP;
2151fbaec0eaSJiri Pirko 		}
2152fbaec0eaSJiri Pirko 	}
2153fbaec0eaSJiri Pirko 	return 0;
2154fbaec0eaSJiri Pirko }
2155fbaec0eaSJiri Pirko 
215690c325e3SNicolas Dichtel #define DO_SETLINK_MODIFIED	0x01
2157ba998906SNicolas Dichtel /* notify flag means notify + modified. */
2158ba998906SNicolas Dichtel #define DO_SETLINK_NOTIFY	0x03
215990f62cf3SEric W. Biederman static int do_setlink(const struct sk_buff *skb,
216090f62cf3SEric W. Biederman 		      struct net_device *dev, struct ifinfomsg *ifm,
2161ddf9f970SJakub Kicinski 		      struct netlink_ext_ack *extack,
216290c325e3SNicolas Dichtel 		      struct nlattr **tb, char *ifname, int status)
21630157f60cSPatrick McHardy {
2164d314774cSStephen Hemminger 	const struct net_device_ops *ops = dev->netdev_ops;
21650157f60cSPatrick McHardy 	int err;
21660157f60cSPatrick McHardy 
2167f0630529SEric W. Biederman 	if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
216881adee47SEric W. Biederman 		struct net *net = rtnl_link_get_net(dev_net(dev), tb);
2169d8a5ec67SEric W. Biederman 		if (IS_ERR(net)) {
2170d8a5ec67SEric W. Biederman 			err = PTR_ERR(net);
2171d8a5ec67SEric W. Biederman 			goto errout;
2172d8a5ec67SEric W. Biederman 		}
217390f62cf3SEric W. Biederman 		if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
2174e0ebde0eSNicolas Dichtel 			put_net(net);
2175b51642f6SEric W. Biederman 			err = -EPERM;
2176b51642f6SEric W. Biederman 			goto errout;
2177b51642f6SEric W. Biederman 		}
2178d8a5ec67SEric W. Biederman 		err = dev_change_net_namespace(dev, net, ifname);
2179d8a5ec67SEric W. Biederman 		put_net(net);
2180d8a5ec67SEric W. Biederman 		if (err)
2181d8a5ec67SEric W. Biederman 			goto errout;
218290c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
2183d8a5ec67SEric W. Biederman 	}
2184d8a5ec67SEric W. Biederman 
21850157f60cSPatrick McHardy 	if (tb[IFLA_MAP]) {
21860157f60cSPatrick McHardy 		struct rtnl_link_ifmap *u_map;
21870157f60cSPatrick McHardy 		struct ifmap k_map;
21880157f60cSPatrick McHardy 
2189d314774cSStephen Hemminger 		if (!ops->ndo_set_config) {
21900157f60cSPatrick McHardy 			err = -EOPNOTSUPP;
21910157f60cSPatrick McHardy 			goto errout;
21920157f60cSPatrick McHardy 		}
21930157f60cSPatrick McHardy 
21940157f60cSPatrick McHardy 		if (!netif_device_present(dev)) {
21950157f60cSPatrick McHardy 			err = -ENODEV;
21960157f60cSPatrick McHardy 			goto errout;
21970157f60cSPatrick McHardy 		}
21980157f60cSPatrick McHardy 
21990157f60cSPatrick McHardy 		u_map = nla_data(tb[IFLA_MAP]);
22000157f60cSPatrick McHardy 		k_map.mem_start = (unsigned long) u_map->mem_start;
22010157f60cSPatrick McHardy 		k_map.mem_end = (unsigned long) u_map->mem_end;
22020157f60cSPatrick McHardy 		k_map.base_addr = (unsigned short) u_map->base_addr;
22030157f60cSPatrick McHardy 		k_map.irq = (unsigned char) u_map->irq;
22040157f60cSPatrick McHardy 		k_map.dma = (unsigned char) u_map->dma;
22050157f60cSPatrick McHardy 		k_map.port = (unsigned char) u_map->port;
22060157f60cSPatrick McHardy 
2207d314774cSStephen Hemminger 		err = ops->ndo_set_config(dev, &k_map);
22080157f60cSPatrick McHardy 		if (err < 0)
22090157f60cSPatrick McHardy 			goto errout;
22100157f60cSPatrick McHardy 
2211ba998906SNicolas Dichtel 		status |= DO_SETLINK_NOTIFY;
22120157f60cSPatrick McHardy 	}
22130157f60cSPatrick McHardy 
22140157f60cSPatrick McHardy 	if (tb[IFLA_ADDRESS]) {
22150157f60cSPatrick McHardy 		struct sockaddr *sa;
22160157f60cSPatrick McHardy 		int len;
22170157f60cSPatrick McHardy 
2218153711f9SWANG Cong 		len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
2219153711f9SWANG Cong 						  sizeof(*sa));
22200157f60cSPatrick McHardy 		sa = kmalloc(len, GFP_KERNEL);
22210157f60cSPatrick McHardy 		if (!sa) {
22220157f60cSPatrick McHardy 			err = -ENOMEM;
22230157f60cSPatrick McHardy 			goto errout;
22240157f60cSPatrick McHardy 		}
22250157f60cSPatrick McHardy 		sa->sa_family = dev->type;
22260157f60cSPatrick McHardy 		memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
22270157f60cSPatrick McHardy 		       dev->addr_len);
2228e7c3273eSJiri Pirko 		err = dev_set_mac_address(dev, sa);
22290157f60cSPatrick McHardy 		kfree(sa);
22300157f60cSPatrick McHardy 		if (err)
22310157f60cSPatrick McHardy 			goto errout;
223290c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
22330157f60cSPatrick McHardy 	}
22340157f60cSPatrick McHardy 
22350157f60cSPatrick McHardy 	if (tb[IFLA_MTU]) {
22360157f60cSPatrick McHardy 		err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
22370157f60cSPatrick McHardy 		if (err < 0)
22380157f60cSPatrick McHardy 			goto errout;
223990c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
22400157f60cSPatrick McHardy 	}
22410157f60cSPatrick McHardy 
2242cbda10faSVlad Dogaru 	if (tb[IFLA_GROUP]) {
2243cbda10faSVlad Dogaru 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
2244ba998906SNicolas Dichtel 		status |= DO_SETLINK_NOTIFY;
2245cbda10faSVlad Dogaru 	}
2246cbda10faSVlad Dogaru 
22470157f60cSPatrick McHardy 	/*
22480157f60cSPatrick McHardy 	 * Interface selected by interface index but interface
22490157f60cSPatrick McHardy 	 * name provided implies that a name change has been
22500157f60cSPatrick McHardy 	 * requested.
22510157f60cSPatrick McHardy 	 */
22520157f60cSPatrick McHardy 	if (ifm->ifi_index > 0 && ifname[0]) {
22530157f60cSPatrick McHardy 		err = dev_change_name(dev, ifname);
22540157f60cSPatrick McHardy 		if (err < 0)
22550157f60cSPatrick McHardy 			goto errout;
225690c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
22570157f60cSPatrick McHardy 	}
22580157f60cSPatrick McHardy 
22590b815a1aSStephen Hemminger 	if (tb[IFLA_IFALIAS]) {
22600b815a1aSStephen Hemminger 		err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
22610b815a1aSStephen Hemminger 				    nla_len(tb[IFLA_IFALIAS]));
22620b815a1aSStephen Hemminger 		if (err < 0)
22630b815a1aSStephen Hemminger 			goto errout;
2264ba998906SNicolas Dichtel 		status |= DO_SETLINK_NOTIFY;
22650b815a1aSStephen Hemminger 	}
22660b815a1aSStephen Hemminger 
22670157f60cSPatrick McHardy 	if (tb[IFLA_BROADCAST]) {
22680157f60cSPatrick McHardy 		nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
2269e7c3273eSJiri Pirko 		call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
22700157f60cSPatrick McHardy 	}
22710157f60cSPatrick McHardy 
22720157f60cSPatrick McHardy 	if (ifm->ifi_flags || ifm->ifi_change) {
22733729d502SPatrick McHardy 		err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
22745f9021cfSJohannes Berg 		if (err < 0)
22755f9021cfSJohannes Berg 			goto errout;
22760157f60cSPatrick McHardy 	}
22770157f60cSPatrick McHardy 
2278fbaec0eaSJiri Pirko 	if (tb[IFLA_MASTER]) {
227933eaf2a6SDavid Ahern 		err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
2280fbaec0eaSJiri Pirko 		if (err)
2281fbaec0eaSJiri Pirko 			goto errout;
228290c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
2283fbaec0eaSJiri Pirko 	}
2284fbaec0eaSJiri Pirko 
22859a57247fSJiri Pirko 	if (tb[IFLA_CARRIER]) {
22869a57247fSJiri Pirko 		err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
22879a57247fSJiri Pirko 		if (err)
22889a57247fSJiri Pirko 			goto errout;
228990c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
22909a57247fSJiri Pirko 	}
22919a57247fSJiri Pirko 
22925d1180fcSNicolas Dichtel 	if (tb[IFLA_TXQLEN]) {
22930cd29503SAlexey Dobriyan 		unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]);
22940cd29503SAlexey Dobriyan 		unsigned int orig_len = dev->tx_queue_len;
22955d1180fcSNicolas Dichtel 
229608294a26SJason Wang 		if (dev->tx_queue_len ^ value) {
22975d1180fcSNicolas Dichtel 			dev->tx_queue_len = value;
229808294a26SJason Wang 			err = call_netdevice_notifiers(
229908294a26SJason Wang 			      NETDEV_CHANGE_TX_QUEUE_LEN, dev);
230008294a26SJason Wang 			err = notifier_to_errno(err);
230108294a26SJason Wang 			if (err) {
230208294a26SJason Wang 				dev->tx_queue_len = orig_len;
230308294a26SJason Wang 				goto errout;
230408294a26SJason Wang 			}
23052d7f669bSXin Long 			status |= DO_SETLINK_MODIFIED;
230608294a26SJason Wang 		}
23075d1180fcSNicolas Dichtel 	}
23080157f60cSPatrick McHardy 
230946e6b992SStephen Hemminger 	if (tb[IFLA_GSO_MAX_SIZE]) {
231046e6b992SStephen Hemminger 		u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]);
231146e6b992SStephen Hemminger 
231246e6b992SStephen Hemminger 		if (max_size > GSO_MAX_SIZE) {
231346e6b992SStephen Hemminger 			err = -EINVAL;
231446e6b992SStephen Hemminger 			goto errout;
231546e6b992SStephen Hemminger 		}
231646e6b992SStephen Hemminger 
231746e6b992SStephen Hemminger 		if (dev->gso_max_size ^ max_size) {
231846e6b992SStephen Hemminger 			netif_set_gso_max_size(dev, max_size);
231946e6b992SStephen Hemminger 			status |= DO_SETLINK_MODIFIED;
232046e6b992SStephen Hemminger 		}
232146e6b992SStephen Hemminger 	}
232246e6b992SStephen Hemminger 
232346e6b992SStephen Hemminger 	if (tb[IFLA_GSO_MAX_SEGS]) {
232446e6b992SStephen Hemminger 		u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
232546e6b992SStephen Hemminger 
232646e6b992SStephen Hemminger 		if (max_segs > GSO_MAX_SEGS) {
232746e6b992SStephen Hemminger 			err = -EINVAL;
232846e6b992SStephen Hemminger 			goto errout;
232946e6b992SStephen Hemminger 		}
233046e6b992SStephen Hemminger 
233146e6b992SStephen Hemminger 		if (dev->gso_max_segs ^ max_segs) {
233246e6b992SStephen Hemminger 			dev->gso_max_segs = max_segs;
233346e6b992SStephen Hemminger 			status |= DO_SETLINK_MODIFIED;
233446e6b992SStephen Hemminger 		}
233546e6b992SStephen Hemminger 	}
233646e6b992SStephen Hemminger 
23370157f60cSPatrick McHardy 	if (tb[IFLA_OPERSTATE])
233893b2d4a2SDavid S. Miller 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
23390157f60cSPatrick McHardy 
23400157f60cSPatrick McHardy 	if (tb[IFLA_LINKMODE]) {
23411889b0e7SNicolas Dichtel 		unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
23421889b0e7SNicolas Dichtel 
23430157f60cSPatrick McHardy 		write_lock_bh(&dev_base_lock);
23441889b0e7SNicolas Dichtel 		if (dev->link_mode ^ value)
2345ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
23461889b0e7SNicolas Dichtel 		dev->link_mode = value;
234793b2d4a2SDavid S. Miller 		write_unlock_bh(&dev_base_lock);
23480157f60cSPatrick McHardy 	}
23490157f60cSPatrick McHardy 
2350c02db8c6SChris Wright 	if (tb[IFLA_VFINFO_LIST]) {
23514f7d2cdfSDaniel Borkmann 		struct nlattr *vfinfo[IFLA_VF_MAX + 1];
2352c02db8c6SChris Wright 		struct nlattr *attr;
2353c02db8c6SChris Wright 		int rem;
23544f7d2cdfSDaniel Borkmann 
2355c02db8c6SChris Wright 		nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
23564f7d2cdfSDaniel Borkmann 			if (nla_type(attr) != IFLA_VF_INFO ||
23574f7d2cdfSDaniel Borkmann 			    nla_len(attr) < NLA_HDRLEN) {
2358253683bbSDavid Howells 				err = -EINVAL;
2359c02db8c6SChris Wright 				goto errout;
2360253683bbSDavid Howells 			}
23614f7d2cdfSDaniel Borkmann 			err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr,
2362fceb6435SJohannes Berg 					       ifla_vf_policy, NULL);
23634f7d2cdfSDaniel Borkmann 			if (err < 0)
23644f7d2cdfSDaniel Borkmann 				goto errout;
23654f7d2cdfSDaniel Borkmann 			err = do_setvfinfo(dev, vfinfo);
2366ebc08a6fSWilliams, Mitch A 			if (err < 0)
2367ebc08a6fSWilliams, Mitch A 				goto errout;
2368ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
2369ebc08a6fSWilliams, Mitch A 		}
2370ebc08a6fSWilliams, Mitch A 	}
23710157f60cSPatrick McHardy 	err = 0;
23720157f60cSPatrick McHardy 
237357b61080SScott Feldman 	if (tb[IFLA_VF_PORTS]) {
237457b61080SScott Feldman 		struct nlattr *port[IFLA_PORT_MAX+1];
237557b61080SScott Feldman 		struct nlattr *attr;
237657b61080SScott Feldman 		int vf;
237757b61080SScott Feldman 		int rem;
237857b61080SScott Feldman 
237957b61080SScott Feldman 		err = -EOPNOTSUPP;
238057b61080SScott Feldman 		if (!ops->ndo_set_vf_port)
238157b61080SScott Feldman 			goto errout;
238257b61080SScott Feldman 
238357b61080SScott Feldman 		nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
2384035d210fSDaniel Borkmann 			if (nla_type(attr) != IFLA_VF_PORT ||
2385035d210fSDaniel Borkmann 			    nla_len(attr) < NLA_HDRLEN) {
2386035d210fSDaniel Borkmann 				err = -EINVAL;
2387035d210fSDaniel Borkmann 				goto errout;
2388035d210fSDaniel Borkmann 			}
2389035d210fSDaniel Borkmann 			err = nla_parse_nested(port, IFLA_PORT_MAX, attr,
2390fceb6435SJohannes Berg 					       ifla_port_policy, NULL);
239157b61080SScott Feldman 			if (err < 0)
239257b61080SScott Feldman 				goto errout;
239357b61080SScott Feldman 			if (!port[IFLA_PORT_VF]) {
239457b61080SScott Feldman 				err = -EOPNOTSUPP;
239557b61080SScott Feldman 				goto errout;
239657b61080SScott Feldman 			}
239757b61080SScott Feldman 			vf = nla_get_u32(port[IFLA_PORT_VF]);
239857b61080SScott Feldman 			err = ops->ndo_set_vf_port(dev, vf, port);
239957b61080SScott Feldman 			if (err < 0)
240057b61080SScott Feldman 				goto errout;
2401ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
240257b61080SScott Feldman 		}
240357b61080SScott Feldman 	}
240457b61080SScott Feldman 	err = 0;
240557b61080SScott Feldman 
240657b61080SScott Feldman 	if (tb[IFLA_PORT_SELF]) {
240757b61080SScott Feldman 		struct nlattr *port[IFLA_PORT_MAX+1];
240857b61080SScott Feldman 
240957b61080SScott Feldman 		err = nla_parse_nested(port, IFLA_PORT_MAX,
2410fceb6435SJohannes Berg 				       tb[IFLA_PORT_SELF], ifla_port_policy,
2411fceb6435SJohannes Berg 				       NULL);
241257b61080SScott Feldman 		if (err < 0)
241357b61080SScott Feldman 			goto errout;
241457b61080SScott Feldman 
241557b61080SScott Feldman 		err = -EOPNOTSUPP;
241657b61080SScott Feldman 		if (ops->ndo_set_vf_port)
241757b61080SScott Feldman 			err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
241857b61080SScott Feldman 		if (err < 0)
241957b61080SScott Feldman 			goto errout;
2420ba998906SNicolas Dichtel 		status |= DO_SETLINK_NOTIFY;
242157b61080SScott Feldman 	}
2422f8ff182cSThomas Graf 
2423f8ff182cSThomas Graf 	if (tb[IFLA_AF_SPEC]) {
2424f8ff182cSThomas Graf 		struct nlattr *af;
2425f8ff182cSThomas Graf 		int rem;
2426f8ff182cSThomas Graf 
2427f8ff182cSThomas Graf 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2428f8ff182cSThomas Graf 			const struct rtnl_af_ops *af_ops;
2429f8ff182cSThomas Graf 
24305fa85a09SFlorian Westphal 			rcu_read_lock();
24315fa85a09SFlorian Westphal 
2432058c8d59SGustavo A. R. Silva 			BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af))));
2433f8ff182cSThomas Graf 
2434cf7afbfeSThomas Graf 			err = af_ops->set_link_af(dev, af);
24355fa85a09SFlorian Westphal 			if (err < 0) {
24365fa85a09SFlorian Westphal 				rcu_read_unlock();
2437f8ff182cSThomas Graf 				goto errout;
24385fa85a09SFlorian Westphal 			}
2439f8ff182cSThomas Graf 
24405fa85a09SFlorian Westphal 			rcu_read_unlock();
2441ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
2442f8ff182cSThomas Graf 		}
2443f8ff182cSThomas Graf 	}
244457b61080SScott Feldman 	err = 0;
244557b61080SScott Feldman 
244688d6378bSAnuradha Karuppiah 	if (tb[IFLA_PROTO_DOWN]) {
244788d6378bSAnuradha Karuppiah 		err = dev_change_proto_down(dev,
244888d6378bSAnuradha Karuppiah 					    nla_get_u8(tb[IFLA_PROTO_DOWN]));
244988d6378bSAnuradha Karuppiah 		if (err)
245088d6378bSAnuradha Karuppiah 			goto errout;
245188d6378bSAnuradha Karuppiah 		status |= DO_SETLINK_NOTIFY;
245288d6378bSAnuradha Karuppiah 	}
245388d6378bSAnuradha Karuppiah 
2454d1fdd913SBrenden Blanco 	if (tb[IFLA_XDP]) {
2455d1fdd913SBrenden Blanco 		struct nlattr *xdp[IFLA_XDP_MAX + 1];
245685de8576SDaniel Borkmann 		u32 xdp_flags = 0;
2457d1fdd913SBrenden Blanco 
2458d1fdd913SBrenden Blanco 		err = nla_parse_nested(xdp, IFLA_XDP_MAX, tb[IFLA_XDP],
2459fceb6435SJohannes Berg 				       ifla_xdp_policy, NULL);
2460d1fdd913SBrenden Blanco 		if (err < 0)
2461d1fdd913SBrenden Blanco 			goto errout;
2462d1fdd913SBrenden Blanco 
246358038695SMartin KaFai Lau 		if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
2464262d8625SBrenden Blanco 			err = -EINVAL;
2465262d8625SBrenden Blanco 			goto errout;
2466262d8625SBrenden Blanco 		}
246785de8576SDaniel Borkmann 
246885de8576SDaniel Borkmann 		if (xdp[IFLA_XDP_FLAGS]) {
246985de8576SDaniel Borkmann 			xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
247085de8576SDaniel Borkmann 			if (xdp_flags & ~XDP_FLAGS_MASK) {
247185de8576SDaniel Borkmann 				err = -EINVAL;
247285de8576SDaniel Borkmann 				goto errout;
247385de8576SDaniel Borkmann 			}
2474ee5d032fSJakub Kicinski 			if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) {
24750489df9aSDaniel Borkmann 				err = -EINVAL;
24760489df9aSDaniel Borkmann 				goto errout;
24770489df9aSDaniel Borkmann 			}
247885de8576SDaniel Borkmann 		}
247985de8576SDaniel Borkmann 
2480d1fdd913SBrenden Blanco 		if (xdp[IFLA_XDP_FD]) {
2481ddf9f970SJakub Kicinski 			err = dev_change_xdp_fd(dev, extack,
248285de8576SDaniel Borkmann 						nla_get_s32(xdp[IFLA_XDP_FD]),
248385de8576SDaniel Borkmann 						xdp_flags);
2484d1fdd913SBrenden Blanco 			if (err)
2485d1fdd913SBrenden Blanco 				goto errout;
2486d1fdd913SBrenden Blanco 			status |= DO_SETLINK_NOTIFY;
2487d1fdd913SBrenden Blanco 		}
2488d1fdd913SBrenden Blanco 	}
2489d1fdd913SBrenden Blanco 
24900157f60cSPatrick McHardy errout:
2491ba998906SNicolas Dichtel 	if (status & DO_SETLINK_MODIFIED) {
249264ff90ccSXin Long 		if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY)
2493ba998906SNicolas Dichtel 			netdev_state_change(dev);
2494ba998906SNicolas Dichtel 
2495ba998906SNicolas Dichtel 		if (err < 0)
2496e87cc472SJoe 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",
2497e87cc472SJoe Perches 					     dev->name);
2498ba998906SNicolas Dichtel 	}
24990157f60cSPatrick McHardy 
25000157f60cSPatrick McHardy 	return err;
25010157f60cSPatrick McHardy }
25020157f60cSPatrick McHardy 
2503c21ef3e3SDavid Ahern static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2504c21ef3e3SDavid Ahern 			struct netlink_ext_ack *extack)
2505da5e0494SThomas Graf {
25063b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
2507da5e0494SThomas Graf 	struct ifinfomsg *ifm;
2508da5e0494SThomas Graf 	struct net_device *dev;
25090157f60cSPatrick McHardy 	int err;
2510da5e0494SThomas Graf 	struct nlattr *tb[IFLA_MAX+1];
25111da177e4SLinus Torvalds 	char ifname[IFNAMSIZ];
25121da177e4SLinus Torvalds 
2513c21ef3e3SDavid Ahern 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy,
2514c21ef3e3SDavid Ahern 			  extack);
2515da5e0494SThomas Graf 	if (err < 0)
2516da5e0494SThomas Graf 		goto errout;
25171da177e4SLinus Torvalds 
251879e1ad14SJiri Benc 	if (tb[IFLA_IF_NETNSID])
251979e1ad14SJiri Benc 		return -EOPNOTSUPP;
252079e1ad14SJiri Benc 
25215176f91eSThomas Graf 	if (tb[IFLA_IFNAME])
25225176f91eSThomas Graf 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
252378e5b891SPatrick McHardy 	else
252478e5b891SPatrick McHardy 		ifname[0] = '\0';
25251da177e4SLinus Torvalds 
25261da177e4SLinus Torvalds 	err = -EINVAL;
2527da5e0494SThomas Graf 	ifm = nlmsg_data(nlh);
252851055be8SPatrick McHardy 	if (ifm->ifi_index > 0)
2529a3d12891SEric Dumazet 		dev = __dev_get_by_index(net, ifm->ifi_index);
2530da5e0494SThomas Graf 	else if (tb[IFLA_IFNAME])
2531a3d12891SEric Dumazet 		dev = __dev_get_by_name(net, ifname);
2532da5e0494SThomas Graf 	else
2533da5e0494SThomas Graf 		goto errout;
25341da177e4SLinus Torvalds 
2535da5e0494SThomas Graf 	if (dev == NULL) {
2536da5e0494SThomas Graf 		err = -ENODEV;
2537da5e0494SThomas Graf 		goto errout;
2538da5e0494SThomas Graf 	}
25391da177e4SLinus Torvalds 
2540e0d087afSEric Dumazet 	err = validate_linkmsg(dev, tb);
2541e0d087afSEric Dumazet 	if (err < 0)
2542a3d12891SEric Dumazet 		goto errout;
2543da5e0494SThomas Graf 
2544ddf9f970SJakub Kicinski 	err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0);
2545da5e0494SThomas Graf errout:
25461da177e4SLinus Torvalds 	return err;
25471da177e4SLinus Torvalds }
25481da177e4SLinus Torvalds 
254966400d54SWANG Cong static int rtnl_group_dellink(const struct net *net, int group)
255066400d54SWANG Cong {
255166400d54SWANG Cong 	struct net_device *dev, *aux;
255266400d54SWANG Cong 	LIST_HEAD(list_kill);
255366400d54SWANG Cong 	bool found = false;
255466400d54SWANG Cong 
255566400d54SWANG Cong 	if (!group)
255666400d54SWANG Cong 		return -EPERM;
255766400d54SWANG Cong 
255866400d54SWANG Cong 	for_each_netdev(net, dev) {
255966400d54SWANG Cong 		if (dev->group == group) {
256066400d54SWANG Cong 			const struct rtnl_link_ops *ops;
256166400d54SWANG Cong 
256266400d54SWANG Cong 			found = true;
256366400d54SWANG Cong 			ops = dev->rtnl_link_ops;
256466400d54SWANG Cong 			if (!ops || !ops->dellink)
256566400d54SWANG Cong 				return -EOPNOTSUPP;
256666400d54SWANG Cong 		}
256766400d54SWANG Cong 	}
256866400d54SWANG Cong 
256966400d54SWANG Cong 	if (!found)
257066400d54SWANG Cong 		return -ENODEV;
257166400d54SWANG Cong 
257266400d54SWANG Cong 	for_each_netdev_safe(net, dev, aux) {
257366400d54SWANG Cong 		if (dev->group == group) {
257466400d54SWANG Cong 			const struct rtnl_link_ops *ops;
257566400d54SWANG Cong 
257666400d54SWANG Cong 			ops = dev->rtnl_link_ops;
257766400d54SWANG Cong 			ops->dellink(dev, &list_kill);
257866400d54SWANG Cong 		}
257966400d54SWANG Cong 	}
258066400d54SWANG Cong 	unregister_netdevice_many(&list_kill);
258166400d54SWANG Cong 
258266400d54SWANG Cong 	return 0;
258366400d54SWANG Cong }
258466400d54SWANG Cong 
2585614732eaSThomas Graf int rtnl_delete_link(struct net_device *dev)
2586614732eaSThomas Graf {
2587614732eaSThomas Graf 	const struct rtnl_link_ops *ops;
2588614732eaSThomas Graf 	LIST_HEAD(list_kill);
2589614732eaSThomas Graf 
2590614732eaSThomas Graf 	ops = dev->rtnl_link_ops;
2591614732eaSThomas Graf 	if (!ops || !ops->dellink)
2592614732eaSThomas Graf 		return -EOPNOTSUPP;
2593614732eaSThomas Graf 
2594614732eaSThomas Graf 	ops->dellink(dev, &list_kill);
2595614732eaSThomas Graf 	unregister_netdevice_many(&list_kill);
2596614732eaSThomas Graf 
2597614732eaSThomas Graf 	return 0;
2598614732eaSThomas Graf }
2599614732eaSThomas Graf EXPORT_SYMBOL_GPL(rtnl_delete_link);
2600614732eaSThomas Graf 
2601c21ef3e3SDavid Ahern static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
2602c21ef3e3SDavid Ahern 			struct netlink_ext_ack *extack)
260338f7b870SPatrick McHardy {
26043b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
260538f7b870SPatrick McHardy 	struct net_device *dev;
260638f7b870SPatrick McHardy 	struct ifinfomsg *ifm;
260738f7b870SPatrick McHardy 	char ifname[IFNAMSIZ];
260838f7b870SPatrick McHardy 	struct nlattr *tb[IFLA_MAX+1];
260938f7b870SPatrick McHardy 	int err;
261038f7b870SPatrick McHardy 
2611c21ef3e3SDavid Ahern 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
261238f7b870SPatrick McHardy 	if (err < 0)
261338f7b870SPatrick McHardy 		return err;
261438f7b870SPatrick McHardy 
261579e1ad14SJiri Benc 	if (tb[IFLA_IF_NETNSID])
261679e1ad14SJiri Benc 		return -EOPNOTSUPP;
261779e1ad14SJiri Benc 
261838f7b870SPatrick McHardy 	if (tb[IFLA_IFNAME])
261938f7b870SPatrick McHardy 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
262038f7b870SPatrick McHardy 
262138f7b870SPatrick McHardy 	ifm = nlmsg_data(nlh);
262238f7b870SPatrick McHardy 	if (ifm->ifi_index > 0)
2623881d966bSEric W. Biederman 		dev = __dev_get_by_index(net, ifm->ifi_index);
262438f7b870SPatrick McHardy 	else if (tb[IFLA_IFNAME])
2625881d966bSEric W. Biederman 		dev = __dev_get_by_name(net, ifname);
262666400d54SWANG Cong 	else if (tb[IFLA_GROUP])
262766400d54SWANG Cong 		return rtnl_group_dellink(net, nla_get_u32(tb[IFLA_GROUP]));
262838f7b870SPatrick McHardy 	else
262938f7b870SPatrick McHardy 		return -EINVAL;
263038f7b870SPatrick McHardy 
263138f7b870SPatrick McHardy 	if (!dev)
263238f7b870SPatrick McHardy 		return -ENODEV;
263338f7b870SPatrick McHardy 
2634614732eaSThomas Graf 	return rtnl_delete_link(dev);
263538f7b870SPatrick McHardy }
263638f7b870SPatrick McHardy 
26373729d502SPatrick McHardy int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
26383729d502SPatrick McHardy {
26393729d502SPatrick McHardy 	unsigned int old_flags;
26403729d502SPatrick McHardy 	int err;
26413729d502SPatrick McHardy 
26423729d502SPatrick McHardy 	old_flags = dev->flags;
26433729d502SPatrick McHardy 	if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
26443729d502SPatrick McHardy 		err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
26453729d502SPatrick McHardy 		if (err < 0)
26463729d502SPatrick McHardy 			return err;
26473729d502SPatrick McHardy 	}
26483729d502SPatrick McHardy 
26493729d502SPatrick McHardy 	dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
26503729d502SPatrick McHardy 
2651a528c219SNicolas Dichtel 	__dev_notify_flags(dev, old_flags, ~0U);
26523729d502SPatrick McHardy 	return 0;
26533729d502SPatrick McHardy }
26543729d502SPatrick McHardy EXPORT_SYMBOL(rtnl_configure_link);
26553729d502SPatrick McHardy 
2656c0713563SRami Rosen struct net_device *rtnl_create_link(struct net *net,
265778ebb0d0SThomas Graf 	const char *ifname, unsigned char name_assign_type,
26585517750fSTom Gundersen 	const struct rtnl_link_ops *ops, struct nlattr *tb[])
2659e7199288SPavel Emelianov {
2660e7199288SPavel Emelianov 	struct net_device *dev;
2661d40156aaSJiri Pirko 	unsigned int num_tx_queues = 1;
2662d40156aaSJiri Pirko 	unsigned int num_rx_queues = 1;
2663e7199288SPavel Emelianov 
266476ff5cc9SJiri Pirko 	if (tb[IFLA_NUM_TX_QUEUES])
266576ff5cc9SJiri Pirko 		num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
266676ff5cc9SJiri Pirko 	else if (ops->get_num_tx_queues)
2667d40156aaSJiri Pirko 		num_tx_queues = ops->get_num_tx_queues();
266876ff5cc9SJiri Pirko 
266976ff5cc9SJiri Pirko 	if (tb[IFLA_NUM_RX_QUEUES])
267076ff5cc9SJiri Pirko 		num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
267176ff5cc9SJiri Pirko 	else if (ops->get_num_rx_queues)
2672d40156aaSJiri Pirko 		num_rx_queues = ops->get_num_rx_queues();
2673efacb309Sstephen hemminger 
26745517750fSTom Gundersen 	dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
2675c835a677STom Gundersen 			       ops->setup, num_tx_queues, num_rx_queues);
2676e7199288SPavel Emelianov 	if (!dev)
2677d1892e4eSTobias Klauser 		return ERR_PTR(-ENOMEM);
2678e7199288SPavel Emelianov 
267981adee47SEric W. Biederman 	dev_net_set(dev, net);
268081adee47SEric W. Biederman 	dev->rtnl_link_ops = ops;
26813729d502SPatrick McHardy 	dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
268281adee47SEric W. Biederman 
2683e7199288SPavel Emelianov 	if (tb[IFLA_MTU])
2684e7199288SPavel Emelianov 		dev->mtu = nla_get_u32(tb[IFLA_MTU]);
26852afb9b53SJiri Pirko 	if (tb[IFLA_ADDRESS]) {
2686e7199288SPavel Emelianov 		memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
2687e7199288SPavel Emelianov 				nla_len(tb[IFLA_ADDRESS]));
26882afb9b53SJiri Pirko 		dev->addr_assign_type = NET_ADDR_SET;
26892afb9b53SJiri Pirko 	}
2690e7199288SPavel Emelianov 	if (tb[IFLA_BROADCAST])
2691e7199288SPavel Emelianov 		memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
2692e7199288SPavel Emelianov 				nla_len(tb[IFLA_BROADCAST]));
2693e7199288SPavel Emelianov 	if (tb[IFLA_TXQLEN])
2694e7199288SPavel Emelianov 		dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
2695e7199288SPavel Emelianov 	if (tb[IFLA_OPERSTATE])
269693b2d4a2SDavid S. Miller 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
2697e7199288SPavel Emelianov 	if (tb[IFLA_LINKMODE])
2698e7199288SPavel Emelianov 		dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
2699ffa934f1SPatrick McHardy 	if (tb[IFLA_GROUP])
2700ffa934f1SPatrick McHardy 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
270146e6b992SStephen Hemminger 	if (tb[IFLA_GSO_MAX_SIZE])
270246e6b992SStephen Hemminger 		netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE]));
270346e6b992SStephen Hemminger 	if (tb[IFLA_GSO_MAX_SEGS])
2704a0b586faSStephen Hemminger 		dev->gso_max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
2705e7199288SPavel Emelianov 
2706e7199288SPavel Emelianov 	return dev;
2707e7199288SPavel Emelianov }
2708e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_create_link);
2709e7199288SPavel Emelianov 
271090f62cf3SEric W. Biederman static int rtnl_group_changelink(const struct sk_buff *skb,
271190f62cf3SEric W. Biederman 		struct net *net, int group,
2712e7ed828fSVlad Dogaru 		struct ifinfomsg *ifm,
2713ddf9f970SJakub Kicinski 		struct netlink_ext_ack *extack,
2714e7ed828fSVlad Dogaru 		struct nlattr **tb)
2715e7ed828fSVlad Dogaru {
2716d079535dSWANG Cong 	struct net_device *dev, *aux;
2717e7ed828fSVlad Dogaru 	int err;
2718e7ed828fSVlad Dogaru 
2719d079535dSWANG Cong 	for_each_netdev_safe(net, dev, aux) {
2720e7ed828fSVlad Dogaru 		if (dev->group == group) {
2721ddf9f970SJakub Kicinski 			err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0);
2722e7ed828fSVlad Dogaru 			if (err < 0)
2723e7ed828fSVlad Dogaru 				return err;
2724e7ed828fSVlad Dogaru 		}
2725e7ed828fSVlad Dogaru 	}
2726e7ed828fSVlad Dogaru 
2727e7ed828fSVlad Dogaru 	return 0;
2728e7ed828fSVlad Dogaru }
2729e7ed828fSVlad Dogaru 
2730c21ef3e3SDavid Ahern static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2731c21ef3e3SDavid Ahern 			struct netlink_ext_ack *extack)
273238f7b870SPatrick McHardy {
27333b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
273438f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops;
2735ba7d49b1SJiri Pirko 	const struct rtnl_link_ops *m_ops = NULL;
273638f7b870SPatrick McHardy 	struct net_device *dev;
2737ba7d49b1SJiri Pirko 	struct net_device *master_dev = NULL;
273838f7b870SPatrick McHardy 	struct ifinfomsg *ifm;
273938f7b870SPatrick McHardy 	char kind[MODULE_NAME_LEN];
274038f7b870SPatrick McHardy 	char ifname[IFNAMSIZ];
274138f7b870SPatrick McHardy 	struct nlattr *tb[IFLA_MAX+1];
274238f7b870SPatrick McHardy 	struct nlattr *linkinfo[IFLA_INFO_MAX+1];
27435517750fSTom Gundersen 	unsigned char name_assign_type = NET_NAME_USER;
274438f7b870SPatrick McHardy 	int err;
274538f7b870SPatrick McHardy 
274695a5afcaSJohannes Berg #ifdef CONFIG_MODULES
274738f7b870SPatrick McHardy replay:
27488072f085SThomas Graf #endif
2749c21ef3e3SDavid Ahern 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
275038f7b870SPatrick McHardy 	if (err < 0)
275138f7b870SPatrick McHardy 		return err;
275238f7b870SPatrick McHardy 
275379e1ad14SJiri Benc 	if (tb[IFLA_IF_NETNSID])
275479e1ad14SJiri Benc 		return -EOPNOTSUPP;
275579e1ad14SJiri Benc 
275638f7b870SPatrick McHardy 	if (tb[IFLA_IFNAME])
275738f7b870SPatrick McHardy 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
275838f7b870SPatrick McHardy 	else
275938f7b870SPatrick McHardy 		ifname[0] = '\0';
276038f7b870SPatrick McHardy 
276138f7b870SPatrick McHardy 	ifm = nlmsg_data(nlh);
276238f7b870SPatrick McHardy 	if (ifm->ifi_index > 0)
2763881d966bSEric W. Biederman 		dev = __dev_get_by_index(net, ifm->ifi_index);
2764e7ed828fSVlad Dogaru 	else {
2765e7ed828fSVlad Dogaru 		if (ifname[0])
2766881d966bSEric W. Biederman 			dev = __dev_get_by_name(net, ifname);
276738f7b870SPatrick McHardy 		else
276838f7b870SPatrick McHardy 			dev = NULL;
2769e7ed828fSVlad Dogaru 	}
277038f7b870SPatrick McHardy 
2771ba7d49b1SJiri Pirko 	if (dev) {
2772ba7d49b1SJiri Pirko 		master_dev = netdev_master_upper_dev_get(dev);
2773ba7d49b1SJiri Pirko 		if (master_dev)
2774ba7d49b1SJiri Pirko 			m_ops = master_dev->rtnl_link_ops;
2775ba7d49b1SJiri Pirko 	}
2776ba7d49b1SJiri Pirko 
2777e0d087afSEric Dumazet 	err = validate_linkmsg(dev, tb);
2778e0d087afSEric Dumazet 	if (err < 0)
27791840bb13SThomas Graf 		return err;
27801840bb13SThomas Graf 
278138f7b870SPatrick McHardy 	if (tb[IFLA_LINKINFO]) {
278238f7b870SPatrick McHardy 		err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
2783fceb6435SJohannes Berg 				       tb[IFLA_LINKINFO], ifla_info_policy,
2784fceb6435SJohannes Berg 				       NULL);
278538f7b870SPatrick McHardy 		if (err < 0)
278638f7b870SPatrick McHardy 			return err;
278738f7b870SPatrick McHardy 	} else
278838f7b870SPatrick McHardy 		memset(linkinfo, 0, sizeof(linkinfo));
278938f7b870SPatrick McHardy 
279038f7b870SPatrick McHardy 	if (linkinfo[IFLA_INFO_KIND]) {
279138f7b870SPatrick McHardy 		nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
279238f7b870SPatrick McHardy 		ops = rtnl_link_ops_get(kind);
279338f7b870SPatrick McHardy 	} else {
279438f7b870SPatrick McHardy 		kind[0] = '\0';
279538f7b870SPatrick McHardy 		ops = NULL;
279638f7b870SPatrick McHardy 	}
279738f7b870SPatrick McHardy 
279838f7b870SPatrick McHardy 	if (1) {
27994e10fd5bSSasha Levin 		struct nlattr *attr[ops ? ops->maxtype + 1 : 1];
28004e10fd5bSSasha Levin 		struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 1];
2801ba7d49b1SJiri Pirko 		struct nlattr **data = NULL;
2802ba7d49b1SJiri Pirko 		struct nlattr **slave_data = NULL;
2803317f4810SNicolas Dichtel 		struct net *dest_net, *link_net = NULL;
280438f7b870SPatrick McHardy 
280538f7b870SPatrick McHardy 		if (ops) {
280638f7b870SPatrick McHardy 			if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
280738f7b870SPatrick McHardy 				err = nla_parse_nested(attr, ops->maxtype,
280838f7b870SPatrick McHardy 						       linkinfo[IFLA_INFO_DATA],
2809fceb6435SJohannes Berg 						       ops->policy, NULL);
281038f7b870SPatrick McHardy 				if (err < 0)
281138f7b870SPatrick McHardy 					return err;
281238f7b870SPatrick McHardy 				data = attr;
281338f7b870SPatrick McHardy 			}
281438f7b870SPatrick McHardy 			if (ops->validate) {
2815a8b8a889SMatthias Schiffer 				err = ops->validate(tb, data, extack);
281638f7b870SPatrick McHardy 				if (err < 0)
281738f7b870SPatrick McHardy 					return err;
281838f7b870SPatrick McHardy 			}
281938f7b870SPatrick McHardy 		}
282038f7b870SPatrick McHardy 
2821ba7d49b1SJiri Pirko 		if (m_ops) {
2822ba7d49b1SJiri Pirko 			if (m_ops->slave_maxtype &&
2823ba7d49b1SJiri Pirko 			    linkinfo[IFLA_INFO_SLAVE_DATA]) {
2824ba7d49b1SJiri Pirko 				err = nla_parse_nested(slave_attr,
2825ba7d49b1SJiri Pirko 						       m_ops->slave_maxtype,
2826ba7d49b1SJiri Pirko 						       linkinfo[IFLA_INFO_SLAVE_DATA],
2827fceb6435SJohannes Berg 						       m_ops->slave_policy,
2828fceb6435SJohannes Berg 						       NULL);
2829ba7d49b1SJiri Pirko 				if (err < 0)
2830ba7d49b1SJiri Pirko 					return err;
2831ba7d49b1SJiri Pirko 				slave_data = slave_attr;
2832ba7d49b1SJiri Pirko 			}
2833ba7d49b1SJiri Pirko 		}
2834ba7d49b1SJiri Pirko 
283538f7b870SPatrick McHardy 		if (dev) {
283690c325e3SNicolas Dichtel 			int status = 0;
283738f7b870SPatrick McHardy 
283838f7b870SPatrick McHardy 			if (nlh->nlmsg_flags & NLM_F_EXCL)
283938f7b870SPatrick McHardy 				return -EEXIST;
284038f7b870SPatrick McHardy 			if (nlh->nlmsg_flags & NLM_F_REPLACE)
284138f7b870SPatrick McHardy 				return -EOPNOTSUPP;
284238f7b870SPatrick McHardy 
284338f7b870SPatrick McHardy 			if (linkinfo[IFLA_INFO_DATA]) {
284438f7b870SPatrick McHardy 				if (!ops || ops != dev->rtnl_link_ops ||
284538f7b870SPatrick McHardy 				    !ops->changelink)
284638f7b870SPatrick McHardy 					return -EOPNOTSUPP;
284738f7b870SPatrick McHardy 
2848ad744b22SMatthias Schiffer 				err = ops->changelink(dev, tb, data, extack);
284938f7b870SPatrick McHardy 				if (err < 0)
285038f7b870SPatrick McHardy 					return err;
2851ba998906SNicolas Dichtel 				status |= DO_SETLINK_NOTIFY;
285238f7b870SPatrick McHardy 			}
285338f7b870SPatrick McHardy 
2854ba7d49b1SJiri Pirko 			if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
2855ba7d49b1SJiri Pirko 				if (!m_ops || !m_ops->slave_changelink)
2856ba7d49b1SJiri Pirko 					return -EOPNOTSUPP;
2857ba7d49b1SJiri Pirko 
2858ba7d49b1SJiri Pirko 				err = m_ops->slave_changelink(master_dev, dev,
285917dd0ec4SMatthias Schiffer 							      tb, slave_data,
286017dd0ec4SMatthias Schiffer 							      extack);
2861ba7d49b1SJiri Pirko 				if (err < 0)
2862ba7d49b1SJiri Pirko 					return err;
2863ba998906SNicolas Dichtel 				status |= DO_SETLINK_NOTIFY;
2864ba7d49b1SJiri Pirko 			}
2865ba7d49b1SJiri Pirko 
2866ddf9f970SJakub Kicinski 			return do_setlink(skb, dev, ifm, extack, tb, ifname,
2867ddf9f970SJakub Kicinski 					  status);
286838f7b870SPatrick McHardy 		}
286938f7b870SPatrick McHardy 
2870ffa934f1SPatrick McHardy 		if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
2871ffa934f1SPatrick McHardy 			if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
287290f62cf3SEric W. Biederman 				return rtnl_group_changelink(skb, net,
2873ffa934f1SPatrick McHardy 						nla_get_u32(tb[IFLA_GROUP]),
2874ddf9f970SJakub Kicinski 						ifm, extack, tb);
287538f7b870SPatrick McHardy 			return -ENODEV;
2876ffa934f1SPatrick McHardy 		}
287738f7b870SPatrick McHardy 
2878160ca014STheuns Verwoerd 		if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
287938f7b870SPatrick McHardy 			return -EOPNOTSUPP;
288038f7b870SPatrick McHardy 
288138f7b870SPatrick McHardy 		if (!ops) {
288295a5afcaSJohannes Berg #ifdef CONFIG_MODULES
288338f7b870SPatrick McHardy 			if (kind[0]) {
288438f7b870SPatrick McHardy 				__rtnl_unlock();
288538f7b870SPatrick McHardy 				request_module("rtnl-link-%s", kind);
288638f7b870SPatrick McHardy 				rtnl_lock();
288738f7b870SPatrick McHardy 				ops = rtnl_link_ops_get(kind);
288838f7b870SPatrick McHardy 				if (ops)
288938f7b870SPatrick McHardy 					goto replay;
289038f7b870SPatrick McHardy 			}
289138f7b870SPatrick McHardy #endif
289238f7b870SPatrick McHardy 			return -EOPNOTSUPP;
289338f7b870SPatrick McHardy 		}
289438f7b870SPatrick McHardy 
2895b0ab2fabSJiri Pirko 		if (!ops->setup)
2896b0ab2fabSJiri Pirko 			return -EOPNOTSUPP;
2897b0ab2fabSJiri Pirko 
28985517750fSTom Gundersen 		if (!ifname[0]) {
289938f7b870SPatrick McHardy 			snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
29005517750fSTom Gundersen 			name_assign_type = NET_NAME_ENUM;
29015517750fSTom Gundersen 		}
290238f7b870SPatrick McHardy 
290381adee47SEric W. Biederman 		dest_net = rtnl_link_get_net(net, tb);
290413ad1774SEric W. Biederman 		if (IS_ERR(dest_net))
290513ad1774SEric W. Biederman 			return PTR_ERR(dest_net);
290613ad1774SEric W. Biederman 
2907505ce415SEric W. Biederman 		err = -EPERM;
2908505ce415SEric W. Biederman 		if (!netlink_ns_capable(skb, dest_net->user_ns, CAP_NET_ADMIN))
2909505ce415SEric W. Biederman 			goto out;
2910505ce415SEric W. Biederman 
2911317f4810SNicolas Dichtel 		if (tb[IFLA_LINK_NETNSID]) {
2912317f4810SNicolas Dichtel 			int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
2913317f4810SNicolas Dichtel 
2914317f4810SNicolas Dichtel 			link_net = get_net_ns_by_id(dest_net, id);
2915317f4810SNicolas Dichtel 			if (!link_net) {
2916317f4810SNicolas Dichtel 				err =  -EINVAL;
2917317f4810SNicolas Dichtel 				goto out;
2918317f4810SNicolas Dichtel 			}
291906615bedSEric W. Biederman 			err = -EPERM;
292006615bedSEric W. Biederman 			if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
292106615bedSEric W. Biederman 				goto out;
2922317f4810SNicolas Dichtel 		}
2923317f4810SNicolas Dichtel 
2924317f4810SNicolas Dichtel 		dev = rtnl_create_link(link_net ? : dest_net, ifname,
2925317f4810SNicolas Dichtel 				       name_assign_type, ops, tb);
29269c7dafbfSPavel Emelyanov 		if (IS_ERR(dev)) {
2927e7199288SPavel Emelianov 			err = PTR_ERR(dev);
29289c7dafbfSPavel Emelyanov 			goto out;
29299c7dafbfSPavel Emelyanov 		}
29309c7dafbfSPavel Emelyanov 
29319c7dafbfSPavel Emelyanov 		dev->ifindex = ifm->ifi_index;
29329c7dafbfSPavel Emelyanov 
29330e0eee24SCong Wang 		if (ops->newlink) {
29347a3f4a18SMatthias Schiffer 			err = ops->newlink(link_net ? : net, dev, tb, data,
29357a3f4a18SMatthias Schiffer 					   extack);
29360e0eee24SCong Wang 			/* Drivers should call free_netdev() in ->destructor
2937e51fb152SCong Wang 			 * and unregister it on failure after registration
2938e51fb152SCong Wang 			 * so that device could be finally freed in rtnl_unlock.
29390e0eee24SCong Wang 			 */
2940e51fb152SCong Wang 			if (err < 0) {
2941e51fb152SCong Wang 				/* If device is not registered at all, free it now */
2942e51fb152SCong Wang 				if (dev->reg_state == NETREG_UNINITIALIZED)
2943e51fb152SCong Wang 					free_netdev(dev);
29440e0eee24SCong Wang 				goto out;
2945e51fb152SCong Wang 			}
29460e0eee24SCong Wang 		} else {
29472d85cba2SPatrick McHardy 			err = register_netdevice(dev);
2948fce9b9beSDan Carpenter 			if (err < 0) {
294938f7b870SPatrick McHardy 				free_netdev(dev);
29503729d502SPatrick McHardy 				goto out;
2951fce9b9beSDan Carpenter 			}
29520e0eee24SCong Wang 		}
29533729d502SPatrick McHardy 		err = rtnl_configure_link(dev, ifm);
295443638900SDavid S. Miller 		if (err < 0)
295543638900SDavid S. Miller 			goto out_unregister;
295643638900SDavid S. Miller 		if (link_net) {
295743638900SDavid S. Miller 			err = dev_change_net_namespace(dev, dest_net, ifname);
295843638900SDavid S. Miller 			if (err < 0)
295943638900SDavid S. Miller 				goto out_unregister;
296043638900SDavid S. Miller 		}
2961160ca014STheuns Verwoerd 		if (tb[IFLA_MASTER]) {
296233eaf2a6SDavid Ahern 			err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]),
296333eaf2a6SDavid Ahern 					    extack);
2964160ca014STheuns Verwoerd 			if (err)
2965160ca014STheuns Verwoerd 				goto out_unregister;
2966160ca014STheuns Verwoerd 		}
296743638900SDavid S. Miller out:
296843638900SDavid S. Miller 		if (link_net)
296943638900SDavid S. Miller 			put_net(link_net);
297043638900SDavid S. Miller 		put_net(dest_net);
297143638900SDavid S. Miller 		return err;
297243638900SDavid S. Miller out_unregister:
29737afb8886SWANG Cong 		if (ops->newlink) {
29747afb8886SWANG Cong 			LIST_HEAD(list_kill);
29757afb8886SWANG Cong 
29767afb8886SWANG Cong 			ops->dellink(dev, &list_kill);
29777afb8886SWANG Cong 			unregister_netdevice_many(&list_kill);
29787afb8886SWANG Cong 		} else {
29793729d502SPatrick McHardy 			unregister_netdevice(dev);
29807afb8886SWANG Cong 		}
2981317f4810SNicolas Dichtel 		goto out;
2982317f4810SNicolas Dichtel 	}
298338f7b870SPatrick McHardy }
298438f7b870SPatrick McHardy 
2985c21ef3e3SDavid Ahern static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2986c21ef3e3SDavid Ahern 			struct netlink_ext_ack *extack)
2987711e2c33SJean Tourrilhes {
29883b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
298979e1ad14SJiri Benc 	struct net *tgt_net = net;
2990b60c5115SThomas Graf 	struct ifinfomsg *ifm;
2991a3d12891SEric Dumazet 	char ifname[IFNAMSIZ];
2992b60c5115SThomas Graf 	struct nlattr *tb[IFLA_MAX+1];
2993b60c5115SThomas Graf 	struct net_device *dev = NULL;
2994b60c5115SThomas Graf 	struct sk_buff *nskb;
299579e1ad14SJiri Benc 	int netnsid = -1;
2996339bf98fSThomas Graf 	int err;
2997115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
2998711e2c33SJean Tourrilhes 
2999c21ef3e3SDavid Ahern 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
3000b60c5115SThomas Graf 	if (err < 0)
30019918f230SEric Sesterhenn 		return err;
3002b60c5115SThomas Graf 
300379e1ad14SJiri Benc 	if (tb[IFLA_IF_NETNSID]) {
300479e1ad14SJiri Benc 		netnsid = nla_get_s32(tb[IFLA_IF_NETNSID]);
3005f428fe4aSAndrei Vagin 		tgt_net = get_target_net(NETLINK_CB(skb).sk, netnsid);
300679e1ad14SJiri Benc 		if (IS_ERR(tgt_net))
300779e1ad14SJiri Benc 			return PTR_ERR(tgt_net);
300879e1ad14SJiri Benc 	}
300979e1ad14SJiri Benc 
3010a3d12891SEric Dumazet 	if (tb[IFLA_IFNAME])
3011a3d12891SEric Dumazet 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
3012a3d12891SEric Dumazet 
3013115c9b81SGreg Rose 	if (tb[IFLA_EXT_MASK])
3014115c9b81SGreg Rose 		ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3015115c9b81SGreg Rose 
301679e1ad14SJiri Benc 	err = -EINVAL;
3017b60c5115SThomas Graf 	ifm = nlmsg_data(nlh);
3018a3d12891SEric Dumazet 	if (ifm->ifi_index > 0)
301979e1ad14SJiri Benc 		dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
3020a3d12891SEric Dumazet 	else if (tb[IFLA_IFNAME])
302179e1ad14SJiri Benc 		dev = __dev_get_by_name(tgt_net, ifname);
3022a3d12891SEric Dumazet 	else
302379e1ad14SJiri Benc 		goto out;
3024b60c5115SThomas Graf 
302579e1ad14SJiri Benc 	err = -ENODEV;
3026a3d12891SEric Dumazet 	if (dev == NULL)
302779e1ad14SJiri Benc 		goto out;
3028a3d12891SEric Dumazet 
302979e1ad14SJiri Benc 	err = -ENOBUFS;
3030115c9b81SGreg Rose 	nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
3031a3d12891SEric Dumazet 	if (nskb == NULL)
303279e1ad14SJiri Benc 		goto out;
3033711e2c33SJean Tourrilhes 
303479e1ad14SJiri Benc 	err = rtnl_fill_ifinfo(nskb, dev, net,
303579e1ad14SJiri Benc 			       RTM_NEWLINK, NETLINK_CB(skb).portid,
303679e1ad14SJiri Benc 			       nlh->nlmsg_seq, 0, 0, ext_filter_mask,
303779e1ad14SJiri Benc 			       0, NULL, netnsid);
303826932566SPatrick McHardy 	if (err < 0) {
303926932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in if_nlmsg_size */
304026932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
304126932566SPatrick McHardy 		kfree_skb(nskb);
3042a3d12891SEric Dumazet 	} else
304315e47304SEric W. Biederman 		err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
304479e1ad14SJiri Benc out:
304579e1ad14SJiri Benc 	if (netnsid >= 0)
304679e1ad14SJiri Benc 		put_net(tgt_net);
3047b60c5115SThomas Graf 
3048711e2c33SJean Tourrilhes 	return err;
3049711e2c33SJean Tourrilhes }
3050711e2c33SJean Tourrilhes 
3051115c9b81SGreg Rose static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
3052c7ac8679SGreg Rose {
3053115c9b81SGreg Rose 	struct net *net = sock_net(skb->sk);
3054115c9b81SGreg Rose 	struct net_device *dev;
3055115c9b81SGreg Rose 	struct nlattr *tb[IFLA_MAX+1];
3056115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
3057115c9b81SGreg Rose 	u16 min_ifinfo_dump_size = 0;
3058e5eca6d4SMichal Schmidt 	int hdrlen;
3059115c9b81SGreg Rose 
3060e5eca6d4SMichal Schmidt 	/* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
3061e5eca6d4SMichal Schmidt 	hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
3062e5eca6d4SMichal Schmidt 		 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
3063e5eca6d4SMichal Schmidt 
3064fceb6435SJohannes Berg 	if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
3065115c9b81SGreg Rose 		if (tb[IFLA_EXT_MASK])
3066115c9b81SGreg Rose 			ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3067a4b64fbeSEric Dumazet 	}
3068115c9b81SGreg Rose 
3069115c9b81SGreg Rose 	if (!ext_filter_mask)
3070115c9b81SGreg Rose 		return NLMSG_GOODSIZE;
3071115c9b81SGreg Rose 	/*
3072115c9b81SGreg Rose 	 * traverse the list of net devices and compute the minimum
3073115c9b81SGreg Rose 	 * buffer size based upon the filter mask.
3074115c9b81SGreg Rose 	 */
30756853dd48SFlorian Westphal 	rcu_read_lock();
30766853dd48SFlorian Westphal 	for_each_netdev_rcu(net, dev) {
3077115c9b81SGreg Rose 		min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
3078115c9b81SGreg Rose 					     if_nlmsg_size(dev,
3079115c9b81SGreg Rose 						           ext_filter_mask));
3080115c9b81SGreg Rose 	}
30816853dd48SFlorian Westphal 	rcu_read_unlock();
3082115c9b81SGreg Rose 
308393af2056SZhang Shengju 	return nlmsg_total_size(min_ifinfo_dump_size);
3084c7ac8679SGreg Rose }
3085c7ac8679SGreg Rose 
308642bad1daSAdrian Bunk static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
30871da177e4SLinus Torvalds {
30881da177e4SLinus Torvalds 	int idx;
30891da177e4SLinus Torvalds 	int s_idx = cb->family;
30901da177e4SLinus Torvalds 
30911da177e4SLinus Torvalds 	if (s_idx == 0)
30921da177e4SLinus Torvalds 		s_idx = 1;
30936853dd48SFlorian Westphal 
309425239ceeSPatrick McHardy 	for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
3095addf9b90SFlorian Westphal 		struct rtnl_link **tab;
30961da177e4SLinus Torvalds 		int type = cb->nlh->nlmsg_type-RTM_BASE;
3097addf9b90SFlorian Westphal 		struct rtnl_link *link;
30986853dd48SFlorian Westphal 		rtnl_dumpit_func dumpit;
30996853dd48SFlorian Westphal 
31001da177e4SLinus Torvalds 		if (idx < s_idx || idx == PF_PACKET)
31011da177e4SLinus Torvalds 			continue;
31026853dd48SFlorian Westphal 
3103addf9b90SFlorian Westphal 		if (type < 0 || type >= RTM_NR_MSGTYPES)
31041da177e4SLinus Torvalds 			continue;
31056853dd48SFlorian Westphal 
3106addf9b90SFlorian Westphal 		tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]);
3107addf9b90SFlorian Westphal 		if (!tab)
3108addf9b90SFlorian Westphal 			continue;
3109addf9b90SFlorian Westphal 
3110addf9b90SFlorian Westphal 		link = tab[type];
3111addf9b90SFlorian Westphal 		if (!link)
3112addf9b90SFlorian Westphal 			continue;
3113addf9b90SFlorian Westphal 
3114addf9b90SFlorian Westphal 		dumpit = link->dumpit;
31156853dd48SFlorian Westphal 		if (!dumpit)
31166853dd48SFlorian Westphal 			continue;
31176853dd48SFlorian Westphal 
31180465277fSNicolas Dichtel 		if (idx > s_idx) {
31191da177e4SLinus Torvalds 			memset(&cb->args[0], 0, sizeof(cb->args));
31200465277fSNicolas Dichtel 			cb->prev_seq = 0;
31210465277fSNicolas Dichtel 			cb->seq = 0;
31220465277fSNicolas Dichtel 		}
31236853dd48SFlorian Westphal 		if (dumpit(skb, cb))
31241da177e4SLinus Torvalds 			break;
31251da177e4SLinus Torvalds 	}
31261da177e4SLinus Torvalds 	cb->family = idx;
31271da177e4SLinus Torvalds 
31281da177e4SLinus Torvalds 	return skb->len;
31291da177e4SLinus Torvalds }
31301da177e4SLinus Torvalds 
3131395eea6cSMahesh Bandewar struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
31323d3ea5afSVlad Yasevich 				       unsigned int change,
31336621dd29SNicolas Dichtel 				       u32 event, gfp_t flags, int *new_nsid)
31341da177e4SLinus Torvalds {
3135c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
31361da177e4SLinus Torvalds 	struct sk_buff *skb;
31370ec6d3f4SThomas Graf 	int err = -ENOBUFS;
3138c7ac8679SGreg Rose 	size_t if_info_size;
31391da177e4SLinus Torvalds 
31407f294054SAlexei Starovoitov 	skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
31410ec6d3f4SThomas Graf 	if (skb == NULL)
31420ec6d3f4SThomas Graf 		goto errout;
31431da177e4SLinus Torvalds 
314479e1ad14SJiri Benc 	err = rtnl_fill_ifinfo(skb, dev, dev_net(dev),
314579e1ad14SJiri Benc 			       type, 0, 0, change, 0, 0, event,
314679e1ad14SJiri Benc 			       new_nsid, -1);
314726932566SPatrick McHardy 	if (err < 0) {
314826932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in if_nlmsg_size() */
314926932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
315026932566SPatrick McHardy 		kfree_skb(skb);
315126932566SPatrick McHardy 		goto errout;
315226932566SPatrick McHardy 	}
3153395eea6cSMahesh Bandewar 	return skb;
31540ec6d3f4SThomas Graf errout:
31550ec6d3f4SThomas Graf 	if (err < 0)
31564b3da706SEric W. Biederman 		rtnl_set_sk_err(net, RTNLGRP_LINK, err);
3157395eea6cSMahesh Bandewar 	return NULL;
3158395eea6cSMahesh Bandewar }
3159395eea6cSMahesh Bandewar 
3160395eea6cSMahesh Bandewar void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
3161395eea6cSMahesh Bandewar {
3162395eea6cSMahesh Bandewar 	struct net *net = dev_net(dev);
3163395eea6cSMahesh Bandewar 
3164395eea6cSMahesh Bandewar 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
3165395eea6cSMahesh Bandewar }
3166395eea6cSMahesh Bandewar 
31673d3ea5afSVlad Yasevich static void rtmsg_ifinfo_event(int type, struct net_device *dev,
31683d3ea5afSVlad Yasevich 			       unsigned int change, u32 event,
31696621dd29SNicolas Dichtel 			       gfp_t flags, int *new_nsid)
3170395eea6cSMahesh Bandewar {
3171395eea6cSMahesh Bandewar 	struct sk_buff *skb;
3172395eea6cSMahesh Bandewar 
3173ed2a80abSNicolas Dichtel 	if (dev->reg_state != NETREG_REGISTERED)
3174ed2a80abSNicolas Dichtel 		return;
3175ed2a80abSNicolas Dichtel 
31766621dd29SNicolas Dichtel 	skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid);
3177395eea6cSMahesh Bandewar 	if (skb)
3178395eea6cSMahesh Bandewar 		rtmsg_ifinfo_send(skb, dev, flags);
31791da177e4SLinus Torvalds }
31803d3ea5afSVlad Yasevich 
31813d3ea5afSVlad Yasevich void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
31823d3ea5afSVlad Yasevich 		  gfp_t flags)
31833d3ea5afSVlad Yasevich {
31846621dd29SNicolas Dichtel 	rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags, NULL);
31853d3ea5afSVlad Yasevich }
31861da177e4SLinus Torvalds 
31876621dd29SNicolas Dichtel void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change,
31886621dd29SNicolas Dichtel 			 gfp_t flags, int *new_nsid)
31896621dd29SNicolas Dichtel {
31906621dd29SNicolas Dichtel 	rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
31916621dd29SNicolas Dichtel 			   new_nsid);
31926621dd29SNicolas Dichtel }
31936621dd29SNicolas Dichtel 
3194d83b0603SJohn Fastabend static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
3195d83b0603SJohn Fastabend 				   struct net_device *dev,
31961e53d5bbSHubert Sokolowski 				   u8 *addr, u16 vid, u32 pid, u32 seq,
31971c104a6bSNicolas Dichtel 				   int type, unsigned int flags,
3198b3379041SHubert Sokolowski 				   int nlflags, u16 ndm_state)
3199d83b0603SJohn Fastabend {
3200d83b0603SJohn Fastabend 	struct nlmsghdr *nlh;
3201d83b0603SJohn Fastabend 	struct ndmsg *ndm;
3202d83b0603SJohn Fastabend 
32031c104a6bSNicolas Dichtel 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
3204d83b0603SJohn Fastabend 	if (!nlh)
3205d83b0603SJohn Fastabend 		return -EMSGSIZE;
3206d83b0603SJohn Fastabend 
3207d83b0603SJohn Fastabend 	ndm = nlmsg_data(nlh);
3208d83b0603SJohn Fastabend 	ndm->ndm_family  = AF_BRIDGE;
3209d83b0603SJohn Fastabend 	ndm->ndm_pad1	 = 0;
3210d83b0603SJohn Fastabend 	ndm->ndm_pad2    = 0;
3211d83b0603SJohn Fastabend 	ndm->ndm_flags	 = flags;
3212d83b0603SJohn Fastabend 	ndm->ndm_type	 = 0;
3213d83b0603SJohn Fastabend 	ndm->ndm_ifindex = dev->ifindex;
3214b3379041SHubert Sokolowski 	ndm->ndm_state   = ndm_state;
3215d83b0603SJohn Fastabend 
3216d83b0603SJohn Fastabend 	if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
3217d83b0603SJohn Fastabend 		goto nla_put_failure;
32181e53d5bbSHubert Sokolowski 	if (vid)
32191e53d5bbSHubert Sokolowski 		if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
32201e53d5bbSHubert Sokolowski 			goto nla_put_failure;
3221d83b0603SJohn Fastabend 
3222053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
3223053c095aSJohannes Berg 	return 0;
3224d83b0603SJohn Fastabend 
3225d83b0603SJohn Fastabend nla_put_failure:
3226d83b0603SJohn Fastabend 	nlmsg_cancel(skb, nlh);
3227d83b0603SJohn Fastabend 	return -EMSGSIZE;
3228d83b0603SJohn Fastabend }
3229d83b0603SJohn Fastabend 
32303ff661c3SJohn Fastabend static inline size_t rtnl_fdb_nlmsg_size(void)
32313ff661c3SJohn Fastabend {
3232f82ef3e1SSabrina Dubroca 	return NLMSG_ALIGN(sizeof(struct ndmsg)) +
3233f82ef3e1SSabrina Dubroca 	       nla_total_size(ETH_ALEN) +	/* NDA_LLADDR */
3234f82ef3e1SSabrina Dubroca 	       nla_total_size(sizeof(u16)) +	/* NDA_VLAN */
3235f82ef3e1SSabrina Dubroca 	       0;
32363ff661c3SJohn Fastabend }
32373ff661c3SJohn Fastabend 
3238b3379041SHubert Sokolowski static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
3239b3379041SHubert Sokolowski 			    u16 ndm_state)
32403ff661c3SJohn Fastabend {
32413ff661c3SJohn Fastabend 	struct net *net = dev_net(dev);
32423ff661c3SJohn Fastabend 	struct sk_buff *skb;
32433ff661c3SJohn Fastabend 	int err = -ENOBUFS;
32443ff661c3SJohn Fastabend 
32453ff661c3SJohn Fastabend 	skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
32463ff661c3SJohn Fastabend 	if (!skb)
32473ff661c3SJohn Fastabend 		goto errout;
32483ff661c3SJohn Fastabend 
32491e53d5bbSHubert Sokolowski 	err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
3250b3379041SHubert Sokolowski 				      0, 0, type, NTF_SELF, 0, ndm_state);
32513ff661c3SJohn Fastabend 	if (err < 0) {
32523ff661c3SJohn Fastabend 		kfree_skb(skb);
32533ff661c3SJohn Fastabend 		goto errout;
32543ff661c3SJohn Fastabend 	}
32553ff661c3SJohn Fastabend 
32563ff661c3SJohn Fastabend 	rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
32573ff661c3SJohn Fastabend 	return;
32583ff661c3SJohn Fastabend errout:
32593ff661c3SJohn Fastabend 	rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
32603ff661c3SJohn Fastabend }
32613ff661c3SJohn Fastabend 
3262090096bfSVlad Yasevich /**
3263090096bfSVlad Yasevich  * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
3264090096bfSVlad Yasevich  */
3265090096bfSVlad Yasevich int ndo_dflt_fdb_add(struct ndmsg *ndm,
3266090096bfSVlad Yasevich 		     struct nlattr *tb[],
3267090096bfSVlad Yasevich 		     struct net_device *dev,
3268f6f6424bSJiri Pirko 		     const unsigned char *addr, u16 vid,
3269090096bfSVlad Yasevich 		     u16 flags)
3270090096bfSVlad Yasevich {
3271090096bfSVlad Yasevich 	int err = -EINVAL;
3272090096bfSVlad Yasevich 
3273090096bfSVlad Yasevich 	/* If aging addresses are supported device will need to
3274090096bfSVlad Yasevich 	 * implement its own handler for this.
3275090096bfSVlad Yasevich 	 */
3276090096bfSVlad Yasevich 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
3277090096bfSVlad Yasevich 		pr_info("%s: FDB only supports static addresses\n", dev->name);
3278090096bfSVlad Yasevich 		return err;
3279090096bfSVlad Yasevich 	}
3280090096bfSVlad Yasevich 
328165891feaSOr Gerlitz 	if (vid) {
328265891feaSOr Gerlitz 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
328365891feaSOr Gerlitz 		return err;
328465891feaSOr Gerlitz 	}
328565891feaSOr Gerlitz 
3286090096bfSVlad Yasevich 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3287090096bfSVlad Yasevich 		err = dev_uc_add_excl(dev, addr);
3288090096bfSVlad Yasevich 	else if (is_multicast_ether_addr(addr))
3289090096bfSVlad Yasevich 		err = dev_mc_add_excl(dev, addr);
3290090096bfSVlad Yasevich 
3291090096bfSVlad Yasevich 	/* Only return duplicate errors if NLM_F_EXCL is set */
3292090096bfSVlad Yasevich 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
3293090096bfSVlad Yasevich 		err = 0;
3294090096bfSVlad Yasevich 
3295090096bfSVlad Yasevich 	return err;
3296090096bfSVlad Yasevich }
3297090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_add);
3298090096bfSVlad Yasevich 
3299b88d12e4SFlorian Westphal static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid,
3300b88d12e4SFlorian Westphal 			 struct netlink_ext_ack *extack)
3301f6f6424bSJiri Pirko {
3302f6f6424bSJiri Pirko 	u16 vid = 0;
3303f6f6424bSJiri Pirko 
3304f6f6424bSJiri Pirko 	if (vlan_attr) {
3305f6f6424bSJiri Pirko 		if (nla_len(vlan_attr) != sizeof(u16)) {
3306b88d12e4SFlorian Westphal 			NL_SET_ERR_MSG(extack, "invalid vlan attribute size");
3307f6f6424bSJiri Pirko 			return -EINVAL;
3308f6f6424bSJiri Pirko 		}
3309f6f6424bSJiri Pirko 
3310f6f6424bSJiri Pirko 		vid = nla_get_u16(vlan_attr);
3311f6f6424bSJiri Pirko 
3312f6f6424bSJiri Pirko 		if (!vid || vid >= VLAN_VID_MASK) {
3313b88d12e4SFlorian Westphal 			NL_SET_ERR_MSG(extack, "invalid vlan id");
3314f6f6424bSJiri Pirko 			return -EINVAL;
3315f6f6424bSJiri Pirko 		}
3316f6f6424bSJiri Pirko 	}
3317f6f6424bSJiri Pirko 	*p_vid = vid;
3318f6f6424bSJiri Pirko 	return 0;
3319f6f6424bSJiri Pirko }
3320f6f6424bSJiri Pirko 
3321c21ef3e3SDavid Ahern static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
3322c21ef3e3SDavid Ahern 			struct netlink_ext_ack *extack)
332377162022SJohn Fastabend {
332477162022SJohn Fastabend 	struct net *net = sock_net(skb->sk);
332577162022SJohn Fastabend 	struct ndmsg *ndm;
332677162022SJohn Fastabend 	struct nlattr *tb[NDA_MAX+1];
332777162022SJohn Fastabend 	struct net_device *dev;
332877162022SJohn Fastabend 	u8 *addr;
3329f6f6424bSJiri Pirko 	u16 vid;
333077162022SJohn Fastabend 	int err;
333177162022SJohn Fastabend 
3332c21ef3e3SDavid Ahern 	err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
333377162022SJohn Fastabend 	if (err < 0)
333477162022SJohn Fastabend 		return err;
333577162022SJohn Fastabend 
333677162022SJohn Fastabend 	ndm = nlmsg_data(nlh);
333777162022SJohn Fastabend 	if (ndm->ndm_ifindex == 0) {
3338b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "invalid ifindex");
333977162022SJohn Fastabend 		return -EINVAL;
334077162022SJohn Fastabend 	}
334177162022SJohn Fastabend 
334277162022SJohn Fastabend 	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
334377162022SJohn Fastabend 	if (dev == NULL) {
3344b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "unknown ifindex");
334577162022SJohn Fastabend 		return -ENODEV;
334677162022SJohn Fastabend 	}
334777162022SJohn Fastabend 
334877162022SJohn Fastabend 	if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
3349b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "invalid address");
335077162022SJohn Fastabend 		return -EINVAL;
335177162022SJohn Fastabend 	}
335277162022SJohn Fastabend 
335377162022SJohn Fastabend 	addr = nla_data(tb[NDA_LLADDR]);
335477162022SJohn Fastabend 
3355b88d12e4SFlorian Westphal 	err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
3356f6f6424bSJiri Pirko 	if (err)
3357f6f6424bSJiri Pirko 		return err;
3358f6f6424bSJiri Pirko 
335977162022SJohn Fastabend 	err = -EOPNOTSUPP;
336077162022SJohn Fastabend 
336177162022SJohn Fastabend 	/* Support fdb on master device the net/bridge default case */
336277162022SJohn Fastabend 	if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
336377162022SJohn Fastabend 	    (dev->priv_flags & IFF_BRIDGE_PORT)) {
3364898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3365898e5061SJiri Pirko 		const struct net_device_ops *ops = br_dev->netdev_ops;
3366898e5061SJiri Pirko 
3367f6f6424bSJiri Pirko 		err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
3368f6f6424bSJiri Pirko 				       nlh->nlmsg_flags);
336977162022SJohn Fastabend 		if (err)
337077162022SJohn Fastabend 			goto out;
337177162022SJohn Fastabend 		else
337277162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_MASTER;
337377162022SJohn Fastabend 	}
337477162022SJohn Fastabend 
337577162022SJohn Fastabend 	/* Embedded bridge, macvlan, and any other device support */
3376090096bfSVlad Yasevich 	if ((ndm->ndm_flags & NTF_SELF)) {
3377090096bfSVlad Yasevich 		if (dev->netdev_ops->ndo_fdb_add)
3378090096bfSVlad Yasevich 			err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
3379f6f6424bSJiri Pirko 							   vid,
3380090096bfSVlad Yasevich 							   nlh->nlmsg_flags);
3381090096bfSVlad Yasevich 		else
3382f6f6424bSJiri Pirko 			err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
338377162022SJohn Fastabend 					       nlh->nlmsg_flags);
338477162022SJohn Fastabend 
33853ff661c3SJohn Fastabend 		if (!err) {
3386b3379041SHubert Sokolowski 			rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
3387b3379041SHubert Sokolowski 					ndm->ndm_state);
338877162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_SELF;
338977162022SJohn Fastabend 		}
33903ff661c3SJohn Fastabend 	}
339177162022SJohn Fastabend out:
339277162022SJohn Fastabend 	return err;
339377162022SJohn Fastabend }
339477162022SJohn Fastabend 
3395090096bfSVlad Yasevich /**
3396090096bfSVlad Yasevich  * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
3397090096bfSVlad Yasevich  */
3398090096bfSVlad Yasevich int ndo_dflt_fdb_del(struct ndmsg *ndm,
3399090096bfSVlad Yasevich 		     struct nlattr *tb[],
3400090096bfSVlad Yasevich 		     struct net_device *dev,
3401f6f6424bSJiri Pirko 		     const unsigned char *addr, u16 vid)
3402090096bfSVlad Yasevich {
3403c8a89c4aSAlexander Duyck 	int err = -EINVAL;
3404090096bfSVlad Yasevich 
3405090096bfSVlad Yasevich 	/* If aging addresses are supported device will need to
3406090096bfSVlad Yasevich 	 * implement its own handler for this.
3407090096bfSVlad Yasevich 	 */
340864535993SSridhar Samudrala 	if (!(ndm->ndm_state & NUD_PERMANENT)) {
3409090096bfSVlad Yasevich 		pr_info("%s: FDB only supports static addresses\n", dev->name);
3410c8a89c4aSAlexander Duyck 		return err;
3411090096bfSVlad Yasevich 	}
3412090096bfSVlad Yasevich 
3413090096bfSVlad Yasevich 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3414090096bfSVlad Yasevich 		err = dev_uc_del(dev, addr);
3415090096bfSVlad Yasevich 	else if (is_multicast_ether_addr(addr))
3416090096bfSVlad Yasevich 		err = dev_mc_del(dev, addr);
3417090096bfSVlad Yasevich 
3418090096bfSVlad Yasevich 	return err;
3419090096bfSVlad Yasevich }
3420090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_del);
3421090096bfSVlad Yasevich 
3422c21ef3e3SDavid Ahern static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
3423c21ef3e3SDavid Ahern 			struct netlink_ext_ack *extack)
342477162022SJohn Fastabend {
342577162022SJohn Fastabend 	struct net *net = sock_net(skb->sk);
342677162022SJohn Fastabend 	struct ndmsg *ndm;
34271690be63SVlad Yasevich 	struct nlattr *tb[NDA_MAX+1];
342877162022SJohn Fastabend 	struct net_device *dev;
342977162022SJohn Fastabend 	int err = -EINVAL;
343077162022SJohn Fastabend 	__u8 *addr;
3431f6f6424bSJiri Pirko 	u16 vid;
343277162022SJohn Fastabend 
343390f62cf3SEric W. Biederman 	if (!netlink_capable(skb, CAP_NET_ADMIN))
34341690be63SVlad Yasevich 		return -EPERM;
34351690be63SVlad Yasevich 
3436c21ef3e3SDavid Ahern 	err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
34371690be63SVlad Yasevich 	if (err < 0)
34381690be63SVlad Yasevich 		return err;
343977162022SJohn Fastabend 
344077162022SJohn Fastabend 	ndm = nlmsg_data(nlh);
344177162022SJohn Fastabend 	if (ndm->ndm_ifindex == 0) {
3442b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "invalid ifindex");
344377162022SJohn Fastabend 		return -EINVAL;
344477162022SJohn Fastabend 	}
344577162022SJohn Fastabend 
344677162022SJohn Fastabend 	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
344777162022SJohn Fastabend 	if (dev == NULL) {
3448b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "unknown ifindex");
344977162022SJohn Fastabend 		return -ENODEV;
345077162022SJohn Fastabend 	}
345177162022SJohn Fastabend 
34521690be63SVlad Yasevich 	if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
3453b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "invalid address");
345477162022SJohn Fastabend 		return -EINVAL;
345577162022SJohn Fastabend 	}
345677162022SJohn Fastabend 
34571690be63SVlad Yasevich 	addr = nla_data(tb[NDA_LLADDR]);
34581690be63SVlad Yasevich 
3459b88d12e4SFlorian Westphal 	err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
3460f6f6424bSJiri Pirko 	if (err)
3461f6f6424bSJiri Pirko 		return err;
3462f6f6424bSJiri Pirko 
346377162022SJohn Fastabend 	err = -EOPNOTSUPP;
346477162022SJohn Fastabend 
346577162022SJohn Fastabend 	/* Support fdb on master device the net/bridge default case */
346677162022SJohn Fastabend 	if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
346777162022SJohn Fastabend 	    (dev->priv_flags & IFF_BRIDGE_PORT)) {
3468898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3469898e5061SJiri Pirko 		const struct net_device_ops *ops = br_dev->netdev_ops;
347077162022SJohn Fastabend 
3471898e5061SJiri Pirko 		if (ops->ndo_fdb_del)
3472f6f6424bSJiri Pirko 			err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
347377162022SJohn Fastabend 
347477162022SJohn Fastabend 		if (err)
347577162022SJohn Fastabend 			goto out;
347677162022SJohn Fastabend 		else
347777162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_MASTER;
347877162022SJohn Fastabend 	}
347977162022SJohn Fastabend 
348077162022SJohn Fastabend 	/* Embedded bridge, macvlan, and any other device support */
3481090096bfSVlad Yasevich 	if (ndm->ndm_flags & NTF_SELF) {
3482090096bfSVlad Yasevich 		if (dev->netdev_ops->ndo_fdb_del)
3483f6f6424bSJiri Pirko 			err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
3484f6f6424bSJiri Pirko 							   vid);
3485090096bfSVlad Yasevich 		else
3486f6f6424bSJiri Pirko 			err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
348777162022SJohn Fastabend 
34883ff661c3SJohn Fastabend 		if (!err) {
3489b3379041SHubert Sokolowski 			rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
3490b3379041SHubert Sokolowski 					ndm->ndm_state);
349177162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_SELF;
349277162022SJohn Fastabend 		}
34933ff661c3SJohn Fastabend 	}
349477162022SJohn Fastabend out:
349577162022SJohn Fastabend 	return err;
349677162022SJohn Fastabend }
349777162022SJohn Fastabend 
3498d83b0603SJohn Fastabend static int nlmsg_populate_fdb(struct sk_buff *skb,
3499d83b0603SJohn Fastabend 			      struct netlink_callback *cb,
3500d83b0603SJohn Fastabend 			      struct net_device *dev,
3501d83b0603SJohn Fastabend 			      int *idx,
3502d83b0603SJohn Fastabend 			      struct netdev_hw_addr_list *list)
3503d83b0603SJohn Fastabend {
3504d83b0603SJohn Fastabend 	struct netdev_hw_addr *ha;
3505d83b0603SJohn Fastabend 	int err;
350615e47304SEric W. Biederman 	u32 portid, seq;
3507d83b0603SJohn Fastabend 
350815e47304SEric W. Biederman 	portid = NETLINK_CB(cb->skb).portid;
3509d83b0603SJohn Fastabend 	seq = cb->nlh->nlmsg_seq;
3510d83b0603SJohn Fastabend 
3511d83b0603SJohn Fastabend 	list_for_each_entry(ha, &list->list, list) {
3512d297653dSRoopa Prabhu 		if (*idx < cb->args[2])
3513d83b0603SJohn Fastabend 			goto skip;
3514d83b0603SJohn Fastabend 
35151e53d5bbSHubert Sokolowski 		err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
3516a7a558feSJohn Fastabend 					      portid, seq,
35171c104a6bSNicolas Dichtel 					      RTM_NEWNEIGH, NTF_SELF,
3518b3379041SHubert Sokolowski 					      NLM_F_MULTI, NUD_PERMANENT);
3519d83b0603SJohn Fastabend 		if (err < 0)
3520d83b0603SJohn Fastabend 			return err;
3521d83b0603SJohn Fastabend skip:
3522d83b0603SJohn Fastabend 		*idx += 1;
3523d83b0603SJohn Fastabend 	}
3524d83b0603SJohn Fastabend 	return 0;
3525d83b0603SJohn Fastabend }
3526d83b0603SJohn Fastabend 
3527d83b0603SJohn Fastabend /**
35282c53040fSBen Hutchings  * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
3529d83b0603SJohn Fastabend  * @nlh: netlink message header
3530d83b0603SJohn Fastabend  * @dev: netdevice
3531d83b0603SJohn Fastabend  *
3532d83b0603SJohn Fastabend  * Default netdevice operation to dump the existing unicast address list.
353391f3e7b1SJohn Fastabend  * Returns number of addresses from list put in skb.
3534d83b0603SJohn Fastabend  */
3535d83b0603SJohn Fastabend int ndo_dflt_fdb_dump(struct sk_buff *skb,
3536d83b0603SJohn Fastabend 		      struct netlink_callback *cb,
3537d83b0603SJohn Fastabend 		      struct net_device *dev,
35385d5eacb3SJamal Hadi Salim 		      struct net_device *filter_dev,
3539d297653dSRoopa Prabhu 		      int *idx)
3540d83b0603SJohn Fastabend {
3541d83b0603SJohn Fastabend 	int err;
3542d83b0603SJohn Fastabend 
3543d83b0603SJohn Fastabend 	netif_addr_lock_bh(dev);
3544d297653dSRoopa Prabhu 	err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
3545d83b0603SJohn Fastabend 	if (err)
3546d83b0603SJohn Fastabend 		goto out;
35472934c9dbSZhang Shengju 	err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
3548d83b0603SJohn Fastabend out:
3549d83b0603SJohn Fastabend 	netif_addr_unlock_bh(dev);
3550d297653dSRoopa Prabhu 	return err;
3551d83b0603SJohn Fastabend }
3552d83b0603SJohn Fastabend EXPORT_SYMBOL(ndo_dflt_fdb_dump);
3553d83b0603SJohn Fastabend 
355477162022SJohn Fastabend static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
355577162022SJohn Fastabend {
355677162022SJohn Fastabend 	struct net_device *dev;
35575e6d2435SJamal Hadi Salim 	struct nlattr *tb[IFLA_MAX+1];
35585e6d2435SJamal Hadi Salim 	struct net_device *br_dev = NULL;
35595e6d2435SJamal Hadi Salim 	const struct net_device_ops *ops = NULL;
35605e6d2435SJamal Hadi Salim 	const struct net_device_ops *cops = NULL;
35615e6d2435SJamal Hadi Salim 	struct ifinfomsg *ifm = nlmsg_data(cb->nlh);
35625e6d2435SJamal Hadi Salim 	struct net *net = sock_net(skb->sk);
3563d297653dSRoopa Prabhu 	struct hlist_head *head;
35645e6d2435SJamal Hadi Salim 	int brport_idx = 0;
35655e6d2435SJamal Hadi Salim 	int br_idx = 0;
3566d297653dSRoopa Prabhu 	int h, s_h;
3567d297653dSRoopa Prabhu 	int idx = 0, s_idx;
3568d297653dSRoopa Prabhu 	int err = 0;
3569d297653dSRoopa Prabhu 	int fidx = 0;
357077162022SJohn Fastabend 
35710ff50e83SAlexander Potapenko 	err = nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb,
35720ff50e83SAlexander Potapenko 			  IFLA_MAX, ifla_policy, NULL);
35730ff50e83SAlexander Potapenko 	if (err < 0) {
35740ff50e83SAlexander Potapenko 		return -EINVAL;
35750ff50e83SAlexander Potapenko 	} else if (err == 0) {
35765e6d2435SJamal Hadi Salim 		if (tb[IFLA_MASTER])
35775e6d2435SJamal Hadi Salim 			br_idx = nla_get_u32(tb[IFLA_MASTER]);
35785e6d2435SJamal Hadi Salim 	}
357977162022SJohn Fastabend 
35805e6d2435SJamal Hadi Salim 	brport_idx = ifm->ifi_index;
35815e6d2435SJamal Hadi Salim 
35825e6d2435SJamal Hadi Salim 	if (br_idx) {
35835e6d2435SJamal Hadi Salim 		br_dev = __dev_get_by_index(net, br_idx);
35845e6d2435SJamal Hadi Salim 		if (!br_dev)
35855e6d2435SJamal Hadi Salim 			return -ENODEV;
35865e6d2435SJamal Hadi Salim 
3587898e5061SJiri Pirko 		ops = br_dev->netdev_ops;
35885e6d2435SJamal Hadi Salim 	}
35895e6d2435SJamal Hadi Salim 
3590d297653dSRoopa Prabhu 	s_h = cb->args[0];
3591d297653dSRoopa Prabhu 	s_idx = cb->args[1];
3592d297653dSRoopa Prabhu 
3593d297653dSRoopa Prabhu 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
3594d297653dSRoopa Prabhu 		idx = 0;
3595d297653dSRoopa Prabhu 		head = &net->dev_index_head[h];
3596d297653dSRoopa Prabhu 		hlist_for_each_entry(dev, head, index_hlist) {
3597d297653dSRoopa Prabhu 
35985e6d2435SJamal Hadi Salim 			if (brport_idx && (dev->ifindex != brport_idx))
35995e6d2435SJamal Hadi Salim 				continue;
36005e6d2435SJamal Hadi Salim 
36015e6d2435SJamal Hadi Salim 			if (!br_idx) { /* user did not specify a specific bridge */
36025e6d2435SJamal Hadi Salim 				if (dev->priv_flags & IFF_BRIDGE_PORT) {
36035e6d2435SJamal Hadi Salim 					br_dev = netdev_master_upper_dev_get(dev);
36045e6d2435SJamal Hadi Salim 					cops = br_dev->netdev_ops;
36055e6d2435SJamal Hadi Salim 				}
36065e6d2435SJamal Hadi Salim 			} else {
36075e6d2435SJamal Hadi Salim 				if (dev != br_dev &&
36085e6d2435SJamal Hadi Salim 				    !(dev->priv_flags & IFF_BRIDGE_PORT))
36095e6d2435SJamal Hadi Salim 					continue;
36105e6d2435SJamal Hadi Salim 
36115e6d2435SJamal Hadi Salim 				if (br_dev != netdev_master_upper_dev_get(dev) &&
36125e6d2435SJamal Hadi Salim 				    !(dev->priv_flags & IFF_EBRIDGE))
36135e6d2435SJamal Hadi Salim 					continue;
36145e6d2435SJamal Hadi Salim 				cops = ops;
36155e6d2435SJamal Hadi Salim 			}
36165e6d2435SJamal Hadi Salim 
3617d297653dSRoopa Prabhu 			if (idx < s_idx)
3618d297653dSRoopa Prabhu 				goto cont;
3619d297653dSRoopa Prabhu 
36205e6d2435SJamal Hadi Salim 			if (dev->priv_flags & IFF_BRIDGE_PORT) {
3621d297653dSRoopa Prabhu 				if (cops && cops->ndo_fdb_dump) {
3622d297653dSRoopa Prabhu 					err = cops->ndo_fdb_dump(skb, cb,
3623d297653dSRoopa Prabhu 								br_dev, dev,
3624d297653dSRoopa Prabhu 								&fidx);
3625d297653dSRoopa Prabhu 					if (err == -EMSGSIZE)
3626d297653dSRoopa Prabhu 						goto out;
362777162022SJohn Fastabend 				}
3628d297653dSRoopa Prabhu 			}
362977162022SJohn Fastabend 
36305e6d2435SJamal Hadi Salim 			if (dev->netdev_ops->ndo_fdb_dump)
3631d297653dSRoopa Prabhu 				err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
3632d297653dSRoopa Prabhu 								    dev, NULL,
3633d297653dSRoopa Prabhu 								    &fidx);
36346cb69742SHubert Sokolowski 			else
3635d297653dSRoopa Prabhu 				err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
3636d297653dSRoopa Prabhu 							&fidx);
3637d297653dSRoopa Prabhu 			if (err == -EMSGSIZE)
3638d297653dSRoopa Prabhu 				goto out;
36395e6d2435SJamal Hadi Salim 
36405e6d2435SJamal Hadi Salim 			cops = NULL;
3641d297653dSRoopa Prabhu 
3642d297653dSRoopa Prabhu 			/* reset fdb offset to 0 for rest of the interfaces */
3643d297653dSRoopa Prabhu 			cb->args[2] = 0;
3644d297653dSRoopa Prabhu 			fidx = 0;
3645d297653dSRoopa Prabhu cont:
3646d297653dSRoopa Prabhu 			idx++;
3647d297653dSRoopa Prabhu 		}
364877162022SJohn Fastabend 	}
364977162022SJohn Fastabend 
3650d297653dSRoopa Prabhu out:
3651d297653dSRoopa Prabhu 	cb->args[0] = h;
3652d297653dSRoopa Prabhu 	cb->args[1] = idx;
3653d297653dSRoopa Prabhu 	cb->args[2] = fidx;
3654d297653dSRoopa Prabhu 
365577162022SJohn Fastabend 	return skb->len;
365677162022SJohn Fastabend }
365777162022SJohn Fastabend 
36582c3c031cSScott Feldman static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
36592c3c031cSScott Feldman 			       unsigned int attrnum, unsigned int flag)
36602c3c031cSScott Feldman {
36612c3c031cSScott Feldman 	if (mask & flag)
36622c3c031cSScott Feldman 		return nla_put_u8(skb, attrnum, !!(flags & flag));
36632c3c031cSScott Feldman 	return 0;
36642c3c031cSScott Feldman }
36652c3c031cSScott Feldman 
3666815cccbfSJohn Fastabend int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
36672c3c031cSScott Feldman 			    struct net_device *dev, u16 mode,
36687d4f8d87SScott Feldman 			    u32 flags, u32 mask, int nlflags,
36697d4f8d87SScott Feldman 			    u32 filter_mask,
36707d4f8d87SScott Feldman 			    int (*vlan_fill)(struct sk_buff *skb,
36717d4f8d87SScott Feldman 					     struct net_device *dev,
36727d4f8d87SScott Feldman 					     u32 filter_mask))
3673815cccbfSJohn Fastabend {
3674815cccbfSJohn Fastabend 	struct nlmsghdr *nlh;
3675815cccbfSJohn Fastabend 	struct ifinfomsg *ifm;
3676815cccbfSJohn Fastabend 	struct nlattr *br_afspec;
36772c3c031cSScott Feldman 	struct nlattr *protinfo;
3678815cccbfSJohn Fastabend 	u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
3679898e5061SJiri Pirko 	struct net_device *br_dev = netdev_master_upper_dev_get(dev);
36807d4f8d87SScott Feldman 	int err = 0;
3681815cccbfSJohn Fastabend 
368246c264daSNicolas Dichtel 	nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
3683815cccbfSJohn Fastabend 	if (nlh == NULL)
3684815cccbfSJohn Fastabend 		return -EMSGSIZE;
3685815cccbfSJohn Fastabend 
3686815cccbfSJohn Fastabend 	ifm = nlmsg_data(nlh);
3687815cccbfSJohn Fastabend 	ifm->ifi_family = AF_BRIDGE;
3688815cccbfSJohn Fastabend 	ifm->__ifi_pad = 0;
3689815cccbfSJohn Fastabend 	ifm->ifi_type = dev->type;
3690815cccbfSJohn Fastabend 	ifm->ifi_index = dev->ifindex;
3691815cccbfSJohn Fastabend 	ifm->ifi_flags = dev_get_flags(dev);
3692815cccbfSJohn Fastabend 	ifm->ifi_change = 0;
3693815cccbfSJohn Fastabend 
3694815cccbfSJohn Fastabend 
3695815cccbfSJohn Fastabend 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
3696815cccbfSJohn Fastabend 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
3697815cccbfSJohn Fastabend 	    nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
3698898e5061SJiri Pirko 	    (br_dev &&
3699898e5061SJiri Pirko 	     nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
3700815cccbfSJohn Fastabend 	    (dev->addr_len &&
3701815cccbfSJohn Fastabend 	     nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
3702a54acb3aSNicolas Dichtel 	    (dev->ifindex != dev_get_iflink(dev) &&
3703a54acb3aSNicolas Dichtel 	     nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
3704815cccbfSJohn Fastabend 		goto nla_put_failure;
3705815cccbfSJohn Fastabend 
3706815cccbfSJohn Fastabend 	br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
3707815cccbfSJohn Fastabend 	if (!br_afspec)
3708815cccbfSJohn Fastabend 		goto nla_put_failure;
3709815cccbfSJohn Fastabend 
37101d460b98SRoopa Prabhu 	if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
3711815cccbfSJohn Fastabend 		nla_nest_cancel(skb, br_afspec);
3712815cccbfSJohn Fastabend 		goto nla_put_failure;
3713815cccbfSJohn Fastabend 	}
37141d460b98SRoopa Prabhu 
37151d460b98SRoopa Prabhu 	if (mode != BRIDGE_MODE_UNDEF) {
37161d460b98SRoopa Prabhu 		if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
37171d460b98SRoopa Prabhu 			nla_nest_cancel(skb, br_afspec);
37181d460b98SRoopa Prabhu 			goto nla_put_failure;
37191d460b98SRoopa Prabhu 		}
37201d460b98SRoopa Prabhu 	}
37217d4f8d87SScott Feldman 	if (vlan_fill) {
37227d4f8d87SScott Feldman 		err = vlan_fill(skb, dev, filter_mask);
37237d4f8d87SScott Feldman 		if (err) {
37247d4f8d87SScott Feldman 			nla_nest_cancel(skb, br_afspec);
37257d4f8d87SScott Feldman 			goto nla_put_failure;
37267d4f8d87SScott Feldman 		}
37277d4f8d87SScott Feldman 	}
3728815cccbfSJohn Fastabend 	nla_nest_end(skb, br_afspec);
3729815cccbfSJohn Fastabend 
37302c3c031cSScott Feldman 	protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
37312c3c031cSScott Feldman 	if (!protinfo)
37322c3c031cSScott Feldman 		goto nla_put_failure;
37332c3c031cSScott Feldman 
37342c3c031cSScott Feldman 	if (brport_nla_put_flag(skb, flags, mask,
37352c3c031cSScott Feldman 				IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
37362c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
37372c3c031cSScott Feldman 				IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
37382c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
37392c3c031cSScott Feldman 				IFLA_BRPORT_FAST_LEAVE,
37402c3c031cSScott Feldman 				BR_MULTICAST_FAST_LEAVE) ||
37412c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
37422c3c031cSScott Feldman 				IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
37432c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
37442c3c031cSScott Feldman 				IFLA_BRPORT_LEARNING, BR_LEARNING) ||
37452c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
37462c3c031cSScott Feldman 				IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
37472c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
37482c3c031cSScott Feldman 				IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
37492c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
37502c3c031cSScott Feldman 				IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
37512c3c031cSScott Feldman 		nla_nest_cancel(skb, protinfo);
37522c3c031cSScott Feldman 		goto nla_put_failure;
37532c3c031cSScott Feldman 	}
37542c3c031cSScott Feldman 
37552c3c031cSScott Feldman 	nla_nest_end(skb, protinfo);
37562c3c031cSScott Feldman 
3757053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
3758053c095aSJohannes Berg 	return 0;
3759815cccbfSJohn Fastabend nla_put_failure:
3760815cccbfSJohn Fastabend 	nlmsg_cancel(skb, nlh);
37617d4f8d87SScott Feldman 	return err ? err : -EMSGSIZE;
3762815cccbfSJohn Fastabend }
37637d4f8d87SScott Feldman EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
3764815cccbfSJohn Fastabend 
3765e5a55a89SJohn Fastabend static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
3766e5a55a89SJohn Fastabend {
3767e5a55a89SJohn Fastabend 	struct net *net = sock_net(skb->sk);
3768e5a55a89SJohn Fastabend 	struct net_device *dev;
3769e5a55a89SJohn Fastabend 	int idx = 0;
3770e5a55a89SJohn Fastabend 	u32 portid = NETLINK_CB(cb->skb).portid;
3771e5a55a89SJohn Fastabend 	u32 seq = cb->nlh->nlmsg_seq;
37726cbdceebSVlad Yasevich 	u32 filter_mask = 0;
3773d64f69b0SRoopa Prabhu 	int err;
37746cbdceebSVlad Yasevich 
3775aa68c20fSThomas Graf 	if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) {
3776aa68c20fSThomas Graf 		struct nlattr *extfilt;
3777aa68c20fSThomas Graf 
37783e805ad2SAsbjoern Sloth Toennesen 		extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
37796cbdceebSVlad Yasevich 					  IFLA_EXT_MASK);
3780aa68c20fSThomas Graf 		if (extfilt) {
3781aa68c20fSThomas Graf 			if (nla_len(extfilt) < sizeof(filter_mask))
3782aa68c20fSThomas Graf 				return -EINVAL;
3783aa68c20fSThomas Graf 
37846cbdceebSVlad Yasevich 			filter_mask = nla_get_u32(extfilt);
3785aa68c20fSThomas Graf 		}
3786aa68c20fSThomas Graf 	}
3787e5a55a89SJohn Fastabend 
3788e5a55a89SJohn Fastabend 	rcu_read_lock();
3789e5a55a89SJohn Fastabend 	for_each_netdev_rcu(net, dev) {
3790e5a55a89SJohn Fastabend 		const struct net_device_ops *ops = dev->netdev_ops;
3791898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3792e5a55a89SJohn Fastabend 
3793898e5061SJiri Pirko 		if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
3794d64f69b0SRoopa Prabhu 			if (idx >= cb->args[0]) {
3795d64f69b0SRoopa Prabhu 				err = br_dev->netdev_ops->ndo_bridge_getlink(
3796d64f69b0SRoopa Prabhu 						skb, portid, seq, dev,
3797d64f69b0SRoopa Prabhu 						filter_mask, NLM_F_MULTI);
3798f6c5775fSDavid Ahern 				if (err < 0 && err != -EOPNOTSUPP) {
3799f6c5775fSDavid Ahern 					if (likely(skb->len))
3800e5a55a89SJohn Fastabend 						break;
3801f6c5775fSDavid Ahern 
3802f6c5775fSDavid Ahern 					goto out_err;
3803f6c5775fSDavid Ahern 				}
3804d64f69b0SRoopa Prabhu 			}
3805e5a55a89SJohn Fastabend 			idx++;
3806e5a55a89SJohn Fastabend 		}
3807e5a55a89SJohn Fastabend 
3808e5a55a89SJohn Fastabend 		if (ops->ndo_bridge_getlink) {
3809d64f69b0SRoopa Prabhu 			if (idx >= cb->args[0]) {
3810d64f69b0SRoopa Prabhu 				err = ops->ndo_bridge_getlink(skb, portid,
3811d64f69b0SRoopa Prabhu 							      seq, dev,
381246c264daSNicolas Dichtel 							      filter_mask,
3813d64f69b0SRoopa Prabhu 							      NLM_F_MULTI);
3814f6c5775fSDavid Ahern 				if (err < 0 && err != -EOPNOTSUPP) {
3815f6c5775fSDavid Ahern 					if (likely(skb->len))
3816e5a55a89SJohn Fastabend 						break;
3817f6c5775fSDavid Ahern 
3818f6c5775fSDavid Ahern 					goto out_err;
3819f6c5775fSDavid Ahern 				}
3820d64f69b0SRoopa Prabhu 			}
3821e5a55a89SJohn Fastabend 			idx++;
3822e5a55a89SJohn Fastabend 		}
3823e5a55a89SJohn Fastabend 	}
3824f6c5775fSDavid Ahern 	err = skb->len;
3825f6c5775fSDavid Ahern out_err:
3826e5a55a89SJohn Fastabend 	rcu_read_unlock();
3827e5a55a89SJohn Fastabend 	cb->args[0] = idx;
3828e5a55a89SJohn Fastabend 
3829f6c5775fSDavid Ahern 	return err;
3830e5a55a89SJohn Fastabend }
3831e5a55a89SJohn Fastabend 
38322469ffd7SJohn Fastabend static inline size_t bridge_nlmsg_size(void)
38332469ffd7SJohn Fastabend {
38342469ffd7SJohn Fastabend 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
38352469ffd7SJohn Fastabend 		+ nla_total_size(IFNAMSIZ)	/* IFLA_IFNAME */
38362469ffd7SJohn Fastabend 		+ nla_total_size(MAX_ADDR_LEN)	/* IFLA_ADDRESS */
38372469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_MASTER */
38382469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_MTU */
38392469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_LINK */
38402469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_OPERSTATE */
38412469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u8))	/* IFLA_PROTINFO */
38422469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(struct nlattr))	/* IFLA_AF_SPEC */
38432469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u16))	/* IFLA_BRIDGE_FLAGS */
38442469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u16));	/* IFLA_BRIDGE_MODE */
38452469ffd7SJohn Fastabend }
38462469ffd7SJohn Fastabend 
384702dba438SRoopa Prabhu static int rtnl_bridge_notify(struct net_device *dev)
38482469ffd7SJohn Fastabend {
38492469ffd7SJohn Fastabend 	struct net *net = dev_net(dev);
38502469ffd7SJohn Fastabend 	struct sk_buff *skb;
38512469ffd7SJohn Fastabend 	int err = -EOPNOTSUPP;
38522469ffd7SJohn Fastabend 
385302dba438SRoopa Prabhu 	if (!dev->netdev_ops->ndo_bridge_getlink)
385402dba438SRoopa Prabhu 		return 0;
385502dba438SRoopa Prabhu 
38562469ffd7SJohn Fastabend 	skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
38572469ffd7SJohn Fastabend 	if (!skb) {
38582469ffd7SJohn Fastabend 		err = -ENOMEM;
38592469ffd7SJohn Fastabend 		goto errout;
38602469ffd7SJohn Fastabend 	}
38612469ffd7SJohn Fastabend 
386246c264daSNicolas Dichtel 	err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
3863c38e01b8SJohn Fastabend 	if (err < 0)
3864c38e01b8SJohn Fastabend 		goto errout;
38652469ffd7SJohn Fastabend 
386659ccaaaaSRoopa Prabhu 	if (!skb->len)
386759ccaaaaSRoopa Prabhu 		goto errout;
386859ccaaaaSRoopa Prabhu 
38692469ffd7SJohn Fastabend 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
38702469ffd7SJohn Fastabend 	return 0;
38712469ffd7SJohn Fastabend errout:
38722469ffd7SJohn Fastabend 	WARN_ON(err == -EMSGSIZE);
38732469ffd7SJohn Fastabend 	kfree_skb(skb);
387459ccaaaaSRoopa Prabhu 	if (err)
38752469ffd7SJohn Fastabend 		rtnl_set_sk_err(net, RTNLGRP_LINK, err);
38762469ffd7SJohn Fastabend 	return err;
38772469ffd7SJohn Fastabend }
38782469ffd7SJohn Fastabend 
3879c21ef3e3SDavid Ahern static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3880c21ef3e3SDavid Ahern 			       struct netlink_ext_ack *extack)
3881e5a55a89SJohn Fastabend {
3882e5a55a89SJohn Fastabend 	struct net *net = sock_net(skb->sk);
3883e5a55a89SJohn Fastabend 	struct ifinfomsg *ifm;
3884e5a55a89SJohn Fastabend 	struct net_device *dev;
38852469ffd7SJohn Fastabend 	struct nlattr *br_spec, *attr = NULL;
38862469ffd7SJohn Fastabend 	int rem, err = -EOPNOTSUPP;
38874de8b413SRosen, Rami 	u16 flags = 0;
3888c38e01b8SJohn Fastabend 	bool have_flags = false;
3889e5a55a89SJohn Fastabend 
3890e5a55a89SJohn Fastabend 	if (nlmsg_len(nlh) < sizeof(*ifm))
3891e5a55a89SJohn Fastabend 		return -EINVAL;
3892e5a55a89SJohn Fastabend 
3893e5a55a89SJohn Fastabend 	ifm = nlmsg_data(nlh);
3894e5a55a89SJohn Fastabend 	if (ifm->ifi_family != AF_BRIDGE)
3895e5a55a89SJohn Fastabend 		return -EPFNOSUPPORT;
3896e5a55a89SJohn Fastabend 
3897e5a55a89SJohn Fastabend 	dev = __dev_get_by_index(net, ifm->ifi_index);
3898e5a55a89SJohn Fastabend 	if (!dev) {
3899b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "unknown ifindex");
3900e5a55a89SJohn Fastabend 		return -ENODEV;
3901e5a55a89SJohn Fastabend 	}
3902e5a55a89SJohn Fastabend 
39032469ffd7SJohn Fastabend 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
39042469ffd7SJohn Fastabend 	if (br_spec) {
39052469ffd7SJohn Fastabend 		nla_for_each_nested(attr, br_spec, rem) {
39062469ffd7SJohn Fastabend 			if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
39076e8d1c55SThomas Graf 				if (nla_len(attr) < sizeof(flags))
39086e8d1c55SThomas Graf 					return -EINVAL;
39096e8d1c55SThomas Graf 
3910c38e01b8SJohn Fastabend 				have_flags = true;
39112469ffd7SJohn Fastabend 				flags = nla_get_u16(attr);
39122469ffd7SJohn Fastabend 				break;
39132469ffd7SJohn Fastabend 			}
39142469ffd7SJohn Fastabend 		}
39152469ffd7SJohn Fastabend 	}
39162469ffd7SJohn Fastabend 
39172469ffd7SJohn Fastabend 	if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
3918898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3919898e5061SJiri Pirko 
3920898e5061SJiri Pirko 		if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
39212469ffd7SJohn Fastabend 			err = -EOPNOTSUPP;
3922e5a55a89SJohn Fastabend 			goto out;
3923e5a55a89SJohn Fastabend 		}
3924e5a55a89SJohn Fastabend 
3925add511b3SRoopa Prabhu 		err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags);
39262469ffd7SJohn Fastabend 		if (err)
39272469ffd7SJohn Fastabend 			goto out;
39282469ffd7SJohn Fastabend 
39292469ffd7SJohn Fastabend 		flags &= ~BRIDGE_FLAGS_MASTER;
39302469ffd7SJohn Fastabend 	}
39312469ffd7SJohn Fastabend 
39322469ffd7SJohn Fastabend 	if ((flags & BRIDGE_FLAGS_SELF)) {
39332469ffd7SJohn Fastabend 		if (!dev->netdev_ops->ndo_bridge_setlink)
39342469ffd7SJohn Fastabend 			err = -EOPNOTSUPP;
39352469ffd7SJohn Fastabend 		else
3936add511b3SRoopa Prabhu 			err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
3937add511b3SRoopa Prabhu 								  flags);
393802dba438SRoopa Prabhu 		if (!err) {
39392469ffd7SJohn Fastabend 			flags &= ~BRIDGE_FLAGS_SELF;
394002dba438SRoopa Prabhu 
394102dba438SRoopa Prabhu 			/* Generate event to notify upper layer of bridge
394202dba438SRoopa Prabhu 			 * change
394302dba438SRoopa Prabhu 			 */
394402dba438SRoopa Prabhu 			err = rtnl_bridge_notify(dev);
394502dba438SRoopa Prabhu 		}
39462469ffd7SJohn Fastabend 	}
39472469ffd7SJohn Fastabend 
3948c38e01b8SJohn Fastabend 	if (have_flags)
39492469ffd7SJohn Fastabend 		memcpy(nla_data(attr), &flags, sizeof(flags));
3950e5a55a89SJohn Fastabend out:
3951e5a55a89SJohn Fastabend 	return err;
3952e5a55a89SJohn Fastabend }
3953e5a55a89SJohn Fastabend 
3954c21ef3e3SDavid Ahern static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
3955c21ef3e3SDavid Ahern 			       struct netlink_ext_ack *extack)
3956407af329SVlad Yasevich {
3957407af329SVlad Yasevich 	struct net *net = sock_net(skb->sk);
3958407af329SVlad Yasevich 	struct ifinfomsg *ifm;
3959407af329SVlad Yasevich 	struct net_device *dev;
3960407af329SVlad Yasevich 	struct nlattr *br_spec, *attr = NULL;
3961407af329SVlad Yasevich 	int rem, err = -EOPNOTSUPP;
39624de8b413SRosen, Rami 	u16 flags = 0;
3963407af329SVlad Yasevich 	bool have_flags = false;
3964407af329SVlad Yasevich 
3965407af329SVlad Yasevich 	if (nlmsg_len(nlh) < sizeof(*ifm))
3966407af329SVlad Yasevich 		return -EINVAL;
3967407af329SVlad Yasevich 
3968407af329SVlad Yasevich 	ifm = nlmsg_data(nlh);
3969407af329SVlad Yasevich 	if (ifm->ifi_family != AF_BRIDGE)
3970407af329SVlad Yasevich 		return -EPFNOSUPPORT;
3971407af329SVlad Yasevich 
3972407af329SVlad Yasevich 	dev = __dev_get_by_index(net, ifm->ifi_index);
3973407af329SVlad Yasevich 	if (!dev) {
3974b88d12e4SFlorian Westphal 		NL_SET_ERR_MSG(extack, "unknown ifindex");
3975407af329SVlad Yasevich 		return -ENODEV;
3976407af329SVlad Yasevich 	}
3977407af329SVlad Yasevich 
3978407af329SVlad Yasevich 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3979407af329SVlad Yasevich 	if (br_spec) {
3980407af329SVlad Yasevich 		nla_for_each_nested(attr, br_spec, rem) {
3981407af329SVlad Yasevich 			if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
39826e8d1c55SThomas Graf 				if (nla_len(attr) < sizeof(flags))
39836e8d1c55SThomas Graf 					return -EINVAL;
39846e8d1c55SThomas Graf 
3985407af329SVlad Yasevich 				have_flags = true;
3986407af329SVlad Yasevich 				flags = nla_get_u16(attr);
3987407af329SVlad Yasevich 				break;
3988407af329SVlad Yasevich 			}
3989407af329SVlad Yasevich 		}
3990407af329SVlad Yasevich 	}
3991407af329SVlad Yasevich 
3992407af329SVlad Yasevich 	if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
3993407af329SVlad Yasevich 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3994407af329SVlad Yasevich 
3995407af329SVlad Yasevich 		if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
3996407af329SVlad Yasevich 			err = -EOPNOTSUPP;
3997407af329SVlad Yasevich 			goto out;
3998407af329SVlad Yasevich 		}
3999407af329SVlad Yasevich 
4000add511b3SRoopa Prabhu 		err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
4001407af329SVlad Yasevich 		if (err)
4002407af329SVlad Yasevich 			goto out;
4003407af329SVlad Yasevich 
4004407af329SVlad Yasevich 		flags &= ~BRIDGE_FLAGS_MASTER;
4005407af329SVlad Yasevich 	}
4006407af329SVlad Yasevich 
4007407af329SVlad Yasevich 	if ((flags & BRIDGE_FLAGS_SELF)) {
4008407af329SVlad Yasevich 		if (!dev->netdev_ops->ndo_bridge_dellink)
4009407af329SVlad Yasevich 			err = -EOPNOTSUPP;
4010407af329SVlad Yasevich 		else
4011add511b3SRoopa Prabhu 			err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
4012add511b3SRoopa Prabhu 								  flags);
4013407af329SVlad Yasevich 
401402dba438SRoopa Prabhu 		if (!err) {
4015407af329SVlad Yasevich 			flags &= ~BRIDGE_FLAGS_SELF;
401602dba438SRoopa Prabhu 
401702dba438SRoopa Prabhu 			/* Generate event to notify upper layer of bridge
401802dba438SRoopa Prabhu 			 * change
401902dba438SRoopa Prabhu 			 */
402002dba438SRoopa Prabhu 			err = rtnl_bridge_notify(dev);
402102dba438SRoopa Prabhu 		}
4022407af329SVlad Yasevich 	}
4023407af329SVlad Yasevich 
4024407af329SVlad Yasevich 	if (have_flags)
4025407af329SVlad Yasevich 		memcpy(nla_data(attr), &flags, sizeof(flags));
4026407af329SVlad Yasevich out:
4027407af329SVlad Yasevich 	return err;
4028407af329SVlad Yasevich }
4029407af329SVlad Yasevich 
4030e8872a25SNikolay Aleksandrov static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
4031e8872a25SNikolay Aleksandrov {
4032e8872a25SNikolay Aleksandrov 	return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
4033e8872a25SNikolay Aleksandrov 	       (!idxattr || idxattr == attrid);
4034e8872a25SNikolay Aleksandrov }
4035e8872a25SNikolay Aleksandrov 
403669ae6ad2SNogah Frankel #define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
403769ae6ad2SNogah Frankel static int rtnl_get_offload_stats_attr_size(int attr_id)
403869ae6ad2SNogah Frankel {
403969ae6ad2SNogah Frankel 	switch (attr_id) {
404069ae6ad2SNogah Frankel 	case IFLA_OFFLOAD_XSTATS_CPU_HIT:
404169ae6ad2SNogah Frankel 		return sizeof(struct rtnl_link_stats64);
404269ae6ad2SNogah Frankel 	}
404369ae6ad2SNogah Frankel 
404469ae6ad2SNogah Frankel 	return 0;
404569ae6ad2SNogah Frankel }
404669ae6ad2SNogah Frankel 
404769ae6ad2SNogah Frankel static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev,
404869ae6ad2SNogah Frankel 				  int *prividx)
404969ae6ad2SNogah Frankel {
405069ae6ad2SNogah Frankel 	struct nlattr *attr = NULL;
405169ae6ad2SNogah Frankel 	int attr_id, size;
405269ae6ad2SNogah Frankel 	void *attr_data;
405369ae6ad2SNogah Frankel 	int err;
405469ae6ad2SNogah Frankel 
405569ae6ad2SNogah Frankel 	if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
405669ae6ad2SNogah Frankel 	      dev->netdev_ops->ndo_get_offload_stats))
405769ae6ad2SNogah Frankel 		return -ENODATA;
405869ae6ad2SNogah Frankel 
405969ae6ad2SNogah Frankel 	for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
406069ae6ad2SNogah Frankel 	     attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
406169ae6ad2SNogah Frankel 		if (attr_id < *prividx)
406269ae6ad2SNogah Frankel 			continue;
406369ae6ad2SNogah Frankel 
406469ae6ad2SNogah Frankel 		size = rtnl_get_offload_stats_attr_size(attr_id);
406569ae6ad2SNogah Frankel 		if (!size)
406669ae6ad2SNogah Frankel 			continue;
406769ae6ad2SNogah Frankel 
40683df5b3c6SOr Gerlitz 		if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
406969ae6ad2SNogah Frankel 			continue;
407069ae6ad2SNogah Frankel 
407169ae6ad2SNogah Frankel 		attr = nla_reserve_64bit(skb, attr_id, size,
407269ae6ad2SNogah Frankel 					 IFLA_OFFLOAD_XSTATS_UNSPEC);
407369ae6ad2SNogah Frankel 		if (!attr)
407469ae6ad2SNogah Frankel 			goto nla_put_failure;
407569ae6ad2SNogah Frankel 
407669ae6ad2SNogah Frankel 		attr_data = nla_data(attr);
407769ae6ad2SNogah Frankel 		memset(attr_data, 0, size);
407869ae6ad2SNogah Frankel 		err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev,
407969ae6ad2SNogah Frankel 							     attr_data);
408069ae6ad2SNogah Frankel 		if (err)
408169ae6ad2SNogah Frankel 			goto get_offload_stats_failure;
408269ae6ad2SNogah Frankel 	}
408369ae6ad2SNogah Frankel 
408469ae6ad2SNogah Frankel 	if (!attr)
408569ae6ad2SNogah Frankel 		return -ENODATA;
408669ae6ad2SNogah Frankel 
408769ae6ad2SNogah Frankel 	*prividx = 0;
408869ae6ad2SNogah Frankel 	return 0;
408969ae6ad2SNogah Frankel 
409069ae6ad2SNogah Frankel nla_put_failure:
409169ae6ad2SNogah Frankel 	err = -EMSGSIZE;
409269ae6ad2SNogah Frankel get_offload_stats_failure:
409369ae6ad2SNogah Frankel 	*prividx = attr_id;
409469ae6ad2SNogah Frankel 	return err;
409569ae6ad2SNogah Frankel }
409669ae6ad2SNogah Frankel 
409769ae6ad2SNogah Frankel static int rtnl_get_offload_stats_size(const struct net_device *dev)
409869ae6ad2SNogah Frankel {
409969ae6ad2SNogah Frankel 	int nla_size = 0;
410069ae6ad2SNogah Frankel 	int attr_id;
410169ae6ad2SNogah Frankel 	int size;
410269ae6ad2SNogah Frankel 
410369ae6ad2SNogah Frankel 	if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
410469ae6ad2SNogah Frankel 	      dev->netdev_ops->ndo_get_offload_stats))
410569ae6ad2SNogah Frankel 		return 0;
410669ae6ad2SNogah Frankel 
410769ae6ad2SNogah Frankel 	for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
410869ae6ad2SNogah Frankel 	     attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
41093df5b3c6SOr Gerlitz 		if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
411069ae6ad2SNogah Frankel 			continue;
411169ae6ad2SNogah Frankel 		size = rtnl_get_offload_stats_attr_size(attr_id);
411269ae6ad2SNogah Frankel 		nla_size += nla_total_size_64bit(size);
411369ae6ad2SNogah Frankel 	}
411469ae6ad2SNogah Frankel 
411569ae6ad2SNogah Frankel 	if (nla_size != 0)
411669ae6ad2SNogah Frankel 		nla_size += nla_total_size(0);
411769ae6ad2SNogah Frankel 
411869ae6ad2SNogah Frankel 	return nla_size;
411969ae6ad2SNogah Frankel }
412069ae6ad2SNogah Frankel 
412110c9ead9SRoopa Prabhu static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
412210c9ead9SRoopa Prabhu 			       int type, u32 pid, u32 seq, u32 change,
4123e8872a25SNikolay Aleksandrov 			       unsigned int flags, unsigned int filter_mask,
4124e8872a25SNikolay Aleksandrov 			       int *idxattr, int *prividx)
412510c9ead9SRoopa Prabhu {
412610c9ead9SRoopa Prabhu 	struct if_stats_msg *ifsm;
412710c9ead9SRoopa Prabhu 	struct nlmsghdr *nlh;
412810c9ead9SRoopa Prabhu 	struct nlattr *attr;
4129e8872a25SNikolay Aleksandrov 	int s_prividx = *prividx;
413069ae6ad2SNogah Frankel 	int err;
413110c9ead9SRoopa Prabhu 
413210c9ead9SRoopa Prabhu 	ASSERT_RTNL();
413310c9ead9SRoopa Prabhu 
413410c9ead9SRoopa Prabhu 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
413510c9ead9SRoopa Prabhu 	if (!nlh)
413610c9ead9SRoopa Prabhu 		return -EMSGSIZE;
413710c9ead9SRoopa Prabhu 
413810c9ead9SRoopa Prabhu 	ifsm = nlmsg_data(nlh);
4139ce024f42SNikolay Aleksandrov 	ifsm->family = PF_UNSPEC;
4140ce024f42SNikolay Aleksandrov 	ifsm->pad1 = 0;
4141ce024f42SNikolay Aleksandrov 	ifsm->pad2 = 0;
414210c9ead9SRoopa Prabhu 	ifsm->ifindex = dev->ifindex;
414310c9ead9SRoopa Prabhu 	ifsm->filter_mask = filter_mask;
414410c9ead9SRoopa Prabhu 
4145e8872a25SNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
414610c9ead9SRoopa Prabhu 		struct rtnl_link_stats64 *sp;
414710c9ead9SRoopa Prabhu 
414858414d32SNicolas Dichtel 		attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
414958414d32SNicolas Dichtel 					 sizeof(struct rtnl_link_stats64),
415058414d32SNicolas Dichtel 					 IFLA_STATS_UNSPEC);
415110c9ead9SRoopa Prabhu 		if (!attr)
415210c9ead9SRoopa Prabhu 			goto nla_put_failure;
415310c9ead9SRoopa Prabhu 
415410c9ead9SRoopa Prabhu 		sp = nla_data(attr);
415510c9ead9SRoopa Prabhu 		dev_get_stats(dev, sp);
415610c9ead9SRoopa Prabhu 	}
415710c9ead9SRoopa Prabhu 
415897a47facSNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
415997a47facSNikolay Aleksandrov 		const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
416097a47facSNikolay Aleksandrov 
416197a47facSNikolay Aleksandrov 		if (ops && ops->fill_linkxstats) {
416297a47facSNikolay Aleksandrov 			*idxattr = IFLA_STATS_LINK_XSTATS;
416397a47facSNikolay Aleksandrov 			attr = nla_nest_start(skb,
416497a47facSNikolay Aleksandrov 					      IFLA_STATS_LINK_XSTATS);
416597a47facSNikolay Aleksandrov 			if (!attr)
416697a47facSNikolay Aleksandrov 				goto nla_put_failure;
416797a47facSNikolay Aleksandrov 
416880e73cc5SNikolay Aleksandrov 			err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
416980e73cc5SNikolay Aleksandrov 			nla_nest_end(skb, attr);
417080e73cc5SNikolay Aleksandrov 			if (err)
417180e73cc5SNikolay Aleksandrov 				goto nla_put_failure;
417280e73cc5SNikolay Aleksandrov 			*idxattr = 0;
417380e73cc5SNikolay Aleksandrov 		}
417480e73cc5SNikolay Aleksandrov 	}
417580e73cc5SNikolay Aleksandrov 
417680e73cc5SNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
417780e73cc5SNikolay Aleksandrov 			     *idxattr)) {
417880e73cc5SNikolay Aleksandrov 		const struct rtnl_link_ops *ops = NULL;
417980e73cc5SNikolay Aleksandrov 		const struct net_device *master;
418080e73cc5SNikolay Aleksandrov 
418180e73cc5SNikolay Aleksandrov 		master = netdev_master_upper_dev_get(dev);
418280e73cc5SNikolay Aleksandrov 		if (master)
418380e73cc5SNikolay Aleksandrov 			ops = master->rtnl_link_ops;
418480e73cc5SNikolay Aleksandrov 		if (ops && ops->fill_linkxstats) {
418580e73cc5SNikolay Aleksandrov 			*idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
418680e73cc5SNikolay Aleksandrov 			attr = nla_nest_start(skb,
418780e73cc5SNikolay Aleksandrov 					      IFLA_STATS_LINK_XSTATS_SLAVE);
418880e73cc5SNikolay Aleksandrov 			if (!attr)
418980e73cc5SNikolay Aleksandrov 				goto nla_put_failure;
419080e73cc5SNikolay Aleksandrov 
419180e73cc5SNikolay Aleksandrov 			err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
419297a47facSNikolay Aleksandrov 			nla_nest_end(skb, attr);
419397a47facSNikolay Aleksandrov 			if (err)
419497a47facSNikolay Aleksandrov 				goto nla_put_failure;
419597a47facSNikolay Aleksandrov 			*idxattr = 0;
419697a47facSNikolay Aleksandrov 		}
419797a47facSNikolay Aleksandrov 	}
419897a47facSNikolay Aleksandrov 
419969ae6ad2SNogah Frankel 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
420069ae6ad2SNogah Frankel 			     *idxattr)) {
420169ae6ad2SNogah Frankel 		*idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
420269ae6ad2SNogah Frankel 		attr = nla_nest_start(skb, IFLA_STATS_LINK_OFFLOAD_XSTATS);
420369ae6ad2SNogah Frankel 		if (!attr)
420469ae6ad2SNogah Frankel 			goto nla_put_failure;
420569ae6ad2SNogah Frankel 
420669ae6ad2SNogah Frankel 		err = rtnl_get_offload_stats(skb, dev, prividx);
420769ae6ad2SNogah Frankel 		if (err == -ENODATA)
420869ae6ad2SNogah Frankel 			nla_nest_cancel(skb, attr);
420969ae6ad2SNogah Frankel 		else
421069ae6ad2SNogah Frankel 			nla_nest_end(skb, attr);
421169ae6ad2SNogah Frankel 
421269ae6ad2SNogah Frankel 		if (err && err != -ENODATA)
421369ae6ad2SNogah Frankel 			goto nla_put_failure;
421469ae6ad2SNogah Frankel 		*idxattr = 0;
421569ae6ad2SNogah Frankel 	}
421669ae6ad2SNogah Frankel 
4217aefb4d4aSRobert Shearman 	if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
4218aefb4d4aSRobert Shearman 		struct rtnl_af_ops *af_ops;
4219aefb4d4aSRobert Shearman 
4220aefb4d4aSRobert Shearman 		*idxattr = IFLA_STATS_AF_SPEC;
4221aefb4d4aSRobert Shearman 		attr = nla_nest_start(skb, IFLA_STATS_AF_SPEC);
4222aefb4d4aSRobert Shearman 		if (!attr)
4223aefb4d4aSRobert Shearman 			goto nla_put_failure;
4224aefb4d4aSRobert Shearman 
42255fa85a09SFlorian Westphal 		rcu_read_lock();
42265fa85a09SFlorian Westphal 		list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
4227aefb4d4aSRobert Shearman 			if (af_ops->fill_stats_af) {
4228aefb4d4aSRobert Shearman 				struct nlattr *af;
4229aefb4d4aSRobert Shearman 				int err;
4230aefb4d4aSRobert Shearman 
4231aefb4d4aSRobert Shearman 				af = nla_nest_start(skb, af_ops->family);
42325fa85a09SFlorian Westphal 				if (!af) {
42335fa85a09SFlorian Westphal 					rcu_read_unlock();
4234aefb4d4aSRobert Shearman 					goto nla_put_failure;
42355fa85a09SFlorian Westphal 				}
4236aefb4d4aSRobert Shearman 				err = af_ops->fill_stats_af(skb, dev);
4237aefb4d4aSRobert Shearman 
42385fa85a09SFlorian Westphal 				if (err == -ENODATA) {
4239aefb4d4aSRobert Shearman 					nla_nest_cancel(skb, af);
42405fa85a09SFlorian Westphal 				} else if (err < 0) {
42415fa85a09SFlorian Westphal 					rcu_read_unlock();
4242aefb4d4aSRobert Shearman 					goto nla_put_failure;
42435fa85a09SFlorian Westphal 				}
4244aefb4d4aSRobert Shearman 
4245aefb4d4aSRobert Shearman 				nla_nest_end(skb, af);
4246aefb4d4aSRobert Shearman 			}
4247aefb4d4aSRobert Shearman 		}
42485fa85a09SFlorian Westphal 		rcu_read_unlock();
4249aefb4d4aSRobert Shearman 
4250aefb4d4aSRobert Shearman 		nla_nest_end(skb, attr);
4251aefb4d4aSRobert Shearman 
4252aefb4d4aSRobert Shearman 		*idxattr = 0;
4253aefb4d4aSRobert Shearman 	}
4254aefb4d4aSRobert Shearman 
425510c9ead9SRoopa Prabhu 	nlmsg_end(skb, nlh);
425610c9ead9SRoopa Prabhu 
425710c9ead9SRoopa Prabhu 	return 0;
425810c9ead9SRoopa Prabhu 
425910c9ead9SRoopa Prabhu nla_put_failure:
4260e8872a25SNikolay Aleksandrov 	/* not a multi message or no progress mean a real error */
4261e8872a25SNikolay Aleksandrov 	if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
426210c9ead9SRoopa Prabhu 		nlmsg_cancel(skb, nlh);
4263e8872a25SNikolay Aleksandrov 	else
4264e8872a25SNikolay Aleksandrov 		nlmsg_end(skb, nlh);
426510c9ead9SRoopa Prabhu 
426610c9ead9SRoopa Prabhu 	return -EMSGSIZE;
426710c9ead9SRoopa Prabhu }
426810c9ead9SRoopa Prabhu 
426910c9ead9SRoopa Prabhu static size_t if_nlmsg_stats_size(const struct net_device *dev,
427010c9ead9SRoopa Prabhu 				  u32 filter_mask)
427110c9ead9SRoopa Prabhu {
427210c9ead9SRoopa Prabhu 	size_t size = 0;
427310c9ead9SRoopa Prabhu 
4274e8872a25SNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
427510c9ead9SRoopa Prabhu 		size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
427610c9ead9SRoopa Prabhu 
427797a47facSNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
427897a47facSNikolay Aleksandrov 		const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
427980e73cc5SNikolay Aleksandrov 		int attr = IFLA_STATS_LINK_XSTATS;
428097a47facSNikolay Aleksandrov 
428197a47facSNikolay Aleksandrov 		if (ops && ops->get_linkxstats_size) {
428280e73cc5SNikolay Aleksandrov 			size += nla_total_size(ops->get_linkxstats_size(dev,
428380e73cc5SNikolay Aleksandrov 									attr));
428497a47facSNikolay Aleksandrov 			/* for IFLA_STATS_LINK_XSTATS */
428597a47facSNikolay Aleksandrov 			size += nla_total_size(0);
428697a47facSNikolay Aleksandrov 		}
428797a47facSNikolay Aleksandrov 	}
428897a47facSNikolay Aleksandrov 
428980e73cc5SNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
429080e73cc5SNikolay Aleksandrov 		struct net_device *_dev = (struct net_device *)dev;
429180e73cc5SNikolay Aleksandrov 		const struct rtnl_link_ops *ops = NULL;
429280e73cc5SNikolay Aleksandrov 		const struct net_device *master;
429380e73cc5SNikolay Aleksandrov 
429480e73cc5SNikolay Aleksandrov 		/* netdev_master_upper_dev_get can't take const */
429580e73cc5SNikolay Aleksandrov 		master = netdev_master_upper_dev_get(_dev);
429680e73cc5SNikolay Aleksandrov 		if (master)
429780e73cc5SNikolay Aleksandrov 			ops = master->rtnl_link_ops;
429880e73cc5SNikolay Aleksandrov 		if (ops && ops->get_linkxstats_size) {
429980e73cc5SNikolay Aleksandrov 			int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
430080e73cc5SNikolay Aleksandrov 
430180e73cc5SNikolay Aleksandrov 			size += nla_total_size(ops->get_linkxstats_size(dev,
430280e73cc5SNikolay Aleksandrov 									attr));
430380e73cc5SNikolay Aleksandrov 			/* for IFLA_STATS_LINK_XSTATS_SLAVE */
430480e73cc5SNikolay Aleksandrov 			size += nla_total_size(0);
430580e73cc5SNikolay Aleksandrov 		}
430680e73cc5SNikolay Aleksandrov 	}
430780e73cc5SNikolay Aleksandrov 
430869ae6ad2SNogah Frankel 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0))
430969ae6ad2SNogah Frankel 		size += rtnl_get_offload_stats_size(dev);
431069ae6ad2SNogah Frankel 
4311aefb4d4aSRobert Shearman 	if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
4312aefb4d4aSRobert Shearman 		struct rtnl_af_ops *af_ops;
4313aefb4d4aSRobert Shearman 
4314aefb4d4aSRobert Shearman 		/* for IFLA_STATS_AF_SPEC */
4315aefb4d4aSRobert Shearman 		size += nla_total_size(0);
4316aefb4d4aSRobert Shearman 
43175fa85a09SFlorian Westphal 		rcu_read_lock();
43185fa85a09SFlorian Westphal 		list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
4319aefb4d4aSRobert Shearman 			if (af_ops->get_stats_af_size) {
4320aefb4d4aSRobert Shearman 				size += nla_total_size(
4321aefb4d4aSRobert Shearman 					af_ops->get_stats_af_size(dev));
4322aefb4d4aSRobert Shearman 
4323aefb4d4aSRobert Shearman 				/* for AF_* */
4324aefb4d4aSRobert Shearman 				size += nla_total_size(0);
4325aefb4d4aSRobert Shearman 			}
4326aefb4d4aSRobert Shearman 		}
43275fa85a09SFlorian Westphal 		rcu_read_unlock();
4328aefb4d4aSRobert Shearman 	}
4329aefb4d4aSRobert Shearman 
433010c9ead9SRoopa Prabhu 	return size;
433110c9ead9SRoopa Prabhu }
433210c9ead9SRoopa Prabhu 
4333c21ef3e3SDavid Ahern static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
4334c21ef3e3SDavid Ahern 			  struct netlink_ext_ack *extack)
433510c9ead9SRoopa Prabhu {
433610c9ead9SRoopa Prabhu 	struct net *net = sock_net(skb->sk);
433710c9ead9SRoopa Prabhu 	struct net_device *dev = NULL;
4338e8872a25SNikolay Aleksandrov 	int idxattr = 0, prividx = 0;
4339e8872a25SNikolay Aleksandrov 	struct if_stats_msg *ifsm;
434010c9ead9SRoopa Prabhu 	struct sk_buff *nskb;
434110c9ead9SRoopa Prabhu 	u32 filter_mask;
434210c9ead9SRoopa Prabhu 	int err;
434310c9ead9SRoopa Prabhu 
43444775cc1fSMathias Krause 	if (nlmsg_len(nlh) < sizeof(*ifsm))
43454775cc1fSMathias Krause 		return -EINVAL;
43464775cc1fSMathias Krause 
434710c9ead9SRoopa Prabhu 	ifsm = nlmsg_data(nlh);
434810c9ead9SRoopa Prabhu 	if (ifsm->ifindex > 0)
434910c9ead9SRoopa Prabhu 		dev = __dev_get_by_index(net, ifsm->ifindex);
435010c9ead9SRoopa Prabhu 	else
435110c9ead9SRoopa Prabhu 		return -EINVAL;
435210c9ead9SRoopa Prabhu 
435310c9ead9SRoopa Prabhu 	if (!dev)
435410c9ead9SRoopa Prabhu 		return -ENODEV;
435510c9ead9SRoopa Prabhu 
435610c9ead9SRoopa Prabhu 	filter_mask = ifsm->filter_mask;
435710c9ead9SRoopa Prabhu 	if (!filter_mask)
435810c9ead9SRoopa Prabhu 		return -EINVAL;
435910c9ead9SRoopa Prabhu 
436010c9ead9SRoopa Prabhu 	nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
436110c9ead9SRoopa Prabhu 	if (!nskb)
436210c9ead9SRoopa Prabhu 		return -ENOBUFS;
436310c9ead9SRoopa Prabhu 
436410c9ead9SRoopa Prabhu 	err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
436510c9ead9SRoopa Prabhu 				  NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
4366e8872a25SNikolay Aleksandrov 				  0, filter_mask, &idxattr, &prividx);
436710c9ead9SRoopa Prabhu 	if (err < 0) {
436810c9ead9SRoopa Prabhu 		/* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
436910c9ead9SRoopa Prabhu 		WARN_ON(err == -EMSGSIZE);
437010c9ead9SRoopa Prabhu 		kfree_skb(nskb);
437110c9ead9SRoopa Prabhu 	} else {
437210c9ead9SRoopa Prabhu 		err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
437310c9ead9SRoopa Prabhu 	}
437410c9ead9SRoopa Prabhu 
437510c9ead9SRoopa Prabhu 	return err;
437610c9ead9SRoopa Prabhu }
437710c9ead9SRoopa Prabhu 
437810c9ead9SRoopa Prabhu static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
437910c9ead9SRoopa Prabhu {
4380e8872a25SNikolay Aleksandrov 	int h, s_h, err, s_idx, s_idxattr, s_prividx;
438110c9ead9SRoopa Prabhu 	struct net *net = sock_net(skb->sk);
438210c9ead9SRoopa Prabhu 	unsigned int flags = NLM_F_MULTI;
4383e8872a25SNikolay Aleksandrov 	struct if_stats_msg *ifsm;
4384e8872a25SNikolay Aleksandrov 	struct hlist_head *head;
4385e8872a25SNikolay Aleksandrov 	struct net_device *dev;
438610c9ead9SRoopa Prabhu 	u32 filter_mask = 0;
4387e8872a25SNikolay Aleksandrov 	int idx = 0;
438810c9ead9SRoopa Prabhu 
438910c9ead9SRoopa Prabhu 	s_h = cb->args[0];
439010c9ead9SRoopa Prabhu 	s_idx = cb->args[1];
4391e8872a25SNikolay Aleksandrov 	s_idxattr = cb->args[2];
4392e8872a25SNikolay Aleksandrov 	s_prividx = cb->args[3];
439310c9ead9SRoopa Prabhu 
439410c9ead9SRoopa Prabhu 	cb->seq = net->dev_base_seq;
439510c9ead9SRoopa Prabhu 
43964775cc1fSMathias Krause 	if (nlmsg_len(cb->nlh) < sizeof(*ifsm))
43974775cc1fSMathias Krause 		return -EINVAL;
43984775cc1fSMathias Krause 
439910c9ead9SRoopa Prabhu 	ifsm = nlmsg_data(cb->nlh);
440010c9ead9SRoopa Prabhu 	filter_mask = ifsm->filter_mask;
440110c9ead9SRoopa Prabhu 	if (!filter_mask)
440210c9ead9SRoopa Prabhu 		return -EINVAL;
440310c9ead9SRoopa Prabhu 
440410c9ead9SRoopa Prabhu 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
440510c9ead9SRoopa Prabhu 		idx = 0;
440610c9ead9SRoopa Prabhu 		head = &net->dev_index_head[h];
440710c9ead9SRoopa Prabhu 		hlist_for_each_entry(dev, head, index_hlist) {
440810c9ead9SRoopa Prabhu 			if (idx < s_idx)
440910c9ead9SRoopa Prabhu 				goto cont;
441010c9ead9SRoopa Prabhu 			err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
441110c9ead9SRoopa Prabhu 						  NETLINK_CB(cb->skb).portid,
441210c9ead9SRoopa Prabhu 						  cb->nlh->nlmsg_seq, 0,
4413e8872a25SNikolay Aleksandrov 						  flags, filter_mask,
4414e8872a25SNikolay Aleksandrov 						  &s_idxattr, &s_prividx);
441510c9ead9SRoopa Prabhu 			/* If we ran out of room on the first message,
441610c9ead9SRoopa Prabhu 			 * we're in trouble
441710c9ead9SRoopa Prabhu 			 */
441810c9ead9SRoopa Prabhu 			WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
441910c9ead9SRoopa Prabhu 
442010c9ead9SRoopa Prabhu 			if (err < 0)
442110c9ead9SRoopa Prabhu 				goto out;
4422e8872a25SNikolay Aleksandrov 			s_prividx = 0;
4423e8872a25SNikolay Aleksandrov 			s_idxattr = 0;
442410c9ead9SRoopa Prabhu 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
442510c9ead9SRoopa Prabhu cont:
442610c9ead9SRoopa Prabhu 			idx++;
442710c9ead9SRoopa Prabhu 		}
442810c9ead9SRoopa Prabhu 	}
442910c9ead9SRoopa Prabhu out:
4430e8872a25SNikolay Aleksandrov 	cb->args[3] = s_prividx;
4431e8872a25SNikolay Aleksandrov 	cb->args[2] = s_idxattr;
443210c9ead9SRoopa Prabhu 	cb->args[1] = idx;
443310c9ead9SRoopa Prabhu 	cb->args[0] = h;
443410c9ead9SRoopa Prabhu 
443510c9ead9SRoopa Prabhu 	return skb->len;
443610c9ead9SRoopa Prabhu }
443710c9ead9SRoopa Prabhu 
44381da177e4SLinus Torvalds /* Process one rtnetlink message. */
44391da177e4SLinus Torvalds 
44402d4bc933SJohannes Berg static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
44412d4bc933SJohannes Berg 			     struct netlink_ext_ack *extack)
44421da177e4SLinus Torvalds {
44433b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
4444addf9b90SFlorian Westphal 	struct rtnl_link *link;
4445e4202511SFlorian Westphal 	struct module *owner;
44466853dd48SFlorian Westphal 	int err = -EOPNOTSUPP;
4447e2849863SThomas Graf 	rtnl_doit_func doit;
444862256f98SFlorian Westphal 	unsigned int flags;
4449617cfc75SAlexander Kuleshov 	int kind;
44501da177e4SLinus Torvalds 	int family;
44511da177e4SLinus Torvalds 	int type;
44521da177e4SLinus Torvalds 
44531da177e4SLinus Torvalds 	type = nlh->nlmsg_type;
44541da177e4SLinus Torvalds 	if (type > RTM_MAX)
4455038890feSThomas Graf 		return -EOPNOTSUPP;
44561da177e4SLinus Torvalds 
44571da177e4SLinus Torvalds 	type -= RTM_BASE;
44581da177e4SLinus Torvalds 
44591da177e4SLinus Torvalds 	/* All the messages must have at least 1 byte length */
4460573ce260SHong zhi guo 	if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
44611da177e4SLinus Torvalds 		return 0;
44621da177e4SLinus Torvalds 
4463573ce260SHong zhi guo 	family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
44641da177e4SLinus Torvalds 	kind = type&3;
44651da177e4SLinus Torvalds 
446690f62cf3SEric W. Biederman 	if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
44671d00a4ebSThomas Graf 		return -EPERM;
44681da177e4SLinus Torvalds 
44696853dd48SFlorian Westphal 	rcu_read_lock();
4470b8f3ab42SDavid S. Miller 	if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
447197c53cacSDenis V. Lunev 		struct sock *rtnl;
4472e2849863SThomas Graf 		rtnl_dumpit_func dumpit;
4473c7ac8679SGreg Rose 		u16 min_dump_alloc = 0;
44741da177e4SLinus Torvalds 
4475addf9b90SFlorian Westphal 		link = rtnl_get_link(family, type);
4476addf9b90SFlorian Westphal 		if (!link || !link->dumpit) {
44776853dd48SFlorian Westphal 			family = PF_UNSPEC;
4478addf9b90SFlorian Westphal 			link = rtnl_get_link(family, type);
4479addf9b90SFlorian Westphal 			if (!link || !link->dumpit)
44806853dd48SFlorian Westphal 				goto err_unlock;
44816853dd48SFlorian Westphal 		}
4482e4202511SFlorian Westphal 		owner = link->owner;
4483addf9b90SFlorian Westphal 		dumpit = link->dumpit;
44846853dd48SFlorian Westphal 
44855c2bb9b6SFlorian Westphal 		if (type == RTM_GETLINK - RTM_BASE)
4486e1fa6d21SFlorian Westphal 			min_dump_alloc = rtnl_calcit(skb, nlh);
44871da177e4SLinus Torvalds 
4488e4202511SFlorian Westphal 		err = 0;
4489e4202511SFlorian Westphal 		/* need to do this before rcu_read_unlock() */
4490e4202511SFlorian Westphal 		if (!try_module_get(owner))
4491e4202511SFlorian Westphal 			err = -EPROTONOSUPPORT;
4492e4202511SFlorian Westphal 
44936853dd48SFlorian Westphal 		rcu_read_unlock();
44946853dd48SFlorian Westphal 
449597c53cacSDenis V. Lunev 		rtnl = net->rtnl;
4496e4202511SFlorian Westphal 		if (err == 0) {
449780d326faSPablo Neira Ayuso 			struct netlink_dump_control c = {
449880d326faSPablo Neira Ayuso 				.dump		= dumpit,
449980d326faSPablo Neira Ayuso 				.min_dump_alloc	= min_dump_alloc,
4500e4202511SFlorian Westphal 				.module		= owner,
450180d326faSPablo Neira Ayuso 			};
450280d326faSPablo Neira Ayuso 			err = netlink_dump_start(rtnl, skb, nlh, &c);
4503e4202511SFlorian Westphal 			/* netlink_dump_start() will keep a reference on
4504e4202511SFlorian Westphal 			 * module if dump is still in progress.
4505e4202511SFlorian Westphal 			 */
4506e4202511SFlorian Westphal 			module_put(owner);
450780d326faSPablo Neira Ayuso 		}
45082907c35fSEric Dumazet 		return err;
45091da177e4SLinus Torvalds 	}
45101da177e4SLinus Torvalds 
4511addf9b90SFlorian Westphal 	link = rtnl_get_link(family, type);
4512addf9b90SFlorian Westphal 	if (!link || !link->doit) {
45138caa38b5SFlorian Westphal 		family = PF_UNSPEC;
4514addf9b90SFlorian Westphal 		link = rtnl_get_link(PF_UNSPEC, type);
4515addf9b90SFlorian Westphal 		if (!link || !link->doit)
4516addf9b90SFlorian Westphal 			goto out_unlock;
45178caa38b5SFlorian Westphal 	}
45188caa38b5SFlorian Westphal 
4519e4202511SFlorian Westphal 	owner = link->owner;
4520e4202511SFlorian Westphal 	if (!try_module_get(owner)) {
4521e4202511SFlorian Westphal 		err = -EPROTONOSUPPORT;
4522e4202511SFlorian Westphal 		goto out_unlock;
4523e4202511SFlorian Westphal 	}
4524e4202511SFlorian Westphal 
4525addf9b90SFlorian Westphal 	flags = link->flags;
452662256f98SFlorian Westphal 	if (flags & RTNL_FLAG_DOIT_UNLOCKED) {
4527addf9b90SFlorian Westphal 		doit = link->doit;
452862256f98SFlorian Westphal 		rcu_read_unlock();
452962256f98SFlorian Westphal 		if (doit)
453062256f98SFlorian Westphal 			err = doit(skb, nlh, extack);
4531e4202511SFlorian Westphal 		module_put(owner);
453262256f98SFlorian Westphal 		return err;
453362256f98SFlorian Westphal 	}
45346853dd48SFlorian Westphal 	rcu_read_unlock();
45356853dd48SFlorian Westphal 
45360cc09020SFlorian Westphal 	rtnl_lock();
4537addf9b90SFlorian Westphal 	link = rtnl_get_link(family, type);
4538addf9b90SFlorian Westphal 	if (link && link->doit)
4539addf9b90SFlorian Westphal 		err = link->doit(skb, nlh, extack);
45400cc09020SFlorian Westphal 	rtnl_unlock();
4541addf9b90SFlorian Westphal 
4542e4202511SFlorian Westphal 	module_put(owner);
4543e4202511SFlorian Westphal 
4544addf9b90SFlorian Westphal 	return err;
4545addf9b90SFlorian Westphal 
4546addf9b90SFlorian Westphal out_unlock:
4547addf9b90SFlorian Westphal 	rcu_read_unlock();
45480cc09020SFlorian Westphal 	return err;
45490cc09020SFlorian Westphal 
45500cc09020SFlorian Westphal err_unlock:
45516853dd48SFlorian Westphal 	rcu_read_unlock();
45520cc09020SFlorian Westphal 	return -EOPNOTSUPP;
45531da177e4SLinus Torvalds }
45541da177e4SLinus Torvalds 
4555cd40b7d3SDenis V. Lunev static void rtnetlink_rcv(struct sk_buff *skb)
45561da177e4SLinus Torvalds {
4557cd40b7d3SDenis V. Lunev 	netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
45581da177e4SLinus Torvalds }
45591da177e4SLinus Torvalds 
45605f729eaaSJulien Gomes static int rtnetlink_bind(struct net *net, int group)
45615f729eaaSJulien Gomes {
45625f729eaaSJulien Gomes 	switch (group) {
45635f729eaaSJulien Gomes 	case RTNLGRP_IPV4_MROUTE_R:
45645f729eaaSJulien Gomes 	case RTNLGRP_IPV6_MROUTE_R:
45655f729eaaSJulien Gomes 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
45665f729eaaSJulien Gomes 			return -EPERM;
45675f729eaaSJulien Gomes 		break;
45685f729eaaSJulien Gomes 	}
45695f729eaaSJulien Gomes 	return 0;
45705f729eaaSJulien Gomes }
45715f729eaaSJulien Gomes 
45721da177e4SLinus Torvalds static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
45731da177e4SLinus Torvalds {
4574351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
4575e9dc8653SEric W. Biederman 
45761da177e4SLinus Torvalds 	switch (event) {
45775138e86fSVlad Yasevich 	case NETDEV_REBOOT:
45788a212589SXin Long 	case NETDEV_CHANGEMTU:
45793753654eSDavid Ahern 	case NETDEV_CHANGEADDR:
45805138e86fSVlad Yasevich 	case NETDEV_CHANGENAME:
45815138e86fSVlad Yasevich 	case NETDEV_FEAT_CHANGE:
45825138e86fSVlad Yasevich 	case NETDEV_BONDING_FAILOVER:
4583e6e66594SXin Long 	case NETDEV_POST_TYPE_CHANGE:
45845138e86fSVlad Yasevich 	case NETDEV_NOTIFY_PEERS:
4585dc709f37SXin Long 	case NETDEV_CHANGEUPPER:
45865138e86fSVlad Yasevich 	case NETDEV_RESEND_IGMP:
45875138e86fSVlad Yasevich 	case NETDEV_CHANGEINFODATA:
4588eeda3fb9SXin Long 	case NETDEV_CHANGELOWERSTATE:
4589ebdcf045SXin Long 	case NETDEV_CHANGE_TX_QUEUE_LEN:
45903d3ea5afSVlad Yasevich 		rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
45916621dd29SNicolas Dichtel 				   GFP_KERNEL, NULL);
45921da177e4SLinus Torvalds 		break;
45931da177e4SLinus Torvalds 	default:
45941da177e4SLinus Torvalds 		break;
45951da177e4SLinus Torvalds 	}
45961da177e4SLinus Torvalds 	return NOTIFY_DONE;
45971da177e4SLinus Torvalds }
45981da177e4SLinus Torvalds 
45991da177e4SLinus Torvalds static struct notifier_block rtnetlink_dev_notifier = {
46001da177e4SLinus Torvalds 	.notifier_call	= rtnetlink_event,
46011da177e4SLinus Torvalds };
46021da177e4SLinus Torvalds 
460397c53cacSDenis V. Lunev 
46042c8c1e72SAlexey Dobriyan static int __net_init rtnetlink_net_init(struct net *net)
460597c53cacSDenis V. Lunev {
460697c53cacSDenis V. Lunev 	struct sock *sk;
4607a31f2d17SPablo Neira Ayuso 	struct netlink_kernel_cfg cfg = {
4608a31f2d17SPablo Neira Ayuso 		.groups		= RTNLGRP_MAX,
4609a31f2d17SPablo Neira Ayuso 		.input		= rtnetlink_rcv,
4610a31f2d17SPablo Neira Ayuso 		.cb_mutex	= &rtnl_mutex,
46119785e10aSPablo Neira Ayuso 		.flags		= NL_CFG_F_NONROOT_RECV,
46125f729eaaSJulien Gomes 		.bind		= rtnetlink_bind,
4613a31f2d17SPablo Neira Ayuso 	};
4614a31f2d17SPablo Neira Ayuso 
46159f00d977SPablo Neira Ayuso 	sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
461697c53cacSDenis V. Lunev 	if (!sk)
461797c53cacSDenis V. Lunev 		return -ENOMEM;
461897c53cacSDenis V. Lunev 	net->rtnl = sk;
461997c53cacSDenis V. Lunev 	return 0;
462097c53cacSDenis V. Lunev }
462197c53cacSDenis V. Lunev 
46222c8c1e72SAlexey Dobriyan static void __net_exit rtnetlink_net_exit(struct net *net)
462397c53cacSDenis V. Lunev {
4624b7c6ba6eSDenis V. Lunev 	netlink_kernel_release(net->rtnl);
462597c53cacSDenis V. Lunev 	net->rtnl = NULL;
462697c53cacSDenis V. Lunev }
462797c53cacSDenis V. Lunev 
462897c53cacSDenis V. Lunev static struct pernet_operations rtnetlink_net_ops = {
462997c53cacSDenis V. Lunev 	.init = rtnetlink_net_init,
463097c53cacSDenis V. Lunev 	.exit = rtnetlink_net_exit,
463197c53cacSDenis V. Lunev };
463297c53cacSDenis V. Lunev 
46331da177e4SLinus Torvalds void __init rtnetlink_init(void)
46341da177e4SLinus Torvalds {
463597c53cacSDenis V. Lunev 	if (register_pernet_subsys(&rtnetlink_net_ops))
46361da177e4SLinus Torvalds 		panic("rtnetlink_init: cannot initialize rtnetlink\n");
463797c53cacSDenis V. Lunev 
46381da177e4SLinus Torvalds 	register_netdevice_notifier(&rtnetlink_dev_notifier);
4639340d17fcSThomas Graf 
4640c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
4641b97bac64SFlorian Westphal 		      rtnl_dump_ifinfo, 0);
4642b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0);
4643b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0);
4644b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0);
4645687ad8ccSThomas Graf 
4646b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0);
4647b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0);
4648b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0);
464977162022SJohn Fastabend 
4650b97bac64SFlorian Westphal 	rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
4651b97bac64SFlorian Westphal 	rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0);
4652b97bac64SFlorian Westphal 	rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, 0);
4653e5a55a89SJohn Fastabend 
4654b97bac64SFlorian Westphal 	rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0);
4655b97bac64SFlorian Westphal 	rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0);
4656b97bac64SFlorian Westphal 	rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0);
465710c9ead9SRoopa Prabhu 
465810c9ead9SRoopa Prabhu 	rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,
4659b97bac64SFlorian Westphal 		      0);
46601da177e4SLinus Torvalds }
4661