control_tester.go 5.1 KB

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