5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-11-29 17:51:35 +00:00

Fix data race on peermacs

This commit is contained in:
Neil Alexander 2019-07-06 15:15:43 +01:00
parent e8272926a4
commit a10c141896
No known key found for this signature in database
GPG Key ID: A02A2019A2BB0944
2 changed files with 11 additions and 3 deletions

View File

@ -313,6 +313,14 @@ func (i *ICMPv6) Solicit(addr address.Address) {
} }
} }
func (i *ICMPv6) getNeighbor(addr address.Address) (neighbor, bool) {
i.peermacsmutex.RLock()
defer i.peermacsmutex.RUnlock()
n, ok := i.peermacs[addr]
return n, ok
}
func (i *ICMPv6) createNDPL2(dst address.Address) ([]byte, error) { func (i *ICMPv6) createNDPL2(dst address.Address) ([]byte, error) {
// Create the ND payload // Create the ND payload
var payload [28]byte var payload [28]byte

View File

@ -41,7 +41,7 @@ func (tun *TunAdapter) writer() error {
return errors.New("Invalid address family") return errors.New("Invalid address family")
} }
sendndp := func(dstAddr address.Address) { sendndp := func(dstAddr address.Address) {
neigh, known := tun.icmpv6.peermacs[dstAddr] neigh, known := tun.icmpv6.getNeighbor(dstAddr)
known = known && (time.Since(neigh.lastsolicitation).Seconds() < 30) known = known && (time.Since(neigh.lastsolicitation).Seconds() < 30)
if !known { if !known {
tun.icmpv6.Solicit(dstAddr) tun.icmpv6.Solicit(dstAddr)
@ -56,12 +56,12 @@ func (tun *TunAdapter) writer() error {
dstAddr = tun.addr dstAddr = tun.addr
} }
} }
if neighbor, ok := tun.icmpv6.peermacs[dstAddr]; ok && neighbor.learned { if neighbor, ok := tun.icmpv6.getNeighbor(dstAddr); ok && neighbor.learned {
// If we've learned the MAC of a 300::/7 address, for example, or a CKR // If we've learned the MAC of a 300::/7 address, for example, or a CKR
// address, use the MAC address of that // address, use the MAC address of that
peermac = neighbor.mac peermac = neighbor.mac
peerknown = true peerknown = true
} else if neighbor, ok := tun.icmpv6.peermacs[tun.addr]; ok && neighbor.learned { } else if neighbor, ok := tun.icmpv6.getNeighbor(tun.addr); ok && neighbor.learned {
// Otherwise send directly to the MAC address of the host if that's // Otherwise send directly to the MAC address of the host if that's
// known instead // known instead
peermac = neighbor.mac peermac = neighbor.mac