Quantcast
Channel: Linux Kernel Newbies Forum
Viewing all articles
Browse latest Browse all 7450

Using Netfilter in a Module and getting a crash (1 reply)

$
0
0
/* 
	Coder: Adel *. *******
	Creation Date: April/5th/2012
	Last Modification Date: April/6th/2012
	Purpose: A module to test capturing traffic and just letting it go after knowing if it's an ICMP traffic or not
	Notes: This modules has always been crashing the kernel I am running it on(it shouldn't), my kernel is 2.6.32-33 (Note by Adel)
 */
#include <linux/module.h>       /* Needed by all modules */
#include <linux/kernel.h>       /* Needed for KERN_INFO */
#include <linux/init.h>         /* Needed for the macros */

#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>

#include <linux/skbuff.h>		/* For the sk_buff struct, which is the struct that contains EVERYTHING in a network packet */
#include <linux/ip.h>                  /* For IP header */
#include <linux/icmp.h>		       /* For ICMP Header */

#include <linux/in.h> /* For the IPPROTO_ICMP enum */ 

/* This is the structure we shall use to register our function */
static struct nf_hook_ops nfho;

/* 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 sk_buff *sb = *skb;
	struct iphdr* iph;
	struct icmphdr *icmph;	
	iph = ip_hdr(sb);
	if(sb == NULL)
		return NF_ACCEPT;
	if(iph != NULL){
		printk(KERN_DEBUG"IP header is not null\n");
		if(iph->protocol == IPPROTO_ICMP){
			icmph = icmp_hdr(sb);
			if(icmph != NULL){
				printk(KERN_DEBUG"ICMP header is not null\n");
				return NF_ACCEPT;
			}/* If ICMP not null */
			return NF_ACCEPT;
		}/* if IPPROTO_ICMP */
		return NF_ACCEPT;
	}
	return NF_DROP;/* The packet is NULL */
}


static int __init hello_start(void)
{
	printk(KERN_INFO "Loading Test module...\n");
	printk(KERN_ALERT "Hello world\n");
  	/* Fill in our hook structure */
    	nfho.hook = hook_func;         /* Handler function */
    	nfho.hooknum  = NF_INET_POST_ROUTING; /* POST_ROUTING Traffic before it hits the wire */
    	nfho.pf       = PF_INET;
    	nfho.priority = NF_IP_PRI_FIRST;   /* Make our function first */

    	nf_register_hook(&nfho);
	return 0;
}

static void __exit hello_end(void)
{
	nf_unregister_hook(&nfho);
	printk(KERN_ALERT "Goodbye Mr.\n");
}

module_init(hello_start);
module_exit(hello_end);

As you can see, the code is doing nothing except just capturing the traffic, checking if it's ICMP and if it is, it prints that the struct of the ICMP is not null.
EVERYTIME I load this module, my whole system crashes(the keyboard leds start flashing even)

Am I doing anything stupid here?

Viewing all articles
Browse latest Browse all 7450

Trending Articles