Fix: revert to classic TUN creation (#254)

This commit is contained in:
xjasonlyu
2023-05-30 16:29:01 +08:00
parent 46c04db29f
commit 44ad654d72
4 changed files with 32 additions and 14 deletions

View File

@@ -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)
}

View File

@@ -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)
}

View File

@@ -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)
}

View File

@@ -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)
}