Introduction
One of the core elements of my home network is a specific VLAN which sends all of its traffic out over a VPN. I use my UniFi USG4 and ProtonVPN in order to accomplish this. Much of the configuration is taken from the guide written by 'rggn' here and some from the video here.
This setup gives me some peace of mind, because I know that any device running on that VLAN (or its acompanying SSID) is ensured to be tunneled through a VPN. This avoids problems with a VPN client on a device going down without me knowing.
Recently, a bad UniFi update completly bricked my network, so I had to run through this process again, and found it a little bit tedious so... time to automate. There is defenetley a lesson learned there about not just backing up, but restoring from backups, but getting a chance to touch this again was beneficial.
Setup
This is pretty much just following the first guide again. I recomend building each of these on a remote machine and then scping them onto the USG.
Here are the files I will use:
charon.conf
config.gateway.json # note that this one goes on the UniFi Cloud Controller
ipsec-updown.sh
protonvpn-check
protonvpn.ipsec.conf
protonvpn.ipsec.secrets
charon.conf
charon {
install_routes = no
install_virtual_ip = no
}
config.gateway.json
The config.gateway.json is the only file that gets placed on the UniFi Cloud Key. If you dont remember that you may spend several hours repeatedly putting it on your USG (like every other file) and wondering why things are not working. T
his file is where you are going to create your custom routing rules. It is pretty self explanatory. If you get confused to can take the existing config off of the USG (located at /config/config.boot) and throw it into a json linter in order to see all the moving parts and assign the right things. This isn't exactly whats in the guide from rggn because they were tunneling all of their traffic, and I had to add a custom rule to only send a specific VLAN.
If this is your first time doing this, before you make this file you should go into your UniFi Network GUI and make a new VLAN XX that will be your VPN VLAN, and assign it a subnet A.B.XX.0/24. Just sub this subnet and mask into the line 13 below, assign whatever description you want in line 8, and put the VLAN ID in line 34. you should be good. As long as you dont have any crazy rules existing in your iptables, the rule numbers should not overlap.
{
"firewall": {
"modify": {
"SOURCE_ROUTE": {
"rule": {
"10": {
"action": "modify",
"description": "route VLANXX to protonVPN",
"modify": {
"table": "5"
},
"source": {
"address": "10.0.XX.0/24"
}
}
}
}
},
"options": {
"mss-clamp": {
"interface-type": [
"pppoe",
"pptp",
"vti"
],
"mss": "1350"
}
}
},
"interfaces": {
"ethernet": {
"eth0": {
"vif": {
"XX": {
"firewall": {
"in": {
"modify": "SOURCE_ROUTE",
"name": "LAN_IN"
}
}
}
}
}
},
"vti": {
"vti0": {
"description": "IPSec VTI interface"
}
}
},
"protocols": {
"static": {
"table": {
"5": {
"interface-route": {
"0.0.0.0/0": {
"next-hop-interface": {
"vti0": "''"
}
}
}
}
}
}
},
"service": {
"nat": {
"rule": {
"5004": {
"description": "masq to vpn vti0",
"destination": {
"address": "0.0.0.0/0"
},
"outbound-interface": "vti0",
"type": "masquerade"
}
}
}
},
"vpn": {
"ipsec": {
"auto-firewall-nat-exclude": "enable",
"include-ipsec-conf": "/config/protonvpn.ipsec.conf",
"include-ipsec-secrets": "/config/auth/protonvpn.ipsec.secrets"
}
}
}
ipsec-updown.sh
#!/bin/bash
set -o nounset
set -o errexit
# $VTI_IFACE must match the interface in config.gateway.json
VTI_IFACE="vti0"
case "${PLUTO_VERB}" in
up-client)
echo "Creating tunnel interface ${VTI_IFACE}"
ip tunnel add "${VTI_IFACE}" local "${PLUTO_ME}" remote "${PLUTO_PEER}" mode vti
echo "Activating tunnel interface ${VTI_IFACE}"
ip link set "${VTI_IFACE}" up
echo "Adding ${PLUTO_MY_SOURCEIP} to ${VTI_IFACE}"
ip addr add "${PLUTO_MY_SOURCEIP}" dev "${VTI_IFACE}"
echo "Disabling IPsec policy (SPD) for ${VTI_IFACE}"
sysctl -w "net.ipv4.conf.${VTI_IFACE}.disable_policy=1"
DEFAULT_ROUTE="$(ip route show default | grep default | awk '{print $3}')"
echo "Identified default route as ${DEFAULT_ROUTE}"
echo "Adding route: ${PLUTO_PEER} via ${DEFAULT_ROUTE} dev ${PLUTO_INTERFACE}"
ip route add "${PLUTO_PEER}" via "${DEFAULT_ROUTE}" dev "${PLUTO_INTERFACE}"
;;
down-client)
echo "Deleting interface ${VTI_IFACE}"
ip tunnel del "${VTI_IFACE}"
echo "Deleting route for ${PLUTO_PEER}"
ip route del "${PLUTO_PEER}"
;;
esac
protonvpn-check
* * * * * root /usr/sbin/ipsec statusall | grep '0 up' && /usr/sbin/ipsec up protonvpn
protonvpn.ipsec.conf
I made some slight updates to protonvpn.ipsec.conf. I changed reauth to yes in order to prevent some annoying glitches where the tunnel would go down until manually restarted (even despite the cronjob). Set the right to whatever server you want to use. You can also list several servers seperated by commas.
conn %default
keyexchange = ikev2
type = tunnel
conn protonvpn
dpddelay = 30s # check peer liveness every 30s if there's no traffic
dpdtimeout = 90s # peer is considered dead after 90s, re-establish IKE_SA
dpdaction = restart
reauth = yes # ProtonVPN sets IKE_SA lifetime, see notes
auto = start # can't use auto=route, see notes
leftsourceip = %config4
leftsubnet = 0.0.0.0/0
leftupdown = /config/ipsec-updown.sh
leftauth = eap-mschapv2
eap_identity = abcDEFghiJKLmnoPQR
right = us.protonvpn.com
rightid = %any
rightsubnet = 0.0.0.0/0
rightauth = pubkey
protonvpn.ipsec.secrets
For protonvpn.ipsec.secrets, grab your credentials from the protonVPN website, and substitute them for the two asdfs below. Leave the spaces, colon, and EAP
unameasdf : EAP passwordasdf
Automation
Now that I have all my files on the local machine, I made a script to automatically place them at the correct places on the USG.
push_VPN_VLAN_
#!/bin/bash
# CREATED BY NISB 15 APRIL 2022
#
# This script is designed to automatically put all fo the correct files in place on
# a USG Pro 4 and UniFi Cloud Key to allow for selectively routing one already
# created VLAN
#
# First thing you need to do is go in and make sure the EAP identitites and
# config.json are appropriatley configured so that they can be coppied over
###
# VARIABLES
###
usg_uname = "XXX"
usg_ip = "10.0.0.1"
uck_uname = "XXX"
uck_ip = "10.0.0.11"
###
# END VARIABLES
###
usg = "$usg_uname@$usg_ip"
uck = "$uck_uname@$uck_ip"
# First lets pull down the latest protonVPN.der
curl -fsSL -O https://protonvpn.com/download/ProtonVPN_ike_root.der
### USG ###
echo "use the USG password for $usg_uname for the next 5 SCPs"
# Upload all of our files to the USG
scp charon.conf "$usg:/etc/strongswan.d/"
scp ipsec-updown.sh protonvpn.ipsec.conf "$usg:/config/"
scp protonvpn-check "$usg:/etc/cron.d/"
scp protonvpn.ipsec.secrets "$usg:/config/auth/"
scp ProtonVPN_ike_root.der "$usg:/etc/ipsec.d/cacerts/"
# Go in to the usg and modify the files that need executable permisions
ssh $usg "chmod +x /config/ipsec-updown.sh; chmod +x /etc/cron.d/protonvpn-check"
### UCK ###
echo "use the UCK password for $uck_uname for the next SSH and SCP"
# Make the directory for our config to sit in
ssh $uck "mkdir -p /srv/unifi/data/sites/default/"
# Put the file in place
scp config.gateway.json "$ucc:/srv/unifi/data/sites/default/"
So you will have to input the passwords for each of the scp and ssh calls, but I did not want to get into dependencies with installing sshpass (plus this is not great for security)
Conclusion
This method should also work for any VPN provider who allows IKEv2, can be adapted for OpenVPN configurations, and potentially could be exported out to more UniFi products (untested).
No comments:
Post a Comment