rfc1201.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. /*
  2. * Linux ARCnet driver - RFC1201 (standard) packet encapsulation
  3. *
  4. * Written 1994-1999 by Avery Pennarun.
  5. * Derived from skeleton.c by Donald Becker.
  6. *
  7. * Special thanks to Contemporary Controls, Inc. (www.ccontrols.com)
  8. * for sponsoring the further development of this driver.
  9. *
  10. * **********************
  11. *
  12. * The original copyright of skeleton.c was as follows:
  13. *
  14. * skeleton.c Written 1993 by Donald Becker.
  15. * Copyright 1993 United States Government as represented by the
  16. * Director, National Security Agency. This software may only be used
  17. * and distributed according to the terms of the GNU General Public License as
  18. * modified by SRC, incorporated herein by reference.
  19. *
  20. * **********************
  21. *
  22. * For more details, see drivers/net/arcnet.c
  23. *
  24. * **********************
  25. */
  26. #include <linux/module.h>
  27. #include <linux/init.h>
  28. #include <linux/if_arp.h>
  29. #include <linux/netdevice.h>
  30. #include <linux/skbuff.h>
  31. #include <linux/arcdevice.h>
  32. MODULE_LICENSE("GPL");
  33. #define VERSION "arcnet: RFC1201 \"standard\" (`a') encapsulation support loaded.\n"
  34. static unsigned short type_trans(struct sk_buff *skb, struct net_device *dev);
  35. static void rx(struct net_device *dev, int bufnum,
  36. struct archdr *pkthdr, int length);
  37. static int build_header(struct sk_buff *skb, struct net_device *dev,
  38. unsigned short type, uint8_t daddr);
  39. static int prepare_tx(struct net_device *dev, struct archdr *pkt, int length,
  40. int bufnum);
  41. static int continue_tx(struct net_device *dev, int bufnum);
  42. struct ArcProto rfc1201_proto =
  43. {
  44. .suffix = 'a',
  45. .mtu = 1500, /* could be more, but some receivers can't handle it... */
  46. .is_ip = 1, /* This is for sending IP and ARP packages */
  47. .rx = rx,
  48. .build_header = build_header,
  49. .prepare_tx = prepare_tx,
  50. .continue_tx = continue_tx,
  51. .ack_tx = NULL
  52. };
  53. static int __init arcnet_rfc1201_init(void)
  54. {
  55. printk(VERSION);
  56. arc_proto_map[ARC_P_IP]
  57. = arc_proto_map[ARC_P_IPV6]
  58. = arc_proto_map[ARC_P_ARP]
  59. = arc_proto_map[ARC_P_RARP]
  60. = arc_proto_map[ARC_P_IPX]
  61. = arc_proto_map[ARC_P_NOVELL_EC]
  62. = &rfc1201_proto;
  63. /* if someone else already owns the broadcast, we won't take it */
  64. if (arc_bcast_proto == arc_proto_default)
  65. arc_bcast_proto = &rfc1201_proto;
  66. return 0;
  67. }
  68. static void __exit arcnet_rfc1201_exit(void)
  69. {
  70. arcnet_unregister_proto(&rfc1201_proto);
  71. }
  72. module_init(arcnet_rfc1201_init);
  73. module_exit(arcnet_rfc1201_exit);
  74. /*
  75. * Determine a packet's protocol ID.
  76. *
  77. * With ARCnet we have to convert everything to Ethernet-style stuff.
  78. */
  79. static unsigned short type_trans(struct sk_buff *skb, struct net_device *dev)
  80. {
  81. struct archdr *pkt = (struct archdr *) skb->data;
  82. struct arc_rfc1201 *soft = &pkt->soft.rfc1201;
  83. struct arcnet_local *lp = dev->priv;
  84. int hdr_size = ARC_HDR_SIZE + RFC1201_HDR_SIZE;
  85. /* Pull off the arcnet header. */
  86. skb->mac.raw = skb->data;
  87. skb_pull(skb, hdr_size);
  88. if (pkt->hard.dest == 0)
  89. skb->pkt_type = PACKET_BROADCAST;
  90. else if (dev->flags & IFF_PROMISC) {
  91. /* if we're not sending to ourselves :) */
  92. if (pkt->hard.dest != dev->dev_addr[0])
  93. skb->pkt_type = PACKET_OTHERHOST;
  94. }
  95. /* now return the protocol number */
  96. switch (soft->proto) {
  97. case ARC_P_IP:
  98. return htons(ETH_P_IP);
  99. case ARC_P_IPV6:
  100. return htons(ETH_P_IPV6);
  101. case ARC_P_ARP:
  102. return htons(ETH_P_ARP);
  103. case ARC_P_RARP:
  104. return htons(ETH_P_RARP);
  105. case ARC_P_IPX:
  106. case ARC_P_NOVELL_EC:
  107. return htons(ETH_P_802_3);
  108. default:
  109. lp->stats.rx_errors++;
  110. lp->stats.rx_crc_errors++;
  111. return 0;
  112. }
  113. return htons(ETH_P_IP);
  114. }
  115. /* packet receiver */
  116. static void rx(struct net_device *dev, int bufnum,
  117. struct archdr *pkthdr, int length)
  118. {
  119. struct arcnet_local *lp = dev->priv;
  120. struct sk_buff *skb;
  121. struct archdr *pkt = pkthdr;
  122. struct arc_rfc1201 *soft = &pkthdr->soft.rfc1201;
  123. int saddr = pkt->hard.source, ofs;
  124. struct Incoming *in = &lp->rfc1201.incoming[saddr];
  125. BUGMSG(D_DURING, "it's an RFC1201 packet (length=%d)\n", length);
  126. if (length >= MinTU)
  127. ofs = 512 - length;
  128. else
  129. ofs = 256 - length;
  130. if (soft->split_flag == 0xFF) { /* Exception Packet */
  131. if (length >= 4 + RFC1201_HDR_SIZE)
  132. BUGMSG(D_DURING, "compensating for exception packet\n");
  133. else {
  134. BUGMSG(D_EXTRA, "short RFC1201 exception packet from %02Xh",
  135. saddr);
  136. return;
  137. }
  138. /* skip over 4-byte junkola */
  139. length -= 4;
  140. ofs += 4;
  141. lp->hw.copy_from_card(dev, bufnum, 512 - length,
  142. soft, sizeof(pkt->soft));
  143. }
  144. if (!soft->split_flag) { /* not split */
  145. BUGMSG(D_RX, "incoming is not split (splitflag=%d)\n",
  146. soft->split_flag);
  147. if (in->skb) { /* already assembling one! */
  148. BUGMSG(D_EXTRA, "aborting assembly (seq=%d) for unsplit packet (splitflag=%d, seq=%d)\n",
  149. in->sequence, soft->split_flag, soft->sequence);
  150. lp->rfc1201.aborted_seq = soft->sequence;
  151. dev_kfree_skb_irq(in->skb);
  152. lp->stats.rx_errors++;
  153. lp->stats.rx_missed_errors++;
  154. in->skb = NULL;
  155. }
  156. in->sequence = soft->sequence;
  157. skb = alloc_skb(length + ARC_HDR_SIZE, GFP_ATOMIC);
  158. if (skb == NULL) {
  159. BUGMSG(D_NORMAL, "Memory squeeze, dropping packet.\n");
  160. lp->stats.rx_dropped++;
  161. return;
  162. }
  163. skb_put(skb, length + ARC_HDR_SIZE);
  164. skb->dev = dev;
  165. pkt = (struct archdr *) skb->data;
  166. soft = &pkt->soft.rfc1201;
  167. /* up to sizeof(pkt->soft) has already been copied from the card */
  168. memcpy(pkt, pkthdr, sizeof(struct archdr));
  169. if (length > sizeof(pkt->soft))
  170. lp->hw.copy_from_card(dev, bufnum, ofs + sizeof(pkt->soft),
  171. pkt->soft.raw + sizeof(pkt->soft),
  172. length - sizeof(pkt->soft));
  173. /*
  174. * ARP packets have problems when sent from some DOS systems: the
  175. * source address is always 0! So we take the hardware source addr
  176. * (which is impossible to fumble) and insert it ourselves.
  177. */
  178. if (soft->proto == ARC_P_ARP) {
  179. struct arphdr *arp = (struct arphdr *) soft->payload;
  180. /* make sure addresses are the right length */
  181. if (arp->ar_hln == 1 && arp->ar_pln == 4) {
  182. uint8_t *cptr = (uint8_t *) arp + sizeof(struct arphdr);
  183. if (!*cptr) { /* is saddr = 00? */
  184. BUGMSG(D_EXTRA,
  185. "ARP source address was 00h, set to %02Xh.\n",
  186. saddr);
  187. lp->stats.rx_crc_errors++;
  188. *cptr = saddr;
  189. } else {
  190. BUGMSG(D_DURING, "ARP source address (%Xh) is fine.\n",
  191. *cptr);
  192. }
  193. } else {
  194. BUGMSG(D_NORMAL, "funny-shaped ARP packet. (%Xh, %Xh)\n",
  195. arp->ar_hln, arp->ar_pln);
  196. lp->stats.rx_errors++;
  197. lp->stats.rx_crc_errors++;
  198. }
  199. }
  200. BUGLVL(D_SKB) arcnet_dump_skb(dev, skb, "rx");
  201. skb->protocol = type_trans(skb, dev);
  202. netif_rx(skb);
  203. dev->last_rx = jiffies;
  204. } else { /* split packet */
  205. /*
  206. * NOTE: MSDOS ARP packet correction should only need to apply to
  207. * unsplit packets, since ARP packets are so short.
  208. *
  209. * My interpretation of the RFC1201 document is that if a packet is
  210. * received out of order, the entire assembly process should be
  211. * aborted.
  212. *
  213. * The RFC also mentions "it is possible for successfully received
  214. * packets to be retransmitted." As of 0.40 all previously received
  215. * packets are allowed, not just the most recent one.
  216. *
  217. * We allow multiple assembly processes, one for each ARCnet card
  218. * possible on the network. Seems rather like a waste of memory,
  219. * but there's no other way to be reliable.
  220. */
  221. BUGMSG(D_RX, "packet is split (splitflag=%d, seq=%d)\n",
  222. soft->split_flag, in->sequence);
  223. if (in->skb && in->sequence != soft->sequence) {
  224. BUGMSG(D_EXTRA, "wrong seq number (saddr=%d, expected=%d, seq=%d, splitflag=%d)\n",
  225. saddr, in->sequence, soft->sequence,
  226. soft->split_flag);
  227. dev_kfree_skb_irq(in->skb);
  228. in->skb = NULL;
  229. lp->stats.rx_errors++;
  230. lp->stats.rx_missed_errors++;
  231. in->lastpacket = in->numpackets = 0;
  232. }
  233. if (soft->split_flag & 1) { /* first packet in split */
  234. BUGMSG(D_RX, "brand new splitpacket (splitflag=%d)\n",
  235. soft->split_flag);
  236. if (in->skb) { /* already assembling one! */
  237. BUGMSG(D_EXTRA, "aborting previous (seq=%d) assembly "
  238. "(splitflag=%d, seq=%d)\n",
  239. in->sequence, soft->split_flag,
  240. soft->sequence);
  241. lp->stats.rx_errors++;
  242. lp->stats.rx_missed_errors++;
  243. dev_kfree_skb_irq(in->skb);
  244. }
  245. in->sequence = soft->sequence;
  246. in->numpackets = ((unsigned) soft->split_flag >> 1) + 2;
  247. in->lastpacket = 1;
  248. if (in->numpackets > 16) {
  249. BUGMSG(D_EXTRA, "incoming packet more than 16 segments; dropping. (splitflag=%d)\n",
  250. soft->split_flag);
  251. lp->rfc1201.aborted_seq = soft->sequence;
  252. lp->stats.rx_errors++;
  253. lp->stats.rx_length_errors++;
  254. return;
  255. }
  256. in->skb = skb = alloc_skb(508 * in->numpackets + ARC_HDR_SIZE,
  257. GFP_ATOMIC);
  258. if (skb == NULL) {
  259. BUGMSG(D_NORMAL, "(split) memory squeeze, dropping packet.\n");
  260. lp->rfc1201.aborted_seq = soft->sequence;
  261. lp->stats.rx_dropped++;
  262. return;
  263. }
  264. skb->dev = dev;
  265. pkt = (struct archdr *) skb->data;
  266. soft = &pkt->soft.rfc1201;
  267. memcpy(pkt, pkthdr, ARC_HDR_SIZE + RFC1201_HDR_SIZE);
  268. skb_put(skb, ARC_HDR_SIZE + RFC1201_HDR_SIZE);
  269. soft->split_flag = 0; /* end result won't be split */
  270. } else { /* not first packet */
  271. int packetnum = ((unsigned) soft->split_flag >> 1) + 1;
  272. /*
  273. * if we're not assembling, there's no point trying to
  274. * continue.
  275. */
  276. if (!in->skb) {
  277. if (lp->rfc1201.aborted_seq != soft->sequence) {
  278. BUGMSG(D_EXTRA, "can't continue split without starting "
  279. "first! (splitflag=%d, seq=%d, aborted=%d)\n",
  280. soft->split_flag, soft->sequence,
  281. lp->rfc1201.aborted_seq);
  282. lp->stats.rx_errors++;
  283. lp->stats.rx_missed_errors++;
  284. }
  285. return;
  286. }
  287. in->lastpacket++;
  288. if (packetnum != in->lastpacket) { /* not the right flag! */
  289. /* harmless duplicate? ignore. */
  290. if (packetnum <= in->lastpacket - 1) {
  291. BUGMSG(D_EXTRA, "duplicate splitpacket ignored! (splitflag=%d)\n",
  292. soft->split_flag);
  293. lp->stats.rx_errors++;
  294. lp->stats.rx_frame_errors++;
  295. return;
  296. }
  297. /* "bad" duplicate, kill reassembly */
  298. BUGMSG(D_EXTRA, "out-of-order splitpacket, reassembly "
  299. "(seq=%d) aborted (splitflag=%d, seq=%d)\n",
  300. in->sequence, soft->split_flag, soft->sequence);
  301. lp->rfc1201.aborted_seq = soft->sequence;
  302. dev_kfree_skb_irq(in->skb);
  303. in->skb = NULL;
  304. lp->stats.rx_errors++;
  305. lp->stats.rx_missed_errors++;
  306. in->lastpacket = in->numpackets = 0;
  307. return;
  308. }
  309. pkt = (struct archdr *) in->skb->data;
  310. soft = &pkt->soft.rfc1201;
  311. }
  312. skb = in->skb;
  313. lp->hw.copy_from_card(dev, bufnum, ofs + RFC1201_HDR_SIZE,
  314. skb->data + skb->len,
  315. length - RFC1201_HDR_SIZE);
  316. skb_put(skb, length - RFC1201_HDR_SIZE);
  317. /* are we done? */
  318. if (in->lastpacket == in->numpackets) {
  319. in->skb = NULL;
  320. in->lastpacket = in->numpackets = 0;
  321. BUGMSG(D_SKB_SIZE, "skb: received %d bytes from %02X (unsplit)\n",
  322. skb->len, pkt->hard.source);
  323. BUGMSG(D_SKB_SIZE, "skb: received %d bytes from %02X (split)\n",
  324. skb->len, pkt->hard.source);
  325. BUGLVL(D_SKB) arcnet_dump_skb(dev, skb, "rx");
  326. skb->protocol = type_trans(skb, dev);
  327. netif_rx(skb);
  328. dev->last_rx = jiffies;
  329. }
  330. }
  331. }
  332. /* Create the ARCnet hard/soft headers for RFC1201. */
  333. static int build_header(struct sk_buff *skb, struct net_device *dev,
  334. unsigned short type, uint8_t daddr)
  335. {
  336. struct arcnet_local *lp = dev->priv;
  337. int hdr_size = ARC_HDR_SIZE + RFC1201_HDR_SIZE;
  338. struct archdr *pkt = (struct archdr *) skb_push(skb, hdr_size);
  339. struct arc_rfc1201 *soft = &pkt->soft.rfc1201;
  340. /* set the protocol ID according to RFC1201 */
  341. switch (type) {
  342. case ETH_P_IP:
  343. soft->proto = ARC_P_IP;
  344. break;
  345. case ETH_P_IPV6:
  346. soft->proto = ARC_P_IPV6;
  347. break;
  348. case ETH_P_ARP:
  349. soft->proto = ARC_P_ARP;
  350. break;
  351. case ETH_P_RARP:
  352. soft->proto = ARC_P_RARP;
  353. break;
  354. case ETH_P_IPX:
  355. case ETH_P_802_3:
  356. case ETH_P_802_2:
  357. soft->proto = ARC_P_IPX;
  358. break;
  359. case ETH_P_ATALK:
  360. soft->proto = ARC_P_ATALK;
  361. break;
  362. default:
  363. BUGMSG(D_NORMAL, "RFC1201: I don't understand protocol %d (%Xh)\n",
  364. type, type);
  365. lp->stats.tx_errors++;
  366. lp->stats.tx_aborted_errors++;
  367. return 0;
  368. }
  369. /*
  370. * Set the source hardware address.
  371. *
  372. * This is pretty pointless for most purposes, but it can help in
  373. * debugging. ARCnet does not allow us to change the source address in
  374. * the actual packet sent)
  375. */
  376. pkt->hard.source = *dev->dev_addr;
  377. soft->sequence = htons(lp->rfc1201.sequence++);
  378. soft->split_flag = 0; /* split packets are done elsewhere */
  379. /* see linux/net/ethernet/eth.c to see where I got the following */
  380. if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) {
  381. /*
  382. * FIXME: fill in the last byte of the dest ipaddr here to better
  383. * comply with RFC1051 in "noarp" mode. For now, always broadcasting
  384. * will probably at least get packets sent out :)
  385. */
  386. pkt->hard.dest = 0;
  387. return hdr_size;
  388. }
  389. /* otherwise, drop in the dest address */
  390. pkt->hard.dest = daddr;
  391. return hdr_size;
  392. }
  393. static void load_pkt(struct net_device *dev, struct arc_hardware *hard,
  394. struct arc_rfc1201 *soft, int softlen, int bufnum)
  395. {
  396. struct arcnet_local *lp = dev->priv;
  397. int ofs;
  398. /* assume length <= XMTU: someone should have handled that by now. */
  399. if (softlen > MinTU) {
  400. hard->offset[0] = 0;
  401. hard->offset[1] = ofs = 512 - softlen;
  402. } else if (softlen > MTU) { /* exception packet - add an extra header */
  403. struct arc_rfc1201 excsoft;
  404. excsoft.proto = soft->proto;
  405. excsoft.split_flag = 0xff;
  406. excsoft.sequence = 0xffff;
  407. hard->offset[0] = 0;
  408. ofs = 512 - softlen;
  409. hard->offset[1] = ofs - RFC1201_HDR_SIZE;
  410. lp->hw.copy_to_card(dev, bufnum, ofs - RFC1201_HDR_SIZE,
  411. &excsoft, RFC1201_HDR_SIZE);
  412. } else
  413. hard->offset[0] = ofs = 256 - softlen;
  414. lp->hw.copy_to_card(dev, bufnum, 0, hard, ARC_HDR_SIZE);
  415. lp->hw.copy_to_card(dev, bufnum, ofs, soft, softlen);
  416. lp->lastload_dest = hard->dest;
  417. }
  418. static int prepare_tx(struct net_device *dev, struct archdr *pkt, int length,
  419. int bufnum)
  420. {
  421. struct arcnet_local *lp = dev->priv;
  422. const int maxsegsize = XMTU - RFC1201_HDR_SIZE;
  423. struct Outgoing *out;
  424. BUGMSG(D_DURING, "prepare_tx: txbufs=%d/%d/%d\n",
  425. lp->next_tx, lp->cur_tx, bufnum);
  426. length -= ARC_HDR_SIZE; /* hard header is not included in packet length */
  427. pkt->soft.rfc1201.split_flag = 0;
  428. /* need to do a split packet? */
  429. if (length > XMTU) {
  430. out = &lp->outgoing;
  431. out->length = length - RFC1201_HDR_SIZE;
  432. out->dataleft = lp->outgoing.length;
  433. out->numsegs = (out->dataleft + maxsegsize - 1) / maxsegsize;
  434. out->segnum = 0;
  435. BUGMSG(D_DURING, "rfc1201 prep_tx: ready for %d-segment split "
  436. "(%d bytes, seq=%d)\n", out->numsegs, out->length,
  437. pkt->soft.rfc1201.sequence);
  438. return 0; /* not done */
  439. }
  440. /* just load the packet into the buffers and send it off */
  441. load_pkt(dev, &pkt->hard, &pkt->soft.rfc1201, length, bufnum);
  442. return 1; /* done */
  443. }
  444. static int continue_tx(struct net_device *dev, int bufnum)
  445. {
  446. struct arcnet_local *lp = dev->priv;
  447. struct Outgoing *out = &lp->outgoing;
  448. struct arc_hardware *hard = &out->pkt->hard;
  449. struct arc_rfc1201 *soft = &out->pkt->soft.rfc1201, *newsoft;
  450. int maxsegsize = XMTU - RFC1201_HDR_SIZE;
  451. int seglen;
  452. BUGMSG(D_DURING,
  453. "rfc1201 continue_tx: loading segment %d(+1) of %d (seq=%d)\n",
  454. out->segnum, out->numsegs, soft->sequence);
  455. /* the "new" soft header comes right before the data chunk */
  456. newsoft = (struct arc_rfc1201 *)
  457. (out->pkt->soft.raw + out->length - out->dataleft);
  458. if (!out->segnum) /* first packet; newsoft == soft */
  459. newsoft->split_flag = ((out->numsegs - 2) << 1) | 1;
  460. else {
  461. newsoft->split_flag = out->segnum << 1;
  462. newsoft->proto = soft->proto;
  463. newsoft->sequence = soft->sequence;
  464. }
  465. seglen = maxsegsize;
  466. if (seglen > out->dataleft)
  467. seglen = out->dataleft;
  468. out->dataleft -= seglen;
  469. load_pkt(dev, hard, newsoft, seglen + RFC1201_HDR_SIZE, bufnum);
  470. out->segnum++;
  471. if (out->segnum >= out->numsegs)
  472. return 1;
  473. else
  474. return 0;
  475. }