I have a number of services running on my server to which I want to restrict access to only those connecting through OpenVPN. I did manage to get routing to work to a second IP I added to the machine, but this is not ideal.
*.*.*.1
is the primary public IP. It is tied to the local IP 172.31.20.102
. *.*.*.2
is the secondary IP on the same machine that created for testing. It is tied to the local IP 172.31.20.103
. This is done in the Amazon EC2 interface, not on the server.
Note: The *s are just to mask the IP address here. The real configs has the actual public IPs.
Here are the relevant bits of the OpenVPN server config:
dev tun
server 172.16.128.32 255.255.255.240
#push "route *.*.*.1 255.255.255.255 net_gateway"
push "route *.*.*.2 255.255.255.255"
I have tried both with and without the push "route *.*.*.1 255.255.255.255 net_gateway"
line, which is why I have it commented in this sample. Note that routing does work to *.*.*.2
in the current configuration.
The server's IP on the VPN is 172.16.128.33
.
Here is the relevant line from sysctl.conf
:
net.ipv4.ip_forward = 1
Here are the relevant bits from iptables-save
:
*nat
-A PREROUTING -p tcp -m state --state NEW -m tcp --dport 22 -j DNAT --to-destination 172.16.128.33
-A POSTROUTING -s 172.16.128.32/28 -o eth0 -j MASQUERADE
COMMIT
*filter
-A FORWARD -i tun0 -s 172.16.128.32/28 -d *.*.*.1 -j ACCEPT
-A FORWARD -i tun0 -s 172.16.128.32/28 -d *.*.*.2 -j ACCEPT
-A FORWARD -i eth0 -o tun0 -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -s 172.16.128.32/28 -j ACCEPT
-A INPUT -s 172.31.20.100/28 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
-A PREROUTING -p tcp -m state --state NEW -m tcp --dport 22 -j DNAT --to-destination 172.16.128.33
allowed me to SSH into the alternate interface. Adding one of these lines for every port isn't ideal, but would be acceptable if there are no other options.
I want to be able to access port 22, for example, on *.*.*.1
. I would prefer that the connection originate from *.*.*.1
, rather than my public IP. I realize that I can't route ALL traffic through the tunnel, as the tunnel itself must be maintained, but I want everything else to go through the tunnel.
Let me know if I missed any useful information.