opvn client PBR with port specific exception to wan

Post new topic   Reply to topic    DD-WRT Forum Index -> Advanced Networking
Goto page 1, 2  Next
Author Message
slidermike
DD-WRT Guru


Joined: 11 Nov 2013
Posts: 1487
Location: USA

PostPosted: Tue Jan 29, 2019 19:28    Post subject: opvn client PBR with port specific exception to wan Reply with quote
Gents,
I know this particular request has been covered several times as my searching has many results.
However since this isnt a baked in feature to dd-wrt and seems to require either eigrab's script or additional repo downloads to get iptables I am going to see if this can be made clearer for those of us less Linux savvy.
I am also adding a diagram showing what I would like to achieve.

Router:
R7800 Atheros chipset
Kong build: 38340M (1_20_2019 test build)
Standard IP scope (192.168.1.0/24)
ovpn client (nordvpn)
Using the PBR field for 1 client to use vpn (192.168.1.19)

I am happy to do all the PBR via the FW rules/IPtables etc.. if guided to the proper steps. Since the dd-wrt PBR is limited to source or destination IP only.
I need port based PBR at least for this one client device on the LAN so it is remotely available.

I have a Synology NAS which I want remote access to using port 5010.
I have port forwarding enabled for this on the router and I know that is working.
However, since this single client is also PBR to use the Nord ovpn, remote access is lost.
Request from internet coming in over WAN port 5010
but all 192.168.1.19 traffic PBR out the ovpn interface.
= broken remote management.

I do not currently (but have and can attach) a usb jiffs drive attached.

For some of you this is quite simple to fix. I have looked at eibgrabs script file floating around and honestly understand very little. Not enough to see what its doing and how to tweak it to my specific needs.
Also read that with kongs repo the iptables can be used.
Again, more skills that I do not currently have and with a busy work I dont have much time to devote to the deeper dive.

I am trying to get this working this way since taking my pfSense box out of the picture. In pfSense its easy to do.
Would have went with the stock firmware for the r7800 but it does not support nordvpn clients.

Thank you in advance.
Mike

_________________
Router currently owned:
Netgear R7800 - Router
Netgear R7000 - AP mode

R7000 specific Tips/Tricks.
http://www.dd-wrt.com/phpBB2/viewtopic.php?t=264152
Sponsor
egc
DD-WRT Guru


Joined: 18 Mar 2014
Posts: 12889
Location: Netherlands

PostPosted: Wed Jan 30, 2019 9:59    Post subject: Reply with quote
Mike, this is something for @Eibgrad, unfortunately he is MIA.

I am not a networking engineer but will share my thoughts for what it is worth.

It can be done manually but as your VPN is dynamic this does not seem like good idea.

I suggest using one of @Eibgrad's scripts, as you are dealing with ports you have to use the advanced script.

What it does is creating an alternate routing table just like PBR, only it creates table 200 instead of table 10 for PBR.
It adds routes just like PBR is doing.

But traffic is diverted using IP Tables instead of routing.
A new chain "ovpn_split" is added to the PREROUTING chain of the mangle table:
Code:

iptables -t mangle -N ovpn_split
iptables -t mangle -A PREROUTING -j ovpn_split

Traffic you specify is marked with MARK 1:
Code:
iptables -t mangle -A ovpn_split -p tcp -s 192.168.1.19 ! --dport 5010 -j MARK --set-mark 1

The final step is to "bind" the marked traffic to the alternate routing table with:
Code:
ip rule add fwmark 1 table 200


This is a simplification of the process (and only as far as I understand it)

I attached the script.

Important: delete anything from the PBR field but add: "route-noexec" in the additonal config field of the OVPN client.

Furthermore the following directives from @Eibgrad:

# 2. copy modified script to /jffs (or external storage, e.g., usb)
# 3. make script executable:
# chmod +x /jffs/ddwrt-ovpn-split-advanced-mike.sh
# 4. call this script from the startup script:
# /jffs/ddwrt-ovpn-split-advanced-mike.sh

# 6. disable policy based routing (services->vpn->openvpn client)
# 7. disable nat loopback (security->firewall, "filter wan nat redirection"
# must be checked)
# 8. disable qos (nat/qos->qos)
# 9. enable syslogd (services->services->system log)
# 10. reboot router
# limitations:
# - this script is NOT compatible w/ dd-wrt policy based routing
# - this script is NOT compatible w/ dd-wrt nat loopback
# - this script is NOT compatible w/ dd-wrt qos

# WARNING: do NOT skip steps #6 thru #9 or it won't work!

As this is complicated I will be surprised if it works out of the box but you never know

_________________
Routers:Netgear R7000, R6400v1, R6400v2, EA6900 (XvortexCFE), E2000, E1200v1, WRT54GS v1.
Install guide R6400v2, R6700v3,XR300:https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=316399
Install guide R7800/XR500: https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=320614
Forum Guide Lines (important read):https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=324087
portsup
DD-WRT User


Joined: 20 Oct 2018
Posts: 210

PostPosted: Thu Jan 31, 2019 3:05    Post subject: Reply with quote
Simple Google search


https://www.linuxquestions.org/questions/linux-networking-3/add-route-based-on-port-not-ip-486823/

I would guess you could use this with PBR. Would need to route the mark to the default table and might need to execute when inside the openvpn routeup script that makes the pbr.
slidermike
DD-WRT Guru


Joined: 11 Nov 2013
Posts: 1487
Location: USA

PostPosted: Thu Jan 31, 2019 14:10    Post subject: Reply with quote
Thanks guys,
I will review the suggestions.
As I indicated earlier, very busy with work.

_________________
Router currently owned:
Netgear R7800 - Router
Netgear R7000 - AP mode

R7000 specific Tips/Tricks.
http://www.dd-wrt.com/phpBB2/viewtopic.php?t=264152
slidermike
DD-WRT Guru


Joined: 11 Nov 2013
Posts: 1487
Location: USA

PostPosted: Sat Feb 02, 2019 15:55    Post subject: Reply with quote
egc,
thank you for providing the script and tweak.
Questions before implementation.
1)
Does
"add_rule -p tcp -s 192.168.1.19 ! --dport 5010"
under the "Begin Rules" section mean this:
add all 192.168.1.19 traffic except tcp port 5010 to go out the ovpn interface?
2)
I am guessing that the argument "!" means "except", would that be correct?
3)
Can I replace tcp with ip for all packets from that client on port 5010 or would I need a 2nd line for udp?
4)
What do the -s, -p, -i arguments do?
I assume -p means "pass" or "port".
I did a quick google search but didnt find a simple list of those variables.
5)
I want to also force all traffic from .18 over ovpn so the rule would look like this correct?
add_rule -s 192.168.1.18

Thank you in advance egc.
Your time and patience has been much appreciated.

_________________
Router currently owned:
Netgear R7800 - Router
Netgear R7000 - AP mode

R7000 specific Tips/Tricks.
http://www.dd-wrt.com/phpBB2/viewtopic.php?t=264152
egc
DD-WRT Guru


Joined: 18 Mar 2014
Posts: 12889
Location: Netherlands

PostPosted: Sat Feb 02, 2019 16:56    Post subject: Reply with quote
slidermike wrote:
egc,
thank you for providing the script and tweak.
Questions before implementation.
1)
Does a
"add_rule -p tcp -s 192.168.1.19 ! --dport 5010"
under the "Begin Rules" section mean this:
add all 192.168.1.19 traffic except tcp port 5010 to go out the ovpn interface?


That is correct

slidermike wrote:
2)
I am guessing that the argument "!" means "except", would that be correct?


! means not (except)

slidermike wrote:
3)
Can I replace tcp with ip for all packets from that client on port 5010 or would I need a 2nd line for udp?


No you need a second rule with udp: add_rule -p udp -s 192.168.1.19 ! --dport 5010

slidermike wrote:
4)
What do the -s, -p, -i arguments do?
I assume -p means "pass" or "port".
I did a quick google search but didnt find a simple list of those variables.


-s: source ip, -p: port, -i: in-interface

slidermike wrote:
5)
I want to also force all traffic from .18 over ovpn so the rule would look like this correct?
add_rule -s 192.168.1.18


Correct

slidermike wrote:

Your time and patience has been much appreciated.

You are welcome Smile

I hope it is gonna work, do not forget to reboot.

_________________
Routers:Netgear R7000, R6400v1, R6400v2, EA6900 (XvortexCFE), E2000, E1200v1, WRT54GS v1.
Install guide R6400v2, R6700v3,XR300:https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=316399
Install guide R7800/XR500: https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=320614
Forum Guide Lines (important read):https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=324087
slidermike
DD-WRT Guru


Joined: 11 Nov 2013
Posts: 1487
Location: USA

PostPosted: Sat Feb 02, 2019 17:11    Post subject: Reply with quote
One last question if you dont mind.
I was re-reading your OP response and I noticed in the body of your guidance you have
Quote:
But traffic is diverted using IP Tables instead of routing.
A new chain "ovpn_split" is added to the PREROUTING chain of the mangle table:
Code:

iptables -t mangle -N ovpn_split
iptables -t mangle -A PREROUTING -j ovpn_split

Traffic you specify is marked with MARK 1:
Code:
iptables -t mangle -A ovpn_split -p tcp -s 192.168.1.19 ! --dport 5010 -j MARK --set-mark 1

The final step is to "bind" the marked traffic to the alternate routing table with:
Code:
ip rule add fwmark 1 table 200

Do I need to add the rest of the variables to the script?
This is in the script
"add_rule -p tcp -s 192.168.1.19 ! --dport 5010"
and this is in your instructions
"iptables -t mangle -A ovpn_split -p tcp -s 192.168.1.19 ! --dport 5010 -j MARK --set-mark 1"

_________________
Router currently owned:
Netgear R7800 - Router
Netgear R7000 - AP mode

R7000 specific Tips/Tricks.
http://www.dd-wrt.com/phpBB2/viewtopic.php?t=264152
egc
DD-WRT Guru


Joined: 18 Mar 2014
Posts: 12889
Location: Netherlands

PostPosted: Sat Feb 02, 2019 17:14    Post subject: Reply with quote
Oh no, that is only an explanation of what the script does, you only have to add the udp rule and the rule for the other source address (-s 192.168.1.18 ) Smile
_________________
Routers:Netgear R7000, R6400v1, R6400v2, EA6900 (XvortexCFE), E2000, E1200v1, WRT54GS v1.
Install guide R6400v2, R6700v3,XR300:https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=316399
Install guide R7800/XR500: https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=320614
Forum Guide Lines (important read):https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=324087
slidermike
DD-WRT Guru


Joined: 11 Nov 2013
Posts: 1487
Location: USA

PostPosted: Sun Feb 03, 2019 20:58    Post subject: Reply with quote
To the best of my understanding I installed the script.
Added a couple of lines to it.
Made it executable.
Verified the certain services were configured as instructed and rebooted.
No traffic is going over the vpn tunnel now.
Its all going out the wan.
Sad

Checked it 2 more times, rebooted 3 additional times and the same results.

The only other thing I have in place is a DNSMASQ FW script to block adds which I took out completely for testing.

It appears nothing is flowing over the vpn tunnel.
Counters arent incrementing and the cpu usage isnt spiking which I see when I use the default PBR and say point the .19 client over the vpn.

Here is What i added to ddwrt-ovpn-split-advanced-mike.sh
# ------------------------------- BEGIN RULES -------------------------------- #
add_rule -p tcp -s 192.168.1.18 ! --dport 5010
add_rule -p tcp -s 192.168.1.19 ! --dport 5010
add_rule -s 192.168.1.18
add_rule -s 192.168.2.0/24

The 2.0/24 is a guest ssid



Added to the additional config section of ovpn.
remote-cert-tls server
remote-random
nobind
tun-mtu 1500
tun-mtu-extra 32
mssfix 1450
persist-key
persist-tun
ping-timer-rem
reneg-sec 0
route-noexec

Most of that is Nordvpn specific options. The last line is what the script instructions said to add.




root@R7800:/# ls jffs/
ddwrt-ovpn-split-advanced-mike.sh

_________________
Router currently owned:
Netgear R7800 - Router
Netgear R7000 - AP mode

R7000 specific Tips/Tricks.
http://www.dd-wrt.com/phpBB2/viewtopic.php?t=264152
egc
DD-WRT Guru


Joined: 18 Mar 2014
Posts: 12889
Location: Netherlands

PostPosted: Mon Feb 04, 2019 21:55    Post subject: Reply with quote
Sorry I did not responded sooner, I had urgent matters to attend to.

First thing is to check if the script is running.

Unfortunately the script is not in debug mode by default and I forgot to enable it.
In the script near the top is the line:
Code:
#DEBUG= # uncomment/comment to enable/disable debug mode


Remove the first # so that theline will be:
Code:
DEBUG= # uncomment/comment to enable/disable debug mode


Now you can see what the script is doing in syslog (of course syslog must be enabled).

If the script does not run, check if it is set as executable and if you use a windows editor check if there are no <CR> at the end of the lines (If you are not familiar with this I will give detailed instructions)

If the sript runs without errors the next thing is to see if the routing tables are created and the iptables rules are implemented show output of:

ip route show
ip route show table 200

ip rule show

iptables -vnL -t mangle

I am rather busy at the moment so my next answer can take a day or two Sad

_________________
Routers:Netgear R7000, R6400v1, R6400v2, EA6900 (XvortexCFE), E2000, E1200v1, WRT54GS v1.
Install guide R6400v2, R6700v3,XR300:https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=316399
Install guide R7800/XR500: https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=320614
Forum Guide Lines (important read):https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=324087
slidermike
DD-WRT Guru


Joined: 11 Nov 2013
Posts: 1487
Location: USA

PostPosted: Tue Feb 05, 2019 9:53    Post subject: Reply with quote
Thanks for your help egc.
Dont feel like you have to rush to help me get this sorted out.
Free support comes when it comes.
Smile

I will recreate the script and then edit it on the router with nano. I previously did the file edit on my linux machine.

I will report back my findings.

Thanks again!
Mike

_________________
Router currently owned:
Netgear R7800 - Router
Netgear R7000 - AP mode

R7000 specific Tips/Tricks.
http://www.dd-wrt.com/phpBB2/viewtopic.php?t=264152
slidermike
DD-WRT Guru


Joined: 11 Nov 2013
Posts: 1487
Location: USA

PostPosted: Tue Feb 05, 2019 20:09    Post subject: Reply with quote
So i put the script back onto the router.
Used nano on the router to edit & save the script changes.
Enabled debug.
Then made the script executable.
Verified steps 6-9, saved and rebooted the router.
Still appears everything is going out the wan interface.

Got the log output and command line output requested.
I dont know how to verify the script is executable now or how to see if it ran (or didnt) in the logs.

Attaching things here.

I will be rolling back for now.
egc
DD-WRT Guru


Joined: 18 Mar 2014
Posts: 12889
Location: Netherlands

PostPosted: Wed Feb 06, 2019 9:07    Post subject: Reply with quote
The script does not appear to run Sad
If it runs you should see every step in syslog (if debug is on)

Tomorrow I will get a new router to setup for someone and I will try running the script and see what is going on.

To be continued

_________________
Routers:Netgear R7000, R6400v1, R6400v2, EA6900 (XvortexCFE), E2000, E1200v1, WRT54GS v1.
Install guide R6400v2, R6700v3,XR300:https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=316399
Install guide R7800/XR500: https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=320614
Forum Guide Lines (important read):https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=324087
egc
DD-WRT Guru


Joined: 18 Mar 2014
Posts: 12889
Location: Netherlands

PostPosted: Thu Feb 07, 2019 12:56    Post subject: Reply with quote
I just revised the script, attached a revision.

The data you send indicate that the script has not ran.
It ran fine on my R6400 with the latest Kong build.

But lets take it step by step.

Set up your router and the VPN client.
Check that the VPN client is running and traffic is send via the VPN
In the Additional config of the VPN client add:
Code:
route-noexec

Check that traffic is now routed via the WAN

Unzip script and place in /jffs of your USB stick
(probably redundant do not enable JFFS2 support on Administration tab)

run script from the command line:
Code:
/jffs/ddwrt-ovpn-split-advanced-mike.sh

See if any error is thrown.

After about 30-60 seconds check if the script has run with:
Code:
iptables -vnL ovpn_split -t mangle


You should see your rules implemented:
Quote:
root@R6400v2:~# iptables -vnL ovpn_split -t mangle
Chain ovpn_split (1 references)
pkts bytes target prot opt in out source destination
18 1098 CONNMARK 0 -- * * 0.0.0.0/0 0.0.0.0/0 CONNMARK restore
12 626 RETURN 0 -- * * 0.0.0.0/0 0.0.0.0/0 mark match !0x0
0 0 MARK 0 -- tun1 * 0.0.0.0/0 0.0.0.0/0 MARK set 0x1
0 0 MARK 0 -- * * 192.168.1.18 0.0.0.0/0 MARK set 0x1
0 0 MARK tcp -- * * 192.168.1.19 0.0.0.0/0 tcp dpt:!5010 MARK set 0x1
6 472 MARK 0 -- * * 0.0.0.0/0 0.0.0.0/0 mark match !0x1 MARK set 0x2
6 472 CONNMARK 0 -- * * 0.0.0.0/0 0.0.0.0/0 CONNMARK save
root@R6400v2:~#


As I have enabled debugging you can see the execution of the script also in syslog.

I have tested with my setup and it works with a simple IP address, I have not tested it with the port exclusion you are implementing.
I am curious to know if it is going to work for you.

If it works you can add it to Administration/Commands and save as Startup.

For posterity there is actually a flaw in the script, hard to believe knowing @Eibgrad's skills.
But if you disable the use of an external rule file then the internal rules from the script are not executed because the invocation of the add_rules() is to deeply nested.
Disclaimer: my coding days are long gone so I could be totally wrong Smile

_________________
Routers:Netgear R7000, R6400v1, R6400v2, EA6900 (XvortexCFE), E2000, E1200v1, WRT54GS v1.
Install guide R6400v2, R6700v3,XR300:https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=316399
Install guide R7800/XR500: https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=320614
Forum Guide Lines (important read):https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=324087
slidermike
DD-WRT Guru


Joined: 11 Nov 2013
Posts: 1487
Location: USA

PostPosted: Fri Feb 08, 2019 22:30    Post subject: Reply with quote
Thanks man.,
I will give it another shot this weekend.
I didnt get a notification you had posted in my email so I didnt check until today.

Appreciate you taking the time to assist.

Is there a command like ls -l in the jffs dir to show if a file is executable?

Here is what it is (with the not working ver)
Quote:
root@R7800:/jffs# ls -l
-rwxrwxr-x 1 root root 15601 Feb 5 12:43 ddwrt-ovpn-split-advanced-mike.sh

Appears to be a permissions issue.
Quote:
root@R7800:/jffs# /jffs/ddwrt-ovpn-split-advanced-mike.sh
-sh: /jffs/ddwrt-ovpn-split-advanced-mike.sh: Permission denied
root@R7800:/jffs# pwd
/jffs

_________________
Router currently owned:
Netgear R7800 - Router
Netgear R7000 - AP mode

R7000 specific Tips/Tricks.
http://www.dd-wrt.com/phpBB2/viewtopic.php?t=264152
Goto page 1, 2  Next Display posts from previous:    Page 1 of 2
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