Overlay Network Configuration

2022-06-16

The next step in my nerdy networking hobby was to expand my [server configuration]({{< ref “/posts/my-server-structure” >}}) to allow remote access to my home network. Naturally, this involved setting up a VPN and some way to access it from outside the network, among other things.

The motivation is to be able to access my NAS for backups and Navidrome for playing my music collection remotely. In future, I’ll most likely include more services.

I decided to go a step further and set up a kind of an overlay network. That way, all my devices will be able to talk to each other and to any device at home, regardless of where they are. In addition, I can grant access to my family members’ devices so that they can use any service the network provides, while being separable from my devices by putting them on different subnets.

Connecting to the home network

The first thing that needs to be addressed is how to actually access the home network. There are a few ways to achieve that:

The gateway is, unsurprisingly, a Raspberry Pi. Normally, one would install WireGuard on their router, but I’m running a MikroTik device, and their RouterOS won’t support WireGuard until version 7 (which isn’t exactly stable at the time of writing).

The Raspberry must be configured to act as a router. This means that IP forwarding must be enabled, and the correct firewall rules must be in place. This is similar to the server configuration described below. Furthermore, a keepalive setting is necessary to keep the NAT mapping open.

In addition, the AllowedIPs section of all peers’ WireGuard configs must be set to allow access to the entire private network range.

Implementation

The network configuration is implemented as a systemd service running on my server. The service controls a shell script that sets up the network components based on a configuration file described below. The script really only accepts two commands: one to set up the network, the other to tear it down.

Because the script only sets up a couple of system options, a service isn’t strictly necessary. However, it provides me with more control over its runtime, allowing me to easily launch it on startup or stop when necessary. Such kind of service that only launches a particular program is known as a oneshot service.

Network structure

The structure of the entire network is configured in a JSON file that contains the IPv4 and IPv6 ranges of all subnets, IP addresses of the gateway, and local DNS entries. The control script uses jq to extract the individual entries.

Schematically, the network looks like this:

                     +-----------------+
        +--------+ +-+ Internal subnet |
        |        +-+ +-----------------+
        | Server |
        |        +-+ +-----------------+
        +----+---+ +-+ Isolated subnet |
             |       +-----------------+
             |
     +-------+------+
     | Home gateway |
     +-------+------+
             |      
      +------+------+
      | Home subnet |
      +-------------+

Network configuration components

The remainder of this posts describes what needs to be done to setup the network control on the server. The steps are performed in this order when starting, and in the reverse order when stopping the network service.

Nftables entries

First, nftables must be configured in the following way (in addition to the already configured rules; input to the server is already allowed from the WireGuard interface).

I tried a few different ways, and eventually settled on pre-configuring empty sets of IP addresses and address ranges, and the above forwarding rules. The network control script then simply fills the sets with the correct entries when starting the service, and flushes them when stopping it.

Local DNS

The next step is to configure the Unbound resolver to resolve local domains to the correct IP addresses. This is done by creating a file with the local DNS records inside the /etc/unbound/unbound.conf.d directory, whose content is set to be automatically included in my Unbound config.

There’s an unfortunate situation around TLS certificates. Modern browsers sound all kinds of alarms when they access a domain with no certificate configured, regardless of whether the IP address is in an RFC 1918-reserved range or not.

Of course, a TLS certificate for a site only accessible by trusted computers in a home network or through a VPN is hardly necessary, but tell that to the browsers. Naturally, it’s not possible to obtain a Let’s Encrypt certificate for a private IP address. Using a self-signed certificate is of no help, because then the browsers just complain about it being untrusted.

I could reserve a special subdomain of my domain for the home network, get a Let’s Encrypt certificate for that, and enjoy my complaint-free web browsing experience. My domain is a bit too long as it is, though, and going through this hassle only to make browsers happy is not worth it.

In the end, I simply didn’t configure any certificate at all and I hope that the HTTPS everywhere Firefox mode won’t become mandatory. In turn, this allows me to use the reserved .home.arpa domain. I saw that people often use the .local domain, but that’s actually reserved for mDNS.

IP forwarding

This step simply enables IPv4 and IPv6 forwarding by setting the net.ipv4.ip_forward and net.ipv6.conf.all.forwarding kernel parameters using the sysctl utility.

IP routes

Finally, IP routes are set so that all packets for the home subnet are routed via the home gateway.