2021-10-21 23:24:11 +02:00
|
|
|
//go:build ios && !e2e_testing
|
|
|
|
// +build ios,!e2e_testing
|
2020-06-30 20:48:58 +02:00
|
|
|
|
2021-11-11 23:37:29 +01:00
|
|
|
package overlay
|
2020-06-30 20:48:58 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"os"
|
2021-11-12 18:19:28 +01:00
|
|
|
"runtime"
|
2020-06-30 20:48:58 +02:00
|
|
|
"sync"
|
|
|
|
"syscall"
|
2021-04-22 22:23:40 +02:00
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
2021-11-12 18:19:28 +01:00
|
|
|
"github.com/slackhq/nebula/iputil"
|
2020-06-30 20:48:58 +02:00
|
|
|
)
|
|
|
|
|
2021-11-12 17:47:36 +01:00
|
|
|
type tun struct {
|
2020-06-30 20:48:58 +02:00
|
|
|
io.ReadWriteCloser
|
2021-11-12 18:19:28 +01:00
|
|
|
Cidr *net.IPNet
|
2020-06-30 20:48:58 +02:00
|
|
|
}
|
|
|
|
|
2021-11-12 18:19:28 +01:00
|
|
|
func newTun(_ *logrus.Logger, _ string, _ *net.IPNet, _ int, _ []Route, _ int, _ bool) (*tun, error) {
|
2020-06-30 20:48:58 +02:00
|
|
|
return nil, fmt.Errorf("newTun not supported in iOS")
|
|
|
|
}
|
|
|
|
|
2021-11-12 18:19:28 +01:00
|
|
|
func newTunFromFd(_ *logrus.Logger, deviceFd int, cidr *net.IPNet, _ int, routes []Route, _ int) (*tun, error) {
|
2020-06-30 20:48:58 +02:00
|
|
|
if len(routes) > 0 {
|
2021-11-12 18:19:28 +01:00
|
|
|
return nil, fmt.Errorf("routes are not supported in %s", runtime.GOOS)
|
2020-06-30 20:48:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
file := os.NewFile(uintptr(deviceFd), "/dev/tun")
|
2021-11-12 17:47:36 +01:00
|
|
|
return &tun{
|
2020-06-30 20:48:58 +02:00
|
|
|
Cidr: cidr,
|
2020-07-28 14:53:16 +02:00
|
|
|
Device: "iOS",
|
2020-06-30 20:48:58 +02:00
|
|
|
ReadWriteCloser: &tunReadCloser{f: file},
|
2021-11-12 17:47:36 +01:00
|
|
|
}, nil
|
2020-06-30 20:48:58 +02:00
|
|
|
}
|
|
|
|
|
2021-11-12 17:47:36 +01:00
|
|
|
func (t *tun) Activate() error {
|
2020-06-30 20:48:58 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-12 18:19:28 +01:00
|
|
|
func (t *tun) RouteFor(iputil.VpnIp) iputil.VpnIp {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2021-11-12 17:47:36 +01:00
|
|
|
func (t *tun) WriteRaw(b []byte) error {
|
|
|
|
_, err := t.Write(b)
|
2020-06-30 20:48:58 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// The following is hoisted up from water, we do this so we can inject our own fd on iOS
|
|
|
|
type tunReadCloser struct {
|
|
|
|
f io.ReadWriteCloser
|
|
|
|
|
|
|
|
rMu sync.Mutex
|
|
|
|
rBuf []byte
|
|
|
|
|
|
|
|
wMu sync.Mutex
|
|
|
|
wBuf []byte
|
|
|
|
}
|
|
|
|
|
2021-11-12 17:47:36 +01:00
|
|
|
func (tr *tunReadCloser) Read(to []byte) (int, error) {
|
|
|
|
tr.rMu.Lock()
|
|
|
|
defer tr.rMu.Unlock()
|
2020-06-30 20:48:58 +02:00
|
|
|
|
2021-11-12 17:47:36 +01:00
|
|
|
if cap(tr.rBuf) < len(to)+4 {
|
|
|
|
tr.rBuf = make([]byte, len(to)+4)
|
2020-06-30 20:48:58 +02:00
|
|
|
}
|
2021-11-12 17:47:36 +01:00
|
|
|
tr.rBuf = tr.rBuf[:len(to)+4]
|
2020-06-30 20:48:58 +02:00
|
|
|
|
2021-11-12 17:47:36 +01:00
|
|
|
n, err := tr.f.Read(tr.rBuf)
|
|
|
|
copy(to, tr.rBuf[4:])
|
2020-06-30 20:48:58 +02:00
|
|
|
return n - 4, err
|
|
|
|
}
|
|
|
|
|
2021-11-12 17:47:36 +01:00
|
|
|
func (tr *tunReadCloser) Write(from []byte) (int, error) {
|
2020-06-30 20:48:58 +02:00
|
|
|
if len(from) == 0 {
|
|
|
|
return 0, syscall.EIO
|
|
|
|
}
|
|
|
|
|
2021-11-12 17:47:36 +01:00
|
|
|
tr.wMu.Lock()
|
|
|
|
defer tr.wMu.Unlock()
|
2020-06-30 20:48:58 +02:00
|
|
|
|
2021-11-12 17:47:36 +01:00
|
|
|
if cap(tr.wBuf) < len(from)+4 {
|
|
|
|
tr.wBuf = make([]byte, len(from)+4)
|
2020-06-30 20:48:58 +02:00
|
|
|
}
|
2021-11-12 17:47:36 +01:00
|
|
|
tr.wBuf = tr.wBuf[:len(from)+4]
|
2020-06-30 20:48:58 +02:00
|
|
|
|
|
|
|
// Determine the IP Family for the NULL L2 Header
|
|
|
|
ipVer := from[0] >> 4
|
|
|
|
if ipVer == 4 {
|
2021-11-12 17:47:36 +01:00
|
|
|
tr.wBuf[3] = syscall.AF_INET
|
2020-06-30 20:48:58 +02:00
|
|
|
} else if ipVer == 6 {
|
2021-11-12 17:47:36 +01:00
|
|
|
tr.wBuf[3] = syscall.AF_INET6
|
2020-06-30 20:48:58 +02:00
|
|
|
} else {
|
|
|
|
return 0, errors.New("unable to determine IP version from packet")
|
|
|
|
}
|
|
|
|
|
2021-11-12 17:47:36 +01:00
|
|
|
copy(tr.wBuf[4:], from)
|
2020-06-30 20:48:58 +02:00
|
|
|
|
2021-11-12 17:47:36 +01:00
|
|
|
n, err := tr.f.Write(tr.wBuf)
|
2020-06-30 20:48:58 +02:00
|
|
|
return n - 4, err
|
|
|
|
}
|
|
|
|
|
2021-11-12 17:47:36 +01:00
|
|
|
func (tr *tunReadCloser) Close() error {
|
|
|
|
return tr.f.Close()
|
2020-06-30 20:48:58 +02:00
|
|
|
}
|
2020-07-28 14:53:16 +02:00
|
|
|
|
2021-11-12 17:47:36 +01:00
|
|
|
func (t *tun) CidrNet() *net.IPNet {
|
|
|
|
return t.Cidr
|
2020-07-28 14:53:16 +02:00
|
|
|
}
|
|
|
|
|
2021-11-12 17:47:36 +01:00
|
|
|
func (t *tun) DeviceName() string {
|
2021-11-12 18:19:28 +01:00
|
|
|
return "iOS"
|
2020-07-28 14:53:16 +02:00
|
|
|
}
|
2021-02-25 21:01:14 +01:00
|
|
|
|
2021-11-12 17:47:36 +01:00
|
|
|
func (t *tun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
|
2021-02-25 21:01:14 +01:00
|
|
|
return nil, fmt.Errorf("TODO: multiqueue not implemented for ios")
|
|
|
|
}
|