xref: /openbmc/linux/net/core/rtnetlink.c (revision 14c0b97ddfc2944982d078b8e33b088840068976)
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>
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>
52*14c0b97dSThomas 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);
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds void rtnl_lock(void)
621da177e4SLinus Torvalds {
636756ae4bSStephen Hemminger 	mutex_lock(&rtnl_mutex);
641da177e4SLinus Torvalds }
651da177e4SLinus Torvalds 
666756ae4bSStephen Hemminger void __rtnl_unlock(void)
671da177e4SLinus Torvalds {
686756ae4bSStephen Hemminger 	mutex_unlock(&rtnl_mutex);
691da177e4SLinus Torvalds }
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds void rtnl_unlock(void)
721da177e4SLinus Torvalds {
736756ae4bSStephen Hemminger 	mutex_unlock(&rtnl_mutex);
746756ae4bSStephen Hemminger 	if (rtnl && rtnl->sk_receive_queue.qlen)
756756ae4bSStephen Hemminger 		rtnl->sk_data_ready(rtnl, 0);
761da177e4SLinus Torvalds 	netdev_run_todo();
771da177e4SLinus Torvalds }
781da177e4SLinus Torvalds 
796756ae4bSStephen Hemminger int rtnl_trylock(void)
806756ae4bSStephen Hemminger {
816756ae4bSStephen Hemminger 	return mutex_trylock(&rtnl_mutex);
826756ae4bSStephen Hemminger }
836756ae4bSStephen Hemminger 
841da177e4SLinus Torvalds int rtattr_parse(struct rtattr *tb[], int maxattr, struct rtattr *rta, int len)
851da177e4SLinus Torvalds {
861da177e4SLinus Torvalds 	memset(tb, 0, sizeof(struct rtattr*)*maxattr);
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds 	while (RTA_OK(rta, len)) {
891da177e4SLinus Torvalds 		unsigned flavor = rta->rta_type;
901da177e4SLinus Torvalds 		if (flavor && flavor <= maxattr)
911da177e4SLinus Torvalds 			tb[flavor-1] = rta;
921da177e4SLinus Torvalds 		rta = RTA_NEXT(rta, len);
931da177e4SLinus Torvalds 	}
941da177e4SLinus Torvalds 	return 0;
951da177e4SLinus Torvalds }
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds struct sock *rtnl;
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds struct rtnetlink_link * rtnetlink_links[NPROTO];
1001da177e4SLinus Torvalds 
101db46edc6SThomas Graf static const int rtm_min[RTM_NR_FAMILIES] =
1021da177e4SLinus Torvalds {
103f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWLINK)]      = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
104f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWADDR)]      = NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
105f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWROUTE)]     = NLMSG_LENGTH(sizeof(struct rtmsg)),
106f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWNEIGH)]     = NLMSG_LENGTH(sizeof(struct ndmsg)),
107*14c0b97dSThomas 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)),
115c7fb64dbSThomas Graf 	[RTM_FAM(RTM_NEWNEIGHTBL)]  = NLMSG_LENGTH(sizeof(struct ndtmsg)),
1161da177e4SLinus Torvalds };
1171da177e4SLinus Torvalds 
118db46edc6SThomas Graf static const int rta_max[RTM_NR_FAMILIES] =
1191da177e4SLinus Torvalds {
120f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWLINK)]      = IFLA_MAX,
121f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWADDR)]      = IFA_MAX,
122f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWROUTE)]     = RTA_MAX,
123f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWNEIGH)]     = NDA_MAX,
124*14c0b97dSThomas Graf 	[RTM_FAM(RTM_NEWRULE)]      = FRA_MAX,
125f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWQDISC)]     = TCA_MAX,
126f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWTCLASS)]    = TCA_MAX,
127f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWTFILTER)]   = TCA_MAX,
128f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWACTION)]    = TCAA_MAX,
129c7fb64dbSThomas Graf 	[RTM_FAM(RTM_NEWNEIGHTBL)]  = NDTA_MAX,
1301da177e4SLinus Torvalds };
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
1331da177e4SLinus Torvalds {
1341da177e4SLinus Torvalds 	struct rtattr *rta;
1351da177e4SLinus Torvalds 	int size = RTA_LENGTH(attrlen);
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds 	rta = (struct rtattr*)skb_put(skb, RTA_ALIGN(size));
1381da177e4SLinus Torvalds 	rta->rta_type = attrtype;
1391da177e4SLinus Torvalds 	rta->rta_len = size;
1401da177e4SLinus Torvalds 	memcpy(RTA_DATA(rta), data, attrlen);
141b3563c4fSPatrick McHardy 	memset(RTA_DATA(rta) + attrlen, 0, RTA_ALIGN(size) - size);
1421da177e4SLinus Torvalds }
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds size_t rtattr_strlcpy(char *dest, const struct rtattr *rta, size_t size)
1451da177e4SLinus Torvalds {
1461da177e4SLinus Torvalds 	size_t ret = RTA_PAYLOAD(rta);
1471da177e4SLinus Torvalds 	char *src = RTA_DATA(rta);
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds 	if (ret > 0 && src[ret - 1] == '\0')
1501da177e4SLinus Torvalds 		ret--;
1511da177e4SLinus Torvalds 	if (size > 0) {
1521da177e4SLinus Torvalds 		size_t len = (ret >= size) ? size - 1 : ret;
1531da177e4SLinus Torvalds 		memset(dest, 0, size);
1541da177e4SLinus Torvalds 		memcpy(dest, src, len);
1551da177e4SLinus Torvalds 	}
1561da177e4SLinus Torvalds 	return ret;
1571da177e4SLinus Torvalds }
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds int rtnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo)
1601da177e4SLinus Torvalds {
1611da177e4SLinus Torvalds 	int err = 0;
1621da177e4SLinus Torvalds 
163ac6d439dSPatrick McHardy 	NETLINK_CB(skb).dst_group = group;
1641da177e4SLinus Torvalds 	if (echo)
1651da177e4SLinus Torvalds 		atomic_inc(&skb->users);
1661da177e4SLinus Torvalds 	netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
1671da177e4SLinus Torvalds 	if (echo)
1681da177e4SLinus Torvalds 		err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
1691da177e4SLinus Torvalds 	return err;
1701da177e4SLinus Torvalds }
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
1731da177e4SLinus Torvalds {
1741da177e4SLinus Torvalds 	struct rtattr *mx = (struct rtattr*)skb->tail;
1751da177e4SLinus Torvalds 	int i;
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds 	RTA_PUT(skb, RTA_METRICS, 0, NULL);
1781da177e4SLinus Torvalds 	for (i=0; i<RTAX_MAX; i++) {
1791da177e4SLinus Torvalds 		if (metrics[i])
1801da177e4SLinus Torvalds 			RTA_PUT(skb, i+1, sizeof(u32), metrics+i);
1811da177e4SLinus Torvalds 	}
1821da177e4SLinus Torvalds 	mx->rta_len = skb->tail - (u8*)mx;
1831da177e4SLinus Torvalds 	if (mx->rta_len == RTA_LENGTH(0))
1841da177e4SLinus Torvalds 		skb_trim(skb, (u8*)mx - skb->data);
1851da177e4SLinus Torvalds 	return 0;
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds rtattr_failure:
1881da177e4SLinus Torvalds 	skb_trim(skb, (u8*)mx - skb->data);
1891da177e4SLinus Torvalds 	return -1;
1901da177e4SLinus Torvalds }
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds 
193b00055aaSStefan Rompf static void set_operstate(struct net_device *dev, unsigned char transition)
194b00055aaSStefan Rompf {
195b00055aaSStefan Rompf 	unsigned char operstate = dev->operstate;
196b00055aaSStefan Rompf 
197b00055aaSStefan Rompf 	switch(transition) {
198b00055aaSStefan Rompf 	case IF_OPER_UP:
199b00055aaSStefan Rompf 		if ((operstate == IF_OPER_DORMANT ||
200b00055aaSStefan Rompf 		     operstate == IF_OPER_UNKNOWN) &&
201b00055aaSStefan Rompf 		    !netif_dormant(dev))
202b00055aaSStefan Rompf 			operstate = IF_OPER_UP;
203b00055aaSStefan Rompf 		break;
204b00055aaSStefan Rompf 
205b00055aaSStefan Rompf 	case IF_OPER_DORMANT:
206b00055aaSStefan Rompf 		if (operstate == IF_OPER_UP ||
207b00055aaSStefan Rompf 		    operstate == IF_OPER_UNKNOWN)
208b00055aaSStefan Rompf 			operstate = IF_OPER_DORMANT;
209b00055aaSStefan Rompf 		break;
210b00055aaSStefan Rompf 	};
211b00055aaSStefan Rompf 
212b00055aaSStefan Rompf 	if (dev->operstate != operstate) {
213b00055aaSStefan Rompf 		write_lock_bh(&dev_base_lock);
214b00055aaSStefan Rompf 		dev->operstate = operstate;
215b00055aaSStefan Rompf 		write_unlock_bh(&dev_base_lock);
216b00055aaSStefan Rompf 		netdev_state_change(dev);
217b00055aaSStefan Rompf 	}
218b00055aaSStefan Rompf }
219b00055aaSStefan Rompf 
2201da177e4SLinus Torvalds static int rtnetlink_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
221b6544c0bSJamal Hadi Salim 				 int type, u32 pid, u32 seq, u32 change,
222b6544c0bSJamal Hadi Salim 				 unsigned int flags)
2231da177e4SLinus Torvalds {
2241da177e4SLinus Torvalds 	struct ifinfomsg *r;
2251da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
2261da177e4SLinus Torvalds 	unsigned char	 *b = skb->tail;
2271da177e4SLinus Torvalds 
228b6544c0bSJamal Hadi Salim 	nlh = NLMSG_NEW(skb, pid, seq, type, sizeof(*r), flags);
2291da177e4SLinus Torvalds 	r = NLMSG_DATA(nlh);
2301da177e4SLinus Torvalds 	r->ifi_family = AF_UNSPEC;
2319ef1d4c7SPatrick McHardy 	r->__ifi_pad = 0;
2321da177e4SLinus Torvalds 	r->ifi_type = dev->type;
2331da177e4SLinus Torvalds 	r->ifi_index = dev->ifindex;
2341da177e4SLinus Torvalds 	r->ifi_flags = dev_get_flags(dev);
2351da177e4SLinus Torvalds 	r->ifi_change = change;
2361da177e4SLinus Torvalds 
2371da177e4SLinus Torvalds 	RTA_PUT(skb, IFLA_IFNAME, strlen(dev->name)+1, dev->name);
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds 	if (1) {
2401da177e4SLinus Torvalds 		u32 txqlen = dev->tx_queue_len;
2411da177e4SLinus Torvalds 		RTA_PUT(skb, IFLA_TXQLEN, sizeof(txqlen), &txqlen);
2421da177e4SLinus Torvalds 	}
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds 	if (1) {
2451da177e4SLinus Torvalds 		u32 weight = dev->weight;
2461da177e4SLinus Torvalds 		RTA_PUT(skb, IFLA_WEIGHT, sizeof(weight), &weight);
2471da177e4SLinus Torvalds 	}
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds 	if (1) {
250b00055aaSStefan Rompf 		u8 operstate = netif_running(dev)?dev->operstate:IF_OPER_DOWN;
251b00055aaSStefan Rompf 		u8 link_mode = dev->link_mode;
252b00055aaSStefan Rompf 		RTA_PUT(skb, IFLA_OPERSTATE, sizeof(operstate), &operstate);
253b00055aaSStefan Rompf 		RTA_PUT(skb, IFLA_LINKMODE, sizeof(link_mode), &link_mode);
254b00055aaSStefan Rompf 	}
255b00055aaSStefan Rompf 
256b00055aaSStefan Rompf 	if (1) {
2571da177e4SLinus Torvalds 		struct rtnl_link_ifmap map = {
2581da177e4SLinus Torvalds 			.mem_start   = dev->mem_start,
2591da177e4SLinus Torvalds 			.mem_end     = dev->mem_end,
2601da177e4SLinus Torvalds 			.base_addr   = dev->base_addr,
2611da177e4SLinus Torvalds 			.irq         = dev->irq,
2621da177e4SLinus Torvalds 			.dma         = dev->dma,
2631da177e4SLinus Torvalds 			.port        = dev->if_port,
2641da177e4SLinus Torvalds 		};
2651da177e4SLinus Torvalds 		RTA_PUT(skb, IFLA_MAP, sizeof(map), &map);
2661da177e4SLinus Torvalds 	}
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds 	if (dev->addr_len) {
2691da177e4SLinus Torvalds 		RTA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
2701da177e4SLinus Torvalds 		RTA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast);
2711da177e4SLinus Torvalds 	}
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds 	if (1) {
2741da177e4SLinus Torvalds 		u32 mtu = dev->mtu;
2751da177e4SLinus Torvalds 		RTA_PUT(skb, IFLA_MTU, sizeof(mtu), &mtu);
2761da177e4SLinus Torvalds 	}
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds 	if (dev->ifindex != dev->iflink) {
2791da177e4SLinus Torvalds 		u32 iflink = dev->iflink;
2801da177e4SLinus Torvalds 		RTA_PUT(skb, IFLA_LINK, sizeof(iflink), &iflink);
2811da177e4SLinus Torvalds 	}
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds 	if (dev->qdisc_sleeping)
2841da177e4SLinus Torvalds 		RTA_PUT(skb, IFLA_QDISC,
2851da177e4SLinus Torvalds 			strlen(dev->qdisc_sleeping->ops->id) + 1,
2861da177e4SLinus Torvalds 			dev->qdisc_sleeping->ops->id);
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds 	if (dev->master) {
2891da177e4SLinus Torvalds 		u32 master = dev->master->ifindex;
2901da177e4SLinus Torvalds 		RTA_PUT(skb, IFLA_MASTER, sizeof(master), &master);
2911da177e4SLinus Torvalds 	}
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds 	if (dev->get_stats) {
2941da177e4SLinus Torvalds 		unsigned long *stats = (unsigned long*)dev->get_stats(dev);
2951da177e4SLinus Torvalds 		if (stats) {
2961da177e4SLinus Torvalds 			struct rtattr  *a;
2971da177e4SLinus Torvalds 			__u32	       *s;
2981da177e4SLinus Torvalds 			int		i;
2991da177e4SLinus Torvalds 			int		n = sizeof(struct rtnl_link_stats)/4;
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds 			a = __RTA_PUT(skb, IFLA_STATS, n*4);
3021da177e4SLinus Torvalds 			s = RTA_DATA(a);
3031da177e4SLinus Torvalds 			for (i=0; i<n; i++)
3041da177e4SLinus Torvalds 				s[i] = stats[i];
3051da177e4SLinus Torvalds 		}
3061da177e4SLinus Torvalds 	}
3071da177e4SLinus Torvalds 	nlh->nlmsg_len = skb->tail - b;
3081da177e4SLinus Torvalds 	return skb->len;
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds nlmsg_failure:
3111da177e4SLinus Torvalds rtattr_failure:
3121da177e4SLinus Torvalds 	skb_trim(skb, b - skb->data);
3131da177e4SLinus Torvalds 	return -1;
3141da177e4SLinus Torvalds }
3151da177e4SLinus Torvalds 
3161da177e4SLinus Torvalds static int rtnetlink_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
3171da177e4SLinus Torvalds {
3181da177e4SLinus Torvalds 	int idx;
3191da177e4SLinus Torvalds 	int s_idx = cb->args[0];
3201da177e4SLinus Torvalds 	struct net_device *dev;
3211da177e4SLinus Torvalds 
3221da177e4SLinus Torvalds 	read_lock(&dev_base_lock);
3231da177e4SLinus Torvalds 	for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
3241da177e4SLinus Torvalds 		if (idx < s_idx)
3251da177e4SLinus Torvalds 			continue;
326b6544c0bSJamal Hadi Salim 		if (rtnetlink_fill_ifinfo(skb, dev, RTM_NEWLINK,
327b6544c0bSJamal Hadi Salim 					  NETLINK_CB(cb->skb).pid,
328b6544c0bSJamal Hadi Salim 					  cb->nlh->nlmsg_seq, 0,
329b6544c0bSJamal Hadi Salim 					  NLM_F_MULTI) <= 0)
3301da177e4SLinus Torvalds 			break;
3311da177e4SLinus Torvalds 	}
3321da177e4SLinus Torvalds 	read_unlock(&dev_base_lock);
3331da177e4SLinus Torvalds 	cb->args[0] = idx;
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds 	return skb->len;
3361da177e4SLinus Torvalds }
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds static int do_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
3391da177e4SLinus Torvalds {
3401da177e4SLinus Torvalds 	struct ifinfomsg  *ifm = NLMSG_DATA(nlh);
3411da177e4SLinus Torvalds 	struct rtattr    **ida = arg;
3421da177e4SLinus Torvalds 	struct net_device *dev;
3431da177e4SLinus Torvalds 	int err, send_addr_notify = 0;
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds 	if (ifm->ifi_index >= 0)
3461da177e4SLinus Torvalds 		dev = dev_get_by_index(ifm->ifi_index);
3471da177e4SLinus Torvalds 	else if (ida[IFLA_IFNAME - 1]) {
3481da177e4SLinus Torvalds 		char ifname[IFNAMSIZ];
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds 		if (rtattr_strlcpy(ifname, ida[IFLA_IFNAME - 1],
3511da177e4SLinus Torvalds 		                   IFNAMSIZ) >= IFNAMSIZ)
3521da177e4SLinus Torvalds 			return -EINVAL;
3531da177e4SLinus Torvalds 		dev = dev_get_by_name(ifname);
3541da177e4SLinus Torvalds 	} else
3551da177e4SLinus Torvalds 		return -EINVAL;
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds 	if (!dev)
3581da177e4SLinus Torvalds 		return -ENODEV;
3591da177e4SLinus Torvalds 
3601da177e4SLinus Torvalds 	err = -EINVAL;
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds 	if (ifm->ifi_flags)
3631da177e4SLinus Torvalds 		dev_change_flags(dev, ifm->ifi_flags);
3641da177e4SLinus Torvalds 
3651da177e4SLinus Torvalds 	if (ida[IFLA_MAP - 1]) {
3661da177e4SLinus Torvalds 		struct rtnl_link_ifmap *u_map;
3671da177e4SLinus Torvalds 		struct ifmap k_map;
3681da177e4SLinus Torvalds 
3691da177e4SLinus Torvalds 		if (!dev->set_config) {
3701da177e4SLinus Torvalds 			err = -EOPNOTSUPP;
3711da177e4SLinus Torvalds 			goto out;
3721da177e4SLinus Torvalds 		}
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds 		if (!netif_device_present(dev)) {
3751da177e4SLinus Torvalds 			err = -ENODEV;
3761da177e4SLinus Torvalds 			goto out;
3771da177e4SLinus Torvalds 		}
3781da177e4SLinus Torvalds 
3791da177e4SLinus Torvalds 		if (ida[IFLA_MAP - 1]->rta_len != RTA_LENGTH(sizeof(*u_map)))
3801da177e4SLinus Torvalds 			goto out;
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds 		u_map = RTA_DATA(ida[IFLA_MAP - 1]);
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds 		k_map.mem_start = (unsigned long) u_map->mem_start;
3851da177e4SLinus Torvalds 		k_map.mem_end = (unsigned long) u_map->mem_end;
3861da177e4SLinus Torvalds 		k_map.base_addr = (unsigned short) u_map->base_addr;
3871da177e4SLinus Torvalds 		k_map.irq = (unsigned char) u_map->irq;
3881da177e4SLinus Torvalds 		k_map.dma = (unsigned char) u_map->dma;
3891da177e4SLinus Torvalds 		k_map.port = (unsigned char) u_map->port;
3901da177e4SLinus Torvalds 
3911da177e4SLinus Torvalds 		err = dev->set_config(dev, &k_map);
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds 		if (err)
3941da177e4SLinus Torvalds 			goto out;
3951da177e4SLinus Torvalds 	}
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds 	if (ida[IFLA_ADDRESS - 1]) {
39870f8e78eSDavid S. Miller 		struct sockaddr *sa;
39970f8e78eSDavid S. Miller 		int len;
40070f8e78eSDavid S. Miller 
4011da177e4SLinus Torvalds 		if (!dev->set_mac_address) {
4021da177e4SLinus Torvalds 			err = -EOPNOTSUPP;
4031da177e4SLinus Torvalds 			goto out;
4041da177e4SLinus Torvalds 		}
4051da177e4SLinus Torvalds 		if (!netif_device_present(dev)) {
4061da177e4SLinus Torvalds 			err = -ENODEV;
4071da177e4SLinus Torvalds 			goto out;
4081da177e4SLinus Torvalds 		}
4091da177e4SLinus Torvalds 		if (ida[IFLA_ADDRESS - 1]->rta_len != RTA_LENGTH(dev->addr_len))
4101da177e4SLinus Torvalds 			goto out;
4111da177e4SLinus Torvalds 
41270f8e78eSDavid S. Miller 		len = sizeof(sa_family_t) + dev->addr_len;
41370f8e78eSDavid S. Miller 		sa = kmalloc(len, GFP_KERNEL);
41470f8e78eSDavid S. Miller 		if (!sa) {
41570f8e78eSDavid S. Miller 			err = -ENOMEM;
41670f8e78eSDavid S. Miller 			goto out;
41770f8e78eSDavid S. Miller 		}
41870f8e78eSDavid S. Miller 		sa->sa_family = dev->type;
41970f8e78eSDavid S. Miller 		memcpy(sa->sa_data, RTA_DATA(ida[IFLA_ADDRESS - 1]),
42070f8e78eSDavid S. Miller 		       dev->addr_len);
42170f8e78eSDavid S. Miller 		err = dev->set_mac_address(dev, sa);
42270f8e78eSDavid S. Miller 		kfree(sa);
4231da177e4SLinus Torvalds 		if (err)
4241da177e4SLinus Torvalds 			goto out;
4251da177e4SLinus Torvalds 		send_addr_notify = 1;
4261da177e4SLinus Torvalds 	}
4271da177e4SLinus Torvalds 
4281da177e4SLinus Torvalds 	if (ida[IFLA_BROADCAST - 1]) {
4291da177e4SLinus Torvalds 		if (ida[IFLA_BROADCAST - 1]->rta_len != RTA_LENGTH(dev->addr_len))
4301da177e4SLinus Torvalds 			goto out;
4311da177e4SLinus Torvalds 		memcpy(dev->broadcast, RTA_DATA(ida[IFLA_BROADCAST - 1]),
4321da177e4SLinus Torvalds 		       dev->addr_len);
4331da177e4SLinus Torvalds 		send_addr_notify = 1;
4341da177e4SLinus Torvalds 	}
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds 	if (ida[IFLA_MTU - 1]) {
4371da177e4SLinus Torvalds 		if (ida[IFLA_MTU - 1]->rta_len != RTA_LENGTH(sizeof(u32)))
4381da177e4SLinus Torvalds 			goto out;
4391da177e4SLinus Torvalds 		err = dev_set_mtu(dev, *((u32 *) RTA_DATA(ida[IFLA_MTU - 1])));
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds 		if (err)
4421da177e4SLinus Torvalds 			goto out;
4431da177e4SLinus Torvalds 
4441da177e4SLinus Torvalds 	}
4451da177e4SLinus Torvalds 
4461da177e4SLinus Torvalds 	if (ida[IFLA_TXQLEN - 1]) {
4471da177e4SLinus Torvalds 		if (ida[IFLA_TXQLEN - 1]->rta_len != RTA_LENGTH(sizeof(u32)))
4481da177e4SLinus Torvalds 			goto out;
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds 		dev->tx_queue_len = *((u32 *) RTA_DATA(ida[IFLA_TXQLEN - 1]));
4511da177e4SLinus Torvalds 	}
4521da177e4SLinus Torvalds 
4531da177e4SLinus Torvalds 	if (ida[IFLA_WEIGHT - 1]) {
4541da177e4SLinus Torvalds 		if (ida[IFLA_WEIGHT - 1]->rta_len != RTA_LENGTH(sizeof(u32)))
4551da177e4SLinus Torvalds 			goto out;
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds 		dev->weight = *((u32 *) RTA_DATA(ida[IFLA_WEIGHT - 1]));
4581da177e4SLinus Torvalds 	}
4591da177e4SLinus Torvalds 
460b00055aaSStefan Rompf 	if (ida[IFLA_OPERSTATE - 1]) {
461b00055aaSStefan Rompf 		if (ida[IFLA_OPERSTATE - 1]->rta_len != RTA_LENGTH(sizeof(u8)))
462b00055aaSStefan Rompf 			goto out;
463b00055aaSStefan Rompf 
464b00055aaSStefan Rompf 		set_operstate(dev, *((u8 *) RTA_DATA(ida[IFLA_OPERSTATE - 1])));
465b00055aaSStefan Rompf 	}
466b00055aaSStefan Rompf 
467b00055aaSStefan Rompf 	if (ida[IFLA_LINKMODE - 1]) {
468b00055aaSStefan Rompf 		if (ida[IFLA_LINKMODE - 1]->rta_len != RTA_LENGTH(sizeof(u8)))
469b00055aaSStefan Rompf 			goto out;
470b00055aaSStefan Rompf 
471b00055aaSStefan Rompf 		write_lock_bh(&dev_base_lock);
472b00055aaSStefan Rompf 		dev->link_mode = *((u8 *) RTA_DATA(ida[IFLA_LINKMODE - 1]));
473b00055aaSStefan Rompf 		write_unlock_bh(&dev_base_lock);
474b00055aaSStefan Rompf 	}
475b00055aaSStefan Rompf 
4761da177e4SLinus Torvalds 	if (ifm->ifi_index >= 0 && ida[IFLA_IFNAME - 1]) {
4771da177e4SLinus Torvalds 		char ifname[IFNAMSIZ];
4781da177e4SLinus Torvalds 
4791da177e4SLinus Torvalds 		if (rtattr_strlcpy(ifname, ida[IFLA_IFNAME - 1],
4801da177e4SLinus Torvalds 		                   IFNAMSIZ) >= IFNAMSIZ)
4811da177e4SLinus Torvalds 			goto out;
4821da177e4SLinus Torvalds 		err = dev_change_name(dev, ifname);
4831da177e4SLinus Torvalds 		if (err)
4841da177e4SLinus Torvalds 			goto out;
4851da177e4SLinus Torvalds 	}
4861da177e4SLinus Torvalds 
487711e2c33SJean Tourrilhes #ifdef CONFIG_NET_WIRELESS_RTNETLINK
488711e2c33SJean Tourrilhes 	if (ida[IFLA_WIRELESS - 1]) {
489711e2c33SJean Tourrilhes 
490711e2c33SJean Tourrilhes 		/* Call Wireless Extensions.
491711e2c33SJean Tourrilhes 		 * Various stuff checked in there... */
492711e2c33SJean Tourrilhes 		err = wireless_rtnetlink_set(dev, RTA_DATA(ida[IFLA_WIRELESS - 1]), ida[IFLA_WIRELESS - 1]->rta_len);
493711e2c33SJean Tourrilhes 		if (err)
494711e2c33SJean Tourrilhes 			goto out;
495711e2c33SJean Tourrilhes 	}
496711e2c33SJean Tourrilhes #endif	/* CONFIG_NET_WIRELESS_RTNETLINK */
497711e2c33SJean Tourrilhes 
4981da177e4SLinus Torvalds 	err = 0;
4991da177e4SLinus Torvalds 
5001da177e4SLinus Torvalds out:
5011da177e4SLinus Torvalds 	if (send_addr_notify)
5021da177e4SLinus Torvalds 		call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
5031da177e4SLinus Torvalds 
5041da177e4SLinus Torvalds 	dev_put(dev);
5051da177e4SLinus Torvalds 	return err;
5061da177e4SLinus Torvalds }
5071da177e4SLinus Torvalds 
508711e2c33SJean Tourrilhes #ifdef CONFIG_NET_WIRELESS_RTNETLINK
509711e2c33SJean Tourrilhes static int do_getlink(struct sk_buff *in_skb, struct nlmsghdr* in_nlh, void *arg)
510711e2c33SJean Tourrilhes {
511711e2c33SJean Tourrilhes 	struct ifinfomsg  *ifm = NLMSG_DATA(in_nlh);
512711e2c33SJean Tourrilhes 	struct rtattr    **ida = arg;
513711e2c33SJean Tourrilhes 	struct net_device *dev;
514711e2c33SJean Tourrilhes 	struct ifinfomsg *r;
515711e2c33SJean Tourrilhes 	struct nlmsghdr  *nlh;
516711e2c33SJean Tourrilhes 	int err = -ENOBUFS;
517711e2c33SJean Tourrilhes 	struct sk_buff *skb;
518711e2c33SJean Tourrilhes 	unsigned char	 *b;
519711e2c33SJean Tourrilhes 	char *iw_buf = NULL;
520711e2c33SJean Tourrilhes 	int iw_buf_len = 0;
521711e2c33SJean Tourrilhes 
522711e2c33SJean Tourrilhes 	if (ifm->ifi_index >= 0)
523711e2c33SJean Tourrilhes 		dev = dev_get_by_index(ifm->ifi_index);
524711e2c33SJean Tourrilhes 	else
525711e2c33SJean Tourrilhes 		return -EINVAL;
526711e2c33SJean Tourrilhes 	if (!dev)
527711e2c33SJean Tourrilhes 		return -ENODEV;
528711e2c33SJean Tourrilhes 
529711e2c33SJean Tourrilhes #ifdef CONFIG_NET_WIRELESS_RTNETLINK
530711e2c33SJean Tourrilhes 	if (ida[IFLA_WIRELESS - 1]) {
531711e2c33SJean Tourrilhes 
532711e2c33SJean Tourrilhes 		/* Call Wireless Extensions. We need to know the size before
533711e2c33SJean Tourrilhes 		 * we can alloc. Various stuff checked in there... */
534711e2c33SJean Tourrilhes 		err = wireless_rtnetlink_get(dev, RTA_DATA(ida[IFLA_WIRELESS - 1]), ida[IFLA_WIRELESS - 1]->rta_len, &iw_buf, &iw_buf_len);
535711e2c33SJean Tourrilhes 		if (err)
536711e2c33SJean Tourrilhes 			goto out;
537711e2c33SJean Tourrilhes 	}
538711e2c33SJean Tourrilhes #endif	/* CONFIG_NET_WIRELESS_RTNETLINK */
539711e2c33SJean Tourrilhes 
540711e2c33SJean Tourrilhes 	/* Create a skb big enough to include all the data.
541711e2c33SJean Tourrilhes 	 * Some requests are way bigger than 4k... Jean II */
542711e2c33SJean Tourrilhes 	skb = alloc_skb((NLMSG_LENGTH(sizeof(*r))) + (RTA_SPACE(iw_buf_len)),
543711e2c33SJean Tourrilhes 			GFP_KERNEL);
544711e2c33SJean Tourrilhes 	if (!skb)
545711e2c33SJean Tourrilhes 		goto out;
546711e2c33SJean Tourrilhes 	b = skb->tail;
547711e2c33SJean Tourrilhes 
548711e2c33SJean Tourrilhes 	/* Put in the message the usual good stuff */
549711e2c33SJean Tourrilhes 	nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid, in_nlh->nlmsg_seq,
550711e2c33SJean Tourrilhes 			RTM_NEWLINK, sizeof(*r));
551711e2c33SJean Tourrilhes 	r = NLMSG_DATA(nlh);
552711e2c33SJean Tourrilhes 	r->ifi_family = AF_UNSPEC;
553711e2c33SJean Tourrilhes 	r->__ifi_pad = 0;
554711e2c33SJean Tourrilhes 	r->ifi_type = dev->type;
555711e2c33SJean Tourrilhes 	r->ifi_index = dev->ifindex;
556711e2c33SJean Tourrilhes 	r->ifi_flags = dev->flags;
557711e2c33SJean Tourrilhes 	r->ifi_change = 0;
558711e2c33SJean Tourrilhes 
559711e2c33SJean Tourrilhes 	/* Put the wireless payload if it exist */
560711e2c33SJean Tourrilhes 	if(iw_buf != NULL)
561711e2c33SJean Tourrilhes 		RTA_PUT(skb, IFLA_WIRELESS, iw_buf_len,
562711e2c33SJean Tourrilhes 			iw_buf + IW_EV_POINT_OFF);
563711e2c33SJean Tourrilhes 
564711e2c33SJean Tourrilhes 	nlh->nlmsg_len = skb->tail - b;
565711e2c33SJean Tourrilhes 
566711e2c33SJean Tourrilhes 	/* Needed ? */
567711e2c33SJean Tourrilhes 	NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
568711e2c33SJean Tourrilhes 
569711e2c33SJean Tourrilhes 	err = netlink_unicast(rtnl, skb, NETLINK_CB(in_skb).pid, MSG_DONTWAIT);
570711e2c33SJean Tourrilhes 	if (err > 0)
571711e2c33SJean Tourrilhes 		err = 0;
572711e2c33SJean Tourrilhes out:
573711e2c33SJean Tourrilhes 	if(iw_buf != NULL)
574711e2c33SJean Tourrilhes 		kfree(iw_buf);
575711e2c33SJean Tourrilhes 	dev_put(dev);
576711e2c33SJean Tourrilhes 	return err;
577711e2c33SJean Tourrilhes 
578711e2c33SJean Tourrilhes rtattr_failure:
579711e2c33SJean Tourrilhes nlmsg_failure:
580711e2c33SJean Tourrilhes 	kfree_skb(skb);
581711e2c33SJean Tourrilhes 	goto out;
582711e2c33SJean Tourrilhes }
583711e2c33SJean Tourrilhes #endif	/* CONFIG_NET_WIRELESS_RTNETLINK */
584711e2c33SJean Tourrilhes 
5851da177e4SLinus Torvalds static int rtnetlink_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
5861da177e4SLinus Torvalds {
5871da177e4SLinus Torvalds 	int idx;
5881da177e4SLinus Torvalds 	int s_idx = cb->family;
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds 	if (s_idx == 0)
5911da177e4SLinus Torvalds 		s_idx = 1;
5921da177e4SLinus Torvalds 	for (idx=1; idx<NPROTO; idx++) {
5931da177e4SLinus Torvalds 		int type = cb->nlh->nlmsg_type-RTM_BASE;
5941da177e4SLinus Torvalds 		if (idx < s_idx || idx == PF_PACKET)
5951da177e4SLinus Torvalds 			continue;
5961da177e4SLinus Torvalds 		if (rtnetlink_links[idx] == NULL ||
5971da177e4SLinus Torvalds 		    rtnetlink_links[idx][type].dumpit == NULL)
5981da177e4SLinus Torvalds 			continue;
5991da177e4SLinus Torvalds 		if (idx > s_idx)
6001da177e4SLinus Torvalds 			memset(&cb->args[0], 0, sizeof(cb->args));
6011da177e4SLinus Torvalds 		if (rtnetlink_links[idx][type].dumpit(skb, cb))
6021da177e4SLinus Torvalds 			break;
6031da177e4SLinus Torvalds 	}
6041da177e4SLinus Torvalds 	cb->family = idx;
6051da177e4SLinus Torvalds 
6061da177e4SLinus Torvalds 	return skb->len;
6071da177e4SLinus Torvalds }
6081da177e4SLinus Torvalds 
6091da177e4SLinus Torvalds void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change)
6101da177e4SLinus Torvalds {
6111da177e4SLinus Torvalds 	struct sk_buff *skb;
6121da177e4SLinus Torvalds 	int size = NLMSG_SPACE(sizeof(struct ifinfomsg) +
6131da177e4SLinus Torvalds 			       sizeof(struct rtnl_link_ifmap) +
6141da177e4SLinus Torvalds 			       sizeof(struct rtnl_link_stats) + 128);
6151da177e4SLinus Torvalds 
6161da177e4SLinus Torvalds 	skb = alloc_skb(size, GFP_KERNEL);
6171da177e4SLinus Torvalds 	if (!skb)
6181da177e4SLinus Torvalds 		return;
6191da177e4SLinus Torvalds 
62028633514SAlexey Kuznetsov 	if (rtnetlink_fill_ifinfo(skb, dev, type, 0, 0, change, 0) < 0) {
6211da177e4SLinus Torvalds 		kfree_skb(skb);
6221da177e4SLinus Torvalds 		return;
6231da177e4SLinus Torvalds 	}
624ac6d439dSPatrick McHardy 	NETLINK_CB(skb).dst_group = RTNLGRP_LINK;
625ac6d439dSPatrick McHardy 	netlink_broadcast(rtnl, skb, 0, RTNLGRP_LINK, GFP_KERNEL);
6261da177e4SLinus Torvalds }
6271da177e4SLinus Torvalds 
6281da177e4SLinus Torvalds /* Protected by RTNL sempahore.  */
6291da177e4SLinus Torvalds static struct rtattr **rta_buf;
6301da177e4SLinus Torvalds static int rtattr_max;
6311da177e4SLinus Torvalds 
6321da177e4SLinus Torvalds /* Process one rtnetlink message. */
6331da177e4SLinus Torvalds 
6341da177e4SLinus Torvalds static __inline__ int
6351da177e4SLinus Torvalds rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
6361da177e4SLinus Torvalds {
6371da177e4SLinus Torvalds 	struct rtnetlink_link *link;
6381da177e4SLinus Torvalds 	struct rtnetlink_link *link_tab;
6391da177e4SLinus Torvalds 	int sz_idx, kind;
6401da177e4SLinus Torvalds 	int min_len;
6411da177e4SLinus Torvalds 	int family;
6421da177e4SLinus Torvalds 	int type;
6431da177e4SLinus Torvalds 	int err;
6441da177e4SLinus Torvalds 
6451da177e4SLinus Torvalds 	/* Only requests are handled by kernel now */
6461da177e4SLinus Torvalds 	if (!(nlh->nlmsg_flags&NLM_F_REQUEST))
6471da177e4SLinus Torvalds 		return 0;
6481da177e4SLinus Torvalds 
6491da177e4SLinus Torvalds 	type = nlh->nlmsg_type;
6501da177e4SLinus Torvalds 
6511da177e4SLinus Torvalds 	/* A control message: ignore them */
6521da177e4SLinus Torvalds 	if (type < RTM_BASE)
6531da177e4SLinus Torvalds 		return 0;
6541da177e4SLinus Torvalds 
6551da177e4SLinus Torvalds 	/* Unknown message: reply with EINVAL */
6561da177e4SLinus Torvalds 	if (type > RTM_MAX)
6571da177e4SLinus Torvalds 		goto err_inval;
6581da177e4SLinus Torvalds 
6591da177e4SLinus Torvalds 	type -= RTM_BASE;
6601da177e4SLinus Torvalds 
6611da177e4SLinus Torvalds 	/* All the messages must have at least 1 byte length */
6621da177e4SLinus Torvalds 	if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg)))
6631da177e4SLinus Torvalds 		return 0;
6641da177e4SLinus Torvalds 
6651da177e4SLinus Torvalds 	family = ((struct rtgenmsg*)NLMSG_DATA(nlh))->rtgen_family;
6661da177e4SLinus Torvalds 	if (family >= NPROTO) {
6671da177e4SLinus Torvalds 		*errp = -EAFNOSUPPORT;
6681da177e4SLinus Torvalds 		return -1;
6691da177e4SLinus Torvalds 	}
6701da177e4SLinus Torvalds 
6711da177e4SLinus Torvalds 	link_tab = rtnetlink_links[family];
6721da177e4SLinus Torvalds 	if (link_tab == NULL)
6731da177e4SLinus Torvalds 		link_tab = rtnetlink_links[PF_UNSPEC];
6741da177e4SLinus Torvalds 	link = &link_tab[type];
6751da177e4SLinus Torvalds 
6761da177e4SLinus Torvalds 	sz_idx = type>>2;
6771da177e4SLinus Torvalds 	kind = type&3;
6781da177e4SLinus Torvalds 
679c7bdb545SDarrel Goeddel 	if (kind != 2 && security_netlink_recv(skb, CAP_NET_ADMIN)) {
6801da177e4SLinus Torvalds 		*errp = -EPERM;
6811da177e4SLinus Torvalds 		return -1;
6821da177e4SLinus Torvalds 	}
6831da177e4SLinus Torvalds 
6841da177e4SLinus Torvalds 	if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
6851da177e4SLinus Torvalds 		if (link->dumpit == NULL)
6861da177e4SLinus Torvalds 			link = &(rtnetlink_links[PF_UNSPEC][type]);
6871da177e4SLinus Torvalds 
6881da177e4SLinus Torvalds 		if (link->dumpit == NULL)
6891da177e4SLinus Torvalds 			goto err_inval;
6901da177e4SLinus Torvalds 
6911da177e4SLinus Torvalds 		if ((*errp = netlink_dump_start(rtnl, skb, nlh,
692a8f74b22SThomas Graf 						link->dumpit, NULL)) != 0) {
6931da177e4SLinus Torvalds 			return -1;
6941da177e4SLinus Torvalds 		}
6959ac4a169SThomas Graf 
6969ac4a169SThomas Graf 		netlink_queue_skip(nlh, skb);
6971da177e4SLinus Torvalds 		return -1;
6981da177e4SLinus Torvalds 	}
6991da177e4SLinus Torvalds 
7001da177e4SLinus Torvalds 	memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *)));
7011da177e4SLinus Torvalds 
7021da177e4SLinus Torvalds 	min_len = rtm_min[sz_idx];
7031da177e4SLinus Torvalds 	if (nlh->nlmsg_len < min_len)
7041da177e4SLinus Torvalds 		goto err_inval;
7051da177e4SLinus Torvalds 
7061da177e4SLinus Torvalds 	if (nlh->nlmsg_len > min_len) {
7071da177e4SLinus Torvalds 		int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
7081da177e4SLinus Torvalds 		struct rtattr *attr = (void*)nlh + NLMSG_ALIGN(min_len);
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds 		while (RTA_OK(attr, attrlen)) {
7111da177e4SLinus Torvalds 			unsigned flavor = attr->rta_type;
7121da177e4SLinus Torvalds 			if (flavor) {
7131da177e4SLinus Torvalds 				if (flavor > rta_max[sz_idx])
7141da177e4SLinus Torvalds 					goto err_inval;
7151da177e4SLinus Torvalds 				rta_buf[flavor-1] = attr;
7161da177e4SLinus Torvalds 			}
7171da177e4SLinus Torvalds 			attr = RTA_NEXT(attr, attrlen);
7181da177e4SLinus Torvalds 		}
7191da177e4SLinus Torvalds 	}
7201da177e4SLinus Torvalds 
7211da177e4SLinus Torvalds 	if (link->doit == NULL)
7221da177e4SLinus Torvalds 		link = &(rtnetlink_links[PF_UNSPEC][type]);
7231da177e4SLinus Torvalds 	if (link->doit == NULL)
7241da177e4SLinus Torvalds 		goto err_inval;
7251da177e4SLinus Torvalds 	err = link->doit(skb, nlh, (void *)&rta_buf[0]);
7261da177e4SLinus Torvalds 
7271da177e4SLinus Torvalds 	*errp = err;
7281da177e4SLinus Torvalds 	return err;
7291da177e4SLinus Torvalds 
7301da177e4SLinus Torvalds err_inval:
7311da177e4SLinus Torvalds 	*errp = -EINVAL;
7321da177e4SLinus Torvalds 	return -1;
7331da177e4SLinus Torvalds }
7341da177e4SLinus Torvalds 
7351da177e4SLinus Torvalds static void rtnetlink_rcv(struct sock *sk, int len)
7361da177e4SLinus Torvalds {
7379ac4a169SThomas Graf 	unsigned int qlen = 0;
7382a0a6ebeSHerbert Xu 
7391da177e4SLinus Torvalds 	do {
7406756ae4bSStephen Hemminger 		mutex_lock(&rtnl_mutex);
7419ac4a169SThomas Graf 		netlink_run_queue(sk, &qlen, &rtnetlink_rcv_msg);
7426756ae4bSStephen Hemminger 		mutex_unlock(&rtnl_mutex);
7431da177e4SLinus Torvalds 
7441da177e4SLinus Torvalds 		netdev_run_todo();
7452a0a6ebeSHerbert Xu 	} while (qlen);
7461da177e4SLinus Torvalds }
7471da177e4SLinus Torvalds 
748db46edc6SThomas Graf static struct rtnetlink_link link_rtnetlink_table[RTM_NR_MSGTYPES] =
7491da177e4SLinus Torvalds {
750711e2c33SJean Tourrilhes 	[RTM_GETLINK     - RTM_BASE] = {
751711e2c33SJean Tourrilhes #ifdef CONFIG_NET_WIRELESS_RTNETLINK
752711e2c33SJean Tourrilhes 					 .doit   = do_getlink,
753711e2c33SJean Tourrilhes #endif	/* CONFIG_NET_WIRELESS_RTNETLINK */
754711e2c33SJean Tourrilhes 					 .dumpit = rtnetlink_dump_ifinfo },
7551da177e4SLinus Torvalds 	[RTM_SETLINK     - RTM_BASE] = { .doit   = do_setlink		 },
7561da177e4SLinus Torvalds 	[RTM_GETADDR     - RTM_BASE] = { .dumpit = rtnetlink_dump_all	 },
7571da177e4SLinus Torvalds 	[RTM_GETROUTE    - RTM_BASE] = { .dumpit = rtnetlink_dump_all	 },
7581da177e4SLinus Torvalds 	[RTM_NEWNEIGH    - RTM_BASE] = { .doit   = neigh_add		 },
7591da177e4SLinus Torvalds 	[RTM_DELNEIGH    - RTM_BASE] = { .doit   = neigh_delete		 },
760240eed95SThomas Graf 	[RTM_GETNEIGH    - RTM_BASE] = { .dumpit = neigh_dump_info	 },
761*14c0b97dSThomas Graf #ifdef CONFIG_FIB_RULES
762*14c0b97dSThomas Graf 	[RTM_NEWRULE     - RTM_BASE] = { .doit   = fib_nl_newrule	 },
763*14c0b97dSThomas Graf 	[RTM_DELRULE     - RTM_BASE] = { .doit   = fib_nl_delrule	 },
764*14c0b97dSThomas Graf #endif
76598f245e7SDavid S. Miller 	[RTM_GETRULE     - RTM_BASE] = { .dumpit = rtnetlink_dump_all	 },
766c7fb64dbSThomas Graf 	[RTM_GETNEIGHTBL - RTM_BASE] = { .dumpit = neightbl_dump_info	 },
767c7fb64dbSThomas Graf 	[RTM_SETNEIGHTBL - RTM_BASE] = { .doit   = neightbl_set		 },
7681da177e4SLinus Torvalds };
7691da177e4SLinus Torvalds 
7701da177e4SLinus Torvalds static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
7711da177e4SLinus Torvalds {
7721da177e4SLinus Torvalds 	struct net_device *dev = ptr;
7731da177e4SLinus Torvalds 	switch (event) {
7741da177e4SLinus Torvalds 	case NETDEV_UNREGISTER:
7751da177e4SLinus Torvalds 		rtmsg_ifinfo(RTM_DELLINK, dev, ~0U);
7761da177e4SLinus Torvalds 		break;
7771da177e4SLinus Torvalds 	case NETDEV_REGISTER:
7781da177e4SLinus Torvalds 		rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
7791da177e4SLinus Torvalds 		break;
7801da177e4SLinus Torvalds 	case NETDEV_UP:
7811da177e4SLinus Torvalds 	case NETDEV_DOWN:
7821da177e4SLinus Torvalds 		rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
7831da177e4SLinus Torvalds 		break;
7841da177e4SLinus Torvalds 	case NETDEV_CHANGE:
7851da177e4SLinus Torvalds 	case NETDEV_GOING_DOWN:
7861da177e4SLinus Torvalds 		break;
7871da177e4SLinus Torvalds 	default:
7881da177e4SLinus Torvalds 		rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
7891da177e4SLinus Torvalds 		break;
7901da177e4SLinus Torvalds 	}
7911da177e4SLinus Torvalds 	return NOTIFY_DONE;
7921da177e4SLinus Torvalds }
7931da177e4SLinus Torvalds 
7941da177e4SLinus Torvalds static struct notifier_block rtnetlink_dev_notifier = {
7951da177e4SLinus Torvalds 	.notifier_call	= rtnetlink_event,
7961da177e4SLinus Torvalds };
7971da177e4SLinus Torvalds 
7981da177e4SLinus Torvalds void __init rtnetlink_init(void)
7991da177e4SLinus Torvalds {
8001da177e4SLinus Torvalds 	int i;
8011da177e4SLinus Torvalds 
8021da177e4SLinus Torvalds 	rtattr_max = 0;
8031da177e4SLinus Torvalds 	for (i = 0; i < ARRAY_SIZE(rta_max); i++)
8041da177e4SLinus Torvalds 		if (rta_max[i] > rtattr_max)
8051da177e4SLinus Torvalds 			rtattr_max = rta_max[i];
8061da177e4SLinus Torvalds 	rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL);
8071da177e4SLinus Torvalds 	if (!rta_buf)
8081da177e4SLinus Torvalds 		panic("rtnetlink_init: cannot allocate rta_buf\n");
8091da177e4SLinus Torvalds 
81006628607SPatrick McHardy 	rtnl = netlink_kernel_create(NETLINK_ROUTE, RTNLGRP_MAX, rtnetlink_rcv,
81106628607SPatrick McHardy 	                             THIS_MODULE);
8121da177e4SLinus Torvalds 	if (rtnl == NULL)
8131da177e4SLinus Torvalds 		panic("rtnetlink_init: cannot initialize rtnetlink\n");
8141da177e4SLinus Torvalds 	netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV);
8151da177e4SLinus Torvalds 	register_netdevice_notifier(&rtnetlink_dev_notifier);
8161da177e4SLinus Torvalds 	rtnetlink_links[PF_UNSPEC] = link_rtnetlink_table;
8171da177e4SLinus Torvalds 	rtnetlink_links[PF_PACKET] = link_rtnetlink_table;
8181da177e4SLinus Torvalds }
8191da177e4SLinus Torvalds 
8201da177e4SLinus Torvalds EXPORT_SYMBOL(__rta_fill);
8211da177e4SLinus Torvalds EXPORT_SYMBOL(rtattr_strlcpy);
8221da177e4SLinus Torvalds EXPORT_SYMBOL(rtattr_parse);
8231da177e4SLinus Torvalds EXPORT_SYMBOL(rtnetlink_links);
8241da177e4SLinus Torvalds EXPORT_SYMBOL(rtnetlink_put_metrics);
8251da177e4SLinus Torvalds EXPORT_SYMBOL(rtnl);
8261da177e4SLinus Torvalds EXPORT_SYMBOL(rtnl_lock);
8276756ae4bSStephen Hemminger EXPORT_SYMBOL(rtnl_trylock);
8281da177e4SLinus Torvalds EXPORT_SYMBOL(rtnl_unlock);
829