[How To] Protect Your Home Network with Mullvad VPN & OPNsense

In this post, we'll walk through how to connect an OPNsense firewall to Mullvad's wireguard VPN. This can be used in various deployments to help protect your home network traffic.

As a quick note, this post is not sponsored by Mullvad - I just happen to really like their service & appreciate their approach to privacy and security. In fact, they currently don't offer any incentives or paid promotions of their product. More info on their current policy can be found here


Creating an Account

Mullvad is unique in that they don't really require any sign up to get started with their service. Instead, when you sign up for new service, they randomly generate an account number. That's it. They don't require any additional information from you.

So first we can hit their new account page here - then click to generate a new account number.

create-account

Keep this account number somewhere safe. Mullvad has very limited ability to help you recover the account number.

Once we have that, we can click the button to Add time to our account.

Mullvad doesn't support any recurring subscriptions, so that they are able to keep less data about their customers. Instead, we just add time increments in months for how long we would like to use the service. This can be anywhere from a single month up to a year.

In terms of payments, they do offer a number of options, including the usual PayPal/credit cards - or if you're truly concerned about privacy, they accept a few cryptocurrencies or you can actually mail them a cash payment as well.

After a payment has been made & time added to the account, we can quickly jump into the configuration side of things.

Config: Mullvad Side

First we'll take a quick look at the configuration required on the Mullvad VPN side of things.

On the left side of the account page, we'll click on WireGuard Configuration.

First we'll select Linux as our platform (In this context, what is selected here isn't really important) - and then click the button to generate WireGuard keys:

generate-keys

Note: Alternatively, we could have generated our WireGuard keys on our OPNsense firewall - then applied them here. It's up to you on which method you prefer!

Next, we'll take a look at which server(s) we would like to connect to.

select-server

When selecting a server, we have the option to pick our desired country & location - as well as picking a specific server to connect to if we choose. There is also an option to select all servers - in which case the config generator will create a WireGuard configuration file for each server.

For the purpose of this walk through, we'll keep things simple & only select a single server. So in the screenshot above, I've selected us-qas-wg-004.

We'll also take a quick look at the advanced options, which give us a little flexibility if we need it. For most people, these additional options will not be necessary to change or modify.

We can enable Multihop functionality, so long as we only selected a single server to connect to. So if you selected the All Servers option, this won't be available. This allows us to specify an entry & exit server for our VPN. In other words, our device would directly connect to the entry server we select - then Mullvad would tunnel our traffic across their network to the exit server, where our traffic would be decrypted & forwarded out to the internet. Depending on your privacy & security desires, this is a really nice option to have the ability to enable.

Next, we have options on what type of connection we would like & which traffic to forward.

Server connection protocol will specify whether we are using IPv4 or IPv6 between our device & our VPN server. Most likely you'll want to leave this at IPv4, unless you have an IPv6-only internet connection (or if you just would prefer to use IPv6 anyways!).

Tunnel traffic is how we can specify whether we would like IPv4 or IPv6 client-side traffic to be forwarded over the VPN. So this would depend on whether the clients on our network have IPv4 vs IPv6 connectivity, or both - and whether we prefer to only forward certain types of traffic over the VPN. I'll be leaving this setting as the default: Both.

Next we can specify a custom port if we would like. By default, wireguard will use UDP port 51820 - and we probably won't need to change this unless the port is being blocked upstream.

Lastly, we also have the option to enable content blocking across the VPN. Mullvad accomplishes content filtering through DNS-level blocking - and when we finish generating a configuration file, the file will include DNS servers to use. This is okay to use if you were connecting a single client to their VPN service. However, if you're using a router or device like OPNsense, you would need to update the DNS on all clients on your network to make this work. This is possible by updating DHCP on our router with the new DNS server address - or configuring a DNS rewrite. We won't get into either of those in this post - so for now I will leave the content blocking options unchecked.

Once we're good with our configuration - we can click the Download File button. We'll get a standard WireGuard config file, that looks like this:

wireguard-config

At this point, we're good to move onto the next part!

Config: OPNsense Side

Okay, so now that we have everything ready to go on the Mullvad side - we can configure our OPNsense device.

Make sure you already have WireGuard installed on OPNsense. This can be done by navigating to System > Firmware > Plugins then searching for wireguard & clicking the install button.

Next, we'll enable Wireguard by navigating to VPN > Wireguard and checking the box to Enable WireGuard, then Apply.

enable-wireguard

Then we'll hop over to the Endpoints tab & configure our Mullvad VPN peer.

For this part of the configuration, we'll just copy our public key, allowed IPs, endpoint address, and endpoint port from our Mullvad config file. In the screenshot below, I also named my endpoint with the specific Mullvad server I'll be connecting to:

mullvad-peer-config-1

Then we can click Save and Apply.

If you wanted multiple Mullvad servers configured, just create a new endpoint for each one. Then, make sure that you select all of the Mullvad peers on the next step below.

After that, we can move over to the Local tab to define our OPNsense tunnel configuration.

Click the button to add a new peer, then we'll fill in our private key and tunnel address(es) from the Mullvad config file. Under Peers, we'll also select our Mullvad VPN peer that we configured just a moment ago:

opnsense-local-1

UPDATE: Looks like with a recent OPNsense update, they now require you to enter both the WireGuard private & public key into the local config (shown above). In the the screenshot, I only show entering the private key - since this was all that was required at the time.
Two ways to get your public key:

  1. Log into Mullvad & check the "Devices" tab under "Account Management". This will show your device public key (They don't keep your private key after generating it for you, only the public).
  2. If you have wireguard installed somewhere else, you can use the "wg pubkey" command to derive a public key from your private key. Command: echo <private_key> | wg pubkey

Note: By default with the configuration we've applied so far, this VPN will forward ALL traffic on our network to Mullvad. If we would prefer to selectively choose which traffic to send over the VPN, we can check the box for Disable Routes - then use policy routing to forward specific things to Mullvad.
We'll take a look at how to do this later in the post - but for now just be aware that our current configuration will forward ALL traffic.

Okay, with that all done we can click Apply and Save here as well.

With any luck, we can check the Status tab & see that there is data being transmitted & successful WireGuard handshakes:

wg-status

However, before our clients traffic can be forwarded over the VPN, we'll need to create a firewall rule to permit traffic & a NAT rule to translate our client addresses to our Mullvad IP.

We'll navigate to Firewall > Rules > WireGuard (Group). Then we'll click to create a new rule.

Within this new rule, I'll update Direction to Out and change TCP/IP Version to IPv4+IPv6. I'll leave the source as Any:

wgrule1

We can also leave the Destination as Any, but I'll update the rule to enable logging:

wgrule2

This rule will allow any clients behind our OPNsense firewall to reach anything on the internet.

Next, we'll have to create a NAT rule. This ensures that our client addresses on our network get appropriately translated to the tunnel IP address that Mullvad has assigned us.

We'll navigate to Firewall > NAT > Outbound. By default, OPNsense will be set to Automatic outbound NAT rule generation. We'll need to update this to Hybrid outbound NAT rule generation to allow custom NAT rules. Then we can click Save and Apply.

nat-before

Next, we'll create a new Manual NAT rule, where we'll update our Interface to WireGuard (Group):

nat-rule1

Then we'll make sure our Translation / target is set to Interface Address - and again, I'll enable logging:

nat-rule2

After we click save, we should have a NAT rule that looks like this:

nat-after

At this point, we should be good to test our clients!

Testing

Of course, it's easy enough to use one of our clients to check that we still have internet access - but how can we be sure that they're using the VPN?

The easiest way might be to check the mullvad.net, where they do have a quick validation at the top of the page:

mullvad-test

So according to Mullvad, it looks like we're connected & they even show which server we're connecting from.

We can also double check using a traceroute or tracepath command:

tracepath

In this case, we can see that our traffic to Google hits our OPNsense gateway, then the Mullvad VPN gateway followed by another external address owned by Mullvad.

So based on some quick testing, it looks like we're all good!

Policy Routing

So in the above walkthrough, we configured a Mullvad VPN from our OPNsense firewall - but it is forwarding ALL of our network clients over the VPN. What about if we only wanted certain clients to use the VPN? Or all clients to use it, but only for certain destinations?

We can accomplish this through policy routing.

So the first thing we'll do is go back to our WireGuard config, then under the Local tab. We'll edit our configuration here, and check the box for Disable Routes.

disable-routes

By default, OPNsense / WireGuard will install routes for any IPs listed in the AllowedIPs field for each peer. In our set up, we configured 0.0.0.0/0 - which matches all traffic. By checking the box for Disable Routes, we prevent OPNsense from installing that default route - and instead we can manually specify our own.

If you're curious to double check this, you can try hitting Mullvad's website after changing this setting - and it should show that you're no longer connected.

Then, we'll need to set up our WireGuard configuration to use a dedicated, named interface so that we can create a static gateway.

We'll head to Interfaces > Assignments - and create a new interface. From the drop-down, we'll select our WireGuard interface - in my case this was wg1. Then we can assign it a name:

create-interface

Then click the + icon to add, and Save.

Then we can navigate to the interface name under the Interfaces menu - and enable the new interface:

enable-interface

Next we can create a gateway to route traffic through. Navigate to System > Gateways > Single.

Create a new gateway & give it a name. Then we'll enter our Mullvad VPN gateway IP address, which in my case was 10.64.0.1. How did we find this? Well earlier when we tested our VPN connectivity - we performed a tracepath. In the output of this tracepath, our second hop (the one right after our OPNsense firewall) would be the Mullvad VPN gateway. So this is the address we'll use for our gateway here:

mullvad-gateway

Then clic Save and Apply.

Note: You may get an error here, like "The gateway address "10.64.0.1" does not lie within one of the chosen interface's IPv4 subnets."
To resolve this, we'll temporarily change our interface address. Navigate to the WireGuard local config, and re-enter your tunnel address excluding the "/32". For example, if your tunnel IP was 10.10.10.10/32, change this to just 10.10.10.10
You should be able to set the gateway address now. Be sure to change your tunnel IP back afterwards!

Next, we'll create a firewall rule for each set of sources or destinations we would like to manipulate.

So we can navigate to Firewall > Rules > Floating (or select a specific interface for clients, like LAN).

In here, we'll set whichever parameters we would like to match. So for this example, let's say I have a client PC at 10.100.100.10 and I only want traffic to 8.8.8.8 to use Mullvad. All other traffic can use the normal internet connection & not use the VPN.

In that case, I'll set my source address to 10.100.100.10/32 and my destination to 8.8.8.8/32:

policy-route1

Then all we need to do is update our Gateway to use the Mullvad gateway we just created:

policy-route2

Now, if we hop over to our client PC - Mullvad's website will say that we're not connected to the VPN. However, we can check that traffic to 8.8.8.8 is actually being sent through Mullvad using tracepath again:

split-routing

In the screenshot above, I also included a tracepath to 8.8.4.4, just to show that it is going through my normal internet connection & not Mullvad.

This was just one example, but you could easily create multiple firewall rules for different sources and/or destinations to control where traffic is sent. Aliases can also be used to group sources or destinations, so that multiple can be added to a single firewall rule.


Okay - I think that's about all I wanted to cover in this post.

Hope it is helpful! Feel free to leave a comment below - or follow me on YouTube 😊