2021-11-14 11:18:05 +00:00
|
|
|
package quic
|
|
|
|
|
|
|
|
import (
|
2022-08-01 12:48:33 +00:00
|
|
|
"context"
|
2021-11-14 11:18:05 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
2021-11-23 12:45:59 +00:00
|
|
|
"github.com/pkg/errors"
|
2023-05-06 00:42:41 +00:00
|
|
|
"github.com/quic-go/quic-go"
|
2022-05-30 12:38:15 +00:00
|
|
|
"github.com/rs/zerolog"
|
2022-08-17 15:46:49 +00:00
|
|
|
|
|
|
|
"github.com/cloudflare/cloudflared/packet"
|
2021-11-14 11:18:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-01-06 12:17:10 +00:00
|
|
|
sessionIDLen = len(uuid.UUID{})
|
2021-11-14 11:18:05 +00:00
|
|
|
)
|
|
|
|
|
2022-08-01 12:48:33 +00:00
|
|
|
type BaseDatagramMuxer interface {
|
2022-08-17 15:46:49 +00:00
|
|
|
// SendToSession suffix the session ID to the payload so the other end of the QUIC connection can demultiplex the
|
|
|
|
// payload from multiple datagram sessions.
|
|
|
|
SendToSession(session *packet.Session) error
|
2022-08-01 12:48:33 +00:00
|
|
|
// ServeReceive starts a loop to receive datagrams from the QUIC connection
|
|
|
|
ServeReceive(ctx context.Context) error
|
|
|
|
}
|
|
|
|
|
2021-11-23 12:45:59 +00:00
|
|
|
type DatagramMuxer struct {
|
2022-08-01 12:48:33 +00:00
|
|
|
session quic.Connection
|
|
|
|
logger *zerolog.Logger
|
2022-08-17 15:46:49 +00:00
|
|
|
demuxChan chan<- *packet.Session
|
2021-11-23 12:45:59 +00:00
|
|
|
}
|
|
|
|
|
2022-08-17 15:46:49 +00:00
|
|
|
func NewDatagramMuxer(quicSession quic.Connection, log *zerolog.Logger, demuxChan chan<- *packet.Session) *DatagramMuxer {
|
2022-08-01 12:48:33 +00:00
|
|
|
logger := log.With().Uint8("datagramVersion", 1).Logger()
|
2021-11-23 12:45:59 +00:00
|
|
|
return &DatagramMuxer{
|
2022-08-01 12:48:33 +00:00
|
|
|
session: quicSession,
|
|
|
|
logger: &logger,
|
|
|
|
demuxChan: demuxChan,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Maximum application payload to send to / receive from QUIC datagram frame
|
|
|
|
func (dm *DatagramMuxer) mtu() int {
|
|
|
|
return maxDatagramPayloadSize
|
2021-11-23 12:45:59 +00:00
|
|
|
}
|
|
|
|
|
2022-08-17 15:46:49 +00:00
|
|
|
func (dm *DatagramMuxer) 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())
|
2021-11-23 12:45:59 +00:00
|
|
|
}
|
2022-09-20 10:39:51 +00:00
|
|
|
payloadWithMetadata, err := SuffixSessionID(session.ID, session.Payload)
|
2021-11-23 12:45:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Failed to suffix session ID to datagram, it will be dropped")
|
|
|
|
}
|
2022-08-01 12:48:33 +00:00
|
|
|
if err := dm.session.SendMessage(payloadWithMetadata); err != nil {
|
2021-11-23 12:45:59 +00:00
|
|
|
return errors.Wrap(err, "Failed to send datagram back to edge")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-01 12:48:33 +00:00
|
|
|
func (dm *DatagramMuxer) ServeReceive(ctx context.Context) error {
|
|
|
|
for {
|
|
|
|
// Extracts datagram session ID, then sends the session ID and payload to receiver
|
|
|
|
// which determines how to proxy to the origin. It assumes the datagram session has already been
|
|
|
|
// registered with receiver through other side channel
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2021-11-23 12:45:59 +00:00
|
|
|
}
|
2022-08-01 12:48:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (dm *DatagramMuxer) demux(ctx context.Context, msg []byte) error {
|
2022-05-30 12:38:15 +00:00
|
|
|
sessionID, payload, err := extractSessionID(msg)
|
|
|
|
if err != nil {
|
2022-08-01 12:48:33 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-08-17 15:46:49 +00:00
|
|
|
sessionDatagram := packet.Session{
|
2022-08-01 12:48:33 +00:00
|
|
|
ID: sessionID,
|
|
|
|
Payload: payload,
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case dm.demuxChan <- &sessionDatagram:
|
|
|
|
return nil
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
2022-05-30 12:38:15 +00:00
|
|
|
}
|
2021-12-22 18:07:44 +00:00
|
|
|
}
|
|
|
|
|
2021-11-14 11:18:05 +00:00
|
|
|
// Each QUIC datagram should be suffixed with session ID.
|
2022-01-06 12:17:10 +00:00
|
|
|
// extractSessionID extracts the session ID and a slice with only the payload
|
|
|
|
func extractSessionID(b []byte) (uuid.UUID, []byte, error) {
|
2021-11-14 11:18:05 +00:00
|
|
|
msgLen := len(b)
|
|
|
|
if msgLen < sessionIDLen {
|
|
|
|
return uuid.Nil, nil, fmt.Errorf("session ID has %d bytes, but data only has %d", sessionIDLen, len(b))
|
|
|
|
}
|
|
|
|
// Parse last 16 bytess as UUID and remove it from slice
|
|
|
|
sessionID, err := uuid.FromBytes(b[len(b)-sessionIDLen:])
|
|
|
|
if err != nil {
|
|
|
|
return uuid.Nil, nil, err
|
|
|
|
}
|
|
|
|
b = b[:len(b)-sessionIDLen]
|
|
|
|
return sessionID, b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SuffixSessionID appends the session ID at the end of the payload. Suffix is more performant than prefix because
|
|
|
|
// the payload slice might already have enough capacity to append the session ID at the end
|
2022-09-20 10:39:51 +00:00
|
|
|
func SuffixSessionID(sessionID uuid.UUID, b []byte) ([]byte, error) {
|
2022-10-13 20:30:43 +00:00
|
|
|
return suffixMetadata(b, sessionID[:])
|
|
|
|
}
|
|
|
|
|
|
|
|
func suffixMetadata(payload, metadata []byte) ([]byte, error) {
|
|
|
|
if len(payload)+len(metadata) > MaxDatagramFrameSize {
|
2021-11-14 11:18:05 +00:00
|
|
|
return nil, fmt.Errorf("datagram size exceed %d", MaxDatagramFrameSize)
|
|
|
|
}
|
2022-10-13 20:30:43 +00:00
|
|
|
return append(payload, metadata...), nil
|
2021-11-14 11:18:05 +00:00
|
|
|
}
|