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