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 Neighbour Functions (Adjacency Database and
7 *                                                        On-Ethernet Cache)
8 *
9 * Author:      Steve Whitehouse <SteveW@ACM.org>
10 *
11 *
12 * Changes:
13 *     Steve Whitehouse     : Fixed router listing routine
14 *     Steve Whitehouse     : Added error_report functions
15 *     Steve Whitehouse     : Added default router detection
16 *     Steve Whitehouse     : Hop counts in outgoing messages
17 *     Steve Whitehouse     : Fixed src/dst in outgoing messages so
18 *                            forwarding now stands a good chance of
19 *                            working.
20 *     Steve Whitehouse     : Fixed neighbour states (for now anyway).
21 *     Steve Whitehouse     : Made error_report functions dummies. This
22 *                            is not the right place to return skbs.
23 *     Steve Whitehouse     : Convert to seq_file
24 *
25 */
26
27#include <linux/net.h>
28#include <linux/module.h>
29#include <linux/socket.h>
30#include <linux/if_arp.h>
31#include <linux/if_ether.h>
32#include <linux/init.h>
33#include <linux/proc_fs.h>
34#include <linux/string.h>
35#include <linux/netfilter_decnet.h>
36#include <linux/spinlock.h>
37#include <linux/seq_file.h>
38#include <linux/rcupdate.h>
39#include <linux/jhash.h>
40#include <asm/atomic.h>
41#include <net/neighbour.h>
42#include <net/dst.h>
43#include <net/flow.h>
44#include <net/dn.h>
45#include <net/dn_dev.h>
46#include <net/dn_neigh.h>
47#include <net/dn_route.h>
48
49static u32 dn_neigh_hash(const void *pkey, const struct net_device *dev);
50static int dn_neigh_construct(struct neighbour *);
51static void dn_long_error_report(struct neighbour *, struct sk_buff *);
52static void dn_short_error_report(struct neighbour *, struct sk_buff *);
53static int dn_long_output(struct sk_buff *);
54static int dn_short_output(struct sk_buff *);
55static int dn_phase3_output(struct sk_buff *);
56
57
58/*
59 * For talking to broadcast devices: Ethernet & PPP
60 */
61static struct neigh_ops dn_long_ops = {
62	.family =		AF_DECnet,
63	.error_report =		dn_long_error_report,
64	.output =		dn_long_output,
65	.connected_output =	dn_long_output,
66	.hh_output =		dev_queue_xmit,
67	.queue_xmit =		dev_queue_xmit,
68};
69
70/*
71 * For talking to pointopoint and multidrop devices: DDCMP and X.25
72 */
73static struct neigh_ops dn_short_ops = {
74	.family =		AF_DECnet,
75	.error_report =		dn_short_error_report,
76	.output =		dn_short_output,
77	.connected_output =	dn_short_output,
78	.hh_output =		dev_queue_xmit,
79	.queue_xmit =		dev_queue_xmit,
80};
81
82/*
83 * For talking to DECnet phase III nodes
84 */
85static struct neigh_ops dn_phase3_ops = {
86	.family =		AF_DECnet,
87	.error_report =		dn_short_error_report, /* Can use short version here */
88	.output =		dn_phase3_output,
89	.connected_output =	dn_phase3_output,
90	.hh_output =		dev_queue_xmit,
91	.queue_xmit =		dev_queue_xmit
92};
93
94struct neigh_table dn_neigh_table = {
95	.family =			PF_DECnet,
96	.entry_size =			sizeof(struct dn_neigh),
97	.key_len =			sizeof(__le16),
98	.hash =				dn_neigh_hash,
99	.constructor =			dn_neigh_construct,
100	.id =				"dn_neigh_cache",
101	.parms ={
102		.tbl =			&dn_neigh_table,
103		.base_reachable_time =	30 * HZ,
104		.retrans_time =	1 * HZ,
105		.gc_staletime =	60 * HZ,
106		.reachable_time =		30 * HZ,
107		.delay_probe_time =	5 * HZ,
108		.queue_len =		3,
109		.ucast_probes =	0,
110		.app_probes =		0,
111		.mcast_probes =	0,
112		.anycast_delay =	0,
113		.proxy_delay =		0,
114		.proxy_qlen =		0,
115		.locktime =		1 * HZ,
116	},
117	.gc_interval =			30 * HZ,
118	.gc_thresh1 =			128,
119	.gc_thresh2 =			512,
120	.gc_thresh3 =			1024,
121};
122
123static u32 dn_neigh_hash(const void *pkey, const struct net_device *dev)
124{
125	return jhash_2words(*(__u16 *)pkey, 0, dn_neigh_table.hash_rnd);
126}
127
128static int dn_neigh_construct(struct neighbour *neigh)
129{
130	struct net_device *dev = neigh->dev;
131	struct dn_neigh *dn = (struct dn_neigh *)neigh;
132	struct dn_dev *dn_db;
133	struct neigh_parms *parms;
134
135	rcu_read_lock();
136	dn_db = rcu_dereference(dev->dn_ptr);
137	if (dn_db == NULL) {
138		rcu_read_unlock();
139		return -EINVAL;
140	}
141
142	parms = dn_db->neigh_parms;
143	if (!parms) {
144		rcu_read_unlock();
145		return -EINVAL;
146	}
147
148	__neigh_parms_put(neigh->parms);
149	neigh->parms = neigh_parms_clone(parms);
150
151	if (dn_db->use_long)
152		neigh->ops = &dn_long_ops;
153	else
154		neigh->ops = &dn_short_ops;
155	rcu_read_unlock();
156
157	if (dn->flags & DN_NDFLAG_P3)
158		neigh->ops = &dn_phase3_ops;
159
160	neigh->nud_state = NUD_NOARP;
161	neigh->output = neigh->ops->connected_output;
162
163	if ((dev->type == ARPHRD_IPGRE) || (dev->flags & IFF_POINTOPOINT))
164		memcpy(neigh->ha, dev->broadcast, dev->addr_len);
165	else if ((dev->type == ARPHRD_ETHER) || (dev->type == ARPHRD_LOOPBACK))
166		dn_dn2eth(neigh->ha, dn->addr);
167	else {
168		if (net_ratelimit())
169			printk(KERN_DEBUG "Trying to create neigh for hw %d\n",  dev->type);
170		return -EINVAL;
171	}
172
173	/*
174	 * Make an estimate of the remote block size by assuming that its
175	 * two less then the device mtu, which it true for ethernet (and
176	 * other things which support long format headers) since there is
177	 * an extra length field (of 16 bits) which isn't part of the
178	 * ethernet headers and which the DECnet specs won't admit is part
179	 * of the DECnet routing headers either.
180	 *
181	 * If we over estimate here its no big deal, the NSP negotiations
182	 * will prevent us from sending packets which are too large for the
183	 * remote node to handle. In any case this figure is normally updated
184	 * by a hello message in most cases.
185	 */
186	dn->blksize = dev->mtu - 2;
187
188	return 0;
189}
190
191static void dn_long_error_report(struct neighbour *neigh, struct sk_buff *skb)
192{
193	printk(KERN_DEBUG "dn_long_error_report: called\n");
194	kfree_skb(skb);
195}
196
197
198static void dn_short_error_report(struct neighbour *neigh, struct sk_buff *skb)
199{
200	printk(KERN_DEBUG "dn_short_error_report: called\n");
201	kfree_skb(skb);
202}
203
204static int dn_neigh_output_packet(struct sk_buff *skb)
205{
206	struct dst_entry *dst = skb->dst;
207	struct dn_route *rt = (struct dn_route *)dst;
208	struct neighbour *neigh = dst->neighbour;
209	struct net_device *dev = neigh->dev;
210	char mac_addr[ETH_ALEN];
211
212	dn_dn2eth(mac_addr, rt->rt_local_src);
213	if (!dev->hard_header || dev->hard_header(skb, dev, ntohs(skb->protocol), neigh->ha, mac_addr, skb->len) >= 0)
214		return neigh->ops->queue_xmit(skb);
215
216	if (net_ratelimit())
217		printk(KERN_DEBUG "dn_neigh_output_packet: oops, can't send packet\n");
218
219	kfree_skb(skb);
220	return -EINVAL;
221}
222
223static int dn_long_output(struct sk_buff *skb)
224{
225	struct dst_entry *dst = skb->dst;
226	struct neighbour *neigh = dst->neighbour;
227	struct net_device *dev = neigh->dev;
228	int headroom = dev->hard_header_len + sizeof(struct dn_long_packet) + 3;
229	unsigned char *data;
230	struct dn_long_packet *lp;
231	struct dn_skb_cb *cb = DN_SKB_CB(skb);
232
233
234	if (skb_headroom(skb) < headroom) {
235		struct sk_buff *skb2 = skb_realloc_headroom(skb, headroom);
236		if (skb2 == NULL) {
237			if (net_ratelimit())
238				printk(KERN_CRIT "dn_long_output: no memory\n");
239			kfree_skb(skb);
240			return -ENOBUFS;
241		}
242		kfree_skb(skb);
243		skb = skb2;
244		if (net_ratelimit())
245			printk(KERN_INFO "dn_long_output: Increasing headroom\n");
246	}
247
248	data = skb_push(skb, sizeof(struct dn_long_packet) + 3);
249	lp = (struct dn_long_packet *)(data+3);
250
251	*((__le16 *)data) = dn_htons(skb->len - 2);
252	*(data + 2) = 1 | DN_RT_F_PF; /* Padding */
253
254	lp->msgflg   = DN_RT_PKT_LONG|(cb->rt_flags&(DN_RT_F_IE|DN_RT_F_RQR|DN_RT_F_RTS));
255	lp->d_area   = lp->d_subarea = 0;
256	dn_dn2eth(lp->d_id, cb->dst);
257	lp->s_area   = lp->s_subarea = 0;
258	dn_dn2eth(lp->s_id, cb->src);
259	lp->nl2      = 0;
260	lp->visit_ct = cb->hops & 0x3f;
261	lp->s_class  = 0;
262	lp->pt       = 0;
263
264	skb_reset_network_header(skb);
265
266	return NF_HOOK(PF_DECnet, NF_DN_POST_ROUTING, skb, NULL, neigh->dev, dn_neigh_output_packet);
267}
268
269static int dn_short_output(struct sk_buff *skb)
270{
271	struct dst_entry *dst = skb->dst;
272	struct neighbour *neigh = dst->neighbour;
273	struct net_device *dev = neigh->dev;
274	int headroom = dev->hard_header_len + sizeof(struct dn_short_packet) + 2;
275	struct dn_short_packet *sp;
276	unsigned char *data;
277	struct dn_skb_cb *cb = DN_SKB_CB(skb);
278
279
280	if (skb_headroom(skb) < headroom) {
281		struct sk_buff *skb2 = skb_realloc_headroom(skb, headroom);
282		if (skb2 == NULL) {
283			if (net_ratelimit())
284				printk(KERN_CRIT "dn_short_output: no memory\n");
285			kfree_skb(skb);
286			return -ENOBUFS;
287		}
288		kfree_skb(skb);
289		skb = skb2;
290		if (net_ratelimit())
291			printk(KERN_INFO "dn_short_output: Increasing headroom\n");
292	}
293
294	data = skb_push(skb, sizeof(struct dn_short_packet) + 2);
295	*((__le16 *)data) = dn_htons(skb->len - 2);
296	sp = (struct dn_short_packet *)(data+2);
297
298	sp->msgflg     = DN_RT_PKT_SHORT|(cb->rt_flags&(DN_RT_F_RQR|DN_RT_F_RTS));
299	sp->dstnode    = cb->dst;
300	sp->srcnode    = cb->src;
301	sp->forward    = cb->hops & 0x3f;
302
303	skb_reset_network_header(skb);
304
305	return NF_HOOK(PF_DECnet, NF_DN_POST_ROUTING, skb, NULL, neigh->dev, dn_neigh_output_packet);
306}
307
308/*
309 * Phase 3 output is the same is short output, execpt that
310 * it clears the area bits before transmission.
311 */
312static int dn_phase3_output(struct sk_buff *skb)
313{
314	struct dst_entry *dst = skb->dst;
315	struct neighbour *neigh = dst->neighbour;
316	struct net_device *dev = neigh->dev;
317	int headroom = dev->hard_header_len + sizeof(struct dn_short_packet) + 2;
318	struct dn_short_packet *sp;
319	unsigned char *data;
320	struct dn_skb_cb *cb = DN_SKB_CB(skb);
321
322	if (skb_headroom(skb) < headroom) {
323		struct sk_buff *skb2 = skb_realloc_headroom(skb, headroom);
324		if (skb2 == NULL) {
325			if (net_ratelimit())
326				printk(KERN_CRIT "dn_phase3_output: no memory\n");
327			kfree_skb(skb);
328			return -ENOBUFS;
329		}
330		kfree_skb(skb);
331		skb = skb2;
332		if (net_ratelimit())
333			printk(KERN_INFO "dn_phase3_output: Increasing headroom\n");
334	}
335
336	data = skb_push(skb, sizeof(struct dn_short_packet) + 2);
337	*((__le16 *)data) = dn_htons(skb->len - 2);
338	sp = (struct dn_short_packet *)(data + 2);
339
340	sp->msgflg   = DN_RT_PKT_SHORT|(cb->rt_flags&(DN_RT_F_RQR|DN_RT_F_RTS));
341	sp->dstnode  = cb->dst & dn_htons(0x03ff);
342	sp->srcnode  = cb->src & dn_htons(0x03ff);
343	sp->forward  = cb->hops & 0x3f;
344
345	skb_reset_network_header(skb);
346
347	return NF_HOOK(PF_DECnet, NF_DN_POST_ROUTING, skb, NULL, neigh->dev, dn_neigh_output_packet);
348}
349
350/*
351 * Unfortunately, the neighbour code uses the device in its hash
352 * function, so we don't get any advantage from it. This function
353 * basically does a neigh_lookup(), but without comparing the device
354 * field. This is required for the On-Ethernet cache
355 */
356
357/*
358 * Pointopoint link receives a hello message
359 */
360void dn_neigh_pointopoint_hello(struct sk_buff *skb)
361{
362	kfree_skb(skb);
363}
364
365/*
366 * Ethernet router hello message received
367 */
368int dn_neigh_router_hello(struct sk_buff *skb)
369{
370	struct rtnode_hello_message *msg = (struct rtnode_hello_message *)skb->data;
371
372	struct neighbour *neigh;
373	struct dn_neigh *dn;
374	struct dn_dev *dn_db;
375	__le16 src;
376
377	src = dn_eth2dn(msg->id);
378
379	neigh = __neigh_lookup(&dn_neigh_table, &src, skb->dev, 1);
380
381	dn = (struct dn_neigh *)neigh;
382
383	if (neigh) {
384		write_lock(&neigh->lock);
385
386		neigh->used = jiffies;
387		dn_db = (struct dn_dev *)neigh->dev->dn_ptr;
388
389		if (!(neigh->nud_state & NUD_PERMANENT)) {
390			neigh->updated = jiffies;
391
392			if (neigh->dev->type == ARPHRD_ETHER)
393				memcpy(neigh->ha, &eth_hdr(skb)->h_source, ETH_ALEN);
394
395			dn->blksize  = dn_ntohs(msg->blksize);
396			dn->priority = msg->priority;
397
398			dn->flags &= ~DN_NDFLAG_P3;
399
400			switch(msg->iinfo & DN_RT_INFO_TYPE) {
401				case DN_RT_INFO_L1RT:
402					dn->flags &=~DN_NDFLAG_R2;
403					dn->flags |= DN_NDFLAG_R1;
404					break;
405				case DN_RT_INFO_L2RT:
406					dn->flags |= DN_NDFLAG_R2;
407			}
408		}
409
410		/* Only use routers in our area */
411		if ((dn_ntohs(src)>>10) == (dn_ntohs((decnet_address))>>10)) {
412			if (!dn_db->router) {
413				dn_db->router = neigh_clone(neigh);
414			} else {
415				if (msg->priority > ((struct dn_neigh *)dn_db->router)->priority)
416					neigh_release(xchg(&dn_db->router, neigh_clone(neigh)));
417			}
418		}
419		write_unlock(&neigh->lock);
420		neigh_release(neigh);
421	}
422
423	kfree_skb(skb);
424	return 0;
425}
426
427/*
428 * Endnode hello message received
429 */
430int dn_neigh_endnode_hello(struct sk_buff *skb)
431{
432	struct endnode_hello_message *msg = (struct endnode_hello_message *)skb->data;
433	struct neighbour *neigh;
434	struct dn_neigh *dn;
435	__le16 src;
436
437	src = dn_eth2dn(msg->id);
438
439	neigh = __neigh_lookup(&dn_neigh_table, &src, skb->dev, 1);
440
441	dn = (struct dn_neigh *)neigh;
442
443	if (neigh) {
444		write_lock(&neigh->lock);
445
446		neigh->used = jiffies;
447
448		if (!(neigh->nud_state & NUD_PERMANENT)) {
449			neigh->updated = jiffies;
450
451			if (neigh->dev->type == ARPHRD_ETHER)
452				memcpy(neigh->ha, &eth_hdr(skb)->h_source, ETH_ALEN);
453			dn->flags   &= ~(DN_NDFLAG_R1 | DN_NDFLAG_R2);
454			dn->blksize  = dn_ntohs(msg->blksize);
455			dn->priority = 0;
456		}
457
458		write_unlock(&neigh->lock);
459		neigh_release(neigh);
460	}
461
462	kfree_skb(skb);
463	return 0;
464}
465
466static char *dn_find_slot(char *base, int max, int priority)
467{
468	int i;
469	unsigned char *min = NULL;
470
471	base += 6; /* skip first id */
472
473	for(i = 0; i < max; i++) {
474		if (!min || (*base < *min))
475			min = base;
476		base += 7; /* find next priority */
477	}
478
479	if (!min)
480		return NULL;
481
482	return (*min < priority) ? (min - 6) : NULL;
483}
484
485struct elist_cb_state {
486	struct net_device *dev;
487	unsigned char *ptr;
488	unsigned char *rs;
489	int t, n;
490};
491
492static void neigh_elist_cb(struct neighbour *neigh, void *_info)
493{
494	struct elist_cb_state *s = _info;
495	struct dn_neigh *dn;
496
497	if (neigh->dev != s->dev)
498		return;
499
500	dn = (struct dn_neigh *) neigh;
501	if (!(dn->flags & (DN_NDFLAG_R1|DN_NDFLAG_R2)))
502		return;
503
504	if (s->t == s->n)
505		s->rs = dn_find_slot(s->ptr, s->n, dn->priority);
506	else
507		s->t++;
508	if (s->rs == NULL)
509		return;
510
511	dn_dn2eth(s->rs, dn->addr);
512	s->rs += 6;
513	*(s->rs) = neigh->nud_state & NUD_CONNECTED ? 0x80 : 0x0;
514	*(s->rs) |= dn->priority;
515	s->rs++;
516}
517
518int dn_neigh_elist(struct net_device *dev, unsigned char *ptr, int n)
519{
520	struct elist_cb_state state;
521
522	state.dev = dev;
523	state.t = 0;
524	state.n = n;
525	state.ptr = ptr;
526	state.rs = ptr;
527
528	neigh_for_each(&dn_neigh_table, neigh_elist_cb, &state);
529
530	return state.t;
531}
532
533
534#ifdef CONFIG_PROC_FS
535
536static inline void dn_neigh_format_entry(struct seq_file *seq,
537					 struct neighbour *n)
538{
539	struct dn_neigh *dn = (struct dn_neigh *) n;
540	char buf[DN_ASCBUF_LEN];
541
542	read_lock(&n->lock);
543	seq_printf(seq, "%-7s %s%s%s   %02x    %02d  %07ld %-8s\n",
544		   dn_addr2asc(dn_ntohs(dn->addr), buf),
545		   (dn->flags&DN_NDFLAG_R1) ? "1" : "-",
546		   (dn->flags&DN_NDFLAG_R2) ? "2" : "-",
547		   (dn->flags&DN_NDFLAG_P3) ? "3" : "-",
548		   dn->n.nud_state,
549		   atomic_read(&dn->n.refcnt),
550		   dn->blksize,
551		   (dn->n.dev) ? dn->n.dev->name : "?");
552	read_unlock(&n->lock);
553}
554
555static int dn_neigh_seq_show(struct seq_file *seq, void *v)
556{
557	if (v == SEQ_START_TOKEN) {
558		seq_puts(seq, "Addr    Flags State Use Blksize Dev\n");
559	} else {
560		dn_neigh_format_entry(seq, v);
561	}
562
563	return 0;
564}
565
566static void *dn_neigh_seq_start(struct seq_file *seq, loff_t *pos)
567{
568	return neigh_seq_start(seq, pos, &dn_neigh_table,
569			       NEIGH_SEQ_NEIGH_ONLY);
570}
571
572static struct seq_operations dn_neigh_seq_ops = {
573	.start = dn_neigh_seq_start,
574	.next  = neigh_seq_next,
575	.stop  = neigh_seq_stop,
576	.show  = dn_neigh_seq_show,
577};
578
579static int dn_neigh_seq_open(struct inode *inode, struct file *file)
580{
581	struct seq_file *seq;
582	int rc = -ENOMEM;
583	struct neigh_seq_state *s = kzalloc(sizeof(*s), GFP_KERNEL);
584
585	if (!s)
586		goto out;
587
588	rc = seq_open(file, &dn_neigh_seq_ops);
589	if (rc)
590		goto out_kfree;
591
592	seq          = file->private_data;
593	seq->private = s;
594out:
595	return rc;
596out_kfree:
597	kfree(s);
598	goto out;
599}
600
601static const struct file_operations dn_neigh_seq_fops = {
602	.owner		= THIS_MODULE,
603	.open		= dn_neigh_seq_open,
604	.read		= seq_read,
605	.llseek		= seq_lseek,
606	.release	= seq_release_private,
607};
608
609#endif
610
611void __init dn_neigh_init(void)
612{
613	neigh_table_init(&dn_neigh_table);
614	proc_net_fops_create("decnet_neigh", S_IRUGO, &dn_neigh_seq_fops);
615}
616
617void __exit dn_neigh_cleanup(void)
618{
619	proc_net_remove("decnet_neigh");
620	neigh_table_clear(&dn_neigh_table);
621}
622