OPNsense Forum

English Forums => Tutorials and FAQs => Topic started by: balrog on March 11, 2022, 08:36:06 PM

Title: [Tutorial] Selective Routing to Private-VPN (via Wireguard)
Post by: balrog on March 11, 2022, 08:36:06 PM
Introduction

In the following I would like to show how I have set up "Selective Routing" via Wireguard. Primarily to access websites with region lock or to hide my real public IP.
For that, I have an Azure VM, which is located in the appropriate country. This VM can also be from another provider.

This manual is basically based on: https://docs.opnsense.org/manual/how-tos/wireguard-selective-routing.html (https://docs.opnsense.org/manual/how-tos/wireguard-selective-routing.html)

Tested and working on OPNsense 22.1.2_1



Changelog
Quote

    17.03.2022:
    • Changed the Gateway-Address from local WG-Address to the remote WG-Address (Local WG-Gateway), as described in the official manual.
    • Fixed some typos an formatting


Requirements


Configuration: OPNsense

Create Endpoint
Go to: VPN -> Wireguard -> Endpoints -> + (create new)

(https://i.postimg.cc/2yv45Md8/ZZEmd-Zq-NVP.png)
Click on "Save" and "Apply"

Create Local Configuration

(https://i.postimg.cc/XJzL0yS0/Bebq-VJKg3-K.png)
Click on "Save" and "Apply"

Click on "Edit" (Pencil) and copy the new generated Public Key to a .txt-File or similar.

Create new Interface
Go to: Interfaces -> Assignments

Go to: Interfaces -> [NEW-WG-INTERFACE]
(https://i.postimg.cc/pV6HT0YD/xh-Wvtxz0-Hf.png)


Create a Gateway
Go to: System -> Gateways -> Single
(https://i.postimg.cc/TPtRw1pP/s6yt-BGMYl-K.png)
Click on "Save" and "Apply changes"

Create Aliases (Destination URLs, Networks)

Go to: Firewall -> Aliases -> Add (create new alias)

In my case, certain services are loaded from my accessed website via a CDN, which is why I also need to enter all the subnets used by the CDN. If this is not the case for you, you can simply create a URL alias and skip creating a network alias.

URL:
(https://i.postimg.cc/43qGty4Z/suy-Fgy-ZCv5.png)
Click on "Save" and "Apply"

Networks:
(https://i.postimg.cc/FHhCW4CS/4n3z3k907t.png)
Click on "Save" and "Apply"

Firewall Rules

In my case the complete internal network should run through my wireguard endpoint when accessing my "region block website".
If you don't want to do that over the whole network, you can also create an alias for the affected IPs/hosts and specify them as "source" later. in the Rule-Creation.

Go to: Firewall -> Rules -> [LAN-Interface] -> Add (create new rule)
(https://i.postimg.cc/G3QKHTYG/rk-EACeht-F7-1.png)
Click on "Save" and "Apply"

If you have also created a Network-Alias like me, this just created firewall rule needs to be copied and the destination (4) needs to be adjusted to this alias. After that "Save" and "Apply" again.

NAT Configuration
The NAT is only needed if you also want to access the endpoint host itself!

Go to: Firewall -> NAT -> Outbound

Select "Hybrid NAT..."
(https://i.postimg.cc/g0BwJQM5/0bgy-E4-XFQu.png)

Add a new "Manual rule":
(https://i.postimg.cc/9Mny6r5D/p-Ni-FIVve67.png)
Click on "Save" and "Apply"

Now we are basically done with the configuration on the OPNsense side. We only have to enter the PublicKey of the Wireguard endpoint later.


Configure: Rocky Linux / Wireguard-Endpoint

At this point you can use any Linux/BSD distribution as long as iptables and wireguard are available for it.
However, only the procedure on RockyLinux is described here.

Installation prerequisites / Wireguard

Install Epel-Release:
dnf install epel-release -y

Install Wireguard:
dnf install kmod-wireguard wireguard-tools -y

Create WireGuard Configuration Directory
mkdir /etc/wireguard

Generate WireGuard Private/Public Keys

Change Permissions:
umask 077

Generate and write a new private and a public key to a file:
wg genkey | tee /etc/wireguard/wireguard.key | wg pubkey > /etc/wireguard/wireguard.pub.key

Copy the created private key and write it temporary somewhere down:
cat /etc/wireguard/wireguard.key

Create a new Wireguard-Configfile:
vi /etc/wireguard/wg0.conf

Paste the following lines to this file:
[Interface]
ListenPort = 55555
PrivateKey = [PrivateKey from the Endpoint/this Machine]
Address = 10.0.8.1/32
PreUp = iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PreUp = iptables -A FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
PostDown = iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu

[Peer]
PublicKey = [PublicKey from OPNsense-Wireguard]
AllowedIPs = 10.0.8.2/32


Note that on my interface eth0 the gateway is defined. This may be different for you. Change it accordingly in the lines "PreUp" and "PostDown".

Save the File (vi = :wq)

Enable IP-Forwarding
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf

sysctl -p


Copy the public key to OPNsense

Copy the created public key and write it temporary somewhere down:
cat /etc/wireguard/wireguard.pub.key

Go back to your OPNsense WebGUI
Go to: VPN -> Wireguard -> Endpoints -> Edit your prevoiusly created config

(https://i.postimg.cc/1529spMK/x-Nvd-Nr-LNJY.png)
Click on "Save" and "Apply"

Start Wireguard on the Endpoint
Go back to your Endpoint and start your Wireguard-Tunnel:
wg-quick up wg0

If your tunnel is already running, do:
wg-quick down wg0
wg-quick up wg0

You can check if the tunnel is up and running:
wg

Create a service
systemctl start wg-quick@wg0
systemctl enable wg-quick@wg0


Summary
Now we are ready and you can test/use your configuration.
Title: Re: [Tutorial] Selective Routing to Private-VPN (via Wireguard)
Post by: wrobelda on March 16, 2022, 07:19:38 PM
In "Create a Gateway" section, step "4. Enter the Tunnel Address from your Local Wireguard Configuration" is inconsistent with https://docs.opnsense.org/manual/how-tos/wireguard-selective-routing.html#step-6-create-a-gateway.
Title: Re: [Tutorial] Selective Routing to Private-VPN (via Wireguard)
Post by: balrog on March 16, 2022, 10:07:38 PM
Hi wrobelda

Thanks for the hint! I have adjusted the section in the tutorial. Nevertheless, it also works when the tunnel address is entered.
Title: Re: [Tutorial] Selective Routing to Private-VPN (via Wireguard)
Post by: LennieSmalls23 on October 02, 2024, 02:29:40 AM
Thanks you balrog for this, this tut was reassuring for me to read as I had followed the official guide which did not suffice my needs like yours.

To note for anyone else i had to move the rule to the top as i already had an any out rule on the lan which was taking precedent.

I have been using opnsense for a few months now and the journey has been great so far, this has just topped it off.

Thanks!