日期:2014-05-16 浏览次数:20762 次
#include <linux/module.h> #include <linux/init.h> #include <linux/netfilter.h> #include <linux/netfilter_ipv4.h> #include <linux/kernel.h> #include <linux/skbuff.h> #include <linux/udp.h> #include <linux/ip.h> MODULE_LICENSE("Dual BSD/GPL"); static struct nf_hook_ops nfho; //struct holding set of hook function options struct sk_buff *sock_buff; struct udphdr *udp_header; //udp header struct (not used) struct iphdr *ip_header; //function to be called by hook 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 *)) { sock_buff = *skb; ip_header = (struct iphdr *)skb_network_header(sock_buff); //grab network header using accessor if(!sock_buff) { return NF_ACCEPT;} if (ip_header->protocol==17) { udp_header = (struct udphdr *)skb_transport_header(sock_buff); //grab transport header printk(KERN_INFO "got udp packet \n"); //log we’ve got udp packet to /var/log/messages return NF_DROP; } return NF_ACCEPT; } static int hello_init(void) { nfho.hook = hook_func; //function to call when conditions below met nfho.hooknum = NF_IP_PRE_ROUTING; //called right after packet recieved, first hook in Netfilter nfho.pf = PF_INET; //IPV4 packets nfho.priority = NF_IP_PRI_FIRST; //set to highest priority over all other hook functions nf_register_hook(&nfho); //register hook return 0; } static void hello_exit(void) { nf_unregister_hook(&nfho); } module_init(hello_init); module_exit(hello_exit);
#KVER = /usr/src/linux-source-2.6.38 KVER = /lib/modules/2.6.18-274.el5/build CURDIR = $(shell pwd) # Kernel modules obj-m := hello.o build: kernel_modules kernel_modules: $(MAKE) -C $(KVER) M=$(CURDIR) modules clean: $(MAKE) -C $(KVER) M=$(CURDIR) clean