2022-09-02 16:29:50 +00:00
|
|
|
//go:build darwin || linux
|
|
|
|
|
|
|
|
package ingress
|
|
|
|
|
|
|
|
// This file extracts logic shared by Linux and Darwin implementation if ICMPProxy.
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/netip"
|
2024-01-15 16:49:17 +00:00
|
|
|
"sync/atomic"
|
2022-09-02 16:29:50 +00:00
|
|
|
|
|
|
|
"github.com/google/gopacket/layers"
|
2022-11-08 23:12:33 +00:00
|
|
|
"github.com/rs/zerolog"
|
2022-09-02 16:29:50 +00:00
|
|
|
"golang.org/x/net/icmp"
|
|
|
|
|
|
|
|
"github.com/cloudflare/cloudflared/packet"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Opens a non-privileged ICMP socket on Linux and Darwin
|
2024-11-27 20:46:08 +00:00
|
|
|
func newICMPConn(listenIP netip.Addr) (*icmp.PacketConn, error) {
|
2022-09-02 16:29:50 +00:00
|
|
|
if listenIP.Is4() {
|
2022-09-20 10:39:51 +00:00
|
|
|
return icmp.ListenPacket("udp4", listenIP.String())
|
2022-09-02 16:29:50 +00:00
|
|
|
}
|
2024-11-27 20:46:08 +00:00
|
|
|
return icmp.ListenPacket("udp6", listenIP.String())
|
2022-09-02 16:29:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func netipAddr(addr net.Addr) (netip.Addr, bool) {
|
|
|
|
udpAddr, ok := addr.(*net.UDPAddr)
|
|
|
|
if !ok {
|
|
|
|
return netip.Addr{}, false
|
|
|
|
}
|
2024-11-27 20:46:08 +00:00
|
|
|
|
|
|
|
return udpAddr.AddrPort().Addr(), true
|
2022-09-02 16:29:50 +00:00
|
|
|
}
|
|
|
|
|
2022-09-09 15:48:42 +00:00
|
|
|
type flow3Tuple struct {
|
|
|
|
srcIP netip.Addr
|
|
|
|
dstIP netip.Addr
|
|
|
|
originalEchoID int
|
2022-09-02 16:29:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// icmpEchoFlow implements the packet.Funnel interface.
|
|
|
|
type icmpEchoFlow struct {
|
2022-10-13 10:01:25 +00:00
|
|
|
*packet.ActivityTracker
|
|
|
|
closeCallback func() error
|
2024-01-15 16:49:17 +00:00
|
|
|
closed *atomic.Bool
|
2022-10-13 10:01:25 +00:00
|
|
|
src netip.Addr
|
|
|
|
originConn *icmp.PacketConn
|
2024-11-27 20:46:08 +00:00
|
|
|
responder ICMPResponder
|
2022-09-02 16:29:50 +00:00
|
|
|
assignedEchoID int
|
|
|
|
originalEchoID int
|
|
|
|
}
|
|
|
|
|
2024-11-27 20:46:08 +00:00
|
|
|
func newICMPEchoFlow(src netip.Addr, closeCallback func() error, originConn *icmp.PacketConn, responder ICMPResponder, assignedEchoID, originalEchoID int) *icmpEchoFlow {
|
2022-09-02 16:29:50 +00:00
|
|
|
return &icmpEchoFlow{
|
2022-10-13 10:01:25 +00:00
|
|
|
ActivityTracker: packet.NewActivityTracker(),
|
|
|
|
closeCallback: closeCallback,
|
2024-01-15 16:49:17 +00:00
|
|
|
closed: &atomic.Bool{},
|
2022-10-13 10:01:25 +00:00
|
|
|
src: src,
|
|
|
|
originConn: originConn,
|
|
|
|
responder: responder,
|
2022-09-02 16:29:50 +00:00
|
|
|
assignedEchoID: assignedEchoID,
|
|
|
|
originalEchoID: originalEchoID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-13 10:01:25 +00:00
|
|
|
func (ief *icmpEchoFlow) Equal(other packet.Funnel) bool {
|
|
|
|
otherICMPFlow, ok := other.(*icmpEchoFlow)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if otherICMPFlow.src != ief.src {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if otherICMPFlow.originalEchoID != ief.originalEchoID {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if otherICMPFlow.assignedEchoID != ief.assignedEchoID {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ief *icmpEchoFlow) Close() error {
|
2024-01-15 16:49:17 +00:00
|
|
|
ief.closed.Store(true)
|
2022-10-13 10:01:25 +00:00
|
|
|
return ief.closeCallback()
|
|
|
|
}
|
|
|
|
|
2024-01-15 16:49:17 +00:00
|
|
|
func (ief *icmpEchoFlow) IsClosed() bool {
|
|
|
|
return ief.closed.Load()
|
|
|
|
}
|
|
|
|
|
2022-09-02 16:29:50 +00:00
|
|
|
// sendToDst rewrites the echo ID to the one assigned to this flow
|
|
|
|
func (ief *icmpEchoFlow) sendToDst(dst netip.Addr, msg *icmp.Message) error {
|
2022-10-13 10:01:25 +00:00
|
|
|
ief.UpdateLastActive()
|
2022-09-02 16:29:50 +00:00
|
|
|
originalEcho, err := getICMPEcho(msg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
sendMsg := icmp.Message{
|
|
|
|
Type: msg.Type,
|
|
|
|
Code: msg.Code,
|
|
|
|
Body: &icmp.Echo{
|
|
|
|
ID: ief.assignedEchoID,
|
|
|
|
Seq: originalEcho.Seq,
|
|
|
|
Data: originalEcho.Data,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
// For IPv4, the pseudoHeader is not used because the checksum is always calculated
|
|
|
|
var pseudoHeader []byte = nil
|
|
|
|
serializedPacket, err := sendMsg.Marshal(pseudoHeader)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-13 10:01:25 +00:00
|
|
|
_, err = ief.originConn.WriteTo(serializedPacket, &net.UDPAddr{
|
|
|
|
IP: dst.AsSlice(),
|
|
|
|
})
|
|
|
|
return err
|
2022-09-02 16:29:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// returnToSrc rewrites the echo ID to the original echo ID from the eyeball
|
|
|
|
func (ief *icmpEchoFlow) returnToSrc(reply *echoReply) error {
|
2022-10-13 10:01:25 +00:00
|
|
|
ief.UpdateLastActive()
|
2022-09-02 16:29:50 +00:00
|
|
|
reply.echo.ID = ief.originalEchoID
|
|
|
|
reply.msg.Body = reply.echo
|
|
|
|
pk := packet.ICMP{
|
|
|
|
IP: &packet.IP{
|
|
|
|
Src: reply.from,
|
2022-10-13 10:01:25 +00:00
|
|
|
Dst: ief.src,
|
2022-09-02 16:29:50 +00:00
|
|
|
Protocol: layers.IPProtocol(reply.msg.Type.Protocol()),
|
2022-09-13 13:00:54 +00:00
|
|
|
TTL: packet.DefaultTTL,
|
2022-09-02 16:29:50 +00:00
|
|
|
},
|
|
|
|
Message: reply.msg,
|
|
|
|
}
|
2024-11-27 20:46:08 +00:00
|
|
|
return ief.responder.ReturnPacket(&pk)
|
2022-09-02 16:29:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type echoReply struct {
|
|
|
|
from netip.Addr
|
|
|
|
msg *icmp.Message
|
|
|
|
echo *icmp.Echo
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseReply(from net.Addr, rawMsg []byte) (*echoReply, error) {
|
2022-09-06 12:46:21 +00:00
|
|
|
fromAddr, ok := netipAddr(from)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("cannot convert %s to netip.Addr", from)
|
|
|
|
}
|
|
|
|
proto := layers.IPProtocolICMPv4
|
|
|
|
if fromAddr.Is6() {
|
|
|
|
proto = layers.IPProtocolICMPv6
|
|
|
|
}
|
|
|
|
msg, err := icmp.ParseMessage(int(proto), rawMsg)
|
2022-09-02 16:29:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
echo, err := getICMPEcho(msg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &echoReply{
|
|
|
|
from: fromAddr,
|
|
|
|
msg: msg,
|
|
|
|
echo: echo,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func toICMPEchoFlow(funnel packet.Funnel) (*icmpEchoFlow, error) {
|
|
|
|
icmpFlow, ok := funnel.(*icmpEchoFlow)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("%v is not *ICMPEchoFunnel", funnel)
|
|
|
|
}
|
|
|
|
return icmpFlow, nil
|
|
|
|
}
|
2022-11-08 23:12:33 +00:00
|
|
|
|
2024-11-27 20:46:08 +00:00
|
|
|
func createShouldReplaceFunnelFunc(logger *zerolog.Logger, responder ICMPResponder, pk *packet.ICMP, originalEchoID int) func(packet.Funnel) bool {
|
2022-11-08 23:12:33 +00:00
|
|
|
return func(existing packet.Funnel) bool {
|
|
|
|
existingFlow, err := toICMPEchoFlow(existing)
|
|
|
|
if err != nil {
|
|
|
|
logger.Err(err).
|
|
|
|
Str("src", pk.Src.String()).
|
|
|
|
Str("dst", pk.Dst.String()).
|
|
|
|
Int("originalEchoID", originalEchoID).
|
|
|
|
Msg("Funnel of wrong type found")
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
// Each quic connection should have a unique muxer.
|
|
|
|
// If the existing flow has a different muxer, there's a new quic connection where return packets should be
|
|
|
|
// routed. Otherwise, return packets will be send to the first observed incoming connection, rather than the
|
|
|
|
// most recently observed connection.
|
2024-11-27 20:46:08 +00:00
|
|
|
if existingFlow.responder.ConnectionIndex() != responder.ConnectionIndex() {
|
2022-11-08 23:12:33 +00:00
|
|
|
logger.Debug().
|
|
|
|
Str("src", pk.Src.String()).
|
|
|
|
Str("dst", pk.Dst.String()).
|
|
|
|
Int("originalEchoID", originalEchoID).
|
|
|
|
Msg("Replacing funnel with new responder")
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|