【问题标题】:How to reach struct sk_buff members?如何访问 struct sk_buff 成员?
【发布时间】:2013-06-22 17:52:01
【问题描述】:

我正在尝试将来自机器的所有数据包的源 IP 修改为我在此内核模块中指定的内容,但每次我尝试访问 nh.iph->saddr 时都会收到错误消息在 编译 的时候,说 Struct sk_buff 没有名为 nh 的成员
我在这里做错了什么? 我错过了一些标题吗??

#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

【问题讨论】:

    标签: linux-kernel kernel-module


    【解决方案1】:

    在您的内核版本中,struct sk_buff 已更改。它不再有那些成员。要访问 ip 标头,您应该尝试:

    #include <linux/ip.h>
    
    struct iphdr* iph = ip_hdr(skb);
    

    然后只需使用iph 变量来更改地址,例如:

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

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

    【讨论】:

    • 非常感谢 =D 已解决 ^^ 编辑:IP 校验和,好的...但是为什么传输数据包校验和也是如此?
    • 例如UDP校验和占所谓的ip pseudo header,这意味着UDP校验和使用IP头的一些字段,例如源地址和目的地址。我不太记得了,但我认为 TCP 也是这样做的。
    【解决方案2】:

    你可以在'include/linux/skbuff.h'中找到敲击sk_buff的定义。

    它没有 nh 字段,它解释了您看到的编译错误。它确实有一个“network_header”字段,这可能就是您要查找的内容。

    【讨论】:

      猜你喜欢
      • 2012-03-08
      • 1970-01-01
      • 1970-01-01
      • 2018-11-25
      • 2020-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多