gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 22:23:40 26 4
gpt4 key购买 nike

我正在尝试将从机器传出的所有数据包的源 IP 修改为我在此内核模块中指定的内容,但每次我尝试访问 nh.iph->saddr 我在编译时收到一条错误消息 结构 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 header ,您应该尝试:

#include <linux/ip.h>

struct iphdr* iph = ip_hdr(skb);

然后只需使用 iph变量来更改地址,例如:
iph->saddr = ....
iph->daddr = ....

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

关于linux-kernel - 如何访问 struct sk_buff 成员?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10025026/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com