gpt4 book ai didi

tcp - netfilter 传入 Hook : struct tcphdr -> dest pointer does not point to the correct location?

转载 作者:可可西里 更新时间:2023-11-01 02:41:22 25 4
gpt4 key购买 nike

我们正在尝试为 linux 内核实现一个 NAT 模块。面临的问题是,对于传入 Hook 处的 TCP 数据包,预期指向目标端口的指针并没有这样做。传出钩子(Hook)的片段:

unsigned int incoming_hook(unsigned int hooknum,
struct sk_buff *skb,
const struct net_device *in,
const struct net_device *out,
int (*okfn)(struct sk_buff*))
{
struct iphdr *iph;
struct tcphdr *tcph;
unsigned int dst_addr;
unsigned short dst_port;
unsigned short *dpptr;

printk(KERN_ALERT "****Incoming hook starts****\n");

if (!skb)return NF_ACCEPT;

iph = ip_hdr(skb);
if (!iph)return NF_ACCEPT;

dst_addr = ntohl(iph->daddr); // works correct

printk(KERN_ALERT " dst addr is %d \n", dst_addr);


if (iph->protocol==IPPROTO_TCP)
{
tcph = tcp_hdr(skb);
if (!tcph)return NF_ACCEPT;

dst_port = ntohs(tcph->dest); // gives incorrect result (unexpected.)
// dpptr = (unsigned short *)((char *)iph + 22); // just a try. does not work either

printk(KERN_ALERT " dst port is %d \n", *dpptr);
dst_port = ntohs(*dpptr);

// continues with the NAT code

然而,同样的事情适用于更改源地址和源端口的传出钩子(Hook)处的数据包。以下适用于传出数据包

tcph = tcp_hdr(skb);
if (!tcph)return NF_ACCEPT;

src_port = ntohs(tcph->source);

传入数据包的 IP header 大小是否有问题,或者 struct tcphdr 有错误或 tcphdr->dest 指针有问题?还是其他地方可能出了问题?

需要帮助。谢谢。

最佳答案

我遇到了同样的问题,感谢一些同学和the class' teacher.解决了

对于通过 netfilter 的 incoming 钩子(Hook)到达的数据包,您应该使用 +20(目前您使用的是 +22)。

示例代码:

skb_linearize(skb);
struct tcphdr* tcp_header;
tcp_header = (struct tcphdr *)(skb_transport_header(skb)+20);

关于tcp - netfilter 传入 Hook : struct tcphdr -> dest pointer does not point to the correct location?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13556399/

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