2021-10-21 15:24:11 -06:00
|
|
|
//go:build !e2e_testing
|
2021-03-29 13:29:20 -06:00
|
|
|
// +build !e2e_testing
|
|
|
|
|
2021-11-11 15:37:29 -07:00
|
|
|
package overlay
|
2020-05-26 20:23:23 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2021-03-26 08:46:30 -06:00
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
2020-05-26 20:23:23 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
var deviceNameRE = regexp.MustCompile(`^tun[0-9]+$`)
|
|
|
|
|
2021-11-12 09:47:36 -07:00
|
|
|
type tun struct {
|
2020-05-26 20:23:23 -06:00
|
|
|
Device string
|
|
|
|
Cidr *net.IPNet
|
|
|
|
MTU int
|
2021-11-11 15:37:29 -07:00
|
|
|
UnsafeRoutes []Route
|
2021-03-26 08:46:30 -06:00
|
|
|
l *logrus.Logger
|
2020-05-26 20:23:23 -06:00
|
|
|
|
|
|
|
io.ReadWriteCloser
|
|
|
|
}
|
|
|
|
|
2021-11-12 09:47:36 -07:00
|
|
|
func (t *tun) Close() error {
|
|
|
|
if t.ReadWriteCloser != nil {
|
|
|
|
return t.ReadWriteCloser.Close()
|
2021-11-02 12:14:26 -06:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-12 09:47:36 -07:00
|
|
|
func newTunFromFd(_ *logrus.Logger, _ int, _ *net.IPNet, _ int, _ []Route, _ []Route, _ int) (*tun, error) {
|
2020-06-30 12:48:58 -06:00
|
|
|
return nil, fmt.Errorf("newTunFromFd not supported in FreeBSD")
|
|
|
|
}
|
|
|
|
|
2021-11-12 09:47:36 -07:00
|
|
|
func newTun(l *logrus.Logger, deviceName string, cidr *net.IPNet, defaultMTU int, routes []Route, unsafeRoutes []Route, _ int, _ bool) (*tun, error) {
|
2020-05-26 20:23:23 -06:00
|
|
|
if len(routes) > 0 {
|
2021-11-12 09:47:36 -07:00
|
|
|
return nil, fmt.Errorf("route MTU not supported in FreeBSD")
|
2020-05-26 20:23:23 -06:00
|
|
|
}
|
|
|
|
if strings.HasPrefix(deviceName, "/dev/") {
|
|
|
|
deviceName = strings.TrimPrefix(deviceName, "/dev/")
|
|
|
|
}
|
|
|
|
if !deviceNameRE.MatchString(deviceName) {
|
|
|
|
return nil, fmt.Errorf("tun.dev must match `tun[0-9]+`")
|
|
|
|
}
|
2021-11-12 09:47:36 -07:00
|
|
|
return &tun{
|
2020-05-26 20:23:23 -06:00
|
|
|
Device: deviceName,
|
|
|
|
Cidr: cidr,
|
|
|
|
MTU: defaultMTU,
|
|
|
|
UnsafeRoutes: unsafeRoutes,
|
2021-03-26 08:46:30 -06:00
|
|
|
l: l,
|
2020-05-26 20:23:23 -06:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-11-12 09:47:36 -07:00
|
|
|
func (t *tun) Activate() error {
|
2020-05-26 20:23:23 -06:00
|
|
|
var err error
|
2021-11-12 09:47:36 -07:00
|
|
|
t.ReadWriteCloser, err = os.OpenFile("/dev/"+t.Device, os.O_RDWR, 0)
|
2020-05-26 20:23:23 -06:00
|
|
|
if err != nil {
|
2021-11-12 09:47:36 -07:00
|
|
|
return fmt.Errorf("activate failed: %v", err)
|
2020-05-26 20:23:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO use syscalls instead of exec.Command
|
2021-11-12 09:47:36 -07:00
|
|
|
t.l.Debug("command: ifconfig", t.Device, t.Cidr.String(), t.Cidr.IP.String())
|
|
|
|
if err = exec.Command("/sbin/ifconfig", t.Device, t.Cidr.String(), t.Cidr.IP.String()).Run(); err != nil {
|
2020-05-26 20:23:23 -06:00
|
|
|
return fmt.Errorf("failed to run 'ifconfig': %s", err)
|
|
|
|
}
|
2021-11-12 09:47:36 -07:00
|
|
|
t.l.Debug("command: route", "-n", "add", "-net", t.Cidr.String(), "-interface", t.Device)
|
|
|
|
if err = exec.Command("/sbin/route", "-n", "add", "-net", t.Cidr.String(), "-interface", t.Device).Run(); err != nil {
|
2020-05-26 20:23:23 -06:00
|
|
|
return fmt.Errorf("failed to run 'route add': %s", err)
|
|
|
|
}
|
2021-11-12 09:47:36 -07:00
|
|
|
t.l.Debug("command: ifconfig", t.Device, "mtu", strconv.Itoa(t.MTU))
|
|
|
|
if err = exec.Command("/sbin/ifconfig", t.Device, "mtu", strconv.Itoa(t.MTU)).Run(); err != nil {
|
2020-05-26 20:23:23 -06:00
|
|
|
return fmt.Errorf("failed to run 'ifconfig': %s", err)
|
|
|
|
}
|
|
|
|
// Unsafe path routes
|
2021-11-12 09:47:36 -07:00
|
|
|
for _, r := range t.UnsafeRoutes {
|
|
|
|
t.l.Debug("command: route", "-n", "add", "-net", r.Cidr.String(), "-interface", t.Device)
|
|
|
|
if err = exec.Command("/sbin/route", "-n", "add", "-net", r.Cidr.String(), "-interface", t.Device).Run(); err != nil {
|
2021-11-11 15:37:29 -07:00
|
|
|
return fmt.Errorf("failed to run 'route add' for unsafe_route %s: %s", r.Cidr.String(), err)
|
2020-05-26 20:23:23 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-12 09:47:36 -07:00
|
|
|
func (t *tun) CidrNet() *net.IPNet {
|
|
|
|
return t.Cidr
|
2020-07-28 06:53:16 -06:00
|
|
|
}
|
|
|
|
|
2021-11-12 09:47:36 -07:00
|
|
|
func (t *tun) DeviceName() string {
|
|
|
|
return t.Device
|
2020-07-28 06:53:16 -06:00
|
|
|
}
|
|
|
|
|
2021-11-12 09:47:36 -07:00
|
|
|
func (t *tun) WriteRaw(b []byte) error {
|
|
|
|
_, err := t.Write(b)
|
2020-05-26 20:23:23 -06:00
|
|
|
return err
|
|
|
|
}
|
2021-02-25 13:01:14 -07:00
|
|
|
|
2021-11-12 09:47:36 -07:00
|
|
|
func (t *tun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
|
2021-02-25 13:01:14 -07:00
|
|
|
return nil, fmt.Errorf("TODO: multiqueue not implemented for freebsd")
|
|
|
|
}
|