kernel/linux/src: let userspace clear a peer's session

When tailscaled learns that a peer restarted (its disco key changed), the
peer has lost its WireGuard keys, but the kernel would keep sending with
the dead session until WireGuard's own timers start a new handshake about
15 seconds later.

Add TSWGPEER_A_CLEAR_SESSION, an NLA_FLAG in the nested WGPEER_A_TSWG
attribute, which forgets the peer's keypairs and any handshake in
progress, as wg_pm_notification does for every peer before suspend, and
resets the handshake rate limit, so that the next packet to the peer is
staged and starts a new handshake right away.

Updates #426

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: Ia9821a47e6c8114bbabd81d8d5a2be528295e74f
This commit is contained in:
Brad Fitzpatrick committed 2026-11-07 05:00:03 +00:00
1 parent fe0c4864e9
commit c32ba694d1
3 files changed
+17

No files matched your search

+1
View File
@@ -27,6 +27,7 @@ const struct nla_policy wireguard_wgallowedip_nl_policy[WGALLOWEDIP_A_FLAGS + 1]
*/
const struct nla_policy tswg_peer_nl_policy[TSWGPEER_A_MAX + 1] = {
[TSWGPEER_A_FLAGS] = NLA_POLICY_MASK(NLA_U32, TSWGPEER_F_NO_ROAM),
[TSWGPEER_A_CLEAR_SESSION] = { .type = NLA_FLAG, },
};
const struct nla_policy wireguard_wgpeer_nl_policy[WGPEER_A_TSWG + 1] = { /* TSWG */
@@ -107,6 +107,14 @@ enum tswgpeer_flag {
enum tswgpeer_attribute {
TSWGPEER_A_UNSPEC,
TSWGPEER_A_FLAGS, /* NLA_U32 of enum tswgpeer_flag; set replaces all */
/*
* NLA_FLAG, set only: forget the peer's session keys and any
* handshake in progress, as on suspend, so that the next packet to the
* peer starts a new handshake right away. For when userspace learns
* that the peer restarted (and so lost its keys), such as from a new
* disco key.
*/
TSWGPEER_A_CLEAR_SESSION,
__TSWGPEER_A_LAST
};
+8
View File
@@ -655,6 +655,14 @@ int tswg_set_peer(struct wg_peer *peer, struct nlattr *attr)
WRITE_ONCE(peer->tswg_no_roam, !!(flags & TSWGPEER_F_NO_ROAM));
}
if (tb[TSWGPEER_A_CLEAR_SESSION]) {
/* As wg_pm_notification does for every peer before suspend. */
timer_delete(&peer->timer_zero_key_material);
wg_noise_handshake_clear(&peer->handshake);
wg_noise_keypairs_clear(&peer->keypairs);
/* Don't make the next handshake wait for REKEY_TIMEOUT. */
wg_noise_reset_last_sent_handshake(&peer->last_sent_handshake);
}
return 0;
}