tun_android.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //go:build !e2e_testing
  2. // +build !e2e_testing
  3. package overlay
  4. import (
  5. "fmt"
  6. "io"
  7. "net/netip"
  8. "os"
  9. "sync/atomic"
  10. "github.com/gaissmai/bart"
  11. "github.com/sirupsen/logrus"
  12. "github.com/slackhq/nebula/config"
  13. "github.com/slackhq/nebula/routing"
  14. "github.com/slackhq/nebula/util"
  15. )
  16. type tun struct {
  17. io.ReadWriteCloser
  18. fd int
  19. vpnNetworks []netip.Prefix
  20. Routes atomic.Pointer[[]Route]
  21. routeTree atomic.Pointer[bart.Table[routing.Gateways]]
  22. l *logrus.Logger
  23. }
  24. func newTunFromFd(c *config.C, l *logrus.Logger, deviceFd int, vpnNetworks []netip.Prefix) (*tun, error) {
  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. t := &tun{
  29. ReadWriteCloser: file,
  30. fd: deviceFd,
  31. vpnNetworks: vpnNetworks,
  32. l: l,
  33. }
  34. err := t.reload(c, true)
  35. if err != nil {
  36. return nil, err
  37. }
  38. c.RegisterReloadCallback(func(c *config.C) {
  39. err := t.reload(c, false)
  40. if err != nil {
  41. util.LogWithContextIfNeeded("failed to reload tun device", err, t.l)
  42. }
  43. })
  44. return t, nil
  45. }
  46. func newTun(_ *config.C, _ *logrus.Logger, _ []netip.Prefix, _ bool) (*tun, error) {
  47. return nil, fmt.Errorf("newTun not supported in Android")
  48. }
  49. func (t *tun) RoutesFor(ip netip.Addr) routing.Gateways {
  50. r, _ := t.routeTree.Load().Lookup(ip)
  51. return r
  52. }
  53. func (t tun) Activate() error {
  54. return nil
  55. }
  56. func (t *tun) reload(c *config.C, initial bool) error {
  57. change, routes, err := getAllRoutesFromConfig(c, t.vpnNetworks, initial)
  58. if err != nil {
  59. return err
  60. }
  61. if !initial && !change {
  62. return nil
  63. }
  64. routeTree, err := makeRouteTree(t.l, routes, false)
  65. if err != nil {
  66. return err
  67. }
  68. // Teach nebula how to handle the routes
  69. t.Routes.Store(&routes)
  70. t.routeTree.Store(routeTree)
  71. return nil
  72. }
  73. func (t *tun) Networks() []netip.Prefix {
  74. return t.vpnNetworks
  75. }
  76. func (t *tun) Name() string {
  77. return "android"
  78. }
  79. func (t *tun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
  80. return nil, fmt.Errorf("TODO: multiqueue not implemented for android")
  81. }