userspace_linux_test.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. //go:build linux
  2. package ike
  3. import (
  4. "context"
  5. "errors"
  6. "net"
  7. "os"
  8. "os/exec"
  9. "strconv"
  10. "strings"
  11. "testing"
  12. "time"
  13. )
  14. func TestValidateUserspaceRoutesAllowsOnlyNegotiatedEndpoints(t *testing.T) {
  15. t.Parallel()
  16. config := userspaceRouteTestConfig()
  17. if err := validateUserspaceRoutes(config); err != nil {
  18. t.Fatalf("valid negotiated route: %v", err)
  19. }
  20. config.PCSCF = []net.IP{net.IPv4(203, 0, 113, 10)}
  21. if err := validateUserspaceRoutes(config); err == nil {
  22. t.Fatal("P-CSCF outside responder selector was accepted")
  23. }
  24. }
  25. func TestValidateUserspaceRoutesRequiresMatchingAddressFamily(t *testing.T) {
  26. t.Parallel()
  27. config := userspaceRouteTestConfig()
  28. config.PCSCF = []net.IP{net.ParseIP("2001:db8::20")}
  29. config.ResponderSelectors = []trafficSelector{{
  30. StartPort: 0,
  31. EndPort: 65535,
  32. StartIP: net.ParseIP("2001:db8::"),
  33. EndIP: net.ParseIP("2001:db8::ffff"),
  34. }}
  35. if err := validateUserspaceRoutes(config); err == nil {
  36. t.Fatal("IPv6 P-CSCF without an assigned inner IPv6 address was accepted")
  37. }
  38. }
  39. func TestUserspaceRulePriorityAlwaysPrecedesMainRoute(t *testing.T) {
  40. t.Parallel()
  41. for _, spi := range []uint32{
  42. 0,
  43. 1,
  44. 32767,
  45. 0x01020304,
  46. 0x80000000,
  47. 0xffffffff,
  48. } {
  49. table, priority := userspaceRoutingIdentifiers(spi)
  50. if table <= 255 {
  51. t.Fatalf("SPI %08x produced reserved routing table %d", spi, table)
  52. }
  53. if priority < 10000 || priority >= 30000 || priority >= 32766 {
  54. t.Fatalf(
  55. "SPI %08x produced unsafe rule priority %d",
  56. spi,
  57. priority,
  58. )
  59. }
  60. }
  61. }
  62. func userspaceRouteTestConfig() ChildSAConfig {
  63. return ChildSAConfig{
  64. InnerLocalIPv4: net.IPv4(10, 132, 116, 34),
  65. PCSCF: []net.IP{net.IPv4(10, 127, 192, 82)},
  66. InitiatorSelectors: []trafficSelector{{
  67. StartPort: 0,
  68. EndPort: 65535,
  69. StartIP: net.IPv4(10, 132, 116, 34),
  70. EndIP: net.IPv4(10, 132, 116, 34),
  71. }},
  72. ResponderSelectors: []trafficSelector{{
  73. StartPort: 0,
  74. EndPort: 65535,
  75. StartIP: net.IPv4(10, 0, 0, 0),
  76. EndIP: net.IPv4(10, 255, 255, 255),
  77. }},
  78. }
  79. }
  80. type blockingNATTRelay struct{}
  81. func (blockingNATTRelay) SendESP(ctx context.Context, _ []byte) error {
  82. return ctx.Err()
  83. }
  84. func (blockingNATTRelay) ReceiveESP(ctx context.Context, _ []byte) (int, error) {
  85. <-ctx.Done()
  86. return 0, ctx.Err()
  87. }
  88. func TestLinuxUserspaceInstallerLifecycle(t *testing.T) {
  89. if os.Getenv("VOCAT_NETNS_TEST") != "1" {
  90. t.Skip("set VOCAT_NETNS_TEST=1 inside an isolated Linux network namespace")
  91. }
  92. config := userspaceRouteTestConfig()
  93. config.Name = "vocat-swu-test"
  94. config.InboundSPI = 0x01020304
  95. config.OutboundSPI = 0x05060708
  96. config.Encryption = "aes-cbc-128"
  97. config.Integrity = "hmac-sha1-96"
  98. config.InboundEncKey = make([]byte, 16)
  99. config.InboundAuthKey = make([]byte, 20)
  100. config.OutboundEncKey = make([]byte, 16)
  101. config.OutboundAuthKey = make([]byte, 20)
  102. config.UDPEncapsulation = true
  103. config.Relay = blockingNATTRelay{}
  104. tableID, _ := userspaceRoutingIdentifiers(config.InboundSPI)
  105. table := strconv.FormatUint(uint64(tableID), 10)
  106. if output, err := exec.Command(
  107. "ip", "-4", "route", "add",
  108. "table", table, "unreachable", "default",
  109. ).CombinedOutput(); err != nil {
  110. t.Fatalf("preoccupy route table: %v: %s", err, output)
  111. }
  112. if conflicting, err := (linuxUserspaceInstaller{ipCommand: "ip"}).Install(
  113. context.Background(),
  114. config,
  115. ); err == nil {
  116. _ = conflicting.Close(context.Background())
  117. t.Fatal("installer accepted a preoccupied routing table")
  118. }
  119. if output, err := exec.Command(
  120. "ip", "-4", "route", "delete",
  121. "table", table, "unreachable", "default",
  122. ).CombinedOutput(); err != nil {
  123. t.Fatalf("release preoccupied route table: %v: %s", err, output)
  124. }
  125. handle, err := (linuxUserspaceInstaller{ipCommand: "ip"}).Install(
  126. context.Background(),
  127. config,
  128. )
  129. if err != nil {
  130. t.Fatalf("install user-space CHILD_SA: %v", err)
  131. }
  132. if mode := handle.(DataplaneEvidence).DataplaneMode(); mode != "userspace" {
  133. t.Fatalf("dataplane mode = %q", mode)
  134. }
  135. if output, err := exec.Command("ip", "link", "show", "dev", config.Name).CombinedOutput(); err != nil {
  136. t.Fatalf("TUN interface was not created: %v: %s", err, output)
  137. }
  138. if output, err := exec.Command(
  139. "ip", "-4", "route", "show", "table", table,
  140. ).CombinedOutput(); err != nil ||
  141. !strings.Contains(string(output), "10.127.192.82") ||
  142. !strings.Contains(string(output), "unreachable default") {
  143. t.Fatalf("isolated P-CSCF routes are missing: %v: %s", err, output)
  144. }
  145. if output, err := exec.Command(
  146. "ip", "-4", "route", "get", "10.127.192.82",
  147. "from", "10.132.116.34",
  148. ).CombinedOutput(); err != nil ||
  149. !strings.Contains(string(output), "dev "+config.Name) ||
  150. !strings.Contains(string(output), "table "+table) {
  151. t.Fatalf(
  152. "P-CSCF route did not win before the main table: %v: %s",
  153. err,
  154. output,
  155. )
  156. }
  157. if output, err := exec.Command(
  158. "ip", "-4", "route", "get", "198.51.100.1",
  159. "from", "10.132.116.34",
  160. ).CombinedOutput(); err == nil {
  161. t.Fatalf(
  162. "non-P-CSCF inner traffic escaped the unreachable default: %s",
  163. output,
  164. )
  165. }
  166. expectedFailure := errors.New("test relay stopped")
  167. concrete := handle.(*linuxUserspaceHandle)
  168. concrete.fail(expectedFailure)
  169. select {
  170. case failure := <-concrete.Failures():
  171. if !errors.Is(failure, expectedFailure) {
  172. t.Fatalf("runtime failure = %v", failure)
  173. }
  174. case <-time.After(time.Second):
  175. t.Fatal("terminal failure was not published")
  176. }
  177. closeContext, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  178. defer cancel()
  179. if err := handle.Close(closeContext); err != nil {
  180. t.Fatalf("close user-space CHILD_SA: %v", err)
  181. }
  182. if err := exec.Command("ip", "link", "show", "dev", config.Name).Run(); err == nil {
  183. t.Fatal("TUN interface survived Close")
  184. }
  185. output, err := exec.Command("ip", "-4", "rule", "show").CombinedOutput()
  186. if err != nil {
  187. t.Fatal(err)
  188. }
  189. if strings.Contains(string(output), "from 10.132.116.34") {
  190. t.Fatalf("source rule survived Close: %s", output)
  191. }
  192. }
  193. var _ NATTPacketRelay = blockingNATTRelay{}