tun_freebsd.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. //go:build !e2e_testing
  2. // +build !e2e_testing
  3. package overlay
  4. import (
  5. "bytes"
  6. "errors"
  7. "fmt"
  8. "io"
  9. "io/fs"
  10. "net"
  11. "os"
  12. "os/exec"
  13. "strconv"
  14. "syscall"
  15. "unsafe"
  16. "github.com/sirupsen/logrus"
  17. "github.com/slackhq/nebula/cidr"
  18. "github.com/slackhq/nebula/iputil"
  19. )
  20. const (
  21. // FIODGNAME is defined in sys/sys/filio.h on FreeBSD
  22. // For 32-bit systems, use FIODGNAME_32 (not defined in this file: 0x80086678)
  23. FIODGNAME = 0x80106678
  24. )
  25. type fiodgnameArg struct {
  26. length int32
  27. pad [4]byte
  28. buf unsafe.Pointer
  29. }
  30. type ifreqRename struct {
  31. Name [16]byte
  32. Data uintptr
  33. }
  34. type ifreqDestroy struct {
  35. Name [16]byte
  36. pad [16]byte
  37. }
  38. type tun struct {
  39. Device string
  40. cidr *net.IPNet
  41. MTU int
  42. Routes []Route
  43. routeTree *cidr.Tree4
  44. l *logrus.Logger
  45. io.ReadWriteCloser
  46. }
  47. func (t *tun) Close() error {
  48. if t.ReadWriteCloser != nil {
  49. if err := t.ReadWriteCloser.Close(); err != nil {
  50. return err
  51. }
  52. s, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_DGRAM, syscall.IPPROTO_IP)
  53. if err != nil {
  54. return err
  55. }
  56. defer syscall.Close(s)
  57. ifreq := ifreqDestroy{Name: t.deviceBytes()}
  58. // Destroy the interface
  59. err = ioctl(uintptr(s), syscall.SIOCIFDESTROY, uintptr(unsafe.Pointer(&ifreq)))
  60. return err
  61. }
  62. return nil
  63. }
  64. func newTunFromFd(_ *logrus.Logger, _ int, _ *net.IPNet, _ int, _ []Route, _ int, _ bool) (*tun, error) {
  65. return nil, fmt.Errorf("newTunFromFd not supported in FreeBSD")
  66. }
  67. func newTun(l *logrus.Logger, deviceName string, cidr *net.IPNet, defaultMTU int, routes []Route, _ int, _ bool, _ bool) (*tun, error) {
  68. // Try to open existing tun device
  69. var file *os.File
  70. var err error
  71. if deviceName != "" {
  72. file, err = os.OpenFile("/dev/"+deviceName, os.O_RDWR, 0)
  73. }
  74. if errors.Is(err, fs.ErrNotExist) || deviceName == "" {
  75. // If the device doesn't already exist, request a new one and rename it
  76. file, err = os.OpenFile("/dev/tun", os.O_RDWR, 0)
  77. }
  78. if err != nil {
  79. return nil, err
  80. }
  81. rawConn, err := file.SyscallConn()
  82. if err != nil {
  83. return nil, fmt.Errorf("SyscallConn: %v", err)
  84. }
  85. var name [16]byte
  86. var ctrlErr error
  87. rawConn.Control(func(fd uintptr) {
  88. // Read the name of the interface
  89. arg := fiodgnameArg{length: 16, buf: unsafe.Pointer(&name)}
  90. ctrlErr = ioctl(fd, FIODGNAME, uintptr(unsafe.Pointer(&arg)))
  91. })
  92. if ctrlErr != nil {
  93. return nil, err
  94. }
  95. ifName := string(bytes.TrimRight(name[:], "\x00"))
  96. if deviceName == "" {
  97. deviceName = ifName
  98. }
  99. // If the name doesn't match the desired interface name, rename it now
  100. if ifName != deviceName {
  101. s, err := syscall.Socket(
  102. syscall.AF_INET,
  103. syscall.SOCK_DGRAM,
  104. syscall.IPPROTO_IP,
  105. )
  106. if err != nil {
  107. return nil, err
  108. }
  109. defer syscall.Close(s)
  110. fd := uintptr(s)
  111. var fromName [16]byte
  112. var toName [16]byte
  113. copy(fromName[:], ifName)
  114. copy(toName[:], deviceName)
  115. ifrr := ifreqRename{
  116. Name: fromName,
  117. Data: uintptr(unsafe.Pointer(&toName)),
  118. }
  119. // Set the device name
  120. ioctl(fd, syscall.SIOCSIFNAME, uintptr(unsafe.Pointer(&ifrr)))
  121. }
  122. routeTree, err := makeRouteTree(l, routes, false)
  123. if err != nil {
  124. return nil, err
  125. }
  126. return &tun{
  127. ReadWriteCloser: file,
  128. Device: deviceName,
  129. cidr: cidr,
  130. MTU: defaultMTU,
  131. Routes: routes,
  132. routeTree: routeTree,
  133. l: l,
  134. }, nil
  135. }
  136. func (t *tun) Activate() error {
  137. var err error
  138. // TODO use syscalls instead of exec.Command
  139. t.l.Debug("command: ifconfig", t.Device, t.cidr.String(), t.cidr.IP.String())
  140. if err = exec.Command("/sbin/ifconfig", t.Device, t.cidr.String(), t.cidr.IP.String()).Run(); err != nil {
  141. return fmt.Errorf("failed to run 'ifconfig': %s", err)
  142. }
  143. t.l.Debug("command: route", "-n", "add", "-net", t.cidr.String(), "-interface", t.Device)
  144. if err = exec.Command("/sbin/route", "-n", "add", "-net", t.cidr.String(), "-interface", t.Device).Run(); err != nil {
  145. return fmt.Errorf("failed to run 'route add': %s", err)
  146. }
  147. t.l.Debug("command: ifconfig", t.Device, "mtu", strconv.Itoa(t.MTU))
  148. if err = exec.Command("/sbin/ifconfig", t.Device, "mtu", strconv.Itoa(t.MTU)).Run(); err != nil {
  149. return fmt.Errorf("failed to run 'ifconfig': %s", err)
  150. }
  151. // Unsafe path routes
  152. for _, r := range t.Routes {
  153. if r.Via == nil || !r.Install {
  154. // We don't allow route MTUs so only install routes with a via
  155. continue
  156. }
  157. t.l.Debug("command: route", "-n", "add", "-net", r.Cidr.String(), "-interface", t.Device)
  158. if err = exec.Command("/sbin/route", "-n", "add", "-net", r.Cidr.String(), "-interface", t.Device).Run(); err != nil {
  159. return fmt.Errorf("failed to run 'route add' for unsafe_route %s: %s", r.Cidr.String(), err)
  160. }
  161. }
  162. return nil
  163. }
  164. func (t *tun) RouteFor(ip iputil.VpnIp) iputil.VpnIp {
  165. r := t.routeTree.MostSpecificContains(ip)
  166. if r != nil {
  167. return r.(iputil.VpnIp)
  168. }
  169. return 0
  170. }
  171. func (t *tun) Cidr() *net.IPNet {
  172. return t.cidr
  173. }
  174. func (t *tun) Name() string {
  175. return t.Device
  176. }
  177. func (t *tun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
  178. return nil, fmt.Errorf("TODO: multiqueue not implemented for freebsd")
  179. }
  180. func (t *tun) deviceBytes() (o [16]byte) {
  181. for i, c := range t.Device {
  182. o[i] = byte(c)
  183. }
  184. return
  185. }