Cleanup most of the remaining nits (#578)
This commit is contained in:
parent
e07524a654
commit
2f1f0d602f
|
@ -11,7 +11,6 @@ import (
|
||||||
"github.com/flynn/noise"
|
"github.com/flynn/noise"
|
||||||
"github.com/slackhq/nebula/cert"
|
"github.com/slackhq/nebula/cert"
|
||||||
"github.com/slackhq/nebula/iputil"
|
"github.com/slackhq/nebula/iputil"
|
||||||
"github.com/slackhq/nebula/overlay"
|
|
||||||
"github.com/slackhq/nebula/test"
|
"github.com/slackhq/nebula/test"
|
||||||
"github.com/slackhq/nebula/udp"
|
"github.com/slackhq/nebula/udp"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
@ -39,7 +38,7 @@ func Test_NewConnectionManagerTest(t *testing.T) {
|
||||||
lh := NewLightHouse(l, false, &net.IPNet{IP: net.IP{0, 0, 0, 0}, Mask: net.IPMask{0, 0, 0, 0}}, []iputil.VpnIp{}, 1000, 0, &udp.Conn{}, false, 1, false)
|
lh := NewLightHouse(l, false, &net.IPNet{IP: net.IP{0, 0, 0, 0}, Mask: net.IPMask{0, 0, 0, 0}}, []iputil.VpnIp{}, 1000, 0, &udp.Conn{}, false, 1, false)
|
||||||
ifce := &Interface{
|
ifce := &Interface{
|
||||||
hostMap: hostMap,
|
hostMap: hostMap,
|
||||||
inside: &overlay.Tun{},
|
inside: &test.NoopTun{},
|
||||||
outside: &udp.Conn{},
|
outside: &udp.Conn{},
|
||||||
certState: cs,
|
certState: cs,
|
||||||
firewall: &Firewall{},
|
firewall: &Firewall{},
|
||||||
|
@ -108,7 +107,7 @@ func Test_NewConnectionManagerTest2(t *testing.T) {
|
||||||
lh := NewLightHouse(l, false, &net.IPNet{IP: net.IP{0, 0, 0, 0}, Mask: net.IPMask{0, 0, 0, 0}}, []iputil.VpnIp{}, 1000, 0, &udp.Conn{}, false, 1, false)
|
lh := NewLightHouse(l, false, &net.IPNet{IP: net.IP{0, 0, 0, 0}, Mask: net.IPMask{0, 0, 0, 0}}, []iputil.VpnIp{}, 1000, 0, &udp.Conn{}, false, 1, false)
|
||||||
ifce := &Interface{
|
ifce := &Interface{
|
||||||
hostMap: hostMap,
|
hostMap: hostMap,
|
||||||
inside: &overlay.Tun{},
|
inside: &test.NoopTun{},
|
||||||
outside: &udp.Conn{},
|
outside: &udp.Conn{},
|
||||||
certState: cs,
|
certState: cs,
|
||||||
firewall: &Firewall{},
|
firewall: &Firewall{},
|
||||||
|
@ -217,7 +216,7 @@ func Test_NewConnectionManagerTest_DisconnectInvalid(t *testing.T) {
|
||||||
lh := NewLightHouse(l, false, &net.IPNet{IP: net.IP{0, 0, 0, 0}, Mask: net.IPMask{0, 0, 0, 0}}, []iputil.VpnIp{}, 1000, 0, &udp.Conn{}, false, 1, false)
|
lh := NewLightHouse(l, false, &net.IPNet{IP: net.IP{0, 0, 0, 0}, Mask: net.IPMask{0, 0, 0, 0}}, []iputil.VpnIp{}, 1000, 0, &udp.Conn{}, false, 1, false)
|
||||||
ifce := &Interface{
|
ifce := &Interface{
|
||||||
hostMap: hostMap,
|
hostMap: hostMap,
|
||||||
inside: &overlay.Tun{},
|
inside: &test.NoopTun{},
|
||||||
outside: &udp.Conn{},
|
outside: &udp.Conn{},
|
||||||
certState: cs,
|
certState: cs,
|
||||||
firewall: &Firewall{},
|
firewall: &Firewall{},
|
||||||
|
|
|
@ -65,7 +65,7 @@ func (c *Control) InjectLightHouseAddr(vpnIp net.IP, toAddr *net.UDPAddr) {
|
||||||
|
|
||||||
// GetFromTun will pull a packet off the tun side of nebula
|
// GetFromTun will pull a packet off the tun side of nebula
|
||||||
func (c *Control) GetFromTun(block bool) []byte {
|
func (c *Control) GetFromTun(block bool) []byte {
|
||||||
return c.f.inside.(*overlay.Tun).Get(block)
|
return c.f.inside.(*overlay.TestTun).Get(block)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetFromUDP will pull a udp packet off the udp side of nebula
|
// GetFromUDP will pull a udp packet off the udp side of nebula
|
||||||
|
@ -78,7 +78,7 @@ func (c *Control) GetUDPTxChan() <-chan *udp.Packet {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Control) GetTunTxChan() <-chan []byte {
|
func (c *Control) GetTunTxChan() <-chan []byte {
|
||||||
return c.f.inside.(*overlay.Tun).TxPackets
|
return c.f.inside.(*overlay.TestTun).TxPackets
|
||||||
}
|
}
|
||||||
|
|
||||||
// InjectUDPPacket will inject a packet into the udp side of nebula
|
// InjectUDPPacket will inject a packet into the udp side of nebula
|
||||||
|
@ -115,7 +115,7 @@ func (c *Control) InjectTunUDPPacket(toIp net.IP, toPort uint16, fromPort uint16
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.f.inside.(*overlay.Tun).Send(buffer.Bytes())
|
c.f.inside.(*overlay.TestTun).Send(buffer.Bytes())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Control) GetUDPAddr() string {
|
func (c *Control) GetUDPAddr() string {
|
||||||
|
|
|
@ -13,7 +13,7 @@ import (
|
||||||
"golang.org/x/sys/unix"
|
"golang.org/x/sys/unix"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Tun struct {
|
type tun struct {
|
||||||
io.ReadWriteCloser
|
io.ReadWriteCloser
|
||||||
fd int
|
fd int
|
||||||
Device string
|
Device string
|
||||||
|
@ -26,10 +26,10 @@ type Tun struct {
|
||||||
l *logrus.Logger
|
l *logrus.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
func newTunFromFd(l *logrus.Logger, deviceFd int, cidr *net.IPNet, defaultMTU int, routes []Route, unsafeRoutes []Route, txQueueLen int) (ifce *Tun, err error) {
|
func newTunFromFd(l *logrus.Logger, deviceFd int, cidr *net.IPNet, defaultMTU int, routes []Route, unsafeRoutes []Route, txQueueLen int) (*tun, error) {
|
||||||
file := os.NewFile(uintptr(deviceFd), "/dev/net/tun")
|
file := os.NewFile(uintptr(deviceFd), "/dev/net/tun")
|
||||||
|
|
||||||
ifce = &Tun{
|
return &tun{
|
||||||
ReadWriteCloser: file,
|
ReadWriteCloser: file,
|
||||||
fd: int(file.Fd()),
|
fd: int(file.Fd()),
|
||||||
Device: "android",
|
Device: "android",
|
||||||
|
@ -39,19 +39,18 @@ func newTunFromFd(l *logrus.Logger, deviceFd int, cidr *net.IPNet, defaultMTU in
|
||||||
Routes: routes,
|
Routes: routes,
|
||||||
UnsafeRoutes: unsafeRoutes,
|
UnsafeRoutes: unsafeRoutes,
|
||||||
l: l,
|
l: l,
|
||||||
}
|
}, nil
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func newTun(l *logrus.Logger, deviceName string, cidr *net.IPNet, defaultMTU int, routes []Route, unsafeRoutes []Route, txQueueLen int, multiqueue bool) (ifce *Tun, err error) {
|
func newTun(_ *logrus.Logger, _ string, _ *net.IPNet, _ int, _ []Route, _ []Route, _ int, _ bool) (*tun, error) {
|
||||||
return nil, fmt.Errorf("newTun not supported in Android")
|
return nil, fmt.Errorf("newTun not supported in Android")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Tun) WriteRaw(b []byte) error {
|
func (t *tun) WriteRaw(b []byte) error {
|
||||||
var nn int
|
var nn int
|
||||||
for {
|
for {
|
||||||
max := len(b)
|
max := len(b)
|
||||||
n, err := unix.Write(c.fd, b[nn:max])
|
n, err := unix.Write(t.fd, b[nn:max])
|
||||||
if n > 0 {
|
if n > 0 {
|
||||||
nn += n
|
nn += n
|
||||||
}
|
}
|
||||||
|
@ -69,18 +68,18 @@ func (c *Tun) WriteRaw(b []byte) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Tun) Activate() error {
|
func (t tun) Activate() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Tun) CidrNet() *net.IPNet {
|
func (t *tun) CidrNet() *net.IPNet {
|
||||||
return c.Cidr
|
return t.Cidr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Tun) DeviceName() string {
|
func (t *tun) DeviceName() string {
|
||||||
return c.Device
|
return t.Device
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Tun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
|
func (t *tun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
|
||||||
return nil, fmt.Errorf("TODO: multiqueue not implemented for android")
|
return nil, fmt.Errorf("TODO: multiqueue not implemented for android")
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,7 +16,7 @@ import (
|
||||||
"golang.org/x/sys/unix"
|
"golang.org/x/sys/unix"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Tun struct {
|
type tun struct {
|
||||||
io.ReadWriteCloser
|
io.ReadWriteCloser
|
||||||
Device string
|
Device string
|
||||||
Cidr *net.IPNet
|
Cidr *net.IPNet
|
||||||
|
@ -74,13 +74,7 @@ type ifreqMTU struct {
|
||||||
pad [8]byte
|
pad [8]byte
|
||||||
}
|
}
|
||||||
|
|
||||||
type ifreqQLEN struct {
|
func newTun(l *logrus.Logger, name string, cidr *net.IPNet, defaultMTU int, routes []Route, unsafeRoutes []Route, txQueueLen int, _ bool) (*tun, error) {
|
||||||
Name [16]byte
|
|
||||||
Value int32
|
|
||||||
pad [8]byte
|
|
||||||
}
|
|
||||||
|
|
||||||
func newTun(l *logrus.Logger, name string, cidr *net.IPNet, defaultMTU int, routes []Route, unsafeRoutes []Route, txQueueLen int, multiqueue bool) (ifce *Tun, err error) {
|
|
||||||
if len(routes) > 0 {
|
if len(routes) > 0 {
|
||||||
return nil, fmt.Errorf("route MTU not supported in Darwin")
|
return nil, fmt.Errorf("route MTU not supported in Darwin")
|
||||||
}
|
}
|
||||||
|
@ -106,7 +100,7 @@ func newTun(l *logrus.Logger, name string, cidr *net.IPNet, defaultMTU int, rout
|
||||||
ctlName [96]byte
|
ctlName [96]byte
|
||||||
}{}
|
}{}
|
||||||
|
|
||||||
copy(ctlInfo.ctlName[:], []byte(utunControlName))
|
copy(ctlInfo.ctlName[:], utunControlName)
|
||||||
|
|
||||||
err = ioctl(uintptr(fd), uintptr(_CTLIOCGINFO), uintptr(unsafe.Pointer(ctlInfo)))
|
err = ioctl(uintptr(fd), uintptr(_CTLIOCGINFO), uintptr(unsafe.Pointer(ctlInfo)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -125,7 +119,7 @@ func newTun(l *logrus.Logger, name string, cidr *net.IPNet, defaultMTU int, rout
|
||||||
unix.SYS_CONNECT,
|
unix.SYS_CONNECT,
|
||||||
uintptr(fd),
|
uintptr(fd),
|
||||||
uintptr(unsafe.Pointer(&sc)),
|
uintptr(unsafe.Pointer(&sc)),
|
||||||
uintptr(sockaddrCtlSize),
|
sockaddrCtlSize,
|
||||||
)
|
)
|
||||||
if errno != 0 {
|
if errno != 0 {
|
||||||
return nil, fmt.Errorf("SYS_CONNECT: %v", errno)
|
return nil, fmt.Errorf("SYS_CONNECT: %v", errno)
|
||||||
|
@ -152,7 +146,7 @@ func newTun(l *logrus.Logger, name string, cidr *net.IPNet, defaultMTU int, rout
|
||||||
|
|
||||||
file := os.NewFile(uintptr(fd), "")
|
file := os.NewFile(uintptr(fd), "")
|
||||||
|
|
||||||
tun := &Tun{
|
tun := &tun{
|
||||||
ReadWriteCloser: file,
|
ReadWriteCloser: file,
|
||||||
Device: name,
|
Device: name,
|
||||||
Cidr: cidr,
|
Cidr: cidr,
|
||||||
|
@ -165,25 +159,25 @@ func newTun(l *logrus.Logger, name string, cidr *net.IPNet, defaultMTU int, rout
|
||||||
return tun, nil
|
return tun, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Tun) deviceBytes() (o [16]byte) {
|
func (t *tun) deviceBytes() (o [16]byte) {
|
||||||
for i, c := range t.Device {
|
for i, c := range t.Device {
|
||||||
o[i] = byte(c)
|
o[i] = byte(c)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func newTunFromFd(l *logrus.Logger, deviceFd int, cidr *net.IPNet, defaultMTU int, routes []Route, unsafeRoutes []Route, txQueueLen int) (ifce *Tun, err error) {
|
func newTunFromFd(_ *logrus.Logger, _ int, _ *net.IPNet, _ int, _ []Route, _ []Route, _ int) (*tun, error) {
|
||||||
return nil, fmt.Errorf("newTunFromFd not supported in Darwin")
|
return nil, fmt.Errorf("newTunFromFd not supported in Darwin")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Tun) Close() error {
|
func (t *tun) Close() error {
|
||||||
if c.ReadWriteCloser != nil {
|
if t.ReadWriteCloser != nil {
|
||||||
return c.ReadWriteCloser.Close()
|
return t.ReadWriteCloser.Close()
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Tun) Activate() error {
|
func (t *tun) Activate() error {
|
||||||
devName := t.deviceBytes()
|
devName := t.deviceBytes()
|
||||||
|
|
||||||
var addr, mask [4]byte
|
var addr, mask [4]byte
|
||||||
|
@ -231,7 +225,7 @@ func (t *Tun) Activate() error {
|
||||||
// Set the MTU on the device
|
// Set the MTU on the device
|
||||||
ifm := ifreqMTU{Name: devName, MTU: int32(t.DefaultMTU)}
|
ifm := ifreqMTU{Name: devName, MTU: int32(t.DefaultMTU)}
|
||||||
if err = ioctl(fd, unix.SIOCSIFMTU, uintptr(unsafe.Pointer(&ifm))); err != nil {
|
if err = ioctl(fd, unix.SIOCSIFMTU, uintptr(unsafe.Pointer(&ifm))); err != nil {
|
||||||
return fmt.Errorf("Failed to set tun mtu: %v", err)
|
return fmt.Errorf("failed to set tun mtu: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -353,9 +347,7 @@ func addRoute(sock int, addr, mask *netroute.Inet4Addr, link *netroute.LinkAddr)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ io.ReadWriteCloser = (*Tun)(nil)
|
func (t *tun) Read(to []byte) (int, error) {
|
||||||
|
|
||||||
func (t *Tun) Read(to []byte) (int, error) {
|
|
||||||
|
|
||||||
buf := make([]byte, len(to)+4)
|
buf := make([]byte, len(to)+4)
|
||||||
|
|
||||||
|
@ -366,7 +358,7 @@ func (t *Tun) Read(to []byte) (int, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write is only valid for single threaded use
|
// Write is only valid for single threaded use
|
||||||
func (t *Tun) Write(from []byte) (int, error) {
|
func (t *tun) Write(from []byte) (int, error) {
|
||||||
buf := t.out
|
buf := t.out
|
||||||
if cap(buf) < len(from)+4 {
|
if cap(buf) < len(from)+4 {
|
||||||
buf = make([]byte, len(from)+4)
|
buf = make([]byte, len(from)+4)
|
||||||
|
@ -385,7 +377,7 @@ func (t *Tun) Write(from []byte) (int, error) {
|
||||||
} else if ipVer == 6 {
|
} else if ipVer == 6 {
|
||||||
buf[3] = syscall.AF_INET6
|
buf[3] = syscall.AF_INET6
|
||||||
} else {
|
} else {
|
||||||
return 0, fmt.Errorf("Unable to determine IP version from packet")
|
return 0, fmt.Errorf("unable to determine IP version from packet")
|
||||||
}
|
}
|
||||||
|
|
||||||
copy(buf[4:], from)
|
copy(buf[4:], from)
|
||||||
|
@ -394,19 +386,19 @@ func (t *Tun) Write(from []byte) (int, error) {
|
||||||
return n - 4, err
|
return n - 4, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Tun) CidrNet() *net.IPNet {
|
func (t *tun) CidrNet() *net.IPNet {
|
||||||
return c.Cidr
|
return t.Cidr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Tun) DeviceName() string {
|
func (t *tun) DeviceName() string {
|
||||||
return c.Device
|
return t.Device
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Tun) WriteRaw(b []byte) error {
|
func (t *tun) WriteRaw(b []byte) error {
|
||||||
_, err := c.Write(b)
|
_, err := t.Write(b)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Tun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
|
func (t *tun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
|
||||||
return nil, fmt.Errorf("TODO: multiqueue not implemented for darwin")
|
return nil, fmt.Errorf("TODO: multiqueue not implemented for darwin")
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,7 +18,7 @@ import (
|
||||||
|
|
||||||
var deviceNameRE = regexp.MustCompile(`^tun[0-9]+$`)
|
var deviceNameRE = regexp.MustCompile(`^tun[0-9]+$`)
|
||||||
|
|
||||||
type Tun struct {
|
type tun struct {
|
||||||
Device string
|
Device string
|
||||||
Cidr *net.IPNet
|
Cidr *net.IPNet
|
||||||
MTU int
|
MTU int
|
||||||
|
@ -28,20 +28,20 @@ type Tun struct {
|
||||||
io.ReadWriteCloser
|
io.ReadWriteCloser
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Tun) Close() error {
|
func (t *tun) Close() error {
|
||||||
if c.ReadWriteCloser != nil {
|
if t.ReadWriteCloser != nil {
|
||||||
return c.ReadWriteCloser.Close()
|
return t.ReadWriteCloser.Close()
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func newTunFromFd(l *logrus.Logger, deviceFd int, cidr *net.IPNet, defaultMTU int, routes []Route, unsafeRoutes []Route, txQueueLen int) (ifce *Tun, err error) {
|
func newTunFromFd(_ *logrus.Logger, _ int, _ *net.IPNet, _ int, _ []Route, _ []Route, _ int) (*tun, error) {
|
||||||
return nil, fmt.Errorf("newTunFromFd not supported in FreeBSD")
|
return nil, fmt.Errorf("newTunFromFd not supported in FreeBSD")
|
||||||
}
|
}
|
||||||
|
|
||||||
func newTun(l *logrus.Logger, deviceName string, cidr *net.IPNet, defaultMTU int, routes []Route, unsafeRoutes []Route, txQueueLen int, multiqueue bool) (ifce *Tun, err error) {
|
func newTun(l *logrus.Logger, deviceName string, cidr *net.IPNet, defaultMTU int, routes []Route, unsafeRoutes []Route, _ int, _ bool) (*tun, error) {
|
||||||
if len(routes) > 0 {
|
if len(routes) > 0 {
|
||||||
return nil, fmt.Errorf("Route MTU not supported in FreeBSD")
|
return nil, fmt.Errorf("route MTU not supported in FreeBSD")
|
||||||
}
|
}
|
||||||
if strings.HasPrefix(deviceName, "/dev/") {
|
if strings.HasPrefix(deviceName, "/dev/") {
|
||||||
deviceName = strings.TrimPrefix(deviceName, "/dev/")
|
deviceName = strings.TrimPrefix(deviceName, "/dev/")
|
||||||
|
@ -49,7 +49,7 @@ func newTun(l *logrus.Logger, deviceName string, cidr *net.IPNet, defaultMTU int
|
||||||
if !deviceNameRE.MatchString(deviceName) {
|
if !deviceNameRE.MatchString(deviceName) {
|
||||||
return nil, fmt.Errorf("tun.dev must match `tun[0-9]+`")
|
return nil, fmt.Errorf("tun.dev must match `tun[0-9]+`")
|
||||||
}
|
}
|
||||||
return &Tun{
|
return &tun{
|
||||||
Device: deviceName,
|
Device: deviceName,
|
||||||
Cidr: cidr,
|
Cidr: cidr,
|
||||||
MTU: defaultMTU,
|
MTU: defaultMTU,
|
||||||
|
@ -58,30 +58,30 @@ func newTun(l *logrus.Logger, deviceName string, cidr *net.IPNet, defaultMTU int
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Tun) Activate() error {
|
func (t *tun) Activate() error {
|
||||||
var err error
|
var err error
|
||||||
c.ReadWriteCloser, err = os.OpenFile("/dev/"+c.Device, os.O_RDWR, 0)
|
t.ReadWriteCloser, err = os.OpenFile("/dev/"+t.Device, os.O_RDWR, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Activate failed: %v", err)
|
return fmt.Errorf("activate failed: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO use syscalls instead of exec.Command
|
// TODO use syscalls instead of exec.Command
|
||||||
c.l.Debug("command: ifconfig", c.Device, c.Cidr.String(), c.Cidr.IP.String())
|
t.l.Debug("command: ifconfig", t.Device, t.Cidr.String(), t.Cidr.IP.String())
|
||||||
if err = exec.Command("/sbin/ifconfig", c.Device, c.Cidr.String(), c.Cidr.IP.String()).Run(); err != nil {
|
if err = exec.Command("/sbin/ifconfig", t.Device, t.Cidr.String(), t.Cidr.IP.String()).Run(); err != nil {
|
||||||
return fmt.Errorf("failed to run 'ifconfig': %s", err)
|
return fmt.Errorf("failed to run 'ifconfig': %s", err)
|
||||||
}
|
}
|
||||||
c.l.Debug("command: route", "-n", "add", "-net", c.Cidr.String(), "-interface", c.Device)
|
t.l.Debug("command: route", "-n", "add", "-net", t.Cidr.String(), "-interface", t.Device)
|
||||||
if err = exec.Command("/sbin/route", "-n", "add", "-net", c.Cidr.String(), "-interface", c.Device).Run(); err != nil {
|
if err = exec.Command("/sbin/route", "-n", "add", "-net", t.Cidr.String(), "-interface", t.Device).Run(); err != nil {
|
||||||
return fmt.Errorf("failed to run 'route add': %s", err)
|
return fmt.Errorf("failed to run 'route add': %s", err)
|
||||||
}
|
}
|
||||||
c.l.Debug("command: ifconfig", c.Device, "mtu", strconv.Itoa(c.MTU))
|
t.l.Debug("command: ifconfig", t.Device, "mtu", strconv.Itoa(t.MTU))
|
||||||
if err = exec.Command("/sbin/ifconfig", c.Device, "mtu", strconv.Itoa(c.MTU)).Run(); err != nil {
|
if err = exec.Command("/sbin/ifconfig", t.Device, "mtu", strconv.Itoa(t.MTU)).Run(); err != nil {
|
||||||
return fmt.Errorf("failed to run 'ifconfig': %s", err)
|
return fmt.Errorf("failed to run 'ifconfig': %s", err)
|
||||||
}
|
}
|
||||||
// Unsafe path routes
|
// Unsafe path routes
|
||||||
for _, r := range c.UnsafeRoutes {
|
for _, r := range t.UnsafeRoutes {
|
||||||
c.l.Debug("command: route", "-n", "add", "-net", r.Cidr.String(), "-interface", c.Device)
|
t.l.Debug("command: route", "-n", "add", "-net", r.Cidr.String(), "-interface", t.Device)
|
||||||
if err = exec.Command("/sbin/route", "-n", "add", "-net", r.Cidr.String(), "-interface", c.Device).Run(); err != nil {
|
if err = exec.Command("/sbin/route", "-n", "add", "-net", r.Cidr.String(), "-interface", t.Device).Run(); err != nil {
|
||||||
return fmt.Errorf("failed to run 'route add' for unsafe_route %s: %s", r.Cidr.String(), err)
|
return fmt.Errorf("failed to run 'route add' for unsafe_route %s: %s", r.Cidr.String(), err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -89,19 +89,19 @@ func (c *Tun) Activate() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Tun) CidrNet() *net.IPNet {
|
func (t *tun) CidrNet() *net.IPNet {
|
||||||
return c.Cidr
|
return t.Cidr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Tun) DeviceName() string {
|
func (t *tun) DeviceName() string {
|
||||||
return c.Device
|
return t.Device
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Tun) WriteRaw(b []byte) error {
|
func (t *tun) WriteRaw(b []byte) error {
|
||||||
_, err := c.Write(b)
|
_, err := t.Write(b)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Tun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
|
func (t *tun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
|
||||||
return nil, fmt.Errorf("TODO: multiqueue not implemented for freebsd")
|
return nil, fmt.Errorf("TODO: multiqueue not implemented for freebsd")
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,36 +15,35 @@ import (
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Tun struct {
|
type tun struct {
|
||||||
io.ReadWriteCloser
|
io.ReadWriteCloser
|
||||||
Device string
|
Device string
|
||||||
Cidr *net.IPNet
|
Cidr *net.IPNet
|
||||||
}
|
}
|
||||||
|
|
||||||
func newTun(l *logrus.Logger, deviceName string, cidr *net.IPNet, defaultMTU int, routes []Route, unsafeRoutes []Route, txQueueLen int, multiqueue bool) (ifce *Tun, err error) {
|
func newTun(_ *logrus.Logger, _ string, _ *net.IPNet, _ int, _ []Route, _ []Route, _ int, _ bool) (*tun, error) {
|
||||||
return nil, fmt.Errorf("newTun not supported in iOS")
|
return nil, fmt.Errorf("newTun not supported in iOS")
|
||||||
}
|
}
|
||||||
|
|
||||||
func newTunFromFd(l *logrus.Logger, deviceFd int, cidr *net.IPNet, defaultMTU int, routes []Route, unsafeRoutes []Route, txQueueLen int) (ifce *Tun, err error) {
|
func newTunFromFd(_ *logrus.Logger, deviceFd int, cidr *net.IPNet, _ int, routes []Route, _ []Route, _ int) (*tun, error) {
|
||||||
if len(routes) > 0 {
|
if len(routes) > 0 {
|
||||||
return nil, fmt.Errorf("route MTU not supported in Darwin")
|
return nil, fmt.Errorf("route MTU not supported in Darwin")
|
||||||
}
|
}
|
||||||
|
|
||||||
file := os.NewFile(uintptr(deviceFd), "/dev/tun")
|
file := os.NewFile(uintptr(deviceFd), "/dev/tun")
|
||||||
ifce = &Tun{
|
return &tun{
|
||||||
Cidr: cidr,
|
Cidr: cidr,
|
||||||
Device: "iOS",
|
Device: "iOS",
|
||||||
ReadWriteCloser: &tunReadCloser{f: file},
|
ReadWriteCloser: &tunReadCloser{f: file},
|
||||||
}
|
}, nil
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Tun) Activate() error {
|
func (t *tun) Activate() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Tun) WriteRaw(b []byte) error {
|
func (t *tun) WriteRaw(b []byte) error {
|
||||||
_, err := c.Write(b)
|
_, err := t.Write(b)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -59,62 +58,62 @@ type tunReadCloser struct {
|
||||||
wBuf []byte
|
wBuf []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tunReadCloser) Read(to []byte) (int, error) {
|
func (tr *tunReadCloser) Read(to []byte) (int, error) {
|
||||||
t.rMu.Lock()
|
tr.rMu.Lock()
|
||||||
defer t.rMu.Unlock()
|
defer tr.rMu.Unlock()
|
||||||
|
|
||||||
if cap(t.rBuf) < len(to)+4 {
|
if cap(tr.rBuf) < len(to)+4 {
|
||||||
t.rBuf = make([]byte, len(to)+4)
|
tr.rBuf = make([]byte, len(to)+4)
|
||||||
}
|
}
|
||||||
t.rBuf = t.rBuf[:len(to)+4]
|
tr.rBuf = tr.rBuf[:len(to)+4]
|
||||||
|
|
||||||
n, err := t.f.Read(t.rBuf)
|
n, err := tr.f.Read(tr.rBuf)
|
||||||
copy(to, t.rBuf[4:])
|
copy(to, tr.rBuf[4:])
|
||||||
return n - 4, err
|
return n - 4, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tunReadCloser) Write(from []byte) (int, error) {
|
func (tr *tunReadCloser) Write(from []byte) (int, error) {
|
||||||
|
|
||||||
if len(from) == 0 {
|
if len(from) == 0 {
|
||||||
return 0, syscall.EIO
|
return 0, syscall.EIO
|
||||||
}
|
}
|
||||||
|
|
||||||
t.wMu.Lock()
|
tr.wMu.Lock()
|
||||||
defer t.wMu.Unlock()
|
defer tr.wMu.Unlock()
|
||||||
|
|
||||||
if cap(t.wBuf) < len(from)+4 {
|
if cap(tr.wBuf) < len(from)+4 {
|
||||||
t.wBuf = make([]byte, len(from)+4)
|
tr.wBuf = make([]byte, len(from)+4)
|
||||||
}
|
}
|
||||||
t.wBuf = t.wBuf[:len(from)+4]
|
tr.wBuf = tr.wBuf[:len(from)+4]
|
||||||
|
|
||||||
// Determine the IP Family for the NULL L2 Header
|
// Determine the IP Family for the NULL L2 Header
|
||||||
ipVer := from[0] >> 4
|
ipVer := from[0] >> 4
|
||||||
if ipVer == 4 {
|
if ipVer == 4 {
|
||||||
t.wBuf[3] = syscall.AF_INET
|
tr.wBuf[3] = syscall.AF_INET
|
||||||
} else if ipVer == 6 {
|
} else if ipVer == 6 {
|
||||||
t.wBuf[3] = syscall.AF_INET6
|
tr.wBuf[3] = syscall.AF_INET6
|
||||||
} else {
|
} else {
|
||||||
return 0, errors.New("unable to determine IP version from packet")
|
return 0, errors.New("unable to determine IP version from packet")
|
||||||
}
|
}
|
||||||
|
|
||||||
copy(t.wBuf[4:], from)
|
copy(tr.wBuf[4:], from)
|
||||||
|
|
||||||
n, err := t.f.Write(t.wBuf)
|
n, err := tr.f.Write(tr.wBuf)
|
||||||
return n - 4, err
|
return n - 4, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tunReadCloser) Close() error {
|
func (tr *tunReadCloser) Close() error {
|
||||||
return t.f.Close()
|
return tr.f.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Tun) CidrNet() *net.IPNet {
|
func (t *tun) CidrNet() *net.IPNet {
|
||||||
return c.Cidr
|
return t.Cidr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Tun) DeviceName() string {
|
func (t *tun) DeviceName() string {
|
||||||
return c.Device
|
return t.Device
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Tun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
|
func (t *tun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
|
||||||
return nil, fmt.Errorf("TODO: multiqueue not implemented for ios")
|
return nil, fmt.Errorf("TODO: multiqueue not implemented for ios")
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,7 +16,7 @@ import (
|
||||||
"golang.org/x/sys/unix"
|
"golang.org/x/sys/unix"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Tun struct {
|
type tun struct {
|
||||||
io.ReadWriteCloser
|
io.ReadWriteCloser
|
||||||
fd int
|
fd int
|
||||||
Device string
|
Device string
|
||||||
|
@ -43,26 +43,6 @@ func ioctl(a1, a2, a3 uintptr) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
func ipv4(addr string) (o [4]byte, err error) {
|
|
||||||
ip := net.ParseIP(addr).To4()
|
|
||||||
if ip == nil {
|
|
||||||
err = fmt.Errorf("failed to parse addr %s", addr)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
for i, b := range ip {
|
|
||||||
o[i] = b
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
const (
|
|
||||||
cIFF_TUN = 0x0001
|
|
||||||
cIFF_NO_PI = 0x1000
|
|
||||||
cIFF_MULTI_QUEUE = 0x0100
|
|
||||||
)
|
|
||||||
|
|
||||||
type ifreqAddr struct {
|
type ifreqAddr struct {
|
||||||
Name [16]byte
|
Name [16]byte
|
||||||
Addr unix.RawSockaddrInet4
|
Addr unix.RawSockaddrInet4
|
||||||
|
@ -81,11 +61,11 @@ type ifreqQLEN struct {
|
||||||
pad [8]byte
|
pad [8]byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func newTunFromFd(l *logrus.Logger, deviceFd int, cidr *net.IPNet, defaultMTU int, routes []Route, unsafeRoutes []Route, txQueueLen int) (ifce *Tun, err error) {
|
func newTunFromFd(l *logrus.Logger, deviceFd int, cidr *net.IPNet, defaultMTU int, routes []Route, unsafeRoutes []Route, txQueueLen int) (*tun, error) {
|
||||||
|
|
||||||
file := os.NewFile(uintptr(deviceFd), "/dev/net/tun")
|
file := os.NewFile(uintptr(deviceFd), "/dev/net/tun")
|
||||||
|
|
||||||
ifce = &Tun{
|
return &tun{
|
||||||
ReadWriteCloser: file,
|
ReadWriteCloser: file,
|
||||||
fd: int(file.Fd()),
|
fd: int(file.Fd()),
|
||||||
Device: "tun0",
|
Device: "tun0",
|
||||||
|
@ -95,20 +75,19 @@ func newTunFromFd(l *logrus.Logger, deviceFd int, cidr *net.IPNet, defaultMTU in
|
||||||
Routes: routes,
|
Routes: routes,
|
||||||
UnsafeRoutes: unsafeRoutes,
|
UnsafeRoutes: unsafeRoutes,
|
||||||
l: l,
|
l: l,
|
||||||
}
|
}, nil
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func newTun(l *logrus.Logger, deviceName string, cidr *net.IPNet, defaultMTU int, routes []Route, unsafeRoutes []Route, txQueueLen int, multiqueue bool) (ifce *Tun, err error) {
|
func newTun(l *logrus.Logger, deviceName string, cidr *net.IPNet, defaultMTU int, routes []Route, unsafeRoutes []Route, txQueueLen int, multiqueue bool) (*tun, error) {
|
||||||
fd, err := unix.Open("/dev/net/tun", os.O_RDWR, 0)
|
fd, err := unix.Open("/dev/net/tun", os.O_RDWR, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var req ifReq
|
var req ifReq
|
||||||
req.Flags = uint16(cIFF_TUN | cIFF_NO_PI)
|
req.Flags = uint16(unix.IFF_TUN | unix.IFF_NO_PI)
|
||||||
if multiqueue {
|
if multiqueue {
|
||||||
req.Flags |= cIFF_MULTI_QUEUE
|
req.Flags |= unix.IFF_MULTI_QUEUE
|
||||||
}
|
}
|
||||||
copy(req.Name[:], deviceName)
|
copy(req.Name[:], deviceName)
|
||||||
if err = ioctl(uintptr(fd), uintptr(unix.TUNSETIFF), uintptr(unsafe.Pointer(&req))); err != nil {
|
if err = ioctl(uintptr(fd), uintptr(unix.TUNSETIFF), uintptr(unsafe.Pointer(&req))); err != nil {
|
||||||
|
@ -125,7 +104,7 @@ func newTun(l *logrus.Logger, deviceName string, cidr *net.IPNet, defaultMTU int
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ifce = &Tun{
|
return &tun{
|
||||||
ReadWriteCloser: file,
|
ReadWriteCloser: file,
|
||||||
fd: int(file.Fd()),
|
fd: int(file.Fd()),
|
||||||
Device: name,
|
Device: name,
|
||||||
|
@ -136,19 +115,18 @@ func newTun(l *logrus.Logger, deviceName string, cidr *net.IPNet, defaultMTU int
|
||||||
Routes: routes,
|
Routes: routes,
|
||||||
UnsafeRoutes: unsafeRoutes,
|
UnsafeRoutes: unsafeRoutes,
|
||||||
l: l,
|
l: l,
|
||||||
}
|
}, nil
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Tun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
|
func (t *tun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
|
||||||
fd, err := unix.Open("/dev/net/tun", os.O_RDWR, 0)
|
fd, err := unix.Open("/dev/net/tun", os.O_RDWR, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var req ifReq
|
var req ifReq
|
||||||
req.Flags = uint16(cIFF_TUN | cIFF_NO_PI | cIFF_MULTI_QUEUE)
|
req.Flags = uint16(unix.IFF_TUN | unix.IFF_NO_PI | unix.IFF_MULTI_QUEUE)
|
||||||
copy(req.Name[:], c.Device)
|
copy(req.Name[:], t.Device)
|
||||||
if err = ioctl(uintptr(fd), uintptr(unix.TUNSETIFF), uintptr(unsafe.Pointer(&req))); err != nil {
|
if err = ioctl(uintptr(fd), uintptr(unix.TUNSETIFF), uintptr(unsafe.Pointer(&req))); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -158,11 +136,11 @@ func (c *Tun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
|
||||||
return file, nil
|
return file, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Tun) WriteRaw(b []byte) error {
|
func (t *tun) WriteRaw(b []byte) error {
|
||||||
var nn int
|
var nn int
|
||||||
for {
|
for {
|
||||||
max := len(b)
|
max := len(b)
|
||||||
n, err := unix.Write(c.fd, b[nn:max])
|
n, err := unix.Write(t.fd, b[nn:max])
|
||||||
if n > 0 {
|
if n > 0 {
|
||||||
nn += n
|
nn += n
|
||||||
}
|
}
|
||||||
|
@ -180,24 +158,24 @@ func (c *Tun) WriteRaw(b []byte) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Tun) Write(b []byte) (int, error) {
|
func (t *tun) Write(b []byte) (int, error) {
|
||||||
return len(b), c.WriteRaw(b)
|
return len(b), t.WriteRaw(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Tun) deviceBytes() (o [16]byte) {
|
func (t tun) deviceBytes() (o [16]byte) {
|
||||||
for i, c := range c.Device {
|
for i, c := range t.Device {
|
||||||
o[i] = byte(c)
|
o[i] = byte(c)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Tun) Activate() error {
|
func (t tun) Activate() error {
|
||||||
devName := c.deviceBytes()
|
devName := t.deviceBytes()
|
||||||
|
|
||||||
var addr, mask [4]byte
|
var addr, mask [4]byte
|
||||||
|
|
||||||
copy(addr[:], c.Cidr.IP.To4())
|
copy(addr[:], t.Cidr.IP.To4())
|
||||||
copy(mask[:], c.Cidr.Mask)
|
copy(mask[:], t.Cidr.Mask)
|
||||||
|
|
||||||
s, err := unix.Socket(
|
s, err := unix.Socket(
|
||||||
unix.AF_INET,
|
unix.AF_INET,
|
||||||
|
@ -235,17 +213,17 @@ func (c Tun) Activate() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the MTU on the device
|
// Set the MTU on the device
|
||||||
ifm := ifreqMTU{Name: devName, MTU: int32(c.MaxMTU)}
|
ifm := ifreqMTU{Name: devName, MTU: int32(t.MaxMTU)}
|
||||||
if err = ioctl(fd, unix.SIOCSIFMTU, uintptr(unsafe.Pointer(&ifm))); err != nil {
|
if err = ioctl(fd, unix.SIOCSIFMTU, uintptr(unsafe.Pointer(&ifm))); err != nil {
|
||||||
// This is currently a non fatal condition because the route table must have the MTU set appropriately as well
|
// This is currently a non fatal condition because the route table must have the MTU set appropriately as well
|
||||||
c.l.WithError(err).Error("Failed to set tun mtu")
|
t.l.WithError(err).Error("Failed to set tun mtu")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the transmit queue length
|
// Set the transmit queue length
|
||||||
ifrq := ifreqQLEN{Name: devName, Value: int32(c.TXQueueLen)}
|
ifrq := ifreqQLEN{Name: devName, Value: int32(t.TXQueueLen)}
|
||||||
if err = ioctl(fd, unix.SIOCSIFTXQLEN, uintptr(unsafe.Pointer(&ifrq))); err != nil {
|
if err = ioctl(fd, unix.SIOCSIFTXQLEN, uintptr(unsafe.Pointer(&ifrq))); err != nil {
|
||||||
// If we can't set the queue length nebula will still work but it may lead to packet loss
|
// If we can't set the queue length nebula will still work but it may lead to packet loss
|
||||||
c.l.WithError(err).Error("Failed to set tun tx queue length")
|
t.l.WithError(err).Error("Failed to set tun tx queue length")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bring up the interface
|
// Bring up the interface
|
||||||
|
@ -255,36 +233,36 @@ func (c Tun) Activate() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the routes
|
// Set the routes
|
||||||
link, err := netlink.LinkByName(c.Device)
|
link, err := netlink.LinkByName(t.Device)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to get tun device link: %s", err)
|
return fmt.Errorf("failed to get tun device link: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Default route
|
// Default route
|
||||||
dr := &net.IPNet{IP: c.Cidr.IP.Mask(c.Cidr.Mask), Mask: c.Cidr.Mask}
|
dr := &net.IPNet{IP: t.Cidr.IP.Mask(t.Cidr.Mask), Mask: t.Cidr.Mask}
|
||||||
nr := netlink.Route{
|
nr := netlink.Route{
|
||||||
LinkIndex: link.Attrs().Index,
|
LinkIndex: link.Attrs().Index,
|
||||||
Dst: dr,
|
Dst: dr,
|
||||||
MTU: c.DefaultMTU,
|
MTU: t.DefaultMTU,
|
||||||
AdvMSS: c.advMSS(Route{}),
|
AdvMSS: t.advMSS(Route{}),
|
||||||
Scope: unix.RT_SCOPE_LINK,
|
Scope: unix.RT_SCOPE_LINK,
|
||||||
Src: c.Cidr.IP,
|
Src: t.Cidr.IP,
|
||||||
Protocol: unix.RTPROT_KERNEL,
|
Protocol: unix.RTPROT_KERNEL,
|
||||||
Table: unix.RT_TABLE_MAIN,
|
Table: unix.RT_TABLE_MAIN,
|
||||||
Type: unix.RTN_UNICAST,
|
Type: unix.RTN_UNICAST,
|
||||||
}
|
}
|
||||||
err = netlink.RouteReplace(&nr)
|
err = netlink.RouteReplace(&nr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to set mtu %v on the default route %v; %v", c.DefaultMTU, dr, err)
|
return fmt.Errorf("failed to set mtu %v on the default route %v; %v", t.DefaultMTU, dr, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Path routes
|
// Path routes
|
||||||
for _, r := range c.Routes {
|
for _, r := range t.Routes {
|
||||||
nr := netlink.Route{
|
nr := netlink.Route{
|
||||||
LinkIndex: link.Attrs().Index,
|
LinkIndex: link.Attrs().Index,
|
||||||
Dst: r.Cidr,
|
Dst: r.Cidr,
|
||||||
MTU: r.MTU,
|
MTU: r.MTU,
|
||||||
AdvMSS: c.advMSS(r),
|
AdvMSS: t.advMSS(r),
|
||||||
Scope: unix.RT_SCOPE_LINK,
|
Scope: unix.RT_SCOPE_LINK,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -295,13 +273,13 @@ func (c Tun) Activate() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unsafe path routes
|
// Unsafe path routes
|
||||||
for _, r := range c.UnsafeRoutes {
|
for _, r := range t.UnsafeRoutes {
|
||||||
nr := netlink.Route{
|
nr := netlink.Route{
|
||||||
LinkIndex: link.Attrs().Index,
|
LinkIndex: link.Attrs().Index,
|
||||||
Dst: r.Cidr,
|
Dst: r.Cidr,
|
||||||
MTU: r.MTU,
|
MTU: r.MTU,
|
||||||
Priority: r.Metric,
|
Priority: r.Metric,
|
||||||
AdvMSS: c.advMSS(r),
|
AdvMSS: t.advMSS(r),
|
||||||
Scope: unix.RT_SCOPE_LINK,
|
Scope: unix.RT_SCOPE_LINK,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -320,22 +298,22 @@ func (c Tun) Activate() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Tun) CidrNet() *net.IPNet {
|
func (t *tun) CidrNet() *net.IPNet {
|
||||||
return c.Cidr
|
return t.Cidr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Tun) DeviceName() string {
|
func (t *tun) DeviceName() string {
|
||||||
return c.Device
|
return t.Device
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Tun) advMSS(r Route) int {
|
func (t tun) advMSS(r Route) int {
|
||||||
mtu := r.MTU
|
mtu := r.MTU
|
||||||
if r.MTU == 0 {
|
if r.MTU == 0 {
|
||||||
mtu = c.DefaultMTU
|
mtu = t.DefaultMTU
|
||||||
}
|
}
|
||||||
|
|
||||||
// We only need to set advmss if the route MTU does not match the device MTU
|
// We only need to set advmss if the route MTU does not match the device MTU
|
||||||
if mtu != c.MaxMTU {
|
if mtu != t.MaxMTU {
|
||||||
return mtu - 40
|
return mtu - 40
|
||||||
}
|
}
|
||||||
return 0
|
return 0
|
||||||
|
|
|
@ -7,19 +7,19 @@ import "testing"
|
||||||
|
|
||||||
var runAdvMSSTests = []struct {
|
var runAdvMSSTests = []struct {
|
||||||
name string
|
name string
|
||||||
tun Tun
|
tun tun
|
||||||
r Route
|
r Route
|
||||||
expected int
|
expected int
|
||||||
}{
|
}{
|
||||||
// Standard case, default MTU is the device max MTU
|
// Standard case, default MTU is the device max MTU
|
||||||
{"default", Tun{DefaultMTU: 1440, MaxMTU: 1440}, Route{}, 0},
|
{"default", tun{DefaultMTU: 1440, MaxMTU: 1440}, Route{}, 0},
|
||||||
{"default-min", Tun{DefaultMTU: 1440, MaxMTU: 1440}, Route{MTU: 1440}, 0},
|
{"default-min", tun{DefaultMTU: 1440, MaxMTU: 1440}, Route{MTU: 1440}, 0},
|
||||||
{"default-low", Tun{DefaultMTU: 1440, MaxMTU: 1440}, Route{MTU: 1200}, 1160},
|
{"default-low", tun{DefaultMTU: 1440, MaxMTU: 1440}, Route{MTU: 1200}, 1160},
|
||||||
|
|
||||||
// Case where we have a route MTU set higher than the default
|
// Case where we have a route MTU set higher than the default
|
||||||
{"route", Tun{DefaultMTU: 1440, MaxMTU: 8941}, Route{}, 1400},
|
{"route", tun{DefaultMTU: 1440, MaxMTU: 8941}, Route{}, 1400},
|
||||||
{"route-min", Tun{DefaultMTU: 1440, MaxMTU: 8941}, Route{MTU: 1440}, 1400},
|
{"route-min", tun{DefaultMTU: 1440, MaxMTU: 8941}, Route{MTU: 1440}, 1400},
|
||||||
{"route-high", Tun{DefaultMTU: 1440, MaxMTU: 8941}, Route{MTU: 8941}, 0},
|
{"route-high", tun{DefaultMTU: 1440, MaxMTU: 8941}, Route{MTU: 8941}, 0},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTunAdvMSS(t *testing.T) {
|
func TestTunAdvMSS(t *testing.T) {
|
||||||
|
|
|
@ -11,7 +11,7 @@ import (
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Tun struct {
|
type TestTun struct {
|
||||||
Device string
|
Device string
|
||||||
Cidr *net.IPNet
|
Cidr *net.IPNet
|
||||||
MTU int
|
MTU int
|
||||||
|
@ -22,8 +22,8 @@ type Tun struct {
|
||||||
TxPackets chan []byte // Packets transmitted outside by nebula
|
TxPackets chan []byte // Packets transmitted outside by nebula
|
||||||
}
|
}
|
||||||
|
|
||||||
func newTun(l *logrus.Logger, deviceName string, cidr *net.IPNet, defaultMTU int, _ []Route, unsafeRoutes []Route, _ int, _ bool) (ifce *Tun, err error) {
|
func newTun(l *logrus.Logger, deviceName string, cidr *net.IPNet, defaultMTU int, _ []Route, unsafeRoutes []Route, _ int, _ bool) (*TestTun, error) {
|
||||||
return &Tun{
|
return &TestTun{
|
||||||
Device: deviceName,
|
Device: deviceName,
|
||||||
Cidr: cidr,
|
Cidr: cidr,
|
||||||
MTU: defaultMTU,
|
MTU: defaultMTU,
|
||||||
|
@ -34,28 +34,28 @@ func newTun(l *logrus.Logger, deviceName string, cidr *net.IPNet, defaultMTU int
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func newTunFromFd(_ *logrus.Logger, _ int, _ *net.IPNet, _ int, _ []Route, _ []Route, _ int) (ifce *Tun, err error) {
|
func newTunFromFd(_ *logrus.Logger, _ int, _ *net.IPNet, _ int, _ []Route, _ []Route, _ int) (*TestTun, error) {
|
||||||
return nil, fmt.Errorf("newTunFromFd not supported")
|
return nil, fmt.Errorf("newTunFromFd not supported")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send will place a byte array onto the receive queue for nebula to consume
|
// 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.
|
// These are unencrypted ip layer frames destined for another nebula node.
|
||||||
// packets should exit the udp side, capture them with udpConn.Get
|
// packets should exit the udp side, capture them with udpConn.Get
|
||||||
func (c *Tun) Send(packet []byte) {
|
func (t *TestTun) Send(packet []byte) {
|
||||||
c.l.WithField("dataLen", len(packet)).Info("Tun receiving injected packet")
|
t.l.WithField("dataLen", len(packet)).Info("Tun receiving injected packet")
|
||||||
c.rxPackets <- packet
|
t.rxPackets <- packet
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get will pull an unencrypted ip layer frame from the transmit queue
|
// 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
|
// 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
|
// packets were ingested from the udp side, you can send them with udpConn.Send
|
||||||
func (c *Tun) Get(block bool) []byte {
|
func (t *TestTun) Get(block bool) []byte {
|
||||||
if block {
|
if block {
|
||||||
return <-c.TxPackets
|
return <-t.TxPackets
|
||||||
}
|
}
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case p := <-c.TxPackets:
|
case p := <-t.TxPackets:
|
||||||
return p
|
return p
|
||||||
default:
|
default:
|
||||||
return nil
|
return nil
|
||||||
|
@ -66,40 +66,40 @@ func (c *Tun) Get(block bool) []byte {
|
||||||
// Below this is boilerplate implementation to make nebula actually work
|
// Below this is boilerplate implementation to make nebula actually work
|
||||||
//********************************************************************************************************************//
|
//********************************************************************************************************************//
|
||||||
|
|
||||||
func (c *Tun) Activate() error {
|
func (t *TestTun) Activate() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Tun) CidrNet() *net.IPNet {
|
func (t *TestTun) CidrNet() *net.IPNet {
|
||||||
return c.Cidr
|
return t.Cidr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Tun) DeviceName() string {
|
func (t *TestTun) DeviceName() string {
|
||||||
return c.Device
|
return t.Device
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Tun) Write(b []byte) (n int, err error) {
|
func (t *TestTun) Write(b []byte) (n int, err error) {
|
||||||
return len(b), c.WriteRaw(b)
|
return len(b), t.WriteRaw(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Tun) Close() error {
|
func (t *TestTun) Close() error {
|
||||||
close(c.rxPackets)
|
close(t.rxPackets)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Tun) WriteRaw(b []byte) error {
|
func (t *TestTun) WriteRaw(b []byte) error {
|
||||||
packet := make([]byte, len(b), len(b))
|
packet := make([]byte, len(b), len(b))
|
||||||
copy(packet, b)
|
copy(packet, b)
|
||||||
c.TxPackets <- packet
|
t.TxPackets <- packet
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Tun) Read(b []byte) (int, error) {
|
func (t *TestTun) Read(b []byte) (int, error) {
|
||||||
p := <-c.rxPackets
|
p := <-t.rxPackets
|
||||||
copy(b, p)
|
copy(b, p)
|
||||||
return len(p), nil
|
return len(p), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Tun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
|
func (t *TestTun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
|
||||||
return nil, fmt.Errorf("TODO: multiqueue not implemented")
|
return nil, fmt.Errorf("TODO: multiqueue not implemented")
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,7 +10,7 @@ import (
|
||||||
"github.com/songgao/water"
|
"github.com/songgao/water"
|
||||||
)
|
)
|
||||||
|
|
||||||
type WindowsWaterTun struct {
|
type waterTun struct {
|
||||||
Device string
|
Device string
|
||||||
Cidr *net.IPNet
|
Cidr *net.IPNet
|
||||||
MTU int
|
MTU int
|
||||||
|
@ -19,37 +19,37 @@ type WindowsWaterTun struct {
|
||||||
*water.Interface
|
*water.Interface
|
||||||
}
|
}
|
||||||
|
|
||||||
func newWindowsWaterTun(deviceName string, cidr *net.IPNet, defaultMTU int, unsafeRoutes []Route, txQueueLen int) (ifce *WindowsWaterTun, err error) {
|
func newWaterTun(cidr *net.IPNet, defaultMTU int, unsafeRoutes []Route) (*waterTun, error) {
|
||||||
// NOTE: You cannot set the deviceName under Windows, so you must check tun.Device after calling .Activate()
|
// NOTE: You cannot set the deviceName under Windows, so you must check tun.Device after calling .Activate()
|
||||||
return &WindowsWaterTun{
|
return &waterTun{
|
||||||
Cidr: cidr,
|
Cidr: cidr,
|
||||||
MTU: defaultMTU,
|
MTU: defaultMTU,
|
||||||
UnsafeRoutes: unsafeRoutes,
|
UnsafeRoutes: unsafeRoutes,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *WindowsWaterTun) Activate() error {
|
func (t *waterTun) Activate() error {
|
||||||
var err error
|
var err error
|
||||||
c.Interface, err = water.New(water.Config{
|
t.Interface, err = water.New(water.Config{
|
||||||
DeviceType: water.TUN,
|
DeviceType: water.TUN,
|
||||||
PlatformSpecificParams: water.PlatformSpecificParams{
|
PlatformSpecificParams: water.PlatformSpecificParams{
|
||||||
ComponentID: "tap0901",
|
ComponentID: "tap0901",
|
||||||
Network: c.Cidr.String(),
|
Network: t.Cidr.String(),
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Activate failed: %v", err)
|
return fmt.Errorf("activate failed: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Device = c.Interface.Name()
|
t.Device = t.Interface.Name()
|
||||||
|
|
||||||
// TODO use syscalls instead of exec.Command
|
// TODO use syscalls instead of exec.Command
|
||||||
err = exec.Command(
|
err = exec.Command(
|
||||||
`C:\Windows\System32\netsh.exe`, "interface", "ipv4", "set", "address",
|
`C:\Windows\System32\netsh.exe`, "interface", "ipv4", "set", "address",
|
||||||
fmt.Sprintf("name=%s", c.Device),
|
fmt.Sprintf("name=%s", t.Device),
|
||||||
"source=static",
|
"source=static",
|
||||||
fmt.Sprintf("addr=%s", c.Cidr.IP),
|
fmt.Sprintf("addr=%s", t.Cidr.IP),
|
||||||
fmt.Sprintf("mask=%s", net.IP(c.Cidr.Mask)),
|
fmt.Sprintf("mask=%s", net.IP(t.Cidr.Mask)),
|
||||||
"gateway=none",
|
"gateway=none",
|
||||||
).Run()
|
).Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -57,19 +57,19 @@ func (c *WindowsWaterTun) Activate() error {
|
||||||
}
|
}
|
||||||
err = exec.Command(
|
err = exec.Command(
|
||||||
`C:\Windows\System32\netsh.exe`, "interface", "ipv4", "set", "interface",
|
`C:\Windows\System32\netsh.exe`, "interface", "ipv4", "set", "interface",
|
||||||
c.Device,
|
t.Device,
|
||||||
fmt.Sprintf("mtu=%d", c.MTU),
|
fmt.Sprintf("mtu=%d", t.MTU),
|
||||||
).Run()
|
).Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to run 'netsh' to set MTU: %s", err)
|
return fmt.Errorf("failed to run 'netsh' to set MTU: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
iface, err := net.InterfaceByName(c.Device)
|
iface, err := net.InterfaceByName(t.Device)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to find interface named %s: %v", c.Device, err)
|
return fmt.Errorf("failed to find interface named %s: %v", t.Device, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, r := range c.UnsafeRoutes {
|
for _, r := range t.UnsafeRoutes {
|
||||||
err = exec.Command(
|
err = exec.Command(
|
||||||
"C:\\Windows\\System32\\route.exe", "add", r.Cidr.String(), r.Via.String(), "IF", strconv.Itoa(iface.Index), "METRIC", strconv.Itoa(r.Metric),
|
"C:\\Windows\\System32\\route.exe", "add", r.Cidr.String(), r.Via.String(), "IF", strconv.Itoa(iface.Index), "METRIC", strconv.Itoa(r.Metric),
|
||||||
).Run()
|
).Run()
|
||||||
|
@ -81,27 +81,27 @@ func (c *WindowsWaterTun) Activate() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *WindowsWaterTun) CidrNet() *net.IPNet {
|
func (t *waterTun) CidrNet() *net.IPNet {
|
||||||
return c.Cidr
|
return t.Cidr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *WindowsWaterTun) DeviceName() string {
|
func (t *waterTun) DeviceName() string {
|
||||||
return c.Device
|
return t.Device
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *WindowsWaterTun) WriteRaw(b []byte) error {
|
func (t *waterTun) WriteRaw(b []byte) error {
|
||||||
_, err := c.Write(b)
|
_, err := t.Write(b)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *WindowsWaterTun) Close() error {
|
func (t *waterTun) Close() error {
|
||||||
if c.Interface == nil {
|
if t.Interface == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.Interface.Close()
|
return t.Interface.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *WindowsWaterTun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
|
func (t *waterTun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
|
||||||
return nil, fmt.Errorf("TODO: multiqueue not implemented for windows")
|
return nil, fmt.Errorf("TODO: multiqueue not implemented for windows")
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,6 @@ package overlay
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
@ -15,41 +14,34 @@ import (
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Tun struct {
|
func newTunFromFd(_ *logrus.Logger, _ int, _ *net.IPNet, _ int, _ []Route, _ []Route, _ int) (Device, error) {
|
||||||
Device
|
|
||||||
}
|
|
||||||
|
|
||||||
func newTunFromFd(l *logrus.Logger, deviceFd int, cidr *net.IPNet, defaultMTU int, routes []Route, unsafeRoutes []Route, txQueueLen int) (ifce *Tun, err error) {
|
|
||||||
return nil, fmt.Errorf("newTunFromFd not supported in Windows")
|
return nil, fmt.Errorf("newTunFromFd not supported in Windows")
|
||||||
}
|
}
|
||||||
|
|
||||||
func newTun(l *logrus.Logger, deviceName string, cidr *net.IPNet, defaultMTU int, routes []Route, unsafeRoutes []Route, txQueueLen int, multiqueue bool) (ifce *Tun, err error) {
|
func newTun(l *logrus.Logger, deviceName string, cidr *net.IPNet, defaultMTU int, routes []Route, unsafeRoutes []Route, _ int, _ bool) (Device, error) {
|
||||||
if len(routes) > 0 {
|
if len(routes) > 0 {
|
||||||
return nil, fmt.Errorf("route MTU not supported in Windows")
|
return nil, fmt.Errorf("route MTU not supported in Windows")
|
||||||
}
|
}
|
||||||
|
|
||||||
useWintun := true
|
useWintun := true
|
||||||
if err = checkWinTunExists(); err != nil {
|
if err := checkWinTunExists(); err != nil {
|
||||||
l.WithError(err).Warn("Check Wintun driver failed, fallback to wintap driver")
|
l.WithError(err).Warn("Check Wintun driver failed, fallback to wintap driver")
|
||||||
useWintun = false
|
useWintun = false
|
||||||
}
|
}
|
||||||
|
|
||||||
var inside Device
|
|
||||||
if useWintun {
|
if useWintun {
|
||||||
inside, err = newWinTun(deviceName, cidr, defaultMTU, unsafeRoutes, txQueueLen)
|
device, err := newWinTun(deviceName, cidr, defaultMTU, unsafeRoutes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("Create Wintun interface failed, %w", err)
|
return nil, fmt.Errorf("create Wintun interface failed, %w", err)
|
||||||
}
|
|
||||||
} else {
|
|
||||||
inside, err = newWindowsWaterTun(deviceName, cidr, defaultMTU, unsafeRoutes, txQueueLen)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("Create wintap driver failed, %w", err)
|
|
||||||
}
|
}
|
||||||
|
return device, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Tun{
|
device, err := newWaterTun(cidr, defaultMTU, unsafeRoutes)
|
||||||
Device: inside,
|
if err != nil {
|
||||||
}, nil
|
return nil, fmt.Errorf("create wintap driver failed, %w", err)
|
||||||
|
}
|
||||||
|
return device, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkWinTunExists() error {
|
func checkWinTunExists() error {
|
||||||
|
@ -68,7 +60,3 @@ func checkWinTunExists() error {
|
||||||
_, err = syscall.LoadDLL(filepath.Join(filepath.Dir(myPath), "dist", "windows", "wintun", "bin", arch, "wintun.dll"))
|
_, err = syscall.LoadDLL(filepath.Join(filepath.Dir(myPath), "dist", "windows", "wintun", "bin", arch, "wintun.dll"))
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Tun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
|
|
||||||
return nil, fmt.Errorf("TODO: multiqueue not implemented for windows")
|
|
||||||
}
|
|
||||||
|
|
|
@ -14,7 +14,7 @@ import (
|
||||||
|
|
||||||
const tunGUIDLabel = "Fixed Nebula Windows GUID v1"
|
const tunGUIDLabel = "Fixed Nebula Windows GUID v1"
|
||||||
|
|
||||||
type WinTun struct {
|
type winTun struct {
|
||||||
Device string
|
Device string
|
||||||
Cidr *net.IPNet
|
Cidr *net.IPNet
|
||||||
MTU int
|
MTU int
|
||||||
|
@ -42,40 +42,38 @@ func generateGUIDByDeviceName(name string) (*windows.GUID, error) {
|
||||||
return (*windows.GUID)(unsafe.Pointer(&sum[0])), nil
|
return (*windows.GUID)(unsafe.Pointer(&sum[0])), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func newWinTun(deviceName string, cidr *net.IPNet, defaultMTU int, unsafeRoutes []Route, txQueueLen int) (ifce *WinTun, err error) {
|
func newWinTun(deviceName string, cidr *net.IPNet, defaultMTU int, unsafeRoutes []Route) (*winTun, error) {
|
||||||
guid, err := generateGUIDByDeviceName(deviceName)
|
guid, err := generateGUIDByDeviceName(deviceName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("Generate GUID failed: %w", err)
|
return nil, fmt.Errorf("generate GUID failed: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
tunDevice, err := wintun.CreateTUNWithRequestedGUID(deviceName, guid, defaultMTU)
|
tunDevice, err := wintun.CreateTUNWithRequestedGUID(deviceName, guid, defaultMTU)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("Create TUN device failed: %w", err)
|
return nil, fmt.Errorf("create TUN device failed: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
ifce = &WinTun{
|
return &winTun{
|
||||||
Device: deviceName,
|
Device: deviceName,
|
||||||
Cidr: cidr,
|
Cidr: cidr,
|
||||||
MTU: defaultMTU,
|
MTU: defaultMTU,
|
||||||
UnsafeRoutes: unsafeRoutes,
|
UnsafeRoutes: unsafeRoutes,
|
||||||
|
|
||||||
tun: tunDevice.(*wintun.NativeTun),
|
tun: tunDevice.(*wintun.NativeTun),
|
||||||
}
|
}, nil
|
||||||
|
|
||||||
return ifce, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *WinTun) Activate() error {
|
func (t *winTun) Activate() error {
|
||||||
luid := winipcfg.LUID(c.tun.LUID())
|
luid := winipcfg.LUID(t.tun.LUID())
|
||||||
|
|
||||||
if err := luid.SetIPAddresses([]net.IPNet{*c.Cidr}); err != nil {
|
if err := luid.SetIPAddresses([]net.IPNet{*t.Cidr}); err != nil {
|
||||||
return fmt.Errorf("failed to set address: %w", err)
|
return fmt.Errorf("failed to set address: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
foundDefault4 := false
|
foundDefault4 := false
|
||||||
routes := make([]*winipcfg.RouteData, 0, len(c.UnsafeRoutes)+1)
|
routes := make([]*winipcfg.RouteData, 0, len(t.UnsafeRoutes)+1)
|
||||||
|
|
||||||
for _, r := range c.UnsafeRoutes {
|
for _, r := range t.UnsafeRoutes {
|
||||||
if !foundDefault4 {
|
if !foundDefault4 {
|
||||||
if cidr, bits := r.Cidr.Mask.Size(); cidr == 0 && bits != 0 {
|
if cidr, bits := r.Cidr.Mask.Size(); cidr == 0 && bits != 0 {
|
||||||
foundDefault4 = true
|
foundDefault4 = true
|
||||||
|
@ -99,7 +97,7 @@ func (c *WinTun) Activate() error {
|
||||||
return fmt.Errorf("failed to get ip interface: %w", err)
|
return fmt.Errorf("failed to get ip interface: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
ipif.NLMTU = uint32(c.MTU)
|
ipif.NLMTU = uint32(t.MTU)
|
||||||
if foundDefault4 {
|
if foundDefault4 {
|
||||||
ipif.UseAutomaticMetric = false
|
ipif.UseAutomaticMetric = false
|
||||||
ipif.Metric = 0
|
ipif.Metric = 0
|
||||||
|
@ -112,35 +110,35 @@ func (c *WinTun) Activate() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *WinTun) CidrNet() *net.IPNet {
|
func (t *winTun) CidrNet() *net.IPNet {
|
||||||
return c.Cidr
|
return t.Cidr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *WinTun) DeviceName() string {
|
func (t *winTun) DeviceName() string {
|
||||||
return c.Device
|
return t.Device
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *WinTun) Read(b []byte) (int, error) {
|
func (t *winTun) Read(b []byte) (int, error) {
|
||||||
return c.tun.Read(b, 0)
|
return t.tun.Read(b, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *WinTun) Write(b []byte) (int, error) {
|
func (t *winTun) Write(b []byte) (int, error) {
|
||||||
return c.tun.Write(b, 0)
|
return t.tun.Write(b, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *WinTun) WriteRaw(b []byte) error {
|
func (t *winTun) WriteRaw(b []byte) error {
|
||||||
_, err := c.Write(b)
|
_, err := t.Write(b)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *WinTun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
|
func (t *winTun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
|
||||||
return nil, fmt.Errorf("TODO: multiqueue not implemented for windows")
|
return nil, fmt.Errorf("TODO: multiqueue not implemented for windows")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *WinTun) Close() error {
|
func (t *winTun) Close() error {
|
||||||
// It seems that the Windows networking stack doesn't like it when we destroy interfaces that have active routes,
|
// It seems that the Windows networking stack doesn't like it when we destroy interfaces that have active routes,
|
||||||
// so to be certain, just remove everything before destroying.
|
// so to be certain, just remove everything before destroying.
|
||||||
luid := winipcfg.LUID(c.tun.LUID())
|
luid := winipcfg.LUID(t.tun.LUID())
|
||||||
_ = luid.FlushRoutes(windows.AF_INET)
|
_ = luid.FlushRoutes(windows.AF_INET)
|
||||||
_ = luid.FlushIPAddresses(windows.AF_INET)
|
_ = luid.FlushIPAddresses(windows.AF_INET)
|
||||||
/* We don't support IPV6 yet
|
/* We don't support IPV6 yet
|
||||||
|
@ -149,5 +147,5 @@ func (c *WinTun) Close() error {
|
||||||
*/
|
*/
|
||||||
_ = luid.FlushDNS(windows.AF_INET)
|
_ = luid.FlushDNS(windows.AF_INET)
|
||||||
|
|
||||||
return c.tun.Close()
|
return t.tun.Close()
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,47 @@
|
||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"io"
|
||||||
|
"net"
|
||||||
|
|
||||||
|
"github.com/slackhq/nebula/iputil"
|
||||||
|
)
|
||||||
|
|
||||||
|
type NoopTun struct{}
|
||||||
|
|
||||||
|
func (NoopTun) RouteFor(iputil.VpnIp) iputil.VpnIp {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (NoopTun) Activate() error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (NoopTun) CidrNet() *net.IPNet {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (NoopTun) DeviceName() string {
|
||||||
|
return "noop"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (NoopTun) Read([]byte) (int, error) {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (NoopTun) Write([]byte) (int, error) {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (NoopTun) WriteRaw([]byte) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (NoopTun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
|
||||||
|
return nil, errors.New("unsupported")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (NoopTun) Close() error {
|
||||||
|
return nil
|
||||||
|
}
|
Loading…
Reference in New Issue