lsquic_parse_common.c revision 06b2a236
1/* Copyright (c) 2017 - 2021 LiteSpeed Technologies Inc.  See LICENSE. */
2#include <assert.h>
3#include <string.h>
4#include <sys/queue.h>
5#include <sys/types.h>
6
7#include "lsquic.h"
8#include "lsquic_types.h"
9#include "lsquic_int_types.h"
10#include "lsquic_packet_common.h"
11#include "lsquic_packet_in.h"
12#include "lsquic_parse_common.h"
13#include "lsquic_parse.h"
14#include "lsquic_enc_sess.h"
15#include "lsquic_version.h"
16#include "lsquic_qtags.h"
17
18
19static int
20parse_ietf_v1_or_Q046plus_long_begin (struct lsquic_packet_in *packet_in,
21                size_t length, int is_server, unsigned cid_len,
22                struct packin_parse_state *state)
23{
24    lsquic_ver_tag_t tag;
25
26    if (length >= 5)
27    {
28        memcpy(&tag, packet_in->pi_data + 1, 4);
29        switch (tag)
30        {
31        case TAG('Q', '0', '4', '6'):
32            return lsquic_Q046_parse_packet_in_long_begin(packet_in, length,
33                                                    is_server, cid_len, state);
34        case TAG('Q', '0', '5', '0'):
35            return lsquic_Q050_parse_packet_in_long_begin(packet_in, length,
36                                                    is_server, cid_len, state);
37        default:
38            return lsquic_ietf_v1_parse_packet_in_long_begin(packet_in, length,
39                                                    is_server, cid_len, state);
40        }
41    }
42    else
43        return -1;
44}
45
46
47static int (* const parse_begin_funcs[32]) (struct lsquic_packet_in *,
48                size_t length, int is_server, unsigned cid_len,
49                struct packin_parse_state *) =
50{
51    /* Xs vary, Gs are iGnored: */
52#define PBEL(mask) [(mask) >> 3]
53    /* 1X11 XGGG: */
54    PBEL(0x80|0x40|0x20|0x10|0x08)  = lsquic_Q046_parse_packet_in_long_begin,
55    PBEL(0x80|0x00|0x20|0x10|0x08)  = lsquic_Q046_parse_packet_in_long_begin,
56    PBEL(0x80|0x40|0x20|0x10|0x00)  = lsquic_Q046_parse_packet_in_long_begin,
57    PBEL(0x80|0x00|0x20|0x10|0x00)  = lsquic_Q046_parse_packet_in_long_begin,
58    /* 1X00 XGGG: */
59    PBEL(0x80|0x40|0x00|0x00|0x08)  = parse_ietf_v1_or_Q046plus_long_begin,
60    PBEL(0x80|0x00|0x00|0x00|0x08)  = parse_ietf_v1_or_Q046plus_long_begin,
61    PBEL(0x80|0x40|0x00|0x00|0x00)  = parse_ietf_v1_or_Q046plus_long_begin,
62    PBEL(0x80|0x00|0x00|0x00|0x00)  = parse_ietf_v1_or_Q046plus_long_begin,
63    /* 1X01 XGGG: */
64    PBEL(0x80|0x40|0x00|0x10|0x08)  = parse_ietf_v1_or_Q046plus_long_begin,
65    PBEL(0x80|0x00|0x00|0x10|0x08)  = parse_ietf_v1_or_Q046plus_long_begin,
66    PBEL(0x80|0x40|0x00|0x10|0x00)  = parse_ietf_v1_or_Q046plus_long_begin,
67    PBEL(0x80|0x00|0x00|0x10|0x00)  = parse_ietf_v1_or_Q046plus_long_begin,
68    /* 1X10 XGGG: */
69    PBEL(0x80|0x40|0x20|0x00|0x08)  = parse_ietf_v1_or_Q046plus_long_begin,
70    PBEL(0x80|0x00|0x20|0x00|0x08)  = parse_ietf_v1_or_Q046plus_long_begin,
71    PBEL(0x80|0x40|0x20|0x00|0x00)  = parse_ietf_v1_or_Q046plus_long_begin,
72    PBEL(0x80|0x00|0x20|0x00|0x00)  = parse_ietf_v1_or_Q046plus_long_begin,
73    /* 01XX XGGG */
74    PBEL(0x00|0x40|0x00|0x00|0x00)  = lsquic_ietf_v1_parse_packet_in_short_begin,
75    PBEL(0x00|0x40|0x00|0x00|0x08)  = lsquic_ietf_v1_parse_packet_in_short_begin,
76    PBEL(0x00|0x40|0x00|0x10|0x00)  = lsquic_ietf_v1_parse_packet_in_short_begin,
77    PBEL(0x00|0x40|0x00|0x10|0x08)  = lsquic_ietf_v1_parse_packet_in_short_begin,
78    PBEL(0x00|0x40|0x20|0x00|0x00)  = lsquic_ietf_v1_parse_packet_in_short_begin,
79    PBEL(0x00|0x40|0x20|0x00|0x08)  = lsquic_ietf_v1_parse_packet_in_short_begin,
80    PBEL(0x00|0x40|0x20|0x10|0x00)  = lsquic_ietf_v1_parse_packet_in_short_begin,
81    PBEL(0x00|0x40|0x20|0x10|0x08)  = lsquic_ietf_v1_parse_packet_in_short_begin,
82    /* 00XX 0GGG */
83    PBEL(0x00|0x00|0x00|0x00|0x00)  = lsquic_Q046_parse_packet_in_short_begin,
84    PBEL(0x00|0x00|0x00|0x10|0x00)  = lsquic_Q046_parse_packet_in_short_begin,
85    PBEL(0x00|0x00|0x20|0x00|0x00)  = lsquic_Q046_parse_packet_in_short_begin,
86    PBEL(0x00|0x00|0x20|0x10|0x00)  = lsquic_Q046_parse_packet_in_short_begin,
87    /* 00XX 1GGG */
88    PBEL(0x00|0x00|0x00|0x00|0x08)  = lsquic_gquic_parse_packet_in_begin,
89    PBEL(0x00|0x00|0x00|0x10|0x08)  = lsquic_gquic_parse_packet_in_begin,
90    PBEL(0x00|0x00|0x20|0x00|0x08)  = lsquic_gquic_parse_packet_in_begin,
91    PBEL(0x00|0x00|0x20|0x10|0x08)  = lsquic_gquic_parse_packet_in_begin,
92#undef PBEL
93};
94
95
96int
97lsquic_parse_packet_in_server_begin (struct lsquic_packet_in *packet_in,
98                    size_t length, int is_server_UNUSED, unsigned cid_len,
99                    struct packin_parse_state *state)
100{
101    if (length)
102        return parse_begin_funcs[ packet_in->pi_data[0] >> 3 ](
103                                    packet_in, length, 1, cid_len, state);
104    else
105        return -1;
106
107}
108
109
110int
111lsquic_parse_packet_in_begin (lsquic_packet_in_t *packet_in, size_t length,
112            int is_server, unsigned cid_len, struct packin_parse_state *state)
113{
114    if (length > 0)
115    {
116        switch (packet_in->pi_data[0] & 0xC0)
117        {
118        case 0xC0:
119        case 0x80:
120            return parse_ietf_v1_or_Q046plus_long_begin(packet_in,
121                                        length, is_server, cid_len, state);
122        case 0x00:
123            return lsquic_gquic_parse_packet_in_begin(packet_in, length,
124                                                    is_server, cid_len, state);
125        default:
126            return lsquic_ietf_v1_parse_packet_in_short_begin(packet_in,
127                                        length, is_server, cid_len, state);
128        }
129    }
130    else
131        return -1;
132}
133
134
135int
136lsquic_ietf_v1_parse_packet_in_begin (struct lsquic_packet_in *packet_in,
137            size_t length, int is_server, unsigned cid_len,
138            struct packin_parse_state *state)
139{
140    if (length > 0)
141    {
142        if (0 == (packet_in->pi_data[0] & 0x80))
143            return lsquic_ietf_v1_parse_packet_in_short_begin(packet_in, length,
144                                                    is_server, cid_len, state);
145        else
146            return lsquic_ietf_v1_parse_packet_in_long_begin(packet_in, length,
147                                                    is_server, cid_len, state);
148    }
149    else
150        return -1;
151}
152
153
154int
155lsquic_Q046_parse_packet_in_begin (struct lsquic_packet_in *packet_in,
156            size_t length, int is_server, unsigned cid_len,
157            struct packin_parse_state *state)
158{
159    assert(!is_server);
160    assert(cid_len == GQUIC_CID_LEN);
161    if (length > 0)
162    {
163        if (0 == (packet_in->pi_data[0] & 0x80))
164            return lsquic_ietf_v1_parse_packet_in_short_begin(packet_in, length,
165                                    is_server, is_server ? cid_len : 0, state);
166        else
167            return lsquic_Q046_parse_packet_in_long_begin(packet_in, length,
168                                                    is_server, cid_len, state);
169    }
170    else
171        return -1;
172}
173
174
175int
176lsquic_Q050_parse_packet_in_begin (struct lsquic_packet_in *packet_in,
177            size_t length, int is_server, unsigned cid_len,
178            struct packin_parse_state *state)
179{
180    assert(!is_server);
181    assert(cid_len == GQUIC_CID_LEN);
182    if (length > 0)
183    {
184        if (0 == (packet_in->pi_data[0] & 0x80))
185            return lsquic_ietf_v1_parse_packet_in_short_begin(packet_in, length,
186                                    is_server, is_server ? cid_len : 0, state);
187        else
188            return lsquic_Q050_parse_packet_in_long_begin(packet_in, length,
189                                                    is_server, cid_len, state);
190    }
191    else
192        return -1;
193}
194
195
196/* TODO This function uses the full packet parsing functionality to get at
197 * the CID.  This is an overkill and could be optimized -- at the cost of
198 * some code duplication, of course.
199 */
200int
201lsquic_cid_from_packet (const unsigned char *buf, size_t bufsz,
202                                                            lsquic_cid_t *cid)
203{
204    struct lsquic_packet_in packet_in;
205    struct packin_parse_state pps;
206    int s;
207
208    packet_in.pi_data = (unsigned char *) buf;
209    s = lsquic_parse_packet_in_server_begin(&packet_in, bufsz, 1, 8, &pps);
210    if (0 == s && (packet_in.pi_flags & PI_CONN_ID))
211    {
212        *cid = packet_in.pi_dcid;
213        return 0;
214    }
215    else
216        return -1;
217}
218
219
220/* See [draft-ietf-quic-transport-28], Section 12.4 (Table 3) */
221const enum quic_ft_bit lsquic_legal_frames_by_level[N_LSQVER][N_ENC_LEVS] =
222{
223    [LSQVER_ID32] = {
224    [ENC_LEV_CLEAR] = QUIC_FTBIT_CRYPTO | QUIC_FTBIT_PADDING | QUIC_FTBIT_PING
225                    | QUIC_FTBIT_ACK | QUIC_FTBIT_CONNECTION_CLOSE,
226    [ENC_LEV_EARLY] = QUIC_FTBIT_PADDING | QUIC_FTBIT_PING
227                    | QUIC_FTBIT_STREAM | QUIC_FTBIT_RST_STREAM
228                    | QUIC_FTBIT_BLOCKED | QUIC_FTBIT_CONNECTION_CLOSE
229                    | QUIC_FTBIT_MAX_DATA | QUIC_FTBIT_MAX_STREAM_DATA
230                    | QUIC_FTBIT_MAX_STREAMS | QUIC_FTBIT_STREAM_BLOCKED
231                    | QUIC_FTBIT_STREAMS_BLOCKED
232                    | QUIC_FTBIT_NEW_CONNECTION_ID | QUIC_FTBIT_STOP_SENDING
233                    | QUIC_FTBIT_PATH_CHALLENGE | QUIC_FTBIT_PATH_RESPONSE
234                    | QUIC_FTBIT_DATAGRAM
235                    | QUIC_FTBIT_RETIRE_CONNECTION_ID,
236    [ENC_LEV_INIT]  = QUIC_FTBIT_CRYPTO | QUIC_FTBIT_PADDING | QUIC_FTBIT_PING
237                    | QUIC_FTBIT_ACK| QUIC_FTBIT_CONNECTION_CLOSE,
238    [ENC_LEV_FORW]  = QUIC_FTBIT_CRYPTO | QUIC_FTBIT_PADDING | QUIC_FTBIT_PING
239                    | QUIC_FTBIT_ACK | QUIC_FTBIT_CONNECTION_CLOSE
240                    | QUIC_FTBIT_STREAM | QUIC_FTBIT_RST_STREAM
241                    | QUIC_FTBIT_BLOCKED
242                    | QUIC_FTBIT_MAX_DATA | QUIC_FTBIT_MAX_STREAM_DATA
243                    | QUIC_FTBIT_MAX_STREAMS | QUIC_FTBIT_STREAM_BLOCKED
244                    | QUIC_FTBIT_STREAMS_BLOCKED
245                    | QUIC_FTBIT_NEW_CONNECTION_ID | QUIC_FTBIT_STOP_SENDING
246                    | QUIC_FTBIT_PATH_CHALLENGE | QUIC_FTBIT_PATH_RESPONSE
247                    | QUIC_FTBIT_HANDSHAKE_DONE | QUIC_FTBIT_ACK_FREQUENCY
248                    | QUIC_FTBIT_RETIRE_CONNECTION_ID | QUIC_FTBIT_NEW_TOKEN
249                    | QUIC_FTBIT_TIMESTAMP
250                    | QUIC_FTBIT_DATAGRAM
251                    ,
252    },
253    [LSQVER_ID29] = {
254    [ENC_LEV_CLEAR] = QUIC_FTBIT_CRYPTO | QUIC_FTBIT_PADDING | QUIC_FTBIT_PING
255                    | QUIC_FTBIT_ACK | QUIC_FTBIT_CONNECTION_CLOSE,
256    [ENC_LEV_EARLY] = QUIC_FTBIT_PADDING | QUIC_FTBIT_PING
257                    | QUIC_FTBIT_STREAM | QUIC_FTBIT_RST_STREAM
258                    | QUIC_FTBIT_BLOCKED | QUIC_FTBIT_CONNECTION_CLOSE
259                    | QUIC_FTBIT_MAX_DATA | QUIC_FTBIT_MAX_STREAM_DATA
260                    | QUIC_FTBIT_MAX_STREAMS | QUIC_FTBIT_STREAM_BLOCKED
261                    | QUIC_FTBIT_STREAMS_BLOCKED
262                    | QUIC_FTBIT_NEW_CONNECTION_ID | QUIC_FTBIT_STOP_SENDING
263                    | QUIC_FTBIT_PATH_CHALLENGE | QUIC_FTBIT_PATH_RESPONSE
264                    | QUIC_FTBIT_DATAGRAM
265                    | QUIC_FTBIT_RETIRE_CONNECTION_ID,
266    [ENC_LEV_INIT]  = QUIC_FTBIT_CRYPTO | QUIC_FTBIT_PADDING | QUIC_FTBIT_PING
267                    | QUIC_FTBIT_ACK| QUIC_FTBIT_CONNECTION_CLOSE,
268    [ENC_LEV_FORW]  = QUIC_FTBIT_CRYPTO | QUIC_FTBIT_PADDING | QUIC_FTBIT_PING
269                    | QUIC_FTBIT_ACK | QUIC_FTBIT_CONNECTION_CLOSE
270                    | QUIC_FTBIT_STREAM | QUIC_FTBIT_RST_STREAM
271                    | QUIC_FTBIT_BLOCKED
272                    | QUIC_FTBIT_MAX_DATA | QUIC_FTBIT_MAX_STREAM_DATA
273                    | QUIC_FTBIT_MAX_STREAMS | QUIC_FTBIT_STREAM_BLOCKED
274                    | QUIC_FTBIT_STREAMS_BLOCKED
275                    | QUIC_FTBIT_NEW_CONNECTION_ID | QUIC_FTBIT_STOP_SENDING
276                    | QUIC_FTBIT_PATH_CHALLENGE | QUIC_FTBIT_PATH_RESPONSE
277                    | QUIC_FTBIT_HANDSHAKE_DONE | QUIC_FTBIT_ACK_FREQUENCY
278                    | QUIC_FTBIT_RETIRE_CONNECTION_ID | QUIC_FTBIT_NEW_TOKEN
279                    | QUIC_FTBIT_TIMESTAMP
280                    | QUIC_FTBIT_DATAGRAM
281                    ,
282    },
283    [LSQVER_ID28] = {
284    [ENC_LEV_CLEAR] = QUIC_FTBIT_CRYPTO | QUIC_FTBIT_PADDING | QUIC_FTBIT_PING
285                    | QUIC_FTBIT_ACK | QUIC_FTBIT_CONNECTION_CLOSE,
286    [ENC_LEV_EARLY] = QUIC_FTBIT_PADDING | QUIC_FTBIT_PING
287                    | QUIC_FTBIT_STREAM | QUIC_FTBIT_RST_STREAM
288                    | QUIC_FTBIT_BLOCKED | QUIC_FTBIT_CONNECTION_CLOSE
289                    | QUIC_FTBIT_MAX_DATA | QUIC_FTBIT_MAX_STREAM_DATA
290                    | QUIC_FTBIT_MAX_STREAMS | QUIC_FTBIT_STREAM_BLOCKED
291                    | QUIC_FTBIT_STREAMS_BLOCKED
292                    | QUIC_FTBIT_NEW_CONNECTION_ID | QUIC_FTBIT_STOP_SENDING
293                    | QUIC_FTBIT_PATH_CHALLENGE | QUIC_FTBIT_PATH_RESPONSE
294                    | QUIC_FTBIT_RETIRE_CONNECTION_ID,
295    [ENC_LEV_INIT]  = QUIC_FTBIT_CRYPTO | QUIC_FTBIT_PADDING | QUIC_FTBIT_PING
296                    | QUIC_FTBIT_ACK| QUIC_FTBIT_CONNECTION_CLOSE,
297    [ENC_LEV_FORW]  = QUIC_FTBIT_CRYPTO | QUIC_FTBIT_PADDING | QUIC_FTBIT_PING
298                    | QUIC_FTBIT_ACK | QUIC_FTBIT_CONNECTION_CLOSE
299                    | QUIC_FTBIT_STREAM | QUIC_FTBIT_RST_STREAM
300                    | QUIC_FTBIT_BLOCKED
301                    | QUIC_FTBIT_MAX_DATA | QUIC_FTBIT_MAX_STREAM_DATA
302                    | QUIC_FTBIT_MAX_STREAMS | QUIC_FTBIT_STREAM_BLOCKED
303                    | QUIC_FTBIT_STREAMS_BLOCKED
304                    | QUIC_FTBIT_NEW_CONNECTION_ID | QUIC_FTBIT_STOP_SENDING
305                    | QUIC_FTBIT_PATH_CHALLENGE | QUIC_FTBIT_PATH_RESPONSE
306                    | QUIC_FTBIT_HANDSHAKE_DONE | QUIC_FTBIT_ACK_FREQUENCY
307                    | QUIC_FTBIT_RETIRE_CONNECTION_ID | QUIC_FTBIT_NEW_TOKEN
308                    | QUIC_FTBIT_TIMESTAMP
309                    ,
310    },
311    [LSQVER_ID27] = {
312    [ENC_LEV_CLEAR] = QUIC_FTBIT_CRYPTO | QUIC_FTBIT_PADDING | QUIC_FTBIT_PING
313                    | QUIC_FTBIT_ACK | QUIC_FTBIT_CONNECTION_CLOSE,
314    [ENC_LEV_EARLY] = QUIC_FTBIT_PADDING | QUIC_FTBIT_PING
315                    | QUIC_FTBIT_STREAM | QUIC_FTBIT_RST_STREAM
316                    | QUIC_FTBIT_BLOCKED
317                    | QUIC_FTBIT_MAX_DATA | QUIC_FTBIT_MAX_STREAM_DATA
318                    | QUIC_FTBIT_MAX_STREAMS | QUIC_FTBIT_STREAM_BLOCKED
319                    | QUIC_FTBIT_STREAMS_BLOCKED
320                    | QUIC_FTBIT_NEW_CONNECTION_ID | QUIC_FTBIT_STOP_SENDING
321                    | QUIC_FTBIT_PATH_CHALLENGE | QUIC_FTBIT_PATH_RESPONSE
322                    | QUIC_FTBIT_RETIRE_CONNECTION_ID
323                    | QUIC_FTBIT_DATAGRAM
324                    ,
325    [ENC_LEV_INIT]  = QUIC_FTBIT_CRYPTO | QUIC_FTBIT_PADDING | QUIC_FTBIT_PING
326                    | QUIC_FTBIT_ACK| QUIC_FTBIT_CONNECTION_CLOSE,
327    [ENC_LEV_FORW]  = QUIC_FTBIT_CRYPTO | QUIC_FTBIT_PADDING | QUIC_FTBIT_PING
328                    | QUIC_FTBIT_ACK | QUIC_FTBIT_CONNECTION_CLOSE
329                    | QUIC_FTBIT_STREAM | QUIC_FTBIT_RST_STREAM
330                    | QUIC_FTBIT_BLOCKED
331                    | QUIC_FTBIT_MAX_DATA | QUIC_FTBIT_MAX_STREAM_DATA
332                    | QUIC_FTBIT_MAX_STREAMS | QUIC_FTBIT_STREAM_BLOCKED
333                    | QUIC_FTBIT_STREAMS_BLOCKED
334                    | QUIC_FTBIT_NEW_CONNECTION_ID | QUIC_FTBIT_STOP_SENDING
335                    | QUIC_FTBIT_PATH_CHALLENGE | QUIC_FTBIT_PATH_RESPONSE
336                    | QUIC_FTBIT_HANDSHAKE_DONE | QUIC_FTBIT_ACK_FREQUENCY
337                    | QUIC_FTBIT_RETIRE_CONNECTION_ID | QUIC_FTBIT_NEW_TOKEN
338                    | QUIC_FTBIT_TIMESTAMP
339                    | QUIC_FTBIT_DATAGRAM
340                    ,
341    },
342};
343