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