netfilter test examples are for linux 2.4. Now these examples are rewrite on linux kernel 3.2.19
filter_tcp.c
--------------------Begin--------------------------------------
/* Sample code to install a Netfilter hook function that will
* drop all incoming packets from an IP address we specify */
//#define __KERNEL__
//#define MODULE
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/skbuff.h>
#include <linux/ip.h> /* For IP header */
#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;
/* IP address we want to drop packets from, in NB order */
//static unsigned char *drop_ip = "\x7f\x00\x00\x01";
/* 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 *))
{
struct iphdr *ip1 = NULL;
if (!skb){
return NF_ACCEPT;
}
ip1 = ip_hdr(skb);
if (NULL != ip1){
if (IPPROTO_TCP == ip1->protocol){
printk("tcp,drop\n");
return NF_DROP;
}
}else{
printk("null!\n");
}
return NF_ACCEPT;
}
/* Initialisation routine */
int init_module()
{
/* Fill in our hook structure */
nfho.hook = hook_func;
/* Handler function */
nfho.hooknum = NF_INET_PRE_ROUTING; /* First for IPv4 */
nfho.pf = PF_INET;
nfho.priority = NF_IP_PRI_FIRST; /* Make our func first */
printk("init_module,filter_tcp\n");
nf_register_hook(&nfho);
return 0;
}
/* Cleanup routine */
void cleanup_module()
{
printk("cleanup_module,filter_tcp\n");
nf_unregister_hook(&nfho);
}
----------------------End-----------------------
Makefile
-------------------------Begin---------------------
MODULE_NAME:=filter_tcp
ifneq ($(KERNELRELEASE),)
mymodule-objs:=${MODULE_NAME}.o
obj-m:=${MODULE_NAME}.o
else
PWD:=$(shell pwd)
KVER:=$(shell uname -r)
KDIR:=/usr/src/linux-source-3.2.0/linux-source-3.2.0
all:
$(MAKE) -C $(KDIR) M=$(PWD)
clean:
@rm -rf .*.com *.o *.mod.c *.ko .tmp_versions modules.order Module.symvers
install:
echo ${KDIR}
@insmod ${MODULE_NAME}.ko
uninstall:
@rmmod ${MODULE_NAME}.ko
endif
-------------------------End-----------------------
filter_tcp.c
--------------------Begin--------------------------------------
/* Sample code to install a Netfilter hook function that will
* drop all incoming packets from an IP address we specify */
//#define __KERNEL__
//#define MODULE
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/skbuff.h>
#include <linux/ip.h> /* For IP header */
#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;
/* IP address we want to drop packets from, in NB order */
//static unsigned char *drop_ip = "\x7f\x00\x00\x01";
/* 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 *))
{
struct iphdr *ip1 = NULL;
if (!skb){
return NF_ACCEPT;
}
ip1 = ip_hdr(skb);
if (NULL != ip1){
if (IPPROTO_TCP == ip1->protocol){
printk("tcp,drop\n");
return NF_DROP;
}
}else{
printk("null!\n");
}
return NF_ACCEPT;
}
/* Initialisation routine */
int init_module()
{
/* Fill in our hook structure */
nfho.hook = hook_func;
/* Handler function */
nfho.hooknum = NF_INET_PRE_ROUTING; /* First for IPv4 */
nfho.pf = PF_INET;
nfho.priority = NF_IP_PRI_FIRST; /* Make our func first */
printk("init_module,filter_tcp\n");
nf_register_hook(&nfho);
return 0;
}
/* Cleanup routine */
void cleanup_module()
{
printk("cleanup_module,filter_tcp\n");
nf_unregister_hook(&nfho);
}
----------------------End-----------------------
Makefile
-------------------------Begin---------------------
MODULE_NAME:=filter_tcp
ifneq ($(KERNELRELEASE),)
mymodule-objs:=${MODULE_NAME}.o
obj-m:=${MODULE_NAME}.o
else
PWD:=$(shell pwd)
KVER:=$(shell uname -r)
KDIR:=/usr/src/linux-source-3.2.0/linux-source-3.2.0
all:
$(MAKE) -C $(KDIR) M=$(PWD)
clean:
@rm -rf .*.com *.o *.mod.c *.ko .tmp_versions modules.order Module.symvers
install:
echo ${KDIR}
@insmod ${MODULE_NAME}.ko
uninstall:
@rmmod ${MODULE_NAME}.ko
endif
-------------------------End-----------------------