lsquic_parse_ietf_v1.c revision 2f2f4363
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
475static int
476ietf_v1_dec_stream_frame_size (unsigned char *p, size_t new_size)
477{
478    /* 0b00001XXX
479     *  0x4     OFF
480     *  0x2     LEN
481     *  0x1     FIN
482     */
483    unsigned bits;
484
485    const char type = *p++;
486    if (!(type & 0x2))
487        return 1;
488
489    /* Stream ID */
490    bits = *p >> 6;
491    p += 1 << bits;
492
493    if (type & 0x4)
494    {
495        /* Offset */
496        bits = *p >> 6;
497        p += 1 << bits;
498    }
499
500    /* Write new size */
501    bits = *p >> 6;
502    vint_write(p, new_size, bits, 1 << bits);
503    return 0;
504}
505
506
507/* return parsed (used) buffer length */
508static int
509ietf_v1_parse_stream_frame (const unsigned char *buf, size_t rem_packet_sz,
510                                        struct stream_frame *stream_frame)
511{
512    /* 0b00001XXX
513     *  0x4     OFF
514     *  0x2     LEN
515     *  0x1     FIN
516     */
517    const unsigned char *const pend = buf + rem_packet_sz;
518    const unsigned char *p = buf;
519    lsquic_stream_id_t stream_id;
520    uint64_t offset, data_sz;
521    int r;
522
523    CHECK_SPACE(1, p, pend);
524    const char type = *p++;
525
526    r = vint_read(p, pend, &stream_id);
527    if (r < 0)
528        return -1;
529    p += r;
530
531    if (type & 0x4)
532    {
533        r = vint_read(p, pend, &offset);
534        if (r < 0)
535            return -1;
536        p += r;
537    }
538    else
539        offset = 0;
540
541    if (type & 0x2)
542    {
543        r = vint_read(p, pend, &data_sz);
544        if (r < 0)
545            return -1;
546        p += r;
547        CHECK_SPACE(data_sz, p, pend);
548    }
549    else
550        data_sz = pend - p;
551
552    /* Largest offset cannot exceed this value and we MUST detect this error */
553    if (VINT_MAX_VALUE - offset < data_sz)
554        return -1;
555
556    stream_frame->stream_id             = stream_id;
557    stream_frame->data_frame.df_fin     = type & 0x1;
558    stream_frame->data_frame.df_offset  = offset;
559    stream_frame->data_frame.df_size    = data_sz;
560    stream_frame->data_frame.df_data    = p;
561    stream_frame->data_frame.df_read_off= 0;
562    stream_frame->packet_in             = NULL;
563
564    assert(p <= pend);
565
566    return p + data_sz - (unsigned char *) buf;
567}
568
569
570int
571lsquic_ietf_v1_parse_crypto_frame (const unsigned char *buf,
572                    size_t rem_packet_sz, struct stream_frame *stream_frame)
573{
574    const unsigned char *const pend = buf + rem_packet_sz;
575    const unsigned char *p = buf;
576    uint64_t offset, data_sz;
577    int r;
578
579    CHECK_SPACE(1, p, pend);
580
581    ++p;
582
583    r = vint_read(p, pend, &offset);
584    if (r < 0)
585        return -1;
586    p += r;
587
588    r = vint_read(p, pend, &data_sz);
589    if (r < 0)
590        return -1;
591    p += r;
592    CHECK_SPACE(data_sz, p, pend);
593
594    /* Largest offset cannot exceed this value and we MUST detect this error */
595    if (VINT_MAX_VALUE - offset < data_sz)
596        return -1;
597
598    stream_frame->stream_id             = ~0ULL;    /* Unset */
599    stream_frame->data_frame.df_fin     = 0;
600    stream_frame->data_frame.df_offset  = offset;
601    stream_frame->data_frame.df_size    = data_sz;
602    stream_frame->data_frame.df_data    = p;
603    stream_frame->data_frame.df_read_off= 0;
604    stream_frame->packet_in             = NULL;
605
606    assert(p <= pend);
607
608    return p + data_sz - (unsigned char *) buf;
609}
610
611
612static int
613ietf_v1_parse_crypto_frame (const unsigned char *buf, size_t rem_packet_sz,
614                                        struct stream_frame *stream_frame)
615{
616    if (rem_packet_sz > 0)
617    {
618        assert(0x06 == buf[0]);
619        return lsquic_ietf_v1_parse_crypto_frame(buf, rem_packet_sz,
620                                                            stream_frame);
621    }
622    else
623        return -1;
624}
625
626
627#if __GNUC__
628#   define UNLIKELY(cond) __builtin_expect(cond, 0)
629#else
630#   define UNLIKELY(cond) cond
631#endif
632
633
634/* Bits 10 (2) is ECT(0); * bits 01 (1) is ECT(1). */
635static const int ecnmap[4] = { 0, 2, 1, 3, };
636
637
638static int
639ietf_v1_parse_ack_frame (const unsigned char *const buf, size_t buf_len,
640                                            struct ack_info *ack, uint8_t exp)
641{
642    const unsigned char *p = buf;
643    const unsigned char *const end = buf + buf_len;
644    uint64_t block_count, gap, block;
645    enum ecn ecn;
646    unsigned i;
647    int r;
648
649    ++p;
650    r = vint_read(p, end, &ack->ranges[0].high);
651    if (UNLIKELY(r < 0))
652        return -1;
653    p += r;
654    r = vint_read(p, end, &ack->lack_delta);
655    if (UNLIKELY(r < 0))
656        return -1;
657    p += r;
658    ack->lack_delta <<= exp;
659    r = vint_read(p, end, &block_count);
660    if (UNLIKELY(r < 0))
661        return -1;
662    p += r;
663    r = vint_read(p, end, &block);
664    if (UNLIKELY(r < 0))
665        return -1;
666    ack->ranges[0].low = ack->ranges[0].high - block;
667    if (UNLIKELY(ack->ranges[0].high < ack->ranges[0].low))
668        return -1;
669    p += r;
670
671    for (i = 1; i <= block_count; ++i)
672    {
673        r = vint_read(p, end, &gap);
674        if (UNLIKELY(r < 0))
675            return -1;
676        p += r;
677        r = vint_read(p, end, &block);
678        if (UNLIKELY(r < 0))
679            return -1;
680        p += r;
681        if (i < sizeof(ack->ranges) / sizeof(ack->ranges[0]))
682        {
683            ack->ranges[i].high = ack->ranges[i - 1].low - gap - 2;
684            ack->ranges[i].low  = ack->ranges[i].high - block;
685            if (UNLIKELY(ack->ranges[i].high >= ack->ranges[i - 1].low
686                         || ack->ranges[i].high < ack->ranges[i].low))
687                return -1;
688        }
689    }
690
691    if (i < sizeof(ack->ranges) / sizeof(ack->ranges[0]))
692    {
693        ack->flags = 0;
694        ack->n_ranges = block_count + 1;
695    }
696    else
697    {
698        ack->flags = AI_TRUNCATED;
699        ack->n_ranges = sizeof(ack->ranges) / sizeof(ack->ranges[0]);
700    }
701
702
703    if (0x03 == buf[0])
704    {
705        for (ecn = 1; ecn <= 3; ++ecn)
706        {
707            r = vint_read(p, end, &ack->ecn_counts[ecnmap[ecn]]);
708            if (UNLIKELY(r < 0))
709                return -1;
710            p += r;
711        }
712        ack->flags |= AI_ECN;
713    }
714
715    return p - buf;
716}
717
718
719static unsigned
720ietf_v1_rst_frame_size (lsquic_stream_id_t stream_id, uint64_t error_code,
721                                                            uint64_t final_size)
722{
723    return 1                        /* Type */
724         + vint_size(stream_id)   /* Stream ID (i) */
725         + vint_size(error_code)  /* Application Error Code (i) */
726         + vint_size(final_size); /* Final Size (i) */
727}
728
729
730static int
731ietf_v1_gen_rst_frame (unsigned char *buf, size_t buf_len,
732        lsquic_stream_id_t stream_id, uint64_t final_size, uint64_t error_code)
733{
734    unsigned vbits;
735    unsigned char *p;
736
737    if (buf_len < ietf_v1_rst_frame_size(stream_id, error_code, final_size))
738        return -1;
739    p = buf;
740
741    *p++ = 0x04;
742    /* Stream ID (i) */
743    vbits = vint_val2bits(stream_id);
744    vint_write(p, stream_id, vbits, 1 << vbits);
745    p += 1 << vbits;
746
747    /* Application Error Code (i) */
748    vbits = vint_val2bits(error_code);
749    vint_write(p, error_code, vbits, 1 << vbits);
750    p += 1 << vbits;
751
752    /* Final Size (i) */
753    vbits = vint_val2bits(final_size);
754    vint_write(p, final_size, vbits, 1 << vbits);
755    p += 1 << vbits;
756
757    return p - buf;
758}
759
760
761static int
762ietf_v1_parse_rst_frame (const unsigned char *buf, size_t buf_len,
763    lsquic_stream_id_t *stream_id_p, uint64_t *final_size_p, uint64_t *error_code_p)
764{
765    const unsigned char *p = buf + 1;
766    const unsigned char *const end = buf + buf_len;
767    uint64_t stream_id, final_size, error_code;
768    int r;
769
770    /* Stream ID (i) */
771    r = vint_read(p, end, &stream_id);
772    if (r < 0)
773        return r;
774    p += r;
775
776    /* Application Error Code (i) */
777    r = vint_read(p, end, &error_code);
778    if (r < 0)
779        return r;
780    p += r;
781
782    /* Final Size (i) */
783    r = vint_read(p, end, &final_size);
784    if (r < 0)
785        return r;
786    p += r;
787
788    *stream_id_p = stream_id;
789    *final_size_p = final_size;
790    *error_code_p = error_code;
791
792    return p - buf;
793}
794
795
796static int
797ietf_v1_parse_stop_sending_frame (const unsigned char *buf, size_t buf_len,
798                        lsquic_stream_id_t *stream_id, uint64_t *error_code)
799{
800    const unsigned char *p = buf + 1;
801    const unsigned char *const end = buf + buf_len;
802    int r;
803
804    r = vint_read(p, end, stream_id);
805    if (r < 0)
806        return r;
807    p += r;
808
809    r = vint_read(p, end, error_code);
810    if (r < 0)
811        return r;
812    p += r;
813
814    return p - buf;
815}
816
817
818static int
819ietf_v1_gen_stop_sending_frame (unsigned char *buf, size_t len,
820                            lsquic_stream_id_t stream_id, uint64_t error_code)
821{
822    return ietf_v1_gen_two_varints(buf, len, 0x05, (uint64_t[]){ stream_id,
823                                                                error_code, });
824}
825
826
827static unsigned
828ietf_v1_stop_sending_frame_size (lsquic_stream_id_t val, uint64_t error_code)
829{
830    return 1 + vint_size(val) + vint_size(error_code);
831}
832
833
834static int
835ietf_v1_parse_new_token_frame (const unsigned char *buf, size_t buf_len,
836                            const unsigned char **token, size_t *token_size_p)
837{
838    uint64_t token_size;
839    const unsigned char *p = buf + 1;
840    const unsigned char *const end = buf + buf_len;
841    int r;
842
843    r = vint_read(p, end, &token_size);
844    if (r < 0)
845        return r;
846    p += r;
847
848    if (p + token_size > end)
849        return -1;
850    *token = p;
851    p += token_size;
852    *token_size_p = token_size;
853
854    return p - buf;
855}
856
857
858static int
859ietf_v1_gen_ping_frame (unsigned char *buf, int buf_len)
860{
861    if (buf_len > 0)
862    {
863        buf[0] = 0x01;
864        return 1;
865    }
866    else
867        return -1;
868}
869
870
871static size_t
872ietf_v1_connect_close_frame_size (int app_error, unsigned error_code,
873                                unsigned frame_type, size_t reason_len)
874{
875    return 1                                                         /* Type */
876         + (1 << vint_val2bits(error_code))                    /* Error code */
877         + (app_error ? 0 : 1 << vint_val2bits(frame_type))    /* Frame type */
878         + (1 << vint_val2bits(reason_len))          /* Reason Phrase Length */
879         + reason_len
880         ;
881}
882
883
884static int
885ietf_v1_gen_connect_close_frame (unsigned char *buf, size_t buf_len,
886    int app_error, unsigned error_code, const char *reason, int reason_len)
887{
888    size_t needed;
889    unsigned bits_error, bits_reason;
890    unsigned char *p;
891
892    assert(!!reason == !!reason_len);
893
894    bits_reason = vint_val2bits(reason_len);
895    bits_error = vint_val2bits(error_code);
896    needed = 1 /* Type */ + (1 << bits_error)
897           + (app_error ? 0 : 1) /* Frame type */
898        /* TODO: frame type instead of just zero */
899           + (1 << bits_reason) + reason_len;
900
901    if (buf_len < needed)
902        return -1;
903
904    p = buf;
905    *p = 0x1C + !!app_error;
906    ++p;
907    vint_write(p, error_code, bits_error, 1 << bits_error);
908    p += 1 << bits_error;
909    if (!app_error)
910        *p++ = 0;   /* Frame type */ /* TODO */
911    vint_write(p, reason_len, bits_reason, 1 << bits_reason);
912    p += 1 << bits_reason;
913    if (reason_len)
914    {
915        memcpy(p, reason, reason_len);
916        p += reason_len;
917    }
918
919    assert((unsigned) (p - buf) == needed);
920    return p - buf;
921}
922
923
924static int
925ietf_v1_parse_connect_close_frame (const unsigned char *buf, size_t buf_len,
926        int *app_error_p, uint64_t *error_code, uint16_t *reason_len,
927        uint8_t *reason_offset)
928{
929    const unsigned char *const pend = buf + buf_len;
930    const unsigned char *p = buf + 1;
931    uint64_t len;
932    ptrdiff_t off;
933    int app_error, r;
934
935    r = vint_read(p, pend, error_code);
936    if (r < 0)
937        return -1;
938    p += r;
939
940    app_error = buf[0] == 0x1D;
941    if (!app_error)
942    {
943        r = vint_read(p, pend, &len);
944        if (r < 0)
945            return -1;
946        p += r;
947    }
948
949    r = vint_read(p, pend, &len);
950    if (r < 0)
951        return -1;
952    if (len > UINT16_MAX)
953        return -1;
954    p += r;
955
956    off = p - buf;
957    if (buf_len < off + len)
958        return -2;
959
960    *app_error_p = app_error;
961    *reason_len = len;
962    *reason_offset = off;
963    return off + len;
964}
965
966
967/* Returns number of bytes written or -1 on failure */
968/* This function makes an assumption that there is at least one range */
969static int
970ietf_v1_gen_ack_frame (unsigned char *outbuf, size_t outbuf_sz,
971        gaf_rechist_first_f rechist_first, gaf_rechist_next_f rechist_next,
972        gaf_rechist_largest_recv_f rechist_largest_recv,
973        void *rechist, lsquic_time_t now, int *has_missing,
974        lsquic_packno_t *largest_received, const uint64_t *ecn_counts)
975{
976    unsigned char *block_count_p, *p = outbuf;
977    unsigned char *const end = p + outbuf_sz;
978    lsquic_time_t time_diff;
979    lsquic_packno_t packno_diff, gap, prev_low, maxno, rsize;
980    size_t sz;
981    const struct lsquic_packno_range *range;
982    unsigned a, b, c, addl_ack_blocks, ecn_needs;
983    unsigned bits[4];
984    enum ecn ecn;
985
986#define AVAIL() (end - p)
987
988#define CHECKOUT(sz) do {                                               \
989    if ((intptr_t) (sz) > AVAIL()) {                                    \
990        errno = ENOBUFS;                                                \
991        return -1;                                                      \
992    }                                                                   \
993} while (0)
994
995    range = rechist_first(rechist);
996    if (!range)
997    {
998        errno = EINVAL;
999        return -1;
1000    }
1001    // LSQ_DEBUG("range [%"PRIu64" - %"PRIu64"]", range->high, range->low);
1002
1003    time_diff = now - rechist_largest_recv(rechist);
1004    time_diff >>= TP_DEF_ACK_DELAY_EXP;
1005
1006    maxno = range->high;
1007    packno_diff = maxno - range->low;
1008
1009    a = vint_val2bits(maxno);
1010    b = vint_val2bits(time_diff);
1011    c = vint_val2bits(packno_diff);
1012    sz = 1          /* Type */
1013       + (1 << a)   /* Largest Acknowledged */
1014       + (1 << b)   /* ACK Delay */
1015       + 1          /* ACK Block Count */
1016       + (1 << c)   /* ACK Blocks */
1017       ;
1018
1019    CHECKOUT(sz);
1020
1021    if (ecn_counts)
1022    {
1023        for (ecn = 1; ecn <= 3; ++ecn)
1024            bits[ecn] = vint_val2bits(ecn_counts[ecn]);
1025        ecn_needs = (1 << bits[1]) + (1 << bits[2]) + (1 << bits[3]);
1026    }
1027    else
1028        ecn_needs = 0;
1029
1030    *p = 0x02 + !!ecn_counts;
1031    ++p;
1032
1033    vint_write(p, maxno, a, 1 << a);
1034    p += 1 << a;
1035    vint_write(p, time_diff, b, 1 << b);
1036    p += 1 << b;
1037    block_count_p = p;
1038    p += 1; /* Initial guess that we have fewer than 64 additional ACK Blocks */
1039    vint_write(p, packno_diff, c, 1 << c);
1040    p += 1 << c;
1041
1042    prev_low = range->low;
1043    addl_ack_blocks = 0;
1044    while ((range = rechist_next(rechist)))
1045    {
1046        // LSQ_DEBUG("range [%"PRIu64" - %"PRIu64"]", range->high, range->low);
1047        gap = prev_low - range->high - 1;
1048        rsize = range->high - range->low;
1049        a = vint_val2bits(gap - 1);
1050        b = vint_val2bits(rsize);
1051        if (ecn_needs + (1 << a) + (1 << b) > (unsigned)AVAIL())
1052            break;
1053        if (addl_ack_blocks == VINT_MAX_ONE_BYTE)
1054        {
1055            memmove(block_count_p + 2, block_count_p + 1,
1056                                                p - block_count_p - 1);
1057            ++p;
1058        }
1059        vint_write(p, gap - 1, a, 1 << a);
1060        p += 1 << a;
1061        vint_write(p, rsize, b, 1 << b);
1062        p += 1 << b;
1063        ++addl_ack_blocks;
1064        prev_low = range->low;
1065    }
1066
1067    /* Here we assume that addl_ack_blocks < (1 << 14), which is a safe
1068     * assumption to make.
1069     */
1070    vint_write(block_count_p, addl_ack_blocks,
1071                        addl_ack_blocks > VINT_MAX_ONE_BYTE,
1072                        1 + (addl_ack_blocks > VINT_MAX_ONE_BYTE));
1073
1074    if (ecn_counts)
1075    {
1076        assert(ecn_needs <= (unsigned)AVAIL());
1077        for (ecn = 1; ecn <= 3; ++ecn)
1078        {
1079            vint_write(p, ecn_counts[ecnmap[ecn]], bits[ecnmap[ecn]], 1 << bits[ecnmap[ecn]]);
1080            p += 1 << bits[ecnmap[ecn]];
1081        }
1082    }
1083
1084    *has_missing = addl_ack_blocks > 0;
1085    *largest_received = maxno;
1086    return p - (unsigned char *) outbuf;
1087
1088#undef CHECKOUT
1089#undef AVAIL
1090}
1091
1092
1093static size_t
1094ietf_v1_calc_stream_frame_header_sz (lsquic_stream_id_t stream_id,
1095                                            uint64_t offset, unsigned data_sz)
1096{
1097    if (offset)
1098        return 1
1099            + (1 << vint_val2bits(stream_id))
1100            + (1 << vint_val2bits(data_sz))
1101            + (1 << vint_val2bits(offset));
1102    else
1103        return 1
1104            + (1 << vint_val2bits(data_sz))
1105            + (1 << vint_val2bits(stream_id));
1106}
1107
1108
1109/* [draft-ietf-quic-transport-24] Section 19.6 */
1110static size_t
1111ietf_v1_calc_crypto_frame_header_sz (uint64_t offset, unsigned data_sz)
1112{
1113    return 1    /* Frame type */
1114         + (1 << vint_val2bits(offset))
1115         + (1 << vint_val2bits(data_sz))
1116         ;
1117}
1118
1119
1120static enum quic_frame_type
1121ietf_v1_parse_frame_type (const unsigned char *buf, size_t len)
1122{
1123    uint64_t val;
1124    int s;
1125
1126    if (len > 0 && buf[0] < 0x40)
1127        return lsquic_iquic_byte2type[buf[0]];
1128
1129    s = vint_read(buf, buf + len, &val);
1130    if (s > 0 && (unsigned) s == (1u << vint_val2bits(val)))
1131        switch (val)
1132        {
1133        case FRAME_TYPE_ACK_FREQUENCY:  return QUIC_FRAME_ACK_FREQUENCY;
1134        case FRAME_TYPE_TIMESTAMP:      return QUIC_FRAME_TIMESTAMP;
1135        default:    break;
1136        }
1137
1138    return QUIC_FRAME_INVALID;
1139}
1140
1141
1142static unsigned
1143ietf_v1_path_chal_frame_size (void)
1144{
1145    return 1 + sizeof(uint64_t);
1146}
1147
1148
1149static int
1150ietf_v1_gen_path_chal_frame (unsigned char *buf, size_t len, uint64_t chal)
1151{
1152    if (len >= 1 + sizeof(chal))
1153    {
1154        *buf = 0x1A;
1155        memcpy(buf + 1, &chal, sizeof(chal));
1156        return 1 + sizeof(chal);
1157    }
1158    else
1159        return -1;
1160}
1161
1162
1163static int
1164ietf_v1_parse_path_chal_frame (const unsigned char *buf, size_t len,
1165                                                            uint64_t *chal)
1166{
1167    if (len >= 9)
1168    {
1169        memcpy(chal, buf + 1, 8);
1170        return 9;
1171    }
1172    else
1173        return -1;
1174}
1175
1176
1177static unsigned
1178ietf_v1_path_resp_frame_size (void)
1179{
1180    return 1 + sizeof(uint64_t);
1181}
1182
1183
1184static int
1185ietf_v1_gen_path_resp_frame (unsigned char *buf, size_t len, uint64_t resp)
1186{
1187    if (len >= 1 + sizeof(resp))
1188    {
1189        *buf = 0x1B;
1190        memcpy(buf + 1, &resp, sizeof(resp));
1191        return 1 + sizeof(resp);
1192    }
1193    else
1194        return -1;
1195}
1196
1197
1198static int
1199ietf_v1_parse_path_resp_frame (const unsigned char *buf, size_t len,
1200                                                            uint64_t *resp)
1201{
1202    return ietf_v1_parse_path_chal_frame(buf, len, resp);
1203}
1204
1205
1206static void
1207ietf_v1_turn_on_fin (unsigned char *stream_frame_header)
1208{
1209    *stream_frame_header |= 1;
1210}
1211
1212
1213static unsigned
1214ietf_v1_packno_bits2len (enum packno_bits bits)
1215{
1216    return iquic_packno_bits2len(bits);
1217}
1218
1219
1220static enum packno_bits
1221ietf_v1_calc_packno_bits (lsquic_packno_t packno,
1222                    lsquic_packno_t least_unacked, uint64_t n_in_flight)
1223{
1224    uint64_t delta;
1225    unsigned bits;
1226
1227    delta = packno - least_unacked;
1228    if (n_in_flight > delta)
1229        delta = n_in_flight;
1230
1231    delta *= 4;
1232    bits = (delta >= (1ULL <<  8))
1233         + (delta >= (1ULL << 16))
1234         + (delta >= (1ULL << 24))
1235         ;
1236
1237    return bits;
1238}
1239
1240
1241static int
1242ietf_v1_parse_one_varint (const unsigned char *buf, size_t len, uint64_t *val)
1243{
1244    int s;
1245
1246    s = vint_read(buf + 1, buf + len, val);
1247    if (s >= 0)
1248        return 1 + s;
1249    else
1250        return s;
1251}
1252
1253
1254static int
1255ietf_v1_gen_one_varint (unsigned char *buf, size_t len,
1256                                        unsigned char type, uint64_t val)
1257{
1258    unsigned vbits;
1259    unsigned char *p;
1260
1261    vbits = vint_val2bits(val);
1262
1263    if (1u + (1u << vbits) > len)
1264        return -1;
1265
1266    p = buf;
1267    *p++ = type;
1268    vint_write(p, val, vbits, 1 << vbits);
1269    p += 1 << vbits;
1270
1271    return p - buf;
1272}
1273
1274
1275static int
1276ietf_v1_gen_blocked_frame (unsigned char *buf, size_t buf_len, uint64_t off)
1277{
1278    return ietf_v1_gen_one_varint(buf, buf_len, 0x14, off);
1279}
1280
1281
1282static int
1283ietf_v1_parse_blocked_frame (const unsigned char *buf, size_t sz, uint64_t *off)
1284{
1285    return ietf_v1_parse_one_varint(buf, sz, off);
1286}
1287
1288
1289static unsigned
1290ietf_v1_blocked_frame_size (uint64_t off)
1291{
1292    return 1 + vint_size(off);
1293}
1294
1295
1296static int
1297ietf_v1_parse_max_data (const unsigned char *buf, size_t len, uint64_t *val)
1298{
1299    return ietf_v1_parse_one_varint(buf, len, val);
1300}
1301
1302
1303static int
1304ietf_v1_gen_max_data_frame (unsigned char *buf, size_t len, uint64_t val)
1305{
1306    return ietf_v1_gen_one_varint(buf, len, 0x10, val);
1307}
1308
1309
1310static unsigned
1311ietf_v1_max_data_frame_size (uint64_t val)
1312{
1313    return 1 + vint_size(val);
1314}
1315
1316
1317static int
1318ietf_v1_parse_retire_cid_frame (const unsigned char *buf, size_t len,
1319                                                                uint64_t *val)
1320{
1321    return ietf_v1_parse_one_varint(buf, len, val);
1322}
1323
1324
1325static int
1326ietf_v1_gen_retire_cid_frame (unsigned char *buf, size_t len, uint64_t val)
1327{
1328    return ietf_v1_gen_one_varint(buf, len, 0x19, val);
1329}
1330
1331
1332static size_t
1333ietf_v1_retire_cid_frame_size (uint64_t val)
1334{
1335    return 1 + vint_size(val);
1336}
1337
1338
1339static int
1340ietf_v1_parse_new_conn_id (const unsigned char *buf, size_t len,
1341                        uint64_t *seqno, uint64_t *retire_prior_to,
1342                        lsquic_cid_t *cid, const unsigned char **reset_token)
1343{
1344    const unsigned char *p = buf + 1;
1345    const unsigned char *const end = buf + len;
1346    unsigned char cid_len;
1347    int s;
1348
1349    s = vint_read(p, end, seqno);
1350    if (s < 0)
1351        return s;
1352    p += s;
1353
1354    s = vint_read(p, end, retire_prior_to);
1355    if (s < 0)
1356        return s;
1357    p += s;
1358
1359    if (p >= end)
1360        return -1;
1361
1362    cid_len = *p++;
1363    if (cid_len == 0 || cid_len > MAX_CID_LEN)
1364        return -2;
1365
1366    if ((unsigned) (end - p) < cid_len + IQUIC_SRESET_TOKEN_SZ)
1367        return -1;
1368    cid->len = cid_len;
1369    memcpy(cid->idbuf, p, cid_len);
1370    p += cid_len;
1371    if (reset_token)
1372        *reset_token = p;
1373    p += IQUIC_SRESET_TOKEN_SZ;
1374
1375    return p - buf;
1376}
1377
1378
1379/* Size of a frame that contains two varints */
1380static unsigned
1381ietf_v1_two_varints_size (uint64_t vals[2])
1382{
1383    unsigned vbits[2];
1384
1385    vbits[0] = vint_val2bits(vals[0]);
1386    vbits[1] = vint_val2bits(vals[1]);
1387    return 1u + (1u << vbits[0]) + (1u << vbits[1]);
1388}
1389
1390
1391static int
1392ietf_v1_gen_two_varints (unsigned char *buf, size_t len,
1393                                    unsigned char type, uint64_t vals[2])
1394{
1395    unsigned vbits[2];
1396    unsigned char *p;
1397
1398    vbits[0] = vint_val2bits(vals[0]);
1399    vbits[1] = vint_val2bits(vals[1]);
1400
1401    if (1u + (1u << vbits[0]) + (1u << vbits[1]) > len)
1402        return -1;
1403
1404    p = buf;
1405    *p++ = type;
1406    vint_write(p, vals[0], vbits[0], 1 << vbits[0]);
1407    p += 1 << vbits[0];
1408    vint_write(p, vals[1], vbits[1], 1 << vbits[1]);
1409    p += 1 << vbits[1];
1410
1411    return p - buf;
1412}
1413
1414
1415static int
1416ietf_v1_parse_two_varints (const unsigned char *buf, size_t len, uint64_t *vals[2])
1417{
1418    const unsigned char *p = buf;
1419    const unsigned char *const end = p + len;
1420    int s;
1421
1422    if (len < 2)
1423        return -1;
1424
1425    ++p;    /* Type */
1426
1427    s = vint_read(p, end, vals[0]);
1428    if (s < 0)
1429        return s;
1430    p += s;
1431
1432    s = vint_read(p, end, vals[1]);
1433    if (s < 0)
1434        return s;
1435    p += s;
1436
1437    return p - buf;
1438}
1439
1440
1441/* vals[0] is the frame type */
1442static unsigned
1443ietf_v1_frame_with_varints_size (unsigned n, uint64_t vals[])
1444{
1445    unsigned vbits, size;
1446
1447    assert(n > 0);
1448    vbits = vint_val2bits(vals[0]);
1449    size = 1 << vbits;
1450    while (--n)
1451    {
1452        vbits = vint_val2bits(vals[n]);
1453        size += 1 << vbits;
1454    }
1455
1456    return size;
1457}
1458
1459
1460/* vals[0] is the frame type */
1461static int
1462ietf_v1_gen_frame_with_varints (unsigned char *buf, size_t len,
1463                                    unsigned count, uint64_t vals[])
1464{
1465    unsigned vbits, n;
1466    unsigned char *p;
1467
1468    if (ietf_v1_frame_with_varints_size(count, vals) > len)
1469        return -1;
1470
1471    p = buf;
1472    for (n = 0; n < count; ++n)
1473    {
1474        vbits = vint_val2bits(vals[n]);
1475        vint_write(p, vals[n], vbits, 1 << vbits);
1476        p += 1 << vbits;
1477    }
1478
1479    return p - buf;
1480}
1481
1482
1483/* Frame type is checked when frame type is parsed.  The only use here is
1484 * to calculate skip length.
1485 */
1486static int
1487ietf_v1_parse_frame_with_varints (const unsigned char *buf, size_t len,
1488            const uint64_t frame_type, unsigned count, uint64_t *vals[])
1489{
1490    const unsigned char *p = buf;
1491    const unsigned char *const end = p + len;
1492    unsigned vbits, n;
1493    int s;
1494
1495    vbits = vint_val2bits(frame_type);
1496    p += 1 << vbits;
1497
1498    for (n = 0; n < count; ++n)
1499    {
1500        s = vint_read(p, end, vals[n]);
1501        if (s < 0)
1502            return s;
1503        p += s;
1504    }
1505
1506    return p - buf;
1507}
1508
1509
1510
1511static int
1512ietf_v1_parse_stream_blocked_frame (const unsigned char *buf, size_t len,
1513                            lsquic_stream_id_t *stream_id, uint64_t *offset)
1514{
1515    return ietf_v1_parse_two_varints(buf, len,
1516                                       (uint64_t *[]) { stream_id, offset, });
1517}
1518
1519
1520static unsigned
1521ietf_v1_stream_blocked_frame_size (lsquic_stream_id_t stream_id, uint64_t off)
1522{
1523    return ietf_v1_two_varints_size((uint64_t []) { stream_id, off, });
1524}
1525
1526
1527static int
1528ietf_v1_gen_streams_blocked_frame (unsigned char *buf, size_t len,
1529                                    enum stream_dir sd, uint64_t limit)
1530{
1531    return ietf_v1_gen_one_varint(buf, len, 0x16 + (sd == SD_UNI), limit);
1532}
1533
1534
1535static int
1536ietf_v1_parse_streams_blocked_frame (const unsigned char *buf, size_t len,
1537                                    enum stream_dir *sd, uint64_t *limit)
1538{
1539    int s;
1540
1541    s = ietf_v1_parse_one_varint(buf, len, limit);
1542    if (s > 0)
1543    {
1544        if (buf[0] == 0x16)
1545            *sd = SD_BIDI;
1546        else
1547            *sd = SD_UNI;
1548    }
1549    return s;
1550}
1551
1552
1553static unsigned
1554ietf_v1_streams_blocked_frame_size (uint64_t limit)
1555{
1556    return 1 + vint_size(limit);
1557}
1558
1559
1560static int
1561ietf_v1_gen_stream_blocked_frame (unsigned char *buf, size_t len,
1562                                    lsquic_stream_id_t stream_id, uint64_t off)
1563{
1564    return ietf_v1_gen_two_varints(buf, len, 0x15, (uint64_t[]){ stream_id, off, });
1565}
1566
1567
1568static int
1569ietf_v1_gen_max_stream_data_frame (unsigned char *buf, size_t len,
1570                                    lsquic_stream_id_t stream_id, uint64_t off)
1571{
1572    return ietf_v1_gen_two_varints(buf, len, 0x11, (uint64_t[]){ stream_id, off, });
1573}
1574
1575
1576static unsigned
1577ietf_v1_max_stream_data_frame_size (lsquic_stream_id_t stream_id, uint64_t off)
1578{
1579    return ietf_v1_two_varints_size((uint64_t []) { stream_id, off, });
1580}
1581
1582
1583
1584static int
1585ietf_v1_parse_max_stream_data_frame (const unsigned char *buf, size_t len,
1586                                lsquic_stream_id_t *stream_id, uint64_t *off)
1587{
1588    return ietf_v1_parse_two_varints(buf, len, (uint64_t *[]){ stream_id, off, });
1589}
1590
1591
1592static int
1593ietf_v1_parse_max_streams_frame (const unsigned char *buf, size_t len,
1594                                    enum stream_dir *sd, uint64_t *max_streams)
1595{
1596    int s;
1597
1598    s = ietf_v1_parse_one_varint(buf, len, max_streams);
1599    if (s > 0)
1600        *sd = buf[0] == 0x12 ? SD_BIDI : SD_UNI;
1601    return s;
1602}
1603
1604
1605static int
1606ietf_v1_gen_max_streams_frame (unsigned char *buf, size_t len,
1607                                    enum stream_dir sd, uint64_t limit)
1608{
1609    return ietf_v1_gen_one_varint(buf, len, 0x12 + (sd == SD_UNI), limit);
1610}
1611
1612
1613static unsigned
1614ietf_v1_max_streams_frame_size (uint64_t limit)
1615{
1616    return 1 + vint_size(limit);
1617}
1618
1619
1620static size_t
1621ietf_v1_new_token_frame_size (size_t token_sz)
1622{
1623    unsigned bits;
1624
1625    bits = vint_val2bits(token_sz);
1626    return 1 + (1 << bits) + token_sz;
1627}
1628
1629
1630static int
1631ietf_v1_gen_new_token_frame (unsigned char *buf, size_t buf_sz,
1632                                const unsigned char *token, size_t token_sz)
1633{
1634    unsigned char *p;
1635    unsigned bits;
1636
1637    bits = vint_val2bits(token_sz);
1638    if (buf_sz < 1 + (1 << bits) + token_sz)
1639    {
1640        errno = ENOBUFS;
1641        return -1;
1642    }
1643
1644    p = buf;
1645    *p++ = 0x07;
1646    vint_write(p, token_sz, bits, 1 << bits);
1647    p += 1 << bits;
1648    memcpy(p, token, token_sz);
1649    p += token_sz;
1650
1651    return p - buf;
1652}
1653
1654
1655static size_t
1656ietf_v1_new_connection_id_frame_size (unsigned seqno, unsigned scid_len)
1657{
1658    unsigned bits;
1659
1660    bits = vint_val2bits(seqno);
1661    return 1                /* Frame Type */
1662         + (1 << bits)      /* Sequence Number */
1663         + 1                /* Retire Prior To (we always set it to zero */
1664         + 1                /* CID length */
1665         + scid_len
1666         + IQUIC_SRESET_TOKEN_SZ;
1667}
1668
1669
1670static int
1671ietf_v1_gen_new_connection_id_frame (unsigned char *buf, size_t buf_sz,
1672            unsigned seqno, const struct lsquic_cid *cid,
1673            const unsigned char *token, size_t token_sz)
1674{
1675    unsigned char *p;
1676    unsigned bits;
1677
1678    if (buf_sz < ietf_v1_new_connection_id_frame_size(seqno, cid->len))
1679        return -1;
1680
1681    p = buf;
1682    *p++ = 0x18;
1683    bits = vint_val2bits(seqno);
1684    vint_write(p, seqno, bits, 1 << bits);
1685    p += 1 << bits;
1686    *p++ = 0;   /* Retire Prior To */
1687    *p++ = cid->len;
1688    memcpy(p, cid->idbuf, cid->len);
1689    p += cid->len;
1690    memcpy(p, token, token_sz);
1691    p += token_sz;
1692
1693    return p - buf;
1694}
1695
1696
1697/* [draft-ietf-quic-transport-17] Section-17.2 */
1698static const enum header_type bits2ht[4] =
1699{
1700    [0] = HETY_INITIAL,
1701    [1] = HETY_0RTT,
1702    [2] = HETY_HANDSHAKE,
1703    [3] = HETY_RETRY,
1704};
1705
1706
1707int
1708lsquic_ietf_v1_parse_packet_in_long_begin (struct lsquic_packet_in *packet_in,
1709                size_t length, int is_server, unsigned cid_len,
1710                struct packin_parse_state *state)
1711{
1712    const unsigned char *p = packet_in->pi_data;
1713    const unsigned char *const end = p + length;
1714    lsquic_ver_tag_t tag;
1715    enum header_type header_type;
1716    unsigned dcil, scil;
1717    int verneg, r;
1718    unsigned char first_byte;
1719    uint64_t payload_len, token_len;
1720
1721    if (length < 6)
1722        return -1;
1723    first_byte = *p++;
1724
1725    memcpy(&tag, p, 4);
1726    p += 4;
1727    verneg = 0 == tag;
1728    if (!verneg)
1729        header_type = bits2ht[ (first_byte >> 4) & 3 ];
1730    else
1731        header_type = HETY_VERNEG;
1732
1733    packet_in->pi_header_type = header_type;
1734
1735    dcil = *p++;
1736    if (p + dcil >= end || dcil > MAX_CID_LEN)
1737        return -1;
1738    if (dcil)
1739    {
1740        memcpy(packet_in->pi_dcid.idbuf, p, dcil);
1741        packet_in->pi_flags |= PI_CONN_ID;
1742        p += dcil;
1743    }
1744    packet_in->pi_dcid.len = dcil;
1745
1746    scil = *p++;
1747    if (p + scil > end || scil > MAX_CID_LEN)
1748        return -1;
1749    if (scil)
1750    {
1751        packet_in->pi_scid_off = p - packet_in->pi_data;
1752        p += scil;
1753    }
1754    packet_in->pi_scid_len = scil;
1755
1756    switch (header_type)
1757    {
1758    case HETY_INITIAL:
1759        if (is_server && dcil < MIN_INITIAL_DCID_LEN)
1760            return -1;
1761        r = vint_read(p, end, &token_len);
1762        if (r < 0)
1763            return -1;
1764        if (token_len && !is_server)
1765        {
1766            /* From [draft-ietf-quic-transport-14]:
1767             *
1768             *  Token Length:  A variable-length integer specifying the
1769             *  length of the Token field, in bytes.  This value is zero
1770             *  if no token is present.  Initial packets sent by the
1771             *  server MUST set the Token Length field to zero; clients
1772             *  that receive an Initial packet with a non-zero Token
1773             *  Length field MUST either discard the packet or generate
1774             *  a connection error of type PROTOCOL_VIOLATION.
1775             */
1776            return -1;
1777        }
1778        p += r;
1779        if (token_len)
1780        {
1781            if (token_len >=
1782                        1ull << (sizeof(packet_in->pi_token_size) * 8))
1783                return -1;
1784            if (p + token_len > end)
1785                return -1;
1786            packet_in->pi_token = p - packet_in->pi_data;
1787            packet_in->pi_token_size = token_len;
1788            p += token_len;
1789        }
1790        /* fall-through */
1791    case HETY_HANDSHAKE:
1792    case HETY_0RTT:
1793        if (p >= end)
1794            return -1;
1795        r = vint_read(p, end, &payload_len);
1796        if (r < 0)
1797            return -1;
1798        p += r;
1799        if (p - packet_in->pi_data + payload_len > length)
1800            return -1;
1801        length = p - packet_in->pi_data + payload_len;
1802        if (end - p < 4)
1803            return -1;
1804        state->pps_p      = p - r;
1805        state->pps_nbytes = r;
1806        packet_in->pi_quic_ver = 1;
1807        break;
1808    case HETY_RETRY:
1809        if (p >= end)
1810            return -1;
1811        if (p
1812            /* [draft-ietf-quic-transport-25] Section 17.2.5 says that "a
1813             * client MUST discard a Retry packet with a zero-length Retry
1814             * Token field."  We might as well do it here.
1815             */
1816            + 1
1817            /* Integrity tag length: */
1818            + 16 > end)
1819                return -1;
1820        packet_in->pi_token = p - packet_in->pi_data;
1821        packet_in->pi_token_size = end - p - 16;
1822        /* Tag validation happens later */
1823        p = end;
1824        length = end - packet_in->pi_data;
1825        state->pps_p      = NULL;
1826        state->pps_nbytes = 0;
1827        packet_in->pi_quic_ver = 1;
1828        break;
1829    default:
1830        assert(header_type == HETY_VERNEG);
1831        if (p >= end || (3 & (uintptr_t) (end - p)))
1832            return -1;
1833        packet_in->pi_quic_ver = p - packet_in->pi_data;
1834        p = end;
1835        state->pps_p      = NULL;
1836        state->pps_nbytes = 0;
1837        break;
1838    }
1839
1840    packet_in->pi_header_sz     = p - packet_in->pi_data;
1841    packet_in->pi_data_sz       = length;
1842    packet_in->pi_nonce         = 0;
1843    packet_in->pi_refcnt        = 0;
1844    packet_in->pi_frame_types   = 0;
1845    memset(&packet_in->pi_next, 0, sizeof(packet_in->pi_next));
1846    packet_in->pi_refcnt        = 0;
1847    packet_in->pi_received      = 0;
1848
1849    /* Packet number is set to an invalid value.  The packet number must
1850     * be decrypted, which happens later.
1851     */
1852    packet_in->pi_packno        = 1ULL << 62;
1853
1854    return 0;
1855}
1856
1857
1858/* Is this a valid Initial packet?  We take the perspective of the server. */
1859int
1860lsquic_is_valid_ietf_v1_or_Q046plus_hs_packet (const unsigned char *buf,
1861                                        size_t length, lsquic_ver_tag_t *tagp)
1862{
1863    const unsigned char *p = buf;
1864    const unsigned char *const end = p + length;
1865    lsquic_ver_tag_t tag;
1866    enum header_type header_type;
1867    unsigned dcil, scil;
1868    int r;
1869    unsigned char first_byte;
1870    uint64_t payload_len, token_len, packet_len;
1871
1872    if (length < 6)
1873        return 0;
1874    first_byte = *p++;
1875
1876    header_type = bits2ht[ (first_byte >> 4) & 3 ];
1877    if (header_type != HETY_INITIAL)
1878        return 0;
1879
1880    memcpy(&tag, p, 4);
1881    p += 4;
1882    switch (tag)
1883    {
1884    case 0:
1885        return 0;       /* Client never sends version negotiation packets */
1886    case TAG('Q', '0', '4', '6'):
1887        dcil = p[0] >> 4;
1888        if (dcil)
1889            dcil += 3;
1890        scil = p[0] & 0xF;
1891        if (scil)
1892            scil += 3;
1893        ++p;
1894
1895        if (!(dcil == GQUIC_CID_LEN && scil == 0))
1896            return 0;
1897
1898        packet_len = first_byte & 3;
1899
1900        if (end - p < (ptrdiff_t) (dcil + scil + packet_len))
1901            return 0;
1902        break;
1903    case TAG('Q', '0', '5', '0'):
1904        dcil = *p++;
1905        if (dcil != 8)
1906            return 0;
1907        if (p + dcil + 1 >= end)
1908            return 0;
1909        p += dcil;
1910        scil = *p++;
1911        if (scil != 0)
1912            return 0;
1913        goto read_token;
1914    default:
1915        dcil = *p++;
1916        if (dcil < MIN_INITIAL_DCID_LEN || dcil > MAX_CID_LEN)
1917            return 0;
1918        if (p + dcil >= end)
1919            return 0;
1920        p += dcil;
1921        scil = *p++;
1922        if (p + scil > end || scil > MAX_CID_LEN)
1923            return 0;
1924        p += scil;
1925  read_token:
1926        r = vint_read(p, end, &token_len);
1927        if (r < 0)
1928            return 0;
1929        p += r;
1930        p += token_len;
1931        if (p >= end)
1932            return 0;
1933        r = vint_read(p, end, &payload_len);
1934        if (r < 0)
1935            return 0;
1936        p += r;
1937        if (p - buf + payload_len > length)
1938            return 0;
1939        if (end - p < 4)
1940            return 0;
1941    }
1942
1943    *tagp = tag;
1944    return 1;
1945}
1946
1947
1948int
1949lsquic_ietf_v1_parse_packet_in_short_begin (struct lsquic_packet_in *packet_in,
1950                size_t length, int is_server, unsigned cid_len,
1951                struct packin_parse_state *state)
1952{
1953    unsigned char byte;
1954    unsigned header_sz;
1955
1956    /* By the time this function has been called, we know length is non-zero */
1957    byte = packet_in->pi_data[0];
1958
1959    /* [draft-ietf-quic-transport-17] Section 17.3 */
1960    /* 01SRRKPP */
1961
1962    if (cid_len)
1963    {
1964        header_sz = 1 + cid_len;
1965        if (length < header_sz)
1966            return -1;
1967        memcpy(packet_in->pi_dcid.idbuf, packet_in->pi_data + 1, cid_len);
1968        packet_in->pi_dcid.len = cid_len;
1969        packet_in->pi_flags |= PI_CONN_ID;
1970    }
1971    else
1972        header_sz = 1;
1973
1974    packet_in->pi_flags |= ((byte & 0x20) > 0) << PIBIT_SPIN_SHIFT;
1975    packet_in->pi_flags |= (byte & 3) << PIBIT_BITS_SHIFT;
1976
1977    packet_in->pi_header_sz     = header_sz;
1978    packet_in->pi_data_sz       = length;
1979    packet_in->pi_quic_ver      = 0;
1980    packet_in->pi_nonce         = 0;
1981    packet_in->pi_refcnt        = 0;
1982    packet_in->pi_frame_types   = 0;
1983    memset(&packet_in->pi_next, 0, sizeof(packet_in->pi_next));
1984    packet_in->pi_refcnt        = 0;
1985    packet_in->pi_received      = 0;
1986
1987    /* This is so that Q046 works, ID-18 code does not use it */
1988    state->pps_p                = packet_in->pi_data + header_sz;
1989    state->pps_nbytes           = 1 + (byte & 3);
1990
1991    return 0;
1992}
1993
1994
1995#if __GNUC__
1996#   define popcount __builtin_popcount
1997#else
1998static int
1999popcount (unsigned v)
2000{
2001    int count, i;
2002    for (i = 0, count = 0; i < sizeof(v) * 8; ++i)
2003        if (v & (1 << i))
2004            ++count;
2005    return count;
2006}
2007#endif
2008
2009
2010int
2011lsquic_ietf_v1_gen_ver_nego_pkt (unsigned char *buf, size_t bufsz,
2012         const lsquic_cid_t *scid, const lsquic_cid_t *dcid, unsigned versions,
2013         uint8_t rand)
2014{
2015    size_t need;
2016    int r;
2017
2018    need = 1 /* Type */ + 4 /* Version */ + 1 /* DCIL */
2019                + dcid->len + 1 /* SCIL */ + scid->len + popcount(versions) * 4;
2020
2021    if (need > bufsz)
2022        return -1;
2023
2024    *buf++ = 0x80 | QUIC_BIT | rand;
2025    memset(buf, 0, 4);
2026    buf += 4;
2027
2028    /* From [draft-ietf-quic-transport-22], Section 17.2.1:
2029     *
2030     *  The server MUST include the value from the Source Connection ID field
2031     *  of the packet it receives in the Destination Connection ID field.
2032     *  The value for Source Connection ID MUST be copied from the
2033     *  Destination Connection ID of the received packet, which is initially
2034     *  randomly selected by a client.  Echoing both connection IDs gives
2035     *  clients some assurance that the server received the packet and that
2036     *  the Version Negotiation packet was not generated by an off-path
2037     *  attacker.
2038     */
2039
2040    *buf++ = dcid->len;
2041    memcpy(buf, dcid->idbuf, dcid->len);
2042    buf += dcid->len;
2043    *buf++ = scid->len;
2044    memcpy(buf, scid->idbuf, scid->len);
2045    buf += scid->len;
2046
2047    r = lsquic_gen_ver_tags(buf, bufsz - 1 - 4 - 2 - dcid->len - scid->len,
2048                                                                    versions);
2049    if (r < 0)
2050        return -1;
2051    assert((unsigned) r == popcount(versions) * 4u);
2052
2053    return need;
2054}
2055
2056
2057static int
2058ietf_v1_gen_handshake_done_frame (unsigned char *buf, size_t buf_len)
2059{
2060    if (buf_len > 0)
2061    {
2062        *buf = 0x1E;
2063        return 1;
2064    }
2065    else
2066        return -1;
2067}
2068
2069
2070static int
2071ietf_v1_parse_handshake_done_frame (const unsigned char *buf, size_t buf_len)
2072{
2073    assert(buf[0] == 0x1E);
2074    assert(buf_len > 0);
2075    return 1;
2076}
2077
2078
2079static int
2080ietf_v1_gen_ack_frequency_frame (unsigned char *buf, size_t buf_len,
2081    uint64_t seqno, uint64_t pack_tol, uint64_t upd_mad, int ignore)
2082{
2083    int sz;
2084
2085    sz = ietf_v1_gen_frame_with_varints(buf, buf_len, 4,
2086        (uint64_t[]){ FRAME_TYPE_ACK_FREQUENCY, seqno, pack_tol, upd_mad });
2087    if (sz > 0 && (size_t) sz < buf_len)
2088    {
2089        buf[sz++] = !!ignore;
2090        return sz;
2091    }
2092    else
2093        return -1;
2094}
2095
2096
2097static int
2098ietf_v1_parse_ack_frequency_frame (const unsigned char *buf, size_t buf_len,
2099    uint64_t *seqno, uint64_t *pack_tol, uint64_t *upd_mad, int *ignore)
2100{
2101    int sz;
2102
2103    sz = ietf_v1_parse_frame_with_varints(buf, buf_len,
2104                FRAME_TYPE_ACK_FREQUENCY,
2105                3, (uint64_t *[]) { seqno, pack_tol, upd_mad });
2106    if (sz > 0 && (size_t) sz < buf_len && buf[sz] < 2)
2107    {
2108        *ignore = buf[sz++];
2109        return sz;
2110    }
2111    else
2112        return -1;
2113}
2114
2115
2116static unsigned
2117ietf_v1_ack_frequency_frame_size (uint64_t seqno, uint64_t pack_tol,
2118    uint64_t upd_mad)
2119{
2120    return 1 + ietf_v1_frame_with_varints_size(4,
2121            (uint64_t[]){ FRAME_TYPE_ACK_FREQUENCY, seqno, pack_tol, upd_mad });
2122}
2123
2124
2125static unsigned
2126ietf_v1_handshake_done_frame_size (void)
2127{
2128    return 1;
2129}
2130
2131
2132static int
2133ietf_v1_gen_timestamp_frame (unsigned char *buf, size_t buf_len,
2134                                                            uint64_t timestamp)
2135{
2136    return ietf_v1_gen_frame_with_varints(buf, buf_len, 2,
2137                            (uint64_t[]){ FRAME_TYPE_TIMESTAMP, timestamp });
2138}
2139
2140
2141static int
2142ietf_v1_parse_timestamp_frame (const unsigned char *buf, size_t buf_len,
2143                                                            uint64_t *timestamp)
2144{
2145    return ietf_v1_parse_frame_with_varints(buf, buf_len,
2146                FRAME_TYPE_TIMESTAMP, 1, (uint64_t *[]) { timestamp });
2147}
2148
2149
2150const struct parse_funcs lsquic_parse_funcs_ietf_v1 =
2151{
2152    .pf_gen_reg_pkt_header            =  ietf_v1_gen_reg_pkt_header,
2153    .pf_parse_packet_in_finish        =  ietf_v1_parse_packet_in_finish,
2154    .pf_gen_stream_frame              =  ietf_v1_gen_stream_frame,
2155    .pf_calc_stream_frame_header_sz   =  ietf_v1_calc_stream_frame_header_sz,
2156    .pf_parse_stream_frame            =  ietf_v1_parse_stream_frame,
2157    .pf_dec_stream_frame_size         =  ietf_v1_dec_stream_frame_size,
2158    .pf_parse_ack_frame               =  ietf_v1_parse_ack_frame,
2159    .pf_gen_ack_frame                 =  ietf_v1_gen_ack_frame,
2160    .pf_gen_blocked_frame             =  ietf_v1_gen_blocked_frame,
2161    .pf_parse_blocked_frame           =  ietf_v1_parse_blocked_frame,
2162    .pf_blocked_frame_size            =  ietf_v1_blocked_frame_size,
2163    .pf_rst_frame_size                =  ietf_v1_rst_frame_size,
2164    .pf_gen_rst_frame                 =  ietf_v1_gen_rst_frame,
2165    .pf_parse_rst_frame               =  ietf_v1_parse_rst_frame,
2166    .pf_connect_close_frame_size      =  ietf_v1_connect_close_frame_size,
2167    .pf_gen_connect_close_frame       =  ietf_v1_gen_connect_close_frame,
2168    .pf_parse_connect_close_frame     =  ietf_v1_parse_connect_close_frame,
2169    .pf_gen_ping_frame                =  ietf_v1_gen_ping_frame,
2170    .pf_parse_frame_type              =  ietf_v1_parse_frame_type,
2171    .pf_turn_on_fin                   =  ietf_v1_turn_on_fin,
2172    .pf_packout_size                  =  ietf_v1_packout_size,
2173    .pf_packout_max_header_size       =  ietf_v1_packout_max_header_size,
2174    .pf_path_chal_frame_size          =  ietf_v1_path_chal_frame_size,
2175    .pf_parse_path_chal_frame         =  ietf_v1_parse_path_chal_frame,
2176    .pf_gen_path_chal_frame           =  ietf_v1_gen_path_chal_frame,
2177    .pf_path_resp_frame_size          =  ietf_v1_path_resp_frame_size,
2178    .pf_gen_path_resp_frame           =  ietf_v1_gen_path_resp_frame,
2179    .pf_parse_path_resp_frame         =  ietf_v1_parse_path_resp_frame,
2180    .pf_calc_packno_bits              =  ietf_v1_calc_packno_bits,
2181    .pf_packno_bits2len               =  ietf_v1_packno_bits2len,
2182    .pf_gen_crypto_frame              =  ietf_v1_gen_crypto_frame,
2183    .pf_parse_crypto_frame            =  ietf_v1_parse_crypto_frame,
2184    .pf_calc_crypto_frame_header_sz   =  ietf_v1_calc_crypto_frame_header_sz,
2185    .pf_parse_max_data                =  ietf_v1_parse_max_data,
2186    .pf_gen_max_data_frame            =  ietf_v1_gen_max_data_frame,
2187    .pf_max_data_frame_size           =  ietf_v1_max_data_frame_size,
2188    .pf_parse_new_conn_id             =  ietf_v1_parse_new_conn_id,
2189    .pf_gen_stream_blocked_frame      =  ietf_v1_gen_stream_blocked_frame,
2190    .pf_parse_stream_blocked_frame    =  ietf_v1_parse_stream_blocked_frame,
2191    .pf_stream_blocked_frame_size     =  ietf_v1_stream_blocked_frame_size,
2192    .pf_gen_max_stream_data_frame     =  ietf_v1_gen_max_stream_data_frame,
2193    .pf_parse_max_stream_data_frame   =  ietf_v1_parse_max_stream_data_frame,
2194    .pf_max_stream_data_frame_size    =  ietf_v1_max_stream_data_frame_size,
2195    .pf_parse_stop_sending_frame      =  ietf_v1_parse_stop_sending_frame,
2196    .pf_gen_stop_sending_frame        =  ietf_v1_gen_stop_sending_frame,
2197    .pf_stop_sending_frame_size       =  ietf_v1_stop_sending_frame_size,
2198    .pf_parse_new_token_frame         =  ietf_v1_parse_new_token_frame,
2199    .pf_new_connection_id_frame_size  =  ietf_v1_new_connection_id_frame_size,
2200    .pf_gen_new_connection_id_frame   =  ietf_v1_gen_new_connection_id_frame,
2201    .pf_new_token_frame_size          =  ietf_v1_new_token_frame_size,
2202    .pf_gen_new_token_frame           =  ietf_v1_gen_new_token_frame,
2203    .pf_parse_retire_cid_frame        =  ietf_v1_parse_retire_cid_frame,
2204    .pf_gen_retire_cid_frame          =  ietf_v1_gen_retire_cid_frame,
2205    .pf_retire_cid_frame_size         =  ietf_v1_retire_cid_frame_size,
2206    .pf_gen_streams_blocked_frame     =  ietf_v1_gen_streams_blocked_frame,
2207    .pf_parse_streams_blocked_frame   =  ietf_v1_parse_streams_blocked_frame,
2208    .pf_streams_blocked_frame_size    =  ietf_v1_streams_blocked_frame_size,
2209    .pf_gen_max_streams_frame         =  ietf_v1_gen_max_streams_frame,
2210    .pf_parse_max_streams_frame       =  ietf_v1_parse_max_streams_frame,
2211    .pf_max_streams_frame_size        =  ietf_v1_max_streams_frame_size,
2212    .pf_gen_handshake_done_frame      =  ietf_v1_gen_handshake_done_frame,
2213    .pf_parse_handshake_done_frame    =  ietf_v1_parse_handshake_done_frame,
2214    .pf_handshake_done_frame_size     =  ietf_v1_handshake_done_frame_size,
2215    .pf_gen_ack_frequency_frame       =  ietf_v1_gen_ack_frequency_frame,
2216    .pf_parse_ack_frequency_frame     =  ietf_v1_parse_ack_frequency_frame,
2217    .pf_ack_frequency_frame_size      =  ietf_v1_ack_frequency_frame_size,
2218    .pf_gen_timestamp_frame           =  ietf_v1_gen_timestamp_frame,
2219    .pf_parse_timestamp_frame         =  ietf_v1_parse_timestamp_frame,
2220};
2221