[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re:Question about skbuff



Hi all,

I have a problem while I try to retrieve the pointer of the 
data content (payload) in skbuff (TCP protocol)

And the code is the following.

#define MODULE
#include <linux/module.h>

#include <linux/init.h>
#include <linux/config.h>
#include <linux/kernel.h>

#include <asm/types.h>
#include <linux/string.h>//memset
#include <linux/config.h>
#include <linux/module.h>
#include <linux/netfilter_ipv4.h>
#include <linux/ip.h>
#include <net/ip.h> //IPPROTO_TCP, IPPROTO_UDP
#include <linux/malloc.h>
#include <asm-i386/types.h>


static unsigned int hkpt(unsigned int hook, struct sk_buff 
**pskb, const struct net_device *indev, const struct net_device 
*outdev, int(*okfn)(struct sk_buff *))
{
  struct iphdr *ip = (*pskb)->nh.iph;
  unsigned char *data = NULL;
  int src_port = 0;
  int dst_port = 0;


  if (ip->protocol == IPPROTO_TCP || ip->protocol == 
IPPROTO_TCP) {
    struct tcphdr * tcph = (void *) ip + ip->ihl *4;
    int tcplen = (*pskb)->len - ip->ihl *4;
    data = (unsigned char *)tcph + (tcph->doff*4);
    src_port = ntohs(tcph->source);
    dst_port = ntohs(tcph->dest);

    if (tcplen != tcph->doff * 4)
      printk("TCP length not match\n");
    else
      printk("TCP length match..\n");


    printk("Source addr : %u.%u.%u.%u:%hu\n", 
NIPQUAD(ip->saddr), src_port);
    printk("Dest addr : %u.%u.%u.%u:%hu\n", NIPQUAD(ip->daddr), 
dst_port);
    printk("Length : %hu\n", ntohs(ip->tot_len));
    printk("Skb Data addr : %p\n", (*pskb)->data);
    printk("IP addr : %p\n", (*pskb)->nh.iph);
    printk("IP->ihl : %d\n", ip->ihl);
    printk("TCP addr : %p\n", tcph);
    printk("Data : %s\n", data);
    printk("data addr : %p\n", data);
    printk("tail : %p\n", (*pskb)->tail);
    printk("end : %p\n", (*pskb)->end);
  }

  return NF_ACCEPT;
}

static struct nf_hook_ops nf_ops = { {NULL, NULL}, hkpt, 
PF_INET, NF_IP_LOCAL_OUT, NF_IP_PRI_FILTER-1};
static struct nf_hook_ops nf_ops2 = { {NULL, NULL}, hkpt, 
PF_INET, NF_IP_LOCAL_IN, NF_IP_PRI_FILTER-1};

static int __init init(void) {

nf_register_hook(&nf_ops);
nf_register_hook(&nf_ops2);

 return 0;
}

static void __exit fini(void) {
nf_unregister_hook(&nf_ops);
nf_unregister_hook(&nf_ops2);

}

module_init(init);
module_exit(fini);



The result is :
Source addr : 161.40.211.86:6190
Dest addr : 161.40.211.86:1037
Length : 52
Skb Data addr : c7ca3d1c
IP addr : c7ca3d1c
IP->ihl : 5
TCP addr : c7ca3d30
Data :
data addr : c7ca3d50
tail : c7ca3d50
end : c7ca3d50


Could you correct me please.

Honestly, I'm a newbie in kernel programming.  And if I made 
any stupid mistakes in the above.  Please point me out or give 
some advices to me that I can look for reference.  Thanks
Thanks a lot.

your sincerely,
marco.




-
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
IRC Channel:   irc.openprojects.net / #kernelnewbies
Web Page:      http://www.kernelnewbies.org/