tr.c 15 KB

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