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