1/*
2 * NET3:	Token ring device handling subroutines
3 *
4 *		This program is free software; you can redistribute it and/or
5 *		modify it under the terms of the GNU General Public License
6 *		as published by the Free Software Foundation; either version
7 *		2 of the License, or (at your option) any later version.
8 *
9 * Fixes:       3 Feb 97 Paul Norton <pnorton@cts.com> Minor routing fixes.
10 *              Added rif table to /proc/net/tr_rif and rif timeout to
11 *              /proc/sys/net/token-ring/rif_timeout.
12 *              22 Jun 98 Paul Norton <p.norton@computer.org> Rearranged
13 *              tr_header and tr_type_trans to handle passing IPX SNAP and
14 *              802.2 through the correct layers. Eliminated tr_reformat.
15 *
16 */
17
18#include <asm/uaccess.h>
19#include <asm/system.h>
20#include <linux/module.h>
21#include <linux/types.h>
22#include <linux/kernel.h>
23#include <linux/jiffies.h>
24#include <linux/string.h>
25#include <linux/mm.h>
26#include <linux/socket.h>
27#include <linux/in.h>
28#include <linux/inet.h>
29#include <linux/netdevice.h>
30#include <linux/trdevice.h>
31#include <linux/skbuff.h>
32#include <linux/errno.h>
33#include <linux/timer.h>
34#include <linux/net.h>
35#include <linux/proc_fs.h>
36#include <linux/seq_file.h>
37#include <linux/init.h>
38#include <net/arp.h>
39
40static void tr_add_rif_info(struct trh_hdr *trh, struct net_device *dev);
41static void rif_check_expire(unsigned long dummy);
42
43#define TR_SR_DEBUG 0
44
45/*
46 *	Each RIF entry we learn is kept this way
47 */
48
49struct rif_cache {
50	unsigned char addr[TR_ALEN];
51	int iface;
52	__be16 rcf;
53	__be16 rseg[8];
54	struct rif_cache *next;
55	unsigned long last_used;
56	unsigned char local_ring;
57};
58
59#define RIF_TABLE_SIZE 32
60
61/*
62 *	We hash the RIF cache 32 ways. We do after all have to look it
63 *	up a lot.
64 */
65
66static struct rif_cache *rif_table[RIF_TABLE_SIZE];
67
68static DEFINE_SPINLOCK(rif_lock);
69
70
71/*
72 *	Garbage disposal timer.
73 */
74
75static struct timer_list rif_timer;
76
77int sysctl_tr_rif_timeout = 60*10*HZ;
78
79static inline unsigned long rif_hash(const unsigned char *addr)
80{
81	unsigned long x;
82
83	x = addr[0];
84	x = (x << 2) ^ addr[1];
85	x = (x << 2) ^ addr[2];
86	x = (x << 2) ^ addr[3];
87	x = (x << 2) ^ addr[4];
88	x = (x << 2) ^ addr[5];
89
90	x ^= x >> 8;
91
92	return x & (RIF_TABLE_SIZE - 1);
93}
94
95/*
96 *	Put the headers on a token ring packet. Token ring source routing
97 *	makes this a little more exciting than on ethernet.
98 */
99
100static int tr_header(struct sk_buff *skb, struct net_device *dev,
101		     unsigned short type,
102		     void *daddr, void *saddr, unsigned len)
103{
104	struct trh_hdr *trh;
105	int hdr_len;
106
107	/*
108	 * Add the 802.2 SNAP header if IP as the IPv4/IPv6 code calls
109	 * dev->hard_header directly.
110	 */
111	if (type == ETH_P_IP || type == ETH_P_IPV6 || type == ETH_P_ARP)
112	{
113		struct trllc *trllc;
114
115		hdr_len = sizeof(struct trh_hdr) + sizeof(struct trllc);
116		trh = (struct trh_hdr *)skb_push(skb, hdr_len);
117		trllc = (struct trllc *)(trh+1);
118		trllc->dsap = trllc->ssap = EXTENDED_SAP;
119		trllc->llc = UI_CMD;
120		trllc->protid[0] = trllc->protid[1] = trllc->protid[2] = 0x00;
121		trllc->ethertype = htons(type);
122	}
123	else
124	{
125		hdr_len = sizeof(struct trh_hdr);
126		trh = (struct trh_hdr *)skb_push(skb, hdr_len);
127	}
128
129	trh->ac=AC;
130	trh->fc=LLC_FRAME;
131
132	if(saddr)
133		memcpy(trh->saddr,saddr,dev->addr_len);
134	else
135		memcpy(trh->saddr,dev->dev_addr,dev->addr_len);
136
137	/*
138	 *	Build the destination and then source route the frame
139	 */
140
141	if(daddr)
142	{
143		memcpy(trh->daddr,daddr,dev->addr_len);
144		tr_source_route(skb,trh,dev);
145		return(hdr_len);
146	}
147
148	return -hdr_len;
149}
150
151/*
152 *	A neighbour discovery of some species (eg arp) has completed. We
153 *	can now send the packet.
154 */
155
156static int tr_rebuild_header(struct sk_buff *skb)
157{
158	struct trh_hdr *trh=(struct trh_hdr *)skb->data;
159	struct trllc *trllc=(struct trllc *)(skb->data+sizeof(struct trh_hdr));
160	struct net_device *dev = skb->dev;
161
162
163	if(trllc->ethertype != htons(ETH_P_IP)) {
164		printk("tr_rebuild_header: Don't know how to resolve type %04X addresses ?\n", ntohs(trllc->ethertype));
165		return 0;
166	}
167
168#ifdef CONFIG_INET
169	if(arp_find(trh->daddr, skb)) {
170			return 1;
171	}
172	else
173#endif
174	{
175		tr_source_route(skb,trh,dev);
176		return 0;
177	}
178}
179
180/*
181 *	Some of this is a bit hackish. We intercept RIF information
182 *	used for source routing. We also grab IP directly and don't feed
183 *	it via SNAP.
184 */
185
186__be16 tr_type_trans(struct sk_buff *skb, struct net_device *dev)
187{
188
189	struct trh_hdr *trh;
190	struct trllc *trllc;
191	unsigned riflen=0;
192
193	skb->dev = dev;
194	skb_reset_mac_header(skb);
195	trh = tr_hdr(skb);
196
197	if(trh->saddr[0] & TR_RII)
198		riflen = (ntohs(trh->rcf) & TR_RCF_LEN_MASK) >> 8;
199
200	trllc = (struct trllc *)(skb->data+sizeof(struct trh_hdr)-TR_MAXRIFLEN+riflen);
201
202	skb_pull(skb,sizeof(struct trh_hdr)-TR_MAXRIFLEN+riflen);
203
204	if(*trh->daddr & 0x80)
205	{
206		if(!memcmp(trh->daddr,dev->broadcast,TR_ALEN))
207			skb->pkt_type=PACKET_BROADCAST;
208		else
209			skb->pkt_type=PACKET_MULTICAST;
210	}
211	else if ( (trh->daddr[0] & 0x01) && (trh->daddr[1] & 0x00) && (trh->daddr[2] & 0x5E))
212	{
213		skb->pkt_type=PACKET_MULTICAST;
214	}
215	else if(dev->flags & IFF_PROMISC)
216	{
217		if(memcmp(trh->daddr, dev->dev_addr, TR_ALEN))
218			skb->pkt_type=PACKET_OTHERHOST;
219	}
220
221	if ((skb->pkt_type != PACKET_BROADCAST) &&
222	    (skb->pkt_type != PACKET_MULTICAST))
223		tr_add_rif_info(trh,dev) ;
224
225	/*
226	 * Strip the SNAP header from ARP packets since we don't
227	 * pass them through to the 802.2/SNAP layers.
228	 */
229
230	if (trllc->dsap == EXTENDED_SAP &&
231	    (trllc->ethertype == htons(ETH_P_IP) ||
232	     trllc->ethertype == htons(ETH_P_IPV6) ||
233	     trllc->ethertype == htons(ETH_P_ARP)))
234	{
235		skb_pull(skb, sizeof(struct trllc));
236		return trllc->ethertype;
237	}
238
239	return htons(ETH_P_TR_802_2);
240}
241
242/*
243 *	We try to do source routing...
244 */
245
246void tr_source_route(struct sk_buff *skb,struct trh_hdr *trh,struct net_device *dev)
247{
248	int slack;
249	unsigned int hash;
250	struct rif_cache *entry;
251	unsigned char *olddata;
252	unsigned long flags;
253	static const unsigned char mcast_func_addr[]
254		= {0xC0,0x00,0x00,0x04,0x00,0x00};
255
256	spin_lock_irqsave(&rif_lock, flags);
257
258	/*
259	 *	Broadcasts are single route as stated in RFC 1042
260	 */
261	if( (!memcmp(&(trh->daddr[0]),&(dev->broadcast[0]),TR_ALEN)) ||
262	    (!memcmp(&(trh->daddr[0]),&(mcast_func_addr[0]), TR_ALEN))  )
263	{
264		trh->rcf=htons((((sizeof(trh->rcf)) << 8) & TR_RCF_LEN_MASK)
265			       | TR_RCF_FRAME2K | TR_RCF_LIMITED_BROADCAST);
266		trh->saddr[0]|=TR_RII;
267	}
268	else
269	{
270		hash = rif_hash(trh->daddr);
271		/*
272		 *	Walk the hash table and look for an entry
273		 */
274		for(entry=rif_table[hash];entry && memcmp(&(entry->addr[0]),&(trh->daddr[0]),TR_ALEN);entry=entry->next);
275
276		/*
277		 *	If we found an entry we can route the frame.
278		 */
279		if(entry)
280		{
281#if TR_SR_DEBUG
282printk("source routing for %02X:%02X:%02X:%02X:%02X:%02X\n",trh->daddr[0],
283		  trh->daddr[1],trh->daddr[2],trh->daddr[3],trh->daddr[4],trh->daddr[5]);
284#endif
285			if(!entry->local_ring && (ntohs(entry->rcf) & TR_RCF_LEN_MASK) >> 8)
286			{
287				trh->rcf=entry->rcf;
288				memcpy(&trh->rseg[0],&entry->rseg[0],8*sizeof(unsigned short));
289				trh->rcf^=htons(TR_RCF_DIR_BIT);
290				trh->rcf&=htons(0x1fff);	/* Issam Chehab <ichehab@madge1.demon.co.uk> */
291
292				trh->saddr[0]|=TR_RII;
293#if TR_SR_DEBUG
294				printk("entry found with rcf %04x\n", entry->rcf);
295			}
296			else
297			{
298				printk("entry found but without rcf length, local=%02x\n", entry->local_ring);
299#endif
300			}
301			entry->last_used=jiffies;
302		}
303		else
304		{
305			/*
306			 *	Without the information we simply have to shout
307			 *	on the wire. The replies should rapidly clean this
308			 *	situation up.
309			 */
310			trh->rcf=htons((((sizeof(trh->rcf)) << 8) & TR_RCF_LEN_MASK)
311				       | TR_RCF_FRAME2K | TR_RCF_LIMITED_BROADCAST);
312			trh->saddr[0]|=TR_RII;
313#if TR_SR_DEBUG
314			printk("no entry in rif table found - broadcasting frame\n");
315#endif
316		}
317	}
318
319	/* Compress the RIF here so we don't have to do it in the driver(s) */
320	if (!(trh->saddr[0] & 0x80))
321		slack = 18;
322	else
323		slack = 18 - ((ntohs(trh->rcf) & TR_RCF_LEN_MASK)>>8);
324	olddata = skb->data;
325	spin_unlock_irqrestore(&rif_lock, flags);
326
327	skb_pull(skb, slack);
328	memmove(skb->data, olddata, sizeof(struct trh_hdr) - slack);
329}
330
331/*
332 *	We have learned some new RIF information for our source
333 *	routing.
334 */
335
336static void tr_add_rif_info(struct trh_hdr *trh, struct net_device *dev)
337{
338	unsigned int hash, rii_p = 0;
339	unsigned long flags;
340	struct rif_cache *entry;
341	unsigned char saddr0;
342
343	spin_lock_irqsave(&rif_lock, flags);
344	saddr0 = trh->saddr[0];
345
346	/*
347	 *	Firstly see if the entry exists
348	 */
349
350	if(trh->saddr[0] & TR_RII)
351	{
352		trh->saddr[0]&=0x7f;
353		if (((ntohs(trh->rcf) & TR_RCF_LEN_MASK) >> 8) > 2)
354		{
355			rii_p = 1;
356		}
357	}
358
359	hash = rif_hash(trh->saddr);
360	for(entry=rif_table[hash];entry && memcmp(&(entry->addr[0]),&(trh->saddr[0]),TR_ALEN);entry=entry->next);
361
362	if(entry==NULL)
363	{
364#if TR_SR_DEBUG
365printk("adding rif_entry: addr:%02X:%02X:%02X:%02X:%02X:%02X rcf:%04X\n",
366		trh->saddr[0],trh->saddr[1],trh->saddr[2],
367		trh->saddr[3],trh->saddr[4],trh->saddr[5],
368		ntohs(trh->rcf));
369#endif
370		entry=kmalloc(sizeof(struct rif_cache),GFP_ATOMIC);
371
372		if(!entry)
373		{
374			printk(KERN_DEBUG "tr.c: Couldn't malloc rif cache entry !\n");
375			spin_unlock_irqrestore(&rif_lock, flags);
376			return;
377		}
378
379		memcpy(&(entry->addr[0]),&(trh->saddr[0]),TR_ALEN);
380		entry->iface = dev->ifindex;
381		entry->next=rif_table[hash];
382		entry->last_used=jiffies;
383		rif_table[hash]=entry;
384
385		if (rii_p)
386		{
387			entry->rcf = trh->rcf & htons((unsigned short)~TR_RCF_BROADCAST_MASK);
388			memcpy(&(entry->rseg[0]),&(trh->rseg[0]),8*sizeof(unsigned short));
389			entry->local_ring = 0;
390		}
391		else
392		{
393			entry->local_ring = 1;
394		}
395	}
396	else	/* Y. Tahara added */
397	{
398		/*
399		 *	Update existing entries
400		 */
401		if (!entry->local_ring)
402		    if (entry->rcf != (trh->rcf & htons((unsigned short)~TR_RCF_BROADCAST_MASK)) &&
403			 !(trh->rcf & htons(TR_RCF_BROADCAST_MASK)))
404		    {
405#if TR_SR_DEBUG
406printk("updating rif_entry: addr:%02X:%02X:%02X:%02X:%02X:%02X rcf:%04X\n",
407		trh->saddr[0],trh->saddr[1],trh->saddr[2],
408		trh->saddr[3],trh->saddr[4],trh->saddr[5],
409		ntohs(trh->rcf));
410#endif
411			    entry->rcf = trh->rcf & htons((unsigned short)~TR_RCF_BROADCAST_MASK);
412			    memcpy(&(entry->rseg[0]),&(trh->rseg[0]),8*sizeof(unsigned short));
413		    }
414		entry->last_used=jiffies;
415	}
416	trh->saddr[0]=saddr0; /* put the routing indicator back for tcpdump */
417	spin_unlock_irqrestore(&rif_lock, flags);
418}
419
420/*
421 *	Scan the cache with a timer and see what we need to throw out.
422 */
423
424static void rif_check_expire(unsigned long dummy)
425{
426	int i;
427	unsigned long flags, next_interval = jiffies + sysctl_tr_rif_timeout/2;
428
429	spin_lock_irqsave(&rif_lock, flags);
430
431	for(i =0; i < RIF_TABLE_SIZE; i++) {
432		struct rif_cache *entry, **pentry;
433
434		pentry = rif_table+i;
435		while((entry=*pentry) != NULL) {
436			unsigned long expires
437				= entry->last_used + sysctl_tr_rif_timeout;
438
439			if (time_before_eq(expires, jiffies)) {
440				*pentry = entry->next;
441				kfree(entry);
442			} else {
443				pentry = &entry->next;
444
445				if (time_before(expires, next_interval))
446					next_interval = expires;
447			}
448		}
449	}
450
451	spin_unlock_irqrestore(&rif_lock, flags);
452
453	mod_timer(&rif_timer, next_interval);
454
455}
456
457/*
458 *	Generate the /proc/net information for the token ring RIF
459 *	routing.
460 */
461
462#ifdef CONFIG_PROC_FS
463
464static struct rif_cache *rif_get_idx(loff_t pos)
465{
466	int i;
467	struct rif_cache *entry;
468	loff_t off = 0;
469
470	for(i = 0; i < RIF_TABLE_SIZE; i++)
471		for(entry = rif_table[i]; entry; entry = entry->next) {
472			if (off == pos)
473				return entry;
474			++off;
475		}
476
477	return NULL;
478}
479
480static void *rif_seq_start(struct seq_file *seq, loff_t *pos)
481{
482	spin_lock_irq(&rif_lock);
483
484	return *pos ? rif_get_idx(*pos - 1) : SEQ_START_TOKEN;
485}
486
487static void *rif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
488{
489	int i;
490	struct rif_cache *ent = v;
491
492	++*pos;
493
494	if (v == SEQ_START_TOKEN) {
495		i = -1;
496		goto scan;
497	}
498
499	if (ent->next)
500		return ent->next;
501
502	i = rif_hash(ent->addr);
503 scan:
504	while (++i < RIF_TABLE_SIZE) {
505		if ((ent = rif_table[i]) != NULL)
506			return ent;
507	}
508	return NULL;
509}
510
511static void rif_seq_stop(struct seq_file *seq, void *v)
512{
513	spin_unlock_irq(&rif_lock);
514}
515
516static int rif_seq_show(struct seq_file *seq, void *v)
517{
518	int j, rcf_len, segment, brdgnmb;
519	struct rif_cache *entry = v;
520
521	if (v == SEQ_START_TOKEN)
522		seq_puts(seq,
523		     "if     TR address       TTL   rcf   routing segments\n");
524	else {
525		struct net_device *dev = dev_get_by_index(entry->iface);
526		long ttl = (long) (entry->last_used + sysctl_tr_rif_timeout)
527				- (long) jiffies;
528
529		seq_printf(seq, "%s %02X:%02X:%02X:%02X:%02X:%02X %7li ",
530			   dev?dev->name:"?",
531			   entry->addr[0],entry->addr[1],entry->addr[2],
532			   entry->addr[3],entry->addr[4],entry->addr[5],
533			   ttl/HZ);
534
535			if (entry->local_ring)
536				seq_puts(seq, "local\n");
537			else {
538
539				seq_printf(seq, "%04X", ntohs(entry->rcf));
540				rcf_len = ((ntohs(entry->rcf) & TR_RCF_LEN_MASK)>>8)-2;
541				if (rcf_len)
542					rcf_len >>= 1;
543				for(j = 1; j < rcf_len; j++) {
544					if(j==1) {
545						segment=ntohs(entry->rseg[j-1])>>4;
546						seq_printf(seq,"  %03X",segment);
547					}
548
549					segment=ntohs(entry->rseg[j])>>4;
550					brdgnmb=ntohs(entry->rseg[j-1])&0x00f;
551					seq_printf(seq,"-%01X-%03X",brdgnmb,segment);
552				}
553				seq_putc(seq, '\n');
554			}
555		}
556	return 0;
557}
558
559
560static struct seq_operations rif_seq_ops = {
561	.start = rif_seq_start,
562	.next  = rif_seq_next,
563	.stop  = rif_seq_stop,
564	.show  = rif_seq_show,
565};
566
567static int rif_seq_open(struct inode *inode, struct file *file)
568{
569	return seq_open(file, &rif_seq_ops);
570}
571
572static const struct file_operations rif_seq_fops = {
573	.owner	 = THIS_MODULE,
574	.open    = rif_seq_open,
575	.read    = seq_read,
576	.llseek  = seq_lseek,
577	.release = seq_release,
578};
579
580#endif
581
582static void tr_setup(struct net_device *dev)
583{
584	/*
585	 *	Configure and register
586	 */
587
588	dev->hard_header	= tr_header;
589	dev->rebuild_header	= tr_rebuild_header;
590
591	dev->type		= ARPHRD_IEEE802_TR;
592	dev->hard_header_len	= TR_HLEN;
593	dev->mtu		= 2000;
594	dev->addr_len		= TR_ALEN;
595	dev->tx_queue_len	= 100;	/* Long queues on tr */
596
597	memset(dev->broadcast,0xFF, TR_ALEN);
598
599	/* New-style flags. */
600	dev->flags		= IFF_BROADCAST | IFF_MULTICAST ;
601}
602
603/**
604 * alloc_trdev - Register token ring device
605 * @sizeof_priv: Size of additional driver-private structure to be allocated
606 *	for this token ring device
607 *
608 * Fill in the fields of the device structure with token ring-generic values.
609 *
610 * Constructs a new net device, complete with a private data area of
611 * size @sizeof_priv.  A 32-byte (not bit) alignment is enforced for
612 * this private data area.
613 */
614struct net_device *alloc_trdev(int sizeof_priv)
615{
616	return alloc_netdev(sizeof_priv, "tr%d", tr_setup);
617}
618
619/*
620 *	Called during bootup.  We don't actually have to initialise
621 *	too much for this.
622 */
623
624static int __init rif_init(void)
625{
626	init_timer(&rif_timer);
627	rif_timer.expires  = sysctl_tr_rif_timeout;
628	rif_timer.data     = 0L;
629	rif_timer.function = rif_check_expire;
630	add_timer(&rif_timer);
631
632	proc_net_fops_create("tr_rif", S_IRUGO, &rif_seq_fops);
633	return 0;
634}
635
636module_init(rif_init);
637
638EXPORT_SYMBOL(tr_type_trans);
639EXPORT_SYMBOL(alloc_trdev);
640