Linux之虚拟网卡驱动

目录

 

一、网卡驱动程序框架

二、编写虚拟网卡驱动

2.1 测试直接注册net_device 

2.2 提供发包函数 

2.3 添加统计信息

2.4 构造假包ping通


一、网卡驱动程序框架

网卡驱动程序时网络驱动程序的一部分,并不是网络驱动程序,网络驱动程序分为很多层,在这里只是最底层,最终会操作到硬件,在硬件上,有硬件相关层的驱动程序,即我们要写的网卡驱动

网卡驱动需要具备收发能力,使数据底层到上层,也能从上层到底层,大多驱动程序都是以面向对象的思想实现,参考真正的网卡驱动程序cs89x0.c,在入口函数中分配了一个net_dev结构体,设置提供发包函数hard_start_xmit函数,在中断中提供收包的功能netif_rx函数,并且注册register_netdev

int __init init_module(void)
{
	struct net_device *dev = alloc_etherdev(sizeof(struct net_local));
...
	ret = cs89x0_probe1(dev, io, 1);
	if (ret)
		goto out;

	dev_cs89x0 = dev;
	return 0;
out:
	free_netdev(dev);
	return ret;
}

static int __init
cs89x0_probe1(struct net_device *dev, int ioaddr, int modular)
{
	struct net_local *lp = netdev_priv(dev);
	static unsigned version_printed;
	int i;
	int tmp;
	unsigned rev_type = 0;
	int eeprom_buff[CHKSUM_LEN];
	int retval;

...

	dev->open		= net_open;
	dev->stop		= net_close;
	dev->tx_timeout		= net_timeout;
	dev->watchdog_timeo	= HZ;
	dev->hard_start_xmit 	= net_send_packet;
	dev->get_stats		= net_get_stats;
	dev->set_multicast_list = set_multicast_list;
	dev->set_mac_address 	= set_mac_address;
#ifdef CONFIG_NET_POLL_CONTROLLER
	dev->poll_controller	= net_poll_controller;
#endif

	printk("\n");
	if (net_debug)
		printk("cs89x0_probe1() successful\n");

	retval = register_netdev(dev);

...
}

static irqreturn_t net_interrupt(int irq, void *dev_id)
{
	struct net_device *dev = dev_id;
	struct net_local *lp;
	int ioaddr, status;

...
		case ISQ_RECEIVER_EVENT:
			/* Got a packet(s). */
			net_rx(dev);
			break;
		case ISQ_TRANSMITTER_EVENT:
			lp->stats.tx_packets++;
			netif_wake_queue(dev);
			if ((status & TX_OK) == 0) lp->stats.tx_errors++;
			if (status & TX_LOST_CRS) lp->stats.tx_carrier_errors++;
			if (status & TX_SQE_ERROR) lp->stats.tx_heartbeat_errors++;
			if (status & TX_LATE_COL) lp->stats.tx_window_errors++;
			if (status & TX_16_COL) lp->stats.tx_aborted_errors++;
			break;
...
}

static void
net_rx(struct net_device *dev)
{
	struct net_local *lp = netdev_priv(dev);
	struct sk_buff *skb;
	int status, length;

...
	netif_rx(skb);
	dev->last_rx = jiffies;
	lp->stats.rx_packets++;
	lp->stats.rx_bytes += length;
}

hard_start_xmit和netif_rx之间传输的sk_buff,总体框架如下

APP:socket
 

-----------------------

-----------------------

-----------------------

-----------------------

若干层网络协议(纯软件)
发包函数hard_start_xmit

   ||  sk_buff     /\

   \/                  ||

收包函数netif_rx
硬件相关的驱动程序(要提供hard_start_xmit,有数据时,用netif_rx上报)
硬件

写网卡驱动程序:

    1. 分配一个net_device结构体
    2. 设置:
    2.1 发包函数: hard_start_xmit
    2.2 收到数据时(在中断处理函数里)用netif_rx上报数据
    2.3 其他设置
    3. 注册: register_netdev
在cs89x0.c中分配net_dev结构体使用如下,若想自己修改名称,直接使用alloc_netdev函数来分配,ether_setup为默认的设置函数,sizeof_priv为私有数据,alloc_netdev可以多分配出一块内存,其中有指针可以指向这块内存

struct net_device *alloc_etherdev(int sizeof_priv)
{
    return alloc_netdev(sizeof_priv, "eth%d", ether_setup);
}

二、编写虚拟网卡驱动

2.1 测试直接注册net_device 

#include <linux/module.h>
#include <linux/errno.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/fcntl.h>
#include <linux/interrupt.h>
#include <linux/ioport.h>
#include <linux/in.h>
#include <linux/skbuff.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/string.h>
#include <linux/init.h>
#include <linux/bitops.h>
#include <linux/delay.h>

#include <asm/system.h>
#include <asm/io.h>
#include <asm/irq.h>

static struct net_device *vnet_dev;

static int virt_net_init(void)
{
	/* 1. 分配一个net_device结构体 */
	vnet_dev = alloc_netdev(0, "vnet%d", ether_setup);;  /* alloc_etherdev */

	/* 2. 设置 */

	/* 3. 注册 */
	register_netdev(vnet_dev);
	
	return 0;
}

static void virt_net_exit(void)
{
	unregister_netdev(vnet_dev);
	free_netdev(vnet_dev);
}

module_init(virt_net_init);
module_exit(virt_net_exit);

MODULE_AUTHOR("xiaoma"); //可以写入作者信息
MODULE_LICENSE("GPL");

编译驱动程序,加载到单板上,其中eth0为网卡DM9000,vnet0则是我们注册的网卡驱动,在没有提供发包函数,在这里能够ping得通自己,说明在网络层纯软件中来回,IP是纯软件的概念,跟硬件无关,当我们ping 3.3.3.4后发出去会调用到hard_start_xmit发包函数,这里没有编写,因此会死机,为什么ping 3.3.3.4的时候从网卡vnet0出去,是因为两者之间处于同一网段,所以当PC机有两个网卡时,最好不要处于同一网段,否则ping一个IP或者访问一个主机的时候,操作系统不知道把数据发给哪个网卡

# insmod virt_net.ko
# ifconfig vnet0 3.3.3.3
# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:60:6E:33:44:55
          inet addr:192.168.0.19  Bcast:192.168.0.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1770 errors:0 dropped:0 overruns:0 frame:0
          TX packets:720 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:2170957 (2.0 MiB)  TX bytes:111980 (109.3 KiB)
          Interrupt:51 Base address:0xa000

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

vnet0     Link encap:Ethernet  HWaddr 00:00:00:00:00:00
          inet addr:3.3.3.3  Bcast:3.255.255.255  Mask:255.0.0.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

# ping 3.3.3.3
PING 3.3.3.3 (3.3.3.3): 56 data bytes
64 bytes from 3.3.3.3: seq=0 ttl=64 time=1.064 ms
64 bytes from 3.3.3.3: seq=1 ttl=64 time=0.463 ms
64 bytes from 3.3.3.3: seq=2 ttl=64 time=0.466 ms
64 bytes from 3.3.3.3: seq=3 ttl=64 time=0.463 ms
64 bytes from 3.3.3.3: seq=4 ttl=64 time=0.461 ms
64 bytes from 3.3.3.3: seq=5 ttl=64 time=0.458 ms
64 bytes from 3.3.3.3: seq=6 ttl=64 time=0.464 ms

--- 3.3.3.3 ping statistics ---
7 packets transmitted, 7 packets received, 0% packet loss
round-trip min/avg/max = 0.458/0.548/1.064 ms

# ping 3.3.3.4
PING 3.3.3.4 (3.3.3.4): 56 data bytes
Unable to handle kernel NULL pointer dereference at virtual address 00000000
pgd = c0004000
[00000000] *pgd=00000000
Internal error: Oops: 0 [#1]
Modules linked in: virt_net
CPU: 0    Not tainted  (2.6.22.6 #1)
PC is at __init_begin+0x3fff8000/0x30
LR is at dev_hard_start_xmit+0x1a8/0x240                                   //没有发包函数
pc : [<00000000>]    lr : [<c0245798>]    psr: 60000013
sp : c035ddd8  ip : c035ddfc  fp : c035ddf8
r10: c06cec00  r9 : 04030303  r8 : c03c79b8
r7 : c06cec00  r6 : c07d6780  r5 : c07d6780  r4 : c06cec00
r3 : 00000000  r2 : c06e50a0  r1 : c06cec00  r0 : c07d6780
Flags: nZCv  IRQs on  FIQs on  Mode SVC_32  Segment kernel
Control: c000717f  Table: 33ecc000  DAC: 00000017
Process swapper (pid: 0, stack limit = 0xc035c258)
Stack: (0xc035ddd8 to 0xc035e000)
ddc0:                                                       c06cec00 00000000
dde0: c07d6780 c06cec2c 03030303 c035de18 c035ddfc c0252680 c0245600 c06cec00
de00: c07d6780 00000000 c07189a0 c035de34 c035de1c c0245994 c02525e0 c06f5160
de20: c07d6140 00000000 c035de44 c035de38 c027ee70 c0245840 c035de64 c035de48
de40: c027eebc c027ee70 03030303 00000000 c06ced30 00000000 c035dea4 c035de68
...

2.2 提供发包函数 

对于真实的网卡,virt_net_send_packet会把skb里的数据通过网卡发送出去

static int virt_net_send_packet(struct sk_buff *skb, struct net_device *dev)
{
    static int cnt = 0;
    printk("virt_net_send_packet cnt = %d\n", ++cnt);
    return 0;
}

vnet_dev->hard_start_xmit = virt_net_send_packet;

重新加载驱动,ping 3.3.3.4成功,发送了8次,但是重新查看数据信息,TX packets为0

 # ifconfig vnet0 3.3.3.3
# ping 3.3.3.4
PING 3.3.3.4 (3.3.3.4): 56 data bytes
virt_net_send_packet cnt = 1
virt_net_send_packet cnt = 2
virt_net_send_packet cnt = 3
virt_net_send_packet cnt = 4
virt_net_send_packet cnt = 5
virt_net_send_packet cnt = 6
virt_net_send_packet cnt = 7
virt_net_send_packet cnt = 8

...

# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:60:6E:33:44:55
          inet addr:192.168.0.19  Bcast:192.168.0.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:3250 errors:0 dropped:0 overruns:0 frame:0
          TX packets:900 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:2734036 (2.6 MiB)  TX bytes:141182 (137.8 KiB)
          Interrupt:51 Base address:0xa000

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:10 errors:0 dropped:0 overruns:0 frame:0
          TX packets:10 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:1120 (1.0 KiB)  TX bytes:1120 (1.0 KiB)

vnet0     Link encap:Ethernet  HWaddr 00:00:00:00:00:00
          inet addr:3.3.3.3  Bcast:3.255.255.255  Mask:255.0.0.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

2.3 添加统计信息

在net_device结构体中有struct net_device_stats stats成员包含了信息

struct net_device_stats
{
	unsigned long	rx_packets;		/* total packets received	*/
	unsigned long	tx_packets;		/* total packets transmitted	*/
	unsigned long	rx_bytes;		/* total bytes received 	*/
	unsigned long	tx_bytes;		/* total bytes transmitted	*/
	unsigned long	rx_errors;		/* bad packets received		*/
	unsigned long	tx_errors;		/* packet transmit problems	*/
	unsigned long	rx_dropped;		/* no space in linux buffers	*/
	unsigned long	tx_dropped;		/* no space available in linux	*/
	unsigned long	multicast;		/* multicast packets received	*/
	unsigned long	collisions;

	/* detailed rx_errors: */
	unsigned long	rx_length_errors;
	unsigned long	rx_over_errors;		/* receiver ring buff overflow	*/
	unsigned long	rx_crc_errors;		/* recved pkt with crc error	*/
	unsigned long	rx_frame_errors;	/* recv'd frame alignment error */
	unsigned long	rx_fifo_errors;		/* recv'r fifo overrun		*/
	unsigned long	rx_missed_errors;	/* receiver missed packet	*/

	/* detailed tx_errors */
	unsigned long	tx_aborted_errors;
	unsigned long	tx_carrier_errors;
	unsigned long	tx_fifo_errors;
	unsigned long	tx_heartbeat_errors;
	unsigned long	tx_window_errors;
	
	/* for cslip etc */
	unsigned long	rx_compressed;
	unsigned long	tx_compressed;
};

 修改hard_start_xmit函数

static int virt_net_send_packet(struct sk_buff *skb, struct net_device *dev)
{
	static int cnt = 0;
	printk("virt_net_send_packet cnt = %d\n", ++cnt);

	/* 更新统计信息 */
	dev->stats.tx_packets++;
	dev->stats.tx_bytes += skb->len;
	
	return 0;
}

设置MAC地址,参考cs89x0.c

 vnet_dev = alloc_netdev(0, "vnet%d", ether_setup);
    vnet_dev->dev_addr[0] = 0x08;
    vnet_dev->dev_addr[1] = 0x89;
    vnet_dev->dev_addr[2] = 0x89;
    vnet_dev->dev_addr[3] = 0x89;
    vnet_dev->dev_addr[4] = 0x89;
    vnet_dev->dev_addr[5] = 0x11;

代码如下:

#include <linux/module.h>
#include <linux/errno.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/fcntl.h>
#include <linux/interrupt.h>
#include <linux/ioport.h>
#include <linux/in.h>
#include <linux/skbuff.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/string.h>
#include <linux/init.h>
#include <linux/bitops.h>
#include <linux/delay.h>

#include <asm/system.h>
#include <asm/io.h>
#include <asm/irq.h>

static struct net_device *vnet_dev;

static int virt_net_send_packet(struct sk_buff *skb, struct net_device *dev)
{
	static int cnt = 0;
	printk("virt_net_send_packet cnt = %d\n", ++cnt);

	/* 对于真实的网卡, 把skb里的数据通过网卡发送出去 */

	/* 更新统计信息 */
	dev->stats.tx_packets++;
	dev->stats.tx_bytes += skb->len;
	
	return 0;
}


static int virt_net_init(void)
{
	/* 1. 分配一个net_device结构体 */
	vnet_dev = alloc_netdev(0, "vnet%d", ether_setup);;  /* alloc_etherdev */

	/* 2. 设置 */
	vnet_dev->hard_start_xmit = virt_net_send_packet;

	/* 设置MAC地址 */
    vnet_dev->dev_addr[0] = 0x08;
    vnet_dev->dev_addr[1] = 0x89;
    vnet_dev->dev_addr[2] = 0x89;
    vnet_dev->dev_addr[3] = 0x89;
    vnet_dev->dev_addr[4] = 0x89;
    vnet_dev->dev_addr[5] = 0x11;

	/* 3. 注册 */
	register_netdev(vnet_dev);
	
	return 0;
}

static void virt_net_exit(void)
{
	unregister_netdev(vnet_dev);
	free_netdev(vnet_dev);
}

module_init(virt_net_init);
module_exit(virt_net_exit);

MODULE_AUTHOR("xiaoma");
MODULE_LICENSE("GPL");

测试结果如下: 

# rmmod virt_net
# insmod virt_net.ko
# ifconfig vnet0 3.3.3.3
# ping 3.3.3.4
PING 3.3.3.4 (3.3.3.4): 56 data bytes
virt_net_send_packet cnt = 1
virt_net_send_packet cnt = 2
virt_net_send_packet cnt = 3
virt_net_send_packet cnt = 4
virt_net_send_packet cnt = 5
virt_net_send_packet cnt = 6

--- 3.3.3.4 ping statistics ---
6 packets transmitted, 0 packets received, 100% packet loss
# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:60:6E:33:44:55
          inet addr:192.168.0.19  Bcast:192.168.0.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:4684 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1023 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:3144891 (2.9 MiB)  TX bytes:157598 (153.9 KiB)
          Interrupt:51 Base address:0xa000

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:16 errors:0 dropped:0 overruns:0 frame:0
          TX packets:16 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:1792 (1.7 KiB)  TX bytes:1792 (1.7 KiB)

vnet0     Link encap:Ethernet  HWaddr 08:89:89:89:89:11
          inet addr:3.3.3.3  Bcast:3.255.255.255  Mask:255.0.0.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 B)  TX bytes:252 (252.0 B)

2.4 构造假包ping通

假设有IP为3.3.3.4的设备,当我们ping它的时候,会发包(hard_start_xmit)给它,它收到包后会返回数据,有数据时要用(netif_rx)上报,就可以ping通,由于现在是虚拟网卡,并没有真正的网线,也没有3.3.3.4的设备,这里构造假包,直接返回数据,这样我们就可以ping得通

构造一个假的sk_buff,上报,函数自己实现

emulator_rx_packet(skb, dev);

参考cs89x0.c,对于真正的网卡,首先会停止该网卡的队列,把skb的数据写入网卡,释放skb就会有中断产生,数据发送完后会在中断函数唤醒网卡的队列,在这里直接唤醒

    /* 对于真实的网卡, 把skb里的数据通过网卡发送出去 */
    netif_stop_queue(dev); /* 停止该网卡的队列 */
    /* ...... */           /* 把skb的数据写入网卡,这里会操作网卡 */

    /* 构造一个假的sk_buff,上报 */
    emulator_rx_packet(skb, dev);

    dev_kfree_skb (skb);   /* 释放skb */
    netif_wake_queue(dev); /* 数据全部发送出去后,唤醒网卡的队列 */

编写emulator_rx_packet,参考LDD3,包中有源目的长度三要素,数据放在skb->data中

static void emulator_rx_packet(struct sk_buff *skb, struct net_device *dev)
{
	unsigned char *type;
	struct iphdr *ih;
	__be32 *saddr, *daddr, tmp;
	unsigned char	tmp_dev_addr[ETH_ALEN];
	struct ethhdr *ethhdr;
	
	struct sk_buff *rx_skb;
		
	// 从硬件读出/保存数据
	/* 对调"源/目的"的mac地址 */
	ethhdr = (struct ethhdr *)skb->data;
	memcpy(tmp_dev_addr, ethhdr->h_dest, ETH_ALEN);
	memcpy(ethhdr->h_dest, ethhdr->h_source, ETH_ALEN);
	memcpy(ethhdr->h_source, tmp_dev_addr, ETH_ALEN);

	/* 对调"源/目的"的ip地址 */    
	ih = (struct iphdr *)(skb->data + sizeof(struct ethhdr));
	saddr = &ih->saddr;
	daddr = &ih->daddr;

	tmp = *saddr;
	*saddr = *daddr;
	*daddr = tmp;
	
	//((u8 *)saddr)[2] ^= 1; /* change the third octet (class C) */
	//((u8 *)daddr)[2] ^= 1;
	type = skb->data + sizeof(struct ethhdr) + sizeof(struct iphdr);
	//printk("tx package type = %02x\n", *type);
	// 修改类型, 原来0x8表示ping
	*type = 0; /* 0表示reply */
	
	ih->check = 0;		   /* and rebuild the checksum (ip needs it) */
	ih->check = ip_fast_csum((unsigned char *)ih,ih->ihl);
	
	// 构造一个sk_buff
	rx_skb = dev_alloc_skb(skb->len + 2);  //保留2个字节
	skb_reserve(rx_skb, 2); /* align IP on 16B boundary */	
	memcpy(skb_put(rx_skb, skb->len), skb->data, skb->len);//环型暖冲区

	/* Write metadata, and then pass to the receive level */
	rx_skb->dev = dev;
	rx_skb->protocol = eth_type_trans(rx_skb, dev);
	rx_skb->ip_summed = CHECKSUM_UNNECESSARY; /* don't check it */
	dev->stats.rx_packets++;  //更新统计信息
	dev->stats.rx_bytes += skb->len;

	// 提交sk_buff
	netif_rx(rx_skb);
}

对于skb->data,一开始是MAC头,在MAC中有目的MAC,源MAC,还有h_proto,当我们返回数据时,需要把目的和源调换一下位置,MAC头紧接着就是IP头,有源IP和目的IP,同样需要调换,还有check校验码用ip_fast_csum函数来校验,IP头紧接着就是type和数据,type对于0x8原来表示ping包,修改为0表示回复包

目的MAC源MAC...源IP目的IP...type数据

struct ethhdr {
    unsigned char    h_dest[ETH_ALEN];    /* destination eth addr    */
    unsigned char    h_source[ETH_ALEN];    /* source ether addr    */
    __be16        h_proto;        /* packet type ID field    */
} __attribute__((packed)); 

ethhdr = (struct ethhdr *)skb->data;

struct iphdr {
#if defined(__LITTLE_ENDIAN_BITFIELD)
    __u8    ihl:4,
        version:4;
#elif defined (__BIG_ENDIAN_BITFIELD)
    __u8    version:4,
          ihl:4;
#else
#error    "Please fix <asm/byteorder.h>"
#endif
    __u8    tos;
    __be16    tot_len;
    __be16    id;
    __be16    frag_off;
    __u8    ttl;
    __u8    protocol;
    __sum16    check;
    __be32    saddr;
    __be32    daddr;
    /*The options start here. */
};

ih = (struct iphdr *)(skb->data + sizeof(struct ethhdr));

加上相关标志,才能ping通

    vnet_dev->flags           |= IFF_NOARP;
    vnet_dev->features        |= NETIF_F_NO_CSUM;

整体代码:

#include <linux/module.h>
#include <linux/errno.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/fcntl.h>
#include <linux/interrupt.h>
#include <linux/ioport.h>
#include <linux/in.h>
#include <linux/skbuff.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/string.h>
#include <linux/init.h>
#include <linux/bitops.h>
#include <linux/delay.h>
#include <linux/ip.h>  // 添加ip头文件

#include <asm/system.h>
#include <asm/io.h>
#include <asm/irq.h>

static struct net_device *vnet_dev;

static void emulator_rx_packet(struct sk_buff *skb, struct net_device *dev)
{
	/* 参考LDD3 */
	unsigned char *type;
	struct iphdr *ih;
	__be32 *saddr, *daddr, tmp;
	unsigned char	tmp_dev_addr[ETH_ALEN];
	struct ethhdr *ethhdr;
	
	struct sk_buff *rx_skb;
		
	// 从硬件读出/保存数据
	/* 对调"源/目的"的mac地址 */
	ethhdr = (struct ethhdr *)skb->data;
	memcpy(tmp_dev_addr, ethhdr->h_dest, ETH_ALEN);
	memcpy(ethhdr->h_dest, ethhdr->h_source, ETH_ALEN);
	memcpy(ethhdr->h_source, tmp_dev_addr, ETH_ALEN);

	/* 对调"源/目的"的ip地址 */    
	ih = (struct iphdr *)(skb->data + sizeof(struct ethhdr));
	saddr = &ih->saddr;
	daddr = &ih->daddr;

	tmp = *saddr;
	*saddr = *daddr;
	*daddr = tmp;
	
	//((u8 *)saddr)[2] ^= 1; /* change the third octet (class C) */
	//((u8 *)daddr)[2] ^= 1;
	type = skb->data + sizeof(struct ethhdr) + sizeof(struct iphdr);
	//printk("tx package type = %02x\n", *type);
	// 修改类型, 原来0x8表示ping
	*type = 0; /* 0表示reply */
	
	ih->check = 0;		   /* and rebuild the checksum (ip needs it) */
	ih->check = ip_fast_csum((unsigned char *)ih,ih->ihl);
	
	// 构造一个sk_buff
	rx_skb = dev_alloc_skb(skb->len + 2);
	skb_reserve(rx_skb, 2); /* align IP on 16B boundary */	
	memcpy(skb_put(rx_skb, skb->len), skb->data, skb->len);

	/* Write metadata, and then pass to the receive level */
	rx_skb->dev = dev;
	rx_skb->protocol = eth_type_trans(rx_skb, dev);
	rx_skb->ip_summed = CHECKSUM_UNNECESSARY; /* don't check it */
	dev->stats.rx_packets++;
	dev->stats.rx_bytes += skb->len;

	// 提交sk_buff
	netif_rx(rx_skb);
}

static int virt_net_send_packet(struct sk_buff *skb, struct net_device *dev)
{
	static int cnt = 0;
	printk("virt_net_send_packet cnt = %d\n", ++cnt);

	/* 对于真实的网卡, 把skb里的数据通过网卡发送出去 */
	netif_stop_queue(dev); /* 停止该网卡的队列 */
    /* ...... */           /* 把skb的数据写入网卡 */

	/* 构造一个假的sk_buff,上报 */
	emulator_rx_packet(skb, dev);

	dev_kfree_skb (skb);   /* 释放skb */
	netif_wake_queue(dev); /* 数据全部发送出去后,唤醒网卡的队列 */

	/* 更新统计信息 */
	dev->stats.tx_packets++;
	dev->stats.tx_bytes += skb->len;
	
	return 0;
}


static int virt_net_init(void)
{
	/* 1. 分配一个net_device结构体 */
	vnet_dev = alloc_netdev(0, "vnet%d", ether_setup);;  /* alloc_etherdev */

	/* 2. 设置 */
	vnet_dev->hard_start_xmit = virt_net_send_packet;

	/* 设置MAC地址 */
    vnet_dev->dev_addr[0] = 0x08;
    vnet_dev->dev_addr[1] = 0x89;
    vnet_dev->dev_addr[2] = 0x89;
    vnet_dev->dev_addr[3] = 0x89;
    vnet_dev->dev_addr[4] = 0x89;
    vnet_dev->dev_addr[5] = 0x11;

    /* 设置下面两项才能ping通 */
	vnet_dev->flags           |= IFF_NOARP;
	vnet_dev->features        |= NETIF_F_NO_CSUM;	

	/* 3. 注册 */
	register_netdev(vnet_dev);
	
	return 0;
}

static void virt_net_exit(void)
{
	unregister_netdev(vnet_dev);
	free_netdev(vnet_dev);
}

module_init(virt_net_init);
module_exit(virt_net_exit);

MODULE_AUTHOR("xiaoma");
MODULE_LICENSE("GPL");

 测试结果:能ping 3.3.3.4并查看统计信息

# rmmod virt_net
# insmod virt_net.ko
# ifconfig vnet0 3.3.3.3
# ping 3.3.3.4
PING 3.3.3.4 (3.3.3.4): 56 data bytes
virt_net_send_packet cnt = 1
64 bytes from 3.3.3.4: seq=0 ttl=64 time=0.838 ms
virt_net_send_packet cnt = 2
64 bytes from 3.3.3.4: seq=1 ttl=64 time=0.518 ms
virt_net_send_packet cnt = 3
64 bytes from 3.3.3.4: seq=2 ttl=64 time=0.504 ms
virt_net_send_packet cnt = 4
64 bytes from 3.3.3.4: seq=3 ttl=64 time=0.442 ms
virt_net_send_packet cnt = 5
64 bytes from 3.3.3.4: seq=4 ttl=64 time=0.502 ms

--- 3.3.3.4 ping statistics ---
5 packets transmitted, 5 packets received, 0% packet loss
round-trip min/avg/max = 0.442/0.560/0.838 ms

ifconfig

...
vnet0     Link encap:Ethernet  HWaddr 08:89:89:89:89:11
          inet addr:3.3.3.3  Bcast:3.255.255.255  Mask:255.0.0.0
          UP BROADCAST RUNNING NOARP MULTICAST  MTU:1500  Metric:1
          RX packets:5 errors:0 dropped:0 overruns:0 frame:0
          TX packets:5 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:490 (490.0 B)  TX bytes:490 (490.0 B)

 

相关推荐
©️2020 CSDN 皮肤主题: 黑客帝国 设计师:白松林 返回首页