tun_freebsd.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //go:build !e2e_testing
  2. // +build !e2e_testing
  3. package overlay
  4. import (
  5. "fmt"
  6. "io"
  7. "net"
  8. "os"
  9. "os/exec"
  10. "regexp"
  11. "strconv"
  12. "strings"
  13. "github.com/sirupsen/logrus"
  14. "github.com/slackhq/nebula/cidr"
  15. "github.com/slackhq/nebula/iputil"
  16. )
  17. var deviceNameRE = regexp.MustCompile(`^tun[0-9]+$`)
  18. type tun struct {
  19. Device string
  20. cidr *net.IPNet
  21. MTU int
  22. Routes []Route
  23. routeTree *cidr.Tree4
  24. l *logrus.Logger
  25. io.ReadWriteCloser
  26. }
  27. func (t *tun) Close() error {
  28. if t.ReadWriteCloser != nil {
  29. return t.ReadWriteCloser.Close()
  30. }
  31. return nil
  32. }
  33. func newTunFromFd(_ *logrus.Logger, _ int, _ *net.IPNet, _ int, _ []Route, _ int) (*tun, error) {
  34. return nil, fmt.Errorf("newTunFromFd not supported in FreeBSD")
  35. }
  36. func newTun(l *logrus.Logger, deviceName string, cidr *net.IPNet, defaultMTU int, routes []Route, _ int, _ bool) (*tun, error) {
  37. routeTree, err := makeRouteTree(routes, false)
  38. if err != nil {
  39. return nil, err
  40. }
  41. if strings.HasPrefix(deviceName, "/dev/") {
  42. deviceName = strings.TrimPrefix(deviceName, "/dev/")
  43. }
  44. if !deviceNameRE.MatchString(deviceName) {
  45. return nil, fmt.Errorf("tun.dev must match `tun[0-9]+`")
  46. }
  47. return &tun{
  48. Device: deviceName,
  49. cidr: cidr,
  50. MTU: defaultMTU,
  51. Routes: routes,
  52. routeTree: routeTree,
  53. l: l,
  54. }, nil
  55. }
  56. func (t *tun) Activate() error {
  57. var err error
  58. t.ReadWriteCloser, err = os.OpenFile("/dev/"+t.Device, os.O_RDWR, 0)
  59. if err != nil {
  60. return fmt.Errorf("activate failed: %v", err)
  61. }
  62. // TODO use syscalls instead of exec.Command
  63. t.l.Debug("command: ifconfig", t.Device, t.cidr.String(), t.cidr.IP.String())
  64. if err = exec.Command("/sbin/ifconfig", t.Device, t.cidr.String(), t.cidr.IP.String()).Run(); err != nil {
  65. return fmt.Errorf("failed to run 'ifconfig': %s", err)
  66. }
  67. t.l.Debug("command: route", "-n", "add", "-net", t.cidr.String(), "-interface", t.Device)
  68. if err = exec.Command("/sbin/route", "-n", "add", "-net", t.cidr.String(), "-interface", t.Device).Run(); err != nil {
  69. return fmt.Errorf("failed to run 'route add': %s", err)
  70. }
  71. t.l.Debug("command: ifconfig", t.Device, "mtu", strconv.Itoa(t.MTU))
  72. if err = exec.Command("/sbin/ifconfig", t.Device, "mtu", strconv.Itoa(t.MTU)).Run(); err != nil {
  73. return fmt.Errorf("failed to run 'ifconfig': %s", err)
  74. }
  75. // Unsafe path routes
  76. for _, r := range t.Routes {
  77. if r.Via == nil {
  78. // We don't allow route MTUs so only install routes with a via
  79. continue
  80. }
  81. t.l.Debug("command: route", "-n", "add", "-net", r.Cidr.String(), "-interface", t.Device)
  82. if err = exec.Command("/sbin/route", "-n", "add", "-net", r.Cidr.String(), "-interface", t.Device).Run(); err != nil {
  83. return fmt.Errorf("failed to run 'route add' for unsafe_route %s: %s", r.Cidr.String(), err)
  84. }
  85. }
  86. return nil
  87. }
  88. func (t *tun) RouteFor(ip iputil.VpnIp) iputil.VpnIp {
  89. r := t.routeTree.MostSpecificContains(ip)
  90. if r != nil {
  91. return r.(iputil.VpnIp)
  92. }
  93. return 0
  94. }
  95. func (t *tun) Cidr() *net.IPNet {
  96. return t.cidr
  97. }
  98. func (t *tun) Name() string {
  99. return t.Device
  100. }
  101. func (t *tun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
  102. return nil, fmt.Errorf("TODO: multiqueue not implemented for freebsd")
  103. }