You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
WireGuard has a passive keepalive mechanism. To quote the spec:
> If a peer has received a validly-authenticated transport data message
(section 5.4.6), but does not have any packets
itself to send back for Keepalive-Timeout seconds, it sends a keepalive
message.
This is currently implemented correctly in `boringtun` but it is
somewhat convoluted.
1. On various parts in the code, the internal timers are ticked over.
For example, when we receive keepalive messages or data messages.
2. Whether or not we should send a passive keepalive is tracked in the
`want_keepalive` boolean.
3. This boolean is set whenever we receive _any_ packet (including
keepalives). This is a bug.
4. The above bug is mitigated because of an additional condition that
the last received data packet must be after the last sent packet.
5. Lastly, the `want_keepalive` boolean is checked and directly set to
false as part of our timer code. Combining these two things makes the
code hard to reason about.
We can simplify this greatly by directly tracking the timestamp, when a
keepalive is due. The new `want_keepalive_at` timer is set every time we
receive a data packet and cleared every time we send a packet. In
`update_timers_at`, we simply check if `now` has surpassed that timer
and send a keepalive if that is the case.
As a bonus, this functionality is now also unit-tested.
0 commit comments