xref: /openbmc/linux/net/core/rtnetlink.c (revision 6cb69742daa1770fe1ce54cf45f8951376518176)
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>
531da177e4SLinus Torvalds #include <net/sock.h>
541da177e4SLinus Torvalds #include <net/pkt_sched.h>
5514c0b97dSThomas Graf #include <net/fib_rules.h>
56e2849863SThomas Graf #include <net/rtnetlink.h>
5730ffee84SJohannes Berg #include <net/net_namespace.h>
581da177e4SLinus Torvalds 
59e0d087afSEric Dumazet struct rtnl_link {
60e2849863SThomas Graf 	rtnl_doit_func		doit;
61e2849863SThomas Graf 	rtnl_dumpit_func	dumpit;
62c7ac8679SGreg Rose 	rtnl_calcit_func 	calcit;
63e2849863SThomas Graf };
64e2849863SThomas Graf 
656756ae4bSStephen Hemminger static DEFINE_MUTEX(rtnl_mutex);
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds void rtnl_lock(void)
681da177e4SLinus Torvalds {
696756ae4bSStephen Hemminger 	mutex_lock(&rtnl_mutex);
701da177e4SLinus Torvalds }
71e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_lock);
721da177e4SLinus Torvalds 
736756ae4bSStephen Hemminger void __rtnl_unlock(void)
741da177e4SLinus Torvalds {
756756ae4bSStephen Hemminger 	mutex_unlock(&rtnl_mutex);
761da177e4SLinus Torvalds }
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds void rtnl_unlock(void)
791da177e4SLinus Torvalds {
8058ec3b4dSHerbert Xu 	/* This fellow will unlock it for us. */
811da177e4SLinus Torvalds 	netdev_run_todo();
821da177e4SLinus Torvalds }
83e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unlock);
841da177e4SLinus Torvalds 
856756ae4bSStephen Hemminger int rtnl_trylock(void)
866756ae4bSStephen Hemminger {
876756ae4bSStephen Hemminger 	return mutex_trylock(&rtnl_mutex);
886756ae4bSStephen Hemminger }
89e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_trylock);
906756ae4bSStephen Hemminger 
91c9c1014bSPatrick McHardy int rtnl_is_locked(void)
92c9c1014bSPatrick McHardy {
93c9c1014bSPatrick McHardy 	return mutex_is_locked(&rtnl_mutex);
94c9c1014bSPatrick McHardy }
95e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_is_locked);
96c9c1014bSPatrick McHardy 
97a898def2SPaul E. McKenney #ifdef CONFIG_PROVE_LOCKING
98a898def2SPaul E. McKenney int lockdep_rtnl_is_held(void)
99a898def2SPaul E. McKenney {
100a898def2SPaul E. McKenney 	return lockdep_is_held(&rtnl_mutex);
101a898def2SPaul E. McKenney }
102a898def2SPaul E. McKenney EXPORT_SYMBOL(lockdep_rtnl_is_held);
103a898def2SPaul E. McKenney #endif /* #ifdef CONFIG_PROVE_LOCKING */
104a898def2SPaul E. McKenney 
10525239ceeSPatrick McHardy static struct rtnl_link *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
106e2849863SThomas Graf 
107e2849863SThomas Graf static inline int rtm_msgindex(int msgtype)
108e2849863SThomas Graf {
109e2849863SThomas Graf 	int msgindex = msgtype - RTM_BASE;
110e2849863SThomas Graf 
111e2849863SThomas Graf 	/*
112e2849863SThomas Graf 	 * msgindex < 0 implies someone tried to register a netlink
113e2849863SThomas Graf 	 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
114e2849863SThomas Graf 	 * the message type has not been added to linux/rtnetlink.h
115e2849863SThomas Graf 	 */
116e2849863SThomas Graf 	BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
117e2849863SThomas Graf 
118e2849863SThomas Graf 	return msgindex;
119e2849863SThomas Graf }
120e2849863SThomas Graf 
121e2849863SThomas Graf static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex)
122e2849863SThomas Graf {
123e2849863SThomas Graf 	struct rtnl_link *tab;
124e2849863SThomas Graf 
12525239ceeSPatrick McHardy 	if (protocol <= RTNL_FAMILY_MAX)
126e2849863SThomas Graf 		tab = rtnl_msg_handlers[protocol];
1270f87b1ddSPatrick McHardy 	else
1280f87b1ddSPatrick McHardy 		tab = NULL;
1290f87b1ddSPatrick McHardy 
13051057f2fSThomas Graf 	if (tab == NULL || tab[msgindex].doit == NULL)
131e2849863SThomas Graf 		tab = rtnl_msg_handlers[PF_UNSPEC];
132e2849863SThomas Graf 
133c80bbeaeSHans Zhang 	return tab[msgindex].doit;
134e2849863SThomas Graf }
135e2849863SThomas Graf 
136e2849863SThomas Graf static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex)
137e2849863SThomas Graf {
138e2849863SThomas Graf 	struct rtnl_link *tab;
139e2849863SThomas Graf 
14025239ceeSPatrick McHardy 	if (protocol <= RTNL_FAMILY_MAX)
141e2849863SThomas Graf 		tab = rtnl_msg_handlers[protocol];
1420f87b1ddSPatrick McHardy 	else
1430f87b1ddSPatrick McHardy 		tab = NULL;
1440f87b1ddSPatrick McHardy 
14551057f2fSThomas Graf 	if (tab == NULL || tab[msgindex].dumpit == NULL)
146e2849863SThomas Graf 		tab = rtnl_msg_handlers[PF_UNSPEC];
147e2849863SThomas Graf 
148c80bbeaeSHans Zhang 	return tab[msgindex].dumpit;
149e2849863SThomas Graf }
150e2849863SThomas Graf 
151c7ac8679SGreg Rose static rtnl_calcit_func rtnl_get_calcit(int protocol, int msgindex)
152c7ac8679SGreg Rose {
153c7ac8679SGreg Rose 	struct rtnl_link *tab;
154c7ac8679SGreg Rose 
155c7ac8679SGreg Rose 	if (protocol <= RTNL_FAMILY_MAX)
156c7ac8679SGreg Rose 		tab = rtnl_msg_handlers[protocol];
157c7ac8679SGreg Rose 	else
158c7ac8679SGreg Rose 		tab = NULL;
159c7ac8679SGreg Rose 
160c7ac8679SGreg Rose 	if (tab == NULL || tab[msgindex].calcit == NULL)
161c7ac8679SGreg Rose 		tab = rtnl_msg_handlers[PF_UNSPEC];
162c7ac8679SGreg Rose 
163c80bbeaeSHans Zhang 	return tab[msgindex].calcit;
164c7ac8679SGreg Rose }
165c7ac8679SGreg Rose 
166e2849863SThomas Graf /**
167e2849863SThomas Graf  * __rtnl_register - Register a rtnetlink message type
168e2849863SThomas Graf  * @protocol: Protocol family or PF_UNSPEC
169e2849863SThomas Graf  * @msgtype: rtnetlink message type
170e2849863SThomas Graf  * @doit: Function pointer called for each request message
171e2849863SThomas Graf  * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
172c7ac8679SGreg Rose  * @calcit: Function pointer to calc size of dump message
173e2849863SThomas Graf  *
174e2849863SThomas Graf  * Registers the specified function pointers (at least one of them has
175e2849863SThomas Graf  * to be non-NULL) to be called whenever a request message for the
176e2849863SThomas Graf  * specified protocol family and message type is received.
177e2849863SThomas Graf  *
178e2849863SThomas Graf  * The special protocol family PF_UNSPEC may be used to define fallback
179e2849863SThomas Graf  * function pointers for the case when no entry for the specific protocol
180e2849863SThomas Graf  * family exists.
181e2849863SThomas Graf  *
182e2849863SThomas Graf  * Returns 0 on success or a negative error code.
183e2849863SThomas Graf  */
184e2849863SThomas Graf int __rtnl_register(int protocol, int msgtype,
185c7ac8679SGreg Rose 		    rtnl_doit_func doit, rtnl_dumpit_func dumpit,
186c7ac8679SGreg Rose 		    rtnl_calcit_func calcit)
187e2849863SThomas Graf {
188e2849863SThomas Graf 	struct rtnl_link *tab;
189e2849863SThomas Graf 	int msgindex;
190e2849863SThomas Graf 
19125239ceeSPatrick McHardy 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
192e2849863SThomas Graf 	msgindex = rtm_msgindex(msgtype);
193e2849863SThomas Graf 
194e2849863SThomas Graf 	tab = rtnl_msg_handlers[protocol];
195e2849863SThomas Graf 	if (tab == NULL) {
196e2849863SThomas Graf 		tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL);
197e2849863SThomas Graf 		if (tab == NULL)
198e2849863SThomas Graf 			return -ENOBUFS;
199e2849863SThomas Graf 
200e2849863SThomas Graf 		rtnl_msg_handlers[protocol] = tab;
201e2849863SThomas Graf 	}
202e2849863SThomas Graf 
203e2849863SThomas Graf 	if (doit)
204e2849863SThomas Graf 		tab[msgindex].doit = doit;
205e2849863SThomas Graf 
206e2849863SThomas Graf 	if (dumpit)
207e2849863SThomas Graf 		tab[msgindex].dumpit = dumpit;
208e2849863SThomas Graf 
209c7ac8679SGreg Rose 	if (calcit)
210c7ac8679SGreg Rose 		tab[msgindex].calcit = calcit;
211c7ac8679SGreg Rose 
212e2849863SThomas Graf 	return 0;
213e2849863SThomas Graf }
214e2849863SThomas Graf EXPORT_SYMBOL_GPL(__rtnl_register);
215e2849863SThomas Graf 
216e2849863SThomas Graf /**
217e2849863SThomas Graf  * rtnl_register - Register a rtnetlink message type
218e2849863SThomas Graf  *
219e2849863SThomas Graf  * Identical to __rtnl_register() but panics on failure. This is useful
220e2849863SThomas Graf  * as failure of this function is very unlikely, it can only happen due
221e2849863SThomas Graf  * to lack of memory when allocating the chain to store all message
222e2849863SThomas Graf  * handlers for a protocol. Meant for use in init functions where lack
22325985edcSLucas De Marchi  * of memory implies no sense in continuing.
224e2849863SThomas Graf  */
225e2849863SThomas Graf void rtnl_register(int protocol, int msgtype,
226c7ac8679SGreg Rose 		   rtnl_doit_func doit, rtnl_dumpit_func dumpit,
227c7ac8679SGreg Rose 		   rtnl_calcit_func calcit)
228e2849863SThomas Graf {
229c7ac8679SGreg Rose 	if (__rtnl_register(protocol, msgtype, doit, dumpit, calcit) < 0)
230e2849863SThomas Graf 		panic("Unable to register rtnetlink message handler, "
231e2849863SThomas Graf 		      "protocol = %d, message type = %d\n",
232e2849863SThomas Graf 		      protocol, msgtype);
233e2849863SThomas Graf }
234e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_register);
235e2849863SThomas Graf 
236e2849863SThomas Graf /**
237e2849863SThomas Graf  * rtnl_unregister - Unregister a rtnetlink message type
238e2849863SThomas Graf  * @protocol: Protocol family or PF_UNSPEC
239e2849863SThomas Graf  * @msgtype: rtnetlink message type
240e2849863SThomas Graf  *
241e2849863SThomas Graf  * Returns 0 on success or a negative error code.
242e2849863SThomas Graf  */
243e2849863SThomas Graf int rtnl_unregister(int protocol, int msgtype)
244e2849863SThomas Graf {
245e2849863SThomas Graf 	int msgindex;
246e2849863SThomas Graf 
24725239ceeSPatrick McHardy 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
248e2849863SThomas Graf 	msgindex = rtm_msgindex(msgtype);
249e2849863SThomas Graf 
250e2849863SThomas Graf 	if (rtnl_msg_handlers[protocol] == NULL)
251e2849863SThomas Graf 		return -ENOENT;
252e2849863SThomas Graf 
253e2849863SThomas Graf 	rtnl_msg_handlers[protocol][msgindex].doit = NULL;
254e2849863SThomas Graf 	rtnl_msg_handlers[protocol][msgindex].dumpit = NULL;
255e2849863SThomas Graf 
256e2849863SThomas Graf 	return 0;
257e2849863SThomas Graf }
258e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister);
259e2849863SThomas Graf 
260e2849863SThomas Graf /**
261e2849863SThomas Graf  * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
262e2849863SThomas Graf  * @protocol : Protocol family or PF_UNSPEC
263e2849863SThomas Graf  *
264e2849863SThomas Graf  * Identical to calling rtnl_unregster() for all registered message types
265e2849863SThomas Graf  * of a certain protocol family.
266e2849863SThomas Graf  */
267e2849863SThomas Graf void rtnl_unregister_all(int protocol)
268e2849863SThomas Graf {
26925239ceeSPatrick McHardy 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
270e2849863SThomas Graf 
271e2849863SThomas Graf 	kfree(rtnl_msg_handlers[protocol]);
272e2849863SThomas Graf 	rtnl_msg_handlers[protocol] = NULL;
273e2849863SThomas Graf }
274e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister_all);
2751da177e4SLinus Torvalds 
27638f7b870SPatrick McHardy static LIST_HEAD(link_ops);
27738f7b870SPatrick McHardy 
278c63044f0SEric Dumazet static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
279c63044f0SEric Dumazet {
280c63044f0SEric Dumazet 	const struct rtnl_link_ops *ops;
281c63044f0SEric Dumazet 
282c63044f0SEric Dumazet 	list_for_each_entry(ops, &link_ops, list) {
283c63044f0SEric Dumazet 		if (!strcmp(ops->kind, kind))
284c63044f0SEric Dumazet 			return ops;
285c63044f0SEric Dumazet 	}
286c63044f0SEric Dumazet 	return NULL;
287c63044f0SEric Dumazet }
288c63044f0SEric Dumazet 
28938f7b870SPatrick McHardy /**
29038f7b870SPatrick McHardy  * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
29138f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to register
29238f7b870SPatrick McHardy  *
29338f7b870SPatrick McHardy  * The caller must hold the rtnl_mutex. This function should be used
29438f7b870SPatrick McHardy  * by drivers that create devices during module initialization. It
29538f7b870SPatrick McHardy  * must be called before registering the devices.
29638f7b870SPatrick McHardy  *
29738f7b870SPatrick McHardy  * Returns 0 on success or a negative error code.
29838f7b870SPatrick McHardy  */
29938f7b870SPatrick McHardy int __rtnl_link_register(struct rtnl_link_ops *ops)
30038f7b870SPatrick McHardy {
301c63044f0SEric Dumazet 	if (rtnl_link_ops_get(ops->kind))
302c63044f0SEric Dumazet 		return -EEXIST;
303c63044f0SEric Dumazet 
304b0ab2fabSJiri Pirko 	/* The check for setup is here because if ops
305b0ab2fabSJiri Pirko 	 * does not have that filled up, it is not possible
306b0ab2fabSJiri Pirko 	 * to use the ops for creating device. So do not
307b0ab2fabSJiri Pirko 	 * fill up dellink as well. That disables rtnl_dellink.
308b0ab2fabSJiri Pirko 	 */
309b0ab2fabSJiri Pirko 	if (ops->setup && !ops->dellink)
31023289a37SEric Dumazet 		ops->dellink = unregister_netdevice_queue;
3112d85cba2SPatrick McHardy 
31238f7b870SPatrick McHardy 	list_add_tail(&ops->list, &link_ops);
31338f7b870SPatrick McHardy 	return 0;
31438f7b870SPatrick McHardy }
31538f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_register);
31638f7b870SPatrick McHardy 
31738f7b870SPatrick McHardy /**
31838f7b870SPatrick McHardy  * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
31938f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to register
32038f7b870SPatrick McHardy  *
32138f7b870SPatrick McHardy  * Returns 0 on success or a negative error code.
32238f7b870SPatrick McHardy  */
32338f7b870SPatrick McHardy int rtnl_link_register(struct rtnl_link_ops *ops)
32438f7b870SPatrick McHardy {
32538f7b870SPatrick McHardy 	int err;
32638f7b870SPatrick McHardy 
32738f7b870SPatrick McHardy 	rtnl_lock();
32838f7b870SPatrick McHardy 	err = __rtnl_link_register(ops);
32938f7b870SPatrick McHardy 	rtnl_unlock();
33038f7b870SPatrick McHardy 	return err;
33138f7b870SPatrick McHardy }
33238f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_register);
33338f7b870SPatrick McHardy 
334669f87baSPavel Emelyanov static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
335669f87baSPavel Emelyanov {
336669f87baSPavel Emelyanov 	struct net_device *dev;
33723289a37SEric Dumazet 	LIST_HEAD(list_kill);
33823289a37SEric Dumazet 
339669f87baSPavel Emelyanov 	for_each_netdev(net, dev) {
34023289a37SEric Dumazet 		if (dev->rtnl_link_ops == ops)
34123289a37SEric Dumazet 			ops->dellink(dev, &list_kill);
342669f87baSPavel Emelyanov 	}
34323289a37SEric Dumazet 	unregister_netdevice_many(&list_kill);
344669f87baSPavel Emelyanov }
345669f87baSPavel Emelyanov 
34638f7b870SPatrick McHardy /**
34738f7b870SPatrick McHardy  * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
34838f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to unregister
34938f7b870SPatrick McHardy  *
3502d85cba2SPatrick McHardy  * The caller must hold the rtnl_mutex.
35138f7b870SPatrick McHardy  */
35238f7b870SPatrick McHardy void __rtnl_link_unregister(struct rtnl_link_ops *ops)
35338f7b870SPatrick McHardy {
354881d966bSEric W. Biederman 	struct net *net;
3552d85cba2SPatrick McHardy 
356881d966bSEric W. Biederman 	for_each_net(net) {
357669f87baSPavel Emelyanov 		__rtnl_kill_links(net, ops);
358881d966bSEric W. Biederman 	}
35938f7b870SPatrick McHardy 	list_del(&ops->list);
36038f7b870SPatrick McHardy }
36138f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
36238f7b870SPatrick McHardy 
363200b916fSCong Wang /* Return with the rtnl_lock held when there are no network
364200b916fSCong Wang  * devices unregistering in any network namespace.
365200b916fSCong Wang  */
366200b916fSCong Wang static void rtnl_lock_unregistering_all(void)
367200b916fSCong Wang {
368200b916fSCong Wang 	struct net *net;
369200b916fSCong Wang 	bool unregistering;
370ff960a73SPeter Zijlstra 	DEFINE_WAIT_FUNC(wait, woken_wake_function);
371200b916fSCong Wang 
372ff960a73SPeter Zijlstra 	add_wait_queue(&netdev_unregistering_wq, &wait);
373200b916fSCong Wang 	for (;;) {
374200b916fSCong Wang 		unregistering = false;
375200b916fSCong Wang 		rtnl_lock();
376200b916fSCong Wang 		for_each_net(net) {
377200b916fSCong Wang 			if (net->dev_unreg_count > 0) {
378200b916fSCong Wang 				unregistering = true;
379200b916fSCong Wang 				break;
380200b916fSCong Wang 			}
381200b916fSCong Wang 		}
382200b916fSCong Wang 		if (!unregistering)
383200b916fSCong Wang 			break;
384200b916fSCong Wang 		__rtnl_unlock();
385ff960a73SPeter Zijlstra 
386ff960a73SPeter Zijlstra 		wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
387200b916fSCong Wang 	}
388ff960a73SPeter Zijlstra 	remove_wait_queue(&netdev_unregistering_wq, &wait);
389200b916fSCong Wang }
390200b916fSCong Wang 
39138f7b870SPatrick McHardy /**
39238f7b870SPatrick McHardy  * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
39338f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to unregister
39438f7b870SPatrick McHardy  */
39538f7b870SPatrick McHardy void rtnl_link_unregister(struct rtnl_link_ops *ops)
39638f7b870SPatrick McHardy {
397200b916fSCong Wang 	/* Close the race with cleanup_net() */
398200b916fSCong Wang 	mutex_lock(&net_mutex);
399200b916fSCong Wang 	rtnl_lock_unregistering_all();
40038f7b870SPatrick McHardy 	__rtnl_link_unregister(ops);
40138f7b870SPatrick McHardy 	rtnl_unlock();
402200b916fSCong Wang 	mutex_unlock(&net_mutex);
40338f7b870SPatrick McHardy }
40438f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_unregister);
40538f7b870SPatrick McHardy 
406ba7d49b1SJiri Pirko static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
407ba7d49b1SJiri Pirko {
408ba7d49b1SJiri Pirko 	struct net_device *master_dev;
409ba7d49b1SJiri Pirko 	const struct rtnl_link_ops *ops;
410ba7d49b1SJiri Pirko 
411ba7d49b1SJiri Pirko 	master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
412ba7d49b1SJiri Pirko 	if (!master_dev)
413ba7d49b1SJiri Pirko 		return 0;
414ba7d49b1SJiri Pirko 	ops = master_dev->rtnl_link_ops;
4156049f253SFernando Luis Vazquez Cao 	if (!ops || !ops->get_slave_size)
416ba7d49b1SJiri Pirko 		return 0;
417ba7d49b1SJiri Pirko 	/* IFLA_INFO_SLAVE_DATA + nested data */
418ba7d49b1SJiri Pirko 	return nla_total_size(sizeof(struct nlattr)) +
419ba7d49b1SJiri Pirko 	       ops->get_slave_size(master_dev, dev);
420ba7d49b1SJiri Pirko }
421ba7d49b1SJiri Pirko 
42238f7b870SPatrick McHardy static size_t rtnl_link_get_size(const struct net_device *dev)
42338f7b870SPatrick McHardy {
42438f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
42538f7b870SPatrick McHardy 	size_t size;
42638f7b870SPatrick McHardy 
42738f7b870SPatrick McHardy 	if (!ops)
42838f7b870SPatrick McHardy 		return 0;
42938f7b870SPatrick McHardy 
430369cf77aSThomas Graf 	size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
431369cf77aSThomas Graf 	       nla_total_size(strlen(ops->kind) + 1);  /* IFLA_INFO_KIND */
43238f7b870SPatrick McHardy 
43338f7b870SPatrick McHardy 	if (ops->get_size)
43438f7b870SPatrick McHardy 		/* IFLA_INFO_DATA + nested data */
435369cf77aSThomas Graf 		size += nla_total_size(sizeof(struct nlattr)) +
43638f7b870SPatrick McHardy 			ops->get_size(dev);
43738f7b870SPatrick McHardy 
43838f7b870SPatrick McHardy 	if (ops->get_xstats_size)
439369cf77aSThomas Graf 		/* IFLA_INFO_XSTATS */
440369cf77aSThomas Graf 		size += nla_total_size(ops->get_xstats_size(dev));
44138f7b870SPatrick McHardy 
442ba7d49b1SJiri Pirko 	size += rtnl_link_get_slave_info_data_size(dev);
443ba7d49b1SJiri Pirko 
44438f7b870SPatrick McHardy 	return size;
44538f7b870SPatrick McHardy }
44638f7b870SPatrick McHardy 
447f8ff182cSThomas Graf static LIST_HEAD(rtnl_af_ops);
448f8ff182cSThomas Graf 
449f8ff182cSThomas Graf static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
450f8ff182cSThomas Graf {
451f8ff182cSThomas Graf 	const struct rtnl_af_ops *ops;
452f8ff182cSThomas Graf 
453f8ff182cSThomas Graf 	list_for_each_entry(ops, &rtnl_af_ops, list) {
454f8ff182cSThomas Graf 		if (ops->family == family)
455f8ff182cSThomas Graf 			return ops;
456f8ff182cSThomas Graf 	}
457f8ff182cSThomas Graf 
458f8ff182cSThomas Graf 	return NULL;
459f8ff182cSThomas Graf }
460f8ff182cSThomas Graf 
461f8ff182cSThomas Graf /**
462f8ff182cSThomas Graf  * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
463f8ff182cSThomas Graf  * @ops: struct rtnl_af_ops * to register
464f8ff182cSThomas Graf  *
465f8ff182cSThomas Graf  * Returns 0 on success or a negative error code.
466f8ff182cSThomas Graf  */
4673678a9d8Sstephen hemminger void rtnl_af_register(struct rtnl_af_ops *ops)
468f8ff182cSThomas Graf {
469f8ff182cSThomas Graf 	rtnl_lock();
4703678a9d8Sstephen hemminger 	list_add_tail(&ops->list, &rtnl_af_ops);
471f8ff182cSThomas Graf 	rtnl_unlock();
472f8ff182cSThomas Graf }
473f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_register);
474f8ff182cSThomas Graf 
475f8ff182cSThomas Graf /**
476f8ff182cSThomas Graf  * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
477f8ff182cSThomas Graf  * @ops: struct rtnl_af_ops * to unregister
478f8ff182cSThomas Graf  *
479f8ff182cSThomas Graf  * The caller must hold the rtnl_mutex.
480f8ff182cSThomas Graf  */
481f8ff182cSThomas Graf void __rtnl_af_unregister(struct rtnl_af_ops *ops)
482f8ff182cSThomas Graf {
483f8ff182cSThomas Graf 	list_del(&ops->list);
484f8ff182cSThomas Graf }
485f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(__rtnl_af_unregister);
486f8ff182cSThomas Graf 
487f8ff182cSThomas Graf /**
488f8ff182cSThomas Graf  * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
489f8ff182cSThomas Graf  * @ops: struct rtnl_af_ops * to unregister
490f8ff182cSThomas Graf  */
491f8ff182cSThomas Graf void rtnl_af_unregister(struct rtnl_af_ops *ops)
492f8ff182cSThomas Graf {
493f8ff182cSThomas Graf 	rtnl_lock();
494f8ff182cSThomas Graf 	__rtnl_af_unregister(ops);
495f8ff182cSThomas Graf 	rtnl_unlock();
496f8ff182cSThomas Graf }
497f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_unregister);
498f8ff182cSThomas Graf 
499f8ff182cSThomas Graf static size_t rtnl_link_get_af_size(const struct net_device *dev)
500f8ff182cSThomas Graf {
501f8ff182cSThomas Graf 	struct rtnl_af_ops *af_ops;
502f8ff182cSThomas Graf 	size_t size;
503f8ff182cSThomas Graf 
504f8ff182cSThomas Graf 	/* IFLA_AF_SPEC */
505f8ff182cSThomas Graf 	size = nla_total_size(sizeof(struct nlattr));
506f8ff182cSThomas Graf 
507f8ff182cSThomas Graf 	list_for_each_entry(af_ops, &rtnl_af_ops, list) {
508f8ff182cSThomas Graf 		if (af_ops->get_link_af_size) {
509f8ff182cSThomas Graf 			/* AF_* + nested data */
510f8ff182cSThomas Graf 			size += nla_total_size(sizeof(struct nlattr)) +
511f8ff182cSThomas Graf 				af_ops->get_link_af_size(dev);
512f8ff182cSThomas Graf 		}
513f8ff182cSThomas Graf 	}
514f8ff182cSThomas Graf 
515f8ff182cSThomas Graf 	return size;
516f8ff182cSThomas Graf }
517f8ff182cSThomas Graf 
518ba7d49b1SJiri Pirko static bool rtnl_have_link_slave_info(const struct net_device *dev)
519ba7d49b1SJiri Pirko {
520ba7d49b1SJiri Pirko 	struct net_device *master_dev;
521ba7d49b1SJiri Pirko 
522ba7d49b1SJiri Pirko 	master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
523813f020cSJiri Pirko 	if (master_dev && master_dev->rtnl_link_ops)
524ba7d49b1SJiri Pirko 		return true;
525ba7d49b1SJiri Pirko 	return false;
526ba7d49b1SJiri Pirko }
527ba7d49b1SJiri Pirko 
528ba7d49b1SJiri Pirko static int rtnl_link_slave_info_fill(struct sk_buff *skb,
529ba7d49b1SJiri Pirko 				     const struct net_device *dev)
530ba7d49b1SJiri Pirko {
531ba7d49b1SJiri Pirko 	struct net_device *master_dev;
532ba7d49b1SJiri Pirko 	const struct rtnl_link_ops *ops;
533ba7d49b1SJiri Pirko 	struct nlattr *slave_data;
534ba7d49b1SJiri Pirko 	int err;
535ba7d49b1SJiri Pirko 
536ba7d49b1SJiri Pirko 	master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
537ba7d49b1SJiri Pirko 	if (!master_dev)
538ba7d49b1SJiri Pirko 		return 0;
539ba7d49b1SJiri Pirko 	ops = master_dev->rtnl_link_ops;
540ba7d49b1SJiri Pirko 	if (!ops)
541ba7d49b1SJiri Pirko 		return 0;
542ba7d49b1SJiri Pirko 	if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
543ba7d49b1SJiri Pirko 		return -EMSGSIZE;
544ba7d49b1SJiri Pirko 	if (ops->fill_slave_info) {
545ba7d49b1SJiri Pirko 		slave_data = nla_nest_start(skb, IFLA_INFO_SLAVE_DATA);
546ba7d49b1SJiri Pirko 		if (!slave_data)
547ba7d49b1SJiri Pirko 			return -EMSGSIZE;
548ba7d49b1SJiri Pirko 		err = ops->fill_slave_info(skb, master_dev, dev);
549ba7d49b1SJiri Pirko 		if (err < 0)
550ba7d49b1SJiri Pirko 			goto err_cancel_slave_data;
551ba7d49b1SJiri Pirko 		nla_nest_end(skb, slave_data);
552ba7d49b1SJiri Pirko 	}
553ba7d49b1SJiri Pirko 	return 0;
554ba7d49b1SJiri Pirko 
555ba7d49b1SJiri Pirko err_cancel_slave_data:
556ba7d49b1SJiri Pirko 	nla_nest_cancel(skb, slave_data);
557ba7d49b1SJiri Pirko 	return err;
558ba7d49b1SJiri Pirko }
559ba7d49b1SJiri Pirko 
560ba7d49b1SJiri Pirko static int rtnl_link_info_fill(struct sk_buff *skb,
561ba7d49b1SJiri Pirko 			       const struct net_device *dev)
56238f7b870SPatrick McHardy {
56338f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
564ba7d49b1SJiri Pirko 	struct nlattr *data;
565ba7d49b1SJiri Pirko 	int err;
566ba7d49b1SJiri Pirko 
567ba7d49b1SJiri Pirko 	if (!ops)
568ba7d49b1SJiri Pirko 		return 0;
569ba7d49b1SJiri Pirko 	if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
570ba7d49b1SJiri Pirko 		return -EMSGSIZE;
571ba7d49b1SJiri Pirko 	if (ops->fill_xstats) {
572ba7d49b1SJiri Pirko 		err = ops->fill_xstats(skb, dev);
573ba7d49b1SJiri Pirko 		if (err < 0)
574ba7d49b1SJiri Pirko 			return err;
575ba7d49b1SJiri Pirko 	}
576ba7d49b1SJiri Pirko 	if (ops->fill_info) {
577ba7d49b1SJiri Pirko 		data = nla_nest_start(skb, IFLA_INFO_DATA);
578ba7d49b1SJiri Pirko 		if (data == NULL)
579ba7d49b1SJiri Pirko 			return -EMSGSIZE;
580ba7d49b1SJiri Pirko 		err = ops->fill_info(skb, dev);
581ba7d49b1SJiri Pirko 		if (err < 0)
582ba7d49b1SJiri Pirko 			goto err_cancel_data;
583ba7d49b1SJiri Pirko 		nla_nest_end(skb, data);
584ba7d49b1SJiri Pirko 	}
585ba7d49b1SJiri Pirko 	return 0;
586ba7d49b1SJiri Pirko 
587ba7d49b1SJiri Pirko err_cancel_data:
588ba7d49b1SJiri Pirko 	nla_nest_cancel(skb, data);
589ba7d49b1SJiri Pirko 	return err;
590ba7d49b1SJiri Pirko }
591ba7d49b1SJiri Pirko 
592ba7d49b1SJiri Pirko static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
593ba7d49b1SJiri Pirko {
594ba7d49b1SJiri Pirko 	struct nlattr *linkinfo;
59538f7b870SPatrick McHardy 	int err = -EMSGSIZE;
59638f7b870SPatrick McHardy 
59738f7b870SPatrick McHardy 	linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
59838f7b870SPatrick McHardy 	if (linkinfo == NULL)
59938f7b870SPatrick McHardy 		goto out;
60038f7b870SPatrick McHardy 
601ba7d49b1SJiri Pirko 	err = rtnl_link_info_fill(skb, dev);
60238f7b870SPatrick McHardy 	if (err < 0)
60338f7b870SPatrick McHardy 		goto err_cancel_link;
604ba7d49b1SJiri Pirko 
605ba7d49b1SJiri Pirko 	err = rtnl_link_slave_info_fill(skb, dev);
60638f7b870SPatrick McHardy 	if (err < 0)
607ba7d49b1SJiri Pirko 		goto err_cancel_link;
60838f7b870SPatrick McHardy 
60938f7b870SPatrick McHardy 	nla_nest_end(skb, linkinfo);
61038f7b870SPatrick McHardy 	return 0;
61138f7b870SPatrick McHardy 
61238f7b870SPatrick McHardy err_cancel_link:
61338f7b870SPatrick McHardy 	nla_nest_cancel(skb, linkinfo);
61438f7b870SPatrick McHardy out:
61538f7b870SPatrick McHardy 	return err;
61638f7b870SPatrick McHardy }
61738f7b870SPatrick McHardy 
61895c96174SEric Dumazet int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
6191da177e4SLinus Torvalds {
62097c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
6211da177e4SLinus Torvalds 	int err = 0;
6221da177e4SLinus Torvalds 
623ac6d439dSPatrick McHardy 	NETLINK_CB(skb).dst_group = group;
6241da177e4SLinus Torvalds 	if (echo)
6251da177e4SLinus Torvalds 		atomic_inc(&skb->users);
6261da177e4SLinus Torvalds 	netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
6271da177e4SLinus Torvalds 	if (echo)
6281da177e4SLinus Torvalds 		err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
6291da177e4SLinus Torvalds 	return err;
6301da177e4SLinus Torvalds }
6311da177e4SLinus Torvalds 
63297c53cacSDenis V. Lunev int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
6332942e900SThomas Graf {
63497c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
63597c53cacSDenis V. Lunev 
6362942e900SThomas Graf 	return nlmsg_unicast(rtnl, skb, pid);
6372942e900SThomas Graf }
638e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unicast);
6392942e900SThomas Graf 
6401ce85fe4SPablo Neira Ayuso void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
64197676b6bSThomas Graf 		 struct nlmsghdr *nlh, gfp_t flags)
64297676b6bSThomas Graf {
64397c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
64497676b6bSThomas Graf 	int report = 0;
64597676b6bSThomas Graf 
64697676b6bSThomas Graf 	if (nlh)
64797676b6bSThomas Graf 		report = nlmsg_report(nlh);
64897676b6bSThomas Graf 
6491ce85fe4SPablo Neira Ayuso 	nlmsg_notify(rtnl, skb, pid, group, report, flags);
65097676b6bSThomas Graf }
651e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_notify);
65297676b6bSThomas Graf 
65397c53cacSDenis V. Lunev void rtnl_set_sk_err(struct net *net, u32 group, int error)
65497676b6bSThomas Graf {
65597c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
65697c53cacSDenis V. Lunev 
65797676b6bSThomas Graf 	netlink_set_err(rtnl, 0, group, error);
65897676b6bSThomas Graf }
659e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_set_sk_err);
66097676b6bSThomas Graf 
6611da177e4SLinus Torvalds int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
6621da177e4SLinus Torvalds {
6632d7202bfSThomas Graf 	struct nlattr *mx;
6642d7202bfSThomas Graf 	int i, valid = 0;
6651da177e4SLinus Torvalds 
6662d7202bfSThomas Graf 	mx = nla_nest_start(skb, RTA_METRICS);
6672d7202bfSThomas Graf 	if (mx == NULL)
6682d7202bfSThomas Graf 		return -ENOBUFS;
6692d7202bfSThomas Graf 
6701da177e4SLinus Torvalds 	for (i = 0; i < RTAX_MAX; i++) {
6712d7202bfSThomas Graf 		if (metrics[i]) {
6722d7202bfSThomas Graf 			valid++;
673a6574349SDavid S. Miller 			if (nla_put_u32(skb, i+1, metrics[i]))
674a6574349SDavid S. Miller 				goto nla_put_failure;
6751da177e4SLinus Torvalds 		}
6762d7202bfSThomas Graf 	}
6771da177e4SLinus Torvalds 
678a57d27fcSDavid S. Miller 	if (!valid) {
679a57d27fcSDavid S. Miller 		nla_nest_cancel(skb, mx);
680a57d27fcSDavid S. Miller 		return 0;
681a57d27fcSDavid S. Miller 	}
6822d7202bfSThomas Graf 
6832d7202bfSThomas Graf 	return nla_nest_end(skb, mx);
6842d7202bfSThomas Graf 
6852d7202bfSThomas Graf nla_put_failure:
686bc3ed28cSThomas Graf 	nla_nest_cancel(skb, mx);
687bc3ed28cSThomas Graf 	return -EMSGSIZE;
6881da177e4SLinus Torvalds }
689e0d087afSEric Dumazet EXPORT_SYMBOL(rtnetlink_put_metrics);
6901da177e4SLinus Torvalds 
691e3703b3dSThomas Graf int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
69287a50699SDavid S. Miller 		       long expires, u32 error)
693e3703b3dSThomas Graf {
694e3703b3dSThomas Graf 	struct rta_cacheinfo ci = {
695a399a805SEric Dumazet 		.rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse),
696e3703b3dSThomas Graf 		.rta_used = dst->__use,
697e3703b3dSThomas Graf 		.rta_clntref = atomic_read(&(dst->__refcnt)),
698e3703b3dSThomas Graf 		.rta_error = error,
699e3703b3dSThomas Graf 		.rta_id =  id,
700e3703b3dSThomas Graf 	};
701e3703b3dSThomas Graf 
7028253947eSLi Wei 	if (expires) {
7038253947eSLi Wei 		unsigned long clock;
704e3703b3dSThomas Graf 
7058253947eSLi Wei 		clock = jiffies_to_clock_t(abs(expires));
7068253947eSLi Wei 		clock = min_t(unsigned long, clock, INT_MAX);
7078253947eSLi Wei 		ci.rta_expires = (expires > 0) ? clock : -clock;
7088253947eSLi Wei 	}
709e3703b3dSThomas Graf 	return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
710e3703b3dSThomas Graf }
711e3703b3dSThomas Graf EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
7121da177e4SLinus Torvalds 
71393b2d4a2SDavid S. Miller static void set_operstate(struct net_device *dev, unsigned char transition)
714b00055aaSStefan Rompf {
715b00055aaSStefan Rompf 	unsigned char operstate = dev->operstate;
716b00055aaSStefan Rompf 
717b00055aaSStefan Rompf 	switch (transition) {
718b00055aaSStefan Rompf 	case IF_OPER_UP:
719b00055aaSStefan Rompf 		if ((operstate == IF_OPER_DORMANT ||
720b00055aaSStefan Rompf 		     operstate == IF_OPER_UNKNOWN) &&
721b00055aaSStefan Rompf 		    !netif_dormant(dev))
722b00055aaSStefan Rompf 			operstate = IF_OPER_UP;
723b00055aaSStefan Rompf 		break;
724b00055aaSStefan Rompf 
725b00055aaSStefan Rompf 	case IF_OPER_DORMANT:
726b00055aaSStefan Rompf 		if (operstate == IF_OPER_UP ||
727b00055aaSStefan Rompf 		    operstate == IF_OPER_UNKNOWN)
728b00055aaSStefan Rompf 			operstate = IF_OPER_DORMANT;
729b00055aaSStefan Rompf 		break;
7303ff50b79SStephen Hemminger 	}
731b00055aaSStefan Rompf 
732b00055aaSStefan Rompf 	if (dev->operstate != operstate) {
733b00055aaSStefan Rompf 		write_lock_bh(&dev_base_lock);
734b00055aaSStefan Rompf 		dev->operstate = operstate;
735b00055aaSStefan Rompf 		write_unlock_bh(&dev_base_lock);
736b00055aaSStefan Rompf 		netdev_state_change(dev);
73793b2d4a2SDavid S. Miller 	}
738b00055aaSStefan Rompf }
739b00055aaSStefan Rompf 
740b1beb681SJiri Benc static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
741b1beb681SJiri Benc {
742b1beb681SJiri Benc 	return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
743b1beb681SJiri Benc 	       (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
744b1beb681SJiri Benc }
745b1beb681SJiri Benc 
7463729d502SPatrick McHardy static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
7473729d502SPatrick McHardy 					   const struct ifinfomsg *ifm)
7483729d502SPatrick McHardy {
7493729d502SPatrick McHardy 	unsigned int flags = ifm->ifi_flags;
7503729d502SPatrick McHardy 
7513729d502SPatrick McHardy 	/* bugwards compatibility: ifi_change == 0 is treated as ~0 */
7523729d502SPatrick McHardy 	if (ifm->ifi_change)
7533729d502SPatrick McHardy 		flags = (flags & ifm->ifi_change) |
754b1beb681SJiri Benc 			(rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
7553729d502SPatrick McHardy 
7563729d502SPatrick McHardy 	return flags;
7573729d502SPatrick McHardy }
7583729d502SPatrick McHardy 
759b60c5115SThomas Graf static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
760be1f3c2cSBen Hutchings 				 const struct rtnl_link_stats64 *b)
7611da177e4SLinus Torvalds {
762b60c5115SThomas Graf 	a->rx_packets = b->rx_packets;
763b60c5115SThomas Graf 	a->tx_packets = b->tx_packets;
764b60c5115SThomas Graf 	a->rx_bytes = b->rx_bytes;
765b60c5115SThomas Graf 	a->tx_bytes = b->tx_bytes;
766b60c5115SThomas Graf 	a->rx_errors = b->rx_errors;
767b60c5115SThomas Graf 	a->tx_errors = b->tx_errors;
768b60c5115SThomas Graf 	a->rx_dropped = b->rx_dropped;
769b60c5115SThomas Graf 	a->tx_dropped = b->tx_dropped;
770b60c5115SThomas Graf 
771b60c5115SThomas Graf 	a->multicast = b->multicast;
772b60c5115SThomas Graf 	a->collisions = b->collisions;
773b60c5115SThomas Graf 
774b60c5115SThomas Graf 	a->rx_length_errors = b->rx_length_errors;
775b60c5115SThomas Graf 	a->rx_over_errors = b->rx_over_errors;
776b60c5115SThomas Graf 	a->rx_crc_errors = b->rx_crc_errors;
777b60c5115SThomas Graf 	a->rx_frame_errors = b->rx_frame_errors;
778b60c5115SThomas Graf 	a->rx_fifo_errors = b->rx_fifo_errors;
779b60c5115SThomas Graf 	a->rx_missed_errors = b->rx_missed_errors;
780b60c5115SThomas Graf 
781b60c5115SThomas Graf 	a->tx_aborted_errors = b->tx_aborted_errors;
782b60c5115SThomas Graf 	a->tx_carrier_errors = b->tx_carrier_errors;
783b60c5115SThomas Graf 	a->tx_fifo_errors = b->tx_fifo_errors;
784b60c5115SThomas Graf 	a->tx_heartbeat_errors = b->tx_heartbeat_errors;
785b60c5115SThomas Graf 	a->tx_window_errors = b->tx_window_errors;
786b60c5115SThomas Graf 
787b60c5115SThomas Graf 	a->rx_compressed = b->rx_compressed;
788b60c5115SThomas Graf 	a->tx_compressed = b->tx_compressed;
78910708f37SJan Engelhardt }
79010708f37SJan Engelhardt 
791be1f3c2cSBen Hutchings static void copy_rtnl_link_stats64(void *v, const struct rtnl_link_stats64 *b)
79210708f37SJan Engelhardt {
793afdcba37SEric Dumazet 	memcpy(v, b, sizeof(*b));
79410708f37SJan Engelhardt }
795b60c5115SThomas Graf 
796c02db8c6SChris Wright /* All VF info */
797115c9b81SGreg Rose static inline int rtnl_vfinfo_size(const struct net_device *dev,
798115c9b81SGreg Rose 				   u32 ext_filter_mask)
799ebc08a6fSWilliams, Mitch A {
800115c9b81SGreg Rose 	if (dev->dev.parent && dev_is_pci(dev->dev.parent) &&
801115c9b81SGreg Rose 	    (ext_filter_mask & RTEXT_FILTER_VF)) {
802c02db8c6SChris Wright 		int num_vfs = dev_num_vf(dev->dev.parent);
803045de01aSScott Feldman 		size_t size = nla_total_size(sizeof(struct nlattr));
804045de01aSScott Feldman 		size += nla_total_size(num_vfs * sizeof(struct nlattr));
805045de01aSScott Feldman 		size += num_vfs *
806045de01aSScott Feldman 			(nla_total_size(sizeof(struct ifla_vf_mac)) +
807045de01aSScott Feldman 			 nla_total_size(sizeof(struct ifla_vf_vlan)) +
808ed616689SSucheta Chakraborty 			 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
809945a3676SJiri Benc 			 nla_total_size(sizeof(struct ifla_vf_rate)) +
810945a3676SJiri Benc 			 nla_total_size(sizeof(struct ifla_vf_link_state)));
811c02db8c6SChris Wright 		return size;
812c02db8c6SChris Wright 	} else
813ebc08a6fSWilliams, Mitch A 		return 0;
814ebc08a6fSWilliams, Mitch A }
815ebc08a6fSWilliams, Mitch A 
816c53864fdSDavid Gibson static size_t rtnl_port_size(const struct net_device *dev,
817c53864fdSDavid Gibson 			     u32 ext_filter_mask)
81857b61080SScott Feldman {
81957b61080SScott Feldman 	size_t port_size = nla_total_size(4)		/* PORT_VF */
82057b61080SScott Feldman 		+ nla_total_size(PORT_PROFILE_MAX)	/* PORT_PROFILE */
82157b61080SScott Feldman 		+ nla_total_size(sizeof(struct ifla_port_vsi))
82257b61080SScott Feldman 							/* PORT_VSI_TYPE */
82357b61080SScott Feldman 		+ nla_total_size(PORT_UUID_MAX)		/* PORT_INSTANCE_UUID */
82457b61080SScott Feldman 		+ nla_total_size(PORT_UUID_MAX)		/* PORT_HOST_UUID */
82557b61080SScott Feldman 		+ nla_total_size(1)			/* PROT_VDP_REQUEST */
82657b61080SScott Feldman 		+ nla_total_size(2);			/* PORT_VDP_RESPONSE */
82757b61080SScott Feldman 	size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
82857b61080SScott Feldman 	size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
82957b61080SScott Feldman 		+ port_size;
83057b61080SScott Feldman 	size_t port_self_size = nla_total_size(sizeof(struct nlattr))
83157b61080SScott Feldman 		+ port_size;
83257b61080SScott Feldman 
833c53864fdSDavid Gibson 	if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
834c53864fdSDavid Gibson 	    !(ext_filter_mask & RTEXT_FILTER_VF))
83557b61080SScott Feldman 		return 0;
83657b61080SScott Feldman 	if (dev_num_vf(dev->dev.parent))
83757b61080SScott Feldman 		return port_self_size + vf_ports_size +
83857b61080SScott Feldman 			vf_port_size * dev_num_vf(dev->dev.parent);
83957b61080SScott Feldman 	else
84057b61080SScott Feldman 		return port_self_size;
84157b61080SScott Feldman }
84257b61080SScott Feldman 
843115c9b81SGreg Rose static noinline size_t if_nlmsg_size(const struct net_device *dev,
844115c9b81SGreg Rose 				     u32 ext_filter_mask)
845339bf98fSThomas Graf {
846339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
847339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
8480b815a1aSStephen Hemminger 	       + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
849339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
850339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rtnl_link_ifmap))
851339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rtnl_link_stats))
852adcfe196SJan Engelhardt 	       + nla_total_size(sizeof(struct rtnl_link_stats64))
853339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
854339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
855339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_TXQLEN */
856339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_WEIGHT */
857339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MTU */
858339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_LINK */
859339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MASTER */
8609a57247fSJiri Pirko 	       + nla_total_size(1) /* IFLA_CARRIER */
861edbc0bb3SBen Greear 	       + nla_total_size(4) /* IFLA_PROMISCUITY */
86276ff5cc9SJiri Pirko 	       + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
86376ff5cc9SJiri Pirko 	       + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
864339bf98fSThomas Graf 	       + nla_total_size(1) /* IFLA_OPERSTATE */
86538f7b870SPatrick McHardy 	       + nla_total_size(1) /* IFLA_LINKMODE */
8662d3b479dSdavid decotigny 	       + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
867115c9b81SGreg Rose 	       + nla_total_size(ext_filter_mask
868115c9b81SGreg Rose 			        & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
869115c9b81SGreg Rose 	       + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
870c53864fdSDavid Gibson 	       + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
871f8ff182cSThomas Graf 	       + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
87266cae9edSJiri Pirko 	       + rtnl_link_get_af_size(dev) /* IFLA_AF_SPEC */
87382f28412SJiri Pirko 	       + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
87482f28412SJiri Pirko 	       + nla_total_size(MAX_PHYS_ITEM_ID_LEN); /* IFLA_PHYS_SWITCH_ID */
875339bf98fSThomas Graf }
876339bf98fSThomas Graf 
87757b61080SScott Feldman static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
87857b61080SScott Feldman {
87957b61080SScott Feldman 	struct nlattr *vf_ports;
88057b61080SScott Feldman 	struct nlattr *vf_port;
88157b61080SScott Feldman 	int vf;
88257b61080SScott Feldman 	int err;
88357b61080SScott Feldman 
88457b61080SScott Feldman 	vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
88557b61080SScott Feldman 	if (!vf_ports)
88657b61080SScott Feldman 		return -EMSGSIZE;
88757b61080SScott Feldman 
88857b61080SScott Feldman 	for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
88957b61080SScott Feldman 		vf_port = nla_nest_start(skb, IFLA_VF_PORT);
8908ca94183SScott Feldman 		if (!vf_port)
8918ca94183SScott Feldman 			goto nla_put_failure;
892a6574349SDavid S. Miller 		if (nla_put_u32(skb, IFLA_PORT_VF, vf))
893a6574349SDavid S. Miller 			goto nla_put_failure;
89457b61080SScott Feldman 		err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
8958ca94183SScott Feldman 		if (err == -EMSGSIZE)
8968ca94183SScott Feldman 			goto nla_put_failure;
89757b61080SScott Feldman 		if (err) {
89857b61080SScott Feldman 			nla_nest_cancel(skb, vf_port);
89957b61080SScott Feldman 			continue;
90057b61080SScott Feldman 		}
90157b61080SScott Feldman 		nla_nest_end(skb, vf_port);
90257b61080SScott Feldman 	}
90357b61080SScott Feldman 
90457b61080SScott Feldman 	nla_nest_end(skb, vf_ports);
90557b61080SScott Feldman 
90657b61080SScott Feldman 	return 0;
9078ca94183SScott Feldman 
9088ca94183SScott Feldman nla_put_failure:
9098ca94183SScott Feldman 	nla_nest_cancel(skb, vf_ports);
9108ca94183SScott Feldman 	return -EMSGSIZE;
91157b61080SScott Feldman }
91257b61080SScott Feldman 
91357b61080SScott Feldman static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
91457b61080SScott Feldman {
91557b61080SScott Feldman 	struct nlattr *port_self;
91657b61080SScott Feldman 	int err;
91757b61080SScott Feldman 
91857b61080SScott Feldman 	port_self = nla_nest_start(skb, IFLA_PORT_SELF);
91957b61080SScott Feldman 	if (!port_self)
92057b61080SScott Feldman 		return -EMSGSIZE;
92157b61080SScott Feldman 
92257b61080SScott Feldman 	err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
92357b61080SScott Feldman 	if (err) {
92457b61080SScott Feldman 		nla_nest_cancel(skb, port_self);
9258ca94183SScott Feldman 		return (err == -EMSGSIZE) ? err : 0;
92657b61080SScott Feldman 	}
92757b61080SScott Feldman 
92857b61080SScott Feldman 	nla_nest_end(skb, port_self);
92957b61080SScott Feldman 
93057b61080SScott Feldman 	return 0;
93157b61080SScott Feldman }
93257b61080SScott Feldman 
933c53864fdSDavid Gibson static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
934c53864fdSDavid Gibson 			  u32 ext_filter_mask)
93557b61080SScott Feldman {
93657b61080SScott Feldman 	int err;
93757b61080SScott Feldman 
938c53864fdSDavid Gibson 	if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
939c53864fdSDavid Gibson 	    !(ext_filter_mask & RTEXT_FILTER_VF))
94057b61080SScott Feldman 		return 0;
94157b61080SScott Feldman 
94257b61080SScott Feldman 	err = rtnl_port_self_fill(skb, dev);
94357b61080SScott Feldman 	if (err)
94457b61080SScott Feldman 		return err;
94557b61080SScott Feldman 
94657b61080SScott Feldman 	if (dev_num_vf(dev->dev.parent)) {
94757b61080SScott Feldman 		err = rtnl_vf_ports_fill(skb, dev);
94857b61080SScott Feldman 		if (err)
94957b61080SScott Feldman 			return err;
95057b61080SScott Feldman 	}
95157b61080SScott Feldman 
95257b61080SScott Feldman 	return 0;
95357b61080SScott Feldman }
95457b61080SScott Feldman 
95566cae9edSJiri Pirko static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
95666cae9edSJiri Pirko {
95766cae9edSJiri Pirko 	int err;
95802637fceSJiri Pirko 	struct netdev_phys_item_id ppid;
95966cae9edSJiri Pirko 
96066cae9edSJiri Pirko 	err = dev_get_phys_port_id(dev, &ppid);
96166cae9edSJiri Pirko 	if (err) {
96266cae9edSJiri Pirko 		if (err == -EOPNOTSUPP)
96366cae9edSJiri Pirko 			return 0;
96466cae9edSJiri Pirko 		return err;
96566cae9edSJiri Pirko 	}
96666cae9edSJiri Pirko 
96766cae9edSJiri Pirko 	if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
96866cae9edSJiri Pirko 		return -EMSGSIZE;
96966cae9edSJiri Pirko 
97066cae9edSJiri Pirko 	return 0;
97166cae9edSJiri Pirko }
97266cae9edSJiri Pirko 
97382f28412SJiri Pirko static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
97482f28412SJiri Pirko {
97582f28412SJiri Pirko 	int err;
97682f28412SJiri Pirko 	struct netdev_phys_item_id psid;
97782f28412SJiri Pirko 
97882f28412SJiri Pirko 	err = netdev_switch_parent_id_get(dev, &psid);
97982f28412SJiri Pirko 	if (err) {
98082f28412SJiri Pirko 		if (err == -EOPNOTSUPP)
98182f28412SJiri Pirko 			return 0;
98282f28412SJiri Pirko 		return err;
98382f28412SJiri Pirko 	}
98482f28412SJiri Pirko 
98582f28412SJiri Pirko 	if (nla_put(skb, IFLA_PHYS_SWITCH_ID, psid.id_len, psid.id))
98682f28412SJiri Pirko 		return -EMSGSIZE;
98782f28412SJiri Pirko 
98882f28412SJiri Pirko 	return 0;
98982f28412SJiri Pirko }
99082f28412SJiri Pirko 
991b60c5115SThomas Graf static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
992575c3e2aSPatrick McHardy 			    int type, u32 pid, u32 seq, u32 change,
993115c9b81SGreg Rose 			    unsigned int flags, u32 ext_filter_mask)
994b60c5115SThomas Graf {
995b60c5115SThomas Graf 	struct ifinfomsg *ifm;
9961da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
99728172739SEric Dumazet 	struct rtnl_link_stats64 temp;
998be1f3c2cSBen Hutchings 	const struct rtnl_link_stats64 *stats;
999f8ff182cSThomas Graf 	struct nlattr *attr, *af_spec;
1000f8ff182cSThomas Graf 	struct rtnl_af_ops *af_ops;
1001898e5061SJiri Pirko 	struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
10021da177e4SLinus Torvalds 
10032907c35fSEric Dumazet 	ASSERT_RTNL();
1004b60c5115SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1005b60c5115SThomas Graf 	if (nlh == NULL)
100626932566SPatrick McHardy 		return -EMSGSIZE;
10071da177e4SLinus Torvalds 
1008b60c5115SThomas Graf 	ifm = nlmsg_data(nlh);
1009b60c5115SThomas Graf 	ifm->ifi_family = AF_UNSPEC;
1010b60c5115SThomas Graf 	ifm->__ifi_pad = 0;
1011b60c5115SThomas Graf 	ifm->ifi_type = dev->type;
1012b60c5115SThomas Graf 	ifm->ifi_index = dev->ifindex;
1013b60c5115SThomas Graf 	ifm->ifi_flags = dev_get_flags(dev);
1014b60c5115SThomas Graf 	ifm->ifi_change = change;
10151da177e4SLinus Torvalds 
1016a6574349SDavid S. Miller 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1017a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1018a6574349SDavid S. Miller 	    nla_put_u8(skb, IFLA_OPERSTATE,
1019a6574349SDavid S. Miller 		       netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1020a6574349SDavid S. Miller 	    nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1021a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1022a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_GROUP, dev->group) ||
1023edbc0bb3SBen Greear 	    nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
102476ff5cc9SJiri Pirko 	    nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
10251d69c2b3SMark A. Greer #ifdef CONFIG_RPS
102676ff5cc9SJiri Pirko 	    nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
10271d69c2b3SMark A. Greer #endif
1028a6574349SDavid S. Miller 	    (dev->ifindex != dev->iflink &&
1029a6574349SDavid S. Miller 	     nla_put_u32(skb, IFLA_LINK, dev->iflink)) ||
1030898e5061SJiri Pirko 	    (upper_dev &&
1031898e5061SJiri Pirko 	     nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) ||
10329a57247fSJiri Pirko 	    nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
1033a6574349SDavid S. Miller 	    (dev->qdisc &&
1034a6574349SDavid S. Miller 	     nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
1035a6574349SDavid S. Miller 	    (dev->ifalias &&
10362d3b479dSdavid decotigny 	     nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)) ||
10372d3b479dSdavid decotigny 	    nla_put_u32(skb, IFLA_CARRIER_CHANGES,
10382d3b479dSdavid decotigny 			atomic_read(&dev->carrier_changes)))
1039a6574349SDavid S. Miller 		goto nla_put_failure;
10400b815a1aSStephen Hemminger 
1041b00055aaSStefan Rompf 	if (1) {
10421da177e4SLinus Torvalds 		struct rtnl_link_ifmap map = {
10431da177e4SLinus Torvalds 			.mem_start   = dev->mem_start,
10441da177e4SLinus Torvalds 			.mem_end     = dev->mem_end,
10451da177e4SLinus Torvalds 			.base_addr   = dev->base_addr,
10461da177e4SLinus Torvalds 			.irq         = dev->irq,
10471da177e4SLinus Torvalds 			.dma         = dev->dma,
10481da177e4SLinus Torvalds 			.port        = dev->if_port,
10491da177e4SLinus Torvalds 		};
1050a6574349SDavid S. Miller 		if (nla_put(skb, IFLA_MAP, sizeof(map), &map))
1051a6574349SDavid S. Miller 			goto nla_put_failure;
10521da177e4SLinus Torvalds 	}
10531da177e4SLinus Torvalds 
10541da177e4SLinus Torvalds 	if (dev->addr_len) {
1055a6574349SDavid S. Miller 		if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1056a6574349SDavid S. Miller 		    nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1057a6574349SDavid S. Miller 			goto nla_put_failure;
10581da177e4SLinus Torvalds 	}
10591da177e4SLinus Torvalds 
106066cae9edSJiri Pirko 	if (rtnl_phys_port_id_fill(skb, dev))
106166cae9edSJiri Pirko 		goto nla_put_failure;
106266cae9edSJiri Pirko 
106382f28412SJiri Pirko 	if (rtnl_phys_switch_id_fill(skb, dev))
106482f28412SJiri Pirko 		goto nla_put_failure;
106582f28412SJiri Pirko 
1066b60c5115SThomas Graf 	attr = nla_reserve(skb, IFLA_STATS,
1067b60c5115SThomas Graf 			sizeof(struct rtnl_link_stats));
1068b60c5115SThomas Graf 	if (attr == NULL)
1069b60c5115SThomas Graf 		goto nla_put_failure;
1070b60c5115SThomas Graf 
107128172739SEric Dumazet 	stats = dev_get_stats(dev, &temp);
1072b60c5115SThomas Graf 	copy_rtnl_link_stats(nla_data(attr), stats);
10731da177e4SLinus Torvalds 
107410708f37SJan Engelhardt 	attr = nla_reserve(skb, IFLA_STATS64,
107510708f37SJan Engelhardt 			sizeof(struct rtnl_link_stats64));
107610708f37SJan Engelhardt 	if (attr == NULL)
107710708f37SJan Engelhardt 		goto nla_put_failure;
107810708f37SJan Engelhardt 	copy_rtnl_link_stats64(nla_data(attr), stats);
107910708f37SJan Engelhardt 
1080a6574349SDavid S. Miller 	if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) &&
1081a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent)))
1082a6574349SDavid S. Miller 		goto nla_put_failure;
108357b61080SScott Feldman 
1084115c9b81SGreg Rose 	if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent
1085115c9b81SGreg Rose 	    && (ext_filter_mask & RTEXT_FILTER_VF)) {
1086ebc08a6fSWilliams, Mitch A 		int i;
1087ebc08a6fSWilliams, Mitch A 
1088c02db8c6SChris Wright 		struct nlattr *vfinfo, *vf;
1089c02db8c6SChris Wright 		int num_vfs = dev_num_vf(dev->dev.parent);
1090c02db8c6SChris Wright 
1091c02db8c6SChris Wright 		vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1092c02db8c6SChris Wright 		if (!vfinfo)
1093c02db8c6SChris Wright 			goto nla_put_failure;
1094c02db8c6SChris Wright 		for (i = 0; i < num_vfs; i++) {
1095c02db8c6SChris Wright 			struct ifla_vf_info ivi;
1096c02db8c6SChris Wright 			struct ifla_vf_mac vf_mac;
1097c02db8c6SChris Wright 			struct ifla_vf_vlan vf_vlan;
1098ed616689SSucheta Chakraborty 			struct ifla_vf_rate vf_rate;
1099c02db8c6SChris Wright 			struct ifla_vf_tx_rate vf_tx_rate;
11005f8444a3SGreg Rose 			struct ifla_vf_spoofchk vf_spoofchk;
11011d8faf48SRony Efraim 			struct ifla_vf_link_state vf_linkstate;
11025f8444a3SGreg Rose 
11035f8444a3SGreg Rose 			/*
11045f8444a3SGreg Rose 			 * Not all SR-IOV capable drivers support the
11055f8444a3SGreg Rose 			 * spoofcheck query.  Preset to -1 so the user
11065f8444a3SGreg Rose 			 * space tool can detect that the driver didn't
11075f8444a3SGreg Rose 			 * report anything.
11085f8444a3SGreg Rose 			 */
11095f8444a3SGreg Rose 			ivi.spoofchk = -1;
111084d73cd3SMathias Krause 			memset(ivi.mac, 0, sizeof(ivi.mac));
11111d8faf48SRony Efraim 			/* The default value for VF link state is "auto"
11121d8faf48SRony Efraim 			 * IFLA_VF_LINK_STATE_AUTO which equals zero
11131d8faf48SRony Efraim 			 */
11141d8faf48SRony Efraim 			ivi.linkstate = 0;
1115ebc08a6fSWilliams, Mitch A 			if (dev->netdev_ops->ndo_get_vf_config(dev, i, &ivi))
1116ebc08a6fSWilliams, Mitch A 				break;
11175f8444a3SGreg Rose 			vf_mac.vf =
11185f8444a3SGreg Rose 				vf_vlan.vf =
1119ed616689SSucheta Chakraborty 				vf_rate.vf =
11205f8444a3SGreg Rose 				vf_tx_rate.vf =
11211d8faf48SRony Efraim 				vf_spoofchk.vf =
11221d8faf48SRony Efraim 				vf_linkstate.vf = ivi.vf;
11235f8444a3SGreg Rose 
1124c02db8c6SChris Wright 			memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1125c02db8c6SChris Wright 			vf_vlan.vlan = ivi.vlan;
1126c02db8c6SChris Wright 			vf_vlan.qos = ivi.qos;
1127ed616689SSucheta Chakraborty 			vf_tx_rate.rate = ivi.max_tx_rate;
1128ed616689SSucheta Chakraborty 			vf_rate.min_tx_rate = ivi.min_tx_rate;
1129ed616689SSucheta Chakraborty 			vf_rate.max_tx_rate = ivi.max_tx_rate;
11305f8444a3SGreg Rose 			vf_spoofchk.setting = ivi.spoofchk;
11311d8faf48SRony Efraim 			vf_linkstate.link_state = ivi.linkstate;
1132c02db8c6SChris Wright 			vf = nla_nest_start(skb, IFLA_VF_INFO);
1133c02db8c6SChris Wright 			if (!vf) {
1134c02db8c6SChris Wright 				nla_nest_cancel(skb, vfinfo);
1135c02db8c6SChris Wright 				goto nla_put_failure;
1136ebc08a6fSWilliams, Mitch A 			}
1137a6574349SDavid S. Miller 			if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1138a6574349SDavid S. Miller 			    nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1139ed616689SSucheta Chakraborty 			    nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1140ed616689SSucheta Chakraborty 				    &vf_rate) ||
1141a6574349SDavid S. Miller 			    nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1142a6574349SDavid S. Miller 				    &vf_tx_rate) ||
1143a6574349SDavid S. Miller 			    nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
11441d8faf48SRony Efraim 				    &vf_spoofchk) ||
11451d8faf48SRony Efraim 			    nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
11461d8faf48SRony Efraim 				    &vf_linkstate))
1147a6574349SDavid S. Miller 				goto nla_put_failure;
1148c02db8c6SChris Wright 			nla_nest_end(skb, vf);
1149c02db8c6SChris Wright 		}
1150c02db8c6SChris Wright 		nla_nest_end(skb, vfinfo);
1151ebc08a6fSWilliams, Mitch A 	}
115257b61080SScott Feldman 
1153c53864fdSDavid Gibson 	if (rtnl_port_fill(skb, dev, ext_filter_mask))
115457b61080SScott Feldman 		goto nla_put_failure;
115557b61080SScott Feldman 
1156ba7d49b1SJiri Pirko 	if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
115738f7b870SPatrick McHardy 		if (rtnl_link_fill(skb, dev) < 0)
115838f7b870SPatrick McHardy 			goto nla_put_failure;
115938f7b870SPatrick McHardy 	}
116038f7b870SPatrick McHardy 
1161f8ff182cSThomas Graf 	if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
1162f8ff182cSThomas Graf 		goto nla_put_failure;
1163f8ff182cSThomas Graf 
1164f8ff182cSThomas Graf 	list_for_each_entry(af_ops, &rtnl_af_ops, list) {
1165f8ff182cSThomas Graf 		if (af_ops->fill_link_af) {
1166f8ff182cSThomas Graf 			struct nlattr *af;
1167f8ff182cSThomas Graf 			int err;
1168f8ff182cSThomas Graf 
1169f8ff182cSThomas Graf 			if (!(af = nla_nest_start(skb, af_ops->family)))
1170f8ff182cSThomas Graf 				goto nla_put_failure;
1171f8ff182cSThomas Graf 
1172f8ff182cSThomas Graf 			err = af_ops->fill_link_af(skb, dev);
1173f8ff182cSThomas Graf 
1174f8ff182cSThomas Graf 			/*
1175f8ff182cSThomas Graf 			 * Caller may return ENODATA to indicate that there
1176f8ff182cSThomas Graf 			 * was no data to be dumped. This is not an error, it
1177f8ff182cSThomas Graf 			 * means we should trim the attribute header and
1178f8ff182cSThomas Graf 			 * continue.
1179f8ff182cSThomas Graf 			 */
1180f8ff182cSThomas Graf 			if (err == -ENODATA)
1181f8ff182cSThomas Graf 				nla_nest_cancel(skb, af);
1182f8ff182cSThomas Graf 			else if (err < 0)
1183f8ff182cSThomas Graf 				goto nla_put_failure;
1184f8ff182cSThomas Graf 
1185f8ff182cSThomas Graf 			nla_nest_end(skb, af);
1186f8ff182cSThomas Graf 		}
1187f8ff182cSThomas Graf 	}
1188f8ff182cSThomas Graf 
1189f8ff182cSThomas Graf 	nla_nest_end(skb, af_spec);
1190f8ff182cSThomas Graf 
1191b60c5115SThomas Graf 	return nlmsg_end(skb, nlh);
1192b60c5115SThomas Graf 
1193b60c5115SThomas Graf nla_put_failure:
119426932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
119526932566SPatrick McHardy 	return -EMSGSIZE;
11961da177e4SLinus Torvalds }
11971da177e4SLinus Torvalds 
1198f7b12606SJiri Pirko static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1199f7b12606SJiri Pirko 	[IFLA_IFNAME]		= { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1200f7b12606SJiri Pirko 	[IFLA_ADDRESS]		= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1201f7b12606SJiri Pirko 	[IFLA_BROADCAST]	= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1202f7b12606SJiri Pirko 	[IFLA_MAP]		= { .len = sizeof(struct rtnl_link_ifmap) },
1203f7b12606SJiri Pirko 	[IFLA_MTU]		= { .type = NLA_U32 },
1204f7b12606SJiri Pirko 	[IFLA_LINK]		= { .type = NLA_U32 },
1205f7b12606SJiri Pirko 	[IFLA_MASTER]		= { .type = NLA_U32 },
1206f7b12606SJiri Pirko 	[IFLA_CARRIER]		= { .type = NLA_U8 },
1207f7b12606SJiri Pirko 	[IFLA_TXQLEN]		= { .type = NLA_U32 },
1208f7b12606SJiri Pirko 	[IFLA_WEIGHT]		= { .type = NLA_U32 },
1209f7b12606SJiri Pirko 	[IFLA_OPERSTATE]	= { .type = NLA_U8 },
1210f7b12606SJiri Pirko 	[IFLA_LINKMODE]		= { .type = NLA_U8 },
1211f7b12606SJiri Pirko 	[IFLA_LINKINFO]		= { .type = NLA_NESTED },
1212f7b12606SJiri Pirko 	[IFLA_NET_NS_PID]	= { .type = NLA_U32 },
1213f7b12606SJiri Pirko 	[IFLA_NET_NS_FD]	= { .type = NLA_U32 },
1214f7b12606SJiri Pirko 	[IFLA_IFALIAS]	        = { .type = NLA_STRING, .len = IFALIASZ-1 },
1215f7b12606SJiri Pirko 	[IFLA_VFINFO_LIST]	= {. type = NLA_NESTED },
1216f7b12606SJiri Pirko 	[IFLA_VF_PORTS]		= { .type = NLA_NESTED },
1217f7b12606SJiri Pirko 	[IFLA_PORT_SELF]	= { .type = NLA_NESTED },
1218f7b12606SJiri Pirko 	[IFLA_AF_SPEC]		= { .type = NLA_NESTED },
1219f7b12606SJiri Pirko 	[IFLA_EXT_MASK]		= { .type = NLA_U32 },
1220f7b12606SJiri Pirko 	[IFLA_PROMISCUITY]	= { .type = NLA_U32 },
1221f7b12606SJiri Pirko 	[IFLA_NUM_TX_QUEUES]	= { .type = NLA_U32 },
1222f7b12606SJiri Pirko 	[IFLA_NUM_RX_QUEUES]	= { .type = NLA_U32 },
122302637fceSJiri Pirko 	[IFLA_PHYS_PORT_ID]	= { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
12242d3b479dSdavid decotigny 	[IFLA_CARRIER_CHANGES]	= { .type = NLA_U32 },  /* ignored */
122582f28412SJiri Pirko 	[IFLA_PHYS_SWITCH_ID]	= { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1226f7b12606SJiri Pirko };
1227f7b12606SJiri Pirko 
1228f7b12606SJiri Pirko static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1229f7b12606SJiri Pirko 	[IFLA_INFO_KIND]	= { .type = NLA_STRING },
1230f7b12606SJiri Pirko 	[IFLA_INFO_DATA]	= { .type = NLA_NESTED },
1231f7b12606SJiri Pirko 	[IFLA_INFO_SLAVE_KIND]	= { .type = NLA_STRING },
1232f7b12606SJiri Pirko 	[IFLA_INFO_SLAVE_DATA]	= { .type = NLA_NESTED },
1233f7b12606SJiri Pirko };
1234f7b12606SJiri Pirko 
1235f7b12606SJiri Pirko static const struct nla_policy ifla_vfinfo_policy[IFLA_VF_INFO_MAX+1] = {
1236f7b12606SJiri Pirko 	[IFLA_VF_INFO]		= { .type = NLA_NESTED },
1237f7b12606SJiri Pirko };
1238f7b12606SJiri Pirko 
1239f7b12606SJiri Pirko static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1240f7b12606SJiri Pirko 	[IFLA_VF_MAC]		= { .type = NLA_BINARY,
1241f7b12606SJiri Pirko 				    .len = sizeof(struct ifla_vf_mac) },
1242f7b12606SJiri Pirko 	[IFLA_VF_VLAN]		= { .type = NLA_BINARY,
1243f7b12606SJiri Pirko 				    .len = sizeof(struct ifla_vf_vlan) },
1244f7b12606SJiri Pirko 	[IFLA_VF_TX_RATE]	= { .type = NLA_BINARY,
1245f7b12606SJiri Pirko 				    .len = sizeof(struct ifla_vf_tx_rate) },
1246f7b12606SJiri Pirko 	[IFLA_VF_SPOOFCHK]	= { .type = NLA_BINARY,
1247f7b12606SJiri Pirko 				    .len = sizeof(struct ifla_vf_spoofchk) },
1248ed616689SSucheta Chakraborty 	[IFLA_VF_RATE]		= { .type = NLA_BINARY,
1249ed616689SSucheta Chakraborty 				    .len = sizeof(struct ifla_vf_rate) },
1250c5b46160SDoug Ledford 	[IFLA_VF_LINK_STATE]	= { .type = NLA_BINARY,
1251c5b46160SDoug Ledford 				    .len = sizeof(struct ifla_vf_link_state) },
1252f7b12606SJiri Pirko };
1253f7b12606SJiri Pirko 
1254f7b12606SJiri Pirko static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1255f7b12606SJiri Pirko 	[IFLA_PORT_VF]		= { .type = NLA_U32 },
1256f7b12606SJiri Pirko 	[IFLA_PORT_PROFILE]	= { .type = NLA_STRING,
1257f7b12606SJiri Pirko 				    .len = PORT_PROFILE_MAX },
1258f7b12606SJiri Pirko 	[IFLA_PORT_VSI_TYPE]	= { .type = NLA_BINARY,
1259f7b12606SJiri Pirko 				    .len = sizeof(struct ifla_port_vsi)},
1260f7b12606SJiri Pirko 	[IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1261f7b12606SJiri Pirko 				      .len = PORT_UUID_MAX },
1262f7b12606SJiri Pirko 	[IFLA_PORT_HOST_UUID]	= { .type = NLA_STRING,
1263f7b12606SJiri Pirko 				    .len = PORT_UUID_MAX },
1264f7b12606SJiri Pirko 	[IFLA_PORT_REQUEST]	= { .type = NLA_U8, },
1265f7b12606SJiri Pirko 	[IFLA_PORT_RESPONSE]	= { .type = NLA_U16, },
1266f7b12606SJiri Pirko };
1267f7b12606SJiri Pirko 
1268b60c5115SThomas Graf static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
12691da177e4SLinus Torvalds {
12703b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
12717c28bd0bSEric Dumazet 	int h, s_h;
12727c28bd0bSEric Dumazet 	int idx = 0, s_idx;
12731da177e4SLinus Torvalds 	struct net_device *dev;
12747c28bd0bSEric Dumazet 	struct hlist_head *head;
1275115c9b81SGreg Rose 	struct nlattr *tb[IFLA_MAX+1];
1276115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
1277973462bbSDavid Gibson 	int err;
1278e5eca6d4SMichal Schmidt 	int hdrlen;
12791da177e4SLinus Torvalds 
12807c28bd0bSEric Dumazet 	s_h = cb->args[0];
12817c28bd0bSEric Dumazet 	s_idx = cb->args[1];
12827c28bd0bSEric Dumazet 
1283e67f88ddSEric Dumazet 	rcu_read_lock();
12844e985adaSThomas Graf 	cb->seq = net->dev_base_seq;
12854e985adaSThomas Graf 
1286e5eca6d4SMichal Schmidt 	/* A hack to preserve kernel<->userspace interface.
1287e5eca6d4SMichal Schmidt 	 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1288e5eca6d4SMichal Schmidt 	 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1289e5eca6d4SMichal Schmidt 	 * what iproute2 < v3.9.0 used.
1290e5eca6d4SMichal Schmidt 	 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1291e5eca6d4SMichal Schmidt 	 * attribute, its netlink message is shorter than struct ifinfomsg.
1292e5eca6d4SMichal Schmidt 	 */
1293e5eca6d4SMichal Schmidt 	hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ?
1294e5eca6d4SMichal Schmidt 		 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1295e5eca6d4SMichal Schmidt 
1296e5eca6d4SMichal Schmidt 	if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
1297115c9b81SGreg Rose 
1298115c9b81SGreg Rose 		if (tb[IFLA_EXT_MASK])
1299115c9b81SGreg Rose 			ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1300a4b64fbeSEric Dumazet 	}
1301115c9b81SGreg Rose 
13027c28bd0bSEric Dumazet 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
13037562f876SPavel Emelianov 		idx = 0;
13047c28bd0bSEric Dumazet 		head = &net->dev_index_head[h];
1305b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu(dev, head, index_hlist) {
13061da177e4SLinus Torvalds 			if (idx < s_idx)
13077562f876SPavel Emelianov 				goto cont;
1308973462bbSDavid Gibson 			err = rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
130915e47304SEric W. Biederman 					       NETLINK_CB(cb->skb).portid,
13107c28bd0bSEric Dumazet 					       cb->nlh->nlmsg_seq, 0,
1311115c9b81SGreg Rose 					       NLM_F_MULTI,
1312973462bbSDavid Gibson 					       ext_filter_mask);
1313973462bbSDavid Gibson 			/* If we ran out of room on the first message,
1314973462bbSDavid Gibson 			 * we're in trouble
1315973462bbSDavid Gibson 			 */
1316973462bbSDavid Gibson 			WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
1317973462bbSDavid Gibson 
1318973462bbSDavid Gibson 			if (err <= 0)
13197c28bd0bSEric Dumazet 				goto out;
13204e985adaSThomas Graf 
13214e985adaSThomas Graf 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
13227562f876SPavel Emelianov cont:
13237562f876SPavel Emelianov 			idx++;
13241da177e4SLinus Torvalds 		}
13257c28bd0bSEric Dumazet 	}
13267c28bd0bSEric Dumazet out:
1327e67f88ddSEric Dumazet 	rcu_read_unlock();
13287c28bd0bSEric Dumazet 	cb->args[1] = idx;
13297c28bd0bSEric Dumazet 	cb->args[0] = h;
13301da177e4SLinus Torvalds 
13311da177e4SLinus Torvalds 	return skb->len;
13321da177e4SLinus Torvalds }
13331da177e4SLinus Torvalds 
1334f7b12606SJiri Pirko int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len)
1335f7b12606SJiri Pirko {
1336f7b12606SJiri Pirko 	return nla_parse(tb, IFLA_MAX, head, len, ifla_policy);
1337f7b12606SJiri Pirko }
1338f7b12606SJiri Pirko EXPORT_SYMBOL(rtnl_nla_parse_ifla);
133957b61080SScott Feldman 
134081adee47SEric W. Biederman struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
134181adee47SEric W. Biederman {
134281adee47SEric W. Biederman 	struct net *net;
134381adee47SEric W. Biederman 	/* Examine the link attributes and figure out which
134481adee47SEric W. Biederman 	 * network namespace we are talking about.
134581adee47SEric W. Biederman 	 */
134681adee47SEric W. Biederman 	if (tb[IFLA_NET_NS_PID])
134781adee47SEric W. Biederman 		net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
1348f0630529SEric W. Biederman 	else if (tb[IFLA_NET_NS_FD])
1349f0630529SEric W. Biederman 		net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
135081adee47SEric W. Biederman 	else
135181adee47SEric W. Biederman 		net = get_net(src_net);
135281adee47SEric W. Biederman 	return net;
135381adee47SEric W. Biederman }
135481adee47SEric W. Biederman EXPORT_SYMBOL(rtnl_link_get_net);
135581adee47SEric W. Biederman 
13561840bb13SThomas Graf static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
13571840bb13SThomas Graf {
13581840bb13SThomas Graf 	if (dev) {
13591840bb13SThomas Graf 		if (tb[IFLA_ADDRESS] &&
13601840bb13SThomas Graf 		    nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
13611840bb13SThomas Graf 			return -EINVAL;
13621840bb13SThomas Graf 
13631840bb13SThomas Graf 		if (tb[IFLA_BROADCAST] &&
13641840bb13SThomas Graf 		    nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
13651840bb13SThomas Graf 			return -EINVAL;
13661840bb13SThomas Graf 	}
13671840bb13SThomas Graf 
1368cf7afbfeSThomas Graf 	if (tb[IFLA_AF_SPEC]) {
1369cf7afbfeSThomas Graf 		struct nlattr *af;
1370cf7afbfeSThomas Graf 		int rem, err;
1371cf7afbfeSThomas Graf 
1372cf7afbfeSThomas Graf 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1373cf7afbfeSThomas Graf 			const struct rtnl_af_ops *af_ops;
1374cf7afbfeSThomas Graf 
1375cf7afbfeSThomas Graf 			if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1376cf7afbfeSThomas Graf 				return -EAFNOSUPPORT;
1377cf7afbfeSThomas Graf 
1378cf7afbfeSThomas Graf 			if (!af_ops->set_link_af)
1379cf7afbfeSThomas Graf 				return -EOPNOTSUPP;
1380cf7afbfeSThomas Graf 
1381cf7afbfeSThomas Graf 			if (af_ops->validate_link_af) {
13826d3a9a68SKurt Van Dijck 				err = af_ops->validate_link_af(dev, af);
1383cf7afbfeSThomas Graf 				if (err < 0)
1384cf7afbfeSThomas Graf 					return err;
1385cf7afbfeSThomas Graf 			}
1386cf7afbfeSThomas Graf 		}
1387cf7afbfeSThomas Graf 	}
1388cf7afbfeSThomas Graf 
13891840bb13SThomas Graf 	return 0;
13901840bb13SThomas Graf }
13911840bb13SThomas Graf 
1392c02db8c6SChris Wright static int do_setvfinfo(struct net_device *dev, struct nlattr *attr)
1393c02db8c6SChris Wright {
1394c02db8c6SChris Wright 	int rem, err = -EINVAL;
1395c02db8c6SChris Wright 	struct nlattr *vf;
1396c02db8c6SChris Wright 	const struct net_device_ops *ops = dev->netdev_ops;
1397c02db8c6SChris Wright 
1398c02db8c6SChris Wright 	nla_for_each_nested(vf, attr, rem) {
1399c02db8c6SChris Wright 		switch (nla_type(vf)) {
1400c02db8c6SChris Wright 		case IFLA_VF_MAC: {
1401c02db8c6SChris Wright 			struct ifla_vf_mac *ivm;
1402c02db8c6SChris Wright 			ivm = nla_data(vf);
1403c02db8c6SChris Wright 			err = -EOPNOTSUPP;
1404c02db8c6SChris Wright 			if (ops->ndo_set_vf_mac)
1405c02db8c6SChris Wright 				err = ops->ndo_set_vf_mac(dev, ivm->vf,
1406c02db8c6SChris Wright 							  ivm->mac);
1407c02db8c6SChris Wright 			break;
1408c02db8c6SChris Wright 		}
1409c02db8c6SChris Wright 		case IFLA_VF_VLAN: {
1410c02db8c6SChris Wright 			struct ifla_vf_vlan *ivv;
1411c02db8c6SChris Wright 			ivv = nla_data(vf);
1412c02db8c6SChris Wright 			err = -EOPNOTSUPP;
1413c02db8c6SChris Wright 			if (ops->ndo_set_vf_vlan)
1414c02db8c6SChris Wright 				err = ops->ndo_set_vf_vlan(dev, ivv->vf,
1415c02db8c6SChris Wright 							   ivv->vlan,
1416c02db8c6SChris Wright 							   ivv->qos);
1417c02db8c6SChris Wright 			break;
1418c02db8c6SChris Wright 		}
1419c02db8c6SChris Wright 		case IFLA_VF_TX_RATE: {
1420c02db8c6SChris Wright 			struct ifla_vf_tx_rate *ivt;
1421ed616689SSucheta Chakraborty 			struct ifla_vf_info ivf;
1422c02db8c6SChris Wright 			ivt = nla_data(vf);
1423c02db8c6SChris Wright 			err = -EOPNOTSUPP;
1424ed616689SSucheta Chakraborty 			if (ops->ndo_get_vf_config)
1425ed616689SSucheta Chakraborty 				err = ops->ndo_get_vf_config(dev, ivt->vf,
1426ed616689SSucheta Chakraborty 							     &ivf);
1427ed616689SSucheta Chakraborty 			if (err)
1428ed616689SSucheta Chakraborty 				break;
1429ed616689SSucheta Chakraborty 			err = -EOPNOTSUPP;
1430ed616689SSucheta Chakraborty 			if (ops->ndo_set_vf_rate)
1431ed616689SSucheta Chakraborty 				err = ops->ndo_set_vf_rate(dev, ivt->vf,
1432ed616689SSucheta Chakraborty 							   ivf.min_tx_rate,
1433c02db8c6SChris Wright 							   ivt->rate);
1434c02db8c6SChris Wright 			break;
1435c02db8c6SChris Wright 		}
1436ed616689SSucheta Chakraborty 		case IFLA_VF_RATE: {
1437ed616689SSucheta Chakraborty 			struct ifla_vf_rate *ivt;
1438ed616689SSucheta Chakraborty 			ivt = nla_data(vf);
1439ed616689SSucheta Chakraborty 			err = -EOPNOTSUPP;
1440ed616689SSucheta Chakraborty 			if (ops->ndo_set_vf_rate)
1441ed616689SSucheta Chakraborty 				err = ops->ndo_set_vf_rate(dev, ivt->vf,
1442ed616689SSucheta Chakraborty 							   ivt->min_tx_rate,
1443ed616689SSucheta Chakraborty 							   ivt->max_tx_rate);
1444ed616689SSucheta Chakraborty 			break;
1445ed616689SSucheta Chakraborty 		}
14465f8444a3SGreg Rose 		case IFLA_VF_SPOOFCHK: {
14475f8444a3SGreg Rose 			struct ifla_vf_spoofchk *ivs;
14485f8444a3SGreg Rose 			ivs = nla_data(vf);
14495f8444a3SGreg Rose 			err = -EOPNOTSUPP;
14505f8444a3SGreg Rose 			if (ops->ndo_set_vf_spoofchk)
14515f8444a3SGreg Rose 				err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
14525f8444a3SGreg Rose 							       ivs->setting);
14535f8444a3SGreg Rose 			break;
14545f8444a3SGreg Rose 		}
14551d8faf48SRony Efraim 		case IFLA_VF_LINK_STATE: {
14561d8faf48SRony Efraim 			struct ifla_vf_link_state *ivl;
14571d8faf48SRony Efraim 			ivl = nla_data(vf);
14581d8faf48SRony Efraim 			err = -EOPNOTSUPP;
14591d8faf48SRony Efraim 			if (ops->ndo_set_vf_link_state)
14601d8faf48SRony Efraim 				err = ops->ndo_set_vf_link_state(dev, ivl->vf,
14611d8faf48SRony Efraim 								 ivl->link_state);
14621d8faf48SRony Efraim 			break;
14631d8faf48SRony Efraim 		}
1464c02db8c6SChris Wright 		default:
1465c02db8c6SChris Wright 			err = -EINVAL;
1466c02db8c6SChris Wright 			break;
1467c02db8c6SChris Wright 		}
1468c02db8c6SChris Wright 		if (err)
1469c02db8c6SChris Wright 			break;
1470c02db8c6SChris Wright 	}
1471c02db8c6SChris Wright 	return err;
1472c02db8c6SChris Wright }
1473c02db8c6SChris Wright 
1474fbaec0eaSJiri Pirko static int do_set_master(struct net_device *dev, int ifindex)
1475fbaec0eaSJiri Pirko {
1476898e5061SJiri Pirko 	struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
1477fbaec0eaSJiri Pirko 	const struct net_device_ops *ops;
1478fbaec0eaSJiri Pirko 	int err;
1479fbaec0eaSJiri Pirko 
1480898e5061SJiri Pirko 	if (upper_dev) {
1481898e5061SJiri Pirko 		if (upper_dev->ifindex == ifindex)
1482fbaec0eaSJiri Pirko 			return 0;
1483898e5061SJiri Pirko 		ops = upper_dev->netdev_ops;
1484fbaec0eaSJiri Pirko 		if (ops->ndo_del_slave) {
1485898e5061SJiri Pirko 			err = ops->ndo_del_slave(upper_dev, dev);
1486fbaec0eaSJiri Pirko 			if (err)
1487fbaec0eaSJiri Pirko 				return err;
1488fbaec0eaSJiri Pirko 		} else {
1489fbaec0eaSJiri Pirko 			return -EOPNOTSUPP;
1490fbaec0eaSJiri Pirko 		}
1491fbaec0eaSJiri Pirko 	}
1492fbaec0eaSJiri Pirko 
1493fbaec0eaSJiri Pirko 	if (ifindex) {
1494898e5061SJiri Pirko 		upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
1495898e5061SJiri Pirko 		if (!upper_dev)
1496fbaec0eaSJiri Pirko 			return -EINVAL;
1497898e5061SJiri Pirko 		ops = upper_dev->netdev_ops;
1498fbaec0eaSJiri Pirko 		if (ops->ndo_add_slave) {
1499898e5061SJiri Pirko 			err = ops->ndo_add_slave(upper_dev, dev);
1500fbaec0eaSJiri Pirko 			if (err)
1501fbaec0eaSJiri Pirko 				return err;
1502fbaec0eaSJiri Pirko 		} else {
1503fbaec0eaSJiri Pirko 			return -EOPNOTSUPP;
1504fbaec0eaSJiri Pirko 		}
1505fbaec0eaSJiri Pirko 	}
1506fbaec0eaSJiri Pirko 	return 0;
1507fbaec0eaSJiri Pirko }
1508fbaec0eaSJiri Pirko 
150990c325e3SNicolas Dichtel #define DO_SETLINK_MODIFIED	0x01
1510ba998906SNicolas Dichtel /* notify flag means notify + modified. */
1511ba998906SNicolas Dichtel #define DO_SETLINK_NOTIFY	0x03
151290f62cf3SEric W. Biederman static int do_setlink(const struct sk_buff *skb,
151390f62cf3SEric W. Biederman 		      struct net_device *dev, struct ifinfomsg *ifm,
151490c325e3SNicolas Dichtel 		      struct nlattr **tb, char *ifname, int status)
15150157f60cSPatrick McHardy {
1516d314774cSStephen Hemminger 	const struct net_device_ops *ops = dev->netdev_ops;
15170157f60cSPatrick McHardy 	int err;
15180157f60cSPatrick McHardy 
1519f0630529SEric W. Biederman 	if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
152081adee47SEric W. Biederman 		struct net *net = rtnl_link_get_net(dev_net(dev), tb);
1521d8a5ec67SEric W. Biederman 		if (IS_ERR(net)) {
1522d8a5ec67SEric W. Biederman 			err = PTR_ERR(net);
1523d8a5ec67SEric W. Biederman 			goto errout;
1524d8a5ec67SEric W. Biederman 		}
152590f62cf3SEric W. Biederman 		if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
1526e0ebde0eSNicolas Dichtel 			put_net(net);
1527b51642f6SEric W. Biederman 			err = -EPERM;
1528b51642f6SEric W. Biederman 			goto errout;
1529b51642f6SEric W. Biederman 		}
1530d8a5ec67SEric W. Biederman 		err = dev_change_net_namespace(dev, net, ifname);
1531d8a5ec67SEric W. Biederman 		put_net(net);
1532d8a5ec67SEric W. Biederman 		if (err)
1533d8a5ec67SEric W. Biederman 			goto errout;
153490c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
1535d8a5ec67SEric W. Biederman 	}
1536d8a5ec67SEric W. Biederman 
15370157f60cSPatrick McHardy 	if (tb[IFLA_MAP]) {
15380157f60cSPatrick McHardy 		struct rtnl_link_ifmap *u_map;
15390157f60cSPatrick McHardy 		struct ifmap k_map;
15400157f60cSPatrick McHardy 
1541d314774cSStephen Hemminger 		if (!ops->ndo_set_config) {
15420157f60cSPatrick McHardy 			err = -EOPNOTSUPP;
15430157f60cSPatrick McHardy 			goto errout;
15440157f60cSPatrick McHardy 		}
15450157f60cSPatrick McHardy 
15460157f60cSPatrick McHardy 		if (!netif_device_present(dev)) {
15470157f60cSPatrick McHardy 			err = -ENODEV;
15480157f60cSPatrick McHardy 			goto errout;
15490157f60cSPatrick McHardy 		}
15500157f60cSPatrick McHardy 
15510157f60cSPatrick McHardy 		u_map = nla_data(tb[IFLA_MAP]);
15520157f60cSPatrick McHardy 		k_map.mem_start = (unsigned long) u_map->mem_start;
15530157f60cSPatrick McHardy 		k_map.mem_end = (unsigned long) u_map->mem_end;
15540157f60cSPatrick McHardy 		k_map.base_addr = (unsigned short) u_map->base_addr;
15550157f60cSPatrick McHardy 		k_map.irq = (unsigned char) u_map->irq;
15560157f60cSPatrick McHardy 		k_map.dma = (unsigned char) u_map->dma;
15570157f60cSPatrick McHardy 		k_map.port = (unsigned char) u_map->port;
15580157f60cSPatrick McHardy 
1559d314774cSStephen Hemminger 		err = ops->ndo_set_config(dev, &k_map);
15600157f60cSPatrick McHardy 		if (err < 0)
15610157f60cSPatrick McHardy 			goto errout;
15620157f60cSPatrick McHardy 
1563ba998906SNicolas Dichtel 		status |= DO_SETLINK_NOTIFY;
15640157f60cSPatrick McHardy 	}
15650157f60cSPatrick McHardy 
15660157f60cSPatrick McHardy 	if (tb[IFLA_ADDRESS]) {
15670157f60cSPatrick McHardy 		struct sockaddr *sa;
15680157f60cSPatrick McHardy 		int len;
15690157f60cSPatrick McHardy 
15700157f60cSPatrick McHardy 		len = sizeof(sa_family_t) + dev->addr_len;
15710157f60cSPatrick McHardy 		sa = kmalloc(len, GFP_KERNEL);
15720157f60cSPatrick McHardy 		if (!sa) {
15730157f60cSPatrick McHardy 			err = -ENOMEM;
15740157f60cSPatrick McHardy 			goto errout;
15750157f60cSPatrick McHardy 		}
15760157f60cSPatrick McHardy 		sa->sa_family = dev->type;
15770157f60cSPatrick McHardy 		memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
15780157f60cSPatrick McHardy 		       dev->addr_len);
1579e7c3273eSJiri Pirko 		err = dev_set_mac_address(dev, sa);
15800157f60cSPatrick McHardy 		kfree(sa);
15810157f60cSPatrick McHardy 		if (err)
15820157f60cSPatrick McHardy 			goto errout;
158390c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
15840157f60cSPatrick McHardy 	}
15850157f60cSPatrick McHardy 
15860157f60cSPatrick McHardy 	if (tb[IFLA_MTU]) {
15870157f60cSPatrick McHardy 		err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
15880157f60cSPatrick McHardy 		if (err < 0)
15890157f60cSPatrick McHardy 			goto errout;
159090c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
15910157f60cSPatrick McHardy 	}
15920157f60cSPatrick McHardy 
1593cbda10faSVlad Dogaru 	if (tb[IFLA_GROUP]) {
1594cbda10faSVlad Dogaru 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
1595ba998906SNicolas Dichtel 		status |= DO_SETLINK_NOTIFY;
1596cbda10faSVlad Dogaru 	}
1597cbda10faSVlad Dogaru 
15980157f60cSPatrick McHardy 	/*
15990157f60cSPatrick McHardy 	 * Interface selected by interface index but interface
16000157f60cSPatrick McHardy 	 * name provided implies that a name change has been
16010157f60cSPatrick McHardy 	 * requested.
16020157f60cSPatrick McHardy 	 */
16030157f60cSPatrick McHardy 	if (ifm->ifi_index > 0 && ifname[0]) {
16040157f60cSPatrick McHardy 		err = dev_change_name(dev, ifname);
16050157f60cSPatrick McHardy 		if (err < 0)
16060157f60cSPatrick McHardy 			goto errout;
160790c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
16080157f60cSPatrick McHardy 	}
16090157f60cSPatrick McHardy 
16100b815a1aSStephen Hemminger 	if (tb[IFLA_IFALIAS]) {
16110b815a1aSStephen Hemminger 		err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
16120b815a1aSStephen Hemminger 				    nla_len(tb[IFLA_IFALIAS]));
16130b815a1aSStephen Hemminger 		if (err < 0)
16140b815a1aSStephen Hemminger 			goto errout;
1615ba998906SNicolas Dichtel 		status |= DO_SETLINK_NOTIFY;
16160b815a1aSStephen Hemminger 	}
16170b815a1aSStephen Hemminger 
16180157f60cSPatrick McHardy 	if (tb[IFLA_BROADCAST]) {
16190157f60cSPatrick McHardy 		nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
1620e7c3273eSJiri Pirko 		call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
16210157f60cSPatrick McHardy 	}
16220157f60cSPatrick McHardy 
16230157f60cSPatrick McHardy 	if (ifm->ifi_flags || ifm->ifi_change) {
16243729d502SPatrick McHardy 		err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
16255f9021cfSJohannes Berg 		if (err < 0)
16265f9021cfSJohannes Berg 			goto errout;
16270157f60cSPatrick McHardy 	}
16280157f60cSPatrick McHardy 
1629fbaec0eaSJiri Pirko 	if (tb[IFLA_MASTER]) {
1630fbaec0eaSJiri Pirko 		err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
1631fbaec0eaSJiri Pirko 		if (err)
1632fbaec0eaSJiri Pirko 			goto errout;
163390c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
1634fbaec0eaSJiri Pirko 	}
1635fbaec0eaSJiri Pirko 
16369a57247fSJiri Pirko 	if (tb[IFLA_CARRIER]) {
16379a57247fSJiri Pirko 		err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
16389a57247fSJiri Pirko 		if (err)
16399a57247fSJiri Pirko 			goto errout;
164090c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
16419a57247fSJiri Pirko 	}
16429a57247fSJiri Pirko 
16435d1180fcSNicolas Dichtel 	if (tb[IFLA_TXQLEN]) {
16445d1180fcSNicolas Dichtel 		unsigned long value = nla_get_u32(tb[IFLA_TXQLEN]);
16455d1180fcSNicolas Dichtel 
16465d1180fcSNicolas Dichtel 		if (dev->tx_queue_len ^ value)
1647ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
16485d1180fcSNicolas Dichtel 
16495d1180fcSNicolas Dichtel 		dev->tx_queue_len = value;
16505d1180fcSNicolas Dichtel 	}
16510157f60cSPatrick McHardy 
16520157f60cSPatrick McHardy 	if (tb[IFLA_OPERSTATE])
165393b2d4a2SDavid S. Miller 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
16540157f60cSPatrick McHardy 
16550157f60cSPatrick McHardy 	if (tb[IFLA_LINKMODE]) {
16561889b0e7SNicolas Dichtel 		unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
16571889b0e7SNicolas Dichtel 
16580157f60cSPatrick McHardy 		write_lock_bh(&dev_base_lock);
16591889b0e7SNicolas Dichtel 		if (dev->link_mode ^ value)
1660ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
16611889b0e7SNicolas Dichtel 		dev->link_mode = value;
166293b2d4a2SDavid S. Miller 		write_unlock_bh(&dev_base_lock);
16630157f60cSPatrick McHardy 	}
16640157f60cSPatrick McHardy 
1665c02db8c6SChris Wright 	if (tb[IFLA_VFINFO_LIST]) {
1666c02db8c6SChris Wright 		struct nlattr *attr;
1667c02db8c6SChris Wright 		int rem;
1668c02db8c6SChris Wright 		nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
1669253683bbSDavid Howells 			if (nla_type(attr) != IFLA_VF_INFO) {
1670253683bbSDavid Howells 				err = -EINVAL;
1671c02db8c6SChris Wright 				goto errout;
1672253683bbSDavid Howells 			}
1673c02db8c6SChris Wright 			err = do_setvfinfo(dev, attr);
1674ebc08a6fSWilliams, Mitch A 			if (err < 0)
1675ebc08a6fSWilliams, Mitch A 				goto errout;
1676ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
1677ebc08a6fSWilliams, Mitch A 		}
1678ebc08a6fSWilliams, Mitch A 	}
16790157f60cSPatrick McHardy 	err = 0;
16800157f60cSPatrick McHardy 
168157b61080SScott Feldman 	if (tb[IFLA_VF_PORTS]) {
168257b61080SScott Feldman 		struct nlattr *port[IFLA_PORT_MAX+1];
168357b61080SScott Feldman 		struct nlattr *attr;
168457b61080SScott Feldman 		int vf;
168557b61080SScott Feldman 		int rem;
168657b61080SScott Feldman 
168757b61080SScott Feldman 		err = -EOPNOTSUPP;
168857b61080SScott Feldman 		if (!ops->ndo_set_vf_port)
168957b61080SScott Feldman 			goto errout;
169057b61080SScott Feldman 
169157b61080SScott Feldman 		nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
169257b61080SScott Feldman 			if (nla_type(attr) != IFLA_VF_PORT)
169357b61080SScott Feldman 				continue;
169457b61080SScott Feldman 			err = nla_parse_nested(port, IFLA_PORT_MAX,
169557b61080SScott Feldman 				attr, ifla_port_policy);
169657b61080SScott Feldman 			if (err < 0)
169757b61080SScott Feldman 				goto errout;
169857b61080SScott Feldman 			if (!port[IFLA_PORT_VF]) {
169957b61080SScott Feldman 				err = -EOPNOTSUPP;
170057b61080SScott Feldman 				goto errout;
170157b61080SScott Feldman 			}
170257b61080SScott Feldman 			vf = nla_get_u32(port[IFLA_PORT_VF]);
170357b61080SScott Feldman 			err = ops->ndo_set_vf_port(dev, vf, port);
170457b61080SScott Feldman 			if (err < 0)
170557b61080SScott Feldman 				goto errout;
1706ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
170757b61080SScott Feldman 		}
170857b61080SScott Feldman 	}
170957b61080SScott Feldman 	err = 0;
171057b61080SScott Feldman 
171157b61080SScott Feldman 	if (tb[IFLA_PORT_SELF]) {
171257b61080SScott Feldman 		struct nlattr *port[IFLA_PORT_MAX+1];
171357b61080SScott Feldman 
171457b61080SScott Feldman 		err = nla_parse_nested(port, IFLA_PORT_MAX,
171557b61080SScott Feldman 			tb[IFLA_PORT_SELF], ifla_port_policy);
171657b61080SScott Feldman 		if (err < 0)
171757b61080SScott Feldman 			goto errout;
171857b61080SScott Feldman 
171957b61080SScott Feldman 		err = -EOPNOTSUPP;
172057b61080SScott Feldman 		if (ops->ndo_set_vf_port)
172157b61080SScott Feldman 			err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
172257b61080SScott Feldman 		if (err < 0)
172357b61080SScott Feldman 			goto errout;
1724ba998906SNicolas Dichtel 		status |= DO_SETLINK_NOTIFY;
172557b61080SScott Feldman 	}
1726f8ff182cSThomas Graf 
1727f8ff182cSThomas Graf 	if (tb[IFLA_AF_SPEC]) {
1728f8ff182cSThomas Graf 		struct nlattr *af;
1729f8ff182cSThomas Graf 		int rem;
1730f8ff182cSThomas Graf 
1731f8ff182cSThomas Graf 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1732f8ff182cSThomas Graf 			const struct rtnl_af_ops *af_ops;
1733f8ff182cSThomas Graf 
1734f8ff182cSThomas Graf 			if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1735cf7afbfeSThomas Graf 				BUG();
1736f8ff182cSThomas Graf 
1737cf7afbfeSThomas Graf 			err = af_ops->set_link_af(dev, af);
1738f8ff182cSThomas Graf 			if (err < 0)
1739f8ff182cSThomas Graf 				goto errout;
1740f8ff182cSThomas Graf 
1741ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
1742f8ff182cSThomas Graf 		}
1743f8ff182cSThomas Graf 	}
174457b61080SScott Feldman 	err = 0;
174557b61080SScott Feldman 
17460157f60cSPatrick McHardy errout:
1747ba998906SNicolas Dichtel 	if (status & DO_SETLINK_MODIFIED) {
1748ba998906SNicolas Dichtel 		if (status & DO_SETLINK_NOTIFY)
1749ba998906SNicolas Dichtel 			netdev_state_change(dev);
1750ba998906SNicolas Dichtel 
1751ba998906SNicolas Dichtel 		if (err < 0)
1752e87cc472SJoe 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",
1753e87cc472SJoe Perches 					     dev->name);
1754ba998906SNicolas Dichtel 	}
17550157f60cSPatrick McHardy 
17560157f60cSPatrick McHardy 	return err;
17570157f60cSPatrick McHardy }
17580157f60cSPatrick McHardy 
1759661d2967SThomas Graf static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
1760da5e0494SThomas Graf {
17613b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
1762da5e0494SThomas Graf 	struct ifinfomsg *ifm;
1763da5e0494SThomas Graf 	struct net_device *dev;
17640157f60cSPatrick McHardy 	int err;
1765da5e0494SThomas Graf 	struct nlattr *tb[IFLA_MAX+1];
17661da177e4SLinus Torvalds 	char ifname[IFNAMSIZ];
17671da177e4SLinus Torvalds 
1768da5e0494SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1769da5e0494SThomas Graf 	if (err < 0)
1770da5e0494SThomas Graf 		goto errout;
17711da177e4SLinus Torvalds 
17725176f91eSThomas Graf 	if (tb[IFLA_IFNAME])
17735176f91eSThomas Graf 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
177478e5b891SPatrick McHardy 	else
177578e5b891SPatrick McHardy 		ifname[0] = '\0';
17761da177e4SLinus Torvalds 
17771da177e4SLinus Torvalds 	err = -EINVAL;
1778da5e0494SThomas Graf 	ifm = nlmsg_data(nlh);
177951055be8SPatrick McHardy 	if (ifm->ifi_index > 0)
1780a3d12891SEric Dumazet 		dev = __dev_get_by_index(net, ifm->ifi_index);
1781da5e0494SThomas Graf 	else if (tb[IFLA_IFNAME])
1782a3d12891SEric Dumazet 		dev = __dev_get_by_name(net, ifname);
1783da5e0494SThomas Graf 	else
1784da5e0494SThomas Graf 		goto errout;
17851da177e4SLinus Torvalds 
1786da5e0494SThomas Graf 	if (dev == NULL) {
1787da5e0494SThomas Graf 		err = -ENODEV;
1788da5e0494SThomas Graf 		goto errout;
1789da5e0494SThomas Graf 	}
17901da177e4SLinus Torvalds 
1791e0d087afSEric Dumazet 	err = validate_linkmsg(dev, tb);
1792e0d087afSEric Dumazet 	if (err < 0)
1793a3d12891SEric Dumazet 		goto errout;
1794da5e0494SThomas Graf 
179590f62cf3SEric W. Biederman 	err = do_setlink(skb, dev, ifm, tb, ifname, 0);
1796da5e0494SThomas Graf errout:
17971da177e4SLinus Torvalds 	return err;
17981da177e4SLinus Torvalds }
17991da177e4SLinus Torvalds 
1800661d2967SThomas Graf static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
180138f7b870SPatrick McHardy {
18023b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
180338f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops;
180438f7b870SPatrick McHardy 	struct net_device *dev;
180538f7b870SPatrick McHardy 	struct ifinfomsg *ifm;
180638f7b870SPatrick McHardy 	char ifname[IFNAMSIZ];
180738f7b870SPatrick McHardy 	struct nlattr *tb[IFLA_MAX+1];
180838f7b870SPatrick McHardy 	int err;
1809226bd341SEric Dumazet 	LIST_HEAD(list_kill);
181038f7b870SPatrick McHardy 
181138f7b870SPatrick McHardy 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
181238f7b870SPatrick McHardy 	if (err < 0)
181338f7b870SPatrick McHardy 		return err;
181438f7b870SPatrick McHardy 
181538f7b870SPatrick McHardy 	if (tb[IFLA_IFNAME])
181638f7b870SPatrick McHardy 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
181738f7b870SPatrick McHardy 
181838f7b870SPatrick McHardy 	ifm = nlmsg_data(nlh);
181938f7b870SPatrick McHardy 	if (ifm->ifi_index > 0)
1820881d966bSEric W. Biederman 		dev = __dev_get_by_index(net, ifm->ifi_index);
182138f7b870SPatrick McHardy 	else if (tb[IFLA_IFNAME])
1822881d966bSEric W. Biederman 		dev = __dev_get_by_name(net, ifname);
182338f7b870SPatrick McHardy 	else
182438f7b870SPatrick McHardy 		return -EINVAL;
182538f7b870SPatrick McHardy 
182638f7b870SPatrick McHardy 	if (!dev)
182738f7b870SPatrick McHardy 		return -ENODEV;
182838f7b870SPatrick McHardy 
182938f7b870SPatrick McHardy 	ops = dev->rtnl_link_ops;
1830b0ab2fabSJiri Pirko 	if (!ops || !ops->dellink)
183138f7b870SPatrick McHardy 		return -EOPNOTSUPP;
183238f7b870SPatrick McHardy 
1833226bd341SEric Dumazet 	ops->dellink(dev, &list_kill);
1834226bd341SEric Dumazet 	unregister_netdevice_many(&list_kill);
183538f7b870SPatrick McHardy 	return 0;
183638f7b870SPatrick McHardy }
183738f7b870SPatrick McHardy 
18383729d502SPatrick McHardy int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
18393729d502SPatrick McHardy {
18403729d502SPatrick McHardy 	unsigned int old_flags;
18413729d502SPatrick McHardy 	int err;
18423729d502SPatrick McHardy 
18433729d502SPatrick McHardy 	old_flags = dev->flags;
18443729d502SPatrick McHardy 	if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
18453729d502SPatrick McHardy 		err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
18463729d502SPatrick McHardy 		if (err < 0)
18473729d502SPatrick McHardy 			return err;
18483729d502SPatrick McHardy 	}
18493729d502SPatrick McHardy 
18503729d502SPatrick McHardy 	dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
18513729d502SPatrick McHardy 
1852a528c219SNicolas Dichtel 	__dev_notify_flags(dev, old_flags, ~0U);
18533729d502SPatrick McHardy 	return 0;
18543729d502SPatrick McHardy }
18553729d502SPatrick McHardy EXPORT_SYMBOL(rtnl_configure_link);
18563729d502SPatrick McHardy 
1857c0713563SRami Rosen struct net_device *rtnl_create_link(struct net *net,
18585517750fSTom Gundersen 	char *ifname, unsigned char name_assign_type,
18595517750fSTom Gundersen 	const struct rtnl_link_ops *ops, struct nlattr *tb[])
1860e7199288SPavel Emelianov {
1861e7199288SPavel Emelianov 	int err;
1862e7199288SPavel Emelianov 	struct net_device *dev;
1863d40156aaSJiri Pirko 	unsigned int num_tx_queues = 1;
1864d40156aaSJiri Pirko 	unsigned int num_rx_queues = 1;
1865e7199288SPavel Emelianov 
186676ff5cc9SJiri Pirko 	if (tb[IFLA_NUM_TX_QUEUES])
186776ff5cc9SJiri Pirko 		num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
186876ff5cc9SJiri Pirko 	else if (ops->get_num_tx_queues)
1869d40156aaSJiri Pirko 		num_tx_queues = ops->get_num_tx_queues();
187076ff5cc9SJiri Pirko 
187176ff5cc9SJiri Pirko 	if (tb[IFLA_NUM_RX_QUEUES])
187276ff5cc9SJiri Pirko 		num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
187376ff5cc9SJiri Pirko 	else if (ops->get_num_rx_queues)
1874d40156aaSJiri Pirko 		num_rx_queues = ops->get_num_rx_queues();
1875efacb309Sstephen hemminger 
1876e7199288SPavel Emelianov 	err = -ENOMEM;
18775517750fSTom Gundersen 	dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
1878c835a677STom Gundersen 			       ops->setup, num_tx_queues, num_rx_queues);
1879e7199288SPavel Emelianov 	if (!dev)
1880e7199288SPavel Emelianov 		goto err;
1881e7199288SPavel Emelianov 
188281adee47SEric W. Biederman 	dev_net_set(dev, net);
188381adee47SEric W. Biederman 	dev->rtnl_link_ops = ops;
18843729d502SPatrick McHardy 	dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
188581adee47SEric W. Biederman 
1886e7199288SPavel Emelianov 	if (tb[IFLA_MTU])
1887e7199288SPavel Emelianov 		dev->mtu = nla_get_u32(tb[IFLA_MTU]);
18882afb9b53SJiri Pirko 	if (tb[IFLA_ADDRESS]) {
1889e7199288SPavel Emelianov 		memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
1890e7199288SPavel Emelianov 				nla_len(tb[IFLA_ADDRESS]));
18912afb9b53SJiri Pirko 		dev->addr_assign_type = NET_ADDR_SET;
18922afb9b53SJiri Pirko 	}
1893e7199288SPavel Emelianov 	if (tb[IFLA_BROADCAST])
1894e7199288SPavel Emelianov 		memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
1895e7199288SPavel Emelianov 				nla_len(tb[IFLA_BROADCAST]));
1896e7199288SPavel Emelianov 	if (tb[IFLA_TXQLEN])
1897e7199288SPavel Emelianov 		dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
1898e7199288SPavel Emelianov 	if (tb[IFLA_OPERSTATE])
189993b2d4a2SDavid S. Miller 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
1900e7199288SPavel Emelianov 	if (tb[IFLA_LINKMODE])
1901e7199288SPavel Emelianov 		dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
1902ffa934f1SPatrick McHardy 	if (tb[IFLA_GROUP])
1903ffa934f1SPatrick McHardy 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
1904e7199288SPavel Emelianov 
1905e7199288SPavel Emelianov 	return dev;
1906e7199288SPavel Emelianov 
1907e7199288SPavel Emelianov err:
1908e7199288SPavel Emelianov 	return ERR_PTR(err);
1909e7199288SPavel Emelianov }
1910e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_create_link);
1911e7199288SPavel Emelianov 
191290f62cf3SEric W. Biederman static int rtnl_group_changelink(const struct sk_buff *skb,
191390f62cf3SEric W. Biederman 		struct net *net, int group,
1914e7ed828fSVlad Dogaru 		struct ifinfomsg *ifm,
1915e7ed828fSVlad Dogaru 		struct nlattr **tb)
1916e7ed828fSVlad Dogaru {
1917e7ed828fSVlad Dogaru 	struct net_device *dev;
1918e7ed828fSVlad Dogaru 	int err;
1919e7ed828fSVlad Dogaru 
1920e7ed828fSVlad Dogaru 	for_each_netdev(net, dev) {
1921e7ed828fSVlad Dogaru 		if (dev->group == group) {
192290f62cf3SEric W. Biederman 			err = do_setlink(skb, dev, ifm, tb, NULL, 0);
1923e7ed828fSVlad Dogaru 			if (err < 0)
1924e7ed828fSVlad Dogaru 				return err;
1925e7ed828fSVlad Dogaru 		}
1926e7ed828fSVlad Dogaru 	}
1927e7ed828fSVlad Dogaru 
1928e7ed828fSVlad Dogaru 	return 0;
1929e7ed828fSVlad Dogaru }
1930e7ed828fSVlad Dogaru 
1931661d2967SThomas Graf static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh)
193238f7b870SPatrick McHardy {
19333b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
193438f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops;
1935ba7d49b1SJiri Pirko 	const struct rtnl_link_ops *m_ops = NULL;
193638f7b870SPatrick McHardy 	struct net_device *dev;
1937ba7d49b1SJiri Pirko 	struct net_device *master_dev = NULL;
193838f7b870SPatrick McHardy 	struct ifinfomsg *ifm;
193938f7b870SPatrick McHardy 	char kind[MODULE_NAME_LEN];
194038f7b870SPatrick McHardy 	char ifname[IFNAMSIZ];
194138f7b870SPatrick McHardy 	struct nlattr *tb[IFLA_MAX+1];
194238f7b870SPatrick McHardy 	struct nlattr *linkinfo[IFLA_INFO_MAX+1];
19435517750fSTom Gundersen 	unsigned char name_assign_type = NET_NAME_USER;
194438f7b870SPatrick McHardy 	int err;
194538f7b870SPatrick McHardy 
194695a5afcaSJohannes Berg #ifdef CONFIG_MODULES
194738f7b870SPatrick McHardy replay:
19488072f085SThomas Graf #endif
194938f7b870SPatrick McHardy 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
195038f7b870SPatrick McHardy 	if (err < 0)
195138f7b870SPatrick McHardy 		return err;
195238f7b870SPatrick McHardy 
195338f7b870SPatrick McHardy 	if (tb[IFLA_IFNAME])
195438f7b870SPatrick McHardy 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
195538f7b870SPatrick McHardy 	else
195638f7b870SPatrick McHardy 		ifname[0] = '\0';
195738f7b870SPatrick McHardy 
195838f7b870SPatrick McHardy 	ifm = nlmsg_data(nlh);
195938f7b870SPatrick McHardy 	if (ifm->ifi_index > 0)
1960881d966bSEric W. Biederman 		dev = __dev_get_by_index(net, ifm->ifi_index);
1961e7ed828fSVlad Dogaru 	else {
1962e7ed828fSVlad Dogaru 		if (ifname[0])
1963881d966bSEric W. Biederman 			dev = __dev_get_by_name(net, ifname);
196438f7b870SPatrick McHardy 		else
196538f7b870SPatrick McHardy 			dev = NULL;
1966e7ed828fSVlad Dogaru 	}
196738f7b870SPatrick McHardy 
1968ba7d49b1SJiri Pirko 	if (dev) {
1969ba7d49b1SJiri Pirko 		master_dev = netdev_master_upper_dev_get(dev);
1970ba7d49b1SJiri Pirko 		if (master_dev)
1971ba7d49b1SJiri Pirko 			m_ops = master_dev->rtnl_link_ops;
1972ba7d49b1SJiri Pirko 	}
1973ba7d49b1SJiri Pirko 
1974e0d087afSEric Dumazet 	err = validate_linkmsg(dev, tb);
1975e0d087afSEric Dumazet 	if (err < 0)
19761840bb13SThomas Graf 		return err;
19771840bb13SThomas Graf 
197838f7b870SPatrick McHardy 	if (tb[IFLA_LINKINFO]) {
197938f7b870SPatrick McHardy 		err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
198038f7b870SPatrick McHardy 				       tb[IFLA_LINKINFO], ifla_info_policy);
198138f7b870SPatrick McHardy 		if (err < 0)
198238f7b870SPatrick McHardy 			return err;
198338f7b870SPatrick McHardy 	} else
198438f7b870SPatrick McHardy 		memset(linkinfo, 0, sizeof(linkinfo));
198538f7b870SPatrick McHardy 
198638f7b870SPatrick McHardy 	if (linkinfo[IFLA_INFO_KIND]) {
198738f7b870SPatrick McHardy 		nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
198838f7b870SPatrick McHardy 		ops = rtnl_link_ops_get(kind);
198938f7b870SPatrick McHardy 	} else {
199038f7b870SPatrick McHardy 		kind[0] = '\0';
199138f7b870SPatrick McHardy 		ops = NULL;
199238f7b870SPatrick McHardy 	}
199338f7b870SPatrick McHardy 
199438f7b870SPatrick McHardy 	if (1) {
1995ba7d49b1SJiri Pirko 		struct nlattr *attr[ops ? ops->maxtype + 1 : 0];
1996ba7d49b1SJiri Pirko 		struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 0];
1997ba7d49b1SJiri Pirko 		struct nlattr **data = NULL;
1998ba7d49b1SJiri Pirko 		struct nlattr **slave_data = NULL;
199981adee47SEric W. Biederman 		struct net *dest_net;
200038f7b870SPatrick McHardy 
200138f7b870SPatrick McHardy 		if (ops) {
200238f7b870SPatrick McHardy 			if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
200338f7b870SPatrick McHardy 				err = nla_parse_nested(attr, ops->maxtype,
200438f7b870SPatrick McHardy 						       linkinfo[IFLA_INFO_DATA],
200538f7b870SPatrick McHardy 						       ops->policy);
200638f7b870SPatrick McHardy 				if (err < 0)
200738f7b870SPatrick McHardy 					return err;
200838f7b870SPatrick McHardy 				data = attr;
200938f7b870SPatrick McHardy 			}
201038f7b870SPatrick McHardy 			if (ops->validate) {
201138f7b870SPatrick McHardy 				err = ops->validate(tb, data);
201238f7b870SPatrick McHardy 				if (err < 0)
201338f7b870SPatrick McHardy 					return err;
201438f7b870SPatrick McHardy 			}
201538f7b870SPatrick McHardy 		}
201638f7b870SPatrick McHardy 
2017ba7d49b1SJiri Pirko 		if (m_ops) {
2018ba7d49b1SJiri Pirko 			if (m_ops->slave_maxtype &&
2019ba7d49b1SJiri Pirko 			    linkinfo[IFLA_INFO_SLAVE_DATA]) {
2020ba7d49b1SJiri Pirko 				err = nla_parse_nested(slave_attr,
2021ba7d49b1SJiri Pirko 						       m_ops->slave_maxtype,
2022ba7d49b1SJiri Pirko 						       linkinfo[IFLA_INFO_SLAVE_DATA],
2023ba7d49b1SJiri Pirko 						       m_ops->slave_policy);
2024ba7d49b1SJiri Pirko 				if (err < 0)
2025ba7d49b1SJiri Pirko 					return err;
2026ba7d49b1SJiri Pirko 				slave_data = slave_attr;
2027ba7d49b1SJiri Pirko 			}
2028ba7d49b1SJiri Pirko 			if (m_ops->slave_validate) {
2029ba7d49b1SJiri Pirko 				err = m_ops->slave_validate(tb, slave_data);
2030ba7d49b1SJiri Pirko 				if (err < 0)
2031ba7d49b1SJiri Pirko 					return err;
2032ba7d49b1SJiri Pirko 			}
2033ba7d49b1SJiri Pirko 		}
2034ba7d49b1SJiri Pirko 
203538f7b870SPatrick McHardy 		if (dev) {
203690c325e3SNicolas Dichtel 			int status = 0;
203738f7b870SPatrick McHardy 
203838f7b870SPatrick McHardy 			if (nlh->nlmsg_flags & NLM_F_EXCL)
203938f7b870SPatrick McHardy 				return -EEXIST;
204038f7b870SPatrick McHardy 			if (nlh->nlmsg_flags & NLM_F_REPLACE)
204138f7b870SPatrick McHardy 				return -EOPNOTSUPP;
204238f7b870SPatrick McHardy 
204338f7b870SPatrick McHardy 			if (linkinfo[IFLA_INFO_DATA]) {
204438f7b870SPatrick McHardy 				if (!ops || ops != dev->rtnl_link_ops ||
204538f7b870SPatrick McHardy 				    !ops->changelink)
204638f7b870SPatrick McHardy 					return -EOPNOTSUPP;
204738f7b870SPatrick McHardy 
204838f7b870SPatrick McHardy 				err = ops->changelink(dev, tb, data);
204938f7b870SPatrick McHardy 				if (err < 0)
205038f7b870SPatrick McHardy 					return err;
2051ba998906SNicolas Dichtel 				status |= DO_SETLINK_NOTIFY;
205238f7b870SPatrick McHardy 			}
205338f7b870SPatrick McHardy 
2054ba7d49b1SJiri Pirko 			if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
2055ba7d49b1SJiri Pirko 				if (!m_ops || !m_ops->slave_changelink)
2056ba7d49b1SJiri Pirko 					return -EOPNOTSUPP;
2057ba7d49b1SJiri Pirko 
2058ba7d49b1SJiri Pirko 				err = m_ops->slave_changelink(master_dev, dev,
2059ba7d49b1SJiri Pirko 							      tb, slave_data);
2060ba7d49b1SJiri Pirko 				if (err < 0)
2061ba7d49b1SJiri Pirko 					return err;
2062ba998906SNicolas Dichtel 				status |= DO_SETLINK_NOTIFY;
2063ba7d49b1SJiri Pirko 			}
2064ba7d49b1SJiri Pirko 
206590c325e3SNicolas Dichtel 			return do_setlink(skb, dev, ifm, tb, ifname, status);
206638f7b870SPatrick McHardy 		}
206738f7b870SPatrick McHardy 
2068ffa934f1SPatrick McHardy 		if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
2069ffa934f1SPatrick McHardy 			if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
207090f62cf3SEric W. Biederman 				return rtnl_group_changelink(skb, net,
2071ffa934f1SPatrick McHardy 						nla_get_u32(tb[IFLA_GROUP]),
2072ffa934f1SPatrick McHardy 						ifm, tb);
207338f7b870SPatrick McHardy 			return -ENODEV;
2074ffa934f1SPatrick McHardy 		}
207538f7b870SPatrick McHardy 
20760e06877cSPatrick McHardy 		if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
207738f7b870SPatrick McHardy 			return -EOPNOTSUPP;
207838f7b870SPatrick McHardy 
207938f7b870SPatrick McHardy 		if (!ops) {
208095a5afcaSJohannes Berg #ifdef CONFIG_MODULES
208138f7b870SPatrick McHardy 			if (kind[0]) {
208238f7b870SPatrick McHardy 				__rtnl_unlock();
208338f7b870SPatrick McHardy 				request_module("rtnl-link-%s", kind);
208438f7b870SPatrick McHardy 				rtnl_lock();
208538f7b870SPatrick McHardy 				ops = rtnl_link_ops_get(kind);
208638f7b870SPatrick McHardy 				if (ops)
208738f7b870SPatrick McHardy 					goto replay;
208838f7b870SPatrick McHardy 			}
208938f7b870SPatrick McHardy #endif
209038f7b870SPatrick McHardy 			return -EOPNOTSUPP;
209138f7b870SPatrick McHardy 		}
209238f7b870SPatrick McHardy 
2093b0ab2fabSJiri Pirko 		if (!ops->setup)
2094b0ab2fabSJiri Pirko 			return -EOPNOTSUPP;
2095b0ab2fabSJiri Pirko 
20965517750fSTom Gundersen 		if (!ifname[0]) {
209738f7b870SPatrick McHardy 			snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
20985517750fSTom Gundersen 			name_assign_type = NET_NAME_ENUM;
20995517750fSTom Gundersen 		}
210038f7b870SPatrick McHardy 
210181adee47SEric W. Biederman 		dest_net = rtnl_link_get_net(net, tb);
210213ad1774SEric W. Biederman 		if (IS_ERR(dest_net))
210313ad1774SEric W. Biederman 			return PTR_ERR(dest_net);
210413ad1774SEric W. Biederman 
21055517750fSTom Gundersen 		dev = rtnl_create_link(dest_net, ifname, name_assign_type, ops, tb);
21069c7dafbfSPavel Emelyanov 		if (IS_ERR(dev)) {
2107e7199288SPavel Emelianov 			err = PTR_ERR(dev);
21089c7dafbfSPavel Emelyanov 			goto out;
21099c7dafbfSPavel Emelyanov 		}
21109c7dafbfSPavel Emelyanov 
21119c7dafbfSPavel Emelyanov 		dev->ifindex = ifm->ifi_index;
21129c7dafbfSPavel Emelyanov 
21130e0eee24SCong Wang 		if (ops->newlink) {
211481adee47SEric W. Biederman 			err = ops->newlink(net, dev, tb, data);
21150e0eee24SCong Wang 			/* Drivers should call free_netdev() in ->destructor
2116e51fb152SCong Wang 			 * and unregister it on failure after registration
2117e51fb152SCong Wang 			 * so that device could be finally freed in rtnl_unlock.
21180e0eee24SCong Wang 			 */
2119e51fb152SCong Wang 			if (err < 0) {
2120e51fb152SCong Wang 				/* If device is not registered at all, free it now */
2121e51fb152SCong Wang 				if (dev->reg_state == NETREG_UNINITIALIZED)
2122e51fb152SCong Wang 					free_netdev(dev);
21230e0eee24SCong Wang 				goto out;
2124e51fb152SCong Wang 			}
21250e0eee24SCong Wang 		} else {
21262d85cba2SPatrick McHardy 			err = register_netdevice(dev);
2127fce9b9beSDan Carpenter 			if (err < 0) {
212838f7b870SPatrick McHardy 				free_netdev(dev);
21293729d502SPatrick McHardy 				goto out;
2130fce9b9beSDan Carpenter 			}
21310e0eee24SCong Wang 		}
21323729d502SPatrick McHardy 		err = rtnl_configure_link(dev, ifm);
21333729d502SPatrick McHardy 		if (err < 0)
21343729d502SPatrick McHardy 			unregister_netdevice(dev);
21353729d502SPatrick McHardy out:
213681adee47SEric W. Biederman 		put_net(dest_net);
213738f7b870SPatrick McHardy 		return err;
213838f7b870SPatrick McHardy 	}
213938f7b870SPatrick McHardy }
214038f7b870SPatrick McHardy 
2141661d2967SThomas Graf static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh)
2142711e2c33SJean Tourrilhes {
21433b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
2144b60c5115SThomas Graf 	struct ifinfomsg *ifm;
2145a3d12891SEric Dumazet 	char ifname[IFNAMSIZ];
2146b60c5115SThomas Graf 	struct nlattr *tb[IFLA_MAX+1];
2147b60c5115SThomas Graf 	struct net_device *dev = NULL;
2148b60c5115SThomas Graf 	struct sk_buff *nskb;
2149339bf98fSThomas Graf 	int err;
2150115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
2151711e2c33SJean Tourrilhes 
2152b60c5115SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2153b60c5115SThomas Graf 	if (err < 0)
21549918f230SEric Sesterhenn 		return err;
2155b60c5115SThomas Graf 
2156a3d12891SEric Dumazet 	if (tb[IFLA_IFNAME])
2157a3d12891SEric Dumazet 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2158a3d12891SEric Dumazet 
2159115c9b81SGreg Rose 	if (tb[IFLA_EXT_MASK])
2160115c9b81SGreg Rose 		ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2161115c9b81SGreg Rose 
2162b60c5115SThomas Graf 	ifm = nlmsg_data(nlh);
2163a3d12891SEric Dumazet 	if (ifm->ifi_index > 0)
2164a3d12891SEric Dumazet 		dev = __dev_get_by_index(net, ifm->ifi_index);
2165a3d12891SEric Dumazet 	else if (tb[IFLA_IFNAME])
2166a3d12891SEric Dumazet 		dev = __dev_get_by_name(net, ifname);
2167a3d12891SEric Dumazet 	else
2168b60c5115SThomas Graf 		return -EINVAL;
2169b60c5115SThomas Graf 
2170a3d12891SEric Dumazet 	if (dev == NULL)
2171a3d12891SEric Dumazet 		return -ENODEV;
2172a3d12891SEric Dumazet 
2173115c9b81SGreg Rose 	nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
2174a3d12891SEric Dumazet 	if (nskb == NULL)
2175a3d12891SEric Dumazet 		return -ENOBUFS;
2176711e2c33SJean Tourrilhes 
217715e47304SEric W. Biederman 	err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid,
2178115c9b81SGreg Rose 			       nlh->nlmsg_seq, 0, 0, ext_filter_mask);
217926932566SPatrick McHardy 	if (err < 0) {
218026932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in if_nlmsg_size */
218126932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
218226932566SPatrick McHardy 		kfree_skb(nskb);
2183a3d12891SEric Dumazet 	} else
218415e47304SEric W. Biederman 		err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
2185b60c5115SThomas Graf 
2186711e2c33SJean Tourrilhes 	return err;
2187711e2c33SJean Tourrilhes }
2188711e2c33SJean Tourrilhes 
2189115c9b81SGreg Rose static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
2190c7ac8679SGreg Rose {
2191115c9b81SGreg Rose 	struct net *net = sock_net(skb->sk);
2192115c9b81SGreg Rose 	struct net_device *dev;
2193115c9b81SGreg Rose 	struct nlattr *tb[IFLA_MAX+1];
2194115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
2195115c9b81SGreg Rose 	u16 min_ifinfo_dump_size = 0;
2196e5eca6d4SMichal Schmidt 	int hdrlen;
2197115c9b81SGreg Rose 
2198e5eca6d4SMichal Schmidt 	/* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
2199e5eca6d4SMichal Schmidt 	hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2200e5eca6d4SMichal Schmidt 		 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2201e5eca6d4SMichal Schmidt 
2202e5eca6d4SMichal Schmidt 	if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
2203115c9b81SGreg Rose 		if (tb[IFLA_EXT_MASK])
2204115c9b81SGreg Rose 			ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2205a4b64fbeSEric Dumazet 	}
2206115c9b81SGreg Rose 
2207115c9b81SGreg Rose 	if (!ext_filter_mask)
2208115c9b81SGreg Rose 		return NLMSG_GOODSIZE;
2209115c9b81SGreg Rose 	/*
2210115c9b81SGreg Rose 	 * traverse the list of net devices and compute the minimum
2211115c9b81SGreg Rose 	 * buffer size based upon the filter mask.
2212115c9b81SGreg Rose 	 */
2213115c9b81SGreg Rose 	list_for_each_entry(dev, &net->dev_base_head, dev_list) {
2214115c9b81SGreg Rose 		min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
2215115c9b81SGreg Rose 					     if_nlmsg_size(dev,
2216115c9b81SGreg Rose 						           ext_filter_mask));
2217115c9b81SGreg Rose 	}
2218115c9b81SGreg Rose 
2219c7ac8679SGreg Rose 	return min_ifinfo_dump_size;
2220c7ac8679SGreg Rose }
2221c7ac8679SGreg Rose 
222242bad1daSAdrian Bunk static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
22231da177e4SLinus Torvalds {
22241da177e4SLinus Torvalds 	int idx;
22251da177e4SLinus Torvalds 	int s_idx = cb->family;
22261da177e4SLinus Torvalds 
22271da177e4SLinus Torvalds 	if (s_idx == 0)
22281da177e4SLinus Torvalds 		s_idx = 1;
222925239ceeSPatrick McHardy 	for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
22301da177e4SLinus Torvalds 		int type = cb->nlh->nlmsg_type-RTM_BASE;
22311da177e4SLinus Torvalds 		if (idx < s_idx || idx == PF_PACKET)
22321da177e4SLinus Torvalds 			continue;
2233e2849863SThomas Graf 		if (rtnl_msg_handlers[idx] == NULL ||
2234e2849863SThomas Graf 		    rtnl_msg_handlers[idx][type].dumpit == NULL)
22351da177e4SLinus Torvalds 			continue;
22360465277fSNicolas Dichtel 		if (idx > s_idx) {
22371da177e4SLinus Torvalds 			memset(&cb->args[0], 0, sizeof(cb->args));
22380465277fSNicolas Dichtel 			cb->prev_seq = 0;
22390465277fSNicolas Dichtel 			cb->seq = 0;
22400465277fSNicolas Dichtel 		}
2241e2849863SThomas Graf 		if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
22421da177e4SLinus Torvalds 			break;
22431da177e4SLinus Torvalds 	}
22441da177e4SLinus Torvalds 	cb->family = idx;
22451da177e4SLinus Torvalds 
22461da177e4SLinus Torvalds 	return skb->len;
22471da177e4SLinus Torvalds }
22481da177e4SLinus Torvalds 
2249395eea6cSMahesh Bandewar struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
2250395eea6cSMahesh Bandewar 				       unsigned int change, gfp_t flags)
22511da177e4SLinus Torvalds {
2252c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
22531da177e4SLinus Torvalds 	struct sk_buff *skb;
22540ec6d3f4SThomas Graf 	int err = -ENOBUFS;
2255c7ac8679SGreg Rose 	size_t if_info_size;
22561da177e4SLinus Torvalds 
22577f294054SAlexei Starovoitov 	skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
22580ec6d3f4SThomas Graf 	if (skb == NULL)
22590ec6d3f4SThomas Graf 		goto errout;
22601da177e4SLinus Torvalds 
2261115c9b81SGreg Rose 	err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0);
226226932566SPatrick McHardy 	if (err < 0) {
226326932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in if_nlmsg_size() */
226426932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
226526932566SPatrick McHardy 		kfree_skb(skb);
226626932566SPatrick McHardy 		goto errout;
226726932566SPatrick McHardy 	}
2268395eea6cSMahesh Bandewar 	return skb;
22690ec6d3f4SThomas Graf errout:
22700ec6d3f4SThomas Graf 	if (err < 0)
22714b3da706SEric W. Biederman 		rtnl_set_sk_err(net, RTNLGRP_LINK, err);
2272395eea6cSMahesh Bandewar 	return NULL;
2273395eea6cSMahesh Bandewar }
2274395eea6cSMahesh Bandewar 
2275395eea6cSMahesh Bandewar void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
2276395eea6cSMahesh Bandewar {
2277395eea6cSMahesh Bandewar 	struct net *net = dev_net(dev);
2278395eea6cSMahesh Bandewar 
2279395eea6cSMahesh Bandewar 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
2280395eea6cSMahesh Bandewar }
2281395eea6cSMahesh Bandewar 
2282395eea6cSMahesh Bandewar void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
2283395eea6cSMahesh Bandewar 		  gfp_t flags)
2284395eea6cSMahesh Bandewar {
2285395eea6cSMahesh Bandewar 	struct sk_buff *skb;
2286395eea6cSMahesh Bandewar 
2287395eea6cSMahesh Bandewar 	skb = rtmsg_ifinfo_build_skb(type, dev, change, flags);
2288395eea6cSMahesh Bandewar 	if (skb)
2289395eea6cSMahesh Bandewar 		rtmsg_ifinfo_send(skb, dev, flags);
22901da177e4SLinus Torvalds }
2291471cb5a3SJiri Pirko EXPORT_SYMBOL(rtmsg_ifinfo);
22921da177e4SLinus Torvalds 
2293d83b0603SJohn Fastabend static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
2294d83b0603SJohn Fastabend 				   struct net_device *dev,
2295d83b0603SJohn Fastabend 				   u8 *addr, u32 pid, u32 seq,
22961c104a6bSNicolas Dichtel 				   int type, unsigned int flags,
22971c104a6bSNicolas Dichtel 				   int nlflags)
2298d83b0603SJohn Fastabend {
2299d83b0603SJohn Fastabend 	struct nlmsghdr *nlh;
2300d83b0603SJohn Fastabend 	struct ndmsg *ndm;
2301d83b0603SJohn Fastabend 
23021c104a6bSNicolas Dichtel 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
2303d83b0603SJohn Fastabend 	if (!nlh)
2304d83b0603SJohn Fastabend 		return -EMSGSIZE;
2305d83b0603SJohn Fastabend 
2306d83b0603SJohn Fastabend 	ndm = nlmsg_data(nlh);
2307d83b0603SJohn Fastabend 	ndm->ndm_family  = AF_BRIDGE;
2308d83b0603SJohn Fastabend 	ndm->ndm_pad1	 = 0;
2309d83b0603SJohn Fastabend 	ndm->ndm_pad2    = 0;
2310d83b0603SJohn Fastabend 	ndm->ndm_flags	 = flags;
2311d83b0603SJohn Fastabend 	ndm->ndm_type	 = 0;
2312d83b0603SJohn Fastabend 	ndm->ndm_ifindex = dev->ifindex;
2313d83b0603SJohn Fastabend 	ndm->ndm_state   = NUD_PERMANENT;
2314d83b0603SJohn Fastabend 
2315d83b0603SJohn Fastabend 	if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
2316d83b0603SJohn Fastabend 		goto nla_put_failure;
2317d83b0603SJohn Fastabend 
2318d83b0603SJohn Fastabend 	return nlmsg_end(skb, nlh);
2319d83b0603SJohn Fastabend 
2320d83b0603SJohn Fastabend nla_put_failure:
2321d83b0603SJohn Fastabend 	nlmsg_cancel(skb, nlh);
2322d83b0603SJohn Fastabend 	return -EMSGSIZE;
2323d83b0603SJohn Fastabend }
2324d83b0603SJohn Fastabend 
23253ff661c3SJohn Fastabend static inline size_t rtnl_fdb_nlmsg_size(void)
23263ff661c3SJohn Fastabend {
23273ff661c3SJohn Fastabend 	return NLMSG_ALIGN(sizeof(struct ndmsg)) + nla_total_size(ETH_ALEN);
23283ff661c3SJohn Fastabend }
23293ff661c3SJohn Fastabend 
23303ff661c3SJohn Fastabend static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, int type)
23313ff661c3SJohn Fastabend {
23323ff661c3SJohn Fastabend 	struct net *net = dev_net(dev);
23333ff661c3SJohn Fastabend 	struct sk_buff *skb;
23343ff661c3SJohn Fastabend 	int err = -ENOBUFS;
23353ff661c3SJohn Fastabend 
23363ff661c3SJohn Fastabend 	skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
23373ff661c3SJohn Fastabend 	if (!skb)
23383ff661c3SJohn Fastabend 		goto errout;
23393ff661c3SJohn Fastabend 
23401c104a6bSNicolas Dichtel 	err = nlmsg_populate_fdb_fill(skb, dev, addr, 0, 0, type, NTF_SELF, 0);
23413ff661c3SJohn Fastabend 	if (err < 0) {
23423ff661c3SJohn Fastabend 		kfree_skb(skb);
23433ff661c3SJohn Fastabend 		goto errout;
23443ff661c3SJohn Fastabend 	}
23453ff661c3SJohn Fastabend 
23463ff661c3SJohn Fastabend 	rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
23473ff661c3SJohn Fastabend 	return;
23483ff661c3SJohn Fastabend errout:
23493ff661c3SJohn Fastabend 	rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
23503ff661c3SJohn Fastabend }
23513ff661c3SJohn Fastabend 
2352090096bfSVlad Yasevich /**
2353090096bfSVlad Yasevich  * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
2354090096bfSVlad Yasevich  */
2355090096bfSVlad Yasevich int ndo_dflt_fdb_add(struct ndmsg *ndm,
2356090096bfSVlad Yasevich 		     struct nlattr *tb[],
2357090096bfSVlad Yasevich 		     struct net_device *dev,
2358f6f6424bSJiri Pirko 		     const unsigned char *addr, u16 vid,
2359090096bfSVlad Yasevich 		     u16 flags)
2360090096bfSVlad Yasevich {
2361090096bfSVlad Yasevich 	int err = -EINVAL;
2362090096bfSVlad Yasevich 
2363090096bfSVlad Yasevich 	/* If aging addresses are supported device will need to
2364090096bfSVlad Yasevich 	 * implement its own handler for this.
2365090096bfSVlad Yasevich 	 */
2366090096bfSVlad Yasevich 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
2367090096bfSVlad Yasevich 		pr_info("%s: FDB only supports static addresses\n", dev->name);
2368090096bfSVlad Yasevich 		return err;
2369090096bfSVlad Yasevich 	}
2370090096bfSVlad Yasevich 
237165891feaSOr Gerlitz 	if (vid) {
237265891feaSOr Gerlitz 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
237365891feaSOr Gerlitz 		return err;
237465891feaSOr Gerlitz 	}
237565891feaSOr Gerlitz 
2376090096bfSVlad Yasevich 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2377090096bfSVlad Yasevich 		err = dev_uc_add_excl(dev, addr);
2378090096bfSVlad Yasevich 	else if (is_multicast_ether_addr(addr))
2379090096bfSVlad Yasevich 		err = dev_mc_add_excl(dev, addr);
2380090096bfSVlad Yasevich 
2381090096bfSVlad Yasevich 	/* Only return duplicate errors if NLM_F_EXCL is set */
2382090096bfSVlad Yasevich 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
2383090096bfSVlad Yasevich 		err = 0;
2384090096bfSVlad Yasevich 
2385090096bfSVlad Yasevich 	return err;
2386090096bfSVlad Yasevich }
2387090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_add);
2388090096bfSVlad Yasevich 
2389f6f6424bSJiri Pirko static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid)
2390f6f6424bSJiri Pirko {
2391f6f6424bSJiri Pirko 	u16 vid = 0;
2392f6f6424bSJiri Pirko 
2393f6f6424bSJiri Pirko 	if (vlan_attr) {
2394f6f6424bSJiri Pirko 		if (nla_len(vlan_attr) != sizeof(u16)) {
2395f6f6424bSJiri Pirko 			pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan\n");
2396f6f6424bSJiri Pirko 			return -EINVAL;
2397f6f6424bSJiri Pirko 		}
2398f6f6424bSJiri Pirko 
2399f6f6424bSJiri Pirko 		vid = nla_get_u16(vlan_attr);
2400f6f6424bSJiri Pirko 
2401f6f6424bSJiri Pirko 		if (!vid || vid >= VLAN_VID_MASK) {
2402f6f6424bSJiri Pirko 			pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan id %d\n",
2403f6f6424bSJiri Pirko 				vid);
2404f6f6424bSJiri Pirko 			return -EINVAL;
2405f6f6424bSJiri Pirko 		}
2406f6f6424bSJiri Pirko 	}
2407f6f6424bSJiri Pirko 	*p_vid = vid;
2408f6f6424bSJiri Pirko 	return 0;
2409f6f6424bSJiri Pirko }
2410f6f6424bSJiri Pirko 
2411661d2967SThomas Graf static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh)
241277162022SJohn Fastabend {
241377162022SJohn Fastabend 	struct net *net = sock_net(skb->sk);
241477162022SJohn Fastabend 	struct ndmsg *ndm;
241577162022SJohn Fastabend 	struct nlattr *tb[NDA_MAX+1];
241677162022SJohn Fastabend 	struct net_device *dev;
241777162022SJohn Fastabend 	u8 *addr;
2418f6f6424bSJiri Pirko 	u16 vid;
241977162022SJohn Fastabend 	int err;
242077162022SJohn Fastabend 
242177162022SJohn Fastabend 	err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
242277162022SJohn Fastabend 	if (err < 0)
242377162022SJohn Fastabend 		return err;
242477162022SJohn Fastabend 
242577162022SJohn Fastabend 	ndm = nlmsg_data(nlh);
242677162022SJohn Fastabend 	if (ndm->ndm_ifindex == 0) {
242777162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n");
242877162022SJohn Fastabend 		return -EINVAL;
242977162022SJohn Fastabend 	}
243077162022SJohn Fastabend 
243177162022SJohn Fastabend 	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
243277162022SJohn Fastabend 	if (dev == NULL) {
243377162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n");
243477162022SJohn Fastabend 		return -ENODEV;
243577162022SJohn Fastabend 	}
243677162022SJohn Fastabend 
243777162022SJohn Fastabend 	if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
243877162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n");
243977162022SJohn Fastabend 		return -EINVAL;
244077162022SJohn Fastabend 	}
244177162022SJohn Fastabend 
244277162022SJohn Fastabend 	addr = nla_data(tb[NDA_LLADDR]);
244377162022SJohn Fastabend 
2444f6f6424bSJiri Pirko 	err = fdb_vid_parse(tb[NDA_VLAN], &vid);
2445f6f6424bSJiri Pirko 	if (err)
2446f6f6424bSJiri Pirko 		return err;
2447f6f6424bSJiri Pirko 
244877162022SJohn Fastabend 	err = -EOPNOTSUPP;
244977162022SJohn Fastabend 
245077162022SJohn Fastabend 	/* Support fdb on master device the net/bridge default case */
245177162022SJohn Fastabend 	if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
245277162022SJohn Fastabend 	    (dev->priv_flags & IFF_BRIDGE_PORT)) {
2453898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2454898e5061SJiri Pirko 		const struct net_device_ops *ops = br_dev->netdev_ops;
2455898e5061SJiri Pirko 
2456f6f6424bSJiri Pirko 		err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
2457f6f6424bSJiri Pirko 				       nlh->nlmsg_flags);
245877162022SJohn Fastabend 		if (err)
245977162022SJohn Fastabend 			goto out;
246077162022SJohn Fastabend 		else
246177162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_MASTER;
246277162022SJohn Fastabend 	}
246377162022SJohn Fastabend 
246477162022SJohn Fastabend 	/* Embedded bridge, macvlan, and any other device support */
2465090096bfSVlad Yasevich 	if ((ndm->ndm_flags & NTF_SELF)) {
2466090096bfSVlad Yasevich 		if (dev->netdev_ops->ndo_fdb_add)
2467090096bfSVlad Yasevich 			err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
2468f6f6424bSJiri Pirko 							   vid,
2469090096bfSVlad Yasevich 							   nlh->nlmsg_flags);
2470090096bfSVlad Yasevich 		else
2471f6f6424bSJiri Pirko 			err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
247277162022SJohn Fastabend 					       nlh->nlmsg_flags);
247377162022SJohn Fastabend 
24743ff661c3SJohn Fastabend 		if (!err) {
24753ff661c3SJohn Fastabend 			rtnl_fdb_notify(dev, addr, RTM_NEWNEIGH);
247677162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_SELF;
247777162022SJohn Fastabend 		}
24783ff661c3SJohn Fastabend 	}
247977162022SJohn Fastabend out:
248077162022SJohn Fastabend 	return err;
248177162022SJohn Fastabend }
248277162022SJohn Fastabend 
2483090096bfSVlad Yasevich /**
2484090096bfSVlad Yasevich  * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
2485090096bfSVlad Yasevich  */
2486090096bfSVlad Yasevich int ndo_dflt_fdb_del(struct ndmsg *ndm,
2487090096bfSVlad Yasevich 		     struct nlattr *tb[],
2488090096bfSVlad Yasevich 		     struct net_device *dev,
2489f6f6424bSJiri Pirko 		     const unsigned char *addr, u16 vid)
2490090096bfSVlad Yasevich {
2491c8a89c4aSAlexander Duyck 	int err = -EINVAL;
2492090096bfSVlad Yasevich 
2493090096bfSVlad Yasevich 	/* If aging addresses are supported device will need to
2494090096bfSVlad Yasevich 	 * implement its own handler for this.
2495090096bfSVlad Yasevich 	 */
249664535993SSridhar Samudrala 	if (!(ndm->ndm_state & NUD_PERMANENT)) {
2497090096bfSVlad Yasevich 		pr_info("%s: FDB only supports static addresses\n", dev->name);
2498c8a89c4aSAlexander Duyck 		return err;
2499090096bfSVlad Yasevich 	}
2500090096bfSVlad Yasevich 
2501090096bfSVlad Yasevich 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2502090096bfSVlad Yasevich 		err = dev_uc_del(dev, addr);
2503090096bfSVlad Yasevich 	else if (is_multicast_ether_addr(addr))
2504090096bfSVlad Yasevich 		err = dev_mc_del(dev, addr);
2505090096bfSVlad Yasevich 
2506090096bfSVlad Yasevich 	return err;
2507090096bfSVlad Yasevich }
2508090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_del);
2509090096bfSVlad Yasevich 
2510661d2967SThomas Graf static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh)
251177162022SJohn Fastabend {
251277162022SJohn Fastabend 	struct net *net = sock_net(skb->sk);
251377162022SJohn Fastabend 	struct ndmsg *ndm;
25141690be63SVlad Yasevich 	struct nlattr *tb[NDA_MAX+1];
251577162022SJohn Fastabend 	struct net_device *dev;
251677162022SJohn Fastabend 	int err = -EINVAL;
251777162022SJohn Fastabend 	__u8 *addr;
2518f6f6424bSJiri Pirko 	u16 vid;
251977162022SJohn Fastabend 
252090f62cf3SEric W. Biederman 	if (!netlink_capable(skb, CAP_NET_ADMIN))
25211690be63SVlad Yasevich 		return -EPERM;
25221690be63SVlad Yasevich 
25231690be63SVlad Yasevich 	err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
25241690be63SVlad Yasevich 	if (err < 0)
25251690be63SVlad Yasevich 		return err;
252677162022SJohn Fastabend 
252777162022SJohn Fastabend 	ndm = nlmsg_data(nlh);
252877162022SJohn Fastabend 	if (ndm->ndm_ifindex == 0) {
252977162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n");
253077162022SJohn Fastabend 		return -EINVAL;
253177162022SJohn Fastabend 	}
253277162022SJohn Fastabend 
253377162022SJohn Fastabend 	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
253477162022SJohn Fastabend 	if (dev == NULL) {
253577162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n");
253677162022SJohn Fastabend 		return -ENODEV;
253777162022SJohn Fastabend 	}
253877162022SJohn Fastabend 
25391690be63SVlad Yasevich 	if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
25401690be63SVlad Yasevich 		pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n");
254177162022SJohn Fastabend 		return -EINVAL;
254277162022SJohn Fastabend 	}
254377162022SJohn Fastabend 
25441690be63SVlad Yasevich 	addr = nla_data(tb[NDA_LLADDR]);
25451690be63SVlad Yasevich 
2546f6f6424bSJiri Pirko 	err = fdb_vid_parse(tb[NDA_VLAN], &vid);
2547f6f6424bSJiri Pirko 	if (err)
2548f6f6424bSJiri Pirko 		return err;
2549f6f6424bSJiri Pirko 
255077162022SJohn Fastabend 	err = -EOPNOTSUPP;
255177162022SJohn Fastabend 
255277162022SJohn Fastabend 	/* Support fdb on master device the net/bridge default case */
255377162022SJohn Fastabend 	if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
255477162022SJohn Fastabend 	    (dev->priv_flags & IFF_BRIDGE_PORT)) {
2555898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2556898e5061SJiri Pirko 		const struct net_device_ops *ops = br_dev->netdev_ops;
255777162022SJohn Fastabend 
2558898e5061SJiri Pirko 		if (ops->ndo_fdb_del)
2559f6f6424bSJiri Pirko 			err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
256077162022SJohn Fastabend 
256177162022SJohn Fastabend 		if (err)
256277162022SJohn Fastabend 			goto out;
256377162022SJohn Fastabend 		else
256477162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_MASTER;
256577162022SJohn Fastabend 	}
256677162022SJohn Fastabend 
256777162022SJohn Fastabend 	/* Embedded bridge, macvlan, and any other device support */
2568090096bfSVlad Yasevich 	if (ndm->ndm_flags & NTF_SELF) {
2569090096bfSVlad Yasevich 		if (dev->netdev_ops->ndo_fdb_del)
2570f6f6424bSJiri Pirko 			err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
2571f6f6424bSJiri Pirko 							   vid);
2572090096bfSVlad Yasevich 		else
2573f6f6424bSJiri Pirko 			err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
257477162022SJohn Fastabend 
25753ff661c3SJohn Fastabend 		if (!err) {
25763ff661c3SJohn Fastabend 			rtnl_fdb_notify(dev, addr, RTM_DELNEIGH);
257777162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_SELF;
257877162022SJohn Fastabend 		}
25793ff661c3SJohn Fastabend 	}
258077162022SJohn Fastabend out:
258177162022SJohn Fastabend 	return err;
258277162022SJohn Fastabend }
258377162022SJohn Fastabend 
2584d83b0603SJohn Fastabend static int nlmsg_populate_fdb(struct sk_buff *skb,
2585d83b0603SJohn Fastabend 			      struct netlink_callback *cb,
2586d83b0603SJohn Fastabend 			      struct net_device *dev,
2587d83b0603SJohn Fastabend 			      int *idx,
2588d83b0603SJohn Fastabend 			      struct netdev_hw_addr_list *list)
2589d83b0603SJohn Fastabend {
2590d83b0603SJohn Fastabend 	struct netdev_hw_addr *ha;
2591d83b0603SJohn Fastabend 	int err;
259215e47304SEric W. Biederman 	u32 portid, seq;
2593d83b0603SJohn Fastabend 
259415e47304SEric W. Biederman 	portid = NETLINK_CB(cb->skb).portid;
2595d83b0603SJohn Fastabend 	seq = cb->nlh->nlmsg_seq;
2596d83b0603SJohn Fastabend 
2597d83b0603SJohn Fastabend 	list_for_each_entry(ha, &list->list, list) {
2598d83b0603SJohn Fastabend 		if (*idx < cb->args[0])
2599d83b0603SJohn Fastabend 			goto skip;
2600d83b0603SJohn Fastabend 
2601d83b0603SJohn Fastabend 		err = nlmsg_populate_fdb_fill(skb, dev, ha->addr,
2602a7a558feSJohn Fastabend 					      portid, seq,
26031c104a6bSNicolas Dichtel 					      RTM_NEWNEIGH, NTF_SELF,
26041c104a6bSNicolas Dichtel 					      NLM_F_MULTI);
2605d83b0603SJohn Fastabend 		if (err < 0)
2606d83b0603SJohn Fastabend 			return err;
2607d83b0603SJohn Fastabend skip:
2608d83b0603SJohn Fastabend 		*idx += 1;
2609d83b0603SJohn Fastabend 	}
2610d83b0603SJohn Fastabend 	return 0;
2611d83b0603SJohn Fastabend }
2612d83b0603SJohn Fastabend 
2613d83b0603SJohn Fastabend /**
26142c53040fSBen Hutchings  * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
2615d83b0603SJohn Fastabend  * @nlh: netlink message header
2616d83b0603SJohn Fastabend  * @dev: netdevice
2617d83b0603SJohn Fastabend  *
2618d83b0603SJohn Fastabend  * Default netdevice operation to dump the existing unicast address list.
261991f3e7b1SJohn Fastabend  * Returns number of addresses from list put in skb.
2620d83b0603SJohn Fastabend  */
2621d83b0603SJohn Fastabend int ndo_dflt_fdb_dump(struct sk_buff *skb,
2622d83b0603SJohn Fastabend 		      struct netlink_callback *cb,
2623d83b0603SJohn Fastabend 		      struct net_device *dev,
26245d5eacb3SJamal Hadi Salim 		      struct net_device *filter_dev,
2625d83b0603SJohn Fastabend 		      int idx)
2626d83b0603SJohn Fastabend {
2627d83b0603SJohn Fastabend 	int err;
2628d83b0603SJohn Fastabend 
2629d83b0603SJohn Fastabend 	netif_addr_lock_bh(dev);
2630d83b0603SJohn Fastabend 	err = nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->uc);
2631d83b0603SJohn Fastabend 	if (err)
2632d83b0603SJohn Fastabend 		goto out;
2633d83b0603SJohn Fastabend 	nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->mc);
2634d83b0603SJohn Fastabend out:
2635d83b0603SJohn Fastabend 	netif_addr_unlock_bh(dev);
2636d83b0603SJohn Fastabend 	return idx;
2637d83b0603SJohn Fastabend }
2638d83b0603SJohn Fastabend EXPORT_SYMBOL(ndo_dflt_fdb_dump);
2639d83b0603SJohn Fastabend 
264077162022SJohn Fastabend static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
264177162022SJohn Fastabend {
264277162022SJohn Fastabend 	struct net_device *dev;
26435e6d2435SJamal Hadi Salim 	struct nlattr *tb[IFLA_MAX+1];
26445e6d2435SJamal Hadi Salim 	struct net_device *bdev = NULL;
26455e6d2435SJamal Hadi Salim 	struct net_device *br_dev = NULL;
26465e6d2435SJamal Hadi Salim 	const struct net_device_ops *ops = NULL;
26475e6d2435SJamal Hadi Salim 	const struct net_device_ops *cops = NULL;
26485e6d2435SJamal Hadi Salim 	struct ifinfomsg *ifm = nlmsg_data(cb->nlh);
26495e6d2435SJamal Hadi Salim 	struct net *net = sock_net(skb->sk);
26505e6d2435SJamal Hadi Salim 	int brport_idx = 0;
26515e6d2435SJamal Hadi Salim 	int br_idx = 0;
26525e6d2435SJamal Hadi Salim 	int idx = 0;
265377162022SJohn Fastabend 
26545e6d2435SJamal Hadi Salim 	if (nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX,
26555e6d2435SJamal Hadi Salim 			ifla_policy) == 0) {
26565e6d2435SJamal Hadi Salim 		if (tb[IFLA_MASTER])
26575e6d2435SJamal Hadi Salim 			br_idx = nla_get_u32(tb[IFLA_MASTER]);
26585e6d2435SJamal Hadi Salim 	}
265977162022SJohn Fastabend 
26605e6d2435SJamal Hadi Salim 	brport_idx = ifm->ifi_index;
26615e6d2435SJamal Hadi Salim 
26625e6d2435SJamal Hadi Salim 	if (br_idx) {
26635e6d2435SJamal Hadi Salim 		br_dev = __dev_get_by_index(net, br_idx);
26645e6d2435SJamal Hadi Salim 		if (!br_dev)
26655e6d2435SJamal Hadi Salim 			return -ENODEV;
26665e6d2435SJamal Hadi Salim 
2667898e5061SJiri Pirko 		ops = br_dev->netdev_ops;
26685e6d2435SJamal Hadi Salim 		bdev = br_dev;
26695e6d2435SJamal Hadi Salim 	}
26705e6d2435SJamal Hadi Salim 
26715e6d2435SJamal Hadi Salim 	for_each_netdev(net, dev) {
26725e6d2435SJamal Hadi Salim 		if (brport_idx && (dev->ifindex != brport_idx))
26735e6d2435SJamal Hadi Salim 			continue;
26745e6d2435SJamal Hadi Salim 
26755e6d2435SJamal Hadi Salim 		if (!br_idx) { /* user did not specify a specific bridge */
26765e6d2435SJamal Hadi Salim 			if (dev->priv_flags & IFF_BRIDGE_PORT) {
26775e6d2435SJamal Hadi Salim 				br_dev = netdev_master_upper_dev_get(dev);
26785e6d2435SJamal Hadi Salim 				cops = br_dev->netdev_ops;
26795e6d2435SJamal Hadi Salim 			}
26805e6d2435SJamal Hadi Salim 
26815e6d2435SJamal Hadi Salim 			bdev = dev;
26825e6d2435SJamal Hadi Salim 		} else {
26835e6d2435SJamal Hadi Salim 			if (dev != br_dev &&
26845e6d2435SJamal Hadi Salim 			    !(dev->priv_flags & IFF_BRIDGE_PORT))
26855e6d2435SJamal Hadi Salim 				continue;
26865e6d2435SJamal Hadi Salim 
26875e6d2435SJamal Hadi Salim 			if (br_dev != netdev_master_upper_dev_get(dev) &&
26885e6d2435SJamal Hadi Salim 			    !(dev->priv_flags & IFF_EBRIDGE))
26895e6d2435SJamal Hadi Salim 				continue;
26905e6d2435SJamal Hadi Salim 
26915e6d2435SJamal Hadi Salim 			bdev = br_dev;
26925e6d2435SJamal Hadi Salim 			cops = ops;
26935e6d2435SJamal Hadi Salim 		}
26945e6d2435SJamal Hadi Salim 
26955e6d2435SJamal Hadi Salim 		if (dev->priv_flags & IFF_BRIDGE_PORT) {
26965e6d2435SJamal Hadi Salim 			if (cops && cops->ndo_fdb_dump)
26975e6d2435SJamal Hadi Salim 				idx = cops->ndo_fdb_dump(skb, cb, br_dev, dev,
26985d5eacb3SJamal Hadi Salim 							 idx);
269977162022SJohn Fastabend 		}
270077162022SJohn Fastabend 
27015e6d2435SJamal Hadi Salim 		if (dev->netdev_ops->ndo_fdb_dump)
2702*6cb69742SHubert Sokolowski 			idx = dev->netdev_ops->ndo_fdb_dump(skb, cb, dev, NULL,
27035e6d2435SJamal Hadi Salim 							    idx);
2704*6cb69742SHubert Sokolowski 		else
2705*6cb69742SHubert Sokolowski 			idx = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx);
27065e6d2435SJamal Hadi Salim 
27075e6d2435SJamal Hadi Salim 		cops = NULL;
270877162022SJohn Fastabend 	}
270977162022SJohn Fastabend 
271077162022SJohn Fastabend 	cb->args[0] = idx;
271177162022SJohn Fastabend 	return skb->len;
271277162022SJohn Fastabend }
271377162022SJohn Fastabend 
27142c3c031cSScott Feldman static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
27152c3c031cSScott Feldman 			       unsigned int attrnum, unsigned int flag)
27162c3c031cSScott Feldman {
27172c3c031cSScott Feldman 	if (mask & flag)
27182c3c031cSScott Feldman 		return nla_put_u8(skb, attrnum, !!(flags & flag));
27192c3c031cSScott Feldman 	return 0;
27202c3c031cSScott Feldman }
27212c3c031cSScott Feldman 
2722815cccbfSJohn Fastabend int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
27232c3c031cSScott Feldman 			    struct net_device *dev, u16 mode,
27242c3c031cSScott Feldman 			    u32 flags, u32 mask)
2725815cccbfSJohn Fastabend {
2726815cccbfSJohn Fastabend 	struct nlmsghdr *nlh;
2727815cccbfSJohn Fastabend 	struct ifinfomsg *ifm;
2728815cccbfSJohn Fastabend 	struct nlattr *br_afspec;
27292c3c031cSScott Feldman 	struct nlattr *protinfo;
2730815cccbfSJohn Fastabend 	u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
2731898e5061SJiri Pirko 	struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2732815cccbfSJohn Fastabend 
2733815cccbfSJohn Fastabend 	nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), NLM_F_MULTI);
2734815cccbfSJohn Fastabend 	if (nlh == NULL)
2735815cccbfSJohn Fastabend 		return -EMSGSIZE;
2736815cccbfSJohn Fastabend 
2737815cccbfSJohn Fastabend 	ifm = nlmsg_data(nlh);
2738815cccbfSJohn Fastabend 	ifm->ifi_family = AF_BRIDGE;
2739815cccbfSJohn Fastabend 	ifm->__ifi_pad = 0;
2740815cccbfSJohn Fastabend 	ifm->ifi_type = dev->type;
2741815cccbfSJohn Fastabend 	ifm->ifi_index = dev->ifindex;
2742815cccbfSJohn Fastabend 	ifm->ifi_flags = dev_get_flags(dev);
2743815cccbfSJohn Fastabend 	ifm->ifi_change = 0;
2744815cccbfSJohn Fastabend 
2745815cccbfSJohn Fastabend 
2746815cccbfSJohn Fastabend 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
2747815cccbfSJohn Fastabend 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
2748815cccbfSJohn Fastabend 	    nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
2749898e5061SJiri Pirko 	    (br_dev &&
2750898e5061SJiri Pirko 	     nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
2751815cccbfSJohn Fastabend 	    (dev->addr_len &&
2752815cccbfSJohn Fastabend 	     nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
2753815cccbfSJohn Fastabend 	    (dev->ifindex != dev->iflink &&
2754815cccbfSJohn Fastabend 	     nla_put_u32(skb, IFLA_LINK, dev->iflink)))
2755815cccbfSJohn Fastabend 		goto nla_put_failure;
2756815cccbfSJohn Fastabend 
2757815cccbfSJohn Fastabend 	br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
2758815cccbfSJohn Fastabend 	if (!br_afspec)
2759815cccbfSJohn Fastabend 		goto nla_put_failure;
2760815cccbfSJohn Fastabend 
27611d460b98SRoopa Prabhu 	if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
2762815cccbfSJohn Fastabend 		nla_nest_cancel(skb, br_afspec);
2763815cccbfSJohn Fastabend 		goto nla_put_failure;
2764815cccbfSJohn Fastabend 	}
27651d460b98SRoopa Prabhu 
27661d460b98SRoopa Prabhu 	if (mode != BRIDGE_MODE_UNDEF) {
27671d460b98SRoopa Prabhu 		if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
27681d460b98SRoopa Prabhu 			nla_nest_cancel(skb, br_afspec);
27691d460b98SRoopa Prabhu 			goto nla_put_failure;
27701d460b98SRoopa Prabhu 		}
27711d460b98SRoopa Prabhu 	}
2772815cccbfSJohn Fastabend 	nla_nest_end(skb, br_afspec);
2773815cccbfSJohn Fastabend 
27742c3c031cSScott Feldman 	protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
27752c3c031cSScott Feldman 	if (!protinfo)
27762c3c031cSScott Feldman 		goto nla_put_failure;
27772c3c031cSScott Feldman 
27782c3c031cSScott Feldman 	if (brport_nla_put_flag(skb, flags, mask,
27792c3c031cSScott Feldman 				IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
27802c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
27812c3c031cSScott Feldman 				IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
27822c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
27832c3c031cSScott Feldman 				IFLA_BRPORT_FAST_LEAVE,
27842c3c031cSScott Feldman 				BR_MULTICAST_FAST_LEAVE) ||
27852c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
27862c3c031cSScott Feldman 				IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
27872c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
27882c3c031cSScott Feldman 				IFLA_BRPORT_LEARNING, BR_LEARNING) ||
27892c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
27902c3c031cSScott Feldman 				IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
27912c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
27922c3c031cSScott Feldman 				IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
27932c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
27942c3c031cSScott Feldman 				IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
27952c3c031cSScott Feldman 		nla_nest_cancel(skb, protinfo);
27962c3c031cSScott Feldman 		goto nla_put_failure;
27972c3c031cSScott Feldman 	}
27982c3c031cSScott Feldman 
27992c3c031cSScott Feldman 	nla_nest_end(skb, protinfo);
28002c3c031cSScott Feldman 
2801815cccbfSJohn Fastabend 	return nlmsg_end(skb, nlh);
2802815cccbfSJohn Fastabend nla_put_failure:
2803815cccbfSJohn Fastabend 	nlmsg_cancel(skb, nlh);
2804815cccbfSJohn Fastabend 	return -EMSGSIZE;
2805815cccbfSJohn Fastabend }
2806815cccbfSJohn Fastabend EXPORT_SYMBOL(ndo_dflt_bridge_getlink);
2807815cccbfSJohn Fastabend 
2808e5a55a89SJohn Fastabend static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
2809e5a55a89SJohn Fastabend {
2810e5a55a89SJohn Fastabend 	struct net *net = sock_net(skb->sk);
2811e5a55a89SJohn Fastabend 	struct net_device *dev;
2812e5a55a89SJohn Fastabend 	int idx = 0;
2813e5a55a89SJohn Fastabend 	u32 portid = NETLINK_CB(cb->skb).portid;
2814e5a55a89SJohn Fastabend 	u32 seq = cb->nlh->nlmsg_seq;
28156cbdceebSVlad Yasevich 	u32 filter_mask = 0;
28166cbdceebSVlad Yasevich 
2817aa68c20fSThomas Graf 	if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) {
2818aa68c20fSThomas Graf 		struct nlattr *extfilt;
2819aa68c20fSThomas Graf 
28203e805ad2SAsbjoern Sloth Toennesen 		extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
28216cbdceebSVlad Yasevich 					  IFLA_EXT_MASK);
2822aa68c20fSThomas Graf 		if (extfilt) {
2823aa68c20fSThomas Graf 			if (nla_len(extfilt) < sizeof(filter_mask))
2824aa68c20fSThomas Graf 				return -EINVAL;
2825aa68c20fSThomas Graf 
28266cbdceebSVlad Yasevich 			filter_mask = nla_get_u32(extfilt);
2827aa68c20fSThomas Graf 		}
2828aa68c20fSThomas Graf 	}
2829e5a55a89SJohn Fastabend 
2830e5a55a89SJohn Fastabend 	rcu_read_lock();
2831e5a55a89SJohn Fastabend 	for_each_netdev_rcu(net, dev) {
2832e5a55a89SJohn Fastabend 		const struct net_device_ops *ops = dev->netdev_ops;
2833898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2834e5a55a89SJohn Fastabend 
2835898e5061SJiri Pirko 		if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
283625b1e679SBen Hutchings 			if (idx >= cb->args[0] &&
2837898e5061SJiri Pirko 			    br_dev->netdev_ops->ndo_bridge_getlink(
28386cbdceebSVlad Yasevich 				    skb, portid, seq, dev, filter_mask) < 0)
2839e5a55a89SJohn Fastabend 				break;
2840e5a55a89SJohn Fastabend 			idx++;
2841e5a55a89SJohn Fastabend 		}
2842e5a55a89SJohn Fastabend 
2843e5a55a89SJohn Fastabend 		if (ops->ndo_bridge_getlink) {
284425b1e679SBen Hutchings 			if (idx >= cb->args[0] &&
28456cbdceebSVlad Yasevich 			    ops->ndo_bridge_getlink(skb, portid, seq, dev,
28466cbdceebSVlad Yasevich 						    filter_mask) < 0)
2847e5a55a89SJohn Fastabend 				break;
2848e5a55a89SJohn Fastabend 			idx++;
2849e5a55a89SJohn Fastabend 		}
2850e5a55a89SJohn Fastabend 	}
2851e5a55a89SJohn Fastabend 	rcu_read_unlock();
2852e5a55a89SJohn Fastabend 	cb->args[0] = idx;
2853e5a55a89SJohn Fastabend 
2854e5a55a89SJohn Fastabend 	return skb->len;
2855e5a55a89SJohn Fastabend }
2856e5a55a89SJohn Fastabend 
28572469ffd7SJohn Fastabend static inline size_t bridge_nlmsg_size(void)
28582469ffd7SJohn Fastabend {
28592469ffd7SJohn Fastabend 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
28602469ffd7SJohn Fastabend 		+ nla_total_size(IFNAMSIZ)	/* IFLA_IFNAME */
28612469ffd7SJohn Fastabend 		+ nla_total_size(MAX_ADDR_LEN)	/* IFLA_ADDRESS */
28622469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_MASTER */
28632469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_MTU */
28642469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_LINK */
28652469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_OPERSTATE */
28662469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u8))	/* IFLA_PROTINFO */
28672469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(struct nlattr))	/* IFLA_AF_SPEC */
28682469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u16))	/* IFLA_BRIDGE_FLAGS */
28692469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u16));	/* IFLA_BRIDGE_MODE */
28702469ffd7SJohn Fastabend }
28712469ffd7SJohn Fastabend 
28722469ffd7SJohn Fastabend static int rtnl_bridge_notify(struct net_device *dev, u16 flags)
28732469ffd7SJohn Fastabend {
28742469ffd7SJohn Fastabend 	struct net *net = dev_net(dev);
2875898e5061SJiri Pirko 	struct net_device *br_dev = netdev_master_upper_dev_get(dev);
28762469ffd7SJohn Fastabend 	struct sk_buff *skb;
28772469ffd7SJohn Fastabend 	int err = -EOPNOTSUPP;
28782469ffd7SJohn Fastabend 
28792469ffd7SJohn Fastabend 	skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
28802469ffd7SJohn Fastabend 	if (!skb) {
28812469ffd7SJohn Fastabend 		err = -ENOMEM;
28822469ffd7SJohn Fastabend 		goto errout;
28832469ffd7SJohn Fastabend 	}
28842469ffd7SJohn Fastabend 
2885c38e01b8SJohn Fastabend 	if ((!flags || (flags & BRIDGE_FLAGS_MASTER)) &&
2886898e5061SJiri Pirko 	    br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
28876cbdceebSVlad Yasevich 		err = br_dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0);
28882469ffd7SJohn Fastabend 		if (err < 0)
28892469ffd7SJohn Fastabend 			goto errout;
2890c38e01b8SJohn Fastabend 	}
2891c38e01b8SJohn Fastabend 
2892c38e01b8SJohn Fastabend 	if ((flags & BRIDGE_FLAGS_SELF) &&
2893c38e01b8SJohn Fastabend 	    dev->netdev_ops->ndo_bridge_getlink) {
28946cbdceebSVlad Yasevich 		err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0);
2895c38e01b8SJohn Fastabend 		if (err < 0)
2896c38e01b8SJohn Fastabend 			goto errout;
2897c38e01b8SJohn Fastabend 	}
28982469ffd7SJohn Fastabend 
28992469ffd7SJohn Fastabend 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
29002469ffd7SJohn Fastabend 	return 0;
29012469ffd7SJohn Fastabend errout:
29022469ffd7SJohn Fastabend 	WARN_ON(err == -EMSGSIZE);
29032469ffd7SJohn Fastabend 	kfree_skb(skb);
29042469ffd7SJohn Fastabend 	rtnl_set_sk_err(net, RTNLGRP_LINK, err);
29052469ffd7SJohn Fastabend 	return err;
29062469ffd7SJohn Fastabend }
29072469ffd7SJohn Fastabend 
2908661d2967SThomas Graf static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
2909e5a55a89SJohn Fastabend {
2910e5a55a89SJohn Fastabend 	struct net *net = sock_net(skb->sk);
2911e5a55a89SJohn Fastabend 	struct ifinfomsg *ifm;
2912e5a55a89SJohn Fastabend 	struct net_device *dev;
29132469ffd7SJohn Fastabend 	struct nlattr *br_spec, *attr = NULL;
29142469ffd7SJohn Fastabend 	int rem, err = -EOPNOTSUPP;
2915c38e01b8SJohn Fastabend 	u16 oflags, flags = 0;
2916c38e01b8SJohn Fastabend 	bool have_flags = false;
2917e5a55a89SJohn Fastabend 
2918e5a55a89SJohn Fastabend 	if (nlmsg_len(nlh) < sizeof(*ifm))
2919e5a55a89SJohn Fastabend 		return -EINVAL;
2920e5a55a89SJohn Fastabend 
2921e5a55a89SJohn Fastabend 	ifm = nlmsg_data(nlh);
2922e5a55a89SJohn Fastabend 	if (ifm->ifi_family != AF_BRIDGE)
2923e5a55a89SJohn Fastabend 		return -EPFNOSUPPORT;
2924e5a55a89SJohn Fastabend 
2925e5a55a89SJohn Fastabend 	dev = __dev_get_by_index(net, ifm->ifi_index);
2926e5a55a89SJohn Fastabend 	if (!dev) {
2927e5a55a89SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
2928e5a55a89SJohn Fastabend 		return -ENODEV;
2929e5a55a89SJohn Fastabend 	}
2930e5a55a89SJohn Fastabend 
29312469ffd7SJohn Fastabend 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
29322469ffd7SJohn Fastabend 	if (br_spec) {
29332469ffd7SJohn Fastabend 		nla_for_each_nested(attr, br_spec, rem) {
29342469ffd7SJohn Fastabend 			if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
29356e8d1c55SThomas Graf 				if (nla_len(attr) < sizeof(flags))
29366e8d1c55SThomas Graf 					return -EINVAL;
29376e8d1c55SThomas Graf 
2938c38e01b8SJohn Fastabend 				have_flags = true;
29392469ffd7SJohn Fastabend 				flags = nla_get_u16(attr);
29402469ffd7SJohn Fastabend 				break;
29412469ffd7SJohn Fastabend 			}
29422469ffd7SJohn Fastabend 		}
29432469ffd7SJohn Fastabend 	}
29442469ffd7SJohn Fastabend 
2945c38e01b8SJohn Fastabend 	oflags = flags;
2946c38e01b8SJohn Fastabend 
29472469ffd7SJohn Fastabend 	if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
2948898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2949898e5061SJiri Pirko 
2950898e5061SJiri Pirko 		if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
29512469ffd7SJohn Fastabend 			err = -EOPNOTSUPP;
2952e5a55a89SJohn Fastabend 			goto out;
2953e5a55a89SJohn Fastabend 		}
2954e5a55a89SJohn Fastabend 
2955898e5061SJiri Pirko 		err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh);
29562469ffd7SJohn Fastabend 		if (err)
29572469ffd7SJohn Fastabend 			goto out;
29582469ffd7SJohn Fastabend 
29592469ffd7SJohn Fastabend 		flags &= ~BRIDGE_FLAGS_MASTER;
29602469ffd7SJohn Fastabend 	}
29612469ffd7SJohn Fastabend 
29622469ffd7SJohn Fastabend 	if ((flags & BRIDGE_FLAGS_SELF)) {
29632469ffd7SJohn Fastabend 		if (!dev->netdev_ops->ndo_bridge_setlink)
29642469ffd7SJohn Fastabend 			err = -EOPNOTSUPP;
29652469ffd7SJohn Fastabend 		else
2966e5a55a89SJohn Fastabend 			err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh);
2967e5a55a89SJohn Fastabend 
29682469ffd7SJohn Fastabend 		if (!err)
29692469ffd7SJohn Fastabend 			flags &= ~BRIDGE_FLAGS_SELF;
29702469ffd7SJohn Fastabend 	}
29712469ffd7SJohn Fastabend 
2972c38e01b8SJohn Fastabend 	if (have_flags)
29732469ffd7SJohn Fastabend 		memcpy(nla_data(attr), &flags, sizeof(flags));
29742469ffd7SJohn Fastabend 	/* Generate event to notify upper layer of bridge change */
29752469ffd7SJohn Fastabend 	if (!err)
2976c38e01b8SJohn Fastabend 		err = rtnl_bridge_notify(dev, oflags);
2977e5a55a89SJohn Fastabend out:
2978e5a55a89SJohn Fastabend 	return err;
2979e5a55a89SJohn Fastabend }
2980e5a55a89SJohn Fastabend 
2981661d2967SThomas Graf static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
2982407af329SVlad Yasevich {
2983407af329SVlad Yasevich 	struct net *net = sock_net(skb->sk);
2984407af329SVlad Yasevich 	struct ifinfomsg *ifm;
2985407af329SVlad Yasevich 	struct net_device *dev;
2986407af329SVlad Yasevich 	struct nlattr *br_spec, *attr = NULL;
2987407af329SVlad Yasevich 	int rem, err = -EOPNOTSUPP;
2988407af329SVlad Yasevich 	u16 oflags, flags = 0;
2989407af329SVlad Yasevich 	bool have_flags = false;
2990407af329SVlad Yasevich 
2991407af329SVlad Yasevich 	if (nlmsg_len(nlh) < sizeof(*ifm))
2992407af329SVlad Yasevich 		return -EINVAL;
2993407af329SVlad Yasevich 
2994407af329SVlad Yasevich 	ifm = nlmsg_data(nlh);
2995407af329SVlad Yasevich 	if (ifm->ifi_family != AF_BRIDGE)
2996407af329SVlad Yasevich 		return -EPFNOSUPPORT;
2997407af329SVlad Yasevich 
2998407af329SVlad Yasevich 	dev = __dev_get_by_index(net, ifm->ifi_index);
2999407af329SVlad Yasevich 	if (!dev) {
3000407af329SVlad Yasevich 		pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3001407af329SVlad Yasevich 		return -ENODEV;
3002407af329SVlad Yasevich 	}
3003407af329SVlad Yasevich 
3004407af329SVlad Yasevich 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3005407af329SVlad Yasevich 	if (br_spec) {
3006407af329SVlad Yasevich 		nla_for_each_nested(attr, br_spec, rem) {
3007407af329SVlad Yasevich 			if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
30086e8d1c55SThomas Graf 				if (nla_len(attr) < sizeof(flags))
30096e8d1c55SThomas Graf 					return -EINVAL;
30106e8d1c55SThomas Graf 
3011407af329SVlad Yasevich 				have_flags = true;
3012407af329SVlad Yasevich 				flags = nla_get_u16(attr);
3013407af329SVlad Yasevich 				break;
3014407af329SVlad Yasevich 			}
3015407af329SVlad Yasevich 		}
3016407af329SVlad Yasevich 	}
3017407af329SVlad Yasevich 
3018407af329SVlad Yasevich 	oflags = flags;
3019407af329SVlad Yasevich 
3020407af329SVlad Yasevich 	if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
3021407af329SVlad Yasevich 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3022407af329SVlad Yasevich 
3023407af329SVlad Yasevich 		if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
3024407af329SVlad Yasevich 			err = -EOPNOTSUPP;
3025407af329SVlad Yasevich 			goto out;
3026407af329SVlad Yasevich 		}
3027407af329SVlad Yasevich 
3028407af329SVlad Yasevich 		err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh);
3029407af329SVlad Yasevich 		if (err)
3030407af329SVlad Yasevich 			goto out;
3031407af329SVlad Yasevich 
3032407af329SVlad Yasevich 		flags &= ~BRIDGE_FLAGS_MASTER;
3033407af329SVlad Yasevich 	}
3034407af329SVlad Yasevich 
3035407af329SVlad Yasevich 	if ((flags & BRIDGE_FLAGS_SELF)) {
3036407af329SVlad Yasevich 		if (!dev->netdev_ops->ndo_bridge_dellink)
3037407af329SVlad Yasevich 			err = -EOPNOTSUPP;
3038407af329SVlad Yasevich 		else
3039407af329SVlad Yasevich 			err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh);
3040407af329SVlad Yasevich 
3041407af329SVlad Yasevich 		if (!err)
3042407af329SVlad Yasevich 			flags &= ~BRIDGE_FLAGS_SELF;
3043407af329SVlad Yasevich 	}
3044407af329SVlad Yasevich 
3045407af329SVlad Yasevich 	if (have_flags)
3046407af329SVlad Yasevich 		memcpy(nla_data(attr), &flags, sizeof(flags));
3047407af329SVlad Yasevich 	/* Generate event to notify upper layer of bridge change */
3048407af329SVlad Yasevich 	if (!err)
3049407af329SVlad Yasevich 		err = rtnl_bridge_notify(dev, oflags);
3050407af329SVlad Yasevich out:
3051407af329SVlad Yasevich 	return err;
3052407af329SVlad Yasevich }
3053407af329SVlad Yasevich 
30541da177e4SLinus Torvalds /* Process one rtnetlink message. */
30551da177e4SLinus Torvalds 
30561d00a4ebSThomas Graf static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
30571da177e4SLinus Torvalds {
30583b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
3059e2849863SThomas Graf 	rtnl_doit_func doit;
30601da177e4SLinus Torvalds 	int sz_idx, kind;
30611da177e4SLinus Torvalds 	int family;
30621da177e4SLinus Torvalds 	int type;
30632907c35fSEric Dumazet 	int err;
30641da177e4SLinus Torvalds 
30651da177e4SLinus Torvalds 	type = nlh->nlmsg_type;
30661da177e4SLinus Torvalds 	if (type > RTM_MAX)
3067038890feSThomas Graf 		return -EOPNOTSUPP;
30681da177e4SLinus Torvalds 
30691da177e4SLinus Torvalds 	type -= RTM_BASE;
30701da177e4SLinus Torvalds 
30711da177e4SLinus Torvalds 	/* All the messages must have at least 1 byte length */
3072573ce260SHong zhi guo 	if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
30731da177e4SLinus Torvalds 		return 0;
30741da177e4SLinus Torvalds 
3075573ce260SHong zhi guo 	family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
30761da177e4SLinus Torvalds 	sz_idx = type>>2;
30771da177e4SLinus Torvalds 	kind = type&3;
30781da177e4SLinus Torvalds 
307990f62cf3SEric W. Biederman 	if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
30801d00a4ebSThomas Graf 		return -EPERM;
30811da177e4SLinus Torvalds 
3082b8f3ab42SDavid S. Miller 	if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
308397c53cacSDenis V. Lunev 		struct sock *rtnl;
3084e2849863SThomas Graf 		rtnl_dumpit_func dumpit;
3085c7ac8679SGreg Rose 		rtnl_calcit_func calcit;
3086c7ac8679SGreg Rose 		u16 min_dump_alloc = 0;
30871da177e4SLinus Torvalds 
3088e2849863SThomas Graf 		dumpit = rtnl_get_dumpit(family, type);
3089e2849863SThomas Graf 		if (dumpit == NULL)
3090038890feSThomas Graf 			return -EOPNOTSUPP;
3091c7ac8679SGreg Rose 		calcit = rtnl_get_calcit(family, type);
3092c7ac8679SGreg Rose 		if (calcit)
3093115c9b81SGreg Rose 			min_dump_alloc = calcit(skb, nlh);
30941da177e4SLinus Torvalds 
30952907c35fSEric Dumazet 		__rtnl_unlock();
309697c53cacSDenis V. Lunev 		rtnl = net->rtnl;
309780d326faSPablo Neira Ayuso 		{
309880d326faSPablo Neira Ayuso 			struct netlink_dump_control c = {
309980d326faSPablo Neira Ayuso 				.dump		= dumpit,
310080d326faSPablo Neira Ayuso 				.min_dump_alloc	= min_dump_alloc,
310180d326faSPablo Neira Ayuso 			};
310280d326faSPablo Neira Ayuso 			err = netlink_dump_start(rtnl, skb, nlh, &c);
310380d326faSPablo Neira Ayuso 		}
31042907c35fSEric Dumazet 		rtnl_lock();
31052907c35fSEric Dumazet 		return err;
31061da177e4SLinus Torvalds 	}
31071da177e4SLinus Torvalds 
3108e2849863SThomas Graf 	doit = rtnl_get_doit(family, type);
3109e2849863SThomas Graf 	if (doit == NULL)
3110038890feSThomas Graf 		return -EOPNOTSUPP;
31111da177e4SLinus Torvalds 
3112661d2967SThomas Graf 	return doit(skb, nlh);
31131da177e4SLinus Torvalds }
31141da177e4SLinus Torvalds 
3115cd40b7d3SDenis V. Lunev static void rtnetlink_rcv(struct sk_buff *skb)
31161da177e4SLinus Torvalds {
31171536cc0dSDenis V. Lunev 	rtnl_lock();
3118cd40b7d3SDenis V. Lunev 	netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
31191536cc0dSDenis V. Lunev 	rtnl_unlock();
31201da177e4SLinus Torvalds }
31211da177e4SLinus Torvalds 
31221da177e4SLinus Torvalds static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
31231da177e4SLinus Torvalds {
3124351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3125e9dc8653SEric W. Biederman 
31261da177e4SLinus Torvalds 	switch (event) {
31271da177e4SLinus Torvalds 	case NETDEV_UP:
31281da177e4SLinus Torvalds 	case NETDEV_DOWN:
312910de05afSPatrick McHardy 	case NETDEV_PRE_UP:
3130d90a909eSEric W. Biederman 	case NETDEV_POST_INIT:
3131d90a909eSEric W. Biederman 	case NETDEV_REGISTER:
31321da177e4SLinus Torvalds 	case NETDEV_CHANGE:
3133755d0e77SPatrick McHardy 	case NETDEV_PRE_TYPE_CHANGE:
31341da177e4SLinus Torvalds 	case NETDEV_GOING_DOWN:
3135a2835763SPatrick McHardy 	case NETDEV_UNREGISTER:
31360115e8e3SEric Dumazet 	case NETDEV_UNREGISTER_FINAL:
3137ac3d3f81SAmerigo Wang 	case NETDEV_RELEASE:
3138ac3d3f81SAmerigo Wang 	case NETDEV_JOIN:
31391da177e4SLinus Torvalds 		break;
31401da177e4SLinus Torvalds 	default:
31417f294054SAlexei Starovoitov 		rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
31421da177e4SLinus Torvalds 		break;
31431da177e4SLinus Torvalds 	}
31441da177e4SLinus Torvalds 	return NOTIFY_DONE;
31451da177e4SLinus Torvalds }
31461da177e4SLinus Torvalds 
31471da177e4SLinus Torvalds static struct notifier_block rtnetlink_dev_notifier = {
31481da177e4SLinus Torvalds 	.notifier_call	= rtnetlink_event,
31491da177e4SLinus Torvalds };
31501da177e4SLinus Torvalds 
315197c53cacSDenis V. Lunev 
31522c8c1e72SAlexey Dobriyan static int __net_init rtnetlink_net_init(struct net *net)
315397c53cacSDenis V. Lunev {
315497c53cacSDenis V. Lunev 	struct sock *sk;
3155a31f2d17SPablo Neira Ayuso 	struct netlink_kernel_cfg cfg = {
3156a31f2d17SPablo Neira Ayuso 		.groups		= RTNLGRP_MAX,
3157a31f2d17SPablo Neira Ayuso 		.input		= rtnetlink_rcv,
3158a31f2d17SPablo Neira Ayuso 		.cb_mutex	= &rtnl_mutex,
31599785e10aSPablo Neira Ayuso 		.flags		= NL_CFG_F_NONROOT_RECV,
3160a31f2d17SPablo Neira Ayuso 	};
3161a31f2d17SPablo Neira Ayuso 
31629f00d977SPablo Neira Ayuso 	sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
316397c53cacSDenis V. Lunev 	if (!sk)
316497c53cacSDenis V. Lunev 		return -ENOMEM;
316597c53cacSDenis V. Lunev 	net->rtnl = sk;
316697c53cacSDenis V. Lunev 	return 0;
316797c53cacSDenis V. Lunev }
316897c53cacSDenis V. Lunev 
31692c8c1e72SAlexey Dobriyan static void __net_exit rtnetlink_net_exit(struct net *net)
317097c53cacSDenis V. Lunev {
3171b7c6ba6eSDenis V. Lunev 	netlink_kernel_release(net->rtnl);
317297c53cacSDenis V. Lunev 	net->rtnl = NULL;
317397c53cacSDenis V. Lunev }
317497c53cacSDenis V. Lunev 
317597c53cacSDenis V. Lunev static struct pernet_operations rtnetlink_net_ops = {
317697c53cacSDenis V. Lunev 	.init = rtnetlink_net_init,
317797c53cacSDenis V. Lunev 	.exit = rtnetlink_net_exit,
317897c53cacSDenis V. Lunev };
317997c53cacSDenis V. Lunev 
31801da177e4SLinus Torvalds void __init rtnetlink_init(void)
31811da177e4SLinus Torvalds {
318297c53cacSDenis V. Lunev 	if (register_pernet_subsys(&rtnetlink_net_ops))
31831da177e4SLinus Torvalds 		panic("rtnetlink_init: cannot initialize rtnetlink\n");
318497c53cacSDenis V. Lunev 
31851da177e4SLinus Torvalds 	register_netdevice_notifier(&rtnetlink_dev_notifier);
3186340d17fcSThomas Graf 
3187c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
3188c7ac8679SGreg Rose 		      rtnl_dump_ifinfo, rtnl_calcit);
3189c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL);
3190c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL);
3191c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL);
3192687ad8ccSThomas Graf 
3193c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL);
3194c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL);
319577162022SJohn Fastabend 
319677162022SJohn Fastabend 	rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, NULL);
319777162022SJohn Fastabend 	rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, NULL);
319877162022SJohn Fastabend 	rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, NULL);
3199e5a55a89SJohn Fastabend 
3200e5a55a89SJohn Fastabend 	rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, NULL);
3201407af329SVlad Yasevich 	rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, NULL);
3202e5a55a89SJohn Fastabend 	rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, NULL);
32031da177e4SLinus Torvalds }
32041da177e4SLinus Torvalds 
3205