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 (
|
2023-06-22 10:13:31 -06:00
|
|
|
"bytes"
|
|
|
|
"errors"
|
2020-05-26 20:23:23 -06:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2023-06-22 10:13:31 -06:00
|
|
|
"io/fs"
|
2020-05-26 20:23:23 -06:00
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"strconv"
|
2023-06-22 10:13:31 -06:00
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
2021-03-26 08:46:30 -06:00
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
2021-11-12 10:19:28 -07:00
|
|
|
"github.com/slackhq/nebula/cidr"
|
|
|
|
"github.com/slackhq/nebula/iputil"
|
2020-05-26 20:23:23 -06:00
|
|
|
)
|
|
|
|
|
2023-06-22 10:13:31 -06:00
|
|
|
const (
|
|
|
|
// FIODGNAME is defined in sys/sys/filio.h on FreeBSD
|
|
|
|
// For 32-bit systems, use FIODGNAME_32 (not defined in this file: 0x80086678)
|
|
|
|
FIODGNAME = 0x80106678
|
|
|
|
)
|
|
|
|
|
|
|
|
type fiodgnameArg struct {
|
|
|
|
length int32
|
|
|
|
pad [4]byte
|
|
|
|
buf unsafe.Pointer
|
|
|
|
}
|
|
|
|
|
|
|
|
type ifreqRename struct {
|
|
|
|
Name [16]byte
|
|
|
|
Data uintptr
|
|
|
|
}
|
|
|
|
|
|
|
|
type ifreqDestroy struct {
|
|
|
|
Name [16]byte
|
|
|
|
pad [16]byte
|
|
|
|
}
|
2020-05-26 20:23:23 -06:00
|
|
|
|
2021-11-12 09:47:36 -07:00
|
|
|
type tun struct {
|
2021-11-12 11:47:09 -07:00
|
|
|
Device string
|
|
|
|
cidr *net.IPNet
|
|
|
|
MTU int
|
|
|
|
Routes []Route
|
|
|
|
routeTree *cidr.Tree4
|
|
|
|
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 {
|
2023-06-22 10:13:31 -06:00
|
|
|
if err := t.ReadWriteCloser.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_DGRAM, syscall.IPPROTO_IP)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer syscall.Close(s)
|
|
|
|
|
|
|
|
ifreq := ifreqDestroy{Name: t.deviceBytes()}
|
|
|
|
|
|
|
|
// Destroy the interface
|
|
|
|
err = ioctl(uintptr(s), syscall.SIOCIFDESTROY, uintptr(unsafe.Pointer(&ifreq)))
|
|
|
|
return err
|
2021-11-02 12:14:26 -06:00
|
|
|
}
|
2023-06-22 10:13:31 -06:00
|
|
|
|
2021-11-02 12:14:26 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-09 09:36:55 -06:00
|
|
|
func newTunFromFd(_ *logrus.Logger, _ int, _ *net.IPNet, _ int, _ []Route, _ int, _ bool) (*tun, error) {
|
2020-06-30 12:48:58 -06:00
|
|
|
return nil, fmt.Errorf("newTunFromFd not supported in FreeBSD")
|
|
|
|
}
|
|
|
|
|
2023-05-09 09:36:55 -06:00
|
|
|
func newTun(l *logrus.Logger, deviceName string, cidr *net.IPNet, defaultMTU int, routes []Route, _ int, _ bool, _ bool) (*tun, error) {
|
2023-06-22 10:13:31 -06:00
|
|
|
// Try to open existing tun device
|
|
|
|
var file *os.File
|
|
|
|
var err error
|
|
|
|
if deviceName != "" {
|
|
|
|
file, err = os.OpenFile("/dev/"+deviceName, os.O_RDWR, 0)
|
|
|
|
}
|
|
|
|
if errors.Is(err, fs.ErrNotExist) || deviceName == "" {
|
|
|
|
// If the device doesn't already exist, request a new one and rename it
|
|
|
|
file, err = os.OpenFile("/dev/tun", os.O_RDWR, 0)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rawConn, err := file.SyscallConn()
|
2021-11-12 10:19:28 -07:00
|
|
|
if err != nil {
|
2023-06-22 10:13:31 -06:00
|
|
|
return nil, fmt.Errorf("SyscallConn: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var name [16]byte
|
|
|
|
var ctrlErr error
|
|
|
|
rawConn.Control(func(fd uintptr) {
|
|
|
|
// Read the name of the interface
|
|
|
|
arg := fiodgnameArg{length: 16, buf: unsafe.Pointer(&name)}
|
|
|
|
ctrlErr = ioctl(fd, FIODGNAME, uintptr(unsafe.Pointer(&arg)))
|
|
|
|
})
|
|
|
|
if ctrlErr != nil {
|
2021-11-12 10:19:28 -07:00
|
|
|
return nil, err
|
2020-05-26 20:23:23 -06:00
|
|
|
}
|
2021-11-12 10:19:28 -07:00
|
|
|
|
2023-06-22 10:13:31 -06:00
|
|
|
ifName := string(bytes.TrimRight(name[:], "\x00"))
|
|
|
|
if deviceName == "" {
|
|
|
|
deviceName = ifName
|
2020-05-26 20:23:23 -06:00
|
|
|
}
|
2023-06-22 10:13:31 -06:00
|
|
|
|
|
|
|
// If the name doesn't match the desired interface name, rename it now
|
|
|
|
if ifName != deviceName {
|
|
|
|
s, err := syscall.Socket(
|
|
|
|
syscall.AF_INET,
|
|
|
|
syscall.SOCK_DGRAM,
|
|
|
|
syscall.IPPROTO_IP,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer syscall.Close(s)
|
|
|
|
|
|
|
|
fd := uintptr(s)
|
|
|
|
|
|
|
|
var fromName [16]byte
|
|
|
|
var toName [16]byte
|
|
|
|
copy(fromName[:], ifName)
|
|
|
|
copy(toName[:], deviceName)
|
|
|
|
|
|
|
|
ifrr := ifreqRename{
|
|
|
|
Name: fromName,
|
|
|
|
Data: uintptr(unsafe.Pointer(&toName)),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the device name
|
|
|
|
ioctl(fd, syscall.SIOCSIFNAME, uintptr(unsafe.Pointer(&ifrr)))
|
2020-05-26 20:23:23 -06:00
|
|
|
}
|
2023-06-22 10:13:31 -06:00
|
|
|
|
|
|
|
routeTree, err := makeRouteTree(l, routes, false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-11-12 09:47:36 -07:00
|
|
|
return &tun{
|
2023-06-22 10:13:31 -06:00
|
|
|
ReadWriteCloser: file,
|
|
|
|
Device: deviceName,
|
|
|
|
cidr: cidr,
|
|
|
|
MTU: defaultMTU,
|
|
|
|
Routes: routes,
|
|
|
|
routeTree: routeTree,
|
|
|
|
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
|
|
|
|
// TODO use syscalls instead of exec.Command
|
2021-11-12 11:47:09 -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 11:47:09 -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 10:19:28 -07:00
|
|
|
for _, r := range t.Routes {
|
2023-04-10 11:32:37 -06:00
|
|
|
if r.Via == nil || !r.Install {
|
2021-11-12 10:19:28 -07:00
|
|
|
// We don't allow route MTUs so only install routes with a via
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-11-12 09:47:36 -07:00
|
|
|
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 10:19:28 -07:00
|
|
|
func (t *tun) RouteFor(ip iputil.VpnIp) iputil.VpnIp {
|
2021-11-12 11:47:09 -07:00
|
|
|
r := t.routeTree.MostSpecificContains(ip)
|
2021-11-12 10:19:28 -07:00
|
|
|
if r != nil {
|
|
|
|
return r.(iputil.VpnIp)
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2021-11-12 11:47:09 -07:00
|
|
|
func (t *tun) Cidr() *net.IPNet {
|
|
|
|
return t.cidr
|
2020-07-28 06:53:16 -06:00
|
|
|
}
|
|
|
|
|
2021-11-12 11:47:09 -07:00
|
|
|
func (t *tun) Name() string {
|
2021-11-12 09:47:36 -07:00
|
|
|
return t.Device
|
2020-07-28 06:53:16 -06: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")
|
|
|
|
}
|
2023-06-22 10:13:31 -06:00
|
|
|
|
|
|
|
func (t *tun) deviceBytes() (o [16]byte) {
|
|
|
|
for i, c := range t.Device {
|
|
|
|
o[i] = byte(c)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|