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

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * ROSE release 003
  3.  *
  4.  * This code REQUIRES 2.1.15 or higher/ NET3.038
  5.  *
  6.  * This module:
  7.  * This module is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU General Public License
  9.  * as published by the Free Software Foundation; either version
  10.  * 2 of the License, or (at your option) any later version.
  11.  *
  12.  * History
  13.  * ROSE 001 Jonathan(G4KLX) Cloned from nr_dev.c.
  14.  * Hans(PE1AYX) Fixed interface to IP layer.
  15.  */
  16. #include <linux/config.h>
  17. #define __NO_VERSION__
  18. #include <linux/module.h>
  19. #include <linux/proc_fs.h>
  20. #include <linux/kernel.h>
  21. #include <linux/sched.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/fs.h>
  24. #include <linux/types.h>
  25. #include <linux/sysctl.h>
  26. #include <linux/string.h>
  27. #include <linux/socket.h>
  28. #include <linux/errno.h>
  29. #include <linux/fcntl.h>
  30. #include <linux/in.h>
  31. #include <linux/if_ether.h> /* For the statistics structure. */
  32. #include <asm/system.h>
  33. #include <asm/segment.h>
  34. #include <asm/io.h>
  35. #include <linux/inet.h>
  36. #include <linux/netdevice.h>
  37. #include <linux/etherdevice.h>
  38. #include <linux/if_arp.h>
  39. #include <linux/skbuff.h>
  40. #include <net/ip.h>
  41. #include <net/arp.h>
  42. #include <net/ax25.h>
  43. #include <net/rose.h>
  44. /*
  45.  * Only allow IP over ROSE frames through if the netrom device is up.
  46.  */
  47. int rose_rx_ip(struct sk_buff *skb, struct net_device *dev)
  48. {
  49. struct net_device_stats *stats = (struct net_device_stats *)dev->priv;
  50. #ifdef CONFIG_INET
  51. if (!netif_running(dev)) {
  52. stats->rx_errors++;
  53. return 0;
  54. }
  55. stats->rx_packets++;
  56. stats->rx_bytes += skb->len;
  57. skb->protocol = htons(ETH_P_IP);
  58. /* Spoof incoming device */
  59. skb->dev      = dev;
  60. skb->h.raw    = skb->data;
  61. skb->nh.raw   = skb->data;
  62. skb->pkt_type = PACKET_HOST;
  63. ip_rcv(skb, skb->dev, NULL);
  64. #else
  65. kfree_skb(skb);
  66. #endif
  67. return 1;
  68. }
  69. static int rose_header(struct sk_buff *skb, struct net_device *dev, unsigned short type,
  70. void *daddr, void *saddr, unsigned len)
  71. {
  72. unsigned char *buff = skb_push(skb, ROSE_MIN_LEN + 2);
  73. *buff++ = ROSE_GFI | ROSE_Q_BIT;
  74. *buff++ = 0x00;
  75. *buff++ = ROSE_DATA;
  76. *buff++ = 0x7F;
  77. *buff++ = AX25_P_IP;
  78. if (daddr != NULL)
  79. return 37;
  80. return -37;
  81. }
  82. static int rose_rebuild_header(struct sk_buff *skb)
  83. {
  84. struct net_device *dev = skb->dev;
  85. struct net_device_stats *stats = (struct net_device_stats *)dev->priv;
  86. unsigned char *bp = (unsigned char *)skb->data;
  87. struct sk_buff *skbn;
  88. #ifdef CONFIG_INET
  89. if (arp_find(bp + 7, skb)) {
  90. return 1;
  91. }
  92. if ((skbn = skb_clone(skb, GFP_ATOMIC)) == NULL) {
  93. kfree_skb(skb);
  94. return 1;
  95. }
  96. if (skb->sk != NULL)
  97. skb_set_owner_w(skbn, skb->sk);
  98. kfree_skb(skb);
  99. if (!rose_route_frame(skbn, NULL)) {
  100. kfree_skb(skbn);
  101. stats->tx_errors++;
  102. return 1;
  103. }
  104. stats->tx_packets++;
  105. stats->tx_bytes += skbn->len;
  106. #endif
  107. return 1;
  108. }
  109. static int rose_set_mac_address(struct net_device *dev, void *addr)
  110. {
  111. struct sockaddr *sa = addr;
  112. rose_del_loopback_node((rose_address *)dev->dev_addr);
  113. memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
  114. rose_add_loopback_node((rose_address *)dev->dev_addr);
  115. return 0;
  116. }
  117. static int rose_open(struct net_device *dev)
  118. {
  119. MOD_INC_USE_COUNT;
  120. netif_start_queue(dev);
  121. rose_add_loopback_node((rose_address *)dev->dev_addr);
  122. return 0;
  123. }
  124. static int rose_close(struct net_device *dev)
  125. {
  126. netif_stop_queue(dev);
  127. rose_del_loopback_node((rose_address *)dev->dev_addr);
  128. MOD_DEC_USE_COUNT;
  129. return 0;
  130. }
  131. static int rose_xmit(struct sk_buff *skb, struct net_device *dev)
  132. {
  133. struct net_device_stats *stats = (struct net_device_stats *)dev->priv;
  134. if (!netif_running(dev)) {
  135. printk(KERN_ERR "ROSE: rose_xmit - called when iface is downn");
  136. return 1;
  137. }
  138. dev_kfree_skb(skb);
  139. stats->tx_errors++;
  140. return 0;
  141. }
  142. static struct net_device_stats *rose_get_stats(struct net_device *dev)
  143. {
  144. return (struct net_device_stats *)dev->priv;
  145. }
  146. int rose_init(struct net_device *dev)
  147. {
  148. dev->mtu = ROSE_MAX_PACKET_SIZE - 2;
  149. dev->hard_start_xmit = rose_xmit;
  150. dev->open = rose_open;
  151. dev->stop = rose_close;
  152. dev->hard_header = rose_header;
  153. dev->hard_header_len = AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN;
  154. dev->addr_len = ROSE_ADDR_LEN;
  155. dev->type = ARPHRD_ROSE;
  156. dev->rebuild_header = rose_rebuild_header;
  157. dev->set_mac_address    = rose_set_mac_address;
  158. /* New-style flags. */
  159. dev->flags = 0;
  160. if ((dev->priv = kmalloc(sizeof(struct net_device_stats), GFP_KERNEL)) == NULL)
  161. return -ENOMEM;
  162. memset(dev->priv, 0, sizeof(struct net_device_stats));
  163. dev->get_stats = rose_get_stats;
  164. return 0;
  165. };