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