加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

linux-kernel – 如何访问struct sk_buff成员?

发布时间:2020-12-14 01:29:54 所属栏目:Linux 来源:网络整理
导读:我试图将从机器输出的所有数据包的源IP修改为我在此内核模块中指定的内容,但每次我尝试访问nh.iph- saddr时我在编译时遇到错误,说明struct sk_buff没有成员名为nh 我在这做错了什么? 我错过了一些标题还是什么? #include linux/module.h #include linux/ke
我试图将从机器输出的所有数据包的源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

解决方法

在你的内核版本中,struct sk_buff已经改变了.它不再拥有这些成员.要访问ip标头,您应该尝试:

#include <linux/ip.h>

struct iphdr* iph = ip_hdr(skb);

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

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读