xref: /openbmc/linux/net/core/rtnetlink.c (revision c2805fbb)
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>
539ac4a169SThomas Graf #include <net/netlink.h>
54711e2c33SJean Tourrilhes #ifdef CONFIG_NET_WIRELESS_RTNETLINK
55711e2c33SJean Tourrilhes #include <linux/wireless.h>
56711e2c33SJean Tourrilhes #include <net/iw_handler.h>
57711e2c33SJean Tourrilhes #endif	/* CONFIG_NET_WIRELESS_RTNETLINK */
581da177e4SLinus Torvalds 
596756ae4bSStephen Hemminger static DEFINE_MUTEX(rtnl_mutex);
6056fc85acSThomas Graf static struct sock *rtnl;
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds void rtnl_lock(void)
631da177e4SLinus Torvalds {
646756ae4bSStephen Hemminger 	mutex_lock(&rtnl_mutex);
651da177e4SLinus Torvalds }
661da177e4SLinus Torvalds 
676756ae4bSStephen Hemminger void __rtnl_unlock(void)
681da177e4SLinus Torvalds {
696756ae4bSStephen Hemminger 	mutex_unlock(&rtnl_mutex);
701da177e4SLinus Torvalds }
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds void rtnl_unlock(void)
731da177e4SLinus Torvalds {
746756ae4bSStephen Hemminger 	mutex_unlock(&rtnl_mutex);
756756ae4bSStephen Hemminger 	if (rtnl && rtnl->sk_receive_queue.qlen)
766756ae4bSStephen Hemminger 		rtnl->sk_data_ready(rtnl, 0);
771da177e4SLinus Torvalds 	netdev_run_todo();
781da177e4SLinus Torvalds }
791da177e4SLinus Torvalds 
806756ae4bSStephen Hemminger int rtnl_trylock(void)
816756ae4bSStephen Hemminger {
826756ae4bSStephen Hemminger 	return mutex_trylock(&rtnl_mutex);
836756ae4bSStephen Hemminger }
846756ae4bSStephen Hemminger 
851da177e4SLinus Torvalds int rtattr_parse(struct rtattr *tb[], int maxattr, struct rtattr *rta, int len)
861da177e4SLinus Torvalds {
871da177e4SLinus Torvalds 	memset(tb, 0, sizeof(struct rtattr*)*maxattr);
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds 	while (RTA_OK(rta, len)) {
901da177e4SLinus Torvalds 		unsigned flavor = rta->rta_type;
911da177e4SLinus Torvalds 		if (flavor && flavor <= maxattr)
921da177e4SLinus Torvalds 			tb[flavor-1] = rta;
931da177e4SLinus Torvalds 		rta = RTA_NEXT(rta, len);
941da177e4SLinus Torvalds 	}
951da177e4SLinus Torvalds 	return 0;
961da177e4SLinus Torvalds }
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds struct rtnetlink_link * rtnetlink_links[NPROTO];
991da177e4SLinus Torvalds 
100db46edc6SThomas Graf static const int rtm_min[RTM_NR_FAMILIES] =
1011da177e4SLinus Torvalds {
102f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWLINK)]      = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
103f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWADDR)]      = NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
104f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWROUTE)]     = NLMSG_LENGTH(sizeof(struct rtmsg)),
10514c0b97dSThomas Graf 	[RTM_FAM(RTM_NEWRULE)]      = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)),
106f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWQDISC)]     = NLMSG_LENGTH(sizeof(struct tcmsg)),
107f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWTCLASS)]    = NLMSG_LENGTH(sizeof(struct tcmsg)),
108f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWTFILTER)]   = NLMSG_LENGTH(sizeof(struct tcmsg)),
109f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWACTION)]    = NLMSG_LENGTH(sizeof(struct tcamsg)),
110f90a0a74SThomas Graf 	[RTM_FAM(RTM_GETMULTICAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
111f90a0a74SThomas Graf 	[RTM_FAM(RTM_GETANYCAST)]   = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
1121da177e4SLinus Torvalds };
1131da177e4SLinus Torvalds 
114db46edc6SThomas Graf static const int rta_max[RTM_NR_FAMILIES] =
1151da177e4SLinus Torvalds {
116f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWLINK)]      = IFLA_MAX,
117f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWADDR)]      = IFA_MAX,
118f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWROUTE)]     = RTA_MAX,
11914c0b97dSThomas Graf 	[RTM_FAM(RTM_NEWRULE)]      = FRA_MAX,
120f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWQDISC)]     = TCA_MAX,
121f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWTCLASS)]    = TCA_MAX,
122f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWTFILTER)]   = TCA_MAX,
123f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWACTION)]    = TCAA_MAX,
1241da177e4SLinus Torvalds };
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
1271da177e4SLinus Torvalds {
1281da177e4SLinus Torvalds 	struct rtattr *rta;
1291da177e4SLinus Torvalds 	int size = RTA_LENGTH(attrlen);
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds 	rta = (struct rtattr*)skb_put(skb, RTA_ALIGN(size));
1321da177e4SLinus Torvalds 	rta->rta_type = attrtype;
1331da177e4SLinus Torvalds 	rta->rta_len = size;
1341da177e4SLinus Torvalds 	memcpy(RTA_DATA(rta), data, attrlen);
135b3563c4fSPatrick McHardy 	memset(RTA_DATA(rta) + attrlen, 0, RTA_ALIGN(size) - size);
1361da177e4SLinus Torvalds }
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds size_t rtattr_strlcpy(char *dest, const struct rtattr *rta, size_t size)
1391da177e4SLinus Torvalds {
1401da177e4SLinus Torvalds 	size_t ret = RTA_PAYLOAD(rta);
1411da177e4SLinus Torvalds 	char *src = RTA_DATA(rta);
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds 	if (ret > 0 && src[ret - 1] == '\0')
1441da177e4SLinus Torvalds 		ret--;
1451da177e4SLinus Torvalds 	if (size > 0) {
1461da177e4SLinus Torvalds 		size_t len = (ret >= size) ? size - 1 : ret;
1471da177e4SLinus Torvalds 		memset(dest, 0, size);
1481da177e4SLinus Torvalds 		memcpy(dest, src, len);
1491da177e4SLinus Torvalds 	}
1501da177e4SLinus Torvalds 	return ret;
1511da177e4SLinus Torvalds }
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds int rtnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo)
1541da177e4SLinus Torvalds {
1551da177e4SLinus Torvalds 	int err = 0;
1561da177e4SLinus Torvalds 
157ac6d439dSPatrick McHardy 	NETLINK_CB(skb).dst_group = group;
1581da177e4SLinus Torvalds 	if (echo)
1591da177e4SLinus Torvalds 		atomic_inc(&skb->users);
1601da177e4SLinus Torvalds 	netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
1611da177e4SLinus Torvalds 	if (echo)
1621da177e4SLinus Torvalds 		err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
1631da177e4SLinus Torvalds 	return err;
1641da177e4SLinus Torvalds }
1651da177e4SLinus Torvalds 
1662942e900SThomas Graf int rtnl_unicast(struct sk_buff *skb, u32 pid)
1672942e900SThomas Graf {
1682942e900SThomas Graf 	return nlmsg_unicast(rtnl, skb, pid);
1692942e900SThomas Graf }
1702942e900SThomas Graf 
17197676b6bSThomas Graf int rtnl_notify(struct sk_buff *skb, u32 pid, u32 group,
17297676b6bSThomas Graf 		struct nlmsghdr *nlh, gfp_t flags)
17397676b6bSThomas Graf {
17497676b6bSThomas Graf 	int report = 0;
17597676b6bSThomas Graf 
17697676b6bSThomas Graf 	if (nlh)
17797676b6bSThomas Graf 		report = nlmsg_report(nlh);
17897676b6bSThomas Graf 
17997676b6bSThomas Graf 	return nlmsg_notify(rtnl, skb, pid, group, report, flags);
18097676b6bSThomas Graf }
18197676b6bSThomas Graf 
18297676b6bSThomas Graf void rtnl_set_sk_err(u32 group, int error)
18397676b6bSThomas Graf {
18497676b6bSThomas Graf 	netlink_set_err(rtnl, 0, group, error);
18597676b6bSThomas Graf }
18697676b6bSThomas Graf 
1871da177e4SLinus Torvalds int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
1881da177e4SLinus Torvalds {
1892d7202bfSThomas Graf 	struct nlattr *mx;
1902d7202bfSThomas Graf 	int i, valid = 0;
1911da177e4SLinus Torvalds 
1922d7202bfSThomas Graf 	mx = nla_nest_start(skb, RTA_METRICS);
1932d7202bfSThomas Graf 	if (mx == NULL)
1942d7202bfSThomas Graf 		return -ENOBUFS;
1952d7202bfSThomas Graf 
1961da177e4SLinus Torvalds 	for (i = 0; i < RTAX_MAX; i++) {
1972d7202bfSThomas Graf 		if (metrics[i]) {
1982d7202bfSThomas Graf 			valid++;
1992d7202bfSThomas Graf 			NLA_PUT_U32(skb, i+1, metrics[i]);
2001da177e4SLinus Torvalds 		}
2012d7202bfSThomas Graf 	}
2021da177e4SLinus Torvalds 
203a57d27fcSDavid S. Miller 	if (!valid) {
204a57d27fcSDavid S. Miller 		nla_nest_cancel(skb, mx);
205a57d27fcSDavid S. Miller 		return 0;
206a57d27fcSDavid S. Miller 	}
2072d7202bfSThomas Graf 
2082d7202bfSThomas Graf 	return nla_nest_end(skb, mx);
2092d7202bfSThomas Graf 
2102d7202bfSThomas Graf nla_put_failure:
2112d7202bfSThomas Graf 	return nla_nest_cancel(skb, mx);
2121da177e4SLinus Torvalds }
2131da177e4SLinus Torvalds 
214e3703b3dSThomas Graf int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
215e3703b3dSThomas Graf 		       u32 ts, u32 tsage, long expires, u32 error)
216e3703b3dSThomas Graf {
217e3703b3dSThomas Graf 	struct rta_cacheinfo ci = {
218e3703b3dSThomas Graf 		.rta_lastuse = jiffies_to_clock_t(jiffies - dst->lastuse),
219e3703b3dSThomas Graf 		.rta_used = dst->__use,
220e3703b3dSThomas Graf 		.rta_clntref = atomic_read(&(dst->__refcnt)),
221e3703b3dSThomas Graf 		.rta_error = error,
222e3703b3dSThomas Graf 		.rta_id =  id,
223e3703b3dSThomas Graf 		.rta_ts = ts,
224e3703b3dSThomas Graf 		.rta_tsage = tsage,
225e3703b3dSThomas Graf 	};
226e3703b3dSThomas Graf 
227e3703b3dSThomas Graf 	if (expires)
228e3703b3dSThomas Graf 		ci.rta_expires = jiffies_to_clock_t(expires);
229e3703b3dSThomas Graf 
230e3703b3dSThomas Graf 	return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
231e3703b3dSThomas Graf }
232e3703b3dSThomas Graf 
233e3703b3dSThomas Graf EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
2341da177e4SLinus Torvalds 
235b00055aaSStefan Rompf static void set_operstate(struct net_device *dev, unsigned char transition)
236b00055aaSStefan Rompf {
237b00055aaSStefan Rompf 	unsigned char operstate = dev->operstate;
238b00055aaSStefan Rompf 
239b00055aaSStefan Rompf 	switch(transition) {
240b00055aaSStefan Rompf 	case IF_OPER_UP:
241b00055aaSStefan Rompf 		if ((operstate == IF_OPER_DORMANT ||
242b00055aaSStefan Rompf 		     operstate == IF_OPER_UNKNOWN) &&
243b00055aaSStefan Rompf 		    !netif_dormant(dev))
244b00055aaSStefan Rompf 			operstate = IF_OPER_UP;
245b00055aaSStefan Rompf 		break;
246b00055aaSStefan Rompf 
247b00055aaSStefan Rompf 	case IF_OPER_DORMANT:
248b00055aaSStefan Rompf 		if (operstate == IF_OPER_UP ||
249b00055aaSStefan Rompf 		    operstate == IF_OPER_UNKNOWN)
250b00055aaSStefan Rompf 			operstate = IF_OPER_DORMANT;
251b00055aaSStefan Rompf 		break;
252b00055aaSStefan Rompf 	};
253b00055aaSStefan Rompf 
254b00055aaSStefan Rompf 	if (dev->operstate != operstate) {
255b00055aaSStefan Rompf 		write_lock_bh(&dev_base_lock);
256b00055aaSStefan Rompf 		dev->operstate = operstate;
257b00055aaSStefan Rompf 		write_unlock_bh(&dev_base_lock);
258b00055aaSStefan Rompf 		netdev_state_change(dev);
259b00055aaSStefan Rompf 	}
260b00055aaSStefan Rompf }
261b00055aaSStefan Rompf 
262b60c5115SThomas Graf static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
263b60c5115SThomas Graf 				 struct net_device_stats *b)
2641da177e4SLinus Torvalds {
265b60c5115SThomas Graf 	a->rx_packets = b->rx_packets;
266b60c5115SThomas Graf 	a->tx_packets = b->tx_packets;
267b60c5115SThomas Graf 	a->rx_bytes = b->rx_bytes;
268b60c5115SThomas Graf 	a->tx_bytes = b->tx_bytes;
269b60c5115SThomas Graf 	a->rx_errors = b->rx_errors;
270b60c5115SThomas Graf 	a->tx_errors = b->tx_errors;
271b60c5115SThomas Graf 	a->rx_dropped = b->rx_dropped;
272b60c5115SThomas Graf 	a->tx_dropped = b->tx_dropped;
273b60c5115SThomas Graf 
274b60c5115SThomas Graf 	a->multicast = b->multicast;
275b60c5115SThomas Graf 	a->collisions = b->collisions;
276b60c5115SThomas Graf 
277b60c5115SThomas Graf 	a->rx_length_errors = b->rx_length_errors;
278b60c5115SThomas Graf 	a->rx_over_errors = b->rx_over_errors;
279b60c5115SThomas Graf 	a->rx_crc_errors = b->rx_crc_errors;
280b60c5115SThomas Graf 	a->rx_frame_errors = b->rx_frame_errors;
281b60c5115SThomas Graf 	a->rx_fifo_errors = b->rx_fifo_errors;
282b60c5115SThomas Graf 	a->rx_missed_errors = b->rx_missed_errors;
283b60c5115SThomas Graf 
284b60c5115SThomas Graf 	a->tx_aborted_errors = b->tx_aborted_errors;
285b60c5115SThomas Graf 	a->tx_carrier_errors = b->tx_carrier_errors;
286b60c5115SThomas Graf 	a->tx_fifo_errors = b->tx_fifo_errors;
287b60c5115SThomas Graf 	a->tx_heartbeat_errors = b->tx_heartbeat_errors;
288b60c5115SThomas Graf 	a->tx_window_errors = b->tx_window_errors;
289b60c5115SThomas Graf 
290b60c5115SThomas Graf 	a->rx_compressed = b->rx_compressed;
291b60c5115SThomas Graf 	a->tx_compressed = b->tx_compressed;
292b60c5115SThomas Graf };
293b60c5115SThomas Graf 
294339bf98fSThomas Graf static inline size_t if_nlmsg_size(int iwbuflen)
295339bf98fSThomas Graf {
296339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
297339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
298339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
299339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rtnl_link_ifmap))
300339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rtnl_link_stats))
301339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
302339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
303339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_TXQLEN */
304339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_WEIGHT */
305339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MTU */
306339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_LINK */
307339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MASTER */
308339bf98fSThomas Graf 	       + nla_total_size(1) /* IFLA_OPERSTATE */
309339bf98fSThomas Graf 	       + nla_total_size(1) /* IFLA_LINKMODE */
310339bf98fSThomas Graf 	       + nla_total_size(iwbuflen);
311339bf98fSThomas Graf }
312339bf98fSThomas Graf 
313b60c5115SThomas Graf static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
314b60c5115SThomas Graf 			    void *iwbuf, int iwbuflen, int type, u32 pid,
315b60c5115SThomas Graf 			    u32 seq, u32 change, unsigned int flags)
316b60c5115SThomas Graf {
317b60c5115SThomas Graf 	struct ifinfomsg *ifm;
3181da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
3191da177e4SLinus Torvalds 
320b60c5115SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
321b60c5115SThomas Graf 	if (nlh == NULL)
32226932566SPatrick McHardy 		return -EMSGSIZE;
3231da177e4SLinus Torvalds 
324b60c5115SThomas Graf 	ifm = nlmsg_data(nlh);
325b60c5115SThomas Graf 	ifm->ifi_family = AF_UNSPEC;
326b60c5115SThomas Graf 	ifm->__ifi_pad = 0;
327b60c5115SThomas Graf 	ifm->ifi_type = dev->type;
328b60c5115SThomas Graf 	ifm->ifi_index = dev->ifindex;
329b60c5115SThomas Graf 	ifm->ifi_flags = dev_get_flags(dev);
330b60c5115SThomas Graf 	ifm->ifi_change = change;
3311da177e4SLinus Torvalds 
332b60c5115SThomas Graf 	NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
333b60c5115SThomas Graf 	NLA_PUT_U32(skb, IFLA_TXQLEN, dev->tx_queue_len);
334b60c5115SThomas Graf 	NLA_PUT_U32(skb, IFLA_WEIGHT, dev->weight);
335b60c5115SThomas Graf 	NLA_PUT_U8(skb, IFLA_OPERSTATE,
336b60c5115SThomas Graf 		   netif_running(dev) ? dev->operstate : IF_OPER_DOWN);
337b60c5115SThomas Graf 	NLA_PUT_U8(skb, IFLA_LINKMODE, dev->link_mode);
338b60c5115SThomas Graf 	NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
3391da177e4SLinus Torvalds 
340b60c5115SThomas Graf 	if (dev->ifindex != dev->iflink)
341b60c5115SThomas Graf 		NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
3421da177e4SLinus Torvalds 
343b60c5115SThomas Graf 	if (dev->master)
344b60c5115SThomas Graf 		NLA_PUT_U32(skb, IFLA_MASTER, dev->master->ifindex);
345b60c5115SThomas Graf 
346b60c5115SThomas Graf 	if (dev->qdisc_sleeping)
347b60c5115SThomas Graf 		NLA_PUT_STRING(skb, IFLA_QDISC, dev->qdisc_sleeping->ops->id);
348b00055aaSStefan Rompf 
349b00055aaSStefan Rompf 	if (1) {
3501da177e4SLinus Torvalds 		struct rtnl_link_ifmap map = {
3511da177e4SLinus Torvalds 			.mem_start   = dev->mem_start,
3521da177e4SLinus Torvalds 			.mem_end     = dev->mem_end,
3531da177e4SLinus Torvalds 			.base_addr   = dev->base_addr,
3541da177e4SLinus Torvalds 			.irq         = dev->irq,
3551da177e4SLinus Torvalds 			.dma         = dev->dma,
3561da177e4SLinus Torvalds 			.port        = dev->if_port,
3571da177e4SLinus Torvalds 		};
358b60c5115SThomas Graf 		NLA_PUT(skb, IFLA_MAP, sizeof(map), &map);
3591da177e4SLinus Torvalds 	}
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds 	if (dev->addr_len) {
362b60c5115SThomas Graf 		NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
363b60c5115SThomas Graf 		NLA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast);
3641da177e4SLinus Torvalds 	}
3651da177e4SLinus Torvalds 
3661da177e4SLinus Torvalds 	if (dev->get_stats) {
367b60c5115SThomas Graf 		struct net_device_stats *stats = dev->get_stats(dev);
3681da177e4SLinus Torvalds 		if (stats) {
369b60c5115SThomas Graf 			struct nlattr *attr;
3701da177e4SLinus Torvalds 
371b60c5115SThomas Graf 			attr = nla_reserve(skb, IFLA_STATS,
372b60c5115SThomas Graf 					   sizeof(struct rtnl_link_stats));
373b60c5115SThomas Graf 			if (attr == NULL)
374b60c5115SThomas Graf 				goto nla_put_failure;
375b60c5115SThomas Graf 
376b60c5115SThomas Graf 			copy_rtnl_link_stats(nla_data(attr), stats);
3771da177e4SLinus Torvalds 		}
3781da177e4SLinus Torvalds 	}
3791da177e4SLinus Torvalds 
380b60c5115SThomas Graf 	if (iwbuf)
381b60c5115SThomas Graf 		NLA_PUT(skb, IFLA_WIRELESS, iwbuflen, iwbuf);
382b60c5115SThomas Graf 
383b60c5115SThomas Graf 	return nlmsg_end(skb, nlh);
384b60c5115SThomas Graf 
385b60c5115SThomas Graf nla_put_failure:
38626932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
38726932566SPatrick McHardy 	return -EMSGSIZE;
3881da177e4SLinus Torvalds }
3891da177e4SLinus Torvalds 
390b60c5115SThomas Graf static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
3911da177e4SLinus Torvalds {
3921da177e4SLinus Torvalds 	int idx;
3931da177e4SLinus Torvalds 	int s_idx = cb->args[0];
3941da177e4SLinus Torvalds 	struct net_device *dev;
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds 	read_lock(&dev_base_lock);
3971da177e4SLinus Torvalds 	for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
3981da177e4SLinus Torvalds 		if (idx < s_idx)
3991da177e4SLinus Torvalds 			continue;
400b60c5115SThomas Graf 		if (rtnl_fill_ifinfo(skb, dev, NULL, 0, RTM_NEWLINK,
401b6544c0bSJamal Hadi Salim 				     NETLINK_CB(cb->skb).pid,
402b60c5115SThomas Graf 				     cb->nlh->nlmsg_seq, 0, NLM_F_MULTI) <= 0)
4031da177e4SLinus Torvalds 			break;
4041da177e4SLinus Torvalds 	}
4051da177e4SLinus Torvalds 	read_unlock(&dev_base_lock);
4061da177e4SLinus Torvalds 	cb->args[0] = idx;
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds 	return skb->len;
4091da177e4SLinus Torvalds }
4101da177e4SLinus Torvalds 
411da5e0494SThomas Graf static struct nla_policy ifla_policy[IFLA_MAX+1] __read_mostly = {
4125176f91eSThomas Graf 	[IFLA_IFNAME]		= { .type = NLA_STRING, .len = IFNAMSIZ-1 },
4135176f91eSThomas Graf 	[IFLA_MAP]		= { .len = sizeof(struct rtnl_link_ifmap) },
414da5e0494SThomas Graf 	[IFLA_MTU]		= { .type = NLA_U32 },
415da5e0494SThomas Graf 	[IFLA_TXQLEN]		= { .type = NLA_U32 },
416da5e0494SThomas Graf 	[IFLA_WEIGHT]		= { .type = NLA_U32 },
417da5e0494SThomas Graf 	[IFLA_OPERSTATE]	= { .type = NLA_U8 },
418da5e0494SThomas Graf 	[IFLA_LINKMODE]		= { .type = NLA_U8 },
419da5e0494SThomas Graf };
4201da177e4SLinus Torvalds 
421da5e0494SThomas Graf static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
422da5e0494SThomas Graf {
423da5e0494SThomas Graf 	struct ifinfomsg *ifm;
424da5e0494SThomas Graf 	struct net_device *dev;
425da5e0494SThomas Graf 	int err, send_addr_notify = 0, modified = 0;
426da5e0494SThomas Graf 	struct nlattr *tb[IFLA_MAX+1];
4271da177e4SLinus Torvalds 	char ifname[IFNAMSIZ];
4281da177e4SLinus Torvalds 
429da5e0494SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
430da5e0494SThomas Graf 	if (err < 0)
431da5e0494SThomas Graf 		goto errout;
4321da177e4SLinus Torvalds 
4335176f91eSThomas Graf 	if (tb[IFLA_IFNAME])
4345176f91eSThomas Graf 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
43578e5b891SPatrick McHardy 	else
43678e5b891SPatrick McHardy 		ifname[0] = '\0';
4371da177e4SLinus Torvalds 
4381da177e4SLinus Torvalds 	err = -EINVAL;
439da5e0494SThomas Graf 	ifm = nlmsg_data(nlh);
440da5e0494SThomas Graf 	if (ifm->ifi_index >= 0)
441da5e0494SThomas Graf 		dev = dev_get_by_index(ifm->ifi_index);
442da5e0494SThomas Graf 	else if (tb[IFLA_IFNAME])
443da5e0494SThomas Graf 		dev = dev_get_by_name(ifname);
444da5e0494SThomas Graf 	else
445da5e0494SThomas Graf 		goto errout;
4461da177e4SLinus Torvalds 
447da5e0494SThomas Graf 	if (dev == NULL) {
448da5e0494SThomas Graf 		err = -ENODEV;
449da5e0494SThomas Graf 		goto errout;
450da5e0494SThomas Graf 	}
4511da177e4SLinus Torvalds 
452da5e0494SThomas Graf 	if (tb[IFLA_ADDRESS] &&
453da5e0494SThomas Graf 	    nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
454da5e0494SThomas Graf 		goto errout_dev;
455da5e0494SThomas Graf 
456da5e0494SThomas Graf 	if (tb[IFLA_BROADCAST] &&
457da5e0494SThomas Graf 	    nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
458da5e0494SThomas Graf 		goto errout_dev;
459da5e0494SThomas Graf 
460da5e0494SThomas Graf 	if (tb[IFLA_MAP]) {
4611da177e4SLinus Torvalds 		struct rtnl_link_ifmap *u_map;
4621da177e4SLinus Torvalds 		struct ifmap k_map;
4631da177e4SLinus Torvalds 
4641da177e4SLinus Torvalds 		if (!dev->set_config) {
4651da177e4SLinus Torvalds 			err = -EOPNOTSUPP;
466da5e0494SThomas Graf 			goto errout_dev;
4671da177e4SLinus Torvalds 		}
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds 		if (!netif_device_present(dev)) {
4701da177e4SLinus Torvalds 			err = -ENODEV;
471da5e0494SThomas Graf 			goto errout_dev;
4721da177e4SLinus Torvalds 		}
4731da177e4SLinus Torvalds 
474da5e0494SThomas Graf 		u_map = nla_data(tb[IFLA_MAP]);
4751da177e4SLinus Torvalds 		k_map.mem_start = (unsigned long) u_map->mem_start;
4761da177e4SLinus Torvalds 		k_map.mem_end = (unsigned long) u_map->mem_end;
4771da177e4SLinus Torvalds 		k_map.base_addr = (unsigned short) u_map->base_addr;
4781da177e4SLinus Torvalds 		k_map.irq = (unsigned char) u_map->irq;
4791da177e4SLinus Torvalds 		k_map.dma = (unsigned char) u_map->dma;
4801da177e4SLinus Torvalds 		k_map.port = (unsigned char) u_map->port;
4811da177e4SLinus Torvalds 
4821da177e4SLinus Torvalds 		err = dev->set_config(dev, &k_map);
483da5e0494SThomas Graf 		if (err < 0)
484da5e0494SThomas Graf 			goto errout_dev;
4851da177e4SLinus Torvalds 
486da5e0494SThomas Graf 		modified = 1;
4871da177e4SLinus Torvalds 	}
4881da177e4SLinus Torvalds 
489da5e0494SThomas Graf 	if (tb[IFLA_ADDRESS]) {
49070f8e78eSDavid S. Miller 		struct sockaddr *sa;
49170f8e78eSDavid S. Miller 		int len;
49270f8e78eSDavid S. Miller 
4931da177e4SLinus Torvalds 		if (!dev->set_mac_address) {
4941da177e4SLinus Torvalds 			err = -EOPNOTSUPP;
495da5e0494SThomas Graf 			goto errout_dev;
4961da177e4SLinus Torvalds 		}
497da5e0494SThomas Graf 
4981da177e4SLinus Torvalds 		if (!netif_device_present(dev)) {
4991da177e4SLinus Torvalds 			err = -ENODEV;
500da5e0494SThomas Graf 			goto errout_dev;
5011da177e4SLinus Torvalds 		}
5021da177e4SLinus Torvalds 
50370f8e78eSDavid S. Miller 		len = sizeof(sa_family_t) + dev->addr_len;
50470f8e78eSDavid S. Miller 		sa = kmalloc(len, GFP_KERNEL);
50570f8e78eSDavid S. Miller 		if (!sa) {
50670f8e78eSDavid S. Miller 			err = -ENOMEM;
507da5e0494SThomas Graf 			goto errout_dev;
50870f8e78eSDavid S. Miller 		}
50970f8e78eSDavid S. Miller 		sa->sa_family = dev->type;
510da5e0494SThomas Graf 		memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
51170f8e78eSDavid S. Miller 		       dev->addr_len);
51270f8e78eSDavid S. Miller 		err = dev->set_mac_address(dev, sa);
51370f8e78eSDavid S. Miller 		kfree(sa);
5141da177e4SLinus Torvalds 		if (err)
515da5e0494SThomas Graf 			goto errout_dev;
5161da177e4SLinus Torvalds 		send_addr_notify = 1;
517da5e0494SThomas Graf 		modified = 1;
5181da177e4SLinus Torvalds 	}
5191da177e4SLinus Torvalds 
520da5e0494SThomas Graf 	if (tb[IFLA_MTU]) {
521da5e0494SThomas Graf 		err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
522da5e0494SThomas Graf 		if (err < 0)
523da5e0494SThomas Graf 			goto errout_dev;
524da5e0494SThomas Graf 		modified = 1;
5251da177e4SLinus Torvalds 	}
5261da177e4SLinus Torvalds 
527da5e0494SThomas Graf 	/*
528da5e0494SThomas Graf 	 * Interface selected by interface index but interface
529da5e0494SThomas Graf 	 * name provided implies that a name change has been
530da5e0494SThomas Graf 	 * requested.
531da5e0494SThomas Graf 	 */
532da5e0494SThomas Graf 	if (ifm->ifi_index >= 0 && ifname[0]) {
5331da177e4SLinus Torvalds 		err = dev_change_name(dev, ifname);
534da5e0494SThomas Graf 		if (err < 0)
535da5e0494SThomas Graf 			goto errout_dev;
536da5e0494SThomas Graf 		modified = 1;
5371da177e4SLinus Torvalds 	}
5381da177e4SLinus Torvalds 
539711e2c33SJean Tourrilhes #ifdef CONFIG_NET_WIRELESS_RTNETLINK
540da5e0494SThomas Graf 	if (tb[IFLA_WIRELESS]) {
541711e2c33SJean Tourrilhes 		/* Call Wireless Extensions.
542711e2c33SJean Tourrilhes 		 * Various stuff checked in there... */
543da5e0494SThomas Graf 		err = wireless_rtnetlink_set(dev, nla_data(tb[IFLA_WIRELESS]),
544da5e0494SThomas Graf 					     nla_len(tb[IFLA_WIRELESS]));
545da5e0494SThomas Graf 		if (err < 0)
546da5e0494SThomas Graf 			goto errout_dev;
547711e2c33SJean Tourrilhes 	}
548711e2c33SJean Tourrilhes #endif	/* CONFIG_NET_WIRELESS_RTNETLINK */
549711e2c33SJean Tourrilhes 
550da5e0494SThomas Graf 	if (tb[IFLA_BROADCAST]) {
551da5e0494SThomas Graf 		nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
552da5e0494SThomas Graf 		send_addr_notify = 1;
553da5e0494SThomas Graf 	}
554da5e0494SThomas Graf 
555da5e0494SThomas Graf 
556da5e0494SThomas Graf 	if (ifm->ifi_flags)
557da5e0494SThomas Graf 		dev_change_flags(dev, ifm->ifi_flags);
558da5e0494SThomas Graf 
559da5e0494SThomas Graf 	if (tb[IFLA_TXQLEN])
560da5e0494SThomas Graf 		dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
561da5e0494SThomas Graf 
562da5e0494SThomas Graf 	if (tb[IFLA_WEIGHT])
563da5e0494SThomas Graf 		dev->weight = nla_get_u32(tb[IFLA_WEIGHT]);
564da5e0494SThomas Graf 
565da5e0494SThomas Graf 	if (tb[IFLA_OPERSTATE])
566da5e0494SThomas Graf 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
567da5e0494SThomas Graf 
568da5e0494SThomas Graf 	if (tb[IFLA_LINKMODE]) {
569da5e0494SThomas Graf 		write_lock_bh(&dev_base_lock);
570da5e0494SThomas Graf 		dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
571da5e0494SThomas Graf 		write_unlock_bh(&dev_base_lock);
572da5e0494SThomas Graf 	}
573da5e0494SThomas Graf 
5741da177e4SLinus Torvalds 	err = 0;
5751da177e4SLinus Torvalds 
576da5e0494SThomas Graf errout_dev:
577da5e0494SThomas Graf 	if (err < 0 && modified && net_ratelimit())
578da5e0494SThomas Graf 		printk(KERN_WARNING "A link change request failed with "
579da5e0494SThomas Graf 		       "some changes comitted already. Interface %s may "
580da5e0494SThomas Graf 		       "have been left with an inconsistent configuration, "
581da5e0494SThomas Graf 		       "please check.\n", dev->name);
582da5e0494SThomas Graf 
5831da177e4SLinus Torvalds 	if (send_addr_notify)
5841da177e4SLinus Torvalds 		call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
5851da177e4SLinus Torvalds 
5861da177e4SLinus Torvalds 	dev_put(dev);
587da5e0494SThomas Graf errout:
5881da177e4SLinus Torvalds 	return err;
5891da177e4SLinus Torvalds }
5901da177e4SLinus Torvalds 
591b60c5115SThomas Graf static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
592711e2c33SJean Tourrilhes {
593b60c5115SThomas Graf 	struct ifinfomsg *ifm;
594b60c5115SThomas Graf 	struct nlattr *tb[IFLA_MAX+1];
595b60c5115SThomas Graf 	struct net_device *dev = NULL;
596b60c5115SThomas Graf 	struct sk_buff *nskb;
597b60c5115SThomas Graf 	char *iw_buf = NULL, *iw = NULL;
598711e2c33SJean Tourrilhes 	int iw_buf_len = 0;
599339bf98fSThomas Graf 	int err;
600711e2c33SJean Tourrilhes 
601b60c5115SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
602b60c5115SThomas Graf 	if (err < 0)
6039918f230SEric Sesterhenn 		return err;
604b60c5115SThomas Graf 
605b60c5115SThomas Graf 	ifm = nlmsg_data(nlh);
606b60c5115SThomas Graf 	if (ifm->ifi_index >= 0) {
607711e2c33SJean Tourrilhes 		dev = dev_get_by_index(ifm->ifi_index);
608b60c5115SThomas Graf 		if (dev == NULL)
609711e2c33SJean Tourrilhes 			return -ENODEV;
610b60c5115SThomas Graf 	} else
611b60c5115SThomas Graf 		return -EINVAL;
612b60c5115SThomas Graf 
613711e2c33SJean Tourrilhes 
614711e2c33SJean Tourrilhes #ifdef CONFIG_NET_WIRELESS_RTNETLINK
615b60c5115SThomas Graf 	if (tb[IFLA_WIRELESS]) {
616711e2c33SJean Tourrilhes 		/* Call Wireless Extensions. We need to know the size before
617711e2c33SJean Tourrilhes 		 * we can alloc. Various stuff checked in there... */
618b60c5115SThomas Graf 		err = wireless_rtnetlink_get(dev, nla_data(tb[IFLA_WIRELESS]),
619b60c5115SThomas Graf 					     nla_len(tb[IFLA_WIRELESS]),
620b60c5115SThomas Graf 					     &iw_buf, &iw_buf_len);
621b60c5115SThomas Graf 		if (err < 0)
622b60c5115SThomas Graf 			goto errout;
623b60c5115SThomas Graf 
624c2805fbbSJean Tourrilhes 		/* Payload is at an offset in buffer */
625c2805fbbSJean Tourrilhes 		iw = iw_buf + IW_EV_POINT_OFF;
626711e2c33SJean Tourrilhes 	}
627711e2c33SJean Tourrilhes #endif	/* CONFIG_NET_WIRELESS_RTNETLINK */
628711e2c33SJean Tourrilhes 
629339bf98fSThomas Graf 	nskb = nlmsg_new(if_nlmsg_size(iw_buf_len), GFP_KERNEL);
630b60c5115SThomas Graf 	if (nskb == NULL) {
631b60c5115SThomas Graf 		err = -ENOBUFS;
632b60c5115SThomas Graf 		goto errout;
633b60c5115SThomas Graf 	}
634711e2c33SJean Tourrilhes 
635b60c5115SThomas Graf 	err = rtnl_fill_ifinfo(nskb, dev, iw, iw_buf_len, RTM_NEWLINK,
636b60c5115SThomas Graf 			       NETLINK_CB(skb).pid, nlh->nlmsg_seq, 0, 0);
63726932566SPatrick McHardy 	if (err < 0) {
63826932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in if_nlmsg_size */
63926932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
64026932566SPatrick McHardy 		kfree_skb(nskb);
64126932566SPatrick McHardy 		goto errout;
64226932566SPatrick McHardy 	}
643b974179aSPatrick McHardy 	err = rtnl_unicast(nskb, NETLINK_CB(skb).pid);
644b60c5115SThomas Graf errout:
645711e2c33SJean Tourrilhes 	kfree(iw_buf);
646711e2c33SJean Tourrilhes 	dev_put(dev);
647b60c5115SThomas Graf 
648711e2c33SJean Tourrilhes 	return err;
649711e2c33SJean Tourrilhes }
650711e2c33SJean Tourrilhes 
651b60c5115SThomas Graf static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
6521da177e4SLinus Torvalds {
6531da177e4SLinus Torvalds 	int idx;
6541da177e4SLinus Torvalds 	int s_idx = cb->family;
6551da177e4SLinus Torvalds 
6561da177e4SLinus Torvalds 	if (s_idx == 0)
6571da177e4SLinus Torvalds 		s_idx = 1;
6581da177e4SLinus Torvalds 	for (idx=1; idx<NPROTO; idx++) {
6591da177e4SLinus Torvalds 		int type = cb->nlh->nlmsg_type-RTM_BASE;
6601da177e4SLinus Torvalds 		if (idx < s_idx || idx == PF_PACKET)
6611da177e4SLinus Torvalds 			continue;
6621da177e4SLinus Torvalds 		if (rtnetlink_links[idx] == NULL ||
6631da177e4SLinus Torvalds 		    rtnetlink_links[idx][type].dumpit == NULL)
6641da177e4SLinus Torvalds 			continue;
6651da177e4SLinus Torvalds 		if (idx > s_idx)
6661da177e4SLinus Torvalds 			memset(&cb->args[0], 0, sizeof(cb->args));
6671da177e4SLinus Torvalds 		if (rtnetlink_links[idx][type].dumpit(skb, cb))
6681da177e4SLinus Torvalds 			break;
6691da177e4SLinus Torvalds 	}
6701da177e4SLinus Torvalds 	cb->family = idx;
6711da177e4SLinus Torvalds 
6721da177e4SLinus Torvalds 	return skb->len;
6731da177e4SLinus Torvalds }
6741da177e4SLinus Torvalds 
6751da177e4SLinus Torvalds void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change)
6761da177e4SLinus Torvalds {
6771da177e4SLinus Torvalds 	struct sk_buff *skb;
6780ec6d3f4SThomas Graf 	int err = -ENOBUFS;
6791da177e4SLinus Torvalds 
680339bf98fSThomas Graf 	skb = nlmsg_new(if_nlmsg_size(0), GFP_KERNEL);
6810ec6d3f4SThomas Graf 	if (skb == NULL)
6820ec6d3f4SThomas Graf 		goto errout;
6831da177e4SLinus Torvalds 
6840ec6d3f4SThomas Graf 	err = rtnl_fill_ifinfo(skb, dev, NULL, 0, type, 0, 0, change, 0);
68526932566SPatrick McHardy 	if (err < 0) {
68626932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in if_nlmsg_size() */
68726932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
68826932566SPatrick McHardy 		kfree_skb(skb);
68926932566SPatrick McHardy 		goto errout;
69026932566SPatrick McHardy 	}
6910ec6d3f4SThomas Graf 	err = rtnl_notify(skb, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
6920ec6d3f4SThomas Graf errout:
6930ec6d3f4SThomas Graf 	if (err < 0)
6940ec6d3f4SThomas Graf 		rtnl_set_sk_err(RTNLGRP_LINK, err);
6951da177e4SLinus Torvalds }
6961da177e4SLinus Torvalds 
6971da177e4SLinus Torvalds /* Protected by RTNL sempahore.  */
6981da177e4SLinus Torvalds static struct rtattr **rta_buf;
6991da177e4SLinus Torvalds static int rtattr_max;
7001da177e4SLinus Torvalds 
7011da177e4SLinus Torvalds /* Process one rtnetlink message. */
7021da177e4SLinus Torvalds 
7031da177e4SLinus Torvalds static __inline__ int
7041da177e4SLinus Torvalds rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
7051da177e4SLinus Torvalds {
7061da177e4SLinus Torvalds 	struct rtnetlink_link *link;
7071da177e4SLinus Torvalds 	struct rtnetlink_link *link_tab;
7081da177e4SLinus Torvalds 	int sz_idx, kind;
7091da177e4SLinus Torvalds 	int min_len;
7101da177e4SLinus Torvalds 	int family;
7111da177e4SLinus Torvalds 	int type;
7121da177e4SLinus Torvalds 	int err;
7131da177e4SLinus Torvalds 
7141da177e4SLinus Torvalds 	/* Only requests are handled by kernel now */
7151da177e4SLinus Torvalds 	if (!(nlh->nlmsg_flags&NLM_F_REQUEST))
7161da177e4SLinus Torvalds 		return 0;
7171da177e4SLinus Torvalds 
7181da177e4SLinus Torvalds 	type = nlh->nlmsg_type;
7191da177e4SLinus Torvalds 
7201da177e4SLinus Torvalds 	/* A control message: ignore them */
7211da177e4SLinus Torvalds 	if (type < RTM_BASE)
7221da177e4SLinus Torvalds 		return 0;
7231da177e4SLinus Torvalds 
7241da177e4SLinus Torvalds 	/* Unknown message: reply with EINVAL */
7251da177e4SLinus Torvalds 	if (type > RTM_MAX)
7261da177e4SLinus Torvalds 		goto err_inval;
7271da177e4SLinus Torvalds 
7281da177e4SLinus Torvalds 	type -= RTM_BASE;
7291da177e4SLinus Torvalds 
7301da177e4SLinus Torvalds 	/* All the messages must have at least 1 byte length */
7311da177e4SLinus Torvalds 	if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg)))
7321da177e4SLinus Torvalds 		return 0;
7331da177e4SLinus Torvalds 
7341da177e4SLinus Torvalds 	family = ((struct rtgenmsg*)NLMSG_DATA(nlh))->rtgen_family;
7351da177e4SLinus Torvalds 	if (family >= NPROTO) {
7361da177e4SLinus Torvalds 		*errp = -EAFNOSUPPORT;
7371da177e4SLinus Torvalds 		return -1;
7381da177e4SLinus Torvalds 	}
7391da177e4SLinus Torvalds 
7401da177e4SLinus Torvalds 	link_tab = rtnetlink_links[family];
7411da177e4SLinus Torvalds 	if (link_tab == NULL)
7421da177e4SLinus Torvalds 		link_tab = rtnetlink_links[PF_UNSPEC];
7431da177e4SLinus Torvalds 	link = &link_tab[type];
7441da177e4SLinus Torvalds 
7451da177e4SLinus Torvalds 	sz_idx = type>>2;
7461da177e4SLinus Torvalds 	kind = type&3;
7471da177e4SLinus Torvalds 
748c7bdb545SDarrel Goeddel 	if (kind != 2 && security_netlink_recv(skb, CAP_NET_ADMIN)) {
7491da177e4SLinus Torvalds 		*errp = -EPERM;
7501da177e4SLinus Torvalds 		return -1;
7511da177e4SLinus Torvalds 	}
7521da177e4SLinus Torvalds 
7531da177e4SLinus Torvalds 	if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
7541da177e4SLinus Torvalds 		if (link->dumpit == NULL)
7551da177e4SLinus Torvalds 			link = &(rtnetlink_links[PF_UNSPEC][type]);
7561da177e4SLinus Torvalds 
7571da177e4SLinus Torvalds 		if (link->dumpit == NULL)
7581da177e4SLinus Torvalds 			goto err_inval;
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds 		if ((*errp = netlink_dump_start(rtnl, skb, nlh,
761a8f74b22SThomas Graf 						link->dumpit, NULL)) != 0) {
7621da177e4SLinus Torvalds 			return -1;
7631da177e4SLinus Torvalds 		}
7649ac4a169SThomas Graf 
7659ac4a169SThomas Graf 		netlink_queue_skip(nlh, skb);
7661da177e4SLinus Torvalds 		return -1;
7671da177e4SLinus Torvalds 	}
7681da177e4SLinus Torvalds 
7691da177e4SLinus Torvalds 	memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *)));
7701da177e4SLinus Torvalds 
7711da177e4SLinus Torvalds 	min_len = rtm_min[sz_idx];
7721da177e4SLinus Torvalds 	if (nlh->nlmsg_len < min_len)
7731da177e4SLinus Torvalds 		goto err_inval;
7741da177e4SLinus Torvalds 
7751da177e4SLinus Torvalds 	if (nlh->nlmsg_len > min_len) {
7761da177e4SLinus Torvalds 		int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
7771da177e4SLinus Torvalds 		struct rtattr *attr = (void*)nlh + NLMSG_ALIGN(min_len);
7781da177e4SLinus Torvalds 
7791da177e4SLinus Torvalds 		while (RTA_OK(attr, attrlen)) {
7801da177e4SLinus Torvalds 			unsigned flavor = attr->rta_type;
7811da177e4SLinus Torvalds 			if (flavor) {
7821da177e4SLinus Torvalds 				if (flavor > rta_max[sz_idx])
7831da177e4SLinus Torvalds 					goto err_inval;
7841da177e4SLinus Torvalds 				rta_buf[flavor-1] = attr;
7851da177e4SLinus Torvalds 			}
7861da177e4SLinus Torvalds 			attr = RTA_NEXT(attr, attrlen);
7871da177e4SLinus Torvalds 		}
7881da177e4SLinus Torvalds 	}
7891da177e4SLinus Torvalds 
7901da177e4SLinus Torvalds 	if (link->doit == NULL)
7911da177e4SLinus Torvalds 		link = &(rtnetlink_links[PF_UNSPEC][type]);
7921da177e4SLinus Torvalds 	if (link->doit == NULL)
7931da177e4SLinus Torvalds 		goto err_inval;
7941da177e4SLinus Torvalds 	err = link->doit(skb, nlh, (void *)&rta_buf[0]);
7951da177e4SLinus Torvalds 
7961da177e4SLinus Torvalds 	*errp = err;
7971da177e4SLinus Torvalds 	return err;
7981da177e4SLinus Torvalds 
7991da177e4SLinus Torvalds err_inval:
8001da177e4SLinus Torvalds 	*errp = -EINVAL;
8011da177e4SLinus Torvalds 	return -1;
8021da177e4SLinus Torvalds }
8031da177e4SLinus Torvalds 
8041da177e4SLinus Torvalds static void rtnetlink_rcv(struct sock *sk, int len)
8051da177e4SLinus Torvalds {
8069ac4a169SThomas Graf 	unsigned int qlen = 0;
8072a0a6ebeSHerbert Xu 
8081da177e4SLinus Torvalds 	do {
8096756ae4bSStephen Hemminger 		mutex_lock(&rtnl_mutex);
8109ac4a169SThomas Graf 		netlink_run_queue(sk, &qlen, &rtnetlink_rcv_msg);
8116756ae4bSStephen Hemminger 		mutex_unlock(&rtnl_mutex);
8121da177e4SLinus Torvalds 
8131da177e4SLinus Torvalds 		netdev_run_todo();
8142a0a6ebeSHerbert Xu 	} while (qlen);
8151da177e4SLinus Torvalds }
8161da177e4SLinus Torvalds 
817db46edc6SThomas Graf static struct rtnetlink_link link_rtnetlink_table[RTM_NR_MSGTYPES] =
8181da177e4SLinus Torvalds {
819b60c5115SThomas Graf 	[RTM_GETLINK     - RTM_BASE] = { .doit   = rtnl_getlink,
820b60c5115SThomas Graf 					 .dumpit = rtnl_dump_ifinfo	 },
821da5e0494SThomas Graf 	[RTM_SETLINK     - RTM_BASE] = { .doit   = rtnl_setlink		 },
822b60c5115SThomas Graf 	[RTM_GETADDR     - RTM_BASE] = { .dumpit = rtnl_dump_all	 },
823b60c5115SThomas Graf 	[RTM_GETROUTE    - RTM_BASE] = { .dumpit = rtnl_dump_all	 },
8241da177e4SLinus Torvalds 	[RTM_NEWNEIGH    - RTM_BASE] = { .doit   = neigh_add		 },
8251da177e4SLinus Torvalds 	[RTM_DELNEIGH    - RTM_BASE] = { .doit   = neigh_delete		 },
826240eed95SThomas Graf 	[RTM_GETNEIGH    - RTM_BASE] = { .dumpit = neigh_dump_info	 },
82714c0b97dSThomas Graf #ifdef CONFIG_FIB_RULES
82814c0b97dSThomas Graf 	[RTM_NEWRULE     - RTM_BASE] = { .doit   = fib_nl_newrule	 },
82914c0b97dSThomas Graf 	[RTM_DELRULE     - RTM_BASE] = { .doit   = fib_nl_delrule	 },
83014c0b97dSThomas Graf #endif
831b60c5115SThomas Graf 	[RTM_GETRULE     - RTM_BASE] = { .dumpit = rtnl_dump_all	 },
832c7fb64dbSThomas Graf 	[RTM_GETNEIGHTBL - RTM_BASE] = { .dumpit = neightbl_dump_info	 },
833c7fb64dbSThomas Graf 	[RTM_SETNEIGHTBL - RTM_BASE] = { .doit   = neightbl_set		 },
8341da177e4SLinus Torvalds };
8351da177e4SLinus Torvalds 
8361da177e4SLinus Torvalds static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
8371da177e4SLinus Torvalds {
8381da177e4SLinus Torvalds 	struct net_device *dev = ptr;
8391da177e4SLinus Torvalds 	switch (event) {
8401da177e4SLinus Torvalds 	case NETDEV_UNREGISTER:
8411da177e4SLinus Torvalds 		rtmsg_ifinfo(RTM_DELLINK, dev, ~0U);
8421da177e4SLinus Torvalds 		break;
8431da177e4SLinus Torvalds 	case NETDEV_REGISTER:
8441da177e4SLinus Torvalds 		rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
8451da177e4SLinus Torvalds 		break;
8461da177e4SLinus Torvalds 	case NETDEV_UP:
8471da177e4SLinus Torvalds 	case NETDEV_DOWN:
8481da177e4SLinus Torvalds 		rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
8491da177e4SLinus Torvalds 		break;
8501da177e4SLinus Torvalds 	case NETDEV_CHANGE:
8511da177e4SLinus Torvalds 	case NETDEV_GOING_DOWN:
8521da177e4SLinus Torvalds 		break;
8531da177e4SLinus Torvalds 	default:
8541da177e4SLinus Torvalds 		rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
8551da177e4SLinus Torvalds 		break;
8561da177e4SLinus Torvalds 	}
8571da177e4SLinus Torvalds 	return NOTIFY_DONE;
8581da177e4SLinus Torvalds }
8591da177e4SLinus Torvalds 
8601da177e4SLinus Torvalds static struct notifier_block rtnetlink_dev_notifier = {
8611da177e4SLinus Torvalds 	.notifier_call	= rtnetlink_event,
8621da177e4SLinus Torvalds };
8631da177e4SLinus Torvalds 
8641da177e4SLinus Torvalds void __init rtnetlink_init(void)
8651da177e4SLinus Torvalds {
8661da177e4SLinus Torvalds 	int i;
8671da177e4SLinus Torvalds 
8681da177e4SLinus Torvalds 	rtattr_max = 0;
8691da177e4SLinus Torvalds 	for (i = 0; i < ARRAY_SIZE(rta_max); i++)
8701da177e4SLinus Torvalds 		if (rta_max[i] > rtattr_max)
8711da177e4SLinus Torvalds 			rtattr_max = rta_max[i];
8721da177e4SLinus Torvalds 	rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL);
8731da177e4SLinus Torvalds 	if (!rta_buf)
8741da177e4SLinus Torvalds 		panic("rtnetlink_init: cannot allocate rta_buf\n");
8751da177e4SLinus Torvalds 
87606628607SPatrick McHardy 	rtnl = netlink_kernel_create(NETLINK_ROUTE, RTNLGRP_MAX, rtnetlink_rcv,
87706628607SPatrick McHardy 				     THIS_MODULE);
8781da177e4SLinus Torvalds 	if (rtnl == NULL)
8791da177e4SLinus Torvalds 		panic("rtnetlink_init: cannot initialize rtnetlink\n");
8801da177e4SLinus Torvalds 	netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV);
8811da177e4SLinus Torvalds 	register_netdevice_notifier(&rtnetlink_dev_notifier);
8821da177e4SLinus Torvalds 	rtnetlink_links[PF_UNSPEC] = link_rtnetlink_table;
8831da177e4SLinus Torvalds 	rtnetlink_links[PF_PACKET] = link_rtnetlink_table;
8841da177e4SLinus Torvalds }
8851da177e4SLinus Torvalds 
8861da177e4SLinus Torvalds EXPORT_SYMBOL(__rta_fill);
8871da177e4SLinus Torvalds EXPORT_SYMBOL(rtattr_strlcpy);
8881da177e4SLinus Torvalds EXPORT_SYMBOL(rtattr_parse);
8891da177e4SLinus Torvalds EXPORT_SYMBOL(rtnetlink_links);
8901da177e4SLinus Torvalds EXPORT_SYMBOL(rtnetlink_put_metrics);
8911da177e4SLinus Torvalds EXPORT_SYMBOL(rtnl_lock);
8926756ae4bSStephen Hemminger EXPORT_SYMBOL(rtnl_trylock);
8931da177e4SLinus Torvalds EXPORT_SYMBOL(rtnl_unlock);
8942942e900SThomas Graf EXPORT_SYMBOL(rtnl_unicast);
89597676b6bSThomas Graf EXPORT_SYMBOL(rtnl_notify);
89697676b6bSThomas Graf EXPORT_SYMBOL(rtnl_set_sk_err);
897