lsquic_parse_gquic_common.c revision bdf79b05
1/* Copyright (c) 2017 - 2018 LiteSpeed Technologies Inc.  See LICENSE. */
2/*
3 * lsquic_parse_gquic_common.c -- Parsing functions common to GQUIC
4 */
5
6#include <assert.h>
7#include <errno.h>
8#include <inttypes.h>
9#include <string.h>
10#include <stdlib.h>
11#include <sys/queue.h>
12#ifndef WIN32
13#include <sys/types.h>
14#else
15#include <vc_compat.h>
16#endif
17
18#include "lsquic_types.h"
19#include "lsquic_packet_common.h"
20#include "lsquic_packet_in.h"
21#include "lsquic_parse.h"
22#include "lsquic.h"
23
24#define LSQUIC_LOGGER_MODULE LSQLM_PARSE
25#include "lsquic_logger.h"
26
27#define CHECK_SPACE(need, pstart, pend)  \
28    do { if ((intptr_t) (need) > ((pend) - (pstart))) { return -1; } } while (0)
29
30/* This partially parses `packet_in' and returns 0 if in case it succeeded and
31 * -1 on failure.
32 *
33 * After this function returns 0, connection ID, nonce, and version fields can
34 * be examined.  To finsh parsing the packet, call version-specific
35 * pf_parse_packet_in_finish() routine.
36 */
37int
38parse_packet_in_begin (lsquic_packet_in_t *packet_in, size_t length,
39                            int is_server, struct packin_parse_state *state)
40{
41    int nbytes;
42    enum PACKET_PUBLIC_FLAGS public_flags;
43    const unsigned char *p = packet_in->pi_data;
44    const unsigned char *const pend = packet_in->pi_data + length;
45
46    CHECK_SPACE(1, p, pend);
47
48    public_flags = *p++;
49
50    if (public_flags & PACKET_PUBLIC_FLAGS_8BYTE_CONNECTION_ID)
51    {
52        CHECK_SPACE(8, p, pend);
53        memcpy(&packet_in->pi_conn_id, p, 8);
54        packet_in->pi_flags |= PI_CONN_ID;
55        p += 8;
56    }
57
58    if (public_flags & PACKET_PUBLIC_FLAGS_VERSION)
59    {
60        /* It seems that version negotiation packets sent by Google may have
61         * NONCE bit set.  Ignore it:
62         */
63        public_flags &= ~PACKET_PUBLIC_FLAGS_NONCE;
64
65        if (is_server)
66        {
67            CHECK_SPACE(4, p, pend);
68            packet_in->pi_quic_ver = p - packet_in->pi_data;
69            p += 4;
70        }
71        else
72        {   /* OK, we have a version negotiation packet.  We need to verify
73             * that it has correct structure.  See Section 4.3 of
74             * [draft-ietf-quic-transport-00].
75             */
76            if ((public_flags & ~(PACKET_PUBLIC_FLAGS_VERSION|
77                                  PACKET_PUBLIC_FLAGS_8BYTE_CONNECTION_ID))
78                || ((pend - p) & 3))
79                return -1;
80            CHECK_SPACE(4, p, pend);
81            packet_in->pi_quic_ver = p - packet_in->pi_data;
82            p = pend;
83        }
84    }
85    else
86    {
87        /* From [draft-hamilton-quic-transport-protocol-01]:
88         *    0x40 = MULTIPATH. This bit is reserved for multipath use.
89         *    0x80 is currently unused, and must be set to 0.
90         *
91         * The reference implementation checks that two high bits are not set
92         * if version flag is not set or if the version is the same.  For our
93         * purposes, all GQUIC version we support so far have these bits set
94         * to zero.
95         */
96        if (public_flags & (0x80|0x40))
97            return -1;
98        packet_in->pi_quic_ver = 0;
99    }
100
101    if (!is_server && (public_flags & PACKET_PUBLIC_FLAGS_NONCE) ==
102                                            PACKET_PUBLIC_FLAGS_NONCE)
103    {
104        CHECK_SPACE(32, p, pend);
105        packet_in->pi_nonce = p - packet_in->pi_data;
106        p += 32;
107    }
108    else
109        packet_in->pi_nonce = 0;
110
111    state->pps_p = p;
112
113    packet_in->pi_packno = 0;
114    if (0 == (public_flags & (PACKET_PUBLIC_FLAGS_VERSION|PACKET_PUBLIC_FLAGS_RST))
115        || ((public_flags & PACKET_PUBLIC_FLAGS_VERSION) && is_server))
116    {
117        nbytes = twobit_to_1246((public_flags >> 4) & 3);
118        CHECK_SPACE(nbytes, p, pend);
119        p += nbytes;
120        state->pps_nbytes = nbytes;
121    }
122    else
123        state->pps_nbytes = 0;
124
125    packet_in->pi_header_sz    = p - packet_in->pi_data;
126    packet_in->pi_frame_types  = 0;
127    memset(&packet_in->pi_next, 0, sizeof(packet_in->pi_next));
128    packet_in->pi_data_sz      = length;
129    packet_in->pi_refcnt       = 0;
130    packet_in->pi_received     = 0;
131
132    return 0;
133}
134
135
136static const enum QUIC_FRAME_TYPE byte2frame_type_Q035_thru_Q039[0x100] =
137{
138    [0x00] = QUIC_FRAME_PADDING,
139    [0x01] = QUIC_FRAME_RST_STREAM,
140    [0x02] = QUIC_FRAME_CONNECTION_CLOSE,
141    [0x03] = QUIC_FRAME_GOAWAY,
142    [0x04] = QUIC_FRAME_WINDOW_UPDATE,
143    [0x05] = QUIC_FRAME_BLOCKED,
144    [0x06] = QUIC_FRAME_STOP_WAITING,
145    [0x07] = QUIC_FRAME_PING,
146    [0x08] = QUIC_FRAME_INVALID,
147    [0x09] = QUIC_FRAME_INVALID,
148    [0x0A] = QUIC_FRAME_INVALID,
149    [0x0B] = QUIC_FRAME_INVALID,
150    [0x0C] = QUIC_FRAME_INVALID,
151    [0x0D] = QUIC_FRAME_INVALID,
152    [0x0E] = QUIC_FRAME_INVALID,
153    [0x0F] = QUIC_FRAME_INVALID,
154    [0x10] = QUIC_FRAME_INVALID,
155    [0x11] = QUIC_FRAME_INVALID,
156    [0x12] = QUIC_FRAME_INVALID,
157    [0x13] = QUIC_FRAME_INVALID,
158    [0x14] = QUIC_FRAME_INVALID,
159    [0x15] = QUIC_FRAME_INVALID,
160    [0x16] = QUIC_FRAME_INVALID,
161    [0x17] = QUIC_FRAME_INVALID,
162    [0x18] = QUIC_FRAME_INVALID,
163    [0x19] = QUIC_FRAME_INVALID,
164    [0x1A] = QUIC_FRAME_INVALID,
165    [0x1B] = QUIC_FRAME_INVALID,
166    [0x1C] = QUIC_FRAME_INVALID,
167    [0x1D] = QUIC_FRAME_INVALID,
168    [0x1E] = QUIC_FRAME_INVALID,
169    [0x1F] = QUIC_FRAME_INVALID,
170    [0x20] = QUIC_FRAME_INVALID,
171    [0x21] = QUIC_FRAME_INVALID,
172    [0x22] = QUIC_FRAME_INVALID,
173    [0x23] = QUIC_FRAME_INVALID,
174    [0x24] = QUIC_FRAME_INVALID,
175    [0x25] = QUIC_FRAME_INVALID,
176    [0x26] = QUIC_FRAME_INVALID,
177    [0x27] = QUIC_FRAME_INVALID,
178    [0x28] = QUIC_FRAME_INVALID,
179    [0x29] = QUIC_FRAME_INVALID,
180    [0x2A] = QUIC_FRAME_INVALID,
181    [0x2B] = QUIC_FRAME_INVALID,
182    [0x2C] = QUIC_FRAME_INVALID,
183    [0x2D] = QUIC_FRAME_INVALID,
184    [0x2E] = QUIC_FRAME_INVALID,
185    [0x2F] = QUIC_FRAME_INVALID,
186    [0x30] = QUIC_FRAME_INVALID,
187    [0x31] = QUIC_FRAME_INVALID,
188    [0x32] = QUIC_FRAME_INVALID,
189    [0x33] = QUIC_FRAME_INVALID,
190    [0x34] = QUIC_FRAME_INVALID,
191    [0x35] = QUIC_FRAME_INVALID,
192    [0x36] = QUIC_FRAME_INVALID,
193    [0x37] = QUIC_FRAME_INVALID,
194    [0x38] = QUIC_FRAME_INVALID,
195    [0x39] = QUIC_FRAME_INVALID,
196    [0x3A] = QUIC_FRAME_INVALID,
197    [0x3B] = QUIC_FRAME_INVALID,
198    [0x3C] = QUIC_FRAME_INVALID,
199    [0x3D] = QUIC_FRAME_INVALID,
200    [0x3E] = QUIC_FRAME_INVALID,
201    [0x3F] = QUIC_FRAME_INVALID,
202    [0x40] = QUIC_FRAME_ACK,
203    [0x41] = QUIC_FRAME_ACK,
204    [0x42] = QUIC_FRAME_ACK,
205    [0x43] = QUIC_FRAME_ACK,
206    [0x44] = QUIC_FRAME_ACK,
207    [0x45] = QUIC_FRAME_ACK,
208    [0x46] = QUIC_FRAME_ACK,
209    [0x47] = QUIC_FRAME_ACK,
210    [0x48] = QUIC_FRAME_ACK,
211    [0x49] = QUIC_FRAME_ACK,
212    [0x4A] = QUIC_FRAME_ACK,
213    [0x4B] = QUIC_FRAME_ACK,
214    [0x4C] = QUIC_FRAME_ACK,
215    [0x4D] = QUIC_FRAME_ACK,
216    [0x4E] = QUIC_FRAME_ACK,
217    [0x4F] = QUIC_FRAME_ACK,
218    [0x50] = QUIC_FRAME_ACK,
219    [0x51] = QUIC_FRAME_ACK,
220    [0x52] = QUIC_FRAME_ACK,
221    [0x53] = QUIC_FRAME_ACK,
222    [0x54] = QUIC_FRAME_ACK,
223    [0x55] = QUIC_FRAME_ACK,
224    [0x56] = QUIC_FRAME_ACK,
225    [0x57] = QUIC_FRAME_ACK,
226    [0x58] = QUIC_FRAME_ACK,
227    [0x59] = QUIC_FRAME_ACK,
228    [0x5A] = QUIC_FRAME_ACK,
229    [0x5B] = QUIC_FRAME_ACK,
230    [0x5C] = QUIC_FRAME_ACK,
231    [0x5D] = QUIC_FRAME_ACK,
232    [0x5E] = QUIC_FRAME_ACK,
233    [0x5F] = QUIC_FRAME_ACK,
234    [0x60] = QUIC_FRAME_ACK,
235    [0x61] = QUIC_FRAME_ACK,
236    [0x62] = QUIC_FRAME_ACK,
237    [0x63] = QUIC_FRAME_ACK,
238    [0x64] = QUIC_FRAME_ACK,
239    [0x65] = QUIC_FRAME_ACK,
240    [0x66] = QUIC_FRAME_ACK,
241    [0x67] = QUIC_FRAME_ACK,
242    [0x68] = QUIC_FRAME_ACK,
243    [0x69] = QUIC_FRAME_ACK,
244    [0x6A] = QUIC_FRAME_ACK,
245    [0x6B] = QUIC_FRAME_ACK,
246    [0x6C] = QUIC_FRAME_ACK,
247    [0x6D] = QUIC_FRAME_ACK,
248    [0x6E] = QUIC_FRAME_ACK,
249    [0x6F] = QUIC_FRAME_ACK,
250    [0x70] = QUIC_FRAME_ACK,
251    [0x71] = QUIC_FRAME_ACK,
252    [0x72] = QUIC_FRAME_ACK,
253    [0x73] = QUIC_FRAME_ACK,
254    [0x74] = QUIC_FRAME_ACK,
255    [0x75] = QUIC_FRAME_ACK,
256    [0x76] = QUIC_FRAME_ACK,
257    [0x77] = QUIC_FRAME_ACK,
258    [0x78] = QUIC_FRAME_ACK,
259    [0x79] = QUIC_FRAME_ACK,
260    [0x7A] = QUIC_FRAME_ACK,
261    [0x7B] = QUIC_FRAME_ACK,
262    [0x7C] = QUIC_FRAME_ACK,
263    [0x7D] = QUIC_FRAME_ACK,
264    [0x7E] = QUIC_FRAME_ACK,
265    [0x7F] = QUIC_FRAME_ACK,
266    [0x80] = QUIC_FRAME_STREAM,
267    [0x81] = QUIC_FRAME_STREAM,
268    [0x82] = QUIC_FRAME_STREAM,
269    [0x83] = QUIC_FRAME_STREAM,
270    [0x84] = QUIC_FRAME_STREAM,
271    [0x85] = QUIC_FRAME_STREAM,
272    [0x86] = QUIC_FRAME_STREAM,
273    [0x87] = QUIC_FRAME_STREAM,
274    [0x88] = QUIC_FRAME_STREAM,
275    [0x89] = QUIC_FRAME_STREAM,
276    [0x8A] = QUIC_FRAME_STREAM,
277    [0x8B] = QUIC_FRAME_STREAM,
278    [0x8C] = QUIC_FRAME_STREAM,
279    [0x8D] = QUIC_FRAME_STREAM,
280    [0x8E] = QUIC_FRAME_STREAM,
281    [0x8F] = QUIC_FRAME_STREAM,
282    [0x90] = QUIC_FRAME_STREAM,
283    [0x91] = QUIC_FRAME_STREAM,
284    [0x92] = QUIC_FRAME_STREAM,
285    [0x93] = QUIC_FRAME_STREAM,
286    [0x94] = QUIC_FRAME_STREAM,
287    [0x95] = QUIC_FRAME_STREAM,
288    [0x96] = QUIC_FRAME_STREAM,
289    [0x97] = QUIC_FRAME_STREAM,
290    [0x98] = QUIC_FRAME_STREAM,
291    [0x99] = QUIC_FRAME_STREAM,
292    [0x9A] = QUIC_FRAME_STREAM,
293    [0x9B] = QUIC_FRAME_STREAM,
294    [0x9C] = QUIC_FRAME_STREAM,
295    [0x9D] = QUIC_FRAME_STREAM,
296    [0x9E] = QUIC_FRAME_STREAM,
297    [0x9F] = QUIC_FRAME_STREAM,
298    [0xA0] = QUIC_FRAME_STREAM,
299    [0xA1] = QUIC_FRAME_STREAM,
300    [0xA2] = QUIC_FRAME_STREAM,
301    [0xA3] = QUIC_FRAME_STREAM,
302    [0xA4] = QUIC_FRAME_STREAM,
303    [0xA5] = QUIC_FRAME_STREAM,
304    [0xA6] = QUIC_FRAME_STREAM,
305    [0xA7] = QUIC_FRAME_STREAM,
306    [0xA8] = QUIC_FRAME_STREAM,
307    [0xA9] = QUIC_FRAME_STREAM,
308    [0xAA] = QUIC_FRAME_STREAM,
309    [0xAB] = QUIC_FRAME_STREAM,
310    [0xAC] = QUIC_FRAME_STREAM,
311    [0xAD] = QUIC_FRAME_STREAM,
312    [0xAE] = QUIC_FRAME_STREAM,
313    [0xAF] = QUIC_FRAME_STREAM,
314    [0xB0] = QUIC_FRAME_STREAM,
315    [0xB1] = QUIC_FRAME_STREAM,
316    [0xB2] = QUIC_FRAME_STREAM,
317    [0xB3] = QUIC_FRAME_STREAM,
318    [0xB4] = QUIC_FRAME_STREAM,
319    [0xB5] = QUIC_FRAME_STREAM,
320    [0xB6] = QUIC_FRAME_STREAM,
321    [0xB7] = QUIC_FRAME_STREAM,
322    [0xB8] = QUIC_FRAME_STREAM,
323    [0xB9] = QUIC_FRAME_STREAM,
324    [0xBA] = QUIC_FRAME_STREAM,
325    [0xBB] = QUIC_FRAME_STREAM,
326    [0xBC] = QUIC_FRAME_STREAM,
327    [0xBD] = QUIC_FRAME_STREAM,
328    [0xBE] = QUIC_FRAME_STREAM,
329    [0xBF] = QUIC_FRAME_STREAM,
330    [0xC0] = QUIC_FRAME_STREAM,
331    [0xC1] = QUIC_FRAME_STREAM,
332    [0xC2] = QUIC_FRAME_STREAM,
333    [0xC3] = QUIC_FRAME_STREAM,
334    [0xC4] = QUIC_FRAME_STREAM,
335    [0xC5] = QUIC_FRAME_STREAM,
336    [0xC6] = QUIC_FRAME_STREAM,
337    [0xC7] = QUIC_FRAME_STREAM,
338    [0xC8] = QUIC_FRAME_STREAM,
339    [0xC9] = QUIC_FRAME_STREAM,
340    [0xCA] = QUIC_FRAME_STREAM,
341    [0xCB] = QUIC_FRAME_STREAM,
342    [0xCC] = QUIC_FRAME_STREAM,
343    [0xCD] = QUIC_FRAME_STREAM,
344    [0xCE] = QUIC_FRAME_STREAM,
345    [0xCF] = QUIC_FRAME_STREAM,
346    [0xD0] = QUIC_FRAME_STREAM,
347    [0xD1] = QUIC_FRAME_STREAM,
348    [0xD2] = QUIC_FRAME_STREAM,
349    [0xD3] = QUIC_FRAME_STREAM,
350    [0xD4] = QUIC_FRAME_STREAM,
351    [0xD5] = QUIC_FRAME_STREAM,
352    [0xD6] = QUIC_FRAME_STREAM,
353    [0xD7] = QUIC_FRAME_STREAM,
354    [0xD8] = QUIC_FRAME_STREAM,
355    [0xD9] = QUIC_FRAME_STREAM,
356    [0xDA] = QUIC_FRAME_STREAM,
357    [0xDB] = QUIC_FRAME_STREAM,
358    [0xDC] = QUIC_FRAME_STREAM,
359    [0xDD] = QUIC_FRAME_STREAM,
360    [0xDE] = QUIC_FRAME_STREAM,
361    [0xDF] = QUIC_FRAME_STREAM,
362    [0xE0] = QUIC_FRAME_STREAM,
363    [0xE1] = QUIC_FRAME_STREAM,
364    [0xE2] = QUIC_FRAME_STREAM,
365    [0xE3] = QUIC_FRAME_STREAM,
366    [0xE4] = QUIC_FRAME_STREAM,
367    [0xE5] = QUIC_FRAME_STREAM,
368    [0xE6] = QUIC_FRAME_STREAM,
369    [0xE7] = QUIC_FRAME_STREAM,
370    [0xE8] = QUIC_FRAME_STREAM,
371    [0xE9] = QUIC_FRAME_STREAM,
372    [0xEA] = QUIC_FRAME_STREAM,
373    [0xEB] = QUIC_FRAME_STREAM,
374    [0xEC] = QUIC_FRAME_STREAM,
375    [0xED] = QUIC_FRAME_STREAM,
376    [0xEE] = QUIC_FRAME_STREAM,
377    [0xEF] = QUIC_FRAME_STREAM,
378    [0xF0] = QUIC_FRAME_STREAM,
379    [0xF1] = QUIC_FRAME_STREAM,
380    [0xF2] = QUIC_FRAME_STREAM,
381    [0xF3] = QUIC_FRAME_STREAM,
382    [0xF4] = QUIC_FRAME_STREAM,
383    [0xF5] = QUIC_FRAME_STREAM,
384    [0xF6] = QUIC_FRAME_STREAM,
385    [0xF7] = QUIC_FRAME_STREAM,
386    [0xF8] = QUIC_FRAME_STREAM,
387    [0xF9] = QUIC_FRAME_STREAM,
388    [0xFA] = QUIC_FRAME_STREAM,
389    [0xFB] = QUIC_FRAME_STREAM,
390    [0xFC] = QUIC_FRAME_STREAM,
391    [0xFD] = QUIC_FRAME_STREAM,
392    [0xFE] = QUIC_FRAME_STREAM,
393    [0xFF] = QUIC_FRAME_STREAM,
394};
395
396
397enum QUIC_FRAME_TYPE
398parse_frame_type_gquic_Q035_thru_Q039 (unsigned char b)
399{
400    return byte2frame_type_Q035_thru_Q039[b];
401}
402
403
404void
405lsquic_turn_on_fin_Q035_thru_Q039 (unsigned char *stream_header)
406{
407    /* 1fdoooss */
408    *stream_header |= 0x40;
409}
410
411
412size_t
413calc_stream_frame_header_sz_gquic (uint32_t stream_id, uint64_t offset)
414{
415    return
416        /* Type */
417        1
418        /* Stream ID length */
419      + ((stream_id) > 0x0000FF)
420      + ((stream_id) > 0x00FFFF)
421      + ((stream_id) > 0xFFFFFF)
422      + 1
423        /* Offset length */
424      + ((offset) >= (1ULL << 56))
425      + ((offset) >= (1ULL << 48))
426      + ((offset) >= (1ULL << 40))
427      + ((offset) >= (1ULL << 32))
428      + ((offset) >= (1ULL << 24))
429      + ((offset) >= (1ULL << 16))
430      + (((offset) > 0) << 1)
431        /* Add data length (2) yourself, if necessary */
432    ;
433}
434
435
436char *
437acki2str (const struct ack_info *acki, size_t *sz)
438{
439    size_t off, bufsz, nw;
440    unsigned n;
441    char *buf;
442
443    bufsz = acki->n_ranges * (3 /* [-] */ + 20 /* ~0ULL */ * 2);
444    buf = malloc(bufsz);
445    if (!buf)
446    {
447        LSQ_WARN("%s: malloc(%zd) failure: %s", __func__, bufsz,
448                                                        strerror(errno));
449        return NULL;
450    }
451
452    off = 0;
453    for (n = 0; n < acki->n_ranges; ++n)
454    {
455        nw = snprintf(buf + off, bufsz - off, "[%"PRIu64"-%"PRIu64"]",
456                acki->ranges[n].high, acki->ranges[n].low);
457        if (nw > bufsz - off)
458            break;
459        off += nw;
460    }
461
462    *sz = off;
463    return buf;
464}
465
466
467