初次使用netfilter,写了一个禁止ping命令的小程序

banping.c

#include"linux/module.h"
#include"linux/netfilter_ipv4.h"
#include"linux/kernel.h"
#include"linux/skbuff.h"
#include"linux/ip.h"
#include"linux/if_ether.h"
#include"linux/if_packet.h"


/*copyright statement*/
MODULE_LICENSE("Dual BSD/GPL");


static unsigned int nf_hook_out(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 *sk = skb;
	struct iphdr *iph=ip_hdr(sk);
	if(iph->protocol==IPPROTO_ICMP)
	{
		return NF_DROP;
	}else
	{
		return NF_ACCEPT;
	}
}


static struct nf_hook_ops nfout=
{
	.list = {NULL,NULL},
	.hook = nf_hook_out,
	.hooknum = NF_INET_LOCAL_OUT,
	.pf = PF_INET,
	.priority = NF_IP_PRI_FIRST
};

/*initialization module*/
static int __init banping_init(void)
{
	nf_register_hook(&nfout);
	printk(KERN_ALERT"Banping module init\n");
	return 0;
}
/*clear module*/
static void __exit banping_exit(void)
{
	nf_unregister_hook(&nfout);
	printk(KERN_ALERT"Banping module exit\n");
}
module_init(banping_init);
module_exit(banping_exit);

MODULE_AUTHOR("FEI FENG");
MODULE_DESCRIPTION("Ban ping");
MODULE_VERSION("0.0.1");

makefile

target = banping
obj-m := $(target).o
KERNELDIR = /lib/modules/$(shell uname -r)/build
default:
	$(MAKE) -C $(KERNELDIR) M=$(shell pwd)
install:
	insmod $(target).ko
uninstall:
	rmmod $(target).ko
clean:
	rm -rf *.o *.mod.c *.ko
	rm -rf Module.symvers .*cmd .tmp_versions


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值