TUN/TAP now uses config, log, etc from adapter.go

This commit is contained in:
Neil Alexander 2019-04-01 20:10:14 +01:00
parent 58f5cc88d0
commit 350b51cabb
No known key found for this signature in database
GPG Key ID: A02A2019A2BB0944
7 changed files with 65 additions and 65 deletions

View File

@ -35,8 +35,6 @@ type TunAdapter struct {
yggdrasil.Adapter yggdrasil.Adapter
addr address.Address addr address.Address
subnet address.Subnet subnet address.Subnet
log *log.Logger
config *config.NodeState
icmpv6 ICMPv6 icmpv6 ICMPv6
mtu int mtu int
iface *water.Interface iface *water.Interface
@ -98,20 +96,18 @@ func MaximumMTU() int {
// Init initialises the TUN/TAP adapter. // Init initialises the TUN/TAP adapter.
func (tun *TunAdapter) Init(config *config.NodeState, log *log.Logger, send chan<- []byte, recv <-chan []byte, reject <-chan yggdrasil.RejectedPacket) { func (tun *TunAdapter) Init(config *config.NodeState, log *log.Logger, send chan<- []byte, recv <-chan []byte, reject <-chan yggdrasil.RejectedPacket) {
tun.config = config
tun.log = log
tun.Adapter.Init(config, log, send, recv, reject) tun.Adapter.Init(config, log, send, recv, reject)
tun.icmpv6.Init(tun) tun.icmpv6.Init(tun)
go func() { go func() {
for { for {
e := <-tun.Reconfigure e := <-tun.Reconfigure
tun.config.Mutex.RLock() tun.Config.Mutex.RLock()
updated := tun.config.Current.IfName != tun.config.Previous.IfName || updated := tun.Config.Current.IfName != tun.Config.Previous.IfName ||
tun.config.Current.IfTAPMode != tun.config.Previous.IfTAPMode || tun.Config.Current.IfTAPMode != tun.Config.Previous.IfTAPMode ||
tun.config.Current.IfMTU != tun.config.Previous.IfMTU tun.Config.Current.IfMTU != tun.Config.Previous.IfMTU
tun.config.Mutex.RUnlock() tun.Config.Mutex.RUnlock()
if updated { if updated {
tun.log.Warnln("Reconfiguring TUN/TAP is not supported yet") tun.Log.Warnln("Reconfiguring TUN/TAP is not supported yet")
e <- nil e <- nil
} else { } else {
e <- nil e <- nil
@ -125,34 +121,34 @@ func (tun *TunAdapter) Init(config *config.NodeState, log *log.Logger, send chan
func (tun *TunAdapter) Start(a address.Address, s address.Subnet) error { func (tun *TunAdapter) Start(a address.Address, s address.Subnet) error {
tun.addr = a tun.addr = a
tun.subnet = s tun.subnet = s
if tun.config == nil { if tun.Config == nil {
return errors.New("No configuration available to TUN/TAP") return errors.New("No configuration available to TUN/TAP")
} }
tun.config.Mutex.RLock() tun.Config.Mutex.RLock()
ifname := tun.config.Current.IfName ifname := tun.Config.Current.IfName
iftapmode := tun.config.Current.IfTAPMode iftapmode := tun.Config.Current.IfTAPMode
addr := fmt.Sprintf("%s/%d", net.IP(tun.addr[:]).String(), 8*len(address.GetPrefix())-1) addr := fmt.Sprintf("%s/%d", net.IP(tun.addr[:]).String(), 8*len(address.GetPrefix())-1)
mtu := tun.config.Current.IfMTU mtu := tun.Config.Current.IfMTU
tun.config.Mutex.RUnlock() tun.Config.Mutex.RUnlock()
if ifname != "none" { if ifname != "none" {
if err := tun.setup(ifname, iftapmode, addr, mtu); err != nil { if err := tun.setup(ifname, iftapmode, addr, mtu); err != nil {
return err return err
} }
} }
if ifname == "none" || ifname == "dummy" { if ifname == "none" || ifname == "dummy" {
tun.log.Debugln("Not starting TUN/TAP as ifname is none or dummy") tun.Log.Debugln("Not starting TUN/TAP as ifname is none or dummy")
return nil return nil
} }
tun.mutex.Lock() tun.mutex.Lock()
tun.isOpen = true tun.isOpen = true
tun.mutex.Unlock() tun.mutex.Unlock()
go func() { go func() {
tun.log.Debugln("Starting TUN/TAP reader goroutine") tun.Log.Debugln("Starting TUN/TAP reader goroutine")
tun.log.Errorln("WARNING: tun.read() exited with error:", tun.read()) tun.Log.Errorln("WARNING: tun.read() exited with error:", tun.read())
}() }()
go func() { go func() {
tun.log.Debugln("Starting TUN/TAP writer goroutine") tun.Log.Debugln("Starting TUN/TAP writer goroutine")
tun.log.Errorln("WARNING: tun.write() exited with error:", tun.write()) tun.Log.Errorln("WARNING: tun.write() exited with error:", tun.write())
}() }()
if iftapmode { if iftapmode {
go func() { go func() {

View File

@ -109,14 +109,14 @@ func (tun *TunAdapter) setupAddress(addr string) error {
// Create system socket // Create system socket
if sfd, err = unix.Socket(unix.AF_INET, unix.SOCK_DGRAM, 0); err != nil { if sfd, err = unix.Socket(unix.AF_INET, unix.SOCK_DGRAM, 0); err != nil {
tun.log.Printf("Create AF_INET socket failed: %v.", err) tun.Log.Printf("Create AF_INET socket failed: %v.", err)
return err return err
} }
// Friendly output // Friendly output
tun.log.Infof("Interface name: %s", tun.iface.Name()) tun.Log.Infof("Interface name: %s", tun.iface.Name())
tun.log.Infof("Interface IPv6: %s", addr) tun.Log.Infof("Interface IPv6: %s", addr)
tun.log.Infof("Interface MTU: %d", tun.mtu) tun.Log.Infof("Interface MTU: %d", tun.mtu)
// Create the MTU request // Create the MTU request
var ir in6_ifreq_mtu var ir in6_ifreq_mtu
@ -126,15 +126,15 @@ func (tun *TunAdapter) setupAddress(addr string) error {
// Set the MTU // Set the MTU
if _, _, errno := unix.Syscall(unix.SYS_IOCTL, uintptr(sfd), uintptr(syscall.SIOCSIFMTU), uintptr(unsafe.Pointer(&ir))); errno != 0 { if _, _, errno := unix.Syscall(unix.SYS_IOCTL, uintptr(sfd), uintptr(syscall.SIOCSIFMTU), uintptr(unsafe.Pointer(&ir))); errno != 0 {
err = errno err = errno
tun.log.Errorf("Error in SIOCSIFMTU: %v", errno) tun.Log.Errorf("Error in SIOCSIFMTU: %v", errno)
// Fall back to ifconfig to set the MTU // Fall back to ifconfig to set the MTU
cmd := exec.Command("ifconfig", tun.iface.Name(), "mtu", string(tun.mtu)) cmd := exec.Command("ifconfig", tun.iface.Name(), "mtu", string(tun.mtu))
tun.log.Warnf("Using ifconfig as fallback: %v", strings.Join(cmd.Args, " ")) tun.Log.Warnf("Using ifconfig as fallback: %v", strings.Join(cmd.Args, " "))
output, err := cmd.CombinedOutput() output, err := cmd.CombinedOutput()
if err != nil { if err != nil {
tun.log.Errorf("SIOCSIFMTU fallback failed: %v.", err) tun.Log.Errorf("SIOCSIFMTU fallback failed: %v.", err)
tun.log.Traceln(string(output)) tun.Log.Traceln(string(output))
} }
} }
@ -155,15 +155,15 @@ func (tun *TunAdapter) setupAddress(addr string) error {
// Set the interface address // Set the interface address
if _, _, errno := unix.Syscall(unix.SYS_IOCTL, uintptr(sfd), uintptr(SIOCSIFADDR_IN6), uintptr(unsafe.Pointer(&ar))); errno != 0 { if _, _, errno := unix.Syscall(unix.SYS_IOCTL, uintptr(sfd), uintptr(SIOCSIFADDR_IN6), uintptr(unsafe.Pointer(&ar))); errno != 0 {
err = errno err = errno
tun.log.Errorf("Error in SIOCSIFADDR_IN6: %v", errno) tun.Log.Errorf("Error in SIOCSIFADDR_IN6: %v", errno)
// Fall back to ifconfig to set the address // Fall back to ifconfig to set the address
cmd := exec.Command("ifconfig", tun.iface.Name(), "inet6", addr) cmd := exec.Command("ifconfig", tun.iface.Name(), "inet6", addr)
tun.log.Warnf("Using ifconfig as fallback: %v", strings.Join(cmd.Args, " ")) tun.Log.Warnf("Using ifconfig as fallback: %v", strings.Join(cmd.Args, " "))
output, err := cmd.CombinedOutput() output, err := cmd.CombinedOutput()
if err != nil { if err != nil {
tun.log.Errorf("SIOCSIFADDR_IN6 fallback failed: %v.", err) tun.Log.Errorf("SIOCSIFADDR_IN6 fallback failed: %v.", err)
tun.log.Traceln(string(output)) tun.Log.Traceln(string(output))
} }
} }

View File

@ -18,7 +18,7 @@ import (
// Configures the "utun" adapter with the correct IPv6 address and MTU. // Configures the "utun" adapter with the correct IPv6 address and MTU.
func (tun *TunAdapter) setup(ifname string, iftapmode bool, addr string, mtu int) error { func (tun *TunAdapter) setup(ifname string, iftapmode bool, addr string, mtu int) error {
if iftapmode { if iftapmode {
tun.log.Warnln("TAP mode is not supported on this platform, defaulting to TUN") tun.Log.Warnln("TAP mode is not supported on this platform, defaulting to TUN")
} }
config := water.Config{DeviceType: water.TUN} config := water.Config{DeviceType: water.TUN}
iface, err := water.New(config) iface, err := water.New(config)
@ -69,7 +69,7 @@ func (tun *TunAdapter) setupAddress(addr string) error {
var err error var err error
if fd, err = unix.Socket(unix.AF_INET6, unix.SOCK_DGRAM, 0); err != nil { if fd, err = unix.Socket(unix.AF_INET6, unix.SOCK_DGRAM, 0); err != nil {
tun.log.Printf("Create AF_SYSTEM socket failed: %v.", err) tun.Log.Printf("Create AF_SYSTEM socket failed: %v.", err)
return err return err
} }
@ -98,19 +98,19 @@ func (tun *TunAdapter) setupAddress(addr string) error {
copy(ir.ifr_name[:], tun.iface.Name()) copy(ir.ifr_name[:], tun.iface.Name())
ir.ifru_mtu = uint32(tun.mtu) ir.ifru_mtu = uint32(tun.mtu)
tun.log.Infof("Interface name: %s", ar.ifra_name) tun.Log.Infof("Interface name: %s", ar.ifra_name)
tun.log.Infof("Interface IPv6: %s", addr) tun.Log.Infof("Interface IPv6: %s", addr)
tun.log.Infof("Interface MTU: %d", ir.ifru_mtu) tun.Log.Infof("Interface MTU: %d", ir.ifru_mtu)
if _, _, errno := unix.Syscall(unix.SYS_IOCTL, uintptr(fd), uintptr(darwin_SIOCAIFADDR_IN6), uintptr(unsafe.Pointer(&ar))); errno != 0 { if _, _, errno := unix.Syscall(unix.SYS_IOCTL, uintptr(fd), uintptr(darwin_SIOCAIFADDR_IN6), uintptr(unsafe.Pointer(&ar))); errno != 0 {
err = errno err = errno
tun.log.Errorf("Error in darwin_SIOCAIFADDR_IN6: %v", errno) tun.Log.Errorf("Error in darwin_SIOCAIFADDR_IN6: %v", errno)
return err return err
} }
if _, _, errno := unix.Syscall(unix.SYS_IOCTL, uintptr(fd), uintptr(unix.SIOCSIFMTU), uintptr(unsafe.Pointer(&ir))); errno != 0 { if _, _, errno := unix.Syscall(unix.SYS_IOCTL, uintptr(fd), uintptr(unix.SIOCSIFMTU), uintptr(unsafe.Pointer(&ir))); errno != 0 {
err = errno err = errno
tun.log.Errorf("Error in SIOCSIFMTU: %v", errno) tun.Log.Errorf("Error in SIOCSIFMTU: %v", errno)
return err return err
} }

View File

@ -40,9 +40,9 @@ func (tun *TunAdapter) setup(ifname string, iftapmode bool, addr string, mtu int
} }
} }
// Friendly output // Friendly output
tun.log.Infof("Interface name: %s", tun.iface.Name()) tun.Log.Infof("Interface name: %s", tun.iface.Name())
tun.log.Infof("Interface IPv6: %s", addr) tun.Log.Infof("Interface IPv6: %s", addr)
tun.log.Infof("Interface MTU: %d", tun.mtu) tun.Log.Infof("Interface MTU: %d", tun.mtu)
return tun.setupAddress(addr) return tun.setupAddress(addr)
} }

View File

@ -28,6 +28,6 @@ func (tun *TunAdapter) setup(ifname string, iftapmode bool, addr string, mtu int
// We don't know how to set the IPv6 address on an unknown platform, therefore // We don't know how to set the IPv6 address on an unknown platform, therefore
// write about it to stdout and don't try to do anything further. // write about it to stdout and don't try to do anything further.
func (tun *TunAdapter) setupAddress(addr string) error { func (tun *TunAdapter) setupAddress(addr string) error {
tun.log.Warnln("Platform not supported, you must set the address of", tun.iface.Name(), "to", addr) tun.Log.Warnln("Platform not supported, you must set the address of", tun.iface.Name(), "to", addr)
return nil return nil
} }

View File

@ -15,7 +15,7 @@ import (
// delegate the hard work to "netsh". // delegate the hard work to "netsh".
func (tun *TunAdapter) setup(ifname string, iftapmode bool, addr string, mtu int) error { func (tun *TunAdapter) setup(ifname string, iftapmode bool, addr string, mtu int) error {
if !iftapmode { if !iftapmode {
tun.log.Warnln("TUN mode is not supported on this platform, defaulting to TAP") tun.Log.Warnln("TUN mode is not supported on this platform, defaulting to TAP")
} }
config := water.Config{DeviceType: water.TAP} config := water.Config{DeviceType: water.TAP}
config.PlatformSpecificParams.ComponentID = "tap0901" config.PlatformSpecificParams.ComponentID = "tap0901"
@ -31,19 +31,19 @@ func (tun *TunAdapter) setup(ifname string, iftapmode bool, addr string, mtu int
} }
// Disable/enable the interface to resets its configuration (invalidating iface) // Disable/enable the interface to resets its configuration (invalidating iface)
cmd := exec.Command("netsh", "interface", "set", "interface", iface.Name(), "admin=DISABLED") cmd := exec.Command("netsh", "interface", "set", "interface", iface.Name(), "admin=DISABLED")
tun.log.Printf("netsh command: %v", strings.Join(cmd.Args, " ")) tun.Log.Printf("netsh command: %v", strings.Join(cmd.Args, " "))
output, err := cmd.CombinedOutput() output, err := cmd.CombinedOutput()
if err != nil { if err != nil {
tun.log.Errorf("Windows netsh failed: %v.", err) tun.Log.Errorf("Windows netsh failed: %v.", err)
tun.log.Traceln(string(output)) tun.Log.Traceln(string(output))
return err return err
} }
cmd = exec.Command("netsh", "interface", "set", "interface", iface.Name(), "admin=ENABLED") cmd = exec.Command("netsh", "interface", "set", "interface", iface.Name(), "admin=ENABLED")
tun.log.Printf("netsh command: %v", strings.Join(cmd.Args, " ")) tun.Log.Printf("netsh command: %v", strings.Join(cmd.Args, " "))
output, err = cmd.CombinedOutput() output, err = cmd.CombinedOutput()
if err != nil { if err != nil {
tun.log.Errorf("Windows netsh failed: %v.", err) tun.Log.Errorf("Windows netsh failed: %v.", err)
tun.log.Traceln(string(output)) tun.Log.Traceln(string(output))
return err return err
} }
// Get a new iface // Get a new iface
@ -58,9 +58,9 @@ func (tun *TunAdapter) setup(ifname string, iftapmode bool, addr string, mtu int
panic(err) panic(err)
} }
// Friendly output // Friendly output
tun.log.Infof("Interface name: %s", tun.iface.Name()) tun.Log.Infof("Interface name: %s", tun.iface.Name())
tun.log.Infof("Interface IPv6: %s", addr) tun.Log.Infof("Interface IPv6: %s", addr)
tun.log.Infof("Interface MTU: %d", tun.mtu) tun.Log.Infof("Interface MTU: %d", tun.mtu)
return tun.setupAddress(addr) return tun.setupAddress(addr)
} }
@ -71,11 +71,11 @@ func (tun *TunAdapter) setupMTU(mtu int) error {
fmt.Sprintf("interface=%s", tun.iface.Name()), fmt.Sprintf("interface=%s", tun.iface.Name()),
fmt.Sprintf("mtu=%d", mtu), fmt.Sprintf("mtu=%d", mtu),
"store=active") "store=active")
tun.log.Debugln("netsh command: %v", strings.Join(cmd.Args, " ")) tun.Log.Debugln("netsh command: %v", strings.Join(cmd.Args, " "))
output, err := cmd.CombinedOutput() output, err := cmd.CombinedOutput()
if err != nil { if err != nil {
tun.log.Errorf("Windows netsh failed: %v.", err) tun.Log.Errorf("Windows netsh failed: %v.", err)
tun.log.Traceln(string(output)) tun.Log.Traceln(string(output))
return err return err
} }
return nil return nil
@ -88,11 +88,11 @@ func (tun *TunAdapter) setupAddress(addr string) error {
fmt.Sprintf("interface=%s", tun.iface.Name()), fmt.Sprintf("interface=%s", tun.iface.Name()),
fmt.Sprintf("addr=%s", addr), fmt.Sprintf("addr=%s", addr),
"store=active") "store=active")
tun.log.Debugln("netsh command: %v", strings.Join(cmd.Args, " ")) tun.Log.Debugln("netsh command: %v", strings.Join(cmd.Args, " "))
output, err := cmd.CombinedOutput() output, err := cmd.CombinedOutput()
if err != nil { if err != nil {
tun.log.Errorf("Windows netsh failed: %v.", err) tun.Log.Errorf("Windows netsh failed: %v.", err)
tun.log.Traceln(string(output)) tun.Log.Traceln(string(output))
return err return err
} }
return nil return nil

View File

@ -6,13 +6,15 @@ import (
"github.com/yggdrasil-network/yggdrasil-go/src/config" "github.com/yggdrasil-network/yggdrasil-go/src/config"
) )
// Defines the minimum required struct members for an adapter type. This is now // Adapter defines the minimum required struct members for an adapter type. This
// the base type for adapters like tun.go. When implementing a new adapter type, // is now the base type for adapters like tun.go. When implementing a new
// you should extend the adapter struct with this one and should call the // adapter type, you should extend the adapter struct with this one and should
// Adapter.Init() function when initialising. // call the Adapter.Init() function when initialising.
type Adapter struct { type Adapter struct {
adapterImplementation adapterImplementation
Core *Core Core *Core
Config *config.NodeState
Log *log.Logger
Send chan<- []byte Send chan<- []byte
Recv <-chan []byte Recv <-chan []byte
Reject <-chan RejectedPacket Reject <-chan RejectedPacket
@ -31,11 +33,13 @@ type adapterImplementation interface {
Close() error Close() error
} }
// Initialises the adapter with the necessary channels to operate from the // Init initialises the adapter with the necessary channels to operate from the
// router. When defining a new Adapter type, the Adapter should call this // router. When defining a new Adapter type, the Adapter should call this
// function from within it's own Init function to set up the channels. It is // function from within it's own Init function to set up the channels. It is
// otherwise not expected for you to call this function directly. // otherwise not expected for you to call this function directly.
func (adapter *Adapter) Init(config *config.NodeState, log *log.Logger, send chan<- []byte, recv <-chan []byte, reject <-chan RejectedPacket) { func (adapter *Adapter) Init(config *config.NodeState, log *log.Logger, send chan<- []byte, recv <-chan []byte, reject <-chan RejectedPacket) {
adapter.Config = config
adapter.Log = log
adapter.Send = send adapter.Send = send
adapter.Recv = recv adapter.Recv = recv
adapter.Reject = reject adapter.Reject = reject