2022-10-13 10:01:25 +00:00
|
|
|
package ingress
|
2022-09-13 13:00:54 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/netip"
|
2022-09-30 09:43:39 +00:00
|
|
|
"sync/atomic"
|
2022-09-13 13:00:54 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/google/gopacket/layers"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"golang.org/x/net/icmp"
|
|
|
|
"golang.org/x/net/ipv4"
|
|
|
|
"golang.org/x/net/ipv6"
|
2022-10-13 10:01:25 +00:00
|
|
|
|
|
|
|
"github.com/cloudflare/cloudflared/packet"
|
|
|
|
quicpogs "github.com/cloudflare/cloudflared/quic"
|
2022-09-13 13:00:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2024-11-27 20:46:08 +00:00
|
|
|
defaultRouter = &icmpRouter{
|
|
|
|
ipv4Proxy: nil,
|
|
|
|
ipv4Src: netip.MustParseAddr("172.16.0.1"),
|
|
|
|
ipv6Proxy: nil,
|
|
|
|
ipv6Src: netip.MustParseAddr("fd51:2391:523:f4ee::1"),
|
2022-09-30 09:43:39 +00:00
|
|
|
}
|
2022-09-13 13:00:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRouterReturnTTLExceed(t *testing.T) {
|
2022-10-13 10:01:25 +00:00
|
|
|
muxer := newMockMuxer(0)
|
2024-11-27 20:46:08 +00:00
|
|
|
router := NewPacketRouter(defaultRouter, muxer, 0, &noopLogger)
|
2022-09-13 13:00:54 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
routerStopped := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
router.Serve(ctx)
|
|
|
|
close(routerStopped)
|
|
|
|
}()
|
|
|
|
|
2022-10-13 10:01:25 +00:00
|
|
|
pk := packet.ICMP{
|
|
|
|
IP: &packet.IP{
|
2022-09-13 13:00:54 +00:00
|
|
|
Src: netip.MustParseAddr("192.168.1.1"),
|
|
|
|
Dst: netip.MustParseAddr("10.0.0.1"),
|
|
|
|
Protocol: layers.IPProtocolICMPv4,
|
|
|
|
TTL: 1,
|
|
|
|
},
|
|
|
|
Message: &icmp.Message{
|
|
|
|
Type: ipv4.ICMPTypeEcho,
|
|
|
|
Code: 0,
|
|
|
|
Body: &icmp.Echo{
|
|
|
|
ID: 12481,
|
|
|
|
Seq: 8036,
|
|
|
|
Data: []byte("TTL exceed"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2024-11-27 20:46:08 +00:00
|
|
|
assertTTLExceed(t, &pk, defaultRouter.ipv4Src, muxer)
|
2022-10-13 10:01:25 +00:00
|
|
|
pk = packet.ICMP{
|
|
|
|
IP: &packet.IP{
|
2022-09-13 13:00:54 +00:00
|
|
|
Src: netip.MustParseAddr("fd51:2391:523:f4ee::1"),
|
|
|
|
Dst: netip.MustParseAddr("fd51:2391:697:f4ee::2"),
|
|
|
|
Protocol: layers.IPProtocolICMPv6,
|
|
|
|
TTL: 1,
|
|
|
|
},
|
|
|
|
Message: &icmp.Message{
|
|
|
|
Type: ipv6.ICMPTypeEchoRequest,
|
|
|
|
Code: 0,
|
|
|
|
Body: &icmp.Echo{
|
|
|
|
ID: 42583,
|
|
|
|
Seq: 7039,
|
|
|
|
Data: []byte("TTL exceed"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2024-11-27 20:46:08 +00:00
|
|
|
assertTTLExceed(t, &pk, defaultRouter.ipv6Src, muxer)
|
2022-09-13 13:00:54 +00:00
|
|
|
|
|
|
|
cancel()
|
|
|
|
<-routerStopped
|
|
|
|
}
|
|
|
|
|
2022-10-13 10:01:25 +00:00
|
|
|
func assertTTLExceed(t *testing.T, originalPacket *packet.ICMP, expectedSrc netip.Addr, muxer *mockMuxer) {
|
|
|
|
encoder := packet.NewEncoder()
|
2022-09-13 13:00:54 +00:00
|
|
|
rawPacket, err := encoder.Encode(originalPacket)
|
|
|
|
require.NoError(t, err)
|
2022-10-13 10:01:25 +00:00
|
|
|
muxer.edgeToCfd <- quicpogs.RawPacket(rawPacket)
|
2022-09-30 09:43:39 +00:00
|
|
|
|
2022-10-13 10:01:25 +00:00
|
|
|
resp := <-muxer.cfdToEdge
|
|
|
|
decoder := packet.NewICMPDecoder()
|
|
|
|
decoded, err := decoder.Decode(packet.RawPacket(resp.(quicpogs.RawPacket)))
|
2022-09-13 13:00:54 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, expectedSrc, decoded.Src)
|
|
|
|
require.Equal(t, originalPacket.Src, decoded.Dst)
|
|
|
|
require.Equal(t, originalPacket.Protocol, decoded.Protocol)
|
2022-10-13 10:01:25 +00:00
|
|
|
require.Equal(t, packet.DefaultTTL, decoded.TTL)
|
2022-09-13 13:00:54 +00:00
|
|
|
if originalPacket.Dst.Is4() {
|
|
|
|
require.Equal(t, ipv4.ICMPTypeTimeExceeded, decoded.Type)
|
|
|
|
} else {
|
|
|
|
require.Equal(t, ipv6.ICMPTypeTimeExceeded, decoded.Type)
|
|
|
|
}
|
|
|
|
require.Equal(t, 0, decoded.Code)
|
|
|
|
timeExceed, ok := decoded.Body.(*icmp.TimeExceeded)
|
|
|
|
require.True(t, ok)
|
|
|
|
require.True(t, bytes.Equal(rawPacket.Data, timeExceed.Data))
|
|
|
|
}
|
|
|
|
|
2022-10-13 10:01:25 +00:00
|
|
|
type mockMuxer struct {
|
|
|
|
cfdToEdge chan quicpogs.Packet
|
|
|
|
edgeToCfd chan quicpogs.Packet
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMockMuxer(capacity int) *mockMuxer {
|
|
|
|
return &mockMuxer{
|
|
|
|
cfdToEdge: make(chan quicpogs.Packet, capacity),
|
|
|
|
edgeToCfd: make(chan quicpogs.Packet, capacity),
|
|
|
|
}
|
2022-09-13 13:00:54 +00:00
|
|
|
}
|
|
|
|
|
2022-10-13 10:01:25 +00:00
|
|
|
// Copy packet, because icmpProxy expects the encoder buffer to be reusable after the packet is sent
|
|
|
|
func (mm *mockMuxer) SendPacket(pk quicpogs.Packet) error {
|
|
|
|
payload := pk.Payload()
|
|
|
|
copiedPayload := make([]byte, len(payload))
|
|
|
|
copy(copiedPayload, payload)
|
|
|
|
|
|
|
|
metadata := pk.Metadata()
|
|
|
|
copiedMetadata := make([]byte, len(metadata))
|
|
|
|
copy(copiedMetadata, metadata)
|
|
|
|
|
|
|
|
var copiedPacket quicpogs.Packet
|
|
|
|
switch pk.Type() {
|
|
|
|
case quicpogs.DatagramTypeIP:
|
|
|
|
copiedPacket = quicpogs.RawPacket(packet.RawPacket{
|
|
|
|
Data: copiedPayload,
|
|
|
|
})
|
|
|
|
case quicpogs.DatagramTypeIPWithTrace:
|
|
|
|
copiedPacket = &quicpogs.TracedPacket{
|
|
|
|
Packet: packet.RawPacket{
|
|
|
|
Data: copiedPayload,
|
|
|
|
},
|
|
|
|
TracingIdentity: copiedMetadata,
|
|
|
|
}
|
2022-10-13 10:01:25 +00:00
|
|
|
case quicpogs.DatagramTypeTracingSpan:
|
|
|
|
copiedPacket = &quicpogs.TracingSpanPacket{
|
|
|
|
Spans: copiedPayload,
|
|
|
|
TracingIdentity: copiedMetadata,
|
|
|
|
}
|
2022-10-13 10:01:25 +00:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("unexpected metadata type %d", pk.Type())
|
2022-09-30 09:43:39 +00:00
|
|
|
}
|
2022-10-13 10:01:25 +00:00
|
|
|
mm.cfdToEdge <- copiedPacket
|
2022-09-30 09:43:39 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-13 10:01:25 +00:00
|
|
|
func (mm *mockMuxer) ReceivePacket(ctx context.Context) (quicpogs.Packet, error) {
|
2022-09-13 13:00:54 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2022-10-13 10:01:25 +00:00
|
|
|
return nil, ctx.Err()
|
|
|
|
case pk := <-mm.edgeToCfd:
|
2022-09-13 13:00:54 +00:00
|
|
|
return pk, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 09:43:39 +00:00
|
|
|
type routerEnabledChecker struct {
|
|
|
|
enabled uint32
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rec *routerEnabledChecker) isEnabled() bool {
|
|
|
|
if atomic.LoadUint32(&rec.enabled) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rec *routerEnabledChecker) set(enabled bool) {
|
|
|
|
if enabled {
|
|
|
|
atomic.StoreUint32(&rec.enabled, 1)
|
|
|
|
} else {
|
|
|
|
atomic.StoreUint32(&rec.enabled, 0)
|
|
|
|
}
|
|
|
|
}
|