tun_windows_route_test.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. //go:build windows && !e2e_testing
  2. package overlay
  3. import (
  4. "bytes"
  5. "fmt"
  6. "net/netip"
  7. "strings"
  8. "testing"
  9. "github.com/slackhq/nebula/config"
  10. "github.com/slackhq/nebula/test"
  11. "github.com/stretchr/testify/assert"
  12. "github.com/stretchr/testify/require"
  13. "golang.org/x/sys/windows"
  14. "golang.zx2c4.com/wireguard/windows/tunnel/winipcfg"
  15. )
  16. type routeTest struct {
  17. c *config.C
  18. tun *winTun
  19. luid winipcfg.LUID
  20. log *bytes.Buffer
  21. }
  22. // newRouteTest makes a wintun device of its own, before installing tun.unsafe_routes from cfg
  23. func newRouteTest(t *testing.T, cfg string) *routeTest {
  24. t.Helper()
  25. if !windows.GetCurrentProcessToken().IsElevated() {
  26. t.Skip("creating a wintun device needs an elevated process")
  27. }
  28. if err := checkWinTunExists(); err != nil {
  29. t.Skipf("wintun.dll is not where nebula looks for it: %v", err)
  30. }
  31. rt := &routeTest{log: &bytes.Buffer{}}
  32. l := test.NewLoggerWithOutput(rt.log)
  33. rt.c = config.NewC(l)
  34. require.NoError(t, rt.c.LoadString(cfg))
  35. var err error
  36. rt.tun, err = newTun(rt.c, l, []netip.Prefix{netip.MustParsePrefix("10.250.0.1/24")}, false)
  37. require.NoError(t, err)
  38. t.Cleanup(func() { _ = rt.tun.Close() })
  39. rt.luid = winipcfg.LUID(rt.tun.tun.LUID())
  40. return rt
  41. }
  42. func (rt *routeTest) metric(t *testing.T, cidr string) uint32 {
  43. t.Helper()
  44. p := netip.MustParsePrefix(cidr)
  45. row, err := rt.luid.Route(p, unspecifiedNextHop(p))
  46. require.NoError(t, err)
  47. return row.Metric
  48. }
  49. // reload applies cfg and expects nothing in it to fail
  50. func (rt *routeTest) reload(t *testing.T, cfg string) {
  51. t.Helper()
  52. res := rt.c.ReloadConfigString(cfg)
  53. require.NoError(t, res.Err)
  54. assert.False(t, res.Failed, "the reload reported a failure")
  55. }
  56. func unsafeRoutes(routes ...string) string {
  57. var b strings.Builder
  58. b.WriteString("tun:\n dev: nebroutetest\n unsafe_routes:\n")
  59. // cidr[@metric][!], the ! for install: false
  60. for _, r := range routes {
  61. r, noInstall := strings.CutSuffix(r, "!")
  62. cidr, metric, _ := strings.Cut(r, "@")
  63. fmt.Fprintf(&b, " - route: %s\n via: 10.250.0.2\n", cidr)
  64. if metric != "" {
  65. fmt.Fprintf(&b, " metric: %s\n", metric)
  66. }
  67. if noInstall {
  68. b.WriteString(" install: false\n")
  69. }
  70. }
  71. return b.String()
  72. }
  73. // A reload that adds a route re-adds the ones already installed, Windows refusing those as already there isn't a failure
  74. func TestWinTun_ReloadKeepsInstalledRoutes(t *testing.T) {
  75. forFamilies(t, func(t *testing.T, a, b string) {
  76. rt := newRouteTest(t, unsafeRoutes(a))
  77. require.NoError(t, rt.tun.Activate())
  78. rt.reload(t, unsafeRoutes(a, b))
  79. assert.NotContains(t, rt.log.String(), "Failed to add route")
  80. rt.metric(t, a)
  81. rt.metric(t, b)
  82. })
  83. }
  84. // Windows calls a route the same by destination and next hop, so a changed metric is set on the installed route in
  85. // place rather than deleted and added, which would leave a moment with no route
  86. func TestWinTun_ReloadMetricChangeApplies(t *testing.T) {
  87. forFamilies(t, func(t *testing.T, a, b string) {
  88. rt := newRouteTest(t, unsafeRoutes(a+"@100"))
  89. require.NoError(t, rt.tun.Activate())
  90. require.Equal(t, uint32(100), rt.metric(t, a))
  91. rt.reload(t, unsafeRoutes(a+"@200"))
  92. assert.NotContains(t, rt.log.String(), "Failed to add route")
  93. assert.NotContains(t, rt.log.String(), "Removed route")
  94. assert.Equal(t, uint32(200), rt.metric(t, a))
  95. })
  96. }
  97. // A route someone else put on the device with another metric is brought to ours, at startup too
  98. func TestWinTun_ForeignRouteGetsOurMetric(t *testing.T) {
  99. forFamilies(t, func(t *testing.T, a, b string) {
  100. rt := newRouteTest(t, unsafeRoutes(a+"@100"))
  101. p := netip.MustParsePrefix(a)
  102. require.NoError(t, rt.luid.AddRoute(p, unspecifiedNextHop(p), 50))
  103. require.NoError(t, rt.tun.Activate())
  104. assert.Equal(t, uint32(100), rt.metric(t, a))
  105. assert.Contains(t, rt.log.String(), "level=INFO msg=\"Updated route metric\"")
  106. })
  107. }
  108. // A CIDR listed twice is installed once, by its last entry, and reloads leave it there instead of flipping between the
  109. // two metrics
  110. func TestWinTun_DuplicateCidrInstallsTheLastEntry(t *testing.T) {
  111. forFamilies(t, func(t *testing.T, a, b string) {
  112. rt := newRouteTest(t, unsafeRoutes(a+"@100", a+"@200"))
  113. require.NoError(t, rt.tun.Activate())
  114. assert.Equal(t, uint32(200), rt.metric(t, a))
  115. rt.reload(t, unsafeRoutes(a+"@100", a+"@200", b))
  116. assert.Equal(t, uint32(200), rt.metric(t, a))
  117. assert.NotContains(t, rt.log.String(), "Updated route metric")
  118. assert.NotContains(t, rt.log.String(), "Failed to add route")
  119. })
  120. }
  121. // Dropping the duplicate that wasn't installed leaves the installed route alone. Windows deletes by destination and
  122. // next hop, deleting the dropped one would take the live route with it
  123. func TestWinTun_DroppingAnUninstalledDuplicateKeepsTheRoute(t *testing.T) {
  124. forFamilies(t, func(t *testing.T, a, b string) {
  125. rt := newRouteTest(t, unsafeRoutes(a+"@100", a+"@200"))
  126. require.NoError(t, rt.tun.Activate())
  127. rt.reload(t, unsafeRoutes(a+"@200"))
  128. assert.NotContains(t, rt.log.String(), "Removed route")
  129. assert.Equal(t, uint32(200), rt.metric(t, a))
  130. })
  131. }
  132. // A reload ends where a fresh start with the same config would, a CIDR whose last entry says not to install it is
  133. // removed
  134. func TestWinTun_ReloadMatchesAFreshStart(t *testing.T) {
  135. forFamilies(t, func(t *testing.T, a, b string) {
  136. cidr := netip.MustParsePrefix(a)
  137. routes := unsafeRoutes(a+"@100", a+"@100!")
  138. fresh := newRouteTest(t, routes)
  139. require.NoError(t, fresh.tun.Activate())
  140. _, err := fresh.luid.Route(cidr, unspecifiedNextHop(cidr))
  141. require.Error(t, err, "a fresh start installed it")
  142. require.NoError(t, fresh.tun.Close())
  143. rt := newRouteTest(t, unsafeRoutes(a+"@100"))
  144. require.NoError(t, rt.tun.Activate())
  145. rt.reload(t, routes)
  146. _, err = rt.luid.Route(cidr, unspecifiedNextHop(cidr))
  147. assert.Error(t, err, "the reload left it installed")
  148. })
  149. }
  150. // Removing a route that is already gone is what we wanted, not a failure
  151. func TestWinTun_ReloadRouteAlreadyGone(t *testing.T) {
  152. forFamilies(t, func(t *testing.T, a, b string) {
  153. rt := newRouteTest(t, unsafeRoutes(a, b))
  154. require.NoError(t, rt.tun.Activate())
  155. p := netip.MustParsePrefix(b)
  156. require.NoError(t, rt.luid.DeleteRoute(p, unspecifiedNextHop(p)))
  157. rt.reload(t, unsafeRoutes(a))
  158. })
  159. }
  160. // A network category we can't parse would fail the restart, so it fails the reload instead of asking for one
  161. func TestWinTun_ReloadInvalidNetworkCategoryFails(t *testing.T) {
  162. rt := newRouteTest(t, unsafeRoutes())
  163. for range 2 {
  164. res := rt.c.ReloadConfigString(unsafeRoutes() + " network_category: nope\n")
  165. require.NoError(t, res.Err)
  166. assert.True(t, res.Failed, "a bad category that didn't change since the last reload is still bad")
  167. assert.False(t, res.RestartRequired)
  168. }
  169. // Fixing it back to the running category needs nothing
  170. res := rt.c.ReloadConfigString(unsafeRoutes() + " network_category: private\n")
  171. require.NoError(t, res.Err)
  172. assert.False(t, res.Failed)
  173. assert.False(t, res.RestartRequired)
  174. res = rt.c.ReloadConfigString(unsafeRoutes() + " network_category: public\n")
  175. require.NoError(t, res.Err)
  176. assert.Equal(t, []string{"tun.network_category"}, res.RestartKeys)
  177. }
  178. // forFamilies runs f with two unsafe_routes CIDRs of each address family, the tun's own network stays IPv4
  179. func forFamilies(t *testing.T, f func(t *testing.T, a, b string)) {
  180. t.Run("ipv4", func(t *testing.T) { f(t, "10.251.1.0/24", "10.251.2.0/24") })
  181. t.Run("ipv6", func(t *testing.T) { f(t, "fd00:251:1::/64", "fd00:251:2::/64") })
  182. }