From 7d8e321a65ee1dc2659b41ad13370f5a530c498e Mon Sep 17 00:00:00 2001 From: Mikael Magnusson Date: Tue, 17 Mar 2020 20:03:03 +0100 Subject: WIP SendIcmp --- pkg/tcpip/network/ipv4/ipv4.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/pkg/tcpip/network/ipv4/ipv4.go b/pkg/tcpip/network/ipv4/ipv4.go index 9953f83cd..c0af9a2d8 100644 --- a/pkg/tcpip/network/ipv4/ipv4.go +++ b/pkg/tcpip/network/ipv4/ipv4.go @@ -408,6 +408,42 @@ func (e *endpoint) HandlePacket(r *stack.Route, pkt tcpip.PacketBuffer) { e.dispatcher.DeliverTransportPacket(r, p, pkt) } +func (e *endpoint) SendIcmp(pkt tcpip.PacketBuffer, icmpv4type header.ICMPv4Type, icmpv4code byte, nextHopMtu uint16) *tcpip.Error { + headerView := pkt.Header.View() + h := header.IPv4(headerView) + vv := headerView.ToVectorisedView() + vv.Append(pkt.Data.Clone(nil)) + fmt.Println("SendIcmp ", vv.Size()) + hdr := buffer.NewPrependable(int(e.MaxHeaderLength()) + header.ICMPv4MinimumSize) + icmp := header.ICMPv4(hdr.Prepend(header.ICMPv4MinimumSize)) + copy(icmp, h) + icmp.SetType(icmpv4type) + icmp.SetCode(icmpv4code) + if nextHopMtu != 0 { + icmp.SetMTU(nextHopMtu) + } + icmp.SetChecksum(0) + icmp.SetChecksum(^header.Checksum(icmp, header.ChecksumVV(vv, 0))) + //sent := stats.ICMP.V4PacketsSent + r, err := e.stack.FindRoute(0, "", h.SourceAddress(), ProtocolNumber, false /* multicastLoop */) + if err != nil { + fmt.Println("FindRoute error ", err) + return tcpip.ErrInvalidOptionValue + } + ttl := r.DefaultTTL() + if err := r.WritePacket(nil /* gso */, stack.NetworkHeaderParams{Protocol: header.ICMPv4ProtocolNumber, TTL: ttl, TOS: stack.DefaultTOS}, tcpip.PacketBuffer{ + Header: hdr, + Data: vv, +// TransportHeader: buffer.View(icmp), + }); err != nil { + //sent.Dropped.Increment() + fmt.Println("WritePacket error ", err) + return tcpip.ErrInvalidOptionValue + } + + return nil +} + func (*endpoint) UpdateTTL(pkt tcpip.PacketBuffer, n uint8) *tcpip.Error { headerView := pkt.Header.View() h := header.IPv4(headerView) -- cgit v1.2.3