虚拟网络驱动,能够ifconfig能够看到网络,并且能够ping通

hello.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 <linux/ip.h>

MODULE_LICENSE(“Dual BSD/GPL”);
MODULE_AUTHOR(“YYZ”);

static struct net_device *eth0_dev;
static int eth0_send_packer(struct sk_buff *skb,struct net_device *dev)
{
dev->stats.tx_packets++;
dev->stats.tx_bytes+=skb->len;
dev_kfree_skb(skb);
return 0;
}
static struct net_device_ops eth0_ops=
{
.ndo_start_xmit=eth0_send_packer,
};
static int eth0_init(void)
{
eth0_dev=alloc_netdev(0,“eth%d”,NET_NAME_UNKNOWN,ether_setup);
eth0_dev->netdev_ops=&eth0_ops;

memcpy(eth0_dev->dev_addr,"\x01\x02\x03\x04\x05\x06",6);
eth0_dev->flags |= IFF_NOARP;
register_netdev(eth0_dev);
return 0;

}
static void eth0_exit(void)
{
unregister_netdev(eth0_dev);
free_netdev(eth0_dev);
}

module_init(eth0_init);
module_exit(eth0_exit);

Makefile:
obj-m := hello.o
KERNEL_DIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
all:
make -C ( K E R N E L D I R ) S U B D I R S = (KERNEL_DIR) SUBDIRS= (KERNELDIR)SUBDIRS=(PWD) modules
clean:
rm *.o *.ko *.mod.c

.PHONY:clean

挂载模块能够实现,ifconfig 查看到网络设备,设置ip后能够ping通

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值