tun_android.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. //go:build !e2e_testing
  2. // +build !e2e_testing
  3. package overlay
  4. import (
  5. "fmt"
  6. "io"
  7. "net"
  8. "os"
  9. "github.com/sirupsen/logrus"
  10. "github.com/slackhq/nebula/cidr"
  11. "github.com/slackhq/nebula/iputil"
  12. )
  13. type tun struct {
  14. io.ReadWriteCloser
  15. fd int
  16. cidr *net.IPNet
  17. routeTree *cidr.Tree4[iputil.VpnIp]
  18. l *logrus.Logger
  19. }
  20. func newTunFromFd(l *logrus.Logger, deviceFd int, cidr *net.IPNet, _ int, routes []Route, _ int, _ bool) (*tun, error) {
  21. routeTree, err := makeRouteTree(l, routes, false)
  22. if err != nil {
  23. return nil, err
  24. }
  25. // XXX Android returns an fd in non-blocking mode which is necessary for shutdown to work properly.
  26. // Be sure not to call file.Fd() as it will set the fd to blocking mode.
  27. file := os.NewFile(uintptr(deviceFd), "/dev/net/tun")
  28. return &tun{
  29. ReadWriteCloser: file,
  30. fd: deviceFd,
  31. cidr: cidr,
  32. l: l,
  33. routeTree: routeTree,
  34. }, nil
  35. }
  36. func newTun(_ *logrus.Logger, _ string, _ *net.IPNet, _ int, _ []Route, _ int, _ bool, _ bool) (*tun, error) {
  37. return nil, fmt.Errorf("newTun not supported in Android")
  38. }
  39. func (t *tun) RouteFor(ip iputil.VpnIp) iputil.VpnIp {
  40. _, r := t.routeTree.MostSpecificContains(ip)
  41. return r
  42. }
  43. func (t tun) Activate() error {
  44. return nil
  45. }
  46. func (t *tun) Cidr() *net.IPNet {
  47. return t.cidr
  48. }
  49. func (t *tun) Name() string {
  50. return "android"
  51. }
  52. func (t *tun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
  53. return nil, fmt.Errorf("TODO: multiqueue not implemented for android")
  54. }