xref: /openbmc/linux/net/core/rtnetlink.c (revision 93b2d4a208eeb19bff95e98c34503ce4b03cd0f9)
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>
38d8a5ec67SEric W. Biederman #include <linux/nsproxy.h>
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds #include <asm/uaccess.h>
411da177e4SLinus Torvalds #include <asm/system.h>
421da177e4SLinus Torvalds #include <asm/string.h>
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds #include <linux/inet.h>
451da177e4SLinus Torvalds #include <linux/netdevice.h>
461da177e4SLinus Torvalds #include <net/ip.h>
471da177e4SLinus Torvalds #include <net/protocol.h>
481da177e4SLinus Torvalds #include <net/arp.h>
491da177e4SLinus Torvalds #include <net/route.h>
501da177e4SLinus Torvalds #include <net/udp.h>
511da177e4SLinus Torvalds #include <net/sock.h>
521da177e4SLinus Torvalds #include <net/pkt_sched.h>
5314c0b97dSThomas Graf #include <net/fib_rules.h>
54e2849863SThomas Graf #include <net/rtnetlink.h>
551da177e4SLinus Torvalds 
56e2849863SThomas Graf struct rtnl_link
57e2849863SThomas Graf {
58e2849863SThomas Graf 	rtnl_doit_func		doit;
59e2849863SThomas Graf 	rtnl_dumpit_func	dumpit;
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 }
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 {
766756ae4bSStephen Hemminger 	mutex_unlock(&rtnl_mutex);
771da177e4SLinus Torvalds 	netdev_run_todo();
781da177e4SLinus Torvalds }
791da177e4SLinus Torvalds 
806756ae4bSStephen Hemminger int rtnl_trylock(void)
816756ae4bSStephen Hemminger {
826756ae4bSStephen Hemminger 	return mutex_trylock(&rtnl_mutex);
836756ae4bSStephen Hemminger }
846756ae4bSStephen Hemminger 
8542bad1daSAdrian Bunk static struct rtnl_link *rtnl_msg_handlers[NPROTO];
86e2849863SThomas Graf 
87e2849863SThomas Graf static inline int rtm_msgindex(int msgtype)
88e2849863SThomas Graf {
89e2849863SThomas Graf 	int msgindex = msgtype - RTM_BASE;
90e2849863SThomas Graf 
91e2849863SThomas Graf 	/*
92e2849863SThomas Graf 	 * msgindex < 0 implies someone tried to register a netlink
93e2849863SThomas Graf 	 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
94e2849863SThomas Graf 	 * the message type has not been added to linux/rtnetlink.h
95e2849863SThomas Graf 	 */
96e2849863SThomas Graf 	BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
97e2849863SThomas Graf 
98e2849863SThomas Graf 	return msgindex;
99e2849863SThomas Graf }
100e2849863SThomas Graf 
101e2849863SThomas Graf static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex)
102e2849863SThomas Graf {
103e2849863SThomas Graf 	struct rtnl_link *tab;
104e2849863SThomas Graf 
105e2849863SThomas Graf 	tab = rtnl_msg_handlers[protocol];
10651057f2fSThomas Graf 	if (tab == NULL || tab[msgindex].doit == NULL)
107e2849863SThomas Graf 		tab = rtnl_msg_handlers[PF_UNSPEC];
108e2849863SThomas Graf 
10951057f2fSThomas Graf 	return tab ? tab[msgindex].doit : NULL;
110e2849863SThomas Graf }
111e2849863SThomas Graf 
112e2849863SThomas Graf static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex)
113e2849863SThomas Graf {
114e2849863SThomas Graf 	struct rtnl_link *tab;
115e2849863SThomas Graf 
116e2849863SThomas Graf 	tab = rtnl_msg_handlers[protocol];
11751057f2fSThomas Graf 	if (tab == NULL || tab[msgindex].dumpit == NULL)
118e2849863SThomas Graf 		tab = rtnl_msg_handlers[PF_UNSPEC];
119e2849863SThomas Graf 
12051057f2fSThomas Graf 	return tab ? tab[msgindex].dumpit : NULL;
121e2849863SThomas Graf }
122e2849863SThomas Graf 
123e2849863SThomas Graf /**
124e2849863SThomas Graf  * __rtnl_register - Register a rtnetlink message type
125e2849863SThomas Graf  * @protocol: Protocol family or PF_UNSPEC
126e2849863SThomas Graf  * @msgtype: rtnetlink message type
127e2849863SThomas Graf  * @doit: Function pointer called for each request message
128e2849863SThomas Graf  * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
129e2849863SThomas Graf  *
130e2849863SThomas Graf  * Registers the specified function pointers (at least one of them has
131e2849863SThomas Graf  * to be non-NULL) to be called whenever a request message for the
132e2849863SThomas Graf  * specified protocol family and message type is received.
133e2849863SThomas Graf  *
134e2849863SThomas Graf  * The special protocol family PF_UNSPEC may be used to define fallback
135e2849863SThomas Graf  * function pointers for the case when no entry for the specific protocol
136e2849863SThomas Graf  * family exists.
137e2849863SThomas Graf  *
138e2849863SThomas Graf  * Returns 0 on success or a negative error code.
139e2849863SThomas Graf  */
140e2849863SThomas Graf int __rtnl_register(int protocol, int msgtype,
141e2849863SThomas Graf 		    rtnl_doit_func doit, rtnl_dumpit_func dumpit)
142e2849863SThomas Graf {
143e2849863SThomas Graf 	struct rtnl_link *tab;
144e2849863SThomas Graf 	int msgindex;
145e2849863SThomas Graf 
146e2849863SThomas Graf 	BUG_ON(protocol < 0 || protocol >= NPROTO);
147e2849863SThomas Graf 	msgindex = rtm_msgindex(msgtype);
148e2849863SThomas Graf 
149e2849863SThomas Graf 	tab = rtnl_msg_handlers[protocol];
150e2849863SThomas Graf 	if (tab == NULL) {
151e2849863SThomas Graf 		tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL);
152e2849863SThomas Graf 		if (tab == NULL)
153e2849863SThomas Graf 			return -ENOBUFS;
154e2849863SThomas Graf 
155e2849863SThomas Graf 		rtnl_msg_handlers[protocol] = tab;
156e2849863SThomas Graf 	}
157e2849863SThomas Graf 
158e2849863SThomas Graf 	if (doit)
159e2849863SThomas Graf 		tab[msgindex].doit = doit;
160e2849863SThomas Graf 
161e2849863SThomas Graf 	if (dumpit)
162e2849863SThomas Graf 		tab[msgindex].dumpit = dumpit;
163e2849863SThomas Graf 
164e2849863SThomas Graf 	return 0;
165e2849863SThomas Graf }
166e2849863SThomas Graf 
167e2849863SThomas Graf EXPORT_SYMBOL_GPL(__rtnl_register);
168e2849863SThomas Graf 
169e2849863SThomas Graf /**
170e2849863SThomas Graf  * rtnl_register - Register a rtnetlink message type
171e2849863SThomas Graf  *
172e2849863SThomas Graf  * Identical to __rtnl_register() but panics on failure. This is useful
173e2849863SThomas Graf  * as failure of this function is very unlikely, it can only happen due
174e2849863SThomas Graf  * to lack of memory when allocating the chain to store all message
175e2849863SThomas Graf  * handlers for a protocol. Meant for use in init functions where lack
176e2849863SThomas Graf  * of memory implies no sense in continueing.
177e2849863SThomas Graf  */
178e2849863SThomas Graf void rtnl_register(int protocol, int msgtype,
179e2849863SThomas Graf 		   rtnl_doit_func doit, rtnl_dumpit_func dumpit)
180e2849863SThomas Graf {
181e2849863SThomas Graf 	if (__rtnl_register(protocol, msgtype, doit, dumpit) < 0)
182e2849863SThomas Graf 		panic("Unable to register rtnetlink message handler, "
183e2849863SThomas Graf 		      "protocol = %d, message type = %d\n",
184e2849863SThomas Graf 		      protocol, msgtype);
185e2849863SThomas Graf }
186e2849863SThomas Graf 
187e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_register);
188e2849863SThomas Graf 
189e2849863SThomas Graf /**
190e2849863SThomas Graf  * rtnl_unregister - Unregister a rtnetlink message type
191e2849863SThomas Graf  * @protocol: Protocol family or PF_UNSPEC
192e2849863SThomas Graf  * @msgtype: rtnetlink message type
193e2849863SThomas Graf  *
194e2849863SThomas Graf  * Returns 0 on success or a negative error code.
195e2849863SThomas Graf  */
196e2849863SThomas Graf int rtnl_unregister(int protocol, int msgtype)
197e2849863SThomas Graf {
198e2849863SThomas Graf 	int msgindex;
199e2849863SThomas Graf 
200e2849863SThomas Graf 	BUG_ON(protocol < 0 || protocol >= NPROTO);
201e2849863SThomas Graf 	msgindex = rtm_msgindex(msgtype);
202e2849863SThomas Graf 
203e2849863SThomas Graf 	if (rtnl_msg_handlers[protocol] == NULL)
204e2849863SThomas Graf 		return -ENOENT;
205e2849863SThomas Graf 
206e2849863SThomas Graf 	rtnl_msg_handlers[protocol][msgindex].doit = NULL;
207e2849863SThomas Graf 	rtnl_msg_handlers[protocol][msgindex].dumpit = NULL;
208e2849863SThomas Graf 
209e2849863SThomas Graf 	return 0;
210e2849863SThomas Graf }
211e2849863SThomas Graf 
212e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister);
213e2849863SThomas Graf 
214e2849863SThomas Graf /**
215e2849863SThomas Graf  * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
216e2849863SThomas Graf  * @protocol : Protocol family or PF_UNSPEC
217e2849863SThomas Graf  *
218e2849863SThomas Graf  * Identical to calling rtnl_unregster() for all registered message types
219e2849863SThomas Graf  * of a certain protocol family.
220e2849863SThomas Graf  */
221e2849863SThomas Graf void rtnl_unregister_all(int protocol)
222e2849863SThomas Graf {
223e2849863SThomas Graf 	BUG_ON(protocol < 0 || protocol >= NPROTO);
224e2849863SThomas Graf 
225e2849863SThomas Graf 	kfree(rtnl_msg_handlers[protocol]);
226e2849863SThomas Graf 	rtnl_msg_handlers[protocol] = NULL;
227e2849863SThomas Graf }
228e2849863SThomas Graf 
229e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister_all);
2301da177e4SLinus Torvalds 
23138f7b870SPatrick McHardy static LIST_HEAD(link_ops);
23238f7b870SPatrick McHardy 
23338f7b870SPatrick McHardy /**
23438f7b870SPatrick McHardy  * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
23538f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to register
23638f7b870SPatrick McHardy  *
23738f7b870SPatrick McHardy  * The caller must hold the rtnl_mutex. This function should be used
23838f7b870SPatrick McHardy  * by drivers that create devices during module initialization. It
23938f7b870SPatrick McHardy  * must be called before registering the devices.
24038f7b870SPatrick McHardy  *
24138f7b870SPatrick McHardy  * Returns 0 on success or a negative error code.
24238f7b870SPatrick McHardy  */
24338f7b870SPatrick McHardy int __rtnl_link_register(struct rtnl_link_ops *ops)
24438f7b870SPatrick McHardy {
2452d85cba2SPatrick McHardy 	if (!ops->dellink)
2462d85cba2SPatrick McHardy 		ops->dellink = unregister_netdevice;
2472d85cba2SPatrick McHardy 
24838f7b870SPatrick McHardy 	list_add_tail(&ops->list, &link_ops);
24938f7b870SPatrick McHardy 	return 0;
25038f7b870SPatrick McHardy }
25138f7b870SPatrick McHardy 
25238f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_register);
25338f7b870SPatrick McHardy 
25438f7b870SPatrick McHardy /**
25538f7b870SPatrick McHardy  * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
25638f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to register
25738f7b870SPatrick McHardy  *
25838f7b870SPatrick McHardy  * Returns 0 on success or a negative error code.
25938f7b870SPatrick McHardy  */
26038f7b870SPatrick McHardy int rtnl_link_register(struct rtnl_link_ops *ops)
26138f7b870SPatrick McHardy {
26238f7b870SPatrick McHardy 	int err;
26338f7b870SPatrick McHardy 
26438f7b870SPatrick McHardy 	rtnl_lock();
26538f7b870SPatrick McHardy 	err = __rtnl_link_register(ops);
26638f7b870SPatrick McHardy 	rtnl_unlock();
26738f7b870SPatrick McHardy 	return err;
26838f7b870SPatrick McHardy }
26938f7b870SPatrick McHardy 
27038f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_register);
27138f7b870SPatrick McHardy 
27238f7b870SPatrick McHardy /**
27338f7b870SPatrick McHardy  * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
27438f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to unregister
27538f7b870SPatrick McHardy  *
2762d85cba2SPatrick McHardy  * The caller must hold the rtnl_mutex.
27738f7b870SPatrick McHardy  */
27838f7b870SPatrick McHardy void __rtnl_link_unregister(struct rtnl_link_ops *ops)
27938f7b870SPatrick McHardy {
2802d85cba2SPatrick McHardy 	struct net_device *dev, *n;
281881d966bSEric W. Biederman 	struct net *net;
2822d85cba2SPatrick McHardy 
283881d966bSEric W. Biederman 	for_each_net(net) {
28468365458SPatrick McHardy restart:
285881d966bSEric W. Biederman 		for_each_netdev_safe(net, dev, n) {
28668365458SPatrick McHardy 			if (dev->rtnl_link_ops == ops) {
2872d85cba2SPatrick McHardy 				ops->dellink(dev);
28868365458SPatrick McHardy 				goto restart;
28968365458SPatrick McHardy 			}
2902d85cba2SPatrick McHardy 		}
291881d966bSEric W. Biederman 	}
29238f7b870SPatrick McHardy 	list_del(&ops->list);
29338f7b870SPatrick McHardy }
29438f7b870SPatrick McHardy 
29538f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
29638f7b870SPatrick McHardy 
29738f7b870SPatrick McHardy /**
29838f7b870SPatrick McHardy  * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
29938f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to unregister
30038f7b870SPatrick McHardy  */
30138f7b870SPatrick McHardy void rtnl_link_unregister(struct rtnl_link_ops *ops)
30238f7b870SPatrick McHardy {
30338f7b870SPatrick McHardy 	rtnl_lock();
30438f7b870SPatrick McHardy 	__rtnl_link_unregister(ops);
30538f7b870SPatrick McHardy 	rtnl_unlock();
30638f7b870SPatrick McHardy }
30738f7b870SPatrick McHardy 
30838f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_unregister);
30938f7b870SPatrick McHardy 
31038f7b870SPatrick McHardy static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
31138f7b870SPatrick McHardy {
31238f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops;
31338f7b870SPatrick McHardy 
31438f7b870SPatrick McHardy 	list_for_each_entry(ops, &link_ops, list) {
31538f7b870SPatrick McHardy 		if (!strcmp(ops->kind, kind))
31638f7b870SPatrick McHardy 			return ops;
31738f7b870SPatrick McHardy 	}
31838f7b870SPatrick McHardy 	return NULL;
31938f7b870SPatrick McHardy }
32038f7b870SPatrick McHardy 
32138f7b870SPatrick McHardy static size_t rtnl_link_get_size(const struct net_device *dev)
32238f7b870SPatrick McHardy {
32338f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
32438f7b870SPatrick McHardy 	size_t size;
32538f7b870SPatrick McHardy 
32638f7b870SPatrick McHardy 	if (!ops)
32738f7b870SPatrick McHardy 		return 0;
32838f7b870SPatrick McHardy 
32938f7b870SPatrick McHardy 	size = nlmsg_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
33038f7b870SPatrick McHardy 	       nlmsg_total_size(strlen(ops->kind) + 1);	 /* IFLA_INFO_KIND */
33138f7b870SPatrick McHardy 
33238f7b870SPatrick McHardy 	if (ops->get_size)
33338f7b870SPatrick McHardy 		/* IFLA_INFO_DATA + nested data */
33438f7b870SPatrick McHardy 		size += nlmsg_total_size(sizeof(struct nlattr)) +
33538f7b870SPatrick McHardy 			ops->get_size(dev);
33638f7b870SPatrick McHardy 
33738f7b870SPatrick McHardy 	if (ops->get_xstats_size)
33838f7b870SPatrick McHardy 		size += ops->get_xstats_size(dev);	/* IFLA_INFO_XSTATS */
33938f7b870SPatrick McHardy 
34038f7b870SPatrick McHardy 	return size;
34138f7b870SPatrick McHardy }
34238f7b870SPatrick McHardy 
34338f7b870SPatrick McHardy static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
34438f7b870SPatrick McHardy {
34538f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
34638f7b870SPatrick McHardy 	struct nlattr *linkinfo, *data;
34738f7b870SPatrick McHardy 	int err = -EMSGSIZE;
34838f7b870SPatrick McHardy 
34938f7b870SPatrick McHardy 	linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
35038f7b870SPatrick McHardy 	if (linkinfo == NULL)
35138f7b870SPatrick McHardy 		goto out;
35238f7b870SPatrick McHardy 
35338f7b870SPatrick McHardy 	if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
35438f7b870SPatrick McHardy 		goto err_cancel_link;
35538f7b870SPatrick McHardy 	if (ops->fill_xstats) {
35638f7b870SPatrick McHardy 		err = ops->fill_xstats(skb, dev);
35738f7b870SPatrick McHardy 		if (err < 0)
35838f7b870SPatrick McHardy 			goto err_cancel_link;
35938f7b870SPatrick McHardy 	}
36038f7b870SPatrick McHardy 	if (ops->fill_info) {
36138f7b870SPatrick McHardy 		data = nla_nest_start(skb, IFLA_INFO_DATA);
36238f7b870SPatrick McHardy 		if (data == NULL)
36338f7b870SPatrick McHardy 			goto err_cancel_link;
36438f7b870SPatrick McHardy 		err = ops->fill_info(skb, dev);
36538f7b870SPatrick McHardy 		if (err < 0)
36638f7b870SPatrick McHardy 			goto err_cancel_data;
36738f7b870SPatrick McHardy 		nla_nest_end(skb, data);
36838f7b870SPatrick McHardy 	}
36938f7b870SPatrick McHardy 
37038f7b870SPatrick McHardy 	nla_nest_end(skb, linkinfo);
37138f7b870SPatrick McHardy 	return 0;
37238f7b870SPatrick McHardy 
37338f7b870SPatrick McHardy err_cancel_data:
37438f7b870SPatrick McHardy 	nla_nest_cancel(skb, data);
37538f7b870SPatrick McHardy err_cancel_link:
37638f7b870SPatrick McHardy 	nla_nest_cancel(skb, linkinfo);
37738f7b870SPatrick McHardy out:
37838f7b870SPatrick McHardy 	return err;
37938f7b870SPatrick McHardy }
38038f7b870SPatrick McHardy 
381db46edc6SThomas Graf static const int rtm_min[RTM_NR_FAMILIES] =
3821da177e4SLinus Torvalds {
383f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWLINK)]      = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
384f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWADDR)]      = NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
385f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWROUTE)]     = NLMSG_LENGTH(sizeof(struct rtmsg)),
38614c0b97dSThomas Graf 	[RTM_FAM(RTM_NEWRULE)]      = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)),
387f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWQDISC)]     = NLMSG_LENGTH(sizeof(struct tcmsg)),
388f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWTCLASS)]    = NLMSG_LENGTH(sizeof(struct tcmsg)),
389f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWTFILTER)]   = NLMSG_LENGTH(sizeof(struct tcmsg)),
390f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWACTION)]    = NLMSG_LENGTH(sizeof(struct tcamsg)),
391f90a0a74SThomas Graf 	[RTM_FAM(RTM_GETMULTICAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
392f90a0a74SThomas Graf 	[RTM_FAM(RTM_GETANYCAST)]   = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
3931da177e4SLinus Torvalds };
3941da177e4SLinus Torvalds 
395db46edc6SThomas Graf static const int rta_max[RTM_NR_FAMILIES] =
3961da177e4SLinus Torvalds {
397f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWLINK)]      = IFLA_MAX,
398f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWADDR)]      = IFA_MAX,
399f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWROUTE)]     = RTA_MAX,
40014c0b97dSThomas Graf 	[RTM_FAM(RTM_NEWRULE)]      = FRA_MAX,
401f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWQDISC)]     = TCA_MAX,
402f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWTCLASS)]    = TCA_MAX,
403f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWTFILTER)]   = TCA_MAX,
404f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWACTION)]    = TCAA_MAX,
4051da177e4SLinus Torvalds };
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
4081da177e4SLinus Torvalds {
4091da177e4SLinus Torvalds 	struct rtattr *rta;
4101da177e4SLinus Torvalds 	int size = RTA_LENGTH(attrlen);
4111da177e4SLinus Torvalds 
4121da177e4SLinus Torvalds 	rta = (struct rtattr*)skb_put(skb, RTA_ALIGN(size));
4131da177e4SLinus Torvalds 	rta->rta_type = attrtype;
4141da177e4SLinus Torvalds 	rta->rta_len = size;
4151da177e4SLinus Torvalds 	memcpy(RTA_DATA(rta), data, attrlen);
416b3563c4fSPatrick McHardy 	memset(RTA_DATA(rta) + attrlen, 0, RTA_ALIGN(size) - size);
4171da177e4SLinus Torvalds }
4181da177e4SLinus Torvalds 
41997c53cacSDenis V. Lunev int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned group, int echo)
4201da177e4SLinus Torvalds {
42197c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
4221da177e4SLinus Torvalds 	int err = 0;
4231da177e4SLinus Torvalds 
424ac6d439dSPatrick McHardy 	NETLINK_CB(skb).dst_group = group;
4251da177e4SLinus Torvalds 	if (echo)
4261da177e4SLinus Torvalds 		atomic_inc(&skb->users);
4271da177e4SLinus Torvalds 	netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
4281da177e4SLinus Torvalds 	if (echo)
4291da177e4SLinus Torvalds 		err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
4301da177e4SLinus Torvalds 	return err;
4311da177e4SLinus Torvalds }
4321da177e4SLinus Torvalds 
43397c53cacSDenis V. Lunev int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
4342942e900SThomas Graf {
43597c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
43697c53cacSDenis V. Lunev 
4372942e900SThomas Graf 	return nlmsg_unicast(rtnl, skb, pid);
4382942e900SThomas Graf }
4392942e900SThomas Graf 
44097c53cacSDenis V. Lunev int rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
44197676b6bSThomas Graf 		struct nlmsghdr *nlh, gfp_t flags)
44297676b6bSThomas Graf {
44397c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
44497676b6bSThomas Graf 	int report = 0;
44597676b6bSThomas Graf 
44697676b6bSThomas Graf 	if (nlh)
44797676b6bSThomas Graf 		report = nlmsg_report(nlh);
44897676b6bSThomas Graf 
44997676b6bSThomas Graf 	return nlmsg_notify(rtnl, skb, pid, group, report, flags);
45097676b6bSThomas Graf }
45197676b6bSThomas Graf 
45297c53cacSDenis V. Lunev void rtnl_set_sk_err(struct net *net, u32 group, int error)
45397676b6bSThomas Graf {
45497c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
45597c53cacSDenis V. Lunev 
45697676b6bSThomas Graf 	netlink_set_err(rtnl, 0, group, error);
45797676b6bSThomas Graf }
45897676b6bSThomas Graf 
4591da177e4SLinus Torvalds int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
4601da177e4SLinus Torvalds {
4612d7202bfSThomas Graf 	struct nlattr *mx;
4622d7202bfSThomas Graf 	int i, valid = 0;
4631da177e4SLinus Torvalds 
4642d7202bfSThomas Graf 	mx = nla_nest_start(skb, RTA_METRICS);
4652d7202bfSThomas Graf 	if (mx == NULL)
4662d7202bfSThomas Graf 		return -ENOBUFS;
4672d7202bfSThomas Graf 
4681da177e4SLinus Torvalds 	for (i = 0; i < RTAX_MAX; i++) {
4692d7202bfSThomas Graf 		if (metrics[i]) {
4702d7202bfSThomas Graf 			valid++;
4712d7202bfSThomas Graf 			NLA_PUT_U32(skb, i+1, metrics[i]);
4721da177e4SLinus Torvalds 		}
4732d7202bfSThomas Graf 	}
4741da177e4SLinus Torvalds 
475a57d27fcSDavid S. Miller 	if (!valid) {
476a57d27fcSDavid S. Miller 		nla_nest_cancel(skb, mx);
477a57d27fcSDavid S. Miller 		return 0;
478a57d27fcSDavid S. Miller 	}
4792d7202bfSThomas Graf 
4802d7202bfSThomas Graf 	return nla_nest_end(skb, mx);
4812d7202bfSThomas Graf 
4822d7202bfSThomas Graf nla_put_failure:
4832d7202bfSThomas Graf 	return nla_nest_cancel(skb, mx);
4841da177e4SLinus Torvalds }
4851da177e4SLinus Torvalds 
486e3703b3dSThomas Graf int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
487e3703b3dSThomas Graf 		       u32 ts, u32 tsage, long expires, u32 error)
488e3703b3dSThomas Graf {
489e3703b3dSThomas Graf 	struct rta_cacheinfo ci = {
490e3703b3dSThomas Graf 		.rta_lastuse = jiffies_to_clock_t(jiffies - dst->lastuse),
491e3703b3dSThomas Graf 		.rta_used = dst->__use,
492e3703b3dSThomas Graf 		.rta_clntref = atomic_read(&(dst->__refcnt)),
493e3703b3dSThomas Graf 		.rta_error = error,
494e3703b3dSThomas Graf 		.rta_id =  id,
495e3703b3dSThomas Graf 		.rta_ts = ts,
496e3703b3dSThomas Graf 		.rta_tsage = tsage,
497e3703b3dSThomas Graf 	};
498e3703b3dSThomas Graf 
499e3703b3dSThomas Graf 	if (expires)
500e3703b3dSThomas Graf 		ci.rta_expires = jiffies_to_clock_t(expires);
501e3703b3dSThomas Graf 
502e3703b3dSThomas Graf 	return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
503e3703b3dSThomas Graf }
504e3703b3dSThomas Graf 
505e3703b3dSThomas Graf EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
5061da177e4SLinus Torvalds 
507*93b2d4a2SDavid S. Miller static void set_operstate(struct net_device *dev, unsigned char transition)
508b00055aaSStefan Rompf {
509b00055aaSStefan Rompf 	unsigned char operstate = dev->operstate;
510b00055aaSStefan Rompf 
511b00055aaSStefan Rompf 	switch(transition) {
512b00055aaSStefan Rompf 	case IF_OPER_UP:
513b00055aaSStefan Rompf 		if ((operstate == IF_OPER_DORMANT ||
514b00055aaSStefan Rompf 		     operstate == IF_OPER_UNKNOWN) &&
515b00055aaSStefan Rompf 		    !netif_dormant(dev))
516b00055aaSStefan Rompf 			operstate = IF_OPER_UP;
517b00055aaSStefan Rompf 		break;
518b00055aaSStefan Rompf 
519b00055aaSStefan Rompf 	case IF_OPER_DORMANT:
520b00055aaSStefan Rompf 		if (operstate == IF_OPER_UP ||
521b00055aaSStefan Rompf 		    operstate == IF_OPER_UNKNOWN)
522b00055aaSStefan Rompf 			operstate = IF_OPER_DORMANT;
523b00055aaSStefan Rompf 		break;
5243ff50b79SStephen Hemminger 	}
525b00055aaSStefan Rompf 
526b00055aaSStefan Rompf 	if (dev->operstate != operstate) {
527b00055aaSStefan Rompf 		write_lock_bh(&dev_base_lock);
528b00055aaSStefan Rompf 		dev->operstate = operstate;
529b00055aaSStefan Rompf 		write_unlock_bh(&dev_base_lock);
530b00055aaSStefan Rompf 		netdev_state_change(dev);
531*93b2d4a2SDavid S. Miller 	}
532b00055aaSStefan Rompf }
533b00055aaSStefan Rompf 
534b60c5115SThomas Graf static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
535b60c5115SThomas Graf 				 struct net_device_stats *b)
5361da177e4SLinus Torvalds {
537b60c5115SThomas Graf 	a->rx_packets = b->rx_packets;
538b60c5115SThomas Graf 	a->tx_packets = b->tx_packets;
539b60c5115SThomas Graf 	a->rx_bytes = b->rx_bytes;
540b60c5115SThomas Graf 	a->tx_bytes = b->tx_bytes;
541b60c5115SThomas Graf 	a->rx_errors = b->rx_errors;
542b60c5115SThomas Graf 	a->tx_errors = b->tx_errors;
543b60c5115SThomas Graf 	a->rx_dropped = b->rx_dropped;
544b60c5115SThomas Graf 	a->tx_dropped = b->tx_dropped;
545b60c5115SThomas Graf 
546b60c5115SThomas Graf 	a->multicast = b->multicast;
547b60c5115SThomas Graf 	a->collisions = b->collisions;
548b60c5115SThomas Graf 
549b60c5115SThomas Graf 	a->rx_length_errors = b->rx_length_errors;
550b60c5115SThomas Graf 	a->rx_over_errors = b->rx_over_errors;
551b60c5115SThomas Graf 	a->rx_crc_errors = b->rx_crc_errors;
552b60c5115SThomas Graf 	a->rx_frame_errors = b->rx_frame_errors;
553b60c5115SThomas Graf 	a->rx_fifo_errors = b->rx_fifo_errors;
554b60c5115SThomas Graf 	a->rx_missed_errors = b->rx_missed_errors;
555b60c5115SThomas Graf 
556b60c5115SThomas Graf 	a->tx_aborted_errors = b->tx_aborted_errors;
557b60c5115SThomas Graf 	a->tx_carrier_errors = b->tx_carrier_errors;
558b60c5115SThomas Graf 	a->tx_fifo_errors = b->tx_fifo_errors;
559b60c5115SThomas Graf 	a->tx_heartbeat_errors = b->tx_heartbeat_errors;
560b60c5115SThomas Graf 	a->tx_window_errors = b->tx_window_errors;
561b60c5115SThomas Graf 
562b60c5115SThomas Graf 	a->rx_compressed = b->rx_compressed;
563b60c5115SThomas Graf 	a->tx_compressed = b->tx_compressed;
564b60c5115SThomas Graf };
565b60c5115SThomas Graf 
56638f7b870SPatrick McHardy static inline size_t if_nlmsg_size(const struct net_device *dev)
567339bf98fSThomas Graf {
568339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
569339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
570339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
571339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rtnl_link_ifmap))
572339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rtnl_link_stats))
573339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
574339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
575339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_TXQLEN */
576339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_WEIGHT */
577339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MTU */
578339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_LINK */
579339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MASTER */
580339bf98fSThomas Graf 	       + nla_total_size(1) /* IFLA_OPERSTATE */
58138f7b870SPatrick McHardy 	       + nla_total_size(1) /* IFLA_LINKMODE */
58238f7b870SPatrick McHardy 	       + rtnl_link_get_size(dev); /* IFLA_LINKINFO */
583339bf98fSThomas Graf }
584339bf98fSThomas Graf 
585b60c5115SThomas Graf static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
586575c3e2aSPatrick McHardy 			    int type, u32 pid, u32 seq, u32 change,
587575c3e2aSPatrick McHardy 			    unsigned int flags)
588b60c5115SThomas Graf {
589b60c5115SThomas Graf 	struct ifinfomsg *ifm;
5901da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
5911da177e4SLinus Torvalds 
592b60c5115SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
593b60c5115SThomas Graf 	if (nlh == NULL)
59426932566SPatrick McHardy 		return -EMSGSIZE;
5951da177e4SLinus Torvalds 
596b60c5115SThomas Graf 	ifm = nlmsg_data(nlh);
597b60c5115SThomas Graf 	ifm->ifi_family = AF_UNSPEC;
598b60c5115SThomas Graf 	ifm->__ifi_pad = 0;
599b60c5115SThomas Graf 	ifm->ifi_type = dev->type;
600b60c5115SThomas Graf 	ifm->ifi_index = dev->ifindex;
601b60c5115SThomas Graf 	ifm->ifi_flags = dev_get_flags(dev);
602b60c5115SThomas Graf 	ifm->ifi_change = change;
6031da177e4SLinus Torvalds 
604b60c5115SThomas Graf 	NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
605b60c5115SThomas Graf 	NLA_PUT_U32(skb, IFLA_TXQLEN, dev->tx_queue_len);
606b60c5115SThomas Graf 	NLA_PUT_U8(skb, IFLA_OPERSTATE,
607b60c5115SThomas Graf 		   netif_running(dev) ? dev->operstate : IF_OPER_DOWN);
608b60c5115SThomas Graf 	NLA_PUT_U8(skb, IFLA_LINKMODE, dev->link_mode);
609b60c5115SThomas Graf 	NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
6101da177e4SLinus Torvalds 
611b60c5115SThomas Graf 	if (dev->ifindex != dev->iflink)
612b60c5115SThomas Graf 		NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
6131da177e4SLinus Torvalds 
614b60c5115SThomas Graf 	if (dev->master)
615b60c5115SThomas Graf 		NLA_PUT_U32(skb, IFLA_MASTER, dev->master->ifindex);
616b60c5115SThomas Graf 
617b60c5115SThomas Graf 	if (dev->qdisc_sleeping)
618b60c5115SThomas Graf 		NLA_PUT_STRING(skb, IFLA_QDISC, dev->qdisc_sleeping->ops->id);
619b00055aaSStefan Rompf 
620b00055aaSStefan Rompf 	if (1) {
6211da177e4SLinus Torvalds 		struct rtnl_link_ifmap map = {
6221da177e4SLinus Torvalds 			.mem_start   = dev->mem_start,
6231da177e4SLinus Torvalds 			.mem_end     = dev->mem_end,
6241da177e4SLinus Torvalds 			.base_addr   = dev->base_addr,
6251da177e4SLinus Torvalds 			.irq         = dev->irq,
6261da177e4SLinus Torvalds 			.dma         = dev->dma,
6271da177e4SLinus Torvalds 			.port        = dev->if_port,
6281da177e4SLinus Torvalds 		};
629b60c5115SThomas Graf 		NLA_PUT(skb, IFLA_MAP, sizeof(map), &map);
6301da177e4SLinus Torvalds 	}
6311da177e4SLinus Torvalds 
6321da177e4SLinus Torvalds 	if (dev->addr_len) {
633b60c5115SThomas Graf 		NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
634b60c5115SThomas Graf 		NLA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast);
6351da177e4SLinus Torvalds 	}
6361da177e4SLinus Torvalds 
6371da177e4SLinus Torvalds 	if (dev->get_stats) {
638b60c5115SThomas Graf 		struct net_device_stats *stats = dev->get_stats(dev);
6391da177e4SLinus Torvalds 		if (stats) {
640b60c5115SThomas Graf 			struct nlattr *attr;
6411da177e4SLinus Torvalds 
642b60c5115SThomas Graf 			attr = nla_reserve(skb, IFLA_STATS,
643b60c5115SThomas Graf 					   sizeof(struct rtnl_link_stats));
644b60c5115SThomas Graf 			if (attr == NULL)
645b60c5115SThomas Graf 				goto nla_put_failure;
646b60c5115SThomas Graf 
647b60c5115SThomas Graf 			copy_rtnl_link_stats(nla_data(attr), stats);
6481da177e4SLinus Torvalds 		}
6491da177e4SLinus Torvalds 	}
6501da177e4SLinus Torvalds 
65138f7b870SPatrick McHardy 	if (dev->rtnl_link_ops) {
65238f7b870SPatrick McHardy 		if (rtnl_link_fill(skb, dev) < 0)
65338f7b870SPatrick McHardy 			goto nla_put_failure;
65438f7b870SPatrick McHardy 	}
65538f7b870SPatrick McHardy 
656b60c5115SThomas Graf 	return nlmsg_end(skb, nlh);
657b60c5115SThomas Graf 
658b60c5115SThomas Graf nla_put_failure:
65926932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
66026932566SPatrick McHardy 	return -EMSGSIZE;
6611da177e4SLinus Torvalds }
6621da177e4SLinus Torvalds 
663b60c5115SThomas Graf static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
6641da177e4SLinus Torvalds {
665881d966bSEric W. Biederman 	struct net *net = skb->sk->sk_net;
6661da177e4SLinus Torvalds 	int idx;
6671da177e4SLinus Torvalds 	int s_idx = cb->args[0];
6681da177e4SLinus Torvalds 	struct net_device *dev;
6691da177e4SLinus Torvalds 
6707562f876SPavel Emelianov 	idx = 0;
671881d966bSEric W. Biederman 	for_each_netdev(net, dev) {
6721da177e4SLinus Torvalds 		if (idx < s_idx)
6737562f876SPavel Emelianov 			goto cont;
674575c3e2aSPatrick McHardy 		if (rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
675b6544c0bSJamal Hadi Salim 				     NETLINK_CB(cb->skb).pid,
676b60c5115SThomas Graf 				     cb->nlh->nlmsg_seq, 0, NLM_F_MULTI) <= 0)
6771da177e4SLinus Torvalds 			break;
6787562f876SPavel Emelianov cont:
6797562f876SPavel Emelianov 		idx++;
6801da177e4SLinus Torvalds 	}
6811da177e4SLinus Torvalds 	cb->args[0] = idx;
6821da177e4SLinus Torvalds 
6831da177e4SLinus Torvalds 	return skb->len;
6841da177e4SLinus Torvalds }
6851da177e4SLinus Torvalds 
686e7199288SPavel Emelianov const struct nla_policy ifla_policy[IFLA_MAX+1] = {
6875176f91eSThomas Graf 	[IFLA_IFNAME]		= { .type = NLA_STRING, .len = IFNAMSIZ-1 },
68838f7b870SPatrick McHardy 	[IFLA_ADDRESS]		= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
68938f7b870SPatrick McHardy 	[IFLA_BROADCAST]	= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
6905176f91eSThomas Graf 	[IFLA_MAP]		= { .len = sizeof(struct rtnl_link_ifmap) },
691da5e0494SThomas Graf 	[IFLA_MTU]		= { .type = NLA_U32 },
692da5e0494SThomas Graf 	[IFLA_TXQLEN]		= { .type = NLA_U32 },
693da5e0494SThomas Graf 	[IFLA_WEIGHT]		= { .type = NLA_U32 },
694da5e0494SThomas Graf 	[IFLA_OPERSTATE]	= { .type = NLA_U8 },
695da5e0494SThomas Graf 	[IFLA_LINKMODE]		= { .type = NLA_U8 },
696d8a5ec67SEric W. Biederman 	[IFLA_NET_NS_PID]	= { .type = NLA_U32 },
697da5e0494SThomas Graf };
6981da177e4SLinus Torvalds 
69938f7b870SPatrick McHardy static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
70038f7b870SPatrick McHardy 	[IFLA_INFO_KIND]	= { .type = NLA_STRING },
70138f7b870SPatrick McHardy 	[IFLA_INFO_DATA]	= { .type = NLA_NESTED },
70238f7b870SPatrick McHardy };
70338f7b870SPatrick McHardy 
704d8a5ec67SEric W. Biederman static struct net *get_net_ns_by_pid(pid_t pid)
705d8a5ec67SEric W. Biederman {
706d8a5ec67SEric W. Biederman 	struct task_struct *tsk;
707d8a5ec67SEric W. Biederman 	struct net *net;
708d8a5ec67SEric W. Biederman 
709d8a5ec67SEric W. Biederman 	/* Lookup the network namespace */
710d8a5ec67SEric W. Biederman 	net = ERR_PTR(-ESRCH);
711d8a5ec67SEric W. Biederman 	rcu_read_lock();
712ceaa79c4SEric W. Biederman 	tsk = find_task_by_vpid(pid);
713d8a5ec67SEric W. Biederman 	if (tsk) {
714cf7b708cSPavel Emelyanov 		struct nsproxy *nsproxy;
715cf7b708cSPavel Emelyanov 		nsproxy = task_nsproxy(tsk);
716cf7b708cSPavel Emelyanov 		if (nsproxy)
717cf7b708cSPavel Emelyanov 			net = get_net(nsproxy->net_ns);
718d8a5ec67SEric W. Biederman 	}
719d8a5ec67SEric W. Biederman 	rcu_read_unlock();
720d8a5ec67SEric W. Biederman 	return net;
721d8a5ec67SEric W. Biederman }
722d8a5ec67SEric W. Biederman 
7230157f60cSPatrick McHardy static int do_setlink(struct net_device *dev, struct ifinfomsg *ifm,
72438f7b870SPatrick McHardy 		      struct nlattr **tb, char *ifname, int modified)
7250157f60cSPatrick McHardy {
72638f7b870SPatrick McHardy 	int send_addr_notify = 0;
7270157f60cSPatrick McHardy 	int err;
7280157f60cSPatrick McHardy 
729d8a5ec67SEric W. Biederman 	if (tb[IFLA_NET_NS_PID]) {
730d8a5ec67SEric W. Biederman 		struct net *net;
731d8a5ec67SEric W. Biederman 		net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
732d8a5ec67SEric W. Biederman 		if (IS_ERR(net)) {
733d8a5ec67SEric W. Biederman 			err = PTR_ERR(net);
734d8a5ec67SEric W. Biederman 			goto errout;
735d8a5ec67SEric W. Biederman 		}
736d8a5ec67SEric W. Biederman 		err = dev_change_net_namespace(dev, net, ifname);
737d8a5ec67SEric W. Biederman 		put_net(net);
738d8a5ec67SEric W. Biederman 		if (err)
739d8a5ec67SEric W. Biederman 			goto errout;
740d8a5ec67SEric W. Biederman 		modified = 1;
741d8a5ec67SEric W. Biederman 	}
742d8a5ec67SEric W. Biederman 
7430157f60cSPatrick McHardy 	if (tb[IFLA_MAP]) {
7440157f60cSPatrick McHardy 		struct rtnl_link_ifmap *u_map;
7450157f60cSPatrick McHardy 		struct ifmap k_map;
7460157f60cSPatrick McHardy 
7470157f60cSPatrick McHardy 		if (!dev->set_config) {
7480157f60cSPatrick McHardy 			err = -EOPNOTSUPP;
7490157f60cSPatrick McHardy 			goto errout;
7500157f60cSPatrick McHardy 		}
7510157f60cSPatrick McHardy 
7520157f60cSPatrick McHardy 		if (!netif_device_present(dev)) {
7530157f60cSPatrick McHardy 			err = -ENODEV;
7540157f60cSPatrick McHardy 			goto errout;
7550157f60cSPatrick McHardy 		}
7560157f60cSPatrick McHardy 
7570157f60cSPatrick McHardy 		u_map = nla_data(tb[IFLA_MAP]);
7580157f60cSPatrick McHardy 		k_map.mem_start = (unsigned long) u_map->mem_start;
7590157f60cSPatrick McHardy 		k_map.mem_end = (unsigned long) u_map->mem_end;
7600157f60cSPatrick McHardy 		k_map.base_addr = (unsigned short) u_map->base_addr;
7610157f60cSPatrick McHardy 		k_map.irq = (unsigned char) u_map->irq;
7620157f60cSPatrick McHardy 		k_map.dma = (unsigned char) u_map->dma;
7630157f60cSPatrick McHardy 		k_map.port = (unsigned char) u_map->port;
7640157f60cSPatrick McHardy 
7650157f60cSPatrick McHardy 		err = dev->set_config(dev, &k_map);
7660157f60cSPatrick McHardy 		if (err < 0)
7670157f60cSPatrick McHardy 			goto errout;
7680157f60cSPatrick McHardy 
7690157f60cSPatrick McHardy 		modified = 1;
7700157f60cSPatrick McHardy 	}
7710157f60cSPatrick McHardy 
7720157f60cSPatrick McHardy 	if (tb[IFLA_ADDRESS]) {
7730157f60cSPatrick McHardy 		struct sockaddr *sa;
7740157f60cSPatrick McHardy 		int len;
7750157f60cSPatrick McHardy 
7760157f60cSPatrick McHardy 		if (!dev->set_mac_address) {
7770157f60cSPatrick McHardy 			err = -EOPNOTSUPP;
7780157f60cSPatrick McHardy 			goto errout;
7790157f60cSPatrick McHardy 		}
7800157f60cSPatrick McHardy 
7810157f60cSPatrick McHardy 		if (!netif_device_present(dev)) {
7820157f60cSPatrick McHardy 			err = -ENODEV;
7830157f60cSPatrick McHardy 			goto errout;
7840157f60cSPatrick McHardy 		}
7850157f60cSPatrick McHardy 
7860157f60cSPatrick McHardy 		len = sizeof(sa_family_t) + dev->addr_len;
7870157f60cSPatrick McHardy 		sa = kmalloc(len, GFP_KERNEL);
7880157f60cSPatrick McHardy 		if (!sa) {
7890157f60cSPatrick McHardy 			err = -ENOMEM;
7900157f60cSPatrick McHardy 			goto errout;
7910157f60cSPatrick McHardy 		}
7920157f60cSPatrick McHardy 		sa->sa_family = dev->type;
7930157f60cSPatrick McHardy 		memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
7940157f60cSPatrick McHardy 		       dev->addr_len);
7950157f60cSPatrick McHardy 		err = dev->set_mac_address(dev, sa);
7960157f60cSPatrick McHardy 		kfree(sa);
7970157f60cSPatrick McHardy 		if (err)
7980157f60cSPatrick McHardy 			goto errout;
7990157f60cSPatrick McHardy 		send_addr_notify = 1;
8000157f60cSPatrick McHardy 		modified = 1;
8010157f60cSPatrick McHardy 	}
8020157f60cSPatrick McHardy 
8030157f60cSPatrick McHardy 	if (tb[IFLA_MTU]) {
8040157f60cSPatrick McHardy 		err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
8050157f60cSPatrick McHardy 		if (err < 0)
8060157f60cSPatrick McHardy 			goto errout;
8070157f60cSPatrick McHardy 		modified = 1;
8080157f60cSPatrick McHardy 	}
8090157f60cSPatrick McHardy 
8100157f60cSPatrick McHardy 	/*
8110157f60cSPatrick McHardy 	 * Interface selected by interface index but interface
8120157f60cSPatrick McHardy 	 * name provided implies that a name change has been
8130157f60cSPatrick McHardy 	 * requested.
8140157f60cSPatrick McHardy 	 */
8150157f60cSPatrick McHardy 	if (ifm->ifi_index > 0 && ifname[0]) {
8160157f60cSPatrick McHardy 		err = dev_change_name(dev, ifname);
8170157f60cSPatrick McHardy 		if (err < 0)
8180157f60cSPatrick McHardy 			goto errout;
8190157f60cSPatrick McHardy 		modified = 1;
8200157f60cSPatrick McHardy 	}
8210157f60cSPatrick McHardy 
8220157f60cSPatrick McHardy 	if (tb[IFLA_BROADCAST]) {
8230157f60cSPatrick McHardy 		nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
8240157f60cSPatrick McHardy 		send_addr_notify = 1;
8250157f60cSPatrick McHardy 	}
8260157f60cSPatrick McHardy 
8270157f60cSPatrick McHardy 	if (ifm->ifi_flags || ifm->ifi_change) {
8280157f60cSPatrick McHardy 		unsigned int flags = ifm->ifi_flags;
8290157f60cSPatrick McHardy 
8300157f60cSPatrick McHardy 		/* bugwards compatibility: ifi_change == 0 is treated as ~0 */
8310157f60cSPatrick McHardy 		if (ifm->ifi_change)
8320157f60cSPatrick McHardy 			flags = (flags & ifm->ifi_change) |
8330157f60cSPatrick McHardy 				(dev->flags & ~ifm->ifi_change);
8340157f60cSPatrick McHardy 		dev_change_flags(dev, flags);
8350157f60cSPatrick McHardy 	}
8360157f60cSPatrick McHardy 
837*93b2d4a2SDavid S. Miller 	if (tb[IFLA_TXQLEN])
8380157f60cSPatrick McHardy 		dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
8390157f60cSPatrick McHardy 
8400157f60cSPatrick McHardy 	if (tb[IFLA_OPERSTATE])
841*93b2d4a2SDavid S. Miller 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
8420157f60cSPatrick McHardy 
8430157f60cSPatrick McHardy 	if (tb[IFLA_LINKMODE]) {
8440157f60cSPatrick McHardy 		write_lock_bh(&dev_base_lock);
8450157f60cSPatrick McHardy 		dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
846*93b2d4a2SDavid S. Miller 		write_unlock_bh(&dev_base_lock);
8470157f60cSPatrick McHardy 	}
8480157f60cSPatrick McHardy 
8490157f60cSPatrick McHardy 	err = 0;
8500157f60cSPatrick McHardy 
8510157f60cSPatrick McHardy errout:
8520157f60cSPatrick McHardy 	if (err < 0 && modified && net_ratelimit())
8530157f60cSPatrick McHardy 		printk(KERN_WARNING "A link change request failed with "
8540157f60cSPatrick McHardy 		       "some changes comitted already. Interface %s may "
8550157f60cSPatrick McHardy 		       "have been left with an inconsistent configuration, "
8560157f60cSPatrick McHardy 		       "please check.\n", dev->name);
8570157f60cSPatrick McHardy 
8580157f60cSPatrick McHardy 	if (send_addr_notify)
8590157f60cSPatrick McHardy 		call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
8600157f60cSPatrick McHardy 	return err;
8610157f60cSPatrick McHardy }
8620157f60cSPatrick McHardy 
863da5e0494SThomas Graf static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
864da5e0494SThomas Graf {
865881d966bSEric W. Biederman 	struct net *net = skb->sk->sk_net;
866da5e0494SThomas Graf 	struct ifinfomsg *ifm;
867da5e0494SThomas Graf 	struct net_device *dev;
8680157f60cSPatrick McHardy 	int err;
869da5e0494SThomas Graf 	struct nlattr *tb[IFLA_MAX+1];
8701da177e4SLinus Torvalds 	char ifname[IFNAMSIZ];
8711da177e4SLinus Torvalds 
872da5e0494SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
873da5e0494SThomas Graf 	if (err < 0)
874da5e0494SThomas Graf 		goto errout;
8751da177e4SLinus Torvalds 
8765176f91eSThomas Graf 	if (tb[IFLA_IFNAME])
8775176f91eSThomas Graf 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
87878e5b891SPatrick McHardy 	else
87978e5b891SPatrick McHardy 		ifname[0] = '\0';
8801da177e4SLinus Torvalds 
8811da177e4SLinus Torvalds 	err = -EINVAL;
882da5e0494SThomas Graf 	ifm = nlmsg_data(nlh);
88351055be8SPatrick McHardy 	if (ifm->ifi_index > 0)
884881d966bSEric W. Biederman 		dev = dev_get_by_index(net, ifm->ifi_index);
885da5e0494SThomas Graf 	else if (tb[IFLA_IFNAME])
886881d966bSEric W. Biederman 		dev = dev_get_by_name(net, ifname);
887da5e0494SThomas Graf 	else
888da5e0494SThomas Graf 		goto errout;
8891da177e4SLinus Torvalds 
890da5e0494SThomas Graf 	if (dev == NULL) {
891da5e0494SThomas Graf 		err = -ENODEV;
892da5e0494SThomas Graf 		goto errout;
893da5e0494SThomas Graf 	}
8941da177e4SLinus Torvalds 
895da5e0494SThomas Graf 	if (tb[IFLA_ADDRESS] &&
896da5e0494SThomas Graf 	    nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
897da5e0494SThomas Graf 		goto errout_dev;
898da5e0494SThomas Graf 
899da5e0494SThomas Graf 	if (tb[IFLA_BROADCAST] &&
900da5e0494SThomas Graf 	    nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
901da5e0494SThomas Graf 		goto errout_dev;
902da5e0494SThomas Graf 
90338f7b870SPatrick McHardy 	err = do_setlink(dev, ifm, tb, ifname, 0);
904da5e0494SThomas Graf errout_dev:
9051da177e4SLinus Torvalds 	dev_put(dev);
906da5e0494SThomas Graf errout:
9071da177e4SLinus Torvalds 	return err;
9081da177e4SLinus Torvalds }
9091da177e4SLinus Torvalds 
91038f7b870SPatrick McHardy static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
91138f7b870SPatrick McHardy {
912881d966bSEric W. Biederman 	struct net *net = skb->sk->sk_net;
91338f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops;
91438f7b870SPatrick McHardy 	struct net_device *dev;
91538f7b870SPatrick McHardy 	struct ifinfomsg *ifm;
91638f7b870SPatrick McHardy 	char ifname[IFNAMSIZ];
91738f7b870SPatrick McHardy 	struct nlattr *tb[IFLA_MAX+1];
91838f7b870SPatrick McHardy 	int err;
91938f7b870SPatrick McHardy 
92038f7b870SPatrick McHardy 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
92138f7b870SPatrick McHardy 	if (err < 0)
92238f7b870SPatrick McHardy 		return err;
92338f7b870SPatrick McHardy 
92438f7b870SPatrick McHardy 	if (tb[IFLA_IFNAME])
92538f7b870SPatrick McHardy 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
92638f7b870SPatrick McHardy 
92738f7b870SPatrick McHardy 	ifm = nlmsg_data(nlh);
92838f7b870SPatrick McHardy 	if (ifm->ifi_index > 0)
929881d966bSEric W. Biederman 		dev = __dev_get_by_index(net, ifm->ifi_index);
93038f7b870SPatrick McHardy 	else if (tb[IFLA_IFNAME])
931881d966bSEric W. Biederman 		dev = __dev_get_by_name(net, ifname);
93238f7b870SPatrick McHardy 	else
93338f7b870SPatrick McHardy 		return -EINVAL;
93438f7b870SPatrick McHardy 
93538f7b870SPatrick McHardy 	if (!dev)
93638f7b870SPatrick McHardy 		return -ENODEV;
93738f7b870SPatrick McHardy 
93838f7b870SPatrick McHardy 	ops = dev->rtnl_link_ops;
93938f7b870SPatrick McHardy 	if (!ops)
94038f7b870SPatrick McHardy 		return -EOPNOTSUPP;
94138f7b870SPatrick McHardy 
94238f7b870SPatrick McHardy 	ops->dellink(dev);
94338f7b870SPatrick McHardy 	return 0;
94438f7b870SPatrick McHardy }
94538f7b870SPatrick McHardy 
946881d966bSEric W. Biederman struct net_device *rtnl_create_link(struct net *net, char *ifname,
947e7199288SPavel Emelianov 		const struct rtnl_link_ops *ops, struct nlattr *tb[])
948e7199288SPavel Emelianov {
949e7199288SPavel Emelianov 	int err;
950e7199288SPavel Emelianov 	struct net_device *dev;
951e7199288SPavel Emelianov 
952e7199288SPavel Emelianov 	err = -ENOMEM;
953e7199288SPavel Emelianov 	dev = alloc_netdev(ops->priv_size, ifname, ops->setup);
954e7199288SPavel Emelianov 	if (!dev)
955e7199288SPavel Emelianov 		goto err;
956e7199288SPavel Emelianov 
957e7199288SPavel Emelianov 	if (strchr(dev->name, '%')) {
958e7199288SPavel Emelianov 		err = dev_alloc_name(dev, dev->name);
959e7199288SPavel Emelianov 		if (err < 0)
960e7199288SPavel Emelianov 			goto err_free;
961e7199288SPavel Emelianov 	}
962e7199288SPavel Emelianov 
963881d966bSEric W. Biederman 	dev->nd_net = net;
964e7199288SPavel Emelianov 	dev->rtnl_link_ops = ops;
965e7199288SPavel Emelianov 
966e7199288SPavel Emelianov 	if (tb[IFLA_MTU])
967e7199288SPavel Emelianov 		dev->mtu = nla_get_u32(tb[IFLA_MTU]);
968e7199288SPavel Emelianov 	if (tb[IFLA_ADDRESS])
969e7199288SPavel Emelianov 		memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
970e7199288SPavel Emelianov 				nla_len(tb[IFLA_ADDRESS]));
971e7199288SPavel Emelianov 	if (tb[IFLA_BROADCAST])
972e7199288SPavel Emelianov 		memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
973e7199288SPavel Emelianov 				nla_len(tb[IFLA_BROADCAST]));
974e7199288SPavel Emelianov 	if (tb[IFLA_TXQLEN])
975e7199288SPavel Emelianov 		dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
976e7199288SPavel Emelianov 	if (tb[IFLA_OPERSTATE])
977*93b2d4a2SDavid S. Miller 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
978e7199288SPavel Emelianov 	if (tb[IFLA_LINKMODE])
979e7199288SPavel Emelianov 		dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
980e7199288SPavel Emelianov 
981e7199288SPavel Emelianov 	return dev;
982e7199288SPavel Emelianov 
983e7199288SPavel Emelianov err_free:
984e7199288SPavel Emelianov 	free_netdev(dev);
985e7199288SPavel Emelianov err:
986e7199288SPavel Emelianov 	return ERR_PTR(err);
987e7199288SPavel Emelianov }
988e7199288SPavel Emelianov 
98938f7b870SPatrick McHardy static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
99038f7b870SPatrick McHardy {
991881d966bSEric W. Biederman 	struct net *net = skb->sk->sk_net;
99238f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops;
99338f7b870SPatrick McHardy 	struct net_device *dev;
99438f7b870SPatrick McHardy 	struct ifinfomsg *ifm;
99538f7b870SPatrick McHardy 	char kind[MODULE_NAME_LEN];
99638f7b870SPatrick McHardy 	char ifname[IFNAMSIZ];
99738f7b870SPatrick McHardy 	struct nlattr *tb[IFLA_MAX+1];
99838f7b870SPatrick McHardy 	struct nlattr *linkinfo[IFLA_INFO_MAX+1];
99938f7b870SPatrick McHardy 	int err;
100038f7b870SPatrick McHardy 
10018072f085SThomas Graf #ifdef CONFIG_KMOD
100238f7b870SPatrick McHardy replay:
10038072f085SThomas Graf #endif
100438f7b870SPatrick McHardy 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
100538f7b870SPatrick McHardy 	if (err < 0)
100638f7b870SPatrick McHardy 		return err;
100738f7b870SPatrick McHardy 
100838f7b870SPatrick McHardy 	if (tb[IFLA_IFNAME])
100938f7b870SPatrick McHardy 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
101038f7b870SPatrick McHardy 	else
101138f7b870SPatrick McHardy 		ifname[0] = '\0';
101238f7b870SPatrick McHardy 
101338f7b870SPatrick McHardy 	ifm = nlmsg_data(nlh);
101438f7b870SPatrick McHardy 	if (ifm->ifi_index > 0)
1015881d966bSEric W. Biederman 		dev = __dev_get_by_index(net, ifm->ifi_index);
101638f7b870SPatrick McHardy 	else if (ifname[0])
1017881d966bSEric W. Biederman 		dev = __dev_get_by_name(net, ifname);
101838f7b870SPatrick McHardy 	else
101938f7b870SPatrick McHardy 		dev = NULL;
102038f7b870SPatrick McHardy 
102138f7b870SPatrick McHardy 	if (tb[IFLA_LINKINFO]) {
102238f7b870SPatrick McHardy 		err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
102338f7b870SPatrick McHardy 				       tb[IFLA_LINKINFO], ifla_info_policy);
102438f7b870SPatrick McHardy 		if (err < 0)
102538f7b870SPatrick McHardy 			return err;
102638f7b870SPatrick McHardy 	} else
102738f7b870SPatrick McHardy 		memset(linkinfo, 0, sizeof(linkinfo));
102838f7b870SPatrick McHardy 
102938f7b870SPatrick McHardy 	if (linkinfo[IFLA_INFO_KIND]) {
103038f7b870SPatrick McHardy 		nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
103138f7b870SPatrick McHardy 		ops = rtnl_link_ops_get(kind);
103238f7b870SPatrick McHardy 	} else {
103338f7b870SPatrick McHardy 		kind[0] = '\0';
103438f7b870SPatrick McHardy 		ops = NULL;
103538f7b870SPatrick McHardy 	}
103638f7b870SPatrick McHardy 
103738f7b870SPatrick McHardy 	if (1) {
103838f7b870SPatrick McHardy 		struct nlattr *attr[ops ? ops->maxtype + 1 : 0], **data = NULL;
103938f7b870SPatrick McHardy 
104038f7b870SPatrick McHardy 		if (ops) {
104138f7b870SPatrick McHardy 			if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
104238f7b870SPatrick McHardy 				err = nla_parse_nested(attr, ops->maxtype,
104338f7b870SPatrick McHardy 						       linkinfo[IFLA_INFO_DATA],
104438f7b870SPatrick McHardy 						       ops->policy);
104538f7b870SPatrick McHardy 				if (err < 0)
104638f7b870SPatrick McHardy 					return err;
104738f7b870SPatrick McHardy 				data = attr;
104838f7b870SPatrick McHardy 			}
104938f7b870SPatrick McHardy 			if (ops->validate) {
105038f7b870SPatrick McHardy 				err = ops->validate(tb, data);
105138f7b870SPatrick McHardy 				if (err < 0)
105238f7b870SPatrick McHardy 					return err;
105338f7b870SPatrick McHardy 			}
105438f7b870SPatrick McHardy 		}
105538f7b870SPatrick McHardy 
105638f7b870SPatrick McHardy 		if (dev) {
105738f7b870SPatrick McHardy 			int modified = 0;
105838f7b870SPatrick McHardy 
105938f7b870SPatrick McHardy 			if (nlh->nlmsg_flags & NLM_F_EXCL)
106038f7b870SPatrick McHardy 				return -EEXIST;
106138f7b870SPatrick McHardy 			if (nlh->nlmsg_flags & NLM_F_REPLACE)
106238f7b870SPatrick McHardy 				return -EOPNOTSUPP;
106338f7b870SPatrick McHardy 
106438f7b870SPatrick McHardy 			if (linkinfo[IFLA_INFO_DATA]) {
106538f7b870SPatrick McHardy 				if (!ops || ops != dev->rtnl_link_ops ||
106638f7b870SPatrick McHardy 				    !ops->changelink)
106738f7b870SPatrick McHardy 					return -EOPNOTSUPP;
106838f7b870SPatrick McHardy 
106938f7b870SPatrick McHardy 				err = ops->changelink(dev, tb, data);
107038f7b870SPatrick McHardy 				if (err < 0)
107138f7b870SPatrick McHardy 					return err;
107238f7b870SPatrick McHardy 				modified = 1;
107338f7b870SPatrick McHardy 			}
107438f7b870SPatrick McHardy 
107538f7b870SPatrick McHardy 			return do_setlink(dev, ifm, tb, ifname, modified);
107638f7b870SPatrick McHardy 		}
107738f7b870SPatrick McHardy 
107838f7b870SPatrick McHardy 		if (!(nlh->nlmsg_flags & NLM_F_CREATE))
107938f7b870SPatrick McHardy 			return -ENODEV;
108038f7b870SPatrick McHardy 
108138f7b870SPatrick McHardy 		if (ifm->ifi_index || ifm->ifi_flags || ifm->ifi_change)
108238f7b870SPatrick McHardy 			return -EOPNOTSUPP;
10830e06877cSPatrick McHardy 		if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
108438f7b870SPatrick McHardy 			return -EOPNOTSUPP;
108538f7b870SPatrick McHardy 
108638f7b870SPatrick McHardy 		if (!ops) {
108738f7b870SPatrick McHardy #ifdef CONFIG_KMOD
108838f7b870SPatrick McHardy 			if (kind[0]) {
108938f7b870SPatrick McHardy 				__rtnl_unlock();
109038f7b870SPatrick McHardy 				request_module("rtnl-link-%s", kind);
109138f7b870SPatrick McHardy 				rtnl_lock();
109238f7b870SPatrick McHardy 				ops = rtnl_link_ops_get(kind);
109338f7b870SPatrick McHardy 				if (ops)
109438f7b870SPatrick McHardy 					goto replay;
109538f7b870SPatrick McHardy 			}
109638f7b870SPatrick McHardy #endif
109738f7b870SPatrick McHardy 			return -EOPNOTSUPP;
109838f7b870SPatrick McHardy 		}
109938f7b870SPatrick McHardy 
110038f7b870SPatrick McHardy 		if (!ifname[0])
110138f7b870SPatrick McHardy 			snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
110238f7b870SPatrick McHardy 
1103881d966bSEric W. Biederman 		dev = rtnl_create_link(net, ifname, ops, tb);
110438f7b870SPatrick McHardy 
1105e7199288SPavel Emelianov 		if (IS_ERR(dev))
1106e7199288SPavel Emelianov 			err = PTR_ERR(dev);
1107e7199288SPavel Emelianov 		else if (ops->newlink)
110838f7b870SPatrick McHardy 			err = ops->newlink(dev, tb, data);
11092d85cba2SPatrick McHardy 		else
11102d85cba2SPatrick McHardy 			err = register_netdevice(dev);
1111e7199288SPavel Emelianov 
1112e7199288SPavel Emelianov 		if (err < 0 && !IS_ERR(dev))
111338f7b870SPatrick McHardy 			free_netdev(dev);
111438f7b870SPatrick McHardy 		return err;
111538f7b870SPatrick McHardy 	}
111638f7b870SPatrick McHardy }
111738f7b870SPatrick McHardy 
1118b60c5115SThomas Graf static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
1119711e2c33SJean Tourrilhes {
1120881d966bSEric W. Biederman 	struct net *net = skb->sk->sk_net;
1121b60c5115SThomas Graf 	struct ifinfomsg *ifm;
1122b60c5115SThomas Graf 	struct nlattr *tb[IFLA_MAX+1];
1123b60c5115SThomas Graf 	struct net_device *dev = NULL;
1124b60c5115SThomas Graf 	struct sk_buff *nskb;
1125339bf98fSThomas Graf 	int err;
1126711e2c33SJean Tourrilhes 
1127b60c5115SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1128b60c5115SThomas Graf 	if (err < 0)
11299918f230SEric Sesterhenn 		return err;
1130b60c5115SThomas Graf 
1131b60c5115SThomas Graf 	ifm = nlmsg_data(nlh);
113251055be8SPatrick McHardy 	if (ifm->ifi_index > 0) {
1133881d966bSEric W. Biederman 		dev = dev_get_by_index(net, ifm->ifi_index);
1134b60c5115SThomas Graf 		if (dev == NULL)
1135711e2c33SJean Tourrilhes 			return -ENODEV;
1136b60c5115SThomas Graf 	} else
1137b60c5115SThomas Graf 		return -EINVAL;
1138b60c5115SThomas Graf 
113938f7b870SPatrick McHardy 	nskb = nlmsg_new(if_nlmsg_size(dev), GFP_KERNEL);
1140b60c5115SThomas Graf 	if (nskb == NULL) {
1141b60c5115SThomas Graf 		err = -ENOBUFS;
1142b60c5115SThomas Graf 		goto errout;
1143b60c5115SThomas Graf 	}
1144711e2c33SJean Tourrilhes 
1145575c3e2aSPatrick McHardy 	err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).pid,
1146575c3e2aSPatrick McHardy 			       nlh->nlmsg_seq, 0, 0);
114726932566SPatrick McHardy 	if (err < 0) {
114826932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in if_nlmsg_size */
114926932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
115026932566SPatrick McHardy 		kfree_skb(nskb);
115126932566SPatrick McHardy 		goto errout;
115226932566SPatrick McHardy 	}
115397c53cacSDenis V. Lunev 	err = rtnl_unicast(nskb, net, NETLINK_CB(skb).pid);
1154b60c5115SThomas Graf errout:
1155711e2c33SJean Tourrilhes 	dev_put(dev);
1156b60c5115SThomas Graf 
1157711e2c33SJean Tourrilhes 	return err;
1158711e2c33SJean Tourrilhes }
1159711e2c33SJean Tourrilhes 
116042bad1daSAdrian Bunk static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
11611da177e4SLinus Torvalds {
11621da177e4SLinus Torvalds 	int idx;
11631da177e4SLinus Torvalds 	int s_idx = cb->family;
11641da177e4SLinus Torvalds 
11651da177e4SLinus Torvalds 	if (s_idx == 0)
11661da177e4SLinus Torvalds 		s_idx = 1;
11671da177e4SLinus Torvalds 	for (idx=1; idx<NPROTO; idx++) {
11681da177e4SLinus Torvalds 		int type = cb->nlh->nlmsg_type-RTM_BASE;
11691da177e4SLinus Torvalds 		if (idx < s_idx || idx == PF_PACKET)
11701da177e4SLinus Torvalds 			continue;
1171e2849863SThomas Graf 		if (rtnl_msg_handlers[idx] == NULL ||
1172e2849863SThomas Graf 		    rtnl_msg_handlers[idx][type].dumpit == NULL)
11731da177e4SLinus Torvalds 			continue;
11741da177e4SLinus Torvalds 		if (idx > s_idx)
11751da177e4SLinus Torvalds 			memset(&cb->args[0], 0, sizeof(cb->args));
1176e2849863SThomas Graf 		if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
11771da177e4SLinus Torvalds 			break;
11781da177e4SLinus Torvalds 	}
11791da177e4SLinus Torvalds 	cb->family = idx;
11801da177e4SLinus Torvalds 
11811da177e4SLinus Torvalds 	return skb->len;
11821da177e4SLinus Torvalds }
11831da177e4SLinus Torvalds 
11841da177e4SLinus Torvalds void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change)
11851da177e4SLinus Torvalds {
11864b3da706SEric W. Biederman 	struct net *net = dev->nd_net;
11871da177e4SLinus Torvalds 	struct sk_buff *skb;
11880ec6d3f4SThomas Graf 	int err = -ENOBUFS;
11891da177e4SLinus Torvalds 
119038f7b870SPatrick McHardy 	skb = nlmsg_new(if_nlmsg_size(dev), GFP_KERNEL);
11910ec6d3f4SThomas Graf 	if (skb == NULL)
11920ec6d3f4SThomas Graf 		goto errout;
11931da177e4SLinus Torvalds 
1194575c3e2aSPatrick McHardy 	err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0);
119526932566SPatrick McHardy 	if (err < 0) {
119626932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in if_nlmsg_size() */
119726932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
119826932566SPatrick McHardy 		kfree_skb(skb);
119926932566SPatrick McHardy 		goto errout;
120026932566SPatrick McHardy 	}
12014b3da706SEric W. Biederman 	err = rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
12020ec6d3f4SThomas Graf errout:
12030ec6d3f4SThomas Graf 	if (err < 0)
12044b3da706SEric W. Biederman 		rtnl_set_sk_err(net, RTNLGRP_LINK, err);
12051da177e4SLinus Torvalds }
12061da177e4SLinus Torvalds 
12071da177e4SLinus Torvalds /* Protected by RTNL sempahore.  */
12081da177e4SLinus Torvalds static struct rtattr **rta_buf;
12091da177e4SLinus Torvalds static int rtattr_max;
12101da177e4SLinus Torvalds 
12111da177e4SLinus Torvalds /* Process one rtnetlink message. */
12121da177e4SLinus Torvalds 
12131d00a4ebSThomas Graf static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
12141da177e4SLinus Torvalds {
121597c53cacSDenis V. Lunev 	struct net *net = skb->sk->sk_net;
1216e2849863SThomas Graf 	rtnl_doit_func doit;
12171da177e4SLinus Torvalds 	int sz_idx, kind;
12181da177e4SLinus Torvalds 	int min_len;
12191da177e4SLinus Torvalds 	int family;
12201da177e4SLinus Torvalds 	int type;
12211c2d670fSPatrick McHardy 	int err;
12221da177e4SLinus Torvalds 
12231da177e4SLinus Torvalds 	type = nlh->nlmsg_type;
12241da177e4SLinus Torvalds 	if (type > RTM_MAX)
1225038890feSThomas Graf 		return -EOPNOTSUPP;
12261da177e4SLinus Torvalds 
12271da177e4SLinus Torvalds 	type -= RTM_BASE;
12281da177e4SLinus Torvalds 
12291da177e4SLinus Torvalds 	/* All the messages must have at least 1 byte length */
12301da177e4SLinus Torvalds 	if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg)))
12311da177e4SLinus Torvalds 		return 0;
12321da177e4SLinus Torvalds 
12331da177e4SLinus Torvalds 	family = ((struct rtgenmsg*)NLMSG_DATA(nlh))->rtgen_family;
12341d00a4ebSThomas Graf 	if (family >= NPROTO)
12351d00a4ebSThomas Graf 		return -EAFNOSUPPORT;
12361da177e4SLinus Torvalds 
12371da177e4SLinus Torvalds 	sz_idx = type>>2;
12381da177e4SLinus Torvalds 	kind = type&3;
12391da177e4SLinus Torvalds 
12401d00a4ebSThomas Graf 	if (kind != 2 && security_netlink_recv(skb, CAP_NET_ADMIN))
12411d00a4ebSThomas Graf 		return -EPERM;
12421da177e4SLinus Torvalds 
12431da177e4SLinus Torvalds 	if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
124497c53cacSDenis V. Lunev 		struct sock *rtnl;
1245e2849863SThomas Graf 		rtnl_dumpit_func dumpit;
12461da177e4SLinus Torvalds 
1247e2849863SThomas Graf 		dumpit = rtnl_get_dumpit(family, type);
1248e2849863SThomas Graf 		if (dumpit == NULL)
1249038890feSThomas Graf 			return -EOPNOTSUPP;
12501da177e4SLinus Torvalds 
12511c2d670fSPatrick McHardy 		__rtnl_unlock();
125297c53cacSDenis V. Lunev 		rtnl = net->rtnl;
12531c2d670fSPatrick McHardy 		err = netlink_dump_start(rtnl, skb, nlh, dumpit, NULL);
12541c2d670fSPatrick McHardy 		rtnl_lock();
12551c2d670fSPatrick McHardy 		return err;
12561da177e4SLinus Torvalds 	}
12571da177e4SLinus Torvalds 
12581da177e4SLinus Torvalds 	memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *)));
12591da177e4SLinus Torvalds 
12601da177e4SLinus Torvalds 	min_len = rtm_min[sz_idx];
12611da177e4SLinus Torvalds 	if (nlh->nlmsg_len < min_len)
12621d00a4ebSThomas Graf 		return -EINVAL;
12631da177e4SLinus Torvalds 
12641da177e4SLinus Torvalds 	if (nlh->nlmsg_len > min_len) {
12651da177e4SLinus Torvalds 		int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
12661da177e4SLinus Torvalds 		struct rtattr *attr = (void*)nlh + NLMSG_ALIGN(min_len);
12671da177e4SLinus Torvalds 
12681da177e4SLinus Torvalds 		while (RTA_OK(attr, attrlen)) {
12691da177e4SLinus Torvalds 			unsigned flavor = attr->rta_type;
12701da177e4SLinus Torvalds 			if (flavor) {
12711da177e4SLinus Torvalds 				if (flavor > rta_max[sz_idx])
12721d00a4ebSThomas Graf 					return -EINVAL;
12731da177e4SLinus Torvalds 				rta_buf[flavor-1] = attr;
12741da177e4SLinus Torvalds 			}
12751da177e4SLinus Torvalds 			attr = RTA_NEXT(attr, attrlen);
12761da177e4SLinus Torvalds 		}
12771da177e4SLinus Torvalds 	}
12781da177e4SLinus Torvalds 
1279e2849863SThomas Graf 	doit = rtnl_get_doit(family, type);
1280e2849863SThomas Graf 	if (doit == NULL)
1281038890feSThomas Graf 		return -EOPNOTSUPP;
12821da177e4SLinus Torvalds 
12831d00a4ebSThomas Graf 	return doit(skb, nlh, (void *)&rta_buf[0]);
12841da177e4SLinus Torvalds }
12851da177e4SLinus Torvalds 
1286cd40b7d3SDenis V. Lunev static void rtnetlink_rcv(struct sk_buff *skb)
12871da177e4SLinus Torvalds {
12881536cc0dSDenis V. Lunev 	rtnl_lock();
1289cd40b7d3SDenis V. Lunev 	netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
12901536cc0dSDenis V. Lunev 	rtnl_unlock();
12911da177e4SLinus Torvalds }
12921da177e4SLinus Torvalds 
12931da177e4SLinus Torvalds static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
12941da177e4SLinus Torvalds {
12951da177e4SLinus Torvalds 	struct net_device *dev = ptr;
1296e9dc8653SEric W. Biederman 
12971da177e4SLinus Torvalds 	switch (event) {
12981da177e4SLinus Torvalds 	case NETDEV_UNREGISTER:
12991da177e4SLinus Torvalds 		rtmsg_ifinfo(RTM_DELLINK, dev, ~0U);
13001da177e4SLinus Torvalds 		break;
13011da177e4SLinus Torvalds 	case NETDEV_REGISTER:
13021da177e4SLinus Torvalds 		rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
13031da177e4SLinus Torvalds 		break;
13041da177e4SLinus Torvalds 	case NETDEV_UP:
13051da177e4SLinus Torvalds 	case NETDEV_DOWN:
13061da177e4SLinus Torvalds 		rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
13071da177e4SLinus Torvalds 		break;
13081da177e4SLinus Torvalds 	case NETDEV_CHANGE:
13091da177e4SLinus Torvalds 	case NETDEV_GOING_DOWN:
13101da177e4SLinus Torvalds 		break;
13111da177e4SLinus Torvalds 	default:
13121da177e4SLinus Torvalds 		rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
13131da177e4SLinus Torvalds 		break;
13141da177e4SLinus Torvalds 	}
13151da177e4SLinus Torvalds 	return NOTIFY_DONE;
13161da177e4SLinus Torvalds }
13171da177e4SLinus Torvalds 
13181da177e4SLinus Torvalds static struct notifier_block rtnetlink_dev_notifier = {
13191da177e4SLinus Torvalds 	.notifier_call	= rtnetlink_event,
13201da177e4SLinus Torvalds };
13211da177e4SLinus Torvalds 
132297c53cacSDenis V. Lunev 
132397c53cacSDenis V. Lunev static int rtnetlink_net_init(struct net *net)
132497c53cacSDenis V. Lunev {
132597c53cacSDenis V. Lunev 	struct sock *sk;
132697c53cacSDenis V. Lunev 	sk = netlink_kernel_create(net, NETLINK_ROUTE, RTNLGRP_MAX,
132797c53cacSDenis V. Lunev 				   rtnetlink_rcv, &rtnl_mutex, THIS_MODULE);
132897c53cacSDenis V. Lunev 	if (!sk)
132997c53cacSDenis V. Lunev 		return -ENOMEM;
133097c53cacSDenis V. Lunev 	net->rtnl = sk;
133197c53cacSDenis V. Lunev 	return 0;
133297c53cacSDenis V. Lunev }
133397c53cacSDenis V. Lunev 
133497c53cacSDenis V. Lunev static void rtnetlink_net_exit(struct net *net)
133597c53cacSDenis V. Lunev {
1336b7c6ba6eSDenis V. Lunev 	netlink_kernel_release(net->rtnl);
133797c53cacSDenis V. Lunev 	net->rtnl = NULL;
133897c53cacSDenis V. Lunev }
133997c53cacSDenis V. Lunev 
134097c53cacSDenis V. Lunev static struct pernet_operations rtnetlink_net_ops = {
134197c53cacSDenis V. Lunev 	.init = rtnetlink_net_init,
134297c53cacSDenis V. Lunev 	.exit = rtnetlink_net_exit,
134397c53cacSDenis V. Lunev };
134497c53cacSDenis V. Lunev 
13451da177e4SLinus Torvalds void __init rtnetlink_init(void)
13461da177e4SLinus Torvalds {
13471da177e4SLinus Torvalds 	int i;
13481da177e4SLinus Torvalds 
13491da177e4SLinus Torvalds 	rtattr_max = 0;
13501da177e4SLinus Torvalds 	for (i = 0; i < ARRAY_SIZE(rta_max); i++)
13511da177e4SLinus Torvalds 		if (rta_max[i] > rtattr_max)
13521da177e4SLinus Torvalds 			rtattr_max = rta_max[i];
13531da177e4SLinus Torvalds 	rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL);
13541da177e4SLinus Torvalds 	if (!rta_buf)
13551da177e4SLinus Torvalds 		panic("rtnetlink_init: cannot allocate rta_buf\n");
13561da177e4SLinus Torvalds 
135797c53cacSDenis V. Lunev 	if (register_pernet_subsys(&rtnetlink_net_ops))
13581da177e4SLinus Torvalds 		panic("rtnetlink_init: cannot initialize rtnetlink\n");
135997c53cacSDenis V. Lunev 
13601da177e4SLinus Torvalds 	netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV);
13611da177e4SLinus Torvalds 	register_netdevice_notifier(&rtnetlink_dev_notifier);
1362340d17fcSThomas Graf 
1363340d17fcSThomas Graf 	rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, rtnl_dump_ifinfo);
1364340d17fcSThomas Graf 	rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL);
136538f7b870SPatrick McHardy 	rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL);
136638f7b870SPatrick McHardy 	rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL);
1367687ad8ccSThomas Graf 
1368687ad8ccSThomas Graf 	rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all);
1369687ad8ccSThomas Graf 	rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all);
13701da177e4SLinus Torvalds }
13711da177e4SLinus Torvalds 
13721da177e4SLinus Torvalds EXPORT_SYMBOL(__rta_fill);
13731da177e4SLinus Torvalds EXPORT_SYMBOL(rtnetlink_put_metrics);
13741da177e4SLinus Torvalds EXPORT_SYMBOL(rtnl_lock);
13756756ae4bSStephen Hemminger EXPORT_SYMBOL(rtnl_trylock);
13761da177e4SLinus Torvalds EXPORT_SYMBOL(rtnl_unlock);
13772942e900SThomas Graf EXPORT_SYMBOL(rtnl_unicast);
137897676b6bSThomas Graf EXPORT_SYMBOL(rtnl_notify);
137997676b6bSThomas Graf EXPORT_SYMBOL(rtnl_set_sk_err);
1380e7199288SPavel Emelianov EXPORT_SYMBOL(rtnl_create_link);
1381e7199288SPavel Emelianov EXPORT_SYMBOL(ifla_policy);
1382