From 26a56a652eeeece7677ba4f1896da34c83930652 Mon Sep 17 00:00:00 2001
From: "Jason A. Donenfeld" <Jason@zx2c4.com>
Date: Wed, 18 Apr 2018 16:39:14 +0200
Subject: [PATCH] Allow determining name

---
 device.go    |  8 ++++++++
 tun.go       |  2 +-
 tun_linux.go | 25 +++++++++++++++++++------
 3 files changed, 28 insertions(+), 7 deletions(-)

diff --git a/device.go b/device.go
index 79ec0b8..5adf33a 100644
--- a/device.go
+++ b/device.go
@@ -255,7 +255,15 @@ func NewDevice(tun TUNDevice, logger *Logger) *Device {
 	device.isClosed.Set(false)
 
 	device.log = logger
+
 	device.tun.device = tun
+	mtu, err := device.tun.device.MTU()
+	if err != nil {
+		logger.Error.Println("Trouble determining MTU, assuming 1420:", err)
+		mtu = 1420
+	}
+	device.tun.mtu = int32(mtu)
+
 	device.peers.keyMap = make(map[NoisePublicKey]*Peer)
 
 	// initialize anti-DoS / anti-scanning features
diff --git a/tun.go b/tun.go
index 7b044ad..318772a 100644
--- a/tun.go
+++ b/tun.go
@@ -20,7 +20,7 @@ type TUNDevice interface {
 	Read([]byte, int) (int, error)  // read a packet from the device (without any additional headers)
 	Write([]byte, int) (int, error) // writes a packet to the device (without any additional headers)
 	MTU() (int, error)              // returns the MTU of the device
-	Name() string                   // returns the current name
+	Name() (string, error)          // fetches and returns the current name
 	Events() chan TUNEvent          // returns a constant channel of events related to the device
 	Close() error                   // stops the device and closes the event channel
 }
diff --git a/tun_linux.go b/tun_linux.go
index da53907..06b5af4 100644
--- a/tun_linux.go
+++ b/tun_linux.go
@@ -11,6 +11,7 @@ import (
 	"golang.org/x/sys/unix"
 	"net"
 	"os"
+	"strconv"
 	"strings"
 	"time"
 	"unsafe"
@@ -130,10 +131,6 @@ func (tun *NativeTun) isUp() (bool, error) {
 	return inter.Flags&net.FlagUp != 0, err
 }
 
-func (tun *NativeTun) Name() string {
-	return tun.name
-}
-
 func getDummySock() (int, error) {
 	return unix.Socket(
 		unix.AF_INET,
@@ -229,7 +226,7 @@ func (tun *NativeTun) MTU() (int, error) {
 		uintptr(unsafe.Pointer(&ifr[0])),
 	)
 	if errno != 0 {
-		return 0, errors.New("Failed to get MTU of TUN device")
+		return 0, errors.New("Failed to get MTU of TUN device: " + strconv.FormatInt(int64(errno), 10))
 	}
 
 	// convert result to signed 32-bit int
@@ -241,6 +238,22 @@ func (tun *NativeTun) MTU() (int, error) {
 	return int(val), nil
 }
 
+func (tun *NativeTun) Name() (string, error) {
+
+	var ifr [ifReqSize]byte
+	_, _, errno := unix.Syscall(
+		unix.SYS_IOCTL,
+		tun.fd.Fd(),
+		uintptr(unix.TUNGETIFF),
+		uintptr(unsafe.Pointer(&ifr[0])),
+	)
+	if errno != 0 {
+		return "", errors.New("Failed to get name of TUN device: " + strconv.FormatInt(int64(errno), 10))
+	}
+	tun.name = string(ifr[:])
+	return tun.name, nil
+}
+
 func (tun *NativeTun) Write(buff []byte, offset int) (int, error) {
 
 	if tun.nopi {
@@ -342,7 +355,7 @@ func CreateTUN(name string) (TUNDevice, error) {
 
 	_, _, errno := unix.Syscall(
 		unix.SYS_IOCTL,
-		uintptr(fd.Fd()),
+		fd.Fd(),
 		uintptr(unix.TUNSETIFF),
 		uintptr(unsafe.Pointer(&ifr[0])),
 	)