root/net/802/tr.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. tr_header
  2. tr_rebuild_header
  3. tr_type_trans
  4. tr_source_route
  5. tr_add_rif_info
  6. rif_check_expire
  7. rif_get_info
  8. rif_init

   1 #include <asm/segment.h>
   2 #include <asm/system.h>
   3 #include <linux/types.h>
   4 #include <linux/kernel.h>
   5 #include <linux/sched.h>
   6 #include <linux/string.h>
   7 #include <linux/mm.h>
   8 #include <linux/socket.h>
   9 #include <linux/in.h>
  10 #include <linux/inet.h>
  11 #include <linux/netdevice.h>
  12 #include <linux/trdevice.h>
  13 #include <linux/skbuff.h>
  14 #include <linux/errno.h>
  15 #include <linux/string.h>
  16 #include <linux/timer.h>
  17 #include <linux/net.h>
  18 #include <net/arp.h>
  19 
  20 static void tr_source_route(struct trh_hdr *trh,struct device *dev);
  21 static void tr_add_rif_info(struct trh_hdr *trh);
  22 static void rif_check_expire(unsigned long dummy);
  23 
  24 typedef struct rif_cache_s *rif_cache;
  25 
  26 struct rif_cache_s {    
  27          unsigned char addr[TR_ALEN];
  28          unsigned short rcf;
  29          unsigned short rseg[8];
  30          rif_cache next;
  31          unsigned long last_used;
  32 };
  33 
  34 #define RIF_TABLE_SIZE 16
  35 rif_cache rif_table[RIF_TABLE_SIZE]={ NULL, };
  36 
  37 #define RIF_TIMEOUT 60*10*HZ
  38 #define RIF_CHECK_INTERVAL 60*HZ
  39 static struct timer_list rif_timer={ NULL,NULL,RIF_CHECK_INTERVAL,0L,rif_check_expire };
  40 
  41 int tr_header(unsigned char *buff, struct device *dev, unsigned short type,
     /* [previous][next][first][last][top][bottom][index][help] */
  42               void *daddr, void *saddr, unsigned len, struct sk_buff *skb) {
  43 
  44         struct trh_hdr *trh=(struct trh_hdr *)buff;
  45         struct trllc *trllc=(struct trllc *)(buff+sizeof(struct trh_hdr));
  46         
  47         trh->ac=AC;
  48         trh->fc=LLC_FRAME;
  49 
  50         if(saddr)
  51                 memcpy(trh->saddr,saddr,dev->addr_len);
  52         else
  53                 memset(trh->saddr,0,dev->addr_len); /* Adapter fills in address */
  54 
  55         trllc->dsap=trllc->ssap=EXTENDED_SAP;
  56         trllc->llc=UI_CMD;
  57         
  58         trllc->protid[0]=trllc->protid[1]=trllc->protid[2]=0x00;
  59         trllc->ethertype=htons(type);
  60 
  61         if(daddr) {
  62                 memcpy(trh->daddr,daddr,dev->addr_len);
  63                 tr_source_route(trh,dev);
  64                 return(dev->hard_header_len);
  65         }
  66         return -dev->hard_header_len;
  67 
  68 }
  69         
  70 int tr_rebuild_header(void *buff, struct device *dev, unsigned long dest,
     /* [previous][next][first][last][top][bottom][index][help] */
  71                                                          struct sk_buff *skb) {
  72 
  73         struct trh_hdr *trh=(struct trh_hdr *)buff;
  74         struct trllc *trllc=(struct trllc *)(buff+sizeof(struct trh_hdr));
  75 
  76         if(trllc->ethertype != htons(ETH_P_IP)) {
  77                 printk("tr_rebuild_header: Don't know how to resolve type %04X addresses ?\n",(unsigned int)htons(      trllc->ethertype));
  78                 return 0;
  79         }
  80 
  81         if(arp_find(trh->daddr, dest, dev, dev->pa_addr, skb)) {
  82                         return 1;
  83         }
  84         else {  
  85                 tr_source_route(trh,dev); 
  86                 return 0;
  87         }
  88 }
  89         
  90 unsigned short tr_type_trans(struct sk_buff *skb, struct device *dev) {
     /* [previous][next][first][last][top][bottom][index][help] */
  91 
  92         struct trh_hdr *trh=(struct trh_hdr *)skb->data;
  93         struct trllc *trllc=(struct trllc *)(skb->data+sizeof(struct trh_hdr));
  94         
  95         if(trh->saddr[0] & TR_RII)
  96                 tr_add_rif_info(trh);
  97 
  98         if(*trh->daddr & 1) 
  99         {
 100                 if(!memcmp(trh->daddr,dev->broadcast,TR_ALEN))  
 101                         skb->pkt_type=PACKET_BROADCAST;
 102                 else
 103                         skb->pkt_type=PACKET_MULTICAST;
 104         }
 105 
 106         else if(dev->flags & IFF_PROMISC) 
 107         {
 108                 if(memcmp(trh->daddr, dev->dev_addr, TR_ALEN))
 109                         skb->pkt_type=PACKET_OTHERHOST;
 110         }
 111 
 112         return trllc->ethertype;
 113 }
 114 
 115 /* We try to do source routing... */
 116 
 117 static void tr_source_route(struct trh_hdr *trh,struct device *dev) {
     /* [previous][next][first][last][top][bottom][index][help] */
 118 
 119         int i;
 120         unsigned int hash;
 121         rif_cache entry;
 122 
 123         /* Broadcasts are single route as stated in RFC 1042 */
 124         if(!memcmp(&(trh->daddr[0]),&(dev->broadcast[0]),TR_ALEN)) {
 125                 trh->rcf=htons((((sizeof(trh->rcf)) << 8) & TR_RCF_LEN_MASK)  
 126                                | TR_RCF_FRAME2K | TR_RCF_LIMITED_BROADCAST);
 127                 trh->saddr[0]|=TR_RII;
 128         }
 129         else {
 130                 for(i=0,hash=0;i<TR_ALEN;hash+=trh->daddr[i++]);
 131                 hash&=RIF_TABLE_SIZE-1;
 132                 for(entry=rif_table[hash];entry && memcmp(&(entry->addr[0]),&(trh->daddr[0]),TR_ALEN);entry=entry->next);
 133 
 134                 if(entry) {
 135 #if 0
 136 printk("source routing for %02X %02X %02X %02X %02X %02X\n",trh->daddr[0],
 137                   trh->daddr[1],trh->daddr[2],trh->daddr[3],trh->daddr[4],trh->daddr[5]);
 138 #endif
 139                         if((ntohs(entry->rcf) & TR_RCF_LEN_MASK) >> 8) {
 140                                 trh->rcf=entry->rcf;
 141                                 memcpy(&trh->rseg[0],&entry->rseg[0],8*sizeof(unsigned short));
 142                                 trh->rcf^=htons(TR_RCF_DIR_BIT);        
 143                                 trh->rcf&=htons(0x1fff);        /* Issam Chehab <ichehab@madge1.demon.co.uk> */
 144 
 145                                 trh->saddr[0]|=TR_RII;
 146                                 entry->last_used=jiffies;
 147                         }
 148                 }
 149                 else {
 150                         trh->rcf=htons((((sizeof(trh->rcf)) << 8) & TR_RCF_LEN_MASK)  
 151                                        | TR_RCF_FRAME2K | TR_RCF_LIMITED_BROADCAST);
 152                         trh->saddr[0]|=TR_RII;
 153                 }
 154         }
 155                         
 156 }
 157 
 158 static void tr_add_rif_info(struct trh_hdr *trh) {
     /* [previous][next][first][last][top][bottom][index][help] */
 159 
 160         int i;
 161         unsigned int hash;
 162         rif_cache entry;
 163 
 164 
 165         trh->saddr[0]&=0x7f;
 166         for(i=0,hash=0;i<TR_ALEN;hash+=trh->saddr[i++]);
 167         hash&=RIF_TABLE_SIZE-1;
 168 #if 0
 169         printk("hash: %d\n",hash);
 170 #endif
 171         for(entry=rif_table[hash];entry && memcmp(&(entry->addr[0]),&(trh->saddr[0]),TR_ALEN);entry=entry->next);
 172 
 173         if(entry==NULL) {
 174 #if 0
 175 printk("adding rif_entry: addr:%02X:%02X:%02X:%02X:%02X:%02X rcf:%04X\n",
 176                 trh->saddr[0],trh->saddr[1],trh->saddr[2],
 177                 trh->saddr[3],trh->saddr[4],trh->saddr[5],
 178                 trh->rcf);
 179 #endif
 180                 entry=kmalloc(sizeof(struct rif_cache_s),GFP_ATOMIC);
 181                 if(!entry) {
 182                         printk("tr.c: Couldn't malloc rif cache entry !\n");
 183                         return;
 184                 }
 185                 entry->rcf=trh->rcf;
 186                 memcpy(&(entry->rseg[0]),&(trh->rseg[0]),8*sizeof(unsigned short));
 187                 memcpy(&(entry->addr[0]),&(trh->saddr[0]),TR_ALEN);
 188                 entry->next=rif_table[hash];
 189                 entry->last_used=jiffies;
 190                 rif_table[hash]=entry;
 191         }
 192 /* Y. Tahara added */
 193    else {                                       
 194                 if ( entry->rcf != trh->rcf ) {               
 195                                 if (!(trh->rcf & htons(TR_RCF_BROADCAST_MASK))) {
 196 #if 0
 197 printk("updating rif_entry: addr:%02X:%02X:%02X:%02X:%02X:%02X rcf:%04X\n",
 198                 trh->saddr[0],trh->saddr[1],trh->saddr[2],
 199                 trh->saddr[3],trh->saddr[4],trh->saddr[5],
 200                 trh->rcf);
 201 #endif
 202                        entry->rcf = trh->rcf;                  
 203                             memcpy(&(entry->rseg[0]),&(trh->rseg[0]),8*sizeof(unsigned short));
 204                          entry->last_used=jiffies;               
 205                                 }                                          
 206                 }                                             
 207         }
 208 
 209 }
 210 
 211 static void rif_check_expire(unsigned long dummy) {
     /* [previous][next][first][last][top][bottom][index][help] */
 212 
 213         int i;
 214         unsigned long now=jiffies,flags;
 215 
 216         save_flags(flags);
 217         cli();
 218 
 219         for(i=0; i < RIF_TABLE_SIZE;i++) {
 220 
 221         rif_cache entry, *pentry=rif_table+i;   
 222 
 223                 while((entry=*pentry)) 
 224                         if((now-entry->last_used) > RIF_TIMEOUT) {
 225                                 *pentry=entry->next;
 226                                 kfree_s(entry,sizeof(struct rif_cache_s));
 227                         }
 228                         else
 229                                 pentry=&entry->next;    
 230         }
 231         restore_flags(flags);
 232 
 233         del_timer(&rif_timer);
 234         rif_timer.expires=RIF_CHECK_INTERVAL;
 235         add_timer(&rif_timer);
 236 
 237 }
 238 
 239 int rif_get_info(char *buffer,char **start, off_t offset, int length) {
     /* [previous][next][first][last][top][bottom][index][help] */
 240 
 241    int len=0;
 242    off_t begin=0;
 243    off_t pos=0;
 244    int size,i;
 245 
 246    rif_cache entry;
 247 
 248         size=sprintf(buffer,
 249 "   TR address     rcf             routing segments             TTL\n\n");
 250    pos+=size;
 251    len+=size;
 252 
 253         for(i=0;i < RIF_TABLE_SIZE;i++) {
 254                 for(entry=rif_table[i];entry;entry=entry->next) {
 255                         size=sprintf(buffer+len,"%02X:%02X:%02X:%02X:%02X:%02X %04X %04X %04X %04X %04X %04X %04X %04X %04X %lu\n",
 256                                                                 entry->addr[0],entry->addr[1],entry->addr[2],entry->addr[3],entry->addr[4],entry->addr[5],
 257                                                                 entry->rcf,entry->rseg[0],entry->rseg[1],entry->rseg[2],entry->rseg[3],
 258                                                                 entry->rseg[4],entry->rseg[5],entry->rseg[6],entry->rseg[7],jiffies-entry->last_used); 
 259                         len+=size;
 260                         pos=begin+len;
 261 
 262                         if(pos<offset) {
 263                                 len=0;
 264                                 begin=pos;
 265                         }
 266                         if(pos>offset+length)
 267                                 break;
 268         }
 269                 if(pos>offset+length)
 270                         break;
 271         }
 272 
 273    *start=buffer+(offset-begin); /* Start of wanted data */
 274    len-=(offset-begin);    /* Start slop */
 275    if(len>length)
 276       len=length;    /* Ending slop */
 277    return len;
 278 }
 279 
 280 void rif_init(struct net_proto *unused) {
     /* [previous][next][first][last][top][bottom][index][help] */
 281 
 282         add_timer(&rif_timer);
 283 
 284 }
 285 

/* [previous][next][first][last][top][bottom][index][help] */