xref: /openbmc/linux/net/core/rtnetlink.c (revision b6544c0b)
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/config.h>
201da177e4SLinus Torvalds #include <linux/errno.h>
211da177e4SLinus Torvalds #include <linux/module.h>
221da177e4SLinus Torvalds #include <linux/types.h>
231da177e4SLinus Torvalds #include <linux/socket.h>
241da177e4SLinus Torvalds #include <linux/kernel.h>
251da177e4SLinus Torvalds #include <linux/sched.h>
261da177e4SLinus Torvalds #include <linux/timer.h>
271da177e4SLinus Torvalds #include <linux/string.h>
281da177e4SLinus Torvalds #include <linux/sockios.h>
291da177e4SLinus Torvalds #include <linux/net.h>
301da177e4SLinus Torvalds #include <linux/fcntl.h>
311da177e4SLinus Torvalds #include <linux/mm.h>
321da177e4SLinus Torvalds #include <linux/slab.h>
331da177e4SLinus Torvalds #include <linux/interrupt.h>
341da177e4SLinus Torvalds #include <linux/capability.h>
351da177e4SLinus Torvalds #include <linux/skbuff.h>
361da177e4SLinus Torvalds #include <linux/init.h>
371da177e4SLinus Torvalds #include <linux/security.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>
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds DECLARE_MUTEX(rtnl_sem);
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds void rtnl_lock(void)
561da177e4SLinus Torvalds {
571da177e4SLinus Torvalds 	rtnl_shlock();
581da177e4SLinus Torvalds }
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds int rtnl_lock_interruptible(void)
611da177e4SLinus Torvalds {
621da177e4SLinus Torvalds 	return down_interruptible(&rtnl_sem);
631da177e4SLinus Torvalds }
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds void rtnl_unlock(void)
661da177e4SLinus Torvalds {
671da177e4SLinus Torvalds 	rtnl_shunlock();
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds 	netdev_run_todo();
701da177e4SLinus Torvalds }
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds int rtattr_parse(struct rtattr *tb[], int maxattr, struct rtattr *rta, int len)
731da177e4SLinus Torvalds {
741da177e4SLinus Torvalds 	memset(tb, 0, sizeof(struct rtattr*)*maxattr);
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds 	while (RTA_OK(rta, len)) {
771da177e4SLinus Torvalds 		unsigned flavor = rta->rta_type;
781da177e4SLinus Torvalds 		if (flavor && flavor <= maxattr)
791da177e4SLinus Torvalds 			tb[flavor-1] = rta;
801da177e4SLinus Torvalds 		rta = RTA_NEXT(rta, len);
811da177e4SLinus Torvalds 	}
821da177e4SLinus Torvalds 	return 0;
831da177e4SLinus Torvalds }
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds struct sock *rtnl;
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds struct rtnetlink_link * rtnetlink_links[NPROTO];
881da177e4SLinus Torvalds 
89db46edc6SThomas Graf static const int rtm_min[RTM_NR_FAMILIES] =
901da177e4SLinus Torvalds {
91f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWLINK)]      = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
92f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWADDR)]      = NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
93f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWROUTE)]     = NLMSG_LENGTH(sizeof(struct rtmsg)),
94f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWNEIGH)]     = NLMSG_LENGTH(sizeof(struct ndmsg)),
95f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWRULE)]      = NLMSG_LENGTH(sizeof(struct rtmsg)),
96f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWQDISC)]     = NLMSG_LENGTH(sizeof(struct tcmsg)),
97f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWTCLASS)]    = NLMSG_LENGTH(sizeof(struct tcmsg)),
98f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWTFILTER)]   = NLMSG_LENGTH(sizeof(struct tcmsg)),
99f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWACTION)]    = NLMSG_LENGTH(sizeof(struct tcamsg)),
100f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWPREFIX)]    = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
101f90a0a74SThomas Graf 	[RTM_FAM(RTM_GETMULTICAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
102f90a0a74SThomas Graf 	[RTM_FAM(RTM_GETANYCAST)]   = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
103c7fb64dbSThomas Graf 	[RTM_FAM(RTM_NEWNEIGHTBL)]  = NLMSG_LENGTH(sizeof(struct ndtmsg)),
1041da177e4SLinus Torvalds };
1051da177e4SLinus Torvalds 
106db46edc6SThomas Graf static const int rta_max[RTM_NR_FAMILIES] =
1071da177e4SLinus Torvalds {
108f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWLINK)]      = IFLA_MAX,
109f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWADDR)]      = IFA_MAX,
110f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWROUTE)]     = RTA_MAX,
111f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWNEIGH)]     = NDA_MAX,
112f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWRULE)]      = RTA_MAX,
113f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWQDISC)]     = TCA_MAX,
114f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWTCLASS)]    = TCA_MAX,
115f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWTFILTER)]   = TCA_MAX,
116f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWACTION)]    = TCAA_MAX,
117c7fb64dbSThomas Graf 	[RTM_FAM(RTM_NEWNEIGHTBL)]  = NDTA_MAX,
1181da177e4SLinus Torvalds };
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
1211da177e4SLinus Torvalds {
1221da177e4SLinus Torvalds 	struct rtattr *rta;
1231da177e4SLinus Torvalds 	int size = RTA_LENGTH(attrlen);
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds 	rta = (struct rtattr*)skb_put(skb, RTA_ALIGN(size));
1261da177e4SLinus Torvalds 	rta->rta_type = attrtype;
1271da177e4SLinus Torvalds 	rta->rta_len = size;
1281da177e4SLinus Torvalds 	memcpy(RTA_DATA(rta), data, attrlen);
1291da177e4SLinus Torvalds }
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds size_t rtattr_strlcpy(char *dest, const struct rtattr *rta, size_t size)
1321da177e4SLinus Torvalds {
1331da177e4SLinus Torvalds 	size_t ret = RTA_PAYLOAD(rta);
1341da177e4SLinus Torvalds 	char *src = RTA_DATA(rta);
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds 	if (ret > 0 && src[ret - 1] == '\0')
1371da177e4SLinus Torvalds 		ret--;
1381da177e4SLinus Torvalds 	if (size > 0) {
1391da177e4SLinus Torvalds 		size_t len = (ret >= size) ? size - 1 : ret;
1401da177e4SLinus Torvalds 		memset(dest, 0, size);
1411da177e4SLinus Torvalds 		memcpy(dest, src, len);
1421da177e4SLinus Torvalds 	}
1431da177e4SLinus Torvalds 	return ret;
1441da177e4SLinus Torvalds }
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds int rtnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo)
1471da177e4SLinus Torvalds {
1481da177e4SLinus Torvalds 	int err = 0;
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds 	NETLINK_CB(skb).dst_groups = group;
1511da177e4SLinus Torvalds 	if (echo)
1521da177e4SLinus Torvalds 		atomic_inc(&skb->users);
1531da177e4SLinus Torvalds 	netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
1541da177e4SLinus Torvalds 	if (echo)
1551da177e4SLinus Torvalds 		err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
1561da177e4SLinus Torvalds 	return err;
1571da177e4SLinus Torvalds }
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
1601da177e4SLinus Torvalds {
1611da177e4SLinus Torvalds 	struct rtattr *mx = (struct rtattr*)skb->tail;
1621da177e4SLinus Torvalds 	int i;
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 	RTA_PUT(skb, RTA_METRICS, 0, NULL);
1651da177e4SLinus Torvalds 	for (i=0; i<RTAX_MAX; i++) {
1661da177e4SLinus Torvalds 		if (metrics[i])
1671da177e4SLinus Torvalds 			RTA_PUT(skb, i+1, sizeof(u32), metrics+i);
1681da177e4SLinus Torvalds 	}
1691da177e4SLinus Torvalds 	mx->rta_len = skb->tail - (u8*)mx;
1701da177e4SLinus Torvalds 	if (mx->rta_len == RTA_LENGTH(0))
1711da177e4SLinus Torvalds 		skb_trim(skb, (u8*)mx - skb->data);
1721da177e4SLinus Torvalds 	return 0;
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds rtattr_failure:
1751da177e4SLinus Torvalds 	skb_trim(skb, (u8*)mx - skb->data);
1761da177e4SLinus Torvalds 	return -1;
1771da177e4SLinus Torvalds }
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds static int rtnetlink_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
181b6544c0bSJamal Hadi Salim 				 int type, u32 pid, u32 seq, u32 change,
182b6544c0bSJamal Hadi Salim 				 unsigned int flags)
1831da177e4SLinus Torvalds {
1841da177e4SLinus Torvalds 	struct ifinfomsg *r;
1851da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
1861da177e4SLinus Torvalds 	unsigned char	 *b = skb->tail;
1871da177e4SLinus Torvalds 
188b6544c0bSJamal Hadi Salim 	nlh = NLMSG_NEW(skb, pid, seq, type, sizeof(*r), flags);
1891da177e4SLinus Torvalds 	r = NLMSG_DATA(nlh);
1901da177e4SLinus Torvalds 	r->ifi_family = AF_UNSPEC;
1911da177e4SLinus Torvalds 	r->ifi_type = dev->type;
1921da177e4SLinus Torvalds 	r->ifi_index = dev->ifindex;
1931da177e4SLinus Torvalds 	r->ifi_flags = dev_get_flags(dev);
1941da177e4SLinus Torvalds 	r->ifi_change = change;
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds 	RTA_PUT(skb, IFLA_IFNAME, strlen(dev->name)+1, dev->name);
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds 	if (1) {
1991da177e4SLinus Torvalds 		u32 txqlen = dev->tx_queue_len;
2001da177e4SLinus Torvalds 		RTA_PUT(skb, IFLA_TXQLEN, sizeof(txqlen), &txqlen);
2011da177e4SLinus Torvalds 	}
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds 	if (1) {
2041da177e4SLinus Torvalds 		u32 weight = dev->weight;
2051da177e4SLinus Torvalds 		RTA_PUT(skb, IFLA_WEIGHT, sizeof(weight), &weight);
2061da177e4SLinus Torvalds 	}
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds 	if (1) {
2091da177e4SLinus Torvalds 		struct rtnl_link_ifmap map = {
2101da177e4SLinus Torvalds 			.mem_start   = dev->mem_start,
2111da177e4SLinus Torvalds 			.mem_end     = dev->mem_end,
2121da177e4SLinus Torvalds 			.base_addr   = dev->base_addr,
2131da177e4SLinus Torvalds 			.irq         = dev->irq,
2141da177e4SLinus Torvalds 			.dma         = dev->dma,
2151da177e4SLinus Torvalds 			.port        = dev->if_port,
2161da177e4SLinus Torvalds 		};
2171da177e4SLinus Torvalds 		RTA_PUT(skb, IFLA_MAP, sizeof(map), &map);
2181da177e4SLinus Torvalds 	}
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds 	if (dev->addr_len) {
2211da177e4SLinus Torvalds 		RTA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
2221da177e4SLinus Torvalds 		RTA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast);
2231da177e4SLinus Torvalds 	}
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds 	if (1) {
2261da177e4SLinus Torvalds 		u32 mtu = dev->mtu;
2271da177e4SLinus Torvalds 		RTA_PUT(skb, IFLA_MTU, sizeof(mtu), &mtu);
2281da177e4SLinus Torvalds 	}
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds 	if (dev->ifindex != dev->iflink) {
2311da177e4SLinus Torvalds 		u32 iflink = dev->iflink;
2321da177e4SLinus Torvalds 		RTA_PUT(skb, IFLA_LINK, sizeof(iflink), &iflink);
2331da177e4SLinus Torvalds 	}
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds 	if (dev->qdisc_sleeping)
2361da177e4SLinus Torvalds 		RTA_PUT(skb, IFLA_QDISC,
2371da177e4SLinus Torvalds 			strlen(dev->qdisc_sleeping->ops->id) + 1,
2381da177e4SLinus Torvalds 			dev->qdisc_sleeping->ops->id);
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds 	if (dev->master) {
2411da177e4SLinus Torvalds 		u32 master = dev->master->ifindex;
2421da177e4SLinus Torvalds 		RTA_PUT(skb, IFLA_MASTER, sizeof(master), &master);
2431da177e4SLinus Torvalds 	}
2441da177e4SLinus Torvalds 
2451da177e4SLinus Torvalds 	if (dev->get_stats) {
2461da177e4SLinus Torvalds 		unsigned long *stats = (unsigned long*)dev->get_stats(dev);
2471da177e4SLinus Torvalds 		if (stats) {
2481da177e4SLinus Torvalds 			struct rtattr  *a;
2491da177e4SLinus Torvalds 			__u32	       *s;
2501da177e4SLinus Torvalds 			int		i;
2511da177e4SLinus Torvalds 			int		n = sizeof(struct rtnl_link_stats)/4;
2521da177e4SLinus Torvalds 
2531da177e4SLinus Torvalds 			a = __RTA_PUT(skb, IFLA_STATS, n*4);
2541da177e4SLinus Torvalds 			s = RTA_DATA(a);
2551da177e4SLinus Torvalds 			for (i=0; i<n; i++)
2561da177e4SLinus Torvalds 				s[i] = stats[i];
2571da177e4SLinus Torvalds 		}
2581da177e4SLinus Torvalds 	}
2591da177e4SLinus Torvalds 	nlh->nlmsg_len = skb->tail - b;
2601da177e4SLinus Torvalds 	return skb->len;
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds nlmsg_failure:
2631da177e4SLinus Torvalds rtattr_failure:
2641da177e4SLinus Torvalds 	skb_trim(skb, b - skb->data);
2651da177e4SLinus Torvalds 	return -1;
2661da177e4SLinus Torvalds }
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds static int rtnetlink_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
2691da177e4SLinus Torvalds {
2701da177e4SLinus Torvalds 	int idx;
2711da177e4SLinus Torvalds 	int s_idx = cb->args[0];
2721da177e4SLinus Torvalds 	struct net_device *dev;
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds 	read_lock(&dev_base_lock);
2751da177e4SLinus Torvalds 	for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
2761da177e4SLinus Torvalds 		if (idx < s_idx)
2771da177e4SLinus Torvalds 			continue;
278b6544c0bSJamal Hadi Salim 		if (rtnetlink_fill_ifinfo(skb, dev, RTM_NEWLINK,
279b6544c0bSJamal Hadi Salim 					  NETLINK_CB(cb->skb).pid,
280b6544c0bSJamal Hadi Salim 					  cb->nlh->nlmsg_seq, 0,
281b6544c0bSJamal Hadi Salim 					  NLM_F_MULTI) <= 0)
2821da177e4SLinus Torvalds 			break;
2831da177e4SLinus Torvalds 	}
2841da177e4SLinus Torvalds 	read_unlock(&dev_base_lock);
2851da177e4SLinus Torvalds 	cb->args[0] = idx;
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds 	return skb->len;
2881da177e4SLinus Torvalds }
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds static int do_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
2911da177e4SLinus Torvalds {
2921da177e4SLinus Torvalds 	struct ifinfomsg  *ifm = NLMSG_DATA(nlh);
2931da177e4SLinus Torvalds 	struct rtattr    **ida = arg;
2941da177e4SLinus Torvalds 	struct net_device *dev;
2951da177e4SLinus Torvalds 	int err, send_addr_notify = 0;
2961da177e4SLinus Torvalds 
2971da177e4SLinus Torvalds 	if (ifm->ifi_index >= 0)
2981da177e4SLinus Torvalds 		dev = dev_get_by_index(ifm->ifi_index);
2991da177e4SLinus Torvalds 	else if (ida[IFLA_IFNAME - 1]) {
3001da177e4SLinus Torvalds 		char ifname[IFNAMSIZ];
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds 		if (rtattr_strlcpy(ifname, ida[IFLA_IFNAME - 1],
3031da177e4SLinus Torvalds 		                   IFNAMSIZ) >= IFNAMSIZ)
3041da177e4SLinus Torvalds 			return -EINVAL;
3051da177e4SLinus Torvalds 		dev = dev_get_by_name(ifname);
3061da177e4SLinus Torvalds 	} else
3071da177e4SLinus Torvalds 		return -EINVAL;
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds 	if (!dev)
3101da177e4SLinus Torvalds 		return -ENODEV;
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds 	err = -EINVAL;
3131da177e4SLinus Torvalds 
3141da177e4SLinus Torvalds 	if (ifm->ifi_flags)
3151da177e4SLinus Torvalds 		dev_change_flags(dev, ifm->ifi_flags);
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds 	if (ida[IFLA_MAP - 1]) {
3181da177e4SLinus Torvalds 		struct rtnl_link_ifmap *u_map;
3191da177e4SLinus Torvalds 		struct ifmap k_map;
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds 		if (!dev->set_config) {
3221da177e4SLinus Torvalds 			err = -EOPNOTSUPP;
3231da177e4SLinus Torvalds 			goto out;
3241da177e4SLinus Torvalds 		}
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds 		if (!netif_device_present(dev)) {
3271da177e4SLinus Torvalds 			err = -ENODEV;
3281da177e4SLinus Torvalds 			goto out;
3291da177e4SLinus Torvalds 		}
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds 		if (ida[IFLA_MAP - 1]->rta_len != RTA_LENGTH(sizeof(*u_map)))
3321da177e4SLinus Torvalds 			goto out;
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds 		u_map = RTA_DATA(ida[IFLA_MAP - 1]);
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 		k_map.mem_start = (unsigned long) u_map->mem_start;
3371da177e4SLinus Torvalds 		k_map.mem_end = (unsigned long) u_map->mem_end;
3381da177e4SLinus Torvalds 		k_map.base_addr = (unsigned short) u_map->base_addr;
3391da177e4SLinus Torvalds 		k_map.irq = (unsigned char) u_map->irq;
3401da177e4SLinus Torvalds 		k_map.dma = (unsigned char) u_map->dma;
3411da177e4SLinus Torvalds 		k_map.port = (unsigned char) u_map->port;
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds 		err = dev->set_config(dev, &k_map);
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds 		if (err)
3461da177e4SLinus Torvalds 			goto out;
3471da177e4SLinus Torvalds 	}
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds 	if (ida[IFLA_ADDRESS - 1]) {
3501da177e4SLinus Torvalds 		if (!dev->set_mac_address) {
3511da177e4SLinus Torvalds 			err = -EOPNOTSUPP;
3521da177e4SLinus Torvalds 			goto out;
3531da177e4SLinus Torvalds 		}
3541da177e4SLinus Torvalds 		if (!netif_device_present(dev)) {
3551da177e4SLinus Torvalds 			err = -ENODEV;
3561da177e4SLinus Torvalds 			goto out;
3571da177e4SLinus Torvalds 		}
3581da177e4SLinus Torvalds 		if (ida[IFLA_ADDRESS - 1]->rta_len != RTA_LENGTH(dev->addr_len))
3591da177e4SLinus Torvalds 			goto out;
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds 		err = dev->set_mac_address(dev, RTA_DATA(ida[IFLA_ADDRESS - 1]));
3621da177e4SLinus Torvalds 		if (err)
3631da177e4SLinus Torvalds 			goto out;
3641da177e4SLinus Torvalds 		send_addr_notify = 1;
3651da177e4SLinus Torvalds 	}
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds 	if (ida[IFLA_BROADCAST - 1]) {
3681da177e4SLinus Torvalds 		if (ida[IFLA_BROADCAST - 1]->rta_len != RTA_LENGTH(dev->addr_len))
3691da177e4SLinus Torvalds 			goto out;
3701da177e4SLinus Torvalds 		memcpy(dev->broadcast, RTA_DATA(ida[IFLA_BROADCAST - 1]),
3711da177e4SLinus Torvalds 		       dev->addr_len);
3721da177e4SLinus Torvalds 		send_addr_notify = 1;
3731da177e4SLinus Torvalds 	}
3741da177e4SLinus Torvalds 
3751da177e4SLinus Torvalds 	if (ida[IFLA_MTU - 1]) {
3761da177e4SLinus Torvalds 		if (ida[IFLA_MTU - 1]->rta_len != RTA_LENGTH(sizeof(u32)))
3771da177e4SLinus Torvalds 			goto out;
3781da177e4SLinus Torvalds 		err = dev_set_mtu(dev, *((u32 *) RTA_DATA(ida[IFLA_MTU - 1])));
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds 		if (err)
3811da177e4SLinus Torvalds 			goto out;
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds 	}
3841da177e4SLinus Torvalds 
3851da177e4SLinus Torvalds 	if (ida[IFLA_TXQLEN - 1]) {
3861da177e4SLinus Torvalds 		if (ida[IFLA_TXQLEN - 1]->rta_len != RTA_LENGTH(sizeof(u32)))
3871da177e4SLinus Torvalds 			goto out;
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds 		dev->tx_queue_len = *((u32 *) RTA_DATA(ida[IFLA_TXQLEN - 1]));
3901da177e4SLinus Torvalds 	}
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds 	if (ida[IFLA_WEIGHT - 1]) {
3931da177e4SLinus Torvalds 		if (ida[IFLA_WEIGHT - 1]->rta_len != RTA_LENGTH(sizeof(u32)))
3941da177e4SLinus Torvalds 			goto out;
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds 		dev->weight = *((u32 *) RTA_DATA(ida[IFLA_WEIGHT - 1]));
3971da177e4SLinus Torvalds 	}
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds 	if (ifm->ifi_index >= 0 && ida[IFLA_IFNAME - 1]) {
4001da177e4SLinus Torvalds 		char ifname[IFNAMSIZ];
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds 		if (rtattr_strlcpy(ifname, ida[IFLA_IFNAME - 1],
4031da177e4SLinus Torvalds 		                   IFNAMSIZ) >= IFNAMSIZ)
4041da177e4SLinus Torvalds 			goto out;
4051da177e4SLinus Torvalds 		err = dev_change_name(dev, ifname);
4061da177e4SLinus Torvalds 		if (err)
4071da177e4SLinus Torvalds 			goto out;
4081da177e4SLinus Torvalds 	}
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds 	err = 0;
4111da177e4SLinus Torvalds 
4121da177e4SLinus Torvalds out:
4131da177e4SLinus Torvalds 	if (send_addr_notify)
4141da177e4SLinus Torvalds 		call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds 	dev_put(dev);
4171da177e4SLinus Torvalds 	return err;
4181da177e4SLinus Torvalds }
4191da177e4SLinus Torvalds 
4201da177e4SLinus Torvalds static int rtnetlink_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
4211da177e4SLinus Torvalds {
4221da177e4SLinus Torvalds 	int idx;
4231da177e4SLinus Torvalds 	int s_idx = cb->family;
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds 	if (s_idx == 0)
4261da177e4SLinus Torvalds 		s_idx = 1;
4271da177e4SLinus Torvalds 	for (idx=1; idx<NPROTO; idx++) {
4281da177e4SLinus Torvalds 		int type = cb->nlh->nlmsg_type-RTM_BASE;
4291da177e4SLinus Torvalds 		if (idx < s_idx || idx == PF_PACKET)
4301da177e4SLinus Torvalds 			continue;
4311da177e4SLinus Torvalds 		if (rtnetlink_links[idx] == NULL ||
4321da177e4SLinus Torvalds 		    rtnetlink_links[idx][type].dumpit == NULL)
4331da177e4SLinus Torvalds 			continue;
4341da177e4SLinus Torvalds 		if (idx > s_idx)
4351da177e4SLinus Torvalds 			memset(&cb->args[0], 0, sizeof(cb->args));
4361da177e4SLinus Torvalds 		if (rtnetlink_links[idx][type].dumpit(skb, cb))
4371da177e4SLinus Torvalds 			break;
4381da177e4SLinus Torvalds 	}
4391da177e4SLinus Torvalds 	cb->family = idx;
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds 	return skb->len;
4421da177e4SLinus Torvalds }
4431da177e4SLinus Torvalds 
4441da177e4SLinus Torvalds void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change)
4451da177e4SLinus Torvalds {
4461da177e4SLinus Torvalds 	struct sk_buff *skb;
4471da177e4SLinus Torvalds 	int size = NLMSG_SPACE(sizeof(struct ifinfomsg) +
4481da177e4SLinus Torvalds 			       sizeof(struct rtnl_link_ifmap) +
4491da177e4SLinus Torvalds 			       sizeof(struct rtnl_link_stats) + 128);
4501da177e4SLinus Torvalds 
4511da177e4SLinus Torvalds 	skb = alloc_skb(size, GFP_KERNEL);
4521da177e4SLinus Torvalds 	if (!skb)
4531da177e4SLinus Torvalds 		return;
4541da177e4SLinus Torvalds 
455b6544c0bSJamal Hadi Salim 	if (rtnetlink_fill_ifinfo(skb, dev, type, 0, 0, change, 0) < 0) {
4561da177e4SLinus Torvalds 		kfree_skb(skb);
4571da177e4SLinus Torvalds 		return;
4581da177e4SLinus Torvalds 	}
4591da177e4SLinus Torvalds 	NETLINK_CB(skb).dst_groups = RTMGRP_LINK;
4601da177e4SLinus Torvalds 	netlink_broadcast(rtnl, skb, 0, RTMGRP_LINK, GFP_KERNEL);
4611da177e4SLinus Torvalds }
4621da177e4SLinus Torvalds 
4631da177e4SLinus Torvalds static int rtnetlink_done(struct netlink_callback *cb)
4641da177e4SLinus Torvalds {
4651da177e4SLinus Torvalds 	return 0;
4661da177e4SLinus Torvalds }
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds /* Protected by RTNL sempahore.  */
4691da177e4SLinus Torvalds static struct rtattr **rta_buf;
4701da177e4SLinus Torvalds static int rtattr_max;
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds /* Process one rtnetlink message. */
4731da177e4SLinus Torvalds 
4741da177e4SLinus Torvalds static __inline__ int
4751da177e4SLinus Torvalds rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
4761da177e4SLinus Torvalds {
4771da177e4SLinus Torvalds 	struct rtnetlink_link *link;
4781da177e4SLinus Torvalds 	struct rtnetlink_link *link_tab;
4791da177e4SLinus Torvalds 	int sz_idx, kind;
4801da177e4SLinus Torvalds 	int min_len;
4811da177e4SLinus Torvalds 	int family;
4821da177e4SLinus Torvalds 	int type;
4831da177e4SLinus Torvalds 	int err;
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds 	/* Only requests are handled by kernel now */
4861da177e4SLinus Torvalds 	if (!(nlh->nlmsg_flags&NLM_F_REQUEST))
4871da177e4SLinus Torvalds 		return 0;
4881da177e4SLinus Torvalds 
4891da177e4SLinus Torvalds 	type = nlh->nlmsg_type;
4901da177e4SLinus Torvalds 
4911da177e4SLinus Torvalds 	/* A control message: ignore them */
4921da177e4SLinus Torvalds 	if (type < RTM_BASE)
4931da177e4SLinus Torvalds 		return 0;
4941da177e4SLinus Torvalds 
4951da177e4SLinus Torvalds 	/* Unknown message: reply with EINVAL */
4961da177e4SLinus Torvalds 	if (type > RTM_MAX)
4971da177e4SLinus Torvalds 		goto err_inval;
4981da177e4SLinus Torvalds 
4991da177e4SLinus Torvalds 	type -= RTM_BASE;
5001da177e4SLinus Torvalds 
5011da177e4SLinus Torvalds 	/* All the messages must have at least 1 byte length */
5021da177e4SLinus Torvalds 	if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg)))
5031da177e4SLinus Torvalds 		return 0;
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds 	family = ((struct rtgenmsg*)NLMSG_DATA(nlh))->rtgen_family;
5061da177e4SLinus Torvalds 	if (family >= NPROTO) {
5071da177e4SLinus Torvalds 		*errp = -EAFNOSUPPORT;
5081da177e4SLinus Torvalds 		return -1;
5091da177e4SLinus Torvalds 	}
5101da177e4SLinus Torvalds 
5111da177e4SLinus Torvalds 	link_tab = rtnetlink_links[family];
5121da177e4SLinus Torvalds 	if (link_tab == NULL)
5131da177e4SLinus Torvalds 		link_tab = rtnetlink_links[PF_UNSPEC];
5141da177e4SLinus Torvalds 	link = &link_tab[type];
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds 	sz_idx = type>>2;
5171da177e4SLinus Torvalds 	kind = type&3;
5181da177e4SLinus Torvalds 
5191da177e4SLinus Torvalds 	if (kind != 2 && security_netlink_recv(skb)) {
5201da177e4SLinus Torvalds 		*errp = -EPERM;
5211da177e4SLinus Torvalds 		return -1;
5221da177e4SLinus Torvalds 	}
5231da177e4SLinus Torvalds 
5241da177e4SLinus Torvalds 	if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
5251da177e4SLinus Torvalds 		u32 rlen;
5261da177e4SLinus Torvalds 
5271da177e4SLinus Torvalds 		if (link->dumpit == NULL)
5281da177e4SLinus Torvalds 			link = &(rtnetlink_links[PF_UNSPEC][type]);
5291da177e4SLinus Torvalds 
5301da177e4SLinus Torvalds 		if (link->dumpit == NULL)
5311da177e4SLinus Torvalds 			goto err_inval;
5321da177e4SLinus Torvalds 
5331da177e4SLinus Torvalds 		if ((*errp = netlink_dump_start(rtnl, skb, nlh,
5341da177e4SLinus Torvalds 						link->dumpit,
5351da177e4SLinus Torvalds 						rtnetlink_done)) != 0) {
5361da177e4SLinus Torvalds 			return -1;
5371da177e4SLinus Torvalds 		}
5381da177e4SLinus Torvalds 		rlen = NLMSG_ALIGN(nlh->nlmsg_len);
5391da177e4SLinus Torvalds 		if (rlen > skb->len)
5401da177e4SLinus Torvalds 			rlen = skb->len;
5411da177e4SLinus Torvalds 		skb_pull(skb, rlen);
5421da177e4SLinus Torvalds 		return -1;
5431da177e4SLinus Torvalds 	}
5441da177e4SLinus Torvalds 
5451da177e4SLinus Torvalds 	memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *)));
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds 	min_len = rtm_min[sz_idx];
5481da177e4SLinus Torvalds 	if (nlh->nlmsg_len < min_len)
5491da177e4SLinus Torvalds 		goto err_inval;
5501da177e4SLinus Torvalds 
5511da177e4SLinus Torvalds 	if (nlh->nlmsg_len > min_len) {
5521da177e4SLinus Torvalds 		int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
5531da177e4SLinus Torvalds 		struct rtattr *attr = (void*)nlh + NLMSG_ALIGN(min_len);
5541da177e4SLinus Torvalds 
5551da177e4SLinus Torvalds 		while (RTA_OK(attr, attrlen)) {
5561da177e4SLinus Torvalds 			unsigned flavor = attr->rta_type;
5571da177e4SLinus Torvalds 			if (flavor) {
5581da177e4SLinus Torvalds 				if (flavor > rta_max[sz_idx])
5591da177e4SLinus Torvalds 					goto err_inval;
5601da177e4SLinus Torvalds 				rta_buf[flavor-1] = attr;
5611da177e4SLinus Torvalds 			}
5621da177e4SLinus Torvalds 			attr = RTA_NEXT(attr, attrlen);
5631da177e4SLinus Torvalds 		}
5641da177e4SLinus Torvalds 	}
5651da177e4SLinus Torvalds 
5661da177e4SLinus Torvalds 	if (link->doit == NULL)
5671da177e4SLinus Torvalds 		link = &(rtnetlink_links[PF_UNSPEC][type]);
5681da177e4SLinus Torvalds 	if (link->doit == NULL)
5691da177e4SLinus Torvalds 		goto err_inval;
5701da177e4SLinus Torvalds 	err = link->doit(skb, nlh, (void *)&rta_buf[0]);
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds 	*errp = err;
5731da177e4SLinus Torvalds 	return err;
5741da177e4SLinus Torvalds 
5751da177e4SLinus Torvalds err_inval:
5761da177e4SLinus Torvalds 	*errp = -EINVAL;
5771da177e4SLinus Torvalds 	return -1;
5781da177e4SLinus Torvalds }
5791da177e4SLinus Torvalds 
5801da177e4SLinus Torvalds /*
5811da177e4SLinus Torvalds  * Process one packet of messages.
5821da177e4SLinus Torvalds  * Malformed skbs with wrong lengths of messages are discarded silently.
5831da177e4SLinus Torvalds  */
5841da177e4SLinus Torvalds 
5851da177e4SLinus Torvalds static inline int rtnetlink_rcv_skb(struct sk_buff *skb)
5861da177e4SLinus Torvalds {
5871da177e4SLinus Torvalds 	int err;
5881da177e4SLinus Torvalds 	struct nlmsghdr * nlh;
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds 	while (skb->len >= NLMSG_SPACE(0)) {
5911da177e4SLinus Torvalds 		u32 rlen;
5921da177e4SLinus Torvalds 
5931da177e4SLinus Torvalds 		nlh = (struct nlmsghdr *)skb->data;
5941da177e4SLinus Torvalds 		if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
5951da177e4SLinus Torvalds 			return 0;
5961da177e4SLinus Torvalds 		rlen = NLMSG_ALIGN(nlh->nlmsg_len);
5971da177e4SLinus Torvalds 		if (rlen > skb->len)
5981da177e4SLinus Torvalds 			rlen = skb->len;
5991da177e4SLinus Torvalds 		if (rtnetlink_rcv_msg(skb, nlh, &err)) {
6001da177e4SLinus Torvalds 			/* Not error, but we must interrupt processing here:
6011da177e4SLinus Torvalds 			 *   Note, that in this case we do not pull message
6021da177e4SLinus Torvalds 			 *   from skb, it will be processed later.
6031da177e4SLinus Torvalds 			 */
6041da177e4SLinus Torvalds 			if (err == 0)
6051da177e4SLinus Torvalds 				return -1;
6061da177e4SLinus Torvalds 			netlink_ack(skb, nlh, err);
6071da177e4SLinus Torvalds 		} else if (nlh->nlmsg_flags&NLM_F_ACK)
6081da177e4SLinus Torvalds 			netlink_ack(skb, nlh, 0);
6091da177e4SLinus Torvalds 		skb_pull(skb, rlen);
6101da177e4SLinus Torvalds 	}
6111da177e4SLinus Torvalds 
6121da177e4SLinus Torvalds 	return 0;
6131da177e4SLinus Torvalds }
6141da177e4SLinus Torvalds 
6151da177e4SLinus Torvalds /*
6161da177e4SLinus Torvalds  *  rtnetlink input queue processing routine:
6172a0a6ebeSHerbert Xu  *	- process as much as there was in the queue upon entry.
6181da177e4SLinus Torvalds  *	- feed skbs to rtnetlink_rcv_skb, until it refuse a message,
6192a0a6ebeSHerbert Xu  *	  that will occur, when a dump started.
6201da177e4SLinus Torvalds  */
6211da177e4SLinus Torvalds 
6221da177e4SLinus Torvalds static void rtnetlink_rcv(struct sock *sk, int len)
6231da177e4SLinus Torvalds {
6242a0a6ebeSHerbert Xu 	unsigned int qlen = skb_queue_len(&sk->sk_receive_queue);
6252a0a6ebeSHerbert Xu 
6261da177e4SLinus Torvalds 	do {
6271da177e4SLinus Torvalds 		struct sk_buff *skb;
6281da177e4SLinus Torvalds 
6292a0a6ebeSHerbert Xu 		rtnl_lock();
6301da177e4SLinus Torvalds 
6312a0a6ebeSHerbert Xu 		if (qlen > skb_queue_len(&sk->sk_receive_queue))
6322a0a6ebeSHerbert Xu 			qlen = skb_queue_len(&sk->sk_receive_queue);
6332a0a6ebeSHerbert Xu 
63409e14305SDavid S. Miller 		for (; qlen; qlen--) {
6352a0a6ebeSHerbert Xu 			skb = skb_dequeue(&sk->sk_receive_queue);
6361da177e4SLinus Torvalds 			if (rtnetlink_rcv_skb(skb)) {
63709e14305SDavid S. Miller 				if (skb->len)
6381da177e4SLinus Torvalds 					skb_queue_head(&sk->sk_receive_queue,
6391da177e4SLinus Torvalds 						       skb);
6400f4821e7SDavid S. Miller 				else {
6411da177e4SLinus Torvalds 					kfree_skb(skb);
6420f4821e7SDavid S. Miller 					qlen--;
6430f4821e7SDavid S. Miller 				}
6441da177e4SLinus Torvalds 				break;
6451da177e4SLinus Torvalds 			}
6461da177e4SLinus Torvalds 			kfree_skb(skb);
6471da177e4SLinus Torvalds 		}
6481da177e4SLinus Torvalds 
6491da177e4SLinus Torvalds 		up(&rtnl_sem);
6501da177e4SLinus Torvalds 
6511da177e4SLinus Torvalds 		netdev_run_todo();
6522a0a6ebeSHerbert Xu 	} while (qlen);
6531da177e4SLinus Torvalds }
6541da177e4SLinus Torvalds 
655db46edc6SThomas Graf static struct rtnetlink_link link_rtnetlink_table[RTM_NR_MSGTYPES] =
6561da177e4SLinus Torvalds {
6571da177e4SLinus Torvalds 	[RTM_GETLINK     - RTM_BASE] = { .dumpit = rtnetlink_dump_ifinfo },
6581da177e4SLinus Torvalds 	[RTM_SETLINK     - RTM_BASE] = { .doit   = do_setlink		 },
6591da177e4SLinus Torvalds 	[RTM_GETADDR     - RTM_BASE] = { .dumpit = rtnetlink_dump_all	 },
6601da177e4SLinus Torvalds 	[RTM_GETROUTE    - RTM_BASE] = { .dumpit = rtnetlink_dump_all	 },
6611da177e4SLinus Torvalds 	[RTM_NEWNEIGH    - RTM_BASE] = { .doit   = neigh_add		 },
6621da177e4SLinus Torvalds 	[RTM_DELNEIGH    - RTM_BASE] = { .doit   = neigh_delete		 },
663240eed95SThomas Graf 	[RTM_GETNEIGH    - RTM_BASE] = { .dumpit = neigh_dump_info	 },
66498f245e7SDavid S. Miller 	[RTM_GETRULE     - RTM_BASE] = { .dumpit = rtnetlink_dump_all	 },
665c7fb64dbSThomas Graf 	[RTM_GETNEIGHTBL - RTM_BASE] = { .dumpit = neightbl_dump_info	 },
666c7fb64dbSThomas Graf 	[RTM_SETNEIGHTBL - RTM_BASE] = { .doit   = neightbl_set		 },
6671da177e4SLinus Torvalds };
6681da177e4SLinus Torvalds 
6691da177e4SLinus Torvalds static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
6701da177e4SLinus Torvalds {
6711da177e4SLinus Torvalds 	struct net_device *dev = ptr;
6721da177e4SLinus Torvalds 	switch (event) {
6731da177e4SLinus Torvalds 	case NETDEV_UNREGISTER:
6741da177e4SLinus Torvalds 		rtmsg_ifinfo(RTM_DELLINK, dev, ~0U);
6751da177e4SLinus Torvalds 		break;
6761da177e4SLinus Torvalds 	case NETDEV_REGISTER:
6771da177e4SLinus Torvalds 		rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
6781da177e4SLinus Torvalds 		break;
6791da177e4SLinus Torvalds 	case NETDEV_UP:
6801da177e4SLinus Torvalds 	case NETDEV_DOWN:
6811da177e4SLinus Torvalds 		rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
6821da177e4SLinus Torvalds 		break;
6831da177e4SLinus Torvalds 	case NETDEV_CHANGE:
6841da177e4SLinus Torvalds 	case NETDEV_GOING_DOWN:
6851da177e4SLinus Torvalds 		break;
6861da177e4SLinus Torvalds 	default:
6871da177e4SLinus Torvalds 		rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
6881da177e4SLinus Torvalds 		break;
6891da177e4SLinus Torvalds 	}
6901da177e4SLinus Torvalds 	return NOTIFY_DONE;
6911da177e4SLinus Torvalds }
6921da177e4SLinus Torvalds 
6931da177e4SLinus Torvalds static struct notifier_block rtnetlink_dev_notifier = {
6941da177e4SLinus Torvalds 	.notifier_call	= rtnetlink_event,
6951da177e4SLinus Torvalds };
6961da177e4SLinus Torvalds 
6971da177e4SLinus Torvalds void __init rtnetlink_init(void)
6981da177e4SLinus Torvalds {
6991da177e4SLinus Torvalds 	int i;
7001da177e4SLinus Torvalds 
7011da177e4SLinus Torvalds 	rtattr_max = 0;
7021da177e4SLinus Torvalds 	for (i = 0; i < ARRAY_SIZE(rta_max); i++)
7031da177e4SLinus Torvalds 		if (rta_max[i] > rtattr_max)
7041da177e4SLinus Torvalds 			rtattr_max = rta_max[i];
7051da177e4SLinus Torvalds 	rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL);
7061da177e4SLinus Torvalds 	if (!rta_buf)
7071da177e4SLinus Torvalds 		panic("rtnetlink_init: cannot allocate rta_buf\n");
7081da177e4SLinus Torvalds 
7091da177e4SLinus Torvalds 	rtnl = netlink_kernel_create(NETLINK_ROUTE, rtnetlink_rcv);
7101da177e4SLinus Torvalds 	if (rtnl == NULL)
7111da177e4SLinus Torvalds 		panic("rtnetlink_init: cannot initialize rtnetlink\n");
7121da177e4SLinus Torvalds 	netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV);
7131da177e4SLinus Torvalds 	register_netdevice_notifier(&rtnetlink_dev_notifier);
7141da177e4SLinus Torvalds 	rtnetlink_links[PF_UNSPEC] = link_rtnetlink_table;
7151da177e4SLinus Torvalds 	rtnetlink_links[PF_PACKET] = link_rtnetlink_table;
7161da177e4SLinus Torvalds }
7171da177e4SLinus Torvalds 
7181da177e4SLinus Torvalds EXPORT_SYMBOL(__rta_fill);
7191da177e4SLinus Torvalds EXPORT_SYMBOL(rtattr_strlcpy);
7201da177e4SLinus Torvalds EXPORT_SYMBOL(rtattr_parse);
7211da177e4SLinus Torvalds EXPORT_SYMBOL(rtnetlink_links);
7221da177e4SLinus Torvalds EXPORT_SYMBOL(rtnetlink_put_metrics);
7231da177e4SLinus Torvalds EXPORT_SYMBOL(rtnl);
7241da177e4SLinus Torvalds EXPORT_SYMBOL(rtnl_lock);
7251da177e4SLinus Torvalds EXPORT_SYMBOL(rtnl_lock_interruptible);
7261da177e4SLinus Torvalds EXPORT_SYMBOL(rtnl_sem);
7271da177e4SLinus Torvalds EXPORT_SYMBOL(rtnl_unlock);
728