Skip to content

Send immediate handshake when endpoint address changes#148

Open
pappz wants to merge 1 commit into
WireGuard:masterfrom
netbirdio:immediate-handshake-on-endpoint-change
Open

Send immediate handshake when endpoint address changes#148
pappz wants to merge 1 commit into
WireGuard:masterfrom
netbirdio:immediate-handshake-on-endpoint-change

Conversation

@pappz

@pappz pappz commented Jun 30, 2026

Copy link
Copy Markdown

Summary

The goal is to bring peer connections up as quickly as possible. Setting
a peer's endpoint via UAPI gives us a fresh, reachable address and is a
potential trigger for initiating a handshake.

The problem happens when the peer is already retrying a handshake with
its old, no longer valid address. The retry timer is still tied to that
old endpoint, and the RekeyTimeout throttle blocks a new handshake.
Because of this, a new working address can still wait up to one
RekeyTimeout (~5s) before a handshake is started. This delay is
unnecessary because the timer belongs to the old endpoint.

Change

This change starts a handshake from handlePostConfig() when the endpoint
address changes, so the new address is used immediately. To do this, it
skips the RekeyTimeout throttle on purpose and resets handshakeAttempts.
This is safe because the throttle was counting retries to the old
endpoint, which no longer matters.

It makes sure we only start a handshake when there is no usable session.
A healthy active session does not trigger an unnecessary rekey after an
address change.

Only changes to an existing endpoint trigger this path. Initial endpoint
setup and no-op updates keep using SendStagedPackets().

When a peer's endpoint is changed via UAPI to a new, non-nil address
while a handshake retry cycle is still mid-RekeyTimeout, force a
handshake initiation right away instead of waiting out the remaining
~5s. The retry timer and the RekeyTimeout throttle are tied to the old,
no-longer-valid endpoint, so the throttle is skipped on purpose:
SendHandshakeInitiationOnEndpointChange() resets handshakeAttempts and
calls sendHandshakeInitiation() directly, bypassing the RekeyTimeout
check in SendHandshakeInitiation().

The send is guarded by needsHandshake() so an address change on a live,
working session does not trigger an unnecessary rekey. Only an actual
change of an already-set endpoint takes this path; first-time endpoint
assignment and no-op reassignment keep using SendStagedPackets().

Signed-off-by: Zoltán Papp <zoltan.pmail@gmail.com>
@pappz

pappz commented Jun 30, 2026

Copy link
Copy Markdown
Author

I sent the same patch to the mailing list too, but I did not get a denial or any feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant