能ping别人,但别人ping 不到

来源:互联网 发布:靠谱的无印良品淘宝店 编辑:程序博客网 时间:2024/04/29 08:42
/*
* File : li_filter.c writen by kunlong
* Kernel : 2.2.16 or 2.2.14 or 2.4.2
* Complie : gcc -O3 -c li_filter.c
* Usage : insmod li_filter.o -f
* Date : 2001-06-28
*/
#ifndef __KERNEL__
#define __KERNEL__
#endif
#ifndef MODULE
#define MODULE
#endif
#include <linux/config.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/version.h>
#include <linux/netdevice.h>
#include <linux/if_ether.h>
#include <linux/if_packet.h>
#include <linux/skbuff.h>
#include <linux/ip.h>
#include <linux/icmp.h>
#include <linux/in.h>
#ifndef KERNEL_VERSION
#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
#endif

#if LINUX_VERSION_CODE > KERNEL_VERSION(2,2,0)
#include <asm/uaccess.h>
#endif

static struct device * filter_dev = NULL;
static char * dev = NULL;

/* 定义insmod命令行参数 */
#if LINUX_VERSION_CODE > KERNEL_VERSION(2,2,0)
MODULE_PARM( dev, "s" );
#endif

int filter_rcv ( struct sk_buff * skb, struct device * dv, struct packet_type * pt )
{
/* 注意pkt_type是什么 */
if ( ( skb->pkt_type == PACKET_HOST ) && ( skb->protocol == __constant_htons( ETH_P_IP) ) )
{
if ( ( skb->nh.iph->version == 4 ) && ( skb->nh.iph->protocol == IPPROTO_ICMP ) ) /* 不考虑ipv6 */
{
skb->h.raw = skb->nh.raw + ( skb->nh.iph->ihl << 2 );
if ( skb->h.icmph->type == 8 )
{
unsigned char *saddr = &(skb->nh.iph->saddr);
printk("<1>--- ping from: %d.%d.%d.%d ---/n", *saddr,*(saddr+1),*(saddr+2),*(saddr+3));

skb->h.icmph->checksum += 1; // Change the checksum, then this IP packet will be Ignored.
}
}
}
kfree_skb( skb );
return( 0 );
} /* end of filter_rcv */

static struct packet_type filter_packet_type =
{
__constant_htons( ETH_P_ALL ), /* 此时可以接收到来自lo的回送报文,比如本机发送出去的 */
NULL, /* All devices */
filter_rcv,
NULL, /* 如果是2.4内核,这里可以考虑设置成非零,但是filter_rcv需要改变 */
NULL,
};

int init_module ( void ) /* 模块初始化 */
{
if ( dev != NULL )
{
filter_dev = dev_get( dev );
if ( filter_dev != NULL )
{
filter_packet_type.dev = filter_dev;
}
}
dev_add_pack( &filter_packet_type );
EXPORT_NO_SYMBOLS;
return( 0 );
} /* end of init_module */

void cleanup_module ( void ) /* 模块卸载 */
{
dev_remove_pack( &filter_packet_type );
return;
} /* end of cleanup_module */
原创粉丝点击