Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

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.

share|improve this answer
    
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. –  UniMouS Dec 10 '12 at 4:47
    
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 '12 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. –  UniMouS Dec 10 '12 at 10:56
    
@UniMouS: Seems that I've forgotten about MASQUERADE thing - I've edited my answer and added new links to articles. –  Sergey Dec 10 '12 at 20:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.