tr.c 15 KB

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