28

I am doing a network experiment about ip packet forwarding, but I don't know why it does work.

I have a linux machine with two network interfaces, eth0 and eth1 both with static IP address (eth0: 192.168.100.1, eth1: 192.168.101.2).

My goal is simple, I just want to forward ip packets from eth1 with destination in subnet 192.168.100.0/24 to eth0, and forward ip packets from eth0 with destination in subnet 192.168.101.0/24 to eth1.

I turned on ip forwarding with:

sysctl -w net.ipv4.ip_forward=1

my routing table is like this:

# route -n
Kernel IP routing table
Destination     Gateway     Genmask        Flags Metric Ref   Use  Iface
192.168.100.0   0.0.0.0     255.255.255.0  U     0      0       0  eth0
192.168.101.0   0.0.0.0     255.255.255.0  U     0      0       0  eth1

But, when I try to ping from 192.168.100.25 to 192.168.101.47, it does not work.

2 Answers 2

26

You need to add a forwarding rule using iptables command, something like this:

modprobe iptable_nat
echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
iptables -A FORWARD -i eth1 -j ACCEPT

see man iptables for more details, or search internet for howto articles, for example How to set up a NAT router on a Linux-based computer

Here is Linux IP Masquerade HOWTO which discusses the topic in details.

You should also ensure that you have no other rules (e.g. in the FORWARD chain) that are overriding the above ACCEPT rule. If there are, you probably want to delete them.

6
  • I don't know why, but it still don't work. I have added both iptables -A FORWARD -i eth0 -o eth1 -j ACCEPT and iptables -A FORWARD -i eth1 -o eth0 -j ACCEPT to my tables. And my INPUT, OUTPUT, and FORWARD policy are all ACCEPT.
    – Yishu Fang
    Dec 10, 2012 at 4:47
  • 1
    There are many things which can be configured wrong. For example, you host's IP should be specified as "gateway IP" on the hosts in the "internal" network, so they know that if an IP is not in the range of their "local" network the packets need to be sent to your gateway machine. This is similar to the usual setup where ADSL router is registered as gateway for hosts in the LAN
    – Sergey
    Dec 10, 2012 at 9:29
  • When I use traceroute on my host, it shows that the packet goes to the "router" (192.168.100.1), but it don't go any further.
    – Yishu Fang
    Dec 10, 2012 at 10:56
  • 1
    @UniMouS: Seems that I've forgotten about MASQUERADE thing - I've edited my answer and added new links to articles.
    – Sergey
    Dec 10, 2012 at 20:52
  • @Sergey 10 years later and that comment is very true. For me the trouble was not in the device with forwarding, but in the client device. It was the dhcp client service assigning a link local address at some point (even though the interface has a static ip configuration) which in turn messed up the routes somehow. Disabling the dhcp service solved it.
    – eglasius
    Jun 28, 2022 at 15:12
6

You need to add a route to both 192.168.100.25 and 192.168.101.47.

If your forwarding server has IPs 192.168.100.1 and 192.168.101.1 you would add in client 192.168.100.25

ip route 192.168.101.0/24 via 192.1268.100.1

and in client 192.168.101.47

ip route 192.168.100.0/24 via 192.168.101.1

(This works with just forwarding enabled, no iptables).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .