■ ■ ■ ■ ■ ■
core/device/tun/tun_wireguard_linux.go
| skipped 2 lines |
3 | 3 | | package tun |
4 | 4 | | |
5 | 5 | | import ( |
6 | | - | "unsafe" |
| 6 | + | "fmt" |
| 7 | + | "os" |
| 8 | + | |
| 9 | + | "golang.zx2c4.com/wireguard/tun" |
| 10 | + | gun "gvisor.dev/gvisor/pkg/tcpip/link/tun" |
7 | 11 | | ) |
8 | 12 | | |
9 | 13 | | const ( |
10 | | - | virtioNetHdrLen = int(unsafe.Sizeof(virtioNetHdr{})) |
11 | | - | offset = virtioNetHdrLen + 0 /* NO_PI */ |
12 | | - | defaultMTU = 1500 |
| 14 | + | offset = 0 /* IFF_NO_PI */ |
| 15 | + | defaultMTU = 1500 |
13 | 16 | | ) |
14 | 17 | | |
15 | | - | // virtioNetHdr is defined in the kernel in include/uapi/linux/virtio_net.h. The |
16 | | - | // kernel symbol is virtio_net_hdr. |
17 | | - | type virtioNetHdr struct { |
18 | | - | flags uint8 |
19 | | - | gsoType uint8 |
20 | | - | hdrLen uint16 |
21 | | - | gsoSize uint16 |
22 | | - | csumStart uint16 |
23 | | - | csumOffset uint16 |
| 18 | + | func createTUN(name string, mtu int) (tun.Device, error) { |
| 19 | + | nfd, err := gun.Open(name) |
| 20 | + | if err != nil { |
| 21 | + | return nil, fmt.Errorf("create tun: %w", err) |
| 22 | + | } |
| 23 | + | |
| 24 | + | fd := os.NewFile(uintptr(nfd), "/dev/net/tun") |
| 25 | + | return tun.CreateTUNFromFile(fd, mtu) |
24 | 26 | | } |
25 | 27 | | |