lsquic_packet_common.h revision 55cd0b38
1/* Copyright (c) 2017 - 2019 LiteSpeed Technologies Inc.  See LICENSE. */
2#ifndef LSQUIC_PACKET_COMMON_H
3#define LSQUIC_PACKET_COMMON_H 1
4
5#include <stdint.h>
6
7#include "lsquic_int_types.h"
8
9enum PACKET_PUBLIC_FLAGS
10{
11  PACKET_PUBLIC_FLAGS_VERSION = 1,
12  PACKET_PUBLIC_FLAGS_RST = 2,
13  PACKET_PUBLIC_FLAGS_NONCE = 4,
14  PACKET_PUBLIC_FLAGS_8BYTE_CONNECTION_ID = 8,
15  PACKET_PUBLIC_FLAGS_MULTIPATH = 1 << 6,
16  PACKET_PUBLIC_FLAGS_TWO_OR_MORE_BYTES = 1 << 7,
17};
18
19enum quic_frame_type
20{
21    QUIC_FRAME_INVALID,
22
23    /*Special*/
24    QUIC_FRAME_STREAM,
25    QUIC_FRAME_ACK,
26
27    /*Regular*/
28    QUIC_FRAME_PADDING,
29    QUIC_FRAME_RST_STREAM,
30    QUIC_FRAME_CONNECTION_CLOSE,
31    QUIC_FRAME_GOAWAY,
32    QUIC_FRAME_WINDOW_UPDATE,
33    QUIC_FRAME_BLOCKED,
34    QUIC_FRAME_STOP_WAITING,
35    QUIC_FRAME_PING,
36    N_QUIC_FRAMES
37};
38
39enum quic_ft_bit {
40    QUIC_FTBIT_INVALID          = 1 << QUIC_FRAME_INVALID,
41    QUIC_FTBIT_STREAM           = 1 << QUIC_FRAME_STREAM,
42    QUIC_FTBIT_ACK              = 1 << QUIC_FRAME_ACK,
43    QUIC_FTBIT_PADDING          = 1 << QUIC_FRAME_PADDING,
44    QUIC_FTBIT_RST_STREAM       = 1 << QUIC_FRAME_RST_STREAM,
45    QUIC_FTBIT_CONNECTION_CLOSE = 1 << QUIC_FRAME_CONNECTION_CLOSE,
46    QUIC_FTBIT_GOAWAY           = 1 << QUIC_FRAME_GOAWAY,
47    QUIC_FTBIT_WINDOW_UPDATE    = 1 << QUIC_FRAME_WINDOW_UPDATE,
48    QUIC_FTBIT_BLOCKED          = 1 << QUIC_FRAME_BLOCKED,
49    QUIC_FTBIT_STOP_WAITING     = 1 << QUIC_FRAME_STOP_WAITING,
50    QUIC_FTBIT_PING             = 1 << QUIC_FRAME_PING,
51};
52
53static const char * const frame_type_2_str[N_QUIC_FRAMES] = {
54    [QUIC_FRAME_INVALID]           =  "QUIC_FRAME_INVALID",
55    [QUIC_FRAME_STREAM]            =  "QUIC_FRAME_STREAM",
56    [QUIC_FRAME_ACK]               =  "QUIC_FRAME_ACK",
57    [QUIC_FRAME_PADDING]           =  "QUIC_FRAME_PADDING",
58    [QUIC_FRAME_RST_STREAM]        =  "QUIC_FRAME_RST_STREAM",
59    [QUIC_FRAME_CONNECTION_CLOSE]  =  "QUIC_FRAME_CONNECTION_CLOSE",
60    [QUIC_FRAME_GOAWAY]            =  "QUIC_FRAME_GOAWAY",
61    [QUIC_FRAME_WINDOW_UPDATE]     =  "QUIC_FRAME_WINDOW_UPDATE",
62    [QUIC_FRAME_BLOCKED]           =  "QUIC_FRAME_BLOCKED",
63    [QUIC_FRAME_STOP_WAITING]      =  "QUIC_FRAME_STOP_WAITING",
64    [QUIC_FRAME_PING]              =  "QUIC_FRAME_PING",
65};
66
67#define QUIC_FRAME_PRELEN   (sizeof("QUIC_FRAME_"))
68#define QUIC_FRAME_SLEN(x)  (sizeof(#x) - QUIC_FRAME_PRELEN)
69#define QUIC_FRAME_NAME(i)  (frame_type_2_str[i] + QUIC_FRAME_PRELEN - 1)
70
71
72    /* We don't need to include INVALID frame in this list because it is
73     * never a part of any frame list bitmask (e.g. po_frame_types).
74     */
75#define lsquic_frame_types_str_sz  \
76    QUIC_FRAME_SLEN(QUIC_FRAME_STREAM)           + 1 + \
77    QUIC_FRAME_SLEN(QUIC_FRAME_ACK)              + 1 + \
78    QUIC_FRAME_SLEN(QUIC_FRAME_PADDING)          + 1 + \
79    QUIC_FRAME_SLEN(QUIC_FRAME_RST_STREAM)       + 1 + \
80    QUIC_FRAME_SLEN(QUIC_FRAME_CONNECTION_CLOSE) + 1 + \
81    QUIC_FRAME_SLEN(QUIC_FRAME_GOAWAY)           + 1 + \
82    QUIC_FRAME_SLEN(QUIC_FRAME_WINDOW_UPDATE)    + 1 + \
83    QUIC_FRAME_SLEN(QUIC_FRAME_BLOCKED)          + 1 + \
84    QUIC_FRAME_SLEN(QUIC_FRAME_STOP_WAITING)     + 1 + \
85    QUIC_FRAME_SLEN(QUIC_FRAME_PING)             + 1
86
87
88
89const char *
90lsquic_frame_types_to_str (char *buf, size_t bufsz, enum quic_ft_bit);
91
92#define QFRAME_REGEN_MASK ((1 << QUIC_FRAME_ACK)                \
93                         | (1 << QUIC_FRAME_STOP_WAITING))
94
95#define QFRAME_REGENERATE(frame_type) ((1 << (frame_type)) & QFRAME_REGEN_MASK)
96
97#define QFRAME_ACKABLE_MASK (                               \
98    (1 << QUIC_FRAME_STREAM)                                \
99  | (1 << QUIC_FRAME_RST_STREAM)                            \
100  | (1 << QUIC_FRAME_GOAWAY)                                \
101  | (1 << QUIC_FRAME_WINDOW_UPDATE)                         \
102  | (1 << QUIC_FRAME_PING)                                  \
103  | (1 << QUIC_FRAME_BLOCKED)                               \
104)
105
106#define QFRAME_ACKABLE(frame_type) ((1 << (frame_type)) & QFRAME_ACKABLE_MASK)
107
108#define QFRAME_RETRANSMITTABLE_MASK (                       \
109    (1 << QUIC_FRAME_STREAM)                                \
110  | (1 << QUIC_FRAME_RST_STREAM)                            \
111  | (1 << QUIC_FRAME_GOAWAY)                                \
112  | (1 << QUIC_FRAME_WINDOW_UPDATE)                         \
113  | (1 << QUIC_FRAME_BLOCKED)                               \
114  | (1 << QUIC_FRAME_CONNECTION_CLOSE)                      \
115)
116
117#define QFRAME_RETRANSMITTABLE(frame_type) \
118                        ((1 << (frame_type)) & QFRAME_RETRANSMITTABLE_MASK)
119
120#define GQUIC_MAX_PUBHDR_SZ (1 /* Type */ + 8 /* CID */ + 4 /* Version */ \
121                            + 32 /* Nonce */ + 6 /* Packet Number */ )
122
123#define GQUIC_MIN_PUBHDR_SZ (1 /* Type */ + 1 /* Packet number */)
124
125#define GQUIC_IETF_LONG_HEADER_SIZE (1 /* Type */ + 4 /* Version */ \
126                + 1 /* DCIL/SCIL */ + 8 /* CID */ + 4 /* Packet number */)
127
128/* XXX Nonce? */
129#define IQUIC_MAX_PUBHDR_SZ GQUIC_IETF_LONG_HEADER_SIZE
130
131#define IQUIC_MIN_PUBHDR_SZ (1 /* Type */ + 8 /* CID */ \
132                                                + 1 /* Packet number */)
133
134#define QUIC_MAX_PUBHDR_SZ (GQUIC_MAX_PUBHDR_SZ > IQUIC_MAX_PUBHDR_SZ \
135                                ? GQUIC_MAX_PUBHDR_SZ : IQUIC_MAX_PUBHDR_SZ)
136
137#define QUIC_MIN_PUBHDR_SZ (GQUIC_MIN_PUBHDR_SZ < IQUIC_MIN_PUBHDR_SZ \
138                                ? GQUIC_MIN_PUBHDR_SZ : IQUIC_MIN_PUBHDR_SZ)
139
140/* 12 bytes of FNV hash or encryption IV */
141#define QUIC_PACKET_HASH_SZ 12
142
143/* [draft-hamilton-quic-transport-protocol-01], Section 7 */
144#define QUIC_MAX_IPv4_PACKET_SZ 1370
145#define QUIC_MAX_IPv6_PACKET_SZ 1350
146
147#define QUIC_MAX_PACKET_SZ (QUIC_MAX_IPv4_PACKET_SZ > \
148    QUIC_MAX_IPv6_PACKET_SZ ? QUIC_MAX_IPv4_PACKET_SZ : QUIC_MAX_IPv6_PACKET_SZ)
149
150#define QUIC_MIN_PACKET_OVERHEAD (QUIC_PACKET_HASH_SZ + QUIC_MIN_PUBHDR_SZ)
151
152#define QUIC_MAX_PAYLOAD_SZ (QUIC_MAX_PACKET_SZ - QUIC_MIN_PACKET_OVERHEAD)
153
154#define QUIC_WUF_SZ 13  /* Type (1) + Stream ID (4) + Offset (8) */
155#define QUIC_BLOCKED_FRAME_SZ 5  /* Type (1) + Stream ID (4) */
156#define QUIC_RST_STREAM_SZ 17    /* Type (1) + Stream ID (4) + Offset (8) +
157                                                            Error code (4) */
158#define QUIC_GOAWAY_FRAME_SZ 11  /* Type (1) + Error code (4) + Stream ID (4) +
159                                                Reason phrase length (2) */
160
161
162/* This value represents a different number of bytes used to encode the packet
163 * length based on whether GQUIC or IQUIC is used.
164 */
165enum packno_bits
166{
167    PACKNO_BITS_0 = 0,
168    PACKNO_BITS_1 = 1,
169    PACKNO_BITS_2 = 2,
170    PACKNO_BITS_3 = 3,
171};
172
173
174/* GQUIC maps 0, 1, 2, 3 -> 1, 2, 4, 6 */
175enum
176{
177    GQUIC_PACKNO_LEN_1 = PACKNO_BITS_0,
178    GQUIC_PACKNO_LEN_2 = PACKNO_BITS_1,
179    GQUIC_PACKNO_LEN_4 = PACKNO_BITS_2,
180    GQUIC_PACKNO_LEN_6 = PACKNO_BITS_3,
181};
182
183
184/* IQUIC maps 0, 1, 2, 3 -> 1, 2, 3, 4 (as of ID-17) */
185enum
186{
187    IQUIC_PACKNO_LEN_1 = PACKNO_BITS_0,
188    IQUIC_PACKNO_LEN_2 = PACKNO_BITS_1,
189    IQUIC_PACKNO_LEN_3 = PACKNO_BITS_2,
190    IQUIC_PACKNO_LEN_4 = PACKNO_BITS_3,
191};
192
193
194enum header_type
195{
196    HETY_NOT_SET,       /* This value must be zero */
197    HETY_VERNEG,
198    HETY_INITIAL,
199    HETY_RETRY,
200    HETY_HANDSHAKE,
201    HETY_0RTT,
202};
203
204extern const char *const lsquic_hety2str[];
205
206enum packno_bits
207calc_packno_bits (lsquic_packno_t packno, lsquic_packno_t least_unacked,
208                  uint64_t n_in_flight);
209
210#define gquic_packno_bits2len(b) (((b) << 1) + !(b))
211
212lsquic_packno_t
213restore_packno (lsquic_packno_t cur_packno,
214                unsigned packet_len,
215                lsquic_packno_t max_packno);
216
217#endif
218