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

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * Linux ARCnet driver - RFC1051 ("simple" 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 <net/arp.h>
  30. #include <linux/netdevice.h>
  31. #include <linux/skbuff.h>
  32. #include <linux/arcdevice.h>
  33. #define VERSION "arcnet: RFC1051 "simple standard" (`s') encapsulation support loaded.n"
  34. static unsigned short type_trans(struct sk_buff *skb, struct net_device *dev);
  35. static void rx(struct net_device *dev, int bufnum,
  36.        struct archdr *pkthdr, int length);
  37. static int build_header(struct sk_buff *skb, unsigned short type,
  38. uint8_t daddr);
  39. static int prepare_tx(struct net_device *dev, struct archdr *pkt, int length,
  40.       int bufnum);
  41. struct ArcProto rfc1051_proto =
  42. {
  43. 's',
  44. XMTU - RFC1051_HDR_SIZE,
  45. rx,
  46. build_header,
  47. prepare_tx
  48. };
  49. void __init arcnet_rfc1051_init(void)
  50. {
  51. arc_proto_map[ARC_P_IP_RFC1051]
  52.     = arc_proto_map[ARC_P_ARP_RFC1051]
  53.     = &rfc1051_proto;
  54. /* if someone else already owns the broadcast, we won't take it */
  55. if (arc_bcast_proto == arc_proto_default)
  56. arc_bcast_proto = &rfc1051_proto;
  57. }
  58. #ifdef MODULE
  59. MODULE_LICENSE("GPL");
  60. int __init init_module(void)
  61. {
  62. printk(VERSION);
  63. arcnet_rfc1051_init();
  64. return 0;
  65. }
  66. void cleanup_module(void)
  67. {
  68. arcnet_unregister_proto(&rfc1051_proto);
  69. }
  70. #endif /* MODULE */
  71. /*
  72.  * Determine a packet's protocol ID.
  73.  * 
  74.  * With ARCnet we have to convert everything to Ethernet-style stuff.
  75.  */
  76. static unsigned short type_trans(struct sk_buff *skb, struct net_device *dev)
  77. {
  78. struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
  79. struct archdr *pkt = (struct archdr *) skb->data;
  80. struct arc_rfc1051 *soft = &pkt->soft.rfc1051;
  81. int hdr_size = ARC_HDR_SIZE + RFC1051_HDR_SIZE;
  82. /* Pull off the arcnet header. */
  83. skb->mac.raw = skb->data;
  84. skb_pull(skb, hdr_size);
  85. if (pkt->hard.dest == 0)
  86. skb->pkt_type = PACKET_BROADCAST;
  87. else if (dev->flags & IFF_PROMISC) {
  88. /* if we're not sending to ourselves :) */
  89. if (pkt->hard.dest != dev->dev_addr[0])
  90. skb->pkt_type = PACKET_OTHERHOST;
  91. }
  92. /* now return the protocol number */
  93. switch (soft->proto) {
  94. case ARC_P_IP_RFC1051:
  95. return htons(ETH_P_IP);
  96. case ARC_P_ARP_RFC1051:
  97. return htons(ETH_P_ARP);
  98. default:
  99. lp->stats.rx_errors++;
  100. lp->stats.rx_crc_errors++;
  101. return 0;
  102. }
  103. return htons(ETH_P_IP);
  104. }
  105. /* packet receiver */
  106. static void rx(struct net_device *dev, int bufnum,
  107.        struct archdr *pkthdr, int length)
  108. {
  109. struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
  110. struct sk_buff *skb;
  111. struct archdr *pkt = pkthdr;
  112. int ofs;
  113. BUGMSG(D_DURING, "it's a raw packet (length=%d)n", length);
  114. if (length >= MinTU)
  115. ofs = 512 - length;
  116. else
  117. ofs = 256 - length;
  118. skb = alloc_skb(length + ARC_HDR_SIZE, GFP_ATOMIC);
  119. if (skb == NULL) {
  120. BUGMSG(D_NORMAL, "Memory squeeze, dropping packet.n");
  121. lp->stats.rx_dropped++;
  122. return;
  123. }
  124. skb_put(skb, length + ARC_HDR_SIZE);
  125. skb->dev = dev;
  126. pkt = (struct archdr *) skb->data;
  127. /* up to sizeof(pkt->soft) has already been copied from the card */
  128. memcpy(pkt, pkthdr, sizeof(struct archdr));
  129. if (length > sizeof(pkt->soft))
  130. lp->hw.copy_from_card(dev, bufnum, ofs + sizeof(pkt->soft),
  131.       pkt->soft.raw + sizeof(pkt->soft),
  132.       length - sizeof(pkt->soft));
  133. BUGLVL(D_SKB) arcnet_dump_skb(dev, skb, "rx");
  134. skb->protocol = type_trans(skb, dev);
  135. netif_rx(skb);
  136. dev->last_rx = jiffies;
  137. }
  138. /*
  139.  * Create the ARCnet hard/soft headers for RFC1051.
  140.  */
  141. static int build_header(struct sk_buff *skb, unsigned short type,
  142. uint8_t daddr)
  143. {
  144. struct net_device *dev = skb->dev;
  145. struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
  146. int hdr_size = ARC_HDR_SIZE + RFC1051_HDR_SIZE;
  147. struct archdr *pkt = (struct archdr *) skb_push(skb, hdr_size);
  148. struct arc_rfc1051 *soft = &pkt->soft.rfc1051;
  149. /* set the protocol ID according to RFC1051 */
  150. switch (type) {
  151. case ETH_P_IP:
  152. soft->proto = ARC_P_IP_RFC1051;
  153. break;
  154. case ETH_P_ARP:
  155. soft->proto = ARC_P_ARP_RFC1051;
  156. break;
  157. default:
  158. BUGMSG(D_NORMAL, "RFC1051: I don't understand protocol %d (%Xh)n",
  159.        type, type);
  160. lp->stats.tx_errors++;
  161. lp->stats.tx_aborted_errors++;
  162. return 0;
  163. }
  164. /*
  165.  * Set the source hardware address.
  166.  *
  167.  * This is pretty pointless for most purposes, but it can help in
  168.  * debugging.  ARCnet does not allow us to change the source address in
  169.  * the actual packet sent)
  170.  */
  171. pkt->hard.source = *dev->dev_addr;
  172. /* see linux/net/ethernet/eth.c to see where I got the following */
  173. if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) {
  174. /* 
  175.  * FIXME: fill in the last byte of the dest ipaddr here to better
  176.  * comply with RFC1051 in "noarp" mode.
  177.  */
  178. pkt->hard.dest = 0;
  179. return hdr_size;
  180. }
  181. /* otherwise, just fill it in and go! */
  182. pkt->hard.dest = daddr;
  183. return hdr_size; /* success */
  184. }
  185. static int prepare_tx(struct net_device *dev, struct archdr *pkt, int length,
  186.       int bufnum)
  187. {
  188. struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
  189. struct arc_hardware *hard = &pkt->hard;
  190. int ofs;
  191. BUGMSG(D_DURING, "prepare_tx: txbufs=%d/%d/%dn",
  192.        lp->next_tx, lp->cur_tx, bufnum);
  193. length -= ARC_HDR_SIZE; /* hard header is not included in packet length */
  194. if (length > XMTU) {
  195. /* should never happen! other people already check for this. */
  196. BUGMSG(D_NORMAL, "Bug!  prepare_tx with size %d (> %d)n",
  197.        length, XMTU);
  198. length = XMTU;
  199. }
  200. if (length > MinTU) {
  201. hard->offset[0] = 0;
  202. hard->offset[1] = ofs = 512 - length;
  203. } else if (length > MTU) {
  204. hard->offset[0] = 0;
  205. hard->offset[1] = ofs = 512 - length - 3;
  206. } else
  207. hard->offset[0] = ofs = 256 - length;
  208. lp->hw.copy_to_card(dev, bufnum, 0, hard, ARC_HDR_SIZE);
  209. lp->hw.copy_to_card(dev, bufnum, ofs, &pkt->soft, length);
  210. lp->lastload_dest = hard->dest;
  211. return 1; /* done */
  212. }