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) forcedMTU = int(t.mtu)
} }
nt, err := tun.CreateTUN(t.name, forcedMTU) nt, err := createTUN(t.name, forcedMTU)
if err != nil { if err != nil {
return nil, fmt.Errorf("create tun: %w", err) return nil, fmt.Errorf("create tun: %w", err)
} }

View File

@@ -3,22 +3,24 @@
package tun package tun
import ( import (
"unsafe" "fmt"
"os"
"golang.zx2c4.com/wireguard/tun"
gun "gvisor.dev/gvisor/pkg/tcpip/link/tun"
) )
const ( const (
virtioNetHdrLen = int(unsafe.Sizeof(virtioNetHdr{})) offset = 0 /* IFF_NO_PI */
offset = virtioNetHdrLen + 0 /* NO_PI */ defaultMTU = 1500
defaultMTU = 1500
) )
// virtioNetHdr is defined in the kernel in include/uapi/linux/virtio_net.h. The func createTUN(name string, mtu int) (tun.Device, error) {
// kernel symbol is virtio_net_hdr. nfd, err := gun.Open(name)
type virtioNetHdr struct { if err != nil {
flags uint8 return nil, fmt.Errorf("create tun: %w", err)
gsoType uint8 }
hdrLen uint16
gsoSize uint16 fd := os.NewFile(uintptr(nfd), "/dev/net/tun")
csumStart uint16 return tun.CreateTUNFromFile(fd, mtu)
csumOffset uint16
} }

View File

@@ -2,7 +2,15 @@
package tun package tun
import (
"golang.zx2c4.com/wireguard/tun"
)
const ( const (
offset = 4 /* 4 bytes TUN_PI */ offset = 4 /* 4 bytes TUN_PI */
defaultMTU = 1500 defaultMTU = 1500
) )
func createTUN(name string, mtu int) (tun.Device, error) {
return tun.CreateTUN(name, mtu)
}

View File

@@ -1,6 +1,14 @@
package tun package tun
import (
"golang.zx2c4.com/wireguard/tun"
)
const ( const (
offset = 0 offset = 0
defaultMTU = 0 /* auto */ defaultMTU = 0 /* auto */
) )
func createTUN(name string, mtu int) (tun.Device, error) {
return tun.CreateTUN(name, mtu)
}