tun_water_windows.go 2.7 KB

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