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