Architecture: Critical Sequences
Firezone is a distributed system with many moving parts, but some parts are especially critical to the integrity of the entire system:
- Authentication: User authentication, usually with an identity provider.
- Policy evaluation: Deciding whether to allow or deny a connection request.
- Detailed Connection Setup: How connections between Clients and Gateways are established.
- DNS resolution: Resolving DNS-based Resources.
- High availability: How Firezone achieves high availability through load balancing and automatic failover.
These will be explained in more detail below.
Authentication
Firezone authenticates users using two primary methods:
- Email (OTP): Users receive a one-time password (OTP) via email.
- OpenID Connect: Users authenticate with an identity provider that supports OpenID Connect (OIDC).
The authentication process for each is similar. Both methods begin the
authentication process at your Firezone account's sign in page:
https://app.firezone.dev/<your-account> for US accounts or
https://eu.app.firezone.dev/<your-account> for EU accounts.
However, the OIDC flow redirects the user to the identity provider for authentication before the final redirect back to Firezone.
Here's how the authentication flow works:
- User clicks
Sign infrom the Client. - The Client generates random 32-byte
stateandnoncevalues. These are used to prevent certain kinds of forgery and injection attacks. - A browser window opens to your account's sign in page,
the account’s regional portal URL containing the
nonceandstateparameters. - The user chooses which authentication method to use. If OIDC, the user is redirected out to the identity provider.
- After successfully authenticating, the user is redirected back to the admin portal.
- The admin portal mints a Firezone
tokencreated from thenonceparameter and other information. - The admin portal issues a final redirect to
firezone-fd0020211111://handle_client_sign_in_callbackwith thetokenandstateparameters from the initial request. - The Client receives this callback URL and validates the
stateparameter matches what it originally sent. This prevents other applications from injecting tokens into the Client's callback handler. - The Client saves this
tokenin a platform-specific secure storage mechanism, for example Keychain on macOS and iOS. - The Client now has a valid
tokenand uses it to authenticate with the control plane API. - The authentication process is complete.
Policy evaluation
Policy evaluation is the process the Policy Engine uses to decide whether to allow or deny a connection request from a Client to a Resource.
If the request is allowed, connection setup information is sent to the Client and the appropriate Gateway. If the request is denied, it's logged and then dropped. This ensures that Clients are only connected to Gateways that are serving Resources the User is allowed to access.
Connections in Firezone are always default-deny. Policies must be created to allow access.
Here's how the process works:
- The User attempts to access a Resource, e.g.
10.10.10.10. - The Client sees the request and opens a connection request to the Policy Engine.
- The Policy Engine evaluates the request against the configured Policies in your account based on factors such as the Groups the user is a part of, which Resource is being accessed, and so forth. If a match is found, the connection is allowed. If no match is found, the connection is dropped.
- If the connection is allowed, the Policy Engine sends the Client the WireGuard keys and NAT traversal information for the Gateway that will serve the Resource.
- The Policy Engine sends similar details to the Gateway.
- The Client and Gateway establish a WireGuard tunnel, and the Gateway sets up a forwarding rule to the Resource.
- Connection setup is complete. The User can now access the Resource.
Since the Client only receives WireGuard keys and NAT traversal information when a connection is allowed, it's not possible for a Client to exchange packets with the Gateway until explicitly allowed by the Policy Engine.
This means Gateways remain invisible to the outside world, helping to protect against classes of attacks that perimeter-based models may be susceptible to, such as DDoS attacks.
Detailed Connection Setup
The above "Policy evaluation" section touches on this topic briefly in step 6. This section describes in more detail how this connection is established.
Firezone supports NAT traversal, which means that neither the Client nor the Gateway needs to be exposed to the public Internet. Instead of opening and forwarding a port on the NAT device, direct connections are established with a technique called "hole-punching". If that fails, the connection uses a TURN relay. We operate TURN servers in every region offered by Azure to minimize overhead, wherever you are in the world.
Firezone uses its own NAT traversal, built directly on WireGuard. There is no separate connectivity protocol running next to the tunnel. To learn why we built it this way, read WireGuard-native NAT traversal.
Addresses
Each peer can be reached on several addresses:
- Host addresses: The IP and port of the peer's local network interface.
- Public addresses: The IP and port that a server on the Internet sees as the source of the peer's packets. In other words, the public IP of your home router, for example.
- Relay addresses: Sockets allocated on a TURN server. Traffic received on such a socket is forwarded to the owner of that socket. Both Clients and Gateways allocate one on each of the two geographically closest TURN servers as soon as they sign in, before they establish any connections. This way the addresses are ready by the time a connection is needed.
The peers exchange these addresses and their WireGuard public keys through the Firezone control plane API.
Establishing the connection
- The peers send a WireGuard handshake over their relay addresses. Relays are always reachable, so this works even when both peers are behind strict NAT devices. As soon as the handshake completes, traffic can flow.
- Inside the WireGuard tunnel, the peers send small probe packets over every other pair of addresses. A reply proves that a path works in both directions and tells the peers how long a round trip takes.
- Each peer moves traffic to the best working path. Direct paths win over relayed ones, and faster paths win over slower ones.
- The peers keep probing the path in use so that NAT devices keep it open. If the path stops working, for example because a laptop changed networks, the peers probe all pairs again and move to the best one that still works.
WireGuard's handshake requires prior knowledge of the remote's public key, which the peers received from the control plane API. Using these public keys, Client and Gateway agree on secret session keys via Diffie-Hellman. These session keys encrypt every packet and are rotated every 2 minutes.
Hole-punching
Hole-punching happens as part of the probing step. Most NAT devices are stateful. They remember the source port of an outgoing packet and allow packets arriving at the same port back in. Both peers probe the same pairs of addresses, so each one sends packets to the same address the other one sends from.
For example, assume a Client's public IP is 35.10.10.10 and the Gateway's
public IP is 40.10.10.10. Locally, both Firezone Clients and Firezone Gateways
listen on port 52625 by default. Each peer learns its own public address, so
both of them probe the pair 35.10.10.10:52625 <> 40.10.10.10:52625.
- Client sends a UDP packet from its local socket using source port
52625towards40.10.10.10:52625 - Gateway sends a UDP packet from its local socket using source port
52625towards35.10.10.10.52625 - NAT device on the Client's network rewrites the source of the outgoing packet
to
35.10.10.10:52625. It also registers a new "connection" on this port. - NAT device on the Gateways's network rewrites the source of the outgoing
packet to
40.10.10.10:52625. It also registers a new "connection" on this port. - Packet from the Client arrives at the Gateway's NAT device. The lookup by
source address (
35.10.10.10:52625) yields the connection created in step 4. - Packet from the Gateway arrives at the Client's NAT device. The lookup by
source address (
40.10.10.10:52625) yields the connection created in step 3.
Due to differing latencies, the timing of these steps can vary in practice. That isn't an issue though as UDP is by design stateless and the next packet will simply be allowed through.
Relayed connections
A relayed connection is one where the best working path uses a relay address. This happens when no probe over a direct path succeeds, for example when both peers are behind symmetric NAT devices. Relays are transparent to the WireGuard handshake and the encrypted packets.
To send data through a TURN server, the sender constructs a channel-data packet. This packet is a lightweight wrapper consisting of a 4-byte header that identifies a previously allocated channel. Channel allocation is handled as part of the TURN client–server protocol. Each channel is bound to a single remote peer, and all packets sent on that channel are forwarded to that peer. When the TURN server receives a channel-data packet, it removes the header and forwards the remaining payload to the designated peer. To improve their efficiency and throughput, Firezone's TURN servers make use of eBPF eXpress data path (XDP) and implement this routing of channel-data packets directly in the kernel. Specifically, in the network card driver even before the packet gets parsed.
To the receiver of the packet, this process is transparent. They simply send and receive UDP traffic from an IP and port and outside of applying heuristics based on e.g. IP location, cannot differentiate between this socket being a relay or the remote peer directly.
DNS resolution
Secure DNS resolution is a critical function in most organizations.
Firezone employs a unique, granular approach to split DNS to ensure traffic intended only for DNS-based Resources is routed through Firezone, leaving other traffic untouched -- even when resolved IP addresses overlap.
To achieve this, Firezone embeds a tiny, in-memory DNS resolver in each Client that intercepts all DNS queries on the system.
When the resolver sees a query that doesn't match a known Resource, it operates in pass-through mode, forwarding the query to the system's default resolvers or configured upstream resolvers in your account.
If the query matches a Resource, however, the following happens:
- The resolver generates a special, internal IP from the range
100.96.0.0/11orfd00:2021:1111:8000::/107and stores an internal mapping of this IP to the DNS name originally queried. The IP is returned to the application that made the query. - When the Client sees traffic for the IP, it triggers connection setup. The Client sends a request to the Policy Engine for evaluation. If the request is allowed, the Policy Engine finds an appropriate Gateway to route the traffic.
- If the Policy Engine approves the Client's request, it sends the Client the WireGuard keys and NAT traversal information for the Gateway that will serve the Resource.
- Once the connection is established (and every time an application re-queries a DNS name), the Client sends a message using Firezone's p2p control protocol to the Gateway, triggering a DNS query using the Gateway's system resolver for the DNS name. The result of this DNS query is stored on the Gateway in a lookup table and cached for the TTL of each DNS record.
- Traffic from the application flows, and the Gateway translates the internal IP back to the actual IP address of the Resource, forwarding the traffic accordingly.
This is why you'll see DNS-based Resources resolve to IPs such as 100.96.0.1
while the Client is signed in:
> nslookup github.com
Server: 100.100.111.1
Address: 100.100.111.1#53
Non-authoritative answer:
Name: github.com
Address: 100.96.0.1
Notice in the above process that at no point does the Client's system resolver see the actual IP address of the Resource. This ensures that your DNS data remains private and secure.
For a deeper dive into how (and why) DNS works this way in Firezone, see the How DNS works in Firezone article.
Why Firezone uses a mapped address for DNS Resources
This is a common source of confusion among new Firezone users, so it's helpful to explain why Firezone uses mapped IPs for DNS Resources instead of simply using the actual resolved IP.
Consider the case where two DNS Resources resolve to the same IP address, such as when Name-based virtual hosting is used to host two web applications on the same server:
gitlab.company.comresolves to IP172.16.0.5jenkins.company.comalso resolves to IP172.16.0.5
Remember that routing happens at the IP level. We can't independently route
packets for the same IP to two different places. If Firezone used the Resource's
actual IP address to route packets, the User would be able to access
jenkins.company.com if they were granted access only to gitlab.company.com.
Using mapped IPs allows Firezone to securely route DNS Resources no matter how many other services share the same IP address.
High availability
Firezone was designed from the ground up to support high availability requirements. This is achieved through a combination of load balancing and automatic failover, described below.
Load balancing
When a Client wants to connect to a Resource, Firezone automatically selects a Gateway in the Resource's Site to handle the request:
- Only Gateways that are online and run a version compatible with the Client are considered.
- If the Client is already connected to one or more of these Gateways, Firezone picks from those first. This reuses existing connections.
- Among the remaining Gateways, Firezone picks the one closest to the Client's geolocated IP address.
- If two Gateways are equally close, the one running the newer version wins. If they also run the same version, Firezone picks one at random.
The Client maintains the connection to that Gateway until either the Client disconnects or the Gateway becomes unhealthy.
This effectively shards Client connections across all Gateways in a Site, achieving higher overall throughput than otherwise possible with a single Gateway.
Automatic failover
Two or more Gateways deployed within a Site provide automatic failover in the event of a Gateway failure.
Here's how it works:
- When the admin portal detects a particular Gateway is unhealthy, it will stop using it for new connection requests to Resources in the Site.
- Existing Clients will remain connected to the Gateway until they themselves detect it to be unhealthy.
- Clients identify unhealthy gateways using keepalive timers. If the timer expires, the Client will disconnect from the unhealthy Gateway and request a new, healthy one from the portal.
- The Client keepalive timer expires after 15 seconds. This is the maximum time it takes for existing Client connections to be rerouted to a healthy Gateway in the event of a Gateway failure.
By using two independent health checks in the portal and the Client, Firezone ensures that temporary network issues between the Client and portal do not interrupt existing connections to healthy Gateways.
Need help? See all support options.