tun_water_windows.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package overlay
  2. import (
  3. "fmt"
  4. "io"
  5. "net"
  6. "os/exec"
  7. "strconv"
  8. "github.com/sirupsen/logrus"
  9. "github.com/slackhq/nebula/cidr"
  10. "github.com/slackhq/nebula/iputil"
  11. "github.com/songgao/water"
  12. )
  13. type waterTun struct {
  14. Device string
  15. cidr *net.IPNet
  16. MTU int
  17. Routes []Route
  18. routeTree *cidr.Tree4
  19. *water.Interface
  20. }
  21. func newWaterTun(l *logrus.Logger, cidr *net.IPNet, defaultMTU int, routes []Route) (*waterTun, error) {
  22. routeTree, err := makeRouteTree(l, routes, false)
  23. if err != nil {
  24. return nil, err
  25. }
  26. // NOTE: You cannot set the deviceName under Windows, so you must check tun.Device after calling .Activate()
  27. return &waterTun{
  28. cidr: cidr,
  29. MTU: defaultMTU,
  30. Routes: routes,
  31. routeTree: routeTree,
  32. }, nil
  33. }
  34. func (t *waterTun) Activate() error {
  35. var err error
  36. t.Interface, err = water.New(water.Config{
  37. DeviceType: water.TUN,
  38. PlatformSpecificParams: water.PlatformSpecificParams{
  39. ComponentID: "tap0901",
  40. Network: t.cidr.String(),
  41. },
  42. })
  43. if err != nil {
  44. return fmt.Errorf("activate failed: %v", err)
  45. }
  46. t.Device = t.Interface.Name()
  47. // TODO use syscalls instead of exec.Command
  48. err = exec.Command(
  49. `C:\Windows\System32\netsh.exe`, "interface", "ipv4", "set", "address",
  50. fmt.Sprintf("name=%s", t.Device),
  51. "source=static",
  52. fmt.Sprintf("addr=%s", t.cidr.IP),
  53. fmt.Sprintf("mask=%s", net.IP(t.cidr.Mask)),
  54. "gateway=none",
  55. ).Run()
  56. if err != nil {
  57. return fmt.Errorf("failed to run 'netsh' to set address: %s", err)
  58. }
  59. err = exec.Command(
  60. `C:\Windows\System32\netsh.exe`, "interface", "ipv4", "set", "interface",
  61. t.Device,
  62. fmt.Sprintf("mtu=%d", t.MTU),
  63. ).Run()
  64. if err != nil {
  65. return fmt.Errorf("failed to run 'netsh' to set MTU: %s", err)
  66. }
  67. iface, err := net.InterfaceByName(t.Device)
  68. if err != nil {
  69. return fmt.Errorf("failed to find interface named %s: %v", t.Device, err)
  70. }
  71. for _, r := range t.Routes {
  72. if r.Via == nil || !r.Install {
  73. // We don't allow route MTUs so only install routes with a via
  74. continue
  75. }
  76. err = exec.Command(
  77. "C:\\Windows\\System32\\route.exe", "add", r.Cidr.String(), r.Via.String(), "IF", strconv.Itoa(iface.Index), "METRIC", strconv.Itoa(r.Metric),
  78. ).Run()
  79. if err != nil {
  80. return fmt.Errorf("failed to add the unsafe_route %s: %v", r.Cidr.String(), err)
  81. }
  82. }
  83. return nil
  84. }
  85. func (t *waterTun) RouteFor(ip iputil.VpnIp) iputil.VpnIp {
  86. r := t.routeTree.MostSpecificContains(ip)
  87. if r != nil {
  88. return r.(iputil.VpnIp)
  89. }
  90. return 0
  91. }
  92. func (t *waterTun) Cidr() *net.IPNet {
  93. return t.cidr
  94. }
  95. func (t *waterTun) Name() string {
  96. return t.Device
  97. }
  98. func (t *waterTun) Close() error {
  99. if t.Interface == nil {
  100. return nil
  101. }
  102. return t.Interface.Close()
  103. }
  104. func (t *waterTun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
  105. return nil, fmt.Errorf("TODO: multiqueue not implemented for windows")
  106. }