control_tester.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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/udp"
  11. )
  12. // WaitForTypeByIndex 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.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.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 net.IP, toAddr *net.UDPAddr) {
  45. c.f.lightHouse.Lock()
  46. remoteList := c.f.lightHouse.unlockedGetRemoteList(iputil.Ip2VpnIp(vpnIp))
  47. remoteList.Lock()
  48. defer remoteList.Unlock()
  49. c.f.lightHouse.Unlock()
  50. iVpnIp := iputil.Ip2VpnIp(vpnIp)
  51. if v4 := toAddr.IP.To4(); v4 != nil {
  52. remoteList.unlockedPrependV4(iVpnIp, NewIp4AndPort(v4, uint32(toAddr.Port)))
  53. } else {
  54. remoteList.unlockedPrependV6(iVpnIp, NewIp6AndPort(toAddr.IP, uint32(toAddr.Port)))
  55. }
  56. }
  57. // GetFromTun will pull a packet off the tun side of nebula
  58. func (c *Control) GetFromTun(block bool) []byte {
  59. return c.f.inside.(*Tun).Get(block)
  60. }
  61. // GetFromUDP will pull a udp packet off the udp side of nebula
  62. func (c *Control) GetFromUDP(block bool) *udp.Packet {
  63. return c.f.outside.Get(block)
  64. }
  65. func (c *Control) GetUDPTxChan() <-chan *udp.Packet {
  66. return c.f.outside.TxPackets
  67. }
  68. func (c *Control) GetTunTxChan() <-chan []byte {
  69. return c.f.inside.(*Tun).txPackets
  70. }
  71. // InjectUDPPacket will inject a packet into the udp side of nebula
  72. func (c *Control) InjectUDPPacket(p *udp.Packet) {
  73. c.f.outside.Send(p)
  74. }
  75. // InjectTunUDPPacket puts a udp packet on the tun interface. Using UDP here because it's a simpler protocol
  76. func (c *Control) InjectTunUDPPacket(toIp net.IP, toPort uint16, fromPort uint16, data []byte) {
  77. ip := layers.IPv4{
  78. Version: 4,
  79. TTL: 64,
  80. Protocol: layers.IPProtocolUDP,
  81. SrcIP: c.f.inside.CidrNet().IP,
  82. DstIP: toIp,
  83. }
  84. udp := layers.UDP{
  85. SrcPort: layers.UDPPort(fromPort),
  86. DstPort: layers.UDPPort(toPort),
  87. }
  88. err := udp.SetNetworkLayerForChecksum(&ip)
  89. if err != nil {
  90. panic(err)
  91. }
  92. buffer := gopacket.NewSerializeBuffer()
  93. opt := gopacket.SerializeOptions{
  94. ComputeChecksums: true,
  95. FixLengths: true,
  96. }
  97. err = gopacket.SerializeLayers(buffer, opt, &ip, &udp, gopacket.Payload(data))
  98. if err != nil {
  99. panic(err)
  100. }
  101. c.f.inside.(*Tun).Send(buffer.Bytes())
  102. }
  103. func (c *Control) GetUDPAddr() string {
  104. return c.f.outside.Addr.String()
  105. }
  106. func (c *Control) KillPendingTunnel(vpnIp net.IP) bool {
  107. hostinfo, ok := c.f.handshakeManager.pendingHostMap.Hosts[iputil.Ip2VpnIp(vpnIp)]
  108. if !ok {
  109. return false
  110. }
  111. c.f.handshakeManager.pendingHostMap.DeleteHostInfo(hostinfo)
  112. return true
  113. }