WireGuard VPN: Secure Access to the Office Network
Why a VPN instead of port forwarding, keys and peers, AllowedIPs as routing table and filter, split and full tunnel, with the configuration in OPNsense.
People who work away from the office often need its services: the management software, the shared folders on the NAS, the interface of a device. The most common shortcut is to open a port on the firewall and forward it to the server, but that makes the service reachable from the whole Internet. A VPN solves the problem more cleanly: the remote device enters an encrypted, authenticated tunnel and from there reaches the internal network as if it were in the office. Let us see how WireGuard, a modern and minimal VPN protocol, works, and how to configure it in OPNsense.
Why a VPN instead of port forwarding
With port forwarding, the firewall passes to the internal server every connection that arrives on a given port, from any address. From that moment on, the service's login page, its passwords and any vulnerabilities it may have are exposed to the automated scans that sweep the Internet non-stop.
With a VPN, instead, a single entry point is exposed, one designed precisely to face the Internet: the VPN itself. The internal services stay closed and, to reach them, the device must first prove that it holds an authorised cryptographic key. WireGuard adds a further advantage: it does not answer packets that are not authenticated with a valid key, so to anyone scanning the Internet its port looks closed.
Two ways to use a VPN: remote access and site-to-site
- Remote access (road warrior): a single device, a laptop or a smartphone, connects to the office network from anywhere. Its address changes all the time and it often sits behind the NAT of a hotel or a mobile network.
- Site-to-site VPN: two firewalls permanently connect the networks of two sites. Users install nothing: the firewalls route the traffic for the other site into the tunnel.
WireGuard handles both cases with the same concept, the peer: the other end of the tunnel, which may be a laptop or the firewall of another site.
WireGuard in brief: keys, peers and one UDP port
Each end has a key pair based on the Curve25519 elliptic curve: the private key stays on the device and is never transmitted, the public key is handed to the other side. The principle is similar to SSH keys: there are no certificates and no usernames, and a peer is identified precisely by its public key.
The configuration of a WireGuard interface contains its own private key, the listen port and the address inside the tunnel. For each peer you specify the public key, the AllowedIPs and, when needed, the endpoint, that is the address and port at which it can be reached on the Internet.
Cryptography is not negotiated: WireGuard always uses the same set of modern algorithms, based on the Noise framework, with Curve25519 for key exchange, ChaCha20-Poly1305 for authenticated encryption and BLAKE2s for hashing. There are no suites to choose and therefore no weak combinations configured by mistake. The handshake is lightweight: two messages are enough to establish the session keys, which are renewed automatically every few minutes.
All traffic travels over a single UDP port. The usual value, used in the documentation examples, is 51820, but the port can be chosen freely. The original packet is encrypted and encapsulated in a UDP packet addressed to the peer's endpoint; encapsulation adds 60 bytes with IPv4 and 80 with IPv6, which is why the WireGuard interface normally uses an MTU of 1420 bytes.
AllowedIPs: routing table and filter
The heart of WireGuard is cryptokey routing: each peer is associated with a public key and a list of networks, the AllowedIPs. The same list does two jobs.
- Outbound, it is a routing table. When a packet enters the WireGuard interface, its destination is compared with the peers' AllowedIPs: the packet is encrypted with the keys of the matching peer and sent to its endpoint. If no peer matches, the packet is dropped.
- Inbound, it is a filter. A packet received and decrypted with a peer's keys is accepted only if its source address falls within that peer's AllowedIPs. An authorised device therefore cannot pose as another one.
On the firewall, a remote device usually gets a single address, with a /32 prefix: its address in the tunnel. A given prefix belongs to only one peer of the interface, and it is precisely this binding between address and key that makes the source of packets trustworthy.
Split tunnel and full tunnel
On the remote device, the AllowedIPs of the "office" peer decide which traffic enters the tunnel; the WireGuard applications create the routes accordingly.
- Split tunnel: only the office networks go through the tunnel, the rest of the traffic goes straight to the Internet. It uses less of the site's bandwidth, but the device's browsing is not protected by the office firewall.
- Full tunnel: with
0.0.0.0/0and::/0all traffic passes through the office and leaves through its connection. It is useful on Wi-Fi networks you do not trust, but it needs upload bandwidth at the site and outbound NAT for the tunnel network.
| Scenario | AllowedIPs on the remote device | AllowedIPs of the peer on the firewall |
|---|---|---|
| Remote access, split tunnel | 10.10.10.0/24, 192.168.10.0/24 | 10.10.10.2/32 |
| Remote access, full tunnel | 0.0.0.0/0, ::/0 | 10.10.10.2/32 |
| Site-to-site (site B firewall) | 10.10.10.1/32, 192.168.10.0/24 | 10.10.10.4/32, 192.168.20.0/24 |
In the site-to-site case, the middle column describes the "office" peer configured on the firewall of site B, whose LAN is 192.168.20.0/24; the right-hand column describes the "site B" peer on the office firewall.
Behind NAT: endpoint and PersistentKeepalive
The office firewall has a fixed public address and port, so on the laptop the endpoint is written once and for all: 203.0.113.25:51820. On the firewall, instead, a remote device's endpoint is not needed: WireGuard learns it from the latest authenticated packet received. If the laptop moves from the hotel Wi-Fi to the mobile network, the tunnel keeps working.
The NAT of the hotel or of the home router, however, forgets the UDP translation after a period of inactivity: until the laptop transmits again, the office cannot reach it. The PersistentKeepalive option makes the device send a small authenticated packet at regular intervals; the WireGuard documentation gives 25 seconds as a sensible value. It is needed only on the side behind NAT and is disabled by default.
The office firewall must in any case be reachable from outside: if its WAN sits behind the provider's CGNAT, incoming connections do not arrive, just as with port forwarding.
In OPNsense: instance, peers, interface and rules
In recent versions of OPNsense WireGuard is built into the system, whereas in the past it was a plugin to install; it is configured under VPN › WireGuard. The names of tabs and fields may vary slightly from one version to another: it is best to follow the official documentation for the version in use. For remote access, the steps are these.
- Instances: create the local instance, that is the firewall's WireGuard interface, with its key pair, listen port 51820 and the address in the tunnel, for example 10.10.10.1/24.
- Peers: create one peer per device, with its public key, AllowedIPs 10.10.10.2/32 and the instance it belongs to; optionally a pre-shared key. Then enable the service and apply the changes.
- Rule on the WAN: in Firewall › Rules › WAN, a Pass rule for the UDP protocol, with the WAN address as destination and port 51820. Without this rule the handshake does not arrive.
- Rules for tunnel traffic: OPNsense gathers the WireGuard interfaces in a group with its own rules tab. Peer traffic is blocked until a rule allows it: it is best to permit only what is needed, for example TCP 443 to the server 192.168.10.20.
- Interface assignment: in Interfaces › Assignments you can assign the instance's WireGuard device as a real interface and enable it. This is useful for writing dedicated rules, for outbound NAT and, in site-to-site VPNs, for managing gateways and routes.
On the laptop the configuration is the other side of the coin: its own private key and the address 10.10.10.2/32; for the "office" peer, the firewall's public key, the endpoint, the chosen AllowedIPs and, if needed, PersistentKeepalive = 25. For a full tunnel, check that outbound NAT, in Firewall › NAT › Outbound, also translates the 10.10.10.0/24 network.
Networks that must not overlap
A frequent mistake comes from addressing. If the office uses 192.168.1.0/24 and the home or hotel network also uses 192.168.1.0/24, the remote device no longer knows which of the two to reach. It is wise to choose uncommon subnets for the office and the tunnel, for example within 10.0.0.0/8 or 172.16.0.0/12, and to record them in an addressing plan. In site-to-site VPNs the rule is absolute: the LANs of the sites and the tunnel network must all be different.
DNS in the VPN
To reach internal servers by name, the remote device must use the office resolver. In the client configuration you set as DNS, for example, the firewall's address in the tunnel or on the LAN. Two checks: the resolver must accept queries coming from the tunnel network, and the rules of the WireGuard interface must allow port 53 to it.
Good practices
- One key per device: never the same configuration on several devices or for several people. Each peer has a name that identifies the device and its owner.
- Immediate revocation: if a laptop is lost or a colleague leaves the company, delete or disable their peer, and its key is no longer accepted.
- Private keys kept safe: generate them on the device when possible and never send them by email or message.
- Optional pre-shared key: an extra symmetric key, different for each peer, combined with the Curve25519 exchange; the WireGuard whitepaper proposes it as an additional defence against future attacks based on quantum computers.
- Least privilege: AllowedIPs reduced to what is needed, tunnel rules that allow only the intended services, logging enabled, and firewall and clients always up to date.
Conclusions
A WireGuard VPN replaces the open ports of individual services with a single UDP entrance, protected by cryptographic keys and silent towards anyone who does not hold them. The mechanism can be summed up in a few words: a key pair for each end, a peer for each device or site, and AllowedIPs acting as both routing table and filter. In OPNsense all it takes is an instance, the peers, a rule on the WAN and targeted rules on the tunnel interface. With networks that do not overlap, a reachable internal DNS and one key per device, access to the office network becomes simple and verifiable.
The video
Further reading
- Book: LOCAL AREA NETWORKS (LAN) — Volume 5: LAN Security and Integration (English edition)
- Book: LOCAL AREA NETWORKS (LAN) — Volume 4: Routing, NAT, and Internet Access (English edition)
- Book: OPNsense® in Practice — Volume 1: The Foundations of a Professional Journey (English edition)
- Video course: Capire i Firewall con OPNsense (video course in Italian)
- Related article: NAT and Port Forwarding: How a Private Network Talks to the Internet
- Related article: How a Firewall Rule Works
- Specialization: Firewalls & Network Security
Technical sources
- J. A. Donenfeld — «WireGuard: Next Generation Kernel Network Tunnel» (whitepaper) — Cryptokey routing and AllowedIPs, a one-round-trip handshake based on the Noise framework, Curve25519, ChaCha20-Poly1305 and BLAKE2s, session key renewal, no reply to unauthenticated packets, optional pre-shared key, endpoint roaming.
- WireGuard — documentazione ufficiale (Quick Start, Protocol & Cryptography) e pagina di manuale wg(8) — Public and private keys, port 51820 in the examples, AllowedIPs, Endpoint, PresharedKey and PersistentKeepalive, with 25 seconds given as a value that suits most NATs and firewalls.
- WireGuard — pagina di manuale wg-quick(8) — Interface Address and DNS, routes created from AllowedIPs, full tunnel with 0.0.0.0/0 and ::/0, default MTU of 1420 bytes.
- Documentazione ufficiale di OPNsense — VPN › WireGuard (configurazione e Road Warrior Setup) — Instances and Peers, the WAN rule for the UDP port, rules on the WireGuard interface group, interface assignment and outbound NAT for the tunnel network.
- RFC 1918 — Private addresses for internal networks and for the tunnel network (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16).
- RFC 5737 — IPv4 address blocks reserved for documentation (203.0.113.0/24 and 198.51.100.0/24, used in the examples).
Standards and official documentation available from the respective organisations.