xref: /openbmc/linux/net/core/rtnetlink.c (revision 90c325e3bfe14ef360de6650fa2a2e92685e5cee)
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>
39ebc08a6fSWilliams, Mitch A #include <linux/pci.h>
4077162022SJohn Fastabend #include <linux/etherdevice.h>
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds #include <asm/uaccess.h>
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds #include <linux/inet.h>
451da177e4SLinus Torvalds #include <linux/netdevice.h>
461da177e4SLinus Torvalds #include <net/ip.h>
471da177e4SLinus Torvalds #include <net/protocol.h>
481da177e4SLinus Torvalds #include <net/arp.h>
491da177e4SLinus Torvalds #include <net/route.h>
501da177e4SLinus Torvalds #include <net/udp.h>
511da177e4SLinus Torvalds #include <net/sock.h>
521da177e4SLinus Torvalds #include <net/pkt_sched.h>
5314c0b97dSThomas Graf #include <net/fib_rules.h>
54e2849863SThomas Graf #include <net/rtnetlink.h>
5530ffee84SJohannes Berg #include <net/net_namespace.h>
561da177e4SLinus Torvalds 
57e0d087afSEric Dumazet struct rtnl_link {
58e2849863SThomas Graf 	rtnl_doit_func		doit;
59e2849863SThomas Graf 	rtnl_dumpit_func	dumpit;
60c7ac8679SGreg Rose 	rtnl_calcit_func 	calcit;
61e2849863SThomas Graf };
62e2849863SThomas Graf 
636756ae4bSStephen Hemminger static DEFINE_MUTEX(rtnl_mutex);
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds void rtnl_lock(void)
661da177e4SLinus Torvalds {
676756ae4bSStephen Hemminger 	mutex_lock(&rtnl_mutex);
681da177e4SLinus Torvalds }
69e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_lock);
701da177e4SLinus Torvalds 
716756ae4bSStephen Hemminger void __rtnl_unlock(void)
721da177e4SLinus Torvalds {
736756ae4bSStephen Hemminger 	mutex_unlock(&rtnl_mutex);
741da177e4SLinus Torvalds }
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds void rtnl_unlock(void)
771da177e4SLinus Torvalds {
7858ec3b4dSHerbert Xu 	/* This fellow will unlock it for us. */
791da177e4SLinus Torvalds 	netdev_run_todo();
801da177e4SLinus Torvalds }
81e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unlock);
821da177e4SLinus Torvalds 
836756ae4bSStephen Hemminger int rtnl_trylock(void)
846756ae4bSStephen Hemminger {
856756ae4bSStephen Hemminger 	return mutex_trylock(&rtnl_mutex);
866756ae4bSStephen Hemminger }
87e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_trylock);
886756ae4bSStephen Hemminger 
89c9c1014bSPatrick McHardy int rtnl_is_locked(void)
90c9c1014bSPatrick McHardy {
91c9c1014bSPatrick McHardy 	return mutex_is_locked(&rtnl_mutex);
92c9c1014bSPatrick McHardy }
93e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_is_locked);
94c9c1014bSPatrick McHardy 
95a898def2SPaul E. McKenney #ifdef CONFIG_PROVE_LOCKING
96a898def2SPaul E. McKenney int lockdep_rtnl_is_held(void)
97a898def2SPaul E. McKenney {
98a898def2SPaul E. McKenney 	return lockdep_is_held(&rtnl_mutex);
99a898def2SPaul E. McKenney }
100a898def2SPaul E. McKenney EXPORT_SYMBOL(lockdep_rtnl_is_held);
101a898def2SPaul E. McKenney #endif /* #ifdef CONFIG_PROVE_LOCKING */
102a898def2SPaul E. McKenney 
10325239ceeSPatrick McHardy static struct rtnl_link *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
104e2849863SThomas Graf 
105e2849863SThomas Graf static inline int rtm_msgindex(int msgtype)
106e2849863SThomas Graf {
107e2849863SThomas Graf 	int msgindex = msgtype - RTM_BASE;
108e2849863SThomas Graf 
109e2849863SThomas Graf 	/*
110e2849863SThomas Graf 	 * msgindex < 0 implies someone tried to register a netlink
111e2849863SThomas Graf 	 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
112e2849863SThomas Graf 	 * the message type has not been added to linux/rtnetlink.h
113e2849863SThomas Graf 	 */
114e2849863SThomas Graf 	BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
115e2849863SThomas Graf 
116e2849863SThomas Graf 	return msgindex;
117e2849863SThomas Graf }
118e2849863SThomas Graf 
119e2849863SThomas Graf static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex)
120e2849863SThomas Graf {
121e2849863SThomas Graf 	struct rtnl_link *tab;
122e2849863SThomas Graf 
12325239ceeSPatrick McHardy 	if (protocol <= RTNL_FAMILY_MAX)
124e2849863SThomas Graf 		tab = rtnl_msg_handlers[protocol];
1250f87b1ddSPatrick McHardy 	else
1260f87b1ddSPatrick McHardy 		tab = NULL;
1270f87b1ddSPatrick McHardy 
12851057f2fSThomas Graf 	if (tab == NULL || tab[msgindex].doit == NULL)
129e2849863SThomas Graf 		tab = rtnl_msg_handlers[PF_UNSPEC];
130e2849863SThomas Graf 
131c80bbeaeSHans Zhang 	return tab[msgindex].doit;
132e2849863SThomas Graf }
133e2849863SThomas Graf 
134e2849863SThomas Graf static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex)
135e2849863SThomas Graf {
136e2849863SThomas Graf 	struct rtnl_link *tab;
137e2849863SThomas Graf 
13825239ceeSPatrick McHardy 	if (protocol <= RTNL_FAMILY_MAX)
139e2849863SThomas Graf 		tab = rtnl_msg_handlers[protocol];
1400f87b1ddSPatrick McHardy 	else
1410f87b1ddSPatrick McHardy 		tab = NULL;
1420f87b1ddSPatrick McHardy 
14351057f2fSThomas Graf 	if (tab == NULL || tab[msgindex].dumpit == NULL)
144e2849863SThomas Graf 		tab = rtnl_msg_handlers[PF_UNSPEC];
145e2849863SThomas Graf 
146c80bbeaeSHans Zhang 	return tab[msgindex].dumpit;
147e2849863SThomas Graf }
148e2849863SThomas Graf 
149c7ac8679SGreg Rose static rtnl_calcit_func rtnl_get_calcit(int protocol, int msgindex)
150c7ac8679SGreg Rose {
151c7ac8679SGreg Rose 	struct rtnl_link *tab;
152c7ac8679SGreg Rose 
153c7ac8679SGreg Rose 	if (protocol <= RTNL_FAMILY_MAX)
154c7ac8679SGreg Rose 		tab = rtnl_msg_handlers[protocol];
155c7ac8679SGreg Rose 	else
156c7ac8679SGreg Rose 		tab = NULL;
157c7ac8679SGreg Rose 
158c7ac8679SGreg Rose 	if (tab == NULL || tab[msgindex].calcit == NULL)
159c7ac8679SGreg Rose 		tab = rtnl_msg_handlers[PF_UNSPEC];
160c7ac8679SGreg Rose 
161c80bbeaeSHans Zhang 	return tab[msgindex].calcit;
162c7ac8679SGreg Rose }
163c7ac8679SGreg Rose 
164e2849863SThomas Graf /**
165e2849863SThomas Graf  * __rtnl_register - Register a rtnetlink message type
166e2849863SThomas Graf  * @protocol: Protocol family or PF_UNSPEC
167e2849863SThomas Graf  * @msgtype: rtnetlink message type
168e2849863SThomas Graf  * @doit: Function pointer called for each request message
169e2849863SThomas Graf  * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
170c7ac8679SGreg Rose  * @calcit: Function pointer to calc size of dump message
171e2849863SThomas Graf  *
172e2849863SThomas Graf  * Registers the specified function pointers (at least one of them has
173e2849863SThomas Graf  * to be non-NULL) to be called whenever a request message for the
174e2849863SThomas Graf  * specified protocol family and message type is received.
175e2849863SThomas Graf  *
176e2849863SThomas Graf  * The special protocol family PF_UNSPEC may be used to define fallback
177e2849863SThomas Graf  * function pointers for the case when no entry for the specific protocol
178e2849863SThomas Graf  * family exists.
179e2849863SThomas Graf  *
180e2849863SThomas Graf  * Returns 0 on success or a negative error code.
181e2849863SThomas Graf  */
182e2849863SThomas Graf int __rtnl_register(int protocol, int msgtype,
183c7ac8679SGreg Rose 		    rtnl_doit_func doit, rtnl_dumpit_func dumpit,
184c7ac8679SGreg Rose 		    rtnl_calcit_func calcit)
185e2849863SThomas Graf {
186e2849863SThomas Graf 	struct rtnl_link *tab;
187e2849863SThomas Graf 	int msgindex;
188e2849863SThomas Graf 
18925239ceeSPatrick McHardy 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
190e2849863SThomas Graf 	msgindex = rtm_msgindex(msgtype);
191e2849863SThomas Graf 
192e2849863SThomas Graf 	tab = rtnl_msg_handlers[protocol];
193e2849863SThomas Graf 	if (tab == NULL) {
194e2849863SThomas Graf 		tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL);
195e2849863SThomas Graf 		if (tab == NULL)
196e2849863SThomas Graf 			return -ENOBUFS;
197e2849863SThomas Graf 
198e2849863SThomas Graf 		rtnl_msg_handlers[protocol] = tab;
199e2849863SThomas Graf 	}
200e2849863SThomas Graf 
201e2849863SThomas Graf 	if (doit)
202e2849863SThomas Graf 		tab[msgindex].doit = doit;
203e2849863SThomas Graf 
204e2849863SThomas Graf 	if (dumpit)
205e2849863SThomas Graf 		tab[msgindex].dumpit = dumpit;
206e2849863SThomas Graf 
207c7ac8679SGreg Rose 	if (calcit)
208c7ac8679SGreg Rose 		tab[msgindex].calcit = calcit;
209c7ac8679SGreg Rose 
210e2849863SThomas Graf 	return 0;
211e2849863SThomas Graf }
212e2849863SThomas Graf EXPORT_SYMBOL_GPL(__rtnl_register);
213e2849863SThomas Graf 
214e2849863SThomas Graf /**
215e2849863SThomas Graf  * rtnl_register - Register a rtnetlink message type
216e2849863SThomas Graf  *
217e2849863SThomas Graf  * Identical to __rtnl_register() but panics on failure. This is useful
218e2849863SThomas Graf  * as failure of this function is very unlikely, it can only happen due
219e2849863SThomas Graf  * to lack of memory when allocating the chain to store all message
220e2849863SThomas Graf  * handlers for a protocol. Meant for use in init functions where lack
22125985edcSLucas De Marchi  * of memory implies no sense in continuing.
222e2849863SThomas Graf  */
223e2849863SThomas Graf void rtnl_register(int protocol, int msgtype,
224c7ac8679SGreg Rose 		   rtnl_doit_func doit, rtnl_dumpit_func dumpit,
225c7ac8679SGreg Rose 		   rtnl_calcit_func calcit)
226e2849863SThomas Graf {
227c7ac8679SGreg Rose 	if (__rtnl_register(protocol, msgtype, doit, dumpit, calcit) < 0)
228e2849863SThomas Graf 		panic("Unable to register rtnetlink message handler, "
229e2849863SThomas Graf 		      "protocol = %d, message type = %d\n",
230e2849863SThomas Graf 		      protocol, msgtype);
231e2849863SThomas Graf }
232e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_register);
233e2849863SThomas Graf 
234e2849863SThomas Graf /**
235e2849863SThomas Graf  * rtnl_unregister - Unregister a rtnetlink message type
236e2849863SThomas Graf  * @protocol: Protocol family or PF_UNSPEC
237e2849863SThomas Graf  * @msgtype: rtnetlink message type
238e2849863SThomas Graf  *
239e2849863SThomas Graf  * Returns 0 on success or a negative error code.
240e2849863SThomas Graf  */
241e2849863SThomas Graf int rtnl_unregister(int protocol, int msgtype)
242e2849863SThomas Graf {
243e2849863SThomas Graf 	int msgindex;
244e2849863SThomas Graf 
24525239ceeSPatrick McHardy 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
246e2849863SThomas Graf 	msgindex = rtm_msgindex(msgtype);
247e2849863SThomas Graf 
248e2849863SThomas Graf 	if (rtnl_msg_handlers[protocol] == NULL)
249e2849863SThomas Graf 		return -ENOENT;
250e2849863SThomas Graf 
251e2849863SThomas Graf 	rtnl_msg_handlers[protocol][msgindex].doit = NULL;
252e2849863SThomas Graf 	rtnl_msg_handlers[protocol][msgindex].dumpit = NULL;
253e2849863SThomas Graf 
254e2849863SThomas Graf 	return 0;
255e2849863SThomas Graf }
256e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister);
257e2849863SThomas Graf 
258e2849863SThomas Graf /**
259e2849863SThomas Graf  * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
260e2849863SThomas Graf  * @protocol : Protocol family or PF_UNSPEC
261e2849863SThomas Graf  *
262e2849863SThomas Graf  * Identical to calling rtnl_unregster() for all registered message types
263e2849863SThomas Graf  * of a certain protocol family.
264e2849863SThomas Graf  */
265e2849863SThomas Graf void rtnl_unregister_all(int protocol)
266e2849863SThomas Graf {
26725239ceeSPatrick McHardy 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
268e2849863SThomas Graf 
269e2849863SThomas Graf 	kfree(rtnl_msg_handlers[protocol]);
270e2849863SThomas Graf 	rtnl_msg_handlers[protocol] = NULL;
271e2849863SThomas Graf }
272e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister_all);
2731da177e4SLinus Torvalds 
27438f7b870SPatrick McHardy static LIST_HEAD(link_ops);
27538f7b870SPatrick McHardy 
276c63044f0SEric Dumazet static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
277c63044f0SEric Dumazet {
278c63044f0SEric Dumazet 	const struct rtnl_link_ops *ops;
279c63044f0SEric Dumazet 
280c63044f0SEric Dumazet 	list_for_each_entry(ops, &link_ops, list) {
281c63044f0SEric Dumazet 		if (!strcmp(ops->kind, kind))
282c63044f0SEric Dumazet 			return ops;
283c63044f0SEric Dumazet 	}
284c63044f0SEric Dumazet 	return NULL;
285c63044f0SEric Dumazet }
286c63044f0SEric Dumazet 
28738f7b870SPatrick McHardy /**
28838f7b870SPatrick McHardy  * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
28938f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to register
29038f7b870SPatrick McHardy  *
29138f7b870SPatrick McHardy  * The caller must hold the rtnl_mutex. This function should be used
29238f7b870SPatrick McHardy  * by drivers that create devices during module initialization. It
29338f7b870SPatrick McHardy  * must be called before registering the devices.
29438f7b870SPatrick McHardy  *
29538f7b870SPatrick McHardy  * Returns 0 on success or a negative error code.
29638f7b870SPatrick McHardy  */
29738f7b870SPatrick McHardy int __rtnl_link_register(struct rtnl_link_ops *ops)
29838f7b870SPatrick McHardy {
299c63044f0SEric Dumazet 	if (rtnl_link_ops_get(ops->kind))
300c63044f0SEric Dumazet 		return -EEXIST;
301c63044f0SEric Dumazet 
302b0ab2fabSJiri Pirko 	/* The check for setup is here because if ops
303b0ab2fabSJiri Pirko 	 * does not have that filled up, it is not possible
304b0ab2fabSJiri Pirko 	 * to use the ops for creating device. So do not
305b0ab2fabSJiri Pirko 	 * fill up dellink as well. That disables rtnl_dellink.
306b0ab2fabSJiri Pirko 	 */
307b0ab2fabSJiri Pirko 	if (ops->setup && !ops->dellink)
30823289a37SEric Dumazet 		ops->dellink = unregister_netdevice_queue;
3092d85cba2SPatrick McHardy 
31038f7b870SPatrick McHardy 	list_add_tail(&ops->list, &link_ops);
31138f7b870SPatrick McHardy 	return 0;
31238f7b870SPatrick McHardy }
31338f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_register);
31438f7b870SPatrick McHardy 
31538f7b870SPatrick McHardy /**
31638f7b870SPatrick McHardy  * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
31738f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to register
31838f7b870SPatrick McHardy  *
31938f7b870SPatrick McHardy  * Returns 0 on success or a negative error code.
32038f7b870SPatrick McHardy  */
32138f7b870SPatrick McHardy int rtnl_link_register(struct rtnl_link_ops *ops)
32238f7b870SPatrick McHardy {
32338f7b870SPatrick McHardy 	int err;
32438f7b870SPatrick McHardy 
32538f7b870SPatrick McHardy 	rtnl_lock();
32638f7b870SPatrick McHardy 	err = __rtnl_link_register(ops);
32738f7b870SPatrick McHardy 	rtnl_unlock();
32838f7b870SPatrick McHardy 	return err;
32938f7b870SPatrick McHardy }
33038f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_register);
33138f7b870SPatrick McHardy 
332669f87baSPavel Emelyanov static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
333669f87baSPavel Emelyanov {
334669f87baSPavel Emelyanov 	struct net_device *dev;
33523289a37SEric Dumazet 	LIST_HEAD(list_kill);
33623289a37SEric Dumazet 
337669f87baSPavel Emelyanov 	for_each_netdev(net, dev) {
33823289a37SEric Dumazet 		if (dev->rtnl_link_ops == ops)
33923289a37SEric Dumazet 			ops->dellink(dev, &list_kill);
340669f87baSPavel Emelyanov 	}
34123289a37SEric Dumazet 	unregister_netdevice_many(&list_kill);
342669f87baSPavel Emelyanov }
343669f87baSPavel Emelyanov 
34438f7b870SPatrick McHardy /**
34538f7b870SPatrick McHardy  * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
34638f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to unregister
34738f7b870SPatrick McHardy  *
3482d85cba2SPatrick McHardy  * The caller must hold the rtnl_mutex.
34938f7b870SPatrick McHardy  */
35038f7b870SPatrick McHardy void __rtnl_link_unregister(struct rtnl_link_ops *ops)
35138f7b870SPatrick McHardy {
352881d966bSEric W. Biederman 	struct net *net;
3532d85cba2SPatrick McHardy 
354881d966bSEric W. Biederman 	for_each_net(net) {
355669f87baSPavel Emelyanov 		__rtnl_kill_links(net, ops);
356881d966bSEric W. Biederman 	}
35738f7b870SPatrick McHardy 	list_del(&ops->list);
35838f7b870SPatrick McHardy }
35938f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
36038f7b870SPatrick McHardy 
361200b916fSCong Wang /* Return with the rtnl_lock held when there are no network
362200b916fSCong Wang  * devices unregistering in any network namespace.
363200b916fSCong Wang  */
364200b916fSCong Wang static void rtnl_lock_unregistering_all(void)
365200b916fSCong Wang {
366200b916fSCong Wang 	struct net *net;
367200b916fSCong Wang 	bool unregistering;
368200b916fSCong Wang 	DEFINE_WAIT(wait);
369200b916fSCong Wang 
370200b916fSCong Wang 	for (;;) {
371200b916fSCong Wang 		prepare_to_wait(&netdev_unregistering_wq, &wait,
372200b916fSCong Wang 				TASK_UNINTERRUPTIBLE);
373200b916fSCong Wang 		unregistering = false;
374200b916fSCong Wang 		rtnl_lock();
375200b916fSCong Wang 		for_each_net(net) {
376200b916fSCong Wang 			if (net->dev_unreg_count > 0) {
377200b916fSCong Wang 				unregistering = true;
378200b916fSCong Wang 				break;
379200b916fSCong Wang 			}
380200b916fSCong Wang 		}
381200b916fSCong Wang 		if (!unregistering)
382200b916fSCong Wang 			break;
383200b916fSCong Wang 		__rtnl_unlock();
384200b916fSCong Wang 		schedule();
385200b916fSCong Wang 	}
386200b916fSCong Wang 	finish_wait(&netdev_unregistering_wq, &wait);
387200b916fSCong Wang }
388200b916fSCong Wang 
38938f7b870SPatrick McHardy /**
39038f7b870SPatrick McHardy  * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
39138f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to unregister
39238f7b870SPatrick McHardy  */
39338f7b870SPatrick McHardy void rtnl_link_unregister(struct rtnl_link_ops *ops)
39438f7b870SPatrick McHardy {
395200b916fSCong Wang 	/* Close the race with cleanup_net() */
396200b916fSCong Wang 	mutex_lock(&net_mutex);
397200b916fSCong Wang 	rtnl_lock_unregistering_all();
39838f7b870SPatrick McHardy 	__rtnl_link_unregister(ops);
39938f7b870SPatrick McHardy 	rtnl_unlock();
400200b916fSCong Wang 	mutex_unlock(&net_mutex);
40138f7b870SPatrick McHardy }
40238f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_unregister);
40338f7b870SPatrick McHardy 
404ba7d49b1SJiri Pirko static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
405ba7d49b1SJiri Pirko {
406ba7d49b1SJiri Pirko 	struct net_device *master_dev;
407ba7d49b1SJiri Pirko 	const struct rtnl_link_ops *ops;
408ba7d49b1SJiri Pirko 
409ba7d49b1SJiri Pirko 	master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
410ba7d49b1SJiri Pirko 	if (!master_dev)
411ba7d49b1SJiri Pirko 		return 0;
412ba7d49b1SJiri Pirko 	ops = master_dev->rtnl_link_ops;
4136049f253SFernando Luis Vazquez Cao 	if (!ops || !ops->get_slave_size)
414ba7d49b1SJiri Pirko 		return 0;
415ba7d49b1SJiri Pirko 	/* IFLA_INFO_SLAVE_DATA + nested data */
416ba7d49b1SJiri Pirko 	return nla_total_size(sizeof(struct nlattr)) +
417ba7d49b1SJiri Pirko 	       ops->get_slave_size(master_dev, dev);
418ba7d49b1SJiri Pirko }
419ba7d49b1SJiri Pirko 
42038f7b870SPatrick McHardy static size_t rtnl_link_get_size(const struct net_device *dev)
42138f7b870SPatrick McHardy {
42238f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
42338f7b870SPatrick McHardy 	size_t size;
42438f7b870SPatrick McHardy 
42538f7b870SPatrick McHardy 	if (!ops)
42638f7b870SPatrick McHardy 		return 0;
42738f7b870SPatrick McHardy 
428369cf77aSThomas Graf 	size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
429369cf77aSThomas Graf 	       nla_total_size(strlen(ops->kind) + 1);  /* IFLA_INFO_KIND */
43038f7b870SPatrick McHardy 
43138f7b870SPatrick McHardy 	if (ops->get_size)
43238f7b870SPatrick McHardy 		/* IFLA_INFO_DATA + nested data */
433369cf77aSThomas Graf 		size += nla_total_size(sizeof(struct nlattr)) +
43438f7b870SPatrick McHardy 			ops->get_size(dev);
43538f7b870SPatrick McHardy 
43638f7b870SPatrick McHardy 	if (ops->get_xstats_size)
437369cf77aSThomas Graf 		/* IFLA_INFO_XSTATS */
438369cf77aSThomas Graf 		size += nla_total_size(ops->get_xstats_size(dev));
43938f7b870SPatrick McHardy 
440ba7d49b1SJiri Pirko 	size += rtnl_link_get_slave_info_data_size(dev);
441ba7d49b1SJiri Pirko 
44238f7b870SPatrick McHardy 	return size;
44338f7b870SPatrick McHardy }
44438f7b870SPatrick McHardy 
445f8ff182cSThomas Graf static LIST_HEAD(rtnl_af_ops);
446f8ff182cSThomas Graf 
447f8ff182cSThomas Graf static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
448f8ff182cSThomas Graf {
449f8ff182cSThomas Graf 	const struct rtnl_af_ops *ops;
450f8ff182cSThomas Graf 
451f8ff182cSThomas Graf 	list_for_each_entry(ops, &rtnl_af_ops, list) {
452f8ff182cSThomas Graf 		if (ops->family == family)
453f8ff182cSThomas Graf 			return ops;
454f8ff182cSThomas Graf 	}
455f8ff182cSThomas Graf 
456f8ff182cSThomas Graf 	return NULL;
457f8ff182cSThomas Graf }
458f8ff182cSThomas Graf 
459f8ff182cSThomas Graf /**
460f8ff182cSThomas Graf  * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
461f8ff182cSThomas Graf  * @ops: struct rtnl_af_ops * to register
462f8ff182cSThomas Graf  *
463f8ff182cSThomas Graf  * Returns 0 on success or a negative error code.
464f8ff182cSThomas Graf  */
4653678a9d8Sstephen hemminger void rtnl_af_register(struct rtnl_af_ops *ops)
466f8ff182cSThomas Graf {
467f8ff182cSThomas Graf 	rtnl_lock();
4683678a9d8Sstephen hemminger 	list_add_tail(&ops->list, &rtnl_af_ops);
469f8ff182cSThomas Graf 	rtnl_unlock();
470f8ff182cSThomas Graf }
471f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_register);
472f8ff182cSThomas Graf 
473f8ff182cSThomas Graf /**
474f8ff182cSThomas Graf  * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
475f8ff182cSThomas Graf  * @ops: struct rtnl_af_ops * to unregister
476f8ff182cSThomas Graf  *
477f8ff182cSThomas Graf  * The caller must hold the rtnl_mutex.
478f8ff182cSThomas Graf  */
479f8ff182cSThomas Graf void __rtnl_af_unregister(struct rtnl_af_ops *ops)
480f8ff182cSThomas Graf {
481f8ff182cSThomas Graf 	list_del(&ops->list);
482f8ff182cSThomas Graf }
483f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(__rtnl_af_unregister);
484f8ff182cSThomas Graf 
485f8ff182cSThomas Graf /**
486f8ff182cSThomas Graf  * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
487f8ff182cSThomas Graf  * @ops: struct rtnl_af_ops * to unregister
488f8ff182cSThomas Graf  */
489f8ff182cSThomas Graf void rtnl_af_unregister(struct rtnl_af_ops *ops)
490f8ff182cSThomas Graf {
491f8ff182cSThomas Graf 	rtnl_lock();
492f8ff182cSThomas Graf 	__rtnl_af_unregister(ops);
493f8ff182cSThomas Graf 	rtnl_unlock();
494f8ff182cSThomas Graf }
495f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_unregister);
496f8ff182cSThomas Graf 
497f8ff182cSThomas Graf static size_t rtnl_link_get_af_size(const struct net_device *dev)
498f8ff182cSThomas Graf {
499f8ff182cSThomas Graf 	struct rtnl_af_ops *af_ops;
500f8ff182cSThomas Graf 	size_t size;
501f8ff182cSThomas Graf 
502f8ff182cSThomas Graf 	/* IFLA_AF_SPEC */
503f8ff182cSThomas Graf 	size = nla_total_size(sizeof(struct nlattr));
504f8ff182cSThomas Graf 
505f8ff182cSThomas Graf 	list_for_each_entry(af_ops, &rtnl_af_ops, list) {
506f8ff182cSThomas Graf 		if (af_ops->get_link_af_size) {
507f8ff182cSThomas Graf 			/* AF_* + nested data */
508f8ff182cSThomas Graf 			size += nla_total_size(sizeof(struct nlattr)) +
509f8ff182cSThomas Graf 				af_ops->get_link_af_size(dev);
510f8ff182cSThomas Graf 		}
511f8ff182cSThomas Graf 	}
512f8ff182cSThomas Graf 
513f8ff182cSThomas Graf 	return size;
514f8ff182cSThomas Graf }
515f8ff182cSThomas Graf 
516ba7d49b1SJiri Pirko static bool rtnl_have_link_slave_info(const struct net_device *dev)
517ba7d49b1SJiri Pirko {
518ba7d49b1SJiri Pirko 	struct net_device *master_dev;
519ba7d49b1SJiri Pirko 
520ba7d49b1SJiri Pirko 	master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
521813f020cSJiri Pirko 	if (master_dev && master_dev->rtnl_link_ops)
522ba7d49b1SJiri Pirko 		return true;
523ba7d49b1SJiri Pirko 	return false;
524ba7d49b1SJiri Pirko }
525ba7d49b1SJiri Pirko 
526ba7d49b1SJiri Pirko static int rtnl_link_slave_info_fill(struct sk_buff *skb,
527ba7d49b1SJiri Pirko 				     const struct net_device *dev)
528ba7d49b1SJiri Pirko {
529ba7d49b1SJiri Pirko 	struct net_device *master_dev;
530ba7d49b1SJiri Pirko 	const struct rtnl_link_ops *ops;
531ba7d49b1SJiri Pirko 	struct nlattr *slave_data;
532ba7d49b1SJiri Pirko 	int err;
533ba7d49b1SJiri Pirko 
534ba7d49b1SJiri Pirko 	master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
535ba7d49b1SJiri Pirko 	if (!master_dev)
536ba7d49b1SJiri Pirko 		return 0;
537ba7d49b1SJiri Pirko 	ops = master_dev->rtnl_link_ops;
538ba7d49b1SJiri Pirko 	if (!ops)
539ba7d49b1SJiri Pirko 		return 0;
540ba7d49b1SJiri Pirko 	if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
541ba7d49b1SJiri Pirko 		return -EMSGSIZE;
542ba7d49b1SJiri Pirko 	if (ops->fill_slave_info) {
543ba7d49b1SJiri Pirko 		slave_data = nla_nest_start(skb, IFLA_INFO_SLAVE_DATA);
544ba7d49b1SJiri Pirko 		if (!slave_data)
545ba7d49b1SJiri Pirko 			return -EMSGSIZE;
546ba7d49b1SJiri Pirko 		err = ops->fill_slave_info(skb, master_dev, dev);
547ba7d49b1SJiri Pirko 		if (err < 0)
548ba7d49b1SJiri Pirko 			goto err_cancel_slave_data;
549ba7d49b1SJiri Pirko 		nla_nest_end(skb, slave_data);
550ba7d49b1SJiri Pirko 	}
551ba7d49b1SJiri Pirko 	return 0;
552ba7d49b1SJiri Pirko 
553ba7d49b1SJiri Pirko err_cancel_slave_data:
554ba7d49b1SJiri Pirko 	nla_nest_cancel(skb, slave_data);
555ba7d49b1SJiri Pirko 	return err;
556ba7d49b1SJiri Pirko }
557ba7d49b1SJiri Pirko 
558ba7d49b1SJiri Pirko static int rtnl_link_info_fill(struct sk_buff *skb,
559ba7d49b1SJiri Pirko 			       const struct net_device *dev)
56038f7b870SPatrick McHardy {
56138f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
562ba7d49b1SJiri Pirko 	struct nlattr *data;
563ba7d49b1SJiri Pirko 	int err;
564ba7d49b1SJiri Pirko 
565ba7d49b1SJiri Pirko 	if (!ops)
566ba7d49b1SJiri Pirko 		return 0;
567ba7d49b1SJiri Pirko 	if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
568ba7d49b1SJiri Pirko 		return -EMSGSIZE;
569ba7d49b1SJiri Pirko 	if (ops->fill_xstats) {
570ba7d49b1SJiri Pirko 		err = ops->fill_xstats(skb, dev);
571ba7d49b1SJiri Pirko 		if (err < 0)
572ba7d49b1SJiri Pirko 			return err;
573ba7d49b1SJiri Pirko 	}
574ba7d49b1SJiri Pirko 	if (ops->fill_info) {
575ba7d49b1SJiri Pirko 		data = nla_nest_start(skb, IFLA_INFO_DATA);
576ba7d49b1SJiri Pirko 		if (data == NULL)
577ba7d49b1SJiri Pirko 			return -EMSGSIZE;
578ba7d49b1SJiri Pirko 		err = ops->fill_info(skb, dev);
579ba7d49b1SJiri Pirko 		if (err < 0)
580ba7d49b1SJiri Pirko 			goto err_cancel_data;
581ba7d49b1SJiri Pirko 		nla_nest_end(skb, data);
582ba7d49b1SJiri Pirko 	}
583ba7d49b1SJiri Pirko 	return 0;
584ba7d49b1SJiri Pirko 
585ba7d49b1SJiri Pirko err_cancel_data:
586ba7d49b1SJiri Pirko 	nla_nest_cancel(skb, data);
587ba7d49b1SJiri Pirko 	return err;
588ba7d49b1SJiri Pirko }
589ba7d49b1SJiri Pirko 
590ba7d49b1SJiri Pirko static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
591ba7d49b1SJiri Pirko {
592ba7d49b1SJiri Pirko 	struct nlattr *linkinfo;
59338f7b870SPatrick McHardy 	int err = -EMSGSIZE;
59438f7b870SPatrick McHardy 
59538f7b870SPatrick McHardy 	linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
59638f7b870SPatrick McHardy 	if (linkinfo == NULL)
59738f7b870SPatrick McHardy 		goto out;
59838f7b870SPatrick McHardy 
599ba7d49b1SJiri Pirko 	err = rtnl_link_info_fill(skb, dev);
60038f7b870SPatrick McHardy 	if (err < 0)
60138f7b870SPatrick McHardy 		goto err_cancel_link;
602ba7d49b1SJiri Pirko 
603ba7d49b1SJiri Pirko 	err = rtnl_link_slave_info_fill(skb, dev);
60438f7b870SPatrick McHardy 	if (err < 0)
605ba7d49b1SJiri Pirko 		goto err_cancel_link;
60638f7b870SPatrick McHardy 
60738f7b870SPatrick McHardy 	nla_nest_end(skb, linkinfo);
60838f7b870SPatrick McHardy 	return 0;
60938f7b870SPatrick McHardy 
61038f7b870SPatrick McHardy err_cancel_link:
61138f7b870SPatrick McHardy 	nla_nest_cancel(skb, linkinfo);
61238f7b870SPatrick McHardy out:
61338f7b870SPatrick McHardy 	return err;
61438f7b870SPatrick McHardy }
61538f7b870SPatrick McHardy 
61695c96174SEric Dumazet int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
6171da177e4SLinus Torvalds {
61897c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
6191da177e4SLinus Torvalds 	int err = 0;
6201da177e4SLinus Torvalds 
621ac6d439dSPatrick McHardy 	NETLINK_CB(skb).dst_group = group;
6221da177e4SLinus Torvalds 	if (echo)
6231da177e4SLinus Torvalds 		atomic_inc(&skb->users);
6241da177e4SLinus Torvalds 	netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
6251da177e4SLinus Torvalds 	if (echo)
6261da177e4SLinus Torvalds 		err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
6271da177e4SLinus Torvalds 	return err;
6281da177e4SLinus Torvalds }
6291da177e4SLinus Torvalds 
63097c53cacSDenis V. Lunev int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
6312942e900SThomas Graf {
63297c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
63397c53cacSDenis V. Lunev 
6342942e900SThomas Graf 	return nlmsg_unicast(rtnl, skb, pid);
6352942e900SThomas Graf }
636e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unicast);
6372942e900SThomas Graf 
6381ce85fe4SPablo Neira Ayuso void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
63997676b6bSThomas Graf 		 struct nlmsghdr *nlh, gfp_t flags)
64097676b6bSThomas Graf {
64197c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
64297676b6bSThomas Graf 	int report = 0;
64397676b6bSThomas Graf 
64497676b6bSThomas Graf 	if (nlh)
64597676b6bSThomas Graf 		report = nlmsg_report(nlh);
64697676b6bSThomas Graf 
6471ce85fe4SPablo Neira Ayuso 	nlmsg_notify(rtnl, skb, pid, group, report, flags);
64897676b6bSThomas Graf }
649e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_notify);
65097676b6bSThomas Graf 
65197c53cacSDenis V. Lunev void rtnl_set_sk_err(struct net *net, u32 group, int error)
65297676b6bSThomas Graf {
65397c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
65497c53cacSDenis V. Lunev 
65597676b6bSThomas Graf 	netlink_set_err(rtnl, 0, group, error);
65697676b6bSThomas Graf }
657e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_set_sk_err);
65897676b6bSThomas Graf 
6591da177e4SLinus Torvalds int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
6601da177e4SLinus Torvalds {
6612d7202bfSThomas Graf 	struct nlattr *mx;
6622d7202bfSThomas Graf 	int i, valid = 0;
6631da177e4SLinus Torvalds 
6642d7202bfSThomas Graf 	mx = nla_nest_start(skb, RTA_METRICS);
6652d7202bfSThomas Graf 	if (mx == NULL)
6662d7202bfSThomas Graf 		return -ENOBUFS;
6672d7202bfSThomas Graf 
6681da177e4SLinus Torvalds 	for (i = 0; i < RTAX_MAX; i++) {
6692d7202bfSThomas Graf 		if (metrics[i]) {
6702d7202bfSThomas Graf 			valid++;
671a6574349SDavid S. Miller 			if (nla_put_u32(skb, i+1, metrics[i]))
672a6574349SDavid S. Miller 				goto nla_put_failure;
6731da177e4SLinus Torvalds 		}
6742d7202bfSThomas Graf 	}
6751da177e4SLinus Torvalds 
676a57d27fcSDavid S. Miller 	if (!valid) {
677a57d27fcSDavid S. Miller 		nla_nest_cancel(skb, mx);
678a57d27fcSDavid S. Miller 		return 0;
679a57d27fcSDavid S. Miller 	}
6802d7202bfSThomas Graf 
6812d7202bfSThomas Graf 	return nla_nest_end(skb, mx);
6822d7202bfSThomas Graf 
6832d7202bfSThomas Graf nla_put_failure:
684bc3ed28cSThomas Graf 	nla_nest_cancel(skb, mx);
685bc3ed28cSThomas Graf 	return -EMSGSIZE;
6861da177e4SLinus Torvalds }
687e0d087afSEric Dumazet EXPORT_SYMBOL(rtnetlink_put_metrics);
6881da177e4SLinus Torvalds 
689e3703b3dSThomas Graf int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
69087a50699SDavid S. Miller 		       long expires, u32 error)
691e3703b3dSThomas Graf {
692e3703b3dSThomas Graf 	struct rta_cacheinfo ci = {
693a399a805SEric Dumazet 		.rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse),
694e3703b3dSThomas Graf 		.rta_used = dst->__use,
695e3703b3dSThomas Graf 		.rta_clntref = atomic_read(&(dst->__refcnt)),
696e3703b3dSThomas Graf 		.rta_error = error,
697e3703b3dSThomas Graf 		.rta_id =  id,
698e3703b3dSThomas Graf 	};
699e3703b3dSThomas Graf 
7008253947eSLi Wei 	if (expires) {
7018253947eSLi Wei 		unsigned long clock;
702e3703b3dSThomas Graf 
7038253947eSLi Wei 		clock = jiffies_to_clock_t(abs(expires));
7048253947eSLi Wei 		clock = min_t(unsigned long, clock, INT_MAX);
7058253947eSLi Wei 		ci.rta_expires = (expires > 0) ? clock : -clock;
7068253947eSLi Wei 	}
707e3703b3dSThomas Graf 	return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
708e3703b3dSThomas Graf }
709e3703b3dSThomas Graf EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
7101da177e4SLinus Torvalds 
71193b2d4a2SDavid S. Miller static void set_operstate(struct net_device *dev, unsigned char transition)
712b00055aaSStefan Rompf {
713b00055aaSStefan Rompf 	unsigned char operstate = dev->operstate;
714b00055aaSStefan Rompf 
715b00055aaSStefan Rompf 	switch (transition) {
716b00055aaSStefan Rompf 	case IF_OPER_UP:
717b00055aaSStefan Rompf 		if ((operstate == IF_OPER_DORMANT ||
718b00055aaSStefan Rompf 		     operstate == IF_OPER_UNKNOWN) &&
719b00055aaSStefan Rompf 		    !netif_dormant(dev))
720b00055aaSStefan Rompf 			operstate = IF_OPER_UP;
721b00055aaSStefan Rompf 		break;
722b00055aaSStefan Rompf 
723b00055aaSStefan Rompf 	case IF_OPER_DORMANT:
724b00055aaSStefan Rompf 		if (operstate == IF_OPER_UP ||
725b00055aaSStefan Rompf 		    operstate == IF_OPER_UNKNOWN)
726b00055aaSStefan Rompf 			operstate = IF_OPER_DORMANT;
727b00055aaSStefan Rompf 		break;
7283ff50b79SStephen Hemminger 	}
729b00055aaSStefan Rompf 
730b00055aaSStefan Rompf 	if (dev->operstate != operstate) {
731b00055aaSStefan Rompf 		write_lock_bh(&dev_base_lock);
732b00055aaSStefan Rompf 		dev->operstate = operstate;
733b00055aaSStefan Rompf 		write_unlock_bh(&dev_base_lock);
734b00055aaSStefan Rompf 		netdev_state_change(dev);
73593b2d4a2SDavid S. Miller 	}
736b00055aaSStefan Rompf }
737b00055aaSStefan Rompf 
738b1beb681SJiri Benc static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
739b1beb681SJiri Benc {
740b1beb681SJiri Benc 	return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
741b1beb681SJiri Benc 	       (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
742b1beb681SJiri Benc }
743b1beb681SJiri Benc 
7443729d502SPatrick McHardy static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
7453729d502SPatrick McHardy 					   const struct ifinfomsg *ifm)
7463729d502SPatrick McHardy {
7473729d502SPatrick McHardy 	unsigned int flags = ifm->ifi_flags;
7483729d502SPatrick McHardy 
7493729d502SPatrick McHardy 	/* bugwards compatibility: ifi_change == 0 is treated as ~0 */
7503729d502SPatrick McHardy 	if (ifm->ifi_change)
7513729d502SPatrick McHardy 		flags = (flags & ifm->ifi_change) |
752b1beb681SJiri Benc 			(rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
7533729d502SPatrick McHardy 
7543729d502SPatrick McHardy 	return flags;
7553729d502SPatrick McHardy }
7563729d502SPatrick McHardy 
757b60c5115SThomas Graf static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
758be1f3c2cSBen Hutchings 				 const struct rtnl_link_stats64 *b)
7591da177e4SLinus Torvalds {
760b60c5115SThomas Graf 	a->rx_packets = b->rx_packets;
761b60c5115SThomas Graf 	a->tx_packets = b->tx_packets;
762b60c5115SThomas Graf 	a->rx_bytes = b->rx_bytes;
763b60c5115SThomas Graf 	a->tx_bytes = b->tx_bytes;
764b60c5115SThomas Graf 	a->rx_errors = b->rx_errors;
765b60c5115SThomas Graf 	a->tx_errors = b->tx_errors;
766b60c5115SThomas Graf 	a->rx_dropped = b->rx_dropped;
767b60c5115SThomas Graf 	a->tx_dropped = b->tx_dropped;
768b60c5115SThomas Graf 
769b60c5115SThomas Graf 	a->multicast = b->multicast;
770b60c5115SThomas Graf 	a->collisions = b->collisions;
771b60c5115SThomas Graf 
772b60c5115SThomas Graf 	a->rx_length_errors = b->rx_length_errors;
773b60c5115SThomas Graf 	a->rx_over_errors = b->rx_over_errors;
774b60c5115SThomas Graf 	a->rx_crc_errors = b->rx_crc_errors;
775b60c5115SThomas Graf 	a->rx_frame_errors = b->rx_frame_errors;
776b60c5115SThomas Graf 	a->rx_fifo_errors = b->rx_fifo_errors;
777b60c5115SThomas Graf 	a->rx_missed_errors = b->rx_missed_errors;
778b60c5115SThomas Graf 
779b60c5115SThomas Graf 	a->tx_aborted_errors = b->tx_aborted_errors;
780b60c5115SThomas Graf 	a->tx_carrier_errors = b->tx_carrier_errors;
781b60c5115SThomas Graf 	a->tx_fifo_errors = b->tx_fifo_errors;
782b60c5115SThomas Graf 	a->tx_heartbeat_errors = b->tx_heartbeat_errors;
783b60c5115SThomas Graf 	a->tx_window_errors = b->tx_window_errors;
784b60c5115SThomas Graf 
785b60c5115SThomas Graf 	a->rx_compressed = b->rx_compressed;
786b60c5115SThomas Graf 	a->tx_compressed = b->tx_compressed;
78710708f37SJan Engelhardt }
78810708f37SJan Engelhardt 
789be1f3c2cSBen Hutchings static void copy_rtnl_link_stats64(void *v, const struct rtnl_link_stats64 *b)
79010708f37SJan Engelhardt {
791afdcba37SEric Dumazet 	memcpy(v, b, sizeof(*b));
79210708f37SJan Engelhardt }
793b60c5115SThomas Graf 
794c02db8c6SChris Wright /* All VF info */
795115c9b81SGreg Rose static inline int rtnl_vfinfo_size(const struct net_device *dev,
796115c9b81SGreg Rose 				   u32 ext_filter_mask)
797ebc08a6fSWilliams, Mitch A {
798115c9b81SGreg Rose 	if (dev->dev.parent && dev_is_pci(dev->dev.parent) &&
799115c9b81SGreg Rose 	    (ext_filter_mask & RTEXT_FILTER_VF)) {
800c02db8c6SChris Wright 		int num_vfs = dev_num_vf(dev->dev.parent);
801045de01aSScott Feldman 		size_t size = nla_total_size(sizeof(struct nlattr));
802045de01aSScott Feldman 		size += nla_total_size(num_vfs * sizeof(struct nlattr));
803045de01aSScott Feldman 		size += num_vfs *
804045de01aSScott Feldman 			(nla_total_size(sizeof(struct ifla_vf_mac)) +
805045de01aSScott Feldman 			 nla_total_size(sizeof(struct ifla_vf_vlan)) +
806ed616689SSucheta Chakraborty 			 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
807945a3676SJiri Benc 			 nla_total_size(sizeof(struct ifla_vf_rate)) +
808945a3676SJiri Benc 			 nla_total_size(sizeof(struct ifla_vf_link_state)));
809c02db8c6SChris Wright 		return size;
810c02db8c6SChris Wright 	} else
811ebc08a6fSWilliams, Mitch A 		return 0;
812ebc08a6fSWilliams, Mitch A }
813ebc08a6fSWilliams, Mitch A 
814c53864fdSDavid Gibson static size_t rtnl_port_size(const struct net_device *dev,
815c53864fdSDavid Gibson 			     u32 ext_filter_mask)
81657b61080SScott Feldman {
81757b61080SScott Feldman 	size_t port_size = nla_total_size(4)		/* PORT_VF */
81857b61080SScott Feldman 		+ nla_total_size(PORT_PROFILE_MAX)	/* PORT_PROFILE */
81957b61080SScott Feldman 		+ nla_total_size(sizeof(struct ifla_port_vsi))
82057b61080SScott Feldman 							/* PORT_VSI_TYPE */
82157b61080SScott Feldman 		+ nla_total_size(PORT_UUID_MAX)		/* PORT_INSTANCE_UUID */
82257b61080SScott Feldman 		+ nla_total_size(PORT_UUID_MAX)		/* PORT_HOST_UUID */
82357b61080SScott Feldman 		+ nla_total_size(1)			/* PROT_VDP_REQUEST */
82457b61080SScott Feldman 		+ nla_total_size(2);			/* PORT_VDP_RESPONSE */
82557b61080SScott Feldman 	size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
82657b61080SScott Feldman 	size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
82757b61080SScott Feldman 		+ port_size;
82857b61080SScott Feldman 	size_t port_self_size = nla_total_size(sizeof(struct nlattr))
82957b61080SScott Feldman 		+ port_size;
83057b61080SScott Feldman 
831c53864fdSDavid Gibson 	if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
832c53864fdSDavid Gibson 	    !(ext_filter_mask & RTEXT_FILTER_VF))
83357b61080SScott Feldman 		return 0;
83457b61080SScott Feldman 	if (dev_num_vf(dev->dev.parent))
83557b61080SScott Feldman 		return port_self_size + vf_ports_size +
83657b61080SScott Feldman 			vf_port_size * dev_num_vf(dev->dev.parent);
83757b61080SScott Feldman 	else
83857b61080SScott Feldman 		return port_self_size;
83957b61080SScott Feldman }
84057b61080SScott Feldman 
841115c9b81SGreg Rose static noinline size_t if_nlmsg_size(const struct net_device *dev,
842115c9b81SGreg Rose 				     u32 ext_filter_mask)
843339bf98fSThomas Graf {
844339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
845339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
8460b815a1aSStephen Hemminger 	       + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
847339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
848339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rtnl_link_ifmap))
849339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rtnl_link_stats))
850adcfe196SJan Engelhardt 	       + nla_total_size(sizeof(struct rtnl_link_stats64))
851339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
852339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
853339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_TXQLEN */
854339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_WEIGHT */
855339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MTU */
856339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_LINK */
857339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MASTER */
8589a57247fSJiri Pirko 	       + nla_total_size(1) /* IFLA_CARRIER */
859edbc0bb3SBen Greear 	       + nla_total_size(4) /* IFLA_PROMISCUITY */
86076ff5cc9SJiri Pirko 	       + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
86176ff5cc9SJiri Pirko 	       + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
862339bf98fSThomas Graf 	       + nla_total_size(1) /* IFLA_OPERSTATE */
86338f7b870SPatrick McHardy 	       + nla_total_size(1) /* IFLA_LINKMODE */
8642d3b479dSdavid decotigny 	       + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
865115c9b81SGreg Rose 	       + nla_total_size(ext_filter_mask
866115c9b81SGreg Rose 			        & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
867115c9b81SGreg Rose 	       + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
868c53864fdSDavid Gibson 	       + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
869f8ff182cSThomas Graf 	       + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
87066cae9edSJiri Pirko 	       + rtnl_link_get_af_size(dev) /* IFLA_AF_SPEC */
87166cae9edSJiri Pirko 	       + nla_total_size(MAX_PHYS_PORT_ID_LEN); /* IFLA_PHYS_PORT_ID */
872339bf98fSThomas Graf }
873339bf98fSThomas Graf 
87457b61080SScott Feldman static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
87557b61080SScott Feldman {
87657b61080SScott Feldman 	struct nlattr *vf_ports;
87757b61080SScott Feldman 	struct nlattr *vf_port;
87857b61080SScott Feldman 	int vf;
87957b61080SScott Feldman 	int err;
88057b61080SScott Feldman 
88157b61080SScott Feldman 	vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
88257b61080SScott Feldman 	if (!vf_ports)
88357b61080SScott Feldman 		return -EMSGSIZE;
88457b61080SScott Feldman 
88557b61080SScott Feldman 	for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
88657b61080SScott Feldman 		vf_port = nla_nest_start(skb, IFLA_VF_PORT);
8878ca94183SScott Feldman 		if (!vf_port)
8888ca94183SScott Feldman 			goto nla_put_failure;
889a6574349SDavid S. Miller 		if (nla_put_u32(skb, IFLA_PORT_VF, vf))
890a6574349SDavid S. Miller 			goto nla_put_failure;
89157b61080SScott Feldman 		err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
8928ca94183SScott Feldman 		if (err == -EMSGSIZE)
8938ca94183SScott Feldman 			goto nla_put_failure;
89457b61080SScott Feldman 		if (err) {
89557b61080SScott Feldman 			nla_nest_cancel(skb, vf_port);
89657b61080SScott Feldman 			continue;
89757b61080SScott Feldman 		}
89857b61080SScott Feldman 		nla_nest_end(skb, vf_port);
89957b61080SScott Feldman 	}
90057b61080SScott Feldman 
90157b61080SScott Feldman 	nla_nest_end(skb, vf_ports);
90257b61080SScott Feldman 
90357b61080SScott Feldman 	return 0;
9048ca94183SScott Feldman 
9058ca94183SScott Feldman nla_put_failure:
9068ca94183SScott Feldman 	nla_nest_cancel(skb, vf_ports);
9078ca94183SScott Feldman 	return -EMSGSIZE;
90857b61080SScott Feldman }
90957b61080SScott Feldman 
91057b61080SScott Feldman static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
91157b61080SScott Feldman {
91257b61080SScott Feldman 	struct nlattr *port_self;
91357b61080SScott Feldman 	int err;
91457b61080SScott Feldman 
91557b61080SScott Feldman 	port_self = nla_nest_start(skb, IFLA_PORT_SELF);
91657b61080SScott Feldman 	if (!port_self)
91757b61080SScott Feldman 		return -EMSGSIZE;
91857b61080SScott Feldman 
91957b61080SScott Feldman 	err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
92057b61080SScott Feldman 	if (err) {
92157b61080SScott Feldman 		nla_nest_cancel(skb, port_self);
9228ca94183SScott Feldman 		return (err == -EMSGSIZE) ? err : 0;
92357b61080SScott Feldman 	}
92457b61080SScott Feldman 
92557b61080SScott Feldman 	nla_nest_end(skb, port_self);
92657b61080SScott Feldman 
92757b61080SScott Feldman 	return 0;
92857b61080SScott Feldman }
92957b61080SScott Feldman 
930c53864fdSDavid Gibson static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
931c53864fdSDavid Gibson 			  u32 ext_filter_mask)
93257b61080SScott Feldman {
93357b61080SScott Feldman 	int err;
93457b61080SScott Feldman 
935c53864fdSDavid Gibson 	if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
936c53864fdSDavid Gibson 	    !(ext_filter_mask & RTEXT_FILTER_VF))
93757b61080SScott Feldman 		return 0;
93857b61080SScott Feldman 
93957b61080SScott Feldman 	err = rtnl_port_self_fill(skb, dev);
94057b61080SScott Feldman 	if (err)
94157b61080SScott Feldman 		return err;
94257b61080SScott Feldman 
94357b61080SScott Feldman 	if (dev_num_vf(dev->dev.parent)) {
94457b61080SScott Feldman 		err = rtnl_vf_ports_fill(skb, dev);
94557b61080SScott Feldman 		if (err)
94657b61080SScott Feldman 			return err;
94757b61080SScott Feldman 	}
94857b61080SScott Feldman 
94957b61080SScott Feldman 	return 0;
95057b61080SScott Feldman }
95157b61080SScott Feldman 
95266cae9edSJiri Pirko static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
95366cae9edSJiri Pirko {
95466cae9edSJiri Pirko 	int err;
95566cae9edSJiri Pirko 	struct netdev_phys_port_id ppid;
95666cae9edSJiri Pirko 
95766cae9edSJiri Pirko 	err = dev_get_phys_port_id(dev, &ppid);
95866cae9edSJiri Pirko 	if (err) {
95966cae9edSJiri Pirko 		if (err == -EOPNOTSUPP)
96066cae9edSJiri Pirko 			return 0;
96166cae9edSJiri Pirko 		return err;
96266cae9edSJiri Pirko 	}
96366cae9edSJiri Pirko 
96466cae9edSJiri Pirko 	if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
96566cae9edSJiri Pirko 		return -EMSGSIZE;
96666cae9edSJiri Pirko 
96766cae9edSJiri Pirko 	return 0;
96866cae9edSJiri Pirko }
96966cae9edSJiri Pirko 
970b60c5115SThomas Graf static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
971575c3e2aSPatrick McHardy 			    int type, u32 pid, u32 seq, u32 change,
972115c9b81SGreg Rose 			    unsigned int flags, u32 ext_filter_mask)
973b60c5115SThomas Graf {
974b60c5115SThomas Graf 	struct ifinfomsg *ifm;
9751da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
97628172739SEric Dumazet 	struct rtnl_link_stats64 temp;
977be1f3c2cSBen Hutchings 	const struct rtnl_link_stats64 *stats;
978f8ff182cSThomas Graf 	struct nlattr *attr, *af_spec;
979f8ff182cSThomas Graf 	struct rtnl_af_ops *af_ops;
980898e5061SJiri Pirko 	struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
9811da177e4SLinus Torvalds 
9822907c35fSEric Dumazet 	ASSERT_RTNL();
983b60c5115SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
984b60c5115SThomas Graf 	if (nlh == NULL)
98526932566SPatrick McHardy 		return -EMSGSIZE;
9861da177e4SLinus Torvalds 
987b60c5115SThomas Graf 	ifm = nlmsg_data(nlh);
988b60c5115SThomas Graf 	ifm->ifi_family = AF_UNSPEC;
989b60c5115SThomas Graf 	ifm->__ifi_pad = 0;
990b60c5115SThomas Graf 	ifm->ifi_type = dev->type;
991b60c5115SThomas Graf 	ifm->ifi_index = dev->ifindex;
992b60c5115SThomas Graf 	ifm->ifi_flags = dev_get_flags(dev);
993b60c5115SThomas Graf 	ifm->ifi_change = change;
9941da177e4SLinus Torvalds 
995a6574349SDavid S. Miller 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
996a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
997a6574349SDavid S. Miller 	    nla_put_u8(skb, IFLA_OPERSTATE,
998a6574349SDavid S. Miller 		       netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
999a6574349SDavid S. Miller 	    nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1000a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1001a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_GROUP, dev->group) ||
1002edbc0bb3SBen Greear 	    nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
100376ff5cc9SJiri Pirko 	    nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
10041d69c2b3SMark A. Greer #ifdef CONFIG_RPS
100576ff5cc9SJiri Pirko 	    nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
10061d69c2b3SMark A. Greer #endif
1007a6574349SDavid S. Miller 	    (dev->ifindex != dev->iflink &&
1008a6574349SDavid S. Miller 	     nla_put_u32(skb, IFLA_LINK, dev->iflink)) ||
1009898e5061SJiri Pirko 	    (upper_dev &&
1010898e5061SJiri Pirko 	     nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) ||
10119a57247fSJiri Pirko 	    nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
1012a6574349SDavid S. Miller 	    (dev->qdisc &&
1013a6574349SDavid S. Miller 	     nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
1014a6574349SDavid S. Miller 	    (dev->ifalias &&
10152d3b479dSdavid decotigny 	     nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)) ||
10162d3b479dSdavid decotigny 	    nla_put_u32(skb, IFLA_CARRIER_CHANGES,
10172d3b479dSdavid decotigny 			atomic_read(&dev->carrier_changes)))
1018a6574349SDavid S. Miller 		goto nla_put_failure;
10190b815a1aSStephen Hemminger 
1020b00055aaSStefan Rompf 	if (1) {
10211da177e4SLinus Torvalds 		struct rtnl_link_ifmap map = {
10221da177e4SLinus Torvalds 			.mem_start   = dev->mem_start,
10231da177e4SLinus Torvalds 			.mem_end     = dev->mem_end,
10241da177e4SLinus Torvalds 			.base_addr   = dev->base_addr,
10251da177e4SLinus Torvalds 			.irq         = dev->irq,
10261da177e4SLinus Torvalds 			.dma         = dev->dma,
10271da177e4SLinus Torvalds 			.port        = dev->if_port,
10281da177e4SLinus Torvalds 		};
1029a6574349SDavid S. Miller 		if (nla_put(skb, IFLA_MAP, sizeof(map), &map))
1030a6574349SDavid S. Miller 			goto nla_put_failure;
10311da177e4SLinus Torvalds 	}
10321da177e4SLinus Torvalds 
10331da177e4SLinus Torvalds 	if (dev->addr_len) {
1034a6574349SDavid S. Miller 		if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1035a6574349SDavid S. Miller 		    nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1036a6574349SDavid S. Miller 			goto nla_put_failure;
10371da177e4SLinus Torvalds 	}
10381da177e4SLinus Torvalds 
103966cae9edSJiri Pirko 	if (rtnl_phys_port_id_fill(skb, dev))
104066cae9edSJiri Pirko 		goto nla_put_failure;
104166cae9edSJiri Pirko 
1042b60c5115SThomas Graf 	attr = nla_reserve(skb, IFLA_STATS,
1043b60c5115SThomas Graf 			sizeof(struct rtnl_link_stats));
1044b60c5115SThomas Graf 	if (attr == NULL)
1045b60c5115SThomas Graf 		goto nla_put_failure;
1046b60c5115SThomas Graf 
104728172739SEric Dumazet 	stats = dev_get_stats(dev, &temp);
1048b60c5115SThomas Graf 	copy_rtnl_link_stats(nla_data(attr), stats);
10491da177e4SLinus Torvalds 
105010708f37SJan Engelhardt 	attr = nla_reserve(skb, IFLA_STATS64,
105110708f37SJan Engelhardt 			sizeof(struct rtnl_link_stats64));
105210708f37SJan Engelhardt 	if (attr == NULL)
105310708f37SJan Engelhardt 		goto nla_put_failure;
105410708f37SJan Engelhardt 	copy_rtnl_link_stats64(nla_data(attr), stats);
105510708f37SJan Engelhardt 
1056a6574349SDavid S. Miller 	if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) &&
1057a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent)))
1058a6574349SDavid S. Miller 		goto nla_put_failure;
105957b61080SScott Feldman 
1060115c9b81SGreg Rose 	if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent
1061115c9b81SGreg Rose 	    && (ext_filter_mask & RTEXT_FILTER_VF)) {
1062ebc08a6fSWilliams, Mitch A 		int i;
1063ebc08a6fSWilliams, Mitch A 
1064c02db8c6SChris Wright 		struct nlattr *vfinfo, *vf;
1065c02db8c6SChris Wright 		int num_vfs = dev_num_vf(dev->dev.parent);
1066c02db8c6SChris Wright 
1067c02db8c6SChris Wright 		vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1068c02db8c6SChris Wright 		if (!vfinfo)
1069c02db8c6SChris Wright 			goto nla_put_failure;
1070c02db8c6SChris Wright 		for (i = 0; i < num_vfs; i++) {
1071c02db8c6SChris Wright 			struct ifla_vf_info ivi;
1072c02db8c6SChris Wright 			struct ifla_vf_mac vf_mac;
1073c02db8c6SChris Wright 			struct ifla_vf_vlan vf_vlan;
1074ed616689SSucheta Chakraborty 			struct ifla_vf_rate vf_rate;
1075c02db8c6SChris Wright 			struct ifla_vf_tx_rate vf_tx_rate;
10765f8444a3SGreg Rose 			struct ifla_vf_spoofchk vf_spoofchk;
10771d8faf48SRony Efraim 			struct ifla_vf_link_state vf_linkstate;
10785f8444a3SGreg Rose 
10795f8444a3SGreg Rose 			/*
10805f8444a3SGreg Rose 			 * Not all SR-IOV capable drivers support the
10815f8444a3SGreg Rose 			 * spoofcheck query.  Preset to -1 so the user
10825f8444a3SGreg Rose 			 * space tool can detect that the driver didn't
10835f8444a3SGreg Rose 			 * report anything.
10845f8444a3SGreg Rose 			 */
10855f8444a3SGreg Rose 			ivi.spoofchk = -1;
108684d73cd3SMathias Krause 			memset(ivi.mac, 0, sizeof(ivi.mac));
10871d8faf48SRony Efraim 			/* The default value for VF link state is "auto"
10881d8faf48SRony Efraim 			 * IFLA_VF_LINK_STATE_AUTO which equals zero
10891d8faf48SRony Efraim 			 */
10901d8faf48SRony Efraim 			ivi.linkstate = 0;
1091ebc08a6fSWilliams, Mitch A 			if (dev->netdev_ops->ndo_get_vf_config(dev, i, &ivi))
1092ebc08a6fSWilliams, Mitch A 				break;
10935f8444a3SGreg Rose 			vf_mac.vf =
10945f8444a3SGreg Rose 				vf_vlan.vf =
1095ed616689SSucheta Chakraborty 				vf_rate.vf =
10965f8444a3SGreg Rose 				vf_tx_rate.vf =
10971d8faf48SRony Efraim 				vf_spoofchk.vf =
10981d8faf48SRony Efraim 				vf_linkstate.vf = ivi.vf;
10995f8444a3SGreg Rose 
1100c02db8c6SChris Wright 			memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1101c02db8c6SChris Wright 			vf_vlan.vlan = ivi.vlan;
1102c02db8c6SChris Wright 			vf_vlan.qos = ivi.qos;
1103ed616689SSucheta Chakraborty 			vf_tx_rate.rate = ivi.max_tx_rate;
1104ed616689SSucheta Chakraborty 			vf_rate.min_tx_rate = ivi.min_tx_rate;
1105ed616689SSucheta Chakraborty 			vf_rate.max_tx_rate = ivi.max_tx_rate;
11065f8444a3SGreg Rose 			vf_spoofchk.setting = ivi.spoofchk;
11071d8faf48SRony Efraim 			vf_linkstate.link_state = ivi.linkstate;
1108c02db8c6SChris Wright 			vf = nla_nest_start(skb, IFLA_VF_INFO);
1109c02db8c6SChris Wright 			if (!vf) {
1110c02db8c6SChris Wright 				nla_nest_cancel(skb, vfinfo);
1111c02db8c6SChris Wright 				goto nla_put_failure;
1112ebc08a6fSWilliams, Mitch A 			}
1113a6574349SDavid S. Miller 			if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1114a6574349SDavid S. Miller 			    nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1115ed616689SSucheta Chakraborty 			    nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1116ed616689SSucheta Chakraborty 				    &vf_rate) ||
1117a6574349SDavid S. Miller 			    nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1118a6574349SDavid S. Miller 				    &vf_tx_rate) ||
1119a6574349SDavid S. Miller 			    nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
11201d8faf48SRony Efraim 				    &vf_spoofchk) ||
11211d8faf48SRony Efraim 			    nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
11221d8faf48SRony Efraim 				    &vf_linkstate))
1123a6574349SDavid S. Miller 				goto nla_put_failure;
1124c02db8c6SChris Wright 			nla_nest_end(skb, vf);
1125c02db8c6SChris Wright 		}
1126c02db8c6SChris Wright 		nla_nest_end(skb, vfinfo);
1127ebc08a6fSWilliams, Mitch A 	}
112857b61080SScott Feldman 
1129c53864fdSDavid Gibson 	if (rtnl_port_fill(skb, dev, ext_filter_mask))
113057b61080SScott Feldman 		goto nla_put_failure;
113157b61080SScott Feldman 
1132ba7d49b1SJiri Pirko 	if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
113338f7b870SPatrick McHardy 		if (rtnl_link_fill(skb, dev) < 0)
113438f7b870SPatrick McHardy 			goto nla_put_failure;
113538f7b870SPatrick McHardy 	}
113638f7b870SPatrick McHardy 
1137f8ff182cSThomas Graf 	if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
1138f8ff182cSThomas Graf 		goto nla_put_failure;
1139f8ff182cSThomas Graf 
1140f8ff182cSThomas Graf 	list_for_each_entry(af_ops, &rtnl_af_ops, list) {
1141f8ff182cSThomas Graf 		if (af_ops->fill_link_af) {
1142f8ff182cSThomas Graf 			struct nlattr *af;
1143f8ff182cSThomas Graf 			int err;
1144f8ff182cSThomas Graf 
1145f8ff182cSThomas Graf 			if (!(af = nla_nest_start(skb, af_ops->family)))
1146f8ff182cSThomas Graf 				goto nla_put_failure;
1147f8ff182cSThomas Graf 
1148f8ff182cSThomas Graf 			err = af_ops->fill_link_af(skb, dev);
1149f8ff182cSThomas Graf 
1150f8ff182cSThomas Graf 			/*
1151f8ff182cSThomas Graf 			 * Caller may return ENODATA to indicate that there
1152f8ff182cSThomas Graf 			 * was no data to be dumped. This is not an error, it
1153f8ff182cSThomas Graf 			 * means we should trim the attribute header and
1154f8ff182cSThomas Graf 			 * continue.
1155f8ff182cSThomas Graf 			 */
1156f8ff182cSThomas Graf 			if (err == -ENODATA)
1157f8ff182cSThomas Graf 				nla_nest_cancel(skb, af);
1158f8ff182cSThomas Graf 			else if (err < 0)
1159f8ff182cSThomas Graf 				goto nla_put_failure;
1160f8ff182cSThomas Graf 
1161f8ff182cSThomas Graf 			nla_nest_end(skb, af);
1162f8ff182cSThomas Graf 		}
1163f8ff182cSThomas Graf 	}
1164f8ff182cSThomas Graf 
1165f8ff182cSThomas Graf 	nla_nest_end(skb, af_spec);
1166f8ff182cSThomas Graf 
1167b60c5115SThomas Graf 	return nlmsg_end(skb, nlh);
1168b60c5115SThomas Graf 
1169b60c5115SThomas Graf nla_put_failure:
117026932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
117126932566SPatrick McHardy 	return -EMSGSIZE;
11721da177e4SLinus Torvalds }
11731da177e4SLinus Torvalds 
1174f7b12606SJiri Pirko static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1175f7b12606SJiri Pirko 	[IFLA_IFNAME]		= { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1176f7b12606SJiri Pirko 	[IFLA_ADDRESS]		= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1177f7b12606SJiri Pirko 	[IFLA_BROADCAST]	= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1178f7b12606SJiri Pirko 	[IFLA_MAP]		= { .len = sizeof(struct rtnl_link_ifmap) },
1179f7b12606SJiri Pirko 	[IFLA_MTU]		= { .type = NLA_U32 },
1180f7b12606SJiri Pirko 	[IFLA_LINK]		= { .type = NLA_U32 },
1181f7b12606SJiri Pirko 	[IFLA_MASTER]		= { .type = NLA_U32 },
1182f7b12606SJiri Pirko 	[IFLA_CARRIER]		= { .type = NLA_U8 },
1183f7b12606SJiri Pirko 	[IFLA_TXQLEN]		= { .type = NLA_U32 },
1184f7b12606SJiri Pirko 	[IFLA_WEIGHT]		= { .type = NLA_U32 },
1185f7b12606SJiri Pirko 	[IFLA_OPERSTATE]	= { .type = NLA_U8 },
1186f7b12606SJiri Pirko 	[IFLA_LINKMODE]		= { .type = NLA_U8 },
1187f7b12606SJiri Pirko 	[IFLA_LINKINFO]		= { .type = NLA_NESTED },
1188f7b12606SJiri Pirko 	[IFLA_NET_NS_PID]	= { .type = NLA_U32 },
1189f7b12606SJiri Pirko 	[IFLA_NET_NS_FD]	= { .type = NLA_U32 },
1190f7b12606SJiri Pirko 	[IFLA_IFALIAS]	        = { .type = NLA_STRING, .len = IFALIASZ-1 },
1191f7b12606SJiri Pirko 	[IFLA_VFINFO_LIST]	= {. type = NLA_NESTED },
1192f7b12606SJiri Pirko 	[IFLA_VF_PORTS]		= { .type = NLA_NESTED },
1193f7b12606SJiri Pirko 	[IFLA_PORT_SELF]	= { .type = NLA_NESTED },
1194f7b12606SJiri Pirko 	[IFLA_AF_SPEC]		= { .type = NLA_NESTED },
1195f7b12606SJiri Pirko 	[IFLA_EXT_MASK]		= { .type = NLA_U32 },
1196f7b12606SJiri Pirko 	[IFLA_PROMISCUITY]	= { .type = NLA_U32 },
1197f7b12606SJiri Pirko 	[IFLA_NUM_TX_QUEUES]	= { .type = NLA_U32 },
1198f7b12606SJiri Pirko 	[IFLA_NUM_RX_QUEUES]	= { .type = NLA_U32 },
1199f7b12606SJiri Pirko 	[IFLA_PHYS_PORT_ID]	= { .type = NLA_BINARY, .len = MAX_PHYS_PORT_ID_LEN },
12002d3b479dSdavid decotigny 	[IFLA_CARRIER_CHANGES]	= { .type = NLA_U32 },  /* ignored */
1201f7b12606SJiri Pirko };
1202f7b12606SJiri Pirko 
1203f7b12606SJiri Pirko static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1204f7b12606SJiri Pirko 	[IFLA_INFO_KIND]	= { .type = NLA_STRING },
1205f7b12606SJiri Pirko 	[IFLA_INFO_DATA]	= { .type = NLA_NESTED },
1206f7b12606SJiri Pirko 	[IFLA_INFO_SLAVE_KIND]	= { .type = NLA_STRING },
1207f7b12606SJiri Pirko 	[IFLA_INFO_SLAVE_DATA]	= { .type = NLA_NESTED },
1208f7b12606SJiri Pirko };
1209f7b12606SJiri Pirko 
1210f7b12606SJiri Pirko static const struct nla_policy ifla_vfinfo_policy[IFLA_VF_INFO_MAX+1] = {
1211f7b12606SJiri Pirko 	[IFLA_VF_INFO]		= { .type = NLA_NESTED },
1212f7b12606SJiri Pirko };
1213f7b12606SJiri Pirko 
1214f7b12606SJiri Pirko static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1215f7b12606SJiri Pirko 	[IFLA_VF_MAC]		= { .type = NLA_BINARY,
1216f7b12606SJiri Pirko 				    .len = sizeof(struct ifla_vf_mac) },
1217f7b12606SJiri Pirko 	[IFLA_VF_VLAN]		= { .type = NLA_BINARY,
1218f7b12606SJiri Pirko 				    .len = sizeof(struct ifla_vf_vlan) },
1219f7b12606SJiri Pirko 	[IFLA_VF_TX_RATE]	= { .type = NLA_BINARY,
1220f7b12606SJiri Pirko 				    .len = sizeof(struct ifla_vf_tx_rate) },
1221f7b12606SJiri Pirko 	[IFLA_VF_SPOOFCHK]	= { .type = NLA_BINARY,
1222f7b12606SJiri Pirko 				    .len = sizeof(struct ifla_vf_spoofchk) },
1223ed616689SSucheta Chakraborty 	[IFLA_VF_RATE]		= { .type = NLA_BINARY,
1224ed616689SSucheta Chakraborty 				    .len = sizeof(struct ifla_vf_rate) },
1225c5b46160SDoug Ledford 	[IFLA_VF_LINK_STATE]	= { .type = NLA_BINARY,
1226c5b46160SDoug Ledford 				    .len = sizeof(struct ifla_vf_link_state) },
1227f7b12606SJiri Pirko };
1228f7b12606SJiri Pirko 
1229f7b12606SJiri Pirko static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1230f7b12606SJiri Pirko 	[IFLA_PORT_VF]		= { .type = NLA_U32 },
1231f7b12606SJiri Pirko 	[IFLA_PORT_PROFILE]	= { .type = NLA_STRING,
1232f7b12606SJiri Pirko 				    .len = PORT_PROFILE_MAX },
1233f7b12606SJiri Pirko 	[IFLA_PORT_VSI_TYPE]	= { .type = NLA_BINARY,
1234f7b12606SJiri Pirko 				    .len = sizeof(struct ifla_port_vsi)},
1235f7b12606SJiri Pirko 	[IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1236f7b12606SJiri Pirko 				      .len = PORT_UUID_MAX },
1237f7b12606SJiri Pirko 	[IFLA_PORT_HOST_UUID]	= { .type = NLA_STRING,
1238f7b12606SJiri Pirko 				    .len = PORT_UUID_MAX },
1239f7b12606SJiri Pirko 	[IFLA_PORT_REQUEST]	= { .type = NLA_U8, },
1240f7b12606SJiri Pirko 	[IFLA_PORT_RESPONSE]	= { .type = NLA_U16, },
1241f7b12606SJiri Pirko };
1242f7b12606SJiri Pirko 
1243b60c5115SThomas Graf static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
12441da177e4SLinus Torvalds {
12453b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
12467c28bd0bSEric Dumazet 	int h, s_h;
12477c28bd0bSEric Dumazet 	int idx = 0, s_idx;
12481da177e4SLinus Torvalds 	struct net_device *dev;
12497c28bd0bSEric Dumazet 	struct hlist_head *head;
1250115c9b81SGreg Rose 	struct nlattr *tb[IFLA_MAX+1];
1251115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
1252973462bbSDavid Gibson 	int err;
1253e5eca6d4SMichal Schmidt 	int hdrlen;
12541da177e4SLinus Torvalds 
12557c28bd0bSEric Dumazet 	s_h = cb->args[0];
12567c28bd0bSEric Dumazet 	s_idx = cb->args[1];
12577c28bd0bSEric Dumazet 
1258e67f88ddSEric Dumazet 	rcu_read_lock();
12594e985adaSThomas Graf 	cb->seq = net->dev_base_seq;
12604e985adaSThomas Graf 
1261e5eca6d4SMichal Schmidt 	/* A hack to preserve kernel<->userspace interface.
1262e5eca6d4SMichal Schmidt 	 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1263e5eca6d4SMichal Schmidt 	 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1264e5eca6d4SMichal Schmidt 	 * what iproute2 < v3.9.0 used.
1265e5eca6d4SMichal Schmidt 	 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1266e5eca6d4SMichal Schmidt 	 * attribute, its netlink message is shorter than struct ifinfomsg.
1267e5eca6d4SMichal Schmidt 	 */
1268e5eca6d4SMichal Schmidt 	hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ?
1269e5eca6d4SMichal Schmidt 		 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1270e5eca6d4SMichal Schmidt 
1271e5eca6d4SMichal Schmidt 	if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
1272115c9b81SGreg Rose 
1273115c9b81SGreg Rose 		if (tb[IFLA_EXT_MASK])
1274115c9b81SGreg Rose 			ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1275a4b64fbeSEric Dumazet 	}
1276115c9b81SGreg Rose 
12777c28bd0bSEric Dumazet 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
12787562f876SPavel Emelianov 		idx = 0;
12797c28bd0bSEric Dumazet 		head = &net->dev_index_head[h];
1280b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu(dev, head, index_hlist) {
12811da177e4SLinus Torvalds 			if (idx < s_idx)
12827562f876SPavel Emelianov 				goto cont;
1283973462bbSDavid Gibson 			err = rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
128415e47304SEric W. Biederman 					       NETLINK_CB(cb->skb).portid,
12857c28bd0bSEric Dumazet 					       cb->nlh->nlmsg_seq, 0,
1286115c9b81SGreg Rose 					       NLM_F_MULTI,
1287973462bbSDavid Gibson 					       ext_filter_mask);
1288973462bbSDavid Gibson 			/* If we ran out of room on the first message,
1289973462bbSDavid Gibson 			 * we're in trouble
1290973462bbSDavid Gibson 			 */
1291973462bbSDavid Gibson 			WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
1292973462bbSDavid Gibson 
1293973462bbSDavid Gibson 			if (err <= 0)
12947c28bd0bSEric Dumazet 				goto out;
12954e985adaSThomas Graf 
12964e985adaSThomas Graf 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
12977562f876SPavel Emelianov cont:
12987562f876SPavel Emelianov 			idx++;
12991da177e4SLinus Torvalds 		}
13007c28bd0bSEric Dumazet 	}
13017c28bd0bSEric Dumazet out:
1302e67f88ddSEric Dumazet 	rcu_read_unlock();
13037c28bd0bSEric Dumazet 	cb->args[1] = idx;
13047c28bd0bSEric Dumazet 	cb->args[0] = h;
13051da177e4SLinus Torvalds 
13061da177e4SLinus Torvalds 	return skb->len;
13071da177e4SLinus Torvalds }
13081da177e4SLinus Torvalds 
1309f7b12606SJiri Pirko int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len)
1310f7b12606SJiri Pirko {
1311f7b12606SJiri Pirko 	return nla_parse(tb, IFLA_MAX, head, len, ifla_policy);
1312f7b12606SJiri Pirko }
1313f7b12606SJiri Pirko EXPORT_SYMBOL(rtnl_nla_parse_ifla);
131457b61080SScott Feldman 
131581adee47SEric W. Biederman struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
131681adee47SEric W. Biederman {
131781adee47SEric W. Biederman 	struct net *net;
131881adee47SEric W. Biederman 	/* Examine the link attributes and figure out which
131981adee47SEric W. Biederman 	 * network namespace we are talking about.
132081adee47SEric W. Biederman 	 */
132181adee47SEric W. Biederman 	if (tb[IFLA_NET_NS_PID])
132281adee47SEric W. Biederman 		net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
1323f0630529SEric W. Biederman 	else if (tb[IFLA_NET_NS_FD])
1324f0630529SEric W. Biederman 		net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
132581adee47SEric W. Biederman 	else
132681adee47SEric W. Biederman 		net = get_net(src_net);
132781adee47SEric W. Biederman 	return net;
132881adee47SEric W. Biederman }
132981adee47SEric W. Biederman EXPORT_SYMBOL(rtnl_link_get_net);
133081adee47SEric W. Biederman 
13311840bb13SThomas Graf static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
13321840bb13SThomas Graf {
13331840bb13SThomas Graf 	if (dev) {
13341840bb13SThomas Graf 		if (tb[IFLA_ADDRESS] &&
13351840bb13SThomas Graf 		    nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
13361840bb13SThomas Graf 			return -EINVAL;
13371840bb13SThomas Graf 
13381840bb13SThomas Graf 		if (tb[IFLA_BROADCAST] &&
13391840bb13SThomas Graf 		    nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
13401840bb13SThomas Graf 			return -EINVAL;
13411840bb13SThomas Graf 	}
13421840bb13SThomas Graf 
1343cf7afbfeSThomas Graf 	if (tb[IFLA_AF_SPEC]) {
1344cf7afbfeSThomas Graf 		struct nlattr *af;
1345cf7afbfeSThomas Graf 		int rem, err;
1346cf7afbfeSThomas Graf 
1347cf7afbfeSThomas Graf 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1348cf7afbfeSThomas Graf 			const struct rtnl_af_ops *af_ops;
1349cf7afbfeSThomas Graf 
1350cf7afbfeSThomas Graf 			if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1351cf7afbfeSThomas Graf 				return -EAFNOSUPPORT;
1352cf7afbfeSThomas Graf 
1353cf7afbfeSThomas Graf 			if (!af_ops->set_link_af)
1354cf7afbfeSThomas Graf 				return -EOPNOTSUPP;
1355cf7afbfeSThomas Graf 
1356cf7afbfeSThomas Graf 			if (af_ops->validate_link_af) {
13576d3a9a68SKurt Van Dijck 				err = af_ops->validate_link_af(dev, af);
1358cf7afbfeSThomas Graf 				if (err < 0)
1359cf7afbfeSThomas Graf 					return err;
1360cf7afbfeSThomas Graf 			}
1361cf7afbfeSThomas Graf 		}
1362cf7afbfeSThomas Graf 	}
1363cf7afbfeSThomas Graf 
13641840bb13SThomas Graf 	return 0;
13651840bb13SThomas Graf }
13661840bb13SThomas Graf 
1367c02db8c6SChris Wright static int do_setvfinfo(struct net_device *dev, struct nlattr *attr)
1368c02db8c6SChris Wright {
1369c02db8c6SChris Wright 	int rem, err = -EINVAL;
1370c02db8c6SChris Wright 	struct nlattr *vf;
1371c02db8c6SChris Wright 	const struct net_device_ops *ops = dev->netdev_ops;
1372c02db8c6SChris Wright 
1373c02db8c6SChris Wright 	nla_for_each_nested(vf, attr, rem) {
1374c02db8c6SChris Wright 		switch (nla_type(vf)) {
1375c02db8c6SChris Wright 		case IFLA_VF_MAC: {
1376c02db8c6SChris Wright 			struct ifla_vf_mac *ivm;
1377c02db8c6SChris Wright 			ivm = nla_data(vf);
1378c02db8c6SChris Wright 			err = -EOPNOTSUPP;
1379c02db8c6SChris Wright 			if (ops->ndo_set_vf_mac)
1380c02db8c6SChris Wright 				err = ops->ndo_set_vf_mac(dev, ivm->vf,
1381c02db8c6SChris Wright 							  ivm->mac);
1382c02db8c6SChris Wright 			break;
1383c02db8c6SChris Wright 		}
1384c02db8c6SChris Wright 		case IFLA_VF_VLAN: {
1385c02db8c6SChris Wright 			struct ifla_vf_vlan *ivv;
1386c02db8c6SChris Wright 			ivv = nla_data(vf);
1387c02db8c6SChris Wright 			err = -EOPNOTSUPP;
1388c02db8c6SChris Wright 			if (ops->ndo_set_vf_vlan)
1389c02db8c6SChris Wright 				err = ops->ndo_set_vf_vlan(dev, ivv->vf,
1390c02db8c6SChris Wright 							   ivv->vlan,
1391c02db8c6SChris Wright 							   ivv->qos);
1392c02db8c6SChris Wright 			break;
1393c02db8c6SChris Wright 		}
1394c02db8c6SChris Wright 		case IFLA_VF_TX_RATE: {
1395c02db8c6SChris Wright 			struct ifla_vf_tx_rate *ivt;
1396ed616689SSucheta Chakraborty 			struct ifla_vf_info ivf;
1397c02db8c6SChris Wright 			ivt = nla_data(vf);
1398c02db8c6SChris Wright 			err = -EOPNOTSUPP;
1399ed616689SSucheta Chakraborty 			if (ops->ndo_get_vf_config)
1400ed616689SSucheta Chakraborty 				err = ops->ndo_get_vf_config(dev, ivt->vf,
1401ed616689SSucheta Chakraborty 							     &ivf);
1402ed616689SSucheta Chakraborty 			if (err)
1403ed616689SSucheta Chakraborty 				break;
1404ed616689SSucheta Chakraborty 			err = -EOPNOTSUPP;
1405ed616689SSucheta Chakraborty 			if (ops->ndo_set_vf_rate)
1406ed616689SSucheta Chakraborty 				err = ops->ndo_set_vf_rate(dev, ivt->vf,
1407ed616689SSucheta Chakraborty 							   ivf.min_tx_rate,
1408c02db8c6SChris Wright 							   ivt->rate);
1409c02db8c6SChris Wright 			break;
1410c02db8c6SChris Wright 		}
1411ed616689SSucheta Chakraborty 		case IFLA_VF_RATE: {
1412ed616689SSucheta Chakraborty 			struct ifla_vf_rate *ivt;
1413ed616689SSucheta Chakraborty 			ivt = nla_data(vf);
1414ed616689SSucheta Chakraborty 			err = -EOPNOTSUPP;
1415ed616689SSucheta Chakraborty 			if (ops->ndo_set_vf_rate)
1416ed616689SSucheta Chakraborty 				err = ops->ndo_set_vf_rate(dev, ivt->vf,
1417ed616689SSucheta Chakraborty 							   ivt->min_tx_rate,
1418ed616689SSucheta Chakraborty 							   ivt->max_tx_rate);
1419ed616689SSucheta Chakraborty 			break;
1420ed616689SSucheta Chakraborty 		}
14215f8444a3SGreg Rose 		case IFLA_VF_SPOOFCHK: {
14225f8444a3SGreg Rose 			struct ifla_vf_spoofchk *ivs;
14235f8444a3SGreg Rose 			ivs = nla_data(vf);
14245f8444a3SGreg Rose 			err = -EOPNOTSUPP;
14255f8444a3SGreg Rose 			if (ops->ndo_set_vf_spoofchk)
14265f8444a3SGreg Rose 				err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
14275f8444a3SGreg Rose 							       ivs->setting);
14285f8444a3SGreg Rose 			break;
14295f8444a3SGreg Rose 		}
14301d8faf48SRony Efraim 		case IFLA_VF_LINK_STATE: {
14311d8faf48SRony Efraim 			struct ifla_vf_link_state *ivl;
14321d8faf48SRony Efraim 			ivl = nla_data(vf);
14331d8faf48SRony Efraim 			err = -EOPNOTSUPP;
14341d8faf48SRony Efraim 			if (ops->ndo_set_vf_link_state)
14351d8faf48SRony Efraim 				err = ops->ndo_set_vf_link_state(dev, ivl->vf,
14361d8faf48SRony Efraim 								 ivl->link_state);
14371d8faf48SRony Efraim 			break;
14381d8faf48SRony Efraim 		}
1439c02db8c6SChris Wright 		default:
1440c02db8c6SChris Wright 			err = -EINVAL;
1441c02db8c6SChris Wright 			break;
1442c02db8c6SChris Wright 		}
1443c02db8c6SChris Wright 		if (err)
1444c02db8c6SChris Wright 			break;
1445c02db8c6SChris Wright 	}
1446c02db8c6SChris Wright 	return err;
1447c02db8c6SChris Wright }
1448c02db8c6SChris Wright 
1449fbaec0eaSJiri Pirko static int do_set_master(struct net_device *dev, int ifindex)
1450fbaec0eaSJiri Pirko {
1451898e5061SJiri Pirko 	struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
1452fbaec0eaSJiri Pirko 	const struct net_device_ops *ops;
1453fbaec0eaSJiri Pirko 	int err;
1454fbaec0eaSJiri Pirko 
1455898e5061SJiri Pirko 	if (upper_dev) {
1456898e5061SJiri Pirko 		if (upper_dev->ifindex == ifindex)
1457fbaec0eaSJiri Pirko 			return 0;
1458898e5061SJiri Pirko 		ops = upper_dev->netdev_ops;
1459fbaec0eaSJiri Pirko 		if (ops->ndo_del_slave) {
1460898e5061SJiri Pirko 			err = ops->ndo_del_slave(upper_dev, dev);
1461fbaec0eaSJiri Pirko 			if (err)
1462fbaec0eaSJiri Pirko 				return err;
1463fbaec0eaSJiri Pirko 		} else {
1464fbaec0eaSJiri Pirko 			return -EOPNOTSUPP;
1465fbaec0eaSJiri Pirko 		}
1466fbaec0eaSJiri Pirko 	}
1467fbaec0eaSJiri Pirko 
1468fbaec0eaSJiri Pirko 	if (ifindex) {
1469898e5061SJiri Pirko 		upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
1470898e5061SJiri Pirko 		if (!upper_dev)
1471fbaec0eaSJiri Pirko 			return -EINVAL;
1472898e5061SJiri Pirko 		ops = upper_dev->netdev_ops;
1473fbaec0eaSJiri Pirko 		if (ops->ndo_add_slave) {
1474898e5061SJiri Pirko 			err = ops->ndo_add_slave(upper_dev, dev);
1475fbaec0eaSJiri Pirko 			if (err)
1476fbaec0eaSJiri Pirko 				return err;
1477fbaec0eaSJiri Pirko 		} else {
1478fbaec0eaSJiri Pirko 			return -EOPNOTSUPP;
1479fbaec0eaSJiri Pirko 		}
1480fbaec0eaSJiri Pirko 	}
1481fbaec0eaSJiri Pirko 	return 0;
1482fbaec0eaSJiri Pirko }
1483fbaec0eaSJiri Pirko 
1484*90c325e3SNicolas Dichtel #define DO_SETLINK_MODIFIED	0x01
148590f62cf3SEric W. Biederman static int do_setlink(const struct sk_buff *skb,
148690f62cf3SEric W. Biederman 		      struct net_device *dev, struct ifinfomsg *ifm,
1487*90c325e3SNicolas Dichtel 		      struct nlattr **tb, char *ifname, int status)
14880157f60cSPatrick McHardy {
1489d314774cSStephen Hemminger 	const struct net_device_ops *ops = dev->netdev_ops;
14900157f60cSPatrick McHardy 	int err;
14910157f60cSPatrick McHardy 
1492f0630529SEric W. Biederman 	if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
149381adee47SEric W. Biederman 		struct net *net = rtnl_link_get_net(dev_net(dev), tb);
1494d8a5ec67SEric W. Biederman 		if (IS_ERR(net)) {
1495d8a5ec67SEric W. Biederman 			err = PTR_ERR(net);
1496d8a5ec67SEric W. Biederman 			goto errout;
1497d8a5ec67SEric W. Biederman 		}
149890f62cf3SEric W. Biederman 		if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
1499b51642f6SEric W. Biederman 			err = -EPERM;
1500b51642f6SEric W. Biederman 			goto errout;
1501b51642f6SEric W. Biederman 		}
1502d8a5ec67SEric W. Biederman 		err = dev_change_net_namespace(dev, net, ifname);
1503d8a5ec67SEric W. Biederman 		put_net(net);
1504d8a5ec67SEric W. Biederman 		if (err)
1505d8a5ec67SEric W. Biederman 			goto errout;
1506*90c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
1507d8a5ec67SEric W. Biederman 	}
1508d8a5ec67SEric W. Biederman 
15090157f60cSPatrick McHardy 	if (tb[IFLA_MAP]) {
15100157f60cSPatrick McHardy 		struct rtnl_link_ifmap *u_map;
15110157f60cSPatrick McHardy 		struct ifmap k_map;
15120157f60cSPatrick McHardy 
1513d314774cSStephen Hemminger 		if (!ops->ndo_set_config) {
15140157f60cSPatrick McHardy 			err = -EOPNOTSUPP;
15150157f60cSPatrick McHardy 			goto errout;
15160157f60cSPatrick McHardy 		}
15170157f60cSPatrick McHardy 
15180157f60cSPatrick McHardy 		if (!netif_device_present(dev)) {
15190157f60cSPatrick McHardy 			err = -ENODEV;
15200157f60cSPatrick McHardy 			goto errout;
15210157f60cSPatrick McHardy 		}
15220157f60cSPatrick McHardy 
15230157f60cSPatrick McHardy 		u_map = nla_data(tb[IFLA_MAP]);
15240157f60cSPatrick McHardy 		k_map.mem_start = (unsigned long) u_map->mem_start;
15250157f60cSPatrick McHardy 		k_map.mem_end = (unsigned long) u_map->mem_end;
15260157f60cSPatrick McHardy 		k_map.base_addr = (unsigned short) u_map->base_addr;
15270157f60cSPatrick McHardy 		k_map.irq = (unsigned char) u_map->irq;
15280157f60cSPatrick McHardy 		k_map.dma = (unsigned char) u_map->dma;
15290157f60cSPatrick McHardy 		k_map.port = (unsigned char) u_map->port;
15300157f60cSPatrick McHardy 
1531d314774cSStephen Hemminger 		err = ops->ndo_set_config(dev, &k_map);
15320157f60cSPatrick McHardy 		if (err < 0)
15330157f60cSPatrick McHardy 			goto errout;
15340157f60cSPatrick McHardy 
1535*90c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
15360157f60cSPatrick McHardy 	}
15370157f60cSPatrick McHardy 
15380157f60cSPatrick McHardy 	if (tb[IFLA_ADDRESS]) {
15390157f60cSPatrick McHardy 		struct sockaddr *sa;
15400157f60cSPatrick McHardy 		int len;
15410157f60cSPatrick McHardy 
15420157f60cSPatrick McHardy 		len = sizeof(sa_family_t) + dev->addr_len;
15430157f60cSPatrick McHardy 		sa = kmalloc(len, GFP_KERNEL);
15440157f60cSPatrick McHardy 		if (!sa) {
15450157f60cSPatrick McHardy 			err = -ENOMEM;
15460157f60cSPatrick McHardy 			goto errout;
15470157f60cSPatrick McHardy 		}
15480157f60cSPatrick McHardy 		sa->sa_family = dev->type;
15490157f60cSPatrick McHardy 		memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
15500157f60cSPatrick McHardy 		       dev->addr_len);
1551e7c3273eSJiri Pirko 		err = dev_set_mac_address(dev, sa);
15520157f60cSPatrick McHardy 		kfree(sa);
15530157f60cSPatrick McHardy 		if (err)
15540157f60cSPatrick McHardy 			goto errout;
1555*90c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
15560157f60cSPatrick McHardy 	}
15570157f60cSPatrick McHardy 
15580157f60cSPatrick McHardy 	if (tb[IFLA_MTU]) {
15590157f60cSPatrick McHardy 		err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
15600157f60cSPatrick McHardy 		if (err < 0)
15610157f60cSPatrick McHardy 			goto errout;
1562*90c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
15630157f60cSPatrick McHardy 	}
15640157f60cSPatrick McHardy 
1565cbda10faSVlad Dogaru 	if (tb[IFLA_GROUP]) {
1566cbda10faSVlad Dogaru 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
1567*90c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
1568cbda10faSVlad Dogaru 	}
1569cbda10faSVlad Dogaru 
15700157f60cSPatrick McHardy 	/*
15710157f60cSPatrick McHardy 	 * Interface selected by interface index but interface
15720157f60cSPatrick McHardy 	 * name provided implies that a name change has been
15730157f60cSPatrick McHardy 	 * requested.
15740157f60cSPatrick McHardy 	 */
15750157f60cSPatrick McHardy 	if (ifm->ifi_index > 0 && ifname[0]) {
15760157f60cSPatrick McHardy 		err = dev_change_name(dev, ifname);
15770157f60cSPatrick McHardy 		if (err < 0)
15780157f60cSPatrick McHardy 			goto errout;
1579*90c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
15800157f60cSPatrick McHardy 	}
15810157f60cSPatrick McHardy 
15820b815a1aSStephen Hemminger 	if (tb[IFLA_IFALIAS]) {
15830b815a1aSStephen Hemminger 		err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
15840b815a1aSStephen Hemminger 				    nla_len(tb[IFLA_IFALIAS]));
15850b815a1aSStephen Hemminger 		if (err < 0)
15860b815a1aSStephen Hemminger 			goto errout;
1587*90c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
15880b815a1aSStephen Hemminger 	}
15890b815a1aSStephen Hemminger 
15900157f60cSPatrick McHardy 	if (tb[IFLA_BROADCAST]) {
15910157f60cSPatrick McHardy 		nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
1592e7c3273eSJiri Pirko 		call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
15930157f60cSPatrick McHardy 	}
15940157f60cSPatrick McHardy 
15950157f60cSPatrick McHardy 	if (ifm->ifi_flags || ifm->ifi_change) {
15963729d502SPatrick McHardy 		err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
15975f9021cfSJohannes Berg 		if (err < 0)
15985f9021cfSJohannes Berg 			goto errout;
15990157f60cSPatrick McHardy 	}
16000157f60cSPatrick McHardy 
1601fbaec0eaSJiri Pirko 	if (tb[IFLA_MASTER]) {
1602fbaec0eaSJiri Pirko 		err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
1603fbaec0eaSJiri Pirko 		if (err)
1604fbaec0eaSJiri Pirko 			goto errout;
1605*90c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
1606fbaec0eaSJiri Pirko 	}
1607fbaec0eaSJiri Pirko 
16089a57247fSJiri Pirko 	if (tb[IFLA_CARRIER]) {
16099a57247fSJiri Pirko 		err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
16109a57247fSJiri Pirko 		if (err)
16119a57247fSJiri Pirko 			goto errout;
1612*90c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
16139a57247fSJiri Pirko 	}
16149a57247fSJiri Pirko 
16155d1180fcSNicolas Dichtel 	if (tb[IFLA_TXQLEN]) {
16165d1180fcSNicolas Dichtel 		unsigned long value = nla_get_u32(tb[IFLA_TXQLEN]);
16175d1180fcSNicolas Dichtel 
16185d1180fcSNicolas Dichtel 		if (dev->tx_queue_len ^ value)
1619*90c325e3SNicolas Dichtel 			status |= DO_SETLINK_MODIFIED;
16205d1180fcSNicolas Dichtel 
16215d1180fcSNicolas Dichtel 		dev->tx_queue_len = value;
16225d1180fcSNicolas Dichtel 	}
16230157f60cSPatrick McHardy 
16240157f60cSPatrick McHardy 	if (tb[IFLA_OPERSTATE])
162593b2d4a2SDavid S. Miller 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
16260157f60cSPatrick McHardy 
16270157f60cSPatrick McHardy 	if (tb[IFLA_LINKMODE]) {
16281889b0e7SNicolas Dichtel 		unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
16291889b0e7SNicolas Dichtel 
16300157f60cSPatrick McHardy 		write_lock_bh(&dev_base_lock);
16311889b0e7SNicolas Dichtel 		if (dev->link_mode ^ value)
1632*90c325e3SNicolas Dichtel 			status |= DO_SETLINK_MODIFIED;
16331889b0e7SNicolas Dichtel 		dev->link_mode = value;
163493b2d4a2SDavid S. Miller 		write_unlock_bh(&dev_base_lock);
16350157f60cSPatrick McHardy 	}
16360157f60cSPatrick McHardy 
1637c02db8c6SChris Wright 	if (tb[IFLA_VFINFO_LIST]) {
1638c02db8c6SChris Wright 		struct nlattr *attr;
1639c02db8c6SChris Wright 		int rem;
1640c02db8c6SChris Wright 		nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
1641253683bbSDavid Howells 			if (nla_type(attr) != IFLA_VF_INFO) {
1642253683bbSDavid Howells 				err = -EINVAL;
1643c02db8c6SChris Wright 				goto errout;
1644253683bbSDavid Howells 			}
1645c02db8c6SChris Wright 			err = do_setvfinfo(dev, attr);
1646ebc08a6fSWilliams, Mitch A 			if (err < 0)
1647ebc08a6fSWilliams, Mitch A 				goto errout;
1648*90c325e3SNicolas Dichtel 			status |= DO_SETLINK_MODIFIED;
1649ebc08a6fSWilliams, Mitch A 		}
1650ebc08a6fSWilliams, Mitch A 	}
16510157f60cSPatrick McHardy 	err = 0;
16520157f60cSPatrick McHardy 
165357b61080SScott Feldman 	if (tb[IFLA_VF_PORTS]) {
165457b61080SScott Feldman 		struct nlattr *port[IFLA_PORT_MAX+1];
165557b61080SScott Feldman 		struct nlattr *attr;
165657b61080SScott Feldman 		int vf;
165757b61080SScott Feldman 		int rem;
165857b61080SScott Feldman 
165957b61080SScott Feldman 		err = -EOPNOTSUPP;
166057b61080SScott Feldman 		if (!ops->ndo_set_vf_port)
166157b61080SScott Feldman 			goto errout;
166257b61080SScott Feldman 
166357b61080SScott Feldman 		nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
166457b61080SScott Feldman 			if (nla_type(attr) != IFLA_VF_PORT)
166557b61080SScott Feldman 				continue;
166657b61080SScott Feldman 			err = nla_parse_nested(port, IFLA_PORT_MAX,
166757b61080SScott Feldman 				attr, ifla_port_policy);
166857b61080SScott Feldman 			if (err < 0)
166957b61080SScott Feldman 				goto errout;
167057b61080SScott Feldman 			if (!port[IFLA_PORT_VF]) {
167157b61080SScott Feldman 				err = -EOPNOTSUPP;
167257b61080SScott Feldman 				goto errout;
167357b61080SScott Feldman 			}
167457b61080SScott Feldman 			vf = nla_get_u32(port[IFLA_PORT_VF]);
167557b61080SScott Feldman 			err = ops->ndo_set_vf_port(dev, vf, port);
167657b61080SScott Feldman 			if (err < 0)
167757b61080SScott Feldman 				goto errout;
1678*90c325e3SNicolas Dichtel 			status |= DO_SETLINK_MODIFIED;
167957b61080SScott Feldman 		}
168057b61080SScott Feldman 	}
168157b61080SScott Feldman 	err = 0;
168257b61080SScott Feldman 
168357b61080SScott Feldman 	if (tb[IFLA_PORT_SELF]) {
168457b61080SScott Feldman 		struct nlattr *port[IFLA_PORT_MAX+1];
168557b61080SScott Feldman 
168657b61080SScott Feldman 		err = nla_parse_nested(port, IFLA_PORT_MAX,
168757b61080SScott Feldman 			tb[IFLA_PORT_SELF], ifla_port_policy);
168857b61080SScott Feldman 		if (err < 0)
168957b61080SScott Feldman 			goto errout;
169057b61080SScott Feldman 
169157b61080SScott Feldman 		err = -EOPNOTSUPP;
169257b61080SScott Feldman 		if (ops->ndo_set_vf_port)
169357b61080SScott Feldman 			err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
169457b61080SScott Feldman 		if (err < 0)
169557b61080SScott Feldman 			goto errout;
1696*90c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
169757b61080SScott Feldman 	}
1698f8ff182cSThomas Graf 
1699f8ff182cSThomas Graf 	if (tb[IFLA_AF_SPEC]) {
1700f8ff182cSThomas Graf 		struct nlattr *af;
1701f8ff182cSThomas Graf 		int rem;
1702f8ff182cSThomas Graf 
1703f8ff182cSThomas Graf 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1704f8ff182cSThomas Graf 			const struct rtnl_af_ops *af_ops;
1705f8ff182cSThomas Graf 
1706f8ff182cSThomas Graf 			if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1707cf7afbfeSThomas Graf 				BUG();
1708f8ff182cSThomas Graf 
1709cf7afbfeSThomas Graf 			err = af_ops->set_link_af(dev, af);
1710f8ff182cSThomas Graf 			if (err < 0)
1711f8ff182cSThomas Graf 				goto errout;
1712f8ff182cSThomas Graf 
1713*90c325e3SNicolas Dichtel 			status |= DO_SETLINK_MODIFIED;
1714f8ff182cSThomas Graf 		}
1715f8ff182cSThomas Graf 	}
171657b61080SScott Feldman 	err = 0;
171757b61080SScott Feldman 
17180157f60cSPatrick McHardy errout:
1719*90c325e3SNicolas Dichtel 	if (err < 0 && status & DO_SETLINK_MODIFIED)
1720e87cc472SJoe 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",
1721e87cc472SJoe Perches 				     dev->name);
17220157f60cSPatrick McHardy 
17230157f60cSPatrick McHardy 	return err;
17240157f60cSPatrick McHardy }
17250157f60cSPatrick McHardy 
1726661d2967SThomas Graf static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
1727da5e0494SThomas Graf {
17283b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
1729da5e0494SThomas Graf 	struct ifinfomsg *ifm;
1730da5e0494SThomas Graf 	struct net_device *dev;
17310157f60cSPatrick McHardy 	int err;
1732da5e0494SThomas Graf 	struct nlattr *tb[IFLA_MAX+1];
17331da177e4SLinus Torvalds 	char ifname[IFNAMSIZ];
17341da177e4SLinus Torvalds 
1735da5e0494SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1736da5e0494SThomas Graf 	if (err < 0)
1737da5e0494SThomas Graf 		goto errout;
17381da177e4SLinus Torvalds 
17395176f91eSThomas Graf 	if (tb[IFLA_IFNAME])
17405176f91eSThomas Graf 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
174178e5b891SPatrick McHardy 	else
174278e5b891SPatrick McHardy 		ifname[0] = '\0';
17431da177e4SLinus Torvalds 
17441da177e4SLinus Torvalds 	err = -EINVAL;
1745da5e0494SThomas Graf 	ifm = nlmsg_data(nlh);
174651055be8SPatrick McHardy 	if (ifm->ifi_index > 0)
1747a3d12891SEric Dumazet 		dev = __dev_get_by_index(net, ifm->ifi_index);
1748da5e0494SThomas Graf 	else if (tb[IFLA_IFNAME])
1749a3d12891SEric Dumazet 		dev = __dev_get_by_name(net, ifname);
1750da5e0494SThomas Graf 	else
1751da5e0494SThomas Graf 		goto errout;
17521da177e4SLinus Torvalds 
1753da5e0494SThomas Graf 	if (dev == NULL) {
1754da5e0494SThomas Graf 		err = -ENODEV;
1755da5e0494SThomas Graf 		goto errout;
1756da5e0494SThomas Graf 	}
17571da177e4SLinus Torvalds 
1758e0d087afSEric Dumazet 	err = validate_linkmsg(dev, tb);
1759e0d087afSEric Dumazet 	if (err < 0)
1760a3d12891SEric Dumazet 		goto errout;
1761da5e0494SThomas Graf 
176290f62cf3SEric W. Biederman 	err = do_setlink(skb, dev, ifm, tb, ifname, 0);
1763da5e0494SThomas Graf errout:
17641da177e4SLinus Torvalds 	return err;
17651da177e4SLinus Torvalds }
17661da177e4SLinus Torvalds 
1767661d2967SThomas Graf static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
176838f7b870SPatrick McHardy {
17693b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
177038f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops;
177138f7b870SPatrick McHardy 	struct net_device *dev;
177238f7b870SPatrick McHardy 	struct ifinfomsg *ifm;
177338f7b870SPatrick McHardy 	char ifname[IFNAMSIZ];
177438f7b870SPatrick McHardy 	struct nlattr *tb[IFLA_MAX+1];
177538f7b870SPatrick McHardy 	int err;
1776226bd341SEric Dumazet 	LIST_HEAD(list_kill);
177738f7b870SPatrick McHardy 
177838f7b870SPatrick McHardy 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
177938f7b870SPatrick McHardy 	if (err < 0)
178038f7b870SPatrick McHardy 		return err;
178138f7b870SPatrick McHardy 
178238f7b870SPatrick McHardy 	if (tb[IFLA_IFNAME])
178338f7b870SPatrick McHardy 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
178438f7b870SPatrick McHardy 
178538f7b870SPatrick McHardy 	ifm = nlmsg_data(nlh);
178638f7b870SPatrick McHardy 	if (ifm->ifi_index > 0)
1787881d966bSEric W. Biederman 		dev = __dev_get_by_index(net, ifm->ifi_index);
178838f7b870SPatrick McHardy 	else if (tb[IFLA_IFNAME])
1789881d966bSEric W. Biederman 		dev = __dev_get_by_name(net, ifname);
179038f7b870SPatrick McHardy 	else
179138f7b870SPatrick McHardy 		return -EINVAL;
179238f7b870SPatrick McHardy 
179338f7b870SPatrick McHardy 	if (!dev)
179438f7b870SPatrick McHardy 		return -ENODEV;
179538f7b870SPatrick McHardy 
179638f7b870SPatrick McHardy 	ops = dev->rtnl_link_ops;
1797b0ab2fabSJiri Pirko 	if (!ops || !ops->dellink)
179838f7b870SPatrick McHardy 		return -EOPNOTSUPP;
179938f7b870SPatrick McHardy 
1800226bd341SEric Dumazet 	ops->dellink(dev, &list_kill);
1801226bd341SEric Dumazet 	unregister_netdevice_many(&list_kill);
180238f7b870SPatrick McHardy 	return 0;
180338f7b870SPatrick McHardy }
180438f7b870SPatrick McHardy 
18053729d502SPatrick McHardy int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
18063729d502SPatrick McHardy {
18073729d502SPatrick McHardy 	unsigned int old_flags;
18083729d502SPatrick McHardy 	int err;
18093729d502SPatrick McHardy 
18103729d502SPatrick McHardy 	old_flags = dev->flags;
18113729d502SPatrick McHardy 	if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
18123729d502SPatrick McHardy 		err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
18133729d502SPatrick McHardy 		if (err < 0)
18143729d502SPatrick McHardy 			return err;
18153729d502SPatrick McHardy 	}
18163729d502SPatrick McHardy 
18173729d502SPatrick McHardy 	dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
18183729d502SPatrick McHardy 
1819a528c219SNicolas Dichtel 	__dev_notify_flags(dev, old_flags, ~0U);
18203729d502SPatrick McHardy 	return 0;
18213729d502SPatrick McHardy }
18223729d502SPatrick McHardy EXPORT_SYMBOL(rtnl_configure_link);
18233729d502SPatrick McHardy 
1824c0713563SRami Rosen struct net_device *rtnl_create_link(struct net *net,
18255517750fSTom Gundersen 	char *ifname, unsigned char name_assign_type,
18265517750fSTom Gundersen 	const struct rtnl_link_ops *ops, struct nlattr *tb[])
1827e7199288SPavel Emelianov {
1828e7199288SPavel Emelianov 	int err;
1829e7199288SPavel Emelianov 	struct net_device *dev;
1830d40156aaSJiri Pirko 	unsigned int num_tx_queues = 1;
1831d40156aaSJiri Pirko 	unsigned int num_rx_queues = 1;
1832e7199288SPavel Emelianov 
183376ff5cc9SJiri Pirko 	if (tb[IFLA_NUM_TX_QUEUES])
183476ff5cc9SJiri Pirko 		num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
183576ff5cc9SJiri Pirko 	else if (ops->get_num_tx_queues)
1836d40156aaSJiri Pirko 		num_tx_queues = ops->get_num_tx_queues();
183776ff5cc9SJiri Pirko 
183876ff5cc9SJiri Pirko 	if (tb[IFLA_NUM_RX_QUEUES])
183976ff5cc9SJiri Pirko 		num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
184076ff5cc9SJiri Pirko 	else if (ops->get_num_rx_queues)
1841d40156aaSJiri Pirko 		num_rx_queues = ops->get_num_rx_queues();
1842efacb309Sstephen hemminger 
1843e7199288SPavel Emelianov 	err = -ENOMEM;
18445517750fSTom Gundersen 	dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
1845c835a677STom Gundersen 			       ops->setup, num_tx_queues, num_rx_queues);
1846e7199288SPavel Emelianov 	if (!dev)
1847e7199288SPavel Emelianov 		goto err;
1848e7199288SPavel Emelianov 
184981adee47SEric W. Biederman 	dev_net_set(dev, net);
185081adee47SEric W. Biederman 	dev->rtnl_link_ops = ops;
18513729d502SPatrick McHardy 	dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
185281adee47SEric W. Biederman 
1853e7199288SPavel Emelianov 	if (tb[IFLA_MTU])
1854e7199288SPavel Emelianov 		dev->mtu = nla_get_u32(tb[IFLA_MTU]);
18552afb9b53SJiri Pirko 	if (tb[IFLA_ADDRESS]) {
1856e7199288SPavel Emelianov 		memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
1857e7199288SPavel Emelianov 				nla_len(tb[IFLA_ADDRESS]));
18582afb9b53SJiri Pirko 		dev->addr_assign_type = NET_ADDR_SET;
18592afb9b53SJiri Pirko 	}
1860e7199288SPavel Emelianov 	if (tb[IFLA_BROADCAST])
1861e7199288SPavel Emelianov 		memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
1862e7199288SPavel Emelianov 				nla_len(tb[IFLA_BROADCAST]));
1863e7199288SPavel Emelianov 	if (tb[IFLA_TXQLEN])
1864e7199288SPavel Emelianov 		dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
1865e7199288SPavel Emelianov 	if (tb[IFLA_OPERSTATE])
186693b2d4a2SDavid S. Miller 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
1867e7199288SPavel Emelianov 	if (tb[IFLA_LINKMODE])
1868e7199288SPavel Emelianov 		dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
1869ffa934f1SPatrick McHardy 	if (tb[IFLA_GROUP])
1870ffa934f1SPatrick McHardy 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
1871e7199288SPavel Emelianov 
1872e7199288SPavel Emelianov 	return dev;
1873e7199288SPavel Emelianov 
1874e7199288SPavel Emelianov err:
1875e7199288SPavel Emelianov 	return ERR_PTR(err);
1876e7199288SPavel Emelianov }
1877e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_create_link);
1878e7199288SPavel Emelianov 
187990f62cf3SEric W. Biederman static int rtnl_group_changelink(const struct sk_buff *skb,
188090f62cf3SEric W. Biederman 		struct net *net, int group,
1881e7ed828fSVlad Dogaru 		struct ifinfomsg *ifm,
1882e7ed828fSVlad Dogaru 		struct nlattr **tb)
1883e7ed828fSVlad Dogaru {
1884e7ed828fSVlad Dogaru 	struct net_device *dev;
1885e7ed828fSVlad Dogaru 	int err;
1886e7ed828fSVlad Dogaru 
1887e7ed828fSVlad Dogaru 	for_each_netdev(net, dev) {
1888e7ed828fSVlad Dogaru 		if (dev->group == group) {
188990f62cf3SEric W. Biederman 			err = do_setlink(skb, dev, ifm, tb, NULL, 0);
1890e7ed828fSVlad Dogaru 			if (err < 0)
1891e7ed828fSVlad Dogaru 				return err;
1892e7ed828fSVlad Dogaru 		}
1893e7ed828fSVlad Dogaru 	}
1894e7ed828fSVlad Dogaru 
1895e7ed828fSVlad Dogaru 	return 0;
1896e7ed828fSVlad Dogaru }
1897e7ed828fSVlad Dogaru 
1898661d2967SThomas Graf static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh)
189938f7b870SPatrick McHardy {
19003b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
190138f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops;
1902ba7d49b1SJiri Pirko 	const struct rtnl_link_ops *m_ops = NULL;
190338f7b870SPatrick McHardy 	struct net_device *dev;
1904ba7d49b1SJiri Pirko 	struct net_device *master_dev = NULL;
190538f7b870SPatrick McHardy 	struct ifinfomsg *ifm;
190638f7b870SPatrick McHardy 	char kind[MODULE_NAME_LEN];
190738f7b870SPatrick McHardy 	char ifname[IFNAMSIZ];
190838f7b870SPatrick McHardy 	struct nlattr *tb[IFLA_MAX+1];
190938f7b870SPatrick McHardy 	struct nlattr *linkinfo[IFLA_INFO_MAX+1];
19105517750fSTom Gundersen 	unsigned char name_assign_type = NET_NAME_USER;
191138f7b870SPatrick McHardy 	int err;
191238f7b870SPatrick McHardy 
191395a5afcaSJohannes Berg #ifdef CONFIG_MODULES
191438f7b870SPatrick McHardy replay:
19158072f085SThomas Graf #endif
191638f7b870SPatrick McHardy 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
191738f7b870SPatrick McHardy 	if (err < 0)
191838f7b870SPatrick McHardy 		return err;
191938f7b870SPatrick McHardy 
192038f7b870SPatrick McHardy 	if (tb[IFLA_IFNAME])
192138f7b870SPatrick McHardy 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
192238f7b870SPatrick McHardy 	else
192338f7b870SPatrick McHardy 		ifname[0] = '\0';
192438f7b870SPatrick McHardy 
192538f7b870SPatrick McHardy 	ifm = nlmsg_data(nlh);
192638f7b870SPatrick McHardy 	if (ifm->ifi_index > 0)
1927881d966bSEric W. Biederman 		dev = __dev_get_by_index(net, ifm->ifi_index);
1928e7ed828fSVlad Dogaru 	else {
1929e7ed828fSVlad Dogaru 		if (ifname[0])
1930881d966bSEric W. Biederman 			dev = __dev_get_by_name(net, ifname);
193138f7b870SPatrick McHardy 		else
193238f7b870SPatrick McHardy 			dev = NULL;
1933e7ed828fSVlad Dogaru 	}
193438f7b870SPatrick McHardy 
1935ba7d49b1SJiri Pirko 	if (dev) {
1936ba7d49b1SJiri Pirko 		master_dev = netdev_master_upper_dev_get(dev);
1937ba7d49b1SJiri Pirko 		if (master_dev)
1938ba7d49b1SJiri Pirko 			m_ops = master_dev->rtnl_link_ops;
1939ba7d49b1SJiri Pirko 	}
1940ba7d49b1SJiri Pirko 
1941e0d087afSEric Dumazet 	err = validate_linkmsg(dev, tb);
1942e0d087afSEric Dumazet 	if (err < 0)
19431840bb13SThomas Graf 		return err;
19441840bb13SThomas Graf 
194538f7b870SPatrick McHardy 	if (tb[IFLA_LINKINFO]) {
194638f7b870SPatrick McHardy 		err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
194738f7b870SPatrick McHardy 				       tb[IFLA_LINKINFO], ifla_info_policy);
194838f7b870SPatrick McHardy 		if (err < 0)
194938f7b870SPatrick McHardy 			return err;
195038f7b870SPatrick McHardy 	} else
195138f7b870SPatrick McHardy 		memset(linkinfo, 0, sizeof(linkinfo));
195238f7b870SPatrick McHardy 
195338f7b870SPatrick McHardy 	if (linkinfo[IFLA_INFO_KIND]) {
195438f7b870SPatrick McHardy 		nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
195538f7b870SPatrick McHardy 		ops = rtnl_link_ops_get(kind);
195638f7b870SPatrick McHardy 	} else {
195738f7b870SPatrick McHardy 		kind[0] = '\0';
195838f7b870SPatrick McHardy 		ops = NULL;
195938f7b870SPatrick McHardy 	}
196038f7b870SPatrick McHardy 
196138f7b870SPatrick McHardy 	if (1) {
1962ba7d49b1SJiri Pirko 		struct nlattr *attr[ops ? ops->maxtype + 1 : 0];
1963ba7d49b1SJiri Pirko 		struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 0];
1964ba7d49b1SJiri Pirko 		struct nlattr **data = NULL;
1965ba7d49b1SJiri Pirko 		struct nlattr **slave_data = NULL;
196681adee47SEric W. Biederman 		struct net *dest_net;
196738f7b870SPatrick McHardy 
196838f7b870SPatrick McHardy 		if (ops) {
196938f7b870SPatrick McHardy 			if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
197038f7b870SPatrick McHardy 				err = nla_parse_nested(attr, ops->maxtype,
197138f7b870SPatrick McHardy 						       linkinfo[IFLA_INFO_DATA],
197238f7b870SPatrick McHardy 						       ops->policy);
197338f7b870SPatrick McHardy 				if (err < 0)
197438f7b870SPatrick McHardy 					return err;
197538f7b870SPatrick McHardy 				data = attr;
197638f7b870SPatrick McHardy 			}
197738f7b870SPatrick McHardy 			if (ops->validate) {
197838f7b870SPatrick McHardy 				err = ops->validate(tb, data);
197938f7b870SPatrick McHardy 				if (err < 0)
198038f7b870SPatrick McHardy 					return err;
198138f7b870SPatrick McHardy 			}
198238f7b870SPatrick McHardy 		}
198338f7b870SPatrick McHardy 
1984ba7d49b1SJiri Pirko 		if (m_ops) {
1985ba7d49b1SJiri Pirko 			if (m_ops->slave_maxtype &&
1986ba7d49b1SJiri Pirko 			    linkinfo[IFLA_INFO_SLAVE_DATA]) {
1987ba7d49b1SJiri Pirko 				err = nla_parse_nested(slave_attr,
1988ba7d49b1SJiri Pirko 						       m_ops->slave_maxtype,
1989ba7d49b1SJiri Pirko 						       linkinfo[IFLA_INFO_SLAVE_DATA],
1990ba7d49b1SJiri Pirko 						       m_ops->slave_policy);
1991ba7d49b1SJiri Pirko 				if (err < 0)
1992ba7d49b1SJiri Pirko 					return err;
1993ba7d49b1SJiri Pirko 				slave_data = slave_attr;
1994ba7d49b1SJiri Pirko 			}
1995ba7d49b1SJiri Pirko 			if (m_ops->slave_validate) {
1996ba7d49b1SJiri Pirko 				err = m_ops->slave_validate(tb, slave_data);
1997ba7d49b1SJiri Pirko 				if (err < 0)
1998ba7d49b1SJiri Pirko 					return err;
1999ba7d49b1SJiri Pirko 			}
2000ba7d49b1SJiri Pirko 		}
2001ba7d49b1SJiri Pirko 
200238f7b870SPatrick McHardy 		if (dev) {
2003*90c325e3SNicolas Dichtel 			int status = 0;
200438f7b870SPatrick McHardy 
200538f7b870SPatrick McHardy 			if (nlh->nlmsg_flags & NLM_F_EXCL)
200638f7b870SPatrick McHardy 				return -EEXIST;
200738f7b870SPatrick McHardy 			if (nlh->nlmsg_flags & NLM_F_REPLACE)
200838f7b870SPatrick McHardy 				return -EOPNOTSUPP;
200938f7b870SPatrick McHardy 
201038f7b870SPatrick McHardy 			if (linkinfo[IFLA_INFO_DATA]) {
201138f7b870SPatrick McHardy 				if (!ops || ops != dev->rtnl_link_ops ||
201238f7b870SPatrick McHardy 				    !ops->changelink)
201338f7b870SPatrick McHardy 					return -EOPNOTSUPP;
201438f7b870SPatrick McHardy 
201538f7b870SPatrick McHardy 				err = ops->changelink(dev, tb, data);
201638f7b870SPatrick McHardy 				if (err < 0)
201738f7b870SPatrick McHardy 					return err;
2018*90c325e3SNicolas Dichtel 				status |= DO_SETLINK_MODIFIED;
201938f7b870SPatrick McHardy 			}
202038f7b870SPatrick McHardy 
2021ba7d49b1SJiri Pirko 			if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
2022ba7d49b1SJiri Pirko 				if (!m_ops || !m_ops->slave_changelink)
2023ba7d49b1SJiri Pirko 					return -EOPNOTSUPP;
2024ba7d49b1SJiri Pirko 
2025ba7d49b1SJiri Pirko 				err = m_ops->slave_changelink(master_dev, dev,
2026ba7d49b1SJiri Pirko 							      tb, slave_data);
2027ba7d49b1SJiri Pirko 				if (err < 0)
2028ba7d49b1SJiri Pirko 					return err;
2029*90c325e3SNicolas Dichtel 				status |= DO_SETLINK_MODIFIED;
2030ba7d49b1SJiri Pirko 			}
2031ba7d49b1SJiri Pirko 
2032*90c325e3SNicolas Dichtel 			return do_setlink(skb, dev, ifm, tb, ifname, status);
203338f7b870SPatrick McHardy 		}
203438f7b870SPatrick McHardy 
2035ffa934f1SPatrick McHardy 		if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
2036ffa934f1SPatrick McHardy 			if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
203790f62cf3SEric W. Biederman 				return rtnl_group_changelink(skb, net,
2038ffa934f1SPatrick McHardy 						nla_get_u32(tb[IFLA_GROUP]),
2039ffa934f1SPatrick McHardy 						ifm, tb);
204038f7b870SPatrick McHardy 			return -ENODEV;
2041ffa934f1SPatrick McHardy 		}
204238f7b870SPatrick McHardy 
20430e06877cSPatrick McHardy 		if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
204438f7b870SPatrick McHardy 			return -EOPNOTSUPP;
204538f7b870SPatrick McHardy 
204638f7b870SPatrick McHardy 		if (!ops) {
204795a5afcaSJohannes Berg #ifdef CONFIG_MODULES
204838f7b870SPatrick McHardy 			if (kind[0]) {
204938f7b870SPatrick McHardy 				__rtnl_unlock();
205038f7b870SPatrick McHardy 				request_module("rtnl-link-%s", kind);
205138f7b870SPatrick McHardy 				rtnl_lock();
205238f7b870SPatrick McHardy 				ops = rtnl_link_ops_get(kind);
205338f7b870SPatrick McHardy 				if (ops)
205438f7b870SPatrick McHardy 					goto replay;
205538f7b870SPatrick McHardy 			}
205638f7b870SPatrick McHardy #endif
205738f7b870SPatrick McHardy 			return -EOPNOTSUPP;
205838f7b870SPatrick McHardy 		}
205938f7b870SPatrick McHardy 
2060b0ab2fabSJiri Pirko 		if (!ops->setup)
2061b0ab2fabSJiri Pirko 			return -EOPNOTSUPP;
2062b0ab2fabSJiri Pirko 
20635517750fSTom Gundersen 		if (!ifname[0]) {
206438f7b870SPatrick McHardy 			snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
20655517750fSTom Gundersen 			name_assign_type = NET_NAME_ENUM;
20665517750fSTom Gundersen 		}
206738f7b870SPatrick McHardy 
206881adee47SEric W. Biederman 		dest_net = rtnl_link_get_net(net, tb);
206913ad1774SEric W. Biederman 		if (IS_ERR(dest_net))
207013ad1774SEric W. Biederman 			return PTR_ERR(dest_net);
207113ad1774SEric W. Biederman 
20725517750fSTom Gundersen 		dev = rtnl_create_link(dest_net, ifname, name_assign_type, ops, tb);
20739c7dafbfSPavel Emelyanov 		if (IS_ERR(dev)) {
2074e7199288SPavel Emelianov 			err = PTR_ERR(dev);
20759c7dafbfSPavel Emelyanov 			goto out;
20769c7dafbfSPavel Emelyanov 		}
20779c7dafbfSPavel Emelyanov 
20789c7dafbfSPavel Emelyanov 		dev->ifindex = ifm->ifi_index;
20799c7dafbfSPavel Emelyanov 
20800e0eee24SCong Wang 		if (ops->newlink) {
208181adee47SEric W. Biederman 			err = ops->newlink(net, dev, tb, data);
20820e0eee24SCong Wang 			/* Drivers should call free_netdev() in ->destructor
2083e51fb152SCong Wang 			 * and unregister it on failure after registration
2084e51fb152SCong Wang 			 * so that device could be finally freed in rtnl_unlock.
20850e0eee24SCong Wang 			 */
2086e51fb152SCong Wang 			if (err < 0) {
2087e51fb152SCong Wang 				/* If device is not registered at all, free it now */
2088e51fb152SCong Wang 				if (dev->reg_state == NETREG_UNINITIALIZED)
2089e51fb152SCong Wang 					free_netdev(dev);
20900e0eee24SCong Wang 				goto out;
2091e51fb152SCong Wang 			}
20920e0eee24SCong Wang 		} else {
20932d85cba2SPatrick McHardy 			err = register_netdevice(dev);
2094fce9b9beSDan Carpenter 			if (err < 0) {
209538f7b870SPatrick McHardy 				free_netdev(dev);
20963729d502SPatrick McHardy 				goto out;
2097fce9b9beSDan Carpenter 			}
20980e0eee24SCong Wang 		}
20993729d502SPatrick McHardy 		err = rtnl_configure_link(dev, ifm);
21003729d502SPatrick McHardy 		if (err < 0)
21013729d502SPatrick McHardy 			unregister_netdevice(dev);
21023729d502SPatrick McHardy out:
210381adee47SEric W. Biederman 		put_net(dest_net);
210438f7b870SPatrick McHardy 		return err;
210538f7b870SPatrick McHardy 	}
210638f7b870SPatrick McHardy }
210738f7b870SPatrick McHardy 
2108661d2967SThomas Graf static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh)
2109711e2c33SJean Tourrilhes {
21103b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
2111b60c5115SThomas Graf 	struct ifinfomsg *ifm;
2112a3d12891SEric Dumazet 	char ifname[IFNAMSIZ];
2113b60c5115SThomas Graf 	struct nlattr *tb[IFLA_MAX+1];
2114b60c5115SThomas Graf 	struct net_device *dev = NULL;
2115b60c5115SThomas Graf 	struct sk_buff *nskb;
2116339bf98fSThomas Graf 	int err;
2117115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
2118711e2c33SJean Tourrilhes 
2119b60c5115SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2120b60c5115SThomas Graf 	if (err < 0)
21219918f230SEric Sesterhenn 		return err;
2122b60c5115SThomas Graf 
2123a3d12891SEric Dumazet 	if (tb[IFLA_IFNAME])
2124a3d12891SEric Dumazet 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2125a3d12891SEric Dumazet 
2126115c9b81SGreg Rose 	if (tb[IFLA_EXT_MASK])
2127115c9b81SGreg Rose 		ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2128115c9b81SGreg Rose 
2129b60c5115SThomas Graf 	ifm = nlmsg_data(nlh);
2130a3d12891SEric Dumazet 	if (ifm->ifi_index > 0)
2131a3d12891SEric Dumazet 		dev = __dev_get_by_index(net, ifm->ifi_index);
2132a3d12891SEric Dumazet 	else if (tb[IFLA_IFNAME])
2133a3d12891SEric Dumazet 		dev = __dev_get_by_name(net, ifname);
2134a3d12891SEric Dumazet 	else
2135b60c5115SThomas Graf 		return -EINVAL;
2136b60c5115SThomas Graf 
2137a3d12891SEric Dumazet 	if (dev == NULL)
2138a3d12891SEric Dumazet 		return -ENODEV;
2139a3d12891SEric Dumazet 
2140115c9b81SGreg Rose 	nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
2141a3d12891SEric Dumazet 	if (nskb == NULL)
2142a3d12891SEric Dumazet 		return -ENOBUFS;
2143711e2c33SJean Tourrilhes 
214415e47304SEric W. Biederman 	err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid,
2145115c9b81SGreg Rose 			       nlh->nlmsg_seq, 0, 0, ext_filter_mask);
214626932566SPatrick McHardy 	if (err < 0) {
214726932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in if_nlmsg_size */
214826932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
214926932566SPatrick McHardy 		kfree_skb(nskb);
2150a3d12891SEric Dumazet 	} else
215115e47304SEric W. Biederman 		err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
2152b60c5115SThomas Graf 
2153711e2c33SJean Tourrilhes 	return err;
2154711e2c33SJean Tourrilhes }
2155711e2c33SJean Tourrilhes 
2156115c9b81SGreg Rose static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
2157c7ac8679SGreg Rose {
2158115c9b81SGreg Rose 	struct net *net = sock_net(skb->sk);
2159115c9b81SGreg Rose 	struct net_device *dev;
2160115c9b81SGreg Rose 	struct nlattr *tb[IFLA_MAX+1];
2161115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
2162115c9b81SGreg Rose 	u16 min_ifinfo_dump_size = 0;
2163e5eca6d4SMichal Schmidt 	int hdrlen;
2164115c9b81SGreg Rose 
2165e5eca6d4SMichal Schmidt 	/* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
2166e5eca6d4SMichal Schmidt 	hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2167e5eca6d4SMichal Schmidt 		 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2168e5eca6d4SMichal Schmidt 
2169e5eca6d4SMichal Schmidt 	if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
2170115c9b81SGreg Rose 		if (tb[IFLA_EXT_MASK])
2171115c9b81SGreg Rose 			ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2172a4b64fbeSEric Dumazet 	}
2173115c9b81SGreg Rose 
2174115c9b81SGreg Rose 	if (!ext_filter_mask)
2175115c9b81SGreg Rose 		return NLMSG_GOODSIZE;
2176115c9b81SGreg Rose 	/*
2177115c9b81SGreg Rose 	 * traverse the list of net devices and compute the minimum
2178115c9b81SGreg Rose 	 * buffer size based upon the filter mask.
2179115c9b81SGreg Rose 	 */
2180115c9b81SGreg Rose 	list_for_each_entry(dev, &net->dev_base_head, dev_list) {
2181115c9b81SGreg Rose 		min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
2182115c9b81SGreg Rose 					     if_nlmsg_size(dev,
2183115c9b81SGreg Rose 						           ext_filter_mask));
2184115c9b81SGreg Rose 	}
2185115c9b81SGreg Rose 
2186c7ac8679SGreg Rose 	return min_ifinfo_dump_size;
2187c7ac8679SGreg Rose }
2188c7ac8679SGreg Rose 
218942bad1daSAdrian Bunk static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
21901da177e4SLinus Torvalds {
21911da177e4SLinus Torvalds 	int idx;
21921da177e4SLinus Torvalds 	int s_idx = cb->family;
21931da177e4SLinus Torvalds 
21941da177e4SLinus Torvalds 	if (s_idx == 0)
21951da177e4SLinus Torvalds 		s_idx = 1;
219625239ceeSPatrick McHardy 	for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
21971da177e4SLinus Torvalds 		int type = cb->nlh->nlmsg_type-RTM_BASE;
21981da177e4SLinus Torvalds 		if (idx < s_idx || idx == PF_PACKET)
21991da177e4SLinus Torvalds 			continue;
2200e2849863SThomas Graf 		if (rtnl_msg_handlers[idx] == NULL ||
2201e2849863SThomas Graf 		    rtnl_msg_handlers[idx][type].dumpit == NULL)
22021da177e4SLinus Torvalds 			continue;
22030465277fSNicolas Dichtel 		if (idx > s_idx) {
22041da177e4SLinus Torvalds 			memset(&cb->args[0], 0, sizeof(cb->args));
22050465277fSNicolas Dichtel 			cb->prev_seq = 0;
22060465277fSNicolas Dichtel 			cb->seq = 0;
22070465277fSNicolas Dichtel 		}
2208e2849863SThomas Graf 		if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
22091da177e4SLinus Torvalds 			break;
22101da177e4SLinus Torvalds 	}
22111da177e4SLinus Torvalds 	cb->family = idx;
22121da177e4SLinus Torvalds 
22131da177e4SLinus Torvalds 	return skb->len;
22141da177e4SLinus Torvalds }
22151da177e4SLinus Torvalds 
22167f294054SAlexei Starovoitov void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
22177f294054SAlexei Starovoitov 		  gfp_t flags)
22181da177e4SLinus Torvalds {
2219c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
22201da177e4SLinus Torvalds 	struct sk_buff *skb;
22210ec6d3f4SThomas Graf 	int err = -ENOBUFS;
2222c7ac8679SGreg Rose 	size_t if_info_size;
22231da177e4SLinus Torvalds 
22247f294054SAlexei Starovoitov 	skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
22250ec6d3f4SThomas Graf 	if (skb == NULL)
22260ec6d3f4SThomas Graf 		goto errout;
22271da177e4SLinus Torvalds 
2228115c9b81SGreg Rose 	err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0);
222926932566SPatrick McHardy 	if (err < 0) {
223026932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in if_nlmsg_size() */
223126932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
223226932566SPatrick McHardy 		kfree_skb(skb);
223326932566SPatrick McHardy 		goto errout;
223426932566SPatrick McHardy 	}
22357f294054SAlexei Starovoitov 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
22361ce85fe4SPablo Neira Ayuso 	return;
22370ec6d3f4SThomas Graf errout:
22380ec6d3f4SThomas Graf 	if (err < 0)
22394b3da706SEric W. Biederman 		rtnl_set_sk_err(net, RTNLGRP_LINK, err);
22401da177e4SLinus Torvalds }
2241471cb5a3SJiri Pirko EXPORT_SYMBOL(rtmsg_ifinfo);
22421da177e4SLinus Torvalds 
2243d83b0603SJohn Fastabend static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
2244d83b0603SJohn Fastabend 				   struct net_device *dev,
2245d83b0603SJohn Fastabend 				   u8 *addr, u32 pid, u32 seq,
22461c104a6bSNicolas Dichtel 				   int type, unsigned int flags,
22471c104a6bSNicolas Dichtel 				   int nlflags)
2248d83b0603SJohn Fastabend {
2249d83b0603SJohn Fastabend 	struct nlmsghdr *nlh;
2250d83b0603SJohn Fastabend 	struct ndmsg *ndm;
2251d83b0603SJohn Fastabend 
22521c104a6bSNicolas Dichtel 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
2253d83b0603SJohn Fastabend 	if (!nlh)
2254d83b0603SJohn Fastabend 		return -EMSGSIZE;
2255d83b0603SJohn Fastabend 
2256d83b0603SJohn Fastabend 	ndm = nlmsg_data(nlh);
2257d83b0603SJohn Fastabend 	ndm->ndm_family  = AF_BRIDGE;
2258d83b0603SJohn Fastabend 	ndm->ndm_pad1	 = 0;
2259d83b0603SJohn Fastabend 	ndm->ndm_pad2    = 0;
2260d83b0603SJohn Fastabend 	ndm->ndm_flags	 = flags;
2261d83b0603SJohn Fastabend 	ndm->ndm_type	 = 0;
2262d83b0603SJohn Fastabend 	ndm->ndm_ifindex = dev->ifindex;
2263d83b0603SJohn Fastabend 	ndm->ndm_state   = NUD_PERMANENT;
2264d83b0603SJohn Fastabend 
2265d83b0603SJohn Fastabend 	if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
2266d83b0603SJohn Fastabend 		goto nla_put_failure;
2267d83b0603SJohn Fastabend 
2268d83b0603SJohn Fastabend 	return nlmsg_end(skb, nlh);
2269d83b0603SJohn Fastabend 
2270d83b0603SJohn Fastabend nla_put_failure:
2271d83b0603SJohn Fastabend 	nlmsg_cancel(skb, nlh);
2272d83b0603SJohn Fastabend 	return -EMSGSIZE;
2273d83b0603SJohn Fastabend }
2274d83b0603SJohn Fastabend 
22753ff661c3SJohn Fastabend static inline size_t rtnl_fdb_nlmsg_size(void)
22763ff661c3SJohn Fastabend {
22773ff661c3SJohn Fastabend 	return NLMSG_ALIGN(sizeof(struct ndmsg)) + nla_total_size(ETH_ALEN);
22783ff661c3SJohn Fastabend }
22793ff661c3SJohn Fastabend 
22803ff661c3SJohn Fastabend static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, int type)
22813ff661c3SJohn Fastabend {
22823ff661c3SJohn Fastabend 	struct net *net = dev_net(dev);
22833ff661c3SJohn Fastabend 	struct sk_buff *skb;
22843ff661c3SJohn Fastabend 	int err = -ENOBUFS;
22853ff661c3SJohn Fastabend 
22863ff661c3SJohn Fastabend 	skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
22873ff661c3SJohn Fastabend 	if (!skb)
22883ff661c3SJohn Fastabend 		goto errout;
22893ff661c3SJohn Fastabend 
22901c104a6bSNicolas Dichtel 	err = nlmsg_populate_fdb_fill(skb, dev, addr, 0, 0, type, NTF_SELF, 0);
22913ff661c3SJohn Fastabend 	if (err < 0) {
22923ff661c3SJohn Fastabend 		kfree_skb(skb);
22933ff661c3SJohn Fastabend 		goto errout;
22943ff661c3SJohn Fastabend 	}
22953ff661c3SJohn Fastabend 
22963ff661c3SJohn Fastabend 	rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
22973ff661c3SJohn Fastabend 	return;
22983ff661c3SJohn Fastabend errout:
22993ff661c3SJohn Fastabend 	rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
23003ff661c3SJohn Fastabend }
23013ff661c3SJohn Fastabend 
2302090096bfSVlad Yasevich /**
2303090096bfSVlad Yasevich  * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
2304090096bfSVlad Yasevich  */
2305090096bfSVlad Yasevich int ndo_dflt_fdb_add(struct ndmsg *ndm,
2306090096bfSVlad Yasevich 		     struct nlattr *tb[],
2307090096bfSVlad Yasevich 		     struct net_device *dev,
2308090096bfSVlad Yasevich 		     const unsigned char *addr,
2309090096bfSVlad Yasevich 		     u16 flags)
2310090096bfSVlad Yasevich {
2311090096bfSVlad Yasevich 	int err = -EINVAL;
2312090096bfSVlad Yasevich 
2313090096bfSVlad Yasevich 	/* If aging addresses are supported device will need to
2314090096bfSVlad Yasevich 	 * implement its own handler for this.
2315090096bfSVlad Yasevich 	 */
2316090096bfSVlad Yasevich 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
2317090096bfSVlad Yasevich 		pr_info("%s: FDB only supports static addresses\n", dev->name);
2318090096bfSVlad Yasevich 		return err;
2319090096bfSVlad Yasevich 	}
2320090096bfSVlad Yasevich 
2321090096bfSVlad Yasevich 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2322090096bfSVlad Yasevich 		err = dev_uc_add_excl(dev, addr);
2323090096bfSVlad Yasevich 	else if (is_multicast_ether_addr(addr))
2324090096bfSVlad Yasevich 		err = dev_mc_add_excl(dev, addr);
2325090096bfSVlad Yasevich 
2326090096bfSVlad Yasevich 	/* Only return duplicate errors if NLM_F_EXCL is set */
2327090096bfSVlad Yasevich 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
2328090096bfSVlad Yasevich 		err = 0;
2329090096bfSVlad Yasevich 
2330090096bfSVlad Yasevich 	return err;
2331090096bfSVlad Yasevich }
2332090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_add);
2333090096bfSVlad Yasevich 
2334661d2967SThomas Graf static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh)
233577162022SJohn Fastabend {
233677162022SJohn Fastabend 	struct net *net = sock_net(skb->sk);
233777162022SJohn Fastabend 	struct ndmsg *ndm;
233877162022SJohn Fastabend 	struct nlattr *tb[NDA_MAX+1];
233977162022SJohn Fastabend 	struct net_device *dev;
234077162022SJohn Fastabend 	u8 *addr;
234177162022SJohn Fastabend 	int err;
234277162022SJohn Fastabend 
234377162022SJohn Fastabend 	err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
234477162022SJohn Fastabend 	if (err < 0)
234577162022SJohn Fastabend 		return err;
234677162022SJohn Fastabend 
234777162022SJohn Fastabend 	ndm = nlmsg_data(nlh);
234877162022SJohn Fastabend 	if (ndm->ndm_ifindex == 0) {
234977162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n");
235077162022SJohn Fastabend 		return -EINVAL;
235177162022SJohn Fastabend 	}
235277162022SJohn Fastabend 
235377162022SJohn Fastabend 	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
235477162022SJohn Fastabend 	if (dev == NULL) {
235577162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n");
235677162022SJohn Fastabend 		return -ENODEV;
235777162022SJohn Fastabend 	}
235877162022SJohn Fastabend 
235977162022SJohn Fastabend 	if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
236077162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n");
236177162022SJohn Fastabend 		return -EINVAL;
236277162022SJohn Fastabend 	}
236377162022SJohn Fastabend 
236477162022SJohn Fastabend 	addr = nla_data(tb[NDA_LLADDR]);
236577162022SJohn Fastabend 
236677162022SJohn Fastabend 	err = -EOPNOTSUPP;
236777162022SJohn Fastabend 
236877162022SJohn Fastabend 	/* Support fdb on master device the net/bridge default case */
236977162022SJohn Fastabend 	if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
237077162022SJohn Fastabend 	    (dev->priv_flags & IFF_BRIDGE_PORT)) {
2371898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2372898e5061SJiri Pirko 		const struct net_device_ops *ops = br_dev->netdev_ops;
2373898e5061SJiri Pirko 
2374898e5061SJiri Pirko 		err = ops->ndo_fdb_add(ndm, tb, dev, addr, nlh->nlmsg_flags);
237577162022SJohn Fastabend 		if (err)
237677162022SJohn Fastabend 			goto out;
237777162022SJohn Fastabend 		else
237877162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_MASTER;
237977162022SJohn Fastabend 	}
238077162022SJohn Fastabend 
238177162022SJohn Fastabend 	/* Embedded bridge, macvlan, and any other device support */
2382090096bfSVlad Yasevich 	if ((ndm->ndm_flags & NTF_SELF)) {
2383090096bfSVlad Yasevich 		if (dev->netdev_ops->ndo_fdb_add)
2384090096bfSVlad Yasevich 			err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
2385090096bfSVlad Yasevich 							   nlh->nlmsg_flags);
2386090096bfSVlad Yasevich 		else
2387090096bfSVlad Yasevich 			err = ndo_dflt_fdb_add(ndm, tb, dev, addr,
238877162022SJohn Fastabend 					       nlh->nlmsg_flags);
238977162022SJohn Fastabend 
23903ff661c3SJohn Fastabend 		if (!err) {
23913ff661c3SJohn Fastabend 			rtnl_fdb_notify(dev, addr, RTM_NEWNEIGH);
239277162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_SELF;
239377162022SJohn Fastabend 		}
23943ff661c3SJohn Fastabend 	}
239577162022SJohn Fastabend out:
239677162022SJohn Fastabend 	return err;
239777162022SJohn Fastabend }
239877162022SJohn Fastabend 
2399090096bfSVlad Yasevich /**
2400090096bfSVlad Yasevich  * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
2401090096bfSVlad Yasevich  */
2402090096bfSVlad Yasevich int ndo_dflt_fdb_del(struct ndmsg *ndm,
2403090096bfSVlad Yasevich 		     struct nlattr *tb[],
2404090096bfSVlad Yasevich 		     struct net_device *dev,
2405090096bfSVlad Yasevich 		     const unsigned char *addr)
2406090096bfSVlad Yasevich {
2407c8a89c4aSAlexander Duyck 	int err = -EINVAL;
2408090096bfSVlad Yasevich 
2409090096bfSVlad Yasevich 	/* If aging addresses are supported device will need to
2410090096bfSVlad Yasevich 	 * implement its own handler for this.
2411090096bfSVlad Yasevich 	 */
241264535993SSridhar Samudrala 	if (!(ndm->ndm_state & NUD_PERMANENT)) {
2413090096bfSVlad Yasevich 		pr_info("%s: FDB only supports static addresses\n", dev->name);
2414c8a89c4aSAlexander Duyck 		return err;
2415090096bfSVlad Yasevich 	}
2416090096bfSVlad Yasevich 
2417090096bfSVlad Yasevich 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2418090096bfSVlad Yasevich 		err = dev_uc_del(dev, addr);
2419090096bfSVlad Yasevich 	else if (is_multicast_ether_addr(addr))
2420090096bfSVlad Yasevich 		err = dev_mc_del(dev, addr);
2421090096bfSVlad Yasevich 
2422090096bfSVlad Yasevich 	return err;
2423090096bfSVlad Yasevich }
2424090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_del);
2425090096bfSVlad Yasevich 
2426661d2967SThomas Graf static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh)
242777162022SJohn Fastabend {
242877162022SJohn Fastabend 	struct net *net = sock_net(skb->sk);
242977162022SJohn Fastabend 	struct ndmsg *ndm;
24301690be63SVlad Yasevich 	struct nlattr *tb[NDA_MAX+1];
243177162022SJohn Fastabend 	struct net_device *dev;
243277162022SJohn Fastabend 	int err = -EINVAL;
243377162022SJohn Fastabend 	__u8 *addr;
243477162022SJohn Fastabend 
243590f62cf3SEric W. Biederman 	if (!netlink_capable(skb, CAP_NET_ADMIN))
24361690be63SVlad Yasevich 		return -EPERM;
24371690be63SVlad Yasevich 
24381690be63SVlad Yasevich 	err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
24391690be63SVlad Yasevich 	if (err < 0)
24401690be63SVlad Yasevich 		return err;
244177162022SJohn Fastabend 
244277162022SJohn Fastabend 	ndm = nlmsg_data(nlh);
244377162022SJohn Fastabend 	if (ndm->ndm_ifindex == 0) {
244477162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n");
244577162022SJohn Fastabend 		return -EINVAL;
244677162022SJohn Fastabend 	}
244777162022SJohn Fastabend 
244877162022SJohn Fastabend 	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
244977162022SJohn Fastabend 	if (dev == NULL) {
245077162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n");
245177162022SJohn Fastabend 		return -ENODEV;
245277162022SJohn Fastabend 	}
245377162022SJohn Fastabend 
24541690be63SVlad Yasevich 	if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
24551690be63SVlad Yasevich 		pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n");
245677162022SJohn Fastabend 		return -EINVAL;
245777162022SJohn Fastabend 	}
245877162022SJohn Fastabend 
24591690be63SVlad Yasevich 	addr = nla_data(tb[NDA_LLADDR]);
24601690be63SVlad Yasevich 
246177162022SJohn Fastabend 	err = -EOPNOTSUPP;
246277162022SJohn Fastabend 
246377162022SJohn Fastabend 	/* Support fdb on master device the net/bridge default case */
246477162022SJohn Fastabend 	if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
246577162022SJohn Fastabend 	    (dev->priv_flags & IFF_BRIDGE_PORT)) {
2466898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2467898e5061SJiri Pirko 		const struct net_device_ops *ops = br_dev->netdev_ops;
246877162022SJohn Fastabend 
2469898e5061SJiri Pirko 		if (ops->ndo_fdb_del)
24701690be63SVlad Yasevich 			err = ops->ndo_fdb_del(ndm, tb, dev, addr);
247177162022SJohn Fastabend 
247277162022SJohn Fastabend 		if (err)
247377162022SJohn Fastabend 			goto out;
247477162022SJohn Fastabend 		else
247577162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_MASTER;
247677162022SJohn Fastabend 	}
247777162022SJohn Fastabend 
247877162022SJohn Fastabend 	/* Embedded bridge, macvlan, and any other device support */
2479090096bfSVlad Yasevich 	if (ndm->ndm_flags & NTF_SELF) {
2480090096bfSVlad Yasevich 		if (dev->netdev_ops->ndo_fdb_del)
24811690be63SVlad Yasevich 			err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr);
2482090096bfSVlad Yasevich 		else
2483090096bfSVlad Yasevich 			err = ndo_dflt_fdb_del(ndm, tb, dev, addr);
248477162022SJohn Fastabend 
24853ff661c3SJohn Fastabend 		if (!err) {
24863ff661c3SJohn Fastabend 			rtnl_fdb_notify(dev, addr, RTM_DELNEIGH);
248777162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_SELF;
248877162022SJohn Fastabend 		}
24893ff661c3SJohn Fastabend 	}
249077162022SJohn Fastabend out:
249177162022SJohn Fastabend 	return err;
249277162022SJohn Fastabend }
249377162022SJohn Fastabend 
2494d83b0603SJohn Fastabend static int nlmsg_populate_fdb(struct sk_buff *skb,
2495d83b0603SJohn Fastabend 			      struct netlink_callback *cb,
2496d83b0603SJohn Fastabend 			      struct net_device *dev,
2497d83b0603SJohn Fastabend 			      int *idx,
2498d83b0603SJohn Fastabend 			      struct netdev_hw_addr_list *list)
2499d83b0603SJohn Fastabend {
2500d83b0603SJohn Fastabend 	struct netdev_hw_addr *ha;
2501d83b0603SJohn Fastabend 	int err;
250215e47304SEric W. Biederman 	u32 portid, seq;
2503d83b0603SJohn Fastabend 
250415e47304SEric W. Biederman 	portid = NETLINK_CB(cb->skb).portid;
2505d83b0603SJohn Fastabend 	seq = cb->nlh->nlmsg_seq;
2506d83b0603SJohn Fastabend 
2507d83b0603SJohn Fastabend 	list_for_each_entry(ha, &list->list, list) {
2508d83b0603SJohn Fastabend 		if (*idx < cb->args[0])
2509d83b0603SJohn Fastabend 			goto skip;
2510d83b0603SJohn Fastabend 
2511d83b0603SJohn Fastabend 		err = nlmsg_populate_fdb_fill(skb, dev, ha->addr,
2512a7a558feSJohn Fastabend 					      portid, seq,
25131c104a6bSNicolas Dichtel 					      RTM_NEWNEIGH, NTF_SELF,
25141c104a6bSNicolas Dichtel 					      NLM_F_MULTI);
2515d83b0603SJohn Fastabend 		if (err < 0)
2516d83b0603SJohn Fastabend 			return err;
2517d83b0603SJohn Fastabend skip:
2518d83b0603SJohn Fastabend 		*idx += 1;
2519d83b0603SJohn Fastabend 	}
2520d83b0603SJohn Fastabend 	return 0;
2521d83b0603SJohn Fastabend }
2522d83b0603SJohn Fastabend 
2523d83b0603SJohn Fastabend /**
25242c53040fSBen Hutchings  * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
2525d83b0603SJohn Fastabend  * @nlh: netlink message header
2526d83b0603SJohn Fastabend  * @dev: netdevice
2527d83b0603SJohn Fastabend  *
2528d83b0603SJohn Fastabend  * Default netdevice operation to dump the existing unicast address list.
252991f3e7b1SJohn Fastabend  * Returns number of addresses from list put in skb.
2530d83b0603SJohn Fastabend  */
2531d83b0603SJohn Fastabend int ndo_dflt_fdb_dump(struct sk_buff *skb,
2532d83b0603SJohn Fastabend 		      struct netlink_callback *cb,
2533d83b0603SJohn Fastabend 		      struct net_device *dev,
25345d5eacb3SJamal Hadi Salim 		      struct net_device *filter_dev,
2535d83b0603SJohn Fastabend 		      int idx)
2536d83b0603SJohn Fastabend {
2537d83b0603SJohn Fastabend 	int err;
2538d83b0603SJohn Fastabend 
2539d83b0603SJohn Fastabend 	netif_addr_lock_bh(dev);
2540d83b0603SJohn Fastabend 	err = nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->uc);
2541d83b0603SJohn Fastabend 	if (err)
2542d83b0603SJohn Fastabend 		goto out;
2543d83b0603SJohn Fastabend 	nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->mc);
2544d83b0603SJohn Fastabend out:
2545d83b0603SJohn Fastabend 	netif_addr_unlock_bh(dev);
2546d83b0603SJohn Fastabend 	return idx;
2547d83b0603SJohn Fastabend }
2548d83b0603SJohn Fastabend EXPORT_SYMBOL(ndo_dflt_fdb_dump);
2549d83b0603SJohn Fastabend 
255077162022SJohn Fastabend static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
255177162022SJohn Fastabend {
255277162022SJohn Fastabend 	struct net_device *dev;
25535e6d2435SJamal Hadi Salim 	struct nlattr *tb[IFLA_MAX+1];
25545e6d2435SJamal Hadi Salim 	struct net_device *bdev = NULL;
25555e6d2435SJamal Hadi Salim 	struct net_device *br_dev = NULL;
25565e6d2435SJamal Hadi Salim 	const struct net_device_ops *ops = NULL;
25575e6d2435SJamal Hadi Salim 	const struct net_device_ops *cops = NULL;
25585e6d2435SJamal Hadi Salim 	struct ifinfomsg *ifm = nlmsg_data(cb->nlh);
25595e6d2435SJamal Hadi Salim 	struct net *net = sock_net(skb->sk);
25605e6d2435SJamal Hadi Salim 	int brport_idx = 0;
25615e6d2435SJamal Hadi Salim 	int br_idx = 0;
25625e6d2435SJamal Hadi Salim 	int idx = 0;
256377162022SJohn Fastabend 
25645e6d2435SJamal Hadi Salim 	if (nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX,
25655e6d2435SJamal Hadi Salim 			ifla_policy) == 0) {
25665e6d2435SJamal Hadi Salim 		if (tb[IFLA_MASTER])
25675e6d2435SJamal Hadi Salim 			br_idx = nla_get_u32(tb[IFLA_MASTER]);
25685e6d2435SJamal Hadi Salim 	}
256977162022SJohn Fastabend 
25705e6d2435SJamal Hadi Salim 	brport_idx = ifm->ifi_index;
25715e6d2435SJamal Hadi Salim 
25725e6d2435SJamal Hadi Salim 	if (br_idx) {
25735e6d2435SJamal Hadi Salim 		br_dev = __dev_get_by_index(net, br_idx);
25745e6d2435SJamal Hadi Salim 		if (!br_dev)
25755e6d2435SJamal Hadi Salim 			return -ENODEV;
25765e6d2435SJamal Hadi Salim 
2577898e5061SJiri Pirko 		ops = br_dev->netdev_ops;
25785e6d2435SJamal Hadi Salim 		bdev = br_dev;
25795e6d2435SJamal Hadi Salim 	}
25805e6d2435SJamal Hadi Salim 
25815e6d2435SJamal Hadi Salim 	for_each_netdev(net, dev) {
25825e6d2435SJamal Hadi Salim 		if (brport_idx && (dev->ifindex != brport_idx))
25835e6d2435SJamal Hadi Salim 			continue;
25845e6d2435SJamal Hadi Salim 
25855e6d2435SJamal Hadi Salim 		if (!br_idx) { /* user did not specify a specific bridge */
25865e6d2435SJamal Hadi Salim 			if (dev->priv_flags & IFF_BRIDGE_PORT) {
25875e6d2435SJamal Hadi Salim 				br_dev = netdev_master_upper_dev_get(dev);
25885e6d2435SJamal Hadi Salim 				cops = br_dev->netdev_ops;
25895e6d2435SJamal Hadi Salim 			}
25905e6d2435SJamal Hadi Salim 
25915e6d2435SJamal Hadi Salim 			bdev = dev;
25925e6d2435SJamal Hadi Salim 		} else {
25935e6d2435SJamal Hadi Salim 			if (dev != br_dev &&
25945e6d2435SJamal Hadi Salim 			    !(dev->priv_flags & IFF_BRIDGE_PORT))
25955e6d2435SJamal Hadi Salim 				continue;
25965e6d2435SJamal Hadi Salim 
25975e6d2435SJamal Hadi Salim 			if (br_dev != netdev_master_upper_dev_get(dev) &&
25985e6d2435SJamal Hadi Salim 			    !(dev->priv_flags & IFF_EBRIDGE))
25995e6d2435SJamal Hadi Salim 				continue;
26005e6d2435SJamal Hadi Salim 
26015e6d2435SJamal Hadi Salim 			bdev = br_dev;
26025e6d2435SJamal Hadi Salim 			cops = ops;
26035e6d2435SJamal Hadi Salim 		}
26045e6d2435SJamal Hadi Salim 
26055e6d2435SJamal Hadi Salim 		if (dev->priv_flags & IFF_BRIDGE_PORT) {
26065e6d2435SJamal Hadi Salim 			if (cops && cops->ndo_fdb_dump)
26075e6d2435SJamal Hadi Salim 				idx = cops->ndo_fdb_dump(skb, cb, br_dev, dev,
26085d5eacb3SJamal Hadi Salim 							 idx);
260977162022SJohn Fastabend 		}
261077162022SJohn Fastabend 
26115d5eacb3SJamal Hadi Salim 		idx = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx);
26125e6d2435SJamal Hadi Salim 		if (dev->netdev_ops->ndo_fdb_dump)
26135e6d2435SJamal Hadi Salim 			idx = dev->netdev_ops->ndo_fdb_dump(skb, cb, bdev, dev,
26145e6d2435SJamal Hadi Salim 							    idx);
26155e6d2435SJamal Hadi Salim 
26165e6d2435SJamal Hadi Salim 		cops = NULL;
261777162022SJohn Fastabend 	}
261877162022SJohn Fastabend 
261977162022SJohn Fastabend 	cb->args[0] = idx;
262077162022SJohn Fastabend 	return skb->len;
262177162022SJohn Fastabend }
262277162022SJohn Fastabend 
2623815cccbfSJohn Fastabend int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
2624815cccbfSJohn Fastabend 			    struct net_device *dev, u16 mode)
2625815cccbfSJohn Fastabend {
2626815cccbfSJohn Fastabend 	struct nlmsghdr *nlh;
2627815cccbfSJohn Fastabend 	struct ifinfomsg *ifm;
2628815cccbfSJohn Fastabend 	struct nlattr *br_afspec;
2629815cccbfSJohn Fastabend 	u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
2630898e5061SJiri Pirko 	struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2631815cccbfSJohn Fastabend 
2632815cccbfSJohn Fastabend 	nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), NLM_F_MULTI);
2633815cccbfSJohn Fastabend 	if (nlh == NULL)
2634815cccbfSJohn Fastabend 		return -EMSGSIZE;
2635815cccbfSJohn Fastabend 
2636815cccbfSJohn Fastabend 	ifm = nlmsg_data(nlh);
2637815cccbfSJohn Fastabend 	ifm->ifi_family = AF_BRIDGE;
2638815cccbfSJohn Fastabend 	ifm->__ifi_pad = 0;
2639815cccbfSJohn Fastabend 	ifm->ifi_type = dev->type;
2640815cccbfSJohn Fastabend 	ifm->ifi_index = dev->ifindex;
2641815cccbfSJohn Fastabend 	ifm->ifi_flags = dev_get_flags(dev);
2642815cccbfSJohn Fastabend 	ifm->ifi_change = 0;
2643815cccbfSJohn Fastabend 
2644815cccbfSJohn Fastabend 
2645815cccbfSJohn Fastabend 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
2646815cccbfSJohn Fastabend 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
2647815cccbfSJohn Fastabend 	    nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
2648898e5061SJiri Pirko 	    (br_dev &&
2649898e5061SJiri Pirko 	     nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
2650815cccbfSJohn Fastabend 	    (dev->addr_len &&
2651815cccbfSJohn Fastabend 	     nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
2652815cccbfSJohn Fastabend 	    (dev->ifindex != dev->iflink &&
2653815cccbfSJohn Fastabend 	     nla_put_u32(skb, IFLA_LINK, dev->iflink)))
2654815cccbfSJohn Fastabend 		goto nla_put_failure;
2655815cccbfSJohn Fastabend 
2656815cccbfSJohn Fastabend 	br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
2657815cccbfSJohn Fastabend 	if (!br_afspec)
2658815cccbfSJohn Fastabend 		goto nla_put_failure;
2659815cccbfSJohn Fastabend 
2660815cccbfSJohn Fastabend 	if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF) ||
2661815cccbfSJohn Fastabend 	    nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
2662815cccbfSJohn Fastabend 		nla_nest_cancel(skb, br_afspec);
2663815cccbfSJohn Fastabend 		goto nla_put_failure;
2664815cccbfSJohn Fastabend 	}
2665815cccbfSJohn Fastabend 	nla_nest_end(skb, br_afspec);
2666815cccbfSJohn Fastabend 
2667815cccbfSJohn Fastabend 	return nlmsg_end(skb, nlh);
2668815cccbfSJohn Fastabend nla_put_failure:
2669815cccbfSJohn Fastabend 	nlmsg_cancel(skb, nlh);
2670815cccbfSJohn Fastabend 	return -EMSGSIZE;
2671815cccbfSJohn Fastabend }
2672815cccbfSJohn Fastabend EXPORT_SYMBOL(ndo_dflt_bridge_getlink);
2673815cccbfSJohn Fastabend 
2674e5a55a89SJohn Fastabend static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
2675e5a55a89SJohn Fastabend {
2676e5a55a89SJohn Fastabend 	struct net *net = sock_net(skb->sk);
2677e5a55a89SJohn Fastabend 	struct net_device *dev;
2678e5a55a89SJohn Fastabend 	int idx = 0;
2679e5a55a89SJohn Fastabend 	u32 portid = NETLINK_CB(cb->skb).portid;
2680e5a55a89SJohn Fastabend 	u32 seq = cb->nlh->nlmsg_seq;
26816cbdceebSVlad Yasevich 	struct nlattr *extfilt;
26826cbdceebSVlad Yasevich 	u32 filter_mask = 0;
26836cbdceebSVlad Yasevich 
26843e805ad2SAsbjoern Sloth Toennesen 	extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
26856cbdceebSVlad Yasevich 				  IFLA_EXT_MASK);
26866cbdceebSVlad Yasevich 	if (extfilt)
26876cbdceebSVlad Yasevich 		filter_mask = nla_get_u32(extfilt);
2688e5a55a89SJohn Fastabend 
2689e5a55a89SJohn Fastabend 	rcu_read_lock();
2690e5a55a89SJohn Fastabend 	for_each_netdev_rcu(net, dev) {
2691e5a55a89SJohn Fastabend 		const struct net_device_ops *ops = dev->netdev_ops;
2692898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2693e5a55a89SJohn Fastabend 
2694898e5061SJiri Pirko 		if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
269525b1e679SBen Hutchings 			if (idx >= cb->args[0] &&
2696898e5061SJiri Pirko 			    br_dev->netdev_ops->ndo_bridge_getlink(
26976cbdceebSVlad Yasevich 				    skb, portid, seq, dev, filter_mask) < 0)
2698e5a55a89SJohn Fastabend 				break;
2699e5a55a89SJohn Fastabend 			idx++;
2700e5a55a89SJohn Fastabend 		}
2701e5a55a89SJohn Fastabend 
2702e5a55a89SJohn Fastabend 		if (ops->ndo_bridge_getlink) {
270325b1e679SBen Hutchings 			if (idx >= cb->args[0] &&
27046cbdceebSVlad Yasevich 			    ops->ndo_bridge_getlink(skb, portid, seq, dev,
27056cbdceebSVlad Yasevich 						    filter_mask) < 0)
2706e5a55a89SJohn Fastabend 				break;
2707e5a55a89SJohn Fastabend 			idx++;
2708e5a55a89SJohn Fastabend 		}
2709e5a55a89SJohn Fastabend 	}
2710e5a55a89SJohn Fastabend 	rcu_read_unlock();
2711e5a55a89SJohn Fastabend 	cb->args[0] = idx;
2712e5a55a89SJohn Fastabend 
2713e5a55a89SJohn Fastabend 	return skb->len;
2714e5a55a89SJohn Fastabend }
2715e5a55a89SJohn Fastabend 
27162469ffd7SJohn Fastabend static inline size_t bridge_nlmsg_size(void)
27172469ffd7SJohn Fastabend {
27182469ffd7SJohn Fastabend 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
27192469ffd7SJohn Fastabend 		+ nla_total_size(IFNAMSIZ)	/* IFLA_IFNAME */
27202469ffd7SJohn Fastabend 		+ nla_total_size(MAX_ADDR_LEN)	/* IFLA_ADDRESS */
27212469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_MASTER */
27222469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_MTU */
27232469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_LINK */
27242469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_OPERSTATE */
27252469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u8))	/* IFLA_PROTINFO */
27262469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(struct nlattr))	/* IFLA_AF_SPEC */
27272469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u16))	/* IFLA_BRIDGE_FLAGS */
27282469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u16));	/* IFLA_BRIDGE_MODE */
27292469ffd7SJohn Fastabend }
27302469ffd7SJohn Fastabend 
27312469ffd7SJohn Fastabend static int rtnl_bridge_notify(struct net_device *dev, u16 flags)
27322469ffd7SJohn Fastabend {
27332469ffd7SJohn Fastabend 	struct net *net = dev_net(dev);
2734898e5061SJiri Pirko 	struct net_device *br_dev = netdev_master_upper_dev_get(dev);
27352469ffd7SJohn Fastabend 	struct sk_buff *skb;
27362469ffd7SJohn Fastabend 	int err = -EOPNOTSUPP;
27372469ffd7SJohn Fastabend 
27382469ffd7SJohn Fastabend 	skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
27392469ffd7SJohn Fastabend 	if (!skb) {
27402469ffd7SJohn Fastabend 		err = -ENOMEM;
27412469ffd7SJohn Fastabend 		goto errout;
27422469ffd7SJohn Fastabend 	}
27432469ffd7SJohn Fastabend 
2744c38e01b8SJohn Fastabend 	if ((!flags || (flags & BRIDGE_FLAGS_MASTER)) &&
2745898e5061SJiri Pirko 	    br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
27466cbdceebSVlad Yasevich 		err = br_dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0);
27472469ffd7SJohn Fastabend 		if (err < 0)
27482469ffd7SJohn Fastabend 			goto errout;
2749c38e01b8SJohn Fastabend 	}
2750c38e01b8SJohn Fastabend 
2751c38e01b8SJohn Fastabend 	if ((flags & BRIDGE_FLAGS_SELF) &&
2752c38e01b8SJohn Fastabend 	    dev->netdev_ops->ndo_bridge_getlink) {
27536cbdceebSVlad Yasevich 		err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0);
2754c38e01b8SJohn Fastabend 		if (err < 0)
2755c38e01b8SJohn Fastabend 			goto errout;
2756c38e01b8SJohn Fastabend 	}
27572469ffd7SJohn Fastabend 
27582469ffd7SJohn Fastabend 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
27592469ffd7SJohn Fastabend 	return 0;
27602469ffd7SJohn Fastabend errout:
27612469ffd7SJohn Fastabend 	WARN_ON(err == -EMSGSIZE);
27622469ffd7SJohn Fastabend 	kfree_skb(skb);
27632469ffd7SJohn Fastabend 	rtnl_set_sk_err(net, RTNLGRP_LINK, err);
27642469ffd7SJohn Fastabend 	return err;
27652469ffd7SJohn Fastabend }
27662469ffd7SJohn Fastabend 
2767661d2967SThomas Graf static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
2768e5a55a89SJohn Fastabend {
2769e5a55a89SJohn Fastabend 	struct net *net = sock_net(skb->sk);
2770e5a55a89SJohn Fastabend 	struct ifinfomsg *ifm;
2771e5a55a89SJohn Fastabend 	struct net_device *dev;
27722469ffd7SJohn Fastabend 	struct nlattr *br_spec, *attr = NULL;
27732469ffd7SJohn Fastabend 	int rem, err = -EOPNOTSUPP;
2774c38e01b8SJohn Fastabend 	u16 oflags, flags = 0;
2775c38e01b8SJohn Fastabend 	bool have_flags = false;
2776e5a55a89SJohn Fastabend 
2777e5a55a89SJohn Fastabend 	if (nlmsg_len(nlh) < sizeof(*ifm))
2778e5a55a89SJohn Fastabend 		return -EINVAL;
2779e5a55a89SJohn Fastabend 
2780e5a55a89SJohn Fastabend 	ifm = nlmsg_data(nlh);
2781e5a55a89SJohn Fastabend 	if (ifm->ifi_family != AF_BRIDGE)
2782e5a55a89SJohn Fastabend 		return -EPFNOSUPPORT;
2783e5a55a89SJohn Fastabend 
2784e5a55a89SJohn Fastabend 	dev = __dev_get_by_index(net, ifm->ifi_index);
2785e5a55a89SJohn Fastabend 	if (!dev) {
2786e5a55a89SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
2787e5a55a89SJohn Fastabend 		return -ENODEV;
2788e5a55a89SJohn Fastabend 	}
2789e5a55a89SJohn Fastabend 
27902469ffd7SJohn Fastabend 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
27912469ffd7SJohn Fastabend 	if (br_spec) {
27922469ffd7SJohn Fastabend 		nla_for_each_nested(attr, br_spec, rem) {
27932469ffd7SJohn Fastabend 			if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
2794c38e01b8SJohn Fastabend 				have_flags = true;
27952469ffd7SJohn Fastabend 				flags = nla_get_u16(attr);
27962469ffd7SJohn Fastabend 				break;
27972469ffd7SJohn Fastabend 			}
27982469ffd7SJohn Fastabend 		}
27992469ffd7SJohn Fastabend 	}
28002469ffd7SJohn Fastabend 
2801c38e01b8SJohn Fastabend 	oflags = flags;
2802c38e01b8SJohn Fastabend 
28032469ffd7SJohn Fastabend 	if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
2804898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2805898e5061SJiri Pirko 
2806898e5061SJiri Pirko 		if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
28072469ffd7SJohn Fastabend 			err = -EOPNOTSUPP;
2808e5a55a89SJohn Fastabend 			goto out;
2809e5a55a89SJohn Fastabend 		}
2810e5a55a89SJohn Fastabend 
2811898e5061SJiri Pirko 		err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh);
28122469ffd7SJohn Fastabend 		if (err)
28132469ffd7SJohn Fastabend 			goto out;
28142469ffd7SJohn Fastabend 
28152469ffd7SJohn Fastabend 		flags &= ~BRIDGE_FLAGS_MASTER;
28162469ffd7SJohn Fastabend 	}
28172469ffd7SJohn Fastabend 
28182469ffd7SJohn Fastabend 	if ((flags & BRIDGE_FLAGS_SELF)) {
28192469ffd7SJohn Fastabend 		if (!dev->netdev_ops->ndo_bridge_setlink)
28202469ffd7SJohn Fastabend 			err = -EOPNOTSUPP;
28212469ffd7SJohn Fastabend 		else
2822e5a55a89SJohn Fastabend 			err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh);
2823e5a55a89SJohn Fastabend 
28242469ffd7SJohn Fastabend 		if (!err)
28252469ffd7SJohn Fastabend 			flags &= ~BRIDGE_FLAGS_SELF;
28262469ffd7SJohn Fastabend 	}
28272469ffd7SJohn Fastabend 
2828c38e01b8SJohn Fastabend 	if (have_flags)
28292469ffd7SJohn Fastabend 		memcpy(nla_data(attr), &flags, sizeof(flags));
28302469ffd7SJohn Fastabend 	/* Generate event to notify upper layer of bridge change */
28312469ffd7SJohn Fastabend 	if (!err)
2832c38e01b8SJohn Fastabend 		err = rtnl_bridge_notify(dev, oflags);
2833e5a55a89SJohn Fastabend out:
2834e5a55a89SJohn Fastabend 	return err;
2835e5a55a89SJohn Fastabend }
2836e5a55a89SJohn Fastabend 
2837661d2967SThomas Graf static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
2838407af329SVlad Yasevich {
2839407af329SVlad Yasevich 	struct net *net = sock_net(skb->sk);
2840407af329SVlad Yasevich 	struct ifinfomsg *ifm;
2841407af329SVlad Yasevich 	struct net_device *dev;
2842407af329SVlad Yasevich 	struct nlattr *br_spec, *attr = NULL;
2843407af329SVlad Yasevich 	int rem, err = -EOPNOTSUPP;
2844407af329SVlad Yasevich 	u16 oflags, flags = 0;
2845407af329SVlad Yasevich 	bool have_flags = false;
2846407af329SVlad Yasevich 
2847407af329SVlad Yasevich 	if (nlmsg_len(nlh) < sizeof(*ifm))
2848407af329SVlad Yasevich 		return -EINVAL;
2849407af329SVlad Yasevich 
2850407af329SVlad Yasevich 	ifm = nlmsg_data(nlh);
2851407af329SVlad Yasevich 	if (ifm->ifi_family != AF_BRIDGE)
2852407af329SVlad Yasevich 		return -EPFNOSUPPORT;
2853407af329SVlad Yasevich 
2854407af329SVlad Yasevich 	dev = __dev_get_by_index(net, ifm->ifi_index);
2855407af329SVlad Yasevich 	if (!dev) {
2856407af329SVlad Yasevich 		pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
2857407af329SVlad Yasevich 		return -ENODEV;
2858407af329SVlad Yasevich 	}
2859407af329SVlad Yasevich 
2860407af329SVlad Yasevich 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
2861407af329SVlad Yasevich 	if (br_spec) {
2862407af329SVlad Yasevich 		nla_for_each_nested(attr, br_spec, rem) {
2863407af329SVlad Yasevich 			if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
2864407af329SVlad Yasevich 				have_flags = true;
2865407af329SVlad Yasevich 				flags = nla_get_u16(attr);
2866407af329SVlad Yasevich 				break;
2867407af329SVlad Yasevich 			}
2868407af329SVlad Yasevich 		}
2869407af329SVlad Yasevich 	}
2870407af329SVlad Yasevich 
2871407af329SVlad Yasevich 	oflags = flags;
2872407af329SVlad Yasevich 
2873407af329SVlad Yasevich 	if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
2874407af329SVlad Yasevich 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2875407af329SVlad Yasevich 
2876407af329SVlad Yasevich 		if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
2877407af329SVlad Yasevich 			err = -EOPNOTSUPP;
2878407af329SVlad Yasevich 			goto out;
2879407af329SVlad Yasevich 		}
2880407af329SVlad Yasevich 
2881407af329SVlad Yasevich 		err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh);
2882407af329SVlad Yasevich 		if (err)
2883407af329SVlad Yasevich 			goto out;
2884407af329SVlad Yasevich 
2885407af329SVlad Yasevich 		flags &= ~BRIDGE_FLAGS_MASTER;
2886407af329SVlad Yasevich 	}
2887407af329SVlad Yasevich 
2888407af329SVlad Yasevich 	if ((flags & BRIDGE_FLAGS_SELF)) {
2889407af329SVlad Yasevich 		if (!dev->netdev_ops->ndo_bridge_dellink)
2890407af329SVlad Yasevich 			err = -EOPNOTSUPP;
2891407af329SVlad Yasevich 		else
2892407af329SVlad Yasevich 			err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh);
2893407af329SVlad Yasevich 
2894407af329SVlad Yasevich 		if (!err)
2895407af329SVlad Yasevich 			flags &= ~BRIDGE_FLAGS_SELF;
2896407af329SVlad Yasevich 	}
2897407af329SVlad Yasevich 
2898407af329SVlad Yasevich 	if (have_flags)
2899407af329SVlad Yasevich 		memcpy(nla_data(attr), &flags, sizeof(flags));
2900407af329SVlad Yasevich 	/* Generate event to notify upper layer of bridge change */
2901407af329SVlad Yasevich 	if (!err)
2902407af329SVlad Yasevich 		err = rtnl_bridge_notify(dev, oflags);
2903407af329SVlad Yasevich out:
2904407af329SVlad Yasevich 	return err;
2905407af329SVlad Yasevich }
2906407af329SVlad Yasevich 
29071da177e4SLinus Torvalds /* Process one rtnetlink message. */
29081da177e4SLinus Torvalds 
29091d00a4ebSThomas Graf static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
29101da177e4SLinus Torvalds {
29113b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
2912e2849863SThomas Graf 	rtnl_doit_func doit;
29131da177e4SLinus Torvalds 	int sz_idx, kind;
29141da177e4SLinus Torvalds 	int family;
29151da177e4SLinus Torvalds 	int type;
29162907c35fSEric Dumazet 	int err;
29171da177e4SLinus Torvalds 
29181da177e4SLinus Torvalds 	type = nlh->nlmsg_type;
29191da177e4SLinus Torvalds 	if (type > RTM_MAX)
2920038890feSThomas Graf 		return -EOPNOTSUPP;
29211da177e4SLinus Torvalds 
29221da177e4SLinus Torvalds 	type -= RTM_BASE;
29231da177e4SLinus Torvalds 
29241da177e4SLinus Torvalds 	/* All the messages must have at least 1 byte length */
2925573ce260SHong zhi guo 	if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
29261da177e4SLinus Torvalds 		return 0;
29271da177e4SLinus Torvalds 
2928573ce260SHong zhi guo 	family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
29291da177e4SLinus Torvalds 	sz_idx = type>>2;
29301da177e4SLinus Torvalds 	kind = type&3;
29311da177e4SLinus Torvalds 
293290f62cf3SEric W. Biederman 	if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
29331d00a4ebSThomas Graf 		return -EPERM;
29341da177e4SLinus Torvalds 
2935b8f3ab42SDavid S. Miller 	if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
293697c53cacSDenis V. Lunev 		struct sock *rtnl;
2937e2849863SThomas Graf 		rtnl_dumpit_func dumpit;
2938c7ac8679SGreg Rose 		rtnl_calcit_func calcit;
2939c7ac8679SGreg Rose 		u16 min_dump_alloc = 0;
29401da177e4SLinus Torvalds 
2941e2849863SThomas Graf 		dumpit = rtnl_get_dumpit(family, type);
2942e2849863SThomas Graf 		if (dumpit == NULL)
2943038890feSThomas Graf 			return -EOPNOTSUPP;
2944c7ac8679SGreg Rose 		calcit = rtnl_get_calcit(family, type);
2945c7ac8679SGreg Rose 		if (calcit)
2946115c9b81SGreg Rose 			min_dump_alloc = calcit(skb, nlh);
29471da177e4SLinus Torvalds 
29482907c35fSEric Dumazet 		__rtnl_unlock();
294997c53cacSDenis V. Lunev 		rtnl = net->rtnl;
295080d326faSPablo Neira Ayuso 		{
295180d326faSPablo Neira Ayuso 			struct netlink_dump_control c = {
295280d326faSPablo Neira Ayuso 				.dump		= dumpit,
295380d326faSPablo Neira Ayuso 				.min_dump_alloc	= min_dump_alloc,
295480d326faSPablo Neira Ayuso 			};
295580d326faSPablo Neira Ayuso 			err = netlink_dump_start(rtnl, skb, nlh, &c);
295680d326faSPablo Neira Ayuso 		}
29572907c35fSEric Dumazet 		rtnl_lock();
29582907c35fSEric Dumazet 		return err;
29591da177e4SLinus Torvalds 	}
29601da177e4SLinus Torvalds 
2961e2849863SThomas Graf 	doit = rtnl_get_doit(family, type);
2962e2849863SThomas Graf 	if (doit == NULL)
2963038890feSThomas Graf 		return -EOPNOTSUPP;
29641da177e4SLinus Torvalds 
2965661d2967SThomas Graf 	return doit(skb, nlh);
29661da177e4SLinus Torvalds }
29671da177e4SLinus Torvalds 
2968cd40b7d3SDenis V. Lunev static void rtnetlink_rcv(struct sk_buff *skb)
29691da177e4SLinus Torvalds {
29701536cc0dSDenis V. Lunev 	rtnl_lock();
2971cd40b7d3SDenis V. Lunev 	netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
29721536cc0dSDenis V. Lunev 	rtnl_unlock();
29731da177e4SLinus Torvalds }
29741da177e4SLinus Torvalds 
29751da177e4SLinus Torvalds static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
29761da177e4SLinus Torvalds {
2977351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2978e9dc8653SEric W. Biederman 
29791da177e4SLinus Torvalds 	switch (event) {
29801da177e4SLinus Torvalds 	case NETDEV_UP:
29811da177e4SLinus Torvalds 	case NETDEV_DOWN:
298210de05afSPatrick McHardy 	case NETDEV_PRE_UP:
2983d90a909eSEric W. Biederman 	case NETDEV_POST_INIT:
2984d90a909eSEric W. Biederman 	case NETDEV_REGISTER:
29851da177e4SLinus Torvalds 	case NETDEV_CHANGE:
2986755d0e77SPatrick McHardy 	case NETDEV_PRE_TYPE_CHANGE:
29871da177e4SLinus Torvalds 	case NETDEV_GOING_DOWN:
2988a2835763SPatrick McHardy 	case NETDEV_UNREGISTER:
29890115e8e3SEric Dumazet 	case NETDEV_UNREGISTER_FINAL:
2990ac3d3f81SAmerigo Wang 	case NETDEV_RELEASE:
2991ac3d3f81SAmerigo Wang 	case NETDEV_JOIN:
29921da177e4SLinus Torvalds 		break;
29931da177e4SLinus Torvalds 	default:
29947f294054SAlexei Starovoitov 		rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
29951da177e4SLinus Torvalds 		break;
29961da177e4SLinus Torvalds 	}
29971da177e4SLinus Torvalds 	return NOTIFY_DONE;
29981da177e4SLinus Torvalds }
29991da177e4SLinus Torvalds 
30001da177e4SLinus Torvalds static struct notifier_block rtnetlink_dev_notifier = {
30011da177e4SLinus Torvalds 	.notifier_call	= rtnetlink_event,
30021da177e4SLinus Torvalds };
30031da177e4SLinus Torvalds 
300497c53cacSDenis V. Lunev 
30052c8c1e72SAlexey Dobriyan static int __net_init rtnetlink_net_init(struct net *net)
300697c53cacSDenis V. Lunev {
300797c53cacSDenis V. Lunev 	struct sock *sk;
3008a31f2d17SPablo Neira Ayuso 	struct netlink_kernel_cfg cfg = {
3009a31f2d17SPablo Neira Ayuso 		.groups		= RTNLGRP_MAX,
3010a31f2d17SPablo Neira Ayuso 		.input		= rtnetlink_rcv,
3011a31f2d17SPablo Neira Ayuso 		.cb_mutex	= &rtnl_mutex,
30129785e10aSPablo Neira Ayuso 		.flags		= NL_CFG_F_NONROOT_RECV,
3013a31f2d17SPablo Neira Ayuso 	};
3014a31f2d17SPablo Neira Ayuso 
30159f00d977SPablo Neira Ayuso 	sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
301697c53cacSDenis V. Lunev 	if (!sk)
301797c53cacSDenis V. Lunev 		return -ENOMEM;
301897c53cacSDenis V. Lunev 	net->rtnl = sk;
301997c53cacSDenis V. Lunev 	return 0;
302097c53cacSDenis V. Lunev }
302197c53cacSDenis V. Lunev 
30222c8c1e72SAlexey Dobriyan static void __net_exit rtnetlink_net_exit(struct net *net)
302397c53cacSDenis V. Lunev {
3024b7c6ba6eSDenis V. Lunev 	netlink_kernel_release(net->rtnl);
302597c53cacSDenis V. Lunev 	net->rtnl = NULL;
302697c53cacSDenis V. Lunev }
302797c53cacSDenis V. Lunev 
302897c53cacSDenis V. Lunev static struct pernet_operations rtnetlink_net_ops = {
302997c53cacSDenis V. Lunev 	.init = rtnetlink_net_init,
303097c53cacSDenis V. Lunev 	.exit = rtnetlink_net_exit,
303197c53cacSDenis V. Lunev };
303297c53cacSDenis V. Lunev 
30331da177e4SLinus Torvalds void __init rtnetlink_init(void)
30341da177e4SLinus Torvalds {
303597c53cacSDenis V. Lunev 	if (register_pernet_subsys(&rtnetlink_net_ops))
30361da177e4SLinus Torvalds 		panic("rtnetlink_init: cannot initialize rtnetlink\n");
303797c53cacSDenis V. Lunev 
30381da177e4SLinus Torvalds 	register_netdevice_notifier(&rtnetlink_dev_notifier);
3039340d17fcSThomas Graf 
3040c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
3041c7ac8679SGreg Rose 		      rtnl_dump_ifinfo, rtnl_calcit);
3042c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL);
3043c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL);
3044c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL);
3045687ad8ccSThomas Graf 
3046c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL);
3047c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL);
304877162022SJohn Fastabend 
304977162022SJohn Fastabend 	rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, NULL);
305077162022SJohn Fastabend 	rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, NULL);
305177162022SJohn Fastabend 	rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, NULL);
3052e5a55a89SJohn Fastabend 
3053e5a55a89SJohn Fastabend 	rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, NULL);
3054407af329SVlad Yasevich 	rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, NULL);
3055e5a55a89SJohn Fastabend 	rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, NULL);
30561da177e4SLinus Torvalds }
30571da177e4SLinus Torvalds 
3058