control_tester.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //go:build e2e_testing
  2. // +build e2e_testing
  3. package nebula
  4. import (
  5. "net/netip"
  6. "github.com/google/gopacket"
  7. "github.com/google/gopacket/layers"
  8. "github.com/slackhq/nebula/header"
  9. "github.com/slackhq/nebula/overlay"
  10. "github.com/slackhq/nebula/udp"
  11. )
  12. // WaitForType will pipe all messages from this control device into the pipeTo control device
  13. // returning after a message matching the criteria has been piped
  14. func (c *Control) WaitForType(msgType header.MessageType, subType header.MessageSubType, pipeTo *Control) {
  15. h := &header.H{}
  16. for {
  17. p := c.f.outside.(*udp.TesterConn).Get(true)
  18. if err := h.Parse(p.Data); err != nil {
  19. panic(err)
  20. }
  21. pipeTo.InjectUDPPacket(p)
  22. if h.Type == msgType && h.Subtype == subType {
  23. return
  24. }
  25. }
  26. }
  27. // WaitForTypeByIndex is similar to WaitForType except it adds an index check
  28. // Useful if you have many nodes communicating and want to wait to find a specific nodes packet
  29. func (c *Control) WaitForTypeByIndex(toIndex uint32, msgType header.MessageType, subType header.MessageSubType, pipeTo *Control) {
  30. h := &header.H{}
  31. for {
  32. p := c.f.outside.(*udp.TesterConn).Get(true)
  33. if err := h.Parse(p.Data); err != nil {
  34. panic(err)
  35. }
  36. pipeTo.InjectUDPPacket(p)
  37. if h.RemoteIndex == toIndex && h.Type == msgType && h.Subtype == subType {
  38. return
  39. }
  40. }
  41. }
  42. // InjectLightHouseAddr will push toAddr into the local lighthouse cache for the vpnIp
  43. // This is necessary if you did not configure static hosts or are not running a lighthouse
  44. func (c *Control) InjectLightHouseAddr(vpnIp netip.Addr, toAddr netip.AddrPort) {
  45. c.f.lightHouse.Lock()
  46. remoteList := c.f.lightHouse.unlockedGetRemoteList([]netip.Addr{vpnIp})
  47. remoteList.Lock()
  48. defer remoteList.Unlock()
  49. c.f.lightHouse.Unlock()
  50. if toAddr.Addr().Is4() {
  51. remoteList.unlockedPrependV4(vpnIp, netAddrToProtoV4AddrPort(toAddr.Addr(), toAddr.Port()))
  52. } else {
  53. remoteList.unlockedPrependV6(vpnIp, netAddrToProtoV6AddrPort(toAddr.Addr(), toAddr.Port()))
  54. }
  55. }
  56. // InjectRelays will push relayVpnIps into the local lighthouse cache for the vpnIp
  57. // This is necessary to inform an initiator of possible relays for communicating with a responder
  58. func (c *Control) InjectRelays(vpnIp netip.Addr, relayVpnIps []netip.Addr) {
  59. c.f.lightHouse.Lock()
  60. remoteList := c.f.lightHouse.unlockedGetRemoteList([]netip.Addr{vpnIp})
  61. remoteList.Lock()
  62. defer remoteList.Unlock()
  63. c.f.lightHouse.Unlock()
  64. remoteList.unlockedSetRelay(vpnIp, relayVpnIps)
  65. }
  66. // GetFromTun will pull a packet off the tun side of nebula
  67. func (c *Control) GetFromTun(block bool) []byte {
  68. return c.f.inside.(*overlay.TestTun).Get(block)
  69. }
  70. // GetFromUDP will pull a udp packet off the udp side of nebula
  71. func (c *Control) GetFromUDP(block bool) *udp.Packet {
  72. return c.f.outside.(*udp.TesterConn).Get(block)
  73. }
  74. func (c *Control) GetUDPTxChan() <-chan *udp.Packet {
  75. return c.f.outside.(*udp.TesterConn).TxPackets
  76. }
  77. func (c *Control) GetTunTxChan() <-chan []byte {
  78. return c.f.inside.(*overlay.TestTun).TxPackets
  79. }
  80. // InjectUDPPacket will inject a packet into the udp side of nebula
  81. func (c *Control) InjectUDPPacket(p *udp.Packet) {
  82. c.f.outside.(*udp.TesterConn).Send(p)
  83. }
  84. // InjectTunUDPPacket puts a udp packet on the tun interface. Using UDP here because it's a simpler protocol
  85. func (c *Control) InjectTunUDPPacket(toAddr netip.Addr, toPort uint16, fromAddr netip.Addr, fromPort uint16, data []byte) {
  86. serialize := make([]gopacket.SerializableLayer, 0)
  87. var netLayer gopacket.NetworkLayer
  88. if toAddr.Is6() {
  89. if !fromAddr.Is6() {
  90. panic("Cant send ipv6 to ipv4")
  91. }
  92. ip := &layers.IPv6{
  93. Version: 6,
  94. NextHeader: layers.IPProtocolUDP,
  95. SrcIP: fromAddr.Unmap().AsSlice(),
  96. DstIP: toAddr.Unmap().AsSlice(),
  97. }
  98. serialize = append(serialize, ip)
  99. netLayer = ip
  100. } else {
  101. if !fromAddr.Is4() {
  102. panic("Cant send ipv4 to ipv6")
  103. }
  104. ip := &layers.IPv4{
  105. Version: 4,
  106. TTL: 64,
  107. Protocol: layers.IPProtocolUDP,
  108. SrcIP: fromAddr.Unmap().AsSlice(),
  109. DstIP: toAddr.Unmap().AsSlice(),
  110. }
  111. serialize = append(serialize, ip)
  112. netLayer = ip
  113. }
  114. udp := layers.UDP{
  115. SrcPort: layers.UDPPort(fromPort),
  116. DstPort: layers.UDPPort(toPort),
  117. }
  118. err := udp.SetNetworkLayerForChecksum(netLayer)
  119. if err != nil {
  120. panic(err)
  121. }
  122. buffer := gopacket.NewSerializeBuffer()
  123. opt := gopacket.SerializeOptions{
  124. ComputeChecksums: true,
  125. FixLengths: true,
  126. }
  127. serialize = append(serialize, &udp, gopacket.Payload(data))
  128. err = gopacket.SerializeLayers(buffer, opt, serialize...)
  129. if err != nil {
  130. panic(err)
  131. }
  132. c.f.inside.(*overlay.TestTun).Send(buffer.Bytes())
  133. }
  134. func (c *Control) GetVpnAddrs() []netip.Addr {
  135. return c.f.myVpnAddrs
  136. }
  137. func (c *Control) GetUDPAddr() netip.AddrPort {
  138. return c.f.outside.(*udp.TesterConn).Addr
  139. }
  140. func (c *Control) KillPendingTunnel(vpnIp netip.Addr) bool {
  141. hostinfo := c.f.handshakeManager.QueryVpnAddr(vpnIp)
  142. if hostinfo == nil {
  143. return false
  144. }
  145. c.f.handshakeManager.DeleteHostInfo(hostinfo)
  146. return true
  147. }
  148. func (c *Control) GetHostmap() *HostMap {
  149. return c.f.hostMap
  150. }
  151. func (c *Control) GetCertState() *CertState {
  152. return c.f.pki.getCertState()
  153. }
  154. func (c *Control) ReHandshake(vpnIp netip.Addr) {
  155. c.f.handshakeManager.StartHandshake(vpnIp, nil)
  156. }