Skip to content

Commit

Permalink
Fix: revert to classic TUN creation (#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
xjasonlyu committed May 30, 2023
1 parent 46c04db commit 44ad654
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 14 deletions.
2 changes: 1 addition & 1 deletion core/device/tun/tun_wireguard.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func Open(name string, mtu uint32) (_ device.Device, err error) {
forcedMTU = int(t.mtu)
}

nt, err := tun.CreateTUN(t.name, forcedMTU)
nt, err := createTUN(t.name, forcedMTU)
if err != nil {
return nil, fmt.Errorf("create tun: %w", err)
}
Expand Down
28 changes: 15 additions & 13 deletions core/device/tun/tun_wireguard_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,24 @@
package tun

import (
"unsafe"
"fmt"
"os"

"golang.zx2c4.com/wireguard/tun"
gun "gvisor.dev/gvisor/pkg/tcpip/link/tun"
)

const (
virtioNetHdrLen = int(unsafe.Sizeof(virtioNetHdr{}))
offset = virtioNetHdrLen + 0 /* NO_PI */
defaultMTU = 1500
offset = 0 /* IFF_NO_PI */
defaultMTU = 1500
)

// virtioNetHdr is defined in the kernel in include/uapi/linux/virtio_net.h. The
// kernel symbol is virtio_net_hdr.
type virtioNetHdr struct {
flags uint8
gsoType uint8
hdrLen uint16
gsoSize uint16
csumStart uint16
csumOffset uint16
func createTUN(name string, mtu int) (tun.Device, error) {
nfd, err := gun.Open(name)
if err != nil {
return nil, fmt.Errorf("create tun: %w", err)
}

fd := os.NewFile(uintptr(nfd), "/dev/net/tun")
return tun.CreateTUNFromFile(fd, mtu)
}
8 changes: 8 additions & 0 deletions core/device/tun/tun_wireguard_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@

package tun

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

const (
offset = 4 /* 4 bytes TUN_PI */
defaultMTU = 1500
)

func createTUN(name string, mtu int) (tun.Device, error) {
return tun.CreateTUN(name, mtu)
}
8 changes: 8 additions & 0 deletions core/device/tun/tun_wireguard_windows.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
package tun

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

const (
offset = 0
defaultMTU = 0 /* auto */
)

func createTUN(name string, mtu int) (tun.Device, error) {
return tun.CreateTUN(name, mtu)
}

0 comments on commit 44ad654

Please sign in to comment.