devinet.c 31.9 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3
/*
 *	NET3	IP device support routines.
 *
Linus Torvalds's avatar
Linus Torvalds committed
4
 *	Version: $Id: devinet.c,v 1.44 2001/10/31 21:55:54 davem Exp $
Linus Torvalds's avatar
Linus Torvalds committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 *		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
 * 		Authors:	Ross Biro, <bir7@leland.Stanford.Edu>
 *				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:
21 22
 *		Alexey Kuznetsov:	pa_* fields are replaced with ifaddr
 *					lists.
Linus Torvalds's avatar
Linus Torvalds committed
23
 *		Cyrus Durgin:		updated for kmod
24
 *		Matthias Andree:	in devinet_ioctl, compare label and
Linus Torvalds's avatar
Linus Torvalds committed
25 26 27
 *					address (4.4BSD alias style support),
 *					fall back to comparing just the label
 *					if no match found.
Linus Torvalds's avatar
Linus Torvalds committed
28 29 30
 */

#include <linux/config.h>
31

Linus Torvalds's avatar
Linus Torvalds committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
#include <asm/uaccess.h>
#include <asm/system.h>
#include <asm/bitops.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#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>
#include <linux/if_ether.h>
#include <linux/inet.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/skbuff.h>
#include <linux/rtnetlink.h>
#include <linux/init.h>
#include <linux/notifier.h>
#include <linux/inetdevice.h>
#include <linux/igmp.h>
#ifdef CONFIG_SYSCTL
#include <linux/sysctl.h>
#endif
#include <linux/kmod.h>

#include <net/ip.h>
#include <net/route.h>
#include <net/ip_fib.h>

64
struct ipv4_devconf ipv4_devconf = {
65 66 67 68
	.accept_redirects = 1,
	.send_redirects =  1,
	.secure_redirects = 1,
	.shared_media =	  1,
69 70 71
};

static struct ipv4_devconf ipv4_devconf_dflt = {
72 73 74 75 76
	.accept_redirects =  1,
	.send_redirects =    1,
	.secure_redirects =  1,
	.shared_media =	     1,
	.accept_source_route = 1,
77
};
Linus Torvalds's avatar
Linus Torvalds committed
78 79 80 81

static void rtmsg_ifa(int event, struct in_ifaddr *);

static struct notifier_block *inetaddr_chain;
82 83
static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
			 int destroy);
Linus Torvalds's avatar
Linus Torvalds committed
84
#ifdef CONFIG_SYSCTL
85 86
static void devinet_sysctl_register(struct in_device *in_dev,
				    struct ipv4_devconf *p);
Linus Torvalds's avatar
Linus Torvalds committed
87 88 89 90 91 92 93 94 95 96
static void devinet_sysctl_unregister(struct ipv4_devconf *p);
#endif

int inet_ifa_count;
int inet_dev_count;

/* Locks all the inet devices. */

rwlock_t inetdev_lock = RW_LOCK_UNLOCKED;

97
static struct in_ifaddr *inet_alloc_ifa(void)
Linus Torvalds's avatar
Linus Torvalds committed
98
{
99
	struct in_ifaddr *ifa = kmalloc(sizeof(*ifa), GFP_KERNEL);
Linus Torvalds's avatar
Linus Torvalds committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120

	if (ifa) {
		memset(ifa, 0, sizeof(*ifa));
		inet_ifa_count++;
	}

	return ifa;
}

static __inline__ void inet_free_ifa(struct in_ifaddr *ifa)
{
	if (ifa->ifa_dev)
		__in_dev_put(ifa->ifa_dev);
	kfree(ifa);
	inet_ifa_count--;
}

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

121 122
	BUG_TRAP(!idev->ifa_list);
	BUG_TRAP(!idev->mc_list);
Linus Torvalds's avatar
Linus Torvalds committed
123
#ifdef NET_REFCNT_DEBUG
124 125
	printk(KERN_DEBUG "in_dev_finish_destroy: %p=%s\n",
	       idev, dev ? dev->name : "NIL");
Linus Torvalds's avatar
Linus Torvalds committed
126 127
#endif
	dev_put(dev);
128
	if (!idev->dead)
Linus Torvalds's avatar
Linus Torvalds committed
129
		printk("Freeing alive in_device %p\n", idev);
130 131 132
	else {
		inet_dev_count--;
		kfree(idev);
Linus Torvalds's avatar
Linus Torvalds committed
133 134 135 136 137 138 139 140 141 142 143
	}
}

struct in_device *inetdev_init(struct net_device *dev)
{
	struct in_device *in_dev;

	ASSERT_RTNL();

	in_dev = kmalloc(sizeof(*in_dev), GFP_KERNEL);
	if (!in_dev)
144
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
145 146 147 148 149
	memset(in_dev, 0, sizeof(*in_dev));
	in_dev->lock = RW_LOCK_UNLOCKED;
	memcpy(&in_dev->cnf, &ipv4_devconf_dflt, sizeof(in_dev->cnf));
	in_dev->cnf.sysctl = NULL;
	in_dev->dev = dev;
150 151
	if ((in_dev->arp_parms = neigh_parms_alloc(dev, &arp_tbl)) == NULL)
		goto out_kfree;
Linus Torvalds's avatar
Linus Torvalds committed
152 153 154 155
	inet_dev_count++;
	/* Reference in_dev->dev */
	dev_hold(dev);
#ifdef CONFIG_SYSCTL
156 157
	neigh_sysctl_register(dev, in_dev->arp_parms, NET_IPV4,
			      NET_IPV4_NEIGH, "ipv4");
Linus Torvalds's avatar
Linus Torvalds committed
158 159 160 161 162 163 164 165 166
#endif
	write_lock_bh(&inetdev_lock);
	dev->ip_ptr = in_dev;
	/* Account for reference dev->ip_ptr */
	in_dev_hold(in_dev);
	write_unlock_bh(&inetdev_lock);
#ifdef CONFIG_SYSCTL
	devinet_sysctl_register(in_dev, &in_dev->cnf);
#endif
167
	if (dev->flags & IFF_UP)
Linus Torvalds's avatar
Linus Torvalds committed
168
		ip_mc_up(in_dev);
169
out:
Linus Torvalds's avatar
Linus Torvalds committed
170
	return in_dev;
171 172 173 174
out_kfree:
	kfree(in_dev);
	in_dev = NULL;
	goto out;
Linus Torvalds's avatar
Linus Torvalds committed
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
}

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

	ASSERT_RTNL();

	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);
	}

#ifdef CONFIG_SYSCTL
	devinet_sysctl_unregister(&in_dev->cnf);
#endif
	write_lock_bh(&inetdev_lock);
	in_dev->dev->ip_ptr = NULL;
	/* in_dev_put following below will kill the in_device */
	write_unlock_bh(&inetdev_lock);


	neigh_parms_release(&arp_tbl, in_dev->arp_parms);
	in_dev_put(in_dev);
}

int inet_addr_onlink(struct in_device *in_dev, u32 a, u32 b)
{
	read_lock(&in_dev->lock);
	for_primary_ifa(in_dev) {
		if (inet_ifa_match(a, ifa)) {
			if (!b || inet_ifa_match(b, ifa)) {
				read_unlock(&in_dev->lock);
				return 1;
			}
		}
	} endfor_ifa(in_dev);
	read_unlock(&in_dev->lock);
	return 0;
218
}
Linus Torvalds's avatar
Linus Torvalds committed
219

220 221
static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
			 int destroy)
Linus Torvalds's avatar
Linus Torvalds committed
222 223 224 225 226 227 228
{
	struct in_ifaddr *ifa1 = *ifap;

	ASSERT_RTNL();

	/* 1. Deleting primary ifaddr forces deletion all secondaries */

229
	if (!(ifa1->ifa_flags & IFA_F_SECONDARY)) {
Linus Torvalds's avatar
Linus Torvalds committed
230 231 232
		struct in_ifaddr *ifa;
		struct in_ifaddr **ifap1 = &ifa1->ifa_next;

233 234
		while ((ifa = *ifap1) != NULL) {
			if (!(ifa->ifa_flags & IFA_F_SECONDARY) ||
Linus Torvalds's avatar
Linus Torvalds committed
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
			    ifa1->ifa_mask != ifa->ifa_mask ||
			    !inet_ifa_match(ifa1->ifa_address, ifa)) {
				ifap1 = &ifa->ifa_next;
				continue;
			}
			write_lock_bh(&in_dev->lock);
			*ifap1 = ifa->ifa_next;
			write_unlock_bh(&in_dev->lock);

			rtmsg_ifa(RTM_DELADDR, ifa);
			notifier_call_chain(&inetaddr_chain, NETDEV_DOWN, ifa);
			inet_free_ifa(ifa);
		}
	}

	/* 2. Unlink it */

	write_lock_bh(&in_dev->lock);
	*ifap = ifa1->ifa_next;
	write_unlock_bh(&in_dev->lock);

	/* 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.
	 */
	rtmsg_ifa(RTM_DELADDR, ifa1);
	notifier_call_chain(&inetaddr_chain, NETDEV_DOWN, ifa1);
	if (destroy) {
		inet_free_ifa(ifa1);

271
		if (!in_dev->ifa_list)
Linus Torvalds's avatar
Linus Torvalds committed
272 273 274 275
			inetdev_destroy(in_dev);
	}
}

276
static int inet_insert_ifa(struct in_ifaddr *ifa)
Linus Torvalds's avatar
Linus Torvalds committed
277 278 279 280 281 282
{
	struct in_device *in_dev = ifa->ifa_dev;
	struct in_ifaddr *ifa1, **ifap, **last_primary;

	ASSERT_RTNL();

283
	if (!ifa->ifa_local) {
Linus Torvalds's avatar
Linus Torvalds committed
284 285 286 287 288 289 290
		inet_free_ifa(ifa);
		return 0;
	}

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

291 292 293 294
	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)
Linus Torvalds's avatar
Linus Torvalds committed
295
			last_primary = &ifa1->ifa_next;
296 297
		if (ifa1->ifa_mask == ifa->ifa_mask &&
		    inet_ifa_match(ifa1->ifa_address, ifa)) {
Linus Torvalds's avatar
Linus Torvalds committed
298 299 300 301 302 303 304 305 306 307 308 309
			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;
		}
	}

310
	if (!(ifa->ifa_flags & IFA_F_SECONDARY)) {
Linus Torvalds's avatar
Linus Torvalds committed
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
		net_srandom(ifa->ifa_local);
		ifap = last_primary;
	}

	ifa->ifa_next = *ifap;
	write_lock_bh(&in_dev->lock);
	*ifap = ifa;
	write_unlock_bh(&in_dev->lock);

	/* Send message first, then call notifier.
	   Notifier will trigger FIB update, so that
	   listeners of netlink will know about new ifaddr */
	rtmsg_ifa(RTM_NEWADDR, ifa);
	notifier_call_chain(&inetaddr_chain, NETDEV_UP, ifa);

	return 0;
}

329
static int inet_set_ifa(struct net_device *dev, struct in_ifaddr *ifa)
Linus Torvalds's avatar
Linus Torvalds committed
330 331 332 333 334
{
	struct in_device *in_dev = __in_dev_get(dev);

	ASSERT_RTNL();

335
	if (!in_dev) {
Linus Torvalds's avatar
Linus Torvalds committed
336
		in_dev = inetdev_init(dev);
337
		if (!in_dev) {
Linus Torvalds's avatar
Linus Torvalds committed
338 339 340 341 342
			inet_free_ifa(ifa);
			return -ENOBUFS;
		}
	}
	if (ifa->ifa_dev != in_dev) {
343
		BUG_TRAP(!ifa->ifa_dev);
Linus Torvalds's avatar
Linus Torvalds committed
344
		in_dev_hold(in_dev);
345
		ifa->ifa_dev = in_dev;
Linus Torvalds's avatar
Linus Torvalds committed
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
	}
	if (LOOPBACK(ifa->ifa_local))
		ifa->ifa_scope = RT_SCOPE_HOST;
	return inet_insert_ifa(ifa);
}

struct in_device *inetdev_by_index(int ifindex)
{
	struct net_device *dev;
	struct in_device *in_dev = NULL;
	read_lock(&dev_base_lock);
	dev = __dev_get_by_index(ifindex);
	if (dev)
		in_dev = in_dev_get(dev);
	read_unlock(&dev_base_lock);
	return in_dev;
}

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

366 367
struct in_ifaddr *inet_ifa_byprefix(struct in_device *in_dev, u32 prefix,
				    u32 mask)
Linus Torvalds's avatar
Linus Torvalds committed
368 369 370 371 372 373 374 375 376 377
{
	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;
}

378
int inet_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
Linus Torvalds's avatar
Linus Torvalds committed
379
{
380
	struct rtattr **rta = arg;
Linus Torvalds's avatar
Linus Torvalds committed
381 382 383 384 385 386 387
	struct in_device *in_dev;
	struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
	struct in_ifaddr *ifa, **ifap;

	ASSERT_RTNL();

	if ((in_dev = inetdev_by_index(ifm->ifa_index)) == NULL)
388
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
389 390
	__in_dev_put(in_dev);

391 392 393 394 395 396 397 398
	for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
	     ifap = &ifa->ifa_next) {
		if ((rta[IFA_LOCAL - 1] &&
		     memcmp(RTA_DATA(rta[IFA_LOCAL - 1]),
			    &ifa->ifa_local, 4)) ||
		    (rta[IFA_LABEL - 1] &&
		     strcmp(RTA_DATA(rta[IFA_LABEL - 1]), ifa->ifa_label)) ||
		    (rta[IFA_ADDRESS - 1] &&
Linus Torvalds's avatar
Linus Torvalds committed
399
		     (ifm->ifa_prefixlen != ifa->ifa_prefixlen ||
400 401
		      !inet_ifa_match(*(u32*)RTA_DATA(rta[IFA_ADDRESS - 1]),
			      	      ifa))))
Linus Torvalds's avatar
Linus Torvalds committed
402 403 404 405
			continue;
		inet_del_ifa(in_dev, ifap, 1);
		return 0;
	}
406
out:
Linus Torvalds's avatar
Linus Torvalds committed
407 408 409
	return -EADDRNOTAVAIL;
}

410
int inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
Linus Torvalds's avatar
Linus Torvalds committed
411 412 413 414 415 416
{
	struct rtattr **rta = arg;
	struct net_device *dev;
	struct in_device *in_dev;
	struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
	struct in_ifaddr *ifa;
417
	int rc = -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
418 419 420

	ASSERT_RTNL();

421 422
	if (ifm->ifa_prefixlen > 32 || !rta[IFA_LOCAL - 1])
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
423

424
	rc = -ENODEV;
Linus Torvalds's avatar
Linus Torvalds committed
425
	if ((dev = __dev_get_by_index(ifm->ifa_index)) == NULL)
426
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
427

428
	rc = -ENOBUFS;
Linus Torvalds's avatar
Linus Torvalds committed
429 430 431
	if ((in_dev = __in_dev_get(dev)) == NULL) {
		in_dev = inetdev_init(dev);
		if (!in_dev)
432
			goto out;
Linus Torvalds's avatar
Linus Torvalds committed
433 434 435
	}

	if ((ifa = inet_alloc_ifa()) == NULL)
436
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
437

438 439 440 441
	if (!rta[IFA_ADDRESS - 1])
		rta[IFA_ADDRESS - 1] = rta[IFA_LOCAL - 1];
	memcpy(&ifa->ifa_local, RTA_DATA(rta[IFA_LOCAL - 1]), 4);
	memcpy(&ifa->ifa_address, RTA_DATA(rta[IFA_ADDRESS - 1]), 4);
Linus Torvalds's avatar
Linus Torvalds committed
442 443
	ifa->ifa_prefixlen = ifm->ifa_prefixlen;
	ifa->ifa_mask = inet_make_mask(ifm->ifa_prefixlen);
444 445 446 447 448
	if (rta[IFA_BROADCAST - 1])
		memcpy(&ifa->ifa_broadcast,
		       RTA_DATA(rta[IFA_BROADCAST - 1]), 4);
	if (rta[IFA_ANYCAST - 1])
		memcpy(&ifa->ifa_anycast, RTA_DATA(rta[IFA_ANYCAST - 1]), 4);
Linus Torvalds's avatar
Linus Torvalds committed
449 450 451
	ifa->ifa_flags = ifm->ifa_flags;
	ifa->ifa_scope = ifm->ifa_scope;
	in_dev_hold(in_dev);
452 453 454
	ifa->ifa_dev   = in_dev;
	if (rta[IFA_LABEL - 1])
		memcpy(ifa->ifa_label, RTA_DATA(rta[IFA_LABEL - 1]), IFNAMSIZ);
Linus Torvalds's avatar
Linus Torvalds committed
455 456 457
	else
		memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);

458 459 460
	rc = inet_insert_ifa(ifa);
out:
	return rc;
Linus Torvalds's avatar
Linus Torvalds committed
461 462
}

463 464
/*
 *	Determine a default network mask, based on the IP address.
Linus Torvalds's avatar
Linus Torvalds committed
465 466 467 468
 */

static __inline__ int inet_abc_len(u32 addr)
{
469
	int rc = -1;	/* Something else, probably a multicast. */
Linus Torvalds's avatar
Linus Torvalds committed
470

471 472 473 474 475 476 477 478 479 480 481 482
  	if (ZERONET(addr))
  		rc = 0;
	else {
		addr = ntohl(addr);

		if (IN_CLASSA(addr))
			rc = 8;
		else if (IN_CLASSB(addr))
			rc = 16;
		else if (IN_CLASSC(addr))
			rc = 24;
	}
Linus Torvalds's avatar
Linus Torvalds committed
483

484
  	return rc;
Linus Torvalds's avatar
Linus Torvalds committed
485 486 487 488 489 490
}


int devinet_ioctl(unsigned int cmd, void *arg)
{
	struct ifreq ifr;
Linus Torvalds's avatar
Linus Torvalds committed
491
	struct sockaddr_in sin_orig;
Linus Torvalds's avatar
Linus Torvalds committed
492 493 494 495 496 497
	struct sockaddr_in *sin = (struct sockaddr_in *)&ifr.ifr_addr;
	struct in_device *in_dev;
	struct in_ifaddr **ifap = NULL;
	struct in_ifaddr *ifa = NULL;
	struct net_device *dev;
	char *colon;
498
	int ret = -EFAULT;
Linus Torvalds's avatar
Linus Torvalds committed
499
	int tryaddrmatch = 0;
Linus Torvalds's avatar
Linus Torvalds committed
500 501 502 503 504 505

	/*
	 *	Fetch the caller's info block into kernel space
	 */

	if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
506 507
		goto out;
	ifr.ifr_name[IFNAMSIZ - 1] = 0;
Linus Torvalds's avatar
Linus Torvalds committed
508

Linus Torvalds's avatar
Linus Torvalds committed
509 510 511
	/* save original address for comparison */
	memcpy(&sin_orig, sin, sizeof(*sin));

Linus Torvalds's avatar
Linus Torvalds committed
512 513 514 515 516 517 518 519 520 521 522 523 524
	colon = strchr(ifr.ifr_name, ':');
	if (colon)
		*colon = 0;

#ifdef CONFIG_KMOD
	dev_load(ifr.ifr_name);
#endif

	switch(cmd) {
	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 */
Linus Torvalds's avatar
Linus Torvalds committed
525
		/* Note that these ioctls will not sleep,
Linus Torvalds's avatar
Linus Torvalds committed
526 527 528
		   so that we do not impose a lock.
		   One day we will be forced to put shlock here (I mean SMP)
		 */
Linus Torvalds's avatar
Linus Torvalds committed
529
		tryaddrmatch = (sin_orig.sin_family == AF_INET);
Linus Torvalds's avatar
Linus Torvalds committed
530 531 532 533 534
		memset(sin, 0, sizeof(*sin));
		sin->sin_family = AF_INET;
		break;

	case SIOCSIFFLAGS:
535
		ret = -EACCES;
Linus Torvalds's avatar
Linus Torvalds committed
536
		if (!capable(CAP_NET_ADMIN))
537
			goto out;
Linus Torvalds's avatar
Linus Torvalds committed
538 539 540 541 542
		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 */
543
		ret = -EACCES;
Linus Torvalds's avatar
Linus Torvalds committed
544
		if (!capable(CAP_NET_ADMIN))
545 546
			goto out;
		ret = -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
547
		if (sin->sin_family != AF_INET)
548
			goto out;
Linus Torvalds's avatar
Linus Torvalds committed
549 550
		break;
	default:
551 552
		ret = -EINVAL;
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
553 554 555 556 557
	}

	dev_probe_lock();
	rtnl_lock();

558 559
	ret = -ENODEV;
	if ((dev = __dev_get_by_name(ifr.ifr_name)) == NULL)
Linus Torvalds's avatar
Linus Torvalds committed
560 561 562 563 564
		goto done;

	if (colon)
		*colon = ':';

565
	if ((in_dev = __in_dev_get(dev)) != NULL) {
Linus Torvalds's avatar
Linus Torvalds committed
566 567 568 569 570 571
		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. */
572 573 574 575 576
			for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
			     ifap = &ifa->ifa_next) {
				if (!strcmp(ifr.ifr_name, ifa->ifa_label) &&
				    sin_orig.sin_addr.s_addr ==
							ifa->ifa_address) {
Linus Torvalds's avatar
Linus Torvalds committed
577 578 579 580 581
					break; /* found */
				}
			}
		}
		/* we didn't get a match, maybe the application is
582
		   4.3BSD-style and passed in junk so we fall back to
Linus Torvalds's avatar
Linus Torvalds committed
583
		   comparing just the label */
584 585 586 587
		if (!ifa) {
			for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
			     ifap = &ifa->ifa_next)
				if (!strcmp(ifr.ifr_name, ifa->ifa_label))
Linus Torvalds's avatar
Linus Torvalds committed
588 589
					break;
		}
Linus Torvalds's avatar
Linus Torvalds committed
590 591
	}

592 593
	ret = -EADDRNOTAVAIL;
	if (!ifa && cmd != SIOCSIFADDR && cmd != SIOCSIFFLAGS)
Linus Torvalds's avatar
Linus Torvalds committed
594 595 596
		goto done;

	switch(cmd) {
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
	case SIOCGIFADDR:	/* Get interface address */
		sin->sin_addr.s_addr = ifa->ifa_local;
		goto rarok;

	case SIOCGIFBRDADDR:	/* Get the broadcast address */
		sin->sin_addr.s_addr = ifa->ifa_broadcast;
		goto rarok;

	case SIOCGIFDSTADDR:	/* Get the destination address */
		sin->sin_addr.s_addr = ifa->ifa_address;
		goto rarok;

	case SIOCGIFNETMASK:	/* Get the netmask for the interface */
		sin->sin_addr.s_addr = ifa->ifa_mask;
		goto rarok;

	case SIOCSIFFLAGS:
		if (colon) {
			ret = -EADDRNOTAVAIL;
			if (!ifa)
Linus Torvalds's avatar
Linus Torvalds committed
617
				break;
618 619 620 621 622 623 624 625 626 627 628
			ret = 0;
			if (!(ifr.ifr_flags & IFF_UP))
				inet_del_ifa(in_dev, ifap, 1);
			break;
		}
		ret = dev_change_flags(dev, ifr.ifr_flags);
		break;

	case SIOCSIFADDR:	/* Set interface address (and family) */
		ret = -EINVAL;
		if (inet_abc_len(sin->sin_addr.s_addr) < 0)
Linus Torvalds's avatar
Linus Torvalds committed
629
			break;
630 631 632 633

		if (!ifa) {
			ret = -ENOBUFS;
			if ((ifa = inet_alloc_ifa()) == NULL)
Linus Torvalds's avatar
Linus Torvalds committed
634
				break;
635 636 637 638 639 640 641 642 643 644 645 646
			if (colon)
				memcpy(ifa->ifa_label, ifr.ifr_name, IFNAMSIZ);
			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;
			ifa->ifa_anycast = 0;
		}
Linus Torvalds's avatar
Linus Torvalds committed
647

648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
		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);
		}
		ret = inet_set_ifa(dev, ifa);
		break;
Linus Torvalds's avatar
Linus Torvalds committed
663

664 665 666 667 668 669 670 671
	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;
Linus Torvalds's avatar
Linus Torvalds committed
672

673 674 675
	case SIOCSIFDSTADDR:	/* Set the destination address */
		ret = 0;
		if (ifa->ifa_address == sin->sin_addr.s_addr)
Linus Torvalds's avatar
Linus Torvalds committed
676
			break;
677 678
		ret = -EINVAL;
		if (inet_abc_len(sin->sin_addr.s_addr) < 0)
Linus Torvalds's avatar
Linus Torvalds committed
679
			break;
680 681 682 683 684
		ret = 0;
		inet_del_ifa(in_dev, ifap, 0);
		ifa->ifa_address = sin->sin_addr.s_addr;
		inet_insert_ifa(ifa);
		break;
Linus Torvalds's avatar
Linus Torvalds committed
685

686
	case SIOCSIFNETMASK: 	/* Set the netmask for the interface */
Linus Torvalds's avatar
Linus Torvalds committed
687

688 689 690 691 692
		/*
		 *	The mask we set must be legal.
		 */
		ret = -EINVAL;
		if (bad_mask(sin->sin_addr.s_addr, 0))
Linus Torvalds's avatar
Linus Torvalds committed
693
			break;
694 695 696 697 698 699 700 701
		ret = 0;
		if (ifa->ifa_mask != sin->sin_addr.s_addr) {
			inet_del_ifa(in_dev, ifap, 0);
			ifa->ifa_mask = sin->sin_addr.s_addr;
			ifa->ifa_prefixlen = inet_mask_len(ifa->ifa_mask);
			inet_insert_ifa(ifa);
		}
		break;
Linus Torvalds's avatar
Linus Torvalds committed
702 703 704 705
	}
done:
	rtnl_unlock();
	dev_probe_unlock();
706
out:
Linus Torvalds's avatar
Linus Torvalds committed
707 708 709 710
	return ret;
rarok:
	rtnl_unlock();
	dev_probe_unlock();
711 712
	ret = copy_to_user(arg, &ifr, sizeof(struct ifreq)) ? -EFAULT : 0;
	goto out;
Linus Torvalds's avatar
Linus Torvalds committed
713 714
}

715
static int inet_gifconf(struct net_device *dev, char *buf, int len)
Linus Torvalds's avatar
Linus Torvalds committed
716 717 718 719
{
	struct in_device *in_dev = __in_dev_get(dev);
	struct in_ifaddr *ifa;
	struct ifreq ifr;
720
	int done = 0;
Linus Torvalds's avatar
Linus Torvalds committed
721

722 723
	if (!in_dev || (ifa = in_dev->ifa_list) == NULL)
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
724

725
	for (; ifa; ifa = ifa->ifa_next) {
Linus Torvalds's avatar
Linus Torvalds committed
726 727 728 729 730
		if (!buf) {
			done += sizeof(ifr);
			continue;
		}
		if (len < (int) sizeof(ifr))
731
			break;
Linus Torvalds's avatar
Linus Torvalds committed
732 733 734 735 736 737
		memset(&ifr, 0, sizeof(struct ifreq));
		if (ifa->ifa_label)
			strcpy(ifr.ifr_name, ifa->ifa_label);
		else
			strcpy(ifr.ifr_name, dev->name);

738 739 740
		(*(struct sockaddr_in *)&ifr.ifr_addr).sin_family = AF_INET;
		(*(struct sockaddr_in *)&ifr.ifr_addr).sin_addr.s_addr =
								ifa->ifa_local;
Linus Torvalds's avatar
Linus Torvalds committed
741

742 743 744 745 746 747
		if (copy_to_user(buf, &ifr, sizeof(struct ifreq))) {
			done = -EFAULT;
			break;
		}
		buf  += sizeof(struct ifreq);
		len  -= sizeof(struct ifreq);
Linus Torvalds's avatar
Linus Torvalds committed
748 749
		done += sizeof(struct ifreq);
	}
750
out:
Linus Torvalds's avatar
Linus Torvalds committed
751 752 753 754 755 756 757 758 759 760
	return done;
}

u32 inet_select_addr(const struct net_device *dev, u32 dst, int scope)
{
	u32 addr = 0;
	struct in_device *in_dev;

	read_lock(&inetdev_lock);
	in_dev = __in_dev_get(dev);
761 762
	if (!in_dev)
		goto out_unlock_inetdev;
Linus Torvalds's avatar
Linus Torvalds committed
763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778

	read_lock(&in_dev->lock);
	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);
	read_unlock(&in_dev->lock);
	read_unlock(&inetdev_lock);

	if (addr)
779
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
780 781 782 783 784 785 786

	/* Not loopback addresses on loopback should be preferred
	   in this case. It is importnat that lo is the first interface
	   in dev_base list.
	 */
	read_lock(&dev_base_lock);
	read_lock(&inetdev_lock);
787 788
	for (dev = dev_base; dev; dev = dev->next) {
		if ((in_dev = __in_dev_get(dev)) == NULL)
Linus Torvalds's avatar
Linus Torvalds committed
789 790 791 792 793 794 795
			continue;

		read_lock(&in_dev->lock);
		for_primary_ifa(in_dev) {
			if (ifa->ifa_scope != RT_SCOPE_LINK &&
			    ifa->ifa_scope <= scope) {
				read_unlock(&in_dev->lock);
796 797
				addr = ifa->ifa_local;
				goto out_unlock_both;
Linus Torvalds's avatar
Linus Torvalds committed
798 799 800 801
			}
		} endfor_ifa(in_dev);
		read_unlock(&in_dev->lock);
	}
802
out_unlock_both:
Linus Torvalds's avatar
Linus Torvalds committed
803 804
	read_unlock(&inetdev_lock);
	read_unlock(&dev_base_lock);
805 806 807 808 809
out:
	return addr;
out_unlock_inetdev:
	read_unlock(&inetdev_lock);
	goto out;
Linus Torvalds's avatar
Linus Torvalds committed
810 811 812 813 814 815 816 817 818 819 820 821 822
}

/*
 *	Device notifier
 */

int register_inetaddr_notifier(struct notifier_block *nb)
{
	return notifier_chain_register(&inetaddr_chain, nb);
}

int unregister_inetaddr_notifier(struct notifier_block *nb)
{
823
	return notifier_chain_unregister(&inetaddr_chain, nb);
Linus Torvalds's avatar
Linus Torvalds committed
824 825
}

826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
/* Rename ifa_labels for a device name change. Make some effort to preserve existing
 * alias numbering and to create unique labels if possible.
*/
static void inetdev_changename(struct net_device *dev, struct in_device *in_dev)
{ 
	struct in_ifaddr *ifa;
	int named = 0;

	for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) { 
		char old[IFNAMSIZ], *dot; 

		memcpy(old, ifa->ifa_label, IFNAMSIZ);
		memcpy(ifa->ifa_label, dev->name, IFNAMSIZ); 
		if (named++ == 0)
			continue;
		dot = strchr(ifa->ifa_label, ':');
		if (dot == NULL) { 
			sprintf(old, ":%d", named); 
			dot = old;
		}
		if (strlen(dot) + strlen(dev->name) < IFNAMSIZ) { 
			strcat(ifa->ifa_label, dot); 
		} else { 
			strcpy(ifa->ifa_label + (IFNAMSIZ - strlen(dot) - 1), dot); 
		} 
	}	
} 

Linus Torvalds's avatar
Linus Torvalds committed
854 855
/* Called only under RTNL semaphore */

856 857
static int inetdev_event(struct notifier_block *this, unsigned long event,
			 void *ptr)
Linus Torvalds's avatar
Linus Torvalds committed
858 859 860 861 862 863
{
	struct net_device *dev = ptr;
	struct in_device *in_dev = __in_dev_get(dev);

	ASSERT_RTNL();

864 865
	if (!in_dev)
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
866 867 868 869 870 871 872 873 874 875 876 877 878

	switch (event) {
	case NETDEV_REGISTER:
		printk(KERN_DEBUG "inetdev_event: bug\n");
		dev->ip_ptr = NULL;
		break;
	case NETDEV_UP:
		if (dev->mtu < 68)
			break;
		if (dev == &loopback_dev) {
			struct in_ifaddr *ifa;
			if ((ifa = inet_alloc_ifa()) != NULL) {
				ifa->ifa_local =
879
				  ifa->ifa_address = htonl(INADDR_LOOPBACK);
Linus Torvalds's avatar
Linus Torvalds committed
880 881 882 883 884 885 886 887
				ifa->ifa_prefixlen = 8;
				ifa->ifa_mask = inet_make_mask(8);
				in_dev_hold(in_dev);
				ifa->ifa_dev = in_dev;
				ifa->ifa_scope = RT_SCOPE_HOST;
				memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
				inet_insert_ifa(ifa);
			}
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
888 889
			in_dev->cnf.no_xfrm = 1;
			in_dev->cnf.no_policy = 1;
Linus Torvalds's avatar
Linus Torvalds committed
890 891 892 893 894 895 896 897 898 899 900 901 902 903
		}
		ip_mc_up(in_dev);
		break;
	case NETDEV_DOWN:
		ip_mc_down(in_dev);
		break;
	case NETDEV_CHANGEMTU:
		if (dev->mtu >= 68)
			break;
		/* MTU falled under 68, disable IP */
	case NETDEV_UNREGISTER:
		inetdev_destroy(in_dev);
		break;
	case NETDEV_CHANGENAME:
904 905 906 907
		/* Do not notify about label change, this event is
		 * not interesting to applications using netlink.
		 */
		inetdev_changename(dev, in_dev);
Linus Torvalds's avatar
Linus Torvalds committed
908 909
		break;
	}
910
out:
Linus Torvalds's avatar
Linus Torvalds committed
911 912 913
	return NOTIFY_DONE;
}

Linus Torvalds's avatar
Linus Torvalds committed
914
struct notifier_block ip_netdev_notifier = {
915
	.notifier_call =inetdev_event,
Linus Torvalds's avatar
Linus Torvalds committed
916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956
};

static int inet_fill_ifaddr(struct sk_buff *skb, struct in_ifaddr *ifa,
			    u32 pid, u32 seq, int event)
{
	struct ifaddrmsg *ifm;
	struct nlmsghdr  *nlh;
	unsigned char	 *b = skb->tail;

	nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*ifm));
	ifm = NLMSG_DATA(nlh);
	ifm->ifa_family = AF_INET;
	ifm->ifa_prefixlen = ifa->ifa_prefixlen;
	ifm->ifa_flags = ifa->ifa_flags|IFA_F_PERMANENT;
	ifm->ifa_scope = ifa->ifa_scope;
	ifm->ifa_index = ifa->ifa_dev->dev->ifindex;
	if (ifa->ifa_address)
		RTA_PUT(skb, IFA_ADDRESS, 4, &ifa->ifa_address);
	if (ifa->ifa_local)
		RTA_PUT(skb, IFA_LOCAL, 4, &ifa->ifa_local);
	if (ifa->ifa_broadcast)
		RTA_PUT(skb, IFA_BROADCAST, 4, &ifa->ifa_broadcast);
	if (ifa->ifa_anycast)
		RTA_PUT(skb, IFA_ANYCAST, 4, &ifa->ifa_anycast);
	if (ifa->ifa_label[0])
		RTA_PUT(skb, IFA_LABEL, IFNAMSIZ, &ifa->ifa_label);
	nlh->nlmsg_len = skb->tail - b;
	return skb->len;

nlmsg_failure:
rtattr_failure:
	skb_trim(skb, b - skb->data);
	return -1;
}

static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
{
	int idx, ip_idx;
	struct net_device *dev;
	struct in_device *in_dev;
	struct in_ifaddr *ifa;
957
	int s_ip_idx, s_idx = cb->args[0];
Linus Torvalds's avatar
Linus Torvalds committed
958 959 960

	s_ip_idx = ip_idx = cb->args[1];
	read_lock(&dev_base_lock);
961
	for (dev = dev_base, idx = 0; dev; dev = dev->next, idx++) {
Linus Torvalds's avatar
Linus Torvalds committed
962 963 964 965 966 967 968 969 970 971 972 973 974 975 976
		if (idx < s_idx)
			continue;
		if (idx > s_idx)
			s_ip_idx = 0;
		read_lock(&inetdev_lock);
		if ((in_dev = __in_dev_get(dev)) == NULL) {
			read_unlock(&inetdev_lock);
			continue;
		}
		read_lock(&in_dev->lock);
		for (ifa = in_dev->ifa_list, ip_idx = 0; ifa;
		     ifa = ifa->ifa_next, ip_idx++) {
			if (ip_idx < s_ip_idx)
				continue;
			if (inet_fill_ifaddr(skb, ifa, NETLINK_CB(cb->skb).pid,
977 978
					     cb->nlh->nlmsg_seq,
					     RTM_NEWADDR) <= 0) {
Linus Torvalds's avatar
Linus Torvalds committed
979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995
				read_unlock(&in_dev->lock);
				read_unlock(&inetdev_lock);
				goto done;
			}
		}
		read_unlock(&in_dev->lock);
		read_unlock(&inetdev_lock);
	}

done:
	read_unlock(&dev_base_lock);
	cb->args[0] = idx;
	cb->args[1] = ip_idx;

	return skb->len;
}

996
static void rtmsg_ifa(int event, struct in_ifaddr* ifa)
Linus Torvalds's avatar
Linus Torvalds committed
997
{
998 999
	int size = NLMSG_SPACE(sizeof(struct ifaddrmsg) + 128);
	struct sk_buff *skb = alloc_skb(size, GFP_KERNEL);
Linus Torvalds's avatar
Linus Torvalds committed
1000

1001
	if (!skb)
Linus Torvalds's avatar
Linus Torvalds committed
1002
		netlink_set_err(rtnl, 0, RTMGRP_IPV4_IFADDR, ENOBUFS);
1003
	else if (inet_fill_ifaddr(skb, ifa, 0, 0, event) < 0) {
Linus Torvalds's avatar
Linus Torvalds committed
1004 1005
		kfree_skb(skb);
		netlink_set_err(rtnl, 0, RTMGRP_IPV4_IFADDR, EINVAL);
1006 1007 1008
	} else {
		NETLINK_CB(skb).dst_groups = RTMGRP_IPV4_IFADDR;
		netlink_broadcast(rtnl, skb, 0, RTMGRP_IPV4_IFADDR, GFP_KERNEL);
Linus Torvalds's avatar
Linus Torvalds committed
1009 1010 1011
	}
}

1012
static struct rtnetlink_link inet_rtnetlink_table[RTM_MAX - RTM_BASE + 1] = {
1013 1014 1015 1016 1017 1018
	 [4] = { .doit	 = inet_rtm_newaddr,  },
	 [5] = { .doit	 = inet_rtm_deladdr,  },
	 [6] = { .dumpit = inet_dump_ifaddr,  },
	 [8] = { .doit	 = inet_rtm_newroute, },
	 [9] = { .doit	 = inet_rtm_delroute, },
	[10] = { .doit	 = inet_rtm_getroute, .dumpit = inet_dump_fib, },
Linus Torvalds's avatar
Linus Torvalds committed
1019
#ifdef CONFIG_IP_MULTIPLE_TABLES
1020 1021 1022
	[16] = { .doit	 = inet_rtm_newrule, },
	[17] = { .doit	 = inet_rtm_delrule, },
	[18] = { .dumpit = inet_dump_rules,  },
Linus Torvalds's avatar
Linus Torvalds committed
1023 1024 1025 1026 1027
#endif
};

#ifdef CONFIG_SYSCTL

1028
void inet_forward_change(void)
Linus Torvalds's avatar
Linus Torvalds committed
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
{
	struct net_device *dev;
	int on = ipv4_devconf.forwarding;

	ipv4_devconf.accept_redirects = !on;
	ipv4_devconf_dflt.forwarding = on;

	read_lock(&dev_base_lock);
	for (dev = dev_base; dev; dev = dev->next) {
		struct in_device *in_dev;
		read_lock(&inetdev_lock);
		in_dev = __in_dev_get(dev);
		if (in_dev)
			in_dev->cnf.forwarding = on;
		read_unlock(&inetdev_lock);
	}
	read_unlock(&dev_base_lock);

	rt_cache_flush(0);
}

1050 1051 1052
static int devinet_sysctl_forward(ctl_table *ctl, int write,
				  struct file* filp, void *buffer,
				  size_t *lenp)
Linus Torvalds's avatar
Linus Torvalds committed
1053 1054 1055
{
	int *valp = ctl->data;
	int val = *valp;
1056
	int ret = proc_dointvec(ctl, write, filp, buffer, lenp);
Linus Torvalds's avatar
Linus Torvalds committed
1057 1058 1059 1060 1061 1062 1063 1064

	if (write && *valp != val) {
		if (valp == &ipv4_devconf.forwarding)
			inet_forward_change();
		else if (valp != &ipv4_devconf_dflt.forwarding)
			rt_cache_flush(0);
	}

1065
	return ret;
Linus Torvalds's avatar
Linus Torvalds committed
1066 1067
}

1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123
static int ipv4_doint_and_flush(ctl_table *ctl, int write,
				struct file* filp, void *buffer,
				size_t *lenp)
{
	int *valp = ctl->data;
	int val = *valp;
	int ret = proc_dointvec(ctl, write, filp, buffer, lenp);

	if (write && *valp != val)
		rt_cache_flush(0);

	return ret;
}

static int ipv4_doint_and_flush_strategy(ctl_table *table, int *name, int nlen,
					 void *oldval, size_t *oldlenp,
					 void *newval, size_t newlen, 
					 void **context)
{
	int *valp = table->data;
	int new;

	if (!newval || !newlen)
		return 0;

	if (newlen != sizeof(int))
		return -EINVAL;

	if (get_user(new, (int *)newval))
		return -EFAULT;

	if (new == *valp)
		return 0;

	if (oldval && oldlenp) {
		size_t len;

		if (get_user(len, oldlenp))
			return -EFAULT;

		if (len) {
			if (len > table->maxlen)
				len = table->maxlen;
			if (copy_to_user(oldval, valp, len))
				return -EFAULT;
			if (put_user(len, oldlenp))
				return -EFAULT;
		}
	}

	*valp = new;
	rt_cache_flush(0);
	return 1;
}


1124
static struct devinet_sysctl_table {
Linus Torvalds's avatar
Linus Torvalds committed
1125
	struct ctl_table_header *sysctl_header;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1126
	ctl_table		devinet_vars[17];
1127 1128 1129 1130
	ctl_table		devinet_dev[2];
	ctl_table		devinet_conf_dir[2];
	ctl_table		devinet_proto_dir[2];
	ctl_table		devinet_root_dir[2];
Linus Torvalds's avatar
Linus Torvalds committed
1131
} devinet_sysctl = {
1132
	.devinet_vars = {
1133
		{
1134 1135 1136 1137 1138 1139
			.ctl_name =	NET_IPV4_CONF_FORWARDING,
			.procname =	"forwarding",
			.data =	&ipv4_devconf.forwarding,
			.maxlen =		sizeof(int),
			.mode =	0644,
			.proc_handler =&devinet_sysctl_forward,
1140 1141
		},
		{
1142 1143 1144 1145 1146 1147
			.ctl_name =	NET_IPV4_CONF_MC_FORWARDING,
			.procname =	"mc_forwarding",
			.data =	&ipv4_devconf.mc_forwarding,
			.maxlen =		sizeof(int),
			.mode =	0444,
			.proc_handler =&proc_dointvec,
1148 1149
		},
		{
1150 1151 1152 1153 1154 1155
			.ctl_name =	NET_IPV4_CONF_ACCEPT_REDIRECTS,
			.procname =	"accept_redirects",
			.data =	&ipv4_devconf.accept_redirects,
			.maxlen =		sizeof(int),
			.mode =	0644,
			.proc_handler =&proc_dointvec,
1156 1157
		},
		{
1158 1159 1160 1161 1162 1163
			.ctl_name =	NET_IPV4_CONF_SECURE_REDIRECTS,
			.procname =	"secure_redirects",
			.data =	&ipv4_devconf.secure_redirects,
			.maxlen =		sizeof(int),
			.mode =	0644,
			.proc_handler =&proc_dointvec,
1164 1165
		},
		{
1166 1167 1168 1169 1170 1171
			.ctl_name =	NET_IPV4_CONF_SHARED_MEDIA,
			.procname =	"shared_media",
			.data =	&ipv4_devconf.shared_media,
			.maxlen =		sizeof(int),
			.mode =	0644,
			.proc_handler =&proc_dointvec,
1172 1173
		},
		{
1174 1175 1176 1177 1178 1179
			.ctl_name =	NET_IPV4_CONF_RP_FILTER,
			.procname =	"rp_filter",
			.data =	&ipv4_devconf.rp_filter,
			.maxlen =		sizeof(int),
			.mode =	0644,
			.proc_handler =&proc_dointvec,
1180 1181
		},
		{
1182 1183 1184 1185 1186 1187
			.ctl_name =	NET_IPV4_CONF_SEND_REDIRECTS,
			.procname =	"send_redirects",
			.data =	&ipv4_devconf.send_redirects,
			.maxlen =		sizeof(int),
			.mode =	0644,
			.proc_handler =&proc_dointvec,
1188 1189
		},
		{
1190 1191 1192 1193 1194 1195
			.ctl_name =	NET_IPV4_CONF_ACCEPT_SOURCE_ROUTE,
			.procname =	"accept_source_route",
			.data =	&ipv4_devconf.accept_source_route,
			.maxlen =		sizeof(int),
			.mode =	0644,
			.proc_handler =&proc_dointvec,
1196 1197
		},
		{
1198 1199 1200 1201 1202 1203
			.ctl_name =	NET_IPV4_CONF_PROXY_ARP,
			.procname =	"proxy_arp",
			.data =	&ipv4_devconf.proxy_arp,
			.maxlen =		sizeof(int),
			.mode =	0644,
			.proc_handler =&proc_dointvec,
1204 1205
		},
		{
1206 1207 1208 1209 1210 1211
			.ctl_name =	NET_IPV4_CONF_MEDIUM_ID,
			.procname =	"medium_id",
			.data =	&ipv4_devconf.medium_id,
			.maxlen =		sizeof(int),
			.mode =	0644,
			.proc_handler =&proc_dointvec,
1212 1213
		},
		{
1214 1215 1216 1217 1218 1219
			.ctl_name =	NET_IPV4_CONF_BOOTP_RELAY,
			.procname =	"bootp_relay",
			.data =	&ipv4_devconf.bootp_relay,
			.maxlen =		sizeof(int),
			.mode =	0644,
			.proc_handler =&proc_dointvec,
1220 1221
		},
		{
1222 1223 1224 1225 1226 1227
			.ctl_name =	NET_IPV4_CONF_LOG_MARTIANS,
			.procname =	"log_martians",
			.data =	&ipv4_devconf.log_martians,
			.maxlen =		sizeof(int),
			.mode =	0644,
			.proc_handler =&proc_dointvec,
1228 1229
		},
		{
1230 1231 1232 1233 1234 1235
			.ctl_name =	NET_IPV4_CONF_TAG,
			.procname =	"tag",
			.data =	&ipv4_devconf.tag,
			.maxlen =		sizeof(int),
			.mode =	0644,
			.proc_handler =&proc_dointvec,
1236 1237
		},
		{
1238 1239 1240 1241 1242 1243
			.ctl_name =	NET_IPV4_CONF_ARPFILTER,
			.procname =	"arp_filter",
			.data =	&ipv4_devconf.arp_filter,
			.maxlen =		sizeof(int),
			.mode =	0644,
			.proc_handler =&proc_dointvec,
1244
		},
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1245 1246 1247 1248 1249 1250
		{
			.ctl_name =	NET_IPV4_CONF_NOXFRM,
			.procname =	"disable_xfrm",
			.data =	&ipv4_devconf.no_xfrm,
			.maxlen =		sizeof(int),
			.mode =	0644,
1251 1252
			.proc_handler = &ipv4_doint_and_flush,
			.strategy = &ipv4_doint_and_flush_strategy,
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1253 1254 1255 1256 1257 1258 1259
		},
		{
			.ctl_name =	NET_IPV4_CONF_NOPOLICY,
			.procname =	"disable_policy",
			.data =	&ipv4_devconf.no_policy,
			.maxlen =		sizeof(int),
			.mode =	0644,
1260 1261
			.proc_handler = &ipv4_doint_and_flush,
			.strategy = &ipv4_doint_and_flush_strategy,
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1262
		},
1263
	},
1264
	.devinet_dev = {
1265
		{
1266 1267 1268 1269
			.ctl_name =	NET_PROTO_CONF_ALL,
			.procname =	"all",
			.mode =	0555,
			.child =		devinet_sysctl.devinet_vars,
1270 1271
		},
	},
1272
	.devinet_conf_dir = {
1273
	        {
1274 1275 1276 1277
			.ctl_name =	NET_IPV4_CONF,
			.procname =	"conf",
			.mode =	0555,
			.child =		devinet_sysctl.devinet_dev,
1278 1279
		},
	},
1280
	.devinet_proto_dir = {
1281
		{
1282 1283 1284 1285
			.ctl_name =	NET_IPV4,
			.procname =	"ipv4",
			.mode =	0555,
			.child =		devinet_sysctl.devinet_conf_dir,
1286 1287
		},
	},
1288
	.devinet_root_dir = {
1289
		{
1290 1291 1292 1293
			.ctl_name =	CTL_NET,
			.procname =	"net",
			.mode =	0555,
			.child =		devinet_sysctl.devinet_proto_dir,
1294 1295
		},
	},
Linus Torvalds's avatar
Linus Torvalds committed
1296 1297
};

1298 1299
static void devinet_sysctl_register(struct in_device *in_dev,
				    struct ipv4_devconf *p)
Linus Torvalds's avatar
Linus Torvalds committed
1300 1301 1302
{
	int i;
	struct net_device *dev = in_dev ? in_dev->dev : NULL;
1303
	struct devinet_sysctl_table *t = kmalloc(sizeof(*t), GFP_KERNEL);
Linus Torvalds's avatar
Linus Torvalds committed
1304

1305
	if (!t)
Linus Torvalds's avatar
Linus Torvalds committed
1306 1307
		return;
	memcpy(t, &devinet_sysctl, sizeof(*t));
1308 1309 1310 1311
	for (i = 0;
	     i < sizeof(t->devinet_vars) / sizeof(t->devinet_vars[0]) - 1;
	     i++) {
		t->devinet_vars[i].data += (char *)p - (char *)&ipv4_devconf;
Linus Torvalds's avatar
Linus Torvalds committed
1312 1313 1314 1315 1316 1317 1318 1319 1320
		t->devinet_vars[i].de = NULL;
	}
	if (dev) {
		t->devinet_dev[0].procname = dev->name;
		t->devinet_dev[0].ctl_name = dev->ifindex;
	} else {
		t->devinet_dev[0].procname = "default";
		t->devinet_dev[0].ctl_name = NET_PROTO_CONF_DEFAULT;
	}
1321 1322 1323 1324
	t->devinet_dev[0].child	      = t->devinet_vars;
	t->devinet_dev[0].de	      = NULL;
	t->devinet_conf_dir[0].child  = t->devinet_dev;
	t->devinet_conf_dir[0].de     = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
1325
	t->devinet_proto_dir[0].child = t->devinet_conf_dir;
1326 1327 1328
	t->devinet_proto_dir[0].de    = NULL;
	t->devinet_root_dir[0].child  = t->devinet_proto_dir;
	t->devinet_root_dir[0].de     = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
1329 1330

	t->sysctl_header = register_sysctl_table(t->devinet_root_dir, 0);
1331
	if (!t->sysctl_header)
Linus Torvalds's avatar
Linus Torvalds committed
1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
		kfree(t);
	else
		p->sysctl = t;
}

static void devinet_sysctl_unregister(struct ipv4_devconf *p)
{
	if (p->sysctl) {
		struct devinet_sysctl_table *t = p->sysctl;
		p->sysctl = NULL;
		unregister_sysctl_table(t->sysctl_header);
		kfree(t);
	}
}
#endif

void __init devinet_init(void)
{
	register_gifconf(PF_INET, inet_gifconf);
	register_netdevice_notifier(&ip_netdev_notifier);
	rtnetlink_links[PF_INET] = inet_rtnetlink_table;
#ifdef CONFIG_SYSCTL
	devinet_sysctl.sysctl_header =
		register_sysctl_table(devinet_sysctl.devinet_root_dir, 0);
	devinet_sysctl_register(NULL, &ipv4_devconf_dflt);
#endif
}