From c3600d14d666743e7082178addba57c9f82f5874 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Fri, 5 Jan 2018 23:35:59 +0000 Subject: [PATCH 1/5] Implement a tun interface to reduce dependency on Water library --- src/yggdrasil/tun.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/yggdrasil/tun.go b/src/yggdrasil/tun.go index 7c4a06ea..3a819982 100644 --- a/src/yggdrasil/tun.go +++ b/src/yggdrasil/tun.go @@ -2,16 +2,25 @@ package yggdrasil // This manages the tun driver to send/recv packets to/from applications -import water "github.com/songgao/water" +// import water "github.com/songgao/water" const IPv6_HEADER_LENGTH = 40 +type tunInterface interface { + IsTUN() bool + IsTAP() bool + Name() string + Read(to []byte) (int, error) + Write(from []byte) (int, error) + Close() error +} + type tunDevice struct { core *Core send chan<- []byte recv <-chan []byte mtu int - iface *water.Interface + iface tunInterface } func (tun *tunDevice) init(core *Core) { From d69190715f50befa2d15ee016e71563fc937952b Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Sat, 6 Jan 2018 16:39:30 +0000 Subject: [PATCH 2/5] Added a darwin tun module. It should work, but it doesn't! --- src/yggdrasil/tun_darwin.go | 105 ++++++++++++++++++++++++++++++++++++ src/yggdrasil/tun_other.go | 1 + 2 files changed, 106 insertions(+) create mode 100644 src/yggdrasil/tun_darwin.go diff --git a/src/yggdrasil/tun_darwin.go b/src/yggdrasil/tun_darwin.go new file mode 100644 index 00000000..7562a909 --- /dev/null +++ b/src/yggdrasil/tun_darwin.go @@ -0,0 +1,105 @@ +package yggdrasil + +// The darwin platform specific tun parts + +import "syscall" +import "unsafe" +import "strings" +import "strconv" +import "encoding/binary" + +import water "github.com/songgao/water" + +func (tun *tunDevice) setup(ifname string, addr string, mtu int) error { + config := water.Config{DeviceType: water.TUN} + iface, err := water.New(config) + if err != nil { + panic(err) + } + tun.iface = iface + tun.mtu = mtu + return tun.setupAddress(addr) +} + +const AF_INET6 = 30 +const IN6_IFF_NODAD = 0x0020 +const SIOCAIFADDR_IN6 = (0x80000000) | ((4 & 0x1fff) << 16) | uint32(byte('i'))<<8 | 26 + +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 +} + +func (tun *tunDevice) setupAddress(addr string) error { + var fd int + var err error + + if fd, err = syscall.Socket(syscall.AF_INET6, syscall.SOCK_DGRAM, 0); err != nil { + tun.core.log.Printf("Create AF_SYSTEM socket failed: %v.", err) + return err + } + + var ar in6_aliasreq + copy(ar.ifra_name[:], tun.iface.Name()) + + ar.ifra_prefixmask.sin6_len = uint8(unsafe.Sizeof(ar.ifra_prefixmask)) + b := make([]byte, 16); binary.LittleEndian.PutUint16(b, uint16(0xFF00)) + ar.ifra_prefixmask.sin6_addr[0] = uint16(binary.BigEndian.Uint16(b)) + + ar.ifra_addr.sin6_len = uint8(unsafe.Sizeof(ar.ifra_addr)) + ar.ifra_addr.sin6_family = AF_INET6 + parts := strings.Split(strings.TrimRight(addr, "/8"), ":") + for i := 0; i < 8; i++ { + addr, _ := strconv.ParseUint(parts[i], 16, 16) + b := make([]byte, 16); binary.LittleEndian.PutUint16(b, uint16(addr)) + ar.ifra_addr.sin6_addr[i] = uint16(binary.BigEndian.Uint16(b)) + } + + ar.ifra_lifetime.ia6t_vltime = 0xFFFFFFFF + ar.ifra_lifetime.ia6t_pltime = 0xFFFFFFFF + + tun.core.log.Printf("Interface name: %s", ar.ifra_name) + tun.core.log.Printf("Interface IPv6: %s", addr) + + /* + var bytes *[unsafe.Sizeof(ar)]byte = (*[unsafe.Sizeof(ar)]byte)(unsafe.Pointer(&ar)); + var out string; + for i := 0; i < int(unsafe.Sizeof(ar)); i ++ { + out += fmt.Sprintf("0x%02x", (*bytes)[i]); + if i % 8 == 7 { + out += "\n" + } else { + out += " " + } + } + tun.core.log.Printf("in6_aliasreq:\n%s", out); + */ + + if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, uintptr(fd), uintptr(SIOCAIFADDR_IN6), uintptr(unsafe.Pointer(&ar))); errno != 0 { + err = errno + tun.core.log.Printf("Error in SIOCAIFADDR_IN6: %v", errno) + return err + } + + return err +} diff --git a/src/yggdrasil/tun_other.go b/src/yggdrasil/tun_other.go index 92be426d..e525827c 100644 --- a/src/yggdrasil/tun_other.go +++ b/src/yggdrasil/tun_other.go @@ -1,4 +1,5 @@ // +build !linux +// +build !darwin package yggdrasil From 5d2dd061f08d2068bb330e5950d6cda1b5bb87fd Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Mon, 15 Jan 2018 14:14:18 +0000 Subject: [PATCH 3/5] Fix definition of SIOCAIFADDR_IN6 --- src/yggdrasil/tun_darwin.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/yggdrasil/tun_darwin.go b/src/yggdrasil/tun_darwin.go index 7562a909..4793a5c1 100644 --- a/src/yggdrasil/tun_darwin.go +++ b/src/yggdrasil/tun_darwin.go @@ -7,6 +7,7 @@ import "unsafe" import "strings" import "strconv" import "encoding/binary" +// import "fmt" import water "github.com/songgao/water" @@ -23,7 +24,7 @@ func (tun *tunDevice) setup(ifname string, addr string, mtu int) error { const AF_INET6 = 30 const IN6_IFF_NODAD = 0x0020 -const SIOCAIFADDR_IN6 = (0x80000000) | ((4 & 0x1fff) << 16) | uint32(byte('i'))<<8 | 26 +const SIOCAIFADDR_IN6 = 2155899162 type in6_addrlifetime struct { ia6t_expire float64 @@ -81,7 +82,7 @@ func (tun *tunDevice) setupAddress(addr string) error { tun.core.log.Printf("Interface name: %s", ar.ifra_name) tun.core.log.Printf("Interface IPv6: %s", addr) - /* + /* var bytes *[unsafe.Sizeof(ar)]byte = (*[unsafe.Sizeof(ar)]byte)(unsafe.Pointer(&ar)); var out string; for i := 0; i < int(unsafe.Sizeof(ar)); i ++ { From d7be706413e9ae8ed9c2f7c8229d83a8cc807bf0 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Mon, 15 Jan 2018 14:35:37 +0000 Subject: [PATCH 4/5] Move to unix.Syscall etc and tidy up --- src/yggdrasil/tun_darwin.go | 41 ++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/src/yggdrasil/tun_darwin.go b/src/yggdrasil/tun_darwin.go index 4793a5c1..efd9845d 100644 --- a/src/yggdrasil/tun_darwin.go +++ b/src/yggdrasil/tun_darwin.go @@ -2,12 +2,11 @@ package yggdrasil // The darwin platform specific tun parts -import "syscall" import "unsafe" import "strings" import "strconv" import "encoding/binary" -// import "fmt" +import "golang.org/x/sys/unix" import water "github.com/songgao/water" @@ -22,8 +21,6 @@ func (tun *tunDevice) setup(ifname string, addr string, mtu int) error { return tun.setupAddress(addr) } -const AF_INET6 = 30 -const IN6_IFF_NODAD = 0x0020 const SIOCAIFADDR_IN6 = 2155899162 type in6_addrlifetime struct { @@ -51,11 +48,16 @@ type in6_aliasreq struct { ifra_lifetime in6_addrlifetime } +type ifreq struct { + ifr_name [16]byte + ifru_mtu uint32 +} + func (tun *tunDevice) setupAddress(addr string) error { var fd int var err error - if fd, err = syscall.Socket(syscall.AF_INET6, syscall.SOCK_DGRAM, 0); err != nil { + if fd, err = unix.Socket(unix.AF_INET6, unix.SOCK_DGRAM, 0); err != nil { tun.core.log.Printf("Create AF_SYSTEM socket failed: %v.", err) return err } @@ -68,7 +70,7 @@ func (tun *tunDevice) setupAddress(addr string) error { ar.ifra_prefixmask.sin6_addr[0] = uint16(binary.BigEndian.Uint16(b)) ar.ifra_addr.sin6_len = uint8(unsafe.Sizeof(ar.ifra_addr)) - ar.ifra_addr.sin6_family = AF_INET6 + ar.ifra_addr.sin6_family = unix.AF_INET6 parts := strings.Split(strings.TrimRight(addr, "/8"), ":") for i := 0; i < 8; i++ { addr, _ := strconv.ParseUint(parts[i], 16, 16) @@ -79,28 +81,25 @@ func (tun *tunDevice) setupAddress(addr string) error { ar.ifra_lifetime.ia6t_vltime = 0xFFFFFFFF ar.ifra_lifetime.ia6t_pltime = 0xFFFFFFFF + var ir ifreq + copy(ir.ifr_name[:], tun.iface.Name()) + ir.ifru_mtu = 1280 + tun.core.log.Printf("Interface name: %s", ar.ifra_name) tun.core.log.Printf("Interface IPv6: %s", addr) + tun.core.log.Printf("Interface MTU: %d", ir.ifru_mtu) - /* - var bytes *[unsafe.Sizeof(ar)]byte = (*[unsafe.Sizeof(ar)]byte)(unsafe.Pointer(&ar)); - var out string; - for i := 0; i < int(unsafe.Sizeof(ar)); i ++ { - out += fmt.Sprintf("0x%02x", (*bytes)[i]); - if i % 8 == 7 { - out += "\n" - } else { - out += " " - } - } - tun.core.log.Printf("in6_aliasreq:\n%s", out); - */ - - if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, uintptr(fd), uintptr(SIOCAIFADDR_IN6), uintptr(unsafe.Pointer(&ar))); errno != 0 { + if _, _, errno := unix.Syscall(unix.SYS_IOCTL, uintptr(fd), uintptr(SIOCAIFADDR_IN6), uintptr(unsafe.Pointer(&ar))); errno != 0 { err = errno tun.core.log.Printf("Error in SIOCAIFADDR_IN6: %v", errno) return err } + if _, _, errno := unix.Syscall(unix.SYS_IOCTL, uintptr(fd), uintptr(unix.SIOCSIFMTU), uintptr(unsafe.Pointer(&ir))); errno != 0 { + err = errno + tun.core.log.Printf("Error in SIOCSIFMTU: %v", errno) + return err + } + return err } From 62c45e7cc72610203a489a8149eb36bb7c5576b6 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Mon, 15 Jan 2018 14:39:07 +0000 Subject: [PATCH 5/5] Use MTU from interface definition --- src/yggdrasil/tun_darwin.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/yggdrasil/tun_darwin.go b/src/yggdrasil/tun_darwin.go index efd9845d..df191355 100644 --- a/src/yggdrasil/tun_darwin.go +++ b/src/yggdrasil/tun_darwin.go @@ -83,7 +83,7 @@ func (tun *tunDevice) setupAddress(addr string) error { var ir ifreq copy(ir.ifr_name[:], tun.iface.Name()) - ir.ifru_mtu = 1280 + ir.ifru_mtu = uint32(tun.mtu) tun.core.log.Printf("Interface name: %s", ar.ifra_name) tun.core.log.Printf("Interface IPv6: %s", addr)