- 论坛徽章:
- 0
|
本帖最后由 shuimuyq 于 2015-07-29 17:16 编辑
源码如下:
/* Sample code to install a Netfilter hook function that will
* drop all incoming packets. */
#define __KERNEL__
#define MODULE
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
/* This is the structure we shall use to register our function */
static struct nf_hook_ops nfho;
/* This is the hook function itself */
unsigned int hook_func(unsigned int hooknum,
struct sk_buff **skb,
const struct net_device *in,
const struct net_device *out,
int (*okfn)(struct sk_buff *))
{
return NF_DROP; /* Drop ALL packets */
}
/* Initialisation routine */
int init_module()
{
/* Fill in our hook structure */
nfho.hook = hook_func; /* Handler function */
nfho.hooknum = NF_IP_PRE_ROUTING; /* First hook for IPv4 */
nfho.pf = PF_INET;
nfho.priority = NF_IP_PRI_FIRST; /* Make our function first */
nf_register_hook(&nfho);
return 0;
}
/* Cleanup routine */
void cleanup_module()
{
nf_unregister_hook(&nfho);
}
makefile如下:
obj-m += dropall.o
all: $(obj-m)
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
编译错误信息如下:
cc -c -o dropall.o dropall.c
dropall.c:7:26: error: linux/module.h: No such file or directory
In file included from dropall.c:9:
/usr/include/linux/netfilter.h:64: error: field ‘in’ has incomplete type
/usr/include/linux/netfilter.h:65: error: field ‘in6’ has incomplete type
In file included from dropall.c:10:
/usr/include/linux/netfilter_ipv4.h:53: error: ‘INT_MIN’ undeclared here (not in a function)
/usr/include/linux/netfilter_ipv4.h:64: error: ‘INT_MAX’ undeclared here (not in a function)
dropall.c:20: warning: ‘struct net_device’ declared inside parameter list
dropall.c:20: warning: its scope is only this definition or declaration, which is probably not what you want
dropall.c:20: warning: ‘struct sk_buff’ declared inside parameter list
dropall.c: In function ‘init_module’:
dropall.c:29: error: invalid use of undefined type ‘struct nf_hook_ops’
dropall.c:30: error: invalid use of undefined type ‘struct nf_hook_ops’
dropall.c:31: error: invalid use of undefined type ‘struct nf_hook_ops’
dropall.c:31: error: ‘PF_INET’ undeclared (first use in this function)
dropall.c:31: error: (Each undeclared identifier is reported only once
dropall.c:31: error: for each function it appears in.)
dropall.c:32: error: invalid use of undefined type ‘struct nf_hook_ops’
make: *** [dropall.o] Error 1
不了解模块编译要怎么处理,求帮助! |
|