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