xref: /openbmc/linux/net/core/rtnetlink.c (revision f6f6424ba773da6221ecaaa70973eb4dacfa03b2)
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>
39*f6f6424bSJiri 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>
471da177e4SLinus Torvalds #include <net/ip.h>
481da177e4SLinus Torvalds #include <net/protocol.h>
491da177e4SLinus Torvalds #include <net/arp.h>
501da177e4SLinus Torvalds #include <net/route.h>
511da177e4SLinus Torvalds #include <net/udp.h>
521da177e4SLinus Torvalds #include <net/sock.h>
531da177e4SLinus Torvalds #include <net/pkt_sched.h>
5414c0b97dSThomas Graf #include <net/fib_rules.h>
55e2849863SThomas Graf #include <net/rtnetlink.h>
5630ffee84SJohannes Berg #include <net/net_namespace.h>
571da177e4SLinus Torvalds 
58e0d087afSEric Dumazet struct rtnl_link {
59e2849863SThomas Graf 	rtnl_doit_func		doit;
60e2849863SThomas Graf 	rtnl_dumpit_func	dumpit;
61c7ac8679SGreg Rose 	rtnl_calcit_func 	calcit;
62e2849863SThomas Graf };
63e2849863SThomas Graf 
646756ae4bSStephen Hemminger static DEFINE_MUTEX(rtnl_mutex);
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds void rtnl_lock(void)
671da177e4SLinus Torvalds {
686756ae4bSStephen Hemminger 	mutex_lock(&rtnl_mutex);
691da177e4SLinus Torvalds }
70e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_lock);
711da177e4SLinus Torvalds 
726756ae4bSStephen Hemminger void __rtnl_unlock(void)
731da177e4SLinus Torvalds {
746756ae4bSStephen Hemminger 	mutex_unlock(&rtnl_mutex);
751da177e4SLinus Torvalds }
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds void rtnl_unlock(void)
781da177e4SLinus Torvalds {
7958ec3b4dSHerbert Xu 	/* This fellow will unlock it for us. */
801da177e4SLinus Torvalds 	netdev_run_todo();
811da177e4SLinus Torvalds }
82e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unlock);
831da177e4SLinus Torvalds 
846756ae4bSStephen Hemminger int rtnl_trylock(void)
856756ae4bSStephen Hemminger {
866756ae4bSStephen Hemminger 	return mutex_trylock(&rtnl_mutex);
876756ae4bSStephen Hemminger }
88e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_trylock);
896756ae4bSStephen Hemminger 
90c9c1014bSPatrick McHardy int rtnl_is_locked(void)
91c9c1014bSPatrick McHardy {
92c9c1014bSPatrick McHardy 	return mutex_is_locked(&rtnl_mutex);
93c9c1014bSPatrick McHardy }
94e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_is_locked);
95c9c1014bSPatrick McHardy 
96a898def2SPaul E. McKenney #ifdef CONFIG_PROVE_LOCKING
97a898def2SPaul E. McKenney int lockdep_rtnl_is_held(void)
98a898def2SPaul E. McKenney {
99a898def2SPaul E. McKenney 	return lockdep_is_held(&rtnl_mutex);
100a898def2SPaul E. McKenney }
101a898def2SPaul E. McKenney EXPORT_SYMBOL(lockdep_rtnl_is_held);
102a898def2SPaul E. McKenney #endif /* #ifdef CONFIG_PROVE_LOCKING */
103a898def2SPaul E. McKenney 
10425239ceeSPatrick McHardy static struct rtnl_link *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
105e2849863SThomas Graf 
106e2849863SThomas Graf static inline int rtm_msgindex(int msgtype)
107e2849863SThomas Graf {
108e2849863SThomas Graf 	int msgindex = msgtype - RTM_BASE;
109e2849863SThomas Graf 
110e2849863SThomas Graf 	/*
111e2849863SThomas Graf 	 * msgindex < 0 implies someone tried to register a netlink
112e2849863SThomas Graf 	 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
113e2849863SThomas Graf 	 * the message type has not been added to linux/rtnetlink.h
114e2849863SThomas Graf 	 */
115e2849863SThomas Graf 	BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
116e2849863SThomas Graf 
117e2849863SThomas Graf 	return msgindex;
118e2849863SThomas Graf }
119e2849863SThomas Graf 
120e2849863SThomas Graf static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex)
121e2849863SThomas Graf {
122e2849863SThomas Graf 	struct rtnl_link *tab;
123e2849863SThomas Graf 
12425239ceeSPatrick McHardy 	if (protocol <= RTNL_FAMILY_MAX)
125e2849863SThomas Graf 		tab = rtnl_msg_handlers[protocol];
1260f87b1ddSPatrick McHardy 	else
1270f87b1ddSPatrick McHardy 		tab = NULL;
1280f87b1ddSPatrick McHardy 
12951057f2fSThomas Graf 	if (tab == NULL || tab[msgindex].doit == NULL)
130e2849863SThomas Graf 		tab = rtnl_msg_handlers[PF_UNSPEC];
131e2849863SThomas Graf 
132c80bbeaeSHans Zhang 	return tab[msgindex].doit;
133e2849863SThomas Graf }
134e2849863SThomas Graf 
135e2849863SThomas Graf static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex)
136e2849863SThomas Graf {
137e2849863SThomas Graf 	struct rtnl_link *tab;
138e2849863SThomas Graf 
13925239ceeSPatrick McHardy 	if (protocol <= RTNL_FAMILY_MAX)
140e2849863SThomas Graf 		tab = rtnl_msg_handlers[protocol];
1410f87b1ddSPatrick McHardy 	else
1420f87b1ddSPatrick McHardy 		tab = NULL;
1430f87b1ddSPatrick McHardy 
14451057f2fSThomas Graf 	if (tab == NULL || tab[msgindex].dumpit == NULL)
145e2849863SThomas Graf 		tab = rtnl_msg_handlers[PF_UNSPEC];
146e2849863SThomas Graf 
147c80bbeaeSHans Zhang 	return tab[msgindex].dumpit;
148e2849863SThomas Graf }
149e2849863SThomas Graf 
150c7ac8679SGreg Rose static rtnl_calcit_func rtnl_get_calcit(int protocol, int msgindex)
151c7ac8679SGreg Rose {
152c7ac8679SGreg Rose 	struct rtnl_link *tab;
153c7ac8679SGreg Rose 
154c7ac8679SGreg Rose 	if (protocol <= RTNL_FAMILY_MAX)
155c7ac8679SGreg Rose 		tab = rtnl_msg_handlers[protocol];
156c7ac8679SGreg Rose 	else
157c7ac8679SGreg Rose 		tab = NULL;
158c7ac8679SGreg Rose 
159c7ac8679SGreg Rose 	if (tab == NULL || tab[msgindex].calcit == NULL)
160c7ac8679SGreg Rose 		tab = rtnl_msg_handlers[PF_UNSPEC];
161c7ac8679SGreg Rose 
162c80bbeaeSHans Zhang 	return tab[msgindex].calcit;
163c7ac8679SGreg Rose }
164c7ac8679SGreg Rose 
165e2849863SThomas Graf /**
166e2849863SThomas Graf  * __rtnl_register - Register a rtnetlink message type
167e2849863SThomas Graf  * @protocol: Protocol family or PF_UNSPEC
168e2849863SThomas Graf  * @msgtype: rtnetlink message type
169e2849863SThomas Graf  * @doit: Function pointer called for each request message
170e2849863SThomas Graf  * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
171c7ac8679SGreg Rose  * @calcit: Function pointer to calc size of dump message
172e2849863SThomas Graf  *
173e2849863SThomas Graf  * Registers the specified function pointers (at least one of them has
174e2849863SThomas Graf  * to be non-NULL) to be called whenever a request message for the
175e2849863SThomas Graf  * specified protocol family and message type is received.
176e2849863SThomas Graf  *
177e2849863SThomas Graf  * The special protocol family PF_UNSPEC may be used to define fallback
178e2849863SThomas Graf  * function pointers for the case when no entry for the specific protocol
179e2849863SThomas Graf  * family exists.
180e2849863SThomas Graf  *
181e2849863SThomas Graf  * Returns 0 on success or a negative error code.
182e2849863SThomas Graf  */
183e2849863SThomas Graf int __rtnl_register(int protocol, int msgtype,
184c7ac8679SGreg Rose 		    rtnl_doit_func doit, rtnl_dumpit_func dumpit,
185c7ac8679SGreg Rose 		    rtnl_calcit_func calcit)
186e2849863SThomas Graf {
187e2849863SThomas Graf 	struct rtnl_link *tab;
188e2849863SThomas Graf 	int msgindex;
189e2849863SThomas Graf 
19025239ceeSPatrick McHardy 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
191e2849863SThomas Graf 	msgindex = rtm_msgindex(msgtype);
192e2849863SThomas Graf 
193e2849863SThomas Graf 	tab = rtnl_msg_handlers[protocol];
194e2849863SThomas Graf 	if (tab == NULL) {
195e2849863SThomas Graf 		tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL);
196e2849863SThomas Graf 		if (tab == NULL)
197e2849863SThomas Graf 			return -ENOBUFS;
198e2849863SThomas Graf 
199e2849863SThomas Graf 		rtnl_msg_handlers[protocol] = tab;
200e2849863SThomas Graf 	}
201e2849863SThomas Graf 
202e2849863SThomas Graf 	if (doit)
203e2849863SThomas Graf 		tab[msgindex].doit = doit;
204e2849863SThomas Graf 
205e2849863SThomas Graf 	if (dumpit)
206e2849863SThomas Graf 		tab[msgindex].dumpit = dumpit;
207e2849863SThomas Graf 
208c7ac8679SGreg Rose 	if (calcit)
209c7ac8679SGreg Rose 		tab[msgindex].calcit = calcit;
210c7ac8679SGreg Rose 
211e2849863SThomas Graf 	return 0;
212e2849863SThomas Graf }
213e2849863SThomas Graf EXPORT_SYMBOL_GPL(__rtnl_register);
214e2849863SThomas Graf 
215e2849863SThomas Graf /**
216e2849863SThomas Graf  * rtnl_register - Register a rtnetlink message type
217e2849863SThomas Graf  *
218e2849863SThomas Graf  * Identical to __rtnl_register() but panics on failure. This is useful
219e2849863SThomas Graf  * as failure of this function is very unlikely, it can only happen due
220e2849863SThomas Graf  * to lack of memory when allocating the chain to store all message
221e2849863SThomas Graf  * handlers for a protocol. Meant for use in init functions where lack
22225985edcSLucas De Marchi  * of memory implies no sense in continuing.
223e2849863SThomas Graf  */
224e2849863SThomas Graf void rtnl_register(int protocol, int msgtype,
225c7ac8679SGreg Rose 		   rtnl_doit_func doit, rtnl_dumpit_func dumpit,
226c7ac8679SGreg Rose 		   rtnl_calcit_func calcit)
227e2849863SThomas Graf {
228c7ac8679SGreg Rose 	if (__rtnl_register(protocol, msgtype, doit, dumpit, calcit) < 0)
229e2849863SThomas Graf 		panic("Unable to register rtnetlink message handler, "
230e2849863SThomas Graf 		      "protocol = %d, message type = %d\n",
231e2849863SThomas Graf 		      protocol, msgtype);
232e2849863SThomas Graf }
233e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_register);
234e2849863SThomas Graf 
235e2849863SThomas Graf /**
236e2849863SThomas Graf  * rtnl_unregister - Unregister a rtnetlink message type
237e2849863SThomas Graf  * @protocol: Protocol family or PF_UNSPEC
238e2849863SThomas Graf  * @msgtype: rtnetlink message type
239e2849863SThomas Graf  *
240e2849863SThomas Graf  * Returns 0 on success or a negative error code.
241e2849863SThomas Graf  */
242e2849863SThomas Graf int rtnl_unregister(int protocol, int msgtype)
243e2849863SThomas Graf {
244e2849863SThomas Graf 	int msgindex;
245e2849863SThomas Graf 
24625239ceeSPatrick McHardy 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
247e2849863SThomas Graf 	msgindex = rtm_msgindex(msgtype);
248e2849863SThomas Graf 
249e2849863SThomas Graf 	if (rtnl_msg_handlers[protocol] == NULL)
250e2849863SThomas Graf 		return -ENOENT;
251e2849863SThomas Graf 
252e2849863SThomas Graf 	rtnl_msg_handlers[protocol][msgindex].doit = NULL;
253e2849863SThomas Graf 	rtnl_msg_handlers[protocol][msgindex].dumpit = NULL;
254e2849863SThomas Graf 
255e2849863SThomas Graf 	return 0;
256e2849863SThomas Graf }
257e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister);
258e2849863SThomas Graf 
259e2849863SThomas Graf /**
260e2849863SThomas Graf  * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
261e2849863SThomas Graf  * @protocol : Protocol family or PF_UNSPEC
262e2849863SThomas Graf  *
263e2849863SThomas Graf  * Identical to calling rtnl_unregster() for all registered message types
264e2849863SThomas Graf  * of a certain protocol family.
265e2849863SThomas Graf  */
266e2849863SThomas Graf void rtnl_unregister_all(int protocol)
267e2849863SThomas Graf {
26825239ceeSPatrick McHardy 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
269e2849863SThomas Graf 
270e2849863SThomas Graf 	kfree(rtnl_msg_handlers[protocol]);
271e2849863SThomas Graf 	rtnl_msg_handlers[protocol] = NULL;
272e2849863SThomas Graf }
273e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister_all);
2741da177e4SLinus Torvalds 
27538f7b870SPatrick McHardy static LIST_HEAD(link_ops);
27638f7b870SPatrick McHardy 
277c63044f0SEric Dumazet static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
278c63044f0SEric Dumazet {
279c63044f0SEric Dumazet 	const struct rtnl_link_ops *ops;
280c63044f0SEric Dumazet 
281c63044f0SEric Dumazet 	list_for_each_entry(ops, &link_ops, list) {
282c63044f0SEric Dumazet 		if (!strcmp(ops->kind, kind))
283c63044f0SEric Dumazet 			return ops;
284c63044f0SEric Dumazet 	}
285c63044f0SEric Dumazet 	return NULL;
286c63044f0SEric Dumazet }
287c63044f0SEric Dumazet 
28838f7b870SPatrick McHardy /**
28938f7b870SPatrick McHardy  * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
29038f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to register
29138f7b870SPatrick McHardy  *
29238f7b870SPatrick McHardy  * The caller must hold the rtnl_mutex. This function should be used
29338f7b870SPatrick McHardy  * by drivers that create devices during module initialization. It
29438f7b870SPatrick McHardy  * must be called before registering the devices.
29538f7b870SPatrick McHardy  *
29638f7b870SPatrick McHardy  * Returns 0 on success or a negative error code.
29738f7b870SPatrick McHardy  */
29838f7b870SPatrick McHardy int __rtnl_link_register(struct rtnl_link_ops *ops)
29938f7b870SPatrick McHardy {
300c63044f0SEric Dumazet 	if (rtnl_link_ops_get(ops->kind))
301c63044f0SEric Dumazet 		return -EEXIST;
302c63044f0SEric Dumazet 
303b0ab2fabSJiri Pirko 	/* The check for setup is here because if ops
304b0ab2fabSJiri Pirko 	 * does not have that filled up, it is not possible
305b0ab2fabSJiri Pirko 	 * to use the ops for creating device. So do not
306b0ab2fabSJiri Pirko 	 * fill up dellink as well. That disables rtnl_dellink.
307b0ab2fabSJiri Pirko 	 */
308b0ab2fabSJiri Pirko 	if (ops->setup && !ops->dellink)
30923289a37SEric Dumazet 		ops->dellink = unregister_netdevice_queue;
3102d85cba2SPatrick McHardy 
31138f7b870SPatrick McHardy 	list_add_tail(&ops->list, &link_ops);
31238f7b870SPatrick McHardy 	return 0;
31338f7b870SPatrick McHardy }
31438f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_register);
31538f7b870SPatrick McHardy 
31638f7b870SPatrick McHardy /**
31738f7b870SPatrick McHardy  * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
31838f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to register
31938f7b870SPatrick McHardy  *
32038f7b870SPatrick McHardy  * Returns 0 on success or a negative error code.
32138f7b870SPatrick McHardy  */
32238f7b870SPatrick McHardy int rtnl_link_register(struct rtnl_link_ops *ops)
32338f7b870SPatrick McHardy {
32438f7b870SPatrick McHardy 	int err;
32538f7b870SPatrick McHardy 
32638f7b870SPatrick McHardy 	rtnl_lock();
32738f7b870SPatrick McHardy 	err = __rtnl_link_register(ops);
32838f7b870SPatrick McHardy 	rtnl_unlock();
32938f7b870SPatrick McHardy 	return err;
33038f7b870SPatrick McHardy }
33138f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_register);
33238f7b870SPatrick McHardy 
333669f87baSPavel Emelyanov static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
334669f87baSPavel Emelyanov {
335669f87baSPavel Emelyanov 	struct net_device *dev;
33623289a37SEric Dumazet 	LIST_HEAD(list_kill);
33723289a37SEric Dumazet 
338669f87baSPavel Emelyanov 	for_each_netdev(net, dev) {
33923289a37SEric Dumazet 		if (dev->rtnl_link_ops == ops)
34023289a37SEric Dumazet 			ops->dellink(dev, &list_kill);
341669f87baSPavel Emelyanov 	}
34223289a37SEric Dumazet 	unregister_netdevice_many(&list_kill);
343669f87baSPavel Emelyanov }
344669f87baSPavel Emelyanov 
34538f7b870SPatrick McHardy /**
34638f7b870SPatrick McHardy  * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
34738f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to unregister
34838f7b870SPatrick McHardy  *
3492d85cba2SPatrick McHardy  * The caller must hold the rtnl_mutex.
35038f7b870SPatrick McHardy  */
35138f7b870SPatrick McHardy void __rtnl_link_unregister(struct rtnl_link_ops *ops)
35238f7b870SPatrick McHardy {
353881d966bSEric W. Biederman 	struct net *net;
3542d85cba2SPatrick McHardy 
355881d966bSEric W. Biederman 	for_each_net(net) {
356669f87baSPavel Emelyanov 		__rtnl_kill_links(net, ops);
357881d966bSEric W. Biederman 	}
35838f7b870SPatrick McHardy 	list_del(&ops->list);
35938f7b870SPatrick McHardy }
36038f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
36138f7b870SPatrick McHardy 
362200b916fSCong Wang /* Return with the rtnl_lock held when there are no network
363200b916fSCong Wang  * devices unregistering in any network namespace.
364200b916fSCong Wang  */
365200b916fSCong Wang static void rtnl_lock_unregistering_all(void)
366200b916fSCong Wang {
367200b916fSCong Wang 	struct net *net;
368200b916fSCong Wang 	bool unregistering;
369200b916fSCong Wang 	DEFINE_WAIT(wait);
370200b916fSCong Wang 
371200b916fSCong Wang 	for (;;) {
372200b916fSCong Wang 		prepare_to_wait(&netdev_unregistering_wq, &wait,
373200b916fSCong Wang 				TASK_UNINTERRUPTIBLE);
374200b916fSCong Wang 		unregistering = false;
375200b916fSCong Wang 		rtnl_lock();
376200b916fSCong Wang 		for_each_net(net) {
377200b916fSCong Wang 			if (net->dev_unreg_count > 0) {
378200b916fSCong Wang 				unregistering = true;
379200b916fSCong Wang 				break;
380200b916fSCong Wang 			}
381200b916fSCong Wang 		}
382200b916fSCong Wang 		if (!unregistering)
383200b916fSCong Wang 			break;
384200b916fSCong Wang 		__rtnl_unlock();
385200b916fSCong Wang 		schedule();
386200b916fSCong Wang 	}
387200b916fSCong Wang 	finish_wait(&netdev_unregistering_wq, &wait);
388200b916fSCong Wang }
389200b916fSCong Wang 
39038f7b870SPatrick McHardy /**
39138f7b870SPatrick McHardy  * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
39238f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to unregister
39338f7b870SPatrick McHardy  */
39438f7b870SPatrick McHardy void rtnl_link_unregister(struct rtnl_link_ops *ops)
39538f7b870SPatrick McHardy {
396200b916fSCong Wang 	/* Close the race with cleanup_net() */
397200b916fSCong Wang 	mutex_lock(&net_mutex);
398200b916fSCong Wang 	rtnl_lock_unregistering_all();
39938f7b870SPatrick McHardy 	__rtnl_link_unregister(ops);
40038f7b870SPatrick McHardy 	rtnl_unlock();
401200b916fSCong Wang 	mutex_unlock(&net_mutex);
40238f7b870SPatrick McHardy }
40338f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_unregister);
40438f7b870SPatrick McHardy 
405ba7d49b1SJiri Pirko static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
406ba7d49b1SJiri Pirko {
407ba7d49b1SJiri Pirko 	struct net_device *master_dev;
408ba7d49b1SJiri Pirko 	const struct rtnl_link_ops *ops;
409ba7d49b1SJiri Pirko 
410ba7d49b1SJiri Pirko 	master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
411ba7d49b1SJiri Pirko 	if (!master_dev)
412ba7d49b1SJiri Pirko 		return 0;
413ba7d49b1SJiri Pirko 	ops = master_dev->rtnl_link_ops;
4146049f253SFernando Luis Vazquez Cao 	if (!ops || !ops->get_slave_size)
415ba7d49b1SJiri Pirko 		return 0;
416ba7d49b1SJiri Pirko 	/* IFLA_INFO_SLAVE_DATA + nested data */
417ba7d49b1SJiri Pirko 	return nla_total_size(sizeof(struct nlattr)) +
418ba7d49b1SJiri Pirko 	       ops->get_slave_size(master_dev, dev);
419ba7d49b1SJiri Pirko }
420ba7d49b1SJiri Pirko 
42138f7b870SPatrick McHardy static size_t rtnl_link_get_size(const struct net_device *dev)
42238f7b870SPatrick McHardy {
42338f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
42438f7b870SPatrick McHardy 	size_t size;
42538f7b870SPatrick McHardy 
42638f7b870SPatrick McHardy 	if (!ops)
42738f7b870SPatrick McHardy 		return 0;
42838f7b870SPatrick McHardy 
429369cf77aSThomas Graf 	size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
430369cf77aSThomas Graf 	       nla_total_size(strlen(ops->kind) + 1);  /* IFLA_INFO_KIND */
43138f7b870SPatrick McHardy 
43238f7b870SPatrick McHardy 	if (ops->get_size)
43338f7b870SPatrick McHardy 		/* IFLA_INFO_DATA + nested data */
434369cf77aSThomas Graf 		size += nla_total_size(sizeof(struct nlattr)) +
43538f7b870SPatrick McHardy 			ops->get_size(dev);
43638f7b870SPatrick McHardy 
43738f7b870SPatrick McHardy 	if (ops->get_xstats_size)
438369cf77aSThomas Graf 		/* IFLA_INFO_XSTATS */
439369cf77aSThomas Graf 		size += nla_total_size(ops->get_xstats_size(dev));
44038f7b870SPatrick McHardy 
441ba7d49b1SJiri Pirko 	size += rtnl_link_get_slave_info_data_size(dev);
442ba7d49b1SJiri Pirko 
44338f7b870SPatrick McHardy 	return size;
44438f7b870SPatrick McHardy }
44538f7b870SPatrick McHardy 
446f8ff182cSThomas Graf static LIST_HEAD(rtnl_af_ops);
447f8ff182cSThomas Graf 
448f8ff182cSThomas Graf static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
449f8ff182cSThomas Graf {
450f8ff182cSThomas Graf 	const struct rtnl_af_ops *ops;
451f8ff182cSThomas Graf 
452f8ff182cSThomas Graf 	list_for_each_entry(ops, &rtnl_af_ops, list) {
453f8ff182cSThomas Graf 		if (ops->family == family)
454f8ff182cSThomas Graf 			return ops;
455f8ff182cSThomas Graf 	}
456f8ff182cSThomas Graf 
457f8ff182cSThomas Graf 	return NULL;
458f8ff182cSThomas Graf }
459f8ff182cSThomas Graf 
460f8ff182cSThomas Graf /**
461f8ff182cSThomas Graf  * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
462f8ff182cSThomas Graf  * @ops: struct rtnl_af_ops * to register
463f8ff182cSThomas Graf  *
464f8ff182cSThomas Graf  * Returns 0 on success or a negative error code.
465f8ff182cSThomas Graf  */
4663678a9d8Sstephen hemminger void rtnl_af_register(struct rtnl_af_ops *ops)
467f8ff182cSThomas Graf {
468f8ff182cSThomas Graf 	rtnl_lock();
4693678a9d8Sstephen hemminger 	list_add_tail(&ops->list, &rtnl_af_ops);
470f8ff182cSThomas Graf 	rtnl_unlock();
471f8ff182cSThomas Graf }
472f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_register);
473f8ff182cSThomas Graf 
474f8ff182cSThomas Graf /**
475f8ff182cSThomas Graf  * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
476f8ff182cSThomas Graf  * @ops: struct rtnl_af_ops * to unregister
477f8ff182cSThomas Graf  *
478f8ff182cSThomas Graf  * The caller must hold the rtnl_mutex.
479f8ff182cSThomas Graf  */
480f8ff182cSThomas Graf void __rtnl_af_unregister(struct rtnl_af_ops *ops)
481f8ff182cSThomas Graf {
482f8ff182cSThomas Graf 	list_del(&ops->list);
483f8ff182cSThomas Graf }
484f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(__rtnl_af_unregister);
485f8ff182cSThomas Graf 
486f8ff182cSThomas Graf /**
487f8ff182cSThomas Graf  * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
488f8ff182cSThomas Graf  * @ops: struct rtnl_af_ops * to unregister
489f8ff182cSThomas Graf  */
490f8ff182cSThomas Graf void rtnl_af_unregister(struct rtnl_af_ops *ops)
491f8ff182cSThomas Graf {
492f8ff182cSThomas Graf 	rtnl_lock();
493f8ff182cSThomas Graf 	__rtnl_af_unregister(ops);
494f8ff182cSThomas Graf 	rtnl_unlock();
495f8ff182cSThomas Graf }
496f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_unregister);
497f8ff182cSThomas Graf 
498f8ff182cSThomas Graf static size_t rtnl_link_get_af_size(const struct net_device *dev)
499f8ff182cSThomas Graf {
500f8ff182cSThomas Graf 	struct rtnl_af_ops *af_ops;
501f8ff182cSThomas Graf 	size_t size;
502f8ff182cSThomas Graf 
503f8ff182cSThomas Graf 	/* IFLA_AF_SPEC */
504f8ff182cSThomas Graf 	size = nla_total_size(sizeof(struct nlattr));
505f8ff182cSThomas Graf 
506f8ff182cSThomas Graf 	list_for_each_entry(af_ops, &rtnl_af_ops, list) {
507f8ff182cSThomas Graf 		if (af_ops->get_link_af_size) {
508f8ff182cSThomas Graf 			/* AF_* + nested data */
509f8ff182cSThomas Graf 			size += nla_total_size(sizeof(struct nlattr)) +
510f8ff182cSThomas Graf 				af_ops->get_link_af_size(dev);
511f8ff182cSThomas Graf 		}
512f8ff182cSThomas Graf 	}
513f8ff182cSThomas Graf 
514f8ff182cSThomas Graf 	return size;
515f8ff182cSThomas Graf }
516f8ff182cSThomas Graf 
517ba7d49b1SJiri Pirko static bool rtnl_have_link_slave_info(const struct net_device *dev)
518ba7d49b1SJiri Pirko {
519ba7d49b1SJiri Pirko 	struct net_device *master_dev;
520ba7d49b1SJiri Pirko 
521ba7d49b1SJiri Pirko 	master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
522813f020cSJiri Pirko 	if (master_dev && master_dev->rtnl_link_ops)
523ba7d49b1SJiri Pirko 		return true;
524ba7d49b1SJiri Pirko 	return false;
525ba7d49b1SJiri Pirko }
526ba7d49b1SJiri Pirko 
527ba7d49b1SJiri Pirko static int rtnl_link_slave_info_fill(struct sk_buff *skb,
528ba7d49b1SJiri Pirko 				     const struct net_device *dev)
529ba7d49b1SJiri Pirko {
530ba7d49b1SJiri Pirko 	struct net_device *master_dev;
531ba7d49b1SJiri Pirko 	const struct rtnl_link_ops *ops;
532ba7d49b1SJiri Pirko 	struct nlattr *slave_data;
533ba7d49b1SJiri Pirko 	int err;
534ba7d49b1SJiri Pirko 
535ba7d49b1SJiri Pirko 	master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
536ba7d49b1SJiri Pirko 	if (!master_dev)
537ba7d49b1SJiri Pirko 		return 0;
538ba7d49b1SJiri Pirko 	ops = master_dev->rtnl_link_ops;
539ba7d49b1SJiri Pirko 	if (!ops)
540ba7d49b1SJiri Pirko 		return 0;
541ba7d49b1SJiri Pirko 	if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
542ba7d49b1SJiri Pirko 		return -EMSGSIZE;
543ba7d49b1SJiri Pirko 	if (ops->fill_slave_info) {
544ba7d49b1SJiri Pirko 		slave_data = nla_nest_start(skb, IFLA_INFO_SLAVE_DATA);
545ba7d49b1SJiri Pirko 		if (!slave_data)
546ba7d49b1SJiri Pirko 			return -EMSGSIZE;
547ba7d49b1SJiri Pirko 		err = ops->fill_slave_info(skb, master_dev, dev);
548ba7d49b1SJiri Pirko 		if (err < 0)
549ba7d49b1SJiri Pirko 			goto err_cancel_slave_data;
550ba7d49b1SJiri Pirko 		nla_nest_end(skb, slave_data);
551ba7d49b1SJiri Pirko 	}
552ba7d49b1SJiri Pirko 	return 0;
553ba7d49b1SJiri Pirko 
554ba7d49b1SJiri Pirko err_cancel_slave_data:
555ba7d49b1SJiri Pirko 	nla_nest_cancel(skb, slave_data);
556ba7d49b1SJiri Pirko 	return err;
557ba7d49b1SJiri Pirko }
558ba7d49b1SJiri Pirko 
559ba7d49b1SJiri Pirko static int rtnl_link_info_fill(struct sk_buff *skb,
560ba7d49b1SJiri Pirko 			       const struct net_device *dev)
56138f7b870SPatrick McHardy {
56238f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
563ba7d49b1SJiri Pirko 	struct nlattr *data;
564ba7d49b1SJiri Pirko 	int err;
565ba7d49b1SJiri Pirko 
566ba7d49b1SJiri Pirko 	if (!ops)
567ba7d49b1SJiri Pirko 		return 0;
568ba7d49b1SJiri Pirko 	if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
569ba7d49b1SJiri Pirko 		return -EMSGSIZE;
570ba7d49b1SJiri Pirko 	if (ops->fill_xstats) {
571ba7d49b1SJiri Pirko 		err = ops->fill_xstats(skb, dev);
572ba7d49b1SJiri Pirko 		if (err < 0)
573ba7d49b1SJiri Pirko 			return err;
574ba7d49b1SJiri Pirko 	}
575ba7d49b1SJiri Pirko 	if (ops->fill_info) {
576ba7d49b1SJiri Pirko 		data = nla_nest_start(skb, IFLA_INFO_DATA);
577ba7d49b1SJiri Pirko 		if (data == NULL)
578ba7d49b1SJiri Pirko 			return -EMSGSIZE;
579ba7d49b1SJiri Pirko 		err = ops->fill_info(skb, dev);
580ba7d49b1SJiri Pirko 		if (err < 0)
581ba7d49b1SJiri Pirko 			goto err_cancel_data;
582ba7d49b1SJiri Pirko 		nla_nest_end(skb, data);
583ba7d49b1SJiri Pirko 	}
584ba7d49b1SJiri Pirko 	return 0;
585ba7d49b1SJiri Pirko 
586ba7d49b1SJiri Pirko err_cancel_data:
587ba7d49b1SJiri Pirko 	nla_nest_cancel(skb, data);
588ba7d49b1SJiri Pirko 	return err;
589ba7d49b1SJiri Pirko }
590ba7d49b1SJiri Pirko 
591ba7d49b1SJiri Pirko static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
592ba7d49b1SJiri Pirko {
593ba7d49b1SJiri Pirko 	struct nlattr *linkinfo;
59438f7b870SPatrick McHardy 	int err = -EMSGSIZE;
59538f7b870SPatrick McHardy 
59638f7b870SPatrick McHardy 	linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
59738f7b870SPatrick McHardy 	if (linkinfo == NULL)
59838f7b870SPatrick McHardy 		goto out;
59938f7b870SPatrick McHardy 
600ba7d49b1SJiri Pirko 	err = rtnl_link_info_fill(skb, dev);
60138f7b870SPatrick McHardy 	if (err < 0)
60238f7b870SPatrick McHardy 		goto err_cancel_link;
603ba7d49b1SJiri Pirko 
604ba7d49b1SJiri Pirko 	err = rtnl_link_slave_info_fill(skb, dev);
60538f7b870SPatrick McHardy 	if (err < 0)
606ba7d49b1SJiri Pirko 		goto err_cancel_link;
60738f7b870SPatrick McHardy 
60838f7b870SPatrick McHardy 	nla_nest_end(skb, linkinfo);
60938f7b870SPatrick McHardy 	return 0;
61038f7b870SPatrick McHardy 
61138f7b870SPatrick McHardy err_cancel_link:
61238f7b870SPatrick McHardy 	nla_nest_cancel(skb, linkinfo);
61338f7b870SPatrick McHardy out:
61438f7b870SPatrick McHardy 	return err;
61538f7b870SPatrick McHardy }
61638f7b870SPatrick McHardy 
61795c96174SEric Dumazet int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
6181da177e4SLinus Torvalds {
61997c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
6201da177e4SLinus Torvalds 	int err = 0;
6211da177e4SLinus Torvalds 
622ac6d439dSPatrick McHardy 	NETLINK_CB(skb).dst_group = group;
6231da177e4SLinus Torvalds 	if (echo)
6241da177e4SLinus Torvalds 		atomic_inc(&skb->users);
6251da177e4SLinus Torvalds 	netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
6261da177e4SLinus Torvalds 	if (echo)
6271da177e4SLinus Torvalds 		err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
6281da177e4SLinus Torvalds 	return err;
6291da177e4SLinus Torvalds }
6301da177e4SLinus Torvalds 
63197c53cacSDenis V. Lunev int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
6322942e900SThomas Graf {
63397c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
63497c53cacSDenis V. Lunev 
6352942e900SThomas Graf 	return nlmsg_unicast(rtnl, skb, pid);
6362942e900SThomas Graf }
637e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unicast);
6382942e900SThomas Graf 
6391ce85fe4SPablo Neira Ayuso void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
64097676b6bSThomas Graf 		 struct nlmsghdr *nlh, gfp_t flags)
64197676b6bSThomas Graf {
64297c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
64397676b6bSThomas Graf 	int report = 0;
64497676b6bSThomas Graf 
64597676b6bSThomas Graf 	if (nlh)
64697676b6bSThomas Graf 		report = nlmsg_report(nlh);
64797676b6bSThomas Graf 
6481ce85fe4SPablo Neira Ayuso 	nlmsg_notify(rtnl, skb, pid, group, report, flags);
64997676b6bSThomas Graf }
650e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_notify);
65197676b6bSThomas Graf 
65297c53cacSDenis V. Lunev void rtnl_set_sk_err(struct net *net, u32 group, int error)
65397676b6bSThomas Graf {
65497c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
65597c53cacSDenis V. Lunev 
65697676b6bSThomas Graf 	netlink_set_err(rtnl, 0, group, error);
65797676b6bSThomas Graf }
658e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_set_sk_err);
65997676b6bSThomas Graf 
6601da177e4SLinus Torvalds int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
6611da177e4SLinus Torvalds {
6622d7202bfSThomas Graf 	struct nlattr *mx;
6632d7202bfSThomas Graf 	int i, valid = 0;
6641da177e4SLinus Torvalds 
6652d7202bfSThomas Graf 	mx = nla_nest_start(skb, RTA_METRICS);
6662d7202bfSThomas Graf 	if (mx == NULL)
6672d7202bfSThomas Graf 		return -ENOBUFS;
6682d7202bfSThomas Graf 
6691da177e4SLinus Torvalds 	for (i = 0; i < RTAX_MAX; i++) {
6702d7202bfSThomas Graf 		if (metrics[i]) {
6712d7202bfSThomas Graf 			valid++;
672a6574349SDavid S. Miller 			if (nla_put_u32(skb, i+1, metrics[i]))
673a6574349SDavid S. Miller 				goto nla_put_failure;
6741da177e4SLinus Torvalds 		}
6752d7202bfSThomas Graf 	}
6761da177e4SLinus Torvalds 
677a57d27fcSDavid S. Miller 	if (!valid) {
678a57d27fcSDavid S. Miller 		nla_nest_cancel(skb, mx);
679a57d27fcSDavid S. Miller 		return 0;
680a57d27fcSDavid S. Miller 	}
6812d7202bfSThomas Graf 
6822d7202bfSThomas Graf 	return nla_nest_end(skb, mx);
6832d7202bfSThomas Graf 
6842d7202bfSThomas Graf nla_put_failure:
685bc3ed28cSThomas Graf 	nla_nest_cancel(skb, mx);
686bc3ed28cSThomas Graf 	return -EMSGSIZE;
6871da177e4SLinus Torvalds }
688e0d087afSEric Dumazet EXPORT_SYMBOL(rtnetlink_put_metrics);
6891da177e4SLinus Torvalds 
690e3703b3dSThomas Graf int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
69187a50699SDavid S. Miller 		       long expires, u32 error)
692e3703b3dSThomas Graf {
693e3703b3dSThomas Graf 	struct rta_cacheinfo ci = {
694a399a805SEric Dumazet 		.rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse),
695e3703b3dSThomas Graf 		.rta_used = dst->__use,
696e3703b3dSThomas Graf 		.rta_clntref = atomic_read(&(dst->__refcnt)),
697e3703b3dSThomas Graf 		.rta_error = error,
698e3703b3dSThomas Graf 		.rta_id =  id,
699e3703b3dSThomas Graf 	};
700e3703b3dSThomas Graf 
7018253947eSLi Wei 	if (expires) {
7028253947eSLi Wei 		unsigned long clock;
703e3703b3dSThomas Graf 
7048253947eSLi Wei 		clock = jiffies_to_clock_t(abs(expires));
7058253947eSLi Wei 		clock = min_t(unsigned long, clock, INT_MAX);
7068253947eSLi Wei 		ci.rta_expires = (expires > 0) ? clock : -clock;
7078253947eSLi Wei 	}
708e3703b3dSThomas Graf 	return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
709e3703b3dSThomas Graf }
710e3703b3dSThomas Graf EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
7111da177e4SLinus Torvalds 
71293b2d4a2SDavid S. Miller static void set_operstate(struct net_device *dev, unsigned char transition)
713b00055aaSStefan Rompf {
714b00055aaSStefan Rompf 	unsigned char operstate = dev->operstate;
715b00055aaSStefan Rompf 
716b00055aaSStefan Rompf 	switch (transition) {
717b00055aaSStefan Rompf 	case IF_OPER_UP:
718b00055aaSStefan Rompf 		if ((operstate == IF_OPER_DORMANT ||
719b00055aaSStefan Rompf 		     operstate == IF_OPER_UNKNOWN) &&
720b00055aaSStefan Rompf 		    !netif_dormant(dev))
721b00055aaSStefan Rompf 			operstate = IF_OPER_UP;
722b00055aaSStefan Rompf 		break;
723b00055aaSStefan Rompf 
724b00055aaSStefan Rompf 	case IF_OPER_DORMANT:
725b00055aaSStefan Rompf 		if (operstate == IF_OPER_UP ||
726b00055aaSStefan Rompf 		    operstate == IF_OPER_UNKNOWN)
727b00055aaSStefan Rompf 			operstate = IF_OPER_DORMANT;
728b00055aaSStefan Rompf 		break;
7293ff50b79SStephen Hemminger 	}
730b00055aaSStefan Rompf 
731b00055aaSStefan Rompf 	if (dev->operstate != operstate) {
732b00055aaSStefan Rompf 		write_lock_bh(&dev_base_lock);
733b00055aaSStefan Rompf 		dev->operstate = operstate;
734b00055aaSStefan Rompf 		write_unlock_bh(&dev_base_lock);
735b00055aaSStefan Rompf 		netdev_state_change(dev);
73693b2d4a2SDavid S. Miller 	}
737b00055aaSStefan Rompf }
738b00055aaSStefan Rompf 
739b1beb681SJiri Benc static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
740b1beb681SJiri Benc {
741b1beb681SJiri Benc 	return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
742b1beb681SJiri Benc 	       (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
743b1beb681SJiri Benc }
744b1beb681SJiri Benc 
7453729d502SPatrick McHardy static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
7463729d502SPatrick McHardy 					   const struct ifinfomsg *ifm)
7473729d502SPatrick McHardy {
7483729d502SPatrick McHardy 	unsigned int flags = ifm->ifi_flags;
7493729d502SPatrick McHardy 
7503729d502SPatrick McHardy 	/* bugwards compatibility: ifi_change == 0 is treated as ~0 */
7513729d502SPatrick McHardy 	if (ifm->ifi_change)
7523729d502SPatrick McHardy 		flags = (flags & ifm->ifi_change) |
753b1beb681SJiri Benc 			(rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
7543729d502SPatrick McHardy 
7553729d502SPatrick McHardy 	return flags;
7563729d502SPatrick McHardy }
7573729d502SPatrick McHardy 
758b60c5115SThomas Graf static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
759be1f3c2cSBen Hutchings 				 const struct rtnl_link_stats64 *b)
7601da177e4SLinus Torvalds {
761b60c5115SThomas Graf 	a->rx_packets = b->rx_packets;
762b60c5115SThomas Graf 	a->tx_packets = b->tx_packets;
763b60c5115SThomas Graf 	a->rx_bytes = b->rx_bytes;
764b60c5115SThomas Graf 	a->tx_bytes = b->tx_bytes;
765b60c5115SThomas Graf 	a->rx_errors = b->rx_errors;
766b60c5115SThomas Graf 	a->tx_errors = b->tx_errors;
767b60c5115SThomas Graf 	a->rx_dropped = b->rx_dropped;
768b60c5115SThomas Graf 	a->tx_dropped = b->tx_dropped;
769b60c5115SThomas Graf 
770b60c5115SThomas Graf 	a->multicast = b->multicast;
771b60c5115SThomas Graf 	a->collisions = b->collisions;
772b60c5115SThomas Graf 
773b60c5115SThomas Graf 	a->rx_length_errors = b->rx_length_errors;
774b60c5115SThomas Graf 	a->rx_over_errors = b->rx_over_errors;
775b60c5115SThomas Graf 	a->rx_crc_errors = b->rx_crc_errors;
776b60c5115SThomas Graf 	a->rx_frame_errors = b->rx_frame_errors;
777b60c5115SThomas Graf 	a->rx_fifo_errors = b->rx_fifo_errors;
778b60c5115SThomas Graf 	a->rx_missed_errors = b->rx_missed_errors;
779b60c5115SThomas Graf 
780b60c5115SThomas Graf 	a->tx_aborted_errors = b->tx_aborted_errors;
781b60c5115SThomas Graf 	a->tx_carrier_errors = b->tx_carrier_errors;
782b60c5115SThomas Graf 	a->tx_fifo_errors = b->tx_fifo_errors;
783b60c5115SThomas Graf 	a->tx_heartbeat_errors = b->tx_heartbeat_errors;
784b60c5115SThomas Graf 	a->tx_window_errors = b->tx_window_errors;
785b60c5115SThomas Graf 
786b60c5115SThomas Graf 	a->rx_compressed = b->rx_compressed;
787b60c5115SThomas Graf 	a->tx_compressed = b->tx_compressed;
78810708f37SJan Engelhardt }
78910708f37SJan Engelhardt 
790be1f3c2cSBen Hutchings static void copy_rtnl_link_stats64(void *v, const struct rtnl_link_stats64 *b)
79110708f37SJan Engelhardt {
792afdcba37SEric Dumazet 	memcpy(v, b, sizeof(*b));
79310708f37SJan Engelhardt }
794b60c5115SThomas Graf 
795c02db8c6SChris Wright /* All VF info */
796115c9b81SGreg Rose static inline int rtnl_vfinfo_size(const struct net_device *dev,
797115c9b81SGreg Rose 				   u32 ext_filter_mask)
798ebc08a6fSWilliams, Mitch A {
799115c9b81SGreg Rose 	if (dev->dev.parent && dev_is_pci(dev->dev.parent) &&
800115c9b81SGreg Rose 	    (ext_filter_mask & RTEXT_FILTER_VF)) {
801c02db8c6SChris Wright 		int num_vfs = dev_num_vf(dev->dev.parent);
802045de01aSScott Feldman 		size_t size = nla_total_size(sizeof(struct nlattr));
803045de01aSScott Feldman 		size += nla_total_size(num_vfs * sizeof(struct nlattr));
804045de01aSScott Feldman 		size += num_vfs *
805045de01aSScott Feldman 			(nla_total_size(sizeof(struct ifla_vf_mac)) +
806045de01aSScott Feldman 			 nla_total_size(sizeof(struct ifla_vf_vlan)) +
807ed616689SSucheta Chakraborty 			 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
808945a3676SJiri Benc 			 nla_total_size(sizeof(struct ifla_vf_rate)) +
809945a3676SJiri Benc 			 nla_total_size(sizeof(struct ifla_vf_link_state)));
810c02db8c6SChris Wright 		return size;
811c02db8c6SChris Wright 	} else
812ebc08a6fSWilliams, Mitch A 		return 0;
813ebc08a6fSWilliams, Mitch A }
814ebc08a6fSWilliams, Mitch A 
815c53864fdSDavid Gibson static size_t rtnl_port_size(const struct net_device *dev,
816c53864fdSDavid Gibson 			     u32 ext_filter_mask)
81757b61080SScott Feldman {
81857b61080SScott Feldman 	size_t port_size = nla_total_size(4)		/* PORT_VF */
81957b61080SScott Feldman 		+ nla_total_size(PORT_PROFILE_MAX)	/* PORT_PROFILE */
82057b61080SScott Feldman 		+ nla_total_size(sizeof(struct ifla_port_vsi))
82157b61080SScott Feldman 							/* PORT_VSI_TYPE */
82257b61080SScott Feldman 		+ nla_total_size(PORT_UUID_MAX)		/* PORT_INSTANCE_UUID */
82357b61080SScott Feldman 		+ nla_total_size(PORT_UUID_MAX)		/* PORT_HOST_UUID */
82457b61080SScott Feldman 		+ nla_total_size(1)			/* PROT_VDP_REQUEST */
82557b61080SScott Feldman 		+ nla_total_size(2);			/* PORT_VDP_RESPONSE */
82657b61080SScott Feldman 	size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
82757b61080SScott Feldman 	size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
82857b61080SScott Feldman 		+ port_size;
82957b61080SScott Feldman 	size_t port_self_size = nla_total_size(sizeof(struct nlattr))
83057b61080SScott Feldman 		+ port_size;
83157b61080SScott Feldman 
832c53864fdSDavid Gibson 	if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
833c53864fdSDavid Gibson 	    !(ext_filter_mask & RTEXT_FILTER_VF))
83457b61080SScott Feldman 		return 0;
83557b61080SScott Feldman 	if (dev_num_vf(dev->dev.parent))
83657b61080SScott Feldman 		return port_self_size + vf_ports_size +
83757b61080SScott Feldman 			vf_port_size * dev_num_vf(dev->dev.parent);
83857b61080SScott Feldman 	else
83957b61080SScott Feldman 		return port_self_size;
84057b61080SScott Feldman }
84157b61080SScott Feldman 
842115c9b81SGreg Rose static noinline size_t if_nlmsg_size(const struct net_device *dev,
843115c9b81SGreg Rose 				     u32 ext_filter_mask)
844339bf98fSThomas Graf {
845339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
846339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
8470b815a1aSStephen Hemminger 	       + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
848339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
849339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rtnl_link_ifmap))
850339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rtnl_link_stats))
851adcfe196SJan Engelhardt 	       + nla_total_size(sizeof(struct rtnl_link_stats64))
852339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
853339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
854339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_TXQLEN */
855339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_WEIGHT */
856339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MTU */
857339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_LINK */
858339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MASTER */
8599a57247fSJiri Pirko 	       + nla_total_size(1) /* IFLA_CARRIER */
860edbc0bb3SBen Greear 	       + nla_total_size(4) /* IFLA_PROMISCUITY */
86176ff5cc9SJiri Pirko 	       + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
86276ff5cc9SJiri Pirko 	       + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
863339bf98fSThomas Graf 	       + nla_total_size(1) /* IFLA_OPERSTATE */
86438f7b870SPatrick McHardy 	       + nla_total_size(1) /* IFLA_LINKMODE */
8652d3b479dSdavid decotigny 	       + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
866115c9b81SGreg Rose 	       + nla_total_size(ext_filter_mask
867115c9b81SGreg Rose 			        & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
868115c9b81SGreg Rose 	       + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
869c53864fdSDavid Gibson 	       + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
870f8ff182cSThomas Graf 	       + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
87166cae9edSJiri Pirko 	       + rtnl_link_get_af_size(dev) /* IFLA_AF_SPEC */
87266cae9edSJiri Pirko 	       + nla_total_size(MAX_PHYS_PORT_ID_LEN); /* IFLA_PHYS_PORT_ID */
873339bf98fSThomas Graf }
874339bf98fSThomas Graf 
87557b61080SScott Feldman static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
87657b61080SScott Feldman {
87757b61080SScott Feldman 	struct nlattr *vf_ports;
87857b61080SScott Feldman 	struct nlattr *vf_port;
87957b61080SScott Feldman 	int vf;
88057b61080SScott Feldman 	int err;
88157b61080SScott Feldman 
88257b61080SScott Feldman 	vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
88357b61080SScott Feldman 	if (!vf_ports)
88457b61080SScott Feldman 		return -EMSGSIZE;
88557b61080SScott Feldman 
88657b61080SScott Feldman 	for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
88757b61080SScott Feldman 		vf_port = nla_nest_start(skb, IFLA_VF_PORT);
8888ca94183SScott Feldman 		if (!vf_port)
8898ca94183SScott Feldman 			goto nla_put_failure;
890a6574349SDavid S. Miller 		if (nla_put_u32(skb, IFLA_PORT_VF, vf))
891a6574349SDavid S. Miller 			goto nla_put_failure;
89257b61080SScott Feldman 		err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
8938ca94183SScott Feldman 		if (err == -EMSGSIZE)
8948ca94183SScott Feldman 			goto nla_put_failure;
89557b61080SScott Feldman 		if (err) {
89657b61080SScott Feldman 			nla_nest_cancel(skb, vf_port);
89757b61080SScott Feldman 			continue;
89857b61080SScott Feldman 		}
89957b61080SScott Feldman 		nla_nest_end(skb, vf_port);
90057b61080SScott Feldman 	}
90157b61080SScott Feldman 
90257b61080SScott Feldman 	nla_nest_end(skb, vf_ports);
90357b61080SScott Feldman 
90457b61080SScott Feldman 	return 0;
9058ca94183SScott Feldman 
9068ca94183SScott Feldman nla_put_failure:
9078ca94183SScott Feldman 	nla_nest_cancel(skb, vf_ports);
9088ca94183SScott Feldman 	return -EMSGSIZE;
90957b61080SScott Feldman }
91057b61080SScott Feldman 
91157b61080SScott Feldman static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
91257b61080SScott Feldman {
91357b61080SScott Feldman 	struct nlattr *port_self;
91457b61080SScott Feldman 	int err;
91557b61080SScott Feldman 
91657b61080SScott Feldman 	port_self = nla_nest_start(skb, IFLA_PORT_SELF);
91757b61080SScott Feldman 	if (!port_self)
91857b61080SScott Feldman 		return -EMSGSIZE;
91957b61080SScott Feldman 
92057b61080SScott Feldman 	err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
92157b61080SScott Feldman 	if (err) {
92257b61080SScott Feldman 		nla_nest_cancel(skb, port_self);
9238ca94183SScott Feldman 		return (err == -EMSGSIZE) ? err : 0;
92457b61080SScott Feldman 	}
92557b61080SScott Feldman 
92657b61080SScott Feldman 	nla_nest_end(skb, port_self);
92757b61080SScott Feldman 
92857b61080SScott Feldman 	return 0;
92957b61080SScott Feldman }
93057b61080SScott Feldman 
931c53864fdSDavid Gibson static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
932c53864fdSDavid Gibson 			  u32 ext_filter_mask)
93357b61080SScott Feldman {
93457b61080SScott Feldman 	int err;
93557b61080SScott Feldman 
936c53864fdSDavid Gibson 	if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
937c53864fdSDavid Gibson 	    !(ext_filter_mask & RTEXT_FILTER_VF))
93857b61080SScott Feldman 		return 0;
93957b61080SScott Feldman 
94057b61080SScott Feldman 	err = rtnl_port_self_fill(skb, dev);
94157b61080SScott Feldman 	if (err)
94257b61080SScott Feldman 		return err;
94357b61080SScott Feldman 
94457b61080SScott Feldman 	if (dev_num_vf(dev->dev.parent)) {
94557b61080SScott Feldman 		err = rtnl_vf_ports_fill(skb, dev);
94657b61080SScott Feldman 		if (err)
94757b61080SScott Feldman 			return err;
94857b61080SScott Feldman 	}
94957b61080SScott Feldman 
95057b61080SScott Feldman 	return 0;
95157b61080SScott Feldman }
95257b61080SScott Feldman 
95366cae9edSJiri Pirko static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
95466cae9edSJiri Pirko {
95566cae9edSJiri Pirko 	int err;
95666cae9edSJiri Pirko 	struct netdev_phys_port_id ppid;
95766cae9edSJiri Pirko 
95866cae9edSJiri Pirko 	err = dev_get_phys_port_id(dev, &ppid);
95966cae9edSJiri Pirko 	if (err) {
96066cae9edSJiri Pirko 		if (err == -EOPNOTSUPP)
96166cae9edSJiri Pirko 			return 0;
96266cae9edSJiri Pirko 		return err;
96366cae9edSJiri Pirko 	}
96466cae9edSJiri Pirko 
96566cae9edSJiri Pirko 	if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
96666cae9edSJiri Pirko 		return -EMSGSIZE;
96766cae9edSJiri Pirko 
96866cae9edSJiri Pirko 	return 0;
96966cae9edSJiri Pirko }
97066cae9edSJiri Pirko 
971b60c5115SThomas Graf static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
972575c3e2aSPatrick McHardy 			    int type, u32 pid, u32 seq, u32 change,
973115c9b81SGreg Rose 			    unsigned int flags, u32 ext_filter_mask)
974b60c5115SThomas Graf {
975b60c5115SThomas Graf 	struct ifinfomsg *ifm;
9761da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
97728172739SEric Dumazet 	struct rtnl_link_stats64 temp;
978be1f3c2cSBen Hutchings 	const struct rtnl_link_stats64 *stats;
979f8ff182cSThomas Graf 	struct nlattr *attr, *af_spec;
980f8ff182cSThomas Graf 	struct rtnl_af_ops *af_ops;
981898e5061SJiri Pirko 	struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
9821da177e4SLinus Torvalds 
9832907c35fSEric Dumazet 	ASSERT_RTNL();
984b60c5115SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
985b60c5115SThomas Graf 	if (nlh == NULL)
98626932566SPatrick McHardy 		return -EMSGSIZE;
9871da177e4SLinus Torvalds 
988b60c5115SThomas Graf 	ifm = nlmsg_data(nlh);
989b60c5115SThomas Graf 	ifm->ifi_family = AF_UNSPEC;
990b60c5115SThomas Graf 	ifm->__ifi_pad = 0;
991b60c5115SThomas Graf 	ifm->ifi_type = dev->type;
992b60c5115SThomas Graf 	ifm->ifi_index = dev->ifindex;
993b60c5115SThomas Graf 	ifm->ifi_flags = dev_get_flags(dev);
994b60c5115SThomas Graf 	ifm->ifi_change = change;
9951da177e4SLinus Torvalds 
996a6574349SDavid S. Miller 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
997a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
998a6574349SDavid S. Miller 	    nla_put_u8(skb, IFLA_OPERSTATE,
999a6574349SDavid S. Miller 		       netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1000a6574349SDavid S. Miller 	    nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1001a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1002a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_GROUP, dev->group) ||
1003edbc0bb3SBen Greear 	    nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
100476ff5cc9SJiri Pirko 	    nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
10051d69c2b3SMark A. Greer #ifdef CONFIG_RPS
100676ff5cc9SJiri Pirko 	    nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
10071d69c2b3SMark A. Greer #endif
1008a6574349SDavid S. Miller 	    (dev->ifindex != dev->iflink &&
1009a6574349SDavid S. Miller 	     nla_put_u32(skb, IFLA_LINK, dev->iflink)) ||
1010898e5061SJiri Pirko 	    (upper_dev &&
1011898e5061SJiri Pirko 	     nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) ||
10129a57247fSJiri Pirko 	    nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
1013a6574349SDavid S. Miller 	    (dev->qdisc &&
1014a6574349SDavid S. Miller 	     nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
1015a6574349SDavid S. Miller 	    (dev->ifalias &&
10162d3b479dSdavid decotigny 	     nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)) ||
10172d3b479dSdavid decotigny 	    nla_put_u32(skb, IFLA_CARRIER_CHANGES,
10182d3b479dSdavid decotigny 			atomic_read(&dev->carrier_changes)))
1019a6574349SDavid S. Miller 		goto nla_put_failure;
10200b815a1aSStephen Hemminger 
1021b00055aaSStefan Rompf 	if (1) {
10221da177e4SLinus Torvalds 		struct rtnl_link_ifmap map = {
10231da177e4SLinus Torvalds 			.mem_start   = dev->mem_start,
10241da177e4SLinus Torvalds 			.mem_end     = dev->mem_end,
10251da177e4SLinus Torvalds 			.base_addr   = dev->base_addr,
10261da177e4SLinus Torvalds 			.irq         = dev->irq,
10271da177e4SLinus Torvalds 			.dma         = dev->dma,
10281da177e4SLinus Torvalds 			.port        = dev->if_port,
10291da177e4SLinus Torvalds 		};
1030a6574349SDavid S. Miller 		if (nla_put(skb, IFLA_MAP, sizeof(map), &map))
1031a6574349SDavid S. Miller 			goto nla_put_failure;
10321da177e4SLinus Torvalds 	}
10331da177e4SLinus Torvalds 
10341da177e4SLinus Torvalds 	if (dev->addr_len) {
1035a6574349SDavid S. Miller 		if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1036a6574349SDavid S. Miller 		    nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1037a6574349SDavid S. Miller 			goto nla_put_failure;
10381da177e4SLinus Torvalds 	}
10391da177e4SLinus Torvalds 
104066cae9edSJiri Pirko 	if (rtnl_phys_port_id_fill(skb, dev))
104166cae9edSJiri Pirko 		goto nla_put_failure;
104266cae9edSJiri Pirko 
1043b60c5115SThomas Graf 	attr = nla_reserve(skb, IFLA_STATS,
1044b60c5115SThomas Graf 			sizeof(struct rtnl_link_stats));
1045b60c5115SThomas Graf 	if (attr == NULL)
1046b60c5115SThomas Graf 		goto nla_put_failure;
1047b60c5115SThomas Graf 
104828172739SEric Dumazet 	stats = dev_get_stats(dev, &temp);
1049b60c5115SThomas Graf 	copy_rtnl_link_stats(nla_data(attr), stats);
10501da177e4SLinus Torvalds 
105110708f37SJan Engelhardt 	attr = nla_reserve(skb, IFLA_STATS64,
105210708f37SJan Engelhardt 			sizeof(struct rtnl_link_stats64));
105310708f37SJan Engelhardt 	if (attr == NULL)
105410708f37SJan Engelhardt 		goto nla_put_failure;
105510708f37SJan Engelhardt 	copy_rtnl_link_stats64(nla_data(attr), stats);
105610708f37SJan Engelhardt 
1057a6574349SDavid S. Miller 	if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) &&
1058a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent)))
1059a6574349SDavid S. Miller 		goto nla_put_failure;
106057b61080SScott Feldman 
1061115c9b81SGreg Rose 	if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent
1062115c9b81SGreg Rose 	    && (ext_filter_mask & RTEXT_FILTER_VF)) {
1063ebc08a6fSWilliams, Mitch A 		int i;
1064ebc08a6fSWilliams, Mitch A 
1065c02db8c6SChris Wright 		struct nlattr *vfinfo, *vf;
1066c02db8c6SChris Wright 		int num_vfs = dev_num_vf(dev->dev.parent);
1067c02db8c6SChris Wright 
1068c02db8c6SChris Wright 		vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1069c02db8c6SChris Wright 		if (!vfinfo)
1070c02db8c6SChris Wright 			goto nla_put_failure;
1071c02db8c6SChris Wright 		for (i = 0; i < num_vfs; i++) {
1072c02db8c6SChris Wright 			struct ifla_vf_info ivi;
1073c02db8c6SChris Wright 			struct ifla_vf_mac vf_mac;
1074c02db8c6SChris Wright 			struct ifla_vf_vlan vf_vlan;
1075ed616689SSucheta Chakraborty 			struct ifla_vf_rate vf_rate;
1076c02db8c6SChris Wright 			struct ifla_vf_tx_rate vf_tx_rate;
10775f8444a3SGreg Rose 			struct ifla_vf_spoofchk vf_spoofchk;
10781d8faf48SRony Efraim 			struct ifla_vf_link_state vf_linkstate;
10795f8444a3SGreg Rose 
10805f8444a3SGreg Rose 			/*
10815f8444a3SGreg Rose 			 * Not all SR-IOV capable drivers support the
10825f8444a3SGreg Rose 			 * spoofcheck query.  Preset to -1 so the user
10835f8444a3SGreg Rose 			 * space tool can detect that the driver didn't
10845f8444a3SGreg Rose 			 * report anything.
10855f8444a3SGreg Rose 			 */
10865f8444a3SGreg Rose 			ivi.spoofchk = -1;
108784d73cd3SMathias Krause 			memset(ivi.mac, 0, sizeof(ivi.mac));
10881d8faf48SRony Efraim 			/* The default value for VF link state is "auto"
10891d8faf48SRony Efraim 			 * IFLA_VF_LINK_STATE_AUTO which equals zero
10901d8faf48SRony Efraim 			 */
10911d8faf48SRony Efraim 			ivi.linkstate = 0;
1092ebc08a6fSWilliams, Mitch A 			if (dev->netdev_ops->ndo_get_vf_config(dev, i, &ivi))
1093ebc08a6fSWilliams, Mitch A 				break;
10945f8444a3SGreg Rose 			vf_mac.vf =
10955f8444a3SGreg Rose 				vf_vlan.vf =
1096ed616689SSucheta Chakraborty 				vf_rate.vf =
10975f8444a3SGreg Rose 				vf_tx_rate.vf =
10981d8faf48SRony Efraim 				vf_spoofchk.vf =
10991d8faf48SRony Efraim 				vf_linkstate.vf = ivi.vf;
11005f8444a3SGreg Rose 
1101c02db8c6SChris Wright 			memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1102c02db8c6SChris Wright 			vf_vlan.vlan = ivi.vlan;
1103c02db8c6SChris Wright 			vf_vlan.qos = ivi.qos;
1104ed616689SSucheta Chakraborty 			vf_tx_rate.rate = ivi.max_tx_rate;
1105ed616689SSucheta Chakraborty 			vf_rate.min_tx_rate = ivi.min_tx_rate;
1106ed616689SSucheta Chakraborty 			vf_rate.max_tx_rate = ivi.max_tx_rate;
11075f8444a3SGreg Rose 			vf_spoofchk.setting = ivi.spoofchk;
11081d8faf48SRony Efraim 			vf_linkstate.link_state = ivi.linkstate;
1109c02db8c6SChris Wright 			vf = nla_nest_start(skb, IFLA_VF_INFO);
1110c02db8c6SChris Wright 			if (!vf) {
1111c02db8c6SChris Wright 				nla_nest_cancel(skb, vfinfo);
1112c02db8c6SChris Wright 				goto nla_put_failure;
1113ebc08a6fSWilliams, Mitch A 			}
1114a6574349SDavid S. Miller 			if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1115a6574349SDavid S. Miller 			    nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1116ed616689SSucheta Chakraborty 			    nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1117ed616689SSucheta Chakraborty 				    &vf_rate) ||
1118a6574349SDavid S. Miller 			    nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1119a6574349SDavid S. Miller 				    &vf_tx_rate) ||
1120a6574349SDavid S. Miller 			    nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
11211d8faf48SRony Efraim 				    &vf_spoofchk) ||
11221d8faf48SRony Efraim 			    nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
11231d8faf48SRony Efraim 				    &vf_linkstate))
1124a6574349SDavid S. Miller 				goto nla_put_failure;
1125c02db8c6SChris Wright 			nla_nest_end(skb, vf);
1126c02db8c6SChris Wright 		}
1127c02db8c6SChris Wright 		nla_nest_end(skb, vfinfo);
1128ebc08a6fSWilliams, Mitch A 	}
112957b61080SScott Feldman 
1130c53864fdSDavid Gibson 	if (rtnl_port_fill(skb, dev, ext_filter_mask))
113157b61080SScott Feldman 		goto nla_put_failure;
113257b61080SScott Feldman 
1133ba7d49b1SJiri Pirko 	if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
113438f7b870SPatrick McHardy 		if (rtnl_link_fill(skb, dev) < 0)
113538f7b870SPatrick McHardy 			goto nla_put_failure;
113638f7b870SPatrick McHardy 	}
113738f7b870SPatrick McHardy 
1138f8ff182cSThomas Graf 	if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
1139f8ff182cSThomas Graf 		goto nla_put_failure;
1140f8ff182cSThomas Graf 
1141f8ff182cSThomas Graf 	list_for_each_entry(af_ops, &rtnl_af_ops, list) {
1142f8ff182cSThomas Graf 		if (af_ops->fill_link_af) {
1143f8ff182cSThomas Graf 			struct nlattr *af;
1144f8ff182cSThomas Graf 			int err;
1145f8ff182cSThomas Graf 
1146f8ff182cSThomas Graf 			if (!(af = nla_nest_start(skb, af_ops->family)))
1147f8ff182cSThomas Graf 				goto nla_put_failure;
1148f8ff182cSThomas Graf 
1149f8ff182cSThomas Graf 			err = af_ops->fill_link_af(skb, dev);
1150f8ff182cSThomas Graf 
1151f8ff182cSThomas Graf 			/*
1152f8ff182cSThomas Graf 			 * Caller may return ENODATA to indicate that there
1153f8ff182cSThomas Graf 			 * was no data to be dumped. This is not an error, it
1154f8ff182cSThomas Graf 			 * means we should trim the attribute header and
1155f8ff182cSThomas Graf 			 * continue.
1156f8ff182cSThomas Graf 			 */
1157f8ff182cSThomas Graf 			if (err == -ENODATA)
1158f8ff182cSThomas Graf 				nla_nest_cancel(skb, af);
1159f8ff182cSThomas Graf 			else if (err < 0)
1160f8ff182cSThomas Graf 				goto nla_put_failure;
1161f8ff182cSThomas Graf 
1162f8ff182cSThomas Graf 			nla_nest_end(skb, af);
1163f8ff182cSThomas Graf 		}
1164f8ff182cSThomas Graf 	}
1165f8ff182cSThomas Graf 
1166f8ff182cSThomas Graf 	nla_nest_end(skb, af_spec);
1167f8ff182cSThomas Graf 
1168b60c5115SThomas Graf 	return nlmsg_end(skb, nlh);
1169b60c5115SThomas Graf 
1170b60c5115SThomas Graf nla_put_failure:
117126932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
117226932566SPatrick McHardy 	return -EMSGSIZE;
11731da177e4SLinus Torvalds }
11741da177e4SLinus Torvalds 
1175f7b12606SJiri Pirko static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1176f7b12606SJiri Pirko 	[IFLA_IFNAME]		= { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1177f7b12606SJiri Pirko 	[IFLA_ADDRESS]		= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1178f7b12606SJiri Pirko 	[IFLA_BROADCAST]	= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1179f7b12606SJiri Pirko 	[IFLA_MAP]		= { .len = sizeof(struct rtnl_link_ifmap) },
1180f7b12606SJiri Pirko 	[IFLA_MTU]		= { .type = NLA_U32 },
1181f7b12606SJiri Pirko 	[IFLA_LINK]		= { .type = NLA_U32 },
1182f7b12606SJiri Pirko 	[IFLA_MASTER]		= { .type = NLA_U32 },
1183f7b12606SJiri Pirko 	[IFLA_CARRIER]		= { .type = NLA_U8 },
1184f7b12606SJiri Pirko 	[IFLA_TXQLEN]		= { .type = NLA_U32 },
1185f7b12606SJiri Pirko 	[IFLA_WEIGHT]		= { .type = NLA_U32 },
1186f7b12606SJiri Pirko 	[IFLA_OPERSTATE]	= { .type = NLA_U8 },
1187f7b12606SJiri Pirko 	[IFLA_LINKMODE]		= { .type = NLA_U8 },
1188f7b12606SJiri Pirko 	[IFLA_LINKINFO]		= { .type = NLA_NESTED },
1189f7b12606SJiri Pirko 	[IFLA_NET_NS_PID]	= { .type = NLA_U32 },
1190f7b12606SJiri Pirko 	[IFLA_NET_NS_FD]	= { .type = NLA_U32 },
1191f7b12606SJiri Pirko 	[IFLA_IFALIAS]	        = { .type = NLA_STRING, .len = IFALIASZ-1 },
1192f7b12606SJiri Pirko 	[IFLA_VFINFO_LIST]	= {. type = NLA_NESTED },
1193f7b12606SJiri Pirko 	[IFLA_VF_PORTS]		= { .type = NLA_NESTED },
1194f7b12606SJiri Pirko 	[IFLA_PORT_SELF]	= { .type = NLA_NESTED },
1195f7b12606SJiri Pirko 	[IFLA_AF_SPEC]		= { .type = NLA_NESTED },
1196f7b12606SJiri Pirko 	[IFLA_EXT_MASK]		= { .type = NLA_U32 },
1197f7b12606SJiri Pirko 	[IFLA_PROMISCUITY]	= { .type = NLA_U32 },
1198f7b12606SJiri Pirko 	[IFLA_NUM_TX_QUEUES]	= { .type = NLA_U32 },
1199f7b12606SJiri Pirko 	[IFLA_NUM_RX_QUEUES]	= { .type = NLA_U32 },
1200f7b12606SJiri Pirko 	[IFLA_PHYS_PORT_ID]	= { .type = NLA_BINARY, .len = MAX_PHYS_PORT_ID_LEN },
12012d3b479dSdavid decotigny 	[IFLA_CARRIER_CHANGES]	= { .type = NLA_U32 },  /* ignored */
1202f7b12606SJiri Pirko };
1203f7b12606SJiri Pirko 
1204f7b12606SJiri Pirko static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1205f7b12606SJiri Pirko 	[IFLA_INFO_KIND]	= { .type = NLA_STRING },
1206f7b12606SJiri Pirko 	[IFLA_INFO_DATA]	= { .type = NLA_NESTED },
1207f7b12606SJiri Pirko 	[IFLA_INFO_SLAVE_KIND]	= { .type = NLA_STRING },
1208f7b12606SJiri Pirko 	[IFLA_INFO_SLAVE_DATA]	= { .type = NLA_NESTED },
1209f7b12606SJiri Pirko };
1210f7b12606SJiri Pirko 
1211f7b12606SJiri Pirko static const struct nla_policy ifla_vfinfo_policy[IFLA_VF_INFO_MAX+1] = {
1212f7b12606SJiri Pirko 	[IFLA_VF_INFO]		= { .type = NLA_NESTED },
1213f7b12606SJiri Pirko };
1214f7b12606SJiri Pirko 
1215f7b12606SJiri Pirko static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1216f7b12606SJiri Pirko 	[IFLA_VF_MAC]		= { .type = NLA_BINARY,
1217f7b12606SJiri Pirko 				    .len = sizeof(struct ifla_vf_mac) },
1218f7b12606SJiri Pirko 	[IFLA_VF_VLAN]		= { .type = NLA_BINARY,
1219f7b12606SJiri Pirko 				    .len = sizeof(struct ifla_vf_vlan) },
1220f7b12606SJiri Pirko 	[IFLA_VF_TX_RATE]	= { .type = NLA_BINARY,
1221f7b12606SJiri Pirko 				    .len = sizeof(struct ifla_vf_tx_rate) },
1222f7b12606SJiri Pirko 	[IFLA_VF_SPOOFCHK]	= { .type = NLA_BINARY,
1223f7b12606SJiri Pirko 				    .len = sizeof(struct ifla_vf_spoofchk) },
1224ed616689SSucheta Chakraborty 	[IFLA_VF_RATE]		= { .type = NLA_BINARY,
1225ed616689SSucheta Chakraborty 				    .len = sizeof(struct ifla_vf_rate) },
1226c5b46160SDoug Ledford 	[IFLA_VF_LINK_STATE]	= { .type = NLA_BINARY,
1227c5b46160SDoug Ledford 				    .len = sizeof(struct ifla_vf_link_state) },
1228f7b12606SJiri Pirko };
1229f7b12606SJiri Pirko 
1230f7b12606SJiri Pirko static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1231f7b12606SJiri Pirko 	[IFLA_PORT_VF]		= { .type = NLA_U32 },
1232f7b12606SJiri Pirko 	[IFLA_PORT_PROFILE]	= { .type = NLA_STRING,
1233f7b12606SJiri Pirko 				    .len = PORT_PROFILE_MAX },
1234f7b12606SJiri Pirko 	[IFLA_PORT_VSI_TYPE]	= { .type = NLA_BINARY,
1235f7b12606SJiri Pirko 				    .len = sizeof(struct ifla_port_vsi)},
1236f7b12606SJiri Pirko 	[IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1237f7b12606SJiri Pirko 				      .len = PORT_UUID_MAX },
1238f7b12606SJiri Pirko 	[IFLA_PORT_HOST_UUID]	= { .type = NLA_STRING,
1239f7b12606SJiri Pirko 				    .len = PORT_UUID_MAX },
1240f7b12606SJiri Pirko 	[IFLA_PORT_REQUEST]	= { .type = NLA_U8, },
1241f7b12606SJiri Pirko 	[IFLA_PORT_RESPONSE]	= { .type = NLA_U16, },
1242f7b12606SJiri Pirko };
1243f7b12606SJiri Pirko 
1244b60c5115SThomas Graf static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
12451da177e4SLinus Torvalds {
12463b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
12477c28bd0bSEric Dumazet 	int h, s_h;
12487c28bd0bSEric Dumazet 	int idx = 0, s_idx;
12491da177e4SLinus Torvalds 	struct net_device *dev;
12507c28bd0bSEric Dumazet 	struct hlist_head *head;
1251115c9b81SGreg Rose 	struct nlattr *tb[IFLA_MAX+1];
1252115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
1253973462bbSDavid Gibson 	int err;
1254e5eca6d4SMichal Schmidt 	int hdrlen;
12551da177e4SLinus Torvalds 
12567c28bd0bSEric Dumazet 	s_h = cb->args[0];
12577c28bd0bSEric Dumazet 	s_idx = cb->args[1];
12587c28bd0bSEric Dumazet 
1259e67f88ddSEric Dumazet 	rcu_read_lock();
12604e985adaSThomas Graf 	cb->seq = net->dev_base_seq;
12614e985adaSThomas Graf 
1262e5eca6d4SMichal Schmidt 	/* A hack to preserve kernel<->userspace interface.
1263e5eca6d4SMichal Schmidt 	 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1264e5eca6d4SMichal Schmidt 	 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1265e5eca6d4SMichal Schmidt 	 * what iproute2 < v3.9.0 used.
1266e5eca6d4SMichal Schmidt 	 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1267e5eca6d4SMichal Schmidt 	 * attribute, its netlink message is shorter than struct ifinfomsg.
1268e5eca6d4SMichal Schmidt 	 */
1269e5eca6d4SMichal Schmidt 	hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ?
1270e5eca6d4SMichal Schmidt 		 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1271e5eca6d4SMichal Schmidt 
1272e5eca6d4SMichal Schmidt 	if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
1273115c9b81SGreg Rose 
1274115c9b81SGreg Rose 		if (tb[IFLA_EXT_MASK])
1275115c9b81SGreg Rose 			ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1276a4b64fbeSEric Dumazet 	}
1277115c9b81SGreg Rose 
12787c28bd0bSEric Dumazet 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
12797562f876SPavel Emelianov 		idx = 0;
12807c28bd0bSEric Dumazet 		head = &net->dev_index_head[h];
1281b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu(dev, head, index_hlist) {
12821da177e4SLinus Torvalds 			if (idx < s_idx)
12837562f876SPavel Emelianov 				goto cont;
1284973462bbSDavid Gibson 			err = rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
128515e47304SEric W. Biederman 					       NETLINK_CB(cb->skb).portid,
12867c28bd0bSEric Dumazet 					       cb->nlh->nlmsg_seq, 0,
1287115c9b81SGreg Rose 					       NLM_F_MULTI,
1288973462bbSDavid Gibson 					       ext_filter_mask);
1289973462bbSDavid Gibson 			/* If we ran out of room on the first message,
1290973462bbSDavid Gibson 			 * we're in trouble
1291973462bbSDavid Gibson 			 */
1292973462bbSDavid Gibson 			WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
1293973462bbSDavid Gibson 
1294973462bbSDavid Gibson 			if (err <= 0)
12957c28bd0bSEric Dumazet 				goto out;
12964e985adaSThomas Graf 
12974e985adaSThomas Graf 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
12987562f876SPavel Emelianov cont:
12997562f876SPavel Emelianov 			idx++;
13001da177e4SLinus Torvalds 		}
13017c28bd0bSEric Dumazet 	}
13027c28bd0bSEric Dumazet out:
1303e67f88ddSEric Dumazet 	rcu_read_unlock();
13047c28bd0bSEric Dumazet 	cb->args[1] = idx;
13057c28bd0bSEric Dumazet 	cb->args[0] = h;
13061da177e4SLinus Torvalds 
13071da177e4SLinus Torvalds 	return skb->len;
13081da177e4SLinus Torvalds }
13091da177e4SLinus Torvalds 
1310f7b12606SJiri Pirko int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len)
1311f7b12606SJiri Pirko {
1312f7b12606SJiri Pirko 	return nla_parse(tb, IFLA_MAX, head, len, ifla_policy);
1313f7b12606SJiri Pirko }
1314f7b12606SJiri Pirko EXPORT_SYMBOL(rtnl_nla_parse_ifla);
131557b61080SScott Feldman 
131681adee47SEric W. Biederman struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
131781adee47SEric W. Biederman {
131881adee47SEric W. Biederman 	struct net *net;
131981adee47SEric W. Biederman 	/* Examine the link attributes and figure out which
132081adee47SEric W. Biederman 	 * network namespace we are talking about.
132181adee47SEric W. Biederman 	 */
132281adee47SEric W. Biederman 	if (tb[IFLA_NET_NS_PID])
132381adee47SEric W. Biederman 		net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
1324f0630529SEric W. Biederman 	else if (tb[IFLA_NET_NS_FD])
1325f0630529SEric W. Biederman 		net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
132681adee47SEric W. Biederman 	else
132781adee47SEric W. Biederman 		net = get_net(src_net);
132881adee47SEric W. Biederman 	return net;
132981adee47SEric W. Biederman }
133081adee47SEric W. Biederman EXPORT_SYMBOL(rtnl_link_get_net);
133181adee47SEric W. Biederman 
13321840bb13SThomas Graf static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
13331840bb13SThomas Graf {
13341840bb13SThomas Graf 	if (dev) {
13351840bb13SThomas Graf 		if (tb[IFLA_ADDRESS] &&
13361840bb13SThomas Graf 		    nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
13371840bb13SThomas Graf 			return -EINVAL;
13381840bb13SThomas Graf 
13391840bb13SThomas Graf 		if (tb[IFLA_BROADCAST] &&
13401840bb13SThomas Graf 		    nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
13411840bb13SThomas Graf 			return -EINVAL;
13421840bb13SThomas Graf 	}
13431840bb13SThomas Graf 
1344cf7afbfeSThomas Graf 	if (tb[IFLA_AF_SPEC]) {
1345cf7afbfeSThomas Graf 		struct nlattr *af;
1346cf7afbfeSThomas Graf 		int rem, err;
1347cf7afbfeSThomas Graf 
1348cf7afbfeSThomas Graf 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1349cf7afbfeSThomas Graf 			const struct rtnl_af_ops *af_ops;
1350cf7afbfeSThomas Graf 
1351cf7afbfeSThomas Graf 			if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1352cf7afbfeSThomas Graf 				return -EAFNOSUPPORT;
1353cf7afbfeSThomas Graf 
1354cf7afbfeSThomas Graf 			if (!af_ops->set_link_af)
1355cf7afbfeSThomas Graf 				return -EOPNOTSUPP;
1356cf7afbfeSThomas Graf 
1357cf7afbfeSThomas Graf 			if (af_ops->validate_link_af) {
13586d3a9a68SKurt Van Dijck 				err = af_ops->validate_link_af(dev, af);
1359cf7afbfeSThomas Graf 				if (err < 0)
1360cf7afbfeSThomas Graf 					return err;
1361cf7afbfeSThomas Graf 			}
1362cf7afbfeSThomas Graf 		}
1363cf7afbfeSThomas Graf 	}
1364cf7afbfeSThomas Graf 
13651840bb13SThomas Graf 	return 0;
13661840bb13SThomas Graf }
13671840bb13SThomas Graf 
1368c02db8c6SChris Wright static int do_setvfinfo(struct net_device *dev, struct nlattr *attr)
1369c02db8c6SChris Wright {
1370c02db8c6SChris Wright 	int rem, err = -EINVAL;
1371c02db8c6SChris Wright 	struct nlattr *vf;
1372c02db8c6SChris Wright 	const struct net_device_ops *ops = dev->netdev_ops;
1373c02db8c6SChris Wright 
1374c02db8c6SChris Wright 	nla_for_each_nested(vf, attr, rem) {
1375c02db8c6SChris Wright 		switch (nla_type(vf)) {
1376c02db8c6SChris Wright 		case IFLA_VF_MAC: {
1377c02db8c6SChris Wright 			struct ifla_vf_mac *ivm;
1378c02db8c6SChris Wright 			ivm = nla_data(vf);
1379c02db8c6SChris Wright 			err = -EOPNOTSUPP;
1380c02db8c6SChris Wright 			if (ops->ndo_set_vf_mac)
1381c02db8c6SChris Wright 				err = ops->ndo_set_vf_mac(dev, ivm->vf,
1382c02db8c6SChris Wright 							  ivm->mac);
1383c02db8c6SChris Wright 			break;
1384c02db8c6SChris Wright 		}
1385c02db8c6SChris Wright 		case IFLA_VF_VLAN: {
1386c02db8c6SChris Wright 			struct ifla_vf_vlan *ivv;
1387c02db8c6SChris Wright 			ivv = nla_data(vf);
1388c02db8c6SChris Wright 			err = -EOPNOTSUPP;
1389c02db8c6SChris Wright 			if (ops->ndo_set_vf_vlan)
1390c02db8c6SChris Wright 				err = ops->ndo_set_vf_vlan(dev, ivv->vf,
1391c02db8c6SChris Wright 							   ivv->vlan,
1392c02db8c6SChris Wright 							   ivv->qos);
1393c02db8c6SChris Wright 			break;
1394c02db8c6SChris Wright 		}
1395c02db8c6SChris Wright 		case IFLA_VF_TX_RATE: {
1396c02db8c6SChris Wright 			struct ifla_vf_tx_rate *ivt;
1397ed616689SSucheta Chakraborty 			struct ifla_vf_info ivf;
1398c02db8c6SChris Wright 			ivt = nla_data(vf);
1399c02db8c6SChris Wright 			err = -EOPNOTSUPP;
1400ed616689SSucheta Chakraborty 			if (ops->ndo_get_vf_config)
1401ed616689SSucheta Chakraborty 				err = ops->ndo_get_vf_config(dev, ivt->vf,
1402ed616689SSucheta Chakraborty 							     &ivf);
1403ed616689SSucheta Chakraborty 			if (err)
1404ed616689SSucheta Chakraborty 				break;
1405ed616689SSucheta Chakraborty 			err = -EOPNOTSUPP;
1406ed616689SSucheta Chakraborty 			if (ops->ndo_set_vf_rate)
1407ed616689SSucheta Chakraborty 				err = ops->ndo_set_vf_rate(dev, ivt->vf,
1408ed616689SSucheta Chakraborty 							   ivf.min_tx_rate,
1409c02db8c6SChris Wright 							   ivt->rate);
1410c02db8c6SChris Wright 			break;
1411c02db8c6SChris Wright 		}
1412ed616689SSucheta Chakraborty 		case IFLA_VF_RATE: {
1413ed616689SSucheta Chakraborty 			struct ifla_vf_rate *ivt;
1414ed616689SSucheta Chakraborty 			ivt = nla_data(vf);
1415ed616689SSucheta Chakraborty 			err = -EOPNOTSUPP;
1416ed616689SSucheta Chakraborty 			if (ops->ndo_set_vf_rate)
1417ed616689SSucheta Chakraborty 				err = ops->ndo_set_vf_rate(dev, ivt->vf,
1418ed616689SSucheta Chakraborty 							   ivt->min_tx_rate,
1419ed616689SSucheta Chakraborty 							   ivt->max_tx_rate);
1420ed616689SSucheta Chakraborty 			break;
1421ed616689SSucheta Chakraborty 		}
14225f8444a3SGreg Rose 		case IFLA_VF_SPOOFCHK: {
14235f8444a3SGreg Rose 			struct ifla_vf_spoofchk *ivs;
14245f8444a3SGreg Rose 			ivs = nla_data(vf);
14255f8444a3SGreg Rose 			err = -EOPNOTSUPP;
14265f8444a3SGreg Rose 			if (ops->ndo_set_vf_spoofchk)
14275f8444a3SGreg Rose 				err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
14285f8444a3SGreg Rose 							       ivs->setting);
14295f8444a3SGreg Rose 			break;
14305f8444a3SGreg Rose 		}
14311d8faf48SRony Efraim 		case IFLA_VF_LINK_STATE: {
14321d8faf48SRony Efraim 			struct ifla_vf_link_state *ivl;
14331d8faf48SRony Efraim 			ivl = nla_data(vf);
14341d8faf48SRony Efraim 			err = -EOPNOTSUPP;
14351d8faf48SRony Efraim 			if (ops->ndo_set_vf_link_state)
14361d8faf48SRony Efraim 				err = ops->ndo_set_vf_link_state(dev, ivl->vf,
14371d8faf48SRony Efraim 								 ivl->link_state);
14381d8faf48SRony Efraim 			break;
14391d8faf48SRony Efraim 		}
1440c02db8c6SChris Wright 		default:
1441c02db8c6SChris Wright 			err = -EINVAL;
1442c02db8c6SChris Wright 			break;
1443c02db8c6SChris Wright 		}
1444c02db8c6SChris Wright 		if (err)
1445c02db8c6SChris Wright 			break;
1446c02db8c6SChris Wright 	}
1447c02db8c6SChris Wright 	return err;
1448c02db8c6SChris Wright }
1449c02db8c6SChris Wright 
1450fbaec0eaSJiri Pirko static int do_set_master(struct net_device *dev, int ifindex)
1451fbaec0eaSJiri Pirko {
1452898e5061SJiri Pirko 	struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
1453fbaec0eaSJiri Pirko 	const struct net_device_ops *ops;
1454fbaec0eaSJiri Pirko 	int err;
1455fbaec0eaSJiri Pirko 
1456898e5061SJiri Pirko 	if (upper_dev) {
1457898e5061SJiri Pirko 		if (upper_dev->ifindex == ifindex)
1458fbaec0eaSJiri Pirko 			return 0;
1459898e5061SJiri Pirko 		ops = upper_dev->netdev_ops;
1460fbaec0eaSJiri Pirko 		if (ops->ndo_del_slave) {
1461898e5061SJiri Pirko 			err = ops->ndo_del_slave(upper_dev, dev);
1462fbaec0eaSJiri Pirko 			if (err)
1463fbaec0eaSJiri Pirko 				return err;
1464fbaec0eaSJiri Pirko 		} else {
1465fbaec0eaSJiri Pirko 			return -EOPNOTSUPP;
1466fbaec0eaSJiri Pirko 		}
1467fbaec0eaSJiri Pirko 	}
1468fbaec0eaSJiri Pirko 
1469fbaec0eaSJiri Pirko 	if (ifindex) {
1470898e5061SJiri Pirko 		upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
1471898e5061SJiri Pirko 		if (!upper_dev)
1472fbaec0eaSJiri Pirko 			return -EINVAL;
1473898e5061SJiri Pirko 		ops = upper_dev->netdev_ops;
1474fbaec0eaSJiri Pirko 		if (ops->ndo_add_slave) {
1475898e5061SJiri Pirko 			err = ops->ndo_add_slave(upper_dev, dev);
1476fbaec0eaSJiri Pirko 			if (err)
1477fbaec0eaSJiri Pirko 				return err;
1478fbaec0eaSJiri Pirko 		} else {
1479fbaec0eaSJiri Pirko 			return -EOPNOTSUPP;
1480fbaec0eaSJiri Pirko 		}
1481fbaec0eaSJiri Pirko 	}
1482fbaec0eaSJiri Pirko 	return 0;
1483fbaec0eaSJiri Pirko }
1484fbaec0eaSJiri Pirko 
148590c325e3SNicolas Dichtel #define DO_SETLINK_MODIFIED	0x01
1486ba998906SNicolas Dichtel /* notify flag means notify + modified. */
1487ba998906SNicolas Dichtel #define DO_SETLINK_NOTIFY	0x03
148890f62cf3SEric W. Biederman static int do_setlink(const struct sk_buff *skb,
148990f62cf3SEric W. Biederman 		      struct net_device *dev, struct ifinfomsg *ifm,
149090c325e3SNicolas Dichtel 		      struct nlattr **tb, char *ifname, int status)
14910157f60cSPatrick McHardy {
1492d314774cSStephen Hemminger 	const struct net_device_ops *ops = dev->netdev_ops;
14930157f60cSPatrick McHardy 	int err;
14940157f60cSPatrick McHardy 
1495f0630529SEric W. Biederman 	if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
149681adee47SEric W. Biederman 		struct net *net = rtnl_link_get_net(dev_net(dev), tb);
1497d8a5ec67SEric W. Biederman 		if (IS_ERR(net)) {
1498d8a5ec67SEric W. Biederman 			err = PTR_ERR(net);
1499d8a5ec67SEric W. Biederman 			goto errout;
1500d8a5ec67SEric W. Biederman 		}
150190f62cf3SEric W. Biederman 		if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
1502b51642f6SEric W. Biederman 			err = -EPERM;
1503b51642f6SEric W. Biederman 			goto errout;
1504b51642f6SEric W. Biederman 		}
1505d8a5ec67SEric W. Biederman 		err = dev_change_net_namespace(dev, net, ifname);
1506d8a5ec67SEric W. Biederman 		put_net(net);
1507d8a5ec67SEric W. Biederman 		if (err)
1508d8a5ec67SEric W. Biederman 			goto errout;
150990c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
1510d8a5ec67SEric W. Biederman 	}
1511d8a5ec67SEric W. Biederman 
15120157f60cSPatrick McHardy 	if (tb[IFLA_MAP]) {
15130157f60cSPatrick McHardy 		struct rtnl_link_ifmap *u_map;
15140157f60cSPatrick McHardy 		struct ifmap k_map;
15150157f60cSPatrick McHardy 
1516d314774cSStephen Hemminger 		if (!ops->ndo_set_config) {
15170157f60cSPatrick McHardy 			err = -EOPNOTSUPP;
15180157f60cSPatrick McHardy 			goto errout;
15190157f60cSPatrick McHardy 		}
15200157f60cSPatrick McHardy 
15210157f60cSPatrick McHardy 		if (!netif_device_present(dev)) {
15220157f60cSPatrick McHardy 			err = -ENODEV;
15230157f60cSPatrick McHardy 			goto errout;
15240157f60cSPatrick McHardy 		}
15250157f60cSPatrick McHardy 
15260157f60cSPatrick McHardy 		u_map = nla_data(tb[IFLA_MAP]);
15270157f60cSPatrick McHardy 		k_map.mem_start = (unsigned long) u_map->mem_start;
15280157f60cSPatrick McHardy 		k_map.mem_end = (unsigned long) u_map->mem_end;
15290157f60cSPatrick McHardy 		k_map.base_addr = (unsigned short) u_map->base_addr;
15300157f60cSPatrick McHardy 		k_map.irq = (unsigned char) u_map->irq;
15310157f60cSPatrick McHardy 		k_map.dma = (unsigned char) u_map->dma;
15320157f60cSPatrick McHardy 		k_map.port = (unsigned char) u_map->port;
15330157f60cSPatrick McHardy 
1534d314774cSStephen Hemminger 		err = ops->ndo_set_config(dev, &k_map);
15350157f60cSPatrick McHardy 		if (err < 0)
15360157f60cSPatrick McHardy 			goto errout;
15370157f60cSPatrick McHardy 
1538ba998906SNicolas Dichtel 		status |= DO_SETLINK_NOTIFY;
15390157f60cSPatrick McHardy 	}
15400157f60cSPatrick McHardy 
15410157f60cSPatrick McHardy 	if (tb[IFLA_ADDRESS]) {
15420157f60cSPatrick McHardy 		struct sockaddr *sa;
15430157f60cSPatrick McHardy 		int len;
15440157f60cSPatrick McHardy 
15450157f60cSPatrick McHardy 		len = sizeof(sa_family_t) + dev->addr_len;
15460157f60cSPatrick McHardy 		sa = kmalloc(len, GFP_KERNEL);
15470157f60cSPatrick McHardy 		if (!sa) {
15480157f60cSPatrick McHardy 			err = -ENOMEM;
15490157f60cSPatrick McHardy 			goto errout;
15500157f60cSPatrick McHardy 		}
15510157f60cSPatrick McHardy 		sa->sa_family = dev->type;
15520157f60cSPatrick McHardy 		memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
15530157f60cSPatrick McHardy 		       dev->addr_len);
1554e7c3273eSJiri Pirko 		err = dev_set_mac_address(dev, sa);
15550157f60cSPatrick McHardy 		kfree(sa);
15560157f60cSPatrick McHardy 		if (err)
15570157f60cSPatrick McHardy 			goto errout;
155890c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
15590157f60cSPatrick McHardy 	}
15600157f60cSPatrick McHardy 
15610157f60cSPatrick McHardy 	if (tb[IFLA_MTU]) {
15620157f60cSPatrick McHardy 		err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
15630157f60cSPatrick McHardy 		if (err < 0)
15640157f60cSPatrick McHardy 			goto errout;
156590c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
15660157f60cSPatrick McHardy 	}
15670157f60cSPatrick McHardy 
1568cbda10faSVlad Dogaru 	if (tb[IFLA_GROUP]) {
1569cbda10faSVlad Dogaru 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
1570ba998906SNicolas Dichtel 		status |= DO_SETLINK_NOTIFY;
1571cbda10faSVlad Dogaru 	}
1572cbda10faSVlad Dogaru 
15730157f60cSPatrick McHardy 	/*
15740157f60cSPatrick McHardy 	 * Interface selected by interface index but interface
15750157f60cSPatrick McHardy 	 * name provided implies that a name change has been
15760157f60cSPatrick McHardy 	 * requested.
15770157f60cSPatrick McHardy 	 */
15780157f60cSPatrick McHardy 	if (ifm->ifi_index > 0 && ifname[0]) {
15790157f60cSPatrick McHardy 		err = dev_change_name(dev, ifname);
15800157f60cSPatrick McHardy 		if (err < 0)
15810157f60cSPatrick McHardy 			goto errout;
158290c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
15830157f60cSPatrick McHardy 	}
15840157f60cSPatrick McHardy 
15850b815a1aSStephen Hemminger 	if (tb[IFLA_IFALIAS]) {
15860b815a1aSStephen Hemminger 		err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
15870b815a1aSStephen Hemminger 				    nla_len(tb[IFLA_IFALIAS]));
15880b815a1aSStephen Hemminger 		if (err < 0)
15890b815a1aSStephen Hemminger 			goto errout;
1590ba998906SNicolas Dichtel 		status |= DO_SETLINK_NOTIFY;
15910b815a1aSStephen Hemminger 	}
15920b815a1aSStephen Hemminger 
15930157f60cSPatrick McHardy 	if (tb[IFLA_BROADCAST]) {
15940157f60cSPatrick McHardy 		nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
1595e7c3273eSJiri Pirko 		call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
15960157f60cSPatrick McHardy 	}
15970157f60cSPatrick McHardy 
15980157f60cSPatrick McHardy 	if (ifm->ifi_flags || ifm->ifi_change) {
15993729d502SPatrick McHardy 		err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
16005f9021cfSJohannes Berg 		if (err < 0)
16015f9021cfSJohannes Berg 			goto errout;
16020157f60cSPatrick McHardy 	}
16030157f60cSPatrick McHardy 
1604fbaec0eaSJiri Pirko 	if (tb[IFLA_MASTER]) {
1605fbaec0eaSJiri Pirko 		err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
1606fbaec0eaSJiri Pirko 		if (err)
1607fbaec0eaSJiri Pirko 			goto errout;
160890c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
1609fbaec0eaSJiri Pirko 	}
1610fbaec0eaSJiri Pirko 
16119a57247fSJiri Pirko 	if (tb[IFLA_CARRIER]) {
16129a57247fSJiri Pirko 		err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
16139a57247fSJiri Pirko 		if (err)
16149a57247fSJiri Pirko 			goto errout;
161590c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
16169a57247fSJiri Pirko 	}
16179a57247fSJiri Pirko 
16185d1180fcSNicolas Dichtel 	if (tb[IFLA_TXQLEN]) {
16195d1180fcSNicolas Dichtel 		unsigned long value = nla_get_u32(tb[IFLA_TXQLEN]);
16205d1180fcSNicolas Dichtel 
16215d1180fcSNicolas Dichtel 		if (dev->tx_queue_len ^ value)
1622ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
16235d1180fcSNicolas Dichtel 
16245d1180fcSNicolas Dichtel 		dev->tx_queue_len = value;
16255d1180fcSNicolas Dichtel 	}
16260157f60cSPatrick McHardy 
16270157f60cSPatrick McHardy 	if (tb[IFLA_OPERSTATE])
162893b2d4a2SDavid S. Miller 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
16290157f60cSPatrick McHardy 
16300157f60cSPatrick McHardy 	if (tb[IFLA_LINKMODE]) {
16311889b0e7SNicolas Dichtel 		unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
16321889b0e7SNicolas Dichtel 
16330157f60cSPatrick McHardy 		write_lock_bh(&dev_base_lock);
16341889b0e7SNicolas Dichtel 		if (dev->link_mode ^ value)
1635ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
16361889b0e7SNicolas Dichtel 		dev->link_mode = value;
163793b2d4a2SDavid S. Miller 		write_unlock_bh(&dev_base_lock);
16380157f60cSPatrick McHardy 	}
16390157f60cSPatrick McHardy 
1640c02db8c6SChris Wright 	if (tb[IFLA_VFINFO_LIST]) {
1641c02db8c6SChris Wright 		struct nlattr *attr;
1642c02db8c6SChris Wright 		int rem;
1643c02db8c6SChris Wright 		nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
1644253683bbSDavid Howells 			if (nla_type(attr) != IFLA_VF_INFO) {
1645253683bbSDavid Howells 				err = -EINVAL;
1646c02db8c6SChris Wright 				goto errout;
1647253683bbSDavid Howells 			}
1648c02db8c6SChris Wright 			err = do_setvfinfo(dev, attr);
1649ebc08a6fSWilliams, Mitch A 			if (err < 0)
1650ebc08a6fSWilliams, Mitch A 				goto errout;
1651ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
1652ebc08a6fSWilliams, Mitch A 		}
1653ebc08a6fSWilliams, Mitch A 	}
16540157f60cSPatrick McHardy 	err = 0;
16550157f60cSPatrick McHardy 
165657b61080SScott Feldman 	if (tb[IFLA_VF_PORTS]) {
165757b61080SScott Feldman 		struct nlattr *port[IFLA_PORT_MAX+1];
165857b61080SScott Feldman 		struct nlattr *attr;
165957b61080SScott Feldman 		int vf;
166057b61080SScott Feldman 		int rem;
166157b61080SScott Feldman 
166257b61080SScott Feldman 		err = -EOPNOTSUPP;
166357b61080SScott Feldman 		if (!ops->ndo_set_vf_port)
166457b61080SScott Feldman 			goto errout;
166557b61080SScott Feldman 
166657b61080SScott Feldman 		nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
166757b61080SScott Feldman 			if (nla_type(attr) != IFLA_VF_PORT)
166857b61080SScott Feldman 				continue;
166957b61080SScott Feldman 			err = nla_parse_nested(port, IFLA_PORT_MAX,
167057b61080SScott Feldman 				attr, ifla_port_policy);
167157b61080SScott Feldman 			if (err < 0)
167257b61080SScott Feldman 				goto errout;
167357b61080SScott Feldman 			if (!port[IFLA_PORT_VF]) {
167457b61080SScott Feldman 				err = -EOPNOTSUPP;
167557b61080SScott Feldman 				goto errout;
167657b61080SScott Feldman 			}
167757b61080SScott Feldman 			vf = nla_get_u32(port[IFLA_PORT_VF]);
167857b61080SScott Feldman 			err = ops->ndo_set_vf_port(dev, vf, port);
167957b61080SScott Feldman 			if (err < 0)
168057b61080SScott Feldman 				goto errout;
1681ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
168257b61080SScott Feldman 		}
168357b61080SScott Feldman 	}
168457b61080SScott Feldman 	err = 0;
168557b61080SScott Feldman 
168657b61080SScott Feldman 	if (tb[IFLA_PORT_SELF]) {
168757b61080SScott Feldman 		struct nlattr *port[IFLA_PORT_MAX+1];
168857b61080SScott Feldman 
168957b61080SScott Feldman 		err = nla_parse_nested(port, IFLA_PORT_MAX,
169057b61080SScott Feldman 			tb[IFLA_PORT_SELF], ifla_port_policy);
169157b61080SScott Feldman 		if (err < 0)
169257b61080SScott Feldman 			goto errout;
169357b61080SScott Feldman 
169457b61080SScott Feldman 		err = -EOPNOTSUPP;
169557b61080SScott Feldman 		if (ops->ndo_set_vf_port)
169657b61080SScott Feldman 			err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
169757b61080SScott Feldman 		if (err < 0)
169857b61080SScott Feldman 			goto errout;
1699ba998906SNicolas Dichtel 		status |= DO_SETLINK_NOTIFY;
170057b61080SScott Feldman 	}
1701f8ff182cSThomas Graf 
1702f8ff182cSThomas Graf 	if (tb[IFLA_AF_SPEC]) {
1703f8ff182cSThomas Graf 		struct nlattr *af;
1704f8ff182cSThomas Graf 		int rem;
1705f8ff182cSThomas Graf 
1706f8ff182cSThomas Graf 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1707f8ff182cSThomas Graf 			const struct rtnl_af_ops *af_ops;
1708f8ff182cSThomas Graf 
1709f8ff182cSThomas Graf 			if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1710cf7afbfeSThomas Graf 				BUG();
1711f8ff182cSThomas Graf 
1712cf7afbfeSThomas Graf 			err = af_ops->set_link_af(dev, af);
1713f8ff182cSThomas Graf 			if (err < 0)
1714f8ff182cSThomas Graf 				goto errout;
1715f8ff182cSThomas Graf 
1716ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
1717f8ff182cSThomas Graf 		}
1718f8ff182cSThomas Graf 	}
171957b61080SScott Feldman 	err = 0;
172057b61080SScott Feldman 
17210157f60cSPatrick McHardy errout:
1722ba998906SNicolas Dichtel 	if (status & DO_SETLINK_MODIFIED) {
1723ba998906SNicolas Dichtel 		if (status & DO_SETLINK_NOTIFY)
1724ba998906SNicolas Dichtel 			netdev_state_change(dev);
1725ba998906SNicolas Dichtel 
1726ba998906SNicolas Dichtel 		if (err < 0)
1727e87cc472SJoe 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",
1728e87cc472SJoe Perches 					     dev->name);
1729ba998906SNicolas Dichtel 	}
17300157f60cSPatrick McHardy 
17310157f60cSPatrick McHardy 	return err;
17320157f60cSPatrick McHardy }
17330157f60cSPatrick McHardy 
1734661d2967SThomas Graf static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
1735da5e0494SThomas Graf {
17363b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
1737da5e0494SThomas Graf 	struct ifinfomsg *ifm;
1738da5e0494SThomas Graf 	struct net_device *dev;
17390157f60cSPatrick McHardy 	int err;
1740da5e0494SThomas Graf 	struct nlattr *tb[IFLA_MAX+1];
17411da177e4SLinus Torvalds 	char ifname[IFNAMSIZ];
17421da177e4SLinus Torvalds 
1743da5e0494SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1744da5e0494SThomas Graf 	if (err < 0)
1745da5e0494SThomas Graf 		goto errout;
17461da177e4SLinus Torvalds 
17475176f91eSThomas Graf 	if (tb[IFLA_IFNAME])
17485176f91eSThomas Graf 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
174978e5b891SPatrick McHardy 	else
175078e5b891SPatrick McHardy 		ifname[0] = '\0';
17511da177e4SLinus Torvalds 
17521da177e4SLinus Torvalds 	err = -EINVAL;
1753da5e0494SThomas Graf 	ifm = nlmsg_data(nlh);
175451055be8SPatrick McHardy 	if (ifm->ifi_index > 0)
1755a3d12891SEric Dumazet 		dev = __dev_get_by_index(net, ifm->ifi_index);
1756da5e0494SThomas Graf 	else if (tb[IFLA_IFNAME])
1757a3d12891SEric Dumazet 		dev = __dev_get_by_name(net, ifname);
1758da5e0494SThomas Graf 	else
1759da5e0494SThomas Graf 		goto errout;
17601da177e4SLinus Torvalds 
1761da5e0494SThomas Graf 	if (dev == NULL) {
1762da5e0494SThomas Graf 		err = -ENODEV;
1763da5e0494SThomas Graf 		goto errout;
1764da5e0494SThomas Graf 	}
17651da177e4SLinus Torvalds 
1766e0d087afSEric Dumazet 	err = validate_linkmsg(dev, tb);
1767e0d087afSEric Dumazet 	if (err < 0)
1768a3d12891SEric Dumazet 		goto errout;
1769da5e0494SThomas Graf 
177090f62cf3SEric W. Biederman 	err = do_setlink(skb, dev, ifm, tb, ifname, 0);
1771da5e0494SThomas Graf errout:
17721da177e4SLinus Torvalds 	return err;
17731da177e4SLinus Torvalds }
17741da177e4SLinus Torvalds 
1775661d2967SThomas Graf static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
177638f7b870SPatrick McHardy {
17773b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
177838f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops;
177938f7b870SPatrick McHardy 	struct net_device *dev;
178038f7b870SPatrick McHardy 	struct ifinfomsg *ifm;
178138f7b870SPatrick McHardy 	char ifname[IFNAMSIZ];
178238f7b870SPatrick McHardy 	struct nlattr *tb[IFLA_MAX+1];
178338f7b870SPatrick McHardy 	int err;
1784226bd341SEric Dumazet 	LIST_HEAD(list_kill);
178538f7b870SPatrick McHardy 
178638f7b870SPatrick McHardy 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
178738f7b870SPatrick McHardy 	if (err < 0)
178838f7b870SPatrick McHardy 		return err;
178938f7b870SPatrick McHardy 
179038f7b870SPatrick McHardy 	if (tb[IFLA_IFNAME])
179138f7b870SPatrick McHardy 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
179238f7b870SPatrick McHardy 
179338f7b870SPatrick McHardy 	ifm = nlmsg_data(nlh);
179438f7b870SPatrick McHardy 	if (ifm->ifi_index > 0)
1795881d966bSEric W. Biederman 		dev = __dev_get_by_index(net, ifm->ifi_index);
179638f7b870SPatrick McHardy 	else if (tb[IFLA_IFNAME])
1797881d966bSEric W. Biederman 		dev = __dev_get_by_name(net, ifname);
179838f7b870SPatrick McHardy 	else
179938f7b870SPatrick McHardy 		return -EINVAL;
180038f7b870SPatrick McHardy 
180138f7b870SPatrick McHardy 	if (!dev)
180238f7b870SPatrick McHardy 		return -ENODEV;
180338f7b870SPatrick McHardy 
180438f7b870SPatrick McHardy 	ops = dev->rtnl_link_ops;
1805b0ab2fabSJiri Pirko 	if (!ops || !ops->dellink)
180638f7b870SPatrick McHardy 		return -EOPNOTSUPP;
180738f7b870SPatrick McHardy 
1808226bd341SEric Dumazet 	ops->dellink(dev, &list_kill);
1809226bd341SEric Dumazet 	unregister_netdevice_many(&list_kill);
181038f7b870SPatrick McHardy 	return 0;
181138f7b870SPatrick McHardy }
181238f7b870SPatrick McHardy 
18133729d502SPatrick McHardy int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
18143729d502SPatrick McHardy {
18153729d502SPatrick McHardy 	unsigned int old_flags;
18163729d502SPatrick McHardy 	int err;
18173729d502SPatrick McHardy 
18183729d502SPatrick McHardy 	old_flags = dev->flags;
18193729d502SPatrick McHardy 	if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
18203729d502SPatrick McHardy 		err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
18213729d502SPatrick McHardy 		if (err < 0)
18223729d502SPatrick McHardy 			return err;
18233729d502SPatrick McHardy 	}
18243729d502SPatrick McHardy 
18253729d502SPatrick McHardy 	dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
18263729d502SPatrick McHardy 
1827a528c219SNicolas Dichtel 	__dev_notify_flags(dev, old_flags, ~0U);
18283729d502SPatrick McHardy 	return 0;
18293729d502SPatrick McHardy }
18303729d502SPatrick McHardy EXPORT_SYMBOL(rtnl_configure_link);
18313729d502SPatrick McHardy 
1832c0713563SRami Rosen struct net_device *rtnl_create_link(struct net *net,
18335517750fSTom Gundersen 	char *ifname, unsigned char name_assign_type,
18345517750fSTom Gundersen 	const struct rtnl_link_ops *ops, struct nlattr *tb[])
1835e7199288SPavel Emelianov {
1836e7199288SPavel Emelianov 	int err;
1837e7199288SPavel Emelianov 	struct net_device *dev;
1838d40156aaSJiri Pirko 	unsigned int num_tx_queues = 1;
1839d40156aaSJiri Pirko 	unsigned int num_rx_queues = 1;
1840e7199288SPavel Emelianov 
184176ff5cc9SJiri Pirko 	if (tb[IFLA_NUM_TX_QUEUES])
184276ff5cc9SJiri Pirko 		num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
184376ff5cc9SJiri Pirko 	else if (ops->get_num_tx_queues)
1844d40156aaSJiri Pirko 		num_tx_queues = ops->get_num_tx_queues();
184576ff5cc9SJiri Pirko 
184676ff5cc9SJiri Pirko 	if (tb[IFLA_NUM_RX_QUEUES])
184776ff5cc9SJiri Pirko 		num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
184876ff5cc9SJiri Pirko 	else if (ops->get_num_rx_queues)
1849d40156aaSJiri Pirko 		num_rx_queues = ops->get_num_rx_queues();
1850efacb309Sstephen hemminger 
1851e7199288SPavel Emelianov 	err = -ENOMEM;
18525517750fSTom Gundersen 	dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
1853c835a677STom Gundersen 			       ops->setup, num_tx_queues, num_rx_queues);
1854e7199288SPavel Emelianov 	if (!dev)
1855e7199288SPavel Emelianov 		goto err;
1856e7199288SPavel Emelianov 
185781adee47SEric W. Biederman 	dev_net_set(dev, net);
185881adee47SEric W. Biederman 	dev->rtnl_link_ops = ops;
18593729d502SPatrick McHardy 	dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
186081adee47SEric W. Biederman 
1861e7199288SPavel Emelianov 	if (tb[IFLA_MTU])
1862e7199288SPavel Emelianov 		dev->mtu = nla_get_u32(tb[IFLA_MTU]);
18632afb9b53SJiri Pirko 	if (tb[IFLA_ADDRESS]) {
1864e7199288SPavel Emelianov 		memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
1865e7199288SPavel Emelianov 				nla_len(tb[IFLA_ADDRESS]));
18662afb9b53SJiri Pirko 		dev->addr_assign_type = NET_ADDR_SET;
18672afb9b53SJiri Pirko 	}
1868e7199288SPavel Emelianov 	if (tb[IFLA_BROADCAST])
1869e7199288SPavel Emelianov 		memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
1870e7199288SPavel Emelianov 				nla_len(tb[IFLA_BROADCAST]));
1871e7199288SPavel Emelianov 	if (tb[IFLA_TXQLEN])
1872e7199288SPavel Emelianov 		dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
1873e7199288SPavel Emelianov 	if (tb[IFLA_OPERSTATE])
187493b2d4a2SDavid S. Miller 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
1875e7199288SPavel Emelianov 	if (tb[IFLA_LINKMODE])
1876e7199288SPavel Emelianov 		dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
1877ffa934f1SPatrick McHardy 	if (tb[IFLA_GROUP])
1878ffa934f1SPatrick McHardy 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
1879e7199288SPavel Emelianov 
1880e7199288SPavel Emelianov 	return dev;
1881e7199288SPavel Emelianov 
1882e7199288SPavel Emelianov err:
1883e7199288SPavel Emelianov 	return ERR_PTR(err);
1884e7199288SPavel Emelianov }
1885e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_create_link);
1886e7199288SPavel Emelianov 
188790f62cf3SEric W. Biederman static int rtnl_group_changelink(const struct sk_buff *skb,
188890f62cf3SEric W. Biederman 		struct net *net, int group,
1889e7ed828fSVlad Dogaru 		struct ifinfomsg *ifm,
1890e7ed828fSVlad Dogaru 		struct nlattr **tb)
1891e7ed828fSVlad Dogaru {
1892e7ed828fSVlad Dogaru 	struct net_device *dev;
1893e7ed828fSVlad Dogaru 	int err;
1894e7ed828fSVlad Dogaru 
1895e7ed828fSVlad Dogaru 	for_each_netdev(net, dev) {
1896e7ed828fSVlad Dogaru 		if (dev->group == group) {
189790f62cf3SEric W. Biederman 			err = do_setlink(skb, dev, ifm, tb, NULL, 0);
1898e7ed828fSVlad Dogaru 			if (err < 0)
1899e7ed828fSVlad Dogaru 				return err;
1900e7ed828fSVlad Dogaru 		}
1901e7ed828fSVlad Dogaru 	}
1902e7ed828fSVlad Dogaru 
1903e7ed828fSVlad Dogaru 	return 0;
1904e7ed828fSVlad Dogaru }
1905e7ed828fSVlad Dogaru 
1906661d2967SThomas Graf static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh)
190738f7b870SPatrick McHardy {
19083b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
190938f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops;
1910ba7d49b1SJiri Pirko 	const struct rtnl_link_ops *m_ops = NULL;
191138f7b870SPatrick McHardy 	struct net_device *dev;
1912ba7d49b1SJiri Pirko 	struct net_device *master_dev = NULL;
191338f7b870SPatrick McHardy 	struct ifinfomsg *ifm;
191438f7b870SPatrick McHardy 	char kind[MODULE_NAME_LEN];
191538f7b870SPatrick McHardy 	char ifname[IFNAMSIZ];
191638f7b870SPatrick McHardy 	struct nlattr *tb[IFLA_MAX+1];
191738f7b870SPatrick McHardy 	struct nlattr *linkinfo[IFLA_INFO_MAX+1];
19185517750fSTom Gundersen 	unsigned char name_assign_type = NET_NAME_USER;
191938f7b870SPatrick McHardy 	int err;
192038f7b870SPatrick McHardy 
192195a5afcaSJohannes Berg #ifdef CONFIG_MODULES
192238f7b870SPatrick McHardy replay:
19238072f085SThomas Graf #endif
192438f7b870SPatrick McHardy 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
192538f7b870SPatrick McHardy 	if (err < 0)
192638f7b870SPatrick McHardy 		return err;
192738f7b870SPatrick McHardy 
192838f7b870SPatrick McHardy 	if (tb[IFLA_IFNAME])
192938f7b870SPatrick McHardy 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
193038f7b870SPatrick McHardy 	else
193138f7b870SPatrick McHardy 		ifname[0] = '\0';
193238f7b870SPatrick McHardy 
193338f7b870SPatrick McHardy 	ifm = nlmsg_data(nlh);
193438f7b870SPatrick McHardy 	if (ifm->ifi_index > 0)
1935881d966bSEric W. Biederman 		dev = __dev_get_by_index(net, ifm->ifi_index);
1936e7ed828fSVlad Dogaru 	else {
1937e7ed828fSVlad Dogaru 		if (ifname[0])
1938881d966bSEric W. Biederman 			dev = __dev_get_by_name(net, ifname);
193938f7b870SPatrick McHardy 		else
194038f7b870SPatrick McHardy 			dev = NULL;
1941e7ed828fSVlad Dogaru 	}
194238f7b870SPatrick McHardy 
1943ba7d49b1SJiri Pirko 	if (dev) {
1944ba7d49b1SJiri Pirko 		master_dev = netdev_master_upper_dev_get(dev);
1945ba7d49b1SJiri Pirko 		if (master_dev)
1946ba7d49b1SJiri Pirko 			m_ops = master_dev->rtnl_link_ops;
1947ba7d49b1SJiri Pirko 	}
1948ba7d49b1SJiri Pirko 
1949e0d087afSEric Dumazet 	err = validate_linkmsg(dev, tb);
1950e0d087afSEric Dumazet 	if (err < 0)
19511840bb13SThomas Graf 		return err;
19521840bb13SThomas Graf 
195338f7b870SPatrick McHardy 	if (tb[IFLA_LINKINFO]) {
195438f7b870SPatrick McHardy 		err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
195538f7b870SPatrick McHardy 				       tb[IFLA_LINKINFO], ifla_info_policy);
195638f7b870SPatrick McHardy 		if (err < 0)
195738f7b870SPatrick McHardy 			return err;
195838f7b870SPatrick McHardy 	} else
195938f7b870SPatrick McHardy 		memset(linkinfo, 0, sizeof(linkinfo));
196038f7b870SPatrick McHardy 
196138f7b870SPatrick McHardy 	if (linkinfo[IFLA_INFO_KIND]) {
196238f7b870SPatrick McHardy 		nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
196338f7b870SPatrick McHardy 		ops = rtnl_link_ops_get(kind);
196438f7b870SPatrick McHardy 	} else {
196538f7b870SPatrick McHardy 		kind[0] = '\0';
196638f7b870SPatrick McHardy 		ops = NULL;
196738f7b870SPatrick McHardy 	}
196838f7b870SPatrick McHardy 
196938f7b870SPatrick McHardy 	if (1) {
1970ba7d49b1SJiri Pirko 		struct nlattr *attr[ops ? ops->maxtype + 1 : 0];
1971ba7d49b1SJiri Pirko 		struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 0];
1972ba7d49b1SJiri Pirko 		struct nlattr **data = NULL;
1973ba7d49b1SJiri Pirko 		struct nlattr **slave_data = NULL;
197481adee47SEric W. Biederman 		struct net *dest_net;
197538f7b870SPatrick McHardy 
197638f7b870SPatrick McHardy 		if (ops) {
197738f7b870SPatrick McHardy 			if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
197838f7b870SPatrick McHardy 				err = nla_parse_nested(attr, ops->maxtype,
197938f7b870SPatrick McHardy 						       linkinfo[IFLA_INFO_DATA],
198038f7b870SPatrick McHardy 						       ops->policy);
198138f7b870SPatrick McHardy 				if (err < 0)
198238f7b870SPatrick McHardy 					return err;
198338f7b870SPatrick McHardy 				data = attr;
198438f7b870SPatrick McHardy 			}
198538f7b870SPatrick McHardy 			if (ops->validate) {
198638f7b870SPatrick McHardy 				err = ops->validate(tb, data);
198738f7b870SPatrick McHardy 				if (err < 0)
198838f7b870SPatrick McHardy 					return err;
198938f7b870SPatrick McHardy 			}
199038f7b870SPatrick McHardy 		}
199138f7b870SPatrick McHardy 
1992ba7d49b1SJiri Pirko 		if (m_ops) {
1993ba7d49b1SJiri Pirko 			if (m_ops->slave_maxtype &&
1994ba7d49b1SJiri Pirko 			    linkinfo[IFLA_INFO_SLAVE_DATA]) {
1995ba7d49b1SJiri Pirko 				err = nla_parse_nested(slave_attr,
1996ba7d49b1SJiri Pirko 						       m_ops->slave_maxtype,
1997ba7d49b1SJiri Pirko 						       linkinfo[IFLA_INFO_SLAVE_DATA],
1998ba7d49b1SJiri Pirko 						       m_ops->slave_policy);
1999ba7d49b1SJiri Pirko 				if (err < 0)
2000ba7d49b1SJiri Pirko 					return err;
2001ba7d49b1SJiri Pirko 				slave_data = slave_attr;
2002ba7d49b1SJiri Pirko 			}
2003ba7d49b1SJiri Pirko 			if (m_ops->slave_validate) {
2004ba7d49b1SJiri Pirko 				err = m_ops->slave_validate(tb, slave_data);
2005ba7d49b1SJiri Pirko 				if (err < 0)
2006ba7d49b1SJiri Pirko 					return err;
2007ba7d49b1SJiri Pirko 			}
2008ba7d49b1SJiri Pirko 		}
2009ba7d49b1SJiri Pirko 
201038f7b870SPatrick McHardy 		if (dev) {
201190c325e3SNicolas Dichtel 			int status = 0;
201238f7b870SPatrick McHardy 
201338f7b870SPatrick McHardy 			if (nlh->nlmsg_flags & NLM_F_EXCL)
201438f7b870SPatrick McHardy 				return -EEXIST;
201538f7b870SPatrick McHardy 			if (nlh->nlmsg_flags & NLM_F_REPLACE)
201638f7b870SPatrick McHardy 				return -EOPNOTSUPP;
201738f7b870SPatrick McHardy 
201838f7b870SPatrick McHardy 			if (linkinfo[IFLA_INFO_DATA]) {
201938f7b870SPatrick McHardy 				if (!ops || ops != dev->rtnl_link_ops ||
202038f7b870SPatrick McHardy 				    !ops->changelink)
202138f7b870SPatrick McHardy 					return -EOPNOTSUPP;
202238f7b870SPatrick McHardy 
202338f7b870SPatrick McHardy 				err = ops->changelink(dev, tb, data);
202438f7b870SPatrick McHardy 				if (err < 0)
202538f7b870SPatrick McHardy 					return err;
2026ba998906SNicolas Dichtel 				status |= DO_SETLINK_NOTIFY;
202738f7b870SPatrick McHardy 			}
202838f7b870SPatrick McHardy 
2029ba7d49b1SJiri Pirko 			if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
2030ba7d49b1SJiri Pirko 				if (!m_ops || !m_ops->slave_changelink)
2031ba7d49b1SJiri Pirko 					return -EOPNOTSUPP;
2032ba7d49b1SJiri Pirko 
2033ba7d49b1SJiri Pirko 				err = m_ops->slave_changelink(master_dev, dev,
2034ba7d49b1SJiri Pirko 							      tb, slave_data);
2035ba7d49b1SJiri Pirko 				if (err < 0)
2036ba7d49b1SJiri Pirko 					return err;
2037ba998906SNicolas Dichtel 				status |= DO_SETLINK_NOTIFY;
2038ba7d49b1SJiri Pirko 			}
2039ba7d49b1SJiri Pirko 
204090c325e3SNicolas Dichtel 			return do_setlink(skb, dev, ifm, tb, ifname, status);
204138f7b870SPatrick McHardy 		}
204238f7b870SPatrick McHardy 
2043ffa934f1SPatrick McHardy 		if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
2044ffa934f1SPatrick McHardy 			if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
204590f62cf3SEric W. Biederman 				return rtnl_group_changelink(skb, net,
2046ffa934f1SPatrick McHardy 						nla_get_u32(tb[IFLA_GROUP]),
2047ffa934f1SPatrick McHardy 						ifm, tb);
204838f7b870SPatrick McHardy 			return -ENODEV;
2049ffa934f1SPatrick McHardy 		}
205038f7b870SPatrick McHardy 
20510e06877cSPatrick McHardy 		if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
205238f7b870SPatrick McHardy 			return -EOPNOTSUPP;
205338f7b870SPatrick McHardy 
205438f7b870SPatrick McHardy 		if (!ops) {
205595a5afcaSJohannes Berg #ifdef CONFIG_MODULES
205638f7b870SPatrick McHardy 			if (kind[0]) {
205738f7b870SPatrick McHardy 				__rtnl_unlock();
205838f7b870SPatrick McHardy 				request_module("rtnl-link-%s", kind);
205938f7b870SPatrick McHardy 				rtnl_lock();
206038f7b870SPatrick McHardy 				ops = rtnl_link_ops_get(kind);
206138f7b870SPatrick McHardy 				if (ops)
206238f7b870SPatrick McHardy 					goto replay;
206338f7b870SPatrick McHardy 			}
206438f7b870SPatrick McHardy #endif
206538f7b870SPatrick McHardy 			return -EOPNOTSUPP;
206638f7b870SPatrick McHardy 		}
206738f7b870SPatrick McHardy 
2068b0ab2fabSJiri Pirko 		if (!ops->setup)
2069b0ab2fabSJiri Pirko 			return -EOPNOTSUPP;
2070b0ab2fabSJiri Pirko 
20715517750fSTom Gundersen 		if (!ifname[0]) {
207238f7b870SPatrick McHardy 			snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
20735517750fSTom Gundersen 			name_assign_type = NET_NAME_ENUM;
20745517750fSTom Gundersen 		}
207538f7b870SPatrick McHardy 
207681adee47SEric W. Biederman 		dest_net = rtnl_link_get_net(net, tb);
207713ad1774SEric W. Biederman 		if (IS_ERR(dest_net))
207813ad1774SEric W. Biederman 			return PTR_ERR(dest_net);
207913ad1774SEric W. Biederman 
20805517750fSTom Gundersen 		dev = rtnl_create_link(dest_net, ifname, name_assign_type, ops, tb);
20819c7dafbfSPavel Emelyanov 		if (IS_ERR(dev)) {
2082e7199288SPavel Emelianov 			err = PTR_ERR(dev);
20839c7dafbfSPavel Emelyanov 			goto out;
20849c7dafbfSPavel Emelyanov 		}
20859c7dafbfSPavel Emelyanov 
20869c7dafbfSPavel Emelyanov 		dev->ifindex = ifm->ifi_index;
20879c7dafbfSPavel Emelyanov 
20880e0eee24SCong Wang 		if (ops->newlink) {
208981adee47SEric W. Biederman 			err = ops->newlink(net, dev, tb, data);
20900e0eee24SCong Wang 			/* Drivers should call free_netdev() in ->destructor
2091e51fb152SCong Wang 			 * and unregister it on failure after registration
2092e51fb152SCong Wang 			 * so that device could be finally freed in rtnl_unlock.
20930e0eee24SCong Wang 			 */
2094e51fb152SCong Wang 			if (err < 0) {
2095e51fb152SCong Wang 				/* If device is not registered at all, free it now */
2096e51fb152SCong Wang 				if (dev->reg_state == NETREG_UNINITIALIZED)
2097e51fb152SCong Wang 					free_netdev(dev);
20980e0eee24SCong Wang 				goto out;
2099e51fb152SCong Wang 			}
21000e0eee24SCong Wang 		} else {
21012d85cba2SPatrick McHardy 			err = register_netdevice(dev);
2102fce9b9beSDan Carpenter 			if (err < 0) {
210338f7b870SPatrick McHardy 				free_netdev(dev);
21043729d502SPatrick McHardy 				goto out;
2105fce9b9beSDan Carpenter 			}
21060e0eee24SCong Wang 		}
21073729d502SPatrick McHardy 		err = rtnl_configure_link(dev, ifm);
21083729d502SPatrick McHardy 		if (err < 0)
21093729d502SPatrick McHardy 			unregister_netdevice(dev);
21103729d502SPatrick McHardy out:
211181adee47SEric W. Biederman 		put_net(dest_net);
211238f7b870SPatrick McHardy 		return err;
211338f7b870SPatrick McHardy 	}
211438f7b870SPatrick McHardy }
211538f7b870SPatrick McHardy 
2116661d2967SThomas Graf static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh)
2117711e2c33SJean Tourrilhes {
21183b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
2119b60c5115SThomas Graf 	struct ifinfomsg *ifm;
2120a3d12891SEric Dumazet 	char ifname[IFNAMSIZ];
2121b60c5115SThomas Graf 	struct nlattr *tb[IFLA_MAX+1];
2122b60c5115SThomas Graf 	struct net_device *dev = NULL;
2123b60c5115SThomas Graf 	struct sk_buff *nskb;
2124339bf98fSThomas Graf 	int err;
2125115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
2126711e2c33SJean Tourrilhes 
2127b60c5115SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2128b60c5115SThomas Graf 	if (err < 0)
21299918f230SEric Sesterhenn 		return err;
2130b60c5115SThomas Graf 
2131a3d12891SEric Dumazet 	if (tb[IFLA_IFNAME])
2132a3d12891SEric Dumazet 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2133a3d12891SEric Dumazet 
2134115c9b81SGreg Rose 	if (tb[IFLA_EXT_MASK])
2135115c9b81SGreg Rose 		ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2136115c9b81SGreg Rose 
2137b60c5115SThomas Graf 	ifm = nlmsg_data(nlh);
2138a3d12891SEric Dumazet 	if (ifm->ifi_index > 0)
2139a3d12891SEric Dumazet 		dev = __dev_get_by_index(net, ifm->ifi_index);
2140a3d12891SEric Dumazet 	else if (tb[IFLA_IFNAME])
2141a3d12891SEric Dumazet 		dev = __dev_get_by_name(net, ifname);
2142a3d12891SEric Dumazet 	else
2143b60c5115SThomas Graf 		return -EINVAL;
2144b60c5115SThomas Graf 
2145a3d12891SEric Dumazet 	if (dev == NULL)
2146a3d12891SEric Dumazet 		return -ENODEV;
2147a3d12891SEric Dumazet 
2148115c9b81SGreg Rose 	nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
2149a3d12891SEric Dumazet 	if (nskb == NULL)
2150a3d12891SEric Dumazet 		return -ENOBUFS;
2151711e2c33SJean Tourrilhes 
215215e47304SEric W. Biederman 	err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid,
2153115c9b81SGreg Rose 			       nlh->nlmsg_seq, 0, 0, ext_filter_mask);
215426932566SPatrick McHardy 	if (err < 0) {
215526932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in if_nlmsg_size */
215626932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
215726932566SPatrick McHardy 		kfree_skb(nskb);
2158a3d12891SEric Dumazet 	} else
215915e47304SEric W. Biederman 		err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
2160b60c5115SThomas Graf 
2161711e2c33SJean Tourrilhes 	return err;
2162711e2c33SJean Tourrilhes }
2163711e2c33SJean Tourrilhes 
2164115c9b81SGreg Rose static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
2165c7ac8679SGreg Rose {
2166115c9b81SGreg Rose 	struct net *net = sock_net(skb->sk);
2167115c9b81SGreg Rose 	struct net_device *dev;
2168115c9b81SGreg Rose 	struct nlattr *tb[IFLA_MAX+1];
2169115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
2170115c9b81SGreg Rose 	u16 min_ifinfo_dump_size = 0;
2171e5eca6d4SMichal Schmidt 	int hdrlen;
2172115c9b81SGreg Rose 
2173e5eca6d4SMichal Schmidt 	/* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
2174e5eca6d4SMichal Schmidt 	hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2175e5eca6d4SMichal Schmidt 		 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2176e5eca6d4SMichal Schmidt 
2177e5eca6d4SMichal Schmidt 	if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
2178115c9b81SGreg Rose 		if (tb[IFLA_EXT_MASK])
2179115c9b81SGreg Rose 			ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2180a4b64fbeSEric Dumazet 	}
2181115c9b81SGreg Rose 
2182115c9b81SGreg Rose 	if (!ext_filter_mask)
2183115c9b81SGreg Rose 		return NLMSG_GOODSIZE;
2184115c9b81SGreg Rose 	/*
2185115c9b81SGreg Rose 	 * traverse the list of net devices and compute the minimum
2186115c9b81SGreg Rose 	 * buffer size based upon the filter mask.
2187115c9b81SGreg Rose 	 */
2188115c9b81SGreg Rose 	list_for_each_entry(dev, &net->dev_base_head, dev_list) {
2189115c9b81SGreg Rose 		min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
2190115c9b81SGreg Rose 					     if_nlmsg_size(dev,
2191115c9b81SGreg Rose 						           ext_filter_mask));
2192115c9b81SGreg Rose 	}
2193115c9b81SGreg Rose 
2194c7ac8679SGreg Rose 	return min_ifinfo_dump_size;
2195c7ac8679SGreg Rose }
2196c7ac8679SGreg Rose 
219742bad1daSAdrian Bunk static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
21981da177e4SLinus Torvalds {
21991da177e4SLinus Torvalds 	int idx;
22001da177e4SLinus Torvalds 	int s_idx = cb->family;
22011da177e4SLinus Torvalds 
22021da177e4SLinus Torvalds 	if (s_idx == 0)
22031da177e4SLinus Torvalds 		s_idx = 1;
220425239ceeSPatrick McHardy 	for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
22051da177e4SLinus Torvalds 		int type = cb->nlh->nlmsg_type-RTM_BASE;
22061da177e4SLinus Torvalds 		if (idx < s_idx || idx == PF_PACKET)
22071da177e4SLinus Torvalds 			continue;
2208e2849863SThomas Graf 		if (rtnl_msg_handlers[idx] == NULL ||
2209e2849863SThomas Graf 		    rtnl_msg_handlers[idx][type].dumpit == NULL)
22101da177e4SLinus Torvalds 			continue;
22110465277fSNicolas Dichtel 		if (idx > s_idx) {
22121da177e4SLinus Torvalds 			memset(&cb->args[0], 0, sizeof(cb->args));
22130465277fSNicolas Dichtel 			cb->prev_seq = 0;
22140465277fSNicolas Dichtel 			cb->seq = 0;
22150465277fSNicolas Dichtel 		}
2216e2849863SThomas Graf 		if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
22171da177e4SLinus Torvalds 			break;
22181da177e4SLinus Torvalds 	}
22191da177e4SLinus Torvalds 	cb->family = idx;
22201da177e4SLinus Torvalds 
22211da177e4SLinus Torvalds 	return skb->len;
22221da177e4SLinus Torvalds }
22231da177e4SLinus Torvalds 
22247f294054SAlexei Starovoitov void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
22257f294054SAlexei Starovoitov 		  gfp_t flags)
22261da177e4SLinus Torvalds {
2227c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
22281da177e4SLinus Torvalds 	struct sk_buff *skb;
22290ec6d3f4SThomas Graf 	int err = -ENOBUFS;
2230c7ac8679SGreg Rose 	size_t if_info_size;
22311da177e4SLinus Torvalds 
22327f294054SAlexei Starovoitov 	skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
22330ec6d3f4SThomas Graf 	if (skb == NULL)
22340ec6d3f4SThomas Graf 		goto errout;
22351da177e4SLinus Torvalds 
2236115c9b81SGreg Rose 	err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0);
223726932566SPatrick McHardy 	if (err < 0) {
223826932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in if_nlmsg_size() */
223926932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
224026932566SPatrick McHardy 		kfree_skb(skb);
224126932566SPatrick McHardy 		goto errout;
224226932566SPatrick McHardy 	}
22437f294054SAlexei Starovoitov 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
22441ce85fe4SPablo Neira Ayuso 	return;
22450ec6d3f4SThomas Graf errout:
22460ec6d3f4SThomas Graf 	if (err < 0)
22474b3da706SEric W. Biederman 		rtnl_set_sk_err(net, RTNLGRP_LINK, err);
22481da177e4SLinus Torvalds }
2249471cb5a3SJiri Pirko EXPORT_SYMBOL(rtmsg_ifinfo);
22501da177e4SLinus Torvalds 
2251d83b0603SJohn Fastabend static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
2252d83b0603SJohn Fastabend 				   struct net_device *dev,
2253d83b0603SJohn Fastabend 				   u8 *addr, u32 pid, u32 seq,
22541c104a6bSNicolas Dichtel 				   int type, unsigned int flags,
22551c104a6bSNicolas Dichtel 				   int nlflags)
2256d83b0603SJohn Fastabend {
2257d83b0603SJohn Fastabend 	struct nlmsghdr *nlh;
2258d83b0603SJohn Fastabend 	struct ndmsg *ndm;
2259d83b0603SJohn Fastabend 
22601c104a6bSNicolas Dichtel 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
2261d83b0603SJohn Fastabend 	if (!nlh)
2262d83b0603SJohn Fastabend 		return -EMSGSIZE;
2263d83b0603SJohn Fastabend 
2264d83b0603SJohn Fastabend 	ndm = nlmsg_data(nlh);
2265d83b0603SJohn Fastabend 	ndm->ndm_family  = AF_BRIDGE;
2266d83b0603SJohn Fastabend 	ndm->ndm_pad1	 = 0;
2267d83b0603SJohn Fastabend 	ndm->ndm_pad2    = 0;
2268d83b0603SJohn Fastabend 	ndm->ndm_flags	 = flags;
2269d83b0603SJohn Fastabend 	ndm->ndm_type	 = 0;
2270d83b0603SJohn Fastabend 	ndm->ndm_ifindex = dev->ifindex;
2271d83b0603SJohn Fastabend 	ndm->ndm_state   = NUD_PERMANENT;
2272d83b0603SJohn Fastabend 
2273d83b0603SJohn Fastabend 	if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
2274d83b0603SJohn Fastabend 		goto nla_put_failure;
2275d83b0603SJohn Fastabend 
2276d83b0603SJohn Fastabend 	return nlmsg_end(skb, nlh);
2277d83b0603SJohn Fastabend 
2278d83b0603SJohn Fastabend nla_put_failure:
2279d83b0603SJohn Fastabend 	nlmsg_cancel(skb, nlh);
2280d83b0603SJohn Fastabend 	return -EMSGSIZE;
2281d83b0603SJohn Fastabend }
2282d83b0603SJohn Fastabend 
22833ff661c3SJohn Fastabend static inline size_t rtnl_fdb_nlmsg_size(void)
22843ff661c3SJohn Fastabend {
22853ff661c3SJohn Fastabend 	return NLMSG_ALIGN(sizeof(struct ndmsg)) + nla_total_size(ETH_ALEN);
22863ff661c3SJohn Fastabend }
22873ff661c3SJohn Fastabend 
22883ff661c3SJohn Fastabend static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, int type)
22893ff661c3SJohn Fastabend {
22903ff661c3SJohn Fastabend 	struct net *net = dev_net(dev);
22913ff661c3SJohn Fastabend 	struct sk_buff *skb;
22923ff661c3SJohn Fastabend 	int err = -ENOBUFS;
22933ff661c3SJohn Fastabend 
22943ff661c3SJohn Fastabend 	skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
22953ff661c3SJohn Fastabend 	if (!skb)
22963ff661c3SJohn Fastabend 		goto errout;
22973ff661c3SJohn Fastabend 
22981c104a6bSNicolas Dichtel 	err = nlmsg_populate_fdb_fill(skb, dev, addr, 0, 0, type, NTF_SELF, 0);
22993ff661c3SJohn Fastabend 	if (err < 0) {
23003ff661c3SJohn Fastabend 		kfree_skb(skb);
23013ff661c3SJohn Fastabend 		goto errout;
23023ff661c3SJohn Fastabend 	}
23033ff661c3SJohn Fastabend 
23043ff661c3SJohn Fastabend 	rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
23053ff661c3SJohn Fastabend 	return;
23063ff661c3SJohn Fastabend errout:
23073ff661c3SJohn Fastabend 	rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
23083ff661c3SJohn Fastabend }
23093ff661c3SJohn Fastabend 
2310090096bfSVlad Yasevich /**
2311090096bfSVlad Yasevich  * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
2312090096bfSVlad Yasevich  */
2313090096bfSVlad Yasevich int ndo_dflt_fdb_add(struct ndmsg *ndm,
2314090096bfSVlad Yasevich 		     struct nlattr *tb[],
2315090096bfSVlad Yasevich 		     struct net_device *dev,
2316*f6f6424bSJiri Pirko 		     const unsigned char *addr, u16 vid,
2317090096bfSVlad Yasevich 		     u16 flags)
2318090096bfSVlad Yasevich {
2319090096bfSVlad Yasevich 	int err = -EINVAL;
2320090096bfSVlad Yasevich 
2321090096bfSVlad Yasevich 	/* If aging addresses are supported device will need to
2322090096bfSVlad Yasevich 	 * implement its own handler for this.
2323090096bfSVlad Yasevich 	 */
2324090096bfSVlad Yasevich 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
2325090096bfSVlad Yasevich 		pr_info("%s: FDB only supports static addresses\n", dev->name);
2326090096bfSVlad Yasevich 		return err;
2327090096bfSVlad Yasevich 	}
2328090096bfSVlad Yasevich 
2329090096bfSVlad Yasevich 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2330090096bfSVlad Yasevich 		err = dev_uc_add_excl(dev, addr);
2331090096bfSVlad Yasevich 	else if (is_multicast_ether_addr(addr))
2332090096bfSVlad Yasevich 		err = dev_mc_add_excl(dev, addr);
2333090096bfSVlad Yasevich 
2334090096bfSVlad Yasevich 	/* Only return duplicate errors if NLM_F_EXCL is set */
2335090096bfSVlad Yasevich 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
2336090096bfSVlad Yasevich 		err = 0;
2337090096bfSVlad Yasevich 
2338090096bfSVlad Yasevich 	return err;
2339090096bfSVlad Yasevich }
2340090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_add);
2341090096bfSVlad Yasevich 
2342*f6f6424bSJiri Pirko static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid)
2343*f6f6424bSJiri Pirko {
2344*f6f6424bSJiri Pirko 	u16 vid = 0;
2345*f6f6424bSJiri Pirko 
2346*f6f6424bSJiri Pirko 	if (vlan_attr) {
2347*f6f6424bSJiri Pirko 		if (nla_len(vlan_attr) != sizeof(u16)) {
2348*f6f6424bSJiri Pirko 			pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan\n");
2349*f6f6424bSJiri Pirko 			return -EINVAL;
2350*f6f6424bSJiri Pirko 		}
2351*f6f6424bSJiri Pirko 
2352*f6f6424bSJiri Pirko 		vid = nla_get_u16(vlan_attr);
2353*f6f6424bSJiri Pirko 
2354*f6f6424bSJiri Pirko 		if (!vid || vid >= VLAN_VID_MASK) {
2355*f6f6424bSJiri Pirko 			pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan id %d\n",
2356*f6f6424bSJiri Pirko 				vid);
2357*f6f6424bSJiri Pirko 			return -EINVAL;
2358*f6f6424bSJiri Pirko 		}
2359*f6f6424bSJiri Pirko 	}
2360*f6f6424bSJiri Pirko 	*p_vid = vid;
2361*f6f6424bSJiri Pirko 	return 0;
2362*f6f6424bSJiri Pirko }
2363*f6f6424bSJiri Pirko 
2364661d2967SThomas Graf static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh)
236577162022SJohn Fastabend {
236677162022SJohn Fastabend 	struct net *net = sock_net(skb->sk);
236777162022SJohn Fastabend 	struct ndmsg *ndm;
236877162022SJohn Fastabend 	struct nlattr *tb[NDA_MAX+1];
236977162022SJohn Fastabend 	struct net_device *dev;
237077162022SJohn Fastabend 	u8 *addr;
2371*f6f6424bSJiri Pirko 	u16 vid;
237277162022SJohn Fastabend 	int err;
237377162022SJohn Fastabend 
237477162022SJohn Fastabend 	err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
237577162022SJohn Fastabend 	if (err < 0)
237677162022SJohn Fastabend 		return err;
237777162022SJohn Fastabend 
237877162022SJohn Fastabend 	ndm = nlmsg_data(nlh);
237977162022SJohn Fastabend 	if (ndm->ndm_ifindex == 0) {
238077162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n");
238177162022SJohn Fastabend 		return -EINVAL;
238277162022SJohn Fastabend 	}
238377162022SJohn Fastabend 
238477162022SJohn Fastabend 	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
238577162022SJohn Fastabend 	if (dev == NULL) {
238677162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n");
238777162022SJohn Fastabend 		return -ENODEV;
238877162022SJohn Fastabend 	}
238977162022SJohn Fastabend 
239077162022SJohn Fastabend 	if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
239177162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n");
239277162022SJohn Fastabend 		return -EINVAL;
239377162022SJohn Fastabend 	}
239477162022SJohn Fastabend 
239577162022SJohn Fastabend 	addr = nla_data(tb[NDA_LLADDR]);
239677162022SJohn Fastabend 
2397*f6f6424bSJiri Pirko 	err = fdb_vid_parse(tb[NDA_VLAN], &vid);
2398*f6f6424bSJiri Pirko 	if (err)
2399*f6f6424bSJiri Pirko 		return err;
2400*f6f6424bSJiri Pirko 
240177162022SJohn Fastabend 	err = -EOPNOTSUPP;
240277162022SJohn Fastabend 
240377162022SJohn Fastabend 	/* Support fdb on master device the net/bridge default case */
240477162022SJohn Fastabend 	if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
240577162022SJohn Fastabend 	    (dev->priv_flags & IFF_BRIDGE_PORT)) {
2406898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2407898e5061SJiri Pirko 		const struct net_device_ops *ops = br_dev->netdev_ops;
2408898e5061SJiri Pirko 
2409*f6f6424bSJiri Pirko 		err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
2410*f6f6424bSJiri Pirko 				       nlh->nlmsg_flags);
241177162022SJohn Fastabend 		if (err)
241277162022SJohn Fastabend 			goto out;
241377162022SJohn Fastabend 		else
241477162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_MASTER;
241577162022SJohn Fastabend 	}
241677162022SJohn Fastabend 
241777162022SJohn Fastabend 	/* Embedded bridge, macvlan, and any other device support */
2418090096bfSVlad Yasevich 	if ((ndm->ndm_flags & NTF_SELF)) {
2419090096bfSVlad Yasevich 		if (dev->netdev_ops->ndo_fdb_add)
2420090096bfSVlad Yasevich 			err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
2421*f6f6424bSJiri Pirko 							   vid,
2422090096bfSVlad Yasevich 							   nlh->nlmsg_flags);
2423090096bfSVlad Yasevich 		else
2424*f6f6424bSJiri Pirko 			err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
242577162022SJohn Fastabend 					       nlh->nlmsg_flags);
242677162022SJohn Fastabend 
24273ff661c3SJohn Fastabend 		if (!err) {
24283ff661c3SJohn Fastabend 			rtnl_fdb_notify(dev, addr, RTM_NEWNEIGH);
242977162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_SELF;
243077162022SJohn Fastabend 		}
24313ff661c3SJohn Fastabend 	}
243277162022SJohn Fastabend out:
243377162022SJohn Fastabend 	return err;
243477162022SJohn Fastabend }
243577162022SJohn Fastabend 
2436090096bfSVlad Yasevich /**
2437090096bfSVlad Yasevich  * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
2438090096bfSVlad Yasevich  */
2439090096bfSVlad Yasevich int ndo_dflt_fdb_del(struct ndmsg *ndm,
2440090096bfSVlad Yasevich 		     struct nlattr *tb[],
2441090096bfSVlad Yasevich 		     struct net_device *dev,
2442*f6f6424bSJiri Pirko 		     const unsigned char *addr, u16 vid)
2443090096bfSVlad Yasevich {
2444c8a89c4aSAlexander Duyck 	int err = -EINVAL;
2445090096bfSVlad Yasevich 
2446090096bfSVlad Yasevich 	/* If aging addresses are supported device will need to
2447090096bfSVlad Yasevich 	 * implement its own handler for this.
2448090096bfSVlad Yasevich 	 */
244964535993SSridhar Samudrala 	if (!(ndm->ndm_state & NUD_PERMANENT)) {
2450090096bfSVlad Yasevich 		pr_info("%s: FDB only supports static addresses\n", dev->name);
2451c8a89c4aSAlexander Duyck 		return err;
2452090096bfSVlad Yasevich 	}
2453090096bfSVlad Yasevich 
2454090096bfSVlad Yasevich 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2455090096bfSVlad Yasevich 		err = dev_uc_del(dev, addr);
2456090096bfSVlad Yasevich 	else if (is_multicast_ether_addr(addr))
2457090096bfSVlad Yasevich 		err = dev_mc_del(dev, addr);
2458090096bfSVlad Yasevich 
2459090096bfSVlad Yasevich 	return err;
2460090096bfSVlad Yasevich }
2461090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_del);
2462090096bfSVlad Yasevich 
2463661d2967SThomas Graf static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh)
246477162022SJohn Fastabend {
246577162022SJohn Fastabend 	struct net *net = sock_net(skb->sk);
246677162022SJohn Fastabend 	struct ndmsg *ndm;
24671690be63SVlad Yasevich 	struct nlattr *tb[NDA_MAX+1];
246877162022SJohn Fastabend 	struct net_device *dev;
246977162022SJohn Fastabend 	int err = -EINVAL;
247077162022SJohn Fastabend 	__u8 *addr;
2471*f6f6424bSJiri Pirko 	u16 vid;
247277162022SJohn Fastabend 
247390f62cf3SEric W. Biederman 	if (!netlink_capable(skb, CAP_NET_ADMIN))
24741690be63SVlad Yasevich 		return -EPERM;
24751690be63SVlad Yasevich 
24761690be63SVlad Yasevich 	err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
24771690be63SVlad Yasevich 	if (err < 0)
24781690be63SVlad Yasevich 		return err;
247977162022SJohn Fastabend 
248077162022SJohn Fastabend 	ndm = nlmsg_data(nlh);
248177162022SJohn Fastabend 	if (ndm->ndm_ifindex == 0) {
248277162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n");
248377162022SJohn Fastabend 		return -EINVAL;
248477162022SJohn Fastabend 	}
248577162022SJohn Fastabend 
248677162022SJohn Fastabend 	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
248777162022SJohn Fastabend 	if (dev == NULL) {
248877162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n");
248977162022SJohn Fastabend 		return -ENODEV;
249077162022SJohn Fastabend 	}
249177162022SJohn Fastabend 
24921690be63SVlad Yasevich 	if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
24931690be63SVlad Yasevich 		pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n");
249477162022SJohn Fastabend 		return -EINVAL;
249577162022SJohn Fastabend 	}
249677162022SJohn Fastabend 
24971690be63SVlad Yasevich 	addr = nla_data(tb[NDA_LLADDR]);
24981690be63SVlad Yasevich 
2499*f6f6424bSJiri Pirko 	err = fdb_vid_parse(tb[NDA_VLAN], &vid);
2500*f6f6424bSJiri Pirko 	if (err)
2501*f6f6424bSJiri Pirko 		return err;
2502*f6f6424bSJiri Pirko 
250377162022SJohn Fastabend 	err = -EOPNOTSUPP;
250477162022SJohn Fastabend 
250577162022SJohn Fastabend 	/* Support fdb on master device the net/bridge default case */
250677162022SJohn Fastabend 	if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
250777162022SJohn Fastabend 	    (dev->priv_flags & IFF_BRIDGE_PORT)) {
2508898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2509898e5061SJiri Pirko 		const struct net_device_ops *ops = br_dev->netdev_ops;
251077162022SJohn Fastabend 
2511898e5061SJiri Pirko 		if (ops->ndo_fdb_del)
2512*f6f6424bSJiri Pirko 			err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
251377162022SJohn Fastabend 
251477162022SJohn Fastabend 		if (err)
251577162022SJohn Fastabend 			goto out;
251677162022SJohn Fastabend 		else
251777162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_MASTER;
251877162022SJohn Fastabend 	}
251977162022SJohn Fastabend 
252077162022SJohn Fastabend 	/* Embedded bridge, macvlan, and any other device support */
2521090096bfSVlad Yasevich 	if (ndm->ndm_flags & NTF_SELF) {
2522090096bfSVlad Yasevich 		if (dev->netdev_ops->ndo_fdb_del)
2523*f6f6424bSJiri Pirko 			err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
2524*f6f6424bSJiri Pirko 							   vid);
2525090096bfSVlad Yasevich 		else
2526*f6f6424bSJiri Pirko 			err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
252777162022SJohn Fastabend 
25283ff661c3SJohn Fastabend 		if (!err) {
25293ff661c3SJohn Fastabend 			rtnl_fdb_notify(dev, addr, RTM_DELNEIGH);
253077162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_SELF;
253177162022SJohn Fastabend 		}
25323ff661c3SJohn Fastabend 	}
253377162022SJohn Fastabend out:
253477162022SJohn Fastabend 	return err;
253577162022SJohn Fastabend }
253677162022SJohn Fastabend 
2537d83b0603SJohn Fastabend static int nlmsg_populate_fdb(struct sk_buff *skb,
2538d83b0603SJohn Fastabend 			      struct netlink_callback *cb,
2539d83b0603SJohn Fastabend 			      struct net_device *dev,
2540d83b0603SJohn Fastabend 			      int *idx,
2541d83b0603SJohn Fastabend 			      struct netdev_hw_addr_list *list)
2542d83b0603SJohn Fastabend {
2543d83b0603SJohn Fastabend 	struct netdev_hw_addr *ha;
2544d83b0603SJohn Fastabend 	int err;
254515e47304SEric W. Biederman 	u32 portid, seq;
2546d83b0603SJohn Fastabend 
254715e47304SEric W. Biederman 	portid = NETLINK_CB(cb->skb).portid;
2548d83b0603SJohn Fastabend 	seq = cb->nlh->nlmsg_seq;
2549d83b0603SJohn Fastabend 
2550d83b0603SJohn Fastabend 	list_for_each_entry(ha, &list->list, list) {
2551d83b0603SJohn Fastabend 		if (*idx < cb->args[0])
2552d83b0603SJohn Fastabend 			goto skip;
2553d83b0603SJohn Fastabend 
2554d83b0603SJohn Fastabend 		err = nlmsg_populate_fdb_fill(skb, dev, ha->addr,
2555a7a558feSJohn Fastabend 					      portid, seq,
25561c104a6bSNicolas Dichtel 					      RTM_NEWNEIGH, NTF_SELF,
25571c104a6bSNicolas Dichtel 					      NLM_F_MULTI);
2558d83b0603SJohn Fastabend 		if (err < 0)
2559d83b0603SJohn Fastabend 			return err;
2560d83b0603SJohn Fastabend skip:
2561d83b0603SJohn Fastabend 		*idx += 1;
2562d83b0603SJohn Fastabend 	}
2563d83b0603SJohn Fastabend 	return 0;
2564d83b0603SJohn Fastabend }
2565d83b0603SJohn Fastabend 
2566d83b0603SJohn Fastabend /**
25672c53040fSBen Hutchings  * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
2568d83b0603SJohn Fastabend  * @nlh: netlink message header
2569d83b0603SJohn Fastabend  * @dev: netdevice
2570d83b0603SJohn Fastabend  *
2571d83b0603SJohn Fastabend  * Default netdevice operation to dump the existing unicast address list.
257291f3e7b1SJohn Fastabend  * Returns number of addresses from list put in skb.
2573d83b0603SJohn Fastabend  */
2574d83b0603SJohn Fastabend int ndo_dflt_fdb_dump(struct sk_buff *skb,
2575d83b0603SJohn Fastabend 		      struct netlink_callback *cb,
2576d83b0603SJohn Fastabend 		      struct net_device *dev,
25775d5eacb3SJamal Hadi Salim 		      struct net_device *filter_dev,
2578d83b0603SJohn Fastabend 		      int idx)
2579d83b0603SJohn Fastabend {
2580d83b0603SJohn Fastabend 	int err;
2581d83b0603SJohn Fastabend 
2582d83b0603SJohn Fastabend 	netif_addr_lock_bh(dev);
2583d83b0603SJohn Fastabend 	err = nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->uc);
2584d83b0603SJohn Fastabend 	if (err)
2585d83b0603SJohn Fastabend 		goto out;
2586d83b0603SJohn Fastabend 	nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->mc);
2587d83b0603SJohn Fastabend out:
2588d83b0603SJohn Fastabend 	netif_addr_unlock_bh(dev);
2589d83b0603SJohn Fastabend 	return idx;
2590d83b0603SJohn Fastabend }
2591d83b0603SJohn Fastabend EXPORT_SYMBOL(ndo_dflt_fdb_dump);
2592d83b0603SJohn Fastabend 
259377162022SJohn Fastabend static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
259477162022SJohn Fastabend {
259577162022SJohn Fastabend 	struct net_device *dev;
25965e6d2435SJamal Hadi Salim 	struct nlattr *tb[IFLA_MAX+1];
25975e6d2435SJamal Hadi Salim 	struct net_device *bdev = NULL;
25985e6d2435SJamal Hadi Salim 	struct net_device *br_dev = NULL;
25995e6d2435SJamal Hadi Salim 	const struct net_device_ops *ops = NULL;
26005e6d2435SJamal Hadi Salim 	const struct net_device_ops *cops = NULL;
26015e6d2435SJamal Hadi Salim 	struct ifinfomsg *ifm = nlmsg_data(cb->nlh);
26025e6d2435SJamal Hadi Salim 	struct net *net = sock_net(skb->sk);
26035e6d2435SJamal Hadi Salim 	int brport_idx = 0;
26045e6d2435SJamal Hadi Salim 	int br_idx = 0;
26055e6d2435SJamal Hadi Salim 	int idx = 0;
260677162022SJohn Fastabend 
26075e6d2435SJamal Hadi Salim 	if (nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX,
26085e6d2435SJamal Hadi Salim 			ifla_policy) == 0) {
26095e6d2435SJamal Hadi Salim 		if (tb[IFLA_MASTER])
26105e6d2435SJamal Hadi Salim 			br_idx = nla_get_u32(tb[IFLA_MASTER]);
26115e6d2435SJamal Hadi Salim 	}
261277162022SJohn Fastabend 
26135e6d2435SJamal Hadi Salim 	brport_idx = ifm->ifi_index;
26145e6d2435SJamal Hadi Salim 
26155e6d2435SJamal Hadi Salim 	if (br_idx) {
26165e6d2435SJamal Hadi Salim 		br_dev = __dev_get_by_index(net, br_idx);
26175e6d2435SJamal Hadi Salim 		if (!br_dev)
26185e6d2435SJamal Hadi Salim 			return -ENODEV;
26195e6d2435SJamal Hadi Salim 
2620898e5061SJiri Pirko 		ops = br_dev->netdev_ops;
26215e6d2435SJamal Hadi Salim 		bdev = br_dev;
26225e6d2435SJamal Hadi Salim 	}
26235e6d2435SJamal Hadi Salim 
26245e6d2435SJamal Hadi Salim 	for_each_netdev(net, dev) {
26255e6d2435SJamal Hadi Salim 		if (brport_idx && (dev->ifindex != brport_idx))
26265e6d2435SJamal Hadi Salim 			continue;
26275e6d2435SJamal Hadi Salim 
26285e6d2435SJamal Hadi Salim 		if (!br_idx) { /* user did not specify a specific bridge */
26295e6d2435SJamal Hadi Salim 			if (dev->priv_flags & IFF_BRIDGE_PORT) {
26305e6d2435SJamal Hadi Salim 				br_dev = netdev_master_upper_dev_get(dev);
26315e6d2435SJamal Hadi Salim 				cops = br_dev->netdev_ops;
26325e6d2435SJamal Hadi Salim 			}
26335e6d2435SJamal Hadi Salim 
26345e6d2435SJamal Hadi Salim 			bdev = dev;
26355e6d2435SJamal Hadi Salim 		} else {
26365e6d2435SJamal Hadi Salim 			if (dev != br_dev &&
26375e6d2435SJamal Hadi Salim 			    !(dev->priv_flags & IFF_BRIDGE_PORT))
26385e6d2435SJamal Hadi Salim 				continue;
26395e6d2435SJamal Hadi Salim 
26405e6d2435SJamal Hadi Salim 			if (br_dev != netdev_master_upper_dev_get(dev) &&
26415e6d2435SJamal Hadi Salim 			    !(dev->priv_flags & IFF_EBRIDGE))
26425e6d2435SJamal Hadi Salim 				continue;
26435e6d2435SJamal Hadi Salim 
26445e6d2435SJamal Hadi Salim 			bdev = br_dev;
26455e6d2435SJamal Hadi Salim 			cops = ops;
26465e6d2435SJamal Hadi Salim 		}
26475e6d2435SJamal Hadi Salim 
26485e6d2435SJamal Hadi Salim 		if (dev->priv_flags & IFF_BRIDGE_PORT) {
26495e6d2435SJamal Hadi Salim 			if (cops && cops->ndo_fdb_dump)
26505e6d2435SJamal Hadi Salim 				idx = cops->ndo_fdb_dump(skb, cb, br_dev, dev,
26515d5eacb3SJamal Hadi Salim 							 idx);
265277162022SJohn Fastabend 		}
265377162022SJohn Fastabend 
26545d5eacb3SJamal Hadi Salim 		idx = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx);
26555e6d2435SJamal Hadi Salim 		if (dev->netdev_ops->ndo_fdb_dump)
26565e6d2435SJamal Hadi Salim 			idx = dev->netdev_ops->ndo_fdb_dump(skb, cb, bdev, dev,
26575e6d2435SJamal Hadi Salim 							    idx);
26585e6d2435SJamal Hadi Salim 
26595e6d2435SJamal Hadi Salim 		cops = NULL;
266077162022SJohn Fastabend 	}
266177162022SJohn Fastabend 
266277162022SJohn Fastabend 	cb->args[0] = idx;
266377162022SJohn Fastabend 	return skb->len;
266477162022SJohn Fastabend }
266577162022SJohn Fastabend 
2666815cccbfSJohn Fastabend int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
2667815cccbfSJohn Fastabend 			    struct net_device *dev, u16 mode)
2668815cccbfSJohn Fastabend {
2669815cccbfSJohn Fastabend 	struct nlmsghdr *nlh;
2670815cccbfSJohn Fastabend 	struct ifinfomsg *ifm;
2671815cccbfSJohn Fastabend 	struct nlattr *br_afspec;
2672815cccbfSJohn Fastabend 	u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
2673898e5061SJiri Pirko 	struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2674815cccbfSJohn Fastabend 
2675815cccbfSJohn Fastabend 	nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), NLM_F_MULTI);
2676815cccbfSJohn Fastabend 	if (nlh == NULL)
2677815cccbfSJohn Fastabend 		return -EMSGSIZE;
2678815cccbfSJohn Fastabend 
2679815cccbfSJohn Fastabend 	ifm = nlmsg_data(nlh);
2680815cccbfSJohn Fastabend 	ifm->ifi_family = AF_BRIDGE;
2681815cccbfSJohn Fastabend 	ifm->__ifi_pad = 0;
2682815cccbfSJohn Fastabend 	ifm->ifi_type = dev->type;
2683815cccbfSJohn Fastabend 	ifm->ifi_index = dev->ifindex;
2684815cccbfSJohn Fastabend 	ifm->ifi_flags = dev_get_flags(dev);
2685815cccbfSJohn Fastabend 	ifm->ifi_change = 0;
2686815cccbfSJohn Fastabend 
2687815cccbfSJohn Fastabend 
2688815cccbfSJohn Fastabend 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
2689815cccbfSJohn Fastabend 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
2690815cccbfSJohn Fastabend 	    nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
2691898e5061SJiri Pirko 	    (br_dev &&
2692898e5061SJiri Pirko 	     nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
2693815cccbfSJohn Fastabend 	    (dev->addr_len &&
2694815cccbfSJohn Fastabend 	     nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
2695815cccbfSJohn Fastabend 	    (dev->ifindex != dev->iflink &&
2696815cccbfSJohn Fastabend 	     nla_put_u32(skb, IFLA_LINK, dev->iflink)))
2697815cccbfSJohn Fastabend 		goto nla_put_failure;
2698815cccbfSJohn Fastabend 
2699815cccbfSJohn Fastabend 	br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
2700815cccbfSJohn Fastabend 	if (!br_afspec)
2701815cccbfSJohn Fastabend 		goto nla_put_failure;
2702815cccbfSJohn Fastabend 
2703815cccbfSJohn Fastabend 	if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF) ||
2704815cccbfSJohn Fastabend 	    nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
2705815cccbfSJohn Fastabend 		nla_nest_cancel(skb, br_afspec);
2706815cccbfSJohn Fastabend 		goto nla_put_failure;
2707815cccbfSJohn Fastabend 	}
2708815cccbfSJohn Fastabend 	nla_nest_end(skb, br_afspec);
2709815cccbfSJohn Fastabend 
2710815cccbfSJohn Fastabend 	return nlmsg_end(skb, nlh);
2711815cccbfSJohn Fastabend nla_put_failure:
2712815cccbfSJohn Fastabend 	nlmsg_cancel(skb, nlh);
2713815cccbfSJohn Fastabend 	return -EMSGSIZE;
2714815cccbfSJohn Fastabend }
2715815cccbfSJohn Fastabend EXPORT_SYMBOL(ndo_dflt_bridge_getlink);
2716815cccbfSJohn Fastabend 
2717e5a55a89SJohn Fastabend static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
2718e5a55a89SJohn Fastabend {
2719e5a55a89SJohn Fastabend 	struct net *net = sock_net(skb->sk);
2720e5a55a89SJohn Fastabend 	struct net_device *dev;
2721e5a55a89SJohn Fastabend 	int idx = 0;
2722e5a55a89SJohn Fastabend 	u32 portid = NETLINK_CB(cb->skb).portid;
2723e5a55a89SJohn Fastabend 	u32 seq = cb->nlh->nlmsg_seq;
27246cbdceebSVlad Yasevich 	u32 filter_mask = 0;
27256cbdceebSVlad Yasevich 
2726aa68c20fSThomas Graf 	if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) {
2727aa68c20fSThomas Graf 		struct nlattr *extfilt;
2728aa68c20fSThomas Graf 
27293e805ad2SAsbjoern Sloth Toennesen 		extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
27306cbdceebSVlad Yasevich 					  IFLA_EXT_MASK);
2731aa68c20fSThomas Graf 		if (extfilt) {
2732aa68c20fSThomas Graf 			if (nla_len(extfilt) < sizeof(filter_mask))
2733aa68c20fSThomas Graf 				return -EINVAL;
2734aa68c20fSThomas Graf 
27356cbdceebSVlad Yasevich 			filter_mask = nla_get_u32(extfilt);
2736aa68c20fSThomas Graf 		}
2737aa68c20fSThomas Graf 	}
2738e5a55a89SJohn Fastabend 
2739e5a55a89SJohn Fastabend 	rcu_read_lock();
2740e5a55a89SJohn Fastabend 	for_each_netdev_rcu(net, dev) {
2741e5a55a89SJohn Fastabend 		const struct net_device_ops *ops = dev->netdev_ops;
2742898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2743e5a55a89SJohn Fastabend 
2744898e5061SJiri Pirko 		if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
274525b1e679SBen Hutchings 			if (idx >= cb->args[0] &&
2746898e5061SJiri Pirko 			    br_dev->netdev_ops->ndo_bridge_getlink(
27476cbdceebSVlad Yasevich 				    skb, portid, seq, dev, filter_mask) < 0)
2748e5a55a89SJohn Fastabend 				break;
2749e5a55a89SJohn Fastabend 			idx++;
2750e5a55a89SJohn Fastabend 		}
2751e5a55a89SJohn Fastabend 
2752e5a55a89SJohn Fastabend 		if (ops->ndo_bridge_getlink) {
275325b1e679SBen Hutchings 			if (idx >= cb->args[0] &&
27546cbdceebSVlad Yasevich 			    ops->ndo_bridge_getlink(skb, portid, seq, dev,
27556cbdceebSVlad Yasevich 						    filter_mask) < 0)
2756e5a55a89SJohn Fastabend 				break;
2757e5a55a89SJohn Fastabend 			idx++;
2758e5a55a89SJohn Fastabend 		}
2759e5a55a89SJohn Fastabend 	}
2760e5a55a89SJohn Fastabend 	rcu_read_unlock();
2761e5a55a89SJohn Fastabend 	cb->args[0] = idx;
2762e5a55a89SJohn Fastabend 
2763e5a55a89SJohn Fastabend 	return skb->len;
2764e5a55a89SJohn Fastabend }
2765e5a55a89SJohn Fastabend 
27662469ffd7SJohn Fastabend static inline size_t bridge_nlmsg_size(void)
27672469ffd7SJohn Fastabend {
27682469ffd7SJohn Fastabend 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
27692469ffd7SJohn Fastabend 		+ nla_total_size(IFNAMSIZ)	/* IFLA_IFNAME */
27702469ffd7SJohn Fastabend 		+ nla_total_size(MAX_ADDR_LEN)	/* IFLA_ADDRESS */
27712469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_MASTER */
27722469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_MTU */
27732469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_LINK */
27742469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_OPERSTATE */
27752469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u8))	/* IFLA_PROTINFO */
27762469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(struct nlattr))	/* IFLA_AF_SPEC */
27772469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u16))	/* IFLA_BRIDGE_FLAGS */
27782469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u16));	/* IFLA_BRIDGE_MODE */
27792469ffd7SJohn Fastabend }
27802469ffd7SJohn Fastabend 
27812469ffd7SJohn Fastabend static int rtnl_bridge_notify(struct net_device *dev, u16 flags)
27822469ffd7SJohn Fastabend {
27832469ffd7SJohn Fastabend 	struct net *net = dev_net(dev);
2784898e5061SJiri Pirko 	struct net_device *br_dev = netdev_master_upper_dev_get(dev);
27852469ffd7SJohn Fastabend 	struct sk_buff *skb;
27862469ffd7SJohn Fastabend 	int err = -EOPNOTSUPP;
27872469ffd7SJohn Fastabend 
27882469ffd7SJohn Fastabend 	skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
27892469ffd7SJohn Fastabend 	if (!skb) {
27902469ffd7SJohn Fastabend 		err = -ENOMEM;
27912469ffd7SJohn Fastabend 		goto errout;
27922469ffd7SJohn Fastabend 	}
27932469ffd7SJohn Fastabend 
2794c38e01b8SJohn Fastabend 	if ((!flags || (flags & BRIDGE_FLAGS_MASTER)) &&
2795898e5061SJiri Pirko 	    br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
27966cbdceebSVlad Yasevich 		err = br_dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0);
27972469ffd7SJohn Fastabend 		if (err < 0)
27982469ffd7SJohn Fastabend 			goto errout;
2799c38e01b8SJohn Fastabend 	}
2800c38e01b8SJohn Fastabend 
2801c38e01b8SJohn Fastabend 	if ((flags & BRIDGE_FLAGS_SELF) &&
2802c38e01b8SJohn Fastabend 	    dev->netdev_ops->ndo_bridge_getlink) {
28036cbdceebSVlad Yasevich 		err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0);
2804c38e01b8SJohn Fastabend 		if (err < 0)
2805c38e01b8SJohn Fastabend 			goto errout;
2806c38e01b8SJohn Fastabend 	}
28072469ffd7SJohn Fastabend 
28082469ffd7SJohn Fastabend 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
28092469ffd7SJohn Fastabend 	return 0;
28102469ffd7SJohn Fastabend errout:
28112469ffd7SJohn Fastabend 	WARN_ON(err == -EMSGSIZE);
28122469ffd7SJohn Fastabend 	kfree_skb(skb);
28132469ffd7SJohn Fastabend 	rtnl_set_sk_err(net, RTNLGRP_LINK, err);
28142469ffd7SJohn Fastabend 	return err;
28152469ffd7SJohn Fastabend }
28162469ffd7SJohn Fastabend 
2817661d2967SThomas Graf static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
2818e5a55a89SJohn Fastabend {
2819e5a55a89SJohn Fastabend 	struct net *net = sock_net(skb->sk);
2820e5a55a89SJohn Fastabend 	struct ifinfomsg *ifm;
2821e5a55a89SJohn Fastabend 	struct net_device *dev;
28222469ffd7SJohn Fastabend 	struct nlattr *br_spec, *attr = NULL;
28232469ffd7SJohn Fastabend 	int rem, err = -EOPNOTSUPP;
2824c38e01b8SJohn Fastabend 	u16 oflags, flags = 0;
2825c38e01b8SJohn Fastabend 	bool have_flags = false;
2826e5a55a89SJohn Fastabend 
2827e5a55a89SJohn Fastabend 	if (nlmsg_len(nlh) < sizeof(*ifm))
2828e5a55a89SJohn Fastabend 		return -EINVAL;
2829e5a55a89SJohn Fastabend 
2830e5a55a89SJohn Fastabend 	ifm = nlmsg_data(nlh);
2831e5a55a89SJohn Fastabend 	if (ifm->ifi_family != AF_BRIDGE)
2832e5a55a89SJohn Fastabend 		return -EPFNOSUPPORT;
2833e5a55a89SJohn Fastabend 
2834e5a55a89SJohn Fastabend 	dev = __dev_get_by_index(net, ifm->ifi_index);
2835e5a55a89SJohn Fastabend 	if (!dev) {
2836e5a55a89SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
2837e5a55a89SJohn Fastabend 		return -ENODEV;
2838e5a55a89SJohn Fastabend 	}
2839e5a55a89SJohn Fastabend 
28402469ffd7SJohn Fastabend 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
28412469ffd7SJohn Fastabend 	if (br_spec) {
28422469ffd7SJohn Fastabend 		nla_for_each_nested(attr, br_spec, rem) {
28432469ffd7SJohn Fastabend 			if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
28446e8d1c55SThomas Graf 				if (nla_len(attr) < sizeof(flags))
28456e8d1c55SThomas Graf 					return -EINVAL;
28466e8d1c55SThomas Graf 
2847c38e01b8SJohn Fastabend 				have_flags = true;
28482469ffd7SJohn Fastabend 				flags = nla_get_u16(attr);
28492469ffd7SJohn Fastabend 				break;
28502469ffd7SJohn Fastabend 			}
28512469ffd7SJohn Fastabend 		}
28522469ffd7SJohn Fastabend 	}
28532469ffd7SJohn Fastabend 
2854c38e01b8SJohn Fastabend 	oflags = flags;
2855c38e01b8SJohn Fastabend 
28562469ffd7SJohn Fastabend 	if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
2857898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2858898e5061SJiri Pirko 
2859898e5061SJiri Pirko 		if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
28602469ffd7SJohn Fastabend 			err = -EOPNOTSUPP;
2861e5a55a89SJohn Fastabend 			goto out;
2862e5a55a89SJohn Fastabend 		}
2863e5a55a89SJohn Fastabend 
2864898e5061SJiri Pirko 		err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh);
28652469ffd7SJohn Fastabend 		if (err)
28662469ffd7SJohn Fastabend 			goto out;
28672469ffd7SJohn Fastabend 
28682469ffd7SJohn Fastabend 		flags &= ~BRIDGE_FLAGS_MASTER;
28692469ffd7SJohn Fastabend 	}
28702469ffd7SJohn Fastabend 
28712469ffd7SJohn Fastabend 	if ((flags & BRIDGE_FLAGS_SELF)) {
28722469ffd7SJohn Fastabend 		if (!dev->netdev_ops->ndo_bridge_setlink)
28732469ffd7SJohn Fastabend 			err = -EOPNOTSUPP;
28742469ffd7SJohn Fastabend 		else
2875e5a55a89SJohn Fastabend 			err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh);
2876e5a55a89SJohn Fastabend 
28772469ffd7SJohn Fastabend 		if (!err)
28782469ffd7SJohn Fastabend 			flags &= ~BRIDGE_FLAGS_SELF;
28792469ffd7SJohn Fastabend 	}
28802469ffd7SJohn Fastabend 
2881c38e01b8SJohn Fastabend 	if (have_flags)
28822469ffd7SJohn Fastabend 		memcpy(nla_data(attr), &flags, sizeof(flags));
28832469ffd7SJohn Fastabend 	/* Generate event to notify upper layer of bridge change */
28842469ffd7SJohn Fastabend 	if (!err)
2885c38e01b8SJohn Fastabend 		err = rtnl_bridge_notify(dev, oflags);
2886e5a55a89SJohn Fastabend out:
2887e5a55a89SJohn Fastabend 	return err;
2888e5a55a89SJohn Fastabend }
2889e5a55a89SJohn Fastabend 
2890661d2967SThomas Graf static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
2891407af329SVlad Yasevich {
2892407af329SVlad Yasevich 	struct net *net = sock_net(skb->sk);
2893407af329SVlad Yasevich 	struct ifinfomsg *ifm;
2894407af329SVlad Yasevich 	struct net_device *dev;
2895407af329SVlad Yasevich 	struct nlattr *br_spec, *attr = NULL;
2896407af329SVlad Yasevich 	int rem, err = -EOPNOTSUPP;
2897407af329SVlad Yasevich 	u16 oflags, flags = 0;
2898407af329SVlad Yasevich 	bool have_flags = false;
2899407af329SVlad Yasevich 
2900407af329SVlad Yasevich 	if (nlmsg_len(nlh) < sizeof(*ifm))
2901407af329SVlad Yasevich 		return -EINVAL;
2902407af329SVlad Yasevich 
2903407af329SVlad Yasevich 	ifm = nlmsg_data(nlh);
2904407af329SVlad Yasevich 	if (ifm->ifi_family != AF_BRIDGE)
2905407af329SVlad Yasevich 		return -EPFNOSUPPORT;
2906407af329SVlad Yasevich 
2907407af329SVlad Yasevich 	dev = __dev_get_by_index(net, ifm->ifi_index);
2908407af329SVlad Yasevich 	if (!dev) {
2909407af329SVlad Yasevich 		pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
2910407af329SVlad Yasevich 		return -ENODEV;
2911407af329SVlad Yasevich 	}
2912407af329SVlad Yasevich 
2913407af329SVlad Yasevich 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
2914407af329SVlad Yasevich 	if (br_spec) {
2915407af329SVlad Yasevich 		nla_for_each_nested(attr, br_spec, rem) {
2916407af329SVlad Yasevich 			if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
29176e8d1c55SThomas Graf 				if (nla_len(attr) < sizeof(flags))
29186e8d1c55SThomas Graf 					return -EINVAL;
29196e8d1c55SThomas Graf 
2920407af329SVlad Yasevich 				have_flags = true;
2921407af329SVlad Yasevich 				flags = nla_get_u16(attr);
2922407af329SVlad Yasevich 				break;
2923407af329SVlad Yasevich 			}
2924407af329SVlad Yasevich 		}
2925407af329SVlad Yasevich 	}
2926407af329SVlad Yasevich 
2927407af329SVlad Yasevich 	oflags = flags;
2928407af329SVlad Yasevich 
2929407af329SVlad Yasevich 	if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
2930407af329SVlad Yasevich 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2931407af329SVlad Yasevich 
2932407af329SVlad Yasevich 		if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
2933407af329SVlad Yasevich 			err = -EOPNOTSUPP;
2934407af329SVlad Yasevich 			goto out;
2935407af329SVlad Yasevich 		}
2936407af329SVlad Yasevich 
2937407af329SVlad Yasevich 		err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh);
2938407af329SVlad Yasevich 		if (err)
2939407af329SVlad Yasevich 			goto out;
2940407af329SVlad Yasevich 
2941407af329SVlad Yasevich 		flags &= ~BRIDGE_FLAGS_MASTER;
2942407af329SVlad Yasevich 	}
2943407af329SVlad Yasevich 
2944407af329SVlad Yasevich 	if ((flags & BRIDGE_FLAGS_SELF)) {
2945407af329SVlad Yasevich 		if (!dev->netdev_ops->ndo_bridge_dellink)
2946407af329SVlad Yasevich 			err = -EOPNOTSUPP;
2947407af329SVlad Yasevich 		else
2948407af329SVlad Yasevich 			err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh);
2949407af329SVlad Yasevich 
2950407af329SVlad Yasevich 		if (!err)
2951407af329SVlad Yasevich 			flags &= ~BRIDGE_FLAGS_SELF;
2952407af329SVlad Yasevich 	}
2953407af329SVlad Yasevich 
2954407af329SVlad Yasevich 	if (have_flags)
2955407af329SVlad Yasevich 		memcpy(nla_data(attr), &flags, sizeof(flags));
2956407af329SVlad Yasevich 	/* Generate event to notify upper layer of bridge change */
2957407af329SVlad Yasevich 	if (!err)
2958407af329SVlad Yasevich 		err = rtnl_bridge_notify(dev, oflags);
2959407af329SVlad Yasevich out:
2960407af329SVlad Yasevich 	return err;
2961407af329SVlad Yasevich }
2962407af329SVlad Yasevich 
29631da177e4SLinus Torvalds /* Process one rtnetlink message. */
29641da177e4SLinus Torvalds 
29651d00a4ebSThomas Graf static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
29661da177e4SLinus Torvalds {
29673b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
2968e2849863SThomas Graf 	rtnl_doit_func doit;
29691da177e4SLinus Torvalds 	int sz_idx, kind;
29701da177e4SLinus Torvalds 	int family;
29711da177e4SLinus Torvalds 	int type;
29722907c35fSEric Dumazet 	int err;
29731da177e4SLinus Torvalds 
29741da177e4SLinus Torvalds 	type = nlh->nlmsg_type;
29751da177e4SLinus Torvalds 	if (type > RTM_MAX)
2976038890feSThomas Graf 		return -EOPNOTSUPP;
29771da177e4SLinus Torvalds 
29781da177e4SLinus Torvalds 	type -= RTM_BASE;
29791da177e4SLinus Torvalds 
29801da177e4SLinus Torvalds 	/* All the messages must have at least 1 byte length */
2981573ce260SHong zhi guo 	if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
29821da177e4SLinus Torvalds 		return 0;
29831da177e4SLinus Torvalds 
2984573ce260SHong zhi guo 	family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
29851da177e4SLinus Torvalds 	sz_idx = type>>2;
29861da177e4SLinus Torvalds 	kind = type&3;
29871da177e4SLinus Torvalds 
298890f62cf3SEric W. Biederman 	if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
29891d00a4ebSThomas Graf 		return -EPERM;
29901da177e4SLinus Torvalds 
2991b8f3ab42SDavid S. Miller 	if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
299297c53cacSDenis V. Lunev 		struct sock *rtnl;
2993e2849863SThomas Graf 		rtnl_dumpit_func dumpit;
2994c7ac8679SGreg Rose 		rtnl_calcit_func calcit;
2995c7ac8679SGreg Rose 		u16 min_dump_alloc = 0;
29961da177e4SLinus Torvalds 
2997e2849863SThomas Graf 		dumpit = rtnl_get_dumpit(family, type);
2998e2849863SThomas Graf 		if (dumpit == NULL)
2999038890feSThomas Graf 			return -EOPNOTSUPP;
3000c7ac8679SGreg Rose 		calcit = rtnl_get_calcit(family, type);
3001c7ac8679SGreg Rose 		if (calcit)
3002115c9b81SGreg Rose 			min_dump_alloc = calcit(skb, nlh);
30031da177e4SLinus Torvalds 
30042907c35fSEric Dumazet 		__rtnl_unlock();
300597c53cacSDenis V. Lunev 		rtnl = net->rtnl;
300680d326faSPablo Neira Ayuso 		{
300780d326faSPablo Neira Ayuso 			struct netlink_dump_control c = {
300880d326faSPablo Neira Ayuso 				.dump		= dumpit,
300980d326faSPablo Neira Ayuso 				.min_dump_alloc	= min_dump_alloc,
301080d326faSPablo Neira Ayuso 			};
301180d326faSPablo Neira Ayuso 			err = netlink_dump_start(rtnl, skb, nlh, &c);
301280d326faSPablo Neira Ayuso 		}
30132907c35fSEric Dumazet 		rtnl_lock();
30142907c35fSEric Dumazet 		return err;
30151da177e4SLinus Torvalds 	}
30161da177e4SLinus Torvalds 
3017e2849863SThomas Graf 	doit = rtnl_get_doit(family, type);
3018e2849863SThomas Graf 	if (doit == NULL)
3019038890feSThomas Graf 		return -EOPNOTSUPP;
30201da177e4SLinus Torvalds 
3021661d2967SThomas Graf 	return doit(skb, nlh);
30221da177e4SLinus Torvalds }
30231da177e4SLinus Torvalds 
3024cd40b7d3SDenis V. Lunev static void rtnetlink_rcv(struct sk_buff *skb)
30251da177e4SLinus Torvalds {
30261536cc0dSDenis V. Lunev 	rtnl_lock();
3027cd40b7d3SDenis V. Lunev 	netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
30281536cc0dSDenis V. Lunev 	rtnl_unlock();
30291da177e4SLinus Torvalds }
30301da177e4SLinus Torvalds 
30311da177e4SLinus Torvalds static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
30321da177e4SLinus Torvalds {
3033351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3034e9dc8653SEric W. Biederman 
30351da177e4SLinus Torvalds 	switch (event) {
30361da177e4SLinus Torvalds 	case NETDEV_UP:
30371da177e4SLinus Torvalds 	case NETDEV_DOWN:
303810de05afSPatrick McHardy 	case NETDEV_PRE_UP:
3039d90a909eSEric W. Biederman 	case NETDEV_POST_INIT:
3040d90a909eSEric W. Biederman 	case NETDEV_REGISTER:
30411da177e4SLinus Torvalds 	case NETDEV_CHANGE:
3042755d0e77SPatrick McHardy 	case NETDEV_PRE_TYPE_CHANGE:
30431da177e4SLinus Torvalds 	case NETDEV_GOING_DOWN:
3044a2835763SPatrick McHardy 	case NETDEV_UNREGISTER:
30450115e8e3SEric Dumazet 	case NETDEV_UNREGISTER_FINAL:
3046ac3d3f81SAmerigo Wang 	case NETDEV_RELEASE:
3047ac3d3f81SAmerigo Wang 	case NETDEV_JOIN:
30481da177e4SLinus Torvalds 		break;
30491da177e4SLinus Torvalds 	default:
30507f294054SAlexei Starovoitov 		rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
30511da177e4SLinus Torvalds 		break;
30521da177e4SLinus Torvalds 	}
30531da177e4SLinus Torvalds 	return NOTIFY_DONE;
30541da177e4SLinus Torvalds }
30551da177e4SLinus Torvalds 
30561da177e4SLinus Torvalds static struct notifier_block rtnetlink_dev_notifier = {
30571da177e4SLinus Torvalds 	.notifier_call	= rtnetlink_event,
30581da177e4SLinus Torvalds };
30591da177e4SLinus Torvalds 
306097c53cacSDenis V. Lunev 
30612c8c1e72SAlexey Dobriyan static int __net_init rtnetlink_net_init(struct net *net)
306297c53cacSDenis V. Lunev {
306397c53cacSDenis V. Lunev 	struct sock *sk;
3064a31f2d17SPablo Neira Ayuso 	struct netlink_kernel_cfg cfg = {
3065a31f2d17SPablo Neira Ayuso 		.groups		= RTNLGRP_MAX,
3066a31f2d17SPablo Neira Ayuso 		.input		= rtnetlink_rcv,
3067a31f2d17SPablo Neira Ayuso 		.cb_mutex	= &rtnl_mutex,
30689785e10aSPablo Neira Ayuso 		.flags		= NL_CFG_F_NONROOT_RECV,
3069a31f2d17SPablo Neira Ayuso 	};
3070a31f2d17SPablo Neira Ayuso 
30719f00d977SPablo Neira Ayuso 	sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
307297c53cacSDenis V. Lunev 	if (!sk)
307397c53cacSDenis V. Lunev 		return -ENOMEM;
307497c53cacSDenis V. Lunev 	net->rtnl = sk;
307597c53cacSDenis V. Lunev 	return 0;
307697c53cacSDenis V. Lunev }
307797c53cacSDenis V. Lunev 
30782c8c1e72SAlexey Dobriyan static void __net_exit rtnetlink_net_exit(struct net *net)
307997c53cacSDenis V. Lunev {
3080b7c6ba6eSDenis V. Lunev 	netlink_kernel_release(net->rtnl);
308197c53cacSDenis V. Lunev 	net->rtnl = NULL;
308297c53cacSDenis V. Lunev }
308397c53cacSDenis V. Lunev 
308497c53cacSDenis V. Lunev static struct pernet_operations rtnetlink_net_ops = {
308597c53cacSDenis V. Lunev 	.init = rtnetlink_net_init,
308697c53cacSDenis V. Lunev 	.exit = rtnetlink_net_exit,
308797c53cacSDenis V. Lunev };
308897c53cacSDenis V. Lunev 
30891da177e4SLinus Torvalds void __init rtnetlink_init(void)
30901da177e4SLinus Torvalds {
309197c53cacSDenis V. Lunev 	if (register_pernet_subsys(&rtnetlink_net_ops))
30921da177e4SLinus Torvalds 		panic("rtnetlink_init: cannot initialize rtnetlink\n");
309397c53cacSDenis V. Lunev 
30941da177e4SLinus Torvalds 	register_netdevice_notifier(&rtnetlink_dev_notifier);
3095340d17fcSThomas Graf 
3096c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
3097c7ac8679SGreg Rose 		      rtnl_dump_ifinfo, rtnl_calcit);
3098c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL);
3099c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL);
3100c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL);
3101687ad8ccSThomas Graf 
3102c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL);
3103c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL);
310477162022SJohn Fastabend 
310577162022SJohn Fastabend 	rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, NULL);
310677162022SJohn Fastabend 	rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, NULL);
310777162022SJohn Fastabend 	rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, NULL);
3108e5a55a89SJohn Fastabend 
3109e5a55a89SJohn Fastabend 	rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, NULL);
3110407af329SVlad Yasevich 	rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, NULL);
3111e5a55a89SJohn Fastabend 	rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, NULL);
31121da177e4SLinus Torvalds }
31131da177e4SLinus Torvalds 
3114