tun_freebsd.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. )
  15. var deviceNameRE = regexp.MustCompile(`^tun[0-9]+$`)
  16. type tun struct {
  17. Device string
  18. Cidr *net.IPNet
  19. MTU int
  20. UnsafeRoutes []Route
  21. l *logrus.Logger
  22. io.ReadWriteCloser
  23. }
  24. func (t *tun) Close() error {
  25. if t.ReadWriteCloser != nil {
  26. return t.ReadWriteCloser.Close()
  27. }
  28. return nil
  29. }
  30. func newTunFromFd(_ *logrus.Logger, _ int, _ *net.IPNet, _ int, _ []Route, _ []Route, _ int) (*tun, error) {
  31. return nil, fmt.Errorf("newTunFromFd not supported in FreeBSD")
  32. }
  33. func newTun(l *logrus.Logger, deviceName string, cidr *net.IPNet, defaultMTU int, routes []Route, unsafeRoutes []Route, _ int, _ bool) (*tun, error) {
  34. if len(routes) > 0 {
  35. return nil, fmt.Errorf("route MTU not supported in FreeBSD")
  36. }
  37. if strings.HasPrefix(deviceName, "/dev/") {
  38. deviceName = strings.TrimPrefix(deviceName, "/dev/")
  39. }
  40. if !deviceNameRE.MatchString(deviceName) {
  41. return nil, fmt.Errorf("tun.dev must match `tun[0-9]+`")
  42. }
  43. return &tun{
  44. Device: deviceName,
  45. Cidr: cidr,
  46. MTU: defaultMTU,
  47. UnsafeRoutes: unsafeRoutes,
  48. l: l,
  49. }, nil
  50. }
  51. func (t *tun) Activate() error {
  52. var err error
  53. t.ReadWriteCloser, err = os.OpenFile("/dev/"+t.Device, os.O_RDWR, 0)
  54. if err != nil {
  55. return fmt.Errorf("activate failed: %v", err)
  56. }
  57. // TODO use syscalls instead of exec.Command
  58. t.l.Debug("command: ifconfig", t.Device, t.Cidr.String(), t.Cidr.IP.String())
  59. if err = exec.Command("/sbin/ifconfig", t.Device, t.Cidr.String(), t.Cidr.IP.String()).Run(); err != nil {
  60. return fmt.Errorf("failed to run 'ifconfig': %s", err)
  61. }
  62. t.l.Debug("command: route", "-n", "add", "-net", t.Cidr.String(), "-interface", t.Device)
  63. if err = exec.Command("/sbin/route", "-n", "add", "-net", t.Cidr.String(), "-interface", t.Device).Run(); err != nil {
  64. return fmt.Errorf("failed to run 'route add': %s", err)
  65. }
  66. t.l.Debug("command: ifconfig", t.Device, "mtu", strconv.Itoa(t.MTU))
  67. if err = exec.Command("/sbin/ifconfig", t.Device, "mtu", strconv.Itoa(t.MTU)).Run(); err != nil {
  68. return fmt.Errorf("failed to run 'ifconfig': %s", err)
  69. }
  70. // Unsafe path routes
  71. for _, r := range t.UnsafeRoutes {
  72. t.l.Debug("command: route", "-n", "add", "-net", r.Cidr.String(), "-interface", t.Device)
  73. if err = exec.Command("/sbin/route", "-n", "add", "-net", r.Cidr.String(), "-interface", t.Device).Run(); err != nil {
  74. return fmt.Errorf("failed to run 'route add' for unsafe_route %s: %s", r.Cidr.String(), err)
  75. }
  76. }
  77. return nil
  78. }
  79. func (t *tun) CidrNet() *net.IPNet {
  80. return t.Cidr
  81. }
  82. func (t *tun) DeviceName() string {
  83. return t.Device
  84. }
  85. func (t *tun) WriteRaw(b []byte) error {
  86. _, err := t.Write(b)
  87. return err
  88. }
  89. func (t *tun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
  90. return nil, fmt.Errorf("TODO: multiqueue not implemented for freebsd")
  91. }