2021-10-21 15:24:11 -06:00
|
|
|
//go:build ios && !e2e_testing
|
|
|
|
// +build ios,!e2e_testing
|
2020-06-30 12:48:58 -06:00
|
|
|
|
2021-11-11 15:37:29 -07:00
|
|
|
package overlay
|
2020-06-30 12:48:58 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"sync"
|
2024-03-28 14:17:28 -06:00
|
|
|
"sync/atomic"
|
2020-06-30 12:48:58 -06:00
|
|
|
"syscall"
|
2021-04-22 14:23:40 -06:00
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
2022-08-05 08:58:10 -06: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"
|
2024-03-28 14:17:28 -06:00
|
|
|
"github.com/slackhq/nebula/util"
|
2020-06-30 12:48:58 -06:00
|
|
|
)
|
|
|
|
|
2021-11-12 09:47:36 -07:00
|
|
|
type tun struct {
|
2020-06-30 12:48:58 -06:00
|
|
|
io.ReadWriteCloser
|
2022-08-05 08:58:10 -06:00
|
|
|
cidr *net.IPNet
|
2024-03-28 14:17:28 -06:00
|
|
|
Routes atomic.Pointer[[]Route]
|
|
|
|
routeTree atomic.Pointer[cidr.Tree4[iputil.VpnIp]]
|
|
|
|
l *logrus.Logger
|
2020-06-30 12:48:58 -06:00
|
|
|
}
|
|
|
|
|
2024-03-28 14:17:28 -06:00
|
|
|
func newTun(_ *config.C, _ *logrus.Logger, _ *net.IPNet, _ bool) (*tun, error) {
|
2020-06-30 12:48:58 -06:00
|
|
|
return nil, fmt.Errorf("newTun not supported in iOS")
|
|
|
|
}
|
|
|
|
|
2024-03-28 14:17:28 -06:00
|
|
|
func newTunFromFd(c *config.C, l *logrus.Logger, deviceFd int, cidr *net.IPNet) (*tun, error) {
|
|
|
|
file := os.NewFile(uintptr(deviceFd), "/dev/tun")
|
|
|
|
t := &tun{
|
|
|
|
cidr: cidr,
|
|
|
|
ReadWriteCloser: &tunReadCloser{f: file},
|
|
|
|
l: l,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := t.reload(c, true)
|
2022-08-05 08:58:10 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-06-30 12:48:58 -06:00
|
|
|
}
|
|
|
|
|
2024-03-28 14:17:28 -06:00
|
|
|
c.RegisterReloadCallback(func(c *config.C) {
|
|
|
|
err := t.reload(c, false)
|
|
|
|
if err != nil {
|
|
|
|
util.LogWithContextIfNeeded("failed to reload tun device", err, t.l)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return t, nil
|
2020-06-30 12:48:58 -06:00
|
|
|
}
|
|
|
|
|
2021-11-12 09:47:36 -07:00
|
|
|
func (t *tun) Activate() error {
|
2020-06-30 12:48:58 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-03-28 14:17:28 -06:00
|
|
|
func (t *tun) reload(c *config.C, initial bool) error {
|
|
|
|
change, routes, err := getAllRoutesFromConfig(c, t.cidr, initial)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !initial && !change {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
routeTree, err := makeRouteTree(t.l, routes, false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Teach nebula how to handle the routes
|
|
|
|
t.Routes.Store(&routes)
|
|
|
|
t.routeTree.Store(routeTree)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-05 08:58:10 -06:00
|
|
|
func (t *tun) RouteFor(ip iputil.VpnIp) iputil.VpnIp {
|
2024-03-28 14:17:28 -06:00
|
|
|
_, r := t.routeTree.Load().MostSpecificContains(ip)
|
2023-12-06 14:18:21 -07:00
|
|
|
return r
|
2021-11-12 10:19:28 -07:00
|
|
|
}
|
|
|
|
|
2020-06-30 12:48:58 -06:00
|
|
|
// 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 09:47:36 -07:00
|
|
|
func (tr *tunReadCloser) Read(to []byte) (int, error) {
|
|
|
|
tr.rMu.Lock()
|
|
|
|
defer tr.rMu.Unlock()
|
2020-06-30 12:48:58 -06:00
|
|
|
|
2021-11-12 09:47:36 -07:00
|
|
|
if cap(tr.rBuf) < len(to)+4 {
|
|
|
|
tr.rBuf = make([]byte, len(to)+4)
|
2020-06-30 12:48:58 -06:00
|
|
|
}
|
2021-11-12 09:47:36 -07:00
|
|
|
tr.rBuf = tr.rBuf[:len(to)+4]
|
2020-06-30 12:48:58 -06:00
|
|
|
|
2021-11-12 09:47:36 -07:00
|
|
|
n, err := tr.f.Read(tr.rBuf)
|
|
|
|
copy(to, tr.rBuf[4:])
|
2020-06-30 12:48:58 -06:00
|
|
|
return n - 4, err
|
|
|
|
}
|
|
|
|
|
2021-11-12 09:47:36 -07:00
|
|
|
func (tr *tunReadCloser) Write(from []byte) (int, error) {
|
2020-06-30 12:48:58 -06:00
|
|
|
if len(from) == 0 {
|
|
|
|
return 0, syscall.EIO
|
|
|
|
}
|
|
|
|
|
2021-11-12 09:47:36 -07:00
|
|
|
tr.wMu.Lock()
|
|
|
|
defer tr.wMu.Unlock()
|
2020-06-30 12:48:58 -06:00
|
|
|
|
2021-11-12 09:47:36 -07:00
|
|
|
if cap(tr.wBuf) < len(from)+4 {
|
|
|
|
tr.wBuf = make([]byte, len(from)+4)
|
2020-06-30 12:48:58 -06:00
|
|
|
}
|
2021-11-12 09:47:36 -07:00
|
|
|
tr.wBuf = tr.wBuf[:len(from)+4]
|
2020-06-30 12:48:58 -06:00
|
|
|
|
|
|
|
// Determine the IP Family for the NULL L2 Header
|
|
|
|
ipVer := from[0] >> 4
|
|
|
|
if ipVer == 4 {
|
2021-11-12 09:47:36 -07:00
|
|
|
tr.wBuf[3] = syscall.AF_INET
|
2020-06-30 12:48:58 -06:00
|
|
|
} else if ipVer == 6 {
|
2021-11-12 09:47:36 -07:00
|
|
|
tr.wBuf[3] = syscall.AF_INET6
|
2020-06-30 12:48:58 -06:00
|
|
|
} else {
|
|
|
|
return 0, errors.New("unable to determine IP version from packet")
|
|
|
|
}
|
|
|
|
|
2021-11-12 09:47:36 -07:00
|
|
|
copy(tr.wBuf[4:], from)
|
2020-06-30 12:48:58 -06:00
|
|
|
|
2021-11-12 09:47:36 -07:00
|
|
|
n, err := tr.f.Write(tr.wBuf)
|
2020-06-30 12:48:58 -06:00
|
|
|
return n - 4, err
|
|
|
|
}
|
|
|
|
|
2021-11-12 09:47:36 -07:00
|
|
|
func (tr *tunReadCloser) Close() error {
|
|
|
|
return tr.f.Close()
|
2020-06-30 12:48:58 -06:00
|
|
|
}
|
2020-07-28 06:53:16 -06:00
|
|
|
|
2021-11-12 11:47:09 -07:00
|
|
|
func (t *tun) Cidr() *net.IPNet {
|
|
|
|
return t.cidr
|
2020-07-28 06:53:16 -06:00
|
|
|
}
|
|
|
|
|
2021-11-12 11:47:09 -07:00
|
|
|
func (t *tun) Name() string {
|
2021-11-12 10:19:28 -07:00
|
|
|
return "iOS"
|
2020-07-28 06:53:16 -06:00
|
|
|
}
|
2021-02-25 13:01:14 -07:00
|
|
|
|
2021-11-12 09:47:36 -07:00
|
|
|
func (t *tun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
|
2021-02-25 13:01:14 -07:00
|
|
|
return nil, fmt.Errorf("TODO: multiqueue not implemented for ios")
|
|
|
|
}
|