rfc1201.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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/gfp.h>
  27. #include <linux/module.h>
  28. #include <linux/init.h>
  29. #include <linux/if_arp.h>
  30. #include <linux/netdevice.h>
  31. #include <linux/skbuff.h>
  32. #include <linux/arcdevice.h>
  33. MODULE_LICENSE("GPL");
  34. #define VERSION "arcnet: RFC1201 \"standard\" (`a') encapsulation support loaded.\n"
  35. static __be16 type_trans(struct sk_buff *skb, struct net_device *dev);
  36. static void rx(struct net_device *dev, int bufnum,
  37. struct archdr *pkthdr, int length);
  38. static int build_header(struct sk_buff *skb, struct net_device *dev,
  39. unsigned short type, uint8_t daddr);
  40. static int prepare_tx(struct net_device *dev, struct archdr *pkt, int length,
  41. int bufnum);
  42. static int continue_tx(struct net_device *dev, int bufnum);
  43. static struct ArcProto rfc1201_proto =
  44. {
  45. .suffix = 'a',
  46. .mtu = 1500, /* could be more, but some receivers can't handle it... */
  47. .is_ip = 1, /* This is for sending IP and ARP packages */
  48. .rx = rx,
  49. .build_header = build_header,
  50. .prepare_tx = prepare_tx,
  51. .continue_tx = continue_tx,
  52. .ack_tx = NULL
  53. };
  54. static int __init arcnet_rfc1201_init(void)
  55. {
  56. printk(VERSION);
  57. arc_proto_map[ARC_P_IP]
  58. = arc_proto_map[ARC_P_IPV6]
  59. = arc_proto_map[ARC_P_ARP]
  60. = arc_proto_map[ARC_P_RARP]
  61. = arc_proto_map[ARC_P_IPX]
  62. = arc_proto_map[ARC_P_NOVELL_EC]
  63. = &rfc1201_proto;
  64. /* if someone else already owns the broadcast, we won't take it */
  65. if (arc_bcast_proto == arc_proto_default)
  66. arc_bcast_proto = &rfc1201_proto;
  67. return 0;
  68. }
  69. static void __exit arcnet_rfc1201_exit(void)
  70. {
  71. arcnet_unregister_proto(&rfc1201_proto);
  72. }
  73. module_init(arcnet_rfc1201_init);
  74. module_exit(arcnet_rfc1201_exit);
  75. /*
  76. * Determine a packet's protocol ID.
  77. *
  78. * With ARCnet we have to convert everything to Ethernet-style stuff.
  79. */
  80. static __be16 type_trans(struct sk_buff *skb, struct net_device *dev)
  81. {
  82. struct archdr *pkt = (struct archdr *) skb->data;
  83. struct arc_rfc1201 *soft = &pkt->soft.rfc1201;
  84. int hdr_size = ARC_HDR_SIZE + RFC1201_HDR_SIZE;
  85. /* Pull off the arcnet header. */
  86. skb_reset_mac_header(skb);
  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. dev->stats.rx_errors++;
  110. dev->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 = netdev_priv(dev);
  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. dev->stats.rx_errors++;
  153. dev->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. dev->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. dev->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. dev->stats.rx_errors++;
  197. dev->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. } else { /* split packet */
  204. /*
  205. * NOTE: MSDOS ARP packet correction should only need to apply to
  206. * unsplit packets, since ARP packets are so short.
  207. *
  208. * My interpretation of the RFC1201 document is that if a packet is
  209. * received out of order, the entire assembly process should be
  210. * aborted.
  211. *
  212. * The RFC also mentions "it is possible for successfully received
  213. * packets to be retransmitted." As of 0.40 all previously received
  214. * packets are allowed, not just the most recent one.
  215. *
  216. * We allow multiple assembly processes, one for each ARCnet card
  217. * possible on the network. Seems rather like a waste of memory,
  218. * but there's no other way to be reliable.
  219. */
  220. BUGMSG(D_RX, "packet is split (splitflag=%d, seq=%d)\n",
  221. soft->split_flag, in->sequence);
  222. if (in->skb && in->sequence != soft->sequence) {
  223. BUGMSG(D_EXTRA, "wrong seq number (saddr=%d, expected=%d, seq=%d, splitflag=%d)\n",
  224. saddr, in->sequence, soft->sequence,
  225. soft->split_flag);
  226. dev_kfree_skb_irq(in->skb);
  227. in->skb = NULL;
  228. dev->stats.rx_errors++;
  229. dev->stats.rx_missed_errors++;
  230. in->lastpacket = in->numpackets = 0;
  231. }
  232. if (soft->split_flag & 1) { /* first packet in split */
  233. BUGMSG(D_RX, "brand new splitpacket (splitflag=%d)\n",
  234. soft->split_flag);
  235. if (in->skb) { /* already assembling one! */
  236. BUGMSG(D_EXTRA, "aborting previous (seq=%d) assembly "
  237. "(splitflag=%d, seq=%d)\n",
  238. in->sequence, soft->split_flag,
  239. soft->sequence);
  240. dev->stats.rx_errors++;
  241. dev->stats.rx_missed_errors++;
  242. dev_kfree_skb_irq(in->skb);
  243. }
  244. in->sequence = soft->sequence;
  245. in->numpackets = ((unsigned) soft->split_flag >> 1) + 2;
  246. in->lastpacket = 1;
  247. if (in->numpackets > 16) {
  248. BUGMSG(D_EXTRA, "incoming packet more than 16 segments; dropping. (splitflag=%d)\n",
  249. soft->split_flag);
  250. lp->rfc1201.aborted_seq = soft->sequence;
  251. dev->stats.rx_errors++;
  252. dev->stats.rx_length_errors++;
  253. return;
  254. }
  255. in->skb = skb = alloc_skb(508 * in->numpackets + ARC_HDR_SIZE,
  256. GFP_ATOMIC);
  257. if (skb == NULL) {
  258. BUGMSG(D_NORMAL, "(split) memory squeeze, dropping packet.\n");
  259. lp->rfc1201.aborted_seq = soft->sequence;
  260. dev->stats.rx_dropped++;
  261. return;
  262. }
  263. skb->dev = dev;
  264. pkt = (struct archdr *) skb->data;
  265. soft = &pkt->soft.rfc1201;
  266. memcpy(pkt, pkthdr, ARC_HDR_SIZE + RFC1201_HDR_SIZE);
  267. skb_put(skb, ARC_HDR_SIZE + RFC1201_HDR_SIZE);
  268. soft->split_flag = 0; /* end result won't be split */
  269. } else { /* not first packet */
  270. int packetnum = ((unsigned) soft->split_flag >> 1) + 1;
  271. /*
  272. * if we're not assembling, there's no point trying to
  273. * continue.
  274. */
  275. if (!in->skb) {
  276. if (lp->rfc1201.aborted_seq != soft->sequence) {
  277. BUGMSG(D_EXTRA, "can't continue split without starting "
  278. "first! (splitflag=%d, seq=%d, aborted=%d)\n",
  279. soft->split_flag, soft->sequence,
  280. lp->rfc1201.aborted_seq);
  281. dev->stats.rx_errors++;
  282. dev->stats.rx_missed_errors++;
  283. }
  284. return;
  285. }
  286. in->lastpacket++;
  287. if (packetnum != in->lastpacket) { /* not the right flag! */
  288. /* harmless duplicate? ignore. */
  289. if (packetnum <= in->lastpacket - 1) {
  290. BUGMSG(D_EXTRA, "duplicate splitpacket ignored! (splitflag=%d)\n",
  291. soft->split_flag);
  292. dev->stats.rx_errors++;
  293. dev->stats.rx_frame_errors++;
  294. return;
  295. }
  296. /* "bad" duplicate, kill reassembly */
  297. BUGMSG(D_EXTRA, "out-of-order splitpacket, reassembly "
  298. "(seq=%d) aborted (splitflag=%d, seq=%d)\n",
  299. in->sequence, soft->split_flag, soft->sequence);
  300. lp->rfc1201.aborted_seq = soft->sequence;
  301. dev_kfree_skb_irq(in->skb);
  302. in->skb = NULL;
  303. dev->stats.rx_errors++;
  304. dev->stats.rx_missed_errors++;
  305. in->lastpacket = in->numpackets = 0;
  306. return;
  307. }
  308. pkt = (struct archdr *) in->skb->data;
  309. soft = &pkt->soft.rfc1201;
  310. }
  311. skb = in->skb;
  312. lp->hw.copy_from_card(dev, bufnum, ofs + RFC1201_HDR_SIZE,
  313. skb->data + skb->len,
  314. length - RFC1201_HDR_SIZE);
  315. skb_put(skb, length - RFC1201_HDR_SIZE);
  316. /* are we done? */
  317. if (in->lastpacket == in->numpackets) {
  318. in->skb = NULL;
  319. in->lastpacket = in->numpackets = 0;
  320. BUGMSG(D_SKB_SIZE, "skb: received %d bytes from %02X (unsplit)\n",
  321. skb->len, pkt->hard.source);
  322. BUGMSG(D_SKB_SIZE, "skb: received %d bytes from %02X (split)\n",
  323. skb->len, pkt->hard.source);
  324. BUGLVL(D_SKB) arcnet_dump_skb(dev, skb, "rx");
  325. skb->protocol = type_trans(skb, dev);
  326. netif_rx(skb);
  327. }
  328. }
  329. }
  330. /* Create the ARCnet hard/soft headers for RFC1201. */
  331. static int build_header(struct sk_buff *skb, struct net_device *dev,
  332. unsigned short type, uint8_t daddr)
  333. {
  334. struct arcnet_local *lp = netdev_priv(dev);
  335. int hdr_size = ARC_HDR_SIZE + RFC1201_HDR_SIZE;
  336. struct archdr *pkt = (struct archdr *) skb_push(skb, hdr_size);
  337. struct arc_rfc1201 *soft = &pkt->soft.rfc1201;
  338. /* set the protocol ID according to RFC1201 */
  339. switch (type) {
  340. case ETH_P_IP:
  341. soft->proto = ARC_P_IP;
  342. break;
  343. case ETH_P_IPV6:
  344. soft->proto = ARC_P_IPV6;
  345. break;
  346. case ETH_P_ARP:
  347. soft->proto = ARC_P_ARP;
  348. break;
  349. case ETH_P_RARP:
  350. soft->proto = ARC_P_RARP;
  351. break;
  352. case ETH_P_IPX:
  353. case ETH_P_802_3:
  354. case ETH_P_802_2:
  355. soft->proto = ARC_P_IPX;
  356. break;
  357. case ETH_P_ATALK:
  358. soft->proto = ARC_P_ATALK;
  359. break;
  360. default:
  361. BUGMSG(D_NORMAL, "RFC1201: I don't understand protocol %d (%Xh)\n",
  362. type, type);
  363. dev->stats.tx_errors++;
  364. dev->stats.tx_aborted_errors++;
  365. return 0;
  366. }
  367. /*
  368. * Set the source hardware address.
  369. *
  370. * This is pretty pointless for most purposes, but it can help in
  371. * debugging. ARCnet does not allow us to change the source address in
  372. * the actual packet sent)
  373. */
  374. pkt->hard.source = *dev->dev_addr;
  375. soft->sequence = htons(lp->rfc1201.sequence++);
  376. soft->split_flag = 0; /* split packets are done elsewhere */
  377. /* see linux/net/ethernet/eth.c to see where I got the following */
  378. if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) {
  379. /*
  380. * FIXME: fill in the last byte of the dest ipaddr here to better
  381. * comply with RFC1051 in "noarp" mode. For now, always broadcasting
  382. * will probably at least get packets sent out :)
  383. */
  384. pkt->hard.dest = 0;
  385. return hdr_size;
  386. }
  387. /* otherwise, drop in the dest address */
  388. pkt->hard.dest = daddr;
  389. return hdr_size;
  390. }
  391. static void load_pkt(struct net_device *dev, struct arc_hardware *hard,
  392. struct arc_rfc1201 *soft, int softlen, int bufnum)
  393. {
  394. struct arcnet_local *lp = netdev_priv(dev);
  395. int ofs;
  396. /* assume length <= XMTU: someone should have handled that by now. */
  397. if (softlen > MinTU) {
  398. hard->offset[0] = 0;
  399. hard->offset[1] = ofs = 512 - softlen;
  400. } else if (softlen > MTU) { /* exception packet - add an extra header */
  401. struct arc_rfc1201 excsoft;
  402. excsoft.proto = soft->proto;
  403. excsoft.split_flag = 0xff;
  404. excsoft.sequence = htons(0xffff);
  405. hard->offset[0] = 0;
  406. ofs = 512 - softlen;
  407. hard->offset[1] = ofs - RFC1201_HDR_SIZE;
  408. lp->hw.copy_to_card(dev, bufnum, ofs - RFC1201_HDR_SIZE,
  409. &excsoft, RFC1201_HDR_SIZE);
  410. } else
  411. hard->offset[0] = ofs = 256 - softlen;
  412. lp->hw.copy_to_card(dev, bufnum, 0, hard, ARC_HDR_SIZE);
  413. lp->hw.copy_to_card(dev, bufnum, ofs, soft, softlen);
  414. lp->lastload_dest = hard->dest;
  415. }
  416. static int prepare_tx(struct net_device *dev, struct archdr *pkt, int length,
  417. int bufnum)
  418. {
  419. struct arcnet_local *lp = netdev_priv(dev);
  420. const int maxsegsize = XMTU - RFC1201_HDR_SIZE;
  421. struct Outgoing *out;
  422. BUGMSG(D_DURING, "prepare_tx: txbufs=%d/%d/%d\n",
  423. lp->next_tx, lp->cur_tx, bufnum);
  424. length -= ARC_HDR_SIZE; /* hard header is not included in packet length */
  425. pkt->soft.rfc1201.split_flag = 0;
  426. /* need to do a split packet? */
  427. if (length > XMTU) {
  428. out = &lp->outgoing;
  429. out->length = length - RFC1201_HDR_SIZE;
  430. out->dataleft = lp->outgoing.length;
  431. out->numsegs = (out->dataleft + maxsegsize - 1) / maxsegsize;
  432. out->segnum = 0;
  433. BUGMSG(D_DURING, "rfc1201 prep_tx: ready for %d-segment split "
  434. "(%d bytes, seq=%d)\n", out->numsegs, out->length,
  435. pkt->soft.rfc1201.sequence);
  436. return 0; /* not done */
  437. }
  438. /* just load the packet into the buffers and send it off */
  439. load_pkt(dev, &pkt->hard, &pkt->soft.rfc1201, length, bufnum);
  440. return 1; /* done */
  441. }
  442. static int continue_tx(struct net_device *dev, int bufnum)
  443. {
  444. struct arcnet_local *lp = netdev_priv(dev);
  445. struct Outgoing *out = &lp->outgoing;
  446. struct arc_hardware *hard = &out->pkt->hard;
  447. struct arc_rfc1201 *soft = &out->pkt->soft.rfc1201, *newsoft;
  448. int maxsegsize = XMTU - RFC1201_HDR_SIZE;
  449. int seglen;
  450. BUGMSG(D_DURING,
  451. "rfc1201 continue_tx: loading segment %d(+1) of %d (seq=%d)\n",
  452. out->segnum, out->numsegs, soft->sequence);
  453. /* the "new" soft header comes right before the data chunk */
  454. newsoft = (struct arc_rfc1201 *)
  455. (out->pkt->soft.raw + out->length - out->dataleft);
  456. if (!out->segnum) /* first packet; newsoft == soft */
  457. newsoft->split_flag = ((out->numsegs - 2) << 1) | 1;
  458. else {
  459. newsoft->split_flag = out->segnum << 1;
  460. newsoft->proto = soft->proto;
  461. newsoft->sequence = soft->sequence;
  462. }
  463. seglen = maxsegsize;
  464. if (seglen > out->dataleft)
  465. seglen = out->dataleft;
  466. out->dataleft -= seglen;
  467. load_pkt(dev, hard, newsoft, seglen + RFC1201_HDR_SIZE, bufnum);
  468. out->segnum++;
  469. if (out->segnum >= out->numsegs)
  470. return 1;
  471. else
  472. return 0;
  473. }