lsquic_packet_common.h revision b1a7c3f9
1/* Copyright (c) 2017 - 2020 LiteSpeed Technologies Inc.  See LICENSE. */
2#ifndef LSQUIC_PACKET_COMMON_H
3#define LSQUIC_PACKET_COMMON_H 1
4
5/* The list of frames contains frames both in GQUIC and in IETF QUIC.
6 * They are marked as follows:
7 *  G   Applicable to GQUIC only
8 *  I   Applicable to IETF QUIC only
9 *  B   Applicable to both GQUIC and IETF QUIC.
10 */
11enum quic_frame_type
12{
13    QUIC_FRAME_INVALID,
14    QUIC_FRAME_STREAM,              /* B */
15    QUIC_FRAME_ACK,                 /* B */
16    QUIC_FRAME_PADDING,             /* B */
17    QUIC_FRAME_RST_STREAM,          /* B */
18    QUIC_FRAME_CONNECTION_CLOSE,    /* B */
19    QUIC_FRAME_GOAWAY,              /* G */
20    QUIC_FRAME_WINDOW_UPDATE,       /* G */
21    QUIC_FRAME_BLOCKED,             /* B */
22    QUIC_FRAME_STOP_WAITING,        /* G */
23    QUIC_FRAME_PING,                /* B */
24    QUIC_FRAME_MAX_DATA,            /* I */
25    QUIC_FRAME_MAX_STREAM_DATA,     /* I */
26    QUIC_FRAME_MAX_STREAMS,         /* I */
27    QUIC_FRAME_STREAM_BLOCKED,      /* I */
28    QUIC_FRAME_STREAMS_BLOCKED,     /* I */
29    QUIC_FRAME_NEW_CONNECTION_ID,   /* I */
30    QUIC_FRAME_STOP_SENDING,        /* I */
31    QUIC_FRAME_PATH_CHALLENGE,      /* I */
32    QUIC_FRAME_PATH_RESPONSE,       /* I */
33    QUIC_FRAME_CRYPTO,              /* B */
34    QUIC_FRAME_RETIRE_CONNECTION_ID,/* I */
35    QUIC_FRAME_NEW_TOKEN,           /* I */
36    QUIC_FRAME_HANDSHAKE_DONE,      /* I */
37    QUIC_FRAME_ACK_FREQUENCY,       /* I */
38    QUIC_FRAME_TIMESTAMP,           /* I */
39    QUIC_FRAME_DATAGRAM,            /* I */
40    N_QUIC_FRAMES
41};
42
43enum quic_ft_bit {
44    QUIC_FTBIT_INVALID           = 1 << QUIC_FRAME_INVALID,
45    QUIC_FTBIT_STREAM            = 1 << QUIC_FRAME_STREAM,
46    QUIC_FTBIT_ACK               = 1 << QUIC_FRAME_ACK,
47    QUIC_FTBIT_PADDING           = 1 << QUIC_FRAME_PADDING,
48    QUIC_FTBIT_RST_STREAM        = 1 << QUIC_FRAME_RST_STREAM,
49    QUIC_FTBIT_CONNECTION_CLOSE  = 1 << QUIC_FRAME_CONNECTION_CLOSE,
50    QUIC_FTBIT_GOAWAY            = 1 << QUIC_FRAME_GOAWAY,
51    QUIC_FTBIT_WINDOW_UPDATE     = 1 << QUIC_FRAME_WINDOW_UPDATE,
52    QUIC_FTBIT_BLOCKED           = 1 << QUIC_FRAME_BLOCKED,
53    QUIC_FTBIT_STOP_WAITING      = 1 << QUIC_FRAME_STOP_WAITING,
54    QUIC_FTBIT_PING              = 1 << QUIC_FRAME_PING,
55    QUIC_FTBIT_MAX_DATA          = 1 << QUIC_FRAME_MAX_DATA,
56    QUIC_FTBIT_MAX_STREAM_DATA   = 1 << QUIC_FRAME_MAX_STREAM_DATA,
57    QUIC_FTBIT_MAX_STREAMS       = 1 << QUIC_FRAME_MAX_STREAMS,
58    QUIC_FTBIT_STREAM_BLOCKED    = 1 << QUIC_FRAME_STREAM_BLOCKED,
59    QUIC_FTBIT_STREAMS_BLOCKED   = 1 << QUIC_FRAME_STREAMS_BLOCKED,
60    QUIC_FTBIT_NEW_CONNECTION_ID = 1 << QUIC_FRAME_NEW_CONNECTION_ID,
61    QUIC_FTBIT_STOP_SENDING      = 1 << QUIC_FRAME_STOP_SENDING,
62    QUIC_FTBIT_PATH_CHALLENGE    = 1 << QUIC_FRAME_PATH_CHALLENGE,
63    QUIC_FTBIT_PATH_RESPONSE     = 1 << QUIC_FRAME_PATH_RESPONSE,
64    QUIC_FTBIT_CRYPTO            = 1 << QUIC_FRAME_CRYPTO,
65    QUIC_FTBIT_NEW_TOKEN         = 1 << QUIC_FRAME_NEW_TOKEN,
66    QUIC_FTBIT_RETIRE_CONNECTION_ID = 1 << QUIC_FRAME_RETIRE_CONNECTION_ID,
67    QUIC_FTBIT_HANDSHAKE_DONE    = 1 << QUIC_FRAME_HANDSHAKE_DONE,
68    QUIC_FTBIT_ACK_FREQUENCY     = 1 << QUIC_FRAME_ACK_FREQUENCY,
69    QUIC_FTBIT_TIMESTAMP         = 1 << QUIC_FRAME_TIMESTAMP,
70    QUIC_FTBIT_DATAGRAM          = 1 << QUIC_FRAME_DATAGRAM,
71};
72
73static const char * const frame_type_2_str[N_QUIC_FRAMES] = {
74    [QUIC_FRAME_INVALID]           =  "QUIC_FRAME_INVALID",
75    [QUIC_FRAME_STREAM]            =  "QUIC_FRAME_STREAM",
76    [QUIC_FRAME_ACK]               =  "QUIC_FRAME_ACK",
77    [QUIC_FRAME_PADDING]           =  "QUIC_FRAME_PADDING",
78    [QUIC_FRAME_RST_STREAM]        =  "QUIC_FRAME_RST_STREAM",
79    [QUIC_FRAME_CONNECTION_CLOSE]  =  "QUIC_FRAME_CONNECTION_CLOSE",
80    [QUIC_FRAME_GOAWAY]            =  "QUIC_FRAME_GOAWAY",
81    [QUIC_FRAME_WINDOW_UPDATE]     =  "QUIC_FRAME_WINDOW_UPDATE",
82    [QUIC_FRAME_BLOCKED]           =  "QUIC_FRAME_BLOCKED",
83    [QUIC_FRAME_STOP_WAITING]      =  "QUIC_FRAME_STOP_WAITING",
84    [QUIC_FRAME_PING]              =  "QUIC_FRAME_PING",
85    [QUIC_FRAME_MAX_DATA]          =  "QUIC_FRAME_MAX_DATA",
86    [QUIC_FRAME_MAX_STREAM_DATA]   =  "QUIC_FRAME_MAX_STREAM_DATA",
87    [QUIC_FRAME_MAX_STREAMS]       =  "QUIC_FRAME_MAX_STREAMS",
88    [QUIC_FRAME_STREAM_BLOCKED]    =  "QUIC_FRAME_STREAM_BLOCKED",
89    [QUIC_FRAME_STREAMS_BLOCKED]   =  "QUIC_FRAME_STREAMS_BLOCKED",
90    [QUIC_FRAME_NEW_CONNECTION_ID] =  "QUIC_FRAME_NEW_CONNECTION_ID",
91    [QUIC_FRAME_STOP_SENDING]      =  "QUIC_FRAME_STOP_SENDING",
92    [QUIC_FRAME_PATH_CHALLENGE]    =  "QUIC_FRAME_PATH_CHALLENGE",
93    [QUIC_FRAME_PATH_RESPONSE]     =  "QUIC_FRAME_PATH_RESPONSE",
94    [QUIC_FRAME_CRYPTO]            =  "QUIC_FRAME_CRYPTO",
95    [QUIC_FRAME_NEW_TOKEN]         =  "QUIC_FRAME_NEW_TOKEN",
96    [QUIC_FRAME_RETIRE_CONNECTION_ID]  =  "QUIC_FRAME_RETIRE_CONNECTION_ID",
97    [QUIC_FRAME_HANDSHAKE_DONE]    =  "QUIC_FRAME_HANDSHAKE_DONE",
98    [QUIC_FRAME_ACK_FREQUENCY]     =  "QUIC_FRAME_ACK_FREQUENCY",
99    [QUIC_FRAME_TIMESTAMP]         =  "QUIC_FRAME_TIMESTAMP",
100    [QUIC_FRAME_DATAGRAM]          =  "QUIC_FRAME_DATAGRAM",
101};
102
103#define QUIC_FRAME_PRELEN   (sizeof("QUIC_FRAME_"))
104#define QUIC_FRAME_SLEN(x)  (sizeof(#x) - QUIC_FRAME_PRELEN)
105#define QUIC_FRAME_NAME(i)  (frame_type_2_str[i] + QUIC_FRAME_PRELEN - 1)
106
107
108    /* We don't need to include INVALID frame in this list because it is
109     * never a part of any frame list bitmask (e.g. po_frame_types).
110     */
111#define lsquic_frame_types_str_sz  \
112    QUIC_FRAME_SLEN(QUIC_FRAME_STREAM)            + 1 + \
113    QUIC_FRAME_SLEN(QUIC_FRAME_ACK)               + 1 + \
114    QUIC_FRAME_SLEN(QUIC_FRAME_PADDING)           + 1 + \
115    QUIC_FRAME_SLEN(QUIC_FRAME_RST_STREAM)        + 1 + \
116    QUIC_FRAME_SLEN(QUIC_FRAME_CONNECTION_CLOSE)  + 1 + \
117    QUIC_FRAME_SLEN(QUIC_FRAME_GOAWAY)            + 1 + \
118    QUIC_FRAME_SLEN(QUIC_FRAME_WINDOW_UPDATE)     + 1 + \
119    QUIC_FRAME_SLEN(QUIC_FRAME_BLOCKED)           + 1 + \
120    QUIC_FRAME_SLEN(QUIC_FRAME_STOP_WAITING)      + 1 + \
121    QUIC_FRAME_SLEN(QUIC_FRAME_PING)              + 1 + \
122    QUIC_FRAME_SLEN(QUIC_FRAME_MAX_DATA)          + 1 + \
123    QUIC_FRAME_SLEN(QUIC_FRAME_MAX_STREAM_DATA)   + 1 + \
124    QUIC_FRAME_SLEN(QUIC_FRAME_MAX_STREAMS)       + 1 + \
125    QUIC_FRAME_SLEN(QUIC_FRAME_STREAM_BLOCKED)    + 1 + \
126    QUIC_FRAME_SLEN(QUIC_FRAME_STREAMS_BLOCKED)   + 1 + \
127    QUIC_FRAME_SLEN(QUIC_FRAME_NEW_CONNECTION_ID) + 1 + \
128    QUIC_FRAME_SLEN(QUIC_FRAME_STOP_SENDING)      + 1 + \
129    QUIC_FRAME_SLEN(QUIC_FRAME_PATH_CHALLENGE)    + 1 + \
130    QUIC_FRAME_SLEN(QUIC_FRAME_PATH_RESPONSE)     + 1 + \
131    QUIC_FRAME_SLEN(QUIC_FRAME_CRYPTO)            + 1 + \
132    QUIC_FRAME_SLEN(QUIC_FRAME_RETIRE_CONNECTION_ID) \
133                                                  + 1 + \
134    QUIC_FRAME_SLEN(QUIC_FRAME_NEW_TOKEN)         + 1 + \
135    QUIC_FRAME_SLEN(QUIC_FRAME_HANDSHAKE_DONE)    + 1 + \
136    QUIC_FRAME_SLEN(QUIC_FRAME_ACK_FREQUENCY)     + 1 + \
137    QUIC_FRAME_SLEN(QUIC_FRAME_TIMESTAMP)         + 1 + \
138    QUIC_FRAME_SLEN(QUIC_FRAME_DATAGRAM)          + 1 + \
139    0
140
141
142const char *
143lsquic_frame_types_to_str (char *buf, size_t bufsz, enum quic_ft_bit);
144
145/* This value represents a different number of bytes used to encode the packet
146 * length based on whether GQUIC or IQUIC is used.
147 */
148enum packno_bits
149{
150    PACKNO_BITS_0 = 0,
151    PACKNO_BITS_1 = 1,
152    PACKNO_BITS_2 = 2,
153    PACKNO_BITS_3 = 3,
154};
155
156
157/* GQUIC maps 0, 1, 2, 3 -> 1, 2, 4, 6 */
158enum
159{
160    GQUIC_PACKNO_LEN_1 = PACKNO_BITS_0,
161    GQUIC_PACKNO_LEN_2 = PACKNO_BITS_1,
162    GQUIC_PACKNO_LEN_4 = PACKNO_BITS_2,
163    GQUIC_PACKNO_LEN_6 = PACKNO_BITS_3,
164};
165
166
167/* IQUIC maps 0, 1, 2, 3 -> 1, 2, 3, 4 (as of ID-17) */
168enum
169{
170    IQUIC_PACKNO_LEN_1 = PACKNO_BITS_0,
171    IQUIC_PACKNO_LEN_2 = PACKNO_BITS_1,
172    IQUIC_PACKNO_LEN_3 = PACKNO_BITS_2,
173    IQUIC_PACKNO_LEN_4 = PACKNO_BITS_3,
174};
175
176
177enum header_type
178{
179    HETY_NOT_SET,       /* This value must be zero */
180    HETY_VERNEG,
181    HETY_INITIAL,
182    HETY_RETRY,
183    HETY_HANDSHAKE,
184    HETY_0RTT,
185};
186
187extern const char *const lsquic_hety2str[];
188
189#define IQUIC_MAX_PACKNO ((1ULL << 62) - 1)
190#define IQUIC_INVALID_PACKNO (IQUIC_MAX_PACKNO + 1)
191
192/* IETF QUIC only: */
193#define is_valid_packno(packno) ((packno) <= IQUIC_MAX_PACKNO)
194
195enum packnum_space
196{
197    PNS_INIT,
198    PNS_HSK,
199    PNS_APP,
200    N_PNS
201};
202
203extern const enum packnum_space lsquic_hety2pns[];
204extern const enum packnum_space lsquic_enclev2pns[];
205extern const char *const lsquic_pns2str[];
206
207#define ALL_IQUIC_FRAMES (           \
208       QUIC_FTBIT_STREAM             \
209    |  QUIC_FTBIT_ACK                \
210    |  QUIC_FTBIT_PADDING            \
211    |  QUIC_FTBIT_RST_STREAM         \
212    |  QUIC_FTBIT_CONNECTION_CLOSE   \
213    |  QUIC_FTBIT_BLOCKED            \
214    |  QUIC_FTBIT_PING               \
215    |  QUIC_FTBIT_MAX_DATA           \
216    |  QUIC_FTBIT_MAX_STREAM_DATA    \
217    |  QUIC_FTBIT_MAX_STREAMS        \
218    |  QUIC_FTBIT_STREAM_BLOCKED     \
219    |  QUIC_FTBIT_STREAMS_BLOCKED    \
220    |  QUIC_FTBIT_NEW_CONNECTION_ID  \
221    |  QUIC_FTBIT_STOP_SENDING       \
222    |  QUIC_FTBIT_PATH_CHALLENGE     \
223    |  QUIC_FTBIT_PATH_RESPONSE      \
224    |  QUIC_FTBIT_RETIRE_CONNECTION_ID      \
225    |  QUIC_FTBIT_NEW_TOKEN          \
226    |  QUIC_FTBIT_HANDSHAKE_DONE     \
227    |  QUIC_FTBIT_ACK_FREQUENCY      \
228    |  QUIC_FTBIT_TIMESTAMP          \
229    |  QUIC_FTBIT_CRYPTO             )
230
231/* [draft-ietf-quic-transport-24] Section 1.2 */
232#define IQUIC_FRAME_ACKABLE_MASK (  \
233    ALL_IQUIC_FRAMES & ~(QUIC_FTBIT_ACK|QUIC_FTBIT_PADDING\
234                            |QUIC_FTBIT_CONNECTION_CLOSE|QUIC_FTBIT_TIMESTAMP))
235
236/* [draft-ietf-quic-transport-20], Section 13.2 */
237/* We bend some rules and retransmit BLOCKED, MAX_DATA, MAX_STREAM_DATA,
238 * MAX_STREAMS, STREAM_BLOCKED, and STREAMS_BLOCKED frames instead of
239 * regenerating them.  This keeps the code simple(r).
240 */
241#define IQUIC_FRAME_RETX_MASK (  \
242    ALL_IQUIC_FRAMES & ~(QUIC_FTBIT_PADDING|QUIC_FTBIT_PATH_RESPONSE    \
243            |QUIC_FTBIT_PATH_CHALLENGE|QUIC_FTBIT_ACK|QUIC_FTBIT_TIMESTAMP))
244
245extern const enum quic_ft_bit lsquic_legal_frames_by_level[][4];
246
247/* Applies both to gQUIC and IETF QUIC, thus "B" for "both" */
248#define BQUIC_FRAME_REGEN_MASK ((1 << QUIC_FRAME_ACK)                \
249  | (1 << QUIC_FRAME_PATH_CHALLENGE) | (1 << QUIC_FRAME_PATH_RESPONSE) \
250  | (1 << QUIC_FRAME_STOP_WAITING) | (1 << QUIC_FRAME_TIMESTAMP))
251
252#endif
253