Fix gvisor icmp destination

This commit is contained in:
世界 2025-08-23 16:21:48 +08:00
parent 8dbb51cfb7
commit f46791bc0d
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
6 changed files with 24 additions and 27 deletions

View file

@ -15,6 +15,7 @@ import (
"github.com/sagernet/gvisor/pkg/tcpip/network/ipv4"
"github.com/sagernet/gvisor/pkg/tcpip/network/ipv6"
"github.com/sagernet/gvisor/pkg/tcpip/stack"
"github.com/sagernet/sing/common/buf"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
)
@ -62,8 +63,7 @@ func (f *ICMPForwarder) HandlePacket(id stack.TransportEndpointID, pkt *stack.Pa
}
if action != nil {
// TODO: handle error
pkt.IncRef()
_ = action.WritePacketBuffer(pkt)
_ = icmpWritePacketBuffer(action, pkt)
return true
}
icmpHdr.SetType(header.ICMPv4EchoReply)
@ -120,7 +120,7 @@ func (f *ICMPForwarder) HandlePacket(id stack.TransportEndpointID, pkt *stack.Pa
if action != nil {
// TODO: handle error
pkt.IncRef()
_ = action.WritePacketBuffer(pkt)
_ = icmpWritePacketBuffer(action, pkt)
return true
}
icmpHdr.SetType(header.ICMPv6EchoReply)
@ -207,3 +207,9 @@ func (w *ICMPBackWriter) WritePacket(p []byte) error {
}
return nil
}
func icmpWritePacketBuffer(action DirectRouteDestination, packetBuffer *stack.PacketBuffer) error {
packetSlice := packetBuffer.TransportHeader().Slice()
packetSlice = append(packetSlice, packetBuffer.Data().AsRange().ToSlice()...)
return action.WritePacket(buf.As(packetSlice).ToOwned())
}