Port forward to local webserver issues (Open VPN)

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


Joined: 02 Apr 2018
Posts: 32

PostPosted: Mon Apr 02, 2018 16:03    Post subject: Port forward to local webserver issues (Open VPN) Reply with quote
Hello DD-WRT community.

I have an issue with my DDWRT router in regards to port forwarding and I am convinced its user error hence my first post here.

I have a TP-Link TL-WDR3600 with the latest DDWRT firmware installed from this site.

I have an open VPN script that I run via command (run at startup) that allows any connected device to use the VPN.

The service works but I am unable to forward ports on the router via GUI. The only way I can get port forwarding working is to use the command line interface option for port forwarding, see below.

Code:
iptables -t nat -I PREROUTING -p tcp --dport 80 -j DNAT --to 192.168.222.111:80
iptables -I FORWARD -p tcp -d 192.168.222.111 --dport 80 -j ACCEPT


When I do this my port forward works as expected, however I am no longer able to access the routers administration panel loacally (LAN) and any device plugged into the router other than the one thats been port forwarded does not get access to the internet.

I contacted the company who provided my OpenVPN service and they told me to run the below port forward command instead, replacing <INCOMING IF> with my interface (eth0, eth1 etc), in the below command I used eth0.

Code:
iptables -t nat -I PREROUTING -i <INCOMING IF> -p tcp --dport 80 -j DNAT --to 192.168.222.111:80
iptables -I FORWARD -i <INCOMING IF> -p tcp -d 192.168.222.111 --dport 80 -j ACCEPT


However when I ran this, there seemed to be no change, I was able to access the VPN & router but the port forward failed for the webserver.

My setup is below, any assistance would be much appreciated.




Regards
Sponsor
massop
DD-WRT Novice


Joined: 02 Apr 2018
Posts: 32

PostPosted: Mon Apr 02, 2018 20:22    Post subject: Reply with quote
eibgrad wrote:
I assume by OpenVPN and script you mean you have a script which configures and starts an OpenVPN client on startup...


Thanks for the feedback,

Yes you are right the script configures and starts the OpenVPN on startup.

The VPN is setup over a 1:1 static (unique IP) that allows incoming connections so communication to the local webserver is through the same network interface.

Like I said before running the below command works perfectly

Code:
iptables -t nat -I PREROUTING -p tcp --dport 80 -j DNAT --to 192.168.222.111:80
iptables -I FORWARD -p tcp -d 192.168.222.111 --dport 80 -j ACCEPT


But once I do this any other device plugged into the router will not gain access to the routers local admin page (192.168.222.1) and will also have no internet.

Why is the above command restricting every other device?

Sorry for my poor explanation, perhaps this will make things more clear.

Regards
massop
DD-WRT Novice


Joined: 02 Apr 2018
Posts: 32

PostPosted: Mon Apr 02, 2018 21:59    Post subject: Reply with quote
[quote="eibgrad"]
massop wrote:


Code:
iptables -t nat -I PREROUTING -p tcp -d $(nvram get wan_ipaddr) --dport 80 -j DNAT --to 192.168.222.111:80
iptables -I FORWARD -p tcp -d 192.168.222.111 --dport 80 -j ACCEPT


Thanks I will try this first thing in the morning and let you know my results.

The reason I am not using the GUI is because no matter what method I tried in the GUI to forward a port nothing would work.

There was also nothing in the log file to suggest any issues, this could be due to the script I am running on startup which you will see below (sensitive data removed)

Code:
# VPNUK OpenVPN Configuration file
# for DD-WRT compatible routers
# https://www.vpnuk.info

#!/bin/sh

# first we synchronize the time
ntpclient pool.ntp.org

# remove the temp directory
rm -rf /var/tmp/openvpn
mkdir -p /tmp/openvpn/

# create the config file
echo "
client
nobind
# Here is the IP address of your VPNUK server
# followed by the port and protocol to use
remote 109.169.3.2 1194 udp
dev tun
dev-type tun
comp-lzo yes
verb 3
log /tmp/openvpn.log
script-security 3
auth-user-pass user.txt
<ca>
-----BEGIN CERTIFICATE-----
XXX
-----END CERTIFICATE-----
</ca>

key-direction 1
<tls-auth>
-----BEGIN OpenVPN Static key V1-----
YYY
-----END OpenVPN Static key V1-----
</tls-auth>" > /tmp/openvpn/vpnuk.conf

# enter your VPNUK username and password
# separated by a new line
echo "username
password" > /tmp/openvpn/user.txt

chmod go-rwx /tmp/openvpn/user.txt

killall -9 openvpn
# start openvpn
cd /tmp/openvpn
/usr/sbin/openvpn --config /tmp/openvpn/vpnuk.conf &

sleep 60

iptables="/usr/sbin/iptables"
fw="/tmp/openvpn/port_forwards.sh"

for port in $forward_spec;
do
SPORT=`echo $port | cut -d \: -f 4 | cut -d \> -f 1`
DST=`echo $port | cut -d \: -f 4 | cut -d \> -f 2`
DPORT=`echo $port | cut -d \: -f 5`
for proto in tcp udp;
do
echo "$iptables -I FORWARD -i tun0 -p $proto --dport $SPORT -j ACCEPT" > $fw
echo "$iptables -t nat -I PREROUTING -i tun0 -p $proto --dport $SPORT -j DNAT --to $DST:$DPORT" >>$fw
done
done

for port in $forward_port;
do
SPORT1=`echo $port | cut -d \: -f 4`
SPORT2=`echo $port | cut -d \: -f 5 | cut -d \> -f 1`
DST=`echo $port | cut -d \: -f 5 | cut -d \> -f 2`
for proto in tcp udp;
do
echo "$iptables -I FORWARD -i tun0 -p $proto --dport $SPORT1-$SPORT2 -j ACCEPT" >>$fw
echo "$iptables -t nat -I PREROUTING -i tun0 -p $proto --dport $SPORT1-$SPORT2 -j DNAT --to $DST" >>$fw
done
done

sh $fw

# assure the network traffic is NATted
/usr/sbin/iptables -t nat -A POSTROUTING -o tun+ -j MASQUERADE

sleep 20

# first octets of the network of the VPN tunnel
NET=`ip ro ls 0.0.0.0/1 | cut -d \  -f 3 | awk 'BEGIN {FS="."} {print $1"."$2"."$3}'`

# enter the new DNS server on top of the old ones
echo "nameserver $NET.1" > /tmp/resolv.dnsmasq.TMP
cat /tmp/resolv.dnsmasq >> /tmp/resolv.dnsmasq.TMP
mv /tmp/resolv.dnsmasq.TMP /tmp/resolv.dnsmasq

# change some DNSMasq options
sed -i 's/stop-dns-rebind//g' /tmp/dnsmasq.conf
killall dnsmasq
dnsmasq --conf-file=/tmp/dnsmasq.conf
massop
DD-WRT Novice


Joined: 02 Apr 2018
Posts: 32

PostPosted: Tue Apr 03, 2018 10:28    Post subject: Reply with quote
Good morning,

I ran the below command

Code:
iptables -t nat -I PREROUTING -p tcp -d $(nvram get wan_ipaddr) --dport 80 -j DNAT --to 192.168.222.111:80
iptables -I FORWARD -p tcp -d 192.168.222.111 --dport 80 -j ACCEPT


But the port forward did not work, if you go to http://109.169.3.4 you will see the DDWRT admin page and not the webserver sitting behind it, see below




Is this command failing because the WAN IP is local? (192.x) the router does not show the VPN WAN address because it sits behind another router but when connected to it you do get the VPN IP (also navigating to 109.169.3.4 from the outside gets you to the router as well)

Could there also be an issue with the initial script that starts the openVPN service conflicting with this port forward request?

And could there be something in that script that would stop the port forward function working through the GUI?

Thanks in advance, I really appreciate the help here.
Per Yngve Berg
DD-WRT Guru


Joined: 13 Aug 2013
Posts: 6870
Location: Romerike, Norway

PostPosted: Tue Apr 03, 2018 10:58    Post subject: Reply with quote
You have to specify the correct interface with VPN. It's usually "-i tun2".
massop
DD-WRT Novice


Joined: 02 Apr 2018
Posts: 32

PostPosted: Tue Apr 03, 2018 11:27    Post subject: Reply with quote
Per Yngve Berg wrote:
You have to specify the correct interface with VPN. It's usually "-i tun2".


Thanks for the response,

can you specify exactly where I would put this? I want to ensure i dont make any mistakes.

Thanks
massop
DD-WRT Novice


Joined: 02 Apr 2018
Posts: 32

PostPosted: Tue Apr 03, 2018 15:18    Post subject: Reply with quote
Thanks for the feedback on this.

I ran each suggested command but I still did not get access to the webserver.

To ensure the webserver is actually working I used the old command below and it worked (with no access to the router locally as before)

Code:
iptables -t nat -I PREROUTING -p tcp --dport 80 -j DNAT --to 192.168.222.111:80
iptables -I FORWARD -p tcp -d 192.168.222.111 --dport 80 -j ACCEPT


The following 2 commands did not forward to the webserver

Code:
iptables -t nat -I PREROUTING -p tcp -d 109.169.3.4 --dport 80 -j DNAT --to 192.168.222.111:80
iptables -I FORWARD -p tcp -d 192.168.222.111 --dport 80 -j ACCEPT


Code:
iptables -t nat -I PREROUTING -i tun0 -p tcp --dport 80 -j DNAT --to 192.168.222.111:80
iptables -I FORWARD -p tcp -d 192.168.222.111 --dport 80 -j ACCEPT


In regards to access via http i will definitely take your advice on this, if i run the final command you mentioned..

Code:
iptables -I INPUT -i tun0 -m state --state NEW -j DROP
iptables -I INPUT -i tun0 -p tcp --dport 22 -m state --state NEW -j ACCEPT


Will that restrict access to https? or will it only allow access via SSH?

Thanks[/code]
massop
DD-WRT Novice


Joined: 02 Apr 2018
Posts: 32

PostPosted: Tue Apr 03, 2018 15:44    Post subject: Reply with quote
Ok here is where my understanding of this may hinder the solution.

with that command you just stated, if I try to access my static VPN IP via port 10080 I should finally see my webserver?

I entered http://109.169.3.4:10080/ into my web browser and got a refused connection error.

Is this how I should be reaching my web server?

Thanks
massop
DD-WRT Novice


Joined: 02 Apr 2018
Posts: 32

PostPosted: Tue Apr 03, 2018 16:31    Post subject: Reply with quote
Thanks for the feedback,

this service I have from my VPN provider is dedicated, IOW I am the only user who has access to this IP and in turn there are no blocked ports on the service (you pay extra for this).

Just to be sure I spoke to the support guys and they confirmed that there are no blocked ports on my VPN IP and 100% I am the only person who has access to that IP.

Just to be sure however i will try your command with some different ports and let you know my results.

Thanks
massop
DD-WRT Novice


Joined: 02 Apr 2018
Posts: 32

PostPosted: Tue Apr 03, 2018 17:01    Post subject: Reply with quote
Please see below response, I had just reboot the router so the last port forward command will not be present, please let me know if you want me to run the last command and rerun this query. Thanks

Code:
root@DD-WRT:~# iptables -t nat -vnL
Chain PREROUTING (policy ACCEPT 45 packets, 5024 bytes)
 pkts bytes target     prot opt in     out     source               destination                                                                                                                                                                                                                                             
    0     0 DNAT       tcp  --  *      *       0.0.0.0/0            192.168.0.24                                                                                                                                                                                                                                                     tcp dpt:23 to:192.168.222.1:23
    0     0 DNAT       icmp --  *      *       0.0.0.0/0            192.168.0.24                                                                                                                                                                                                                                                     to:192.168.222.1
    0     0 TRIGGER    0    --  *      *       0.0.0.0/0            192.168.0.24                                                                                                                                                                                                                                                     TRIGGER type:dnat match:0 relate:0

Chain INPUT (policy ACCEPT 38 packets, 2395 bytes)
 pkts bytes target     prot opt in     out     source               destination                                                                                                                                                                                                                                             

Chain OUTPUT (policy ACCEPT 1 packets, 70 bytes)
 pkts bytes target     prot opt in     out     source               destination                                                                                                                                                                                                                                             

Chain POSTROUTING (policy ACCEPT 1 packets, 70 bytes)
 pkts bytes target     prot opt in     out     source               destination                                                                                                                                                                                                                                             
    0     0 SNAT       0    --  *      vlan2   192.168.222.0/24     0.0.0.0/0                                                                                                                                                                                                                                                        to:192.168.0.24
    0     0 MASQUERADE  0    --  *      *       0.0.0.0/0            0.0.0.0/0                                                                                                                                                                                                                                                        mark match 0x80000000/0x80000000
    0     0 MASQUERADE  0    --  *      tun+    0.0.0.0/0            0.0.0.0/0                                                                                                                                                                                                                                               
root@DD-WRT:~# iptables -vnL INPUT
Chain INPUT (policy ACCEPT 556 packets, 74063 bytes)
 pkts bytes target     prot opt in     out     source               destination                                                                                                                                                                                                                                             
    0     0 logdrop    tcp  --  vlan2  *       0.0.0.0/0            0.0.0.0/0                                                                                                                                                                                                                                                        tcp dpt:8080
    0     0 logdrop    tcp  --  vlan2  *       0.0.0.0/0            0.0.0.0/0                                                                                                                                                                                                                                                        tcp dpt:80
    0     0 logdrop    tcp  --  vlan2  *       0.0.0.0/0            0.0.0.0/0                                                                                                                                                                                                                                                        tcp dpt:443
    0     0 logdrop    tcp  --  vlan2  *       0.0.0.0/0            0.0.0.0/0                                                                                                                                                                                                                                                        tcp dpt:69
    0     0 logdrop    tcp  --  vlan2  *       0.0.0.0/0            0.0.0.0/0                                                                                                                                                                                                                                                        tcp dpt:22
root@DD-WRT:~# iptables -vnL FORWARD
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
    0     0 lan2wan    0    --  *      *       0.0.0.0/0            0.0.0.0/0
    0     0 ACCEPT     0    --  *      *       0.0.0.0/0            0.0.0.0/0           state RELATED,                                                                                                                                                                                                                       ESTABLISHED
    0     0 TCPMSS     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp flags:0x06                                                                                                                                                                                                                       /0x02 TCPMSS clamp to PMTU
    0     0 ACCEPT     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:i                                                                                                                                                                                                                       n match:0 relate:0
    0     0 trigger_out  0    --  br0    *       0.0.0.0/0            0.0.0.0/0
    0     0 ACCEPT     0    --  br0    *       0.0.0.0/0            0.0.0.0/0           state NEW
massop
DD-WRT Novice


Joined: 02 Apr 2018
Posts: 32

PostPosted: Tue Apr 03, 2018 19:42    Post subject: Reply with quote
eibgrad wrote:
Seems to me you're doing a lot more messing around w/ iptables than originally indicated. For example, where's the ESTABLISHED/RELATED rule in the INPUT chain? And normally there's a DROP rule at the end of the INPUT and FORWARD chains as well (to catch fall-thru situations).

If you're manipulating the firewall beyond what the script requires and adding the VPN port forwards, then it's going to be difficult to debug the problem.


Thanks for taking your time to help me with this.

I have had a development while working on this over the last few hours.

I have been using the "run command" option in the DDWRT GUI to apply the port forward and it has been working without issue this whole time.

However, I just went to apply the command that I know works (but locks off local access) and the port forward did not apply at all.

Since i had the Telnet session open I ran the command in there and it worked immediately

This leads me to believe the "run command" function in the gui does not always parse the commands you enter, possibly due to latency issues as I am running off a 4G connection here.

Anyway I went back over all the commands you listed before and the below command now shows my webserver

Code:
iptables -t nat -I PREROUTING -i tun0 -p tcp --dport 80 -j DNAT --to 192.168.222.111:80
iptables -I FORWARD -p tcp -d 192.168.222.111 --dport 80 -j ACCEPT


I am not sure if the local machines have internet but I will be able to confirm this tomorrow afternoon.

If it does work I will be sure to reply here with the full solution incase anyone else has a similar issue.

I will keep you posted.

Regards
massop
DD-WRT Novice


Joined: 02 Apr 2018
Posts: 32

PostPosted: Wed Apr 04, 2018 15:23    Post subject: Reply with quote
eibgrad wrote:
Btw, the Run Commands feature is known to have issues. I never use it for anything. It doesn't quite work like a shell (telnet/ssh) as you might expect. It interprets entered commands, esp. multi-line commands, in an odd way I've never understood. If you're just experimenting w/ commands/scripts, always use a proper shell (telnet/ssh). And once these firewall rules are working to your satisfaction, place them in the firewall script.


I can confirm the below code works as my local devices have access to the internet & router.

Code:
iptables -t nat -I PREROUTING -i tun0 -p tcp --dport 80 -j DNAT --to 192.168.222.111:80
iptables -I FORWARD -p tcp -d 192.168.222.111 --dport 80 -j ACCEPT


Thanks for your help with this, I have been pulling my hair out for weeks on this and not even the support for the VPN who created the original script could figure out what the issue was.

Now this matter is solved I have 1 last question,

If I want to forward more ports to the same device do I just run the same commands again but with the new ports?

And do you think it would be wise to add all the ports I want forwarded to the script that runs at startup?

Thanks
massop
DD-WRT Novice


Joined: 02 Apr 2018
Posts: 32

PostPosted: Wed Apr 18, 2018 22:53    Post subject: Reply with quote
I stuck to your advise and kept the port forward commands separate from "Run at startup", all seems to be working well.

I have another question but I am not sure if this one is possible.

The below command is routing all web traffic to port 80 on the mentioned device.

Code:
iptables -t nat -I PREROUTING -i tun0 -p tcp --dport 80 -j DNAT --to 192.168.222.111:80
iptables -I FORWARD -p tcp -d 192.168.222.111 --dport 80 -j ACCEPT


Is there anyway to restrict this to a specific WAN IP?
massop
DD-WRT Novice


Joined: 02 Apr 2018
Posts: 32

PostPosted: Thu Apr 19, 2018 10:09    Post subject: Reply with quote
eibgrad wrote:
I assume you mean restrict to a specific public source IP, not WAN ip. The WAN ip is your own public IP. And besides, this is a VPN port forward, not a WAN port forward.

Just use the -s option.

Code:
iptables -t nat -I PREROUTING -i tun0 -s 199.199.199.199 -p tcp --dport 80 -j DNAT --to 192.168.222.111:80
iptables -I FORWARD -p tcp -d 192.168.222.111 --dport 80 -j ACCEPT


Thank you for the clarification, this works perfectly,

In the end I have done the following to completely get my webserver up & running for those of you who use the UKVPN service (& any other vpn service via startup script I assume)


To port forward allowing only a specific public address..

Code:
iptables -t nat -I PREROUTING -i tun0 -s [PUBLICIP] -p tcp --dport 80 -j DNAT --to [LOCALIP:PORT]
iptables -I FORWARD -p tcp -d [LOCALIP] --dport 80 -j ACCEPT


To restrict access to my router accept telnet (port 23)

Code:
iptables -I INPUT -i tun0 -m state --state NEW -j DROP
iptables -I INPUT -i tun0 -p tcp --dport 23 -m state --state NEW -j ACCEPT


To port forward all to specific local address

Code:
iptables -t nat -I PREROUTING -i tun0 -p tcp --dport 80 -j DNAT --to [LOCALIP:PORT]
iptables -I FORWARD -p tcp -d [LOCALIP] --dport 80 -j ACCEPT



Thanks again for all you help.
massop
DD-WRT Novice


Joined: 02 Apr 2018
Posts: 32

PostPosted: Fri Jul 13, 2018 13:24    Post subject: Reply with quote
Hi again,

I have been running this setup abroad and it has worked perfectly.

I have a small issue I need to address to this config however.

I am running a VoIP server through the VPN on this and although I have forwarded some UDP ports I actually need to forward a large range of UDP only (5000 - 65565)

How can I forward a range of UDP ports in 1 command? will the below work?

Code:
iptables -t nat -I PREROUTING -i tun0 -s [Public IP to allow] -p udp --dport 5000:65535 -j DNAT --to 192.168.0.2:5000:65535

iptables -I FORWARD -p udp -d 192.168.0.2 --dport 5000:65535 -j ACCEPT


Any help will be greatly appreciated.

Thanks
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