dn_dev.c 33.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * DECnet       An implementation of the DECnet protocol suite for the LINUX
 *              operating system.  DECnet is implemented using the  BSD Socket
 *              interface as the means of communication with the user level.
 *
 *              DECnet Device Layer
 *
 * Authors:     Steve Whitehouse <SteveW@ACM.org>
 *              Eduardo Marcelo Serrat <emserrat@geocities.com>
 *
 * Changes:
 *          Steve Whitehouse : Devices now see incoming frames so they
 *                             can mark on who it came from.
 *          Steve Whitehouse : Fixed bug in creating neighbours. Each neighbour
 *                             can now have a device specific setup func.
 *          Steve Whitehouse : Added /proc/sys/net/decnet/conf/<dev>/
 *          Steve Whitehouse : Fixed bug which sometimes killed timer
 *          Steve Whitehouse : Multiple ifaddr support
 *          Steve Whitehouse : SIOCGIFCONF is now a compile time option
 *          Steve Whitehouse : /proc/sys/net/decnet/conf/<sys>/forwarding
 *          Steve Whitehouse : Removed timer1 - it's a user space issue now
 *         Patrick Caulfield : Fixed router hello message format
 *          Steve Whitehouse : Got rid of constant sizes for blksize for
 *                             devices. All mtu based now.
 */

27
#include <linux/capability.h>
L
Linus Torvalds 已提交
28 29 30 31 32 33 34 35 36
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/net.h>
#include <linux/netdevice.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/timer.h>
#include <linux/string.h>
37
#include <linux/if_addr.h>
L
Linus Torvalds 已提交
38 39 40 41 42 43 44
#include <linux/if_arp.h>
#include <linux/if_ether.h>
#include <linux/skbuff.h>
#include <linux/sysctl.h>
#include <linux/notifier.h>
#include <asm/uaccess.h>
#include <asm/system.h>
45
#include <net/net_namespace.h>
L
Linus Torvalds 已提交
46 47 48
#include <net/neighbour.h>
#include <net/dst.h>
#include <net/flow.h>
49
#include <net/fib_rules.h>
50
#include <net/netlink.h>
L
Linus Torvalds 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
#include <net/dn.h>
#include <net/dn_dev.h>
#include <net/dn_route.h>
#include <net/dn_neigh.h>
#include <net/dn_fib.h>

#define DN_IFREQ_SIZE (sizeof(struct ifreq) - sizeof(struct sockaddr) + sizeof(struct sockaddr_dn))

static char dn_rt_all_end_mcast[ETH_ALEN] = {0xAB,0x00,0x00,0x04,0x00,0x00};
static char dn_rt_all_rt_mcast[ETH_ALEN]  = {0xAB,0x00,0x00,0x03,0x00,0x00};
static char dn_hiord[ETH_ALEN]            = {0xAA,0x00,0x04,0x00,0x00,0x00};
static unsigned char dn_eco_version[3]    = {0x02,0x00,0x00};

extern struct neigh_table dn_neigh_table;

/*
 * decnet_address is kept in network order.
 */
69
__le16 decnet_address = 0;
L
Linus Torvalds 已提交
70 71 72

static DEFINE_RWLOCK(dndev_lock);
static struct net_device *decnet_default_device;
73
static BLOCKING_NOTIFIER_HEAD(dnaddr_chain);
L
Linus Torvalds 已提交
74 75 76

static struct dn_dev *dn_dev_create(struct net_device *dev, int *err);
static void dn_dev_delete(struct net_device *dev);
77
static void dn_ifaddr_notify(int event, struct dn_ifaddr *ifa);
L
Linus Torvalds 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152

static int dn_eth_up(struct net_device *);
static void dn_eth_down(struct net_device *);
static void dn_send_brd_hello(struct net_device *dev, struct dn_ifaddr *ifa);
static void dn_send_ptp_hello(struct net_device *dev, struct dn_ifaddr *ifa);

static struct dn_dev_parms dn_dev_list[] =  {
{
	.type =		ARPHRD_ETHER, /* Ethernet */
	.mode =		DN_DEV_BCAST,
	.state =	DN_DEV_S_RU,
	.t2 =		1,
	.t3 =		10,
	.name =		"ethernet",
	.ctl_name =	NET_DECNET_CONF_ETHER,
	.up =		dn_eth_up,
	.down = 	dn_eth_down,
	.timer3 =	dn_send_brd_hello,
},
{
	.type =		ARPHRD_IPGRE, /* DECnet tunneled over GRE in IP */
	.mode =		DN_DEV_BCAST,
	.state =	DN_DEV_S_RU,
	.t2 =		1,
	.t3 =		10,
	.name =		"ipgre",
	.ctl_name =	NET_DECNET_CONF_GRE,
	.timer3 =	dn_send_brd_hello,
},
#if 0
{
	.type =		ARPHRD_X25, /* Bog standard X.25 */
	.mode =		DN_DEV_UCAST,
	.state =	DN_DEV_S_DS,
	.t2 =		1,
	.t3 =		120,
	.name =		"x25",
	.ctl_name =	NET_DECNET_CONF_X25,
	.timer3 =	dn_send_ptp_hello,
},
#endif
#if 0
{
	.type =		ARPHRD_PPP, /* DECnet over PPP */
	.mode =		DN_DEV_BCAST,
	.state =	DN_DEV_S_RU,
	.t2 =		1,
	.t3 =		10,
	.name =		"ppp",
	.ctl_name =	NET_DECNET_CONF_PPP,
	.timer3 =	dn_send_brd_hello,
},
#endif
{
	.type =		ARPHRD_DDCMP, /* DECnet over DDCMP */
	.mode =		DN_DEV_UCAST,
	.state =	DN_DEV_S_DS,
	.t2 =		1,
	.t3 =		120,
	.name =		"ddcmp",
	.ctl_name =	NET_DECNET_CONF_DDCMP,
	.timer3 =	dn_send_ptp_hello,
},
{
	.type =		ARPHRD_LOOPBACK, /* Loopback interface - always last */
	.mode =		DN_DEV_BCAST,
	.state =	DN_DEV_S_RU,
	.t2 =		1,
	.t3 =		10,
	.name =		"loopback",
	.ctl_name =	NET_DECNET_CONF_LOOPBACK,
	.timer3 =	dn_send_brd_hello,
}
};

153
#define DN_DEV_LIST_SIZE ARRAY_SIZE(dn_dev_list)
L
Linus Torvalds 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170

#define DN_DEV_PARMS_OFFSET(x) ((int) ((char *) &((struct dn_dev_parms *)0)->x))

#ifdef CONFIG_SYSCTL

static int min_t2[] = { 1 };
static int max_t2[] = { 60 }; /* No max specified, but this seems sensible */
static int min_t3[] = { 1 };
static int max_t3[] = { 8191 }; /* Must fit in 16 bits when multiplied by BCT3MULT or T3MULT */

static int min_priority[1];
static int max_priority[] = { 127 }; /* From DECnet spec */

static int dn_forwarding_proc(ctl_table *, int, struct file *,
			void __user *, size_t *, loff_t *);
static int dn_forwarding_sysctl(ctl_table *table, int __user *name, int nlen,
			void __user *oldval, size_t __user *oldlenp,
171
			void __user *newval, size_t newlen);
L
Linus Torvalds 已提交
172 173 174 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 218 219 220 221 222 223 224 225 226 227 228 229

static struct dn_dev_sysctl_table {
	struct ctl_table_header *sysctl_header;
	ctl_table dn_dev_vars[5];
} dn_dev_sysctl = {
	NULL,
	{
	{
		.ctl_name = NET_DECNET_CONF_DEV_FORWARDING,
		.procname = "forwarding",
		.data = (void *)DN_DEV_PARMS_OFFSET(forwarding),
		.maxlen = sizeof(int),
		.mode = 0644,
		.proc_handler = dn_forwarding_proc,
		.strategy = dn_forwarding_sysctl,
	},
	{
		.ctl_name = NET_DECNET_CONF_DEV_PRIORITY,
		.procname = "priority",
		.data = (void *)DN_DEV_PARMS_OFFSET(priority),
		.maxlen = sizeof(int),
		.mode = 0644,
		.proc_handler = proc_dointvec_minmax,
		.strategy = sysctl_intvec,
		.extra1 = &min_priority,
		.extra2 = &max_priority
	},
	{
		.ctl_name = NET_DECNET_CONF_DEV_T2,
		.procname = "t2",
		.data = (void *)DN_DEV_PARMS_OFFSET(t2),
		.maxlen = sizeof(int),
		.mode = 0644,
		.proc_handler = proc_dointvec_minmax,
		.strategy = sysctl_intvec,
		.extra1 = &min_t2,
		.extra2 = &max_t2
	},
	{
		.ctl_name = NET_DECNET_CONF_DEV_T3,
		.procname = "t3",
		.data = (void *)DN_DEV_PARMS_OFFSET(t3),
		.maxlen = sizeof(int),
		.mode = 0644,
		.proc_handler = proc_dointvec_minmax,
		.strategy = sysctl_intvec,
		.extra1 = &min_t3,
		.extra2 = &max_t3
	},
	{0}
	},
};

static void dn_dev_sysctl_register(struct net_device *dev, struct dn_dev_parms *parms)
{
	struct dn_dev_sysctl_table *t;
	int i;

230 231 232 233 234 235 236 237 238 239
#define DN_CTL_PATH_DEV	3

	struct ctl_path dn_ctl_path[] = {
		{ .procname = "net", .ctl_name = CTL_NET, },
		{ .procname = "decnet", .ctl_name = NET_DECNET, },
		{ .procname = "conf", .ctl_name = NET_DECNET_CONF, },
		{ /* to be set */ },
		{ },
	};

A
Arnaldo Carvalho de Melo 已提交
240
	t = kmemdup(&dn_dev_sysctl, sizeof(*t), GFP_KERNEL);
L
Linus Torvalds 已提交
241 242 243 244 245 246 247 248 249
	if (t == NULL)
		return;

	for(i = 0; i < ARRAY_SIZE(t->dn_dev_vars) - 1; i++) {
		long offset = (long)t->dn_dev_vars[i].data;
		t->dn_dev_vars[i].data = ((char *)parms) + offset;
	}

	if (dev) {
250 251
		dn_ctl_path[DN_CTL_PATH_DEV].procname = dev->name;
		dn_ctl_path[DN_CTL_PATH_DEV].ctl_name = dev->ifindex;
L
Linus Torvalds 已提交
252
	} else {
253 254
		dn_ctl_path[DN_CTL_PATH_DEV].procname = parms->name;
		dn_ctl_path[DN_CTL_PATH_DEV].ctl_name = parms->ctl_name;
L
Linus Torvalds 已提交
255 256 257 258
	}

	t->dn_dev_vars[0].extra1 = (void *)dev;

259
	t->sysctl_header = register_sysctl_paths(dn_ctl_path, t->dn_dev_vars);
L
Linus Torvalds 已提交
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
	if (t->sysctl_header == NULL)
		kfree(t);
	else
		parms->sysctl = t;
}

static void dn_dev_sysctl_unregister(struct dn_dev_parms *parms)
{
	if (parms->sysctl) {
		struct dn_dev_sysctl_table *t = parms->sysctl;
		parms->sysctl = NULL;
		unregister_sysctl_table(t->sysctl_header);
		kfree(t);
	}
}

276
static int dn_forwarding_proc(ctl_table *table, int write,
L
Linus Torvalds 已提交
277 278 279 280 281 282 283 284 285 286 287 288 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 319 320 321 322
				struct file *filep,
				void __user *buffer,
				size_t *lenp, loff_t *ppos)
{
#ifdef CONFIG_DECNET_ROUTER
	struct net_device *dev = table->extra1;
	struct dn_dev *dn_db;
	int err;
	int tmp, old;

	if (table->extra1 == NULL)
		return -EINVAL;

	dn_db = dev->dn_ptr;
	old = dn_db->parms.forwarding;

	err = proc_dointvec(table, write, filep, buffer, lenp, ppos);

	if ((err >= 0) && write) {
		if (dn_db->parms.forwarding < 0)
			dn_db->parms.forwarding = 0;
		if (dn_db->parms.forwarding > 2)
			dn_db->parms.forwarding = 2;
		/*
		 * What an ugly hack this is... its works, just. It
		 * would be nice if sysctl/proc were just that little
		 * bit more flexible so I don't have to write a special
		 * routine, or suffer hacks like this - SJW
		 */
		tmp = dn_db->parms.forwarding;
		dn_db->parms.forwarding = old;
		if (dn_db->parms.down)
			dn_db->parms.down(dev);
		dn_db->parms.forwarding = tmp;
		if (dn_db->parms.up)
			dn_db->parms.up(dev);
	}

	return err;
#else
	return -EINVAL;
#endif
}

static int dn_forwarding_sysctl(ctl_table *table, int __user *name, int nlen,
			void __user *oldval, size_t __user *oldlenp,
323
			void __user *newval, size_t newlen)
L
Linus Torvalds 已提交
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
{
#ifdef CONFIG_DECNET_ROUTER
	struct net_device *dev = table->extra1;
	struct dn_dev *dn_db;
	int value;

	if (table->extra1 == NULL)
		return -EINVAL;

	dn_db = dev->dn_ptr;

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

		if (get_user(value, (int __user *)newval))
			return -EFAULT;
		if (value < 0)
			return -EINVAL;
		if (value > 2)
			return -EINVAL;

		if (dn_db->parms.down)
			dn_db->parms.down(dev);
		dn_db->parms.forwarding = value;
		if (dn_db->parms.up)
			dn_db->parms.up(dev);
	}

	return 0;
#else
	return -EINVAL;
#endif
}

#else /* CONFIG_SYSCTL */
static void dn_dev_sysctl_unregister(struct dn_dev_parms *parms)
{
}
static void dn_dev_sysctl_register(struct net_device *dev, struct dn_dev_parms *parms)
{
}

#endif /* CONFIG_SYSCTL */

static inline __u16 mtu2blksize(struct net_device *dev)
{
	u32 blksize = dev->mtu;
	if (blksize > 0xffff)
		blksize = 0xffff;

	if (dev->type == ARPHRD_ETHER ||
	    dev->type == ARPHRD_PPP ||
	    dev->type == ARPHRD_IPGRE ||
	    dev->type == ARPHRD_LOOPBACK)
		blksize -= 2;

	return (__u16)blksize;
}

static struct dn_ifaddr *dn_dev_alloc_ifa(void)
{
	struct dn_ifaddr *ifa;

388
	ifa = kzalloc(sizeof(*ifa), GFP_KERNEL);
L
Linus Torvalds 已提交
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408

	return ifa;
}

static __inline__ void dn_dev_free_ifa(struct dn_ifaddr *ifa)
{
	kfree(ifa);
}

static void dn_dev_del_ifa(struct dn_dev *dn_db, struct dn_ifaddr **ifap, int destroy)
{
	struct dn_ifaddr *ifa1 = *ifap;
	unsigned char mac_addr[6];
	struct net_device *dev = dn_db->dev;

	ASSERT_RTNL();

	*ifap = ifa1->ifa_next;

	if (dn_db->dev->type == ARPHRD_ETHER) {
409
		if (ifa1->ifa_local != dn_eth2dn(dev->dev_addr)) {
L
Linus Torvalds 已提交
410 411 412 413 414
			dn_dn2eth(mac_addr, ifa1->ifa_local);
			dev_mc_delete(dev, mac_addr, ETH_ALEN, 0);
		}
	}

415
	dn_ifaddr_notify(RTM_DELADDR, ifa1);
416
	blocking_notifier_call_chain(&dnaddr_chain, NETDEV_DOWN, ifa1);
L
Linus Torvalds 已提交
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
	if (destroy) {
		dn_dev_free_ifa(ifa1);

		if (dn_db->ifa_list == NULL)
			dn_dev_delete(dn_db->dev);
	}
}

static int dn_dev_insert_ifa(struct dn_dev *dn_db, struct dn_ifaddr *ifa)
{
	struct net_device *dev = dn_db->dev;
	struct dn_ifaddr *ifa1;
	unsigned char mac_addr[6];

	ASSERT_RTNL();

433
	/* Check for duplicates */
L
Linus Torvalds 已提交
434 435 436 437 438 439
	for(ifa1 = dn_db->ifa_list; ifa1; ifa1 = ifa1->ifa_next) {
		if (ifa1->ifa_local == ifa->ifa_local)
			return -EEXIST;
	}

	if (dev->type == ARPHRD_ETHER) {
440
		if (ifa->ifa_local != dn_eth2dn(dev->dev_addr)) {
L
Linus Torvalds 已提交
441 442 443 444 445 446 447 448
			dn_dn2eth(mac_addr, ifa->ifa_local);
			dev_mc_add(dev, mac_addr, ETH_ALEN, 0);
		}
	}

	ifa->ifa_next = dn_db->ifa_list;
	dn_db->ifa_list = ifa;

449
	dn_ifaddr_notify(RTM_NEWADDR, ifa);
450
	blocking_notifier_call_chain(&dnaddr_chain, NETDEV_UP, ifa);
L
Linus Torvalds 已提交
451 452 453 454 455 456 457 458 459 460 461 462 463 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

	return 0;
}

static int dn_dev_set_ifa(struct net_device *dev, struct dn_ifaddr *ifa)
{
	struct dn_dev *dn_db = dev->dn_ptr;
	int rv;

	if (dn_db == NULL) {
		int err;
		dn_db = dn_dev_create(dev, &err);
		if (dn_db == NULL)
			return err;
	}

	ifa->ifa_dev = dn_db;

	if (dev->flags & IFF_LOOPBACK)
		ifa->ifa_scope = RT_SCOPE_HOST;

	rv = dn_dev_insert_ifa(dn_db, ifa);
	if (rv)
		dn_dev_free_ifa(ifa);
	return rv;
}


int dn_dev_ioctl(unsigned int cmd, void __user *arg)
{
	char buffer[DN_IFREQ_SIZE];
	struct ifreq *ifr = (struct ifreq *)buffer;
	struct sockaddr_dn *sdn = (struct sockaddr_dn *)&ifr->ifr_addr;
	struct dn_dev *dn_db;
	struct net_device *dev;
	struct dn_ifaddr *ifa = NULL, **ifap = NULL;
	int ret = 0;

	if (copy_from_user(ifr, arg, DN_IFREQ_SIZE))
		return -EFAULT;
	ifr->ifr_name[IFNAMSIZ-1] = 0;

#ifdef CONFIG_KMOD
494
	dev_load(&init_net, ifr->ifr_name);
L
Linus Torvalds 已提交
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
#endif

	switch(cmd) {
		case SIOCGIFADDR:
			break;
		case SIOCSIFADDR:
			if (!capable(CAP_NET_ADMIN))
				return -EACCES;
			if (sdn->sdn_family != AF_DECnet)
				return -EINVAL;
			break;
		default:
			return -EINVAL;
	}

	rtnl_lock();

512
	if ((dev = __dev_get_by_name(&init_net, ifr->ifr_name)) == NULL) {
L
Linus Torvalds 已提交
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
		ret = -ENODEV;
		goto done;
	}

	if ((dn_db = dev->dn_ptr) != NULL) {
		for (ifap = &dn_db->ifa_list; (ifa=*ifap) != NULL; ifap = &ifa->ifa_next)
			if (strcmp(ifr->ifr_name, ifa->ifa_label) == 0)
				break;
	}

	if (ifa == NULL && cmd != SIOCSIFADDR) {
		ret = -EADDRNOTAVAIL;
		goto done;
	}

	switch(cmd) {
		case SIOCGIFADDR:
530
			*((__le16 *)sdn->sdn_nodeaddr) = ifa->ifa_local;
L
Linus Torvalds 已提交
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
			goto rarok;

		case SIOCSIFADDR:
			if (!ifa) {
				if ((ifa = dn_dev_alloc_ifa()) == NULL) {
					ret = -ENOBUFS;
					break;
				}
				memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
			} else {
				if (ifa->ifa_local == dn_saddr2dn(sdn))
					break;
				dn_dev_del_ifa(dn_db, ifap, 0);
			}

			ifa->ifa_local = ifa->ifa_address = dn_saddr2dn(sdn);

			ret = dn_dev_set_ifa(dev, ifa);
	}
done:
	rtnl_unlock();

	return ret;
rarok:
	if (copy_to_user(arg, ifr, DN_IFREQ_SIZE))
		ret = -EFAULT;
	goto done;
}

struct net_device *dn_dev_get_default(void)
{
	struct net_device *dev;
	read_lock(&dndev_lock);
	dev = decnet_default_device;
	if (dev) {
		if (dev->dn_ptr)
			dev_hold(dev);
		else
			dev = NULL;
	}
	read_unlock(&dndev_lock);
	return dev;
}

int dn_dev_set_default(struct net_device *dev, int force)
{
	struct net_device *old = NULL;
	int rv = -EBUSY;
	if (!dev->dn_ptr)
		return -ENODEV;
	write_lock(&dndev_lock);
	if (force || decnet_default_device == NULL) {
		old = decnet_default_device;
		decnet_default_device = dev;
		rv = 0;
	}
	write_unlock(&dndev_lock);
	if (old)
P
Patrick Caulfield 已提交
589
		dev_put(old);
L
Linus Torvalds 已提交
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
	return rv;
}

static void dn_dev_check_default(struct net_device *dev)
{
	write_lock(&dndev_lock);
	if (dev == decnet_default_device) {
		decnet_default_device = NULL;
	} else {
		dev = NULL;
	}
	write_unlock(&dndev_lock);
	if (dev)
		dev_put(dev);
}

static struct dn_dev *dn_dev_by_index(int ifindex)
{
	struct net_device *dev;
	struct dn_dev *dn_dev = NULL;
610
	dev = dev_get_by_index(&init_net, ifindex);
L
Linus Torvalds 已提交
611 612 613 614 615 616 617 618
	if (dev) {
		dn_dev = dev->dn_ptr;
		dev_put(dev);
	}

	return dn_dev;
}

619
static const struct nla_policy dn_ifa_policy[IFA_MAX+1] = {
620 621 622 623 624 625 626
	[IFA_ADDRESS]		= { .type = NLA_U16 },
	[IFA_LOCAL]		= { .type = NLA_U16 },
	[IFA_LABEL]		= { .type = NLA_STRING,
				    .len = IFNAMSIZ - 1 },
};

static int dn_nl_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
L
Linus Torvalds 已提交
627
{
628
	struct net *net = sock_net(skb->sk);
629
	struct nlattr *tb[IFA_MAX+1];
L
Linus Torvalds 已提交
630
	struct dn_dev *dn_db;
631
	struct ifaddrmsg *ifm;
L
Linus Torvalds 已提交
632
	struct dn_ifaddr *ifa, **ifap;
633 634 635 636
	int err = -EINVAL;

	if (net != &init_net)
		goto errout;
637 638 639 640

	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, dn_ifa_policy);
	if (err < 0)
		goto errout;
L
Linus Torvalds 已提交
641

642
	err = -ENODEV;
643
	ifm = nlmsg_data(nlh);
L
Linus Torvalds 已提交
644
	if ((dn_db = dn_dev_by_index(ifm->ifa_index)) == NULL)
645 646
		goto errout;

647
	err = -EADDRNOTAVAIL;
648 649 650 651
	for (ifap = &dn_db->ifa_list; (ifa = *ifap); ifap = &ifa->ifa_next) {
		if (tb[IFA_LOCAL] &&
		    nla_memcmp(tb[IFA_LOCAL], &ifa->ifa_local, 2))
			continue;
L
Linus Torvalds 已提交
652

653
		if (tb[IFA_LABEL] && nla_strcmp(tb[IFA_LABEL], ifa->ifa_label))
L
Linus Torvalds 已提交
654 655 656 657 658 659
			continue;

		dn_dev_del_ifa(dn_db, ifap, 1);
		return 0;
	}

660 661
errout:
	return err;
L
Linus Torvalds 已提交
662 663
}

664
static int dn_nl_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
L
Linus Torvalds 已提交
665
{
666
	struct net *net = sock_net(skb->sk);
667
	struct nlattr *tb[IFA_MAX+1];
L
Linus Torvalds 已提交
668 669
	struct net_device *dev;
	struct dn_dev *dn_db;
670
	struct ifaddrmsg *ifm;
L
Linus Torvalds 已提交
671
	struct dn_ifaddr *ifa;
672 673
	int err;

674 675 676
	if (net != &init_net)
		return -EINVAL;

677 678 679
	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, dn_ifa_policy);
	if (err < 0)
		return err;
L
Linus Torvalds 已提交
680

681
	if (tb[IFA_LOCAL] == NULL)
L
Linus Torvalds 已提交
682 683
		return -EINVAL;

684
	ifm = nlmsg_data(nlh);
685
	if ((dev = __dev_get_by_index(&init_net, ifm->ifa_index)) == NULL)
L
Linus Torvalds 已提交
686 687 688 689 690 691 692 693
		return -ENODEV;

	if ((dn_db = dev->dn_ptr) == NULL) {
		int err;
		dn_db = dn_dev_create(dev, &err);
		if (!dn_db)
			return err;
	}
694

L
Linus Torvalds 已提交
695 696 697
	if ((ifa = dn_dev_alloc_ifa()) == NULL)
		return -ENOBUFS;

698 699 700 701 702
	if (tb[IFA_ADDRESS] == NULL)
		tb[IFA_ADDRESS] = tb[IFA_LOCAL];

	ifa->ifa_local = nla_get_le16(tb[IFA_LOCAL]);
	ifa->ifa_address = nla_get_le16(tb[IFA_ADDRESS]);
L
Linus Torvalds 已提交
703 704 705
	ifa->ifa_flags = ifm->ifa_flags;
	ifa->ifa_scope = ifm->ifa_scope;
	ifa->ifa_dev = dn_db;
706 707 708

	if (tb[IFA_LABEL])
		nla_strlcpy(ifa->ifa_label, tb[IFA_LABEL], IFNAMSIZ);
L
Linus Torvalds 已提交
709 710 711
	else
		memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);

712 713
	err = dn_dev_insert_ifa(dn_db, ifa);
	if (err)
L
Linus Torvalds 已提交
714
		dn_dev_free_ifa(ifa);
715 716

	return err;
L
Linus Torvalds 已提交
717 718
}

719 720 721 722 723 724 725 726
static inline size_t dn_ifaddr_nlmsg_size(void)
{
	return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
	       + nla_total_size(IFNAMSIZ) /* IFA_LABEL */
	       + nla_total_size(2) /* IFA_ADDRESS */
	       + nla_total_size(2); /* IFA_LOCAL */
}

727 728
static int dn_nl_fill_ifaddr(struct sk_buff *skb, struct dn_ifaddr *ifa,
			     u32 pid, u32 seq, int event, unsigned int flags)
L
Linus Torvalds 已提交
729 730 731 732
{
	struct ifaddrmsg *ifm;
	struct nlmsghdr *nlh;

733 734
	nlh = nlmsg_put(skb, pid, seq, event, sizeof(*ifm), flags);
	if (nlh == NULL)
735
		return -EMSGSIZE;
L
Linus Torvalds 已提交
736

737
	ifm = nlmsg_data(nlh);
L
Linus Torvalds 已提交
738 739 740 741 742
	ifm->ifa_family = AF_DECnet;
	ifm->ifa_prefixlen = 16;
	ifm->ifa_flags = ifa->ifa_flags | IFA_F_PERMANENT;
	ifm->ifa_scope = ifa->ifa_scope;
	ifm->ifa_index = ifa->ifa_dev->dev->ifindex;
743

L
Linus Torvalds 已提交
744
	if (ifa->ifa_address)
745
		NLA_PUT_LE16(skb, IFA_ADDRESS, ifa->ifa_address);
L
Linus Torvalds 已提交
746
	if (ifa->ifa_local)
747
		NLA_PUT_LE16(skb, IFA_LOCAL, ifa->ifa_local);
L
Linus Torvalds 已提交
748
	if (ifa->ifa_label[0])
749
		NLA_PUT_STRING(skb, IFA_LABEL, ifa->ifa_label);
L
Linus Torvalds 已提交
750

751 752 753
	return nlmsg_end(skb, nlh);

nla_put_failure:
754 755
	nlmsg_cancel(skb, nlh);
	return -EMSGSIZE;
L
Linus Torvalds 已提交
756 757
}

758
static void dn_ifaddr_notify(int event, struct dn_ifaddr *ifa)
L
Linus Torvalds 已提交
759 760
{
	struct sk_buff *skb;
761
	int err = -ENOBUFS;
L
Linus Torvalds 已提交
762

763
	skb = alloc_skb(dn_ifaddr_nlmsg_size(), GFP_KERNEL);
764 765 766
	if (skb == NULL)
		goto errout;

767
	err = dn_nl_fill_ifaddr(skb, ifa, 0, 0, event, 0);
768 769 770 771 772 773
	if (err < 0) {
		/* -EMSGSIZE implies BUG in dn_ifaddr_nlmsg_size() */
		WARN_ON(err == -EMSGSIZE);
		kfree_skb(skb);
		goto errout;
	}
774
	err = rtnl_notify(skb, &init_net, 0, RTNLGRP_DECnet_IFADDR, NULL, GFP_KERNEL);
775 776
errout:
	if (err < 0)
777
		rtnl_set_sk_err(&init_net, RTNLGRP_DECnet_IFADDR, err);
L
Linus Torvalds 已提交
778 779
}

780
static int dn_nl_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
L
Linus Torvalds 已提交
781
{
782
	struct net *net = sock_net(skb->sk);
783
	int idx, dn_idx = 0, skip_ndevs, skip_naddr;
L
Linus Torvalds 已提交
784 785 786 787
	struct net_device *dev;
	struct dn_dev *dn_db;
	struct dn_ifaddr *ifa;

788 789 790
	if (net != &init_net)
		return 0;

791 792 793
	skip_ndevs = cb->args[0];
	skip_naddr = cb->args[1];

794
	idx = 0;
795
	for_each_netdev(&init_net, dev) {
796
		if (idx < skip_ndevs)
797
			goto cont;
798 799 800 801 802 803
		else if (idx > skip_ndevs) {
			/* Only skip over addresses for first dev dumped
			 * in this iteration (idx == skip_ndevs) */
			skip_naddr = 0;
		}

L
Linus Torvalds 已提交
804
		if ((dn_db = dev->dn_ptr) == NULL)
805
			goto cont;
L
Linus Torvalds 已提交
806

807 808 809
		for (ifa = dn_db->ifa_list, dn_idx = 0; ifa;
		     ifa = ifa->ifa_next, dn_idx++) {
			if (dn_idx < skip_naddr)
810
				continue;
L
Linus Torvalds 已提交
811

812 813 814
			if (dn_nl_fill_ifaddr(skb, ifa, NETLINK_CB(cb->skb).pid,
					      cb->nlh->nlmsg_seq, RTM_NEWADDR,
					      NLM_F_MULTI) < 0)
L
Linus Torvalds 已提交
815 816
				goto done;
		}
817 818
cont:
		idx++;
L
Linus Torvalds 已提交
819 820 821 822 823 824 825 826
	}
done:
	cb->args[0] = idx;
	cb->args[1] = dn_idx;

	return skb->len;
}

827
static int dn_dev_get_first(struct net_device *dev, __le16 *addr)
L
Linus Torvalds 已提交
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
{
	struct dn_dev *dn_db = (struct dn_dev *)dev->dn_ptr;
	struct dn_ifaddr *ifa;
	int rv = -ENODEV;
	if (dn_db == NULL)
		goto out;
	ifa = dn_db->ifa_list;
	if (ifa != NULL) {
		*addr = ifa->ifa_local;
		rv = 0;
	}
out:
	return rv;
}

843
/*
L
Linus Torvalds 已提交
844 845 846 847 848 849 850 851 852
 * Find a default address to bind to.
 *
 * This is one of those areas where the initial VMS concepts don't really
 * map onto the Linux concepts, and since we introduced multiple addresses
 * per interface we have to cope with slightly odd ways of finding out what
 * "our address" really is. Mostly it's not a problem; for this we just guess
 * a sensible default. Eventually the routing code will take care of all the
 * nasties for us I hope.
 */
853
int dn_dev_bind_default(__le16 *addr)
L
Linus Torvalds 已提交
854 855 856 857 858 859 860 861 862 863
{
	struct net_device *dev;
	int rv;
	dev = dn_dev_get_default();
last_chance:
	if (dev) {
		read_lock(&dev_base_lock);
		rv = dn_dev_get_first(dev, addr);
		read_unlock(&dev_base_lock);
		dev_put(dev);
864
		if (rv == 0 || dev == init_net.loopback_dev)
L
Linus Torvalds 已提交
865 866
			return rv;
	}
867
	dev = init_net.loopback_dev;
L
Linus Torvalds 已提交
868 869 870 871 872 873
	dev_hold(dev);
	goto last_chance;
}

static void dn_send_endnode_hello(struct net_device *dev, struct dn_ifaddr *ifa)
{
874 875 876
	struct endnode_hello_message *msg;
	struct sk_buff *skb = NULL;
	__le16 *pktlen;
L
Linus Torvalds 已提交
877 878
	struct dn_dev *dn_db = (struct dn_dev *)dev->dn_ptr;

879
	if ((skb = dn_alloc_skb(NULL, sizeof(*msg), GFP_ATOMIC)) == NULL)
L
Linus Torvalds 已提交
880 881
		return;

882
	skb->dev = dev;
L
Linus Torvalds 已提交
883

884
	msg = (struct endnode_hello_message *)skb_put(skb,sizeof(*msg));
L
Linus Torvalds 已提交
885

886 887
	msg->msgflg  = 0x0D;
	memcpy(msg->tiver, dn_eco_version, 3);
L
Linus Torvalds 已提交
888
	dn_dn2eth(msg->id, ifa->ifa_local);
889 890 891 892 893
	msg->iinfo   = DN_RT_INFO_ENDN;
	msg->blksize = dn_htons(mtu2blksize(dev));
	msg->area    = 0x00;
	memset(msg->seed, 0, 8);
	memcpy(msg->neighbor, dn_hiord, ETH_ALEN);
L
Linus Torvalds 已提交
894 895 896 897 898 899

	if (dn_db->router) {
		struct dn_neigh *dn = (struct dn_neigh *)dn_db->router;
		dn_dn2eth(msg->neighbor, dn->addr);
	}

900 901 902 903 904 905 906
	msg->timer   = dn_htons((unsigned short)dn_db->parms.t3);
	msg->mpd     = 0x00;
	msg->datalen = 0x02;
	memset(msg->data, 0xAA, 2);

	pktlen = (__le16 *)skb_push(skb,2);
	*pktlen = dn_htons(skb->len - 2);
L
Linus Torvalds 已提交
907

908
	skb_reset_network_header(skb);
L
Linus Torvalds 已提交
909 910 911 912 913 914 915 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

	dn_rt_finish_output(skb, dn_rt_all_rt_mcast, msg->id);
}


#define DRDELAY (5 * HZ)

static int dn_am_i_a_router(struct dn_neigh *dn, struct dn_dev *dn_db, struct dn_ifaddr *ifa)
{
	/* First check time since device went up */
	if ((jiffies - dn_db->uptime) < DRDELAY)
		return 0;

	/* If there is no router, then yes... */
	if (!dn_db->router)
		return 1;

	/* otherwise only if we have a higher priority or.. */
	if (dn->priority < dn_db->parms.priority)
		return 1;

	/* if we have equal priority and a higher node number */
	if (dn->priority != dn_db->parms.priority)
		return 0;

	if (dn_ntohs(dn->addr) < dn_ntohs(ifa->ifa_local))
		return 1;

	return 0;
}

static void dn_send_router_hello(struct net_device *dev, struct dn_ifaddr *ifa)
{
	int n;
	struct dn_dev *dn_db = dev->dn_ptr;
	struct dn_neigh *dn = (struct dn_neigh *)dn_db->router;
	struct sk_buff *skb;
	size_t size;
	unsigned char *ptr;
	unsigned char *i1, *i2;
949
	__le16 *pktlen;
L
Linus Torvalds 已提交
950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975
	char *src;

	if (mtu2blksize(dev) < (26 + 7))
		return;

	n = mtu2blksize(dev) - 26;
	n /= 7;

	if (n > 32)
		n = 32;

	size = 2 + 26 + 7 * n;

	if ((skb = dn_alloc_skb(NULL, size, GFP_ATOMIC)) == NULL)
		return;

	skb->dev = dev;
	ptr = skb_put(skb, size);

	*ptr++ = DN_RT_PKT_CNTL | DN_RT_PKT_ERTH;
	*ptr++ = 2; /* ECO */
	*ptr++ = 0;
	*ptr++ = 0;
	dn_dn2eth(ptr, ifa->ifa_local);
	src = ptr;
	ptr += ETH_ALEN;
976
	*ptr++ = dn_db->parms.forwarding == 1 ?
L
Linus Torvalds 已提交
977
			DN_RT_INFO_L1RT : DN_RT_INFO_L2RT;
978
	*((__le16 *)ptr) = dn_htons(mtu2blksize(dev));
L
Linus Torvalds 已提交
979
	ptr += 2;
980
	*ptr++ = dn_db->parms.priority; /* Priority */
L
Linus Torvalds 已提交
981
	*ptr++ = 0; /* Area: Reserved */
982
	*((__le16 *)ptr) = dn_htons((unsigned short)dn_db->parms.t3);
L
Linus Torvalds 已提交
983 984 985 986 987 988 989 990 991 992 993 994 995 996
	ptr += 2;
	*ptr++ = 0; /* MPD: Reserved */
	i1 = ptr++;
	memset(ptr, 0, 7); /* Name: Reserved */
	ptr += 7;
	i2 = ptr++;

	n = dn_neigh_elist(dev, ptr, n);

	*i2 = 7 * n;
	*i1 = 8 + *i2;

	skb_trim(skb, (27 + *i2));

997
	pktlen = (__le16 *)skb_push(skb, 2);
L
Linus Torvalds 已提交
998 999
	*pktlen = dn_htons(skb->len - 2);

1000
	skb_reset_network_header(skb);
L
Linus Torvalds 已提交
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038

	if (dn_am_i_a_router(dn, dn_db, ifa)) {
		struct sk_buff *skb2 = skb_copy(skb, GFP_ATOMIC);
		if (skb2) {
			dn_rt_finish_output(skb2, dn_rt_all_end_mcast, src);
		}
	}

	dn_rt_finish_output(skb, dn_rt_all_rt_mcast, src);
}

static void dn_send_brd_hello(struct net_device *dev, struct dn_ifaddr *ifa)
{
	struct dn_dev *dn_db = (struct dn_dev *)dev->dn_ptr;

	if (dn_db->parms.forwarding == 0)
		dn_send_endnode_hello(dev, ifa);
	else
		dn_send_router_hello(dev, ifa);
}

static void dn_send_ptp_hello(struct net_device *dev, struct dn_ifaddr *ifa)
{
	int tdlen = 16;
	int size = dev->hard_header_len + 2 + 4 + tdlen;
	struct sk_buff *skb = dn_alloc_skb(NULL, size, GFP_ATOMIC);
	int i;
	unsigned char *ptr;
	char src[ETH_ALEN];

	if (skb == NULL)
		return ;

	skb->dev = dev;
	skb_push(skb, dev->hard_header_len);
	ptr = skb_put(skb, 2 + 4 + tdlen);

	*ptr++ = DN_RT_PKT_HELO;
1039
	*((__le16 *)ptr) = ifa->ifa_local;
L
Linus Torvalds 已提交
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 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 1124 1125 1126
	ptr += 2;
	*ptr++ = tdlen;

	for(i = 0; i < tdlen; i++)
		*ptr++ = 0252;

	dn_dn2eth(src, ifa->ifa_local);
	dn_rt_finish_output(skb, dn_rt_all_rt_mcast, src);
}

static int dn_eth_up(struct net_device *dev)
{
	struct dn_dev *dn_db = dev->dn_ptr;

	if (dn_db->parms.forwarding == 0)
		dev_mc_add(dev, dn_rt_all_end_mcast, ETH_ALEN, 0);
	else
		dev_mc_add(dev, dn_rt_all_rt_mcast, ETH_ALEN, 0);

	dn_db->use_long = 1;

	return 0;
}

static void dn_eth_down(struct net_device *dev)
{
	struct dn_dev *dn_db = dev->dn_ptr;

	if (dn_db->parms.forwarding == 0)
		dev_mc_delete(dev, dn_rt_all_end_mcast, ETH_ALEN, 0);
	else
		dev_mc_delete(dev, dn_rt_all_rt_mcast, ETH_ALEN, 0);
}

static void dn_dev_set_timer(struct net_device *dev);

static void dn_dev_timer_func(unsigned long arg)
{
	struct net_device *dev = (struct net_device *)arg;
	struct dn_dev *dn_db = dev->dn_ptr;
	struct dn_ifaddr *ifa;

	if (dn_db->t3 <= dn_db->parms.t2) {
		if (dn_db->parms.timer3) {
			for(ifa = dn_db->ifa_list; ifa; ifa = ifa->ifa_next) {
				if (!(ifa->ifa_flags & IFA_F_SECONDARY))
					dn_db->parms.timer3(dev, ifa);
			}
		}
		dn_db->t3 = dn_db->parms.t3;
	} else {
		dn_db->t3 -= dn_db->parms.t2;
	}

	dn_dev_set_timer(dev);
}

static void dn_dev_set_timer(struct net_device *dev)
{
	struct dn_dev *dn_db = dev->dn_ptr;

	if (dn_db->parms.t2 > dn_db->parms.t3)
		dn_db->parms.t2 = dn_db->parms.t3;

	dn_db->timer.data = (unsigned long)dev;
	dn_db->timer.function = dn_dev_timer_func;
	dn_db->timer.expires = jiffies + (dn_db->parms.t2 * HZ);

	add_timer(&dn_db->timer);
}

struct dn_dev *dn_dev_create(struct net_device *dev, int *err)
{
	int i;
	struct dn_dev_parms *p = dn_dev_list;
	struct dn_dev *dn_db;

	for(i = 0; i < DN_DEV_LIST_SIZE; i++, p++) {
		if (p->type == dev->type)
			break;
	}

	*err = -ENODEV;
	if (i == DN_DEV_LIST_SIZE)
		return NULL;

	*err = -ENOBUFS;
1127
	if ((dn_db = kzalloc(sizeof(struct dn_dev), GFP_ATOMIC)) == NULL)
L
Linus Torvalds 已提交
1128 1129 1130 1131 1132 1133 1134 1135 1136
		return NULL;

	memcpy(&dn_db->parms, p, sizeof(struct dn_dev_parms));
	smp_wmb();
	dev->dn_ptr = dn_db;
	dn_db->dev = dev;
	init_timer(&dn_db->timer);

	dn_db->uptime = jiffies;
1137 1138 1139 1140 1141 1142 1143 1144

	dn_db->neigh_parms = neigh_parms_alloc(dev, &dn_neigh_table);
	if (!dn_db->neigh_parms) {
		dev->dn_ptr = NULL;
		kfree(dn_db);
		return NULL;
	}

L
Linus Torvalds 已提交
1145 1146
	if (dn_db->parms.up) {
		if (dn_db->parms.up(dev) < 0) {
1147
			neigh_parms_release(&dn_neigh_table, dn_db->neigh_parms);
L
Linus Torvalds 已提交
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
			dev->dn_ptr = NULL;
			kfree(dn_db);
			return NULL;
		}
	}

	dn_dev_sysctl_register(dev, &dn_db->parms);

	dn_dev_set_timer(dev);

	*err = 0;
	return dn_db;
}


/*
 * This processes a device up event. We only start up
 * the loopback device & ethernet devices with correct
 * MAC addreses automatically. Others must be started
 * specifically.
 *
 * FIXME: How should we configure the loopback address ? If we could dispense
 * with using decnet_address here and for autobind, it will be one less thing
 * for users to worry about setting up.
 */

void dn_dev_up(struct net_device *dev)
{
	struct dn_ifaddr *ifa;
1177
	__le16 addr = decnet_address;
L
Linus Torvalds 已提交
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
	int maybe_default = 0;
	struct dn_dev *dn_db = (struct dn_dev *)dev->dn_ptr;

	if ((dev->type != ARPHRD_ETHER) && (dev->type != ARPHRD_LOOPBACK))
		return;

	/*
	 * Need to ensure that loopback device has a dn_db attached to it
	 * to allow creation of neighbours against it, even though it might
	 * not have a local address of its own. Might as well do the same for
	 * all autoconfigured interfaces.
	 */
	if (dn_db == NULL) {
		int err;
		dn_db = dn_dev_create(dev, &err);
		if (dn_db == NULL)
			return;
	}

	if (dev->type == ARPHRD_ETHER) {
		if (memcmp(dev->dev_addr, dn_hiord, 4) != 0)
			return;
1200
		addr = dn_eth2dn(dev->dev_addr);
L
Linus Torvalds 已提交
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
		maybe_default = 1;
	}

	if (addr == 0)
		return;

	if ((ifa = dn_dev_alloc_ifa()) == NULL)
		return;

	ifa->ifa_local = ifa->ifa_address = addr;
	ifa->ifa_flags = 0;
	ifa->ifa_scope = RT_SCOPE_UNIVERSE;
	strcpy(ifa->ifa_label, dev->name);

	dn_dev_set_ifa(dev, ifa);

	/*
	 * Automagically set the default device to the first automatically
	 * configured ethernet card in the system.
	 */
	if (maybe_default) {
		dev_hold(dev);
		if (dn_dev_set_default(dev, 0))
			dev_put(dev);
	}
}

static void dn_dev_delete(struct net_device *dev)
{
	struct dn_dev *dn_db = dev->dn_ptr;

	if (dn_db == NULL)
		return;

	del_timer_sync(&dn_db->timer);
	dn_dev_sysctl_unregister(&dn_db->parms);
	dn_dev_check_default(dev);
	neigh_ifdown(&dn_neigh_table, dev);

	if (dn_db->parms.down)
		dn_db->parms.down(dev);

	dev->dn_ptr = NULL;

	neigh_parms_release(&dn_neigh_table, dn_db->neigh_parms);
	neigh_ifdown(&dn_neigh_table, dev);

	if (dn_db->router)
		neigh_release(dn_db->router);
	if (dn_db->peer)
		neigh_release(dn_db->peer);

	kfree(dn_db);
}

void dn_dev_down(struct net_device *dev)
{
	struct dn_dev *dn_db = dev->dn_ptr;
	struct dn_ifaddr *ifa;

	if (dn_db == NULL)
		return;

	while((ifa = dn_db->ifa_list) != NULL) {
		dn_dev_del_ifa(dn_db, &dn_db->ifa_list, 0);
		dn_dev_free_ifa(ifa);
	}

	dn_dev_delete(dev);
}

void dn_dev_init_pkt(struct sk_buff *skb)
{
	return;
}

void dn_dev_veri_pkt(struct sk_buff *skb)
{
	return;
}

void dn_dev_hello(struct sk_buff *skb)
{
	return;
}

void dn_dev_devices_off(void)
{
	struct net_device *dev;

	rtnl_lock();
1292
	for_each_netdev(&init_net, dev)
L
Linus Torvalds 已提交
1293 1294 1295 1296 1297 1298 1299 1300 1301 1302
		dn_dev_down(dev);
	rtnl_unlock();

}

void dn_dev_devices_on(void)
{
	struct net_device *dev;

	rtnl_lock();
1303
	for_each_netdev(&init_net, dev) {
L
Linus Torvalds 已提交
1304 1305 1306 1307 1308 1309 1310 1311
		if (dev->flags & IFF_UP)
			dn_dev_up(dev);
	}
	rtnl_unlock();
}

int register_dnaddr_notifier(struct notifier_block *nb)
{
1312
	return blocking_notifier_chain_register(&dnaddr_chain, nb);
L
Linus Torvalds 已提交
1313 1314 1315 1316
}

int unregister_dnaddr_notifier(struct notifier_block *nb)
{
1317
	return blocking_notifier_chain_unregister(&dnaddr_chain, nb);
L
Linus Torvalds 已提交
1318 1319 1320
}

#ifdef CONFIG_PROC_FS
1321
static inline int is_dn_dev(struct net_device *dev)
L
Linus Torvalds 已提交
1322
{
1323
	return dev->dn_ptr != NULL;
L
Linus Torvalds 已提交
1324 1325
}

1326
static void *dn_dev_seq_start(struct seq_file *seq, loff_t *pos)
L
Linus Torvalds 已提交
1327
{
1328
	int i;
L
Linus Torvalds 已提交
1329 1330
	struct net_device *dev;

1331
	read_lock(&dev_base_lock);
L
Linus Torvalds 已提交
1332

1333 1334 1335 1336
	if (*pos == 0)
		return SEQ_START_TOKEN;

	i = 1;
1337
	for_each_netdev(&init_net, dev) {
1338 1339 1340 1341 1342
		if (!is_dn_dev(dev))
			continue;

		if (i++ == *pos)
			return dev;
L
Linus Torvalds 已提交
1343
	}
1344 1345

	return NULL;
L
Linus Torvalds 已提交
1346 1347 1348 1349
}

static void *dn_dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
{
1350
	struct net_device *dev;
L
Linus Torvalds 已提交
1351 1352

	++*pos;
1353 1354 1355

	dev = (struct net_device *)v;
	if (v == SEQ_START_TOKEN)
1356
		dev = net_device_entry(&init_net.dev_base_head);
1357

1358
	for_each_netdev_continue(&init_net, dev) {
1359 1360 1361 1362 1363 1364 1365
		if (!is_dn_dev(dev))
			continue;

		return dev;
	}

	return NULL;
L
Linus Torvalds 已提交
1366 1367 1368 1369
}

static void dn_dev_seq_stop(struct seq_file *seq, void *v)
{
1370
	read_unlock(&dev_base_lock);
L
Linus Torvalds 已提交
1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389
}

static char *dn_type2asc(char type)
{
	switch(type) {
		case DN_DEV_BCAST:
			return "B";
		case DN_DEV_UCAST:
			return "U";
		case DN_DEV_MPOINT:
			return "M";
	}

	return "?";
}

static int dn_dev_seq_show(struct seq_file *seq, void *v)
{
	if (v == SEQ_START_TOKEN)
1390
		seq_puts(seq, "Name     Flags T1   Timer1 T3   Timer3 BlkSize Pri State DevType    Router Peer\n");
L
Linus Torvalds 已提交
1391 1392 1393 1394 1395 1396
	else {
		struct net_device *dev = v;
		char peer_buf[DN_ASCBUF_LEN];
		char router_buf[DN_ASCBUF_LEN];
		struct dn_dev *dn_db = dev->dn_ptr;

1397
		seq_printf(seq, "%-8s %1s     %04u %04u   %04lu %04lu"
L
Linus Torvalds 已提交
1398
				"   %04hu    %03d %02x    %-10s %-7s %-7s\n",
1399 1400 1401
				dev->name ? dev->name : "???",
				dn_type2asc(dn_db->parms.mode),
				0, 0,
L
Linus Torvalds 已提交
1402 1403 1404 1405
				dn_db->t3, dn_db->parms.t3,
				mtu2blksize(dev),
				dn_db->parms.priority,
				dn_db->parms.state, dn_db->parms.name,
1406 1407
				dn_db->router ? dn_addr2asc(dn_ntohs(*(__le16 *)dn_db->router->primary_key), router_buf) : "",
				dn_db->peer ? dn_addr2asc(dn_ntohs(*(__le16 *)dn_db->peer->primary_key), peer_buf) : "");
L
Linus Torvalds 已提交
1408 1409 1410 1411
	}
	return 0;
}

1412
static const struct seq_operations dn_dev_seq_ops = {
L
Linus Torvalds 已提交
1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423
	.start	= dn_dev_seq_start,
	.next	= dn_dev_seq_next,
	.stop	= dn_dev_seq_stop,
	.show	= dn_dev_seq_show,
};

static int dn_dev_seq_open(struct inode *inode, struct file *file)
{
	return seq_open(file, &dn_dev_seq_ops);
}

1424
static const struct file_operations dn_dev_seq_fops = {
L
Linus Torvalds 已提交
1425 1426 1427 1428 1429 1430 1431 1432 1433
	.owner	 = THIS_MODULE,
	.open	 = dn_dev_seq_open,
	.read	 = seq_read,
	.llseek	 = seq_lseek,
	.release = seq_release,
};

#endif /* CONFIG_PROC_FS */

1434
static int addr[2];
L
Linus Torvalds 已提交
1435 1436 1437 1438 1439
module_param_array(addr, int, NULL, 0444);
MODULE_PARM_DESC(addr, "The DECnet address of this machine: area,node");

void __init dn_dev_init(void)
{
1440 1441 1442 1443
	if (addr[0] > 63 || addr[0] < 0) {
		printk(KERN_ERR "DECnet: Area must be between 0 and 63");
		return;
	}
L
Linus Torvalds 已提交
1444

1445 1446 1447 1448
	if (addr[1] > 1023 || addr[1] < 0) {
		printk(KERN_ERR "DECnet: Node must be between 0 and 1023");
		return;
	}
L
Linus Torvalds 已提交
1449

1450
	decnet_address = dn_htons((addr[0] << 10) | addr[1]);
L
Linus Torvalds 已提交
1451 1452 1453

	dn_dev_devices_on();

1454 1455 1456
	rtnl_register(PF_DECnet, RTM_NEWADDR, dn_nl_newaddr, NULL);
	rtnl_register(PF_DECnet, RTM_DELADDR, dn_nl_deladdr, NULL);
	rtnl_register(PF_DECnet, RTM_GETADDR, NULL, dn_nl_dump_ifaddr);
L
Linus Torvalds 已提交
1457

1458
	proc_net_fops_create(&init_net, "decnet_dev", S_IRUGO, &dn_dev_seq_fops);
L
Linus Torvalds 已提交
1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478

#ifdef CONFIG_SYSCTL
	{
		int i;
		for(i = 0; i < DN_DEV_LIST_SIZE; i++)
			dn_dev_sysctl_register(NULL, &dn_dev_list[i]);
	}
#endif /* CONFIG_SYSCTL */
}

void __exit dn_dev_cleanup(void)
{
#ifdef CONFIG_SYSCTL
	{
		int i;
		for(i = 0; i < DN_DEV_LIST_SIZE; i++)
			dn_dev_sysctl_unregister(&dn_dev_list[i]);
	}
#endif /* CONFIG_SYSCTL */

1479
	proc_net_remove(&init_net, "decnet_dev");
L
Linus Torvalds 已提交
1480 1481 1482

	dn_dev_devices_off();
}