3
0

control_tester.go 5.1 KB

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