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