lsquic_parse_ietf_v1.c revision 49f1f4f6
1/* Copyright (c) 2017 - 2020 LiteSpeed Technologies Inc.  See LICENSE. */
2/*
3 * lsquic_parse_ietf_v1.c -- Parsing functions specific to IETF QUIC v1
4 */
5
6#include <assert.h>
7#include <inttypes.h>
8#include <errno.h>
9#include <stddef.h>
10#include <string.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_int_types.h"
20#include "lsquic_sizes.h"
21#include "lsquic_packet_common.h"
22#include "lsquic_packet_ietf.h"
23#include "lsquic_packet_in.h"
24#include "lsquic_packet_out.h"
25#include "lsquic_parse.h"
26#include "lsquic_parse_common.h"
27#include "lsquic_sfcw.h"
28#include "lsquic_varint.h"
29#include "lsquic_hq.h"
30#include "lsquic_hash.h"
31#include "lsquic_stream.h"
32#include "lsquic_mm.h"
33#include "lsquic_malo.h"
34#include "lsquic_version.h"
35#include "lsquic.h"
36#include "lsquic_byteswap.h"
37#include "lsquic_conn.h"
38#include "lsquic_enc_sess.h"
39#include "lsquic_trans_params.h"
40#include "lsquic_parse_ietf.h"
41#include "lsquic_qtags.h"
42
43#define LSQUIC_LOGGER_MODULE LSQLM_PARSE
44#include "lsquic_logger.h"
45
46#define CHECK_SPACE(need, pstart, pend)  \
47    do { if ((intptr_t) (need) > ((pend) - (pstart))) { return -1; } } while (0)
48
49#define FRAME_TYPE_ACK_FREQUENCY    0xAF
50#define FRAME_TYPE_TIMESTAMP        0x2F5
51
52static int
53ietf_v1_gen_one_varint (unsigned char *, size_t, unsigned char, uint64_t);
54
55static int
56ietf_v1_gen_two_varints (unsigned char *, size_t, unsigned char, uint64_t[2]);
57
58static void
59ietf_v1_parse_packet_in_finish (lsquic_packet_in_t *packet_in,
60                                            struct packin_parse_state *state)
61{
62    /* Packet number is set to an invalid value.  The packet number must
63     * be decrypted, which happens later.
64     */
65    packet_in->pi_packno        = 1ULL << 62;
66}
67
68
69/* Note: token size is not accounted for */
70static size_t
71ietf_v1_packout_header_size_long_by_flags (const struct lsquic_conn *lconn,
72                enum header_type header_type, enum packet_out_flags flags,
73                size_t dcid_len)
74{
75    size_t sz;
76    enum packno_bits packno_bits;
77
78    packno_bits = (flags >> POBIT_SHIFT) & 0x3;
79
80    sz = 1 /* Type */
81       + 4 /* Version */
82       + 1 /* DCIL */
83       + dcid_len
84       + 1 /* SCIL */
85       + CN_SCID(lconn)->len
86       + (header_type == HETY_INITIAL)  /* Token length */
87       + 2 /* Always use two bytes to encode payload length */
88       + iquic_packno_bits2len(packno_bits)
89       ;
90
91    return sz;
92}
93
94
95static size_t
96ietf_v1_packout_header_size_long_by_packet (const struct lsquic_conn *lconn,
97                                const struct lsquic_packet_out *packet_out)
98{
99    size_t sz;
100    unsigned token_len; /* Need intermediate value to quiet compiler warning */
101    enum packno_bits packno_bits;
102
103    packno_bits = lsquic_packet_out_packno_bits(packet_out);
104
105    sz = 1 /* Type */
106       + 4 /* Version */
107       + 1 /* DCIL */
108       + packet_out->po_path->np_dcid.len
109       + 1 /* SCIL */
110       + CN_SCID(lconn)->len
111       + (packet_out->po_header_type == HETY_INITIAL ?
112            (token_len = packet_out->po_token_len,
113                (1 << vint_val2bits(token_len)) + token_len) : 0)
114       + 2 /* Always use two bytes to encode payload length */
115       + iquic_packno_bits2len(packno_bits)
116       ;
117
118    return sz;
119}
120
121
122static size_t
123ietf_v1_packout_header_size_short (const struct lsquic_conn *lconn,
124                                enum packet_out_flags flags, size_t dcid_len)
125{
126    enum packno_bits bits;
127    size_t sz;
128
129    bits = (flags >> POBIT_SHIFT) & 0x3;
130    sz = 1 /* Type */
131       + (flags & PO_CONN_ID ? dcid_len : 0)
132       + iquic_packno_bits2len(bits)
133       ;
134
135    return sz;
136}
137
138
139static size_t
140ietf_v1_packout_max_header_size (const struct lsquic_conn *lconn,
141                                enum packet_out_flags flags, size_t dcid_len)
142{
143    if (lconn->cn_flags & LSCONN_HANDSHAKE_DONE)
144        return ietf_v1_packout_header_size_short(lconn, flags, dcid_len);
145    else if (lconn->cn_flags & LSCONN_SERVER)
146        /* Server does not set the token in its Initial packet header: set
147         * the packet type to something else in order not to overestimate
148         * header size.
149         */
150        return ietf_v1_packout_header_size_long_by_flags(lconn, HETY_HANDSHAKE,
151                                                            flags, dcid_len);
152    else
153        return ietf_v1_packout_header_size_long_by_flags(lconn, HETY_INITIAL,
154                                                            flags, dcid_len);
155}
156
157
158/* [draft-ietf-quic-transport-17] Section-17.2 */
159static const unsigned char header_type_to_bin[] = {
160    [HETY_INITIAL]      = 0x0,
161    [HETY_0RTT]         = 0x1,
162    [HETY_HANDSHAKE]    = 0x2,
163    [HETY_RETRY]        = 0x3,
164};
165
166
167static unsigned
168write_packno (unsigned char *p, lsquic_packno_t packno,
169                                                enum packno_bits bits)
170{
171    unsigned char *const begin = p;
172
173    switch (bits)
174    {
175    case IQUIC_PACKNO_LEN_4:
176        *p++ = packno >> 24;
177        /* fall through */
178    case IQUIC_PACKNO_LEN_3:
179        *p++ = packno >> 16;
180        /* fall through */
181    case IQUIC_PACKNO_LEN_2:
182        *p++ = packno >> 8;
183        /* fall through */
184    default:
185        *p++ = packno;
186    }
187
188    return p - begin;
189}
190
191
192static int
193gen_long_pkt_header (const struct lsquic_conn *lconn,
194            const struct lsquic_packet_out *packet_out, unsigned char *buf,
195            size_t bufsz, unsigned *packno_off_p, unsigned *packno_len_p)
196{
197    unsigned payload_len, bits;
198    enum packno_bits packno_bits;
199    lsquic_ver_tag_t ver_tag;
200    unsigned char *p;
201    unsigned token_len;
202    size_t need;
203
204    need = ietf_v1_packout_header_size_long_by_packet(lconn, packet_out);
205    if (need > bufsz)
206    {
207        errno = EINVAL;
208        return -1;
209    }
210
211    packno_bits = lsquic_packet_out_packno_bits(packet_out);
212    p = buf;
213    *p++ = 0xC0
214         | ( header_type_to_bin[ packet_out->po_header_type ] << 4)
215         | packno_bits
216         ;
217    ver_tag = lsquic_ver2tag(lconn->cn_version);
218    memcpy(p, &ver_tag, sizeof(ver_tag));
219    p += sizeof(ver_tag);
220
221    *p++ = packet_out->po_path->np_dcid.len;
222    memcpy(p, packet_out->po_path->np_dcid.idbuf, packet_out->po_path->np_dcid.len);
223    p += packet_out->po_path->np_dcid.len;
224    *p++ = CN_SCID(lconn)->len;
225    memcpy(p, CN_SCID(lconn)->idbuf, CN_SCID(lconn)->len);
226    p +=  CN_SCID(lconn)->len;
227
228    if (HETY_INITIAL == packet_out->po_header_type)
229    {
230        token_len = packet_out->po_token_len;
231        bits = vint_val2bits(token_len);
232        vint_write(p, token_len, bits, 1 << bits);
233        p += 1 << bits;
234        memcpy(p, packet_out->po_token, token_len);
235        p += token_len;
236    }
237
238    payload_len = packet_out->po_data_sz
239                + lconn->cn_esf_c->esf_tag_len
240                + iquic_packno_bits2len(packno_bits);
241    bits = 1;   /* Always use two bytes to encode payload length */
242    vint_write(p, payload_len, bits, 1 << bits);
243    p += 1 << bits;
244
245    *packno_off_p = p - buf;
246    *packno_len_p = iquic_packno_bits2len(packno_bits);
247    p += write_packno(p, packet_out->po_packno, packno_bits);
248
249    return p - buf;
250}
251
252
253static int
254gen_short_pkt_header (const struct lsquic_conn *lconn,
255            const struct lsquic_packet_out *packet_out, unsigned char *buf,
256            size_t bufsz, unsigned *packno_off_p, unsigned *packno_len_p)
257{
258    unsigned packno_len, cid_len, need;
259    enum packno_bits packno_bits;
260
261    packno_bits = lsquic_packet_out_packno_bits(packet_out);
262    packno_len = iquic_packno_bits2len(packno_bits);
263    cid_len = packet_out->po_flags & PO_CONN_ID ? packet_out->po_path->np_dcid.len : 0;
264
265    need = 1 + cid_len + packno_len;
266    if (need > bufsz)
267        return -1;
268
269    buf[0] = QUIC_BIT
270           | (lsquic_packet_out_spin_bit(packet_out) << 5)
271           | (lsquic_packet_out_square_bit(packet_out) << 4)
272           | (lsquic_packet_out_loss_bit(packet_out) << 3)
273           | (lsquic_packet_out_kp(packet_out) << 2)
274           | packno_bits
275           ;
276
277    if (cid_len)
278        memcpy(buf + 1, packet_out->po_path->np_dcid.idbuf, cid_len);
279
280    (void) write_packno(buf + 1 + cid_len, packet_out->po_packno, packno_bits);
281
282    *packno_off_p = 1 + cid_len;
283    *packno_len_p = packno_len;
284    return need;
285}
286
287
288static int
289ietf_v1_gen_reg_pkt_header (const struct lsquic_conn *lconn,
290            const struct lsquic_packet_out *packet_out, unsigned char *buf,
291            size_t bufsz, unsigned *packno_off, unsigned *packno_len)
292{
293    if (packet_out->po_header_type == HETY_NOT_SET)
294        return gen_short_pkt_header(lconn, packet_out, buf, bufsz, packno_off,
295                                                                    packno_len);
296    else
297        return gen_long_pkt_header(lconn, packet_out, buf, bufsz, packno_off,
298                                                                    packno_len);
299}
300
301
302static size_t
303ietf_v1_packout_size (const struct lsquic_conn *lconn,
304                                const struct lsquic_packet_out *packet_out)
305{
306    size_t sz;
307
308    if ((lconn->cn_flags & LSCONN_HANDSHAKE_DONE)
309                                && packet_out->po_header_type == HETY_NOT_SET)
310        sz = ietf_v1_packout_header_size_short(lconn, packet_out->po_flags,
311                                            packet_out->po_path->np_dcid.len);
312    else
313        sz = ietf_v1_packout_header_size_long_by_packet(lconn, packet_out);
314
315    sz += packet_out->po_data_sz;
316    sz += lconn->cn_esf_c->esf_tag_len;
317
318    return sz;
319}
320
321
322static int
323ietf_v1_gen_stream_frame (unsigned char *buf, size_t buf_len,
324        lsquic_stream_id_t stream_id, uint64_t offset, int fin, size_t size,
325        gsf_read_f gsf_read, void *stream)
326{
327    /* 0b00001XXX
328     *  0x4     OFF
329     *  0x2     LEN
330     *  0x1     FIN
331     */
332    unsigned sbits, obits, dbits;
333    unsigned slen, olen, dlen;
334    unsigned char *p = buf + 1;
335
336#if _MSC_VER
337    obits = 0, dbits = 0;
338#endif
339
340    assert(!!fin ^ !!size);
341
342    /* We do not check that stream_id, offset, and size are smaller
343     * than 2^62: this is not necessary, as this code will never generate
344     * this many stream IDs, nor will it even transfer this much data.
345     * The size is limited by our own code.
346     */
347
348    sbits = vint_val2bits(stream_id);
349    slen = 1 << sbits;
350    if (offset)
351    {
352        obits = vint_val2bits(offset);
353        olen = 1 << obits;
354    }
355    else
356        olen = 0;
357
358    if (!fin)
359    {
360        unsigned n_avail;
361        size_t nr;
362
363        n_avail = buf_len - (p + slen + olen - buf);
364
365        /* If we cannot fill remaining buffer, we need to include data
366         * length.
367         */
368        if (size < n_avail)
369        {
370            dbits = vint_val2bits(size);
371            dlen = 1 << dbits;
372            n_avail -= dlen;
373            if (size > n_avail)
374                size = n_avail;
375        }
376        else
377        {
378            dlen = 0;
379            size = n_avail;
380        }
381
382        CHECK_STREAM_SPACE(1 + olen + slen + dlen +
383            + 1 /* We need to write at least 1 byte */, buf, buf + buf_len);
384
385        vint_write(p, stream_id, sbits, slen);
386        p += slen;
387
388        if (olen)
389            vint_write(p, offset, obits, olen);
390        p += olen;
391
392        /* Read as much as we can */
393        nr = gsf_read(stream, p + dlen, size, &fin);
394        assert(nr != 0);
395        assert(nr <= size);
396
397        if (dlen)
398            vint_write(p, nr, dbits, dlen);
399
400        p += dlen + nr;
401    }
402    else
403    {
404        dlen = 1 + slen + olen < buf_len;
405        CHECK_STREAM_SPACE(1 + slen + olen + dlen, buf, buf + buf_len);
406        vint_write(p, stream_id, sbits, slen);
407        p += slen;
408        if (olen)
409            vint_write(p, offset, obits, olen);
410        p += olen;
411        if (dlen)
412            *p++ = 0;
413    }
414
415    buf[0] = 0x08
416           | (!!olen << 2)
417           | (!!dlen << 1)
418           | (!!fin  << 0)
419           ;
420    return p - buf;
421}
422
423
424int
425lsquic_ietf_v1_gen_crypto_frame (unsigned char *buf, unsigned char first_byte,
426            size_t buf_len, lsquic_stream_id_t UNUSED_1, uint64_t offset,
427            int UNUSED_2, size_t size, gsf_read_f gsf_read, void *stream)
428{
429    unsigned char *const end = buf + buf_len;
430    unsigned char *p;
431    unsigned obits, dbits;
432    unsigned olen, dlen;
433    size_t nr, n_avail;
434    int dummy_fin;
435
436    obits = vint_val2bits(offset);
437    olen = 1 << obits;
438    dbits = vint_val2bits(size);
439    dlen = 1 << dbits;
440
441    CHECK_SPACE(1 + olen + dlen
442        + (dlen > 0) /* We need to write at least 1 byte */, buf, end);
443
444    n_avail = end - buf - 1 - olen - dlen;
445    if (n_avail < size)
446        size = n_avail;
447
448    p = buf;
449    *p++ = first_byte;
450
451    vint_write(p, offset, obits, olen);
452    p += olen;
453
454    nr = gsf_read(stream, p + dlen, size, &dummy_fin);
455    assert(nr != 0);    /* This indicates error in the caller */
456    assert(nr <= size); /* This also indicates an error in the caller */
457
458    vint_write(p, nr, dbits, dlen);
459    p += dlen + nr;
460
461    return p - buf;
462}
463
464
465static int
466ietf_v1_gen_crypto_frame (unsigned char *buf, size_t buf_len,
467        lsquic_stream_id_t stream_id, uint64_t offset, int fin,
468        size_t size, gsf_read_f gsf_read, void *stream)
469{
470    return lsquic_ietf_v1_gen_crypto_frame(buf, 0x6, buf_len, stream_id,
471                                        offset, fin, size, gsf_read, stream);
472}
473
474
475/* return parsed (used) buffer length */
476static int
477ietf_v1_parse_stream_frame (const unsigned char *buf, size_t rem_packet_sz,
478                                        struct stream_frame *stream_frame)
479{
480    /* 0b00001XXX
481     *  0x4     OFF
482     *  0x2     LEN
483     *  0x1     FIN
484     */
485    const unsigned char *const pend = buf + rem_packet_sz;
486    const unsigned char *p = buf;
487    lsquic_stream_id_t stream_id;
488    uint64_t offset, data_sz;
489    int r;
490
491    CHECK_SPACE(1, p, pend);
492    const char type = *p++;
493
494    r = vint_read(p, pend, &stream_id);
495    if (r < 0)
496        return -1;
497    p += r;
498
499    if (type & 0x4)
500    {
501        r = vint_read(p, pend, &offset);
502        if (r < 0)
503            return -1;
504        p += r;
505    }
506    else
507        offset = 0;
508
509    if (type & 0x2)
510    {
511        r = vint_read(p, pend, &data_sz);
512        if (r < 0)
513            return -1;
514        p += r;
515        CHECK_SPACE(data_sz, p, pend);
516    }
517    else
518        data_sz = pend - p;
519
520    /* Largest offset cannot exceed this value and we MUST detect this error */
521    if (VINT_MAX_VALUE - offset < data_sz)
522        return -1;
523
524    stream_frame->stream_id             = stream_id;
525    stream_frame->data_frame.df_fin     = type & 0x1;
526    stream_frame->data_frame.df_offset  = offset;
527    stream_frame->data_frame.df_size    = data_sz;
528    stream_frame->data_frame.df_data    = p;
529    stream_frame->data_frame.df_read_off= 0;
530    stream_frame->packet_in             = NULL;
531
532    assert(p <= pend);
533
534    return p + data_sz - (unsigned char *) buf;
535}
536
537
538int
539lsquic_ietf_v1_parse_crypto_frame (const unsigned char *buf,
540                    size_t rem_packet_sz, struct stream_frame *stream_frame)
541{
542    const unsigned char *const pend = buf + rem_packet_sz;
543    const unsigned char *p = buf;
544    uint64_t offset, data_sz;
545    int r;
546
547    CHECK_SPACE(1, p, pend);
548
549    ++p;
550
551    r = vint_read(p, pend, &offset);
552    if (r < 0)
553        return -1;
554    p += r;
555
556    r = vint_read(p, pend, &data_sz);
557    if (r < 0)
558        return -1;
559    p += r;
560    CHECK_SPACE(data_sz, p, pend);
561
562    /* Largest offset cannot exceed this value and we MUST detect this error */
563    if (VINT_MAX_VALUE - offset < data_sz)
564        return -1;
565
566    stream_frame->stream_id             = ~0ULL;    /* Unset */
567    stream_frame->data_frame.df_fin     = 0;
568    stream_frame->data_frame.df_offset  = offset;
569    stream_frame->data_frame.df_size    = data_sz;
570    stream_frame->data_frame.df_data    = p;
571    stream_frame->data_frame.df_read_off= 0;
572    stream_frame->packet_in             = NULL;
573
574    assert(p <= pend);
575
576    return p + data_sz - (unsigned char *) buf;
577}
578
579
580static int
581ietf_v1_parse_crypto_frame (const unsigned char *buf, size_t rem_packet_sz,
582                                        struct stream_frame *stream_frame)
583{
584    if (rem_packet_sz > 0)
585    {
586        assert(0x06 == buf[0]);
587        return lsquic_ietf_v1_parse_crypto_frame(buf, rem_packet_sz,
588                                                            stream_frame);
589    }
590    else
591        return -1;
592}
593
594
595#if __GNUC__
596#   define UNLIKELY(cond) __builtin_expect(cond, 0)
597#else
598#   define UNLIKELY(cond) cond
599#endif
600
601
602/* Bits 10 (2) is ECT(0); * bits 01 (1) is ECT(1). */
603static const int ecnmap[4] = { 0, 2, 1, 3, };
604
605
606static int
607ietf_v1_parse_ack_frame (const unsigned char *const buf, size_t buf_len,
608                                            struct ack_info *ack, uint8_t exp)
609{
610    const unsigned char *p = buf;
611    const unsigned char *const end = buf + buf_len;
612    uint64_t block_count, gap, block;
613    enum ecn ecn;
614    unsigned i;
615    int r;
616
617    ++p;
618    r = vint_read(p, end, &ack->ranges[0].high);
619    if (UNLIKELY(r < 0))
620        return -1;
621    p += r;
622    r = vint_read(p, end, &ack->lack_delta);
623    if (UNLIKELY(r < 0))
624        return -1;
625    p += r;
626    ack->lack_delta <<= exp;
627    r = vint_read(p, end, &block_count);
628    if (UNLIKELY(r < 0))
629        return -1;
630    p += r;
631    r = vint_read(p, end, &block);
632    if (UNLIKELY(r < 0))
633        return -1;
634    ack->ranges[0].low = ack->ranges[0].high - block;
635    if (UNLIKELY(ack->ranges[0].high < ack->ranges[0].low))
636        return -1;
637    p += r;
638
639    for (i = 1; i <= block_count; ++i)
640    {
641        r = vint_read(p, end, &gap);
642        if (UNLIKELY(r < 0))
643            return -1;
644        p += r;
645        r = vint_read(p, end, &block);
646        if (UNLIKELY(r < 0))
647            return -1;
648        p += r;
649        if (i < sizeof(ack->ranges) / sizeof(ack->ranges[0]))
650        {
651            ack->ranges[i].high = ack->ranges[i - 1].low - gap - 2;
652            ack->ranges[i].low  = ack->ranges[i].high - block;
653            if (UNLIKELY(ack->ranges[i].high >= ack->ranges[i - 1].low
654                         || ack->ranges[i].high < ack->ranges[i].low))
655                return -1;
656        }
657    }
658
659    if (i < sizeof(ack->ranges) / sizeof(ack->ranges[0]))
660    {
661        ack->flags = 0;
662        ack->n_ranges = block_count + 1;
663    }
664    else
665    {
666        ack->flags = AI_TRUNCATED;
667        ack->n_ranges = sizeof(ack->ranges) / sizeof(ack->ranges[0]);
668    }
669
670
671    if (0x03 == buf[0])
672    {
673        for (ecn = 1; ecn <= 3; ++ecn)
674        {
675            r = vint_read(p, end, &ack->ecn_counts[ecnmap[ecn]]);
676            if (UNLIKELY(r < 0))
677                return -1;
678            p += r;
679        }
680        ack->flags |= AI_ECN;
681    }
682
683    return p - buf;
684}
685
686
687static unsigned
688ietf_v1_rst_frame_size (lsquic_stream_id_t stream_id, uint64_t error_code,
689                                                            uint64_t final_size)
690{
691    return 1                        /* Type */
692         + vint_size(stream_id)   /* Stream ID (i) */
693         + vint_size(error_code)  /* Application Error Code (i) */
694         + vint_size(final_size); /* Final Size (i) */
695}
696
697
698static int
699ietf_v1_gen_rst_frame (unsigned char *buf, size_t buf_len,
700        lsquic_stream_id_t stream_id, uint64_t error_code, uint64_t final_size)
701{
702    unsigned vbits;
703    unsigned char *p;
704
705    if (buf_len < ietf_v1_rst_frame_size(stream_id, error_code, final_size))
706        return -1;
707    p = buf;
708
709    *p++ = 0x04;
710    /* Stream ID (i) */
711    vbits = vint_val2bits(stream_id);
712    vint_write(p, stream_id, vbits, 1 << vbits);
713    p += 1 << vbits;
714
715    /* Application Error Code (i) */
716    vbits = vint_val2bits(error_code);
717    vint_write(p, error_code, vbits, 1 << vbits);
718    p += 1 << vbits;
719
720    /* Final Size (i) */
721    vbits = vint_val2bits(final_size);
722    vint_write(p, final_size, vbits, 1 << vbits);
723    p += 1 << vbits;
724
725    return p - buf;
726}
727
728
729static int
730ietf_v1_parse_rst_frame (const unsigned char *buf, size_t buf_len,
731    lsquic_stream_id_t *stream_id_p, uint64_t *final_size_p, uint64_t *error_code_p)
732{
733    const unsigned char *p = buf + 1;
734    const unsigned char *const end = buf + buf_len;
735    uint64_t stream_id, final_size, error_code;
736    int r;
737
738    /* Stream ID (i) */
739    r = vint_read(p, end, &stream_id);
740    if (r < 0)
741        return r;
742    p += r;
743
744    /* Application Error Code (i) */
745    r = vint_read(p, end, &error_code);
746    if (r < 0)
747        return r;
748    p += r;
749
750    /* Final Size (i) */
751    r = vint_read(p, end, &final_size);
752    if (r < 0)
753        return r;
754    p += r;
755
756    *stream_id_p = stream_id;
757    *final_size_p = final_size;
758    *error_code_p = error_code;
759
760    return p - buf;
761}
762
763
764static int
765ietf_v1_parse_stop_sending_frame (const unsigned char *buf, size_t buf_len,
766                        lsquic_stream_id_t *stream_id, uint64_t *error_code)
767{
768    const unsigned char *p = buf + 1;
769    const unsigned char *const end = buf + buf_len;
770    int r;
771
772    r = vint_read(p, end, stream_id);
773    if (r < 0)
774        return r;
775    p += r;
776
777    r = vint_read(p, end, error_code);
778    if (r < 0)
779        return r;
780    p += r;
781
782    return p - buf;
783}
784
785
786static int
787ietf_v1_gen_stop_sending_frame (unsigned char *buf, size_t len,
788                            lsquic_stream_id_t stream_id, uint64_t error_code)
789{
790    return ietf_v1_gen_two_varints(buf, len, 0x05, (uint64_t[]){ stream_id,
791                                                                error_code, });
792}
793
794
795static unsigned
796ietf_v1_stop_sending_frame_size (lsquic_stream_id_t val, uint64_t error_code)
797{
798    return 1 + vint_size(val) + vint_size(error_code);
799}
800
801
802static int
803ietf_v1_parse_new_token_frame (const unsigned char *buf, size_t buf_len,
804                            const unsigned char **token, size_t *token_size_p)
805{
806    uint64_t token_size;
807    const unsigned char *p = buf + 1;
808    const unsigned char *const end = buf + buf_len;
809    int r;
810
811    r = vint_read(p, end, &token_size);
812    if (r < 0)
813        return r;
814    p += r;
815
816    if (p + token_size > end)
817        return -1;
818    *token = p;
819    p += token_size;
820    *token_size_p = token_size;
821
822    return p - buf;
823}
824
825
826static int
827ietf_v1_gen_ping_frame (unsigned char *buf, int buf_len)
828{
829    if (buf_len > 0)
830    {
831        buf[0] = 0x01;
832        return 1;
833    }
834    else
835        return -1;
836}
837
838
839static size_t
840ietf_v1_connect_close_frame_size (int app_error, unsigned error_code,
841                                unsigned frame_type, size_t reason_len)
842{
843    return 1                                                         /* Type */
844         + (1 << vint_val2bits(error_code))                    /* Error code */
845         + (app_error ? 0 : 1 << vint_val2bits(frame_type))    /* Frame type */
846         + (1 << vint_val2bits(reason_len))          /* Reason Phrase Length */
847         + reason_len
848         ;
849}
850
851
852static int
853ietf_v1_gen_connect_close_frame (unsigned char *buf, size_t buf_len,
854    int app_error, unsigned error_code, const char *reason, int reason_len)
855{
856    size_t needed;
857    unsigned bits_error, bits_reason;
858    unsigned char *p;
859
860    assert(!!reason == !!reason_len);
861
862    bits_reason = vint_val2bits(reason_len);
863    bits_error = vint_val2bits(error_code);
864    needed = 1 /* Type */ + (1 << bits_error)
865           + (app_error ? 0 : 1) /* Frame type */
866        /* TODO: frame type instead of just zero */
867           + (1 << bits_reason) + reason_len;
868
869    if (buf_len < needed)
870        return -1;
871
872    p = buf;
873    *p = 0x1C + !!app_error;
874    ++p;
875    vint_write(p, error_code, bits_error, 1 << bits_error);
876    p += 1 << bits_error;
877    if (!app_error)
878        *p++ = 0;   /* Frame type */ /* TODO */
879    vint_write(p, reason_len, bits_reason, 1 << bits_reason);
880    p += 1 << bits_reason;
881    if (reason_len)
882    {
883        memcpy(p, reason, reason_len);
884        p += reason_len;
885    }
886
887    assert((unsigned) (p - buf) == needed);
888    return p - buf;
889}
890
891
892static int
893ietf_v1_parse_connect_close_frame (const unsigned char *buf, size_t buf_len,
894        int *app_error_p, uint64_t *error_code, uint16_t *reason_len,
895        uint8_t *reason_offset)
896{
897    const unsigned char *const pend = buf + buf_len;
898    const unsigned char *p = buf + 1;
899    uint64_t len;
900    ptrdiff_t off;
901    int app_error, r;
902
903    r = vint_read(p, pend, error_code);
904    if (r < 0)
905        return -1;
906    p += r;
907
908    app_error = buf[0] == 0x1D;
909    if (!app_error)
910    {
911        r = vint_read(p, pend, &len);
912        if (r < 0)
913            return -1;
914        p += r;
915    }
916
917    r = vint_read(p, pend, &len);
918    if (r < 0)
919        return -1;
920    if (len > UINT16_MAX)
921        return -1;
922    p += r;
923
924    off = p - buf;
925    if (buf_len < off + len)
926        return -2;
927
928    *app_error_p = app_error;
929    *reason_len = len;
930    *reason_offset = off;
931    return off + len;
932}
933
934
935/* Returns number of bytes written or -1 on failure */
936/* This function makes an assumption that there is at least one range */
937static int
938ietf_v1_gen_ack_frame (unsigned char *outbuf, size_t outbuf_sz,
939        gaf_rechist_first_f rechist_first, gaf_rechist_next_f rechist_next,
940        gaf_rechist_largest_recv_f rechist_largest_recv,
941        void *rechist, lsquic_time_t now, int *has_missing,
942        lsquic_packno_t *largest_received, const uint64_t *ecn_counts)
943{
944    unsigned char *block_count_p, *p = outbuf;
945    unsigned char *const end = p + outbuf_sz;
946    lsquic_time_t time_diff;
947    lsquic_packno_t packno_diff, gap, prev_low, maxno, rsize;
948    size_t sz;
949    const struct lsquic_packno_range *range;
950    unsigned a, b, c, addl_ack_blocks, ecn_needs;
951    unsigned bits[4];
952    enum ecn ecn;
953
954#define AVAIL() (end - p)
955
956#define CHECKOUT(sz) do {                                               \
957    if ((intptr_t) (sz) > AVAIL()) {                                    \
958        errno = ENOBUFS;                                                \
959        return -1;                                                      \
960    }                                                                   \
961} while (0)
962
963    range = rechist_first(rechist);
964    if (!range)
965    {
966        errno = EINVAL;
967        return -1;
968    }
969    // LSQ_DEBUG("range [%"PRIu64" - %"PRIu64"]", range->high, range->low);
970
971    time_diff = now - rechist_largest_recv(rechist);
972    time_diff >>= TP_DEF_ACK_DELAY_EXP;
973
974    maxno = range->high;
975    packno_diff = maxno - range->low;
976
977    a = vint_val2bits(maxno);
978    b = vint_val2bits(time_diff);
979    c = vint_val2bits(packno_diff);
980    sz = 1          /* Type */
981       + (1 << a)   /* Largest Acknowledged */
982       + (1 << b)   /* ACK Delay */
983       + 1          /* ACK Block Count */
984       + (1 << c)   /* ACK Blocks */
985       ;
986
987    CHECKOUT(sz);
988
989    if (ecn_counts)
990    {
991        for (ecn = 1; ecn <= 3; ++ecn)
992            bits[ecn] = vint_val2bits(ecn_counts[ecn]);
993        ecn_needs = (1 << bits[1]) + (1 << bits[2]) + (1 << bits[3]);
994    }
995    else
996        ecn_needs = 0;
997
998    *p = 0x02 + !!ecn_counts;
999    ++p;
1000
1001    vint_write(p, maxno, a, 1 << a);
1002    p += 1 << a;
1003    vint_write(p, time_diff, b, 1 << b);
1004    p += 1 << b;
1005    block_count_p = p;
1006    p += 1; /* Initial guess that we have fewer than 64 additional ACK Blocks */
1007    vint_write(p, packno_diff, c, 1 << c);
1008    p += 1 << c;
1009
1010    prev_low = range->low;
1011    addl_ack_blocks = 0;
1012    while ((range = rechist_next(rechist)))
1013    {
1014        // LSQ_DEBUG("range [%"PRIu64" - %"PRIu64"]", range->high, range->low);
1015        gap = prev_low - range->high - 1;
1016        rsize = range->high - range->low;
1017        a = vint_val2bits(gap - 1);
1018        b = vint_val2bits(rsize);
1019        if (ecn_needs + (1 << a) + (1 << b) > (unsigned)AVAIL())
1020            break;
1021        if (addl_ack_blocks == VINT_MAX_ONE_BYTE)
1022        {
1023            memmove(block_count_p + 2, block_count_p + 1,
1024                                                p - block_count_p - 1);
1025            ++p;
1026        }
1027        vint_write(p, gap - 1, a, 1 << a);
1028        p += 1 << a;
1029        vint_write(p, rsize, b, 1 << b);
1030        p += 1 << b;
1031        ++addl_ack_blocks;
1032        prev_low = range->low;
1033    }
1034
1035    /* Here we assume that addl_ack_blocks < (1 << 14), which is a safe
1036     * assumption to make.
1037     */
1038    vint_write(block_count_p, addl_ack_blocks,
1039                        addl_ack_blocks > VINT_MAX_ONE_BYTE,
1040                        1 + (addl_ack_blocks > VINT_MAX_ONE_BYTE));
1041
1042    if (ecn_counts)
1043    {
1044        assert(ecn_needs <= (unsigned)AVAIL());
1045        for (ecn = 1; ecn <= 3; ++ecn)
1046        {
1047            vint_write(p, ecn_counts[ecnmap[ecn]], bits[ecnmap[ecn]], 1 << bits[ecnmap[ecn]]);
1048            p += 1 << bits[ecnmap[ecn]];
1049        }
1050    }
1051
1052    *has_missing = addl_ack_blocks > 0;
1053    *largest_received = maxno;
1054    return p - (unsigned char *) outbuf;
1055
1056#undef CHECKOUT
1057#undef AVAIL
1058}
1059
1060
1061static size_t
1062ietf_v1_calc_stream_frame_header_sz (lsquic_stream_id_t stream_id,
1063                                            uint64_t offset, unsigned data_sz)
1064{
1065    if (offset)
1066        return 1
1067            + (1 << vint_val2bits(stream_id))
1068            + (1 << vint_val2bits(data_sz))
1069            + (1 << vint_val2bits(offset));
1070    else
1071        return 1
1072            + (1 << vint_val2bits(data_sz))
1073            + (1 << vint_val2bits(stream_id));
1074}
1075
1076
1077/* [draft-ietf-quic-transport-24] Section 19.6 */
1078static size_t
1079ietf_v1_calc_crypto_frame_header_sz (uint64_t offset, unsigned data_sz)
1080{
1081    return 1    /* Frame type */
1082         + (1 << vint_val2bits(offset))
1083         + (1 << vint_val2bits(data_sz))
1084         ;
1085}
1086
1087
1088static enum quic_frame_type
1089ietf_v1_parse_frame_type (const unsigned char *buf, size_t len)
1090{
1091    uint64_t val;
1092    int s;
1093
1094    if (len > 0 && buf[0] < 0x40)
1095        return lsquic_iquic_byte2type[buf[0]];
1096
1097    s = vint_read(buf, buf + len, &val);
1098    if (s > 0 && (unsigned) s == (1u << vint_val2bits(val)))
1099        switch (val)
1100        {
1101        case FRAME_TYPE_ACK_FREQUENCY:  return QUIC_FRAME_ACK_FREQUENCY;
1102        case FRAME_TYPE_TIMESTAMP:      return QUIC_FRAME_TIMESTAMP;
1103        default:    break;
1104        }
1105
1106    return QUIC_FRAME_INVALID;
1107}
1108
1109
1110static unsigned
1111ietf_v1_path_chal_frame_size (void)
1112{
1113    return 1 + sizeof(uint64_t);
1114}
1115
1116
1117static int
1118ietf_v1_gen_path_chal_frame (unsigned char *buf, size_t len, uint64_t chal)
1119{
1120    if (len >= 1 + sizeof(chal))
1121    {
1122        *buf = 0x1A;
1123        memcpy(buf + 1, &chal, sizeof(chal));
1124        return 1 + sizeof(chal);
1125    }
1126    else
1127        return -1;
1128}
1129
1130
1131static int
1132ietf_v1_parse_path_chal_frame (const unsigned char *buf, size_t len,
1133                                                            uint64_t *chal)
1134{
1135    if (len >= 9)
1136    {
1137        memcpy(chal, buf + 1, 8);
1138        return 9;
1139    }
1140    else
1141        return -1;
1142}
1143
1144
1145static unsigned
1146ietf_v1_path_resp_frame_size (void)
1147{
1148    return 1 + sizeof(uint64_t);
1149}
1150
1151
1152static int
1153ietf_v1_gen_path_resp_frame (unsigned char *buf, size_t len, uint64_t resp)
1154{
1155    if (len >= 1 + sizeof(resp))
1156    {
1157        *buf = 0x1B;
1158        memcpy(buf + 1, &resp, sizeof(resp));
1159        return 1 + sizeof(resp);
1160    }
1161    else
1162        return -1;
1163}
1164
1165
1166static int
1167ietf_v1_parse_path_resp_frame (const unsigned char *buf, size_t len,
1168                                                            uint64_t *resp)
1169{
1170    return ietf_v1_parse_path_chal_frame(buf, len, resp);
1171}
1172
1173
1174static void
1175ietf_v1_turn_on_fin (unsigned char *stream_frame_header)
1176{
1177    *stream_frame_header |= 1;
1178}
1179
1180
1181static unsigned
1182ietf_v1_packno_bits2len (enum packno_bits bits)
1183{
1184    return iquic_packno_bits2len(bits);
1185}
1186
1187
1188static enum packno_bits
1189ietf_v1_calc_packno_bits (lsquic_packno_t packno,
1190                    lsquic_packno_t least_unacked, uint64_t n_in_flight)
1191{
1192    uint64_t delta;
1193    unsigned bits;
1194
1195    delta = packno - least_unacked;
1196    if (n_in_flight > delta)
1197        delta = n_in_flight;
1198
1199    delta *= 4;
1200    bits = (delta >= (1ULL <<  8))
1201         + (delta >= (1ULL << 16))
1202         + (delta >= (1ULL << 24))
1203         ;
1204
1205    return bits;
1206}
1207
1208
1209static int
1210ietf_v1_parse_one_varint (const unsigned char *buf, size_t len, uint64_t *val)
1211{
1212    int s;
1213
1214    s = vint_read(buf + 1, buf + len, val);
1215    if (s >= 0)
1216        return 1 + s;
1217    else
1218        return s;
1219}
1220
1221
1222static int
1223ietf_v1_gen_one_varint (unsigned char *buf, size_t len,
1224                                        unsigned char type, uint64_t val)
1225{
1226    unsigned vbits;
1227    unsigned char *p;
1228
1229    vbits = vint_val2bits(val);
1230
1231    if (1u + (1u << vbits) > len)
1232        return -1;
1233
1234    p = buf;
1235    *p++ = type;
1236    vint_write(p, val, vbits, 1 << vbits);
1237    p += 1 << vbits;
1238
1239    return p - buf;
1240}
1241
1242
1243static int
1244ietf_v1_gen_blocked_frame (unsigned char *buf, size_t buf_len, uint64_t off)
1245{
1246    return ietf_v1_gen_one_varint(buf, buf_len, 0x14, off);
1247}
1248
1249
1250static int
1251ietf_v1_parse_blocked_frame (const unsigned char *buf, size_t sz, uint64_t *off)
1252{
1253    return ietf_v1_parse_one_varint(buf, sz, off);
1254}
1255
1256
1257static unsigned
1258ietf_v1_blocked_frame_size (uint64_t off)
1259{
1260    return 1 + vint_size(off);
1261}
1262
1263
1264static int
1265ietf_v1_parse_max_data (const unsigned char *buf, size_t len, uint64_t *val)
1266{
1267    return ietf_v1_parse_one_varint(buf, len, val);
1268}
1269
1270
1271static int
1272ietf_v1_gen_max_data_frame (unsigned char *buf, size_t len, uint64_t val)
1273{
1274    return ietf_v1_gen_one_varint(buf, len, 0x10, val);
1275}
1276
1277
1278static unsigned
1279ietf_v1_max_data_frame_size (uint64_t val)
1280{
1281    return 1 + vint_size(val);
1282}
1283
1284
1285static int
1286ietf_v1_parse_retire_cid_frame (const unsigned char *buf, size_t len,
1287                                                                uint64_t *val)
1288{
1289    return ietf_v1_parse_one_varint(buf, len, val);
1290}
1291
1292
1293static int
1294ietf_v1_gen_retire_cid_frame (unsigned char *buf, size_t len, uint64_t val)
1295{
1296    return ietf_v1_gen_one_varint(buf, len, 0x19, val);
1297}
1298
1299
1300static size_t
1301ietf_v1_retire_cid_frame_size (uint64_t val)
1302{
1303    return 1 + vint_size(val);
1304}
1305
1306
1307static int
1308ietf_v1_parse_new_conn_id (const unsigned char *buf, size_t len,
1309                        uint64_t *seqno, uint64_t *retire_prior_to,
1310                        lsquic_cid_t *cid, const unsigned char **reset_token)
1311{
1312    const unsigned char *p = buf + 1;
1313    const unsigned char *const end = buf + len;
1314    unsigned char cid_len;
1315    int s;
1316
1317    s = vint_read(p, end, seqno);
1318    if (s < 0)
1319        return s;
1320    p += s;
1321
1322    s = vint_read(p, end, retire_prior_to);
1323    if (s < 0)
1324        return s;
1325    p += s;
1326
1327    if (p >= end)
1328        return -1;
1329
1330    cid_len = *p++;
1331    if (cid_len == 0 || cid_len > MAX_CID_LEN)
1332        return -2;
1333
1334    if ((unsigned) (end - p) < cid_len + IQUIC_SRESET_TOKEN_SZ)
1335        return -1;
1336    cid->len = cid_len;
1337    memcpy(cid->idbuf, p, cid_len);
1338    p += cid_len;
1339    if (reset_token)
1340        *reset_token = p;
1341    p += IQUIC_SRESET_TOKEN_SZ;
1342
1343    return p - buf;
1344}
1345
1346
1347/* Size of a frame that contains two varints */
1348static unsigned
1349ietf_v1_two_varints_size (uint64_t vals[2])
1350{
1351    unsigned vbits[2];
1352
1353    vbits[0] = vint_val2bits(vals[0]);
1354    vbits[1] = vint_val2bits(vals[1]);
1355    return 1u + (1u << vbits[0]) + (1u << vbits[1]);
1356}
1357
1358
1359static int
1360ietf_v1_gen_two_varints (unsigned char *buf, size_t len,
1361                                    unsigned char type, uint64_t vals[2])
1362{
1363    unsigned vbits[2];
1364    unsigned char *p;
1365
1366    vbits[0] = vint_val2bits(vals[0]);
1367    vbits[1] = vint_val2bits(vals[1]);
1368
1369    if (1u + (1u << vbits[0]) + (1u << vbits[1]) > len)
1370        return -1;
1371
1372    p = buf;
1373    *p++ = type;
1374    vint_write(p, vals[0], vbits[0], 1 << vbits[0]);
1375    p += 1 << vbits[0];
1376    vint_write(p, vals[1], vbits[1], 1 << vbits[1]);
1377    p += 1 << vbits[1];
1378
1379    return p - buf;
1380}
1381
1382
1383static int
1384ietf_v1_parse_two_varints (const unsigned char *buf, size_t len, uint64_t *vals[2])
1385{
1386    const unsigned char *p = buf;
1387    const unsigned char *const end = p + len;
1388    int s;
1389
1390    if (len < 2)
1391        return -1;
1392
1393    ++p;    /* Type */
1394
1395    s = vint_read(p, end, vals[0]);
1396    if (s < 0)
1397        return s;
1398    p += s;
1399
1400    s = vint_read(p, end, vals[1]);
1401    if (s < 0)
1402        return s;
1403    p += s;
1404
1405    return p - buf;
1406}
1407
1408
1409/* vals[0] is the frame type */
1410static unsigned
1411ietf_v1_frame_with_varints_size (unsigned n, uint64_t vals[])
1412{
1413    unsigned vbits, size;
1414
1415    assert(n > 0);
1416    vbits = vint_val2bits(vals[0]);
1417    size = 1 << vbits;
1418    while (--n)
1419    {
1420        vbits = vint_val2bits(vals[n]);
1421        size += 1 << vbits;
1422    }
1423
1424    return size;
1425}
1426
1427
1428/* vals[0] is the frame type */
1429static int
1430ietf_v1_gen_frame_with_varints (unsigned char *buf, size_t len,
1431                                    unsigned count, uint64_t vals[])
1432{
1433    unsigned vbits, n;
1434    unsigned char *p;
1435
1436    if (ietf_v1_frame_with_varints_size(count, vals) > len)
1437        return -1;
1438
1439    p = buf;
1440    for (n = 0; n < count; ++n)
1441    {
1442        vbits = vint_val2bits(vals[n]);
1443        vint_write(p, vals[n], vbits, 1 << vbits);
1444        p += 1 << vbits;
1445    }
1446
1447    return p - buf;
1448}
1449
1450
1451/* Frame type is checked when frame type is parsed.  The only use here is
1452 * to calculate skip length.
1453 */
1454static int
1455ietf_v1_parse_frame_with_varints (const unsigned char *buf, size_t len,
1456            const uint64_t frame_type, unsigned count, uint64_t *vals[])
1457{
1458    const unsigned char *p = buf;
1459    const unsigned char *const end = p + len;
1460    unsigned vbits, n;
1461    int s;
1462
1463    vbits = vint_val2bits(frame_type);
1464    p += 1 << vbits;
1465
1466    for (n = 0; n < count; ++n)
1467    {
1468        s = vint_read(p, end, vals[n]);
1469        if (s < 0)
1470            return s;
1471        p += s;
1472    }
1473
1474    return p - buf;
1475}
1476
1477
1478
1479static int
1480ietf_v1_parse_stream_blocked_frame (const unsigned char *buf, size_t len,
1481                            lsquic_stream_id_t *stream_id, uint64_t *offset)
1482{
1483    return ietf_v1_parse_two_varints(buf, len,
1484                                       (uint64_t *[]) { stream_id, offset, });
1485}
1486
1487
1488static unsigned
1489ietf_v1_stream_blocked_frame_size (lsquic_stream_id_t stream_id, uint64_t off)
1490{
1491    return ietf_v1_two_varints_size((uint64_t []) { stream_id, off, });
1492}
1493
1494
1495static int
1496ietf_v1_gen_streams_blocked_frame (unsigned char *buf, size_t len,
1497                                    enum stream_dir sd, uint64_t limit)
1498{
1499    return ietf_v1_gen_one_varint(buf, len, 0x16 + (sd == SD_UNI), limit);
1500}
1501
1502
1503static int
1504ietf_v1_parse_streams_blocked_frame (const unsigned char *buf, size_t len,
1505                                    enum stream_dir *sd, uint64_t *limit)
1506{
1507    int s;
1508
1509    s = ietf_v1_parse_one_varint(buf, len, limit);
1510    if (s > 0)
1511    {
1512        if (buf[0] == 0x16)
1513            *sd = SD_BIDI;
1514        else
1515            *sd = SD_UNI;
1516    }
1517    return s;
1518}
1519
1520
1521static unsigned
1522ietf_v1_streams_blocked_frame_size (uint64_t limit)
1523{
1524    return 1 + vint_size(limit);
1525}
1526
1527
1528static int
1529ietf_v1_gen_stream_blocked_frame (unsigned char *buf, size_t len,
1530                                    lsquic_stream_id_t stream_id, uint64_t off)
1531{
1532    return ietf_v1_gen_two_varints(buf, len, 0x15, (uint64_t[]){ stream_id, off, });
1533}
1534
1535
1536static int
1537ietf_v1_gen_max_stream_data_frame (unsigned char *buf, size_t len,
1538                                    lsquic_stream_id_t stream_id, uint64_t off)
1539{
1540    return ietf_v1_gen_two_varints(buf, len, 0x11, (uint64_t[]){ stream_id, off, });
1541}
1542
1543
1544static unsigned
1545ietf_v1_max_stream_data_frame_size (lsquic_stream_id_t stream_id, uint64_t off)
1546{
1547    return ietf_v1_two_varints_size((uint64_t []) { stream_id, off, });
1548}
1549
1550
1551
1552static int
1553ietf_v1_parse_max_stream_data_frame (const unsigned char *buf, size_t len,
1554                                lsquic_stream_id_t *stream_id, uint64_t *off)
1555{
1556    return ietf_v1_parse_two_varints(buf, len, (uint64_t *[]){ stream_id, off, });
1557}
1558
1559
1560static int
1561ietf_v1_parse_max_streams_frame (const unsigned char *buf, size_t len,
1562                                    enum stream_dir *sd, uint64_t *max_streams)
1563{
1564    int s;
1565
1566    s = ietf_v1_parse_one_varint(buf, len, max_streams);
1567    if (s > 0)
1568        *sd = buf[0] == 0x12 ? SD_BIDI : SD_UNI;
1569    return s;
1570}
1571
1572
1573static int
1574ietf_v1_gen_max_streams_frame (unsigned char *buf, size_t len,
1575                                    enum stream_dir sd, uint64_t limit)
1576{
1577    return ietf_v1_gen_one_varint(buf, len, 0x12 + (sd == SD_UNI), limit);
1578}
1579
1580
1581static unsigned
1582ietf_v1_max_streams_frame_size (uint64_t limit)
1583{
1584    return 1 + vint_size(limit);
1585}
1586
1587
1588static size_t
1589ietf_v1_new_token_frame_size (size_t token_sz)
1590{
1591    unsigned bits;
1592
1593    bits = vint_val2bits(token_sz);
1594    return 1 + (1 << bits) + token_sz;
1595}
1596
1597
1598static int
1599ietf_v1_gen_new_token_frame (unsigned char *buf, size_t buf_sz,
1600                                const unsigned char *token, size_t token_sz)
1601{
1602    unsigned char *p;
1603    unsigned bits;
1604
1605    bits = vint_val2bits(token_sz);
1606    if (buf_sz < 1 + (1 << bits) + token_sz)
1607    {
1608        errno = ENOBUFS;
1609        return -1;
1610    }
1611
1612    p = buf;
1613    *p++ = 0x07;
1614    vint_write(p, token_sz, bits, 1 << bits);
1615    p += 1 << bits;
1616    memcpy(p, token, token_sz);
1617    p += token_sz;
1618
1619    return p - buf;
1620}
1621
1622
1623static size_t
1624ietf_v1_new_connection_id_frame_size (unsigned seqno, unsigned scid_len)
1625{
1626    unsigned bits;
1627
1628    bits = vint_val2bits(seqno);
1629    return 1                /* Frame Type */
1630         + (1 << bits)      /* Sequence Number */
1631         + 1                /* Retire Prior To (we always set it to zero */
1632         + 1                /* CID length */
1633         + scid_len
1634         + IQUIC_SRESET_TOKEN_SZ;
1635}
1636
1637
1638static int
1639ietf_v1_gen_new_connection_id_frame (unsigned char *buf, size_t buf_sz,
1640            unsigned seqno, const struct lsquic_cid *cid,
1641            const unsigned char *token, size_t token_sz)
1642{
1643    unsigned char *p;
1644    unsigned bits;
1645
1646    if (buf_sz < ietf_v1_new_connection_id_frame_size(seqno, cid->len))
1647        return -1;
1648
1649    p = buf;
1650    *p++ = 0x18;
1651    bits = vint_val2bits(seqno);
1652    vint_write(p, seqno, bits, 1 << bits);
1653    p += 1 << bits;
1654    *p++ = 0;   /* Retire Prior To */
1655    *p++ = cid->len;
1656    memcpy(p, cid->idbuf, cid->len);
1657    p += cid->len;
1658    memcpy(p, token, token_sz);
1659    p += token_sz;
1660
1661    return p - buf;
1662}
1663
1664
1665/* [draft-ietf-quic-transport-17] Section-17.2 */
1666static const enum header_type bits2ht[4] =
1667{
1668    [0] = HETY_INITIAL,
1669    [1] = HETY_0RTT,
1670    [2] = HETY_HANDSHAKE,
1671    [3] = HETY_RETRY,
1672};
1673
1674
1675int
1676lsquic_ietf_v1_parse_packet_in_long_begin (struct lsquic_packet_in *packet_in,
1677                size_t length, int is_server, unsigned cid_len,
1678                struct packin_parse_state *state)
1679{
1680    const unsigned char *p = packet_in->pi_data;
1681    const unsigned char *const end = p + length;
1682    lsquic_ver_tag_t tag;
1683    enum header_type header_type;
1684    unsigned dcil, scil;
1685    int verneg, r;
1686    unsigned char first_byte;
1687    uint64_t payload_len, token_len;
1688
1689    if (length < 6)
1690        return -1;
1691    first_byte = *p++;
1692
1693    memcpy(&tag, p, 4);
1694    p += 4;
1695    verneg = 0 == tag;
1696    if (!verneg)
1697        header_type = bits2ht[ (first_byte >> 4) & 3 ];
1698    else
1699        header_type = HETY_VERNEG;
1700
1701    packet_in->pi_header_type = header_type;
1702
1703    dcil = *p++;
1704    if (p + dcil >= end || dcil > MAX_CID_LEN)
1705        return -1;
1706    if (dcil)
1707    {
1708        memcpy(packet_in->pi_dcid.idbuf, p, dcil);
1709        packet_in->pi_flags |= PI_CONN_ID;
1710        p += dcil;
1711    }
1712    packet_in->pi_dcid.len = dcil;
1713
1714    scil = *p++;
1715    if (p + scil > end || scil > MAX_CID_LEN)
1716        return -1;
1717    if (scil)
1718    {
1719        packet_in->pi_scid_off = p - packet_in->pi_data;
1720        p += scil;
1721    }
1722    packet_in->pi_scid_len = scil;
1723
1724    switch (header_type)
1725    {
1726    case HETY_INITIAL:
1727        if (is_server && dcil < MIN_INITIAL_DCID_LEN)
1728            return -1;
1729        r = vint_read(p, end, &token_len);
1730        if (r < 0)
1731            return -1;
1732        if (token_len && !is_server)
1733        {
1734            /* From [draft-ietf-quic-transport-14]:
1735             *
1736             *  Token Length:  A variable-length integer specifying the
1737             *  length of the Token field, in bytes.  This value is zero
1738             *  if no token is present.  Initial packets sent by the
1739             *  server MUST set the Token Length field to zero; clients
1740             *  that receive an Initial packet with a non-zero Token
1741             *  Length field MUST either discard the packet or generate
1742             *  a connection error of type PROTOCOL_VIOLATION.
1743             */
1744            return -1;
1745        }
1746        p += r;
1747        if (token_len)
1748        {
1749            if (token_len >=
1750                        1ull << (sizeof(packet_in->pi_token_size) * 8))
1751                return -1;
1752            if (p + token_len > end)
1753                return -1;
1754            packet_in->pi_token = p - packet_in->pi_data;
1755            packet_in->pi_token_size = token_len;
1756            p += token_len;
1757        }
1758        /* fall-through */
1759    case HETY_HANDSHAKE:
1760    case HETY_0RTT:
1761        if (p >= end)
1762            return -1;
1763        r = vint_read(p, end, &payload_len);
1764        if (r < 0)
1765            return -1;
1766        p += r;
1767        if (p - packet_in->pi_data + payload_len > length)
1768            return -1;
1769        length = p - packet_in->pi_data + payload_len;
1770        if (end - p < 4)
1771            return -1;
1772        state->pps_p      = p - r;
1773        state->pps_nbytes = r;
1774        packet_in->pi_quic_ver = 1;
1775        break;
1776    case HETY_RETRY:
1777        if (p >= end)
1778            return -1;
1779        if (p
1780            /* [draft-ietf-quic-transport-25] Section 17.2.5 says that "a
1781             * client MUST discard a Retry packet with a zero-length Retry
1782             * Token field."  We might as well do it here.
1783             */
1784            + 1
1785            /* Integrity tag length: */
1786            + 16 > end)
1787                return -1;
1788        packet_in->pi_token = p - packet_in->pi_data;
1789        packet_in->pi_token_size = end - p - 16;
1790        /* Tag validation happens later */
1791        p = end;
1792        length = end - packet_in->pi_data;
1793        state->pps_p      = NULL;
1794        state->pps_nbytes = 0;
1795        packet_in->pi_quic_ver = 1;
1796        break;
1797    default:
1798        assert(header_type == HETY_VERNEG);
1799        if (p >= end || (3 & (uintptr_t) (end - p)))
1800            return -1;
1801        packet_in->pi_quic_ver = p - packet_in->pi_data;
1802        p = end;
1803        state->pps_p      = NULL;
1804        state->pps_nbytes = 0;
1805        break;
1806    }
1807
1808    packet_in->pi_header_sz     = p - packet_in->pi_data;
1809    packet_in->pi_data_sz       = length;
1810    packet_in->pi_nonce         = 0;
1811    packet_in->pi_refcnt        = 0;
1812    packet_in->pi_frame_types   = 0;
1813    memset(&packet_in->pi_next, 0, sizeof(packet_in->pi_next));
1814    packet_in->pi_refcnt        = 0;
1815    packet_in->pi_received      = 0;
1816
1817    /* Packet number is set to an invalid value.  The packet number must
1818     * be decrypted, which happens later.
1819     */
1820    packet_in->pi_packno        = 1ULL << 62;
1821
1822    return 0;
1823}
1824
1825
1826/* Is this a valid Initial packet?  We take the perspective of the server. */
1827int
1828lsquic_is_valid_ietf_v1_or_Q046plus_hs_packet (const unsigned char *buf,
1829                                        size_t length, lsquic_ver_tag_t *tagp)
1830{
1831    const unsigned char *p = buf;
1832    const unsigned char *const end = p + length;
1833    lsquic_ver_tag_t tag;
1834    enum header_type header_type;
1835    unsigned dcil, scil;
1836    int r;
1837    unsigned char first_byte;
1838    uint64_t payload_len, token_len, packet_len;
1839
1840    if (length < 6)
1841        return 0;
1842    first_byte = *p++;
1843
1844    header_type = bits2ht[ (first_byte >> 4) & 3 ];
1845    if (header_type != HETY_INITIAL)
1846        return 0;
1847
1848    memcpy(&tag, p, 4);
1849    p += 4;
1850    switch (tag)
1851    {
1852    case 0:
1853        return 0;       /* Client never sends version negotiation packets */
1854    case TAG('Q', '0', '4', '6'):
1855        dcil = p[0] >> 4;
1856        if (dcil)
1857            dcil += 3;
1858        scil = p[0] & 0xF;
1859        if (scil)
1860            scil += 3;
1861        ++p;
1862
1863        if (!(dcil == GQUIC_CID_LEN && scil == 0))
1864            return 0;
1865
1866        packet_len = first_byte & 3;
1867
1868        if (end - p < (ptrdiff_t) (dcil + scil + packet_len))
1869            return 0;
1870        break;
1871    case TAG('Q', '0', '5', '0'):
1872        dcil = *p++;
1873        if (dcil != 8)
1874            return 0;
1875        if (p + dcil + 1 >= end)
1876            return 0;
1877        p += dcil;
1878        scil = *p++;
1879        if (scil != 0)
1880            return 0;
1881        goto read_token;
1882    default:
1883        dcil = *p++;
1884        if (dcil < MIN_INITIAL_DCID_LEN || dcil > MAX_CID_LEN)
1885            return 0;
1886        if (p + dcil >= end)
1887            return 0;
1888        p += dcil;
1889        scil = *p++;
1890        if (p + scil > end || scil > MAX_CID_LEN)
1891            return 0;
1892        p += scil;
1893  read_token:
1894        r = vint_read(p, end, &token_len);
1895        if (r < 0)
1896            return 0;
1897        p += r;
1898        p += token_len;
1899        if (p >= end)
1900            return 0;
1901        r = vint_read(p, end, &payload_len);
1902        if (r < 0)
1903            return 0;
1904        p += r;
1905        if (p - buf + payload_len > length)
1906            return 0;
1907        if (end - p < 4)
1908            return 0;
1909    }
1910
1911    *tagp = tag;
1912    return 1;
1913}
1914
1915
1916int
1917lsquic_ietf_v1_parse_packet_in_short_begin (struct lsquic_packet_in *packet_in,
1918                size_t length, int is_server, unsigned cid_len,
1919                struct packin_parse_state *state)
1920{
1921    unsigned char byte;
1922    unsigned header_sz;
1923
1924    /* By the time this function has been called, we know length is non-zero */
1925    byte = packet_in->pi_data[0];
1926
1927    /* [draft-ietf-quic-transport-17] Section 17.3 */
1928    /* 01SRRKPP */
1929
1930    if (cid_len)
1931    {
1932        header_sz = 1 + cid_len;
1933        if (length < header_sz)
1934            return -1;
1935        memcpy(packet_in->pi_dcid.idbuf, packet_in->pi_data + 1, cid_len);
1936        packet_in->pi_dcid.len = cid_len;
1937        packet_in->pi_flags |= PI_CONN_ID;
1938    }
1939    else
1940        header_sz = 1;
1941
1942    packet_in->pi_flags |= ((byte & 0x20) > 0) << PIBIT_SPIN_SHIFT;
1943    packet_in->pi_flags |= (byte & 3) << PIBIT_BITS_SHIFT;
1944
1945    packet_in->pi_header_sz     = header_sz;
1946    packet_in->pi_data_sz       = length;
1947    packet_in->pi_quic_ver      = 0;
1948    packet_in->pi_nonce         = 0;
1949    packet_in->pi_refcnt        = 0;
1950    packet_in->pi_frame_types   = 0;
1951    memset(&packet_in->pi_next, 0, sizeof(packet_in->pi_next));
1952    packet_in->pi_refcnt        = 0;
1953    packet_in->pi_received      = 0;
1954
1955    /* This is so that Q046 works, ID-18 code does not use it */
1956    state->pps_p                = packet_in->pi_data + header_sz;
1957    state->pps_nbytes           = 1 + (byte & 3);
1958
1959    return 0;
1960}
1961
1962
1963#if __GNUC__
1964#   define popcount __builtin_popcount
1965#else
1966static int
1967popcount (unsigned v)
1968{
1969    int count, i;
1970    for (i = 0, count = 0; i < sizeof(v) * 8; ++i)
1971        if (v & (1 << i))
1972            ++count;
1973    return count;
1974}
1975#endif
1976
1977
1978int
1979lsquic_ietf_v1_gen_ver_nego_pkt (unsigned char *buf, size_t bufsz,
1980         const lsquic_cid_t *scid, const lsquic_cid_t *dcid, unsigned versions,
1981         uint8_t rand)
1982{
1983    size_t need;
1984    int r;
1985
1986    need = 1 /* Type */ + 4 /* Version */ + 1 /* DCIL */
1987                + dcid->len + 1 /* SCIL */ + scid->len + popcount(versions) * 4;
1988
1989    if (need > bufsz)
1990        return -1;
1991
1992    *buf++ = 0x80 | QUIC_BIT | rand;
1993    memset(buf, 0, 4);
1994    buf += 4;
1995
1996    /* From [draft-ietf-quic-transport-22], Section 17.2.1:
1997     *
1998     *  The server MUST include the value from the Source Connection ID field
1999     *  of the packet it receives in the Destination Connection ID field.
2000     *  The value for Source Connection ID MUST be copied from the
2001     *  Destination Connection ID of the received packet, which is initially
2002     *  randomly selected by a client.  Echoing both connection IDs gives
2003     *  clients some assurance that the server received the packet and that
2004     *  the Version Negotiation packet was not generated by an off-path
2005     *  attacker.
2006     */
2007
2008    *buf++ = dcid->len;
2009    memcpy(buf, dcid->idbuf, dcid->len);
2010    buf += dcid->len;
2011    *buf++ = scid->len;
2012    memcpy(buf, scid->idbuf, scid->len);
2013    buf += scid->len;
2014
2015    r = lsquic_gen_ver_tags(buf, bufsz - 1 - 4 - 2 - dcid->len - scid->len,
2016                                                                    versions);
2017    if (r < 0)
2018        return -1;
2019    assert((unsigned) r == popcount(versions) * 4u);
2020
2021    return need;
2022}
2023
2024
2025static int
2026ietf_v1_gen_handshake_done_frame (unsigned char *buf, size_t buf_len)
2027{
2028    if (buf_len > 0)
2029    {
2030        *buf = 0x1E;
2031        return 1;
2032    }
2033    else
2034        return -1;
2035}
2036
2037
2038static int
2039ietf_v1_parse_handshake_done_frame (const unsigned char *buf, size_t buf_len)
2040{
2041    assert(buf[0] == 0x1E);
2042    assert(buf_len > 0);
2043    return 1;
2044}
2045
2046
2047static int
2048ietf_v1_gen_ack_frequency_frame (unsigned char *buf, size_t buf_len,
2049    uint64_t seqno, uint64_t pack_tol, uint64_t upd_mad, int ignore)
2050{
2051    int sz;
2052
2053    sz = ietf_v1_gen_frame_with_varints(buf, buf_len, 4,
2054        (uint64_t[]){ FRAME_TYPE_ACK_FREQUENCY, seqno, pack_tol, upd_mad });
2055    if (sz > 0 && (size_t) sz < buf_len)
2056    {
2057        buf[sz++] = !!ignore;
2058        return sz;
2059    }
2060    else
2061        return -1;
2062}
2063
2064
2065static int
2066ietf_v1_parse_ack_frequency_frame (const unsigned char *buf, size_t buf_len,
2067    uint64_t *seqno, uint64_t *pack_tol, uint64_t *upd_mad, int *ignore)
2068{
2069    int sz;
2070
2071    sz = ietf_v1_parse_frame_with_varints(buf, buf_len,
2072                FRAME_TYPE_ACK_FREQUENCY,
2073                3, (uint64_t *[]) { seqno, pack_tol, upd_mad });
2074    if (sz > 0 && (size_t) sz < buf_len && buf[sz] < 2)
2075    {
2076        *ignore = buf[sz++];
2077        return sz;
2078    }
2079    else
2080        return -1;
2081}
2082
2083
2084static unsigned
2085ietf_v1_ack_frequency_frame_size (uint64_t seqno, uint64_t pack_tol,
2086    uint64_t upd_mad)
2087{
2088    return 1 + ietf_v1_frame_with_varints_size(4,
2089            (uint64_t[]){ FRAME_TYPE_ACK_FREQUENCY, seqno, pack_tol, upd_mad });
2090}
2091
2092
2093static unsigned
2094ietf_v1_handshake_done_frame_size (void)
2095{
2096    return 1;
2097}
2098
2099
2100static int
2101ietf_v1_gen_timestamp_frame (unsigned char *buf, size_t buf_len,
2102                                                            uint64_t timestamp)
2103{
2104    return ietf_v1_gen_frame_with_varints(buf, buf_len, 2,
2105                            (uint64_t[]){ FRAME_TYPE_TIMESTAMP, timestamp });
2106}
2107
2108
2109static int
2110ietf_v1_parse_timestamp_frame (const unsigned char *buf, size_t buf_len,
2111                                                            uint64_t *timestamp)
2112{
2113    return ietf_v1_parse_frame_with_varints(buf, buf_len,
2114                FRAME_TYPE_TIMESTAMP, 1, (uint64_t *[]) { timestamp });
2115}
2116
2117
2118const struct parse_funcs lsquic_parse_funcs_ietf_v1 =
2119{
2120    .pf_gen_reg_pkt_header            =  ietf_v1_gen_reg_pkt_header,
2121    .pf_parse_packet_in_finish        =  ietf_v1_parse_packet_in_finish,
2122    .pf_gen_stream_frame              =  ietf_v1_gen_stream_frame,
2123    .pf_calc_stream_frame_header_sz   =  ietf_v1_calc_stream_frame_header_sz,
2124    .pf_parse_stream_frame            =  ietf_v1_parse_stream_frame,
2125    .pf_parse_ack_frame               =  ietf_v1_parse_ack_frame,
2126    .pf_gen_ack_frame                 =  ietf_v1_gen_ack_frame,
2127    .pf_gen_blocked_frame             =  ietf_v1_gen_blocked_frame,
2128    .pf_parse_blocked_frame           =  ietf_v1_parse_blocked_frame,
2129    .pf_blocked_frame_size            =  ietf_v1_blocked_frame_size,
2130    .pf_rst_frame_size                =  ietf_v1_rst_frame_size,
2131    .pf_gen_rst_frame                 =  ietf_v1_gen_rst_frame,
2132    .pf_parse_rst_frame               =  ietf_v1_parse_rst_frame,
2133    .pf_connect_close_frame_size      =  ietf_v1_connect_close_frame_size,
2134    .pf_gen_connect_close_frame       =  ietf_v1_gen_connect_close_frame,
2135    .pf_parse_connect_close_frame     =  ietf_v1_parse_connect_close_frame,
2136    .pf_gen_ping_frame                =  ietf_v1_gen_ping_frame,
2137    .pf_parse_frame_type              =  ietf_v1_parse_frame_type,
2138    .pf_turn_on_fin                   =  ietf_v1_turn_on_fin,
2139    .pf_packout_size                  =  ietf_v1_packout_size,
2140    .pf_packout_max_header_size       =  ietf_v1_packout_max_header_size,
2141    .pf_path_chal_frame_size          =  ietf_v1_path_chal_frame_size,
2142    .pf_parse_path_chal_frame         =  ietf_v1_parse_path_chal_frame,
2143    .pf_gen_path_chal_frame           =  ietf_v1_gen_path_chal_frame,
2144    .pf_path_resp_frame_size          =  ietf_v1_path_resp_frame_size,
2145    .pf_gen_path_resp_frame           =  ietf_v1_gen_path_resp_frame,
2146    .pf_parse_path_resp_frame         =  ietf_v1_parse_path_resp_frame,
2147    .pf_calc_packno_bits              =  ietf_v1_calc_packno_bits,
2148    .pf_packno_bits2len               =  ietf_v1_packno_bits2len,
2149    .pf_gen_crypto_frame              =  ietf_v1_gen_crypto_frame,
2150    .pf_parse_crypto_frame            =  ietf_v1_parse_crypto_frame,
2151    .pf_calc_crypto_frame_header_sz   =  ietf_v1_calc_crypto_frame_header_sz,
2152    .pf_parse_max_data                =  ietf_v1_parse_max_data,
2153    .pf_gen_max_data_frame            =  ietf_v1_gen_max_data_frame,
2154    .pf_max_data_frame_size           =  ietf_v1_max_data_frame_size,
2155    .pf_parse_new_conn_id             =  ietf_v1_parse_new_conn_id,
2156    .pf_gen_stream_blocked_frame      =  ietf_v1_gen_stream_blocked_frame,
2157    .pf_parse_stream_blocked_frame    =  ietf_v1_parse_stream_blocked_frame,
2158    .pf_stream_blocked_frame_size     =  ietf_v1_stream_blocked_frame_size,
2159    .pf_gen_max_stream_data_frame     =  ietf_v1_gen_max_stream_data_frame,
2160    .pf_parse_max_stream_data_frame   =  ietf_v1_parse_max_stream_data_frame,
2161    .pf_max_stream_data_frame_size    =  ietf_v1_max_stream_data_frame_size,
2162    .pf_parse_stop_sending_frame      =  ietf_v1_parse_stop_sending_frame,
2163    .pf_gen_stop_sending_frame        =  ietf_v1_gen_stop_sending_frame,
2164    .pf_stop_sending_frame_size       =  ietf_v1_stop_sending_frame_size,
2165    .pf_parse_new_token_frame         =  ietf_v1_parse_new_token_frame,
2166    .pf_new_connection_id_frame_size  =  ietf_v1_new_connection_id_frame_size,
2167    .pf_gen_new_connection_id_frame   =  ietf_v1_gen_new_connection_id_frame,
2168    .pf_new_token_frame_size          =  ietf_v1_new_token_frame_size,
2169    .pf_gen_new_token_frame           =  ietf_v1_gen_new_token_frame,
2170    .pf_parse_retire_cid_frame        =  ietf_v1_parse_retire_cid_frame,
2171    .pf_gen_retire_cid_frame          =  ietf_v1_gen_retire_cid_frame,
2172    .pf_retire_cid_frame_size         =  ietf_v1_retire_cid_frame_size,
2173    .pf_gen_streams_blocked_frame     =  ietf_v1_gen_streams_blocked_frame,
2174    .pf_parse_streams_blocked_frame   =  ietf_v1_parse_streams_blocked_frame,
2175    .pf_streams_blocked_frame_size    =  ietf_v1_streams_blocked_frame_size,
2176    .pf_gen_max_streams_frame         =  ietf_v1_gen_max_streams_frame,
2177    .pf_parse_max_streams_frame       =  ietf_v1_parse_max_streams_frame,
2178    .pf_max_streams_frame_size        =  ietf_v1_max_streams_frame_size,
2179    .pf_gen_handshake_done_frame      =  ietf_v1_gen_handshake_done_frame,
2180    .pf_parse_handshake_done_frame    =  ietf_v1_parse_handshake_done_frame,
2181    .pf_handshake_done_frame_size     =  ietf_v1_handshake_done_frame_size,
2182    .pf_gen_ack_frequency_frame       =  ietf_v1_gen_ack_frequency_frame,
2183    .pf_parse_ack_frequency_frame     =  ietf_v1_parse_ack_frequency_frame,
2184    .pf_ack_frequency_frame_size      =  ietf_v1_ack_frequency_frame_size,
2185    .pf_gen_timestamp_frame           =  ietf_v1_gen_timestamp_frame,
2186    .pf_parse_timestamp_frame         =  ietf_v1_parse_timestamp_frame,
2187};
2188