2022-08-18 15:03:47 +00:00
|
|
|
package ingress
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2022-09-02 16:29:50 +00:00
|
|
|
"net"
|
2022-08-18 15:03:47 +00:00
|
|
|
"net/netip"
|
2022-09-06 12:46:21 +00:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
2022-08-18 15:03:47 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/google/gopacket/layers"
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"golang.org/x/net/icmp"
|
|
|
|
"golang.org/x/net/ipv4"
|
2022-09-06 12:46:21 +00:00
|
|
|
"golang.org/x/net/ipv6"
|
2022-08-18 15:03:47 +00:00
|
|
|
|
|
|
|
"github.com/cloudflare/cloudflared/packet"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-09-06 12:46:21 +00:00
|
|
|
noopLogger = zerolog.Nop()
|
|
|
|
localhostIP = netip.MustParseAddr("127.0.0.1")
|
|
|
|
localhostIPv6 = netip.MustParseAddr("::1")
|
2022-08-18 15:03:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TestICMPProxyEcho makes sure we can send ICMP echo via the Request method and receives response via the
|
|
|
|
// ListenResponse method
|
2022-09-06 12:20:50 +00:00
|
|
|
//
|
|
|
|
// Note: if this test fails on your device under Linux, then most likely you need to make sure that your user
|
|
|
|
// is allowed in ping_group_range. See the following gist for how to do that:
|
|
|
|
// https://github.com/ValentinBELYN/icmplib/blob/main/docs/6-use-icmplib-without-privileges.md
|
2022-08-18 15:03:47 +00:00
|
|
|
func TestICMPProxyEcho(t *testing.T) {
|
2022-09-06 12:46:21 +00:00
|
|
|
testICMPProxyEcho(t, true)
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Skip("TODO: TUN-6743: test ICMPv6 on Windows")
|
|
|
|
}
|
|
|
|
testICMPProxyEcho(t, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testICMPProxyEcho(t *testing.T, sendIPv4 bool) {
|
2022-08-18 15:03:47 +00:00
|
|
|
const (
|
|
|
|
echoID = 36571
|
2022-09-06 12:46:21 +00:00
|
|
|
endSeq = 20
|
2022-08-18 15:03:47 +00:00
|
|
|
)
|
2022-08-22 16:41:51 +00:00
|
|
|
|
2022-09-06 12:46:21 +00:00
|
|
|
proxy, err := NewICMPProxy(&noopLogger)
|
2022-08-18 15:03:47 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
proxyDone := make(chan struct{})
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
go func() {
|
2022-08-25 11:34:19 +00:00
|
|
|
proxy.Serve(ctx)
|
2022-08-18 15:03:47 +00:00
|
|
|
close(proxyDone)
|
|
|
|
}()
|
|
|
|
|
|
|
|
responder := echoFlowResponder{
|
|
|
|
decoder: packet.NewICMPDecoder(),
|
2022-08-29 17:49:07 +00:00
|
|
|
respChan: make(chan []byte, 1),
|
2022-08-18 15:03:47 +00:00
|
|
|
}
|
|
|
|
|
2022-09-06 12:46:21 +00:00
|
|
|
protocol := layers.IPProtocolICMPv6
|
|
|
|
if sendIPv4 {
|
|
|
|
protocol = layers.IPProtocolICMPv4
|
2022-08-18 15:03:47 +00:00
|
|
|
}
|
2022-09-06 12:46:21 +00:00
|
|
|
localIPs := getLocalIPs(t, sendIPv4)
|
|
|
|
ips := make([]*packet.IP, len(localIPs))
|
|
|
|
for i, localIP := range localIPs {
|
|
|
|
ips[i] = &packet.IP{
|
|
|
|
Src: localIP,
|
|
|
|
Dst: localIP,
|
|
|
|
Protocol: protocol,
|
2022-09-02 16:29:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-06 12:46:21 +00:00
|
|
|
var icmpType icmp.Type = ipv6.ICMPTypeEchoRequest
|
|
|
|
if sendIPv4 {
|
|
|
|
icmpType = ipv4.ICMPTypeEcho
|
|
|
|
}
|
2022-09-02 16:29:50 +00:00
|
|
|
for seq := 0; seq < endSeq; seq++ {
|
|
|
|
for i, ip := range ips {
|
|
|
|
pk := packet.ICMP{
|
2022-09-06 12:46:21 +00:00
|
|
|
IP: ip,
|
2022-09-02 16:29:50 +00:00
|
|
|
Message: &icmp.Message{
|
2022-09-06 12:46:21 +00:00
|
|
|
Type: icmpType,
|
2022-09-02 16:29:50 +00:00
|
|
|
Code: 0,
|
|
|
|
Body: &icmp.Echo{
|
|
|
|
ID: echoID + i,
|
|
|
|
Seq: seq,
|
|
|
|
Data: []byte(fmt.Sprintf("icmp echo seq %d", seq)),
|
|
|
|
},
|
2022-08-18 15:03:47 +00:00
|
|
|
},
|
2022-09-02 16:29:50 +00:00
|
|
|
}
|
|
|
|
require.NoError(t, proxy.Request(&pk, &responder))
|
|
|
|
responder.validate(t, &pk)
|
2022-08-18 15:03:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
cancel()
|
|
|
|
<-proxyDone
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestICMPProxyRejectNotEcho makes sure it rejects messages other than echo
|
|
|
|
func TestICMPProxyRejectNotEcho(t *testing.T) {
|
|
|
|
msgs := []icmp.Message{
|
|
|
|
{
|
|
|
|
Type: ipv4.ICMPTypeDestinationUnreachable,
|
|
|
|
Code: 1,
|
|
|
|
Body: &icmp.DstUnreach{
|
|
|
|
Data: []byte("original packet"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: ipv4.ICMPTypeTimeExceeded,
|
|
|
|
Code: 1,
|
|
|
|
Body: &icmp.TimeExceeded{
|
|
|
|
Data: []byte("original packet"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: ipv4.ICMPType(2),
|
|
|
|
Code: 0,
|
|
|
|
Body: &icmp.PacketTooBig{
|
|
|
|
MTU: 1280,
|
|
|
|
Data: []byte("original packet"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2022-09-06 12:46:21 +00:00
|
|
|
testICMPProxyRejectNotEcho(t, localhostIP, msgs)
|
|
|
|
msgsV6 := []icmp.Message{
|
|
|
|
{
|
|
|
|
Type: ipv6.ICMPTypeDestinationUnreachable,
|
|
|
|
Code: 3,
|
|
|
|
Body: &icmp.DstUnreach{
|
|
|
|
Data: []byte("original packet"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: ipv6.ICMPTypeTimeExceeded,
|
|
|
|
Code: 0,
|
|
|
|
Body: &icmp.TimeExceeded{
|
|
|
|
Data: []byte("original packet"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: ipv6.ICMPTypePacketTooBig,
|
|
|
|
Code: 0,
|
|
|
|
Body: &icmp.PacketTooBig{
|
|
|
|
MTU: 1280,
|
|
|
|
Data: []byte("original packet"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
testICMPProxyRejectNotEcho(t, localhostIPv6, msgsV6)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testICMPProxyRejectNotEcho(t *testing.T, srcDstIP netip.Addr, msgs []icmp.Message) {
|
|
|
|
proxy, err := NewICMPProxy(&noopLogger)
|
2022-08-18 15:03:47 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
responder := echoFlowResponder{
|
|
|
|
decoder: packet.NewICMPDecoder(),
|
|
|
|
respChan: make(chan []byte),
|
|
|
|
}
|
2022-09-06 12:46:21 +00:00
|
|
|
protocol := layers.IPProtocolICMPv4
|
|
|
|
if srcDstIP.Is6() {
|
|
|
|
protocol = layers.IPProtocolICMPv6
|
|
|
|
}
|
2022-08-18 15:03:47 +00:00
|
|
|
for _, m := range msgs {
|
|
|
|
pk := packet.ICMP{
|
|
|
|
IP: &packet.IP{
|
2022-09-06 12:46:21 +00:00
|
|
|
Src: srcDstIP,
|
|
|
|
Dst: srcDstIP,
|
|
|
|
Protocol: protocol,
|
2022-08-18 15:03:47 +00:00
|
|
|
},
|
|
|
|
Message: &m,
|
|
|
|
}
|
|
|
|
require.Error(t, proxy.Request(&pk, &responder))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type echoFlowResponder struct {
|
|
|
|
decoder *packet.ICMPDecoder
|
|
|
|
respChan chan []byte
|
|
|
|
}
|
|
|
|
|
2022-09-02 16:29:50 +00:00
|
|
|
func (efr *echoFlowResponder) SendPacket(dst netip.Addr, pk packet.RawPacket) error {
|
2022-08-18 15:03:47 +00:00
|
|
|
copiedPacket := make([]byte, len(pk.Data))
|
|
|
|
copy(copiedPacket, pk.Data)
|
|
|
|
efr.respChan <- copiedPacket
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-02 16:29:50 +00:00
|
|
|
func (efr *echoFlowResponder) Close() error {
|
|
|
|
close(efr.respChan)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-18 15:03:47 +00:00
|
|
|
func (efr *echoFlowResponder) validate(t *testing.T, echoReq *packet.ICMP) {
|
|
|
|
pk := <-efr.respChan
|
|
|
|
decoded, err := efr.decoder.Decode(packet.RawPacket{Data: pk})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, decoded.Src, echoReq.Dst)
|
|
|
|
require.Equal(t, decoded.Dst, echoReq.Src)
|
|
|
|
require.Equal(t, echoReq.Protocol, decoded.Protocol)
|
|
|
|
|
2022-09-06 12:46:21 +00:00
|
|
|
if echoReq.Type == ipv4.ICMPTypeEcho {
|
|
|
|
require.Equal(t, ipv4.ICMPTypeEchoReply, decoded.Type)
|
|
|
|
} else {
|
|
|
|
require.Equal(t, ipv6.ICMPTypeEchoReply, decoded.Type)
|
|
|
|
}
|
2022-08-18 15:03:47 +00:00
|
|
|
require.Equal(t, 0, decoded.Code)
|
2022-09-06 12:46:21 +00:00
|
|
|
if echoReq.Type == ipv4.ICMPTypeEcho {
|
|
|
|
require.NotZero(t, decoded.Checksum)
|
|
|
|
} else {
|
|
|
|
// For ICMPv6, the kernel will compute the checksum during transmission unless pseudo header is not nil
|
|
|
|
require.Zero(t, decoded.Checksum)
|
|
|
|
}
|
|
|
|
|
2022-08-22 16:41:51 +00:00
|
|
|
require.Equal(t, echoReq.Body, decoded.Body)
|
2022-08-18 15:03:47 +00:00
|
|
|
}
|
2022-09-06 12:46:21 +00:00
|
|
|
|
|
|
|
func getLocalIPs(t *testing.T, ipv4 bool) []netip.Addr {
|
|
|
|
interfaces, err := net.Interfaces()
|
|
|
|
require.NoError(t, err)
|
|
|
|
localIPs := []netip.Addr{}
|
|
|
|
for _, i := range interfaces {
|
|
|
|
// Skip TUN devices
|
|
|
|
if strings.Contains(i.Name, "tun") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
addrs, err := i.Addrs()
|
|
|
|
require.NoError(t, err)
|
|
|
|
for _, addr := range addrs {
|
|
|
|
if ipnet, ok := addr.(*net.IPNet); ok && (ipnet.IP.IsPrivate() || ipnet.IP.IsLoopback()) {
|
|
|
|
if (ipv4 && ipnet.IP.To4() != nil) || (!ipv4 && ipnet.IP.To4() == nil) {
|
|
|
|
localIPs = append(localIPs, netip.MustParseAddr(ipnet.IP.String()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return localIPs
|
|
|
|
}
|