Skip to content

Commit 4127b99

Browse files
committed
netstack/icmp: m debug logs
1 parent 8dec82d commit 4127b99

2 files changed

Lines changed: 37 additions & 17 deletions

File tree

intra/core/wire/parsed.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,9 +401,9 @@ func (q *Parsed) IP6Header() IP6Header {
401401
func (q *Parsed) ICMPHeaderString() string {
402402
switch q.IPProto {
403403
case ICMPv4:
404-
return fmt.Sprintf("%v", q.ICMP4Header())
404+
return q.ICMP4Header().String()
405405
case ICMPv6:
406-
return fmt.Sprintf("%v", q.ICMP6Header())
406+
return q.ICMP6Header().String()
407407
}
408408
return "ICMP" + string(q.IPVersion) + "{???}"
409409
}
@@ -416,6 +416,14 @@ func (q *Parsed) ICMP4Header() ICMP4Header {
416416
}
417417
}
418418

419+
func (h ICMP4Header) String() string {
420+
return fmt.Sprintf("%v", h)
421+
}
422+
423+
func (h ICMP6Header) String() string {
424+
return fmt.Sprintf("%v", h)
425+
}
426+
419427
func (q *Parsed) ICMP6Header() ICMP6Header {
420428
return ICMP6Header{
421429
IP6Header: q.IP6Header(),

intra/netstack/icmpecho.go

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -145,46 +145,58 @@ func (r *icmpResponder) process(h GICMPHandler, pkt *wire.Parsed, src, dst netip
145145

146146
icmpMsg := pkt.Transport()
147147
payload, truncated := pkt.Payload()
148-
if truncated || len(icmpMsg) <= 0 {
149-
log.E("icmp: responder: truncated? %t or missing? %t ICMPv%d; %s => %s; id: %d; h: %s; sz: %d",
148+
notok := truncated || len(icmpMsg) <= 0
149+
150+
if notok || settings.Debug {
151+
logwv(notok)("icmp: responder: truncated? %t or missing? %t ICMPv%d; %s => %s; id: %d; h: %s; sz: %d",
150152
truncated, len(icmpMsg) <= 0, pkt.IPVersion, src, dst, pkt.EchoIDSeq(), pkt.ICMPHeaderString(), len(payload))
153+
}
154+
155+
if notok {
151156
return
152157
}
153158

154159
pinged := h.Ping(icmpMsg, src, dst)
155160

156-
resp, proto, err := r.echoReply(pkt, payload, pinged)
161+
resp, proto, tag, err := r.echoReply(pkt, payload, pinged)
162+
notok = err != nil || len(resp) == 0
163+
164+
if notok || settings.Debug {
165+
logwv(notok)("icmp: responder: reply %s <= %s (sz: %d / id: %d); ping? %t; res: %s; err? %v",
166+
src, dst, len(resp), pkt.EchoIDSeq(), pinged, tag, err)
167+
}
168+
157169
if err != nil || len(resp) == 0 {
158-
log.W("icmp: responder: reply %s <= %s (sz: %d / id: %d); ping? %t; err? %v",
159-
src, dst, len(resp), pkt.EchoIDSeq(), pinged, err)
160170
return
161171
}
162172

163173
r.inject(proto, resp)
164174
}
165175

166-
func (r *icmpResponder) echoReply(pkt *wire.Parsed, d []byte, ok bool) ([]byte, tcpip.NetworkProtocolNumber, error) {
176+
func (r *icmpResponder) echoReply(pkt *wire.Parsed, d []byte, ok bool) ([]byte, tcpip.NetworkProtocolNumber, string, error) {
167177
// github.com/tailscale/tailscale/blob/7de1b0b33082cc/wgengine/netstack/netstack.go#L1201-L1212
168178
switch pkt.IPVersion {
169179
case 4:
170180
icmpHdr := pkt.ICMP4Header()
171-
icmpHdr.ToResponse()
181+
(&icmpHdr).ToResponse()
172182
if !ok {
173-
icmpHdr.Type = wire.ICMP4Unreachable
174-
icmpHdr.Code = wire.ICMP4HostUnreachable
183+
(&icmpHdr).Type = wire.ICMP4Unreachable
184+
(&icmpHdr).Code = wire.ICMP4HostUnreachable
175185
}
176-
return wire.Generate(&icmpHdr, d), header.IPv4ProtocolNumber, nil
186+
tag := icmpHdr.String()
187+
return wire.Generate(&icmpHdr, d), header.IPv4ProtocolNumber, tag, nil
177188
case 6:
178189
icmpHdr := pkt.ICMP6Header()
179-
icmpHdr.ToResponse()
190+
(&icmpHdr).ToResponse()
180191
if !ok {
181-
icmpHdr.Type = wire.ICMP6Unreachable
182-
icmpHdr.Code = wire.ICMP6NoRoute
192+
(&icmpHdr).Type = wire.ICMP6Unreachable
193+
(&icmpHdr).Code = wire.ICMP6NoRoute
183194
}
195+
tag := icmpHdr.String()
184196
// github.com/tailscale/tailscale/blob/7de1b0b33082cc/wgengine/userspace.go#L577
185-
return wire.Generate(&icmpHdr, d), header.IPv6ProtocolNumber, nil
197+
return wire.Generate(&icmpHdr, d), header.IPv6ProtocolNumber, tag, nil
186198
default:
187-
return nil, 0, fmt.Errorf("unsupported ip version: %d", pkt.IPVersion)
199+
return nil, 0, "<nil>", fmt.Errorf("unsupported ip version: %d", pkt.IPVersion)
188200
}
189201
}
190202

0 commit comments

Comments
 (0)