Skip to content

Commit fe99046

Browse files
committed
netstack/icmp: m clone pkt
1 parent a715280 commit fe99046

1 file changed

Lines changed: 14 additions & 24 deletions

File tree

intra/netstack/icmpecho.go

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@ func (r *icmpResponder) respond(pkt *stack.PacketBuffer) (handled bool) {
6262
if !r.ok() {
6363
return
6464
}
65-
defer pkt.DecRef()
66-
67-
return r.handle(pkt.NICID, pkt.IncRef())
65+
return r.handle(pkt.NICID, pkt.Clone())
6866
}
6967

7068
// handle returns true if the packet is ICMP and is handled (or dropped) by the
7169
// bypass path.
7270
func (r *icmpResponder) handle(nic tcpip.NICID, pkt *stack.PacketBuffer) (handled bool) {
71+
defer pkt.DecRef()
72+
7373
if !r.ok() {
7474
return
7575
}
@@ -81,25 +81,23 @@ func (r *icmpResponder) handle(nic tcpip.NICID, pkt *stack.PacketBuffer) (handle
8181
return
8282
}
8383

84-
b := pkt.ToBuffer()
84+
v := pkt.ToView()
85+
b := v.ToSlice()
8586
h := hdlEcho.Load()
86-
var w core.ByteWriter
87-
defer w.Close()
88-
// lightweight determine if b is ICMP echo request
89-
// flatten is expensive, so we avoid it if possible
90-
n, err := b.ReadToWriter(&w, expectedICMPPacketSize)
91-
92-
if settings.Debug {
93-
logeif(err)("icmp: responder: read to writer (sz: %d / %d / %d); h? %t / fwd? %t, err? %v",
94-
n, w.Len(), inSize, h != nil, useIcmpForwarder, err)
87+
n := len(b)
88+
89+
notok := n <= 0 || h == nil
90+
if settings.Debug || notok {
91+
logwv(notok)("icmp: responder: read to writer (sz: %d / %d / %d); h? %t / fwd? %t",
92+
n, v.Size(), inSize, h != nil, useIcmpForwarder)
9593
}
96-
if err != nil || n == 0 || h == nil || w.Len() == 0 {
94+
if notok {
9795
return
9896
}
9997

100-
truncated := inSize > w.Len()
98+
truncated := false
10199
parsed := wire.Pool.Get()
102-
parsed.DecodeTrunc(w.Copy(), truncated)
100+
parsed.DecodeTrunc(b, truncated)
103101

104102
// Only echo requests are handled; other ICMP packets are dropped to avoid
105103
// feeding them back into netstack.
@@ -125,14 +123,6 @@ func (r *icmpResponder) handle(nic tcpip.NICID, pkt *stack.PacketBuffer) (handle
125123
return
126124
}
127125

128-
if truncated {
129-
// There is more data beyond the minimum ICMP echo request.
130-
// Reconstruct the full packet.
131-
w.Reset()
132-
b.ReadToWriter(&w, int64(inSize))
133-
parsed.Decode(w.Copy())
134-
}
135-
136126
if !parsed.IsEchoRequest() {
137127
if settings.Debug {
138128
log.VV("icmp: responder: not echo request ipv%d (trunc? %t); %s => %s; h: %s; %x",

0 commit comments

Comments
 (0)