Find the answer to your Linux question:
Results 1 to 3 of 3
Hi, I have to write a module to implement a simple Firewall taking the datas directly from the sk_buff. I donīt know how to make a test to determine the ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jul 2010
    Posts
    5

    sk_buff network_header access


    Hi,

    I have to write a module to implement a simple Firewall taking the datas directly from the sk_buff. I donīt know how to make a test to determine the packet network header (to know if it is IP or sth else).
    Here is what I do, but does not work:


    if (&skb->network_header == ETH_P_IP){
    name_NLP = "IP";
    printk("Network Layer Header = %s\n",name_NLP);
    }

    I tried to use access function skb_network_header(skb) but does not work neither.

    Any ideas?

  2. #2
    Just Joined!
    Join Date
    Jul 2010
    Posts
    3
    I am just learning this stuff too but if I had to venture a guess, I beleive that skb->network_header is probably in network byte order so for a comparison you would need

    skb->network_header == htons(ETH_P_IP)

  3. #3
    Just Joined!
    Join Date
    Jul 2010
    Posts
    5
    thks but I finally find it. Here is my module:

    Code:
    /******************************************************
     * file:     LengthSkbuff.c
     * Date:     05/07/2010
     * Author:   Maxime ZAGO
     * Simple packet capture && analysis programm
     * Uses SKbuff
     *****************************************************/
    
    #include<linux/module.h>
    #include<linux/kernel.h>
    #include<linux/init.h>
    #include<linux/cdev.h>
    #include<linux/skbuff.h>
    #include<linux/netdevice.h>
    #include <linux/udp.h>
    #include <linux/tcp.h>
    #include <linux/ip.h>
    
    
    /* Module Information */
    #define AUTHOR "Maxime ZAGO <mzago@etu.enseeiht.fr>"
    #define DESCRIPTION   " Simple packet capture programm"
    MODULE_AUTHOR(AUTHOR);
    MODULE_DESCRIPTION(DESCRIPTION);	
    MODULE_LICENSE("GPL");
    
    /* Is the module used */
    #define	MOD_INC_USE_COUNT
    #define MOD_DEC_USE_COUNT
    
    /* Number of packets to capture */
    #define NB_PACKETS 10
    
    int pck_cptr;   /* Counter for number of packets */
    
    
    /* This is the function that handles the packets. */
    static __s32 packetprocessor_func(struct sk_buff *skb,
    		struct net_device *dev, 
    		struct packet_type *pt,
    		struct net_device *orig_dev){
    
    	struct iphdr *network_header;  // ip header struct
    	struct tcphdr *tcp_header;     // tcp header struct
    	struct udphdr *udp_header;     // udp header struct 
    	int sport, dport;              // source & dest port
    
    
    	if(pck_cptr<NB_PACKETS){
    		printk("\n==> Packet number %d\n",pck_cptr+1);
    		network_header = (struct iphdr *)skb_network_header(skb);
    		if (network_header->protocol==IPPROTO_UDP){
    			udp_header = (struct udphdr *)skb_transport_header(skb); 
    			sport = ntohs((unsigned short int) udp_header->source);
    			dport = ntohs((unsigned short int) udp_header->dest);
    			printk(" IN: %s\n",skb->dev->name);
    			printk(" Protocol: UDP\n");
    			printk(" Length: %d\n",skb->len);
    			printk(" TTL: %d\n",network_header->ttl);
    			printk(" ID: %d\n",network_header->id);
    			printk(" S_PORT: %d\n",sport);
    			printk(" D_PORT: %d\n",dport);
    			printk(" @_SRC: %d.%d.%d.%d\n",NIPQUAD(network_header->saddr));
    			printk(" @_DST: %d.%d.%d.%d\n",NIPQUAD(network_header->daddr));
    		}
    		if (network_header->protocol==IPPROTO_TCP){
    			tcp_header = (struct tcphdr *)skb_transport_header(skb); 
    			sport = ntohs((unsigned short int) tcp_header->source);
    			dport = ntohs((unsigned short int) tcp_header->dest);
    			printk(" IN: %s\n",skb->dev->name);
    			printk(" Protocol: TCP\n");
    			printk(" Length: %d\n",skb->len);
    			printk(" TTL: %d\n",network_header->ttl);
    			printk(" ID: %d\n",network_header->id);
    			printk(" S_PORT: %d\n",sport);
    			printk(" D_PORT: %d\n",dport);
    			printk(" @_SRC: %d.%d.%d.%d\n",NIPQUAD(network_header->saddr));
    			printk(" @_DST: %d.%d.%d.%d\n",NIPQUAD(network_header->daddr));
    
    		}
    		pck_cptr ++;
    		kfree_skb(skb);
    	}
    	return 0;
    }
    
    static struct packet_type pt = { 
    	.type = __constant_htons(ETH_P_IP),
    	.func = packetprocessor_func,
    };
    
    static __s32 __init packetprocessor_init(void){
    	MOD_INC_USE_COUNT; // Module in use
    	printk(KERN_ALERT "\n<==========================>\n");
    	printk(KERN_ALERT "Loading Packet Processor.\n");
    	printk(KERN_ALERT "Letīs catch %d packets\n",NB_PACKETS);
    	printk(KERN_ALERT "<==========================>\n");
    	dev_add_pack(&pt); // Register the packet handler.
    	return 0;
    }
    
    static void __exit packetprocessor_exit(void){
    	printk(KERN_ALERT "\n<==========================>\n");
    	printk(KERN_ALERT "Unloading Packet Processor.\n");
    	printk(KERN_ALERT "<==========================>\n");
    	dev_remove_pack(&pt);
    	MOD_DEC_USE_COUNT;
    }
    
    module_init(packetprocessor_init);
    module_exit(packetprocessor_exit);

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •