Skip to content

Commit feda367

Browse files
committed
Fix gvisor icmp write
1 parent a256dca commit feda367

File tree

6 files changed

+16
-9
lines changed

6 files changed

+16
-9
lines changed

ping/destination.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,15 @@ func (d *Destination) loopRead() {
6969
}
7070
}
7171

72-
func (d *Destination) WritePacket(packet *buf.Buffer) error {
72+
func (d *Destination) WriteIP(packet *buf.Buffer) error {
7373
return d.conn.WriteIP(packet)
7474
}
7575

76+
func (d *Destination) WriteICMP(packet *buf.Buffer, sourceAddress netip.Addr) error {
77+
d.conn.SetSourceAddress(sourceAddress)
78+
return d.conn.WriteICMP(packet)
79+
}
80+
7681
func (d *Destination) Close() error {
7782
return d.conn.Close()
7883
}

ping/ping.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,8 @@ func (c *Conn) WriteICMP(buffer *buf.Buffer) error {
194194
return common.Error(c.conn.Write(buffer.Bytes()))
195195
}
196196

197-
func (c *Conn) SetLocalAddr(addr netip.Addr) {
198-
c.source.Store(addr)
197+
func (c *Conn) SetSourceAddress(address netip.Addr) {
198+
c.source.Store(address)
199199
}
200200

201201
func (c *Conn) SetReadDeadline(t time.Time) error {

ping/ping_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func testPingIPv4ReadIP(t *testing.T, privileged bool, addr string) {
8585
err = conn.WriteICMP(buf.As(request))
8686
require.NoError(t, err)
8787

88-
conn.SetLocalAddr(netip.MustParseAddr("127.0.0.1"))
88+
conn.SetSourceAddress(netip.MustParseAddr("127.0.0.1"))
8989
require.NoError(t, conn.SetReadDeadline(time.Now().Add(3*time.Second)))
9090

9191
response := buf.NewPacket()
@@ -147,7 +147,7 @@ func testPingIPv6ReadIP(t *testing.T, privileged bool, addr string) {
147147
err = conn.WriteICMP(buf.As(request))
148148
require.NoError(t, err)
149149

150-
conn.SetLocalAddr(netip.MustParseAddr("::1"))
150+
conn.SetSourceAddress(netip.MustParseAddr("::1"))
151151
require.NoError(t, conn.SetReadDeadline(time.Now().Add(3*time.Second)))
152152

153153
response := buf.NewPacket()

route_nat.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import (
1010
)
1111

1212
type DirectRouteDestination interface {
13-
WritePacket(packet *buf.Buffer) error
13+
WriteIP(packet *buf.Buffer) error
14+
WriteICMP(packet *buf.Buffer, source netip.Addr) error
1415
Close() error
1516
}
1617

stack_gvisor_icmp.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/sagernet/gvisor/pkg/tcpip/network/ipv4"
1717
"github.com/sagernet/gvisor/pkg/tcpip/network/ipv6"
1818
"github.com/sagernet/gvisor/pkg/tcpip/stack"
19+
"github.com/sagernet/sing/common"
1920
"github.com/sagernet/sing/common/buf"
2021
M "github.com/sagernet/sing/common/metadata"
2122
N "github.com/sagernet/sing/common/network"
@@ -227,5 +228,5 @@ func (w *ICMPBackWriter) WritePacket(p []byte) error {
227228
func icmpWritePacketBuffer(action DirectRouteDestination, packetBuffer *stack.PacketBuffer) error {
228229
packetSlice := packetBuffer.TransportHeader().Slice()
229230
packetSlice = append(packetSlice, packetBuffer.Data().AsRange().ToSlice()...)
230-
return action.WritePacket(buf.As(packetSlice).ToOwned())
231+
return action.WriteICMP(buf.As(packetSlice).ToOwned(), M.AddrFromIP(common.Ptr(packetBuffer.Network().SourceAddress()).AsSlice()))
231232
}

stack_system.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,7 @@ func (s *System) processIPv4ICMP(ipHdr header.IPv4, icmpHdr header.ICMPv4) (bool
670670
return false, nil
671671
}
672672
if action != nil {
673-
return false, action.WritePacket(buf.As(ipHdr).ToOwned())
673+
return false, action.WriteIP(buf.As(ipHdr).ToOwned())
674674
}
675675
}
676676
icmpHdr.SetType(header.ICMPv4EchoReply)
@@ -741,7 +741,7 @@ func (s *System) processIPv6ICMP(ipHdr header.IPv6, icmpHdr header.ICMPv6) (bool
741741
return false, nil
742742
}
743743
if action != nil {
744-
return false, action.WritePacket(buf.As(ipHdr).ToOwned())
744+
return false, action.WriteIP(buf.As(ipHdr).ToOwned())
745745
}
746746
}
747747
icmpHdr.SetType(header.ICMPv6EchoReply)

0 commit comments

Comments
 (0)