xref: /linux/net/ipv4/devinet.c (revision 75c78500ddad74b229cd0691496b8549490496a2)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *	NET3	IP device support routines.
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *		This program is free software; you can redistribute it and/or
51da177e4SLinus Torvalds  *		modify it under the terms of the GNU General Public License
61da177e4SLinus Torvalds  *		as published by the Free Software Foundation; either version
71da177e4SLinus Torvalds  *		2 of the License, or (at your option) any later version.
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  *	Derived from the IP parts of dev.c 1.0.19
1002c30a84SJesper Juhl  * 		Authors:	Ross Biro
111da177e4SLinus Torvalds  *				Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
121da177e4SLinus Torvalds  *				Mark Evans, <evansmp@uhura.aston.ac.uk>
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  *	Additional Authors:
151da177e4SLinus Torvalds  *		Alan Cox, <gw4pts@gw4pts.ampr.org>
161da177e4SLinus Torvalds  *		Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
171da177e4SLinus Torvalds  *
181da177e4SLinus Torvalds  *	Changes:
191da177e4SLinus Torvalds  *		Alexey Kuznetsov:	pa_* fields are replaced with ifaddr
201da177e4SLinus Torvalds  *					lists.
211da177e4SLinus Torvalds  *		Cyrus Durgin:		updated for kmod
221da177e4SLinus Torvalds  *		Matthias Andree:	in devinet_ioctl, compare label and
231da177e4SLinus Torvalds  *					address (4.4BSD alias style support),
241da177e4SLinus Torvalds  *					fall back to comparing just the label
251da177e4SLinus Torvalds  *					if no match found.
261da177e4SLinus Torvalds  */
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds #include <asm/uaccess.h>
301da177e4SLinus Torvalds #include <asm/system.h>
311da177e4SLinus Torvalds #include <linux/bitops.h>
324fc268d2SRandy Dunlap #include <linux/capability.h>
331da177e4SLinus Torvalds #include <linux/module.h>
341da177e4SLinus Torvalds #include <linux/types.h>
351da177e4SLinus Torvalds #include <linux/kernel.h>
361da177e4SLinus Torvalds #include <linux/string.h>
371da177e4SLinus Torvalds #include <linux/mm.h>
381da177e4SLinus Torvalds #include <linux/socket.h>
391da177e4SLinus Torvalds #include <linux/sockios.h>
401da177e4SLinus Torvalds #include <linux/in.h>
411da177e4SLinus Torvalds #include <linux/errno.h>
421da177e4SLinus Torvalds #include <linux/interrupt.h>
431823730fSThomas Graf #include <linux/if_addr.h>
441da177e4SLinus Torvalds #include <linux/if_ether.h>
451da177e4SLinus Torvalds #include <linux/inet.h>
461da177e4SLinus Torvalds #include <linux/netdevice.h>
471da177e4SLinus Torvalds #include <linux/etherdevice.h>
481da177e4SLinus Torvalds #include <linux/skbuff.h>
491da177e4SLinus Torvalds #include <linux/init.h>
501da177e4SLinus Torvalds #include <linux/notifier.h>
511da177e4SLinus Torvalds #include <linux/inetdevice.h>
521da177e4SLinus Torvalds #include <linux/igmp.h>
531da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
541da177e4SLinus Torvalds #include <linux/sysctl.h>
551da177e4SLinus Torvalds #endif
561da177e4SLinus Torvalds #include <linux/kmod.h>
571da177e4SLinus Torvalds 
5814c85021SArnaldo Carvalho de Melo #include <net/arp.h>
591da177e4SLinus Torvalds #include <net/ip.h>
601da177e4SLinus Torvalds #include <net/route.h>
611da177e4SLinus Torvalds #include <net/ip_fib.h>
6263f3444fSThomas Graf #include <net/rtnetlink.h>
63752d14dcSPavel Emelyanov #include <net/net_namespace.h>
641da177e4SLinus Torvalds 
650027ba84SAdrian Bunk static struct ipv4_devconf ipv4_devconf = {
6642f811b8SHerbert Xu 	.data = {
6742f811b8SHerbert Xu 		[NET_IPV4_CONF_ACCEPT_REDIRECTS - 1] = 1,
6842f811b8SHerbert Xu 		[NET_IPV4_CONF_SEND_REDIRECTS - 1] = 1,
6942f811b8SHerbert Xu 		[NET_IPV4_CONF_SECURE_REDIRECTS - 1] = 1,
7042f811b8SHerbert Xu 		[NET_IPV4_CONF_SHARED_MEDIA - 1] = 1,
7142f811b8SHerbert Xu 	},
721da177e4SLinus Torvalds };
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds static struct ipv4_devconf ipv4_devconf_dflt = {
7542f811b8SHerbert Xu 	.data = {
7642f811b8SHerbert Xu 		[NET_IPV4_CONF_ACCEPT_REDIRECTS - 1] = 1,
7742f811b8SHerbert Xu 		[NET_IPV4_CONF_SEND_REDIRECTS - 1] = 1,
7842f811b8SHerbert Xu 		[NET_IPV4_CONF_SECURE_REDIRECTS - 1] = 1,
7942f811b8SHerbert Xu 		[NET_IPV4_CONF_SHARED_MEDIA - 1] = 1,
8042f811b8SHerbert Xu 		[NET_IPV4_CONF_ACCEPT_SOURCE_ROUTE - 1] = 1,
8142f811b8SHerbert Xu 	},
821da177e4SLinus Torvalds };
831da177e4SLinus Torvalds 
849355bbd6SPavel Emelyanov #define IPV4_DEVCONF_DFLT(net, attr) \
859355bbd6SPavel Emelyanov 	IPV4_DEVCONF((*net->ipv4.devconf_dflt), attr)
8642f811b8SHerbert Xu 
87ef7c79edSPatrick McHardy static const struct nla_policy ifa_ipv4_policy[IFA_MAX+1] = {
885c753978SThomas Graf 	[IFA_LOCAL]     	= { .type = NLA_U32 },
895c753978SThomas Graf 	[IFA_ADDRESS]   	= { .type = NLA_U32 },
905c753978SThomas Graf 	[IFA_BROADCAST] 	= { .type = NLA_U32 },
915176f91eSThomas Graf 	[IFA_LABEL]     	= { .type = NLA_STRING, .len = IFNAMSIZ - 1 },
925c753978SThomas Graf };
935c753978SThomas Graf 
94d6062cbbSThomas Graf static void rtmsg_ifa(int event, struct in_ifaddr *, struct nlmsghdr *, u32);
951da177e4SLinus Torvalds 
96e041c683SAlan Stern static BLOCKING_NOTIFIER_HEAD(inetaddr_chain);
971da177e4SLinus Torvalds static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
981da177e4SLinus Torvalds 			 int destroy);
991da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
10066f27a52SPavel Emelyanov static void devinet_sysctl_register(struct in_device *idev);
10151602b2aSPavel Emelyanov static void devinet_sysctl_unregister(struct in_device *idev);
10251602b2aSPavel Emelyanov #else
10351602b2aSPavel Emelyanov static inline void devinet_sysctl_register(struct in_device *idev)
10451602b2aSPavel Emelyanov {
10551602b2aSPavel Emelyanov }
10651602b2aSPavel Emelyanov static inline void devinet_sysctl_unregister(struct in_device *idev)
10751602b2aSPavel Emelyanov {
10851602b2aSPavel Emelyanov }
1091da177e4SLinus Torvalds #endif
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds /* Locks all the inet devices. */
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds static struct in_ifaddr *inet_alloc_ifa(void)
1141da177e4SLinus Torvalds {
11593adcc80SAlexey Dobriyan 	return kzalloc(sizeof(struct in_ifaddr), GFP_KERNEL);
1161da177e4SLinus Torvalds }
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds static void inet_rcu_free_ifa(struct rcu_head *head)
1191da177e4SLinus Torvalds {
1201da177e4SLinus Torvalds 	struct in_ifaddr *ifa = container_of(head, struct in_ifaddr, rcu_head);
1211da177e4SLinus Torvalds 	if (ifa->ifa_dev)
1221da177e4SLinus Torvalds 		in_dev_put(ifa->ifa_dev);
1231da177e4SLinus Torvalds 	kfree(ifa);
1241da177e4SLinus Torvalds }
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds static inline void inet_free_ifa(struct in_ifaddr *ifa)
1271da177e4SLinus Torvalds {
1281da177e4SLinus Torvalds 	call_rcu(&ifa->rcu_head, inet_rcu_free_ifa);
1291da177e4SLinus Torvalds }
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds void in_dev_finish_destroy(struct in_device *idev)
1321da177e4SLinus Torvalds {
1331da177e4SLinus Torvalds 	struct net_device *dev = idev->dev;
1341da177e4SLinus Torvalds 
135547b792cSIlpo Järvinen 	WARN_ON(idev->ifa_list);
136547b792cSIlpo Järvinen 	WARN_ON(idev->mc_list);
1371da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG
1381da177e4SLinus Torvalds 	printk(KERN_DEBUG "in_dev_finish_destroy: %p=%s\n",
1391da177e4SLinus Torvalds 	       idev, dev ? dev->name : "NIL");
1401da177e4SLinus Torvalds #endif
1411da177e4SLinus Torvalds 	dev_put(dev);
1421da177e4SLinus Torvalds 	if (!idev->dead)
1431da177e4SLinus Torvalds 		printk("Freeing alive in_device %p\n", idev);
1441da177e4SLinus Torvalds 	else {
1451da177e4SLinus Torvalds 		kfree(idev);
1461da177e4SLinus Torvalds 	}
1471da177e4SLinus Torvalds }
1481da177e4SLinus Torvalds 
14971e27da9SHerbert Xu static struct in_device *inetdev_init(struct net_device *dev)
1501da177e4SLinus Torvalds {
1511da177e4SLinus Torvalds 	struct in_device *in_dev;
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds 	ASSERT_RTNL();
1541da177e4SLinus Torvalds 
1550da974f4SPanagiotis Issaris 	in_dev = kzalloc(sizeof(*in_dev), GFP_KERNEL);
1561da177e4SLinus Torvalds 	if (!in_dev)
1571da177e4SLinus Torvalds 		goto out;
158c346dca1SYOSHIFUJI Hideaki 	memcpy(&in_dev->cnf, dev_net(dev)->ipv4.devconf_dflt,
1599355bbd6SPavel Emelyanov 			sizeof(in_dev->cnf));
1601da177e4SLinus Torvalds 	in_dev->cnf.sysctl = NULL;
1611da177e4SLinus Torvalds 	in_dev->dev = dev;
1621da177e4SLinus Torvalds 	if ((in_dev->arp_parms = neigh_parms_alloc(dev, &arp_tbl)) == NULL)
1631da177e4SLinus Torvalds 		goto out_kfree;
1640187bdfbSBen Hutchings 	if (IPV4_DEVCONF(in_dev->cnf, FORWARDING))
1650187bdfbSBen Hutchings 		dev_disable_lro(dev);
1661da177e4SLinus Torvalds 	/* Reference in_dev->dev */
1671da177e4SLinus Torvalds 	dev_hold(dev);
16830c4cf57SDavid L Stevens 	/* Account for reference dev->ip_ptr (below) */
1691da177e4SLinus Torvalds 	in_dev_hold(in_dev);
1701da177e4SLinus Torvalds 
17166f27a52SPavel Emelyanov 	devinet_sysctl_register(in_dev);
1721da177e4SLinus Torvalds 	ip_mc_init_dev(in_dev);
1731da177e4SLinus Torvalds 	if (dev->flags & IFF_UP)
1741da177e4SLinus Torvalds 		ip_mc_up(in_dev);
175483479ecSJarek Poplawski 
17630c4cf57SDavid L Stevens 	/* we can receive as soon as ip_ptr is set -- do this last */
17730c4cf57SDavid L Stevens 	rcu_assign_pointer(dev->ip_ptr, in_dev);
178483479ecSJarek Poplawski out:
1791da177e4SLinus Torvalds 	return in_dev;
1801da177e4SLinus Torvalds out_kfree:
1811da177e4SLinus Torvalds 	kfree(in_dev);
1821da177e4SLinus Torvalds 	in_dev = NULL;
1831da177e4SLinus Torvalds 	goto out;
1841da177e4SLinus Torvalds }
1851da177e4SLinus Torvalds 
1861da177e4SLinus Torvalds static void in_dev_rcu_put(struct rcu_head *head)
1871da177e4SLinus Torvalds {
1881da177e4SLinus Torvalds 	struct in_device *idev = container_of(head, struct in_device, rcu_head);
1891da177e4SLinus Torvalds 	in_dev_put(idev);
1901da177e4SLinus Torvalds }
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds static void inetdev_destroy(struct in_device *in_dev)
1931da177e4SLinus Torvalds {
1941da177e4SLinus Torvalds 	struct in_ifaddr *ifa;
1951da177e4SLinus Torvalds 	struct net_device *dev;
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds 	ASSERT_RTNL();
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds 	dev = in_dev->dev;
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds 	in_dev->dead = 1;
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds 	ip_mc_destroy_dev(in_dev);
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds 	while ((ifa = in_dev->ifa_list) != NULL) {
2061da177e4SLinus Torvalds 		inet_del_ifa(in_dev, &in_dev->ifa_list, 0);
2071da177e4SLinus Torvalds 		inet_free_ifa(ifa);
2081da177e4SLinus Torvalds 	}
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds 	dev->ip_ptr = NULL;
2111da177e4SLinus Torvalds 
21251602b2aSPavel Emelyanov 	devinet_sysctl_unregister(in_dev);
2131da177e4SLinus Torvalds 	neigh_parms_release(&arp_tbl, in_dev->arp_parms);
2141da177e4SLinus Torvalds 	arp_ifdown(dev);
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds 	call_rcu(&in_dev->rcu_head, in_dev_rcu_put);
2171da177e4SLinus Torvalds }
2181da177e4SLinus Torvalds 
219ff428d72SAl Viro int inet_addr_onlink(struct in_device *in_dev, __be32 a, __be32 b)
2201da177e4SLinus Torvalds {
2211da177e4SLinus Torvalds 	rcu_read_lock();
2221da177e4SLinus Torvalds 	for_primary_ifa(in_dev) {
2231da177e4SLinus Torvalds 		if (inet_ifa_match(a, ifa)) {
2241da177e4SLinus Torvalds 			if (!b || inet_ifa_match(b, ifa)) {
2251da177e4SLinus Torvalds 				rcu_read_unlock();
2261da177e4SLinus Torvalds 				return 1;
2271da177e4SLinus Torvalds 			}
2281da177e4SLinus Torvalds 		}
2291da177e4SLinus Torvalds 	} endfor_ifa(in_dev);
2301da177e4SLinus Torvalds 	rcu_read_unlock();
2311da177e4SLinus Torvalds 	return 0;
2321da177e4SLinus Torvalds }
2331da177e4SLinus Torvalds 
234d6062cbbSThomas Graf static void __inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
235d6062cbbSThomas Graf 			 int destroy, struct nlmsghdr *nlh, u32 pid)
2361da177e4SLinus Torvalds {
2378f937c60SHarald Welte 	struct in_ifaddr *promote = NULL;
2380ff60a45SJamal Hadi Salim 	struct in_ifaddr *ifa, *ifa1 = *ifap;
2390ff60a45SJamal Hadi Salim 	struct in_ifaddr *last_prim = in_dev->ifa_list;
2400ff60a45SJamal Hadi Salim 	struct in_ifaddr *prev_prom = NULL;
2410ff60a45SJamal Hadi Salim 	int do_promote = IN_DEV_PROMOTE_SECONDARIES(in_dev);
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds 	ASSERT_RTNL();
2441da177e4SLinus Torvalds 
2458f937c60SHarald Welte 	/* 1. Deleting primary ifaddr forces deletion all secondaries
2468f937c60SHarald Welte 	 * unless alias promotion is set
2478f937c60SHarald Welte 	 **/
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds 	if (!(ifa1->ifa_flags & IFA_F_SECONDARY)) {
2501da177e4SLinus Torvalds 		struct in_ifaddr **ifap1 = &ifa1->ifa_next;
2511da177e4SLinus Torvalds 
2521da177e4SLinus Torvalds 		while ((ifa = *ifap1) != NULL) {
2530ff60a45SJamal Hadi Salim 			if (!(ifa->ifa_flags & IFA_F_SECONDARY) &&
2540ff60a45SJamal Hadi Salim 			    ifa1->ifa_scope <= ifa->ifa_scope)
2550ff60a45SJamal Hadi Salim 				last_prim = ifa;
2560ff60a45SJamal Hadi Salim 
2571da177e4SLinus Torvalds 			if (!(ifa->ifa_flags & IFA_F_SECONDARY) ||
2581da177e4SLinus Torvalds 			    ifa1->ifa_mask != ifa->ifa_mask ||
2591da177e4SLinus Torvalds 			    !inet_ifa_match(ifa1->ifa_address, ifa)) {
2601da177e4SLinus Torvalds 				ifap1 = &ifa->ifa_next;
2610ff60a45SJamal Hadi Salim 				prev_prom = ifa;
2621da177e4SLinus Torvalds 				continue;
2631da177e4SLinus Torvalds 			}
2641da177e4SLinus Torvalds 
2650ff60a45SJamal Hadi Salim 			if (!do_promote) {
2661da177e4SLinus Torvalds 				*ifap1 = ifa->ifa_next;
2671da177e4SLinus Torvalds 
268d6062cbbSThomas Graf 				rtmsg_ifa(RTM_DELADDR, ifa, nlh, pid);
269e041c683SAlan Stern 				blocking_notifier_call_chain(&inetaddr_chain,
270e041c683SAlan Stern 						NETDEV_DOWN, ifa);
2711da177e4SLinus Torvalds 				inet_free_ifa(ifa);
2728f937c60SHarald Welte 			} else {
2738f937c60SHarald Welte 				promote = ifa;
2748f937c60SHarald Welte 				break;
2758f937c60SHarald Welte 			}
2761da177e4SLinus Torvalds 		}
2771da177e4SLinus Torvalds 	}
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds 	/* 2. Unlink it */
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds 	*ifap = ifa1->ifa_next;
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds 	/* 3. Announce address deletion */
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds 	/* Send message first, then call notifier.
2861da177e4SLinus Torvalds 	   At first sight, FIB update triggered by notifier
2871da177e4SLinus Torvalds 	   will refer to already deleted ifaddr, that could confuse
2881da177e4SLinus Torvalds 	   netlink listeners. It is not true: look, gated sees
2891da177e4SLinus Torvalds 	   that route deleted and if it still thinks that ifaddr
2901da177e4SLinus Torvalds 	   is valid, it will try to restore deleted routes... Grr.
2911da177e4SLinus Torvalds 	   So that, this order is correct.
2921da177e4SLinus Torvalds 	 */
293d6062cbbSThomas Graf 	rtmsg_ifa(RTM_DELADDR, ifa1, nlh, pid);
294e041c683SAlan Stern 	blocking_notifier_call_chain(&inetaddr_chain, NETDEV_DOWN, ifa1);
2950ff60a45SJamal Hadi Salim 
2960ff60a45SJamal Hadi Salim 	if (promote) {
2970ff60a45SJamal Hadi Salim 
2980ff60a45SJamal Hadi Salim 		if (prev_prom) {
2990ff60a45SJamal Hadi Salim 			prev_prom->ifa_next = promote->ifa_next;
3000ff60a45SJamal Hadi Salim 			promote->ifa_next = last_prim->ifa_next;
3010ff60a45SJamal Hadi Salim 			last_prim->ifa_next = promote;
3020ff60a45SJamal Hadi Salim 		}
3030ff60a45SJamal Hadi Salim 
3040ff60a45SJamal Hadi Salim 		promote->ifa_flags &= ~IFA_F_SECONDARY;
305d6062cbbSThomas Graf 		rtmsg_ifa(RTM_NEWADDR, promote, nlh, pid);
306e041c683SAlan Stern 		blocking_notifier_call_chain(&inetaddr_chain,
307e041c683SAlan Stern 				NETDEV_UP, promote);
3080ff60a45SJamal Hadi Salim 		for (ifa = promote->ifa_next; ifa; ifa = ifa->ifa_next) {
3090ff60a45SJamal Hadi Salim 			if (ifa1->ifa_mask != ifa->ifa_mask ||
3100ff60a45SJamal Hadi Salim 			    !inet_ifa_match(ifa1->ifa_address, ifa))
3110ff60a45SJamal Hadi Salim 					continue;
3120ff60a45SJamal Hadi Salim 			fib_add_ifaddr(ifa);
3130ff60a45SJamal Hadi Salim 		}
3140ff60a45SJamal Hadi Salim 
3150ff60a45SJamal Hadi Salim 	}
3166363097cSHerbert Xu 	if (destroy)
3171da177e4SLinus Torvalds 		inet_free_ifa(ifa1);
3181da177e4SLinus Torvalds }
3191da177e4SLinus Torvalds 
320d6062cbbSThomas Graf static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
321d6062cbbSThomas Graf 			 int destroy)
322d6062cbbSThomas Graf {
323d6062cbbSThomas Graf 	__inet_del_ifa(in_dev, ifap, destroy, NULL, 0);
324d6062cbbSThomas Graf }
325d6062cbbSThomas Graf 
326d6062cbbSThomas Graf static int __inet_insert_ifa(struct in_ifaddr *ifa, struct nlmsghdr *nlh,
327d6062cbbSThomas Graf 			     u32 pid)
3281da177e4SLinus Torvalds {
3291da177e4SLinus Torvalds 	struct in_device *in_dev = ifa->ifa_dev;
3301da177e4SLinus Torvalds 	struct in_ifaddr *ifa1, **ifap, **last_primary;
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds 	ASSERT_RTNL();
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds 	if (!ifa->ifa_local) {
3351da177e4SLinus Torvalds 		inet_free_ifa(ifa);
3361da177e4SLinus Torvalds 		return 0;
3371da177e4SLinus Torvalds 	}
3381da177e4SLinus Torvalds 
3391da177e4SLinus Torvalds 	ifa->ifa_flags &= ~IFA_F_SECONDARY;
3401da177e4SLinus Torvalds 	last_primary = &in_dev->ifa_list;
3411da177e4SLinus Torvalds 
3421da177e4SLinus Torvalds 	for (ifap = &in_dev->ifa_list; (ifa1 = *ifap) != NULL;
3431da177e4SLinus Torvalds 	     ifap = &ifa1->ifa_next) {
3441da177e4SLinus Torvalds 		if (!(ifa1->ifa_flags & IFA_F_SECONDARY) &&
3451da177e4SLinus Torvalds 		    ifa->ifa_scope <= ifa1->ifa_scope)
3461da177e4SLinus Torvalds 			last_primary = &ifa1->ifa_next;
3471da177e4SLinus Torvalds 		if (ifa1->ifa_mask == ifa->ifa_mask &&
3481da177e4SLinus Torvalds 		    inet_ifa_match(ifa1->ifa_address, ifa)) {
3491da177e4SLinus Torvalds 			if (ifa1->ifa_local == ifa->ifa_local) {
3501da177e4SLinus Torvalds 				inet_free_ifa(ifa);
3511da177e4SLinus Torvalds 				return -EEXIST;
3521da177e4SLinus Torvalds 			}
3531da177e4SLinus Torvalds 			if (ifa1->ifa_scope != ifa->ifa_scope) {
3541da177e4SLinus Torvalds 				inet_free_ifa(ifa);
3551da177e4SLinus Torvalds 				return -EINVAL;
3561da177e4SLinus Torvalds 			}
3571da177e4SLinus Torvalds 			ifa->ifa_flags |= IFA_F_SECONDARY;
3581da177e4SLinus Torvalds 		}
3591da177e4SLinus Torvalds 	}
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds 	if (!(ifa->ifa_flags & IFA_F_SECONDARY)) {
3621da177e4SLinus Torvalds 		net_srandom(ifa->ifa_local);
3631da177e4SLinus Torvalds 		ifap = last_primary;
3641da177e4SLinus Torvalds 	}
3651da177e4SLinus Torvalds 
3661da177e4SLinus Torvalds 	ifa->ifa_next = *ifap;
3671da177e4SLinus Torvalds 	*ifap = ifa;
3681da177e4SLinus Torvalds 
3691da177e4SLinus Torvalds 	/* Send message first, then call notifier.
3701da177e4SLinus Torvalds 	   Notifier will trigger FIB update, so that
3711da177e4SLinus Torvalds 	   listeners of netlink will know about new ifaddr */
372d6062cbbSThomas Graf 	rtmsg_ifa(RTM_NEWADDR, ifa, nlh, pid);
373e041c683SAlan Stern 	blocking_notifier_call_chain(&inetaddr_chain, NETDEV_UP, ifa);
3741da177e4SLinus Torvalds 
3751da177e4SLinus Torvalds 	return 0;
3761da177e4SLinus Torvalds }
3771da177e4SLinus Torvalds 
378d6062cbbSThomas Graf static int inet_insert_ifa(struct in_ifaddr *ifa)
379d6062cbbSThomas Graf {
380d6062cbbSThomas Graf 	return __inet_insert_ifa(ifa, NULL, 0);
381d6062cbbSThomas Graf }
382d6062cbbSThomas Graf 
3831da177e4SLinus Torvalds static int inet_set_ifa(struct net_device *dev, struct in_ifaddr *ifa)
3841da177e4SLinus Torvalds {
385e5ed6399SHerbert Xu 	struct in_device *in_dev = __in_dev_get_rtnl(dev);
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds 	ASSERT_RTNL();
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds 	if (!in_dev) {
3901da177e4SLinus Torvalds 		inet_free_ifa(ifa);
3911da177e4SLinus Torvalds 		return -ENOBUFS;
3921da177e4SLinus Torvalds 	}
39371e27da9SHerbert Xu 	ipv4_devconf_setall(in_dev);
3941da177e4SLinus Torvalds 	if (ifa->ifa_dev != in_dev) {
395547b792cSIlpo Järvinen 		WARN_ON(ifa->ifa_dev);
3961da177e4SLinus Torvalds 		in_dev_hold(in_dev);
3971da177e4SLinus Torvalds 		ifa->ifa_dev = in_dev;
3981da177e4SLinus Torvalds 	}
399f97c1e0cSJoe Perches 	if (ipv4_is_loopback(ifa->ifa_local))
4001da177e4SLinus Torvalds 		ifa->ifa_scope = RT_SCOPE_HOST;
4011da177e4SLinus Torvalds 	return inet_insert_ifa(ifa);
4021da177e4SLinus Torvalds }
4031da177e4SLinus Torvalds 
4047fee0ca2SDenis V. Lunev struct in_device *inetdev_by_index(struct net *net, int ifindex)
4051da177e4SLinus Torvalds {
4061da177e4SLinus Torvalds 	struct net_device *dev;
4071da177e4SLinus Torvalds 	struct in_device *in_dev = NULL;
4081da177e4SLinus Torvalds 	read_lock(&dev_base_lock);
4097fee0ca2SDenis V. Lunev 	dev = __dev_get_by_index(net, ifindex);
4101da177e4SLinus Torvalds 	if (dev)
4111da177e4SLinus Torvalds 		in_dev = in_dev_get(dev);
4121da177e4SLinus Torvalds 	read_unlock(&dev_base_lock);
4131da177e4SLinus Torvalds 	return in_dev;
4141da177e4SLinus Torvalds }
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds /* Called only from RTNL semaphored context. No locks. */
4171da177e4SLinus Torvalds 
41860cad5daSAl Viro struct in_ifaddr *inet_ifa_byprefix(struct in_device *in_dev, __be32 prefix,
41960cad5daSAl Viro 				    __be32 mask)
4201da177e4SLinus Torvalds {
4211da177e4SLinus Torvalds 	ASSERT_RTNL();
4221da177e4SLinus Torvalds 
4231da177e4SLinus Torvalds 	for_primary_ifa(in_dev) {
4241da177e4SLinus Torvalds 		if (ifa->ifa_mask == mask && inet_ifa_match(prefix, ifa))
4251da177e4SLinus Torvalds 			return ifa;
4261da177e4SLinus Torvalds 	} endfor_ifa(in_dev);
4271da177e4SLinus Torvalds 	return NULL;
4281da177e4SLinus Torvalds }
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds static int inet_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
4311da177e4SLinus Torvalds {
4323b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
433dfdd5fd4SThomas Graf 	struct nlattr *tb[IFA_MAX+1];
4341da177e4SLinus Torvalds 	struct in_device *in_dev;
435dfdd5fd4SThomas Graf 	struct ifaddrmsg *ifm;
4361da177e4SLinus Torvalds 	struct in_ifaddr *ifa, **ifap;
437dfdd5fd4SThomas Graf 	int err = -EINVAL;
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds 	ASSERT_RTNL();
4401da177e4SLinus Torvalds 
441dfdd5fd4SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv4_policy);
442dfdd5fd4SThomas Graf 	if (err < 0)
443dfdd5fd4SThomas Graf 		goto errout;
444dfdd5fd4SThomas Graf 
445dfdd5fd4SThomas Graf 	ifm = nlmsg_data(nlh);
4467fee0ca2SDenis V. Lunev 	in_dev = inetdev_by_index(net, ifm->ifa_index);
447dfdd5fd4SThomas Graf 	if (in_dev == NULL) {
448dfdd5fd4SThomas Graf 		err = -ENODEV;
449dfdd5fd4SThomas Graf 		goto errout;
450dfdd5fd4SThomas Graf 	}
451dfdd5fd4SThomas Graf 
4521da177e4SLinus Torvalds 	__in_dev_put(in_dev);
4531da177e4SLinus Torvalds 
4541da177e4SLinus Torvalds 	for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
4551da177e4SLinus Torvalds 	     ifap = &ifa->ifa_next) {
456dfdd5fd4SThomas Graf 		if (tb[IFA_LOCAL] &&
457a7a628c4SAl Viro 		    ifa->ifa_local != nla_get_be32(tb[IFA_LOCAL]))
4581da177e4SLinus Torvalds 			continue;
459dfdd5fd4SThomas Graf 
460dfdd5fd4SThomas Graf 		if (tb[IFA_LABEL] && nla_strcmp(tb[IFA_LABEL], ifa->ifa_label))
461dfdd5fd4SThomas Graf 			continue;
462dfdd5fd4SThomas Graf 
463dfdd5fd4SThomas Graf 		if (tb[IFA_ADDRESS] &&
464dfdd5fd4SThomas Graf 		    (ifm->ifa_prefixlen != ifa->ifa_prefixlen ||
465a7a628c4SAl Viro 		    !inet_ifa_match(nla_get_be32(tb[IFA_ADDRESS]), ifa)))
466dfdd5fd4SThomas Graf 			continue;
467dfdd5fd4SThomas Graf 
468d6062cbbSThomas Graf 		__inet_del_ifa(in_dev, ifap, 1, nlh, NETLINK_CB(skb).pid);
4691da177e4SLinus Torvalds 		return 0;
4701da177e4SLinus Torvalds 	}
471dfdd5fd4SThomas Graf 
472dfdd5fd4SThomas Graf 	err = -EADDRNOTAVAIL;
473dfdd5fd4SThomas Graf errout:
474dfdd5fd4SThomas Graf 	return err;
4751da177e4SLinus Torvalds }
4761da177e4SLinus Torvalds 
4774b8aa9abSDenis V. Lunev static struct in_ifaddr *rtm_to_ifaddr(struct net *net, struct nlmsghdr *nlh)
4781da177e4SLinus Torvalds {
4795c753978SThomas Graf 	struct nlattr *tb[IFA_MAX+1];
4805c753978SThomas Graf 	struct in_ifaddr *ifa;
4815c753978SThomas Graf 	struct ifaddrmsg *ifm;
4821da177e4SLinus Torvalds 	struct net_device *dev;
4831da177e4SLinus Torvalds 	struct in_device *in_dev;
4847b218574SDenis V. Lunev 	int err;
4851da177e4SLinus Torvalds 
4865c753978SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv4_policy);
4875c753978SThomas Graf 	if (err < 0)
4885c753978SThomas Graf 		goto errout;
4891da177e4SLinus Torvalds 
4905c753978SThomas Graf 	ifm = nlmsg_data(nlh);
491c4e38f41SEvgeniy Polyakov 	err = -EINVAL;
4927b218574SDenis V. Lunev 	if (ifm->ifa_prefixlen > 32 || tb[IFA_LOCAL] == NULL)
4935c753978SThomas Graf 		goto errout;
4941da177e4SLinus Torvalds 
4954b8aa9abSDenis V. Lunev 	dev = __dev_get_by_index(net, ifm->ifa_index);
4965c753978SThomas Graf 	err = -ENODEV;
4977b218574SDenis V. Lunev 	if (dev == NULL)
4985c753978SThomas Graf 		goto errout;
4991da177e4SLinus Torvalds 
5005c753978SThomas Graf 	in_dev = __in_dev_get_rtnl(dev);
5015c753978SThomas Graf 	err = -ENOBUFS;
5027b218574SDenis V. Lunev 	if (in_dev == NULL)
5035c753978SThomas Graf 		goto errout;
50471e27da9SHerbert Xu 
5055c753978SThomas Graf 	ifa = inet_alloc_ifa();
5067b218574SDenis V. Lunev 	if (ifa == NULL)
5075c753978SThomas Graf 		/*
5085c753978SThomas Graf 		 * A potential indev allocation can be left alive, it stays
5095c753978SThomas Graf 		 * assigned to its device and is destroy with it.
5105c753978SThomas Graf 		 */
5115c753978SThomas Graf 		goto errout;
5125c753978SThomas Graf 
513a4e65d36SPavel Emelyanov 	ipv4_devconf_setall(in_dev);
5145c753978SThomas Graf 	in_dev_hold(in_dev);
5155c753978SThomas Graf 
5165c753978SThomas Graf 	if (tb[IFA_ADDRESS] == NULL)
5175c753978SThomas Graf 		tb[IFA_ADDRESS] = tb[IFA_LOCAL];
5185c753978SThomas Graf 
5191da177e4SLinus Torvalds 	ifa->ifa_prefixlen = ifm->ifa_prefixlen;
5201da177e4SLinus Torvalds 	ifa->ifa_mask = inet_make_mask(ifm->ifa_prefixlen);
5211da177e4SLinus Torvalds 	ifa->ifa_flags = ifm->ifa_flags;
5221da177e4SLinus Torvalds 	ifa->ifa_scope = ifm->ifa_scope;
5231da177e4SLinus Torvalds 	ifa->ifa_dev = in_dev;
5245c753978SThomas Graf 
525a7a628c4SAl Viro 	ifa->ifa_local = nla_get_be32(tb[IFA_LOCAL]);
526a7a628c4SAl Viro 	ifa->ifa_address = nla_get_be32(tb[IFA_ADDRESS]);
5275c753978SThomas Graf 
5285c753978SThomas Graf 	if (tb[IFA_BROADCAST])
529a7a628c4SAl Viro 		ifa->ifa_broadcast = nla_get_be32(tb[IFA_BROADCAST]);
5305c753978SThomas Graf 
5315c753978SThomas Graf 	if (tb[IFA_LABEL])
5325c753978SThomas Graf 		nla_strlcpy(ifa->ifa_label, tb[IFA_LABEL], IFNAMSIZ);
5331da177e4SLinus Torvalds 	else
5341da177e4SLinus Torvalds 		memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
5351da177e4SLinus Torvalds 
5365c753978SThomas Graf 	return ifa;
5375c753978SThomas Graf 
5385c753978SThomas Graf errout:
5395c753978SThomas Graf 	return ERR_PTR(err);
5405c753978SThomas Graf }
5415c753978SThomas Graf 
5425c753978SThomas Graf static int inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
5435c753978SThomas Graf {
5443b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
5455c753978SThomas Graf 	struct in_ifaddr *ifa;
5465c753978SThomas Graf 
5475c753978SThomas Graf 	ASSERT_RTNL();
5485c753978SThomas Graf 
5494b8aa9abSDenis V. Lunev 	ifa = rtm_to_ifaddr(net, nlh);
5505c753978SThomas Graf 	if (IS_ERR(ifa))
5515c753978SThomas Graf 		return PTR_ERR(ifa);
5525c753978SThomas Graf 
553d6062cbbSThomas Graf 	return __inet_insert_ifa(ifa, nlh, NETLINK_CB(skb).pid);
5541da177e4SLinus Torvalds }
5551da177e4SLinus Torvalds 
5561da177e4SLinus Torvalds /*
5571da177e4SLinus Torvalds  *	Determine a default network mask, based on the IP address.
5581da177e4SLinus Torvalds  */
5591da177e4SLinus Torvalds 
560714e85beSAl Viro static __inline__ int inet_abc_len(__be32 addr)
5611da177e4SLinus Torvalds {
5621da177e4SLinus Torvalds 	int rc = -1;	/* Something else, probably a multicast. */
5631da177e4SLinus Torvalds 
564f97c1e0cSJoe Perches 	if (ipv4_is_zeronet(addr))
5651da177e4SLinus Torvalds 		rc = 0;
5661da177e4SLinus Torvalds 	else {
567714e85beSAl Viro 		__u32 haddr = ntohl(addr);
5681da177e4SLinus Torvalds 
569714e85beSAl Viro 		if (IN_CLASSA(haddr))
5701da177e4SLinus Torvalds 			rc = 8;
571714e85beSAl Viro 		else if (IN_CLASSB(haddr))
5721da177e4SLinus Torvalds 			rc = 16;
573714e85beSAl Viro 		else if (IN_CLASSC(haddr))
5741da177e4SLinus Torvalds 			rc = 24;
5751da177e4SLinus Torvalds 	}
5761da177e4SLinus Torvalds 
5771da177e4SLinus Torvalds 	return rc;
5781da177e4SLinus Torvalds }
5791da177e4SLinus Torvalds 
5801da177e4SLinus Torvalds 
581e5b13cb1SDenis V. Lunev int devinet_ioctl(struct net *net, unsigned int cmd, void __user *arg)
5821da177e4SLinus Torvalds {
5831da177e4SLinus Torvalds 	struct ifreq ifr;
5841da177e4SLinus Torvalds 	struct sockaddr_in sin_orig;
5851da177e4SLinus Torvalds 	struct sockaddr_in *sin = (struct sockaddr_in *)&ifr.ifr_addr;
5861da177e4SLinus Torvalds 	struct in_device *in_dev;
5871da177e4SLinus Torvalds 	struct in_ifaddr **ifap = NULL;
5881da177e4SLinus Torvalds 	struct in_ifaddr *ifa = NULL;
5891da177e4SLinus Torvalds 	struct net_device *dev;
5901da177e4SLinus Torvalds 	char *colon;
5911da177e4SLinus Torvalds 	int ret = -EFAULT;
5921da177e4SLinus Torvalds 	int tryaddrmatch = 0;
5931da177e4SLinus Torvalds 
5941da177e4SLinus Torvalds 	/*
5951da177e4SLinus Torvalds 	 *	Fetch the caller's info block into kernel space
5961da177e4SLinus Torvalds 	 */
5971da177e4SLinus Torvalds 
5981da177e4SLinus Torvalds 	if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
5991da177e4SLinus Torvalds 		goto out;
6001da177e4SLinus Torvalds 	ifr.ifr_name[IFNAMSIZ - 1] = 0;
6011da177e4SLinus Torvalds 
6021da177e4SLinus Torvalds 	/* save original address for comparison */
6031da177e4SLinus Torvalds 	memcpy(&sin_orig, sin, sizeof(*sin));
6041da177e4SLinus Torvalds 
6051da177e4SLinus Torvalds 	colon = strchr(ifr.ifr_name, ':');
6061da177e4SLinus Torvalds 	if (colon)
6071da177e4SLinus Torvalds 		*colon = 0;
6081da177e4SLinus Torvalds 
609e5b13cb1SDenis V. Lunev 	dev_load(net, ifr.ifr_name);
6101da177e4SLinus Torvalds 
6111da177e4SLinus Torvalds 	switch (cmd) {
6121da177e4SLinus Torvalds 	case SIOCGIFADDR:	/* Get interface address */
6131da177e4SLinus Torvalds 	case SIOCGIFBRDADDR:	/* Get the broadcast address */
6141da177e4SLinus Torvalds 	case SIOCGIFDSTADDR:	/* Get the destination address */
6151da177e4SLinus Torvalds 	case SIOCGIFNETMASK:	/* Get the netmask for the interface */
6161da177e4SLinus Torvalds 		/* Note that these ioctls will not sleep,
6171da177e4SLinus Torvalds 		   so that we do not impose a lock.
6181da177e4SLinus Torvalds 		   One day we will be forced to put shlock here (I mean SMP)
6191da177e4SLinus Torvalds 		 */
6201da177e4SLinus Torvalds 		tryaddrmatch = (sin_orig.sin_family == AF_INET);
6211da177e4SLinus Torvalds 		memset(sin, 0, sizeof(*sin));
6221da177e4SLinus Torvalds 		sin->sin_family = AF_INET;
6231da177e4SLinus Torvalds 		break;
6241da177e4SLinus Torvalds 
6251da177e4SLinus Torvalds 	case SIOCSIFFLAGS:
6261da177e4SLinus Torvalds 		ret = -EACCES;
6271da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
6281da177e4SLinus Torvalds 			goto out;
6291da177e4SLinus Torvalds 		break;
6301da177e4SLinus Torvalds 	case SIOCSIFADDR:	/* Set interface address (and family) */
6311da177e4SLinus Torvalds 	case SIOCSIFBRDADDR:	/* Set the broadcast address */
6321da177e4SLinus Torvalds 	case SIOCSIFDSTADDR:	/* Set the destination address */
6331da177e4SLinus Torvalds 	case SIOCSIFNETMASK: 	/* Set the netmask for the interface */
6341da177e4SLinus Torvalds 		ret = -EACCES;
6351da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
6361da177e4SLinus Torvalds 			goto out;
6371da177e4SLinus Torvalds 		ret = -EINVAL;
6381da177e4SLinus Torvalds 		if (sin->sin_family != AF_INET)
6391da177e4SLinus Torvalds 			goto out;
6401da177e4SLinus Torvalds 		break;
6411da177e4SLinus Torvalds 	default:
6421da177e4SLinus Torvalds 		ret = -EINVAL;
6431da177e4SLinus Torvalds 		goto out;
6441da177e4SLinus Torvalds 	}
6451da177e4SLinus Torvalds 
6461da177e4SLinus Torvalds 	rtnl_lock();
6471da177e4SLinus Torvalds 
6481da177e4SLinus Torvalds 	ret = -ENODEV;
649e5b13cb1SDenis V. Lunev 	if ((dev = __dev_get_by_name(net, ifr.ifr_name)) == NULL)
6501da177e4SLinus Torvalds 		goto done;
6511da177e4SLinus Torvalds 
6521da177e4SLinus Torvalds 	if (colon)
6531da177e4SLinus Torvalds 		*colon = ':';
6541da177e4SLinus Torvalds 
655e5ed6399SHerbert Xu 	if ((in_dev = __in_dev_get_rtnl(dev)) != NULL) {
6561da177e4SLinus Torvalds 		if (tryaddrmatch) {
6571da177e4SLinus Torvalds 			/* Matthias Andree */
6581da177e4SLinus Torvalds 			/* compare label and address (4.4BSD style) */
6591da177e4SLinus Torvalds 			/* note: we only do this for a limited set of ioctls
6601da177e4SLinus Torvalds 			   and only if the original address family was AF_INET.
6611da177e4SLinus Torvalds 			   This is checked above. */
6621da177e4SLinus Torvalds 			for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
6631da177e4SLinus Torvalds 			     ifap = &ifa->ifa_next) {
6641da177e4SLinus Torvalds 				if (!strcmp(ifr.ifr_name, ifa->ifa_label) &&
6651da177e4SLinus Torvalds 				    sin_orig.sin_addr.s_addr ==
6661da177e4SLinus Torvalds 							ifa->ifa_address) {
6671da177e4SLinus Torvalds 					break; /* found */
6681da177e4SLinus Torvalds 				}
6691da177e4SLinus Torvalds 			}
6701da177e4SLinus Torvalds 		}
6711da177e4SLinus Torvalds 		/* we didn't get a match, maybe the application is
6721da177e4SLinus Torvalds 		   4.3BSD-style and passed in junk so we fall back to
6731da177e4SLinus Torvalds 		   comparing just the label */
6741da177e4SLinus Torvalds 		if (!ifa) {
6751da177e4SLinus Torvalds 			for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
6761da177e4SLinus Torvalds 			     ifap = &ifa->ifa_next)
6771da177e4SLinus Torvalds 				if (!strcmp(ifr.ifr_name, ifa->ifa_label))
6781da177e4SLinus Torvalds 					break;
6791da177e4SLinus Torvalds 		}
6801da177e4SLinus Torvalds 	}
6811da177e4SLinus Torvalds 
6821da177e4SLinus Torvalds 	ret = -EADDRNOTAVAIL;
6831da177e4SLinus Torvalds 	if (!ifa && cmd != SIOCSIFADDR && cmd != SIOCSIFFLAGS)
6841da177e4SLinus Torvalds 		goto done;
6851da177e4SLinus Torvalds 
6861da177e4SLinus Torvalds 	switch (cmd) {
6871da177e4SLinus Torvalds 	case SIOCGIFADDR:	/* Get interface address */
6881da177e4SLinus Torvalds 		sin->sin_addr.s_addr = ifa->ifa_local;
6891da177e4SLinus Torvalds 		goto rarok;
6901da177e4SLinus Torvalds 
6911da177e4SLinus Torvalds 	case SIOCGIFBRDADDR:	/* Get the broadcast address */
6921da177e4SLinus Torvalds 		sin->sin_addr.s_addr = ifa->ifa_broadcast;
6931da177e4SLinus Torvalds 		goto rarok;
6941da177e4SLinus Torvalds 
6951da177e4SLinus Torvalds 	case SIOCGIFDSTADDR:	/* Get the destination address */
6961da177e4SLinus Torvalds 		sin->sin_addr.s_addr = ifa->ifa_address;
6971da177e4SLinus Torvalds 		goto rarok;
6981da177e4SLinus Torvalds 
6991da177e4SLinus Torvalds 	case SIOCGIFNETMASK:	/* Get the netmask for the interface */
7001da177e4SLinus Torvalds 		sin->sin_addr.s_addr = ifa->ifa_mask;
7011da177e4SLinus Torvalds 		goto rarok;
7021da177e4SLinus Torvalds 
7031da177e4SLinus Torvalds 	case SIOCSIFFLAGS:
7041da177e4SLinus Torvalds 		if (colon) {
7051da177e4SLinus Torvalds 			ret = -EADDRNOTAVAIL;
7061da177e4SLinus Torvalds 			if (!ifa)
7071da177e4SLinus Torvalds 				break;
7081da177e4SLinus Torvalds 			ret = 0;
7091da177e4SLinus Torvalds 			if (!(ifr.ifr_flags & IFF_UP))
7101da177e4SLinus Torvalds 				inet_del_ifa(in_dev, ifap, 1);
7111da177e4SLinus Torvalds 			break;
7121da177e4SLinus Torvalds 		}
7131da177e4SLinus Torvalds 		ret = dev_change_flags(dev, ifr.ifr_flags);
7141da177e4SLinus Torvalds 		break;
7151da177e4SLinus Torvalds 
7161da177e4SLinus Torvalds 	case SIOCSIFADDR:	/* Set interface address (and family) */
7171da177e4SLinus Torvalds 		ret = -EINVAL;
7181da177e4SLinus Torvalds 		if (inet_abc_len(sin->sin_addr.s_addr) < 0)
7191da177e4SLinus Torvalds 			break;
7201da177e4SLinus Torvalds 
7211da177e4SLinus Torvalds 		if (!ifa) {
7221da177e4SLinus Torvalds 			ret = -ENOBUFS;
7231da177e4SLinus Torvalds 			if ((ifa = inet_alloc_ifa()) == NULL)
7241da177e4SLinus Torvalds 				break;
7251da177e4SLinus Torvalds 			if (colon)
7261da177e4SLinus Torvalds 				memcpy(ifa->ifa_label, ifr.ifr_name, IFNAMSIZ);
7271da177e4SLinus Torvalds 			else
7281da177e4SLinus Torvalds 				memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
7291da177e4SLinus Torvalds 		} else {
7301da177e4SLinus Torvalds 			ret = 0;
7311da177e4SLinus Torvalds 			if (ifa->ifa_local == sin->sin_addr.s_addr)
7321da177e4SLinus Torvalds 				break;
7331da177e4SLinus Torvalds 			inet_del_ifa(in_dev, ifap, 0);
7341da177e4SLinus Torvalds 			ifa->ifa_broadcast = 0;
735148f9729SBjorn Mork 			ifa->ifa_scope = 0;
7361da177e4SLinus Torvalds 		}
7371da177e4SLinus Torvalds 
7381da177e4SLinus Torvalds 		ifa->ifa_address = ifa->ifa_local = sin->sin_addr.s_addr;
7391da177e4SLinus Torvalds 
7401da177e4SLinus Torvalds 		if (!(dev->flags & IFF_POINTOPOINT)) {
7411da177e4SLinus Torvalds 			ifa->ifa_prefixlen = inet_abc_len(ifa->ifa_address);
7421da177e4SLinus Torvalds 			ifa->ifa_mask = inet_make_mask(ifa->ifa_prefixlen);
7431da177e4SLinus Torvalds 			if ((dev->flags & IFF_BROADCAST) &&
7441da177e4SLinus Torvalds 			    ifa->ifa_prefixlen < 31)
7451da177e4SLinus Torvalds 				ifa->ifa_broadcast = ifa->ifa_address |
7461da177e4SLinus Torvalds 						     ~ifa->ifa_mask;
7471da177e4SLinus Torvalds 		} else {
7481da177e4SLinus Torvalds 			ifa->ifa_prefixlen = 32;
7491da177e4SLinus Torvalds 			ifa->ifa_mask = inet_make_mask(32);
7501da177e4SLinus Torvalds 		}
7511da177e4SLinus Torvalds 		ret = inet_set_ifa(dev, ifa);
7521da177e4SLinus Torvalds 		break;
7531da177e4SLinus Torvalds 
7541da177e4SLinus Torvalds 	case SIOCSIFBRDADDR:	/* Set the broadcast address */
7551da177e4SLinus Torvalds 		ret = 0;
7561da177e4SLinus Torvalds 		if (ifa->ifa_broadcast != sin->sin_addr.s_addr) {
7571da177e4SLinus Torvalds 			inet_del_ifa(in_dev, ifap, 0);
7581da177e4SLinus Torvalds 			ifa->ifa_broadcast = sin->sin_addr.s_addr;
7591da177e4SLinus Torvalds 			inet_insert_ifa(ifa);
7601da177e4SLinus Torvalds 		}
7611da177e4SLinus Torvalds 		break;
7621da177e4SLinus Torvalds 
7631da177e4SLinus Torvalds 	case SIOCSIFDSTADDR:	/* Set the destination address */
7641da177e4SLinus Torvalds 		ret = 0;
7651da177e4SLinus Torvalds 		if (ifa->ifa_address == sin->sin_addr.s_addr)
7661da177e4SLinus Torvalds 			break;
7671da177e4SLinus Torvalds 		ret = -EINVAL;
7681da177e4SLinus Torvalds 		if (inet_abc_len(sin->sin_addr.s_addr) < 0)
7691da177e4SLinus Torvalds 			break;
7701da177e4SLinus Torvalds 		ret = 0;
7711da177e4SLinus Torvalds 		inet_del_ifa(in_dev, ifap, 0);
7721da177e4SLinus Torvalds 		ifa->ifa_address = sin->sin_addr.s_addr;
7731da177e4SLinus Torvalds 		inet_insert_ifa(ifa);
7741da177e4SLinus Torvalds 		break;
7751da177e4SLinus Torvalds 
7761da177e4SLinus Torvalds 	case SIOCSIFNETMASK: 	/* Set the netmask for the interface */
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds 		/*
7791da177e4SLinus Torvalds 		 *	The mask we set must be legal.
7801da177e4SLinus Torvalds 		 */
7811da177e4SLinus Torvalds 		ret = -EINVAL;
7821da177e4SLinus Torvalds 		if (bad_mask(sin->sin_addr.s_addr, 0))
7831da177e4SLinus Torvalds 			break;
7841da177e4SLinus Torvalds 		ret = 0;
7851da177e4SLinus Torvalds 		if (ifa->ifa_mask != sin->sin_addr.s_addr) {
786a144ea4bSAl Viro 			__be32 old_mask = ifa->ifa_mask;
7871da177e4SLinus Torvalds 			inet_del_ifa(in_dev, ifap, 0);
7881da177e4SLinus Torvalds 			ifa->ifa_mask = sin->sin_addr.s_addr;
7891da177e4SLinus Torvalds 			ifa->ifa_prefixlen = inet_mask_len(ifa->ifa_mask);
7901da177e4SLinus Torvalds 
7911da177e4SLinus Torvalds 			/* See if current broadcast address matches
7921da177e4SLinus Torvalds 			 * with current netmask, then recalculate
7931da177e4SLinus Torvalds 			 * the broadcast address. Otherwise it's a
7941da177e4SLinus Torvalds 			 * funny address, so don't touch it since
7951da177e4SLinus Torvalds 			 * the user seems to know what (s)he's doing...
7961da177e4SLinus Torvalds 			 */
7971da177e4SLinus Torvalds 			if ((dev->flags & IFF_BROADCAST) &&
7981da177e4SLinus Torvalds 			    (ifa->ifa_prefixlen < 31) &&
7991da177e4SLinus Torvalds 			    (ifa->ifa_broadcast ==
800dcab5e1eSDavid Engel 			     (ifa->ifa_local|~old_mask))) {
8011da177e4SLinus Torvalds 				ifa->ifa_broadcast = (ifa->ifa_local |
8021da177e4SLinus Torvalds 						      ~sin->sin_addr.s_addr);
8031da177e4SLinus Torvalds 			}
8041da177e4SLinus Torvalds 			inet_insert_ifa(ifa);
8051da177e4SLinus Torvalds 		}
8061da177e4SLinus Torvalds 		break;
8071da177e4SLinus Torvalds 	}
8081da177e4SLinus Torvalds done:
8091da177e4SLinus Torvalds 	rtnl_unlock();
8101da177e4SLinus Torvalds out:
8111da177e4SLinus Torvalds 	return ret;
8121da177e4SLinus Torvalds rarok:
8131da177e4SLinus Torvalds 	rtnl_unlock();
8141da177e4SLinus Torvalds 	ret = copy_to_user(arg, &ifr, sizeof(struct ifreq)) ? -EFAULT : 0;
8151da177e4SLinus Torvalds 	goto out;
8161da177e4SLinus Torvalds }
8171da177e4SLinus Torvalds 
8181da177e4SLinus Torvalds static int inet_gifconf(struct net_device *dev, char __user *buf, int len)
8191da177e4SLinus Torvalds {
820e5ed6399SHerbert Xu 	struct in_device *in_dev = __in_dev_get_rtnl(dev);
8211da177e4SLinus Torvalds 	struct in_ifaddr *ifa;
8221da177e4SLinus Torvalds 	struct ifreq ifr;
8231da177e4SLinus Torvalds 	int done = 0;
8241da177e4SLinus Torvalds 
8251da177e4SLinus Torvalds 	if (!in_dev || (ifa = in_dev->ifa_list) == NULL)
8261da177e4SLinus Torvalds 		goto out;
8271da177e4SLinus Torvalds 
8281da177e4SLinus Torvalds 	for (; ifa; ifa = ifa->ifa_next) {
8291da177e4SLinus Torvalds 		if (!buf) {
8301da177e4SLinus Torvalds 			done += sizeof(ifr);
8311da177e4SLinus Torvalds 			continue;
8321da177e4SLinus Torvalds 		}
8331da177e4SLinus Torvalds 		if (len < (int) sizeof(ifr))
8341da177e4SLinus Torvalds 			break;
8351da177e4SLinus Torvalds 		memset(&ifr, 0, sizeof(struct ifreq));
8361da177e4SLinus Torvalds 		if (ifa->ifa_label)
8371da177e4SLinus Torvalds 			strcpy(ifr.ifr_name, ifa->ifa_label);
8381da177e4SLinus Torvalds 		else
8391da177e4SLinus Torvalds 			strcpy(ifr.ifr_name, dev->name);
8401da177e4SLinus Torvalds 
8411da177e4SLinus Torvalds 		(*(struct sockaddr_in *)&ifr.ifr_addr).sin_family = AF_INET;
8421da177e4SLinus Torvalds 		(*(struct sockaddr_in *)&ifr.ifr_addr).sin_addr.s_addr =
8431da177e4SLinus Torvalds 								ifa->ifa_local;
8441da177e4SLinus Torvalds 
8451da177e4SLinus Torvalds 		if (copy_to_user(buf, &ifr, sizeof(struct ifreq))) {
8461da177e4SLinus Torvalds 			done = -EFAULT;
8471da177e4SLinus Torvalds 			break;
8481da177e4SLinus Torvalds 		}
8491da177e4SLinus Torvalds 		buf  += sizeof(struct ifreq);
8501da177e4SLinus Torvalds 		len  -= sizeof(struct ifreq);
8511da177e4SLinus Torvalds 		done += sizeof(struct ifreq);
8521da177e4SLinus Torvalds 	}
8531da177e4SLinus Torvalds out:
8541da177e4SLinus Torvalds 	return done;
8551da177e4SLinus Torvalds }
8561da177e4SLinus Torvalds 
857a61ced5dSAl Viro __be32 inet_select_addr(const struct net_device *dev, __be32 dst, int scope)
8581da177e4SLinus Torvalds {
859a61ced5dSAl Viro 	__be32 addr = 0;
8601da177e4SLinus Torvalds 	struct in_device *in_dev;
861c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
8621da177e4SLinus Torvalds 
8631da177e4SLinus Torvalds 	rcu_read_lock();
864e5ed6399SHerbert Xu 	in_dev = __in_dev_get_rcu(dev);
8651da177e4SLinus Torvalds 	if (!in_dev)
8661da177e4SLinus Torvalds 		goto no_in_dev;
8671da177e4SLinus Torvalds 
8681da177e4SLinus Torvalds 	for_primary_ifa(in_dev) {
8691da177e4SLinus Torvalds 		if (ifa->ifa_scope > scope)
8701da177e4SLinus Torvalds 			continue;
8711da177e4SLinus Torvalds 		if (!dst || inet_ifa_match(dst, ifa)) {
8721da177e4SLinus Torvalds 			addr = ifa->ifa_local;
8731da177e4SLinus Torvalds 			break;
8741da177e4SLinus Torvalds 		}
8751da177e4SLinus Torvalds 		if (!addr)
8761da177e4SLinus Torvalds 			addr = ifa->ifa_local;
8771da177e4SLinus Torvalds 	} endfor_ifa(in_dev);
8781da177e4SLinus Torvalds no_in_dev:
8791da177e4SLinus Torvalds 	rcu_read_unlock();
8801da177e4SLinus Torvalds 
8811da177e4SLinus Torvalds 	if (addr)
8821da177e4SLinus Torvalds 		goto out;
8831da177e4SLinus Torvalds 
8841da177e4SLinus Torvalds 	/* Not loopback addresses on loopback should be preferred
8851da177e4SLinus Torvalds 	   in this case. It is importnat that lo is the first interface
8861da177e4SLinus Torvalds 	   in dev_base list.
8871da177e4SLinus Torvalds 	 */
8881da177e4SLinus Torvalds 	read_lock(&dev_base_lock);
8891da177e4SLinus Torvalds 	rcu_read_lock();
890c4544c72SDenis V. Lunev 	for_each_netdev(net, dev) {
891e5ed6399SHerbert Xu 		if ((in_dev = __in_dev_get_rcu(dev)) == NULL)
8921da177e4SLinus Torvalds 			continue;
8931da177e4SLinus Torvalds 
8941da177e4SLinus Torvalds 		for_primary_ifa(in_dev) {
8951da177e4SLinus Torvalds 			if (ifa->ifa_scope != RT_SCOPE_LINK &&
8961da177e4SLinus Torvalds 			    ifa->ifa_scope <= scope) {
8971da177e4SLinus Torvalds 				addr = ifa->ifa_local;
8981da177e4SLinus Torvalds 				goto out_unlock_both;
8991da177e4SLinus Torvalds 			}
9001da177e4SLinus Torvalds 		} endfor_ifa(in_dev);
9011da177e4SLinus Torvalds 	}
9021da177e4SLinus Torvalds out_unlock_both:
9031da177e4SLinus Torvalds 	read_unlock(&dev_base_lock);
9041da177e4SLinus Torvalds 	rcu_read_unlock();
9051da177e4SLinus Torvalds out:
9061da177e4SLinus Torvalds 	return addr;
9071da177e4SLinus Torvalds }
9081da177e4SLinus Torvalds 
90960cad5daSAl Viro static __be32 confirm_addr_indev(struct in_device *in_dev, __be32 dst,
91060cad5daSAl Viro 			      __be32 local, int scope)
9111da177e4SLinus Torvalds {
9121da177e4SLinus Torvalds 	int same = 0;
913a144ea4bSAl Viro 	__be32 addr = 0;
9141da177e4SLinus Torvalds 
9151da177e4SLinus Torvalds 	for_ifa(in_dev) {
9161da177e4SLinus Torvalds 		if (!addr &&
9171da177e4SLinus Torvalds 		    (local == ifa->ifa_local || !local) &&
9181da177e4SLinus Torvalds 		    ifa->ifa_scope <= scope) {
9191da177e4SLinus Torvalds 			addr = ifa->ifa_local;
9201da177e4SLinus Torvalds 			if (same)
9211da177e4SLinus Torvalds 				break;
9221da177e4SLinus Torvalds 		}
9231da177e4SLinus Torvalds 		if (!same) {
9241da177e4SLinus Torvalds 			same = (!local || inet_ifa_match(local, ifa)) &&
9251da177e4SLinus Torvalds 				(!dst || inet_ifa_match(dst, ifa));
9261da177e4SLinus Torvalds 			if (same && addr) {
9271da177e4SLinus Torvalds 				if (local || !dst)
9281da177e4SLinus Torvalds 					break;
9291da177e4SLinus Torvalds 				/* Is the selected addr into dst subnet? */
9301da177e4SLinus Torvalds 				if (inet_ifa_match(addr, ifa))
9311da177e4SLinus Torvalds 					break;
9321da177e4SLinus Torvalds 				/* No, then can we use new local src? */
9331da177e4SLinus Torvalds 				if (ifa->ifa_scope <= scope) {
9341da177e4SLinus Torvalds 					addr = ifa->ifa_local;
9351da177e4SLinus Torvalds 					break;
9361da177e4SLinus Torvalds 				}
9371da177e4SLinus Torvalds 				/* search for large dst subnet for addr */
9381da177e4SLinus Torvalds 				same = 0;
9391da177e4SLinus Torvalds 			}
9401da177e4SLinus Torvalds 		}
9411da177e4SLinus Torvalds 	} endfor_ifa(in_dev);
9421da177e4SLinus Torvalds 
9431da177e4SLinus Torvalds 	return same? addr : 0;
9441da177e4SLinus Torvalds }
9451da177e4SLinus Torvalds 
9461da177e4SLinus Torvalds /*
9471da177e4SLinus Torvalds  * Confirm that local IP address exists using wildcards:
9489bd85e32SDenis V. Lunev  * - in_dev: only on this interface, 0=any interface
9491da177e4SLinus Torvalds  * - dst: only in the same subnet as dst, 0=any dst
9501da177e4SLinus Torvalds  * - local: address, 0=autoselect the local address
9511da177e4SLinus Torvalds  * - scope: maximum allowed scope value for the local address
9521da177e4SLinus Torvalds  */
9539bd85e32SDenis V. Lunev __be32 inet_confirm_addr(struct in_device *in_dev,
9549bd85e32SDenis V. Lunev 			 __be32 dst, __be32 local, int scope)
9551da177e4SLinus Torvalds {
95660cad5daSAl Viro 	__be32 addr = 0;
9579bd85e32SDenis V. Lunev 	struct net_device *dev;
95839a6d063SDenis V. Lunev 	struct net *net;
9591da177e4SLinus Torvalds 
96039a6d063SDenis V. Lunev 	if (scope != RT_SCOPE_LINK)
9619bd85e32SDenis V. Lunev 		return confirm_addr_indev(in_dev, dst, local, scope);
9621da177e4SLinus Torvalds 
963c346dca1SYOSHIFUJI Hideaki 	net = dev_net(in_dev->dev);
9641da177e4SLinus Torvalds 	read_lock(&dev_base_lock);
9651da177e4SLinus Torvalds 	rcu_read_lock();
96639a6d063SDenis V. Lunev 	for_each_netdev(net, dev) {
967e5ed6399SHerbert Xu 		if ((in_dev = __in_dev_get_rcu(dev))) {
9681da177e4SLinus Torvalds 			addr = confirm_addr_indev(in_dev, dst, local, scope);
9691da177e4SLinus Torvalds 			if (addr)
9701da177e4SLinus Torvalds 				break;
9711da177e4SLinus Torvalds 		}
9721da177e4SLinus Torvalds 	}
9731da177e4SLinus Torvalds 	rcu_read_unlock();
9741da177e4SLinus Torvalds 	read_unlock(&dev_base_lock);
9751da177e4SLinus Torvalds 
9761da177e4SLinus Torvalds 	return addr;
9771da177e4SLinus Torvalds }
9781da177e4SLinus Torvalds 
9791da177e4SLinus Torvalds /*
9801da177e4SLinus Torvalds  *	Device notifier
9811da177e4SLinus Torvalds  */
9821da177e4SLinus Torvalds 
9831da177e4SLinus Torvalds int register_inetaddr_notifier(struct notifier_block *nb)
9841da177e4SLinus Torvalds {
985e041c683SAlan Stern 	return blocking_notifier_chain_register(&inetaddr_chain, nb);
9861da177e4SLinus Torvalds }
9871da177e4SLinus Torvalds 
9881da177e4SLinus Torvalds int unregister_inetaddr_notifier(struct notifier_block *nb)
9891da177e4SLinus Torvalds {
990e041c683SAlan Stern 	return blocking_notifier_chain_unregister(&inetaddr_chain, nb);
9911da177e4SLinus Torvalds }
9921da177e4SLinus Torvalds 
9931da177e4SLinus Torvalds /* Rename ifa_labels for a device name change. Make some effort to preserve existing
9941da177e4SLinus Torvalds  * alias numbering and to create unique labels if possible.
9951da177e4SLinus Torvalds */
9961da177e4SLinus Torvalds static void inetdev_changename(struct net_device *dev, struct in_device *in_dev)
9971da177e4SLinus Torvalds {
9981da177e4SLinus Torvalds 	struct in_ifaddr *ifa;
9991da177e4SLinus Torvalds 	int named = 0;
10001da177e4SLinus Torvalds 
10011da177e4SLinus Torvalds 	for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
10021da177e4SLinus Torvalds 		char old[IFNAMSIZ], *dot;
10031da177e4SLinus Torvalds 
10041da177e4SLinus Torvalds 		memcpy(old, ifa->ifa_label, IFNAMSIZ);
10051da177e4SLinus Torvalds 		memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
10061da177e4SLinus Torvalds 		if (named++ == 0)
1007573bf470SThomas Graf 			goto skip;
100844344b2aSMark McLoughlin 		dot = strchr(old, ':');
10091da177e4SLinus Torvalds 		if (dot == NULL) {
10101da177e4SLinus Torvalds 			sprintf(old, ":%d", named);
10111da177e4SLinus Torvalds 			dot = old;
10121da177e4SLinus Torvalds 		}
10131da177e4SLinus Torvalds 		if (strlen(dot) + strlen(dev->name) < IFNAMSIZ) {
10141da177e4SLinus Torvalds 			strcat(ifa->ifa_label, dot);
10151da177e4SLinus Torvalds 		} else {
10161da177e4SLinus Torvalds 			strcpy(ifa->ifa_label + (IFNAMSIZ - strlen(dot) - 1), dot);
10171da177e4SLinus Torvalds 		}
1018573bf470SThomas Graf skip:
1019573bf470SThomas Graf 		rtmsg_ifa(RTM_NEWADDR, ifa, NULL, 0);
10201da177e4SLinus Torvalds 	}
10211da177e4SLinus Torvalds }
10221da177e4SLinus Torvalds 
102306770843SBreno Leitao static inline bool inetdev_valid_mtu(unsigned mtu)
102406770843SBreno Leitao {
102506770843SBreno Leitao 	return mtu >= 68;
102606770843SBreno Leitao }
102706770843SBreno Leitao 
10281da177e4SLinus Torvalds /* Called only under RTNL semaphore */
10291da177e4SLinus Torvalds 
10301da177e4SLinus Torvalds static int inetdev_event(struct notifier_block *this, unsigned long event,
10311da177e4SLinus Torvalds 			 void *ptr)
10321da177e4SLinus Torvalds {
10331da177e4SLinus Torvalds 	struct net_device *dev = ptr;
1034e5ed6399SHerbert Xu 	struct in_device *in_dev = __in_dev_get_rtnl(dev);
10351da177e4SLinus Torvalds 
10361da177e4SLinus Torvalds 	ASSERT_RTNL();
10371da177e4SLinus Torvalds 
10381da177e4SLinus Torvalds 	if (!in_dev) {
10398030f544SHerbert Xu 		if (event == NETDEV_REGISTER) {
10401da177e4SLinus Torvalds 			in_dev = inetdev_init(dev);
10418d76527eSHerbert Xu 			if (!in_dev)
1042b217d616SHerbert Xu 				return notifier_from_errno(-ENOMEM);
10430cc217e1SEric W. Biederman 			if (dev->flags & IFF_LOOPBACK) {
104442f811b8SHerbert Xu 				IN_DEV_CONF_SET(in_dev, NOXFRM, 1);
104542f811b8SHerbert Xu 				IN_DEV_CONF_SET(in_dev, NOPOLICY, 1);
10461da177e4SLinus Torvalds 			}
104706770843SBreno Leitao 		} else if (event == NETDEV_CHANGEMTU) {
104806770843SBreno Leitao 			/* Re-enabling IP */
104906770843SBreno Leitao 			if (inetdev_valid_mtu(dev->mtu))
105006770843SBreno Leitao 				in_dev = inetdev_init(dev);
10518030f544SHerbert Xu 		}
10521da177e4SLinus Torvalds 		goto out;
10531da177e4SLinus Torvalds 	}
10541da177e4SLinus Torvalds 
10551da177e4SLinus Torvalds 	switch (event) {
10561da177e4SLinus Torvalds 	case NETDEV_REGISTER:
10571da177e4SLinus Torvalds 		printk(KERN_DEBUG "inetdev_event: bug\n");
10581da177e4SLinus Torvalds 		dev->ip_ptr = NULL;
10591da177e4SLinus Torvalds 		break;
10601da177e4SLinus Torvalds 	case NETDEV_UP:
106106770843SBreno Leitao 		if (!inetdev_valid_mtu(dev->mtu))
10621da177e4SLinus Torvalds 			break;
10630cc217e1SEric W. Biederman 		if (dev->flags & IFF_LOOPBACK) {
10641da177e4SLinus Torvalds 			struct in_ifaddr *ifa;
10651da177e4SLinus Torvalds 			if ((ifa = inet_alloc_ifa()) != NULL) {
10661da177e4SLinus Torvalds 				ifa->ifa_local =
10671da177e4SLinus Torvalds 				  ifa->ifa_address = htonl(INADDR_LOOPBACK);
10681da177e4SLinus Torvalds 				ifa->ifa_prefixlen = 8;
10691da177e4SLinus Torvalds 				ifa->ifa_mask = inet_make_mask(8);
10701da177e4SLinus Torvalds 				in_dev_hold(in_dev);
10711da177e4SLinus Torvalds 				ifa->ifa_dev = in_dev;
10721da177e4SLinus Torvalds 				ifa->ifa_scope = RT_SCOPE_HOST;
10731da177e4SLinus Torvalds 				memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
10741da177e4SLinus Torvalds 				inet_insert_ifa(ifa);
10751da177e4SLinus Torvalds 			}
10761da177e4SLinus Torvalds 		}
10771da177e4SLinus Torvalds 		ip_mc_up(in_dev);
1078eefef1cfSStephen Hemminger 		/* fall through */
1079eefef1cfSStephen Hemminger 	case NETDEV_CHANGEADDR:
1080eefef1cfSStephen Hemminger 		if (IN_DEV_ARP_NOTIFY(in_dev))
1081eefef1cfSStephen Hemminger 			arp_send(ARPOP_REQUEST, ETH_P_ARP,
1082eefef1cfSStephen Hemminger 				 in_dev->ifa_list->ifa_address,
1083eefef1cfSStephen Hemminger 				 dev,
1084eefef1cfSStephen Hemminger 				 in_dev->ifa_list->ifa_address,
1085eefef1cfSStephen Hemminger 				 NULL, dev->dev_addr, NULL);
10861da177e4SLinus Torvalds 		break;
10871da177e4SLinus Torvalds 	case NETDEV_DOWN:
10881da177e4SLinus Torvalds 		ip_mc_down(in_dev);
10891da177e4SLinus Torvalds 		break;
1090*75c78500SMoni Shoua 	case NETDEV_BONDING_OLDTYPE:
1091*75c78500SMoni Shoua 		ip_mc_unmap(in_dev);
1092*75c78500SMoni Shoua 		break;
1093*75c78500SMoni Shoua 	case NETDEV_BONDING_NEWTYPE:
1094*75c78500SMoni Shoua 		ip_mc_remap(in_dev);
1095*75c78500SMoni Shoua 		break;
10961da177e4SLinus Torvalds 	case NETDEV_CHANGEMTU:
109706770843SBreno Leitao 		if (inetdev_valid_mtu(dev->mtu))
10981da177e4SLinus Torvalds 			break;
109906770843SBreno Leitao 		/* disable IP when MTU is not enough */
11001da177e4SLinus Torvalds 	case NETDEV_UNREGISTER:
11011da177e4SLinus Torvalds 		inetdev_destroy(in_dev);
11021da177e4SLinus Torvalds 		break;
11031da177e4SLinus Torvalds 	case NETDEV_CHANGENAME:
11041da177e4SLinus Torvalds 		/* Do not notify about label change, this event is
11051da177e4SLinus Torvalds 		 * not interesting to applications using netlink.
11061da177e4SLinus Torvalds 		 */
11071da177e4SLinus Torvalds 		inetdev_changename(dev, in_dev);
11081da177e4SLinus Torvalds 
110951602b2aSPavel Emelyanov 		devinet_sysctl_unregister(in_dev);
111066f27a52SPavel Emelyanov 		devinet_sysctl_register(in_dev);
11111da177e4SLinus Torvalds 		break;
11121da177e4SLinus Torvalds 	}
11131da177e4SLinus Torvalds out:
11141da177e4SLinus Torvalds 	return NOTIFY_DONE;
11151da177e4SLinus Torvalds }
11161da177e4SLinus Torvalds 
11171da177e4SLinus Torvalds static struct notifier_block ip_netdev_notifier = {
11181da177e4SLinus Torvalds 	.notifier_call = inetdev_event,
11191da177e4SLinus Torvalds };
11201da177e4SLinus Torvalds 
1121339bf98fSThomas Graf static inline size_t inet_nlmsg_size(void)
1122339bf98fSThomas Graf {
1123339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
1124339bf98fSThomas Graf 	       + nla_total_size(4) /* IFA_ADDRESS */
1125339bf98fSThomas Graf 	       + nla_total_size(4) /* IFA_LOCAL */
1126339bf98fSThomas Graf 	       + nla_total_size(4) /* IFA_BROADCAST */
1127339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ); /* IFA_LABEL */
1128339bf98fSThomas Graf }
1129339bf98fSThomas Graf 
11301da177e4SLinus Torvalds static int inet_fill_ifaddr(struct sk_buff *skb, struct in_ifaddr *ifa,
1131b6544c0bSJamal Hadi Salim 			    u32 pid, u32 seq, int event, unsigned int flags)
11321da177e4SLinus Torvalds {
11331da177e4SLinus Torvalds 	struct ifaddrmsg *ifm;
11341da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
11351da177e4SLinus Torvalds 
113647f68512SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, event, sizeof(*ifm), flags);
113747f68512SThomas Graf 	if (nlh == NULL)
113826932566SPatrick McHardy 		return -EMSGSIZE;
113947f68512SThomas Graf 
114047f68512SThomas Graf 	ifm = nlmsg_data(nlh);
11411da177e4SLinus Torvalds 	ifm->ifa_family = AF_INET;
11421da177e4SLinus Torvalds 	ifm->ifa_prefixlen = ifa->ifa_prefixlen;
11431da177e4SLinus Torvalds 	ifm->ifa_flags = ifa->ifa_flags|IFA_F_PERMANENT;
11441da177e4SLinus Torvalds 	ifm->ifa_scope = ifa->ifa_scope;
11451da177e4SLinus Torvalds 	ifm->ifa_index = ifa->ifa_dev->dev->ifindex;
11461da177e4SLinus Torvalds 
114747f68512SThomas Graf 	if (ifa->ifa_address)
1148a7a628c4SAl Viro 		NLA_PUT_BE32(skb, IFA_ADDRESS, ifa->ifa_address);
114947f68512SThomas Graf 
115047f68512SThomas Graf 	if (ifa->ifa_local)
1151a7a628c4SAl Viro 		NLA_PUT_BE32(skb, IFA_LOCAL, ifa->ifa_local);
115247f68512SThomas Graf 
115347f68512SThomas Graf 	if (ifa->ifa_broadcast)
1154a7a628c4SAl Viro 		NLA_PUT_BE32(skb, IFA_BROADCAST, ifa->ifa_broadcast);
115547f68512SThomas Graf 
115647f68512SThomas Graf 	if (ifa->ifa_label[0])
115747f68512SThomas Graf 		NLA_PUT_STRING(skb, IFA_LABEL, ifa->ifa_label);
115847f68512SThomas Graf 
115947f68512SThomas Graf 	return nlmsg_end(skb, nlh);
116047f68512SThomas Graf 
116147f68512SThomas Graf nla_put_failure:
116226932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
116326932566SPatrick McHardy 	return -EMSGSIZE;
11641da177e4SLinus Torvalds }
11651da177e4SLinus Torvalds 
11661da177e4SLinus Torvalds static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
11671da177e4SLinus Torvalds {
11683b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
11691da177e4SLinus Torvalds 	int idx, ip_idx;
11701da177e4SLinus Torvalds 	struct net_device *dev;
11711da177e4SLinus Torvalds 	struct in_device *in_dev;
11721da177e4SLinus Torvalds 	struct in_ifaddr *ifa;
11731da177e4SLinus Torvalds 	int s_ip_idx, s_idx = cb->args[0];
11741da177e4SLinus Torvalds 
11751da177e4SLinus Torvalds 	s_ip_idx = ip_idx = cb->args[1];
11767562f876SPavel Emelianov 	idx = 0;
11774b8aa9abSDenis V. Lunev 	for_each_netdev(net, dev) {
11781da177e4SLinus Torvalds 		if (idx < s_idx)
11797562f876SPavel Emelianov 			goto cont;
11801da177e4SLinus Torvalds 		if (idx > s_idx)
11811da177e4SLinus Torvalds 			s_ip_idx = 0;
11826313c1e0SPatrick McHardy 		if ((in_dev = __in_dev_get_rtnl(dev)) == NULL)
11837562f876SPavel Emelianov 			goto cont;
11841da177e4SLinus Torvalds 
11851da177e4SLinus Torvalds 		for (ifa = in_dev->ifa_list, ip_idx = 0; ifa;
11861da177e4SLinus Torvalds 		     ifa = ifa->ifa_next, ip_idx++) {
11871da177e4SLinus Torvalds 			if (ip_idx < s_ip_idx)
1188596e4150SStephen Hemminger 				continue;
11891da177e4SLinus Torvalds 			if (inet_fill_ifaddr(skb, ifa, NETLINK_CB(cb->skb).pid,
11901da177e4SLinus Torvalds 					     cb->nlh->nlmsg_seq,
11916313c1e0SPatrick McHardy 					     RTM_NEWADDR, NLM_F_MULTI) <= 0)
11921da177e4SLinus Torvalds 				goto done;
11931da177e4SLinus Torvalds 		}
11947562f876SPavel Emelianov cont:
11957562f876SPavel Emelianov 		idx++;
11961da177e4SLinus Torvalds 	}
11971da177e4SLinus Torvalds 
11981da177e4SLinus Torvalds done:
11991da177e4SLinus Torvalds 	cb->args[0] = idx;
12001da177e4SLinus Torvalds 	cb->args[1] = ip_idx;
12011da177e4SLinus Torvalds 
12021da177e4SLinus Torvalds 	return skb->len;
12031da177e4SLinus Torvalds }
12041da177e4SLinus Torvalds 
1205d6062cbbSThomas Graf static void rtmsg_ifa(int event, struct in_ifaddr *ifa, struct nlmsghdr *nlh,
1206d6062cbbSThomas Graf 		      u32 pid)
12071da177e4SLinus Torvalds {
120847f68512SThomas Graf 	struct sk_buff *skb;
1209d6062cbbSThomas Graf 	u32 seq = nlh ? nlh->nlmsg_seq : 0;
1210d6062cbbSThomas Graf 	int err = -ENOBUFS;
12114b8aa9abSDenis V. Lunev 	struct net *net;
12121da177e4SLinus Torvalds 
1213c346dca1SYOSHIFUJI Hideaki 	net = dev_net(ifa->ifa_dev->dev);
1214339bf98fSThomas Graf 	skb = nlmsg_new(inet_nlmsg_size(), GFP_KERNEL);
121547f68512SThomas Graf 	if (skb == NULL)
1216d6062cbbSThomas Graf 		goto errout;
1217d6062cbbSThomas Graf 
1218d6062cbbSThomas Graf 	err = inet_fill_ifaddr(skb, ifa, pid, seq, event, 0);
121926932566SPatrick McHardy 	if (err < 0) {
122026932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet_nlmsg_size() */
122126932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
122226932566SPatrick McHardy 		kfree_skb(skb);
122326932566SPatrick McHardy 		goto errout;
122426932566SPatrick McHardy 	}
12251ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, net, pid, RTNLGRP_IPV4_IFADDR, nlh, GFP_KERNEL);
12261ce85fe4SPablo Neira Ayuso 	return;
1227d6062cbbSThomas Graf errout:
1228d6062cbbSThomas Graf 	if (err < 0)
12294b8aa9abSDenis V. Lunev 		rtnl_set_sk_err(net, RTNLGRP_IPV4_IFADDR, err);
12301da177e4SLinus Torvalds }
12311da177e4SLinus Torvalds 
12321da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
12331da177e4SLinus Torvalds 
1234c0ce9fb3SPavel Emelyanov static void devinet_copy_dflt_conf(struct net *net, int i)
123531be3085SHerbert Xu {
123631be3085SHerbert Xu 	struct net_device *dev;
123731be3085SHerbert Xu 
123831be3085SHerbert Xu 	read_lock(&dev_base_lock);
1239c0ce9fb3SPavel Emelyanov 	for_each_netdev(net, dev) {
124031be3085SHerbert Xu 		struct in_device *in_dev;
124131be3085SHerbert Xu 		rcu_read_lock();
124231be3085SHerbert Xu 		in_dev = __in_dev_get_rcu(dev);
124331be3085SHerbert Xu 		if (in_dev && !test_bit(i, in_dev->cnf.state))
12449355bbd6SPavel Emelyanov 			in_dev->cnf.data[i] = net->ipv4.devconf_dflt->data[i];
124531be3085SHerbert Xu 		rcu_read_unlock();
124631be3085SHerbert Xu 	}
124731be3085SHerbert Xu 	read_unlock(&dev_base_lock);
124831be3085SHerbert Xu }
124931be3085SHerbert Xu 
1250c0ce9fb3SPavel Emelyanov static void inet_forward_change(struct net *net)
125168dd299bSPavel Emelyanov {
125268dd299bSPavel Emelyanov 	struct net_device *dev;
1253586f1211SPavel Emelyanov 	int on = IPV4_DEVCONF_ALL(net, FORWARDING);
125468dd299bSPavel Emelyanov 
1255586f1211SPavel Emelyanov 	IPV4_DEVCONF_ALL(net, ACCEPT_REDIRECTS) = !on;
12569355bbd6SPavel Emelyanov 	IPV4_DEVCONF_DFLT(net, FORWARDING) = on;
125768dd299bSPavel Emelyanov 
125868dd299bSPavel Emelyanov 	read_lock(&dev_base_lock);
1259c0ce9fb3SPavel Emelyanov 	for_each_netdev(net, dev) {
126068dd299bSPavel Emelyanov 		struct in_device *in_dev;
12610187bdfbSBen Hutchings 		if (on)
12620187bdfbSBen Hutchings 			dev_disable_lro(dev);
126368dd299bSPavel Emelyanov 		rcu_read_lock();
126468dd299bSPavel Emelyanov 		in_dev = __in_dev_get_rcu(dev);
126568dd299bSPavel Emelyanov 		if (in_dev)
126668dd299bSPavel Emelyanov 			IN_DEV_CONF_SET(in_dev, FORWARDING, on);
126768dd299bSPavel Emelyanov 		rcu_read_unlock();
126868dd299bSPavel Emelyanov 	}
126968dd299bSPavel Emelyanov 	read_unlock(&dev_base_lock);
127068dd299bSPavel Emelyanov }
127168dd299bSPavel Emelyanov 
127231be3085SHerbert Xu static int devinet_conf_proc(ctl_table *ctl, int write,
127331be3085SHerbert Xu 			     struct file *filp, void __user *buffer,
127431be3085SHerbert Xu 			     size_t *lenp, loff_t *ppos)
127531be3085SHerbert Xu {
127631be3085SHerbert Xu 	int ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
127731be3085SHerbert Xu 
127831be3085SHerbert Xu 	if (write) {
127931be3085SHerbert Xu 		struct ipv4_devconf *cnf = ctl->extra1;
1280c0ce9fb3SPavel Emelyanov 		struct net *net = ctl->extra2;
128131be3085SHerbert Xu 		int i = (int *)ctl->data - cnf->data;
128231be3085SHerbert Xu 
128331be3085SHerbert Xu 		set_bit(i, cnf->state);
128431be3085SHerbert Xu 
12859355bbd6SPavel Emelyanov 		if (cnf == net->ipv4.devconf_dflt)
1286c0ce9fb3SPavel Emelyanov 			devinet_copy_dflt_conf(net, i);
128731be3085SHerbert Xu 	}
128831be3085SHerbert Xu 
128931be3085SHerbert Xu 	return ret;
129031be3085SHerbert Xu }
129131be3085SHerbert Xu 
1292f221e726SAlexey Dobriyan static int devinet_conf_sysctl(ctl_table *table,
129331be3085SHerbert Xu 			       void __user *oldval, size_t __user *oldlenp,
129431be3085SHerbert Xu 			       void __user *newval, size_t newlen)
129531be3085SHerbert Xu {
129631be3085SHerbert Xu 	struct ipv4_devconf *cnf;
1297c0ce9fb3SPavel Emelyanov 	struct net *net;
129831be3085SHerbert Xu 	int *valp = table->data;
129931be3085SHerbert Xu 	int new;
130031be3085SHerbert Xu 	int i;
130131be3085SHerbert Xu 
130231be3085SHerbert Xu 	if (!newval || !newlen)
130331be3085SHerbert Xu 		return 0;
130431be3085SHerbert Xu 
130531be3085SHerbert Xu 	if (newlen != sizeof(int))
130631be3085SHerbert Xu 		return -EINVAL;
130731be3085SHerbert Xu 
130831be3085SHerbert Xu 	if (get_user(new, (int __user *)newval))
130931be3085SHerbert Xu 		return -EFAULT;
131031be3085SHerbert Xu 
131131be3085SHerbert Xu 	if (new == *valp)
131231be3085SHerbert Xu 		return 0;
131331be3085SHerbert Xu 
131431be3085SHerbert Xu 	if (oldval && oldlenp) {
131531be3085SHerbert Xu 		size_t len;
131631be3085SHerbert Xu 
131731be3085SHerbert Xu 		if (get_user(len, oldlenp))
131831be3085SHerbert Xu 			return -EFAULT;
131931be3085SHerbert Xu 
132031be3085SHerbert Xu 		if (len) {
132131be3085SHerbert Xu 			if (len > table->maxlen)
132231be3085SHerbert Xu 				len = table->maxlen;
132331be3085SHerbert Xu 			if (copy_to_user(oldval, valp, len))
132431be3085SHerbert Xu 				return -EFAULT;
132531be3085SHerbert Xu 			if (put_user(len, oldlenp))
132631be3085SHerbert Xu 				return -EFAULT;
132731be3085SHerbert Xu 		}
132831be3085SHerbert Xu 	}
132931be3085SHerbert Xu 
133031be3085SHerbert Xu 	*valp = new;
133131be3085SHerbert Xu 
133231be3085SHerbert Xu 	cnf = table->extra1;
1333c0ce9fb3SPavel Emelyanov 	net = table->extra2;
133431be3085SHerbert Xu 	i = (int *)table->data - cnf->data;
133531be3085SHerbert Xu 
133631be3085SHerbert Xu 	set_bit(i, cnf->state);
133731be3085SHerbert Xu 
13389355bbd6SPavel Emelyanov 	if (cnf == net->ipv4.devconf_dflt)
1339c0ce9fb3SPavel Emelyanov 		devinet_copy_dflt_conf(net, i);
134031be3085SHerbert Xu 
134131be3085SHerbert Xu 	return 1;
134231be3085SHerbert Xu }
134331be3085SHerbert Xu 
13441da177e4SLinus Torvalds static int devinet_sysctl_forward(ctl_table *ctl, int write,
13451da177e4SLinus Torvalds 				  struct file *filp, void __user *buffer,
13461da177e4SLinus Torvalds 				  size_t *lenp, loff_t *ppos)
13471da177e4SLinus Torvalds {
13481da177e4SLinus Torvalds 	int *valp = ctl->data;
13491da177e4SLinus Torvalds 	int val = *valp;
13501da177e4SLinus Torvalds 	int ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
13511da177e4SLinus Torvalds 
13521da177e4SLinus Torvalds 	if (write && *valp != val) {
1353c0ce9fb3SPavel Emelyanov 		struct net *net = ctl->extra2;
1354c0ce9fb3SPavel Emelyanov 
13550187bdfbSBen Hutchings 		if (valp != &IPV4_DEVCONF_DFLT(net, FORWARDING)) {
13569b8adb5eSEric W. Biederman 			if (!rtnl_trylock())
13579b8adb5eSEric W. Biederman 				return restart_syscall();
13580187bdfbSBen Hutchings 			if (valp == &IPV4_DEVCONF_ALL(net, FORWARDING)) {
1359c0ce9fb3SPavel Emelyanov 				inet_forward_change(net);
13600187bdfbSBen Hutchings 			} else if (*valp) {
13610187bdfbSBen Hutchings 				struct ipv4_devconf *cnf = ctl->extra1;
13620187bdfbSBen Hutchings 				struct in_device *idev =
13630187bdfbSBen Hutchings 					container_of(cnf, struct in_device, cnf);
13640187bdfbSBen Hutchings 				dev_disable_lro(idev->dev);
13650187bdfbSBen Hutchings 			}
13660187bdfbSBen Hutchings 			rtnl_unlock();
136776e6ebfbSDenis V. Lunev 			rt_cache_flush(net, 0);
13681da177e4SLinus Torvalds 		}
13690187bdfbSBen Hutchings 	}
13701da177e4SLinus Torvalds 
13711da177e4SLinus Torvalds 	return ret;
13721da177e4SLinus Torvalds }
13731da177e4SLinus Torvalds 
13741da177e4SLinus Torvalds int ipv4_doint_and_flush(ctl_table *ctl, int write,
13751da177e4SLinus Torvalds 			 struct file *filp, void __user *buffer,
13761da177e4SLinus Torvalds 			 size_t *lenp, loff_t *ppos)
13771da177e4SLinus Torvalds {
13781da177e4SLinus Torvalds 	int *valp = ctl->data;
13791da177e4SLinus Torvalds 	int val = *valp;
13801da177e4SLinus Torvalds 	int ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
138176e6ebfbSDenis V. Lunev 	struct net *net = ctl->extra2;
13821da177e4SLinus Torvalds 
13831da177e4SLinus Torvalds 	if (write && *valp != val)
138476e6ebfbSDenis V. Lunev 		rt_cache_flush(net, 0);
13851da177e4SLinus Torvalds 
13861da177e4SLinus Torvalds 	return ret;
13871da177e4SLinus Torvalds }
13881da177e4SLinus Torvalds 
1389f221e726SAlexey Dobriyan int ipv4_doint_and_flush_strategy(ctl_table *table,
13901da177e4SLinus Torvalds 				  void __user *oldval, size_t __user *oldlenp,
13911f29bcd7SAlexey Dobriyan 				  void __user *newval, size_t newlen)
13921da177e4SLinus Torvalds {
1393f221e726SAlexey Dobriyan 	int ret = devinet_conf_sysctl(table, oldval, oldlenp, newval, newlen);
139476e6ebfbSDenis V. Lunev 	struct net *net = table->extra2;
13951da177e4SLinus Torvalds 
139631be3085SHerbert Xu 	if (ret == 1)
139776e6ebfbSDenis V. Lunev 		rt_cache_flush(net, 0);
139831be3085SHerbert Xu 
139931be3085SHerbert Xu 	return ret;
14001da177e4SLinus Torvalds }
14011da177e4SLinus Torvalds 
14021da177e4SLinus Torvalds 
140342f811b8SHerbert Xu #define DEVINET_SYSCTL_ENTRY(attr, name, mval, proc, sysctl) \
140442f811b8SHerbert Xu 	{ \
140542f811b8SHerbert Xu 		.ctl_name	= NET_IPV4_CONF_ ## attr, \
140642f811b8SHerbert Xu 		.procname	= name, \
140742f811b8SHerbert Xu 		.data		= ipv4_devconf.data + \
140842f811b8SHerbert Xu 				  NET_IPV4_CONF_ ## attr - 1, \
140942f811b8SHerbert Xu 		.maxlen		= sizeof(int), \
141042f811b8SHerbert Xu 		.mode		= mval, \
141142f811b8SHerbert Xu 		.proc_handler	= proc, \
141242f811b8SHerbert Xu 		.strategy	= sysctl, \
141331be3085SHerbert Xu 		.extra1		= &ipv4_devconf, \
141442f811b8SHerbert Xu 	}
141542f811b8SHerbert Xu 
141642f811b8SHerbert Xu #define DEVINET_SYSCTL_RW_ENTRY(attr, name) \
141731be3085SHerbert Xu 	DEVINET_SYSCTL_ENTRY(attr, name, 0644, devinet_conf_proc, \
141831be3085SHerbert Xu 			     devinet_conf_sysctl)
141942f811b8SHerbert Xu 
142042f811b8SHerbert Xu #define DEVINET_SYSCTL_RO_ENTRY(attr, name) \
142131be3085SHerbert Xu 	DEVINET_SYSCTL_ENTRY(attr, name, 0444, devinet_conf_proc, \
142231be3085SHerbert Xu 			     devinet_conf_sysctl)
142342f811b8SHerbert Xu 
142442f811b8SHerbert Xu #define DEVINET_SYSCTL_COMPLEX_ENTRY(attr, name, proc, sysctl) \
142542f811b8SHerbert Xu 	DEVINET_SYSCTL_ENTRY(attr, name, 0644, proc, sysctl)
142642f811b8SHerbert Xu 
142742f811b8SHerbert Xu #define DEVINET_SYSCTL_FLUSHING_ENTRY(attr, name) \
142842f811b8SHerbert Xu 	DEVINET_SYSCTL_COMPLEX_ENTRY(attr, name, ipv4_doint_and_flush, \
142942f811b8SHerbert Xu 				     ipv4_doint_and_flush_strategy)
143042f811b8SHerbert Xu 
14311da177e4SLinus Torvalds static struct devinet_sysctl_table {
14321da177e4SLinus Torvalds 	struct ctl_table_header *sysctl_header;
1433bfada697SPavel Emelyanov 	struct ctl_table devinet_vars[__NET_IPV4_CONF_MAX];
1434bfada697SPavel Emelyanov 	char *dev_name;
14351da177e4SLinus Torvalds } devinet_sysctl = {
14361da177e4SLinus Torvalds 	.devinet_vars = {
143742f811b8SHerbert Xu 		DEVINET_SYSCTL_COMPLEX_ENTRY(FORWARDING, "forwarding",
143831be3085SHerbert Xu 					     devinet_sysctl_forward,
143931be3085SHerbert Xu 					     devinet_conf_sysctl),
144042f811b8SHerbert Xu 		DEVINET_SYSCTL_RO_ENTRY(MC_FORWARDING, "mc_forwarding"),
144142f811b8SHerbert Xu 
144242f811b8SHerbert Xu 		DEVINET_SYSCTL_RW_ENTRY(ACCEPT_REDIRECTS, "accept_redirects"),
144342f811b8SHerbert Xu 		DEVINET_SYSCTL_RW_ENTRY(SECURE_REDIRECTS, "secure_redirects"),
144442f811b8SHerbert Xu 		DEVINET_SYSCTL_RW_ENTRY(SHARED_MEDIA, "shared_media"),
144542f811b8SHerbert Xu 		DEVINET_SYSCTL_RW_ENTRY(RP_FILTER, "rp_filter"),
144642f811b8SHerbert Xu 		DEVINET_SYSCTL_RW_ENTRY(SEND_REDIRECTS, "send_redirects"),
144742f811b8SHerbert Xu 		DEVINET_SYSCTL_RW_ENTRY(ACCEPT_SOURCE_ROUTE,
144842f811b8SHerbert Xu 					"accept_source_route"),
144942f811b8SHerbert Xu 		DEVINET_SYSCTL_RW_ENTRY(PROXY_ARP, "proxy_arp"),
145042f811b8SHerbert Xu 		DEVINET_SYSCTL_RW_ENTRY(MEDIUM_ID, "medium_id"),
145142f811b8SHerbert Xu 		DEVINET_SYSCTL_RW_ENTRY(BOOTP_RELAY, "bootp_relay"),
145242f811b8SHerbert Xu 		DEVINET_SYSCTL_RW_ENTRY(LOG_MARTIANS, "log_martians"),
145342f811b8SHerbert Xu 		DEVINET_SYSCTL_RW_ENTRY(TAG, "tag"),
145442f811b8SHerbert Xu 		DEVINET_SYSCTL_RW_ENTRY(ARPFILTER, "arp_filter"),
145542f811b8SHerbert Xu 		DEVINET_SYSCTL_RW_ENTRY(ARP_ANNOUNCE, "arp_announce"),
145642f811b8SHerbert Xu 		DEVINET_SYSCTL_RW_ENTRY(ARP_IGNORE, "arp_ignore"),
145742f811b8SHerbert Xu 		DEVINET_SYSCTL_RW_ENTRY(ARP_ACCEPT, "arp_accept"),
1458eefef1cfSStephen Hemminger 		DEVINET_SYSCTL_RW_ENTRY(ARP_NOTIFY, "arp_notify"),
145942f811b8SHerbert Xu 
146042f811b8SHerbert Xu 		DEVINET_SYSCTL_FLUSHING_ENTRY(NOXFRM, "disable_xfrm"),
146142f811b8SHerbert Xu 		DEVINET_SYSCTL_FLUSHING_ENTRY(NOPOLICY, "disable_policy"),
146242f811b8SHerbert Xu 		DEVINET_SYSCTL_FLUSHING_ENTRY(FORCE_IGMP_VERSION,
146342f811b8SHerbert Xu 					      "force_igmp_version"),
146442f811b8SHerbert Xu 		DEVINET_SYSCTL_FLUSHING_ENTRY(PROMOTE_SECONDARIES,
146542f811b8SHerbert Xu 					      "promote_secondaries"),
14661da177e4SLinus Torvalds 	},
14671da177e4SLinus Torvalds };
14681da177e4SLinus Torvalds 
1469ea40b324SPavel Emelyanov static int __devinet_sysctl_register(struct net *net, char *dev_name,
1470ea40b324SPavel Emelyanov 		int ctl_name, struct ipv4_devconf *p)
14711da177e4SLinus Torvalds {
14721da177e4SLinus Torvalds 	int i;
14739fa89642SPavel Emelyanov 	struct devinet_sysctl_table *t;
14741da177e4SLinus Torvalds 
1475bfada697SPavel Emelyanov #define DEVINET_CTL_PATH_DEV	3
1476bfada697SPavel Emelyanov 
1477bfada697SPavel Emelyanov 	struct ctl_path devinet_ctl_path[] = {
1478bfada697SPavel Emelyanov 		{ .procname = "net", .ctl_name = CTL_NET, },
1479bfada697SPavel Emelyanov 		{ .procname = "ipv4", .ctl_name = NET_IPV4, },
1480bfada697SPavel Emelyanov 		{ .procname = "conf", .ctl_name = NET_IPV4_CONF, },
1481bfada697SPavel Emelyanov 		{ /* to be set */ },
1482bfada697SPavel Emelyanov 		{ },
1483bfada697SPavel Emelyanov 	};
1484bfada697SPavel Emelyanov 
14859fa89642SPavel Emelyanov 	t = kmemdup(&devinet_sysctl, sizeof(*t), GFP_KERNEL);
14861da177e4SLinus Torvalds 	if (!t)
14879fa89642SPavel Emelyanov 		goto out;
14889fa89642SPavel Emelyanov 
14891da177e4SLinus Torvalds 	for (i = 0; i < ARRAY_SIZE(t->devinet_vars) - 1; i++) {
14901da177e4SLinus Torvalds 		t->devinet_vars[i].data += (char *)p - (char *)&ipv4_devconf;
149131be3085SHerbert Xu 		t->devinet_vars[i].extra1 = p;
1492c0ce9fb3SPavel Emelyanov 		t->devinet_vars[i].extra2 = net;
14931da177e4SLinus Torvalds 	}
14941da177e4SLinus Torvalds 
14951da177e4SLinus Torvalds 	/*
14961da177e4SLinus Torvalds 	 * Make a copy of dev_name, because '.procname' is regarded as const
14971da177e4SLinus Torvalds 	 * by sysctl and we wouldn't want anyone to change it under our feet
14981da177e4SLinus Torvalds 	 * (see SIOCSIFNAME).
14991da177e4SLinus Torvalds 	 */
1500bfada697SPavel Emelyanov 	t->dev_name = kstrdup(dev_name, GFP_KERNEL);
1501bfada697SPavel Emelyanov 	if (!t->dev_name)
15021da177e4SLinus Torvalds 		goto free;
15031da177e4SLinus Torvalds 
1504bfada697SPavel Emelyanov 	devinet_ctl_path[DEVINET_CTL_PATH_DEV].procname = t->dev_name;
1505bfada697SPavel Emelyanov 	devinet_ctl_path[DEVINET_CTL_PATH_DEV].ctl_name = ctl_name;
15061da177e4SLinus Torvalds 
1507752d14dcSPavel Emelyanov 	t->sysctl_header = register_net_sysctl_table(net, devinet_ctl_path,
1508bfada697SPavel Emelyanov 			t->devinet_vars);
15091da177e4SLinus Torvalds 	if (!t->sysctl_header)
15101da177e4SLinus Torvalds 		goto free_procname;
15111da177e4SLinus Torvalds 
15121da177e4SLinus Torvalds 	p->sysctl = t;
1513ea40b324SPavel Emelyanov 	return 0;
15141da177e4SLinus Torvalds 
15151da177e4SLinus Torvalds free_procname:
1516bfada697SPavel Emelyanov 	kfree(t->dev_name);
15171da177e4SLinus Torvalds free:
15181da177e4SLinus Torvalds 	kfree(t);
15199fa89642SPavel Emelyanov out:
1520ea40b324SPavel Emelyanov 	return -ENOBUFS;
15211da177e4SLinus Torvalds }
15221da177e4SLinus Torvalds 
152351602b2aSPavel Emelyanov static void __devinet_sysctl_unregister(struct ipv4_devconf *cnf)
152466f27a52SPavel Emelyanov {
152551602b2aSPavel Emelyanov 	struct devinet_sysctl_table *t = cnf->sysctl;
152666f27a52SPavel Emelyanov 
152751602b2aSPavel Emelyanov 	if (t == NULL)
152851602b2aSPavel Emelyanov 		return;
152951602b2aSPavel Emelyanov 
153051602b2aSPavel Emelyanov 	cnf->sysctl = NULL;
15311da177e4SLinus Torvalds 	unregister_sysctl_table(t->sysctl_header);
1532bfada697SPavel Emelyanov 	kfree(t->dev_name);
15331da177e4SLinus Torvalds 	kfree(t);
15341da177e4SLinus Torvalds }
153551602b2aSPavel Emelyanov 
153651602b2aSPavel Emelyanov static void devinet_sysctl_register(struct in_device *idev)
153751602b2aSPavel Emelyanov {
153851602b2aSPavel Emelyanov 	neigh_sysctl_register(idev->dev, idev->arp_parms, NET_IPV4,
153951602b2aSPavel Emelyanov 			NET_IPV4_NEIGH, "ipv4", NULL, NULL);
1540c346dca1SYOSHIFUJI Hideaki 	__devinet_sysctl_register(dev_net(idev->dev), idev->dev->name,
1541c0ce9fb3SPavel Emelyanov 			idev->dev->ifindex, &idev->cnf);
154251602b2aSPavel Emelyanov }
154351602b2aSPavel Emelyanov 
154451602b2aSPavel Emelyanov static void devinet_sysctl_unregister(struct in_device *idev)
154551602b2aSPavel Emelyanov {
154651602b2aSPavel Emelyanov 	__devinet_sysctl_unregister(&idev->cnf);
154751602b2aSPavel Emelyanov 	neigh_sysctl_unregister(idev->arp_parms);
15481da177e4SLinus Torvalds }
15491da177e4SLinus Torvalds 
155068dd299bSPavel Emelyanov static struct ctl_table ctl_forward_entry[] = {
155168dd299bSPavel Emelyanov 	{
155268dd299bSPavel Emelyanov 		.ctl_name	= NET_IPV4_FORWARD,
155368dd299bSPavel Emelyanov 		.procname	= "ip_forward",
155468dd299bSPavel Emelyanov 		.data		= &ipv4_devconf.data[
155568dd299bSPavel Emelyanov 					NET_IPV4_CONF_FORWARDING - 1],
155668dd299bSPavel Emelyanov 		.maxlen		= sizeof(int),
155768dd299bSPavel Emelyanov 		.mode		= 0644,
155868dd299bSPavel Emelyanov 		.proc_handler	= devinet_sysctl_forward,
155968dd299bSPavel Emelyanov 		.strategy	= devinet_conf_sysctl,
156068dd299bSPavel Emelyanov 		.extra1		= &ipv4_devconf,
1561c0ce9fb3SPavel Emelyanov 		.extra2		= &init_net,
156268dd299bSPavel Emelyanov 	},
156368dd299bSPavel Emelyanov 	{ },
156468dd299bSPavel Emelyanov };
156568dd299bSPavel Emelyanov 
1566752d14dcSPavel Emelyanov static __net_initdata struct ctl_path net_ipv4_path[] = {
156768dd299bSPavel Emelyanov 	{ .procname = "net", .ctl_name = CTL_NET, },
156868dd299bSPavel Emelyanov 	{ .procname = "ipv4", .ctl_name = NET_IPV4, },
156968dd299bSPavel Emelyanov 	{ },
157068dd299bSPavel Emelyanov };
15712a75de0cSEric Dumazet #endif
157268dd299bSPavel Emelyanov 
1573752d14dcSPavel Emelyanov static __net_init int devinet_init_net(struct net *net)
1574752d14dcSPavel Emelyanov {
1575752d14dcSPavel Emelyanov 	int err;
1576752d14dcSPavel Emelyanov 	struct ipv4_devconf *all, *dflt;
15772a75de0cSEric Dumazet #ifdef CONFIG_SYSCTL
15782a75de0cSEric Dumazet 	struct ctl_table *tbl = ctl_forward_entry;
1579752d14dcSPavel Emelyanov 	struct ctl_table_header *forw_hdr;
15802a75de0cSEric Dumazet #endif
1581752d14dcSPavel Emelyanov 
1582752d14dcSPavel Emelyanov 	err = -ENOMEM;
1583752d14dcSPavel Emelyanov 	all = &ipv4_devconf;
1584752d14dcSPavel Emelyanov 	dflt = &ipv4_devconf_dflt;
1585752d14dcSPavel Emelyanov 
1586752d14dcSPavel Emelyanov 	if (net != &init_net) {
1587752d14dcSPavel Emelyanov 		all = kmemdup(all, sizeof(ipv4_devconf), GFP_KERNEL);
1588752d14dcSPavel Emelyanov 		if (all == NULL)
1589752d14dcSPavel Emelyanov 			goto err_alloc_all;
1590752d14dcSPavel Emelyanov 
1591752d14dcSPavel Emelyanov 		dflt = kmemdup(dflt, sizeof(ipv4_devconf_dflt), GFP_KERNEL);
1592752d14dcSPavel Emelyanov 		if (dflt == NULL)
1593752d14dcSPavel Emelyanov 			goto err_alloc_dflt;
1594752d14dcSPavel Emelyanov 
15952a75de0cSEric Dumazet #ifdef CONFIG_SYSCTL
1596752d14dcSPavel Emelyanov 		tbl = kmemdup(tbl, sizeof(ctl_forward_entry), GFP_KERNEL);
1597752d14dcSPavel Emelyanov 		if (tbl == NULL)
1598752d14dcSPavel Emelyanov 			goto err_alloc_ctl;
1599752d14dcSPavel Emelyanov 
1600752d14dcSPavel Emelyanov 		tbl[0].data = &all->data[NET_IPV4_CONF_FORWARDING - 1];
1601752d14dcSPavel Emelyanov 		tbl[0].extra1 = all;
1602752d14dcSPavel Emelyanov 		tbl[0].extra2 = net;
16032a75de0cSEric Dumazet #endif
1604752d14dcSPavel Emelyanov 	}
1605752d14dcSPavel Emelyanov 
1606752d14dcSPavel Emelyanov #ifdef CONFIG_SYSCTL
1607752d14dcSPavel Emelyanov 	err = __devinet_sysctl_register(net, "all",
1608752d14dcSPavel Emelyanov 			NET_PROTO_CONF_ALL, all);
1609752d14dcSPavel Emelyanov 	if (err < 0)
1610752d14dcSPavel Emelyanov 		goto err_reg_all;
1611752d14dcSPavel Emelyanov 
1612752d14dcSPavel Emelyanov 	err = __devinet_sysctl_register(net, "default",
1613752d14dcSPavel Emelyanov 			NET_PROTO_CONF_DEFAULT, dflt);
1614752d14dcSPavel Emelyanov 	if (err < 0)
1615752d14dcSPavel Emelyanov 		goto err_reg_dflt;
1616752d14dcSPavel Emelyanov 
1617752d14dcSPavel Emelyanov 	err = -ENOMEM;
1618752d14dcSPavel Emelyanov 	forw_hdr = register_net_sysctl_table(net, net_ipv4_path, tbl);
1619752d14dcSPavel Emelyanov 	if (forw_hdr == NULL)
1620752d14dcSPavel Emelyanov 		goto err_reg_ctl;
16212a75de0cSEric Dumazet 	net->ipv4.forw_hdr = forw_hdr;
1622752d14dcSPavel Emelyanov #endif
1623752d14dcSPavel Emelyanov 
1624752d14dcSPavel Emelyanov 	net->ipv4.devconf_all = all;
1625752d14dcSPavel Emelyanov 	net->ipv4.devconf_dflt = dflt;
1626752d14dcSPavel Emelyanov 	return 0;
1627752d14dcSPavel Emelyanov 
1628752d14dcSPavel Emelyanov #ifdef CONFIG_SYSCTL
1629752d14dcSPavel Emelyanov err_reg_ctl:
1630752d14dcSPavel Emelyanov 	__devinet_sysctl_unregister(dflt);
1631752d14dcSPavel Emelyanov err_reg_dflt:
1632752d14dcSPavel Emelyanov 	__devinet_sysctl_unregister(all);
1633752d14dcSPavel Emelyanov err_reg_all:
1634752d14dcSPavel Emelyanov 	if (tbl != ctl_forward_entry)
1635752d14dcSPavel Emelyanov 		kfree(tbl);
1636752d14dcSPavel Emelyanov err_alloc_ctl:
16372a75de0cSEric Dumazet #endif
1638752d14dcSPavel Emelyanov 	if (dflt != &ipv4_devconf_dflt)
1639752d14dcSPavel Emelyanov 		kfree(dflt);
1640752d14dcSPavel Emelyanov err_alloc_dflt:
1641752d14dcSPavel Emelyanov 	if (all != &ipv4_devconf)
1642752d14dcSPavel Emelyanov 		kfree(all);
1643752d14dcSPavel Emelyanov err_alloc_all:
1644752d14dcSPavel Emelyanov 	return err;
1645752d14dcSPavel Emelyanov }
1646752d14dcSPavel Emelyanov 
1647752d14dcSPavel Emelyanov static __net_exit void devinet_exit_net(struct net *net)
1648752d14dcSPavel Emelyanov {
16492a75de0cSEric Dumazet #ifdef CONFIG_SYSCTL
1650752d14dcSPavel Emelyanov 	struct ctl_table *tbl;
1651752d14dcSPavel Emelyanov 
1652752d14dcSPavel Emelyanov 	tbl = net->ipv4.forw_hdr->ctl_table_arg;
1653752d14dcSPavel Emelyanov 	unregister_net_sysctl_table(net->ipv4.forw_hdr);
1654752d14dcSPavel Emelyanov 	__devinet_sysctl_unregister(net->ipv4.devconf_dflt);
1655752d14dcSPavel Emelyanov 	__devinet_sysctl_unregister(net->ipv4.devconf_all);
1656752d14dcSPavel Emelyanov 	kfree(tbl);
16572a75de0cSEric Dumazet #endif
1658752d14dcSPavel Emelyanov 	kfree(net->ipv4.devconf_dflt);
1659752d14dcSPavel Emelyanov 	kfree(net->ipv4.devconf_all);
1660752d14dcSPavel Emelyanov }
1661752d14dcSPavel Emelyanov 
1662752d14dcSPavel Emelyanov static __net_initdata struct pernet_operations devinet_ops = {
1663752d14dcSPavel Emelyanov 	.init = devinet_init_net,
1664752d14dcSPavel Emelyanov 	.exit = devinet_exit_net,
1665752d14dcSPavel Emelyanov };
1666752d14dcSPavel Emelyanov 
16671da177e4SLinus Torvalds void __init devinet_init(void)
16681da177e4SLinus Torvalds {
1669752d14dcSPavel Emelyanov 	register_pernet_subsys(&devinet_ops);
1670752d14dcSPavel Emelyanov 
16711da177e4SLinus Torvalds 	register_gifconf(PF_INET, inet_gifconf);
16721da177e4SLinus Torvalds 	register_netdevice_notifier(&ip_netdev_notifier);
167363f3444fSThomas Graf 
167463f3444fSThomas Graf 	rtnl_register(PF_INET, RTM_NEWADDR, inet_rtm_newaddr, NULL);
167563f3444fSThomas Graf 	rtnl_register(PF_INET, RTM_DELADDR, inet_rtm_deladdr, NULL);
167663f3444fSThomas Graf 	rtnl_register(PF_INET, RTM_GETADDR, NULL, inet_dump_ifaddr);
16771da177e4SLinus Torvalds }
16781da177e4SLinus Torvalds 
16791da177e4SLinus Torvalds EXPORT_SYMBOL(in_dev_finish_destroy);
16801da177e4SLinus Torvalds EXPORT_SYMBOL(inet_select_addr);
16811da177e4SLinus Torvalds EXPORT_SYMBOL(inetdev_by_index);
16821da177e4SLinus Torvalds EXPORT_SYMBOL(register_inetaddr_notifier);
16831da177e4SLinus Torvalds EXPORT_SYMBOL(unregister_inetaddr_notifier);
1684