From 90300178a6c24d4dad3c49f8ce636aeafbddfa2a Mon Sep 17 00:00:00 2001 From: tqbf Date: Wed, 2 Feb 2022 13:38:49 -0600 Subject: [PATCH] Handle whole ICMP headers Theoretically, this change would allow us to send ICMP replies from these sockets. In practice, netstack filters anything but EchoRequest, so this change mostly just adds annoyance right now. But in the future, netstack might fix this, and this API would then work for more than one use case. Signed-off-by: Thomas Ptacek --- tun/netstack/tun.go | 24 +++--------------------- 1 file changed, 3 insertions(+), 21 deletions(-) diff --git a/tun/netstack/tun.go b/tun/netstack/tun.go index 058aca5..9d4b467 100644 --- a/tun/netstack/tun.go +++ b/tun/netstack/tun.go @@ -398,19 +398,7 @@ func (pc *PingConn) WriteTo(p []byte, addr net.Addr) (n int, err error) { return 0, fmt.Errorf("ping write: mismatched protocols") } - var buf buffer.View - if ia.addr.Is4() { - buf = buffer.NewView(header.ICMPv4MinimumSize + len(p)) - copy(buf[header.ICMPv4MinimumSize:], p) - icmp := header.ICMPv4(buf) - icmp.SetType(header.ICMPv4Echo) - } else if ia.addr.Is6() { - buf = buffer.NewView(header.ICMPv6MinimumSize + len(p)) - copy(buf[header.ICMPv6MinimumSize:], p) - icmp := header.ICMPv6(buf) - icmp.SetType(header.ICMPv6EchoRequest) - } - + buf := buffer.NewViewFromBytes(p) rdr := buf.Reader() rfa, _ := convertToFullAddr(netip.AddrPortFrom(ia.addr, 0)) // won't block, no deadlines @@ -462,12 +450,7 @@ func (pc *PingConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) { } } - min := header.ICMPv6MinimumSize - if pc.laddr.addr.Is4() { - min = header.ICMPv4MinimumSize - } - reply := make([]byte, min+len(p)) - w := tcpip.SliceWriter(reply) + w := tcpip.SliceWriter(p) res, tcpipErr := pc.ep.Read(&w, tcpip.ReadOptions{ NeedRemoteAddr: true, @@ -477,8 +460,7 @@ func (pc *PingConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) { } addr = PingAddr{netip.AddrFromSlice([]byte(res.RemoteAddr.Addr))} - copy(p, reply[min:res.Count]) - return res.Count - min, addr, nil + return res.Count, addr, nil } func (pc *PingConn) Read(p []byte) (n int, err error) {