xref: /openbmc/linux/net/core/rtnetlink.c (revision 2942e90050569525628a9f34e0daaa9b661b49cc)
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/sched.h>
251da177e4SLinus Torvalds #include <linux/timer.h>
261da177e4SLinus Torvalds #include <linux/string.h>
271da177e4SLinus Torvalds #include <linux/sockios.h>
281da177e4SLinus Torvalds #include <linux/net.h>
291da177e4SLinus Torvalds #include <linux/fcntl.h>
301da177e4SLinus Torvalds #include <linux/mm.h>
311da177e4SLinus Torvalds #include <linux/slab.h>
321da177e4SLinus Torvalds #include <linux/interrupt.h>
331da177e4SLinus Torvalds #include <linux/capability.h>
341da177e4SLinus Torvalds #include <linux/skbuff.h>
351da177e4SLinus Torvalds #include <linux/init.h>
361da177e4SLinus Torvalds #include <linux/security.h>
376756ae4bSStephen Hemminger #include <linux/mutex.h>
381823730fSThomas Graf #include <linux/if_addr.h>
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds #include <asm/uaccess.h>
411da177e4SLinus Torvalds #include <asm/system.h>
421da177e4SLinus Torvalds #include <asm/string.h>
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds #include <linux/inet.h>
451da177e4SLinus Torvalds #include <linux/netdevice.h>
461da177e4SLinus Torvalds #include <net/ip.h>
471da177e4SLinus Torvalds #include <net/protocol.h>
481da177e4SLinus Torvalds #include <net/arp.h>
491da177e4SLinus Torvalds #include <net/route.h>
501da177e4SLinus Torvalds #include <net/udp.h>
511da177e4SLinus Torvalds #include <net/sock.h>
521da177e4SLinus Torvalds #include <net/pkt_sched.h>
5314c0b97dSThomas Graf #include <net/fib_rules.h>
549ac4a169SThomas Graf #include <net/netlink.h>
55711e2c33SJean Tourrilhes #ifdef CONFIG_NET_WIRELESS_RTNETLINK
56711e2c33SJean Tourrilhes #include <linux/wireless.h>
57711e2c33SJean Tourrilhes #include <net/iw_handler.h>
58711e2c33SJean Tourrilhes #endif	/* CONFIG_NET_WIRELESS_RTNETLINK */
591da177e4SLinus Torvalds 
606756ae4bSStephen Hemminger static DEFINE_MUTEX(rtnl_mutex);
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 sock *rtnl;
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds struct rtnetlink_link * rtnetlink_links[NPROTO];
1011da177e4SLinus Torvalds 
102db46edc6SThomas Graf static const int rtm_min[RTM_NR_FAMILIES] =
1031da177e4SLinus Torvalds {
104f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWLINK)]      = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
105f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWADDR)]      = NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
106f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWROUTE)]     = NLMSG_LENGTH(sizeof(struct rtmsg)),
10714c0b97dSThomas Graf 	[RTM_FAM(RTM_NEWRULE)]      = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)),
108f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWQDISC)]     = NLMSG_LENGTH(sizeof(struct tcmsg)),
109f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWTCLASS)]    = NLMSG_LENGTH(sizeof(struct tcmsg)),
110f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWTFILTER)]   = NLMSG_LENGTH(sizeof(struct tcmsg)),
111f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWACTION)]    = NLMSG_LENGTH(sizeof(struct tcamsg)),
112f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWPREFIX)]    = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
113f90a0a74SThomas Graf 	[RTM_FAM(RTM_GETMULTICAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
114f90a0a74SThomas Graf 	[RTM_FAM(RTM_GETANYCAST)]   = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
1151da177e4SLinus Torvalds };
1161da177e4SLinus Torvalds 
117db46edc6SThomas Graf static const int rta_max[RTM_NR_FAMILIES] =
1181da177e4SLinus Torvalds {
119f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWLINK)]      = IFLA_MAX,
120f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWADDR)]      = IFA_MAX,
121f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWROUTE)]     = RTA_MAX,
12214c0b97dSThomas Graf 	[RTM_FAM(RTM_NEWRULE)]      = FRA_MAX,
123f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWQDISC)]     = TCA_MAX,
124f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWTCLASS)]    = TCA_MAX,
125f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWTFILTER)]   = TCA_MAX,
126f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWACTION)]    = TCAA_MAX,
1271da177e4SLinus Torvalds };
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
1301da177e4SLinus Torvalds {
1311da177e4SLinus Torvalds 	struct rtattr *rta;
1321da177e4SLinus Torvalds 	int size = RTA_LENGTH(attrlen);
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds 	rta = (struct rtattr*)skb_put(skb, RTA_ALIGN(size));
1351da177e4SLinus Torvalds 	rta->rta_type = attrtype;
1361da177e4SLinus Torvalds 	rta->rta_len = size;
1371da177e4SLinus Torvalds 	memcpy(RTA_DATA(rta), data, attrlen);
138b3563c4fSPatrick McHardy 	memset(RTA_DATA(rta) + attrlen, 0, RTA_ALIGN(size) - size);
1391da177e4SLinus Torvalds }
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds size_t rtattr_strlcpy(char *dest, const struct rtattr *rta, size_t size)
1421da177e4SLinus Torvalds {
1431da177e4SLinus Torvalds 	size_t ret = RTA_PAYLOAD(rta);
1441da177e4SLinus Torvalds 	char *src = RTA_DATA(rta);
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds 	if (ret > 0 && src[ret - 1] == '\0')
1471da177e4SLinus Torvalds 		ret--;
1481da177e4SLinus Torvalds 	if (size > 0) {
1491da177e4SLinus Torvalds 		size_t len = (ret >= size) ? size - 1 : ret;
1501da177e4SLinus Torvalds 		memset(dest, 0, size);
1511da177e4SLinus Torvalds 		memcpy(dest, src, len);
1521da177e4SLinus Torvalds 	}
1531da177e4SLinus Torvalds 	return ret;
1541da177e4SLinus Torvalds }
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds int rtnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo)
1571da177e4SLinus Torvalds {
1581da177e4SLinus Torvalds 	int err = 0;
1591da177e4SLinus Torvalds 
160ac6d439dSPatrick McHardy 	NETLINK_CB(skb).dst_group = group;
1611da177e4SLinus Torvalds 	if (echo)
1621da177e4SLinus Torvalds 		atomic_inc(&skb->users);
1631da177e4SLinus Torvalds 	netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
1641da177e4SLinus Torvalds 	if (echo)
1651da177e4SLinus Torvalds 		err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
1661da177e4SLinus Torvalds 	return err;
1671da177e4SLinus Torvalds }
1681da177e4SLinus Torvalds 
169*2942e900SThomas Graf int rtnl_unicast(struct sk_buff *skb, u32 pid)
170*2942e900SThomas Graf {
171*2942e900SThomas Graf 	return nlmsg_unicast(rtnl, skb, pid);
172*2942e900SThomas Graf }
173*2942e900SThomas Graf 
1741da177e4SLinus Torvalds int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
1751da177e4SLinus Torvalds {
1761da177e4SLinus Torvalds 	struct rtattr *mx = (struct rtattr*)skb->tail;
1771da177e4SLinus Torvalds 	int i;
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds 	RTA_PUT(skb, RTA_METRICS, 0, NULL);
1801da177e4SLinus Torvalds 	for (i=0; i<RTAX_MAX; i++) {
1811da177e4SLinus Torvalds 		if (metrics[i])
1821da177e4SLinus Torvalds 			RTA_PUT(skb, i+1, sizeof(u32), metrics+i);
1831da177e4SLinus Torvalds 	}
1841da177e4SLinus Torvalds 	mx->rta_len = skb->tail - (u8*)mx;
1851da177e4SLinus Torvalds 	if (mx->rta_len == RTA_LENGTH(0))
1861da177e4SLinus Torvalds 		skb_trim(skb, (u8*)mx - skb->data);
1871da177e4SLinus Torvalds 	return 0;
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds rtattr_failure:
1901da177e4SLinus Torvalds 	skb_trim(skb, (u8*)mx - skb->data);
1911da177e4SLinus Torvalds 	return -1;
1921da177e4SLinus Torvalds }
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds 
195b00055aaSStefan Rompf static void set_operstate(struct net_device *dev, unsigned char transition)
196b00055aaSStefan Rompf {
197b00055aaSStefan Rompf 	unsigned char operstate = dev->operstate;
198b00055aaSStefan Rompf 
199b00055aaSStefan Rompf 	switch(transition) {
200b00055aaSStefan Rompf 	case IF_OPER_UP:
201b00055aaSStefan Rompf 		if ((operstate == IF_OPER_DORMANT ||
202b00055aaSStefan Rompf 		     operstate == IF_OPER_UNKNOWN) &&
203b00055aaSStefan Rompf 		    !netif_dormant(dev))
204b00055aaSStefan Rompf 			operstate = IF_OPER_UP;
205b00055aaSStefan Rompf 		break;
206b00055aaSStefan Rompf 
207b00055aaSStefan Rompf 	case IF_OPER_DORMANT:
208b00055aaSStefan Rompf 		if (operstate == IF_OPER_UP ||
209b00055aaSStefan Rompf 		    operstate == IF_OPER_UNKNOWN)
210b00055aaSStefan Rompf 			operstate = IF_OPER_DORMANT;
211b00055aaSStefan Rompf 		break;
212b00055aaSStefan Rompf 	};
213b00055aaSStefan Rompf 
214b00055aaSStefan Rompf 	if (dev->operstate != operstate) {
215b00055aaSStefan Rompf 		write_lock_bh(&dev_base_lock);
216b00055aaSStefan Rompf 		dev->operstate = operstate;
217b00055aaSStefan Rompf 		write_unlock_bh(&dev_base_lock);
218b00055aaSStefan Rompf 		netdev_state_change(dev);
219b00055aaSStefan Rompf 	}
220b00055aaSStefan Rompf }
221b00055aaSStefan Rompf 
222b60c5115SThomas Graf static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
223b60c5115SThomas Graf 				 struct net_device_stats *b)
2241da177e4SLinus Torvalds {
225b60c5115SThomas Graf 	a->rx_packets = b->rx_packets;
226b60c5115SThomas Graf 	a->tx_packets = b->tx_packets;
227b60c5115SThomas Graf 	a->rx_bytes = b->rx_bytes;
228b60c5115SThomas Graf 	a->tx_bytes = b->tx_bytes;
229b60c5115SThomas Graf 	a->rx_errors = b->rx_errors;
230b60c5115SThomas Graf 	a->tx_errors = b->tx_errors;
231b60c5115SThomas Graf 	a->rx_dropped = b->rx_dropped;
232b60c5115SThomas Graf 	a->tx_dropped = b->tx_dropped;
233b60c5115SThomas Graf 
234b60c5115SThomas Graf 	a->multicast = b->multicast;
235b60c5115SThomas Graf 	a->collisions = b->collisions;
236b60c5115SThomas Graf 
237b60c5115SThomas Graf 	a->rx_length_errors = b->rx_length_errors;
238b60c5115SThomas Graf 	a->rx_over_errors = b->rx_over_errors;
239b60c5115SThomas Graf 	a->rx_crc_errors = b->rx_crc_errors;
240b60c5115SThomas Graf 	a->rx_frame_errors = b->rx_frame_errors;
241b60c5115SThomas Graf 	a->rx_fifo_errors = b->rx_fifo_errors;
242b60c5115SThomas Graf 	a->rx_missed_errors = b->rx_missed_errors;
243b60c5115SThomas Graf 
244b60c5115SThomas Graf 	a->tx_aborted_errors = b->tx_aborted_errors;
245b60c5115SThomas Graf 	a->tx_carrier_errors = b->tx_carrier_errors;
246b60c5115SThomas Graf 	a->tx_fifo_errors = b->tx_fifo_errors;
247b60c5115SThomas Graf 	a->tx_heartbeat_errors = b->tx_heartbeat_errors;
248b60c5115SThomas Graf 	a->tx_window_errors = b->tx_window_errors;
249b60c5115SThomas Graf 
250b60c5115SThomas Graf 	a->rx_compressed = b->rx_compressed;
251b60c5115SThomas Graf 	a->tx_compressed = b->tx_compressed;
252b60c5115SThomas Graf };
253b60c5115SThomas Graf 
254b60c5115SThomas Graf static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
255b60c5115SThomas Graf 			    void *iwbuf, int iwbuflen, int type, u32 pid,
256b60c5115SThomas Graf 			    u32 seq, u32 change, unsigned int flags)
257b60c5115SThomas Graf {
258b60c5115SThomas Graf 	struct ifinfomsg *ifm;
2591da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
2601da177e4SLinus Torvalds 
261b60c5115SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
262b60c5115SThomas Graf 	if (nlh == NULL)
263b60c5115SThomas Graf 		return -ENOBUFS;
2641da177e4SLinus Torvalds 
265b60c5115SThomas Graf 	ifm = nlmsg_data(nlh);
266b60c5115SThomas Graf 	ifm->ifi_family = AF_UNSPEC;
267b60c5115SThomas Graf 	ifm->__ifi_pad = 0;
268b60c5115SThomas Graf 	ifm->ifi_type = dev->type;
269b60c5115SThomas Graf 	ifm->ifi_index = dev->ifindex;
270b60c5115SThomas Graf 	ifm->ifi_flags = dev_get_flags(dev);
271b60c5115SThomas Graf 	ifm->ifi_change = change;
2721da177e4SLinus Torvalds 
273b60c5115SThomas Graf 	NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
274b60c5115SThomas Graf 	NLA_PUT_U32(skb, IFLA_TXQLEN, dev->tx_queue_len);
275b60c5115SThomas Graf 	NLA_PUT_U32(skb, IFLA_WEIGHT, dev->weight);
276b60c5115SThomas Graf 	NLA_PUT_U8(skb, IFLA_OPERSTATE,
277b60c5115SThomas Graf 		   netif_running(dev) ? dev->operstate : IF_OPER_DOWN);
278b60c5115SThomas Graf 	NLA_PUT_U8(skb, IFLA_LINKMODE, dev->link_mode);
279b60c5115SThomas Graf 	NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
2801da177e4SLinus Torvalds 
281b60c5115SThomas Graf 	if (dev->ifindex != dev->iflink)
282b60c5115SThomas Graf 		NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
2831da177e4SLinus Torvalds 
284b60c5115SThomas Graf 	if (dev->master)
285b60c5115SThomas Graf 		NLA_PUT_U32(skb, IFLA_MASTER, dev->master->ifindex);
286b60c5115SThomas Graf 
287b60c5115SThomas Graf 	if (dev->qdisc_sleeping)
288b60c5115SThomas Graf 		NLA_PUT_STRING(skb, IFLA_QDISC, dev->qdisc_sleeping->ops->id);
289b00055aaSStefan Rompf 
290b00055aaSStefan Rompf 	if (1) {
2911da177e4SLinus Torvalds 		struct rtnl_link_ifmap map = {
2921da177e4SLinus Torvalds 			.mem_start   = dev->mem_start,
2931da177e4SLinus Torvalds 			.mem_end     = dev->mem_end,
2941da177e4SLinus Torvalds 			.base_addr   = dev->base_addr,
2951da177e4SLinus Torvalds 			.irq         = dev->irq,
2961da177e4SLinus Torvalds 			.dma         = dev->dma,
2971da177e4SLinus Torvalds 			.port        = dev->if_port,
2981da177e4SLinus Torvalds 		};
299b60c5115SThomas Graf 		NLA_PUT(skb, IFLA_MAP, sizeof(map), &map);
3001da177e4SLinus Torvalds 	}
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds 	if (dev->addr_len) {
303b60c5115SThomas Graf 		NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
304b60c5115SThomas Graf 		NLA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast);
3051da177e4SLinus Torvalds 	}
3061da177e4SLinus Torvalds 
3071da177e4SLinus Torvalds 	if (dev->get_stats) {
308b60c5115SThomas Graf 		struct net_device_stats *stats = dev->get_stats(dev);
3091da177e4SLinus Torvalds 		if (stats) {
310b60c5115SThomas Graf 			struct nlattr *attr;
3111da177e4SLinus Torvalds 
312b60c5115SThomas Graf 			attr = nla_reserve(skb, IFLA_STATS,
313b60c5115SThomas Graf 					   sizeof(struct rtnl_link_stats));
314b60c5115SThomas Graf 			if (attr == NULL)
315b60c5115SThomas Graf 				goto nla_put_failure;
316b60c5115SThomas Graf 
317b60c5115SThomas Graf 			copy_rtnl_link_stats(nla_data(attr), stats);
3181da177e4SLinus Torvalds 		}
3191da177e4SLinus Torvalds 	}
3201da177e4SLinus Torvalds 
321b60c5115SThomas Graf 	if (iwbuf)
322b60c5115SThomas Graf 		NLA_PUT(skb, IFLA_WIRELESS, iwbuflen, iwbuf);
323b60c5115SThomas Graf 
324b60c5115SThomas Graf 	return nlmsg_end(skb, nlh);
325b60c5115SThomas Graf 
326b60c5115SThomas Graf nla_put_failure:
327b60c5115SThomas Graf 	return nlmsg_cancel(skb, nlh);
3281da177e4SLinus Torvalds }
3291da177e4SLinus Torvalds 
330b60c5115SThomas Graf static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
3311da177e4SLinus Torvalds {
3321da177e4SLinus Torvalds 	int idx;
3331da177e4SLinus Torvalds 	int s_idx = cb->args[0];
3341da177e4SLinus Torvalds 	struct net_device *dev;
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 	read_lock(&dev_base_lock);
3371da177e4SLinus Torvalds 	for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
3381da177e4SLinus Torvalds 		if (idx < s_idx)
3391da177e4SLinus Torvalds 			continue;
340b60c5115SThomas Graf 		if (rtnl_fill_ifinfo(skb, dev, NULL, 0, RTM_NEWLINK,
341b6544c0bSJamal Hadi Salim 				     NETLINK_CB(cb->skb).pid,
342b60c5115SThomas Graf 				     cb->nlh->nlmsg_seq, 0, NLM_F_MULTI) <= 0)
3431da177e4SLinus Torvalds 			break;
3441da177e4SLinus Torvalds 	}
3451da177e4SLinus Torvalds 	read_unlock(&dev_base_lock);
3461da177e4SLinus Torvalds 	cb->args[0] = idx;
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds 	return skb->len;
3491da177e4SLinus Torvalds }
3501da177e4SLinus Torvalds 
351da5e0494SThomas Graf static struct nla_policy ifla_policy[IFLA_MAX+1] __read_mostly = {
352da5e0494SThomas Graf 	[IFLA_IFNAME]		= { .type = NLA_STRING },
353da5e0494SThomas Graf 	[IFLA_MAP]		= { .minlen = sizeof(struct rtnl_link_ifmap) },
354da5e0494SThomas Graf 	[IFLA_MTU]		= { .type = NLA_U32 },
355da5e0494SThomas Graf 	[IFLA_TXQLEN]		= { .type = NLA_U32 },
356da5e0494SThomas Graf 	[IFLA_WEIGHT]		= { .type = NLA_U32 },
357da5e0494SThomas Graf 	[IFLA_OPERSTATE]	= { .type = NLA_U8 },
358da5e0494SThomas Graf 	[IFLA_LINKMODE]		= { .type = NLA_U8 },
359da5e0494SThomas Graf };
3601da177e4SLinus Torvalds 
361da5e0494SThomas Graf static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
362da5e0494SThomas Graf {
363da5e0494SThomas Graf 	struct ifinfomsg *ifm;
364da5e0494SThomas Graf 	struct net_device *dev;
365da5e0494SThomas Graf 	int err, send_addr_notify = 0, modified = 0;
366da5e0494SThomas Graf 	struct nlattr *tb[IFLA_MAX+1];
3671da177e4SLinus Torvalds 	char ifname[IFNAMSIZ];
3681da177e4SLinus Torvalds 
369da5e0494SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
370da5e0494SThomas Graf 	if (err < 0)
371da5e0494SThomas Graf 		goto errout;
3721da177e4SLinus Torvalds 
373da5e0494SThomas Graf 	if (tb[IFLA_IFNAME] &&
374da5e0494SThomas Graf 	    nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ) >= IFNAMSIZ)
375da5e0494SThomas Graf 		return -EINVAL;
3761da177e4SLinus Torvalds 
3771da177e4SLinus Torvalds 	err = -EINVAL;
378da5e0494SThomas Graf 	ifm = nlmsg_data(nlh);
379da5e0494SThomas Graf 	if (ifm->ifi_index >= 0)
380da5e0494SThomas Graf 		dev = dev_get_by_index(ifm->ifi_index);
381da5e0494SThomas Graf 	else if (tb[IFLA_IFNAME])
382da5e0494SThomas Graf 		dev = dev_get_by_name(ifname);
383da5e0494SThomas Graf 	else
384da5e0494SThomas Graf 		goto errout;
3851da177e4SLinus Torvalds 
386da5e0494SThomas Graf 	if (dev == NULL) {
387da5e0494SThomas Graf 		err = -ENODEV;
388da5e0494SThomas Graf 		goto errout;
389da5e0494SThomas Graf 	}
3901da177e4SLinus Torvalds 
391da5e0494SThomas Graf 	if (tb[IFLA_ADDRESS] &&
392da5e0494SThomas Graf 	    nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
393da5e0494SThomas Graf 		goto errout_dev;
394da5e0494SThomas Graf 
395da5e0494SThomas Graf 	if (tb[IFLA_BROADCAST] &&
396da5e0494SThomas Graf 	    nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
397da5e0494SThomas Graf 		goto errout_dev;
398da5e0494SThomas Graf 
399da5e0494SThomas Graf 	if (tb[IFLA_MAP]) {
4001da177e4SLinus Torvalds 		struct rtnl_link_ifmap *u_map;
4011da177e4SLinus Torvalds 		struct ifmap k_map;
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds 		if (!dev->set_config) {
4041da177e4SLinus Torvalds 			err = -EOPNOTSUPP;
405da5e0494SThomas Graf 			goto errout_dev;
4061da177e4SLinus Torvalds 		}
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds 		if (!netif_device_present(dev)) {
4091da177e4SLinus Torvalds 			err = -ENODEV;
410da5e0494SThomas Graf 			goto errout_dev;
4111da177e4SLinus Torvalds 		}
4121da177e4SLinus Torvalds 
413da5e0494SThomas Graf 		u_map = nla_data(tb[IFLA_MAP]);
4141da177e4SLinus Torvalds 		k_map.mem_start = (unsigned long) u_map->mem_start;
4151da177e4SLinus Torvalds 		k_map.mem_end = (unsigned long) u_map->mem_end;
4161da177e4SLinus Torvalds 		k_map.base_addr = (unsigned short) u_map->base_addr;
4171da177e4SLinus Torvalds 		k_map.irq = (unsigned char) u_map->irq;
4181da177e4SLinus Torvalds 		k_map.dma = (unsigned char) u_map->dma;
4191da177e4SLinus Torvalds 		k_map.port = (unsigned char) u_map->port;
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds 		err = dev->set_config(dev, &k_map);
422da5e0494SThomas Graf 		if (err < 0)
423da5e0494SThomas Graf 			goto errout_dev;
4241da177e4SLinus Torvalds 
425da5e0494SThomas Graf 		modified = 1;
4261da177e4SLinus Torvalds 	}
4271da177e4SLinus Torvalds 
428da5e0494SThomas Graf 	if (tb[IFLA_ADDRESS]) {
42970f8e78eSDavid S. Miller 		struct sockaddr *sa;
43070f8e78eSDavid S. Miller 		int len;
43170f8e78eSDavid S. Miller 
4321da177e4SLinus Torvalds 		if (!dev->set_mac_address) {
4331da177e4SLinus Torvalds 			err = -EOPNOTSUPP;
434da5e0494SThomas Graf 			goto errout_dev;
4351da177e4SLinus Torvalds 		}
436da5e0494SThomas Graf 
4371da177e4SLinus Torvalds 		if (!netif_device_present(dev)) {
4381da177e4SLinus Torvalds 			err = -ENODEV;
439da5e0494SThomas Graf 			goto errout_dev;
4401da177e4SLinus Torvalds 		}
4411da177e4SLinus Torvalds 
44270f8e78eSDavid S. Miller 		len = sizeof(sa_family_t) + dev->addr_len;
44370f8e78eSDavid S. Miller 		sa = kmalloc(len, GFP_KERNEL);
44470f8e78eSDavid S. Miller 		if (!sa) {
44570f8e78eSDavid S. Miller 			err = -ENOMEM;
446da5e0494SThomas Graf 			goto errout_dev;
44770f8e78eSDavid S. Miller 		}
44870f8e78eSDavid S. Miller 		sa->sa_family = dev->type;
449da5e0494SThomas Graf 		memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
45070f8e78eSDavid S. Miller 		       dev->addr_len);
45170f8e78eSDavid S. Miller 		err = dev->set_mac_address(dev, sa);
45270f8e78eSDavid S. Miller 		kfree(sa);
4531da177e4SLinus Torvalds 		if (err)
454da5e0494SThomas Graf 			goto errout_dev;
4551da177e4SLinus Torvalds 		send_addr_notify = 1;
456da5e0494SThomas Graf 		modified = 1;
4571da177e4SLinus Torvalds 	}
4581da177e4SLinus Torvalds 
459da5e0494SThomas Graf 	if (tb[IFLA_MTU]) {
460da5e0494SThomas Graf 		err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
461da5e0494SThomas Graf 		if (err < 0)
462da5e0494SThomas Graf 			goto errout_dev;
463da5e0494SThomas Graf 		modified = 1;
4641da177e4SLinus Torvalds 	}
4651da177e4SLinus Torvalds 
466da5e0494SThomas Graf 	/*
467da5e0494SThomas Graf 	 * Interface selected by interface index but interface
468da5e0494SThomas Graf 	 * name provided implies that a name change has been
469da5e0494SThomas Graf 	 * requested.
470da5e0494SThomas Graf 	 */
471da5e0494SThomas Graf 	if (ifm->ifi_index >= 0 && ifname[0]) {
4721da177e4SLinus Torvalds 		err = dev_change_name(dev, ifname);
473da5e0494SThomas Graf 		if (err < 0)
474da5e0494SThomas Graf 			goto errout_dev;
475da5e0494SThomas Graf 		modified = 1;
4761da177e4SLinus Torvalds 	}
4771da177e4SLinus Torvalds 
478711e2c33SJean Tourrilhes #ifdef CONFIG_NET_WIRELESS_RTNETLINK
479da5e0494SThomas Graf 	if (tb[IFLA_WIRELESS]) {
480711e2c33SJean Tourrilhes 		/* Call Wireless Extensions.
481711e2c33SJean Tourrilhes 		 * Various stuff checked in there... */
482da5e0494SThomas Graf 		err = wireless_rtnetlink_set(dev, nla_data(tb[IFLA_WIRELESS]),
483da5e0494SThomas Graf 					     nla_len(tb[IFLA_WIRELESS]));
484da5e0494SThomas Graf 		if (err < 0)
485da5e0494SThomas Graf 			goto errout_dev;
486711e2c33SJean Tourrilhes 	}
487711e2c33SJean Tourrilhes #endif	/* CONFIG_NET_WIRELESS_RTNETLINK */
488711e2c33SJean Tourrilhes 
489da5e0494SThomas Graf 	if (tb[IFLA_BROADCAST]) {
490da5e0494SThomas Graf 		nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
491da5e0494SThomas Graf 		send_addr_notify = 1;
492da5e0494SThomas Graf 	}
493da5e0494SThomas Graf 
494da5e0494SThomas Graf 
495da5e0494SThomas Graf 	if (ifm->ifi_flags)
496da5e0494SThomas Graf 		dev_change_flags(dev, ifm->ifi_flags);
497da5e0494SThomas Graf 
498da5e0494SThomas Graf 	if (tb[IFLA_TXQLEN])
499da5e0494SThomas Graf 		dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
500da5e0494SThomas Graf 
501da5e0494SThomas Graf 	if (tb[IFLA_WEIGHT])
502da5e0494SThomas Graf 		dev->weight = nla_get_u32(tb[IFLA_WEIGHT]);
503da5e0494SThomas Graf 
504da5e0494SThomas Graf 	if (tb[IFLA_OPERSTATE])
505da5e0494SThomas Graf 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
506da5e0494SThomas Graf 
507da5e0494SThomas Graf 	if (tb[IFLA_LINKMODE]) {
508da5e0494SThomas Graf 		write_lock_bh(&dev_base_lock);
509da5e0494SThomas Graf 		dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
510da5e0494SThomas Graf 		write_unlock_bh(&dev_base_lock);
511da5e0494SThomas Graf 	}
512da5e0494SThomas Graf 
5131da177e4SLinus Torvalds 	err = 0;
5141da177e4SLinus Torvalds 
515da5e0494SThomas Graf errout_dev:
516da5e0494SThomas Graf 	if (err < 0 && modified && net_ratelimit())
517da5e0494SThomas Graf 		printk(KERN_WARNING "A link change request failed with "
518da5e0494SThomas Graf 		       "some changes comitted already. Interface %s may "
519da5e0494SThomas Graf 		       "have been left with an inconsistent configuration, "
520da5e0494SThomas Graf 		       "please check.\n", dev->name);
521da5e0494SThomas Graf 
5221da177e4SLinus Torvalds 	if (send_addr_notify)
5231da177e4SLinus Torvalds 		call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds 	dev_put(dev);
526da5e0494SThomas Graf errout:
5271da177e4SLinus Torvalds 	return err;
5281da177e4SLinus Torvalds }
5291da177e4SLinus Torvalds 
530b60c5115SThomas Graf static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
531711e2c33SJean Tourrilhes {
532b60c5115SThomas Graf 	struct ifinfomsg *ifm;
533b60c5115SThomas Graf 	struct nlattr *tb[IFLA_MAX+1];
534b60c5115SThomas Graf 	struct net_device *dev = NULL;
535b60c5115SThomas Graf 	struct sk_buff *nskb;
536b60c5115SThomas Graf 	char *iw_buf = NULL, *iw = NULL;
537711e2c33SJean Tourrilhes 	int iw_buf_len = 0;
538b60c5115SThomas Graf 	int err, payload;
539711e2c33SJean Tourrilhes 
540b60c5115SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
541b60c5115SThomas Graf 	if (err < 0)
542b60c5115SThomas Graf 		goto errout;
543b60c5115SThomas Graf 
544b60c5115SThomas Graf 	ifm = nlmsg_data(nlh);
545b60c5115SThomas Graf 	if (ifm->ifi_index >= 0) {
546711e2c33SJean Tourrilhes 		dev = dev_get_by_index(ifm->ifi_index);
547b60c5115SThomas Graf 		if (dev == NULL)
548711e2c33SJean Tourrilhes 			return -ENODEV;
549b60c5115SThomas Graf 	} else
550b60c5115SThomas Graf 		return -EINVAL;
551b60c5115SThomas Graf 
552711e2c33SJean Tourrilhes 
553711e2c33SJean Tourrilhes #ifdef CONFIG_NET_WIRELESS_RTNETLINK
554b60c5115SThomas Graf 	if (tb[IFLA_WIRELESS]) {
555711e2c33SJean Tourrilhes 		/* Call Wireless Extensions. We need to know the size before
556711e2c33SJean Tourrilhes 		 * we can alloc. Various stuff checked in there... */
557b60c5115SThomas Graf 		err = wireless_rtnetlink_get(dev, nla_data(tb[IFLA_WIRELESS]),
558b60c5115SThomas Graf 					     nla_len(tb[IFLA_WIRELESS]),
559b60c5115SThomas Graf 					     &iw_buf, &iw_buf_len);
560b60c5115SThomas Graf 		if (err < 0)
561b60c5115SThomas Graf 			goto errout;
562b60c5115SThomas Graf 
563b60c5115SThomas Graf 		iw += IW_EV_POINT_OFF;
564711e2c33SJean Tourrilhes 	}
565711e2c33SJean Tourrilhes #endif	/* CONFIG_NET_WIRELESS_RTNETLINK */
566711e2c33SJean Tourrilhes 
567b60c5115SThomas Graf 	payload = NLMSG_ALIGN(sizeof(struct ifinfomsg) +
568b60c5115SThomas Graf 			      nla_total_size(iw_buf_len));
569b60c5115SThomas Graf 	nskb = nlmsg_new(nlmsg_total_size(payload), GFP_KERNEL);
570b60c5115SThomas Graf 	if (nskb == NULL) {
571b60c5115SThomas Graf 		err = -ENOBUFS;
572b60c5115SThomas Graf 		goto errout;
573b60c5115SThomas Graf 	}
574711e2c33SJean Tourrilhes 
575b60c5115SThomas Graf 	err = rtnl_fill_ifinfo(nskb, dev, iw, iw_buf_len, RTM_NEWLINK,
576b60c5115SThomas Graf 			       NETLINK_CB(skb).pid, nlh->nlmsg_seq, 0, 0);
577b60c5115SThomas Graf 	if (err <= 0) {
578b60c5115SThomas Graf 		kfree_skb(skb);
579b60c5115SThomas Graf 		goto errout;
580b60c5115SThomas Graf 	}
581711e2c33SJean Tourrilhes 
582*2942e900SThomas Graf 	err = rtnl_unicast(skb, NETLINK_CB(skb).pid);
583b60c5115SThomas Graf errout:
584711e2c33SJean Tourrilhes 	kfree(iw_buf);
585711e2c33SJean Tourrilhes 	dev_put(dev);
586b60c5115SThomas Graf 
587711e2c33SJean Tourrilhes 	return err;
588711e2c33SJean Tourrilhes }
589711e2c33SJean Tourrilhes 
590b60c5115SThomas Graf static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
5911da177e4SLinus Torvalds {
5921da177e4SLinus Torvalds 	int idx;
5931da177e4SLinus Torvalds 	int s_idx = cb->family;
5941da177e4SLinus Torvalds 
5951da177e4SLinus Torvalds 	if (s_idx == 0)
5961da177e4SLinus Torvalds 		s_idx = 1;
5971da177e4SLinus Torvalds 	for (idx=1; idx<NPROTO; idx++) {
5981da177e4SLinus Torvalds 		int type = cb->nlh->nlmsg_type-RTM_BASE;
5991da177e4SLinus Torvalds 		if (idx < s_idx || idx == PF_PACKET)
6001da177e4SLinus Torvalds 			continue;
6011da177e4SLinus Torvalds 		if (rtnetlink_links[idx] == NULL ||
6021da177e4SLinus Torvalds 		    rtnetlink_links[idx][type].dumpit == NULL)
6031da177e4SLinus Torvalds 			continue;
6041da177e4SLinus Torvalds 		if (idx > s_idx)
6051da177e4SLinus Torvalds 			memset(&cb->args[0], 0, sizeof(cb->args));
6061da177e4SLinus Torvalds 		if (rtnetlink_links[idx][type].dumpit(skb, cb))
6071da177e4SLinus Torvalds 			break;
6081da177e4SLinus Torvalds 	}
6091da177e4SLinus Torvalds 	cb->family = idx;
6101da177e4SLinus Torvalds 
6111da177e4SLinus Torvalds 	return skb->len;
6121da177e4SLinus Torvalds }
6131da177e4SLinus Torvalds 
6141da177e4SLinus Torvalds void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change)
6151da177e4SLinus Torvalds {
6161da177e4SLinus Torvalds 	struct sk_buff *skb;
6171da177e4SLinus Torvalds 	int size = NLMSG_SPACE(sizeof(struct ifinfomsg) +
6181da177e4SLinus Torvalds 			       sizeof(struct rtnl_link_ifmap) +
6191da177e4SLinus Torvalds 			       sizeof(struct rtnl_link_stats) + 128);
6201da177e4SLinus Torvalds 
621b60c5115SThomas Graf 	skb = nlmsg_new(size, GFP_KERNEL);
6221da177e4SLinus Torvalds 	if (!skb)
6231da177e4SLinus Torvalds 		return;
6241da177e4SLinus Torvalds 
625b60c5115SThomas Graf 	if (rtnl_fill_ifinfo(skb, dev, NULL, 0, type, 0, 0, change, 0) < 0) {
6261da177e4SLinus Torvalds 		kfree_skb(skb);
6271da177e4SLinus Torvalds 		return;
6281da177e4SLinus Torvalds 	}
629ac6d439dSPatrick McHardy 	NETLINK_CB(skb).dst_group = RTNLGRP_LINK;
630ac6d439dSPatrick McHardy 	netlink_broadcast(rtnl, skb, 0, RTNLGRP_LINK, GFP_KERNEL);
6311da177e4SLinus Torvalds }
6321da177e4SLinus Torvalds 
6331da177e4SLinus Torvalds /* Protected by RTNL sempahore.  */
6341da177e4SLinus Torvalds static struct rtattr **rta_buf;
6351da177e4SLinus Torvalds static int rtattr_max;
6361da177e4SLinus Torvalds 
6371da177e4SLinus Torvalds /* Process one rtnetlink message. */
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds static __inline__ int
6401da177e4SLinus Torvalds rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
6411da177e4SLinus Torvalds {
6421da177e4SLinus Torvalds 	struct rtnetlink_link *link;
6431da177e4SLinus Torvalds 	struct rtnetlink_link *link_tab;
6441da177e4SLinus Torvalds 	int sz_idx, kind;
6451da177e4SLinus Torvalds 	int min_len;
6461da177e4SLinus Torvalds 	int family;
6471da177e4SLinus Torvalds 	int type;
6481da177e4SLinus Torvalds 	int err;
6491da177e4SLinus Torvalds 
6501da177e4SLinus Torvalds 	/* Only requests are handled by kernel now */
6511da177e4SLinus Torvalds 	if (!(nlh->nlmsg_flags&NLM_F_REQUEST))
6521da177e4SLinus Torvalds 		return 0;
6531da177e4SLinus Torvalds 
6541da177e4SLinus Torvalds 	type = nlh->nlmsg_type;
6551da177e4SLinus Torvalds 
6561da177e4SLinus Torvalds 	/* A control message: ignore them */
6571da177e4SLinus Torvalds 	if (type < RTM_BASE)
6581da177e4SLinus Torvalds 		return 0;
6591da177e4SLinus Torvalds 
6601da177e4SLinus Torvalds 	/* Unknown message: reply with EINVAL */
6611da177e4SLinus Torvalds 	if (type > RTM_MAX)
6621da177e4SLinus Torvalds 		goto err_inval;
6631da177e4SLinus Torvalds 
6641da177e4SLinus Torvalds 	type -= RTM_BASE;
6651da177e4SLinus Torvalds 
6661da177e4SLinus Torvalds 	/* All the messages must have at least 1 byte length */
6671da177e4SLinus Torvalds 	if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg)))
6681da177e4SLinus Torvalds 		return 0;
6691da177e4SLinus Torvalds 
6701da177e4SLinus Torvalds 	family = ((struct rtgenmsg*)NLMSG_DATA(nlh))->rtgen_family;
6711da177e4SLinus Torvalds 	if (family >= NPROTO) {
6721da177e4SLinus Torvalds 		*errp = -EAFNOSUPPORT;
6731da177e4SLinus Torvalds 		return -1;
6741da177e4SLinus Torvalds 	}
6751da177e4SLinus Torvalds 
6761da177e4SLinus Torvalds 	link_tab = rtnetlink_links[family];
6771da177e4SLinus Torvalds 	if (link_tab == NULL)
6781da177e4SLinus Torvalds 		link_tab = rtnetlink_links[PF_UNSPEC];
6791da177e4SLinus Torvalds 	link = &link_tab[type];
6801da177e4SLinus Torvalds 
6811da177e4SLinus Torvalds 	sz_idx = type>>2;
6821da177e4SLinus Torvalds 	kind = type&3;
6831da177e4SLinus Torvalds 
684c7bdb545SDarrel Goeddel 	if (kind != 2 && security_netlink_recv(skb, CAP_NET_ADMIN)) {
6851da177e4SLinus Torvalds 		*errp = -EPERM;
6861da177e4SLinus Torvalds 		return -1;
6871da177e4SLinus Torvalds 	}
6881da177e4SLinus Torvalds 
6891da177e4SLinus Torvalds 	if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
6901da177e4SLinus Torvalds 		if (link->dumpit == NULL)
6911da177e4SLinus Torvalds 			link = &(rtnetlink_links[PF_UNSPEC][type]);
6921da177e4SLinus Torvalds 
6931da177e4SLinus Torvalds 		if (link->dumpit == NULL)
6941da177e4SLinus Torvalds 			goto err_inval;
6951da177e4SLinus Torvalds 
6961da177e4SLinus Torvalds 		if ((*errp = netlink_dump_start(rtnl, skb, nlh,
697a8f74b22SThomas Graf 						link->dumpit, NULL)) != 0) {
6981da177e4SLinus Torvalds 			return -1;
6991da177e4SLinus Torvalds 		}
7009ac4a169SThomas Graf 
7019ac4a169SThomas Graf 		netlink_queue_skip(nlh, skb);
7021da177e4SLinus Torvalds 		return -1;
7031da177e4SLinus Torvalds 	}
7041da177e4SLinus Torvalds 
7051da177e4SLinus Torvalds 	memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *)));
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds 	min_len = rtm_min[sz_idx];
7081da177e4SLinus Torvalds 	if (nlh->nlmsg_len < min_len)
7091da177e4SLinus Torvalds 		goto err_inval;
7101da177e4SLinus Torvalds 
7111da177e4SLinus Torvalds 	if (nlh->nlmsg_len > min_len) {
7121da177e4SLinus Torvalds 		int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
7131da177e4SLinus Torvalds 		struct rtattr *attr = (void*)nlh + NLMSG_ALIGN(min_len);
7141da177e4SLinus Torvalds 
7151da177e4SLinus Torvalds 		while (RTA_OK(attr, attrlen)) {
7161da177e4SLinus Torvalds 			unsigned flavor = attr->rta_type;
7171da177e4SLinus Torvalds 			if (flavor) {
7181da177e4SLinus Torvalds 				if (flavor > rta_max[sz_idx])
7191da177e4SLinus Torvalds 					goto err_inval;
7201da177e4SLinus Torvalds 				rta_buf[flavor-1] = attr;
7211da177e4SLinus Torvalds 			}
7221da177e4SLinus Torvalds 			attr = RTA_NEXT(attr, attrlen);
7231da177e4SLinus Torvalds 		}
7241da177e4SLinus Torvalds 	}
7251da177e4SLinus Torvalds 
7261da177e4SLinus Torvalds 	if (link->doit == NULL)
7271da177e4SLinus Torvalds 		link = &(rtnetlink_links[PF_UNSPEC][type]);
7281da177e4SLinus Torvalds 	if (link->doit == NULL)
7291da177e4SLinus Torvalds 		goto err_inval;
7301da177e4SLinus Torvalds 	err = link->doit(skb, nlh, (void *)&rta_buf[0]);
7311da177e4SLinus Torvalds 
7321da177e4SLinus Torvalds 	*errp = err;
7331da177e4SLinus Torvalds 	return err;
7341da177e4SLinus Torvalds 
7351da177e4SLinus Torvalds err_inval:
7361da177e4SLinus Torvalds 	*errp = -EINVAL;
7371da177e4SLinus Torvalds 	return -1;
7381da177e4SLinus Torvalds }
7391da177e4SLinus Torvalds 
7401da177e4SLinus Torvalds static void rtnetlink_rcv(struct sock *sk, int len)
7411da177e4SLinus Torvalds {
7429ac4a169SThomas Graf 	unsigned int qlen = 0;
7432a0a6ebeSHerbert Xu 
7441da177e4SLinus Torvalds 	do {
7456756ae4bSStephen Hemminger 		mutex_lock(&rtnl_mutex);
7469ac4a169SThomas Graf 		netlink_run_queue(sk, &qlen, &rtnetlink_rcv_msg);
7476756ae4bSStephen Hemminger 		mutex_unlock(&rtnl_mutex);
7481da177e4SLinus Torvalds 
7491da177e4SLinus Torvalds 		netdev_run_todo();
7502a0a6ebeSHerbert Xu 	} while (qlen);
7511da177e4SLinus Torvalds }
7521da177e4SLinus Torvalds 
753db46edc6SThomas Graf static struct rtnetlink_link link_rtnetlink_table[RTM_NR_MSGTYPES] =
7541da177e4SLinus Torvalds {
755b60c5115SThomas Graf 	[RTM_GETLINK     - RTM_BASE] = { .doit   = rtnl_getlink,
756b60c5115SThomas Graf 					 .dumpit = rtnl_dump_ifinfo	 },
757da5e0494SThomas Graf 	[RTM_SETLINK     - RTM_BASE] = { .doit   = rtnl_setlink		 },
758b60c5115SThomas Graf 	[RTM_GETADDR     - RTM_BASE] = { .dumpit = rtnl_dump_all	 },
759b60c5115SThomas Graf 	[RTM_GETROUTE    - RTM_BASE] = { .dumpit = rtnl_dump_all	 },
7601da177e4SLinus Torvalds 	[RTM_NEWNEIGH    - RTM_BASE] = { .doit   = neigh_add		 },
7611da177e4SLinus Torvalds 	[RTM_DELNEIGH    - RTM_BASE] = { .doit   = neigh_delete		 },
762240eed95SThomas Graf 	[RTM_GETNEIGH    - RTM_BASE] = { .dumpit = neigh_dump_info	 },
76314c0b97dSThomas Graf #ifdef CONFIG_FIB_RULES
76414c0b97dSThomas Graf 	[RTM_NEWRULE     - RTM_BASE] = { .doit   = fib_nl_newrule	 },
76514c0b97dSThomas Graf 	[RTM_DELRULE     - RTM_BASE] = { .doit   = fib_nl_delrule	 },
76614c0b97dSThomas Graf #endif
767b60c5115SThomas Graf 	[RTM_GETRULE     - RTM_BASE] = { .dumpit = rtnl_dump_all	 },
768c7fb64dbSThomas Graf 	[RTM_GETNEIGHTBL - RTM_BASE] = { .dumpit = neightbl_dump_info	 },
769c7fb64dbSThomas Graf 	[RTM_SETNEIGHTBL - RTM_BASE] = { .doit   = neightbl_set		 },
7701da177e4SLinus Torvalds };
7711da177e4SLinus Torvalds 
7721da177e4SLinus Torvalds static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
7731da177e4SLinus Torvalds {
7741da177e4SLinus Torvalds 	struct net_device *dev = ptr;
7751da177e4SLinus Torvalds 	switch (event) {
7761da177e4SLinus Torvalds 	case NETDEV_UNREGISTER:
7771da177e4SLinus Torvalds 		rtmsg_ifinfo(RTM_DELLINK, dev, ~0U);
7781da177e4SLinus Torvalds 		break;
7791da177e4SLinus Torvalds 	case NETDEV_REGISTER:
7801da177e4SLinus Torvalds 		rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
7811da177e4SLinus Torvalds 		break;
7821da177e4SLinus Torvalds 	case NETDEV_UP:
7831da177e4SLinus Torvalds 	case NETDEV_DOWN:
7841da177e4SLinus Torvalds 		rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
7851da177e4SLinus Torvalds 		break;
7861da177e4SLinus Torvalds 	case NETDEV_CHANGE:
7871da177e4SLinus Torvalds 	case NETDEV_GOING_DOWN:
7881da177e4SLinus Torvalds 		break;
7891da177e4SLinus Torvalds 	default:
7901da177e4SLinus Torvalds 		rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
7911da177e4SLinus Torvalds 		break;
7921da177e4SLinus Torvalds 	}
7931da177e4SLinus Torvalds 	return NOTIFY_DONE;
7941da177e4SLinus Torvalds }
7951da177e4SLinus Torvalds 
7961da177e4SLinus Torvalds static struct notifier_block rtnetlink_dev_notifier = {
7971da177e4SLinus Torvalds 	.notifier_call	= rtnetlink_event,
7981da177e4SLinus Torvalds };
7991da177e4SLinus Torvalds 
8001da177e4SLinus Torvalds void __init rtnetlink_init(void)
8011da177e4SLinus Torvalds {
8021da177e4SLinus Torvalds 	int i;
8031da177e4SLinus Torvalds 
8041da177e4SLinus Torvalds 	rtattr_max = 0;
8051da177e4SLinus Torvalds 	for (i = 0; i < ARRAY_SIZE(rta_max); i++)
8061da177e4SLinus Torvalds 		if (rta_max[i] > rtattr_max)
8071da177e4SLinus Torvalds 			rtattr_max = rta_max[i];
8081da177e4SLinus Torvalds 	rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL);
8091da177e4SLinus Torvalds 	if (!rta_buf)
8101da177e4SLinus Torvalds 		panic("rtnetlink_init: cannot allocate rta_buf\n");
8111da177e4SLinus Torvalds 
81206628607SPatrick McHardy 	rtnl = netlink_kernel_create(NETLINK_ROUTE, RTNLGRP_MAX, rtnetlink_rcv,
81306628607SPatrick McHardy 	                             THIS_MODULE);
8141da177e4SLinus Torvalds 	if (rtnl == NULL)
8151da177e4SLinus Torvalds 		panic("rtnetlink_init: cannot initialize rtnetlink\n");
8161da177e4SLinus Torvalds 	netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV);
8171da177e4SLinus Torvalds 	register_netdevice_notifier(&rtnetlink_dev_notifier);
8181da177e4SLinus Torvalds 	rtnetlink_links[PF_UNSPEC] = link_rtnetlink_table;
8191da177e4SLinus Torvalds 	rtnetlink_links[PF_PACKET] = link_rtnetlink_table;
8201da177e4SLinus Torvalds }
8211da177e4SLinus Torvalds 
8221da177e4SLinus Torvalds EXPORT_SYMBOL(__rta_fill);
8231da177e4SLinus Torvalds EXPORT_SYMBOL(rtattr_strlcpy);
8241da177e4SLinus Torvalds EXPORT_SYMBOL(rtattr_parse);
8251da177e4SLinus Torvalds EXPORT_SYMBOL(rtnetlink_links);
8261da177e4SLinus Torvalds EXPORT_SYMBOL(rtnetlink_put_metrics);
8271da177e4SLinus Torvalds EXPORT_SYMBOL(rtnl);
8281da177e4SLinus Torvalds EXPORT_SYMBOL(rtnl_lock);
8296756ae4bSStephen Hemminger EXPORT_SYMBOL(rtnl_trylock);
8301da177e4SLinus Torvalds EXPORT_SYMBOL(rtnl_unlock);
831*2942e900SThomas Graf EXPORT_SYMBOL(rtnl_unicast);
832