2012-04-05 74 views
3

我试图修改从机器输出的所有数据包的源IP到我在这个内核模块中指定的东西,但每次我尝试访问nh.iph-> saddr我在编译时得到一个错误Struct sk_buff没有任何成员命名为nh
我在做什么错在这里? 我错过了一些标题或东西?如何到达struct sk_buff成员?

#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; 
} 

请注意,我运行Ubuntu 10.04 LTS 64位
内核2.6.32-33

回答

13

在你的内核版本struct sk_buff发生了变化。它不再拥有这些成员。访问IP报头,你应该尝试:

#include <linux/ip.h> 

struct iphdr* iph = ip_hdr(skb); 

然后,只需使用iph变量可以更改地址,是这样的:

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

另外,不要忘记,你可能需要重新计算ip和可能的传输数据包校验和。

+0

非常感谢= D解决^^ 编辑:IP校验,好吧... 但为什么传输数据包校验和? – Fingolfin 2012-04-05 12:36:59

+2

例如,UDP校验和占所谓的“ip伪头”,这意味着UDP校验和使用IP头的某些字段,例如源地址和目标地址。我不太记得,但我认为TCP也是这样做的。 – Fred 2012-04-05 13:00:36

-1

您可以在'include/linux/skbuff.h'中找到struck sk_buff的定义。

它没有nh字段,它解释了您所看到的编译错误。它有一个'network_header'字段,这可能是你要找的。

相关问题