rfc1201.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:16k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  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. #define VERSION "arcnet: RFC1201 "standard" (`a') encapsulation support loaded.n"
  33. static unsigned short type_trans(struct sk_buff *skb, struct net_device *dev);
  34. static void rx(struct net_device *dev, int bufnum,
  35.        struct archdr *pkthdr, int length);
  36. static int build_header(struct sk_buff *skb, unsigned short type,
  37. uint8_t daddr);
  38. static int prepare_tx(struct net_device *dev, struct archdr *pkt, int length,
  39.       int bufnum);
  40. static int continue_tx(struct net_device *dev, int bufnum);
  41. struct ArcProto rfc1201_proto =
  42. {
  43. 'a',
  44. 1500, /* could be more, but some receivers can't handle it... */
  45. rx,
  46. build_header,
  47. prepare_tx,
  48. continue_tx
  49. };
  50. void __init arcnet_rfc1201_init(void)
  51. {
  52. arc_proto_map[ARC_P_IP]
  53.     = arc_proto_map[ARC_P_ARP]
  54.     = arc_proto_map[ARC_P_RARP]
  55.     = arc_proto_map[ARC_P_IPX]
  56.     = arc_proto_map[ARC_P_NOVELL_EC]
  57.     = &rfc1201_proto;
  58. /* if someone else already owns the broadcast, we won't take it */
  59. if (arc_bcast_proto == arc_proto_default)
  60. arc_bcast_proto = &rfc1201_proto;
  61. }
  62. #ifdef MODULE
  63. MODULE_LICENSE("GPL");
  64. int __init init_module(void)
  65. {
  66. printk(VERSION);
  67. arcnet_rfc1201_init();
  68. return 0;
  69. }
  70. void cleanup_module(void)
  71. {
  72. arcnet_unregister_proto(&rfc1201_proto);
  73. }
  74. #endif /* MODULE */
  75. /*
  76.  * Determine a packet's protocol ID.
  77.  * 
  78.  * With ARCnet we have to convert everything to Ethernet-style stuff.
  79.  */
  80. static unsigned short 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. struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
  85. int hdr_size = ARC_HDR_SIZE + RFC1201_HDR_SIZE;
  86. /* Pull off the arcnet header. */
  87. skb->mac.raw = skb->data;
  88. skb_pull(skb, hdr_size);
  89. if (pkt->hard.dest == 0)
  90. skb->pkt_type = PACKET_BROADCAST;
  91. else if (dev->flags & IFF_PROMISC) {
  92. /* if we're not sending to ourselves :) */
  93. if (pkt->hard.dest != dev->dev_addr[0])
  94. skb->pkt_type = PACKET_OTHERHOST;
  95. }
  96. /* now return the protocol number */
  97. switch (soft->proto) {
  98. case ARC_P_IP:
  99. return htons(ETH_P_IP);
  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. lp->stats.rx_errors++;
  109. lp->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 = (struct arcnet_local *) dev->priv;
  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 packetn");
  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. lp->stats.rx_errors++;
  152. lp->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. lp->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. lp->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. lp->stats.rx_errors++;
  196. lp->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. dev->last_rx = jiffies;
  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. lp->stats.rx_errors++;
  229. lp->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. lp->stats.rx_errors++;
  241. lp->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. lp->stats.rx_errors++;
  252. lp->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. lp->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. lp->stats.rx_errors++;
  282. lp->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. lp->stats.rx_errors++;
  293. lp->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. lp->stats.rx_errors++;
  304. lp->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. dev->last_rx = jiffies;
  328. }
  329. }
  330. }
  331. /* Create the ARCnet hard/soft headers for RFC1201. */
  332. static int build_header(struct sk_buff *skb, unsigned short type,
  333. uint8_t daddr)
  334. {
  335. struct net_device *dev = skb->dev;
  336. struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
  337. int hdr_size = ARC_HDR_SIZE + RFC1201_HDR_SIZE;
  338. struct archdr *pkt = (struct archdr *) skb_push(skb, hdr_size);
  339. struct arc_rfc1201 *soft = &pkt->soft.rfc1201;
  340. /* set the protocol ID according to RFC1201 */
  341. switch (type) {
  342. case ETH_P_IP:
  343. soft->proto = ARC_P_IP;
  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. lp->stats.tx_errors++;
  363. lp->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 = (struct arcnet_local *) dev->priv;
  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 = 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 = (struct arcnet_local *) dev->priv;
  419. const int maxsegsize = XMTU - RFC1201_HDR_SIZE;
  420. struct Outgoing *out;
  421. BUGMSG(D_DURING, "prepare_tx: txbufs=%d/%d/%dn",
  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 = (struct arcnet_local *) dev->priv;
  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. }