wgengine/magicsock: fix bug in peerMap.upsertEndpoint

Found by inspection. Contains before/after for easy toggling.
This commit is contained in:
Josh Bleecher Snyder 2021-11-10 14:03:47 -08:00
parent cab2c9376f
commit 67dcfb2356
2 changed files with 18 additions and 8 deletions

View File

@ -167,17 +167,26 @@ func (m *peerMap) forEachEndpointWithDiscoKey(dk tailcfg.DiscoKey, f func(ep *en
// upsertEndpoint stores endpoint in the peerInfo for // upsertEndpoint stores endpoint in the peerInfo for
// ep.publicKey, and updates indexes. m must already have a // ep.publicKey, and updates indexes. m must already have a
// tailcfg.Node for ep.publicKey. // tailcfg.Node for ep.publicKey.
func (m *peerMap) upsertEndpoint(ep *endpoint) { func (m *peerMap) upsertEndpoint(ep *endpoint, oldDiscoKey tailcfg.DiscoKey) {
pi := m.byNodeKey[ep.publicKey] pi := m.byNodeKey[ep.publicKey]
if pi == nil { if pi == nil {
pi = newPeerInfo(ep) pi = newPeerInfo(ep)
m.byNodeKey[ep.publicKey] = pi m.byNodeKey[ep.publicKey] = pi
} else { } else {
const useBuggyVersion = true
if useBuggyVersion {
// buggy version
old := pi.ep old := pi.ep
pi.ep = ep pi.ep = ep
if old.discoKey != ep.discoKey { if old.discoKey != ep.discoKey {
delete(m.nodesOfDisco[old.discoKey], ep.publicKey) delete(m.nodesOfDisco[old.discoKey], ep.publicKey)
} }
} else {
// fixed version
if oldDiscoKey != ep.discoKey {
delete(m.nodesOfDisco[oldDiscoKey], ep.publicKey)
}
}
} }
if !ep.discoKey.IsZero() { if !ep.discoKey.IsZero() {
set := m.nodesOfDisco[ep.discoKey] set := m.nodesOfDisco[ep.discoKey]
@ -2237,8 +2246,9 @@ func (c *Conn) SetNetworkMap(nm *netmap.NetworkMap) {
// handle full set updates. // handle full set updates.
for _, n := range nm.Peers { for _, n := range nm.Peers {
if ep, ok := c.peerMap.endpointForNodeKey(n.Key); ok { if ep, ok := c.peerMap.endpointForNodeKey(n.Key); ok {
oldDiscoKey := ep.discoKey
ep.updateFromNode(n) ep.updateFromNode(n)
c.peerMap.upsertEndpoint(ep) // maybe update discokey mappings in peerMap c.peerMap.upsertEndpoint(ep, oldDiscoKey) // maybe update discokey mappings in peerMap
continue continue
} }
@ -2278,7 +2288,7 @@ func (c *Conn) SetNetworkMap(nm *netmap.NetworkMap) {
} }
})) }))
ep.updateFromNode(n) ep.updateFromNode(n)
c.peerMap.upsertEndpoint(ep) c.peerMap.upsertEndpoint(ep, tailcfg.DiscoKey{})
} }
// If the set of nodes changed since the last SetNetworkMap, the // If the set of nodes changed since the last SetNetworkMap, the

View File

@ -1148,7 +1148,7 @@ func TestDiscoMessage(t *testing.T) {
c.peerMap.upsertEndpoint(&endpoint{ c.peerMap.upsertEndpoint(&endpoint{
publicKey: n.Key, publicKey: n.Key,
discoKey: n.DiscoKey, discoKey: n.DiscoKey,
}) }, tailcfg.DiscoKey{})
const payload = "why hello" const payload = "why hello"