tr.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:13k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

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