xref: /openbmc/linux/net/core/rtnetlink.c (revision afdc3238ec948531205f5c5f77d2de7bae519c71)
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>
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds #include <asm/uaccess.h>
401da177e4SLinus Torvalds #include <asm/system.h>
411da177e4SLinus Torvalds #include <asm/string.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>
541da177e4SLinus Torvalds 
55e2849863SThomas Graf struct rtnl_link
56e2849863SThomas Graf {
57e2849863SThomas Graf 	rtnl_doit_func		doit;
58e2849863SThomas Graf 	rtnl_dumpit_func	dumpit;
59e2849863SThomas Graf };
60e2849863SThomas Graf 
616756ae4bSStephen Hemminger static DEFINE_MUTEX(rtnl_mutex);
6256fc85acSThomas Graf static struct sock *rtnl;
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);
776756ae4bSStephen Hemminger 	if (rtnl && rtnl->sk_receive_queue.qlen)
786756ae4bSStephen Hemminger 		rtnl->sk_data_ready(rtnl, 0);
791da177e4SLinus Torvalds 	netdev_run_todo();
801da177e4SLinus Torvalds }
811da177e4SLinus Torvalds 
826756ae4bSStephen Hemminger int rtnl_trylock(void)
836756ae4bSStephen Hemminger {
846756ae4bSStephen Hemminger 	return mutex_trylock(&rtnl_mutex);
856756ae4bSStephen Hemminger }
866756ae4bSStephen Hemminger 
871da177e4SLinus Torvalds int rtattr_parse(struct rtattr *tb[], int maxattr, struct rtattr *rta, int len)
881da177e4SLinus Torvalds {
891da177e4SLinus Torvalds 	memset(tb, 0, sizeof(struct rtattr*)*maxattr);
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds 	while (RTA_OK(rta, len)) {
921da177e4SLinus Torvalds 		unsigned flavor = rta->rta_type;
931da177e4SLinus Torvalds 		if (flavor && flavor <= maxattr)
941da177e4SLinus Torvalds 			tb[flavor-1] = rta;
951da177e4SLinus Torvalds 		rta = RTA_NEXT(rta, len);
961da177e4SLinus Torvalds 	}
971da177e4SLinus Torvalds 	return 0;
981da177e4SLinus Torvalds }
991da177e4SLinus Torvalds 
100*afdc3238SPatrick McHardy int rtattr_parse_nested_compat(struct rtattr *tb[], int maxattr,
101*afdc3238SPatrick McHardy 			       struct rtattr *rta, void **data, int len)
102*afdc3238SPatrick McHardy {
103*afdc3238SPatrick McHardy 	if (RTA_PAYLOAD(rta) < len)
104*afdc3238SPatrick McHardy 		return -1;
105*afdc3238SPatrick McHardy 	*data = RTA_DATA(rta);
106*afdc3238SPatrick McHardy 
107*afdc3238SPatrick McHardy 	if (RTA_PAYLOAD(rta) >= RTA_ALIGN(len) + sizeof(struct rtattr)) {
108*afdc3238SPatrick McHardy 		rta = RTA_DATA(rta) + RTA_ALIGN(len);
109*afdc3238SPatrick McHardy 		return rtattr_parse_nested(tb, maxattr, rta);
110*afdc3238SPatrick McHardy 	}
111*afdc3238SPatrick McHardy 	memset(tb, 0, sizeof(struct rtattr *) * maxattr);
112*afdc3238SPatrick McHardy 	return 0;
113*afdc3238SPatrick McHardy }
114*afdc3238SPatrick McHardy 
11542bad1daSAdrian Bunk static struct rtnl_link *rtnl_msg_handlers[NPROTO];
116e2849863SThomas Graf 
117e2849863SThomas Graf static inline int rtm_msgindex(int msgtype)
118e2849863SThomas Graf {
119e2849863SThomas Graf 	int msgindex = msgtype - RTM_BASE;
120e2849863SThomas Graf 
121e2849863SThomas Graf 	/*
122e2849863SThomas Graf 	 * msgindex < 0 implies someone tried to register a netlink
123e2849863SThomas Graf 	 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
124e2849863SThomas Graf 	 * the message type has not been added to linux/rtnetlink.h
125e2849863SThomas Graf 	 */
126e2849863SThomas Graf 	BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
127e2849863SThomas Graf 
128e2849863SThomas Graf 	return msgindex;
129e2849863SThomas Graf }
130e2849863SThomas Graf 
131e2849863SThomas Graf static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex)
132e2849863SThomas Graf {
133e2849863SThomas Graf 	struct rtnl_link *tab;
134e2849863SThomas Graf 
135e2849863SThomas Graf 	tab = rtnl_msg_handlers[protocol];
13651057f2fSThomas Graf 	if (tab == NULL || tab[msgindex].doit == NULL)
137e2849863SThomas Graf 		tab = rtnl_msg_handlers[PF_UNSPEC];
138e2849863SThomas Graf 
13951057f2fSThomas Graf 	return tab ? tab[msgindex].doit : NULL;
140e2849863SThomas Graf }
141e2849863SThomas Graf 
142e2849863SThomas Graf static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex)
143e2849863SThomas Graf {
144e2849863SThomas Graf 	struct rtnl_link *tab;
145e2849863SThomas Graf 
146e2849863SThomas Graf 	tab = rtnl_msg_handlers[protocol];
14751057f2fSThomas Graf 	if (tab == NULL || tab[msgindex].dumpit == NULL)
148e2849863SThomas Graf 		tab = rtnl_msg_handlers[PF_UNSPEC];
149e2849863SThomas Graf 
15051057f2fSThomas Graf 	return tab ? tab[msgindex].dumpit : NULL;
151e2849863SThomas Graf }
152e2849863SThomas Graf 
153e2849863SThomas Graf /**
154e2849863SThomas Graf  * __rtnl_register - Register a rtnetlink message type
155e2849863SThomas Graf  * @protocol: Protocol family or PF_UNSPEC
156e2849863SThomas Graf  * @msgtype: rtnetlink message type
157e2849863SThomas Graf  * @doit: Function pointer called for each request message
158e2849863SThomas Graf  * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
159e2849863SThomas Graf  *
160e2849863SThomas Graf  * Registers the specified function pointers (at least one of them has
161e2849863SThomas Graf  * to be non-NULL) to be called whenever a request message for the
162e2849863SThomas Graf  * specified protocol family and message type is received.
163e2849863SThomas Graf  *
164e2849863SThomas Graf  * The special protocol family PF_UNSPEC may be used to define fallback
165e2849863SThomas Graf  * function pointers for the case when no entry for the specific protocol
166e2849863SThomas Graf  * family exists.
167e2849863SThomas Graf  *
168e2849863SThomas Graf  * Returns 0 on success or a negative error code.
169e2849863SThomas Graf  */
170e2849863SThomas Graf int __rtnl_register(int protocol, int msgtype,
171e2849863SThomas Graf 		    rtnl_doit_func doit, rtnl_dumpit_func dumpit)
172e2849863SThomas Graf {
173e2849863SThomas Graf 	struct rtnl_link *tab;
174e2849863SThomas Graf 	int msgindex;
175e2849863SThomas Graf 
176e2849863SThomas Graf 	BUG_ON(protocol < 0 || protocol >= NPROTO);
177e2849863SThomas Graf 	msgindex = rtm_msgindex(msgtype);
178e2849863SThomas Graf 
179e2849863SThomas Graf 	tab = rtnl_msg_handlers[protocol];
180e2849863SThomas Graf 	if (tab == NULL) {
181e2849863SThomas Graf 		tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL);
182e2849863SThomas Graf 		if (tab == NULL)
183e2849863SThomas Graf 			return -ENOBUFS;
184e2849863SThomas Graf 
185e2849863SThomas Graf 		rtnl_msg_handlers[protocol] = tab;
186e2849863SThomas Graf 	}
187e2849863SThomas Graf 
188e2849863SThomas Graf 	if (doit)
189e2849863SThomas Graf 		tab[msgindex].doit = doit;
190e2849863SThomas Graf 
191e2849863SThomas Graf 	if (dumpit)
192e2849863SThomas Graf 		tab[msgindex].dumpit = dumpit;
193e2849863SThomas Graf 
194e2849863SThomas Graf 	return 0;
195e2849863SThomas Graf }
196e2849863SThomas Graf 
197e2849863SThomas Graf EXPORT_SYMBOL_GPL(__rtnl_register);
198e2849863SThomas Graf 
199e2849863SThomas Graf /**
200e2849863SThomas Graf  * rtnl_register - Register a rtnetlink message type
201e2849863SThomas Graf  *
202e2849863SThomas Graf  * Identical to __rtnl_register() but panics on failure. This is useful
203e2849863SThomas Graf  * as failure of this function is very unlikely, it can only happen due
204e2849863SThomas Graf  * to lack of memory when allocating the chain to store all message
205e2849863SThomas Graf  * handlers for a protocol. Meant for use in init functions where lack
206e2849863SThomas Graf  * of memory implies no sense in continueing.
207e2849863SThomas Graf  */
208e2849863SThomas Graf void rtnl_register(int protocol, int msgtype,
209e2849863SThomas Graf 		   rtnl_doit_func doit, rtnl_dumpit_func dumpit)
210e2849863SThomas Graf {
211e2849863SThomas Graf 	if (__rtnl_register(protocol, msgtype, doit, dumpit) < 0)
212e2849863SThomas Graf 		panic("Unable to register rtnetlink message handler, "
213e2849863SThomas Graf 		      "protocol = %d, message type = %d\n",
214e2849863SThomas Graf 		      protocol, msgtype);
215e2849863SThomas Graf }
216e2849863SThomas Graf 
217e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_register);
218e2849863SThomas Graf 
219e2849863SThomas Graf /**
220e2849863SThomas Graf  * rtnl_unregister - Unregister a rtnetlink message type
221e2849863SThomas Graf  * @protocol: Protocol family or PF_UNSPEC
222e2849863SThomas Graf  * @msgtype: rtnetlink message type
223e2849863SThomas Graf  *
224e2849863SThomas Graf  * Returns 0 on success or a negative error code.
225e2849863SThomas Graf  */
226e2849863SThomas Graf int rtnl_unregister(int protocol, int msgtype)
227e2849863SThomas Graf {
228e2849863SThomas Graf 	int msgindex;
229e2849863SThomas Graf 
230e2849863SThomas Graf 	BUG_ON(protocol < 0 || protocol >= NPROTO);
231e2849863SThomas Graf 	msgindex = rtm_msgindex(msgtype);
232e2849863SThomas Graf 
233e2849863SThomas Graf 	if (rtnl_msg_handlers[protocol] == NULL)
234e2849863SThomas Graf 		return -ENOENT;
235e2849863SThomas Graf 
236e2849863SThomas Graf 	rtnl_msg_handlers[protocol][msgindex].doit = NULL;
237e2849863SThomas Graf 	rtnl_msg_handlers[protocol][msgindex].dumpit = NULL;
238e2849863SThomas Graf 
239e2849863SThomas Graf 	return 0;
240e2849863SThomas Graf }
241e2849863SThomas Graf 
242e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister);
243e2849863SThomas Graf 
244e2849863SThomas Graf /**
245e2849863SThomas Graf  * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
246e2849863SThomas Graf  * @protocol : Protocol family or PF_UNSPEC
247e2849863SThomas Graf  *
248e2849863SThomas Graf  * Identical to calling rtnl_unregster() for all registered message types
249e2849863SThomas Graf  * of a certain protocol family.
250e2849863SThomas Graf  */
251e2849863SThomas Graf void rtnl_unregister_all(int protocol)
252e2849863SThomas Graf {
253e2849863SThomas Graf 	BUG_ON(protocol < 0 || protocol >= NPROTO);
254e2849863SThomas Graf 
255e2849863SThomas Graf 	kfree(rtnl_msg_handlers[protocol]);
256e2849863SThomas Graf 	rtnl_msg_handlers[protocol] = NULL;
257e2849863SThomas Graf }
258e2849863SThomas Graf 
259e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister_all);
2601da177e4SLinus Torvalds 
26138f7b870SPatrick McHardy static LIST_HEAD(link_ops);
26238f7b870SPatrick McHardy 
26338f7b870SPatrick McHardy /**
26438f7b870SPatrick McHardy  * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
26538f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to register
26638f7b870SPatrick McHardy  *
26738f7b870SPatrick McHardy  * The caller must hold the rtnl_mutex. This function should be used
26838f7b870SPatrick McHardy  * by drivers that create devices during module initialization. It
26938f7b870SPatrick McHardy  * must be called before registering the devices.
27038f7b870SPatrick McHardy  *
27138f7b870SPatrick McHardy  * Returns 0 on success or a negative error code.
27238f7b870SPatrick McHardy  */
27338f7b870SPatrick McHardy int __rtnl_link_register(struct rtnl_link_ops *ops)
27438f7b870SPatrick McHardy {
27538f7b870SPatrick McHardy 	list_add_tail(&ops->list, &link_ops);
27638f7b870SPatrick McHardy 	return 0;
27738f7b870SPatrick McHardy }
27838f7b870SPatrick McHardy 
27938f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_register);
28038f7b870SPatrick McHardy 
28138f7b870SPatrick McHardy /**
28238f7b870SPatrick McHardy  * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
28338f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to register
28438f7b870SPatrick McHardy  *
28538f7b870SPatrick McHardy  * Returns 0 on success or a negative error code.
28638f7b870SPatrick McHardy  */
28738f7b870SPatrick McHardy int rtnl_link_register(struct rtnl_link_ops *ops)
28838f7b870SPatrick McHardy {
28938f7b870SPatrick McHardy 	int err;
29038f7b870SPatrick McHardy 
29138f7b870SPatrick McHardy 	rtnl_lock();
29238f7b870SPatrick McHardy 	err = __rtnl_link_register(ops);
29338f7b870SPatrick McHardy 	rtnl_unlock();
29438f7b870SPatrick McHardy 	return err;
29538f7b870SPatrick McHardy }
29638f7b870SPatrick McHardy 
29738f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_register);
29838f7b870SPatrick McHardy 
29938f7b870SPatrick McHardy /**
30038f7b870SPatrick McHardy  * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
30138f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to unregister
30238f7b870SPatrick McHardy  *
30338f7b870SPatrick McHardy  * The caller must hold the rtnl_mutex. This function should be used
30438f7b870SPatrick McHardy  * by drivers that unregister devices during module unloading. It must
30538f7b870SPatrick McHardy  * be called after unregistering the devices.
30638f7b870SPatrick McHardy  */
30738f7b870SPatrick McHardy void __rtnl_link_unregister(struct rtnl_link_ops *ops)
30838f7b870SPatrick McHardy {
30938f7b870SPatrick McHardy 	list_del(&ops->list);
31038f7b870SPatrick McHardy }
31138f7b870SPatrick McHardy 
31238f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
31338f7b870SPatrick McHardy 
31438f7b870SPatrick McHardy /**
31538f7b870SPatrick McHardy  * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
31638f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to unregister
31738f7b870SPatrick McHardy  */
31838f7b870SPatrick McHardy void rtnl_link_unregister(struct rtnl_link_ops *ops)
31938f7b870SPatrick McHardy {
32038f7b870SPatrick McHardy 	rtnl_lock();
32138f7b870SPatrick McHardy 	__rtnl_link_unregister(ops);
32238f7b870SPatrick McHardy 	rtnl_unlock();
32338f7b870SPatrick McHardy }
32438f7b870SPatrick McHardy 
32538f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_unregister);
32638f7b870SPatrick McHardy 
32738f7b870SPatrick McHardy static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
32838f7b870SPatrick McHardy {
32938f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops;
33038f7b870SPatrick McHardy 
33138f7b870SPatrick McHardy 	list_for_each_entry(ops, &link_ops, list) {
33238f7b870SPatrick McHardy 		if (!strcmp(ops->kind, kind))
33338f7b870SPatrick McHardy 			return ops;
33438f7b870SPatrick McHardy 	}
33538f7b870SPatrick McHardy 	return NULL;
33638f7b870SPatrick McHardy }
33738f7b870SPatrick McHardy 
33838f7b870SPatrick McHardy static size_t rtnl_link_get_size(const struct net_device *dev)
33938f7b870SPatrick McHardy {
34038f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
34138f7b870SPatrick McHardy 	size_t size;
34238f7b870SPatrick McHardy 
34338f7b870SPatrick McHardy 	if (!ops)
34438f7b870SPatrick McHardy 		return 0;
34538f7b870SPatrick McHardy 
34638f7b870SPatrick McHardy 	size = nlmsg_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
34738f7b870SPatrick McHardy 	       nlmsg_total_size(strlen(ops->kind) + 1);	 /* IFLA_INFO_KIND */
34838f7b870SPatrick McHardy 
34938f7b870SPatrick McHardy 	if (ops->get_size)
35038f7b870SPatrick McHardy 		/* IFLA_INFO_DATA + nested data */
35138f7b870SPatrick McHardy 		size += nlmsg_total_size(sizeof(struct nlattr)) +
35238f7b870SPatrick McHardy 			ops->get_size(dev);
35338f7b870SPatrick McHardy 
35438f7b870SPatrick McHardy 	if (ops->get_xstats_size)
35538f7b870SPatrick McHardy 		size += ops->get_xstats_size(dev);	/* IFLA_INFO_XSTATS */
35638f7b870SPatrick McHardy 
35738f7b870SPatrick McHardy 	return size;
35838f7b870SPatrick McHardy }
35938f7b870SPatrick McHardy 
36038f7b870SPatrick McHardy static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
36138f7b870SPatrick McHardy {
36238f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
36338f7b870SPatrick McHardy 	struct nlattr *linkinfo, *data;
36438f7b870SPatrick McHardy 	int err = -EMSGSIZE;
36538f7b870SPatrick McHardy 
36638f7b870SPatrick McHardy 	linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
36738f7b870SPatrick McHardy 	if (linkinfo == NULL)
36838f7b870SPatrick McHardy 		goto out;
36938f7b870SPatrick McHardy 
37038f7b870SPatrick McHardy 	if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
37138f7b870SPatrick McHardy 		goto err_cancel_link;
37238f7b870SPatrick McHardy 	if (ops->fill_xstats) {
37338f7b870SPatrick McHardy 		err = ops->fill_xstats(skb, dev);
37438f7b870SPatrick McHardy 		if (err < 0)
37538f7b870SPatrick McHardy 			goto err_cancel_link;
37638f7b870SPatrick McHardy 	}
37738f7b870SPatrick McHardy 	if (ops->fill_info) {
37838f7b870SPatrick McHardy 		data = nla_nest_start(skb, IFLA_INFO_DATA);
37938f7b870SPatrick McHardy 		if (data == NULL)
38038f7b870SPatrick McHardy 			goto err_cancel_link;
38138f7b870SPatrick McHardy 		err = ops->fill_info(skb, dev);
38238f7b870SPatrick McHardy 		if (err < 0)
38338f7b870SPatrick McHardy 			goto err_cancel_data;
38438f7b870SPatrick McHardy 		nla_nest_end(skb, data);
38538f7b870SPatrick McHardy 	}
38638f7b870SPatrick McHardy 
38738f7b870SPatrick McHardy 	nla_nest_end(skb, linkinfo);
38838f7b870SPatrick McHardy 	return 0;
38938f7b870SPatrick McHardy 
39038f7b870SPatrick McHardy err_cancel_data:
39138f7b870SPatrick McHardy 	nla_nest_cancel(skb, data);
39238f7b870SPatrick McHardy err_cancel_link:
39338f7b870SPatrick McHardy 	nla_nest_cancel(skb, linkinfo);
39438f7b870SPatrick McHardy out:
39538f7b870SPatrick McHardy 	return err;
39638f7b870SPatrick McHardy }
39738f7b870SPatrick McHardy 
398db46edc6SThomas Graf static const int rtm_min[RTM_NR_FAMILIES] =
3991da177e4SLinus Torvalds {
400f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWLINK)]      = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
401f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWADDR)]      = NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
402f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWROUTE)]     = NLMSG_LENGTH(sizeof(struct rtmsg)),
40314c0b97dSThomas Graf 	[RTM_FAM(RTM_NEWRULE)]      = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)),
404f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWQDISC)]     = NLMSG_LENGTH(sizeof(struct tcmsg)),
405f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWTCLASS)]    = NLMSG_LENGTH(sizeof(struct tcmsg)),
406f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWTFILTER)]   = NLMSG_LENGTH(sizeof(struct tcmsg)),
407f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWACTION)]    = NLMSG_LENGTH(sizeof(struct tcamsg)),
408f90a0a74SThomas Graf 	[RTM_FAM(RTM_GETMULTICAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
409f90a0a74SThomas Graf 	[RTM_FAM(RTM_GETANYCAST)]   = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
4101da177e4SLinus Torvalds };
4111da177e4SLinus Torvalds 
412db46edc6SThomas Graf static const int rta_max[RTM_NR_FAMILIES] =
4131da177e4SLinus Torvalds {
414f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWLINK)]      = IFLA_MAX,
415f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWADDR)]      = IFA_MAX,
416f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWROUTE)]     = RTA_MAX,
41714c0b97dSThomas Graf 	[RTM_FAM(RTM_NEWRULE)]      = FRA_MAX,
418f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWQDISC)]     = TCA_MAX,
419f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWTCLASS)]    = TCA_MAX,
420f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWTFILTER)]   = TCA_MAX,
421f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWACTION)]    = TCAA_MAX,
4221da177e4SLinus Torvalds };
4231da177e4SLinus Torvalds 
4241da177e4SLinus Torvalds void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
4251da177e4SLinus Torvalds {
4261da177e4SLinus Torvalds 	struct rtattr *rta;
4271da177e4SLinus Torvalds 	int size = RTA_LENGTH(attrlen);
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds 	rta = (struct rtattr*)skb_put(skb, RTA_ALIGN(size));
4301da177e4SLinus Torvalds 	rta->rta_type = attrtype;
4311da177e4SLinus Torvalds 	rta->rta_len = size;
4321da177e4SLinus Torvalds 	memcpy(RTA_DATA(rta), data, attrlen);
433b3563c4fSPatrick McHardy 	memset(RTA_DATA(rta) + attrlen, 0, RTA_ALIGN(size) - size);
4341da177e4SLinus Torvalds }
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds size_t rtattr_strlcpy(char *dest, const struct rtattr *rta, size_t size)
4371da177e4SLinus Torvalds {
4381da177e4SLinus Torvalds 	size_t ret = RTA_PAYLOAD(rta);
4391da177e4SLinus Torvalds 	char *src = RTA_DATA(rta);
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds 	if (ret > 0 && src[ret - 1] == '\0')
4421da177e4SLinus Torvalds 		ret--;
4431da177e4SLinus Torvalds 	if (size > 0) {
4441da177e4SLinus Torvalds 		size_t len = (ret >= size) ? size - 1 : ret;
4451da177e4SLinus Torvalds 		memset(dest, 0, size);
4461da177e4SLinus Torvalds 		memcpy(dest, src, len);
4471da177e4SLinus Torvalds 	}
4481da177e4SLinus Torvalds 	return ret;
4491da177e4SLinus Torvalds }
4501da177e4SLinus Torvalds 
4511da177e4SLinus Torvalds int rtnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo)
4521da177e4SLinus Torvalds {
4531da177e4SLinus Torvalds 	int err = 0;
4541da177e4SLinus Torvalds 
455ac6d439dSPatrick McHardy 	NETLINK_CB(skb).dst_group = group;
4561da177e4SLinus Torvalds 	if (echo)
4571da177e4SLinus Torvalds 		atomic_inc(&skb->users);
4581da177e4SLinus Torvalds 	netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
4591da177e4SLinus Torvalds 	if (echo)
4601da177e4SLinus Torvalds 		err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
4611da177e4SLinus Torvalds 	return err;
4621da177e4SLinus Torvalds }
4631da177e4SLinus Torvalds 
4642942e900SThomas Graf int rtnl_unicast(struct sk_buff *skb, u32 pid)
4652942e900SThomas Graf {
4662942e900SThomas Graf 	return nlmsg_unicast(rtnl, skb, pid);
4672942e900SThomas Graf }
4682942e900SThomas Graf 
46997676b6bSThomas Graf int rtnl_notify(struct sk_buff *skb, u32 pid, u32 group,
47097676b6bSThomas Graf 		struct nlmsghdr *nlh, gfp_t flags)
47197676b6bSThomas Graf {
47297676b6bSThomas Graf 	int report = 0;
47397676b6bSThomas Graf 
47497676b6bSThomas Graf 	if (nlh)
47597676b6bSThomas Graf 		report = nlmsg_report(nlh);
47697676b6bSThomas Graf 
47797676b6bSThomas Graf 	return nlmsg_notify(rtnl, skb, pid, group, report, flags);
47897676b6bSThomas Graf }
47997676b6bSThomas Graf 
48097676b6bSThomas Graf void rtnl_set_sk_err(u32 group, int error)
48197676b6bSThomas Graf {
48297676b6bSThomas Graf 	netlink_set_err(rtnl, 0, group, error);
48397676b6bSThomas Graf }
48497676b6bSThomas Graf 
4851da177e4SLinus Torvalds int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
4861da177e4SLinus Torvalds {
4872d7202bfSThomas Graf 	struct nlattr *mx;
4882d7202bfSThomas Graf 	int i, valid = 0;
4891da177e4SLinus Torvalds 
4902d7202bfSThomas Graf 	mx = nla_nest_start(skb, RTA_METRICS);
4912d7202bfSThomas Graf 	if (mx == NULL)
4922d7202bfSThomas Graf 		return -ENOBUFS;
4932d7202bfSThomas Graf 
4941da177e4SLinus Torvalds 	for (i = 0; i < RTAX_MAX; i++) {
4952d7202bfSThomas Graf 		if (metrics[i]) {
4962d7202bfSThomas Graf 			valid++;
4972d7202bfSThomas Graf 			NLA_PUT_U32(skb, i+1, metrics[i]);
4981da177e4SLinus Torvalds 		}
4992d7202bfSThomas Graf 	}
5001da177e4SLinus Torvalds 
501a57d27fcSDavid S. Miller 	if (!valid) {
502a57d27fcSDavid S. Miller 		nla_nest_cancel(skb, mx);
503a57d27fcSDavid S. Miller 		return 0;
504a57d27fcSDavid S. Miller 	}
5052d7202bfSThomas Graf 
5062d7202bfSThomas Graf 	return nla_nest_end(skb, mx);
5072d7202bfSThomas Graf 
5082d7202bfSThomas Graf nla_put_failure:
5092d7202bfSThomas Graf 	return nla_nest_cancel(skb, mx);
5101da177e4SLinus Torvalds }
5111da177e4SLinus Torvalds 
512e3703b3dSThomas Graf int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
513e3703b3dSThomas Graf 		       u32 ts, u32 tsage, long expires, u32 error)
514e3703b3dSThomas Graf {
515e3703b3dSThomas Graf 	struct rta_cacheinfo ci = {
516e3703b3dSThomas Graf 		.rta_lastuse = jiffies_to_clock_t(jiffies - dst->lastuse),
517e3703b3dSThomas Graf 		.rta_used = dst->__use,
518e3703b3dSThomas Graf 		.rta_clntref = atomic_read(&(dst->__refcnt)),
519e3703b3dSThomas Graf 		.rta_error = error,
520e3703b3dSThomas Graf 		.rta_id =  id,
521e3703b3dSThomas Graf 		.rta_ts = ts,
522e3703b3dSThomas Graf 		.rta_tsage = tsage,
523e3703b3dSThomas Graf 	};
524e3703b3dSThomas Graf 
525e3703b3dSThomas Graf 	if (expires)
526e3703b3dSThomas Graf 		ci.rta_expires = jiffies_to_clock_t(expires);
527e3703b3dSThomas Graf 
528e3703b3dSThomas Graf 	return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
529e3703b3dSThomas Graf }
530e3703b3dSThomas Graf 
531e3703b3dSThomas Graf EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
5321da177e4SLinus Torvalds 
533b00055aaSStefan Rompf 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);
557b00055aaSStefan Rompf 	}
558b00055aaSStefan Rompf }
559b00055aaSStefan Rompf 
560b60c5115SThomas Graf static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
561b60c5115SThomas Graf 				 struct net_device_stats *b)
5621da177e4SLinus Torvalds {
563b60c5115SThomas Graf 	a->rx_packets = b->rx_packets;
564b60c5115SThomas Graf 	a->tx_packets = b->tx_packets;
565b60c5115SThomas Graf 	a->rx_bytes = b->rx_bytes;
566b60c5115SThomas Graf 	a->tx_bytes = b->tx_bytes;
567b60c5115SThomas Graf 	a->rx_errors = b->rx_errors;
568b60c5115SThomas Graf 	a->tx_errors = b->tx_errors;
569b60c5115SThomas Graf 	a->rx_dropped = b->rx_dropped;
570b60c5115SThomas Graf 	a->tx_dropped = b->tx_dropped;
571b60c5115SThomas Graf 
572b60c5115SThomas Graf 	a->multicast = b->multicast;
573b60c5115SThomas Graf 	a->collisions = b->collisions;
574b60c5115SThomas Graf 
575b60c5115SThomas Graf 	a->rx_length_errors = b->rx_length_errors;
576b60c5115SThomas Graf 	a->rx_over_errors = b->rx_over_errors;
577b60c5115SThomas Graf 	a->rx_crc_errors = b->rx_crc_errors;
578b60c5115SThomas Graf 	a->rx_frame_errors = b->rx_frame_errors;
579b60c5115SThomas Graf 	a->rx_fifo_errors = b->rx_fifo_errors;
580b60c5115SThomas Graf 	a->rx_missed_errors = b->rx_missed_errors;
581b60c5115SThomas Graf 
582b60c5115SThomas Graf 	a->tx_aborted_errors = b->tx_aborted_errors;
583b60c5115SThomas Graf 	a->tx_carrier_errors = b->tx_carrier_errors;
584b60c5115SThomas Graf 	a->tx_fifo_errors = b->tx_fifo_errors;
585b60c5115SThomas Graf 	a->tx_heartbeat_errors = b->tx_heartbeat_errors;
586b60c5115SThomas Graf 	a->tx_window_errors = b->tx_window_errors;
587b60c5115SThomas Graf 
588b60c5115SThomas Graf 	a->rx_compressed = b->rx_compressed;
589b60c5115SThomas Graf 	a->tx_compressed = b->tx_compressed;
590b60c5115SThomas Graf };
591b60c5115SThomas Graf 
59238f7b870SPatrick McHardy static inline size_t if_nlmsg_size(const struct net_device *dev)
593339bf98fSThomas Graf {
594339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
595339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
596339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
597339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rtnl_link_ifmap))
598339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rtnl_link_stats))
599339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
600339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
601339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_TXQLEN */
602339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_WEIGHT */
603339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MTU */
604339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_LINK */
605339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MASTER */
606339bf98fSThomas Graf 	       + nla_total_size(1) /* IFLA_OPERSTATE */
60738f7b870SPatrick McHardy 	       + nla_total_size(1) /* IFLA_LINKMODE */
60838f7b870SPatrick McHardy 	       + rtnl_link_get_size(dev); /* IFLA_LINKINFO */
609339bf98fSThomas Graf }
610339bf98fSThomas Graf 
611b60c5115SThomas Graf static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
612575c3e2aSPatrick McHardy 			    int type, u32 pid, u32 seq, u32 change,
613575c3e2aSPatrick McHardy 			    unsigned int flags)
614b60c5115SThomas Graf {
615b60c5115SThomas Graf 	struct ifinfomsg *ifm;
6161da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
6171da177e4SLinus Torvalds 
618b60c5115SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
619b60c5115SThomas Graf 	if (nlh == NULL)
62026932566SPatrick McHardy 		return -EMSGSIZE;
6211da177e4SLinus Torvalds 
622b60c5115SThomas Graf 	ifm = nlmsg_data(nlh);
623b60c5115SThomas Graf 	ifm->ifi_family = AF_UNSPEC;
624b60c5115SThomas Graf 	ifm->__ifi_pad = 0;
625b60c5115SThomas Graf 	ifm->ifi_type = dev->type;
626b60c5115SThomas Graf 	ifm->ifi_index = dev->ifindex;
627b60c5115SThomas Graf 	ifm->ifi_flags = dev_get_flags(dev);
628b60c5115SThomas Graf 	ifm->ifi_change = change;
6291da177e4SLinus Torvalds 
630b60c5115SThomas Graf 	NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
631b60c5115SThomas Graf 	NLA_PUT_U32(skb, IFLA_TXQLEN, dev->tx_queue_len);
632b60c5115SThomas Graf 	NLA_PUT_U32(skb, IFLA_WEIGHT, dev->weight);
633b60c5115SThomas Graf 	NLA_PUT_U8(skb, IFLA_OPERSTATE,
634b60c5115SThomas Graf 		   netif_running(dev) ? dev->operstate : IF_OPER_DOWN);
635b60c5115SThomas Graf 	NLA_PUT_U8(skb, IFLA_LINKMODE, dev->link_mode);
636b60c5115SThomas Graf 	NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
6371da177e4SLinus Torvalds 
638b60c5115SThomas Graf 	if (dev->ifindex != dev->iflink)
639b60c5115SThomas Graf 		NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
6401da177e4SLinus Torvalds 
641b60c5115SThomas Graf 	if (dev->master)
642b60c5115SThomas Graf 		NLA_PUT_U32(skb, IFLA_MASTER, dev->master->ifindex);
643b60c5115SThomas Graf 
644b60c5115SThomas Graf 	if (dev->qdisc_sleeping)
645b60c5115SThomas Graf 		NLA_PUT_STRING(skb, IFLA_QDISC, dev->qdisc_sleeping->ops->id);
646b00055aaSStefan Rompf 
647b00055aaSStefan Rompf 	if (1) {
6481da177e4SLinus Torvalds 		struct rtnl_link_ifmap map = {
6491da177e4SLinus Torvalds 			.mem_start   = dev->mem_start,
6501da177e4SLinus Torvalds 			.mem_end     = dev->mem_end,
6511da177e4SLinus Torvalds 			.base_addr   = dev->base_addr,
6521da177e4SLinus Torvalds 			.irq         = dev->irq,
6531da177e4SLinus Torvalds 			.dma         = dev->dma,
6541da177e4SLinus Torvalds 			.port        = dev->if_port,
6551da177e4SLinus Torvalds 		};
656b60c5115SThomas Graf 		NLA_PUT(skb, IFLA_MAP, sizeof(map), &map);
6571da177e4SLinus Torvalds 	}
6581da177e4SLinus Torvalds 
6591da177e4SLinus Torvalds 	if (dev->addr_len) {
660b60c5115SThomas Graf 		NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
661b60c5115SThomas Graf 		NLA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast);
6621da177e4SLinus Torvalds 	}
6631da177e4SLinus Torvalds 
6641da177e4SLinus Torvalds 	if (dev->get_stats) {
665b60c5115SThomas Graf 		struct net_device_stats *stats = dev->get_stats(dev);
6661da177e4SLinus Torvalds 		if (stats) {
667b60c5115SThomas Graf 			struct nlattr *attr;
6681da177e4SLinus Torvalds 
669b60c5115SThomas Graf 			attr = nla_reserve(skb, IFLA_STATS,
670b60c5115SThomas Graf 					   sizeof(struct rtnl_link_stats));
671b60c5115SThomas Graf 			if (attr == NULL)
672b60c5115SThomas Graf 				goto nla_put_failure;
673b60c5115SThomas Graf 
674b60c5115SThomas Graf 			copy_rtnl_link_stats(nla_data(attr), stats);
6751da177e4SLinus Torvalds 		}
6761da177e4SLinus Torvalds 	}
6771da177e4SLinus Torvalds 
67838f7b870SPatrick McHardy 	if (dev->rtnl_link_ops) {
67938f7b870SPatrick McHardy 		if (rtnl_link_fill(skb, dev) < 0)
68038f7b870SPatrick McHardy 			goto nla_put_failure;
68138f7b870SPatrick McHardy 	}
68238f7b870SPatrick McHardy 
683b60c5115SThomas Graf 	return nlmsg_end(skb, nlh);
684b60c5115SThomas Graf 
685b60c5115SThomas Graf nla_put_failure:
68626932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
68726932566SPatrick McHardy 	return -EMSGSIZE;
6881da177e4SLinus Torvalds }
6891da177e4SLinus Torvalds 
690b60c5115SThomas Graf static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
6911da177e4SLinus Torvalds {
6921da177e4SLinus Torvalds 	int idx;
6931da177e4SLinus Torvalds 	int s_idx = cb->args[0];
6941da177e4SLinus Torvalds 	struct net_device *dev;
6951da177e4SLinus Torvalds 
6967562f876SPavel Emelianov 	idx = 0;
6977562f876SPavel Emelianov 	for_each_netdev(dev) {
6981da177e4SLinus Torvalds 		if (idx < s_idx)
6997562f876SPavel Emelianov 			goto cont;
700575c3e2aSPatrick McHardy 		if (rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
701b6544c0bSJamal Hadi Salim 				     NETLINK_CB(cb->skb).pid,
702b60c5115SThomas Graf 				     cb->nlh->nlmsg_seq, 0, NLM_F_MULTI) <= 0)
7031da177e4SLinus Torvalds 			break;
7047562f876SPavel Emelianov cont:
7057562f876SPavel Emelianov 		idx++;
7061da177e4SLinus Torvalds 	}
7071da177e4SLinus Torvalds 	cb->args[0] = idx;
7081da177e4SLinus Torvalds 
7091da177e4SLinus Torvalds 	return skb->len;
7101da177e4SLinus Torvalds }
7111da177e4SLinus Torvalds 
712ef7c79edSPatrick McHardy static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
7135176f91eSThomas Graf 	[IFLA_IFNAME]		= { .type = NLA_STRING, .len = IFNAMSIZ-1 },
71438f7b870SPatrick McHardy 	[IFLA_ADDRESS]		= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
71538f7b870SPatrick McHardy 	[IFLA_BROADCAST]	= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
7165176f91eSThomas Graf 	[IFLA_MAP]		= { .len = sizeof(struct rtnl_link_ifmap) },
717da5e0494SThomas Graf 	[IFLA_MTU]		= { .type = NLA_U32 },
718da5e0494SThomas Graf 	[IFLA_TXQLEN]		= { .type = NLA_U32 },
719da5e0494SThomas Graf 	[IFLA_WEIGHT]		= { .type = NLA_U32 },
720da5e0494SThomas Graf 	[IFLA_OPERSTATE]	= { .type = NLA_U8 },
721da5e0494SThomas Graf 	[IFLA_LINKMODE]		= { .type = NLA_U8 },
722da5e0494SThomas Graf };
7231da177e4SLinus Torvalds 
72438f7b870SPatrick McHardy static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
72538f7b870SPatrick McHardy 	[IFLA_INFO_KIND]	= { .type = NLA_STRING },
72638f7b870SPatrick McHardy 	[IFLA_INFO_DATA]	= { .type = NLA_NESTED },
72738f7b870SPatrick McHardy };
72838f7b870SPatrick McHardy 
7290157f60cSPatrick McHardy static int do_setlink(struct net_device *dev, struct ifinfomsg *ifm,
73038f7b870SPatrick McHardy 		      struct nlattr **tb, char *ifname, int modified)
7310157f60cSPatrick McHardy {
73238f7b870SPatrick McHardy 	int send_addr_notify = 0;
7330157f60cSPatrick McHardy 	int err;
7340157f60cSPatrick McHardy 
7350157f60cSPatrick McHardy 	if (tb[IFLA_MAP]) {
7360157f60cSPatrick McHardy 		struct rtnl_link_ifmap *u_map;
7370157f60cSPatrick McHardy 		struct ifmap k_map;
7380157f60cSPatrick McHardy 
7390157f60cSPatrick McHardy 		if (!dev->set_config) {
7400157f60cSPatrick McHardy 			err = -EOPNOTSUPP;
7410157f60cSPatrick McHardy 			goto errout;
7420157f60cSPatrick McHardy 		}
7430157f60cSPatrick McHardy 
7440157f60cSPatrick McHardy 		if (!netif_device_present(dev)) {
7450157f60cSPatrick McHardy 			err = -ENODEV;
7460157f60cSPatrick McHardy 			goto errout;
7470157f60cSPatrick McHardy 		}
7480157f60cSPatrick McHardy 
7490157f60cSPatrick McHardy 		u_map = nla_data(tb[IFLA_MAP]);
7500157f60cSPatrick McHardy 		k_map.mem_start = (unsigned long) u_map->mem_start;
7510157f60cSPatrick McHardy 		k_map.mem_end = (unsigned long) u_map->mem_end;
7520157f60cSPatrick McHardy 		k_map.base_addr = (unsigned short) u_map->base_addr;
7530157f60cSPatrick McHardy 		k_map.irq = (unsigned char) u_map->irq;
7540157f60cSPatrick McHardy 		k_map.dma = (unsigned char) u_map->dma;
7550157f60cSPatrick McHardy 		k_map.port = (unsigned char) u_map->port;
7560157f60cSPatrick McHardy 
7570157f60cSPatrick McHardy 		err = dev->set_config(dev, &k_map);
7580157f60cSPatrick McHardy 		if (err < 0)
7590157f60cSPatrick McHardy 			goto errout;
7600157f60cSPatrick McHardy 
7610157f60cSPatrick McHardy 		modified = 1;
7620157f60cSPatrick McHardy 	}
7630157f60cSPatrick McHardy 
7640157f60cSPatrick McHardy 	if (tb[IFLA_ADDRESS]) {
7650157f60cSPatrick McHardy 		struct sockaddr *sa;
7660157f60cSPatrick McHardy 		int len;
7670157f60cSPatrick McHardy 
7680157f60cSPatrick McHardy 		if (!dev->set_mac_address) {
7690157f60cSPatrick McHardy 			err = -EOPNOTSUPP;
7700157f60cSPatrick McHardy 			goto errout;
7710157f60cSPatrick McHardy 		}
7720157f60cSPatrick McHardy 
7730157f60cSPatrick McHardy 		if (!netif_device_present(dev)) {
7740157f60cSPatrick McHardy 			err = -ENODEV;
7750157f60cSPatrick McHardy 			goto errout;
7760157f60cSPatrick McHardy 		}
7770157f60cSPatrick McHardy 
7780157f60cSPatrick McHardy 		len = sizeof(sa_family_t) + dev->addr_len;
7790157f60cSPatrick McHardy 		sa = kmalloc(len, GFP_KERNEL);
7800157f60cSPatrick McHardy 		if (!sa) {
7810157f60cSPatrick McHardy 			err = -ENOMEM;
7820157f60cSPatrick McHardy 			goto errout;
7830157f60cSPatrick McHardy 		}
7840157f60cSPatrick McHardy 		sa->sa_family = dev->type;
7850157f60cSPatrick McHardy 		memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
7860157f60cSPatrick McHardy 		       dev->addr_len);
7870157f60cSPatrick McHardy 		err = dev->set_mac_address(dev, sa);
7880157f60cSPatrick McHardy 		kfree(sa);
7890157f60cSPatrick McHardy 		if (err)
7900157f60cSPatrick McHardy 			goto errout;
7910157f60cSPatrick McHardy 		send_addr_notify = 1;
7920157f60cSPatrick McHardy 		modified = 1;
7930157f60cSPatrick McHardy 	}
7940157f60cSPatrick McHardy 
7950157f60cSPatrick McHardy 	if (tb[IFLA_MTU]) {
7960157f60cSPatrick McHardy 		err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
7970157f60cSPatrick McHardy 		if (err < 0)
7980157f60cSPatrick McHardy 			goto errout;
7990157f60cSPatrick McHardy 		modified = 1;
8000157f60cSPatrick McHardy 	}
8010157f60cSPatrick McHardy 
8020157f60cSPatrick McHardy 	/*
8030157f60cSPatrick McHardy 	 * Interface selected by interface index but interface
8040157f60cSPatrick McHardy 	 * name provided implies that a name change has been
8050157f60cSPatrick McHardy 	 * requested.
8060157f60cSPatrick McHardy 	 */
8070157f60cSPatrick McHardy 	if (ifm->ifi_index > 0 && ifname[0]) {
8080157f60cSPatrick McHardy 		err = dev_change_name(dev, ifname);
8090157f60cSPatrick McHardy 		if (err < 0)
8100157f60cSPatrick McHardy 			goto errout;
8110157f60cSPatrick McHardy 		modified = 1;
8120157f60cSPatrick McHardy 	}
8130157f60cSPatrick McHardy 
8140157f60cSPatrick McHardy 	if (tb[IFLA_BROADCAST]) {
8150157f60cSPatrick McHardy 		nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
8160157f60cSPatrick McHardy 		send_addr_notify = 1;
8170157f60cSPatrick McHardy 	}
8180157f60cSPatrick McHardy 
8190157f60cSPatrick McHardy 	if (ifm->ifi_flags || ifm->ifi_change) {
8200157f60cSPatrick McHardy 		unsigned int flags = ifm->ifi_flags;
8210157f60cSPatrick McHardy 
8220157f60cSPatrick McHardy 		/* bugwards compatibility: ifi_change == 0 is treated as ~0 */
8230157f60cSPatrick McHardy 		if (ifm->ifi_change)
8240157f60cSPatrick McHardy 			flags = (flags & ifm->ifi_change) |
8250157f60cSPatrick McHardy 				(dev->flags & ~ifm->ifi_change);
8260157f60cSPatrick McHardy 		dev_change_flags(dev, flags);
8270157f60cSPatrick McHardy 	}
8280157f60cSPatrick McHardy 
8290157f60cSPatrick McHardy 	if (tb[IFLA_TXQLEN])
8300157f60cSPatrick McHardy 		dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
8310157f60cSPatrick McHardy 
8320157f60cSPatrick McHardy 	if (tb[IFLA_WEIGHT])
8330157f60cSPatrick McHardy 		dev->weight = nla_get_u32(tb[IFLA_WEIGHT]);
8340157f60cSPatrick McHardy 
8350157f60cSPatrick McHardy 	if (tb[IFLA_OPERSTATE])
8360157f60cSPatrick McHardy 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
8370157f60cSPatrick McHardy 
8380157f60cSPatrick McHardy 	if (tb[IFLA_LINKMODE]) {
8390157f60cSPatrick McHardy 		write_lock_bh(&dev_base_lock);
8400157f60cSPatrick McHardy 		dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
8410157f60cSPatrick McHardy 		write_unlock_bh(&dev_base_lock);
8420157f60cSPatrick McHardy 	}
8430157f60cSPatrick McHardy 
8440157f60cSPatrick McHardy 	err = 0;
8450157f60cSPatrick McHardy 
8460157f60cSPatrick McHardy errout:
8470157f60cSPatrick McHardy 	if (err < 0 && modified && net_ratelimit())
8480157f60cSPatrick McHardy 		printk(KERN_WARNING "A link change request failed with "
8490157f60cSPatrick McHardy 		       "some changes comitted already. Interface %s may "
8500157f60cSPatrick McHardy 		       "have been left with an inconsistent configuration, "
8510157f60cSPatrick McHardy 		       "please check.\n", dev->name);
8520157f60cSPatrick McHardy 
8530157f60cSPatrick McHardy 	if (send_addr_notify)
8540157f60cSPatrick McHardy 		call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
8550157f60cSPatrick McHardy 	return err;
8560157f60cSPatrick McHardy }
8570157f60cSPatrick McHardy 
858da5e0494SThomas Graf static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
859da5e0494SThomas Graf {
860da5e0494SThomas Graf 	struct ifinfomsg *ifm;
861da5e0494SThomas Graf 	struct net_device *dev;
8620157f60cSPatrick McHardy 	int err;
863da5e0494SThomas Graf 	struct nlattr *tb[IFLA_MAX+1];
8641da177e4SLinus Torvalds 	char ifname[IFNAMSIZ];
8651da177e4SLinus Torvalds 
866da5e0494SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
867da5e0494SThomas Graf 	if (err < 0)
868da5e0494SThomas Graf 		goto errout;
8691da177e4SLinus Torvalds 
8705176f91eSThomas Graf 	if (tb[IFLA_IFNAME])
8715176f91eSThomas Graf 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
87278e5b891SPatrick McHardy 	else
87378e5b891SPatrick McHardy 		ifname[0] = '\0';
8741da177e4SLinus Torvalds 
8751da177e4SLinus Torvalds 	err = -EINVAL;
876da5e0494SThomas Graf 	ifm = nlmsg_data(nlh);
87751055be8SPatrick McHardy 	if (ifm->ifi_index > 0)
878da5e0494SThomas Graf 		dev = dev_get_by_index(ifm->ifi_index);
879da5e0494SThomas Graf 	else if (tb[IFLA_IFNAME])
880da5e0494SThomas Graf 		dev = dev_get_by_name(ifname);
881da5e0494SThomas Graf 	else
882da5e0494SThomas Graf 		goto errout;
8831da177e4SLinus Torvalds 
884da5e0494SThomas Graf 	if (dev == NULL) {
885da5e0494SThomas Graf 		err = -ENODEV;
886da5e0494SThomas Graf 		goto errout;
887da5e0494SThomas Graf 	}
8881da177e4SLinus Torvalds 
889da5e0494SThomas Graf 	if (tb[IFLA_ADDRESS] &&
890da5e0494SThomas Graf 	    nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
891da5e0494SThomas Graf 		goto errout_dev;
892da5e0494SThomas Graf 
893da5e0494SThomas Graf 	if (tb[IFLA_BROADCAST] &&
894da5e0494SThomas Graf 	    nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
895da5e0494SThomas Graf 		goto errout_dev;
896da5e0494SThomas Graf 
89738f7b870SPatrick McHardy 	err = do_setlink(dev, ifm, tb, ifname, 0);
898da5e0494SThomas Graf errout_dev:
8991da177e4SLinus Torvalds 	dev_put(dev);
900da5e0494SThomas Graf errout:
9011da177e4SLinus Torvalds 	return err;
9021da177e4SLinus Torvalds }
9031da177e4SLinus Torvalds 
90438f7b870SPatrick McHardy static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
90538f7b870SPatrick McHardy {
90638f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops;
90738f7b870SPatrick McHardy 	struct net_device *dev;
90838f7b870SPatrick McHardy 	struct ifinfomsg *ifm;
90938f7b870SPatrick McHardy 	char ifname[IFNAMSIZ];
91038f7b870SPatrick McHardy 	struct nlattr *tb[IFLA_MAX+1];
91138f7b870SPatrick McHardy 	int err;
91238f7b870SPatrick McHardy 
91338f7b870SPatrick McHardy 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
91438f7b870SPatrick McHardy 	if (err < 0)
91538f7b870SPatrick McHardy 		return err;
91638f7b870SPatrick McHardy 
91738f7b870SPatrick McHardy 	if (tb[IFLA_IFNAME])
91838f7b870SPatrick McHardy 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
91938f7b870SPatrick McHardy 
92038f7b870SPatrick McHardy 	ifm = nlmsg_data(nlh);
92138f7b870SPatrick McHardy 	if (ifm->ifi_index > 0)
92238f7b870SPatrick McHardy 		dev = __dev_get_by_index(ifm->ifi_index);
92338f7b870SPatrick McHardy 	else if (tb[IFLA_IFNAME])
92438f7b870SPatrick McHardy 		dev = __dev_get_by_name(ifname);
92538f7b870SPatrick McHardy 	else
92638f7b870SPatrick McHardy 		return -EINVAL;
92738f7b870SPatrick McHardy 
92838f7b870SPatrick McHardy 	if (!dev)
92938f7b870SPatrick McHardy 		return -ENODEV;
93038f7b870SPatrick McHardy 
93138f7b870SPatrick McHardy 	ops = dev->rtnl_link_ops;
93238f7b870SPatrick McHardy 	if (!ops)
93338f7b870SPatrick McHardy 		return -EOPNOTSUPP;
93438f7b870SPatrick McHardy 
93538f7b870SPatrick McHardy 	ops->dellink(dev);
93638f7b870SPatrick McHardy 	return 0;
93738f7b870SPatrick McHardy }
93838f7b870SPatrick McHardy 
93938f7b870SPatrick McHardy static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
94038f7b870SPatrick McHardy {
94138f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops;
94238f7b870SPatrick McHardy 	struct net_device *dev;
94338f7b870SPatrick McHardy 	struct ifinfomsg *ifm;
94438f7b870SPatrick McHardy 	char kind[MODULE_NAME_LEN];
94538f7b870SPatrick McHardy 	char ifname[IFNAMSIZ];
94638f7b870SPatrick McHardy 	struct nlattr *tb[IFLA_MAX+1];
94738f7b870SPatrick McHardy 	struct nlattr *linkinfo[IFLA_INFO_MAX+1];
94838f7b870SPatrick McHardy 	int err;
94938f7b870SPatrick McHardy 
95038f7b870SPatrick McHardy replay:
95138f7b870SPatrick McHardy 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
95238f7b870SPatrick McHardy 	if (err < 0)
95338f7b870SPatrick McHardy 		return err;
95438f7b870SPatrick McHardy 
95538f7b870SPatrick McHardy 	if (tb[IFLA_IFNAME])
95638f7b870SPatrick McHardy 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
95738f7b870SPatrick McHardy 	else
95838f7b870SPatrick McHardy 		ifname[0] = '\0';
95938f7b870SPatrick McHardy 
96038f7b870SPatrick McHardy 	ifm = nlmsg_data(nlh);
96138f7b870SPatrick McHardy 	if (ifm->ifi_index > 0)
96238f7b870SPatrick McHardy 		dev = __dev_get_by_index(ifm->ifi_index);
96338f7b870SPatrick McHardy 	else if (ifname[0])
96438f7b870SPatrick McHardy 		dev = __dev_get_by_name(ifname);
96538f7b870SPatrick McHardy 	else
96638f7b870SPatrick McHardy 		dev = NULL;
96738f7b870SPatrick McHardy 
96838f7b870SPatrick McHardy 	if (tb[IFLA_LINKINFO]) {
96938f7b870SPatrick McHardy 		err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
97038f7b870SPatrick McHardy 				       tb[IFLA_LINKINFO], ifla_info_policy);
97138f7b870SPatrick McHardy 		if (err < 0)
97238f7b870SPatrick McHardy 			return err;
97338f7b870SPatrick McHardy 	} else
97438f7b870SPatrick McHardy 		memset(linkinfo, 0, sizeof(linkinfo));
97538f7b870SPatrick McHardy 
97638f7b870SPatrick McHardy 	if (linkinfo[IFLA_INFO_KIND]) {
97738f7b870SPatrick McHardy 		nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
97838f7b870SPatrick McHardy 		ops = rtnl_link_ops_get(kind);
97938f7b870SPatrick McHardy 	} else {
98038f7b870SPatrick McHardy 		kind[0] = '\0';
98138f7b870SPatrick McHardy 		ops = NULL;
98238f7b870SPatrick McHardy 	}
98338f7b870SPatrick McHardy 
98438f7b870SPatrick McHardy 	if (1) {
98538f7b870SPatrick McHardy 		struct nlattr *attr[ops ? ops->maxtype + 1 : 0], **data = NULL;
98638f7b870SPatrick McHardy 
98738f7b870SPatrick McHardy 		if (ops) {
98838f7b870SPatrick McHardy 			if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
98938f7b870SPatrick McHardy 				err = nla_parse_nested(attr, ops->maxtype,
99038f7b870SPatrick McHardy 						       linkinfo[IFLA_INFO_DATA],
99138f7b870SPatrick McHardy 						       ops->policy);
99238f7b870SPatrick McHardy 				if (err < 0)
99338f7b870SPatrick McHardy 					return err;
99438f7b870SPatrick McHardy 				data = attr;
99538f7b870SPatrick McHardy 			}
99638f7b870SPatrick McHardy 			if (ops->validate) {
99738f7b870SPatrick McHardy 				err = ops->validate(tb, data);
99838f7b870SPatrick McHardy 				if (err < 0)
99938f7b870SPatrick McHardy 					return err;
100038f7b870SPatrick McHardy 			}
100138f7b870SPatrick McHardy 		}
100238f7b870SPatrick McHardy 
100338f7b870SPatrick McHardy 		if (dev) {
100438f7b870SPatrick McHardy 			int modified = 0;
100538f7b870SPatrick McHardy 
100638f7b870SPatrick McHardy 			if (nlh->nlmsg_flags & NLM_F_EXCL)
100738f7b870SPatrick McHardy 				return -EEXIST;
100838f7b870SPatrick McHardy 			if (nlh->nlmsg_flags & NLM_F_REPLACE)
100938f7b870SPatrick McHardy 				return -EOPNOTSUPP;
101038f7b870SPatrick McHardy 
101138f7b870SPatrick McHardy 			if (linkinfo[IFLA_INFO_DATA]) {
101238f7b870SPatrick McHardy 				if (!ops || ops != dev->rtnl_link_ops ||
101338f7b870SPatrick McHardy 				    !ops->changelink)
101438f7b870SPatrick McHardy 					return -EOPNOTSUPP;
101538f7b870SPatrick McHardy 
101638f7b870SPatrick McHardy 				err = ops->changelink(dev, tb, data);
101738f7b870SPatrick McHardy 				if (err < 0)
101838f7b870SPatrick McHardy 					return err;
101938f7b870SPatrick McHardy 				modified = 1;
102038f7b870SPatrick McHardy 			}
102138f7b870SPatrick McHardy 
102238f7b870SPatrick McHardy 			return do_setlink(dev, ifm, tb, ifname, modified);
102338f7b870SPatrick McHardy 		}
102438f7b870SPatrick McHardy 
102538f7b870SPatrick McHardy 		if (!(nlh->nlmsg_flags & NLM_F_CREATE))
102638f7b870SPatrick McHardy 			return -ENODEV;
102738f7b870SPatrick McHardy 
102838f7b870SPatrick McHardy 		if (ifm->ifi_index || ifm->ifi_flags || ifm->ifi_change)
102938f7b870SPatrick McHardy 			return -EOPNOTSUPP;
103038f7b870SPatrick McHardy 		if (tb[IFLA_ADDRESS] || tb[IFLA_BROADCAST] || tb[IFLA_MAP] ||
103138f7b870SPatrick McHardy 		    tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
103238f7b870SPatrick McHardy 			return -EOPNOTSUPP;
103338f7b870SPatrick McHardy 
103438f7b870SPatrick McHardy 		if (!ops) {
103538f7b870SPatrick McHardy #ifdef CONFIG_KMOD
103638f7b870SPatrick McHardy 			if (kind[0]) {
103738f7b870SPatrick McHardy 				__rtnl_unlock();
103838f7b870SPatrick McHardy 				request_module("rtnl-link-%s", kind);
103938f7b870SPatrick McHardy 				rtnl_lock();
104038f7b870SPatrick McHardy 				ops = rtnl_link_ops_get(kind);
104138f7b870SPatrick McHardy 				if (ops)
104238f7b870SPatrick McHardy 					goto replay;
104338f7b870SPatrick McHardy 			}
104438f7b870SPatrick McHardy #endif
104538f7b870SPatrick McHardy 			return -EOPNOTSUPP;
104638f7b870SPatrick McHardy 		}
104738f7b870SPatrick McHardy 
104838f7b870SPatrick McHardy 		if (!ifname[0])
104938f7b870SPatrick McHardy 			snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
105038f7b870SPatrick McHardy 		dev = alloc_netdev(ops->priv_size, ifname, ops->setup);
105138f7b870SPatrick McHardy 		if (!dev)
105238f7b870SPatrick McHardy 			return -ENOMEM;
105338f7b870SPatrick McHardy 
105438f7b870SPatrick McHardy 		if (strchr(dev->name, '%')) {
105538f7b870SPatrick McHardy 			err = dev_alloc_name(dev, dev->name);
105638f7b870SPatrick McHardy 			if (err < 0)
105738f7b870SPatrick McHardy 				goto err_free;
105838f7b870SPatrick McHardy 		}
105938f7b870SPatrick McHardy 		dev->rtnl_link_ops = ops;
106038f7b870SPatrick McHardy 
106138f7b870SPatrick McHardy 		if (tb[IFLA_MTU])
106238f7b870SPatrick McHardy 			dev->mtu = nla_get_u32(tb[IFLA_MTU]);
106338f7b870SPatrick McHardy 		if (tb[IFLA_TXQLEN])
106438f7b870SPatrick McHardy 			dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
106538f7b870SPatrick McHardy 		if (tb[IFLA_WEIGHT])
106638f7b870SPatrick McHardy 			dev->weight = nla_get_u32(tb[IFLA_WEIGHT]);
106738f7b870SPatrick McHardy 		if (tb[IFLA_OPERSTATE])
106838f7b870SPatrick McHardy 			set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
106938f7b870SPatrick McHardy 		if (tb[IFLA_LINKMODE])
107038f7b870SPatrick McHardy 			dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
107138f7b870SPatrick McHardy 
107238f7b870SPatrick McHardy 		err = ops->newlink(dev, tb, data);
107338f7b870SPatrick McHardy err_free:
107438f7b870SPatrick McHardy 		if (err < 0)
107538f7b870SPatrick McHardy 			free_netdev(dev);
107638f7b870SPatrick McHardy 		return err;
107738f7b870SPatrick McHardy 	}
107838f7b870SPatrick McHardy }
107938f7b870SPatrick McHardy 
1080b60c5115SThomas Graf static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
1081711e2c33SJean Tourrilhes {
1082b60c5115SThomas Graf 	struct ifinfomsg *ifm;
1083b60c5115SThomas Graf 	struct nlattr *tb[IFLA_MAX+1];
1084b60c5115SThomas Graf 	struct net_device *dev = NULL;
1085b60c5115SThomas Graf 	struct sk_buff *nskb;
1086339bf98fSThomas Graf 	int err;
1087711e2c33SJean Tourrilhes 
1088b60c5115SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1089b60c5115SThomas Graf 	if (err < 0)
10909918f230SEric Sesterhenn 		return err;
1091b60c5115SThomas Graf 
1092b60c5115SThomas Graf 	ifm = nlmsg_data(nlh);
109351055be8SPatrick McHardy 	if (ifm->ifi_index > 0) {
1094711e2c33SJean Tourrilhes 		dev = dev_get_by_index(ifm->ifi_index);
1095b60c5115SThomas Graf 		if (dev == NULL)
1096711e2c33SJean Tourrilhes 			return -ENODEV;
1097b60c5115SThomas Graf 	} else
1098b60c5115SThomas Graf 		return -EINVAL;
1099b60c5115SThomas Graf 
110038f7b870SPatrick McHardy 	nskb = nlmsg_new(if_nlmsg_size(dev), GFP_KERNEL);
1101b60c5115SThomas Graf 	if (nskb == NULL) {
1102b60c5115SThomas Graf 		err = -ENOBUFS;
1103b60c5115SThomas Graf 		goto errout;
1104b60c5115SThomas Graf 	}
1105711e2c33SJean Tourrilhes 
1106575c3e2aSPatrick McHardy 	err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).pid,
1107575c3e2aSPatrick McHardy 			       nlh->nlmsg_seq, 0, 0);
110826932566SPatrick McHardy 	if (err < 0) {
110926932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in if_nlmsg_size */
111026932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
111126932566SPatrick McHardy 		kfree_skb(nskb);
111226932566SPatrick McHardy 		goto errout;
111326932566SPatrick McHardy 	}
1114b974179aSPatrick McHardy 	err = rtnl_unicast(nskb, NETLINK_CB(skb).pid);
1115b60c5115SThomas Graf errout:
1116711e2c33SJean Tourrilhes 	dev_put(dev);
1117b60c5115SThomas Graf 
1118711e2c33SJean Tourrilhes 	return err;
1119711e2c33SJean Tourrilhes }
1120711e2c33SJean Tourrilhes 
112142bad1daSAdrian Bunk static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
11221da177e4SLinus Torvalds {
11231da177e4SLinus Torvalds 	int idx;
11241da177e4SLinus Torvalds 	int s_idx = cb->family;
11251da177e4SLinus Torvalds 
11261da177e4SLinus Torvalds 	if (s_idx == 0)
11271da177e4SLinus Torvalds 		s_idx = 1;
11281da177e4SLinus Torvalds 	for (idx=1; idx<NPROTO; idx++) {
11291da177e4SLinus Torvalds 		int type = cb->nlh->nlmsg_type-RTM_BASE;
11301da177e4SLinus Torvalds 		if (idx < s_idx || idx == PF_PACKET)
11311da177e4SLinus Torvalds 			continue;
1132e2849863SThomas Graf 		if (rtnl_msg_handlers[idx] == NULL ||
1133e2849863SThomas Graf 		    rtnl_msg_handlers[idx][type].dumpit == NULL)
11341da177e4SLinus Torvalds 			continue;
11351da177e4SLinus Torvalds 		if (idx > s_idx)
11361da177e4SLinus Torvalds 			memset(&cb->args[0], 0, sizeof(cb->args));
1137e2849863SThomas Graf 		if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
11381da177e4SLinus Torvalds 			break;
11391da177e4SLinus Torvalds 	}
11401da177e4SLinus Torvalds 	cb->family = idx;
11411da177e4SLinus Torvalds 
11421da177e4SLinus Torvalds 	return skb->len;
11431da177e4SLinus Torvalds }
11441da177e4SLinus Torvalds 
11451da177e4SLinus Torvalds void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change)
11461da177e4SLinus Torvalds {
11471da177e4SLinus Torvalds 	struct sk_buff *skb;
11480ec6d3f4SThomas Graf 	int err = -ENOBUFS;
11491da177e4SLinus Torvalds 
115038f7b870SPatrick McHardy 	skb = nlmsg_new(if_nlmsg_size(dev), GFP_KERNEL);
11510ec6d3f4SThomas Graf 	if (skb == NULL)
11520ec6d3f4SThomas Graf 		goto errout;
11531da177e4SLinus Torvalds 
1154575c3e2aSPatrick McHardy 	err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0);
115526932566SPatrick McHardy 	if (err < 0) {
115626932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in if_nlmsg_size() */
115726932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
115826932566SPatrick McHardy 		kfree_skb(skb);
115926932566SPatrick McHardy 		goto errout;
116026932566SPatrick McHardy 	}
11610ec6d3f4SThomas Graf 	err = rtnl_notify(skb, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
11620ec6d3f4SThomas Graf errout:
11630ec6d3f4SThomas Graf 	if (err < 0)
11640ec6d3f4SThomas Graf 		rtnl_set_sk_err(RTNLGRP_LINK, err);
11651da177e4SLinus Torvalds }
11661da177e4SLinus Torvalds 
11671da177e4SLinus Torvalds /* Protected by RTNL sempahore.  */
11681da177e4SLinus Torvalds static struct rtattr **rta_buf;
11691da177e4SLinus Torvalds static int rtattr_max;
11701da177e4SLinus Torvalds 
11711da177e4SLinus Torvalds /* Process one rtnetlink message. */
11721da177e4SLinus Torvalds 
11731d00a4ebSThomas Graf static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
11741da177e4SLinus Torvalds {
1175e2849863SThomas Graf 	rtnl_doit_func doit;
11761da177e4SLinus Torvalds 	int sz_idx, kind;
11771da177e4SLinus Torvalds 	int min_len;
11781da177e4SLinus Torvalds 	int family;
11791da177e4SLinus Torvalds 	int type;
11801c2d670fSPatrick McHardy 	int err;
11811da177e4SLinus Torvalds 
11821da177e4SLinus Torvalds 	type = nlh->nlmsg_type;
11831da177e4SLinus Torvalds 	if (type > RTM_MAX)
1184038890feSThomas Graf 		return -EOPNOTSUPP;
11851da177e4SLinus Torvalds 
11861da177e4SLinus Torvalds 	type -= RTM_BASE;
11871da177e4SLinus Torvalds 
11881da177e4SLinus Torvalds 	/* All the messages must have at least 1 byte length */
11891da177e4SLinus Torvalds 	if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg)))
11901da177e4SLinus Torvalds 		return 0;
11911da177e4SLinus Torvalds 
11921da177e4SLinus Torvalds 	family = ((struct rtgenmsg*)NLMSG_DATA(nlh))->rtgen_family;
11931d00a4ebSThomas Graf 	if (family >= NPROTO)
11941d00a4ebSThomas Graf 		return -EAFNOSUPPORT;
11951da177e4SLinus Torvalds 
11961da177e4SLinus Torvalds 	sz_idx = type>>2;
11971da177e4SLinus Torvalds 	kind = type&3;
11981da177e4SLinus Torvalds 
11991d00a4ebSThomas Graf 	if (kind != 2 && security_netlink_recv(skb, CAP_NET_ADMIN))
12001d00a4ebSThomas Graf 		return -EPERM;
12011da177e4SLinus Torvalds 
12021da177e4SLinus Torvalds 	if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
1203e2849863SThomas Graf 		rtnl_dumpit_func dumpit;
12041da177e4SLinus Torvalds 
1205e2849863SThomas Graf 		dumpit = rtnl_get_dumpit(family, type);
1206e2849863SThomas Graf 		if (dumpit == NULL)
1207038890feSThomas Graf 			return -EOPNOTSUPP;
12081da177e4SLinus Torvalds 
12091c2d670fSPatrick McHardy 		__rtnl_unlock();
12101c2d670fSPatrick McHardy 		err = netlink_dump_start(rtnl, skb, nlh, dumpit, NULL);
12111c2d670fSPatrick McHardy 		rtnl_lock();
12121c2d670fSPatrick McHardy 		return err;
12131da177e4SLinus Torvalds 	}
12141da177e4SLinus Torvalds 
12151da177e4SLinus Torvalds 	memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *)));
12161da177e4SLinus Torvalds 
12171da177e4SLinus Torvalds 	min_len = rtm_min[sz_idx];
12181da177e4SLinus Torvalds 	if (nlh->nlmsg_len < min_len)
12191d00a4ebSThomas Graf 		return -EINVAL;
12201da177e4SLinus Torvalds 
12211da177e4SLinus Torvalds 	if (nlh->nlmsg_len > min_len) {
12221da177e4SLinus Torvalds 		int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
12231da177e4SLinus Torvalds 		struct rtattr *attr = (void*)nlh + NLMSG_ALIGN(min_len);
12241da177e4SLinus Torvalds 
12251da177e4SLinus Torvalds 		while (RTA_OK(attr, attrlen)) {
12261da177e4SLinus Torvalds 			unsigned flavor = attr->rta_type;
12271da177e4SLinus Torvalds 			if (flavor) {
12281da177e4SLinus Torvalds 				if (flavor > rta_max[sz_idx])
12291d00a4ebSThomas Graf 					return -EINVAL;
12301da177e4SLinus Torvalds 				rta_buf[flavor-1] = attr;
12311da177e4SLinus Torvalds 			}
12321da177e4SLinus Torvalds 			attr = RTA_NEXT(attr, attrlen);
12331da177e4SLinus Torvalds 		}
12341da177e4SLinus Torvalds 	}
12351da177e4SLinus Torvalds 
1236e2849863SThomas Graf 	doit = rtnl_get_doit(family, type);
1237e2849863SThomas Graf 	if (doit == NULL)
1238038890feSThomas Graf 		return -EOPNOTSUPP;
12391da177e4SLinus Torvalds 
12401d00a4ebSThomas Graf 	return doit(skb, nlh, (void *)&rta_buf[0]);
12411da177e4SLinus Torvalds }
12421da177e4SLinus Torvalds 
12431da177e4SLinus Torvalds static void rtnetlink_rcv(struct sock *sk, int len)
12441da177e4SLinus Torvalds {
12459ac4a169SThomas Graf 	unsigned int qlen = 0;
12462a0a6ebeSHerbert Xu 
12471da177e4SLinus Torvalds 	do {
12486756ae4bSStephen Hemminger 		mutex_lock(&rtnl_mutex);
12499ac4a169SThomas Graf 		netlink_run_queue(sk, &qlen, &rtnetlink_rcv_msg);
12506756ae4bSStephen Hemminger 		mutex_unlock(&rtnl_mutex);
12511da177e4SLinus Torvalds 
12521da177e4SLinus Torvalds 		netdev_run_todo();
12532a0a6ebeSHerbert Xu 	} while (qlen);
12541da177e4SLinus Torvalds }
12551da177e4SLinus Torvalds 
12561da177e4SLinus Torvalds static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
12571da177e4SLinus Torvalds {
12581da177e4SLinus Torvalds 	struct net_device *dev = ptr;
12591da177e4SLinus Torvalds 	switch (event) {
12601da177e4SLinus Torvalds 	case NETDEV_UNREGISTER:
12611da177e4SLinus Torvalds 		rtmsg_ifinfo(RTM_DELLINK, dev, ~0U);
12621da177e4SLinus Torvalds 		break;
12631da177e4SLinus Torvalds 	case NETDEV_REGISTER:
12641da177e4SLinus Torvalds 		rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
12651da177e4SLinus Torvalds 		break;
12661da177e4SLinus Torvalds 	case NETDEV_UP:
12671da177e4SLinus Torvalds 	case NETDEV_DOWN:
12681da177e4SLinus Torvalds 		rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
12691da177e4SLinus Torvalds 		break;
12701da177e4SLinus Torvalds 	case NETDEV_CHANGE:
12711da177e4SLinus Torvalds 	case NETDEV_GOING_DOWN:
12721da177e4SLinus Torvalds 		break;
12731da177e4SLinus Torvalds 	default:
12741da177e4SLinus Torvalds 		rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
12751da177e4SLinus Torvalds 		break;
12761da177e4SLinus Torvalds 	}
12771da177e4SLinus Torvalds 	return NOTIFY_DONE;
12781da177e4SLinus Torvalds }
12791da177e4SLinus Torvalds 
12801da177e4SLinus Torvalds static struct notifier_block rtnetlink_dev_notifier = {
12811da177e4SLinus Torvalds 	.notifier_call	= rtnetlink_event,
12821da177e4SLinus Torvalds };
12831da177e4SLinus Torvalds 
12841da177e4SLinus Torvalds void __init rtnetlink_init(void)
12851da177e4SLinus Torvalds {
12861da177e4SLinus Torvalds 	int i;
12871da177e4SLinus Torvalds 
12881da177e4SLinus Torvalds 	rtattr_max = 0;
12891da177e4SLinus Torvalds 	for (i = 0; i < ARRAY_SIZE(rta_max); i++)
12901da177e4SLinus Torvalds 		if (rta_max[i] > rtattr_max)
12911da177e4SLinus Torvalds 			rtattr_max = rta_max[i];
12921da177e4SLinus Torvalds 	rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL);
12931da177e4SLinus Torvalds 	if (!rta_buf)
12941da177e4SLinus Torvalds 		panic("rtnetlink_init: cannot allocate rta_buf\n");
12951da177e4SLinus Torvalds 
129606628607SPatrick McHardy 	rtnl = netlink_kernel_create(NETLINK_ROUTE, RTNLGRP_MAX, rtnetlink_rcv,
12971c2d670fSPatrick McHardy 				     &rtnl_mutex, THIS_MODULE);
12981da177e4SLinus Torvalds 	if (rtnl == NULL)
12991da177e4SLinus Torvalds 		panic("rtnetlink_init: cannot initialize rtnetlink\n");
13001da177e4SLinus Torvalds 	netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV);
13011da177e4SLinus Torvalds 	register_netdevice_notifier(&rtnetlink_dev_notifier);
1302340d17fcSThomas Graf 
1303340d17fcSThomas Graf 	rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, rtnl_dump_ifinfo);
1304340d17fcSThomas Graf 	rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL);
130538f7b870SPatrick McHardy 	rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL);
130638f7b870SPatrick McHardy 	rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL);
1307687ad8ccSThomas Graf 
1308687ad8ccSThomas Graf 	rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all);
1309687ad8ccSThomas Graf 	rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all);
13101da177e4SLinus Torvalds }
13111da177e4SLinus Torvalds 
13121da177e4SLinus Torvalds EXPORT_SYMBOL(__rta_fill);
13131da177e4SLinus Torvalds EXPORT_SYMBOL(rtattr_strlcpy);
13141da177e4SLinus Torvalds EXPORT_SYMBOL(rtattr_parse);
1315*afdc3238SPatrick McHardy EXPORT_SYMBOL(rtattr_parse_nested_compat);
13161da177e4SLinus Torvalds EXPORT_SYMBOL(rtnetlink_put_metrics);
13171da177e4SLinus Torvalds EXPORT_SYMBOL(rtnl_lock);
13186756ae4bSStephen Hemminger EXPORT_SYMBOL(rtnl_trylock);
13191da177e4SLinus Torvalds EXPORT_SYMBOL(rtnl_unlock);
13202942e900SThomas Graf EXPORT_SYMBOL(rtnl_unicast);
132197676b6bSThomas Graf EXPORT_SYMBOL(rtnl_notify);
132297676b6bSThomas Graf EXPORT_SYMBOL(rtnl_set_sk_err);
1323