Skip to content

Commit

Permalink
hotfix: fix tun bug (#216)
Browse files Browse the repository at this point in the history
Co-authored-by: wencaiwulue <895703375@qq.com>
  • Loading branch information
wencaiwulue authored Apr 12, 2024
1 parent 45491f1 commit 8dbb80b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 26 deletions.
28 changes: 10 additions & 18 deletions pkg/core/tunhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ import (
)

const (
MaxSize = 1000
MaxThread = 10
MaxConn = 1
MaxSize = 1000
)

type tunHandler struct {
Expand Down Expand Up @@ -171,8 +169,7 @@ func (h *tunHandler) printRoute(ctx context.Context) {
}

type Device struct {
tun net.Conn
thread int
tun net.Conn

tunInboundRaw chan *DataElem
tunInbound chan *DataElem
Expand All @@ -195,9 +192,11 @@ func (d *Device) readFromTun() {
}
return
}
d.tunInboundRaw <- &DataElem{
data: b[:],
length: n,
if n != 0 {
d.tunInboundRaw <- &DataElem{
data: b[:],
length: n,
}
}
}
}
Expand Down Expand Up @@ -353,9 +352,7 @@ func genICMPPacketIPv6(src net.IP, dst net.IP) ([]byte, error) {

func (d *Device) Start(ctx context.Context) {
go d.readFromTun()
for i := 0; i < d.thread; i++ {
go d.parseIPHeader()
}
go d.parseIPHeader()
go d.tunInboundHandler(d.tunInbound, d.tunOutbound)
go d.writeToTun()
go heartbeats(d.tun, d.tunInbound)
Expand All @@ -378,7 +375,6 @@ func (h *tunHandler) HandleServer(ctx context.Context, tun net.Conn) {

device := &Device{
tun: tun,
thread: MaxThread,
tunInboundRaw: make(chan *DataElem, MaxSize),
tunInbound: make(chan *DataElem, MaxSize),
tunOutbound: make(chan *DataElem, MaxSize),
Expand Down Expand Up @@ -435,8 +431,7 @@ type udpElem struct {
}

type Peer struct {
conn net.PacketConn
thread int
conn net.PacketConn

connInbound chan *udpElem
parsedConnInfo chan *udpElem
Expand Down Expand Up @@ -591,9 +586,7 @@ func (p *Peer) routeTUN() {
func (p *Peer) Start() {
go p.readFromConn()
go p.readFromTCPConn()
for i := 0; i < p.thread; i++ {
go p.parseHeader()
}
go p.parseHeader()
go p.routePeer()
go p.routeTUN()
}
Expand All @@ -605,7 +598,6 @@ func (p *Peer) Close() {
func transportTun(ctx context.Context, tunInbound <-chan *DataElem, tunOutbound chan<- *DataElem, packetConn net.PacketConn, nat *NAT, connNAT *sync.Map) error {
p := &Peer{
conn: packetConn,
thread: MaxThread,
connInbound: make(chan *udpElem, MaxSize),
parsedConnInfo: make(chan *udpElem, MaxSize),
tunInbound: tunInbound,
Expand Down
24 changes: 18 additions & 6 deletions pkg/tun/tun.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tun
import (
"errors"
"net"
"os"
"time"

"github.com/containernetworking/cni/pkg/types"
Expand Down Expand Up @@ -87,18 +88,29 @@ func (c *tunConn) Read(b []byte) (n int, err error) {
defer config.LPool.Put(bytes[:])

var num int
sizes := []int{1}
sizes := make([]int, 1)
num, err = c.ifce.Read([][]byte{bytes[:]}, sizes, offset)
if err != nil || num == 0 {
if err != nil {
if errors.Is(err, tun.ErrTooManySegments) {
log.Errorf("Dropped some packets from multi-segment read: %v", err)
return 0, nil
}
if !errors.Is(err, os.ErrClosed) {
log.Errorf("Failed to read packet from TUN device: %v", err)
return 0, nil
}
return 0, err
}
if num == 0 {
return 0, nil
}
var size = sizes[0]
if size == 0 {
return 0, errors.New("tun packet is zero")
}
if size > device.MaxSegmentSize-device.MessageTransportHeaderSize {
return 0, errors.New("tun packet is too large")
return 0, nil
}
//if size > device.MaxSegmentSize-device.MessageTransportHeaderSize {
// return 0, errors.New("tun packet is too large")
//}
return copy(b, bytes[offset:offset+size]), nil
}

Expand Down
16 changes: 14 additions & 2 deletions pkg/tun/tun_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"net"
"net/netip"
"os"
"reflect"
"time"

Expand Down Expand Up @@ -153,11 +154,22 @@ func (c *winTunConn) Close() error {
}

func (c *winTunConn) Read(b []byte) (int, error) {
sizes := []int{1}
sizes := make([]int, 1)
num, err := c.ifce.Read([][]byte{b}, sizes, 0)
if err != nil || num == 0 {
if err != nil {
if errors.Is(err, wireguardtun.ErrTooManySegments) {
log.Errorf("Dropped some packets from multi-segment read: %v", err)
return 0, nil
}
if !errors.Is(err, os.ErrClosed) {
log.Errorf("Failed to read packet from TUN device: %v", err)
return 0, nil
}
return 0, err
}
if num == 0 {
return 0, nil
}
return sizes[0], nil
}

Expand Down

0 comments on commit 8dbb80b

Please sign in to comment.