1
0
mirror of https://git.zx2c4.com/wireguard-go synced 2024-11-15 01:05:15 +01:00

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 <thomas@sockpuppet.org>
This commit is contained in:
tqbf 2022-02-02 13:38:49 -06:00
parent f7acdab244
commit 90300178a6

View 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") return 0, fmt.Errorf("ping write: mismatched protocols")
} }
var buf buffer.View buf := buffer.NewViewFromBytes(p)
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)
}
rdr := buf.Reader() rdr := buf.Reader()
rfa, _ := convertToFullAddr(netip.AddrPortFrom(ia.addr, 0)) rfa, _ := convertToFullAddr(netip.AddrPortFrom(ia.addr, 0))
// won't block, no deadlines // 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 w := tcpip.SliceWriter(p)
if pc.laddr.addr.Is4() {
min = header.ICMPv4MinimumSize
}
reply := make([]byte, min+len(p))
w := tcpip.SliceWriter(reply)
res, tcpipErr := pc.ep.Read(&w, tcpip.ReadOptions{ res, tcpipErr := pc.ep.Read(&w, tcpip.ReadOptions{
NeedRemoteAddr: true, 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))} addr = PingAddr{netip.AddrFromSlice([]byte(res.RemoteAddr.Addr))}
copy(p, reply[min:res.Count]) return res.Count, addr, nil
return res.Count - min, addr, nil
} }
func (pc *PingConn) Read(p []byte) (n int, err error) { func (pc *PingConn) Read(p []byte) (n int, err error) {