bojan
By:
bojan

Nat gateway on digital ocean's droplet possible ?

May 19, 2014 10.4k views
Digital ocean introduced private network feature in newyork 2 region. I've created 2 droplets (gateway and gwclient both ubuntu 13.10 x64) with private network feature enabled. Droplet "Gateway" should act as a NAT Gateway router. Another droplet "Gwclient" should use "gateway" droplet to communicate with the outside world. My idea was to use iptables to accomplish this scenario. First of all i've disabled firewalls on both droplets On gateway droplet ip forwarding is enabled.
root@gateway:/# sysctl -p 
net.ipv4.ip_forward = 1
next i applied iptables rules like this:
iptables --table nat -A POSTROUTING -o eth0 -j MASQUERADE 
iptables -A FORWARD --in-interface eth1 -j ACCEPT
Relevant informations for the gateway droplet
root@gateway:/etc/ufw# ifconfig 
eth0 addr:107.170.8.148 Bcast:107.170.8.255 Mask:255.255.255.0 
eth1 addr:10.128.169.173 Bcast:10.128.255.255 Mask:255.255.0.0 
lo inet addr:127.0.0.1 Mask:255.0.0.0

root@gateway:/# route 
Kernel IP routing table 
Destination Gateway Genmask Flags Metric Ref Use Iface 
default 107.170.8.1 0.0.0.0 UG 0 0 0 eth0 
10.128.0.0 * 255.255.0.0 U 0 0 0 eth1 
107.170.8.0 * 255.255.255.0 U 0 0 0 eth0

root@gateway:/# iptables-save 
# Generated by iptables-save v1.4.18 on Sun May 18 16:03:33 2014 
*mangle 
:PREROUTING ACCEPT [478:36001] 
:INPUT ACCEPT [296:23936] 
:FORWARD ACCEPT [173:11525] 
:OUTPUT ACCEPT [305:45020] 
:POSTROUTING ACCEPT [478:56545] 
COMMIT 
# Completed on Sun May 18 16:03:33 2014 
# Generated by iptables-save v1.4.18 on Sun May 18 16:03:33 2014 
*filter 
:INPUT ACCEPT [214:17088] 
:FORWARD ACCEPT [40:3324] 
:OUTPUT ACCEPT [248:36052] 
-A FORWARD -i eth1 -j ACCEPT 
COMMIT 
# Completed on Sun May 18 16:03:33 2014 
# Generated by iptables-save v1.4.18 on Sun May 18 16:03:33 2014 
*nat 
:PREROUTING ACCEPT [41:2538] 
:INPUT ACCEPT [0:0] 
:OUTPUT ACCEPT [0:0] 
:POSTROUTING ACCEPT [0:0] 
-A POSTROUTING -o eth0 -j MASQUERADE 
COMMIT 
# Completed on Sun May 18 16:03:33 2014
On a gwclient droplet i disabled eth0 interface and left only private network interface. Then i added static route for eth1 interface like this ip route add default via 10.128.169.17 dev eth1 Here is the relevant output.
eth1 addr:10.128.169.172 Bcast:10.128.255.255 Mask:255.255.0.0 
lo addr:127.0.0.1 Mask:255.0.0.0

root@gwclient:/etc# ip route 
default via 10.128.169.173 dev eth1 
10.128.0.0/16 dev eth1 proto kernel scope link src 10.128.169.172
Traceroute output from gwclient droplet
root@gwclient:/etc# traceroute 212.62.32.1 
traceroute to 212.62.32.1 (212.62.32.1), 30 hops max, 60 byte packets 
1 10.128.169.173 (10.128.169.173) 0.611 ms 0.609 ms 0.622 ms 
2 * * * 
3 * * * 
From traceroute outoput its obvious that that gateway on 10.128.169.173 (eth1 interface on gateway droplet) is setup correctly. And finally here are tcpdump outputs from gateway droplet on both interfaces
eth0 
16:22:24.088123 IP 107.170.8.148 > ns1.ptt.rs: ICMP echo request, id 2099, seq 210, length 64 
16:22:23.202942 IP ns1.ptt.rs > 107.170.8.148: ICMP echo reply, id 2099, seq 209, length 64

eth1 
16:23:26.584029 IP gwclient > ns1.ptt.rs: ICMP echo request, id 2099, seq 272, length 64 
16:23:26.707862 IP ns1.ptt.rs > gwclient: ICMP echo reply, id 2099, seq 272, length 64
Everything seems good except i never got reply to gwclinet droplet. tcpdump on eth1 (gwclient) remains empty .Packets never arrive . Is it possible at all to setup a NAT gateway on digital ocean droplet. Is it prohibited / blocked or i am doing something wrong. The procedure for building a nat router , i've described above, is a pretty standard . Best regards
4 Answers
Hi there -- Unfortunately, this configuration will not work due to the anti-spoofing ruleset we have in place to protect your droplets. You're correct that everything works on the way out, but where this gets caught is in the return path from the gateway droplet to your private IP only droplet. Since it the gateway box is attempting to send a packet that has a different source address from itself, it will not be allowed through. Please let us know if you have further questions.
You can use a proxy or nat setup on your gateway droplet to achieve this. (traffic leaving gateway droplet natting to the interface ip) The trick is the source address must always stay the droplet assigned ip. As Neal mentioned, it's a security thing and we have to do it.

@bryan I have setup a nat between eth0 and eth1 but I'm still hitting the same problem. Do you have a link or an example of the nat or proxy you are talking about in your comment.

I don't think we do but passed it onto our tutorials group so they can look into getting something.

They did suggest https://www.digitalocean.com/community/tutorials/how-to-install-tinc-and-set-up-a-basic-vpn-on-ubuntu-14-04 as an alternative that might work better for your situation.

by Mitchell Anicas
In this tutorial, we will go over how to use Tinc, an open source Virtual Private Network (VPN) daemon, to create a secure VPN that your servers can communicate on as if they were on a local network. We will also demonstrate how to use Tinc to set up a secure tunnel into a private network. We will be using Ubuntu 14.04 servers, but the configurations can be adapted for use with any other OS.
Have another answer? Share your knowledge.