Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

For ICMP, only use RawConn for the Don't Fagment case #329

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 20 additions & 21 deletions prober/icmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ func getICMPSequence() uint16 {
func ProbeICMP(ctx context.Context, target string, module config.Module, registry *prometheus.Registry, logger log.Logger) (success bool) {
var (
socket net.PacketConn
socket6 *ipv6.PacketConn
requestType icmp.Type
replyType icmp.Type
)
Expand All @@ -73,30 +72,39 @@ func ProbeICMP(ctx context.Context, target string, module config.Module, registr
requestType = ipv6.ICMPTypeEchoRequest
replyType = ipv6.ICMPTypeEchoReply

icmpConn, err := icmp.ListenPacket("ip6:ipv6-icmp", "::")
if srcIP == nil {
srcIP = net.ParseIP("::")
}
icmpConn, err := icmp.ListenPacket("ip6:ipv6-icmp", srcIP.String())
if err != nil {
level.Error(logger).Log("msg", "Error listening to socket", "err", err)
return
}

socket = icmpConn
socket6 = icmpConn.IPv6PacketConn()
} else {
requestType = ipv4.ICMPTypeEcho
replyType = ipv4.ICMPTypeEchoReply

s, err := net.ListenPacket("ip4:icmp", "0.0.0.0")
if srcIP == nil {
srcIP = net.ParseIP("0.0.0.0")
}
icmpConn, err := net.ListenPacket("ip4:icmp", srcIP.String())
if err != nil {
level.Error(logger).Log("msg", "Error listening to socket", "err", err)
return
}

rc, err := ipv4.NewRawConn(s)
if err != nil {
level.Error(logger).Log("msg", "Error creating raw connection", "err", err)
return
if module.ICMP.DontFragment {
rc, err := ipv4.NewRawConn(icmpConn)
if err != nil {
level.Error(logger).Log("msg", "Error creating raw connection", "err", err)
return
}
socket = &v4Conn{c: rc, df: true}
} else {
socket = icmpConn
}
socket = &v4Conn{c: rc, df: module.ICMP.DontFragment, src: srcIP}
}

defer socket.Close()
Expand Down Expand Up @@ -127,18 +135,9 @@ func ProbeICMP(ctx context.Context, target string, module config.Module, registr
return
}
level.Info(logger).Log("msg", "Writing out packet")
if socket6 != nil && srcIP != nil {
// Also set source address for IPv6.
cm := &ipv6.ControlMessage{Src: srcIP}
if _, err = socket6.WriteTo(wb, cm, ip); err != nil {
level.Error(logger).Log("msg", "Error writing to IPv6 socket", "err", err)
return
}
} else {
if _, err = socket.WriteTo(wb, ip); err != nil {
level.Warn(logger).Log("msg", "Error writing to socket", "err", err)
return
}
if _, err = socket.WriteTo(wb, ip); err != nil {
level.Warn(logger).Log("msg", "Error writing to socket", "err", err)
return
}

// Reply should be the same except for the message type.
Expand Down