1/* Copyright (c) 2017 - 2022 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
220int
221lsquic_dcid_from_packet (const unsigned char *buf, size_t bufsz,
222                                unsigned server_cid_len, unsigned *cid_len)
223{
224    const unsigned char *p;
225    unsigned dcil, scil;
226
227    if (bufsz < 9)
228        return -1;
229
230    switch (buf[0] >> 3)
231    {
232    /* Xs vary, Gs are iGnored: */
233    /* 1X11 XGGG: */
234    case (0x80|0x40|0x20|0x10|0x08) >> 3:
235    case (0x80|0x00|0x20|0x10|0x08) >> 3:
236    case (0x80|0x40|0x20|0x10|0x00) >> 3:
237    case (0x80|0x00|0x20|0x10|0x00) >> 3:
238  Q046_long:
239        /* lsquic_Q046_parse_packet_in_long_begin */
240        if (bufsz < 14)
241            return -1;
242        p = buf + 5;
243        dcil = p[0] >> 4;
244        if (dcil)
245            dcil += 3;
246        scil = p[0] & 0xF;
247        if (scil)
248            scil += 3;
249        ++p;
250        if (dcil == GQUIC_CID_LEN && scil == 0)
251        {
252            *cid_len = GQUIC_CID_LEN;
253            return (unsigned) (p - buf);
254        }
255        else
256            return -1;
257    /* 1X00 XGGG: */
258    /*
259    case (0x80|0x40|0x00|0x00|0x08) >> 3:
260    case (0x80|0x00|0x00|0x00|0x08) >> 3:
261    case (0x80|0x40|0x00|0x00|0x00) >> 3:
262    case (0x80|0x00|0x00|0x00|0x00) >> 3:
263    case (0x80|0x40|0x00|0x10|0x08) >> 3:
264    case (0x80|0x00|0x00|0x10|0x08) >> 3:
265    case (0x80|0x40|0x00|0x10|0x00) >> 3:
266    case (0x80|0x00|0x00|0x10|0x00) >> 3:
267    case (0x80|0x40|0x20|0x00|0x08) >> 3:
268    case (0x80|0x00|0x20|0x00|0x08) >> 3:
269    case (0x80|0x40|0x20|0x00|0x00) >> 3:
270    case (0x80|0x00|0x20|0x00|0x00) >> 3:
271    */
272    default:
273        /* parse_ietf_v1_or_Q046plus_long_begin */
274        if (buf[4] == (unsigned) '6')
275            goto Q046_long;
276        /* lsquic_Q050_parse_packet_in_long_begin or
277            lsquic_ietf_v1_parse_packet_in_long_begin */
278        if (bufsz < 14)
279            return -1;
280        dcil = buf[5];
281        if (dcil <= MAX_CID_LEN && 6 + dcil < bufsz)
282        {
283            *cid_len = dcil;
284            return 6;
285        }
286        else
287            return -1;
288    /* 01XX XGGG */
289    case (0x00|0x40|0x00|0x00|0x00) >> 3:
290    case (0x00|0x40|0x00|0x00|0x08) >> 3:
291    case (0x00|0x40|0x00|0x10|0x00) >> 3:
292    case (0x00|0x40|0x00|0x10|0x08) >> 3:
293    case (0x00|0x40|0x20|0x00|0x00) >> 3:
294    case (0x00|0x40|0x20|0x00|0x08) >> 3:
295    case (0x00|0x40|0x20|0x10|0x00) >> 3:
296    case (0x00|0x40|0x20|0x10|0x08) >> 3:
297        /* lsquic_ietf_v1_parse_packet_in_short_begin */
298        if (1 + server_cid_len <= bufsz)
299        {
300            *cid_len = server_cid_len;
301            return 1;
302        }
303        else
304            return -1;
305    /* 00XX 0GGG */
306    case (0x00|0x00|0x00|0x00|0x00) >> 3:
307    case (0x00|0x00|0x00|0x10|0x00) >> 3:
308    case (0x00|0x00|0x20|0x00|0x00) >> 3:
309    case (0x00|0x00|0x20|0x10|0x00) >> 3:
310        /* lsquic_Q046_parse_packet_in_short_begin */
311        if (1 + server_cid_len <= bufsz && (buf[0] & 0x40))
312        {
313            *cid_len = server_cid_len;
314            return 1;
315        }
316        else
317            return -1;
318    /* 00XX 1GGG */
319    case (0x00|0x00|0x00|0x00|0x08) >> 3:
320    case (0x00|0x00|0x00|0x10|0x08) >> 3:
321    case (0x00|0x00|0x20|0x00|0x08) >> 3:
322    case (0x00|0x00|0x20|0x10|0x08) >> 3:
323        /* lsquic_gquic_parse_packet_in_begin */
324        if (1 + GQUIC_CID_LEN <= bufsz
325                        && (buf[0] & PACKET_PUBLIC_FLAGS_8BYTE_CONNECTION_ID))
326        {
327            *cid_len = server_cid_len;
328            return 1;
329        }
330        else
331            return -1;
332    }
333}
334
335
336/* See [draft-ietf-quic-transport-28], Section 12.4 (Table 3) */
337const enum quic_ft_bit lsquic_legal_frames_by_level[N_LSQVER][N_ENC_LEVS] =
338{
339    [LSQVER_I001] = {
340    [ENC_LEV_CLEAR] = QUIC_FTBIT_CRYPTO | QUIC_FTBIT_PADDING | QUIC_FTBIT_PING
341                    | QUIC_FTBIT_ACK | QUIC_FTBIT_CONNECTION_CLOSE,
342    [ENC_LEV_EARLY] = QUIC_FTBIT_PADDING | QUIC_FTBIT_PING
343                    | QUIC_FTBIT_STREAM | QUIC_FTBIT_RST_STREAM
344                    | QUIC_FTBIT_BLOCKED | QUIC_FTBIT_CONNECTION_CLOSE
345                    | QUIC_FTBIT_MAX_DATA | QUIC_FTBIT_MAX_STREAM_DATA
346                    | QUIC_FTBIT_MAX_STREAMS | QUIC_FTBIT_STREAM_BLOCKED
347                    | QUIC_FTBIT_STREAMS_BLOCKED
348                    | QUIC_FTBIT_NEW_CONNECTION_ID | QUIC_FTBIT_STOP_SENDING
349                    | QUIC_FTBIT_PATH_CHALLENGE
350                    | QUIC_FTBIT_DATAGRAM
351                    | QUIC_FTBIT_RETIRE_CONNECTION_ID,
352    [ENC_LEV_INIT]  = QUIC_FTBIT_CRYPTO | QUIC_FTBIT_PADDING | QUIC_FTBIT_PING
353                    | QUIC_FTBIT_ACK| QUIC_FTBIT_CONNECTION_CLOSE,
354    [ENC_LEV_FORW]  = QUIC_FTBIT_CRYPTO | QUIC_FTBIT_PADDING | QUIC_FTBIT_PING
355                    | QUIC_FTBIT_ACK | QUIC_FTBIT_CONNECTION_CLOSE
356                    | QUIC_FTBIT_STREAM | QUIC_FTBIT_RST_STREAM
357                    | QUIC_FTBIT_BLOCKED
358                    | QUIC_FTBIT_MAX_DATA | QUIC_FTBIT_MAX_STREAM_DATA
359                    | QUIC_FTBIT_MAX_STREAMS | QUIC_FTBIT_STREAM_BLOCKED
360                    | QUIC_FTBIT_STREAMS_BLOCKED
361                    | QUIC_FTBIT_NEW_CONNECTION_ID | QUIC_FTBIT_STOP_SENDING
362                    | QUIC_FTBIT_PATH_CHALLENGE | QUIC_FTBIT_PATH_RESPONSE
363                    | QUIC_FTBIT_HANDSHAKE_DONE | QUIC_FTBIT_ACK_FREQUENCY
364                    | QUIC_FTBIT_RETIRE_CONNECTION_ID | QUIC_FTBIT_NEW_TOKEN
365                    | QUIC_FTBIT_TIMESTAMP
366                    | QUIC_FTBIT_DATAGRAM
367                    ,
368    },
369    [LSQVER_ID29] = {
370    [ENC_LEV_CLEAR] = QUIC_FTBIT_CRYPTO | QUIC_FTBIT_PADDING | QUIC_FTBIT_PING
371                    | QUIC_FTBIT_ACK | QUIC_FTBIT_CONNECTION_CLOSE,
372    [ENC_LEV_EARLY] = QUIC_FTBIT_PADDING | QUIC_FTBIT_PING
373                    | QUIC_FTBIT_STREAM | QUIC_FTBIT_RST_STREAM
374                    | QUIC_FTBIT_BLOCKED | QUIC_FTBIT_CONNECTION_CLOSE
375                    | QUIC_FTBIT_MAX_DATA | QUIC_FTBIT_MAX_STREAM_DATA
376                    | QUIC_FTBIT_MAX_STREAMS | QUIC_FTBIT_STREAM_BLOCKED
377                    | QUIC_FTBIT_STREAMS_BLOCKED
378                    | QUIC_FTBIT_NEW_CONNECTION_ID | QUIC_FTBIT_STOP_SENDING
379                    | QUIC_FTBIT_PATH_CHALLENGE | QUIC_FTBIT_PATH_RESPONSE
380                    | QUIC_FTBIT_DATAGRAM
381                    | QUIC_FTBIT_RETIRE_CONNECTION_ID,
382    [ENC_LEV_INIT]  = QUIC_FTBIT_CRYPTO | QUIC_FTBIT_PADDING | QUIC_FTBIT_PING
383                    | QUIC_FTBIT_ACK| QUIC_FTBIT_CONNECTION_CLOSE,
384    [ENC_LEV_FORW]  = QUIC_FTBIT_CRYPTO | QUIC_FTBIT_PADDING | QUIC_FTBIT_PING
385                    | QUIC_FTBIT_ACK | QUIC_FTBIT_CONNECTION_CLOSE
386                    | QUIC_FTBIT_STREAM | QUIC_FTBIT_RST_STREAM
387                    | QUIC_FTBIT_BLOCKED
388                    | QUIC_FTBIT_MAX_DATA | QUIC_FTBIT_MAX_STREAM_DATA
389                    | QUIC_FTBIT_MAX_STREAMS | QUIC_FTBIT_STREAM_BLOCKED
390                    | QUIC_FTBIT_STREAMS_BLOCKED
391                    | QUIC_FTBIT_NEW_CONNECTION_ID | QUIC_FTBIT_STOP_SENDING
392                    | QUIC_FTBIT_PATH_CHALLENGE | QUIC_FTBIT_PATH_RESPONSE
393                    | QUIC_FTBIT_HANDSHAKE_DONE | QUIC_FTBIT_ACK_FREQUENCY
394                    | QUIC_FTBIT_RETIRE_CONNECTION_ID | QUIC_FTBIT_NEW_TOKEN
395                    | QUIC_FTBIT_TIMESTAMP
396                    | QUIC_FTBIT_DATAGRAM
397                    ,
398    },
399    [LSQVER_ID27] = {
400    [ENC_LEV_CLEAR] = QUIC_FTBIT_CRYPTO | QUIC_FTBIT_PADDING | QUIC_FTBIT_PING
401                    | QUIC_FTBIT_ACK | QUIC_FTBIT_CONNECTION_CLOSE,
402    [ENC_LEV_EARLY] = QUIC_FTBIT_PADDING | QUIC_FTBIT_PING
403                    | QUIC_FTBIT_STREAM | QUIC_FTBIT_RST_STREAM
404                    | QUIC_FTBIT_BLOCKED
405                    | QUIC_FTBIT_MAX_DATA | QUIC_FTBIT_MAX_STREAM_DATA
406                    | QUIC_FTBIT_MAX_STREAMS | QUIC_FTBIT_STREAM_BLOCKED
407                    | QUIC_FTBIT_STREAMS_BLOCKED
408                    | QUIC_FTBIT_NEW_CONNECTION_ID | QUIC_FTBIT_STOP_SENDING
409                    | QUIC_FTBIT_PATH_CHALLENGE | QUIC_FTBIT_PATH_RESPONSE
410                    | QUIC_FTBIT_RETIRE_CONNECTION_ID
411                    | QUIC_FTBIT_DATAGRAM
412                    ,
413    [ENC_LEV_INIT]  = QUIC_FTBIT_CRYPTO | QUIC_FTBIT_PADDING | QUIC_FTBIT_PING
414                    | QUIC_FTBIT_ACK| QUIC_FTBIT_CONNECTION_CLOSE,
415    [ENC_LEV_FORW]  = QUIC_FTBIT_CRYPTO | QUIC_FTBIT_PADDING | QUIC_FTBIT_PING
416                    | QUIC_FTBIT_ACK | QUIC_FTBIT_CONNECTION_CLOSE
417                    | QUIC_FTBIT_STREAM | QUIC_FTBIT_RST_STREAM
418                    | QUIC_FTBIT_BLOCKED
419                    | QUIC_FTBIT_MAX_DATA | QUIC_FTBIT_MAX_STREAM_DATA
420                    | QUIC_FTBIT_MAX_STREAMS | QUIC_FTBIT_STREAM_BLOCKED
421                    | QUIC_FTBIT_STREAMS_BLOCKED
422                    | QUIC_FTBIT_NEW_CONNECTION_ID | QUIC_FTBIT_STOP_SENDING
423                    | QUIC_FTBIT_PATH_CHALLENGE | QUIC_FTBIT_PATH_RESPONSE
424                    | QUIC_FTBIT_HANDSHAKE_DONE | QUIC_FTBIT_ACK_FREQUENCY
425                    | QUIC_FTBIT_RETIRE_CONNECTION_ID | QUIC_FTBIT_NEW_TOKEN
426                    | QUIC_FTBIT_TIMESTAMP
427                    | QUIC_FTBIT_DATAGRAM
428                    ,
429    },
430};
431