From f8dda26dbaa25435de04b7069b87662949dc0f8d Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Thu, 1 Mar 2018 11:49:49 +0000 Subject: [PATCH 1/9] Add BSD support (openbsd, freebsd, solaris) --- misc/tests/tunbench-client.go | 2 +- misc/tests/tunbench-server.go | 2 +- misc/tests/tunbench.go | 2 +- src/yggdrasil/tun_bsd.go | 29 +++++++++++++++++++++++++++++ src/yggdrasil/tun_darwin.go | 2 +- src/yggdrasil/tun_linux.go | 2 +- src/yggdrasil/tun_other.go | 5 ++++- src/yggdrasil/tun_windows.go | 2 +- 8 files changed, 39 insertions(+), 7 deletions(-) create mode 100644 src/yggdrasil/tun_bsd.go diff --git a/misc/tests/tunbench-client.go b/misc/tests/tunbench-client.go index ac38b502..9e606799 100644 --- a/misc/tests/tunbench-client.go +++ b/misc/tests/tunbench-client.go @@ -7,7 +7,7 @@ import ( "os/exec" "time" - "github.com/songgao/water" + "github.com/neilalexander/water" ) const mtu = 65535 diff --git a/misc/tests/tunbench-server.go b/misc/tests/tunbench-server.go index 12c114fe..288cea25 100644 --- a/misc/tests/tunbench-server.go +++ b/misc/tests/tunbench-server.go @@ -6,7 +6,7 @@ import ( "net" "os/exec" - "github.com/songgao/water" + "github.com/neilalexander/water" ) const mtu = 65535 diff --git a/misc/tests/tunbench.go b/misc/tests/tunbench.go index 02b75e72..ac9205e2 100644 --- a/misc/tests/tunbench.go +++ b/misc/tests/tunbench.go @@ -6,7 +6,7 @@ import ( "net" "os/exec" - "github.com/songgao/water" + "github.com/neilalexander/water" ) const mtu = 65535 diff --git a/src/yggdrasil/tun_bsd.go b/src/yggdrasil/tun_bsd.go new file mode 100644 index 00000000..4e3fa0c1 --- /dev/null +++ b/src/yggdrasil/tun_bsd.go @@ -0,0 +1,29 @@ +// +build openbsd freebsd solaris + +package yggdrasil + +import water "github.com/neilalexander/water" + +// This is to catch BSD platforms + +func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) error { + var config water.Config + if iftapmode { + config = water.Config{DeviceType: water.TAP} + } else { + config = water.Config{DeviceType: water.TUN} + } + config.Name = ifname + iface, err := water.New(config) + if err != nil { + panic(err) + } + tun.iface = iface + tun.mtu = mtu //1280 // Lets default to the smallest thing allowed for now + return tun.setupAddress(addr) +} + +func (tun *tunDevice) setupAddress(addr string) error { + tun.core.log.Println("Platform not supported, you must set the address of", tun.iface.Name(), "to", addr) + return nil +} diff --git a/src/yggdrasil/tun_darwin.go b/src/yggdrasil/tun_darwin.go index 0e2fa245..eb1489f3 100644 --- a/src/yggdrasil/tun_darwin.go +++ b/src/yggdrasil/tun_darwin.go @@ -8,7 +8,7 @@ import "strconv" import "encoding/binary" import "golang.org/x/sys/unix" -import water "github.com/songgao/water" +import water "github.com/neilalexander/water" func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) error { if iftapmode { diff --git a/src/yggdrasil/tun_linux.go b/src/yggdrasil/tun_linux.go index 8748aa92..5fdf6409 100644 --- a/src/yggdrasil/tun_linux.go +++ b/src/yggdrasil/tun_linux.go @@ -7,7 +7,7 @@ import "fmt" import "os/exec" import "strings" -import water "github.com/songgao/water" +import water "github.com/neilalexander/water" func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) error { var config water.Config diff --git a/src/yggdrasil/tun_other.go b/src/yggdrasil/tun_other.go index 388260cd..147e55ad 100644 --- a/src/yggdrasil/tun_other.go +++ b/src/yggdrasil/tun_other.go @@ -1,10 +1,13 @@ // +build !linux // +build !darwin // +build !windows +// +build !openbsd +// +build !freebsd +// +build !solaris package yggdrasil -import water "github.com/songgao/water" +import water "github.com/neilalexander/water" // This is to catch unsupported platforms // If your platform supports tun devices, you could try configuring it manually diff --git a/src/yggdrasil/tun_windows.go b/src/yggdrasil/tun_windows.go index acaf20fc..a2108fe9 100644 --- a/src/yggdrasil/tun_windows.go +++ b/src/yggdrasil/tun_windows.go @@ -1,6 +1,6 @@ package yggdrasil -import water "github.com/songgao/water" +import water "github.com/neilalexander/water" import "os/exec" import "strings" import "fmt" From 9e4d169208c83ddcb569aa4c69ddaafafa82b987 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Thu, 1 Mar 2018 13:37:05 +0000 Subject: [PATCH 2/9] Set interface IP and MTU on BSD --- src/yggdrasil/tun_bsd.go | 69 +++++++++++++++++++++++++++++++++++--- src/yggdrasil/tun_other.go | 7 +--- 2 files changed, 66 insertions(+), 10 deletions(-) diff --git a/src/yggdrasil/tun_bsd.go b/src/yggdrasil/tun_bsd.go index 4e3fa0c1..67e3b6f0 100644 --- a/src/yggdrasil/tun_bsd.go +++ b/src/yggdrasil/tun_bsd.go @@ -1,17 +1,60 @@ -// +build openbsd freebsd solaris +// +build openbsd freebsd solaris netbsd dragonfly package yggdrasil +import "fmt" +import "os/exec" +import "strings" + import water "github.com/neilalexander/water" // This is to catch BSD platforms +const TUNSIFINFO = (0x80000000) | ((4 & 0x1fff) << 16) | uint32(byte('t'))<<8 | 91 +const TUNGIFINFO = (0x40000000) | ((4 & 0x1fff) << 16) | uint32(byte('t'))<<8 | 92 +const SIOCAIFADDR_IN6 = (0x80000000) | ((4 & 0x1fff) << 16) | uint32(byte('i'))<<8 | 27 + +type in6_addrlifetime struct { + ia6t_expire float64 + ia6t_preferred float64 + ia6t_vltime uint32 + ia6t_pltime uint32 +} + +type sockaddr_in6 struct { + sin6_len uint8 + sin6_family uint8 + sin6_port uint8 + sin6_flowinfo uint32 + sin6_addr [8]uint16 + sin6_scope_id uint32 +} + +type in6_aliasreq struct { + ifra_name [16]byte + ifra_addr sockaddr_in6 + ifra_dstaddr sockaddr_in6 + ifra_prefixmask sockaddr_in6 + ifra_flags uint32 + ifra_lifetime in6_addrlifetime +} + +type tuninfo struct { + tun_baudrate int + tun_mtu int16 + tun_type uint8 + tun_dummy uint8 +} + func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) error { var config water.Config - if iftapmode { + switch { + case iftapmode || ifname[:8] == "/dev/tap": config = water.Config{DeviceType: water.TAP} - } else { + case !iftapmode || ifname[:8] == "/dev/tun": config = water.Config{DeviceType: water.TUN} + default: + panic("TUN/TAP name must be in format /dev/tunX or /dev/tapX") } config.Name = ifname iface, err := water.New(config) @@ -24,6 +67,24 @@ func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) } func (tun *tunDevice) setupAddress(addr string) error { - tun.core.log.Println("Platform not supported, you must set the address of", tun.iface.Name(), "to", addr) + // Set address + cmd := exec.Command("ifconfig", tun.iface.Name(), "inet6", addr) + tun.core.log.Printf("ifconfig command: %v", strings.Join(cmd.Args, " ")) + output, err := cmd.CombinedOutput() + if err != nil { + tun.core.log.Printf("ipconfig failed: %v.", err) + tun.core.log.Println(string(output)) + return err + } + // Set MTU and bring device up + cmd = exec.Command("ifconfig", tun.iface.Name(), "mtu", fmt.Sprintf("%d", tun.mtu), "up") + tun.core.log.Printf("ifconfig command: %v", strings.Join(cmd.Args, " ")) + output, err = cmd.CombinedOutput() + if err != nil { + tun.core.log.Printf("ipconfig failed: %v.", err) + tun.core.log.Println(string(output)) + return err + } + return nil } diff --git a/src/yggdrasil/tun_other.go b/src/yggdrasil/tun_other.go index 147e55ad..06dc2158 100644 --- a/src/yggdrasil/tun_other.go +++ b/src/yggdrasil/tun_other.go @@ -1,9 +1,4 @@ -// +build !linux -// +build !darwin -// +build !windows -// +build !openbsd -// +build !freebsd -// +build !solaris +// +build !linux,!darwin,!windows,!openbsd,!freebsd,!solaris,!netbsd,!dragonfly package yggdrasil From 90393ae03beb40ee94835985254ace0a1e6c014e Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Thu, 1 Mar 2018 15:02:53 +0000 Subject: [PATCH 3/9] Set interface flags properly on OpenBSD --- src/yggdrasil/tun.go | 2 ++ src/yggdrasil/tun_bsd.go | 61 ++++++++++++++++++++++++++++------------ 2 files changed, 45 insertions(+), 18 deletions(-) diff --git a/src/yggdrasil/tun.go b/src/yggdrasil/tun.go index 49acff69..b22a341a 100644 --- a/src/yggdrasil/tun.go +++ b/src/yggdrasil/tun.go @@ -2,6 +2,7 @@ package yggdrasil // This manages the tun driver to send/recv packets to/from applications +import "os" import ethernet "github.com/songgao/packets/ethernet" const IPv6_HEADER_LENGTH = 40 @@ -14,6 +15,7 @@ type tunInterface interface { Read(to []byte) (int, error) Write(from []byte) (int, error) Close() error + FD() *os.File } type tunDevice struct { diff --git a/src/yggdrasil/tun_bsd.go b/src/yggdrasil/tun_bsd.go index 67e3b6f0..a6a5adc7 100644 --- a/src/yggdrasil/tun_bsd.go +++ b/src/yggdrasil/tun_bsd.go @@ -2,16 +2,18 @@ package yggdrasil -import "fmt" import "os/exec" import "strings" +import "unsafe" +import "syscall" +import "golang.org/x/sys/unix" import water "github.com/neilalexander/water" // This is to catch BSD platforms -const TUNSIFINFO = (0x80000000) | ((4 & 0x1fff) << 16) | uint32(byte('t'))<<8 | 91 -const TUNGIFINFO = (0x40000000) | ((4 & 0x1fff) << 16) | uint32(byte('t'))<<8 | 92 +const TUNSIFINFO = (0x80000000) | ((12 & 0x1fff) << 16) | uint32(byte('t'))<<8 | 91 +const TUNGIFINFO = (0x40000000) | ((12 & 0x1fff) << 16) | uint32(byte('t'))<<8 | 92 const SIOCAIFADDR_IN6 = (0x80000000) | ((4 & 0x1fff) << 16) | uint32(byte('i'))<<8 | 27 type in6_addrlifetime struct { @@ -40,10 +42,10 @@ type in6_aliasreq struct { } type tuninfo struct { - tun_baudrate int - tun_mtu int16 - tun_type uint8 - tun_dummy uint8 + tun_mtu uint16 + tun_type uint32 + tun_flags uint32 + tun_dummy uint16 } func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) error { @@ -67,23 +69,46 @@ func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) } func (tun *tunDevice) setupAddress(addr string) error { + fd := tun.iface.FD().Fd() + var err error + var ti tuninfo + + // Get the existing interface flags + if _, _, errno := unix.Syscall(unix.SYS_IOCTL, uintptr(fd), uintptr(TUNGIFINFO), uintptr(unsafe.Pointer(&ti))); errno != 0 { + err = errno + tun.core.log.Printf("Error in TUNGIFINFO: %v", errno) + return err + } + tun.core.log.Printf("TUNGIFINFO: %+v", ti) + + // Set the new MTU + ti.tun_mtu = uint16(tun.mtu) + + // Set the new interface flags + ti.tun_flags |= syscall.IFF_UP + switch { + case tun.iface.IsTAP(): + ti.tun_flags |= syscall.IFF_MULTICAST + ti.tun_flags |= syscall.IFF_BROADCAST + case tun.iface.IsTUN(): + ti.tun_flags |= syscall.IFF_POINTOPOINT + } + + // Set the new interface flags + tun.core.log.Printf("TUNSIFINFO: %+v", ti) + if _, _, errno := unix.Syscall(unix.SYS_IOCTL, uintptr(fd), uintptr(TUNSIFINFO), uintptr(unsafe.Pointer(&ti))); errno != 0 { + err = errno + tun.core.log.Printf("Error in TUNSIFINFO: %v", errno) + return err + } + // Set address cmd := exec.Command("ifconfig", tun.iface.Name(), "inet6", addr) tun.core.log.Printf("ifconfig command: %v", strings.Join(cmd.Args, " ")) output, err := cmd.CombinedOutput() if err != nil { - tun.core.log.Printf("ipconfig failed: %v.", err) + tun.core.log.Printf("ifconfig failed: %v.", err) tun.core.log.Println(string(output)) - return err - } - // Set MTU and bring device up - cmd = exec.Command("ifconfig", tun.iface.Name(), "mtu", fmt.Sprintf("%d", tun.mtu), "up") - tun.core.log.Printf("ifconfig command: %v", strings.Join(cmd.Args, " ")) - output, err = cmd.CombinedOutput() - if err != nil { - tun.core.log.Printf("ipconfig failed: %v.", err) - tun.core.log.Println(string(output)) - return err } return nil From 24be3f1d67a3e8e00eeee6039363777734a2ab8d Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Thu, 1 Mar 2018 15:11:12 +0000 Subject: [PATCH 4/9] Turns out FreeBSD is a bit different so restrict this to OpenBSD for now --- src/yggdrasil/{tun_bsd.go => tun_openbsd.go} | 24 ++++++++++++-------- src/yggdrasil/tun_other.go | 2 +- 2 files changed, 16 insertions(+), 10 deletions(-) rename src/yggdrasil/{tun_bsd.go => tun_openbsd.go} (91%) diff --git a/src/yggdrasil/tun_bsd.go b/src/yggdrasil/tun_openbsd.go similarity index 91% rename from src/yggdrasil/tun_bsd.go rename to src/yggdrasil/tun_openbsd.go index a6a5adc7..25ca3c3c 100644 --- a/src/yggdrasil/tun_bsd.go +++ b/src/yggdrasil/tun_openbsd.go @@ -1,4 +1,4 @@ -// +build openbsd freebsd solaris netbsd dragonfly +// +build openbsd package yggdrasil @@ -10,12 +10,25 @@ import "golang.org/x/sys/unix" import water "github.com/neilalexander/water" -// This is to catch BSD platforms +// This is to catch OpenBSD + +// Warning! When porting this to other BSDs, the tuninfo struct can appear with +// the fields in a different order, and the consts below might also have +// different values + +type tuninfo struct { + tun_mtu uint16 + tun_type uint32 + tun_flags uint32 + tun_dummy uint16 +} const TUNSIFINFO = (0x80000000) | ((12 & 0x1fff) << 16) | uint32(byte('t'))<<8 | 91 const TUNGIFINFO = (0x40000000) | ((12 & 0x1fff) << 16) | uint32(byte('t'))<<8 | 92 const SIOCAIFADDR_IN6 = (0x80000000) | ((4 & 0x1fff) << 16) | uint32(byte('i'))<<8 | 27 +// Below this point seems to be fairly standard at least... + type in6_addrlifetime struct { ia6t_expire float64 ia6t_preferred float64 @@ -41,13 +54,6 @@ type in6_aliasreq struct { ifra_lifetime in6_addrlifetime } -type tuninfo struct { - tun_mtu uint16 - tun_type uint32 - tun_flags uint32 - tun_dummy uint16 -} - func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) error { var config water.Config switch { diff --git a/src/yggdrasil/tun_other.go b/src/yggdrasil/tun_other.go index 06dc2158..ec56408e 100644 --- a/src/yggdrasil/tun_other.go +++ b/src/yggdrasil/tun_other.go @@ -1,4 +1,4 @@ -// +build !linux,!darwin,!windows,!openbsd,!freebsd,!solaris,!netbsd,!dragonfly +// +build !linux,!darwin,!windows,!openbsd package yggdrasil From 6640b33334ca89efa9071dc5641f72a586b06efe Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Thu, 1 Mar 2018 15:19:20 +0000 Subject: [PATCH 5/9] Fix using 'auto' as device name on OpenBSD - default to /dev/tap0 --- src/yggdrasil/tun_openbsd.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/yggdrasil/tun_openbsd.go b/src/yggdrasil/tun_openbsd.go index 25ca3c3c..8c6386d1 100644 --- a/src/yggdrasil/tun_openbsd.go +++ b/src/yggdrasil/tun_openbsd.go @@ -12,6 +12,10 @@ import water "github.com/neilalexander/water" // This is to catch OpenBSD +// TODO: Fix TUN mode for OpenBSD. It turns out that OpenBSD doesn't have a way +// to disable the PI header when in TUN mode, so we need to modify the read/ +// writes to handle those first four bytes + // Warning! When porting this to other BSDs, the tuninfo struct can appear with // the fields in a different order, and the consts below might also have // different values @@ -56,11 +60,18 @@ type in6_aliasreq struct { func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) error { var config water.Config + if ifname[:4] == "auto" { + ifname = "/dev/tap0" + } + if len(ifname) < 9 { + panic("TUN/TAP name must be in format /dev/tunX or /dev/tapX") + } switch { case iftapmode || ifname[:8] == "/dev/tap": config = water.Config{DeviceType: water.TAP} case !iftapmode || ifname[:8] == "/dev/tun": - config = water.Config{DeviceType: water.TUN} + // config = water.Config{DeviceType: water.TUN} + panic("TUN mode is not currently supported on OpenBSD, please use TAP instead") default: panic("TUN/TAP name must be in format /dev/tunX or /dev/tapX") } From 7c0102e43d9c5bee0583954b77cb6087ace33ee1 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Thu, 1 Mar 2018 15:31:49 +0000 Subject: [PATCH 6/9] Be a little bit less verbose on OpenBSD --- src/yggdrasil/tun_openbsd.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/yggdrasil/tun_openbsd.go b/src/yggdrasil/tun_openbsd.go index 8c6386d1..c58518e6 100644 --- a/src/yggdrasil/tun_openbsd.go +++ b/src/yggdrasil/tun_openbsd.go @@ -3,7 +3,6 @@ package yggdrasil import "os/exec" -import "strings" import "unsafe" import "syscall" import "golang.org/x/sys/unix" @@ -90,13 +89,17 @@ func (tun *tunDevice) setupAddress(addr string) error { var err error var ti tuninfo + tun.core.log.Printf("Interface name: %s", tun.iface.Name()) + tun.core.log.Printf("Interface IPv6: %s", addr) + tun.core.log.Printf("Interface MTU: %d", tun.mtu) + // Get the existing interface flags if _, _, errno := unix.Syscall(unix.SYS_IOCTL, uintptr(fd), uintptr(TUNGIFINFO), uintptr(unsafe.Pointer(&ti))); errno != 0 { err = errno tun.core.log.Printf("Error in TUNGIFINFO: %v", errno) return err } - tun.core.log.Printf("TUNGIFINFO: %+v", ti) + //tun.core.log.Printf("TUNGIFINFO: %+v", ti) // Set the new MTU ti.tun_mtu = uint16(tun.mtu) @@ -112,7 +115,7 @@ func (tun *tunDevice) setupAddress(addr string) error { } // Set the new interface flags - tun.core.log.Printf("TUNSIFINFO: %+v", ti) + //tun.core.log.Printf("TUNSIFINFO: %+v", ti) if _, _, errno := unix.Syscall(unix.SYS_IOCTL, uintptr(fd), uintptr(TUNSIFINFO), uintptr(unsafe.Pointer(&ti))); errno != 0 { err = errno tun.core.log.Printf("Error in TUNSIFINFO: %v", errno) @@ -121,7 +124,7 @@ func (tun *tunDevice) setupAddress(addr string) error { // Set address cmd := exec.Command("ifconfig", tun.iface.Name(), "inet6", addr) - tun.core.log.Printf("ifconfig command: %v", strings.Join(cmd.Args, " ")) + //tun.core.log.Printf("ifconfig command: %v", strings.Join(cmd.Args, " ")) output, err := cmd.CombinedOutput() if err != nil { tun.core.log.Printf("ifconfig failed: %v.", err) From bec898a32669e63a7fbfa52b287c826722faaf4a Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Sat, 3 Mar 2018 11:47:14 +0000 Subject: [PATCH 7/9] Don't allow exceeding maximum MTU for a given platform --- src/yggdrasil/tun.go | 11 +++++++++++ src/yggdrasil/tun_darwin.go | 8 +++++++- src/yggdrasil/tun_linux.go | 8 +++++++- src/yggdrasil/tun_openbsd.go | 8 +++++++- src/yggdrasil/tun_other.go | 8 +++++++- src/yggdrasil/tun_windows.go | 8 +++++++- 6 files changed, 46 insertions(+), 5 deletions(-) diff --git a/src/yggdrasil/tun.go b/src/yggdrasil/tun.go index b22a341a..946467ad 100644 --- a/src/yggdrasil/tun.go +++ b/src/yggdrasil/tun.go @@ -27,6 +27,17 @@ type tunDevice struct { iface tunInterface } +type tunDefaultParameters struct { + maxMTU int +} + +func getMTUFromMax(mtu int) int { + if mtu > defaultTUNParameters().maxMTU { + return defaultTUNParameters().maxMTU + } + return mtu +} + func (tun *tunDevice) init(core *Core) { tun.core = core tun.icmpv6.init(tun) diff --git a/src/yggdrasil/tun_darwin.go b/src/yggdrasil/tun_darwin.go index eb1489f3..98abc1bf 100644 --- a/src/yggdrasil/tun_darwin.go +++ b/src/yggdrasil/tun_darwin.go @@ -10,6 +10,12 @@ import "golang.org/x/sys/unix" import water "github.com/neilalexander/water" +func defaultTUNParameters() tunDefaultParameters { + return tunDefaultParameters{ + maxMTU: 65535, + } +} + func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) error { if iftapmode { tun.core.log.Printf("TAP mode is not supported on this platform, defaulting to TUN") @@ -20,7 +26,7 @@ func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) panic(err) } tun.iface = iface - tun.mtu = mtu + tun.mtu = getMTUFromMax(mtu) return tun.setupAddress(addr) } diff --git a/src/yggdrasil/tun_linux.go b/src/yggdrasil/tun_linux.go index 5fdf6409..9287ff37 100644 --- a/src/yggdrasil/tun_linux.go +++ b/src/yggdrasil/tun_linux.go @@ -9,6 +9,12 @@ import "strings" import water "github.com/neilalexander/water" +func defaultTUNParameters() tunDefaultParameters { + return tunDefaultParameters{ + maxMTU: 65535, + } +} + func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) error { var config water.Config if iftapmode { @@ -24,7 +30,7 @@ func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) panic(err) } tun.iface = iface - tun.mtu = mtu //1280 // Lets default to the smallest thing allowed for now + tun.mtu = getMTUFromMax(mtu) return tun.setupAddress(addr) } diff --git a/src/yggdrasil/tun_openbsd.go b/src/yggdrasil/tun_openbsd.go index c58518e6..84ba2d9b 100644 --- a/src/yggdrasil/tun_openbsd.go +++ b/src/yggdrasil/tun_openbsd.go @@ -15,6 +15,12 @@ import water "github.com/neilalexander/water" // to disable the PI header when in TUN mode, so we need to modify the read/ // writes to handle those first four bytes +func defaultTUNParameters() tunDefaultParameters { + return tunDefaultParameters{ + maxMTU: 16384, + } +} + // Warning! When porting this to other BSDs, the tuninfo struct can appear with // the fields in a different order, and the consts below might also have // different values @@ -80,7 +86,7 @@ func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) panic(err) } tun.iface = iface - tun.mtu = mtu //1280 // Lets default to the smallest thing allowed for now + tun.mtu = getMTUFromMax(mtu) return tun.setupAddress(addr) } diff --git a/src/yggdrasil/tun_other.go b/src/yggdrasil/tun_other.go index ec56408e..5e33feec 100644 --- a/src/yggdrasil/tun_other.go +++ b/src/yggdrasil/tun_other.go @@ -7,6 +7,12 @@ import water "github.com/neilalexander/water" // This is to catch unsupported platforms // If your platform supports tun devices, you could try configuring it manually +func defaultTUNParameters() tunDefaultParameters { + return tunDefaultParameters{ + maxMTU: 65535, + } +} + func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) error { var config water.Config if iftapmode { @@ -19,7 +25,7 @@ func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) panic(err) } tun.iface = iface - tun.mtu = mtu //1280 // Lets default to the smallest thing allowed for now + tun.mtu = getMTUFromMax(mtu) return tun.setupAddress(addr) } diff --git a/src/yggdrasil/tun_windows.go b/src/yggdrasil/tun_windows.go index a2108fe9..9e8dda43 100644 --- a/src/yggdrasil/tun_windows.go +++ b/src/yggdrasil/tun_windows.go @@ -7,6 +7,12 @@ import "fmt" // This is to catch Windows platforms +func defaultTUNParameters() tunDefaultParameters { + return tunDefaultParameters{ + maxMTU: 65535, + } +} + func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) error { if !iftapmode { tun.core.log.Printf("TUN mode is not supported on this platform, defaulting to TAP") @@ -41,7 +47,7 @@ func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) panic(err) } tun.iface = iface - tun.mtu = mtu + tun.mtu = getMTUFromMax(mtu) err = tun.setupMTU(tun.mtu) if err != nil { panic(err) From 4917ea3dd2ba06de2a4d42c3dca87c8f4c13ff3a Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Sat, 3 Mar 2018 12:30:54 +0000 Subject: [PATCH 8/9] Per-platform TUN defaults --- src/yggdrasil/debug.go | 14 ++++++++++++++ src/yggdrasil/tun.go | 13 +++++++++---- src/yggdrasil/tun_darwin.go | 9 ++++++--- src/yggdrasil/tun_linux.go | 9 ++++++--- src/yggdrasil/tun_openbsd.go | 9 ++++++--- src/yggdrasil/tun_other.go | 9 ++++++--- src/yggdrasil/tun_windows.go | 9 ++++++--- yggdrasil.go | 10 +++------- 8 files changed, 56 insertions(+), 26 deletions(-) diff --git a/src/yggdrasil/debug.go b/src/yggdrasil/debug.go index 79fa0596..a93b367f 100644 --- a/src/yggdrasil/debug.go +++ b/src/yggdrasil/debug.go @@ -159,6 +159,20 @@ func (c *Core) DEBUG_getDHTSize() int { return total } +// TUN defaults + +func (c *Core) DEBUG_GetTUNDefaultIfName() string { + return getDefaults().defaultIfName +} + +func (c *Core) DEBUG_GetTUNDefaultIfMTU() int { + return getDefaults().defaultIfMTU +} + +func (c *Core) DEBUG_GetTUNDefaultIfTAPMode() bool { + return getDefaults().defaultIfTAPMode +} + // udpInterface // FIXME udpInterface isn't exported // So debug functions need to work differently... diff --git a/src/yggdrasil/tun.go b/src/yggdrasil/tun.go index 946467ad..db65338d 100644 --- a/src/yggdrasil/tun.go +++ b/src/yggdrasil/tun.go @@ -5,6 +5,8 @@ package yggdrasil import "os" import ethernet "github.com/songgao/packets/ethernet" +const DEFAULT_MTU = 65535 + const IPv6_HEADER_LENGTH = 40 const ETHER_HEADER_LENGTH = 14 @@ -28,12 +30,15 @@ type tunDevice struct { } type tunDefaultParameters struct { - maxMTU int + maximumIfMTU int + defaultIfMTU int + defaultIfName string + defaultIfTAPMode bool } -func getMTUFromMax(mtu int) int { - if mtu > defaultTUNParameters().maxMTU { - return defaultTUNParameters().maxMTU +func getSupportedMTU(mtu int) int { + if mtu > getDefaults().maximumIfMTU { + return getDefaults().maximumIfMTU } return mtu } diff --git a/src/yggdrasil/tun_darwin.go b/src/yggdrasil/tun_darwin.go index 98abc1bf..9a0c7127 100644 --- a/src/yggdrasil/tun_darwin.go +++ b/src/yggdrasil/tun_darwin.go @@ -10,9 +10,12 @@ import "golang.org/x/sys/unix" import water "github.com/neilalexander/water" -func defaultTUNParameters() tunDefaultParameters { +func getDefaults() tunDefaultParameters { return tunDefaultParameters{ - maxMTU: 65535, + maximumIfMTU: 65535, + defaultIfMTU: DEFAULT_MTU, + defaultIfName: "auto", + defaultIfTAPMode: false, } } @@ -26,7 +29,7 @@ func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) panic(err) } tun.iface = iface - tun.mtu = getMTUFromMax(mtu) + tun.mtu = getSupportedMTU(mtu) return tun.setupAddress(addr) } diff --git a/src/yggdrasil/tun_linux.go b/src/yggdrasil/tun_linux.go index 9287ff37..be231719 100644 --- a/src/yggdrasil/tun_linux.go +++ b/src/yggdrasil/tun_linux.go @@ -9,9 +9,12 @@ import "strings" import water "github.com/neilalexander/water" -func defaultTUNParameters() tunDefaultParameters { +func getDefaults() tunDefaultParameters { return tunDefaultParameters{ - maxMTU: 65535, + maximumIfMTU: 65535, + defaultIfMTU: DEFAULT_MTU, + defaultIfName: "auto", + defaultIfTAPMode: false, } } @@ -30,7 +33,7 @@ func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) panic(err) } tun.iface = iface - tun.mtu = getMTUFromMax(mtu) + tun.mtu = getSupportedMTU(mtu) return tun.setupAddress(addr) } diff --git a/src/yggdrasil/tun_openbsd.go b/src/yggdrasil/tun_openbsd.go index 84ba2d9b..24f0f709 100644 --- a/src/yggdrasil/tun_openbsd.go +++ b/src/yggdrasil/tun_openbsd.go @@ -15,9 +15,12 @@ import water "github.com/neilalexander/water" // to disable the PI header when in TUN mode, so we need to modify the read/ // writes to handle those first four bytes -func defaultTUNParameters() tunDefaultParameters { +func getDefaults() tunDefaultParameters { return tunDefaultParameters{ - maxMTU: 16384, + maximumIfMTU: 16384, + defaultIfMTU: DEFAULT_MTU, + defaultIfName: "/dev/tap0", + defaultIfTAPMode: true, } } @@ -86,7 +89,7 @@ func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) panic(err) } tun.iface = iface - tun.mtu = getMTUFromMax(mtu) + tun.mtu = getSupportedMTU(mtu) return tun.setupAddress(addr) } diff --git a/src/yggdrasil/tun_other.go b/src/yggdrasil/tun_other.go index 5e33feec..0f4ded6f 100644 --- a/src/yggdrasil/tun_other.go +++ b/src/yggdrasil/tun_other.go @@ -7,9 +7,12 @@ import water "github.com/neilalexander/water" // This is to catch unsupported platforms // If your platform supports tun devices, you could try configuring it manually -func defaultTUNParameters() tunDefaultParameters { +func getDefaults() tunDefaultParameters { return tunDefaultParameters{ - maxMTU: 65535, + maximumIfMTU: 65535, + defaultIfMTU: DEFAULT_MTU, + defaultIfName: "none", + defaultIfTAPMode: false, } } @@ -25,7 +28,7 @@ func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) panic(err) } tun.iface = iface - tun.mtu = getMTUFromMax(mtu) + tun.mtu = getSupportedMTU(mtu) return tun.setupAddress(addr) } diff --git a/src/yggdrasil/tun_windows.go b/src/yggdrasil/tun_windows.go index 9e8dda43..d23b201e 100644 --- a/src/yggdrasil/tun_windows.go +++ b/src/yggdrasil/tun_windows.go @@ -7,9 +7,12 @@ import "fmt" // This is to catch Windows platforms -func defaultTUNParameters() tunDefaultParameters { +func getDefaults() tunDefaultParameters { return tunDefaultParameters{ - maxMTU: 65535, + maximumIfMTU: 65535, + defaultIfMTU: DEFAULT_MTU, + defaultIfName: "auto", + defaultIfTAPMode: true, } } @@ -47,7 +50,7 @@ func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) panic(err) } tun.iface = iface - tun.mtu = getMTUFromMax(mtu) + tun.mtu = getSupportedMTU(mtu) err = tun.setupMTU(tun.mtu) if err != nil { panic(err) diff --git a/yggdrasil.go b/yggdrasil.go index 50a9c63c..0184dede 100644 --- a/yggdrasil.go +++ b/yggdrasil.go @@ -117,13 +117,9 @@ func generateConfig() *nodeConfig { cfg.Peers = []string{} cfg.Multicast = true cfg.LinkLocal = "" - cfg.IfName = "auto" - cfg.IfMTU = 1280 - if runtime.GOOS == "windows" { - cfg.IfTAPMode = true - } else { - cfg.IfTAPMode = false - } + cfg.IfName = core.DEBUG_GetTUNDefaultIfName() + cfg.IfMTU = core.DEBUG_GetTUNDefaultIfMTU() + cfg.IfTAPMode = core.DEBUG_GetTUNDefaultIfTAPMode() return &cfg } From 4e5627f933bd0801f66c5da61954f6f5d8c6fcce Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Sat, 3 Mar 2018 12:43:39 +0000 Subject: [PATCH 9/9] Update default interface MTU to use per-platform instead of global value --- src/yggdrasil/tun.go | 2 -- src/yggdrasil/tun_darwin.go | 2 +- src/yggdrasil/tun_linux.go | 2 +- src/yggdrasil/tun_openbsd.go | 2 +- src/yggdrasil/tun_other.go | 2 +- src/yggdrasil/tun_windows.go | 2 +- 6 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/yggdrasil/tun.go b/src/yggdrasil/tun.go index db65338d..ec883454 100644 --- a/src/yggdrasil/tun.go +++ b/src/yggdrasil/tun.go @@ -5,8 +5,6 @@ package yggdrasil import "os" import ethernet "github.com/songgao/packets/ethernet" -const DEFAULT_MTU = 65535 - const IPv6_HEADER_LENGTH = 40 const ETHER_HEADER_LENGTH = 14 diff --git a/src/yggdrasil/tun_darwin.go b/src/yggdrasil/tun_darwin.go index 9a0c7127..f16ed7c6 100644 --- a/src/yggdrasil/tun_darwin.go +++ b/src/yggdrasil/tun_darwin.go @@ -13,7 +13,7 @@ import water "github.com/neilalexander/water" func getDefaults() tunDefaultParameters { return tunDefaultParameters{ maximumIfMTU: 65535, - defaultIfMTU: DEFAULT_MTU, + defaultIfMTU: 65535, defaultIfName: "auto", defaultIfTAPMode: false, } diff --git a/src/yggdrasil/tun_linux.go b/src/yggdrasil/tun_linux.go index be231719..09206a5f 100644 --- a/src/yggdrasil/tun_linux.go +++ b/src/yggdrasil/tun_linux.go @@ -12,7 +12,7 @@ import water "github.com/neilalexander/water" func getDefaults() tunDefaultParameters { return tunDefaultParameters{ maximumIfMTU: 65535, - defaultIfMTU: DEFAULT_MTU, + defaultIfMTU: 65535, defaultIfName: "auto", defaultIfTAPMode: false, } diff --git a/src/yggdrasil/tun_openbsd.go b/src/yggdrasil/tun_openbsd.go index 24f0f709..dbdf3ea2 100644 --- a/src/yggdrasil/tun_openbsd.go +++ b/src/yggdrasil/tun_openbsd.go @@ -18,7 +18,7 @@ import water "github.com/neilalexander/water" func getDefaults() tunDefaultParameters { return tunDefaultParameters{ maximumIfMTU: 16384, - defaultIfMTU: DEFAULT_MTU, + defaultIfMTU: 16384, defaultIfName: "/dev/tap0", defaultIfTAPMode: true, } diff --git a/src/yggdrasil/tun_other.go b/src/yggdrasil/tun_other.go index 0f4ded6f..ddaa09c0 100644 --- a/src/yggdrasil/tun_other.go +++ b/src/yggdrasil/tun_other.go @@ -10,7 +10,7 @@ import water "github.com/neilalexander/water" func getDefaults() tunDefaultParameters { return tunDefaultParameters{ maximumIfMTU: 65535, - defaultIfMTU: DEFAULT_MTU, + defaultIfMTU: 65535, defaultIfName: "none", defaultIfTAPMode: false, } diff --git a/src/yggdrasil/tun_windows.go b/src/yggdrasil/tun_windows.go index d23b201e..80890f89 100644 --- a/src/yggdrasil/tun_windows.go +++ b/src/yggdrasil/tun_windows.go @@ -10,7 +10,7 @@ import "fmt" func getDefaults() tunDefaultParameters { return tunDefaultParameters{ maximumIfMTU: 65535, - defaultIfMTU: DEFAULT_MTU, + defaultIfMTU: 65535, defaultIfName: "auto", defaultIfTAPMode: true, }