| 1 |
//go:build e2e_testing |
| 2 |
|
| 3 |
package nebula |
| 4 |
|
| 5 |
import ( |
| 6 |
"net/netip" |
| 7 |
|
| 8 |
"github.com/slackhq/nebula/header" |
| 9 |
"github.com/slackhq/nebula/overlay" |
| 10 |
"github.com/slackhq/nebula/udp" |
| 11 |
) |
| 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 |
match := h.Type == msgType && h.Subtype == subType |
| 24 |
p.Release() |
| 25 |
if match { |
| 26 |
return |
| 27 |
} |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
// WaitForTypeByIndex is similar to WaitForType except it adds an index check |
| 32 |
// Useful if you have many nodes communicating and want to wait to find a specific nodes packet |
| 33 |
func (c *Control) WaitForTypeByIndex(toIndex uint32, msgType header.MessageType, subType header.MessageSubType, pipeTo *Control) { |
| 34 |
h := &header.H{} |
| 35 |
for { |
| 36 |
p := c.f.outside.(*udp.TesterConn).Get(true) |
| 37 |
if err := h.Parse(p.Data); err != nil { |
| 38 |
panic(err) |
| 39 |
} |
| 40 |
pipeTo.InjectUDPPacket(p) |
| 41 |
match := h.RemoteIndex == toIndex && h.Type == msgType && h.Subtype == subType |
| 42 |
p.Release() |
| 43 |
if match { |
| 44 |
return |
| 45 |
} |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
// InjectLightHouseAddr will push toAddr into the local lighthouse cache for the vpnIp |
| 50 |
// This is necessary if you did not configure static hosts or are not running a lighthouse |
| 51 |
func (c *Control) InjectLightHouseAddr(vpnIp netip.Addr, toAddr netip.AddrPort) { |
| 52 |
c.f.lightHouse.Lock() |
| 53 |
remoteList := c.f.lightHouse.unlockedGetRemoteList([]netip.Addr{vpnIp}) |
| 54 |
remoteList.Lock() |
| 55 |
defer remoteList.Unlock() |
| 56 |
c.f.lightHouse.Unlock() |
| 57 |
|
| 58 |
if toAddr.Addr().Is4() { |
| 59 |
remoteList.unlockedPrependV4(vpnIp, netAddrToProtoV4AddrPort(toAddr.Addr(), toAddr.Port())) |
| 60 |
} else { |
| 61 |
remoteList.unlockedPrependV6(vpnIp, netAddrToProtoV6AddrPort(toAddr.Addr(), toAddr.Port())) |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
// InjectRelays will push relayVpnIps into the local lighthouse cache for the vpnIp |
| 66 |
// This is necessary to inform an initiator of possible relays for communicating with a responder |
| 67 |
func (c *Control) InjectRelays(vpnIp netip.Addr, relayVpnIps []netip.Addr) { |
| 68 |
c.f.lightHouse.Lock() |
| 69 |
remoteList := c.f.lightHouse.unlockedGetRemoteList([]netip.Addr{vpnIp}) |
| 70 |
remoteList.Lock() |
| 71 |
defer remoteList.Unlock() |
| 72 |
c.f.lightHouse.Unlock() |
| 73 |
|
| 74 |
remoteList.unlockedSetRelay(vpnIp, relayVpnIps) |
| 75 |
} |
| 76 |
|
| 77 |
// GetFromTun will pull a packet off the tun side of nebula |
| 78 |
func (c *Control) GetFromTun(block bool) []byte { |
| 79 |
return c.f.inside.(*overlay.TestTun).Get(block) |
| 80 |
} |
| 81 |
|
| 82 |
// GetFromUDP will pull a udp packet off the udp side of nebula |
| 83 |
func (c *Control) GetFromUDP(block bool) *udp.Packet { |
| 84 |
return c.f.outside.(*udp.TesterConn).Get(block) |
| 85 |
} |
| 86 |
|
| 87 |
func (c *Control) GetUDPTxChan() <-chan *udp.Packet { |
| 88 |
return c.f.outside.(*udp.TesterConn).TxPackets |
| 89 |
} |
| 90 |
|
| 91 |
func (c *Control) GetTunTxChan() <-chan []byte { |
| 92 |
return c.f.inside.(*overlay.TestTun).TxPackets |
| 93 |
} |
| 94 |
|
| 95 |
// InjectUDPPacket injects a packet into the udp side. We copy internally so the caller keeps ownership of p. |
| 96 |
// The copy comes from the freelist so steady-state alloc is zero. |
| 97 |
func (c *Control) InjectUDPPacket(p *udp.Packet) { |
| 98 |
c.f.outside.(*udp.TesterConn).Send(p.Copy()) |
| 99 |
} |
| 100 |
|
| 101 |
// InjectTunPacket pushes an IP packet onto the tun interface. |
| 102 |
func (c *Control) InjectTunPacket(packet []byte) { |
| 103 |
c.f.inside.(*overlay.TestTun).Send(packet) |
| 104 |
} |
| 105 |
|
| 106 |
func (c *Control) GetVpnAddrs() []netip.Addr { |
| 107 |
return c.f.myVpnAddrs |
| 108 |
} |
| 109 |
|
| 110 |
func (c *Control) GetUDPAddr() netip.AddrPort { |
| 111 |
return c.f.outside.(*udp.TesterConn).GetAddr() |
| 112 |
} |
| 113 |
|
| 114 |
// SetUDPAddr moves this node to a new underlay address, standing in for a laptop waking up on a different |
| 115 |
// network. Register the new address with the router as well or nothing will route back. |
| 116 |
func (c *Control) SetUDPAddr(addr netip.AddrPort) { |
| 117 |
c.f.outside.(*udp.TesterConn).SetAddr(addr) |
| 118 |
} |
| 119 |
|
| 120 |
// SetLocalAddrsFn replaces underlay address discovery so a test can advertise its simulated address instead of |
| 121 |
// whatever this machine's NICs happen to be. Call it before Start, SendUpdate reads it from the update worker. |
| 122 |
func (c *Control) SetLocalAddrsFn(fn func(*LocalAllowList) []netip.Addr) { |
| 123 |
c.f.lightHouse.localAddrsFn = fn |
| 124 |
} |
| 125 |
|
| 126 |
func (c *Control) KillPendingTunnel(vpnIp netip.Addr) bool { |
| 127 |
hostinfo := c.f.handshakeManager.QueryVpnAddr(vpnIp) |
| 128 |
if hostinfo == nil { |
| 129 |
return false |
| 130 |
} |
| 131 |
|
| 132 |
c.f.handshakeManager.DeleteHostInfo(hostinfo) |
| 133 |
return true |
| 134 |
} |
| 135 |
|
| 136 |
func (c *Control) GetHostmap() *HostMap { |
| 137 |
return c.f.hostMap |
| 138 |
} |
| 139 |
|
| 140 |
// GetHostmapIndexCount returns the number of entries in the main hostmap Indexes table, holding |
| 141 |
// the hostmap read lock so tests can poll it while connection manager churns tunnels. |
| 142 |
func (c *Control) GetHostmapIndexCount() int { |
| 143 |
c.f.hostMap.RLock() |
| 144 |
defer c.f.hostMap.RUnlock() |
| 145 |
return len(c.f.hostMap.Indexes) |
| 146 |
} |
| 147 |
|
| 148 |
func (c *Control) GetF() *Interface { |
| 149 |
return c.f |
| 150 |
} |
| 151 |
|
| 152 |
func (c *Control) GetCertState() *CertState { |
| 153 |
return c.f.pki.getCertState() |
| 154 |
} |
| 155 |
|
| 156 |
func (c *Control) ReHandshake(vpnIp netip.Addr) { |
| 157 |
c.f.handshakeManager.StartHandshake(vpnIp, nil) |
| 158 |
} |