Development discussion of WireGuard
 help / color / mirror / Atom feed
From: "Aurélien Chabot" <aurelien@chabot.fr>
To: wireguard@lists.zx2c4.com
Subject: [PATCH] Close the tun fd when closing the device
Date: Fri, 10 Nov 2017 22:00:33 +1100	[thread overview]
Message-ID: <20171110110033.24210-2-aurelien@chabot.fr> (raw)
In-Reply-To: <20171110110033.24210-1-aurelien@chabot.fr>

We also need to listen to the stop signal in the read tun function to
avoid reading a close tun device.

Signed-off-by: Aurélien Chabot <aurelien@chabot.fr>
---
 src/device.go |  1 +
 src/send.go   | 77 ++++++++++++++++++++++++++++++++---------------------------
 2 files changed, 43 insertions(+), 35 deletions(-)

diff --git a/src/device.go b/src/device.go
index 61c87bc..8567a36 100644
--- a/src/device.go
+++ b/src/device.go
@@ -205,6 +205,7 @@ func (device *Device) Close() {
 	device.RemoveAllPeers()
 	close(device.signal.stop)
 	closeUDPConn(device)
+	device.tun.device.Close()
 }
 
 func (device *Device) WaitChannel() chan struct{} {
diff --git a/src/send.go b/src/send.go
index 5c88ead..35ae9a3 100644
--- a/src/send.go
+++ b/src/send.go
@@ -141,53 +141,60 @@ func (device *Device) RoutineReadFromTUN() {
 
 	for {
 
-		// read packet
 
-		elem.packet = elem.buffer[MessageTransportHeaderSize:]
-		size, err := device.tun.device.Read(elem.packet)
-		if err != nil {
-			logError.Println("Failed to read packet from TUN device:", err)
-			device.Close()
-			return
-		}
+		select {
+		case <-device.signal.stop:
+		    logDebug.Println("Routine, TUN Reader worker, stopped")
+		    return
+		default:
+		    // read packet
 
-		if size == 0 || size > MaxContentSize {
-			continue
-		}
+		    elem.packet = elem.buffer[MessageTransportHeaderSize:]
+			size, err := device.tun.device.Read(elem.packet)
+			if err != nil {
+				logError.Println("Failed to read packet from TUN device:", err)
+				device.Close()
+				return
+			}
 
-		elem.packet = elem.packet[:size]
+			if size == 0 || size > MaxContentSize {
+				continue
+			}
 
-		// lookup peer
+			elem.packet = elem.packet[:size]
 
-		var peer *Peer
-		switch elem.packet[0] >> 4 {
-		case ipv4.Version:
-			if len(elem.packet) < ipv4.HeaderLen {
-				continue
+			// lookup peer
+
+			var peer *Peer
+			switch elem.packet[0] >> 4 {
+			case ipv4.Version:
+				if len(elem.packet) < ipv4.HeaderLen {
+					continue
+				}
+				dst := elem.packet[IPv4offsetDst : IPv4offsetDst+net.IPv4len]
+				peer = device.routingTable.LookupIPv4(dst)
+
+			case ipv6.Version:
+				if len(elem.packet) < ipv6.HeaderLen {
+					continue
+				}
+				dst := elem.packet[IPv6offsetDst : IPv6offsetDst+net.IPv6len]
+				peer = device.routingTable.LookupIPv6(dst)
+
+			default:
+				logDebug.Println("Receieved packet with unknown IP version")
 			}
-			dst := elem.packet[IPv4offsetDst : IPv4offsetDst+net.IPv4len]
-			peer = device.routingTable.LookupIPv4(dst)
 
-		case ipv6.Version:
-			if len(elem.packet) < ipv6.HeaderLen {
+			if peer == nil {
 				continue
 			}
-			dst := elem.packet[IPv6offsetDst : IPv6offsetDst+net.IPv6len]
-			peer = device.routingTable.LookupIPv6(dst)
 
-		default:
-			logDebug.Println("Receieved packet with unknown IP version")
-		}
+			// insert into nonce/pre-handshake queue
 
-		if peer == nil {
-			continue
+			signalSend(peer.signal.handshakeReset)
+			addToOutboundQueue(peer.queue.nonce, elem)
+			elem = device.NewOutboundElement()
 		}
-
-		// insert into nonce/pre-handshake queue
-
-		signalSend(peer.signal.handshakeReset)
-		addToOutboundQueue(peer.queue.nonce, elem)
-		elem = device.NewOutboundElement()
 	}
 }
 
-- 
2.15.0

  reply	other threads:[~2017-11-10 10:57 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-11-10 11:00 Aurélien Chabot
2017-11-10 11:00 ` Aurélien Chabot [this message]
2017-11-10 11:38 ` Georg Faerber
2017-11-10 11:39   ` Georg Faerber

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20171110110033.24210-2-aurelien@chabot.fr \
    --to=aurelien@chabot.fr \
    --cc=wireguard@lists.zx2c4.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).