Skip to content

Commit 5cccd39

Browse files
authored
update RemoteList.vpnAddrs when we complete a handshake (#1467)
1 parent 8196c22 commit 5cccd39

3 files changed

Lines changed: 24 additions & 14 deletions

File tree

handshake_ix.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ func ixHandshakeStage1(f *Interface, addr netip.AddrPort, via *ViaSender, packet
459459

460460
f.connectionManager.AddTrafficWatch(hostinfo)
461461

462-
hostinfo.remotes.ResetBlockedRemotes()
462+
hostinfo.remotes.RefreshFromHandshake(vpnAddrs)
463463

464464
return
465465
}
@@ -667,7 +667,7 @@ func ixHandshakeStage2(f *Interface, addr netip.AddrPort, via *ViaSender, hh *Ha
667667
f.cachedPacketMetrics.sent.Inc(int64(len(hh.packetStore)))
668668
}
669669

670-
hostinfo.remotes.ResetBlockedRemotes()
670+
hostinfo.remotes.RefreshFromHandshake(vpnAddrs)
671671
f.metricHandshakes.Update(duration)
672672

673673
return false

lighthouse.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ func (lh *LightHouse) QueryCache(vpnAddrs []netip.Addr) *RemoteList {
487487
lh.Lock()
488488
defer lh.Unlock()
489489
// Add an entry if we don't already have one
490-
return lh.unlockedGetRemoteList(vpnAddrs)
490+
return lh.unlockedGetRemoteList(vpnAddrs) //todo CERT-V2 this contains addrmap lookups we could potentially skip
491491
}
492492

493493
// queryAndPrepMessage is a lock helper on RemoteList, assisting the caller to build a lighthouse message containing
@@ -570,7 +570,7 @@ func (lh *LightHouse) addStaticRemotes(i int, d time.Duration, network string, t
570570
am.unlockedSetHostnamesResults(hr)
571571

572572
for _, addrPort := range hr.GetAddrs() {
573-
if !lh.shouldAdd(vpnAddr, addrPort.Addr()) {
573+
if !lh.shouldAdd([]netip.Addr{vpnAddr}, addrPort.Addr()) {
574574
continue
575575
}
576576
switch {
@@ -645,18 +645,17 @@ func (lh *LightHouse) unlockedGetRemoteList(allAddrs []netip.Addr) *RemoteList {
645645
}
646646
}
647647

648-
//TODO lighthouse.remote_allow_ranges is almost certainly broken in a multiple-address-per-cert scenario
649-
am := NewRemoteList(allAddrs, func(a netip.Addr) bool { return lh.shouldAdd(allAddrs[0], a) })
648+
am := NewRemoteList(allAddrs, lh.shouldAdd)
650649
for _, addr := range allAddrs {
651650
lh.addrMap[addr] = am
652651
}
653652
return am
654653
}
655654

656-
func (lh *LightHouse) shouldAdd(vpnAddr netip.Addr, to netip.Addr) bool {
657-
allow := lh.GetRemoteAllowList().Allow(vpnAddr, to)
655+
func (lh *LightHouse) shouldAdd(vpnAddrs []netip.Addr, to netip.Addr) bool {
656+
allow := lh.GetRemoteAllowList().AllowAll(vpnAddrs, to)
658657
if lh.l.Level >= logrus.TraceLevel {
659-
lh.l.WithField("vpnAddr", vpnAddr).WithField("udpAddr", to).WithField("allow", allow).
658+
lh.l.WithField("vpnAddrs", vpnAddrs).WithField("udpAddr", to).WithField("allow", allow).
660659
Trace("remoteAllowList.Allow")
661660
}
662661
if !allow {

remote_list.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ type RemoteList struct {
190190
// The full list of vpn addresses assigned to this host
191191
vpnAddrs []netip.Addr
192192

193-
// A deduplicated set of addresses. Any accessor should lock beforehand.
193+
// A deduplicated set of underlay addresses. Any accessor should lock beforehand.
194194
addrs []netip.AddrPort
195195

196196
// A set of relay addresses. VpnIp addresses that the remote identified as relays.
@@ -201,8 +201,10 @@ type RemoteList struct {
201201
// For learned addresses, this is the vpnIp that sent the packet
202202
cache map[netip.Addr]*cache
203203

204-
hr *hostnamesResults
205-
shouldAdd func(netip.Addr) bool
204+
hr *hostnamesResults
205+
206+
// shouldAdd is a nillable function that decides if x should be added to addrs.
207+
shouldAdd func(vpnAddrs []netip.Addr, x netip.Addr) bool
206208

207209
// This is a list of remotes that we have tried to handshake with and have returned from the wrong vpn ip.
208210
// They should not be tried again during a handshake
@@ -213,7 +215,7 @@ type RemoteList struct {
213215
}
214216

215217
// NewRemoteList creates a new empty RemoteList
216-
func NewRemoteList(vpnAddrs []netip.Addr, shouldAdd func(netip.Addr) bool) *RemoteList {
218+
func NewRemoteList(vpnAddrs []netip.Addr, shouldAdd func([]netip.Addr, netip.Addr) bool) *RemoteList {
217219
r := &RemoteList{
218220
vpnAddrs: make([]netip.Addr, len(vpnAddrs)),
219221
addrs: make([]netip.AddrPort, 0),
@@ -368,6 +370,15 @@ func (r *RemoteList) CopyBlockedRemotes() []netip.AddrPort {
368370
return c
369371
}
370372

373+
// RefreshFromHandshake locks and updates the RemoteList to account for data learned upon a completed handshake
374+
func (r *RemoteList) RefreshFromHandshake(vpnAddrs []netip.Addr) {
375+
r.Lock()
376+
r.badRemotes = nil
377+
r.vpnAddrs = make([]netip.Addr, len(vpnAddrs))
378+
copy(r.vpnAddrs, vpnAddrs)
379+
r.Unlock()
380+
}
381+
371382
// ResetBlockedRemotes locks and clears the blocked remotes list
372383
func (r *RemoteList) ResetBlockedRemotes() {
373384
r.Lock()
@@ -577,7 +588,7 @@ func (r *RemoteList) unlockedCollect() {
577588

578589
dnsAddrs := r.hr.GetAddrs()
579590
for _, addr := range dnsAddrs {
580-
if r.shouldAdd == nil || r.shouldAdd(addr.Addr()) {
591+
if r.shouldAdd == nil || r.shouldAdd(r.vpnAddrs, addr.Addr()) {
581592
if !r.unlockedIsBad(addr) {
582593
addrs = append(addrs, addr)
583594
}

0 commit comments

Comments
 (0)