Open VPN, I want to restrict to only a portion of my network

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


Joined: 10 Sep 2012
Posts: 2

PostPosted: Mon Sep 10, 2012 8:34    Post subject: Open VPN, I want to restrict to only a portion of my network Reply with quote
I am new to DD-WRT and even newer to VPNs. The VPN service I use provides an automatically generated script to put in the router start-up to configure the VPN. The thing is I really don't want everything connected to my router to be routed through the VPN.

I wonder if anyone can point me in the right direction for how to modify the given script so that I can limit the VPN to perhaps a specified IP range or specific IP addresses on my local network? I am of course open to other ways this can be achieved, a vlan.

My router is the Netgear WNDR3700 v2

Here's the script i've got at the moment (private 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 xxx.xxx.xxx.xxx 1194 udp
dev tun
dev-type tun
comp-lzo yes
verb 3
script-security 3
auth-user-pass user.txt
<ca>
-----BEGIN CERTIFICATE-----
MIIDNzCCAqCgAwIBAgIJANT/PXbMiYxfMA0GCSqGSIb3DQEBBQUAMHExCzAJBgNV
....
NvOty17e7gFUz+Y=
-----END CERTIFICATE-----
</ca>

key-direction 1
<tls-auth>
-----BEGIN OpenVPN Static key V1-----
62c8f2b6f4268d6459296c9685d825d9
.....
467d953fcdfc302fa6dd3c52a49c3b20
-----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
Sponsor
woodomat
DD-WRT Novice


Joined: 28 Aug 2012
Posts: 13

PostPosted: Mon Sep 10, 2012 10:31    Post subject: Reply with quote
If I understand you correctly, this thread may solve the problem for you:

http://dd-wrt.com/phpBB2/viewtopic.php?t=161397

It routes only selected destination IP ranges via VPN.

Also try to avoid automated setup and use the GUI instead by enabling OpenVPN client in the Services section (based on 18777 BS or 18946 EKO build)

- ServerName/IP: (the given IP address)
- Port: 1194
- Tunnel Device: TUN
- Tunnel Protocol: UDP
- Encryption cipher: (ask your VPN provider or try blowfish or AES-128)
- Hash Algorithm: (ask your VPN provider or try SHA1)
- nsCertValidation: enable
- Advanced options: yes
- LZO compression: adaptive
- TLS Cipher: (ask your VPN provider or try none)
- NAT: enable
- IP address: (leave empty)
- Subnet mask: (leave empty)
- TUN MTU setting: 1500 (or ask your VPN provider)
- MSS-fix: empty (or ask your VPN provider)

- TLS auth key: paste EVERYTHING from in between the <tls-auth> and </tls-auth> (including the BEGIN and END OpenVPN Static key lines) section of the script you've posted
- Additional config:
auth-user-pass /tmp/openvpncl/user.conf
- Policy-based routing: (leave empty)
- CA cert: paste EVERYTHING from in between the <ca> and </ca> (including the BEGIN and END CERTIFICATE lines) section of the script you've posted

Then save but do NOT yet apply settings.

Switch over to the Administration/Commands section and enter the following (including line breaks!) into the commands box:

echo "username
password" > /tmp/openvpncl/user.conf

Then click on "Save startup". Replace username and password with the credentials supplied by your OpenVPN provider. The command above should show up in the Startup section of this page.

Now reboot your router. Once it's up again, you might need to wait a few more minutes for the OpenVPN channel to be established. Once you've made sure that OpenVPN works as desired you may paste the instructions in the thread I've posted above into the "Additional Config" section of you OpenVPN client setup. DO NOT OVERWRITE THE "auth-user-pass /tmp/openvpncl/user.conf" LINE THOUGH as this is needed to point the client to the correct login credentials.

Oh yes, and before doing all this remove the automated script.
Snood
DD-WRT Novice


Joined: 10 Sep 2012
Posts: 2

PostPosted: Mon Sep 10, 2012 10:49    Post subject: Reply with quote
Thanks for the setup information. The thread you point to is for selective destinations which i don't think is quite what I want. I want to route to all destinations but selective local IP addresses.

If I understand correctly, the link you provided would route all my traffic to selected destinations via VPN. This isn't quite what i'm after.

I want to route to all destinations via VPN but only for specified IP addresses on my local network (in my case I want my Smart TV to connect through the VPN but not my PC)
woodomat
DD-WRT Novice


Joined: 28 Aug 2012
Posts: 13

PostPosted: Mon Sep 10, 2012 12:02    Post subject: Reply with quote
Sorry, then I got you wrong. The link I've posted does NOT do what you want.

I once tried setting up a dedicated virtual WLAN that routes everything over VPN but couldn't get that to work, even though it *should* be possible.
kk5000
DD-WRT Novice


Joined: 25 Jun 2012
Posts: 20

PostPosted: Mon Oct 15, 2012 1:21    Post subject: Reply with quote
Hi Snood,

So was there a solution here? I too am looking to route my Roku via the VPN.
kanishke007
DD-WRT Novice


Joined: 17 Oct 2012
Posts: 1

PostPosted: Wed Oct 17, 2012 22:20    Post subject: Reply with quote
anybody have any solution to this? i am looking to do the same with my Roku
Sash
DD-WRT Guru


Joined: 20 Sep 2006
Posts: 17619
Location: Hesse/Germany

PostPosted: Thu Oct 18, 2012 9:03    Post subject: Reply with quote
wiki: policy based routing

or gui policy based routign

_________________
Forum Guidelines...How to get help
&
Forum Rules
&
RTFM/STFW
&
Throw some buzzwords into the WIKI search Exclamation
_________________
I'm NOT rude, just offer pure facts!
_________________
Atheros (TP-Link & Clones, etc ) debrick service in EU
_________________
Guide on HowTo be Safe, Secure and Protect Your Online Anonymity!
kk5000
DD-WRT Novice


Joined: 25 Jun 2012
Posts: 20

PostPosted: Thu Oct 18, 2012 11:07    Post subject: Reply with quote
Of course policy based routing is a solution if you are by default going to be routing everything through the VPN and want to specify what not to route through it. However, our setup is a bit more complex (at least mine and I know one other person in this thread). We are using these rules in "additional config" :

auth-user-pass /tmp/openvpncl/user.conf

### OpenVPN common configuration
route-nopull
route 208.76.55.2 443 255.255.255.255 net_gateway
### OpenVPN routes

# whatismyip
route 98.207.0.0 255.255.0.0 vpn_gateway

# pandora
route 208.85.40.0 255.255.248.0 vpn_gateway

# amazon ec2 (us)
route 23.20.0.0 255.252.0.0 vpn_gateway
route 50.16.0.0 255.252.0.0 vpn_gateway
route 50.112.0.0 255.255.0.0 vpn_gateway
route 54.224.0.0 255.240.0.0 vpn_gateway
route 54.240.0.0 255.240.0.0 vpn_gateway
route 67.202.0.0 255.255.192.0 vpn_gateway
route 72.44.32.0 255.255.224.0 vpn_gateway

There are many other rules but you get the idea.

Now, considering this, how do we tell the rules to ALWAYS route a particular source IP via the VPN?
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