control_tester.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. out := c.f.outside.(*udp.TesterConn).Get(block)
  73. return out
  74. }
  75. func (c *Control) GetUDPTxChan() <-chan *udp.Packet {
  76. return c.f.outside.(*udp.TesterConn).TxPackets
  77. }
  78. func (c *Control) GetTunTxChan() <-chan []byte {
  79. return c.f.inside.(*overlay.TestTun).TxPackets
  80. }
  81. // InjectUDPPacket will inject a packet into the udp side of nebula
  82. func (c *Control) InjectUDPPacket(p *udp.Packet) {
  83. c.f.outside.(*udp.TesterConn).Send(p)
  84. }
  85. // InjectTunUDPPacket puts a udp packet on the tun interface. Using UDP here because it's a simpler protocol
  86. func (c *Control) InjectTunUDPPacket(toAddr netip.Addr, toPort uint16, fromAddr netip.Addr, fromPort uint16, data []byte) {
  87. serialize := make([]gopacket.SerializableLayer, 0)
  88. var netLayer gopacket.NetworkLayer
  89. if toAddr.Is6() {
  90. if !fromAddr.Is6() {
  91. panic("Cant send ipv6 to ipv4")
  92. }
  93. ip := &layers.IPv6{
  94. Version: 6,
  95. NextHeader: layers.IPProtocolUDP,
  96. SrcIP: fromAddr.Unmap().AsSlice(),
  97. DstIP: toAddr.Unmap().AsSlice(),
  98. }
  99. serialize = append(serialize, ip)
  100. netLayer = ip
  101. } else {
  102. if !fromAddr.Is4() {
  103. panic("Cant send ipv4 to ipv6")
  104. }
  105. ip := &layers.IPv4{
  106. Version: 4,
  107. TTL: 64,
  108. Protocol: layers.IPProtocolUDP,
  109. SrcIP: fromAddr.Unmap().AsSlice(),
  110. DstIP: toAddr.Unmap().AsSlice(),
  111. }
  112. serialize = append(serialize, ip)
  113. netLayer = ip
  114. }
  115. udp := layers.UDP{
  116. SrcPort: layers.UDPPort(fromPort),
  117. DstPort: layers.UDPPort(toPort),
  118. }
  119. err := udp.SetNetworkLayerForChecksum(netLayer)
  120. if err != nil {
  121. panic(err)
  122. }
  123. buffer := gopacket.NewSerializeBuffer()
  124. opt := gopacket.SerializeOptions{
  125. ComputeChecksums: true,
  126. FixLengths: true,
  127. }
  128. serialize = append(serialize, &udp, gopacket.Payload(data))
  129. err = gopacket.SerializeLayers(buffer, opt, serialize...)
  130. if err != nil {
  131. panic(err)
  132. }
  133. c.f.inside.(*overlay.TestTun).Send(buffer.Bytes())
  134. }
  135. func (c *Control) GetVpnAddrs() []netip.Addr {
  136. return c.f.myVpnAddrs
  137. }
  138. func (c *Control) GetUDPAddr() netip.AddrPort {
  139. return c.f.outside.(*udp.TesterConn).Addr
  140. }
  141. func (c *Control) KillPendingTunnel(vpnIp netip.Addr) bool {
  142. hostinfo := c.f.handshakeManager.QueryVpnAddr(vpnIp)
  143. if hostinfo == nil {
  144. return false
  145. }
  146. c.f.handshakeManager.DeleteHostInfo(hostinfo)
  147. return true
  148. }
  149. func (c *Control) GetHostmap() *HostMap {
  150. return c.f.hostMap
  151. }
  152. func (c *Control) GetF() *Interface {
  153. return c.f
  154. }
  155. func (c *Control) GetCertState() *CertState {
  156. return c.f.pki.getCertState()
  157. }
  158. func (c *Control) ReHandshake(vpnIp netip.Addr) {
  159. c.f.handshakeManager.StartHandshake(vpnIp, nil)
  160. }