Fingolfin
Fingolfin

Reputation: 5533

How to reach struct sk_buff members?

I am trying to modify the source IP of all packets outcoming from the machine to something I specify in this Kernel Module, but everytime I try to access nh.iph->saddr I get an error in compile time that says Struct sk_buff has no member named nh
What am I doing wrong here? Have I missed some header or something??

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

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

#include <linux/skbuff.h>
#include <linux/ip.h>                  /* For IP header */

#include <linux/inet.h> /* For in_aton(); htonl(); and other related Network utility functions */ 


static struct nf_hook_ops nfho;


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 in_addr masterIP;

    masterIP.s_addr = htonl (in_aton("192.168.1.10")); 
    sb->nh.iph->saddr = masterIP.s_addr;
    return NF_ACCEPT;
}

Note that I am running Ubuntu 10.04 LTS 64 bits
Kernel 2.6.32-33

Upvotes: 4

Views: 6230

Answers (2)

Kristof Provost
Kristof Provost

Reputation: 26322

You can find the definition of struck sk_buff in 'include/linux/skbuff.h'.

It does not have an nh field, which explains the compilation errors you're seeing. It does have a 'network_header' field, which is probably what you're looking for.

Upvotes: -1

Fred
Fred

Reputation: 17085

In your kernel version the struct sk_buff has changed. It no longer has those members. To access the ip header you should try:

#include <linux/ip.h>

struct iphdr* iph = ip_hdr(skb);

Then just use the iph variable to change the addresses, something like:

iph->saddr = ....
iph->daddr = ....

Also, don't forget that you might need to recalculate ip and possible transport packets checksums.

Upvotes: 13

Related Questions