xref: /linux/net/ipv4/devinet.c (revision 91df42bedccb919902c7cf7eb876c982ae7f1b1d)
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 <linux/bitops.h>
314fc268d2SRandy Dunlap #include <linux/capability.h>
321da177e4SLinus Torvalds #include <linux/module.h>
331da177e4SLinus Torvalds #include <linux/types.h>
341da177e4SLinus Torvalds #include <linux/kernel.h>
351da177e4SLinus Torvalds #include <linux/string.h>
361da177e4SLinus Torvalds #include <linux/mm.h>
371da177e4SLinus Torvalds #include <linux/socket.h>
381da177e4SLinus Torvalds #include <linux/sockios.h>
391da177e4SLinus Torvalds #include <linux/in.h>
401da177e4SLinus Torvalds #include <linux/errno.h>
411da177e4SLinus Torvalds #include <linux/interrupt.h>
421823730fSThomas Graf #include <linux/if_addr.h>
431da177e4SLinus Torvalds #include <linux/if_ether.h>
441da177e4SLinus Torvalds #include <linux/inet.h>
451da177e4SLinus Torvalds #include <linux/netdevice.h>
461da177e4SLinus Torvalds #include <linux/etherdevice.h>
471da177e4SLinus Torvalds #include <linux/skbuff.h>
481da177e4SLinus Torvalds #include <linux/init.h>
491da177e4SLinus Torvalds #include <linux/notifier.h>
501da177e4SLinus Torvalds #include <linux/inetdevice.h>
511da177e4SLinus Torvalds #include <linux/igmp.h>
525a0e3ad6STejun Heo #include <linux/slab.h>
53fd23c3b3SDavid S. Miller #include <linux/hash.h>
541da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
551da177e4SLinus Torvalds #include <linux/sysctl.h>
561da177e4SLinus Torvalds #endif
571da177e4SLinus Torvalds #include <linux/kmod.h>
581da177e4SLinus Torvalds 
5914c85021SArnaldo Carvalho de Melo #include <net/arp.h>
601da177e4SLinus Torvalds #include <net/ip.h>
611da177e4SLinus Torvalds #include <net/route.h>
621da177e4SLinus Torvalds #include <net/ip_fib.h>
6363f3444fSThomas Graf #include <net/rtnetlink.h>
64752d14dcSPavel Emelyanov #include <net/net_namespace.h>
651da177e4SLinus Torvalds 
66406b6f97SDavid S. Miller #include "fib_lookup.h"
67406b6f97SDavid S. Miller 
680027ba84SAdrian Bunk static struct ipv4_devconf ipv4_devconf = {
6942f811b8SHerbert Xu 	.data = {
7002291680SEric W. Biederman 		[IPV4_DEVCONF_ACCEPT_REDIRECTS - 1] = 1,
7102291680SEric W. Biederman 		[IPV4_DEVCONF_SEND_REDIRECTS - 1] = 1,
7202291680SEric W. Biederman 		[IPV4_DEVCONF_SECURE_REDIRECTS - 1] = 1,
7302291680SEric W. Biederman 		[IPV4_DEVCONF_SHARED_MEDIA - 1] = 1,
7442f811b8SHerbert Xu 	},
751da177e4SLinus Torvalds };
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds static struct ipv4_devconf ipv4_devconf_dflt = {
7842f811b8SHerbert Xu 	.data = {
7902291680SEric W. Biederman 		[IPV4_DEVCONF_ACCEPT_REDIRECTS - 1] = 1,
8002291680SEric W. Biederman 		[IPV4_DEVCONF_SEND_REDIRECTS - 1] = 1,
8102291680SEric W. Biederman 		[IPV4_DEVCONF_SECURE_REDIRECTS - 1] = 1,
8202291680SEric W. Biederman 		[IPV4_DEVCONF_SHARED_MEDIA - 1] = 1,
8302291680SEric W. Biederman 		[IPV4_DEVCONF_ACCEPT_SOURCE_ROUTE - 1] = 1,
8442f811b8SHerbert Xu 	},
851da177e4SLinus Torvalds };
861da177e4SLinus Torvalds 
879355bbd6SPavel Emelyanov #define IPV4_DEVCONF_DFLT(net, attr) \
889355bbd6SPavel Emelyanov 	IPV4_DEVCONF((*net->ipv4.devconf_dflt), attr)
8942f811b8SHerbert Xu 
90ef7c79edSPatrick McHardy static const struct nla_policy ifa_ipv4_policy[IFA_MAX+1] = {
915c753978SThomas Graf 	[IFA_LOCAL]     	= { .type = NLA_U32 },
925c753978SThomas Graf 	[IFA_ADDRESS]   	= { .type = NLA_U32 },
935c753978SThomas Graf 	[IFA_BROADCAST] 	= { .type = NLA_U32 },
945176f91eSThomas Graf 	[IFA_LABEL]     	= { .type = NLA_STRING, .len = IFNAMSIZ - 1 },
955c753978SThomas Graf };
965c753978SThomas Graf 
97fd23c3b3SDavid S. Miller /* inet_addr_hash's shifting is dependent upon this IN4_ADDR_HSIZE
98fd23c3b3SDavid S. Miller  * value.  So if you change this define, make appropriate changes to
99fd23c3b3SDavid S. Miller  * inet_addr_hash as well.
100fd23c3b3SDavid S. Miller  */
101fd23c3b3SDavid S. Miller #define IN4_ADDR_HSIZE	256
102fd23c3b3SDavid S. Miller static struct hlist_head inet_addr_lst[IN4_ADDR_HSIZE];
103fd23c3b3SDavid S. Miller static DEFINE_SPINLOCK(inet_addr_hash_lock);
104fd23c3b3SDavid S. Miller 
105fd23c3b3SDavid S. Miller static inline unsigned int inet_addr_hash(struct net *net, __be32 addr)
106fd23c3b3SDavid S. Miller {
107fd23c3b3SDavid S. Miller 	u32 val = (__force u32) addr ^ hash_ptr(net, 8);
108fd23c3b3SDavid S. Miller 
109fd23c3b3SDavid S. Miller 	return ((val ^ (val >> 8) ^ (val >> 16) ^ (val >> 24)) &
110fd23c3b3SDavid S. Miller 		(IN4_ADDR_HSIZE - 1));
111fd23c3b3SDavid S. Miller }
112fd23c3b3SDavid S. Miller 
113fd23c3b3SDavid S. Miller static void inet_hash_insert(struct net *net, struct in_ifaddr *ifa)
114fd23c3b3SDavid S. Miller {
115e066008bSDavid S. Miller 	unsigned int hash = inet_addr_hash(net, ifa->ifa_local);
116fd23c3b3SDavid S. Miller 
117fd23c3b3SDavid S. Miller 	spin_lock(&inet_addr_hash_lock);
118fd23c3b3SDavid S. Miller 	hlist_add_head_rcu(&ifa->hash, &inet_addr_lst[hash]);
119fd23c3b3SDavid S. Miller 	spin_unlock(&inet_addr_hash_lock);
120fd23c3b3SDavid S. Miller }
121fd23c3b3SDavid S. Miller 
122fd23c3b3SDavid S. Miller static void inet_hash_remove(struct in_ifaddr *ifa)
123fd23c3b3SDavid S. Miller {
124fd23c3b3SDavid S. Miller 	spin_lock(&inet_addr_hash_lock);
125fd23c3b3SDavid S. Miller 	hlist_del_init_rcu(&ifa->hash);
126fd23c3b3SDavid S. Miller 	spin_unlock(&inet_addr_hash_lock);
127fd23c3b3SDavid S. Miller }
128fd23c3b3SDavid S. Miller 
1299435eb1cSDavid S. Miller /**
1309435eb1cSDavid S. Miller  * __ip_dev_find - find the first device with a given source address.
1319435eb1cSDavid S. Miller  * @net: the net namespace
1329435eb1cSDavid S. Miller  * @addr: the source address
1339435eb1cSDavid S. Miller  * @devref: if true, take a reference on the found device
1349435eb1cSDavid S. Miller  *
1359435eb1cSDavid S. Miller  * If a caller uses devref=false, it should be protected by RCU, or RTNL
1369435eb1cSDavid S. Miller  */
1379435eb1cSDavid S. Miller struct net_device *__ip_dev_find(struct net *net, __be32 addr, bool devref)
1389435eb1cSDavid S. Miller {
1399435eb1cSDavid S. Miller 	unsigned int hash = inet_addr_hash(net, addr);
1409435eb1cSDavid S. Miller 	struct net_device *result = NULL;
1419435eb1cSDavid S. Miller 	struct in_ifaddr *ifa;
1429435eb1cSDavid S. Miller 	struct hlist_node *node;
1439435eb1cSDavid S. Miller 
1449435eb1cSDavid S. Miller 	rcu_read_lock();
1459435eb1cSDavid S. Miller 	hlist_for_each_entry_rcu(ifa, node, &inet_addr_lst[hash], hash) {
1469435eb1cSDavid S. Miller 		struct net_device *dev = ifa->ifa_dev->dev;
1479435eb1cSDavid S. Miller 
1489435eb1cSDavid S. Miller 		if (!net_eq(dev_net(dev), net))
1499435eb1cSDavid S. Miller 			continue;
150e066008bSDavid S. Miller 		if (ifa->ifa_local == addr) {
1519435eb1cSDavid S. Miller 			result = dev;
1529435eb1cSDavid S. Miller 			break;
1539435eb1cSDavid S. Miller 		}
1549435eb1cSDavid S. Miller 	}
155406b6f97SDavid S. Miller 	if (!result) {
156406b6f97SDavid S. Miller 		struct flowi4 fl4 = { .daddr = addr };
157406b6f97SDavid S. Miller 		struct fib_result res = { 0 };
158406b6f97SDavid S. Miller 		struct fib_table *local;
159406b6f97SDavid S. Miller 
160406b6f97SDavid S. Miller 		/* Fallback to FIB local table so that communication
161406b6f97SDavid S. Miller 		 * over loopback subnets work.
162406b6f97SDavid S. Miller 		 */
163406b6f97SDavid S. Miller 		local = fib_get_table(net, RT_TABLE_LOCAL);
164406b6f97SDavid S. Miller 		if (local &&
165406b6f97SDavid S. Miller 		    !fib_table_lookup(local, &fl4, &res, FIB_LOOKUP_NOREF) &&
166406b6f97SDavid S. Miller 		    res.type == RTN_LOCAL)
167406b6f97SDavid S. Miller 			result = FIB_RES_DEV(res);
168406b6f97SDavid S. Miller 	}
1699435eb1cSDavid S. Miller 	if (result && devref)
1709435eb1cSDavid S. Miller 		dev_hold(result);
1719435eb1cSDavid S. Miller 	rcu_read_unlock();
1729435eb1cSDavid S. Miller 	return result;
1739435eb1cSDavid S. Miller }
1749435eb1cSDavid S. Miller EXPORT_SYMBOL(__ip_dev_find);
1759435eb1cSDavid S. Miller 
176d6062cbbSThomas Graf static void rtmsg_ifa(int event, struct in_ifaddr *, struct nlmsghdr *, u32);
1771da177e4SLinus Torvalds 
178e041c683SAlan Stern static BLOCKING_NOTIFIER_HEAD(inetaddr_chain);
1791da177e4SLinus Torvalds static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
1801da177e4SLinus Torvalds 			 int destroy);
1811da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
18266f27a52SPavel Emelyanov static void devinet_sysctl_register(struct in_device *idev);
18351602b2aSPavel Emelyanov static void devinet_sysctl_unregister(struct in_device *idev);
18451602b2aSPavel Emelyanov #else
18551602b2aSPavel Emelyanov static inline void devinet_sysctl_register(struct in_device *idev)
18651602b2aSPavel Emelyanov {
18751602b2aSPavel Emelyanov }
18851602b2aSPavel Emelyanov static inline void devinet_sysctl_unregister(struct in_device *idev)
18951602b2aSPavel Emelyanov {
19051602b2aSPavel Emelyanov }
1911da177e4SLinus Torvalds #endif
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds /* Locks all the inet devices. */
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds static struct in_ifaddr *inet_alloc_ifa(void)
1961da177e4SLinus Torvalds {
19793adcc80SAlexey Dobriyan 	return kzalloc(sizeof(struct in_ifaddr), GFP_KERNEL);
1981da177e4SLinus Torvalds }
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds static void inet_rcu_free_ifa(struct rcu_head *head)
2011da177e4SLinus Torvalds {
2021da177e4SLinus Torvalds 	struct in_ifaddr *ifa = container_of(head, struct in_ifaddr, rcu_head);
2031da177e4SLinus Torvalds 	if (ifa->ifa_dev)
2041da177e4SLinus Torvalds 		in_dev_put(ifa->ifa_dev);
2051da177e4SLinus Torvalds 	kfree(ifa);
2061da177e4SLinus Torvalds }
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds static inline void inet_free_ifa(struct in_ifaddr *ifa)
2091da177e4SLinus Torvalds {
2101da177e4SLinus Torvalds 	call_rcu(&ifa->rcu_head, inet_rcu_free_ifa);
2111da177e4SLinus Torvalds }
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds void in_dev_finish_destroy(struct in_device *idev)
2141da177e4SLinus Torvalds {
2151da177e4SLinus Torvalds 	struct net_device *dev = idev->dev;
2161da177e4SLinus Torvalds 
217547b792cSIlpo Järvinen 	WARN_ON(idev->ifa_list);
218547b792cSIlpo Järvinen 	WARN_ON(idev->mc_list);
2191da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG
220*91df42beSJoe Perches 	pr_debug("%s: %p=%s\n", __func__, idev, dev ? dev->name : "NIL");
2211da177e4SLinus Torvalds #endif
2221da177e4SLinus Torvalds 	dev_put(dev);
2231da177e4SLinus Torvalds 	if (!idev->dead)
2249f9354b9SEric Dumazet 		pr_err("Freeing alive in_device %p\n", idev);
2259f9354b9SEric Dumazet 	else
2261da177e4SLinus Torvalds 		kfree(idev);
2271da177e4SLinus Torvalds }
2289f9354b9SEric Dumazet EXPORT_SYMBOL(in_dev_finish_destroy);
2291da177e4SLinus Torvalds 
23071e27da9SHerbert Xu static struct in_device *inetdev_init(struct net_device *dev)
2311da177e4SLinus Torvalds {
2321da177e4SLinus Torvalds 	struct in_device *in_dev;
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds 	ASSERT_RTNL();
2351da177e4SLinus Torvalds 
2360da974f4SPanagiotis Issaris 	in_dev = kzalloc(sizeof(*in_dev), GFP_KERNEL);
2371da177e4SLinus Torvalds 	if (!in_dev)
2381da177e4SLinus Torvalds 		goto out;
239c346dca1SYOSHIFUJI Hideaki 	memcpy(&in_dev->cnf, dev_net(dev)->ipv4.devconf_dflt,
2409355bbd6SPavel Emelyanov 			sizeof(in_dev->cnf));
2411da177e4SLinus Torvalds 	in_dev->cnf.sysctl = NULL;
2421da177e4SLinus Torvalds 	in_dev->dev = dev;
2439f9354b9SEric Dumazet 	in_dev->arp_parms = neigh_parms_alloc(dev, &arp_tbl);
2449f9354b9SEric Dumazet 	if (!in_dev->arp_parms)
2451da177e4SLinus Torvalds 		goto out_kfree;
2460187bdfbSBen Hutchings 	if (IPV4_DEVCONF(in_dev->cnf, FORWARDING))
2470187bdfbSBen Hutchings 		dev_disable_lro(dev);
2481da177e4SLinus Torvalds 	/* Reference in_dev->dev */
2491da177e4SLinus Torvalds 	dev_hold(dev);
25030c4cf57SDavid L Stevens 	/* Account for reference dev->ip_ptr (below) */
2511da177e4SLinus Torvalds 	in_dev_hold(in_dev);
2521da177e4SLinus Torvalds 
25366f27a52SPavel Emelyanov 	devinet_sysctl_register(in_dev);
2541da177e4SLinus Torvalds 	ip_mc_init_dev(in_dev);
2551da177e4SLinus Torvalds 	if (dev->flags & IFF_UP)
2561da177e4SLinus Torvalds 		ip_mc_up(in_dev);
257483479ecSJarek Poplawski 
25830c4cf57SDavid L Stevens 	/* we can receive as soon as ip_ptr is set -- do this last */
259cf778b00SEric Dumazet 	rcu_assign_pointer(dev->ip_ptr, in_dev);
260483479ecSJarek Poplawski out:
2611da177e4SLinus Torvalds 	return in_dev;
2621da177e4SLinus Torvalds out_kfree:
2631da177e4SLinus Torvalds 	kfree(in_dev);
2641da177e4SLinus Torvalds 	in_dev = NULL;
2651da177e4SLinus Torvalds 	goto out;
2661da177e4SLinus Torvalds }
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds static void in_dev_rcu_put(struct rcu_head *head)
2691da177e4SLinus Torvalds {
2701da177e4SLinus Torvalds 	struct in_device *idev = container_of(head, struct in_device, rcu_head);
2711da177e4SLinus Torvalds 	in_dev_put(idev);
2721da177e4SLinus Torvalds }
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds static void inetdev_destroy(struct in_device *in_dev)
2751da177e4SLinus Torvalds {
2761da177e4SLinus Torvalds 	struct in_ifaddr *ifa;
2771da177e4SLinus Torvalds 	struct net_device *dev;
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds 	ASSERT_RTNL();
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds 	dev = in_dev->dev;
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds 	in_dev->dead = 1;
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds 	ip_mc_destroy_dev(in_dev);
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds 	while ((ifa = in_dev->ifa_list) != NULL) {
2881da177e4SLinus Torvalds 		inet_del_ifa(in_dev, &in_dev->ifa_list, 0);
2891da177e4SLinus Torvalds 		inet_free_ifa(ifa);
2901da177e4SLinus Torvalds 	}
2911da177e4SLinus Torvalds 
292a9b3cd7fSStephen Hemminger 	RCU_INIT_POINTER(dev->ip_ptr, NULL);
2931da177e4SLinus Torvalds 
29451602b2aSPavel Emelyanov 	devinet_sysctl_unregister(in_dev);
2951da177e4SLinus Torvalds 	neigh_parms_release(&arp_tbl, in_dev->arp_parms);
2961da177e4SLinus Torvalds 	arp_ifdown(dev);
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds 	call_rcu(&in_dev->rcu_head, in_dev_rcu_put);
2991da177e4SLinus Torvalds }
3001da177e4SLinus Torvalds 
301ff428d72SAl Viro int inet_addr_onlink(struct in_device *in_dev, __be32 a, __be32 b)
3021da177e4SLinus Torvalds {
3031da177e4SLinus Torvalds 	rcu_read_lock();
3041da177e4SLinus Torvalds 	for_primary_ifa(in_dev) {
3051da177e4SLinus Torvalds 		if (inet_ifa_match(a, ifa)) {
3061da177e4SLinus Torvalds 			if (!b || inet_ifa_match(b, ifa)) {
3071da177e4SLinus Torvalds 				rcu_read_unlock();
3081da177e4SLinus Torvalds 				return 1;
3091da177e4SLinus Torvalds 			}
3101da177e4SLinus Torvalds 		}
3111da177e4SLinus Torvalds 	} endfor_ifa(in_dev);
3121da177e4SLinus Torvalds 	rcu_read_unlock();
3131da177e4SLinus Torvalds 	return 0;
3141da177e4SLinus Torvalds }
3151da177e4SLinus Torvalds 
316d6062cbbSThomas Graf static void __inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
317d6062cbbSThomas Graf 			 int destroy, struct nlmsghdr *nlh, u32 pid)
3181da177e4SLinus Torvalds {
3198f937c60SHarald Welte 	struct in_ifaddr *promote = NULL;
3200ff60a45SJamal Hadi Salim 	struct in_ifaddr *ifa, *ifa1 = *ifap;
3210ff60a45SJamal Hadi Salim 	struct in_ifaddr *last_prim = in_dev->ifa_list;
3220ff60a45SJamal Hadi Salim 	struct in_ifaddr *prev_prom = NULL;
3230ff60a45SJamal Hadi Salim 	int do_promote = IN_DEV_PROMOTE_SECONDARIES(in_dev);
3241da177e4SLinus Torvalds 
3251da177e4SLinus Torvalds 	ASSERT_RTNL();
3261da177e4SLinus Torvalds 
3278f937c60SHarald Welte 	/* 1. Deleting primary ifaddr forces deletion all secondaries
3288f937c60SHarald Welte 	 * unless alias promotion is set
3298f937c60SHarald Welte 	 **/
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds 	if (!(ifa1->ifa_flags & IFA_F_SECONDARY)) {
3321da177e4SLinus Torvalds 		struct in_ifaddr **ifap1 = &ifa1->ifa_next;
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds 		while ((ifa = *ifap1) != NULL) {
3350ff60a45SJamal Hadi Salim 			if (!(ifa->ifa_flags & IFA_F_SECONDARY) &&
3360ff60a45SJamal Hadi Salim 			    ifa1->ifa_scope <= ifa->ifa_scope)
3370ff60a45SJamal Hadi Salim 				last_prim = ifa;
3380ff60a45SJamal Hadi Salim 
3391da177e4SLinus Torvalds 			if (!(ifa->ifa_flags & IFA_F_SECONDARY) ||
3401da177e4SLinus Torvalds 			    ifa1->ifa_mask != ifa->ifa_mask ||
3411da177e4SLinus Torvalds 			    !inet_ifa_match(ifa1->ifa_address, ifa)) {
3421da177e4SLinus Torvalds 				ifap1 = &ifa->ifa_next;
3430ff60a45SJamal Hadi Salim 				prev_prom = ifa;
3441da177e4SLinus Torvalds 				continue;
3451da177e4SLinus Torvalds 			}
3461da177e4SLinus Torvalds 
3470ff60a45SJamal Hadi Salim 			if (!do_promote) {
348fd23c3b3SDavid S. Miller 				inet_hash_remove(ifa);
3491da177e4SLinus Torvalds 				*ifap1 = ifa->ifa_next;
3501da177e4SLinus Torvalds 
351d6062cbbSThomas Graf 				rtmsg_ifa(RTM_DELADDR, ifa, nlh, pid);
352e041c683SAlan Stern 				blocking_notifier_call_chain(&inetaddr_chain,
353e041c683SAlan Stern 						NETDEV_DOWN, ifa);
3541da177e4SLinus Torvalds 				inet_free_ifa(ifa);
3558f937c60SHarald Welte 			} else {
3568f937c60SHarald Welte 				promote = ifa;
3578f937c60SHarald Welte 				break;
3588f937c60SHarald Welte 			}
3591da177e4SLinus Torvalds 		}
3601da177e4SLinus Torvalds 	}
3611da177e4SLinus Torvalds 
3622d230e2bSJulian Anastasov 	/* On promotion all secondaries from subnet are changing
3632d230e2bSJulian Anastasov 	 * the primary IP, we must remove all their routes silently
3642d230e2bSJulian Anastasov 	 * and later to add them back with new prefsrc. Do this
3652d230e2bSJulian Anastasov 	 * while all addresses are on the device list.
3662d230e2bSJulian Anastasov 	 */
3672d230e2bSJulian Anastasov 	for (ifa = promote; ifa; ifa = ifa->ifa_next) {
3682d230e2bSJulian Anastasov 		if (ifa1->ifa_mask == ifa->ifa_mask &&
3692d230e2bSJulian Anastasov 		    inet_ifa_match(ifa1->ifa_address, ifa))
3702d230e2bSJulian Anastasov 			fib_del_ifaddr(ifa, ifa1);
3712d230e2bSJulian Anastasov 	}
3722d230e2bSJulian Anastasov 
3731da177e4SLinus Torvalds 	/* 2. Unlink it */
3741da177e4SLinus Torvalds 
3751da177e4SLinus Torvalds 	*ifap = ifa1->ifa_next;
376fd23c3b3SDavid S. Miller 	inet_hash_remove(ifa1);
3771da177e4SLinus Torvalds 
3781da177e4SLinus Torvalds 	/* 3. Announce address deletion */
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds 	/* Send message first, then call notifier.
3811da177e4SLinus Torvalds 	   At first sight, FIB update triggered by notifier
3821da177e4SLinus Torvalds 	   will refer to already deleted ifaddr, that could confuse
3831da177e4SLinus Torvalds 	   netlink listeners. It is not true: look, gated sees
3841da177e4SLinus Torvalds 	   that route deleted and if it still thinks that ifaddr
3851da177e4SLinus Torvalds 	   is valid, it will try to restore deleted routes... Grr.
3861da177e4SLinus Torvalds 	   So that, this order is correct.
3871da177e4SLinus Torvalds 	 */
388d6062cbbSThomas Graf 	rtmsg_ifa(RTM_DELADDR, ifa1, nlh, pid);
389e041c683SAlan Stern 	blocking_notifier_call_chain(&inetaddr_chain, NETDEV_DOWN, ifa1);
3900ff60a45SJamal Hadi Salim 
3910ff60a45SJamal Hadi Salim 	if (promote) {
39204024b93SJulian Anastasov 		struct in_ifaddr *next_sec = promote->ifa_next;
3930ff60a45SJamal Hadi Salim 
3940ff60a45SJamal Hadi Salim 		if (prev_prom) {
3950ff60a45SJamal Hadi Salim 			prev_prom->ifa_next = promote->ifa_next;
3960ff60a45SJamal Hadi Salim 			promote->ifa_next = last_prim->ifa_next;
3970ff60a45SJamal Hadi Salim 			last_prim->ifa_next = promote;
3980ff60a45SJamal Hadi Salim 		}
3990ff60a45SJamal Hadi Salim 
4000ff60a45SJamal Hadi Salim 		promote->ifa_flags &= ~IFA_F_SECONDARY;
401d6062cbbSThomas Graf 		rtmsg_ifa(RTM_NEWADDR, promote, nlh, pid);
402e041c683SAlan Stern 		blocking_notifier_call_chain(&inetaddr_chain,
403e041c683SAlan Stern 				NETDEV_UP, promote);
40404024b93SJulian Anastasov 		for (ifa = next_sec; ifa; ifa = ifa->ifa_next) {
4050ff60a45SJamal Hadi Salim 			if (ifa1->ifa_mask != ifa->ifa_mask ||
4060ff60a45SJamal Hadi Salim 			    !inet_ifa_match(ifa1->ifa_address, ifa))
4070ff60a45SJamal Hadi Salim 					continue;
4080ff60a45SJamal Hadi Salim 			fib_add_ifaddr(ifa);
4090ff60a45SJamal Hadi Salim 		}
4100ff60a45SJamal Hadi Salim 
4110ff60a45SJamal Hadi Salim 	}
4126363097cSHerbert Xu 	if (destroy)
4131da177e4SLinus Torvalds 		inet_free_ifa(ifa1);
4141da177e4SLinus Torvalds }
4151da177e4SLinus Torvalds 
416d6062cbbSThomas Graf static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
417d6062cbbSThomas Graf 			 int destroy)
418d6062cbbSThomas Graf {
419d6062cbbSThomas Graf 	__inet_del_ifa(in_dev, ifap, destroy, NULL, 0);
420d6062cbbSThomas Graf }
421d6062cbbSThomas Graf 
422d6062cbbSThomas Graf static int __inet_insert_ifa(struct in_ifaddr *ifa, struct nlmsghdr *nlh,
423d6062cbbSThomas Graf 			     u32 pid)
4241da177e4SLinus Torvalds {
4251da177e4SLinus Torvalds 	struct in_device *in_dev = ifa->ifa_dev;
4261da177e4SLinus Torvalds 	struct in_ifaddr *ifa1, **ifap, **last_primary;
4271da177e4SLinus Torvalds 
4281da177e4SLinus Torvalds 	ASSERT_RTNL();
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds 	if (!ifa->ifa_local) {
4311da177e4SLinus Torvalds 		inet_free_ifa(ifa);
4321da177e4SLinus Torvalds 		return 0;
4331da177e4SLinus Torvalds 	}
4341da177e4SLinus Torvalds 
4351da177e4SLinus Torvalds 	ifa->ifa_flags &= ~IFA_F_SECONDARY;
4361da177e4SLinus Torvalds 	last_primary = &in_dev->ifa_list;
4371da177e4SLinus Torvalds 
4381da177e4SLinus Torvalds 	for (ifap = &in_dev->ifa_list; (ifa1 = *ifap) != NULL;
4391da177e4SLinus Torvalds 	     ifap = &ifa1->ifa_next) {
4401da177e4SLinus Torvalds 		if (!(ifa1->ifa_flags & IFA_F_SECONDARY) &&
4411da177e4SLinus Torvalds 		    ifa->ifa_scope <= ifa1->ifa_scope)
4421da177e4SLinus Torvalds 			last_primary = &ifa1->ifa_next;
4431da177e4SLinus Torvalds 		if (ifa1->ifa_mask == ifa->ifa_mask &&
4441da177e4SLinus Torvalds 		    inet_ifa_match(ifa1->ifa_address, ifa)) {
4451da177e4SLinus Torvalds 			if (ifa1->ifa_local == ifa->ifa_local) {
4461da177e4SLinus Torvalds 				inet_free_ifa(ifa);
4471da177e4SLinus Torvalds 				return -EEXIST;
4481da177e4SLinus Torvalds 			}
4491da177e4SLinus Torvalds 			if (ifa1->ifa_scope != ifa->ifa_scope) {
4501da177e4SLinus Torvalds 				inet_free_ifa(ifa);
4511da177e4SLinus Torvalds 				return -EINVAL;
4521da177e4SLinus Torvalds 			}
4531da177e4SLinus Torvalds 			ifa->ifa_flags |= IFA_F_SECONDARY;
4541da177e4SLinus Torvalds 		}
4551da177e4SLinus Torvalds 	}
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds 	if (!(ifa->ifa_flags & IFA_F_SECONDARY)) {
4581da177e4SLinus Torvalds 		net_srandom(ifa->ifa_local);
4591da177e4SLinus Torvalds 		ifap = last_primary;
4601da177e4SLinus Torvalds 	}
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds 	ifa->ifa_next = *ifap;
4631da177e4SLinus Torvalds 	*ifap = ifa;
4641da177e4SLinus Torvalds 
465fd23c3b3SDavid S. Miller 	inet_hash_insert(dev_net(in_dev->dev), ifa);
466fd23c3b3SDavid S. Miller 
4671da177e4SLinus Torvalds 	/* Send message first, then call notifier.
4681da177e4SLinus Torvalds 	   Notifier will trigger FIB update, so that
4691da177e4SLinus Torvalds 	   listeners of netlink will know about new ifaddr */
470d6062cbbSThomas Graf 	rtmsg_ifa(RTM_NEWADDR, ifa, nlh, pid);
471e041c683SAlan Stern 	blocking_notifier_call_chain(&inetaddr_chain, NETDEV_UP, ifa);
4721da177e4SLinus Torvalds 
4731da177e4SLinus Torvalds 	return 0;
4741da177e4SLinus Torvalds }
4751da177e4SLinus Torvalds 
476d6062cbbSThomas Graf static int inet_insert_ifa(struct in_ifaddr *ifa)
477d6062cbbSThomas Graf {
478d6062cbbSThomas Graf 	return __inet_insert_ifa(ifa, NULL, 0);
479d6062cbbSThomas Graf }
480d6062cbbSThomas Graf 
4811da177e4SLinus Torvalds static int inet_set_ifa(struct net_device *dev, struct in_ifaddr *ifa)
4821da177e4SLinus Torvalds {
483e5ed6399SHerbert Xu 	struct in_device *in_dev = __in_dev_get_rtnl(dev);
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds 	ASSERT_RTNL();
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds 	if (!in_dev) {
4881da177e4SLinus Torvalds 		inet_free_ifa(ifa);
4891da177e4SLinus Torvalds 		return -ENOBUFS;
4901da177e4SLinus Torvalds 	}
49171e27da9SHerbert Xu 	ipv4_devconf_setall(in_dev);
4921da177e4SLinus Torvalds 	if (ifa->ifa_dev != in_dev) {
493547b792cSIlpo Järvinen 		WARN_ON(ifa->ifa_dev);
4941da177e4SLinus Torvalds 		in_dev_hold(in_dev);
4951da177e4SLinus Torvalds 		ifa->ifa_dev = in_dev;
4961da177e4SLinus Torvalds 	}
497f97c1e0cSJoe Perches 	if (ipv4_is_loopback(ifa->ifa_local))
4981da177e4SLinus Torvalds 		ifa->ifa_scope = RT_SCOPE_HOST;
4991da177e4SLinus Torvalds 	return inet_insert_ifa(ifa);
5001da177e4SLinus Torvalds }
5011da177e4SLinus Torvalds 
5028723e1b4SEric Dumazet /* Caller must hold RCU or RTNL :
5038723e1b4SEric Dumazet  * We dont take a reference on found in_device
5048723e1b4SEric Dumazet  */
5057fee0ca2SDenis V. Lunev struct in_device *inetdev_by_index(struct net *net, int ifindex)
5061da177e4SLinus Torvalds {
5071da177e4SLinus Torvalds 	struct net_device *dev;
5081da177e4SLinus Torvalds 	struct in_device *in_dev = NULL;
509c148fc2eSEric Dumazet 
510c148fc2eSEric Dumazet 	rcu_read_lock();
511c148fc2eSEric Dumazet 	dev = dev_get_by_index_rcu(net, ifindex);
5121da177e4SLinus Torvalds 	if (dev)
5138723e1b4SEric Dumazet 		in_dev = rcu_dereference_rtnl(dev->ip_ptr);
514c148fc2eSEric Dumazet 	rcu_read_unlock();
5151da177e4SLinus Torvalds 	return in_dev;
5161da177e4SLinus Torvalds }
5179f9354b9SEric Dumazet EXPORT_SYMBOL(inetdev_by_index);
5181da177e4SLinus Torvalds 
5191da177e4SLinus Torvalds /* Called only from RTNL semaphored context. No locks. */
5201da177e4SLinus Torvalds 
52160cad5daSAl Viro struct in_ifaddr *inet_ifa_byprefix(struct in_device *in_dev, __be32 prefix,
52260cad5daSAl Viro 				    __be32 mask)
5231da177e4SLinus Torvalds {
5241da177e4SLinus Torvalds 	ASSERT_RTNL();
5251da177e4SLinus Torvalds 
5261da177e4SLinus Torvalds 	for_primary_ifa(in_dev) {
5271da177e4SLinus Torvalds 		if (ifa->ifa_mask == mask && inet_ifa_match(prefix, ifa))
5281da177e4SLinus Torvalds 			return ifa;
5291da177e4SLinus Torvalds 	} endfor_ifa(in_dev);
5301da177e4SLinus Torvalds 	return NULL;
5311da177e4SLinus Torvalds }
5321da177e4SLinus Torvalds 
5331da177e4SLinus Torvalds static int inet_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
5341da177e4SLinus Torvalds {
5353b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
536dfdd5fd4SThomas Graf 	struct nlattr *tb[IFA_MAX+1];
5371da177e4SLinus Torvalds 	struct in_device *in_dev;
538dfdd5fd4SThomas Graf 	struct ifaddrmsg *ifm;
5391da177e4SLinus Torvalds 	struct in_ifaddr *ifa, **ifap;
540dfdd5fd4SThomas Graf 	int err = -EINVAL;
5411da177e4SLinus Torvalds 
5421da177e4SLinus Torvalds 	ASSERT_RTNL();
5431da177e4SLinus Torvalds 
544dfdd5fd4SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv4_policy);
545dfdd5fd4SThomas Graf 	if (err < 0)
546dfdd5fd4SThomas Graf 		goto errout;
547dfdd5fd4SThomas Graf 
548dfdd5fd4SThomas Graf 	ifm = nlmsg_data(nlh);
5497fee0ca2SDenis V. Lunev 	in_dev = inetdev_by_index(net, ifm->ifa_index);
550dfdd5fd4SThomas Graf 	if (in_dev == NULL) {
551dfdd5fd4SThomas Graf 		err = -ENODEV;
552dfdd5fd4SThomas Graf 		goto errout;
553dfdd5fd4SThomas Graf 	}
554dfdd5fd4SThomas Graf 
5551da177e4SLinus Torvalds 	for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
5561da177e4SLinus Torvalds 	     ifap = &ifa->ifa_next) {
557dfdd5fd4SThomas Graf 		if (tb[IFA_LOCAL] &&
558a7a628c4SAl Viro 		    ifa->ifa_local != nla_get_be32(tb[IFA_LOCAL]))
5591da177e4SLinus Torvalds 			continue;
560dfdd5fd4SThomas Graf 
561dfdd5fd4SThomas Graf 		if (tb[IFA_LABEL] && nla_strcmp(tb[IFA_LABEL], ifa->ifa_label))
562dfdd5fd4SThomas Graf 			continue;
563dfdd5fd4SThomas Graf 
564dfdd5fd4SThomas Graf 		if (tb[IFA_ADDRESS] &&
565dfdd5fd4SThomas Graf 		    (ifm->ifa_prefixlen != ifa->ifa_prefixlen ||
566a7a628c4SAl Viro 		    !inet_ifa_match(nla_get_be32(tb[IFA_ADDRESS]), ifa)))
567dfdd5fd4SThomas Graf 			continue;
568dfdd5fd4SThomas Graf 
569d6062cbbSThomas Graf 		__inet_del_ifa(in_dev, ifap, 1, nlh, NETLINK_CB(skb).pid);
5701da177e4SLinus Torvalds 		return 0;
5711da177e4SLinus Torvalds 	}
572dfdd5fd4SThomas Graf 
573dfdd5fd4SThomas Graf 	err = -EADDRNOTAVAIL;
574dfdd5fd4SThomas Graf errout:
575dfdd5fd4SThomas Graf 	return err;
5761da177e4SLinus Torvalds }
5771da177e4SLinus Torvalds 
5784b8aa9abSDenis V. Lunev static struct in_ifaddr *rtm_to_ifaddr(struct net *net, struct nlmsghdr *nlh)
5791da177e4SLinus Torvalds {
5805c753978SThomas Graf 	struct nlattr *tb[IFA_MAX+1];
5815c753978SThomas Graf 	struct in_ifaddr *ifa;
5825c753978SThomas Graf 	struct ifaddrmsg *ifm;
5831da177e4SLinus Torvalds 	struct net_device *dev;
5841da177e4SLinus Torvalds 	struct in_device *in_dev;
5857b218574SDenis V. Lunev 	int err;
5861da177e4SLinus Torvalds 
5875c753978SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv4_policy);
5885c753978SThomas Graf 	if (err < 0)
5895c753978SThomas Graf 		goto errout;
5901da177e4SLinus Torvalds 
5915c753978SThomas Graf 	ifm = nlmsg_data(nlh);
592c4e38f41SEvgeniy Polyakov 	err = -EINVAL;
5937b218574SDenis V. Lunev 	if (ifm->ifa_prefixlen > 32 || tb[IFA_LOCAL] == NULL)
5945c753978SThomas Graf 		goto errout;
5951da177e4SLinus Torvalds 
5964b8aa9abSDenis V. Lunev 	dev = __dev_get_by_index(net, ifm->ifa_index);
5975c753978SThomas Graf 	err = -ENODEV;
5987b218574SDenis V. Lunev 	if (dev == NULL)
5995c753978SThomas Graf 		goto errout;
6001da177e4SLinus Torvalds 
6015c753978SThomas Graf 	in_dev = __in_dev_get_rtnl(dev);
6025c753978SThomas Graf 	err = -ENOBUFS;
6037b218574SDenis V. Lunev 	if (in_dev == NULL)
6045c753978SThomas Graf 		goto errout;
60571e27da9SHerbert Xu 
6065c753978SThomas Graf 	ifa = inet_alloc_ifa();
6077b218574SDenis V. Lunev 	if (ifa == NULL)
6085c753978SThomas Graf 		/*
6095c753978SThomas Graf 		 * A potential indev allocation can be left alive, it stays
6105c753978SThomas Graf 		 * assigned to its device and is destroy with it.
6115c753978SThomas Graf 		 */
6125c753978SThomas Graf 		goto errout;
6135c753978SThomas Graf 
614a4e65d36SPavel Emelyanov 	ipv4_devconf_setall(in_dev);
6155c753978SThomas Graf 	in_dev_hold(in_dev);
6165c753978SThomas Graf 
6175c753978SThomas Graf 	if (tb[IFA_ADDRESS] == NULL)
6185c753978SThomas Graf 		tb[IFA_ADDRESS] = tb[IFA_LOCAL];
6195c753978SThomas Graf 
620fd23c3b3SDavid S. Miller 	INIT_HLIST_NODE(&ifa->hash);
6211da177e4SLinus Torvalds 	ifa->ifa_prefixlen = ifm->ifa_prefixlen;
6221da177e4SLinus Torvalds 	ifa->ifa_mask = inet_make_mask(ifm->ifa_prefixlen);
6231da177e4SLinus Torvalds 	ifa->ifa_flags = ifm->ifa_flags;
6241da177e4SLinus Torvalds 	ifa->ifa_scope = ifm->ifa_scope;
6251da177e4SLinus Torvalds 	ifa->ifa_dev = in_dev;
6265c753978SThomas Graf 
627a7a628c4SAl Viro 	ifa->ifa_local = nla_get_be32(tb[IFA_LOCAL]);
628a7a628c4SAl Viro 	ifa->ifa_address = nla_get_be32(tb[IFA_ADDRESS]);
6295c753978SThomas Graf 
6305c753978SThomas Graf 	if (tb[IFA_BROADCAST])
631a7a628c4SAl Viro 		ifa->ifa_broadcast = nla_get_be32(tb[IFA_BROADCAST]);
6325c753978SThomas Graf 
6335c753978SThomas Graf 	if (tb[IFA_LABEL])
6345c753978SThomas Graf 		nla_strlcpy(ifa->ifa_label, tb[IFA_LABEL], IFNAMSIZ);
6351da177e4SLinus Torvalds 	else
6361da177e4SLinus Torvalds 		memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
6371da177e4SLinus Torvalds 
6385c753978SThomas Graf 	return ifa;
6395c753978SThomas Graf 
6405c753978SThomas Graf errout:
6415c753978SThomas Graf 	return ERR_PTR(err);
6425c753978SThomas Graf }
6435c753978SThomas Graf 
6445c753978SThomas Graf static int inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
6455c753978SThomas Graf {
6463b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
6475c753978SThomas Graf 	struct in_ifaddr *ifa;
6485c753978SThomas Graf 
6495c753978SThomas Graf 	ASSERT_RTNL();
6505c753978SThomas Graf 
6514b8aa9abSDenis V. Lunev 	ifa = rtm_to_ifaddr(net, nlh);
6525c753978SThomas Graf 	if (IS_ERR(ifa))
6535c753978SThomas Graf 		return PTR_ERR(ifa);
6545c753978SThomas Graf 
655d6062cbbSThomas Graf 	return __inet_insert_ifa(ifa, nlh, NETLINK_CB(skb).pid);
6561da177e4SLinus Torvalds }
6571da177e4SLinus Torvalds 
6581da177e4SLinus Torvalds /*
6591da177e4SLinus Torvalds  *	Determine a default network mask, based on the IP address.
6601da177e4SLinus Torvalds  */
6611da177e4SLinus Torvalds 
6629f9354b9SEric Dumazet static inline int inet_abc_len(__be32 addr)
6631da177e4SLinus Torvalds {
6641da177e4SLinus Torvalds 	int rc = -1;	/* Something else, probably a multicast. */
6651da177e4SLinus Torvalds 
666f97c1e0cSJoe Perches 	if (ipv4_is_zeronet(addr))
6671da177e4SLinus Torvalds 		rc = 0;
6681da177e4SLinus Torvalds 	else {
669714e85beSAl Viro 		__u32 haddr = ntohl(addr);
6701da177e4SLinus Torvalds 
671714e85beSAl Viro 		if (IN_CLASSA(haddr))
6721da177e4SLinus Torvalds 			rc = 8;
673714e85beSAl Viro 		else if (IN_CLASSB(haddr))
6741da177e4SLinus Torvalds 			rc = 16;
675714e85beSAl Viro 		else if (IN_CLASSC(haddr))
6761da177e4SLinus Torvalds 			rc = 24;
6771da177e4SLinus Torvalds 	}
6781da177e4SLinus Torvalds 
6791da177e4SLinus Torvalds 	return rc;
6801da177e4SLinus Torvalds }
6811da177e4SLinus Torvalds 
6821da177e4SLinus Torvalds 
683e5b13cb1SDenis V. Lunev int devinet_ioctl(struct net *net, unsigned int cmd, void __user *arg)
6841da177e4SLinus Torvalds {
6851da177e4SLinus Torvalds 	struct ifreq ifr;
6861da177e4SLinus Torvalds 	struct sockaddr_in sin_orig;
6871da177e4SLinus Torvalds 	struct sockaddr_in *sin = (struct sockaddr_in *)&ifr.ifr_addr;
6881da177e4SLinus Torvalds 	struct in_device *in_dev;
6891da177e4SLinus Torvalds 	struct in_ifaddr **ifap = NULL;
6901da177e4SLinus Torvalds 	struct in_ifaddr *ifa = NULL;
6911da177e4SLinus Torvalds 	struct net_device *dev;
6921da177e4SLinus Torvalds 	char *colon;
6931da177e4SLinus Torvalds 	int ret = -EFAULT;
6941da177e4SLinus Torvalds 	int tryaddrmatch = 0;
6951da177e4SLinus Torvalds 
6961da177e4SLinus Torvalds 	/*
6971da177e4SLinus Torvalds 	 *	Fetch the caller's info block into kernel space
6981da177e4SLinus Torvalds 	 */
6991da177e4SLinus Torvalds 
7001da177e4SLinus Torvalds 	if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
7011da177e4SLinus Torvalds 		goto out;
7021da177e4SLinus Torvalds 	ifr.ifr_name[IFNAMSIZ - 1] = 0;
7031da177e4SLinus Torvalds 
7041da177e4SLinus Torvalds 	/* save original address for comparison */
7051da177e4SLinus Torvalds 	memcpy(&sin_orig, sin, sizeof(*sin));
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds 	colon = strchr(ifr.ifr_name, ':');
7081da177e4SLinus Torvalds 	if (colon)
7091da177e4SLinus Torvalds 		*colon = 0;
7101da177e4SLinus Torvalds 
711e5b13cb1SDenis V. Lunev 	dev_load(net, ifr.ifr_name);
7121da177e4SLinus Torvalds 
7131da177e4SLinus Torvalds 	switch (cmd) {
7141da177e4SLinus Torvalds 	case SIOCGIFADDR:	/* Get interface address */
7151da177e4SLinus Torvalds 	case SIOCGIFBRDADDR:	/* Get the broadcast address */
7161da177e4SLinus Torvalds 	case SIOCGIFDSTADDR:	/* Get the destination address */
7171da177e4SLinus Torvalds 	case SIOCGIFNETMASK:	/* Get the netmask for the interface */
7181da177e4SLinus Torvalds 		/* Note that these ioctls will not sleep,
7191da177e4SLinus Torvalds 		   so that we do not impose a lock.
7201da177e4SLinus Torvalds 		   One day we will be forced to put shlock here (I mean SMP)
7211da177e4SLinus Torvalds 		 */
7221da177e4SLinus Torvalds 		tryaddrmatch = (sin_orig.sin_family == AF_INET);
7231da177e4SLinus Torvalds 		memset(sin, 0, sizeof(*sin));
7241da177e4SLinus Torvalds 		sin->sin_family = AF_INET;
7251da177e4SLinus Torvalds 		break;
7261da177e4SLinus Torvalds 
7271da177e4SLinus Torvalds 	case SIOCSIFFLAGS:
7281da177e4SLinus Torvalds 		ret = -EACCES;
7291da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
7301da177e4SLinus Torvalds 			goto out;
7311da177e4SLinus Torvalds 		break;
7321da177e4SLinus Torvalds 	case SIOCSIFADDR:	/* Set interface address (and family) */
7331da177e4SLinus Torvalds 	case SIOCSIFBRDADDR:	/* Set the broadcast address */
7341da177e4SLinus Torvalds 	case SIOCSIFDSTADDR:	/* Set the destination address */
7351da177e4SLinus Torvalds 	case SIOCSIFNETMASK: 	/* Set the netmask for the interface */
7361da177e4SLinus Torvalds 		ret = -EACCES;
7371da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
7381da177e4SLinus Torvalds 			goto out;
7391da177e4SLinus Torvalds 		ret = -EINVAL;
7401da177e4SLinus Torvalds 		if (sin->sin_family != AF_INET)
7411da177e4SLinus Torvalds 			goto out;
7421da177e4SLinus Torvalds 		break;
7431da177e4SLinus Torvalds 	default:
7441da177e4SLinus Torvalds 		ret = -EINVAL;
7451da177e4SLinus Torvalds 		goto out;
7461da177e4SLinus Torvalds 	}
7471da177e4SLinus Torvalds 
7481da177e4SLinus Torvalds 	rtnl_lock();
7491da177e4SLinus Torvalds 
7501da177e4SLinus Torvalds 	ret = -ENODEV;
7519f9354b9SEric Dumazet 	dev = __dev_get_by_name(net, ifr.ifr_name);
7529f9354b9SEric Dumazet 	if (!dev)
7531da177e4SLinus Torvalds 		goto done;
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds 	if (colon)
7561da177e4SLinus Torvalds 		*colon = ':';
7571da177e4SLinus Torvalds 
7589f9354b9SEric Dumazet 	in_dev = __in_dev_get_rtnl(dev);
7599f9354b9SEric Dumazet 	if (in_dev) {
7601da177e4SLinus Torvalds 		if (tryaddrmatch) {
7611da177e4SLinus Torvalds 			/* Matthias Andree */
7621da177e4SLinus Torvalds 			/* compare label and address (4.4BSD style) */
7631da177e4SLinus Torvalds 			/* note: we only do this for a limited set of ioctls
7641da177e4SLinus Torvalds 			   and only if the original address family was AF_INET.
7651da177e4SLinus Torvalds 			   This is checked above. */
7661da177e4SLinus Torvalds 			for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
7671da177e4SLinus Torvalds 			     ifap = &ifa->ifa_next) {
7681da177e4SLinus Torvalds 				if (!strcmp(ifr.ifr_name, ifa->ifa_label) &&
7691da177e4SLinus Torvalds 				    sin_orig.sin_addr.s_addr ==
7706c91afe1SDavid S. Miller 							ifa->ifa_local) {
7711da177e4SLinus Torvalds 					break; /* found */
7721da177e4SLinus Torvalds 				}
7731da177e4SLinus Torvalds 			}
7741da177e4SLinus Torvalds 		}
7751da177e4SLinus Torvalds 		/* we didn't get a match, maybe the application is
7761da177e4SLinus Torvalds 		   4.3BSD-style and passed in junk so we fall back to
7771da177e4SLinus Torvalds 		   comparing just the label */
7781da177e4SLinus Torvalds 		if (!ifa) {
7791da177e4SLinus Torvalds 			for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
7801da177e4SLinus Torvalds 			     ifap = &ifa->ifa_next)
7811da177e4SLinus Torvalds 				if (!strcmp(ifr.ifr_name, ifa->ifa_label))
7821da177e4SLinus Torvalds 					break;
7831da177e4SLinus Torvalds 		}
7841da177e4SLinus Torvalds 	}
7851da177e4SLinus Torvalds 
7861da177e4SLinus Torvalds 	ret = -EADDRNOTAVAIL;
7871da177e4SLinus Torvalds 	if (!ifa && cmd != SIOCSIFADDR && cmd != SIOCSIFFLAGS)
7881da177e4SLinus Torvalds 		goto done;
7891da177e4SLinus Torvalds 
7901da177e4SLinus Torvalds 	switch (cmd) {
7911da177e4SLinus Torvalds 	case SIOCGIFADDR:	/* Get interface address */
7921da177e4SLinus Torvalds 		sin->sin_addr.s_addr = ifa->ifa_local;
7931da177e4SLinus Torvalds 		goto rarok;
7941da177e4SLinus Torvalds 
7951da177e4SLinus Torvalds 	case SIOCGIFBRDADDR:	/* Get the broadcast address */
7961da177e4SLinus Torvalds 		sin->sin_addr.s_addr = ifa->ifa_broadcast;
7971da177e4SLinus Torvalds 		goto rarok;
7981da177e4SLinus Torvalds 
7991da177e4SLinus Torvalds 	case SIOCGIFDSTADDR:	/* Get the destination address */
8001da177e4SLinus Torvalds 		sin->sin_addr.s_addr = ifa->ifa_address;
8011da177e4SLinus Torvalds 		goto rarok;
8021da177e4SLinus Torvalds 
8031da177e4SLinus Torvalds 	case SIOCGIFNETMASK:	/* Get the netmask for the interface */
8041da177e4SLinus Torvalds 		sin->sin_addr.s_addr = ifa->ifa_mask;
8051da177e4SLinus Torvalds 		goto rarok;
8061da177e4SLinus Torvalds 
8071da177e4SLinus Torvalds 	case SIOCSIFFLAGS:
8081da177e4SLinus Torvalds 		if (colon) {
8091da177e4SLinus Torvalds 			ret = -EADDRNOTAVAIL;
8101da177e4SLinus Torvalds 			if (!ifa)
8111da177e4SLinus Torvalds 				break;
8121da177e4SLinus Torvalds 			ret = 0;
8131da177e4SLinus Torvalds 			if (!(ifr.ifr_flags & IFF_UP))
8141da177e4SLinus Torvalds 				inet_del_ifa(in_dev, ifap, 1);
8151da177e4SLinus Torvalds 			break;
8161da177e4SLinus Torvalds 		}
8171da177e4SLinus Torvalds 		ret = dev_change_flags(dev, ifr.ifr_flags);
8181da177e4SLinus Torvalds 		break;
8191da177e4SLinus Torvalds 
8201da177e4SLinus Torvalds 	case SIOCSIFADDR:	/* Set interface address (and family) */
8211da177e4SLinus Torvalds 		ret = -EINVAL;
8221da177e4SLinus Torvalds 		if (inet_abc_len(sin->sin_addr.s_addr) < 0)
8231da177e4SLinus Torvalds 			break;
8241da177e4SLinus Torvalds 
8251da177e4SLinus Torvalds 		if (!ifa) {
8261da177e4SLinus Torvalds 			ret = -ENOBUFS;
8279f9354b9SEric Dumazet 			ifa = inet_alloc_ifa();
828fd23c3b3SDavid S. Miller 			INIT_HLIST_NODE(&ifa->hash);
8299f9354b9SEric Dumazet 			if (!ifa)
8301da177e4SLinus Torvalds 				break;
8311da177e4SLinus Torvalds 			if (colon)
8321da177e4SLinus Torvalds 				memcpy(ifa->ifa_label, ifr.ifr_name, IFNAMSIZ);
8331da177e4SLinus Torvalds 			else
8341da177e4SLinus Torvalds 				memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
8351da177e4SLinus Torvalds 		} else {
8361da177e4SLinus Torvalds 			ret = 0;
8371da177e4SLinus Torvalds 			if (ifa->ifa_local == sin->sin_addr.s_addr)
8381da177e4SLinus Torvalds 				break;
8391da177e4SLinus Torvalds 			inet_del_ifa(in_dev, ifap, 0);
8401da177e4SLinus Torvalds 			ifa->ifa_broadcast = 0;
841148f9729SBjorn Mork 			ifa->ifa_scope = 0;
8421da177e4SLinus Torvalds 		}
8431da177e4SLinus Torvalds 
8441da177e4SLinus Torvalds 		ifa->ifa_address = ifa->ifa_local = sin->sin_addr.s_addr;
8451da177e4SLinus Torvalds 
8461da177e4SLinus Torvalds 		if (!(dev->flags & IFF_POINTOPOINT)) {
8471da177e4SLinus Torvalds 			ifa->ifa_prefixlen = inet_abc_len(ifa->ifa_address);
8481da177e4SLinus Torvalds 			ifa->ifa_mask = inet_make_mask(ifa->ifa_prefixlen);
8491da177e4SLinus Torvalds 			if ((dev->flags & IFF_BROADCAST) &&
8501da177e4SLinus Torvalds 			    ifa->ifa_prefixlen < 31)
8511da177e4SLinus Torvalds 				ifa->ifa_broadcast = ifa->ifa_address |
8521da177e4SLinus Torvalds 						     ~ifa->ifa_mask;
8531da177e4SLinus Torvalds 		} else {
8541da177e4SLinus Torvalds 			ifa->ifa_prefixlen = 32;
8551da177e4SLinus Torvalds 			ifa->ifa_mask = inet_make_mask(32);
8561da177e4SLinus Torvalds 		}
8571da177e4SLinus Torvalds 		ret = inet_set_ifa(dev, ifa);
8581da177e4SLinus Torvalds 		break;
8591da177e4SLinus Torvalds 
8601da177e4SLinus Torvalds 	case SIOCSIFBRDADDR:	/* Set the broadcast address */
8611da177e4SLinus Torvalds 		ret = 0;
8621da177e4SLinus Torvalds 		if (ifa->ifa_broadcast != sin->sin_addr.s_addr) {
8631da177e4SLinus Torvalds 			inet_del_ifa(in_dev, ifap, 0);
8641da177e4SLinus Torvalds 			ifa->ifa_broadcast = sin->sin_addr.s_addr;
8651da177e4SLinus Torvalds 			inet_insert_ifa(ifa);
8661da177e4SLinus Torvalds 		}
8671da177e4SLinus Torvalds 		break;
8681da177e4SLinus Torvalds 
8691da177e4SLinus Torvalds 	case SIOCSIFDSTADDR:	/* Set the destination address */
8701da177e4SLinus Torvalds 		ret = 0;
8711da177e4SLinus Torvalds 		if (ifa->ifa_address == sin->sin_addr.s_addr)
8721da177e4SLinus Torvalds 			break;
8731da177e4SLinus Torvalds 		ret = -EINVAL;
8741da177e4SLinus Torvalds 		if (inet_abc_len(sin->sin_addr.s_addr) < 0)
8751da177e4SLinus Torvalds 			break;
8761da177e4SLinus Torvalds 		ret = 0;
8771da177e4SLinus Torvalds 		inet_del_ifa(in_dev, ifap, 0);
8781da177e4SLinus Torvalds 		ifa->ifa_address = sin->sin_addr.s_addr;
8791da177e4SLinus Torvalds 		inet_insert_ifa(ifa);
8801da177e4SLinus Torvalds 		break;
8811da177e4SLinus Torvalds 
8821da177e4SLinus Torvalds 	case SIOCSIFNETMASK: 	/* Set the netmask for the interface */
8831da177e4SLinus Torvalds 
8841da177e4SLinus Torvalds 		/*
8851da177e4SLinus Torvalds 		 *	The mask we set must be legal.
8861da177e4SLinus Torvalds 		 */
8871da177e4SLinus Torvalds 		ret = -EINVAL;
8881da177e4SLinus Torvalds 		if (bad_mask(sin->sin_addr.s_addr, 0))
8891da177e4SLinus Torvalds 			break;
8901da177e4SLinus Torvalds 		ret = 0;
8911da177e4SLinus Torvalds 		if (ifa->ifa_mask != sin->sin_addr.s_addr) {
892a144ea4bSAl Viro 			__be32 old_mask = ifa->ifa_mask;
8931da177e4SLinus Torvalds 			inet_del_ifa(in_dev, ifap, 0);
8941da177e4SLinus Torvalds 			ifa->ifa_mask = sin->sin_addr.s_addr;
8951da177e4SLinus Torvalds 			ifa->ifa_prefixlen = inet_mask_len(ifa->ifa_mask);
8961da177e4SLinus Torvalds 
8971da177e4SLinus Torvalds 			/* See if current broadcast address matches
8981da177e4SLinus Torvalds 			 * with current netmask, then recalculate
8991da177e4SLinus Torvalds 			 * the broadcast address. Otherwise it's a
9001da177e4SLinus Torvalds 			 * funny address, so don't touch it since
9011da177e4SLinus Torvalds 			 * the user seems to know what (s)he's doing...
9021da177e4SLinus Torvalds 			 */
9031da177e4SLinus Torvalds 			if ((dev->flags & IFF_BROADCAST) &&
9041da177e4SLinus Torvalds 			    (ifa->ifa_prefixlen < 31) &&
9051da177e4SLinus Torvalds 			    (ifa->ifa_broadcast ==
906dcab5e1eSDavid Engel 			     (ifa->ifa_local|~old_mask))) {
9071da177e4SLinus Torvalds 				ifa->ifa_broadcast = (ifa->ifa_local |
9081da177e4SLinus Torvalds 						      ~sin->sin_addr.s_addr);
9091da177e4SLinus Torvalds 			}
9101da177e4SLinus Torvalds 			inet_insert_ifa(ifa);
9111da177e4SLinus Torvalds 		}
9121da177e4SLinus Torvalds 		break;
9131da177e4SLinus Torvalds 	}
9141da177e4SLinus Torvalds done:
9151da177e4SLinus Torvalds 	rtnl_unlock();
9161da177e4SLinus Torvalds out:
9171da177e4SLinus Torvalds 	return ret;
9181da177e4SLinus Torvalds rarok:
9191da177e4SLinus Torvalds 	rtnl_unlock();
9201da177e4SLinus Torvalds 	ret = copy_to_user(arg, &ifr, sizeof(struct ifreq)) ? -EFAULT : 0;
9211da177e4SLinus Torvalds 	goto out;
9221da177e4SLinus Torvalds }
9231da177e4SLinus Torvalds 
9241da177e4SLinus Torvalds static int inet_gifconf(struct net_device *dev, char __user *buf, int len)
9251da177e4SLinus Torvalds {
926e5ed6399SHerbert Xu 	struct in_device *in_dev = __in_dev_get_rtnl(dev);
9271da177e4SLinus Torvalds 	struct in_ifaddr *ifa;
9281da177e4SLinus Torvalds 	struct ifreq ifr;
9291da177e4SLinus Torvalds 	int done = 0;
9301da177e4SLinus Torvalds 
9319f9354b9SEric Dumazet 	if (!in_dev)
9321da177e4SLinus Torvalds 		goto out;
9331da177e4SLinus Torvalds 
9349f9354b9SEric Dumazet 	for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
9351da177e4SLinus Torvalds 		if (!buf) {
9361da177e4SLinus Torvalds 			done += sizeof(ifr);
9371da177e4SLinus Torvalds 			continue;
9381da177e4SLinus Torvalds 		}
9391da177e4SLinus Torvalds 		if (len < (int) sizeof(ifr))
9401da177e4SLinus Torvalds 			break;
9411da177e4SLinus Torvalds 		memset(&ifr, 0, sizeof(struct ifreq));
9421da177e4SLinus Torvalds 		if (ifa->ifa_label)
9431da177e4SLinus Torvalds 			strcpy(ifr.ifr_name, ifa->ifa_label);
9441da177e4SLinus Torvalds 		else
9451da177e4SLinus Torvalds 			strcpy(ifr.ifr_name, dev->name);
9461da177e4SLinus Torvalds 
9471da177e4SLinus Torvalds 		(*(struct sockaddr_in *)&ifr.ifr_addr).sin_family = AF_INET;
9481da177e4SLinus Torvalds 		(*(struct sockaddr_in *)&ifr.ifr_addr).sin_addr.s_addr =
9491da177e4SLinus Torvalds 								ifa->ifa_local;
9501da177e4SLinus Torvalds 
9511da177e4SLinus Torvalds 		if (copy_to_user(buf, &ifr, sizeof(struct ifreq))) {
9521da177e4SLinus Torvalds 			done = -EFAULT;
9531da177e4SLinus Torvalds 			break;
9541da177e4SLinus Torvalds 		}
9551da177e4SLinus Torvalds 		buf  += sizeof(struct ifreq);
9561da177e4SLinus Torvalds 		len  -= sizeof(struct ifreq);
9571da177e4SLinus Torvalds 		done += sizeof(struct ifreq);
9581da177e4SLinus Torvalds 	}
9591da177e4SLinus Torvalds out:
9601da177e4SLinus Torvalds 	return done;
9611da177e4SLinus Torvalds }
9621da177e4SLinus Torvalds 
963a61ced5dSAl Viro __be32 inet_select_addr(const struct net_device *dev, __be32 dst, int scope)
9641da177e4SLinus Torvalds {
965a61ced5dSAl Viro 	__be32 addr = 0;
9661da177e4SLinus Torvalds 	struct in_device *in_dev;
967c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
9681da177e4SLinus Torvalds 
9691da177e4SLinus Torvalds 	rcu_read_lock();
970e5ed6399SHerbert Xu 	in_dev = __in_dev_get_rcu(dev);
9711da177e4SLinus Torvalds 	if (!in_dev)
9721da177e4SLinus Torvalds 		goto no_in_dev;
9731da177e4SLinus Torvalds 
9741da177e4SLinus Torvalds 	for_primary_ifa(in_dev) {
9751da177e4SLinus Torvalds 		if (ifa->ifa_scope > scope)
9761da177e4SLinus Torvalds 			continue;
9771da177e4SLinus Torvalds 		if (!dst || inet_ifa_match(dst, ifa)) {
9781da177e4SLinus Torvalds 			addr = ifa->ifa_local;
9791da177e4SLinus Torvalds 			break;
9801da177e4SLinus Torvalds 		}
9811da177e4SLinus Torvalds 		if (!addr)
9821da177e4SLinus Torvalds 			addr = ifa->ifa_local;
9831da177e4SLinus Torvalds 	} endfor_ifa(in_dev);
9841da177e4SLinus Torvalds 
9851da177e4SLinus Torvalds 	if (addr)
986c6d14c84SEric Dumazet 		goto out_unlock;
9879f9354b9SEric Dumazet no_in_dev:
9881da177e4SLinus Torvalds 
9891da177e4SLinus Torvalds 	/* Not loopback addresses on loopback should be preferred
9901da177e4SLinus Torvalds 	   in this case. It is importnat that lo is the first interface
9911da177e4SLinus Torvalds 	   in dev_base list.
9921da177e4SLinus Torvalds 	 */
993c6d14c84SEric Dumazet 	for_each_netdev_rcu(net, dev) {
9949f9354b9SEric Dumazet 		in_dev = __in_dev_get_rcu(dev);
9959f9354b9SEric Dumazet 		if (!in_dev)
9961da177e4SLinus Torvalds 			continue;
9971da177e4SLinus Torvalds 
9981da177e4SLinus Torvalds 		for_primary_ifa(in_dev) {
9991da177e4SLinus Torvalds 			if (ifa->ifa_scope != RT_SCOPE_LINK &&
10001da177e4SLinus Torvalds 			    ifa->ifa_scope <= scope) {
10011da177e4SLinus Torvalds 				addr = ifa->ifa_local;
1002c6d14c84SEric Dumazet 				goto out_unlock;
10031da177e4SLinus Torvalds 			}
10041da177e4SLinus Torvalds 		} endfor_ifa(in_dev);
10051da177e4SLinus Torvalds 	}
1006c6d14c84SEric Dumazet out_unlock:
10071da177e4SLinus Torvalds 	rcu_read_unlock();
10081da177e4SLinus Torvalds 	return addr;
10091da177e4SLinus Torvalds }
10109f9354b9SEric Dumazet EXPORT_SYMBOL(inet_select_addr);
10111da177e4SLinus Torvalds 
101260cad5daSAl Viro static __be32 confirm_addr_indev(struct in_device *in_dev, __be32 dst,
101360cad5daSAl Viro 			      __be32 local, int scope)
10141da177e4SLinus Torvalds {
10151da177e4SLinus Torvalds 	int same = 0;
1016a144ea4bSAl Viro 	__be32 addr = 0;
10171da177e4SLinus Torvalds 
10181da177e4SLinus Torvalds 	for_ifa(in_dev) {
10191da177e4SLinus Torvalds 		if (!addr &&
10201da177e4SLinus Torvalds 		    (local == ifa->ifa_local || !local) &&
10211da177e4SLinus Torvalds 		    ifa->ifa_scope <= scope) {
10221da177e4SLinus Torvalds 			addr = ifa->ifa_local;
10231da177e4SLinus Torvalds 			if (same)
10241da177e4SLinus Torvalds 				break;
10251da177e4SLinus Torvalds 		}
10261da177e4SLinus Torvalds 		if (!same) {
10271da177e4SLinus Torvalds 			same = (!local || inet_ifa_match(local, ifa)) &&
10281da177e4SLinus Torvalds 				(!dst || inet_ifa_match(dst, ifa));
10291da177e4SLinus Torvalds 			if (same && addr) {
10301da177e4SLinus Torvalds 				if (local || !dst)
10311da177e4SLinus Torvalds 					break;
10321da177e4SLinus Torvalds 				/* Is the selected addr into dst subnet? */
10331da177e4SLinus Torvalds 				if (inet_ifa_match(addr, ifa))
10341da177e4SLinus Torvalds 					break;
10351da177e4SLinus Torvalds 				/* No, then can we use new local src? */
10361da177e4SLinus Torvalds 				if (ifa->ifa_scope <= scope) {
10371da177e4SLinus Torvalds 					addr = ifa->ifa_local;
10381da177e4SLinus Torvalds 					break;
10391da177e4SLinus Torvalds 				}
10401da177e4SLinus Torvalds 				/* search for large dst subnet for addr */
10411da177e4SLinus Torvalds 				same = 0;
10421da177e4SLinus Torvalds 			}
10431da177e4SLinus Torvalds 		}
10441da177e4SLinus Torvalds 	} endfor_ifa(in_dev);
10451da177e4SLinus Torvalds 
10461da177e4SLinus Torvalds 	return same ? addr : 0;
10471da177e4SLinus Torvalds }
10481da177e4SLinus Torvalds 
10491da177e4SLinus Torvalds /*
10501da177e4SLinus Torvalds  * Confirm that local IP address exists using wildcards:
10519bd85e32SDenis V. Lunev  * - in_dev: only on this interface, 0=any interface
10521da177e4SLinus Torvalds  * - dst: only in the same subnet as dst, 0=any dst
10531da177e4SLinus Torvalds  * - local: address, 0=autoselect the local address
10541da177e4SLinus Torvalds  * - scope: maximum allowed scope value for the local address
10551da177e4SLinus Torvalds  */
10569bd85e32SDenis V. Lunev __be32 inet_confirm_addr(struct in_device *in_dev,
10579bd85e32SDenis V. Lunev 			 __be32 dst, __be32 local, int scope)
10581da177e4SLinus Torvalds {
105960cad5daSAl Viro 	__be32 addr = 0;
10609bd85e32SDenis V. Lunev 	struct net_device *dev;
106139a6d063SDenis V. Lunev 	struct net *net;
10621da177e4SLinus Torvalds 
106339a6d063SDenis V. Lunev 	if (scope != RT_SCOPE_LINK)
10649bd85e32SDenis V. Lunev 		return confirm_addr_indev(in_dev, dst, local, scope);
10651da177e4SLinus Torvalds 
1066c346dca1SYOSHIFUJI Hideaki 	net = dev_net(in_dev->dev);
10671da177e4SLinus Torvalds 	rcu_read_lock();
1068c6d14c84SEric Dumazet 	for_each_netdev_rcu(net, dev) {
10699f9354b9SEric Dumazet 		in_dev = __in_dev_get_rcu(dev);
10709f9354b9SEric Dumazet 		if (in_dev) {
10711da177e4SLinus Torvalds 			addr = confirm_addr_indev(in_dev, dst, local, scope);
10721da177e4SLinus Torvalds 			if (addr)
10731da177e4SLinus Torvalds 				break;
10741da177e4SLinus Torvalds 		}
10751da177e4SLinus Torvalds 	}
10761da177e4SLinus Torvalds 	rcu_read_unlock();
10771da177e4SLinus Torvalds 
10781da177e4SLinus Torvalds 	return addr;
10791da177e4SLinus Torvalds }
1080eaddcd76SAndy Gospodarek EXPORT_SYMBOL(inet_confirm_addr);
10811da177e4SLinus Torvalds 
10821da177e4SLinus Torvalds /*
10831da177e4SLinus Torvalds  *	Device notifier
10841da177e4SLinus Torvalds  */
10851da177e4SLinus Torvalds 
10861da177e4SLinus Torvalds int register_inetaddr_notifier(struct notifier_block *nb)
10871da177e4SLinus Torvalds {
1088e041c683SAlan Stern 	return blocking_notifier_chain_register(&inetaddr_chain, nb);
10891da177e4SLinus Torvalds }
10909f9354b9SEric Dumazet EXPORT_SYMBOL(register_inetaddr_notifier);
10911da177e4SLinus Torvalds 
10921da177e4SLinus Torvalds int unregister_inetaddr_notifier(struct notifier_block *nb)
10931da177e4SLinus Torvalds {
1094e041c683SAlan Stern 	return blocking_notifier_chain_unregister(&inetaddr_chain, nb);
10951da177e4SLinus Torvalds }
10969f9354b9SEric Dumazet EXPORT_SYMBOL(unregister_inetaddr_notifier);
10971da177e4SLinus Torvalds 
10989f9354b9SEric Dumazet /* Rename ifa_labels for a device name change. Make some effort to preserve
10999f9354b9SEric Dumazet  * existing alias numbering and to create unique labels if possible.
11001da177e4SLinus Torvalds */
11011da177e4SLinus Torvalds static void inetdev_changename(struct net_device *dev, struct in_device *in_dev)
11021da177e4SLinus Torvalds {
11031da177e4SLinus Torvalds 	struct in_ifaddr *ifa;
11041da177e4SLinus Torvalds 	int named = 0;
11051da177e4SLinus Torvalds 
11061da177e4SLinus Torvalds 	for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
11071da177e4SLinus Torvalds 		char old[IFNAMSIZ], *dot;
11081da177e4SLinus Torvalds 
11091da177e4SLinus Torvalds 		memcpy(old, ifa->ifa_label, IFNAMSIZ);
11101da177e4SLinus Torvalds 		memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
11111da177e4SLinus Torvalds 		if (named++ == 0)
1112573bf470SThomas Graf 			goto skip;
111344344b2aSMark McLoughlin 		dot = strchr(old, ':');
11141da177e4SLinus Torvalds 		if (dot == NULL) {
11151da177e4SLinus Torvalds 			sprintf(old, ":%d", named);
11161da177e4SLinus Torvalds 			dot = old;
11171da177e4SLinus Torvalds 		}
11189f9354b9SEric Dumazet 		if (strlen(dot) + strlen(dev->name) < IFNAMSIZ)
11191da177e4SLinus Torvalds 			strcat(ifa->ifa_label, dot);
11209f9354b9SEric Dumazet 		else
11211da177e4SLinus Torvalds 			strcpy(ifa->ifa_label + (IFNAMSIZ - strlen(dot) - 1), dot);
1122573bf470SThomas Graf skip:
1123573bf470SThomas Graf 		rtmsg_ifa(RTM_NEWADDR, ifa, NULL, 0);
11241da177e4SLinus Torvalds 	}
11251da177e4SLinus Torvalds }
11261da177e4SLinus Torvalds 
112795c96174SEric Dumazet static inline bool inetdev_valid_mtu(unsigned int mtu)
112806770843SBreno Leitao {
112906770843SBreno Leitao 	return mtu >= 68;
113006770843SBreno Leitao }
113106770843SBreno Leitao 
1132d11327adSIan Campbell static void inetdev_send_gratuitous_arp(struct net_device *dev,
1133d11327adSIan Campbell 					struct in_device *in_dev)
1134d11327adSIan Campbell 
1135d11327adSIan Campbell {
1136b76d0789SZoltan Kiss 	struct in_ifaddr *ifa;
1137d11327adSIan Campbell 
1138b76d0789SZoltan Kiss 	for (ifa = in_dev->ifa_list; ifa;
1139b76d0789SZoltan Kiss 	     ifa = ifa->ifa_next) {
1140d11327adSIan Campbell 		arp_send(ARPOP_REQUEST, ETH_P_ARP,
11416c91afe1SDavid S. Miller 			 ifa->ifa_local, dev,
11426c91afe1SDavid S. Miller 			 ifa->ifa_local, NULL,
1143d11327adSIan Campbell 			 dev->dev_addr, NULL);
1144d11327adSIan Campbell 	}
1145b76d0789SZoltan Kiss }
1146d11327adSIan Campbell 
11471da177e4SLinus Torvalds /* Called only under RTNL semaphore */
11481da177e4SLinus Torvalds 
11491da177e4SLinus Torvalds static int inetdev_event(struct notifier_block *this, unsigned long event,
11501da177e4SLinus Torvalds 			 void *ptr)
11511da177e4SLinus Torvalds {
11521da177e4SLinus Torvalds 	struct net_device *dev = ptr;
1153e5ed6399SHerbert Xu 	struct in_device *in_dev = __in_dev_get_rtnl(dev);
11541da177e4SLinus Torvalds 
11551da177e4SLinus Torvalds 	ASSERT_RTNL();
11561da177e4SLinus Torvalds 
11571da177e4SLinus Torvalds 	if (!in_dev) {
11588030f544SHerbert Xu 		if (event == NETDEV_REGISTER) {
11591da177e4SLinus Torvalds 			in_dev = inetdev_init(dev);
11608d76527eSHerbert Xu 			if (!in_dev)
1161b217d616SHerbert Xu 				return notifier_from_errno(-ENOMEM);
11620cc217e1SEric W. Biederman 			if (dev->flags & IFF_LOOPBACK) {
116342f811b8SHerbert Xu 				IN_DEV_CONF_SET(in_dev, NOXFRM, 1);
116442f811b8SHerbert Xu 				IN_DEV_CONF_SET(in_dev, NOPOLICY, 1);
11651da177e4SLinus Torvalds 			}
116606770843SBreno Leitao 		} else if (event == NETDEV_CHANGEMTU) {
116706770843SBreno Leitao 			/* Re-enabling IP */
116806770843SBreno Leitao 			if (inetdev_valid_mtu(dev->mtu))
116906770843SBreno Leitao 				in_dev = inetdev_init(dev);
11708030f544SHerbert Xu 		}
11711da177e4SLinus Torvalds 		goto out;
11721da177e4SLinus Torvalds 	}
11731da177e4SLinus Torvalds 
11741da177e4SLinus Torvalds 	switch (event) {
11751da177e4SLinus Torvalds 	case NETDEV_REGISTER:
1176*91df42beSJoe Perches 		pr_debug("%s: bug\n", __func__);
1177a9b3cd7fSStephen Hemminger 		RCU_INIT_POINTER(dev->ip_ptr, NULL);
11781da177e4SLinus Torvalds 		break;
11791da177e4SLinus Torvalds 	case NETDEV_UP:
118006770843SBreno Leitao 		if (!inetdev_valid_mtu(dev->mtu))
11811da177e4SLinus Torvalds 			break;
11820cc217e1SEric W. Biederman 		if (dev->flags & IFF_LOOPBACK) {
11839f9354b9SEric Dumazet 			struct in_ifaddr *ifa = inet_alloc_ifa();
11849f9354b9SEric Dumazet 
11859f9354b9SEric Dumazet 			if (ifa) {
1186fd23c3b3SDavid S. Miller 				INIT_HLIST_NODE(&ifa->hash);
11871da177e4SLinus Torvalds 				ifa->ifa_local =
11881da177e4SLinus Torvalds 				  ifa->ifa_address = htonl(INADDR_LOOPBACK);
11891da177e4SLinus Torvalds 				ifa->ifa_prefixlen = 8;
11901da177e4SLinus Torvalds 				ifa->ifa_mask = inet_make_mask(8);
11911da177e4SLinus Torvalds 				in_dev_hold(in_dev);
11921da177e4SLinus Torvalds 				ifa->ifa_dev = in_dev;
11931da177e4SLinus Torvalds 				ifa->ifa_scope = RT_SCOPE_HOST;
11941da177e4SLinus Torvalds 				memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
11951da177e4SLinus Torvalds 				inet_insert_ifa(ifa);
11961da177e4SLinus Torvalds 			}
11971da177e4SLinus Torvalds 		}
11981da177e4SLinus Torvalds 		ip_mc_up(in_dev);
1199eefef1cfSStephen Hemminger 		/* fall through */
1200eefef1cfSStephen Hemminger 	case NETDEV_CHANGEADDR:
1201d11327adSIan Campbell 		if (!IN_DEV_ARP_NOTIFY(in_dev))
1202d11327adSIan Campbell 			break;
1203d11327adSIan Campbell 		/* fall through */
1204d11327adSIan Campbell 	case NETDEV_NOTIFY_PEERS:
1205a21090cfSStephen Hemminger 		/* Send gratuitous ARP to notify of link change */
1206d11327adSIan Campbell 		inetdev_send_gratuitous_arp(dev, in_dev);
12071da177e4SLinus Torvalds 		break;
12081da177e4SLinus Torvalds 	case NETDEV_DOWN:
12091da177e4SLinus Torvalds 		ip_mc_down(in_dev);
12101da177e4SLinus Torvalds 		break;
121193d9b7d7SJiri Pirko 	case NETDEV_PRE_TYPE_CHANGE:
121275c78500SMoni Shoua 		ip_mc_unmap(in_dev);
121375c78500SMoni Shoua 		break;
121493d9b7d7SJiri Pirko 	case NETDEV_POST_TYPE_CHANGE:
121575c78500SMoni Shoua 		ip_mc_remap(in_dev);
121675c78500SMoni Shoua 		break;
12171da177e4SLinus Torvalds 	case NETDEV_CHANGEMTU:
121806770843SBreno Leitao 		if (inetdev_valid_mtu(dev->mtu))
12191da177e4SLinus Torvalds 			break;
122006770843SBreno Leitao 		/* disable IP when MTU is not enough */
12211da177e4SLinus Torvalds 	case NETDEV_UNREGISTER:
12221da177e4SLinus Torvalds 		inetdev_destroy(in_dev);
12231da177e4SLinus Torvalds 		break;
12241da177e4SLinus Torvalds 	case NETDEV_CHANGENAME:
12251da177e4SLinus Torvalds 		/* Do not notify about label change, this event is
12261da177e4SLinus Torvalds 		 * not interesting to applications using netlink.
12271da177e4SLinus Torvalds 		 */
12281da177e4SLinus Torvalds 		inetdev_changename(dev, in_dev);
12291da177e4SLinus Torvalds 
123051602b2aSPavel Emelyanov 		devinet_sysctl_unregister(in_dev);
123166f27a52SPavel Emelyanov 		devinet_sysctl_register(in_dev);
12321da177e4SLinus Torvalds 		break;
12331da177e4SLinus Torvalds 	}
12341da177e4SLinus Torvalds out:
12351da177e4SLinus Torvalds 	return NOTIFY_DONE;
12361da177e4SLinus Torvalds }
12371da177e4SLinus Torvalds 
12381da177e4SLinus Torvalds static struct notifier_block ip_netdev_notifier = {
12391da177e4SLinus Torvalds 	.notifier_call = inetdev_event,
12401da177e4SLinus Torvalds };
12411da177e4SLinus Torvalds 
1242339bf98fSThomas Graf static inline size_t inet_nlmsg_size(void)
1243339bf98fSThomas Graf {
1244339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
1245339bf98fSThomas Graf 	       + nla_total_size(4) /* IFA_ADDRESS */
1246339bf98fSThomas Graf 	       + nla_total_size(4) /* IFA_LOCAL */
1247339bf98fSThomas Graf 	       + nla_total_size(4) /* IFA_BROADCAST */
1248339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ); /* IFA_LABEL */
1249339bf98fSThomas Graf }
1250339bf98fSThomas Graf 
12511da177e4SLinus Torvalds static int inet_fill_ifaddr(struct sk_buff *skb, struct in_ifaddr *ifa,
1252b6544c0bSJamal Hadi Salim 			    u32 pid, u32 seq, int event, unsigned int flags)
12531da177e4SLinus Torvalds {
12541da177e4SLinus Torvalds 	struct ifaddrmsg *ifm;
12551da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
12561da177e4SLinus Torvalds 
125747f68512SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, event, sizeof(*ifm), flags);
125847f68512SThomas Graf 	if (nlh == NULL)
125926932566SPatrick McHardy 		return -EMSGSIZE;
126047f68512SThomas Graf 
126147f68512SThomas Graf 	ifm = nlmsg_data(nlh);
12621da177e4SLinus Torvalds 	ifm->ifa_family = AF_INET;
12631da177e4SLinus Torvalds 	ifm->ifa_prefixlen = ifa->ifa_prefixlen;
12641da177e4SLinus Torvalds 	ifm->ifa_flags = ifa->ifa_flags|IFA_F_PERMANENT;
12651da177e4SLinus Torvalds 	ifm->ifa_scope = ifa->ifa_scope;
12661da177e4SLinus Torvalds 	ifm->ifa_index = ifa->ifa_dev->dev->ifindex;
12671da177e4SLinus Torvalds 
1268f3756b79SDavid S. Miller 	if ((ifa->ifa_address &&
1269f3756b79SDavid S. Miller 	     nla_put_be32(skb, IFA_ADDRESS, ifa->ifa_address)) ||
1270f3756b79SDavid S. Miller 	    (ifa->ifa_local &&
1271f3756b79SDavid S. Miller 	     nla_put_be32(skb, IFA_LOCAL, ifa->ifa_local)) ||
1272f3756b79SDavid S. Miller 	    (ifa->ifa_broadcast &&
1273f3756b79SDavid S. Miller 	     nla_put_be32(skb, IFA_BROADCAST, ifa->ifa_broadcast)) ||
1274f3756b79SDavid S. Miller 	    (ifa->ifa_label[0] &&
1275f3756b79SDavid S. Miller 	     nla_put_string(skb, IFA_LABEL, ifa->ifa_label)))
1276f3756b79SDavid S. Miller 		goto nla_put_failure;
127747f68512SThomas Graf 
127847f68512SThomas Graf 	return nlmsg_end(skb, nlh);
127947f68512SThomas Graf 
128047f68512SThomas Graf nla_put_failure:
128126932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
128226932566SPatrick McHardy 	return -EMSGSIZE;
12831da177e4SLinus Torvalds }
12841da177e4SLinus Torvalds 
12851da177e4SLinus Torvalds static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
12861da177e4SLinus Torvalds {
12873b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
1288eec4df98SEric Dumazet 	int h, s_h;
1289eec4df98SEric Dumazet 	int idx, s_idx;
1290eec4df98SEric Dumazet 	int ip_idx, s_ip_idx;
12911da177e4SLinus Torvalds 	struct net_device *dev;
12921da177e4SLinus Torvalds 	struct in_device *in_dev;
12931da177e4SLinus Torvalds 	struct in_ifaddr *ifa;
1294eec4df98SEric Dumazet 	struct hlist_head *head;
1295eec4df98SEric Dumazet 	struct hlist_node *node;
12961da177e4SLinus Torvalds 
1297eec4df98SEric Dumazet 	s_h = cb->args[0];
1298eec4df98SEric Dumazet 	s_idx = idx = cb->args[1];
1299eec4df98SEric Dumazet 	s_ip_idx = ip_idx = cb->args[2];
1300eec4df98SEric Dumazet 
1301eec4df98SEric Dumazet 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
13027562f876SPavel Emelianov 		idx = 0;
1303eec4df98SEric Dumazet 		head = &net->dev_index_head[h];
1304eec4df98SEric Dumazet 		rcu_read_lock();
1305eec4df98SEric Dumazet 		hlist_for_each_entry_rcu(dev, node, head, index_hlist) {
13061da177e4SLinus Torvalds 			if (idx < s_idx)
13077562f876SPavel Emelianov 				goto cont;
13084b97efdfSPatrick McHardy 			if (h > s_h || idx > s_idx)
13091da177e4SLinus Torvalds 				s_ip_idx = 0;
1310eec4df98SEric Dumazet 			in_dev = __in_dev_get_rcu(dev);
13119f9354b9SEric Dumazet 			if (!in_dev)
13127562f876SPavel Emelianov 				goto cont;
13131da177e4SLinus Torvalds 
13141da177e4SLinus Torvalds 			for (ifa = in_dev->ifa_list, ip_idx = 0; ifa;
13151da177e4SLinus Torvalds 			     ifa = ifa->ifa_next, ip_idx++) {
13161da177e4SLinus Torvalds 				if (ip_idx < s_ip_idx)
1317596e4150SStephen Hemminger 					continue;
1318eec4df98SEric Dumazet 				if (inet_fill_ifaddr(skb, ifa,
1319eec4df98SEric Dumazet 					     NETLINK_CB(cb->skb).pid,
13201da177e4SLinus Torvalds 					     cb->nlh->nlmsg_seq,
1321eec4df98SEric Dumazet 					     RTM_NEWADDR, NLM_F_MULTI) <= 0) {
1322eec4df98SEric Dumazet 					rcu_read_unlock();
13231da177e4SLinus Torvalds 					goto done;
13241da177e4SLinus Torvalds 				}
1325eec4df98SEric Dumazet 			}
13267562f876SPavel Emelianov cont:
13277562f876SPavel Emelianov 			idx++;
13281da177e4SLinus Torvalds 		}
1329eec4df98SEric Dumazet 		rcu_read_unlock();
1330eec4df98SEric Dumazet 	}
13311da177e4SLinus Torvalds 
13321da177e4SLinus Torvalds done:
1333eec4df98SEric Dumazet 	cb->args[0] = h;
1334eec4df98SEric Dumazet 	cb->args[1] = idx;
1335eec4df98SEric Dumazet 	cb->args[2] = ip_idx;
13361da177e4SLinus Torvalds 
13371da177e4SLinus Torvalds 	return skb->len;
13381da177e4SLinus Torvalds }
13391da177e4SLinus Torvalds 
1340d6062cbbSThomas Graf static void rtmsg_ifa(int event, struct in_ifaddr *ifa, struct nlmsghdr *nlh,
1341d6062cbbSThomas Graf 		      u32 pid)
13421da177e4SLinus Torvalds {
134347f68512SThomas Graf 	struct sk_buff *skb;
1344d6062cbbSThomas Graf 	u32 seq = nlh ? nlh->nlmsg_seq : 0;
1345d6062cbbSThomas Graf 	int err = -ENOBUFS;
13464b8aa9abSDenis V. Lunev 	struct net *net;
13471da177e4SLinus Torvalds 
1348c346dca1SYOSHIFUJI Hideaki 	net = dev_net(ifa->ifa_dev->dev);
1349339bf98fSThomas Graf 	skb = nlmsg_new(inet_nlmsg_size(), GFP_KERNEL);
135047f68512SThomas Graf 	if (skb == NULL)
1351d6062cbbSThomas Graf 		goto errout;
1352d6062cbbSThomas Graf 
1353d6062cbbSThomas Graf 	err = inet_fill_ifaddr(skb, ifa, pid, seq, event, 0);
135426932566SPatrick McHardy 	if (err < 0) {
135526932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet_nlmsg_size() */
135626932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
135726932566SPatrick McHardy 		kfree_skb(skb);
135826932566SPatrick McHardy 		goto errout;
135926932566SPatrick McHardy 	}
13601ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, net, pid, RTNLGRP_IPV4_IFADDR, nlh, GFP_KERNEL);
13611ce85fe4SPablo Neira Ayuso 	return;
1362d6062cbbSThomas Graf errout:
1363d6062cbbSThomas Graf 	if (err < 0)
13644b8aa9abSDenis V. Lunev 		rtnl_set_sk_err(net, RTNLGRP_IPV4_IFADDR, err);
13651da177e4SLinus Torvalds }
13661da177e4SLinus Torvalds 
13679f0f7272SThomas Graf static size_t inet_get_link_af_size(const struct net_device *dev)
13689f0f7272SThomas Graf {
13691fc19affSEric Dumazet 	struct in_device *in_dev = rcu_dereference_rtnl(dev->ip_ptr);
13709f0f7272SThomas Graf 
13719f0f7272SThomas Graf 	if (!in_dev)
13729f0f7272SThomas Graf 		return 0;
13739f0f7272SThomas Graf 
13749f0f7272SThomas Graf 	return nla_total_size(IPV4_DEVCONF_MAX * 4); /* IFLA_INET_CONF */
13759f0f7272SThomas Graf }
13769f0f7272SThomas Graf 
13779f0f7272SThomas Graf static int inet_fill_link_af(struct sk_buff *skb, const struct net_device *dev)
13789f0f7272SThomas Graf {
13791fc19affSEric Dumazet 	struct in_device *in_dev = rcu_dereference_rtnl(dev->ip_ptr);
13809f0f7272SThomas Graf 	struct nlattr *nla;
13819f0f7272SThomas Graf 	int i;
13829f0f7272SThomas Graf 
13839f0f7272SThomas Graf 	if (!in_dev)
13849f0f7272SThomas Graf 		return -ENODATA;
13859f0f7272SThomas Graf 
13869f0f7272SThomas Graf 	nla = nla_reserve(skb, IFLA_INET_CONF, IPV4_DEVCONF_MAX * 4);
13879f0f7272SThomas Graf 	if (nla == NULL)
13889f0f7272SThomas Graf 		return -EMSGSIZE;
13899f0f7272SThomas Graf 
13909f0f7272SThomas Graf 	for (i = 0; i < IPV4_DEVCONF_MAX; i++)
13919f0f7272SThomas Graf 		((u32 *) nla_data(nla))[i] = in_dev->cnf.data[i];
13929f0f7272SThomas Graf 
13939f0f7272SThomas Graf 	return 0;
13949f0f7272SThomas Graf }
13959f0f7272SThomas Graf 
13969f0f7272SThomas Graf static const struct nla_policy inet_af_policy[IFLA_INET_MAX+1] = {
13979f0f7272SThomas Graf 	[IFLA_INET_CONF]	= { .type = NLA_NESTED },
13989f0f7272SThomas Graf };
13999f0f7272SThomas Graf 
1400cf7afbfeSThomas Graf static int inet_validate_link_af(const struct net_device *dev,
1401cf7afbfeSThomas Graf 				 const struct nlattr *nla)
14029f0f7272SThomas Graf {
14039f0f7272SThomas Graf 	struct nlattr *a, *tb[IFLA_INET_MAX+1];
14049f0f7272SThomas Graf 	int err, rem;
14059f0f7272SThomas Graf 
1406f7fce74eSEric Dumazet 	if (dev && !__in_dev_get_rtnl(dev))
1407cf7afbfeSThomas Graf 		return -EAFNOSUPPORT;
14089f0f7272SThomas Graf 
14099f0f7272SThomas Graf 	err = nla_parse_nested(tb, IFLA_INET_MAX, nla, inet_af_policy);
14109f0f7272SThomas Graf 	if (err < 0)
14119f0f7272SThomas Graf 		return err;
14129f0f7272SThomas Graf 
14139f0f7272SThomas Graf 	if (tb[IFLA_INET_CONF]) {
14149f0f7272SThomas Graf 		nla_for_each_nested(a, tb[IFLA_INET_CONF], rem) {
14159f0f7272SThomas Graf 			int cfgid = nla_type(a);
14169f0f7272SThomas Graf 
14179f0f7272SThomas Graf 			if (nla_len(a) < 4)
14189f0f7272SThomas Graf 				return -EINVAL;
14199f0f7272SThomas Graf 
14209f0f7272SThomas Graf 			if (cfgid <= 0 || cfgid > IPV4_DEVCONF_MAX)
14219f0f7272SThomas Graf 				return -EINVAL;
14229f0f7272SThomas Graf 		}
14239f0f7272SThomas Graf 	}
14249f0f7272SThomas Graf 
1425cf7afbfeSThomas Graf 	return 0;
1426cf7afbfeSThomas Graf }
1427cf7afbfeSThomas Graf 
1428cf7afbfeSThomas Graf static int inet_set_link_af(struct net_device *dev, const struct nlattr *nla)
1429cf7afbfeSThomas Graf {
1430f7fce74eSEric Dumazet 	struct in_device *in_dev = __in_dev_get_rtnl(dev);
1431cf7afbfeSThomas Graf 	struct nlattr *a, *tb[IFLA_INET_MAX+1];
1432cf7afbfeSThomas Graf 	int rem;
1433cf7afbfeSThomas Graf 
1434cf7afbfeSThomas Graf 	if (!in_dev)
1435cf7afbfeSThomas Graf 		return -EAFNOSUPPORT;
1436cf7afbfeSThomas Graf 
1437cf7afbfeSThomas Graf 	if (nla_parse_nested(tb, IFLA_INET_MAX, nla, NULL) < 0)
1438cf7afbfeSThomas Graf 		BUG();
1439cf7afbfeSThomas Graf 
14409f0f7272SThomas Graf 	if (tb[IFLA_INET_CONF]) {
14419f0f7272SThomas Graf 		nla_for_each_nested(a, tb[IFLA_INET_CONF], rem)
14429f0f7272SThomas Graf 			ipv4_devconf_set(in_dev, nla_type(a), nla_get_u32(a));
14439f0f7272SThomas Graf 	}
14449f0f7272SThomas Graf 
14459f0f7272SThomas Graf 	return 0;
14469f0f7272SThomas Graf }
14479f0f7272SThomas Graf 
14481da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
14491da177e4SLinus Torvalds 
1450c0ce9fb3SPavel Emelyanov static void devinet_copy_dflt_conf(struct net *net, int i)
145131be3085SHerbert Xu {
145231be3085SHerbert Xu 	struct net_device *dev;
145331be3085SHerbert Xu 
145431be3085SHerbert Xu 	rcu_read_lock();
1455c6d14c84SEric Dumazet 	for_each_netdev_rcu(net, dev) {
1456c6d14c84SEric Dumazet 		struct in_device *in_dev;
1457c6d14c84SEric Dumazet 
145831be3085SHerbert Xu 		in_dev = __in_dev_get_rcu(dev);
145931be3085SHerbert Xu 		if (in_dev && !test_bit(i, in_dev->cnf.state))
14609355bbd6SPavel Emelyanov 			in_dev->cnf.data[i] = net->ipv4.devconf_dflt->data[i];
1461c6d14c84SEric Dumazet 	}
146231be3085SHerbert Xu 	rcu_read_unlock();
146331be3085SHerbert Xu }
146431be3085SHerbert Xu 
1465c6d14c84SEric Dumazet /* called with RTNL locked */
1466c0ce9fb3SPavel Emelyanov static void inet_forward_change(struct net *net)
146768dd299bSPavel Emelyanov {
146868dd299bSPavel Emelyanov 	struct net_device *dev;
1469586f1211SPavel Emelyanov 	int on = IPV4_DEVCONF_ALL(net, FORWARDING);
147068dd299bSPavel Emelyanov 
1471586f1211SPavel Emelyanov 	IPV4_DEVCONF_ALL(net, ACCEPT_REDIRECTS) = !on;
14729355bbd6SPavel Emelyanov 	IPV4_DEVCONF_DFLT(net, FORWARDING) = on;
147368dd299bSPavel Emelyanov 
1474c0ce9fb3SPavel Emelyanov 	for_each_netdev(net, dev) {
147568dd299bSPavel Emelyanov 		struct in_device *in_dev;
14760187bdfbSBen Hutchings 		if (on)
14770187bdfbSBen Hutchings 			dev_disable_lro(dev);
147868dd299bSPavel Emelyanov 		rcu_read_lock();
147968dd299bSPavel Emelyanov 		in_dev = __in_dev_get_rcu(dev);
148068dd299bSPavel Emelyanov 		if (in_dev)
148168dd299bSPavel Emelyanov 			IN_DEV_CONF_SET(in_dev, FORWARDING, on);
148268dd299bSPavel Emelyanov 		rcu_read_unlock();
148368dd299bSPavel Emelyanov 	}
148468dd299bSPavel Emelyanov }
148568dd299bSPavel Emelyanov 
148631be3085SHerbert Xu static int devinet_conf_proc(ctl_table *ctl, int write,
14878d65af78SAlexey Dobriyan 			     void __user *buffer,
148831be3085SHerbert Xu 			     size_t *lenp, loff_t *ppos)
148931be3085SHerbert Xu {
1490d01ff0a0SPeter Pan(潘卫平) 	int old_value = *(int *)ctl->data;
14918d65af78SAlexey Dobriyan 	int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
1492d01ff0a0SPeter Pan(潘卫平) 	int new_value = *(int *)ctl->data;
149331be3085SHerbert Xu 
149431be3085SHerbert Xu 	if (write) {
149531be3085SHerbert Xu 		struct ipv4_devconf *cnf = ctl->extra1;
1496c0ce9fb3SPavel Emelyanov 		struct net *net = ctl->extra2;
149731be3085SHerbert Xu 		int i = (int *)ctl->data - cnf->data;
149831be3085SHerbert Xu 
149931be3085SHerbert Xu 		set_bit(i, cnf->state);
150031be3085SHerbert Xu 
15019355bbd6SPavel Emelyanov 		if (cnf == net->ipv4.devconf_dflt)
1502c0ce9fb3SPavel Emelyanov 			devinet_copy_dflt_conf(net, i);
1503d01ff0a0SPeter Pan(潘卫平) 		if (i == IPV4_DEVCONF_ACCEPT_LOCAL - 1)
1504d01ff0a0SPeter Pan(潘卫平) 			if ((new_value == 0) && (old_value != 0))
1505d01ff0a0SPeter Pan(潘卫平) 				rt_cache_flush(net, 0);
150631be3085SHerbert Xu 	}
150731be3085SHerbert Xu 
150831be3085SHerbert Xu 	return ret;
150931be3085SHerbert Xu }
151031be3085SHerbert Xu 
15111da177e4SLinus Torvalds static int devinet_sysctl_forward(ctl_table *ctl, int write,
15128d65af78SAlexey Dobriyan 				  void __user *buffer,
15131da177e4SLinus Torvalds 				  size_t *lenp, loff_t *ppos)
15141da177e4SLinus Torvalds {
15151da177e4SLinus Torvalds 	int *valp = ctl->data;
15161da177e4SLinus Torvalds 	int val = *valp;
151788af182eSEric W. Biederman 	loff_t pos = *ppos;
15188d65af78SAlexey Dobriyan 	int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
15191da177e4SLinus Torvalds 
15201da177e4SLinus Torvalds 	if (write && *valp != val) {
1521c0ce9fb3SPavel Emelyanov 		struct net *net = ctl->extra2;
1522c0ce9fb3SPavel Emelyanov 
15230187bdfbSBen Hutchings 		if (valp != &IPV4_DEVCONF_DFLT(net, FORWARDING)) {
152488af182eSEric W. Biederman 			if (!rtnl_trylock()) {
152588af182eSEric W. Biederman 				/* Restore the original values before restarting */
152688af182eSEric W. Biederman 				*valp = val;
152788af182eSEric W. Biederman 				*ppos = pos;
15289b8adb5eSEric W. Biederman 				return restart_syscall();
152988af182eSEric W. Biederman 			}
15300187bdfbSBen Hutchings 			if (valp == &IPV4_DEVCONF_ALL(net, FORWARDING)) {
1531c0ce9fb3SPavel Emelyanov 				inet_forward_change(net);
15320187bdfbSBen Hutchings 			} else if (*valp) {
15330187bdfbSBen Hutchings 				struct ipv4_devconf *cnf = ctl->extra1;
15340187bdfbSBen Hutchings 				struct in_device *idev =
15350187bdfbSBen Hutchings 					container_of(cnf, struct in_device, cnf);
15360187bdfbSBen Hutchings 				dev_disable_lro(idev->dev);
15370187bdfbSBen Hutchings 			}
15380187bdfbSBen Hutchings 			rtnl_unlock();
153976e6ebfbSDenis V. Lunev 			rt_cache_flush(net, 0);
15401da177e4SLinus Torvalds 		}
15410187bdfbSBen Hutchings 	}
15421da177e4SLinus Torvalds 
15431da177e4SLinus Torvalds 	return ret;
15441da177e4SLinus Torvalds }
15451da177e4SLinus Torvalds 
1546323e126fSDavid S. Miller static int ipv4_doint_and_flush(ctl_table *ctl, int write,
15478d65af78SAlexey Dobriyan 				void __user *buffer,
15481da177e4SLinus Torvalds 				size_t *lenp, loff_t *ppos)
15491da177e4SLinus Torvalds {
15501da177e4SLinus Torvalds 	int *valp = ctl->data;
15511da177e4SLinus Torvalds 	int val = *valp;
15528d65af78SAlexey Dobriyan 	int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
155376e6ebfbSDenis V. Lunev 	struct net *net = ctl->extra2;
15541da177e4SLinus Torvalds 
15551da177e4SLinus Torvalds 	if (write && *valp != val)
155676e6ebfbSDenis V. Lunev 		rt_cache_flush(net, 0);
15571da177e4SLinus Torvalds 
15581da177e4SLinus Torvalds 	return ret;
15591da177e4SLinus Torvalds }
15601da177e4SLinus Torvalds 
1561f8572d8fSEric W. Biederman #define DEVINET_SYSCTL_ENTRY(attr, name, mval, proc) \
156242f811b8SHerbert Xu 	{ \
156342f811b8SHerbert Xu 		.procname	= name, \
156442f811b8SHerbert Xu 		.data		= ipv4_devconf.data + \
156502291680SEric W. Biederman 				  IPV4_DEVCONF_ ## attr - 1, \
156642f811b8SHerbert Xu 		.maxlen		= sizeof(int), \
156742f811b8SHerbert Xu 		.mode		= mval, \
156842f811b8SHerbert Xu 		.proc_handler	= proc, \
156931be3085SHerbert Xu 		.extra1		= &ipv4_devconf, \
157042f811b8SHerbert Xu 	}
157142f811b8SHerbert Xu 
157242f811b8SHerbert Xu #define DEVINET_SYSCTL_RW_ENTRY(attr, name) \
1573f8572d8fSEric W. Biederman 	DEVINET_SYSCTL_ENTRY(attr, name, 0644, devinet_conf_proc)
157442f811b8SHerbert Xu 
157542f811b8SHerbert Xu #define DEVINET_SYSCTL_RO_ENTRY(attr, name) \
1576f8572d8fSEric W. Biederman 	DEVINET_SYSCTL_ENTRY(attr, name, 0444, devinet_conf_proc)
157742f811b8SHerbert Xu 
1578f8572d8fSEric W. Biederman #define DEVINET_SYSCTL_COMPLEX_ENTRY(attr, name, proc) \
1579f8572d8fSEric W. Biederman 	DEVINET_SYSCTL_ENTRY(attr, name, 0644, proc)
158042f811b8SHerbert Xu 
158142f811b8SHerbert Xu #define DEVINET_SYSCTL_FLUSHING_ENTRY(attr, name) \
1582f8572d8fSEric W. Biederman 	DEVINET_SYSCTL_COMPLEX_ENTRY(attr, name, ipv4_doint_and_flush)
158342f811b8SHerbert Xu 
15841da177e4SLinus Torvalds static struct devinet_sysctl_table {
15851da177e4SLinus Torvalds 	struct ctl_table_header *sysctl_header;
158602291680SEric W. Biederman 	struct ctl_table devinet_vars[__IPV4_DEVCONF_MAX];
15871da177e4SLinus Torvalds } devinet_sysctl = {
15881da177e4SLinus Torvalds 	.devinet_vars = {
158942f811b8SHerbert Xu 		DEVINET_SYSCTL_COMPLEX_ENTRY(FORWARDING, "forwarding",
1590f8572d8fSEric W. Biederman 					     devinet_sysctl_forward),
159142f811b8SHerbert Xu 		DEVINET_SYSCTL_RO_ENTRY(MC_FORWARDING, "mc_forwarding"),
159242f811b8SHerbert Xu 
159342f811b8SHerbert Xu 		DEVINET_SYSCTL_RW_ENTRY(ACCEPT_REDIRECTS, "accept_redirects"),
159442f811b8SHerbert Xu 		DEVINET_SYSCTL_RW_ENTRY(SECURE_REDIRECTS, "secure_redirects"),
159542f811b8SHerbert Xu 		DEVINET_SYSCTL_RW_ENTRY(SHARED_MEDIA, "shared_media"),
159642f811b8SHerbert Xu 		DEVINET_SYSCTL_RW_ENTRY(RP_FILTER, "rp_filter"),
159742f811b8SHerbert Xu 		DEVINET_SYSCTL_RW_ENTRY(SEND_REDIRECTS, "send_redirects"),
159842f811b8SHerbert Xu 		DEVINET_SYSCTL_RW_ENTRY(ACCEPT_SOURCE_ROUTE,
159942f811b8SHerbert Xu 					"accept_source_route"),
16008153a10cSPatrick McHardy 		DEVINET_SYSCTL_RW_ENTRY(ACCEPT_LOCAL, "accept_local"),
160128f6aeeaSJamal Hadi Salim 		DEVINET_SYSCTL_RW_ENTRY(SRC_VMARK, "src_valid_mark"),
160242f811b8SHerbert Xu 		DEVINET_SYSCTL_RW_ENTRY(PROXY_ARP, "proxy_arp"),
160342f811b8SHerbert Xu 		DEVINET_SYSCTL_RW_ENTRY(MEDIUM_ID, "medium_id"),
160442f811b8SHerbert Xu 		DEVINET_SYSCTL_RW_ENTRY(BOOTP_RELAY, "bootp_relay"),
160542f811b8SHerbert Xu 		DEVINET_SYSCTL_RW_ENTRY(LOG_MARTIANS, "log_martians"),
160642f811b8SHerbert Xu 		DEVINET_SYSCTL_RW_ENTRY(TAG, "tag"),
160742f811b8SHerbert Xu 		DEVINET_SYSCTL_RW_ENTRY(ARPFILTER, "arp_filter"),
160842f811b8SHerbert Xu 		DEVINET_SYSCTL_RW_ENTRY(ARP_ANNOUNCE, "arp_announce"),
160942f811b8SHerbert Xu 		DEVINET_SYSCTL_RW_ENTRY(ARP_IGNORE, "arp_ignore"),
161042f811b8SHerbert Xu 		DEVINET_SYSCTL_RW_ENTRY(ARP_ACCEPT, "arp_accept"),
1611eefef1cfSStephen Hemminger 		DEVINET_SYSCTL_RW_ENTRY(ARP_NOTIFY, "arp_notify"),
161265324144SJesper Dangaard Brouer 		DEVINET_SYSCTL_RW_ENTRY(PROXY_ARP_PVLAN, "proxy_arp_pvlan"),
161342f811b8SHerbert Xu 
161442f811b8SHerbert Xu 		DEVINET_SYSCTL_FLUSHING_ENTRY(NOXFRM, "disable_xfrm"),
161542f811b8SHerbert Xu 		DEVINET_SYSCTL_FLUSHING_ENTRY(NOPOLICY, "disable_policy"),
161642f811b8SHerbert Xu 		DEVINET_SYSCTL_FLUSHING_ENTRY(FORCE_IGMP_VERSION,
161742f811b8SHerbert Xu 					      "force_igmp_version"),
161842f811b8SHerbert Xu 		DEVINET_SYSCTL_FLUSHING_ENTRY(PROMOTE_SECONDARIES,
161942f811b8SHerbert Xu 					      "promote_secondaries"),
16201da177e4SLinus Torvalds 	},
16211da177e4SLinus Torvalds };
16221da177e4SLinus Torvalds 
1623ea40b324SPavel Emelyanov static int __devinet_sysctl_register(struct net *net, char *dev_name,
1624f8572d8fSEric W. Biederman 					struct ipv4_devconf *p)
16251da177e4SLinus Torvalds {
16261da177e4SLinus Torvalds 	int i;
16279fa89642SPavel Emelyanov 	struct devinet_sysctl_table *t;
16288607ddb8SEric W. Biederman 	char path[sizeof("net/ipv4/conf/") + IFNAMSIZ];
1629bfada697SPavel Emelyanov 
16309fa89642SPavel Emelyanov 	t = kmemdup(&devinet_sysctl, sizeof(*t), GFP_KERNEL);
16311da177e4SLinus Torvalds 	if (!t)
16329fa89642SPavel Emelyanov 		goto out;
16339fa89642SPavel Emelyanov 
16341da177e4SLinus Torvalds 	for (i = 0; i < ARRAY_SIZE(t->devinet_vars) - 1; i++) {
16351da177e4SLinus Torvalds 		t->devinet_vars[i].data += (char *)p - (char *)&ipv4_devconf;
163631be3085SHerbert Xu 		t->devinet_vars[i].extra1 = p;
1637c0ce9fb3SPavel Emelyanov 		t->devinet_vars[i].extra2 = net;
16381da177e4SLinus Torvalds 	}
16391da177e4SLinus Torvalds 
16408607ddb8SEric W. Biederman 	snprintf(path, sizeof(path), "net/ipv4/conf/%s", dev_name);
16411da177e4SLinus Torvalds 
16428607ddb8SEric W. Biederman 	t->sysctl_header = register_net_sysctl(net, path, t->devinet_vars);
16431da177e4SLinus Torvalds 	if (!t->sysctl_header)
16448607ddb8SEric W. Biederman 		goto free;
16451da177e4SLinus Torvalds 
16461da177e4SLinus Torvalds 	p->sysctl = t;
1647ea40b324SPavel Emelyanov 	return 0;
16481da177e4SLinus Torvalds 
16491da177e4SLinus Torvalds free:
16501da177e4SLinus Torvalds 	kfree(t);
16519fa89642SPavel Emelyanov out:
1652ea40b324SPavel Emelyanov 	return -ENOBUFS;
16531da177e4SLinus Torvalds }
16541da177e4SLinus Torvalds 
165551602b2aSPavel Emelyanov static void __devinet_sysctl_unregister(struct ipv4_devconf *cnf)
165666f27a52SPavel Emelyanov {
165751602b2aSPavel Emelyanov 	struct devinet_sysctl_table *t = cnf->sysctl;
165866f27a52SPavel Emelyanov 
165951602b2aSPavel Emelyanov 	if (t == NULL)
166051602b2aSPavel Emelyanov 		return;
166151602b2aSPavel Emelyanov 
166251602b2aSPavel Emelyanov 	cnf->sysctl = NULL;
1663ff538818SLucian Adrian Grijincu 	unregister_net_sysctl_table(t->sysctl_header);
16641da177e4SLinus Torvalds 	kfree(t);
16651da177e4SLinus Torvalds }
166651602b2aSPavel Emelyanov 
166751602b2aSPavel Emelyanov static void devinet_sysctl_register(struct in_device *idev)
166851602b2aSPavel Emelyanov {
166954716e3bSEric W. Biederman 	neigh_sysctl_register(idev->dev, idev->arp_parms, "ipv4", NULL);
1670c346dca1SYOSHIFUJI Hideaki 	__devinet_sysctl_register(dev_net(idev->dev), idev->dev->name,
1671f8572d8fSEric W. Biederman 					&idev->cnf);
167251602b2aSPavel Emelyanov }
167351602b2aSPavel Emelyanov 
167451602b2aSPavel Emelyanov static void devinet_sysctl_unregister(struct in_device *idev)
167551602b2aSPavel Emelyanov {
167651602b2aSPavel Emelyanov 	__devinet_sysctl_unregister(&idev->cnf);
167751602b2aSPavel Emelyanov 	neigh_sysctl_unregister(idev->arp_parms);
16781da177e4SLinus Torvalds }
16791da177e4SLinus Torvalds 
168068dd299bSPavel Emelyanov static struct ctl_table ctl_forward_entry[] = {
168168dd299bSPavel Emelyanov 	{
168268dd299bSPavel Emelyanov 		.procname	= "ip_forward",
168368dd299bSPavel Emelyanov 		.data		= &ipv4_devconf.data[
168402291680SEric W. Biederman 					IPV4_DEVCONF_FORWARDING - 1],
168568dd299bSPavel Emelyanov 		.maxlen		= sizeof(int),
168668dd299bSPavel Emelyanov 		.mode		= 0644,
168768dd299bSPavel Emelyanov 		.proc_handler	= devinet_sysctl_forward,
168868dd299bSPavel Emelyanov 		.extra1		= &ipv4_devconf,
1689c0ce9fb3SPavel Emelyanov 		.extra2		= &init_net,
169068dd299bSPavel Emelyanov 	},
169168dd299bSPavel Emelyanov 	{ },
169268dd299bSPavel Emelyanov };
16932a75de0cSEric Dumazet #endif
169468dd299bSPavel Emelyanov 
1695752d14dcSPavel Emelyanov static __net_init int devinet_init_net(struct net *net)
1696752d14dcSPavel Emelyanov {
1697752d14dcSPavel Emelyanov 	int err;
1698752d14dcSPavel Emelyanov 	struct ipv4_devconf *all, *dflt;
16992a75de0cSEric Dumazet #ifdef CONFIG_SYSCTL
17002a75de0cSEric Dumazet 	struct ctl_table *tbl = ctl_forward_entry;
1701752d14dcSPavel Emelyanov 	struct ctl_table_header *forw_hdr;
17022a75de0cSEric Dumazet #endif
1703752d14dcSPavel Emelyanov 
1704752d14dcSPavel Emelyanov 	err = -ENOMEM;
1705752d14dcSPavel Emelyanov 	all = &ipv4_devconf;
1706752d14dcSPavel Emelyanov 	dflt = &ipv4_devconf_dflt;
1707752d14dcSPavel Emelyanov 
170809ad9bc7SOctavian Purdila 	if (!net_eq(net, &init_net)) {
1709752d14dcSPavel Emelyanov 		all = kmemdup(all, sizeof(ipv4_devconf), GFP_KERNEL);
1710752d14dcSPavel Emelyanov 		if (all == NULL)
1711752d14dcSPavel Emelyanov 			goto err_alloc_all;
1712752d14dcSPavel Emelyanov 
1713752d14dcSPavel Emelyanov 		dflt = kmemdup(dflt, sizeof(ipv4_devconf_dflt), GFP_KERNEL);
1714752d14dcSPavel Emelyanov 		if (dflt == NULL)
1715752d14dcSPavel Emelyanov 			goto err_alloc_dflt;
1716752d14dcSPavel Emelyanov 
17172a75de0cSEric Dumazet #ifdef CONFIG_SYSCTL
1718752d14dcSPavel Emelyanov 		tbl = kmemdup(tbl, sizeof(ctl_forward_entry), GFP_KERNEL);
1719752d14dcSPavel Emelyanov 		if (tbl == NULL)
1720752d14dcSPavel Emelyanov 			goto err_alloc_ctl;
1721752d14dcSPavel Emelyanov 
172202291680SEric W. Biederman 		tbl[0].data = &all->data[IPV4_DEVCONF_FORWARDING - 1];
1723752d14dcSPavel Emelyanov 		tbl[0].extra1 = all;
1724752d14dcSPavel Emelyanov 		tbl[0].extra2 = net;
17252a75de0cSEric Dumazet #endif
1726752d14dcSPavel Emelyanov 	}
1727752d14dcSPavel Emelyanov 
1728752d14dcSPavel Emelyanov #ifdef CONFIG_SYSCTL
1729f8572d8fSEric W. Biederman 	err = __devinet_sysctl_register(net, "all", all);
1730752d14dcSPavel Emelyanov 	if (err < 0)
1731752d14dcSPavel Emelyanov 		goto err_reg_all;
1732752d14dcSPavel Emelyanov 
1733f8572d8fSEric W. Biederman 	err = __devinet_sysctl_register(net, "default", dflt);
1734752d14dcSPavel Emelyanov 	if (err < 0)
1735752d14dcSPavel Emelyanov 		goto err_reg_dflt;
1736752d14dcSPavel Emelyanov 
1737752d14dcSPavel Emelyanov 	err = -ENOMEM;
17388607ddb8SEric W. Biederman 	forw_hdr = register_net_sysctl(net, "net/ipv4", tbl);
1739752d14dcSPavel Emelyanov 	if (forw_hdr == NULL)
1740752d14dcSPavel Emelyanov 		goto err_reg_ctl;
17412a75de0cSEric Dumazet 	net->ipv4.forw_hdr = forw_hdr;
1742752d14dcSPavel Emelyanov #endif
1743752d14dcSPavel Emelyanov 
1744752d14dcSPavel Emelyanov 	net->ipv4.devconf_all = all;
1745752d14dcSPavel Emelyanov 	net->ipv4.devconf_dflt = dflt;
1746752d14dcSPavel Emelyanov 	return 0;
1747752d14dcSPavel Emelyanov 
1748752d14dcSPavel Emelyanov #ifdef CONFIG_SYSCTL
1749752d14dcSPavel Emelyanov err_reg_ctl:
1750752d14dcSPavel Emelyanov 	__devinet_sysctl_unregister(dflt);
1751752d14dcSPavel Emelyanov err_reg_dflt:
1752752d14dcSPavel Emelyanov 	__devinet_sysctl_unregister(all);
1753752d14dcSPavel Emelyanov err_reg_all:
1754752d14dcSPavel Emelyanov 	if (tbl != ctl_forward_entry)
1755752d14dcSPavel Emelyanov 		kfree(tbl);
1756752d14dcSPavel Emelyanov err_alloc_ctl:
17572a75de0cSEric Dumazet #endif
1758752d14dcSPavel Emelyanov 	if (dflt != &ipv4_devconf_dflt)
1759752d14dcSPavel Emelyanov 		kfree(dflt);
1760752d14dcSPavel Emelyanov err_alloc_dflt:
1761752d14dcSPavel Emelyanov 	if (all != &ipv4_devconf)
1762752d14dcSPavel Emelyanov 		kfree(all);
1763752d14dcSPavel Emelyanov err_alloc_all:
1764752d14dcSPavel Emelyanov 	return err;
1765752d14dcSPavel Emelyanov }
1766752d14dcSPavel Emelyanov 
1767752d14dcSPavel Emelyanov static __net_exit void devinet_exit_net(struct net *net)
1768752d14dcSPavel Emelyanov {
17692a75de0cSEric Dumazet #ifdef CONFIG_SYSCTL
1770752d14dcSPavel Emelyanov 	struct ctl_table *tbl;
1771752d14dcSPavel Emelyanov 
1772752d14dcSPavel Emelyanov 	tbl = net->ipv4.forw_hdr->ctl_table_arg;
1773752d14dcSPavel Emelyanov 	unregister_net_sysctl_table(net->ipv4.forw_hdr);
1774752d14dcSPavel Emelyanov 	__devinet_sysctl_unregister(net->ipv4.devconf_dflt);
1775752d14dcSPavel Emelyanov 	__devinet_sysctl_unregister(net->ipv4.devconf_all);
1776752d14dcSPavel Emelyanov 	kfree(tbl);
17772a75de0cSEric Dumazet #endif
1778752d14dcSPavel Emelyanov 	kfree(net->ipv4.devconf_dflt);
1779752d14dcSPavel Emelyanov 	kfree(net->ipv4.devconf_all);
1780752d14dcSPavel Emelyanov }
1781752d14dcSPavel Emelyanov 
1782752d14dcSPavel Emelyanov static __net_initdata struct pernet_operations devinet_ops = {
1783752d14dcSPavel Emelyanov 	.init = devinet_init_net,
1784752d14dcSPavel Emelyanov 	.exit = devinet_exit_net,
1785752d14dcSPavel Emelyanov };
1786752d14dcSPavel Emelyanov 
17879f0f7272SThomas Graf static struct rtnl_af_ops inet_af_ops = {
17889f0f7272SThomas Graf 	.family		  = AF_INET,
17899f0f7272SThomas Graf 	.fill_link_af	  = inet_fill_link_af,
17909f0f7272SThomas Graf 	.get_link_af_size = inet_get_link_af_size,
1791cf7afbfeSThomas Graf 	.validate_link_af = inet_validate_link_af,
1792cf7afbfeSThomas Graf 	.set_link_af	  = inet_set_link_af,
17939f0f7272SThomas Graf };
17949f0f7272SThomas Graf 
17951da177e4SLinus Torvalds void __init devinet_init(void)
17961da177e4SLinus Torvalds {
1797fd23c3b3SDavid S. Miller 	int i;
1798fd23c3b3SDavid S. Miller 
1799fd23c3b3SDavid S. Miller 	for (i = 0; i < IN4_ADDR_HSIZE; i++)
1800fd23c3b3SDavid S. Miller 		INIT_HLIST_HEAD(&inet_addr_lst[i]);
1801fd23c3b3SDavid S. Miller 
1802752d14dcSPavel Emelyanov 	register_pernet_subsys(&devinet_ops);
1803752d14dcSPavel Emelyanov 
18041da177e4SLinus Torvalds 	register_gifconf(PF_INET, inet_gifconf);
18051da177e4SLinus Torvalds 	register_netdevice_notifier(&ip_netdev_notifier);
180663f3444fSThomas Graf 
18079f0f7272SThomas Graf 	rtnl_af_register(&inet_af_ops);
18089f0f7272SThomas Graf 
1809c7ac8679SGreg Rose 	rtnl_register(PF_INET, RTM_NEWADDR, inet_rtm_newaddr, NULL, NULL);
1810c7ac8679SGreg Rose 	rtnl_register(PF_INET, RTM_DELADDR, inet_rtm_deladdr, NULL, NULL);
1811c7ac8679SGreg Rose 	rtnl_register(PF_INET, RTM_GETADDR, NULL, inet_dump_ifaddr, NULL);
18121da177e4SLinus Torvalds }
18131da177e4SLinus Torvalds 
1814