Skip to content

Commit b5ab74a

Browse files
committed
netstack/icmp: m refactor clone
1 parent 148cc04 commit b5ab74a

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

intra/netstack/icmpecho.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,12 @@ func (r *icmpResponder) respond(pkt *stack.PacketBuffer) (handled bool) {
6363
if !r.ok() {
6464
return
6565
}
66-
return r.handle(pkt.NICID, pkt.Clone())
66+
return r.handle(pkt.NICID, pkt)
6767
}
6868

6969
// handle returns true if the packet is ICMP and is handled (or dropped) by the
7070
// bypass path.
7171
func (r *icmpResponder) handle(nic tcpip.NICID, pkt *stack.PacketBuffer) (handled bool) {
72-
defer pkt.DecRef()
73-
7472
if !r.ok() {
7573
return
7674
}
@@ -82,7 +80,10 @@ func (r *icmpResponder) handle(nic tcpip.NICID, pkt *stack.PacketBuffer) (handle
8280
return
8381
}
8482

85-
v := pkt.ToView()
83+
c := pkt.Clone()
84+
defer c.DecRef()
85+
86+
v := c.ToView()
8687
b := v.ToSlice()
8788
h := hdlEcho.Load()
8889
n := len(b)
@@ -96,9 +97,8 @@ func (r *icmpResponder) handle(nic tcpip.NICID, pkt *stack.PacketBuffer) (handle
9697
return
9798
}
9899

99-
truncated := false
100100
parsed := wire.Pool.Get()
101-
parsed.DecodeTrunc(b, truncated)
101+
parsed.Decode(b)
102102

103103
// Only echo requests are handled; other ICMP packets are dropped to avoid
104104
// feeding them back into netstack.
@@ -116,8 +116,8 @@ func (r *icmpResponder) handle(nic tcpip.NICID, pkt *stack.PacketBuffer) (handle
116116
dst := parsed.Dst
117117
has := parsed.HasTransportData()
118118

119-
logwv(!has)("icmp: responder: request ipv%d; %s => %s; h: %s; trunc? %t, ok? %t",
120-
parsed.IPVersion, src, dst, parsed.ICMPHeaderString(), truncated, has)
119+
logwv(!has)("icmp: responder: request ipv%d; %s => %s; h: %s; ok? %t",
120+
parsed.IPVersion, src, dst, parsed.ICMPHeaderString(), has)
121121

122122
if !has {
123123
wire.Pool.Put(parsed)
@@ -126,8 +126,8 @@ func (r *icmpResponder) handle(nic tcpip.NICID, pkt *stack.PacketBuffer) (handle
126126

127127
if !parsed.IsEchoRequest() {
128128
if settings.Debug {
129-
log.VV("icmp: responder: not echo request ipv%d (trunc? %t); %s => %s; h: %s; %x",
130-
parsed.IPVersion, truncated, src, dst, parsed.ICMPHeaderString(), parsed.Buffer())
129+
log.VV("icmp: responder: not echo request ipv%d; %s => %s; h: %s; %x",
130+
parsed.IPVersion, src, dst, parsed.ICMPHeaderString(), parsed.Buffer())
131131
}
132132
wire.Pool.Put(parsed)
133133
return
@@ -147,7 +147,7 @@ func (r *icmpResponder) handle(nic tcpip.NICID, pkt *stack.PacketBuffer) (handle
147147
}
148148

149149
func (r *icmpResponder) forward(h *icmpForwarder, pkt *stack.PacketBuffer, src, dst netip.AddrPort) bool {
150-
pkt.IncRef()
150+
pkt = pkt.Clone()
151151
defer pkt.DecRef()
152152

153153
// local is dst / remote is src; see: netstack/icmp/icmp.go:func (h *icmpForwarder) reply4

0 commit comments

Comments
 (0)