xref: /openbmc/linux/net/core/rtnetlink.c (revision 51055be81c3cb14d0165a7432b787098b817fd35)
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 
10042bad1daSAdrian Bunk static struct rtnl_link *rtnl_msg_handlers[NPROTO];
101e2849863SThomas Graf 
102e2849863SThomas Graf static inline int rtm_msgindex(int msgtype)
103e2849863SThomas Graf {
104e2849863SThomas Graf 	int msgindex = msgtype - RTM_BASE;
105e2849863SThomas Graf 
106e2849863SThomas Graf 	/*
107e2849863SThomas Graf 	 * msgindex < 0 implies someone tried to register a netlink
108e2849863SThomas Graf 	 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
109e2849863SThomas Graf 	 * the message type has not been added to linux/rtnetlink.h
110e2849863SThomas Graf 	 */
111e2849863SThomas Graf 	BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
112e2849863SThomas Graf 
113e2849863SThomas Graf 	return msgindex;
114e2849863SThomas Graf }
115e2849863SThomas Graf 
116e2849863SThomas Graf static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex)
117e2849863SThomas Graf {
118e2849863SThomas Graf 	struct rtnl_link *tab;
119e2849863SThomas Graf 
120e2849863SThomas Graf 	tab = rtnl_msg_handlers[protocol];
12151057f2fSThomas Graf 	if (tab == NULL || tab[msgindex].doit == NULL)
122e2849863SThomas Graf 		tab = rtnl_msg_handlers[PF_UNSPEC];
123e2849863SThomas Graf 
12451057f2fSThomas Graf 	return tab ? tab[msgindex].doit : NULL;
125e2849863SThomas Graf }
126e2849863SThomas Graf 
127e2849863SThomas Graf static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex)
128e2849863SThomas Graf {
129e2849863SThomas Graf 	struct rtnl_link *tab;
130e2849863SThomas Graf 
131e2849863SThomas Graf 	tab = rtnl_msg_handlers[protocol];
13251057f2fSThomas Graf 	if (tab == NULL || tab[msgindex].dumpit == NULL)
133e2849863SThomas Graf 		tab = rtnl_msg_handlers[PF_UNSPEC];
134e2849863SThomas Graf 
13551057f2fSThomas Graf 	return tab ? tab[msgindex].dumpit : NULL;
136e2849863SThomas Graf }
137e2849863SThomas Graf 
138e2849863SThomas Graf /**
139e2849863SThomas Graf  * __rtnl_register - Register a rtnetlink message type
140e2849863SThomas Graf  * @protocol: Protocol family or PF_UNSPEC
141e2849863SThomas Graf  * @msgtype: rtnetlink message type
142e2849863SThomas Graf  * @doit: Function pointer called for each request message
143e2849863SThomas Graf  * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
144e2849863SThomas Graf  *
145e2849863SThomas Graf  * Registers the specified function pointers (at least one of them has
146e2849863SThomas Graf  * to be non-NULL) to be called whenever a request message for the
147e2849863SThomas Graf  * specified protocol family and message type is received.
148e2849863SThomas Graf  *
149e2849863SThomas Graf  * The special protocol family PF_UNSPEC may be used to define fallback
150e2849863SThomas Graf  * function pointers for the case when no entry for the specific protocol
151e2849863SThomas Graf  * family exists.
152e2849863SThomas Graf  *
153e2849863SThomas Graf  * Returns 0 on success or a negative error code.
154e2849863SThomas Graf  */
155e2849863SThomas Graf int __rtnl_register(int protocol, int msgtype,
156e2849863SThomas Graf 		    rtnl_doit_func doit, rtnl_dumpit_func dumpit)
157e2849863SThomas Graf {
158e2849863SThomas Graf 	struct rtnl_link *tab;
159e2849863SThomas Graf 	int msgindex;
160e2849863SThomas Graf 
161e2849863SThomas Graf 	BUG_ON(protocol < 0 || protocol >= NPROTO);
162e2849863SThomas Graf 	msgindex = rtm_msgindex(msgtype);
163e2849863SThomas Graf 
164e2849863SThomas Graf 	tab = rtnl_msg_handlers[protocol];
165e2849863SThomas Graf 	if (tab == NULL) {
166e2849863SThomas Graf 		tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL);
167e2849863SThomas Graf 		if (tab == NULL)
168e2849863SThomas Graf 			return -ENOBUFS;
169e2849863SThomas Graf 
170e2849863SThomas Graf 		rtnl_msg_handlers[protocol] = tab;
171e2849863SThomas Graf 	}
172e2849863SThomas Graf 
173e2849863SThomas Graf 	if (doit)
174e2849863SThomas Graf 		tab[msgindex].doit = doit;
175e2849863SThomas Graf 
176e2849863SThomas Graf 	if (dumpit)
177e2849863SThomas Graf 		tab[msgindex].dumpit = dumpit;
178e2849863SThomas Graf 
179e2849863SThomas Graf 	return 0;
180e2849863SThomas Graf }
181e2849863SThomas Graf 
182e2849863SThomas Graf EXPORT_SYMBOL_GPL(__rtnl_register);
183e2849863SThomas Graf 
184e2849863SThomas Graf /**
185e2849863SThomas Graf  * rtnl_register - Register a rtnetlink message type
186e2849863SThomas Graf  *
187e2849863SThomas Graf  * Identical to __rtnl_register() but panics on failure. This is useful
188e2849863SThomas Graf  * as failure of this function is very unlikely, it can only happen due
189e2849863SThomas Graf  * to lack of memory when allocating the chain to store all message
190e2849863SThomas Graf  * handlers for a protocol. Meant for use in init functions where lack
191e2849863SThomas Graf  * of memory implies no sense in continueing.
192e2849863SThomas Graf  */
193e2849863SThomas Graf void rtnl_register(int protocol, int msgtype,
194e2849863SThomas Graf 		   rtnl_doit_func doit, rtnl_dumpit_func dumpit)
195e2849863SThomas Graf {
196e2849863SThomas Graf 	if (__rtnl_register(protocol, msgtype, doit, dumpit) < 0)
197e2849863SThomas Graf 		panic("Unable to register rtnetlink message handler, "
198e2849863SThomas Graf 		      "protocol = %d, message type = %d\n",
199e2849863SThomas Graf 		      protocol, msgtype);
200e2849863SThomas Graf }
201e2849863SThomas Graf 
202e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_register);
203e2849863SThomas Graf 
204e2849863SThomas Graf /**
205e2849863SThomas Graf  * rtnl_unregister - Unregister a rtnetlink message type
206e2849863SThomas Graf  * @protocol: Protocol family or PF_UNSPEC
207e2849863SThomas Graf  * @msgtype: rtnetlink message type
208e2849863SThomas Graf  *
209e2849863SThomas Graf  * Returns 0 on success or a negative error code.
210e2849863SThomas Graf  */
211e2849863SThomas Graf int rtnl_unregister(int protocol, int msgtype)
212e2849863SThomas Graf {
213e2849863SThomas Graf 	int msgindex;
214e2849863SThomas Graf 
215e2849863SThomas Graf 	BUG_ON(protocol < 0 || protocol >= NPROTO);
216e2849863SThomas Graf 	msgindex = rtm_msgindex(msgtype);
217e2849863SThomas Graf 
218e2849863SThomas Graf 	if (rtnl_msg_handlers[protocol] == NULL)
219e2849863SThomas Graf 		return -ENOENT;
220e2849863SThomas Graf 
221e2849863SThomas Graf 	rtnl_msg_handlers[protocol][msgindex].doit = NULL;
222e2849863SThomas Graf 	rtnl_msg_handlers[protocol][msgindex].dumpit = NULL;
223e2849863SThomas Graf 
224e2849863SThomas Graf 	return 0;
225e2849863SThomas Graf }
226e2849863SThomas Graf 
227e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister);
228e2849863SThomas Graf 
229e2849863SThomas Graf /**
230e2849863SThomas Graf  * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
231e2849863SThomas Graf  * @protocol : Protocol family or PF_UNSPEC
232e2849863SThomas Graf  *
233e2849863SThomas Graf  * Identical to calling rtnl_unregster() for all registered message types
234e2849863SThomas Graf  * of a certain protocol family.
235e2849863SThomas Graf  */
236e2849863SThomas Graf void rtnl_unregister_all(int protocol)
237e2849863SThomas Graf {
238e2849863SThomas Graf 	BUG_ON(protocol < 0 || protocol >= NPROTO);
239e2849863SThomas Graf 
240e2849863SThomas Graf 	kfree(rtnl_msg_handlers[protocol]);
241e2849863SThomas Graf 	rtnl_msg_handlers[protocol] = NULL;
242e2849863SThomas Graf }
243e2849863SThomas Graf 
244e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister_all);
2451da177e4SLinus Torvalds 
246db46edc6SThomas Graf static const int rtm_min[RTM_NR_FAMILIES] =
2471da177e4SLinus Torvalds {
248f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWLINK)]      = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
249f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWADDR)]      = NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
250f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWROUTE)]     = NLMSG_LENGTH(sizeof(struct rtmsg)),
25114c0b97dSThomas Graf 	[RTM_FAM(RTM_NEWRULE)]      = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)),
252f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWQDISC)]     = NLMSG_LENGTH(sizeof(struct tcmsg)),
253f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWTCLASS)]    = NLMSG_LENGTH(sizeof(struct tcmsg)),
254f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWTFILTER)]   = NLMSG_LENGTH(sizeof(struct tcmsg)),
255f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWACTION)]    = NLMSG_LENGTH(sizeof(struct tcamsg)),
256f90a0a74SThomas Graf 	[RTM_FAM(RTM_GETMULTICAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
257f90a0a74SThomas Graf 	[RTM_FAM(RTM_GETANYCAST)]   = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
2581da177e4SLinus Torvalds };
2591da177e4SLinus Torvalds 
260db46edc6SThomas Graf static const int rta_max[RTM_NR_FAMILIES] =
2611da177e4SLinus Torvalds {
262f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWLINK)]      = IFLA_MAX,
263f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWADDR)]      = IFA_MAX,
264f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWROUTE)]     = RTA_MAX,
26514c0b97dSThomas Graf 	[RTM_FAM(RTM_NEWRULE)]      = FRA_MAX,
266f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWQDISC)]     = TCA_MAX,
267f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWTCLASS)]    = TCA_MAX,
268f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWTFILTER)]   = TCA_MAX,
269f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWACTION)]    = TCAA_MAX,
2701da177e4SLinus Torvalds };
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
2731da177e4SLinus Torvalds {
2741da177e4SLinus Torvalds 	struct rtattr *rta;
2751da177e4SLinus Torvalds 	int size = RTA_LENGTH(attrlen);
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds 	rta = (struct rtattr*)skb_put(skb, RTA_ALIGN(size));
2781da177e4SLinus Torvalds 	rta->rta_type = attrtype;
2791da177e4SLinus Torvalds 	rta->rta_len = size;
2801da177e4SLinus Torvalds 	memcpy(RTA_DATA(rta), data, attrlen);
281b3563c4fSPatrick McHardy 	memset(RTA_DATA(rta) + attrlen, 0, RTA_ALIGN(size) - size);
2821da177e4SLinus Torvalds }
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds size_t rtattr_strlcpy(char *dest, const struct rtattr *rta, size_t size)
2851da177e4SLinus Torvalds {
2861da177e4SLinus Torvalds 	size_t ret = RTA_PAYLOAD(rta);
2871da177e4SLinus Torvalds 	char *src = RTA_DATA(rta);
2881da177e4SLinus Torvalds 
2891da177e4SLinus Torvalds 	if (ret > 0 && src[ret - 1] == '\0')
2901da177e4SLinus Torvalds 		ret--;
2911da177e4SLinus Torvalds 	if (size > 0) {
2921da177e4SLinus Torvalds 		size_t len = (ret >= size) ? size - 1 : ret;
2931da177e4SLinus Torvalds 		memset(dest, 0, size);
2941da177e4SLinus Torvalds 		memcpy(dest, src, len);
2951da177e4SLinus Torvalds 	}
2961da177e4SLinus Torvalds 	return ret;
2971da177e4SLinus Torvalds }
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds int rtnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo)
3001da177e4SLinus Torvalds {
3011da177e4SLinus Torvalds 	int err = 0;
3021da177e4SLinus Torvalds 
303ac6d439dSPatrick McHardy 	NETLINK_CB(skb).dst_group = group;
3041da177e4SLinus Torvalds 	if (echo)
3051da177e4SLinus Torvalds 		atomic_inc(&skb->users);
3061da177e4SLinus Torvalds 	netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
3071da177e4SLinus Torvalds 	if (echo)
3081da177e4SLinus Torvalds 		err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
3091da177e4SLinus Torvalds 	return err;
3101da177e4SLinus Torvalds }
3111da177e4SLinus Torvalds 
3122942e900SThomas Graf int rtnl_unicast(struct sk_buff *skb, u32 pid)
3132942e900SThomas Graf {
3142942e900SThomas Graf 	return nlmsg_unicast(rtnl, skb, pid);
3152942e900SThomas Graf }
3162942e900SThomas Graf 
31797676b6bSThomas Graf int rtnl_notify(struct sk_buff *skb, u32 pid, u32 group,
31897676b6bSThomas Graf 		struct nlmsghdr *nlh, gfp_t flags)
31997676b6bSThomas Graf {
32097676b6bSThomas Graf 	int report = 0;
32197676b6bSThomas Graf 
32297676b6bSThomas Graf 	if (nlh)
32397676b6bSThomas Graf 		report = nlmsg_report(nlh);
32497676b6bSThomas Graf 
32597676b6bSThomas Graf 	return nlmsg_notify(rtnl, skb, pid, group, report, flags);
32697676b6bSThomas Graf }
32797676b6bSThomas Graf 
32897676b6bSThomas Graf void rtnl_set_sk_err(u32 group, int error)
32997676b6bSThomas Graf {
33097676b6bSThomas Graf 	netlink_set_err(rtnl, 0, group, error);
33197676b6bSThomas Graf }
33297676b6bSThomas Graf 
3331da177e4SLinus Torvalds int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
3341da177e4SLinus Torvalds {
3352d7202bfSThomas Graf 	struct nlattr *mx;
3362d7202bfSThomas Graf 	int i, valid = 0;
3371da177e4SLinus Torvalds 
3382d7202bfSThomas Graf 	mx = nla_nest_start(skb, RTA_METRICS);
3392d7202bfSThomas Graf 	if (mx == NULL)
3402d7202bfSThomas Graf 		return -ENOBUFS;
3412d7202bfSThomas Graf 
3421da177e4SLinus Torvalds 	for (i = 0; i < RTAX_MAX; i++) {
3432d7202bfSThomas Graf 		if (metrics[i]) {
3442d7202bfSThomas Graf 			valid++;
3452d7202bfSThomas Graf 			NLA_PUT_U32(skb, i+1, metrics[i]);
3461da177e4SLinus Torvalds 		}
3472d7202bfSThomas Graf 	}
3481da177e4SLinus Torvalds 
349a57d27fcSDavid S. Miller 	if (!valid) {
350a57d27fcSDavid S. Miller 		nla_nest_cancel(skb, mx);
351a57d27fcSDavid S. Miller 		return 0;
352a57d27fcSDavid S. Miller 	}
3532d7202bfSThomas Graf 
3542d7202bfSThomas Graf 	return nla_nest_end(skb, mx);
3552d7202bfSThomas Graf 
3562d7202bfSThomas Graf nla_put_failure:
3572d7202bfSThomas Graf 	return nla_nest_cancel(skb, mx);
3581da177e4SLinus Torvalds }
3591da177e4SLinus Torvalds 
360e3703b3dSThomas Graf int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
361e3703b3dSThomas Graf 		       u32 ts, u32 tsage, long expires, u32 error)
362e3703b3dSThomas Graf {
363e3703b3dSThomas Graf 	struct rta_cacheinfo ci = {
364e3703b3dSThomas Graf 		.rta_lastuse = jiffies_to_clock_t(jiffies - dst->lastuse),
365e3703b3dSThomas Graf 		.rta_used = dst->__use,
366e3703b3dSThomas Graf 		.rta_clntref = atomic_read(&(dst->__refcnt)),
367e3703b3dSThomas Graf 		.rta_error = error,
368e3703b3dSThomas Graf 		.rta_id =  id,
369e3703b3dSThomas Graf 		.rta_ts = ts,
370e3703b3dSThomas Graf 		.rta_tsage = tsage,
371e3703b3dSThomas Graf 	};
372e3703b3dSThomas Graf 
373e3703b3dSThomas Graf 	if (expires)
374e3703b3dSThomas Graf 		ci.rta_expires = jiffies_to_clock_t(expires);
375e3703b3dSThomas Graf 
376e3703b3dSThomas Graf 	return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
377e3703b3dSThomas Graf }
378e3703b3dSThomas Graf 
379e3703b3dSThomas Graf EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
3801da177e4SLinus Torvalds 
381b00055aaSStefan Rompf static void set_operstate(struct net_device *dev, unsigned char transition)
382b00055aaSStefan Rompf {
383b00055aaSStefan Rompf 	unsigned char operstate = dev->operstate;
384b00055aaSStefan Rompf 
385b00055aaSStefan Rompf 	switch(transition) {
386b00055aaSStefan Rompf 	case IF_OPER_UP:
387b00055aaSStefan Rompf 		if ((operstate == IF_OPER_DORMANT ||
388b00055aaSStefan Rompf 		     operstate == IF_OPER_UNKNOWN) &&
389b00055aaSStefan Rompf 		    !netif_dormant(dev))
390b00055aaSStefan Rompf 			operstate = IF_OPER_UP;
391b00055aaSStefan Rompf 		break;
392b00055aaSStefan Rompf 
393b00055aaSStefan Rompf 	case IF_OPER_DORMANT:
394b00055aaSStefan Rompf 		if (operstate == IF_OPER_UP ||
395b00055aaSStefan Rompf 		    operstate == IF_OPER_UNKNOWN)
396b00055aaSStefan Rompf 			operstate = IF_OPER_DORMANT;
397b00055aaSStefan Rompf 		break;
3983ff50b79SStephen Hemminger 	}
399b00055aaSStefan Rompf 
400b00055aaSStefan Rompf 	if (dev->operstate != operstate) {
401b00055aaSStefan Rompf 		write_lock_bh(&dev_base_lock);
402b00055aaSStefan Rompf 		dev->operstate = operstate;
403b00055aaSStefan Rompf 		write_unlock_bh(&dev_base_lock);
404b00055aaSStefan Rompf 		netdev_state_change(dev);
405b00055aaSStefan Rompf 	}
406b00055aaSStefan Rompf }
407b00055aaSStefan Rompf 
408b60c5115SThomas Graf static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
409b60c5115SThomas Graf 				 struct net_device_stats *b)
4101da177e4SLinus Torvalds {
411b60c5115SThomas Graf 	a->rx_packets = b->rx_packets;
412b60c5115SThomas Graf 	a->tx_packets = b->tx_packets;
413b60c5115SThomas Graf 	a->rx_bytes = b->rx_bytes;
414b60c5115SThomas Graf 	a->tx_bytes = b->tx_bytes;
415b60c5115SThomas Graf 	a->rx_errors = b->rx_errors;
416b60c5115SThomas Graf 	a->tx_errors = b->tx_errors;
417b60c5115SThomas Graf 	a->rx_dropped = b->rx_dropped;
418b60c5115SThomas Graf 	a->tx_dropped = b->tx_dropped;
419b60c5115SThomas Graf 
420b60c5115SThomas Graf 	a->multicast = b->multicast;
421b60c5115SThomas Graf 	a->collisions = b->collisions;
422b60c5115SThomas Graf 
423b60c5115SThomas Graf 	a->rx_length_errors = b->rx_length_errors;
424b60c5115SThomas Graf 	a->rx_over_errors = b->rx_over_errors;
425b60c5115SThomas Graf 	a->rx_crc_errors = b->rx_crc_errors;
426b60c5115SThomas Graf 	a->rx_frame_errors = b->rx_frame_errors;
427b60c5115SThomas Graf 	a->rx_fifo_errors = b->rx_fifo_errors;
428b60c5115SThomas Graf 	a->rx_missed_errors = b->rx_missed_errors;
429b60c5115SThomas Graf 
430b60c5115SThomas Graf 	a->tx_aborted_errors = b->tx_aborted_errors;
431b60c5115SThomas Graf 	a->tx_carrier_errors = b->tx_carrier_errors;
432b60c5115SThomas Graf 	a->tx_fifo_errors = b->tx_fifo_errors;
433b60c5115SThomas Graf 	a->tx_heartbeat_errors = b->tx_heartbeat_errors;
434b60c5115SThomas Graf 	a->tx_window_errors = b->tx_window_errors;
435b60c5115SThomas Graf 
436b60c5115SThomas Graf 	a->rx_compressed = b->rx_compressed;
437b60c5115SThomas Graf 	a->tx_compressed = b->tx_compressed;
438b60c5115SThomas Graf };
439b60c5115SThomas Graf 
440575c3e2aSPatrick McHardy static inline size_t if_nlmsg_size(void)
441339bf98fSThomas Graf {
442339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
443339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
444339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
445339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rtnl_link_ifmap))
446339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rtnl_link_stats))
447339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
448339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
449339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_TXQLEN */
450339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_WEIGHT */
451339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MTU */
452339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_LINK */
453339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MASTER */
454339bf98fSThomas Graf 	       + nla_total_size(1) /* IFLA_OPERSTATE */
455575c3e2aSPatrick McHardy 	       + nla_total_size(1); /* IFLA_LINKMODE */
456339bf98fSThomas Graf }
457339bf98fSThomas Graf 
458b60c5115SThomas Graf static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
459575c3e2aSPatrick McHardy 			    int type, u32 pid, u32 seq, u32 change,
460575c3e2aSPatrick McHardy 			    unsigned int flags)
461b60c5115SThomas Graf {
462b60c5115SThomas Graf 	struct ifinfomsg *ifm;
4631da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
4641da177e4SLinus Torvalds 
465b60c5115SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
466b60c5115SThomas Graf 	if (nlh == NULL)
46726932566SPatrick McHardy 		return -EMSGSIZE;
4681da177e4SLinus Torvalds 
469b60c5115SThomas Graf 	ifm = nlmsg_data(nlh);
470b60c5115SThomas Graf 	ifm->ifi_family = AF_UNSPEC;
471b60c5115SThomas Graf 	ifm->__ifi_pad = 0;
472b60c5115SThomas Graf 	ifm->ifi_type = dev->type;
473b60c5115SThomas Graf 	ifm->ifi_index = dev->ifindex;
474b60c5115SThomas Graf 	ifm->ifi_flags = dev_get_flags(dev);
475b60c5115SThomas Graf 	ifm->ifi_change = change;
4761da177e4SLinus Torvalds 
477b60c5115SThomas Graf 	NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
478b60c5115SThomas Graf 	NLA_PUT_U32(skb, IFLA_TXQLEN, dev->tx_queue_len);
479b60c5115SThomas Graf 	NLA_PUT_U32(skb, IFLA_WEIGHT, dev->weight);
480b60c5115SThomas Graf 	NLA_PUT_U8(skb, IFLA_OPERSTATE,
481b60c5115SThomas Graf 		   netif_running(dev) ? dev->operstate : IF_OPER_DOWN);
482b60c5115SThomas Graf 	NLA_PUT_U8(skb, IFLA_LINKMODE, dev->link_mode);
483b60c5115SThomas Graf 	NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
4841da177e4SLinus Torvalds 
485b60c5115SThomas Graf 	if (dev->ifindex != dev->iflink)
486b60c5115SThomas Graf 		NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
4871da177e4SLinus Torvalds 
488b60c5115SThomas Graf 	if (dev->master)
489b60c5115SThomas Graf 		NLA_PUT_U32(skb, IFLA_MASTER, dev->master->ifindex);
490b60c5115SThomas Graf 
491b60c5115SThomas Graf 	if (dev->qdisc_sleeping)
492b60c5115SThomas Graf 		NLA_PUT_STRING(skb, IFLA_QDISC, dev->qdisc_sleeping->ops->id);
493b00055aaSStefan Rompf 
494b00055aaSStefan Rompf 	if (1) {
4951da177e4SLinus Torvalds 		struct rtnl_link_ifmap map = {
4961da177e4SLinus Torvalds 			.mem_start   = dev->mem_start,
4971da177e4SLinus Torvalds 			.mem_end     = dev->mem_end,
4981da177e4SLinus Torvalds 			.base_addr   = dev->base_addr,
4991da177e4SLinus Torvalds 			.irq         = dev->irq,
5001da177e4SLinus Torvalds 			.dma         = dev->dma,
5011da177e4SLinus Torvalds 			.port        = dev->if_port,
5021da177e4SLinus Torvalds 		};
503b60c5115SThomas Graf 		NLA_PUT(skb, IFLA_MAP, sizeof(map), &map);
5041da177e4SLinus Torvalds 	}
5051da177e4SLinus Torvalds 
5061da177e4SLinus Torvalds 	if (dev->addr_len) {
507b60c5115SThomas Graf 		NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
508b60c5115SThomas Graf 		NLA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast);
5091da177e4SLinus Torvalds 	}
5101da177e4SLinus Torvalds 
5111da177e4SLinus Torvalds 	if (dev->get_stats) {
512b60c5115SThomas Graf 		struct net_device_stats *stats = dev->get_stats(dev);
5131da177e4SLinus Torvalds 		if (stats) {
514b60c5115SThomas Graf 			struct nlattr *attr;
5151da177e4SLinus Torvalds 
516b60c5115SThomas Graf 			attr = nla_reserve(skb, IFLA_STATS,
517b60c5115SThomas Graf 					   sizeof(struct rtnl_link_stats));
518b60c5115SThomas Graf 			if (attr == NULL)
519b60c5115SThomas Graf 				goto nla_put_failure;
520b60c5115SThomas Graf 
521b60c5115SThomas Graf 			copy_rtnl_link_stats(nla_data(attr), stats);
5221da177e4SLinus Torvalds 		}
5231da177e4SLinus Torvalds 	}
5241da177e4SLinus Torvalds 
525b60c5115SThomas Graf 	return nlmsg_end(skb, nlh);
526b60c5115SThomas Graf 
527b60c5115SThomas Graf nla_put_failure:
52826932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
52926932566SPatrick McHardy 	return -EMSGSIZE;
5301da177e4SLinus Torvalds }
5311da177e4SLinus Torvalds 
532b60c5115SThomas Graf static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
5331da177e4SLinus Torvalds {
5341da177e4SLinus Torvalds 	int idx;
5351da177e4SLinus Torvalds 	int s_idx = cb->args[0];
5361da177e4SLinus Torvalds 	struct net_device *dev;
5371da177e4SLinus Torvalds 
5387562f876SPavel Emelianov 	idx = 0;
5397562f876SPavel Emelianov 	for_each_netdev(dev) {
5401da177e4SLinus Torvalds 		if (idx < s_idx)
5417562f876SPavel Emelianov 			goto cont;
542575c3e2aSPatrick McHardy 		if (rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
543b6544c0bSJamal Hadi Salim 				     NETLINK_CB(cb->skb).pid,
544b60c5115SThomas Graf 				     cb->nlh->nlmsg_seq, 0, NLM_F_MULTI) <= 0)
5451da177e4SLinus Torvalds 			break;
5467562f876SPavel Emelianov cont:
5477562f876SPavel Emelianov 		idx++;
5481da177e4SLinus Torvalds 	}
5491da177e4SLinus Torvalds 	cb->args[0] = idx;
5501da177e4SLinus Torvalds 
5511da177e4SLinus Torvalds 	return skb->len;
5521da177e4SLinus Torvalds }
5531da177e4SLinus Torvalds 
554ef7c79edSPatrick McHardy static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
5555176f91eSThomas Graf 	[IFLA_IFNAME]		= { .type = NLA_STRING, .len = IFNAMSIZ-1 },
5565176f91eSThomas Graf 	[IFLA_MAP]		= { .len = sizeof(struct rtnl_link_ifmap) },
557da5e0494SThomas Graf 	[IFLA_MTU]		= { .type = NLA_U32 },
558da5e0494SThomas Graf 	[IFLA_TXQLEN]		= { .type = NLA_U32 },
559da5e0494SThomas Graf 	[IFLA_WEIGHT]		= { .type = NLA_U32 },
560da5e0494SThomas Graf 	[IFLA_OPERSTATE]	= { .type = NLA_U8 },
561da5e0494SThomas Graf 	[IFLA_LINKMODE]		= { .type = NLA_U8 },
562da5e0494SThomas Graf };
5631da177e4SLinus Torvalds 
564da5e0494SThomas Graf static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
565da5e0494SThomas Graf {
566da5e0494SThomas Graf 	struct ifinfomsg *ifm;
567da5e0494SThomas Graf 	struct net_device *dev;
568da5e0494SThomas Graf 	int err, send_addr_notify = 0, modified = 0;
569da5e0494SThomas Graf 	struct nlattr *tb[IFLA_MAX+1];
5701da177e4SLinus Torvalds 	char ifname[IFNAMSIZ];
5711da177e4SLinus Torvalds 
572da5e0494SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
573da5e0494SThomas Graf 	if (err < 0)
574da5e0494SThomas Graf 		goto errout;
5751da177e4SLinus Torvalds 
5765176f91eSThomas Graf 	if (tb[IFLA_IFNAME])
5775176f91eSThomas Graf 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
57878e5b891SPatrick McHardy 	else
57978e5b891SPatrick McHardy 		ifname[0] = '\0';
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds 	err = -EINVAL;
582da5e0494SThomas Graf 	ifm = nlmsg_data(nlh);
583*51055be8SPatrick McHardy 	if (ifm->ifi_index > 0)
584da5e0494SThomas Graf 		dev = dev_get_by_index(ifm->ifi_index);
585da5e0494SThomas Graf 	else if (tb[IFLA_IFNAME])
586da5e0494SThomas Graf 		dev = dev_get_by_name(ifname);
587da5e0494SThomas Graf 	else
588da5e0494SThomas Graf 		goto errout;
5891da177e4SLinus Torvalds 
590da5e0494SThomas Graf 	if (dev == NULL) {
591da5e0494SThomas Graf 		err = -ENODEV;
592da5e0494SThomas Graf 		goto errout;
593da5e0494SThomas Graf 	}
5941da177e4SLinus Torvalds 
595da5e0494SThomas Graf 	if (tb[IFLA_ADDRESS] &&
596da5e0494SThomas Graf 	    nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
597da5e0494SThomas Graf 		goto errout_dev;
598da5e0494SThomas Graf 
599da5e0494SThomas Graf 	if (tb[IFLA_BROADCAST] &&
600da5e0494SThomas Graf 	    nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
601da5e0494SThomas Graf 		goto errout_dev;
602da5e0494SThomas Graf 
603da5e0494SThomas Graf 	if (tb[IFLA_MAP]) {
6041da177e4SLinus Torvalds 		struct rtnl_link_ifmap *u_map;
6051da177e4SLinus Torvalds 		struct ifmap k_map;
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds 		if (!dev->set_config) {
6081da177e4SLinus Torvalds 			err = -EOPNOTSUPP;
609da5e0494SThomas Graf 			goto errout_dev;
6101da177e4SLinus Torvalds 		}
6111da177e4SLinus Torvalds 
6121da177e4SLinus Torvalds 		if (!netif_device_present(dev)) {
6131da177e4SLinus Torvalds 			err = -ENODEV;
614da5e0494SThomas Graf 			goto errout_dev;
6151da177e4SLinus Torvalds 		}
6161da177e4SLinus Torvalds 
617da5e0494SThomas Graf 		u_map = nla_data(tb[IFLA_MAP]);
6181da177e4SLinus Torvalds 		k_map.mem_start = (unsigned long) u_map->mem_start;
6191da177e4SLinus Torvalds 		k_map.mem_end = (unsigned long) u_map->mem_end;
6201da177e4SLinus Torvalds 		k_map.base_addr = (unsigned short) u_map->base_addr;
6211da177e4SLinus Torvalds 		k_map.irq = (unsigned char) u_map->irq;
6221da177e4SLinus Torvalds 		k_map.dma = (unsigned char) u_map->dma;
6231da177e4SLinus Torvalds 		k_map.port = (unsigned char) u_map->port;
6241da177e4SLinus Torvalds 
6251da177e4SLinus Torvalds 		err = dev->set_config(dev, &k_map);
626da5e0494SThomas Graf 		if (err < 0)
627da5e0494SThomas Graf 			goto errout_dev;
6281da177e4SLinus Torvalds 
629da5e0494SThomas Graf 		modified = 1;
6301da177e4SLinus Torvalds 	}
6311da177e4SLinus Torvalds 
632da5e0494SThomas Graf 	if (tb[IFLA_ADDRESS]) {
63370f8e78eSDavid S. Miller 		struct sockaddr *sa;
63470f8e78eSDavid S. Miller 		int len;
63570f8e78eSDavid S. Miller 
6361da177e4SLinus Torvalds 		if (!dev->set_mac_address) {
6371da177e4SLinus Torvalds 			err = -EOPNOTSUPP;
638da5e0494SThomas Graf 			goto errout_dev;
6391da177e4SLinus Torvalds 		}
640da5e0494SThomas Graf 
6411da177e4SLinus Torvalds 		if (!netif_device_present(dev)) {
6421da177e4SLinus Torvalds 			err = -ENODEV;
643da5e0494SThomas Graf 			goto errout_dev;
6441da177e4SLinus Torvalds 		}
6451da177e4SLinus Torvalds 
64670f8e78eSDavid S. Miller 		len = sizeof(sa_family_t) + dev->addr_len;
64770f8e78eSDavid S. Miller 		sa = kmalloc(len, GFP_KERNEL);
64870f8e78eSDavid S. Miller 		if (!sa) {
64970f8e78eSDavid S. Miller 			err = -ENOMEM;
650da5e0494SThomas Graf 			goto errout_dev;
65170f8e78eSDavid S. Miller 		}
65270f8e78eSDavid S. Miller 		sa->sa_family = dev->type;
653da5e0494SThomas Graf 		memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
65470f8e78eSDavid S. Miller 		       dev->addr_len);
65570f8e78eSDavid S. Miller 		err = dev->set_mac_address(dev, sa);
65670f8e78eSDavid S. Miller 		kfree(sa);
6571da177e4SLinus Torvalds 		if (err)
658da5e0494SThomas Graf 			goto errout_dev;
6591da177e4SLinus Torvalds 		send_addr_notify = 1;
660da5e0494SThomas Graf 		modified = 1;
6611da177e4SLinus Torvalds 	}
6621da177e4SLinus Torvalds 
663da5e0494SThomas Graf 	if (tb[IFLA_MTU]) {
664da5e0494SThomas Graf 		err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
665da5e0494SThomas Graf 		if (err < 0)
666da5e0494SThomas Graf 			goto errout_dev;
667da5e0494SThomas Graf 		modified = 1;
6681da177e4SLinus Torvalds 	}
6691da177e4SLinus Torvalds 
670da5e0494SThomas Graf 	/*
671da5e0494SThomas Graf 	 * Interface selected by interface index but interface
672da5e0494SThomas Graf 	 * name provided implies that a name change has been
673da5e0494SThomas Graf 	 * requested.
674da5e0494SThomas Graf 	 */
675*51055be8SPatrick McHardy 	if (ifm->ifi_index > 0 && ifname[0]) {
6761da177e4SLinus Torvalds 		err = dev_change_name(dev, ifname);
677da5e0494SThomas Graf 		if (err < 0)
678da5e0494SThomas Graf 			goto errout_dev;
679da5e0494SThomas Graf 		modified = 1;
6801da177e4SLinus Torvalds 	}
6811da177e4SLinus Torvalds 
682da5e0494SThomas Graf 	if (tb[IFLA_BROADCAST]) {
683da5e0494SThomas Graf 		nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
684da5e0494SThomas Graf 		send_addr_notify = 1;
685da5e0494SThomas Graf 	}
686da5e0494SThomas Graf 
687da5e0494SThomas Graf 
68883b496e9SPatrick McHardy 	if (ifm->ifi_flags || ifm->ifi_change) {
68983b496e9SPatrick McHardy 		unsigned int flags = ifm->ifi_flags;
69083b496e9SPatrick McHardy 
69183b496e9SPatrick McHardy 		/* bugwards compatibility: ifi_change == 0 is treated as ~0 */
69283b496e9SPatrick McHardy 		if (ifm->ifi_change)
69383b496e9SPatrick McHardy 			flags = (flags & ifm->ifi_change) |
69483b496e9SPatrick McHardy 				(dev->flags & ~ifm->ifi_change);
69583b496e9SPatrick McHardy 		dev_change_flags(dev, flags);
69683b496e9SPatrick McHardy 	}
697da5e0494SThomas Graf 
698da5e0494SThomas Graf 	if (tb[IFLA_TXQLEN])
699da5e0494SThomas Graf 		dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
700da5e0494SThomas Graf 
701da5e0494SThomas Graf 	if (tb[IFLA_WEIGHT])
702da5e0494SThomas Graf 		dev->weight = nla_get_u32(tb[IFLA_WEIGHT]);
703da5e0494SThomas Graf 
704da5e0494SThomas Graf 	if (tb[IFLA_OPERSTATE])
705da5e0494SThomas Graf 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
706da5e0494SThomas Graf 
707da5e0494SThomas Graf 	if (tb[IFLA_LINKMODE]) {
708da5e0494SThomas Graf 		write_lock_bh(&dev_base_lock);
709da5e0494SThomas Graf 		dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
710da5e0494SThomas Graf 		write_unlock_bh(&dev_base_lock);
711da5e0494SThomas Graf 	}
712da5e0494SThomas Graf 
7131da177e4SLinus Torvalds 	err = 0;
7141da177e4SLinus Torvalds 
715da5e0494SThomas Graf errout_dev:
716da5e0494SThomas Graf 	if (err < 0 && modified && net_ratelimit())
717da5e0494SThomas Graf 		printk(KERN_WARNING "A link change request failed with "
718da5e0494SThomas Graf 		       "some changes comitted already. Interface %s may "
719da5e0494SThomas Graf 		       "have been left with an inconsistent configuration, "
720da5e0494SThomas Graf 		       "please check.\n", dev->name);
721da5e0494SThomas Graf 
7221da177e4SLinus Torvalds 	if (send_addr_notify)
7231da177e4SLinus Torvalds 		call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
7241da177e4SLinus Torvalds 
7251da177e4SLinus Torvalds 	dev_put(dev);
726da5e0494SThomas Graf errout:
7271da177e4SLinus Torvalds 	return err;
7281da177e4SLinus Torvalds }
7291da177e4SLinus Torvalds 
730b60c5115SThomas Graf static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
731711e2c33SJean Tourrilhes {
732b60c5115SThomas Graf 	struct ifinfomsg *ifm;
733b60c5115SThomas Graf 	struct nlattr *tb[IFLA_MAX+1];
734b60c5115SThomas Graf 	struct net_device *dev = NULL;
735b60c5115SThomas Graf 	struct sk_buff *nskb;
736339bf98fSThomas Graf 	int err;
737711e2c33SJean Tourrilhes 
738b60c5115SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
739b60c5115SThomas Graf 	if (err < 0)
7409918f230SEric Sesterhenn 		return err;
741b60c5115SThomas Graf 
742b60c5115SThomas Graf 	ifm = nlmsg_data(nlh);
743*51055be8SPatrick McHardy 	if (ifm->ifi_index > 0) {
744711e2c33SJean Tourrilhes 		dev = dev_get_by_index(ifm->ifi_index);
745b60c5115SThomas Graf 		if (dev == NULL)
746711e2c33SJean Tourrilhes 			return -ENODEV;
747b60c5115SThomas Graf 	} else
748b60c5115SThomas Graf 		return -EINVAL;
749b60c5115SThomas Graf 
750575c3e2aSPatrick McHardy 	nskb = nlmsg_new(if_nlmsg_size(), GFP_KERNEL);
751b60c5115SThomas Graf 	if (nskb == NULL) {
752b60c5115SThomas Graf 		err = -ENOBUFS;
753b60c5115SThomas Graf 		goto errout;
754b60c5115SThomas Graf 	}
755711e2c33SJean Tourrilhes 
756575c3e2aSPatrick McHardy 	err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).pid,
757575c3e2aSPatrick McHardy 			       nlh->nlmsg_seq, 0, 0);
75826932566SPatrick McHardy 	if (err < 0) {
75926932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in if_nlmsg_size */
76026932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
76126932566SPatrick McHardy 		kfree_skb(nskb);
76226932566SPatrick McHardy 		goto errout;
76326932566SPatrick McHardy 	}
764b974179aSPatrick McHardy 	err = rtnl_unicast(nskb, NETLINK_CB(skb).pid);
765b60c5115SThomas Graf errout:
766711e2c33SJean Tourrilhes 	dev_put(dev);
767b60c5115SThomas Graf 
768711e2c33SJean Tourrilhes 	return err;
769711e2c33SJean Tourrilhes }
770711e2c33SJean Tourrilhes 
77142bad1daSAdrian Bunk static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
7721da177e4SLinus Torvalds {
7731da177e4SLinus Torvalds 	int idx;
7741da177e4SLinus Torvalds 	int s_idx = cb->family;
7751da177e4SLinus Torvalds 
7761da177e4SLinus Torvalds 	if (s_idx == 0)
7771da177e4SLinus Torvalds 		s_idx = 1;
7781da177e4SLinus Torvalds 	for (idx=1; idx<NPROTO; idx++) {
7791da177e4SLinus Torvalds 		int type = cb->nlh->nlmsg_type-RTM_BASE;
7801da177e4SLinus Torvalds 		if (idx < s_idx || idx == PF_PACKET)
7811da177e4SLinus Torvalds 			continue;
782e2849863SThomas Graf 		if (rtnl_msg_handlers[idx] == NULL ||
783e2849863SThomas Graf 		    rtnl_msg_handlers[idx][type].dumpit == NULL)
7841da177e4SLinus Torvalds 			continue;
7851da177e4SLinus Torvalds 		if (idx > s_idx)
7861da177e4SLinus Torvalds 			memset(&cb->args[0], 0, sizeof(cb->args));
787e2849863SThomas Graf 		if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
7881da177e4SLinus Torvalds 			break;
7891da177e4SLinus Torvalds 	}
7901da177e4SLinus Torvalds 	cb->family = idx;
7911da177e4SLinus Torvalds 
7921da177e4SLinus Torvalds 	return skb->len;
7931da177e4SLinus Torvalds }
7941da177e4SLinus Torvalds 
7951da177e4SLinus Torvalds void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change)
7961da177e4SLinus Torvalds {
7971da177e4SLinus Torvalds 	struct sk_buff *skb;
7980ec6d3f4SThomas Graf 	int err = -ENOBUFS;
7991da177e4SLinus Torvalds 
800575c3e2aSPatrick McHardy 	skb = nlmsg_new(if_nlmsg_size(), GFP_KERNEL);
8010ec6d3f4SThomas Graf 	if (skb == NULL)
8020ec6d3f4SThomas Graf 		goto errout;
8031da177e4SLinus Torvalds 
804575c3e2aSPatrick McHardy 	err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0);
80526932566SPatrick McHardy 	if (err < 0) {
80626932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in if_nlmsg_size() */
80726932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
80826932566SPatrick McHardy 		kfree_skb(skb);
80926932566SPatrick McHardy 		goto errout;
81026932566SPatrick McHardy 	}
8110ec6d3f4SThomas Graf 	err = rtnl_notify(skb, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
8120ec6d3f4SThomas Graf errout:
8130ec6d3f4SThomas Graf 	if (err < 0)
8140ec6d3f4SThomas Graf 		rtnl_set_sk_err(RTNLGRP_LINK, err);
8151da177e4SLinus Torvalds }
8161da177e4SLinus Torvalds 
8171da177e4SLinus Torvalds /* Protected by RTNL sempahore.  */
8181da177e4SLinus Torvalds static struct rtattr **rta_buf;
8191da177e4SLinus Torvalds static int rtattr_max;
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds /* Process one rtnetlink message. */
8221da177e4SLinus Torvalds 
8231d00a4ebSThomas Graf static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
8241da177e4SLinus Torvalds {
825e2849863SThomas Graf 	rtnl_doit_func doit;
8261da177e4SLinus Torvalds 	int sz_idx, kind;
8271da177e4SLinus Torvalds 	int min_len;
8281da177e4SLinus Torvalds 	int family;
8291da177e4SLinus Torvalds 	int type;
8301c2d670fSPatrick McHardy 	int err;
8311da177e4SLinus Torvalds 
8321da177e4SLinus Torvalds 	type = nlh->nlmsg_type;
8331da177e4SLinus Torvalds 	if (type > RTM_MAX)
834038890feSThomas Graf 		return -EOPNOTSUPP;
8351da177e4SLinus Torvalds 
8361da177e4SLinus Torvalds 	type -= RTM_BASE;
8371da177e4SLinus Torvalds 
8381da177e4SLinus Torvalds 	/* All the messages must have at least 1 byte length */
8391da177e4SLinus Torvalds 	if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg)))
8401da177e4SLinus Torvalds 		return 0;
8411da177e4SLinus Torvalds 
8421da177e4SLinus Torvalds 	family = ((struct rtgenmsg*)NLMSG_DATA(nlh))->rtgen_family;
8431d00a4ebSThomas Graf 	if (family >= NPROTO)
8441d00a4ebSThomas Graf 		return -EAFNOSUPPORT;
8451da177e4SLinus Torvalds 
8461da177e4SLinus Torvalds 	sz_idx = type>>2;
8471da177e4SLinus Torvalds 	kind = type&3;
8481da177e4SLinus Torvalds 
8491d00a4ebSThomas Graf 	if (kind != 2 && security_netlink_recv(skb, CAP_NET_ADMIN))
8501d00a4ebSThomas Graf 		return -EPERM;
8511da177e4SLinus Torvalds 
8521da177e4SLinus Torvalds 	if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
853e2849863SThomas Graf 		rtnl_dumpit_func dumpit;
8541da177e4SLinus Torvalds 
855e2849863SThomas Graf 		dumpit = rtnl_get_dumpit(family, type);
856e2849863SThomas Graf 		if (dumpit == NULL)
857038890feSThomas Graf 			return -EOPNOTSUPP;
8581da177e4SLinus Torvalds 
8591c2d670fSPatrick McHardy 		__rtnl_unlock();
8601c2d670fSPatrick McHardy 		err = netlink_dump_start(rtnl, skb, nlh, dumpit, NULL);
8611c2d670fSPatrick McHardy 		rtnl_lock();
8621c2d670fSPatrick McHardy 		return err;
8631da177e4SLinus Torvalds 	}
8641da177e4SLinus Torvalds 
8651da177e4SLinus Torvalds 	memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *)));
8661da177e4SLinus Torvalds 
8671da177e4SLinus Torvalds 	min_len = rtm_min[sz_idx];
8681da177e4SLinus Torvalds 	if (nlh->nlmsg_len < min_len)
8691d00a4ebSThomas Graf 		return -EINVAL;
8701da177e4SLinus Torvalds 
8711da177e4SLinus Torvalds 	if (nlh->nlmsg_len > min_len) {
8721da177e4SLinus Torvalds 		int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
8731da177e4SLinus Torvalds 		struct rtattr *attr = (void*)nlh + NLMSG_ALIGN(min_len);
8741da177e4SLinus Torvalds 
8751da177e4SLinus Torvalds 		while (RTA_OK(attr, attrlen)) {
8761da177e4SLinus Torvalds 			unsigned flavor = attr->rta_type;
8771da177e4SLinus Torvalds 			if (flavor) {
8781da177e4SLinus Torvalds 				if (flavor > rta_max[sz_idx])
8791d00a4ebSThomas Graf 					return -EINVAL;
8801da177e4SLinus Torvalds 				rta_buf[flavor-1] = attr;
8811da177e4SLinus Torvalds 			}
8821da177e4SLinus Torvalds 			attr = RTA_NEXT(attr, attrlen);
8831da177e4SLinus Torvalds 		}
8841da177e4SLinus Torvalds 	}
8851da177e4SLinus Torvalds 
886e2849863SThomas Graf 	doit = rtnl_get_doit(family, type);
887e2849863SThomas Graf 	if (doit == NULL)
888038890feSThomas Graf 		return -EOPNOTSUPP;
8891da177e4SLinus Torvalds 
8901d00a4ebSThomas Graf 	return doit(skb, nlh, (void *)&rta_buf[0]);
8911da177e4SLinus Torvalds }
8921da177e4SLinus Torvalds 
8931da177e4SLinus Torvalds static void rtnetlink_rcv(struct sock *sk, int len)
8941da177e4SLinus Torvalds {
8959ac4a169SThomas Graf 	unsigned int qlen = 0;
8962a0a6ebeSHerbert Xu 
8971da177e4SLinus Torvalds 	do {
8986756ae4bSStephen Hemminger 		mutex_lock(&rtnl_mutex);
8999ac4a169SThomas Graf 		netlink_run_queue(sk, &qlen, &rtnetlink_rcv_msg);
9006756ae4bSStephen Hemminger 		mutex_unlock(&rtnl_mutex);
9011da177e4SLinus Torvalds 
9021da177e4SLinus Torvalds 		netdev_run_todo();
9032a0a6ebeSHerbert Xu 	} while (qlen);
9041da177e4SLinus Torvalds }
9051da177e4SLinus Torvalds 
9061da177e4SLinus Torvalds static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
9071da177e4SLinus Torvalds {
9081da177e4SLinus Torvalds 	struct net_device *dev = ptr;
9091da177e4SLinus Torvalds 	switch (event) {
9101da177e4SLinus Torvalds 	case NETDEV_UNREGISTER:
9111da177e4SLinus Torvalds 		rtmsg_ifinfo(RTM_DELLINK, dev, ~0U);
9121da177e4SLinus Torvalds 		break;
9131da177e4SLinus Torvalds 	case NETDEV_REGISTER:
9141da177e4SLinus Torvalds 		rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
9151da177e4SLinus Torvalds 		break;
9161da177e4SLinus Torvalds 	case NETDEV_UP:
9171da177e4SLinus Torvalds 	case NETDEV_DOWN:
9181da177e4SLinus Torvalds 		rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
9191da177e4SLinus Torvalds 		break;
9201da177e4SLinus Torvalds 	case NETDEV_CHANGE:
9211da177e4SLinus Torvalds 	case NETDEV_GOING_DOWN:
9221da177e4SLinus Torvalds 		break;
9231da177e4SLinus Torvalds 	default:
9241da177e4SLinus Torvalds 		rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
9251da177e4SLinus Torvalds 		break;
9261da177e4SLinus Torvalds 	}
9271da177e4SLinus Torvalds 	return NOTIFY_DONE;
9281da177e4SLinus Torvalds }
9291da177e4SLinus Torvalds 
9301da177e4SLinus Torvalds static struct notifier_block rtnetlink_dev_notifier = {
9311da177e4SLinus Torvalds 	.notifier_call	= rtnetlink_event,
9321da177e4SLinus Torvalds };
9331da177e4SLinus Torvalds 
9341da177e4SLinus Torvalds void __init rtnetlink_init(void)
9351da177e4SLinus Torvalds {
9361da177e4SLinus Torvalds 	int i;
9371da177e4SLinus Torvalds 
9381da177e4SLinus Torvalds 	rtattr_max = 0;
9391da177e4SLinus Torvalds 	for (i = 0; i < ARRAY_SIZE(rta_max); i++)
9401da177e4SLinus Torvalds 		if (rta_max[i] > rtattr_max)
9411da177e4SLinus Torvalds 			rtattr_max = rta_max[i];
9421da177e4SLinus Torvalds 	rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL);
9431da177e4SLinus Torvalds 	if (!rta_buf)
9441da177e4SLinus Torvalds 		panic("rtnetlink_init: cannot allocate rta_buf\n");
9451da177e4SLinus Torvalds 
94606628607SPatrick McHardy 	rtnl = netlink_kernel_create(NETLINK_ROUTE, RTNLGRP_MAX, rtnetlink_rcv,
9471c2d670fSPatrick McHardy 				     &rtnl_mutex, THIS_MODULE);
9481da177e4SLinus Torvalds 	if (rtnl == NULL)
9491da177e4SLinus Torvalds 		panic("rtnetlink_init: cannot initialize rtnetlink\n");
9501da177e4SLinus Torvalds 	netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV);
9511da177e4SLinus Torvalds 	register_netdevice_notifier(&rtnetlink_dev_notifier);
952340d17fcSThomas Graf 
953340d17fcSThomas Graf 	rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, rtnl_dump_ifinfo);
954340d17fcSThomas Graf 	rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL);
955687ad8ccSThomas Graf 
956687ad8ccSThomas Graf 	rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all);
957687ad8ccSThomas Graf 	rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all);
9581da177e4SLinus Torvalds }
9591da177e4SLinus Torvalds 
9601da177e4SLinus Torvalds EXPORT_SYMBOL(__rta_fill);
9611da177e4SLinus Torvalds EXPORT_SYMBOL(rtattr_strlcpy);
9621da177e4SLinus Torvalds EXPORT_SYMBOL(rtattr_parse);
9631da177e4SLinus Torvalds EXPORT_SYMBOL(rtnetlink_put_metrics);
9641da177e4SLinus Torvalds EXPORT_SYMBOL(rtnl_lock);
9656756ae4bSStephen Hemminger EXPORT_SYMBOL(rtnl_trylock);
9661da177e4SLinus Torvalds EXPORT_SYMBOL(rtnl_unlock);
9672942e900SThomas Graf EXPORT_SYMBOL(rtnl_unicast);
96897676b6bSThomas Graf EXPORT_SYMBOL(rtnl_notify);
96997676b6bSThomas Graf EXPORT_SYMBOL(rtnl_set_sk_err);
970