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

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * Ethertap: A network device for bouncing packets via user space
  3.  *
  4.  * This is a very simple ethernet driver. It bounces ethernet frames
  5.  * to user space on /dev/tap0->/dev/tap15 and expects ethernet frames
  6.  * to be written back to it. By default it does not ARP. If you turn ARP
  7.  * on it will attempt to ARP the user space and reply to ARPS from the
  8.  * user space.
  9.  *
  10.  * As this is an ethernet device you can use it for appletalk, IPX etc
  11.  * even for building bridging tunnels.
  12.  */
  13.  
  14. #include <linux/config.h>
  15. #include <linux/module.h>
  16. #include <linux/kernel.h>
  17. #include <linux/sched.h>
  18. #include <linux/slab.h>
  19. #include <linux/string.h>
  20. #include <linux/errno.h>
  21. #include <linux/netdevice.h>
  22. #include <linux/inetdevice.h>
  23. #include <linux/etherdevice.h>
  24. #include <linux/skbuff.h>
  25. #include <linux/init.h>
  26. #include <net/sock.h>
  27. #include <linux/netlink.h>
  28. /*
  29.  * Index to functions.
  30.  */
  31. int     ethertap_probe(struct net_device *dev);
  32. static int  ethertap_open(struct net_device *dev);
  33. static int  ethertap_start_xmit(struct sk_buff *skb, struct net_device *dev);
  34. static int  ethertap_close(struct net_device *dev);
  35. static struct net_device_stats *ethertap_get_stats(struct net_device *dev);
  36. static void ethertap_rx(struct sock *sk, int len);
  37. #ifdef CONFIG_ETHERTAP_MC
  38. static void set_multicast_list(struct net_device *dev);
  39. #endif
  40. static int ethertap_debug;
  41. static struct net_device *tap_map[32]; /* Returns the tap device for a given netlink */
  42. /*
  43.  * Board-specific info in dev->priv.
  44.  */
  45. struct net_local
  46. {
  47. struct sock *nl;
  48. #ifdef CONFIG_ETHERTAP_MC
  49. __u32 groups;
  50. #endif
  51. struct net_device_stats stats;
  52. };
  53. /*
  54.  * To call this a probe is a bit misleading, however for real
  55.  * hardware it would have to check what was present.
  56.  */
  57.  
  58. int __init ethertap_probe(struct net_device *dev)
  59. {
  60. SET_MODULE_OWNER(dev);
  61. memcpy(dev->dev_addr, "xFExFDx00x00x00x00", 6);
  62. if (dev->mem_start & 0xf)
  63. ethertap_debug = dev->mem_start & 0x7;
  64. /*
  65.  * Initialize the device structure.
  66.  */
  67. dev->priv = kmalloc(sizeof(struct net_local), GFP_KERNEL);
  68. if (dev->priv == NULL)
  69. return -ENOMEM;
  70. memset(dev->priv, 0, sizeof(struct net_local));
  71. /*
  72.  * The tap specific entries in the device structure.
  73.  */
  74. dev->open = ethertap_open;
  75. dev->hard_start_xmit = ethertap_start_xmit;
  76. dev->stop = ethertap_close;
  77. dev->get_stats = ethertap_get_stats;
  78. #ifdef CONFIG_ETHERTAP_MC
  79. dev->set_multicast_list = set_multicast_list;
  80. #endif
  81. /*
  82.  * Setup the generic properties
  83.  */
  84. ether_setup(dev);
  85. dev->tx_queue_len = 0;
  86. dev->flags|=IFF_NOARP;
  87. tap_map[dev->base_addr]=dev;
  88. return 0;
  89. }
  90. /*
  91.  * Open/initialize the board.
  92.  */
  93. static int ethertap_open(struct net_device *dev)
  94. {
  95. struct net_local *lp = (struct net_local*)dev->priv;
  96. if (ethertap_debug > 2)
  97. printk("%s: Doing ethertap_open()...", dev->name);
  98. lp->nl = netlink_kernel_create(dev->base_addr, ethertap_rx);
  99. if (lp->nl == NULL)
  100. return -ENOBUFS;
  101. netif_start_queue(dev);
  102. return 0;
  103. }
  104. #ifdef CONFIG_ETHERTAP_MC
  105. static unsigned ethertap_mc_hash(__u8 *dest)
  106. {
  107. unsigned idx = 0;
  108. idx ^= dest[0];
  109. idx ^= dest[1];
  110. idx ^= dest[2];
  111. idx ^= dest[3];
  112. idx ^= dest[4];
  113. idx ^= dest[5];
  114. return 1U << (idx&0x1F);
  115. }
  116. static void set_multicast_list(struct net_device *dev)
  117. {
  118. unsigned groups = ~0;
  119. struct net_local *lp = (struct net_local *)dev->priv;
  120. if (!(dev->flags&(IFF_NOARP|IFF_PROMISC|IFF_ALLMULTI))) {
  121. struct dev_mc_list *dmi;
  122. groups = ethertap_mc_hash(dev->broadcast);
  123. for (dmi=dev->mc_list; dmi; dmi=dmi->next) {
  124. if (dmi->dmi_addrlen != 6)
  125. continue;
  126. groups |= ethertap_mc_hash(dmi->dmi_addr);
  127. }
  128. }
  129. lp->groups = groups;
  130. if (lp->nl)
  131. lp->nl->protinfo.af_netlink.groups = groups;
  132. }
  133. #endif
  134. /*
  135.  * We transmit by throwing the packet at netlink. We have to clone
  136.  * it for 2.0 so that we dev_kfree_skb() the locked original.
  137.  */
  138.  
  139. static int ethertap_start_xmit(struct sk_buff *skb, struct net_device *dev)
  140. {
  141. struct net_local *lp = (struct net_local *)dev->priv;
  142. #ifdef CONFIG_ETHERTAP_MC
  143. struct ethhdr *eth = (struct ethhdr*)skb->data;
  144. #endif
  145. if (skb_headroom(skb) < 2) {
  146. static int once;
  147.    struct sk_buff *skb2;
  148. if (!once) {
  149. once = 1;
  150. printk(KERN_DEBUG "%s: not aligned xmit by protocol %04xn", dev->name, skb->protocol);
  151. }
  152. skb2 = skb_realloc_headroom(skb, 2);
  153. dev_kfree_skb(skb);
  154. if (skb2 == NULL)
  155. return 0;
  156. skb = skb2;
  157. }
  158. __skb_push(skb, 2);
  159. /* Make the same thing, which loopback does. */
  160. if (skb_shared(skb)) {
  161.    struct sk_buff *skb2 = skb;
  162.    skb = skb_clone(skb, GFP_ATOMIC); /* Clone the buffer */
  163.    if (skb==NULL) {
  164. dev_kfree_skb(skb2);
  165. return 0;
  166. }
  167.    dev_kfree_skb(skb2);
  168. }
  169. /* ... but do not orphan it here, netlink does it in any case. */
  170. lp->stats.tx_bytes+=skb->len;
  171. lp->stats.tx_packets++;
  172. #ifndef CONFIG_ETHERTAP_MC
  173. netlink_broadcast(lp->nl, skb, 0, ~0, GFP_ATOMIC);
  174. #else
  175. if (dev->flags&IFF_NOARP) {
  176. netlink_broadcast(lp->nl, skb, 0, ~0, GFP_ATOMIC);
  177. return 0;
  178. }
  179. if (!(eth->h_dest[0]&1)) {
  180. /* Unicast packet */
  181. __u32 pid;
  182. memcpy(&pid, eth->h_dest+2, 4);
  183. netlink_unicast(lp->nl, skb, ntohl(pid), MSG_DONTWAIT);
  184. } else
  185. netlink_broadcast(lp->nl, skb, 0, ethertap_mc_hash(eth->h_dest), GFP_ATOMIC);
  186. #endif
  187. return 0;
  188. }
  189. static __inline__ int ethertap_rx_skb(struct sk_buff *skb, struct net_device *dev)
  190. {
  191. struct net_local *lp = (struct net_local *)dev->priv;
  192. #ifdef CONFIG_ETHERTAP_MC
  193. struct ethhdr *eth = (struct ethhdr*)(skb->data + 2);
  194. #endif
  195. int len = skb->len;
  196. if (len < 16) {
  197. printk(KERN_DEBUG "%s : rx len = %dn", dev->name, len);
  198. kfree_skb(skb);
  199. return -EINVAL;
  200. }
  201. if (NETLINK_CREDS(skb)->uid) {
  202. printk(KERN_INFO "%s : user %dn", dev->name, NETLINK_CREDS(skb)->uid);
  203. kfree_skb(skb);
  204. return -EPERM;
  205. }
  206. #ifdef CONFIG_ETHERTAP_MC
  207. if (!(dev->flags&(IFF_NOARP|IFF_PROMISC))) {
  208. int drop = 0;
  209. if (eth->h_dest[0]&1) {
  210. if (!(ethertap_mc_hash(eth->h_dest)&lp->groups))
  211. drop = 1;
  212. } else if (memcmp(eth->h_dest, dev->dev_addr, 6) != 0)
  213. drop = 1;
  214. if (drop) {
  215. if (ethertap_debug > 3)
  216. printk(KERN_DEBUG "%s : not for usn", dev->name);
  217. kfree_skb(skb);
  218. return -EINVAL;
  219. }
  220. }
  221. #endif
  222. if (skb_shared(skb)) {
  223.    struct sk_buff *skb2 = skb;
  224.    skb = skb_clone(skb, GFP_KERNEL); /* Clone the buffer */
  225.    if (skb==NULL) {
  226. kfree_skb(skb2);
  227. return -ENOBUFS;
  228. }
  229.    kfree_skb(skb2);
  230. } else
  231. skb_orphan(skb);
  232. skb_pull(skb, 2);
  233. skb->dev = dev;
  234. skb->protocol=eth_type_trans(skb,dev);
  235. memset(skb->cb, 0, sizeof(skb->cb));
  236. lp->stats.rx_packets++;
  237. lp->stats.rx_bytes+=len;
  238. netif_rx(skb);
  239. dev->last_rx = jiffies;
  240. return len;
  241. }
  242. /*
  243.  * The typical workload of the driver:
  244.  * Handle the ether interface interrupts.
  245.  *
  246.  * (In this case handle the packets posted from user space..)
  247.  */
  248. static void ethertap_rx(struct sock *sk, int len)
  249. {
  250. struct net_device *dev = tap_map[sk->protocol];
  251. struct sk_buff *skb;
  252. if (dev==NULL) {
  253. printk(KERN_CRIT "ethertap: bad unit!n");
  254. skb_queue_purge(&sk->receive_queue);
  255. return;
  256. }
  257. if (ethertap_debug > 3)
  258. printk("%s: ethertap_rx()n", dev->name);
  259. while ((skb = skb_dequeue(&sk->receive_queue)) != NULL)
  260. ethertap_rx_skb(skb, dev);
  261. }
  262. static int ethertap_close(struct net_device *dev)
  263. {
  264. struct net_local *lp = (struct net_local *)dev->priv;
  265. struct sock *sk = lp->nl;
  266. if (ethertap_debug > 2)
  267. printk("%s: Shutting down.n", dev->name);
  268. netif_stop_queue(dev);
  269. if (sk) {
  270. lp->nl = NULL;
  271. sock_release(sk->socket);
  272. }
  273. return 0;
  274. }
  275. static struct net_device_stats *ethertap_get_stats(struct net_device *dev)
  276. {
  277. struct net_local *lp = (struct net_local *)dev->priv;
  278. return &lp->stats;
  279. }
  280. #ifdef MODULE
  281. static int unit;
  282. MODULE_PARM(unit,"i");
  283. MODULE_PARM_DESC(unit,"Ethertap device number");
  284. static struct net_device dev_ethertap =
  285. {
  286. " ",
  287. 0, 0, 0, 0,
  288. 1, 5,
  289. 0, 0, 0, NULL, ethertap_probe
  290. };
  291. int init_module(void)
  292. {
  293. dev_ethertap.base_addr=unit+NETLINK_TAPBASE;
  294. sprintf(dev_ethertap.name,"tap%d",unit);
  295. if (dev_get(dev_ethertap.name))
  296. {
  297. printk(KERN_INFO "%s already loaded.n", dev_ethertap.name);
  298. return -EBUSY;
  299. }
  300. if (register_netdev(&dev_ethertap) != 0)
  301. return -EIO;
  302. return 0;
  303. }
  304. void cleanup_module(void)
  305. {
  306. tap_map[dev_ethertap.base_addr]=NULL;
  307. unregister_netdev(&dev_ethertap);
  308. /*
  309.  * Free up the private structure.
  310.  */
  311. kfree(dev_ethertap.priv);
  312. dev_ethertap.priv = NULL; /* gets re-allocated by ethertap_probe */
  313. }
  314. #endif /* MODULE */
  315. MODULE_LICENSE("GPL");