comparison src/event/quic/ngx_event_quic_transport.h @ 8673:046c951e393a quic

QUIC: moved all quic sources into src/event/quic.
author Vladimir Homutov <vl@nginx.com>
date Fri, 25 Dec 2020 14:01:28 +0300
parents src/event/ngx_event_quic_transport.h@2dfc5ef29973
children 7df607cb2d11
comparison
equal deleted inserted replaced
8672:13c537def699 8673:046c951e393a
1
2 /*
3 * Copyright (C) Nginx, Inc.
4 */
5
6
7 #ifndef _NGX_EVENT_QUIC_WIRE_H_INCLUDED_
8 #define _NGX_EVENT_QUIC_WIRE_H_INCLUDED_
9
10
11 #include <ngx_config.h>
12 #include <ngx_core.h>
13
14
15 /* QUIC flags in first byte, see quic-transport 17.2 and 17.3 */
16
17 #define NGX_QUIC_PKT_LONG 0x80 /* header form */
18 #define NGX_QUIC_PKT_FIXED_BIT 0x40
19 #define NGX_QUIC_PKT_TYPE 0x30 /* in long packet */
20 #define NGX_QUIC_PKT_KPHASE 0x04 /* in short packet */
21
22 #define ngx_quic_long_pkt(flags) ((flags) & NGX_QUIC_PKT_LONG)
23 #define ngx_quic_short_pkt(flags) (((flags) & NGX_QUIC_PKT_LONG) == 0)
24
25 /* Long packet types */
26 #define NGX_QUIC_PKT_INITIAL 0x00
27 #define NGX_QUIC_PKT_ZRTT 0x10
28 #define NGX_QUIC_PKT_HANDSHAKE 0x20
29 #define NGX_QUIC_PKT_RETRY 0x30
30
31 #define ngx_quic_pkt_in(flags) \
32 (((flags) & NGX_QUIC_PKT_TYPE) == NGX_QUIC_PKT_INITIAL)
33 #define ngx_quic_pkt_zrtt(flags) \
34 (((flags) & NGX_QUIC_PKT_TYPE) == NGX_QUIC_PKT_ZRTT)
35 #define ngx_quic_pkt_hs(flags) \
36 (((flags) & NGX_QUIC_PKT_TYPE) == NGX_QUIC_PKT_HANDSHAKE)
37 #define ngx_quic_pkt_retry(flags) \
38 (((flags) & NGX_QUIC_PKT_TYPE) == NGX_QUIC_PKT_RETRY)
39
40 #define ngx_quic_pkt_rb_mask(flags) \
41 (ngx_quic_long_pkt(flags) ? 0x0C : 0x18)
42 #define ngx_quic_pkt_hp_mask(flags) \
43 (ngx_quic_long_pkt(flags) ? 0x0F : 0x1F)
44
45 #define ngx_quic_level_name(lvl) \
46 (lvl == ssl_encryption_application) ? "app" \
47 : (lvl == ssl_encryption_initial) ? "init" \
48 : (lvl == ssl_encryption_handshake) ? "hs" : "early"
49
50
51 /* 12.4. Frames and Frame Types */
52 #define NGX_QUIC_FT_PADDING 0x00
53 #define NGX_QUIC_FT_PING 0x01
54 #define NGX_QUIC_FT_ACK 0x02
55 #define NGX_QUIC_FT_ACK_ECN 0x03
56 #define NGX_QUIC_FT_RESET_STREAM 0x04
57 #define NGX_QUIC_FT_STOP_SENDING 0x05
58 #define NGX_QUIC_FT_CRYPTO 0x06
59 #define NGX_QUIC_FT_NEW_TOKEN 0x07
60 #define NGX_QUIC_FT_STREAM0 0x08
61 #define NGX_QUIC_FT_STREAM1 0x09
62 #define NGX_QUIC_FT_STREAM2 0x0A
63 #define NGX_QUIC_FT_STREAM3 0x0B
64 #define NGX_QUIC_FT_STREAM4 0x0C
65 #define NGX_QUIC_FT_STREAM5 0x0D
66 #define NGX_QUIC_FT_STREAM6 0x0E
67 #define NGX_QUIC_FT_STREAM7 0x0F
68 #define NGX_QUIC_FT_MAX_DATA 0x10
69 #define NGX_QUIC_FT_MAX_STREAM_DATA 0x11
70 #define NGX_QUIC_FT_MAX_STREAMS 0x12
71 #define NGX_QUIC_FT_MAX_STREAMS2 0x13
72 #define NGX_QUIC_FT_DATA_BLOCKED 0x14
73 #define NGX_QUIC_FT_STREAM_DATA_BLOCKED 0x15
74 #define NGX_QUIC_FT_STREAMS_BLOCKED 0x16
75 #define NGX_QUIC_FT_STREAMS_BLOCKED2 0x17
76 #define NGX_QUIC_FT_NEW_CONNECTION_ID 0x18
77 #define NGX_QUIC_FT_RETIRE_CONNECTION_ID 0x19
78 #define NGX_QUIC_FT_PATH_CHALLENGE 0x1A
79 #define NGX_QUIC_FT_PATH_RESPONSE 0x1B
80 #define NGX_QUIC_FT_CONNECTION_CLOSE 0x1C
81 #define NGX_QUIC_FT_CONNECTION_CLOSE_APP 0x1D
82 #define NGX_QUIC_FT_HANDSHAKE_DONE 0x1E
83
84 /* 22.4. QUIC Transport Error Codes Registry */
85 /* Keep in sync with ngx_quic_errors[] */
86 #define NGX_QUIC_ERR_NO_ERROR 0x00
87 #define NGX_QUIC_ERR_INTERNAL_ERROR 0x01
88 #define NGX_QUIC_ERR_CONNECTION_REFUSED 0x02
89 #define NGX_QUIC_ERR_FLOW_CONTROL_ERROR 0x03
90 #define NGX_QUIC_ERR_STREAM_LIMIT_ERROR 0x04
91 #define NGX_QUIC_ERR_STREAM_STATE_ERROR 0x05
92 #define NGX_QUIC_ERR_FINAL_SIZE_ERROR 0x06
93 #define NGX_QUIC_ERR_FRAME_ENCODING_ERROR 0x07
94 #define NGX_QUIC_ERR_TRANSPORT_PARAMETER_ERROR 0x08
95 #define NGX_QUIC_ERR_CONNECTION_ID_LIMIT_ERROR 0x09
96 #define NGX_QUIC_ERR_PROTOCOL_VIOLATION 0x0A
97 #define NGX_QUIC_ERR_INVALID_TOKEN 0x0B
98 #define NGX_QUIC_ERR_APPLICATION_ERROR 0x0C
99 #define NGX_QUIC_ERR_CRYPTO_BUFFER_EXCEEDED 0x0D
100 #define NGX_QUIC_ERR_KEY_UPDATE_ERROR 0x0E
101
102 #define NGX_QUIC_ERR_LAST 0x0F
103 #define NGX_QUIC_ERR_CRYPTO_ERROR 0x100
104
105 #define NGX_QUIC_ERR_CRYPTO(e) (NGX_QUIC_ERR_CRYPTO_ERROR + (e))
106
107
108 /* Transport parameters */
109 #define NGX_QUIC_TP_ORIGINAL_DCID 0x00
110 #define NGX_QUIC_TP_MAX_IDLE_TIMEOUT 0x01
111 #define NGX_QUIC_TP_SR_TOKEN 0x02
112 #define NGX_QUIC_TP_MAX_UDP_PAYLOAD_SIZE 0x03
113 #define NGX_QUIC_TP_INITIAL_MAX_DATA 0x04
114 #define NGX_QUIC_TP_INITIAL_MAX_STREAM_DATA_BIDI_LOCAL 0x05
115 #define NGX_QUIC_TP_INITIAL_MAX_STREAM_DATA_BIDI_REMOTE 0x06
116 #define NGX_QUIC_TP_INITIAL_MAX_STREAM_DATA_UNI 0x07
117 #define NGX_QUIC_TP_INITIAL_MAX_STREAMS_BIDI 0x08
118 #define NGX_QUIC_TP_INITIAL_MAX_STREAMS_UNI 0x09
119 #define NGX_QUIC_TP_ACK_DELAY_EXPONENT 0x0A
120 #define NGX_QUIC_TP_MAX_ACK_DELAY 0x0B
121 #define NGX_QUIC_TP_DISABLE_ACTIVE_MIGRATION 0x0C
122 #define NGX_QUIC_TP_PREFERRED_ADDRESS 0x0D
123 #define NGX_QUIC_TP_ACTIVE_CONNECTION_ID_LIMIT 0x0E
124 #define NGX_QUIC_TP_INITIAL_SCID 0x0F
125 #define NGX_QUIC_TP_RETRY_SCID 0x10
126
127 #define NGX_QUIC_CID_LEN_MIN 8
128 #define NGX_QUIC_CID_LEN_MAX 20
129
130 #define NGX_QUIC_MAX_RANGES 10
131
132
133 typedef struct {
134 uint64_t gap;
135 uint64_t range;
136 } ngx_quic_ack_range_t;
137
138
139 typedef struct {
140 uint64_t largest;
141 uint64_t delay;
142 uint64_t range_count;
143 uint64_t first_range;
144 uint64_t ect0;
145 uint64_t ect1;
146 uint64_t ce;
147 uint64_t ranges_length;
148 } ngx_quic_ack_frame_t;
149
150
151 typedef struct {
152 uint64_t seqnum;
153 uint64_t retire;
154 uint8_t len;
155 u_char cid[NGX_QUIC_CID_LEN_MAX];
156 u_char srt[NGX_QUIC_SR_TOKEN_LEN];
157 } ngx_quic_new_conn_id_frame_t;
158
159
160 typedef struct {
161 uint64_t length;
162 u_char *data;
163 } ngx_quic_new_token_frame_t;
164
165 /*
166 * common layout for CRYPTO and STREAM frames;
167 * conceptually, CRYPTO frame is also a stream
168 * frame lacking some properties
169 */
170 typedef struct {
171 uint64_t offset;
172 uint64_t length;
173 } ngx_quic_ordered_frame_t;
174
175 typedef ngx_quic_ordered_frame_t ngx_quic_crypto_frame_t;
176
177
178 typedef struct {
179 /* initial fields same as in ngx_quic_ordered_frame_t */
180 uint64_t offset;
181 uint64_t length;
182
183 uint8_t type;
184 uint64_t stream_id;
185 unsigned off:1;
186 unsigned len:1;
187 unsigned fin:1;
188 } ngx_quic_stream_frame_t;
189
190
191 typedef struct {
192 uint64_t max_data;
193 } ngx_quic_max_data_frame_t;
194
195
196 typedef struct {
197 uint64_t error_code;
198 uint64_t frame_type;
199 ngx_str_t reason;
200 ngx_uint_t app; /* unsigned app:1; */
201 } ngx_quic_close_frame_t;
202
203
204 typedef struct {
205 uint64_t id;
206 uint64_t error_code;
207 uint64_t final_size;
208 } ngx_quic_reset_stream_frame_t;
209
210
211 typedef struct {
212 uint64_t id;
213 uint64_t error_code;
214 } ngx_quic_stop_sending_frame_t;
215
216
217 typedef struct {
218 uint64_t limit;
219 ngx_uint_t bidi; /* unsigned: bidi:1 */
220 } ngx_quic_streams_blocked_frame_t;
221
222
223 typedef struct {
224 uint64_t limit;
225 ngx_uint_t bidi; /* unsigned: bidi:1 */
226 } ngx_quic_max_streams_frame_t;
227
228
229 typedef struct {
230 uint64_t id;
231 uint64_t limit;
232 } ngx_quic_max_stream_data_frame_t;
233
234
235 typedef struct {
236 uint64_t limit;
237 } ngx_quic_data_blocked_frame_t;
238
239
240 typedef struct {
241 uint64_t id;
242 uint64_t limit;
243 } ngx_quic_stream_data_blocked_frame_t;
244
245
246 typedef struct {
247 uint64_t sequence_number;
248 } ngx_quic_retire_cid_frame_t;
249
250
251 typedef struct {
252 u_char data[8];
253 } ngx_quic_path_challenge_frame_t;
254
255
256 typedef struct ngx_quic_frame_s ngx_quic_frame_t;
257
258 struct ngx_quic_frame_s {
259 ngx_uint_t type;
260 enum ssl_encryption_level_t level;
261 ngx_queue_t queue;
262 uint64_t pnum;
263 size_t plen;
264 ngx_msec_t first;
265 ngx_msec_t last;
266 ssize_t len;
267 ngx_uint_t need_ack;
268 /* unsigned need_ack:1; */
269
270 ngx_chain_t *data;
271 union {
272 ngx_quic_ack_frame_t ack;
273 ngx_quic_crypto_frame_t crypto;
274 ngx_quic_ordered_frame_t ord;
275 ngx_quic_new_conn_id_frame_t ncid;
276 ngx_quic_new_token_frame_t token;
277 ngx_quic_stream_frame_t stream;
278 ngx_quic_max_data_frame_t max_data;
279 ngx_quic_close_frame_t close;
280 ngx_quic_reset_stream_frame_t reset_stream;
281 ngx_quic_stop_sending_frame_t stop_sending;
282 ngx_quic_streams_blocked_frame_t streams_blocked;
283 ngx_quic_max_streams_frame_t max_streams;
284 ngx_quic_max_stream_data_frame_t max_stream_data;
285 ngx_quic_data_blocked_frame_t data_blocked;
286 ngx_quic_stream_data_blocked_frame_t stream_data_blocked;
287 ngx_quic_retire_cid_frame_t retire_cid;
288 ngx_quic_path_challenge_frame_t path_challenge;
289 ngx_quic_path_challenge_frame_t path_response;
290 } u;
291 };
292
293
294 typedef struct {
295 ngx_log_t *log;
296
297 ngx_quic_keys_t *keys;
298
299 ngx_msec_t received;
300 uint64_t number;
301 uint8_t num_len;
302 uint32_t trunc;
303 uint8_t flags;
304 uint32_t version;
305 ngx_str_t token;
306 enum ssl_encryption_level_t level;
307 ngx_uint_t error;
308
309 /* filled in by parser */
310 ngx_buf_t *raw; /* udp datagram */
311
312 u_char *data; /* quic packet */
313 size_t len;
314
315 /* cleartext fields */
316 ngx_str_t odcid; /* retry packet tag */
317 ngx_str_t dcid;
318 ngx_str_t scid;
319 uint64_t pn;
320 u_char *plaintext;
321 ngx_str_t payload; /* decrypted data */
322
323 unsigned need_ack:1;
324 unsigned key_phase:1;
325 unsigned key_update:1;
326 unsigned parsed:1;
327 unsigned decrypted:1;
328 } ngx_quic_header_t;
329
330
331 u_char *ngx_quic_error_text(uint64_t error_code);
332
333 ngx_int_t ngx_quic_parse_packet(ngx_quic_header_t *pkt);
334
335 size_t ngx_quic_create_version_negotiation(ngx_quic_header_t *pkt, u_char *out);
336
337 size_t ngx_quic_create_header(ngx_quic_header_t *pkt, u_char *out,
338 size_t pkt_len, u_char **pnp);
339
340 size_t ngx_quic_create_retry_itag(ngx_quic_header_t *pkt, u_char *out,
341 u_char **start);
342
343 ssize_t ngx_quic_parse_frame(ngx_quic_header_t *pkt, u_char *start, u_char *end,
344 ngx_quic_frame_t *frame);
345 ssize_t ngx_quic_create_frame(u_char *p, ngx_quic_frame_t *f);
346
347 ssize_t ngx_quic_parse_ack_range(ngx_log_t *log, u_char *start,
348 u_char *end, uint64_t *gap, uint64_t *range);
349 size_t ngx_quic_create_ack_range(u_char *p, uint64_t gap, uint64_t range);
350
351 ngx_int_t ngx_quic_parse_transport_params(u_char *p, u_char *end,
352 ngx_quic_tp_t *tp, ngx_log_t *log);
353 ssize_t ngx_quic_create_transport_params(u_char *p, u_char *end,
354 ngx_quic_tp_t *tp, size_t *clen);
355
356 #endif /* _NGX_EVENT_QUIC_WIRE_H_INCLUDED_ */