devinet.c 66 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9
/*
 *	NET3	IP device support routines.
 *
 *		This program is free software; you can redistribute it and/or
 *		modify it under the terms of the GNU General Public License
 *		as published by the Free Software Foundation; either version
 *		2 of the License, or (at your option) any later version.
 *
 *	Derived from the IP parts of dev.c 1.0.19
10
 * 		Authors:	Ross Biro
Linus Torvalds's avatar
Linus Torvalds committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 *				Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
 *				Mark Evans, <evansmp@uhura.aston.ac.uk>
 *
 *	Additional Authors:
 *		Alan Cox, <gw4pts@gw4pts.ampr.org>
 *		Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
 *
 *	Changes:
 *		Alexey Kuznetsov:	pa_* fields are replaced with ifaddr
 *					lists.
 *		Cyrus Durgin:		updated for kmod
 *		Matthias Andree:	in devinet_ioctl, compare label and
 *					address (4.4BSD alias style support),
 *					fall back to comparing just the label
 *					if no match found.
 */


29
#include <linux/uaccess.h>
Linus Torvalds's avatar
Linus Torvalds committed
30
#include <linux/bitops.h>
31
#include <linux/capability.h>
Linus Torvalds's avatar
Linus Torvalds committed
32 33 34
#include <linux/module.h>
#include <linux/types.h>
#include <linux/kernel.h>
35
#include <linux/sched/signal.h>
Linus Torvalds's avatar
Linus Torvalds committed
36 37 38 39 40 41 42
#include <linux/string.h>
#include <linux/mm.h>
#include <linux/socket.h>
#include <linux/sockios.h>
#include <linux/in.h>
#include <linux/errno.h>
#include <linux/interrupt.h>
43
#include <linux/if_addr.h>
Linus Torvalds's avatar
Linus Torvalds committed
44 45 46 47 48 49 50 51 52
#include <linux/if_ether.h>
#include <linux/inet.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/skbuff.h>
#include <linux/init.h>
#include <linux/notifier.h>
#include <linux/inetdevice.h>
#include <linux/igmp.h>
53
#include <linux/slab.h>
54
#include <linux/hash.h>
Linus Torvalds's avatar
Linus Torvalds committed
55 56 57 58
#ifdef CONFIG_SYSCTL
#include <linux/sysctl.h>
#endif
#include <linux/kmod.h>
59
#include <linux/netconf.h>
Linus Torvalds's avatar
Linus Torvalds committed
60

61
#include <net/arp.h>
Linus Torvalds's avatar
Linus Torvalds committed
62 63 64
#include <net/ip.h>
#include <net/route.h>
#include <net/ip_fib.h>
65
#include <net/rtnetlink.h>
66
#include <net/net_namespace.h>
67
#include <net/addrconf.h>
Linus Torvalds's avatar
Linus Torvalds committed
68

69
static struct ipv4_devconf ipv4_devconf = {
70
	.data = {
71 72 73 74
		[IPV4_DEVCONF_ACCEPT_REDIRECTS - 1] = 1,
		[IPV4_DEVCONF_SEND_REDIRECTS - 1] = 1,
		[IPV4_DEVCONF_SECURE_REDIRECTS - 1] = 1,
		[IPV4_DEVCONF_SHARED_MEDIA - 1] = 1,
75 76
		[IPV4_DEVCONF_IGMPV2_UNSOLICITED_REPORT_INTERVAL - 1] = 10000 /*ms*/,
		[IPV4_DEVCONF_IGMPV3_UNSOLICITED_REPORT_INTERVAL - 1] =  1000 /*ms*/,
77
	},
Linus Torvalds's avatar
Linus Torvalds committed
78 79 80
};

static struct ipv4_devconf ipv4_devconf_dflt = {
81
	.data = {
82 83 84 85 86
		[IPV4_DEVCONF_ACCEPT_REDIRECTS - 1] = 1,
		[IPV4_DEVCONF_SEND_REDIRECTS - 1] = 1,
		[IPV4_DEVCONF_SECURE_REDIRECTS - 1] = 1,
		[IPV4_DEVCONF_SHARED_MEDIA - 1] = 1,
		[IPV4_DEVCONF_ACCEPT_SOURCE_ROUTE - 1] = 1,
87 88
		[IPV4_DEVCONF_IGMPV2_UNSOLICITED_REPORT_INTERVAL - 1] = 10000 /*ms*/,
		[IPV4_DEVCONF_IGMPV3_UNSOLICITED_REPORT_INTERVAL - 1] =  1000 /*ms*/,
89
	},
Linus Torvalds's avatar
Linus Torvalds committed
90 91
};

92 93
#define IPV4_DEVCONF_DFLT(net, attr) \
	IPV4_DEVCONF((*net->ipv4.devconf_dflt), attr)
94

95
static const struct nla_policy ifa_ipv4_policy[IFA_MAX+1] = {
96 97 98
	[IFA_LOCAL]     	= { .type = NLA_U32 },
	[IFA_ADDRESS]   	= { .type = NLA_U32 },
	[IFA_BROADCAST] 	= { .type = NLA_U32 },
99
	[IFA_LABEL]     	= { .type = NLA_STRING, .len = IFNAMSIZ - 1 },
100
	[IFA_CACHEINFO]		= { .len = sizeof(struct ifa_cacheinfo) },
101
	[IFA_FLAGS]		= { .type = NLA_U32 },
102
	[IFA_RT_PRIORITY]	= { .type = NLA_U32 },
103
	[IFA_TARGET_NETNSID]	= { .type = NLA_S32 },
104 105
};

106 107 108 109 110 111
struct inet_fill_args {
	u32 portid;
	u32 seq;
	int event;
	unsigned int flags;
	int netnsid;
112
	int ifindex;
113 114
};

115 116 117
#define IN4_ADDR_HSIZE_SHIFT	8
#define IN4_ADDR_HSIZE		(1U << IN4_ADDR_HSIZE_SHIFT)

118 119
static struct hlist_head inet_addr_lst[IN4_ADDR_HSIZE];

120
static u32 inet_addr_hash(const struct net *net, __be32 addr)
121
{
122
	u32 val = (__force u32) addr ^ net_hash_mix(net);
123

124
	return hash_32(val, IN4_ADDR_HSIZE_SHIFT);
125 126 127 128
}

static void inet_hash_insert(struct net *net, struct in_ifaddr *ifa)
{
129
	u32 hash = inet_addr_hash(net, ifa->ifa_local);
130

131
	ASSERT_RTNL();
132 133 134 135 136
	hlist_add_head_rcu(&ifa->hash, &inet_addr_lst[hash]);
}

static void inet_hash_remove(struct in_ifaddr *ifa)
{
137
	ASSERT_RTNL();
138 139 140
	hlist_del_init_rcu(&ifa->hash);
}

141 142 143 144 145 146 147 148 149 150 151 152 153 154
/**
 * __ip_dev_find - find the first device with a given source address.
 * @net: the net namespace
 * @addr: the source address
 * @devref: if true, take a reference on the found device
 *
 * If a caller uses devref=false, it should be protected by RCU, or RTNL
 */
struct net_device *__ip_dev_find(struct net *net, __be32 addr, bool devref)
{
	struct net_device *result = NULL;
	struct in_ifaddr *ifa;

	rcu_read_lock();
155 156
	ifa = inet_lookup_ifaddr_rcu(net, addr);
	if (!ifa) {
157 158 159 160 161 162 163 164 165 166 167 168
		struct flowi4 fl4 = { .daddr = addr };
		struct fib_result res = { 0 };
		struct fib_table *local;

		/* Fallback to FIB local table so that communication
		 * over loopback subnets work.
		 */
		local = fib_get_table(net, RT_TABLE_LOCAL);
		if (local &&
		    !fib_table_lookup(local, &fl4, &res, FIB_LOOKUP_NOREF) &&
		    res.type == RTN_LOCAL)
			result = FIB_RES_DEV(res);
169 170
	} else {
		result = ifa->ifa_dev->dev;
171
	}
172 173 174 175 176 177 178
	if (result && devref)
		dev_hold(result);
	rcu_read_unlock();
	return result;
}
EXPORT_SYMBOL(__ip_dev_find);

179 180 181 182 183 184 185 186 187 188 189 190 191 192
/* called under RCU lock */
struct in_ifaddr *inet_lookup_ifaddr_rcu(struct net *net, __be32 addr)
{
	u32 hash = inet_addr_hash(net, addr);
	struct in_ifaddr *ifa;

	hlist_for_each_entry_rcu(ifa, &inet_addr_lst[hash], hash)
		if (ifa->ifa_local == addr &&
		    net_eq(dev_net(ifa->ifa_dev->dev), net))
			return ifa;

	return NULL;
}

193
static void rtmsg_ifa(int event, struct in_ifaddr *, struct nlmsghdr *, u32);
Linus Torvalds's avatar
Linus Torvalds committed
194

195
static BLOCKING_NOTIFIER_HEAD(inetaddr_chain);
196
static BLOCKING_NOTIFIER_HEAD(inetaddr_validator_chain);
Linus Torvalds's avatar
Linus Torvalds committed
197 198 199
static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
			 int destroy);
#ifdef CONFIG_SYSCTL
200
static int devinet_sysctl_register(struct in_device *idev);
201 202
static void devinet_sysctl_unregister(struct in_device *idev);
#else
203
static int devinet_sysctl_register(struct in_device *idev)
204
{
205
	return 0;
206
}
207
static void devinet_sysctl_unregister(struct in_device *idev)
208 209
{
}
Linus Torvalds's avatar
Linus Torvalds committed
210 211 212 213 214 215
#endif

/* Locks all the inet devices. */

static struct in_ifaddr *inet_alloc_ifa(void)
{
216
	return kzalloc(sizeof(struct in_ifaddr), GFP_KERNEL);
Linus Torvalds's avatar
Linus Torvalds committed
217 218 219 220 221 222 223 224 225 226
}

static void inet_rcu_free_ifa(struct rcu_head *head)
{
	struct in_ifaddr *ifa = container_of(head, struct in_ifaddr, rcu_head);
	if (ifa->ifa_dev)
		in_dev_put(ifa->ifa_dev);
	kfree(ifa);
}

227
static void inet_free_ifa(struct in_ifaddr *ifa)
Linus Torvalds's avatar
Linus Torvalds committed
228 229 230 231 232 233 234 235
{
	call_rcu(&ifa->rcu_head, inet_rcu_free_ifa);
}

void in_dev_finish_destroy(struct in_device *idev)
{
	struct net_device *dev = idev->dev;

236 237
	WARN_ON(idev->ifa_list);
	WARN_ON(idev->mc_list);
238
	kfree(rcu_dereference_protected(idev->mc_hash, 1));
Linus Torvalds's avatar
Linus Torvalds committed
239
#ifdef NET_REFCNT_DEBUG
240
	pr_debug("%s: %p=%s\n", __func__, idev, dev ? dev->name : "NIL");
Linus Torvalds's avatar
Linus Torvalds committed
241 242 243
#endif
	dev_put(dev);
	if (!idev->dead)
244 245
		pr_err("Freeing alive in_device %p\n", idev);
	else
Linus Torvalds's avatar
Linus Torvalds committed
246 247
		kfree(idev);
}
248
EXPORT_SYMBOL(in_dev_finish_destroy);
Linus Torvalds's avatar
Linus Torvalds committed
249

250
static struct in_device *inetdev_init(struct net_device *dev)
Linus Torvalds's avatar
Linus Torvalds committed
251 252
{
	struct in_device *in_dev;
253
	int err = -ENOMEM;
Linus Torvalds's avatar
Linus Torvalds committed
254 255 256

	ASSERT_RTNL();

257
	in_dev = kzalloc(sizeof(*in_dev), GFP_KERNEL);
Linus Torvalds's avatar
Linus Torvalds committed
258 259
	if (!in_dev)
		goto out;
260
	memcpy(&in_dev->cnf, dev_net(dev)->ipv4.devconf_dflt,
261
			sizeof(in_dev->cnf));
Linus Torvalds's avatar
Linus Torvalds committed
262 263
	in_dev->cnf.sysctl = NULL;
	in_dev->dev = dev;
264 265
	in_dev->arp_parms = neigh_parms_alloc(dev, &arp_tbl);
	if (!in_dev->arp_parms)
Linus Torvalds's avatar
Linus Torvalds committed
266
		goto out_kfree;
267 268
	if (IPV4_DEVCONF(in_dev->cnf, FORWARDING))
		dev_disable_lro(dev);
Linus Torvalds's avatar
Linus Torvalds committed
269 270
	/* Reference in_dev->dev */
	dev_hold(dev);
271
	/* Account for reference dev->ip_ptr (below) */
272
	refcount_set(&in_dev->refcnt, 1);
Linus Torvalds's avatar
Linus Torvalds committed
273

274 275 276 277 278 279 280
	err = devinet_sysctl_register(in_dev);
	if (err) {
		in_dev->dead = 1;
		in_dev_put(in_dev);
		in_dev = NULL;
		goto out;
	}
Linus Torvalds's avatar
Linus Torvalds committed
281 282 283
	ip_mc_init_dev(in_dev);
	if (dev->flags & IFF_UP)
		ip_mc_up(in_dev);
284

285
	/* we can receive as soon as ip_ptr is set -- do this last */
286
	rcu_assign_pointer(dev->ip_ptr, in_dev);
287
out:
288
	return in_dev ?: ERR_PTR(err);
Linus Torvalds's avatar
Linus Torvalds committed
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
out_kfree:
	kfree(in_dev);
	in_dev = NULL;
	goto out;
}

static void in_dev_rcu_put(struct rcu_head *head)
{
	struct in_device *idev = container_of(head, struct in_device, rcu_head);
	in_dev_put(idev);
}

static void inetdev_destroy(struct in_device *in_dev)
{
	struct in_ifaddr *ifa;
	struct net_device *dev;

	ASSERT_RTNL();

	dev = in_dev->dev;

	in_dev->dead = 1;

	ip_mc_destroy_dev(in_dev);

	while ((ifa = in_dev->ifa_list) != NULL) {
		inet_del_ifa(in_dev, &in_dev->ifa_list, 0);
		inet_free_ifa(ifa);
	}

319
	RCU_INIT_POINTER(dev->ip_ptr, NULL);
Linus Torvalds's avatar
Linus Torvalds committed
320

321
	devinet_sysctl_unregister(in_dev);
Linus Torvalds's avatar
Linus Torvalds committed
322 323 324 325 326 327
	neigh_parms_release(&arp_tbl, in_dev->arp_parms);
	arp_ifdown(dev);

	call_rcu(&in_dev->rcu_head, in_dev_rcu_put);
}

328
int inet_addr_onlink(struct in_device *in_dev, __be32 a, __be32 b)
Linus Torvalds's avatar
Linus Torvalds committed
329 330 331 332 333 334 335 336 337 338 339 340 341 342
{
	rcu_read_lock();
	for_primary_ifa(in_dev) {
		if (inet_ifa_match(a, ifa)) {
			if (!b || inet_ifa_match(b, ifa)) {
				rcu_read_unlock();
				return 1;
			}
		}
	} endfor_ifa(in_dev);
	rcu_read_unlock();
	return 0;
}

343
static void __inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
344
			 int destroy, struct nlmsghdr *nlh, u32 portid)
Linus Torvalds's avatar
Linus Torvalds committed
345
{
346
	struct in_ifaddr *promote = NULL;
347 348 349 350
	struct in_ifaddr *ifa, *ifa1 = *ifap;
	struct in_ifaddr *last_prim = in_dev->ifa_list;
	struct in_ifaddr *prev_prom = NULL;
	int do_promote = IN_DEV_PROMOTE_SECONDARIES(in_dev);
Linus Torvalds's avatar
Linus Torvalds committed
351 352 353

	ASSERT_RTNL();

354 355 356
	if (in_dev->dead)
		goto no_promotions;

357
	/* 1. Deleting primary ifaddr forces deletion all secondaries
358 359
	 * unless alias promotion is set
	 **/
Linus Torvalds's avatar
Linus Torvalds committed
360 361 362 363 364

	if (!(ifa1->ifa_flags & IFA_F_SECONDARY)) {
		struct in_ifaddr **ifap1 = &ifa1->ifa_next;

		while ((ifa = *ifap1) != NULL) {
365
			if (!(ifa->ifa_flags & IFA_F_SECONDARY) &&
366 367 368
			    ifa1->ifa_scope <= ifa->ifa_scope)
				last_prim = ifa;

Linus Torvalds's avatar
Linus Torvalds committed
369 370 371 372
			if (!(ifa->ifa_flags & IFA_F_SECONDARY) ||
			    ifa1->ifa_mask != ifa->ifa_mask ||
			    !inet_ifa_match(ifa1->ifa_address, ifa)) {
				ifap1 = &ifa->ifa_next;
373
				prev_prom = ifa;
Linus Torvalds's avatar
Linus Torvalds committed
374 375 376
				continue;
			}

377
			if (!do_promote) {
378
				inet_hash_remove(ifa);
379
				*ifap1 = ifa->ifa_next;
Linus Torvalds's avatar
Linus Torvalds committed
380

381
				rtmsg_ifa(RTM_DELADDR, ifa, nlh, portid);
382 383
				blocking_notifier_call_chain(&inetaddr_chain,
						NETDEV_DOWN, ifa);
384 385 386 387 388
				inet_free_ifa(ifa);
			} else {
				promote = ifa;
				break;
			}
Linus Torvalds's avatar
Linus Torvalds committed
389 390 391
		}
	}

392 393 394 395 396 397 398 399 400 401 402
	/* On promotion all secondaries from subnet are changing
	 * the primary IP, we must remove all their routes silently
	 * and later to add them back with new prefsrc. Do this
	 * while all addresses are on the device list.
	 */
	for (ifa = promote; ifa; ifa = ifa->ifa_next) {
		if (ifa1->ifa_mask == ifa->ifa_mask &&
		    inet_ifa_match(ifa1->ifa_address, ifa))
			fib_del_ifaddr(ifa, ifa1);
	}

403
no_promotions:
Linus Torvalds's avatar
Linus Torvalds committed
404 405 406
	/* 2. Unlink it */

	*ifap = ifa1->ifa_next;
407
	inet_hash_remove(ifa1);
Linus Torvalds's avatar
Linus Torvalds committed
408 409 410 411 412 413 414 415 416 417 418

	/* 3. Announce address deletion */

	/* Send message first, then call notifier.
	   At first sight, FIB update triggered by notifier
	   will refer to already deleted ifaddr, that could confuse
	   netlink listeners. It is not true: look, gated sees
	   that route deleted and if it still thinks that ifaddr
	   is valid, it will try to restore deleted routes... Grr.
	   So that, this order is correct.
	 */
419
	rtmsg_ifa(RTM_DELADDR, ifa1, nlh, portid);
420
	blocking_notifier_call_chain(&inetaddr_chain, NETDEV_DOWN, ifa1);
Linus Torvalds's avatar
Linus Torvalds committed
421

422
	if (promote) {
423
		struct in_ifaddr *next_sec = promote->ifa_next;
424 425 426 427 428 429

		if (prev_prom) {
			prev_prom->ifa_next = promote->ifa_next;
			promote->ifa_next = last_prim->ifa_next;
			last_prim->ifa_next = promote;
		}
430 431

		promote->ifa_flags &= ~IFA_F_SECONDARY;
432
		rtmsg_ifa(RTM_NEWADDR, promote, nlh, portid);
433 434
		blocking_notifier_call_chain(&inetaddr_chain,
				NETDEV_UP, promote);
435
		for (ifa = next_sec; ifa; ifa = ifa->ifa_next) {
436 437 438 439 440 441 442
			if (ifa1->ifa_mask != ifa->ifa_mask ||
			    !inet_ifa_match(ifa1->ifa_address, ifa))
					continue;
			fib_add_ifaddr(ifa);
		}

	}
443
	if (destroy)
444
		inet_free_ifa(ifa1);
Linus Torvalds's avatar
Linus Torvalds committed
445 446
}

447 448 449 450 451 452
static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
			 int destroy)
{
	__inet_del_ifa(in_dev, ifap, destroy, NULL, 0);
}

453 454 455 456
static void check_lifetime(struct work_struct *work);

static DECLARE_DELAYED_WORK(check_lifetime_work, check_lifetime);

457
static int __inet_insert_ifa(struct in_ifaddr *ifa, struct nlmsghdr *nlh,
458
			     u32 portid, struct netlink_ext_ack *extack)
Linus Torvalds's avatar
Linus Torvalds committed
459 460 461
{
	struct in_device *in_dev = ifa->ifa_dev;
	struct in_ifaddr *ifa1, **ifap, **last_primary;
462 463
	struct in_validator_info ivi;
	int ret;
Linus Torvalds's avatar
Linus Torvalds committed
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493

	ASSERT_RTNL();

	if (!ifa->ifa_local) {
		inet_free_ifa(ifa);
		return 0;
	}

	ifa->ifa_flags &= ~IFA_F_SECONDARY;
	last_primary = &in_dev->ifa_list;

	for (ifap = &in_dev->ifa_list; (ifa1 = *ifap) != NULL;
	     ifap = &ifa1->ifa_next) {
		if (!(ifa1->ifa_flags & IFA_F_SECONDARY) &&
		    ifa->ifa_scope <= ifa1->ifa_scope)
			last_primary = &ifa1->ifa_next;
		if (ifa1->ifa_mask == ifa->ifa_mask &&
		    inet_ifa_match(ifa1->ifa_address, ifa)) {
			if (ifa1->ifa_local == ifa->ifa_local) {
				inet_free_ifa(ifa);
				return -EEXIST;
			}
			if (ifa1->ifa_scope != ifa->ifa_scope) {
				inet_free_ifa(ifa);
				return -EINVAL;
			}
			ifa->ifa_flags |= IFA_F_SECONDARY;
		}
	}

494 495 496 497 498 499 500 501 502
	/* Allow any devices that wish to register ifaddr validtors to weigh
	 * in now, before changes are committed.  The rntl lock is serializing
	 * access here, so the state should not change between a validator call
	 * and a final notify on commit.  This isn't invoked on promotion under
	 * the assumption that validators are checking the address itself, and
	 * not the flags.
	 */
	ivi.ivi_addr = ifa->ifa_address;
	ivi.ivi_dev = ifa->ifa_dev;
503
	ivi.extack = extack;
504 505 506 507 508 509 510 511
	ret = blocking_notifier_call_chain(&inetaddr_validator_chain,
					   NETDEV_UP, &ivi);
	ret = notifier_to_errno(ret);
	if (ret) {
		inet_free_ifa(ifa);
		return ret;
	}

Linus Torvalds's avatar
Linus Torvalds committed
512
	if (!(ifa->ifa_flags & IFA_F_SECONDARY)) {
513
		prandom_seed((__force u32) ifa->ifa_local);
Linus Torvalds's avatar
Linus Torvalds committed
514 515 516 517 518 519
		ifap = last_primary;
	}

	ifa->ifa_next = *ifap;
	*ifap = ifa;

520 521
	inet_hash_insert(dev_net(in_dev->dev), ifa);

522
	cancel_delayed_work(&check_lifetime_work);
523
	queue_delayed_work(system_power_efficient_wq, &check_lifetime_work, 0);
524

Linus Torvalds's avatar
Linus Torvalds committed
525 526 527
	/* Send message first, then call notifier.
	   Notifier will trigger FIB update, so that
	   listeners of netlink will know about new ifaddr */
528
	rtmsg_ifa(RTM_NEWADDR, ifa, nlh, portid);
529
	blocking_notifier_call_chain(&inetaddr_chain, NETDEV_UP, ifa);
Linus Torvalds's avatar
Linus Torvalds committed
530 531 532 533

	return 0;
}

534 535
static int inet_insert_ifa(struct in_ifaddr *ifa)
{
536
	return __inet_insert_ifa(ifa, NULL, 0, NULL);
537 538
}

Linus Torvalds's avatar
Linus Torvalds committed
539 540
static int inet_set_ifa(struct net_device *dev, struct in_ifaddr *ifa)
{
541
	struct in_device *in_dev = __in_dev_get_rtnl(dev);
Linus Torvalds's avatar
Linus Torvalds committed
542 543 544 545

	ASSERT_RTNL();

	if (!in_dev) {
546 547
		inet_free_ifa(ifa);
		return -ENOBUFS;
Linus Torvalds's avatar
Linus Torvalds committed
548
	}
549
	ipv4_devconf_setall(in_dev);
550
	neigh_parms_data_state_setall(in_dev->arp_parms);
Linus Torvalds's avatar
Linus Torvalds committed
551
	if (ifa->ifa_dev != in_dev) {
552
		WARN_ON(ifa->ifa_dev);
Linus Torvalds's avatar
Linus Torvalds committed
553 554 555
		in_dev_hold(in_dev);
		ifa->ifa_dev = in_dev;
	}
556
	if (ipv4_is_loopback(ifa->ifa_local))
Linus Torvalds's avatar
Linus Torvalds committed
557 558 559 560
		ifa->ifa_scope = RT_SCOPE_HOST;
	return inet_insert_ifa(ifa);
}

561 562 563
/* Caller must hold RCU or RTNL :
 * We dont take a reference on found in_device
 */
564
struct in_device *inetdev_by_index(struct net *net, int ifindex)
Linus Torvalds's avatar
Linus Torvalds committed
565 566 567
{
	struct net_device *dev;
	struct in_device *in_dev = NULL;
568 569 570

	rcu_read_lock();
	dev = dev_get_by_index_rcu(net, ifindex);
Linus Torvalds's avatar
Linus Torvalds committed
571
	if (dev)
572
		in_dev = rcu_dereference_rtnl(dev->ip_ptr);
573
	rcu_read_unlock();
Linus Torvalds's avatar
Linus Torvalds committed
574 575
	return in_dev;
}
576
EXPORT_SYMBOL(inetdev_by_index);
Linus Torvalds's avatar
Linus Torvalds committed
577 578 579

/* Called only from RTNL semaphored context. No locks. */

580 581
struct in_ifaddr *inet_ifa_byprefix(struct in_device *in_dev, __be32 prefix,
				    __be32 mask)
Linus Torvalds's avatar
Linus Torvalds committed
582 583 584 585 586 587 588 589 590 591
{
	ASSERT_RTNL();

	for_primary_ifa(in_dev) {
		if (ifa->ifa_mask == mask && inet_ifa_match(prefix, ifa))
			return ifa;
	} endfor_ifa(in_dev);
	return NULL;
}

592 593 594 595 596 597 598 599 600 601 602 603
static int ip_mc_config(struct sock *sk, bool join, const struct in_ifaddr *ifa)
{
	struct ip_mreqn mreq = {
		.imr_multiaddr.s_addr = ifa->ifa_address,
		.imr_ifindex = ifa->ifa_dev->dev->ifindex,
	};
	int ret;

	ASSERT_RTNL();

	lock_sock(sk);
	if (join)
604
		ret = ip_mc_join_group(sk, &mreq);
605
	else
606
		ret = ip_mc_leave_group(sk, &mreq);
607 608 609 610 611
	release_sock(sk);

	return ret;
}

612 613
static int inet_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh,
			    struct netlink_ext_ack *extack)
Linus Torvalds's avatar
Linus Torvalds committed
614
{
615
	struct net *net = sock_net(skb->sk);
616
	struct nlattr *tb[IFA_MAX+1];
Linus Torvalds's avatar
Linus Torvalds committed
617
	struct in_device *in_dev;
618
	struct ifaddrmsg *ifm;
Linus Torvalds's avatar
Linus Torvalds committed
619
	struct in_ifaddr *ifa, **ifap;
620
	int err = -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
621 622 623

	ASSERT_RTNL();

624
	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv4_policy,
625
			  extack);
626 627 628 629
	if (err < 0)
		goto errout;

	ifm = nlmsg_data(nlh);
630
	in_dev = inetdev_by_index(net, ifm->ifa_index);
631
	if (!in_dev) {
632 633 634 635
		err = -ENODEV;
		goto errout;
	}

Linus Torvalds's avatar
Linus Torvalds committed
636 637
	for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
	     ifap = &ifa->ifa_next) {
638
		if (tb[IFA_LOCAL] &&
639
		    ifa->ifa_local != nla_get_in_addr(tb[IFA_LOCAL]))
640 641 642
			continue;

		if (tb[IFA_LABEL] && nla_strcmp(tb[IFA_LABEL], ifa->ifa_label))
Linus Torvalds's avatar
Linus Torvalds committed
643
			continue;
644 645 646

		if (tb[IFA_ADDRESS] &&
		    (ifm->ifa_prefixlen != ifa->ifa_prefixlen ||
647
		    !inet_ifa_match(nla_get_in_addr(tb[IFA_ADDRESS]), ifa)))
648 649
			continue;

650 651
		if (ipv4_is_multicast(ifa->ifa_address))
			ip_mc_config(net->ipv4.mc_autojoin_sk, false, ifa);
652
		__inet_del_ifa(in_dev, ifap, 1, nlh, NETLINK_CB(skb).portid);
Linus Torvalds's avatar
Linus Torvalds committed
653 654
		return 0;
	}
655 656 657 658

	err = -EADDRNOTAVAIL;
errout:
	return err;
Linus Torvalds's avatar
Linus Torvalds committed
659 660
}

661 662 663 664 665 666
#define INFINITY_LIFE_TIME	0xFFFFFFFF

static void check_lifetime(struct work_struct *work)
{
	unsigned long now, next, next_sec, next_sched;
	struct in_ifaddr *ifa;
667
	struct hlist_node *n;
668 669 670 671 672 673
	int i;

	now = jiffies;
	next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);

	for (i = 0; i < IN4_ADDR_HSIZE; i++) {
674 675 676
		bool change_needed = false;

		rcu_read_lock();
677
		hlist_for_each_entry_rcu(ifa, &inet_addr_lst[i], hash) {
678 679 680 681 682 683 684 685 686 687 688
			unsigned long age;

			if (ifa->ifa_flags & IFA_F_PERMANENT)
				continue;

			/* We try to batch several events at once. */
			age = (now - ifa->ifa_tstamp +
			       ADDRCONF_TIMER_FUZZ_MINUS) / HZ;

			if (ifa->ifa_valid_lft != INFINITY_LIFE_TIME &&
			    age >= ifa->ifa_valid_lft) {
689
				change_needed = true;
690 691 692 693 694 695 696 697 698
			} else if (ifa->ifa_preferred_lft ==
				   INFINITY_LIFE_TIME) {
				continue;
			} else if (age >= ifa->ifa_preferred_lft) {
				if (time_before(ifa->ifa_tstamp +
						ifa->ifa_valid_lft * HZ, next))
					next = ifa->ifa_tstamp +
					       ifa->ifa_valid_lft * HZ;

699 700
				if (!(ifa->ifa_flags & IFA_F_DEPRECATED))
					change_needed = true;
701 702 703 704 705 706 707
			} else if (time_before(ifa->ifa_tstamp +
					       ifa->ifa_preferred_lft * HZ,
					       next)) {
				next = ifa->ifa_tstamp +
				       ifa->ifa_preferred_lft * HZ;
			}
		}
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
		rcu_read_unlock();
		if (!change_needed)
			continue;
		rtnl_lock();
		hlist_for_each_entry_safe(ifa, n, &inet_addr_lst[i], hash) {
			unsigned long age;

			if (ifa->ifa_flags & IFA_F_PERMANENT)
				continue;

			/* We try to batch several events at once. */
			age = (now - ifa->ifa_tstamp +
			       ADDRCONF_TIMER_FUZZ_MINUS) / HZ;

			if (ifa->ifa_valid_lft != INFINITY_LIFE_TIME &&
			    age >= ifa->ifa_valid_lft) {
				struct in_ifaddr **ifap;

				for (ifap = &ifa->ifa_dev->ifa_list;
				     *ifap != NULL; ifap = &(*ifap)->ifa_next) {
					if (*ifap == ifa) {
						inet_del_ifa(ifa->ifa_dev,
							     ifap, 1);
						break;
					}
				}
			} else if (ifa->ifa_preferred_lft !=
				   INFINITY_LIFE_TIME &&
				   age >= ifa->ifa_preferred_lft &&
				   !(ifa->ifa_flags & IFA_F_DEPRECATED)) {
				ifa->ifa_flags |= IFA_F_DEPRECATED;
				rtmsg_ifa(RTM_NEWADDR, ifa, NULL, 0);
			}
		}
		rtnl_unlock();
743 744 745 746 747 748 749 750 751 752 753 754 755 756
	}

	next_sec = round_jiffies_up(next);
	next_sched = next;

	/* If rounded timeout is accurate enough, accept it. */
	if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
		next_sched = next_sec;

	now = jiffies;
	/* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
	if (time_before(next_sched, now + ADDRCONF_TIMER_FUZZ_MAX))
		next_sched = now + ADDRCONF_TIMER_FUZZ_MAX;

757 758
	queue_delayed_work(system_power_efficient_wq, &check_lifetime_work,
			next_sched - now);
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785
}

static void set_ifa_lifetime(struct in_ifaddr *ifa, __u32 valid_lft,
			     __u32 prefered_lft)
{
	unsigned long timeout;

	ifa->ifa_flags &= ~(IFA_F_PERMANENT | IFA_F_DEPRECATED);

	timeout = addrconf_timeout_fixup(valid_lft, HZ);
	if (addrconf_finite_timeout(timeout))
		ifa->ifa_valid_lft = timeout;
	else
		ifa->ifa_flags |= IFA_F_PERMANENT;

	timeout = addrconf_timeout_fixup(prefered_lft, HZ);
	if (addrconf_finite_timeout(timeout)) {
		if (timeout == 0)
			ifa->ifa_flags |= IFA_F_DEPRECATED;
		ifa->ifa_preferred_lft = timeout;
	}
	ifa->ifa_tstamp = jiffies;
	if (!ifa->ifa_cstamp)
		ifa->ifa_cstamp = ifa->ifa_tstamp;
}

static struct in_ifaddr *rtm_to_ifaddr(struct net *net, struct nlmsghdr *nlh,
786 787
				       __u32 *pvalid_lft, __u32 *pprefered_lft,
				       struct netlink_ext_ack *extack)
Linus Torvalds's avatar
Linus Torvalds committed
788
{
789 790 791
	struct nlattr *tb[IFA_MAX+1];
	struct in_ifaddr *ifa;
	struct ifaddrmsg *ifm;
Linus Torvalds's avatar
Linus Torvalds committed
792 793
	struct net_device *dev;
	struct in_device *in_dev;
794
	int err;
Linus Torvalds's avatar
Linus Torvalds committed
795

796
	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv4_policy,
797
			  extack);
798 799
	if (err < 0)
		goto errout;
Linus Torvalds's avatar
Linus Torvalds committed
800

801
	ifm = nlmsg_data(nlh);
802
	err = -EINVAL;
803
	if (ifm->ifa_prefixlen > 32 || !tb[IFA_LOCAL])
804
		goto errout;
Linus Torvalds's avatar
Linus Torvalds committed
805

806
	dev = __dev_get_by_index(net, ifm->ifa_index);
807
	err = -ENODEV;
808
	if (!dev)
809
		goto errout;
Linus Torvalds's avatar
Linus Torvalds committed
810

811
	in_dev = __in_dev_get_rtnl(dev);
812
	err = -ENOBUFS;
813
	if (!in_dev)
814
		goto errout;
Linus Torvalds's avatar
Linus Torvalds committed
815

816
	ifa = inet_alloc_ifa();
817
	if (!ifa)
818 819 820 821 822 823
		/*
		 * A potential indev allocation can be left alive, it stays
		 * assigned to its device and is destroy with it.
		 */
		goto errout;

824
	ipv4_devconf_setall(in_dev);
825
	neigh_parms_data_state_setall(in_dev->arp_parms);
826 827
	in_dev_hold(in_dev);

828
	if (!tb[IFA_ADDRESS])
829
		tb[IFA_ADDRESS] = tb[IFA_LOCAL];
Linus Torvalds's avatar
Linus Torvalds committed
830

831
	INIT_HLIST_NODE(&ifa->hash);
Linus Torvalds's avatar
Linus Torvalds committed
832 833
	ifa->ifa_prefixlen = ifm->ifa_prefixlen;
	ifa->ifa_mask = inet_make_mask(ifm->ifa_prefixlen);
834 835
	ifa->ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) :
					 ifm->ifa_flags;
Linus Torvalds's avatar
Linus Torvalds committed
836
	ifa->ifa_scope = ifm->ifa_scope;
837 838
	ifa->ifa_dev = in_dev;

839 840
	ifa->ifa_local = nla_get_in_addr(tb[IFA_LOCAL]);
	ifa->ifa_address = nla_get_in_addr(tb[IFA_ADDRESS]);
841 842

	if (tb[IFA_BROADCAST])
843
		ifa->ifa_broadcast = nla_get_in_addr(tb[IFA_BROADCAST]);
844 845 846

	if (tb[IFA_LABEL])
		nla_strlcpy(ifa->ifa_label, tb[IFA_LABEL], IFNAMSIZ);
Linus Torvalds's avatar
Linus Torvalds committed
847 848 849
	else
		memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);

850 851 852
	if (tb[IFA_RT_PRIORITY])
		ifa->ifa_rt_priority = nla_get_u32(tb[IFA_RT_PRIORITY]);

853 854 855 856 857 858
	if (tb[IFA_CACHEINFO]) {
		struct ifa_cacheinfo *ci;

		ci = nla_data(tb[IFA_CACHEINFO]);
		if (!ci->ifa_valid || ci->ifa_prefered > ci->ifa_valid) {
			err = -EINVAL;
859
			goto errout_free;
860 861 862 863 864
		}
		*pvalid_lft = ci->ifa_valid;
		*pprefered_lft = ci->ifa_prefered;
	}

865 866
	return ifa;

867 868
errout_free:
	inet_free_ifa(ifa);
869 870 871 872
errout:
	return ERR_PTR(err);
}

873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890
static struct in_ifaddr *find_matching_ifa(struct in_ifaddr *ifa)
{
	struct in_device *in_dev = ifa->ifa_dev;
	struct in_ifaddr *ifa1, **ifap;

	if (!ifa->ifa_local)
		return NULL;

	for (ifap = &in_dev->ifa_list; (ifa1 = *ifap) != NULL;
	     ifap = &ifa1->ifa_next) {
		if (ifa1->ifa_mask == ifa->ifa_mask &&
		    inet_ifa_match(ifa1->ifa_address, ifa) &&
		    ifa1->ifa_local == ifa->ifa_local)
			return ifa1;
	}
	return NULL;
}

891 892
static int inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
			    struct netlink_ext_ack *extack)
893
{
894
	struct net *net = sock_net(skb->sk);
895
	struct in_ifaddr *ifa;
896 897 898
	struct in_ifaddr *ifa_existing;
	__u32 valid_lft = INFINITY_LIFE_TIME;
	__u32 prefered_lft = INFINITY_LIFE_TIME;
899 900 901

	ASSERT_RTNL();

902
	ifa = rtm_to_ifaddr(net, nlh, &valid_lft, &prefered_lft, extack);
903 904 905
	if (IS_ERR(ifa))
		return PTR_ERR(ifa);

906 907 908
	ifa_existing = find_matching_ifa(ifa);
	if (!ifa_existing) {
		/* It would be best to check for !NLM_F_CREATE here but
909
		 * userspace already relies on not having to provide this.
910 911
		 */
		set_ifa_lifetime(ifa, valid_lft, prefered_lft);
912 913 914 915 916 917 918 919 920
		if (ifa->ifa_flags & IFA_F_MCAUTOJOIN) {
			int ret = ip_mc_config(net->ipv4.mc_autojoin_sk,
					       true, ifa);

			if (ret < 0) {
				inet_free_ifa(ifa);
				return ret;
			}
		}
921 922
		return __inet_insert_ifa(ifa, nlh, NETLINK_CB(skb).portid,
					 extack);
923
	} else {
924 925
		u32 new_metric = ifa->ifa_rt_priority;

926 927 928 929 930
		inet_free_ifa(ifa);

		if (nlh->nlmsg_flags & NLM_F_EXCL ||
		    !(nlh->nlmsg_flags & NLM_F_REPLACE))
			return -EEXIST;
931
		ifa = ifa_existing;
932 933 934 935 936 937

		if (ifa->ifa_rt_priority != new_metric) {
			fib_modify_prefix_metric(ifa, new_metric);
			ifa->ifa_rt_priority = new_metric;
		}

938
		set_ifa_lifetime(ifa, valid_lft, prefered_lft);
939
		cancel_delayed_work(&check_lifetime_work);
940 941
		queue_delayed_work(system_power_efficient_wq,
				&check_lifetime_work, 0);
942
		rtmsg_ifa(RTM_NEWADDR, ifa, nlh, NETLINK_CB(skb).portid);
943 944
	}
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
945 946 947 948 949 950
}

/*
 *	Determine a default network mask, based on the IP address.
 */

951
static int inet_abc_len(__be32 addr)
Linus Torvalds's avatar
Linus Torvalds committed
952 953 954
{
	int rc = -1;	/* Something else, probably a multicast. */

955
	if (ipv4_is_zeronet(addr) || ipv4_is_lbcast(addr))
956
		rc = 0;
Linus Torvalds's avatar
Linus Torvalds committed
957
	else {
958 959
		__u32 haddr = ntohl(addr);
		if (IN_CLASSA(haddr))
Linus Torvalds's avatar
Linus Torvalds committed
960
			rc = 8;
961
		else if (IN_CLASSB(haddr))
Linus Torvalds's avatar
Linus Torvalds committed
962
			rc = 16;
963
		else if (IN_CLASSC(haddr))
Linus Torvalds's avatar
Linus Torvalds committed
964
			rc = 24;
965 966
		else if (IN_CLASSE(haddr))
			rc = 32;
Linus Torvalds's avatar
Linus Torvalds committed
967 968
	}

969
	return rc;
Linus Torvalds's avatar
Linus Torvalds committed
970 971 972
}


973
int devinet_ioctl(struct net *net, unsigned int cmd, struct ifreq *ifr)
Linus Torvalds's avatar
Linus Torvalds committed
974 975
{
	struct sockaddr_in sin_orig;
976
	struct sockaddr_in *sin = (struct sockaddr_in *)&ifr->ifr_addr;
Linus Torvalds's avatar
Linus Torvalds committed
977 978 979 980 981 982 983 984
	struct in_device *in_dev;
	struct in_ifaddr **ifap = NULL;
	struct in_ifaddr *ifa = NULL;
	struct net_device *dev;
	char *colon;
	int ret = -EFAULT;
	int tryaddrmatch = 0;

985
	ifr->ifr_name[IFNAMSIZ - 1] = 0;
Linus Torvalds's avatar
Linus Torvalds committed
986 987 988 989

	/* save original address for comparison */
	memcpy(&sin_orig, sin, sizeof(*sin));

990
	colon = strchr(ifr->ifr_name, ':');
Linus Torvalds's avatar
Linus Torvalds committed
991 992 993
	if (colon)
		*colon = 0;

994
	dev_load(net, ifr->ifr_name);
Linus Torvalds's avatar
Linus Torvalds committed
995

Stephen Hemminger's avatar
Stephen Hemminger committed
996
	switch (cmd) {
Linus Torvalds's avatar
Linus Torvalds committed
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
	case SIOCGIFADDR:	/* Get interface address */
	case SIOCGIFBRDADDR:	/* Get the broadcast address */
	case SIOCGIFDSTADDR:	/* Get the destination address */
	case SIOCGIFNETMASK:	/* Get the netmask for the interface */
		/* Note that these ioctls will not sleep,
		   so that we do not impose a lock.
		   One day we will be forced to put shlock here (I mean SMP)
		 */
		tryaddrmatch = (sin_orig.sin_family == AF_INET);
		memset(sin, 0, sizeof(*sin));
		sin->sin_family = AF_INET;
		break;

	case SIOCSIFFLAGS:
1011
		ret = -EPERM;
1012
		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds's avatar
Linus Torvalds committed
1013 1014 1015 1016 1017 1018
			goto out;
		break;
	case SIOCSIFADDR:	/* Set interface address (and family) */
	case SIOCSIFBRDADDR:	/* Set the broadcast address */
	case SIOCSIFDSTADDR:	/* Set the destination address */
	case SIOCSIFNETMASK: 	/* Set the netmask for the interface */
1019
		ret = -EPERM;
1020
		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds's avatar
Linus Torvalds committed
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
			goto out;
		ret = -EINVAL;
		if (sin->sin_family != AF_INET)
			goto out;
		break;
	default:
		ret = -EINVAL;
		goto out;
	}

	rtnl_lock();

	ret = -ENODEV;
1034
	dev = __dev_get_by_name(net, ifr->ifr_name);
1035
	if (!dev)
Linus Torvalds's avatar
Linus Torvalds committed
1036 1037 1038 1039 1040
		goto done;

	if (colon)
		*colon = ':';

1041 1042
	in_dev = __in_dev_get_rtnl(dev);
	if (in_dev) {
Linus Torvalds's avatar
Linus Torvalds committed
1043 1044 1045 1046 1047 1048 1049 1050
		if (tryaddrmatch) {
			/* Matthias Andree */
			/* compare label and address (4.4BSD style) */
			/* note: we only do this for a limited set of ioctls
			   and only if the original address family was AF_INET.
			   This is checked above. */
			for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
			     ifap = &ifa->ifa_next) {
1051
				if (!strcmp(ifr->ifr_name, ifa->ifa_label) &&
Linus Torvalds's avatar
Linus Torvalds committed
1052
				    sin_orig.sin_addr.s_addr ==
1053
							ifa->ifa_local) {
Linus Torvalds's avatar
Linus Torvalds committed
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
					break; /* found */
				}
			}
		}
		/* we didn't get a match, maybe the application is
		   4.3BSD-style and passed in junk so we fall back to
		   comparing just the label */
		if (!ifa) {
			for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
			     ifap = &ifa->ifa_next)
1064
				if (!strcmp(ifr->ifr_name, ifa->ifa_label))
Linus Torvalds's avatar
Linus Torvalds committed
1065 1066 1067 1068 1069 1070 1071 1072
					break;
		}
	}

	ret = -EADDRNOTAVAIL;
	if (!ifa && cmd != SIOCSIFADDR && cmd != SIOCSIFFLAGS)
		goto done;

Stephen Hemminger's avatar
Stephen Hemminger committed
1073
	switch (cmd) {
Linus Torvalds's avatar
Linus Torvalds committed
1074
	case SIOCGIFADDR:	/* Get interface address */
1075
		ret = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1076
		sin->sin_addr.s_addr = ifa->ifa_local;
1077
		break;
Linus Torvalds's avatar
Linus Torvalds committed
1078 1079

	case SIOCGIFBRDADDR:	/* Get the broadcast address */
1080
		ret = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1081
		sin->sin_addr.s_addr = ifa->ifa_broadcast;
1082
		break;
Linus Torvalds's avatar
Linus Torvalds committed
1083 1084

	case SIOCGIFDSTADDR:	/* Get the destination address */
1085
		ret = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1086
		sin->sin_addr.s_addr = ifa->ifa_address;
1087
		break;
Linus Torvalds's avatar
Linus Torvalds committed
1088 1089

	case SIOCGIFNETMASK:	/* Get the netmask for the interface */
1090
		ret = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1091
		sin->sin_addr.s_addr = ifa->ifa_mask;
1092
		break;
Linus Torvalds's avatar
Linus Torvalds committed
1093 1094 1095 1096 1097 1098 1099

	case SIOCSIFFLAGS:
		if (colon) {
			ret = -EADDRNOTAVAIL;
			if (!ifa)
				break;
			ret = 0;
1100
			if (!(ifr->ifr_flags & IFF_UP))
Linus Torvalds's avatar
Linus Torvalds committed
1101 1102 1103
				inet_del_ifa(in_dev, ifap, 1);
			break;
		}
1104
		ret = dev_change_flags(dev, ifr->ifr_flags, NULL);
Linus Torvalds's avatar
Linus Torvalds committed
1105 1106 1107 1108 1109 1110 1111 1112 1113
		break;

	case SIOCSIFADDR:	/* Set interface address (and family) */
		ret = -EINVAL;
		if (inet_abc_len(sin->sin_addr.s_addr) < 0)
			break;

		if (!ifa) {
			ret = -ENOBUFS;
1114 1115
			ifa = inet_alloc_ifa();
			if (!ifa)
Linus Torvalds's avatar
Linus Torvalds committed
1116
				break;
1117
			INIT_HLIST_NODE(&ifa->hash);
Linus Torvalds's avatar
Linus Torvalds committed
1118
			if (colon)
1119
				memcpy(ifa->ifa_label, ifr->ifr_name, IFNAMSIZ);
Linus Torvalds's avatar
Linus Torvalds committed
1120 1121 1122 1123 1124 1125 1126 1127
			else
				memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
		} else {
			ret = 0;
			if (ifa->ifa_local == sin->sin_addr.s_addr)
				break;
			inet_del_ifa(in_dev, ifap, 0);
			ifa->ifa_broadcast = 0;
1128
			ifa->ifa_scope = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
		}

		ifa->ifa_address = ifa->ifa_local = sin->sin_addr.s_addr;

		if (!(dev->flags & IFF_POINTOPOINT)) {
			ifa->ifa_prefixlen = inet_abc_len(ifa->ifa_address);
			ifa->ifa_mask = inet_make_mask(ifa->ifa_prefixlen);
			if ((dev->flags & IFF_BROADCAST) &&
			    ifa->ifa_prefixlen < 31)
				ifa->ifa_broadcast = ifa->ifa_address |
						     ~ifa->ifa_mask;
		} else {
			ifa->ifa_prefixlen = 32;
			ifa->ifa_mask = inet_make_mask(32);
		}
1144
		set_ifa_lifetime(ifa, INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Linus Torvalds's avatar
Linus Torvalds committed
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179
		ret = inet_set_ifa(dev, ifa);
		break;

	case SIOCSIFBRDADDR:	/* Set the broadcast address */
		ret = 0;
		if (ifa->ifa_broadcast != sin->sin_addr.s_addr) {
			inet_del_ifa(in_dev, ifap, 0);
			ifa->ifa_broadcast = sin->sin_addr.s_addr;
			inet_insert_ifa(ifa);
		}
		break;

	case SIOCSIFDSTADDR:	/* Set the destination address */
		ret = 0;
		if (ifa->ifa_address == sin->sin_addr.s_addr)
			break;
		ret = -EINVAL;
		if (inet_abc_len(sin->sin_addr.s_addr) < 0)
			break;
		ret = 0;
		inet_del_ifa(in_dev, ifap, 0);
		ifa->ifa_address = sin->sin_addr.s_addr;
		inet_insert_ifa(ifa);
		break;

	case SIOCSIFNETMASK: 	/* Set the netmask for the interface */

		/*
		 *	The mask we set must be legal.
		 */
		ret = -EINVAL;
		if (bad_mask(sin->sin_addr.s_addr, 0))
			break;
		ret = 0;
		if (ifa->ifa_mask != sin->sin_addr.s_addr) {
1180
			__be32 old_mask = ifa->ifa_mask;
Linus Torvalds's avatar
Linus Torvalds committed
1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193
			inet_del_ifa(in_dev, ifap, 0);
			ifa->ifa_mask = sin->sin_addr.s_addr;
			ifa->ifa_prefixlen = inet_mask_len(ifa->ifa_mask);

			/* See if current broadcast address matches
			 * with current netmask, then recalculate
			 * the broadcast address. Otherwise it's a
			 * funny address, so don't touch it since
			 * the user seems to know what (s)he's doing...
			 */
			if ((dev->flags & IFF_BROADCAST) &&
			    (ifa->ifa_prefixlen < 31) &&
			    (ifa->ifa_broadcast ==
1194
			     (ifa->ifa_local|~old_mask))) {
Linus Torvalds's avatar
Linus Torvalds committed
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
				ifa->ifa_broadcast = (ifa->ifa_local |
						      ~sin->sin_addr.s_addr);
			}
			inet_insert_ifa(ifa);
		}
		break;
	}
done:
	rtnl_unlock();
out:
	return ret;
}

1208
static int inet_gifconf(struct net_device *dev, char __user *buf, int len, int size)
Linus Torvalds's avatar
Linus Torvalds committed
1209
{
1210
	struct in_device *in_dev = __in_dev_get_rtnl(dev);
Linus Torvalds's avatar
Linus Torvalds committed
1211 1212 1213 1214
	struct in_ifaddr *ifa;
	struct ifreq ifr;
	int done = 0;

1215 1216 1217
	if (WARN_ON(size > sizeof(struct ifreq)))
		goto out;

1218
	if (!in_dev)
Linus Torvalds's avatar
Linus Torvalds committed
1219 1220
		goto out;

1221
	for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
Linus Torvalds's avatar
Linus Torvalds committed
1222
		if (!buf) {
1223
			done += size;
Linus Torvalds's avatar
Linus Torvalds committed
1224 1225
			continue;
		}
1226
		if (len < size)
Linus Torvalds's avatar
Linus Torvalds committed
1227 1228
			break;
		memset(&ifr, 0, sizeof(struct ifreq));
1229
		strcpy(ifr.ifr_name, ifa->ifa_label);
Linus Torvalds's avatar
Linus Torvalds committed
1230 1231 1232 1233 1234

		(*(struct sockaddr_in *)&ifr.ifr_addr).sin_family = AF_INET;
		(*(struct sockaddr_in *)&ifr.ifr_addr).sin_addr.s_addr =
								ifa->ifa_local;

1235
		if (copy_to_user(buf + done, &ifr, size)) {
Linus Torvalds's avatar
Linus Torvalds committed
1236 1237 1238
			done = -EFAULT;
			break;
		}
1239 1240
		len  -= size;
		done += size;
Linus Torvalds's avatar
Linus Torvalds committed
1241 1242 1243 1244 1245
	}
out:
	return done;
}

1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
static __be32 in_dev_select_addr(const struct in_device *in_dev,
				 int scope)
{
	for_primary_ifa(in_dev) {
		if (ifa->ifa_scope != RT_SCOPE_LINK &&
		    ifa->ifa_scope <= scope)
			return ifa->ifa_local;
	} endfor_ifa(in_dev);

	return 0;
}

1258
__be32 inet_select_addr(const struct net_device *dev, __be32 dst, int scope)
Linus Torvalds's avatar
Linus Torvalds committed
1259
{
1260
	__be32 addr = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1261
	struct in_device *in_dev;
1262
	struct net *net = dev_net(dev);
1263
	int master_idx;
Linus Torvalds's avatar
Linus Torvalds committed
1264 1265

	rcu_read_lock();
1266
	in_dev = __in_dev_get_rcu(dev);
Linus Torvalds's avatar
Linus Torvalds committed
1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281
	if (!in_dev)
		goto no_in_dev;

	for_primary_ifa(in_dev) {
		if (ifa->ifa_scope > scope)
			continue;
		if (!dst || inet_ifa_match(dst, ifa)) {
			addr = ifa->ifa_local;
			break;
		}
		if (!addr)
			addr = ifa->ifa_local;
	} endfor_ifa(in_dev);

	if (addr)
1282
		goto out_unlock;
1283
no_in_dev:
1284
	master_idx = l3mdev_master_ifindex_rcu(dev);
Linus Torvalds's avatar
Linus Torvalds committed
1285

1286 1287 1288 1289 1290 1291 1292 1293
	/* For VRFs, the VRF device takes the place of the loopback device,
	 * with addresses on it being preferred.  Note in such cases the
	 * loopback device will be among the devices that fail the master_idx
	 * equality check in the loop below.
	 */
	if (master_idx &&
	    (dev = dev_get_by_index_rcu(net, master_idx)) &&
	    (in_dev = __in_dev_get_rcu(dev))) {
1294 1295 1296
		addr = in_dev_select_addr(in_dev, scope);
		if (addr)
			goto out_unlock;
1297 1298
	}

Linus Torvalds's avatar
Linus Torvalds committed
1299
	/* Not loopback addresses on loopback should be preferred
Stephen Hemminger's avatar
Stephen Hemminger committed
1300
	   in this case. It is important that lo is the first interface
Linus Torvalds's avatar
Linus Torvalds committed
1301 1302
	   in dev_base list.
	 */
1303
	for_each_netdev_rcu(net, dev) {
1304 1305 1306
		if (l3mdev_master_ifindex_rcu(dev) != master_idx)
			continue;

1307 1308
		in_dev = __in_dev_get_rcu(dev);
		if (!in_dev)
Linus Torvalds's avatar
Linus Torvalds committed
1309 1310
			continue;

1311 1312 1313
		addr = in_dev_select_addr(in_dev, scope);
		if (addr)
			goto out_unlock;
Linus Torvalds's avatar
Linus Torvalds committed
1314
	}
1315
out_unlock:
Linus Torvalds's avatar
Linus Torvalds committed
1316 1317 1318
	rcu_read_unlock();
	return addr;
}
1319
EXPORT_SYMBOL(inet_select_addr);
Linus Torvalds's avatar
Linus Torvalds committed
1320

1321 1322
static __be32 confirm_addr_indev(struct in_device *in_dev, __be32 dst,
			      __be32 local, int scope)
Linus Torvalds's avatar
Linus Torvalds committed
1323 1324
{
	int same = 0;
1325
	__be32 addr = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354

	for_ifa(in_dev) {
		if (!addr &&
		    (local == ifa->ifa_local || !local) &&
		    ifa->ifa_scope <= scope) {
			addr = ifa->ifa_local;
			if (same)
				break;
		}
		if (!same) {
			same = (!local || inet_ifa_match(local, ifa)) &&
				(!dst || inet_ifa_match(dst, ifa));
			if (same && addr) {
				if (local || !dst)
					break;
				/* Is the selected addr into dst subnet? */
				if (inet_ifa_match(addr, ifa))
					break;
				/* No, then can we use new local src? */
				if (ifa->ifa_scope <= scope) {
					addr = ifa->ifa_local;
					break;
				}
				/* search for large dst subnet for addr */
				same = 0;
			}
		}
	} endfor_ifa(in_dev);

1355
	return same ? addr : 0;
Linus Torvalds's avatar
Linus Torvalds committed
1356 1357 1358 1359
}

/*
 * Confirm that local IP address exists using wildcards:
1360 1361
 * - net: netns to check, cannot be NULL
 * - in_dev: only on this interface, NULL=any interface
Linus Torvalds's avatar
Linus Torvalds committed
1362 1363 1364 1365
 * - dst: only in the same subnet as dst, 0=any dst
 * - local: address, 0=autoselect the local address
 * - scope: maximum allowed scope value for the local address
 */
1366
__be32 inet_confirm_addr(struct net *net, struct in_device *in_dev,
1367
			 __be32 dst, __be32 local, int scope)
Linus Torvalds's avatar
Linus Torvalds committed
1368
{
1369
	__be32 addr = 0;
1370
	struct net_device *dev;
Linus Torvalds's avatar
Linus Torvalds committed
1371

1372
	if (in_dev)
1373
		return confirm_addr_indev(in_dev, dst, local, scope);
Linus Torvalds's avatar
Linus Torvalds committed
1374 1375

	rcu_read_lock();
1376
	for_each_netdev_rcu(net, dev) {
1377 1378
		in_dev = __in_dev_get_rcu(dev);
		if (in_dev) {
Linus Torvalds's avatar
Linus Torvalds committed
1379 1380 1381 1382 1383 1384 1385 1386 1387
			addr = confirm_addr_indev(in_dev, dst, local, scope);
			if (addr)
				break;
		}
	}
	rcu_read_unlock();

	return addr;
}
1388
EXPORT_SYMBOL(inet_confirm_addr);
Linus Torvalds's avatar
Linus Torvalds committed
1389 1390 1391 1392 1393 1394 1395

/*
 *	Device notifier
 */

int register_inetaddr_notifier(struct notifier_block *nb)
{
1396
	return blocking_notifier_chain_register(&inetaddr_chain, nb);
Linus Torvalds's avatar
Linus Torvalds committed
1397
}
1398
EXPORT_SYMBOL(register_inetaddr_notifier);
Linus Torvalds's avatar
Linus Torvalds committed
1399 1400 1401

int unregister_inetaddr_notifier(struct notifier_block *nb)
{