2
0

control_tester.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //go:build e2e_testing
  2. // +build e2e_testing
  3. package nebula
  4. import (
  5. "net/netip"
  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/overlay"
  11. "github.com/slackhq/nebula/udp"
  12. )
  13. // WaitForType 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.(*udp.TesterConn).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.(*udp.TesterConn).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 netip.Addr, toAddr netip.AddrPort) {
  46. c.f.lightHouse.Lock()
  47. remoteList := c.f.lightHouse.unlockedGetRemoteList(vpnIp)
  48. remoteList.Lock()
  49. defer remoteList.Unlock()
  50. c.f.lightHouse.Unlock()
  51. if toAddr.Addr().Is4() {
  52. remoteList.unlockedPrependV4(vpnIp, NewIp4AndPortFromNetIP(toAddr.Addr(), toAddr.Port()))
  53. } else {
  54. remoteList.unlockedPrependV6(vpnIp, NewIp6AndPortFromNetIP(toAddr.Addr(), toAddr.Port()))
  55. }
  56. }
  57. // InjectRelays will push relayVpnIps into the local lighthouse cache for the vpnIp
  58. // This is necessary to inform an initiator of possible relays for communicating with a responder
  59. func (c *Control) InjectRelays(vpnIp netip.Addr, relayVpnIps []netip.Addr) {
  60. c.f.lightHouse.Lock()
  61. remoteList := c.f.lightHouse.unlockedGetRemoteList(vpnIp)
  62. remoteList.Lock()
  63. defer remoteList.Unlock()
  64. c.f.lightHouse.Unlock()
  65. remoteList.unlockedSetRelay(vpnIp, vpnIp, relayVpnIps)
  66. }
  67. // GetFromTun will pull a packet off the tun side of nebula
  68. func (c *Control) GetFromTun(block bool) []byte {
  69. return c.f.inside.(*overlay.TestTun).Get(block)
  70. }
  71. // GetFromUDP will pull a udp packet off the udp side of nebula
  72. func (c *Control) GetFromUDP(block bool) *udp.Packet {
  73. return c.f.outside.(*udp.TesterConn).Get(block)
  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(toIp netip.Addr, toPort uint16, fromPort uint16, data []byte) {
  87. //TODO: IPV6-WORK
  88. ip := layers.IPv4{
  89. Version: 4,
  90. TTL: 64,
  91. Protocol: layers.IPProtocolUDP,
  92. SrcIP: c.f.inside.Cidr().Addr().Unmap().AsSlice(),
  93. DstIP: toIp.Unmap().AsSlice(),
  94. }
  95. udp := layers.UDP{
  96. SrcPort: layers.UDPPort(fromPort),
  97. DstPort: layers.UDPPort(toPort),
  98. }
  99. err := udp.SetNetworkLayerForChecksum(&ip)
  100. if err != nil {
  101. panic(err)
  102. }
  103. buffer := gopacket.NewSerializeBuffer()
  104. opt := gopacket.SerializeOptions{
  105. ComputeChecksums: true,
  106. FixLengths: true,
  107. }
  108. err = gopacket.SerializeLayers(buffer, opt, &ip, &udp, gopacket.Payload(data))
  109. if err != nil {
  110. panic(err)
  111. }
  112. c.f.inside.(*overlay.TestTun).Send(buffer.Bytes())
  113. }
  114. func (c *Control) GetVpnIp() netip.Addr {
  115. return c.f.myVpnNet.Addr()
  116. }
  117. func (c *Control) GetUDPAddr() netip.AddrPort {
  118. return c.f.outside.(*udp.TesterConn).Addr
  119. }
  120. func (c *Control) KillPendingTunnel(vpnIp netip.Addr) bool {
  121. hostinfo := c.f.handshakeManager.QueryVpnIp(vpnIp)
  122. if hostinfo == nil {
  123. return false
  124. }
  125. c.f.handshakeManager.DeleteHostInfo(hostinfo)
  126. return true
  127. }
  128. func (c *Control) GetHostmap() *HostMap {
  129. return c.f.hostMap
  130. }
  131. func (c *Control) GetCert() *cert.NebulaCertificate {
  132. return c.f.pki.GetCertState().Certificate
  133. }
  134. func (c *Control) ReHandshake(vpnIp netip.Addr) {
  135. c.f.handshakeManager.StartHandshake(vpnIp, nil)
  136. }