1. 简介
本文章主要实现一个在linux3.4.xxx下的虚拟网卡,并在虚拟网卡中实现 ping 的命令。
网络部分的逻辑分层图如下:
2. 实现简单的虚拟网卡的第一步
virt_net.c 文件
/*
* 参考 drivers\net\cs89x0.c
*/
#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 const struct net_device_ops virt_netdev_ops = {
.ndo_start_xmit = NULL,
};
static int virt_net_init(void)
{
/* 1. 分配一个net_device结构体 */
vnet_dev = alloc_netdev(0, "vnet%d", ether_setup); /* alloc_etherdev */
/* 2. 设置 */
vnet_dev->netdev_ops = &virt_netdev_ops;
/* 3. 注册 */
//register_netdevice(vnet_dev);
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("thisway.diy@163.com,17653039@qq.com");
MODULE_LICENSE("GPL");
Makefile 文件
KERN_DIR = ~/wor_lip/linux-3.4.112
all:
make -C $(KERN_DIR) M=`pwd` modules
clean:
make -C $(KERN_DIR) M=`pwd` modules clean
rm -rf modules.order
obj-m += virt_net.o
需要特别提醒的是,至少应该提供 netdev_ops 中的 ndo_start_xmit 方法。
编译通过后加载。
ifconfig -a 查看所有网卡,
ifconfig vnet0 192.0.0.2,然后就可以 ping 这个地址了,此时如果ping的是192.0.0.1,会调用发送函数,而此部分函数没有提供发送函数,便会 Kernel panic
MAC地址 00:00:00:00:00:00
3. 一个完整的模拟回环网卡程序,实现数据包的自发自收过程
实现思路主要就是交换数据包中的源目的MAC头信息,源目的IP信息等功能。
#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>
#include <asm/system.h>
#include <asm/io.h>
#include <asm/irq.h>
static struct net_device *vnet_dev;
/*
* [ethhdr, iphdr, type]
*/
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 const struct net_device_ops virt_netdev_ops = {
.ndo_start_xmit = virt_net_send_packet,
};
static int virt_net_init(void)
{
/* 1. 分配一个net_device结构体 */
vnet_dev = alloc_netdev(0, "vnet%d", ether_setup);; /* alloc_etherdev */
/* 2. 设置 */
vnet_dev->netdev_ops = &virt_netdev_ops;
/* 设置MAC地址 08:89:... */
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_netdevice(vnet_dev);
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_LICENSE("GPL");
此时可以 ping 相同网段下的不同地址了,比如同上例中的 ping 192.0.0.1 便能成功了。
特殊说明:
停止网卡队列后,将发送的 skb 中的 ethhdr 中的原地址和目的地址交换,iphdr 中的原目的和目的地址,并将 skb 中的 data 区域复制到 新申请的用于发送的新的 sk_buff 的data区,并用 netif_rx "发送",递交到上层。用 netif_wake_queue(dev); 唤醒网卡的队列,发送出去。