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