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"
|
2022-08-25 11:34:19 +00:00
|
|
|
"golang.org/x/net/icmp"
|
2022-08-18 15:03:47 +00:00
|
|
|
|
|
|
|
"github.com/cloudflare/cloudflared/packet"
|
|
|
|
)
|
|
|
|
|
2022-08-25 11:34:19 +00:00
|
|
|
const (
|
|
|
|
defaultCloseAfterIdle = time.Second * 15
|
|
|
|
mtu = 1500
|
2022-08-29 17:49:07 +00:00
|
|
|
icmpTimeoutMs = 1000
|
2022-08-25 11:34:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
errFlowInactive = fmt.Errorf("flow is inactive")
|
|
|
|
errPacketNil = fmt.Errorf("packet is nil")
|
|
|
|
)
|
|
|
|
|
2022-08-18 15:03:47 +00:00
|
|
|
// ICMPProxy sends ICMP messages and listens for their responses
|
|
|
|
type ICMPProxy interface {
|
2022-08-25 11:34:19 +00:00
|
|
|
// Serve starts listening for responses to the requests until context is done
|
|
|
|
Serve(ctx context.Context) error
|
2022-08-18 15:03:47 +00:00
|
|
|
// Request sends an ICMP message
|
|
|
|
Request(pk *packet.ICMP, responder packet.FlowResponder) error
|
|
|
|
}
|
|
|
|
|
2022-08-25 11:34:19 +00:00
|
|
|
func NewICMPProxy(listenIP netip.Addr, logger *zerolog.Logger) (ICMPProxy, error) {
|
2022-08-22 16:41:51 +00:00
|
|
|
return newICMPProxy(listenIP, logger)
|
2022-08-18 15:03:47 +00:00
|
|
|
}
|
2022-08-25 11:34:19 +00:00
|
|
|
|
|
|
|
// Opens a non-privileged ICMP socket on Linux and Darwin
|
|
|
|
func newICMPConn(listenIP netip.Addr) (*icmp.PacketConn, error) {
|
|
|
|
network := "udp6"
|
|
|
|
if listenIP.Is4() {
|
|
|
|
network = "udp4"
|
|
|
|
}
|
|
|
|
return icmp.ListenPacket(network, listenIP.String())
|
|
|
|
}
|
2022-08-29 17:49:07 +00:00
|
|
|
|
|
|
|
func getICMPEcho(pk *packet.ICMP) (*icmp.Echo, error) {
|
|
|
|
echo, ok := pk.Message.Body.(*icmp.Echo)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("expect ICMP echo, got %s", pk.Type)
|
|
|
|
}
|
|
|
|
return echo, nil
|
|
|
|
}
|