forked from cloudflare/boringtun
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: simplify passive keepalive (#70)
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.
- Loading branch information
1 parent
0b87029
commit 98efc13
Showing
2 changed files
with
102 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters