The following picture shows the topology of my network.
I now want to route all http traffic from 10.8.0.15 via 10.8.0.42 to the outside world, using wlan0. Unfortunately, I am a noob when it comes to iptables and routing. Can anyone point me into the right direction?
Edit: So far I executed the following commands on 10.8.0.15
sudo ip rule add fwmark 2 table 3
sudo ip route add default via 10.8.0.42 table 3
sudo ip route flush cache
sudo iptables -t mangle -A OUTPUT -p tcp --dport 80 -j MARK --set-mark 2
sudo iptables -t nat -A POSTROUTING -o eth0 -j SNAT --to-source 10.8.0.15
sudo sysctl -w net.ipv4.conf.eth0.rp_filter=2
sudo iptables -t nat -A OUTPUT -p tcp --dport 80 -j DNAT --to-destination 10.8.0.42
and the following on 10.8.0.42
sudo iptables -I FORWARD -i eth0 -o wlan0 -s 10.8.0.0/24 -m conntrack --ctstate NEW -j ACCEPT
sudo iptables -I FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
sudo sysctl -w net.ipv4.ip_forward=1
when I now want to test the connection on 10.8.0.15 I get the following
curl http://freegeoip.net/json/
curl: (7) Failed to connect to freegeoip.net port 80: Connection refused
and a tcpdump on 10.8.0.42 shows
sudo tcpdump -n -i eth0 -s 0 src or dst port 80
10:05:18.552329 IP 10.8.0.15.36186 > 10.8.0.42.80: Flags [S], seq 460236380, win 29200, options [mss 1308,sackOK,TS val 22927 ecr 0,nop,wscale 6], length 0
10:05:18.552612 IP 10.8.0.42.80 > 10.8.0.15.36186: Flags [R.], seq 0, ack 460236381, win 0, length 0
10:05:19.068397 IP 10.8.0.15.50052 > 10.8.0.42.80: Flags [S], seq 2824330189, win 29200, options [mss 1308,sackOK,TS val 22969 ecr 0,nop,wscale 6], length 0
10:05:19.068674 IP 10.8.0.42.80 > 10.8.0.15.50052: Flags [R.], seq 0, ack 2824330190, win 0, length 0
The purpose of this setup is to use an arbitrary client within the network as internet gateway. While in the diagram there is only 10.8.0.42, there are potentially multiple such clients each with their own dedicated internet access which might be used by other clients.
Update: I now tested the following configuration as suggested below.
box0:
sudo ip route add default via 10.8.0.42 dev tun0 table 3
sudo ip rule add fwmark 2 table 3
sudo ip route flush cache
sudo iptables -A OUTPUT -t mangle -p tcp --dport 80 -j MARK --set-mark 2
sudo sysctl -w net.ipv4.conf.eth0.rp_filter=0
sudo iptables -A POSTROUTING -t nat -o eth0 -p tcp --dport 80 -j SNAT --to-source 10.8.0.15
sudo iptables -A POSTROUTING -t nat -o eth0 -p tcp --dport 443 -j SNAT --to-source 10.8.0.15
box1:
sudo sysctl -w net.ipv4.ip_forward=1
sudo iptables -A POSTROUTING -t nat -s 10.8.0.0/24 -p tcp --dport 80 -j SNAT --to-source 192.168.0.2
sudo iptables -A POSTROUTING -t nat -s 10.8.0.0/24 -p tcp --dport 443 -j SNAT --to-source 192.168.0.2
The tcpdump output on box1 remains the same:
12:24:05.788127 IP 10.8.0.15.45844 > 10.8.0.42.80: Flags [S], seq 4169666416, win 29200, options [mss 1308,sackOK,TS val 15420789 ecr 0,nop,wscale 6], length 0
12:24:05.788414 IP 10.8.0.42.80 > 10.8.0.15.45844: Flags [R.], seq 0, ack 4169666417, win 0, length 0
12:24:06.786406 IP 10.8.0.15.45844 > 10.8.0.42.80: Flags [S], seq 4169666416, win 29200, options [mss 1308,sackOK,TS val 15420889 ecr 0,nop,wscale 6], length 0
12:24:06.786694 IP 10.8.0.42.80 > 10.8.0.15.45844: Flags [R.], seq 0, ack 1, win 0, length 0
12:24:08.789821 IP 10.8.0.15.45844 > 10.8.0.42.80: Flags [S], seq 4169666416, win 29200, options [mss 1308,sackOK,TS val 15421089 ecr 0,nop,wscale 6], length 0
12:24:08.790111 IP 10.8.0.42.80 > 10.8.0.15.45844: Flags [R.], seq 0, ack 1, win 0, length 0
The tcpdump output on box0:
11:26:14.220391 IP 10.8.0.15.33420 > 158.69.242.138.80: Flags [S], seq 2659446178, win 29200, options [mss 1460,sackOK,TS val 15433635 ecr 0,nop,wscale 6], length 0
11:26:14.308915 IP 10.8.0.42.80 > 10.8.0.15.33420: Flags [R.], seq 0, ack 2659446179, win 0, length 0
11:26:15.211761 IP 10.8.0.15.33420 > 158.69.242.138.80: Flags [S], seq 2659446178, win 29200, options [mss 1460,sackOK,TS val 15433735 ecr 0,nop,wscale 6], length 0
11:26:15.337652 IP 10.8.0.42.80 > 10.8.0.15.33420: Flags [R.], seq 0, ack 1, win 0, length 0
I guess the main issue is that the destination for the tcp packets is 10.8.0.42 when it arrives at box1 instead of whatever the IP of the URL (here: 158.69.242.138) of the http request is. But I cannot see where this destination is altered.