1 /*
2  * include/types/quic_frame.h
3  * This file contains QUIC frame definitions.
4  *
5  * Copyright 2019 HAProxy Technologies, Frédéric Lécaille <flecaille@haproxy.com>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation, version 2.1
10  * exclusively.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21 
22 #ifndef _TYPES_QUIC_FRAME_H
23 #define _TYPES_QUIC_FRAME_H
24 #ifdef USE_QUIC
25 #ifndef USE_OPENSSL
26 #error "Must define USE_OPENSSL"
27 #endif
28 
29 #include <stdint.h>
30 #include <stdlib.h>
31 
32 #include <haproxy/list.h>
33 
34 /* QUIC frame types. */
35 enum quic_frame_type {
36 	QUIC_FT_PADDING      = 0x00,
37 	QUIC_FT_PING         = 0x01,
38 	QUIC_FT_ACK          = 0x02,
39 	QUIC_FT_ACK_ECN      = 0x03,
40 	QUIC_FT_RESET_STREAM = 0x04,
41 	QUIC_FT_STOP_SENDING = 0x05,
42 	QUIC_FT_CRYPTO       = 0x06,
43 	QUIC_FT_NEW_TOKEN    = 0x07,
44 
45 	QUIC_FT_STREAM_8     = 0x08,
46 	QUIC_FT_STREAM_9     = 0x09,
47 	QUIC_FT_STREAM_A     = 0x0a,
48 	QUIC_FT_STREAM_B     = 0x0b,
49 	QUIC_FT_STREAM_C     = 0x0c,
50 	QUIC_FT_STREAM_D     = 0x0d,
51 	QUIC_FT_STREAM_E     = 0x0e,
52 	QUIC_FT_STREAM_F     = 0x0f,
53 
54 	QUIC_FT_MAX_DATA             = 0x10,
55 	QUIC_FT_MAX_STREAM_DATA      = 0x11,
56 	QUIC_FT_MAX_STREAMS_BIDI     = 0x12,
57 	QUIC_FT_MAX_STREAMS_UNI      = 0x13,
58 	QUIC_FT_DATA_BLOCKED         = 0x14,
59 	QUIC_FT_STREAM_DATA_BLOCKED  = 0x15,
60 	QUIC_FT_STREAMS_BLOCKED_BIDI = 0x16,
61 	QUIC_FT_STREAMS_BLOCKED_UNI  = 0x17,
62 	QUIC_FT_NEW_CONNECTION_ID    = 0x18,
63 	QUIC_FT_RETIRE_CONNECTION_ID = 0x19,
64 	QUIC_FT_PATH_CHALLENGE       = 0x1a,
65 	QUIC_FT_PATH_RESPONSE        = 0x1b,
66 	QUIC_FT_CONNECTION_CLOSE     = 0x1c,
67 	QUIC_FT_CONNECTION_CLOSE_APP = 0x1d,
68 	QUIC_FT_HANDSHAKE_DONE       = 0x1e,
69 	/* Do not insert enums after the following one. */
70 	QUIC_FT_MAX
71 };
72 
73 #define QUIC_FT_PKT_TYPE_I_BITMASK (1 << QUIC_PACKET_TYPE_INITIAL)
74 #define QUIC_FT_PKT_TYPE_0_BITMASK (1 << QUIC_PACKET_TYPE_0RTT)
75 #define QUIC_FT_PKT_TYPE_H_BITMASK (1 << QUIC_PACKET_TYPE_HANDSHAKE)
76 #define QUIC_FT_PKT_TYPE_1_BITMASK (1 << QUIC_PACKET_TYPE_SHORT)
77 
78 #define QUIC_FT_PKT_TYPE_IH01_BITMASK \
79 	(QUIC_FT_PKT_TYPE_I_BITMASK | QUIC_FT_PKT_TYPE_H_BITMASK | \
80 	 QUIC_FT_PKT_TYPE_0_BITMASK | QUIC_FT_PKT_TYPE_1_BITMASK)
81 
82 #define QUIC_FT_PKT_TYPE_IH_1_BITMASK \
83 	(QUIC_FT_PKT_TYPE_I_BITMASK | QUIC_FT_PKT_TYPE_H_BITMASK | \
84 	 QUIC_FT_PKT_TYPE_1_BITMASK)
85 
86 #define QUIC_FT_PKT_TYPE___01_BITMASK \
87 	(QUIC_FT_PKT_TYPE_0_BITMASK | QUIC_FT_PKT_TYPE_1_BITMASK)
88 
89 #define QUIC_FT_PKT_TYPE____1_BITMASK QUIC_FT_PKT_TYPE_1_BITMASK
90 
91 #define QUIC_STREAM_FRAME_TYPE_FIN_BIT     0x01
92 #define QUIC_STREAM_FRAME_TYPE_LEN_BIT     0x02
93 #define QUIC_STREAM_FRAME_TYPE_OFF_BIT     0x04
94 
95 /* Servers have the stream initiator bit set. */
96 #define QUIC_STREAM_FRAME_ID_INITIATOR_BIT 0x01
97 /* Unidirectional streams have the direction bit set. */
98 #define QUIC_STREAM_FRAME_ID_DIR_BIT       0x02
99 
100 #define QUIC_PATH_CHALLENGE_LEN         8
101 
102 struct quic_padding {
103 	size_t len;
104 };
105 
106 struct quic_ack {
107 	uint64_t largest_ack;
108 	uint64_t ack_delay;
109 	uint64_t ack_range_num;
110 	uint64_t first_ack_range;
111 };
112 
113 /* Structure used when emitting ACK frames. */
114 struct quic_tx_ack {
115 	uint64_t ack_delay;
116 	struct quic_arngs *arngs;
117 };
118 
119 struct quic_reset_stream {
120 	uint64_t id;
121 	uint64_t app_error_code;
122 	uint64_t final_size;
123 };
124 
125 struct quic_stop_sending_frame {
126 	uint64_t id;
127 	uint64_t app_error_code;
128 };
129 
130 struct quic_crypto {
131 	uint64_t offset;
132 	uint64_t len;
133 	const struct quic_enc_level *qel;
134 	const unsigned char *data;
135 };
136 
137 struct quic_new_token {
138 	uint64_t len;
139 	const unsigned char *data;
140 };
141 
142 struct quic_stream {
143 	uint64_t id;
144 	uint64_t offset;
145 	uint64_t len;
146 	const unsigned char *data;
147 };
148 
149 struct quic_max_data {
150 	uint64_t max_data;
151 };
152 
153 struct quic_max_stream_data {
154 	uint64_t id;
155 	uint64_t max_stream_data;
156 };
157 
158 struct quic_max_streams {
159 	uint64_t max_streams;
160 };
161 
162 struct quic_data_blocked {
163 	uint64_t limit;
164 };
165 
166 struct quic_stream_data_blocked {
167 	uint64_t id;
168 	uint64_t limit;
169 };
170 
171 struct quic_streams_blocked {
172 	uint64_t limit;
173 };
174 
175 struct quic_new_connection_id {
176 	uint64_t seq_num;
177 	uint64_t retire_prior_to;
178 	struct {
179 		unsigned char len;
180 		const unsigned char *data;
181 	} cid;
182 	const unsigned char *stateless_reset_token;
183 };
184 
185 struct quic_retire_connection_id {
186 	uint64_t seq_num;
187 };
188 
189 struct quic_path_challenge {
190 	unsigned char data[QUIC_PATH_CHALLENGE_LEN];
191 };
192 
193 struct quic_path_challenge_response {
194 	unsigned char data[QUIC_PATH_CHALLENGE_LEN];
195 };
196 
197 struct quic_connection_close {
198 	uint64_t error_code;
199 	uint64_t frame_type;
200 	uint64_t reason_phrase_len;
201 	unsigned char *reason_phrase;
202 };
203 
204 struct quic_connection_close_app {
205 	uint64_t error_code;
206 	uint64_t reason_phrase_len;
207 	unsigned char *reason_phrase;
208 };
209 
210 struct quic_frame {
211 	struct list list;
212 	unsigned char type;
213 	union {
214 		struct quic_padding padding;
215 		struct quic_ack ack;
216 		struct quic_tx_ack tx_ack;
217 		struct quic_crypto crypto;
218 		struct quic_reset_stream reset_stream;
219 		struct quic_stop_sending_frame stop_sending_frame;
220 		struct quic_new_token new_token;
221 		struct quic_stream stream;
222 		struct quic_max_data max_data;
223 		struct quic_max_stream_data max_stream_data;
224 		struct quic_max_streams max_streams_bidi;
225 		struct quic_max_streams max_streams_uni;
226 		struct quic_data_blocked data_blocked;
227 		struct quic_stream_data_blocked stream_data_blocked;
228 		struct quic_streams_blocked streams_blocked_bidi;
229 		struct quic_streams_blocked streams_blocked_uni;
230 		struct quic_new_connection_id new_connection_id;
231 		struct quic_retire_connection_id retire_connection_id;
232 		struct quic_path_challenge path_challenge;
233 		struct quic_path_challenge_response path_challenge_response;
234 		struct quic_connection_close connection_close;
235 		struct quic_connection_close_app connection_close_app;
236 	};
237 };
238 
239 #endif /* USE_QUIC */
240 #endif /* _TYPES_QUIC_FRAME_H */
241