control_tester.go 4.4 KB

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