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/config.h>
21#include <linux/types.h>
22#include <linux/kernel.h>
23#include <linux/sched.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/init.h>
37#include <net/arp.h>
38
39void tr_source_route(struct sk_buff *skb, struct trh_hdr *trh,
40		     struct net_device *dev);
41static void tr_add_rif_info(struct trh_hdr *trh, struct net_device *dev);
42static void rif_check_expire(unsigned long dummy);
43
44#define TR_SR_DEBUG 0
45
46typedef struct rif_cache_s *rif_cache;
47
48/*
49 *	Each RIF entry we learn is kept this way
50 */
51
52struct rif_cache_s {
53	unsigned char addr[TR_ALEN];
54	int iface;
55	__u16 rcf;
56	__u16 rseg[8];
57	rif_cache next;
58	unsigned long last_used;
59	unsigned char local_ring;
60};
61
62#define RIF_TABLE_SIZE 32
63
64/*
65 *	We hash the RIF cache 32 ways. We do after all have to look it
66 *	up a lot.
67 */
68
69rif_cache rif_table[RIF_TABLE_SIZE];
70
71static spinlock_t rif_lock = SPIN_LOCK_UNLOCKED;
72
73#define RIF_TIMEOUT 60*10*HZ
74#define RIF_CHECK_INTERVAL 60*HZ
75
76/*
77 *	Garbage disposal timer.
78 */
79
80static struct timer_list rif_timer;
81
82int sysctl_tr_rif_timeout = RIF_TIMEOUT;
83
84/*
85 *	Put the headers on a token ring packet. Token ring source routing
86 *	makes this a little more exciting than on ethernet.
87 */
88
89int tr_header(struct sk_buff *skb, struct net_device *dev, unsigned short type,
90              void *daddr, void *saddr, unsigned len)
91{
92	struct trh_hdr *trh;
93	int hdr_len;
94
95	/*
96	 * Add the 802.2 SNAP header if IP as the IPv4 code calls
97	 * dev->hard_header directly.
98	 */
99	if (type == ETH_P_IP || type == ETH_P_ARP)
100	{
101		struct trllc *trllc=(struct trllc *)(trh+1);
102
103		hdr_len = sizeof(struct trh_hdr) + sizeof(struct trllc);
104		trh = (struct trh_hdr *)skb_push(skb, hdr_len);
105		trllc = (struct trllc *)(trh+1);
106		trllc->dsap = trllc->ssap = EXTENDED_SAP;
107		trllc->llc = UI_CMD;
108		trllc->protid[0] = trllc->protid[1] = trllc->protid[2] = 0x00;
109		trllc->ethertype = htons(type);
110	}
111	else
112	{
113		hdr_len = sizeof(struct trh_hdr);
114		trh = (struct trh_hdr *)skb_push(skb, hdr_len);
115	}
116
117	trh->ac=AC;
118	trh->fc=LLC_FRAME;
119
120	if(saddr)
121		memcpy(trh->saddr,saddr,dev->addr_len);
122	else
123		memcpy(trh->saddr,dev->dev_addr,dev->addr_len);
124
125	/*
126	 *	Build the destination and then source route the frame
127	 */
128
129	if(daddr)
130	{
131		memcpy(trh->daddr,daddr,dev->addr_len);
132		tr_source_route(skb,trh,dev);
133		return(hdr_len);
134	}
135
136	return -hdr_len;
137}
138
139/*
140 *	A neighbour discovery of some species (eg arp) has completed. We
141 *	can now send the packet.
142 */
143
144int tr_rebuild_header(struct sk_buff *skb)
145{
146	struct trh_hdr *trh=(struct trh_hdr *)skb->data;
147	struct trllc *trllc=(struct trllc *)(skb->data+sizeof(struct trh_hdr));
148	struct net_device *dev = skb->dev;
149
150
151	if(trllc->ethertype != htons(ETH_P_IP)) {
152		printk("tr_rebuild_header: Don't know how to resolve type %04X addresses ?\n",(unsigned int)htons(trllc->ethertype));
153		return 0;
154	}
155
156#ifdef CONFIG_INET
157	if(arp_find(trh->daddr, skb)) {
158			return 1;
159	}
160	else
161#endif
162	{
163		tr_source_route(skb,trh,dev);
164		return 0;
165	}
166}
167
168/*
169 *	Some of this is a bit hackish. We intercept RIF information
170 *	used for source routing. We also grab IP directly and don't feed
171 *	it via SNAP.
172 */
173
174unsigned short tr_type_trans(struct sk_buff *skb, struct net_device *dev)
175{
176
177	struct trh_hdr *trh=(struct trh_hdr *)skb->data;
178	struct trllc *trllc;
179	unsigned riflen=0;
180
181	skb->mac.raw = skb->data;
182
183       	if(trh->saddr[0] & TR_RII)
184		riflen = (ntohs(trh->rcf) & TR_RCF_LEN_MASK) >> 8;
185
186	trllc = (struct trllc *)(skb->data+sizeof(struct trh_hdr)-TR_MAXRIFLEN+riflen);
187
188	skb_pull(skb,sizeof(struct trh_hdr)-TR_MAXRIFLEN+riflen);
189
190	if(*trh->daddr & 0x80)
191	{
192		if(!memcmp(trh->daddr,dev->broadcast,TR_ALEN))
193			skb->pkt_type=PACKET_BROADCAST;
194		else
195			skb->pkt_type=PACKET_MULTICAST;
196	}
197	else if ( (trh->daddr[0] & 0x01) && (trh->daddr[1] & 0x00) && (trh->daddr[2] & 0x5E))
198	{
199		skb->pkt_type=PACKET_MULTICAST;
200	}
201	else if(dev->flags & IFF_PROMISC)
202	{
203		if(memcmp(trh->daddr, dev->dev_addr, TR_ALEN))
204			skb->pkt_type=PACKET_OTHERHOST;
205	}
206
207	if ((skb->pkt_type != PACKET_BROADCAST) &&
208	    (skb->pkt_type != PACKET_MULTICAST))
209		tr_add_rif_info(trh,dev) ;
210
211	/*
212	 * Strip the SNAP header from ARP packets since we don't
213	 * pass them through to the 802.2/SNAP layers.
214	 */
215
216	if (trllc->dsap == EXTENDED_SAP &&
217	    (trllc->ethertype == ntohs(ETH_P_IP) ||
218	     trllc->ethertype == ntohs(ETH_P_ARP)))
219	{
220		skb_pull(skb, sizeof(struct trllc));
221		return trllc->ethertype;
222	}
223
224	return ntohs(ETH_P_802_2);
225}
226
227/*
228 *	We try to do source routing...
229 */
230
231void tr_source_route(struct sk_buff *skb, struct trh_hdr *trh,
232		     struct net_device *dev)
233{
234	int i, slack;
235	unsigned int hash;
236	rif_cache entry;
237	unsigned char *olddata;
238	unsigned char mcast_func_addr[] = {0xC0,0x00,0x00,0x04,0x00,0x00};
239	unsigned long flags ;
240
241	spin_lock_irqsave(&rif_lock,flags);
242
243	/*
244	 *	Broadcasts are single route as stated in RFC 1042
245	 */
246	if( (!memcmp(&(trh->daddr[0]),&(dev->broadcast[0]),TR_ALEN)) ||
247	    (!memcmp(&(trh->daddr[0]),&(mcast_func_addr[0]), TR_ALEN))  )
248	{
249		trh->rcf=htons((((sizeof(trh->rcf)) << 8) & TR_RCF_LEN_MASK)
250			       | TR_RCF_FRAME2K | TR_RCF_LIMITED_BROADCAST);
251		trh->saddr[0]|=TR_RII;
252	}
253	else
254	{
255		for(i=0,hash=0;i<TR_ALEN;hash+=trh->daddr[i++]);
256		hash&=RIF_TABLE_SIZE-1;
257		/*
258		 *	Walk the hash table and look for an entry
259		 */
260		for(entry=rif_table[hash];entry && memcmp(&(entry->addr[0]),&(trh->daddr[0]),TR_ALEN);entry=entry->next);
261
262		/*
263		 *	If we found an entry we can route the frame.
264		 */
265		if(entry)
266		{
267#if TR_SR_DEBUG
268printk("source routing for %02X:%02X:%02X:%02X:%02X:%02X\n",trh->daddr[0],
269		  trh->daddr[1],trh->daddr[2],trh->daddr[3],trh->daddr[4],trh->daddr[5]);
270#endif
271			if(!entry->local_ring && (ntohs(entry->rcf) & TR_RCF_LEN_MASK) >> 8)
272			{
273				trh->rcf=entry->rcf;
274				memcpy(&trh->rseg[0],&entry->rseg[0],8*sizeof(unsigned short));
275				trh->rcf^=htons(TR_RCF_DIR_BIT);
276				trh->rcf&=htons(0x1fff);	/* Issam Chehab <ichehab@madge1.demon.co.uk> */
277
278				trh->saddr[0]|=TR_RII;
279#if TR_SR_DEBUG
280				printk("entry found with rcf %04x\n", entry->rcf);
281			}
282			else
283			{
284				printk("entry found but without rcf length, local=%02x\n", entry->local_ring);
285#endif
286			}
287			entry->last_used=jiffies;
288		}
289		else
290		{
291			/*
292			 *	Without the information we simply have to shout
293			 *	on the wire. The replies should rapidly clean this
294			 *	situation up.
295			 */
296			trh->rcf=htons((((sizeof(trh->rcf)) << 8) & TR_RCF_LEN_MASK)
297				       | TR_RCF_FRAME2K | TR_RCF_LIMITED_BROADCAST);
298			trh->saddr[0]|=TR_RII;
299#if TR_SR_DEBUG
300			printk("no entry in rif table found - broadcasting frame\n");
301#endif
302		}
303	}
304
305	/* Compress the RIF here so we don't have to do it in the driver(s) */
306	if (!(trh->saddr[0] & 0x80))
307		slack = 18;
308	else
309		slack = 18 - ((ntohs(trh->rcf) & TR_RCF_LEN_MASK)>>8);
310	olddata = skb->data;
311	spin_unlock_irqrestore(&rif_lock,flags);
312
313	skb_pull(skb, slack);
314	memmove(skb->data, olddata, sizeof(struct trh_hdr) - slack);
315}
316
317/*
318 *	We have learned some new RIF information for our source
319 *	routing.
320 */
321
322static void tr_add_rif_info(struct trh_hdr *trh, struct net_device *dev)
323{
324	int i;
325	unsigned int hash, rii_p = 0;
326	rif_cache entry;
327
328
329	spin_lock_bh(&rif_lock);
330
331	/*
332	 *	Firstly see if the entry exists
333	 */
334
335       	if(trh->saddr[0] & TR_RII)
336	{
337		trh->saddr[0]&=0x7f;
338		if (((ntohs(trh->rcf) & TR_RCF_LEN_MASK) >> 8) > 2)
339		{
340			rii_p = 1;
341	        }
342	}
343
344	for(i=0,hash=0;i<TR_ALEN;hash+=trh->saddr[i++]);
345	hash&=RIF_TABLE_SIZE-1;
346	for(entry=rif_table[hash];entry && memcmp(&(entry->addr[0]),&(trh->saddr[0]),TR_ALEN);entry=entry->next);
347
348	if(entry==NULL)
349	{
350#if TR_SR_DEBUG
351printk("adding rif_entry: addr:%02X:%02X:%02X:%02X:%02X:%02X rcf:%04X\n",
352		trh->saddr[0],trh->saddr[1],trh->saddr[2],
353       		trh->saddr[3],trh->saddr[4],trh->saddr[5],
354		ntohs(trh->rcf));
355#endif
356		entry=kmalloc(sizeof(struct rif_cache_s),GFP_ATOMIC);
357
358		if(!entry)
359		{
360			printk(KERN_DEBUG "tr.c: Couldn't malloc rif cache entry !\n");
361			spin_unlock_bh(&rif_lock);
362			return;
363		}
364
365		memcpy(&(entry->addr[0]),&(trh->saddr[0]),TR_ALEN);
366		entry->iface = dev->ifindex;
367		entry->next=rif_table[hash];
368		entry->last_used=jiffies;
369		rif_table[hash]=entry;
370
371		if (rii_p)
372		{
373			entry->rcf = trh->rcf & htons((unsigned short)~TR_RCF_BROADCAST_MASK);
374			memcpy(&(entry->rseg[0]),&(trh->rseg[0]),8*sizeof(unsigned short));
375			entry->local_ring = 0;
376			trh->saddr[0]|=TR_RII; /* put the routing indicator back for tcpdump */
377		}
378		else
379		{
380			entry->local_ring = 1;
381		}
382	}
383	else	/* Y. Tahara added */
384	{
385		/*
386		 *	Update existing entries
387		 */
388		if (!entry->local_ring)
389		    if (entry->rcf != (trh->rcf & htons((unsigned short)~TR_RCF_BROADCAST_MASK)) &&
390			 !(trh->rcf & htons(TR_RCF_BROADCAST_MASK)))
391		    {
392#if TR_SR_DEBUG
393printk("updating rif_entry: addr:%02X:%02X:%02X:%02X:%02X:%02X rcf:%04X\n",
394		trh->saddr[0],trh->saddr[1],trh->saddr[2],
395		trh->saddr[3],trh->saddr[4],trh->saddr[5],
396		ntohs(trh->rcf));
397#endif
398			    entry->rcf = trh->rcf & htons((unsigned short)~TR_RCF_BROADCAST_MASK);
399        		    memcpy(&(entry->rseg[0]),&(trh->rseg[0]),8*sizeof(unsigned short));
400		    }
401           	entry->last_used=jiffies;
402	}
403	spin_unlock_bh(&rif_lock);
404}
405
406/*
407 *	Scan the cache with a timer and see what we need to throw out.
408 */
409
410static void rif_check_expire(unsigned long dummy)
411{
412	int i;
413	unsigned long now=jiffies;
414	unsigned long flags ;
415
416	spin_lock_irqsave(&rif_lock,flags);
417
418	for(i=0; i < RIF_TABLE_SIZE;i++)
419	{
420		rif_cache entry, *pentry=rif_table+i;
421		while((entry=*pentry))
422		{
423			/*
424			 *	Out it goes
425			 */
426			if((now-entry->last_used) > sysctl_tr_rif_timeout)
427			{
428				*pentry=entry->next;
429				kfree(entry);
430			}
431			else
432				pentry=&entry->next;
433		}
434	}
435
436	spin_unlock_irqrestore(&rif_lock,flags);
437
438	/*
439	 *	Reset the timer
440	 */
441
442	mod_timer(&rif_timer, jiffies+sysctl_tr_rif_timeout);
443
444}
445
446/*
447 *	Generate the /proc/net information for the token ring RIF
448 *	routing.
449 */
450
451#ifndef CONFIG_PROC_FS
452static int rif_get_info(char *buffer,char **start, off_t offset, int length)  { return 0;}
453#else
454static int rif_get_info(char *buffer,char **start, off_t offset, int length)
455{
456	int len=0;
457	off_t begin=0;
458	off_t pos=0;
459	int size,i,j,rcf_len,segment,brdgnmb;
460	unsigned long now=jiffies;
461
462	rif_cache entry;
463
464	size=sprintf(buffer,
465		     "if     TR address       TTL   rcf   routing segments\n");
466	pos+=size;
467	len+=size;
468
469	spin_lock_bh(&rif_lock);
470	for(i=0;i < RIF_TABLE_SIZE;i++)
471	{
472		for(entry=rif_table[i];entry;entry=entry->next) {
473			struct net_device *dev = __dev_get_by_index(entry->iface);
474
475			size=sprintf(buffer+len,"%s %02X:%02X:%02X:%02X:%02X:%02X %7li ",
476				     dev?dev->name:"?",entry->addr[0],entry->addr[1],entry->addr[2],entry->addr[3],entry->addr[4],entry->addr[5],
477				     sysctl_tr_rif_timeout-(now-entry->last_used));
478			len+=size;
479			pos=begin+len;
480			if (entry->local_ring)
481			        size=sprintf(buffer+len,"local\n");
482			else {
483			        size=sprintf(buffer+len,"%04X", ntohs(entry->rcf));
484				rcf_len = ((ntohs(entry->rcf) & TR_RCF_LEN_MASK)>>8)-2;
485				if (rcf_len)
486				        rcf_len >>= 1;
487				for(j = 1; j < rcf_len; j++) {
488					if(j==1) {
489						segment=ntohs(entry->rseg[j-1])>>4;
490						len+=size;
491						pos=begin+len;
492						size=sprintf(buffer+len,"  %03X",segment);
493					};
494					segment=ntohs(entry->rseg[j])>>4;
495					brdgnmb=ntohs(entry->rseg[j-1])&0x00f;
496					len+=size;
497					pos=begin+len;
498					size=sprintf(buffer+len,"-%01X-%03X",brdgnmb,segment);
499				}
500				len+=size;
501				pos=begin+len;
502			        size=sprintf(buffer+len,"\n");
503			}
504			len+=size;
505			pos=begin+len;
506
507			if(pos<offset)
508			{
509				len=0;
510				begin=pos;
511			}
512			if(pos>offset+length)
513				break;
514	   	}
515		if(pos>offset+length)
516			break;
517	}
518	spin_unlock_bh(&rif_lock);
519
520	*start=buffer+(offset-begin); /* Start of wanted data */
521	len-=(offset-begin);    /* Start slop */
522	if(len>length)
523		len=length;    /* Ending slop */
524	if (len<0)
525		len=0;
526	return len;
527}
528#endif
529
530/*
531 *	Called during bootup.  We don't actually have to initialise
532 *	too much for this.
533 */
534
535static int __init rif_init(void)
536{
537	rif_timer.expires  = RIF_TIMEOUT;
538	rif_timer.data     = 0L;
539	rif_timer.function = rif_check_expire;
540	init_timer(&rif_timer);
541	add_timer(&rif_timer);
542
543	proc_net_create("tr_rif",0,rif_get_info);
544	return 0;
545}
546
547module_init(rif_init);
548