From a10c141896d1fa292e0c74f7e6fd4df3a0eb7e93 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Sat, 6 Jul 2019 15:15:43 +0100 Subject: [PATCH] Fix data race on peermacs --- src/tuntap/icmpv6.go | 8 ++++++++ src/tuntap/iface.go | 6 +++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/tuntap/icmpv6.go b/src/tuntap/icmpv6.go index ea1a785..fe80dfb 100644 --- a/src/tuntap/icmpv6.go +++ b/src/tuntap/icmpv6.go @@ -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) { // Create the ND payload var payload [28]byte diff --git a/src/tuntap/iface.go b/src/tuntap/iface.go index be3988a..9ffde85 100644 --- a/src/tuntap/iface.go +++ b/src/tuntap/iface.go @@ -41,7 +41,7 @@ func (tun *TunAdapter) writer() error { return errors.New("Invalid address family") } 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) if !known { tun.icmpv6.Solicit(dstAddr) @@ -56,12 +56,12 @@ func (tun *TunAdapter) writer() error { 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 // address, use the MAC address of that peermac = neighbor.mac 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 // known instead peermac = neighbor.mac