Skip to content
Visual explainer 2h 30m to learn 4 steps

VPNs: IPsec, WireGuard & TLS VPNs

Explain site-to-site and remote-access VPNs and set up a WireGuard tunnel.

By Invicly Tech, , in Computer Networking. About 11 minutes to read.

Topic 41 of 51

In short

A VPN builds an encrypted tunnel across an untrusted network so traffic travels as if on a private link. Site-to-site VPNs join whole networks, often with IPsec, while remote-access VPNs connect individual users. WireGuard is a modern, minimal protocol that is fast and easy to configure.

Explained simply

A VPN (virtual private network) is a private, encrypted tunnel through a public network like the internet. Packets go into the tunnel at one end, travel scrambled so nobody in the middle can read or change them, and come out at the other end as if they had crossed a private cable.

There are two main uses. A site-to-site VPN links two whole networks, for example the head office and a branch office, through two gateways that keep the tunnel up all the time. A remote-access VPN connects one person's laptop or phone to the company network from home or a hotel.

Different VPN technologies do the same job in different ways. IPsec is the old, standard choice for site-to-site links between routers and firewalls. WireGuard is a newer, very simple and fast protocol. TLS VPNs, such as OpenVPN, use the same encryption as HTTPS websites and pass through most firewalls.

You also decide what goes through the tunnel. In a full tunnel, all traffic goes through the VPN, even when you watch videos. In a split tunnel, only traffic for company networks uses the VPN and the rest goes straight to the internet. Each choice has costs in speed and security.

Press Play on a diagram to walk through it one step at a time.

CompareCommon VPN technologies
IPsec
  • IKE negotiates keys
  • ESP encrypts packets
  • Standard for site-to-site
  • Complex configuration
WireGuard
  • Small codebase
  • Public-key peers
  • UDP, very fast
  • Simple config files
TLS VPN
  • Runs over TCP/UDP 443
  • Passes most firewalls
  • OpenVPN and vendor clients
  • Common for remote access
MapPorts and protocols to allow for each VPN
VPN traffic through a firewall
  • IPsec
    • UDP 500 for IKE
    • UDP 4500 for NAT traversal
    • IP protocol 50 for ESP
  • WireGuard
    • One UDP port you choose
    • 51820 by convention
  • OpenVPN
    • UDP 1194 by default
    • Often TCP 443 to pass strict networks
  • TLS or SSL VPN appliances
    • TCP 443
    • Sometimes UDP for a faster data channel

In depth

How IPsec builds a tunnel

IPsec has two parts. IKE (Internet Key Exchange, today IKEv2) runs on UDP port 500. The two gateways first do IKE_SA_INIT, a Diffie-Hellman exchange that agrees on keys without sending them, then IKE_AUTH, where each proves who it is with a pre-shared key or a certificate. The result is an IKE security association (SA) and at least one child SA that protects the actual traffic.

The traffic itself is carried by ESP (Encapsulating Security Payload), IP protocol number 50, which encrypts and authenticates each packet. AH (protocol 51) only authenticates and is rarely used because it breaks through NAT. When a NAT device sits between the peers, NAT traversal wraps ESP inside UDP port 4500. So a firewall in front of an IPsec gateway must allow UDP 500, UDP 4500 and usually protocol 50.

  • IKE: UDP 500; NAT traversal: UDP 4500
  • ESP: IP protocol 50; AH: IP protocol 51
  • Both sides must agree on ciphers and DH group
  • Child SAs are rekeyed regularly
SequenceIKEv2 setting up an IPsec tunnel
Branch gateway
HQ gateway
  1. 1IKE_SA_INIT: proposals, DH share, nonce
    Branch gateway to HQ gateway: IKE_SA_INIT: proposals, DH share, nonce
  2. 2IKE_SA_INIT: chosen proposal, DH share
    HQ gateway to Branch gateway: IKE_SA_INIT: chosen proposal, DH share
  3. 3IKE_AUTH: identity, auth, traffic selectors
    Branch gateway to HQ gateway: IKE_AUTH: identity, auth, traffic selectors
  4. 4IKE_AUTH: identity, auth, child SA ready
    HQ gateway to Branch gateway: IKE_AUTH: identity, auth, child SA ready
  5. 5ESP packets carry the real traffic
    Branch gateway to HQ gateway: ESP packets carry the real traffic

Tunnel mode, transport mode and overhead

In tunnel mode, IPsec takes the whole original packet, including its IP header, encrypts it, and puts it inside a new packet addressed from gateway to gateway. Hosts behind the gateways keep their private addresses, and an observer sees only the two gateway IPs. This is the normal mode for site-to-site VPNs. In transport mode, only the payload is protected and the original IP header stays; it is used for host-to-host protection, often together with another tunnel such as GRE.

Encapsulation adds headers, so less room is left for data. ESP in tunnel mode usually costs about 50 to 80 bytes depending on the cipher, and WireGuard adds 60 bytes over IPv4 (80 over IPv6). That is why tunnel interfaces use a smaller MTU, such as WireGuard's common default of 1420. If path MTU discovery is blocked, large packets silently disappear, so gateways often clamp the TCP MSS to fit.

LayersAn IPsec tunnel-mode packet
  1. 1
    New IP header

    Gateway A to gateway B, protocol 50

  2. 2
    ESP header

    SPI and sequence number

  3. 3
    Original IP header (encrypted)

    Private host to private host

  4. 4
    Original TCP or UDP and data (encrypted)

    The user's real traffic

  5. 5
    ESP trailer and ICV

    Padding and integrity check

WireGuard: keys, peers and AllowedIPs

WireGuard is built into the Linux kernel and also runs on Windows, macOS, iOS and Android. It uses one fixed set of modern algorithms (Curve25519 for key exchange, ChaCha20-Poly1305 for encryption) based on the Noise protocol framework, so there are no cipher options to get wrong. Each side has a key pair, made with wg genkey and wg pubkey, and knows its peers only by their public keys. It runs over UDP; 51820 is the conventional port.

The key idea is cryptokey routing. For each peer, AllowedIPs is both an outbound route (traffic to these ranges is sent to this peer) and an inbound filter (packets from this peer must have a source inside these ranges). WireGuard is silent: it does not answer unauthenticated packets, so scanners cannot even see it. A peer behind NAT should set PersistentKeepalive = 25 so the NAT mapping stays open and the server can reach it.

  • wg genkey | tee priv | wg pubkey > pub
  • wg-quick up wg0 reads /etc/wireguard/wg0.conf
  • wg show prints peers, endpoints and latest handshake
  • No handshake in the last 2-3 minutes on an active peer means trouble

Remote access, TLS VPNs and split tunnelling

Remote-access VPNs must work from hotels, cafes and mobile networks that may block unusual ports. TLS VPNs handle this well: OpenVPN uses UDP 1194 by default but can run on TCP 443, and many commercial clients use TLS on 443 so they look like normal HTTPS. Running a VPN over TCP has a cost: when a TCP connection inside a TCP tunnel loses packets, both layers retransmit, which can collapse throughput. Prefer UDP when the network allows it.

Full tunnel sends all traffic through the VPN (AllowedIPs = 0.0.0.0/0, ::/0 in WireGuard). The company can filter and log everything, and users on hostile Wi-Fi are protected, but all video and updates load the VPN gateway. Split tunnel routes only company ranges (for example 10.0.0.0/8) through the VPN; it is faster and cheaper, but the laptop sits on two networks at once, so the endpoint itself must be well protected. DNS must match: with split tunnel, internal names still need the internal DNS server.

CompareFull tunnel versus split tunnel
Full tunnel
  • All traffic via the VPN
  • Central filtering and logging
  • Protects users on public Wi-Fi
  • Heavier load on the VPN gateway
Split tunnel
  • Only company ranges via the VPN
  • Internet traffic goes direct
  • Faster, cheaper
  • Laptop must be well secured

Learn it in 4 short steps

About 40m each. Track them with a timer in your plan.

  1. 1
    Compare site-to-site and remote access~40m
  2. 2
    Explain IPsec IKE, ESP and tunnel mode~40m
  3. 3
    Set up a WireGuard tunnel~40m
  4. 4
    Explain split and full tunnelling~40m

Key points

  • Tunnel mode wraps the whole original packet inside a new one.
  • Split tunnelling sends only corporate traffic through the VPN.
  • Overlapping subnets on both sides break site-to-site routing.
  • Encapsulation overhead lowers the effective MTU.

Key terms

Tap a card to flip it. 0/4 known

Examples

text
# /etc/wireguard/wg0.conf on a laptop
[Interface]
PrivateKey = <laptop-private-key>
Address = 10.8.0.2/24

[Peer]
PublicKey = <server-public-key>
Endpoint = vpn.example.com:51820
AllowedIPs = 10.0.0.0/16       # split tunnel to office ranges
PersistentKeepalive = 25

Bring it up with: sudo wg-quick up wg0, then check with: sudo wg show

A WireGuard server for remote staff

The office server has public IP 203.0.113.10 and LAN 10.0.0.0/16. Step 1: generate a key pair on the server and on each laptop. Step 2: write the server config below, with one [Peer] per laptop and AllowedIPs set to that laptop's single tunnel address. Step 3: enable IP forwarding and allow UDP 51820 in the firewall. Step 4: wg-quick up wg0 and enable wg-quick@wg0 at boot. The laptop config (as in the explainer) points Endpoint at 203.0.113.10:51820.

text
# /etc/wireguard/wg0.conf on the server
[Interface]
Address = 10.8.0.1/24
ListenPort = 51820
PrivateKey = SERVER_PRIVATE_KEY

[Peer]
# alice-laptop
PublicKey = ALICE_PUBLIC_KEY
AllowedIPs = 10.8.0.2/32

# then: sudo sysctl -w net.ipv4.ip_forward=1
#       sudo systemctl enable --now wg-quick@wg0
#       sudo wg show

A site-to-site tunnel that will not come up

A new IPsec tunnel between HQ (10.1.0.0/16) and a branch (10.2.0.0/16) stays down. Step 1: capture on the HQ outside interface: IKE packets arrive on UDP 500, then the branch switches to UDP 4500, which reveals NAT in front of it. Step 2: the HQ firewall allows only UDP 500, so add UDP 4500. Step 3: the tunnel comes up, but only some traffic passes; the branch's traffic selectors list 10.2.1.0/24 while HQ expects 10.2.0.0/16. Step 4: make both sides define the same subnets, and traffic flows.

bash
sudo tcpdump -i eth0 -nn 'udp port 500 or udp port 4500 or ip proto 50'
# Seeing 4500 means NAT-T is in use; allow it on the firewall.
# strongSwan status on Linux gateways:
sudo swanctl --list-sas

Quick reference

  • IPsec: IKE UDP 500, NAT-T UDP 4500, ESP IP protocol 50, AH IP protocol 51
  • Tunnel mode wraps the whole packet (site-to-site); transport mode protects payload only
  • WireGuard: wg genkey | tee priv | wg pubkey > pub
  • AllowedIPs = routes out to the peer and allowed source IPs in from it
  • AllowedIPs = 0.0.0.0/0, ::/0 means full tunnel
  • PersistentKeepalive = 25 keeps NAT mappings open for peers behind NAT
  • OpenVPN default port is UDP 1194; prefer UDP over TCP for tunnels
  • Both sites need non-overlapping subnets, or routing across the tunnel breaks

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1A firewall in front of an IPsec gateway allows UDP 500 only, and one peer sits behind NAT. What else must be allowed?

Question 2

2In WireGuard, what does AllowedIPs = 10.0.0.0/16 on a peer do?

Question 3

3Which IPsec mode encrypts the entire original packet and adds a new IP header?

Question 4

4Why is running a VPN over TCP often slower than over UDP?

Question 5

5What is a downside of split tunnelling?

0/5 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    Generate a WireGuard key pair and show the public key. Explain which key you share and which you keep secret.

  2. Exercise 2medium

    Change a laptop's WireGuard config from split tunnel (office 10.0.0.0/16 only) to full tunnel, and make sure DNS also goes to the office resolver 10.0.40.10.

  3. Exercise 3hard

    Build a WireGuard site-to-site link between two Linux routers: site A LAN 10.1.0.0/16, site B LAN 10.2.0.0/16, tunnel 10.9.0.0/30. Write both configs.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

Is WireGuard more secure than OpenVPN?
Both are considered secure when configured well. WireGuard's small code base and fixed modern ciphers leave less room for mistakes, while OpenVPN is more flexible, for example running over TCP 443.
Does a VPN make me anonymous?
No. It hides your traffic from the local network and your ISP, but the VPN provider or company gateway can see it, and websites can still identify you through logins and cookies.
What port does WireGuard use?
Any single UDP port you set with ListenPort. 51820 is the common convention used in most guides.
Why can I reach office servers by IP but not by name over the VPN?
Your laptop is still using its local DNS server, which does not know internal names. Push the internal DNS server through the VPN, for example with a DNS line in a wg-quick config.

Go deeper

ShareXLinkedInWhatsApp