rfc1201.c 16 KB

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