Skip to content

Commit 2f5d148

Browse files
raggibradfitz
authored andcommitted
conn,device: enable cryptorouting via PeerAwareEndpoint
Introduce an optional extension point for Endpoint that enables a path for WireGuard to inform an integration about the peer public key that is associated with an Endpoint. The API is expected to return either the same or a new Endpoint in response to this function. A future version of this patch could potentially remove the returned Endpoint, but would require larger integrator changes downstream. This adds a small per-packet cost that could later be removed with a larger refactor of the wireguard-go interface and Tailscale magicsock code, as well as introducing a generic bound for Endpoint in a device & bind instance. Updates tailscale/corp#20732
1 parent cfa4567 commit 2f5d148

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

conn/conn.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,20 @@ type Endpoint interface {
8484
SrcIP() netip.Addr
8585
}
8686

87+
// PeerAwareEndpoint is an optional Endpoint specialization for
88+
// integrations that want to know about the outcome of cryptorouting
89+
// identification.
90+
//
91+
// If they receive a packet from a source they had not pre-identified,
92+
// to learn the identification WireGuard can derive from the session
93+
// or handshake.
94+
//
95+
// If GetPeerEndpoint returns nil, WireGuard will be unable to respond
96+
// to the peer until a new endpoint is written by a later packet.
97+
type PeerAwareEndpoint interface {
98+
GetPeerEndpoint(peerPublicKey [32]byte) Endpoint
99+
}
100+
87101
var (
88102
ErrBindAlreadyOpen = errors.New("bind is already open")
89103
ErrWrongEndpointType = errors.New("endpoint type does not correspond with bind type")

device/noise-protocol.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ type Handshake struct {
124124
localEphemeral NoisePrivateKey // ephemeral secret key
125125
localIndex uint32 // used to clear hash-table
126126
remoteIndex uint32 // index for sending
127-
remoteStatic NoisePublicKey // long term key
127+
remoteStatic NoisePublicKey // long term key, never changes, can be accessed without mutex
128128
remoteEphemeral NoisePublicKey // ephemeral public key
129129
precomputedStaticStatic [NoisePublicKeySize]byte // precomputed shared secret
130130
lastTimestamp tai64n.Timestamp

device/peer.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,9 @@ func (peer *Peer) SetEndpointFromPacket(endpoint conn.Endpoint) {
283283
return
284284
}
285285
peer.endpoint.clearSrcOnTx = false
286+
if ep, ok := endpoint.(conn.PeerAwareEndpoint); ok {
287+
endpoint = ep.GetPeerEndpoint(peer.handshake.remoteStatic)
288+
}
286289
peer.endpoint.val = endpoint
287290
}
288291

0 commit comments

Comments
 (0)