2021-10-21 15:24:11 -06:00
|
|
|
//go:build e2e_testing
|
2021-03-29 13:29:20 -06:00
|
|
|
// +build e2e_testing
|
|
|
|
|
|
|
|
package nebula
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
|
2023-02-16 12:23:33 -07:00
|
|
|
"github.com/slackhq/nebula/cert"
|
|
|
|
|
2021-03-29 13:29:20 -06:00
|
|
|
"github.com/google/gopacket"
|
|
|
|
"github.com/google/gopacket/layers"
|
2021-11-03 19:54:04 -06:00
|
|
|
"github.com/slackhq/nebula/header"
|
|
|
|
"github.com/slackhq/nebula/iputil"
|
2021-11-11 15:37:29 -07:00
|
|
|
"github.com/slackhq/nebula/overlay"
|
2021-11-03 19:54:04 -06:00
|
|
|
"github.com/slackhq/nebula/udp"
|
2021-03-29 13:29:20 -06:00
|
|
|
)
|
|
|
|
|
2023-02-16 12:23:33 -07:00
|
|
|
// WaitForType will pipe all messages from this control device into the pipeTo control device
|
2021-03-29 13:29:20 -06:00
|
|
|
// returning after a message matching the criteria has been piped
|
2021-11-03 19:54:04 -06:00
|
|
|
func (c *Control) WaitForType(msgType header.MessageType, subType header.MessageSubType, pipeTo *Control) {
|
|
|
|
h := &header.H{}
|
2021-03-29 13:29:20 -06:00
|
|
|
for {
|
2023-06-14 09:48:52 -06:00
|
|
|
p := c.f.outside.(*udp.TesterConn).Get(true)
|
2021-03-29 13:29:20 -06:00
|
|
|
if err := h.Parse(p.Data); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
pipeTo.InjectUDPPacket(p)
|
|
|
|
if h.Type == msgType && h.Subtype == subType {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WaitForTypeByIndex is similar to WaitForType except it adds an index check
|
|
|
|
// Useful if you have many nodes communicating and want to wait to find a specific nodes packet
|
2021-11-03 19:54:04 -06:00
|
|
|
func (c *Control) WaitForTypeByIndex(toIndex uint32, msgType header.MessageType, subType header.MessageSubType, pipeTo *Control) {
|
|
|
|
h := &header.H{}
|
2021-03-29 13:29:20 -06:00
|
|
|
for {
|
2023-06-14 09:48:52 -06:00
|
|
|
p := c.f.outside.(*udp.TesterConn).Get(true)
|
2021-03-29 13:29:20 -06:00
|
|
|
if err := h.Parse(p.Data); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
pipeTo.InjectUDPPacket(p)
|
|
|
|
if h.RemoteIndex == toIndex && h.Type == msgType && h.Subtype == subType {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// InjectLightHouseAddr will push toAddr into the local lighthouse cache for the vpnIp
|
|
|
|
// This is necessary if you did not configure static hosts or are not running a lighthouse
|
|
|
|
func (c *Control) InjectLightHouseAddr(vpnIp net.IP, toAddr *net.UDPAddr) {
|
2021-04-14 12:50:09 -06:00
|
|
|
c.f.lightHouse.Lock()
|
2021-11-03 19:54:04 -06:00
|
|
|
remoteList := c.f.lightHouse.unlockedGetRemoteList(iputil.Ip2VpnIp(vpnIp))
|
2021-04-14 12:50:09 -06:00
|
|
|
remoteList.Lock()
|
|
|
|
defer remoteList.Unlock()
|
|
|
|
c.f.lightHouse.Unlock()
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
iVpnIp := iputil.Ip2VpnIp(vpnIp)
|
2021-04-14 12:50:09 -06:00
|
|
|
if v4 := toAddr.IP.To4(); v4 != nil {
|
|
|
|
remoteList.unlockedPrependV4(iVpnIp, NewIp4AndPort(v4, uint32(toAddr.Port)))
|
|
|
|
} else {
|
|
|
|
remoteList.unlockedPrependV6(iVpnIp, NewIp6AndPort(toAddr.IP, uint32(toAddr.Port)))
|
|
|
|
}
|
2021-03-29 13:29:20 -06:00
|
|
|
}
|
|
|
|
|
2022-06-27 11:33:29 -06:00
|
|
|
// InjectRelays will push relayVpnIps into the local lighthouse cache for the vpnIp
|
|
|
|
// This is necessary to inform an initiator of possible relays for communicating with a responder
|
|
|
|
func (c *Control) InjectRelays(vpnIp net.IP, relayVpnIps []net.IP) {
|
|
|
|
c.f.lightHouse.Lock()
|
|
|
|
remoteList := c.f.lightHouse.unlockedGetRemoteList(iputil.Ip2VpnIp(vpnIp))
|
|
|
|
remoteList.Lock()
|
|
|
|
defer remoteList.Unlock()
|
|
|
|
c.f.lightHouse.Unlock()
|
|
|
|
|
|
|
|
iVpnIp := iputil.Ip2VpnIp(vpnIp)
|
|
|
|
uVpnIp := []uint32{}
|
|
|
|
for _, rVPnIp := range relayVpnIps {
|
|
|
|
uVpnIp = append(uVpnIp, uint32(iputil.Ip2VpnIp(rVPnIp)))
|
|
|
|
}
|
|
|
|
|
|
|
|
remoteList.unlockedSetRelay(iVpnIp, iVpnIp, uVpnIp)
|
|
|
|
}
|
|
|
|
|
2021-03-29 13:29:20 -06:00
|
|
|
// GetFromTun will pull a packet off the tun side of nebula
|
|
|
|
func (c *Control) GetFromTun(block bool) []byte {
|
2021-11-12 09:47:36 -07:00
|
|
|
return c.f.inside.(*overlay.TestTun).Get(block)
|
2021-03-29 13:29:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetFromUDP will pull a udp packet off the udp side of nebula
|
2021-11-03 19:54:04 -06:00
|
|
|
func (c *Control) GetFromUDP(block bool) *udp.Packet {
|
2023-06-14 09:48:52 -06:00
|
|
|
return c.f.outside.(*udp.TesterConn).Get(block)
|
2021-03-29 13:29:20 -06:00
|
|
|
}
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
func (c *Control) GetUDPTxChan() <-chan *udp.Packet {
|
2023-06-14 09:48:52 -06:00
|
|
|
return c.f.outside.(*udp.TesterConn).TxPackets
|
2021-03-31 09:26:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Control) GetTunTxChan() <-chan []byte {
|
2021-11-12 09:47:36 -07:00
|
|
|
return c.f.inside.(*overlay.TestTun).TxPackets
|
2021-03-31 09:26:35 -06:00
|
|
|
}
|
|
|
|
|
2021-03-29 13:29:20 -06:00
|
|
|
// InjectUDPPacket will inject a packet into the udp side of nebula
|
2021-11-03 19:54:04 -06:00
|
|
|
func (c *Control) InjectUDPPacket(p *udp.Packet) {
|
2023-06-14 09:48:52 -06:00
|
|
|
c.f.outside.(*udp.TesterConn).Send(p)
|
2021-03-29 13:29:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// InjectTunUDPPacket puts a udp packet on the tun interface. Using UDP here because it's a simpler protocol
|
|
|
|
func (c *Control) InjectTunUDPPacket(toIp net.IP, toPort uint16, fromPort uint16, data []byte) {
|
|
|
|
ip := layers.IPv4{
|
|
|
|
Version: 4,
|
|
|
|
TTL: 64,
|
|
|
|
Protocol: layers.IPProtocolUDP,
|
2021-11-12 11:47:09 -07:00
|
|
|
SrcIP: c.f.inside.Cidr().IP,
|
2021-03-29 13:29:20 -06:00
|
|
|
DstIP: toIp,
|
|
|
|
}
|
|
|
|
|
|
|
|
udp := layers.UDP{
|
|
|
|
SrcPort: layers.UDPPort(fromPort),
|
|
|
|
DstPort: layers.UDPPort(toPort),
|
|
|
|
}
|
2021-04-14 12:50:09 -06:00
|
|
|
err := udp.SetNetworkLayerForChecksum(&ip)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-03-29 13:29:20 -06:00
|
|
|
|
|
|
|
buffer := gopacket.NewSerializeBuffer()
|
|
|
|
opt := gopacket.SerializeOptions{
|
|
|
|
ComputeChecksums: true,
|
|
|
|
FixLengths: true,
|
|
|
|
}
|
2021-04-14 12:50:09 -06:00
|
|
|
err = gopacket.SerializeLayers(buffer, opt, &ip, &udp, gopacket.Payload(data))
|
2021-03-29 13:29:20 -06:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-11-12 09:47:36 -07:00
|
|
|
c.f.inside.(*overlay.TestTun).Send(buffer.Bytes())
|
2021-03-29 13:29:20 -06:00
|
|
|
}
|
2021-03-31 09:26:35 -06:00
|
|
|
|
2022-06-27 11:33:29 -06:00
|
|
|
func (c *Control) GetVpnIp() iputil.VpnIp {
|
|
|
|
return c.f.myVpnIp
|
|
|
|
}
|
|
|
|
|
2021-03-31 09:26:35 -06:00
|
|
|
func (c *Control) GetUDPAddr() string {
|
2023-06-14 09:48:52 -06:00
|
|
|
return c.f.outside.(*udp.TesterConn).Addr.String()
|
2021-03-31 09:26:35 -06:00
|
|
|
}
|
2021-04-14 12:50:09 -06:00
|
|
|
|
|
|
|
func (c *Control) KillPendingTunnel(vpnIp net.IP) bool {
|
2023-07-24 11:37:52 -06:00
|
|
|
hostinfo := c.f.handshakeManager.QueryVpnIp(iputil.Ip2VpnIp(vpnIp))
|
|
|
|
if hostinfo == nil {
|
2021-04-14 12:50:09 -06:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-07-24 11:37:52 -06:00
|
|
|
c.f.handshakeManager.DeleteHostInfo(hostinfo)
|
2021-04-14 12:50:09 -06:00
|
|
|
return true
|
|
|
|
}
|
2023-02-16 12:23:33 -07:00
|
|
|
|
|
|
|
func (c *Control) GetHostmap() *HostMap {
|
|
|
|
return c.f.hostMap
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Control) GetCert() *cert.NebulaCertificate {
|
2023-08-14 20:32:40 -06:00
|
|
|
return c.f.pki.GetCertState().Certificate
|
2023-02-16 12:23:33 -07:00
|
|
|
}
|
2023-05-04 14:16:37 -06:00
|
|
|
|
|
|
|
func (c *Control) ReHandshake(vpnIp iputil.VpnIp) {
|
2023-08-21 17:51:45 -06:00
|
|
|
c.f.handshakeManager.StartHandshake(vpnIp, nil)
|
2023-05-04 14:16:37 -06:00
|
|
|
}
|