tr.c 15 KB

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