]> git.ipfire.org Git - thirdparty/wireguard-go.git/commitdiff
Handle whole ICMP headers tp/icmp-fixes
authortqbf <thomas@fly.io>
Wed, 2 Feb 2022 19:38:49 +0000 (13:38 -0600)
committertqbf <thomas@fly.io>
Wed, 2 Feb 2022 19:38:49 +0000 (13:38 -0600)
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 <thomas@sockpuppet.org>
tun/netstack/tun.go

index 058aca50f464870c0aab66989f74b34efc59d14f..9d4b467c959559efe8ba2afe4e7dd113f88a7fa8 100644 (file)
@@ -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) {