Skip to content

Commit

Permalink
Try to fix #4
Browse files Browse the repository at this point in the history
... by catching TCP RST packets in WritePackets and sending them
during the next WritePackets call where no RST packet is being sent

Signed-off-by: Vasyl Gello <[email protected]>
  • Loading branch information
basilgello committed Jul 18, 2024
1 parent 302e563 commit 46d1092
Showing 1 changed file with 41 additions and 10 deletions.
51 changes: 41 additions & 10 deletions src/netstack/yggdrasil.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package netstack

import (
"container/list"
"log"
"net"

Expand All @@ -12,6 +13,7 @@ import (
"gvisor.dev/gvisor/pkg/tcpip/header"
"gvisor.dev/gvisor/pkg/tcpip/network/ipv6"
"gvisor.dev/gvisor/pkg/tcpip/stack"
"gvisor.dev/gvisor/pkg/tcpip/transport/tcp"
)

type YggdrasilNIC struct {
Expand All @@ -20,15 +22,17 @@ type YggdrasilNIC struct {
dispatcher stack.NetworkDispatcher
readBuf []byte
writeBuf []byte
rstPackets *list.List
}

func (s *YggdrasilNetstack) NewYggdrasilNIC(ygg *core.Core) tcpip.Error {
rwc := ipv6rwc.NewReadWriteCloser(ygg)
mtu := rwc.MTU()
nic := &YggdrasilNIC{
ipv6rwc: rwc,
readBuf: make([]byte, mtu),
writeBuf: make([]byte, mtu),
ipv6rwc: rwc,
readBuf: make([]byte, mtu),
writeBuf: make([]byte, mtu),
rstPackets: list.New(),
}
if err := s.stack.CreateNIC(1, nic); err != nil {
return err
Expand Down Expand Up @@ -93,24 +97,51 @@ func (*YggdrasilNIC) LinkAddress() tcpip.LinkAddress { return "" }

func (*YggdrasilNIC) Wait() {}

func (e *YggdrasilNIC) writePacket(
pkt *stack.PacketBuffer,
) tcpip.Error {
vv := pkt.ToView()
n, err := vv.Read(e.writeBuf)
if err != nil {
return &tcpip.ErrAborted{}
}
_, err = e.ipv6rwc.Write(e.writeBuf[:n])
if err != nil {
return &tcpip.ErrAborted{}
}
return nil
}

func (e *YggdrasilNIC) WritePackets(
list stack.PacketBufferList,
) (int, tcpip.Error) {
var i int = 0
var err tcpip.Error = nil
var rstCaught = false
for i, pkt := range list.AsSlice() {
vv := pkt.ToView()
n, err := vv.Read(e.writeBuf)
if err != nil {
log.Println(err)
return i - 1, &tcpip.ErrAborted{}
if pkt.Network().TransportProtocol() == tcp.ProtocolNumber {
tcpHeader := header.TCP(pkt.TransportHeader().Slice())
if (tcpHeader.Flags() & header.TCPFlagRst) == header.TCPFlagRst {
e.rstPackets.PushFront(pkt)
rstCaught = true
continue
}
}
_, err = e.ipv6rwc.Write(e.writeBuf[:n])
err = e.writePacket(pkt)
if err != nil {
log.Println(err)
return i - 1, &tcpip.ErrAborted{}
return i - 1, err
}
}

if !rstCaught {
for rstPkt := e.rstPackets.Front(); rstPkt != nil; rstPkt = rstPkt.Next() {
_ = e.writePacket(rstPkt.Value.(*stack.PacketBuffer))
}

_ = e.rstPackets.Init()
}

return i, nil
}

Expand Down

0 comments on commit 46d1092

Please sign in to comment.