xref: /openbmc/linux/net/core/rtnetlink.c (revision d1fdd913)
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 
191da177e4SLinus Torvalds #include <linux/errno.h>
201da177e4SLinus Torvalds #include <linux/module.h>
211da177e4SLinus Torvalds #include <linux/types.h>
221da177e4SLinus Torvalds #include <linux/socket.h>
231da177e4SLinus Torvalds #include <linux/kernel.h>
241da177e4SLinus Torvalds #include <linux/timer.h>
251da177e4SLinus Torvalds #include <linux/string.h>
261da177e4SLinus Torvalds #include <linux/sockios.h>
271da177e4SLinus Torvalds #include <linux/net.h>
281da177e4SLinus Torvalds #include <linux/fcntl.h>
291da177e4SLinus Torvalds #include <linux/mm.h>
301da177e4SLinus Torvalds #include <linux/slab.h>
311da177e4SLinus Torvalds #include <linux/interrupt.h>
321da177e4SLinus Torvalds #include <linux/capability.h>
331da177e4SLinus Torvalds #include <linux/skbuff.h>
341da177e4SLinus Torvalds #include <linux/init.h>
351da177e4SLinus Torvalds #include <linux/security.h>
366756ae4bSStephen Hemminger #include <linux/mutex.h>
371823730fSThomas Graf #include <linux/if_addr.h>
3877162022SJohn Fastabend #include <linux/if_bridge.h>
39f6f6424bSJiri Pirko #include <linux/if_vlan.h>
40ebc08a6fSWilliams, Mitch A #include <linux/pci.h>
4177162022SJohn Fastabend #include <linux/etherdevice.h>
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds #include <asm/uaccess.h>
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds #include <linux/inet.h>
461da177e4SLinus Torvalds #include <linux/netdevice.h>
4782f28412SJiri Pirko #include <net/switchdev.h>
481da177e4SLinus Torvalds #include <net/ip.h>
491da177e4SLinus Torvalds #include <net/protocol.h>
501da177e4SLinus Torvalds #include <net/arp.h>
511da177e4SLinus Torvalds #include <net/route.h>
521da177e4SLinus Torvalds #include <net/udp.h>
53ea697639SDaniel Borkmann #include <net/tcp.h>
541da177e4SLinus Torvalds #include <net/sock.h>
551da177e4SLinus Torvalds #include <net/pkt_sched.h>
5614c0b97dSThomas Graf #include <net/fib_rules.h>
57e2849863SThomas Graf #include <net/rtnetlink.h>
5830ffee84SJohannes Berg #include <net/net_namespace.h>
591da177e4SLinus Torvalds 
60e0d087afSEric Dumazet struct rtnl_link {
61e2849863SThomas Graf 	rtnl_doit_func		doit;
62e2849863SThomas Graf 	rtnl_dumpit_func	dumpit;
63c7ac8679SGreg Rose 	rtnl_calcit_func 	calcit;
64e2849863SThomas Graf };
65e2849863SThomas Graf 
666756ae4bSStephen Hemminger static DEFINE_MUTEX(rtnl_mutex);
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds void rtnl_lock(void)
691da177e4SLinus Torvalds {
706756ae4bSStephen Hemminger 	mutex_lock(&rtnl_mutex);
711da177e4SLinus Torvalds }
72e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_lock);
731da177e4SLinus Torvalds 
741b5c5493SEric Dumazet static struct sk_buff *defer_kfree_skb_list;
751b5c5493SEric Dumazet void rtnl_kfree_skbs(struct sk_buff *head, struct sk_buff *tail)
761b5c5493SEric Dumazet {
771b5c5493SEric Dumazet 	if (head && tail) {
781b5c5493SEric Dumazet 		tail->next = defer_kfree_skb_list;
791b5c5493SEric Dumazet 		defer_kfree_skb_list = head;
801b5c5493SEric Dumazet 	}
811b5c5493SEric Dumazet }
821b5c5493SEric Dumazet EXPORT_SYMBOL(rtnl_kfree_skbs);
831b5c5493SEric Dumazet 
846756ae4bSStephen Hemminger void __rtnl_unlock(void)
851da177e4SLinus Torvalds {
861b5c5493SEric Dumazet 	struct sk_buff *head = defer_kfree_skb_list;
871b5c5493SEric Dumazet 
881b5c5493SEric Dumazet 	defer_kfree_skb_list = NULL;
891b5c5493SEric Dumazet 
906756ae4bSStephen Hemminger 	mutex_unlock(&rtnl_mutex);
911b5c5493SEric Dumazet 
921b5c5493SEric Dumazet 	while (head) {
931b5c5493SEric Dumazet 		struct sk_buff *next = head->next;
941b5c5493SEric Dumazet 
951b5c5493SEric Dumazet 		kfree_skb(head);
961b5c5493SEric Dumazet 		cond_resched();
971b5c5493SEric Dumazet 		head = next;
981b5c5493SEric Dumazet 	}
991da177e4SLinus Torvalds }
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds void rtnl_unlock(void)
1021da177e4SLinus Torvalds {
10358ec3b4dSHerbert Xu 	/* This fellow will unlock it for us. */
1041da177e4SLinus Torvalds 	netdev_run_todo();
1051da177e4SLinus Torvalds }
106e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unlock);
1071da177e4SLinus Torvalds 
1086756ae4bSStephen Hemminger int rtnl_trylock(void)
1096756ae4bSStephen Hemminger {
1106756ae4bSStephen Hemminger 	return mutex_trylock(&rtnl_mutex);
1116756ae4bSStephen Hemminger }
112e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_trylock);
1136756ae4bSStephen Hemminger 
114c9c1014bSPatrick McHardy int rtnl_is_locked(void)
115c9c1014bSPatrick McHardy {
116c9c1014bSPatrick McHardy 	return mutex_is_locked(&rtnl_mutex);
117c9c1014bSPatrick McHardy }
118e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_is_locked);
119c9c1014bSPatrick McHardy 
120a898def2SPaul E. McKenney #ifdef CONFIG_PROVE_LOCKING
1210cbf3343SYaowei Bai bool lockdep_rtnl_is_held(void)
122a898def2SPaul E. McKenney {
123a898def2SPaul E. McKenney 	return lockdep_is_held(&rtnl_mutex);
124a898def2SPaul E. McKenney }
125a898def2SPaul E. McKenney EXPORT_SYMBOL(lockdep_rtnl_is_held);
126a898def2SPaul E. McKenney #endif /* #ifdef CONFIG_PROVE_LOCKING */
127a898def2SPaul E. McKenney 
12825239ceeSPatrick McHardy static struct rtnl_link *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
129e2849863SThomas Graf 
130e2849863SThomas Graf static inline int rtm_msgindex(int msgtype)
131e2849863SThomas Graf {
132e2849863SThomas Graf 	int msgindex = msgtype - RTM_BASE;
133e2849863SThomas Graf 
134e2849863SThomas Graf 	/*
135e2849863SThomas Graf 	 * msgindex < 0 implies someone tried to register a netlink
136e2849863SThomas Graf 	 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
137e2849863SThomas Graf 	 * the message type has not been added to linux/rtnetlink.h
138e2849863SThomas Graf 	 */
139e2849863SThomas Graf 	BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
140e2849863SThomas Graf 
141e2849863SThomas Graf 	return msgindex;
142e2849863SThomas Graf }
143e2849863SThomas Graf 
144e2849863SThomas Graf static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex)
145e2849863SThomas Graf {
146e2849863SThomas Graf 	struct rtnl_link *tab;
147e2849863SThomas Graf 
14825239ceeSPatrick McHardy 	if (protocol <= RTNL_FAMILY_MAX)
149e2849863SThomas Graf 		tab = rtnl_msg_handlers[protocol];
1500f87b1ddSPatrick McHardy 	else
1510f87b1ddSPatrick McHardy 		tab = NULL;
1520f87b1ddSPatrick McHardy 
15351057f2fSThomas Graf 	if (tab == NULL || tab[msgindex].doit == NULL)
154e2849863SThomas Graf 		tab = rtnl_msg_handlers[PF_UNSPEC];
155e2849863SThomas Graf 
156c80bbeaeSHans Zhang 	return tab[msgindex].doit;
157e2849863SThomas Graf }
158e2849863SThomas Graf 
159e2849863SThomas Graf static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex)
160e2849863SThomas Graf {
161e2849863SThomas Graf 	struct rtnl_link *tab;
162e2849863SThomas Graf 
16325239ceeSPatrick McHardy 	if (protocol <= RTNL_FAMILY_MAX)
164e2849863SThomas Graf 		tab = rtnl_msg_handlers[protocol];
1650f87b1ddSPatrick McHardy 	else
1660f87b1ddSPatrick McHardy 		tab = NULL;
1670f87b1ddSPatrick McHardy 
16851057f2fSThomas Graf 	if (tab == NULL || tab[msgindex].dumpit == NULL)
169e2849863SThomas Graf 		tab = rtnl_msg_handlers[PF_UNSPEC];
170e2849863SThomas Graf 
171c80bbeaeSHans Zhang 	return tab[msgindex].dumpit;
172e2849863SThomas Graf }
173e2849863SThomas Graf 
174c7ac8679SGreg Rose static rtnl_calcit_func rtnl_get_calcit(int protocol, int msgindex)
175c7ac8679SGreg Rose {
176c7ac8679SGreg Rose 	struct rtnl_link *tab;
177c7ac8679SGreg Rose 
178c7ac8679SGreg Rose 	if (protocol <= RTNL_FAMILY_MAX)
179c7ac8679SGreg Rose 		tab = rtnl_msg_handlers[protocol];
180c7ac8679SGreg Rose 	else
181c7ac8679SGreg Rose 		tab = NULL;
182c7ac8679SGreg Rose 
183c7ac8679SGreg Rose 	if (tab == NULL || tab[msgindex].calcit == NULL)
184c7ac8679SGreg Rose 		tab = rtnl_msg_handlers[PF_UNSPEC];
185c7ac8679SGreg Rose 
186c80bbeaeSHans Zhang 	return tab[msgindex].calcit;
187c7ac8679SGreg Rose }
188c7ac8679SGreg Rose 
189e2849863SThomas Graf /**
190e2849863SThomas Graf  * __rtnl_register - Register a rtnetlink message type
191e2849863SThomas Graf  * @protocol: Protocol family or PF_UNSPEC
192e2849863SThomas Graf  * @msgtype: rtnetlink message type
193e2849863SThomas Graf  * @doit: Function pointer called for each request message
194e2849863SThomas Graf  * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
195c7ac8679SGreg Rose  * @calcit: Function pointer to calc size of dump message
196e2849863SThomas Graf  *
197e2849863SThomas Graf  * Registers the specified function pointers (at least one of them has
198e2849863SThomas Graf  * to be non-NULL) to be called whenever a request message for the
199e2849863SThomas Graf  * specified protocol family and message type is received.
200e2849863SThomas Graf  *
201e2849863SThomas Graf  * The special protocol family PF_UNSPEC may be used to define fallback
202e2849863SThomas Graf  * function pointers for the case when no entry for the specific protocol
203e2849863SThomas Graf  * family exists.
204e2849863SThomas Graf  *
205e2849863SThomas Graf  * Returns 0 on success or a negative error code.
206e2849863SThomas Graf  */
207e2849863SThomas Graf int __rtnl_register(int protocol, int msgtype,
208c7ac8679SGreg Rose 		    rtnl_doit_func doit, rtnl_dumpit_func dumpit,
209c7ac8679SGreg Rose 		    rtnl_calcit_func calcit)
210e2849863SThomas Graf {
211e2849863SThomas Graf 	struct rtnl_link *tab;
212e2849863SThomas Graf 	int msgindex;
213e2849863SThomas Graf 
21425239ceeSPatrick McHardy 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
215e2849863SThomas Graf 	msgindex = rtm_msgindex(msgtype);
216e2849863SThomas Graf 
217e2849863SThomas Graf 	tab = rtnl_msg_handlers[protocol];
218e2849863SThomas Graf 	if (tab == NULL) {
219e2849863SThomas Graf 		tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL);
220e2849863SThomas Graf 		if (tab == NULL)
221e2849863SThomas Graf 			return -ENOBUFS;
222e2849863SThomas Graf 
223e2849863SThomas Graf 		rtnl_msg_handlers[protocol] = tab;
224e2849863SThomas Graf 	}
225e2849863SThomas Graf 
226e2849863SThomas Graf 	if (doit)
227e2849863SThomas Graf 		tab[msgindex].doit = doit;
228e2849863SThomas Graf 
229e2849863SThomas Graf 	if (dumpit)
230e2849863SThomas Graf 		tab[msgindex].dumpit = dumpit;
231e2849863SThomas Graf 
232c7ac8679SGreg Rose 	if (calcit)
233c7ac8679SGreg Rose 		tab[msgindex].calcit = calcit;
234c7ac8679SGreg Rose 
235e2849863SThomas Graf 	return 0;
236e2849863SThomas Graf }
237e2849863SThomas Graf EXPORT_SYMBOL_GPL(__rtnl_register);
238e2849863SThomas Graf 
239e2849863SThomas Graf /**
240e2849863SThomas Graf  * rtnl_register - Register a rtnetlink message type
241e2849863SThomas Graf  *
242e2849863SThomas Graf  * Identical to __rtnl_register() but panics on failure. This is useful
243e2849863SThomas Graf  * as failure of this function is very unlikely, it can only happen due
244e2849863SThomas Graf  * to lack of memory when allocating the chain to store all message
245e2849863SThomas Graf  * handlers for a protocol. Meant for use in init functions where lack
24625985edcSLucas De Marchi  * of memory implies no sense in continuing.
247e2849863SThomas Graf  */
248e2849863SThomas Graf void rtnl_register(int protocol, int msgtype,
249c7ac8679SGreg Rose 		   rtnl_doit_func doit, rtnl_dumpit_func dumpit,
250c7ac8679SGreg Rose 		   rtnl_calcit_func calcit)
251e2849863SThomas Graf {
252c7ac8679SGreg Rose 	if (__rtnl_register(protocol, msgtype, doit, dumpit, calcit) < 0)
253e2849863SThomas Graf 		panic("Unable to register rtnetlink message handler, "
254e2849863SThomas Graf 		      "protocol = %d, message type = %d\n",
255e2849863SThomas Graf 		      protocol, msgtype);
256e2849863SThomas Graf }
257e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_register);
258e2849863SThomas Graf 
259e2849863SThomas Graf /**
260e2849863SThomas Graf  * rtnl_unregister - Unregister a rtnetlink message type
261e2849863SThomas Graf  * @protocol: Protocol family or PF_UNSPEC
262e2849863SThomas Graf  * @msgtype: rtnetlink message type
263e2849863SThomas Graf  *
264e2849863SThomas Graf  * Returns 0 on success or a negative error code.
265e2849863SThomas Graf  */
266e2849863SThomas Graf int rtnl_unregister(int protocol, int msgtype)
267e2849863SThomas Graf {
268e2849863SThomas Graf 	int msgindex;
269e2849863SThomas Graf 
27025239ceeSPatrick McHardy 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
271e2849863SThomas Graf 	msgindex = rtm_msgindex(msgtype);
272e2849863SThomas Graf 
273e2849863SThomas Graf 	if (rtnl_msg_handlers[protocol] == NULL)
274e2849863SThomas Graf 		return -ENOENT;
275e2849863SThomas Graf 
276e2849863SThomas Graf 	rtnl_msg_handlers[protocol][msgindex].doit = NULL;
277e2849863SThomas Graf 	rtnl_msg_handlers[protocol][msgindex].dumpit = NULL;
278e2849863SThomas Graf 
279e2849863SThomas Graf 	return 0;
280e2849863SThomas Graf }
281e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister);
282e2849863SThomas Graf 
283e2849863SThomas Graf /**
284e2849863SThomas Graf  * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
285e2849863SThomas Graf  * @protocol : Protocol family or PF_UNSPEC
286e2849863SThomas Graf  *
287e2849863SThomas Graf  * Identical to calling rtnl_unregster() for all registered message types
288e2849863SThomas Graf  * of a certain protocol family.
289e2849863SThomas Graf  */
290e2849863SThomas Graf void rtnl_unregister_all(int protocol)
291e2849863SThomas Graf {
29225239ceeSPatrick McHardy 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
293e2849863SThomas Graf 
294e2849863SThomas Graf 	kfree(rtnl_msg_handlers[protocol]);
295e2849863SThomas Graf 	rtnl_msg_handlers[protocol] = NULL;
296e2849863SThomas Graf }
297e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister_all);
2981da177e4SLinus Torvalds 
29938f7b870SPatrick McHardy static LIST_HEAD(link_ops);
30038f7b870SPatrick McHardy 
301c63044f0SEric Dumazet static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
302c63044f0SEric Dumazet {
303c63044f0SEric Dumazet 	const struct rtnl_link_ops *ops;
304c63044f0SEric Dumazet 
305c63044f0SEric Dumazet 	list_for_each_entry(ops, &link_ops, list) {
306c63044f0SEric Dumazet 		if (!strcmp(ops->kind, kind))
307c63044f0SEric Dumazet 			return ops;
308c63044f0SEric Dumazet 	}
309c63044f0SEric Dumazet 	return NULL;
310c63044f0SEric Dumazet }
311c63044f0SEric Dumazet 
31238f7b870SPatrick McHardy /**
31338f7b870SPatrick McHardy  * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
31438f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to register
31538f7b870SPatrick McHardy  *
31638f7b870SPatrick McHardy  * The caller must hold the rtnl_mutex. This function should be used
31738f7b870SPatrick McHardy  * by drivers that create devices during module initialization. It
31838f7b870SPatrick McHardy  * must be called before registering the devices.
31938f7b870SPatrick McHardy  *
32038f7b870SPatrick McHardy  * Returns 0 on success or a negative error code.
32138f7b870SPatrick McHardy  */
32238f7b870SPatrick McHardy int __rtnl_link_register(struct rtnl_link_ops *ops)
32338f7b870SPatrick McHardy {
324c63044f0SEric Dumazet 	if (rtnl_link_ops_get(ops->kind))
325c63044f0SEric Dumazet 		return -EEXIST;
326c63044f0SEric Dumazet 
327b0ab2fabSJiri Pirko 	/* The check for setup is here because if ops
328b0ab2fabSJiri Pirko 	 * does not have that filled up, it is not possible
329b0ab2fabSJiri Pirko 	 * to use the ops for creating device. So do not
330b0ab2fabSJiri Pirko 	 * fill up dellink as well. That disables rtnl_dellink.
331b0ab2fabSJiri Pirko 	 */
332b0ab2fabSJiri Pirko 	if (ops->setup && !ops->dellink)
33323289a37SEric Dumazet 		ops->dellink = unregister_netdevice_queue;
3342d85cba2SPatrick McHardy 
33538f7b870SPatrick McHardy 	list_add_tail(&ops->list, &link_ops);
33638f7b870SPatrick McHardy 	return 0;
33738f7b870SPatrick McHardy }
33838f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_register);
33938f7b870SPatrick McHardy 
34038f7b870SPatrick McHardy /**
34138f7b870SPatrick McHardy  * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
34238f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to register
34338f7b870SPatrick McHardy  *
34438f7b870SPatrick McHardy  * Returns 0 on success or a negative error code.
34538f7b870SPatrick McHardy  */
34638f7b870SPatrick McHardy int rtnl_link_register(struct rtnl_link_ops *ops)
34738f7b870SPatrick McHardy {
34838f7b870SPatrick McHardy 	int err;
34938f7b870SPatrick McHardy 
35038f7b870SPatrick McHardy 	rtnl_lock();
35138f7b870SPatrick McHardy 	err = __rtnl_link_register(ops);
35238f7b870SPatrick McHardy 	rtnl_unlock();
35338f7b870SPatrick McHardy 	return err;
35438f7b870SPatrick McHardy }
35538f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_register);
35638f7b870SPatrick McHardy 
357669f87baSPavel Emelyanov static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
358669f87baSPavel Emelyanov {
359669f87baSPavel Emelyanov 	struct net_device *dev;
36023289a37SEric Dumazet 	LIST_HEAD(list_kill);
36123289a37SEric Dumazet 
362669f87baSPavel Emelyanov 	for_each_netdev(net, dev) {
36323289a37SEric Dumazet 		if (dev->rtnl_link_ops == ops)
36423289a37SEric Dumazet 			ops->dellink(dev, &list_kill);
365669f87baSPavel Emelyanov 	}
36623289a37SEric Dumazet 	unregister_netdevice_many(&list_kill);
367669f87baSPavel Emelyanov }
368669f87baSPavel Emelyanov 
36938f7b870SPatrick McHardy /**
37038f7b870SPatrick McHardy  * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
37138f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to unregister
37238f7b870SPatrick McHardy  *
3732d85cba2SPatrick McHardy  * The caller must hold the rtnl_mutex.
37438f7b870SPatrick McHardy  */
37538f7b870SPatrick McHardy void __rtnl_link_unregister(struct rtnl_link_ops *ops)
37638f7b870SPatrick McHardy {
377881d966bSEric W. Biederman 	struct net *net;
3782d85cba2SPatrick McHardy 
379881d966bSEric W. Biederman 	for_each_net(net) {
380669f87baSPavel Emelyanov 		__rtnl_kill_links(net, ops);
381881d966bSEric W. Biederman 	}
38238f7b870SPatrick McHardy 	list_del(&ops->list);
38338f7b870SPatrick McHardy }
38438f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
38538f7b870SPatrick McHardy 
386200b916fSCong Wang /* Return with the rtnl_lock held when there are no network
387200b916fSCong Wang  * devices unregistering in any network namespace.
388200b916fSCong Wang  */
389200b916fSCong Wang static void rtnl_lock_unregistering_all(void)
390200b916fSCong Wang {
391200b916fSCong Wang 	struct net *net;
392200b916fSCong Wang 	bool unregistering;
393ff960a73SPeter Zijlstra 	DEFINE_WAIT_FUNC(wait, woken_wake_function);
394200b916fSCong Wang 
395ff960a73SPeter Zijlstra 	add_wait_queue(&netdev_unregistering_wq, &wait);
396200b916fSCong Wang 	for (;;) {
397200b916fSCong Wang 		unregistering = false;
398200b916fSCong Wang 		rtnl_lock();
399200b916fSCong Wang 		for_each_net(net) {
400200b916fSCong Wang 			if (net->dev_unreg_count > 0) {
401200b916fSCong Wang 				unregistering = true;
402200b916fSCong Wang 				break;
403200b916fSCong Wang 			}
404200b916fSCong Wang 		}
405200b916fSCong Wang 		if (!unregistering)
406200b916fSCong Wang 			break;
407200b916fSCong Wang 		__rtnl_unlock();
408ff960a73SPeter Zijlstra 
409ff960a73SPeter Zijlstra 		wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
410200b916fSCong Wang 	}
411ff960a73SPeter Zijlstra 	remove_wait_queue(&netdev_unregistering_wq, &wait);
412200b916fSCong Wang }
413200b916fSCong Wang 
41438f7b870SPatrick McHardy /**
41538f7b870SPatrick McHardy  * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
41638f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to unregister
41738f7b870SPatrick McHardy  */
41838f7b870SPatrick McHardy void rtnl_link_unregister(struct rtnl_link_ops *ops)
41938f7b870SPatrick McHardy {
420200b916fSCong Wang 	/* Close the race with cleanup_net() */
421200b916fSCong Wang 	mutex_lock(&net_mutex);
422200b916fSCong Wang 	rtnl_lock_unregistering_all();
42338f7b870SPatrick McHardy 	__rtnl_link_unregister(ops);
42438f7b870SPatrick McHardy 	rtnl_unlock();
425200b916fSCong Wang 	mutex_unlock(&net_mutex);
42638f7b870SPatrick McHardy }
42738f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_unregister);
42838f7b870SPatrick McHardy 
429ba7d49b1SJiri Pirko static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
430ba7d49b1SJiri Pirko {
431ba7d49b1SJiri Pirko 	struct net_device *master_dev;
432ba7d49b1SJiri Pirko 	const struct rtnl_link_ops *ops;
433ba7d49b1SJiri Pirko 
434ba7d49b1SJiri Pirko 	master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
435ba7d49b1SJiri Pirko 	if (!master_dev)
436ba7d49b1SJiri Pirko 		return 0;
437ba7d49b1SJiri Pirko 	ops = master_dev->rtnl_link_ops;
4386049f253SFernando Luis Vazquez Cao 	if (!ops || !ops->get_slave_size)
439ba7d49b1SJiri Pirko 		return 0;
440ba7d49b1SJiri Pirko 	/* IFLA_INFO_SLAVE_DATA + nested data */
441ba7d49b1SJiri Pirko 	return nla_total_size(sizeof(struct nlattr)) +
442ba7d49b1SJiri Pirko 	       ops->get_slave_size(master_dev, dev);
443ba7d49b1SJiri Pirko }
444ba7d49b1SJiri Pirko 
44538f7b870SPatrick McHardy static size_t rtnl_link_get_size(const struct net_device *dev)
44638f7b870SPatrick McHardy {
44738f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
44838f7b870SPatrick McHardy 	size_t size;
44938f7b870SPatrick McHardy 
45038f7b870SPatrick McHardy 	if (!ops)
45138f7b870SPatrick McHardy 		return 0;
45238f7b870SPatrick McHardy 
453369cf77aSThomas Graf 	size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
454369cf77aSThomas Graf 	       nla_total_size(strlen(ops->kind) + 1);  /* IFLA_INFO_KIND */
45538f7b870SPatrick McHardy 
45638f7b870SPatrick McHardy 	if (ops->get_size)
45738f7b870SPatrick McHardy 		/* IFLA_INFO_DATA + nested data */
458369cf77aSThomas Graf 		size += nla_total_size(sizeof(struct nlattr)) +
45938f7b870SPatrick McHardy 			ops->get_size(dev);
46038f7b870SPatrick McHardy 
46138f7b870SPatrick McHardy 	if (ops->get_xstats_size)
462369cf77aSThomas Graf 		/* IFLA_INFO_XSTATS */
463369cf77aSThomas Graf 		size += nla_total_size(ops->get_xstats_size(dev));
46438f7b870SPatrick McHardy 
465ba7d49b1SJiri Pirko 	size += rtnl_link_get_slave_info_data_size(dev);
466ba7d49b1SJiri Pirko 
46738f7b870SPatrick McHardy 	return size;
46838f7b870SPatrick McHardy }
46938f7b870SPatrick McHardy 
470f8ff182cSThomas Graf static LIST_HEAD(rtnl_af_ops);
471f8ff182cSThomas Graf 
472f8ff182cSThomas Graf static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
473f8ff182cSThomas Graf {
474f8ff182cSThomas Graf 	const struct rtnl_af_ops *ops;
475f8ff182cSThomas Graf 
476f8ff182cSThomas Graf 	list_for_each_entry(ops, &rtnl_af_ops, list) {
477f8ff182cSThomas Graf 		if (ops->family == family)
478f8ff182cSThomas Graf 			return ops;
479f8ff182cSThomas Graf 	}
480f8ff182cSThomas Graf 
481f8ff182cSThomas Graf 	return NULL;
482f8ff182cSThomas Graf }
483f8ff182cSThomas Graf 
484f8ff182cSThomas Graf /**
485f8ff182cSThomas Graf  * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
486f8ff182cSThomas Graf  * @ops: struct rtnl_af_ops * to register
487f8ff182cSThomas Graf  *
488f8ff182cSThomas Graf  * Returns 0 on success or a negative error code.
489f8ff182cSThomas Graf  */
4903678a9d8Sstephen hemminger void rtnl_af_register(struct rtnl_af_ops *ops)
491f8ff182cSThomas Graf {
492f8ff182cSThomas Graf 	rtnl_lock();
4933678a9d8Sstephen hemminger 	list_add_tail(&ops->list, &rtnl_af_ops);
494f8ff182cSThomas Graf 	rtnl_unlock();
495f8ff182cSThomas Graf }
496f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_register);
497f8ff182cSThomas Graf 
498f8ff182cSThomas Graf /**
499f8ff182cSThomas Graf  * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
500f8ff182cSThomas Graf  * @ops: struct rtnl_af_ops * to unregister
501f8ff182cSThomas Graf  *
502f8ff182cSThomas Graf  * The caller must hold the rtnl_mutex.
503f8ff182cSThomas Graf  */
504f8ff182cSThomas Graf void __rtnl_af_unregister(struct rtnl_af_ops *ops)
505f8ff182cSThomas Graf {
506f8ff182cSThomas Graf 	list_del(&ops->list);
507f8ff182cSThomas Graf }
508f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(__rtnl_af_unregister);
509f8ff182cSThomas Graf 
510f8ff182cSThomas Graf /**
511f8ff182cSThomas Graf  * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
512f8ff182cSThomas Graf  * @ops: struct rtnl_af_ops * to unregister
513f8ff182cSThomas Graf  */
514f8ff182cSThomas Graf void rtnl_af_unregister(struct rtnl_af_ops *ops)
515f8ff182cSThomas Graf {
516f8ff182cSThomas Graf 	rtnl_lock();
517f8ff182cSThomas Graf 	__rtnl_af_unregister(ops);
518f8ff182cSThomas Graf 	rtnl_unlock();
519f8ff182cSThomas Graf }
520f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_unregister);
521f8ff182cSThomas Graf 
522b1974ed0SArad, Ronen static size_t rtnl_link_get_af_size(const struct net_device *dev,
523b1974ed0SArad, Ronen 				    u32 ext_filter_mask)
524f8ff182cSThomas Graf {
525f8ff182cSThomas Graf 	struct rtnl_af_ops *af_ops;
526f8ff182cSThomas Graf 	size_t size;
527f8ff182cSThomas Graf 
528f8ff182cSThomas Graf 	/* IFLA_AF_SPEC */
529f8ff182cSThomas Graf 	size = nla_total_size(sizeof(struct nlattr));
530f8ff182cSThomas Graf 
531f8ff182cSThomas Graf 	list_for_each_entry(af_ops, &rtnl_af_ops, list) {
532f8ff182cSThomas Graf 		if (af_ops->get_link_af_size) {
533f8ff182cSThomas Graf 			/* AF_* + nested data */
534f8ff182cSThomas Graf 			size += nla_total_size(sizeof(struct nlattr)) +
535b1974ed0SArad, Ronen 				af_ops->get_link_af_size(dev, ext_filter_mask);
536f8ff182cSThomas Graf 		}
537f8ff182cSThomas Graf 	}
538f8ff182cSThomas Graf 
539f8ff182cSThomas Graf 	return size;
540f8ff182cSThomas Graf }
541f8ff182cSThomas Graf 
542ba7d49b1SJiri Pirko static bool rtnl_have_link_slave_info(const struct net_device *dev)
543ba7d49b1SJiri Pirko {
544ba7d49b1SJiri Pirko 	struct net_device *master_dev;
545ba7d49b1SJiri Pirko 
546ba7d49b1SJiri Pirko 	master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
547813f020cSJiri Pirko 	if (master_dev && master_dev->rtnl_link_ops)
548ba7d49b1SJiri Pirko 		return true;
549ba7d49b1SJiri Pirko 	return false;
550ba7d49b1SJiri Pirko }
551ba7d49b1SJiri Pirko 
552ba7d49b1SJiri Pirko static int rtnl_link_slave_info_fill(struct sk_buff *skb,
553ba7d49b1SJiri Pirko 				     const struct net_device *dev)
554ba7d49b1SJiri Pirko {
555ba7d49b1SJiri Pirko 	struct net_device *master_dev;
556ba7d49b1SJiri Pirko 	const struct rtnl_link_ops *ops;
557ba7d49b1SJiri Pirko 	struct nlattr *slave_data;
558ba7d49b1SJiri Pirko 	int err;
559ba7d49b1SJiri Pirko 
560ba7d49b1SJiri Pirko 	master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
561ba7d49b1SJiri Pirko 	if (!master_dev)
562ba7d49b1SJiri Pirko 		return 0;
563ba7d49b1SJiri Pirko 	ops = master_dev->rtnl_link_ops;
564ba7d49b1SJiri Pirko 	if (!ops)
565ba7d49b1SJiri Pirko 		return 0;
566ba7d49b1SJiri Pirko 	if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
567ba7d49b1SJiri Pirko 		return -EMSGSIZE;
568ba7d49b1SJiri Pirko 	if (ops->fill_slave_info) {
569ba7d49b1SJiri Pirko 		slave_data = nla_nest_start(skb, IFLA_INFO_SLAVE_DATA);
570ba7d49b1SJiri Pirko 		if (!slave_data)
571ba7d49b1SJiri Pirko 			return -EMSGSIZE;
572ba7d49b1SJiri Pirko 		err = ops->fill_slave_info(skb, master_dev, dev);
573ba7d49b1SJiri Pirko 		if (err < 0)
574ba7d49b1SJiri Pirko 			goto err_cancel_slave_data;
575ba7d49b1SJiri Pirko 		nla_nest_end(skb, slave_data);
576ba7d49b1SJiri Pirko 	}
577ba7d49b1SJiri Pirko 	return 0;
578ba7d49b1SJiri Pirko 
579ba7d49b1SJiri Pirko err_cancel_slave_data:
580ba7d49b1SJiri Pirko 	nla_nest_cancel(skb, slave_data);
581ba7d49b1SJiri Pirko 	return err;
582ba7d49b1SJiri Pirko }
583ba7d49b1SJiri Pirko 
584ba7d49b1SJiri Pirko static int rtnl_link_info_fill(struct sk_buff *skb,
585ba7d49b1SJiri Pirko 			       const struct net_device *dev)
58638f7b870SPatrick McHardy {
58738f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
588ba7d49b1SJiri Pirko 	struct nlattr *data;
589ba7d49b1SJiri Pirko 	int err;
590ba7d49b1SJiri Pirko 
591ba7d49b1SJiri Pirko 	if (!ops)
592ba7d49b1SJiri Pirko 		return 0;
593ba7d49b1SJiri Pirko 	if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
594ba7d49b1SJiri Pirko 		return -EMSGSIZE;
595ba7d49b1SJiri Pirko 	if (ops->fill_xstats) {
596ba7d49b1SJiri Pirko 		err = ops->fill_xstats(skb, dev);
597ba7d49b1SJiri Pirko 		if (err < 0)
598ba7d49b1SJiri Pirko 			return err;
599ba7d49b1SJiri Pirko 	}
600ba7d49b1SJiri Pirko 	if (ops->fill_info) {
601ba7d49b1SJiri Pirko 		data = nla_nest_start(skb, IFLA_INFO_DATA);
602ba7d49b1SJiri Pirko 		if (data == NULL)
603ba7d49b1SJiri Pirko 			return -EMSGSIZE;
604ba7d49b1SJiri Pirko 		err = ops->fill_info(skb, dev);
605ba7d49b1SJiri Pirko 		if (err < 0)
606ba7d49b1SJiri Pirko 			goto err_cancel_data;
607ba7d49b1SJiri Pirko 		nla_nest_end(skb, data);
608ba7d49b1SJiri Pirko 	}
609ba7d49b1SJiri Pirko 	return 0;
610ba7d49b1SJiri Pirko 
611ba7d49b1SJiri Pirko err_cancel_data:
612ba7d49b1SJiri Pirko 	nla_nest_cancel(skb, data);
613ba7d49b1SJiri Pirko 	return err;
614ba7d49b1SJiri Pirko }
615ba7d49b1SJiri Pirko 
616ba7d49b1SJiri Pirko static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
617ba7d49b1SJiri Pirko {
618ba7d49b1SJiri Pirko 	struct nlattr *linkinfo;
61938f7b870SPatrick McHardy 	int err = -EMSGSIZE;
62038f7b870SPatrick McHardy 
62138f7b870SPatrick McHardy 	linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
62238f7b870SPatrick McHardy 	if (linkinfo == NULL)
62338f7b870SPatrick McHardy 		goto out;
62438f7b870SPatrick McHardy 
625ba7d49b1SJiri Pirko 	err = rtnl_link_info_fill(skb, dev);
62638f7b870SPatrick McHardy 	if (err < 0)
62738f7b870SPatrick McHardy 		goto err_cancel_link;
628ba7d49b1SJiri Pirko 
629ba7d49b1SJiri Pirko 	err = rtnl_link_slave_info_fill(skb, dev);
63038f7b870SPatrick McHardy 	if (err < 0)
631ba7d49b1SJiri Pirko 		goto err_cancel_link;
63238f7b870SPatrick McHardy 
63338f7b870SPatrick McHardy 	nla_nest_end(skb, linkinfo);
63438f7b870SPatrick McHardy 	return 0;
63538f7b870SPatrick McHardy 
63638f7b870SPatrick McHardy err_cancel_link:
63738f7b870SPatrick McHardy 	nla_nest_cancel(skb, linkinfo);
63838f7b870SPatrick McHardy out:
63938f7b870SPatrick McHardy 	return err;
64038f7b870SPatrick McHardy }
64138f7b870SPatrick McHardy 
64295c96174SEric Dumazet int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
6431da177e4SLinus Torvalds {
64497c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
6451da177e4SLinus Torvalds 	int err = 0;
6461da177e4SLinus Torvalds 
647ac6d439dSPatrick McHardy 	NETLINK_CB(skb).dst_group = group;
6481da177e4SLinus Torvalds 	if (echo)
6491da177e4SLinus Torvalds 		atomic_inc(&skb->users);
6501da177e4SLinus Torvalds 	netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
6511da177e4SLinus Torvalds 	if (echo)
6521da177e4SLinus Torvalds 		err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
6531da177e4SLinus Torvalds 	return err;
6541da177e4SLinus Torvalds }
6551da177e4SLinus Torvalds 
65697c53cacSDenis V. Lunev int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
6572942e900SThomas Graf {
65897c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
65997c53cacSDenis V. Lunev 
6602942e900SThomas Graf 	return nlmsg_unicast(rtnl, skb, pid);
6612942e900SThomas Graf }
662e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unicast);
6632942e900SThomas Graf 
6641ce85fe4SPablo Neira Ayuso void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
66597676b6bSThomas Graf 		 struct nlmsghdr *nlh, gfp_t flags)
66697676b6bSThomas Graf {
66797c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
66897676b6bSThomas Graf 	int report = 0;
66997676b6bSThomas Graf 
67097676b6bSThomas Graf 	if (nlh)
67197676b6bSThomas Graf 		report = nlmsg_report(nlh);
67297676b6bSThomas Graf 
6731ce85fe4SPablo Neira Ayuso 	nlmsg_notify(rtnl, skb, pid, group, report, flags);
67497676b6bSThomas Graf }
675e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_notify);
67697676b6bSThomas Graf 
67797c53cacSDenis V. Lunev void rtnl_set_sk_err(struct net *net, u32 group, int error)
67897676b6bSThomas Graf {
67997c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
68097c53cacSDenis V. Lunev 
68197676b6bSThomas Graf 	netlink_set_err(rtnl, 0, group, error);
68297676b6bSThomas Graf }
683e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_set_sk_err);
68497676b6bSThomas Graf 
6851da177e4SLinus Torvalds int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
6861da177e4SLinus Torvalds {
6872d7202bfSThomas Graf 	struct nlattr *mx;
6882d7202bfSThomas Graf 	int i, valid = 0;
6891da177e4SLinus Torvalds 
6902d7202bfSThomas Graf 	mx = nla_nest_start(skb, RTA_METRICS);
6912d7202bfSThomas Graf 	if (mx == NULL)
6922d7202bfSThomas Graf 		return -ENOBUFS;
6932d7202bfSThomas Graf 
6941da177e4SLinus Torvalds 	for (i = 0; i < RTAX_MAX; i++) {
6952d7202bfSThomas Graf 		if (metrics[i]) {
696ea697639SDaniel Borkmann 			if (i == RTAX_CC_ALGO - 1) {
697ea697639SDaniel Borkmann 				char tmp[TCP_CA_NAME_MAX], *name;
698ea697639SDaniel Borkmann 
699ea697639SDaniel Borkmann 				name = tcp_ca_get_name_by_key(metrics[i], tmp);
700ea697639SDaniel Borkmann 				if (!name)
701ea697639SDaniel Borkmann 					continue;
702ea697639SDaniel Borkmann 				if (nla_put_string(skb, i + 1, name))
703ea697639SDaniel Borkmann 					goto nla_put_failure;
704c3a8d947SDaniel Borkmann 			} else if (i == RTAX_FEATURES - 1) {
705c3a8d947SDaniel Borkmann 				u32 user_features = metrics[i] & RTAX_FEATURE_MASK;
706c3a8d947SDaniel Borkmann 
707c3a8d947SDaniel Borkmann 				BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK);
708c3a8d947SDaniel Borkmann 				if (nla_put_u32(skb, i + 1, user_features))
709c3a8d947SDaniel Borkmann 					goto nla_put_failure;
710ea697639SDaniel Borkmann 			} else {
711a6574349SDavid S. Miller 				if (nla_put_u32(skb, i + 1, metrics[i]))
712a6574349SDavid S. Miller 					goto nla_put_failure;
7131da177e4SLinus Torvalds 			}
714ea697639SDaniel Borkmann 			valid++;
715ea697639SDaniel Borkmann 		}
7162d7202bfSThomas Graf 	}
7171da177e4SLinus Torvalds 
718a57d27fcSDavid S. Miller 	if (!valid) {
719a57d27fcSDavid S. Miller 		nla_nest_cancel(skb, mx);
720a57d27fcSDavid S. Miller 		return 0;
721a57d27fcSDavid S. Miller 	}
7222d7202bfSThomas Graf 
7232d7202bfSThomas Graf 	return nla_nest_end(skb, mx);
7242d7202bfSThomas Graf 
7252d7202bfSThomas Graf nla_put_failure:
726bc3ed28cSThomas Graf 	nla_nest_cancel(skb, mx);
727bc3ed28cSThomas Graf 	return -EMSGSIZE;
7281da177e4SLinus Torvalds }
729e0d087afSEric Dumazet EXPORT_SYMBOL(rtnetlink_put_metrics);
7301da177e4SLinus Torvalds 
731e3703b3dSThomas Graf int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
73287a50699SDavid S. Miller 		       long expires, u32 error)
733e3703b3dSThomas Graf {
734e3703b3dSThomas Graf 	struct rta_cacheinfo ci = {
735a399a805SEric Dumazet 		.rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse),
736e3703b3dSThomas Graf 		.rta_used = dst->__use,
737e3703b3dSThomas Graf 		.rta_clntref = atomic_read(&(dst->__refcnt)),
738e3703b3dSThomas Graf 		.rta_error = error,
739e3703b3dSThomas Graf 		.rta_id =  id,
740e3703b3dSThomas Graf 	};
741e3703b3dSThomas Graf 
7428253947eSLi Wei 	if (expires) {
7438253947eSLi Wei 		unsigned long clock;
744e3703b3dSThomas Graf 
7458253947eSLi Wei 		clock = jiffies_to_clock_t(abs(expires));
7468253947eSLi Wei 		clock = min_t(unsigned long, clock, INT_MAX);
7478253947eSLi Wei 		ci.rta_expires = (expires > 0) ? clock : -clock;
7488253947eSLi Wei 	}
749e3703b3dSThomas Graf 	return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
750e3703b3dSThomas Graf }
751e3703b3dSThomas Graf EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
7521da177e4SLinus Torvalds 
75393b2d4a2SDavid S. Miller static void set_operstate(struct net_device *dev, unsigned char transition)
754b00055aaSStefan Rompf {
755b00055aaSStefan Rompf 	unsigned char operstate = dev->operstate;
756b00055aaSStefan Rompf 
757b00055aaSStefan Rompf 	switch (transition) {
758b00055aaSStefan Rompf 	case IF_OPER_UP:
759b00055aaSStefan Rompf 		if ((operstate == IF_OPER_DORMANT ||
760b00055aaSStefan Rompf 		     operstate == IF_OPER_UNKNOWN) &&
761b00055aaSStefan Rompf 		    !netif_dormant(dev))
762b00055aaSStefan Rompf 			operstate = IF_OPER_UP;
763b00055aaSStefan Rompf 		break;
764b00055aaSStefan Rompf 
765b00055aaSStefan Rompf 	case IF_OPER_DORMANT:
766b00055aaSStefan Rompf 		if (operstate == IF_OPER_UP ||
767b00055aaSStefan Rompf 		    operstate == IF_OPER_UNKNOWN)
768b00055aaSStefan Rompf 			operstate = IF_OPER_DORMANT;
769b00055aaSStefan Rompf 		break;
7703ff50b79SStephen Hemminger 	}
771b00055aaSStefan Rompf 
772b00055aaSStefan Rompf 	if (dev->operstate != operstate) {
773b00055aaSStefan Rompf 		write_lock_bh(&dev_base_lock);
774b00055aaSStefan Rompf 		dev->operstate = operstate;
775b00055aaSStefan Rompf 		write_unlock_bh(&dev_base_lock);
776b00055aaSStefan Rompf 		netdev_state_change(dev);
77793b2d4a2SDavid S. Miller 	}
778b00055aaSStefan Rompf }
779b00055aaSStefan Rompf 
780b1beb681SJiri Benc static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
781b1beb681SJiri Benc {
782b1beb681SJiri Benc 	return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
783b1beb681SJiri Benc 	       (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
784b1beb681SJiri Benc }
785b1beb681SJiri Benc 
7863729d502SPatrick McHardy static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
7873729d502SPatrick McHardy 					   const struct ifinfomsg *ifm)
7883729d502SPatrick McHardy {
7893729d502SPatrick McHardy 	unsigned int flags = ifm->ifi_flags;
7903729d502SPatrick McHardy 
7913729d502SPatrick McHardy 	/* bugwards compatibility: ifi_change == 0 is treated as ~0 */
7923729d502SPatrick McHardy 	if (ifm->ifi_change)
7933729d502SPatrick McHardy 		flags = (flags & ifm->ifi_change) |
794b1beb681SJiri Benc 			(rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
7953729d502SPatrick McHardy 
7963729d502SPatrick McHardy 	return flags;
7973729d502SPatrick McHardy }
7983729d502SPatrick McHardy 
799b60c5115SThomas Graf static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
800be1f3c2cSBen Hutchings 				 const struct rtnl_link_stats64 *b)
8011da177e4SLinus Torvalds {
802b60c5115SThomas Graf 	a->rx_packets = b->rx_packets;
803b60c5115SThomas Graf 	a->tx_packets = b->tx_packets;
804b60c5115SThomas Graf 	a->rx_bytes = b->rx_bytes;
805b60c5115SThomas Graf 	a->tx_bytes = b->tx_bytes;
806b60c5115SThomas Graf 	a->rx_errors = b->rx_errors;
807b60c5115SThomas Graf 	a->tx_errors = b->tx_errors;
808b60c5115SThomas Graf 	a->rx_dropped = b->rx_dropped;
809b60c5115SThomas Graf 	a->tx_dropped = b->tx_dropped;
810b60c5115SThomas Graf 
811b60c5115SThomas Graf 	a->multicast = b->multicast;
812b60c5115SThomas Graf 	a->collisions = b->collisions;
813b60c5115SThomas Graf 
814b60c5115SThomas Graf 	a->rx_length_errors = b->rx_length_errors;
815b60c5115SThomas Graf 	a->rx_over_errors = b->rx_over_errors;
816b60c5115SThomas Graf 	a->rx_crc_errors = b->rx_crc_errors;
817b60c5115SThomas Graf 	a->rx_frame_errors = b->rx_frame_errors;
818b60c5115SThomas Graf 	a->rx_fifo_errors = b->rx_fifo_errors;
819b60c5115SThomas Graf 	a->rx_missed_errors = b->rx_missed_errors;
820b60c5115SThomas Graf 
821b60c5115SThomas Graf 	a->tx_aborted_errors = b->tx_aborted_errors;
822b60c5115SThomas Graf 	a->tx_carrier_errors = b->tx_carrier_errors;
823b60c5115SThomas Graf 	a->tx_fifo_errors = b->tx_fifo_errors;
824b60c5115SThomas Graf 	a->tx_heartbeat_errors = b->tx_heartbeat_errors;
825b60c5115SThomas Graf 	a->tx_window_errors = b->tx_window_errors;
826b60c5115SThomas Graf 
827b60c5115SThomas Graf 	a->rx_compressed = b->rx_compressed;
828b60c5115SThomas Graf 	a->tx_compressed = b->tx_compressed;
8296e7333d3SJarod Wilson 
8306e7333d3SJarod Wilson 	a->rx_nohandler = b->rx_nohandler;
83110708f37SJan Engelhardt }
83210708f37SJan Engelhardt 
833c02db8c6SChris Wright /* All VF info */
834115c9b81SGreg Rose static inline int rtnl_vfinfo_size(const struct net_device *dev,
835115c9b81SGreg Rose 				   u32 ext_filter_mask)
836ebc08a6fSWilliams, Mitch A {
837115c9b81SGreg Rose 	if (dev->dev.parent && dev_is_pci(dev->dev.parent) &&
838115c9b81SGreg Rose 	    (ext_filter_mask & RTEXT_FILTER_VF)) {
839c02db8c6SChris Wright 		int num_vfs = dev_num_vf(dev->dev.parent);
840045de01aSScott Feldman 		size_t size = nla_total_size(sizeof(struct nlattr));
841045de01aSScott Feldman 		size += nla_total_size(num_vfs * sizeof(struct nlattr));
842045de01aSScott Feldman 		size += num_vfs *
843045de01aSScott Feldman 			(nla_total_size(sizeof(struct ifla_vf_mac)) +
844045de01aSScott Feldman 			 nla_total_size(sizeof(struct ifla_vf_vlan)) +
845ed616689SSucheta Chakraborty 			 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
846945a3676SJiri Benc 			 nla_total_size(sizeof(struct ifla_vf_rate)) +
84701a3d796SVlad Zolotarov 			 nla_total_size(sizeof(struct ifla_vf_link_state)) +
8483b766cd8SEran Ben Elisha 			 nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
8493b766cd8SEran Ben Elisha 			 /* IFLA_VF_STATS_RX_PACKETS */
850343a6d8eSNicolas Dichtel 			 nla_total_size_64bit(sizeof(__u64)) +
8513b766cd8SEran Ben Elisha 			 /* IFLA_VF_STATS_TX_PACKETS */
852343a6d8eSNicolas Dichtel 			 nla_total_size_64bit(sizeof(__u64)) +
8533b766cd8SEran Ben Elisha 			 /* IFLA_VF_STATS_RX_BYTES */
854343a6d8eSNicolas Dichtel 			 nla_total_size_64bit(sizeof(__u64)) +
8553b766cd8SEran Ben Elisha 			 /* IFLA_VF_STATS_TX_BYTES */
856343a6d8eSNicolas Dichtel 			 nla_total_size_64bit(sizeof(__u64)) +
8573b766cd8SEran Ben Elisha 			 /* IFLA_VF_STATS_BROADCAST */
858343a6d8eSNicolas Dichtel 			 nla_total_size_64bit(sizeof(__u64)) +
8593b766cd8SEran Ben Elisha 			 /* IFLA_VF_STATS_MULTICAST */
860343a6d8eSNicolas Dichtel 			 nla_total_size_64bit(sizeof(__u64)) +
861dd461d6aSHiroshi Shimamoto 			 nla_total_size(sizeof(struct ifla_vf_trust)));
862c02db8c6SChris Wright 		return size;
863c02db8c6SChris Wright 	} else
864ebc08a6fSWilliams, Mitch A 		return 0;
865ebc08a6fSWilliams, Mitch A }
866ebc08a6fSWilliams, Mitch A 
867c53864fdSDavid Gibson static size_t rtnl_port_size(const struct net_device *dev,
868c53864fdSDavid Gibson 			     u32 ext_filter_mask)
86957b61080SScott Feldman {
87057b61080SScott Feldman 	size_t port_size = nla_total_size(4)		/* PORT_VF */
87157b61080SScott Feldman 		+ nla_total_size(PORT_PROFILE_MAX)	/* PORT_PROFILE */
87257b61080SScott Feldman 		+ nla_total_size(sizeof(struct ifla_port_vsi))
87357b61080SScott Feldman 							/* PORT_VSI_TYPE */
87457b61080SScott Feldman 		+ nla_total_size(PORT_UUID_MAX)		/* PORT_INSTANCE_UUID */
87557b61080SScott Feldman 		+ nla_total_size(PORT_UUID_MAX)		/* PORT_HOST_UUID */
87657b61080SScott Feldman 		+ nla_total_size(1)			/* PROT_VDP_REQUEST */
87757b61080SScott Feldman 		+ nla_total_size(2);			/* PORT_VDP_RESPONSE */
87857b61080SScott Feldman 	size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
87957b61080SScott Feldman 	size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
88057b61080SScott Feldman 		+ port_size;
88157b61080SScott Feldman 	size_t port_self_size = nla_total_size(sizeof(struct nlattr))
88257b61080SScott Feldman 		+ port_size;
88357b61080SScott Feldman 
884c53864fdSDavid Gibson 	if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
885c53864fdSDavid Gibson 	    !(ext_filter_mask & RTEXT_FILTER_VF))
88657b61080SScott Feldman 		return 0;
88757b61080SScott Feldman 	if (dev_num_vf(dev->dev.parent))
88857b61080SScott Feldman 		return port_self_size + vf_ports_size +
88957b61080SScott Feldman 			vf_port_size * dev_num_vf(dev->dev.parent);
89057b61080SScott Feldman 	else
89157b61080SScott Feldman 		return port_self_size;
89257b61080SScott Feldman }
89357b61080SScott Feldman 
894d1fdd913SBrenden Blanco static size_t rtnl_xdp_size(const struct net_device *dev)
895d1fdd913SBrenden Blanco {
896d1fdd913SBrenden Blanco 	size_t xdp_size = nla_total_size(1);	/* XDP_ATTACHED */
897d1fdd913SBrenden Blanco 
898d1fdd913SBrenden Blanco 	if (!dev->netdev_ops->ndo_xdp)
899d1fdd913SBrenden Blanco 		return 0;
900d1fdd913SBrenden Blanco 	else
901d1fdd913SBrenden Blanco 		return xdp_size;
902d1fdd913SBrenden Blanco }
903d1fdd913SBrenden Blanco 
904115c9b81SGreg Rose static noinline size_t if_nlmsg_size(const struct net_device *dev,
905115c9b81SGreg Rose 				     u32 ext_filter_mask)
906339bf98fSThomas Graf {
907339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
908339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
9090b815a1aSStephen Hemminger 	       + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
910339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
911270cb4d0SNicolas Dichtel 	       + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap))
912339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rtnl_link_stats))
91335c58459SDavid S. Miller 	       + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
914339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
915339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
916339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_TXQLEN */
917339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_WEIGHT */
918339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MTU */
919339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_LINK */
920339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MASTER */
9219a57247fSJiri Pirko 	       + nla_total_size(1) /* IFLA_CARRIER */
922edbc0bb3SBen Greear 	       + nla_total_size(4) /* IFLA_PROMISCUITY */
92376ff5cc9SJiri Pirko 	       + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
92476ff5cc9SJiri Pirko 	       + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
925c70ce028SEric Dumazet 	       + nla_total_size(4) /* IFLA_MAX_GSO_SEGS */
926c70ce028SEric Dumazet 	       + nla_total_size(4) /* IFLA_MAX_GSO_SIZE */
927339bf98fSThomas Graf 	       + nla_total_size(1) /* IFLA_OPERSTATE */
92838f7b870SPatrick McHardy 	       + nla_total_size(1) /* IFLA_LINKMODE */
9292d3b479dSdavid decotigny 	       + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
930d37512a2SNicolas Dichtel 	       + nla_total_size(4) /* IFLA_LINK_NETNSID */
931115c9b81SGreg Rose 	       + nla_total_size(ext_filter_mask
932115c9b81SGreg Rose 			        & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
933115c9b81SGreg Rose 	       + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
934c53864fdSDavid Gibson 	       + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
935f8ff182cSThomas Graf 	       + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
936b1974ed0SArad, Ronen 	       + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
93782f28412SJiri Pirko 	       + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
93888d6378bSAnuradha Karuppiah 	       + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
939c57c7a95SNicolas Dichtel 	       + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
940d1fdd913SBrenden Blanco 	       + rtnl_xdp_size(dev) /* IFLA_XDP */
94188d6378bSAnuradha Karuppiah 	       + nla_total_size(1); /* IFLA_PROTO_DOWN */
94288d6378bSAnuradha Karuppiah 
943339bf98fSThomas Graf }
944339bf98fSThomas Graf 
94557b61080SScott Feldman static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
94657b61080SScott Feldman {
94757b61080SScott Feldman 	struct nlattr *vf_ports;
94857b61080SScott Feldman 	struct nlattr *vf_port;
94957b61080SScott Feldman 	int vf;
95057b61080SScott Feldman 	int err;
95157b61080SScott Feldman 
95257b61080SScott Feldman 	vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
95357b61080SScott Feldman 	if (!vf_ports)
95457b61080SScott Feldman 		return -EMSGSIZE;
95557b61080SScott Feldman 
95657b61080SScott Feldman 	for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
95757b61080SScott Feldman 		vf_port = nla_nest_start(skb, IFLA_VF_PORT);
9588ca94183SScott Feldman 		if (!vf_port)
9598ca94183SScott Feldman 			goto nla_put_failure;
960a6574349SDavid S. Miller 		if (nla_put_u32(skb, IFLA_PORT_VF, vf))
961a6574349SDavid S. Miller 			goto nla_put_failure;
96257b61080SScott Feldman 		err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
9638ca94183SScott Feldman 		if (err == -EMSGSIZE)
9648ca94183SScott Feldman 			goto nla_put_failure;
96557b61080SScott Feldman 		if (err) {
96657b61080SScott Feldman 			nla_nest_cancel(skb, vf_port);
96757b61080SScott Feldman 			continue;
96857b61080SScott Feldman 		}
96957b61080SScott Feldman 		nla_nest_end(skb, vf_port);
97057b61080SScott Feldman 	}
97157b61080SScott Feldman 
97257b61080SScott Feldman 	nla_nest_end(skb, vf_ports);
97357b61080SScott Feldman 
97457b61080SScott Feldman 	return 0;
9758ca94183SScott Feldman 
9768ca94183SScott Feldman nla_put_failure:
9778ca94183SScott Feldman 	nla_nest_cancel(skb, vf_ports);
9788ca94183SScott Feldman 	return -EMSGSIZE;
97957b61080SScott Feldman }
98057b61080SScott Feldman 
98157b61080SScott Feldman static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
98257b61080SScott Feldman {
98357b61080SScott Feldman 	struct nlattr *port_self;
98457b61080SScott Feldman 	int err;
98557b61080SScott Feldman 
98657b61080SScott Feldman 	port_self = nla_nest_start(skb, IFLA_PORT_SELF);
98757b61080SScott Feldman 	if (!port_self)
98857b61080SScott Feldman 		return -EMSGSIZE;
98957b61080SScott Feldman 
99057b61080SScott Feldman 	err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
99157b61080SScott Feldman 	if (err) {
99257b61080SScott Feldman 		nla_nest_cancel(skb, port_self);
9938ca94183SScott Feldman 		return (err == -EMSGSIZE) ? err : 0;
99457b61080SScott Feldman 	}
99557b61080SScott Feldman 
99657b61080SScott Feldman 	nla_nest_end(skb, port_self);
99757b61080SScott Feldman 
99857b61080SScott Feldman 	return 0;
99957b61080SScott Feldman }
100057b61080SScott Feldman 
1001c53864fdSDavid Gibson static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
1002c53864fdSDavid Gibson 			  u32 ext_filter_mask)
100357b61080SScott Feldman {
100457b61080SScott Feldman 	int err;
100557b61080SScott Feldman 
1006c53864fdSDavid Gibson 	if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
1007c53864fdSDavid Gibson 	    !(ext_filter_mask & RTEXT_FILTER_VF))
100857b61080SScott Feldman 		return 0;
100957b61080SScott Feldman 
101057b61080SScott Feldman 	err = rtnl_port_self_fill(skb, dev);
101157b61080SScott Feldman 	if (err)
101257b61080SScott Feldman 		return err;
101357b61080SScott Feldman 
101457b61080SScott Feldman 	if (dev_num_vf(dev->dev.parent)) {
101557b61080SScott Feldman 		err = rtnl_vf_ports_fill(skb, dev);
101657b61080SScott Feldman 		if (err)
101757b61080SScott Feldman 			return err;
101857b61080SScott Feldman 	}
101957b61080SScott Feldman 
102057b61080SScott Feldman 	return 0;
102157b61080SScott Feldman }
102257b61080SScott Feldman 
102366cae9edSJiri Pirko static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
102466cae9edSJiri Pirko {
102566cae9edSJiri Pirko 	int err;
102602637fceSJiri Pirko 	struct netdev_phys_item_id ppid;
102766cae9edSJiri Pirko 
102866cae9edSJiri Pirko 	err = dev_get_phys_port_id(dev, &ppid);
102966cae9edSJiri Pirko 	if (err) {
103066cae9edSJiri Pirko 		if (err == -EOPNOTSUPP)
103166cae9edSJiri Pirko 			return 0;
103266cae9edSJiri Pirko 		return err;
103366cae9edSJiri Pirko 	}
103466cae9edSJiri Pirko 
103566cae9edSJiri Pirko 	if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
103666cae9edSJiri Pirko 		return -EMSGSIZE;
103766cae9edSJiri Pirko 
103866cae9edSJiri Pirko 	return 0;
103966cae9edSJiri Pirko }
104066cae9edSJiri Pirko 
1041db24a904SDavid Ahern static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1042db24a904SDavid Ahern {
1043db24a904SDavid Ahern 	char name[IFNAMSIZ];
1044db24a904SDavid Ahern 	int err;
1045db24a904SDavid Ahern 
1046db24a904SDavid Ahern 	err = dev_get_phys_port_name(dev, name, sizeof(name));
1047db24a904SDavid Ahern 	if (err) {
1048db24a904SDavid Ahern 		if (err == -EOPNOTSUPP)
1049db24a904SDavid Ahern 			return 0;
1050db24a904SDavid Ahern 		return err;
1051db24a904SDavid Ahern 	}
1052db24a904SDavid Ahern 
1053db24a904SDavid Ahern 	if (nla_put(skb, IFLA_PHYS_PORT_NAME, strlen(name), name))
1054db24a904SDavid Ahern 		return -EMSGSIZE;
1055db24a904SDavid Ahern 
1056db24a904SDavid Ahern 	return 0;
1057db24a904SDavid Ahern }
1058db24a904SDavid Ahern 
105982f28412SJiri Pirko static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
106082f28412SJiri Pirko {
106182f28412SJiri Pirko 	int err;
1062f8e20a9fSScott Feldman 	struct switchdev_attr attr = {
10636ff64f6fSIdo Schimmel 		.orig_dev = dev,
10641f868398SJiri Pirko 		.id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
1065f8e20a9fSScott Feldman 		.flags = SWITCHDEV_F_NO_RECURSE,
1066f8e20a9fSScott Feldman 	};
106782f28412SJiri Pirko 
1068f8e20a9fSScott Feldman 	err = switchdev_port_attr_get(dev, &attr);
106982f28412SJiri Pirko 	if (err) {
107082f28412SJiri Pirko 		if (err == -EOPNOTSUPP)
107182f28412SJiri Pirko 			return 0;
107282f28412SJiri Pirko 		return err;
107382f28412SJiri Pirko 	}
107482f28412SJiri Pirko 
107542275bd8SScott Feldman 	if (nla_put(skb, IFLA_PHYS_SWITCH_ID, attr.u.ppid.id_len,
107642275bd8SScott Feldman 		    attr.u.ppid.id))
107782f28412SJiri Pirko 		return -EMSGSIZE;
107882f28412SJiri Pirko 
107982f28412SJiri Pirko 	return 0;
108082f28412SJiri Pirko }
108182f28412SJiri Pirko 
1082b22b941bSHannes Frederic Sowa static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1083b22b941bSHannes Frederic Sowa 					      struct net_device *dev)
1084b22b941bSHannes Frederic Sowa {
1085550bce59SRoopa Prabhu 	struct rtnl_link_stats64 *sp;
1086b22b941bSHannes Frederic Sowa 	struct nlattr *attr;
1087b22b941bSHannes Frederic Sowa 
108858414d32SNicolas Dichtel 	attr = nla_reserve_64bit(skb, IFLA_STATS64,
108958414d32SNicolas Dichtel 				 sizeof(struct rtnl_link_stats64), IFLA_PAD);
1090b22b941bSHannes Frederic Sowa 	if (!attr)
1091b22b941bSHannes Frederic Sowa 		return -EMSGSIZE;
1092b22b941bSHannes Frederic Sowa 
1093550bce59SRoopa Prabhu 	sp = nla_data(attr);
1094550bce59SRoopa Prabhu 	dev_get_stats(dev, sp);
1095550bce59SRoopa Prabhu 
1096550bce59SRoopa Prabhu 	attr = nla_reserve(skb, IFLA_STATS,
1097550bce59SRoopa Prabhu 			   sizeof(struct rtnl_link_stats));
1098550bce59SRoopa Prabhu 	if (!attr)
1099550bce59SRoopa Prabhu 		return -EMSGSIZE;
1100550bce59SRoopa Prabhu 
1101550bce59SRoopa Prabhu 	copy_rtnl_link_stats(nla_data(attr), sp);
1102b22b941bSHannes Frederic Sowa 
1103b22b941bSHannes Frederic Sowa 	return 0;
1104b22b941bSHannes Frederic Sowa }
1105b22b941bSHannes Frederic Sowa 
1106b22b941bSHannes Frederic Sowa static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1107b22b941bSHannes Frederic Sowa 					       struct net_device *dev,
1108b22b941bSHannes Frederic Sowa 					       int vfs_num,
1109b22b941bSHannes Frederic Sowa 					       struct nlattr *vfinfo)
1110b22b941bSHannes Frederic Sowa {
1111b22b941bSHannes Frederic Sowa 	struct ifla_vf_rss_query_en vf_rss_query_en;
1112b22b941bSHannes Frederic Sowa 	struct ifla_vf_link_state vf_linkstate;
1113b22b941bSHannes Frederic Sowa 	struct ifla_vf_spoofchk vf_spoofchk;
1114b22b941bSHannes Frederic Sowa 	struct ifla_vf_tx_rate vf_tx_rate;
1115b22b941bSHannes Frederic Sowa 	struct ifla_vf_stats vf_stats;
1116b22b941bSHannes Frederic Sowa 	struct ifla_vf_trust vf_trust;
1117b22b941bSHannes Frederic Sowa 	struct ifla_vf_vlan vf_vlan;
1118b22b941bSHannes Frederic Sowa 	struct ifla_vf_rate vf_rate;
1119b22b941bSHannes Frederic Sowa 	struct nlattr *vf, *vfstats;
1120b22b941bSHannes Frederic Sowa 	struct ifla_vf_mac vf_mac;
1121b22b941bSHannes Frederic Sowa 	struct ifla_vf_info ivi;
1122b22b941bSHannes Frederic Sowa 
1123b22b941bSHannes Frederic Sowa 	/* Not all SR-IOV capable drivers support the
1124b22b941bSHannes Frederic Sowa 	 * spoofcheck and "RSS query enable" query.  Preset to
1125b22b941bSHannes Frederic Sowa 	 * -1 so the user space tool can detect that the driver
1126b22b941bSHannes Frederic Sowa 	 * didn't report anything.
1127b22b941bSHannes Frederic Sowa 	 */
1128b22b941bSHannes Frederic Sowa 	ivi.spoofchk = -1;
1129b22b941bSHannes Frederic Sowa 	ivi.rss_query_en = -1;
1130b22b941bSHannes Frederic Sowa 	ivi.trusted = -1;
1131b22b941bSHannes Frederic Sowa 	memset(ivi.mac, 0, sizeof(ivi.mac));
1132b22b941bSHannes Frederic Sowa 	/* The default value for VF link state is "auto"
1133b22b941bSHannes Frederic Sowa 	 * IFLA_VF_LINK_STATE_AUTO which equals zero
1134b22b941bSHannes Frederic Sowa 	 */
1135b22b941bSHannes Frederic Sowa 	ivi.linkstate = 0;
1136b22b941bSHannes Frederic Sowa 	if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1137b22b941bSHannes Frederic Sowa 		return 0;
1138b22b941bSHannes Frederic Sowa 
1139b22b941bSHannes Frederic Sowa 	vf_mac.vf =
1140b22b941bSHannes Frederic Sowa 		vf_vlan.vf =
1141b22b941bSHannes Frederic Sowa 		vf_rate.vf =
1142b22b941bSHannes Frederic Sowa 		vf_tx_rate.vf =
1143b22b941bSHannes Frederic Sowa 		vf_spoofchk.vf =
1144b22b941bSHannes Frederic Sowa 		vf_linkstate.vf =
1145b22b941bSHannes Frederic Sowa 		vf_rss_query_en.vf =
1146b22b941bSHannes Frederic Sowa 		vf_trust.vf = ivi.vf;
1147b22b941bSHannes Frederic Sowa 
1148b22b941bSHannes Frederic Sowa 	memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1149b22b941bSHannes Frederic Sowa 	vf_vlan.vlan = ivi.vlan;
1150b22b941bSHannes Frederic Sowa 	vf_vlan.qos = ivi.qos;
1151b22b941bSHannes Frederic Sowa 	vf_tx_rate.rate = ivi.max_tx_rate;
1152b22b941bSHannes Frederic Sowa 	vf_rate.min_tx_rate = ivi.min_tx_rate;
1153b22b941bSHannes Frederic Sowa 	vf_rate.max_tx_rate = ivi.max_tx_rate;
1154b22b941bSHannes Frederic Sowa 	vf_spoofchk.setting = ivi.spoofchk;
1155b22b941bSHannes Frederic Sowa 	vf_linkstate.link_state = ivi.linkstate;
1156b22b941bSHannes Frederic Sowa 	vf_rss_query_en.setting = ivi.rss_query_en;
1157b22b941bSHannes Frederic Sowa 	vf_trust.setting = ivi.trusted;
1158b22b941bSHannes Frederic Sowa 	vf = nla_nest_start(skb, IFLA_VF_INFO);
1159b22b941bSHannes Frederic Sowa 	if (!vf) {
1160b22b941bSHannes Frederic Sowa 		nla_nest_cancel(skb, vfinfo);
1161b22b941bSHannes Frederic Sowa 		return -EMSGSIZE;
1162b22b941bSHannes Frederic Sowa 	}
1163b22b941bSHannes Frederic Sowa 	if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1164b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1165b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1166b22b941bSHannes Frederic Sowa 		    &vf_rate) ||
1167b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1168b22b941bSHannes Frederic Sowa 		    &vf_tx_rate) ||
1169b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1170b22b941bSHannes Frederic Sowa 		    &vf_spoofchk) ||
1171b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1172b22b941bSHannes Frederic Sowa 		    &vf_linkstate) ||
1173b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1174b22b941bSHannes Frederic Sowa 		    sizeof(vf_rss_query_en),
1175b22b941bSHannes Frederic Sowa 		    &vf_rss_query_en) ||
1176b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_TRUST,
1177b22b941bSHannes Frederic Sowa 		    sizeof(vf_trust), &vf_trust))
1178b22b941bSHannes Frederic Sowa 		return -EMSGSIZE;
1179b22b941bSHannes Frederic Sowa 	memset(&vf_stats, 0, sizeof(vf_stats));
1180b22b941bSHannes Frederic Sowa 	if (dev->netdev_ops->ndo_get_vf_stats)
1181b22b941bSHannes Frederic Sowa 		dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1182b22b941bSHannes Frederic Sowa 						&vf_stats);
1183b22b941bSHannes Frederic Sowa 	vfstats = nla_nest_start(skb, IFLA_VF_STATS);
1184b22b941bSHannes Frederic Sowa 	if (!vfstats) {
1185b22b941bSHannes Frederic Sowa 		nla_nest_cancel(skb, vf);
1186b22b941bSHannes Frederic Sowa 		nla_nest_cancel(skb, vfinfo);
1187b22b941bSHannes Frederic Sowa 		return -EMSGSIZE;
1188b22b941bSHannes Frederic Sowa 	}
1189343a6d8eSNicolas Dichtel 	if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1190343a6d8eSNicolas Dichtel 			      vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1191343a6d8eSNicolas Dichtel 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1192343a6d8eSNicolas Dichtel 			      vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1193343a6d8eSNicolas Dichtel 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1194343a6d8eSNicolas Dichtel 			      vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1195343a6d8eSNicolas Dichtel 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1196343a6d8eSNicolas Dichtel 			      vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1197343a6d8eSNicolas Dichtel 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1198343a6d8eSNicolas Dichtel 			      vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1199343a6d8eSNicolas Dichtel 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
1200343a6d8eSNicolas Dichtel 			      vf_stats.multicast, IFLA_VF_STATS_PAD))
1201b22b941bSHannes Frederic Sowa 		return -EMSGSIZE;
1202b22b941bSHannes Frederic Sowa 	nla_nest_end(skb, vfstats);
1203b22b941bSHannes Frederic Sowa 	nla_nest_end(skb, vf);
1204b22b941bSHannes Frederic Sowa 	return 0;
1205b22b941bSHannes Frederic Sowa }
1206b22b941bSHannes Frederic Sowa 
1207b22b941bSHannes Frederic Sowa static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1208b22b941bSHannes Frederic Sowa {
12095f8e4474SKangjie Lu 	struct rtnl_link_ifmap map;
12105f8e4474SKangjie Lu 
12115f8e4474SKangjie Lu 	memset(&map, 0, sizeof(map));
12125f8e4474SKangjie Lu 	map.mem_start   = dev->mem_start;
12135f8e4474SKangjie Lu 	map.mem_end     = dev->mem_end;
12145f8e4474SKangjie Lu 	map.base_addr   = dev->base_addr;
12155f8e4474SKangjie Lu 	map.irq         = dev->irq;
12165f8e4474SKangjie Lu 	map.dma         = dev->dma;
12175f8e4474SKangjie Lu 	map.port        = dev->if_port;
12185f8e4474SKangjie Lu 
1219270cb4d0SNicolas Dichtel 	if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
1220b22b941bSHannes Frederic Sowa 		return -EMSGSIZE;
1221b22b941bSHannes Frederic Sowa 
1222b22b941bSHannes Frederic Sowa 	return 0;
1223b22b941bSHannes Frederic Sowa }
1224b22b941bSHannes Frederic Sowa 
1225d1fdd913SBrenden Blanco static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1226d1fdd913SBrenden Blanco {
1227d1fdd913SBrenden Blanco 	struct netdev_xdp xdp_op = {};
1228d1fdd913SBrenden Blanco 	struct nlattr *xdp;
1229d1fdd913SBrenden Blanco 	int err;
1230d1fdd913SBrenden Blanco 
1231d1fdd913SBrenden Blanco 	if (!dev->netdev_ops->ndo_xdp)
1232d1fdd913SBrenden Blanco 		return 0;
1233d1fdd913SBrenden Blanco 	xdp = nla_nest_start(skb, IFLA_XDP);
1234d1fdd913SBrenden Blanco 	if (!xdp)
1235d1fdd913SBrenden Blanco 		return -EMSGSIZE;
1236d1fdd913SBrenden Blanco 	xdp_op.command = XDP_QUERY_PROG;
1237d1fdd913SBrenden Blanco 	err = dev->netdev_ops->ndo_xdp(dev, &xdp_op);
1238d1fdd913SBrenden Blanco 	if (err)
1239d1fdd913SBrenden Blanco 		goto err_cancel;
1240d1fdd913SBrenden Blanco 	err = nla_put_u8(skb, IFLA_XDP_ATTACHED, xdp_op.prog_attached);
1241d1fdd913SBrenden Blanco 	if (err)
1242d1fdd913SBrenden Blanco 		goto err_cancel;
1243d1fdd913SBrenden Blanco 
1244d1fdd913SBrenden Blanco 	nla_nest_end(skb, xdp);
1245d1fdd913SBrenden Blanco 	return 0;
1246d1fdd913SBrenden Blanco 
1247d1fdd913SBrenden Blanco err_cancel:
1248d1fdd913SBrenden Blanco 	nla_nest_cancel(skb, xdp);
1249d1fdd913SBrenden Blanco 	return err;
1250d1fdd913SBrenden Blanco }
1251d1fdd913SBrenden Blanco 
1252b60c5115SThomas Graf static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
1253575c3e2aSPatrick McHardy 			    int type, u32 pid, u32 seq, u32 change,
1254115c9b81SGreg Rose 			    unsigned int flags, u32 ext_filter_mask)
1255b60c5115SThomas Graf {
1256b60c5115SThomas Graf 	struct ifinfomsg *ifm;
12571da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
1258b22b941bSHannes Frederic Sowa 	struct nlattr *af_spec;
1259f8ff182cSThomas Graf 	struct rtnl_af_ops *af_ops;
1260898e5061SJiri Pirko 	struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
12611da177e4SLinus Torvalds 
12622907c35fSEric Dumazet 	ASSERT_RTNL();
1263b60c5115SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1264b60c5115SThomas Graf 	if (nlh == NULL)
126526932566SPatrick McHardy 		return -EMSGSIZE;
12661da177e4SLinus Torvalds 
1267b60c5115SThomas Graf 	ifm = nlmsg_data(nlh);
1268b60c5115SThomas Graf 	ifm->ifi_family = AF_UNSPEC;
1269b60c5115SThomas Graf 	ifm->__ifi_pad = 0;
1270b60c5115SThomas Graf 	ifm->ifi_type = dev->type;
1271b60c5115SThomas Graf 	ifm->ifi_index = dev->ifindex;
1272b60c5115SThomas Graf 	ifm->ifi_flags = dev_get_flags(dev);
1273b60c5115SThomas Graf 	ifm->ifi_change = change;
12741da177e4SLinus Torvalds 
1275a6574349SDavid S. Miller 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1276a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1277a6574349SDavid S. Miller 	    nla_put_u8(skb, IFLA_OPERSTATE,
1278a6574349SDavid S. Miller 		       netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1279a6574349SDavid S. Miller 	    nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1280a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1281a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_GROUP, dev->group) ||
1282edbc0bb3SBen Greear 	    nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
128376ff5cc9SJiri Pirko 	    nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
1284c70ce028SEric Dumazet 	    nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1285c70ce028SEric Dumazet 	    nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
12861d69c2b3SMark A. Greer #ifdef CONFIG_RPS
128776ff5cc9SJiri Pirko 	    nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
12881d69c2b3SMark A. Greer #endif
1289a54acb3aSNicolas Dichtel 	    (dev->ifindex != dev_get_iflink(dev) &&
1290a54acb3aSNicolas Dichtel 	     nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
1291898e5061SJiri Pirko 	    (upper_dev &&
1292898e5061SJiri Pirko 	     nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) ||
12939a57247fSJiri Pirko 	    nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
1294a6574349SDavid S. Miller 	    (dev->qdisc &&
1295a6574349SDavid S. Miller 	     nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
1296a6574349SDavid S. Miller 	    (dev->ifalias &&
12972d3b479dSdavid decotigny 	     nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)) ||
12982d3b479dSdavid decotigny 	    nla_put_u32(skb, IFLA_CARRIER_CHANGES,
129988d6378bSAnuradha Karuppiah 			atomic_read(&dev->carrier_changes)) ||
130088d6378bSAnuradha Karuppiah 	    nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down))
1301a6574349SDavid S. Miller 		goto nla_put_failure;
13020b815a1aSStephen Hemminger 
1303b22b941bSHannes Frederic Sowa 	if (rtnl_fill_link_ifmap(skb, dev))
1304a6574349SDavid S. Miller 		goto nla_put_failure;
13051da177e4SLinus Torvalds 
13061da177e4SLinus Torvalds 	if (dev->addr_len) {
1307a6574349SDavid S. Miller 		if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1308a6574349SDavid S. Miller 		    nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1309a6574349SDavid S. Miller 			goto nla_put_failure;
13101da177e4SLinus Torvalds 	}
13111da177e4SLinus Torvalds 
131266cae9edSJiri Pirko 	if (rtnl_phys_port_id_fill(skb, dev))
131366cae9edSJiri Pirko 		goto nla_put_failure;
131466cae9edSJiri Pirko 
1315db24a904SDavid Ahern 	if (rtnl_phys_port_name_fill(skb, dev))
1316db24a904SDavid Ahern 		goto nla_put_failure;
1317db24a904SDavid Ahern 
131882f28412SJiri Pirko 	if (rtnl_phys_switch_id_fill(skb, dev))
131982f28412SJiri Pirko 		goto nla_put_failure;
132082f28412SJiri Pirko 
1321b22b941bSHannes Frederic Sowa 	if (rtnl_fill_stats(skb, dev))
1322b60c5115SThomas Graf 		goto nla_put_failure;
1323b60c5115SThomas Graf 
1324a6574349SDavid S. Miller 	if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) &&
1325a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent)))
1326a6574349SDavid S. Miller 		goto nla_put_failure;
132757b61080SScott Feldman 
1328b22b941bSHannes Frederic Sowa 	if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent &&
1329b22b941bSHannes Frederic Sowa 	    ext_filter_mask & RTEXT_FILTER_VF) {
1330ebc08a6fSWilliams, Mitch A 		int i;
1331b22b941bSHannes Frederic Sowa 		struct nlattr *vfinfo;
1332c02db8c6SChris Wright 		int num_vfs = dev_num_vf(dev->dev.parent);
1333c02db8c6SChris Wright 
1334c02db8c6SChris Wright 		vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1335c02db8c6SChris Wright 		if (!vfinfo)
1336c02db8c6SChris Wright 			goto nla_put_failure;
1337c02db8c6SChris Wright 		for (i = 0; i < num_vfs; i++) {
1338b22b941bSHannes Frederic Sowa 			if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
1339b22b941bSHannes Frederic Sowa 				goto nla_put_failure;
1340b22b941bSHannes Frederic Sowa 		}
13415f8444a3SGreg Rose 
1342c02db8c6SChris Wright 		nla_nest_end(skb, vfinfo);
1343ebc08a6fSWilliams, Mitch A 	}
134457b61080SScott Feldman 
1345c53864fdSDavid Gibson 	if (rtnl_port_fill(skb, dev, ext_filter_mask))
134657b61080SScott Feldman 		goto nla_put_failure;
134757b61080SScott Feldman 
1348d1fdd913SBrenden Blanco 	if (rtnl_xdp_fill(skb, dev))
1349d1fdd913SBrenden Blanco 		goto nla_put_failure;
1350d1fdd913SBrenden Blanco 
1351ba7d49b1SJiri Pirko 	if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
135238f7b870SPatrick McHardy 		if (rtnl_link_fill(skb, dev) < 0)
135338f7b870SPatrick McHardy 			goto nla_put_failure;
135438f7b870SPatrick McHardy 	}
135538f7b870SPatrick McHardy 
1356d37512a2SNicolas Dichtel 	if (dev->rtnl_link_ops &&
1357d37512a2SNicolas Dichtel 	    dev->rtnl_link_ops->get_link_net) {
1358d37512a2SNicolas Dichtel 		struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1359d37512a2SNicolas Dichtel 
1360d37512a2SNicolas Dichtel 		if (!net_eq(dev_net(dev), link_net)) {
13617a0877d4SNicolas Dichtel 			int id = peernet2id_alloc(dev_net(dev), link_net);
1362d37512a2SNicolas Dichtel 
1363d37512a2SNicolas Dichtel 			if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1364d37512a2SNicolas Dichtel 				goto nla_put_failure;
1365d37512a2SNicolas Dichtel 		}
1366d37512a2SNicolas Dichtel 	}
1367d37512a2SNicolas Dichtel 
1368f8ff182cSThomas Graf 	if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
1369f8ff182cSThomas Graf 		goto nla_put_failure;
1370f8ff182cSThomas Graf 
1371f8ff182cSThomas Graf 	list_for_each_entry(af_ops, &rtnl_af_ops, list) {
1372f8ff182cSThomas Graf 		if (af_ops->fill_link_af) {
1373f8ff182cSThomas Graf 			struct nlattr *af;
1374f8ff182cSThomas Graf 			int err;
1375f8ff182cSThomas Graf 
1376f8ff182cSThomas Graf 			if (!(af = nla_nest_start(skb, af_ops->family)))
1377f8ff182cSThomas Graf 				goto nla_put_failure;
1378f8ff182cSThomas Graf 
1379d5566fd7SSowmini Varadhan 			err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
1380f8ff182cSThomas Graf 
1381f8ff182cSThomas Graf 			/*
1382f8ff182cSThomas Graf 			 * Caller may return ENODATA to indicate that there
1383f8ff182cSThomas Graf 			 * was no data to be dumped. This is not an error, it
1384f8ff182cSThomas Graf 			 * means we should trim the attribute header and
1385f8ff182cSThomas Graf 			 * continue.
1386f8ff182cSThomas Graf 			 */
1387f8ff182cSThomas Graf 			if (err == -ENODATA)
1388f8ff182cSThomas Graf 				nla_nest_cancel(skb, af);
1389f8ff182cSThomas Graf 			else if (err < 0)
1390f8ff182cSThomas Graf 				goto nla_put_failure;
1391f8ff182cSThomas Graf 
1392f8ff182cSThomas Graf 			nla_nest_end(skb, af);
1393f8ff182cSThomas Graf 		}
1394f8ff182cSThomas Graf 	}
1395f8ff182cSThomas Graf 
1396f8ff182cSThomas Graf 	nla_nest_end(skb, af_spec);
1397f8ff182cSThomas Graf 
1398053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
1399053c095aSJohannes Berg 	return 0;
1400b60c5115SThomas Graf 
1401b60c5115SThomas Graf nla_put_failure:
140226932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
140326932566SPatrick McHardy 	return -EMSGSIZE;
14041da177e4SLinus Torvalds }
14051da177e4SLinus Torvalds 
1406f7b12606SJiri Pirko static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1407f7b12606SJiri Pirko 	[IFLA_IFNAME]		= { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1408f7b12606SJiri Pirko 	[IFLA_ADDRESS]		= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1409f7b12606SJiri Pirko 	[IFLA_BROADCAST]	= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1410f7b12606SJiri Pirko 	[IFLA_MAP]		= { .len = sizeof(struct rtnl_link_ifmap) },
1411f7b12606SJiri Pirko 	[IFLA_MTU]		= { .type = NLA_U32 },
1412f7b12606SJiri Pirko 	[IFLA_LINK]		= { .type = NLA_U32 },
1413f7b12606SJiri Pirko 	[IFLA_MASTER]		= { .type = NLA_U32 },
1414f7b12606SJiri Pirko 	[IFLA_CARRIER]		= { .type = NLA_U8 },
1415f7b12606SJiri Pirko 	[IFLA_TXQLEN]		= { .type = NLA_U32 },
1416f7b12606SJiri Pirko 	[IFLA_WEIGHT]		= { .type = NLA_U32 },
1417f7b12606SJiri Pirko 	[IFLA_OPERSTATE]	= { .type = NLA_U8 },
1418f7b12606SJiri Pirko 	[IFLA_LINKMODE]		= { .type = NLA_U8 },
1419f7b12606SJiri Pirko 	[IFLA_LINKINFO]		= { .type = NLA_NESTED },
1420f7b12606SJiri Pirko 	[IFLA_NET_NS_PID]	= { .type = NLA_U32 },
1421f7b12606SJiri Pirko 	[IFLA_NET_NS_FD]	= { .type = NLA_U32 },
1422f7b12606SJiri Pirko 	[IFLA_IFALIAS]	        = { .type = NLA_STRING, .len = IFALIASZ-1 },
1423f7b12606SJiri Pirko 	[IFLA_VFINFO_LIST]	= {. type = NLA_NESTED },
1424f7b12606SJiri Pirko 	[IFLA_VF_PORTS]		= { .type = NLA_NESTED },
1425f7b12606SJiri Pirko 	[IFLA_PORT_SELF]	= { .type = NLA_NESTED },
1426f7b12606SJiri Pirko 	[IFLA_AF_SPEC]		= { .type = NLA_NESTED },
1427f7b12606SJiri Pirko 	[IFLA_EXT_MASK]		= { .type = NLA_U32 },
1428f7b12606SJiri Pirko 	[IFLA_PROMISCUITY]	= { .type = NLA_U32 },
1429f7b12606SJiri Pirko 	[IFLA_NUM_TX_QUEUES]	= { .type = NLA_U32 },
1430f7b12606SJiri Pirko 	[IFLA_NUM_RX_QUEUES]	= { .type = NLA_U32 },
143102637fceSJiri Pirko 	[IFLA_PHYS_PORT_ID]	= { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
14322d3b479dSdavid decotigny 	[IFLA_CARRIER_CHANGES]	= { .type = NLA_U32 },  /* ignored */
143382f28412SJiri Pirko 	[IFLA_PHYS_SWITCH_ID]	= { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1434317f4810SNicolas Dichtel 	[IFLA_LINK_NETNSID]	= { .type = NLA_S32 },
143588d6378bSAnuradha Karuppiah 	[IFLA_PROTO_DOWN]	= { .type = NLA_U8 },
1436d1fdd913SBrenden Blanco 	[IFLA_XDP]		= { .type = NLA_NESTED },
1437f7b12606SJiri Pirko };
1438f7b12606SJiri Pirko 
1439f7b12606SJiri Pirko static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1440f7b12606SJiri Pirko 	[IFLA_INFO_KIND]	= { .type = NLA_STRING },
1441f7b12606SJiri Pirko 	[IFLA_INFO_DATA]	= { .type = NLA_NESTED },
1442f7b12606SJiri Pirko 	[IFLA_INFO_SLAVE_KIND]	= { .type = NLA_STRING },
1443f7b12606SJiri Pirko 	[IFLA_INFO_SLAVE_DATA]	= { .type = NLA_NESTED },
1444f7b12606SJiri Pirko };
1445f7b12606SJiri Pirko 
1446f7b12606SJiri Pirko static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1447364d5716SDaniel Borkmann 	[IFLA_VF_MAC]		= { .len = sizeof(struct ifla_vf_mac) },
1448364d5716SDaniel Borkmann 	[IFLA_VF_VLAN]		= { .len = sizeof(struct ifla_vf_vlan) },
1449364d5716SDaniel Borkmann 	[IFLA_VF_TX_RATE]	= { .len = sizeof(struct ifla_vf_tx_rate) },
1450364d5716SDaniel Borkmann 	[IFLA_VF_SPOOFCHK]	= { .len = sizeof(struct ifla_vf_spoofchk) },
1451364d5716SDaniel Borkmann 	[IFLA_VF_RATE]		= { .len = sizeof(struct ifla_vf_rate) },
1452364d5716SDaniel Borkmann 	[IFLA_VF_LINK_STATE]	= { .len = sizeof(struct ifla_vf_link_state) },
145301a3d796SVlad Zolotarov 	[IFLA_VF_RSS_QUERY_EN]	= { .len = sizeof(struct ifla_vf_rss_query_en) },
14543b766cd8SEran Ben Elisha 	[IFLA_VF_STATS]		= { .type = NLA_NESTED },
1455dd461d6aSHiroshi Shimamoto 	[IFLA_VF_TRUST]		= { .len = sizeof(struct ifla_vf_trust) },
1456cc8e27ccSEli Cohen 	[IFLA_VF_IB_NODE_GUID]	= { .len = sizeof(struct ifla_vf_guid) },
1457cc8e27ccSEli Cohen 	[IFLA_VF_IB_PORT_GUID]	= { .len = sizeof(struct ifla_vf_guid) },
14583b766cd8SEran Ben Elisha };
14593b766cd8SEran Ben Elisha 
1460f7b12606SJiri Pirko static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1461f7b12606SJiri Pirko 	[IFLA_PORT_VF]		= { .type = NLA_U32 },
1462f7b12606SJiri Pirko 	[IFLA_PORT_PROFILE]	= { .type = NLA_STRING,
1463f7b12606SJiri Pirko 				    .len = PORT_PROFILE_MAX },
1464f7b12606SJiri Pirko 	[IFLA_PORT_VSI_TYPE]	= { .type = NLA_BINARY,
1465f7b12606SJiri Pirko 				    .len = sizeof(struct ifla_port_vsi)},
1466f7b12606SJiri Pirko 	[IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1467f7b12606SJiri Pirko 				      .len = PORT_UUID_MAX },
1468f7b12606SJiri Pirko 	[IFLA_PORT_HOST_UUID]	= { .type = NLA_STRING,
1469f7b12606SJiri Pirko 				    .len = PORT_UUID_MAX },
1470f7b12606SJiri Pirko 	[IFLA_PORT_REQUEST]	= { .type = NLA_U8, },
1471f7b12606SJiri Pirko 	[IFLA_PORT_RESPONSE]	= { .type = NLA_U16, },
1472f7b12606SJiri Pirko };
1473f7b12606SJiri Pirko 
1474d1fdd913SBrenden Blanco static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
1475d1fdd913SBrenden Blanco 	[IFLA_XDP_FD]		= { .type = NLA_S32 },
1476d1fdd913SBrenden Blanco 	[IFLA_XDP_ATTACHED]	= { .type = NLA_U8 },
1477d1fdd913SBrenden Blanco };
1478d1fdd913SBrenden Blanco 
1479dc599f76SDavid Ahern static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1480dc599f76SDavid Ahern {
1481dc599f76SDavid Ahern 	const struct rtnl_link_ops *ops = NULL;
1482dc599f76SDavid Ahern 	struct nlattr *linfo[IFLA_INFO_MAX + 1];
1483dc599f76SDavid Ahern 
1484dc599f76SDavid Ahern 	if (nla_parse_nested(linfo, IFLA_INFO_MAX, nla, ifla_info_policy) < 0)
1485dc599f76SDavid Ahern 		return NULL;
1486dc599f76SDavid Ahern 
1487dc599f76SDavid Ahern 	if (linfo[IFLA_INFO_KIND]) {
1488dc599f76SDavid Ahern 		char kind[MODULE_NAME_LEN];
1489dc599f76SDavid Ahern 
1490dc599f76SDavid Ahern 		nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1491dc599f76SDavid Ahern 		ops = rtnl_link_ops_get(kind);
1492dc599f76SDavid Ahern 	}
1493dc599f76SDavid Ahern 
1494dc599f76SDavid Ahern 	return ops;
1495dc599f76SDavid Ahern }
1496dc599f76SDavid Ahern 
1497dc599f76SDavid Ahern static bool link_master_filtered(struct net_device *dev, int master_idx)
1498dc599f76SDavid Ahern {
1499dc599f76SDavid Ahern 	struct net_device *master;
1500dc599f76SDavid Ahern 
1501dc599f76SDavid Ahern 	if (!master_idx)
1502dc599f76SDavid Ahern 		return false;
1503dc599f76SDavid Ahern 
1504dc599f76SDavid Ahern 	master = netdev_master_upper_dev_get(dev);
1505dc599f76SDavid Ahern 	if (!master || master->ifindex != master_idx)
1506dc599f76SDavid Ahern 		return true;
1507dc599f76SDavid Ahern 
1508dc599f76SDavid Ahern 	return false;
1509dc599f76SDavid Ahern }
1510dc599f76SDavid Ahern 
1511dc599f76SDavid Ahern static bool link_kind_filtered(const struct net_device *dev,
1512dc599f76SDavid Ahern 			       const struct rtnl_link_ops *kind_ops)
1513dc599f76SDavid Ahern {
1514dc599f76SDavid Ahern 	if (kind_ops && dev->rtnl_link_ops != kind_ops)
1515dc599f76SDavid Ahern 		return true;
1516dc599f76SDavid Ahern 
1517dc599f76SDavid Ahern 	return false;
1518dc599f76SDavid Ahern }
1519dc599f76SDavid Ahern 
1520dc599f76SDavid Ahern static bool link_dump_filtered(struct net_device *dev,
1521dc599f76SDavid Ahern 			       int master_idx,
1522dc599f76SDavid Ahern 			       const struct rtnl_link_ops *kind_ops)
1523dc599f76SDavid Ahern {
1524dc599f76SDavid Ahern 	if (link_master_filtered(dev, master_idx) ||
1525dc599f76SDavid Ahern 	    link_kind_filtered(dev, kind_ops))
1526dc599f76SDavid Ahern 		return true;
1527dc599f76SDavid Ahern 
1528dc599f76SDavid Ahern 	return false;
1529dc599f76SDavid Ahern }
1530dc599f76SDavid Ahern 
1531b60c5115SThomas Graf static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
15321da177e4SLinus Torvalds {
15333b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
15347c28bd0bSEric Dumazet 	int h, s_h;
15357c28bd0bSEric Dumazet 	int idx = 0, s_idx;
15361da177e4SLinus Torvalds 	struct net_device *dev;
15377c28bd0bSEric Dumazet 	struct hlist_head *head;
1538115c9b81SGreg Rose 	struct nlattr *tb[IFLA_MAX+1];
1539115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
1540dc599f76SDavid Ahern 	const struct rtnl_link_ops *kind_ops = NULL;
1541dc599f76SDavid Ahern 	unsigned int flags = NLM_F_MULTI;
1542dc599f76SDavid Ahern 	int master_idx = 0;
1543973462bbSDavid Gibson 	int err;
1544e5eca6d4SMichal Schmidt 	int hdrlen;
15451da177e4SLinus Torvalds 
15467c28bd0bSEric Dumazet 	s_h = cb->args[0];
15477c28bd0bSEric Dumazet 	s_idx = cb->args[1];
15487c28bd0bSEric Dumazet 
15494e985adaSThomas Graf 	cb->seq = net->dev_base_seq;
15504e985adaSThomas Graf 
1551e5eca6d4SMichal Schmidt 	/* A hack to preserve kernel<->userspace interface.
1552e5eca6d4SMichal Schmidt 	 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1553e5eca6d4SMichal Schmidt 	 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1554e5eca6d4SMichal Schmidt 	 * what iproute2 < v3.9.0 used.
1555e5eca6d4SMichal Schmidt 	 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1556e5eca6d4SMichal Schmidt 	 * attribute, its netlink message is shorter than struct ifinfomsg.
1557e5eca6d4SMichal Schmidt 	 */
1558e5eca6d4SMichal Schmidt 	hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ?
1559e5eca6d4SMichal Schmidt 		 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1560e5eca6d4SMichal Schmidt 
1561e5eca6d4SMichal Schmidt 	if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
1562115c9b81SGreg Rose 
1563115c9b81SGreg Rose 		if (tb[IFLA_EXT_MASK])
1564115c9b81SGreg Rose 			ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1565dc599f76SDavid Ahern 
1566dc599f76SDavid Ahern 		if (tb[IFLA_MASTER])
1567dc599f76SDavid Ahern 			master_idx = nla_get_u32(tb[IFLA_MASTER]);
1568dc599f76SDavid Ahern 
1569dc599f76SDavid Ahern 		if (tb[IFLA_LINKINFO])
1570dc599f76SDavid Ahern 			kind_ops = linkinfo_to_kind_ops(tb[IFLA_LINKINFO]);
1571dc599f76SDavid Ahern 
1572dc599f76SDavid Ahern 		if (master_idx || kind_ops)
1573dc599f76SDavid Ahern 			flags |= NLM_F_DUMP_FILTERED;
1574a4b64fbeSEric Dumazet 	}
1575115c9b81SGreg Rose 
15767c28bd0bSEric Dumazet 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
15777562f876SPavel Emelianov 		idx = 0;
15787c28bd0bSEric Dumazet 		head = &net->dev_index_head[h];
1579cac5e65eSEric Dumazet 		hlist_for_each_entry(dev, head, index_hlist) {
1580dc599f76SDavid Ahern 			if (link_dump_filtered(dev, master_idx, kind_ops))
1581dc599f76SDavid Ahern 				continue;
15821da177e4SLinus Torvalds 			if (idx < s_idx)
15837562f876SPavel Emelianov 				goto cont;
1584973462bbSDavid Gibson 			err = rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
158515e47304SEric W. Biederman 					       NETLINK_CB(cb->skb).portid,
15867c28bd0bSEric Dumazet 					       cb->nlh->nlmsg_seq, 0,
1587dc599f76SDavid Ahern 					       flags,
1588973462bbSDavid Gibson 					       ext_filter_mask);
1589973462bbSDavid Gibson 			/* If we ran out of room on the first message,
1590973462bbSDavid Gibson 			 * we're in trouble
1591973462bbSDavid Gibson 			 */
1592973462bbSDavid Gibson 			WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
1593973462bbSDavid Gibson 
15947b46a644SDavid S. Miller 			if (err < 0)
15957c28bd0bSEric Dumazet 				goto out;
15964e985adaSThomas Graf 
15974e985adaSThomas Graf 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
15987562f876SPavel Emelianov cont:
15997562f876SPavel Emelianov 			idx++;
16001da177e4SLinus Torvalds 		}
16017c28bd0bSEric Dumazet 	}
16027c28bd0bSEric Dumazet out:
16037c28bd0bSEric Dumazet 	cb->args[1] = idx;
16047c28bd0bSEric Dumazet 	cb->args[0] = h;
16051da177e4SLinus Torvalds 
16061da177e4SLinus Torvalds 	return skb->len;
16071da177e4SLinus Torvalds }
16081da177e4SLinus Torvalds 
1609f7b12606SJiri Pirko int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len)
1610f7b12606SJiri Pirko {
1611f7b12606SJiri Pirko 	return nla_parse(tb, IFLA_MAX, head, len, ifla_policy);
1612f7b12606SJiri Pirko }
1613f7b12606SJiri Pirko EXPORT_SYMBOL(rtnl_nla_parse_ifla);
161457b61080SScott Feldman 
161581adee47SEric W. Biederman struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
161681adee47SEric W. Biederman {
161781adee47SEric W. Biederman 	struct net *net;
161881adee47SEric W. Biederman 	/* Examine the link attributes and figure out which
161981adee47SEric W. Biederman 	 * network namespace we are talking about.
162081adee47SEric W. Biederman 	 */
162181adee47SEric W. Biederman 	if (tb[IFLA_NET_NS_PID])
162281adee47SEric W. Biederman 		net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
1623f0630529SEric W. Biederman 	else if (tb[IFLA_NET_NS_FD])
1624f0630529SEric W. Biederman 		net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
162581adee47SEric W. Biederman 	else
162681adee47SEric W. Biederman 		net = get_net(src_net);
162781adee47SEric W. Biederman 	return net;
162881adee47SEric W. Biederman }
162981adee47SEric W. Biederman EXPORT_SYMBOL(rtnl_link_get_net);
163081adee47SEric W. Biederman 
16311840bb13SThomas Graf static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
16321840bb13SThomas Graf {
16331840bb13SThomas Graf 	if (dev) {
16341840bb13SThomas Graf 		if (tb[IFLA_ADDRESS] &&
16351840bb13SThomas Graf 		    nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
16361840bb13SThomas Graf 			return -EINVAL;
16371840bb13SThomas Graf 
16381840bb13SThomas Graf 		if (tb[IFLA_BROADCAST] &&
16391840bb13SThomas Graf 		    nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
16401840bb13SThomas Graf 			return -EINVAL;
16411840bb13SThomas Graf 	}
16421840bb13SThomas Graf 
1643cf7afbfeSThomas Graf 	if (tb[IFLA_AF_SPEC]) {
1644cf7afbfeSThomas Graf 		struct nlattr *af;
1645cf7afbfeSThomas Graf 		int rem, err;
1646cf7afbfeSThomas Graf 
1647cf7afbfeSThomas Graf 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1648cf7afbfeSThomas Graf 			const struct rtnl_af_ops *af_ops;
1649cf7afbfeSThomas Graf 
1650cf7afbfeSThomas Graf 			if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1651cf7afbfeSThomas Graf 				return -EAFNOSUPPORT;
1652cf7afbfeSThomas Graf 
1653cf7afbfeSThomas Graf 			if (!af_ops->set_link_af)
1654cf7afbfeSThomas Graf 				return -EOPNOTSUPP;
1655cf7afbfeSThomas Graf 
1656cf7afbfeSThomas Graf 			if (af_ops->validate_link_af) {
16576d3a9a68SKurt Van Dijck 				err = af_ops->validate_link_af(dev, af);
1658cf7afbfeSThomas Graf 				if (err < 0)
1659cf7afbfeSThomas Graf 					return err;
1660cf7afbfeSThomas Graf 			}
1661cf7afbfeSThomas Graf 		}
1662cf7afbfeSThomas Graf 	}
1663cf7afbfeSThomas Graf 
16641840bb13SThomas Graf 	return 0;
16651840bb13SThomas Graf }
16661840bb13SThomas Graf 
1667cc8e27ccSEli Cohen static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
1668cc8e27ccSEli Cohen 				  int guid_type)
1669cc8e27ccSEli Cohen {
1670cc8e27ccSEli Cohen 	const struct net_device_ops *ops = dev->netdev_ops;
1671cc8e27ccSEli Cohen 
1672cc8e27ccSEli Cohen 	return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
1673cc8e27ccSEli Cohen }
1674cc8e27ccSEli Cohen 
1675cc8e27ccSEli Cohen static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
1676cc8e27ccSEli Cohen {
1677cc8e27ccSEli Cohen 	if (dev->type != ARPHRD_INFINIBAND)
1678cc8e27ccSEli Cohen 		return -EOPNOTSUPP;
1679cc8e27ccSEli Cohen 
1680cc8e27ccSEli Cohen 	return handle_infiniband_guid(dev, ivt, guid_type);
1681cc8e27ccSEli Cohen }
1682cc8e27ccSEli Cohen 
16834f7d2cdfSDaniel Borkmann static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
1684c02db8c6SChris Wright {
1685c02db8c6SChris Wright 	const struct net_device_ops *ops = dev->netdev_ops;
16864f7d2cdfSDaniel Borkmann 	int err = -EINVAL;
1687c02db8c6SChris Wright 
16884f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_MAC]) {
16894f7d2cdfSDaniel Borkmann 		struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
16904f7d2cdfSDaniel Borkmann 
1691c02db8c6SChris Wright 		err = -EOPNOTSUPP;
1692c02db8c6SChris Wright 		if (ops->ndo_set_vf_mac)
1693c02db8c6SChris Wright 			err = ops->ndo_set_vf_mac(dev, ivm->vf,
1694c02db8c6SChris Wright 						  ivm->mac);
16954f7d2cdfSDaniel Borkmann 		if (err < 0)
16964f7d2cdfSDaniel Borkmann 			return err;
1697c02db8c6SChris Wright 	}
16984f7d2cdfSDaniel Borkmann 
16994f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_VLAN]) {
17004f7d2cdfSDaniel Borkmann 		struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
17014f7d2cdfSDaniel Borkmann 
1702c02db8c6SChris Wright 		err = -EOPNOTSUPP;
1703c02db8c6SChris Wright 		if (ops->ndo_set_vf_vlan)
17044f7d2cdfSDaniel Borkmann 			err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
1705c02db8c6SChris Wright 						   ivv->qos);
17064f7d2cdfSDaniel Borkmann 		if (err < 0)
17074f7d2cdfSDaniel Borkmann 			return err;
1708c02db8c6SChris Wright 	}
17094f7d2cdfSDaniel Borkmann 
17104f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_TX_RATE]) {
17114f7d2cdfSDaniel Borkmann 		struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
1712ed616689SSucheta Chakraborty 		struct ifla_vf_info ivf;
17134f7d2cdfSDaniel Borkmann 
1714c02db8c6SChris Wright 		err = -EOPNOTSUPP;
1715ed616689SSucheta Chakraborty 		if (ops->ndo_get_vf_config)
17164f7d2cdfSDaniel Borkmann 			err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
17174f7d2cdfSDaniel Borkmann 		if (err < 0)
17184f7d2cdfSDaniel Borkmann 			return err;
17194f7d2cdfSDaniel Borkmann 
1720ed616689SSucheta Chakraborty 		err = -EOPNOTSUPP;
1721ed616689SSucheta Chakraborty 		if (ops->ndo_set_vf_rate)
1722ed616689SSucheta Chakraborty 			err = ops->ndo_set_vf_rate(dev, ivt->vf,
1723ed616689SSucheta Chakraborty 						   ivf.min_tx_rate,
1724c02db8c6SChris Wright 						   ivt->rate);
17254f7d2cdfSDaniel Borkmann 		if (err < 0)
17264f7d2cdfSDaniel Borkmann 			return err;
1727c02db8c6SChris Wright 	}
17284f7d2cdfSDaniel Borkmann 
17294f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_RATE]) {
17304f7d2cdfSDaniel Borkmann 		struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
17314f7d2cdfSDaniel Borkmann 
1732ed616689SSucheta Chakraborty 		err = -EOPNOTSUPP;
1733ed616689SSucheta Chakraborty 		if (ops->ndo_set_vf_rate)
1734ed616689SSucheta Chakraborty 			err = ops->ndo_set_vf_rate(dev, ivt->vf,
1735ed616689SSucheta Chakraborty 						   ivt->min_tx_rate,
1736ed616689SSucheta Chakraborty 						   ivt->max_tx_rate);
17374f7d2cdfSDaniel Borkmann 		if (err < 0)
17384f7d2cdfSDaniel Borkmann 			return err;
1739ed616689SSucheta Chakraborty 	}
17404f7d2cdfSDaniel Borkmann 
17414f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_SPOOFCHK]) {
17424f7d2cdfSDaniel Borkmann 		struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
17434f7d2cdfSDaniel Borkmann 
17445f8444a3SGreg Rose 		err = -EOPNOTSUPP;
17455f8444a3SGreg Rose 		if (ops->ndo_set_vf_spoofchk)
17465f8444a3SGreg Rose 			err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
17475f8444a3SGreg Rose 						       ivs->setting);
17484f7d2cdfSDaniel Borkmann 		if (err < 0)
17494f7d2cdfSDaniel Borkmann 			return err;
17505f8444a3SGreg Rose 	}
17514f7d2cdfSDaniel Borkmann 
17524f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_LINK_STATE]) {
17534f7d2cdfSDaniel Borkmann 		struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
17544f7d2cdfSDaniel Borkmann 
17551d8faf48SRony Efraim 		err = -EOPNOTSUPP;
17561d8faf48SRony Efraim 		if (ops->ndo_set_vf_link_state)
17571d8faf48SRony Efraim 			err = ops->ndo_set_vf_link_state(dev, ivl->vf,
17581d8faf48SRony Efraim 							 ivl->link_state);
17594f7d2cdfSDaniel Borkmann 		if (err < 0)
17604f7d2cdfSDaniel Borkmann 			return err;
17611d8faf48SRony Efraim 	}
17624f7d2cdfSDaniel Borkmann 
17634f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_RSS_QUERY_EN]) {
176401a3d796SVlad Zolotarov 		struct ifla_vf_rss_query_en *ivrssq_en;
176501a3d796SVlad Zolotarov 
176601a3d796SVlad Zolotarov 		err = -EOPNOTSUPP;
17674f7d2cdfSDaniel Borkmann 		ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
176801a3d796SVlad Zolotarov 		if (ops->ndo_set_vf_rss_query_en)
17694f7d2cdfSDaniel Borkmann 			err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
177001a3d796SVlad Zolotarov 							   ivrssq_en->setting);
17714f7d2cdfSDaniel Borkmann 		if (err < 0)
17724f7d2cdfSDaniel Borkmann 			return err;
177301a3d796SVlad Zolotarov 	}
17744f7d2cdfSDaniel Borkmann 
1775dd461d6aSHiroshi Shimamoto 	if (tb[IFLA_VF_TRUST]) {
1776dd461d6aSHiroshi Shimamoto 		struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
1777dd461d6aSHiroshi Shimamoto 
1778dd461d6aSHiroshi Shimamoto 		err = -EOPNOTSUPP;
1779dd461d6aSHiroshi Shimamoto 		if (ops->ndo_set_vf_trust)
1780dd461d6aSHiroshi Shimamoto 			err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
1781dd461d6aSHiroshi Shimamoto 		if (err < 0)
1782dd461d6aSHiroshi Shimamoto 			return err;
1783dd461d6aSHiroshi Shimamoto 	}
1784dd461d6aSHiroshi Shimamoto 
1785cc8e27ccSEli Cohen 	if (tb[IFLA_VF_IB_NODE_GUID]) {
1786cc8e27ccSEli Cohen 		struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
1787cc8e27ccSEli Cohen 
1788cc8e27ccSEli Cohen 		if (!ops->ndo_set_vf_guid)
1789cc8e27ccSEli Cohen 			return -EOPNOTSUPP;
1790cc8e27ccSEli Cohen 
1791cc8e27ccSEli Cohen 		return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
1792cc8e27ccSEli Cohen 	}
1793cc8e27ccSEli Cohen 
1794cc8e27ccSEli Cohen 	if (tb[IFLA_VF_IB_PORT_GUID]) {
1795cc8e27ccSEli Cohen 		struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
1796cc8e27ccSEli Cohen 
1797cc8e27ccSEli Cohen 		if (!ops->ndo_set_vf_guid)
1798cc8e27ccSEli Cohen 			return -EOPNOTSUPP;
1799cc8e27ccSEli Cohen 
1800cc8e27ccSEli Cohen 		return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
1801cc8e27ccSEli Cohen 	}
1802cc8e27ccSEli Cohen 
1803c02db8c6SChris Wright 	return err;
1804c02db8c6SChris Wright }
1805c02db8c6SChris Wright 
1806fbaec0eaSJiri Pirko static int do_set_master(struct net_device *dev, int ifindex)
1807fbaec0eaSJiri Pirko {
1808898e5061SJiri Pirko 	struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
1809fbaec0eaSJiri Pirko 	const struct net_device_ops *ops;
1810fbaec0eaSJiri Pirko 	int err;
1811fbaec0eaSJiri Pirko 
1812898e5061SJiri Pirko 	if (upper_dev) {
1813898e5061SJiri Pirko 		if (upper_dev->ifindex == ifindex)
1814fbaec0eaSJiri Pirko 			return 0;
1815898e5061SJiri Pirko 		ops = upper_dev->netdev_ops;
1816fbaec0eaSJiri Pirko 		if (ops->ndo_del_slave) {
1817898e5061SJiri Pirko 			err = ops->ndo_del_slave(upper_dev, dev);
1818fbaec0eaSJiri Pirko 			if (err)
1819fbaec0eaSJiri Pirko 				return err;
1820fbaec0eaSJiri Pirko 		} else {
1821fbaec0eaSJiri Pirko 			return -EOPNOTSUPP;
1822fbaec0eaSJiri Pirko 		}
1823fbaec0eaSJiri Pirko 	}
1824fbaec0eaSJiri Pirko 
1825fbaec0eaSJiri Pirko 	if (ifindex) {
1826898e5061SJiri Pirko 		upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
1827898e5061SJiri Pirko 		if (!upper_dev)
1828fbaec0eaSJiri Pirko 			return -EINVAL;
1829898e5061SJiri Pirko 		ops = upper_dev->netdev_ops;
1830fbaec0eaSJiri Pirko 		if (ops->ndo_add_slave) {
1831898e5061SJiri Pirko 			err = ops->ndo_add_slave(upper_dev, dev);
1832fbaec0eaSJiri Pirko 			if (err)
1833fbaec0eaSJiri Pirko 				return err;
1834fbaec0eaSJiri Pirko 		} else {
1835fbaec0eaSJiri Pirko 			return -EOPNOTSUPP;
1836fbaec0eaSJiri Pirko 		}
1837fbaec0eaSJiri Pirko 	}
1838fbaec0eaSJiri Pirko 	return 0;
1839fbaec0eaSJiri Pirko }
1840fbaec0eaSJiri Pirko 
184190c325e3SNicolas Dichtel #define DO_SETLINK_MODIFIED	0x01
1842ba998906SNicolas Dichtel /* notify flag means notify + modified. */
1843ba998906SNicolas Dichtel #define DO_SETLINK_NOTIFY	0x03
184490f62cf3SEric W. Biederman static int do_setlink(const struct sk_buff *skb,
184590f62cf3SEric W. Biederman 		      struct net_device *dev, struct ifinfomsg *ifm,
184690c325e3SNicolas Dichtel 		      struct nlattr **tb, char *ifname, int status)
18470157f60cSPatrick McHardy {
1848d314774cSStephen Hemminger 	const struct net_device_ops *ops = dev->netdev_ops;
18490157f60cSPatrick McHardy 	int err;
18500157f60cSPatrick McHardy 
1851f0630529SEric W. Biederman 	if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
185281adee47SEric W. Biederman 		struct net *net = rtnl_link_get_net(dev_net(dev), tb);
1853d8a5ec67SEric W. Biederman 		if (IS_ERR(net)) {
1854d8a5ec67SEric W. Biederman 			err = PTR_ERR(net);
1855d8a5ec67SEric W. Biederman 			goto errout;
1856d8a5ec67SEric W. Biederman 		}
185790f62cf3SEric W. Biederman 		if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
1858e0ebde0eSNicolas Dichtel 			put_net(net);
1859b51642f6SEric W. Biederman 			err = -EPERM;
1860b51642f6SEric W. Biederman 			goto errout;
1861b51642f6SEric W. Biederman 		}
1862d8a5ec67SEric W. Biederman 		err = dev_change_net_namespace(dev, net, ifname);
1863d8a5ec67SEric W. Biederman 		put_net(net);
1864d8a5ec67SEric W. Biederman 		if (err)
1865d8a5ec67SEric W. Biederman 			goto errout;
186690c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
1867d8a5ec67SEric W. Biederman 	}
1868d8a5ec67SEric W. Biederman 
18690157f60cSPatrick McHardy 	if (tb[IFLA_MAP]) {
18700157f60cSPatrick McHardy 		struct rtnl_link_ifmap *u_map;
18710157f60cSPatrick McHardy 		struct ifmap k_map;
18720157f60cSPatrick McHardy 
1873d314774cSStephen Hemminger 		if (!ops->ndo_set_config) {
18740157f60cSPatrick McHardy 			err = -EOPNOTSUPP;
18750157f60cSPatrick McHardy 			goto errout;
18760157f60cSPatrick McHardy 		}
18770157f60cSPatrick McHardy 
18780157f60cSPatrick McHardy 		if (!netif_device_present(dev)) {
18790157f60cSPatrick McHardy 			err = -ENODEV;
18800157f60cSPatrick McHardy 			goto errout;
18810157f60cSPatrick McHardy 		}
18820157f60cSPatrick McHardy 
18830157f60cSPatrick McHardy 		u_map = nla_data(tb[IFLA_MAP]);
18840157f60cSPatrick McHardy 		k_map.mem_start = (unsigned long) u_map->mem_start;
18850157f60cSPatrick McHardy 		k_map.mem_end = (unsigned long) u_map->mem_end;
18860157f60cSPatrick McHardy 		k_map.base_addr = (unsigned short) u_map->base_addr;
18870157f60cSPatrick McHardy 		k_map.irq = (unsigned char) u_map->irq;
18880157f60cSPatrick McHardy 		k_map.dma = (unsigned char) u_map->dma;
18890157f60cSPatrick McHardy 		k_map.port = (unsigned char) u_map->port;
18900157f60cSPatrick McHardy 
1891d314774cSStephen Hemminger 		err = ops->ndo_set_config(dev, &k_map);
18920157f60cSPatrick McHardy 		if (err < 0)
18930157f60cSPatrick McHardy 			goto errout;
18940157f60cSPatrick McHardy 
1895ba998906SNicolas Dichtel 		status |= DO_SETLINK_NOTIFY;
18960157f60cSPatrick McHardy 	}
18970157f60cSPatrick McHardy 
18980157f60cSPatrick McHardy 	if (tb[IFLA_ADDRESS]) {
18990157f60cSPatrick McHardy 		struct sockaddr *sa;
19000157f60cSPatrick McHardy 		int len;
19010157f60cSPatrick McHardy 
19020157f60cSPatrick McHardy 		len = sizeof(sa_family_t) + dev->addr_len;
19030157f60cSPatrick McHardy 		sa = kmalloc(len, GFP_KERNEL);
19040157f60cSPatrick McHardy 		if (!sa) {
19050157f60cSPatrick McHardy 			err = -ENOMEM;
19060157f60cSPatrick McHardy 			goto errout;
19070157f60cSPatrick McHardy 		}
19080157f60cSPatrick McHardy 		sa->sa_family = dev->type;
19090157f60cSPatrick McHardy 		memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
19100157f60cSPatrick McHardy 		       dev->addr_len);
1911e7c3273eSJiri Pirko 		err = dev_set_mac_address(dev, sa);
19120157f60cSPatrick McHardy 		kfree(sa);
19130157f60cSPatrick McHardy 		if (err)
19140157f60cSPatrick McHardy 			goto errout;
191590c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
19160157f60cSPatrick McHardy 	}
19170157f60cSPatrick McHardy 
19180157f60cSPatrick McHardy 	if (tb[IFLA_MTU]) {
19190157f60cSPatrick McHardy 		err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
19200157f60cSPatrick McHardy 		if (err < 0)
19210157f60cSPatrick McHardy 			goto errout;
192290c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
19230157f60cSPatrick McHardy 	}
19240157f60cSPatrick McHardy 
1925cbda10faSVlad Dogaru 	if (tb[IFLA_GROUP]) {
1926cbda10faSVlad Dogaru 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
1927ba998906SNicolas Dichtel 		status |= DO_SETLINK_NOTIFY;
1928cbda10faSVlad Dogaru 	}
1929cbda10faSVlad Dogaru 
19300157f60cSPatrick McHardy 	/*
19310157f60cSPatrick McHardy 	 * Interface selected by interface index but interface
19320157f60cSPatrick McHardy 	 * name provided implies that a name change has been
19330157f60cSPatrick McHardy 	 * requested.
19340157f60cSPatrick McHardy 	 */
19350157f60cSPatrick McHardy 	if (ifm->ifi_index > 0 && ifname[0]) {
19360157f60cSPatrick McHardy 		err = dev_change_name(dev, ifname);
19370157f60cSPatrick McHardy 		if (err < 0)
19380157f60cSPatrick McHardy 			goto errout;
193990c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
19400157f60cSPatrick McHardy 	}
19410157f60cSPatrick McHardy 
19420b815a1aSStephen Hemminger 	if (tb[IFLA_IFALIAS]) {
19430b815a1aSStephen Hemminger 		err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
19440b815a1aSStephen Hemminger 				    nla_len(tb[IFLA_IFALIAS]));
19450b815a1aSStephen Hemminger 		if (err < 0)
19460b815a1aSStephen Hemminger 			goto errout;
1947ba998906SNicolas Dichtel 		status |= DO_SETLINK_NOTIFY;
19480b815a1aSStephen Hemminger 	}
19490b815a1aSStephen Hemminger 
19500157f60cSPatrick McHardy 	if (tb[IFLA_BROADCAST]) {
19510157f60cSPatrick McHardy 		nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
1952e7c3273eSJiri Pirko 		call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
19530157f60cSPatrick McHardy 	}
19540157f60cSPatrick McHardy 
19550157f60cSPatrick McHardy 	if (ifm->ifi_flags || ifm->ifi_change) {
19563729d502SPatrick McHardy 		err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
19575f9021cfSJohannes Berg 		if (err < 0)
19585f9021cfSJohannes Berg 			goto errout;
19590157f60cSPatrick McHardy 	}
19600157f60cSPatrick McHardy 
1961fbaec0eaSJiri Pirko 	if (tb[IFLA_MASTER]) {
1962fbaec0eaSJiri Pirko 		err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
1963fbaec0eaSJiri Pirko 		if (err)
1964fbaec0eaSJiri Pirko 			goto errout;
196590c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
1966fbaec0eaSJiri Pirko 	}
1967fbaec0eaSJiri Pirko 
19689a57247fSJiri Pirko 	if (tb[IFLA_CARRIER]) {
19699a57247fSJiri Pirko 		err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
19709a57247fSJiri Pirko 		if (err)
19719a57247fSJiri Pirko 			goto errout;
197290c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
19739a57247fSJiri Pirko 	}
19749a57247fSJiri Pirko 
19755d1180fcSNicolas Dichtel 	if (tb[IFLA_TXQLEN]) {
19765d1180fcSNicolas Dichtel 		unsigned long value = nla_get_u32(tb[IFLA_TXQLEN]);
197708294a26SJason Wang 		unsigned long orig_len = dev->tx_queue_len;
19785d1180fcSNicolas Dichtel 
197908294a26SJason Wang 		if (dev->tx_queue_len ^ value) {
19805d1180fcSNicolas Dichtel 			dev->tx_queue_len = value;
198108294a26SJason Wang 			err = call_netdevice_notifiers(
198208294a26SJason Wang 			      NETDEV_CHANGE_TX_QUEUE_LEN, dev);
198308294a26SJason Wang 			err = notifier_to_errno(err);
198408294a26SJason Wang 			if (err) {
198508294a26SJason Wang 				dev->tx_queue_len = orig_len;
198608294a26SJason Wang 				goto errout;
198708294a26SJason Wang 			}
198808294a26SJason Wang 			status |= DO_SETLINK_NOTIFY;
198908294a26SJason Wang 		}
19905d1180fcSNicolas Dichtel 	}
19910157f60cSPatrick McHardy 
19920157f60cSPatrick McHardy 	if (tb[IFLA_OPERSTATE])
199393b2d4a2SDavid S. Miller 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
19940157f60cSPatrick McHardy 
19950157f60cSPatrick McHardy 	if (tb[IFLA_LINKMODE]) {
19961889b0e7SNicolas Dichtel 		unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
19971889b0e7SNicolas Dichtel 
19980157f60cSPatrick McHardy 		write_lock_bh(&dev_base_lock);
19991889b0e7SNicolas Dichtel 		if (dev->link_mode ^ value)
2000ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
20011889b0e7SNicolas Dichtel 		dev->link_mode = value;
200293b2d4a2SDavid S. Miller 		write_unlock_bh(&dev_base_lock);
20030157f60cSPatrick McHardy 	}
20040157f60cSPatrick McHardy 
2005c02db8c6SChris Wright 	if (tb[IFLA_VFINFO_LIST]) {
20064f7d2cdfSDaniel Borkmann 		struct nlattr *vfinfo[IFLA_VF_MAX + 1];
2007c02db8c6SChris Wright 		struct nlattr *attr;
2008c02db8c6SChris Wright 		int rem;
20094f7d2cdfSDaniel Borkmann 
2010c02db8c6SChris Wright 		nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
20114f7d2cdfSDaniel Borkmann 			if (nla_type(attr) != IFLA_VF_INFO ||
20124f7d2cdfSDaniel Borkmann 			    nla_len(attr) < NLA_HDRLEN) {
2013253683bbSDavid Howells 				err = -EINVAL;
2014c02db8c6SChris Wright 				goto errout;
2015253683bbSDavid Howells 			}
20164f7d2cdfSDaniel Borkmann 			err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr,
20174f7d2cdfSDaniel Borkmann 					       ifla_vf_policy);
20184f7d2cdfSDaniel Borkmann 			if (err < 0)
20194f7d2cdfSDaniel Borkmann 				goto errout;
20204f7d2cdfSDaniel Borkmann 			err = do_setvfinfo(dev, vfinfo);
2021ebc08a6fSWilliams, Mitch A 			if (err < 0)
2022ebc08a6fSWilliams, Mitch A 				goto errout;
2023ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
2024ebc08a6fSWilliams, Mitch A 		}
2025ebc08a6fSWilliams, Mitch A 	}
20260157f60cSPatrick McHardy 	err = 0;
20270157f60cSPatrick McHardy 
202857b61080SScott Feldman 	if (tb[IFLA_VF_PORTS]) {
202957b61080SScott Feldman 		struct nlattr *port[IFLA_PORT_MAX+1];
203057b61080SScott Feldman 		struct nlattr *attr;
203157b61080SScott Feldman 		int vf;
203257b61080SScott Feldman 		int rem;
203357b61080SScott Feldman 
203457b61080SScott Feldman 		err = -EOPNOTSUPP;
203557b61080SScott Feldman 		if (!ops->ndo_set_vf_port)
203657b61080SScott Feldman 			goto errout;
203757b61080SScott Feldman 
203857b61080SScott Feldman 		nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
2039035d210fSDaniel Borkmann 			if (nla_type(attr) != IFLA_VF_PORT ||
2040035d210fSDaniel Borkmann 			    nla_len(attr) < NLA_HDRLEN) {
2041035d210fSDaniel Borkmann 				err = -EINVAL;
2042035d210fSDaniel Borkmann 				goto errout;
2043035d210fSDaniel Borkmann 			}
2044035d210fSDaniel Borkmann 			err = nla_parse_nested(port, IFLA_PORT_MAX, attr,
2045035d210fSDaniel Borkmann 					       ifla_port_policy);
204657b61080SScott Feldman 			if (err < 0)
204757b61080SScott Feldman 				goto errout;
204857b61080SScott Feldman 			if (!port[IFLA_PORT_VF]) {
204957b61080SScott Feldman 				err = -EOPNOTSUPP;
205057b61080SScott Feldman 				goto errout;
205157b61080SScott Feldman 			}
205257b61080SScott Feldman 			vf = nla_get_u32(port[IFLA_PORT_VF]);
205357b61080SScott Feldman 			err = ops->ndo_set_vf_port(dev, vf, port);
205457b61080SScott Feldman 			if (err < 0)
205557b61080SScott Feldman 				goto errout;
2056ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
205757b61080SScott Feldman 		}
205857b61080SScott Feldman 	}
205957b61080SScott Feldman 	err = 0;
206057b61080SScott Feldman 
206157b61080SScott Feldman 	if (tb[IFLA_PORT_SELF]) {
206257b61080SScott Feldman 		struct nlattr *port[IFLA_PORT_MAX+1];
206357b61080SScott Feldman 
206457b61080SScott Feldman 		err = nla_parse_nested(port, IFLA_PORT_MAX,
206557b61080SScott Feldman 			tb[IFLA_PORT_SELF], ifla_port_policy);
206657b61080SScott Feldman 		if (err < 0)
206757b61080SScott Feldman 			goto errout;
206857b61080SScott Feldman 
206957b61080SScott Feldman 		err = -EOPNOTSUPP;
207057b61080SScott Feldman 		if (ops->ndo_set_vf_port)
207157b61080SScott Feldman 			err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
207257b61080SScott Feldman 		if (err < 0)
207357b61080SScott Feldman 			goto errout;
2074ba998906SNicolas Dichtel 		status |= DO_SETLINK_NOTIFY;
207557b61080SScott Feldman 	}
2076f8ff182cSThomas Graf 
2077f8ff182cSThomas Graf 	if (tb[IFLA_AF_SPEC]) {
2078f8ff182cSThomas Graf 		struct nlattr *af;
2079f8ff182cSThomas Graf 		int rem;
2080f8ff182cSThomas Graf 
2081f8ff182cSThomas Graf 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2082f8ff182cSThomas Graf 			const struct rtnl_af_ops *af_ops;
2083f8ff182cSThomas Graf 
2084f8ff182cSThomas Graf 			if (!(af_ops = rtnl_af_lookup(nla_type(af))))
2085cf7afbfeSThomas Graf 				BUG();
2086f8ff182cSThomas Graf 
2087cf7afbfeSThomas Graf 			err = af_ops->set_link_af(dev, af);
2088f8ff182cSThomas Graf 			if (err < 0)
2089f8ff182cSThomas Graf 				goto errout;
2090f8ff182cSThomas Graf 
2091ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
2092f8ff182cSThomas Graf 		}
2093f8ff182cSThomas Graf 	}
209457b61080SScott Feldman 	err = 0;
209557b61080SScott Feldman 
209688d6378bSAnuradha Karuppiah 	if (tb[IFLA_PROTO_DOWN]) {
209788d6378bSAnuradha Karuppiah 		err = dev_change_proto_down(dev,
209888d6378bSAnuradha Karuppiah 					    nla_get_u8(tb[IFLA_PROTO_DOWN]));
209988d6378bSAnuradha Karuppiah 		if (err)
210088d6378bSAnuradha Karuppiah 			goto errout;
210188d6378bSAnuradha Karuppiah 		status |= DO_SETLINK_NOTIFY;
210288d6378bSAnuradha Karuppiah 	}
210388d6378bSAnuradha Karuppiah 
2104d1fdd913SBrenden Blanco 	if (tb[IFLA_XDP]) {
2105d1fdd913SBrenden Blanco 		struct nlattr *xdp[IFLA_XDP_MAX + 1];
2106d1fdd913SBrenden Blanco 
2107d1fdd913SBrenden Blanco 		err = nla_parse_nested(xdp, IFLA_XDP_MAX, tb[IFLA_XDP],
2108d1fdd913SBrenden Blanco 				       ifla_xdp_policy);
2109d1fdd913SBrenden Blanco 		if (err < 0)
2110d1fdd913SBrenden Blanco 			goto errout;
2111d1fdd913SBrenden Blanco 
2112d1fdd913SBrenden Blanco 		if (xdp[IFLA_XDP_FD]) {
2113d1fdd913SBrenden Blanco 			err = dev_change_xdp_fd(dev,
2114d1fdd913SBrenden Blanco 						nla_get_s32(xdp[IFLA_XDP_FD]));
2115d1fdd913SBrenden Blanco 			if (err)
2116d1fdd913SBrenden Blanco 				goto errout;
2117d1fdd913SBrenden Blanco 			status |= DO_SETLINK_NOTIFY;
2118d1fdd913SBrenden Blanco 		}
2119d1fdd913SBrenden Blanco 	}
2120d1fdd913SBrenden Blanco 
21210157f60cSPatrick McHardy errout:
2122ba998906SNicolas Dichtel 	if (status & DO_SETLINK_MODIFIED) {
2123ba998906SNicolas Dichtel 		if (status & DO_SETLINK_NOTIFY)
2124ba998906SNicolas Dichtel 			netdev_state_change(dev);
2125ba998906SNicolas Dichtel 
2126ba998906SNicolas Dichtel 		if (err < 0)
2127e87cc472SJoe 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",
2128e87cc472SJoe Perches 					     dev->name);
2129ba998906SNicolas Dichtel 	}
21300157f60cSPatrick McHardy 
21310157f60cSPatrick McHardy 	return err;
21320157f60cSPatrick McHardy }
21330157f60cSPatrick McHardy 
2134661d2967SThomas Graf static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
2135da5e0494SThomas Graf {
21363b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
2137da5e0494SThomas Graf 	struct ifinfomsg *ifm;
2138da5e0494SThomas Graf 	struct net_device *dev;
21390157f60cSPatrick McHardy 	int err;
2140da5e0494SThomas Graf 	struct nlattr *tb[IFLA_MAX+1];
21411da177e4SLinus Torvalds 	char ifname[IFNAMSIZ];
21421da177e4SLinus Torvalds 
2143da5e0494SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2144da5e0494SThomas Graf 	if (err < 0)
2145da5e0494SThomas Graf 		goto errout;
21461da177e4SLinus Torvalds 
21475176f91eSThomas Graf 	if (tb[IFLA_IFNAME])
21485176f91eSThomas Graf 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
214978e5b891SPatrick McHardy 	else
215078e5b891SPatrick McHardy 		ifname[0] = '\0';
21511da177e4SLinus Torvalds 
21521da177e4SLinus Torvalds 	err = -EINVAL;
2153da5e0494SThomas Graf 	ifm = nlmsg_data(nlh);
215451055be8SPatrick McHardy 	if (ifm->ifi_index > 0)
2155a3d12891SEric Dumazet 		dev = __dev_get_by_index(net, ifm->ifi_index);
2156da5e0494SThomas Graf 	else if (tb[IFLA_IFNAME])
2157a3d12891SEric Dumazet 		dev = __dev_get_by_name(net, ifname);
2158da5e0494SThomas Graf 	else
2159da5e0494SThomas Graf 		goto errout;
21601da177e4SLinus Torvalds 
2161da5e0494SThomas Graf 	if (dev == NULL) {
2162da5e0494SThomas Graf 		err = -ENODEV;
2163da5e0494SThomas Graf 		goto errout;
2164da5e0494SThomas Graf 	}
21651da177e4SLinus Torvalds 
2166e0d087afSEric Dumazet 	err = validate_linkmsg(dev, tb);
2167e0d087afSEric Dumazet 	if (err < 0)
2168a3d12891SEric Dumazet 		goto errout;
2169da5e0494SThomas Graf 
217090f62cf3SEric W. Biederman 	err = do_setlink(skb, dev, ifm, tb, ifname, 0);
2171da5e0494SThomas Graf errout:
21721da177e4SLinus Torvalds 	return err;
21731da177e4SLinus Torvalds }
21741da177e4SLinus Torvalds 
217566400d54SWANG Cong static int rtnl_group_dellink(const struct net *net, int group)
217666400d54SWANG Cong {
217766400d54SWANG Cong 	struct net_device *dev, *aux;
217866400d54SWANG Cong 	LIST_HEAD(list_kill);
217966400d54SWANG Cong 	bool found = false;
218066400d54SWANG Cong 
218166400d54SWANG Cong 	if (!group)
218266400d54SWANG Cong 		return -EPERM;
218366400d54SWANG Cong 
218466400d54SWANG Cong 	for_each_netdev(net, dev) {
218566400d54SWANG Cong 		if (dev->group == group) {
218666400d54SWANG Cong 			const struct rtnl_link_ops *ops;
218766400d54SWANG Cong 
218866400d54SWANG Cong 			found = true;
218966400d54SWANG Cong 			ops = dev->rtnl_link_ops;
219066400d54SWANG Cong 			if (!ops || !ops->dellink)
219166400d54SWANG Cong 				return -EOPNOTSUPP;
219266400d54SWANG Cong 		}
219366400d54SWANG Cong 	}
219466400d54SWANG Cong 
219566400d54SWANG Cong 	if (!found)
219666400d54SWANG Cong 		return -ENODEV;
219766400d54SWANG Cong 
219866400d54SWANG Cong 	for_each_netdev_safe(net, dev, aux) {
219966400d54SWANG Cong 		if (dev->group == group) {
220066400d54SWANG Cong 			const struct rtnl_link_ops *ops;
220166400d54SWANG Cong 
220266400d54SWANG Cong 			ops = dev->rtnl_link_ops;
220366400d54SWANG Cong 			ops->dellink(dev, &list_kill);
220466400d54SWANG Cong 		}
220566400d54SWANG Cong 	}
220666400d54SWANG Cong 	unregister_netdevice_many(&list_kill);
220766400d54SWANG Cong 
220866400d54SWANG Cong 	return 0;
220966400d54SWANG Cong }
221066400d54SWANG Cong 
2211614732eaSThomas Graf int rtnl_delete_link(struct net_device *dev)
2212614732eaSThomas Graf {
2213614732eaSThomas Graf 	const struct rtnl_link_ops *ops;
2214614732eaSThomas Graf 	LIST_HEAD(list_kill);
2215614732eaSThomas Graf 
2216614732eaSThomas Graf 	ops = dev->rtnl_link_ops;
2217614732eaSThomas Graf 	if (!ops || !ops->dellink)
2218614732eaSThomas Graf 		return -EOPNOTSUPP;
2219614732eaSThomas Graf 
2220614732eaSThomas Graf 	ops->dellink(dev, &list_kill);
2221614732eaSThomas Graf 	unregister_netdevice_many(&list_kill);
2222614732eaSThomas Graf 
2223614732eaSThomas Graf 	return 0;
2224614732eaSThomas Graf }
2225614732eaSThomas Graf EXPORT_SYMBOL_GPL(rtnl_delete_link);
2226614732eaSThomas Graf 
2227661d2967SThomas Graf static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
222838f7b870SPatrick McHardy {
22293b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
223038f7b870SPatrick McHardy 	struct net_device *dev;
223138f7b870SPatrick McHardy 	struct ifinfomsg *ifm;
223238f7b870SPatrick McHardy 	char ifname[IFNAMSIZ];
223338f7b870SPatrick McHardy 	struct nlattr *tb[IFLA_MAX+1];
223438f7b870SPatrick McHardy 	int err;
223538f7b870SPatrick McHardy 
223638f7b870SPatrick McHardy 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
223738f7b870SPatrick McHardy 	if (err < 0)
223838f7b870SPatrick McHardy 		return err;
223938f7b870SPatrick McHardy 
224038f7b870SPatrick McHardy 	if (tb[IFLA_IFNAME])
224138f7b870SPatrick McHardy 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
224238f7b870SPatrick McHardy 
224338f7b870SPatrick McHardy 	ifm = nlmsg_data(nlh);
224438f7b870SPatrick McHardy 	if (ifm->ifi_index > 0)
2245881d966bSEric W. Biederman 		dev = __dev_get_by_index(net, ifm->ifi_index);
224638f7b870SPatrick McHardy 	else if (tb[IFLA_IFNAME])
2247881d966bSEric W. Biederman 		dev = __dev_get_by_name(net, ifname);
224866400d54SWANG Cong 	else if (tb[IFLA_GROUP])
224966400d54SWANG Cong 		return rtnl_group_dellink(net, nla_get_u32(tb[IFLA_GROUP]));
225038f7b870SPatrick McHardy 	else
225138f7b870SPatrick McHardy 		return -EINVAL;
225238f7b870SPatrick McHardy 
225338f7b870SPatrick McHardy 	if (!dev)
225438f7b870SPatrick McHardy 		return -ENODEV;
225538f7b870SPatrick McHardy 
2256614732eaSThomas Graf 	return rtnl_delete_link(dev);
225738f7b870SPatrick McHardy }
225838f7b870SPatrick McHardy 
22593729d502SPatrick McHardy int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
22603729d502SPatrick McHardy {
22613729d502SPatrick McHardy 	unsigned int old_flags;
22623729d502SPatrick McHardy 	int err;
22633729d502SPatrick McHardy 
22643729d502SPatrick McHardy 	old_flags = dev->flags;
22653729d502SPatrick McHardy 	if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
22663729d502SPatrick McHardy 		err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
22673729d502SPatrick McHardy 		if (err < 0)
22683729d502SPatrick McHardy 			return err;
22693729d502SPatrick McHardy 	}
22703729d502SPatrick McHardy 
22713729d502SPatrick McHardy 	dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
22723729d502SPatrick McHardy 
2273a528c219SNicolas Dichtel 	__dev_notify_flags(dev, old_flags, ~0U);
22743729d502SPatrick McHardy 	return 0;
22753729d502SPatrick McHardy }
22763729d502SPatrick McHardy EXPORT_SYMBOL(rtnl_configure_link);
22773729d502SPatrick McHardy 
2278c0713563SRami Rosen struct net_device *rtnl_create_link(struct net *net,
227978ebb0d0SThomas Graf 	const char *ifname, unsigned char name_assign_type,
22805517750fSTom Gundersen 	const struct rtnl_link_ops *ops, struct nlattr *tb[])
2281e7199288SPavel Emelianov {
2282e7199288SPavel Emelianov 	int err;
2283e7199288SPavel Emelianov 	struct net_device *dev;
2284d40156aaSJiri Pirko 	unsigned int num_tx_queues = 1;
2285d40156aaSJiri Pirko 	unsigned int num_rx_queues = 1;
2286e7199288SPavel Emelianov 
228776ff5cc9SJiri Pirko 	if (tb[IFLA_NUM_TX_QUEUES])
228876ff5cc9SJiri Pirko 		num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
228976ff5cc9SJiri Pirko 	else if (ops->get_num_tx_queues)
2290d40156aaSJiri Pirko 		num_tx_queues = ops->get_num_tx_queues();
229176ff5cc9SJiri Pirko 
229276ff5cc9SJiri Pirko 	if (tb[IFLA_NUM_RX_QUEUES])
229376ff5cc9SJiri Pirko 		num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
229476ff5cc9SJiri Pirko 	else if (ops->get_num_rx_queues)
2295d40156aaSJiri Pirko 		num_rx_queues = ops->get_num_rx_queues();
2296efacb309Sstephen hemminger 
2297e7199288SPavel Emelianov 	err = -ENOMEM;
22985517750fSTom Gundersen 	dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
2299c835a677STom Gundersen 			       ops->setup, num_tx_queues, num_rx_queues);
2300e7199288SPavel Emelianov 	if (!dev)
2301e7199288SPavel Emelianov 		goto err;
2302e7199288SPavel Emelianov 
230381adee47SEric W. Biederman 	dev_net_set(dev, net);
230481adee47SEric W. Biederman 	dev->rtnl_link_ops = ops;
23053729d502SPatrick McHardy 	dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
230681adee47SEric W. Biederman 
2307e7199288SPavel Emelianov 	if (tb[IFLA_MTU])
2308e7199288SPavel Emelianov 		dev->mtu = nla_get_u32(tb[IFLA_MTU]);
23092afb9b53SJiri Pirko 	if (tb[IFLA_ADDRESS]) {
2310e7199288SPavel Emelianov 		memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
2311e7199288SPavel Emelianov 				nla_len(tb[IFLA_ADDRESS]));
23122afb9b53SJiri Pirko 		dev->addr_assign_type = NET_ADDR_SET;
23132afb9b53SJiri Pirko 	}
2314e7199288SPavel Emelianov 	if (tb[IFLA_BROADCAST])
2315e7199288SPavel Emelianov 		memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
2316e7199288SPavel Emelianov 				nla_len(tb[IFLA_BROADCAST]));
2317e7199288SPavel Emelianov 	if (tb[IFLA_TXQLEN])
2318e7199288SPavel Emelianov 		dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
2319e7199288SPavel Emelianov 	if (tb[IFLA_OPERSTATE])
232093b2d4a2SDavid S. Miller 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
2321e7199288SPavel Emelianov 	if (tb[IFLA_LINKMODE])
2322e7199288SPavel Emelianov 		dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
2323ffa934f1SPatrick McHardy 	if (tb[IFLA_GROUP])
2324ffa934f1SPatrick McHardy 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
2325e7199288SPavel Emelianov 
2326e7199288SPavel Emelianov 	return dev;
2327e7199288SPavel Emelianov 
2328e7199288SPavel Emelianov err:
2329e7199288SPavel Emelianov 	return ERR_PTR(err);
2330e7199288SPavel Emelianov }
2331e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_create_link);
2332e7199288SPavel Emelianov 
233390f62cf3SEric W. Biederman static int rtnl_group_changelink(const struct sk_buff *skb,
233490f62cf3SEric W. Biederman 		struct net *net, int group,
2335e7ed828fSVlad Dogaru 		struct ifinfomsg *ifm,
2336e7ed828fSVlad Dogaru 		struct nlattr **tb)
2337e7ed828fSVlad Dogaru {
2338d079535dSWANG Cong 	struct net_device *dev, *aux;
2339e7ed828fSVlad Dogaru 	int err;
2340e7ed828fSVlad Dogaru 
2341d079535dSWANG Cong 	for_each_netdev_safe(net, dev, aux) {
2342e7ed828fSVlad Dogaru 		if (dev->group == group) {
234390f62cf3SEric W. Biederman 			err = do_setlink(skb, dev, ifm, tb, NULL, 0);
2344e7ed828fSVlad Dogaru 			if (err < 0)
2345e7ed828fSVlad Dogaru 				return err;
2346e7ed828fSVlad Dogaru 		}
2347e7ed828fSVlad Dogaru 	}
2348e7ed828fSVlad Dogaru 
2349e7ed828fSVlad Dogaru 	return 0;
2350e7ed828fSVlad Dogaru }
2351e7ed828fSVlad Dogaru 
2352661d2967SThomas Graf static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh)
235338f7b870SPatrick McHardy {
23543b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
235538f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops;
2356ba7d49b1SJiri Pirko 	const struct rtnl_link_ops *m_ops = NULL;
235738f7b870SPatrick McHardy 	struct net_device *dev;
2358ba7d49b1SJiri Pirko 	struct net_device *master_dev = NULL;
235938f7b870SPatrick McHardy 	struct ifinfomsg *ifm;
236038f7b870SPatrick McHardy 	char kind[MODULE_NAME_LEN];
236138f7b870SPatrick McHardy 	char ifname[IFNAMSIZ];
236238f7b870SPatrick McHardy 	struct nlattr *tb[IFLA_MAX+1];
236338f7b870SPatrick McHardy 	struct nlattr *linkinfo[IFLA_INFO_MAX+1];
23645517750fSTom Gundersen 	unsigned char name_assign_type = NET_NAME_USER;
236538f7b870SPatrick McHardy 	int err;
236638f7b870SPatrick McHardy 
236795a5afcaSJohannes Berg #ifdef CONFIG_MODULES
236838f7b870SPatrick McHardy replay:
23698072f085SThomas Graf #endif
237038f7b870SPatrick McHardy 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
237138f7b870SPatrick McHardy 	if (err < 0)
237238f7b870SPatrick McHardy 		return err;
237338f7b870SPatrick McHardy 
237438f7b870SPatrick McHardy 	if (tb[IFLA_IFNAME])
237538f7b870SPatrick McHardy 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
237638f7b870SPatrick McHardy 	else
237738f7b870SPatrick McHardy 		ifname[0] = '\0';
237838f7b870SPatrick McHardy 
237938f7b870SPatrick McHardy 	ifm = nlmsg_data(nlh);
238038f7b870SPatrick McHardy 	if (ifm->ifi_index > 0)
2381881d966bSEric W. Biederman 		dev = __dev_get_by_index(net, ifm->ifi_index);
2382e7ed828fSVlad Dogaru 	else {
2383e7ed828fSVlad Dogaru 		if (ifname[0])
2384881d966bSEric W. Biederman 			dev = __dev_get_by_name(net, ifname);
238538f7b870SPatrick McHardy 		else
238638f7b870SPatrick McHardy 			dev = NULL;
2387e7ed828fSVlad Dogaru 	}
238838f7b870SPatrick McHardy 
2389ba7d49b1SJiri Pirko 	if (dev) {
2390ba7d49b1SJiri Pirko 		master_dev = netdev_master_upper_dev_get(dev);
2391ba7d49b1SJiri Pirko 		if (master_dev)
2392ba7d49b1SJiri Pirko 			m_ops = master_dev->rtnl_link_ops;
2393ba7d49b1SJiri Pirko 	}
2394ba7d49b1SJiri Pirko 
2395e0d087afSEric Dumazet 	err = validate_linkmsg(dev, tb);
2396e0d087afSEric Dumazet 	if (err < 0)
23971840bb13SThomas Graf 		return err;
23981840bb13SThomas Graf 
239938f7b870SPatrick McHardy 	if (tb[IFLA_LINKINFO]) {
240038f7b870SPatrick McHardy 		err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
240138f7b870SPatrick McHardy 				       tb[IFLA_LINKINFO], ifla_info_policy);
240238f7b870SPatrick McHardy 		if (err < 0)
240338f7b870SPatrick McHardy 			return err;
240438f7b870SPatrick McHardy 	} else
240538f7b870SPatrick McHardy 		memset(linkinfo, 0, sizeof(linkinfo));
240638f7b870SPatrick McHardy 
240738f7b870SPatrick McHardy 	if (linkinfo[IFLA_INFO_KIND]) {
240838f7b870SPatrick McHardy 		nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
240938f7b870SPatrick McHardy 		ops = rtnl_link_ops_get(kind);
241038f7b870SPatrick McHardy 	} else {
241138f7b870SPatrick McHardy 		kind[0] = '\0';
241238f7b870SPatrick McHardy 		ops = NULL;
241338f7b870SPatrick McHardy 	}
241438f7b870SPatrick McHardy 
241538f7b870SPatrick McHardy 	if (1) {
24164e10fd5bSSasha Levin 		struct nlattr *attr[ops ? ops->maxtype + 1 : 1];
24174e10fd5bSSasha Levin 		struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 1];
2418ba7d49b1SJiri Pirko 		struct nlattr **data = NULL;
2419ba7d49b1SJiri Pirko 		struct nlattr **slave_data = NULL;
2420317f4810SNicolas Dichtel 		struct net *dest_net, *link_net = NULL;
242138f7b870SPatrick McHardy 
242238f7b870SPatrick McHardy 		if (ops) {
242338f7b870SPatrick McHardy 			if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
242438f7b870SPatrick McHardy 				err = nla_parse_nested(attr, ops->maxtype,
242538f7b870SPatrick McHardy 						       linkinfo[IFLA_INFO_DATA],
242638f7b870SPatrick McHardy 						       ops->policy);
242738f7b870SPatrick McHardy 				if (err < 0)
242838f7b870SPatrick McHardy 					return err;
242938f7b870SPatrick McHardy 				data = attr;
243038f7b870SPatrick McHardy 			}
243138f7b870SPatrick McHardy 			if (ops->validate) {
243238f7b870SPatrick McHardy 				err = ops->validate(tb, data);
243338f7b870SPatrick McHardy 				if (err < 0)
243438f7b870SPatrick McHardy 					return err;
243538f7b870SPatrick McHardy 			}
243638f7b870SPatrick McHardy 		}
243738f7b870SPatrick McHardy 
2438ba7d49b1SJiri Pirko 		if (m_ops) {
2439ba7d49b1SJiri Pirko 			if (m_ops->slave_maxtype &&
2440ba7d49b1SJiri Pirko 			    linkinfo[IFLA_INFO_SLAVE_DATA]) {
2441ba7d49b1SJiri Pirko 				err = nla_parse_nested(slave_attr,
2442ba7d49b1SJiri Pirko 						       m_ops->slave_maxtype,
2443ba7d49b1SJiri Pirko 						       linkinfo[IFLA_INFO_SLAVE_DATA],
2444ba7d49b1SJiri Pirko 						       m_ops->slave_policy);
2445ba7d49b1SJiri Pirko 				if (err < 0)
2446ba7d49b1SJiri Pirko 					return err;
2447ba7d49b1SJiri Pirko 				slave_data = slave_attr;
2448ba7d49b1SJiri Pirko 			}
2449ba7d49b1SJiri Pirko 			if (m_ops->slave_validate) {
2450ba7d49b1SJiri Pirko 				err = m_ops->slave_validate(tb, slave_data);
2451ba7d49b1SJiri Pirko 				if (err < 0)
2452ba7d49b1SJiri Pirko 					return err;
2453ba7d49b1SJiri Pirko 			}
2454ba7d49b1SJiri Pirko 		}
2455ba7d49b1SJiri Pirko 
245638f7b870SPatrick McHardy 		if (dev) {
245790c325e3SNicolas Dichtel 			int status = 0;
245838f7b870SPatrick McHardy 
245938f7b870SPatrick McHardy 			if (nlh->nlmsg_flags & NLM_F_EXCL)
246038f7b870SPatrick McHardy 				return -EEXIST;
246138f7b870SPatrick McHardy 			if (nlh->nlmsg_flags & NLM_F_REPLACE)
246238f7b870SPatrick McHardy 				return -EOPNOTSUPP;
246338f7b870SPatrick McHardy 
246438f7b870SPatrick McHardy 			if (linkinfo[IFLA_INFO_DATA]) {
246538f7b870SPatrick McHardy 				if (!ops || ops != dev->rtnl_link_ops ||
246638f7b870SPatrick McHardy 				    !ops->changelink)
246738f7b870SPatrick McHardy 					return -EOPNOTSUPP;
246838f7b870SPatrick McHardy 
246938f7b870SPatrick McHardy 				err = ops->changelink(dev, tb, data);
247038f7b870SPatrick McHardy 				if (err < 0)
247138f7b870SPatrick McHardy 					return err;
2472ba998906SNicolas Dichtel 				status |= DO_SETLINK_NOTIFY;
247338f7b870SPatrick McHardy 			}
247438f7b870SPatrick McHardy 
2475ba7d49b1SJiri Pirko 			if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
2476ba7d49b1SJiri Pirko 				if (!m_ops || !m_ops->slave_changelink)
2477ba7d49b1SJiri Pirko 					return -EOPNOTSUPP;
2478ba7d49b1SJiri Pirko 
2479ba7d49b1SJiri Pirko 				err = m_ops->slave_changelink(master_dev, dev,
2480ba7d49b1SJiri Pirko 							      tb, slave_data);
2481ba7d49b1SJiri Pirko 				if (err < 0)
2482ba7d49b1SJiri Pirko 					return err;
2483ba998906SNicolas Dichtel 				status |= DO_SETLINK_NOTIFY;
2484ba7d49b1SJiri Pirko 			}
2485ba7d49b1SJiri Pirko 
248690c325e3SNicolas Dichtel 			return do_setlink(skb, dev, ifm, tb, ifname, status);
248738f7b870SPatrick McHardy 		}
248838f7b870SPatrick McHardy 
2489ffa934f1SPatrick McHardy 		if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
2490ffa934f1SPatrick McHardy 			if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
249190f62cf3SEric W. Biederman 				return rtnl_group_changelink(skb, net,
2492ffa934f1SPatrick McHardy 						nla_get_u32(tb[IFLA_GROUP]),
2493ffa934f1SPatrick McHardy 						ifm, tb);
249438f7b870SPatrick McHardy 			return -ENODEV;
2495ffa934f1SPatrick McHardy 		}
249638f7b870SPatrick McHardy 
24970e06877cSPatrick McHardy 		if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
249838f7b870SPatrick McHardy 			return -EOPNOTSUPP;
249938f7b870SPatrick McHardy 
250038f7b870SPatrick McHardy 		if (!ops) {
250195a5afcaSJohannes Berg #ifdef CONFIG_MODULES
250238f7b870SPatrick McHardy 			if (kind[0]) {
250338f7b870SPatrick McHardy 				__rtnl_unlock();
250438f7b870SPatrick McHardy 				request_module("rtnl-link-%s", kind);
250538f7b870SPatrick McHardy 				rtnl_lock();
250638f7b870SPatrick McHardy 				ops = rtnl_link_ops_get(kind);
250738f7b870SPatrick McHardy 				if (ops)
250838f7b870SPatrick McHardy 					goto replay;
250938f7b870SPatrick McHardy 			}
251038f7b870SPatrick McHardy #endif
251138f7b870SPatrick McHardy 			return -EOPNOTSUPP;
251238f7b870SPatrick McHardy 		}
251338f7b870SPatrick McHardy 
2514b0ab2fabSJiri Pirko 		if (!ops->setup)
2515b0ab2fabSJiri Pirko 			return -EOPNOTSUPP;
2516b0ab2fabSJiri Pirko 
25175517750fSTom Gundersen 		if (!ifname[0]) {
251838f7b870SPatrick McHardy 			snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
25195517750fSTom Gundersen 			name_assign_type = NET_NAME_ENUM;
25205517750fSTom Gundersen 		}
252138f7b870SPatrick McHardy 
252281adee47SEric W. Biederman 		dest_net = rtnl_link_get_net(net, tb);
252313ad1774SEric W. Biederman 		if (IS_ERR(dest_net))
252413ad1774SEric W. Biederman 			return PTR_ERR(dest_net);
252513ad1774SEric W. Biederman 
2526505ce415SEric W. Biederman 		err = -EPERM;
2527505ce415SEric W. Biederman 		if (!netlink_ns_capable(skb, dest_net->user_ns, CAP_NET_ADMIN))
2528505ce415SEric W. Biederman 			goto out;
2529505ce415SEric W. Biederman 
2530317f4810SNicolas Dichtel 		if (tb[IFLA_LINK_NETNSID]) {
2531317f4810SNicolas Dichtel 			int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
2532317f4810SNicolas Dichtel 
2533317f4810SNicolas Dichtel 			link_net = get_net_ns_by_id(dest_net, id);
2534317f4810SNicolas Dichtel 			if (!link_net) {
2535317f4810SNicolas Dichtel 				err =  -EINVAL;
2536317f4810SNicolas Dichtel 				goto out;
2537317f4810SNicolas Dichtel 			}
253806615bedSEric W. Biederman 			err = -EPERM;
253906615bedSEric W. Biederman 			if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
254006615bedSEric W. Biederman 				goto out;
2541317f4810SNicolas Dichtel 		}
2542317f4810SNicolas Dichtel 
2543317f4810SNicolas Dichtel 		dev = rtnl_create_link(link_net ? : dest_net, ifname,
2544317f4810SNicolas Dichtel 				       name_assign_type, ops, tb);
25459c7dafbfSPavel Emelyanov 		if (IS_ERR(dev)) {
2546e7199288SPavel Emelianov 			err = PTR_ERR(dev);
25479c7dafbfSPavel Emelyanov 			goto out;
25489c7dafbfSPavel Emelyanov 		}
25499c7dafbfSPavel Emelyanov 
25509c7dafbfSPavel Emelyanov 		dev->ifindex = ifm->ifi_index;
25519c7dafbfSPavel Emelyanov 
25520e0eee24SCong Wang 		if (ops->newlink) {
25537b4ce694SNicolas Dichtel 			err = ops->newlink(link_net ? : net, dev, tb, data);
25540e0eee24SCong Wang 			/* Drivers should call free_netdev() in ->destructor
2555e51fb152SCong Wang 			 * and unregister it on failure after registration
2556e51fb152SCong Wang 			 * so that device could be finally freed in rtnl_unlock.
25570e0eee24SCong Wang 			 */
2558e51fb152SCong Wang 			if (err < 0) {
2559e51fb152SCong Wang 				/* If device is not registered at all, free it now */
2560e51fb152SCong Wang 				if (dev->reg_state == NETREG_UNINITIALIZED)
2561e51fb152SCong Wang 					free_netdev(dev);
25620e0eee24SCong Wang 				goto out;
2563e51fb152SCong Wang 			}
25640e0eee24SCong Wang 		} else {
25652d85cba2SPatrick McHardy 			err = register_netdevice(dev);
2566fce9b9beSDan Carpenter 			if (err < 0) {
256738f7b870SPatrick McHardy 				free_netdev(dev);
25683729d502SPatrick McHardy 				goto out;
2569fce9b9beSDan Carpenter 			}
25700e0eee24SCong Wang 		}
25713729d502SPatrick McHardy 		err = rtnl_configure_link(dev, ifm);
257243638900SDavid S. Miller 		if (err < 0)
257343638900SDavid S. Miller 			goto out_unregister;
257443638900SDavid S. Miller 		if (link_net) {
257543638900SDavid S. Miller 			err = dev_change_net_namespace(dev, dest_net, ifname);
257643638900SDavid S. Miller 			if (err < 0)
257743638900SDavid S. Miller 				goto out_unregister;
257843638900SDavid S. Miller 		}
257943638900SDavid S. Miller out:
258043638900SDavid S. Miller 		if (link_net)
258143638900SDavid S. Miller 			put_net(link_net);
258243638900SDavid S. Miller 		put_net(dest_net);
258343638900SDavid S. Miller 		return err;
258443638900SDavid S. Miller out_unregister:
25857afb8886SWANG Cong 		if (ops->newlink) {
25867afb8886SWANG Cong 			LIST_HEAD(list_kill);
25877afb8886SWANG Cong 
25887afb8886SWANG Cong 			ops->dellink(dev, &list_kill);
25897afb8886SWANG Cong 			unregister_netdevice_many(&list_kill);
25907afb8886SWANG Cong 		} else {
25913729d502SPatrick McHardy 			unregister_netdevice(dev);
25927afb8886SWANG Cong 		}
2593317f4810SNicolas Dichtel 		goto out;
2594317f4810SNicolas Dichtel 	}
259538f7b870SPatrick McHardy }
259638f7b870SPatrick McHardy 
2597661d2967SThomas Graf static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh)
2598711e2c33SJean Tourrilhes {
25993b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
2600b60c5115SThomas Graf 	struct ifinfomsg *ifm;
2601a3d12891SEric Dumazet 	char ifname[IFNAMSIZ];
2602b60c5115SThomas Graf 	struct nlattr *tb[IFLA_MAX+1];
2603b60c5115SThomas Graf 	struct net_device *dev = NULL;
2604b60c5115SThomas Graf 	struct sk_buff *nskb;
2605339bf98fSThomas Graf 	int err;
2606115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
2607711e2c33SJean Tourrilhes 
2608b60c5115SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2609b60c5115SThomas Graf 	if (err < 0)
26109918f230SEric Sesterhenn 		return err;
2611b60c5115SThomas Graf 
2612a3d12891SEric Dumazet 	if (tb[IFLA_IFNAME])
2613a3d12891SEric Dumazet 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2614a3d12891SEric Dumazet 
2615115c9b81SGreg Rose 	if (tb[IFLA_EXT_MASK])
2616115c9b81SGreg Rose 		ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2617115c9b81SGreg Rose 
2618b60c5115SThomas Graf 	ifm = nlmsg_data(nlh);
2619a3d12891SEric Dumazet 	if (ifm->ifi_index > 0)
2620a3d12891SEric Dumazet 		dev = __dev_get_by_index(net, ifm->ifi_index);
2621a3d12891SEric Dumazet 	else if (tb[IFLA_IFNAME])
2622a3d12891SEric Dumazet 		dev = __dev_get_by_name(net, ifname);
2623a3d12891SEric Dumazet 	else
2624b60c5115SThomas Graf 		return -EINVAL;
2625b60c5115SThomas Graf 
2626a3d12891SEric Dumazet 	if (dev == NULL)
2627a3d12891SEric Dumazet 		return -ENODEV;
2628a3d12891SEric Dumazet 
2629115c9b81SGreg Rose 	nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
2630a3d12891SEric Dumazet 	if (nskb == NULL)
2631a3d12891SEric Dumazet 		return -ENOBUFS;
2632711e2c33SJean Tourrilhes 
263315e47304SEric W. Biederman 	err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid,
2634115c9b81SGreg Rose 			       nlh->nlmsg_seq, 0, 0, ext_filter_mask);
263526932566SPatrick McHardy 	if (err < 0) {
263626932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in if_nlmsg_size */
263726932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
263826932566SPatrick McHardy 		kfree_skb(nskb);
2639a3d12891SEric Dumazet 	} else
264015e47304SEric W. Biederman 		err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
2641b60c5115SThomas Graf 
2642711e2c33SJean Tourrilhes 	return err;
2643711e2c33SJean Tourrilhes }
2644711e2c33SJean Tourrilhes 
2645115c9b81SGreg Rose static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
2646c7ac8679SGreg Rose {
2647115c9b81SGreg Rose 	struct net *net = sock_net(skb->sk);
2648115c9b81SGreg Rose 	struct net_device *dev;
2649115c9b81SGreg Rose 	struct nlattr *tb[IFLA_MAX+1];
2650115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
2651115c9b81SGreg Rose 	u16 min_ifinfo_dump_size = 0;
2652e5eca6d4SMichal Schmidt 	int hdrlen;
2653115c9b81SGreg Rose 
2654e5eca6d4SMichal Schmidt 	/* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
2655e5eca6d4SMichal Schmidt 	hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2656e5eca6d4SMichal Schmidt 		 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2657e5eca6d4SMichal Schmidt 
2658e5eca6d4SMichal Schmidt 	if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
2659115c9b81SGreg Rose 		if (tb[IFLA_EXT_MASK])
2660115c9b81SGreg Rose 			ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2661a4b64fbeSEric Dumazet 	}
2662115c9b81SGreg Rose 
2663115c9b81SGreg Rose 	if (!ext_filter_mask)
2664115c9b81SGreg Rose 		return NLMSG_GOODSIZE;
2665115c9b81SGreg Rose 	/*
2666115c9b81SGreg Rose 	 * traverse the list of net devices and compute the minimum
2667115c9b81SGreg Rose 	 * buffer size based upon the filter mask.
2668115c9b81SGreg Rose 	 */
2669115c9b81SGreg Rose 	list_for_each_entry(dev, &net->dev_base_head, dev_list) {
2670115c9b81SGreg Rose 		min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
2671115c9b81SGreg Rose 					     if_nlmsg_size(dev,
2672115c9b81SGreg Rose 						           ext_filter_mask));
2673115c9b81SGreg Rose 	}
2674115c9b81SGreg Rose 
2675c7ac8679SGreg Rose 	return min_ifinfo_dump_size;
2676c7ac8679SGreg Rose }
2677c7ac8679SGreg Rose 
267842bad1daSAdrian Bunk static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
26791da177e4SLinus Torvalds {
26801da177e4SLinus Torvalds 	int idx;
26811da177e4SLinus Torvalds 	int s_idx = cb->family;
26821da177e4SLinus Torvalds 
26831da177e4SLinus Torvalds 	if (s_idx == 0)
26841da177e4SLinus Torvalds 		s_idx = 1;
268525239ceeSPatrick McHardy 	for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
26861da177e4SLinus Torvalds 		int type = cb->nlh->nlmsg_type-RTM_BASE;
26871da177e4SLinus Torvalds 		if (idx < s_idx || idx == PF_PACKET)
26881da177e4SLinus Torvalds 			continue;
2689e2849863SThomas Graf 		if (rtnl_msg_handlers[idx] == NULL ||
2690e2849863SThomas Graf 		    rtnl_msg_handlers[idx][type].dumpit == NULL)
26911da177e4SLinus Torvalds 			continue;
26920465277fSNicolas Dichtel 		if (idx > s_idx) {
26931da177e4SLinus Torvalds 			memset(&cb->args[0], 0, sizeof(cb->args));
26940465277fSNicolas Dichtel 			cb->prev_seq = 0;
26950465277fSNicolas Dichtel 			cb->seq = 0;
26960465277fSNicolas Dichtel 		}
2697e2849863SThomas Graf 		if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
26981da177e4SLinus Torvalds 			break;
26991da177e4SLinus Torvalds 	}
27001da177e4SLinus Torvalds 	cb->family = idx;
27011da177e4SLinus Torvalds 
27021da177e4SLinus Torvalds 	return skb->len;
27031da177e4SLinus Torvalds }
27041da177e4SLinus Torvalds 
2705395eea6cSMahesh Bandewar struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
2706395eea6cSMahesh Bandewar 				       unsigned int change, gfp_t flags)
27071da177e4SLinus Torvalds {
2708c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
27091da177e4SLinus Torvalds 	struct sk_buff *skb;
27100ec6d3f4SThomas Graf 	int err = -ENOBUFS;
2711c7ac8679SGreg Rose 	size_t if_info_size;
27121da177e4SLinus Torvalds 
27137f294054SAlexei Starovoitov 	skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
27140ec6d3f4SThomas Graf 	if (skb == NULL)
27150ec6d3f4SThomas Graf 		goto errout;
27161da177e4SLinus Torvalds 
2717115c9b81SGreg Rose 	err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0);
271826932566SPatrick McHardy 	if (err < 0) {
271926932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in if_nlmsg_size() */
272026932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
272126932566SPatrick McHardy 		kfree_skb(skb);
272226932566SPatrick McHardy 		goto errout;
272326932566SPatrick McHardy 	}
2724395eea6cSMahesh Bandewar 	return skb;
27250ec6d3f4SThomas Graf errout:
27260ec6d3f4SThomas Graf 	if (err < 0)
27274b3da706SEric W. Biederman 		rtnl_set_sk_err(net, RTNLGRP_LINK, err);
2728395eea6cSMahesh Bandewar 	return NULL;
2729395eea6cSMahesh Bandewar }
2730395eea6cSMahesh Bandewar 
2731395eea6cSMahesh Bandewar void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
2732395eea6cSMahesh Bandewar {
2733395eea6cSMahesh Bandewar 	struct net *net = dev_net(dev);
2734395eea6cSMahesh Bandewar 
2735395eea6cSMahesh Bandewar 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
2736395eea6cSMahesh Bandewar }
2737395eea6cSMahesh Bandewar 
2738395eea6cSMahesh Bandewar void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
2739395eea6cSMahesh Bandewar 		  gfp_t flags)
2740395eea6cSMahesh Bandewar {
2741395eea6cSMahesh Bandewar 	struct sk_buff *skb;
2742395eea6cSMahesh Bandewar 
2743ed2a80abSNicolas Dichtel 	if (dev->reg_state != NETREG_REGISTERED)
2744ed2a80abSNicolas Dichtel 		return;
2745ed2a80abSNicolas Dichtel 
2746395eea6cSMahesh Bandewar 	skb = rtmsg_ifinfo_build_skb(type, dev, change, flags);
2747395eea6cSMahesh Bandewar 	if (skb)
2748395eea6cSMahesh Bandewar 		rtmsg_ifinfo_send(skb, dev, flags);
27491da177e4SLinus Torvalds }
2750471cb5a3SJiri Pirko EXPORT_SYMBOL(rtmsg_ifinfo);
27511da177e4SLinus Torvalds 
2752d83b0603SJohn Fastabend static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
2753d83b0603SJohn Fastabend 				   struct net_device *dev,
27541e53d5bbSHubert Sokolowski 				   u8 *addr, u16 vid, u32 pid, u32 seq,
27551c104a6bSNicolas Dichtel 				   int type, unsigned int flags,
2756b3379041SHubert Sokolowski 				   int nlflags, u16 ndm_state)
2757d83b0603SJohn Fastabend {
2758d83b0603SJohn Fastabend 	struct nlmsghdr *nlh;
2759d83b0603SJohn Fastabend 	struct ndmsg *ndm;
2760d83b0603SJohn Fastabend 
27611c104a6bSNicolas Dichtel 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
2762d83b0603SJohn Fastabend 	if (!nlh)
2763d83b0603SJohn Fastabend 		return -EMSGSIZE;
2764d83b0603SJohn Fastabend 
2765d83b0603SJohn Fastabend 	ndm = nlmsg_data(nlh);
2766d83b0603SJohn Fastabend 	ndm->ndm_family  = AF_BRIDGE;
2767d83b0603SJohn Fastabend 	ndm->ndm_pad1	 = 0;
2768d83b0603SJohn Fastabend 	ndm->ndm_pad2    = 0;
2769d83b0603SJohn Fastabend 	ndm->ndm_flags	 = flags;
2770d83b0603SJohn Fastabend 	ndm->ndm_type	 = 0;
2771d83b0603SJohn Fastabend 	ndm->ndm_ifindex = dev->ifindex;
2772b3379041SHubert Sokolowski 	ndm->ndm_state   = ndm_state;
2773d83b0603SJohn Fastabend 
2774d83b0603SJohn Fastabend 	if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
2775d83b0603SJohn Fastabend 		goto nla_put_failure;
27761e53d5bbSHubert Sokolowski 	if (vid)
27771e53d5bbSHubert Sokolowski 		if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
27781e53d5bbSHubert Sokolowski 			goto nla_put_failure;
2779d83b0603SJohn Fastabend 
2780053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
2781053c095aSJohannes Berg 	return 0;
2782d83b0603SJohn Fastabend 
2783d83b0603SJohn Fastabend nla_put_failure:
2784d83b0603SJohn Fastabend 	nlmsg_cancel(skb, nlh);
2785d83b0603SJohn Fastabend 	return -EMSGSIZE;
2786d83b0603SJohn Fastabend }
2787d83b0603SJohn Fastabend 
27883ff661c3SJohn Fastabend static inline size_t rtnl_fdb_nlmsg_size(void)
27893ff661c3SJohn Fastabend {
27903ff661c3SJohn Fastabend 	return NLMSG_ALIGN(sizeof(struct ndmsg)) + nla_total_size(ETH_ALEN);
27913ff661c3SJohn Fastabend }
27923ff661c3SJohn Fastabend 
2793b3379041SHubert Sokolowski static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
2794b3379041SHubert Sokolowski 			    u16 ndm_state)
27953ff661c3SJohn Fastabend {
27963ff661c3SJohn Fastabend 	struct net *net = dev_net(dev);
27973ff661c3SJohn Fastabend 	struct sk_buff *skb;
27983ff661c3SJohn Fastabend 	int err = -ENOBUFS;
27993ff661c3SJohn Fastabend 
28003ff661c3SJohn Fastabend 	skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
28013ff661c3SJohn Fastabend 	if (!skb)
28023ff661c3SJohn Fastabend 		goto errout;
28033ff661c3SJohn Fastabend 
28041e53d5bbSHubert Sokolowski 	err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
2805b3379041SHubert Sokolowski 				      0, 0, type, NTF_SELF, 0, ndm_state);
28063ff661c3SJohn Fastabend 	if (err < 0) {
28073ff661c3SJohn Fastabend 		kfree_skb(skb);
28083ff661c3SJohn Fastabend 		goto errout;
28093ff661c3SJohn Fastabend 	}
28103ff661c3SJohn Fastabend 
28113ff661c3SJohn Fastabend 	rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
28123ff661c3SJohn Fastabend 	return;
28133ff661c3SJohn Fastabend errout:
28143ff661c3SJohn Fastabend 	rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
28153ff661c3SJohn Fastabend }
28163ff661c3SJohn Fastabend 
2817090096bfSVlad Yasevich /**
2818090096bfSVlad Yasevich  * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
2819090096bfSVlad Yasevich  */
2820090096bfSVlad Yasevich int ndo_dflt_fdb_add(struct ndmsg *ndm,
2821090096bfSVlad Yasevich 		     struct nlattr *tb[],
2822090096bfSVlad Yasevich 		     struct net_device *dev,
2823f6f6424bSJiri Pirko 		     const unsigned char *addr, u16 vid,
2824090096bfSVlad Yasevich 		     u16 flags)
2825090096bfSVlad Yasevich {
2826090096bfSVlad Yasevich 	int err = -EINVAL;
2827090096bfSVlad Yasevich 
2828090096bfSVlad Yasevich 	/* If aging addresses are supported device will need to
2829090096bfSVlad Yasevich 	 * implement its own handler for this.
2830090096bfSVlad Yasevich 	 */
2831090096bfSVlad Yasevich 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
2832090096bfSVlad Yasevich 		pr_info("%s: FDB only supports static addresses\n", dev->name);
2833090096bfSVlad Yasevich 		return err;
2834090096bfSVlad Yasevich 	}
2835090096bfSVlad Yasevich 
283665891feaSOr Gerlitz 	if (vid) {
283765891feaSOr Gerlitz 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
283865891feaSOr Gerlitz 		return err;
283965891feaSOr Gerlitz 	}
284065891feaSOr Gerlitz 
2841090096bfSVlad Yasevich 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2842090096bfSVlad Yasevich 		err = dev_uc_add_excl(dev, addr);
2843090096bfSVlad Yasevich 	else if (is_multicast_ether_addr(addr))
2844090096bfSVlad Yasevich 		err = dev_mc_add_excl(dev, addr);
2845090096bfSVlad Yasevich 
2846090096bfSVlad Yasevich 	/* Only return duplicate errors if NLM_F_EXCL is set */
2847090096bfSVlad Yasevich 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
2848090096bfSVlad Yasevich 		err = 0;
2849090096bfSVlad Yasevich 
2850090096bfSVlad Yasevich 	return err;
2851090096bfSVlad Yasevich }
2852090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_add);
2853090096bfSVlad Yasevich 
2854f6f6424bSJiri Pirko static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid)
2855f6f6424bSJiri Pirko {
2856f6f6424bSJiri Pirko 	u16 vid = 0;
2857f6f6424bSJiri Pirko 
2858f6f6424bSJiri Pirko 	if (vlan_attr) {
2859f6f6424bSJiri Pirko 		if (nla_len(vlan_attr) != sizeof(u16)) {
2860f6f6424bSJiri Pirko 			pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan\n");
2861f6f6424bSJiri Pirko 			return -EINVAL;
2862f6f6424bSJiri Pirko 		}
2863f6f6424bSJiri Pirko 
2864f6f6424bSJiri Pirko 		vid = nla_get_u16(vlan_attr);
2865f6f6424bSJiri Pirko 
2866f6f6424bSJiri Pirko 		if (!vid || vid >= VLAN_VID_MASK) {
2867f6f6424bSJiri Pirko 			pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan id %d\n",
2868f6f6424bSJiri Pirko 				vid);
2869f6f6424bSJiri Pirko 			return -EINVAL;
2870f6f6424bSJiri Pirko 		}
2871f6f6424bSJiri Pirko 	}
2872f6f6424bSJiri Pirko 	*p_vid = vid;
2873f6f6424bSJiri Pirko 	return 0;
2874f6f6424bSJiri Pirko }
2875f6f6424bSJiri Pirko 
2876661d2967SThomas Graf static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh)
287777162022SJohn Fastabend {
287877162022SJohn Fastabend 	struct net *net = sock_net(skb->sk);
287977162022SJohn Fastabend 	struct ndmsg *ndm;
288077162022SJohn Fastabend 	struct nlattr *tb[NDA_MAX+1];
288177162022SJohn Fastabend 	struct net_device *dev;
288277162022SJohn Fastabend 	u8 *addr;
2883f6f6424bSJiri Pirko 	u16 vid;
288477162022SJohn Fastabend 	int err;
288577162022SJohn Fastabend 
288677162022SJohn Fastabend 	err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
288777162022SJohn Fastabend 	if (err < 0)
288877162022SJohn Fastabend 		return err;
288977162022SJohn Fastabend 
289077162022SJohn Fastabend 	ndm = nlmsg_data(nlh);
289177162022SJohn Fastabend 	if (ndm->ndm_ifindex == 0) {
289277162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n");
289377162022SJohn Fastabend 		return -EINVAL;
289477162022SJohn Fastabend 	}
289577162022SJohn Fastabend 
289677162022SJohn Fastabend 	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
289777162022SJohn Fastabend 	if (dev == NULL) {
289877162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n");
289977162022SJohn Fastabend 		return -ENODEV;
290077162022SJohn Fastabend 	}
290177162022SJohn Fastabend 
290277162022SJohn Fastabend 	if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
290377162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n");
290477162022SJohn Fastabend 		return -EINVAL;
290577162022SJohn Fastabend 	}
290677162022SJohn Fastabend 
290777162022SJohn Fastabend 	addr = nla_data(tb[NDA_LLADDR]);
290877162022SJohn Fastabend 
2909f6f6424bSJiri Pirko 	err = fdb_vid_parse(tb[NDA_VLAN], &vid);
2910f6f6424bSJiri Pirko 	if (err)
2911f6f6424bSJiri Pirko 		return err;
2912f6f6424bSJiri Pirko 
291377162022SJohn Fastabend 	err = -EOPNOTSUPP;
291477162022SJohn Fastabend 
291577162022SJohn Fastabend 	/* Support fdb on master device the net/bridge default case */
291677162022SJohn Fastabend 	if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
291777162022SJohn Fastabend 	    (dev->priv_flags & IFF_BRIDGE_PORT)) {
2918898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2919898e5061SJiri Pirko 		const struct net_device_ops *ops = br_dev->netdev_ops;
2920898e5061SJiri Pirko 
2921f6f6424bSJiri Pirko 		err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
2922f6f6424bSJiri Pirko 				       nlh->nlmsg_flags);
292377162022SJohn Fastabend 		if (err)
292477162022SJohn Fastabend 			goto out;
292577162022SJohn Fastabend 		else
292677162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_MASTER;
292777162022SJohn Fastabend 	}
292877162022SJohn Fastabend 
292977162022SJohn Fastabend 	/* Embedded bridge, macvlan, and any other device support */
2930090096bfSVlad Yasevich 	if ((ndm->ndm_flags & NTF_SELF)) {
2931090096bfSVlad Yasevich 		if (dev->netdev_ops->ndo_fdb_add)
2932090096bfSVlad Yasevich 			err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
2933f6f6424bSJiri Pirko 							   vid,
2934090096bfSVlad Yasevich 							   nlh->nlmsg_flags);
2935090096bfSVlad Yasevich 		else
2936f6f6424bSJiri Pirko 			err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
293777162022SJohn Fastabend 					       nlh->nlmsg_flags);
293877162022SJohn Fastabend 
29393ff661c3SJohn Fastabend 		if (!err) {
2940b3379041SHubert Sokolowski 			rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
2941b3379041SHubert Sokolowski 					ndm->ndm_state);
294277162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_SELF;
294377162022SJohn Fastabend 		}
29443ff661c3SJohn Fastabend 	}
294577162022SJohn Fastabend out:
294677162022SJohn Fastabend 	return err;
294777162022SJohn Fastabend }
294877162022SJohn Fastabend 
2949090096bfSVlad Yasevich /**
2950090096bfSVlad Yasevich  * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
2951090096bfSVlad Yasevich  */
2952090096bfSVlad Yasevich int ndo_dflt_fdb_del(struct ndmsg *ndm,
2953090096bfSVlad Yasevich 		     struct nlattr *tb[],
2954090096bfSVlad Yasevich 		     struct net_device *dev,
2955f6f6424bSJiri Pirko 		     const unsigned char *addr, u16 vid)
2956090096bfSVlad Yasevich {
2957c8a89c4aSAlexander Duyck 	int err = -EINVAL;
2958090096bfSVlad Yasevich 
2959090096bfSVlad Yasevich 	/* If aging addresses are supported device will need to
2960090096bfSVlad Yasevich 	 * implement its own handler for this.
2961090096bfSVlad Yasevich 	 */
296264535993SSridhar Samudrala 	if (!(ndm->ndm_state & NUD_PERMANENT)) {
2963090096bfSVlad Yasevich 		pr_info("%s: FDB only supports static addresses\n", dev->name);
2964c8a89c4aSAlexander Duyck 		return err;
2965090096bfSVlad Yasevich 	}
2966090096bfSVlad Yasevich 
2967090096bfSVlad Yasevich 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2968090096bfSVlad Yasevich 		err = dev_uc_del(dev, addr);
2969090096bfSVlad Yasevich 	else if (is_multicast_ether_addr(addr))
2970090096bfSVlad Yasevich 		err = dev_mc_del(dev, addr);
2971090096bfSVlad Yasevich 
2972090096bfSVlad Yasevich 	return err;
2973090096bfSVlad Yasevich }
2974090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_del);
2975090096bfSVlad Yasevich 
2976661d2967SThomas Graf static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh)
297777162022SJohn Fastabend {
297877162022SJohn Fastabend 	struct net *net = sock_net(skb->sk);
297977162022SJohn Fastabend 	struct ndmsg *ndm;
29801690be63SVlad Yasevich 	struct nlattr *tb[NDA_MAX+1];
298177162022SJohn Fastabend 	struct net_device *dev;
298277162022SJohn Fastabend 	int err = -EINVAL;
298377162022SJohn Fastabend 	__u8 *addr;
2984f6f6424bSJiri Pirko 	u16 vid;
298577162022SJohn Fastabend 
298690f62cf3SEric W. Biederman 	if (!netlink_capable(skb, CAP_NET_ADMIN))
29871690be63SVlad Yasevich 		return -EPERM;
29881690be63SVlad Yasevich 
29891690be63SVlad Yasevich 	err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
29901690be63SVlad Yasevich 	if (err < 0)
29911690be63SVlad Yasevich 		return err;
299277162022SJohn Fastabend 
299377162022SJohn Fastabend 	ndm = nlmsg_data(nlh);
299477162022SJohn Fastabend 	if (ndm->ndm_ifindex == 0) {
299577162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n");
299677162022SJohn Fastabend 		return -EINVAL;
299777162022SJohn Fastabend 	}
299877162022SJohn Fastabend 
299977162022SJohn Fastabend 	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
300077162022SJohn Fastabend 	if (dev == NULL) {
300177162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n");
300277162022SJohn Fastabend 		return -ENODEV;
300377162022SJohn Fastabend 	}
300477162022SJohn Fastabend 
30051690be63SVlad Yasevich 	if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
30061690be63SVlad Yasevich 		pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n");
300777162022SJohn Fastabend 		return -EINVAL;
300877162022SJohn Fastabend 	}
300977162022SJohn Fastabend 
30101690be63SVlad Yasevich 	addr = nla_data(tb[NDA_LLADDR]);
30111690be63SVlad Yasevich 
3012f6f6424bSJiri Pirko 	err = fdb_vid_parse(tb[NDA_VLAN], &vid);
3013f6f6424bSJiri Pirko 	if (err)
3014f6f6424bSJiri Pirko 		return err;
3015f6f6424bSJiri Pirko 
301677162022SJohn Fastabend 	err = -EOPNOTSUPP;
301777162022SJohn Fastabend 
301877162022SJohn Fastabend 	/* Support fdb on master device the net/bridge default case */
301977162022SJohn Fastabend 	if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
302077162022SJohn Fastabend 	    (dev->priv_flags & IFF_BRIDGE_PORT)) {
3021898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3022898e5061SJiri Pirko 		const struct net_device_ops *ops = br_dev->netdev_ops;
302377162022SJohn Fastabend 
3024898e5061SJiri Pirko 		if (ops->ndo_fdb_del)
3025f6f6424bSJiri Pirko 			err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
302677162022SJohn Fastabend 
302777162022SJohn Fastabend 		if (err)
302877162022SJohn Fastabend 			goto out;
302977162022SJohn Fastabend 		else
303077162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_MASTER;
303177162022SJohn Fastabend 	}
303277162022SJohn Fastabend 
303377162022SJohn Fastabend 	/* Embedded bridge, macvlan, and any other device support */
3034090096bfSVlad Yasevich 	if (ndm->ndm_flags & NTF_SELF) {
3035090096bfSVlad Yasevich 		if (dev->netdev_ops->ndo_fdb_del)
3036f6f6424bSJiri Pirko 			err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
3037f6f6424bSJiri Pirko 							   vid);
3038090096bfSVlad Yasevich 		else
3039f6f6424bSJiri Pirko 			err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
304077162022SJohn Fastabend 
30413ff661c3SJohn Fastabend 		if (!err) {
3042b3379041SHubert Sokolowski 			rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
3043b3379041SHubert Sokolowski 					ndm->ndm_state);
304477162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_SELF;
304577162022SJohn Fastabend 		}
30463ff661c3SJohn Fastabend 	}
304777162022SJohn Fastabend out:
304877162022SJohn Fastabend 	return err;
304977162022SJohn Fastabend }
305077162022SJohn Fastabend 
3051d83b0603SJohn Fastabend static int nlmsg_populate_fdb(struct sk_buff *skb,
3052d83b0603SJohn Fastabend 			      struct netlink_callback *cb,
3053d83b0603SJohn Fastabend 			      struct net_device *dev,
3054d83b0603SJohn Fastabend 			      int *idx,
3055d83b0603SJohn Fastabend 			      struct netdev_hw_addr_list *list)
3056d83b0603SJohn Fastabend {
3057d83b0603SJohn Fastabend 	struct netdev_hw_addr *ha;
3058d83b0603SJohn Fastabend 	int err;
305915e47304SEric W. Biederman 	u32 portid, seq;
3060d83b0603SJohn Fastabend 
306115e47304SEric W. Biederman 	portid = NETLINK_CB(cb->skb).portid;
3062d83b0603SJohn Fastabend 	seq = cb->nlh->nlmsg_seq;
3063d83b0603SJohn Fastabend 
3064d83b0603SJohn Fastabend 	list_for_each_entry(ha, &list->list, list) {
3065d83b0603SJohn Fastabend 		if (*idx < cb->args[0])
3066d83b0603SJohn Fastabend 			goto skip;
3067d83b0603SJohn Fastabend 
30681e53d5bbSHubert Sokolowski 		err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
3069a7a558feSJohn Fastabend 					      portid, seq,
30701c104a6bSNicolas Dichtel 					      RTM_NEWNEIGH, NTF_SELF,
3071b3379041SHubert Sokolowski 					      NLM_F_MULTI, NUD_PERMANENT);
3072d83b0603SJohn Fastabend 		if (err < 0)
3073d83b0603SJohn Fastabend 			return err;
3074d83b0603SJohn Fastabend skip:
3075d83b0603SJohn Fastabend 		*idx += 1;
3076d83b0603SJohn Fastabend 	}
3077d83b0603SJohn Fastabend 	return 0;
3078d83b0603SJohn Fastabend }
3079d83b0603SJohn Fastabend 
3080d83b0603SJohn Fastabend /**
30812c53040fSBen Hutchings  * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
3082d83b0603SJohn Fastabend  * @nlh: netlink message header
3083d83b0603SJohn Fastabend  * @dev: netdevice
3084d83b0603SJohn Fastabend  *
3085d83b0603SJohn Fastabend  * Default netdevice operation to dump the existing unicast address list.
308691f3e7b1SJohn Fastabend  * Returns number of addresses from list put in skb.
3087d83b0603SJohn Fastabend  */
3088d83b0603SJohn Fastabend int ndo_dflt_fdb_dump(struct sk_buff *skb,
3089d83b0603SJohn Fastabend 		      struct netlink_callback *cb,
3090d83b0603SJohn Fastabend 		      struct net_device *dev,
30915d5eacb3SJamal Hadi Salim 		      struct net_device *filter_dev,
3092d83b0603SJohn Fastabend 		      int idx)
3093d83b0603SJohn Fastabend {
3094d83b0603SJohn Fastabend 	int err;
3095d83b0603SJohn Fastabend 
3096d83b0603SJohn Fastabend 	netif_addr_lock_bh(dev);
3097d83b0603SJohn Fastabend 	err = nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->uc);
3098d83b0603SJohn Fastabend 	if (err)
3099d83b0603SJohn Fastabend 		goto out;
3100d83b0603SJohn Fastabend 	nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->mc);
3101d83b0603SJohn Fastabend out:
3102d83b0603SJohn Fastabend 	netif_addr_unlock_bh(dev);
3103472681d5SMINOURA Makoto / 箕浦 真 	cb->args[1] = err;
3104d83b0603SJohn Fastabend 	return idx;
3105d83b0603SJohn Fastabend }
3106d83b0603SJohn Fastabend EXPORT_SYMBOL(ndo_dflt_fdb_dump);
3107d83b0603SJohn Fastabend 
310877162022SJohn Fastabend static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
310977162022SJohn Fastabend {
311077162022SJohn Fastabend 	struct net_device *dev;
31115e6d2435SJamal Hadi Salim 	struct nlattr *tb[IFLA_MAX+1];
31125e6d2435SJamal Hadi Salim 	struct net_device *br_dev = NULL;
31135e6d2435SJamal Hadi Salim 	const struct net_device_ops *ops = NULL;
31145e6d2435SJamal Hadi Salim 	const struct net_device_ops *cops = NULL;
31155e6d2435SJamal Hadi Salim 	struct ifinfomsg *ifm = nlmsg_data(cb->nlh);
31165e6d2435SJamal Hadi Salim 	struct net *net = sock_net(skb->sk);
31175e6d2435SJamal Hadi Salim 	int brport_idx = 0;
31185e6d2435SJamal Hadi Salim 	int br_idx = 0;
31195e6d2435SJamal Hadi Salim 	int idx = 0;
312077162022SJohn Fastabend 
31215e6d2435SJamal Hadi Salim 	if (nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX,
31225e6d2435SJamal Hadi Salim 			ifla_policy) == 0) {
31235e6d2435SJamal Hadi Salim 		if (tb[IFLA_MASTER])
31245e6d2435SJamal Hadi Salim 			br_idx = nla_get_u32(tb[IFLA_MASTER]);
31255e6d2435SJamal Hadi Salim 	}
312677162022SJohn Fastabend 
31275e6d2435SJamal Hadi Salim 	brport_idx = ifm->ifi_index;
31285e6d2435SJamal Hadi Salim 
31295e6d2435SJamal Hadi Salim 	if (br_idx) {
31305e6d2435SJamal Hadi Salim 		br_dev = __dev_get_by_index(net, br_idx);
31315e6d2435SJamal Hadi Salim 		if (!br_dev)
31325e6d2435SJamal Hadi Salim 			return -ENODEV;
31335e6d2435SJamal Hadi Salim 
3134898e5061SJiri Pirko 		ops = br_dev->netdev_ops;
31355e6d2435SJamal Hadi Salim 	}
31365e6d2435SJamal Hadi Salim 
3137472681d5SMINOURA Makoto / 箕浦 真 	cb->args[1] = 0;
31385e6d2435SJamal Hadi Salim 	for_each_netdev(net, dev) {
31395e6d2435SJamal Hadi Salim 		if (brport_idx && (dev->ifindex != brport_idx))
31405e6d2435SJamal Hadi Salim 			continue;
31415e6d2435SJamal Hadi Salim 
31425e6d2435SJamal Hadi Salim 		if (!br_idx) { /* user did not specify a specific bridge */
31435e6d2435SJamal Hadi Salim 			if (dev->priv_flags & IFF_BRIDGE_PORT) {
31445e6d2435SJamal Hadi Salim 				br_dev = netdev_master_upper_dev_get(dev);
31455e6d2435SJamal Hadi Salim 				cops = br_dev->netdev_ops;
31465e6d2435SJamal Hadi Salim 			}
31475e6d2435SJamal Hadi Salim 
31485e6d2435SJamal Hadi Salim 		} else {
31495e6d2435SJamal Hadi Salim 			if (dev != br_dev &&
31505e6d2435SJamal Hadi Salim 			    !(dev->priv_flags & IFF_BRIDGE_PORT))
31515e6d2435SJamal Hadi Salim 				continue;
31525e6d2435SJamal Hadi Salim 
31535e6d2435SJamal Hadi Salim 			if (br_dev != netdev_master_upper_dev_get(dev) &&
31545e6d2435SJamal Hadi Salim 			    !(dev->priv_flags & IFF_EBRIDGE))
31555e6d2435SJamal Hadi Salim 				continue;
31565e6d2435SJamal Hadi Salim 
31575e6d2435SJamal Hadi Salim 			cops = ops;
31585e6d2435SJamal Hadi Salim 		}
31595e6d2435SJamal Hadi Salim 
31605e6d2435SJamal Hadi Salim 		if (dev->priv_flags & IFF_BRIDGE_PORT) {
31615e6d2435SJamal Hadi Salim 			if (cops && cops->ndo_fdb_dump)
31625e6d2435SJamal Hadi Salim 				idx = cops->ndo_fdb_dump(skb, cb, br_dev, dev,
31635d5eacb3SJamal Hadi Salim 							 idx);
316477162022SJohn Fastabend 		}
3165472681d5SMINOURA Makoto / 箕浦 真 		if (cb->args[1] == -EMSGSIZE)
3166472681d5SMINOURA Makoto / 箕浦 真 			break;
316777162022SJohn Fastabend 
31685e6d2435SJamal Hadi Salim 		if (dev->netdev_ops->ndo_fdb_dump)
31696cb69742SHubert Sokolowski 			idx = dev->netdev_ops->ndo_fdb_dump(skb, cb, dev, NULL,
31705e6d2435SJamal Hadi Salim 							    idx);
31716cb69742SHubert Sokolowski 		else
31726cb69742SHubert Sokolowski 			idx = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx);
3173472681d5SMINOURA Makoto / 箕浦 真 		if (cb->args[1] == -EMSGSIZE)
3174472681d5SMINOURA Makoto / 箕浦 真 			break;
31755e6d2435SJamal Hadi Salim 
31765e6d2435SJamal Hadi Salim 		cops = NULL;
317777162022SJohn Fastabend 	}
317877162022SJohn Fastabend 
317977162022SJohn Fastabend 	cb->args[0] = idx;
318077162022SJohn Fastabend 	return skb->len;
318177162022SJohn Fastabend }
318277162022SJohn Fastabend 
31832c3c031cSScott Feldman static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
31842c3c031cSScott Feldman 			       unsigned int attrnum, unsigned int flag)
31852c3c031cSScott Feldman {
31862c3c031cSScott Feldman 	if (mask & flag)
31872c3c031cSScott Feldman 		return nla_put_u8(skb, attrnum, !!(flags & flag));
31882c3c031cSScott Feldman 	return 0;
31892c3c031cSScott Feldman }
31902c3c031cSScott Feldman 
3191815cccbfSJohn Fastabend int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
31922c3c031cSScott Feldman 			    struct net_device *dev, u16 mode,
31937d4f8d87SScott Feldman 			    u32 flags, u32 mask, int nlflags,
31947d4f8d87SScott Feldman 			    u32 filter_mask,
31957d4f8d87SScott Feldman 			    int (*vlan_fill)(struct sk_buff *skb,
31967d4f8d87SScott Feldman 					     struct net_device *dev,
31977d4f8d87SScott Feldman 					     u32 filter_mask))
3198815cccbfSJohn Fastabend {
3199815cccbfSJohn Fastabend 	struct nlmsghdr *nlh;
3200815cccbfSJohn Fastabend 	struct ifinfomsg *ifm;
3201815cccbfSJohn Fastabend 	struct nlattr *br_afspec;
32022c3c031cSScott Feldman 	struct nlattr *protinfo;
3203815cccbfSJohn Fastabend 	u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
3204898e5061SJiri Pirko 	struct net_device *br_dev = netdev_master_upper_dev_get(dev);
32057d4f8d87SScott Feldman 	int err = 0;
3206815cccbfSJohn Fastabend 
320746c264daSNicolas Dichtel 	nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
3208815cccbfSJohn Fastabend 	if (nlh == NULL)
3209815cccbfSJohn Fastabend 		return -EMSGSIZE;
3210815cccbfSJohn Fastabend 
3211815cccbfSJohn Fastabend 	ifm = nlmsg_data(nlh);
3212815cccbfSJohn Fastabend 	ifm->ifi_family = AF_BRIDGE;
3213815cccbfSJohn Fastabend 	ifm->__ifi_pad = 0;
3214815cccbfSJohn Fastabend 	ifm->ifi_type = dev->type;
3215815cccbfSJohn Fastabend 	ifm->ifi_index = dev->ifindex;
3216815cccbfSJohn Fastabend 	ifm->ifi_flags = dev_get_flags(dev);
3217815cccbfSJohn Fastabend 	ifm->ifi_change = 0;
3218815cccbfSJohn Fastabend 
3219815cccbfSJohn Fastabend 
3220815cccbfSJohn Fastabend 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
3221815cccbfSJohn Fastabend 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
3222815cccbfSJohn Fastabend 	    nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
3223898e5061SJiri Pirko 	    (br_dev &&
3224898e5061SJiri Pirko 	     nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
3225815cccbfSJohn Fastabend 	    (dev->addr_len &&
3226815cccbfSJohn Fastabend 	     nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
3227a54acb3aSNicolas Dichtel 	    (dev->ifindex != dev_get_iflink(dev) &&
3228a54acb3aSNicolas Dichtel 	     nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
3229815cccbfSJohn Fastabend 		goto nla_put_failure;
3230815cccbfSJohn Fastabend 
3231815cccbfSJohn Fastabend 	br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
3232815cccbfSJohn Fastabend 	if (!br_afspec)
3233815cccbfSJohn Fastabend 		goto nla_put_failure;
3234815cccbfSJohn Fastabend 
32351d460b98SRoopa Prabhu 	if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
3236815cccbfSJohn Fastabend 		nla_nest_cancel(skb, br_afspec);
3237815cccbfSJohn Fastabend 		goto nla_put_failure;
3238815cccbfSJohn Fastabend 	}
32391d460b98SRoopa Prabhu 
32401d460b98SRoopa Prabhu 	if (mode != BRIDGE_MODE_UNDEF) {
32411d460b98SRoopa Prabhu 		if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
32421d460b98SRoopa Prabhu 			nla_nest_cancel(skb, br_afspec);
32431d460b98SRoopa Prabhu 			goto nla_put_failure;
32441d460b98SRoopa Prabhu 		}
32451d460b98SRoopa Prabhu 	}
32467d4f8d87SScott Feldman 	if (vlan_fill) {
32477d4f8d87SScott Feldman 		err = vlan_fill(skb, dev, filter_mask);
32487d4f8d87SScott Feldman 		if (err) {
32497d4f8d87SScott Feldman 			nla_nest_cancel(skb, br_afspec);
32507d4f8d87SScott Feldman 			goto nla_put_failure;
32517d4f8d87SScott Feldman 		}
32527d4f8d87SScott Feldman 	}
3253815cccbfSJohn Fastabend 	nla_nest_end(skb, br_afspec);
3254815cccbfSJohn Fastabend 
32552c3c031cSScott Feldman 	protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
32562c3c031cSScott Feldman 	if (!protinfo)
32572c3c031cSScott Feldman 		goto nla_put_failure;
32582c3c031cSScott Feldman 
32592c3c031cSScott Feldman 	if (brport_nla_put_flag(skb, flags, mask,
32602c3c031cSScott Feldman 				IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
32612c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
32622c3c031cSScott Feldman 				IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
32632c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
32642c3c031cSScott Feldman 				IFLA_BRPORT_FAST_LEAVE,
32652c3c031cSScott Feldman 				BR_MULTICAST_FAST_LEAVE) ||
32662c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
32672c3c031cSScott Feldman 				IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
32682c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
32692c3c031cSScott Feldman 				IFLA_BRPORT_LEARNING, BR_LEARNING) ||
32702c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
32712c3c031cSScott Feldman 				IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
32722c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
32732c3c031cSScott Feldman 				IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
32742c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
32752c3c031cSScott Feldman 				IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
32762c3c031cSScott Feldman 		nla_nest_cancel(skb, protinfo);
32772c3c031cSScott Feldman 		goto nla_put_failure;
32782c3c031cSScott Feldman 	}
32792c3c031cSScott Feldman 
32802c3c031cSScott Feldman 	nla_nest_end(skb, protinfo);
32812c3c031cSScott Feldman 
3282053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
3283053c095aSJohannes Berg 	return 0;
3284815cccbfSJohn Fastabend nla_put_failure:
3285815cccbfSJohn Fastabend 	nlmsg_cancel(skb, nlh);
32867d4f8d87SScott Feldman 	return err ? err : -EMSGSIZE;
3287815cccbfSJohn Fastabend }
32887d4f8d87SScott Feldman EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
3289815cccbfSJohn Fastabend 
3290e5a55a89SJohn Fastabend static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
3291e5a55a89SJohn Fastabend {
3292e5a55a89SJohn Fastabend 	struct net *net = sock_net(skb->sk);
3293e5a55a89SJohn Fastabend 	struct net_device *dev;
3294e5a55a89SJohn Fastabend 	int idx = 0;
3295e5a55a89SJohn Fastabend 	u32 portid = NETLINK_CB(cb->skb).portid;
3296e5a55a89SJohn Fastabend 	u32 seq = cb->nlh->nlmsg_seq;
32976cbdceebSVlad Yasevich 	u32 filter_mask = 0;
3298d64f69b0SRoopa Prabhu 	int err;
32996cbdceebSVlad Yasevich 
3300aa68c20fSThomas Graf 	if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) {
3301aa68c20fSThomas Graf 		struct nlattr *extfilt;
3302aa68c20fSThomas Graf 
33033e805ad2SAsbjoern Sloth Toennesen 		extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
33046cbdceebSVlad Yasevich 					  IFLA_EXT_MASK);
3305aa68c20fSThomas Graf 		if (extfilt) {
3306aa68c20fSThomas Graf 			if (nla_len(extfilt) < sizeof(filter_mask))
3307aa68c20fSThomas Graf 				return -EINVAL;
3308aa68c20fSThomas Graf 
33096cbdceebSVlad Yasevich 			filter_mask = nla_get_u32(extfilt);
3310aa68c20fSThomas Graf 		}
3311aa68c20fSThomas Graf 	}
3312e5a55a89SJohn Fastabend 
3313e5a55a89SJohn Fastabend 	rcu_read_lock();
3314e5a55a89SJohn Fastabend 	for_each_netdev_rcu(net, dev) {
3315e5a55a89SJohn Fastabend 		const struct net_device_ops *ops = dev->netdev_ops;
3316898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3317e5a55a89SJohn Fastabend 
3318898e5061SJiri Pirko 		if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
3319d64f69b0SRoopa Prabhu 			if (idx >= cb->args[0]) {
3320d64f69b0SRoopa Prabhu 				err = br_dev->netdev_ops->ndo_bridge_getlink(
3321d64f69b0SRoopa Prabhu 						skb, portid, seq, dev,
3322d64f69b0SRoopa Prabhu 						filter_mask, NLM_F_MULTI);
3323d64f69b0SRoopa Prabhu 				if (err < 0 && err != -EOPNOTSUPP)
3324e5a55a89SJohn Fastabend 					break;
3325d64f69b0SRoopa Prabhu 			}
3326e5a55a89SJohn Fastabend 			idx++;
3327e5a55a89SJohn Fastabend 		}
3328e5a55a89SJohn Fastabend 
3329e5a55a89SJohn Fastabend 		if (ops->ndo_bridge_getlink) {
3330d64f69b0SRoopa Prabhu 			if (idx >= cb->args[0]) {
3331d64f69b0SRoopa Prabhu 				err = ops->ndo_bridge_getlink(skb, portid,
3332d64f69b0SRoopa Prabhu 							      seq, dev,
333346c264daSNicolas Dichtel 							      filter_mask,
3334d64f69b0SRoopa Prabhu 							      NLM_F_MULTI);
3335d64f69b0SRoopa Prabhu 				if (err < 0 && err != -EOPNOTSUPP)
3336e5a55a89SJohn Fastabend 					break;
3337d64f69b0SRoopa Prabhu 			}
3338e5a55a89SJohn Fastabend 			idx++;
3339e5a55a89SJohn Fastabend 		}
3340e5a55a89SJohn Fastabend 	}
3341e5a55a89SJohn Fastabend 	rcu_read_unlock();
3342e5a55a89SJohn Fastabend 	cb->args[0] = idx;
3343e5a55a89SJohn Fastabend 
3344e5a55a89SJohn Fastabend 	return skb->len;
3345e5a55a89SJohn Fastabend }
3346e5a55a89SJohn Fastabend 
33472469ffd7SJohn Fastabend static inline size_t bridge_nlmsg_size(void)
33482469ffd7SJohn Fastabend {
33492469ffd7SJohn Fastabend 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
33502469ffd7SJohn Fastabend 		+ nla_total_size(IFNAMSIZ)	/* IFLA_IFNAME */
33512469ffd7SJohn Fastabend 		+ nla_total_size(MAX_ADDR_LEN)	/* IFLA_ADDRESS */
33522469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_MASTER */
33532469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_MTU */
33542469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_LINK */
33552469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_OPERSTATE */
33562469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u8))	/* IFLA_PROTINFO */
33572469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(struct nlattr))	/* IFLA_AF_SPEC */
33582469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u16))	/* IFLA_BRIDGE_FLAGS */
33592469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u16));	/* IFLA_BRIDGE_MODE */
33602469ffd7SJohn Fastabend }
33612469ffd7SJohn Fastabend 
336202dba438SRoopa Prabhu static int rtnl_bridge_notify(struct net_device *dev)
33632469ffd7SJohn Fastabend {
33642469ffd7SJohn Fastabend 	struct net *net = dev_net(dev);
33652469ffd7SJohn Fastabend 	struct sk_buff *skb;
33662469ffd7SJohn Fastabend 	int err = -EOPNOTSUPP;
33672469ffd7SJohn Fastabend 
336802dba438SRoopa Prabhu 	if (!dev->netdev_ops->ndo_bridge_getlink)
336902dba438SRoopa Prabhu 		return 0;
337002dba438SRoopa Prabhu 
33712469ffd7SJohn Fastabend 	skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
33722469ffd7SJohn Fastabend 	if (!skb) {
33732469ffd7SJohn Fastabend 		err = -ENOMEM;
33742469ffd7SJohn Fastabend 		goto errout;
33752469ffd7SJohn Fastabend 	}
33762469ffd7SJohn Fastabend 
337746c264daSNicolas Dichtel 	err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
3378c38e01b8SJohn Fastabend 	if (err < 0)
3379c38e01b8SJohn Fastabend 		goto errout;
33802469ffd7SJohn Fastabend 
338159ccaaaaSRoopa Prabhu 	if (!skb->len)
338259ccaaaaSRoopa Prabhu 		goto errout;
338359ccaaaaSRoopa Prabhu 
33842469ffd7SJohn Fastabend 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
33852469ffd7SJohn Fastabend 	return 0;
33862469ffd7SJohn Fastabend errout:
33872469ffd7SJohn Fastabend 	WARN_ON(err == -EMSGSIZE);
33882469ffd7SJohn Fastabend 	kfree_skb(skb);
338959ccaaaaSRoopa Prabhu 	if (err)
33902469ffd7SJohn Fastabend 		rtnl_set_sk_err(net, RTNLGRP_LINK, err);
33912469ffd7SJohn Fastabend 	return err;
33922469ffd7SJohn Fastabend }
33932469ffd7SJohn Fastabend 
3394661d2967SThomas Graf static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
3395e5a55a89SJohn Fastabend {
3396e5a55a89SJohn Fastabend 	struct net *net = sock_net(skb->sk);
3397e5a55a89SJohn Fastabend 	struct ifinfomsg *ifm;
3398e5a55a89SJohn Fastabend 	struct net_device *dev;
33992469ffd7SJohn Fastabend 	struct nlattr *br_spec, *attr = NULL;
34002469ffd7SJohn Fastabend 	int rem, err = -EOPNOTSUPP;
34014de8b413SRosen, Rami 	u16 flags = 0;
3402c38e01b8SJohn Fastabend 	bool have_flags = false;
3403e5a55a89SJohn Fastabend 
3404e5a55a89SJohn Fastabend 	if (nlmsg_len(nlh) < sizeof(*ifm))
3405e5a55a89SJohn Fastabend 		return -EINVAL;
3406e5a55a89SJohn Fastabend 
3407e5a55a89SJohn Fastabend 	ifm = nlmsg_data(nlh);
3408e5a55a89SJohn Fastabend 	if (ifm->ifi_family != AF_BRIDGE)
3409e5a55a89SJohn Fastabend 		return -EPFNOSUPPORT;
3410e5a55a89SJohn Fastabend 
3411e5a55a89SJohn Fastabend 	dev = __dev_get_by_index(net, ifm->ifi_index);
3412e5a55a89SJohn Fastabend 	if (!dev) {
3413e5a55a89SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3414e5a55a89SJohn Fastabend 		return -ENODEV;
3415e5a55a89SJohn Fastabend 	}
3416e5a55a89SJohn Fastabend 
34172469ffd7SJohn Fastabend 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
34182469ffd7SJohn Fastabend 	if (br_spec) {
34192469ffd7SJohn Fastabend 		nla_for_each_nested(attr, br_spec, rem) {
34202469ffd7SJohn Fastabend 			if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
34216e8d1c55SThomas Graf 				if (nla_len(attr) < sizeof(flags))
34226e8d1c55SThomas Graf 					return -EINVAL;
34236e8d1c55SThomas Graf 
3424c38e01b8SJohn Fastabend 				have_flags = true;
34252469ffd7SJohn Fastabend 				flags = nla_get_u16(attr);
34262469ffd7SJohn Fastabend 				break;
34272469ffd7SJohn Fastabend 			}
34282469ffd7SJohn Fastabend 		}
34292469ffd7SJohn Fastabend 	}
34302469ffd7SJohn Fastabend 
34312469ffd7SJohn Fastabend 	if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
3432898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3433898e5061SJiri Pirko 
3434898e5061SJiri Pirko 		if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
34352469ffd7SJohn Fastabend 			err = -EOPNOTSUPP;
3436e5a55a89SJohn Fastabend 			goto out;
3437e5a55a89SJohn Fastabend 		}
3438e5a55a89SJohn Fastabend 
3439add511b3SRoopa Prabhu 		err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags);
34402469ffd7SJohn Fastabend 		if (err)
34412469ffd7SJohn Fastabend 			goto out;
34422469ffd7SJohn Fastabend 
34432469ffd7SJohn Fastabend 		flags &= ~BRIDGE_FLAGS_MASTER;
34442469ffd7SJohn Fastabend 	}
34452469ffd7SJohn Fastabend 
34462469ffd7SJohn Fastabend 	if ((flags & BRIDGE_FLAGS_SELF)) {
34472469ffd7SJohn Fastabend 		if (!dev->netdev_ops->ndo_bridge_setlink)
34482469ffd7SJohn Fastabend 			err = -EOPNOTSUPP;
34492469ffd7SJohn Fastabend 		else
3450add511b3SRoopa Prabhu 			err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
3451add511b3SRoopa Prabhu 								  flags);
345202dba438SRoopa Prabhu 		if (!err) {
34532469ffd7SJohn Fastabend 			flags &= ~BRIDGE_FLAGS_SELF;
345402dba438SRoopa Prabhu 
345502dba438SRoopa Prabhu 			/* Generate event to notify upper layer of bridge
345602dba438SRoopa Prabhu 			 * change
345702dba438SRoopa Prabhu 			 */
345802dba438SRoopa Prabhu 			err = rtnl_bridge_notify(dev);
345902dba438SRoopa Prabhu 		}
34602469ffd7SJohn Fastabend 	}
34612469ffd7SJohn Fastabend 
3462c38e01b8SJohn Fastabend 	if (have_flags)
34632469ffd7SJohn Fastabend 		memcpy(nla_data(attr), &flags, sizeof(flags));
3464e5a55a89SJohn Fastabend out:
3465e5a55a89SJohn Fastabend 	return err;
3466e5a55a89SJohn Fastabend }
3467e5a55a89SJohn Fastabend 
3468661d2967SThomas Graf static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
3469407af329SVlad Yasevich {
3470407af329SVlad Yasevich 	struct net *net = sock_net(skb->sk);
3471407af329SVlad Yasevich 	struct ifinfomsg *ifm;
3472407af329SVlad Yasevich 	struct net_device *dev;
3473407af329SVlad Yasevich 	struct nlattr *br_spec, *attr = NULL;
3474407af329SVlad Yasevich 	int rem, err = -EOPNOTSUPP;
34754de8b413SRosen, Rami 	u16 flags = 0;
3476407af329SVlad Yasevich 	bool have_flags = false;
3477407af329SVlad Yasevich 
3478407af329SVlad Yasevich 	if (nlmsg_len(nlh) < sizeof(*ifm))
3479407af329SVlad Yasevich 		return -EINVAL;
3480407af329SVlad Yasevich 
3481407af329SVlad Yasevich 	ifm = nlmsg_data(nlh);
3482407af329SVlad Yasevich 	if (ifm->ifi_family != AF_BRIDGE)
3483407af329SVlad Yasevich 		return -EPFNOSUPPORT;
3484407af329SVlad Yasevich 
3485407af329SVlad Yasevich 	dev = __dev_get_by_index(net, ifm->ifi_index);
3486407af329SVlad Yasevich 	if (!dev) {
3487407af329SVlad Yasevich 		pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3488407af329SVlad Yasevich 		return -ENODEV;
3489407af329SVlad Yasevich 	}
3490407af329SVlad Yasevich 
3491407af329SVlad Yasevich 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3492407af329SVlad Yasevich 	if (br_spec) {
3493407af329SVlad Yasevich 		nla_for_each_nested(attr, br_spec, rem) {
3494407af329SVlad Yasevich 			if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
34956e8d1c55SThomas Graf 				if (nla_len(attr) < sizeof(flags))
34966e8d1c55SThomas Graf 					return -EINVAL;
34976e8d1c55SThomas Graf 
3498407af329SVlad Yasevich 				have_flags = true;
3499407af329SVlad Yasevich 				flags = nla_get_u16(attr);
3500407af329SVlad Yasevich 				break;
3501407af329SVlad Yasevich 			}
3502407af329SVlad Yasevich 		}
3503407af329SVlad Yasevich 	}
3504407af329SVlad Yasevich 
3505407af329SVlad Yasevich 	if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
3506407af329SVlad Yasevich 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3507407af329SVlad Yasevich 
3508407af329SVlad Yasevich 		if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
3509407af329SVlad Yasevich 			err = -EOPNOTSUPP;
3510407af329SVlad Yasevich 			goto out;
3511407af329SVlad Yasevich 		}
3512407af329SVlad Yasevich 
3513add511b3SRoopa Prabhu 		err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
3514407af329SVlad Yasevich 		if (err)
3515407af329SVlad Yasevich 			goto out;
3516407af329SVlad Yasevich 
3517407af329SVlad Yasevich 		flags &= ~BRIDGE_FLAGS_MASTER;
3518407af329SVlad Yasevich 	}
3519407af329SVlad Yasevich 
3520407af329SVlad Yasevich 	if ((flags & BRIDGE_FLAGS_SELF)) {
3521407af329SVlad Yasevich 		if (!dev->netdev_ops->ndo_bridge_dellink)
3522407af329SVlad Yasevich 			err = -EOPNOTSUPP;
3523407af329SVlad Yasevich 		else
3524add511b3SRoopa Prabhu 			err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
3525add511b3SRoopa Prabhu 								  flags);
3526407af329SVlad Yasevich 
352702dba438SRoopa Prabhu 		if (!err) {
3528407af329SVlad Yasevich 			flags &= ~BRIDGE_FLAGS_SELF;
352902dba438SRoopa Prabhu 
353002dba438SRoopa Prabhu 			/* Generate event to notify upper layer of bridge
353102dba438SRoopa Prabhu 			 * change
353202dba438SRoopa Prabhu 			 */
353302dba438SRoopa Prabhu 			err = rtnl_bridge_notify(dev);
353402dba438SRoopa Prabhu 		}
3535407af329SVlad Yasevich 	}
3536407af329SVlad Yasevich 
3537407af329SVlad Yasevich 	if (have_flags)
3538407af329SVlad Yasevich 		memcpy(nla_data(attr), &flags, sizeof(flags));
3539407af329SVlad Yasevich out:
3540407af329SVlad Yasevich 	return err;
3541407af329SVlad Yasevich }
3542407af329SVlad Yasevich 
3543e8872a25SNikolay Aleksandrov static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
3544e8872a25SNikolay Aleksandrov {
3545e8872a25SNikolay Aleksandrov 	return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
3546e8872a25SNikolay Aleksandrov 	       (!idxattr || idxattr == attrid);
3547e8872a25SNikolay Aleksandrov }
3548e8872a25SNikolay Aleksandrov 
354910c9ead9SRoopa Prabhu static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
355010c9ead9SRoopa Prabhu 			       int type, u32 pid, u32 seq, u32 change,
3551e8872a25SNikolay Aleksandrov 			       unsigned int flags, unsigned int filter_mask,
3552e8872a25SNikolay Aleksandrov 			       int *idxattr, int *prividx)
355310c9ead9SRoopa Prabhu {
355410c9ead9SRoopa Prabhu 	struct if_stats_msg *ifsm;
355510c9ead9SRoopa Prabhu 	struct nlmsghdr *nlh;
355610c9ead9SRoopa Prabhu 	struct nlattr *attr;
3557e8872a25SNikolay Aleksandrov 	int s_prividx = *prividx;
355810c9ead9SRoopa Prabhu 
355910c9ead9SRoopa Prabhu 	ASSERT_RTNL();
356010c9ead9SRoopa Prabhu 
356110c9ead9SRoopa Prabhu 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
356210c9ead9SRoopa Prabhu 	if (!nlh)
356310c9ead9SRoopa Prabhu 		return -EMSGSIZE;
356410c9ead9SRoopa Prabhu 
356510c9ead9SRoopa Prabhu 	ifsm = nlmsg_data(nlh);
356610c9ead9SRoopa Prabhu 	ifsm->ifindex = dev->ifindex;
356710c9ead9SRoopa Prabhu 	ifsm->filter_mask = filter_mask;
356810c9ead9SRoopa Prabhu 
3569e8872a25SNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
357010c9ead9SRoopa Prabhu 		struct rtnl_link_stats64 *sp;
357110c9ead9SRoopa Prabhu 
357258414d32SNicolas Dichtel 		attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
357358414d32SNicolas Dichtel 					 sizeof(struct rtnl_link_stats64),
357458414d32SNicolas Dichtel 					 IFLA_STATS_UNSPEC);
357510c9ead9SRoopa Prabhu 		if (!attr)
357610c9ead9SRoopa Prabhu 			goto nla_put_failure;
357710c9ead9SRoopa Prabhu 
357810c9ead9SRoopa Prabhu 		sp = nla_data(attr);
357910c9ead9SRoopa Prabhu 		dev_get_stats(dev, sp);
358010c9ead9SRoopa Prabhu 	}
358110c9ead9SRoopa Prabhu 
358297a47facSNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
358397a47facSNikolay Aleksandrov 		const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
358497a47facSNikolay Aleksandrov 
358597a47facSNikolay Aleksandrov 		if (ops && ops->fill_linkxstats) {
358697a47facSNikolay Aleksandrov 			int err;
358797a47facSNikolay Aleksandrov 
358897a47facSNikolay Aleksandrov 			*idxattr = IFLA_STATS_LINK_XSTATS;
358997a47facSNikolay Aleksandrov 			attr = nla_nest_start(skb,
359097a47facSNikolay Aleksandrov 					      IFLA_STATS_LINK_XSTATS);
359197a47facSNikolay Aleksandrov 			if (!attr)
359297a47facSNikolay Aleksandrov 				goto nla_put_failure;
359397a47facSNikolay Aleksandrov 
359480e73cc5SNikolay Aleksandrov 			err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
359580e73cc5SNikolay Aleksandrov 			nla_nest_end(skb, attr);
359680e73cc5SNikolay Aleksandrov 			if (err)
359780e73cc5SNikolay Aleksandrov 				goto nla_put_failure;
359880e73cc5SNikolay Aleksandrov 			*idxattr = 0;
359980e73cc5SNikolay Aleksandrov 		}
360080e73cc5SNikolay Aleksandrov 	}
360180e73cc5SNikolay Aleksandrov 
360280e73cc5SNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
360380e73cc5SNikolay Aleksandrov 			     *idxattr)) {
360480e73cc5SNikolay Aleksandrov 		const struct rtnl_link_ops *ops = NULL;
360580e73cc5SNikolay Aleksandrov 		const struct net_device *master;
360680e73cc5SNikolay Aleksandrov 
360780e73cc5SNikolay Aleksandrov 		master = netdev_master_upper_dev_get(dev);
360880e73cc5SNikolay Aleksandrov 		if (master)
360980e73cc5SNikolay Aleksandrov 			ops = master->rtnl_link_ops;
361080e73cc5SNikolay Aleksandrov 		if (ops && ops->fill_linkxstats) {
361180e73cc5SNikolay Aleksandrov 			int err;
361280e73cc5SNikolay Aleksandrov 
361380e73cc5SNikolay Aleksandrov 			*idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
361480e73cc5SNikolay Aleksandrov 			attr = nla_nest_start(skb,
361580e73cc5SNikolay Aleksandrov 					      IFLA_STATS_LINK_XSTATS_SLAVE);
361680e73cc5SNikolay Aleksandrov 			if (!attr)
361780e73cc5SNikolay Aleksandrov 				goto nla_put_failure;
361880e73cc5SNikolay Aleksandrov 
361980e73cc5SNikolay Aleksandrov 			err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
362097a47facSNikolay Aleksandrov 			nla_nest_end(skb, attr);
362197a47facSNikolay Aleksandrov 			if (err)
362297a47facSNikolay Aleksandrov 				goto nla_put_failure;
362397a47facSNikolay Aleksandrov 			*idxattr = 0;
362497a47facSNikolay Aleksandrov 		}
362597a47facSNikolay Aleksandrov 	}
362697a47facSNikolay Aleksandrov 
362710c9ead9SRoopa Prabhu 	nlmsg_end(skb, nlh);
362810c9ead9SRoopa Prabhu 
362910c9ead9SRoopa Prabhu 	return 0;
363010c9ead9SRoopa Prabhu 
363110c9ead9SRoopa Prabhu nla_put_failure:
3632e8872a25SNikolay Aleksandrov 	/* not a multi message or no progress mean a real error */
3633e8872a25SNikolay Aleksandrov 	if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
363410c9ead9SRoopa Prabhu 		nlmsg_cancel(skb, nlh);
3635e8872a25SNikolay Aleksandrov 	else
3636e8872a25SNikolay Aleksandrov 		nlmsg_end(skb, nlh);
363710c9ead9SRoopa Prabhu 
363810c9ead9SRoopa Prabhu 	return -EMSGSIZE;
363910c9ead9SRoopa Prabhu }
364010c9ead9SRoopa Prabhu 
364110c9ead9SRoopa Prabhu static const struct nla_policy ifla_stats_policy[IFLA_STATS_MAX + 1] = {
364210c9ead9SRoopa Prabhu 	[IFLA_STATS_LINK_64]	= { .len = sizeof(struct rtnl_link_stats64) },
364310c9ead9SRoopa Prabhu };
364410c9ead9SRoopa Prabhu 
364510c9ead9SRoopa Prabhu static size_t if_nlmsg_stats_size(const struct net_device *dev,
364610c9ead9SRoopa Prabhu 				  u32 filter_mask)
364710c9ead9SRoopa Prabhu {
364810c9ead9SRoopa Prabhu 	size_t size = 0;
364910c9ead9SRoopa Prabhu 
3650e8872a25SNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
365110c9ead9SRoopa Prabhu 		size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
365210c9ead9SRoopa Prabhu 
365397a47facSNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
365497a47facSNikolay Aleksandrov 		const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
365580e73cc5SNikolay Aleksandrov 		int attr = IFLA_STATS_LINK_XSTATS;
365697a47facSNikolay Aleksandrov 
365797a47facSNikolay Aleksandrov 		if (ops && ops->get_linkxstats_size) {
365880e73cc5SNikolay Aleksandrov 			size += nla_total_size(ops->get_linkxstats_size(dev,
365980e73cc5SNikolay Aleksandrov 									attr));
366097a47facSNikolay Aleksandrov 			/* for IFLA_STATS_LINK_XSTATS */
366197a47facSNikolay Aleksandrov 			size += nla_total_size(0);
366297a47facSNikolay Aleksandrov 		}
366397a47facSNikolay Aleksandrov 	}
366497a47facSNikolay Aleksandrov 
366580e73cc5SNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
366680e73cc5SNikolay Aleksandrov 		struct net_device *_dev = (struct net_device *)dev;
366780e73cc5SNikolay Aleksandrov 		const struct rtnl_link_ops *ops = NULL;
366880e73cc5SNikolay Aleksandrov 		const struct net_device *master;
366980e73cc5SNikolay Aleksandrov 
367080e73cc5SNikolay Aleksandrov 		/* netdev_master_upper_dev_get can't take const */
367180e73cc5SNikolay Aleksandrov 		master = netdev_master_upper_dev_get(_dev);
367280e73cc5SNikolay Aleksandrov 		if (master)
367380e73cc5SNikolay Aleksandrov 			ops = master->rtnl_link_ops;
367480e73cc5SNikolay Aleksandrov 		if (ops && ops->get_linkxstats_size) {
367580e73cc5SNikolay Aleksandrov 			int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
367680e73cc5SNikolay Aleksandrov 
367780e73cc5SNikolay Aleksandrov 			size += nla_total_size(ops->get_linkxstats_size(dev,
367880e73cc5SNikolay Aleksandrov 									attr));
367980e73cc5SNikolay Aleksandrov 			/* for IFLA_STATS_LINK_XSTATS_SLAVE */
368080e73cc5SNikolay Aleksandrov 			size += nla_total_size(0);
368180e73cc5SNikolay Aleksandrov 		}
368280e73cc5SNikolay Aleksandrov 	}
368380e73cc5SNikolay Aleksandrov 
368410c9ead9SRoopa Prabhu 	return size;
368510c9ead9SRoopa Prabhu }
368610c9ead9SRoopa Prabhu 
368710c9ead9SRoopa Prabhu static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh)
368810c9ead9SRoopa Prabhu {
368910c9ead9SRoopa Prabhu 	struct net *net = sock_net(skb->sk);
369010c9ead9SRoopa Prabhu 	struct net_device *dev = NULL;
3691e8872a25SNikolay Aleksandrov 	int idxattr = 0, prividx = 0;
3692e8872a25SNikolay Aleksandrov 	struct if_stats_msg *ifsm;
369310c9ead9SRoopa Prabhu 	struct sk_buff *nskb;
369410c9ead9SRoopa Prabhu 	u32 filter_mask;
369510c9ead9SRoopa Prabhu 	int err;
369610c9ead9SRoopa Prabhu 
369710c9ead9SRoopa Prabhu 	ifsm = nlmsg_data(nlh);
369810c9ead9SRoopa Prabhu 	if (ifsm->ifindex > 0)
369910c9ead9SRoopa Prabhu 		dev = __dev_get_by_index(net, ifsm->ifindex);
370010c9ead9SRoopa Prabhu 	else
370110c9ead9SRoopa Prabhu 		return -EINVAL;
370210c9ead9SRoopa Prabhu 
370310c9ead9SRoopa Prabhu 	if (!dev)
370410c9ead9SRoopa Prabhu 		return -ENODEV;
370510c9ead9SRoopa Prabhu 
370610c9ead9SRoopa Prabhu 	filter_mask = ifsm->filter_mask;
370710c9ead9SRoopa Prabhu 	if (!filter_mask)
370810c9ead9SRoopa Prabhu 		return -EINVAL;
370910c9ead9SRoopa Prabhu 
371010c9ead9SRoopa Prabhu 	nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
371110c9ead9SRoopa Prabhu 	if (!nskb)
371210c9ead9SRoopa Prabhu 		return -ENOBUFS;
371310c9ead9SRoopa Prabhu 
371410c9ead9SRoopa Prabhu 	err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
371510c9ead9SRoopa Prabhu 				  NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
3716e8872a25SNikolay Aleksandrov 				  0, filter_mask, &idxattr, &prividx);
371710c9ead9SRoopa Prabhu 	if (err < 0) {
371810c9ead9SRoopa Prabhu 		/* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
371910c9ead9SRoopa Prabhu 		WARN_ON(err == -EMSGSIZE);
372010c9ead9SRoopa Prabhu 		kfree_skb(nskb);
372110c9ead9SRoopa Prabhu 	} else {
372210c9ead9SRoopa Prabhu 		err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
372310c9ead9SRoopa Prabhu 	}
372410c9ead9SRoopa Prabhu 
372510c9ead9SRoopa Prabhu 	return err;
372610c9ead9SRoopa Prabhu }
372710c9ead9SRoopa Prabhu 
372810c9ead9SRoopa Prabhu static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
372910c9ead9SRoopa Prabhu {
3730e8872a25SNikolay Aleksandrov 	int h, s_h, err, s_idx, s_idxattr, s_prividx;
373110c9ead9SRoopa Prabhu 	struct net *net = sock_net(skb->sk);
373210c9ead9SRoopa Prabhu 	unsigned int flags = NLM_F_MULTI;
3733e8872a25SNikolay Aleksandrov 	struct if_stats_msg *ifsm;
3734e8872a25SNikolay Aleksandrov 	struct hlist_head *head;
3735e8872a25SNikolay Aleksandrov 	struct net_device *dev;
373610c9ead9SRoopa Prabhu 	u32 filter_mask = 0;
3737e8872a25SNikolay Aleksandrov 	int idx = 0;
373810c9ead9SRoopa Prabhu 
373910c9ead9SRoopa Prabhu 	s_h = cb->args[0];
374010c9ead9SRoopa Prabhu 	s_idx = cb->args[1];
3741e8872a25SNikolay Aleksandrov 	s_idxattr = cb->args[2];
3742e8872a25SNikolay Aleksandrov 	s_prividx = cb->args[3];
374310c9ead9SRoopa Prabhu 
374410c9ead9SRoopa Prabhu 	cb->seq = net->dev_base_seq;
374510c9ead9SRoopa Prabhu 
374610c9ead9SRoopa Prabhu 	ifsm = nlmsg_data(cb->nlh);
374710c9ead9SRoopa Prabhu 	filter_mask = ifsm->filter_mask;
374810c9ead9SRoopa Prabhu 	if (!filter_mask)
374910c9ead9SRoopa Prabhu 		return -EINVAL;
375010c9ead9SRoopa Prabhu 
375110c9ead9SRoopa Prabhu 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
375210c9ead9SRoopa Prabhu 		idx = 0;
375310c9ead9SRoopa Prabhu 		head = &net->dev_index_head[h];
375410c9ead9SRoopa Prabhu 		hlist_for_each_entry(dev, head, index_hlist) {
375510c9ead9SRoopa Prabhu 			if (idx < s_idx)
375610c9ead9SRoopa Prabhu 				goto cont;
375710c9ead9SRoopa Prabhu 			err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
375810c9ead9SRoopa Prabhu 						  NETLINK_CB(cb->skb).portid,
375910c9ead9SRoopa Prabhu 						  cb->nlh->nlmsg_seq, 0,
3760e8872a25SNikolay Aleksandrov 						  flags, filter_mask,
3761e8872a25SNikolay Aleksandrov 						  &s_idxattr, &s_prividx);
376210c9ead9SRoopa Prabhu 			/* If we ran out of room on the first message,
376310c9ead9SRoopa Prabhu 			 * we're in trouble
376410c9ead9SRoopa Prabhu 			 */
376510c9ead9SRoopa Prabhu 			WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
376610c9ead9SRoopa Prabhu 
376710c9ead9SRoopa Prabhu 			if (err < 0)
376810c9ead9SRoopa Prabhu 				goto out;
3769e8872a25SNikolay Aleksandrov 			s_prividx = 0;
3770e8872a25SNikolay Aleksandrov 			s_idxattr = 0;
377110c9ead9SRoopa Prabhu 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
377210c9ead9SRoopa Prabhu cont:
377310c9ead9SRoopa Prabhu 			idx++;
377410c9ead9SRoopa Prabhu 		}
377510c9ead9SRoopa Prabhu 	}
377610c9ead9SRoopa Prabhu out:
3777e8872a25SNikolay Aleksandrov 	cb->args[3] = s_prividx;
3778e8872a25SNikolay Aleksandrov 	cb->args[2] = s_idxattr;
377910c9ead9SRoopa Prabhu 	cb->args[1] = idx;
378010c9ead9SRoopa Prabhu 	cb->args[0] = h;
378110c9ead9SRoopa Prabhu 
378210c9ead9SRoopa Prabhu 	return skb->len;
378310c9ead9SRoopa Prabhu }
378410c9ead9SRoopa Prabhu 
37851da177e4SLinus Torvalds /* Process one rtnetlink message. */
37861da177e4SLinus Torvalds 
37871d00a4ebSThomas Graf static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
37881da177e4SLinus Torvalds {
37893b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
3790e2849863SThomas Graf 	rtnl_doit_func doit;
3791617cfc75SAlexander Kuleshov 	int kind;
37921da177e4SLinus Torvalds 	int family;
37931da177e4SLinus Torvalds 	int type;
37942907c35fSEric Dumazet 	int err;
37951da177e4SLinus Torvalds 
37961da177e4SLinus Torvalds 	type = nlh->nlmsg_type;
37971da177e4SLinus Torvalds 	if (type > RTM_MAX)
3798038890feSThomas Graf 		return -EOPNOTSUPP;
37991da177e4SLinus Torvalds 
38001da177e4SLinus Torvalds 	type -= RTM_BASE;
38011da177e4SLinus Torvalds 
38021da177e4SLinus Torvalds 	/* All the messages must have at least 1 byte length */
3803573ce260SHong zhi guo 	if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
38041da177e4SLinus Torvalds 		return 0;
38051da177e4SLinus Torvalds 
3806573ce260SHong zhi guo 	family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
38071da177e4SLinus Torvalds 	kind = type&3;
38081da177e4SLinus Torvalds 
380990f62cf3SEric W. Biederman 	if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
38101d00a4ebSThomas Graf 		return -EPERM;
38111da177e4SLinus Torvalds 
3812b8f3ab42SDavid S. Miller 	if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
381397c53cacSDenis V. Lunev 		struct sock *rtnl;
3814e2849863SThomas Graf 		rtnl_dumpit_func dumpit;
3815c7ac8679SGreg Rose 		rtnl_calcit_func calcit;
3816c7ac8679SGreg Rose 		u16 min_dump_alloc = 0;
38171da177e4SLinus Torvalds 
3818e2849863SThomas Graf 		dumpit = rtnl_get_dumpit(family, type);
3819e2849863SThomas Graf 		if (dumpit == NULL)
3820038890feSThomas Graf 			return -EOPNOTSUPP;
3821c7ac8679SGreg Rose 		calcit = rtnl_get_calcit(family, type);
3822c7ac8679SGreg Rose 		if (calcit)
3823115c9b81SGreg Rose 			min_dump_alloc = calcit(skb, nlh);
38241da177e4SLinus Torvalds 
38252907c35fSEric Dumazet 		__rtnl_unlock();
382697c53cacSDenis V. Lunev 		rtnl = net->rtnl;
382780d326faSPablo Neira Ayuso 		{
382880d326faSPablo Neira Ayuso 			struct netlink_dump_control c = {
382980d326faSPablo Neira Ayuso 				.dump		= dumpit,
383080d326faSPablo Neira Ayuso 				.min_dump_alloc	= min_dump_alloc,
383180d326faSPablo Neira Ayuso 			};
383280d326faSPablo Neira Ayuso 			err = netlink_dump_start(rtnl, skb, nlh, &c);
383380d326faSPablo Neira Ayuso 		}
38342907c35fSEric Dumazet 		rtnl_lock();
38352907c35fSEric Dumazet 		return err;
38361da177e4SLinus Torvalds 	}
38371da177e4SLinus Torvalds 
3838e2849863SThomas Graf 	doit = rtnl_get_doit(family, type);
3839e2849863SThomas Graf 	if (doit == NULL)
3840038890feSThomas Graf 		return -EOPNOTSUPP;
38411da177e4SLinus Torvalds 
3842661d2967SThomas Graf 	return doit(skb, nlh);
38431da177e4SLinus Torvalds }
38441da177e4SLinus Torvalds 
3845cd40b7d3SDenis V. Lunev static void rtnetlink_rcv(struct sk_buff *skb)
38461da177e4SLinus Torvalds {
38471536cc0dSDenis V. Lunev 	rtnl_lock();
3848cd40b7d3SDenis V. Lunev 	netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
38491536cc0dSDenis V. Lunev 	rtnl_unlock();
38501da177e4SLinus Torvalds }
38511da177e4SLinus Torvalds 
38521da177e4SLinus Torvalds static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
38531da177e4SLinus Torvalds {
3854351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3855e9dc8653SEric W. Biederman 
38561da177e4SLinus Torvalds 	switch (event) {
38571da177e4SLinus Torvalds 	case NETDEV_UP:
38581da177e4SLinus Torvalds 	case NETDEV_DOWN:
385910de05afSPatrick McHardy 	case NETDEV_PRE_UP:
3860d90a909eSEric W. Biederman 	case NETDEV_POST_INIT:
3861d90a909eSEric W. Biederman 	case NETDEV_REGISTER:
38621da177e4SLinus Torvalds 	case NETDEV_CHANGE:
3863755d0e77SPatrick McHardy 	case NETDEV_PRE_TYPE_CHANGE:
38641da177e4SLinus Torvalds 	case NETDEV_GOING_DOWN:
3865a2835763SPatrick McHardy 	case NETDEV_UNREGISTER:
38660115e8e3SEric Dumazet 	case NETDEV_UNREGISTER_FINAL:
3867ac3d3f81SAmerigo Wang 	case NETDEV_RELEASE:
3868ac3d3f81SAmerigo Wang 	case NETDEV_JOIN:
386961bd3857SMoni Shoua 	case NETDEV_BONDING_INFO:
38701da177e4SLinus Torvalds 		break;
38711da177e4SLinus Torvalds 	default:
38727f294054SAlexei Starovoitov 		rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
38731da177e4SLinus Torvalds 		break;
38741da177e4SLinus Torvalds 	}
38751da177e4SLinus Torvalds 	return NOTIFY_DONE;
38761da177e4SLinus Torvalds }
38771da177e4SLinus Torvalds 
38781da177e4SLinus Torvalds static struct notifier_block rtnetlink_dev_notifier = {
38791da177e4SLinus Torvalds 	.notifier_call	= rtnetlink_event,
38801da177e4SLinus Torvalds };
38811da177e4SLinus Torvalds 
388297c53cacSDenis V. Lunev 
38832c8c1e72SAlexey Dobriyan static int __net_init rtnetlink_net_init(struct net *net)
388497c53cacSDenis V. Lunev {
388597c53cacSDenis V. Lunev 	struct sock *sk;
3886a31f2d17SPablo Neira Ayuso 	struct netlink_kernel_cfg cfg = {
3887a31f2d17SPablo Neira Ayuso 		.groups		= RTNLGRP_MAX,
3888a31f2d17SPablo Neira Ayuso 		.input		= rtnetlink_rcv,
3889a31f2d17SPablo Neira Ayuso 		.cb_mutex	= &rtnl_mutex,
38909785e10aSPablo Neira Ayuso 		.flags		= NL_CFG_F_NONROOT_RECV,
3891a31f2d17SPablo Neira Ayuso 	};
3892a31f2d17SPablo Neira Ayuso 
38939f00d977SPablo Neira Ayuso 	sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
389497c53cacSDenis V. Lunev 	if (!sk)
389597c53cacSDenis V. Lunev 		return -ENOMEM;
389697c53cacSDenis V. Lunev 	net->rtnl = sk;
389797c53cacSDenis V. Lunev 	return 0;
389897c53cacSDenis V. Lunev }
389997c53cacSDenis V. Lunev 
39002c8c1e72SAlexey Dobriyan static void __net_exit rtnetlink_net_exit(struct net *net)
390197c53cacSDenis V. Lunev {
3902b7c6ba6eSDenis V. Lunev 	netlink_kernel_release(net->rtnl);
390397c53cacSDenis V. Lunev 	net->rtnl = NULL;
390497c53cacSDenis V. Lunev }
390597c53cacSDenis V. Lunev 
390697c53cacSDenis V. Lunev static struct pernet_operations rtnetlink_net_ops = {
390797c53cacSDenis V. Lunev 	.init = rtnetlink_net_init,
390897c53cacSDenis V. Lunev 	.exit = rtnetlink_net_exit,
390997c53cacSDenis V. Lunev };
391097c53cacSDenis V. Lunev 
39111da177e4SLinus Torvalds void __init rtnetlink_init(void)
39121da177e4SLinus Torvalds {
391397c53cacSDenis V. Lunev 	if (register_pernet_subsys(&rtnetlink_net_ops))
39141da177e4SLinus Torvalds 		panic("rtnetlink_init: cannot initialize rtnetlink\n");
391597c53cacSDenis V. Lunev 
39161da177e4SLinus Torvalds 	register_netdevice_notifier(&rtnetlink_dev_notifier);
3917340d17fcSThomas Graf 
3918c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
3919c7ac8679SGreg Rose 		      rtnl_dump_ifinfo, rtnl_calcit);
3920c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL);
3921c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL);
3922c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL);
3923687ad8ccSThomas Graf 
3924c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL);
3925c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL);
392677162022SJohn Fastabend 
392777162022SJohn Fastabend 	rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, NULL);
392877162022SJohn Fastabend 	rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, NULL);
392977162022SJohn Fastabend 	rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, NULL);
3930e5a55a89SJohn Fastabend 
3931e5a55a89SJohn Fastabend 	rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, NULL);
3932407af329SVlad Yasevich 	rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, NULL);
3933e5a55a89SJohn Fastabend 	rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, NULL);
393410c9ead9SRoopa Prabhu 
393510c9ead9SRoopa Prabhu 	rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,
393610c9ead9SRoopa Prabhu 		      NULL);
39371da177e4SLinus Torvalds }
3938