Skip to contentSkip to conversation

Is my program is correct to print packet data part at IP layer?

DISCUSSION linux lover 2 participants 2 messages Dec 31, 2004 – Jan 1, 2005 netfilter

lore
Share & tools
lore
#0 lore raw

Respected sir,
I request you to please check my program
and reply me whether it is the correct way of getting packets data part at IP layer? I am using netfilter and dumping its output to unsigned string. I just want to know the results return by following program is correct or wrong?
regards,
linux_lover.

#define MODULE
#define __KERNEL__

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/skbuff.h>
#include <linux/ip.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>

#include <linux/string.h>

static struct nf_hook_ops nfho1;

unsigned int hook_func1(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; printk(KERN_DEBUG "calling hook_func at

NF_IP_LOCAL_OUT\n");
      unsigned char *packet;
      unsigned int buflen;
      int  i=0;
      buflen=sb->len;
      packet=kmalloc(buflen,GFP_USER);
      memset(packet,'\0',buflen);
      printk(KERN_DEBUG "Length of skb->data in hook
function = %d\n", buflen);
      strncpy(packet,sb->data,buflen);
      printk(KERN_DEBUG "packet contents of sb->data

in hook function = %02x\n", packet);

     return NF_ACCEPT;
}

static int __init init(void)

  {
              nfho1.hook     = hook_func1;
              nfho1.hooknum  = NF_IP_LOCAL_OUT;
              nfho1.pf       = PF_INET;
              nfho1.priority = NF_IP_PRI_FIRST;
              nf_register_hook(&nfho1);
              return 0;
          }

static void __exit fini(void)

          {
              nf_unregister_hook(&nfho1);
          }

module_init(init);
module_exit(fini);
MODULE_LICENSE("GPL");

__________________________________
Do you Yahoo!?
The all-new My Yahoo! - What will yours do?
http://my.yahoo.com

#1 lore raw
Quoted 7 lines · linux lover

Respected sir,
I request you to please check my program
and reply me whether it is the correct way of getting packets data part at IP layer? I am using netfilter and dumping its output to unsigned string. I just want to know the results return by following program is correct or wrong?

Hi,

The data field may not be linear; it will be in pieces for many
packets. Also, skb->data will point at the IP header, not the data within the packet. I recommend looking at ip_conntrack_ftp which has to look inside TCP packets, for example.

Good luck!
Rusty.

Signature · 1 line
A bad analogy is like a leaky screwdriver -- Richard Braakman