rfc1201.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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. .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. /* Determine a packet's protocol ID.
  75. *
  76. * With ARCnet we have to convert everything to Ethernet-style stuff.
  77. */
  78. static __be16 type_trans(struct sk_buff *skb, struct net_device *dev)
  79. {
  80. struct archdr *pkt = (struct archdr *)skb->data;
  81. struct arc_rfc1201 *soft = &pkt->soft.rfc1201;
  82. int hdr_size = ARC_HDR_SIZE + RFC1201_HDR_SIZE;
  83. /* Pull off the arcnet header. */
  84. skb_reset_mac_header(skb);
  85. skb_pull(skb, hdr_size);
  86. if (pkt->hard.dest == 0) {
  87. skb->pkt_type = PACKET_BROADCAST;
  88. } else if (dev->flags & IFF_PROMISC) {
  89. /* if we're not sending to ourselves :) */
  90. if (pkt->hard.dest != dev->dev_addr[0])
  91. skb->pkt_type = PACKET_OTHERHOST;
  92. }
  93. /* now return the protocol number */
  94. switch (soft->proto) {
  95. case ARC_P_IP:
  96. return htons(ETH_P_IP);
  97. case ARC_P_IPV6:
  98. return htons(ETH_P_IPV6);
  99. case ARC_P_ARP:
  100. return htons(ETH_P_ARP);
  101. case ARC_P_RARP:
  102. return htons(ETH_P_RARP);
  103. case ARC_P_IPX:
  104. case ARC_P_NOVELL_EC:
  105. return htons(ETH_P_802_3);
  106. default:
  107. dev->stats.rx_errors++;
  108. dev->stats.rx_crc_errors++;
  109. return 0;
  110. }
  111. return htons(ETH_P_IP);
  112. }
  113. /* packet receiver */
  114. static void rx(struct net_device *dev, int bufnum,
  115. struct archdr *pkthdr, int length)
  116. {
  117. struct arcnet_local *lp = netdev_priv(dev);
  118. struct sk_buff *skb;
  119. struct archdr *pkt = pkthdr;
  120. struct arc_rfc1201 *soft = &pkthdr->soft.rfc1201;
  121. int saddr = pkt->hard.source, ofs;
  122. struct Incoming *in = &lp->rfc1201.incoming[saddr];
  123. arc_printk(D_DURING, dev, "it's an RFC1201 packet (length=%d)\n",
  124. 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. arc_printk(D_DURING, dev, "compensating for exception packet\n");
  132. } else {
  133. arc_printk(D_EXTRA, dev, "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. arc_printk(D_RX, dev, "incoming is not split (splitflag=%d)\n",
  145. soft->split_flag);
  146. if (in->skb) { /* already assembling one! */
  147. arc_printk(D_EXTRA, dev, "aborting assembly (seq=%d) for unsplit packet (splitflag=%d, seq=%d)\n",
  148. in->sequence, soft->split_flag,
  149. 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. arc_printk(D_NORMAL, dev, "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. /* ARP packets have problems when sent from some DOS systems:
  174. * the source address is always 0!
  175. * So we take the hardware source addr (which is impossible
  176. * 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. arc_printk(D_EXTRA, dev,
  185. "ARP source address was 00h, set to %02Xh\n",
  186. saddr);
  187. dev->stats.rx_crc_errors++;
  188. *cptr = saddr;
  189. } else {
  190. arc_printk(D_DURING, dev, "ARP source address (%Xh) is fine.\n",
  191. *cptr);
  192. }
  193. } else {
  194. arc_printk(D_NORMAL, dev, "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. if (BUGLVL(D_SKB))
  201. arcnet_dump_skb(dev, skb, "rx");
  202. skb->protocol = type_trans(skb, dev);
  203. netif_rx(skb);
  204. } else { /* split packet */
  205. /* NOTE: MSDOS ARP packet correction should only need to
  206. * apply to unsplit packets, since ARP packets are so short.
  207. *
  208. * My interpretation of the RFC1201 document is that if a
  209. * packet is received out of order, the entire assembly
  210. * process should be aborted.
  211. *
  212. * The RFC also mentions "it is possible for successfully
  213. * received packets to be retransmitted." As of 0.40 all
  214. * previously received packets are allowed, not just the
  215. * most recent one.
  216. *
  217. * We allow multiple assembly processes, one for each
  218. * ARCnet card possible on the network.
  219. * Seems rather like a waste of memory, but there's no
  220. * other way to be reliable.
  221. */
  222. arc_printk(D_RX, dev, "packet is split (splitflag=%d, seq=%d)\n",
  223. soft->split_flag, in->sequence);
  224. if (in->skb && in->sequence != soft->sequence) {
  225. arc_printk(D_EXTRA, dev, "wrong seq number (saddr=%d, expected=%d, seq=%d, splitflag=%d)\n",
  226. saddr, in->sequence, soft->sequence,
  227. soft->split_flag);
  228. dev_kfree_skb_irq(in->skb);
  229. in->skb = NULL;
  230. dev->stats.rx_errors++;
  231. dev->stats.rx_missed_errors++;
  232. in->lastpacket = in->numpackets = 0;
  233. }
  234. if (soft->split_flag & 1) { /* first packet in split */
  235. arc_printk(D_RX, dev, "brand new splitpacket (splitflag=%d)\n",
  236. soft->split_flag);
  237. if (in->skb) { /* already assembling one! */
  238. arc_printk(D_EXTRA, dev, "aborting previous (seq=%d) assembly (splitflag=%d, seq=%d)\n",
  239. in->sequence, soft->split_flag,
  240. soft->sequence);
  241. dev->stats.rx_errors++;
  242. dev->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. arc_printk(D_EXTRA, dev, "incoming packet more than 16 segments; dropping. (splitflag=%d)\n",
  250. soft->split_flag);
  251. lp->rfc1201.aborted_seq = soft->sequence;
  252. dev->stats.rx_errors++;
  253. dev->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. arc_printk(D_NORMAL, dev, "(split) memory squeeze, dropping packet.\n");
  260. lp->rfc1201.aborted_seq = soft->sequence;
  261. dev->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. /* 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. arc_printk(D_EXTRA, dev, "can't continue split without starting first! (splitflag=%d, seq=%d, aborted=%d)\n",
  278. soft->split_flag,
  279. 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. arc_printk(D_EXTRA, dev, "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. arc_printk(D_EXTRA, dev, "out-of-order splitpacket, reassembly (seq=%d) aborted (splitflag=%d, seq=%d)\n",
  298. in->sequence, soft->split_flag,
  299. 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. arc_printk(D_SKB_SIZE, dev, "skb: received %d bytes from %02X (unsplit)\n",
  321. skb->len, pkt->hard.source);
  322. arc_printk(D_SKB_SIZE, dev, "skb: received %d bytes from %02X (split)\n",
  323. skb->len, pkt->hard.source);
  324. if (BUGLVL(D_SKB))
  325. arcnet_dump_skb(dev, skb, "rx");
  326. skb->protocol = type_trans(skb, dev);
  327. netif_rx(skb);
  328. }
  329. }
  330. }
  331. /* Create the ARCnet hard/soft headers for RFC1201. */
  332. static int build_header(struct sk_buff *skb, struct net_device *dev,
  333. unsigned short type, uint8_t daddr)
  334. {
  335. struct arcnet_local *lp = netdev_priv(dev);
  336. int hdr_size = ARC_HDR_SIZE + RFC1201_HDR_SIZE;
  337. struct archdr *pkt = (struct archdr *)skb_push(skb, hdr_size);
  338. struct arc_rfc1201 *soft = &pkt->soft.rfc1201;
  339. /* set the protocol ID according to RFC1201 */
  340. switch (type) {
  341. case ETH_P_IP:
  342. soft->proto = ARC_P_IP;
  343. break;
  344. case ETH_P_IPV6:
  345. soft->proto = ARC_P_IPV6;
  346. break;
  347. case ETH_P_ARP:
  348. soft->proto = ARC_P_ARP;
  349. break;
  350. case ETH_P_RARP:
  351. soft->proto = ARC_P_RARP;
  352. break;
  353. case ETH_P_IPX:
  354. case ETH_P_802_3:
  355. case ETH_P_802_2:
  356. soft->proto = ARC_P_IPX;
  357. break;
  358. case ETH_P_ATALK:
  359. soft->proto = ARC_P_ATALK;
  360. break;
  361. default:
  362. arc_printk(D_NORMAL, dev, "RFC1201: I don't understand protocol %d (%Xh)\n",
  363. type, type);
  364. dev->stats.tx_errors++;
  365. dev->stats.tx_aborted_errors++;
  366. return 0;
  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
  372. * in 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. /* FIXME: fill in the last byte of the dest ipaddr here
  380. * to better comply with RFC1051 in "noarp" mode.
  381. * For now, always broadcasting will probably at least get
  382. * 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. }
  413. lp->hw.copy_to_card(dev, bufnum, 0, hard, ARC_HDR_SIZE);
  414. lp->hw.copy_to_card(dev, bufnum, ofs, soft, softlen);
  415. lp->lastload_dest = hard->dest;
  416. }
  417. static int prepare_tx(struct net_device *dev, struct archdr *pkt, int length,
  418. int bufnum)
  419. {
  420. struct arcnet_local *lp = netdev_priv(dev);
  421. const int maxsegsize = XMTU - RFC1201_HDR_SIZE;
  422. struct Outgoing *out;
  423. arc_printk(D_DURING, dev, "prepare_tx: txbufs=%d/%d/%d\n",
  424. lp->next_tx, lp->cur_tx, bufnum);
  425. length -= ARC_HDR_SIZE; /* hard header is not included in packet length */
  426. pkt->soft.rfc1201.split_flag = 0;
  427. /* need to do a split packet? */
  428. if (length > XMTU) {
  429. out = &lp->outgoing;
  430. out->length = length - RFC1201_HDR_SIZE;
  431. out->dataleft = lp->outgoing.length;
  432. out->numsegs = (out->dataleft + maxsegsize - 1) / maxsegsize;
  433. out->segnum = 0;
  434. arc_printk(D_DURING, dev, "rfc1201 prep_tx: ready for %d-segment split (%d bytes, seq=%d)\n",
  435. out->numsegs, out->length,
  436. pkt->soft.rfc1201.sequence);
  437. return 0; /* not done */
  438. }
  439. /* just load the packet into the buffers and send it off */
  440. load_pkt(dev, &pkt->hard, &pkt->soft.rfc1201, length, bufnum);
  441. return 1; /* done */
  442. }
  443. static int continue_tx(struct net_device *dev, int bufnum)
  444. {
  445. struct arcnet_local *lp = netdev_priv(dev);
  446. struct Outgoing *out = &lp->outgoing;
  447. struct arc_hardware *hard = &out->pkt->hard;
  448. struct arc_rfc1201 *soft = &out->pkt->soft.rfc1201, *newsoft;
  449. int maxsegsize = XMTU - RFC1201_HDR_SIZE;
  450. int seglen;
  451. arc_printk(D_DURING, dev,
  452. "rfc1201 continue_tx: loading segment %d(+1) of %d (seq=%d)\n",
  453. out->segnum, out->numsegs, soft->sequence);
  454. /* the "new" soft header comes right before the data chunk */
  455. newsoft = (struct arc_rfc1201 *)
  456. (out->pkt->soft.raw + out->length - out->dataleft);
  457. if (!out->segnum) /* first packet; newsoft == soft */
  458. newsoft->split_flag = ((out->numsegs - 2) << 1) | 1;
  459. else {
  460. newsoft->split_flag = out->segnum << 1;
  461. newsoft->proto = soft->proto;
  462. newsoft->sequence = soft->sequence;
  463. }
  464. seglen = maxsegsize;
  465. if (seglen > out->dataleft)
  466. seglen = out->dataleft;
  467. out->dataleft -= seglen;
  468. load_pkt(dev, hard, newsoft, seglen + RFC1201_HDR_SIZE, bufnum);
  469. out->segnum++;
  470. if (out->segnum >= out->numsegs)
  471. return 1;
  472. else
  473. return 0;
  474. }