2022-08-18 15:03:47 +00:00
|
|
|
package ingress
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-08-25 11:34:19 +00:00
|
|
|
"fmt"
|
|
|
|
"net/netip"
|
|
|
|
"time"
|
2022-08-18 15:03:47 +00:00
|
|
|
|
|
|
|
"github.com/rs/zerolog"
|
2024-01-15 16:49:17 +00:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
2022-08-25 11:34:19 +00:00
|
|
|
"golang.org/x/net/icmp"
|
2022-09-06 12:46:21 +00:00
|
|
|
"golang.org/x/net/ipv4"
|
|
|
|
"golang.org/x/net/ipv6"
|
2022-08-18 15:03:47 +00:00
|
|
|
|
|
|
|
"github.com/cloudflare/cloudflared/packet"
|
2024-11-27 20:46:08 +00:00
|
|
|
"github.com/cloudflare/cloudflared/tracing"
|
2022-08-18 15:03:47 +00:00
|
|
|
)
|
|
|
|
|
2022-08-25 11:34:19 +00:00
|
|
|
const (
|
2024-01-15 16:49:17 +00:00
|
|
|
mtu = 1500
|
2022-09-02 16:29:50 +00:00
|
|
|
// icmpRequestTimeoutMs controls how long to wait for a reply
|
|
|
|
icmpRequestTimeoutMs = 1000
|
2022-08-25 11:34:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-09-02 16:29:50 +00:00
|
|
|
errPacketNil = fmt.Errorf("packet is nil")
|
2022-08-25 11:34:19 +00:00
|
|
|
)
|
|
|
|
|
2024-11-27 20:46:08 +00:00
|
|
|
// ICMPRouterServer is a parent interface over-top of ICMPRouter that allows for the operation of the proxy origin listeners.
|
|
|
|
type ICMPRouterServer interface {
|
|
|
|
ICMPRouter
|
|
|
|
// Serve runs the ICMPRouter proxy origin listeners for any of the IPv4 or IPv6 interfaces configured.
|
|
|
|
Serve(ctx context.Context) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// ICMPRouter manages out-going ICMP requests towards the origin.
|
|
|
|
type ICMPRouter interface {
|
|
|
|
// Request will send an ICMP packet towards the origin with an ICMPResponder to attach to the ICMP flow for the
|
|
|
|
// response to utilize.
|
|
|
|
Request(ctx context.Context, pk *packet.ICMP, responder ICMPResponder) error
|
|
|
|
// ConvertToTTLExceeded will take an ICMP packet and create a ICMP TTL Exceeded response origininating from the
|
|
|
|
// ICMPRouter's IP interface.
|
|
|
|
ConvertToTTLExceeded(pk *packet.ICMP, rawPacket packet.RawPacket) *packet.ICMP
|
|
|
|
}
|
|
|
|
|
|
|
|
// ICMPResponder manages how to handle incoming ICMP messages coming from the origin to the edge.
|
|
|
|
type ICMPResponder interface {
|
|
|
|
ConnectionIndex() uint8
|
|
|
|
ReturnPacket(pk *packet.ICMP) error
|
|
|
|
AddTraceContext(tracedCtx *tracing.TracedContext, serializedIdentity []byte)
|
|
|
|
RequestSpan(ctx context.Context, pk *packet.ICMP) (context.Context, trace.Span)
|
|
|
|
ReplySpan(ctx context.Context, logger *zerolog.Logger) (context.Context, trace.Span)
|
|
|
|
ExportSpan()
|
|
|
|
}
|
|
|
|
|
2022-09-06 12:46:21 +00:00
|
|
|
type icmpRouter struct {
|
2022-09-13 13:00:54 +00:00
|
|
|
ipv4Proxy *icmpProxy
|
2024-11-27 20:46:08 +00:00
|
|
|
ipv4Src netip.Addr
|
2022-09-13 13:00:54 +00:00
|
|
|
ipv6Proxy *icmpProxy
|
2024-11-27 20:46:08 +00:00
|
|
|
ipv6Src netip.Addr
|
2022-09-06 12:46:21 +00:00
|
|
|
}
|
|
|
|
|
2022-09-13 13:00:54 +00:00
|
|
|
// NewICMPRouter doesn't return an error if either ipv4 proxy or ipv6 proxy can be created. The machine might only
|
2024-01-15 16:49:17 +00:00
|
|
|
// support one of them.
|
|
|
|
// funnelIdleTimeout controls how long to wait to close a funnel without send/return
|
2024-11-27 20:46:08 +00:00
|
|
|
func NewICMPRouter(ipv4Addr, ipv6Addr netip.Addr, logger *zerolog.Logger, funnelIdleTimeout time.Duration) (ICMPRouterServer, error) {
|
|
|
|
ipv4Proxy, ipv4Err := newICMPProxy(ipv4Addr, logger, funnelIdleTimeout)
|
|
|
|
ipv6Proxy, ipv6Err := newICMPProxy(ipv6Addr, logger, funnelIdleTimeout)
|
2022-09-06 12:46:21 +00:00
|
|
|
if ipv4Err != nil && ipv6Err != nil {
|
2022-10-13 10:01:25 +00:00
|
|
|
err := fmt.Errorf("cannot create ICMPv4 proxy: %v nor ICMPv6 proxy: %v", ipv4Err, ipv6Err)
|
|
|
|
logger.Debug().Err(err).Msg("ICMP proxy feature is disabled")
|
|
|
|
return nil, err
|
2022-09-06 12:46:21 +00:00
|
|
|
}
|
|
|
|
if ipv4Err != nil {
|
2022-09-13 13:00:54 +00:00
|
|
|
logger.Debug().Err(ipv4Err).Msg("failed to create ICMPv4 proxy, only ICMPv6 proxy is created")
|
2022-09-06 12:46:21 +00:00
|
|
|
ipv4Proxy = nil
|
|
|
|
}
|
|
|
|
if ipv6Err != nil {
|
2022-09-13 13:00:54 +00:00
|
|
|
logger.Debug().Err(ipv6Err).Msg("failed to create ICMPv6 proxy, only ICMPv4 proxy is created")
|
2022-09-06 12:46:21 +00:00
|
|
|
ipv6Proxy = nil
|
|
|
|
}
|
|
|
|
return &icmpRouter{
|
|
|
|
ipv4Proxy: ipv4Proxy,
|
2024-11-27 20:46:08 +00:00
|
|
|
ipv4Src: ipv4Addr,
|
2022-09-06 12:46:21 +00:00
|
|
|
ipv6Proxy: ipv6Proxy,
|
2024-11-27 20:46:08 +00:00
|
|
|
ipv6Src: ipv6Addr,
|
2022-09-06 12:46:21 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ir *icmpRouter) Serve(ctx context.Context) error {
|
|
|
|
if ir.ipv4Proxy != nil && ir.ipv6Proxy != nil {
|
|
|
|
errC := make(chan error, 2)
|
|
|
|
go func() {
|
|
|
|
errC <- ir.ipv4Proxy.Serve(ctx)
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
errC <- ir.ipv6Proxy.Serve(ctx)
|
|
|
|
}()
|
|
|
|
return <-errC
|
|
|
|
}
|
|
|
|
if ir.ipv4Proxy != nil {
|
|
|
|
return ir.ipv4Proxy.Serve(ctx)
|
|
|
|
}
|
|
|
|
if ir.ipv6Proxy != nil {
|
|
|
|
return ir.ipv6Proxy.Serve(ctx)
|
|
|
|
}
|
|
|
|
return fmt.Errorf("ICMPv4 proxy and ICMPv6 proxy are both nil")
|
|
|
|
}
|
|
|
|
|
2024-11-27 20:46:08 +00:00
|
|
|
func (ir *icmpRouter) Request(ctx context.Context, pk *packet.ICMP, responder ICMPResponder) error {
|
2022-10-13 10:01:25 +00:00
|
|
|
if pk == nil {
|
|
|
|
return errPacketNil
|
|
|
|
}
|
2022-09-06 12:46:21 +00:00
|
|
|
if pk.Dst.Is4() {
|
|
|
|
if ir.ipv4Proxy != nil {
|
2022-10-13 10:01:25 +00:00
|
|
|
return ir.ipv4Proxy.Request(ctx, pk, responder)
|
2022-09-06 12:46:21 +00:00
|
|
|
}
|
|
|
|
return fmt.Errorf("ICMPv4 proxy was not instantiated")
|
|
|
|
}
|
|
|
|
if ir.ipv6Proxy != nil {
|
2022-10-13 10:01:25 +00:00
|
|
|
return ir.ipv6Proxy.Request(ctx, pk, responder)
|
2022-09-06 12:46:21 +00:00
|
|
|
}
|
|
|
|
return fmt.Errorf("ICMPv6 proxy was not instantiated")
|
2022-08-18 15:03:47 +00:00
|
|
|
}
|
2022-08-25 11:34:19 +00:00
|
|
|
|
2024-11-27 20:46:08 +00:00
|
|
|
func (ir *icmpRouter) ConvertToTTLExceeded(pk *packet.ICMP, rawPacket packet.RawPacket) *packet.ICMP {
|
|
|
|
var srcIP netip.Addr
|
|
|
|
if pk.Dst.Is4() {
|
|
|
|
srcIP = ir.ipv4Src
|
|
|
|
} else {
|
|
|
|
srcIP = ir.ipv6Src
|
|
|
|
}
|
|
|
|
return packet.NewICMPTTLExceedPacket(pk.IP, rawPacket, srcIP)
|
|
|
|
}
|
|
|
|
|
2022-09-02 16:29:50 +00:00
|
|
|
func getICMPEcho(msg *icmp.Message) (*icmp.Echo, error) {
|
|
|
|
echo, ok := msg.Body.(*icmp.Echo)
|
2022-08-29 17:49:07 +00:00
|
|
|
if !ok {
|
2022-09-02 16:29:50 +00:00
|
|
|
return nil, fmt.Errorf("expect ICMP echo, got %s", msg.Type)
|
2022-08-29 17:49:07 +00:00
|
|
|
}
|
|
|
|
return echo, nil
|
|
|
|
}
|
2022-09-06 12:46:21 +00:00
|
|
|
|
|
|
|
func isEchoReply(msg *icmp.Message) bool {
|
|
|
|
return msg.Type == ipv4.ICMPTypeEchoReply || msg.Type == ipv6.ICMPTypeEchoReply
|
|
|
|
}
|
2024-01-15 16:49:17 +00:00
|
|
|
|
|
|
|
func observeICMPRequest(logger *zerolog.Logger, span trace.Span, src string, dst string, echoID int, seq int) {
|
2024-05-30 21:23:10 +00:00
|
|
|
incrementICMPRequest()
|
2024-01-15 16:49:17 +00:00
|
|
|
logger.Debug().
|
|
|
|
Str("src", src).
|
|
|
|
Str("dst", dst).
|
|
|
|
Int("originalEchoID", echoID).
|
|
|
|
Int("originalEchoSeq", seq).
|
|
|
|
Msg("Received ICMP request")
|
|
|
|
span.SetAttributes(
|
|
|
|
attribute.Int("originalEchoID", echoID),
|
|
|
|
attribute.Int("seq", seq),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func observeICMPReply(logger *zerolog.Logger, span trace.Span, dst string, echoID int, seq int) {
|
2024-05-30 21:23:10 +00:00
|
|
|
incrementICMPReply()
|
2024-01-15 16:49:17 +00:00
|
|
|
logger.Debug().Str("dst", dst).Int("echoID", echoID).Int("seq", seq).Msg("Sent ICMP reply to edge")
|
|
|
|
span.SetAttributes(
|
|
|
|
attribute.String("dst", dst),
|
|
|
|
attribute.Int("echoID", echoID),
|
|
|
|
attribute.Int("seq", seq),
|
|
|
|
)
|
|
|
|
}
|