Port Forwarding through OpenVPN

Post new topic   Reply to topic    DD-WRT Forum Index -> Advanced Networking
Author Message
JonArmy
DD-WRT Novice


Joined: 24 Dec 2015
Posts: 21

PostPosted: Sat Dec 26, 2015 23:33    Post subject: Port Forwarding through OpenVPN Reply with quote
First thanks to everyone that has helped out while I got my OpenVPN up and running.

Before we lost our connection we had one computer that has about 5 ports forwarded due to a program we run on the computer.

I've looked through Google and other forms and might of missed it in this form. By what I've read you loose the port forwarding rules, however the computer I have running on the server OpenVPN has ports forward and they work.

Currently in the router that is the server under Administration > Commands I have only rules for Firewall and the only script that's in there is for the OpenVPN. On the router that runs as a client I have are:

iptables -t net -A POSTROUTING -j MASQUERADE

Which is under the firewall command.

On the router that is running as client we still have the IP address and ports that we have forward, of course there using the IP address that the client router has for them.

Is there a script to place either on the client or server router that will allow the ports that we want forward to be forwarded.
Sponsor
JonArmy
DD-WRT Novice


Joined: 24 Dec 2015
Posts: 21

PostPosted: Sun Dec 27, 2015 1:19    Post subject: Reply with quote
Yes from the client side is were the computer(s) that has ports that need to be forwarded, currently we have on the client side router under NAT/QoS > Port Forwarding we have the following:

Application Protocol Source Net Port from IP Address Port to

Telnet Both 23 192.168.1.100 23

BPQ AXIP 10093 192.168.1.100 10093

BPQ Telnet 8011 192.168.1.100 8011

BPQ HTTP 81 192.168.1.100 81

BPQ VNC 5910 192.168.1.100 5910

BPQ RDP 3389 192.168.1.100 3389

As you can see we have the application name, port from, IP address on client side and port to

The way were using OpenVPN is the network were using at the client end blocks all the ports we use and they don't fell like working with use to open those ports for us, so with the OpenVPN the OpenVPN server is providing the internet access and ability to run our programs that we need the ports.

So the main thing is that we'd need the ability to have the ports opened up so that people can use the ports even with using a OpenVPN server/client
JonArmy
DD-WRT Novice


Joined: 24 Dec 2015
Posts: 21

PostPosted: Sun Dec 27, 2015 2:15    Post subject: Reply with quote
Yes that's what were looking for with the port forwarding.

We had tried a PPTP client, however they had they had blocked so that's why we had to go to a OpenVPN.

I can copy what I have set for both the server and client for adjusting to a tunnel which currently we have the server mode as TUN
JonArmy
DD-WRT Novice


Joined: 24 Dec 2015
Posts: 21

PostPosted: Sun Dec 27, 2015 18:44    Post subject: Reply with quote
eibgrad wrote:
On the OpenVPN server side, add the following to your startup script, replacing <common-name> w/ the common-name you specified when building your client keys w/ easy-rsa.

Code:
mkdir -p /tmp/openvpn/ccd
echo “iroute 192.168.1.0 255.255.255.0” > /tmp/openvpn/ccd/<common-name>


So I'll take the common name from the client key and place it into <common-name> which looking at the key for the client I need the CN which is under Details > Issuer. If that's correct. So with this code all my code is under Firewall so with this code it will still go under startup?

NOTE: Based on your previous post, I’m assuming the network behind the OpenVPN client is 192.168.1.0/24.

Currently the setup is the OpenVPN server router IP is 192.168.0.1 the VPN is 10.0.8.0 and I'm pushing under additional commands 192.168.3.0 the router on the client is pushing IP's of 192.168.1.1 so under firewall commands I'm guessing I'll place 192.168.1.1

Here’s a replacement set of firewall rules for the OpenVPN server that supports site-to-site and port forwarding. Here too, I’m assuming the same config from your other thread.

Code:
#!/bin/sh
OVPN_SERVER="10.0.8.0/24"
OVPN_DEV="tun2"
OVPN_PROTO="tcp"
OVPN_PORT="443"

WAN_IF="$(ip route | awk '/^default/{print $NF}')"

# open the OpenVPN server port
iptables -I INPUT -i $WAN_IF -p $OVPN_PROTO --dport $OVPN_PORT -j ACCEPT

# allow OpenVPN clients to access the OpenVPN server
iptables -I INPUT -i $OVPN_DEV -m state --state NEW -j ACCEPT

# allow OpenVPN clients to access ALL other devices on the LAN
iptables -I FORWARD -i $OVPN_DEV -m state --state NEW -j ACCEPT

# allow OpenVPN clients to access ALL other devices on the LAN
iptables -I FORWARD -i $OVPN_DEV -m state --state NEW -j ACCEPT

# allow OpenVPN clients to use the OpenVPN server as an internet gateway
iptables -t nat -A POSTROUTING -s $OVPN_SERVER -o $WAN_IF -j MASQUERADE

# what follows is only for site-to-site and port forwarding over the the tunnel

LAN_IP="$(nvram get lan_ipaddr)"
LAN_NET="$LAN_IP/$(nvram get lan_netmask)"
REMOTE_NET="192.168.1.0/24"

# allow local devices to become clients of the remote network
iptables -I FORWARD -o $OVPN_DEV -m state --state NEW -j ACCEPT

# NAT anything that's NOT the local network (e.g., WAN) over the OpenVPN tunnel
iptables -t nat -A POSTROUTING -s ! $LAN_NET -o $OVPN_DEV -j MASQUERADE

# NAT the remote network (behind the OpenVPN client) over the WAN
iptables -t nat -A POSTROUTING -s $REMOTE_NET -o $WAN_IF -j MASQUERADE


First verify that clients on the OpenVPN server’s local network can ping devices behind the OpenVPN client. Until that’s working, port forwarding will not work either. Then it should be a simple matter to specify the 192.168.1.x network for your port forwards on the OpenVPN server side and have them routed to the OpenVPN client’s network. Because we’ve NAT’d those packets, the replies should be forwarded back over the VPN as well.

Realize I’ve doing this off the top of my head, so it could still require some adjustments. And if it does work, there’s one other change I’d recommend, but I won’t discuss right now.
JonArmy
DD-WRT Novice


Joined: 24 Dec 2015
Posts: 21

PostPosted: Sun Dec 27, 2015 18:52    Post subject: Reply with quote
Also if this works, will this allow us to forward any ports on the client side and it will still run normal internet and also forward those ports even though using a OpenVPN.
JonArmy
DD-WRT Novice


Joined: 24 Dec 2015
Posts: 21

PostPosted: Mon Dec 28, 2015 3:14    Post subject: Reply with quote
Currently our network is as follows:

Router running OpenVPN Server is using Router IP address of 192.168.0.1 under Setup > Basic Settings. Then under Services > VPN it has set up Network of 10.0.8.0 so under additional config should be something like 192.168.1.0 to push the client network of 192.168.1.0.

On the client the router is set up under Setup > Basic Settings of 192.168.1.1

I've copied all the configuration's from client & server and need to confirm I have everything setup correctly.

startup

mkdir -p /tmp/openvpn/ccd
echo "iroute 192.168.1.0 255.255.255.0"> /tmp/openvpn/ccd/Jon KK4ZIZ (common name on certificate)

Firewall

#!/bin/sh
OVPN_SERVER="10.0.8.0/24"
OVPN_DEV="tun2"
OVPN_PROTO="tcp"
OVPN_PORT="443"

WAN_IF="$(ip route | awk '/^default/{print $NF}')"

# open the OpenVPN server port
iptables -I INPUT -i $WAN_IF -p $OVPN_PROTO --dport $OVPN_PORT -j ACCEPT

# allow OpenVPN clients to access the OpenVPN server
iptables -I INPUT -i $OVPN_DEV -m state --state NEW -j ACCEPT

# allow OpenVPN clients to access ALL other devices on the LAN
iptables -I FORWARD -i $OVPN_DEV -m state --state NEW -j ACCEPT

# allow OpenVPN clients to access ALL other devices on the LAN
iptables -I FORWARD -i $OVPN_DEV -m state --state NEW -j ACCEPT

# allow OpenVPN clients to use the OpenVPN server as an internet gateway
iptables -t nat -A POSTROUTING -s $OVPN_SERVER -o $WAN_IF -j MASQUERADE

# what follows is only for site-to-site and port forwarding over the the tunnel

LAN_IP="$(nvram get lan_ipaddr)"
LAN_NET="$LAN_IP/$(nvram get lan_netmask)"
REMOTE_NET="192.168.1.0/24"

# allow local devices to become clients of the remote network
iptables -I FORWARD -o $OVPN_DEV -m state --state NEW -j ACCEPT

# NAT anything that's NOT the local network (e.g., WAN) over the OpenVPN tunnel
iptables -t nat -A POSTROUTING -s ! $LAN_NET -o $OVPN_DEV -j MASQUERADE

# NAT the remote network (behind the OpenVPN client) over the WAN
iptables -t nat -A POSTROUTING -s $REMOTE_NET -o $WAN_IF -j MASQUERADE



Additional Config

push "route 192.168.0.1 255.255.255.0"
push "dhcp-option DNS 8.8.8.8"
push "redirect-gateway def1"


Client

Firewall

iptables -t net -A POSTROUTING -j MASQUERADE
JonArmy
DD-WRT Novice


Joined: 24 Dec 2015
Posts: 21

PostPosted: Mon Dec 28, 2015 3:27    Post subject: Reply with quote
eibgrad wrote:
Just noticed that I had left out one instruction.

You must add a route to the OpenVPN client's network in the OpenVPN server’s Additional Config field.

So under the OpenVPN server additional config field it will show up as:

Additional Config

push "route 192.168.0.1 255.255.255.0"
push "dhcp-option DNS 8.8.8.8"
push "redirect-gateway def1"
route 192.168.1.0 255.255.255.0

Code:
route 192.168.1.0 255.255.255.0


Notice this is a "route" command, whereas in the CCD file it's a "iroute" command.

The “route” command actually creates the route in the OpenVPN server’s routing table. Because the OpenVPN server could be connected to multiple OpenVPN clients with different common-names, the “iroute” serves the purpose of telling the OpenVPN server for which OpenVPN client(s) this route applies.
JonArmy
DD-WRT Novice


Joined: 24 Dec 2015
Posts: 21

PostPosted: Mon Dec 28, 2015 21:00    Post subject: Reply with quote
Well just updated the server, and after a restart it is talking, I remote into one of the computers on the other network via TeamViewer and I can ping the server router at 192.168.0.1 and get response. If I go to the server connection and ping something on the client side it doesn't I get a time out.

How can I tell it's working correctly, should I be able to ping a computer on the client side, even if the on the client side under Security > Firewall it's enabled and it blocks WAN Requests at Block Anonymous WAN Request (ping), Filter Multicast, Filter IDENT (Port 113), Block WAN SNMP access.

Should I try turning off the Firewall on the client?
JonArmy
DD-WRT Novice


Joined: 24 Dec 2015
Posts: 21

PostPosted: Mon Dec 28, 2015 23:48    Post subject: Reply with quote
eibgrad wrote:
Let's make sure everything is configured properly first.

I noticed the following from an earlier post.

Code:
mkdir -p /tmp/openvpn/ccd
echo "iroute 192.168.1.0 255.255.255.0"> /tmp/openvpn/ccd/Jon KK4ZIZ


Not a good idea to use embedded spaces w/ Linux. They’re nothing but a headache. At the very least you need to surround such arguments w/ quotes.

Code:
mkdir -p /tmp/openvpn/ccd
echo "iroute 192.168.1.0 255.255.255.0"> "/tmp/openvpn/ccd/Jon KK4ZIZ"


On the OpenVPN server side, go to a telnet/ssh session and post the results of the following commands.

Code:
cat /tmp/openvpn/openvpn.conf
[color=red]
root@DD-WRT:~# cat /tmp/openvpn/openvpn.conf
dh /tmp/openvpn/dh.pem
ca /tmp/openvpn/ca.crt
cert /tmp/openvpn/cert.pem
key /tmp/openvpn/key.pem
keepalive 10 120
verb 3
mute 3
syslog
writepid /var/run/openvpnd.pid
management 127.0.0.1 14
management-log-cache 100
topology subnet
script-security 2
port 443
proto tcp-server
cipher bf-cbc
auth sha1
client-config-dir /tmp/openvpn/ccd
comp-lzo adaptive
tls-server
ifconfig-pool-persist /tmp/openvpn/ip-pool 86400
client-to-client
tcp-nodelay
tun-mtu 1500
mtu-disc yes
server 10.0.8.0 255.255.255.0
dev tun2
tun-ipv6
push "route 192.168.0.0 255.255.255.0"
push "dhcp-option DNS 8.8.8.8"
push "redirect-gateway def1"
route 192.168.1.0 255.255.255.0
[/color]
cat "/tmp/openvpn/ccd/Jon KK4ZIZ" # probably returns nothing
[color=red] iroute 192.168.1.0 255.255.255.0 [/color]

echo "jffs_mounted=$(nvram get jffs_mounted)"
[color=red] jffs_mounted= [/color]

route -n
[color=red]
root@DD-WRT:~# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.0.0     0.0.0.0         255.255.255.0   U     0      0        0 br0
10.0.8.0        0.0.0.0         255.255.255.0   U     0      0        0 tun2
172.73.0.0      0.0.0.0         255.255.224.0   U     0      0        0 vlan2
169.254.0.0     0.0.0.0         255.255.0.0     U     0      0        0 br0
127.0.0.0       0.0.0.0         255.0.0.0       U     0      0        0 lo
0.0.0.0         172.73.0.1      0.0.0.0         UG    0      0        0 vlan2 [/color]

JonArmy
DD-WRT Novice


Joined: 24 Dec 2015
Posts: 21

PostPosted: Tue Dec 29, 2015 0:14    Post subject: Reply with quote
So for the "redirect-gateway def1" if that is taken out will it make any difference? I also might of had just restarted since the previous post about the

mkdir -p /tmp/openvpn/ccd
echo "iroute 192.168.1.0 255.255.255.0"> /tmp/openvpn/ccd/Jon KK4ZIZ

to

mkdir -p /tmp/openvpn/ccd
echo "iroute 192.168.1.0 255.255.255.0"> "/tmp/openvpn/ccd/Jon KK4ZIZ"

which now on the Status > OpenVPN I show the following:

Common Name Real Address Virtual Address Bytes Received Bytes Sent Connected Since Connected Since (time_t)
Jon KK4ZIZ xx.xx.xxx.xxx:xxxxx 10.0.8.2 1682084 1168544 Mon Dec 28 18:51:07 2015 1451328667
Virtual Address Common Name Real Address Last Ref
192.168.1.0/24 Jon KK4ZIZ xx.xx.xxx.xxx:xxxxx Mon Dec 28 18:51:10 2015
10.0.8.2 Jon KK4ZIZ xx.xx.xxx.xxx:xxxxx Mon Dec 28 19:12:21 2015
JonArmy
DD-WRT Novice


Joined: 24 Dec 2015
Posts: 21

PostPosted: Tue Dec 29, 2015 3:25    Post subject: Reply with quote
So far I can ping 10.0.8.2 and I get a good ping response. I can even place 10.0.8.2 and I get access to the router page of the client. If I ping one of the IP address on the client I don't get any response, if I even ping 192.168.1.254 which is the routers IP address I don't get a response.
JonArmy
DD-WRT Novice


Joined: 24 Dec 2015
Posts: 21

PostPosted: Tue Dec 29, 2015 9:45    Post subject: Reply with quote
eibgrad wrote:
Let's see if the 192.168.1.0/24 route is in the OpenVPN server's routing table (this time w/ the OpenVPN server running!).

Code:
route -n


root@DD-WRT:~# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 br0
10.0.8.0 0.0.0.0 255.255.255.0 U 0 0 0 tun2
172.73.0.0 0.0.0.0 255.255.224.0 U 0 0 0 vlan2
169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 br0
127.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 lo
0.0.0.0 172.73.0.1 0.0.0.0 UG 0 0 0 vlan2
root@DD-WRT:~#

Let's dump the firewall on the OpenVPN server side as well, just to make sure all is as expected.

Code:
iptables -vnL INPUT
iptables -vnL FORWARD
iptables -t nat -vnL POSTROUTING


root@DD-WRT:~# iptables -vnL INPUT
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination

0 0 ACCEPT 0 -- tun2 * 0.0.0.0/0 0.0.0.0/0
state NEW
13296 1647K ACCEPT tcp -- vlan2 * 0.0.0.0/0 0.0.0.0/0
tcp dpt:443
2395 310K logaccept 0 -- * * 0.0.0.0/0 0.0.0.0/0
state RELATED,ESTABLISHED
0 0 ACCEPT tcp -- vlan2 * 0.0.0.0/0 0.0.0.0/0
tcp dpt:1723
0 0 logaccept tcp -- * * 0.0.0.0/0 0.0.0.0/0
tcp dpt:1723
9 531 logaccept 47 -- * * 0.0.0.0/0 0.0.0.0/0

0 0 logaccept tcp -- * * 0.0.0.0/0 0.0.0.0/0
tcp dpt:443
0 0 ACCEPT 0 -- tun2 * 0.0.0.0/0 0.0.0.0/0

0 0 logdrop udp -- vlan2 * 0.0.0.0/0 0.0.0.0/0
udp dpt:520
0 0 logdrop udp -- br0 * 0.0.0.0/0 0.0.0.0/0
udp dpt:520
0 0 ACCEPT udp -- * * 0.0.0.0/0 0.0.0.0/0
udp dpt:520
0 0 logdrop icmp -- vlan2 * 0.0.0.0/0 0.0.0.0/0

0 0 logdrop 2 -- * * 0.0.0.0/0 0.0.0.0/0

10 599 ACCEPT 0 -- lo * 0.0.0.0/0 0.0.0.0/0
state NEW
763 54716 logaccept 0 -- br0 * 0.0.0.0/0 0.0.0.0/0
state NEW
755 77807 logdrop 0 -- * * 0.0.0.0/0 0.0.0.0/0

root@DD-WRT:~# iptables -nvL FORWARD
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination

0 0 ACCEPT 0 -- * tun2 0.0.0.0/0 0.0.0.0/0
state NEW
200 13815 ACCEPT 0 -- tun2 * 0.0.0.0/0 0.0.0.0/0
state NEW
0 0 ACCEPT 0 -- tun2 * 0.0.0.0/0 0.0.0.0/0
state NEW
0 0 logaccept 47 -- * vlan2 192.168.0.0/24 0.0.0.0/0

0 0 logaccept tcp -- * vlan2 192.168.0.0/24 0.0.0.0/0
tcp dpt:1723
5932 688K ACCEPT 0 -- tun2 * 0.0.0.0/0 0.0.0.0/0

8164 942K ACCEPT 0 -- * tun2 0.0.0.0/0 0.0.0.0/0

36880 18M lan2wan 0 -- * * 0.0.0.0/0 0.0.0.0/0

35707 18M logaccept 0 -- * * 0.0.0.0/0 0.0.0.0/0
state RELATED,ESTABLISHED
907 52292 TCPMSS tcp -- * * 0.0.0.0/0 0.0.0.0/0
tcp flags:0x06/0x02 TCPMSS clamp to PMTU
0 0 logaccept 0 -- br0 br0 0.0.0.0/0 0.0.0.0/0

0 0 TRIGGER 0 -- vlan2 br0 0.0.0.0/0 0.0.0.0/0
TRIGGER type:in match:0 relate:0
1173 82891 trigger_out 0 -- br0 * 0.0.0.0/0 0.0.0.0/0

1063 76847 logaccept 0 -- br0 * 0.0.0.0/0 0.0.0.0/0
state NEW
110 6044 logdrop 0 -- * * 0.0.0.0/0 0.0.0.0/0

root@DD-WRT:~# iptables -t nat -vnL POSTROUTING
Chain POSTROUTING (policy ACCEPT 1645 packets, 109K bytes)
pkts bytes target prot opt in out source destination

937 66424 SNAT 0 -- * vlan2 192.168.0.0/24 0.0.0.0/0
to:172.73.30.44
0 0 MASQUERADE 0 -- * * 0.0.0.0/0 0.0.0.0/0
mark match 0x80000000/0x80000000
185 12871 MASQUERADE 0 -- * vlan2 10.0.8.0/24 0.0.0.0/0

0 0 MASQUERADE 0 -- * tun2 !192.168.0.0/24 0.0.0.0/0

0 0 MASQUERADE 0 -- * vlan2 192.168.1.0/24 0.0.0.0/0
bl@d3runn3r
DD-WRT User


Joined: 10 Jan 2010
Posts: 210

PostPosted: Thu Sep 07, 2017 13:39    Post subject: Reply with quote
eibgrad wrote:
On the OpenVPN server side, add the following to your startup script, replacing <common-name> w/ the common-name you specified when building your client keys w/ easy-rsa.

Code:
mkdir -p /tmp/openvpn/ccd
echo "iroute 192.168.1.0 255.255.255.0" > /tmp/openvpn/ccd/<common-name>


NOTE: Based on your previous post, I’m assuming the network behind the OpenVPN client is 192.168.1.0/24.

Here’s a replacement set of firewall rules for the OpenVPN server that supports site-to-site and port forwarding. Here too, I’m assuming the same config from your other thread.

Code:
#!/bin/sh
OVPN_SERVER="10.0.8.0/24"
OVPN_DEV="tun2"
OVPN_PROTO="tcp"
OVPN_PORT="443"

WAN_IF="$(ip route | awk '/^default/{print $NF}')"

# open the OpenVPN server port
iptables -I INPUT -i $WAN_IF -p $OVPN_PROTO --dport $OVPN_PORT -j ACCEPT

# allow OpenVPN clients to access the OpenVPN server
iptables -I INPUT -i $OVPN_DEV -m state --state NEW -j ACCEPT

# allow OpenVPN clients to access ALL other devices on the LAN
iptables -I FORWARD -i $OVPN_DEV -m state --state NEW -j ACCEPT

# allow OpenVPN clients to access ALL other devices on the LAN
iptables -I FORWARD -i $OVPN_DEV -m state --state NEW -j ACCEPT

# allow OpenVPN clients to use the OpenVPN server as an internet gateway
iptables -t nat -A POSTROUTING -s $OVPN_SERVER -o $WAN_IF -j MASQUERADE

# what follows is only for site-to-site and port forwarding over the the tunnel

LAN_IP="$(nvram get lan_ipaddr)"
LAN_NET="$LAN_IP/$(nvram get lan_netmask)"
REMOTE_NET="192.168.1.0/24"

# allow local devices to become clients of the remote network
iptables -I FORWARD -o $OVPN_DEV -m state --state NEW -j ACCEPT

# NAT anything that's NOT the local network (e.g., WAN) over the OpenVPN tunnel
iptables -t nat -A POSTROUTING -s ! $LAN_NET -o $OVPN_DEV -j MASQUERADE

# NAT the remote network (behind the OpenVPN client) over the WAN
iptables -t nat -A POSTROUTING -s $REMOTE_NET -o $WAN_IF -j MASQUERADE


First verify that clients on the OpenVPN server’s local network can ping devices behind the OpenVPN client. Until that’s working, port forwarding will not work either. Then it should be a simple matter to specify the 192.168.1.x network for your port forwards on the OpenVPN server side and have them routed to the OpenVPN client’s network. Because we’ve NAT’d those packets, the replies should be forwarded back over the VPN as well.

Realize I’ve doing this off the top of my head, so it could still require some adjustments. And if it does work, there’s one other change I’d recommend, but I won’t discuss right now.

I know it's a old thread but is it possible the firewall rules set is incomplete?
I applied the same set as described here but for some reason my VPN clients are unable to access LAN devices.
Display posts from previous:    Page 1 of 1
Post new topic   Reply to topic    DD-WRT Forum Index -> Advanced Networking All times are GMT

Navigation

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum