tun_ios.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //go:build ios && !e2e_testing
  2. // +build ios,!e2e_testing
  3. package overlay
  4. import (
  5. "errors"
  6. "fmt"
  7. "io"
  8. "net"
  9. "os"
  10. "sync"
  11. "syscall"
  12. "github.com/sirupsen/logrus"
  13. "github.com/slackhq/nebula/cidr"
  14. "github.com/slackhq/nebula/iputil"
  15. )
  16. type tun struct {
  17. io.ReadWriteCloser
  18. cidr *net.IPNet
  19. routeTree *cidr.Tree4[iputil.VpnIp]
  20. }
  21. func newTun(_ *logrus.Logger, _ string, _ *net.IPNet, _ int, _ []Route, _ int, _ bool, _ bool) (*tun, error) {
  22. return nil, fmt.Errorf("newTun not supported in iOS")
  23. }
  24. func newTunFromFd(l *logrus.Logger, deviceFd int, cidr *net.IPNet, _ int, routes []Route, _ int, _ bool) (*tun, error) {
  25. routeTree, err := makeRouteTree(l, routes, false)
  26. if err != nil {
  27. return nil, err
  28. }
  29. file := os.NewFile(uintptr(deviceFd), "/dev/tun")
  30. return &tun{
  31. cidr: cidr,
  32. ReadWriteCloser: &tunReadCloser{f: file},
  33. routeTree: routeTree,
  34. }, nil
  35. }
  36. func (t *tun) Activate() error {
  37. return nil
  38. }
  39. func (t *tun) RouteFor(ip iputil.VpnIp) iputil.VpnIp {
  40. _, r := t.routeTree.MostSpecificContains(ip)
  41. return r
  42. }
  43. // The following is hoisted up from water, we do this so we can inject our own fd on iOS
  44. type tunReadCloser struct {
  45. f io.ReadWriteCloser
  46. rMu sync.Mutex
  47. rBuf []byte
  48. wMu sync.Mutex
  49. wBuf []byte
  50. }
  51. func (tr *tunReadCloser) Read(to []byte) (int, error) {
  52. tr.rMu.Lock()
  53. defer tr.rMu.Unlock()
  54. if cap(tr.rBuf) < len(to)+4 {
  55. tr.rBuf = make([]byte, len(to)+4)
  56. }
  57. tr.rBuf = tr.rBuf[:len(to)+4]
  58. n, err := tr.f.Read(tr.rBuf)
  59. copy(to, tr.rBuf[4:])
  60. return n - 4, err
  61. }
  62. func (tr *tunReadCloser) Write(from []byte) (int, error) {
  63. if len(from) == 0 {
  64. return 0, syscall.EIO
  65. }
  66. tr.wMu.Lock()
  67. defer tr.wMu.Unlock()
  68. if cap(tr.wBuf) < len(from)+4 {
  69. tr.wBuf = make([]byte, len(from)+4)
  70. }
  71. tr.wBuf = tr.wBuf[:len(from)+4]
  72. // Determine the IP Family for the NULL L2 Header
  73. ipVer := from[0] >> 4
  74. if ipVer == 4 {
  75. tr.wBuf[3] = syscall.AF_INET
  76. } else if ipVer == 6 {
  77. tr.wBuf[3] = syscall.AF_INET6
  78. } else {
  79. return 0, errors.New("unable to determine IP version from packet")
  80. }
  81. copy(tr.wBuf[4:], from)
  82. n, err := tr.f.Write(tr.wBuf)
  83. return n - 4, err
  84. }
  85. func (tr *tunReadCloser) Close() error {
  86. return tr.f.Close()
  87. }
  88. func (t *tun) Cidr() *net.IPNet {
  89. return t.cidr
  90. }
  91. func (t *tun) Name() string {
  92. return "iOS"
  93. }
  94. func (t *tun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
  95. return nil, fmt.Errorf("TODO: multiqueue not implemented for ios")
  96. }