更改目标ip

Change destination ip

我正在尝试创建一个在特定条件下转发数据包的内核模块。现在我正在尝试做一个硬代码测试来转发在一个接口中收到的数据包并将其转发到另一个接口。在这个测试中,我从 eth0 上的 192.168.56.101 收到一个数据包,我想在 eht1 上转发这个数据包到 192.168.57.1​​03。在 eth0 中,我的 ip 是 192.168.56.102,在 eth1 中,我的 ip 是 192.168.57.1​​02。我使用的传输协议是一个实验协议 (253)。以下代码只是我代码的简化部分:

#define XOR_PROTOCOL 253

static unsigned int xor_pre_routing_hook(const struct nf_hook_ops *ops, struct sk_buff *skb, const struct net_device *in, const struct net_device *out, int (*okfn)(struct sk_buff *))
{
    struct iphdr *iph;
    struct xorhdr *ptr;
    char sip[15];
    char sip2[15];

    iph = ip_hdr(skb);

    sprintf(sip, "%pI4", &iph->saddr);
    sprintf(sip2, "%pI4", &iph->daddr);

    // Check if is XOR protocol
    if (iph->protocol == XOR_PROTOCOL) {
        DEBUG("(Ogirinal) From %pI4 to %pI4.\n", &iph->saddr, &iph->daddr);

        if (strcmp(sip, "192.168.56.101") == 0 && strcmp(sip2, "192.168.56.255") == 0) {
           //iph->saddr = inet_addr("192.168.57.102");
           iph->daddr = inet_addr("192.168.57.103"); 
           DEBUG("(Modified) From %pI4 to %pI4.\n", &iph->saddr, &iph->daddr);
           iph = ip_hdr(skb);
           iph->check = 0;
           ip_send_check (iph);
           return NF_ACCEPT;
        }
    }
accept:
    return NF_ACCEPT;
}

这个钩子在NF_INET_PRE_ROUTING。我还有一个钩子,可以在 NF_INET_FORWARD 中打印源和目标 ip,但是没有数据包通过这个钩子。

我在 virtual box 上使用 3 个 linux 虚拟机进行测试,我在每个虚拟机中启用了转发选项。在这种情况下是否可以转发数据包?我做错了什么,我该怎么做才能解决这个问题?

问题是广播IP 192.168.56.255,IP 192.168.56.102 数据包被转发。