Compare commits

...

2 commits

Author SHA1 Message Date
snow flurry 5657c3c1e8 tun_netbsd: update to follow tun_openbsd changes
Most of tun_netbsd is from tun_openbsd with some small changes, so we
should follow the changes made in tun_openbsd.
2022-02-18 19:15:16 -08:00
snow flurry ff49dc80a3 tun_netbsd: support unpatched kernels for IFHEAD
This previously assumed a user's kernel was patched a la
http://gnats.netbsd.org/cgi-bin/query-pr-single.pl?number=55463, making
it effectively useless for most people. Since we know IFHEAD will always
be one of 0 (false) or something that isn't 0 (true), we can generalize
things a bit more.

Suggestion provided by artemis
https://artemis.sh/2022/02/16/tailscale-on-netbsd-proof-of-concept.html
2022-02-18 19:14:23 -08:00

View file

@ -6,6 +6,7 @@
package tun
import (
"errors"
"fmt"
"io/ioutil"
"net"
@ -130,7 +131,7 @@ func CreateTUN(name string, mtu int) (Device, error) {
} else {
for ifIndex = 0; ifIndex < 256; ifIndex += 1 {
tunfile, err = os.OpenFile(fmt.Sprintf("/dev/tun%d", ifIndex), unix.O_RDWR, 0)
if err == nil || !errorIsEBUSY(err) {
if err == nil || !errors.Is(err, syscall.EBUSY) {
break
}
}
@ -164,7 +165,7 @@ func CreateTUN(name string, mtu int) (Device, error) {
uintptr(unsafe.Pointer(&ifheadmode)),
)
if errno != 0 || ifheadmode != 1 {
if errno != 0 || ifheadmode == 0 {
tunfile.Close()
return nil, fmt.Errorf("Unable to validate IFHEAD mode: %v (ifheadmode = %d)", errno, ifheadmode)
}
@ -249,19 +250,15 @@ func (tun *NativeTun) Read(buff []byte, offset int) (int, error) {
return 0, err
default:
buff := buff[offset-4:]
// buff := buff[offset:]
n, err := tun.tunFile.Read(buff[:])
if n < 4 {
return 0, err
}
return n - 4, err
// return n, err
}
}
func (tun *NativeTun) Write(buff []byte, offset int) (int, error) {
// reserve space for header
buff = buff[offset-4:]
@ -289,15 +286,17 @@ func (tun *NativeTun) Flush() error {
}
func (tun *NativeTun) Close() error {
var err2 error
err1 := tun.tunFile.Close()
if tun.routeSocket != -1 {
unix.Shutdown(tun.routeSocket, unix.SHUT_RDWR)
err2 = unix.Close(tun.routeSocket)
tun.routeSocket = -1
} else if tun.events != nil {
close(tun.events)
}
var err1, err2 error
tun.closeOnce.Do(func () {
err1 = tun.tunFile.Close()
if tun.routeSocket != -1 {
unix.Shutdown(tun.routeSocket, unix.SHUT_RDWR)
err2 = unix.Close(tun.routeSocket)
tun.routeSocket = -1
} else if tun.events != nil {
close(tun.events)
}
})
if err1 != nil {
return err1
}