2022-08-25 11:34:19 +00:00
|
|
|
//go:build linux
|
|
|
|
|
|
|
|
package ingress
|
|
|
|
|
2022-09-02 16:29:50 +00:00
|
|
|
// This file implements ICMPProxy for Linux. Each (source IP, destination IP, echo ID) opens a non-privileged ICMP socket.
|
|
|
|
// The source IP of the requests are rewritten to the bind IP of the socket and echo ID rewritten to the port number of
|
|
|
|
// the socket. The kernel ensures the socket only reads replies whose echo ID matches the port number.
|
|
|
|
// For more information about the socket, see https://man7.org/linux/man-pages/man7/icmp.7.html and https://lwn.net/Articles/422330/
|
|
|
|
|
2022-08-25 11:34:19 +00:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/netip"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"golang.org/x/net/icmp"
|
|
|
|
|
|
|
|
"github.com/cloudflare/cloudflared/packet"
|
|
|
|
)
|
|
|
|
|
|
|
|
type icmpProxy struct {
|
2022-09-02 16:29:50 +00:00
|
|
|
srcFunnelTracker *packet.FunnelTracker
|
2022-08-25 11:34:19 +00:00
|
|
|
listenIP netip.Addr
|
|
|
|
logger *zerolog.Logger
|
2022-09-02 16:29:50 +00:00
|
|
|
idleTimeout time.Duration
|
2022-08-25 11:34:19 +00:00
|
|
|
}
|
|
|
|
|
2022-09-02 16:29:50 +00:00
|
|
|
func newICMPProxy(listenIP netip.Addr, logger *zerolog.Logger, idleTimeout time.Duration) (ICMPProxy, error) {
|
2022-08-25 11:34:19 +00:00
|
|
|
if err := testPermission(listenIP); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &icmpProxy{
|
2022-09-02 16:29:50 +00:00
|
|
|
srcFunnelTracker: packet.NewFunnelTracker(),
|
2022-08-25 11:34:19 +00:00
|
|
|
listenIP: listenIP,
|
|
|
|
logger: logger,
|
2022-09-02 16:29:50 +00:00
|
|
|
idleTimeout: idleTimeout,
|
2022-08-25 11:34:19 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func testPermission(listenIP netip.Addr) error {
|
|
|
|
// Opens a non-privileged ICMP socket. On Linux the group ID of the process needs to be in ping_group_range
|
|
|
|
conn, err := newICMPConn(listenIP)
|
|
|
|
if err != nil {
|
|
|
|
// TODO: TUN-6715 check if cloudflared is in ping_group_range if the check failed. If not log instruction to
|
|
|
|
// change the group ID
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// This conn is only to test if cloudflared has permission to open this type of socket
|
|
|
|
conn.Close()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-02 16:29:50 +00:00
|
|
|
func (ip *icmpProxy) Request(pk *packet.ICMP, responder packet.FunnelUniPipe) error {
|
2022-08-25 11:34:19 +00:00
|
|
|
if pk == nil {
|
|
|
|
return errPacketNil
|
|
|
|
}
|
2022-09-02 16:29:50 +00:00
|
|
|
funnelID := srcIPFunnelID(pk.Src)
|
|
|
|
funnel, exists := ip.srcFunnelTracker.Get(funnelID)
|
|
|
|
if !exists {
|
|
|
|
originalEcho, err := getICMPEcho(pk.Message)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
conn, err := newICMPConn(ip.listenIP)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to open ICMP socket")
|
|
|
|
}
|
|
|
|
localUDPAddr, ok := conn.LocalAddr().(*net.UDPAddr)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("ICMP listener address %s is not net.UDPAddr", conn.LocalAddr())
|
|
|
|
}
|
|
|
|
originSender := originSender{conn: conn}
|
|
|
|
echoID := localUDPAddr.Port
|
|
|
|
icmpFlow := newICMPEchoFlow(pk.Src, &originSender, responder, echoID, originalEcho.ID, packet.NewEncoder())
|
|
|
|
if replaced := ip.srcFunnelTracker.Register(funnelID, icmpFlow); replaced {
|
|
|
|
ip.logger.Info().Str("src", pk.Src.String()).Msg("Replaced funnel")
|
|
|
|
}
|
|
|
|
if err := icmpFlow.sendToDst(pk.Dst, pk.Message); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to send ICMP echo request")
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
defer ip.srcFunnelTracker.Unregister(funnelID, icmpFlow)
|
|
|
|
if err := ip.listenResponse(icmpFlow, conn); err != nil {
|
|
|
|
ip.logger.Err(err).
|
|
|
|
Str("funnelID", funnelID.String()).
|
|
|
|
Int("echoID", echoID).
|
|
|
|
Msg("Failed to listen for ICMP echo response")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
icmpFlow, err := toICMPEchoFlow(funnel)
|
2022-08-29 17:49:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-08-25 11:34:19 +00:00
|
|
|
}
|
2022-09-02 16:29:50 +00:00
|
|
|
if err := icmpFlow.sendToDst(pk.Dst, pk.Message); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to send ICMP echo request")
|
|
|
|
}
|
|
|
|
return nil
|
2022-08-25 11:34:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ip *icmpProxy) Serve(ctx context.Context) error {
|
2022-09-02 16:29:50 +00:00
|
|
|
ip.srcFunnelTracker.ScheduleCleanup(ctx, ip.idleTimeout)
|
2022-08-25 11:34:19 +00:00
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
|
2022-09-02 16:29:50 +00:00
|
|
|
func (ip *icmpProxy) listenResponse(flow *icmpEchoFlow, conn *icmp.PacketConn) error {
|
|
|
|
buf := make([]byte, mtu)
|
|
|
|
for {
|
2022-09-06 12:46:21 +00:00
|
|
|
n, from, err := conn.ReadFrom(buf)
|
2022-09-02 16:29:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-08-25 11:34:19 +00:00
|
|
|
}
|
2022-09-06 12:46:21 +00:00
|
|
|
reply, err := parseReply(from, buf[:n])
|
|
|
|
if err != nil {
|
|
|
|
ip.logger.Error().Err(err).Str("dst", from.String()).Msg("Failed to parse ICMP reply")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !isEchoReply(reply.msg) {
|
|
|
|
ip.logger.Debug().Str("dst", from.String()).Msgf("Drop ICMP %s from reply", reply.msg.Type)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := flow.returnToSrc(reply); err != nil {
|
|
|
|
ip.logger.Err(err).Str("dst", from.String()).Msg("Failed to send ICMP reply")
|
2022-09-02 16:29:50 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-08-25 11:34:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-02 16:29:50 +00:00
|
|
|
// originSender wraps icmp.PacketConn to implement packet.FunnelUniPipe interface
|
|
|
|
type originSender struct {
|
|
|
|
conn *icmp.PacketConn
|
2022-08-25 11:34:19 +00:00
|
|
|
}
|
|
|
|
|
2022-09-02 16:29:50 +00:00
|
|
|
func (os *originSender) SendPacket(dst netip.Addr, pk packet.RawPacket) error {
|
|
|
|
_, err := os.conn.WriteTo(pk.Data, &net.UDPAddr{
|
|
|
|
IP: dst.AsSlice(),
|
2022-08-25 11:34:19 +00:00
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-02 16:29:50 +00:00
|
|
|
func (os *originSender) Close() error {
|
|
|
|
return os.conn.Close()
|
2022-08-25 11:34:19 +00:00
|
|
|
}
|
|
|
|
|
2022-09-02 16:29:50 +00:00
|
|
|
type srcIPFunnelID netip.Addr
|
2022-08-25 11:34:19 +00:00
|
|
|
|
2022-09-02 16:29:50 +00:00
|
|
|
func (sifd srcIPFunnelID) Type() string {
|
|
|
|
return "srcIP"
|
2022-08-25 11:34:19 +00:00
|
|
|
}
|
|
|
|
|
2022-09-02 16:29:50 +00:00
|
|
|
func (sifd srcIPFunnelID) String() string {
|
|
|
|
return netip.Addr(sifd).String()
|
2022-08-25 11:34:19 +00:00
|
|
|
}
|