Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion usrwg/tun_darwin.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
package usrwg

import "golang.zx2c4.com/wireguard/tun"
import (
"os"

"golang.zx2c4.com/wireguard/tun"
)

func createTUN(mtu int) (tun.Device, error) {
return tun.CreateTUN("utun", mtu)
}

func createTUNFromFile(file *os.File, mtu int) (tun.Device, error) {
return tun.CreateTUNFromFile(file, mtu)
}

func createTUNFromFD(fd uintptr, mtu int) (tun.Device, error) {
return createTUNFromFile(os.NewFile(fd, "tun"), mtu)
}
15 changes: 14 additions & 1 deletion usrwg/tun_linux.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
package usrwg

import "golang.zx2c4.com/wireguard/tun"
import (
"os"

"golang.zx2c4.com/wireguard/tun"
)

func createTUN(mtu int) (tun.Device, error) {
return tun.CreateTUN("ts0", mtu)
}

func createTUNFromFile(file *os.File, mtu int) (tun.Device, error) {
return tun.CreateTUNFromFile(file, mtu)
}

func createTUNFromFD(fd uintptr, _ int) (tun.Device, error) {
dev, _, err := tun.CreateUnmonitoredTUNFromFD(int(fd))
return dev, err
}
8 changes: 8 additions & 0 deletions usrwg/tun_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ func createTUN(mtu int) (tun.Device, error) {
return tun.CreateTUN("toversok", mtu)
}

func createTUNFromFile(file *os.File, mtu int) (tun.Device, error) {
return nil, errors.New("not implemented on windows")
}

func createTUNFromFD(_ uintptr, _ int) (tun.Device, error) {
return nil, errors.New("not implemented on windows")
}

func init() {
tun.WintunTunnelType = "ToverSok"
guid, err := windows.GUIDFromString("{37217669-42da-4657-a55b-13375d328250}")
Expand Down
40 changes: 37 additions & 3 deletions usrwg/wgusp.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log/slog"
"net"
"net/netip"
"os"
"slices"
"syscall"

Expand All @@ -27,6 +28,26 @@ func NewUsrWGHost() *UserSpaceWireGuardHost {

type UserSpaceWireGuardHost struct {
running *UserSpaceWireGuardController
tunFile *os.File
tunFD uintptr
}

func (u *UserSpaceWireGuardHost) SetTUNFile(f *os.File) {
u.tunFile = f
if f == nil {
u.tunFD = 0
} else {
u.tunFD = f.Fd()
}
}

func (u *UserSpaceWireGuardHost) SetTUNFD(fd uintptr) {
// TODO: this has the side-effect on linux to use the "unmonitored" creation step,
// instead of a monitored creation step, needs to be made explicit
u.tunFD = fd
if u.tunFile != nil {
u.tunFD = 0
}
}

func (u *UserSpaceWireGuardHost) Reset() error {
Expand All @@ -47,9 +68,7 @@ func (u *UserSpaceWireGuardHost) Controller(privateKey key.NodePrivate, addr4, a
}
}

// TODO set this to 1392 per https://docs.eduvpn.org/server/v3/wireguard.html
// and make adjustable by environment variable
tunDev, err := createTUN(1280)
tunDev, err := u.createTUN()
if err != nil {
return nil, fmt.Errorf("failed to create TUN device: %w", err)
}
Expand Down Expand Up @@ -111,6 +130,21 @@ func (u *UserSpaceWireGuardHost) Controller(privateKey key.NodePrivate, addr4, a
return usrwgc, nil
}

// TODO set this to 1392 per https://docs.eduvpn.org/server/v3/wireguard.html
// and make adjustable by environment variable

const tunMtu = 1280

func (u *UserSpaceWireGuardHost) createTUN() (tun.Device, error) {
if u.tunFile != nil {
return createTUNFromFile(u.tunFile, tunMtu)
} else if u.tunFD != 0 {
return createTUNFromFD(u.tunFD, tunMtu)
}

return createTUN(tunMtu)
}

type UserSpaceWireGuardController struct {
wgDev *device.Device
bind *ToverSokBind
Expand Down