• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/linux/linux-2.6/net/decnet/
1/*
2 * DECnet       An implementation of the DECnet protocol suite for the LINUX
3 *              operating system.  DECnet is implemented using the  BSD Socket
4 *              interface as the means of communication with the user level.
5 *
6 *              DECnet Device Layer
7 *
8 * Authors:     Steve Whitehouse <SteveW@ACM.org>
9 *              Eduardo Marcelo Serrat <emserrat@geocities.com>
10 *
11 * Changes:
12 *          Steve Whitehouse : Devices now see incoming frames so they
13 *                             can mark on who it came from.
14 *          Steve Whitehouse : Fixed bug in creating neighbours. Each neighbour
15 *                             can now have a device specific setup func.
16 *          Steve Whitehouse : Added /proc/sys/net/decnet/conf/<dev>/
17 *          Steve Whitehouse : Fixed bug which sometimes killed timer
18 *          Steve Whitehouse : Multiple ifaddr support
19 *          Steve Whitehouse : SIOCGIFCONF is now a compile time option
20 *          Steve Whitehouse : /proc/sys/net/decnet/conf/<sys>/forwarding
21 *          Steve Whitehouse : Removed timer1 - it's a user space issue now
22 *         Patrick Caulfield : Fixed router hello message format
23 *          Steve Whitehouse : Got rid of constant sizes for blksize for
24 *                             devices. All mtu based now.
25 */
26
27#include <linux/capability.h>
28#include <linux/module.h>
29#include <linux/moduleparam.h>
30#include <linux/init.h>
31#include <linux/net.h>
32#include <linux/netdevice.h>
33#include <linux/proc_fs.h>
34#include <linux/seq_file.h>
35#include <linux/timer.h>
36#include <linux/string.h>
37#include <linux/if_addr.h>
38#include <linux/if_arp.h>
39#include <linux/if_ether.h>
40#include <linux/skbuff.h>
41#include <linux/sysctl.h>
42#include <linux/notifier.h>
43#include <linux/slab.h>
44#include <asm/uaccess.h>
45#include <asm/system.h>
46#include <net/net_namespace.h>
47#include <net/neighbour.h>
48#include <net/dst.h>
49#include <net/flow.h>
50#include <net/fib_rules.h>
51#include <net/netlink.h>
52#include <net/dn.h>
53#include <net/dn_dev.h>
54#include <net/dn_route.h>
55#include <net/dn_neigh.h>
56#include <net/dn_fib.h>
57
58#define DN_IFREQ_SIZE (sizeof(struct ifreq) - sizeof(struct sockaddr) + sizeof(struct sockaddr_dn))
59
60static char dn_rt_all_end_mcast[ETH_ALEN] = {0xAB,0x00,0x00,0x04,0x00,0x00};
61static char dn_rt_all_rt_mcast[ETH_ALEN]  = {0xAB,0x00,0x00,0x03,0x00,0x00};
62static char dn_hiord[ETH_ALEN]            = {0xAA,0x00,0x04,0x00,0x00,0x00};
63static unsigned char dn_eco_version[3]    = {0x02,0x00,0x00};
64
65extern struct neigh_table dn_neigh_table;
66
67/*
68 * decnet_address is kept in network order.
69 */
70__le16 decnet_address = 0;
71
72static DEFINE_SPINLOCK(dndev_lock);
73static struct net_device *decnet_default_device;
74static BLOCKING_NOTIFIER_HEAD(dnaddr_chain);
75
76static struct dn_dev *dn_dev_create(struct net_device *dev, int *err);
77static void dn_dev_delete(struct net_device *dev);
78static void dn_ifaddr_notify(int event, struct dn_ifaddr *ifa);
79
80static int dn_eth_up(struct net_device *);
81static void dn_eth_down(struct net_device *);
82static void dn_send_brd_hello(struct net_device *dev, struct dn_ifaddr *ifa);
83static void dn_send_ptp_hello(struct net_device *dev, struct dn_ifaddr *ifa);
84
85static struct dn_dev_parms dn_dev_list[] =  {
86{
87	.type =		ARPHRD_ETHER, /* Ethernet */
88	.mode =		DN_DEV_BCAST,
89	.state =	DN_DEV_S_RU,
90	.t2 =		1,
91	.t3 =		10,
92	.name =		"ethernet",
93	.up =		dn_eth_up,
94	.down = 	dn_eth_down,
95	.timer3 =	dn_send_brd_hello,
96},
97{
98	.type =		ARPHRD_IPGRE, /* DECnet tunneled over GRE in IP */
99	.mode =		DN_DEV_BCAST,
100	.state =	DN_DEV_S_RU,
101	.t2 =		1,
102	.t3 =		10,
103	.name =		"ipgre",
104	.timer3 =	dn_send_brd_hello,
105},
106{
107	.type =		ARPHRD_DDCMP, /* DECnet over DDCMP */
108	.mode =		DN_DEV_UCAST,
109	.state =	DN_DEV_S_DS,
110	.t2 =		1,
111	.t3 =		120,
112	.name =		"ddcmp",
113	.timer3 =	dn_send_ptp_hello,
114},
115{
116	.type =		ARPHRD_LOOPBACK, /* Loopback interface - always last */
117	.mode =		DN_DEV_BCAST,
118	.state =	DN_DEV_S_RU,
119	.t2 =		1,
120	.t3 =		10,
121	.name =		"loopback",
122	.timer3 =	dn_send_brd_hello,
123}
124};
125
126#define DN_DEV_LIST_SIZE ARRAY_SIZE(dn_dev_list)
127
128#define DN_DEV_PARMS_OFFSET(x) offsetof(struct dn_dev_parms, x)
129
130#ifdef CONFIG_SYSCTL
131
132static int min_t2[] = { 1 };
133static int max_t2[] = { 60 }; /* No max specified, but this seems sensible */
134static int min_t3[] = { 1 };
135static int max_t3[] = { 8191 }; /* Must fit in 16 bits when multiplied by BCT3MULT or T3MULT */
136
137static int min_priority[1];
138static int max_priority[] = { 127 }; /* From DECnet spec */
139
140static int dn_forwarding_proc(ctl_table *, int,
141			void __user *, size_t *, loff_t *);
142static struct dn_dev_sysctl_table {
143	struct ctl_table_header *sysctl_header;
144	ctl_table dn_dev_vars[5];
145} dn_dev_sysctl = {
146	NULL,
147	{
148	{
149		.procname = "forwarding",
150		.data = (void *)DN_DEV_PARMS_OFFSET(forwarding),
151		.maxlen = sizeof(int),
152		.mode = 0644,
153		.proc_handler = dn_forwarding_proc,
154	},
155	{
156		.procname = "priority",
157		.data = (void *)DN_DEV_PARMS_OFFSET(priority),
158		.maxlen = sizeof(int),
159		.mode = 0644,
160		.proc_handler = proc_dointvec_minmax,
161		.extra1 = &min_priority,
162		.extra2 = &max_priority
163	},
164	{
165		.procname = "t2",
166		.data = (void *)DN_DEV_PARMS_OFFSET(t2),
167		.maxlen = sizeof(int),
168		.mode = 0644,
169		.proc_handler = proc_dointvec_minmax,
170		.extra1 = &min_t2,
171		.extra2 = &max_t2
172	},
173	{
174		.procname = "t3",
175		.data = (void *)DN_DEV_PARMS_OFFSET(t3),
176		.maxlen = sizeof(int),
177		.mode = 0644,
178		.proc_handler = proc_dointvec_minmax,
179		.extra1 = &min_t3,
180		.extra2 = &max_t3
181	},
182	{0}
183	},
184};
185
186static void dn_dev_sysctl_register(struct net_device *dev, struct dn_dev_parms *parms)
187{
188	struct dn_dev_sysctl_table *t;
189	int i;
190
191#define DN_CTL_PATH_DEV	3
192
193	struct ctl_path dn_ctl_path[] = {
194		{ .procname = "net",  },
195		{ .procname = "decnet",  },
196		{ .procname = "conf",  },
197		{ /* to be set */ },
198		{ },
199	};
200
201	t = kmemdup(&dn_dev_sysctl, sizeof(*t), GFP_KERNEL);
202	if (t == NULL)
203		return;
204
205	for(i = 0; i < ARRAY_SIZE(t->dn_dev_vars) - 1; i++) {
206		long offset = (long)t->dn_dev_vars[i].data;
207		t->dn_dev_vars[i].data = ((char *)parms) + offset;
208	}
209
210	if (dev) {
211		dn_ctl_path[DN_CTL_PATH_DEV].procname = dev->name;
212	} else {
213		dn_ctl_path[DN_CTL_PATH_DEV].procname = parms->name;
214	}
215
216	t->dn_dev_vars[0].extra1 = (void *)dev;
217
218	t->sysctl_header = register_sysctl_paths(dn_ctl_path, t->dn_dev_vars);
219	if (t->sysctl_header == NULL)
220		kfree(t);
221	else
222		parms->sysctl = t;
223}
224
225static void dn_dev_sysctl_unregister(struct dn_dev_parms *parms)
226{
227	if (parms->sysctl) {
228		struct dn_dev_sysctl_table *t = parms->sysctl;
229		parms->sysctl = NULL;
230		unregister_sysctl_table(t->sysctl_header);
231		kfree(t);
232	}
233}
234
235static int dn_forwarding_proc(ctl_table *table, int write,
236				void __user *buffer,
237				size_t *lenp, loff_t *ppos)
238{
239#ifdef CONFIG_DECNET_ROUTER
240	struct net_device *dev = table->extra1;
241	struct dn_dev *dn_db;
242	int err;
243	int tmp, old;
244
245	if (table->extra1 == NULL)
246		return -EINVAL;
247
248	dn_db = dev->dn_ptr;
249	old = dn_db->parms.forwarding;
250
251	err = proc_dointvec(table, write, buffer, lenp, ppos);
252
253	if ((err >= 0) && write) {
254		if (dn_db->parms.forwarding < 0)
255			dn_db->parms.forwarding = 0;
256		if (dn_db->parms.forwarding > 2)
257			dn_db->parms.forwarding = 2;
258		/*
259		 * What an ugly hack this is... its works, just. It
260		 * would be nice if sysctl/proc were just that little
261		 * bit more flexible so I don't have to write a special
262		 * routine, or suffer hacks like this - SJW
263		 */
264		tmp = dn_db->parms.forwarding;
265		dn_db->parms.forwarding = old;
266		if (dn_db->parms.down)
267			dn_db->parms.down(dev);
268		dn_db->parms.forwarding = tmp;
269		if (dn_db->parms.up)
270			dn_db->parms.up(dev);
271	}
272
273	return err;
274#else
275	return -EINVAL;
276#endif
277}
278
279#else /* CONFIG_SYSCTL */
280static void dn_dev_sysctl_unregister(struct dn_dev_parms *parms)
281{
282}
283static void dn_dev_sysctl_register(struct net_device *dev, struct dn_dev_parms *parms)
284{
285}
286
287#endif /* CONFIG_SYSCTL */
288
289static inline __u16 mtu2blksize(struct net_device *dev)
290{
291	u32 blksize = dev->mtu;
292	if (blksize > 0xffff)
293		blksize = 0xffff;
294
295	if (dev->type == ARPHRD_ETHER ||
296	    dev->type == ARPHRD_PPP ||
297	    dev->type == ARPHRD_IPGRE ||
298	    dev->type == ARPHRD_LOOPBACK)
299		blksize -= 2;
300
301	return (__u16)blksize;
302}
303
304static struct dn_ifaddr *dn_dev_alloc_ifa(void)
305{
306	struct dn_ifaddr *ifa;
307
308	ifa = kzalloc(sizeof(*ifa), GFP_KERNEL);
309
310	return ifa;
311}
312
313static __inline__ void dn_dev_free_ifa(struct dn_ifaddr *ifa)
314{
315	kfree(ifa);
316}
317
318static void dn_dev_del_ifa(struct dn_dev *dn_db, struct dn_ifaddr **ifap, int destroy)
319{
320	struct dn_ifaddr *ifa1 = *ifap;
321	unsigned char mac_addr[6];
322	struct net_device *dev = dn_db->dev;
323
324	ASSERT_RTNL();
325
326	*ifap = ifa1->ifa_next;
327
328	if (dn_db->dev->type == ARPHRD_ETHER) {
329		if (ifa1->ifa_local != dn_eth2dn(dev->dev_addr)) {
330			dn_dn2eth(mac_addr, ifa1->ifa_local);
331			dev_mc_del(dev, mac_addr);
332		}
333	}
334
335	dn_ifaddr_notify(RTM_DELADDR, ifa1);
336	blocking_notifier_call_chain(&dnaddr_chain, NETDEV_DOWN, ifa1);
337	if (destroy) {
338		dn_dev_free_ifa(ifa1);
339
340		if (dn_db->ifa_list == NULL)
341			dn_dev_delete(dn_db->dev);
342	}
343}
344
345static int dn_dev_insert_ifa(struct dn_dev *dn_db, struct dn_ifaddr *ifa)
346{
347	struct net_device *dev = dn_db->dev;
348	struct dn_ifaddr *ifa1;
349	unsigned char mac_addr[6];
350
351	ASSERT_RTNL();
352
353	/* Check for duplicates */
354	for(ifa1 = dn_db->ifa_list; ifa1; ifa1 = ifa1->ifa_next) {
355		if (ifa1->ifa_local == ifa->ifa_local)
356			return -EEXIST;
357	}
358
359	if (dev->type == ARPHRD_ETHER) {
360		if (ifa->ifa_local != dn_eth2dn(dev->dev_addr)) {
361			dn_dn2eth(mac_addr, ifa->ifa_local);
362			dev_mc_add(dev, mac_addr);
363		}
364	}
365
366	ifa->ifa_next = dn_db->ifa_list;
367	dn_db->ifa_list = ifa;
368
369	dn_ifaddr_notify(RTM_NEWADDR, ifa);
370	blocking_notifier_call_chain(&dnaddr_chain, NETDEV_UP, ifa);
371
372	return 0;
373}
374
375static int dn_dev_set_ifa(struct net_device *dev, struct dn_ifaddr *ifa)
376{
377	struct dn_dev *dn_db = dev->dn_ptr;
378	int rv;
379
380	if (dn_db == NULL) {
381		int err;
382		dn_db = dn_dev_create(dev, &err);
383		if (dn_db == NULL)
384			return err;
385	}
386
387	ifa->ifa_dev = dn_db;
388
389	if (dev->flags & IFF_LOOPBACK)
390		ifa->ifa_scope = RT_SCOPE_HOST;
391
392	rv = dn_dev_insert_ifa(dn_db, ifa);
393	if (rv)
394		dn_dev_free_ifa(ifa);
395	return rv;
396}
397
398
399int dn_dev_ioctl(unsigned int cmd, void __user *arg)
400{
401	char buffer[DN_IFREQ_SIZE];
402	struct ifreq *ifr = (struct ifreq *)buffer;
403	struct sockaddr_dn *sdn = (struct sockaddr_dn *)&ifr->ifr_addr;
404	struct dn_dev *dn_db;
405	struct net_device *dev;
406	struct dn_ifaddr *ifa = NULL, **ifap = NULL;
407	int ret = 0;
408
409	if (copy_from_user(ifr, arg, DN_IFREQ_SIZE))
410		return -EFAULT;
411	ifr->ifr_name[IFNAMSIZ-1] = 0;
412
413	dev_load(&init_net, ifr->ifr_name);
414
415	switch(cmd) {
416		case SIOCGIFADDR:
417			break;
418		case SIOCSIFADDR:
419			if (!capable(CAP_NET_ADMIN))
420				return -EACCES;
421			if (sdn->sdn_family != AF_DECnet)
422				return -EINVAL;
423			break;
424		default:
425			return -EINVAL;
426	}
427
428	rtnl_lock();
429
430	if ((dev = __dev_get_by_name(&init_net, ifr->ifr_name)) == NULL) {
431		ret = -ENODEV;
432		goto done;
433	}
434
435	if ((dn_db = dev->dn_ptr) != NULL) {
436		for (ifap = &dn_db->ifa_list; (ifa=*ifap) != NULL; ifap = &ifa->ifa_next)
437			if (strcmp(ifr->ifr_name, ifa->ifa_label) == 0)
438				break;
439	}
440
441	if (ifa == NULL && cmd != SIOCSIFADDR) {
442		ret = -EADDRNOTAVAIL;
443		goto done;
444	}
445
446	switch(cmd) {
447		case SIOCGIFADDR:
448			*((__le16 *)sdn->sdn_nodeaddr) = ifa->ifa_local;
449			goto rarok;
450
451		case SIOCSIFADDR:
452			if (!ifa) {
453				if ((ifa = dn_dev_alloc_ifa()) == NULL) {
454					ret = -ENOBUFS;
455					break;
456				}
457				memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
458			} else {
459				if (ifa->ifa_local == dn_saddr2dn(sdn))
460					break;
461				dn_dev_del_ifa(dn_db, ifap, 0);
462			}
463
464			ifa->ifa_local = ifa->ifa_address = dn_saddr2dn(sdn);
465
466			ret = dn_dev_set_ifa(dev, ifa);
467	}
468done:
469	rtnl_unlock();
470
471	return ret;
472rarok:
473	if (copy_to_user(arg, ifr, DN_IFREQ_SIZE))
474		ret = -EFAULT;
475	goto done;
476}
477
478struct net_device *dn_dev_get_default(void)
479{
480	struct net_device *dev;
481
482	spin_lock(&dndev_lock);
483	dev = decnet_default_device;
484	if (dev) {
485		if (dev->dn_ptr)
486			dev_hold(dev);
487		else
488			dev = NULL;
489	}
490	spin_unlock(&dndev_lock);
491
492	return dev;
493}
494
495int dn_dev_set_default(struct net_device *dev, int force)
496{
497	struct net_device *old = NULL;
498	int rv = -EBUSY;
499	if (!dev->dn_ptr)
500		return -ENODEV;
501
502	spin_lock(&dndev_lock);
503	if (force || decnet_default_device == NULL) {
504		old = decnet_default_device;
505		decnet_default_device = dev;
506		rv = 0;
507	}
508	spin_unlock(&dndev_lock);
509
510	if (old)
511		dev_put(old);
512	return rv;
513}
514
515static void dn_dev_check_default(struct net_device *dev)
516{
517	spin_lock(&dndev_lock);
518	if (dev == decnet_default_device) {
519		decnet_default_device = NULL;
520	} else {
521		dev = NULL;
522	}
523	spin_unlock(&dndev_lock);
524
525	if (dev)
526		dev_put(dev);
527}
528
529/*
530 * Called with RTNL
531 */
532static struct dn_dev *dn_dev_by_index(int ifindex)
533{
534	struct net_device *dev;
535	struct dn_dev *dn_dev = NULL;
536
537	dev = __dev_get_by_index(&init_net, ifindex);
538	if (dev)
539		dn_dev = dev->dn_ptr;
540
541	return dn_dev;
542}
543
544static const struct nla_policy dn_ifa_policy[IFA_MAX+1] = {
545	[IFA_ADDRESS]		= { .type = NLA_U16 },
546	[IFA_LOCAL]		= { .type = NLA_U16 },
547	[IFA_LABEL]		= { .type = NLA_STRING,
548				    .len = IFNAMSIZ - 1 },
549};
550
551static int dn_nl_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
552{
553	struct net *net = sock_net(skb->sk);
554	struct nlattr *tb[IFA_MAX+1];
555	struct dn_dev *dn_db;
556	struct ifaddrmsg *ifm;
557	struct dn_ifaddr *ifa, **ifap;
558	int err = -EINVAL;
559
560	if (!net_eq(net, &init_net))
561		goto errout;
562
563	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, dn_ifa_policy);
564	if (err < 0)
565		goto errout;
566
567	err = -ENODEV;
568	ifm = nlmsg_data(nlh);
569	if ((dn_db = dn_dev_by_index(ifm->ifa_index)) == NULL)
570		goto errout;
571
572	err = -EADDRNOTAVAIL;
573	for (ifap = &dn_db->ifa_list; (ifa = *ifap); ifap = &ifa->ifa_next) {
574		if (tb[IFA_LOCAL] &&
575		    nla_memcmp(tb[IFA_LOCAL], &ifa->ifa_local, 2))
576			continue;
577
578		if (tb[IFA_LABEL] && nla_strcmp(tb[IFA_LABEL], ifa->ifa_label))
579			continue;
580
581		dn_dev_del_ifa(dn_db, ifap, 1);
582		return 0;
583	}
584
585errout:
586	return err;
587}
588
589static int dn_nl_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
590{
591	struct net *net = sock_net(skb->sk);
592	struct nlattr *tb[IFA_MAX+1];
593	struct net_device *dev;
594	struct dn_dev *dn_db;
595	struct ifaddrmsg *ifm;
596	struct dn_ifaddr *ifa;
597	int err;
598
599	if (!net_eq(net, &init_net))
600		return -EINVAL;
601
602	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, dn_ifa_policy);
603	if (err < 0)
604		return err;
605
606	if (tb[IFA_LOCAL] == NULL)
607		return -EINVAL;
608
609	ifm = nlmsg_data(nlh);
610	if ((dev = __dev_get_by_index(&init_net, ifm->ifa_index)) == NULL)
611		return -ENODEV;
612
613	if ((dn_db = dev->dn_ptr) == NULL) {
614		dn_db = dn_dev_create(dev, &err);
615		if (!dn_db)
616			return err;
617	}
618
619	if ((ifa = dn_dev_alloc_ifa()) == NULL)
620		return -ENOBUFS;
621
622	if (tb[IFA_ADDRESS] == NULL)
623		tb[IFA_ADDRESS] = tb[IFA_LOCAL];
624
625	ifa->ifa_local = nla_get_le16(tb[IFA_LOCAL]);
626	ifa->ifa_address = nla_get_le16(tb[IFA_ADDRESS]);
627	ifa->ifa_flags = ifm->ifa_flags;
628	ifa->ifa_scope = ifm->ifa_scope;
629	ifa->ifa_dev = dn_db;
630
631	if (tb[IFA_LABEL])
632		nla_strlcpy(ifa->ifa_label, tb[IFA_LABEL], IFNAMSIZ);
633	else
634		memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
635
636	err = dn_dev_insert_ifa(dn_db, ifa);
637	if (err)
638		dn_dev_free_ifa(ifa);
639
640	return err;
641}
642
643static inline size_t dn_ifaddr_nlmsg_size(void)
644{
645	return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
646	       + nla_total_size(IFNAMSIZ) /* IFA_LABEL */
647	       + nla_total_size(2) /* IFA_ADDRESS */
648	       + nla_total_size(2); /* IFA_LOCAL */
649}
650
651static int dn_nl_fill_ifaddr(struct sk_buff *skb, struct dn_ifaddr *ifa,
652			     u32 pid, u32 seq, int event, unsigned int flags)
653{
654	struct ifaddrmsg *ifm;
655	struct nlmsghdr *nlh;
656
657	nlh = nlmsg_put(skb, pid, seq, event, sizeof(*ifm), flags);
658	if (nlh == NULL)
659		return -EMSGSIZE;
660
661	ifm = nlmsg_data(nlh);
662	ifm->ifa_family = AF_DECnet;
663	ifm->ifa_prefixlen = 16;
664	ifm->ifa_flags = ifa->ifa_flags | IFA_F_PERMANENT;
665	ifm->ifa_scope = ifa->ifa_scope;
666	ifm->ifa_index = ifa->ifa_dev->dev->ifindex;
667
668	if (ifa->ifa_address)
669		NLA_PUT_LE16(skb, IFA_ADDRESS, ifa->ifa_address);
670	if (ifa->ifa_local)
671		NLA_PUT_LE16(skb, IFA_LOCAL, ifa->ifa_local);
672	if (ifa->ifa_label[0])
673		NLA_PUT_STRING(skb, IFA_LABEL, ifa->ifa_label);
674
675	return nlmsg_end(skb, nlh);
676
677nla_put_failure:
678	nlmsg_cancel(skb, nlh);
679	return -EMSGSIZE;
680}
681
682static void dn_ifaddr_notify(int event, struct dn_ifaddr *ifa)
683{
684	struct sk_buff *skb;
685	int err = -ENOBUFS;
686
687	skb = alloc_skb(dn_ifaddr_nlmsg_size(), GFP_KERNEL);
688	if (skb == NULL)
689		goto errout;
690
691	err = dn_nl_fill_ifaddr(skb, ifa, 0, 0, event, 0);
692	if (err < 0) {
693		/* -EMSGSIZE implies BUG in dn_ifaddr_nlmsg_size() */
694		WARN_ON(err == -EMSGSIZE);
695		kfree_skb(skb);
696		goto errout;
697	}
698	rtnl_notify(skb, &init_net, 0, RTNLGRP_DECnet_IFADDR, NULL, GFP_KERNEL);
699	return;
700errout:
701	if (err < 0)
702		rtnl_set_sk_err(&init_net, RTNLGRP_DECnet_IFADDR, err);
703}
704
705static int dn_nl_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
706{
707	struct net *net = sock_net(skb->sk);
708	int idx, dn_idx = 0, skip_ndevs, skip_naddr;
709	struct net_device *dev;
710	struct dn_dev *dn_db;
711	struct dn_ifaddr *ifa;
712
713	if (!net_eq(net, &init_net))
714		return 0;
715
716	skip_ndevs = cb->args[0];
717	skip_naddr = cb->args[1];
718
719	idx = 0;
720	for_each_netdev(&init_net, dev) {
721		if (idx < skip_ndevs)
722			goto cont;
723		else if (idx > skip_ndevs) {
724			/* Only skip over addresses for first dev dumped
725			 * in this iteration (idx == skip_ndevs) */
726			skip_naddr = 0;
727		}
728
729		if ((dn_db = dev->dn_ptr) == NULL)
730			goto cont;
731
732		for (ifa = dn_db->ifa_list, dn_idx = 0; ifa;
733		     ifa = ifa->ifa_next, dn_idx++) {
734			if (dn_idx < skip_naddr)
735				continue;
736
737			if (dn_nl_fill_ifaddr(skb, ifa, NETLINK_CB(cb->skb).pid,
738					      cb->nlh->nlmsg_seq, RTM_NEWADDR,
739					      NLM_F_MULTI) < 0)
740				goto done;
741		}
742cont:
743		idx++;
744	}
745done:
746	cb->args[0] = idx;
747	cb->args[1] = dn_idx;
748
749	return skb->len;
750}
751
752static int dn_dev_get_first(struct net_device *dev, __le16 *addr)
753{
754	struct dn_dev *dn_db = (struct dn_dev *)dev->dn_ptr;
755	struct dn_ifaddr *ifa;
756	int rv = -ENODEV;
757
758	if (dn_db == NULL)
759		goto out;
760
761	rtnl_lock();
762	ifa = dn_db->ifa_list;
763	if (ifa != NULL) {
764		*addr = ifa->ifa_local;
765		rv = 0;
766	}
767	rtnl_unlock();
768out:
769	return rv;
770}
771
772/*
773 * Find a default address to bind to.
774 *
775 * This is one of those areas where the initial VMS concepts don't really
776 * map onto the Linux concepts, and since we introduced multiple addresses
777 * per interface we have to cope with slightly odd ways of finding out what
778 * "our address" really is. Mostly it's not a problem; for this we just guess
779 * a sensible default. Eventually the routing code will take care of all the
780 * nasties for us I hope.
781 */
782int dn_dev_bind_default(__le16 *addr)
783{
784	struct net_device *dev;
785	int rv;
786	dev = dn_dev_get_default();
787last_chance:
788	if (dev) {
789		rv = dn_dev_get_first(dev, addr);
790		dev_put(dev);
791		if (rv == 0 || dev == init_net.loopback_dev)
792			return rv;
793	}
794	dev = init_net.loopback_dev;
795	dev_hold(dev);
796	goto last_chance;
797}
798
799static void dn_send_endnode_hello(struct net_device *dev, struct dn_ifaddr *ifa)
800{
801	struct endnode_hello_message *msg;
802	struct sk_buff *skb = NULL;
803	__le16 *pktlen;
804	struct dn_dev *dn_db = (struct dn_dev *)dev->dn_ptr;
805
806	if ((skb = dn_alloc_skb(NULL, sizeof(*msg), GFP_ATOMIC)) == NULL)
807		return;
808
809	skb->dev = dev;
810
811	msg = (struct endnode_hello_message *)skb_put(skb,sizeof(*msg));
812
813	msg->msgflg  = 0x0D;
814	memcpy(msg->tiver, dn_eco_version, 3);
815	dn_dn2eth(msg->id, ifa->ifa_local);
816	msg->iinfo   = DN_RT_INFO_ENDN;
817	msg->blksize = cpu_to_le16(mtu2blksize(dev));
818	msg->area    = 0x00;
819	memset(msg->seed, 0, 8);
820	memcpy(msg->neighbor, dn_hiord, ETH_ALEN);
821
822	if (dn_db->router) {
823		struct dn_neigh *dn = (struct dn_neigh *)dn_db->router;
824		dn_dn2eth(msg->neighbor, dn->addr);
825	}
826
827	msg->timer   = cpu_to_le16((unsigned short)dn_db->parms.t3);
828	msg->mpd     = 0x00;
829	msg->datalen = 0x02;
830	memset(msg->data, 0xAA, 2);
831
832	pktlen = (__le16 *)skb_push(skb,2);
833	*pktlen = cpu_to_le16(skb->len - 2);
834
835	skb_reset_network_header(skb);
836
837	dn_rt_finish_output(skb, dn_rt_all_rt_mcast, msg->id);
838}
839
840
841#define DRDELAY (5 * HZ)
842
843static int dn_am_i_a_router(struct dn_neigh *dn, struct dn_dev *dn_db, struct dn_ifaddr *ifa)
844{
845	/* First check time since device went up */
846	if ((jiffies - dn_db->uptime) < DRDELAY)
847		return 0;
848
849	/* If there is no router, then yes... */
850	if (!dn_db->router)
851		return 1;
852
853	/* otherwise only if we have a higher priority or.. */
854	if (dn->priority < dn_db->parms.priority)
855		return 1;
856
857	/* if we have equal priority and a higher node number */
858	if (dn->priority != dn_db->parms.priority)
859		return 0;
860
861	if (le16_to_cpu(dn->addr) < le16_to_cpu(ifa->ifa_local))
862		return 1;
863
864	return 0;
865}
866
867static void dn_send_router_hello(struct net_device *dev, struct dn_ifaddr *ifa)
868{
869	int n;
870	struct dn_dev *dn_db = dev->dn_ptr;
871	struct dn_neigh *dn = (struct dn_neigh *)dn_db->router;
872	struct sk_buff *skb;
873	size_t size;
874	unsigned char *ptr;
875	unsigned char *i1, *i2;
876	__le16 *pktlen;
877	char *src;
878
879	if (mtu2blksize(dev) < (26 + 7))
880		return;
881
882	n = mtu2blksize(dev) - 26;
883	n /= 7;
884
885	if (n > 32)
886		n = 32;
887
888	size = 2 + 26 + 7 * n;
889
890	if ((skb = dn_alloc_skb(NULL, size, GFP_ATOMIC)) == NULL)
891		return;
892
893	skb->dev = dev;
894	ptr = skb_put(skb, size);
895
896	*ptr++ = DN_RT_PKT_CNTL | DN_RT_PKT_ERTH;
897	*ptr++ = 2; /* ECO */
898	*ptr++ = 0;
899	*ptr++ = 0;
900	dn_dn2eth(ptr, ifa->ifa_local);
901	src = ptr;
902	ptr += ETH_ALEN;
903	*ptr++ = dn_db->parms.forwarding == 1 ?
904			DN_RT_INFO_L1RT : DN_RT_INFO_L2RT;
905	*((__le16 *)ptr) = cpu_to_le16(mtu2blksize(dev));
906	ptr += 2;
907	*ptr++ = dn_db->parms.priority; /* Priority */
908	*ptr++ = 0; /* Area: Reserved */
909	*((__le16 *)ptr) = cpu_to_le16((unsigned short)dn_db->parms.t3);
910	ptr += 2;
911	*ptr++ = 0; /* MPD: Reserved */
912	i1 = ptr++;
913	memset(ptr, 0, 7); /* Name: Reserved */
914	ptr += 7;
915	i2 = ptr++;
916
917	n = dn_neigh_elist(dev, ptr, n);
918
919	*i2 = 7 * n;
920	*i1 = 8 + *i2;
921
922	skb_trim(skb, (27 + *i2));
923
924	pktlen = (__le16 *)skb_push(skb, 2);
925	*pktlen = cpu_to_le16(skb->len - 2);
926
927	skb_reset_network_header(skb);
928
929	if (dn_am_i_a_router(dn, dn_db, ifa)) {
930		struct sk_buff *skb2 = skb_copy(skb, GFP_ATOMIC);
931		if (skb2) {
932			dn_rt_finish_output(skb2, dn_rt_all_end_mcast, src);
933		}
934	}
935
936	dn_rt_finish_output(skb, dn_rt_all_rt_mcast, src);
937}
938
939static void dn_send_brd_hello(struct net_device *dev, struct dn_ifaddr *ifa)
940{
941	struct dn_dev *dn_db = (struct dn_dev *)dev->dn_ptr;
942
943	if (dn_db->parms.forwarding == 0)
944		dn_send_endnode_hello(dev, ifa);
945	else
946		dn_send_router_hello(dev, ifa);
947}
948
949static void dn_send_ptp_hello(struct net_device *dev, struct dn_ifaddr *ifa)
950{
951	int tdlen = 16;
952	int size = dev->hard_header_len + 2 + 4 + tdlen;
953	struct sk_buff *skb = dn_alloc_skb(NULL, size, GFP_ATOMIC);
954	int i;
955	unsigned char *ptr;
956	char src[ETH_ALEN];
957
958	if (skb == NULL)
959		return ;
960
961	skb->dev = dev;
962	skb_push(skb, dev->hard_header_len);
963	ptr = skb_put(skb, 2 + 4 + tdlen);
964
965	*ptr++ = DN_RT_PKT_HELO;
966	*((__le16 *)ptr) = ifa->ifa_local;
967	ptr += 2;
968	*ptr++ = tdlen;
969
970	for(i = 0; i < tdlen; i++)
971		*ptr++ = 0252;
972
973	dn_dn2eth(src, ifa->ifa_local);
974	dn_rt_finish_output(skb, dn_rt_all_rt_mcast, src);
975}
976
977static int dn_eth_up(struct net_device *dev)
978{
979	struct dn_dev *dn_db = dev->dn_ptr;
980
981	if (dn_db->parms.forwarding == 0)
982		dev_mc_add(dev, dn_rt_all_end_mcast);
983	else
984		dev_mc_add(dev, dn_rt_all_rt_mcast);
985
986	dn_db->use_long = 1;
987
988	return 0;
989}
990
991static void dn_eth_down(struct net_device *dev)
992{
993	struct dn_dev *dn_db = dev->dn_ptr;
994
995	if (dn_db->parms.forwarding == 0)
996		dev_mc_del(dev, dn_rt_all_end_mcast);
997	else
998		dev_mc_del(dev, dn_rt_all_rt_mcast);
999}
1000
1001static void dn_dev_set_timer(struct net_device *dev);
1002
1003static void dn_dev_timer_func(unsigned long arg)
1004{
1005	struct net_device *dev = (struct net_device *)arg;
1006	struct dn_dev *dn_db = dev->dn_ptr;
1007	struct dn_ifaddr *ifa;
1008
1009	if (dn_db->t3 <= dn_db->parms.t2) {
1010		if (dn_db->parms.timer3) {
1011			for(ifa = dn_db->ifa_list; ifa; ifa = ifa->ifa_next) {
1012				if (!(ifa->ifa_flags & IFA_F_SECONDARY))
1013					dn_db->parms.timer3(dev, ifa);
1014			}
1015		}
1016		dn_db->t3 = dn_db->parms.t3;
1017	} else {
1018		dn_db->t3 -= dn_db->parms.t2;
1019	}
1020
1021	dn_dev_set_timer(dev);
1022}
1023
1024static void dn_dev_set_timer(struct net_device *dev)
1025{
1026	struct dn_dev *dn_db = dev->dn_ptr;
1027
1028	if (dn_db->parms.t2 > dn_db->parms.t3)
1029		dn_db->parms.t2 = dn_db->parms.t3;
1030
1031	dn_db->timer.data = (unsigned long)dev;
1032	dn_db->timer.function = dn_dev_timer_func;
1033	dn_db->timer.expires = jiffies + (dn_db->parms.t2 * HZ);
1034
1035	add_timer(&dn_db->timer);
1036}
1037
1038static struct dn_dev *dn_dev_create(struct net_device *dev, int *err)
1039{
1040	int i;
1041	struct dn_dev_parms *p = dn_dev_list;
1042	struct dn_dev *dn_db;
1043
1044	for(i = 0; i < DN_DEV_LIST_SIZE; i++, p++) {
1045		if (p->type == dev->type)
1046			break;
1047	}
1048
1049	*err = -ENODEV;
1050	if (i == DN_DEV_LIST_SIZE)
1051		return NULL;
1052
1053	*err = -ENOBUFS;
1054	if ((dn_db = kzalloc(sizeof(struct dn_dev), GFP_ATOMIC)) == NULL)
1055		return NULL;
1056
1057	memcpy(&dn_db->parms, p, sizeof(struct dn_dev_parms));
1058	smp_wmb();
1059	dev->dn_ptr = dn_db;
1060	dn_db->dev = dev;
1061	init_timer(&dn_db->timer);
1062
1063	dn_db->uptime = jiffies;
1064
1065	dn_db->neigh_parms = neigh_parms_alloc(dev, &dn_neigh_table);
1066	if (!dn_db->neigh_parms) {
1067		dev->dn_ptr = NULL;
1068		kfree(dn_db);
1069		return NULL;
1070	}
1071
1072	if (dn_db->parms.up) {
1073		if (dn_db->parms.up(dev) < 0) {
1074			neigh_parms_release(&dn_neigh_table, dn_db->neigh_parms);
1075			dev->dn_ptr = NULL;
1076			kfree(dn_db);
1077			return NULL;
1078		}
1079	}
1080
1081	dn_dev_sysctl_register(dev, &dn_db->parms);
1082
1083	dn_dev_set_timer(dev);
1084
1085	*err = 0;
1086	return dn_db;
1087}
1088
1089
1090
1091void dn_dev_up(struct net_device *dev)
1092{
1093	struct dn_ifaddr *ifa;
1094	__le16 addr = decnet_address;
1095	int maybe_default = 0;
1096	struct dn_dev *dn_db = (struct dn_dev *)dev->dn_ptr;
1097
1098	if ((dev->type != ARPHRD_ETHER) && (dev->type != ARPHRD_LOOPBACK))
1099		return;
1100
1101	/*
1102	 * Need to ensure that loopback device has a dn_db attached to it
1103	 * to allow creation of neighbours against it, even though it might
1104	 * not have a local address of its own. Might as well do the same for
1105	 * all autoconfigured interfaces.
1106	 */
1107	if (dn_db == NULL) {
1108		int err;
1109		dn_db = dn_dev_create(dev, &err);
1110		if (dn_db == NULL)
1111			return;
1112	}
1113
1114	if (dev->type == ARPHRD_ETHER) {
1115		if (memcmp(dev->dev_addr, dn_hiord, 4) != 0)
1116			return;
1117		addr = dn_eth2dn(dev->dev_addr);
1118		maybe_default = 1;
1119	}
1120
1121	if (addr == 0)
1122		return;
1123
1124	if ((ifa = dn_dev_alloc_ifa()) == NULL)
1125		return;
1126
1127	ifa->ifa_local = ifa->ifa_address = addr;
1128	ifa->ifa_flags = 0;
1129	ifa->ifa_scope = RT_SCOPE_UNIVERSE;
1130	strcpy(ifa->ifa_label, dev->name);
1131
1132	dn_dev_set_ifa(dev, ifa);
1133
1134	/*
1135	 * Automagically set the default device to the first automatically
1136	 * configured ethernet card in the system.
1137	 */
1138	if (maybe_default) {
1139		dev_hold(dev);
1140		if (dn_dev_set_default(dev, 0))
1141			dev_put(dev);
1142	}
1143}
1144
1145static void dn_dev_delete(struct net_device *dev)
1146{
1147	struct dn_dev *dn_db = dev->dn_ptr;
1148
1149	if (dn_db == NULL)
1150		return;
1151
1152	del_timer_sync(&dn_db->timer);
1153	dn_dev_sysctl_unregister(&dn_db->parms);
1154	dn_dev_check_default(dev);
1155	neigh_ifdown(&dn_neigh_table, dev);
1156
1157	if (dn_db->parms.down)
1158		dn_db->parms.down(dev);
1159
1160	dev->dn_ptr = NULL;
1161
1162	neigh_parms_release(&dn_neigh_table, dn_db->neigh_parms);
1163	neigh_ifdown(&dn_neigh_table, dev);
1164
1165	if (dn_db->router)
1166		neigh_release(dn_db->router);
1167	if (dn_db->peer)
1168		neigh_release(dn_db->peer);
1169
1170	kfree(dn_db);
1171}
1172
1173void dn_dev_down(struct net_device *dev)
1174{
1175	struct dn_dev *dn_db = dev->dn_ptr;
1176	struct dn_ifaddr *ifa;
1177
1178	if (dn_db == NULL)
1179		return;
1180
1181	while((ifa = dn_db->ifa_list) != NULL) {
1182		dn_dev_del_ifa(dn_db, &dn_db->ifa_list, 0);
1183		dn_dev_free_ifa(ifa);
1184	}
1185
1186	dn_dev_delete(dev);
1187}
1188
1189void dn_dev_init_pkt(struct sk_buff *skb)
1190{
1191}
1192
1193void dn_dev_veri_pkt(struct sk_buff *skb)
1194{
1195}
1196
1197void dn_dev_hello(struct sk_buff *skb)
1198{
1199}
1200
1201void dn_dev_devices_off(void)
1202{
1203	struct net_device *dev;
1204
1205	rtnl_lock();
1206	for_each_netdev(&init_net, dev)
1207		dn_dev_down(dev);
1208	rtnl_unlock();
1209
1210}
1211
1212void dn_dev_devices_on(void)
1213{
1214	struct net_device *dev;
1215
1216	rtnl_lock();
1217	for_each_netdev(&init_net, dev) {
1218		if (dev->flags & IFF_UP)
1219			dn_dev_up(dev);
1220	}
1221	rtnl_unlock();
1222}
1223
1224int register_dnaddr_notifier(struct notifier_block *nb)
1225{
1226	return blocking_notifier_chain_register(&dnaddr_chain, nb);
1227}
1228
1229int unregister_dnaddr_notifier(struct notifier_block *nb)
1230{
1231	return blocking_notifier_chain_unregister(&dnaddr_chain, nb);
1232}
1233
1234#ifdef CONFIG_PROC_FS
1235static inline int is_dn_dev(struct net_device *dev)
1236{
1237	return dev->dn_ptr != NULL;
1238}
1239
1240static void *dn_dev_seq_start(struct seq_file *seq, loff_t *pos)
1241	__acquires(rcu)
1242{
1243	int i;
1244	struct net_device *dev;
1245
1246	rcu_read_lock();
1247
1248	if (*pos == 0)
1249		return SEQ_START_TOKEN;
1250
1251	i = 1;
1252	for_each_netdev_rcu(&init_net, dev) {
1253		if (!is_dn_dev(dev))
1254			continue;
1255
1256		if (i++ == *pos)
1257			return dev;
1258	}
1259
1260	return NULL;
1261}
1262
1263static void *dn_dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1264{
1265	struct net_device *dev;
1266
1267	++*pos;
1268
1269	dev = (struct net_device *)v;
1270	if (v == SEQ_START_TOKEN)
1271		dev = net_device_entry(&init_net.dev_base_head);
1272
1273	for_each_netdev_continue_rcu(&init_net, dev) {
1274		if (!is_dn_dev(dev))
1275			continue;
1276
1277		return dev;
1278	}
1279
1280	return NULL;
1281}
1282
1283static void dn_dev_seq_stop(struct seq_file *seq, void *v)
1284	__releases(rcu)
1285{
1286	rcu_read_unlock();
1287}
1288
1289static char *dn_type2asc(char type)
1290{
1291	switch(type) {
1292		case DN_DEV_BCAST:
1293			return "B";
1294		case DN_DEV_UCAST:
1295			return "U";
1296		case DN_DEV_MPOINT:
1297			return "M";
1298	}
1299
1300	return "?";
1301}
1302
1303static int dn_dev_seq_show(struct seq_file *seq, void *v)
1304{
1305	if (v == SEQ_START_TOKEN)
1306		seq_puts(seq, "Name     Flags T1   Timer1 T3   Timer3 BlkSize Pri State DevType    Router Peer\n");
1307	else {
1308		struct net_device *dev = v;
1309		char peer_buf[DN_ASCBUF_LEN];
1310		char router_buf[DN_ASCBUF_LEN];
1311		struct dn_dev *dn_db = dev->dn_ptr;
1312
1313		seq_printf(seq, "%-8s %1s     %04u %04u   %04lu %04lu"
1314				"   %04hu    %03d %02x    %-10s %-7s %-7s\n",
1315				dev->name ? dev->name : "???",
1316				dn_type2asc(dn_db->parms.mode),
1317				0, 0,
1318				dn_db->t3, dn_db->parms.t3,
1319				mtu2blksize(dev),
1320				dn_db->parms.priority,
1321				dn_db->parms.state, dn_db->parms.name,
1322				dn_db->router ? dn_addr2asc(le16_to_cpu(*(__le16 *)dn_db->router->primary_key), router_buf) : "",
1323				dn_db->peer ? dn_addr2asc(le16_to_cpu(*(__le16 *)dn_db->peer->primary_key), peer_buf) : "");
1324	}
1325	return 0;
1326}
1327
1328static const struct seq_operations dn_dev_seq_ops = {
1329	.start	= dn_dev_seq_start,
1330	.next	= dn_dev_seq_next,
1331	.stop	= dn_dev_seq_stop,
1332	.show	= dn_dev_seq_show,
1333};
1334
1335static int dn_dev_seq_open(struct inode *inode, struct file *file)
1336{
1337	return seq_open(file, &dn_dev_seq_ops);
1338}
1339
1340static const struct file_operations dn_dev_seq_fops = {
1341	.owner	 = THIS_MODULE,
1342	.open	 = dn_dev_seq_open,
1343	.read	 = seq_read,
1344	.llseek	 = seq_lseek,
1345	.release = seq_release,
1346};
1347
1348#endif /* CONFIG_PROC_FS */
1349
1350static int addr[2];
1351module_param_array(addr, int, NULL, 0444);
1352MODULE_PARM_DESC(addr, "The DECnet address of this machine: area,node");
1353
1354void __init dn_dev_init(void)
1355{
1356	if (addr[0] > 63 || addr[0] < 0) {
1357		printk(KERN_ERR "DECnet: Area must be between 0 and 63");
1358		return;
1359	}
1360
1361	if (addr[1] > 1023 || addr[1] < 0) {
1362		printk(KERN_ERR "DECnet: Node must be between 0 and 1023");
1363		return;
1364	}
1365
1366	decnet_address = cpu_to_le16((addr[0] << 10) | addr[1]);
1367
1368	dn_dev_devices_on();
1369
1370	rtnl_register(PF_DECnet, RTM_NEWADDR, dn_nl_newaddr, NULL);
1371	rtnl_register(PF_DECnet, RTM_DELADDR, dn_nl_deladdr, NULL);
1372	rtnl_register(PF_DECnet, RTM_GETADDR, NULL, dn_nl_dump_ifaddr);
1373
1374	proc_net_fops_create(&init_net, "decnet_dev", S_IRUGO, &dn_dev_seq_fops);
1375
1376#ifdef CONFIG_SYSCTL
1377	{
1378		int i;
1379		for(i = 0; i < DN_DEV_LIST_SIZE; i++)
1380			dn_dev_sysctl_register(NULL, &dn_dev_list[i]);
1381	}
1382#endif /* CONFIG_SYSCTL */
1383}
1384
1385void __exit dn_dev_cleanup(void)
1386{
1387#ifdef CONFIG_SYSCTL
1388	{
1389		int i;
1390		for(i = 0; i < DN_DEV_LIST_SIZE; i++)
1391			dn_dev_sysctl_unregister(&dn_dev_list[i]);
1392	}
1393#endif /* CONFIG_SYSCTL */
1394
1395	proc_net_remove(&init_net, "decnet_dev");
1396
1397	dn_dev_devices_off();
1398}
1399