xref: /openbmc/linux/net/core/rtnetlink.c (revision 2907c35ff64708065e5a7fd54e8ded8263eb3074)
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>
38ebc08a6fSWilliams, Mitch A #include <linux/pci.h>
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds #include <asm/uaccess.h>
411da177e4SLinus Torvalds #include <asm/system.h>
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds #include <linux/inet.h>
441da177e4SLinus Torvalds #include <linux/netdevice.h>
451da177e4SLinus Torvalds #include <net/ip.h>
461da177e4SLinus Torvalds #include <net/protocol.h>
471da177e4SLinus Torvalds #include <net/arp.h>
481da177e4SLinus Torvalds #include <net/route.h>
491da177e4SLinus Torvalds #include <net/udp.h>
501da177e4SLinus Torvalds #include <net/sock.h>
511da177e4SLinus Torvalds #include <net/pkt_sched.h>
5214c0b97dSThomas Graf #include <net/fib_rules.h>
53e2849863SThomas Graf #include <net/rtnetlink.h>
5430ffee84SJohannes Berg #include <net/net_namespace.h>
551da177e4SLinus Torvalds 
56e0d087afSEric Dumazet struct rtnl_link {
57e2849863SThomas Graf 	rtnl_doit_func		doit;
58e2849863SThomas Graf 	rtnl_dumpit_func	dumpit;
59e2849863SThomas Graf };
60e2849863SThomas Graf 
616756ae4bSStephen Hemminger static DEFINE_MUTEX(rtnl_mutex);
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds void rtnl_lock(void)
641da177e4SLinus Torvalds {
656756ae4bSStephen Hemminger 	mutex_lock(&rtnl_mutex);
661da177e4SLinus Torvalds }
67e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_lock);
681da177e4SLinus Torvalds 
696756ae4bSStephen Hemminger void __rtnl_unlock(void)
701da177e4SLinus Torvalds {
716756ae4bSStephen Hemminger 	mutex_unlock(&rtnl_mutex);
721da177e4SLinus Torvalds }
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds void rtnl_unlock(void)
751da177e4SLinus Torvalds {
7658ec3b4dSHerbert Xu 	/* This fellow will unlock it for us. */
771da177e4SLinus Torvalds 	netdev_run_todo();
781da177e4SLinus Torvalds }
79e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unlock);
801da177e4SLinus Torvalds 
816756ae4bSStephen Hemminger int rtnl_trylock(void)
826756ae4bSStephen Hemminger {
836756ae4bSStephen Hemminger 	return mutex_trylock(&rtnl_mutex);
846756ae4bSStephen Hemminger }
85e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_trylock);
866756ae4bSStephen Hemminger 
87c9c1014bSPatrick McHardy int rtnl_is_locked(void)
88c9c1014bSPatrick McHardy {
89c9c1014bSPatrick McHardy 	return mutex_is_locked(&rtnl_mutex);
90c9c1014bSPatrick McHardy }
91e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_is_locked);
92c9c1014bSPatrick McHardy 
93a898def2SPaul E. McKenney #ifdef CONFIG_PROVE_LOCKING
94a898def2SPaul E. McKenney int lockdep_rtnl_is_held(void)
95a898def2SPaul E. McKenney {
96a898def2SPaul E. McKenney 	return lockdep_is_held(&rtnl_mutex);
97a898def2SPaul E. McKenney }
98a898def2SPaul E. McKenney EXPORT_SYMBOL(lockdep_rtnl_is_held);
99a898def2SPaul E. McKenney #endif /* #ifdef CONFIG_PROVE_LOCKING */
100a898def2SPaul E. McKenney 
10125239ceeSPatrick McHardy static struct rtnl_link *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
102e2849863SThomas Graf 
103e2849863SThomas Graf static inline int rtm_msgindex(int msgtype)
104e2849863SThomas Graf {
105e2849863SThomas Graf 	int msgindex = msgtype - RTM_BASE;
106e2849863SThomas Graf 
107e2849863SThomas Graf 	/*
108e2849863SThomas Graf 	 * msgindex < 0 implies someone tried to register a netlink
109e2849863SThomas Graf 	 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
110e2849863SThomas Graf 	 * the message type has not been added to linux/rtnetlink.h
111e2849863SThomas Graf 	 */
112e2849863SThomas Graf 	BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
113e2849863SThomas Graf 
114e2849863SThomas Graf 	return msgindex;
115e2849863SThomas Graf }
116e2849863SThomas Graf 
117e2849863SThomas Graf static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex)
118e2849863SThomas Graf {
119e2849863SThomas Graf 	struct rtnl_link *tab;
120e2849863SThomas Graf 
12125239ceeSPatrick McHardy 	if (protocol <= RTNL_FAMILY_MAX)
122e2849863SThomas Graf 		tab = rtnl_msg_handlers[protocol];
1230f87b1ddSPatrick McHardy 	else
1240f87b1ddSPatrick McHardy 		tab = NULL;
1250f87b1ddSPatrick McHardy 
12651057f2fSThomas Graf 	if (tab == NULL || tab[msgindex].doit == NULL)
127e2849863SThomas Graf 		tab = rtnl_msg_handlers[PF_UNSPEC];
128e2849863SThomas Graf 
12951057f2fSThomas Graf 	return tab ? tab[msgindex].doit : NULL;
130e2849863SThomas Graf }
131e2849863SThomas Graf 
132e2849863SThomas Graf static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex)
133e2849863SThomas Graf {
134e2849863SThomas Graf 	struct rtnl_link *tab;
135e2849863SThomas Graf 
13625239ceeSPatrick McHardy 	if (protocol <= RTNL_FAMILY_MAX)
137e2849863SThomas Graf 		tab = rtnl_msg_handlers[protocol];
1380f87b1ddSPatrick McHardy 	else
1390f87b1ddSPatrick McHardy 		tab = NULL;
1400f87b1ddSPatrick McHardy 
14151057f2fSThomas Graf 	if (tab == NULL || tab[msgindex].dumpit == NULL)
142e2849863SThomas Graf 		tab = rtnl_msg_handlers[PF_UNSPEC];
143e2849863SThomas Graf 
14451057f2fSThomas Graf 	return tab ? tab[msgindex].dumpit : NULL;
145e2849863SThomas Graf }
146e2849863SThomas Graf 
147e2849863SThomas Graf /**
148e2849863SThomas Graf  * __rtnl_register - Register a rtnetlink message type
149e2849863SThomas Graf  * @protocol: Protocol family or PF_UNSPEC
150e2849863SThomas Graf  * @msgtype: rtnetlink message type
151e2849863SThomas Graf  * @doit: Function pointer called for each request message
152e2849863SThomas Graf  * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
153e2849863SThomas Graf  *
154e2849863SThomas Graf  * Registers the specified function pointers (at least one of them has
155e2849863SThomas Graf  * to be non-NULL) to be called whenever a request message for the
156e2849863SThomas Graf  * specified protocol family and message type is received.
157e2849863SThomas Graf  *
158e2849863SThomas Graf  * The special protocol family PF_UNSPEC may be used to define fallback
159e2849863SThomas Graf  * function pointers for the case when no entry for the specific protocol
160e2849863SThomas Graf  * family exists.
161e2849863SThomas Graf  *
162e2849863SThomas Graf  * Returns 0 on success or a negative error code.
163e2849863SThomas Graf  */
164e2849863SThomas Graf int __rtnl_register(int protocol, int msgtype,
165e2849863SThomas Graf 		    rtnl_doit_func doit, rtnl_dumpit_func dumpit)
166e2849863SThomas Graf {
167e2849863SThomas Graf 	struct rtnl_link *tab;
168e2849863SThomas Graf 	int msgindex;
169e2849863SThomas Graf 
17025239ceeSPatrick McHardy 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
171e2849863SThomas Graf 	msgindex = rtm_msgindex(msgtype);
172e2849863SThomas Graf 
173e2849863SThomas Graf 	tab = rtnl_msg_handlers[protocol];
174e2849863SThomas Graf 	if (tab == NULL) {
175e2849863SThomas Graf 		tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL);
176e2849863SThomas Graf 		if (tab == NULL)
177e2849863SThomas Graf 			return -ENOBUFS;
178e2849863SThomas Graf 
179e2849863SThomas Graf 		rtnl_msg_handlers[protocol] = tab;
180e2849863SThomas Graf 	}
181e2849863SThomas Graf 
182e2849863SThomas Graf 	if (doit)
183e2849863SThomas Graf 		tab[msgindex].doit = doit;
184e2849863SThomas Graf 
185e2849863SThomas Graf 	if (dumpit)
186e2849863SThomas Graf 		tab[msgindex].dumpit = dumpit;
187e2849863SThomas Graf 
188e2849863SThomas Graf 	return 0;
189e2849863SThomas Graf }
190e2849863SThomas Graf EXPORT_SYMBOL_GPL(__rtnl_register);
191e2849863SThomas Graf 
192e2849863SThomas Graf /**
193e2849863SThomas Graf  * rtnl_register - Register a rtnetlink message type
194e2849863SThomas Graf  *
195e2849863SThomas Graf  * Identical to __rtnl_register() but panics on failure. This is useful
196e2849863SThomas Graf  * as failure of this function is very unlikely, it can only happen due
197e2849863SThomas Graf  * to lack of memory when allocating the chain to store all message
198e2849863SThomas Graf  * handlers for a protocol. Meant for use in init functions where lack
19925985edcSLucas De Marchi  * of memory implies no sense in continuing.
200e2849863SThomas Graf  */
201e2849863SThomas Graf void rtnl_register(int protocol, int msgtype,
202e2849863SThomas Graf 		   rtnl_doit_func doit, rtnl_dumpit_func dumpit)
203e2849863SThomas Graf {
204e2849863SThomas Graf 	if (__rtnl_register(protocol, msgtype, doit, dumpit) < 0)
205e2849863SThomas Graf 		panic("Unable to register rtnetlink message handler, "
206e2849863SThomas Graf 		      "protocol = %d, message type = %d\n",
207e2849863SThomas Graf 		      protocol, msgtype);
208e2849863SThomas Graf }
209e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_register);
210e2849863SThomas Graf 
211e2849863SThomas Graf /**
212e2849863SThomas Graf  * rtnl_unregister - Unregister a rtnetlink message type
213e2849863SThomas Graf  * @protocol: Protocol family or PF_UNSPEC
214e2849863SThomas Graf  * @msgtype: rtnetlink message type
215e2849863SThomas Graf  *
216e2849863SThomas Graf  * Returns 0 on success or a negative error code.
217e2849863SThomas Graf  */
218e2849863SThomas Graf int rtnl_unregister(int protocol, int msgtype)
219e2849863SThomas Graf {
220e2849863SThomas Graf 	int msgindex;
221e2849863SThomas Graf 
22225239ceeSPatrick McHardy 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
223e2849863SThomas Graf 	msgindex = rtm_msgindex(msgtype);
224e2849863SThomas Graf 
225e2849863SThomas Graf 	if (rtnl_msg_handlers[protocol] == NULL)
226e2849863SThomas Graf 		return -ENOENT;
227e2849863SThomas Graf 
228e2849863SThomas Graf 	rtnl_msg_handlers[protocol][msgindex].doit = NULL;
229e2849863SThomas Graf 	rtnl_msg_handlers[protocol][msgindex].dumpit = NULL;
230e2849863SThomas Graf 
231e2849863SThomas Graf 	return 0;
232e2849863SThomas Graf }
233e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister);
234e2849863SThomas Graf 
235e2849863SThomas Graf /**
236e2849863SThomas Graf  * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
237e2849863SThomas Graf  * @protocol : Protocol family or PF_UNSPEC
238e2849863SThomas Graf  *
239e2849863SThomas Graf  * Identical to calling rtnl_unregster() for all registered message types
240e2849863SThomas Graf  * of a certain protocol family.
241e2849863SThomas Graf  */
242e2849863SThomas Graf void rtnl_unregister_all(int protocol)
243e2849863SThomas Graf {
24425239ceeSPatrick McHardy 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
245e2849863SThomas Graf 
246e2849863SThomas Graf 	kfree(rtnl_msg_handlers[protocol]);
247e2849863SThomas Graf 	rtnl_msg_handlers[protocol] = NULL;
248e2849863SThomas Graf }
249e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister_all);
2501da177e4SLinus Torvalds 
25138f7b870SPatrick McHardy static LIST_HEAD(link_ops);
25238f7b870SPatrick McHardy 
25338f7b870SPatrick McHardy /**
25438f7b870SPatrick McHardy  * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
25538f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to register
25638f7b870SPatrick McHardy  *
25738f7b870SPatrick McHardy  * The caller must hold the rtnl_mutex. This function should be used
25838f7b870SPatrick McHardy  * by drivers that create devices during module initialization. It
25938f7b870SPatrick McHardy  * must be called before registering the devices.
26038f7b870SPatrick McHardy  *
26138f7b870SPatrick McHardy  * Returns 0 on success or a negative error code.
26238f7b870SPatrick McHardy  */
26338f7b870SPatrick McHardy int __rtnl_link_register(struct rtnl_link_ops *ops)
26438f7b870SPatrick McHardy {
2652d85cba2SPatrick McHardy 	if (!ops->dellink)
26623289a37SEric Dumazet 		ops->dellink = unregister_netdevice_queue;
2672d85cba2SPatrick McHardy 
26838f7b870SPatrick McHardy 	list_add_tail(&ops->list, &link_ops);
26938f7b870SPatrick McHardy 	return 0;
27038f7b870SPatrick McHardy }
27138f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_register);
27238f7b870SPatrick McHardy 
27338f7b870SPatrick McHardy /**
27438f7b870SPatrick McHardy  * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
27538f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to register
27638f7b870SPatrick McHardy  *
27738f7b870SPatrick McHardy  * Returns 0 on success or a negative error code.
27838f7b870SPatrick McHardy  */
27938f7b870SPatrick McHardy int rtnl_link_register(struct rtnl_link_ops *ops)
28038f7b870SPatrick McHardy {
28138f7b870SPatrick McHardy 	int err;
28238f7b870SPatrick McHardy 
28338f7b870SPatrick McHardy 	rtnl_lock();
28438f7b870SPatrick McHardy 	err = __rtnl_link_register(ops);
28538f7b870SPatrick McHardy 	rtnl_unlock();
28638f7b870SPatrick McHardy 	return err;
28738f7b870SPatrick McHardy }
28838f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_register);
28938f7b870SPatrick McHardy 
290669f87baSPavel Emelyanov static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
291669f87baSPavel Emelyanov {
292669f87baSPavel Emelyanov 	struct net_device *dev;
29323289a37SEric Dumazet 	LIST_HEAD(list_kill);
29423289a37SEric Dumazet 
295669f87baSPavel Emelyanov 	for_each_netdev(net, dev) {
29623289a37SEric Dumazet 		if (dev->rtnl_link_ops == ops)
29723289a37SEric Dumazet 			ops->dellink(dev, &list_kill);
298669f87baSPavel Emelyanov 	}
29923289a37SEric Dumazet 	unregister_netdevice_many(&list_kill);
300669f87baSPavel Emelyanov }
301669f87baSPavel Emelyanov 
30238f7b870SPatrick McHardy /**
30338f7b870SPatrick McHardy  * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
30438f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to unregister
30538f7b870SPatrick McHardy  *
3062d85cba2SPatrick McHardy  * The caller must hold the rtnl_mutex.
30738f7b870SPatrick McHardy  */
30838f7b870SPatrick McHardy void __rtnl_link_unregister(struct rtnl_link_ops *ops)
30938f7b870SPatrick McHardy {
310881d966bSEric W. Biederman 	struct net *net;
3112d85cba2SPatrick McHardy 
312881d966bSEric W. Biederman 	for_each_net(net) {
313669f87baSPavel Emelyanov 		__rtnl_kill_links(net, ops);
314881d966bSEric W. Biederman 	}
31538f7b870SPatrick McHardy 	list_del(&ops->list);
31638f7b870SPatrick McHardy }
31738f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
31838f7b870SPatrick McHardy 
31938f7b870SPatrick McHardy /**
32038f7b870SPatrick McHardy  * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
32138f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to unregister
32238f7b870SPatrick McHardy  */
32338f7b870SPatrick McHardy void rtnl_link_unregister(struct rtnl_link_ops *ops)
32438f7b870SPatrick McHardy {
32538f7b870SPatrick McHardy 	rtnl_lock();
32638f7b870SPatrick McHardy 	__rtnl_link_unregister(ops);
32738f7b870SPatrick McHardy 	rtnl_unlock();
32838f7b870SPatrick McHardy }
32938f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_unregister);
33038f7b870SPatrick McHardy 
33138f7b870SPatrick McHardy static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
33238f7b870SPatrick McHardy {
33338f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops;
33438f7b870SPatrick McHardy 
33538f7b870SPatrick McHardy 	list_for_each_entry(ops, &link_ops, list) {
33638f7b870SPatrick McHardy 		if (!strcmp(ops->kind, kind))
33738f7b870SPatrick McHardy 			return ops;
33838f7b870SPatrick McHardy 	}
33938f7b870SPatrick McHardy 	return NULL;
34038f7b870SPatrick McHardy }
34138f7b870SPatrick McHardy 
34238f7b870SPatrick McHardy static size_t rtnl_link_get_size(const struct net_device *dev)
34338f7b870SPatrick McHardy {
34438f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
34538f7b870SPatrick McHardy 	size_t size;
34638f7b870SPatrick McHardy 
34738f7b870SPatrick McHardy 	if (!ops)
34838f7b870SPatrick McHardy 		return 0;
34938f7b870SPatrick McHardy 
350369cf77aSThomas Graf 	size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
351369cf77aSThomas Graf 	       nla_total_size(strlen(ops->kind) + 1);  /* IFLA_INFO_KIND */
35238f7b870SPatrick McHardy 
35338f7b870SPatrick McHardy 	if (ops->get_size)
35438f7b870SPatrick McHardy 		/* IFLA_INFO_DATA + nested data */
355369cf77aSThomas Graf 		size += nla_total_size(sizeof(struct nlattr)) +
35638f7b870SPatrick McHardy 			ops->get_size(dev);
35738f7b870SPatrick McHardy 
35838f7b870SPatrick McHardy 	if (ops->get_xstats_size)
359369cf77aSThomas Graf 		/* IFLA_INFO_XSTATS */
360369cf77aSThomas Graf 		size += nla_total_size(ops->get_xstats_size(dev));
36138f7b870SPatrick McHardy 
36238f7b870SPatrick McHardy 	return size;
36338f7b870SPatrick McHardy }
36438f7b870SPatrick McHardy 
365f8ff182cSThomas Graf static LIST_HEAD(rtnl_af_ops);
366f8ff182cSThomas Graf 
367f8ff182cSThomas Graf static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
368f8ff182cSThomas Graf {
369f8ff182cSThomas Graf 	const struct rtnl_af_ops *ops;
370f8ff182cSThomas Graf 
371f8ff182cSThomas Graf 	list_for_each_entry(ops, &rtnl_af_ops, list) {
372f8ff182cSThomas Graf 		if (ops->family == family)
373f8ff182cSThomas Graf 			return ops;
374f8ff182cSThomas Graf 	}
375f8ff182cSThomas Graf 
376f8ff182cSThomas Graf 	return NULL;
377f8ff182cSThomas Graf }
378f8ff182cSThomas Graf 
379f8ff182cSThomas Graf /**
380f8ff182cSThomas Graf  * __rtnl_af_register - Register rtnl_af_ops with rtnetlink.
381f8ff182cSThomas Graf  * @ops: struct rtnl_af_ops * to register
382f8ff182cSThomas Graf  *
383f8ff182cSThomas Graf  * The caller must hold the rtnl_mutex.
384f8ff182cSThomas Graf  *
385f8ff182cSThomas Graf  * Returns 0 on success or a negative error code.
386f8ff182cSThomas Graf  */
387f8ff182cSThomas Graf int __rtnl_af_register(struct rtnl_af_ops *ops)
388f8ff182cSThomas Graf {
389f8ff182cSThomas Graf 	list_add_tail(&ops->list, &rtnl_af_ops);
390f8ff182cSThomas Graf 	return 0;
391f8ff182cSThomas Graf }
392f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(__rtnl_af_register);
393f8ff182cSThomas Graf 
394f8ff182cSThomas Graf /**
395f8ff182cSThomas Graf  * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
396f8ff182cSThomas Graf  * @ops: struct rtnl_af_ops * to register
397f8ff182cSThomas Graf  *
398f8ff182cSThomas Graf  * Returns 0 on success or a negative error code.
399f8ff182cSThomas Graf  */
400f8ff182cSThomas Graf int rtnl_af_register(struct rtnl_af_ops *ops)
401f8ff182cSThomas Graf {
402f8ff182cSThomas Graf 	int err;
403f8ff182cSThomas Graf 
404f8ff182cSThomas Graf 	rtnl_lock();
405f8ff182cSThomas Graf 	err = __rtnl_af_register(ops);
406f8ff182cSThomas Graf 	rtnl_unlock();
407f8ff182cSThomas Graf 	return err;
408f8ff182cSThomas Graf }
409f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_register);
410f8ff182cSThomas Graf 
411f8ff182cSThomas Graf /**
412f8ff182cSThomas Graf  * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
413f8ff182cSThomas Graf  * @ops: struct rtnl_af_ops * to unregister
414f8ff182cSThomas Graf  *
415f8ff182cSThomas Graf  * The caller must hold the rtnl_mutex.
416f8ff182cSThomas Graf  */
417f8ff182cSThomas Graf void __rtnl_af_unregister(struct rtnl_af_ops *ops)
418f8ff182cSThomas Graf {
419f8ff182cSThomas Graf 	list_del(&ops->list);
420f8ff182cSThomas Graf }
421f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(__rtnl_af_unregister);
422f8ff182cSThomas Graf 
423f8ff182cSThomas Graf /**
424f8ff182cSThomas Graf  * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
425f8ff182cSThomas Graf  * @ops: struct rtnl_af_ops * to unregister
426f8ff182cSThomas Graf  */
427f8ff182cSThomas Graf void rtnl_af_unregister(struct rtnl_af_ops *ops)
428f8ff182cSThomas Graf {
429f8ff182cSThomas Graf 	rtnl_lock();
430f8ff182cSThomas Graf 	__rtnl_af_unregister(ops);
431f8ff182cSThomas Graf 	rtnl_unlock();
432f8ff182cSThomas Graf }
433f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_unregister);
434f8ff182cSThomas Graf 
435f8ff182cSThomas Graf static size_t rtnl_link_get_af_size(const struct net_device *dev)
436f8ff182cSThomas Graf {
437f8ff182cSThomas Graf 	struct rtnl_af_ops *af_ops;
438f8ff182cSThomas Graf 	size_t size;
439f8ff182cSThomas Graf 
440f8ff182cSThomas Graf 	/* IFLA_AF_SPEC */
441f8ff182cSThomas Graf 	size = nla_total_size(sizeof(struct nlattr));
442f8ff182cSThomas Graf 
443f8ff182cSThomas Graf 	list_for_each_entry(af_ops, &rtnl_af_ops, list) {
444f8ff182cSThomas Graf 		if (af_ops->get_link_af_size) {
445f8ff182cSThomas Graf 			/* AF_* + nested data */
446f8ff182cSThomas Graf 			size += nla_total_size(sizeof(struct nlattr)) +
447f8ff182cSThomas Graf 				af_ops->get_link_af_size(dev);
448f8ff182cSThomas Graf 		}
449f8ff182cSThomas Graf 	}
450f8ff182cSThomas Graf 
451f8ff182cSThomas Graf 	return size;
452f8ff182cSThomas Graf }
453f8ff182cSThomas Graf 
45438f7b870SPatrick McHardy static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
45538f7b870SPatrick McHardy {
45638f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
45738f7b870SPatrick McHardy 	struct nlattr *linkinfo, *data;
45838f7b870SPatrick McHardy 	int err = -EMSGSIZE;
45938f7b870SPatrick McHardy 
46038f7b870SPatrick McHardy 	linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
46138f7b870SPatrick McHardy 	if (linkinfo == NULL)
46238f7b870SPatrick McHardy 		goto out;
46338f7b870SPatrick McHardy 
46438f7b870SPatrick McHardy 	if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
46538f7b870SPatrick McHardy 		goto err_cancel_link;
46638f7b870SPatrick McHardy 	if (ops->fill_xstats) {
46738f7b870SPatrick McHardy 		err = ops->fill_xstats(skb, dev);
46838f7b870SPatrick McHardy 		if (err < 0)
46938f7b870SPatrick McHardy 			goto err_cancel_link;
47038f7b870SPatrick McHardy 	}
47138f7b870SPatrick McHardy 	if (ops->fill_info) {
47238f7b870SPatrick McHardy 		data = nla_nest_start(skb, IFLA_INFO_DATA);
47338f7b870SPatrick McHardy 		if (data == NULL)
47438f7b870SPatrick McHardy 			goto err_cancel_link;
47538f7b870SPatrick McHardy 		err = ops->fill_info(skb, dev);
47638f7b870SPatrick McHardy 		if (err < 0)
47738f7b870SPatrick McHardy 			goto err_cancel_data;
47838f7b870SPatrick McHardy 		nla_nest_end(skb, data);
47938f7b870SPatrick McHardy 	}
48038f7b870SPatrick McHardy 
48138f7b870SPatrick McHardy 	nla_nest_end(skb, linkinfo);
48238f7b870SPatrick McHardy 	return 0;
48338f7b870SPatrick McHardy 
48438f7b870SPatrick McHardy err_cancel_data:
48538f7b870SPatrick McHardy 	nla_nest_cancel(skb, data);
48638f7b870SPatrick McHardy err_cancel_link:
48738f7b870SPatrick McHardy 	nla_nest_cancel(skb, linkinfo);
48838f7b870SPatrick McHardy out:
48938f7b870SPatrick McHardy 	return err;
49038f7b870SPatrick McHardy }
49138f7b870SPatrick McHardy 
492db46edc6SThomas Graf static const int rtm_min[RTM_NR_FAMILIES] =
4931da177e4SLinus Torvalds {
494f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWLINK)]      = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
495f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWADDR)]      = NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
496f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWROUTE)]     = NLMSG_LENGTH(sizeof(struct rtmsg)),
49714c0b97dSThomas Graf 	[RTM_FAM(RTM_NEWRULE)]      = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)),
498f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWQDISC)]     = NLMSG_LENGTH(sizeof(struct tcmsg)),
499f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWTCLASS)]    = NLMSG_LENGTH(sizeof(struct tcmsg)),
500f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWTFILTER)]   = NLMSG_LENGTH(sizeof(struct tcmsg)),
501f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWACTION)]    = NLMSG_LENGTH(sizeof(struct tcamsg)),
502f90a0a74SThomas Graf 	[RTM_FAM(RTM_GETMULTICAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
503f90a0a74SThomas Graf 	[RTM_FAM(RTM_GETANYCAST)]   = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
5041da177e4SLinus Torvalds };
5051da177e4SLinus Torvalds 
506db46edc6SThomas Graf static const int rta_max[RTM_NR_FAMILIES] =
5071da177e4SLinus Torvalds {
508f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWLINK)]      = IFLA_MAX,
509f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWADDR)]      = IFA_MAX,
510f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWROUTE)]     = RTA_MAX,
51114c0b97dSThomas Graf 	[RTM_FAM(RTM_NEWRULE)]      = FRA_MAX,
512f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWQDISC)]     = TCA_MAX,
513f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWTCLASS)]    = TCA_MAX,
514f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWTFILTER)]   = TCA_MAX,
515f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWACTION)]    = TCAA_MAX,
5161da177e4SLinus Torvalds };
5171da177e4SLinus Torvalds 
5181da177e4SLinus Torvalds void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
5191da177e4SLinus Torvalds {
5201da177e4SLinus Torvalds 	struct rtattr *rta;
5211da177e4SLinus Torvalds 	int size = RTA_LENGTH(attrlen);
5221da177e4SLinus Torvalds 
5231da177e4SLinus Torvalds 	rta = (struct rtattr *)skb_put(skb, RTA_ALIGN(size));
5241da177e4SLinus Torvalds 	rta->rta_type = attrtype;
5251da177e4SLinus Torvalds 	rta->rta_len = size;
5261da177e4SLinus Torvalds 	memcpy(RTA_DATA(rta), data, attrlen);
527b3563c4fSPatrick McHardy 	memset(RTA_DATA(rta) + attrlen, 0, RTA_ALIGN(size) - size);
5281da177e4SLinus Torvalds }
529e0d087afSEric Dumazet EXPORT_SYMBOL(__rta_fill);
5301da177e4SLinus Torvalds 
53197c53cacSDenis V. Lunev int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned group, int echo)
5321da177e4SLinus Torvalds {
53397c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
5341da177e4SLinus Torvalds 	int err = 0;
5351da177e4SLinus Torvalds 
536ac6d439dSPatrick McHardy 	NETLINK_CB(skb).dst_group = group;
5371da177e4SLinus Torvalds 	if (echo)
5381da177e4SLinus Torvalds 		atomic_inc(&skb->users);
5391da177e4SLinus Torvalds 	netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
5401da177e4SLinus Torvalds 	if (echo)
5411da177e4SLinus Torvalds 		err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
5421da177e4SLinus Torvalds 	return err;
5431da177e4SLinus Torvalds }
5441da177e4SLinus Torvalds 
54597c53cacSDenis V. Lunev int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
5462942e900SThomas Graf {
54797c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
54897c53cacSDenis V. Lunev 
5492942e900SThomas Graf 	return nlmsg_unicast(rtnl, skb, pid);
5502942e900SThomas Graf }
551e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unicast);
5522942e900SThomas Graf 
5531ce85fe4SPablo Neira Ayuso void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
55497676b6bSThomas Graf 		 struct nlmsghdr *nlh, gfp_t flags)
55597676b6bSThomas Graf {
55697c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
55797676b6bSThomas Graf 	int report = 0;
55897676b6bSThomas Graf 
55997676b6bSThomas Graf 	if (nlh)
56097676b6bSThomas Graf 		report = nlmsg_report(nlh);
56197676b6bSThomas Graf 
5621ce85fe4SPablo Neira Ayuso 	nlmsg_notify(rtnl, skb, pid, group, report, flags);
56397676b6bSThomas Graf }
564e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_notify);
56597676b6bSThomas Graf 
56697c53cacSDenis V. Lunev void rtnl_set_sk_err(struct net *net, u32 group, int error)
56797676b6bSThomas Graf {
56897c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
56997c53cacSDenis V. Lunev 
57097676b6bSThomas Graf 	netlink_set_err(rtnl, 0, group, error);
57197676b6bSThomas Graf }
572e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_set_sk_err);
57397676b6bSThomas Graf 
5741da177e4SLinus Torvalds int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
5751da177e4SLinus Torvalds {
5762d7202bfSThomas Graf 	struct nlattr *mx;
5772d7202bfSThomas Graf 	int i, valid = 0;
5781da177e4SLinus Torvalds 
5792d7202bfSThomas Graf 	mx = nla_nest_start(skb, RTA_METRICS);
5802d7202bfSThomas Graf 	if (mx == NULL)
5812d7202bfSThomas Graf 		return -ENOBUFS;
5822d7202bfSThomas Graf 
5831da177e4SLinus Torvalds 	for (i = 0; i < RTAX_MAX; i++) {
5842d7202bfSThomas Graf 		if (metrics[i]) {
5852d7202bfSThomas Graf 			valid++;
5862d7202bfSThomas Graf 			NLA_PUT_U32(skb, i+1, metrics[i]);
5871da177e4SLinus Torvalds 		}
5882d7202bfSThomas Graf 	}
5891da177e4SLinus Torvalds 
590a57d27fcSDavid S. Miller 	if (!valid) {
591a57d27fcSDavid S. Miller 		nla_nest_cancel(skb, mx);
592a57d27fcSDavid S. Miller 		return 0;
593a57d27fcSDavid S. Miller 	}
5942d7202bfSThomas Graf 
5952d7202bfSThomas Graf 	return nla_nest_end(skb, mx);
5962d7202bfSThomas Graf 
5972d7202bfSThomas Graf nla_put_failure:
598bc3ed28cSThomas Graf 	nla_nest_cancel(skb, mx);
599bc3ed28cSThomas Graf 	return -EMSGSIZE;
6001da177e4SLinus Torvalds }
601e0d087afSEric Dumazet EXPORT_SYMBOL(rtnetlink_put_metrics);
6021da177e4SLinus Torvalds 
603e3703b3dSThomas Graf int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
604e3703b3dSThomas Graf 		       u32 ts, u32 tsage, long expires, u32 error)
605e3703b3dSThomas Graf {
606e3703b3dSThomas Graf 	struct rta_cacheinfo ci = {
607e3703b3dSThomas Graf 		.rta_lastuse = jiffies_to_clock_t(jiffies - dst->lastuse),
608e3703b3dSThomas Graf 		.rta_used = dst->__use,
609e3703b3dSThomas Graf 		.rta_clntref = atomic_read(&(dst->__refcnt)),
610e3703b3dSThomas Graf 		.rta_error = error,
611e3703b3dSThomas Graf 		.rta_id =  id,
612e3703b3dSThomas Graf 		.rta_ts = ts,
613e3703b3dSThomas Graf 		.rta_tsage = tsage,
614e3703b3dSThomas Graf 	};
615e3703b3dSThomas Graf 
616e3703b3dSThomas Graf 	if (expires)
617e3703b3dSThomas Graf 		ci.rta_expires = jiffies_to_clock_t(expires);
618e3703b3dSThomas Graf 
619e3703b3dSThomas Graf 	return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
620e3703b3dSThomas Graf }
621e3703b3dSThomas Graf EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
6221da177e4SLinus Torvalds 
62393b2d4a2SDavid S. Miller static void set_operstate(struct net_device *dev, unsigned char transition)
624b00055aaSStefan Rompf {
625b00055aaSStefan Rompf 	unsigned char operstate = dev->operstate;
626b00055aaSStefan Rompf 
627b00055aaSStefan Rompf 	switch (transition) {
628b00055aaSStefan Rompf 	case IF_OPER_UP:
629b00055aaSStefan Rompf 		if ((operstate == IF_OPER_DORMANT ||
630b00055aaSStefan Rompf 		     operstate == IF_OPER_UNKNOWN) &&
631b00055aaSStefan Rompf 		    !netif_dormant(dev))
632b00055aaSStefan Rompf 			operstate = IF_OPER_UP;
633b00055aaSStefan Rompf 		break;
634b00055aaSStefan Rompf 
635b00055aaSStefan Rompf 	case IF_OPER_DORMANT:
636b00055aaSStefan Rompf 		if (operstate == IF_OPER_UP ||
637b00055aaSStefan Rompf 		    operstate == IF_OPER_UNKNOWN)
638b00055aaSStefan Rompf 			operstate = IF_OPER_DORMANT;
639b00055aaSStefan Rompf 		break;
6403ff50b79SStephen Hemminger 	}
641b00055aaSStefan Rompf 
642b00055aaSStefan Rompf 	if (dev->operstate != operstate) {
643b00055aaSStefan Rompf 		write_lock_bh(&dev_base_lock);
644b00055aaSStefan Rompf 		dev->operstate = operstate;
645b00055aaSStefan Rompf 		write_unlock_bh(&dev_base_lock);
646b00055aaSStefan Rompf 		netdev_state_change(dev);
64793b2d4a2SDavid S. Miller 	}
648b00055aaSStefan Rompf }
649b00055aaSStefan Rompf 
6503729d502SPatrick McHardy static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
6513729d502SPatrick McHardy 					   const struct ifinfomsg *ifm)
6523729d502SPatrick McHardy {
6533729d502SPatrick McHardy 	unsigned int flags = ifm->ifi_flags;
6543729d502SPatrick McHardy 
6553729d502SPatrick McHardy 	/* bugwards compatibility: ifi_change == 0 is treated as ~0 */
6563729d502SPatrick McHardy 	if (ifm->ifi_change)
6573729d502SPatrick McHardy 		flags = (flags & ifm->ifi_change) |
6583729d502SPatrick McHardy 			(dev->flags & ~ifm->ifi_change);
6593729d502SPatrick McHardy 
6603729d502SPatrick McHardy 	return flags;
6613729d502SPatrick McHardy }
6623729d502SPatrick McHardy 
663b60c5115SThomas Graf static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
664be1f3c2cSBen Hutchings 				 const struct rtnl_link_stats64 *b)
6651da177e4SLinus Torvalds {
666b60c5115SThomas Graf 	a->rx_packets = b->rx_packets;
667b60c5115SThomas Graf 	a->tx_packets = b->tx_packets;
668b60c5115SThomas Graf 	a->rx_bytes = b->rx_bytes;
669b60c5115SThomas Graf 	a->tx_bytes = b->tx_bytes;
670b60c5115SThomas Graf 	a->rx_errors = b->rx_errors;
671b60c5115SThomas Graf 	a->tx_errors = b->tx_errors;
672b60c5115SThomas Graf 	a->rx_dropped = b->rx_dropped;
673b60c5115SThomas Graf 	a->tx_dropped = b->tx_dropped;
674b60c5115SThomas Graf 
675b60c5115SThomas Graf 	a->multicast = b->multicast;
676b60c5115SThomas Graf 	a->collisions = b->collisions;
677b60c5115SThomas Graf 
678b60c5115SThomas Graf 	a->rx_length_errors = b->rx_length_errors;
679b60c5115SThomas Graf 	a->rx_over_errors = b->rx_over_errors;
680b60c5115SThomas Graf 	a->rx_crc_errors = b->rx_crc_errors;
681b60c5115SThomas Graf 	a->rx_frame_errors = b->rx_frame_errors;
682b60c5115SThomas Graf 	a->rx_fifo_errors = b->rx_fifo_errors;
683b60c5115SThomas Graf 	a->rx_missed_errors = b->rx_missed_errors;
684b60c5115SThomas Graf 
685b60c5115SThomas Graf 	a->tx_aborted_errors = b->tx_aborted_errors;
686b60c5115SThomas Graf 	a->tx_carrier_errors = b->tx_carrier_errors;
687b60c5115SThomas Graf 	a->tx_fifo_errors = b->tx_fifo_errors;
688b60c5115SThomas Graf 	a->tx_heartbeat_errors = b->tx_heartbeat_errors;
689b60c5115SThomas Graf 	a->tx_window_errors = b->tx_window_errors;
690b60c5115SThomas Graf 
691b60c5115SThomas Graf 	a->rx_compressed = b->rx_compressed;
692b60c5115SThomas Graf 	a->tx_compressed = b->tx_compressed;
69310708f37SJan Engelhardt }
69410708f37SJan Engelhardt 
695be1f3c2cSBen Hutchings static void copy_rtnl_link_stats64(void *v, const struct rtnl_link_stats64 *b)
69610708f37SJan Engelhardt {
697afdcba37SEric Dumazet 	memcpy(v, b, sizeof(*b));
69810708f37SJan Engelhardt }
699b60c5115SThomas Graf 
700c02db8c6SChris Wright /* All VF info */
701ebc08a6fSWilliams, Mitch A static inline int rtnl_vfinfo_size(const struct net_device *dev)
702ebc08a6fSWilliams, Mitch A {
703c02db8c6SChris Wright 	if (dev->dev.parent && dev_is_pci(dev->dev.parent)) {
704c02db8c6SChris Wright 
705c02db8c6SChris Wright 		int num_vfs = dev_num_vf(dev->dev.parent);
706045de01aSScott Feldman 		size_t size = nla_total_size(sizeof(struct nlattr));
707045de01aSScott Feldman 		size += nla_total_size(num_vfs * sizeof(struct nlattr));
708045de01aSScott Feldman 		size += num_vfs *
709045de01aSScott Feldman 			(nla_total_size(sizeof(struct ifla_vf_mac)) +
710045de01aSScott Feldman 			 nla_total_size(sizeof(struct ifla_vf_vlan)) +
711045de01aSScott Feldman 			 nla_total_size(sizeof(struct ifla_vf_tx_rate)));
712c02db8c6SChris Wright 		return size;
713c02db8c6SChris Wright 	} else
714ebc08a6fSWilliams, Mitch A 		return 0;
715ebc08a6fSWilliams, Mitch A }
716ebc08a6fSWilliams, Mitch A 
71757b61080SScott Feldman static size_t rtnl_port_size(const struct net_device *dev)
71857b61080SScott Feldman {
71957b61080SScott Feldman 	size_t port_size = nla_total_size(4)		/* PORT_VF */
72057b61080SScott Feldman 		+ nla_total_size(PORT_PROFILE_MAX)	/* PORT_PROFILE */
72157b61080SScott Feldman 		+ nla_total_size(sizeof(struct ifla_port_vsi))
72257b61080SScott Feldman 							/* PORT_VSI_TYPE */
72357b61080SScott Feldman 		+ nla_total_size(PORT_UUID_MAX)		/* PORT_INSTANCE_UUID */
72457b61080SScott Feldman 		+ nla_total_size(PORT_UUID_MAX)		/* PORT_HOST_UUID */
72557b61080SScott Feldman 		+ nla_total_size(1)			/* PROT_VDP_REQUEST */
72657b61080SScott Feldman 		+ nla_total_size(2);			/* PORT_VDP_RESPONSE */
72757b61080SScott Feldman 	size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
72857b61080SScott Feldman 	size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
72957b61080SScott Feldman 		+ port_size;
73057b61080SScott Feldman 	size_t port_self_size = nla_total_size(sizeof(struct nlattr))
73157b61080SScott Feldman 		+ port_size;
73257b61080SScott Feldman 
73357b61080SScott Feldman 	if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent)
73457b61080SScott Feldman 		return 0;
73557b61080SScott Feldman 	if (dev_num_vf(dev->dev.parent))
73657b61080SScott Feldman 		return port_self_size + vf_ports_size +
73757b61080SScott Feldman 			vf_port_size * dev_num_vf(dev->dev.parent);
73857b61080SScott Feldman 	else
73957b61080SScott Feldman 		return port_self_size;
74057b61080SScott Feldman }
74157b61080SScott Feldman 
7429e34a5b5SEric Dumazet static noinline size_t if_nlmsg_size(const struct net_device *dev)
743339bf98fSThomas Graf {
744339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
745339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
7460b815a1aSStephen Hemminger 	       + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
747339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
748339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rtnl_link_ifmap))
749339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rtnl_link_stats))
750adcfe196SJan Engelhardt 	       + nla_total_size(sizeof(struct rtnl_link_stats64))
751339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
752339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
753339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_TXQLEN */
754339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_WEIGHT */
755339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MTU */
756339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_LINK */
757339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MASTER */
758339bf98fSThomas Graf 	       + nla_total_size(1) /* IFLA_OPERSTATE */
75938f7b870SPatrick McHardy 	       + nla_total_size(1) /* IFLA_LINKMODE */
760ebc08a6fSWilliams, Mitch A 	       + nla_total_size(4) /* IFLA_NUM_VF */
761c02db8c6SChris Wright 	       + rtnl_vfinfo_size(dev) /* IFLA_VFINFO_LIST */
76257b61080SScott Feldman 	       + rtnl_port_size(dev) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
763f8ff182cSThomas Graf 	       + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
764f8ff182cSThomas Graf 	       + rtnl_link_get_af_size(dev); /* IFLA_AF_SPEC */
765339bf98fSThomas Graf }
766339bf98fSThomas Graf 
76757b61080SScott Feldman static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
76857b61080SScott Feldman {
76957b61080SScott Feldman 	struct nlattr *vf_ports;
77057b61080SScott Feldman 	struct nlattr *vf_port;
77157b61080SScott Feldman 	int vf;
77257b61080SScott Feldman 	int err;
77357b61080SScott Feldman 
77457b61080SScott Feldman 	vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
77557b61080SScott Feldman 	if (!vf_ports)
77657b61080SScott Feldman 		return -EMSGSIZE;
77757b61080SScott Feldman 
77857b61080SScott Feldman 	for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
77957b61080SScott Feldman 		vf_port = nla_nest_start(skb, IFLA_VF_PORT);
7808ca94183SScott Feldman 		if (!vf_port)
7818ca94183SScott Feldman 			goto nla_put_failure;
78257b61080SScott Feldman 		NLA_PUT_U32(skb, IFLA_PORT_VF, vf);
78357b61080SScott Feldman 		err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
7848ca94183SScott Feldman 		if (err == -EMSGSIZE)
7858ca94183SScott Feldman 			goto nla_put_failure;
78657b61080SScott Feldman 		if (err) {
78757b61080SScott Feldman 			nla_nest_cancel(skb, vf_port);
78857b61080SScott Feldman 			continue;
78957b61080SScott Feldman 		}
79057b61080SScott Feldman 		nla_nest_end(skb, vf_port);
79157b61080SScott Feldman 	}
79257b61080SScott Feldman 
79357b61080SScott Feldman 	nla_nest_end(skb, vf_ports);
79457b61080SScott Feldman 
79557b61080SScott Feldman 	return 0;
7968ca94183SScott Feldman 
7978ca94183SScott Feldman nla_put_failure:
7988ca94183SScott Feldman 	nla_nest_cancel(skb, vf_ports);
7998ca94183SScott Feldman 	return -EMSGSIZE;
80057b61080SScott Feldman }
80157b61080SScott Feldman 
80257b61080SScott Feldman static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
80357b61080SScott Feldman {
80457b61080SScott Feldman 	struct nlattr *port_self;
80557b61080SScott Feldman 	int err;
80657b61080SScott Feldman 
80757b61080SScott Feldman 	port_self = nla_nest_start(skb, IFLA_PORT_SELF);
80857b61080SScott Feldman 	if (!port_self)
80957b61080SScott Feldman 		return -EMSGSIZE;
81057b61080SScott Feldman 
81157b61080SScott Feldman 	err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
81257b61080SScott Feldman 	if (err) {
81357b61080SScott Feldman 		nla_nest_cancel(skb, port_self);
8148ca94183SScott Feldman 		return (err == -EMSGSIZE) ? err : 0;
81557b61080SScott Feldman 	}
81657b61080SScott Feldman 
81757b61080SScott Feldman 	nla_nest_end(skb, port_self);
81857b61080SScott Feldman 
81957b61080SScott Feldman 	return 0;
82057b61080SScott Feldman }
82157b61080SScott Feldman 
82257b61080SScott Feldman static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev)
82357b61080SScott Feldman {
82457b61080SScott Feldman 	int err;
82557b61080SScott Feldman 
82657b61080SScott Feldman 	if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent)
82757b61080SScott Feldman 		return 0;
82857b61080SScott Feldman 
82957b61080SScott Feldman 	err = rtnl_port_self_fill(skb, dev);
83057b61080SScott Feldman 	if (err)
83157b61080SScott Feldman 		return err;
83257b61080SScott Feldman 
83357b61080SScott Feldman 	if (dev_num_vf(dev->dev.parent)) {
83457b61080SScott Feldman 		err = rtnl_vf_ports_fill(skb, dev);
83557b61080SScott Feldman 		if (err)
83657b61080SScott Feldman 			return err;
83757b61080SScott Feldman 	}
83857b61080SScott Feldman 
83957b61080SScott Feldman 	return 0;
84057b61080SScott Feldman }
84157b61080SScott Feldman 
842b60c5115SThomas Graf static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
843575c3e2aSPatrick McHardy 			    int type, u32 pid, u32 seq, u32 change,
844575c3e2aSPatrick McHardy 			    unsigned int flags)
845b60c5115SThomas Graf {
846b60c5115SThomas Graf 	struct ifinfomsg *ifm;
8471da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
84828172739SEric Dumazet 	struct rtnl_link_stats64 temp;
849be1f3c2cSBen Hutchings 	const struct rtnl_link_stats64 *stats;
850f8ff182cSThomas Graf 	struct nlattr *attr, *af_spec;
851f8ff182cSThomas Graf 	struct rtnl_af_ops *af_ops;
8521da177e4SLinus Torvalds 
853*2907c35fSEric Dumazet 	ASSERT_RTNL();
854b60c5115SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
855b60c5115SThomas Graf 	if (nlh == NULL)
85626932566SPatrick McHardy 		return -EMSGSIZE;
8571da177e4SLinus Torvalds 
858b60c5115SThomas Graf 	ifm = nlmsg_data(nlh);
859b60c5115SThomas Graf 	ifm->ifi_family = AF_UNSPEC;
860b60c5115SThomas Graf 	ifm->__ifi_pad = 0;
861b60c5115SThomas Graf 	ifm->ifi_type = dev->type;
862b60c5115SThomas Graf 	ifm->ifi_index = dev->ifindex;
863b60c5115SThomas Graf 	ifm->ifi_flags = dev_get_flags(dev);
864b60c5115SThomas Graf 	ifm->ifi_change = change;
8651da177e4SLinus Torvalds 
866b60c5115SThomas Graf 	NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
867b60c5115SThomas Graf 	NLA_PUT_U32(skb, IFLA_TXQLEN, dev->tx_queue_len);
868b60c5115SThomas Graf 	NLA_PUT_U8(skb, IFLA_OPERSTATE,
869b60c5115SThomas Graf 		   netif_running(dev) ? dev->operstate : IF_OPER_DOWN);
870b60c5115SThomas Graf 	NLA_PUT_U8(skb, IFLA_LINKMODE, dev->link_mode);
871b60c5115SThomas Graf 	NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
872cbda10faSVlad Dogaru 	NLA_PUT_U32(skb, IFLA_GROUP, dev->group);
8731da177e4SLinus Torvalds 
874b60c5115SThomas Graf 	if (dev->ifindex != dev->iflink)
875b60c5115SThomas Graf 		NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
8761da177e4SLinus Torvalds 
877b60c5115SThomas Graf 	if (dev->master)
878b60c5115SThomas Graf 		NLA_PUT_U32(skb, IFLA_MASTER, dev->master->ifindex);
879b60c5115SThomas Graf 
880af356afaSPatrick McHardy 	if (dev->qdisc)
881af356afaSPatrick McHardy 		NLA_PUT_STRING(skb, IFLA_QDISC, dev->qdisc->ops->id);
882b00055aaSStefan Rompf 
8830b815a1aSStephen Hemminger 	if (dev->ifalias)
8840b815a1aSStephen Hemminger 		NLA_PUT_STRING(skb, IFLA_IFALIAS, dev->ifalias);
8850b815a1aSStephen Hemminger 
886b00055aaSStefan Rompf 	if (1) {
8871da177e4SLinus Torvalds 		struct rtnl_link_ifmap map = {
8881da177e4SLinus Torvalds 			.mem_start   = dev->mem_start,
8891da177e4SLinus Torvalds 			.mem_end     = dev->mem_end,
8901da177e4SLinus Torvalds 			.base_addr   = dev->base_addr,
8911da177e4SLinus Torvalds 			.irq         = dev->irq,
8921da177e4SLinus Torvalds 			.dma         = dev->dma,
8931da177e4SLinus Torvalds 			.port        = dev->if_port,
8941da177e4SLinus Torvalds 		};
895b60c5115SThomas Graf 		NLA_PUT(skb, IFLA_MAP, sizeof(map), &map);
8961da177e4SLinus Torvalds 	}
8971da177e4SLinus Torvalds 
8981da177e4SLinus Torvalds 	if (dev->addr_len) {
899b60c5115SThomas Graf 		NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
900b60c5115SThomas Graf 		NLA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast);
9011da177e4SLinus Torvalds 	}
9021da177e4SLinus Torvalds 
903b60c5115SThomas Graf 	attr = nla_reserve(skb, IFLA_STATS,
904b60c5115SThomas Graf 			sizeof(struct rtnl_link_stats));
905b60c5115SThomas Graf 	if (attr == NULL)
906b60c5115SThomas Graf 		goto nla_put_failure;
907b60c5115SThomas Graf 
90828172739SEric Dumazet 	stats = dev_get_stats(dev, &temp);
909b60c5115SThomas Graf 	copy_rtnl_link_stats(nla_data(attr), stats);
9101da177e4SLinus Torvalds 
91110708f37SJan Engelhardt 	attr = nla_reserve(skb, IFLA_STATS64,
91210708f37SJan Engelhardt 			sizeof(struct rtnl_link_stats64));
91310708f37SJan Engelhardt 	if (attr == NULL)
91410708f37SJan Engelhardt 		goto nla_put_failure;
91510708f37SJan Engelhardt 	copy_rtnl_link_stats64(nla_data(attr), stats);
91610708f37SJan Engelhardt 
91757b61080SScott Feldman 	if (dev->dev.parent)
91857b61080SScott Feldman 		NLA_PUT_U32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent));
91957b61080SScott Feldman 
920ebc08a6fSWilliams, Mitch A 	if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent) {
921ebc08a6fSWilliams, Mitch A 		int i;
922ebc08a6fSWilliams, Mitch A 
923c02db8c6SChris Wright 		struct nlattr *vfinfo, *vf;
924c02db8c6SChris Wright 		int num_vfs = dev_num_vf(dev->dev.parent);
925c02db8c6SChris Wright 
926c02db8c6SChris Wright 		vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
927c02db8c6SChris Wright 		if (!vfinfo)
928c02db8c6SChris Wright 			goto nla_put_failure;
929c02db8c6SChris Wright 		for (i = 0; i < num_vfs; i++) {
930c02db8c6SChris Wright 			struct ifla_vf_info ivi;
931c02db8c6SChris Wright 			struct ifla_vf_mac vf_mac;
932c02db8c6SChris Wright 			struct ifla_vf_vlan vf_vlan;
933c02db8c6SChris Wright 			struct ifla_vf_tx_rate vf_tx_rate;
934ebc08a6fSWilliams, Mitch A 			if (dev->netdev_ops->ndo_get_vf_config(dev, i, &ivi))
935ebc08a6fSWilliams, Mitch A 				break;
936c02db8c6SChris Wright 			vf_mac.vf = vf_vlan.vf = vf_tx_rate.vf = ivi.vf;
937c02db8c6SChris Wright 			memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
938c02db8c6SChris Wright 			vf_vlan.vlan = ivi.vlan;
939c02db8c6SChris Wright 			vf_vlan.qos = ivi.qos;
940c02db8c6SChris Wright 			vf_tx_rate.rate = ivi.tx_rate;
941c02db8c6SChris Wright 			vf = nla_nest_start(skb, IFLA_VF_INFO);
942c02db8c6SChris Wright 			if (!vf) {
943c02db8c6SChris Wright 				nla_nest_cancel(skb, vfinfo);
944c02db8c6SChris Wright 				goto nla_put_failure;
945ebc08a6fSWilliams, Mitch A 			}
946c02db8c6SChris Wright 			NLA_PUT(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac);
947c02db8c6SChris Wright 			NLA_PUT(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan);
948c02db8c6SChris Wright 			NLA_PUT(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate), &vf_tx_rate);
949c02db8c6SChris Wright 			nla_nest_end(skb, vf);
950c02db8c6SChris Wright 		}
951c02db8c6SChris Wright 		nla_nest_end(skb, vfinfo);
952ebc08a6fSWilliams, Mitch A 	}
95357b61080SScott Feldman 
95457b61080SScott Feldman 	if (rtnl_port_fill(skb, dev))
95557b61080SScott Feldman 		goto nla_put_failure;
95657b61080SScott Feldman 
95738f7b870SPatrick McHardy 	if (dev->rtnl_link_ops) {
95838f7b870SPatrick McHardy 		if (rtnl_link_fill(skb, dev) < 0)
95938f7b870SPatrick McHardy 			goto nla_put_failure;
96038f7b870SPatrick McHardy 	}
96138f7b870SPatrick McHardy 
962f8ff182cSThomas Graf 	if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
963f8ff182cSThomas Graf 		goto nla_put_failure;
964f8ff182cSThomas Graf 
965f8ff182cSThomas Graf 	list_for_each_entry(af_ops, &rtnl_af_ops, list) {
966f8ff182cSThomas Graf 		if (af_ops->fill_link_af) {
967f8ff182cSThomas Graf 			struct nlattr *af;
968f8ff182cSThomas Graf 			int err;
969f8ff182cSThomas Graf 
970f8ff182cSThomas Graf 			if (!(af = nla_nest_start(skb, af_ops->family)))
971f8ff182cSThomas Graf 				goto nla_put_failure;
972f8ff182cSThomas Graf 
973f8ff182cSThomas Graf 			err = af_ops->fill_link_af(skb, dev);
974f8ff182cSThomas Graf 
975f8ff182cSThomas Graf 			/*
976f8ff182cSThomas Graf 			 * Caller may return ENODATA to indicate that there
977f8ff182cSThomas Graf 			 * was no data to be dumped. This is not an error, it
978f8ff182cSThomas Graf 			 * means we should trim the attribute header and
979f8ff182cSThomas Graf 			 * continue.
980f8ff182cSThomas Graf 			 */
981f8ff182cSThomas Graf 			if (err == -ENODATA)
982f8ff182cSThomas Graf 				nla_nest_cancel(skb, af);
983f8ff182cSThomas Graf 			else if (err < 0)
984f8ff182cSThomas Graf 				goto nla_put_failure;
985f8ff182cSThomas Graf 
986f8ff182cSThomas Graf 			nla_nest_end(skb, af);
987f8ff182cSThomas Graf 		}
988f8ff182cSThomas Graf 	}
989f8ff182cSThomas Graf 
990f8ff182cSThomas Graf 	nla_nest_end(skb, af_spec);
991f8ff182cSThomas Graf 
992b60c5115SThomas Graf 	return nlmsg_end(skb, nlh);
993b60c5115SThomas Graf 
994b60c5115SThomas Graf nla_put_failure:
99526932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
99626932566SPatrick McHardy 	return -EMSGSIZE;
9971da177e4SLinus Torvalds }
9981da177e4SLinus Torvalds 
999b60c5115SThomas Graf static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
10001da177e4SLinus Torvalds {
10013b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
10027c28bd0bSEric Dumazet 	int h, s_h;
10037c28bd0bSEric Dumazet 	int idx = 0, s_idx;
10041da177e4SLinus Torvalds 	struct net_device *dev;
10057c28bd0bSEric Dumazet 	struct hlist_head *head;
10067c28bd0bSEric Dumazet 	struct hlist_node *node;
10071da177e4SLinus Torvalds 
10087c28bd0bSEric Dumazet 	s_h = cb->args[0];
10097c28bd0bSEric Dumazet 	s_idx = cb->args[1];
10107c28bd0bSEric Dumazet 
1011e67f88ddSEric Dumazet 	rcu_read_lock();
10127c28bd0bSEric Dumazet 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
10137562f876SPavel Emelianov 		idx = 0;
10147c28bd0bSEric Dumazet 		head = &net->dev_index_head[h];
1015e67f88ddSEric Dumazet 		hlist_for_each_entry_rcu(dev, node, head, index_hlist) {
10161da177e4SLinus Torvalds 			if (idx < s_idx)
10177562f876SPavel Emelianov 				goto cont;
1018575c3e2aSPatrick McHardy 			if (rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
1019b6544c0bSJamal Hadi Salim 					     NETLINK_CB(cb->skb).pid,
10207c28bd0bSEric Dumazet 					     cb->nlh->nlmsg_seq, 0,
10217c28bd0bSEric Dumazet 					     NLM_F_MULTI) <= 0)
10227c28bd0bSEric Dumazet 				goto out;
10237562f876SPavel Emelianov cont:
10247562f876SPavel Emelianov 			idx++;
10251da177e4SLinus Torvalds 		}
10267c28bd0bSEric Dumazet 	}
10277c28bd0bSEric Dumazet out:
1028e67f88ddSEric Dumazet 	rcu_read_unlock();
10297c28bd0bSEric Dumazet 	cb->args[1] = idx;
10307c28bd0bSEric Dumazet 	cb->args[0] = h;
10311da177e4SLinus Torvalds 
10321da177e4SLinus Torvalds 	return skb->len;
10331da177e4SLinus Torvalds }
10341da177e4SLinus Torvalds 
1035e7199288SPavel Emelianov const struct nla_policy ifla_policy[IFLA_MAX+1] = {
10365176f91eSThomas Graf 	[IFLA_IFNAME]		= { .type = NLA_STRING, .len = IFNAMSIZ-1 },
103738f7b870SPatrick McHardy 	[IFLA_ADDRESS]		= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
103838f7b870SPatrick McHardy 	[IFLA_BROADCAST]	= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
10395176f91eSThomas Graf 	[IFLA_MAP]		= { .len = sizeof(struct rtnl_link_ifmap) },
1040da5e0494SThomas Graf 	[IFLA_MTU]		= { .type = NLA_U32 },
104176e87306SThomas Graf 	[IFLA_LINK]		= { .type = NLA_U32 },
1042fbaec0eaSJiri Pirko 	[IFLA_MASTER]		= { .type = NLA_U32 },
1043da5e0494SThomas Graf 	[IFLA_TXQLEN]		= { .type = NLA_U32 },
1044da5e0494SThomas Graf 	[IFLA_WEIGHT]		= { .type = NLA_U32 },
1045da5e0494SThomas Graf 	[IFLA_OPERSTATE]	= { .type = NLA_U8 },
1046da5e0494SThomas Graf 	[IFLA_LINKMODE]		= { .type = NLA_U8 },
104776e87306SThomas Graf 	[IFLA_LINKINFO]		= { .type = NLA_NESTED },
1048d8a5ec67SEric W. Biederman 	[IFLA_NET_NS_PID]	= { .type = NLA_U32 },
10490b815a1aSStephen Hemminger 	[IFLA_IFALIAS]	        = { .type = NLA_STRING, .len = IFALIASZ-1 },
1050c02db8c6SChris Wright 	[IFLA_VFINFO_LIST]	= {. type = NLA_NESTED },
105157b61080SScott Feldman 	[IFLA_VF_PORTS]		= { .type = NLA_NESTED },
105257b61080SScott Feldman 	[IFLA_PORT_SELF]	= { .type = NLA_NESTED },
1053f8ff182cSThomas Graf 	[IFLA_AF_SPEC]		= { .type = NLA_NESTED },
1054da5e0494SThomas Graf };
1055e0d087afSEric Dumazet EXPORT_SYMBOL(ifla_policy);
10561da177e4SLinus Torvalds 
105738f7b870SPatrick McHardy static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
105838f7b870SPatrick McHardy 	[IFLA_INFO_KIND]	= { .type = NLA_STRING },
105938f7b870SPatrick McHardy 	[IFLA_INFO_DATA]	= { .type = NLA_NESTED },
106038f7b870SPatrick McHardy };
106138f7b870SPatrick McHardy 
1062c02db8c6SChris Wright static const struct nla_policy ifla_vfinfo_policy[IFLA_VF_INFO_MAX+1] = {
1063c02db8c6SChris Wright 	[IFLA_VF_INFO]		= { .type = NLA_NESTED },
1064c02db8c6SChris Wright };
1065c02db8c6SChris Wright 
1066c02db8c6SChris Wright static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1067c02db8c6SChris Wright 	[IFLA_VF_MAC]		= { .type = NLA_BINARY,
1068c02db8c6SChris Wright 				    .len = sizeof(struct ifla_vf_mac) },
1069c02db8c6SChris Wright 	[IFLA_VF_VLAN]		= { .type = NLA_BINARY,
1070c02db8c6SChris Wright 				    .len = sizeof(struct ifla_vf_vlan) },
1071c02db8c6SChris Wright 	[IFLA_VF_TX_RATE]	= { .type = NLA_BINARY,
1072c02db8c6SChris Wright 				    .len = sizeof(struct ifla_vf_tx_rate) },
1073c02db8c6SChris Wright };
1074c02db8c6SChris Wright 
107557b61080SScott Feldman static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
107657b61080SScott Feldman 	[IFLA_PORT_VF]		= { .type = NLA_U32 },
107757b61080SScott Feldman 	[IFLA_PORT_PROFILE]	= { .type = NLA_STRING,
107857b61080SScott Feldman 				    .len = PORT_PROFILE_MAX },
107957b61080SScott Feldman 	[IFLA_PORT_VSI_TYPE]	= { .type = NLA_BINARY,
108057b61080SScott Feldman 				    .len = sizeof(struct ifla_port_vsi)},
108157b61080SScott Feldman 	[IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
108257b61080SScott Feldman 				      .len = PORT_UUID_MAX },
108357b61080SScott Feldman 	[IFLA_PORT_HOST_UUID]	= { .type = NLA_STRING,
108457b61080SScott Feldman 				    .len = PORT_UUID_MAX },
108557b61080SScott Feldman 	[IFLA_PORT_REQUEST]	= { .type = NLA_U8, },
108657b61080SScott Feldman 	[IFLA_PORT_RESPONSE]	= { .type = NLA_U16, },
108757b61080SScott Feldman };
108857b61080SScott Feldman 
108981adee47SEric W. Biederman struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
109081adee47SEric W. Biederman {
109181adee47SEric W. Biederman 	struct net *net;
109281adee47SEric W. Biederman 	/* Examine the link attributes and figure out which
109381adee47SEric W. Biederman 	 * network namespace we are talking about.
109481adee47SEric W. Biederman 	 */
109581adee47SEric W. Biederman 	if (tb[IFLA_NET_NS_PID])
109681adee47SEric W. Biederman 		net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
109781adee47SEric W. Biederman 	else
109881adee47SEric W. Biederman 		net = get_net(src_net);
109981adee47SEric W. Biederman 	return net;
110081adee47SEric W. Biederman }
110181adee47SEric W. Biederman EXPORT_SYMBOL(rtnl_link_get_net);
110281adee47SEric W. Biederman 
11031840bb13SThomas Graf static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
11041840bb13SThomas Graf {
11051840bb13SThomas Graf 	if (dev) {
11061840bb13SThomas Graf 		if (tb[IFLA_ADDRESS] &&
11071840bb13SThomas Graf 		    nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
11081840bb13SThomas Graf 			return -EINVAL;
11091840bb13SThomas Graf 
11101840bb13SThomas Graf 		if (tb[IFLA_BROADCAST] &&
11111840bb13SThomas Graf 		    nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
11121840bb13SThomas Graf 			return -EINVAL;
11131840bb13SThomas Graf 	}
11141840bb13SThomas Graf 
1115cf7afbfeSThomas Graf 	if (tb[IFLA_AF_SPEC]) {
1116cf7afbfeSThomas Graf 		struct nlattr *af;
1117cf7afbfeSThomas Graf 		int rem, err;
1118cf7afbfeSThomas Graf 
1119cf7afbfeSThomas Graf 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1120cf7afbfeSThomas Graf 			const struct rtnl_af_ops *af_ops;
1121cf7afbfeSThomas Graf 
1122cf7afbfeSThomas Graf 			if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1123cf7afbfeSThomas Graf 				return -EAFNOSUPPORT;
1124cf7afbfeSThomas Graf 
1125cf7afbfeSThomas Graf 			if (!af_ops->set_link_af)
1126cf7afbfeSThomas Graf 				return -EOPNOTSUPP;
1127cf7afbfeSThomas Graf 
1128cf7afbfeSThomas Graf 			if (af_ops->validate_link_af) {
11296d3a9a68SKurt Van Dijck 				err = af_ops->validate_link_af(dev, af);
1130cf7afbfeSThomas Graf 				if (err < 0)
1131cf7afbfeSThomas Graf 					return err;
1132cf7afbfeSThomas Graf 			}
1133cf7afbfeSThomas Graf 		}
1134cf7afbfeSThomas Graf 	}
1135cf7afbfeSThomas Graf 
11361840bb13SThomas Graf 	return 0;
11371840bb13SThomas Graf }
11381840bb13SThomas Graf 
1139c02db8c6SChris Wright static int do_setvfinfo(struct net_device *dev, struct nlattr *attr)
1140c02db8c6SChris Wright {
1141c02db8c6SChris Wright 	int rem, err = -EINVAL;
1142c02db8c6SChris Wright 	struct nlattr *vf;
1143c02db8c6SChris Wright 	const struct net_device_ops *ops = dev->netdev_ops;
1144c02db8c6SChris Wright 
1145c02db8c6SChris Wright 	nla_for_each_nested(vf, attr, rem) {
1146c02db8c6SChris Wright 		switch (nla_type(vf)) {
1147c02db8c6SChris Wright 		case IFLA_VF_MAC: {
1148c02db8c6SChris Wright 			struct ifla_vf_mac *ivm;
1149c02db8c6SChris Wright 			ivm = nla_data(vf);
1150c02db8c6SChris Wright 			err = -EOPNOTSUPP;
1151c02db8c6SChris Wright 			if (ops->ndo_set_vf_mac)
1152c02db8c6SChris Wright 				err = ops->ndo_set_vf_mac(dev, ivm->vf,
1153c02db8c6SChris Wright 							  ivm->mac);
1154c02db8c6SChris Wright 			break;
1155c02db8c6SChris Wright 		}
1156c02db8c6SChris Wright 		case IFLA_VF_VLAN: {
1157c02db8c6SChris Wright 			struct ifla_vf_vlan *ivv;
1158c02db8c6SChris Wright 			ivv = nla_data(vf);
1159c02db8c6SChris Wright 			err = -EOPNOTSUPP;
1160c02db8c6SChris Wright 			if (ops->ndo_set_vf_vlan)
1161c02db8c6SChris Wright 				err = ops->ndo_set_vf_vlan(dev, ivv->vf,
1162c02db8c6SChris Wright 							   ivv->vlan,
1163c02db8c6SChris Wright 							   ivv->qos);
1164c02db8c6SChris Wright 			break;
1165c02db8c6SChris Wright 		}
1166c02db8c6SChris Wright 		case IFLA_VF_TX_RATE: {
1167c02db8c6SChris Wright 			struct ifla_vf_tx_rate *ivt;
1168c02db8c6SChris Wright 			ivt = nla_data(vf);
1169c02db8c6SChris Wright 			err = -EOPNOTSUPP;
1170c02db8c6SChris Wright 			if (ops->ndo_set_vf_tx_rate)
1171c02db8c6SChris Wright 				err = ops->ndo_set_vf_tx_rate(dev, ivt->vf,
1172c02db8c6SChris Wright 							      ivt->rate);
1173c02db8c6SChris Wright 			break;
1174c02db8c6SChris Wright 		}
1175c02db8c6SChris Wright 		default:
1176c02db8c6SChris Wright 			err = -EINVAL;
1177c02db8c6SChris Wright 			break;
1178c02db8c6SChris Wright 		}
1179c02db8c6SChris Wright 		if (err)
1180c02db8c6SChris Wright 			break;
1181c02db8c6SChris Wright 	}
1182c02db8c6SChris Wright 	return err;
1183c02db8c6SChris Wright }
1184c02db8c6SChris Wright 
1185fbaec0eaSJiri Pirko static int do_set_master(struct net_device *dev, int ifindex)
1186fbaec0eaSJiri Pirko {
1187fbaec0eaSJiri Pirko 	struct net_device *master_dev;
1188fbaec0eaSJiri Pirko 	const struct net_device_ops *ops;
1189fbaec0eaSJiri Pirko 	int err;
1190fbaec0eaSJiri Pirko 
1191fbaec0eaSJiri Pirko 	if (dev->master) {
1192fbaec0eaSJiri Pirko 		if (dev->master->ifindex == ifindex)
1193fbaec0eaSJiri Pirko 			return 0;
1194fbaec0eaSJiri Pirko 		ops = dev->master->netdev_ops;
1195fbaec0eaSJiri Pirko 		if (ops->ndo_del_slave) {
1196fbaec0eaSJiri Pirko 			err = ops->ndo_del_slave(dev->master, dev);
1197fbaec0eaSJiri Pirko 			if (err)
1198fbaec0eaSJiri Pirko 				return err;
1199fbaec0eaSJiri Pirko 		} else {
1200fbaec0eaSJiri Pirko 			return -EOPNOTSUPP;
1201fbaec0eaSJiri Pirko 		}
1202fbaec0eaSJiri Pirko 	}
1203fbaec0eaSJiri Pirko 
1204fbaec0eaSJiri Pirko 	if (ifindex) {
1205fbaec0eaSJiri Pirko 		master_dev = __dev_get_by_index(dev_net(dev), ifindex);
1206fbaec0eaSJiri Pirko 		if (!master_dev)
1207fbaec0eaSJiri Pirko 			return -EINVAL;
1208fbaec0eaSJiri Pirko 		ops = master_dev->netdev_ops;
1209fbaec0eaSJiri Pirko 		if (ops->ndo_add_slave) {
1210fbaec0eaSJiri Pirko 			err = ops->ndo_add_slave(master_dev, dev);
1211fbaec0eaSJiri Pirko 			if (err)
1212fbaec0eaSJiri Pirko 				return err;
1213fbaec0eaSJiri Pirko 		} else {
1214fbaec0eaSJiri Pirko 			return -EOPNOTSUPP;
1215fbaec0eaSJiri Pirko 		}
1216fbaec0eaSJiri Pirko 	}
1217fbaec0eaSJiri Pirko 	return 0;
1218fbaec0eaSJiri Pirko }
1219fbaec0eaSJiri Pirko 
12200157f60cSPatrick McHardy static int do_setlink(struct net_device *dev, struct ifinfomsg *ifm,
122138f7b870SPatrick McHardy 		      struct nlattr **tb, char *ifname, int modified)
12220157f60cSPatrick McHardy {
1223d314774cSStephen Hemminger 	const struct net_device_ops *ops = dev->netdev_ops;
122438f7b870SPatrick McHardy 	int send_addr_notify = 0;
12250157f60cSPatrick McHardy 	int err;
12260157f60cSPatrick McHardy 
1227d8a5ec67SEric W. Biederman 	if (tb[IFLA_NET_NS_PID]) {
122881adee47SEric W. Biederman 		struct net *net = rtnl_link_get_net(dev_net(dev), tb);
1229d8a5ec67SEric W. Biederman 		if (IS_ERR(net)) {
1230d8a5ec67SEric W. Biederman 			err = PTR_ERR(net);
1231d8a5ec67SEric W. Biederman 			goto errout;
1232d8a5ec67SEric W. Biederman 		}
1233d8a5ec67SEric W. Biederman 		err = dev_change_net_namespace(dev, net, ifname);
1234d8a5ec67SEric W. Biederman 		put_net(net);
1235d8a5ec67SEric W. Biederman 		if (err)
1236d8a5ec67SEric W. Biederman 			goto errout;
1237d8a5ec67SEric W. Biederman 		modified = 1;
1238d8a5ec67SEric W. Biederman 	}
1239d8a5ec67SEric W. Biederman 
12400157f60cSPatrick McHardy 	if (tb[IFLA_MAP]) {
12410157f60cSPatrick McHardy 		struct rtnl_link_ifmap *u_map;
12420157f60cSPatrick McHardy 		struct ifmap k_map;
12430157f60cSPatrick McHardy 
1244d314774cSStephen Hemminger 		if (!ops->ndo_set_config) {
12450157f60cSPatrick McHardy 			err = -EOPNOTSUPP;
12460157f60cSPatrick McHardy 			goto errout;
12470157f60cSPatrick McHardy 		}
12480157f60cSPatrick McHardy 
12490157f60cSPatrick McHardy 		if (!netif_device_present(dev)) {
12500157f60cSPatrick McHardy 			err = -ENODEV;
12510157f60cSPatrick McHardy 			goto errout;
12520157f60cSPatrick McHardy 		}
12530157f60cSPatrick McHardy 
12540157f60cSPatrick McHardy 		u_map = nla_data(tb[IFLA_MAP]);
12550157f60cSPatrick McHardy 		k_map.mem_start = (unsigned long) u_map->mem_start;
12560157f60cSPatrick McHardy 		k_map.mem_end = (unsigned long) u_map->mem_end;
12570157f60cSPatrick McHardy 		k_map.base_addr = (unsigned short) u_map->base_addr;
12580157f60cSPatrick McHardy 		k_map.irq = (unsigned char) u_map->irq;
12590157f60cSPatrick McHardy 		k_map.dma = (unsigned char) u_map->dma;
12600157f60cSPatrick McHardy 		k_map.port = (unsigned char) u_map->port;
12610157f60cSPatrick McHardy 
1262d314774cSStephen Hemminger 		err = ops->ndo_set_config(dev, &k_map);
12630157f60cSPatrick McHardy 		if (err < 0)
12640157f60cSPatrick McHardy 			goto errout;
12650157f60cSPatrick McHardy 
12660157f60cSPatrick McHardy 		modified = 1;
12670157f60cSPatrick McHardy 	}
12680157f60cSPatrick McHardy 
12690157f60cSPatrick McHardy 	if (tb[IFLA_ADDRESS]) {
12700157f60cSPatrick McHardy 		struct sockaddr *sa;
12710157f60cSPatrick McHardy 		int len;
12720157f60cSPatrick McHardy 
1273d314774cSStephen Hemminger 		if (!ops->ndo_set_mac_address) {
12740157f60cSPatrick McHardy 			err = -EOPNOTSUPP;
12750157f60cSPatrick McHardy 			goto errout;
12760157f60cSPatrick McHardy 		}
12770157f60cSPatrick McHardy 
12780157f60cSPatrick McHardy 		if (!netif_device_present(dev)) {
12790157f60cSPatrick McHardy 			err = -ENODEV;
12800157f60cSPatrick McHardy 			goto errout;
12810157f60cSPatrick McHardy 		}
12820157f60cSPatrick McHardy 
12830157f60cSPatrick McHardy 		len = sizeof(sa_family_t) + dev->addr_len;
12840157f60cSPatrick McHardy 		sa = kmalloc(len, GFP_KERNEL);
12850157f60cSPatrick McHardy 		if (!sa) {
12860157f60cSPatrick McHardy 			err = -ENOMEM;
12870157f60cSPatrick McHardy 			goto errout;
12880157f60cSPatrick McHardy 		}
12890157f60cSPatrick McHardy 		sa->sa_family = dev->type;
12900157f60cSPatrick McHardy 		memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
12910157f60cSPatrick McHardy 		       dev->addr_len);
1292d314774cSStephen Hemminger 		err = ops->ndo_set_mac_address(dev, sa);
12930157f60cSPatrick McHardy 		kfree(sa);
12940157f60cSPatrick McHardy 		if (err)
12950157f60cSPatrick McHardy 			goto errout;
12960157f60cSPatrick McHardy 		send_addr_notify = 1;
12970157f60cSPatrick McHardy 		modified = 1;
12980157f60cSPatrick McHardy 	}
12990157f60cSPatrick McHardy 
13000157f60cSPatrick McHardy 	if (tb[IFLA_MTU]) {
13010157f60cSPatrick McHardy 		err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
13020157f60cSPatrick McHardy 		if (err < 0)
13030157f60cSPatrick McHardy 			goto errout;
13040157f60cSPatrick McHardy 		modified = 1;
13050157f60cSPatrick McHardy 	}
13060157f60cSPatrick McHardy 
1307cbda10faSVlad Dogaru 	if (tb[IFLA_GROUP]) {
1308cbda10faSVlad Dogaru 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
1309cbda10faSVlad Dogaru 		modified = 1;
1310cbda10faSVlad Dogaru 	}
1311cbda10faSVlad Dogaru 
13120157f60cSPatrick McHardy 	/*
13130157f60cSPatrick McHardy 	 * Interface selected by interface index but interface
13140157f60cSPatrick McHardy 	 * name provided implies that a name change has been
13150157f60cSPatrick McHardy 	 * requested.
13160157f60cSPatrick McHardy 	 */
13170157f60cSPatrick McHardy 	if (ifm->ifi_index > 0 && ifname[0]) {
13180157f60cSPatrick McHardy 		err = dev_change_name(dev, ifname);
13190157f60cSPatrick McHardy 		if (err < 0)
13200157f60cSPatrick McHardy 			goto errout;
13210157f60cSPatrick McHardy 		modified = 1;
13220157f60cSPatrick McHardy 	}
13230157f60cSPatrick McHardy 
13240b815a1aSStephen Hemminger 	if (tb[IFLA_IFALIAS]) {
13250b815a1aSStephen Hemminger 		err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
13260b815a1aSStephen Hemminger 				    nla_len(tb[IFLA_IFALIAS]));
13270b815a1aSStephen Hemminger 		if (err < 0)
13280b815a1aSStephen Hemminger 			goto errout;
13290b815a1aSStephen Hemminger 		modified = 1;
13300b815a1aSStephen Hemminger 	}
13310b815a1aSStephen Hemminger 
13320157f60cSPatrick McHardy 	if (tb[IFLA_BROADCAST]) {
13330157f60cSPatrick McHardy 		nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
13340157f60cSPatrick McHardy 		send_addr_notify = 1;
13350157f60cSPatrick McHardy 	}
13360157f60cSPatrick McHardy 
13370157f60cSPatrick McHardy 	if (ifm->ifi_flags || ifm->ifi_change) {
13383729d502SPatrick McHardy 		err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
13395f9021cfSJohannes Berg 		if (err < 0)
13405f9021cfSJohannes Berg 			goto errout;
13410157f60cSPatrick McHardy 	}
13420157f60cSPatrick McHardy 
1343fbaec0eaSJiri Pirko 	if (tb[IFLA_MASTER]) {
1344fbaec0eaSJiri Pirko 		err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
1345fbaec0eaSJiri Pirko 		if (err)
1346fbaec0eaSJiri Pirko 			goto errout;
1347fbaec0eaSJiri Pirko 		modified = 1;
1348fbaec0eaSJiri Pirko 	}
1349fbaec0eaSJiri Pirko 
135093b2d4a2SDavid S. Miller 	if (tb[IFLA_TXQLEN])
13510157f60cSPatrick McHardy 		dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
13520157f60cSPatrick McHardy 
13530157f60cSPatrick McHardy 	if (tb[IFLA_OPERSTATE])
135493b2d4a2SDavid S. Miller 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
13550157f60cSPatrick McHardy 
13560157f60cSPatrick McHardy 	if (tb[IFLA_LINKMODE]) {
13570157f60cSPatrick McHardy 		write_lock_bh(&dev_base_lock);
13580157f60cSPatrick McHardy 		dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
135993b2d4a2SDavid S. Miller 		write_unlock_bh(&dev_base_lock);
13600157f60cSPatrick McHardy 	}
13610157f60cSPatrick McHardy 
1362c02db8c6SChris Wright 	if (tb[IFLA_VFINFO_LIST]) {
1363c02db8c6SChris Wright 		struct nlattr *attr;
1364c02db8c6SChris Wright 		int rem;
1365c02db8c6SChris Wright 		nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
1366253683bbSDavid Howells 			if (nla_type(attr) != IFLA_VF_INFO) {
1367253683bbSDavid Howells 				err = -EINVAL;
1368c02db8c6SChris Wright 				goto errout;
1369253683bbSDavid Howells 			}
1370c02db8c6SChris Wright 			err = do_setvfinfo(dev, attr);
1371ebc08a6fSWilliams, Mitch A 			if (err < 0)
1372ebc08a6fSWilliams, Mitch A 				goto errout;
1373ebc08a6fSWilliams, Mitch A 			modified = 1;
1374ebc08a6fSWilliams, Mitch A 		}
1375ebc08a6fSWilliams, Mitch A 	}
13760157f60cSPatrick McHardy 	err = 0;
13770157f60cSPatrick McHardy 
137857b61080SScott Feldman 	if (tb[IFLA_VF_PORTS]) {
137957b61080SScott Feldman 		struct nlattr *port[IFLA_PORT_MAX+1];
138057b61080SScott Feldman 		struct nlattr *attr;
138157b61080SScott Feldman 		int vf;
138257b61080SScott Feldman 		int rem;
138357b61080SScott Feldman 
138457b61080SScott Feldman 		err = -EOPNOTSUPP;
138557b61080SScott Feldman 		if (!ops->ndo_set_vf_port)
138657b61080SScott Feldman 			goto errout;
138757b61080SScott Feldman 
138857b61080SScott Feldman 		nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
138957b61080SScott Feldman 			if (nla_type(attr) != IFLA_VF_PORT)
139057b61080SScott Feldman 				continue;
139157b61080SScott Feldman 			err = nla_parse_nested(port, IFLA_PORT_MAX,
139257b61080SScott Feldman 				attr, ifla_port_policy);
139357b61080SScott Feldman 			if (err < 0)
139457b61080SScott Feldman 				goto errout;
139557b61080SScott Feldman 			if (!port[IFLA_PORT_VF]) {
139657b61080SScott Feldman 				err = -EOPNOTSUPP;
139757b61080SScott Feldman 				goto errout;
139857b61080SScott Feldman 			}
139957b61080SScott Feldman 			vf = nla_get_u32(port[IFLA_PORT_VF]);
140057b61080SScott Feldman 			err = ops->ndo_set_vf_port(dev, vf, port);
140157b61080SScott Feldman 			if (err < 0)
140257b61080SScott Feldman 				goto errout;
140357b61080SScott Feldman 			modified = 1;
140457b61080SScott Feldman 		}
140557b61080SScott Feldman 	}
140657b61080SScott Feldman 	err = 0;
140757b61080SScott Feldman 
140857b61080SScott Feldman 	if (tb[IFLA_PORT_SELF]) {
140957b61080SScott Feldman 		struct nlattr *port[IFLA_PORT_MAX+1];
141057b61080SScott Feldman 
141157b61080SScott Feldman 		err = nla_parse_nested(port, IFLA_PORT_MAX,
141257b61080SScott Feldman 			tb[IFLA_PORT_SELF], ifla_port_policy);
141357b61080SScott Feldman 		if (err < 0)
141457b61080SScott Feldman 			goto errout;
141557b61080SScott Feldman 
141657b61080SScott Feldman 		err = -EOPNOTSUPP;
141757b61080SScott Feldman 		if (ops->ndo_set_vf_port)
141857b61080SScott Feldman 			err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
141957b61080SScott Feldman 		if (err < 0)
142057b61080SScott Feldman 			goto errout;
142157b61080SScott Feldman 		modified = 1;
142257b61080SScott Feldman 	}
1423f8ff182cSThomas Graf 
1424f8ff182cSThomas Graf 	if (tb[IFLA_AF_SPEC]) {
1425f8ff182cSThomas Graf 		struct nlattr *af;
1426f8ff182cSThomas Graf 		int rem;
1427f8ff182cSThomas Graf 
1428f8ff182cSThomas Graf 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1429f8ff182cSThomas Graf 			const struct rtnl_af_ops *af_ops;
1430f8ff182cSThomas Graf 
1431f8ff182cSThomas Graf 			if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1432cf7afbfeSThomas Graf 				BUG();
1433f8ff182cSThomas Graf 
1434cf7afbfeSThomas Graf 			err = af_ops->set_link_af(dev, af);
1435f8ff182cSThomas Graf 			if (err < 0)
1436f8ff182cSThomas Graf 				goto errout;
1437f8ff182cSThomas Graf 
1438f8ff182cSThomas Graf 			modified = 1;
1439f8ff182cSThomas Graf 		}
1440f8ff182cSThomas Graf 	}
144157b61080SScott Feldman 	err = 0;
144257b61080SScott Feldman 
14430157f60cSPatrick McHardy errout:
14440157f60cSPatrick McHardy 	if (err < 0 && modified && net_ratelimit())
14450157f60cSPatrick McHardy 		printk(KERN_WARNING "A link change request failed with "
144625985edcSLucas De Marchi 		       "some changes committed already. Interface %s may "
14470157f60cSPatrick McHardy 		       "have been left with an inconsistent configuration, "
14480157f60cSPatrick McHardy 		       "please check.\n", dev->name);
14490157f60cSPatrick McHardy 
14500157f60cSPatrick McHardy 	if (send_addr_notify)
14510157f60cSPatrick McHardy 		call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
14520157f60cSPatrick McHardy 	return err;
14530157f60cSPatrick McHardy }
14540157f60cSPatrick McHardy 
1455da5e0494SThomas Graf static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1456da5e0494SThomas Graf {
14573b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
1458da5e0494SThomas Graf 	struct ifinfomsg *ifm;
1459da5e0494SThomas Graf 	struct net_device *dev;
14600157f60cSPatrick McHardy 	int err;
1461da5e0494SThomas Graf 	struct nlattr *tb[IFLA_MAX+1];
14621da177e4SLinus Torvalds 	char ifname[IFNAMSIZ];
14631da177e4SLinus Torvalds 
1464da5e0494SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1465da5e0494SThomas Graf 	if (err < 0)
1466da5e0494SThomas Graf 		goto errout;
14671da177e4SLinus Torvalds 
14685176f91eSThomas Graf 	if (tb[IFLA_IFNAME])
14695176f91eSThomas Graf 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
147078e5b891SPatrick McHardy 	else
147178e5b891SPatrick McHardy 		ifname[0] = '\0';
14721da177e4SLinus Torvalds 
14731da177e4SLinus Torvalds 	err = -EINVAL;
1474da5e0494SThomas Graf 	ifm = nlmsg_data(nlh);
147551055be8SPatrick McHardy 	if (ifm->ifi_index > 0)
1476a3d12891SEric Dumazet 		dev = __dev_get_by_index(net, ifm->ifi_index);
1477da5e0494SThomas Graf 	else if (tb[IFLA_IFNAME])
1478a3d12891SEric Dumazet 		dev = __dev_get_by_name(net, ifname);
1479da5e0494SThomas Graf 	else
1480da5e0494SThomas Graf 		goto errout;
14811da177e4SLinus Torvalds 
1482da5e0494SThomas Graf 	if (dev == NULL) {
1483da5e0494SThomas Graf 		err = -ENODEV;
1484da5e0494SThomas Graf 		goto errout;
1485da5e0494SThomas Graf 	}
14861da177e4SLinus Torvalds 
1487e0d087afSEric Dumazet 	err = validate_linkmsg(dev, tb);
1488e0d087afSEric Dumazet 	if (err < 0)
1489a3d12891SEric Dumazet 		goto errout;
1490da5e0494SThomas Graf 
149138f7b870SPatrick McHardy 	err = do_setlink(dev, ifm, tb, ifname, 0);
1492da5e0494SThomas Graf errout:
14931da177e4SLinus Torvalds 	return err;
14941da177e4SLinus Torvalds }
14951da177e4SLinus Torvalds 
149638f7b870SPatrick McHardy static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
149738f7b870SPatrick McHardy {
14983b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
149938f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops;
150038f7b870SPatrick McHardy 	struct net_device *dev;
150138f7b870SPatrick McHardy 	struct ifinfomsg *ifm;
150238f7b870SPatrick McHardy 	char ifname[IFNAMSIZ];
150338f7b870SPatrick McHardy 	struct nlattr *tb[IFLA_MAX+1];
150438f7b870SPatrick McHardy 	int err;
1505226bd341SEric Dumazet 	LIST_HEAD(list_kill);
150638f7b870SPatrick McHardy 
150738f7b870SPatrick McHardy 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
150838f7b870SPatrick McHardy 	if (err < 0)
150938f7b870SPatrick McHardy 		return err;
151038f7b870SPatrick McHardy 
151138f7b870SPatrick McHardy 	if (tb[IFLA_IFNAME])
151238f7b870SPatrick McHardy 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
151338f7b870SPatrick McHardy 
151438f7b870SPatrick McHardy 	ifm = nlmsg_data(nlh);
151538f7b870SPatrick McHardy 	if (ifm->ifi_index > 0)
1516881d966bSEric W. Biederman 		dev = __dev_get_by_index(net, ifm->ifi_index);
151738f7b870SPatrick McHardy 	else if (tb[IFLA_IFNAME])
1518881d966bSEric W. Biederman 		dev = __dev_get_by_name(net, ifname);
151938f7b870SPatrick McHardy 	else
152038f7b870SPatrick McHardy 		return -EINVAL;
152138f7b870SPatrick McHardy 
152238f7b870SPatrick McHardy 	if (!dev)
152338f7b870SPatrick McHardy 		return -ENODEV;
152438f7b870SPatrick McHardy 
152538f7b870SPatrick McHardy 	ops = dev->rtnl_link_ops;
152638f7b870SPatrick McHardy 	if (!ops)
152738f7b870SPatrick McHardy 		return -EOPNOTSUPP;
152838f7b870SPatrick McHardy 
1529226bd341SEric Dumazet 	ops->dellink(dev, &list_kill);
1530226bd341SEric Dumazet 	unregister_netdevice_many(&list_kill);
1531226bd341SEric Dumazet 	list_del(&list_kill);
153238f7b870SPatrick McHardy 	return 0;
153338f7b870SPatrick McHardy }
153438f7b870SPatrick McHardy 
15353729d502SPatrick McHardy int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
15363729d502SPatrick McHardy {
15373729d502SPatrick McHardy 	unsigned int old_flags;
15383729d502SPatrick McHardy 	int err;
15393729d502SPatrick McHardy 
15403729d502SPatrick McHardy 	old_flags = dev->flags;
15413729d502SPatrick McHardy 	if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
15423729d502SPatrick McHardy 		err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
15433729d502SPatrick McHardy 		if (err < 0)
15443729d502SPatrick McHardy 			return err;
15453729d502SPatrick McHardy 	}
15463729d502SPatrick McHardy 
15473729d502SPatrick McHardy 	dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
15483729d502SPatrick McHardy 	rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
15493729d502SPatrick McHardy 
15503729d502SPatrick McHardy 	__dev_notify_flags(dev, old_flags);
15513729d502SPatrick McHardy 	return 0;
15523729d502SPatrick McHardy }
15533729d502SPatrick McHardy EXPORT_SYMBOL(rtnl_configure_link);
15543729d502SPatrick McHardy 
155581adee47SEric W. Biederman struct net_device *rtnl_create_link(struct net *src_net, struct net *net,
155681adee47SEric W. Biederman 	char *ifname, const struct rtnl_link_ops *ops, struct nlattr *tb[])
1557e7199288SPavel Emelianov {
1558e7199288SPavel Emelianov 	int err;
1559e7199288SPavel Emelianov 	struct net_device *dev;
15602e59af3dSEric Dumazet 	unsigned int num_queues = 1;
15612e59af3dSEric Dumazet 	unsigned int real_num_queues = 1;
1562e7199288SPavel Emelianov 
15632e59af3dSEric Dumazet 	if (ops->get_tx_queues) {
156481adee47SEric W. Biederman 		err = ops->get_tx_queues(src_net, tb, &num_queues,
1565e0d087afSEric Dumazet 					 &real_num_queues);
15662e59af3dSEric Dumazet 		if (err)
15672e59af3dSEric Dumazet 			goto err;
15682e59af3dSEric Dumazet 	}
1569e7199288SPavel Emelianov 	err = -ENOMEM;
15702e59af3dSEric Dumazet 	dev = alloc_netdev_mq(ops->priv_size, ifname, ops->setup, num_queues);
1571e7199288SPavel Emelianov 	if (!dev)
1572e7199288SPavel Emelianov 		goto err;
1573e7199288SPavel Emelianov 
157481adee47SEric W. Biederman 	dev_net_set(dev, net);
157581adee47SEric W. Biederman 	dev->rtnl_link_ops = ops;
15763729d502SPatrick McHardy 	dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
15772e59af3dSEric Dumazet 	dev->real_num_tx_queues = real_num_queues;
157881adee47SEric W. Biederman 
1579e7199288SPavel Emelianov 	if (tb[IFLA_MTU])
1580e7199288SPavel Emelianov 		dev->mtu = nla_get_u32(tb[IFLA_MTU]);
1581e7199288SPavel Emelianov 	if (tb[IFLA_ADDRESS])
1582e7199288SPavel Emelianov 		memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
1583e7199288SPavel Emelianov 				nla_len(tb[IFLA_ADDRESS]));
1584e7199288SPavel Emelianov 	if (tb[IFLA_BROADCAST])
1585e7199288SPavel Emelianov 		memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
1586e7199288SPavel Emelianov 				nla_len(tb[IFLA_BROADCAST]));
1587e7199288SPavel Emelianov 	if (tb[IFLA_TXQLEN])
1588e7199288SPavel Emelianov 		dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
1589e7199288SPavel Emelianov 	if (tb[IFLA_OPERSTATE])
159093b2d4a2SDavid S. Miller 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
1591e7199288SPavel Emelianov 	if (tb[IFLA_LINKMODE])
1592e7199288SPavel Emelianov 		dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
1593ffa934f1SPatrick McHardy 	if (tb[IFLA_GROUP])
1594ffa934f1SPatrick McHardy 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
1595e7199288SPavel Emelianov 
1596e7199288SPavel Emelianov 	return dev;
1597e7199288SPavel Emelianov 
1598e7199288SPavel Emelianov err:
1599e7199288SPavel Emelianov 	return ERR_PTR(err);
1600e7199288SPavel Emelianov }
1601e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_create_link);
1602e7199288SPavel Emelianov 
1603e7ed828fSVlad Dogaru static int rtnl_group_changelink(struct net *net, int group,
1604e7ed828fSVlad Dogaru 		struct ifinfomsg *ifm,
1605e7ed828fSVlad Dogaru 		struct nlattr **tb)
1606e7ed828fSVlad Dogaru {
1607e7ed828fSVlad Dogaru 	struct net_device *dev;
1608e7ed828fSVlad Dogaru 	int err;
1609e7ed828fSVlad Dogaru 
1610e7ed828fSVlad Dogaru 	for_each_netdev(net, dev) {
1611e7ed828fSVlad Dogaru 		if (dev->group == group) {
1612e7ed828fSVlad Dogaru 			err = do_setlink(dev, ifm, tb, NULL, 0);
1613e7ed828fSVlad Dogaru 			if (err < 0)
1614e7ed828fSVlad Dogaru 				return err;
1615e7ed828fSVlad Dogaru 		}
1616e7ed828fSVlad Dogaru 	}
1617e7ed828fSVlad Dogaru 
1618e7ed828fSVlad Dogaru 	return 0;
1619e7ed828fSVlad Dogaru }
1620e7ed828fSVlad Dogaru 
162138f7b870SPatrick McHardy static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
162238f7b870SPatrick McHardy {
16233b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
162438f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops;
162538f7b870SPatrick McHardy 	struct net_device *dev;
162638f7b870SPatrick McHardy 	struct ifinfomsg *ifm;
162738f7b870SPatrick McHardy 	char kind[MODULE_NAME_LEN];
162838f7b870SPatrick McHardy 	char ifname[IFNAMSIZ];
162938f7b870SPatrick McHardy 	struct nlattr *tb[IFLA_MAX+1];
163038f7b870SPatrick McHardy 	struct nlattr *linkinfo[IFLA_INFO_MAX+1];
163138f7b870SPatrick McHardy 	int err;
163238f7b870SPatrick McHardy 
163395a5afcaSJohannes Berg #ifdef CONFIG_MODULES
163438f7b870SPatrick McHardy replay:
16358072f085SThomas Graf #endif
163638f7b870SPatrick McHardy 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
163738f7b870SPatrick McHardy 	if (err < 0)
163838f7b870SPatrick McHardy 		return err;
163938f7b870SPatrick McHardy 
164038f7b870SPatrick McHardy 	if (tb[IFLA_IFNAME])
164138f7b870SPatrick McHardy 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
164238f7b870SPatrick McHardy 	else
164338f7b870SPatrick McHardy 		ifname[0] = '\0';
164438f7b870SPatrick McHardy 
164538f7b870SPatrick McHardy 	ifm = nlmsg_data(nlh);
164638f7b870SPatrick McHardy 	if (ifm->ifi_index > 0)
1647881d966bSEric W. Biederman 		dev = __dev_get_by_index(net, ifm->ifi_index);
1648e7ed828fSVlad Dogaru 	else {
1649e7ed828fSVlad Dogaru 		if (ifname[0])
1650881d966bSEric W. Biederman 			dev = __dev_get_by_name(net, ifname);
165138f7b870SPatrick McHardy 		else
165238f7b870SPatrick McHardy 			dev = NULL;
1653e7ed828fSVlad Dogaru 	}
165438f7b870SPatrick McHardy 
1655e0d087afSEric Dumazet 	err = validate_linkmsg(dev, tb);
1656e0d087afSEric Dumazet 	if (err < 0)
16571840bb13SThomas Graf 		return err;
16581840bb13SThomas Graf 
165938f7b870SPatrick McHardy 	if (tb[IFLA_LINKINFO]) {
166038f7b870SPatrick McHardy 		err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
166138f7b870SPatrick McHardy 				       tb[IFLA_LINKINFO], ifla_info_policy);
166238f7b870SPatrick McHardy 		if (err < 0)
166338f7b870SPatrick McHardy 			return err;
166438f7b870SPatrick McHardy 	} else
166538f7b870SPatrick McHardy 		memset(linkinfo, 0, sizeof(linkinfo));
166638f7b870SPatrick McHardy 
166738f7b870SPatrick McHardy 	if (linkinfo[IFLA_INFO_KIND]) {
166838f7b870SPatrick McHardy 		nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
166938f7b870SPatrick McHardy 		ops = rtnl_link_ops_get(kind);
167038f7b870SPatrick McHardy 	} else {
167138f7b870SPatrick McHardy 		kind[0] = '\0';
167238f7b870SPatrick McHardy 		ops = NULL;
167338f7b870SPatrick McHardy 	}
167438f7b870SPatrick McHardy 
167538f7b870SPatrick McHardy 	if (1) {
167638f7b870SPatrick McHardy 		struct nlattr *attr[ops ? ops->maxtype + 1 : 0], **data = NULL;
167781adee47SEric W. Biederman 		struct net *dest_net;
167838f7b870SPatrick McHardy 
167938f7b870SPatrick McHardy 		if (ops) {
168038f7b870SPatrick McHardy 			if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
168138f7b870SPatrick McHardy 				err = nla_parse_nested(attr, ops->maxtype,
168238f7b870SPatrick McHardy 						       linkinfo[IFLA_INFO_DATA],
168338f7b870SPatrick McHardy 						       ops->policy);
168438f7b870SPatrick McHardy 				if (err < 0)
168538f7b870SPatrick McHardy 					return err;
168638f7b870SPatrick McHardy 				data = attr;
168738f7b870SPatrick McHardy 			}
168838f7b870SPatrick McHardy 			if (ops->validate) {
168938f7b870SPatrick McHardy 				err = ops->validate(tb, data);
169038f7b870SPatrick McHardy 				if (err < 0)
169138f7b870SPatrick McHardy 					return err;
169238f7b870SPatrick McHardy 			}
169338f7b870SPatrick McHardy 		}
169438f7b870SPatrick McHardy 
169538f7b870SPatrick McHardy 		if (dev) {
169638f7b870SPatrick McHardy 			int modified = 0;
169738f7b870SPatrick McHardy 
169838f7b870SPatrick McHardy 			if (nlh->nlmsg_flags & NLM_F_EXCL)
169938f7b870SPatrick McHardy 				return -EEXIST;
170038f7b870SPatrick McHardy 			if (nlh->nlmsg_flags & NLM_F_REPLACE)
170138f7b870SPatrick McHardy 				return -EOPNOTSUPP;
170238f7b870SPatrick McHardy 
170338f7b870SPatrick McHardy 			if (linkinfo[IFLA_INFO_DATA]) {
170438f7b870SPatrick McHardy 				if (!ops || ops != dev->rtnl_link_ops ||
170538f7b870SPatrick McHardy 				    !ops->changelink)
170638f7b870SPatrick McHardy 					return -EOPNOTSUPP;
170738f7b870SPatrick McHardy 
170838f7b870SPatrick McHardy 				err = ops->changelink(dev, tb, data);
170938f7b870SPatrick McHardy 				if (err < 0)
171038f7b870SPatrick McHardy 					return err;
171138f7b870SPatrick McHardy 				modified = 1;
171238f7b870SPatrick McHardy 			}
171338f7b870SPatrick McHardy 
171438f7b870SPatrick McHardy 			return do_setlink(dev, ifm, tb, ifname, modified);
171538f7b870SPatrick McHardy 		}
171638f7b870SPatrick McHardy 
1717ffa934f1SPatrick McHardy 		if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
1718ffa934f1SPatrick McHardy 			if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
1719ffa934f1SPatrick McHardy 				return rtnl_group_changelink(net,
1720ffa934f1SPatrick McHardy 						nla_get_u32(tb[IFLA_GROUP]),
1721ffa934f1SPatrick McHardy 						ifm, tb);
172238f7b870SPatrick McHardy 			return -ENODEV;
1723ffa934f1SPatrick McHardy 		}
172438f7b870SPatrick McHardy 
17253729d502SPatrick McHardy 		if (ifm->ifi_index)
172638f7b870SPatrick McHardy 			return -EOPNOTSUPP;
17270e06877cSPatrick McHardy 		if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
172838f7b870SPatrick McHardy 			return -EOPNOTSUPP;
172938f7b870SPatrick McHardy 
173038f7b870SPatrick McHardy 		if (!ops) {
173195a5afcaSJohannes Berg #ifdef CONFIG_MODULES
173238f7b870SPatrick McHardy 			if (kind[0]) {
173338f7b870SPatrick McHardy 				__rtnl_unlock();
173438f7b870SPatrick McHardy 				request_module("rtnl-link-%s", kind);
173538f7b870SPatrick McHardy 				rtnl_lock();
173638f7b870SPatrick McHardy 				ops = rtnl_link_ops_get(kind);
173738f7b870SPatrick McHardy 				if (ops)
173838f7b870SPatrick McHardy 					goto replay;
173938f7b870SPatrick McHardy 			}
174038f7b870SPatrick McHardy #endif
174138f7b870SPatrick McHardy 			return -EOPNOTSUPP;
174238f7b870SPatrick McHardy 		}
174338f7b870SPatrick McHardy 
174438f7b870SPatrick McHardy 		if (!ifname[0])
174538f7b870SPatrick McHardy 			snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
174638f7b870SPatrick McHardy 
174781adee47SEric W. Biederman 		dest_net = rtnl_link_get_net(net, tb);
174813ad1774SEric W. Biederman 		if (IS_ERR(dest_net))
174913ad1774SEric W. Biederman 			return PTR_ERR(dest_net);
175013ad1774SEric W. Biederman 
175181adee47SEric W. Biederman 		dev = rtnl_create_link(net, dest_net, ifname, ops, tb);
175238f7b870SPatrick McHardy 
1753e7199288SPavel Emelianov 		if (IS_ERR(dev))
1754e7199288SPavel Emelianov 			err = PTR_ERR(dev);
1755e7199288SPavel Emelianov 		else if (ops->newlink)
175681adee47SEric W. Biederman 			err = ops->newlink(net, dev, tb, data);
17572d85cba2SPatrick McHardy 		else
17582d85cba2SPatrick McHardy 			err = register_netdevice(dev);
175980032cffSDan Carpenter 
176080032cffSDan Carpenter 		if (err < 0 && !IS_ERR(dev))
176138f7b870SPatrick McHardy 			free_netdev(dev);
176280032cffSDan Carpenter 		if (err < 0)
17633729d502SPatrick McHardy 			goto out;
176481adee47SEric W. Biederman 
17653729d502SPatrick McHardy 		err = rtnl_configure_link(dev, ifm);
17663729d502SPatrick McHardy 		if (err < 0)
17673729d502SPatrick McHardy 			unregister_netdevice(dev);
17683729d502SPatrick McHardy out:
176981adee47SEric W. Biederman 		put_net(dest_net);
177038f7b870SPatrick McHardy 		return err;
177138f7b870SPatrick McHardy 	}
177238f7b870SPatrick McHardy }
177338f7b870SPatrick McHardy 
1774b60c5115SThomas Graf static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
1775711e2c33SJean Tourrilhes {
17763b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
1777b60c5115SThomas Graf 	struct ifinfomsg *ifm;
1778a3d12891SEric Dumazet 	char ifname[IFNAMSIZ];
1779b60c5115SThomas Graf 	struct nlattr *tb[IFLA_MAX+1];
1780b60c5115SThomas Graf 	struct net_device *dev = NULL;
1781b60c5115SThomas Graf 	struct sk_buff *nskb;
1782339bf98fSThomas Graf 	int err;
1783711e2c33SJean Tourrilhes 
1784b60c5115SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1785b60c5115SThomas Graf 	if (err < 0)
17869918f230SEric Sesterhenn 		return err;
1787b60c5115SThomas Graf 
1788a3d12891SEric Dumazet 	if (tb[IFLA_IFNAME])
1789a3d12891SEric Dumazet 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1790a3d12891SEric Dumazet 
1791b60c5115SThomas Graf 	ifm = nlmsg_data(nlh);
1792a3d12891SEric Dumazet 	if (ifm->ifi_index > 0)
1793a3d12891SEric Dumazet 		dev = __dev_get_by_index(net, ifm->ifi_index);
1794a3d12891SEric Dumazet 	else if (tb[IFLA_IFNAME])
1795a3d12891SEric Dumazet 		dev = __dev_get_by_name(net, ifname);
1796a3d12891SEric Dumazet 	else
1797b60c5115SThomas Graf 		return -EINVAL;
1798b60c5115SThomas Graf 
1799a3d12891SEric Dumazet 	if (dev == NULL)
1800a3d12891SEric Dumazet 		return -ENODEV;
1801a3d12891SEric Dumazet 
180238f7b870SPatrick McHardy 	nskb = nlmsg_new(if_nlmsg_size(dev), GFP_KERNEL);
1803a3d12891SEric Dumazet 	if (nskb == NULL)
1804a3d12891SEric Dumazet 		return -ENOBUFS;
1805711e2c33SJean Tourrilhes 
1806575c3e2aSPatrick McHardy 	err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).pid,
1807575c3e2aSPatrick McHardy 			       nlh->nlmsg_seq, 0, 0);
180826932566SPatrick McHardy 	if (err < 0) {
180926932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in if_nlmsg_size */
181026932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
181126932566SPatrick McHardy 		kfree_skb(nskb);
1812a3d12891SEric Dumazet 	} else
181397c53cacSDenis V. Lunev 		err = rtnl_unicast(nskb, net, NETLINK_CB(skb).pid);
1814b60c5115SThomas Graf 
1815711e2c33SJean Tourrilhes 	return err;
1816711e2c33SJean Tourrilhes }
1817711e2c33SJean Tourrilhes 
181842bad1daSAdrian Bunk static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
18191da177e4SLinus Torvalds {
18201da177e4SLinus Torvalds 	int idx;
18211da177e4SLinus Torvalds 	int s_idx = cb->family;
18221da177e4SLinus Torvalds 
18231da177e4SLinus Torvalds 	if (s_idx == 0)
18241da177e4SLinus Torvalds 		s_idx = 1;
182525239ceeSPatrick McHardy 	for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
18261da177e4SLinus Torvalds 		int type = cb->nlh->nlmsg_type-RTM_BASE;
18271da177e4SLinus Torvalds 		if (idx < s_idx || idx == PF_PACKET)
18281da177e4SLinus Torvalds 			continue;
1829e2849863SThomas Graf 		if (rtnl_msg_handlers[idx] == NULL ||
1830e2849863SThomas Graf 		    rtnl_msg_handlers[idx][type].dumpit == NULL)
18311da177e4SLinus Torvalds 			continue;
18321da177e4SLinus Torvalds 		if (idx > s_idx)
18331da177e4SLinus Torvalds 			memset(&cb->args[0], 0, sizeof(cb->args));
1834e2849863SThomas Graf 		if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
18351da177e4SLinus Torvalds 			break;
18361da177e4SLinus Torvalds 	}
18371da177e4SLinus Torvalds 	cb->family = idx;
18381da177e4SLinus Torvalds 
18391da177e4SLinus Torvalds 	return skb->len;
18401da177e4SLinus Torvalds }
18411da177e4SLinus Torvalds 
18421da177e4SLinus Torvalds void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change)
18431da177e4SLinus Torvalds {
1844c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
18451da177e4SLinus Torvalds 	struct sk_buff *skb;
18460ec6d3f4SThomas Graf 	int err = -ENOBUFS;
18471da177e4SLinus Torvalds 
184838f7b870SPatrick McHardy 	skb = nlmsg_new(if_nlmsg_size(dev), GFP_KERNEL);
18490ec6d3f4SThomas Graf 	if (skb == NULL)
18500ec6d3f4SThomas Graf 		goto errout;
18511da177e4SLinus Torvalds 
1852575c3e2aSPatrick McHardy 	err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0);
185326932566SPatrick McHardy 	if (err < 0) {
185426932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in if_nlmsg_size() */
185526932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
185626932566SPatrick McHardy 		kfree_skb(skb);
185726932566SPatrick McHardy 		goto errout;
185826932566SPatrick McHardy 	}
18591ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
18601ce85fe4SPablo Neira Ayuso 	return;
18610ec6d3f4SThomas Graf errout:
18620ec6d3f4SThomas Graf 	if (err < 0)
18634b3da706SEric W. Biederman 		rtnl_set_sk_err(net, RTNLGRP_LINK, err);
18641da177e4SLinus Torvalds }
18651da177e4SLinus Torvalds 
18661da177e4SLinus Torvalds /* Protected by RTNL sempahore.  */
18671da177e4SLinus Torvalds static struct rtattr **rta_buf;
18681da177e4SLinus Torvalds static int rtattr_max;
18691da177e4SLinus Torvalds 
18701da177e4SLinus Torvalds /* Process one rtnetlink message. */
18711da177e4SLinus Torvalds 
18721d00a4ebSThomas Graf static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
18731da177e4SLinus Torvalds {
18743b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
1875e2849863SThomas Graf 	rtnl_doit_func doit;
18761da177e4SLinus Torvalds 	int sz_idx, kind;
18771da177e4SLinus Torvalds 	int min_len;
18781da177e4SLinus Torvalds 	int family;
18791da177e4SLinus Torvalds 	int type;
1880*2907c35fSEric Dumazet 	int err;
18811da177e4SLinus Torvalds 
18821da177e4SLinus Torvalds 	type = nlh->nlmsg_type;
18831da177e4SLinus Torvalds 	if (type > RTM_MAX)
1884038890feSThomas Graf 		return -EOPNOTSUPP;
18851da177e4SLinus Torvalds 
18861da177e4SLinus Torvalds 	type -= RTM_BASE;
18871da177e4SLinus Torvalds 
18881da177e4SLinus Torvalds 	/* All the messages must have at least 1 byte length */
18891da177e4SLinus Torvalds 	if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg)))
18901da177e4SLinus Torvalds 		return 0;
18911da177e4SLinus Torvalds 
18921da177e4SLinus Torvalds 	family = ((struct rtgenmsg *)NLMSG_DATA(nlh))->rtgen_family;
18931da177e4SLinus Torvalds 	sz_idx = type>>2;
18941da177e4SLinus Torvalds 	kind = type&3;
18951da177e4SLinus Torvalds 
18961d00a4ebSThomas Graf 	if (kind != 2 && security_netlink_recv(skb, CAP_NET_ADMIN))
18971d00a4ebSThomas Graf 		return -EPERM;
18981da177e4SLinus Torvalds 
1899b8f3ab42SDavid S. Miller 	if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
190097c53cacSDenis V. Lunev 		struct sock *rtnl;
1901e2849863SThomas Graf 		rtnl_dumpit_func dumpit;
19021da177e4SLinus Torvalds 
1903e2849863SThomas Graf 		dumpit = rtnl_get_dumpit(family, type);
1904e2849863SThomas Graf 		if (dumpit == NULL)
1905038890feSThomas Graf 			return -EOPNOTSUPP;
19061da177e4SLinus Torvalds 
1907*2907c35fSEric Dumazet 		__rtnl_unlock();
190897c53cacSDenis V. Lunev 		rtnl = net->rtnl;
1909*2907c35fSEric Dumazet 		err = netlink_dump_start(rtnl, skb, nlh, dumpit, NULL);
1910*2907c35fSEric Dumazet 		rtnl_lock();
1911*2907c35fSEric Dumazet 		return err;
19121da177e4SLinus Torvalds 	}
19131da177e4SLinus Torvalds 
19141da177e4SLinus Torvalds 	memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *)));
19151da177e4SLinus Torvalds 
19161da177e4SLinus Torvalds 	min_len = rtm_min[sz_idx];
19171da177e4SLinus Torvalds 	if (nlh->nlmsg_len < min_len)
19181d00a4ebSThomas Graf 		return -EINVAL;
19191da177e4SLinus Torvalds 
19201da177e4SLinus Torvalds 	if (nlh->nlmsg_len > min_len) {
19211da177e4SLinus Torvalds 		int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
19221da177e4SLinus Torvalds 		struct rtattr *attr = (void *)nlh + NLMSG_ALIGN(min_len);
19231da177e4SLinus Torvalds 
19241da177e4SLinus Torvalds 		while (RTA_OK(attr, attrlen)) {
19251da177e4SLinus Torvalds 			unsigned flavor = attr->rta_type;
19261da177e4SLinus Torvalds 			if (flavor) {
19271da177e4SLinus Torvalds 				if (flavor > rta_max[sz_idx])
19281d00a4ebSThomas Graf 					return -EINVAL;
19291da177e4SLinus Torvalds 				rta_buf[flavor-1] = attr;
19301da177e4SLinus Torvalds 			}
19311da177e4SLinus Torvalds 			attr = RTA_NEXT(attr, attrlen);
19321da177e4SLinus Torvalds 		}
19331da177e4SLinus Torvalds 	}
19341da177e4SLinus Torvalds 
1935e2849863SThomas Graf 	doit = rtnl_get_doit(family, type);
1936e2849863SThomas Graf 	if (doit == NULL)
1937038890feSThomas Graf 		return -EOPNOTSUPP;
19381da177e4SLinus Torvalds 
19391d00a4ebSThomas Graf 	return doit(skb, nlh, (void *)&rta_buf[0]);
19401da177e4SLinus Torvalds }
19411da177e4SLinus Torvalds 
1942cd40b7d3SDenis V. Lunev static void rtnetlink_rcv(struct sk_buff *skb)
19431da177e4SLinus Torvalds {
19441536cc0dSDenis V. Lunev 	rtnl_lock();
1945cd40b7d3SDenis V. Lunev 	netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
19461536cc0dSDenis V. Lunev 	rtnl_unlock();
19471da177e4SLinus Torvalds }
19481da177e4SLinus Torvalds 
19491da177e4SLinus Torvalds static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
19501da177e4SLinus Torvalds {
19511da177e4SLinus Torvalds 	struct net_device *dev = ptr;
1952e9dc8653SEric W. Biederman 
19531da177e4SLinus Torvalds 	switch (event) {
19541da177e4SLinus Torvalds 	case NETDEV_UP:
19551da177e4SLinus Torvalds 	case NETDEV_DOWN:
195610de05afSPatrick McHardy 	case NETDEV_PRE_UP:
1957d90a909eSEric W. Biederman 	case NETDEV_POST_INIT:
1958d90a909eSEric W. Biederman 	case NETDEV_REGISTER:
19591da177e4SLinus Torvalds 	case NETDEV_CHANGE:
1960755d0e77SPatrick McHardy 	case NETDEV_PRE_TYPE_CHANGE:
19611da177e4SLinus Torvalds 	case NETDEV_GOING_DOWN:
1962a2835763SPatrick McHardy 	case NETDEV_UNREGISTER:
1963d90a909eSEric W. Biederman 	case NETDEV_UNREGISTER_BATCH:
1964ac3d3f81SAmerigo Wang 	case NETDEV_RELEASE:
1965ac3d3f81SAmerigo Wang 	case NETDEV_JOIN:
19661da177e4SLinus Torvalds 		break;
19671da177e4SLinus Torvalds 	default:
19681da177e4SLinus Torvalds 		rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
19691da177e4SLinus Torvalds 		break;
19701da177e4SLinus Torvalds 	}
19711da177e4SLinus Torvalds 	return NOTIFY_DONE;
19721da177e4SLinus Torvalds }
19731da177e4SLinus Torvalds 
19741da177e4SLinus Torvalds static struct notifier_block rtnetlink_dev_notifier = {
19751da177e4SLinus Torvalds 	.notifier_call	= rtnetlink_event,
19761da177e4SLinus Torvalds };
19771da177e4SLinus Torvalds 
197897c53cacSDenis V. Lunev 
19792c8c1e72SAlexey Dobriyan static int __net_init rtnetlink_net_init(struct net *net)
198097c53cacSDenis V. Lunev {
198197c53cacSDenis V. Lunev 	struct sock *sk;
198297c53cacSDenis V. Lunev 	sk = netlink_kernel_create(net, NETLINK_ROUTE, RTNLGRP_MAX,
1983*2907c35fSEric Dumazet 				   rtnetlink_rcv, &rtnl_mutex, THIS_MODULE);
198497c53cacSDenis V. Lunev 	if (!sk)
198597c53cacSDenis V. Lunev 		return -ENOMEM;
198697c53cacSDenis V. Lunev 	net->rtnl = sk;
198797c53cacSDenis V. Lunev 	return 0;
198897c53cacSDenis V. Lunev }
198997c53cacSDenis V. Lunev 
19902c8c1e72SAlexey Dobriyan static void __net_exit rtnetlink_net_exit(struct net *net)
199197c53cacSDenis V. Lunev {
1992b7c6ba6eSDenis V. Lunev 	netlink_kernel_release(net->rtnl);
199397c53cacSDenis V. Lunev 	net->rtnl = NULL;
199497c53cacSDenis V. Lunev }
199597c53cacSDenis V. Lunev 
199697c53cacSDenis V. Lunev static struct pernet_operations rtnetlink_net_ops = {
199797c53cacSDenis V. Lunev 	.init = rtnetlink_net_init,
199897c53cacSDenis V. Lunev 	.exit = rtnetlink_net_exit,
199997c53cacSDenis V. Lunev };
200097c53cacSDenis V. Lunev 
20011da177e4SLinus Torvalds void __init rtnetlink_init(void)
20021da177e4SLinus Torvalds {
20031da177e4SLinus Torvalds 	int i;
20041da177e4SLinus Torvalds 
20051da177e4SLinus Torvalds 	rtattr_max = 0;
20061da177e4SLinus Torvalds 	for (i = 0; i < ARRAY_SIZE(rta_max); i++)
20071da177e4SLinus Torvalds 		if (rta_max[i] > rtattr_max)
20081da177e4SLinus Torvalds 			rtattr_max = rta_max[i];
20091da177e4SLinus Torvalds 	rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL);
20101da177e4SLinus Torvalds 	if (!rta_buf)
20111da177e4SLinus Torvalds 		panic("rtnetlink_init: cannot allocate rta_buf\n");
20121da177e4SLinus Torvalds 
201397c53cacSDenis V. Lunev 	if (register_pernet_subsys(&rtnetlink_net_ops))
20141da177e4SLinus Torvalds 		panic("rtnetlink_init: cannot initialize rtnetlink\n");
201597c53cacSDenis V. Lunev 
20161da177e4SLinus Torvalds 	netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV);
20171da177e4SLinus Torvalds 	register_netdevice_notifier(&rtnetlink_dev_notifier);
2018340d17fcSThomas Graf 
2019340d17fcSThomas Graf 	rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, rtnl_dump_ifinfo);
2020340d17fcSThomas Graf 	rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL);
202138f7b870SPatrick McHardy 	rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL);
202238f7b870SPatrick McHardy 	rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL);
2023687ad8ccSThomas Graf 
2024687ad8ccSThomas Graf 	rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all);
2025687ad8ccSThomas Graf 	rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all);
20261da177e4SLinus Torvalds }
20271da177e4SLinus Torvalds 
2028