2021-10-21 15:24:11 -06:00
|
|
|
//go:build e2e_testing
|
2021-03-29 13:29:20 -06:00
|
|
|
// +build e2e_testing
|
|
|
|
|
2021-11-11 15:37:29 -07:00
|
|
|
package overlay
|
2021-03-29 13:29:20 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
2022-09-01 08:44:58 -06:00
|
|
|
"os"
|
2023-07-26 11:52:14 -06:00
|
|
|
"sync/atomic"
|
2021-03-29 13:29:20 -06:00
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
2021-11-12 10:19:28 -07:00
|
|
|
"github.com/slackhq/nebula/cidr"
|
2024-03-28 14:17:28 -06:00
|
|
|
"github.com/slackhq/nebula/config"
|
2021-11-12 10:19:28 -07:00
|
|
|
"github.com/slackhq/nebula/iputil"
|
2021-03-29 13:29:20 -06:00
|
|
|
)
|
|
|
|
|
2021-11-12 09:47:36 -07:00
|
|
|
type TestTun struct {
|
2021-11-12 11:47:09 -07:00
|
|
|
Device string
|
|
|
|
cidr *net.IPNet
|
|
|
|
Routes []Route
|
2023-11-02 16:05:08 -06:00
|
|
|
routeTree *cidr.Tree4[iputil.VpnIp]
|
2021-11-12 11:47:09 -07:00
|
|
|
l *logrus.Logger
|
2021-03-29 13:29:20 -06:00
|
|
|
|
2023-07-26 11:52:14 -06:00
|
|
|
closed atomic.Bool
|
2021-03-29 13:29:20 -06:00
|
|
|
rxPackets chan []byte // Packets to receive into nebula
|
2021-11-11 15:37:29 -07:00
|
|
|
TxPackets chan []byte // Packets transmitted outside by nebula
|
2021-03-29 13:29:20 -06:00
|
|
|
}
|
|
|
|
|
2024-03-28 14:17:28 -06:00
|
|
|
func newTun(c *config.C, l *logrus.Logger, cidr *net.IPNet, _ bool) (*TestTun, error) {
|
|
|
|
_, routes, err := getAllRoutesFromConfig(c, cidr, true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-12-14 09:52:28 -07:00
|
|
|
routeTree, err := makeRouteTree(l, routes, false)
|
2021-11-12 10:19:28 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-11-12 09:47:36 -07:00
|
|
|
return &TestTun{
|
2024-03-28 14:17:28 -06:00
|
|
|
Device: c.GetString("tun.dev", ""),
|
2021-11-12 11:47:09 -07:00
|
|
|
cidr: cidr,
|
2021-11-12 10:19:28 -07:00
|
|
|
Routes: routes,
|
2021-11-12 11:47:09 -07:00
|
|
|
routeTree: routeTree,
|
2021-11-12 10:19:28 -07:00
|
|
|
l: l,
|
2022-06-27 11:33:29 -06:00
|
|
|
rxPackets: make(chan []byte, 10),
|
|
|
|
TxPackets: make(chan []byte, 10),
|
2021-03-29 13:29:20 -06:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-03-28 14:17:28 -06:00
|
|
|
func newTunFromFd(_ *config.C, _ *logrus.Logger, _ int, _ *net.IPNet) (*TestTun, error) {
|
2021-03-29 13:29:20 -06:00
|
|
|
return nil, fmt.Errorf("newTunFromFd not supported")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send will place a byte array onto the receive queue for nebula to consume
|
|
|
|
// These are unencrypted ip layer frames destined for another nebula node.
|
|
|
|
// packets should exit the udp side, capture them with udpConn.Get
|
2021-11-12 09:47:36 -07:00
|
|
|
func (t *TestTun) Send(packet []byte) {
|
2023-07-26 11:52:14 -06:00
|
|
|
if t.closed.Load() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-30 10:09:20 -06:00
|
|
|
if t.l.Level >= logrus.DebugLevel {
|
2023-03-13 11:35:14 -06:00
|
|
|
t.l.WithField("dataLen", len(packet)).Debug("Tun receiving injected packet")
|
2022-09-01 08:44:58 -06:00
|
|
|
}
|
2021-11-12 09:47:36 -07:00
|
|
|
t.rxPackets <- packet
|
2021-03-29 13:29:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get will pull an unencrypted ip layer frame from the transmit queue
|
|
|
|
// nebula meant to send this message to some application on the local system
|
|
|
|
// packets were ingested from the udp side, you can send them with udpConn.Send
|
2021-11-12 09:47:36 -07:00
|
|
|
func (t *TestTun) Get(block bool) []byte {
|
2021-03-29 13:29:20 -06:00
|
|
|
if block {
|
2021-11-12 09:47:36 -07:00
|
|
|
return <-t.TxPackets
|
2021-03-29 13:29:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
2021-11-12 09:47:36 -07:00
|
|
|
case p := <-t.TxPackets:
|
2021-03-29 13:29:20 -06:00
|
|
|
return p
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//********************************************************************************************************************//
|
|
|
|
// Below this is boilerplate implementation to make nebula actually work
|
|
|
|
//********************************************************************************************************************//
|
|
|
|
|
2021-11-12 10:19:28 -07:00
|
|
|
func (t *TestTun) RouteFor(ip iputil.VpnIp) iputil.VpnIp {
|
2023-11-02 16:05:08 -06:00
|
|
|
_, r := t.routeTree.MostSpecificContains(ip)
|
|
|
|
return r
|
2021-11-12 10:19:28 -07:00
|
|
|
}
|
|
|
|
|
2021-11-12 09:47:36 -07:00
|
|
|
func (t *TestTun) Activate() error {
|
2021-03-29 13:29:20 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-12 11:47:09 -07:00
|
|
|
func (t *TestTun) Cidr() *net.IPNet {
|
|
|
|
return t.cidr
|
2021-03-29 13:29:20 -06:00
|
|
|
}
|
|
|
|
|
2021-11-12 11:47:09 -07:00
|
|
|
func (t *TestTun) Name() string {
|
2021-11-12 09:47:36 -07:00
|
|
|
return t.Device
|
2021-03-29 13:29:20 -06:00
|
|
|
}
|
|
|
|
|
2021-11-12 09:47:36 -07:00
|
|
|
func (t *TestTun) Write(b []byte) (n int, err error) {
|
2023-07-26 11:52:14 -06:00
|
|
|
if t.closed.Load() {
|
|
|
|
return 0, io.ErrClosedPipe
|
|
|
|
}
|
|
|
|
|
2021-11-12 11:47:09 -07:00
|
|
|
packet := make([]byte, len(b), len(b))
|
|
|
|
copy(packet, b)
|
|
|
|
t.TxPackets <- packet
|
|
|
|
return len(b), nil
|
2021-03-29 13:29:20 -06:00
|
|
|
}
|
|
|
|
|
2021-11-12 09:47:36 -07:00
|
|
|
func (t *TestTun) Close() error {
|
2023-07-26 11:52:14 -06:00
|
|
|
if t.closed.CompareAndSwap(false, true) {
|
|
|
|
close(t.rxPackets)
|
|
|
|
close(t.TxPackets)
|
|
|
|
}
|
2021-03-29 13:29:20 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-12 09:47:36 -07:00
|
|
|
func (t *TestTun) Read(b []byte) (int, error) {
|
2022-09-01 08:44:58 -06:00
|
|
|
p, ok := <-t.rxPackets
|
|
|
|
if !ok {
|
|
|
|
return 0, os.ErrClosed
|
|
|
|
}
|
2021-03-29 13:29:20 -06:00
|
|
|
copy(b, p)
|
|
|
|
return len(p), nil
|
|
|
|
}
|
|
|
|
|
2021-11-12 09:47:36 -07:00
|
|
|
func (t *TestTun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
|
2021-03-29 13:29:20 -06:00
|
|
|
return nil, fmt.Errorf("TODO: multiqueue not implemented")
|
|
|
|
}
|