tun_ios.go 2.3 KB

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