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
|
2020-07-01 09:20:52 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
|
2021-03-26 08:46:30 -06:00
|
|
|
"github.com/sirupsen/logrus"
|
2022-08-05 08:58:10 -06:00
|
|
|
"github.com/slackhq/nebula/cidr"
|
2021-11-12 10:19:28 -07:00
|
|
|
"github.com/slackhq/nebula/iputil"
|
2020-07-01 09:20:52 -06:00
|
|
|
)
|
|
|
|
|
2021-11-12 09:47:36 -07:00
|
|
|
type tun struct {
|
2020-07-01 09:20:52 -06:00
|
|
|
io.ReadWriteCloser
|
2022-08-05 08:58:10 -06:00
|
|
|
fd int
|
|
|
|
cidr *net.IPNet
|
2023-12-06 14:18:21 -07:00
|
|
|
routeTree *cidr.Tree4[iputil.VpnIp]
|
2022-08-05 08:58:10 -06:00
|
|
|
l *logrus.Logger
|
2020-07-01 09:20:52 -06:00
|
|
|
}
|
|
|
|
|
2023-05-09 09:36:55 -06:00
|
|
|
func newTunFromFd(l *logrus.Logger, deviceFd int, cidr *net.IPNet, _ int, routes []Route, _ int, _ bool) (*tun, error) {
|
2022-08-05 08:58:10 -06:00
|
|
|
routeTree, err := makeRouteTree(l, routes, false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-11-12 10:19:28 -07:00
|
|
|
}
|
|
|
|
|
2022-11-11 09:18:43 -07:00
|
|
|
// 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 09:20:52 -06:00
|
|
|
file := os.NewFile(uintptr(deviceFd), "/dev/net/tun")
|
|
|
|
|
2021-11-12 09:47:36 -07:00
|
|
|
return &tun{
|
2020-07-01 09:20:52 -06:00
|
|
|
ReadWriteCloser: file,
|
2022-11-11 09:18:43 -07:00
|
|
|
fd: deviceFd,
|
2021-11-12 11:47:09 -07:00
|
|
|
cidr: cidr,
|
2021-03-26 08:46:30 -06:00
|
|
|
l: l,
|
2022-08-05 08:58:10 -06:00
|
|
|
routeTree: routeTree,
|
2021-11-12 09:47:36 -07:00
|
|
|
}, nil
|
2020-07-01 09:20:52 -06:00
|
|
|
}
|
|
|
|
|
2023-05-09 09:36:55 -06:00
|
|
|
func newTun(_ *logrus.Logger, _ string, _ *net.IPNet, _ int, _ []Route, _ int, _ bool, _ bool) (*tun, error) {
|
2020-07-01 09:20:52 -06:00
|
|
|
return nil, fmt.Errorf("newTun not supported in Android")
|
|
|
|
}
|
|
|
|
|
2022-08-05 08:58:10 -06:00
|
|
|
func (t *tun) RouteFor(ip iputil.VpnIp) iputil.VpnIp {
|
2023-12-06 14:18:21 -07: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 tun) Activate() error {
|
2020-07-01 09:20:52 -06:00
|
|
|
return nil
|
|
|
|
}
|
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 "android"
|
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 android")
|
|
|
|
}
|