xref: /openbmc/linux/net/core/rtnetlink.c (revision c02db8c6290bb992442fec1407643c94cc414375)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * INET		An implementation of the TCP/IP protocol suite for the LINUX
31da177e4SLinus Torvalds  *		operating system.  INET is implemented using the  BSD Socket
41da177e4SLinus Torvalds  *		interface as the means of communication with the user level.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *		Routing netlink socket interface: protocol independent part.
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  *		This program is free software; you can redistribute it and/or
111da177e4SLinus Torvalds  *		modify it under the terms of the GNU General Public License
121da177e4SLinus Torvalds  *		as published by the Free Software Foundation; either version
131da177e4SLinus Torvalds  *		2 of the License, or (at your option) any later version.
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  *	Fixes:
161da177e4SLinus Torvalds  *	Vitaly E. Lavrov		RTA_OK arithmetics was wrong.
171da177e4SLinus Torvalds  */
181da177e4SLinus Torvalds 
191da177e4SLinus Torvalds #include <linux/errno.h>
201da177e4SLinus Torvalds #include <linux/module.h>
211da177e4SLinus Torvalds #include <linux/types.h>
221da177e4SLinus Torvalds #include <linux/socket.h>
231da177e4SLinus Torvalds #include <linux/kernel.h>
241da177e4SLinus Torvalds #include <linux/timer.h>
251da177e4SLinus Torvalds #include <linux/string.h>
261da177e4SLinus Torvalds #include <linux/sockios.h>
271da177e4SLinus Torvalds #include <linux/net.h>
281da177e4SLinus Torvalds #include <linux/fcntl.h>
291da177e4SLinus Torvalds #include <linux/mm.h>
301da177e4SLinus Torvalds #include <linux/slab.h>
311da177e4SLinus Torvalds #include <linux/interrupt.h>
321da177e4SLinus Torvalds #include <linux/capability.h>
331da177e4SLinus Torvalds #include <linux/skbuff.h>
341da177e4SLinus Torvalds #include <linux/init.h>
351da177e4SLinus Torvalds #include <linux/security.h>
366756ae4bSStephen Hemminger #include <linux/mutex.h>
371823730fSThomas Graf #include <linux/if_addr.h>
38ebc08a6fSWilliams, Mitch A #include <linux/pci.h>
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds #include <asm/uaccess.h>
411da177e4SLinus Torvalds #include <asm/system.h>
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds #include <linux/inet.h>
441da177e4SLinus Torvalds #include <linux/netdevice.h>
451da177e4SLinus Torvalds #include <net/ip.h>
461da177e4SLinus Torvalds #include <net/protocol.h>
471da177e4SLinus Torvalds #include <net/arp.h>
481da177e4SLinus Torvalds #include <net/route.h>
491da177e4SLinus Torvalds #include <net/udp.h>
501da177e4SLinus Torvalds #include <net/sock.h>
511da177e4SLinus Torvalds #include <net/pkt_sched.h>
5214c0b97dSThomas Graf #include <net/fib_rules.h>
53e2849863SThomas Graf #include <net/rtnetlink.h>
5430ffee84SJohannes Berg #include <net/net_namespace.h>
551da177e4SLinus Torvalds 
56e0d087afSEric Dumazet struct rtnl_link {
57e2849863SThomas Graf 	rtnl_doit_func		doit;
58e2849863SThomas Graf 	rtnl_dumpit_func	dumpit;
59e2849863SThomas Graf };
60e2849863SThomas Graf 
616756ae4bSStephen Hemminger static DEFINE_MUTEX(rtnl_mutex);
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds void rtnl_lock(void)
641da177e4SLinus Torvalds {
656756ae4bSStephen Hemminger 	mutex_lock(&rtnl_mutex);
661da177e4SLinus Torvalds }
67e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_lock);
681da177e4SLinus Torvalds 
696756ae4bSStephen Hemminger void __rtnl_unlock(void)
701da177e4SLinus Torvalds {
716756ae4bSStephen Hemminger 	mutex_unlock(&rtnl_mutex);
721da177e4SLinus Torvalds }
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds void rtnl_unlock(void)
751da177e4SLinus Torvalds {
7658ec3b4dSHerbert Xu 	/* This fellow will unlock it for us. */
771da177e4SLinus Torvalds 	netdev_run_todo();
781da177e4SLinus Torvalds }
79e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unlock);
801da177e4SLinus Torvalds 
816756ae4bSStephen Hemminger int rtnl_trylock(void)
826756ae4bSStephen Hemminger {
836756ae4bSStephen Hemminger 	return mutex_trylock(&rtnl_mutex);
846756ae4bSStephen Hemminger }
85e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_trylock);
866756ae4bSStephen Hemminger 
87c9c1014bSPatrick McHardy int rtnl_is_locked(void)
88c9c1014bSPatrick McHardy {
89c9c1014bSPatrick McHardy 	return mutex_is_locked(&rtnl_mutex);
90c9c1014bSPatrick McHardy }
91e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_is_locked);
92c9c1014bSPatrick McHardy 
93a898def2SPaul E. McKenney #ifdef CONFIG_PROVE_LOCKING
94a898def2SPaul E. McKenney int lockdep_rtnl_is_held(void)
95a898def2SPaul E. McKenney {
96a898def2SPaul E. McKenney 	return lockdep_is_held(&rtnl_mutex);
97a898def2SPaul E. McKenney }
98a898def2SPaul E. McKenney EXPORT_SYMBOL(lockdep_rtnl_is_held);
99a898def2SPaul E. McKenney #endif /* #ifdef CONFIG_PROVE_LOCKING */
100a898def2SPaul E. McKenney 
10142bad1daSAdrian Bunk static struct rtnl_link *rtnl_msg_handlers[NPROTO];
102e2849863SThomas Graf 
103e2849863SThomas Graf static inline int rtm_msgindex(int msgtype)
104e2849863SThomas Graf {
105e2849863SThomas Graf 	int msgindex = msgtype - RTM_BASE;
106e2849863SThomas Graf 
107e2849863SThomas Graf 	/*
108e2849863SThomas Graf 	 * msgindex < 0 implies someone tried to register a netlink
109e2849863SThomas Graf 	 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
110e2849863SThomas Graf 	 * the message type has not been added to linux/rtnetlink.h
111e2849863SThomas Graf 	 */
112e2849863SThomas Graf 	BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
113e2849863SThomas Graf 
114e2849863SThomas Graf 	return msgindex;
115e2849863SThomas Graf }
116e2849863SThomas Graf 
117e2849863SThomas Graf static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex)
118e2849863SThomas Graf {
119e2849863SThomas Graf 	struct rtnl_link *tab;
120e2849863SThomas Graf 
121e2849863SThomas Graf 	tab = rtnl_msg_handlers[protocol];
12251057f2fSThomas Graf 	if (tab == NULL || tab[msgindex].doit == NULL)
123e2849863SThomas Graf 		tab = rtnl_msg_handlers[PF_UNSPEC];
124e2849863SThomas Graf 
12551057f2fSThomas Graf 	return tab ? tab[msgindex].doit : NULL;
126e2849863SThomas Graf }
127e2849863SThomas Graf 
128e2849863SThomas Graf static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex)
129e2849863SThomas Graf {
130e2849863SThomas Graf 	struct rtnl_link *tab;
131e2849863SThomas Graf 
132e2849863SThomas Graf 	tab = rtnl_msg_handlers[protocol];
13351057f2fSThomas Graf 	if (tab == NULL || tab[msgindex].dumpit == NULL)
134e2849863SThomas Graf 		tab = rtnl_msg_handlers[PF_UNSPEC];
135e2849863SThomas Graf 
13651057f2fSThomas Graf 	return tab ? tab[msgindex].dumpit : NULL;
137e2849863SThomas Graf }
138e2849863SThomas Graf 
139e2849863SThomas Graf /**
140e2849863SThomas Graf  * __rtnl_register - Register a rtnetlink message type
141e2849863SThomas Graf  * @protocol: Protocol family or PF_UNSPEC
142e2849863SThomas Graf  * @msgtype: rtnetlink message type
143e2849863SThomas Graf  * @doit: Function pointer called for each request message
144e2849863SThomas Graf  * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
145e2849863SThomas Graf  *
146e2849863SThomas Graf  * Registers the specified function pointers (at least one of them has
147e2849863SThomas Graf  * to be non-NULL) to be called whenever a request message for the
148e2849863SThomas Graf  * specified protocol family and message type is received.
149e2849863SThomas Graf  *
150e2849863SThomas Graf  * The special protocol family PF_UNSPEC may be used to define fallback
151e2849863SThomas Graf  * function pointers for the case when no entry for the specific protocol
152e2849863SThomas Graf  * family exists.
153e2849863SThomas Graf  *
154e2849863SThomas Graf  * Returns 0 on success or a negative error code.
155e2849863SThomas Graf  */
156e2849863SThomas Graf int __rtnl_register(int protocol, int msgtype,
157e2849863SThomas Graf 		    rtnl_doit_func doit, rtnl_dumpit_func dumpit)
158e2849863SThomas Graf {
159e2849863SThomas Graf 	struct rtnl_link *tab;
160e2849863SThomas Graf 	int msgindex;
161e2849863SThomas Graf 
162e2849863SThomas Graf 	BUG_ON(protocol < 0 || protocol >= NPROTO);
163e2849863SThomas Graf 	msgindex = rtm_msgindex(msgtype);
164e2849863SThomas Graf 
165e2849863SThomas Graf 	tab = rtnl_msg_handlers[protocol];
166e2849863SThomas Graf 	if (tab == NULL) {
167e2849863SThomas Graf 		tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL);
168e2849863SThomas Graf 		if (tab == NULL)
169e2849863SThomas Graf 			return -ENOBUFS;
170e2849863SThomas Graf 
171e2849863SThomas Graf 		rtnl_msg_handlers[protocol] = tab;
172e2849863SThomas Graf 	}
173e2849863SThomas Graf 
174e2849863SThomas Graf 	if (doit)
175e2849863SThomas Graf 		tab[msgindex].doit = doit;
176e2849863SThomas Graf 
177e2849863SThomas Graf 	if (dumpit)
178e2849863SThomas Graf 		tab[msgindex].dumpit = dumpit;
179e2849863SThomas Graf 
180e2849863SThomas Graf 	return 0;
181e2849863SThomas Graf }
182e2849863SThomas Graf EXPORT_SYMBOL_GPL(__rtnl_register);
183e2849863SThomas Graf 
184e2849863SThomas Graf /**
185e2849863SThomas Graf  * rtnl_register - Register a rtnetlink message type
186e2849863SThomas Graf  *
187e2849863SThomas Graf  * Identical to __rtnl_register() but panics on failure. This is useful
188e2849863SThomas Graf  * as failure of this function is very unlikely, it can only happen due
189e2849863SThomas Graf  * to lack of memory when allocating the chain to store all message
190e2849863SThomas Graf  * handlers for a protocol. Meant for use in init functions where lack
191e2849863SThomas Graf  * of memory implies no sense in continueing.
192e2849863SThomas Graf  */
193e2849863SThomas Graf void rtnl_register(int protocol, int msgtype,
194e2849863SThomas Graf 		   rtnl_doit_func doit, rtnl_dumpit_func dumpit)
195e2849863SThomas Graf {
196e2849863SThomas Graf 	if (__rtnl_register(protocol, msgtype, doit, dumpit) < 0)
197e2849863SThomas Graf 		panic("Unable to register rtnetlink message handler, "
198e2849863SThomas Graf 		      "protocol = %d, message type = %d\n",
199e2849863SThomas Graf 		      protocol, msgtype);
200e2849863SThomas Graf }
201e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_register);
202e2849863SThomas Graf 
203e2849863SThomas Graf /**
204e2849863SThomas Graf  * rtnl_unregister - Unregister a rtnetlink message type
205e2849863SThomas Graf  * @protocol: Protocol family or PF_UNSPEC
206e2849863SThomas Graf  * @msgtype: rtnetlink message type
207e2849863SThomas Graf  *
208e2849863SThomas Graf  * Returns 0 on success or a negative error code.
209e2849863SThomas Graf  */
210e2849863SThomas Graf int rtnl_unregister(int protocol, int msgtype)
211e2849863SThomas Graf {
212e2849863SThomas Graf 	int msgindex;
213e2849863SThomas Graf 
214e2849863SThomas Graf 	BUG_ON(protocol < 0 || protocol >= NPROTO);
215e2849863SThomas Graf 	msgindex = rtm_msgindex(msgtype);
216e2849863SThomas Graf 
217e2849863SThomas Graf 	if (rtnl_msg_handlers[protocol] == NULL)
218e2849863SThomas Graf 		return -ENOENT;
219e2849863SThomas Graf 
220e2849863SThomas Graf 	rtnl_msg_handlers[protocol][msgindex].doit = NULL;
221e2849863SThomas Graf 	rtnl_msg_handlers[protocol][msgindex].dumpit = NULL;
222e2849863SThomas Graf 
223e2849863SThomas Graf 	return 0;
224e2849863SThomas Graf }
225e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister);
226e2849863SThomas Graf 
227e2849863SThomas Graf /**
228e2849863SThomas Graf  * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
229e2849863SThomas Graf  * @protocol : Protocol family or PF_UNSPEC
230e2849863SThomas Graf  *
231e2849863SThomas Graf  * Identical to calling rtnl_unregster() for all registered message types
232e2849863SThomas Graf  * of a certain protocol family.
233e2849863SThomas Graf  */
234e2849863SThomas Graf void rtnl_unregister_all(int protocol)
235e2849863SThomas Graf {
236e2849863SThomas Graf 	BUG_ON(protocol < 0 || protocol >= NPROTO);
237e2849863SThomas Graf 
238e2849863SThomas Graf 	kfree(rtnl_msg_handlers[protocol]);
239e2849863SThomas Graf 	rtnl_msg_handlers[protocol] = NULL;
240e2849863SThomas Graf }
241e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister_all);
2421da177e4SLinus Torvalds 
24338f7b870SPatrick McHardy static LIST_HEAD(link_ops);
24438f7b870SPatrick McHardy 
24538f7b870SPatrick McHardy /**
24638f7b870SPatrick McHardy  * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
24738f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to register
24838f7b870SPatrick McHardy  *
24938f7b870SPatrick McHardy  * The caller must hold the rtnl_mutex. This function should be used
25038f7b870SPatrick McHardy  * by drivers that create devices during module initialization. It
25138f7b870SPatrick McHardy  * must be called before registering the devices.
25238f7b870SPatrick McHardy  *
25338f7b870SPatrick McHardy  * Returns 0 on success or a negative error code.
25438f7b870SPatrick McHardy  */
25538f7b870SPatrick McHardy int __rtnl_link_register(struct rtnl_link_ops *ops)
25638f7b870SPatrick McHardy {
2572d85cba2SPatrick McHardy 	if (!ops->dellink)
25823289a37SEric Dumazet 		ops->dellink = unregister_netdevice_queue;
2592d85cba2SPatrick McHardy 
26038f7b870SPatrick McHardy 	list_add_tail(&ops->list, &link_ops);
26138f7b870SPatrick McHardy 	return 0;
26238f7b870SPatrick McHardy }
26338f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_register);
26438f7b870SPatrick McHardy 
26538f7b870SPatrick McHardy /**
26638f7b870SPatrick McHardy  * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
26738f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to register
26838f7b870SPatrick McHardy  *
26938f7b870SPatrick McHardy  * Returns 0 on success or a negative error code.
27038f7b870SPatrick McHardy  */
27138f7b870SPatrick McHardy int rtnl_link_register(struct rtnl_link_ops *ops)
27238f7b870SPatrick McHardy {
27338f7b870SPatrick McHardy 	int err;
27438f7b870SPatrick McHardy 
27538f7b870SPatrick McHardy 	rtnl_lock();
27638f7b870SPatrick McHardy 	err = __rtnl_link_register(ops);
27738f7b870SPatrick McHardy 	rtnl_unlock();
27838f7b870SPatrick McHardy 	return err;
27938f7b870SPatrick McHardy }
28038f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_register);
28138f7b870SPatrick McHardy 
282669f87baSPavel Emelyanov static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
283669f87baSPavel Emelyanov {
284669f87baSPavel Emelyanov 	struct net_device *dev;
28523289a37SEric Dumazet 	LIST_HEAD(list_kill);
28623289a37SEric Dumazet 
287669f87baSPavel Emelyanov 	for_each_netdev(net, dev) {
28823289a37SEric Dumazet 		if (dev->rtnl_link_ops == ops)
28923289a37SEric Dumazet 			ops->dellink(dev, &list_kill);
290669f87baSPavel Emelyanov 	}
29123289a37SEric Dumazet 	unregister_netdevice_many(&list_kill);
292669f87baSPavel Emelyanov }
293669f87baSPavel Emelyanov 
294669f87baSPavel Emelyanov void rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
295669f87baSPavel Emelyanov {
296669f87baSPavel Emelyanov 	rtnl_lock();
297669f87baSPavel Emelyanov 	__rtnl_kill_links(net, ops);
298669f87baSPavel Emelyanov 	rtnl_unlock();
299669f87baSPavel Emelyanov }
300669f87baSPavel Emelyanov EXPORT_SYMBOL_GPL(rtnl_kill_links);
301669f87baSPavel Emelyanov 
30238f7b870SPatrick McHardy /**
30338f7b870SPatrick McHardy  * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
30438f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to unregister
30538f7b870SPatrick McHardy  *
3062d85cba2SPatrick McHardy  * The caller must hold the rtnl_mutex.
30738f7b870SPatrick McHardy  */
30838f7b870SPatrick McHardy void __rtnl_link_unregister(struct rtnl_link_ops *ops)
30938f7b870SPatrick McHardy {
310881d966bSEric W. Biederman 	struct net *net;
3112d85cba2SPatrick McHardy 
312881d966bSEric W. Biederman 	for_each_net(net) {
313669f87baSPavel Emelyanov 		__rtnl_kill_links(net, ops);
314881d966bSEric W. Biederman 	}
31538f7b870SPatrick McHardy 	list_del(&ops->list);
31638f7b870SPatrick McHardy }
31738f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
31838f7b870SPatrick McHardy 
31938f7b870SPatrick McHardy /**
32038f7b870SPatrick McHardy  * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
32138f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to unregister
32238f7b870SPatrick McHardy  */
32338f7b870SPatrick McHardy void rtnl_link_unregister(struct rtnl_link_ops *ops)
32438f7b870SPatrick McHardy {
32538f7b870SPatrick McHardy 	rtnl_lock();
32638f7b870SPatrick McHardy 	__rtnl_link_unregister(ops);
32738f7b870SPatrick McHardy 	rtnl_unlock();
32838f7b870SPatrick McHardy }
32938f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_unregister);
33038f7b870SPatrick McHardy 
33138f7b870SPatrick McHardy static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
33238f7b870SPatrick McHardy {
33338f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops;
33438f7b870SPatrick McHardy 
33538f7b870SPatrick McHardy 	list_for_each_entry(ops, &link_ops, list) {
33638f7b870SPatrick McHardy 		if (!strcmp(ops->kind, kind))
33738f7b870SPatrick McHardy 			return ops;
33838f7b870SPatrick McHardy 	}
33938f7b870SPatrick McHardy 	return NULL;
34038f7b870SPatrick McHardy }
34138f7b870SPatrick McHardy 
34238f7b870SPatrick McHardy static size_t rtnl_link_get_size(const struct net_device *dev)
34338f7b870SPatrick McHardy {
34438f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
34538f7b870SPatrick McHardy 	size_t size;
34638f7b870SPatrick McHardy 
34738f7b870SPatrick McHardy 	if (!ops)
34838f7b870SPatrick McHardy 		return 0;
34938f7b870SPatrick McHardy 
35038f7b870SPatrick McHardy 	size = nlmsg_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
35138f7b870SPatrick McHardy 	       nlmsg_total_size(strlen(ops->kind) + 1);	 /* IFLA_INFO_KIND */
35238f7b870SPatrick McHardy 
35338f7b870SPatrick McHardy 	if (ops->get_size)
35438f7b870SPatrick McHardy 		/* IFLA_INFO_DATA + nested data */
35538f7b870SPatrick McHardy 		size += nlmsg_total_size(sizeof(struct nlattr)) +
35638f7b870SPatrick McHardy 			ops->get_size(dev);
35738f7b870SPatrick McHardy 
35838f7b870SPatrick McHardy 	if (ops->get_xstats_size)
35938f7b870SPatrick McHardy 		size += ops->get_xstats_size(dev);	/* IFLA_INFO_XSTATS */
36038f7b870SPatrick McHardy 
36138f7b870SPatrick McHardy 	return size;
36238f7b870SPatrick McHardy }
36338f7b870SPatrick McHardy 
36438f7b870SPatrick McHardy static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
36538f7b870SPatrick McHardy {
36638f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
36738f7b870SPatrick McHardy 	struct nlattr *linkinfo, *data;
36838f7b870SPatrick McHardy 	int err = -EMSGSIZE;
36938f7b870SPatrick McHardy 
37038f7b870SPatrick McHardy 	linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
37138f7b870SPatrick McHardy 	if (linkinfo == NULL)
37238f7b870SPatrick McHardy 		goto out;
37338f7b870SPatrick McHardy 
37438f7b870SPatrick McHardy 	if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
37538f7b870SPatrick McHardy 		goto err_cancel_link;
37638f7b870SPatrick McHardy 	if (ops->fill_xstats) {
37738f7b870SPatrick McHardy 		err = ops->fill_xstats(skb, dev);
37838f7b870SPatrick McHardy 		if (err < 0)
37938f7b870SPatrick McHardy 			goto err_cancel_link;
38038f7b870SPatrick McHardy 	}
38138f7b870SPatrick McHardy 	if (ops->fill_info) {
38238f7b870SPatrick McHardy 		data = nla_nest_start(skb, IFLA_INFO_DATA);
38338f7b870SPatrick McHardy 		if (data == NULL)
38438f7b870SPatrick McHardy 			goto err_cancel_link;
38538f7b870SPatrick McHardy 		err = ops->fill_info(skb, dev);
38638f7b870SPatrick McHardy 		if (err < 0)
38738f7b870SPatrick McHardy 			goto err_cancel_data;
38838f7b870SPatrick McHardy 		nla_nest_end(skb, data);
38938f7b870SPatrick McHardy 	}
39038f7b870SPatrick McHardy 
39138f7b870SPatrick McHardy 	nla_nest_end(skb, linkinfo);
39238f7b870SPatrick McHardy 	return 0;
39338f7b870SPatrick McHardy 
39438f7b870SPatrick McHardy err_cancel_data:
39538f7b870SPatrick McHardy 	nla_nest_cancel(skb, data);
39638f7b870SPatrick McHardy err_cancel_link:
39738f7b870SPatrick McHardy 	nla_nest_cancel(skb, linkinfo);
39838f7b870SPatrick McHardy out:
39938f7b870SPatrick McHardy 	return err;
40038f7b870SPatrick McHardy }
40138f7b870SPatrick McHardy 
402db46edc6SThomas Graf static const int rtm_min[RTM_NR_FAMILIES] =
4031da177e4SLinus Torvalds {
404f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWLINK)]      = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
405f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWADDR)]      = NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
406f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWROUTE)]     = NLMSG_LENGTH(sizeof(struct rtmsg)),
40714c0b97dSThomas Graf 	[RTM_FAM(RTM_NEWRULE)]      = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)),
408f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWQDISC)]     = NLMSG_LENGTH(sizeof(struct tcmsg)),
409f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWTCLASS)]    = NLMSG_LENGTH(sizeof(struct tcmsg)),
410f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWTFILTER)]   = NLMSG_LENGTH(sizeof(struct tcmsg)),
411f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWACTION)]    = NLMSG_LENGTH(sizeof(struct tcamsg)),
412f90a0a74SThomas Graf 	[RTM_FAM(RTM_GETMULTICAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
413f90a0a74SThomas Graf 	[RTM_FAM(RTM_GETANYCAST)]   = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
4141da177e4SLinus Torvalds };
4151da177e4SLinus Torvalds 
416db46edc6SThomas Graf static const int rta_max[RTM_NR_FAMILIES] =
4171da177e4SLinus Torvalds {
418f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWLINK)]      = IFLA_MAX,
419f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWADDR)]      = IFA_MAX,
420f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWROUTE)]     = RTA_MAX,
42114c0b97dSThomas Graf 	[RTM_FAM(RTM_NEWRULE)]      = FRA_MAX,
422f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWQDISC)]     = TCA_MAX,
423f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWTCLASS)]    = TCA_MAX,
424f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWTFILTER)]   = TCA_MAX,
425f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWACTION)]    = TCAA_MAX,
4261da177e4SLinus Torvalds };
4271da177e4SLinus Torvalds 
4281da177e4SLinus Torvalds void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
4291da177e4SLinus Torvalds {
4301da177e4SLinus Torvalds 	struct rtattr *rta;
4311da177e4SLinus Torvalds 	int size = RTA_LENGTH(attrlen);
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds 	rta = (struct rtattr *)skb_put(skb, RTA_ALIGN(size));
4341da177e4SLinus Torvalds 	rta->rta_type = attrtype;
4351da177e4SLinus Torvalds 	rta->rta_len = size;
4361da177e4SLinus Torvalds 	memcpy(RTA_DATA(rta), data, attrlen);
437b3563c4fSPatrick McHardy 	memset(RTA_DATA(rta) + attrlen, 0, RTA_ALIGN(size) - size);
4381da177e4SLinus Torvalds }
439e0d087afSEric Dumazet EXPORT_SYMBOL(__rta_fill);
4401da177e4SLinus Torvalds 
44197c53cacSDenis V. Lunev int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned group, int echo)
4421da177e4SLinus Torvalds {
44397c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
4441da177e4SLinus Torvalds 	int err = 0;
4451da177e4SLinus Torvalds 
446ac6d439dSPatrick McHardy 	NETLINK_CB(skb).dst_group = group;
4471da177e4SLinus Torvalds 	if (echo)
4481da177e4SLinus Torvalds 		atomic_inc(&skb->users);
4491da177e4SLinus Torvalds 	netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
4501da177e4SLinus Torvalds 	if (echo)
4511da177e4SLinus Torvalds 		err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
4521da177e4SLinus Torvalds 	return err;
4531da177e4SLinus Torvalds }
4541da177e4SLinus Torvalds 
45597c53cacSDenis V. Lunev int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
4562942e900SThomas Graf {
45797c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
45897c53cacSDenis V. Lunev 
4592942e900SThomas Graf 	return nlmsg_unicast(rtnl, skb, pid);
4602942e900SThomas Graf }
461e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unicast);
4622942e900SThomas Graf 
4631ce85fe4SPablo Neira Ayuso void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
46497676b6bSThomas Graf 		 struct nlmsghdr *nlh, gfp_t flags)
46597676b6bSThomas Graf {
46697c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
46797676b6bSThomas Graf 	int report = 0;
46897676b6bSThomas Graf 
46997676b6bSThomas Graf 	if (nlh)
47097676b6bSThomas Graf 		report = nlmsg_report(nlh);
47197676b6bSThomas Graf 
4721ce85fe4SPablo Neira Ayuso 	nlmsg_notify(rtnl, skb, pid, group, report, flags);
47397676b6bSThomas Graf }
474e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_notify);
47597676b6bSThomas Graf 
47697c53cacSDenis V. Lunev void rtnl_set_sk_err(struct net *net, u32 group, int error)
47797676b6bSThomas Graf {
47897c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
47997c53cacSDenis V. Lunev 
48097676b6bSThomas Graf 	netlink_set_err(rtnl, 0, group, error);
48197676b6bSThomas Graf }
482e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_set_sk_err);
48397676b6bSThomas Graf 
4841da177e4SLinus Torvalds int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
4851da177e4SLinus Torvalds {
4862d7202bfSThomas Graf 	struct nlattr *mx;
4872d7202bfSThomas Graf 	int i, valid = 0;
4881da177e4SLinus Torvalds 
4892d7202bfSThomas Graf 	mx = nla_nest_start(skb, RTA_METRICS);
4902d7202bfSThomas Graf 	if (mx == NULL)
4912d7202bfSThomas Graf 		return -ENOBUFS;
4922d7202bfSThomas Graf 
4931da177e4SLinus Torvalds 	for (i = 0; i < RTAX_MAX; i++) {
4942d7202bfSThomas Graf 		if (metrics[i]) {
4952d7202bfSThomas Graf 			valid++;
4962d7202bfSThomas Graf 			NLA_PUT_U32(skb, i+1, metrics[i]);
4971da177e4SLinus Torvalds 		}
4982d7202bfSThomas Graf 	}
4991da177e4SLinus Torvalds 
500a57d27fcSDavid S. Miller 	if (!valid) {
501a57d27fcSDavid S. Miller 		nla_nest_cancel(skb, mx);
502a57d27fcSDavid S. Miller 		return 0;
503a57d27fcSDavid S. Miller 	}
5042d7202bfSThomas Graf 
5052d7202bfSThomas Graf 	return nla_nest_end(skb, mx);
5062d7202bfSThomas Graf 
5072d7202bfSThomas Graf nla_put_failure:
508bc3ed28cSThomas Graf 	nla_nest_cancel(skb, mx);
509bc3ed28cSThomas Graf 	return -EMSGSIZE;
5101da177e4SLinus Torvalds }
511e0d087afSEric Dumazet EXPORT_SYMBOL(rtnetlink_put_metrics);
5121da177e4SLinus Torvalds 
513e3703b3dSThomas Graf int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
514e3703b3dSThomas Graf 		       u32 ts, u32 tsage, long expires, u32 error)
515e3703b3dSThomas Graf {
516e3703b3dSThomas Graf 	struct rta_cacheinfo ci = {
517e3703b3dSThomas Graf 		.rta_lastuse = jiffies_to_clock_t(jiffies - dst->lastuse),
518e3703b3dSThomas Graf 		.rta_used = dst->__use,
519e3703b3dSThomas Graf 		.rta_clntref = atomic_read(&(dst->__refcnt)),
520e3703b3dSThomas Graf 		.rta_error = error,
521e3703b3dSThomas Graf 		.rta_id =  id,
522e3703b3dSThomas Graf 		.rta_ts = ts,
523e3703b3dSThomas Graf 		.rta_tsage = tsage,
524e3703b3dSThomas Graf 	};
525e3703b3dSThomas Graf 
526e3703b3dSThomas Graf 	if (expires)
527e3703b3dSThomas Graf 		ci.rta_expires = jiffies_to_clock_t(expires);
528e3703b3dSThomas Graf 
529e3703b3dSThomas Graf 	return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
530e3703b3dSThomas Graf }
531e3703b3dSThomas Graf EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
5321da177e4SLinus Torvalds 
53393b2d4a2SDavid S. Miller static void set_operstate(struct net_device *dev, unsigned char transition)
534b00055aaSStefan Rompf {
535b00055aaSStefan Rompf 	unsigned char operstate = dev->operstate;
536b00055aaSStefan Rompf 
537b00055aaSStefan Rompf 	switch (transition) {
538b00055aaSStefan Rompf 	case IF_OPER_UP:
539b00055aaSStefan Rompf 		if ((operstate == IF_OPER_DORMANT ||
540b00055aaSStefan Rompf 		     operstate == IF_OPER_UNKNOWN) &&
541b00055aaSStefan Rompf 		    !netif_dormant(dev))
542b00055aaSStefan Rompf 			operstate = IF_OPER_UP;
543b00055aaSStefan Rompf 		break;
544b00055aaSStefan Rompf 
545b00055aaSStefan Rompf 	case IF_OPER_DORMANT:
546b00055aaSStefan Rompf 		if (operstate == IF_OPER_UP ||
547b00055aaSStefan Rompf 		    operstate == IF_OPER_UNKNOWN)
548b00055aaSStefan Rompf 			operstate = IF_OPER_DORMANT;
549b00055aaSStefan Rompf 		break;
5503ff50b79SStephen Hemminger 	}
551b00055aaSStefan Rompf 
552b00055aaSStefan Rompf 	if (dev->operstate != operstate) {
553b00055aaSStefan Rompf 		write_lock_bh(&dev_base_lock);
554b00055aaSStefan Rompf 		dev->operstate = operstate;
555b00055aaSStefan Rompf 		write_unlock_bh(&dev_base_lock);
556b00055aaSStefan Rompf 		netdev_state_change(dev);
55793b2d4a2SDavid S. Miller 	}
558b00055aaSStefan Rompf }
559b00055aaSStefan Rompf 
5603729d502SPatrick McHardy static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
5613729d502SPatrick McHardy 					   const struct ifinfomsg *ifm)
5623729d502SPatrick McHardy {
5633729d502SPatrick McHardy 	unsigned int flags = ifm->ifi_flags;
5643729d502SPatrick McHardy 
5653729d502SPatrick McHardy 	/* bugwards compatibility: ifi_change == 0 is treated as ~0 */
5663729d502SPatrick McHardy 	if (ifm->ifi_change)
5673729d502SPatrick McHardy 		flags = (flags & ifm->ifi_change) |
5683729d502SPatrick McHardy 			(dev->flags & ~ifm->ifi_change);
5693729d502SPatrick McHardy 
5703729d502SPatrick McHardy 	return flags;
5713729d502SPatrick McHardy }
5723729d502SPatrick McHardy 
573b60c5115SThomas Graf static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
574eeda3fd6SStephen Hemminger 				 const struct net_device_stats *b)
5751da177e4SLinus Torvalds {
576b60c5115SThomas Graf 	a->rx_packets = b->rx_packets;
577b60c5115SThomas Graf 	a->tx_packets = b->tx_packets;
578b60c5115SThomas Graf 	a->rx_bytes = b->rx_bytes;
579b60c5115SThomas Graf 	a->tx_bytes = b->tx_bytes;
580b60c5115SThomas Graf 	a->rx_errors = b->rx_errors;
581b60c5115SThomas Graf 	a->tx_errors = b->tx_errors;
582b60c5115SThomas Graf 	a->rx_dropped = b->rx_dropped;
583b60c5115SThomas Graf 	a->tx_dropped = b->tx_dropped;
584b60c5115SThomas Graf 
585b60c5115SThomas Graf 	a->multicast = b->multicast;
586b60c5115SThomas Graf 	a->collisions = b->collisions;
587b60c5115SThomas Graf 
588b60c5115SThomas Graf 	a->rx_length_errors = b->rx_length_errors;
589b60c5115SThomas Graf 	a->rx_over_errors = b->rx_over_errors;
590b60c5115SThomas Graf 	a->rx_crc_errors = b->rx_crc_errors;
591b60c5115SThomas Graf 	a->rx_frame_errors = b->rx_frame_errors;
592b60c5115SThomas Graf 	a->rx_fifo_errors = b->rx_fifo_errors;
593b60c5115SThomas Graf 	a->rx_missed_errors = b->rx_missed_errors;
594b60c5115SThomas Graf 
595b60c5115SThomas Graf 	a->tx_aborted_errors = b->tx_aborted_errors;
596b60c5115SThomas Graf 	a->tx_carrier_errors = b->tx_carrier_errors;
597b60c5115SThomas Graf 	a->tx_fifo_errors = b->tx_fifo_errors;
598b60c5115SThomas Graf 	a->tx_heartbeat_errors = b->tx_heartbeat_errors;
599b60c5115SThomas Graf 	a->tx_window_errors = b->tx_window_errors;
600b60c5115SThomas Graf 
601b60c5115SThomas Graf 	a->rx_compressed = b->rx_compressed;
602b60c5115SThomas Graf 	a->tx_compressed = b->tx_compressed;
603b60c5115SThomas Graf };
604b60c5115SThomas Graf 
605*c02db8c6SChris Wright /* All VF info */
606ebc08a6fSWilliams, Mitch A static inline int rtnl_vfinfo_size(const struct net_device *dev)
607ebc08a6fSWilliams, Mitch A {
608*c02db8c6SChris Wright 	if (dev->dev.parent && dev_is_pci(dev->dev.parent)) {
609*c02db8c6SChris Wright 
610*c02db8c6SChris Wright 		int num_vfs = dev_num_vf(dev->dev.parent);
611*c02db8c6SChris Wright 		size_t size = nlmsg_total_size(sizeof(struct nlattr));
612*c02db8c6SChris Wright 		size += nlmsg_total_size(num_vfs * sizeof(struct nlattr));
613*c02db8c6SChris Wright 		size += num_vfs * (sizeof(struct ifla_vf_mac) +
614*c02db8c6SChris Wright 				  sizeof(struct ifla_vf_vlan) +
615*c02db8c6SChris Wright 				  sizeof(struct ifla_vf_tx_rate));
616*c02db8c6SChris Wright 		return size;
617*c02db8c6SChris Wright 	} else
618ebc08a6fSWilliams, Mitch A 		return 0;
619ebc08a6fSWilliams, Mitch A }
620ebc08a6fSWilliams, Mitch A 
62138f7b870SPatrick McHardy static inline size_t if_nlmsg_size(const struct net_device *dev)
622339bf98fSThomas Graf {
623339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
624339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
6250b815a1aSStephen Hemminger 	       + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
626339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
627339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rtnl_link_ifmap))
628339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rtnl_link_stats))
629339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
630339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
631339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_TXQLEN */
632339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_WEIGHT */
633339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MTU */
634339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_LINK */
635339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MASTER */
636339bf98fSThomas Graf 	       + nla_total_size(1) /* IFLA_OPERSTATE */
63738f7b870SPatrick McHardy 	       + nla_total_size(1) /* IFLA_LINKMODE */
638ebc08a6fSWilliams, Mitch A 	       + nla_total_size(4) /* IFLA_NUM_VF */
639*c02db8c6SChris Wright 	       + rtnl_vfinfo_size(dev) /* IFLA_VFINFO_LIST */
64038f7b870SPatrick McHardy 	       + rtnl_link_get_size(dev); /* IFLA_LINKINFO */
641339bf98fSThomas Graf }
642339bf98fSThomas Graf 
643b60c5115SThomas Graf static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
644575c3e2aSPatrick McHardy 			    int type, u32 pid, u32 seq, u32 change,
645575c3e2aSPatrick McHardy 			    unsigned int flags)
646b60c5115SThomas Graf {
647b60c5115SThomas Graf 	struct ifinfomsg *ifm;
6481da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
649eeda3fd6SStephen Hemminger 	const struct net_device_stats *stats;
65096e74088SPavel Emelyanov 	struct nlattr *attr;
6511da177e4SLinus Torvalds 
652b60c5115SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
653b60c5115SThomas Graf 	if (nlh == NULL)
65426932566SPatrick McHardy 		return -EMSGSIZE;
6551da177e4SLinus Torvalds 
656b60c5115SThomas Graf 	ifm = nlmsg_data(nlh);
657b60c5115SThomas Graf 	ifm->ifi_family = AF_UNSPEC;
658b60c5115SThomas Graf 	ifm->__ifi_pad = 0;
659b60c5115SThomas Graf 	ifm->ifi_type = dev->type;
660b60c5115SThomas Graf 	ifm->ifi_index = dev->ifindex;
661b60c5115SThomas Graf 	ifm->ifi_flags = dev_get_flags(dev);
662b60c5115SThomas Graf 	ifm->ifi_change = change;
6631da177e4SLinus Torvalds 
664b60c5115SThomas Graf 	NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
665b60c5115SThomas Graf 	NLA_PUT_U32(skb, IFLA_TXQLEN, dev->tx_queue_len);
666b60c5115SThomas Graf 	NLA_PUT_U8(skb, IFLA_OPERSTATE,
667b60c5115SThomas Graf 		   netif_running(dev) ? dev->operstate : IF_OPER_DOWN);
668b60c5115SThomas Graf 	NLA_PUT_U8(skb, IFLA_LINKMODE, dev->link_mode);
669b60c5115SThomas Graf 	NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
6701da177e4SLinus Torvalds 
671b60c5115SThomas Graf 	if (dev->ifindex != dev->iflink)
672b60c5115SThomas Graf 		NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
6731da177e4SLinus Torvalds 
674b60c5115SThomas Graf 	if (dev->master)
675b60c5115SThomas Graf 		NLA_PUT_U32(skb, IFLA_MASTER, dev->master->ifindex);
676b60c5115SThomas Graf 
677af356afaSPatrick McHardy 	if (dev->qdisc)
678af356afaSPatrick McHardy 		NLA_PUT_STRING(skb, IFLA_QDISC, dev->qdisc->ops->id);
679b00055aaSStefan Rompf 
6800b815a1aSStephen Hemminger 	if (dev->ifalias)
6810b815a1aSStephen Hemminger 		NLA_PUT_STRING(skb, IFLA_IFALIAS, dev->ifalias);
6820b815a1aSStephen Hemminger 
683b00055aaSStefan Rompf 	if (1) {
6841da177e4SLinus Torvalds 		struct rtnl_link_ifmap map = {
6851da177e4SLinus Torvalds 			.mem_start   = dev->mem_start,
6861da177e4SLinus Torvalds 			.mem_end     = dev->mem_end,
6871da177e4SLinus Torvalds 			.base_addr   = dev->base_addr,
6881da177e4SLinus Torvalds 			.irq         = dev->irq,
6891da177e4SLinus Torvalds 			.dma         = dev->dma,
6901da177e4SLinus Torvalds 			.port        = dev->if_port,
6911da177e4SLinus Torvalds 		};
692b60c5115SThomas Graf 		NLA_PUT(skb, IFLA_MAP, sizeof(map), &map);
6931da177e4SLinus Torvalds 	}
6941da177e4SLinus Torvalds 
6951da177e4SLinus Torvalds 	if (dev->addr_len) {
696b60c5115SThomas Graf 		NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
697b60c5115SThomas Graf 		NLA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast);
6981da177e4SLinus Torvalds 	}
6991da177e4SLinus Torvalds 
700b60c5115SThomas Graf 	attr = nla_reserve(skb, IFLA_STATS,
701b60c5115SThomas Graf 			sizeof(struct rtnl_link_stats));
702b60c5115SThomas Graf 	if (attr == NULL)
703b60c5115SThomas Graf 		goto nla_put_failure;
704b60c5115SThomas Graf 
705eeda3fd6SStephen Hemminger 	stats = dev_get_stats(dev);
706b60c5115SThomas Graf 	copy_rtnl_link_stats(nla_data(attr), stats);
7071da177e4SLinus Torvalds 
708ebc08a6fSWilliams, Mitch A 	if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent) {
709ebc08a6fSWilliams, Mitch A 		int i;
710ebc08a6fSWilliams, Mitch A 
711*c02db8c6SChris Wright 		struct nlattr *vfinfo, *vf;
712*c02db8c6SChris Wright 		int num_vfs = dev_num_vf(dev->dev.parent);
713*c02db8c6SChris Wright 
714*c02db8c6SChris Wright 		NLA_PUT_U32(skb, IFLA_NUM_VF, num_vfs);
715*c02db8c6SChris Wright 		vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
716*c02db8c6SChris Wright 		if (!vfinfo)
717*c02db8c6SChris Wright 			goto nla_put_failure;
718*c02db8c6SChris Wright 		for (i = 0; i < num_vfs; i++) {
719*c02db8c6SChris Wright 			struct ifla_vf_info ivi;
720*c02db8c6SChris Wright 			struct ifla_vf_mac vf_mac;
721*c02db8c6SChris Wright 			struct ifla_vf_vlan vf_vlan;
722*c02db8c6SChris Wright 			struct ifla_vf_tx_rate vf_tx_rate;
723ebc08a6fSWilliams, Mitch A 			if (dev->netdev_ops->ndo_get_vf_config(dev, i, &ivi))
724ebc08a6fSWilliams, Mitch A 				break;
725*c02db8c6SChris Wright 			vf_mac.vf = vf_vlan.vf = vf_tx_rate.vf = ivi.vf;
726*c02db8c6SChris Wright 			memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
727*c02db8c6SChris Wright 			vf_vlan.vlan = ivi.vlan;
728*c02db8c6SChris Wright 			vf_vlan.qos = ivi.qos;
729*c02db8c6SChris Wright 			vf_tx_rate.rate = ivi.tx_rate;
730*c02db8c6SChris Wright 			vf = nla_nest_start(skb, IFLA_VF_INFO);
731*c02db8c6SChris Wright 			if (!vf) {
732*c02db8c6SChris Wright 				nla_nest_cancel(skb, vfinfo);
733*c02db8c6SChris Wright 				goto nla_put_failure;
734ebc08a6fSWilliams, Mitch A 			}
735*c02db8c6SChris Wright 			NLA_PUT(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac);
736*c02db8c6SChris Wright 			NLA_PUT(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan);
737*c02db8c6SChris Wright 			NLA_PUT(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate), &vf_tx_rate);
738*c02db8c6SChris Wright 			nla_nest_end(skb, vf);
739*c02db8c6SChris Wright 		}
740*c02db8c6SChris Wright 		nla_nest_end(skb, vfinfo);
741ebc08a6fSWilliams, Mitch A 	}
74238f7b870SPatrick McHardy 	if (dev->rtnl_link_ops) {
74338f7b870SPatrick McHardy 		if (rtnl_link_fill(skb, dev) < 0)
74438f7b870SPatrick McHardy 			goto nla_put_failure;
74538f7b870SPatrick McHardy 	}
74638f7b870SPatrick McHardy 
747b60c5115SThomas Graf 	return nlmsg_end(skb, nlh);
748b60c5115SThomas Graf 
749b60c5115SThomas Graf nla_put_failure:
75026932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
75126932566SPatrick McHardy 	return -EMSGSIZE;
7521da177e4SLinus Torvalds }
7531da177e4SLinus Torvalds 
754b60c5115SThomas Graf static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
7551da177e4SLinus Torvalds {
7563b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
7577c28bd0bSEric Dumazet 	int h, s_h;
7587c28bd0bSEric Dumazet 	int idx = 0, s_idx;
7591da177e4SLinus Torvalds 	struct net_device *dev;
7607c28bd0bSEric Dumazet 	struct hlist_head *head;
7617c28bd0bSEric Dumazet 	struct hlist_node *node;
7621da177e4SLinus Torvalds 
7637c28bd0bSEric Dumazet 	s_h = cb->args[0];
7647c28bd0bSEric Dumazet 	s_idx = cb->args[1];
7657c28bd0bSEric Dumazet 
7667c28bd0bSEric Dumazet 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
7677562f876SPavel Emelianov 		idx = 0;
7687c28bd0bSEric Dumazet 		head = &net->dev_index_head[h];
7697c28bd0bSEric Dumazet 		hlist_for_each_entry(dev, node, head, index_hlist) {
7701da177e4SLinus Torvalds 			if (idx < s_idx)
7717562f876SPavel Emelianov 				goto cont;
772575c3e2aSPatrick McHardy 			if (rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
773b6544c0bSJamal Hadi Salim 					     NETLINK_CB(cb->skb).pid,
7747c28bd0bSEric Dumazet 					     cb->nlh->nlmsg_seq, 0,
7757c28bd0bSEric Dumazet 					     NLM_F_MULTI) <= 0)
7767c28bd0bSEric Dumazet 				goto out;
7777562f876SPavel Emelianov cont:
7787562f876SPavel Emelianov 			idx++;
7791da177e4SLinus Torvalds 		}
7807c28bd0bSEric Dumazet 	}
7817c28bd0bSEric Dumazet out:
7827c28bd0bSEric Dumazet 	cb->args[1] = idx;
7837c28bd0bSEric Dumazet 	cb->args[0] = h;
7841da177e4SLinus Torvalds 
7851da177e4SLinus Torvalds 	return skb->len;
7861da177e4SLinus Torvalds }
7871da177e4SLinus Torvalds 
788e7199288SPavel Emelianov const struct nla_policy ifla_policy[IFLA_MAX+1] = {
7895176f91eSThomas Graf 	[IFLA_IFNAME]		= { .type = NLA_STRING, .len = IFNAMSIZ-1 },
79038f7b870SPatrick McHardy 	[IFLA_ADDRESS]		= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
79138f7b870SPatrick McHardy 	[IFLA_BROADCAST]	= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
7925176f91eSThomas Graf 	[IFLA_MAP]		= { .len = sizeof(struct rtnl_link_ifmap) },
793da5e0494SThomas Graf 	[IFLA_MTU]		= { .type = NLA_U32 },
79476e87306SThomas Graf 	[IFLA_LINK]		= { .type = NLA_U32 },
795da5e0494SThomas Graf 	[IFLA_TXQLEN]		= { .type = NLA_U32 },
796da5e0494SThomas Graf 	[IFLA_WEIGHT]		= { .type = NLA_U32 },
797da5e0494SThomas Graf 	[IFLA_OPERSTATE]	= { .type = NLA_U8 },
798da5e0494SThomas Graf 	[IFLA_LINKMODE]		= { .type = NLA_U8 },
79976e87306SThomas Graf 	[IFLA_LINKINFO]		= { .type = NLA_NESTED },
800d8a5ec67SEric W. Biederman 	[IFLA_NET_NS_PID]	= { .type = NLA_U32 },
8010b815a1aSStephen Hemminger 	[IFLA_IFALIAS]	        = { .type = NLA_STRING, .len = IFALIASZ-1 },
802*c02db8c6SChris Wright 	[IFLA_VFINFO_LIST]	= {. type = NLA_NESTED },
803da5e0494SThomas Graf };
804e0d087afSEric Dumazet EXPORT_SYMBOL(ifla_policy);
8051da177e4SLinus Torvalds 
80638f7b870SPatrick McHardy static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
80738f7b870SPatrick McHardy 	[IFLA_INFO_KIND]	= { .type = NLA_STRING },
80838f7b870SPatrick McHardy 	[IFLA_INFO_DATA]	= { .type = NLA_NESTED },
80938f7b870SPatrick McHardy };
81038f7b870SPatrick McHardy 
811*c02db8c6SChris Wright static const struct nla_policy ifla_vfinfo_policy[IFLA_VF_INFO_MAX+1] = {
812*c02db8c6SChris Wright 	[IFLA_VF_INFO]		= { .type = NLA_NESTED },
813*c02db8c6SChris Wright };
814*c02db8c6SChris Wright 
815*c02db8c6SChris Wright static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
816*c02db8c6SChris Wright 	[IFLA_VF_MAC]		= { .type = NLA_BINARY,
817*c02db8c6SChris Wright 				    .len = sizeof(struct ifla_vf_mac) },
818*c02db8c6SChris Wright 	[IFLA_VF_VLAN]		= { .type = NLA_BINARY,
819*c02db8c6SChris Wright 				    .len = sizeof(struct ifla_vf_vlan) },
820*c02db8c6SChris Wright 	[IFLA_VF_TX_RATE]	= { .type = NLA_BINARY,
821*c02db8c6SChris Wright 				    .len = sizeof(struct ifla_vf_tx_rate) },
822*c02db8c6SChris Wright };
823*c02db8c6SChris Wright 
82481adee47SEric W. Biederman struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
82581adee47SEric W. Biederman {
82681adee47SEric W. Biederman 	struct net *net;
82781adee47SEric W. Biederman 	/* Examine the link attributes and figure out which
82881adee47SEric W. Biederman 	 * network namespace we are talking about.
82981adee47SEric W. Biederman 	 */
83081adee47SEric W. Biederman 	if (tb[IFLA_NET_NS_PID])
83181adee47SEric W. Biederman 		net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
83281adee47SEric W. Biederman 	else
83381adee47SEric W. Biederman 		net = get_net(src_net);
83481adee47SEric W. Biederman 	return net;
83581adee47SEric W. Biederman }
83681adee47SEric W. Biederman EXPORT_SYMBOL(rtnl_link_get_net);
83781adee47SEric W. Biederman 
8381840bb13SThomas Graf static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
8391840bb13SThomas Graf {
8401840bb13SThomas Graf 	if (dev) {
8411840bb13SThomas Graf 		if (tb[IFLA_ADDRESS] &&
8421840bb13SThomas Graf 		    nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
8431840bb13SThomas Graf 			return -EINVAL;
8441840bb13SThomas Graf 
8451840bb13SThomas Graf 		if (tb[IFLA_BROADCAST] &&
8461840bb13SThomas Graf 		    nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
8471840bb13SThomas Graf 			return -EINVAL;
8481840bb13SThomas Graf 	}
8491840bb13SThomas Graf 
8501840bb13SThomas Graf 	return 0;
8511840bb13SThomas Graf }
8521840bb13SThomas Graf 
853*c02db8c6SChris Wright static int do_setvfinfo(struct net_device *dev, struct nlattr *attr)
854*c02db8c6SChris Wright {
855*c02db8c6SChris Wright 	int rem, err = -EINVAL;
856*c02db8c6SChris Wright 	struct nlattr *vf;
857*c02db8c6SChris Wright 	const struct net_device_ops *ops = dev->netdev_ops;
858*c02db8c6SChris Wright 
859*c02db8c6SChris Wright 	nla_for_each_nested(vf, attr, rem) {
860*c02db8c6SChris Wright 		switch (nla_type(vf)) {
861*c02db8c6SChris Wright 		case IFLA_VF_MAC: {
862*c02db8c6SChris Wright 			struct ifla_vf_mac *ivm;
863*c02db8c6SChris Wright 			ivm = nla_data(vf);
864*c02db8c6SChris Wright 			err = -EOPNOTSUPP;
865*c02db8c6SChris Wright 			if (ops->ndo_set_vf_mac)
866*c02db8c6SChris Wright 				err = ops->ndo_set_vf_mac(dev, ivm->vf,
867*c02db8c6SChris Wright 							  ivm->mac);
868*c02db8c6SChris Wright 			break;
869*c02db8c6SChris Wright 		}
870*c02db8c6SChris Wright 		case IFLA_VF_VLAN: {
871*c02db8c6SChris Wright 			struct ifla_vf_vlan *ivv;
872*c02db8c6SChris Wright 			ivv = nla_data(vf);
873*c02db8c6SChris Wright 			err = -EOPNOTSUPP;
874*c02db8c6SChris Wright 			if (ops->ndo_set_vf_vlan)
875*c02db8c6SChris Wright 				err = ops->ndo_set_vf_vlan(dev, ivv->vf,
876*c02db8c6SChris Wright 							   ivv->vlan,
877*c02db8c6SChris Wright 							   ivv->qos);
878*c02db8c6SChris Wright 			break;
879*c02db8c6SChris Wright 		}
880*c02db8c6SChris Wright 		case IFLA_VF_TX_RATE: {
881*c02db8c6SChris Wright 			struct ifla_vf_tx_rate *ivt;
882*c02db8c6SChris Wright 			ivt = nla_data(vf);
883*c02db8c6SChris Wright 			err = -EOPNOTSUPP;
884*c02db8c6SChris Wright 			if (ops->ndo_set_vf_tx_rate)
885*c02db8c6SChris Wright 				err = ops->ndo_set_vf_tx_rate(dev, ivt->vf,
886*c02db8c6SChris Wright 							      ivt->rate);
887*c02db8c6SChris Wright 			break;
888*c02db8c6SChris Wright 		}
889*c02db8c6SChris Wright 		default:
890*c02db8c6SChris Wright 			err = -EINVAL;
891*c02db8c6SChris Wright 			break;
892*c02db8c6SChris Wright 		}
893*c02db8c6SChris Wright 		if (err)
894*c02db8c6SChris Wright 			break;
895*c02db8c6SChris Wright 	}
896*c02db8c6SChris Wright 	return err;
897*c02db8c6SChris Wright }
898*c02db8c6SChris Wright 
8990157f60cSPatrick McHardy static int do_setlink(struct net_device *dev, struct ifinfomsg *ifm,
90038f7b870SPatrick McHardy 		      struct nlattr **tb, char *ifname, int modified)
9010157f60cSPatrick McHardy {
902d314774cSStephen Hemminger 	const struct net_device_ops *ops = dev->netdev_ops;
90338f7b870SPatrick McHardy 	int send_addr_notify = 0;
9040157f60cSPatrick McHardy 	int err;
9050157f60cSPatrick McHardy 
906d8a5ec67SEric W. Biederman 	if (tb[IFLA_NET_NS_PID]) {
90781adee47SEric W. Biederman 		struct net *net = rtnl_link_get_net(dev_net(dev), tb);
908d8a5ec67SEric W. Biederman 		if (IS_ERR(net)) {
909d8a5ec67SEric W. Biederman 			err = PTR_ERR(net);
910d8a5ec67SEric W. Biederman 			goto errout;
911d8a5ec67SEric W. Biederman 		}
912d8a5ec67SEric W. Biederman 		err = dev_change_net_namespace(dev, net, ifname);
913d8a5ec67SEric W. Biederman 		put_net(net);
914d8a5ec67SEric W. Biederman 		if (err)
915d8a5ec67SEric W. Biederman 			goto errout;
916d8a5ec67SEric W. Biederman 		modified = 1;
917d8a5ec67SEric W. Biederman 	}
918d8a5ec67SEric W. Biederman 
9190157f60cSPatrick McHardy 	if (tb[IFLA_MAP]) {
9200157f60cSPatrick McHardy 		struct rtnl_link_ifmap *u_map;
9210157f60cSPatrick McHardy 		struct ifmap k_map;
9220157f60cSPatrick McHardy 
923d314774cSStephen Hemminger 		if (!ops->ndo_set_config) {
9240157f60cSPatrick McHardy 			err = -EOPNOTSUPP;
9250157f60cSPatrick McHardy 			goto errout;
9260157f60cSPatrick McHardy 		}
9270157f60cSPatrick McHardy 
9280157f60cSPatrick McHardy 		if (!netif_device_present(dev)) {
9290157f60cSPatrick McHardy 			err = -ENODEV;
9300157f60cSPatrick McHardy 			goto errout;
9310157f60cSPatrick McHardy 		}
9320157f60cSPatrick McHardy 
9330157f60cSPatrick McHardy 		u_map = nla_data(tb[IFLA_MAP]);
9340157f60cSPatrick McHardy 		k_map.mem_start = (unsigned long) u_map->mem_start;
9350157f60cSPatrick McHardy 		k_map.mem_end = (unsigned long) u_map->mem_end;
9360157f60cSPatrick McHardy 		k_map.base_addr = (unsigned short) u_map->base_addr;
9370157f60cSPatrick McHardy 		k_map.irq = (unsigned char) u_map->irq;
9380157f60cSPatrick McHardy 		k_map.dma = (unsigned char) u_map->dma;
9390157f60cSPatrick McHardy 		k_map.port = (unsigned char) u_map->port;
9400157f60cSPatrick McHardy 
941d314774cSStephen Hemminger 		err = ops->ndo_set_config(dev, &k_map);
9420157f60cSPatrick McHardy 		if (err < 0)
9430157f60cSPatrick McHardy 			goto errout;
9440157f60cSPatrick McHardy 
9450157f60cSPatrick McHardy 		modified = 1;
9460157f60cSPatrick McHardy 	}
9470157f60cSPatrick McHardy 
9480157f60cSPatrick McHardy 	if (tb[IFLA_ADDRESS]) {
9490157f60cSPatrick McHardy 		struct sockaddr *sa;
9500157f60cSPatrick McHardy 		int len;
9510157f60cSPatrick McHardy 
952d314774cSStephen Hemminger 		if (!ops->ndo_set_mac_address) {
9530157f60cSPatrick McHardy 			err = -EOPNOTSUPP;
9540157f60cSPatrick McHardy 			goto errout;
9550157f60cSPatrick McHardy 		}
9560157f60cSPatrick McHardy 
9570157f60cSPatrick McHardy 		if (!netif_device_present(dev)) {
9580157f60cSPatrick McHardy 			err = -ENODEV;
9590157f60cSPatrick McHardy 			goto errout;
9600157f60cSPatrick McHardy 		}
9610157f60cSPatrick McHardy 
9620157f60cSPatrick McHardy 		len = sizeof(sa_family_t) + dev->addr_len;
9630157f60cSPatrick McHardy 		sa = kmalloc(len, GFP_KERNEL);
9640157f60cSPatrick McHardy 		if (!sa) {
9650157f60cSPatrick McHardy 			err = -ENOMEM;
9660157f60cSPatrick McHardy 			goto errout;
9670157f60cSPatrick McHardy 		}
9680157f60cSPatrick McHardy 		sa->sa_family = dev->type;
9690157f60cSPatrick McHardy 		memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
9700157f60cSPatrick McHardy 		       dev->addr_len);
971d314774cSStephen Hemminger 		err = ops->ndo_set_mac_address(dev, sa);
9720157f60cSPatrick McHardy 		kfree(sa);
9730157f60cSPatrick McHardy 		if (err)
9740157f60cSPatrick McHardy 			goto errout;
9750157f60cSPatrick McHardy 		send_addr_notify = 1;
9760157f60cSPatrick McHardy 		modified = 1;
9770157f60cSPatrick McHardy 	}
9780157f60cSPatrick McHardy 
9790157f60cSPatrick McHardy 	if (tb[IFLA_MTU]) {
9800157f60cSPatrick McHardy 		err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
9810157f60cSPatrick McHardy 		if (err < 0)
9820157f60cSPatrick McHardy 			goto errout;
9830157f60cSPatrick McHardy 		modified = 1;
9840157f60cSPatrick McHardy 	}
9850157f60cSPatrick McHardy 
9860157f60cSPatrick McHardy 	/*
9870157f60cSPatrick McHardy 	 * Interface selected by interface index but interface
9880157f60cSPatrick McHardy 	 * name provided implies that a name change has been
9890157f60cSPatrick McHardy 	 * requested.
9900157f60cSPatrick McHardy 	 */
9910157f60cSPatrick McHardy 	if (ifm->ifi_index > 0 && ifname[0]) {
9920157f60cSPatrick McHardy 		err = dev_change_name(dev, ifname);
9930157f60cSPatrick McHardy 		if (err < 0)
9940157f60cSPatrick McHardy 			goto errout;
9950157f60cSPatrick McHardy 		modified = 1;
9960157f60cSPatrick McHardy 	}
9970157f60cSPatrick McHardy 
9980b815a1aSStephen Hemminger 	if (tb[IFLA_IFALIAS]) {
9990b815a1aSStephen Hemminger 		err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
10000b815a1aSStephen Hemminger 				    nla_len(tb[IFLA_IFALIAS]));
10010b815a1aSStephen Hemminger 		if (err < 0)
10020b815a1aSStephen Hemminger 			goto errout;
10030b815a1aSStephen Hemminger 		modified = 1;
10040b815a1aSStephen Hemminger 	}
10050b815a1aSStephen Hemminger 
10060157f60cSPatrick McHardy 	if (tb[IFLA_BROADCAST]) {
10070157f60cSPatrick McHardy 		nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
10080157f60cSPatrick McHardy 		send_addr_notify = 1;
10090157f60cSPatrick McHardy 	}
10100157f60cSPatrick McHardy 
10110157f60cSPatrick McHardy 	if (ifm->ifi_flags || ifm->ifi_change) {
10123729d502SPatrick McHardy 		err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
10135f9021cfSJohannes Berg 		if (err < 0)
10145f9021cfSJohannes Berg 			goto errout;
10150157f60cSPatrick McHardy 	}
10160157f60cSPatrick McHardy 
101793b2d4a2SDavid S. Miller 	if (tb[IFLA_TXQLEN])
10180157f60cSPatrick McHardy 		dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
10190157f60cSPatrick McHardy 
10200157f60cSPatrick McHardy 	if (tb[IFLA_OPERSTATE])
102193b2d4a2SDavid S. Miller 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
10220157f60cSPatrick McHardy 
10230157f60cSPatrick McHardy 	if (tb[IFLA_LINKMODE]) {
10240157f60cSPatrick McHardy 		write_lock_bh(&dev_base_lock);
10250157f60cSPatrick McHardy 		dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
102693b2d4a2SDavid S. Miller 		write_unlock_bh(&dev_base_lock);
10270157f60cSPatrick McHardy 	}
10280157f60cSPatrick McHardy 
1029*c02db8c6SChris Wright 	if (tb[IFLA_VFINFO_LIST]) {
1030*c02db8c6SChris Wright 		struct nlattr *attr;
1031*c02db8c6SChris Wright 		int rem;
1032*c02db8c6SChris Wright 		nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
1033*c02db8c6SChris Wright 			if (nla_type(attr) != IFLA_VF_INFO)
1034*c02db8c6SChris Wright 				goto errout;
1035*c02db8c6SChris Wright 			err = do_setvfinfo(dev, attr);
1036ebc08a6fSWilliams, Mitch A 			if (err < 0)
1037ebc08a6fSWilliams, Mitch A 				goto errout;
1038ebc08a6fSWilliams, Mitch A 			modified = 1;
1039ebc08a6fSWilliams, Mitch A 		}
1040ebc08a6fSWilliams, Mitch A 	}
10410157f60cSPatrick McHardy 	err = 0;
10420157f60cSPatrick McHardy 
10430157f60cSPatrick McHardy errout:
10440157f60cSPatrick McHardy 	if (err < 0 && modified && net_ratelimit())
10450157f60cSPatrick McHardy 		printk(KERN_WARNING "A link change request failed with "
10460157f60cSPatrick McHardy 		       "some changes comitted already. Interface %s may "
10470157f60cSPatrick McHardy 		       "have been left with an inconsistent configuration, "
10480157f60cSPatrick McHardy 		       "please check.\n", dev->name);
10490157f60cSPatrick McHardy 
10500157f60cSPatrick McHardy 	if (send_addr_notify)
10510157f60cSPatrick McHardy 		call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
10520157f60cSPatrick McHardy 	return err;
10530157f60cSPatrick McHardy }
10540157f60cSPatrick McHardy 
1055da5e0494SThomas Graf static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1056da5e0494SThomas Graf {
10573b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
1058da5e0494SThomas Graf 	struct ifinfomsg *ifm;
1059da5e0494SThomas Graf 	struct net_device *dev;
10600157f60cSPatrick McHardy 	int err;
1061da5e0494SThomas Graf 	struct nlattr *tb[IFLA_MAX+1];
10621da177e4SLinus Torvalds 	char ifname[IFNAMSIZ];
10631da177e4SLinus Torvalds 
1064da5e0494SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1065da5e0494SThomas Graf 	if (err < 0)
1066da5e0494SThomas Graf 		goto errout;
10671da177e4SLinus Torvalds 
10685176f91eSThomas Graf 	if (tb[IFLA_IFNAME])
10695176f91eSThomas Graf 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
107078e5b891SPatrick McHardy 	else
107178e5b891SPatrick McHardy 		ifname[0] = '\0';
10721da177e4SLinus Torvalds 
10731da177e4SLinus Torvalds 	err = -EINVAL;
1074da5e0494SThomas Graf 	ifm = nlmsg_data(nlh);
107551055be8SPatrick McHardy 	if (ifm->ifi_index > 0)
1076a3d12891SEric Dumazet 		dev = __dev_get_by_index(net, ifm->ifi_index);
1077da5e0494SThomas Graf 	else if (tb[IFLA_IFNAME])
1078a3d12891SEric Dumazet 		dev = __dev_get_by_name(net, ifname);
1079da5e0494SThomas Graf 	else
1080da5e0494SThomas Graf 		goto errout;
10811da177e4SLinus Torvalds 
1082da5e0494SThomas Graf 	if (dev == NULL) {
1083da5e0494SThomas Graf 		err = -ENODEV;
1084da5e0494SThomas Graf 		goto errout;
1085da5e0494SThomas Graf 	}
10861da177e4SLinus Torvalds 
1087e0d087afSEric Dumazet 	err = validate_linkmsg(dev, tb);
1088e0d087afSEric Dumazet 	if (err < 0)
1089a3d12891SEric Dumazet 		goto errout;
1090da5e0494SThomas Graf 
109138f7b870SPatrick McHardy 	err = do_setlink(dev, ifm, tb, ifname, 0);
1092da5e0494SThomas Graf errout:
10931da177e4SLinus Torvalds 	return err;
10941da177e4SLinus Torvalds }
10951da177e4SLinus Torvalds 
109638f7b870SPatrick McHardy static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
109738f7b870SPatrick McHardy {
10983b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
109938f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops;
110038f7b870SPatrick McHardy 	struct net_device *dev;
110138f7b870SPatrick McHardy 	struct ifinfomsg *ifm;
110238f7b870SPatrick McHardy 	char ifname[IFNAMSIZ];
110338f7b870SPatrick McHardy 	struct nlattr *tb[IFLA_MAX+1];
110438f7b870SPatrick McHardy 	int err;
110538f7b870SPatrick McHardy 
110638f7b870SPatrick McHardy 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
110738f7b870SPatrick McHardy 	if (err < 0)
110838f7b870SPatrick McHardy 		return err;
110938f7b870SPatrick McHardy 
111038f7b870SPatrick McHardy 	if (tb[IFLA_IFNAME])
111138f7b870SPatrick McHardy 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
111238f7b870SPatrick McHardy 
111338f7b870SPatrick McHardy 	ifm = nlmsg_data(nlh);
111438f7b870SPatrick McHardy 	if (ifm->ifi_index > 0)
1115881d966bSEric W. Biederman 		dev = __dev_get_by_index(net, ifm->ifi_index);
111638f7b870SPatrick McHardy 	else if (tb[IFLA_IFNAME])
1117881d966bSEric W. Biederman 		dev = __dev_get_by_name(net, ifname);
111838f7b870SPatrick McHardy 	else
111938f7b870SPatrick McHardy 		return -EINVAL;
112038f7b870SPatrick McHardy 
112138f7b870SPatrick McHardy 	if (!dev)
112238f7b870SPatrick McHardy 		return -ENODEV;
112338f7b870SPatrick McHardy 
112438f7b870SPatrick McHardy 	ops = dev->rtnl_link_ops;
112538f7b870SPatrick McHardy 	if (!ops)
112638f7b870SPatrick McHardy 		return -EOPNOTSUPP;
112738f7b870SPatrick McHardy 
112823289a37SEric Dumazet 	ops->dellink(dev, NULL);
112938f7b870SPatrick McHardy 	return 0;
113038f7b870SPatrick McHardy }
113138f7b870SPatrick McHardy 
11323729d502SPatrick McHardy int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
11333729d502SPatrick McHardy {
11343729d502SPatrick McHardy 	unsigned int old_flags;
11353729d502SPatrick McHardy 	int err;
11363729d502SPatrick McHardy 
11373729d502SPatrick McHardy 	old_flags = dev->flags;
11383729d502SPatrick McHardy 	if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
11393729d502SPatrick McHardy 		err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
11403729d502SPatrick McHardy 		if (err < 0)
11413729d502SPatrick McHardy 			return err;
11423729d502SPatrick McHardy 	}
11433729d502SPatrick McHardy 
11443729d502SPatrick McHardy 	dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
11453729d502SPatrick McHardy 	rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
11463729d502SPatrick McHardy 
11473729d502SPatrick McHardy 	__dev_notify_flags(dev, old_flags);
11483729d502SPatrick McHardy 	return 0;
11493729d502SPatrick McHardy }
11503729d502SPatrick McHardy EXPORT_SYMBOL(rtnl_configure_link);
11513729d502SPatrick McHardy 
115281adee47SEric W. Biederman struct net_device *rtnl_create_link(struct net *src_net, struct net *net,
115381adee47SEric W. Biederman 	char *ifname, const struct rtnl_link_ops *ops, struct nlattr *tb[])
1154e7199288SPavel Emelianov {
1155e7199288SPavel Emelianov 	int err;
1156e7199288SPavel Emelianov 	struct net_device *dev;
11572e59af3dSEric Dumazet 	unsigned int num_queues = 1;
11582e59af3dSEric Dumazet 	unsigned int real_num_queues = 1;
1159e7199288SPavel Emelianov 
11602e59af3dSEric Dumazet 	if (ops->get_tx_queues) {
116181adee47SEric W. Biederman 		err = ops->get_tx_queues(src_net, tb, &num_queues,
1162e0d087afSEric Dumazet 					 &real_num_queues);
11632e59af3dSEric Dumazet 		if (err)
11642e59af3dSEric Dumazet 			goto err;
11652e59af3dSEric Dumazet 	}
1166e7199288SPavel Emelianov 	err = -ENOMEM;
11672e59af3dSEric Dumazet 	dev = alloc_netdev_mq(ops->priv_size, ifname, ops->setup, num_queues);
1168e7199288SPavel Emelianov 	if (!dev)
1169e7199288SPavel Emelianov 		goto err;
1170e7199288SPavel Emelianov 
117181adee47SEric W. Biederman 	dev_net_set(dev, net);
117281adee47SEric W. Biederman 	dev->rtnl_link_ops = ops;
11733729d502SPatrick McHardy 	dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
11742e59af3dSEric Dumazet 	dev->real_num_tx_queues = real_num_queues;
117581adee47SEric W. Biederman 
1176e7199288SPavel Emelianov 	if (strchr(dev->name, '%')) {
1177e7199288SPavel Emelianov 		err = dev_alloc_name(dev, dev->name);
1178e7199288SPavel Emelianov 		if (err < 0)
1179e7199288SPavel Emelianov 			goto err_free;
1180e7199288SPavel Emelianov 	}
1181e7199288SPavel Emelianov 
1182e7199288SPavel Emelianov 	if (tb[IFLA_MTU])
1183e7199288SPavel Emelianov 		dev->mtu = nla_get_u32(tb[IFLA_MTU]);
1184e7199288SPavel Emelianov 	if (tb[IFLA_ADDRESS])
1185e7199288SPavel Emelianov 		memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
1186e7199288SPavel Emelianov 				nla_len(tb[IFLA_ADDRESS]));
1187e7199288SPavel Emelianov 	if (tb[IFLA_BROADCAST])
1188e7199288SPavel Emelianov 		memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
1189e7199288SPavel Emelianov 				nla_len(tb[IFLA_BROADCAST]));
1190e7199288SPavel Emelianov 	if (tb[IFLA_TXQLEN])
1191e7199288SPavel Emelianov 		dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
1192e7199288SPavel Emelianov 	if (tb[IFLA_OPERSTATE])
119393b2d4a2SDavid S. Miller 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
1194e7199288SPavel Emelianov 	if (tb[IFLA_LINKMODE])
1195e7199288SPavel Emelianov 		dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
1196e7199288SPavel Emelianov 
1197e7199288SPavel Emelianov 	return dev;
1198e7199288SPavel Emelianov 
1199e7199288SPavel Emelianov err_free:
1200e7199288SPavel Emelianov 	free_netdev(dev);
1201e7199288SPavel Emelianov err:
1202e7199288SPavel Emelianov 	return ERR_PTR(err);
1203e7199288SPavel Emelianov }
1204e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_create_link);
1205e7199288SPavel Emelianov 
120638f7b870SPatrick McHardy static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
120738f7b870SPatrick McHardy {
12083b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
120938f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops;
121038f7b870SPatrick McHardy 	struct net_device *dev;
121138f7b870SPatrick McHardy 	struct ifinfomsg *ifm;
121238f7b870SPatrick McHardy 	char kind[MODULE_NAME_LEN];
121338f7b870SPatrick McHardy 	char ifname[IFNAMSIZ];
121438f7b870SPatrick McHardy 	struct nlattr *tb[IFLA_MAX+1];
121538f7b870SPatrick McHardy 	struct nlattr *linkinfo[IFLA_INFO_MAX+1];
121638f7b870SPatrick McHardy 	int err;
121738f7b870SPatrick McHardy 
121895a5afcaSJohannes Berg #ifdef CONFIG_MODULES
121938f7b870SPatrick McHardy replay:
12208072f085SThomas Graf #endif
122138f7b870SPatrick McHardy 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
122238f7b870SPatrick McHardy 	if (err < 0)
122338f7b870SPatrick McHardy 		return err;
122438f7b870SPatrick McHardy 
122538f7b870SPatrick McHardy 	if (tb[IFLA_IFNAME])
122638f7b870SPatrick McHardy 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
122738f7b870SPatrick McHardy 	else
122838f7b870SPatrick McHardy 		ifname[0] = '\0';
122938f7b870SPatrick McHardy 
123038f7b870SPatrick McHardy 	ifm = nlmsg_data(nlh);
123138f7b870SPatrick McHardy 	if (ifm->ifi_index > 0)
1232881d966bSEric W. Biederman 		dev = __dev_get_by_index(net, ifm->ifi_index);
123338f7b870SPatrick McHardy 	else if (ifname[0])
1234881d966bSEric W. Biederman 		dev = __dev_get_by_name(net, ifname);
123538f7b870SPatrick McHardy 	else
123638f7b870SPatrick McHardy 		dev = NULL;
123738f7b870SPatrick McHardy 
1238e0d087afSEric Dumazet 	err = validate_linkmsg(dev, tb);
1239e0d087afSEric Dumazet 	if (err < 0)
12401840bb13SThomas Graf 		return err;
12411840bb13SThomas Graf 
124238f7b870SPatrick McHardy 	if (tb[IFLA_LINKINFO]) {
124338f7b870SPatrick McHardy 		err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
124438f7b870SPatrick McHardy 				       tb[IFLA_LINKINFO], ifla_info_policy);
124538f7b870SPatrick McHardy 		if (err < 0)
124638f7b870SPatrick McHardy 			return err;
124738f7b870SPatrick McHardy 	} else
124838f7b870SPatrick McHardy 		memset(linkinfo, 0, sizeof(linkinfo));
124938f7b870SPatrick McHardy 
125038f7b870SPatrick McHardy 	if (linkinfo[IFLA_INFO_KIND]) {
125138f7b870SPatrick McHardy 		nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
125238f7b870SPatrick McHardy 		ops = rtnl_link_ops_get(kind);
125338f7b870SPatrick McHardy 	} else {
125438f7b870SPatrick McHardy 		kind[0] = '\0';
125538f7b870SPatrick McHardy 		ops = NULL;
125638f7b870SPatrick McHardy 	}
125738f7b870SPatrick McHardy 
125838f7b870SPatrick McHardy 	if (1) {
125938f7b870SPatrick McHardy 		struct nlattr *attr[ops ? ops->maxtype + 1 : 0], **data = NULL;
126081adee47SEric W. Biederman 		struct net *dest_net;
126138f7b870SPatrick McHardy 
126238f7b870SPatrick McHardy 		if (ops) {
126338f7b870SPatrick McHardy 			if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
126438f7b870SPatrick McHardy 				err = nla_parse_nested(attr, ops->maxtype,
126538f7b870SPatrick McHardy 						       linkinfo[IFLA_INFO_DATA],
126638f7b870SPatrick McHardy 						       ops->policy);
126738f7b870SPatrick McHardy 				if (err < 0)
126838f7b870SPatrick McHardy 					return err;
126938f7b870SPatrick McHardy 				data = attr;
127038f7b870SPatrick McHardy 			}
127138f7b870SPatrick McHardy 			if (ops->validate) {
127238f7b870SPatrick McHardy 				err = ops->validate(tb, data);
127338f7b870SPatrick McHardy 				if (err < 0)
127438f7b870SPatrick McHardy 					return err;
127538f7b870SPatrick McHardy 			}
127638f7b870SPatrick McHardy 		}
127738f7b870SPatrick McHardy 
127838f7b870SPatrick McHardy 		if (dev) {
127938f7b870SPatrick McHardy 			int modified = 0;
128038f7b870SPatrick McHardy 
128138f7b870SPatrick McHardy 			if (nlh->nlmsg_flags & NLM_F_EXCL)
128238f7b870SPatrick McHardy 				return -EEXIST;
128338f7b870SPatrick McHardy 			if (nlh->nlmsg_flags & NLM_F_REPLACE)
128438f7b870SPatrick McHardy 				return -EOPNOTSUPP;
128538f7b870SPatrick McHardy 
128638f7b870SPatrick McHardy 			if (linkinfo[IFLA_INFO_DATA]) {
128738f7b870SPatrick McHardy 				if (!ops || ops != dev->rtnl_link_ops ||
128838f7b870SPatrick McHardy 				    !ops->changelink)
128938f7b870SPatrick McHardy 					return -EOPNOTSUPP;
129038f7b870SPatrick McHardy 
129138f7b870SPatrick McHardy 				err = ops->changelink(dev, tb, data);
129238f7b870SPatrick McHardy 				if (err < 0)
129338f7b870SPatrick McHardy 					return err;
129438f7b870SPatrick McHardy 				modified = 1;
129538f7b870SPatrick McHardy 			}
129638f7b870SPatrick McHardy 
129738f7b870SPatrick McHardy 			return do_setlink(dev, ifm, tb, ifname, modified);
129838f7b870SPatrick McHardy 		}
129938f7b870SPatrick McHardy 
130038f7b870SPatrick McHardy 		if (!(nlh->nlmsg_flags & NLM_F_CREATE))
130138f7b870SPatrick McHardy 			return -ENODEV;
130238f7b870SPatrick McHardy 
13033729d502SPatrick McHardy 		if (ifm->ifi_index)
130438f7b870SPatrick McHardy 			return -EOPNOTSUPP;
13050e06877cSPatrick McHardy 		if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
130638f7b870SPatrick McHardy 			return -EOPNOTSUPP;
130738f7b870SPatrick McHardy 
130838f7b870SPatrick McHardy 		if (!ops) {
130995a5afcaSJohannes Berg #ifdef CONFIG_MODULES
131038f7b870SPatrick McHardy 			if (kind[0]) {
131138f7b870SPatrick McHardy 				__rtnl_unlock();
131238f7b870SPatrick McHardy 				request_module("rtnl-link-%s", kind);
131338f7b870SPatrick McHardy 				rtnl_lock();
131438f7b870SPatrick McHardy 				ops = rtnl_link_ops_get(kind);
131538f7b870SPatrick McHardy 				if (ops)
131638f7b870SPatrick McHardy 					goto replay;
131738f7b870SPatrick McHardy 			}
131838f7b870SPatrick McHardy #endif
131938f7b870SPatrick McHardy 			return -EOPNOTSUPP;
132038f7b870SPatrick McHardy 		}
132138f7b870SPatrick McHardy 
132238f7b870SPatrick McHardy 		if (!ifname[0])
132338f7b870SPatrick McHardy 			snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
132438f7b870SPatrick McHardy 
132581adee47SEric W. Biederman 		dest_net = rtnl_link_get_net(net, tb);
132681adee47SEric W. Biederman 		dev = rtnl_create_link(net, dest_net, ifname, ops, tb);
132738f7b870SPatrick McHardy 
1328e7199288SPavel Emelianov 		if (IS_ERR(dev))
1329e7199288SPavel Emelianov 			err = PTR_ERR(dev);
1330e7199288SPavel Emelianov 		else if (ops->newlink)
133181adee47SEric W. Biederman 			err = ops->newlink(net, dev, tb, data);
13322d85cba2SPatrick McHardy 		else
13332d85cba2SPatrick McHardy 			err = register_netdevice(dev);
133480032cffSDan Carpenter 
133580032cffSDan Carpenter 		if (err < 0 && !IS_ERR(dev))
133638f7b870SPatrick McHardy 			free_netdev(dev);
133780032cffSDan Carpenter 		if (err < 0)
13383729d502SPatrick McHardy 			goto out;
133981adee47SEric W. Biederman 
13403729d502SPatrick McHardy 		err = rtnl_configure_link(dev, ifm);
13413729d502SPatrick McHardy 		if (err < 0)
13423729d502SPatrick McHardy 			unregister_netdevice(dev);
13433729d502SPatrick McHardy out:
134481adee47SEric W. Biederman 		put_net(dest_net);
134538f7b870SPatrick McHardy 		return err;
134638f7b870SPatrick McHardy 	}
134738f7b870SPatrick McHardy }
134838f7b870SPatrick McHardy 
1349b60c5115SThomas Graf static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
1350711e2c33SJean Tourrilhes {
13513b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
1352b60c5115SThomas Graf 	struct ifinfomsg *ifm;
1353a3d12891SEric Dumazet 	char ifname[IFNAMSIZ];
1354b60c5115SThomas Graf 	struct nlattr *tb[IFLA_MAX+1];
1355b60c5115SThomas Graf 	struct net_device *dev = NULL;
1356b60c5115SThomas Graf 	struct sk_buff *nskb;
1357339bf98fSThomas Graf 	int err;
1358711e2c33SJean Tourrilhes 
1359b60c5115SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1360b60c5115SThomas Graf 	if (err < 0)
13619918f230SEric Sesterhenn 		return err;
1362b60c5115SThomas Graf 
1363a3d12891SEric Dumazet 	if (tb[IFLA_IFNAME])
1364a3d12891SEric Dumazet 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1365a3d12891SEric Dumazet 
1366b60c5115SThomas Graf 	ifm = nlmsg_data(nlh);
1367a3d12891SEric Dumazet 	if (ifm->ifi_index > 0)
1368a3d12891SEric Dumazet 		dev = __dev_get_by_index(net, ifm->ifi_index);
1369a3d12891SEric Dumazet 	else if (tb[IFLA_IFNAME])
1370a3d12891SEric Dumazet 		dev = __dev_get_by_name(net, ifname);
1371a3d12891SEric Dumazet 	else
1372b60c5115SThomas Graf 		return -EINVAL;
1373b60c5115SThomas Graf 
1374a3d12891SEric Dumazet 	if (dev == NULL)
1375a3d12891SEric Dumazet 		return -ENODEV;
1376a3d12891SEric Dumazet 
137738f7b870SPatrick McHardy 	nskb = nlmsg_new(if_nlmsg_size(dev), GFP_KERNEL);
1378a3d12891SEric Dumazet 	if (nskb == NULL)
1379a3d12891SEric Dumazet 		return -ENOBUFS;
1380711e2c33SJean Tourrilhes 
1381575c3e2aSPatrick McHardy 	err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).pid,
1382575c3e2aSPatrick McHardy 			       nlh->nlmsg_seq, 0, 0);
138326932566SPatrick McHardy 	if (err < 0) {
138426932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in if_nlmsg_size */
138526932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
138626932566SPatrick McHardy 		kfree_skb(nskb);
1387a3d12891SEric Dumazet 	} else
138897c53cacSDenis V. Lunev 		err = rtnl_unicast(nskb, net, NETLINK_CB(skb).pid);
1389b60c5115SThomas Graf 
1390711e2c33SJean Tourrilhes 	return err;
1391711e2c33SJean Tourrilhes }
1392711e2c33SJean Tourrilhes 
139342bad1daSAdrian Bunk static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
13941da177e4SLinus Torvalds {
13951da177e4SLinus Torvalds 	int idx;
13961da177e4SLinus Torvalds 	int s_idx = cb->family;
13971da177e4SLinus Torvalds 
13981da177e4SLinus Torvalds 	if (s_idx == 0)
13991da177e4SLinus Torvalds 		s_idx = 1;
14001da177e4SLinus Torvalds 	for (idx = 1; idx < NPROTO; idx++) {
14011da177e4SLinus Torvalds 		int type = cb->nlh->nlmsg_type-RTM_BASE;
14021da177e4SLinus Torvalds 		if (idx < s_idx || idx == PF_PACKET)
14031da177e4SLinus Torvalds 			continue;
1404e2849863SThomas Graf 		if (rtnl_msg_handlers[idx] == NULL ||
1405e2849863SThomas Graf 		    rtnl_msg_handlers[idx][type].dumpit == NULL)
14061da177e4SLinus Torvalds 			continue;
14071da177e4SLinus Torvalds 		if (idx > s_idx)
14081da177e4SLinus Torvalds 			memset(&cb->args[0], 0, sizeof(cb->args));
1409e2849863SThomas Graf 		if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
14101da177e4SLinus Torvalds 			break;
14111da177e4SLinus Torvalds 	}
14121da177e4SLinus Torvalds 	cb->family = idx;
14131da177e4SLinus Torvalds 
14141da177e4SLinus Torvalds 	return skb->len;
14151da177e4SLinus Torvalds }
14161da177e4SLinus Torvalds 
14171da177e4SLinus Torvalds void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change)
14181da177e4SLinus Torvalds {
1419c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
14201da177e4SLinus Torvalds 	struct sk_buff *skb;
14210ec6d3f4SThomas Graf 	int err = -ENOBUFS;
14221da177e4SLinus Torvalds 
142338f7b870SPatrick McHardy 	skb = nlmsg_new(if_nlmsg_size(dev), GFP_KERNEL);
14240ec6d3f4SThomas Graf 	if (skb == NULL)
14250ec6d3f4SThomas Graf 		goto errout;
14261da177e4SLinus Torvalds 
1427575c3e2aSPatrick McHardy 	err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0);
142826932566SPatrick McHardy 	if (err < 0) {
142926932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in if_nlmsg_size() */
143026932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
143126932566SPatrick McHardy 		kfree_skb(skb);
143226932566SPatrick McHardy 		goto errout;
143326932566SPatrick McHardy 	}
14341ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
14351ce85fe4SPablo Neira Ayuso 	return;
14360ec6d3f4SThomas Graf errout:
14370ec6d3f4SThomas Graf 	if (err < 0)
14384b3da706SEric W. Biederman 		rtnl_set_sk_err(net, RTNLGRP_LINK, err);
14391da177e4SLinus Torvalds }
14401da177e4SLinus Torvalds 
14411da177e4SLinus Torvalds /* Protected by RTNL sempahore.  */
14421da177e4SLinus Torvalds static struct rtattr **rta_buf;
14431da177e4SLinus Torvalds static int rtattr_max;
14441da177e4SLinus Torvalds 
14451da177e4SLinus Torvalds /* Process one rtnetlink message. */
14461da177e4SLinus Torvalds 
14471d00a4ebSThomas Graf static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
14481da177e4SLinus Torvalds {
14493b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
1450e2849863SThomas Graf 	rtnl_doit_func doit;
14511da177e4SLinus Torvalds 	int sz_idx, kind;
14521da177e4SLinus Torvalds 	int min_len;
14531da177e4SLinus Torvalds 	int family;
14541da177e4SLinus Torvalds 	int type;
14551c2d670fSPatrick McHardy 	int err;
14561da177e4SLinus Torvalds 
14571da177e4SLinus Torvalds 	type = nlh->nlmsg_type;
14581da177e4SLinus Torvalds 	if (type > RTM_MAX)
1459038890feSThomas Graf 		return -EOPNOTSUPP;
14601da177e4SLinus Torvalds 
14611da177e4SLinus Torvalds 	type -= RTM_BASE;
14621da177e4SLinus Torvalds 
14631da177e4SLinus Torvalds 	/* All the messages must have at least 1 byte length */
14641da177e4SLinus Torvalds 	if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg)))
14651da177e4SLinus Torvalds 		return 0;
14661da177e4SLinus Torvalds 
14671da177e4SLinus Torvalds 	family = ((struct rtgenmsg *)NLMSG_DATA(nlh))->rtgen_family;
14681d00a4ebSThomas Graf 	if (family >= NPROTO)
14691d00a4ebSThomas Graf 		return -EAFNOSUPPORT;
14701da177e4SLinus Torvalds 
14711da177e4SLinus Torvalds 	sz_idx = type>>2;
14721da177e4SLinus Torvalds 	kind = type&3;
14731da177e4SLinus Torvalds 
14741d00a4ebSThomas Graf 	if (kind != 2 && security_netlink_recv(skb, CAP_NET_ADMIN))
14751d00a4ebSThomas Graf 		return -EPERM;
14761da177e4SLinus Torvalds 
14771da177e4SLinus Torvalds 	if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
147897c53cacSDenis V. Lunev 		struct sock *rtnl;
1479e2849863SThomas Graf 		rtnl_dumpit_func dumpit;
14801da177e4SLinus Torvalds 
1481e2849863SThomas Graf 		dumpit = rtnl_get_dumpit(family, type);
1482e2849863SThomas Graf 		if (dumpit == NULL)
1483038890feSThomas Graf 			return -EOPNOTSUPP;
14841da177e4SLinus Torvalds 
14851c2d670fSPatrick McHardy 		__rtnl_unlock();
148697c53cacSDenis V. Lunev 		rtnl = net->rtnl;
14871c2d670fSPatrick McHardy 		err = netlink_dump_start(rtnl, skb, nlh, dumpit, NULL);
14881c2d670fSPatrick McHardy 		rtnl_lock();
14891c2d670fSPatrick McHardy 		return err;
14901da177e4SLinus Torvalds 	}
14911da177e4SLinus Torvalds 
14921da177e4SLinus Torvalds 	memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *)));
14931da177e4SLinus Torvalds 
14941da177e4SLinus Torvalds 	min_len = rtm_min[sz_idx];
14951da177e4SLinus Torvalds 	if (nlh->nlmsg_len < min_len)
14961d00a4ebSThomas Graf 		return -EINVAL;
14971da177e4SLinus Torvalds 
14981da177e4SLinus Torvalds 	if (nlh->nlmsg_len > min_len) {
14991da177e4SLinus Torvalds 		int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
15001da177e4SLinus Torvalds 		struct rtattr *attr = (void *)nlh + NLMSG_ALIGN(min_len);
15011da177e4SLinus Torvalds 
15021da177e4SLinus Torvalds 		while (RTA_OK(attr, attrlen)) {
15031da177e4SLinus Torvalds 			unsigned flavor = attr->rta_type;
15041da177e4SLinus Torvalds 			if (flavor) {
15051da177e4SLinus Torvalds 				if (flavor > rta_max[sz_idx])
15061d00a4ebSThomas Graf 					return -EINVAL;
15071da177e4SLinus Torvalds 				rta_buf[flavor-1] = attr;
15081da177e4SLinus Torvalds 			}
15091da177e4SLinus Torvalds 			attr = RTA_NEXT(attr, attrlen);
15101da177e4SLinus Torvalds 		}
15111da177e4SLinus Torvalds 	}
15121da177e4SLinus Torvalds 
1513e2849863SThomas Graf 	doit = rtnl_get_doit(family, type);
1514e2849863SThomas Graf 	if (doit == NULL)
1515038890feSThomas Graf 		return -EOPNOTSUPP;
15161da177e4SLinus Torvalds 
15171d00a4ebSThomas Graf 	return doit(skb, nlh, (void *)&rta_buf[0]);
15181da177e4SLinus Torvalds }
15191da177e4SLinus Torvalds 
1520cd40b7d3SDenis V. Lunev static void rtnetlink_rcv(struct sk_buff *skb)
15211da177e4SLinus Torvalds {
15221536cc0dSDenis V. Lunev 	rtnl_lock();
1523cd40b7d3SDenis V. Lunev 	netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
15241536cc0dSDenis V. Lunev 	rtnl_unlock();
15251da177e4SLinus Torvalds }
15261da177e4SLinus Torvalds 
15271da177e4SLinus Torvalds static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
15281da177e4SLinus Torvalds {
15291da177e4SLinus Torvalds 	struct net_device *dev = ptr;
1530e9dc8653SEric W. Biederman 
15311da177e4SLinus Torvalds 	switch (event) {
15321da177e4SLinus Torvalds 	case NETDEV_UP:
15331da177e4SLinus Torvalds 	case NETDEV_DOWN:
153410de05afSPatrick McHardy 	case NETDEV_PRE_UP:
1535d90a909eSEric W. Biederman 	case NETDEV_POST_INIT:
1536d90a909eSEric W. Biederman 	case NETDEV_REGISTER:
15371da177e4SLinus Torvalds 	case NETDEV_CHANGE:
15381da177e4SLinus Torvalds 	case NETDEV_GOING_DOWN:
1539a2835763SPatrick McHardy 	case NETDEV_UNREGISTER:
1540d90a909eSEric W. Biederman 	case NETDEV_UNREGISTER_BATCH:
15411da177e4SLinus Torvalds 		break;
15421da177e4SLinus Torvalds 	default:
15431da177e4SLinus Torvalds 		rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
15441da177e4SLinus Torvalds 		break;
15451da177e4SLinus Torvalds 	}
15461da177e4SLinus Torvalds 	return NOTIFY_DONE;
15471da177e4SLinus Torvalds }
15481da177e4SLinus Torvalds 
15491da177e4SLinus Torvalds static struct notifier_block rtnetlink_dev_notifier = {
15501da177e4SLinus Torvalds 	.notifier_call	= rtnetlink_event,
15511da177e4SLinus Torvalds };
15521da177e4SLinus Torvalds 
155397c53cacSDenis V. Lunev 
15542c8c1e72SAlexey Dobriyan static int __net_init rtnetlink_net_init(struct net *net)
155597c53cacSDenis V. Lunev {
155697c53cacSDenis V. Lunev 	struct sock *sk;
155797c53cacSDenis V. Lunev 	sk = netlink_kernel_create(net, NETLINK_ROUTE, RTNLGRP_MAX,
155897c53cacSDenis V. Lunev 				   rtnetlink_rcv, &rtnl_mutex, THIS_MODULE);
155997c53cacSDenis V. Lunev 	if (!sk)
156097c53cacSDenis V. Lunev 		return -ENOMEM;
156197c53cacSDenis V. Lunev 	net->rtnl = sk;
156297c53cacSDenis V. Lunev 	return 0;
156397c53cacSDenis V. Lunev }
156497c53cacSDenis V. Lunev 
15652c8c1e72SAlexey Dobriyan static void __net_exit rtnetlink_net_exit(struct net *net)
156697c53cacSDenis V. Lunev {
1567b7c6ba6eSDenis V. Lunev 	netlink_kernel_release(net->rtnl);
156897c53cacSDenis V. Lunev 	net->rtnl = NULL;
156997c53cacSDenis V. Lunev }
157097c53cacSDenis V. Lunev 
157197c53cacSDenis V. Lunev static struct pernet_operations rtnetlink_net_ops = {
157297c53cacSDenis V. Lunev 	.init = rtnetlink_net_init,
157397c53cacSDenis V. Lunev 	.exit = rtnetlink_net_exit,
157497c53cacSDenis V. Lunev };
157597c53cacSDenis V. Lunev 
15761da177e4SLinus Torvalds void __init rtnetlink_init(void)
15771da177e4SLinus Torvalds {
15781da177e4SLinus Torvalds 	int i;
15791da177e4SLinus Torvalds 
15801da177e4SLinus Torvalds 	rtattr_max = 0;
15811da177e4SLinus Torvalds 	for (i = 0; i < ARRAY_SIZE(rta_max); i++)
15821da177e4SLinus Torvalds 		if (rta_max[i] > rtattr_max)
15831da177e4SLinus Torvalds 			rtattr_max = rta_max[i];
15841da177e4SLinus Torvalds 	rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL);
15851da177e4SLinus Torvalds 	if (!rta_buf)
15861da177e4SLinus Torvalds 		panic("rtnetlink_init: cannot allocate rta_buf\n");
15871da177e4SLinus Torvalds 
158897c53cacSDenis V. Lunev 	if (register_pernet_subsys(&rtnetlink_net_ops))
15891da177e4SLinus Torvalds 		panic("rtnetlink_init: cannot initialize rtnetlink\n");
159097c53cacSDenis V. Lunev 
15911da177e4SLinus Torvalds 	netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV);
15921da177e4SLinus Torvalds 	register_netdevice_notifier(&rtnetlink_dev_notifier);
1593340d17fcSThomas Graf 
1594340d17fcSThomas Graf 	rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, rtnl_dump_ifinfo);
1595340d17fcSThomas Graf 	rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL);
159638f7b870SPatrick McHardy 	rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL);
159738f7b870SPatrick McHardy 	rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL);
1598687ad8ccSThomas Graf 
1599687ad8ccSThomas Graf 	rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all);
1600687ad8ccSThomas Graf 	rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all);
16011da177e4SLinus Torvalds }
16021da177e4SLinus Torvalds 
1603