2022-08-01 12:48:33 +00:00
|
|
|
package quic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/lucas-clemente/quic-go"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/rs/zerolog"
|
2022-08-17 15:46:49 +00:00
|
|
|
|
|
|
|
"github.com/cloudflare/cloudflared/packet"
|
2022-10-13 20:30:43 +00:00
|
|
|
"github.com/cloudflare/cloudflared/tracing"
|
2022-08-01 12:48:33 +00:00
|
|
|
)
|
|
|
|
|
2022-09-20 10:39:51 +00:00
|
|
|
type DatagramV2Type byte
|
2022-08-01 12:48:33 +00:00
|
|
|
|
|
|
|
const (
|
2022-10-13 20:30:43 +00:00
|
|
|
// UDP payload
|
2022-09-20 10:39:51 +00:00
|
|
|
DatagramTypeUDP DatagramV2Type = iota
|
2022-10-13 20:30:43 +00:00
|
|
|
// Full IP packet
|
2022-09-20 10:39:51 +00:00
|
|
|
DatagramTypeIP
|
2022-10-13 20:30:43 +00:00
|
|
|
// DatagramTypeIP + tracing ID
|
|
|
|
DatagramTypeIPWithTrace
|
|
|
|
// Tracing spans in protobuf format
|
|
|
|
DatagramTypeTracingSpan
|
2022-09-05 14:09:53 +00:00
|
|
|
)
|
|
|
|
|
2022-10-13 20:30:43 +00:00
|
|
|
type Packet interface {
|
|
|
|
Type() DatagramV2Type
|
|
|
|
Payload() []byte
|
|
|
|
Metadata() []byte
|
|
|
|
}
|
|
|
|
|
2022-09-05 14:09:53 +00:00
|
|
|
const (
|
|
|
|
typeIDLen = 1
|
2022-08-17 17:23:04 +00:00
|
|
|
// Same as sessionDemuxChan capacity
|
2022-08-29 17:49:07 +00:00
|
|
|
packetChanCapacity = 128
|
2022-08-01 12:48:33 +00:00
|
|
|
)
|
|
|
|
|
2022-09-20 10:39:51 +00:00
|
|
|
func SuffixType(b []byte, datagramType DatagramV2Type) ([]byte, error) {
|
2022-09-01 17:50:25 +00:00
|
|
|
if len(b)+typeIDLen > MaxDatagramFrameSize {
|
2022-08-01 12:48:33 +00:00
|
|
|
return nil, fmt.Errorf("datagram size %d exceeds max frame size %d", len(b), MaxDatagramFrameSize)
|
|
|
|
}
|
|
|
|
b = append(b, byte(datagramType))
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Maximum application payload to send to / receive from QUIC datagram frame
|
|
|
|
func (dm *DatagramMuxerV2) mtu() int {
|
|
|
|
return maxDatagramPayloadSize
|
|
|
|
}
|
|
|
|
|
|
|
|
type DatagramMuxerV2 struct {
|
|
|
|
session quic.Connection
|
|
|
|
logger *zerolog.Logger
|
2022-08-17 15:46:49 +00:00
|
|
|
sessionDemuxChan chan<- *packet.Session
|
2022-10-13 20:30:43 +00:00
|
|
|
packetDemuxChan chan Packet
|
2022-08-01 12:48:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewDatagramMuxerV2(
|
|
|
|
quicSession quic.Connection,
|
|
|
|
log *zerolog.Logger,
|
2022-08-17 15:46:49 +00:00
|
|
|
sessionDemuxChan chan<- *packet.Session,
|
2022-08-17 17:23:04 +00:00
|
|
|
) *DatagramMuxerV2 {
|
2022-08-01 12:48:33 +00:00
|
|
|
logger := log.With().Uint8("datagramVersion", 2).Logger()
|
|
|
|
return &DatagramMuxerV2{
|
|
|
|
session: quicSession,
|
|
|
|
logger: &logger,
|
|
|
|
sessionDemuxChan: sessionDemuxChan,
|
2022-10-13 20:30:43 +00:00
|
|
|
packetDemuxChan: make(chan Packet, packetChanCapacity),
|
2022-08-01 12:48:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-17 17:23:04 +00:00
|
|
|
// SendToSession suffix the session ID and datagram version to the payload so the other end of the QUIC connection can
|
2022-08-01 12:48:33 +00:00
|
|
|
// demultiplex the payload from multiple datagram sessions
|
2022-08-17 15:46:49 +00:00
|
|
|
func (dm *DatagramMuxerV2) SendToSession(session *packet.Session) error {
|
|
|
|
if len(session.Payload) > dm.mtu() {
|
2022-08-25 10:05:01 +00:00
|
|
|
packetTooBigDropped.Inc()
|
2022-08-17 15:46:49 +00:00
|
|
|
return fmt.Errorf("origin UDP payload has %d bytes, which exceeds transport MTU %d", len(session.Payload), dm.mtu())
|
2022-08-01 12:48:33 +00:00
|
|
|
}
|
2022-09-20 10:39:51 +00:00
|
|
|
msgWithID, err := SuffixSessionID(session.ID, session.Payload)
|
2022-08-01 12:48:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Failed to suffix session ID to datagram, it will be dropped")
|
|
|
|
}
|
2022-09-20 10:39:51 +00:00
|
|
|
msgWithIDAndType, err := SuffixType(msgWithID, DatagramTypeUDP)
|
2022-08-01 12:48:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Failed to suffix datagram type, it will be dropped")
|
|
|
|
}
|
|
|
|
if err := dm.session.SendMessage(msgWithIDAndType); err != nil {
|
|
|
|
return errors.Wrap(err, "Failed to send datagram back to edge")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-13 20:30:43 +00:00
|
|
|
// SendPacket sends a packet with datagram version in the suffix. If ctx is a TracedContext, it adds the tracing
|
|
|
|
// context between payload and datagram version.
|
|
|
|
// The other end of the QUIC connection can demultiplex by parsing the payload as IP and look at the source and destination.
|
|
|
|
func (dm *DatagramMuxerV2) SendPacket(pk Packet) error {
|
|
|
|
payloadWithMetadata, err := suffixMetadata(pk.Payload(), pk.Metadata())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
payloadWithMetadataAndType, err := SuffixType(payloadWithMetadata, pk.Type())
|
2022-08-01 12:48:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Failed to suffix datagram type, it will be dropped")
|
|
|
|
}
|
2022-10-13 20:30:43 +00:00
|
|
|
if err := dm.session.SendMessage(payloadWithMetadataAndType); err != nil {
|
2022-08-01 12:48:33 +00:00
|
|
|
return errors.Wrap(err, "Failed to send datagram back to edge")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Demux reads datagrams from the QUIC connection and demuxes depending on whether it's a session or packet
|
|
|
|
func (dm *DatagramMuxerV2) ServeReceive(ctx context.Context) error {
|
|
|
|
for {
|
|
|
|
msg, err := dm.session.ReceiveMessage()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := dm.demux(ctx, msg); err != nil {
|
|
|
|
dm.logger.Error().Err(err).Msg("Failed to demux datagram")
|
|
|
|
if err == context.Canceled {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-13 20:30:43 +00:00
|
|
|
func (dm *DatagramMuxerV2) ReceivePacket(ctx context.Context) (pk Packet, err error) {
|
2022-08-17 17:23:04 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2022-10-13 20:30:43 +00:00
|
|
|
return nil, ctx.Err()
|
2022-08-17 17:23:04 +00:00
|
|
|
case pk := <-dm.packetDemuxChan:
|
|
|
|
return pk, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-01 12:48:33 +00:00
|
|
|
func (dm *DatagramMuxerV2) demux(ctx context.Context, msgWithType []byte) error {
|
2022-09-01 17:50:25 +00:00
|
|
|
if len(msgWithType) < typeIDLen {
|
|
|
|
return fmt.Errorf("QUIC datagram should have at least %d byte", typeIDLen)
|
2022-08-01 12:48:33 +00:00
|
|
|
}
|
2022-09-20 10:39:51 +00:00
|
|
|
msgType := DatagramV2Type(msgWithType[len(msgWithType)-typeIDLen])
|
2022-09-01 17:50:25 +00:00
|
|
|
msg := msgWithType[0 : len(msgWithType)-typeIDLen]
|
2022-08-01 12:48:33 +00:00
|
|
|
switch msgType {
|
2022-09-20 10:39:51 +00:00
|
|
|
case DatagramTypeUDP:
|
2022-08-17 17:23:04 +00:00
|
|
|
return dm.handleSession(ctx, msg)
|
2022-08-01 12:48:33 +00:00
|
|
|
default:
|
2022-10-13 20:30:43 +00:00
|
|
|
return dm.handlePacket(ctx, msg, msgType)
|
2022-08-01 12:48:33 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-17 17:23:04 +00:00
|
|
|
|
|
|
|
func (dm *DatagramMuxerV2) handleSession(ctx context.Context, session []byte) error {
|
|
|
|
sessionID, payload, err := extractSessionID(session)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
sessionDatagram := packet.Session{
|
|
|
|
ID: sessionID,
|
|
|
|
Payload: payload,
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case dm.sessionDemuxChan <- &sessionDatagram:
|
|
|
|
return nil
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-13 20:30:43 +00:00
|
|
|
func (dm *DatagramMuxerV2) handlePacket(ctx context.Context, pk []byte, msgType DatagramV2Type) error {
|
|
|
|
var demuxedPacket Packet
|
|
|
|
switch msgType {
|
|
|
|
case DatagramTypeIP:
|
|
|
|
demuxedPacket = RawPacket(packet.RawPacket{Data: pk})
|
|
|
|
case DatagramTypeIPWithTrace:
|
|
|
|
tracingIdentity, payload, err := extractTracingIdentity(pk)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
demuxedPacket = &TracedPacket{
|
|
|
|
Packet: packet.RawPacket{Data: payload},
|
|
|
|
TracingIdentity: tracingIdentity,
|
|
|
|
}
|
|
|
|
case DatagramTypeTracingSpan:
|
|
|
|
tracingIdentity, spans, err := extractTracingIdentity(pk)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
demuxedPacket = &TracingSpanPacket{
|
|
|
|
Spans: spans,
|
|
|
|
TracingIdentity: tracingIdentity,
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("Unexpected datagram type %d", msgType)
|
|
|
|
}
|
2022-08-17 17:23:04 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
2022-10-13 20:30:43 +00:00
|
|
|
case dm.packetDemuxChan <- demuxedPacket:
|
2022-08-17 17:23:04 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2022-10-13 20:30:43 +00:00
|
|
|
|
|
|
|
func extractTracingIdentity(pk []byte) (tracingIdentity []byte, payload []byte, err error) {
|
|
|
|
if len(pk) < tracing.IdentityLength {
|
|
|
|
return nil, nil, fmt.Errorf("packet with tracing context should have at least %d bytes, got %v", tracing.IdentityLength, pk)
|
|
|
|
}
|
|
|
|
tracingIdentity = pk[len(pk)-tracing.IdentityLength:]
|
|
|
|
payload = pk[:len(pk)-tracing.IdentityLength]
|
|
|
|
return tracingIdentity, payload, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type RawPacket packet.RawPacket
|
|
|
|
|
|
|
|
func (rw RawPacket) Type() DatagramV2Type {
|
|
|
|
return DatagramTypeIP
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rw RawPacket) Payload() []byte {
|
|
|
|
return rw.Data
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rw RawPacket) Metadata() []byte {
|
|
|
|
return []byte{}
|
|
|
|
}
|
|
|
|
|
|
|
|
type TracedPacket struct {
|
|
|
|
Packet packet.RawPacket
|
|
|
|
TracingIdentity []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tp *TracedPacket) Type() DatagramV2Type {
|
|
|
|
return DatagramTypeIPWithTrace
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tp *TracedPacket) Payload() []byte {
|
|
|
|
return tp.Packet.Data
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tp *TracedPacket) Metadata() []byte {
|
|
|
|
return tp.TracingIdentity
|
|
|
|
}
|
|
|
|
|
|
|
|
type TracingSpanPacket struct {
|
|
|
|
Spans []byte
|
|
|
|
TracingIdentity []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tsp *TracingSpanPacket) Type() DatagramV2Type {
|
|
|
|
return DatagramTypeTracingSpan
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tsp *TracingSpanPacket) Payload() []byte {
|
|
|
|
return tsp.Spans
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tsp *TracingSpanPacket) Metadata() []byte {
|
|
|
|
return tsp.TracingIdentity
|
|
|
|
}
|