1a74702c6SGeorge Wang/* Copyright (c) 2017 - 2022 LiteSpeed Technologies Inc.  See LICENSE. */
25392f7a3SLiteSpeed Tech#ifndef LSQUIC_PACKET_GQUIC_H
35392f7a3SLiteSpeed Tech#define LSQUIC_PACKET_GQUIC_H 1
45392f7a3SLiteSpeed Tech
55392f7a3SLiteSpeed Tech#include <stdint.h>
65392f7a3SLiteSpeed Tech
75392f7a3SLiteSpeed Tech#include "lsquic_int_types.h"
85392f7a3SLiteSpeed Tech
95392f7a3SLiteSpeed Techenum PACKET_PUBLIC_FLAGS
105392f7a3SLiteSpeed Tech{
115392f7a3SLiteSpeed Tech  PACKET_PUBLIC_FLAGS_VERSION = 1,
125392f7a3SLiteSpeed Tech  PACKET_PUBLIC_FLAGS_RST = 2,
135392f7a3SLiteSpeed Tech  PACKET_PUBLIC_FLAGS_NONCE = 4,
145392f7a3SLiteSpeed Tech  PACKET_PUBLIC_FLAGS_8BYTE_CONNECTION_ID = 8,
155392f7a3SLiteSpeed Tech  PACKET_PUBLIC_FLAGS_MULTIPATH = 1 << 6,
165392f7a3SLiteSpeed Tech  PACKET_PUBLIC_FLAGS_TWO_OR_MORE_BYTES = 1 << 7,
175392f7a3SLiteSpeed Tech};
185392f7a3SLiteSpeed Tech
195392f7a3SLiteSpeed Tech#define GQUIC_FRAME_ACKABLE_MASK (                               \
205392f7a3SLiteSpeed Tech    (1 << QUIC_FRAME_STREAM)                                \
215392f7a3SLiteSpeed Tech  | (1 << QUIC_FRAME_RST_STREAM)                            \
225392f7a3SLiteSpeed Tech  | (1 << QUIC_FRAME_GOAWAY)                                \
235392f7a3SLiteSpeed Tech  | (1 << QUIC_FRAME_WINDOW_UPDATE)                         \
245392f7a3SLiteSpeed Tech  | (1 << QUIC_FRAME_PING)                                  \
255392f7a3SLiteSpeed Tech  | (1 << QUIC_FRAME_BLOCKED)                               \
265392f7a3SLiteSpeed Tech  | (1 << QUIC_FRAME_CRYPTO)                                \
275392f7a3SLiteSpeed Tech)
285392f7a3SLiteSpeed Tech
295392f7a3SLiteSpeed Tech#define GQUIC_FRAME_ACKABLE(frame_type) ((1 << (frame_type)) & GQUIC_FRAME_ACKABLE_MASK)
305392f7a3SLiteSpeed Tech
315392f7a3SLiteSpeed Tech#define GQUIC_FRAME_RETRANSMITTABLE_MASK (                       \
325392f7a3SLiteSpeed Tech    (1 << QUIC_FRAME_STREAM)                                \
335392f7a3SLiteSpeed Tech  | (1 << QUIC_FRAME_RST_STREAM)                            \
345392f7a3SLiteSpeed Tech  | (1 << QUIC_FRAME_GOAWAY)                                \
355392f7a3SLiteSpeed Tech  | (1 << QUIC_FRAME_WINDOW_UPDATE)                         \
365392f7a3SLiteSpeed Tech  | (1 << QUIC_FRAME_BLOCKED)                               \
375392f7a3SLiteSpeed Tech  | (1 << QUIC_FRAME_CONNECTION_CLOSE)                      \
385392f7a3SLiteSpeed Tech  | (1 << QUIC_FRAME_CRYPTO)                                \
395392f7a3SLiteSpeed Tech  | (1 << QUIC_FRAME_PING)                                  \
405392f7a3SLiteSpeed Tech)
415392f7a3SLiteSpeed Tech
425392f7a3SLiteSpeed Tech#define GQUIC_FRAME_RETRANSMITTABLE(frame_type) \
435392f7a3SLiteSpeed Tech                        ((1 << (frame_type)) & GQUIC_FRAME_RETRANSMITTABLE_MASK)
445392f7a3SLiteSpeed Tech
455392f7a3SLiteSpeed Tech#define GQUIC_MAX_PUBHDR_SZ (1 /* Type */ + 8 /* CID */ + 4 /* Version */ \
465392f7a3SLiteSpeed Tech                            + 32 /* Nonce */ + 6 /* Packet Number */ )
475392f7a3SLiteSpeed Tech
485392f7a3SLiteSpeed Tech#define GQUIC_MIN_PUBHDR_SZ (1 /* Type */ + 1 /* Packet number */)
495392f7a3SLiteSpeed Tech
505392f7a3SLiteSpeed Tech#define GQUIC_IETF_LONG_HEADER_SIZE (1 /* Type */ + 4 /* Version */ \
515392f7a3SLiteSpeed Tech                + 1 /* DCIL/SCIL */ + 8 /* CID */ + 4 /* Packet number */)
525392f7a3SLiteSpeed Tech
535392f7a3SLiteSpeed Tech/* XXX Nonce? */
545392f7a3SLiteSpeed Tech#define IQUIC_MAX_PUBHDR_SZ GQUIC_IETF_LONG_HEADER_SIZE
555392f7a3SLiteSpeed Tech
565392f7a3SLiteSpeed Tech#define IQUIC_MIN_PUBHDR_SZ (1 /* Type */ + 8 /* CID */ \
575392f7a3SLiteSpeed Tech                                                + 1 /* Packet number */)
585392f7a3SLiteSpeed Tech
595392f7a3SLiteSpeed Tech#define QUIC_MAX_PUBHDR_SZ (GQUIC_MAX_PUBHDR_SZ > IQUIC_MAX_PUBHDR_SZ \
605392f7a3SLiteSpeed Tech                                ? GQUIC_MAX_PUBHDR_SZ : IQUIC_MAX_PUBHDR_SZ)
615392f7a3SLiteSpeed Tech
625392f7a3SLiteSpeed Tech#define QUIC_MIN_PUBHDR_SZ (GQUIC_MIN_PUBHDR_SZ < IQUIC_MIN_PUBHDR_SZ \
635392f7a3SLiteSpeed Tech                                ? GQUIC_MIN_PUBHDR_SZ : IQUIC_MIN_PUBHDR_SZ)
645392f7a3SLiteSpeed Tech
655392f7a3SLiteSpeed Tech/* 12 bytes of FNV hash or encryption IV */
665392f7a3SLiteSpeed Tech#define GQUIC_PACKET_HASH_SZ 12
675392f7a3SLiteSpeed Tech
685392f7a3SLiteSpeed Tech/* [draft-hamilton-quic-transport-protocol-01], Section 7 */
695392f7a3SLiteSpeed Tech#define GQUIC_MAX_IPv4_PACKET_SZ 1370
705392f7a3SLiteSpeed Tech#define GQUIC_MAX_IPv6_PACKET_SZ 1350
715392f7a3SLiteSpeed Tech
725392f7a3SLiteSpeed Tech#define GQUIC_MAX_PACKET_SZ (GQUIC_MAX_IPv4_PACKET_SZ > \
735392f7a3SLiteSpeed Tech    GQUIC_MAX_IPv6_PACKET_SZ ? GQUIC_MAX_IPv4_PACKET_SZ : GQUIC_MAX_IPv6_PACKET_SZ)
745392f7a3SLiteSpeed Tech
755392f7a3SLiteSpeed Tech#define GQUIC_MIN_PACKET_OVERHEAD (GQUIC_PACKET_HASH_SZ + GQUIC_MIN_PUBHDR_SZ)
765392f7a3SLiteSpeed Tech
775392f7a3SLiteSpeed Tech#define GQUIC_MAX_PAYLOAD_SZ (GQUIC_MAX_PACKET_SZ - GQUIC_MIN_PACKET_OVERHEAD)
785392f7a3SLiteSpeed Tech
795392f7a3SLiteSpeed Tech#define GQUIC_WUF_SZ 13  /* Type (1) + Stream ID (4) + Offset (8) */
805392f7a3SLiteSpeed Tech#define GQUIC_BLOCKED_FRAME_SZ 5  /* Type (1) + Stream ID (4) */
815392f7a3SLiteSpeed Tech#define GQUIC_RST_STREAM_SZ 17    /* Type (1) + Stream ID (4) + Offset (8) +
825392f7a3SLiteSpeed Tech                                                            Error code (4) */
835392f7a3SLiteSpeed Tech#define GQUIC_GOAWAY_FRAME_SZ 11  /* Type (1) + Error code (4) + Stream ID (4) +
845392f7a3SLiteSpeed Tech                                                Reason phrase length (2) */
855392f7a3SLiteSpeed Tech
865392f7a3SLiteSpeed Tech#define gquic_packno_bits2len(b) (((b) << 1) + !(b))
875392f7a3SLiteSpeed Tech
885392f7a3SLiteSpeed Techlsquic_packno_t
89a5fa05f9SDmitri Tikhonovlsquic_restore_packno (lsquic_packno_t cur_packno,
905392f7a3SLiteSpeed Tech                unsigned packet_len,
915392f7a3SLiteSpeed Tech                lsquic_packno_t max_packno);
925392f7a3SLiteSpeed Tech
935392f7a3SLiteSpeed Tech#endif
94