rfc1201.c 16 KB

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