nebula/overlay/tun_android.go

105 lines
2.2 KiB
Go
Raw Normal View History

2021-10-21 16:24:11 -05:00
//go:build !e2e_testing
// +build !e2e_testing
2021-11-11 16:37:29 -06:00
package overlay
2020-07-01 10:20:52 -05:00
import (
"fmt"
"io"
"net/netip"
2020-07-01 10:20:52 -05:00
"os"
2024-03-28 15:17:28 -05:00
"sync/atomic"
2020-07-01 10:20:52 -05:00
"github.com/gaissmai/bart"
2021-03-26 09:46:30 -05:00
"github.com/sirupsen/logrus"
2024-03-28 15:17:28 -05:00
"github.com/slackhq/nebula/config"
"github.com/slackhq/nebula/routing"
2024-03-28 15:17:28 -05:00
"github.com/slackhq/nebula/util"
2020-07-01 10:20:52 -05:00
)
type tun struct {
2020-07-01 10:20:52 -05:00
io.ReadWriteCloser
fd int
vpnNetworks []netip.Prefix
Routes atomic.Pointer[[]Route]
routeTree atomic.Pointer[bart.Table[routing.Gateways]]
l *logrus.Logger
2020-07-01 10:20:52 -05:00
}
func newTunFromFd(c *config.C, l *logrus.Logger, deviceFd int, vpnNetworks []netip.Prefix) (*tun, error) {
// XXX Android returns an fd in non-blocking mode which is necessary for shutdown to work properly.
// Be sure not to call file.Fd() as it will set the fd to blocking mode.
2020-07-01 10:20:52 -05:00
file := os.NewFile(uintptr(deviceFd), "/dev/net/tun")
2024-03-28 15:17:28 -05:00
t := &tun{
2020-07-01 10:20:52 -05:00
ReadWriteCloser: file,
fd: deviceFd,
vpnNetworks: vpnNetworks,
2021-03-26 09:46:30 -05:00
l: l,
2024-03-28 15:17:28 -05:00
}
err := t.reload(c, true)
if err != nil {
return nil, err
}
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-07-01 10:20:52 -05:00
}
func newTun(_ *config.C, _ *logrus.Logger, _ []netip.Prefix, _ bool) (*tun, error) {
2020-07-01 10:20:52 -05:00
return nil, fmt.Errorf("newTun not supported in Android")
}
func (t *tun) RoutesFor(ip netip.Addr) routing.Gateways {
r, _ := t.routeTree.Load().Lookup(ip)
2023-12-06 16:18:21 -05:00
return r
}
func (t tun) Activate() error {
2020-07-01 10:20:52 -05:00
return nil
}
2024-03-28 15:17:28 -05:00
func (t *tun) reload(c *config.C, initial bool) error {
change, routes, err := getAllRoutesFromConfig(c, t.vpnNetworks, initial)
2024-03-28 15:17:28 -05:00
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
}
func (t *tun) Networks() []netip.Prefix {
return t.vpnNetworks
}
func (t *tun) Name() string {
return "android"
}
func (t *tun) SupportsMultiqueue() bool {
return false
}
func (t *tun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
return nil, fmt.Errorf("TODO: multiqueue not implemented for android")
}