当前位置: 动力学知识库 > 问答 > 编程问答 >

c - Change destination ip

问题描述:

I'm trying to create a kernel module that forward packets in certain conditions. Now I'm trying to do just a hard code test to forward a packet received in an interface and forward it to another interface. In this test I'm receiving a packet from 192.168.56.101 on eth0 and I want to forward this packet on eht1 for 192.168.57.103. In eth0 my ip is 192.168.56.102 and in eth1 my ip is 192.168.57.102. The transport protocol I'm using is a experimental protocol (253). The following code is just a simplified part of my code:

#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;

}

This hook in NF_INET_PRE_ROUTING. I also have a hook to just print source and destination ip in NF_INET_FORWARD, but there is no packet passing through this hook.

I'm testing with 3 linux virtual machine on virtual box, and I enabled the forward option in each vm. Is possible to forward packets in this scenario? What I'm doing wrong and what can I do to solve this problem?

网友答案:

The problem is the broadcast ip 192.168.56.255, with the ip 192.168.56.102 the packets were forwarded.

分享给朋友:
您可能感兴趣的文章:
随机阅读: