2019-03-28 00:30:25 +00:00
|
|
|
package tuntap
|
2018-01-24 10:59:01 +00:00
|
|
|
|
2018-06-12 22:50:08 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
water "github.com/yggdrasil-network/water"
|
|
|
|
)
|
2018-01-24 10:59:01 +00:00
|
|
|
|
|
|
|
// This is to catch Windows platforms
|
|
|
|
|
2018-06-12 21:45:53 +00:00
|
|
|
// Configures the TAP adapter with the correct IPv6 address and MTU. On Windows
|
|
|
|
// we don't make use of a direct operating system API to do this - we instead
|
|
|
|
// delegate the hard work to "netsh".
|
2019-03-28 00:30:25 +00:00
|
|
|
func (tun *TunAdapter) setup(ifname string, iftapmode bool, addr string, mtu int) error {
|
2018-02-11 21:45:44 +00:00
|
|
|
if !iftapmode {
|
2019-04-20 15:32:27 +00:00
|
|
|
tun.log.Warnln("TUN mode is not supported on this platform, defaulting to TAP")
|
2018-02-11 21:45:44 +00:00
|
|
|
}
|
2018-01-24 10:59:01 +00:00
|
|
|
config := water.Config{DeviceType: water.TAP}
|
|
|
|
config.PlatformSpecificParams.ComponentID = "tap0901"
|
|
|
|
config.PlatformSpecificParams.Network = "169.254.0.1/32"
|
2018-05-09 15:42:24 +00:00
|
|
|
if ifname == "auto" {
|
|
|
|
config.PlatformSpecificParams.InterfaceName = ""
|
|
|
|
} else {
|
|
|
|
config.PlatformSpecificParams.InterfaceName = ifname
|
|
|
|
}
|
2018-01-24 10:59:01 +00:00
|
|
|
iface, err := water.New(config)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-01-25 03:14:26 +00:00
|
|
|
// Disable/enable the interface to resets its configuration (invalidating iface)
|
|
|
|
cmd := exec.Command("netsh", "interface", "set", "interface", iface.Name(), "admin=DISABLED")
|
2019-04-20 15:32:27 +00:00
|
|
|
tun.log.Printf("netsh command: %v", strings.Join(cmd.Args, " "))
|
2018-01-25 03:14:26 +00:00
|
|
|
output, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
2019-04-20 15:32:27 +00:00
|
|
|
tun.log.Errorf("Windows netsh failed: %v.", err)
|
|
|
|
tun.log.Traceln(string(output))
|
2018-01-25 03:14:26 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
cmd = exec.Command("netsh", "interface", "set", "interface", iface.Name(), "admin=ENABLED")
|
2019-04-20 15:32:27 +00:00
|
|
|
tun.log.Printf("netsh command: %v", strings.Join(cmd.Args, " "))
|
2018-01-25 03:14:26 +00:00
|
|
|
output, err = cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
2019-04-20 15:32:27 +00:00
|
|
|
tun.log.Errorf("Windows netsh failed: %v.", err)
|
|
|
|
tun.log.Traceln(string(output))
|
2018-01-25 03:14:26 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Get a new iface
|
|
|
|
iface, err = water.New(config)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-01-24 10:59:01 +00:00
|
|
|
tun.iface = iface
|
2018-03-03 12:30:54 +00:00
|
|
|
tun.mtu = getSupportedMTU(mtu)
|
2018-01-25 17:44:56 +00:00
|
|
|
err = tun.setupMTU(tun.mtu)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-07-19 09:15:26 +00:00
|
|
|
// Friendly output
|
2019-04-20 15:32:27 +00:00
|
|
|
tun.log.Infof("Interface name: %s", tun.iface.Name())
|
|
|
|
tun.log.Infof("Interface IPv6: %s", addr)
|
|
|
|
tun.log.Infof("Interface MTU: %d", tun.mtu)
|
2018-01-24 10:59:01 +00:00
|
|
|
return tun.setupAddress(addr)
|
|
|
|
}
|
|
|
|
|
2018-06-12 21:45:53 +00:00
|
|
|
// Sets the MTU of the TAP adapter.
|
2019-03-28 00:30:25 +00:00
|
|
|
func (tun *TunAdapter) setupMTU(mtu int) error {
|
2018-01-25 17:44:56 +00:00
|
|
|
// Set MTU
|
|
|
|
cmd := exec.Command("netsh", "interface", "ipv6", "set", "subinterface",
|
|
|
|
fmt.Sprintf("interface=%s", tun.iface.Name()),
|
|
|
|
fmt.Sprintf("mtu=%d", mtu),
|
|
|
|
"store=active")
|
2019-04-20 15:32:27 +00:00
|
|
|
tun.log.Debugln("netsh command: %v", strings.Join(cmd.Args, " "))
|
2018-01-25 17:44:56 +00:00
|
|
|
output, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
2019-04-20 15:32:27 +00:00
|
|
|
tun.log.Errorf("Windows netsh failed: %v.", err)
|
|
|
|
tun.log.Traceln(string(output))
|
2018-01-25 17:44:56 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-06-12 21:45:53 +00:00
|
|
|
// Sets the IPv6 address of the TAP adapter.
|
2019-03-28 00:30:25 +00:00
|
|
|
func (tun *TunAdapter) setupAddress(addr string) error {
|
2018-01-24 10:59:01 +00:00
|
|
|
// Set address
|
2018-01-25 03:14:26 +00:00
|
|
|
cmd := exec.Command("netsh", "interface", "ipv6", "add", "address",
|
2018-01-24 22:37:08 +00:00
|
|
|
fmt.Sprintf("interface=%s", tun.iface.Name()),
|
2018-01-25 03:14:26 +00:00
|
|
|
fmt.Sprintf("addr=%s", addr),
|
|
|
|
"store=active")
|
2019-04-20 15:32:27 +00:00
|
|
|
tun.log.Debugln("netsh command: %v", strings.Join(cmd.Args, " "))
|
2018-01-24 10:59:01 +00:00
|
|
|
output, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
2019-04-20 15:32:27 +00:00
|
|
|
tun.log.Errorf("Windows netsh failed: %v.", err)
|
|
|
|
tun.log.Traceln(string(output))
|
2018-01-24 10:59:01 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|