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 <linux/module.h>
  19. #include <linux/types.h>
  20. #include <linux/kernel.h>
  21. #include <linux/jiffies.h>
  22. #include <linux/string.h>
  23. #include <linux/mm.h>
  24. #include <linux/socket.h>
  25. #include <linux/in.h>
  26. #include <linux/inet.h>
  27. #include <linux/netdevice.h>
  28. #include <linux/trdevice.h>
  29. #include <linux/skbuff.h>
  30. #include <linux/errno.h>
  31. #include <linux/timer.h>
  32. #include <linux/net.h>
  33. #include <linux/proc_fs.h>
  34. #include <linux/seq_file.h>
  35. #include <linux/init.h>
  36. #include <linux/sysctl.h>
  37. #include <linux/slab.h>
  38. #include <net/arp.h>
  39. #include <net/net_namespace.h>
  40. static void tr_add_rif_info(struct trh_hdr *trh, struct net_device *dev);
  41. static void rif_check_expire(unsigned long dummy);
  42. #define TR_SR_DEBUG 0
  43. /*
  44. * Each RIF entry we learn is kept this way
  45. */
  46. struct rif_cache {
  47. unsigned char addr[TR_ALEN];
  48. int iface;
  49. __be16 rcf;
  50. __be16 rseg[8];
  51. struct rif_cache *next;
  52. unsigned long last_used;
  53. unsigned char local_ring;
  54. };
  55. #define RIF_TABLE_SIZE 32
  56. /*
  57. * We hash the RIF cache 32 ways. We do after all have to look it
  58. * up a lot.
  59. */
  60. static struct rif_cache *rif_table[RIF_TABLE_SIZE];
  61. static DEFINE_SPINLOCK(rif_lock);
  62. /*
  63. * Garbage disposal timer.
  64. */
  65. static struct timer_list rif_timer;
  66. static int sysctl_tr_rif_timeout = 60*10*HZ;
  67. static inline unsigned long rif_hash(const unsigned char *addr)
  68. {
  69. unsigned long x;
  70. x = addr[0];
  71. x = (x << 2) ^ addr[1];
  72. x = (x << 2) ^ addr[2];
  73. x = (x << 2) ^ addr[3];
  74. x = (x << 2) ^ addr[4];
  75. x = (x << 2) ^ addr[5];
  76. x ^= x >> 8;
  77. return x & (RIF_TABLE_SIZE - 1);
  78. }
  79. /*
  80. * Put the headers on a token ring packet. Token ring source routing
  81. * makes this a little more exciting than on ethernet.
  82. */
  83. static int tr_header(struct sk_buff *skb, struct net_device *dev,
  84. unsigned short type,
  85. const void *daddr, const void *saddr, unsigned int len)
  86. {
  87. struct trh_hdr *trh;
  88. int hdr_len;
  89. /*
  90. * Add the 802.2 SNAP header if IP as the IPv4/IPv6 code calls
  91. * dev->hard_header directly.
  92. */
  93. if (type == ETH_P_IP || type == ETH_P_IPV6 || type == ETH_P_ARP)
  94. {
  95. struct trllc *trllc;
  96. hdr_len = sizeof(struct trh_hdr) + sizeof(struct trllc);
  97. trh = (struct trh_hdr *)skb_push(skb, hdr_len);
  98. trllc = (struct trllc *)(trh+1);
  99. trllc->dsap = trllc->ssap = EXTENDED_SAP;
  100. trllc->llc = UI_CMD;
  101. trllc->protid[0] = trllc->protid[1] = trllc->protid[2] = 0x00;
  102. trllc->ethertype = htons(type);
  103. }
  104. else
  105. {
  106. hdr_len = sizeof(struct trh_hdr);
  107. trh = (struct trh_hdr *)skb_push(skb, hdr_len);
  108. }
  109. trh->ac=AC;
  110. trh->fc=LLC_FRAME;
  111. if(saddr)
  112. memcpy(trh->saddr,saddr,dev->addr_len);
  113. else
  114. memcpy(trh->saddr,dev->dev_addr,dev->addr_len);
  115. /*
  116. * Build the destination and then source route the frame
  117. */
  118. if(daddr)
  119. {
  120. memcpy(trh->daddr,daddr,dev->addr_len);
  121. tr_source_route(skb, trh, dev);
  122. return hdr_len;
  123. }
  124. return -hdr_len;
  125. }
  126. /*
  127. * A neighbour discovery of some species (eg arp) has completed. We
  128. * can now send the packet.
  129. */
  130. static int tr_rebuild_header(struct sk_buff *skb)
  131. {
  132. struct trh_hdr *trh=(struct trh_hdr *)skb->data;
  133. struct trllc *trllc=(struct trllc *)(skb->data+sizeof(struct trh_hdr));
  134. struct net_device *dev = skb->dev;
  135. /*
  136. * FIXME: We don't yet support IPv6 over token rings
  137. */
  138. if(trllc->ethertype != htons(ETH_P_IP)) {
  139. printk("tr_rebuild_header: Don't know how to resolve type %04X addresses ?\n", ntohs(trllc->ethertype));
  140. return 0;
  141. }
  142. #ifdef CONFIG_INET
  143. if(arp_find(trh->daddr, skb)) {
  144. return 1;
  145. }
  146. else
  147. #endif
  148. {
  149. tr_source_route(skb,trh,dev);
  150. return 0;
  151. }
  152. }
  153. /*
  154. * Some of this is a bit hackish. We intercept RIF information
  155. * used for source routing. We also grab IP directly and don't feed
  156. * it via SNAP.
  157. */
  158. __be16 tr_type_trans(struct sk_buff *skb, struct net_device *dev)
  159. {
  160. struct trh_hdr *trh;
  161. struct trllc *trllc;
  162. unsigned int riflen=0;
  163. skb->dev = dev;
  164. skb_reset_mac_header(skb);
  165. trh = tr_hdr(skb);
  166. if(trh->saddr[0] & TR_RII)
  167. riflen = (ntohs(trh->rcf) & TR_RCF_LEN_MASK) >> 8;
  168. trllc = (struct trllc *)(skb->data+sizeof(struct trh_hdr)-TR_MAXRIFLEN+riflen);
  169. skb_pull(skb,sizeof(struct trh_hdr)-TR_MAXRIFLEN+riflen);
  170. if(*trh->daddr & 0x80)
  171. {
  172. if(!memcmp(trh->daddr,dev->broadcast,TR_ALEN))
  173. skb->pkt_type=PACKET_BROADCAST;
  174. else
  175. skb->pkt_type=PACKET_MULTICAST;
  176. }
  177. else if ( (trh->daddr[0] & 0x01) && (trh->daddr[1] & 0x00) && (trh->daddr[2] & 0x5E))
  178. {
  179. skb->pkt_type=PACKET_MULTICAST;
  180. }
  181. else if(dev->flags & IFF_PROMISC)
  182. {
  183. if(memcmp(trh->daddr, dev->dev_addr, TR_ALEN))
  184. skb->pkt_type=PACKET_OTHERHOST;
  185. }
  186. if ((skb->pkt_type != PACKET_BROADCAST) &&
  187. (skb->pkt_type != PACKET_MULTICAST))
  188. tr_add_rif_info(trh,dev) ;
  189. /*
  190. * Strip the SNAP header from ARP packets since we don't
  191. * pass them through to the 802.2/SNAP layers.
  192. */
  193. if (trllc->dsap == EXTENDED_SAP &&
  194. (trllc->ethertype == htons(ETH_P_IP) ||
  195. trllc->ethertype == htons(ETH_P_IPV6) ||
  196. trllc->ethertype == htons(ETH_P_ARP)))
  197. {
  198. skb_pull(skb, sizeof(struct trllc));
  199. return trllc->ethertype;
  200. }
  201. return htons(ETH_P_TR_802_2);
  202. }
  203. /*
  204. * We try to do source routing...
  205. */
  206. void tr_source_route(struct sk_buff *skb,struct trh_hdr *trh,
  207. struct net_device *dev)
  208. {
  209. int slack;
  210. unsigned int hash;
  211. struct rif_cache *entry;
  212. unsigned char *olddata;
  213. unsigned long flags;
  214. static const unsigned char mcast_func_addr[]
  215. = {0xC0,0x00,0x00,0x04,0x00,0x00};
  216. spin_lock_irqsave(&rif_lock, flags);
  217. /*
  218. * Broadcasts are single route as stated in RFC 1042
  219. */
  220. if( (!memcmp(&(trh->daddr[0]),&(dev->broadcast[0]),TR_ALEN)) ||
  221. (!memcmp(&(trh->daddr[0]),&(mcast_func_addr[0]), TR_ALEN)) )
  222. {
  223. trh->rcf=htons((((sizeof(trh->rcf)) << 8) & TR_RCF_LEN_MASK)
  224. | TR_RCF_FRAME2K | TR_RCF_LIMITED_BROADCAST);
  225. trh->saddr[0]|=TR_RII;
  226. }
  227. else
  228. {
  229. hash = rif_hash(trh->daddr);
  230. /*
  231. * Walk the hash table and look for an entry
  232. */
  233. for(entry=rif_table[hash];entry && memcmp(&(entry->addr[0]),&(trh->daddr[0]),TR_ALEN);entry=entry->next);
  234. /*
  235. * If we found an entry we can route the frame.
  236. */
  237. if(entry)
  238. {
  239. #if TR_SR_DEBUG
  240. printk("source routing for %pM\n", trh->daddr);
  241. #endif
  242. if(!entry->local_ring && (ntohs(entry->rcf) & TR_RCF_LEN_MASK) >> 8)
  243. {
  244. trh->rcf=entry->rcf;
  245. memcpy(&trh->rseg[0],&entry->rseg[0],8*sizeof(unsigned short));
  246. trh->rcf^=htons(TR_RCF_DIR_BIT);
  247. trh->rcf&=htons(0x1fff); /* Issam Chehab <ichehab@madge1.demon.co.uk> */
  248. trh->saddr[0]|=TR_RII;
  249. #if TR_SR_DEBUG
  250. printk("entry found with rcf %04x\n", entry->rcf);
  251. }
  252. else
  253. {
  254. printk("entry found but without rcf length, local=%02x\n", entry->local_ring);
  255. #endif
  256. }
  257. entry->last_used=jiffies;
  258. }
  259. else
  260. {
  261. /*
  262. * Without the information we simply have to shout
  263. * on the wire. The replies should rapidly clean this
  264. * situation up.
  265. */
  266. trh->rcf=htons((((sizeof(trh->rcf)) << 8) & TR_RCF_LEN_MASK)
  267. | TR_RCF_FRAME2K | TR_RCF_LIMITED_BROADCAST);
  268. trh->saddr[0]|=TR_RII;
  269. #if TR_SR_DEBUG
  270. printk("no entry in rif table found - broadcasting frame\n");
  271. #endif
  272. }
  273. }
  274. /* Compress the RIF here so we don't have to do it in the driver(s) */
  275. if (!(trh->saddr[0] & 0x80))
  276. slack = 18;
  277. else
  278. slack = 18 - ((ntohs(trh->rcf) & TR_RCF_LEN_MASK)>>8);
  279. olddata = skb->data;
  280. spin_unlock_irqrestore(&rif_lock, flags);
  281. skb_pull(skb, slack);
  282. memmove(skb->data, olddata, sizeof(struct trh_hdr) - slack);
  283. }
  284. /*
  285. * We have learned some new RIF information for our source
  286. * routing.
  287. */
  288. static void tr_add_rif_info(struct trh_hdr *trh, struct net_device *dev)
  289. {
  290. unsigned int hash, rii_p = 0;
  291. unsigned long flags;
  292. struct rif_cache *entry;
  293. unsigned char saddr0;
  294. spin_lock_irqsave(&rif_lock, flags);
  295. saddr0 = trh->saddr[0];
  296. /*
  297. * Firstly see if the entry exists
  298. */
  299. if(trh->saddr[0] & TR_RII)
  300. {
  301. trh->saddr[0]&=0x7f;
  302. if (((ntohs(trh->rcf) & TR_RCF_LEN_MASK) >> 8) > 2)
  303. {
  304. rii_p = 1;
  305. }
  306. }
  307. hash = rif_hash(trh->saddr);
  308. for(entry=rif_table[hash];entry && memcmp(&(entry->addr[0]),&(trh->saddr[0]),TR_ALEN);entry=entry->next);
  309. if(entry==NULL)
  310. {
  311. #if TR_SR_DEBUG
  312. printk("adding rif_entry: addr:%pM rcf:%04X\n",
  313. trh->saddr, 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:%pM rcf:%04X\n",
  356. trh->saddr, 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. __acquires(&rif_lock)
  413. {
  414. spin_lock_irq(&rif_lock);
  415. return *pos ? rif_get_idx(*pos - 1) : SEQ_START_TOKEN;
  416. }
  417. static void *rif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  418. {
  419. int i;
  420. struct rif_cache *ent = v;
  421. ++*pos;
  422. if (v == SEQ_START_TOKEN) {
  423. i = -1;
  424. goto scan;
  425. }
  426. if (ent->next)
  427. return ent->next;
  428. i = rif_hash(ent->addr);
  429. scan:
  430. while (++i < RIF_TABLE_SIZE) {
  431. if ((ent = rif_table[i]) != NULL)
  432. return ent;
  433. }
  434. return NULL;
  435. }
  436. static void rif_seq_stop(struct seq_file *seq, void *v)
  437. __releases(&rif_lock)
  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(&init_net, entry->iface);
  450. long ttl = (long) (entry->last_used + sysctl_tr_rif_timeout)
  451. - (long) jiffies;
  452. seq_printf(seq, "%s %pM %7li ",
  453. dev?dev->name:"?",
  454. entry->addr,
  455. ttl/HZ);
  456. if (entry->local_ring)
  457. seq_puts(seq, "local\n");
  458. else {
  459. seq_printf(seq, "%04X", ntohs(entry->rcf));
  460. rcf_len = ((ntohs(entry->rcf) & TR_RCF_LEN_MASK)>>8)-2;
  461. if (rcf_len)
  462. rcf_len >>= 1;
  463. for(j = 1; j < rcf_len; j++) {
  464. if(j==1) {
  465. segment=ntohs(entry->rseg[j-1])>>4;
  466. seq_printf(seq," %03X",segment);
  467. }
  468. segment=ntohs(entry->rseg[j])>>4;
  469. brdgnmb=ntohs(entry->rseg[j-1])&0x00f;
  470. seq_printf(seq,"-%01X-%03X",brdgnmb,segment);
  471. }
  472. seq_putc(seq, '\n');
  473. }
  474. if (dev)
  475. dev_put(dev);
  476. }
  477. return 0;
  478. }
  479. static const struct seq_operations rif_seq_ops = {
  480. .start = rif_seq_start,
  481. .next = rif_seq_next,
  482. .stop = rif_seq_stop,
  483. .show = rif_seq_show,
  484. };
  485. static int rif_seq_open(struct inode *inode, struct file *file)
  486. {
  487. return seq_open(file, &rif_seq_ops);
  488. }
  489. static const struct file_operations rif_seq_fops = {
  490. .owner = THIS_MODULE,
  491. .open = rif_seq_open,
  492. .read = seq_read,
  493. .llseek = seq_lseek,
  494. .release = seq_release,
  495. };
  496. #endif
  497. static const struct header_ops tr_header_ops = {
  498. .create = tr_header,
  499. .rebuild= tr_rebuild_header,
  500. };
  501. static void tr_setup(struct net_device *dev)
  502. {
  503. /*
  504. * Configure and register
  505. */
  506. dev->header_ops = &tr_header_ops;
  507. dev->type = ARPHRD_IEEE802_TR;
  508. dev->hard_header_len = TR_HLEN;
  509. dev->mtu = 2000;
  510. dev->addr_len = TR_ALEN;
  511. dev->tx_queue_len = 100; /* Long queues on tr */
  512. memset(dev->broadcast,0xFF, TR_ALEN);
  513. /* New-style flags. */
  514. dev->flags = IFF_BROADCAST | IFF_MULTICAST ;
  515. }
  516. /**
  517. * alloc_trdev - Register token ring device
  518. * @sizeof_priv: Size of additional driver-private structure to be allocated
  519. * for this token ring device
  520. *
  521. * Fill in the fields of the device structure with token ring-generic values.
  522. *
  523. * Constructs a new net device, complete with a private data area of
  524. * size @sizeof_priv. A 32-byte (not bit) alignment is enforced for
  525. * this private data area.
  526. */
  527. struct net_device *alloc_trdev(int sizeof_priv)
  528. {
  529. return alloc_netdev(sizeof_priv, "tr%d", tr_setup);
  530. }
  531. #ifdef CONFIG_SYSCTL
  532. static struct ctl_table tr_table[] = {
  533. {
  534. .procname = "rif_timeout",
  535. .data = &sysctl_tr_rif_timeout,
  536. .maxlen = sizeof(int),
  537. .mode = 0644,
  538. .proc_handler = proc_dointvec
  539. },
  540. { },
  541. };
  542. static __initdata struct ctl_path tr_path[] = {
  543. { .procname = "net", },
  544. { .procname = "token-ring", },
  545. { }
  546. };
  547. #endif
  548. /*
  549. * Called during bootup. We don't actually have to initialise
  550. * too much for this.
  551. */
  552. static int __init rif_init(void)
  553. {
  554. rif_timer.expires = jiffies + sysctl_tr_rif_timeout;
  555. setup_timer(&rif_timer, rif_check_expire, 0);
  556. add_timer(&rif_timer);
  557. #ifdef CONFIG_SYSCTL
  558. register_sysctl_paths(tr_path, tr_table);
  559. #endif
  560. proc_net_fops_create(&init_net, "tr_rif", S_IRUGO, &rif_seq_fops);
  561. return 0;
  562. }
  563. module_init(rif_init);
  564. EXPORT_SYMBOL(tr_type_trans);
  565. EXPORT_SYMBOL(alloc_trdev);
  566. MODULE_LICENSE("GPL");