xref: /openbmc/linux/net/core/rtnetlink.c (revision 82f2841291cfaf4d225aa1766424280254d3e3b2)
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>
47*82f28412SJiri 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;
370200b916fSCong Wang 	DEFINE_WAIT(wait);
371200b916fSCong Wang 
372200b916fSCong Wang 	for (;;) {
373200b916fSCong Wang 		prepare_to_wait(&netdev_unregistering_wq, &wait,
374200b916fSCong Wang 				TASK_UNINTERRUPTIBLE);
375200b916fSCong Wang 		unregistering = false;
376200b916fSCong Wang 		rtnl_lock();
377200b916fSCong Wang 		for_each_net(net) {
378200b916fSCong Wang 			if (net->dev_unreg_count > 0) {
379200b916fSCong Wang 				unregistering = true;
380200b916fSCong Wang 				break;
381200b916fSCong Wang 			}
382200b916fSCong Wang 		}
383200b916fSCong Wang 		if (!unregistering)
384200b916fSCong Wang 			break;
385200b916fSCong Wang 		__rtnl_unlock();
386200b916fSCong Wang 		schedule();
387200b916fSCong Wang 	}
388200b916fSCong Wang 	finish_wait(&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 */
873*82f28412SJiri Pirko 	       + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
874*82f28412SJiri 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 
973*82f28412SJiri Pirko static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
974*82f28412SJiri Pirko {
975*82f28412SJiri Pirko 	int err;
976*82f28412SJiri Pirko 	struct netdev_phys_item_id psid;
977*82f28412SJiri Pirko 
978*82f28412SJiri Pirko 	err = netdev_switch_parent_id_get(dev, &psid);
979*82f28412SJiri Pirko 	if (err) {
980*82f28412SJiri Pirko 		if (err == -EOPNOTSUPP)
981*82f28412SJiri Pirko 			return 0;
982*82f28412SJiri Pirko 		return err;
983*82f28412SJiri Pirko 	}
984*82f28412SJiri Pirko 
985*82f28412SJiri Pirko 	if (nla_put(skb, IFLA_PHYS_SWITCH_ID, psid.id_len, psid.id))
986*82f28412SJiri Pirko 		return -EMSGSIZE;
987*82f28412SJiri Pirko 
988*82f28412SJiri Pirko 	return 0;
989*82f28412SJiri Pirko }
990*82f28412SJiri 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 
1063*82f28412SJiri Pirko 	if (rtnl_phys_switch_id_fill(skb, dev))
1064*82f28412SJiri Pirko 		goto nla_put_failure;
1065*82f28412SJiri 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 */
1225*82f28412SJiri 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)) {
1526b51642f6SEric W. Biederman 			err = -EPERM;
1527b51642f6SEric W. Biederman 			goto errout;
1528b51642f6SEric W. Biederman 		}
1529d8a5ec67SEric W. Biederman 		err = dev_change_net_namespace(dev, net, ifname);
1530d8a5ec67SEric W. Biederman 		put_net(net);
1531d8a5ec67SEric W. Biederman 		if (err)
1532d8a5ec67SEric W. Biederman 			goto errout;
153390c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
1534d8a5ec67SEric W. Biederman 	}
1535d8a5ec67SEric W. Biederman 
15360157f60cSPatrick McHardy 	if (tb[IFLA_MAP]) {
15370157f60cSPatrick McHardy 		struct rtnl_link_ifmap *u_map;
15380157f60cSPatrick McHardy 		struct ifmap k_map;
15390157f60cSPatrick McHardy 
1540d314774cSStephen Hemminger 		if (!ops->ndo_set_config) {
15410157f60cSPatrick McHardy 			err = -EOPNOTSUPP;
15420157f60cSPatrick McHardy 			goto errout;
15430157f60cSPatrick McHardy 		}
15440157f60cSPatrick McHardy 
15450157f60cSPatrick McHardy 		if (!netif_device_present(dev)) {
15460157f60cSPatrick McHardy 			err = -ENODEV;
15470157f60cSPatrick McHardy 			goto errout;
15480157f60cSPatrick McHardy 		}
15490157f60cSPatrick McHardy 
15500157f60cSPatrick McHardy 		u_map = nla_data(tb[IFLA_MAP]);
15510157f60cSPatrick McHardy 		k_map.mem_start = (unsigned long) u_map->mem_start;
15520157f60cSPatrick McHardy 		k_map.mem_end = (unsigned long) u_map->mem_end;
15530157f60cSPatrick McHardy 		k_map.base_addr = (unsigned short) u_map->base_addr;
15540157f60cSPatrick McHardy 		k_map.irq = (unsigned char) u_map->irq;
15550157f60cSPatrick McHardy 		k_map.dma = (unsigned char) u_map->dma;
15560157f60cSPatrick McHardy 		k_map.port = (unsigned char) u_map->port;
15570157f60cSPatrick McHardy 
1558d314774cSStephen Hemminger 		err = ops->ndo_set_config(dev, &k_map);
15590157f60cSPatrick McHardy 		if (err < 0)
15600157f60cSPatrick McHardy 			goto errout;
15610157f60cSPatrick McHardy 
1562ba998906SNicolas Dichtel 		status |= DO_SETLINK_NOTIFY;
15630157f60cSPatrick McHardy 	}
15640157f60cSPatrick McHardy 
15650157f60cSPatrick McHardy 	if (tb[IFLA_ADDRESS]) {
15660157f60cSPatrick McHardy 		struct sockaddr *sa;
15670157f60cSPatrick McHardy 		int len;
15680157f60cSPatrick McHardy 
15690157f60cSPatrick McHardy 		len = sizeof(sa_family_t) + dev->addr_len;
15700157f60cSPatrick McHardy 		sa = kmalloc(len, GFP_KERNEL);
15710157f60cSPatrick McHardy 		if (!sa) {
15720157f60cSPatrick McHardy 			err = -ENOMEM;
15730157f60cSPatrick McHardy 			goto errout;
15740157f60cSPatrick McHardy 		}
15750157f60cSPatrick McHardy 		sa->sa_family = dev->type;
15760157f60cSPatrick McHardy 		memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
15770157f60cSPatrick McHardy 		       dev->addr_len);
1578e7c3273eSJiri Pirko 		err = dev_set_mac_address(dev, sa);
15790157f60cSPatrick McHardy 		kfree(sa);
15800157f60cSPatrick McHardy 		if (err)
15810157f60cSPatrick McHardy 			goto errout;
158290c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
15830157f60cSPatrick McHardy 	}
15840157f60cSPatrick McHardy 
15850157f60cSPatrick McHardy 	if (tb[IFLA_MTU]) {
15860157f60cSPatrick McHardy 		err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
15870157f60cSPatrick McHardy 		if (err < 0)
15880157f60cSPatrick McHardy 			goto errout;
158990c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
15900157f60cSPatrick McHardy 	}
15910157f60cSPatrick McHardy 
1592cbda10faSVlad Dogaru 	if (tb[IFLA_GROUP]) {
1593cbda10faSVlad Dogaru 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
1594ba998906SNicolas Dichtel 		status |= DO_SETLINK_NOTIFY;
1595cbda10faSVlad Dogaru 	}
1596cbda10faSVlad Dogaru 
15970157f60cSPatrick McHardy 	/*
15980157f60cSPatrick McHardy 	 * Interface selected by interface index but interface
15990157f60cSPatrick McHardy 	 * name provided implies that a name change has been
16000157f60cSPatrick McHardy 	 * requested.
16010157f60cSPatrick McHardy 	 */
16020157f60cSPatrick McHardy 	if (ifm->ifi_index > 0 && ifname[0]) {
16030157f60cSPatrick McHardy 		err = dev_change_name(dev, ifname);
16040157f60cSPatrick McHardy 		if (err < 0)
16050157f60cSPatrick McHardy 			goto errout;
160690c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
16070157f60cSPatrick McHardy 	}
16080157f60cSPatrick McHardy 
16090b815a1aSStephen Hemminger 	if (tb[IFLA_IFALIAS]) {
16100b815a1aSStephen Hemminger 		err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
16110b815a1aSStephen Hemminger 				    nla_len(tb[IFLA_IFALIAS]));
16120b815a1aSStephen Hemminger 		if (err < 0)
16130b815a1aSStephen Hemminger 			goto errout;
1614ba998906SNicolas Dichtel 		status |= DO_SETLINK_NOTIFY;
16150b815a1aSStephen Hemminger 	}
16160b815a1aSStephen Hemminger 
16170157f60cSPatrick McHardy 	if (tb[IFLA_BROADCAST]) {
16180157f60cSPatrick McHardy 		nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
1619e7c3273eSJiri Pirko 		call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
16200157f60cSPatrick McHardy 	}
16210157f60cSPatrick McHardy 
16220157f60cSPatrick McHardy 	if (ifm->ifi_flags || ifm->ifi_change) {
16233729d502SPatrick McHardy 		err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
16245f9021cfSJohannes Berg 		if (err < 0)
16255f9021cfSJohannes Berg 			goto errout;
16260157f60cSPatrick McHardy 	}
16270157f60cSPatrick McHardy 
1628fbaec0eaSJiri Pirko 	if (tb[IFLA_MASTER]) {
1629fbaec0eaSJiri Pirko 		err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
1630fbaec0eaSJiri Pirko 		if (err)
1631fbaec0eaSJiri Pirko 			goto errout;
163290c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
1633fbaec0eaSJiri Pirko 	}
1634fbaec0eaSJiri Pirko 
16359a57247fSJiri Pirko 	if (tb[IFLA_CARRIER]) {
16369a57247fSJiri Pirko 		err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
16379a57247fSJiri Pirko 		if (err)
16389a57247fSJiri Pirko 			goto errout;
163990c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
16409a57247fSJiri Pirko 	}
16419a57247fSJiri Pirko 
16425d1180fcSNicolas Dichtel 	if (tb[IFLA_TXQLEN]) {
16435d1180fcSNicolas Dichtel 		unsigned long value = nla_get_u32(tb[IFLA_TXQLEN]);
16445d1180fcSNicolas Dichtel 
16455d1180fcSNicolas Dichtel 		if (dev->tx_queue_len ^ value)
1646ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
16475d1180fcSNicolas Dichtel 
16485d1180fcSNicolas Dichtel 		dev->tx_queue_len = value;
16495d1180fcSNicolas Dichtel 	}
16500157f60cSPatrick McHardy 
16510157f60cSPatrick McHardy 	if (tb[IFLA_OPERSTATE])
165293b2d4a2SDavid S. Miller 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
16530157f60cSPatrick McHardy 
16540157f60cSPatrick McHardy 	if (tb[IFLA_LINKMODE]) {
16551889b0e7SNicolas Dichtel 		unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
16561889b0e7SNicolas Dichtel 
16570157f60cSPatrick McHardy 		write_lock_bh(&dev_base_lock);
16581889b0e7SNicolas Dichtel 		if (dev->link_mode ^ value)
1659ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
16601889b0e7SNicolas Dichtel 		dev->link_mode = value;
166193b2d4a2SDavid S. Miller 		write_unlock_bh(&dev_base_lock);
16620157f60cSPatrick McHardy 	}
16630157f60cSPatrick McHardy 
1664c02db8c6SChris Wright 	if (tb[IFLA_VFINFO_LIST]) {
1665c02db8c6SChris Wright 		struct nlattr *attr;
1666c02db8c6SChris Wright 		int rem;
1667c02db8c6SChris Wright 		nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
1668253683bbSDavid Howells 			if (nla_type(attr) != IFLA_VF_INFO) {
1669253683bbSDavid Howells 				err = -EINVAL;
1670c02db8c6SChris Wright 				goto errout;
1671253683bbSDavid Howells 			}
1672c02db8c6SChris Wright 			err = do_setvfinfo(dev, attr);
1673ebc08a6fSWilliams, Mitch A 			if (err < 0)
1674ebc08a6fSWilliams, Mitch A 				goto errout;
1675ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
1676ebc08a6fSWilliams, Mitch A 		}
1677ebc08a6fSWilliams, Mitch A 	}
16780157f60cSPatrick McHardy 	err = 0;
16790157f60cSPatrick McHardy 
168057b61080SScott Feldman 	if (tb[IFLA_VF_PORTS]) {
168157b61080SScott Feldman 		struct nlattr *port[IFLA_PORT_MAX+1];
168257b61080SScott Feldman 		struct nlattr *attr;
168357b61080SScott Feldman 		int vf;
168457b61080SScott Feldman 		int rem;
168557b61080SScott Feldman 
168657b61080SScott Feldman 		err = -EOPNOTSUPP;
168757b61080SScott Feldman 		if (!ops->ndo_set_vf_port)
168857b61080SScott Feldman 			goto errout;
168957b61080SScott Feldman 
169057b61080SScott Feldman 		nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
169157b61080SScott Feldman 			if (nla_type(attr) != IFLA_VF_PORT)
169257b61080SScott Feldman 				continue;
169357b61080SScott Feldman 			err = nla_parse_nested(port, IFLA_PORT_MAX,
169457b61080SScott Feldman 				attr, ifla_port_policy);
169557b61080SScott Feldman 			if (err < 0)
169657b61080SScott Feldman 				goto errout;
169757b61080SScott Feldman 			if (!port[IFLA_PORT_VF]) {
169857b61080SScott Feldman 				err = -EOPNOTSUPP;
169957b61080SScott Feldman 				goto errout;
170057b61080SScott Feldman 			}
170157b61080SScott Feldman 			vf = nla_get_u32(port[IFLA_PORT_VF]);
170257b61080SScott Feldman 			err = ops->ndo_set_vf_port(dev, vf, port);
170357b61080SScott Feldman 			if (err < 0)
170457b61080SScott Feldman 				goto errout;
1705ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
170657b61080SScott Feldman 		}
170757b61080SScott Feldman 	}
170857b61080SScott Feldman 	err = 0;
170957b61080SScott Feldman 
171057b61080SScott Feldman 	if (tb[IFLA_PORT_SELF]) {
171157b61080SScott Feldman 		struct nlattr *port[IFLA_PORT_MAX+1];
171257b61080SScott Feldman 
171357b61080SScott Feldman 		err = nla_parse_nested(port, IFLA_PORT_MAX,
171457b61080SScott Feldman 			tb[IFLA_PORT_SELF], ifla_port_policy);
171557b61080SScott Feldman 		if (err < 0)
171657b61080SScott Feldman 			goto errout;
171757b61080SScott Feldman 
171857b61080SScott Feldman 		err = -EOPNOTSUPP;
171957b61080SScott Feldman 		if (ops->ndo_set_vf_port)
172057b61080SScott Feldman 			err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
172157b61080SScott Feldman 		if (err < 0)
172257b61080SScott Feldman 			goto errout;
1723ba998906SNicolas Dichtel 		status |= DO_SETLINK_NOTIFY;
172457b61080SScott Feldman 	}
1725f8ff182cSThomas Graf 
1726f8ff182cSThomas Graf 	if (tb[IFLA_AF_SPEC]) {
1727f8ff182cSThomas Graf 		struct nlattr *af;
1728f8ff182cSThomas Graf 		int rem;
1729f8ff182cSThomas Graf 
1730f8ff182cSThomas Graf 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1731f8ff182cSThomas Graf 			const struct rtnl_af_ops *af_ops;
1732f8ff182cSThomas Graf 
1733f8ff182cSThomas Graf 			if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1734cf7afbfeSThomas Graf 				BUG();
1735f8ff182cSThomas Graf 
1736cf7afbfeSThomas Graf 			err = af_ops->set_link_af(dev, af);
1737f8ff182cSThomas Graf 			if (err < 0)
1738f8ff182cSThomas Graf 				goto errout;
1739f8ff182cSThomas Graf 
1740ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
1741f8ff182cSThomas Graf 		}
1742f8ff182cSThomas Graf 	}
174357b61080SScott Feldman 	err = 0;
174457b61080SScott Feldman 
17450157f60cSPatrick McHardy errout:
1746ba998906SNicolas Dichtel 	if (status & DO_SETLINK_MODIFIED) {
1747ba998906SNicolas Dichtel 		if (status & DO_SETLINK_NOTIFY)
1748ba998906SNicolas Dichtel 			netdev_state_change(dev);
1749ba998906SNicolas Dichtel 
1750ba998906SNicolas Dichtel 		if (err < 0)
1751e87cc472SJoe 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",
1752e87cc472SJoe Perches 					     dev->name);
1753ba998906SNicolas Dichtel 	}
17540157f60cSPatrick McHardy 
17550157f60cSPatrick McHardy 	return err;
17560157f60cSPatrick McHardy }
17570157f60cSPatrick McHardy 
1758661d2967SThomas Graf static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
1759da5e0494SThomas Graf {
17603b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
1761da5e0494SThomas Graf 	struct ifinfomsg *ifm;
1762da5e0494SThomas Graf 	struct net_device *dev;
17630157f60cSPatrick McHardy 	int err;
1764da5e0494SThomas Graf 	struct nlattr *tb[IFLA_MAX+1];
17651da177e4SLinus Torvalds 	char ifname[IFNAMSIZ];
17661da177e4SLinus Torvalds 
1767da5e0494SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1768da5e0494SThomas Graf 	if (err < 0)
1769da5e0494SThomas Graf 		goto errout;
17701da177e4SLinus Torvalds 
17715176f91eSThomas Graf 	if (tb[IFLA_IFNAME])
17725176f91eSThomas Graf 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
177378e5b891SPatrick McHardy 	else
177478e5b891SPatrick McHardy 		ifname[0] = '\0';
17751da177e4SLinus Torvalds 
17761da177e4SLinus Torvalds 	err = -EINVAL;
1777da5e0494SThomas Graf 	ifm = nlmsg_data(nlh);
177851055be8SPatrick McHardy 	if (ifm->ifi_index > 0)
1779a3d12891SEric Dumazet 		dev = __dev_get_by_index(net, ifm->ifi_index);
1780da5e0494SThomas Graf 	else if (tb[IFLA_IFNAME])
1781a3d12891SEric Dumazet 		dev = __dev_get_by_name(net, ifname);
1782da5e0494SThomas Graf 	else
1783da5e0494SThomas Graf 		goto errout;
17841da177e4SLinus Torvalds 
1785da5e0494SThomas Graf 	if (dev == NULL) {
1786da5e0494SThomas Graf 		err = -ENODEV;
1787da5e0494SThomas Graf 		goto errout;
1788da5e0494SThomas Graf 	}
17891da177e4SLinus Torvalds 
1790e0d087afSEric Dumazet 	err = validate_linkmsg(dev, tb);
1791e0d087afSEric Dumazet 	if (err < 0)
1792a3d12891SEric Dumazet 		goto errout;
1793da5e0494SThomas Graf 
179490f62cf3SEric W. Biederman 	err = do_setlink(skb, dev, ifm, tb, ifname, 0);
1795da5e0494SThomas Graf errout:
17961da177e4SLinus Torvalds 	return err;
17971da177e4SLinus Torvalds }
17981da177e4SLinus Torvalds 
1799661d2967SThomas Graf static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
180038f7b870SPatrick McHardy {
18013b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
180238f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops;
180338f7b870SPatrick McHardy 	struct net_device *dev;
180438f7b870SPatrick McHardy 	struct ifinfomsg *ifm;
180538f7b870SPatrick McHardy 	char ifname[IFNAMSIZ];
180638f7b870SPatrick McHardy 	struct nlattr *tb[IFLA_MAX+1];
180738f7b870SPatrick McHardy 	int err;
1808226bd341SEric Dumazet 	LIST_HEAD(list_kill);
180938f7b870SPatrick McHardy 
181038f7b870SPatrick McHardy 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
181138f7b870SPatrick McHardy 	if (err < 0)
181238f7b870SPatrick McHardy 		return err;
181338f7b870SPatrick McHardy 
181438f7b870SPatrick McHardy 	if (tb[IFLA_IFNAME])
181538f7b870SPatrick McHardy 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
181638f7b870SPatrick McHardy 
181738f7b870SPatrick McHardy 	ifm = nlmsg_data(nlh);
181838f7b870SPatrick McHardy 	if (ifm->ifi_index > 0)
1819881d966bSEric W. Biederman 		dev = __dev_get_by_index(net, ifm->ifi_index);
182038f7b870SPatrick McHardy 	else if (tb[IFLA_IFNAME])
1821881d966bSEric W. Biederman 		dev = __dev_get_by_name(net, ifname);
182238f7b870SPatrick McHardy 	else
182338f7b870SPatrick McHardy 		return -EINVAL;
182438f7b870SPatrick McHardy 
182538f7b870SPatrick McHardy 	if (!dev)
182638f7b870SPatrick McHardy 		return -ENODEV;
182738f7b870SPatrick McHardy 
182838f7b870SPatrick McHardy 	ops = dev->rtnl_link_ops;
1829b0ab2fabSJiri Pirko 	if (!ops || !ops->dellink)
183038f7b870SPatrick McHardy 		return -EOPNOTSUPP;
183138f7b870SPatrick McHardy 
1832226bd341SEric Dumazet 	ops->dellink(dev, &list_kill);
1833226bd341SEric Dumazet 	unregister_netdevice_many(&list_kill);
183438f7b870SPatrick McHardy 	return 0;
183538f7b870SPatrick McHardy }
183638f7b870SPatrick McHardy 
18373729d502SPatrick McHardy int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
18383729d502SPatrick McHardy {
18393729d502SPatrick McHardy 	unsigned int old_flags;
18403729d502SPatrick McHardy 	int err;
18413729d502SPatrick McHardy 
18423729d502SPatrick McHardy 	old_flags = dev->flags;
18433729d502SPatrick McHardy 	if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
18443729d502SPatrick McHardy 		err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
18453729d502SPatrick McHardy 		if (err < 0)
18463729d502SPatrick McHardy 			return err;
18473729d502SPatrick McHardy 	}
18483729d502SPatrick McHardy 
18493729d502SPatrick McHardy 	dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
18503729d502SPatrick McHardy 
1851a528c219SNicolas Dichtel 	__dev_notify_flags(dev, old_flags, ~0U);
18523729d502SPatrick McHardy 	return 0;
18533729d502SPatrick McHardy }
18543729d502SPatrick McHardy EXPORT_SYMBOL(rtnl_configure_link);
18553729d502SPatrick McHardy 
1856c0713563SRami Rosen struct net_device *rtnl_create_link(struct net *net,
18575517750fSTom Gundersen 	char *ifname, unsigned char name_assign_type,
18585517750fSTom Gundersen 	const struct rtnl_link_ops *ops, struct nlattr *tb[])
1859e7199288SPavel Emelianov {
1860e7199288SPavel Emelianov 	int err;
1861e7199288SPavel Emelianov 	struct net_device *dev;
1862d40156aaSJiri Pirko 	unsigned int num_tx_queues = 1;
1863d40156aaSJiri Pirko 	unsigned int num_rx_queues = 1;
1864e7199288SPavel Emelianov 
186576ff5cc9SJiri Pirko 	if (tb[IFLA_NUM_TX_QUEUES])
186676ff5cc9SJiri Pirko 		num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
186776ff5cc9SJiri Pirko 	else if (ops->get_num_tx_queues)
1868d40156aaSJiri Pirko 		num_tx_queues = ops->get_num_tx_queues();
186976ff5cc9SJiri Pirko 
187076ff5cc9SJiri Pirko 	if (tb[IFLA_NUM_RX_QUEUES])
187176ff5cc9SJiri Pirko 		num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
187276ff5cc9SJiri Pirko 	else if (ops->get_num_rx_queues)
1873d40156aaSJiri Pirko 		num_rx_queues = ops->get_num_rx_queues();
1874efacb309Sstephen hemminger 
1875e7199288SPavel Emelianov 	err = -ENOMEM;
18765517750fSTom Gundersen 	dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
1877c835a677STom Gundersen 			       ops->setup, num_tx_queues, num_rx_queues);
1878e7199288SPavel Emelianov 	if (!dev)
1879e7199288SPavel Emelianov 		goto err;
1880e7199288SPavel Emelianov 
188181adee47SEric W. Biederman 	dev_net_set(dev, net);
188281adee47SEric W. Biederman 	dev->rtnl_link_ops = ops;
18833729d502SPatrick McHardy 	dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
188481adee47SEric W. Biederman 
1885e7199288SPavel Emelianov 	if (tb[IFLA_MTU])
1886e7199288SPavel Emelianov 		dev->mtu = nla_get_u32(tb[IFLA_MTU]);
18872afb9b53SJiri Pirko 	if (tb[IFLA_ADDRESS]) {
1888e7199288SPavel Emelianov 		memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
1889e7199288SPavel Emelianov 				nla_len(tb[IFLA_ADDRESS]));
18902afb9b53SJiri Pirko 		dev->addr_assign_type = NET_ADDR_SET;
18912afb9b53SJiri Pirko 	}
1892e7199288SPavel Emelianov 	if (tb[IFLA_BROADCAST])
1893e7199288SPavel Emelianov 		memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
1894e7199288SPavel Emelianov 				nla_len(tb[IFLA_BROADCAST]));
1895e7199288SPavel Emelianov 	if (tb[IFLA_TXQLEN])
1896e7199288SPavel Emelianov 		dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
1897e7199288SPavel Emelianov 	if (tb[IFLA_OPERSTATE])
189893b2d4a2SDavid S. Miller 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
1899e7199288SPavel Emelianov 	if (tb[IFLA_LINKMODE])
1900e7199288SPavel Emelianov 		dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
1901ffa934f1SPatrick McHardy 	if (tb[IFLA_GROUP])
1902ffa934f1SPatrick McHardy 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
1903e7199288SPavel Emelianov 
1904e7199288SPavel Emelianov 	return dev;
1905e7199288SPavel Emelianov 
1906e7199288SPavel Emelianov err:
1907e7199288SPavel Emelianov 	return ERR_PTR(err);
1908e7199288SPavel Emelianov }
1909e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_create_link);
1910e7199288SPavel Emelianov 
191190f62cf3SEric W. Biederman static int rtnl_group_changelink(const struct sk_buff *skb,
191290f62cf3SEric W. Biederman 		struct net *net, int group,
1913e7ed828fSVlad Dogaru 		struct ifinfomsg *ifm,
1914e7ed828fSVlad Dogaru 		struct nlattr **tb)
1915e7ed828fSVlad Dogaru {
1916e7ed828fSVlad Dogaru 	struct net_device *dev;
1917e7ed828fSVlad Dogaru 	int err;
1918e7ed828fSVlad Dogaru 
1919e7ed828fSVlad Dogaru 	for_each_netdev(net, dev) {
1920e7ed828fSVlad Dogaru 		if (dev->group == group) {
192190f62cf3SEric W. Biederman 			err = do_setlink(skb, dev, ifm, tb, NULL, 0);
1922e7ed828fSVlad Dogaru 			if (err < 0)
1923e7ed828fSVlad Dogaru 				return err;
1924e7ed828fSVlad Dogaru 		}
1925e7ed828fSVlad Dogaru 	}
1926e7ed828fSVlad Dogaru 
1927e7ed828fSVlad Dogaru 	return 0;
1928e7ed828fSVlad Dogaru }
1929e7ed828fSVlad Dogaru 
1930661d2967SThomas Graf static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh)
193138f7b870SPatrick McHardy {
19323b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
193338f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops;
1934ba7d49b1SJiri Pirko 	const struct rtnl_link_ops *m_ops = NULL;
193538f7b870SPatrick McHardy 	struct net_device *dev;
1936ba7d49b1SJiri Pirko 	struct net_device *master_dev = NULL;
193738f7b870SPatrick McHardy 	struct ifinfomsg *ifm;
193838f7b870SPatrick McHardy 	char kind[MODULE_NAME_LEN];
193938f7b870SPatrick McHardy 	char ifname[IFNAMSIZ];
194038f7b870SPatrick McHardy 	struct nlattr *tb[IFLA_MAX+1];
194138f7b870SPatrick McHardy 	struct nlattr *linkinfo[IFLA_INFO_MAX+1];
19425517750fSTom Gundersen 	unsigned char name_assign_type = NET_NAME_USER;
194338f7b870SPatrick McHardy 	int err;
194438f7b870SPatrick McHardy 
194595a5afcaSJohannes Berg #ifdef CONFIG_MODULES
194638f7b870SPatrick McHardy replay:
19478072f085SThomas Graf #endif
194838f7b870SPatrick McHardy 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
194938f7b870SPatrick McHardy 	if (err < 0)
195038f7b870SPatrick McHardy 		return err;
195138f7b870SPatrick McHardy 
195238f7b870SPatrick McHardy 	if (tb[IFLA_IFNAME])
195338f7b870SPatrick McHardy 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
195438f7b870SPatrick McHardy 	else
195538f7b870SPatrick McHardy 		ifname[0] = '\0';
195638f7b870SPatrick McHardy 
195738f7b870SPatrick McHardy 	ifm = nlmsg_data(nlh);
195838f7b870SPatrick McHardy 	if (ifm->ifi_index > 0)
1959881d966bSEric W. Biederman 		dev = __dev_get_by_index(net, ifm->ifi_index);
1960e7ed828fSVlad Dogaru 	else {
1961e7ed828fSVlad Dogaru 		if (ifname[0])
1962881d966bSEric W. Biederman 			dev = __dev_get_by_name(net, ifname);
196338f7b870SPatrick McHardy 		else
196438f7b870SPatrick McHardy 			dev = NULL;
1965e7ed828fSVlad Dogaru 	}
196638f7b870SPatrick McHardy 
1967ba7d49b1SJiri Pirko 	if (dev) {
1968ba7d49b1SJiri Pirko 		master_dev = netdev_master_upper_dev_get(dev);
1969ba7d49b1SJiri Pirko 		if (master_dev)
1970ba7d49b1SJiri Pirko 			m_ops = master_dev->rtnl_link_ops;
1971ba7d49b1SJiri Pirko 	}
1972ba7d49b1SJiri Pirko 
1973e0d087afSEric Dumazet 	err = validate_linkmsg(dev, tb);
1974e0d087afSEric Dumazet 	if (err < 0)
19751840bb13SThomas Graf 		return err;
19761840bb13SThomas Graf 
197738f7b870SPatrick McHardy 	if (tb[IFLA_LINKINFO]) {
197838f7b870SPatrick McHardy 		err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
197938f7b870SPatrick McHardy 				       tb[IFLA_LINKINFO], ifla_info_policy);
198038f7b870SPatrick McHardy 		if (err < 0)
198138f7b870SPatrick McHardy 			return err;
198238f7b870SPatrick McHardy 	} else
198338f7b870SPatrick McHardy 		memset(linkinfo, 0, sizeof(linkinfo));
198438f7b870SPatrick McHardy 
198538f7b870SPatrick McHardy 	if (linkinfo[IFLA_INFO_KIND]) {
198638f7b870SPatrick McHardy 		nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
198738f7b870SPatrick McHardy 		ops = rtnl_link_ops_get(kind);
198838f7b870SPatrick McHardy 	} else {
198938f7b870SPatrick McHardy 		kind[0] = '\0';
199038f7b870SPatrick McHardy 		ops = NULL;
199138f7b870SPatrick McHardy 	}
199238f7b870SPatrick McHardy 
199338f7b870SPatrick McHardy 	if (1) {
1994ba7d49b1SJiri Pirko 		struct nlattr *attr[ops ? ops->maxtype + 1 : 0];
1995ba7d49b1SJiri Pirko 		struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 0];
1996ba7d49b1SJiri Pirko 		struct nlattr **data = NULL;
1997ba7d49b1SJiri Pirko 		struct nlattr **slave_data = NULL;
199881adee47SEric W. Biederman 		struct net *dest_net;
199938f7b870SPatrick McHardy 
200038f7b870SPatrick McHardy 		if (ops) {
200138f7b870SPatrick McHardy 			if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
200238f7b870SPatrick McHardy 				err = nla_parse_nested(attr, ops->maxtype,
200338f7b870SPatrick McHardy 						       linkinfo[IFLA_INFO_DATA],
200438f7b870SPatrick McHardy 						       ops->policy);
200538f7b870SPatrick McHardy 				if (err < 0)
200638f7b870SPatrick McHardy 					return err;
200738f7b870SPatrick McHardy 				data = attr;
200838f7b870SPatrick McHardy 			}
200938f7b870SPatrick McHardy 			if (ops->validate) {
201038f7b870SPatrick McHardy 				err = ops->validate(tb, data);
201138f7b870SPatrick McHardy 				if (err < 0)
201238f7b870SPatrick McHardy 					return err;
201338f7b870SPatrick McHardy 			}
201438f7b870SPatrick McHardy 		}
201538f7b870SPatrick McHardy 
2016ba7d49b1SJiri Pirko 		if (m_ops) {
2017ba7d49b1SJiri Pirko 			if (m_ops->slave_maxtype &&
2018ba7d49b1SJiri Pirko 			    linkinfo[IFLA_INFO_SLAVE_DATA]) {
2019ba7d49b1SJiri Pirko 				err = nla_parse_nested(slave_attr,
2020ba7d49b1SJiri Pirko 						       m_ops->slave_maxtype,
2021ba7d49b1SJiri Pirko 						       linkinfo[IFLA_INFO_SLAVE_DATA],
2022ba7d49b1SJiri Pirko 						       m_ops->slave_policy);
2023ba7d49b1SJiri Pirko 				if (err < 0)
2024ba7d49b1SJiri Pirko 					return err;
2025ba7d49b1SJiri Pirko 				slave_data = slave_attr;
2026ba7d49b1SJiri Pirko 			}
2027ba7d49b1SJiri Pirko 			if (m_ops->slave_validate) {
2028ba7d49b1SJiri Pirko 				err = m_ops->slave_validate(tb, slave_data);
2029ba7d49b1SJiri Pirko 				if (err < 0)
2030ba7d49b1SJiri Pirko 					return err;
2031ba7d49b1SJiri Pirko 			}
2032ba7d49b1SJiri Pirko 		}
2033ba7d49b1SJiri Pirko 
203438f7b870SPatrick McHardy 		if (dev) {
203590c325e3SNicolas Dichtel 			int status = 0;
203638f7b870SPatrick McHardy 
203738f7b870SPatrick McHardy 			if (nlh->nlmsg_flags & NLM_F_EXCL)
203838f7b870SPatrick McHardy 				return -EEXIST;
203938f7b870SPatrick McHardy 			if (nlh->nlmsg_flags & NLM_F_REPLACE)
204038f7b870SPatrick McHardy 				return -EOPNOTSUPP;
204138f7b870SPatrick McHardy 
204238f7b870SPatrick McHardy 			if (linkinfo[IFLA_INFO_DATA]) {
204338f7b870SPatrick McHardy 				if (!ops || ops != dev->rtnl_link_ops ||
204438f7b870SPatrick McHardy 				    !ops->changelink)
204538f7b870SPatrick McHardy 					return -EOPNOTSUPP;
204638f7b870SPatrick McHardy 
204738f7b870SPatrick McHardy 				err = ops->changelink(dev, tb, data);
204838f7b870SPatrick McHardy 				if (err < 0)
204938f7b870SPatrick McHardy 					return err;
2050ba998906SNicolas Dichtel 				status |= DO_SETLINK_NOTIFY;
205138f7b870SPatrick McHardy 			}
205238f7b870SPatrick McHardy 
2053ba7d49b1SJiri Pirko 			if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
2054ba7d49b1SJiri Pirko 				if (!m_ops || !m_ops->slave_changelink)
2055ba7d49b1SJiri Pirko 					return -EOPNOTSUPP;
2056ba7d49b1SJiri Pirko 
2057ba7d49b1SJiri Pirko 				err = m_ops->slave_changelink(master_dev, dev,
2058ba7d49b1SJiri Pirko 							      tb, slave_data);
2059ba7d49b1SJiri Pirko 				if (err < 0)
2060ba7d49b1SJiri Pirko 					return err;
2061ba998906SNicolas Dichtel 				status |= DO_SETLINK_NOTIFY;
2062ba7d49b1SJiri Pirko 			}
2063ba7d49b1SJiri Pirko 
206490c325e3SNicolas Dichtel 			return do_setlink(skb, dev, ifm, tb, ifname, status);
206538f7b870SPatrick McHardy 		}
206638f7b870SPatrick McHardy 
2067ffa934f1SPatrick McHardy 		if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
2068ffa934f1SPatrick McHardy 			if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
206990f62cf3SEric W. Biederman 				return rtnl_group_changelink(skb, net,
2070ffa934f1SPatrick McHardy 						nla_get_u32(tb[IFLA_GROUP]),
2071ffa934f1SPatrick McHardy 						ifm, tb);
207238f7b870SPatrick McHardy 			return -ENODEV;
2073ffa934f1SPatrick McHardy 		}
207438f7b870SPatrick McHardy 
20750e06877cSPatrick McHardy 		if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
207638f7b870SPatrick McHardy 			return -EOPNOTSUPP;
207738f7b870SPatrick McHardy 
207838f7b870SPatrick McHardy 		if (!ops) {
207995a5afcaSJohannes Berg #ifdef CONFIG_MODULES
208038f7b870SPatrick McHardy 			if (kind[0]) {
208138f7b870SPatrick McHardy 				__rtnl_unlock();
208238f7b870SPatrick McHardy 				request_module("rtnl-link-%s", kind);
208338f7b870SPatrick McHardy 				rtnl_lock();
208438f7b870SPatrick McHardy 				ops = rtnl_link_ops_get(kind);
208538f7b870SPatrick McHardy 				if (ops)
208638f7b870SPatrick McHardy 					goto replay;
208738f7b870SPatrick McHardy 			}
208838f7b870SPatrick McHardy #endif
208938f7b870SPatrick McHardy 			return -EOPNOTSUPP;
209038f7b870SPatrick McHardy 		}
209138f7b870SPatrick McHardy 
2092b0ab2fabSJiri Pirko 		if (!ops->setup)
2093b0ab2fabSJiri Pirko 			return -EOPNOTSUPP;
2094b0ab2fabSJiri Pirko 
20955517750fSTom Gundersen 		if (!ifname[0]) {
209638f7b870SPatrick McHardy 			snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
20975517750fSTom Gundersen 			name_assign_type = NET_NAME_ENUM;
20985517750fSTom Gundersen 		}
209938f7b870SPatrick McHardy 
210081adee47SEric W. Biederman 		dest_net = rtnl_link_get_net(net, tb);
210113ad1774SEric W. Biederman 		if (IS_ERR(dest_net))
210213ad1774SEric W. Biederman 			return PTR_ERR(dest_net);
210313ad1774SEric W. Biederman 
21045517750fSTom Gundersen 		dev = rtnl_create_link(dest_net, ifname, name_assign_type, ops, tb);
21059c7dafbfSPavel Emelyanov 		if (IS_ERR(dev)) {
2106e7199288SPavel Emelianov 			err = PTR_ERR(dev);
21079c7dafbfSPavel Emelyanov 			goto out;
21089c7dafbfSPavel Emelyanov 		}
21099c7dafbfSPavel Emelyanov 
21109c7dafbfSPavel Emelyanov 		dev->ifindex = ifm->ifi_index;
21119c7dafbfSPavel Emelyanov 
21120e0eee24SCong Wang 		if (ops->newlink) {
211381adee47SEric W. Biederman 			err = ops->newlink(net, dev, tb, data);
21140e0eee24SCong Wang 			/* Drivers should call free_netdev() in ->destructor
2115e51fb152SCong Wang 			 * and unregister it on failure after registration
2116e51fb152SCong Wang 			 * so that device could be finally freed in rtnl_unlock.
21170e0eee24SCong Wang 			 */
2118e51fb152SCong Wang 			if (err < 0) {
2119e51fb152SCong Wang 				/* If device is not registered at all, free it now */
2120e51fb152SCong Wang 				if (dev->reg_state == NETREG_UNINITIALIZED)
2121e51fb152SCong Wang 					free_netdev(dev);
21220e0eee24SCong Wang 				goto out;
2123e51fb152SCong Wang 			}
21240e0eee24SCong Wang 		} else {
21252d85cba2SPatrick McHardy 			err = register_netdevice(dev);
2126fce9b9beSDan Carpenter 			if (err < 0) {
212738f7b870SPatrick McHardy 				free_netdev(dev);
21283729d502SPatrick McHardy 				goto out;
2129fce9b9beSDan Carpenter 			}
21300e0eee24SCong Wang 		}
21313729d502SPatrick McHardy 		err = rtnl_configure_link(dev, ifm);
21323729d502SPatrick McHardy 		if (err < 0)
21333729d502SPatrick McHardy 			unregister_netdevice(dev);
21343729d502SPatrick McHardy out:
213581adee47SEric W. Biederman 		put_net(dest_net);
213638f7b870SPatrick McHardy 		return err;
213738f7b870SPatrick McHardy 	}
213838f7b870SPatrick McHardy }
213938f7b870SPatrick McHardy 
2140661d2967SThomas Graf static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh)
2141711e2c33SJean Tourrilhes {
21423b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
2143b60c5115SThomas Graf 	struct ifinfomsg *ifm;
2144a3d12891SEric Dumazet 	char ifname[IFNAMSIZ];
2145b60c5115SThomas Graf 	struct nlattr *tb[IFLA_MAX+1];
2146b60c5115SThomas Graf 	struct net_device *dev = NULL;
2147b60c5115SThomas Graf 	struct sk_buff *nskb;
2148339bf98fSThomas Graf 	int err;
2149115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
2150711e2c33SJean Tourrilhes 
2151b60c5115SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2152b60c5115SThomas Graf 	if (err < 0)
21539918f230SEric Sesterhenn 		return err;
2154b60c5115SThomas Graf 
2155a3d12891SEric Dumazet 	if (tb[IFLA_IFNAME])
2156a3d12891SEric Dumazet 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2157a3d12891SEric Dumazet 
2158115c9b81SGreg Rose 	if (tb[IFLA_EXT_MASK])
2159115c9b81SGreg Rose 		ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2160115c9b81SGreg Rose 
2161b60c5115SThomas Graf 	ifm = nlmsg_data(nlh);
2162a3d12891SEric Dumazet 	if (ifm->ifi_index > 0)
2163a3d12891SEric Dumazet 		dev = __dev_get_by_index(net, ifm->ifi_index);
2164a3d12891SEric Dumazet 	else if (tb[IFLA_IFNAME])
2165a3d12891SEric Dumazet 		dev = __dev_get_by_name(net, ifname);
2166a3d12891SEric Dumazet 	else
2167b60c5115SThomas Graf 		return -EINVAL;
2168b60c5115SThomas Graf 
2169a3d12891SEric Dumazet 	if (dev == NULL)
2170a3d12891SEric Dumazet 		return -ENODEV;
2171a3d12891SEric Dumazet 
2172115c9b81SGreg Rose 	nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
2173a3d12891SEric Dumazet 	if (nskb == NULL)
2174a3d12891SEric Dumazet 		return -ENOBUFS;
2175711e2c33SJean Tourrilhes 
217615e47304SEric W. Biederman 	err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid,
2177115c9b81SGreg Rose 			       nlh->nlmsg_seq, 0, 0, ext_filter_mask);
217826932566SPatrick McHardy 	if (err < 0) {
217926932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in if_nlmsg_size */
218026932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
218126932566SPatrick McHardy 		kfree_skb(nskb);
2182a3d12891SEric Dumazet 	} else
218315e47304SEric W. Biederman 		err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
2184b60c5115SThomas Graf 
2185711e2c33SJean Tourrilhes 	return err;
2186711e2c33SJean Tourrilhes }
2187711e2c33SJean Tourrilhes 
2188115c9b81SGreg Rose static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
2189c7ac8679SGreg Rose {
2190115c9b81SGreg Rose 	struct net *net = sock_net(skb->sk);
2191115c9b81SGreg Rose 	struct net_device *dev;
2192115c9b81SGreg Rose 	struct nlattr *tb[IFLA_MAX+1];
2193115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
2194115c9b81SGreg Rose 	u16 min_ifinfo_dump_size = 0;
2195e5eca6d4SMichal Schmidt 	int hdrlen;
2196115c9b81SGreg Rose 
2197e5eca6d4SMichal Schmidt 	/* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
2198e5eca6d4SMichal Schmidt 	hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2199e5eca6d4SMichal Schmidt 		 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2200e5eca6d4SMichal Schmidt 
2201e5eca6d4SMichal Schmidt 	if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
2202115c9b81SGreg Rose 		if (tb[IFLA_EXT_MASK])
2203115c9b81SGreg Rose 			ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2204a4b64fbeSEric Dumazet 	}
2205115c9b81SGreg Rose 
2206115c9b81SGreg Rose 	if (!ext_filter_mask)
2207115c9b81SGreg Rose 		return NLMSG_GOODSIZE;
2208115c9b81SGreg Rose 	/*
2209115c9b81SGreg Rose 	 * traverse the list of net devices and compute the minimum
2210115c9b81SGreg Rose 	 * buffer size based upon the filter mask.
2211115c9b81SGreg Rose 	 */
2212115c9b81SGreg Rose 	list_for_each_entry(dev, &net->dev_base_head, dev_list) {
2213115c9b81SGreg Rose 		min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
2214115c9b81SGreg Rose 					     if_nlmsg_size(dev,
2215115c9b81SGreg Rose 						           ext_filter_mask));
2216115c9b81SGreg Rose 	}
2217115c9b81SGreg Rose 
2218c7ac8679SGreg Rose 	return min_ifinfo_dump_size;
2219c7ac8679SGreg Rose }
2220c7ac8679SGreg Rose 
222142bad1daSAdrian Bunk static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
22221da177e4SLinus Torvalds {
22231da177e4SLinus Torvalds 	int idx;
22241da177e4SLinus Torvalds 	int s_idx = cb->family;
22251da177e4SLinus Torvalds 
22261da177e4SLinus Torvalds 	if (s_idx == 0)
22271da177e4SLinus Torvalds 		s_idx = 1;
222825239ceeSPatrick McHardy 	for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
22291da177e4SLinus Torvalds 		int type = cb->nlh->nlmsg_type-RTM_BASE;
22301da177e4SLinus Torvalds 		if (idx < s_idx || idx == PF_PACKET)
22311da177e4SLinus Torvalds 			continue;
2232e2849863SThomas Graf 		if (rtnl_msg_handlers[idx] == NULL ||
2233e2849863SThomas Graf 		    rtnl_msg_handlers[idx][type].dumpit == NULL)
22341da177e4SLinus Torvalds 			continue;
22350465277fSNicolas Dichtel 		if (idx > s_idx) {
22361da177e4SLinus Torvalds 			memset(&cb->args[0], 0, sizeof(cb->args));
22370465277fSNicolas Dichtel 			cb->prev_seq = 0;
22380465277fSNicolas Dichtel 			cb->seq = 0;
22390465277fSNicolas Dichtel 		}
2240e2849863SThomas Graf 		if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
22411da177e4SLinus Torvalds 			break;
22421da177e4SLinus Torvalds 	}
22431da177e4SLinus Torvalds 	cb->family = idx;
22441da177e4SLinus Torvalds 
22451da177e4SLinus Torvalds 	return skb->len;
22461da177e4SLinus Torvalds }
22471da177e4SLinus Torvalds 
22487f294054SAlexei Starovoitov void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
22497f294054SAlexei Starovoitov 		  gfp_t flags)
22501da177e4SLinus Torvalds {
2251c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
22521da177e4SLinus Torvalds 	struct sk_buff *skb;
22530ec6d3f4SThomas Graf 	int err = -ENOBUFS;
2254c7ac8679SGreg Rose 	size_t if_info_size;
22551da177e4SLinus Torvalds 
22567f294054SAlexei Starovoitov 	skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
22570ec6d3f4SThomas Graf 	if (skb == NULL)
22580ec6d3f4SThomas Graf 		goto errout;
22591da177e4SLinus Torvalds 
2260115c9b81SGreg Rose 	err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0);
226126932566SPatrick McHardy 	if (err < 0) {
226226932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in if_nlmsg_size() */
226326932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
226426932566SPatrick McHardy 		kfree_skb(skb);
226526932566SPatrick McHardy 		goto errout;
226626932566SPatrick McHardy 	}
22677f294054SAlexei Starovoitov 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
22681ce85fe4SPablo Neira Ayuso 	return;
22690ec6d3f4SThomas Graf errout:
22700ec6d3f4SThomas Graf 	if (err < 0)
22714b3da706SEric W. Biederman 		rtnl_set_sk_err(net, RTNLGRP_LINK, err);
22721da177e4SLinus Torvalds }
2273471cb5a3SJiri Pirko EXPORT_SYMBOL(rtmsg_ifinfo);
22741da177e4SLinus Torvalds 
2275d83b0603SJohn Fastabend static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
2276d83b0603SJohn Fastabend 				   struct net_device *dev,
2277d83b0603SJohn Fastabend 				   u8 *addr, u32 pid, u32 seq,
22781c104a6bSNicolas Dichtel 				   int type, unsigned int flags,
22791c104a6bSNicolas Dichtel 				   int nlflags)
2280d83b0603SJohn Fastabend {
2281d83b0603SJohn Fastabend 	struct nlmsghdr *nlh;
2282d83b0603SJohn Fastabend 	struct ndmsg *ndm;
2283d83b0603SJohn Fastabend 
22841c104a6bSNicolas Dichtel 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
2285d83b0603SJohn Fastabend 	if (!nlh)
2286d83b0603SJohn Fastabend 		return -EMSGSIZE;
2287d83b0603SJohn Fastabend 
2288d83b0603SJohn Fastabend 	ndm = nlmsg_data(nlh);
2289d83b0603SJohn Fastabend 	ndm->ndm_family  = AF_BRIDGE;
2290d83b0603SJohn Fastabend 	ndm->ndm_pad1	 = 0;
2291d83b0603SJohn Fastabend 	ndm->ndm_pad2    = 0;
2292d83b0603SJohn Fastabend 	ndm->ndm_flags	 = flags;
2293d83b0603SJohn Fastabend 	ndm->ndm_type	 = 0;
2294d83b0603SJohn Fastabend 	ndm->ndm_ifindex = dev->ifindex;
2295d83b0603SJohn Fastabend 	ndm->ndm_state   = NUD_PERMANENT;
2296d83b0603SJohn Fastabend 
2297d83b0603SJohn Fastabend 	if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
2298d83b0603SJohn Fastabend 		goto nla_put_failure;
2299d83b0603SJohn Fastabend 
2300d83b0603SJohn Fastabend 	return nlmsg_end(skb, nlh);
2301d83b0603SJohn Fastabend 
2302d83b0603SJohn Fastabend nla_put_failure:
2303d83b0603SJohn Fastabend 	nlmsg_cancel(skb, nlh);
2304d83b0603SJohn Fastabend 	return -EMSGSIZE;
2305d83b0603SJohn Fastabend }
2306d83b0603SJohn Fastabend 
23073ff661c3SJohn Fastabend static inline size_t rtnl_fdb_nlmsg_size(void)
23083ff661c3SJohn Fastabend {
23093ff661c3SJohn Fastabend 	return NLMSG_ALIGN(sizeof(struct ndmsg)) + nla_total_size(ETH_ALEN);
23103ff661c3SJohn Fastabend }
23113ff661c3SJohn Fastabend 
23123ff661c3SJohn Fastabend static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, int type)
23133ff661c3SJohn Fastabend {
23143ff661c3SJohn Fastabend 	struct net *net = dev_net(dev);
23153ff661c3SJohn Fastabend 	struct sk_buff *skb;
23163ff661c3SJohn Fastabend 	int err = -ENOBUFS;
23173ff661c3SJohn Fastabend 
23183ff661c3SJohn Fastabend 	skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
23193ff661c3SJohn Fastabend 	if (!skb)
23203ff661c3SJohn Fastabend 		goto errout;
23213ff661c3SJohn Fastabend 
23221c104a6bSNicolas Dichtel 	err = nlmsg_populate_fdb_fill(skb, dev, addr, 0, 0, type, NTF_SELF, 0);
23233ff661c3SJohn Fastabend 	if (err < 0) {
23243ff661c3SJohn Fastabend 		kfree_skb(skb);
23253ff661c3SJohn Fastabend 		goto errout;
23263ff661c3SJohn Fastabend 	}
23273ff661c3SJohn Fastabend 
23283ff661c3SJohn Fastabend 	rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
23293ff661c3SJohn Fastabend 	return;
23303ff661c3SJohn Fastabend errout:
23313ff661c3SJohn Fastabend 	rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
23323ff661c3SJohn Fastabend }
23333ff661c3SJohn Fastabend 
2334090096bfSVlad Yasevich /**
2335090096bfSVlad Yasevich  * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
2336090096bfSVlad Yasevich  */
2337090096bfSVlad Yasevich int ndo_dflt_fdb_add(struct ndmsg *ndm,
2338090096bfSVlad Yasevich 		     struct nlattr *tb[],
2339090096bfSVlad Yasevich 		     struct net_device *dev,
2340f6f6424bSJiri Pirko 		     const unsigned char *addr, u16 vid,
2341090096bfSVlad Yasevich 		     u16 flags)
2342090096bfSVlad Yasevich {
2343090096bfSVlad Yasevich 	int err = -EINVAL;
2344090096bfSVlad Yasevich 
2345090096bfSVlad Yasevich 	/* If aging addresses are supported device will need to
2346090096bfSVlad Yasevich 	 * implement its own handler for this.
2347090096bfSVlad Yasevich 	 */
2348090096bfSVlad Yasevich 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
2349090096bfSVlad Yasevich 		pr_info("%s: FDB only supports static addresses\n", dev->name);
2350090096bfSVlad Yasevich 		return err;
2351090096bfSVlad Yasevich 	}
2352090096bfSVlad Yasevich 
2353090096bfSVlad Yasevich 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2354090096bfSVlad Yasevich 		err = dev_uc_add_excl(dev, addr);
2355090096bfSVlad Yasevich 	else if (is_multicast_ether_addr(addr))
2356090096bfSVlad Yasevich 		err = dev_mc_add_excl(dev, addr);
2357090096bfSVlad Yasevich 
2358090096bfSVlad Yasevich 	/* Only return duplicate errors if NLM_F_EXCL is set */
2359090096bfSVlad Yasevich 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
2360090096bfSVlad Yasevich 		err = 0;
2361090096bfSVlad Yasevich 
2362090096bfSVlad Yasevich 	return err;
2363090096bfSVlad Yasevich }
2364090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_add);
2365090096bfSVlad Yasevich 
2366f6f6424bSJiri Pirko static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid)
2367f6f6424bSJiri Pirko {
2368f6f6424bSJiri Pirko 	u16 vid = 0;
2369f6f6424bSJiri Pirko 
2370f6f6424bSJiri Pirko 	if (vlan_attr) {
2371f6f6424bSJiri Pirko 		if (nla_len(vlan_attr) != sizeof(u16)) {
2372f6f6424bSJiri Pirko 			pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan\n");
2373f6f6424bSJiri Pirko 			return -EINVAL;
2374f6f6424bSJiri Pirko 		}
2375f6f6424bSJiri Pirko 
2376f6f6424bSJiri Pirko 		vid = nla_get_u16(vlan_attr);
2377f6f6424bSJiri Pirko 
2378f6f6424bSJiri Pirko 		if (!vid || vid >= VLAN_VID_MASK) {
2379f6f6424bSJiri Pirko 			pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan id %d\n",
2380f6f6424bSJiri Pirko 				vid);
2381f6f6424bSJiri Pirko 			return -EINVAL;
2382f6f6424bSJiri Pirko 		}
2383f6f6424bSJiri Pirko 	}
2384f6f6424bSJiri Pirko 	*p_vid = vid;
2385f6f6424bSJiri Pirko 	return 0;
2386f6f6424bSJiri Pirko }
2387f6f6424bSJiri Pirko 
2388661d2967SThomas Graf static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh)
238977162022SJohn Fastabend {
239077162022SJohn Fastabend 	struct net *net = sock_net(skb->sk);
239177162022SJohn Fastabend 	struct ndmsg *ndm;
239277162022SJohn Fastabend 	struct nlattr *tb[NDA_MAX+1];
239377162022SJohn Fastabend 	struct net_device *dev;
239477162022SJohn Fastabend 	u8 *addr;
2395f6f6424bSJiri Pirko 	u16 vid;
239677162022SJohn Fastabend 	int err;
239777162022SJohn Fastabend 
239877162022SJohn Fastabend 	err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
239977162022SJohn Fastabend 	if (err < 0)
240077162022SJohn Fastabend 		return err;
240177162022SJohn Fastabend 
240277162022SJohn Fastabend 	ndm = nlmsg_data(nlh);
240377162022SJohn Fastabend 	if (ndm->ndm_ifindex == 0) {
240477162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n");
240577162022SJohn Fastabend 		return -EINVAL;
240677162022SJohn Fastabend 	}
240777162022SJohn Fastabend 
240877162022SJohn Fastabend 	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
240977162022SJohn Fastabend 	if (dev == NULL) {
241077162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n");
241177162022SJohn Fastabend 		return -ENODEV;
241277162022SJohn Fastabend 	}
241377162022SJohn Fastabend 
241477162022SJohn Fastabend 	if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
241577162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n");
241677162022SJohn Fastabend 		return -EINVAL;
241777162022SJohn Fastabend 	}
241877162022SJohn Fastabend 
241977162022SJohn Fastabend 	addr = nla_data(tb[NDA_LLADDR]);
242077162022SJohn Fastabend 
2421f6f6424bSJiri Pirko 	err = fdb_vid_parse(tb[NDA_VLAN], &vid);
2422f6f6424bSJiri Pirko 	if (err)
2423f6f6424bSJiri Pirko 		return err;
2424f6f6424bSJiri Pirko 
242577162022SJohn Fastabend 	err = -EOPNOTSUPP;
242677162022SJohn Fastabend 
242777162022SJohn Fastabend 	/* Support fdb on master device the net/bridge default case */
242877162022SJohn Fastabend 	if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
242977162022SJohn Fastabend 	    (dev->priv_flags & IFF_BRIDGE_PORT)) {
2430898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2431898e5061SJiri Pirko 		const struct net_device_ops *ops = br_dev->netdev_ops;
2432898e5061SJiri Pirko 
2433f6f6424bSJiri Pirko 		err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
2434f6f6424bSJiri Pirko 				       nlh->nlmsg_flags);
243577162022SJohn Fastabend 		if (err)
243677162022SJohn Fastabend 			goto out;
243777162022SJohn Fastabend 		else
243877162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_MASTER;
243977162022SJohn Fastabend 	}
244077162022SJohn Fastabend 
244177162022SJohn Fastabend 	/* Embedded bridge, macvlan, and any other device support */
2442090096bfSVlad Yasevich 	if ((ndm->ndm_flags & NTF_SELF)) {
2443090096bfSVlad Yasevich 		if (dev->netdev_ops->ndo_fdb_add)
2444090096bfSVlad Yasevich 			err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
2445f6f6424bSJiri Pirko 							   vid,
2446090096bfSVlad Yasevich 							   nlh->nlmsg_flags);
2447090096bfSVlad Yasevich 		else
2448f6f6424bSJiri Pirko 			err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
244977162022SJohn Fastabend 					       nlh->nlmsg_flags);
245077162022SJohn Fastabend 
24513ff661c3SJohn Fastabend 		if (!err) {
24523ff661c3SJohn Fastabend 			rtnl_fdb_notify(dev, addr, RTM_NEWNEIGH);
245377162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_SELF;
245477162022SJohn Fastabend 		}
24553ff661c3SJohn Fastabend 	}
245677162022SJohn Fastabend out:
245777162022SJohn Fastabend 	return err;
245877162022SJohn Fastabend }
245977162022SJohn Fastabend 
2460090096bfSVlad Yasevich /**
2461090096bfSVlad Yasevich  * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
2462090096bfSVlad Yasevich  */
2463090096bfSVlad Yasevich int ndo_dflt_fdb_del(struct ndmsg *ndm,
2464090096bfSVlad Yasevich 		     struct nlattr *tb[],
2465090096bfSVlad Yasevich 		     struct net_device *dev,
2466f6f6424bSJiri Pirko 		     const unsigned char *addr, u16 vid)
2467090096bfSVlad Yasevich {
2468c8a89c4aSAlexander Duyck 	int err = -EINVAL;
2469090096bfSVlad Yasevich 
2470090096bfSVlad Yasevich 	/* If aging addresses are supported device will need to
2471090096bfSVlad Yasevich 	 * implement its own handler for this.
2472090096bfSVlad Yasevich 	 */
247364535993SSridhar Samudrala 	if (!(ndm->ndm_state & NUD_PERMANENT)) {
2474090096bfSVlad Yasevich 		pr_info("%s: FDB only supports static addresses\n", dev->name);
2475c8a89c4aSAlexander Duyck 		return err;
2476090096bfSVlad Yasevich 	}
2477090096bfSVlad Yasevich 
2478090096bfSVlad Yasevich 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2479090096bfSVlad Yasevich 		err = dev_uc_del(dev, addr);
2480090096bfSVlad Yasevich 	else if (is_multicast_ether_addr(addr))
2481090096bfSVlad Yasevich 		err = dev_mc_del(dev, addr);
2482090096bfSVlad Yasevich 
2483090096bfSVlad Yasevich 	return err;
2484090096bfSVlad Yasevich }
2485090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_del);
2486090096bfSVlad Yasevich 
2487661d2967SThomas Graf static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh)
248877162022SJohn Fastabend {
248977162022SJohn Fastabend 	struct net *net = sock_net(skb->sk);
249077162022SJohn Fastabend 	struct ndmsg *ndm;
24911690be63SVlad Yasevich 	struct nlattr *tb[NDA_MAX+1];
249277162022SJohn Fastabend 	struct net_device *dev;
249377162022SJohn Fastabend 	int err = -EINVAL;
249477162022SJohn Fastabend 	__u8 *addr;
2495f6f6424bSJiri Pirko 	u16 vid;
249677162022SJohn Fastabend 
249790f62cf3SEric W. Biederman 	if (!netlink_capable(skb, CAP_NET_ADMIN))
24981690be63SVlad Yasevich 		return -EPERM;
24991690be63SVlad Yasevich 
25001690be63SVlad Yasevich 	err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
25011690be63SVlad Yasevich 	if (err < 0)
25021690be63SVlad Yasevich 		return err;
250377162022SJohn Fastabend 
250477162022SJohn Fastabend 	ndm = nlmsg_data(nlh);
250577162022SJohn Fastabend 	if (ndm->ndm_ifindex == 0) {
250677162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n");
250777162022SJohn Fastabend 		return -EINVAL;
250877162022SJohn Fastabend 	}
250977162022SJohn Fastabend 
251077162022SJohn Fastabend 	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
251177162022SJohn Fastabend 	if (dev == NULL) {
251277162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n");
251377162022SJohn Fastabend 		return -ENODEV;
251477162022SJohn Fastabend 	}
251577162022SJohn Fastabend 
25161690be63SVlad Yasevich 	if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
25171690be63SVlad Yasevich 		pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n");
251877162022SJohn Fastabend 		return -EINVAL;
251977162022SJohn Fastabend 	}
252077162022SJohn Fastabend 
25211690be63SVlad Yasevich 	addr = nla_data(tb[NDA_LLADDR]);
25221690be63SVlad Yasevich 
2523f6f6424bSJiri Pirko 	err = fdb_vid_parse(tb[NDA_VLAN], &vid);
2524f6f6424bSJiri Pirko 	if (err)
2525f6f6424bSJiri Pirko 		return err;
2526f6f6424bSJiri Pirko 
252777162022SJohn Fastabend 	err = -EOPNOTSUPP;
252877162022SJohn Fastabend 
252977162022SJohn Fastabend 	/* Support fdb on master device the net/bridge default case */
253077162022SJohn Fastabend 	if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
253177162022SJohn Fastabend 	    (dev->priv_flags & IFF_BRIDGE_PORT)) {
2532898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2533898e5061SJiri Pirko 		const struct net_device_ops *ops = br_dev->netdev_ops;
253477162022SJohn Fastabend 
2535898e5061SJiri Pirko 		if (ops->ndo_fdb_del)
2536f6f6424bSJiri Pirko 			err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
253777162022SJohn Fastabend 
253877162022SJohn Fastabend 		if (err)
253977162022SJohn Fastabend 			goto out;
254077162022SJohn Fastabend 		else
254177162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_MASTER;
254277162022SJohn Fastabend 	}
254377162022SJohn Fastabend 
254477162022SJohn Fastabend 	/* Embedded bridge, macvlan, and any other device support */
2545090096bfSVlad Yasevich 	if (ndm->ndm_flags & NTF_SELF) {
2546090096bfSVlad Yasevich 		if (dev->netdev_ops->ndo_fdb_del)
2547f6f6424bSJiri Pirko 			err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
2548f6f6424bSJiri Pirko 							   vid);
2549090096bfSVlad Yasevich 		else
2550f6f6424bSJiri Pirko 			err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
255177162022SJohn Fastabend 
25523ff661c3SJohn Fastabend 		if (!err) {
25533ff661c3SJohn Fastabend 			rtnl_fdb_notify(dev, addr, RTM_DELNEIGH);
255477162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_SELF;
255577162022SJohn Fastabend 		}
25563ff661c3SJohn Fastabend 	}
255777162022SJohn Fastabend out:
255877162022SJohn Fastabend 	return err;
255977162022SJohn Fastabend }
256077162022SJohn Fastabend 
2561d83b0603SJohn Fastabend static int nlmsg_populate_fdb(struct sk_buff *skb,
2562d83b0603SJohn Fastabend 			      struct netlink_callback *cb,
2563d83b0603SJohn Fastabend 			      struct net_device *dev,
2564d83b0603SJohn Fastabend 			      int *idx,
2565d83b0603SJohn Fastabend 			      struct netdev_hw_addr_list *list)
2566d83b0603SJohn Fastabend {
2567d83b0603SJohn Fastabend 	struct netdev_hw_addr *ha;
2568d83b0603SJohn Fastabend 	int err;
256915e47304SEric W. Biederman 	u32 portid, seq;
2570d83b0603SJohn Fastabend 
257115e47304SEric W. Biederman 	portid = NETLINK_CB(cb->skb).portid;
2572d83b0603SJohn Fastabend 	seq = cb->nlh->nlmsg_seq;
2573d83b0603SJohn Fastabend 
2574d83b0603SJohn Fastabend 	list_for_each_entry(ha, &list->list, list) {
2575d83b0603SJohn Fastabend 		if (*idx < cb->args[0])
2576d83b0603SJohn Fastabend 			goto skip;
2577d83b0603SJohn Fastabend 
2578d83b0603SJohn Fastabend 		err = nlmsg_populate_fdb_fill(skb, dev, ha->addr,
2579a7a558feSJohn Fastabend 					      portid, seq,
25801c104a6bSNicolas Dichtel 					      RTM_NEWNEIGH, NTF_SELF,
25811c104a6bSNicolas Dichtel 					      NLM_F_MULTI);
2582d83b0603SJohn Fastabend 		if (err < 0)
2583d83b0603SJohn Fastabend 			return err;
2584d83b0603SJohn Fastabend skip:
2585d83b0603SJohn Fastabend 		*idx += 1;
2586d83b0603SJohn Fastabend 	}
2587d83b0603SJohn Fastabend 	return 0;
2588d83b0603SJohn Fastabend }
2589d83b0603SJohn Fastabend 
2590d83b0603SJohn Fastabend /**
25912c53040fSBen Hutchings  * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
2592d83b0603SJohn Fastabend  * @nlh: netlink message header
2593d83b0603SJohn Fastabend  * @dev: netdevice
2594d83b0603SJohn Fastabend  *
2595d83b0603SJohn Fastabend  * Default netdevice operation to dump the existing unicast address list.
259691f3e7b1SJohn Fastabend  * Returns number of addresses from list put in skb.
2597d83b0603SJohn Fastabend  */
2598d83b0603SJohn Fastabend int ndo_dflt_fdb_dump(struct sk_buff *skb,
2599d83b0603SJohn Fastabend 		      struct netlink_callback *cb,
2600d83b0603SJohn Fastabend 		      struct net_device *dev,
26015d5eacb3SJamal Hadi Salim 		      struct net_device *filter_dev,
2602d83b0603SJohn Fastabend 		      int idx)
2603d83b0603SJohn Fastabend {
2604d83b0603SJohn Fastabend 	int err;
2605d83b0603SJohn Fastabend 
2606d83b0603SJohn Fastabend 	netif_addr_lock_bh(dev);
2607d83b0603SJohn Fastabend 	err = nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->uc);
2608d83b0603SJohn Fastabend 	if (err)
2609d83b0603SJohn Fastabend 		goto out;
2610d83b0603SJohn Fastabend 	nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->mc);
2611d83b0603SJohn Fastabend out:
2612d83b0603SJohn Fastabend 	netif_addr_unlock_bh(dev);
2613d83b0603SJohn Fastabend 	return idx;
2614d83b0603SJohn Fastabend }
2615d83b0603SJohn Fastabend EXPORT_SYMBOL(ndo_dflt_fdb_dump);
2616d83b0603SJohn Fastabend 
261777162022SJohn Fastabend static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
261877162022SJohn Fastabend {
261977162022SJohn Fastabend 	struct net_device *dev;
26205e6d2435SJamal Hadi Salim 	struct nlattr *tb[IFLA_MAX+1];
26215e6d2435SJamal Hadi Salim 	struct net_device *bdev = NULL;
26225e6d2435SJamal Hadi Salim 	struct net_device *br_dev = NULL;
26235e6d2435SJamal Hadi Salim 	const struct net_device_ops *ops = NULL;
26245e6d2435SJamal Hadi Salim 	const struct net_device_ops *cops = NULL;
26255e6d2435SJamal Hadi Salim 	struct ifinfomsg *ifm = nlmsg_data(cb->nlh);
26265e6d2435SJamal Hadi Salim 	struct net *net = sock_net(skb->sk);
26275e6d2435SJamal Hadi Salim 	int brport_idx = 0;
26285e6d2435SJamal Hadi Salim 	int br_idx = 0;
26295e6d2435SJamal Hadi Salim 	int idx = 0;
263077162022SJohn Fastabend 
26315e6d2435SJamal Hadi Salim 	if (nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX,
26325e6d2435SJamal Hadi Salim 			ifla_policy) == 0) {
26335e6d2435SJamal Hadi Salim 		if (tb[IFLA_MASTER])
26345e6d2435SJamal Hadi Salim 			br_idx = nla_get_u32(tb[IFLA_MASTER]);
26355e6d2435SJamal Hadi Salim 	}
263677162022SJohn Fastabend 
26375e6d2435SJamal Hadi Salim 	brport_idx = ifm->ifi_index;
26385e6d2435SJamal Hadi Salim 
26395e6d2435SJamal Hadi Salim 	if (br_idx) {
26405e6d2435SJamal Hadi Salim 		br_dev = __dev_get_by_index(net, br_idx);
26415e6d2435SJamal Hadi Salim 		if (!br_dev)
26425e6d2435SJamal Hadi Salim 			return -ENODEV;
26435e6d2435SJamal Hadi Salim 
2644898e5061SJiri Pirko 		ops = br_dev->netdev_ops;
26455e6d2435SJamal Hadi Salim 		bdev = br_dev;
26465e6d2435SJamal Hadi Salim 	}
26475e6d2435SJamal Hadi Salim 
26485e6d2435SJamal Hadi Salim 	for_each_netdev(net, dev) {
26495e6d2435SJamal Hadi Salim 		if (brport_idx && (dev->ifindex != brport_idx))
26505e6d2435SJamal Hadi Salim 			continue;
26515e6d2435SJamal Hadi Salim 
26525e6d2435SJamal Hadi Salim 		if (!br_idx) { /* user did not specify a specific bridge */
26535e6d2435SJamal Hadi Salim 			if (dev->priv_flags & IFF_BRIDGE_PORT) {
26545e6d2435SJamal Hadi Salim 				br_dev = netdev_master_upper_dev_get(dev);
26555e6d2435SJamal Hadi Salim 				cops = br_dev->netdev_ops;
26565e6d2435SJamal Hadi Salim 			}
26575e6d2435SJamal Hadi Salim 
26585e6d2435SJamal Hadi Salim 			bdev = dev;
26595e6d2435SJamal Hadi Salim 		} else {
26605e6d2435SJamal Hadi Salim 			if (dev != br_dev &&
26615e6d2435SJamal Hadi Salim 			    !(dev->priv_flags & IFF_BRIDGE_PORT))
26625e6d2435SJamal Hadi Salim 				continue;
26635e6d2435SJamal Hadi Salim 
26645e6d2435SJamal Hadi Salim 			if (br_dev != netdev_master_upper_dev_get(dev) &&
26655e6d2435SJamal Hadi Salim 			    !(dev->priv_flags & IFF_EBRIDGE))
26665e6d2435SJamal Hadi Salim 				continue;
26675e6d2435SJamal Hadi Salim 
26685e6d2435SJamal Hadi Salim 			bdev = br_dev;
26695e6d2435SJamal Hadi Salim 			cops = ops;
26705e6d2435SJamal Hadi Salim 		}
26715e6d2435SJamal Hadi Salim 
26725e6d2435SJamal Hadi Salim 		if (dev->priv_flags & IFF_BRIDGE_PORT) {
26735e6d2435SJamal Hadi Salim 			if (cops && cops->ndo_fdb_dump)
26745e6d2435SJamal Hadi Salim 				idx = cops->ndo_fdb_dump(skb, cb, br_dev, dev,
26755d5eacb3SJamal Hadi Salim 							 idx);
267677162022SJohn Fastabend 		}
267777162022SJohn Fastabend 
26785d5eacb3SJamal Hadi Salim 		idx = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx);
26795e6d2435SJamal Hadi Salim 		if (dev->netdev_ops->ndo_fdb_dump)
26805e6d2435SJamal Hadi Salim 			idx = dev->netdev_ops->ndo_fdb_dump(skb, cb, bdev, dev,
26815e6d2435SJamal Hadi Salim 							    idx);
26825e6d2435SJamal Hadi Salim 
26835e6d2435SJamal Hadi Salim 		cops = NULL;
268477162022SJohn Fastabend 	}
268577162022SJohn Fastabend 
268677162022SJohn Fastabend 	cb->args[0] = idx;
268777162022SJohn Fastabend 	return skb->len;
268877162022SJohn Fastabend }
268977162022SJohn Fastabend 
2690815cccbfSJohn Fastabend int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
2691815cccbfSJohn Fastabend 			    struct net_device *dev, u16 mode)
2692815cccbfSJohn Fastabend {
2693815cccbfSJohn Fastabend 	struct nlmsghdr *nlh;
2694815cccbfSJohn Fastabend 	struct ifinfomsg *ifm;
2695815cccbfSJohn Fastabend 	struct nlattr *br_afspec;
2696815cccbfSJohn Fastabend 	u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
2697898e5061SJiri Pirko 	struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2698815cccbfSJohn Fastabend 
2699815cccbfSJohn Fastabend 	nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), NLM_F_MULTI);
2700815cccbfSJohn Fastabend 	if (nlh == NULL)
2701815cccbfSJohn Fastabend 		return -EMSGSIZE;
2702815cccbfSJohn Fastabend 
2703815cccbfSJohn Fastabend 	ifm = nlmsg_data(nlh);
2704815cccbfSJohn Fastabend 	ifm->ifi_family = AF_BRIDGE;
2705815cccbfSJohn Fastabend 	ifm->__ifi_pad = 0;
2706815cccbfSJohn Fastabend 	ifm->ifi_type = dev->type;
2707815cccbfSJohn Fastabend 	ifm->ifi_index = dev->ifindex;
2708815cccbfSJohn Fastabend 	ifm->ifi_flags = dev_get_flags(dev);
2709815cccbfSJohn Fastabend 	ifm->ifi_change = 0;
2710815cccbfSJohn Fastabend 
2711815cccbfSJohn Fastabend 
2712815cccbfSJohn Fastabend 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
2713815cccbfSJohn Fastabend 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
2714815cccbfSJohn Fastabend 	    nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
2715898e5061SJiri Pirko 	    (br_dev &&
2716898e5061SJiri Pirko 	     nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
2717815cccbfSJohn Fastabend 	    (dev->addr_len &&
2718815cccbfSJohn Fastabend 	     nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
2719815cccbfSJohn Fastabend 	    (dev->ifindex != dev->iflink &&
2720815cccbfSJohn Fastabend 	     nla_put_u32(skb, IFLA_LINK, dev->iflink)))
2721815cccbfSJohn Fastabend 		goto nla_put_failure;
2722815cccbfSJohn Fastabend 
2723815cccbfSJohn Fastabend 	br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
2724815cccbfSJohn Fastabend 	if (!br_afspec)
2725815cccbfSJohn Fastabend 		goto nla_put_failure;
2726815cccbfSJohn Fastabend 
2727815cccbfSJohn Fastabend 	if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF) ||
2728815cccbfSJohn Fastabend 	    nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
2729815cccbfSJohn Fastabend 		nla_nest_cancel(skb, br_afspec);
2730815cccbfSJohn Fastabend 		goto nla_put_failure;
2731815cccbfSJohn Fastabend 	}
2732815cccbfSJohn Fastabend 	nla_nest_end(skb, br_afspec);
2733815cccbfSJohn Fastabend 
2734815cccbfSJohn Fastabend 	return nlmsg_end(skb, nlh);
2735815cccbfSJohn Fastabend nla_put_failure:
2736815cccbfSJohn Fastabend 	nlmsg_cancel(skb, nlh);
2737815cccbfSJohn Fastabend 	return -EMSGSIZE;
2738815cccbfSJohn Fastabend }
2739815cccbfSJohn Fastabend EXPORT_SYMBOL(ndo_dflt_bridge_getlink);
2740815cccbfSJohn Fastabend 
2741e5a55a89SJohn Fastabend static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
2742e5a55a89SJohn Fastabend {
2743e5a55a89SJohn Fastabend 	struct net *net = sock_net(skb->sk);
2744e5a55a89SJohn Fastabend 	struct net_device *dev;
2745e5a55a89SJohn Fastabend 	int idx = 0;
2746e5a55a89SJohn Fastabend 	u32 portid = NETLINK_CB(cb->skb).portid;
2747e5a55a89SJohn Fastabend 	u32 seq = cb->nlh->nlmsg_seq;
27486cbdceebSVlad Yasevich 	u32 filter_mask = 0;
27496cbdceebSVlad Yasevich 
2750aa68c20fSThomas Graf 	if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) {
2751aa68c20fSThomas Graf 		struct nlattr *extfilt;
2752aa68c20fSThomas Graf 
27533e805ad2SAsbjoern Sloth Toennesen 		extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
27546cbdceebSVlad Yasevich 					  IFLA_EXT_MASK);
2755aa68c20fSThomas Graf 		if (extfilt) {
2756aa68c20fSThomas Graf 			if (nla_len(extfilt) < sizeof(filter_mask))
2757aa68c20fSThomas Graf 				return -EINVAL;
2758aa68c20fSThomas Graf 
27596cbdceebSVlad Yasevich 			filter_mask = nla_get_u32(extfilt);
2760aa68c20fSThomas Graf 		}
2761aa68c20fSThomas Graf 	}
2762e5a55a89SJohn Fastabend 
2763e5a55a89SJohn Fastabend 	rcu_read_lock();
2764e5a55a89SJohn Fastabend 	for_each_netdev_rcu(net, dev) {
2765e5a55a89SJohn Fastabend 		const struct net_device_ops *ops = dev->netdev_ops;
2766898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2767e5a55a89SJohn Fastabend 
2768898e5061SJiri Pirko 		if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
276925b1e679SBen Hutchings 			if (idx >= cb->args[0] &&
2770898e5061SJiri Pirko 			    br_dev->netdev_ops->ndo_bridge_getlink(
27716cbdceebSVlad Yasevich 				    skb, portid, seq, dev, filter_mask) < 0)
2772e5a55a89SJohn Fastabend 				break;
2773e5a55a89SJohn Fastabend 			idx++;
2774e5a55a89SJohn Fastabend 		}
2775e5a55a89SJohn Fastabend 
2776e5a55a89SJohn Fastabend 		if (ops->ndo_bridge_getlink) {
277725b1e679SBen Hutchings 			if (idx >= cb->args[0] &&
27786cbdceebSVlad Yasevich 			    ops->ndo_bridge_getlink(skb, portid, seq, dev,
27796cbdceebSVlad Yasevich 						    filter_mask) < 0)
2780e5a55a89SJohn Fastabend 				break;
2781e5a55a89SJohn Fastabend 			idx++;
2782e5a55a89SJohn Fastabend 		}
2783e5a55a89SJohn Fastabend 	}
2784e5a55a89SJohn Fastabend 	rcu_read_unlock();
2785e5a55a89SJohn Fastabend 	cb->args[0] = idx;
2786e5a55a89SJohn Fastabend 
2787e5a55a89SJohn Fastabend 	return skb->len;
2788e5a55a89SJohn Fastabend }
2789e5a55a89SJohn Fastabend 
27902469ffd7SJohn Fastabend static inline size_t bridge_nlmsg_size(void)
27912469ffd7SJohn Fastabend {
27922469ffd7SJohn Fastabend 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
27932469ffd7SJohn Fastabend 		+ nla_total_size(IFNAMSIZ)	/* IFLA_IFNAME */
27942469ffd7SJohn Fastabend 		+ nla_total_size(MAX_ADDR_LEN)	/* IFLA_ADDRESS */
27952469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_MASTER */
27962469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_MTU */
27972469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_LINK */
27982469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_OPERSTATE */
27992469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u8))	/* IFLA_PROTINFO */
28002469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(struct nlattr))	/* IFLA_AF_SPEC */
28012469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u16))	/* IFLA_BRIDGE_FLAGS */
28022469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u16));	/* IFLA_BRIDGE_MODE */
28032469ffd7SJohn Fastabend }
28042469ffd7SJohn Fastabend 
28052469ffd7SJohn Fastabend static int rtnl_bridge_notify(struct net_device *dev, u16 flags)
28062469ffd7SJohn Fastabend {
28072469ffd7SJohn Fastabend 	struct net *net = dev_net(dev);
2808898e5061SJiri Pirko 	struct net_device *br_dev = netdev_master_upper_dev_get(dev);
28092469ffd7SJohn Fastabend 	struct sk_buff *skb;
28102469ffd7SJohn Fastabend 	int err = -EOPNOTSUPP;
28112469ffd7SJohn Fastabend 
28122469ffd7SJohn Fastabend 	skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
28132469ffd7SJohn Fastabend 	if (!skb) {
28142469ffd7SJohn Fastabend 		err = -ENOMEM;
28152469ffd7SJohn Fastabend 		goto errout;
28162469ffd7SJohn Fastabend 	}
28172469ffd7SJohn Fastabend 
2818c38e01b8SJohn Fastabend 	if ((!flags || (flags & BRIDGE_FLAGS_MASTER)) &&
2819898e5061SJiri Pirko 	    br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
28206cbdceebSVlad Yasevich 		err = br_dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0);
28212469ffd7SJohn Fastabend 		if (err < 0)
28222469ffd7SJohn Fastabend 			goto errout;
2823c38e01b8SJohn Fastabend 	}
2824c38e01b8SJohn Fastabend 
2825c38e01b8SJohn Fastabend 	if ((flags & BRIDGE_FLAGS_SELF) &&
2826c38e01b8SJohn Fastabend 	    dev->netdev_ops->ndo_bridge_getlink) {
28276cbdceebSVlad Yasevich 		err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0);
2828c38e01b8SJohn Fastabend 		if (err < 0)
2829c38e01b8SJohn Fastabend 			goto errout;
2830c38e01b8SJohn Fastabend 	}
28312469ffd7SJohn Fastabend 
28322469ffd7SJohn Fastabend 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
28332469ffd7SJohn Fastabend 	return 0;
28342469ffd7SJohn Fastabend errout:
28352469ffd7SJohn Fastabend 	WARN_ON(err == -EMSGSIZE);
28362469ffd7SJohn Fastabend 	kfree_skb(skb);
28372469ffd7SJohn Fastabend 	rtnl_set_sk_err(net, RTNLGRP_LINK, err);
28382469ffd7SJohn Fastabend 	return err;
28392469ffd7SJohn Fastabend }
28402469ffd7SJohn Fastabend 
2841661d2967SThomas Graf static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
2842e5a55a89SJohn Fastabend {
2843e5a55a89SJohn Fastabend 	struct net *net = sock_net(skb->sk);
2844e5a55a89SJohn Fastabend 	struct ifinfomsg *ifm;
2845e5a55a89SJohn Fastabend 	struct net_device *dev;
28462469ffd7SJohn Fastabend 	struct nlattr *br_spec, *attr = NULL;
28472469ffd7SJohn Fastabend 	int rem, err = -EOPNOTSUPP;
2848c38e01b8SJohn Fastabend 	u16 oflags, flags = 0;
2849c38e01b8SJohn Fastabend 	bool have_flags = false;
2850e5a55a89SJohn Fastabend 
2851e5a55a89SJohn Fastabend 	if (nlmsg_len(nlh) < sizeof(*ifm))
2852e5a55a89SJohn Fastabend 		return -EINVAL;
2853e5a55a89SJohn Fastabend 
2854e5a55a89SJohn Fastabend 	ifm = nlmsg_data(nlh);
2855e5a55a89SJohn Fastabend 	if (ifm->ifi_family != AF_BRIDGE)
2856e5a55a89SJohn Fastabend 		return -EPFNOSUPPORT;
2857e5a55a89SJohn Fastabend 
2858e5a55a89SJohn Fastabend 	dev = __dev_get_by_index(net, ifm->ifi_index);
2859e5a55a89SJohn Fastabend 	if (!dev) {
2860e5a55a89SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
2861e5a55a89SJohn Fastabend 		return -ENODEV;
2862e5a55a89SJohn Fastabend 	}
2863e5a55a89SJohn Fastabend 
28642469ffd7SJohn Fastabend 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
28652469ffd7SJohn Fastabend 	if (br_spec) {
28662469ffd7SJohn Fastabend 		nla_for_each_nested(attr, br_spec, rem) {
28672469ffd7SJohn Fastabend 			if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
28686e8d1c55SThomas Graf 				if (nla_len(attr) < sizeof(flags))
28696e8d1c55SThomas Graf 					return -EINVAL;
28706e8d1c55SThomas Graf 
2871c38e01b8SJohn Fastabend 				have_flags = true;
28722469ffd7SJohn Fastabend 				flags = nla_get_u16(attr);
28732469ffd7SJohn Fastabend 				break;
28742469ffd7SJohn Fastabend 			}
28752469ffd7SJohn Fastabend 		}
28762469ffd7SJohn Fastabend 	}
28772469ffd7SJohn Fastabend 
2878c38e01b8SJohn Fastabend 	oflags = flags;
2879c38e01b8SJohn Fastabend 
28802469ffd7SJohn Fastabend 	if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
2881898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2882898e5061SJiri Pirko 
2883898e5061SJiri Pirko 		if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
28842469ffd7SJohn Fastabend 			err = -EOPNOTSUPP;
2885e5a55a89SJohn Fastabend 			goto out;
2886e5a55a89SJohn Fastabend 		}
2887e5a55a89SJohn Fastabend 
2888898e5061SJiri Pirko 		err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh);
28892469ffd7SJohn Fastabend 		if (err)
28902469ffd7SJohn Fastabend 			goto out;
28912469ffd7SJohn Fastabend 
28922469ffd7SJohn Fastabend 		flags &= ~BRIDGE_FLAGS_MASTER;
28932469ffd7SJohn Fastabend 	}
28942469ffd7SJohn Fastabend 
28952469ffd7SJohn Fastabend 	if ((flags & BRIDGE_FLAGS_SELF)) {
28962469ffd7SJohn Fastabend 		if (!dev->netdev_ops->ndo_bridge_setlink)
28972469ffd7SJohn Fastabend 			err = -EOPNOTSUPP;
28982469ffd7SJohn Fastabend 		else
2899e5a55a89SJohn Fastabend 			err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh);
2900e5a55a89SJohn Fastabend 
29012469ffd7SJohn Fastabend 		if (!err)
29022469ffd7SJohn Fastabend 			flags &= ~BRIDGE_FLAGS_SELF;
29032469ffd7SJohn Fastabend 	}
29042469ffd7SJohn Fastabend 
2905c38e01b8SJohn Fastabend 	if (have_flags)
29062469ffd7SJohn Fastabend 		memcpy(nla_data(attr), &flags, sizeof(flags));
29072469ffd7SJohn Fastabend 	/* Generate event to notify upper layer of bridge change */
29082469ffd7SJohn Fastabend 	if (!err)
2909c38e01b8SJohn Fastabend 		err = rtnl_bridge_notify(dev, oflags);
2910e5a55a89SJohn Fastabend out:
2911e5a55a89SJohn Fastabend 	return err;
2912e5a55a89SJohn Fastabend }
2913e5a55a89SJohn Fastabend 
2914661d2967SThomas Graf static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
2915407af329SVlad Yasevich {
2916407af329SVlad Yasevich 	struct net *net = sock_net(skb->sk);
2917407af329SVlad Yasevich 	struct ifinfomsg *ifm;
2918407af329SVlad Yasevich 	struct net_device *dev;
2919407af329SVlad Yasevich 	struct nlattr *br_spec, *attr = NULL;
2920407af329SVlad Yasevich 	int rem, err = -EOPNOTSUPP;
2921407af329SVlad Yasevich 	u16 oflags, flags = 0;
2922407af329SVlad Yasevich 	bool have_flags = false;
2923407af329SVlad Yasevich 
2924407af329SVlad Yasevich 	if (nlmsg_len(nlh) < sizeof(*ifm))
2925407af329SVlad Yasevich 		return -EINVAL;
2926407af329SVlad Yasevich 
2927407af329SVlad Yasevich 	ifm = nlmsg_data(nlh);
2928407af329SVlad Yasevich 	if (ifm->ifi_family != AF_BRIDGE)
2929407af329SVlad Yasevich 		return -EPFNOSUPPORT;
2930407af329SVlad Yasevich 
2931407af329SVlad Yasevich 	dev = __dev_get_by_index(net, ifm->ifi_index);
2932407af329SVlad Yasevich 	if (!dev) {
2933407af329SVlad Yasevich 		pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
2934407af329SVlad Yasevich 		return -ENODEV;
2935407af329SVlad Yasevich 	}
2936407af329SVlad Yasevich 
2937407af329SVlad Yasevich 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
2938407af329SVlad Yasevich 	if (br_spec) {
2939407af329SVlad Yasevich 		nla_for_each_nested(attr, br_spec, rem) {
2940407af329SVlad Yasevich 			if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
29416e8d1c55SThomas Graf 				if (nla_len(attr) < sizeof(flags))
29426e8d1c55SThomas Graf 					return -EINVAL;
29436e8d1c55SThomas Graf 
2944407af329SVlad Yasevich 				have_flags = true;
2945407af329SVlad Yasevich 				flags = nla_get_u16(attr);
2946407af329SVlad Yasevich 				break;
2947407af329SVlad Yasevich 			}
2948407af329SVlad Yasevich 		}
2949407af329SVlad Yasevich 	}
2950407af329SVlad Yasevich 
2951407af329SVlad Yasevich 	oflags = flags;
2952407af329SVlad Yasevich 
2953407af329SVlad Yasevich 	if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
2954407af329SVlad Yasevich 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2955407af329SVlad Yasevich 
2956407af329SVlad Yasevich 		if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
2957407af329SVlad Yasevich 			err = -EOPNOTSUPP;
2958407af329SVlad Yasevich 			goto out;
2959407af329SVlad Yasevich 		}
2960407af329SVlad Yasevich 
2961407af329SVlad Yasevich 		err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh);
2962407af329SVlad Yasevich 		if (err)
2963407af329SVlad Yasevich 			goto out;
2964407af329SVlad Yasevich 
2965407af329SVlad Yasevich 		flags &= ~BRIDGE_FLAGS_MASTER;
2966407af329SVlad Yasevich 	}
2967407af329SVlad Yasevich 
2968407af329SVlad Yasevich 	if ((flags & BRIDGE_FLAGS_SELF)) {
2969407af329SVlad Yasevich 		if (!dev->netdev_ops->ndo_bridge_dellink)
2970407af329SVlad Yasevich 			err = -EOPNOTSUPP;
2971407af329SVlad Yasevich 		else
2972407af329SVlad Yasevich 			err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh);
2973407af329SVlad Yasevich 
2974407af329SVlad Yasevich 		if (!err)
2975407af329SVlad Yasevich 			flags &= ~BRIDGE_FLAGS_SELF;
2976407af329SVlad Yasevich 	}
2977407af329SVlad Yasevich 
2978407af329SVlad Yasevich 	if (have_flags)
2979407af329SVlad Yasevich 		memcpy(nla_data(attr), &flags, sizeof(flags));
2980407af329SVlad Yasevich 	/* Generate event to notify upper layer of bridge change */
2981407af329SVlad Yasevich 	if (!err)
2982407af329SVlad Yasevich 		err = rtnl_bridge_notify(dev, oflags);
2983407af329SVlad Yasevich out:
2984407af329SVlad Yasevich 	return err;
2985407af329SVlad Yasevich }
2986407af329SVlad Yasevich 
29871da177e4SLinus Torvalds /* Process one rtnetlink message. */
29881da177e4SLinus Torvalds 
29891d00a4ebSThomas Graf static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
29901da177e4SLinus Torvalds {
29913b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
2992e2849863SThomas Graf 	rtnl_doit_func doit;
29931da177e4SLinus Torvalds 	int sz_idx, kind;
29941da177e4SLinus Torvalds 	int family;
29951da177e4SLinus Torvalds 	int type;
29962907c35fSEric Dumazet 	int err;
29971da177e4SLinus Torvalds 
29981da177e4SLinus Torvalds 	type = nlh->nlmsg_type;
29991da177e4SLinus Torvalds 	if (type > RTM_MAX)
3000038890feSThomas Graf 		return -EOPNOTSUPP;
30011da177e4SLinus Torvalds 
30021da177e4SLinus Torvalds 	type -= RTM_BASE;
30031da177e4SLinus Torvalds 
30041da177e4SLinus Torvalds 	/* All the messages must have at least 1 byte length */
3005573ce260SHong zhi guo 	if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
30061da177e4SLinus Torvalds 		return 0;
30071da177e4SLinus Torvalds 
3008573ce260SHong zhi guo 	family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
30091da177e4SLinus Torvalds 	sz_idx = type>>2;
30101da177e4SLinus Torvalds 	kind = type&3;
30111da177e4SLinus Torvalds 
301290f62cf3SEric W. Biederman 	if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
30131d00a4ebSThomas Graf 		return -EPERM;
30141da177e4SLinus Torvalds 
3015b8f3ab42SDavid S. Miller 	if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
301697c53cacSDenis V. Lunev 		struct sock *rtnl;
3017e2849863SThomas Graf 		rtnl_dumpit_func dumpit;
3018c7ac8679SGreg Rose 		rtnl_calcit_func calcit;
3019c7ac8679SGreg Rose 		u16 min_dump_alloc = 0;
30201da177e4SLinus Torvalds 
3021e2849863SThomas Graf 		dumpit = rtnl_get_dumpit(family, type);
3022e2849863SThomas Graf 		if (dumpit == NULL)
3023038890feSThomas Graf 			return -EOPNOTSUPP;
3024c7ac8679SGreg Rose 		calcit = rtnl_get_calcit(family, type);
3025c7ac8679SGreg Rose 		if (calcit)
3026115c9b81SGreg Rose 			min_dump_alloc = calcit(skb, nlh);
30271da177e4SLinus Torvalds 
30282907c35fSEric Dumazet 		__rtnl_unlock();
302997c53cacSDenis V. Lunev 		rtnl = net->rtnl;
303080d326faSPablo Neira Ayuso 		{
303180d326faSPablo Neira Ayuso 			struct netlink_dump_control c = {
303280d326faSPablo Neira Ayuso 				.dump		= dumpit,
303380d326faSPablo Neira Ayuso 				.min_dump_alloc	= min_dump_alloc,
303480d326faSPablo Neira Ayuso 			};
303580d326faSPablo Neira Ayuso 			err = netlink_dump_start(rtnl, skb, nlh, &c);
303680d326faSPablo Neira Ayuso 		}
30372907c35fSEric Dumazet 		rtnl_lock();
30382907c35fSEric Dumazet 		return err;
30391da177e4SLinus Torvalds 	}
30401da177e4SLinus Torvalds 
3041e2849863SThomas Graf 	doit = rtnl_get_doit(family, type);
3042e2849863SThomas Graf 	if (doit == NULL)
3043038890feSThomas Graf 		return -EOPNOTSUPP;
30441da177e4SLinus Torvalds 
3045661d2967SThomas Graf 	return doit(skb, nlh);
30461da177e4SLinus Torvalds }
30471da177e4SLinus Torvalds 
3048cd40b7d3SDenis V. Lunev static void rtnetlink_rcv(struct sk_buff *skb)
30491da177e4SLinus Torvalds {
30501536cc0dSDenis V. Lunev 	rtnl_lock();
3051cd40b7d3SDenis V. Lunev 	netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
30521536cc0dSDenis V. Lunev 	rtnl_unlock();
30531da177e4SLinus Torvalds }
30541da177e4SLinus Torvalds 
30551da177e4SLinus Torvalds static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
30561da177e4SLinus Torvalds {
3057351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3058e9dc8653SEric W. Biederman 
30591da177e4SLinus Torvalds 	switch (event) {
30601da177e4SLinus Torvalds 	case NETDEV_UP:
30611da177e4SLinus Torvalds 	case NETDEV_DOWN:
306210de05afSPatrick McHardy 	case NETDEV_PRE_UP:
3063d90a909eSEric W. Biederman 	case NETDEV_POST_INIT:
3064d90a909eSEric W. Biederman 	case NETDEV_REGISTER:
30651da177e4SLinus Torvalds 	case NETDEV_CHANGE:
3066755d0e77SPatrick McHardy 	case NETDEV_PRE_TYPE_CHANGE:
30671da177e4SLinus Torvalds 	case NETDEV_GOING_DOWN:
3068a2835763SPatrick McHardy 	case NETDEV_UNREGISTER:
30690115e8e3SEric Dumazet 	case NETDEV_UNREGISTER_FINAL:
3070ac3d3f81SAmerigo Wang 	case NETDEV_RELEASE:
3071ac3d3f81SAmerigo Wang 	case NETDEV_JOIN:
30721da177e4SLinus Torvalds 		break;
30731da177e4SLinus Torvalds 	default:
30747f294054SAlexei Starovoitov 		rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
30751da177e4SLinus Torvalds 		break;
30761da177e4SLinus Torvalds 	}
30771da177e4SLinus Torvalds 	return NOTIFY_DONE;
30781da177e4SLinus Torvalds }
30791da177e4SLinus Torvalds 
30801da177e4SLinus Torvalds static struct notifier_block rtnetlink_dev_notifier = {
30811da177e4SLinus Torvalds 	.notifier_call	= rtnetlink_event,
30821da177e4SLinus Torvalds };
30831da177e4SLinus Torvalds 
308497c53cacSDenis V. Lunev 
30852c8c1e72SAlexey Dobriyan static int __net_init rtnetlink_net_init(struct net *net)
308697c53cacSDenis V. Lunev {
308797c53cacSDenis V. Lunev 	struct sock *sk;
3088a31f2d17SPablo Neira Ayuso 	struct netlink_kernel_cfg cfg = {
3089a31f2d17SPablo Neira Ayuso 		.groups		= RTNLGRP_MAX,
3090a31f2d17SPablo Neira Ayuso 		.input		= rtnetlink_rcv,
3091a31f2d17SPablo Neira Ayuso 		.cb_mutex	= &rtnl_mutex,
30929785e10aSPablo Neira Ayuso 		.flags		= NL_CFG_F_NONROOT_RECV,
3093a31f2d17SPablo Neira Ayuso 	};
3094a31f2d17SPablo Neira Ayuso 
30959f00d977SPablo Neira Ayuso 	sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
309697c53cacSDenis V. Lunev 	if (!sk)
309797c53cacSDenis V. Lunev 		return -ENOMEM;
309897c53cacSDenis V. Lunev 	net->rtnl = sk;
309997c53cacSDenis V. Lunev 	return 0;
310097c53cacSDenis V. Lunev }
310197c53cacSDenis V. Lunev 
31022c8c1e72SAlexey Dobriyan static void __net_exit rtnetlink_net_exit(struct net *net)
310397c53cacSDenis V. Lunev {
3104b7c6ba6eSDenis V. Lunev 	netlink_kernel_release(net->rtnl);
310597c53cacSDenis V. Lunev 	net->rtnl = NULL;
310697c53cacSDenis V. Lunev }
310797c53cacSDenis V. Lunev 
310897c53cacSDenis V. Lunev static struct pernet_operations rtnetlink_net_ops = {
310997c53cacSDenis V. Lunev 	.init = rtnetlink_net_init,
311097c53cacSDenis V. Lunev 	.exit = rtnetlink_net_exit,
311197c53cacSDenis V. Lunev };
311297c53cacSDenis V. Lunev 
31131da177e4SLinus Torvalds void __init rtnetlink_init(void)
31141da177e4SLinus Torvalds {
311597c53cacSDenis V. Lunev 	if (register_pernet_subsys(&rtnetlink_net_ops))
31161da177e4SLinus Torvalds 		panic("rtnetlink_init: cannot initialize rtnetlink\n");
311797c53cacSDenis V. Lunev 
31181da177e4SLinus Torvalds 	register_netdevice_notifier(&rtnetlink_dev_notifier);
3119340d17fcSThomas Graf 
3120c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
3121c7ac8679SGreg Rose 		      rtnl_dump_ifinfo, rtnl_calcit);
3122c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL);
3123c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL);
3124c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL);
3125687ad8ccSThomas Graf 
3126c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL);
3127c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL);
312877162022SJohn Fastabend 
312977162022SJohn Fastabend 	rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, NULL);
313077162022SJohn Fastabend 	rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, NULL);
313177162022SJohn Fastabend 	rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, NULL);
3132e5a55a89SJohn Fastabend 
3133e5a55a89SJohn Fastabend 	rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, NULL);
3134407af329SVlad Yasevich 	rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, NULL);
3135e5a55a89SJohn Fastabend 	rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, NULL);
31361da177e4SLinus Torvalds }
31371da177e4SLinus Torvalds 
3138