From 189b463b860f47903bbffb30c1e22ea53e24de99 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Fri, 6 Nov 2026 23:07:32 +0000 Subject: [PATCH] kernel/linux/src: add a per-peer flag to stop roaming Upstream WireGuard moves a peer's endpoint to the source of each authenticated packet. In Tailscale, disco picks the path, so that would fight it: a packet that arrives via DERP would steal the endpoint from a good direct path. Add TSWGPEER_F_NO_ROAM, set through TSWGPEER_A_FLAGS nested in a new WGPEER_A_TSWG attribute (numbered 64, like WGDEVICE_A_TSWG) and reported in dumps. With it set, the endpoint learned from handshake initiations, handshake responses and data packets is ignored once the peer has an endpoint, and only userspace changes it. A peer with no endpoint yet still gets one from its first authenticated packet, so it stays reachable if it contacts us before userspace has configured one. The gate, tswg_peer_may_roam, sits at the two learned-endpoint paths rather than in wg_socket_set_peer_endpoint, which netlink also uses for explicit updates. The flag is applied before WGPEER_A_ENDPOINT in the same request, so pinning and setting an endpoint are one step. Tested by TestTSWG{Gokrazy,Ubuntu}NoRoam in tailscale.com's natlab vmtests, which fail as expected with the gate disabled. Updates #426 Signed-off-by: Brad Fitzpatrick Change-Id: I621a6e86d3947e154c4cfa582aa845bb49d42b2f --- kernel/linux/DESIGN.md | 32 ++++++++++++-- kernel/linux/src/generated/netlink.c | 11 ++++- kernel/linux/src/generated/netlink.h | 3 +- kernel/linux/src/include/uapi/linux/tswg.h | 23 ++++++++++ kernel/linux/src/netlink.c | 51 ++++++++++++++++++++-- kernel/linux/src/peer.h | 16 +++++++ kernel/linux/src/receive.c | 3 +- kernel/linux/src/socket.c | 2 + kernel/linux/src/tswg.h | 3 ++ 9 files changed, 134 insertions(+), 10 deletions(-) diff --git a/kernel/linux/DESIGN.md b/kernel/linux/DESIGN.md index 76d3fde46..3bf12f83d 100644 --- a/kernel/linux/DESIGN.md +++ b/kernel/linux/DESIGN.md @@ -241,9 +241,33 @@ Details and decisions: Upstream WireGuard roams a peer's endpoint to the source of the latest authenticated packet. That fights disco's path selection: a packet that -arrives via DERP would steal the endpoint from a good direct path. tswg -adds a per-peer flag (or device-wide default) that disables auto-roaming; -tailscaled sets endpoints explicitly. +arrives via DERP would steal the endpoint from a good direct path. +Implemented 2026-11-06: + +* A per-peer flag, `TSWGPEER_F_NO_ROAM`, in `TSWGPEER_A_FLAGS` nested in + `WGPEER_A_TSWG` (= 64, like the device attribute). Setting + `TSWGPEER_A_FLAGS` replaces all of the peer's tswg flags. Dumps report + it. Go: `tswg.PeerConfig.NoRoam` and `tswg.Peer.NoRoam`. +* Upstream learns endpoints in three places: on a valid handshake + initiation and a valid handshake response (via + `wg_socket_set_peer_endpoint_from_skb`) and on each authenticated data + packet (`wg_packet_consume_data_done` calls + `wg_socket_set_peer_endpoint` directly). `tswg_peer_may_roam()` (in + `peer.h`) gates the learned updates, not `wg_socket_set_peer_endpoint` + itself, which netlink also uses for explicit updates. +* With the flag set, the first authenticated packet still sets the + endpoint if the peer doesn't have one yet; after that only userspace + changes it. That keeps a peer reachable if it contacts us before + tailscaled has given it an endpoint. +* The flag is applied before `WGPEER_A_ENDPOINT` in the same request, so + pinning and setting an endpoint are one step. +* Per-peer rather than device-wide, for now. tailscaled will set it on + every peer; a device-wide default is easy to add if per-peer turns out + to be a hassle. +* Pinning also stops learning the local source address that upstream + records alongside the endpoint (for replying from the same local IP on + multihomed hosts); the kernel picks the source address by routing, as + in userspace Tailscale. Changing a peer's endpoint does not re-key or reset the noise session in upstream WireGuard either; that is already fine. @@ -423,7 +447,7 @@ Everything else: detaches; a new fd reattaches (`TestTSWG{Gokrazy,Ubuntu}SharedSocket`). - [ ] tailscaled: keep the shared sockets in systemd's fd store across restarts. -- [ ] Per-peer no-auto-roam flag. +- [x] Per-peer no-auto-roam flag (`TestTSWG{Gokrazy,Ubuntu}NoRoam`). - [ ] Loopback DERP endpoints; vmtest with DERP-only path. - [ ] tailscaled integration (wgengine backend that programs tswg) on a tailscale.com branch. diff --git a/kernel/linux/src/generated/netlink.c b/kernel/linux/src/generated/netlink.c index 255b1d6ce..ab62a37e3 100644 --- a/kernel/linux/src/generated/netlink.c +++ b/kernel/linux/src/generated/netlink.c @@ -21,7 +21,15 @@ const struct nla_policy wireguard_wgallowedip_nl_policy[WGALLOWEDIP_A_FLAGS + 1] [WGALLOWEDIP_A_FLAGS] = NLA_POLICY_MASK(NLA_U32, 0x1), }; -const struct nla_policy wireguard_wgpeer_nl_policy[WGPEER_A_PROTOCOL_VERSION + 1] = { +/* + * TSWG: not generated. Hand-written policy for the nested WGPEER_A_TSWG + * attribute; see the TODO in DESIGN.md about forking the YNL spec. + */ +const struct nla_policy tswg_peer_nl_policy[TSWGPEER_A_MAX + 1] = { + [TSWGPEER_A_FLAGS] = NLA_POLICY_MASK(NLA_U32, TSWGPEER_F_NO_ROAM), +}; + +const struct nla_policy wireguard_wgpeer_nl_policy[WGPEER_A_TSWG + 1] = { /* TSWG */ [WGPEER_A_PUBLIC_KEY] = NLA_POLICY_EXACT_LEN(WG_KEY_LEN), [WGPEER_A_PRESHARED_KEY] = NLA_POLICY_EXACT_LEN(WG_KEY_LEN), [WGPEER_A_FLAGS] = NLA_POLICY_MASK(NLA_U32, 0x7), @@ -32,6 +40,7 @@ const struct nla_policy wireguard_wgpeer_nl_policy[WGPEER_A_PROTOCOL_VERSION + 1 [WGPEER_A_TX_BYTES] = { .type = NLA_U64, }, [WGPEER_A_ALLOWEDIPS] = NLA_POLICY_NESTED_ARRAY(wireguard_wgallowedip_nl_policy), [WGPEER_A_PROTOCOL_VERSION] = { .type = NLA_U32, }, + [WGPEER_A_TSWG] = NLA_POLICY_NESTED(tswg_peer_nl_policy), /* TSWG */ }; /* WG_CMD_GET_DEVICE - dump */ diff --git a/kernel/linux/src/generated/netlink.h b/kernel/linux/src/generated/netlink.h index 7b0565c6f..78f5f852d 100644 --- a/kernel/linux/src/generated/netlink.h +++ b/kernel/linux/src/generated/netlink.h @@ -16,10 +16,11 @@ /* Common nested types */ extern const struct nla_policy wireguard_wgallowedip_nl_policy[WGALLOWEDIP_A_FLAGS + 1]; -extern const struct nla_policy wireguard_wgpeer_nl_policy[WGPEER_A_PROTOCOL_VERSION + 1]; +extern const struct nla_policy wireguard_wgpeer_nl_policy[WGPEER_A_TSWG + 1]; /* TSWG */ /* TSWG: hand-written, not generated. */ extern const struct nla_policy tswg_device_nl_policy[TSWGDEVICE_A_MAX + 1]; +extern const struct nla_policy tswg_peer_nl_policy[TSWGPEER_A_MAX + 1]; /* Ops table for wireguard */ extern const struct genl_split_ops wireguard_nl_ops[2]; diff --git a/kernel/linux/src/include/uapi/linux/tswg.h b/kernel/linux/src/include/uapi/linux/tswg.h index ac31e45c3..01ae78820 100644 --- a/kernel/linux/src/include/uapi/linux/tswg.h +++ b/kernel/linux/src/include/uapi/linux/tswg.h @@ -89,6 +89,29 @@ enum wgpeer_attribute { }; #define WGPEER_A_MAX (__WGPEER_A_LAST - 1) +/* tswg additions; see WGDEVICE_A_TSWG. */ +#define WGPEER_A_TSWG 64 /* NLA_NESTED, of enum tswgpeer_attribute */ + +enum tswgpeer_flag { + /* + * Don't update the peer's endpoint from incoming packets once it has + * one; only userspace changes it. Upstream WireGuard roams to the + * source of the latest authenticated packet, which would fight + * tailscaled's path selection (a packet arriving via DERP would steal + * the endpoint from a good direct path). If the peer has no endpoint + * yet, the first authenticated packet still sets one. + */ + TSWGPEER_F_NO_ROAM = 1, +}; + +enum tswgpeer_attribute { + TSWGPEER_A_UNSPEC, + TSWGPEER_A_FLAGS, /* NLA_U32 of enum tswgpeer_flag; set replaces all */ + + __TSWGPEER_A_LAST +}; +#define TSWGPEER_A_MAX (__TSWGPEER_A_LAST - 1) + enum wgallowedip_attribute { WGALLOWEDIP_A_UNSPEC, WGALLOWEDIP_A_FAMILY, diff --git a/kernel/linux/src/netlink.c b/kernel/linux/src/netlink.c index 6f053b665..79b2cb1ed 100644 --- a/kernel/linux/src/netlink.c +++ b/kernel/linux/src/netlink.c @@ -114,7 +114,8 @@ get_peer(struct wg_peer *peer, struct sk_buff *skb, struct dump_ctx *ctx) WGPEER_A_UNSPEC) || nla_put_u64_64bit(skb, WGPEER_A_RX_BYTES, peer->rx_bytes, WGPEER_A_UNSPEC) || - nla_put_u32(skb, WGPEER_A_PROTOCOL_VERSION, 1)) + nla_put_u32(skb, WGPEER_A_PROTOCOL_VERSION, 1) || + tswg_get_peer(skb, peer)) goto err; read_lock_bh(&peer->endpoint_lock); @@ -418,6 +419,13 @@ static int set_peer(struct wg_device *wg, struct nlattr **attrs) up_write(&peer->handshake.lock); } + /* TSWG: before the endpoint, so that pinning and setting it are one step. */ + if (attrs[WGPEER_A_TSWG]) { + ret = tswg_set_peer(peer, attrs[WGPEER_A_TSWG]); + if (ret < 0) + goto out; + } + if (attrs[WGPEER_A_ENDPOINT]) { struct sockaddr *addr = nla_data(attrs[WGPEER_A_ENDPOINT]); size_t len = nla_len(attrs[WGPEER_A_ENDPOINT]); @@ -570,11 +578,11 @@ int wg_set_device_doit(struct sk_buff *skb, struct genl_info *info) skip_set_private_key: if (info->attrs[WGDEVICE_A_PEERS]) { - struct nlattr *attr, *peer[WGPEER_A_MAX + 1]; + struct nlattr *attr, *peer[WGPEER_A_TSWG + 1]; /* TSWG */ int rem; nla_for_each_nested(attr, info->attrs[WGDEVICE_A_PEERS], rem) { - ret = nla_parse_nested(peer, WGPEER_A_MAX, attr, + ret = nla_parse_nested(peer, WGPEER_A_TSWG, attr, NULL, NULL); if (ret < 0) goto out; @@ -632,6 +640,43 @@ int tswg_set_device(struct wg_device *wg, struct nlattr *attr, return 0; } +/* TSWG: tswg_set_peer applies the nested WGPEER_A_TSWG attribute to peer. */ +int tswg_set_peer(struct wg_peer *peer, struct nlattr *attr) +{ + struct nlattr *tb[TSWGPEER_A_MAX + 1]; + int ret; + + ret = nla_parse_nested(tb, TSWGPEER_A_MAX, attr, tswg_peer_nl_policy, + NULL); + if (ret < 0) + return ret; + if (tb[TSWGPEER_A_FLAGS]) { + u32 flags = nla_get_u32(tb[TSWGPEER_A_FLAGS]); + + WRITE_ONCE(peer->tswg_no_roam, !!(flags & TSWGPEER_F_NO_ROAM)); + } + return 0; +} + +/* TSWG: tswg_get_peer adds peer's WGPEER_A_TSWG attribute to skb. */ +int tswg_get_peer(struct sk_buff *skb, struct wg_peer *peer) +{ + struct nlattr *nest; + u32 flags = 0; + + if (READ_ONCE(peer->tswg_no_roam)) + flags |= TSWGPEER_F_NO_ROAM; + nest = nla_nest_start(skb, WGPEER_A_TSWG); + if (!nest) + return -EMSGSIZE; + if (nla_put_u32(skb, TSWGPEER_A_FLAGS, flags)) { + nla_nest_cancel(skb, nest); + return -EMSGSIZE; + } + nla_nest_end(skb, nest); + return 0; +} + /* TSWG: tswg_get_device adds wg's WGDEVICE_A_TSWG attribute to skb. */ int tswg_get_device(struct sk_buff *skb, struct wg_device *wg) { diff --git a/kernel/linux/src/peer.h b/kernel/linux/src/peer.h index 718fb42bd..b40c61ec5 100644 --- a/kernel/linux/src/peer.h +++ b/kernel/linux/src/peer.h @@ -64,8 +64,24 @@ struct wg_peer { struct list_head allowedips_list; struct napi_struct napi; u64 internal_id; + + /* + * TSWG: if set, don't learn the endpoint from incoming packets once + * one is set; userspace (disco) picks the path. Read locklessly. + */ + bool tswg_no_roam; }; +/* + * TSWG: tswg_peer_may_roam reports whether an authenticated packet from a + * new address may update peer's endpoint. + */ +static inline bool tswg_peer_may_roam(const struct wg_peer *peer) +{ + return !READ_ONCE(peer->tswg_no_roam) || + !READ_ONCE(peer->endpoint.addr.sa_family); +} + struct wg_peer *wg_peer_create(struct wg_device *wg, const u8 public_key[NOISE_PUBLIC_KEY_LEN], const u8 preshared_key[NOISE_SYMMETRIC_KEY_LEN]); diff --git a/kernel/linux/src/receive.c b/kernel/linux/src/receive.c index 824bbefce..073d61407 100644 --- a/kernel/linux/src/receive.c +++ b/kernel/linux/src/receive.c @@ -340,7 +340,8 @@ static void wg_packet_consume_data_done(struct wg_peer *peer, unsigned int len, len_before_trim; struct wg_peer *routed_peer; - wg_socket_set_peer_endpoint(peer, endpoint); + if (tswg_peer_may_roam(peer)) /* TSWG */ + wg_socket_set_peer_endpoint(peer, endpoint); if (unlikely(wg_noise_received_with_keypair(&peer->keypairs, PACKET_CB(skb)->keypair))) { diff --git a/kernel/linux/src/socket.c b/kernel/linux/src/socket.c index 31c79139e..c8845ed8b 100644 --- a/kernel/linux/src/socket.c +++ b/kernel/linux/src/socket.c @@ -303,6 +303,8 @@ void wg_socket_set_peer_endpoint_from_skb(struct wg_peer *peer, { struct endpoint endpoint; + if (!tswg_peer_may_roam(peer)) /* TSWG */ + return; if (!wg_socket_endpoint_from_skb(&endpoint, skb)) wg_socket_set_peer_endpoint(peer, &endpoint); } diff --git a/kernel/linux/src/tswg.h b/kernel/linux/src/tswg.h index df6f6729e..cc48d7c10 100644 --- a/kernel/linux/src/tswg.h +++ b/kernel/linux/src/tswg.h @@ -10,6 +10,7 @@ #include struct wg_device; +struct wg_peer; struct sock; struct sk_buff; struct nlattr; @@ -26,5 +27,7 @@ u32 tswg_socket_flags(struct wg_device *wg); int tswg_set_device(struct wg_device *wg, struct nlattr *attr, struct netlink_ext_ack *extack); int tswg_get_device(struct sk_buff *skb, struct wg_device *wg); +int tswg_set_peer(struct wg_peer *peer, struct nlattr *attr); +int tswg_get_peer(struct sk_buff *skb, struct wg_peer *peer); #endif /* _TSWG_H */