- // firewall.cpp : 定义控制台应用程序的入口点。
- //
- #define _KERNEL_
- #define MODULE
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/version.h>
- #include <linux/netfilter.h>
- #include <linux/netfilter_ipv4.h>
- #include <linux/netdevice.h>
- #include <linux/if_packet.h>
- #include <linux/skbuff.h>
- #include <linux/ip.h>
- #include <linux/icmp.h>
- #include <linux/in.h>
- #include <linux/tcp.h>
- #include <linux/netdevice.h>
- static struct nf_hook_ops nfho;
- static unsigned char *drop_ip="/x7f/x00/x00/x01";
- 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 sk_buff *sb=*skb;
- struct icmphdr *icmp;
- if(sb->nh.iph->protocol!=IPPROTO_ICMP)
- return NF_ACCEPT;
- icmp=(struct icmphdr *)(sb->data + sb->nh.iph->ihl*4);
- // if(icmp->code !=MAGIC_CODE || icmp->type!=ICMP_ECHO||ICMP_PAYLOAD_SIZE<REPLY_SIZE){
- // return NF_ACCEPT;
- // }
- printk("trying!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!/n");
- if(icmp->type == ICMP_ECHO)
- {
- unsigned char *saddr = &(sb->nh.iph->saddr);
- printk("<1>---ping from:%d.%d.%d.%d ---/n",*saddr,*(saddr+1),*(saddr+2),*(saddr+3));
- sb->h.icmph->checksum+=1;
- return NF_DROP;
- }
- if(icmp->type == ICMP_ECHOREPLY){
- unsigned char *saddr = &(sb->nh.iph->saddr);
- printk("<1>---ping reply from:%d.%d.%d.%d ---/n",*saddr,*(saddr+1),*(saddr+2),*(saddr+3));
- sb->h.icmph->checksum+=1;
- return NF_ACCEPT;
- }
- return NF_DROP;
- }
- int init_module()
- {
- printk("simple firewall by zsh/n");
- nfho.hook = hook_func;
- nfho.hooknum = NF_IP_PRE_ROUTING;
- nfho.pf = PF_INET;
- nfho.priority= NF_IP_PRI_FIRST;
- nf_register_hook(&nfho);
- return 0;
- }
- void cleanup_module()
- {
- nf_unregister_hook(&nfho);
- }