tr.c 15 KB

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