]> git.ipfire.org Git - thirdparty/wireguard-go.git/commitdiff
device: tie encryption queue lifetime to the peers that write to it
authorJosh Bleecher Snyder <josh@tailscale.com>
Tue, 2 Feb 2021 18:46:34 +0000 (10:46 -0800)
committerJason A. Donenfeld <Jason@zx2c4.com>
Tue, 2 Feb 2021 23:57:57 +0000 (00:57 +0100)
Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
device/device.go
device/peer.go
device/send.go

index 5f360362a82cb3e5ec4b3aa6f30abd2e1709f0ab..d860ed39411ffb78614a451a24808f4f5262c83a 100644 (file)
@@ -397,6 +397,10 @@ func (device *Device) Close() {
 
        device.isUp.Set(false)
 
+       // Remove peers before closing queues,
+       // because peers assume that queues are active.
+       device.RemoveAllPeers()
+
        // We kept a reference to the encryption and decryption queues,
        // in case we started any new peers that might write to them.
        // No new peers are coming; we are done with these queues.
@@ -405,8 +409,6 @@ func (device *Device) Close() {
        device.queue.handshake.wg.Done()
        device.state.stopping.Wait()
 
-       device.RemoveAllPeers()
-
        device.rate.limiter.Close()
 
        device.state.changing.Set(false)
index 76f9a96be0efeabbc8d9739f44dc79a0e80d5bd8..65f73a98aae7a90f03653ee0955d65ca99b5864d 100644 (file)
@@ -177,6 +177,7 @@ func (peer *Peer) Start() {
        if peer.queue.staged == nil {
                peer.queue.staged = make(chan *QueueOutboundElement, QueueStagedSize)
        }
+       peer.device.queue.encryption.wg.Add(1) // keep encryption queue open for our writes
 
        peer.timersInit()
        peer.handshake.lastSentHandshake = time.Now().Add(-(RekeyTimeout + time.Second))
@@ -248,6 +249,7 @@ func (peer *Peer) Stop() {
        close(peer.queue.inbound)
        close(peer.queue.outbound)
        peer.stopping.Wait()
+       peer.device.queue.encryption.wg.Done() // no more writes to encryption queue from us
 
        peer.ZeroAndFlushAll()
 }
index 9d63c4e71ae4b8a5be3e72cf87a2e742384c98ef..8def4ce87cab6ab078a434fca1b3b510a9c172c8 100644 (file)
@@ -291,8 +291,6 @@ func (peer *Peer) StagePacket(elem *QueueOutboundElement) {
 }
 
 func (peer *Peer) SendStagedPackets() {
-       peer.device.queue.encryption.wg.Add(1)
-       defer peer.device.queue.encryption.wg.Done()
 top:
        if len(peer.queue.staged) == 0 || !peer.device.isUp.Get() {
                return