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=ð0_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通