xref: /linux/net/ipv4/devinet.c (revision 02c30a84e6298b6b20a56f0896ac80b47839e134)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *	NET3	IP device support routines.
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *	Version: $Id: devinet.c,v 1.44 2001/10/31 21:55:54 davem Exp $
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *		This program is free software; you can redistribute it and/or
71da177e4SLinus Torvalds  *		modify it under the terms of the GNU General Public License
81da177e4SLinus Torvalds  *		as published by the Free Software Foundation; either version
91da177e4SLinus Torvalds  *		2 of the License, or (at your option) any later version.
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  *	Derived from the IP parts of dev.c 1.0.19
12*02c30a84SJesper Juhl  * 		Authors:	Ross Biro
131da177e4SLinus Torvalds  *				Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
141da177e4SLinus Torvalds  *				Mark Evans, <evansmp@uhura.aston.ac.uk>
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  *	Additional Authors:
171da177e4SLinus Torvalds  *		Alan Cox, <gw4pts@gw4pts.ampr.org>
181da177e4SLinus Torvalds  *		Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
191da177e4SLinus Torvalds  *
201da177e4SLinus Torvalds  *	Changes:
211da177e4SLinus Torvalds  *		Alexey Kuznetsov:	pa_* fields are replaced with ifaddr
221da177e4SLinus Torvalds  *					lists.
231da177e4SLinus Torvalds  *		Cyrus Durgin:		updated for kmod
241da177e4SLinus Torvalds  *		Matthias Andree:	in devinet_ioctl, compare label and
251da177e4SLinus Torvalds  *					address (4.4BSD alias style support),
261da177e4SLinus Torvalds  *					fall back to comparing just the label
271da177e4SLinus Torvalds  *					if no match found.
281da177e4SLinus Torvalds  */
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds #include <linux/config.h>
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds #include <asm/uaccess.h>
331da177e4SLinus Torvalds #include <asm/system.h>
341da177e4SLinus Torvalds #include <linux/bitops.h>
351da177e4SLinus Torvalds #include <linux/module.h>
361da177e4SLinus Torvalds #include <linux/types.h>
371da177e4SLinus Torvalds #include <linux/kernel.h>
381da177e4SLinus Torvalds #include <linux/sched.h>
391da177e4SLinus Torvalds #include <linux/string.h>
401da177e4SLinus Torvalds #include <linux/mm.h>
411da177e4SLinus Torvalds #include <linux/socket.h>
421da177e4SLinus Torvalds #include <linux/sockios.h>
431da177e4SLinus Torvalds #include <linux/in.h>
441da177e4SLinus Torvalds #include <linux/errno.h>
451da177e4SLinus Torvalds #include <linux/interrupt.h>
461da177e4SLinus Torvalds #include <linux/if_ether.h>
471da177e4SLinus Torvalds #include <linux/inet.h>
481da177e4SLinus Torvalds #include <linux/netdevice.h>
491da177e4SLinus Torvalds #include <linux/etherdevice.h>
501da177e4SLinus Torvalds #include <linux/skbuff.h>
511da177e4SLinus Torvalds #include <linux/rtnetlink.h>
521da177e4SLinus Torvalds #include <linux/init.h>
531da177e4SLinus Torvalds #include <linux/notifier.h>
541da177e4SLinus Torvalds #include <linux/inetdevice.h>
551da177e4SLinus Torvalds #include <linux/igmp.h>
561da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
571da177e4SLinus Torvalds #include <linux/sysctl.h>
581da177e4SLinus Torvalds #endif
591da177e4SLinus Torvalds #include <linux/kmod.h>
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds #include <net/ip.h>
621da177e4SLinus Torvalds #include <net/route.h>
631da177e4SLinus Torvalds #include <net/ip_fib.h>
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds struct ipv4_devconf ipv4_devconf = {
661da177e4SLinus Torvalds 	.accept_redirects = 1,
671da177e4SLinus Torvalds 	.send_redirects =  1,
681da177e4SLinus Torvalds 	.secure_redirects = 1,
691da177e4SLinus Torvalds 	.shared_media =	  1,
701da177e4SLinus Torvalds };
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds static struct ipv4_devconf ipv4_devconf_dflt = {
731da177e4SLinus Torvalds 	.accept_redirects =  1,
741da177e4SLinus Torvalds 	.send_redirects =    1,
751da177e4SLinus Torvalds 	.secure_redirects =  1,
761da177e4SLinus Torvalds 	.shared_media =	     1,
771da177e4SLinus Torvalds 	.accept_source_route = 1,
781da177e4SLinus Torvalds };
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds static void rtmsg_ifa(int event, struct in_ifaddr *);
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds static struct notifier_block *inetaddr_chain;
831da177e4SLinus Torvalds static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
841da177e4SLinus Torvalds 			 int destroy);
851da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
861da177e4SLinus Torvalds static void devinet_sysctl_register(struct in_device *in_dev,
871da177e4SLinus Torvalds 				    struct ipv4_devconf *p);
881da177e4SLinus Torvalds static void devinet_sysctl_unregister(struct ipv4_devconf *p);
891da177e4SLinus Torvalds #endif
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds /* Locks all the inet devices. */
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds static struct in_ifaddr *inet_alloc_ifa(void)
941da177e4SLinus Torvalds {
951da177e4SLinus Torvalds 	struct in_ifaddr *ifa = kmalloc(sizeof(*ifa), GFP_KERNEL);
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds 	if (ifa) {
981da177e4SLinus Torvalds 		memset(ifa, 0, sizeof(*ifa));
991da177e4SLinus Torvalds 		INIT_RCU_HEAD(&ifa->rcu_head);
1001da177e4SLinus Torvalds 	}
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds 	return ifa;
1031da177e4SLinus Torvalds }
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds static void inet_rcu_free_ifa(struct rcu_head *head)
1061da177e4SLinus Torvalds {
1071da177e4SLinus Torvalds 	struct in_ifaddr *ifa = container_of(head, struct in_ifaddr, rcu_head);
1081da177e4SLinus Torvalds 	if (ifa->ifa_dev)
1091da177e4SLinus Torvalds 		in_dev_put(ifa->ifa_dev);
1101da177e4SLinus Torvalds 	kfree(ifa);
1111da177e4SLinus Torvalds }
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds static inline void inet_free_ifa(struct in_ifaddr *ifa)
1141da177e4SLinus Torvalds {
1151da177e4SLinus Torvalds 	call_rcu(&ifa->rcu_head, inet_rcu_free_ifa);
1161da177e4SLinus Torvalds }
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds void in_dev_finish_destroy(struct in_device *idev)
1191da177e4SLinus Torvalds {
1201da177e4SLinus Torvalds 	struct net_device *dev = idev->dev;
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds 	BUG_TRAP(!idev->ifa_list);
1231da177e4SLinus Torvalds 	BUG_TRAP(!idev->mc_list);
1241da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG
1251da177e4SLinus Torvalds 	printk(KERN_DEBUG "in_dev_finish_destroy: %p=%s\n",
1261da177e4SLinus Torvalds 	       idev, dev ? dev->name : "NIL");
1271da177e4SLinus Torvalds #endif
1281da177e4SLinus Torvalds 	dev_put(dev);
1291da177e4SLinus Torvalds 	if (!idev->dead)
1301da177e4SLinus Torvalds 		printk("Freeing alive in_device %p\n", idev);
1311da177e4SLinus Torvalds 	else {
1321da177e4SLinus Torvalds 		kfree(idev);
1331da177e4SLinus Torvalds 	}
1341da177e4SLinus Torvalds }
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds struct in_device *inetdev_init(struct net_device *dev)
1371da177e4SLinus Torvalds {
1381da177e4SLinus Torvalds 	struct in_device *in_dev;
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds 	ASSERT_RTNL();
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds 	in_dev = kmalloc(sizeof(*in_dev), GFP_KERNEL);
1431da177e4SLinus Torvalds 	if (!in_dev)
1441da177e4SLinus Torvalds 		goto out;
1451da177e4SLinus Torvalds 	memset(in_dev, 0, sizeof(*in_dev));
1461da177e4SLinus Torvalds 	INIT_RCU_HEAD(&in_dev->rcu_head);
1471da177e4SLinus Torvalds 	memcpy(&in_dev->cnf, &ipv4_devconf_dflt, sizeof(in_dev->cnf));
1481da177e4SLinus Torvalds 	in_dev->cnf.sysctl = NULL;
1491da177e4SLinus Torvalds 	in_dev->dev = dev;
1501da177e4SLinus Torvalds 	if ((in_dev->arp_parms = neigh_parms_alloc(dev, &arp_tbl)) == NULL)
1511da177e4SLinus Torvalds 		goto out_kfree;
1521da177e4SLinus Torvalds 	/* Reference in_dev->dev */
1531da177e4SLinus Torvalds 	dev_hold(dev);
1541da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
1551da177e4SLinus Torvalds 	neigh_sysctl_register(dev, in_dev->arp_parms, NET_IPV4,
1561da177e4SLinus Torvalds 			      NET_IPV4_NEIGH, "ipv4", NULL, NULL);
1571da177e4SLinus Torvalds #endif
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds 	/* Account for reference dev->ip_ptr */
1601da177e4SLinus Torvalds 	in_dev_hold(in_dev);
1611da177e4SLinus Torvalds 	rcu_assign_pointer(dev->ip_ptr, in_dev);
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
1641da177e4SLinus Torvalds 	devinet_sysctl_register(in_dev, &in_dev->cnf);
1651da177e4SLinus Torvalds #endif
1661da177e4SLinus Torvalds 	ip_mc_init_dev(in_dev);
1671da177e4SLinus Torvalds 	if (dev->flags & IFF_UP)
1681da177e4SLinus Torvalds 		ip_mc_up(in_dev);
1691da177e4SLinus Torvalds out:
1701da177e4SLinus Torvalds 	return in_dev;
1711da177e4SLinus Torvalds out_kfree:
1721da177e4SLinus Torvalds 	kfree(in_dev);
1731da177e4SLinus Torvalds 	in_dev = NULL;
1741da177e4SLinus Torvalds 	goto out;
1751da177e4SLinus Torvalds }
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds static void in_dev_rcu_put(struct rcu_head *head)
1781da177e4SLinus Torvalds {
1791da177e4SLinus Torvalds 	struct in_device *idev = container_of(head, struct in_device, rcu_head);
1801da177e4SLinus Torvalds 	in_dev_put(idev);
1811da177e4SLinus Torvalds }
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds static void inetdev_destroy(struct in_device *in_dev)
1841da177e4SLinus Torvalds {
1851da177e4SLinus Torvalds 	struct in_ifaddr *ifa;
1861da177e4SLinus Torvalds 	struct net_device *dev;
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds 	ASSERT_RTNL();
1891da177e4SLinus Torvalds 
1901da177e4SLinus Torvalds 	dev = in_dev->dev;
1911da177e4SLinus Torvalds 	if (dev == &loopback_dev)
1921da177e4SLinus Torvalds 		return;
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds 	in_dev->dead = 1;
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds 	ip_mc_destroy_dev(in_dev);
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds 	while ((ifa = in_dev->ifa_list) != NULL) {
1991da177e4SLinus Torvalds 		inet_del_ifa(in_dev, &in_dev->ifa_list, 0);
2001da177e4SLinus Torvalds 		inet_free_ifa(ifa);
2011da177e4SLinus Torvalds 	}
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
2041da177e4SLinus Torvalds 	devinet_sysctl_unregister(&in_dev->cnf);
2051da177e4SLinus Torvalds #endif
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds 	dev->ip_ptr = NULL;
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
2101da177e4SLinus Torvalds 	neigh_sysctl_unregister(in_dev->arp_parms);
2111da177e4SLinus Torvalds #endif
2121da177e4SLinus Torvalds 	neigh_parms_release(&arp_tbl, in_dev->arp_parms);
2131da177e4SLinus Torvalds 	arp_ifdown(dev);
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds 	call_rcu(&in_dev->rcu_head, in_dev_rcu_put);
2161da177e4SLinus Torvalds }
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds int inet_addr_onlink(struct in_device *in_dev, u32 a, u32 b)
2191da177e4SLinus Torvalds {
2201da177e4SLinus Torvalds 	rcu_read_lock();
2211da177e4SLinus Torvalds 	for_primary_ifa(in_dev) {
2221da177e4SLinus Torvalds 		if (inet_ifa_match(a, ifa)) {
2231da177e4SLinus Torvalds 			if (!b || inet_ifa_match(b, ifa)) {
2241da177e4SLinus Torvalds 				rcu_read_unlock();
2251da177e4SLinus Torvalds 				return 1;
2261da177e4SLinus Torvalds 			}
2271da177e4SLinus Torvalds 		}
2281da177e4SLinus Torvalds 	} endfor_ifa(in_dev);
2291da177e4SLinus Torvalds 	rcu_read_unlock();
2301da177e4SLinus Torvalds 	return 0;
2311da177e4SLinus Torvalds }
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
2341da177e4SLinus Torvalds 			 int destroy)
2351da177e4SLinus Torvalds {
2361da177e4SLinus Torvalds 	struct in_ifaddr *ifa1 = *ifap;
2371da177e4SLinus Torvalds 
2381da177e4SLinus Torvalds 	ASSERT_RTNL();
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds 	/* 1. Deleting primary ifaddr forces deletion all secondaries */
2411da177e4SLinus Torvalds 
2421da177e4SLinus Torvalds 	if (!(ifa1->ifa_flags & IFA_F_SECONDARY)) {
2431da177e4SLinus Torvalds 		struct in_ifaddr *ifa;
2441da177e4SLinus Torvalds 		struct in_ifaddr **ifap1 = &ifa1->ifa_next;
2451da177e4SLinus Torvalds 
2461da177e4SLinus Torvalds 		while ((ifa = *ifap1) != NULL) {
2471da177e4SLinus Torvalds 			if (!(ifa->ifa_flags & IFA_F_SECONDARY) ||
2481da177e4SLinus Torvalds 			    ifa1->ifa_mask != ifa->ifa_mask ||
2491da177e4SLinus Torvalds 			    !inet_ifa_match(ifa1->ifa_address, ifa)) {
2501da177e4SLinus Torvalds 				ifap1 = &ifa->ifa_next;
2511da177e4SLinus Torvalds 				continue;
2521da177e4SLinus Torvalds 			}
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds 			*ifap1 = ifa->ifa_next;
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds 			rtmsg_ifa(RTM_DELADDR, ifa);
2571da177e4SLinus Torvalds 			notifier_call_chain(&inetaddr_chain, NETDEV_DOWN, ifa);
2581da177e4SLinus Torvalds 			inet_free_ifa(ifa);
2591da177e4SLinus Torvalds 		}
2601da177e4SLinus Torvalds 	}
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds 	/* 2. Unlink it */
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds 	*ifap = ifa1->ifa_next;
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds 	/* 3. Announce address deletion */
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds 	/* Send message first, then call notifier.
2691da177e4SLinus Torvalds 	   At first sight, FIB update triggered by notifier
2701da177e4SLinus Torvalds 	   will refer to already deleted ifaddr, that could confuse
2711da177e4SLinus Torvalds 	   netlink listeners. It is not true: look, gated sees
2721da177e4SLinus Torvalds 	   that route deleted and if it still thinks that ifaddr
2731da177e4SLinus Torvalds 	   is valid, it will try to restore deleted routes... Grr.
2741da177e4SLinus Torvalds 	   So that, this order is correct.
2751da177e4SLinus Torvalds 	 */
2761da177e4SLinus Torvalds 	rtmsg_ifa(RTM_DELADDR, ifa1);
2771da177e4SLinus Torvalds 	notifier_call_chain(&inetaddr_chain, NETDEV_DOWN, ifa1);
2781da177e4SLinus Torvalds 	if (destroy) {
2791da177e4SLinus Torvalds 		inet_free_ifa(ifa1);
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds 		if (!in_dev->ifa_list)
2821da177e4SLinus Torvalds 			inetdev_destroy(in_dev);
2831da177e4SLinus Torvalds 	}
2841da177e4SLinus Torvalds }
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds static int inet_insert_ifa(struct in_ifaddr *ifa)
2871da177e4SLinus Torvalds {
2881da177e4SLinus Torvalds 	struct in_device *in_dev = ifa->ifa_dev;
2891da177e4SLinus Torvalds 	struct in_ifaddr *ifa1, **ifap, **last_primary;
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds 	ASSERT_RTNL();
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds 	if (!ifa->ifa_local) {
2941da177e4SLinus Torvalds 		inet_free_ifa(ifa);
2951da177e4SLinus Torvalds 		return 0;
2961da177e4SLinus Torvalds 	}
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds 	ifa->ifa_flags &= ~IFA_F_SECONDARY;
2991da177e4SLinus Torvalds 	last_primary = &in_dev->ifa_list;
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds 	for (ifap = &in_dev->ifa_list; (ifa1 = *ifap) != NULL;
3021da177e4SLinus Torvalds 	     ifap = &ifa1->ifa_next) {
3031da177e4SLinus Torvalds 		if (!(ifa1->ifa_flags & IFA_F_SECONDARY) &&
3041da177e4SLinus Torvalds 		    ifa->ifa_scope <= ifa1->ifa_scope)
3051da177e4SLinus Torvalds 			last_primary = &ifa1->ifa_next;
3061da177e4SLinus Torvalds 		if (ifa1->ifa_mask == ifa->ifa_mask &&
3071da177e4SLinus Torvalds 		    inet_ifa_match(ifa1->ifa_address, ifa)) {
3081da177e4SLinus Torvalds 			if (ifa1->ifa_local == ifa->ifa_local) {
3091da177e4SLinus Torvalds 				inet_free_ifa(ifa);
3101da177e4SLinus Torvalds 				return -EEXIST;
3111da177e4SLinus Torvalds 			}
3121da177e4SLinus Torvalds 			if (ifa1->ifa_scope != ifa->ifa_scope) {
3131da177e4SLinus Torvalds 				inet_free_ifa(ifa);
3141da177e4SLinus Torvalds 				return -EINVAL;
3151da177e4SLinus Torvalds 			}
3161da177e4SLinus Torvalds 			ifa->ifa_flags |= IFA_F_SECONDARY;
3171da177e4SLinus Torvalds 		}
3181da177e4SLinus Torvalds 	}
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds 	if (!(ifa->ifa_flags & IFA_F_SECONDARY)) {
3211da177e4SLinus Torvalds 		net_srandom(ifa->ifa_local);
3221da177e4SLinus Torvalds 		ifap = last_primary;
3231da177e4SLinus Torvalds 	}
3241da177e4SLinus Torvalds 
3251da177e4SLinus Torvalds 	ifa->ifa_next = *ifap;
3261da177e4SLinus Torvalds 	*ifap = ifa;
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds 	/* Send message first, then call notifier.
3291da177e4SLinus Torvalds 	   Notifier will trigger FIB update, so that
3301da177e4SLinus Torvalds 	   listeners of netlink will know about new ifaddr */
3311da177e4SLinus Torvalds 	rtmsg_ifa(RTM_NEWADDR, ifa);
3321da177e4SLinus Torvalds 	notifier_call_chain(&inetaddr_chain, NETDEV_UP, ifa);
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds 	return 0;
3351da177e4SLinus Torvalds }
3361da177e4SLinus Torvalds 
3371da177e4SLinus Torvalds static int inet_set_ifa(struct net_device *dev, struct in_ifaddr *ifa)
3381da177e4SLinus Torvalds {
3391da177e4SLinus Torvalds 	struct in_device *in_dev = __in_dev_get(dev);
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds 	ASSERT_RTNL();
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds 	if (!in_dev) {
3441da177e4SLinus Torvalds 		in_dev = inetdev_init(dev);
3451da177e4SLinus Torvalds 		if (!in_dev) {
3461da177e4SLinus Torvalds 			inet_free_ifa(ifa);
3471da177e4SLinus Torvalds 			return -ENOBUFS;
3481da177e4SLinus Torvalds 		}
3491da177e4SLinus Torvalds 	}
3501da177e4SLinus Torvalds 	if (ifa->ifa_dev != in_dev) {
3511da177e4SLinus Torvalds 		BUG_TRAP(!ifa->ifa_dev);
3521da177e4SLinus Torvalds 		in_dev_hold(in_dev);
3531da177e4SLinus Torvalds 		ifa->ifa_dev = in_dev;
3541da177e4SLinus Torvalds 	}
3551da177e4SLinus Torvalds 	if (LOOPBACK(ifa->ifa_local))
3561da177e4SLinus Torvalds 		ifa->ifa_scope = RT_SCOPE_HOST;
3571da177e4SLinus Torvalds 	return inet_insert_ifa(ifa);
3581da177e4SLinus Torvalds }
3591da177e4SLinus Torvalds 
3601da177e4SLinus Torvalds struct in_device *inetdev_by_index(int ifindex)
3611da177e4SLinus Torvalds {
3621da177e4SLinus Torvalds 	struct net_device *dev;
3631da177e4SLinus Torvalds 	struct in_device *in_dev = NULL;
3641da177e4SLinus Torvalds 	read_lock(&dev_base_lock);
3651da177e4SLinus Torvalds 	dev = __dev_get_by_index(ifindex);
3661da177e4SLinus Torvalds 	if (dev)
3671da177e4SLinus Torvalds 		in_dev = in_dev_get(dev);
3681da177e4SLinus Torvalds 	read_unlock(&dev_base_lock);
3691da177e4SLinus Torvalds 	return in_dev;
3701da177e4SLinus Torvalds }
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds /* Called only from RTNL semaphored context. No locks. */
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds struct in_ifaddr *inet_ifa_byprefix(struct in_device *in_dev, u32 prefix,
3751da177e4SLinus Torvalds 				    u32 mask)
3761da177e4SLinus Torvalds {
3771da177e4SLinus Torvalds 	ASSERT_RTNL();
3781da177e4SLinus Torvalds 
3791da177e4SLinus Torvalds 	for_primary_ifa(in_dev) {
3801da177e4SLinus Torvalds 		if (ifa->ifa_mask == mask && inet_ifa_match(prefix, ifa))
3811da177e4SLinus Torvalds 			return ifa;
3821da177e4SLinus Torvalds 	} endfor_ifa(in_dev);
3831da177e4SLinus Torvalds 	return NULL;
3841da177e4SLinus Torvalds }
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds static int inet_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
3871da177e4SLinus Torvalds {
3881da177e4SLinus Torvalds 	struct rtattr **rta = arg;
3891da177e4SLinus Torvalds 	struct in_device *in_dev;
3901da177e4SLinus Torvalds 	struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
3911da177e4SLinus Torvalds 	struct in_ifaddr *ifa, **ifap;
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds 	ASSERT_RTNL();
3941da177e4SLinus Torvalds 
3951da177e4SLinus Torvalds 	if ((in_dev = inetdev_by_index(ifm->ifa_index)) == NULL)
3961da177e4SLinus Torvalds 		goto out;
3971da177e4SLinus Torvalds 	__in_dev_put(in_dev);
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds 	for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
4001da177e4SLinus Torvalds 	     ifap = &ifa->ifa_next) {
4011da177e4SLinus Torvalds 		if ((rta[IFA_LOCAL - 1] &&
4021da177e4SLinus Torvalds 		     memcmp(RTA_DATA(rta[IFA_LOCAL - 1]),
4031da177e4SLinus Torvalds 			    &ifa->ifa_local, 4)) ||
4041da177e4SLinus Torvalds 		    (rta[IFA_LABEL - 1] &&
4051da177e4SLinus Torvalds 		     rtattr_strcmp(rta[IFA_LABEL - 1], ifa->ifa_label)) ||
4061da177e4SLinus Torvalds 		    (rta[IFA_ADDRESS - 1] &&
4071da177e4SLinus Torvalds 		     (ifm->ifa_prefixlen != ifa->ifa_prefixlen ||
4081da177e4SLinus Torvalds 		      !inet_ifa_match(*(u32*)RTA_DATA(rta[IFA_ADDRESS - 1]),
4091da177e4SLinus Torvalds 			      	      ifa))))
4101da177e4SLinus Torvalds 			continue;
4111da177e4SLinus Torvalds 		inet_del_ifa(in_dev, ifap, 1);
4121da177e4SLinus Torvalds 		return 0;
4131da177e4SLinus Torvalds 	}
4141da177e4SLinus Torvalds out:
4151da177e4SLinus Torvalds 	return -EADDRNOTAVAIL;
4161da177e4SLinus Torvalds }
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds static int inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
4191da177e4SLinus Torvalds {
4201da177e4SLinus Torvalds 	struct rtattr **rta = arg;
4211da177e4SLinus Torvalds 	struct net_device *dev;
4221da177e4SLinus Torvalds 	struct in_device *in_dev;
4231da177e4SLinus Torvalds 	struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
4241da177e4SLinus Torvalds 	struct in_ifaddr *ifa;
4251da177e4SLinus Torvalds 	int rc = -EINVAL;
4261da177e4SLinus Torvalds 
4271da177e4SLinus Torvalds 	ASSERT_RTNL();
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds 	if (ifm->ifa_prefixlen > 32 || !rta[IFA_LOCAL - 1])
4301da177e4SLinus Torvalds 		goto out;
4311da177e4SLinus Torvalds 
4321da177e4SLinus Torvalds 	rc = -ENODEV;
4331da177e4SLinus Torvalds 	if ((dev = __dev_get_by_index(ifm->ifa_index)) == NULL)
4341da177e4SLinus Torvalds 		goto out;
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds 	rc = -ENOBUFS;
4371da177e4SLinus Torvalds 	if ((in_dev = __in_dev_get(dev)) == NULL) {
4381da177e4SLinus Torvalds 		in_dev = inetdev_init(dev);
4391da177e4SLinus Torvalds 		if (!in_dev)
4401da177e4SLinus Torvalds 			goto out;
4411da177e4SLinus Torvalds 	}
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds 	if ((ifa = inet_alloc_ifa()) == NULL)
4441da177e4SLinus Torvalds 		goto out;
4451da177e4SLinus Torvalds 
4461da177e4SLinus Torvalds 	if (!rta[IFA_ADDRESS - 1])
4471da177e4SLinus Torvalds 		rta[IFA_ADDRESS - 1] = rta[IFA_LOCAL - 1];
4481da177e4SLinus Torvalds 	memcpy(&ifa->ifa_local, RTA_DATA(rta[IFA_LOCAL - 1]), 4);
4491da177e4SLinus Torvalds 	memcpy(&ifa->ifa_address, RTA_DATA(rta[IFA_ADDRESS - 1]), 4);
4501da177e4SLinus Torvalds 	ifa->ifa_prefixlen = ifm->ifa_prefixlen;
4511da177e4SLinus Torvalds 	ifa->ifa_mask = inet_make_mask(ifm->ifa_prefixlen);
4521da177e4SLinus Torvalds 	if (rta[IFA_BROADCAST - 1])
4531da177e4SLinus Torvalds 		memcpy(&ifa->ifa_broadcast,
4541da177e4SLinus Torvalds 		       RTA_DATA(rta[IFA_BROADCAST - 1]), 4);
4551da177e4SLinus Torvalds 	if (rta[IFA_ANYCAST - 1])
4561da177e4SLinus Torvalds 		memcpy(&ifa->ifa_anycast, RTA_DATA(rta[IFA_ANYCAST - 1]), 4);
4571da177e4SLinus Torvalds 	ifa->ifa_flags = ifm->ifa_flags;
4581da177e4SLinus Torvalds 	ifa->ifa_scope = ifm->ifa_scope;
4591da177e4SLinus Torvalds 	in_dev_hold(in_dev);
4601da177e4SLinus Torvalds 	ifa->ifa_dev   = in_dev;
4611da177e4SLinus Torvalds 	if (rta[IFA_LABEL - 1])
4621da177e4SLinus Torvalds 		rtattr_strlcpy(ifa->ifa_label, rta[IFA_LABEL - 1], IFNAMSIZ);
4631da177e4SLinus Torvalds 	else
4641da177e4SLinus Torvalds 		memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
4651da177e4SLinus Torvalds 
4661da177e4SLinus Torvalds 	rc = inet_insert_ifa(ifa);
4671da177e4SLinus Torvalds out:
4681da177e4SLinus Torvalds 	return rc;
4691da177e4SLinus Torvalds }
4701da177e4SLinus Torvalds 
4711da177e4SLinus Torvalds /*
4721da177e4SLinus Torvalds  *	Determine a default network mask, based on the IP address.
4731da177e4SLinus Torvalds  */
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds static __inline__ int inet_abc_len(u32 addr)
4761da177e4SLinus Torvalds {
4771da177e4SLinus Torvalds 	int rc = -1;	/* Something else, probably a multicast. */
4781da177e4SLinus Torvalds 
4791da177e4SLinus Torvalds   	if (ZERONET(addr))
4801da177e4SLinus Torvalds   		rc = 0;
4811da177e4SLinus Torvalds 	else {
4821da177e4SLinus Torvalds 		addr = ntohl(addr);
4831da177e4SLinus Torvalds 
4841da177e4SLinus Torvalds 		if (IN_CLASSA(addr))
4851da177e4SLinus Torvalds 			rc = 8;
4861da177e4SLinus Torvalds 		else if (IN_CLASSB(addr))
4871da177e4SLinus Torvalds 			rc = 16;
4881da177e4SLinus Torvalds 		else if (IN_CLASSC(addr))
4891da177e4SLinus Torvalds 			rc = 24;
4901da177e4SLinus Torvalds 	}
4911da177e4SLinus Torvalds 
4921da177e4SLinus Torvalds   	return rc;
4931da177e4SLinus Torvalds }
4941da177e4SLinus Torvalds 
4951da177e4SLinus Torvalds 
4961da177e4SLinus Torvalds int devinet_ioctl(unsigned int cmd, void __user *arg)
4971da177e4SLinus Torvalds {
4981da177e4SLinus Torvalds 	struct ifreq ifr;
4991da177e4SLinus Torvalds 	struct sockaddr_in sin_orig;
5001da177e4SLinus Torvalds 	struct sockaddr_in *sin = (struct sockaddr_in *)&ifr.ifr_addr;
5011da177e4SLinus Torvalds 	struct in_device *in_dev;
5021da177e4SLinus Torvalds 	struct in_ifaddr **ifap = NULL;
5031da177e4SLinus Torvalds 	struct in_ifaddr *ifa = NULL;
5041da177e4SLinus Torvalds 	struct net_device *dev;
5051da177e4SLinus Torvalds 	char *colon;
5061da177e4SLinus Torvalds 	int ret = -EFAULT;
5071da177e4SLinus Torvalds 	int tryaddrmatch = 0;
5081da177e4SLinus Torvalds 
5091da177e4SLinus Torvalds 	/*
5101da177e4SLinus Torvalds 	 *	Fetch the caller's info block into kernel space
5111da177e4SLinus Torvalds 	 */
5121da177e4SLinus Torvalds 
5131da177e4SLinus Torvalds 	if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
5141da177e4SLinus Torvalds 		goto out;
5151da177e4SLinus Torvalds 	ifr.ifr_name[IFNAMSIZ - 1] = 0;
5161da177e4SLinus Torvalds 
5171da177e4SLinus Torvalds 	/* save original address for comparison */
5181da177e4SLinus Torvalds 	memcpy(&sin_orig, sin, sizeof(*sin));
5191da177e4SLinus Torvalds 
5201da177e4SLinus Torvalds 	colon = strchr(ifr.ifr_name, ':');
5211da177e4SLinus Torvalds 	if (colon)
5221da177e4SLinus Torvalds 		*colon = 0;
5231da177e4SLinus Torvalds 
5241da177e4SLinus Torvalds #ifdef CONFIG_KMOD
5251da177e4SLinus Torvalds 	dev_load(ifr.ifr_name);
5261da177e4SLinus Torvalds #endif
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds 	switch(cmd) {
5291da177e4SLinus Torvalds 	case SIOCGIFADDR:	/* Get interface address */
5301da177e4SLinus Torvalds 	case SIOCGIFBRDADDR:	/* Get the broadcast address */
5311da177e4SLinus Torvalds 	case SIOCGIFDSTADDR:	/* Get the destination address */
5321da177e4SLinus Torvalds 	case SIOCGIFNETMASK:	/* Get the netmask for the interface */
5331da177e4SLinus Torvalds 		/* Note that these ioctls will not sleep,
5341da177e4SLinus Torvalds 		   so that we do not impose a lock.
5351da177e4SLinus Torvalds 		   One day we will be forced to put shlock here (I mean SMP)
5361da177e4SLinus Torvalds 		 */
5371da177e4SLinus Torvalds 		tryaddrmatch = (sin_orig.sin_family == AF_INET);
5381da177e4SLinus Torvalds 		memset(sin, 0, sizeof(*sin));
5391da177e4SLinus Torvalds 		sin->sin_family = AF_INET;
5401da177e4SLinus Torvalds 		break;
5411da177e4SLinus Torvalds 
5421da177e4SLinus Torvalds 	case SIOCSIFFLAGS:
5431da177e4SLinus Torvalds 		ret = -EACCES;
5441da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
5451da177e4SLinus Torvalds 			goto out;
5461da177e4SLinus Torvalds 		break;
5471da177e4SLinus Torvalds 	case SIOCSIFADDR:	/* Set interface address (and family) */
5481da177e4SLinus Torvalds 	case SIOCSIFBRDADDR:	/* Set the broadcast address */
5491da177e4SLinus Torvalds 	case SIOCSIFDSTADDR:	/* Set the destination address */
5501da177e4SLinus Torvalds 	case SIOCSIFNETMASK: 	/* Set the netmask for the interface */
5511da177e4SLinus Torvalds 		ret = -EACCES;
5521da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
5531da177e4SLinus Torvalds 			goto out;
5541da177e4SLinus Torvalds 		ret = -EINVAL;
5551da177e4SLinus Torvalds 		if (sin->sin_family != AF_INET)
5561da177e4SLinus Torvalds 			goto out;
5571da177e4SLinus Torvalds 		break;
5581da177e4SLinus Torvalds 	default:
5591da177e4SLinus Torvalds 		ret = -EINVAL;
5601da177e4SLinus Torvalds 		goto out;
5611da177e4SLinus Torvalds 	}
5621da177e4SLinus Torvalds 
5631da177e4SLinus Torvalds 	rtnl_lock();
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds 	ret = -ENODEV;
5661da177e4SLinus Torvalds 	if ((dev = __dev_get_by_name(ifr.ifr_name)) == NULL)
5671da177e4SLinus Torvalds 		goto done;
5681da177e4SLinus Torvalds 
5691da177e4SLinus Torvalds 	if (colon)
5701da177e4SLinus Torvalds 		*colon = ':';
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds 	if ((in_dev = __in_dev_get(dev)) != NULL) {
5731da177e4SLinus Torvalds 		if (tryaddrmatch) {
5741da177e4SLinus Torvalds 			/* Matthias Andree */
5751da177e4SLinus Torvalds 			/* compare label and address (4.4BSD style) */
5761da177e4SLinus Torvalds 			/* note: we only do this for a limited set of ioctls
5771da177e4SLinus Torvalds 			   and only if the original address family was AF_INET.
5781da177e4SLinus Torvalds 			   This is checked above. */
5791da177e4SLinus Torvalds 			for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
5801da177e4SLinus Torvalds 			     ifap = &ifa->ifa_next) {
5811da177e4SLinus Torvalds 				if (!strcmp(ifr.ifr_name, ifa->ifa_label) &&
5821da177e4SLinus Torvalds 				    sin_orig.sin_addr.s_addr ==
5831da177e4SLinus Torvalds 							ifa->ifa_address) {
5841da177e4SLinus Torvalds 					break; /* found */
5851da177e4SLinus Torvalds 				}
5861da177e4SLinus Torvalds 			}
5871da177e4SLinus Torvalds 		}
5881da177e4SLinus Torvalds 		/* we didn't get a match, maybe the application is
5891da177e4SLinus Torvalds 		   4.3BSD-style and passed in junk so we fall back to
5901da177e4SLinus Torvalds 		   comparing just the label */
5911da177e4SLinus Torvalds 		if (!ifa) {
5921da177e4SLinus Torvalds 			for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
5931da177e4SLinus Torvalds 			     ifap = &ifa->ifa_next)
5941da177e4SLinus Torvalds 				if (!strcmp(ifr.ifr_name, ifa->ifa_label))
5951da177e4SLinus Torvalds 					break;
5961da177e4SLinus Torvalds 		}
5971da177e4SLinus Torvalds 	}
5981da177e4SLinus Torvalds 
5991da177e4SLinus Torvalds 	ret = -EADDRNOTAVAIL;
6001da177e4SLinus Torvalds 	if (!ifa && cmd != SIOCSIFADDR && cmd != SIOCSIFFLAGS)
6011da177e4SLinus Torvalds 		goto done;
6021da177e4SLinus Torvalds 
6031da177e4SLinus Torvalds 	switch(cmd) {
6041da177e4SLinus Torvalds 	case SIOCGIFADDR:	/* Get interface address */
6051da177e4SLinus Torvalds 		sin->sin_addr.s_addr = ifa->ifa_local;
6061da177e4SLinus Torvalds 		goto rarok;
6071da177e4SLinus Torvalds 
6081da177e4SLinus Torvalds 	case SIOCGIFBRDADDR:	/* Get the broadcast address */
6091da177e4SLinus Torvalds 		sin->sin_addr.s_addr = ifa->ifa_broadcast;
6101da177e4SLinus Torvalds 		goto rarok;
6111da177e4SLinus Torvalds 
6121da177e4SLinus Torvalds 	case SIOCGIFDSTADDR:	/* Get the destination address */
6131da177e4SLinus Torvalds 		sin->sin_addr.s_addr = ifa->ifa_address;
6141da177e4SLinus Torvalds 		goto rarok;
6151da177e4SLinus Torvalds 
6161da177e4SLinus Torvalds 	case SIOCGIFNETMASK:	/* Get the netmask for the interface */
6171da177e4SLinus Torvalds 		sin->sin_addr.s_addr = ifa->ifa_mask;
6181da177e4SLinus Torvalds 		goto rarok;
6191da177e4SLinus Torvalds 
6201da177e4SLinus Torvalds 	case SIOCSIFFLAGS:
6211da177e4SLinus Torvalds 		if (colon) {
6221da177e4SLinus Torvalds 			ret = -EADDRNOTAVAIL;
6231da177e4SLinus Torvalds 			if (!ifa)
6241da177e4SLinus Torvalds 				break;
6251da177e4SLinus Torvalds 			ret = 0;
6261da177e4SLinus Torvalds 			if (!(ifr.ifr_flags & IFF_UP))
6271da177e4SLinus Torvalds 				inet_del_ifa(in_dev, ifap, 1);
6281da177e4SLinus Torvalds 			break;
6291da177e4SLinus Torvalds 		}
6301da177e4SLinus Torvalds 		ret = dev_change_flags(dev, ifr.ifr_flags);
6311da177e4SLinus Torvalds 		break;
6321da177e4SLinus Torvalds 
6331da177e4SLinus Torvalds 	case SIOCSIFADDR:	/* Set interface address (and family) */
6341da177e4SLinus Torvalds 		ret = -EINVAL;
6351da177e4SLinus Torvalds 		if (inet_abc_len(sin->sin_addr.s_addr) < 0)
6361da177e4SLinus Torvalds 			break;
6371da177e4SLinus Torvalds 
6381da177e4SLinus Torvalds 		if (!ifa) {
6391da177e4SLinus Torvalds 			ret = -ENOBUFS;
6401da177e4SLinus Torvalds 			if ((ifa = inet_alloc_ifa()) == NULL)
6411da177e4SLinus Torvalds 				break;
6421da177e4SLinus Torvalds 			if (colon)
6431da177e4SLinus Torvalds 				memcpy(ifa->ifa_label, ifr.ifr_name, IFNAMSIZ);
6441da177e4SLinus Torvalds 			else
6451da177e4SLinus Torvalds 				memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
6461da177e4SLinus Torvalds 		} else {
6471da177e4SLinus Torvalds 			ret = 0;
6481da177e4SLinus Torvalds 			if (ifa->ifa_local == sin->sin_addr.s_addr)
6491da177e4SLinus Torvalds 				break;
6501da177e4SLinus Torvalds 			inet_del_ifa(in_dev, ifap, 0);
6511da177e4SLinus Torvalds 			ifa->ifa_broadcast = 0;
6521da177e4SLinus Torvalds 			ifa->ifa_anycast = 0;
6531da177e4SLinus Torvalds 		}
6541da177e4SLinus Torvalds 
6551da177e4SLinus Torvalds 		ifa->ifa_address = ifa->ifa_local = sin->sin_addr.s_addr;
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds 		if (!(dev->flags & IFF_POINTOPOINT)) {
6581da177e4SLinus Torvalds 			ifa->ifa_prefixlen = inet_abc_len(ifa->ifa_address);
6591da177e4SLinus Torvalds 			ifa->ifa_mask = inet_make_mask(ifa->ifa_prefixlen);
6601da177e4SLinus Torvalds 			if ((dev->flags & IFF_BROADCAST) &&
6611da177e4SLinus Torvalds 			    ifa->ifa_prefixlen < 31)
6621da177e4SLinus Torvalds 				ifa->ifa_broadcast = ifa->ifa_address |
6631da177e4SLinus Torvalds 						     ~ifa->ifa_mask;
6641da177e4SLinus Torvalds 		} else {
6651da177e4SLinus Torvalds 			ifa->ifa_prefixlen = 32;
6661da177e4SLinus Torvalds 			ifa->ifa_mask = inet_make_mask(32);
6671da177e4SLinus Torvalds 		}
6681da177e4SLinus Torvalds 		ret = inet_set_ifa(dev, ifa);
6691da177e4SLinus Torvalds 		break;
6701da177e4SLinus Torvalds 
6711da177e4SLinus Torvalds 	case SIOCSIFBRDADDR:	/* Set the broadcast address */
6721da177e4SLinus Torvalds 		ret = 0;
6731da177e4SLinus Torvalds 		if (ifa->ifa_broadcast != sin->sin_addr.s_addr) {
6741da177e4SLinus Torvalds 			inet_del_ifa(in_dev, ifap, 0);
6751da177e4SLinus Torvalds 			ifa->ifa_broadcast = sin->sin_addr.s_addr;
6761da177e4SLinus Torvalds 			inet_insert_ifa(ifa);
6771da177e4SLinus Torvalds 		}
6781da177e4SLinus Torvalds 		break;
6791da177e4SLinus Torvalds 
6801da177e4SLinus Torvalds 	case SIOCSIFDSTADDR:	/* Set the destination address */
6811da177e4SLinus Torvalds 		ret = 0;
6821da177e4SLinus Torvalds 		if (ifa->ifa_address == sin->sin_addr.s_addr)
6831da177e4SLinus Torvalds 			break;
6841da177e4SLinus Torvalds 		ret = -EINVAL;
6851da177e4SLinus Torvalds 		if (inet_abc_len(sin->sin_addr.s_addr) < 0)
6861da177e4SLinus Torvalds 			break;
6871da177e4SLinus Torvalds 		ret = 0;
6881da177e4SLinus Torvalds 		inet_del_ifa(in_dev, ifap, 0);
6891da177e4SLinus Torvalds 		ifa->ifa_address = sin->sin_addr.s_addr;
6901da177e4SLinus Torvalds 		inet_insert_ifa(ifa);
6911da177e4SLinus Torvalds 		break;
6921da177e4SLinus Torvalds 
6931da177e4SLinus Torvalds 	case SIOCSIFNETMASK: 	/* Set the netmask for the interface */
6941da177e4SLinus Torvalds 
6951da177e4SLinus Torvalds 		/*
6961da177e4SLinus Torvalds 		 *	The mask we set must be legal.
6971da177e4SLinus Torvalds 		 */
6981da177e4SLinus Torvalds 		ret = -EINVAL;
6991da177e4SLinus Torvalds 		if (bad_mask(sin->sin_addr.s_addr, 0))
7001da177e4SLinus Torvalds 			break;
7011da177e4SLinus Torvalds 		ret = 0;
7021da177e4SLinus Torvalds 		if (ifa->ifa_mask != sin->sin_addr.s_addr) {
7031da177e4SLinus Torvalds 			inet_del_ifa(in_dev, ifap, 0);
7041da177e4SLinus Torvalds 			ifa->ifa_mask = sin->sin_addr.s_addr;
7051da177e4SLinus Torvalds 			ifa->ifa_prefixlen = inet_mask_len(ifa->ifa_mask);
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds 			/* See if current broadcast address matches
7081da177e4SLinus Torvalds 			 * with current netmask, then recalculate
7091da177e4SLinus Torvalds 			 * the broadcast address. Otherwise it's a
7101da177e4SLinus Torvalds 			 * funny address, so don't touch it since
7111da177e4SLinus Torvalds 			 * the user seems to know what (s)he's doing...
7121da177e4SLinus Torvalds 			 */
7131da177e4SLinus Torvalds 			if ((dev->flags & IFF_BROADCAST) &&
7141da177e4SLinus Torvalds 			    (ifa->ifa_prefixlen < 31) &&
7151da177e4SLinus Torvalds 			    (ifa->ifa_broadcast ==
7161da177e4SLinus Torvalds 			     (ifa->ifa_local|~ifa->ifa_mask))) {
7171da177e4SLinus Torvalds 				ifa->ifa_broadcast = (ifa->ifa_local |
7181da177e4SLinus Torvalds 						      ~sin->sin_addr.s_addr);
7191da177e4SLinus Torvalds 			}
7201da177e4SLinus Torvalds 			inet_insert_ifa(ifa);
7211da177e4SLinus Torvalds 		}
7221da177e4SLinus Torvalds 		break;
7231da177e4SLinus Torvalds 	}
7241da177e4SLinus Torvalds done:
7251da177e4SLinus Torvalds 	rtnl_unlock();
7261da177e4SLinus Torvalds out:
7271da177e4SLinus Torvalds 	return ret;
7281da177e4SLinus Torvalds rarok:
7291da177e4SLinus Torvalds 	rtnl_unlock();
7301da177e4SLinus Torvalds 	ret = copy_to_user(arg, &ifr, sizeof(struct ifreq)) ? -EFAULT : 0;
7311da177e4SLinus Torvalds 	goto out;
7321da177e4SLinus Torvalds }
7331da177e4SLinus Torvalds 
7341da177e4SLinus Torvalds static int inet_gifconf(struct net_device *dev, char __user *buf, int len)
7351da177e4SLinus Torvalds {
7361da177e4SLinus Torvalds 	struct in_device *in_dev = __in_dev_get(dev);
7371da177e4SLinus Torvalds 	struct in_ifaddr *ifa;
7381da177e4SLinus Torvalds 	struct ifreq ifr;
7391da177e4SLinus Torvalds 	int done = 0;
7401da177e4SLinus Torvalds 
7411da177e4SLinus Torvalds 	if (!in_dev || (ifa = in_dev->ifa_list) == NULL)
7421da177e4SLinus Torvalds 		goto out;
7431da177e4SLinus Torvalds 
7441da177e4SLinus Torvalds 	for (; ifa; ifa = ifa->ifa_next) {
7451da177e4SLinus Torvalds 		if (!buf) {
7461da177e4SLinus Torvalds 			done += sizeof(ifr);
7471da177e4SLinus Torvalds 			continue;
7481da177e4SLinus Torvalds 		}
7491da177e4SLinus Torvalds 		if (len < (int) sizeof(ifr))
7501da177e4SLinus Torvalds 			break;
7511da177e4SLinus Torvalds 		memset(&ifr, 0, sizeof(struct ifreq));
7521da177e4SLinus Torvalds 		if (ifa->ifa_label)
7531da177e4SLinus Torvalds 			strcpy(ifr.ifr_name, ifa->ifa_label);
7541da177e4SLinus Torvalds 		else
7551da177e4SLinus Torvalds 			strcpy(ifr.ifr_name, dev->name);
7561da177e4SLinus Torvalds 
7571da177e4SLinus Torvalds 		(*(struct sockaddr_in *)&ifr.ifr_addr).sin_family = AF_INET;
7581da177e4SLinus Torvalds 		(*(struct sockaddr_in *)&ifr.ifr_addr).sin_addr.s_addr =
7591da177e4SLinus Torvalds 								ifa->ifa_local;
7601da177e4SLinus Torvalds 
7611da177e4SLinus Torvalds 		if (copy_to_user(buf, &ifr, sizeof(struct ifreq))) {
7621da177e4SLinus Torvalds 			done = -EFAULT;
7631da177e4SLinus Torvalds 			break;
7641da177e4SLinus Torvalds 		}
7651da177e4SLinus Torvalds 		buf  += sizeof(struct ifreq);
7661da177e4SLinus Torvalds 		len  -= sizeof(struct ifreq);
7671da177e4SLinus Torvalds 		done += sizeof(struct ifreq);
7681da177e4SLinus Torvalds 	}
7691da177e4SLinus Torvalds out:
7701da177e4SLinus Torvalds 	return done;
7711da177e4SLinus Torvalds }
7721da177e4SLinus Torvalds 
7731da177e4SLinus Torvalds u32 inet_select_addr(const struct net_device *dev, u32 dst, int scope)
7741da177e4SLinus Torvalds {
7751da177e4SLinus Torvalds 	u32 addr = 0;
7761da177e4SLinus Torvalds 	struct in_device *in_dev;
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds 	rcu_read_lock();
7791da177e4SLinus Torvalds 	in_dev = __in_dev_get(dev);
7801da177e4SLinus Torvalds 	if (!in_dev)
7811da177e4SLinus Torvalds 		goto no_in_dev;
7821da177e4SLinus Torvalds 
7831da177e4SLinus Torvalds 	for_primary_ifa(in_dev) {
7841da177e4SLinus Torvalds 		if (ifa->ifa_scope > scope)
7851da177e4SLinus Torvalds 			continue;
7861da177e4SLinus Torvalds 		if (!dst || inet_ifa_match(dst, ifa)) {
7871da177e4SLinus Torvalds 			addr = ifa->ifa_local;
7881da177e4SLinus Torvalds 			break;
7891da177e4SLinus Torvalds 		}
7901da177e4SLinus Torvalds 		if (!addr)
7911da177e4SLinus Torvalds 			addr = ifa->ifa_local;
7921da177e4SLinus Torvalds 	} endfor_ifa(in_dev);
7931da177e4SLinus Torvalds no_in_dev:
7941da177e4SLinus Torvalds 	rcu_read_unlock();
7951da177e4SLinus Torvalds 
7961da177e4SLinus Torvalds 	if (addr)
7971da177e4SLinus Torvalds 		goto out;
7981da177e4SLinus Torvalds 
7991da177e4SLinus Torvalds 	/* Not loopback addresses on loopback should be preferred
8001da177e4SLinus Torvalds 	   in this case. It is importnat that lo is the first interface
8011da177e4SLinus Torvalds 	   in dev_base list.
8021da177e4SLinus Torvalds 	 */
8031da177e4SLinus Torvalds 	read_lock(&dev_base_lock);
8041da177e4SLinus Torvalds 	rcu_read_lock();
8051da177e4SLinus Torvalds 	for (dev = dev_base; dev; dev = dev->next) {
8061da177e4SLinus Torvalds 		if ((in_dev = __in_dev_get(dev)) == NULL)
8071da177e4SLinus Torvalds 			continue;
8081da177e4SLinus Torvalds 
8091da177e4SLinus Torvalds 		for_primary_ifa(in_dev) {
8101da177e4SLinus Torvalds 			if (ifa->ifa_scope != RT_SCOPE_LINK &&
8111da177e4SLinus Torvalds 			    ifa->ifa_scope <= scope) {
8121da177e4SLinus Torvalds 				addr = ifa->ifa_local;
8131da177e4SLinus Torvalds 				goto out_unlock_both;
8141da177e4SLinus Torvalds 			}
8151da177e4SLinus Torvalds 		} endfor_ifa(in_dev);
8161da177e4SLinus Torvalds 	}
8171da177e4SLinus Torvalds out_unlock_both:
8181da177e4SLinus Torvalds 	read_unlock(&dev_base_lock);
8191da177e4SLinus Torvalds 	rcu_read_unlock();
8201da177e4SLinus Torvalds out:
8211da177e4SLinus Torvalds 	return addr;
8221da177e4SLinus Torvalds }
8231da177e4SLinus Torvalds 
8241da177e4SLinus Torvalds static u32 confirm_addr_indev(struct in_device *in_dev, u32 dst,
8251da177e4SLinus Torvalds 			      u32 local, int scope)
8261da177e4SLinus Torvalds {
8271da177e4SLinus Torvalds 	int same = 0;
8281da177e4SLinus Torvalds 	u32 addr = 0;
8291da177e4SLinus Torvalds 
8301da177e4SLinus Torvalds 	for_ifa(in_dev) {
8311da177e4SLinus Torvalds 		if (!addr &&
8321da177e4SLinus Torvalds 		    (local == ifa->ifa_local || !local) &&
8331da177e4SLinus Torvalds 		    ifa->ifa_scope <= scope) {
8341da177e4SLinus Torvalds 			addr = ifa->ifa_local;
8351da177e4SLinus Torvalds 			if (same)
8361da177e4SLinus Torvalds 				break;
8371da177e4SLinus Torvalds 		}
8381da177e4SLinus Torvalds 		if (!same) {
8391da177e4SLinus Torvalds 			same = (!local || inet_ifa_match(local, ifa)) &&
8401da177e4SLinus Torvalds 				(!dst || inet_ifa_match(dst, ifa));
8411da177e4SLinus Torvalds 			if (same && addr) {
8421da177e4SLinus Torvalds 				if (local || !dst)
8431da177e4SLinus Torvalds 					break;
8441da177e4SLinus Torvalds 				/* Is the selected addr into dst subnet? */
8451da177e4SLinus Torvalds 				if (inet_ifa_match(addr, ifa))
8461da177e4SLinus Torvalds 					break;
8471da177e4SLinus Torvalds 				/* No, then can we use new local src? */
8481da177e4SLinus Torvalds 				if (ifa->ifa_scope <= scope) {
8491da177e4SLinus Torvalds 					addr = ifa->ifa_local;
8501da177e4SLinus Torvalds 					break;
8511da177e4SLinus Torvalds 				}
8521da177e4SLinus Torvalds 				/* search for large dst subnet for addr */
8531da177e4SLinus Torvalds 				same = 0;
8541da177e4SLinus Torvalds 			}
8551da177e4SLinus Torvalds 		}
8561da177e4SLinus Torvalds 	} endfor_ifa(in_dev);
8571da177e4SLinus Torvalds 
8581da177e4SLinus Torvalds 	return same? addr : 0;
8591da177e4SLinus Torvalds }
8601da177e4SLinus Torvalds 
8611da177e4SLinus Torvalds /*
8621da177e4SLinus Torvalds  * Confirm that local IP address exists using wildcards:
8631da177e4SLinus Torvalds  * - dev: only on this interface, 0=any interface
8641da177e4SLinus Torvalds  * - dst: only in the same subnet as dst, 0=any dst
8651da177e4SLinus Torvalds  * - local: address, 0=autoselect the local address
8661da177e4SLinus Torvalds  * - scope: maximum allowed scope value for the local address
8671da177e4SLinus Torvalds  */
8681da177e4SLinus Torvalds u32 inet_confirm_addr(const struct net_device *dev, u32 dst, u32 local, int scope)
8691da177e4SLinus Torvalds {
8701da177e4SLinus Torvalds 	u32 addr = 0;
8711da177e4SLinus Torvalds 	struct in_device *in_dev;
8721da177e4SLinus Torvalds 
8731da177e4SLinus Torvalds 	if (dev) {
8741da177e4SLinus Torvalds 		rcu_read_lock();
8751da177e4SLinus Torvalds 		if ((in_dev = __in_dev_get(dev)))
8761da177e4SLinus Torvalds 			addr = confirm_addr_indev(in_dev, dst, local, scope);
8771da177e4SLinus Torvalds 		rcu_read_unlock();
8781da177e4SLinus Torvalds 
8791da177e4SLinus Torvalds 		return addr;
8801da177e4SLinus Torvalds 	}
8811da177e4SLinus Torvalds 
8821da177e4SLinus Torvalds 	read_lock(&dev_base_lock);
8831da177e4SLinus Torvalds 	rcu_read_lock();
8841da177e4SLinus Torvalds 	for (dev = dev_base; dev; dev = dev->next) {
8851da177e4SLinus Torvalds 		if ((in_dev = __in_dev_get(dev))) {
8861da177e4SLinus Torvalds 			addr = confirm_addr_indev(in_dev, dst, local, scope);
8871da177e4SLinus Torvalds 			if (addr)
8881da177e4SLinus Torvalds 				break;
8891da177e4SLinus Torvalds 		}
8901da177e4SLinus Torvalds 	}
8911da177e4SLinus Torvalds 	rcu_read_unlock();
8921da177e4SLinus Torvalds 	read_unlock(&dev_base_lock);
8931da177e4SLinus Torvalds 
8941da177e4SLinus Torvalds 	return addr;
8951da177e4SLinus Torvalds }
8961da177e4SLinus Torvalds 
8971da177e4SLinus Torvalds /*
8981da177e4SLinus Torvalds  *	Device notifier
8991da177e4SLinus Torvalds  */
9001da177e4SLinus Torvalds 
9011da177e4SLinus Torvalds int register_inetaddr_notifier(struct notifier_block *nb)
9021da177e4SLinus Torvalds {
9031da177e4SLinus Torvalds 	return notifier_chain_register(&inetaddr_chain, nb);
9041da177e4SLinus Torvalds }
9051da177e4SLinus Torvalds 
9061da177e4SLinus Torvalds int unregister_inetaddr_notifier(struct notifier_block *nb)
9071da177e4SLinus Torvalds {
9081da177e4SLinus Torvalds 	return notifier_chain_unregister(&inetaddr_chain, nb);
9091da177e4SLinus Torvalds }
9101da177e4SLinus Torvalds 
9111da177e4SLinus Torvalds /* Rename ifa_labels for a device name change. Make some effort to preserve existing
9121da177e4SLinus Torvalds  * alias numbering and to create unique labels if possible.
9131da177e4SLinus Torvalds */
9141da177e4SLinus Torvalds static void inetdev_changename(struct net_device *dev, struct in_device *in_dev)
9151da177e4SLinus Torvalds {
9161da177e4SLinus Torvalds 	struct in_ifaddr *ifa;
9171da177e4SLinus Torvalds 	int named = 0;
9181da177e4SLinus Torvalds 
9191da177e4SLinus Torvalds 	for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
9201da177e4SLinus Torvalds 		char old[IFNAMSIZ], *dot;
9211da177e4SLinus Torvalds 
9221da177e4SLinus Torvalds 		memcpy(old, ifa->ifa_label, IFNAMSIZ);
9231da177e4SLinus Torvalds 		memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
9241da177e4SLinus Torvalds 		if (named++ == 0)
9251da177e4SLinus Torvalds 			continue;
9261da177e4SLinus Torvalds 		dot = strchr(ifa->ifa_label, ':');
9271da177e4SLinus Torvalds 		if (dot == NULL) {
9281da177e4SLinus Torvalds 			sprintf(old, ":%d", named);
9291da177e4SLinus Torvalds 			dot = old;
9301da177e4SLinus Torvalds 		}
9311da177e4SLinus Torvalds 		if (strlen(dot) + strlen(dev->name) < IFNAMSIZ) {
9321da177e4SLinus Torvalds 			strcat(ifa->ifa_label, dot);
9331da177e4SLinus Torvalds 		} else {
9341da177e4SLinus Torvalds 			strcpy(ifa->ifa_label + (IFNAMSIZ - strlen(dot) - 1), dot);
9351da177e4SLinus Torvalds 		}
9361da177e4SLinus Torvalds 	}
9371da177e4SLinus Torvalds }
9381da177e4SLinus Torvalds 
9391da177e4SLinus Torvalds /* Called only under RTNL semaphore */
9401da177e4SLinus Torvalds 
9411da177e4SLinus Torvalds static int inetdev_event(struct notifier_block *this, unsigned long event,
9421da177e4SLinus Torvalds 			 void *ptr)
9431da177e4SLinus Torvalds {
9441da177e4SLinus Torvalds 	struct net_device *dev = ptr;
9451da177e4SLinus Torvalds 	struct in_device *in_dev = __in_dev_get(dev);
9461da177e4SLinus Torvalds 
9471da177e4SLinus Torvalds 	ASSERT_RTNL();
9481da177e4SLinus Torvalds 
9491da177e4SLinus Torvalds 	if (!in_dev) {
9501da177e4SLinus Torvalds 		if (event == NETDEV_REGISTER && dev == &loopback_dev) {
9511da177e4SLinus Torvalds 			in_dev = inetdev_init(dev);
9521da177e4SLinus Torvalds 			if (!in_dev)
9531da177e4SLinus Torvalds 				panic("devinet: Failed to create loopback\n");
9541da177e4SLinus Torvalds 			in_dev->cnf.no_xfrm = 1;
9551da177e4SLinus Torvalds 			in_dev->cnf.no_policy = 1;
9561da177e4SLinus Torvalds 		}
9571da177e4SLinus Torvalds 		goto out;
9581da177e4SLinus Torvalds 	}
9591da177e4SLinus Torvalds 
9601da177e4SLinus Torvalds 	switch (event) {
9611da177e4SLinus Torvalds 	case NETDEV_REGISTER:
9621da177e4SLinus Torvalds 		printk(KERN_DEBUG "inetdev_event: bug\n");
9631da177e4SLinus Torvalds 		dev->ip_ptr = NULL;
9641da177e4SLinus Torvalds 		break;
9651da177e4SLinus Torvalds 	case NETDEV_UP:
9661da177e4SLinus Torvalds 		if (dev->mtu < 68)
9671da177e4SLinus Torvalds 			break;
9681da177e4SLinus Torvalds 		if (dev == &loopback_dev) {
9691da177e4SLinus Torvalds 			struct in_ifaddr *ifa;
9701da177e4SLinus Torvalds 			if ((ifa = inet_alloc_ifa()) != NULL) {
9711da177e4SLinus Torvalds 				ifa->ifa_local =
9721da177e4SLinus Torvalds 				  ifa->ifa_address = htonl(INADDR_LOOPBACK);
9731da177e4SLinus Torvalds 				ifa->ifa_prefixlen = 8;
9741da177e4SLinus Torvalds 				ifa->ifa_mask = inet_make_mask(8);
9751da177e4SLinus Torvalds 				in_dev_hold(in_dev);
9761da177e4SLinus Torvalds 				ifa->ifa_dev = in_dev;
9771da177e4SLinus Torvalds 				ifa->ifa_scope = RT_SCOPE_HOST;
9781da177e4SLinus Torvalds 				memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
9791da177e4SLinus Torvalds 				inet_insert_ifa(ifa);
9801da177e4SLinus Torvalds 			}
9811da177e4SLinus Torvalds 		}
9821da177e4SLinus Torvalds 		ip_mc_up(in_dev);
9831da177e4SLinus Torvalds 		break;
9841da177e4SLinus Torvalds 	case NETDEV_DOWN:
9851da177e4SLinus Torvalds 		ip_mc_down(in_dev);
9861da177e4SLinus Torvalds 		break;
9871da177e4SLinus Torvalds 	case NETDEV_CHANGEMTU:
9881da177e4SLinus Torvalds 		if (dev->mtu >= 68)
9891da177e4SLinus Torvalds 			break;
9901da177e4SLinus Torvalds 		/* MTU falled under 68, disable IP */
9911da177e4SLinus Torvalds 	case NETDEV_UNREGISTER:
9921da177e4SLinus Torvalds 		inetdev_destroy(in_dev);
9931da177e4SLinus Torvalds 		break;
9941da177e4SLinus Torvalds 	case NETDEV_CHANGENAME:
9951da177e4SLinus Torvalds 		/* Do not notify about label change, this event is
9961da177e4SLinus Torvalds 		 * not interesting to applications using netlink.
9971da177e4SLinus Torvalds 		 */
9981da177e4SLinus Torvalds 		inetdev_changename(dev, in_dev);
9991da177e4SLinus Torvalds 
10001da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
10011da177e4SLinus Torvalds 		devinet_sysctl_unregister(&in_dev->cnf);
10021da177e4SLinus Torvalds 		neigh_sysctl_unregister(in_dev->arp_parms);
10031da177e4SLinus Torvalds 		neigh_sysctl_register(dev, in_dev->arp_parms, NET_IPV4,
10041da177e4SLinus Torvalds 				      NET_IPV4_NEIGH, "ipv4", NULL, NULL);
10051da177e4SLinus Torvalds 		devinet_sysctl_register(in_dev, &in_dev->cnf);
10061da177e4SLinus Torvalds #endif
10071da177e4SLinus Torvalds 		break;
10081da177e4SLinus Torvalds 	}
10091da177e4SLinus Torvalds out:
10101da177e4SLinus Torvalds 	return NOTIFY_DONE;
10111da177e4SLinus Torvalds }
10121da177e4SLinus Torvalds 
10131da177e4SLinus Torvalds static struct notifier_block ip_netdev_notifier = {
10141da177e4SLinus Torvalds 	.notifier_call =inetdev_event,
10151da177e4SLinus Torvalds };
10161da177e4SLinus Torvalds 
10171da177e4SLinus Torvalds static int inet_fill_ifaddr(struct sk_buff *skb, struct in_ifaddr *ifa,
10181da177e4SLinus Torvalds 			    u32 pid, u32 seq, int event)
10191da177e4SLinus Torvalds {
10201da177e4SLinus Torvalds 	struct ifaddrmsg *ifm;
10211da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
10221da177e4SLinus Torvalds 	unsigned char	 *b = skb->tail;
10231da177e4SLinus Torvalds 
10241da177e4SLinus Torvalds 	nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*ifm));
10251da177e4SLinus Torvalds 	if (pid) nlh->nlmsg_flags |= NLM_F_MULTI;
10261da177e4SLinus Torvalds 	ifm = NLMSG_DATA(nlh);
10271da177e4SLinus Torvalds 	ifm->ifa_family = AF_INET;
10281da177e4SLinus Torvalds 	ifm->ifa_prefixlen = ifa->ifa_prefixlen;
10291da177e4SLinus Torvalds 	ifm->ifa_flags = ifa->ifa_flags|IFA_F_PERMANENT;
10301da177e4SLinus Torvalds 	ifm->ifa_scope = ifa->ifa_scope;
10311da177e4SLinus Torvalds 	ifm->ifa_index = ifa->ifa_dev->dev->ifindex;
10321da177e4SLinus Torvalds 	if (ifa->ifa_address)
10331da177e4SLinus Torvalds 		RTA_PUT(skb, IFA_ADDRESS, 4, &ifa->ifa_address);
10341da177e4SLinus Torvalds 	if (ifa->ifa_local)
10351da177e4SLinus Torvalds 		RTA_PUT(skb, IFA_LOCAL, 4, &ifa->ifa_local);
10361da177e4SLinus Torvalds 	if (ifa->ifa_broadcast)
10371da177e4SLinus Torvalds 		RTA_PUT(skb, IFA_BROADCAST, 4, &ifa->ifa_broadcast);
10381da177e4SLinus Torvalds 	if (ifa->ifa_anycast)
10391da177e4SLinus Torvalds 		RTA_PUT(skb, IFA_ANYCAST, 4, &ifa->ifa_anycast);
10401da177e4SLinus Torvalds 	if (ifa->ifa_label[0])
10411da177e4SLinus Torvalds 		RTA_PUT(skb, IFA_LABEL, IFNAMSIZ, &ifa->ifa_label);
10421da177e4SLinus Torvalds 	nlh->nlmsg_len = skb->tail - b;
10431da177e4SLinus Torvalds 	return skb->len;
10441da177e4SLinus Torvalds 
10451da177e4SLinus Torvalds nlmsg_failure:
10461da177e4SLinus Torvalds rtattr_failure:
10471da177e4SLinus Torvalds 	skb_trim(skb, b - skb->data);
10481da177e4SLinus Torvalds 	return -1;
10491da177e4SLinus Torvalds }
10501da177e4SLinus Torvalds 
10511da177e4SLinus Torvalds static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
10521da177e4SLinus Torvalds {
10531da177e4SLinus Torvalds 	int idx, ip_idx;
10541da177e4SLinus Torvalds 	struct net_device *dev;
10551da177e4SLinus Torvalds 	struct in_device *in_dev;
10561da177e4SLinus Torvalds 	struct in_ifaddr *ifa;
10571da177e4SLinus Torvalds 	int s_ip_idx, s_idx = cb->args[0];
10581da177e4SLinus Torvalds 
10591da177e4SLinus Torvalds 	s_ip_idx = ip_idx = cb->args[1];
10601da177e4SLinus Torvalds 	read_lock(&dev_base_lock);
10611da177e4SLinus Torvalds 	for (dev = dev_base, idx = 0; dev; dev = dev->next, idx++) {
10621da177e4SLinus Torvalds 		if (idx < s_idx)
10631da177e4SLinus Torvalds 			continue;
10641da177e4SLinus Torvalds 		if (idx > s_idx)
10651da177e4SLinus Torvalds 			s_ip_idx = 0;
10661da177e4SLinus Torvalds 		rcu_read_lock();
10671da177e4SLinus Torvalds 		if ((in_dev = __in_dev_get(dev)) == NULL) {
10681da177e4SLinus Torvalds 			rcu_read_unlock();
10691da177e4SLinus Torvalds 			continue;
10701da177e4SLinus Torvalds 		}
10711da177e4SLinus Torvalds 
10721da177e4SLinus Torvalds 		for (ifa = in_dev->ifa_list, ip_idx = 0; ifa;
10731da177e4SLinus Torvalds 		     ifa = ifa->ifa_next, ip_idx++) {
10741da177e4SLinus Torvalds 			if (ip_idx < s_ip_idx)
10751da177e4SLinus Torvalds 				continue;
10761da177e4SLinus Torvalds 			if (inet_fill_ifaddr(skb, ifa, NETLINK_CB(cb->skb).pid,
10771da177e4SLinus Torvalds 					     cb->nlh->nlmsg_seq,
10781da177e4SLinus Torvalds 					     RTM_NEWADDR) <= 0) {
10791da177e4SLinus Torvalds 				rcu_read_unlock();
10801da177e4SLinus Torvalds 				goto done;
10811da177e4SLinus Torvalds 			}
10821da177e4SLinus Torvalds 		}
10831da177e4SLinus Torvalds 		rcu_read_unlock();
10841da177e4SLinus Torvalds 	}
10851da177e4SLinus Torvalds 
10861da177e4SLinus Torvalds done:
10871da177e4SLinus Torvalds 	read_unlock(&dev_base_lock);
10881da177e4SLinus Torvalds 	cb->args[0] = idx;
10891da177e4SLinus Torvalds 	cb->args[1] = ip_idx;
10901da177e4SLinus Torvalds 
10911da177e4SLinus Torvalds 	return skb->len;
10921da177e4SLinus Torvalds }
10931da177e4SLinus Torvalds 
10941da177e4SLinus Torvalds static void rtmsg_ifa(int event, struct in_ifaddr* ifa)
10951da177e4SLinus Torvalds {
10961da177e4SLinus Torvalds 	int size = NLMSG_SPACE(sizeof(struct ifaddrmsg) + 128);
10971da177e4SLinus Torvalds 	struct sk_buff *skb = alloc_skb(size, GFP_KERNEL);
10981da177e4SLinus Torvalds 
10991da177e4SLinus Torvalds 	if (!skb)
11001da177e4SLinus Torvalds 		netlink_set_err(rtnl, 0, RTMGRP_IPV4_IFADDR, ENOBUFS);
11011da177e4SLinus Torvalds 	else if (inet_fill_ifaddr(skb, ifa, 0, 0, event) < 0) {
11021da177e4SLinus Torvalds 		kfree_skb(skb);
11031da177e4SLinus Torvalds 		netlink_set_err(rtnl, 0, RTMGRP_IPV4_IFADDR, EINVAL);
11041da177e4SLinus Torvalds 	} else {
11051da177e4SLinus Torvalds 		NETLINK_CB(skb).dst_groups = RTMGRP_IPV4_IFADDR;
11061da177e4SLinus Torvalds 		netlink_broadcast(rtnl, skb, 0, RTMGRP_IPV4_IFADDR, GFP_KERNEL);
11071da177e4SLinus Torvalds 	}
11081da177e4SLinus Torvalds }
11091da177e4SLinus Torvalds 
1110db46edc6SThomas Graf static struct rtnetlink_link inet_rtnetlink_table[RTM_NR_MSGTYPES] = {
1111db46edc6SThomas Graf 	[RTM_NEWADDR  - RTM_BASE] = { .doit	= inet_rtm_newaddr,	},
1112db46edc6SThomas Graf 	[RTM_DELADDR  - RTM_BASE] = { .doit	= inet_rtm_deladdr,	},
1113db46edc6SThomas Graf 	[RTM_GETADDR  - RTM_BASE] = { .dumpit	= inet_dump_ifaddr,	},
1114db46edc6SThomas Graf 	[RTM_NEWROUTE - RTM_BASE] = { .doit	= inet_rtm_newroute,	},
1115db46edc6SThomas Graf 	[RTM_DELROUTE - RTM_BASE] = { .doit	= inet_rtm_delroute,	},
1116db46edc6SThomas Graf 	[RTM_GETROUTE - RTM_BASE] = { .doit	= inet_rtm_getroute,
1117db46edc6SThomas Graf 				      .dumpit	= inet_dump_fib,	},
11181da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTIPLE_TABLES
1119db46edc6SThomas Graf 	[RTM_NEWRULE  - RTM_BASE] = { .doit	= inet_rtm_newrule,	},
1120db46edc6SThomas Graf 	[RTM_DELRULE  - RTM_BASE] = { .doit	= inet_rtm_delrule,	},
1121db46edc6SThomas Graf 	[RTM_GETRULE  - RTM_BASE] = { .dumpit	= inet_dump_rules,	},
11221da177e4SLinus Torvalds #endif
11231da177e4SLinus Torvalds };
11241da177e4SLinus Torvalds 
11251da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
11261da177e4SLinus Torvalds 
11271da177e4SLinus Torvalds void inet_forward_change(void)
11281da177e4SLinus Torvalds {
11291da177e4SLinus Torvalds 	struct net_device *dev;
11301da177e4SLinus Torvalds 	int on = ipv4_devconf.forwarding;
11311da177e4SLinus Torvalds 
11321da177e4SLinus Torvalds 	ipv4_devconf.accept_redirects = !on;
11331da177e4SLinus Torvalds 	ipv4_devconf_dflt.forwarding = on;
11341da177e4SLinus Torvalds 
11351da177e4SLinus Torvalds 	read_lock(&dev_base_lock);
11361da177e4SLinus Torvalds 	for (dev = dev_base; dev; dev = dev->next) {
11371da177e4SLinus Torvalds 		struct in_device *in_dev;
11381da177e4SLinus Torvalds 		rcu_read_lock();
11391da177e4SLinus Torvalds 		in_dev = __in_dev_get(dev);
11401da177e4SLinus Torvalds 		if (in_dev)
11411da177e4SLinus Torvalds 			in_dev->cnf.forwarding = on;
11421da177e4SLinus Torvalds 		rcu_read_unlock();
11431da177e4SLinus Torvalds 	}
11441da177e4SLinus Torvalds 	read_unlock(&dev_base_lock);
11451da177e4SLinus Torvalds 
11461da177e4SLinus Torvalds 	rt_cache_flush(0);
11471da177e4SLinus Torvalds }
11481da177e4SLinus Torvalds 
11491da177e4SLinus Torvalds static int devinet_sysctl_forward(ctl_table *ctl, int write,
11501da177e4SLinus Torvalds 				  struct file* filp, void __user *buffer,
11511da177e4SLinus Torvalds 				  size_t *lenp, loff_t *ppos)
11521da177e4SLinus Torvalds {
11531da177e4SLinus Torvalds 	int *valp = ctl->data;
11541da177e4SLinus Torvalds 	int val = *valp;
11551da177e4SLinus Torvalds 	int ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
11561da177e4SLinus Torvalds 
11571da177e4SLinus Torvalds 	if (write && *valp != val) {
11581da177e4SLinus Torvalds 		if (valp == &ipv4_devconf.forwarding)
11591da177e4SLinus Torvalds 			inet_forward_change();
11601da177e4SLinus Torvalds 		else if (valp != &ipv4_devconf_dflt.forwarding)
11611da177e4SLinus Torvalds 			rt_cache_flush(0);
11621da177e4SLinus Torvalds 	}
11631da177e4SLinus Torvalds 
11641da177e4SLinus Torvalds 	return ret;
11651da177e4SLinus Torvalds }
11661da177e4SLinus Torvalds 
11671da177e4SLinus Torvalds int ipv4_doint_and_flush(ctl_table *ctl, int write,
11681da177e4SLinus Torvalds 			 struct file* filp, void __user *buffer,
11691da177e4SLinus Torvalds 			 size_t *lenp, loff_t *ppos)
11701da177e4SLinus Torvalds {
11711da177e4SLinus Torvalds 	int *valp = ctl->data;
11721da177e4SLinus Torvalds 	int val = *valp;
11731da177e4SLinus Torvalds 	int ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
11741da177e4SLinus Torvalds 
11751da177e4SLinus Torvalds 	if (write && *valp != val)
11761da177e4SLinus Torvalds 		rt_cache_flush(0);
11771da177e4SLinus Torvalds 
11781da177e4SLinus Torvalds 	return ret;
11791da177e4SLinus Torvalds }
11801da177e4SLinus Torvalds 
11811da177e4SLinus Torvalds int ipv4_doint_and_flush_strategy(ctl_table *table, int __user *name, int nlen,
11821da177e4SLinus Torvalds 				  void __user *oldval, size_t __user *oldlenp,
11831da177e4SLinus Torvalds 				  void __user *newval, size_t newlen,
11841da177e4SLinus Torvalds 				  void **context)
11851da177e4SLinus Torvalds {
11861da177e4SLinus Torvalds 	int *valp = table->data;
11871da177e4SLinus Torvalds 	int new;
11881da177e4SLinus Torvalds 
11891da177e4SLinus Torvalds 	if (!newval || !newlen)
11901da177e4SLinus Torvalds 		return 0;
11911da177e4SLinus Torvalds 
11921da177e4SLinus Torvalds 	if (newlen != sizeof(int))
11931da177e4SLinus Torvalds 		return -EINVAL;
11941da177e4SLinus Torvalds 
11951da177e4SLinus Torvalds 	if (get_user(new, (int __user *)newval))
11961da177e4SLinus Torvalds 		return -EFAULT;
11971da177e4SLinus Torvalds 
11981da177e4SLinus Torvalds 	if (new == *valp)
11991da177e4SLinus Torvalds 		return 0;
12001da177e4SLinus Torvalds 
12011da177e4SLinus Torvalds 	if (oldval && oldlenp) {
12021da177e4SLinus Torvalds 		size_t len;
12031da177e4SLinus Torvalds 
12041da177e4SLinus Torvalds 		if (get_user(len, oldlenp))
12051da177e4SLinus Torvalds 			return -EFAULT;
12061da177e4SLinus Torvalds 
12071da177e4SLinus Torvalds 		if (len) {
12081da177e4SLinus Torvalds 			if (len > table->maxlen)
12091da177e4SLinus Torvalds 				len = table->maxlen;
12101da177e4SLinus Torvalds 			if (copy_to_user(oldval, valp, len))
12111da177e4SLinus Torvalds 				return -EFAULT;
12121da177e4SLinus Torvalds 			if (put_user(len, oldlenp))
12131da177e4SLinus Torvalds 				return -EFAULT;
12141da177e4SLinus Torvalds 		}
12151da177e4SLinus Torvalds 	}
12161da177e4SLinus Torvalds 
12171da177e4SLinus Torvalds 	*valp = new;
12181da177e4SLinus Torvalds 	rt_cache_flush(0);
12191da177e4SLinus Torvalds 	return 1;
12201da177e4SLinus Torvalds }
12211da177e4SLinus Torvalds 
12221da177e4SLinus Torvalds 
12231da177e4SLinus Torvalds static struct devinet_sysctl_table {
12241da177e4SLinus Torvalds 	struct ctl_table_header *sysctl_header;
12251da177e4SLinus Torvalds 	ctl_table		devinet_vars[__NET_IPV4_CONF_MAX];
12261da177e4SLinus Torvalds 	ctl_table		devinet_dev[2];
12271da177e4SLinus Torvalds 	ctl_table		devinet_conf_dir[2];
12281da177e4SLinus Torvalds 	ctl_table		devinet_proto_dir[2];
12291da177e4SLinus Torvalds 	ctl_table		devinet_root_dir[2];
12301da177e4SLinus Torvalds } devinet_sysctl = {
12311da177e4SLinus Torvalds 	.devinet_vars = {
12321da177e4SLinus Torvalds 		{
12331da177e4SLinus Torvalds 			.ctl_name	= NET_IPV4_CONF_FORWARDING,
12341da177e4SLinus Torvalds 			.procname	= "forwarding",
12351da177e4SLinus Torvalds 			.data		= &ipv4_devconf.forwarding,
12361da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
12371da177e4SLinus Torvalds 			.mode		= 0644,
12381da177e4SLinus Torvalds 			.proc_handler	= &devinet_sysctl_forward,
12391da177e4SLinus Torvalds 		},
12401da177e4SLinus Torvalds 		{
12411da177e4SLinus Torvalds 			.ctl_name	= NET_IPV4_CONF_MC_FORWARDING,
12421da177e4SLinus Torvalds 			.procname	= "mc_forwarding",
12431da177e4SLinus Torvalds 			.data		= &ipv4_devconf.mc_forwarding,
12441da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
12451da177e4SLinus Torvalds 			.mode		= 0444,
12461da177e4SLinus Torvalds 			.proc_handler	= &proc_dointvec,
12471da177e4SLinus Torvalds 		},
12481da177e4SLinus Torvalds 		{
12491da177e4SLinus Torvalds 			.ctl_name	= NET_IPV4_CONF_ACCEPT_REDIRECTS,
12501da177e4SLinus Torvalds 			.procname	= "accept_redirects",
12511da177e4SLinus Torvalds 			.data		= &ipv4_devconf.accept_redirects,
12521da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
12531da177e4SLinus Torvalds 			.mode		= 0644,
12541da177e4SLinus Torvalds 			.proc_handler	= &proc_dointvec,
12551da177e4SLinus Torvalds 		},
12561da177e4SLinus Torvalds 		{
12571da177e4SLinus Torvalds 			.ctl_name	= NET_IPV4_CONF_SECURE_REDIRECTS,
12581da177e4SLinus Torvalds 			.procname	= "secure_redirects",
12591da177e4SLinus Torvalds 			.data		= &ipv4_devconf.secure_redirects,
12601da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
12611da177e4SLinus Torvalds 			.mode		= 0644,
12621da177e4SLinus Torvalds 			.proc_handler	= &proc_dointvec,
12631da177e4SLinus Torvalds 		},
12641da177e4SLinus Torvalds 		{
12651da177e4SLinus Torvalds 			.ctl_name	= NET_IPV4_CONF_SHARED_MEDIA,
12661da177e4SLinus Torvalds 			.procname	= "shared_media",
12671da177e4SLinus Torvalds 			.data		= &ipv4_devconf.shared_media,
12681da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
12691da177e4SLinus Torvalds 			.mode		= 0644,
12701da177e4SLinus Torvalds 			.proc_handler	= &proc_dointvec,
12711da177e4SLinus Torvalds 		},
12721da177e4SLinus Torvalds 		{
12731da177e4SLinus Torvalds 			.ctl_name	= NET_IPV4_CONF_RP_FILTER,
12741da177e4SLinus Torvalds 			.procname	= "rp_filter",
12751da177e4SLinus Torvalds 			.data		= &ipv4_devconf.rp_filter,
12761da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
12771da177e4SLinus Torvalds 			.mode		= 0644,
12781da177e4SLinus Torvalds 			.proc_handler	= &proc_dointvec,
12791da177e4SLinus Torvalds 		},
12801da177e4SLinus Torvalds 		{
12811da177e4SLinus Torvalds 			.ctl_name	= NET_IPV4_CONF_SEND_REDIRECTS,
12821da177e4SLinus Torvalds 			.procname	= "send_redirects",
12831da177e4SLinus Torvalds 			.data		= &ipv4_devconf.send_redirects,
12841da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
12851da177e4SLinus Torvalds 			.mode		= 0644,
12861da177e4SLinus Torvalds 			.proc_handler	= &proc_dointvec,
12871da177e4SLinus Torvalds 		},
12881da177e4SLinus Torvalds 		{
12891da177e4SLinus Torvalds 			.ctl_name	= NET_IPV4_CONF_ACCEPT_SOURCE_ROUTE,
12901da177e4SLinus Torvalds 			.procname	= "accept_source_route",
12911da177e4SLinus Torvalds 			.data		= &ipv4_devconf.accept_source_route,
12921da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
12931da177e4SLinus Torvalds 			.mode		= 0644,
12941da177e4SLinus Torvalds 			.proc_handler	= &proc_dointvec,
12951da177e4SLinus Torvalds 		},
12961da177e4SLinus Torvalds 		{
12971da177e4SLinus Torvalds 			.ctl_name	= NET_IPV4_CONF_PROXY_ARP,
12981da177e4SLinus Torvalds 			.procname	= "proxy_arp",
12991da177e4SLinus Torvalds 			.data		= &ipv4_devconf.proxy_arp,
13001da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
13011da177e4SLinus Torvalds 			.mode		= 0644,
13021da177e4SLinus Torvalds 			.proc_handler	= &proc_dointvec,
13031da177e4SLinus Torvalds 		},
13041da177e4SLinus Torvalds 		{
13051da177e4SLinus Torvalds 			.ctl_name	= NET_IPV4_CONF_MEDIUM_ID,
13061da177e4SLinus Torvalds 			.procname	= "medium_id",
13071da177e4SLinus Torvalds 			.data		= &ipv4_devconf.medium_id,
13081da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
13091da177e4SLinus Torvalds 			.mode		= 0644,
13101da177e4SLinus Torvalds 			.proc_handler	= &proc_dointvec,
13111da177e4SLinus Torvalds 		},
13121da177e4SLinus Torvalds 		{
13131da177e4SLinus Torvalds 			.ctl_name	= NET_IPV4_CONF_BOOTP_RELAY,
13141da177e4SLinus Torvalds 			.procname	= "bootp_relay",
13151da177e4SLinus Torvalds 			.data		= &ipv4_devconf.bootp_relay,
13161da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
13171da177e4SLinus Torvalds 			.mode		= 0644,
13181da177e4SLinus Torvalds 			.proc_handler	= &proc_dointvec,
13191da177e4SLinus Torvalds 		},
13201da177e4SLinus Torvalds 		{
13211da177e4SLinus Torvalds 			.ctl_name	= NET_IPV4_CONF_LOG_MARTIANS,
13221da177e4SLinus Torvalds 			.procname	= "log_martians",
13231da177e4SLinus Torvalds 			.data		= &ipv4_devconf.log_martians,
13241da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
13251da177e4SLinus Torvalds 			.mode		= 0644,
13261da177e4SLinus Torvalds 			.proc_handler	= &proc_dointvec,
13271da177e4SLinus Torvalds 		},
13281da177e4SLinus Torvalds 		{
13291da177e4SLinus Torvalds 			.ctl_name	= NET_IPV4_CONF_TAG,
13301da177e4SLinus Torvalds 			.procname	= "tag",
13311da177e4SLinus Torvalds 			.data		= &ipv4_devconf.tag,
13321da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
13331da177e4SLinus Torvalds 			.mode		= 0644,
13341da177e4SLinus Torvalds 			.proc_handler	= &proc_dointvec,
13351da177e4SLinus Torvalds 		},
13361da177e4SLinus Torvalds 		{
13371da177e4SLinus Torvalds 			.ctl_name	= NET_IPV4_CONF_ARPFILTER,
13381da177e4SLinus Torvalds 			.procname	= "arp_filter",
13391da177e4SLinus Torvalds 			.data		= &ipv4_devconf.arp_filter,
13401da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
13411da177e4SLinus Torvalds 			.mode		= 0644,
13421da177e4SLinus Torvalds 			.proc_handler	= &proc_dointvec,
13431da177e4SLinus Torvalds 		},
13441da177e4SLinus Torvalds 		{
13451da177e4SLinus Torvalds 			.ctl_name	= NET_IPV4_CONF_ARP_ANNOUNCE,
13461da177e4SLinus Torvalds 			.procname	= "arp_announce",
13471da177e4SLinus Torvalds 			.data		= &ipv4_devconf.arp_announce,
13481da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
13491da177e4SLinus Torvalds 			.mode		= 0644,
13501da177e4SLinus Torvalds 			.proc_handler	= &proc_dointvec,
13511da177e4SLinus Torvalds 		},
13521da177e4SLinus Torvalds 		{
13531da177e4SLinus Torvalds 			.ctl_name	= NET_IPV4_CONF_ARP_IGNORE,
13541da177e4SLinus Torvalds 			.procname	= "arp_ignore",
13551da177e4SLinus Torvalds 			.data		= &ipv4_devconf.arp_ignore,
13561da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
13571da177e4SLinus Torvalds 			.mode		= 0644,
13581da177e4SLinus Torvalds 			.proc_handler	= &proc_dointvec,
13591da177e4SLinus Torvalds 		},
13601da177e4SLinus Torvalds 		{
13611da177e4SLinus Torvalds 			.ctl_name	= NET_IPV4_CONF_NOXFRM,
13621da177e4SLinus Torvalds 			.procname	= "disable_xfrm",
13631da177e4SLinus Torvalds 			.data		= &ipv4_devconf.no_xfrm,
13641da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
13651da177e4SLinus Torvalds 			.mode		= 0644,
13661da177e4SLinus Torvalds 			.proc_handler	= &ipv4_doint_and_flush,
13671da177e4SLinus Torvalds 			.strategy	= &ipv4_doint_and_flush_strategy,
13681da177e4SLinus Torvalds 		},
13691da177e4SLinus Torvalds 		{
13701da177e4SLinus Torvalds 			.ctl_name	= NET_IPV4_CONF_NOPOLICY,
13711da177e4SLinus Torvalds 			.procname	= "disable_policy",
13721da177e4SLinus Torvalds 			.data		= &ipv4_devconf.no_policy,
13731da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
13741da177e4SLinus Torvalds 			.mode		= 0644,
13751da177e4SLinus Torvalds 			.proc_handler	= &ipv4_doint_and_flush,
13761da177e4SLinus Torvalds 			.strategy	= &ipv4_doint_and_flush_strategy,
13771da177e4SLinus Torvalds 		},
13781da177e4SLinus Torvalds 		{
13791da177e4SLinus Torvalds 			.ctl_name	= NET_IPV4_CONF_FORCE_IGMP_VERSION,
13801da177e4SLinus Torvalds 			.procname	= "force_igmp_version",
13811da177e4SLinus Torvalds 			.data		= &ipv4_devconf.force_igmp_version,
13821da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
13831da177e4SLinus Torvalds 			.mode		= 0644,
13841da177e4SLinus Torvalds 			.proc_handler	= &ipv4_doint_and_flush,
13851da177e4SLinus Torvalds 			.strategy	= &ipv4_doint_and_flush_strategy,
13861da177e4SLinus Torvalds 		},
13871da177e4SLinus Torvalds 	},
13881da177e4SLinus Torvalds 	.devinet_dev = {
13891da177e4SLinus Torvalds 		{
13901da177e4SLinus Torvalds 			.ctl_name	= NET_PROTO_CONF_ALL,
13911da177e4SLinus Torvalds 			.procname	= "all",
13921da177e4SLinus Torvalds 			.mode		= 0555,
13931da177e4SLinus Torvalds 			.child		= devinet_sysctl.devinet_vars,
13941da177e4SLinus Torvalds 		},
13951da177e4SLinus Torvalds 	},
13961da177e4SLinus Torvalds 	.devinet_conf_dir = {
13971da177e4SLinus Torvalds 	        {
13981da177e4SLinus Torvalds 			.ctl_name	= NET_IPV4_CONF,
13991da177e4SLinus Torvalds 			.procname	= "conf",
14001da177e4SLinus Torvalds 			.mode		= 0555,
14011da177e4SLinus Torvalds 			.child		= devinet_sysctl.devinet_dev,
14021da177e4SLinus Torvalds 		},
14031da177e4SLinus Torvalds 	},
14041da177e4SLinus Torvalds 	.devinet_proto_dir = {
14051da177e4SLinus Torvalds 		{
14061da177e4SLinus Torvalds 			.ctl_name	= NET_IPV4,
14071da177e4SLinus Torvalds 			.procname	= "ipv4",
14081da177e4SLinus Torvalds 			.mode		= 0555,
14091da177e4SLinus Torvalds 			.child 		= devinet_sysctl.devinet_conf_dir,
14101da177e4SLinus Torvalds 		},
14111da177e4SLinus Torvalds 	},
14121da177e4SLinus Torvalds 	.devinet_root_dir = {
14131da177e4SLinus Torvalds 		{
14141da177e4SLinus Torvalds 			.ctl_name	= CTL_NET,
14151da177e4SLinus Torvalds 			.procname 	= "net",
14161da177e4SLinus Torvalds 			.mode		= 0555,
14171da177e4SLinus Torvalds 			.child		= devinet_sysctl.devinet_proto_dir,
14181da177e4SLinus Torvalds 		},
14191da177e4SLinus Torvalds 	},
14201da177e4SLinus Torvalds };
14211da177e4SLinus Torvalds 
14221da177e4SLinus Torvalds static void devinet_sysctl_register(struct in_device *in_dev,
14231da177e4SLinus Torvalds 				    struct ipv4_devconf *p)
14241da177e4SLinus Torvalds {
14251da177e4SLinus Torvalds 	int i;
14261da177e4SLinus Torvalds 	struct net_device *dev = in_dev ? in_dev->dev : NULL;
14271da177e4SLinus Torvalds 	struct devinet_sysctl_table *t = kmalloc(sizeof(*t), GFP_KERNEL);
14281da177e4SLinus Torvalds 	char *dev_name = NULL;
14291da177e4SLinus Torvalds 
14301da177e4SLinus Torvalds 	if (!t)
14311da177e4SLinus Torvalds 		return;
14321da177e4SLinus Torvalds 	memcpy(t, &devinet_sysctl, sizeof(*t));
14331da177e4SLinus Torvalds 	for (i = 0; i < ARRAY_SIZE(t->devinet_vars) - 1; i++) {
14341da177e4SLinus Torvalds 		t->devinet_vars[i].data += (char *)p - (char *)&ipv4_devconf;
14351da177e4SLinus Torvalds 		t->devinet_vars[i].de = NULL;
14361da177e4SLinus Torvalds 	}
14371da177e4SLinus Torvalds 
14381da177e4SLinus Torvalds 	if (dev) {
14391da177e4SLinus Torvalds 		dev_name = dev->name;
14401da177e4SLinus Torvalds 		t->devinet_dev[0].ctl_name = dev->ifindex;
14411da177e4SLinus Torvalds 	} else {
14421da177e4SLinus Torvalds 		dev_name = "default";
14431da177e4SLinus Torvalds 		t->devinet_dev[0].ctl_name = NET_PROTO_CONF_DEFAULT;
14441da177e4SLinus Torvalds 	}
14451da177e4SLinus Torvalds 
14461da177e4SLinus Torvalds 	/*
14471da177e4SLinus Torvalds 	 * Make a copy of dev_name, because '.procname' is regarded as const
14481da177e4SLinus Torvalds 	 * by sysctl and we wouldn't want anyone to change it under our feet
14491da177e4SLinus Torvalds 	 * (see SIOCSIFNAME).
14501da177e4SLinus Torvalds 	 */
14511da177e4SLinus Torvalds 	dev_name = net_sysctl_strdup(dev_name);
14521da177e4SLinus Torvalds 	if (!dev_name)
14531da177e4SLinus Torvalds 	    goto free;
14541da177e4SLinus Torvalds 
14551da177e4SLinus Torvalds 	t->devinet_dev[0].procname    = dev_name;
14561da177e4SLinus Torvalds 	t->devinet_dev[0].child	      = t->devinet_vars;
14571da177e4SLinus Torvalds 	t->devinet_dev[0].de	      = NULL;
14581da177e4SLinus Torvalds 	t->devinet_conf_dir[0].child  = t->devinet_dev;
14591da177e4SLinus Torvalds 	t->devinet_conf_dir[0].de     = NULL;
14601da177e4SLinus Torvalds 	t->devinet_proto_dir[0].child = t->devinet_conf_dir;
14611da177e4SLinus Torvalds 	t->devinet_proto_dir[0].de    = NULL;
14621da177e4SLinus Torvalds 	t->devinet_root_dir[0].child  = t->devinet_proto_dir;
14631da177e4SLinus Torvalds 	t->devinet_root_dir[0].de     = NULL;
14641da177e4SLinus Torvalds 
14651da177e4SLinus Torvalds 	t->sysctl_header = register_sysctl_table(t->devinet_root_dir, 0);
14661da177e4SLinus Torvalds 	if (!t->sysctl_header)
14671da177e4SLinus Torvalds 	    goto free_procname;
14681da177e4SLinus Torvalds 
14691da177e4SLinus Torvalds 	p->sysctl = t;
14701da177e4SLinus Torvalds 	return;
14711da177e4SLinus Torvalds 
14721da177e4SLinus Torvalds 	/* error path */
14731da177e4SLinus Torvalds  free_procname:
14741da177e4SLinus Torvalds 	kfree(dev_name);
14751da177e4SLinus Torvalds  free:
14761da177e4SLinus Torvalds 	kfree(t);
14771da177e4SLinus Torvalds 	return;
14781da177e4SLinus Torvalds }
14791da177e4SLinus Torvalds 
14801da177e4SLinus Torvalds static void devinet_sysctl_unregister(struct ipv4_devconf *p)
14811da177e4SLinus Torvalds {
14821da177e4SLinus Torvalds 	if (p->sysctl) {
14831da177e4SLinus Torvalds 		struct devinet_sysctl_table *t = p->sysctl;
14841da177e4SLinus Torvalds 		p->sysctl = NULL;
14851da177e4SLinus Torvalds 		unregister_sysctl_table(t->sysctl_header);
14861da177e4SLinus Torvalds 		kfree(t->devinet_dev[0].procname);
14871da177e4SLinus Torvalds 		kfree(t);
14881da177e4SLinus Torvalds 	}
14891da177e4SLinus Torvalds }
14901da177e4SLinus Torvalds #endif
14911da177e4SLinus Torvalds 
14921da177e4SLinus Torvalds void __init devinet_init(void)
14931da177e4SLinus Torvalds {
14941da177e4SLinus Torvalds 	register_gifconf(PF_INET, inet_gifconf);
14951da177e4SLinus Torvalds 	register_netdevice_notifier(&ip_netdev_notifier);
14961da177e4SLinus Torvalds 	rtnetlink_links[PF_INET] = inet_rtnetlink_table;
14971da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
14981da177e4SLinus Torvalds 	devinet_sysctl.sysctl_header =
14991da177e4SLinus Torvalds 		register_sysctl_table(devinet_sysctl.devinet_root_dir, 0);
15001da177e4SLinus Torvalds 	devinet_sysctl_register(NULL, &ipv4_devconf_dflt);
15011da177e4SLinus Torvalds #endif
15021da177e4SLinus Torvalds }
15031da177e4SLinus Torvalds 
15041da177e4SLinus Torvalds EXPORT_SYMBOL(devinet_ioctl);
15051da177e4SLinus Torvalds EXPORT_SYMBOL(in_dev_finish_destroy);
15061da177e4SLinus Torvalds EXPORT_SYMBOL(inet_select_addr);
15071da177e4SLinus Torvalds EXPORT_SYMBOL(inetdev_by_index);
15081da177e4SLinus Torvalds EXPORT_SYMBOL(register_inetaddr_notifier);
15091da177e4SLinus Torvalds EXPORT_SYMBOL(unregister_inetaddr_notifier);
1510