Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 618
Fingolfin Avatar asked Dec 16 '22 01:12

Fingolfin


1 Answers

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.

like image 175
Fred Avatar answered Dec 26 '22 12:12

Fred