[TR]: Use tr_hdr() were appropriate
[linux-2.6.git] / net / 802 / tr.c
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
40 static void tr_add_rif_info(struct trh_hdr *trh, struct net_device *dev);
41 static 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
49 struct 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
66 static struct rif_cache *rif_table[RIF_TABLE_SIZE];
67
68 static DEFINE_SPINLOCK(rif_lock);
69
70
71 /*
72  *      Garbage disposal timer.
73  */
74
75 static struct timer_list rif_timer;
76
77 int sysctl_tr_rif_timeout = 60*10*HZ;
78
79 static 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
100 static 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
156 static 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          *      FIXME: We don't yet support IPv6 over token rings
164          */
165
166         if(trllc->ethertype != htons(ETH_P_IP)) {
167                 printk("tr_rebuild_header: Don't know how to resolve type %04X addresses ?\n", ntohs(trllc->ethertype));
168                 return 0;
169         }
170
171 #ifdef CONFIG_INET
172         if(arp_find(trh->daddr, skb)) {
173                         return 1;
174         }
175         else
176 #endif
177         {
178                 tr_source_route(skb,trh,dev);
179                 return 0;
180         }
181 }
182
183 /*
184  *      Some of this is a bit hackish. We intercept RIF information
185  *      used for source routing. We also grab IP directly and don't feed
186  *      it via SNAP.
187  */
188
189 __be16 tr_type_trans(struct sk_buff *skb, struct net_device *dev)
190 {
191
192         struct trh_hdr *trh;
193         struct trllc *trllc;
194         unsigned riflen=0;
195
196         skb->mac.raw = skb->data;
197         trh = tr_hdr(skb);
198
199         if(trh->saddr[0] & TR_RII)
200                 riflen = (ntohs(trh->rcf) & TR_RCF_LEN_MASK) >> 8;
201
202         trllc = (struct trllc *)(skb->data+sizeof(struct trh_hdr)-TR_MAXRIFLEN+riflen);
203
204         skb_pull(skb,sizeof(struct trh_hdr)-TR_MAXRIFLEN+riflen);
205
206         if(*trh->daddr & 0x80)
207         {
208                 if(!memcmp(trh->daddr,dev->broadcast,TR_ALEN))
209                         skb->pkt_type=PACKET_BROADCAST;
210                 else
211                         skb->pkt_type=PACKET_MULTICAST;
212         }
213         else if ( (trh->daddr[0] & 0x01) && (trh->daddr[1] & 0x00) && (trh->daddr[2] & 0x5E))
214         {
215                 skb->pkt_type=PACKET_MULTICAST;
216         }
217         else if(dev->flags & IFF_PROMISC)
218         {
219                 if(memcmp(trh->daddr, dev->dev_addr, TR_ALEN))
220                         skb->pkt_type=PACKET_OTHERHOST;
221         }
222
223         if ((skb->pkt_type != PACKET_BROADCAST) &&
224             (skb->pkt_type != PACKET_MULTICAST))
225                 tr_add_rif_info(trh,dev) ;
226
227         /*
228          * Strip the SNAP header from ARP packets since we don't
229          * pass them through to the 802.2/SNAP layers.
230          */
231
232         if (trllc->dsap == EXTENDED_SAP &&
233             (trllc->ethertype == htons(ETH_P_IP) ||
234              trllc->ethertype == htons(ETH_P_IPV6) ||
235              trllc->ethertype == htons(ETH_P_ARP)))
236         {
237                 skb_pull(skb, sizeof(struct trllc));
238                 return trllc->ethertype;
239         }
240
241         return htons(ETH_P_TR_802_2);
242 }
243
244 /*
245  *      We try to do source routing...
246  */
247
248 void tr_source_route(struct sk_buff *skb,struct trh_hdr *trh,struct net_device *dev)
249 {
250         int slack;
251         unsigned int hash;
252         struct rif_cache *entry;
253         unsigned char *olddata;
254         unsigned long flags;
255         static const unsigned char mcast_func_addr[]
256                 = {0xC0,0x00,0x00,0x04,0x00,0x00};
257
258         spin_lock_irqsave(&rif_lock, flags);
259
260         /*
261          *      Broadcasts are single route as stated in RFC 1042
262          */
263         if( (!memcmp(&(trh->daddr[0]),&(dev->broadcast[0]),TR_ALEN)) ||
264             (!memcmp(&(trh->daddr[0]),&(mcast_func_addr[0]), TR_ALEN))  )
265         {
266                 trh->rcf=htons((((sizeof(trh->rcf)) << 8) & TR_RCF_LEN_MASK)
267                                | TR_RCF_FRAME2K | TR_RCF_LIMITED_BROADCAST);
268                 trh->saddr[0]|=TR_RII;
269         }
270         else
271         {
272                 hash = rif_hash(trh->daddr);
273                 /*
274                  *      Walk the hash table and look for an entry
275                  */
276                 for(entry=rif_table[hash];entry && memcmp(&(entry->addr[0]),&(trh->daddr[0]),TR_ALEN);entry=entry->next);
277
278                 /*
279                  *      If we found an entry we can route the frame.
280                  */
281                 if(entry)
282                 {
283 #if TR_SR_DEBUG
284 printk("source routing for %02X:%02X:%02X:%02X:%02X:%02X\n",trh->daddr[0],
285                   trh->daddr[1],trh->daddr[2],trh->daddr[3],trh->daddr[4],trh->daddr[5]);
286 #endif
287                         if(!entry->local_ring && (ntohs(entry->rcf) & TR_RCF_LEN_MASK) >> 8)
288                         {
289                                 trh->rcf=entry->rcf;
290                                 memcpy(&trh->rseg[0],&entry->rseg[0],8*sizeof(unsigned short));
291                                 trh->rcf^=htons(TR_RCF_DIR_BIT);
292                                 trh->rcf&=htons(0x1fff);        /* Issam Chehab <ichehab@madge1.demon.co.uk> */
293
294                                 trh->saddr[0]|=TR_RII;
295 #if TR_SR_DEBUG
296                                 printk("entry found with rcf %04x\n", entry->rcf);
297                         }
298                         else
299                         {
300                                 printk("entry found but without rcf length, local=%02x\n", entry->local_ring);
301 #endif
302                         }
303                         entry->last_used=jiffies;
304                 }
305                 else
306                 {
307                         /*
308                          *      Without the information we simply have to shout
309                          *      on the wire. The replies should rapidly clean this
310                          *      situation up.
311                          */
312                         trh->rcf=htons((((sizeof(trh->rcf)) << 8) & TR_RCF_LEN_MASK)
313                                        | TR_RCF_FRAME2K | TR_RCF_LIMITED_BROADCAST);
314                         trh->saddr[0]|=TR_RII;
315 #if TR_SR_DEBUG
316                         printk("no entry in rif table found - broadcasting frame\n");
317 #endif
318                 }
319         }
320
321         /* Compress the RIF here so we don't have to do it in the driver(s) */
322         if (!(trh->saddr[0] & 0x80))
323                 slack = 18;
324         else
325                 slack = 18 - ((ntohs(trh->rcf) & TR_RCF_LEN_MASK)>>8);
326         olddata = skb->data;
327         spin_unlock_irqrestore(&rif_lock, flags);
328
329         skb_pull(skb, slack);
330         memmove(skb->data, olddata, sizeof(struct trh_hdr) - slack);
331 }
332
333 /*
334  *      We have learned some new RIF information for our source
335  *      routing.
336  */
337
338 static void tr_add_rif_info(struct trh_hdr *trh, struct net_device *dev)
339 {
340         unsigned int hash, rii_p = 0;
341         unsigned long flags;
342         struct rif_cache *entry;
343         unsigned char saddr0;
344
345         spin_lock_irqsave(&rif_lock, flags);
346         saddr0 = trh->saddr[0];
347
348         /*
349          *      Firstly see if the entry exists
350          */
351
352         if(trh->saddr[0] & TR_RII)
353         {
354                 trh->saddr[0]&=0x7f;
355                 if (((ntohs(trh->rcf) & TR_RCF_LEN_MASK) >> 8) > 2)
356                 {
357                         rii_p = 1;
358                 }
359         }
360
361         hash = rif_hash(trh->saddr);
362         for(entry=rif_table[hash];entry && memcmp(&(entry->addr[0]),&(trh->saddr[0]),TR_ALEN);entry=entry->next);
363
364         if(entry==NULL)
365         {
366 #if TR_SR_DEBUG
367 printk("adding rif_entry: addr:%02X:%02X:%02X:%02X:%02X:%02X rcf:%04X\n",
368                 trh->saddr[0],trh->saddr[1],trh->saddr[2],
369                 trh->saddr[3],trh->saddr[4],trh->saddr[5],
370                 ntohs(trh->rcf));
371 #endif
372                 /*
373                  *      Allocate our new entry. A failure to allocate loses
374                  *      use the information. This is harmless.
375                  *
376                  *      FIXME: We ought to keep some kind of cache size
377                  *      limiting and adjust the timers to suit.
378                  */
379                 entry=kmalloc(sizeof(struct rif_cache),GFP_ATOMIC);
380
381                 if(!entry)
382                 {
383                         printk(KERN_DEBUG "tr.c: Couldn't malloc rif cache entry !\n");
384                         spin_unlock_irqrestore(&rif_lock, flags);
385                         return;
386                 }
387
388                 memcpy(&(entry->addr[0]),&(trh->saddr[0]),TR_ALEN);
389                 entry->iface = dev->ifindex;
390                 entry->next=rif_table[hash];
391                 entry->last_used=jiffies;
392                 rif_table[hash]=entry;
393
394                 if (rii_p)
395                 {
396                         entry->rcf = trh->rcf & htons((unsigned short)~TR_RCF_BROADCAST_MASK);
397                         memcpy(&(entry->rseg[0]),&(trh->rseg[0]),8*sizeof(unsigned short));
398                         entry->local_ring = 0;
399                 }
400                 else
401                 {
402                         entry->local_ring = 1;
403                 }
404         }
405         else    /* Y. Tahara added */
406         {
407                 /*
408                  *      Update existing entries
409                  */
410                 if (!entry->local_ring)
411                     if (entry->rcf != (trh->rcf & htons((unsigned short)~TR_RCF_BROADCAST_MASK)) &&
412                          !(trh->rcf & htons(TR_RCF_BROADCAST_MASK)))
413                     {
414 #if TR_SR_DEBUG
415 printk("updating rif_entry: addr:%02X:%02X:%02X:%02X:%02X:%02X rcf:%04X\n",
416                 trh->saddr[0],trh->saddr[1],trh->saddr[2],
417                 trh->saddr[3],trh->saddr[4],trh->saddr[5],
418                 ntohs(trh->rcf));
419 #endif
420                             entry->rcf = trh->rcf & htons((unsigned short)~TR_RCF_BROADCAST_MASK);
421                             memcpy(&(entry->rseg[0]),&(trh->rseg[0]),8*sizeof(unsigned short));
422                     }
423                 entry->last_used=jiffies;
424         }
425         trh->saddr[0]=saddr0; /* put the routing indicator back for tcpdump */
426         spin_unlock_irqrestore(&rif_lock, flags);
427 }
428
429 /*
430  *      Scan the cache with a timer and see what we need to throw out.
431  */
432
433 static void rif_check_expire(unsigned long dummy)
434 {
435         int i;
436         unsigned long flags, next_interval = jiffies + sysctl_tr_rif_timeout/2;
437
438         spin_lock_irqsave(&rif_lock, flags);
439
440         for(i =0; i < RIF_TABLE_SIZE; i++) {
441                 struct rif_cache *entry, **pentry;
442
443                 pentry = rif_table+i;
444                 while((entry=*pentry) != NULL) {
445                         unsigned long expires
446                                 = entry->last_used + sysctl_tr_rif_timeout;
447
448                         if (time_before_eq(expires, jiffies)) {
449                                 *pentry = entry->next;
450                                 kfree(entry);
451                         } else {
452                                 pentry = &entry->next;
453
454                                 if (time_before(expires, next_interval))
455                                         next_interval = expires;
456                         }
457                 }
458         }
459
460         spin_unlock_irqrestore(&rif_lock, flags);
461
462         mod_timer(&rif_timer, next_interval);
463
464 }
465
466 /*
467  *      Generate the /proc/net information for the token ring RIF
468  *      routing.
469  */
470
471 #ifdef CONFIG_PROC_FS
472
473 static struct rif_cache *rif_get_idx(loff_t pos)
474 {
475         int i;
476         struct rif_cache *entry;
477         loff_t off = 0;
478
479         for(i = 0; i < RIF_TABLE_SIZE; i++)
480                 for(entry = rif_table[i]; entry; entry = entry->next) {
481                         if (off == pos)
482                                 return entry;
483                         ++off;
484                 }
485
486         return NULL;
487 }
488
489 static void *rif_seq_start(struct seq_file *seq, loff_t *pos)
490 {
491         spin_lock_irq(&rif_lock);
492
493         return *pos ? rif_get_idx(*pos - 1) : SEQ_START_TOKEN;
494 }
495
496 static void *rif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
497 {
498         int i;
499         struct rif_cache *ent = v;
500
501         ++*pos;
502
503         if (v == SEQ_START_TOKEN) {
504                 i = -1;
505                 goto scan;
506         }
507
508         if (ent->next)
509                 return ent->next;
510
511         i = rif_hash(ent->addr);
512  scan:
513         while (++i < RIF_TABLE_SIZE) {
514                 if ((ent = rif_table[i]) != NULL)
515                         return ent;
516         }
517         return NULL;
518 }
519
520 static void rif_seq_stop(struct seq_file *seq, void *v)
521 {
522         spin_unlock_irq(&rif_lock);
523 }
524
525 static int rif_seq_show(struct seq_file *seq, void *v)
526 {
527         int j, rcf_len, segment, brdgnmb;
528         struct rif_cache *entry = v;
529
530         if (v == SEQ_START_TOKEN)
531                 seq_puts(seq,
532                      "if     TR address       TTL   rcf   routing segments\n");
533         else {
534                 struct net_device *dev = dev_get_by_index(entry->iface);
535                 long ttl = (long) (entry->last_used + sysctl_tr_rif_timeout)
536                                 - (long) jiffies;
537
538                 seq_printf(seq, "%s %02X:%02X:%02X:%02X:%02X:%02X %7li ",
539                            dev?dev->name:"?",
540                            entry->addr[0],entry->addr[1],entry->addr[2],
541                            entry->addr[3],entry->addr[4],entry->addr[5],
542                            ttl/HZ);
543
544                         if (entry->local_ring)
545                                 seq_puts(seq, "local\n");
546                         else {
547
548                                 seq_printf(seq, "%04X", ntohs(entry->rcf));
549                                 rcf_len = ((ntohs(entry->rcf) & TR_RCF_LEN_MASK)>>8)-2;
550                                 if (rcf_len)
551                                         rcf_len >>= 1;
552                                 for(j = 1; j < rcf_len; j++) {
553                                         if(j==1) {
554                                                 segment=ntohs(entry->rseg[j-1])>>4;
555                                                 seq_printf(seq,"  %03X",segment);
556                                         };
557                                         segment=ntohs(entry->rseg[j])>>4;
558                                         brdgnmb=ntohs(entry->rseg[j-1])&0x00f;
559                                         seq_printf(seq,"-%01X-%03X",brdgnmb,segment);
560                                 }
561                                 seq_putc(seq, '\n');
562                         }
563                 }
564         return 0;
565 }
566
567
568 static struct seq_operations rif_seq_ops = {
569         .start = rif_seq_start,
570         .next  = rif_seq_next,
571         .stop  = rif_seq_stop,
572         .show  = rif_seq_show,
573 };
574
575 static int rif_seq_open(struct inode *inode, struct file *file)
576 {
577         return seq_open(file, &rif_seq_ops);
578 }
579
580 static const struct file_operations rif_seq_fops = {
581         .owner   = THIS_MODULE,
582         .open    = rif_seq_open,
583         .read    = seq_read,
584         .llseek  = seq_lseek,
585         .release = seq_release,
586 };
587
588 #endif
589
590 static void tr_setup(struct net_device *dev)
591 {
592         /*
593          *      Configure and register
594          */
595
596         dev->hard_header        = tr_header;
597         dev->rebuild_header     = tr_rebuild_header;
598
599         dev->type               = ARPHRD_IEEE802_TR;
600         dev->hard_header_len    = TR_HLEN;
601         dev->mtu                = 2000;
602         dev->addr_len           = TR_ALEN;
603         dev->tx_queue_len       = 100;  /* Long queues on tr */
604
605         memset(dev->broadcast,0xFF, TR_ALEN);
606
607         /* New-style flags. */
608         dev->flags              = IFF_BROADCAST | IFF_MULTICAST ;
609 }
610
611 /**
612  * alloc_trdev - Register token ring device
613  * @sizeof_priv: Size of additional driver-private structure to be allocated
614  *      for this token ring device
615  *
616  * Fill in the fields of the device structure with token ring-generic values.
617  *
618  * Constructs a new net device, complete with a private data area of
619  * size @sizeof_priv.  A 32-byte (not bit) alignment is enforced for
620  * this private data area.
621  */
622 struct net_device *alloc_trdev(int sizeof_priv)
623 {
624         return alloc_netdev(sizeof_priv, "tr%d", tr_setup);
625 }
626
627 /*
628  *      Called during bootup.  We don't actually have to initialise
629  *      too much for this.
630  */
631
632 static int __init rif_init(void)
633 {
634         init_timer(&rif_timer);
635         rif_timer.expires  = sysctl_tr_rif_timeout;
636         rif_timer.data     = 0L;
637         rif_timer.function = rif_check_expire;
638         add_timer(&rif_timer);
639
640         proc_net_fops_create("tr_rif", S_IRUGO, &rif_seq_fops);
641         return 0;
642 }
643
644 module_init(rif_init);
645
646 EXPORT_SYMBOL(tr_type_trans);
647 EXPORT_SYMBOL(alloc_trdev);