xref: /openbmc/linux/net/core/rtnetlink.c (revision edbc0bb3)
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;
59c7ac8679SGreg Rose 	rtnl_calcit_func 	calcit;
60e2849863SThomas Graf };
61e2849863SThomas Graf 
626756ae4bSStephen Hemminger static DEFINE_MUTEX(rtnl_mutex);
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds void rtnl_lock(void)
651da177e4SLinus Torvalds {
666756ae4bSStephen Hemminger 	mutex_lock(&rtnl_mutex);
671da177e4SLinus Torvalds }
68e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_lock);
691da177e4SLinus Torvalds 
706756ae4bSStephen Hemminger void __rtnl_unlock(void)
711da177e4SLinus Torvalds {
726756ae4bSStephen Hemminger 	mutex_unlock(&rtnl_mutex);
731da177e4SLinus Torvalds }
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds void rtnl_unlock(void)
761da177e4SLinus Torvalds {
7758ec3b4dSHerbert Xu 	/* This fellow will unlock it for us. */
781da177e4SLinus Torvalds 	netdev_run_todo();
791da177e4SLinus Torvalds }
80e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unlock);
811da177e4SLinus Torvalds 
826756ae4bSStephen Hemminger int rtnl_trylock(void)
836756ae4bSStephen Hemminger {
846756ae4bSStephen Hemminger 	return mutex_trylock(&rtnl_mutex);
856756ae4bSStephen Hemminger }
86e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_trylock);
876756ae4bSStephen Hemminger 
88c9c1014bSPatrick McHardy int rtnl_is_locked(void)
89c9c1014bSPatrick McHardy {
90c9c1014bSPatrick McHardy 	return mutex_is_locked(&rtnl_mutex);
91c9c1014bSPatrick McHardy }
92e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_is_locked);
93c9c1014bSPatrick McHardy 
94a898def2SPaul E. McKenney #ifdef CONFIG_PROVE_LOCKING
95a898def2SPaul E. McKenney int lockdep_rtnl_is_held(void)
96a898def2SPaul E. McKenney {
97a898def2SPaul E. McKenney 	return lockdep_is_held(&rtnl_mutex);
98a898def2SPaul E. McKenney }
99a898def2SPaul E. McKenney EXPORT_SYMBOL(lockdep_rtnl_is_held);
100a898def2SPaul E. McKenney #endif /* #ifdef CONFIG_PROVE_LOCKING */
101a898def2SPaul E. McKenney 
10225239ceeSPatrick McHardy static struct rtnl_link *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
103e2849863SThomas Graf 
104e2849863SThomas Graf static inline int rtm_msgindex(int msgtype)
105e2849863SThomas Graf {
106e2849863SThomas Graf 	int msgindex = msgtype - RTM_BASE;
107e2849863SThomas Graf 
108e2849863SThomas Graf 	/*
109e2849863SThomas Graf 	 * msgindex < 0 implies someone tried to register a netlink
110e2849863SThomas Graf 	 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
111e2849863SThomas Graf 	 * the message type has not been added to linux/rtnetlink.h
112e2849863SThomas Graf 	 */
113e2849863SThomas Graf 	BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
114e2849863SThomas Graf 
115e2849863SThomas Graf 	return msgindex;
116e2849863SThomas Graf }
117e2849863SThomas Graf 
118e2849863SThomas Graf static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex)
119e2849863SThomas Graf {
120e2849863SThomas Graf 	struct rtnl_link *tab;
121e2849863SThomas Graf 
12225239ceeSPatrick McHardy 	if (protocol <= RTNL_FAMILY_MAX)
123e2849863SThomas Graf 		tab = rtnl_msg_handlers[protocol];
1240f87b1ddSPatrick McHardy 	else
1250f87b1ddSPatrick McHardy 		tab = NULL;
1260f87b1ddSPatrick McHardy 
12751057f2fSThomas Graf 	if (tab == NULL || tab[msgindex].doit == NULL)
128e2849863SThomas Graf 		tab = rtnl_msg_handlers[PF_UNSPEC];
129e2849863SThomas Graf 
13051057f2fSThomas Graf 	return tab ? tab[msgindex].doit : NULL;
131e2849863SThomas Graf }
132e2849863SThomas Graf 
133e2849863SThomas Graf static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex)
134e2849863SThomas Graf {
135e2849863SThomas Graf 	struct rtnl_link *tab;
136e2849863SThomas Graf 
13725239ceeSPatrick McHardy 	if (protocol <= RTNL_FAMILY_MAX)
138e2849863SThomas Graf 		tab = rtnl_msg_handlers[protocol];
1390f87b1ddSPatrick McHardy 	else
1400f87b1ddSPatrick McHardy 		tab = NULL;
1410f87b1ddSPatrick McHardy 
14251057f2fSThomas Graf 	if (tab == NULL || tab[msgindex].dumpit == NULL)
143e2849863SThomas Graf 		tab = rtnl_msg_handlers[PF_UNSPEC];
144e2849863SThomas Graf 
14551057f2fSThomas Graf 	return tab ? tab[msgindex].dumpit : NULL;
146e2849863SThomas Graf }
147e2849863SThomas Graf 
148c7ac8679SGreg Rose static rtnl_calcit_func rtnl_get_calcit(int protocol, int msgindex)
149c7ac8679SGreg Rose {
150c7ac8679SGreg Rose 	struct rtnl_link *tab;
151c7ac8679SGreg Rose 
152c7ac8679SGreg Rose 	if (protocol <= RTNL_FAMILY_MAX)
153c7ac8679SGreg Rose 		tab = rtnl_msg_handlers[protocol];
154c7ac8679SGreg Rose 	else
155c7ac8679SGreg Rose 		tab = NULL;
156c7ac8679SGreg Rose 
157c7ac8679SGreg Rose 	if (tab == NULL || tab[msgindex].calcit == NULL)
158c7ac8679SGreg Rose 		tab = rtnl_msg_handlers[PF_UNSPEC];
159c7ac8679SGreg Rose 
160c7ac8679SGreg Rose 	return tab ? tab[msgindex].calcit : NULL;
161c7ac8679SGreg Rose }
162c7ac8679SGreg Rose 
163e2849863SThomas Graf /**
164e2849863SThomas Graf  * __rtnl_register - Register a rtnetlink message type
165e2849863SThomas Graf  * @protocol: Protocol family or PF_UNSPEC
166e2849863SThomas Graf  * @msgtype: rtnetlink message type
167e2849863SThomas Graf  * @doit: Function pointer called for each request message
168e2849863SThomas Graf  * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
169c7ac8679SGreg Rose  * @calcit: Function pointer to calc size of dump message
170e2849863SThomas Graf  *
171e2849863SThomas Graf  * Registers the specified function pointers (at least one of them has
172e2849863SThomas Graf  * to be non-NULL) to be called whenever a request message for the
173e2849863SThomas Graf  * specified protocol family and message type is received.
174e2849863SThomas Graf  *
175e2849863SThomas Graf  * The special protocol family PF_UNSPEC may be used to define fallback
176e2849863SThomas Graf  * function pointers for the case when no entry for the specific protocol
177e2849863SThomas Graf  * family exists.
178e2849863SThomas Graf  *
179e2849863SThomas Graf  * Returns 0 on success or a negative error code.
180e2849863SThomas Graf  */
181e2849863SThomas Graf int __rtnl_register(int protocol, int msgtype,
182c7ac8679SGreg Rose 		    rtnl_doit_func doit, rtnl_dumpit_func dumpit,
183c7ac8679SGreg Rose 		    rtnl_calcit_func calcit)
184e2849863SThomas Graf {
185e2849863SThomas Graf 	struct rtnl_link *tab;
186e2849863SThomas Graf 	int msgindex;
187e2849863SThomas Graf 
18825239ceeSPatrick McHardy 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
189e2849863SThomas Graf 	msgindex = rtm_msgindex(msgtype);
190e2849863SThomas Graf 
191e2849863SThomas Graf 	tab = rtnl_msg_handlers[protocol];
192e2849863SThomas Graf 	if (tab == NULL) {
193e2849863SThomas Graf 		tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL);
194e2849863SThomas Graf 		if (tab == NULL)
195e2849863SThomas Graf 			return -ENOBUFS;
196e2849863SThomas Graf 
197e2849863SThomas Graf 		rtnl_msg_handlers[protocol] = tab;
198e2849863SThomas Graf 	}
199e2849863SThomas Graf 
200e2849863SThomas Graf 	if (doit)
201e2849863SThomas Graf 		tab[msgindex].doit = doit;
202e2849863SThomas Graf 
203e2849863SThomas Graf 	if (dumpit)
204e2849863SThomas Graf 		tab[msgindex].dumpit = dumpit;
205e2849863SThomas Graf 
206c7ac8679SGreg Rose 	if (calcit)
207c7ac8679SGreg Rose 		tab[msgindex].calcit = calcit;
208c7ac8679SGreg Rose 
209e2849863SThomas Graf 	return 0;
210e2849863SThomas Graf }
211e2849863SThomas Graf EXPORT_SYMBOL_GPL(__rtnl_register);
212e2849863SThomas Graf 
213e2849863SThomas Graf /**
214e2849863SThomas Graf  * rtnl_register - Register a rtnetlink message type
215e2849863SThomas Graf  *
216e2849863SThomas Graf  * Identical to __rtnl_register() but panics on failure. This is useful
217e2849863SThomas Graf  * as failure of this function is very unlikely, it can only happen due
218e2849863SThomas Graf  * to lack of memory when allocating the chain to store all message
219e2849863SThomas Graf  * handlers for a protocol. Meant for use in init functions where lack
22025985edcSLucas De Marchi  * of memory implies no sense in continuing.
221e2849863SThomas Graf  */
222e2849863SThomas Graf void rtnl_register(int protocol, int msgtype,
223c7ac8679SGreg Rose 		   rtnl_doit_func doit, rtnl_dumpit_func dumpit,
224c7ac8679SGreg Rose 		   rtnl_calcit_func calcit)
225e2849863SThomas Graf {
226c7ac8679SGreg Rose 	if (__rtnl_register(protocol, msgtype, doit, dumpit, calcit) < 0)
227e2849863SThomas Graf 		panic("Unable to register rtnetlink message handler, "
228e2849863SThomas Graf 		      "protocol = %d, message type = %d\n",
229e2849863SThomas Graf 		      protocol, msgtype);
230e2849863SThomas Graf }
231e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_register);
232e2849863SThomas Graf 
233e2849863SThomas Graf /**
234e2849863SThomas Graf  * rtnl_unregister - Unregister a rtnetlink message type
235e2849863SThomas Graf  * @protocol: Protocol family or PF_UNSPEC
236e2849863SThomas Graf  * @msgtype: rtnetlink message type
237e2849863SThomas Graf  *
238e2849863SThomas Graf  * Returns 0 on success or a negative error code.
239e2849863SThomas Graf  */
240e2849863SThomas Graf int rtnl_unregister(int protocol, int msgtype)
241e2849863SThomas Graf {
242e2849863SThomas Graf 	int msgindex;
243e2849863SThomas Graf 
24425239ceeSPatrick McHardy 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
245e2849863SThomas Graf 	msgindex = rtm_msgindex(msgtype);
246e2849863SThomas Graf 
247e2849863SThomas Graf 	if (rtnl_msg_handlers[protocol] == NULL)
248e2849863SThomas Graf 		return -ENOENT;
249e2849863SThomas Graf 
250e2849863SThomas Graf 	rtnl_msg_handlers[protocol][msgindex].doit = NULL;
251e2849863SThomas Graf 	rtnl_msg_handlers[protocol][msgindex].dumpit = NULL;
252e2849863SThomas Graf 
253e2849863SThomas Graf 	return 0;
254e2849863SThomas Graf }
255e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister);
256e2849863SThomas Graf 
257e2849863SThomas Graf /**
258e2849863SThomas Graf  * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
259e2849863SThomas Graf  * @protocol : Protocol family or PF_UNSPEC
260e2849863SThomas Graf  *
261e2849863SThomas Graf  * Identical to calling rtnl_unregster() for all registered message types
262e2849863SThomas Graf  * of a certain protocol family.
263e2849863SThomas Graf  */
264e2849863SThomas Graf void rtnl_unregister_all(int protocol)
265e2849863SThomas Graf {
26625239ceeSPatrick McHardy 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
267e2849863SThomas Graf 
268e2849863SThomas Graf 	kfree(rtnl_msg_handlers[protocol]);
269e2849863SThomas Graf 	rtnl_msg_handlers[protocol] = NULL;
270e2849863SThomas Graf }
271e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister_all);
2721da177e4SLinus Torvalds 
27338f7b870SPatrick McHardy static LIST_HEAD(link_ops);
27438f7b870SPatrick McHardy 
275c63044f0SEric Dumazet static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
276c63044f0SEric Dumazet {
277c63044f0SEric Dumazet 	const struct rtnl_link_ops *ops;
278c63044f0SEric Dumazet 
279c63044f0SEric Dumazet 	list_for_each_entry(ops, &link_ops, list) {
280c63044f0SEric Dumazet 		if (!strcmp(ops->kind, kind))
281c63044f0SEric Dumazet 			return ops;
282c63044f0SEric Dumazet 	}
283c63044f0SEric Dumazet 	return NULL;
284c63044f0SEric Dumazet }
285c63044f0SEric Dumazet 
28638f7b870SPatrick McHardy /**
28738f7b870SPatrick McHardy  * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
28838f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to register
28938f7b870SPatrick McHardy  *
29038f7b870SPatrick McHardy  * The caller must hold the rtnl_mutex. This function should be used
29138f7b870SPatrick McHardy  * by drivers that create devices during module initialization. It
29238f7b870SPatrick McHardy  * must be called before registering the devices.
29338f7b870SPatrick McHardy  *
29438f7b870SPatrick McHardy  * Returns 0 on success or a negative error code.
29538f7b870SPatrick McHardy  */
29638f7b870SPatrick McHardy int __rtnl_link_register(struct rtnl_link_ops *ops)
29738f7b870SPatrick McHardy {
298c63044f0SEric Dumazet 	if (rtnl_link_ops_get(ops->kind))
299c63044f0SEric Dumazet 		return -EEXIST;
300c63044f0SEric Dumazet 
3012d85cba2SPatrick McHardy 	if (!ops->dellink)
30223289a37SEric Dumazet 		ops->dellink = unregister_netdevice_queue;
3032d85cba2SPatrick McHardy 
30438f7b870SPatrick McHardy 	list_add_tail(&ops->list, &link_ops);
30538f7b870SPatrick McHardy 	return 0;
30638f7b870SPatrick McHardy }
30738f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_register);
30838f7b870SPatrick McHardy 
30938f7b870SPatrick McHardy /**
31038f7b870SPatrick McHardy  * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
31138f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to register
31238f7b870SPatrick McHardy  *
31338f7b870SPatrick McHardy  * Returns 0 on success or a negative error code.
31438f7b870SPatrick McHardy  */
31538f7b870SPatrick McHardy int rtnl_link_register(struct rtnl_link_ops *ops)
31638f7b870SPatrick McHardy {
31738f7b870SPatrick McHardy 	int err;
31838f7b870SPatrick McHardy 
31938f7b870SPatrick McHardy 	rtnl_lock();
32038f7b870SPatrick McHardy 	err = __rtnl_link_register(ops);
32138f7b870SPatrick McHardy 	rtnl_unlock();
32238f7b870SPatrick McHardy 	return err;
32338f7b870SPatrick McHardy }
32438f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_register);
32538f7b870SPatrick McHardy 
326669f87baSPavel Emelyanov static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
327669f87baSPavel Emelyanov {
328669f87baSPavel Emelyanov 	struct net_device *dev;
32923289a37SEric Dumazet 	LIST_HEAD(list_kill);
33023289a37SEric Dumazet 
331669f87baSPavel Emelyanov 	for_each_netdev(net, dev) {
33223289a37SEric Dumazet 		if (dev->rtnl_link_ops == ops)
33323289a37SEric Dumazet 			ops->dellink(dev, &list_kill);
334669f87baSPavel Emelyanov 	}
33523289a37SEric Dumazet 	unregister_netdevice_many(&list_kill);
336669f87baSPavel Emelyanov }
337669f87baSPavel Emelyanov 
33838f7b870SPatrick McHardy /**
33938f7b870SPatrick McHardy  * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
34038f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to unregister
34138f7b870SPatrick McHardy  *
3422d85cba2SPatrick McHardy  * The caller must hold the rtnl_mutex.
34338f7b870SPatrick McHardy  */
34438f7b870SPatrick McHardy void __rtnl_link_unregister(struct rtnl_link_ops *ops)
34538f7b870SPatrick McHardy {
346881d966bSEric W. Biederman 	struct net *net;
3472d85cba2SPatrick McHardy 
348881d966bSEric W. Biederman 	for_each_net(net) {
349669f87baSPavel Emelyanov 		__rtnl_kill_links(net, ops);
350881d966bSEric W. Biederman 	}
35138f7b870SPatrick McHardy 	list_del(&ops->list);
35238f7b870SPatrick McHardy }
35338f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
35438f7b870SPatrick McHardy 
35538f7b870SPatrick McHardy /**
35638f7b870SPatrick McHardy  * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
35738f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to unregister
35838f7b870SPatrick McHardy  */
35938f7b870SPatrick McHardy void rtnl_link_unregister(struct rtnl_link_ops *ops)
36038f7b870SPatrick McHardy {
36138f7b870SPatrick McHardy 	rtnl_lock();
36238f7b870SPatrick McHardy 	__rtnl_link_unregister(ops);
36338f7b870SPatrick McHardy 	rtnl_unlock();
36438f7b870SPatrick McHardy }
36538f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_unregister);
36638f7b870SPatrick McHardy 
36738f7b870SPatrick McHardy static size_t rtnl_link_get_size(const struct net_device *dev)
36838f7b870SPatrick McHardy {
36938f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
37038f7b870SPatrick McHardy 	size_t size;
37138f7b870SPatrick McHardy 
37238f7b870SPatrick McHardy 	if (!ops)
37338f7b870SPatrick McHardy 		return 0;
37438f7b870SPatrick McHardy 
375369cf77aSThomas Graf 	size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
376369cf77aSThomas Graf 	       nla_total_size(strlen(ops->kind) + 1);  /* IFLA_INFO_KIND */
37738f7b870SPatrick McHardy 
37838f7b870SPatrick McHardy 	if (ops->get_size)
37938f7b870SPatrick McHardy 		/* IFLA_INFO_DATA + nested data */
380369cf77aSThomas Graf 		size += nla_total_size(sizeof(struct nlattr)) +
38138f7b870SPatrick McHardy 			ops->get_size(dev);
38238f7b870SPatrick McHardy 
38338f7b870SPatrick McHardy 	if (ops->get_xstats_size)
384369cf77aSThomas Graf 		/* IFLA_INFO_XSTATS */
385369cf77aSThomas Graf 		size += nla_total_size(ops->get_xstats_size(dev));
38638f7b870SPatrick McHardy 
38738f7b870SPatrick McHardy 	return size;
38838f7b870SPatrick McHardy }
38938f7b870SPatrick McHardy 
390f8ff182cSThomas Graf static LIST_HEAD(rtnl_af_ops);
391f8ff182cSThomas Graf 
392f8ff182cSThomas Graf static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
393f8ff182cSThomas Graf {
394f8ff182cSThomas Graf 	const struct rtnl_af_ops *ops;
395f8ff182cSThomas Graf 
396f8ff182cSThomas Graf 	list_for_each_entry(ops, &rtnl_af_ops, list) {
397f8ff182cSThomas Graf 		if (ops->family == family)
398f8ff182cSThomas Graf 			return ops;
399f8ff182cSThomas Graf 	}
400f8ff182cSThomas Graf 
401f8ff182cSThomas Graf 	return NULL;
402f8ff182cSThomas Graf }
403f8ff182cSThomas Graf 
404f8ff182cSThomas Graf /**
405f8ff182cSThomas Graf  * __rtnl_af_register - Register rtnl_af_ops with rtnetlink.
406f8ff182cSThomas Graf  * @ops: struct rtnl_af_ops * to register
407f8ff182cSThomas Graf  *
408f8ff182cSThomas Graf  * The caller must hold the rtnl_mutex.
409f8ff182cSThomas Graf  *
410f8ff182cSThomas Graf  * Returns 0 on success or a negative error code.
411f8ff182cSThomas Graf  */
412f8ff182cSThomas Graf int __rtnl_af_register(struct rtnl_af_ops *ops)
413f8ff182cSThomas Graf {
414f8ff182cSThomas Graf 	list_add_tail(&ops->list, &rtnl_af_ops);
415f8ff182cSThomas Graf 	return 0;
416f8ff182cSThomas Graf }
417f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(__rtnl_af_register);
418f8ff182cSThomas Graf 
419f8ff182cSThomas Graf /**
420f8ff182cSThomas Graf  * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
421f8ff182cSThomas Graf  * @ops: struct rtnl_af_ops * to register
422f8ff182cSThomas Graf  *
423f8ff182cSThomas Graf  * Returns 0 on success or a negative error code.
424f8ff182cSThomas Graf  */
425f8ff182cSThomas Graf int rtnl_af_register(struct rtnl_af_ops *ops)
426f8ff182cSThomas Graf {
427f8ff182cSThomas Graf 	int err;
428f8ff182cSThomas Graf 
429f8ff182cSThomas Graf 	rtnl_lock();
430f8ff182cSThomas Graf 	err = __rtnl_af_register(ops);
431f8ff182cSThomas Graf 	rtnl_unlock();
432f8ff182cSThomas Graf 	return err;
433f8ff182cSThomas Graf }
434f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_register);
435f8ff182cSThomas Graf 
436f8ff182cSThomas Graf /**
437f8ff182cSThomas Graf  * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
438f8ff182cSThomas Graf  * @ops: struct rtnl_af_ops * to unregister
439f8ff182cSThomas Graf  *
440f8ff182cSThomas Graf  * The caller must hold the rtnl_mutex.
441f8ff182cSThomas Graf  */
442f8ff182cSThomas Graf void __rtnl_af_unregister(struct rtnl_af_ops *ops)
443f8ff182cSThomas Graf {
444f8ff182cSThomas Graf 	list_del(&ops->list);
445f8ff182cSThomas Graf }
446f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(__rtnl_af_unregister);
447f8ff182cSThomas Graf 
448f8ff182cSThomas Graf /**
449f8ff182cSThomas Graf  * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
450f8ff182cSThomas Graf  * @ops: struct rtnl_af_ops * to unregister
451f8ff182cSThomas Graf  */
452f8ff182cSThomas Graf void rtnl_af_unregister(struct rtnl_af_ops *ops)
453f8ff182cSThomas Graf {
454f8ff182cSThomas Graf 	rtnl_lock();
455f8ff182cSThomas Graf 	__rtnl_af_unregister(ops);
456f8ff182cSThomas Graf 	rtnl_unlock();
457f8ff182cSThomas Graf }
458f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_unregister);
459f8ff182cSThomas Graf 
460f8ff182cSThomas Graf static size_t rtnl_link_get_af_size(const struct net_device *dev)
461f8ff182cSThomas Graf {
462f8ff182cSThomas Graf 	struct rtnl_af_ops *af_ops;
463f8ff182cSThomas Graf 	size_t size;
464f8ff182cSThomas Graf 
465f8ff182cSThomas Graf 	/* IFLA_AF_SPEC */
466f8ff182cSThomas Graf 	size = nla_total_size(sizeof(struct nlattr));
467f8ff182cSThomas Graf 
468f8ff182cSThomas Graf 	list_for_each_entry(af_ops, &rtnl_af_ops, list) {
469f8ff182cSThomas Graf 		if (af_ops->get_link_af_size) {
470f8ff182cSThomas Graf 			/* AF_* + nested data */
471f8ff182cSThomas Graf 			size += nla_total_size(sizeof(struct nlattr)) +
472f8ff182cSThomas Graf 				af_ops->get_link_af_size(dev);
473f8ff182cSThomas Graf 		}
474f8ff182cSThomas Graf 	}
475f8ff182cSThomas Graf 
476f8ff182cSThomas Graf 	return size;
477f8ff182cSThomas Graf }
478f8ff182cSThomas Graf 
47938f7b870SPatrick McHardy static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
48038f7b870SPatrick McHardy {
48138f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
48238f7b870SPatrick McHardy 	struct nlattr *linkinfo, *data;
48338f7b870SPatrick McHardy 	int err = -EMSGSIZE;
48438f7b870SPatrick McHardy 
48538f7b870SPatrick McHardy 	linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
48638f7b870SPatrick McHardy 	if (linkinfo == NULL)
48738f7b870SPatrick McHardy 		goto out;
48838f7b870SPatrick McHardy 
48938f7b870SPatrick McHardy 	if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
49038f7b870SPatrick McHardy 		goto err_cancel_link;
49138f7b870SPatrick McHardy 	if (ops->fill_xstats) {
49238f7b870SPatrick McHardy 		err = ops->fill_xstats(skb, dev);
49338f7b870SPatrick McHardy 		if (err < 0)
49438f7b870SPatrick McHardy 			goto err_cancel_link;
49538f7b870SPatrick McHardy 	}
49638f7b870SPatrick McHardy 	if (ops->fill_info) {
49738f7b870SPatrick McHardy 		data = nla_nest_start(skb, IFLA_INFO_DATA);
49838f7b870SPatrick McHardy 		if (data == NULL)
49938f7b870SPatrick McHardy 			goto err_cancel_link;
50038f7b870SPatrick McHardy 		err = ops->fill_info(skb, dev);
50138f7b870SPatrick McHardy 		if (err < 0)
50238f7b870SPatrick McHardy 			goto err_cancel_data;
50338f7b870SPatrick McHardy 		nla_nest_end(skb, data);
50438f7b870SPatrick McHardy 	}
50538f7b870SPatrick McHardy 
50638f7b870SPatrick McHardy 	nla_nest_end(skb, linkinfo);
50738f7b870SPatrick McHardy 	return 0;
50838f7b870SPatrick McHardy 
50938f7b870SPatrick McHardy err_cancel_data:
51038f7b870SPatrick McHardy 	nla_nest_cancel(skb, data);
51138f7b870SPatrick McHardy err_cancel_link:
51238f7b870SPatrick McHardy 	nla_nest_cancel(skb, linkinfo);
51338f7b870SPatrick McHardy out:
51438f7b870SPatrick McHardy 	return err;
51538f7b870SPatrick McHardy }
51638f7b870SPatrick McHardy 
517db46edc6SThomas Graf static const int rtm_min[RTM_NR_FAMILIES] =
5181da177e4SLinus Torvalds {
519f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWLINK)]      = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
520f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWADDR)]      = NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
521f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWROUTE)]     = NLMSG_LENGTH(sizeof(struct rtmsg)),
52214c0b97dSThomas Graf 	[RTM_FAM(RTM_NEWRULE)]      = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)),
523f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWQDISC)]     = NLMSG_LENGTH(sizeof(struct tcmsg)),
524f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWTCLASS)]    = NLMSG_LENGTH(sizeof(struct tcmsg)),
525f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWTFILTER)]   = NLMSG_LENGTH(sizeof(struct tcmsg)),
526f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWACTION)]    = NLMSG_LENGTH(sizeof(struct tcamsg)),
527f90a0a74SThomas Graf 	[RTM_FAM(RTM_GETMULTICAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
528f90a0a74SThomas Graf 	[RTM_FAM(RTM_GETANYCAST)]   = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
5291da177e4SLinus Torvalds };
5301da177e4SLinus Torvalds 
531db46edc6SThomas Graf static const int rta_max[RTM_NR_FAMILIES] =
5321da177e4SLinus Torvalds {
533f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWLINK)]      = IFLA_MAX,
534f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWADDR)]      = IFA_MAX,
535f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWROUTE)]     = RTA_MAX,
53614c0b97dSThomas Graf 	[RTM_FAM(RTM_NEWRULE)]      = FRA_MAX,
537f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWQDISC)]     = TCA_MAX,
538f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWTCLASS)]    = TCA_MAX,
539f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWTFILTER)]   = TCA_MAX,
540f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWACTION)]    = TCAA_MAX,
5411da177e4SLinus Torvalds };
5421da177e4SLinus Torvalds 
5431da177e4SLinus Torvalds void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
5441da177e4SLinus Torvalds {
5451da177e4SLinus Torvalds 	struct rtattr *rta;
5461da177e4SLinus Torvalds 	int size = RTA_LENGTH(attrlen);
5471da177e4SLinus Torvalds 
5481da177e4SLinus Torvalds 	rta = (struct rtattr *)skb_put(skb, RTA_ALIGN(size));
5491da177e4SLinus Torvalds 	rta->rta_type = attrtype;
5501da177e4SLinus Torvalds 	rta->rta_len = size;
5511da177e4SLinus Torvalds 	memcpy(RTA_DATA(rta), data, attrlen);
552b3563c4fSPatrick McHardy 	memset(RTA_DATA(rta) + attrlen, 0, RTA_ALIGN(size) - size);
5531da177e4SLinus Torvalds }
554e0d087afSEric Dumazet EXPORT_SYMBOL(__rta_fill);
5551da177e4SLinus Torvalds 
55697c53cacSDenis V. Lunev int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned group, int echo)
5571da177e4SLinus Torvalds {
55897c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
5591da177e4SLinus Torvalds 	int err = 0;
5601da177e4SLinus Torvalds 
561ac6d439dSPatrick McHardy 	NETLINK_CB(skb).dst_group = group;
5621da177e4SLinus Torvalds 	if (echo)
5631da177e4SLinus Torvalds 		atomic_inc(&skb->users);
5641da177e4SLinus Torvalds 	netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
5651da177e4SLinus Torvalds 	if (echo)
5661da177e4SLinus Torvalds 		err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
5671da177e4SLinus Torvalds 	return err;
5681da177e4SLinus Torvalds }
5691da177e4SLinus Torvalds 
57097c53cacSDenis V. Lunev int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
5712942e900SThomas Graf {
57297c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
57397c53cacSDenis V. Lunev 
5742942e900SThomas Graf 	return nlmsg_unicast(rtnl, skb, pid);
5752942e900SThomas Graf }
576e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unicast);
5772942e900SThomas Graf 
5781ce85fe4SPablo Neira Ayuso void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
57997676b6bSThomas Graf 		 struct nlmsghdr *nlh, gfp_t flags)
58097676b6bSThomas Graf {
58197c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
58297676b6bSThomas Graf 	int report = 0;
58397676b6bSThomas Graf 
58497676b6bSThomas Graf 	if (nlh)
58597676b6bSThomas Graf 		report = nlmsg_report(nlh);
58697676b6bSThomas Graf 
5871ce85fe4SPablo Neira Ayuso 	nlmsg_notify(rtnl, skb, pid, group, report, flags);
58897676b6bSThomas Graf }
589e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_notify);
59097676b6bSThomas Graf 
59197c53cacSDenis V. Lunev void rtnl_set_sk_err(struct net *net, u32 group, int error)
59297676b6bSThomas Graf {
59397c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
59497c53cacSDenis V. Lunev 
59597676b6bSThomas Graf 	netlink_set_err(rtnl, 0, group, error);
59697676b6bSThomas Graf }
597e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_set_sk_err);
59897676b6bSThomas Graf 
5991da177e4SLinus Torvalds int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
6001da177e4SLinus Torvalds {
6012d7202bfSThomas Graf 	struct nlattr *mx;
6022d7202bfSThomas Graf 	int i, valid = 0;
6031da177e4SLinus Torvalds 
6042d7202bfSThomas Graf 	mx = nla_nest_start(skb, RTA_METRICS);
6052d7202bfSThomas Graf 	if (mx == NULL)
6062d7202bfSThomas Graf 		return -ENOBUFS;
6072d7202bfSThomas Graf 
6081da177e4SLinus Torvalds 	for (i = 0; i < RTAX_MAX; i++) {
6092d7202bfSThomas Graf 		if (metrics[i]) {
6102d7202bfSThomas Graf 			valid++;
611a6574349SDavid S. Miller 			if (nla_put_u32(skb, i+1, metrics[i]))
612a6574349SDavid S. Miller 				goto nla_put_failure;
6131da177e4SLinus Torvalds 		}
6142d7202bfSThomas Graf 	}
6151da177e4SLinus Torvalds 
616a57d27fcSDavid S. Miller 	if (!valid) {
617a57d27fcSDavid S. Miller 		nla_nest_cancel(skb, mx);
618a57d27fcSDavid S. Miller 		return 0;
619a57d27fcSDavid S. Miller 	}
6202d7202bfSThomas Graf 
6212d7202bfSThomas Graf 	return nla_nest_end(skb, mx);
6222d7202bfSThomas Graf 
6232d7202bfSThomas Graf nla_put_failure:
624bc3ed28cSThomas Graf 	nla_nest_cancel(skb, mx);
625bc3ed28cSThomas Graf 	return -EMSGSIZE;
6261da177e4SLinus Torvalds }
627e0d087afSEric Dumazet EXPORT_SYMBOL(rtnetlink_put_metrics);
6281da177e4SLinus Torvalds 
629e3703b3dSThomas Graf int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
630e3703b3dSThomas Graf 		       u32 ts, u32 tsage, long expires, u32 error)
631e3703b3dSThomas Graf {
632e3703b3dSThomas Graf 	struct rta_cacheinfo ci = {
633e3703b3dSThomas Graf 		.rta_lastuse = jiffies_to_clock_t(jiffies - dst->lastuse),
634e3703b3dSThomas Graf 		.rta_used = dst->__use,
635e3703b3dSThomas Graf 		.rta_clntref = atomic_read(&(dst->__refcnt)),
636e3703b3dSThomas Graf 		.rta_error = error,
637e3703b3dSThomas Graf 		.rta_id =  id,
638e3703b3dSThomas Graf 		.rta_ts = ts,
639e3703b3dSThomas Graf 		.rta_tsage = tsage,
640e3703b3dSThomas Graf 	};
641e3703b3dSThomas Graf 
642e3703b3dSThomas Graf 	if (expires)
643e3703b3dSThomas Graf 		ci.rta_expires = jiffies_to_clock_t(expires);
644e3703b3dSThomas Graf 
645e3703b3dSThomas Graf 	return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
646e3703b3dSThomas Graf }
647e3703b3dSThomas Graf EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
6481da177e4SLinus Torvalds 
64993b2d4a2SDavid S. Miller static void set_operstate(struct net_device *dev, unsigned char transition)
650b00055aaSStefan Rompf {
651b00055aaSStefan Rompf 	unsigned char operstate = dev->operstate;
652b00055aaSStefan Rompf 
653b00055aaSStefan Rompf 	switch (transition) {
654b00055aaSStefan Rompf 	case IF_OPER_UP:
655b00055aaSStefan Rompf 		if ((operstate == IF_OPER_DORMANT ||
656b00055aaSStefan Rompf 		     operstate == IF_OPER_UNKNOWN) &&
657b00055aaSStefan Rompf 		    !netif_dormant(dev))
658b00055aaSStefan Rompf 			operstate = IF_OPER_UP;
659b00055aaSStefan Rompf 		break;
660b00055aaSStefan Rompf 
661b00055aaSStefan Rompf 	case IF_OPER_DORMANT:
662b00055aaSStefan Rompf 		if (operstate == IF_OPER_UP ||
663b00055aaSStefan Rompf 		    operstate == IF_OPER_UNKNOWN)
664b00055aaSStefan Rompf 			operstate = IF_OPER_DORMANT;
665b00055aaSStefan Rompf 		break;
6663ff50b79SStephen Hemminger 	}
667b00055aaSStefan Rompf 
668b00055aaSStefan Rompf 	if (dev->operstate != operstate) {
669b00055aaSStefan Rompf 		write_lock_bh(&dev_base_lock);
670b00055aaSStefan Rompf 		dev->operstate = operstate;
671b00055aaSStefan Rompf 		write_unlock_bh(&dev_base_lock);
672b00055aaSStefan Rompf 		netdev_state_change(dev);
67393b2d4a2SDavid S. Miller 	}
674b00055aaSStefan Rompf }
675b00055aaSStefan Rompf 
6763729d502SPatrick McHardy static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
6773729d502SPatrick McHardy 					   const struct ifinfomsg *ifm)
6783729d502SPatrick McHardy {
6793729d502SPatrick McHardy 	unsigned int flags = ifm->ifi_flags;
6803729d502SPatrick McHardy 
6813729d502SPatrick McHardy 	/* bugwards compatibility: ifi_change == 0 is treated as ~0 */
6823729d502SPatrick McHardy 	if (ifm->ifi_change)
6833729d502SPatrick McHardy 		flags = (flags & ifm->ifi_change) |
6843729d502SPatrick McHardy 			(dev->flags & ~ifm->ifi_change);
6853729d502SPatrick McHardy 
6863729d502SPatrick McHardy 	return flags;
6873729d502SPatrick McHardy }
6883729d502SPatrick McHardy 
689b60c5115SThomas Graf static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
690be1f3c2cSBen Hutchings 				 const struct rtnl_link_stats64 *b)
6911da177e4SLinus Torvalds {
692b60c5115SThomas Graf 	a->rx_packets = b->rx_packets;
693b60c5115SThomas Graf 	a->tx_packets = b->tx_packets;
694b60c5115SThomas Graf 	a->rx_bytes = b->rx_bytes;
695b60c5115SThomas Graf 	a->tx_bytes = b->tx_bytes;
696b60c5115SThomas Graf 	a->rx_errors = b->rx_errors;
697b60c5115SThomas Graf 	a->tx_errors = b->tx_errors;
698b60c5115SThomas Graf 	a->rx_dropped = b->rx_dropped;
699b60c5115SThomas Graf 	a->tx_dropped = b->tx_dropped;
700b60c5115SThomas Graf 
701b60c5115SThomas Graf 	a->multicast = b->multicast;
702b60c5115SThomas Graf 	a->collisions = b->collisions;
703b60c5115SThomas Graf 
704b60c5115SThomas Graf 	a->rx_length_errors = b->rx_length_errors;
705b60c5115SThomas Graf 	a->rx_over_errors = b->rx_over_errors;
706b60c5115SThomas Graf 	a->rx_crc_errors = b->rx_crc_errors;
707b60c5115SThomas Graf 	a->rx_frame_errors = b->rx_frame_errors;
708b60c5115SThomas Graf 	a->rx_fifo_errors = b->rx_fifo_errors;
709b60c5115SThomas Graf 	a->rx_missed_errors = b->rx_missed_errors;
710b60c5115SThomas Graf 
711b60c5115SThomas Graf 	a->tx_aborted_errors = b->tx_aborted_errors;
712b60c5115SThomas Graf 	a->tx_carrier_errors = b->tx_carrier_errors;
713b60c5115SThomas Graf 	a->tx_fifo_errors = b->tx_fifo_errors;
714b60c5115SThomas Graf 	a->tx_heartbeat_errors = b->tx_heartbeat_errors;
715b60c5115SThomas Graf 	a->tx_window_errors = b->tx_window_errors;
716b60c5115SThomas Graf 
717b60c5115SThomas Graf 	a->rx_compressed = b->rx_compressed;
718b60c5115SThomas Graf 	a->tx_compressed = b->tx_compressed;
71910708f37SJan Engelhardt }
72010708f37SJan Engelhardt 
721be1f3c2cSBen Hutchings static void copy_rtnl_link_stats64(void *v, const struct rtnl_link_stats64 *b)
72210708f37SJan Engelhardt {
723afdcba37SEric Dumazet 	memcpy(v, b, sizeof(*b));
72410708f37SJan Engelhardt }
725b60c5115SThomas Graf 
726c02db8c6SChris Wright /* All VF info */
727115c9b81SGreg Rose static inline int rtnl_vfinfo_size(const struct net_device *dev,
728115c9b81SGreg Rose 				   u32 ext_filter_mask)
729ebc08a6fSWilliams, Mitch A {
730115c9b81SGreg Rose 	if (dev->dev.parent && dev_is_pci(dev->dev.parent) &&
731115c9b81SGreg Rose 	    (ext_filter_mask & RTEXT_FILTER_VF)) {
732c02db8c6SChris Wright 		int num_vfs = dev_num_vf(dev->dev.parent);
733045de01aSScott Feldman 		size_t size = nla_total_size(sizeof(struct nlattr));
734045de01aSScott Feldman 		size += nla_total_size(num_vfs * sizeof(struct nlattr));
735045de01aSScott Feldman 		size += num_vfs *
736045de01aSScott Feldman 			(nla_total_size(sizeof(struct ifla_vf_mac)) +
737045de01aSScott Feldman 			 nla_total_size(sizeof(struct ifla_vf_vlan)) +
7385f8444a3SGreg Rose 			 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
7395f8444a3SGreg Rose 			 nla_total_size(sizeof(struct ifla_vf_spoofchk)));
740c02db8c6SChris Wright 		return size;
741c02db8c6SChris Wright 	} else
742ebc08a6fSWilliams, Mitch A 		return 0;
743ebc08a6fSWilliams, Mitch A }
744ebc08a6fSWilliams, Mitch A 
74557b61080SScott Feldman static size_t rtnl_port_size(const struct net_device *dev)
74657b61080SScott Feldman {
74757b61080SScott Feldman 	size_t port_size = nla_total_size(4)		/* PORT_VF */
74857b61080SScott Feldman 		+ nla_total_size(PORT_PROFILE_MAX)	/* PORT_PROFILE */
74957b61080SScott Feldman 		+ nla_total_size(sizeof(struct ifla_port_vsi))
75057b61080SScott Feldman 							/* PORT_VSI_TYPE */
75157b61080SScott Feldman 		+ nla_total_size(PORT_UUID_MAX)		/* PORT_INSTANCE_UUID */
75257b61080SScott Feldman 		+ nla_total_size(PORT_UUID_MAX)		/* PORT_HOST_UUID */
75357b61080SScott Feldman 		+ nla_total_size(1)			/* PROT_VDP_REQUEST */
75457b61080SScott Feldman 		+ nla_total_size(2);			/* PORT_VDP_RESPONSE */
75557b61080SScott Feldman 	size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
75657b61080SScott Feldman 	size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
75757b61080SScott Feldman 		+ port_size;
75857b61080SScott Feldman 	size_t port_self_size = nla_total_size(sizeof(struct nlattr))
75957b61080SScott Feldman 		+ port_size;
76057b61080SScott Feldman 
76157b61080SScott Feldman 	if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent)
76257b61080SScott Feldman 		return 0;
76357b61080SScott Feldman 	if (dev_num_vf(dev->dev.parent))
76457b61080SScott Feldman 		return port_self_size + vf_ports_size +
76557b61080SScott Feldman 			vf_port_size * dev_num_vf(dev->dev.parent);
76657b61080SScott Feldman 	else
76757b61080SScott Feldman 		return port_self_size;
76857b61080SScott Feldman }
76957b61080SScott Feldman 
770115c9b81SGreg Rose static noinline size_t if_nlmsg_size(const struct net_device *dev,
771115c9b81SGreg Rose 				     u32 ext_filter_mask)
772339bf98fSThomas Graf {
773339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
774339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
7750b815a1aSStephen Hemminger 	       + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
776339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
777339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rtnl_link_ifmap))
778339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rtnl_link_stats))
779adcfe196SJan Engelhardt 	       + nla_total_size(sizeof(struct rtnl_link_stats64))
780339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
781339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
782339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_TXQLEN */
783339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_WEIGHT */
784339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MTU */
785339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_LINK */
786339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MASTER */
787edbc0bb3SBen Greear 	       + nla_total_size(4) /* IFLA_PROMISCUITY */
788339bf98fSThomas Graf 	       + nla_total_size(1) /* IFLA_OPERSTATE */
78938f7b870SPatrick McHardy 	       + nla_total_size(1) /* IFLA_LINKMODE */
790115c9b81SGreg Rose 	       + nla_total_size(ext_filter_mask
791115c9b81SGreg Rose 			        & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
792115c9b81SGreg Rose 	       + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
79357b61080SScott Feldman 	       + rtnl_port_size(dev) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
794f8ff182cSThomas Graf 	       + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
795f8ff182cSThomas Graf 	       + rtnl_link_get_af_size(dev); /* IFLA_AF_SPEC */
796339bf98fSThomas Graf }
797339bf98fSThomas Graf 
79857b61080SScott Feldman static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
79957b61080SScott Feldman {
80057b61080SScott Feldman 	struct nlattr *vf_ports;
80157b61080SScott Feldman 	struct nlattr *vf_port;
80257b61080SScott Feldman 	int vf;
80357b61080SScott Feldman 	int err;
80457b61080SScott Feldman 
80557b61080SScott Feldman 	vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
80657b61080SScott Feldman 	if (!vf_ports)
80757b61080SScott Feldman 		return -EMSGSIZE;
80857b61080SScott Feldman 
80957b61080SScott Feldman 	for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
81057b61080SScott Feldman 		vf_port = nla_nest_start(skb, IFLA_VF_PORT);
8118ca94183SScott Feldman 		if (!vf_port)
8128ca94183SScott Feldman 			goto nla_put_failure;
813a6574349SDavid S. Miller 		if (nla_put_u32(skb, IFLA_PORT_VF, vf))
814a6574349SDavid S. Miller 			goto nla_put_failure;
81557b61080SScott Feldman 		err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
8168ca94183SScott Feldman 		if (err == -EMSGSIZE)
8178ca94183SScott Feldman 			goto nla_put_failure;
81857b61080SScott Feldman 		if (err) {
81957b61080SScott Feldman 			nla_nest_cancel(skb, vf_port);
82057b61080SScott Feldman 			continue;
82157b61080SScott Feldman 		}
82257b61080SScott Feldman 		nla_nest_end(skb, vf_port);
82357b61080SScott Feldman 	}
82457b61080SScott Feldman 
82557b61080SScott Feldman 	nla_nest_end(skb, vf_ports);
82657b61080SScott Feldman 
82757b61080SScott Feldman 	return 0;
8288ca94183SScott Feldman 
8298ca94183SScott Feldman nla_put_failure:
8308ca94183SScott Feldman 	nla_nest_cancel(skb, vf_ports);
8318ca94183SScott Feldman 	return -EMSGSIZE;
83257b61080SScott Feldman }
83357b61080SScott Feldman 
83457b61080SScott Feldman static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
83557b61080SScott Feldman {
83657b61080SScott Feldman 	struct nlattr *port_self;
83757b61080SScott Feldman 	int err;
83857b61080SScott Feldman 
83957b61080SScott Feldman 	port_self = nla_nest_start(skb, IFLA_PORT_SELF);
84057b61080SScott Feldman 	if (!port_self)
84157b61080SScott Feldman 		return -EMSGSIZE;
84257b61080SScott Feldman 
84357b61080SScott Feldman 	err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
84457b61080SScott Feldman 	if (err) {
84557b61080SScott Feldman 		nla_nest_cancel(skb, port_self);
8468ca94183SScott Feldman 		return (err == -EMSGSIZE) ? err : 0;
84757b61080SScott Feldman 	}
84857b61080SScott Feldman 
84957b61080SScott Feldman 	nla_nest_end(skb, port_self);
85057b61080SScott Feldman 
85157b61080SScott Feldman 	return 0;
85257b61080SScott Feldman }
85357b61080SScott Feldman 
85457b61080SScott Feldman static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev)
85557b61080SScott Feldman {
85657b61080SScott Feldman 	int err;
85757b61080SScott Feldman 
85857b61080SScott Feldman 	if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent)
85957b61080SScott Feldman 		return 0;
86057b61080SScott Feldman 
86157b61080SScott Feldman 	err = rtnl_port_self_fill(skb, dev);
86257b61080SScott Feldman 	if (err)
86357b61080SScott Feldman 		return err;
86457b61080SScott Feldman 
86557b61080SScott Feldman 	if (dev_num_vf(dev->dev.parent)) {
86657b61080SScott Feldman 		err = rtnl_vf_ports_fill(skb, dev);
86757b61080SScott Feldman 		if (err)
86857b61080SScott Feldman 			return err;
86957b61080SScott Feldman 	}
87057b61080SScott Feldman 
87157b61080SScott Feldman 	return 0;
87257b61080SScott Feldman }
87357b61080SScott Feldman 
874b60c5115SThomas Graf static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
875575c3e2aSPatrick McHardy 			    int type, u32 pid, u32 seq, u32 change,
876115c9b81SGreg Rose 			    unsigned int flags, u32 ext_filter_mask)
877b60c5115SThomas Graf {
878b60c5115SThomas Graf 	struct ifinfomsg *ifm;
8791da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
88028172739SEric Dumazet 	struct rtnl_link_stats64 temp;
881be1f3c2cSBen Hutchings 	const struct rtnl_link_stats64 *stats;
882f8ff182cSThomas Graf 	struct nlattr *attr, *af_spec;
883f8ff182cSThomas Graf 	struct rtnl_af_ops *af_ops;
8841da177e4SLinus Torvalds 
8852907c35fSEric Dumazet 	ASSERT_RTNL();
886b60c5115SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
887b60c5115SThomas Graf 	if (nlh == NULL)
88826932566SPatrick McHardy 		return -EMSGSIZE;
8891da177e4SLinus Torvalds 
890b60c5115SThomas Graf 	ifm = nlmsg_data(nlh);
891b60c5115SThomas Graf 	ifm->ifi_family = AF_UNSPEC;
892b60c5115SThomas Graf 	ifm->__ifi_pad = 0;
893b60c5115SThomas Graf 	ifm->ifi_type = dev->type;
894b60c5115SThomas Graf 	ifm->ifi_index = dev->ifindex;
895b60c5115SThomas Graf 	ifm->ifi_flags = dev_get_flags(dev);
896b60c5115SThomas Graf 	ifm->ifi_change = change;
8971da177e4SLinus Torvalds 
898a6574349SDavid S. Miller 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
899a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
900a6574349SDavid S. Miller 	    nla_put_u8(skb, IFLA_OPERSTATE,
901a6574349SDavid S. Miller 		       netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
902a6574349SDavid S. Miller 	    nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
903a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
904a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_GROUP, dev->group) ||
905edbc0bb3SBen Greear 	    nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
906a6574349SDavid S. Miller 	    (dev->ifindex != dev->iflink &&
907a6574349SDavid S. Miller 	     nla_put_u32(skb, IFLA_LINK, dev->iflink)) ||
908a6574349SDavid S. Miller 	    (dev->master &&
909a6574349SDavid S. Miller 	     nla_put_u32(skb, IFLA_MASTER, dev->master->ifindex)) ||
910a6574349SDavid S. Miller 	    (dev->qdisc &&
911a6574349SDavid S. Miller 	     nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
912a6574349SDavid S. Miller 	    (dev->ifalias &&
913a6574349SDavid S. Miller 	     nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)))
914a6574349SDavid S. Miller 		goto nla_put_failure;
9150b815a1aSStephen Hemminger 
916b00055aaSStefan Rompf 	if (1) {
9171da177e4SLinus Torvalds 		struct rtnl_link_ifmap map = {
9181da177e4SLinus Torvalds 			.mem_start   = dev->mem_start,
9191da177e4SLinus Torvalds 			.mem_end     = dev->mem_end,
9201da177e4SLinus Torvalds 			.base_addr   = dev->base_addr,
9211da177e4SLinus Torvalds 			.irq         = dev->irq,
9221da177e4SLinus Torvalds 			.dma         = dev->dma,
9231da177e4SLinus Torvalds 			.port        = dev->if_port,
9241da177e4SLinus Torvalds 		};
925a6574349SDavid S. Miller 		if (nla_put(skb, IFLA_MAP, sizeof(map), &map))
926a6574349SDavid S. Miller 			goto nla_put_failure;
9271da177e4SLinus Torvalds 	}
9281da177e4SLinus Torvalds 
9291da177e4SLinus Torvalds 	if (dev->addr_len) {
930a6574349SDavid S. Miller 		if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
931a6574349SDavid S. Miller 		    nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
932a6574349SDavid S. Miller 			goto nla_put_failure;
9331da177e4SLinus Torvalds 	}
9341da177e4SLinus Torvalds 
935b60c5115SThomas Graf 	attr = nla_reserve(skb, IFLA_STATS,
936b60c5115SThomas Graf 			sizeof(struct rtnl_link_stats));
937b60c5115SThomas Graf 	if (attr == NULL)
938b60c5115SThomas Graf 		goto nla_put_failure;
939b60c5115SThomas Graf 
94028172739SEric Dumazet 	stats = dev_get_stats(dev, &temp);
941b60c5115SThomas Graf 	copy_rtnl_link_stats(nla_data(attr), stats);
9421da177e4SLinus Torvalds 
94310708f37SJan Engelhardt 	attr = nla_reserve(skb, IFLA_STATS64,
94410708f37SJan Engelhardt 			sizeof(struct rtnl_link_stats64));
94510708f37SJan Engelhardt 	if (attr == NULL)
94610708f37SJan Engelhardt 		goto nla_put_failure;
94710708f37SJan Engelhardt 	copy_rtnl_link_stats64(nla_data(attr), stats);
94810708f37SJan Engelhardt 
949a6574349SDavid S. Miller 	if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) &&
950a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent)))
951a6574349SDavid S. Miller 		goto nla_put_failure;
95257b61080SScott Feldman 
953115c9b81SGreg Rose 	if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent
954115c9b81SGreg Rose 	    && (ext_filter_mask & RTEXT_FILTER_VF)) {
955ebc08a6fSWilliams, Mitch A 		int i;
956ebc08a6fSWilliams, Mitch A 
957c02db8c6SChris Wright 		struct nlattr *vfinfo, *vf;
958c02db8c6SChris Wright 		int num_vfs = dev_num_vf(dev->dev.parent);
959c02db8c6SChris Wright 
960c02db8c6SChris Wright 		vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
961c02db8c6SChris Wright 		if (!vfinfo)
962c02db8c6SChris Wright 			goto nla_put_failure;
963c02db8c6SChris Wright 		for (i = 0; i < num_vfs; i++) {
964c02db8c6SChris Wright 			struct ifla_vf_info ivi;
965c02db8c6SChris Wright 			struct ifla_vf_mac vf_mac;
966c02db8c6SChris Wright 			struct ifla_vf_vlan vf_vlan;
967c02db8c6SChris Wright 			struct ifla_vf_tx_rate vf_tx_rate;
9685f8444a3SGreg Rose 			struct ifla_vf_spoofchk vf_spoofchk;
9695f8444a3SGreg Rose 
9705f8444a3SGreg Rose 			/*
9715f8444a3SGreg Rose 			 * Not all SR-IOV capable drivers support the
9725f8444a3SGreg Rose 			 * spoofcheck query.  Preset to -1 so the user
9735f8444a3SGreg Rose 			 * space tool can detect that the driver didn't
9745f8444a3SGreg Rose 			 * report anything.
9755f8444a3SGreg Rose 			 */
9765f8444a3SGreg Rose 			ivi.spoofchk = -1;
977ebc08a6fSWilliams, Mitch A 			if (dev->netdev_ops->ndo_get_vf_config(dev, i, &ivi))
978ebc08a6fSWilliams, Mitch A 				break;
9795f8444a3SGreg Rose 			vf_mac.vf =
9805f8444a3SGreg Rose 				vf_vlan.vf =
9815f8444a3SGreg Rose 				vf_tx_rate.vf =
9825f8444a3SGreg Rose 				vf_spoofchk.vf = ivi.vf;
9835f8444a3SGreg Rose 
984c02db8c6SChris Wright 			memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
985c02db8c6SChris Wright 			vf_vlan.vlan = ivi.vlan;
986c02db8c6SChris Wright 			vf_vlan.qos = ivi.qos;
987c02db8c6SChris Wright 			vf_tx_rate.rate = ivi.tx_rate;
9885f8444a3SGreg Rose 			vf_spoofchk.setting = ivi.spoofchk;
989c02db8c6SChris Wright 			vf = nla_nest_start(skb, IFLA_VF_INFO);
990c02db8c6SChris Wright 			if (!vf) {
991c02db8c6SChris Wright 				nla_nest_cancel(skb, vfinfo);
992c02db8c6SChris Wright 				goto nla_put_failure;
993ebc08a6fSWilliams, Mitch A 			}
994a6574349SDavid S. Miller 			if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
995a6574349SDavid S. Miller 			    nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
996a6574349SDavid S. Miller 			    nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
997a6574349SDavid S. Miller 				    &vf_tx_rate) ||
998a6574349SDavid S. Miller 			    nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
999a6574349SDavid S. Miller 				    &vf_spoofchk))
1000a6574349SDavid S. Miller 				goto nla_put_failure;
1001c02db8c6SChris Wright 			nla_nest_end(skb, vf);
1002c02db8c6SChris Wright 		}
1003c02db8c6SChris Wright 		nla_nest_end(skb, vfinfo);
1004ebc08a6fSWilliams, Mitch A 	}
100557b61080SScott Feldman 
100657b61080SScott Feldman 	if (rtnl_port_fill(skb, dev))
100757b61080SScott Feldman 		goto nla_put_failure;
100857b61080SScott Feldman 
100938f7b870SPatrick McHardy 	if (dev->rtnl_link_ops) {
101038f7b870SPatrick McHardy 		if (rtnl_link_fill(skb, dev) < 0)
101138f7b870SPatrick McHardy 			goto nla_put_failure;
101238f7b870SPatrick McHardy 	}
101338f7b870SPatrick McHardy 
1014f8ff182cSThomas Graf 	if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
1015f8ff182cSThomas Graf 		goto nla_put_failure;
1016f8ff182cSThomas Graf 
1017f8ff182cSThomas Graf 	list_for_each_entry(af_ops, &rtnl_af_ops, list) {
1018f8ff182cSThomas Graf 		if (af_ops->fill_link_af) {
1019f8ff182cSThomas Graf 			struct nlattr *af;
1020f8ff182cSThomas Graf 			int err;
1021f8ff182cSThomas Graf 
1022f8ff182cSThomas Graf 			if (!(af = nla_nest_start(skb, af_ops->family)))
1023f8ff182cSThomas Graf 				goto nla_put_failure;
1024f8ff182cSThomas Graf 
1025f8ff182cSThomas Graf 			err = af_ops->fill_link_af(skb, dev);
1026f8ff182cSThomas Graf 
1027f8ff182cSThomas Graf 			/*
1028f8ff182cSThomas Graf 			 * Caller may return ENODATA to indicate that there
1029f8ff182cSThomas Graf 			 * was no data to be dumped. This is not an error, it
1030f8ff182cSThomas Graf 			 * means we should trim the attribute header and
1031f8ff182cSThomas Graf 			 * continue.
1032f8ff182cSThomas Graf 			 */
1033f8ff182cSThomas Graf 			if (err == -ENODATA)
1034f8ff182cSThomas Graf 				nla_nest_cancel(skb, af);
1035f8ff182cSThomas Graf 			else if (err < 0)
1036f8ff182cSThomas Graf 				goto nla_put_failure;
1037f8ff182cSThomas Graf 
1038f8ff182cSThomas Graf 			nla_nest_end(skb, af);
1039f8ff182cSThomas Graf 		}
1040f8ff182cSThomas Graf 	}
1041f8ff182cSThomas Graf 
1042f8ff182cSThomas Graf 	nla_nest_end(skb, af_spec);
1043f8ff182cSThomas Graf 
1044b60c5115SThomas Graf 	return nlmsg_end(skb, nlh);
1045b60c5115SThomas Graf 
1046b60c5115SThomas Graf nla_put_failure:
104726932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
104826932566SPatrick McHardy 	return -EMSGSIZE;
10491da177e4SLinus Torvalds }
10501da177e4SLinus Torvalds 
1051b60c5115SThomas Graf static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
10521da177e4SLinus Torvalds {
10533b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
10547c28bd0bSEric Dumazet 	int h, s_h;
10557c28bd0bSEric Dumazet 	int idx = 0, s_idx;
10561da177e4SLinus Torvalds 	struct net_device *dev;
10577c28bd0bSEric Dumazet 	struct hlist_head *head;
10587c28bd0bSEric Dumazet 	struct hlist_node *node;
1059115c9b81SGreg Rose 	struct nlattr *tb[IFLA_MAX+1];
1060115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
10611da177e4SLinus Torvalds 
10627c28bd0bSEric Dumazet 	s_h = cb->args[0];
10637c28bd0bSEric Dumazet 	s_idx = cb->args[1];
10647c28bd0bSEric Dumazet 
1065e67f88ddSEric Dumazet 	rcu_read_lock();
10664e985adaSThomas Graf 	cb->seq = net->dev_base_seq;
10674e985adaSThomas Graf 
1068a4b64fbeSEric Dumazet 	if (nlmsg_parse(cb->nlh, sizeof(struct rtgenmsg), tb, IFLA_MAX,
1069a4b64fbeSEric Dumazet 			ifla_policy) >= 0) {
1070115c9b81SGreg Rose 
1071115c9b81SGreg Rose 		if (tb[IFLA_EXT_MASK])
1072115c9b81SGreg Rose 			ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1073a4b64fbeSEric Dumazet 	}
1074115c9b81SGreg Rose 
10757c28bd0bSEric Dumazet 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
10767562f876SPavel Emelianov 		idx = 0;
10777c28bd0bSEric Dumazet 		head = &net->dev_index_head[h];
1078e67f88ddSEric Dumazet 		hlist_for_each_entry_rcu(dev, node, head, index_hlist) {
10791da177e4SLinus Torvalds 			if (idx < s_idx)
10807562f876SPavel Emelianov 				goto cont;
1081575c3e2aSPatrick McHardy 			if (rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
1082b6544c0bSJamal Hadi Salim 					     NETLINK_CB(cb->skb).pid,
10837c28bd0bSEric Dumazet 					     cb->nlh->nlmsg_seq, 0,
1084115c9b81SGreg Rose 					     NLM_F_MULTI,
1085115c9b81SGreg Rose 					     ext_filter_mask) <= 0)
10867c28bd0bSEric Dumazet 				goto out;
10874e985adaSThomas Graf 
10884e985adaSThomas Graf 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
10897562f876SPavel Emelianov cont:
10907562f876SPavel Emelianov 			idx++;
10911da177e4SLinus Torvalds 		}
10927c28bd0bSEric Dumazet 	}
10937c28bd0bSEric Dumazet out:
1094e67f88ddSEric Dumazet 	rcu_read_unlock();
10957c28bd0bSEric Dumazet 	cb->args[1] = idx;
10967c28bd0bSEric Dumazet 	cb->args[0] = h;
10971da177e4SLinus Torvalds 
10981da177e4SLinus Torvalds 	return skb->len;
10991da177e4SLinus Torvalds }
11001da177e4SLinus Torvalds 
1101e7199288SPavel Emelianov const struct nla_policy ifla_policy[IFLA_MAX+1] = {
11025176f91eSThomas Graf 	[IFLA_IFNAME]		= { .type = NLA_STRING, .len = IFNAMSIZ-1 },
110338f7b870SPatrick McHardy 	[IFLA_ADDRESS]		= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
110438f7b870SPatrick McHardy 	[IFLA_BROADCAST]	= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
11055176f91eSThomas Graf 	[IFLA_MAP]		= { .len = sizeof(struct rtnl_link_ifmap) },
1106da5e0494SThomas Graf 	[IFLA_MTU]		= { .type = NLA_U32 },
110776e87306SThomas Graf 	[IFLA_LINK]		= { .type = NLA_U32 },
1108fbaec0eaSJiri Pirko 	[IFLA_MASTER]		= { .type = NLA_U32 },
1109da5e0494SThomas Graf 	[IFLA_TXQLEN]		= { .type = NLA_U32 },
1110da5e0494SThomas Graf 	[IFLA_WEIGHT]		= { .type = NLA_U32 },
1111da5e0494SThomas Graf 	[IFLA_OPERSTATE]	= { .type = NLA_U8 },
1112da5e0494SThomas Graf 	[IFLA_LINKMODE]		= { .type = NLA_U8 },
111376e87306SThomas Graf 	[IFLA_LINKINFO]		= { .type = NLA_NESTED },
1114d8a5ec67SEric W. Biederman 	[IFLA_NET_NS_PID]	= { .type = NLA_U32 },
1115f0630529SEric W. Biederman 	[IFLA_NET_NS_FD]	= { .type = NLA_U32 },
11160b815a1aSStephen Hemminger 	[IFLA_IFALIAS]	        = { .type = NLA_STRING, .len = IFALIASZ-1 },
1117c02db8c6SChris Wright 	[IFLA_VFINFO_LIST]	= {. type = NLA_NESTED },
111857b61080SScott Feldman 	[IFLA_VF_PORTS]		= { .type = NLA_NESTED },
111957b61080SScott Feldman 	[IFLA_PORT_SELF]	= { .type = NLA_NESTED },
1120f8ff182cSThomas Graf 	[IFLA_AF_SPEC]		= { .type = NLA_NESTED },
1121115c9b81SGreg Rose 	[IFLA_EXT_MASK]		= { .type = NLA_U32 },
1122edbc0bb3SBen Greear 	[IFLA_PROMISCUITY]	= { .type = NLA_U32 },
1123da5e0494SThomas Graf };
1124e0d087afSEric Dumazet EXPORT_SYMBOL(ifla_policy);
11251da177e4SLinus Torvalds 
112638f7b870SPatrick McHardy static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
112738f7b870SPatrick McHardy 	[IFLA_INFO_KIND]	= { .type = NLA_STRING },
112838f7b870SPatrick McHardy 	[IFLA_INFO_DATA]	= { .type = NLA_NESTED },
112938f7b870SPatrick McHardy };
113038f7b870SPatrick McHardy 
1131c02db8c6SChris Wright static const struct nla_policy ifla_vfinfo_policy[IFLA_VF_INFO_MAX+1] = {
1132c02db8c6SChris Wright 	[IFLA_VF_INFO]		= { .type = NLA_NESTED },
1133c02db8c6SChris Wright };
1134c02db8c6SChris Wright 
1135c02db8c6SChris Wright static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1136c02db8c6SChris Wright 	[IFLA_VF_MAC]		= { .type = NLA_BINARY,
1137c02db8c6SChris Wright 				    .len = sizeof(struct ifla_vf_mac) },
1138c02db8c6SChris Wright 	[IFLA_VF_VLAN]		= { .type = NLA_BINARY,
1139c02db8c6SChris Wright 				    .len = sizeof(struct ifla_vf_vlan) },
1140c02db8c6SChris Wright 	[IFLA_VF_TX_RATE]	= { .type = NLA_BINARY,
1141c02db8c6SChris Wright 				    .len = sizeof(struct ifla_vf_tx_rate) },
114248752f65SGreg Rose 	[IFLA_VF_SPOOFCHK]	= { .type = NLA_BINARY,
114348752f65SGreg Rose 				    .len = sizeof(struct ifla_vf_spoofchk) },
1144c02db8c6SChris Wright };
1145c02db8c6SChris Wright 
114657b61080SScott Feldman static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
114757b61080SScott Feldman 	[IFLA_PORT_VF]		= { .type = NLA_U32 },
114857b61080SScott Feldman 	[IFLA_PORT_PROFILE]	= { .type = NLA_STRING,
114957b61080SScott Feldman 				    .len = PORT_PROFILE_MAX },
115057b61080SScott Feldman 	[IFLA_PORT_VSI_TYPE]	= { .type = NLA_BINARY,
115157b61080SScott Feldman 				    .len = sizeof(struct ifla_port_vsi)},
115257b61080SScott Feldman 	[IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
115357b61080SScott Feldman 				      .len = PORT_UUID_MAX },
115457b61080SScott Feldman 	[IFLA_PORT_HOST_UUID]	= { .type = NLA_STRING,
115557b61080SScott Feldman 				    .len = PORT_UUID_MAX },
115657b61080SScott Feldman 	[IFLA_PORT_REQUEST]	= { .type = NLA_U8, },
115757b61080SScott Feldman 	[IFLA_PORT_RESPONSE]	= { .type = NLA_U16, },
115857b61080SScott Feldman };
115957b61080SScott Feldman 
116081adee47SEric W. Biederman struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
116181adee47SEric W. Biederman {
116281adee47SEric W. Biederman 	struct net *net;
116381adee47SEric W. Biederman 	/* Examine the link attributes and figure out which
116481adee47SEric W. Biederman 	 * network namespace we are talking about.
116581adee47SEric W. Biederman 	 */
116681adee47SEric W. Biederman 	if (tb[IFLA_NET_NS_PID])
116781adee47SEric W. Biederman 		net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
1168f0630529SEric W. Biederman 	else if (tb[IFLA_NET_NS_FD])
1169f0630529SEric W. Biederman 		net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
117081adee47SEric W. Biederman 	else
117181adee47SEric W. Biederman 		net = get_net(src_net);
117281adee47SEric W. Biederman 	return net;
117381adee47SEric W. Biederman }
117481adee47SEric W. Biederman EXPORT_SYMBOL(rtnl_link_get_net);
117581adee47SEric W. Biederman 
11761840bb13SThomas Graf static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
11771840bb13SThomas Graf {
11781840bb13SThomas Graf 	if (dev) {
11791840bb13SThomas Graf 		if (tb[IFLA_ADDRESS] &&
11801840bb13SThomas Graf 		    nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
11811840bb13SThomas Graf 			return -EINVAL;
11821840bb13SThomas Graf 
11831840bb13SThomas Graf 		if (tb[IFLA_BROADCAST] &&
11841840bb13SThomas Graf 		    nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
11851840bb13SThomas Graf 			return -EINVAL;
11861840bb13SThomas Graf 	}
11871840bb13SThomas Graf 
1188cf7afbfeSThomas Graf 	if (tb[IFLA_AF_SPEC]) {
1189cf7afbfeSThomas Graf 		struct nlattr *af;
1190cf7afbfeSThomas Graf 		int rem, err;
1191cf7afbfeSThomas Graf 
1192cf7afbfeSThomas Graf 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1193cf7afbfeSThomas Graf 			const struct rtnl_af_ops *af_ops;
1194cf7afbfeSThomas Graf 
1195cf7afbfeSThomas Graf 			if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1196cf7afbfeSThomas Graf 				return -EAFNOSUPPORT;
1197cf7afbfeSThomas Graf 
1198cf7afbfeSThomas Graf 			if (!af_ops->set_link_af)
1199cf7afbfeSThomas Graf 				return -EOPNOTSUPP;
1200cf7afbfeSThomas Graf 
1201cf7afbfeSThomas Graf 			if (af_ops->validate_link_af) {
12026d3a9a68SKurt Van Dijck 				err = af_ops->validate_link_af(dev, af);
1203cf7afbfeSThomas Graf 				if (err < 0)
1204cf7afbfeSThomas Graf 					return err;
1205cf7afbfeSThomas Graf 			}
1206cf7afbfeSThomas Graf 		}
1207cf7afbfeSThomas Graf 	}
1208cf7afbfeSThomas Graf 
12091840bb13SThomas Graf 	return 0;
12101840bb13SThomas Graf }
12111840bb13SThomas Graf 
1212c02db8c6SChris Wright static int do_setvfinfo(struct net_device *dev, struct nlattr *attr)
1213c02db8c6SChris Wright {
1214c02db8c6SChris Wright 	int rem, err = -EINVAL;
1215c02db8c6SChris Wright 	struct nlattr *vf;
1216c02db8c6SChris Wright 	const struct net_device_ops *ops = dev->netdev_ops;
1217c02db8c6SChris Wright 
1218c02db8c6SChris Wright 	nla_for_each_nested(vf, attr, rem) {
1219c02db8c6SChris Wright 		switch (nla_type(vf)) {
1220c02db8c6SChris Wright 		case IFLA_VF_MAC: {
1221c02db8c6SChris Wright 			struct ifla_vf_mac *ivm;
1222c02db8c6SChris Wright 			ivm = nla_data(vf);
1223c02db8c6SChris Wright 			err = -EOPNOTSUPP;
1224c02db8c6SChris Wright 			if (ops->ndo_set_vf_mac)
1225c02db8c6SChris Wright 				err = ops->ndo_set_vf_mac(dev, ivm->vf,
1226c02db8c6SChris Wright 							  ivm->mac);
1227c02db8c6SChris Wright 			break;
1228c02db8c6SChris Wright 		}
1229c02db8c6SChris Wright 		case IFLA_VF_VLAN: {
1230c02db8c6SChris Wright 			struct ifla_vf_vlan *ivv;
1231c02db8c6SChris Wright 			ivv = nla_data(vf);
1232c02db8c6SChris Wright 			err = -EOPNOTSUPP;
1233c02db8c6SChris Wright 			if (ops->ndo_set_vf_vlan)
1234c02db8c6SChris Wright 				err = ops->ndo_set_vf_vlan(dev, ivv->vf,
1235c02db8c6SChris Wright 							   ivv->vlan,
1236c02db8c6SChris Wright 							   ivv->qos);
1237c02db8c6SChris Wright 			break;
1238c02db8c6SChris Wright 		}
1239c02db8c6SChris Wright 		case IFLA_VF_TX_RATE: {
1240c02db8c6SChris Wright 			struct ifla_vf_tx_rate *ivt;
1241c02db8c6SChris Wright 			ivt = nla_data(vf);
1242c02db8c6SChris Wright 			err = -EOPNOTSUPP;
1243c02db8c6SChris Wright 			if (ops->ndo_set_vf_tx_rate)
1244c02db8c6SChris Wright 				err = ops->ndo_set_vf_tx_rate(dev, ivt->vf,
1245c02db8c6SChris Wright 							      ivt->rate);
1246c02db8c6SChris Wright 			break;
1247c02db8c6SChris Wright 		}
12485f8444a3SGreg Rose 		case IFLA_VF_SPOOFCHK: {
12495f8444a3SGreg Rose 			struct ifla_vf_spoofchk *ivs;
12505f8444a3SGreg Rose 			ivs = nla_data(vf);
12515f8444a3SGreg Rose 			err = -EOPNOTSUPP;
12525f8444a3SGreg Rose 			if (ops->ndo_set_vf_spoofchk)
12535f8444a3SGreg Rose 				err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
12545f8444a3SGreg Rose 							       ivs->setting);
12555f8444a3SGreg Rose 			break;
12565f8444a3SGreg Rose 		}
1257c02db8c6SChris Wright 		default:
1258c02db8c6SChris Wright 			err = -EINVAL;
1259c02db8c6SChris Wright 			break;
1260c02db8c6SChris Wright 		}
1261c02db8c6SChris Wright 		if (err)
1262c02db8c6SChris Wright 			break;
1263c02db8c6SChris Wright 	}
1264c02db8c6SChris Wright 	return err;
1265c02db8c6SChris Wright }
1266c02db8c6SChris Wright 
1267fbaec0eaSJiri Pirko static int do_set_master(struct net_device *dev, int ifindex)
1268fbaec0eaSJiri Pirko {
1269fbaec0eaSJiri Pirko 	struct net_device *master_dev;
1270fbaec0eaSJiri Pirko 	const struct net_device_ops *ops;
1271fbaec0eaSJiri Pirko 	int err;
1272fbaec0eaSJiri Pirko 
1273fbaec0eaSJiri Pirko 	if (dev->master) {
1274fbaec0eaSJiri Pirko 		if (dev->master->ifindex == ifindex)
1275fbaec0eaSJiri Pirko 			return 0;
1276fbaec0eaSJiri Pirko 		ops = dev->master->netdev_ops;
1277fbaec0eaSJiri Pirko 		if (ops->ndo_del_slave) {
1278fbaec0eaSJiri Pirko 			err = ops->ndo_del_slave(dev->master, dev);
1279fbaec0eaSJiri Pirko 			if (err)
1280fbaec0eaSJiri Pirko 				return err;
1281fbaec0eaSJiri Pirko 		} else {
1282fbaec0eaSJiri Pirko 			return -EOPNOTSUPP;
1283fbaec0eaSJiri Pirko 		}
1284fbaec0eaSJiri Pirko 	}
1285fbaec0eaSJiri Pirko 
1286fbaec0eaSJiri Pirko 	if (ifindex) {
1287fbaec0eaSJiri Pirko 		master_dev = __dev_get_by_index(dev_net(dev), ifindex);
1288fbaec0eaSJiri Pirko 		if (!master_dev)
1289fbaec0eaSJiri Pirko 			return -EINVAL;
1290fbaec0eaSJiri Pirko 		ops = master_dev->netdev_ops;
1291fbaec0eaSJiri Pirko 		if (ops->ndo_add_slave) {
1292fbaec0eaSJiri Pirko 			err = ops->ndo_add_slave(master_dev, dev);
1293fbaec0eaSJiri Pirko 			if (err)
1294fbaec0eaSJiri Pirko 				return err;
1295fbaec0eaSJiri Pirko 		} else {
1296fbaec0eaSJiri Pirko 			return -EOPNOTSUPP;
1297fbaec0eaSJiri Pirko 		}
1298fbaec0eaSJiri Pirko 	}
1299fbaec0eaSJiri Pirko 	return 0;
1300fbaec0eaSJiri Pirko }
1301fbaec0eaSJiri Pirko 
13020157f60cSPatrick McHardy static int do_setlink(struct net_device *dev, struct ifinfomsg *ifm,
130338f7b870SPatrick McHardy 		      struct nlattr **tb, char *ifname, int modified)
13040157f60cSPatrick McHardy {
1305d314774cSStephen Hemminger 	const struct net_device_ops *ops = dev->netdev_ops;
130638f7b870SPatrick McHardy 	int send_addr_notify = 0;
13070157f60cSPatrick McHardy 	int err;
13080157f60cSPatrick McHardy 
1309f0630529SEric W. Biederman 	if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
131081adee47SEric W. Biederman 		struct net *net = rtnl_link_get_net(dev_net(dev), tb);
1311d8a5ec67SEric W. Biederman 		if (IS_ERR(net)) {
1312d8a5ec67SEric W. Biederman 			err = PTR_ERR(net);
1313d8a5ec67SEric W. Biederman 			goto errout;
1314d8a5ec67SEric W. Biederman 		}
1315d8a5ec67SEric W. Biederman 		err = dev_change_net_namespace(dev, net, ifname);
1316d8a5ec67SEric W. Biederman 		put_net(net);
1317d8a5ec67SEric W. Biederman 		if (err)
1318d8a5ec67SEric W. Biederman 			goto errout;
1319d8a5ec67SEric W. Biederman 		modified = 1;
1320d8a5ec67SEric W. Biederman 	}
1321d8a5ec67SEric W. Biederman 
13220157f60cSPatrick McHardy 	if (tb[IFLA_MAP]) {
13230157f60cSPatrick McHardy 		struct rtnl_link_ifmap *u_map;
13240157f60cSPatrick McHardy 		struct ifmap k_map;
13250157f60cSPatrick McHardy 
1326d314774cSStephen Hemminger 		if (!ops->ndo_set_config) {
13270157f60cSPatrick McHardy 			err = -EOPNOTSUPP;
13280157f60cSPatrick McHardy 			goto errout;
13290157f60cSPatrick McHardy 		}
13300157f60cSPatrick McHardy 
13310157f60cSPatrick McHardy 		if (!netif_device_present(dev)) {
13320157f60cSPatrick McHardy 			err = -ENODEV;
13330157f60cSPatrick McHardy 			goto errout;
13340157f60cSPatrick McHardy 		}
13350157f60cSPatrick McHardy 
13360157f60cSPatrick McHardy 		u_map = nla_data(tb[IFLA_MAP]);
13370157f60cSPatrick McHardy 		k_map.mem_start = (unsigned long) u_map->mem_start;
13380157f60cSPatrick McHardy 		k_map.mem_end = (unsigned long) u_map->mem_end;
13390157f60cSPatrick McHardy 		k_map.base_addr = (unsigned short) u_map->base_addr;
13400157f60cSPatrick McHardy 		k_map.irq = (unsigned char) u_map->irq;
13410157f60cSPatrick McHardy 		k_map.dma = (unsigned char) u_map->dma;
13420157f60cSPatrick McHardy 		k_map.port = (unsigned char) u_map->port;
13430157f60cSPatrick McHardy 
1344d314774cSStephen Hemminger 		err = ops->ndo_set_config(dev, &k_map);
13450157f60cSPatrick McHardy 		if (err < 0)
13460157f60cSPatrick McHardy 			goto errout;
13470157f60cSPatrick McHardy 
13480157f60cSPatrick McHardy 		modified = 1;
13490157f60cSPatrick McHardy 	}
13500157f60cSPatrick McHardy 
13510157f60cSPatrick McHardy 	if (tb[IFLA_ADDRESS]) {
13520157f60cSPatrick McHardy 		struct sockaddr *sa;
13530157f60cSPatrick McHardy 		int len;
13540157f60cSPatrick McHardy 
1355d314774cSStephen Hemminger 		if (!ops->ndo_set_mac_address) {
13560157f60cSPatrick McHardy 			err = -EOPNOTSUPP;
13570157f60cSPatrick McHardy 			goto errout;
13580157f60cSPatrick McHardy 		}
13590157f60cSPatrick McHardy 
13600157f60cSPatrick McHardy 		if (!netif_device_present(dev)) {
13610157f60cSPatrick McHardy 			err = -ENODEV;
13620157f60cSPatrick McHardy 			goto errout;
13630157f60cSPatrick McHardy 		}
13640157f60cSPatrick McHardy 
13650157f60cSPatrick McHardy 		len = sizeof(sa_family_t) + dev->addr_len;
13660157f60cSPatrick McHardy 		sa = kmalloc(len, GFP_KERNEL);
13670157f60cSPatrick McHardy 		if (!sa) {
13680157f60cSPatrick McHardy 			err = -ENOMEM;
13690157f60cSPatrick McHardy 			goto errout;
13700157f60cSPatrick McHardy 		}
13710157f60cSPatrick McHardy 		sa->sa_family = dev->type;
13720157f60cSPatrick McHardy 		memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
13730157f60cSPatrick McHardy 		       dev->addr_len);
1374d314774cSStephen Hemminger 		err = ops->ndo_set_mac_address(dev, sa);
13750157f60cSPatrick McHardy 		kfree(sa);
13760157f60cSPatrick McHardy 		if (err)
13770157f60cSPatrick McHardy 			goto errout;
13780157f60cSPatrick McHardy 		send_addr_notify = 1;
13790157f60cSPatrick McHardy 		modified = 1;
13800157f60cSPatrick McHardy 	}
13810157f60cSPatrick McHardy 
13820157f60cSPatrick McHardy 	if (tb[IFLA_MTU]) {
13830157f60cSPatrick McHardy 		err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
13840157f60cSPatrick McHardy 		if (err < 0)
13850157f60cSPatrick McHardy 			goto errout;
13860157f60cSPatrick McHardy 		modified = 1;
13870157f60cSPatrick McHardy 	}
13880157f60cSPatrick McHardy 
1389cbda10faSVlad Dogaru 	if (tb[IFLA_GROUP]) {
1390cbda10faSVlad Dogaru 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
1391cbda10faSVlad Dogaru 		modified = 1;
1392cbda10faSVlad Dogaru 	}
1393cbda10faSVlad Dogaru 
13940157f60cSPatrick McHardy 	/*
13950157f60cSPatrick McHardy 	 * Interface selected by interface index but interface
13960157f60cSPatrick McHardy 	 * name provided implies that a name change has been
13970157f60cSPatrick McHardy 	 * requested.
13980157f60cSPatrick McHardy 	 */
13990157f60cSPatrick McHardy 	if (ifm->ifi_index > 0 && ifname[0]) {
14000157f60cSPatrick McHardy 		err = dev_change_name(dev, ifname);
14010157f60cSPatrick McHardy 		if (err < 0)
14020157f60cSPatrick McHardy 			goto errout;
14030157f60cSPatrick McHardy 		modified = 1;
14040157f60cSPatrick McHardy 	}
14050157f60cSPatrick McHardy 
14060b815a1aSStephen Hemminger 	if (tb[IFLA_IFALIAS]) {
14070b815a1aSStephen Hemminger 		err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
14080b815a1aSStephen Hemminger 				    nla_len(tb[IFLA_IFALIAS]));
14090b815a1aSStephen Hemminger 		if (err < 0)
14100b815a1aSStephen Hemminger 			goto errout;
14110b815a1aSStephen Hemminger 		modified = 1;
14120b815a1aSStephen Hemminger 	}
14130b815a1aSStephen Hemminger 
14140157f60cSPatrick McHardy 	if (tb[IFLA_BROADCAST]) {
14150157f60cSPatrick McHardy 		nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
14160157f60cSPatrick McHardy 		send_addr_notify = 1;
14170157f60cSPatrick McHardy 	}
14180157f60cSPatrick McHardy 
14190157f60cSPatrick McHardy 	if (ifm->ifi_flags || ifm->ifi_change) {
14203729d502SPatrick McHardy 		err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
14215f9021cfSJohannes Berg 		if (err < 0)
14225f9021cfSJohannes Berg 			goto errout;
14230157f60cSPatrick McHardy 	}
14240157f60cSPatrick McHardy 
1425fbaec0eaSJiri Pirko 	if (tb[IFLA_MASTER]) {
1426fbaec0eaSJiri Pirko 		err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
1427fbaec0eaSJiri Pirko 		if (err)
1428fbaec0eaSJiri Pirko 			goto errout;
1429fbaec0eaSJiri Pirko 		modified = 1;
1430fbaec0eaSJiri Pirko 	}
1431fbaec0eaSJiri Pirko 
143293b2d4a2SDavid S. Miller 	if (tb[IFLA_TXQLEN])
14330157f60cSPatrick McHardy 		dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
14340157f60cSPatrick McHardy 
14350157f60cSPatrick McHardy 	if (tb[IFLA_OPERSTATE])
143693b2d4a2SDavid S. Miller 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
14370157f60cSPatrick McHardy 
14380157f60cSPatrick McHardy 	if (tb[IFLA_LINKMODE]) {
14390157f60cSPatrick McHardy 		write_lock_bh(&dev_base_lock);
14400157f60cSPatrick McHardy 		dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
144193b2d4a2SDavid S. Miller 		write_unlock_bh(&dev_base_lock);
14420157f60cSPatrick McHardy 	}
14430157f60cSPatrick McHardy 
1444c02db8c6SChris Wright 	if (tb[IFLA_VFINFO_LIST]) {
1445c02db8c6SChris Wright 		struct nlattr *attr;
1446c02db8c6SChris Wright 		int rem;
1447c02db8c6SChris Wright 		nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
1448253683bbSDavid Howells 			if (nla_type(attr) != IFLA_VF_INFO) {
1449253683bbSDavid Howells 				err = -EINVAL;
1450c02db8c6SChris Wright 				goto errout;
1451253683bbSDavid Howells 			}
1452c02db8c6SChris Wright 			err = do_setvfinfo(dev, attr);
1453ebc08a6fSWilliams, Mitch A 			if (err < 0)
1454ebc08a6fSWilliams, Mitch A 				goto errout;
1455ebc08a6fSWilliams, Mitch A 			modified = 1;
1456ebc08a6fSWilliams, Mitch A 		}
1457ebc08a6fSWilliams, Mitch A 	}
14580157f60cSPatrick McHardy 	err = 0;
14590157f60cSPatrick McHardy 
146057b61080SScott Feldman 	if (tb[IFLA_VF_PORTS]) {
146157b61080SScott Feldman 		struct nlattr *port[IFLA_PORT_MAX+1];
146257b61080SScott Feldman 		struct nlattr *attr;
146357b61080SScott Feldman 		int vf;
146457b61080SScott Feldman 		int rem;
146557b61080SScott Feldman 
146657b61080SScott Feldman 		err = -EOPNOTSUPP;
146757b61080SScott Feldman 		if (!ops->ndo_set_vf_port)
146857b61080SScott Feldman 			goto errout;
146957b61080SScott Feldman 
147057b61080SScott Feldman 		nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
147157b61080SScott Feldman 			if (nla_type(attr) != IFLA_VF_PORT)
147257b61080SScott Feldman 				continue;
147357b61080SScott Feldman 			err = nla_parse_nested(port, IFLA_PORT_MAX,
147457b61080SScott Feldman 				attr, ifla_port_policy);
147557b61080SScott Feldman 			if (err < 0)
147657b61080SScott Feldman 				goto errout;
147757b61080SScott Feldman 			if (!port[IFLA_PORT_VF]) {
147857b61080SScott Feldman 				err = -EOPNOTSUPP;
147957b61080SScott Feldman 				goto errout;
148057b61080SScott Feldman 			}
148157b61080SScott Feldman 			vf = nla_get_u32(port[IFLA_PORT_VF]);
148257b61080SScott Feldman 			err = ops->ndo_set_vf_port(dev, vf, port);
148357b61080SScott Feldman 			if (err < 0)
148457b61080SScott Feldman 				goto errout;
148557b61080SScott Feldman 			modified = 1;
148657b61080SScott Feldman 		}
148757b61080SScott Feldman 	}
148857b61080SScott Feldman 	err = 0;
148957b61080SScott Feldman 
149057b61080SScott Feldman 	if (tb[IFLA_PORT_SELF]) {
149157b61080SScott Feldman 		struct nlattr *port[IFLA_PORT_MAX+1];
149257b61080SScott Feldman 
149357b61080SScott Feldman 		err = nla_parse_nested(port, IFLA_PORT_MAX,
149457b61080SScott Feldman 			tb[IFLA_PORT_SELF], ifla_port_policy);
149557b61080SScott Feldman 		if (err < 0)
149657b61080SScott Feldman 			goto errout;
149757b61080SScott Feldman 
149857b61080SScott Feldman 		err = -EOPNOTSUPP;
149957b61080SScott Feldman 		if (ops->ndo_set_vf_port)
150057b61080SScott Feldman 			err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
150157b61080SScott Feldman 		if (err < 0)
150257b61080SScott Feldman 			goto errout;
150357b61080SScott Feldman 		modified = 1;
150457b61080SScott Feldman 	}
1505f8ff182cSThomas Graf 
1506f8ff182cSThomas Graf 	if (tb[IFLA_AF_SPEC]) {
1507f8ff182cSThomas Graf 		struct nlattr *af;
1508f8ff182cSThomas Graf 		int rem;
1509f8ff182cSThomas Graf 
1510f8ff182cSThomas Graf 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1511f8ff182cSThomas Graf 			const struct rtnl_af_ops *af_ops;
1512f8ff182cSThomas Graf 
1513f8ff182cSThomas Graf 			if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1514cf7afbfeSThomas Graf 				BUG();
1515f8ff182cSThomas Graf 
1516cf7afbfeSThomas Graf 			err = af_ops->set_link_af(dev, af);
1517f8ff182cSThomas Graf 			if (err < 0)
1518f8ff182cSThomas Graf 				goto errout;
1519f8ff182cSThomas Graf 
1520f8ff182cSThomas Graf 			modified = 1;
1521f8ff182cSThomas Graf 		}
1522f8ff182cSThomas Graf 	}
152357b61080SScott Feldman 	err = 0;
152457b61080SScott Feldman 
15250157f60cSPatrick McHardy errout:
15260157f60cSPatrick McHardy 	if (err < 0 && modified && net_ratelimit())
15270157f60cSPatrick McHardy 		printk(KERN_WARNING "A link change request failed with "
152825985edcSLucas De Marchi 		       "some changes committed already. Interface %s may "
15290157f60cSPatrick McHardy 		       "have been left with an inconsistent configuration, "
15300157f60cSPatrick McHardy 		       "please check.\n", dev->name);
15310157f60cSPatrick McHardy 
15320157f60cSPatrick McHardy 	if (send_addr_notify)
15330157f60cSPatrick McHardy 		call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
1534f18da145SStefan Gula 
15350157f60cSPatrick McHardy 	return err;
15360157f60cSPatrick McHardy }
15370157f60cSPatrick McHardy 
1538da5e0494SThomas Graf static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1539da5e0494SThomas Graf {
15403b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
1541da5e0494SThomas Graf 	struct ifinfomsg *ifm;
1542da5e0494SThomas Graf 	struct net_device *dev;
15430157f60cSPatrick McHardy 	int err;
1544da5e0494SThomas Graf 	struct nlattr *tb[IFLA_MAX+1];
15451da177e4SLinus Torvalds 	char ifname[IFNAMSIZ];
15461da177e4SLinus Torvalds 
1547da5e0494SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1548da5e0494SThomas Graf 	if (err < 0)
1549da5e0494SThomas Graf 		goto errout;
15501da177e4SLinus Torvalds 
15515176f91eSThomas Graf 	if (tb[IFLA_IFNAME])
15525176f91eSThomas Graf 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
155378e5b891SPatrick McHardy 	else
155478e5b891SPatrick McHardy 		ifname[0] = '\0';
15551da177e4SLinus Torvalds 
15561da177e4SLinus Torvalds 	err = -EINVAL;
1557da5e0494SThomas Graf 	ifm = nlmsg_data(nlh);
155851055be8SPatrick McHardy 	if (ifm->ifi_index > 0)
1559a3d12891SEric Dumazet 		dev = __dev_get_by_index(net, ifm->ifi_index);
1560da5e0494SThomas Graf 	else if (tb[IFLA_IFNAME])
1561a3d12891SEric Dumazet 		dev = __dev_get_by_name(net, ifname);
1562da5e0494SThomas Graf 	else
1563da5e0494SThomas Graf 		goto errout;
15641da177e4SLinus Torvalds 
1565da5e0494SThomas Graf 	if (dev == NULL) {
1566da5e0494SThomas Graf 		err = -ENODEV;
1567da5e0494SThomas Graf 		goto errout;
1568da5e0494SThomas Graf 	}
15691da177e4SLinus Torvalds 
1570e0d087afSEric Dumazet 	err = validate_linkmsg(dev, tb);
1571e0d087afSEric Dumazet 	if (err < 0)
1572a3d12891SEric Dumazet 		goto errout;
1573da5e0494SThomas Graf 
157438f7b870SPatrick McHardy 	err = do_setlink(dev, ifm, tb, ifname, 0);
1575da5e0494SThomas Graf errout:
15761da177e4SLinus Torvalds 	return err;
15771da177e4SLinus Torvalds }
15781da177e4SLinus Torvalds 
157938f7b870SPatrick McHardy static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
158038f7b870SPatrick McHardy {
15813b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
158238f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops;
158338f7b870SPatrick McHardy 	struct net_device *dev;
158438f7b870SPatrick McHardy 	struct ifinfomsg *ifm;
158538f7b870SPatrick McHardy 	char ifname[IFNAMSIZ];
158638f7b870SPatrick McHardy 	struct nlattr *tb[IFLA_MAX+1];
158738f7b870SPatrick McHardy 	int err;
1588226bd341SEric Dumazet 	LIST_HEAD(list_kill);
158938f7b870SPatrick McHardy 
159038f7b870SPatrick McHardy 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
159138f7b870SPatrick McHardy 	if (err < 0)
159238f7b870SPatrick McHardy 		return err;
159338f7b870SPatrick McHardy 
159438f7b870SPatrick McHardy 	if (tb[IFLA_IFNAME])
159538f7b870SPatrick McHardy 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
159638f7b870SPatrick McHardy 
159738f7b870SPatrick McHardy 	ifm = nlmsg_data(nlh);
159838f7b870SPatrick McHardy 	if (ifm->ifi_index > 0)
1599881d966bSEric W. Biederman 		dev = __dev_get_by_index(net, ifm->ifi_index);
160038f7b870SPatrick McHardy 	else if (tb[IFLA_IFNAME])
1601881d966bSEric W. Biederman 		dev = __dev_get_by_name(net, ifname);
160238f7b870SPatrick McHardy 	else
160338f7b870SPatrick McHardy 		return -EINVAL;
160438f7b870SPatrick McHardy 
160538f7b870SPatrick McHardy 	if (!dev)
160638f7b870SPatrick McHardy 		return -ENODEV;
160738f7b870SPatrick McHardy 
160838f7b870SPatrick McHardy 	ops = dev->rtnl_link_ops;
160938f7b870SPatrick McHardy 	if (!ops)
161038f7b870SPatrick McHardy 		return -EOPNOTSUPP;
161138f7b870SPatrick McHardy 
1612226bd341SEric Dumazet 	ops->dellink(dev, &list_kill);
1613226bd341SEric Dumazet 	unregister_netdevice_many(&list_kill);
1614226bd341SEric Dumazet 	list_del(&list_kill);
161538f7b870SPatrick McHardy 	return 0;
161638f7b870SPatrick McHardy }
161738f7b870SPatrick McHardy 
16183729d502SPatrick McHardy int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
16193729d502SPatrick McHardy {
16203729d502SPatrick McHardy 	unsigned int old_flags;
16213729d502SPatrick McHardy 	int err;
16223729d502SPatrick McHardy 
16233729d502SPatrick McHardy 	old_flags = dev->flags;
16243729d502SPatrick McHardy 	if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
16253729d502SPatrick McHardy 		err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
16263729d502SPatrick McHardy 		if (err < 0)
16273729d502SPatrick McHardy 			return err;
16283729d502SPatrick McHardy 	}
16293729d502SPatrick McHardy 
16303729d502SPatrick McHardy 	dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
16313729d502SPatrick McHardy 	rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
16323729d502SPatrick McHardy 
16333729d502SPatrick McHardy 	__dev_notify_flags(dev, old_flags);
16343729d502SPatrick McHardy 	return 0;
16353729d502SPatrick McHardy }
16363729d502SPatrick McHardy EXPORT_SYMBOL(rtnl_configure_link);
16373729d502SPatrick McHardy 
163881adee47SEric W. Biederman struct net_device *rtnl_create_link(struct net *src_net, struct net *net,
163981adee47SEric W. Biederman 	char *ifname, const struct rtnl_link_ops *ops, struct nlattr *tb[])
1640e7199288SPavel Emelianov {
1641e7199288SPavel Emelianov 	int err;
1642e7199288SPavel Emelianov 	struct net_device *dev;
16432e59af3dSEric Dumazet 	unsigned int num_queues = 1;
16442e59af3dSEric Dumazet 	unsigned int real_num_queues = 1;
1645e7199288SPavel Emelianov 
16462e59af3dSEric Dumazet 	if (ops->get_tx_queues) {
164781adee47SEric W. Biederman 		err = ops->get_tx_queues(src_net, tb, &num_queues,
1648e0d087afSEric Dumazet 					 &real_num_queues);
16492e59af3dSEric Dumazet 		if (err)
16502e59af3dSEric Dumazet 			goto err;
16512e59af3dSEric Dumazet 	}
1652e7199288SPavel Emelianov 	err = -ENOMEM;
16532e59af3dSEric Dumazet 	dev = alloc_netdev_mq(ops->priv_size, ifname, ops->setup, num_queues);
1654e7199288SPavel Emelianov 	if (!dev)
1655e7199288SPavel Emelianov 		goto err;
1656e7199288SPavel Emelianov 
165781adee47SEric W. Biederman 	dev_net_set(dev, net);
165881adee47SEric W. Biederman 	dev->rtnl_link_ops = ops;
16593729d502SPatrick McHardy 	dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
166081adee47SEric W. Biederman 
1661e7199288SPavel Emelianov 	if (tb[IFLA_MTU])
1662e7199288SPavel Emelianov 		dev->mtu = nla_get_u32(tb[IFLA_MTU]);
1663e7199288SPavel Emelianov 	if (tb[IFLA_ADDRESS])
1664e7199288SPavel Emelianov 		memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
1665e7199288SPavel Emelianov 				nla_len(tb[IFLA_ADDRESS]));
1666e7199288SPavel Emelianov 	if (tb[IFLA_BROADCAST])
1667e7199288SPavel Emelianov 		memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
1668e7199288SPavel Emelianov 				nla_len(tb[IFLA_BROADCAST]));
1669e7199288SPavel Emelianov 	if (tb[IFLA_TXQLEN])
1670e7199288SPavel Emelianov 		dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
1671e7199288SPavel Emelianov 	if (tb[IFLA_OPERSTATE])
167293b2d4a2SDavid S. Miller 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
1673e7199288SPavel Emelianov 	if (tb[IFLA_LINKMODE])
1674e7199288SPavel Emelianov 		dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
1675ffa934f1SPatrick McHardy 	if (tb[IFLA_GROUP])
1676ffa934f1SPatrick McHardy 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
1677e7199288SPavel Emelianov 
1678e7199288SPavel Emelianov 	return dev;
1679e7199288SPavel Emelianov 
1680e7199288SPavel Emelianov err:
1681e7199288SPavel Emelianov 	return ERR_PTR(err);
1682e7199288SPavel Emelianov }
1683e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_create_link);
1684e7199288SPavel Emelianov 
1685e7ed828fSVlad Dogaru static int rtnl_group_changelink(struct net *net, int group,
1686e7ed828fSVlad Dogaru 		struct ifinfomsg *ifm,
1687e7ed828fSVlad Dogaru 		struct nlattr **tb)
1688e7ed828fSVlad Dogaru {
1689e7ed828fSVlad Dogaru 	struct net_device *dev;
1690e7ed828fSVlad Dogaru 	int err;
1691e7ed828fSVlad Dogaru 
1692e7ed828fSVlad Dogaru 	for_each_netdev(net, dev) {
1693e7ed828fSVlad Dogaru 		if (dev->group == group) {
1694e7ed828fSVlad Dogaru 			err = do_setlink(dev, ifm, tb, NULL, 0);
1695e7ed828fSVlad Dogaru 			if (err < 0)
1696e7ed828fSVlad Dogaru 				return err;
1697e7ed828fSVlad Dogaru 		}
1698e7ed828fSVlad Dogaru 	}
1699e7ed828fSVlad Dogaru 
1700e7ed828fSVlad Dogaru 	return 0;
1701e7ed828fSVlad Dogaru }
1702e7ed828fSVlad Dogaru 
170338f7b870SPatrick McHardy static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
170438f7b870SPatrick McHardy {
17053b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
170638f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops;
170738f7b870SPatrick McHardy 	struct net_device *dev;
170838f7b870SPatrick McHardy 	struct ifinfomsg *ifm;
170938f7b870SPatrick McHardy 	char kind[MODULE_NAME_LEN];
171038f7b870SPatrick McHardy 	char ifname[IFNAMSIZ];
171138f7b870SPatrick McHardy 	struct nlattr *tb[IFLA_MAX+1];
171238f7b870SPatrick McHardy 	struct nlattr *linkinfo[IFLA_INFO_MAX+1];
171338f7b870SPatrick McHardy 	int err;
171438f7b870SPatrick McHardy 
171595a5afcaSJohannes Berg #ifdef CONFIG_MODULES
171638f7b870SPatrick McHardy replay:
17178072f085SThomas Graf #endif
171838f7b870SPatrick McHardy 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
171938f7b870SPatrick McHardy 	if (err < 0)
172038f7b870SPatrick McHardy 		return err;
172138f7b870SPatrick McHardy 
172238f7b870SPatrick McHardy 	if (tb[IFLA_IFNAME])
172338f7b870SPatrick McHardy 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
172438f7b870SPatrick McHardy 	else
172538f7b870SPatrick McHardy 		ifname[0] = '\0';
172638f7b870SPatrick McHardy 
172738f7b870SPatrick McHardy 	ifm = nlmsg_data(nlh);
172838f7b870SPatrick McHardy 	if (ifm->ifi_index > 0)
1729881d966bSEric W. Biederman 		dev = __dev_get_by_index(net, ifm->ifi_index);
1730e7ed828fSVlad Dogaru 	else {
1731e7ed828fSVlad Dogaru 		if (ifname[0])
1732881d966bSEric W. Biederman 			dev = __dev_get_by_name(net, ifname);
173338f7b870SPatrick McHardy 		else
173438f7b870SPatrick McHardy 			dev = NULL;
1735e7ed828fSVlad Dogaru 	}
173638f7b870SPatrick McHardy 
1737e0d087afSEric Dumazet 	err = validate_linkmsg(dev, tb);
1738e0d087afSEric Dumazet 	if (err < 0)
17391840bb13SThomas Graf 		return err;
17401840bb13SThomas Graf 
174138f7b870SPatrick McHardy 	if (tb[IFLA_LINKINFO]) {
174238f7b870SPatrick McHardy 		err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
174338f7b870SPatrick McHardy 				       tb[IFLA_LINKINFO], ifla_info_policy);
174438f7b870SPatrick McHardy 		if (err < 0)
174538f7b870SPatrick McHardy 			return err;
174638f7b870SPatrick McHardy 	} else
174738f7b870SPatrick McHardy 		memset(linkinfo, 0, sizeof(linkinfo));
174838f7b870SPatrick McHardy 
174938f7b870SPatrick McHardy 	if (linkinfo[IFLA_INFO_KIND]) {
175038f7b870SPatrick McHardy 		nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
175138f7b870SPatrick McHardy 		ops = rtnl_link_ops_get(kind);
175238f7b870SPatrick McHardy 	} else {
175338f7b870SPatrick McHardy 		kind[0] = '\0';
175438f7b870SPatrick McHardy 		ops = NULL;
175538f7b870SPatrick McHardy 	}
175638f7b870SPatrick McHardy 
175738f7b870SPatrick McHardy 	if (1) {
175838f7b870SPatrick McHardy 		struct nlattr *attr[ops ? ops->maxtype + 1 : 0], **data = NULL;
175981adee47SEric W. Biederman 		struct net *dest_net;
176038f7b870SPatrick McHardy 
176138f7b870SPatrick McHardy 		if (ops) {
176238f7b870SPatrick McHardy 			if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
176338f7b870SPatrick McHardy 				err = nla_parse_nested(attr, ops->maxtype,
176438f7b870SPatrick McHardy 						       linkinfo[IFLA_INFO_DATA],
176538f7b870SPatrick McHardy 						       ops->policy);
176638f7b870SPatrick McHardy 				if (err < 0)
176738f7b870SPatrick McHardy 					return err;
176838f7b870SPatrick McHardy 				data = attr;
176938f7b870SPatrick McHardy 			}
177038f7b870SPatrick McHardy 			if (ops->validate) {
177138f7b870SPatrick McHardy 				err = ops->validate(tb, data);
177238f7b870SPatrick McHardy 				if (err < 0)
177338f7b870SPatrick McHardy 					return err;
177438f7b870SPatrick McHardy 			}
177538f7b870SPatrick McHardy 		}
177638f7b870SPatrick McHardy 
177738f7b870SPatrick McHardy 		if (dev) {
177838f7b870SPatrick McHardy 			int modified = 0;
177938f7b870SPatrick McHardy 
178038f7b870SPatrick McHardy 			if (nlh->nlmsg_flags & NLM_F_EXCL)
178138f7b870SPatrick McHardy 				return -EEXIST;
178238f7b870SPatrick McHardy 			if (nlh->nlmsg_flags & NLM_F_REPLACE)
178338f7b870SPatrick McHardy 				return -EOPNOTSUPP;
178438f7b870SPatrick McHardy 
178538f7b870SPatrick McHardy 			if (linkinfo[IFLA_INFO_DATA]) {
178638f7b870SPatrick McHardy 				if (!ops || ops != dev->rtnl_link_ops ||
178738f7b870SPatrick McHardy 				    !ops->changelink)
178838f7b870SPatrick McHardy 					return -EOPNOTSUPP;
178938f7b870SPatrick McHardy 
179038f7b870SPatrick McHardy 				err = ops->changelink(dev, tb, data);
179138f7b870SPatrick McHardy 				if (err < 0)
179238f7b870SPatrick McHardy 					return err;
179338f7b870SPatrick McHardy 				modified = 1;
179438f7b870SPatrick McHardy 			}
179538f7b870SPatrick McHardy 
179638f7b870SPatrick McHardy 			return do_setlink(dev, ifm, tb, ifname, modified);
179738f7b870SPatrick McHardy 		}
179838f7b870SPatrick McHardy 
1799ffa934f1SPatrick McHardy 		if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
1800ffa934f1SPatrick McHardy 			if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
1801ffa934f1SPatrick McHardy 				return rtnl_group_changelink(net,
1802ffa934f1SPatrick McHardy 						nla_get_u32(tb[IFLA_GROUP]),
1803ffa934f1SPatrick McHardy 						ifm, tb);
180438f7b870SPatrick McHardy 			return -ENODEV;
1805ffa934f1SPatrick McHardy 		}
180638f7b870SPatrick McHardy 
18073729d502SPatrick McHardy 		if (ifm->ifi_index)
180838f7b870SPatrick McHardy 			return -EOPNOTSUPP;
18090e06877cSPatrick McHardy 		if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
181038f7b870SPatrick McHardy 			return -EOPNOTSUPP;
181138f7b870SPatrick McHardy 
181238f7b870SPatrick McHardy 		if (!ops) {
181395a5afcaSJohannes Berg #ifdef CONFIG_MODULES
181438f7b870SPatrick McHardy 			if (kind[0]) {
181538f7b870SPatrick McHardy 				__rtnl_unlock();
181638f7b870SPatrick McHardy 				request_module("rtnl-link-%s", kind);
181738f7b870SPatrick McHardy 				rtnl_lock();
181838f7b870SPatrick McHardy 				ops = rtnl_link_ops_get(kind);
181938f7b870SPatrick McHardy 				if (ops)
182038f7b870SPatrick McHardy 					goto replay;
182138f7b870SPatrick McHardy 			}
182238f7b870SPatrick McHardy #endif
182338f7b870SPatrick McHardy 			return -EOPNOTSUPP;
182438f7b870SPatrick McHardy 		}
182538f7b870SPatrick McHardy 
182638f7b870SPatrick McHardy 		if (!ifname[0])
182738f7b870SPatrick McHardy 			snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
182838f7b870SPatrick McHardy 
182981adee47SEric W. Biederman 		dest_net = rtnl_link_get_net(net, tb);
183013ad1774SEric W. Biederman 		if (IS_ERR(dest_net))
183113ad1774SEric W. Biederman 			return PTR_ERR(dest_net);
183213ad1774SEric W. Biederman 
183381adee47SEric W. Biederman 		dev = rtnl_create_link(net, dest_net, ifname, ops, tb);
183438f7b870SPatrick McHardy 
1835e7199288SPavel Emelianov 		if (IS_ERR(dev))
1836e7199288SPavel Emelianov 			err = PTR_ERR(dev);
1837e7199288SPavel Emelianov 		else if (ops->newlink)
183881adee47SEric W. Biederman 			err = ops->newlink(net, dev, tb, data);
18392d85cba2SPatrick McHardy 		else
18402d85cba2SPatrick McHardy 			err = register_netdevice(dev);
184180032cffSDan Carpenter 
184280032cffSDan Carpenter 		if (err < 0 && !IS_ERR(dev))
184338f7b870SPatrick McHardy 			free_netdev(dev);
184480032cffSDan Carpenter 		if (err < 0)
18453729d502SPatrick McHardy 			goto out;
184681adee47SEric W. Biederman 
18473729d502SPatrick McHardy 		err = rtnl_configure_link(dev, ifm);
18483729d502SPatrick McHardy 		if (err < 0)
18493729d502SPatrick McHardy 			unregister_netdevice(dev);
18503729d502SPatrick McHardy out:
185181adee47SEric W. Biederman 		put_net(dest_net);
185238f7b870SPatrick McHardy 		return err;
185338f7b870SPatrick McHardy 	}
185438f7b870SPatrick McHardy }
185538f7b870SPatrick McHardy 
1856b60c5115SThomas Graf static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
1857711e2c33SJean Tourrilhes {
18583b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
1859b60c5115SThomas Graf 	struct ifinfomsg *ifm;
1860a3d12891SEric Dumazet 	char ifname[IFNAMSIZ];
1861b60c5115SThomas Graf 	struct nlattr *tb[IFLA_MAX+1];
1862b60c5115SThomas Graf 	struct net_device *dev = NULL;
1863b60c5115SThomas Graf 	struct sk_buff *nskb;
1864339bf98fSThomas Graf 	int err;
1865115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
1866711e2c33SJean Tourrilhes 
1867b60c5115SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1868b60c5115SThomas Graf 	if (err < 0)
18699918f230SEric Sesterhenn 		return err;
1870b60c5115SThomas Graf 
1871a3d12891SEric Dumazet 	if (tb[IFLA_IFNAME])
1872a3d12891SEric Dumazet 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1873a3d12891SEric Dumazet 
1874115c9b81SGreg Rose 	if (tb[IFLA_EXT_MASK])
1875115c9b81SGreg Rose 		ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1876115c9b81SGreg Rose 
1877b60c5115SThomas Graf 	ifm = nlmsg_data(nlh);
1878a3d12891SEric Dumazet 	if (ifm->ifi_index > 0)
1879a3d12891SEric Dumazet 		dev = __dev_get_by_index(net, ifm->ifi_index);
1880a3d12891SEric Dumazet 	else if (tb[IFLA_IFNAME])
1881a3d12891SEric Dumazet 		dev = __dev_get_by_name(net, ifname);
1882a3d12891SEric Dumazet 	else
1883b60c5115SThomas Graf 		return -EINVAL;
1884b60c5115SThomas Graf 
1885a3d12891SEric Dumazet 	if (dev == NULL)
1886a3d12891SEric Dumazet 		return -ENODEV;
1887a3d12891SEric Dumazet 
1888115c9b81SGreg Rose 	nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
1889a3d12891SEric Dumazet 	if (nskb == NULL)
1890a3d12891SEric Dumazet 		return -ENOBUFS;
1891711e2c33SJean Tourrilhes 
1892575c3e2aSPatrick McHardy 	err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).pid,
1893115c9b81SGreg Rose 			       nlh->nlmsg_seq, 0, 0, ext_filter_mask);
189426932566SPatrick McHardy 	if (err < 0) {
189526932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in if_nlmsg_size */
189626932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
189726932566SPatrick McHardy 		kfree_skb(nskb);
1898a3d12891SEric Dumazet 	} else
189997c53cacSDenis V. Lunev 		err = rtnl_unicast(nskb, net, NETLINK_CB(skb).pid);
1900b60c5115SThomas Graf 
1901711e2c33SJean Tourrilhes 	return err;
1902711e2c33SJean Tourrilhes }
1903711e2c33SJean Tourrilhes 
1904115c9b81SGreg Rose static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
1905c7ac8679SGreg Rose {
1906115c9b81SGreg Rose 	struct net *net = sock_net(skb->sk);
1907115c9b81SGreg Rose 	struct net_device *dev;
1908115c9b81SGreg Rose 	struct nlattr *tb[IFLA_MAX+1];
1909115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
1910115c9b81SGreg Rose 	u16 min_ifinfo_dump_size = 0;
1911115c9b81SGreg Rose 
1912a4b64fbeSEric Dumazet 	if (nlmsg_parse(nlh, sizeof(struct rtgenmsg), tb, IFLA_MAX,
1913a4b64fbeSEric Dumazet 			ifla_policy) >= 0) {
1914115c9b81SGreg Rose 		if (tb[IFLA_EXT_MASK])
1915115c9b81SGreg Rose 			ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1916a4b64fbeSEric Dumazet 	}
1917115c9b81SGreg Rose 
1918115c9b81SGreg Rose 	if (!ext_filter_mask)
1919115c9b81SGreg Rose 		return NLMSG_GOODSIZE;
1920115c9b81SGreg Rose 	/*
1921115c9b81SGreg Rose 	 * traverse the list of net devices and compute the minimum
1922115c9b81SGreg Rose 	 * buffer size based upon the filter mask.
1923115c9b81SGreg Rose 	 */
1924115c9b81SGreg Rose 	list_for_each_entry(dev, &net->dev_base_head, dev_list) {
1925115c9b81SGreg Rose 		min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
1926115c9b81SGreg Rose 					     if_nlmsg_size(dev,
1927115c9b81SGreg Rose 						           ext_filter_mask));
1928115c9b81SGreg Rose 	}
1929115c9b81SGreg Rose 
1930c7ac8679SGreg Rose 	return min_ifinfo_dump_size;
1931c7ac8679SGreg Rose }
1932c7ac8679SGreg Rose 
193342bad1daSAdrian Bunk static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
19341da177e4SLinus Torvalds {
19351da177e4SLinus Torvalds 	int idx;
19361da177e4SLinus Torvalds 	int s_idx = cb->family;
19371da177e4SLinus Torvalds 
19381da177e4SLinus Torvalds 	if (s_idx == 0)
19391da177e4SLinus Torvalds 		s_idx = 1;
194025239ceeSPatrick McHardy 	for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
19411da177e4SLinus Torvalds 		int type = cb->nlh->nlmsg_type-RTM_BASE;
19421da177e4SLinus Torvalds 		if (idx < s_idx || idx == PF_PACKET)
19431da177e4SLinus Torvalds 			continue;
1944e2849863SThomas Graf 		if (rtnl_msg_handlers[idx] == NULL ||
1945e2849863SThomas Graf 		    rtnl_msg_handlers[idx][type].dumpit == NULL)
19461da177e4SLinus Torvalds 			continue;
19471da177e4SLinus Torvalds 		if (idx > s_idx)
19481da177e4SLinus Torvalds 			memset(&cb->args[0], 0, sizeof(cb->args));
1949e2849863SThomas Graf 		if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
19501da177e4SLinus Torvalds 			break;
19511da177e4SLinus Torvalds 	}
19521da177e4SLinus Torvalds 	cb->family = idx;
19531da177e4SLinus Torvalds 
19541da177e4SLinus Torvalds 	return skb->len;
19551da177e4SLinus Torvalds }
19561da177e4SLinus Torvalds 
19571da177e4SLinus Torvalds void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change)
19581da177e4SLinus Torvalds {
1959c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
19601da177e4SLinus Torvalds 	struct sk_buff *skb;
19610ec6d3f4SThomas Graf 	int err = -ENOBUFS;
1962c7ac8679SGreg Rose 	size_t if_info_size;
19631da177e4SLinus Torvalds 
1964115c9b81SGreg Rose 	skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), GFP_KERNEL);
19650ec6d3f4SThomas Graf 	if (skb == NULL)
19660ec6d3f4SThomas Graf 		goto errout;
19671da177e4SLinus Torvalds 
1968115c9b81SGreg Rose 	err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0);
196926932566SPatrick McHardy 	if (err < 0) {
197026932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in if_nlmsg_size() */
197126932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
197226932566SPatrick McHardy 		kfree_skb(skb);
197326932566SPatrick McHardy 		goto errout;
197426932566SPatrick McHardy 	}
19751ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
19761ce85fe4SPablo Neira Ayuso 	return;
19770ec6d3f4SThomas Graf errout:
19780ec6d3f4SThomas Graf 	if (err < 0)
19794b3da706SEric W. Biederman 		rtnl_set_sk_err(net, RTNLGRP_LINK, err);
19801da177e4SLinus Torvalds }
19811da177e4SLinus Torvalds 
19821da177e4SLinus Torvalds /* Protected by RTNL sempahore.  */
19831da177e4SLinus Torvalds static struct rtattr **rta_buf;
19841da177e4SLinus Torvalds static int rtattr_max;
19851da177e4SLinus Torvalds 
19861da177e4SLinus Torvalds /* Process one rtnetlink message. */
19871da177e4SLinus Torvalds 
19881d00a4ebSThomas Graf static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
19891da177e4SLinus Torvalds {
19903b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
1991e2849863SThomas Graf 	rtnl_doit_func doit;
19921da177e4SLinus Torvalds 	int sz_idx, kind;
19931da177e4SLinus Torvalds 	int min_len;
19941da177e4SLinus Torvalds 	int family;
19951da177e4SLinus Torvalds 	int type;
19962907c35fSEric Dumazet 	int err;
19971da177e4SLinus Torvalds 
19981da177e4SLinus Torvalds 	type = nlh->nlmsg_type;
19991da177e4SLinus Torvalds 	if (type > RTM_MAX)
2000038890feSThomas Graf 		return -EOPNOTSUPP;
20011da177e4SLinus Torvalds 
20021da177e4SLinus Torvalds 	type -= RTM_BASE;
20031da177e4SLinus Torvalds 
20041da177e4SLinus Torvalds 	/* All the messages must have at least 1 byte length */
20051da177e4SLinus Torvalds 	if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg)))
20061da177e4SLinus Torvalds 		return 0;
20071da177e4SLinus Torvalds 
20081da177e4SLinus Torvalds 	family = ((struct rtgenmsg *)NLMSG_DATA(nlh))->rtgen_family;
20091da177e4SLinus Torvalds 	sz_idx = type>>2;
20101da177e4SLinus Torvalds 	kind = type&3;
20111da177e4SLinus Torvalds 
2012fd778461SEric Paris 	if (kind != 2 && !capable(CAP_NET_ADMIN))
20131d00a4ebSThomas Graf 		return -EPERM;
20141da177e4SLinus Torvalds 
2015b8f3ab42SDavid S. Miller 	if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
201697c53cacSDenis V. Lunev 		struct sock *rtnl;
2017e2849863SThomas Graf 		rtnl_dumpit_func dumpit;
2018c7ac8679SGreg Rose 		rtnl_calcit_func calcit;
2019c7ac8679SGreg Rose 		u16 min_dump_alloc = 0;
20201da177e4SLinus Torvalds 
2021e2849863SThomas Graf 		dumpit = rtnl_get_dumpit(family, type);
2022e2849863SThomas Graf 		if (dumpit == NULL)
2023038890feSThomas Graf 			return -EOPNOTSUPP;
2024c7ac8679SGreg Rose 		calcit = rtnl_get_calcit(family, type);
2025c7ac8679SGreg Rose 		if (calcit)
2026115c9b81SGreg Rose 			min_dump_alloc = calcit(skb, nlh);
20271da177e4SLinus Torvalds 
20282907c35fSEric Dumazet 		__rtnl_unlock();
202997c53cacSDenis V. Lunev 		rtnl = net->rtnl;
203080d326faSPablo Neira Ayuso 		{
203180d326faSPablo Neira Ayuso 			struct netlink_dump_control c = {
203280d326faSPablo Neira Ayuso 				.dump		= dumpit,
203380d326faSPablo Neira Ayuso 				.min_dump_alloc	= min_dump_alloc,
203480d326faSPablo Neira Ayuso 			};
203580d326faSPablo Neira Ayuso 			err = netlink_dump_start(rtnl, skb, nlh, &c);
203680d326faSPablo Neira Ayuso 		}
20372907c35fSEric Dumazet 		rtnl_lock();
20382907c35fSEric Dumazet 		return err;
20391da177e4SLinus Torvalds 	}
20401da177e4SLinus Torvalds 
20411da177e4SLinus Torvalds 	memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *)));
20421da177e4SLinus Torvalds 
20431da177e4SLinus Torvalds 	min_len = rtm_min[sz_idx];
20441da177e4SLinus Torvalds 	if (nlh->nlmsg_len < min_len)
20451d00a4ebSThomas Graf 		return -EINVAL;
20461da177e4SLinus Torvalds 
20471da177e4SLinus Torvalds 	if (nlh->nlmsg_len > min_len) {
20481da177e4SLinus Torvalds 		int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
20491da177e4SLinus Torvalds 		struct rtattr *attr = (void *)nlh + NLMSG_ALIGN(min_len);
20501da177e4SLinus Torvalds 
20511da177e4SLinus Torvalds 		while (RTA_OK(attr, attrlen)) {
20521da177e4SLinus Torvalds 			unsigned flavor = attr->rta_type;
20531da177e4SLinus Torvalds 			if (flavor) {
20541da177e4SLinus Torvalds 				if (flavor > rta_max[sz_idx])
20551d00a4ebSThomas Graf 					return -EINVAL;
20561da177e4SLinus Torvalds 				rta_buf[flavor-1] = attr;
20571da177e4SLinus Torvalds 			}
20581da177e4SLinus Torvalds 			attr = RTA_NEXT(attr, attrlen);
20591da177e4SLinus Torvalds 		}
20601da177e4SLinus Torvalds 	}
20611da177e4SLinus Torvalds 
2062e2849863SThomas Graf 	doit = rtnl_get_doit(family, type);
2063e2849863SThomas Graf 	if (doit == NULL)
2064038890feSThomas Graf 		return -EOPNOTSUPP;
20651da177e4SLinus Torvalds 
20661d00a4ebSThomas Graf 	return doit(skb, nlh, (void *)&rta_buf[0]);
20671da177e4SLinus Torvalds }
20681da177e4SLinus Torvalds 
2069cd40b7d3SDenis V. Lunev static void rtnetlink_rcv(struct sk_buff *skb)
20701da177e4SLinus Torvalds {
20711536cc0dSDenis V. Lunev 	rtnl_lock();
2072cd40b7d3SDenis V. Lunev 	netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
20731536cc0dSDenis V. Lunev 	rtnl_unlock();
20741da177e4SLinus Torvalds }
20751da177e4SLinus Torvalds 
20761da177e4SLinus Torvalds static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
20771da177e4SLinus Torvalds {
20781da177e4SLinus Torvalds 	struct net_device *dev = ptr;
2079e9dc8653SEric W. Biederman 
20801da177e4SLinus Torvalds 	switch (event) {
20811da177e4SLinus Torvalds 	case NETDEV_UP:
20821da177e4SLinus Torvalds 	case NETDEV_DOWN:
208310de05afSPatrick McHardy 	case NETDEV_PRE_UP:
2084d90a909eSEric W. Biederman 	case NETDEV_POST_INIT:
2085d90a909eSEric W. Biederman 	case NETDEV_REGISTER:
20861da177e4SLinus Torvalds 	case NETDEV_CHANGE:
2087755d0e77SPatrick McHardy 	case NETDEV_PRE_TYPE_CHANGE:
20881da177e4SLinus Torvalds 	case NETDEV_GOING_DOWN:
2089a2835763SPatrick McHardy 	case NETDEV_UNREGISTER:
2090d90a909eSEric W. Biederman 	case NETDEV_UNREGISTER_BATCH:
2091ac3d3f81SAmerigo Wang 	case NETDEV_RELEASE:
2092ac3d3f81SAmerigo Wang 	case NETDEV_JOIN:
20931da177e4SLinus Torvalds 		break;
20941da177e4SLinus Torvalds 	default:
20951da177e4SLinus Torvalds 		rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
20961da177e4SLinus Torvalds 		break;
20971da177e4SLinus Torvalds 	}
20981da177e4SLinus Torvalds 	return NOTIFY_DONE;
20991da177e4SLinus Torvalds }
21001da177e4SLinus Torvalds 
21011da177e4SLinus Torvalds static struct notifier_block rtnetlink_dev_notifier = {
21021da177e4SLinus Torvalds 	.notifier_call	= rtnetlink_event,
21031da177e4SLinus Torvalds };
21041da177e4SLinus Torvalds 
210597c53cacSDenis V. Lunev 
21062c8c1e72SAlexey Dobriyan static int __net_init rtnetlink_net_init(struct net *net)
210797c53cacSDenis V. Lunev {
210897c53cacSDenis V. Lunev 	struct sock *sk;
210997c53cacSDenis V. Lunev 	sk = netlink_kernel_create(net, NETLINK_ROUTE, RTNLGRP_MAX,
21102907c35fSEric Dumazet 				   rtnetlink_rcv, &rtnl_mutex, THIS_MODULE);
211197c53cacSDenis V. Lunev 	if (!sk)
211297c53cacSDenis V. Lunev 		return -ENOMEM;
211397c53cacSDenis V. Lunev 	net->rtnl = sk;
211497c53cacSDenis V. Lunev 	return 0;
211597c53cacSDenis V. Lunev }
211697c53cacSDenis V. Lunev 
21172c8c1e72SAlexey Dobriyan static void __net_exit rtnetlink_net_exit(struct net *net)
211897c53cacSDenis V. Lunev {
2119b7c6ba6eSDenis V. Lunev 	netlink_kernel_release(net->rtnl);
212097c53cacSDenis V. Lunev 	net->rtnl = NULL;
212197c53cacSDenis V. Lunev }
212297c53cacSDenis V. Lunev 
212397c53cacSDenis V. Lunev static struct pernet_operations rtnetlink_net_ops = {
212497c53cacSDenis V. Lunev 	.init = rtnetlink_net_init,
212597c53cacSDenis V. Lunev 	.exit = rtnetlink_net_exit,
212697c53cacSDenis V. Lunev };
212797c53cacSDenis V. Lunev 
21281da177e4SLinus Torvalds void __init rtnetlink_init(void)
21291da177e4SLinus Torvalds {
21301da177e4SLinus Torvalds 	int i;
21311da177e4SLinus Torvalds 
21321da177e4SLinus Torvalds 	rtattr_max = 0;
21331da177e4SLinus Torvalds 	for (i = 0; i < ARRAY_SIZE(rta_max); i++)
21341da177e4SLinus Torvalds 		if (rta_max[i] > rtattr_max)
21351da177e4SLinus Torvalds 			rtattr_max = rta_max[i];
21361da177e4SLinus Torvalds 	rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL);
21371da177e4SLinus Torvalds 	if (!rta_buf)
21381da177e4SLinus Torvalds 		panic("rtnetlink_init: cannot allocate rta_buf\n");
21391da177e4SLinus Torvalds 
214097c53cacSDenis V. Lunev 	if (register_pernet_subsys(&rtnetlink_net_ops))
21411da177e4SLinus Torvalds 		panic("rtnetlink_init: cannot initialize rtnetlink\n");
214297c53cacSDenis V. Lunev 
21431da177e4SLinus Torvalds 	netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV);
21441da177e4SLinus Torvalds 	register_netdevice_notifier(&rtnetlink_dev_notifier);
2145340d17fcSThomas Graf 
2146c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
2147c7ac8679SGreg Rose 		      rtnl_dump_ifinfo, rtnl_calcit);
2148c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL);
2149c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL);
2150c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL);
2151687ad8ccSThomas Graf 
2152c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL);
2153c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL);
21541da177e4SLinus Torvalds }
21551da177e4SLinus Torvalds 
2156