Skip to content

Commit 8ba5d64

Browse files
authored
Add support for naming FreeBSD tun devices (#903)
1 parent 3bbf5f4 commit 8ba5d64

File tree

6 files changed

+141
-40
lines changed

6 files changed

+141
-40
lines changed

Makefile

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,13 @@ ALL_LINUX = linux-amd64 \
4444
linux-mips-softfloat \
4545
linux-riscv64
4646

47+
ALL_FREEBSD = freebsd-amd64 \
48+
freebsd-arm64
49+
4750
ALL = $(ALL_LINUX) \
51+
$(ALL_FREEBSD) \
4852
darwin-amd64 \
4953
darwin-arm64 \
50-
freebsd-amd64 \
5154
windows-amd64 \
5255
windows-arm64
5356

@@ -75,7 +78,7 @@ release: $(ALL:%=build/nebula-%.tar.gz)
7578

7679
release-linux: $(ALL_LINUX:%=build/nebula-%.tar.gz)
7780

78-
release-freebsd: build/nebula-freebsd-amd64.tar.gz
81+
release-freebsd: $(ALL_FREEBSD:%=build/nebula-%.tar.gz)
7982

8083
release-boringcrypto: build/nebula-linux-$(shell go env GOARCH)-boringcrypto.tar.gz
8184

@@ -93,6 +96,9 @@ bin-darwin: build/darwin-amd64/nebula build/darwin-amd64/nebula-cert
9396
bin-freebsd: build/freebsd-amd64/nebula build/freebsd-amd64/nebula-cert
9497
mv $? .
9598

99+
bin-freebsd-arm64: build/freebsd-arm64/nebula build/freebsd-arm64/nebula-cert
100+
mv $? .
101+
96102
bin-boringcrypto: build/linux-$(shell go env GOARCH)-boringcrypto/nebula build/linux-$(shell go env GOARCH)-boringcrypto/nebula-cert
97103
mv $? .
98104

examples/config.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,6 @@ tun:
194194
disabled: false
195195
# Name of the device. If not set, a default will be chosen by the OS.
196196
# For macOS: if set, must be in the form `utun[0-9]+`.
197-
# For FreeBSD: Required to be set, must be in the form `tun[0-9]+`.
198197
dev: nebula1
199198
# Toggles forwarding of local broadcast packets, the address of which depends on the ip/mask encoded in pki.cert
200199
drop_local_broadcast: false

overlay/tun_darwin.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,6 @@ type ifReq struct {
4747
pad [8]byte
4848
}
4949

50-
func ioctl(a1, a2, a3 uintptr) error {
51-
_, _, errno := unix.Syscall(unix.SYS_IOCTL, a1, a2, a3)
52-
if errno != 0 {
53-
return errno
54-
}
55-
return nil
56-
}
57-
5850
var sockaddrCtlSize uintptr = 32
5951

6052
const (
@@ -194,10 +186,10 @@ func (t *tun) Activate() error {
194186
unix.SOCK_DGRAM,
195187
unix.IPPROTO_IP,
196188
)
197-
198189
if err != nil {
199190
return err
200191
}
192+
defer unix.Close(s)
201193

202194
fd := uintptr(s)
203195

overlay/tun_freebsd.go

Lines changed: 118 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,44 @@
44
package overlay
55

66
import (
7+
"bytes"
8+
"errors"
79
"fmt"
810
"io"
11+
"io/fs"
912
"net"
1013
"os"
1114
"os/exec"
12-
"regexp"
1315
"strconv"
14-
"strings"
16+
"syscall"
17+
"unsafe"
1518

1619
"github.com/sirupsen/logrus"
1720
"github.com/slackhq/nebula/cidr"
1821
"github.com/slackhq/nebula/iputil"
1922
)
2023

21-
var deviceNameRE = regexp.MustCompile(`^tun[0-9]+$`)
24+
const (
25+
// FIODGNAME is defined in sys/sys/filio.h on FreeBSD
26+
// For 32-bit systems, use FIODGNAME_32 (not defined in this file: 0x80086678)
27+
FIODGNAME = 0x80106678
28+
)
29+
30+
type fiodgnameArg struct {
31+
length int32
32+
pad [4]byte
33+
buf unsafe.Pointer
34+
}
35+
36+
type ifreqRename struct {
37+
Name [16]byte
38+
Data uintptr
39+
}
40+
41+
type ifreqDestroy struct {
42+
Name [16]byte
43+
pad [16]byte
44+
}
2245

2346
type tun struct {
2447
Device string
@@ -33,8 +56,23 @@ type tun struct {
3356

3457
func (t *tun) Close() error {
3558
if t.ReadWriteCloser != nil {
36-
return t.ReadWriteCloser.Close()
59+
if err := t.ReadWriteCloser.Close(); err != nil {
60+
return err
61+
}
62+
63+
s, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_DGRAM, syscall.IPPROTO_IP)
64+
if err != nil {
65+
return err
66+
}
67+
defer syscall.Close(s)
68+
69+
ifreq := ifreqDestroy{Name: t.deviceBytes()}
70+
71+
// Destroy the interface
72+
err = ioctl(uintptr(s), syscall.SIOCIFDESTROY, uintptr(unsafe.Pointer(&ifreq)))
73+
return err
3774
}
75+
3876
return nil
3977
}
4078

@@ -43,34 +81,87 @@ func newTunFromFd(_ *logrus.Logger, _ int, _ *net.IPNet, _ int, _ []Route, _ int
4381
}
4482

4583
func newTun(l *logrus.Logger, deviceName string, cidr *net.IPNet, defaultMTU int, routes []Route, _ int, _ bool, _ bool) (*tun, error) {
46-
routeTree, err := makeRouteTree(l, routes, false)
84+
// Try to open existing tun device
85+
var file *os.File
86+
var err error
87+
if deviceName != "" {
88+
file, err = os.OpenFile("/dev/"+deviceName, os.O_RDWR, 0)
89+
}
90+
if errors.Is(err, fs.ErrNotExist) || deviceName == "" {
91+
// If the device doesn't already exist, request a new one and rename it
92+
file, err = os.OpenFile("/dev/tun", os.O_RDWR, 0)
93+
}
94+
if err != nil {
95+
return nil, err
96+
}
97+
98+
rawConn, err := file.SyscallConn()
4799
if err != nil {
100+
return nil, fmt.Errorf("SyscallConn: %v", err)
101+
}
102+
103+
var name [16]byte
104+
var ctrlErr error
105+
rawConn.Control(func(fd uintptr) {
106+
// Read the name of the interface
107+
arg := fiodgnameArg{length: 16, buf: unsafe.Pointer(&name)}
108+
ctrlErr = ioctl(fd, FIODGNAME, uintptr(unsafe.Pointer(&arg)))
109+
})
110+
if ctrlErr != nil {
48111
return nil, err
49112
}
50113

51-
if strings.HasPrefix(deviceName, "/dev/") {
52-
deviceName = strings.TrimPrefix(deviceName, "/dev/")
114+
ifName := string(bytes.TrimRight(name[:], "\x00"))
115+
if deviceName == "" {
116+
deviceName = ifName
53117
}
54-
if !deviceNameRE.MatchString(deviceName) {
55-
return nil, fmt.Errorf("tun.dev must match `tun[0-9]+`")
118+
119+
// If the name doesn't match the desired interface name, rename it now
120+
if ifName != deviceName {
121+
s, err := syscall.Socket(
122+
syscall.AF_INET,
123+
syscall.SOCK_DGRAM,
124+
syscall.IPPROTO_IP,
125+
)
126+
if err != nil {
127+
return nil, err
128+
}
129+
defer syscall.Close(s)
130+
131+
fd := uintptr(s)
132+
133+
var fromName [16]byte
134+
var toName [16]byte
135+
copy(fromName[:], ifName)
136+
copy(toName[:], deviceName)
137+
138+
ifrr := ifreqRename{
139+
Name: fromName,
140+
Data: uintptr(unsafe.Pointer(&toName)),
141+
}
142+
143+
// Set the device name
144+
ioctl(fd, syscall.SIOCSIFNAME, uintptr(unsafe.Pointer(&ifrr)))
56145
}
146+
147+
routeTree, err := makeRouteTree(l, routes, false)
148+
if err != nil {
149+
return nil, err
150+
}
151+
57152
return &tun{
58-
Device: deviceName,
59-
cidr: cidr,
60-
MTU: defaultMTU,
61-
Routes: routes,
62-
routeTree: routeTree,
63-
l: l,
153+
ReadWriteCloser: file,
154+
Device: deviceName,
155+
cidr: cidr,
156+
MTU: defaultMTU,
157+
Routes: routes,
158+
routeTree: routeTree,
159+
l: l,
64160
}, nil
65161
}
66162

67163
func (t *tun) Activate() error {
68164
var err error
69-
t.ReadWriteCloser, err = os.OpenFile("/dev/"+t.Device, os.O_RDWR, 0)
70-
if err != nil {
71-
return fmt.Errorf("activate failed: %v", err)
72-
}
73-
74165
// TODO use syscalls instead of exec.Command
75166
t.l.Debug("command: ifconfig", t.Device, t.cidr.String(), t.cidr.IP.String())
76167
if err = exec.Command("/sbin/ifconfig", t.Device, t.cidr.String(), t.cidr.IP.String()).Run(); err != nil {
@@ -120,3 +211,10 @@ func (t *tun) Name() string {
120211
func (t *tun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
121212
return nil, fmt.Errorf("TODO: multiqueue not implemented for freebsd")
122213
}
214+
215+
func (t *tun) deviceBytes() (o [16]byte) {
216+
for i, c := range t.Device {
217+
o[i] = byte(c)
218+
}
219+
return
220+
}

overlay/tun_linux.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,6 @@ type ifReq struct {
4343
pad [8]byte
4444
}
4545

46-
func ioctl(a1, a2, a3 uintptr) error {
47-
_, _, errno := unix.Syscall(unix.SYS_IOCTL, a1, a2, a3)
48-
if errno != 0 {
49-
return errno
50-
}
51-
return nil
52-
}
53-
5446
type ifreqAddr struct {
5547
Name [16]byte
5648
Addr unix.RawSockaddrInet4

overlay/tun_notwin.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//go:build !windows
2+
// +build !windows
3+
4+
package overlay
5+
6+
import "syscall"
7+
8+
func ioctl(a1, a2, a3 uintptr) error {
9+
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, a1, a2, a3)
10+
if errno != 0 {
11+
return errno
12+
}
13+
return nil
14+
}

0 commit comments

Comments
 (0)