Commit Graph

1092 Commits

Author SHA1 Message Date
Thomas Eizinger
ce06996a14 fix(connlib): allow more than one host candidate per IP version (#9147)
Currently, one machines that have multiple routable egress interfaces,
`connlib` may bounce between the two instead of settling on one. This
happens because we have a dedicated `CandidateSet` that we use to filter
out "duplicate" candidates of the same type. Doing that is important
because if the other party is behind a symmetric NAT, they will send us
many server-reflexive candidates that all only differ by their port,
none of them will actually be routable though.

To prevent sending many of these candidates to the remote, we first
gather them locally in our `CandidateSet` and de-duplicate them.
2025-05-15 02:08:53 +00:00
Thomas Eizinger
09191549eb test(rust): don't delay delivering scheduled Transmits (#9129)
To simulate varying network conditions in our tests, each `Host` in our
test network has an "inbox" that contains all incoming network packets
with an added latency. When another hosts sends a packet, the packet
gets added to the inbox. Internally, the inbox has a binary heap that
sorts incoming `Transmits` by their latency and only delivers them to
the node when that delay is up.

Currently, this delivery doesn't always happen because we fail to take
into account the timestamp as when the next `Transmit` is due when we
figure out what to do next.

Instead of just looking at the inner state via `poll_transmit`, we now
also consult the inbox of messages as to when the next message is due
and wake up at the correct time.

Not doing this caused our state machine to think that packets got
dropped because `REFRESH` messages to the relays were timing out.

Resolves: #9118
2025-05-14 05:37:05 +00:00
Thomas Eizinger
45924eb90b fix(connlib): ignore scopes for IPv6 link-local addresses (#9115)
To send UDP DNS queries to upstream DNS servers, we have a
`UdpSocket::handshake` function that turns a UDP socket into a
single-use object where exactly one datagram is expected from the
address we send a message to. The way this is enforced is via an
equality check.

It appears that this equality check fails if users run an upstream DNS
server on a link-local IPv6 address within a setup that utilises IPv6
scopes. At the time when we receive the response, the packet has already
been successfully routed back to us so we should accept it, even if we
didn't specify a scope as the destination address.
2025-05-13 13:33:28 +00:00
Thomas Eizinger
b8738448df refactor(connlib): forward error from source IP resolver (#9116)
In order to avoid routing loops on Windows, our UDP and TCP sockets in
`connlib` embed a "source IP resolver" that finds the "next best"
interface after our TUN device according to Windows' routing metrics.
This ensures that packets don't get routed back into our TUN device.

Currently, errors during this process are only logged on TRACE and
therefore not visible in Sentry. We fix this by moving around some of
the function interfaces and forward the error from the source IP
resolver together with some context of the destination IP.
2025-05-13 13:33:15 +00:00
Thomas Eizinger
945fed8e9d chore(phoenix-channel): downgrade log about dropped messages (#9092)
This can easily happen if we are briefly disconnected from the portal.
It is not the end of the world and not worth creating Sentry alerts for.

Originally, this was intended to be a way of detecting "bad
connectivity" but that didn't really work.
2025-05-12 11:40:40 +00:00
Thomas Eizinger
f01fd4ddf6 fix(connlib): clear pending sockets on DNS server re-creation (#9093)
Our DNS over TCP implementation uses `smoltcp` which requires us to
manage sockets individually, i.e. there is no such thing as a listening
socket. Instead, we have to create multiple sockets and rotate through
them.

Whenever we receive new DNS servers from the host app, we throw away all
of those sockets and create new ones.

The way we refer to these sockets internally is via `smoltcp`'s
`SocketHandle`. These are just indices into a `Vec` and this access can
panic when it is out of range. Normally that doesn't happen because such
a `SocketHandle` is only created when the socket is created and
therefore, each `SocketHandle` in existence should be valid.

What we overlooked is that these sockets get destroyed and re-created
when we call `set_listen_addresses` which happens when the host app
tells us about new DNS servers. In that case, sockets that we had just
received a query on and are waiting for a response have their handles
stored in a temporary `HashMap`. Attempting to send back a response for
one of those queries will then either fail with an error that the socket
is not in the right state or - worse - panic with an out of bounds error
if the previously had more listen addresses than we have now.

To fix this, we need to clear this map of pending queries every time we
call `set_listen_addresses`.
2025-05-12 11:39:59 +00:00
Thomas Eizinger
7e4fe68485 fix(connlib): take into account header overhead for GSO (#9088)
When calculating the maximum size of the UDP payload we can send in a
single syscall, we need to take into account the overhead of the IP and
UDP headers.
2025-05-12 11:36:10 +00:00
Jamil
537295d8a3 fix(rust): Downgrade fastest nameserver to DEBUG (#9071)
These run every minute and add a lot of noise to the logs.

```
May 11 18:21:14 gateway-z1w4 firezone-gateway[2007]: 2025-05-11T18:21:14.154Z  INFO firezone_tunnel::io::nameserver_set: Evaluating fastest nameserver ips={127.0.0.53}
May 11 18:21:14 gateway-z1w4 firezone-gateway[2007]: 2025-05-11T18:21:14.155Z  INFO firezone_tunnel::io::nameserver_set: Evaluated fastest nameserver fastest=127.0.0.53
May 11 18:22:14 gateway-z1w4 firezone-gateway[2007]: 2025-05-11T18:22:14.154Z  INFO firezone_tunnel::io::nameserver_set: Evaluating fastest nameserver ips={127.0.0.53}
May 11 18:22:14 gateway-z1w4 firezone-gateway[2007]: 2025-05-11T18:22:14.155Z  INFO firezone_tunnel::io::nameserver_set: Evaluated fastest nameserver fastest=127.0.0.53
May 11 18:23:14 gateway-z1w4 firezone-gateway[2007]: 2025-05-11T18:23:14.153Z  INFO firezone_tunnel::io::nameserver_set: Evaluating fastest nameserver ips={127.0.0.53}
May 11 18:23:14 gateway-z1w4 firezone-gateway[2007]: 2025-05-11T18:23:14.155Z  INFO firezone_tunnel::io::nameserver_set: Evaluated fastest nameserver fastest=127.0.0.53
May 11 18:24:14 gateway-z1w4 firezone-gateway[2007]: 2025-05-11T18:24:14.154Z  INFO firezone_tunnel::io::nameserver_set: Evaluating fastest nameserver ips={127.0.0.53}
May 11 18:24:14 gateway-z1w4 firezone-gateway[2007]: 2025-05-11T18:24:14.155Z  INFO firezone_tunnel::io::nameserver_set: Evaluated fastest nameserver fastest=127.0.0.53
May 11 18:25:14 gateway-z1w4 firezone-gateway[2007]: 2025-05-11T18:25:14.153Z  INFO firezone_tunnel::io::nameserver_set: Evaluating fastest nameserver ips={127.0.0.53}
```
2025-05-12 01:58:17 +00:00
Thomas Eizinger
5566f1847f refactor(rust): move crates into a more sensical hierarchy (#9066)
The current `rust/` directory is a bit of a wild-west in terms of how
the crates are organised. Most of them are simply at the top-level when
in reality, they are all `connlib`-related. The Apple and Android FFI
crates - which are entrypoints in the Rust code are defined several
layers deep.

To improve the situation, we move around and rename several crates. The
end result is that all top-level crates / directories are:

- Either entrypoints into the Rust code, i.e. applications such as
Gateway, Relay or a Client
- Or crates shared across all those entrypoints, such as `telemetry` or
`logging`
2025-05-12 01:04:17 +00:00
Thomas Eizinger
3f4e004a48 fix(connlib): don't recreate DNS resource NAT for failed domains (#9064)
Before a Client can send packets to a DNS resource, the Gateway must
first setup a NAT table between the IPs assigned by the Client and the
IPs the domain actually resolves to. This is what we call the DNS
resource NAT.

The communication for this process happens over IP through the tunnel
which is an unreliable transport. To ensure that this works reliably
even in the presence of packet loss on the wire, the Client uses an
idempotent algorithm where it tracks the state of the NAT for each
domain that is has ever assigned IPs for (i.e. received an A or AAAA
query from an application). This algorithm ensures that if we don't hear
anything back from the Gateway within 2s, another packet for setting up
the NAT is sent as soon as we receive _any_ DNS query.

This design balances efficiency (we don't try forever) with reliability
(we always check all of them).

In case a domain does not resolve at all or there are resolution errors,
the Gateway replies with `NatStatus::Inactive`. At present, the Client
doesn't handle this in any particular way other than logging that it was
not able to successfully setup the NAT.

The combination of the above results in an undesirable behaviour: If an
application queries a domain without A and AAAA records once, we will
keep retrying forever to resolve it upon every other DNS query issued to
the system. To fix this, we introduce `dns_resource_nat::State::Failed`.
Entries in this state are ignored as part of the above algorithm and
only recreated when explicitly told to do so which we only do when we
receive another DNS query for this domain.

To handle the increased complexity around this system, we extract it
into its own component and add a fleet of unit tests for its behaviour.
2025-05-09 15:04:21 +00:00
Thomas Eizinger
fa790b231a fix(gateway): respond with SERVFAIL for missing nameserver (#9061)
When we implemented #8350, we chose an error handling strategy that
would shutdown the Gateway in case we didn't have a nameserver selected
for handling those SRV and TXT queries. At the time, this was deemed to
be sufficiently rare to be an adequate strategy. We have since learned
that this can indeed happen when the Gateway starts without network
connectivity which is quite common when using tools such as terraform to
provision infrastructure.

In #9060, we fix this by re-evaluating the fastest nameserver on a
timer. This however doesn't change the error handling strategy when we
don't have a working nameserver at all. It is practically impossible to
have a working Gateway yet us being unable to select a nameserver. We
read them from `/etc/resolv.conf` which is what `libc` uses to also
resolve the domain we connect to for the WebSocket. A working WebSocket
connection is required for us to establish connections to Clients, which
in turn is a precursor to us receiving DNS queries from a Client.

It causes unnecessary complexity to have a code path that can
potentially terminate the Gateway, yet is practically unreachable. To
fix this situation, we remove this code path and instead reply with a
DNS SERVFAIL error.

---------

Signed-off-by: Thomas Eizinger <thomas@eizinger.io>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-05-09 05:55:48 +00:00
Thomas Eizinger
ac339ff63b fix(gateway): evaluate fastest nameserver every 60s (#9060)
Currently, the Gateway reads all nameservers from `/etc/resolv.conf` on
startup and evaluates the fastest one to use for SRV and TXT DNS queries
that are forwarded by the Client. If the machine just booted and we do
not have Internet connectivity just yet, this fails which leaves the
Gateway in state where it cannot fulfill those queries.

In order to ensure we always use the fastest one and to self-heal from
such situations, we add a 60s timer that refreshes this state.
Currently, this will **not** re-read the nameservers from
`/etc/resolv.conf` but still use the same IPs read on startup.
2025-05-09 03:38:35 +00:00
Thomas Eizinger
33d5c32f35 fix(gateway): truncate payload of ICMP errors (#9059)
When the Gateway is handed an IP packet for a DNS resource that it
cannot route, it sends back an ICMP unreachable error. According to RFC
792 [0] (for ICMPv4) and RFC 4443 [1] (for ICMPv6), parts of the
original packet should be included in the ICMP error payload to allow
the sending party to correlate, what could not be sent.

For ICMPv4, the RFC says:

```
Internet Header + 64 bits of Data Datagram

The internet header plus the first 64 bits of the original
datagram's data.  This data is used by the host to match the
message to the appropriate process.  If a higher level protocol
uses port numbers, they are assumed to be in the first 64 data
bits of the original datagram's data.
```

For ICMPv6, the RFC says:

```
As much of invoking packet as possible without the ICMPv6 packet exceeding the minimum IPv6 MTU
```

[0]: https://datatracker.ietf.org/doc/html/rfc792
[1]: https://datatracker.ietf.org/doc/html/rfc4443#section-3.1
2025-05-09 01:38:31 +00:00
Thomas Eizinger
18ec6c6860 refactor(rust): move service implementation to GUI client (#9045)
The module and crate structure around the GUI client and its background
service are currently a mess of circular dependencies. Most of the
service implementation actually sits in `firezone-headless-client`
because the headless-client and the service share certain modules. We
have recently moved most of these to `firezone-bin-shared` which is the
correct place for these modules.

In order to move the background service to `firezone-gui-client`, we
need to untangle a few more things in the GUI client. Those are done
commit-by-commit in this PR. With that out the way, we can finally move
the service module to the GUI client; where is should actually live
given that it has nothing to do with the headless client.

As a result, the headless-client is - as one would expect - really just
a thin wrapper around connlib itself and is reduced down to 4 files with
this PR.

To make things more consistent in the GUI client, we move the `main.rs`
file also into `bin/`. By convention `bin/` is where you define binaries
if a crate has more than one. cargo will then build all of them.

Eventually, we can optimise the compile-times for `firezone-gui-client`
by splitting it into multiple crates:

- Shared structs like IPC messages
- Background service
- GUI client

This will be useful because it allows only re-compiling of the GUI
client alone if nothing in `connlib` changes and vice versa.

Resolves: #6913
Resolves: #5754
2025-05-08 13:22:09 +00:00
dependabot[bot]
bea57c02c4 build(deps): bump libc from 0.2.171 to 0.2.172 in /rust (#9031)
Bumps [libc](https://github.com/rust-lang/libc) from 0.2.171 to 0.2.172.
<details>
<summary>Release notes</summary>
<p><em>Sourced from <a
href="https://github.com/rust-lang/libc/releases">libc's
releases</a>.</em></p>
<blockquote>
<h2>0.2.172</h2>
<h3>Added</h3>
<ul>
<li>Android: Add <code>getauxval</code> for 32-bit targets (<a
href="https://redirect.github.com/rust-lang/libc/pull/4338">#4338</a>)</li>
<li>Android: Add <code>if_tun.h</code> ioctls (<a
href="https://redirect.github.com/rust-lang/libc/pull/4379">#4379</a>)</li>
<li>Android: Define <code>SO_BINDTOIFINDEX</code> (<a
href="https://redirect.github.com/rust-lang/libc/pull/4391">#4391</a>)</li>
<li>Cygwin: Add <code>posix_spawn_file_actions_add[f]chdir[_np]</code>
(<a
href="https://redirect.github.com/rust-lang/libc/pull/4387">#4387</a>)</li>
<li>Cygwin: Add new socket options (<a
href="https://redirect.github.com/rust-lang/libc/pull/4350">#4350</a>)</li>
<li>Cygwin: Add statfs &amp; fcntl (<a
href="https://redirect.github.com/rust-lang/libc/pull/4321">#4321</a>)</li>
<li>FreeBSD: Add <code>filedesc</code> and <code>fdescenttbl</code> (<a
href="https://redirect.github.com/rust-lang/libc/pull/4327">#4327</a>)</li>
<li>Glibc: Add unstable support for _FILE_OFFSET_BITS=64 (<a
href="https://redirect.github.com/rust-lang/libc/pull/4345">#4345</a>)</li>
<li>Hermit: Add <code>AF_UNSPEC</code> (<a
href="https://redirect.github.com/rust-lang/libc/pull/4344">#4344</a>)</li>
<li>Hermit: Add <code>AF_VSOCK</code> (<a
href="https://redirect.github.com/rust-lang/libc/pull/4344">#4344</a>)</li>
<li>Illumos, NetBSD: Add <code>timerfd</code> APIs (<a
href="https://redirect.github.com/rust-lang/libc/pull/4333">#4333</a>)</li>
<li>Linux: Add <code>_IO</code>, <code>_IOW</code>, <code>_IOR</code>,
<code>_IOWR</code> to the exported API (<a
href="https://redirect.github.com/rust-lang/libc/pull/4325">#4325</a>)</li>
<li>Linux: Add <code>tcp_info</code> to uClibc bindings (<a
href="https://redirect.github.com/rust-lang/libc/pull/4347">#4347</a>)</li>
<li>Linux: Add further BPF program flags (<a
href="https://redirect.github.com/rust-lang/libc/pull/4356">#4356</a>)</li>
<li>Linux: Add missing INPUT_PROP_XXX flags from
<code>input-event-codes.h</code> (<a
href="https://redirect.github.com/rust-lang/libc/pull/4326">#4326</a>)</li>
<li>Linux: Add missing TLS bindings (<a
href="https://redirect.github.com/rust-lang/libc/pull/4296">#4296</a>)</li>
<li>Linux: Add more constants from <code>seccomp.h</code> (<a
href="https://redirect.github.com/rust-lang/libc/pull/4330">#4330</a>)</li>
<li>Linux: Add more glibc <code>ptrace_sud_config</code> and related
<code>PTRACE_*ET_SYSCALL_USER_DISPATCH_CONFIG</code>. (<a
href="https://redirect.github.com/rust-lang/libc/pull/4386">#4386</a>)</li>
<li>Linux: Add new netlink flags (<a
href="https://redirect.github.com/rust-lang/libc/pull/4288">#4288</a>)</li>
<li>Linux: Define ioctl codes on more architectures (<a
href="https://redirect.github.com/rust-lang/libc/pull/4382">#4382</a>)</li>
<li>Linux: Add missing <code>pthread_attr_setstack</code> (<a
href="https://redirect.github.com/rust-lang/libc/pull/4349">#4349</a>)</li>
<li>Musl: Add missing <code>utmpx</code> API (<a
href="https://redirect.github.com/rust-lang/libc/pull/4332">#4332</a>)</li>
<li>Musl: Enable <code>getrandom</code> on all platforms (<a
href="https://redirect.github.com/rust-lang/libc/pull/4346">#4346</a>)</li>
<li>NuttX: Add more signal constants (<a
href="https://redirect.github.com/rust-lang/libc/pull/4353">#4353</a>)</li>
<li>QNX: Add QNX 7.1-iosock and 8.0 to list of additional cfgs (<a
href="https://redirect.github.com/rust-lang/libc/pull/4169">#4169</a>)</li>
<li>QNX: Add support for alternative Neutrino network stack
<code>io-sock</code> (<a
href="https://redirect.github.com/rust-lang/libc/pull/4169">#4169</a>)</li>
<li>Redox: Add more <code>sys/socket.h</code> and <code>sys/uio.h</code>
definitions (<a
href="https://redirect.github.com/rust-lang/libc/pull/4388">#4388</a>)</li>
<li>Solaris: Temporarily define <code>O_DIRECT</code> and
<code>SIGINFO</code> (<a
href="https://redirect.github.com/rust-lang/libc/pull/4348">#4348</a>)</li>
<li>Solarish: Add <code>secure_getenv</code> (<a
href="https://redirect.github.com/rust-lang/libc/pull/4342">#4342</a>)</li>
<li>VxWorks: Add missing <code>d_type</code> member to
<code>dirent</code> (<a
href="https://redirect.github.com/rust-lang/libc/pull/4352">#4352</a>)</li>
<li>VxWorks: Add missing signal-related constsants (<a
href="https://redirect.github.com/rust-lang/libc/pull/4352">#4352</a>)</li>
<li>VxWorks: Add more error codes (<a
href="https://redirect.github.com/rust-lang/libc/pull/4337">#4337</a>)</li>
</ul>
<h3>Deprecated</h3>
<ul>
<li>FreeBSD: Deprecate <code>TCP_PCAP_OUT</code> and
<code>TCP_PCAP_IN</code> (<a
href="https://redirect.github.com/rust-lang/libc/pull/4381">#4381</a>)</li>
</ul>
<h3>Fixed</h3>
<ul>
<li>Cygwin: Fix member types of <code>statfs</code> (<a
href="https://redirect.github.com/rust-lang/libc/pull/4324">#4324</a>)</li>
<li>Cygwin: Fix tests (<a
href="https://redirect.github.com/rust-lang/libc/pull/4357">#4357</a>)</li>
<li>Hermit: Make <code>AF_INET = 3</code> (<a
href="https://redirect.github.com/rust-lang/libc/pull/4344">#4344</a>)</li>
<li>Musl: Fix the syscall table on RISC-V-32 (<a
href="https://redirect.github.com/rust-lang/libc/pull/4335">#4335</a>)</li>
<li>Musl: Fix the value of <code>SA_ONSTACK</code> on RISC-V-32 (<a
href="https://redirect.github.com/rust-lang/libc/pull/4335">#4335</a>)</li>
<li>VxWorks: Fix a typo in the <code>waitpid</code> parameter name (<a
href="https://redirect.github.com/rust-lang/libc/pull/4334">#4334</a>)</li>
</ul>
<h3>Removed</h3>
<!-- raw HTML omitted -->
</blockquote>
<p>... (truncated)</p>
</details>
<details>
<summary>Changelog</summary>
<p><em>Sourced from <a
href="https://github.com/rust-lang/libc/blob/0.2.172/CHANGELOG.md">libc's
changelog</a>.</em></p>
<blockquote>
<h2><a
href="https://github.com/rust-lang/libc/compare/0.2.171...0.2.172">0.2.172</a>
- 2025-04-14</h2>
<h3>Added</h3>
<ul>
<li>Android: Add <code>getauxval</code> for 32-bit targets (<a
href="https://redirect.github.com/rust-lang/libc/pull/4338">#4338</a>)</li>
<li>Android: Add <code>if_tun.h</code> ioctls (<a
href="https://redirect.github.com/rust-lang/libc/pull/4379">#4379</a>)</li>
<li>Android: Define <code>SO_BINDTOIFINDEX</code> (<a
href="https://redirect.github.com/rust-lang/libc/pull/4391">#4391</a>)</li>
<li>Cygwin: Add <code>posix_spawn_file_actions_add[f]chdir[_np]</code>
(<a
href="https://redirect.github.com/rust-lang/libc/pull/4387">#4387</a>)</li>
<li>Cygwin: Add new socket options (<a
href="https://redirect.github.com/rust-lang/libc/pull/4350">#4350</a>)</li>
<li>Cygwin: Add statfs &amp; fcntl (<a
href="https://redirect.github.com/rust-lang/libc/pull/4321">#4321</a>)</li>
<li>FreeBSD: Add <code>filedesc</code> and <code>fdescenttbl</code> (<a
href="https://redirect.github.com/rust-lang/libc/pull/4327">#4327</a>)</li>
<li>Glibc: Add unstable support for _FILE_OFFSET_BITS=64 (<a
href="https://redirect.github.com/rust-lang/libc/pull/4345">#4345</a>)</li>
<li>Hermit: Add <code>AF_UNSPEC</code> (<a
href="https://redirect.github.com/rust-lang/libc/pull/4344">#4344</a>)</li>
<li>Hermit: Add <code>AF_VSOCK</code> (<a
href="https://redirect.github.com/rust-lang/libc/pull/4344">#4344</a>)</li>
<li>Illumos, NetBSD: Add <code>timerfd</code> APIs (<a
href="https://redirect.github.com/rust-lang/libc/pull/4333">#4333</a>)</li>
<li>Linux: Add <code>_IO</code>, <code>_IOW</code>, <code>_IOR</code>,
<code>_IOWR</code> to the exported API (<a
href="https://redirect.github.com/rust-lang/libc/pull/4325">#4325</a>)</li>
<li>Linux: Add <code>tcp_info</code> to uClibc bindings (<a
href="https://redirect.github.com/rust-lang/libc/pull/4347">#4347</a>)</li>
<li>Linux: Add further BPF program flags (<a
href="https://redirect.github.com/rust-lang/libc/pull/4356">#4356</a>)</li>
<li>Linux: Add missing INPUT_PROP_XXX flags from
<code>input-event-codes.h</code> (<a
href="https://redirect.github.com/rust-lang/libc/pull/4326">#4326</a>)</li>
<li>Linux: Add missing TLS bindings (<a
href="https://redirect.github.com/rust-lang/libc/pull/4296">#4296</a>)</li>
<li>Linux: Add more constants from <code>seccomp.h</code> (<a
href="https://redirect.github.com/rust-lang/libc/pull/4330">#4330</a>)</li>
<li>Linux: Add more glibc <code>ptrace_sud_config</code> and related
<code>PTRACE_*ET_SYSCALL_USER_DISPATCH_CONFIG</code>. (<a
href="https://redirect.github.com/rust-lang/libc/pull/4386">#4386</a>)</li>
<li>Linux: Add new netlink flags (<a
href="https://redirect.github.com/rust-lang/libc/pull/4288">#4288</a>)</li>
<li>Linux: Define ioctl codes on more architectures (<a
href="https://redirect.github.com/rust-lang/libc/pull/4382">#4382</a>)</li>
<li>Linux: Add missing <code>pthread_attr_setstack</code> (<a
href="https://redirect.github.com/rust-lang/libc/pull/4349">#4349</a>)</li>
<li>Musl: Add missing <code>utmpx</code> API (<a
href="https://redirect.github.com/rust-lang/libc/pull/4332">#4332</a>)</li>
<li>Musl: Enable <code>getrandom</code> on all platforms (<a
href="https://redirect.github.com/rust-lang/libc/pull/4346">#4346</a>)</li>
<li>NuttX: Add more signal constants (<a
href="https://redirect.github.com/rust-lang/libc/pull/4353">#4353</a>)</li>
<li>QNX: Add QNX 7.1-iosock and 8.0 to list of additional cfgs (<a
href="https://redirect.github.com/rust-lang/libc/pull/4169">#4169</a>)</li>
<li>QNX: Add support for alternative Neutrino network stack
<code>io-sock</code> (<a
href="https://redirect.github.com/rust-lang/libc/pull/4169">#4169</a>)</li>
<li>Redox: Add more <code>sys/socket.h</code> and <code>sys/uio.h</code>
definitions (<a
href="https://redirect.github.com/rust-lang/libc/pull/4388">#4388</a>)</li>
<li>Solaris: Temporarily define <code>O_DIRECT</code> and
<code>SIGINFO</code> (<a
href="https://redirect.github.com/rust-lang/libc/pull/4348">#4348</a>)</li>
<li>Solarish: Add <code>secure_getenv</code> (<a
href="https://redirect.github.com/rust-lang/libc/pull/4342">#4342</a>)</li>
<li>VxWorks: Add missing <code>d_type</code> member to
<code>dirent</code> (<a
href="https://redirect.github.com/rust-lang/libc/pull/4352">#4352</a>)</li>
<li>VxWorks: Add missing signal-related constsants (<a
href="https://redirect.github.com/rust-lang/libc/pull/4352">#4352</a>)</li>
<li>VxWorks: Add more error codes (<a
href="https://redirect.github.com/rust-lang/libc/pull/4337">#4337</a>)</li>
</ul>
<h3>Deprecated</h3>
<ul>
<li>FreeBSD: Deprecate <code>TCP_PCAP_OUT</code> and
<code>TCP_PCAP_IN</code> (<a
href="https://redirect.github.com/rust-lang/libc/pull/4381">#4381</a>)</li>
</ul>
<h3>Fixed</h3>
<ul>
<li>Cygwin: Fix member types of <code>statfs</code> (<a
href="https://redirect.github.com/rust-lang/libc/pull/4324">#4324</a>)</li>
<li>Cygwin: Fix tests (<a
href="https://redirect.github.com/rust-lang/libc/pull/4357">#4357</a>)</li>
<li>Hermit: Make <code>AF_INET = 3</code> (<a
href="https://redirect.github.com/rust-lang/libc/pull/4344">#4344</a>)</li>
<li>Musl: Fix the syscall table on RISC-V-32 (<a
href="https://redirect.github.com/rust-lang/libc/pull/4335">#4335</a>)</li>
<li>Musl: Fix the value of <code>SA_ONSTACK</code> on RISC-V-32 (<a
href="https://redirect.github.com/rust-lang/libc/pull/4335">#4335</a>)</li>
<li>VxWorks: Fix a typo in the <code>waitpid</code> parameter name (<a
href="https://redirect.github.com/rust-lang/libc/pull/4334">#4334</a>)</li>
</ul>
<!-- raw HTML omitted -->
</blockquote>
<p>... (truncated)</p>
</details>
<details>
<summary>Commits</summary>
<ul>
<li><a
href="a5eab581f9"><code>a5eab58</code></a>
Merge pull request <a
href="https://redirect.github.com/rust-lang/libc/issues/4410">#4410</a>
from tgross35/release-libc</li>
<li><a
href="481eca7cc3"><code>481eca7</code></a>
chore: release libc 0.2.172</li>
<li><a
href="ce2edbbaa9"><code>ce2edbb</code></a>
Merge pull request <a
href="https://redirect.github.com/rust-lang/libc/issues/4399">#4399</a>
from tgross35/backport-triagebot-branch-warn</li>
<li><a
href="31b3200907"><code>31b3200</code></a>
Suggest stable-nominated in the PR template</li>
<li><a
href="3bffe1d58a"><code>3bffe1d</code></a>
Make triagebot warn on non-default branches</li>
<li><a
href="03e6ffc8c4"><code>03e6ffc</code></a>
Merge pull request <a
href="https://redirect.github.com/rust-lang/libc/issues/4396">#4396</a>
from tgross35/backport-serrano</li>
<li><a
href="f9a47ac811"><code>f9a47ac</code></a>
Define SO_BINDTOIFINDEX on Android</li>
<li><a
href="a358dae479"><code>a358dae</code></a>
Add missing utmpx apis for linux musl</li>
<li><a
href="1ff2f2181a"><code>1ff2f21</code></a>
adding linux glibc ptrace_sud_config and related
PTRACE_*ET_SYSCALL_USER_DISP...</li>
<li><a
href="55c58c956d"><code>55c58c9</code></a>
Add more redox sys/socket.h and sys/uio.h definitions</li>
<li>Additional commits viewable in <a
href="https://github.com/rust-lang/libc/compare/0.2.171...0.2.172">compare
view</a></li>
</ul>
</details>
<br />


[![Dependabot compatibility
score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=libc&package-manager=cargo&previous-version=0.2.171&new-version=0.2.172)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't
alter it yourself. You can also trigger a rebase manually by commenting
`@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits
that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after
your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge
and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating
it. You can achieve the same result by closing it manually
- `@dependabot show <dependency name> ignore conditions` will show all
of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop
Dependabot creating any more for this major version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop
Dependabot creating any more for this minor version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop
Dependabot creating any more for this dependency (unless you reopen the
PR or upgrade to it yourself)


</details>

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Thomas Eizinger <thomas@eizinger.io>
2025-05-06 01:26:26 +00:00
Thomas Eizinger
37529803ce build(rust): bump otel ecosystem crates to 0.29 (#9029) 2025-05-05 12:33:07 +00:00
Thomas Eizinger
2d802edf6a fix(connlib): _always_ use one IP stack for relayed connections (#9018)
At the moment, Firezone already attempts to prefer the same IP stack
across relayed connections all the way through to the Gateway. This is
achieved with a feature in str0m implemented in
https://github.com/algesten/str0m/pull/640 where the `IceAgent` computes
the local preference of an added candidate such that an IPv4 candidate
allocated over an IPv4 network has a higher preference than an IPv6
candidate allocated over an IPv4 network.

If a candidate gets accepted by the local agent, it is signaled to the
remote via our control protocol. The remote peer then adds the candidate
as a remote candidate and the ICE process starts by pairing them with
local ones and testing connectivity.

Currently, str0m's API consumes the candidate and only returns a `bool`
whether it should be sent signaled to the remote. This means the local
preference computed as part of `add_local_candidate` **is not**
reflected in the priority of the candidate sent to the remote. As a
result, if the controlled agent (i.e. the Gateway) is behind symmetric
NAT and therefore only has relay candidates available, the preference of
IPv4 over IPv6 candidates on an IPv4 network is lost. This is what we
are seeing in #8998.

This changes with https://github.com/algesten/str0m/pull/650 being
merged to `main` which we are updating to with this PR. Now,
`add_local_candidate` returns an `Option<&Candidate>` which has been
modified with the local preference of the `IceAgent` around the
preferred IP stack of relay candidates. As such, the priority calculated
and signaled to the remote embeds this information and will be taken
into account by the controlling agent (i.e. the Client) when nominating
a pair.

Resolves: #8998
2025-05-05 01:11:28 +00:00
Jamil
6e0e7343ba chore: release Apple & Gateway with ECN fix (#9013) 2025-05-02 00:16:40 -07:00
Thomas Eizinger
0aab954fa9 fix(connlib): never clear ECT from IP packets (#9009)
ECN information is helpful to allow the congestion controllers to more
easily fine-tune their send and receive windows. When a Firezone Client
receives an IP packet where the ECN bits signal an ECN capable
transport, we mirror this bit on the UDP datagram that carries the
encrypted IP packet.

When receiving a datagram with ECN bits set, the Gateway will then apply
these bits to the decrypted IP packet and pass it along towards its
destination.

This implementation is unfortunately a bit too naive. Not all devices on
the Internet support ECN and therefore, we may receive a datagram that
has its ECN bits cleared when the ECN bits on the inner IP packet still
signal an ECN capable transport. In this case, we should _not_ override
the ECN bits and instead pass the IP packet along as is. Network devices
along the path between Gateway and Resource may still use these ECN bits
to signal congestion.

We fix this by making the `with_ecn` function on `IpPacket` private. It
is not meant to be used outside of the module. We supersede it with a
`with_ecn_from_transport` function that implements the above logic.

---------

Signed-off-by: Thomas Eizinger <thomas@eizinger.io>
Co-authored-by: Jamil <jamilbk@users.noreply.github.com>
2025-05-02 05:28:19 +00:00
Thomas Eizinger
0fd14b993b chore(connlib): buffer most recent TCP SYN (#9004)
When establishing connections that take longer than the TCP RTO, we may
see duplicate TCP SYNs. Those have different timestamps from each other
but are otherwise equal. To provide more accurate timing information to
the TCP stack, we now keep the latest TCP SYN around instead of the very
first one.
2025-05-02 01:06:14 +00:00
Thomas Eizinger
ea5709e8da chore(rust): initialise OTEL with useful metadata (#8945)
Once we start collecting metrics across various Clients and Gateways,
these metrics need to be tagged with the correct `service.name`,
`service.version` as well as an instance ID to differentiate metrics
from different instances.
2025-05-01 05:19:07 +00:00
Thomas Eizinger
8dd794d8c8 chore(gateway): record metrics about dropped packets (#8942)
When a NAT session expires or other unallowed traffic is routed to the
Gateway, we drop these packets. It will be useful to learn, how often
that actually happens and what the reason is for why they got dropped.
To do so, we add a counter metric for these packets.

---------

Signed-off-by: Thomas Eizinger <thomas@eizinger.io>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-04-30 18:24:10 +00:00
Thomas Eizinger
6f11568c8c fix(connlib): move wire::dev::recv log to right location (#8944)
I don't understand why but in the current location, this log simply
doesn't show up for anything other than UDP packets. If we move it up,
it will actually log all packets.
2025-04-30 13:45:51 +00:00
Thomas Eizinger
e031dfdb4a refactor(connlib): introduce our own bufferpool crate (#8928)
We have been using buffer pools for a while all over `connlib` as a way
to efficiently use heap-allocated memory. This PR harmonizes the usage
of buffer pools across the codebase by introducing a dedicated
`bufferpool` crate. This crate offers a convenient and easy-to-use API
for all the things we (currently) need from buffer pools. As a nice
bonus of having it all in one place, we can now also track metrics of
how many buffers we have currently allocated.

An example output from the local metrics exporter looks like this:

```
Name         : system.buffer.count
Description  : The number of buffers allocated in the pool.
Unit         : {buffers}
Type         : Sum
Sum DataPoints
Monotonic    : false
Temporality  : Cumulative
DataPoint #0
	StartTime    : 2025-04-29 12:41:25.278436
	EndTime      : 2025-04-29 12:42:25.278088
	Value        : 96
	Attributes   :
		 ->  system.buffer.pool.name: udp-socket-v6
		 ->  system.buffer.pool.buffer_size: 65535
DataPoint #1
	StartTime    : 2025-04-29 12:41:25.278436
	EndTime      : 2025-04-29 12:42:25.278088
	Value        : 7
	Attributes   :
		 ->  system.buffer.pool.buffer_size: 131600
		 ->  system.buffer.pool.name: gso-queue
DataPoint #2
	StartTime    : 2025-04-29 12:41:25.278436
	EndTime      : 2025-04-29 12:42:25.278088
	Value        : 128
	Attributes   :
		 ->  system.buffer.pool.name: udp-socket-v4
		 ->  system.buffer.pool.buffer_size: 65535
DataPoint #3
	StartTime    : 2025-04-29 12:41:25.278436
	EndTime      : 2025-04-29 12:42:25.278088
	Value        : 8
	Attributes   :
		 ->  system.buffer.pool.buffer_size: 1336
		 ->  system.buffer.pool.name: ip-packet
DataPoint #4
	StartTime    : 2025-04-29 12:41:25.278436
	EndTime      : 2025-04-29 12:42:25.278088
	Value        : 9
	Attributes   :
		 ->  system.buffer.pool.buffer_size: 1336
		 ->  system.buffer.pool.name: snownet
```

Resolves: #8385
2025-04-30 08:52:18 +00:00
Thomas Eizinger
f7df445924 fix(gateway): don't invalidate active NAT sessions (#8937)
Whenever the Gateway is instructed to (re)create the NAT for a DNS
resource, it performs a DNS query and then overwrite the existing
entries in the NAT table. Depending on how the DNS records are defined,
this may lead to a very bad user experience where connections are cut
regularly.

In particular, if a service utilises round-robin DNS where a DNS query
only ever returns a single entry yet that entry may change as soon as
the TTL expires, all connections for this particular DNS resource for a
Client get cut.

To fix this, we now first check for active NAT sessions for a given
proxy IP and only replace it if we don't have an open NAT session. The
NAT sessions have a TTL of 1 minute, meaning there needs to be at least
1 outgoing packet from the Client every minute to keep it open.
2025-04-30 06:58:58 +00:00
Jamil
2650d81444 chore: release clients with GSO fix (#8936) 2025-04-29 23:52:43 -07:00
Thomas Eizinger
c75b6c6641 feat(connlib): record the number of IO errors as a metric (#8934)
It will be interesting to learn for example, how many installations have
no IPv6 connectivity as those will encounter `NetworkUnreachable`
errors. We categorise the errors by IO direction and IP stack which will
allow us to deduce this information.
2025-04-30 05:24:55 +00:00
Thomas Eizinger
6dc5f85cc5 fix(connlib): don't buffer when recreating DNS resource NAT (#8935)
In order to detect changes to DNS records of DNS resources, `connlib`
will recreate the DNS resource NAT whenever it receives a query for a
DNS resource. The way we implemented this was by clearing the local
state of the DNS resource NAT, which triggered us to perform the
handshake with the Gateway again upon the next packet for this resource.
The Gateway would then perform the DNS query and respond back when this
was finished.

In order to not drop any packets, `connlib` has a buffer where it keeps
the packets that are arriving in the meantime. This works reasonably
well when the connection is first set up because we are only buffering a
TCP SYN or equivalent handshake packet. Yet, when the connection is full
use, and the application just so happens to make another DNS query, we
halt the entire flow of packets until this is confirmed again. To
prevent high memory use, the buffer for this packets is constrained to
32 packets which is nowhere near enough when a connection is actively
transferring data (like a file upload).

In most cases, the DNS query on the Gateway will yield the exact same
results as because the records haven't changed. Thus, there is no reason
for us to actually halt the flow of these packets when we are
_recreating_ the DNS resource NAT. That way, this handshake happens in
parallel to the actual packet flow and does not interrupt anything in
the happy path case.
2025-04-30 04:26:49 +00:00
Thomas Eizinger
d19d20da51 fix(connlib): send IO errors from UDP threads to event-loop (#8933)
With #7590, we've moved all UDP IO operations to a separate thread. As a
result, some of the error handling of IO errors within the Client's and
Gateway's event-loop no longer applied as those are now captured within
the respective thread. To fix this, we extend the type-signature of the
receive-channel to also allow for errors and use that to send back
errors from sending AND receiving UDP datagrams.
2025-04-30 02:00:41 +00:00
Thomas Eizinger
2ba7a87899 feat(connlib): add FFI for changing log-level on MacOS (#8927)
This isn't plugged into anything yet on the Swift side but lays the
foundation for changing the log-level at runtime without having to sign
the user out.
2025-04-29 13:51:46 +00:00
Thomas Eizinger
66b7ca6f7f fix(connlib): ensure we don't mistake SYN-ACK for SYN (#8922)
This shouldn't matter because we are only using the `UniquePacketBuffer`
on the client and not on the Gateway where SYN-ACK packets would be sent
from. To be fully correct though, we need to also compare the ACK flag
of the two packets.
2025-04-29 04:17:18 +00:00
Thomas Eizinger
fde8d08423 fix(connlib): maintain packet order across GSO batches (#8920)
Despite our efforts in #8912, the current implementation still does not
do enough to maintain packet ordering across GSO batches.

At present, we very aggressively batch packets of the same length
together. This however is too eager when we consider packet flows such
as the following:

```
9:03:49.585143 IP 10.128.15.241.3000 > 100.69.109.138.53474: Flags [.], seq 1:1229, ack 524, win 249, options [nop,nop,TS val 3862031964 ecr 1928356896], length 1228
09:03:49.585151 IP 10.128.15.241.3000 > 100.69.109.138.53474: Flags [P.], seq 1229:2063, ack 524, win 249, options [nop,nop,TS val 3862031964 ecr 1928356896], length 834
09:03:49.585157 IP 10.128.15.241.3000 > 100.69.109.138.53474: Flags [P.], seq 2063:3094, ack 524, win 249, options [nop,nop,TS val 3862031964 ecr 1928356896], length 1031
09:03:49.585187 IP 10.128.15.241.3000 > 100.69.109.138.53474: Flags [.], seq 3094:4322, ack 524, win 249, options [nop,nop,TS val 3862031964 ecr 1928356896], length 1228
09:03:49.585188 IP 10.128.15.241.3000 > 100.69.109.138.53474: Flags [P.], seq 4322:5156, ack 524, win 249, options [nop,nop,TS val 3862031964 ecr 1928356896], length 834
09:03:49.585227 IP 10.128.15.241.3000 > 100.69.109.138.53474: Flags [.], seq 5156:6384, ack 524, win 249, options [nop,nop,TS val 3862031964 ecr 1928356896], length 1228
09:03:49.585228 IP 10.128.15.241.3000 > 100.69.109.138.53474: Flags [P.], seq 6384:7612, ack 524, win 249, options [nop,nop,TS val 3862031964 ecr 1928356896], length 1228
09:03:49.585230 IP 10.128.15.241.3000 > 100.69.109.138.53474: Flags [P.], seq 7612:8249, ack 524, win 249, options [nop,nop,TS val 3862031964 ecr 1928356896], length 637
09:03:49.585846 IP 10.128.15.241.3000 > 100.69.109.138.53474: Flags [.], seq 8249:9477, ack 524, win 249, options [nop,nop,TS val 3862031964 ecr 1928356896], length 1228
09:03:49.585851 IP 10.128.15.241.3000 > 100.69.109.138.53474: Flags [P.], seq 9477:10705, ack 524, win 249, options [nop,nop,TS val 3862031964 ecr 1928356896], length 1228
```

As we can see here, the remote sends us packet batches of varying
lengths:

- 1228, 834
- 1031
- 1228, 834
- 1228, 1228, 637
- 1228, 1228

1228 represents a "full" TCP packet so any packet following a
full-packet SHOULD be grouped together into a GSO batch.

Currently, we are batching all the 1228 packets together and we ignore
the fact that there were actually smaller sized packets inbetween those
that belong together.

To mitigate this, we refactor the `GsoQueue` to remove the
`segment_size` from the binning key of our map and instead only group
batches by their source, destination and ECN information. Within such a
connection, we then create an ordered list of batches. A new batch is
started if the length differs or we have previously pushed a packet that
isn't of the length of the batch, therefore signalling the end of the
batch.

The result here looks very promising (this is loading
`blog.firezone.dev` via the `lynx` browser from within the
headless-client docker container, so going through a Gateway running
this PR):

|main|this PR|
|---|---|
|![Screenshot From 2025-04-29
10-32-00](https://github.com/user-attachments/assets/ba0535e4-1df9-4601-a2d7-ba099ba2313f)|![image](https://github.com/user-attachments/assets/ab2ccec7-ce96-4305-8514-2e43d82ecc7d)|

Related: #8899
2025-04-29 00:50:23 +00:00
Thomas Eizinger
e0faddf43f chore(connlib): maintain order within a single GSO batch (#8912)
Generic Segmentation Offload (GSO) is a clever way of reducing the
number of syscalls made when a you want to send a lot of packets with
the same length to the same recipient. The way this works is that the
packets are concatenated and passed to the kernel as a single packet
together with the `segment_size` as an out-of-band argument.

The component managing this batching in `connlib` is called `GsoQueue`.
In #8772, we made the order in which these batches are sent to the
kernel explicit by prioritising batches with smaller segments. What we
overlooked with that strategy is that in a particular GSO batch, the
last packet is actually allowed to be of a different length.

For example, say the user is downloading an image of 4500Kb. With our
MTU of 1280, we have a payload size of 1252. This results in three
fully-filled packets and one packet of 744 bytes. With the change in
#8772, the small packet of 744 bytes will be transferred first, followed
by the "train" of fully filled packets.

To fix this, we flip the order here and transfer batches or larger sizes
first. The original problem we attempted to mitigate in #8772 no longer
exists now that we merged #7590. We will simply suspend now if the UDP
socket isn't ready contrary to dropping the next batch.

By flipping the order here, we guarantee that batches with a larger size
are sent before batches with a smaller size. This should also imply that
the encapsulated IP packets of e.g. an image arrive in the correct order
(with the smallest packet last as it is part of a smaller batch). What
we don't guarantee with this is that there won't be any other IP packets
sent "in the middle" of such a batch. This shouldn't be a problem though
as we are simply interleaving packets of different TCP / UDP connections
with each other which already happens on the regular Internet anyway.
2025-04-28 06:53:43 +00:00
Thomas Eizinger
ac5e44d5d0 feat(connlib): request larger buffers for UDP sockets (#8731)
Sufficiently large receive buffers are important to sustain
high-throughput as latency increases. If the receive buffer in the
kernel is too small, packets need to be dropped on arrival.

Firefox uses 1MB in its QUIC stack [0]. `quic-go` recommends to set send
and receive buffers to 7.5 MB [1]. Power users of Firezone are likely
receiving a lot more traffic than the average Firefox user (especially
with Internet Resource activated) so setting it to 10 MB seems
reasonable. Sending packets is likely not as critical because we have
back-pressure through our system such that we will stop reading IP
packets when we cannot write to our UDP socket. The UDP socket is
sitting in a separate thread and those threads are connected with
dedicated queues which act as another buffer. However, as the data below
shows, some systems have really small send buffers which are currently
likely a speed bottleneck because we need to suspend writing so
frequently.

Assuming a 50ms latency, the bandwidth-delay product tells us that we
can (in theory) saturate a 1.6 Gbps link with a 10MB receive buffer
(assuming the OS also has large enough buffer sizes in its TCP or QUIC
stack):

```
80 Mb / 0.05s = 1600Mbps
```

Experiments and research [2] show the following:

|OS|Receive buffer (default)|Receive buffer (this PR)|Send buffer
(default)|Send buffer (this PR)|
|---|---|---|---|---|
|Windows|65KB|10MB|65KB|1MB|
|MacOS|786KB|8MB|9KB|1MB|
|Linux|212KB|212KB|212KB|212KB|

With the exception of Linux, the OSes appear to be quite generous with
how big they allow receive buffers to be. On Linux, these limit can be
changed by setting the `core.net.rmem_max` and `core.net.wmem_max`
parameters using `sysctl`.

Most of our users are on Windows and MacOS, meaning they immediately
benefit from this without having to change any system settings. Larger
client-side UDP receive buffers are critical for any "download" scenario
which is likely the majority of usecases that Firezone is used for.

On Windows, increasing this receive buffer almost doubles the throughput
in an iperf3 download test.

[0]: https://github.com/mozilla/neqo/pull/2470
[1]: https://github.com/quic-go/quic-go/wiki/UDP-Buffer-Sizes
[2]: https://unix.stackexchange.com/a/424381

---------

Signed-off-by: Thomas Eizinger <thomas@eizinger.io>
Co-authored-by: Jamil <jamilbk@users.noreply.github.com>
2025-04-22 06:52:33 +00:00
Thomas Eizinger
d2e275be56 feat(connlib): classify UDP traffic by protocol (#8886)
It creates a bit of duplication with code that we have in `snownet` but
it is code that is unlikely to change because the protocols are already
standarised. Contrary to recording the port, the cardinality of these
protocols is much fixed to a much smaller range which will allow us to
safely record these metrics in an actual time-series database further
down the line whilst still reasoning about how much traffic we are
sending over TURN, as STUN or as WireGuard.
2025-04-22 01:35:38 +00:00
Jamil
5db8e20f3b chore: release Apple and GUI clients (#8882)
- Apple clients 1.4.12
- GUI clients 1.4.11
2025-04-21 21:45:16 +00:00
Jamil
368ace2c6e ci: Release Android 1.4.7 (#8878)
App is live on Play store.
2025-04-21 21:12:27 +00:00
Thomas Eizinger
4c5fd9b256 feat(connlib): prefer relay candidates of same IP version (#8798)
When calculating preferences for candidates, `str0m` currently always
prefer IPv6 over IPv4. This is as per the ICE spec. Howver, this can
lead to sub-optimal situations when a connection ends up using a TURN
server.

TURN allows a client to allocate an IPv4 and an IPv6 address in the same
allocation. This makes it possible for e.g. an IPv4-only client to
connect to an IPv6-only peer as long as the TURN server runs in
dual-stack AND the client requests an IPv6 address in addition to an
IPv4 address with the `ADDITIONAL-ADDRESS-FAMILY` attribute.

Assume that a client sits behind symmetric NAT and therefore needs to
rely on a TURN server to communicate with its peers. The TURN server as
well as all the peers operate in dual-stack mode.

The current priority calculation will yield a communication path that
uses IPv4 to talk to the TURN server (as that is the only one available)
but due to the preference ordering of IPv6 over IPv4, will use an IPv6
path to the peer, despite the peer also supporting IPv4.

This isn't a problem per-se but makes our life unnecessarily difficult.
Our TURN servers use eBPF to efficiently deal with TURN's channel-data
messages. This however is at present only implemented for the IPv4 <>
IPv4 and IPv6 <> IPv6 path. Implementing the other paths is possible but
complicates the eBPF code because we need to also translate IP headers
between versions and not just update the source and destination IPs.

We have since patched `str0m` to extend the `Candidate::relayed`
constructor to also take a `base` address which is - similar to the
other candidate types - the address the client is sending from in order
to use this candidate. In the context of relayed candidates, this is the
address the client is using to talk to the TURN server. We can use this
information in the candidate's priority calculation to prefer candidates
that allow traffic to remain within one IP version, i.e. if the client
talks to the TURN server over IPv4, the candidate with an allocated IPv4
address will have a higher priority than the one with the IPv6 address
because we are applying a "punishment" factor as part of the
local-preference component in the priority formula.

Staying within the same IP version whilst relaying traffic allows our
TURN servers to use their eBPF kernel which results in a better UX due
to lower latency and higher throughput.

The final candidate ordering is ultimately decided by the controlling
ICE agent which in our case is the Firezone Client. Thus, we don't
necessarily need to update Gateways in order to test / benefit from
this. Building a Client with this patch included should be enough to
benefit from this change.

Related: https://github.com/algesten/str0m/pull/640
Related: https://github.com/algesten/str0m/pull/644
2025-04-20 22:41:56 +00:00
Thomas Eizinger
92534ae4ec test(connlib): ensure we have gaps in port ranges (#8862)
This should fix the flaky proptest.
2025-04-20 11:02:55 +00:00
Thomas Eizinger
ae4b7d9d08 test(connlib): correctly assert in Io unit-test (#8861)
Not sure what I was smoking when I wrote this test but the current
assertion makes no sense for what we actually want to test.

As the test name says, we want to assert that if we are given an
`Instant` in the past, we do in fact return a more recent one and
therefore what is returned in `Input::Timeout` is at least as recent as
`now`.
2025-04-19 22:17:51 +00:00
Jamil
5669c83835 ci: Bump Apple clients to 1.4.11 (#8848)
Includes a fix for auto-starting on launch when other VPN clients have
been connected previously.
2025-04-19 11:45:42 +00:00
Jamil
a2e32a4918 ci: Bump apple to 1.4.10 to ship PKG (#8797)
This publishes the 1.4.10 permalinks for the PKG download.
2025-04-17 15:13:44 +00:00
Jamil
aab691a67f ci: Release Apple clients 1.4.9 (#8793)
These contain the recent UDP thread enhancements.
2025-04-15 20:14:43 +00:00
Jamil
743f5fdfeb ci: bump clients/gateway to ship write improvements (#8792)
Signed-off-by: Jamil <jamilbk@users.noreply.github.com>
Co-authored-by: Thomas Eizinger <thomas@eizinger.io>
2025-04-15 06:21:23 +00:00
Thomas Eizinger
901207b274 chore(rust): remove stale error context (#8787)
Minor oversight from #8783. We accidentally retained this `.context`
even though there are now multiple error paths from the `Eventloop`, not
just portal connection errors.
2025-04-14 20:35:11 -07:00
Thomas Eizinger
7f5a81cc5a chore(rust-ffi): log non-authentication errors on error (#8785)
In the FFI layer, it is tricky to decide what we should do with errors.
On the one hand, logging and returning errors is an anti-pattern because
it may lead to duplicate logs. In this particular case however, it is
useful to log the error on the Rust side because it allows our Sentry
integration to capture and include the DEBUG logs prior to this one
which may add crucial context.
2025-04-15 02:28:09 +00:00
Thomas Eizinger
7c2163ddf4 fix(connlib): fail event-loops if UDP threads stop (#8783)
The UDP socket threads added in #7590 are designed to never exit. UDP
sockets are stateless and therefore any error condition on them should
be isolated to sending / receiving a particular datagram. It is however
possible that code panics which will shut down the threads
irrecoverably. In this unlikely event, `connlib`'s event-loop would keep
spinning and spam the log with "UDP socket stopped". There is no good
way on how we can recover from such a situation automatically, so we
just quit `connlib` in that case and shut everything down.

To model this new error path, we refactor the `DisconnectError` to be
internally backed by `anyhow`.
2025-04-15 02:27:37 +00:00
Thomas Eizinger
b3746b330f refactor(connlib): spawn dedicated threads for UDP sockets (#7590)
Correctly implementing asynchronous IO is notoriously hard. In order to
not drop packets in the process, one has to ensure a given socket is
ready to accept packets, buffer them if it is not case, suspend
everything else until the socket is ready and then continue.

Until now, we did this because it was the only option to run the UDP
sockets on the same thread as the actual packet processing. That in turn
was motivated by wanting to pass around references of the received
packets for processing. Rust's borrow-checker does not allow to pass
references between threads which forced us to have the sockets on the
same thread as the packet processing.

Like we already did in other places in `connlib`, this can be solved
through the use of buffer pools. Using a buffer pool, we can use heap
allocations to store the received packets without having to make a new
allocation every time we read new packets. Instead, we can have a
dedicated thread that is connected to `connlib`'s packet processing
thread via two channels (one for inbound and one for outbound packets).
These channels are bounded, which ensures backpressure is maintained in
case one of the two threads lags behind. These bounds also mean that we
have at most N buffers from the buffer pool in-flight (where N is the
capacity of the channel).

Within those dedicated threads, we can then use `async/await` notation
to suspend the entire task when a socket isn't ready for sending.

Resolves: #8000
2025-04-14 06:18:06 +00:00
Thomas Eizinger
859aa3cee0 feat(connlib): add context to event-loop errors (#8773)
This should make it easier to diagnose any error returned from the
event-loop.
2025-04-14 00:07:27 +00:00
Thomas Eizinger
19d954c76c fix(connlib): prioritise GSO batches with smaller segments (#8772)
In order to implement GSO in `connlib`, we opted for an approach where
packets of the same length are being appended to a buffer. Each of these
buffers is the sent to the kernel in a single syscall, which drastically
decreases the per-packet overhead of syscalls and therefore improves
performance.

Within `connlib` itself, we prioritise control-protocol associated
packets over tunnel traffic. The idea here is that even under high-load,
we want to ensure that STUN probes between the peers and to the relays
are sent in a timely manner. Failing to send these probes results in a
false-positive detection of a lost connection because the `connlib`'s
internal state uses timeouts to detect such situations.

Despite processing the packets itself in a timely manner, it is still
possible that they get delayed depending on which order the get flushed
to the socket. This order is currently non-deterministic because
`GsoQueue` uses a `HashMap` internally and when accessing the
batched-together datagrams, we just access it via `iter_mut`.

To fix this, we use a `BTreeMap` instead and explicitly define the `Key`
to start with the `segment_size` field. As a result, entries within the
`BTreeMap` will be sorted ascending by `segment_size` (i.e. the size of
individual packets within the batch). Packets of smaller size are more
likely to be control messages like STUN binding requests or TURN
messages to the relays for managing allocations.

By sorting the map explicitly, we ensure that if the UDP socket is ready
to send, we flush out these messages first before moving on to bigger
packets such as the ones containing (more likely) WireGuard data
messages.
2025-04-14 00:04:39 +00:00