1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 * SPDX-License-Identifier: curl
22 *
23 ***************************************************************************/
24
25 #include "curl_setup.h"
26
27 #if defined(USE_NGTCP2) && defined(USE_NGHTTP3)
28 #include <ngtcp2/ngtcp2.h>
29 #include <nghttp3/nghttp3.h>
30
31 #ifdef USE_OPENSSL
32 #include <openssl/err.h>
33 #if defined(OPENSSL_IS_BORINGSSL) || defined(OPENSSL_IS_AWSLC)
34 #include <ngtcp2/ngtcp2_crypto_boringssl.h>
35 #else
36 #include <ngtcp2/ngtcp2_crypto_quictls.h>
37 #endif
38 #include "vtls/openssl.h"
39 #elif defined(USE_GNUTLS)
40 #include <ngtcp2/ngtcp2_crypto_gnutls.h>
41 #include "vtls/gtls.h"
42 #elif defined(USE_WOLFSSL)
43 #include <ngtcp2/ngtcp2_crypto_wolfssl.h>
44 #endif
45
46 #include "urldata.h"
47 #include "hash.h"
48 #include "sendf.h"
49 #include "strdup.h"
50 #include "rand.h"
51 #include "multiif.h"
52 #include "strcase.h"
53 #include "cfilters.h"
54 #include "cf-socket.h"
55 #include "connect.h"
56 #include "progress.h"
57 #include "strerror.h"
58 #include "dynbuf.h"
59 #include "http1.h"
60 #include "select.h"
61 #include "inet_pton.h"
62 #include "transfer.h"
63 #include "vquic.h"
64 #include "vquic_int.h"
65 #include "vquic-tls.h"
66 #include "vtls/keylog.h"
67 #include "vtls/vtls.h"
68 #include "curl_ngtcp2.h"
69
70 #include "warnless.h"
71
72 /* The last 3 #include files should be in this order */
73 #include "curl_printf.h"
74 #include "curl_memory.h"
75 #include "memdebug.h"
76
77
78 #define QUIC_MAX_STREAMS (256*1024)
79 #define QUIC_MAX_DATA (1*1024*1024)
80 #define QUIC_HANDSHAKE_TIMEOUT (10*NGTCP2_SECONDS)
81
82 /* A stream window is the maximum amount we need to buffer for
83 * each active transfer. We use HTTP/3 flow control and only ACK
84 * when we take things out of the buffer.
85 * Chunk size is large enough to take a full DATA frame */
86 #define H3_STREAM_WINDOW_SIZE (128 * 1024)
87 #define H3_STREAM_CHUNK_SIZE (16 * 1024)
88 /* The pool keeps spares around and half of a full stream windows
89 * seems good. More does not seem to improve performance.
90 * The benefit of the pool is that stream buffer to not keep
91 * spares. So memory consumption goes down when streams run empty,
92 * have a large upload done, etc. */
93 #define H3_STREAM_POOL_SPARES \
94 (H3_STREAM_WINDOW_SIZE / H3_STREAM_CHUNK_SIZE ) / 2
95 /* Receive and Send max number of chunks just follows from the
96 * chunk size and window size */
97 #define H3_STREAM_RECV_CHUNKS \
98 (H3_STREAM_WINDOW_SIZE / H3_STREAM_CHUNK_SIZE)
99 #define H3_STREAM_SEND_CHUNKS \
100 (H3_STREAM_WINDOW_SIZE / H3_STREAM_CHUNK_SIZE)
101
102
103 /*
104 * Store ngtcp2 version info in this buffer.
105 */
Curl_ngtcp2_ver(char * p,size_t len)106 void Curl_ngtcp2_ver(char *p, size_t len)
107 {
108 const ngtcp2_info *ng2 = ngtcp2_version(0);
109 const nghttp3_info *ht3 = nghttp3_version(0);
110 (void)msnprintf(p, len, "ngtcp2/%s nghttp3/%s",
111 ng2->version_str, ht3->version_str);
112 }
113
114 struct cf_ngtcp2_ctx {
115 struct cf_quic_ctx q;
116 struct ssl_peer peer;
117 struct curl_tls_ctx tls;
118 ngtcp2_path connected_path;
119 ngtcp2_conn *qconn;
120 ngtcp2_cid dcid;
121 ngtcp2_cid scid;
122 uint32_t version;
123 ngtcp2_settings settings;
124 ngtcp2_transport_params transport_params;
125 ngtcp2_ccerr last_error;
126 ngtcp2_crypto_conn_ref conn_ref;
127 struct cf_call_data call_data;
128 nghttp3_conn *h3conn;
129 nghttp3_settings h3settings;
130 struct curltime started_at; /* time the current attempt started */
131 struct curltime handshake_at; /* time connect handshake finished */
132 struct curltime reconnect_at; /* time the next attempt should start */
133 struct bufc_pool stream_bufcp; /* chunk pool for streams */
134 struct dynbuf scratch; /* temp buffer for header construction */
135 struct Curl_hash streams; /* hash `data->id` to `h3_stream_ctx` */
136 size_t max_stream_window; /* max flow window for one stream */
137 uint64_t max_idle_ms; /* max idle time for QUIC connection */
138 uint64_t used_bidi_streams; /* bidi streams we have opened */
139 uint64_t max_bidi_streams; /* max bidi streams we can open */
140 int qlogfd;
141 BIT(conn_closed); /* connection is closed */
142 };
143
144 /* How to access `call_data` from a cf_ngtcp2 filter */
145 #undef CF_CTX_CALL_DATA
146 #define CF_CTX_CALL_DATA(cf) \
147 ((struct cf_ngtcp2_ctx *)(cf)->ctx)->call_data
148
149 struct pkt_io_ctx;
150 static CURLcode cf_progress_ingress(struct Curl_cfilter *cf,
151 struct Curl_easy *data,
152 struct pkt_io_ctx *pktx);
153 static CURLcode cf_progress_egress(struct Curl_cfilter *cf,
154 struct Curl_easy *data,
155 struct pkt_io_ctx *pktx);
156
157 /**
158 * All about the H3 internals of a stream
159 */
160 struct h3_stream_ctx {
161 curl_int64_t id; /* HTTP/3 protocol identifier */
162 struct bufq sendbuf; /* h3 request body */
163 struct h1_req_parser h1; /* h1 request parsing */
164 size_t sendbuf_len_in_flight; /* sendbuf amount "in flight" */
165 size_t upload_blocked_len; /* the amount written last and EGAINed */
166 curl_uint64_t error3; /* HTTP/3 stream error code */
167 curl_off_t upload_left; /* number of request bytes left to upload */
168 int status_code; /* HTTP status code */
169 CURLcode xfer_result; /* result from xfer_resp_write(_hd) */
170 bool resp_hds_complete; /* we have a complete, final response */
171 bool closed; /* TRUE on stream close */
172 bool reset; /* TRUE on stream reset */
173 bool send_closed; /* stream is local closed */
174 BIT(quic_flow_blocked); /* stream is blocked by QUIC flow control */
175 };
176
177 #define H3_STREAM_CTX(ctx,data) ((struct h3_stream_ctx *)(\
178 data? Curl_hash_offt_get(&(ctx)->streams, (data)->id) : NULL))
179 #define H3_STREAM_CTX_ID(ctx,id) ((struct h3_stream_ctx *)(\
180 Curl_hash_offt_get(&(ctx)->streams, (id))))
181
h3_stream_ctx_free(struct h3_stream_ctx * stream)182 static void h3_stream_ctx_free(struct h3_stream_ctx *stream)
183 {
184 Curl_bufq_free(&stream->sendbuf);
185 Curl_h1_req_parse_free(&stream->h1);
186 free(stream);
187 }
188
h3_stream_hash_free(void * stream)189 static void h3_stream_hash_free(void *stream)
190 {
191 DEBUGASSERT(stream);
192 h3_stream_ctx_free((struct h3_stream_ctx *)stream);
193 }
194
h3_data_setup(struct Curl_cfilter * cf,struct Curl_easy * data)195 static CURLcode h3_data_setup(struct Curl_cfilter *cf,
196 struct Curl_easy *data)
197 {
198 struct cf_ngtcp2_ctx *ctx = cf->ctx;
199 struct h3_stream_ctx *stream = H3_STREAM_CTX(ctx, data);
200
201 if(!data || !data->req.p.http) {
202 failf(data, "initialization failure, transfer not http initialized");
203 return CURLE_FAILED_INIT;
204 }
205
206 if(stream)
207 return CURLE_OK;
208
209 stream = calloc(1, sizeof(*stream));
210 if(!stream)
211 return CURLE_OUT_OF_MEMORY;
212
213 stream->id = -1;
214 /* on send, we control how much we put into the buffer */
215 Curl_bufq_initp(&stream->sendbuf, &ctx->stream_bufcp,
216 H3_STREAM_SEND_CHUNKS, BUFQ_OPT_NONE);
217 stream->sendbuf_len_in_flight = 0;
218 Curl_h1_req_parse_init(&stream->h1, H1_PARSE_DEFAULT_MAX_LINE_LEN);
219
220 if(!Curl_hash_offt_set(&ctx->streams, data->id, stream)) {
221 h3_stream_ctx_free(stream);
222 return CURLE_OUT_OF_MEMORY;
223 }
224
225 return CURLE_OK;
226 }
227
cf_ngtcp2_stream_close(struct Curl_cfilter * cf,struct Curl_easy * data,struct h3_stream_ctx * stream)228 static void cf_ngtcp2_stream_close(struct Curl_cfilter *cf,
229 struct Curl_easy *data,
230 struct h3_stream_ctx *stream)
231 {
232 struct cf_ngtcp2_ctx *ctx = cf->ctx;
233 DEBUGASSERT(data);
234 DEBUGASSERT(stream);
235 if(!stream->closed && ctx->qconn && ctx->h3conn) {
236 CURLcode result;
237
238 nghttp3_conn_set_stream_user_data(ctx->h3conn, stream->id, NULL);
239 ngtcp2_conn_set_stream_user_data(ctx->qconn, stream->id, NULL);
240 stream->closed = TRUE;
241 (void)ngtcp2_conn_shutdown_stream(ctx->qconn, 0, stream->id,
242 NGHTTP3_H3_REQUEST_CANCELLED);
243 result = cf_progress_egress(cf, data, NULL);
244 if(result)
245 CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] cancel stream -> %d",
246 stream->id, result);
247 }
248 }
249
h3_data_done(struct Curl_cfilter * cf,struct Curl_easy * data)250 static void h3_data_done(struct Curl_cfilter *cf, struct Curl_easy *data)
251 {
252 struct cf_ngtcp2_ctx *ctx = cf->ctx;
253 struct h3_stream_ctx *stream = H3_STREAM_CTX(ctx, data);
254 (void)cf;
255 if(stream) {
256 CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] easy handle is done",
257 stream->id);
258 cf_ngtcp2_stream_close(cf, data, stream);
259 Curl_hash_offt_remove(&ctx->streams, data->id);
260 }
261 }
262
get_stream_easy(struct Curl_cfilter * cf,struct Curl_easy * data,int64_t stream_id,struct h3_stream_ctx ** pstream)263 static struct Curl_easy *get_stream_easy(struct Curl_cfilter *cf,
264 struct Curl_easy *data,
265 int64_t stream_id,
266 struct h3_stream_ctx **pstream)
267 {
268 struct cf_ngtcp2_ctx *ctx = cf->ctx;
269 struct Curl_easy *sdata;
270 struct h3_stream_ctx *stream;
271
272 (void)cf;
273 stream = H3_STREAM_CTX(ctx, data);
274 if(stream && stream->id == stream_id) {
275 *pstream = stream;
276 return data;
277 }
278 else {
279 DEBUGASSERT(data->multi);
280 for(sdata = data->multi->easyp; sdata; sdata = sdata->next) {
281 if(sdata->conn != data->conn)
282 continue;
283 stream = H3_STREAM_CTX(ctx, sdata);
284 if(stream && stream->id == stream_id) {
285 *pstream = stream;
286 return sdata;
287 }
288 }
289 }
290 *pstream = NULL;
291 return NULL;
292 }
293
h3_drain_stream(struct Curl_cfilter * cf,struct Curl_easy * data)294 static void h3_drain_stream(struct Curl_cfilter *cf,
295 struct Curl_easy *data)
296 {
297 struct cf_ngtcp2_ctx *ctx = cf->ctx;
298 struct h3_stream_ctx *stream = H3_STREAM_CTX(ctx, data);
299 unsigned char bits;
300
301 (void)cf;
302 bits = CURL_CSELECT_IN;
303 if(stream && stream->upload_left && !stream->send_closed)
304 bits |= CURL_CSELECT_OUT;
305 if(data->state.select_bits != bits) {
306 data->state.select_bits = bits;
307 Curl_expire(data, 0, EXPIRE_RUN_NOW);
308 }
309 }
310
311 /* ngtcp2 default congestion controller does not perform pacing. Limit
312 the maximum packet burst to MAX_PKT_BURST packets. */
313 #define MAX_PKT_BURST 10
314
315 struct pkt_io_ctx {
316 struct Curl_cfilter *cf;
317 struct Curl_easy *data;
318 ngtcp2_tstamp ts;
319 size_t pkt_count;
320 ngtcp2_path_storage ps;
321 };
322
pktx_update_time(struct pkt_io_ctx * pktx,struct Curl_cfilter * cf)323 static void pktx_update_time(struct pkt_io_ctx *pktx,
324 struct Curl_cfilter *cf)
325 {
326 struct cf_ngtcp2_ctx *ctx = cf->ctx;
327
328 vquic_ctx_update_time(&ctx->q);
329 pktx->ts = ctx->q.last_op.tv_sec * NGTCP2_SECONDS +
330 ctx->q.last_op.tv_usec * NGTCP2_MICROSECONDS;
331 }
332
pktx_init(struct pkt_io_ctx * pktx,struct Curl_cfilter * cf,struct Curl_easy * data)333 static void pktx_init(struct pkt_io_ctx *pktx,
334 struct Curl_cfilter *cf,
335 struct Curl_easy *data)
336 {
337 pktx->cf = cf;
338 pktx->data = data;
339 pktx->pkt_count = 0;
340 ngtcp2_path_storage_zero(&pktx->ps);
341 pktx_update_time(pktx, cf);
342 }
343
344 static int cb_h3_acked_req_body(nghttp3_conn *conn, int64_t stream_id,
345 uint64_t datalen, void *user_data,
346 void *stream_user_data);
347
get_conn(ngtcp2_crypto_conn_ref * conn_ref)348 static ngtcp2_conn *get_conn(ngtcp2_crypto_conn_ref *conn_ref)
349 {
350 struct Curl_cfilter *cf = conn_ref->user_data;
351 struct cf_ngtcp2_ctx *ctx = cf->ctx;
352 return ctx->qconn;
353 }
354
355 #ifdef DEBUG_NGTCP2
quic_printf(void * user_data,const char * fmt,...)356 static void quic_printf(void *user_data, const char *fmt, ...)
357 {
358 struct Curl_cfilter *cf = user_data;
359 struct cf_ngtcp2_ctx *ctx = cf->ctx;
360
361 (void)ctx; /* TODO: need an easy handle to infof() message */
362 va_list ap;
363 va_start(ap, fmt);
364 vfprintf(stderr, fmt, ap);
365 va_end(ap);
366 fprintf(stderr, "\n");
367 }
368 #endif
369
qlog_callback(void * user_data,uint32_t flags,const void * data,size_t datalen)370 static void qlog_callback(void *user_data, uint32_t flags,
371 const void *data, size_t datalen)
372 {
373 struct Curl_cfilter *cf = user_data;
374 struct cf_ngtcp2_ctx *ctx = cf->ctx;
375 (void)flags;
376 if(ctx->qlogfd != -1) {
377 ssize_t rc = write(ctx->qlogfd, data, datalen);
378 if(rc == -1) {
379 /* on write error, stop further write attempts */
380 close(ctx->qlogfd);
381 ctx->qlogfd = -1;
382 }
383 }
384
385 }
386
quic_settings(struct cf_ngtcp2_ctx * ctx,struct Curl_easy * data,struct pkt_io_ctx * pktx)387 static void quic_settings(struct cf_ngtcp2_ctx *ctx,
388 struct Curl_easy *data,
389 struct pkt_io_ctx *pktx)
390 {
391 ngtcp2_settings *s = &ctx->settings;
392 ngtcp2_transport_params *t = &ctx->transport_params;
393
394 ngtcp2_settings_default(s);
395 ngtcp2_transport_params_default(t);
396 #ifdef DEBUG_NGTCP2
397 s->log_printf = quic_printf;
398 #else
399 s->log_printf = NULL;
400 #endif
401
402 (void)data;
403 s->initial_ts = pktx->ts;
404 s->handshake_timeout = QUIC_HANDSHAKE_TIMEOUT;
405 s->max_window = 100 * ctx->max_stream_window;
406 s->max_stream_window = ctx->max_stream_window;
407
408 t->initial_max_data = 10 * ctx->max_stream_window;
409 t->initial_max_stream_data_bidi_local = ctx->max_stream_window;
410 t->initial_max_stream_data_bidi_remote = ctx->max_stream_window;
411 t->initial_max_stream_data_uni = ctx->max_stream_window;
412 t->initial_max_streams_bidi = QUIC_MAX_STREAMS;
413 t->initial_max_streams_uni = QUIC_MAX_STREAMS;
414 t->max_idle_timeout = (ctx->max_idle_ms * NGTCP2_MILLISECONDS);
415 if(ctx->qlogfd != -1) {
416 s->qlog_write = qlog_callback;
417 }
418 }
419
420 static int init_ngh3_conn(struct Curl_cfilter *cf);
421
cb_handshake_completed(ngtcp2_conn * tconn,void * user_data)422 static int cb_handshake_completed(ngtcp2_conn *tconn, void *user_data)
423 {
424 (void)user_data;
425 (void)tconn;
426 return 0;
427 }
428
429 static void cf_ngtcp2_conn_close(struct Curl_cfilter *cf,
430 struct Curl_easy *data);
431
cf_ngtcp2_err_is_fatal(int code)432 static bool cf_ngtcp2_err_is_fatal(int code)
433 {
434 return (NGTCP2_ERR_FATAL >= code) ||
435 (NGTCP2_ERR_DROP_CONN == code) ||
436 (NGTCP2_ERR_IDLE_CLOSE == code);
437 }
438
cf_ngtcp2_err_set(struct Curl_cfilter * cf,struct Curl_easy * data,int code)439 static void cf_ngtcp2_err_set(struct Curl_cfilter *cf,
440 struct Curl_easy *data, int code)
441 {
442 struct cf_ngtcp2_ctx *ctx = cf->ctx;
443 if(!ctx->last_error.error_code) {
444 if(NGTCP2_ERR_CRYPTO == code) {
445 ngtcp2_ccerr_set_tls_alert(&ctx->last_error,
446 ngtcp2_conn_get_tls_alert(ctx->qconn),
447 NULL, 0);
448 }
449 else {
450 ngtcp2_ccerr_set_liberr(&ctx->last_error, code, NULL, 0);
451 }
452 }
453 if(cf_ngtcp2_err_is_fatal(code))
454 cf_ngtcp2_conn_close(cf, data);
455 }
456
cf_ngtcp2_h3_err_is_fatal(int code)457 static bool cf_ngtcp2_h3_err_is_fatal(int code)
458 {
459 return (NGHTTP3_ERR_FATAL >= code) ||
460 (NGHTTP3_ERR_H3_CLOSED_CRITICAL_STREAM == code);
461 }
462
cf_ngtcp2_h3_err_set(struct Curl_cfilter * cf,struct Curl_easy * data,int code)463 static void cf_ngtcp2_h3_err_set(struct Curl_cfilter *cf,
464 struct Curl_easy *data, int code)
465 {
466 struct cf_ngtcp2_ctx *ctx = cf->ctx;
467 if(!ctx->last_error.error_code) {
468 ngtcp2_ccerr_set_application_error(&ctx->last_error,
469 nghttp3_err_infer_quic_app_error_code(code), NULL, 0);
470 }
471 if(cf_ngtcp2_h3_err_is_fatal(code))
472 cf_ngtcp2_conn_close(cf, data);
473 }
474
cb_recv_stream_data(ngtcp2_conn * tconn,uint32_t flags,int64_t sid,uint64_t offset,const uint8_t * buf,size_t buflen,void * user_data,void * stream_user_data)475 static int cb_recv_stream_data(ngtcp2_conn *tconn, uint32_t flags,
476 int64_t sid, uint64_t offset,
477 const uint8_t *buf, size_t buflen,
478 void *user_data, void *stream_user_data)
479 {
480 struct Curl_cfilter *cf = user_data;
481 struct cf_ngtcp2_ctx *ctx = cf->ctx;
482 curl_int64_t stream_id = (curl_int64_t)sid;
483 nghttp3_ssize nconsumed;
484 int fin = (flags & NGTCP2_STREAM_DATA_FLAG_FIN) ? 1 : 0;
485 struct Curl_easy *data = stream_user_data;
486 (void)offset;
487 (void)data;
488
489 nconsumed =
490 nghttp3_conn_read_stream(ctx->h3conn, stream_id, buf, buflen, fin);
491 if(!data)
492 data = CF_DATA_CURRENT(cf);
493 if(data)
494 CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] read_stream(len=%zu) -> %zd",
495 stream_id, buflen, nconsumed);
496 if(nconsumed < 0) {
497 struct h3_stream_ctx *stream = H3_STREAM_CTX_ID(ctx, stream_id);
498 if(data && stream) {
499 CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] error on known stream, "
500 "reset=%d, closed=%d",
501 stream_id, stream->reset, stream->closed);
502 }
503 return NGTCP2_ERR_CALLBACK_FAILURE;
504 }
505
506 /* number of bytes inside buflen which consists of framing overhead
507 * including QPACK HEADERS. In other words, it does not consume payload of
508 * DATA frame. */
509 ngtcp2_conn_extend_max_stream_offset(tconn, stream_id, nconsumed);
510 ngtcp2_conn_extend_max_offset(tconn, nconsumed);
511
512 return 0;
513 }
514
515 static int
cb_acked_stream_data_offset(ngtcp2_conn * tconn,int64_t stream_id,uint64_t offset,uint64_t datalen,void * user_data,void * stream_user_data)516 cb_acked_stream_data_offset(ngtcp2_conn *tconn, int64_t stream_id,
517 uint64_t offset, uint64_t datalen, void *user_data,
518 void *stream_user_data)
519 {
520 struct Curl_cfilter *cf = user_data;
521 struct cf_ngtcp2_ctx *ctx = cf->ctx;
522 int rv;
523 (void)stream_id;
524 (void)tconn;
525 (void)offset;
526 (void)datalen;
527 (void)stream_user_data;
528
529 rv = nghttp3_conn_add_ack_offset(ctx->h3conn, stream_id, datalen);
530 if(rv && rv != NGHTTP3_ERR_STREAM_NOT_FOUND) {
531 return NGTCP2_ERR_CALLBACK_FAILURE;
532 }
533
534 return 0;
535 }
536
cb_stream_close(ngtcp2_conn * tconn,uint32_t flags,int64_t sid,uint64_t app_error_code,void * user_data,void * stream_user_data)537 static int cb_stream_close(ngtcp2_conn *tconn, uint32_t flags,
538 int64_t sid, uint64_t app_error_code,
539 void *user_data, void *stream_user_data)
540 {
541 struct Curl_cfilter *cf = user_data;
542 struct cf_ngtcp2_ctx *ctx = cf->ctx;
543 struct Curl_easy *data = stream_user_data;
544 curl_int64_t stream_id = (curl_int64_t)sid;
545 int rv;
546
547 (void)tconn;
548 /* stream is closed... */
549 if(!data)
550 data = CF_DATA_CURRENT(cf);
551 if(!data)
552 return NGTCP2_ERR_CALLBACK_FAILURE;
553
554 if(!(flags & NGTCP2_STREAM_CLOSE_FLAG_APP_ERROR_CODE_SET)) {
555 app_error_code = NGHTTP3_H3_NO_ERROR;
556 }
557
558 rv = nghttp3_conn_close_stream(ctx->h3conn, stream_id, app_error_code);
559 CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] quic close(app_error=%"
560 CURL_PRIu64 ") -> %d", stream_id, (curl_uint64_t)app_error_code,
561 rv);
562 if(rv && rv != NGHTTP3_ERR_STREAM_NOT_FOUND) {
563 cf_ngtcp2_h3_err_set(cf, data, rv);
564 return NGTCP2_ERR_CALLBACK_FAILURE;
565 }
566
567 return 0;
568 }
569
cb_stream_reset(ngtcp2_conn * tconn,int64_t sid,uint64_t final_size,uint64_t app_error_code,void * user_data,void * stream_user_data)570 static int cb_stream_reset(ngtcp2_conn *tconn, int64_t sid,
571 uint64_t final_size, uint64_t app_error_code,
572 void *user_data, void *stream_user_data)
573 {
574 struct Curl_cfilter *cf = user_data;
575 struct cf_ngtcp2_ctx *ctx = cf->ctx;
576 curl_int64_t stream_id = (curl_int64_t)sid;
577 struct Curl_easy *data = stream_user_data;
578 int rv;
579 (void)tconn;
580 (void)final_size;
581 (void)app_error_code;
582 (void)data;
583
584 rv = nghttp3_conn_shutdown_stream_read(ctx->h3conn, stream_id);
585 CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] reset -> %d", stream_id, rv);
586 if(rv && rv != NGHTTP3_ERR_STREAM_NOT_FOUND) {
587 return NGTCP2_ERR_CALLBACK_FAILURE;
588 }
589
590 return 0;
591 }
592
cb_stream_stop_sending(ngtcp2_conn * tconn,int64_t stream_id,uint64_t app_error_code,void * user_data,void * stream_user_data)593 static int cb_stream_stop_sending(ngtcp2_conn *tconn, int64_t stream_id,
594 uint64_t app_error_code, void *user_data,
595 void *stream_user_data)
596 {
597 struct Curl_cfilter *cf = user_data;
598 struct cf_ngtcp2_ctx *ctx = cf->ctx;
599 int rv;
600 (void)tconn;
601 (void)app_error_code;
602 (void)stream_user_data;
603
604 rv = nghttp3_conn_shutdown_stream_read(ctx->h3conn, stream_id);
605 if(rv && rv != NGHTTP3_ERR_STREAM_NOT_FOUND) {
606 return NGTCP2_ERR_CALLBACK_FAILURE;
607 }
608
609 return 0;
610 }
611
cb_extend_max_local_streams_bidi(ngtcp2_conn * tconn,uint64_t max_streams,void * user_data)612 static int cb_extend_max_local_streams_bidi(ngtcp2_conn *tconn,
613 uint64_t max_streams,
614 void *user_data)
615 {
616 struct Curl_cfilter *cf = user_data;
617 struct cf_ngtcp2_ctx *ctx = cf->ctx;
618 struct Curl_easy *data = CF_DATA_CURRENT(cf);
619
620 (void)tconn;
621 ctx->max_bidi_streams = max_streams;
622 if(data)
623 CURL_TRC_CF(data, cf, "max bidi streams now %" CURL_PRIu64
624 ", used %" CURL_PRIu64, (curl_uint64_t)ctx->max_bidi_streams,
625 (curl_uint64_t)ctx->used_bidi_streams);
626 return 0;
627 }
628
cb_extend_max_stream_data(ngtcp2_conn * tconn,int64_t sid,uint64_t max_data,void * user_data,void * stream_user_data)629 static int cb_extend_max_stream_data(ngtcp2_conn *tconn, int64_t sid,
630 uint64_t max_data, void *user_data,
631 void *stream_user_data)
632 {
633 struct Curl_cfilter *cf = user_data;
634 struct cf_ngtcp2_ctx *ctx = cf->ctx;
635 curl_int64_t stream_id = (curl_int64_t)sid;
636 struct Curl_easy *data = CF_DATA_CURRENT(cf);
637 struct Curl_easy *s_data;
638 struct h3_stream_ctx *stream;
639 int rv;
640 (void)tconn;
641 (void)max_data;
642 (void)stream_user_data;
643
644 rv = nghttp3_conn_unblock_stream(ctx->h3conn, stream_id);
645 if(rv && rv != NGHTTP3_ERR_STREAM_NOT_FOUND) {
646 return NGTCP2_ERR_CALLBACK_FAILURE;
647 }
648 s_data = get_stream_easy(cf, data, stream_id, &stream);
649 if(s_data && stream && stream->quic_flow_blocked) {
650 CURL_TRC_CF(s_data, cf, "[%" CURL_PRId64 "] unblock quic flow",
651 stream_id);
652 stream->quic_flow_blocked = FALSE;
653 h3_drain_stream(cf, s_data);
654 }
655 return 0;
656 }
657
cb_rand(uint8_t * dest,size_t destlen,const ngtcp2_rand_ctx * rand_ctx)658 static void cb_rand(uint8_t *dest, size_t destlen,
659 const ngtcp2_rand_ctx *rand_ctx)
660 {
661 CURLcode result;
662 (void)rand_ctx;
663
664 result = Curl_rand(NULL, dest, destlen);
665 if(result) {
666 /* cb_rand is only used for non-cryptographic context. If Curl_rand
667 failed, just fill 0 and call it *random*. */
668 memset(dest, 0, destlen);
669 }
670 }
671
cb_get_new_connection_id(ngtcp2_conn * tconn,ngtcp2_cid * cid,uint8_t * token,size_t cidlen,void * user_data)672 static int cb_get_new_connection_id(ngtcp2_conn *tconn, ngtcp2_cid *cid,
673 uint8_t *token, size_t cidlen,
674 void *user_data)
675 {
676 CURLcode result;
677 (void)tconn;
678 (void)user_data;
679
680 result = Curl_rand(NULL, cid->data, cidlen);
681 if(result)
682 return NGTCP2_ERR_CALLBACK_FAILURE;
683 cid->datalen = cidlen;
684
685 result = Curl_rand(NULL, token, NGTCP2_STATELESS_RESET_TOKENLEN);
686 if(result)
687 return NGTCP2_ERR_CALLBACK_FAILURE;
688
689 return 0;
690 }
691
cb_recv_rx_key(ngtcp2_conn * tconn,ngtcp2_encryption_level level,void * user_data)692 static int cb_recv_rx_key(ngtcp2_conn *tconn, ngtcp2_encryption_level level,
693 void *user_data)
694 {
695 struct Curl_cfilter *cf = user_data;
696 (void)tconn;
697
698 if(level != NGTCP2_ENCRYPTION_LEVEL_1RTT) {
699 return 0;
700 }
701
702 if(init_ngh3_conn(cf) != CURLE_OK) {
703 return NGTCP2_ERR_CALLBACK_FAILURE;
704 }
705
706 return 0;
707 }
708
709 static ngtcp2_callbacks ng_callbacks = {
710 ngtcp2_crypto_client_initial_cb,
711 NULL, /* recv_client_initial */
712 ngtcp2_crypto_recv_crypto_data_cb,
713 cb_handshake_completed,
714 NULL, /* recv_version_negotiation */
715 ngtcp2_crypto_encrypt_cb,
716 ngtcp2_crypto_decrypt_cb,
717 ngtcp2_crypto_hp_mask_cb,
718 cb_recv_stream_data,
719 cb_acked_stream_data_offset,
720 NULL, /* stream_open */
721 cb_stream_close,
722 NULL, /* recv_stateless_reset */
723 ngtcp2_crypto_recv_retry_cb,
724 cb_extend_max_local_streams_bidi,
725 NULL, /* extend_max_local_streams_uni */
726 cb_rand,
727 cb_get_new_connection_id,
728 NULL, /* remove_connection_id */
729 ngtcp2_crypto_update_key_cb, /* update_key */
730 NULL, /* path_validation */
731 NULL, /* select_preferred_addr */
732 cb_stream_reset,
733 NULL, /* extend_max_remote_streams_bidi */
734 NULL, /* extend_max_remote_streams_uni */
735 cb_extend_max_stream_data,
736 NULL, /* dcid_status */
737 NULL, /* handshake_confirmed */
738 NULL, /* recv_new_token */
739 ngtcp2_crypto_delete_crypto_aead_ctx_cb,
740 ngtcp2_crypto_delete_crypto_cipher_ctx_cb,
741 NULL, /* recv_datagram */
742 NULL, /* ack_datagram */
743 NULL, /* lost_datagram */
744 ngtcp2_crypto_get_path_challenge_data_cb,
745 cb_stream_stop_sending,
746 NULL, /* version_negotiation */
747 cb_recv_rx_key,
748 NULL, /* recv_tx_key */
749 NULL, /* early_data_rejected */
750 };
751
752 /**
753 * Connection maintenance like timeouts on packet ACKs etc. are done by us, not
754 * the OS like for TCP. POLL events on the socket therefore are not
755 * sufficient.
756 * ngtcp2 tells us when it wants to be invoked again. We handle that via
757 * the `Curl_expire()` mechanisms.
758 */
check_and_set_expiry(struct Curl_cfilter * cf,struct Curl_easy * data,struct pkt_io_ctx * pktx)759 static CURLcode check_and_set_expiry(struct Curl_cfilter *cf,
760 struct Curl_easy *data,
761 struct pkt_io_ctx *pktx)
762 {
763 struct cf_ngtcp2_ctx *ctx = cf->ctx;
764 struct pkt_io_ctx local_pktx;
765 ngtcp2_tstamp expiry;
766
767 if(!pktx) {
768 pktx_init(&local_pktx, cf, data);
769 pktx = &local_pktx;
770 }
771 else {
772 pktx_update_time(pktx, cf);
773 }
774
775 expiry = ngtcp2_conn_get_expiry(ctx->qconn);
776 if(expiry != UINT64_MAX) {
777 if(expiry <= pktx->ts) {
778 CURLcode result;
779 int rv = ngtcp2_conn_handle_expiry(ctx->qconn, pktx->ts);
780 if(rv) {
781 failf(data, "ngtcp2_conn_handle_expiry returned error: %s",
782 ngtcp2_strerror(rv));
783 cf_ngtcp2_err_set(cf, data, rv);
784 return CURLE_SEND_ERROR;
785 }
786 result = cf_progress_ingress(cf, data, pktx);
787 if(result)
788 return result;
789 result = cf_progress_egress(cf, data, pktx);
790 if(result)
791 return result;
792 /* ask again, things might have changed */
793 expiry = ngtcp2_conn_get_expiry(ctx->qconn);
794 }
795
796 if(expiry > pktx->ts) {
797 ngtcp2_duration timeout = expiry - pktx->ts;
798 if(timeout % NGTCP2_MILLISECONDS) {
799 timeout += NGTCP2_MILLISECONDS;
800 }
801 Curl_expire(data, timeout / NGTCP2_MILLISECONDS, EXPIRE_QUIC);
802 }
803 }
804 return CURLE_OK;
805 }
806
cf_ngtcp2_adjust_pollset(struct Curl_cfilter * cf,struct Curl_easy * data,struct easy_pollset * ps)807 static void cf_ngtcp2_adjust_pollset(struct Curl_cfilter *cf,
808 struct Curl_easy *data,
809 struct easy_pollset *ps)
810 {
811 struct cf_ngtcp2_ctx *ctx = cf->ctx;
812 bool want_recv, want_send;
813
814 if(!ctx->qconn)
815 return;
816
817 Curl_pollset_check(data, ps, ctx->q.sockfd, &want_recv, &want_send);
818 if(want_recv || want_send) {
819 struct h3_stream_ctx *stream = H3_STREAM_CTX(ctx, data);
820 struct cf_call_data save;
821 bool c_exhaust, s_exhaust;
822
823 CF_DATA_SAVE(save, cf, data);
824 c_exhaust = want_send && (!ngtcp2_conn_get_cwnd_left(ctx->qconn) ||
825 !ngtcp2_conn_get_max_data_left(ctx->qconn));
826 s_exhaust = want_send && stream && stream->id >= 0 &&
827 stream->quic_flow_blocked;
828 want_recv = (want_recv || c_exhaust || s_exhaust);
829 want_send = (!s_exhaust && want_send) ||
830 !Curl_bufq_is_empty(&ctx->q.sendbuf);
831
832 Curl_pollset_set(data, ps, ctx->q.sockfd, want_recv, want_send);
833 CF_DATA_RESTORE(cf, save);
834 }
835 }
836
cb_h3_stream_close(nghttp3_conn * conn,int64_t sid,uint64_t app_error_code,void * user_data,void * stream_user_data)837 static int cb_h3_stream_close(nghttp3_conn *conn, int64_t sid,
838 uint64_t app_error_code, void *user_data,
839 void *stream_user_data)
840 {
841 struct Curl_cfilter *cf = user_data;
842 struct cf_ngtcp2_ctx *ctx = cf->ctx;
843 struct Curl_easy *data = stream_user_data;
844 curl_int64_t stream_id = (curl_int64_t)sid;
845 struct h3_stream_ctx *stream = H3_STREAM_CTX(ctx, data);
846 (void)conn;
847 (void)stream_id;
848
849 /* we might be called by nghttp3 after we already cleaned up */
850 if(!stream)
851 return 0;
852
853 stream->closed = TRUE;
854 stream->error3 = (curl_uint64_t)app_error_code;
855 if(stream->error3 != NGHTTP3_H3_NO_ERROR) {
856 stream->reset = TRUE;
857 stream->send_closed = TRUE;
858 CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] RESET: error %" CURL_PRIu64,
859 stream->id, stream->error3);
860 }
861 else {
862 CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] CLOSED", stream->id);
863 }
864 h3_drain_stream(cf, data);
865 return 0;
866 }
867
h3_xfer_write_resp_hd(struct Curl_cfilter * cf,struct Curl_easy * data,struct h3_stream_ctx * stream,const char * buf,size_t blen,bool eos)868 static void h3_xfer_write_resp_hd(struct Curl_cfilter *cf,
869 struct Curl_easy *data,
870 struct h3_stream_ctx *stream,
871 const char *buf, size_t blen, bool eos)
872 {
873
874 /* If we already encountered an error, skip further writes */
875 if(!stream->xfer_result) {
876 stream->xfer_result = Curl_xfer_write_resp_hd(data, buf, blen, eos);
877 if(stream->xfer_result)
878 CURL_TRC_CF(data, cf, "[%"CURL_PRId64"] error %d writing %zu "
879 "bytes of headers", stream->id, stream->xfer_result, blen);
880 }
881 }
882
h3_xfer_write_resp(struct Curl_cfilter * cf,struct Curl_easy * data,struct h3_stream_ctx * stream,const char * buf,size_t blen,bool eos)883 static void h3_xfer_write_resp(struct Curl_cfilter *cf,
884 struct Curl_easy *data,
885 struct h3_stream_ctx *stream,
886 const char *buf, size_t blen, bool eos)
887 {
888
889 /* If we already encountered an error, skip further writes */
890 if(!stream->xfer_result) {
891 stream->xfer_result = Curl_xfer_write_resp(data, buf, blen, eos);
892 /* If the transfer write is errored, we do not want any more data */
893 if(stream->xfer_result) {
894 CURL_TRC_CF(data, cf, "[%"CURL_PRId64"] error %d writing %zu bytes "
895 "of data", stream->id, stream->xfer_result, blen);
896 }
897 }
898 }
899
cb_h3_recv_data(nghttp3_conn * conn,int64_t stream3_id,const uint8_t * buf,size_t blen,void * user_data,void * stream_user_data)900 static int cb_h3_recv_data(nghttp3_conn *conn, int64_t stream3_id,
901 const uint8_t *buf, size_t blen,
902 void *user_data, void *stream_user_data)
903 {
904 struct Curl_cfilter *cf = user_data;
905 struct cf_ngtcp2_ctx *ctx = cf->ctx;
906 struct Curl_easy *data = stream_user_data;
907 struct h3_stream_ctx *stream = H3_STREAM_CTX(ctx, data);
908
909 (void)conn;
910 (void)stream3_id;
911
912 if(!stream)
913 return NGHTTP3_ERR_CALLBACK_FAILURE;
914
915 h3_xfer_write_resp(cf, data, stream, (char *)buf, blen, FALSE);
916 if(blen) {
917 CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] ACK %zu bytes of DATA",
918 stream->id, blen);
919 ngtcp2_conn_extend_max_stream_offset(ctx->qconn, stream->id, blen);
920 ngtcp2_conn_extend_max_offset(ctx->qconn, blen);
921 }
922 CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] DATA len=%zu", stream->id, blen);
923 return 0;
924 }
925
cb_h3_deferred_consume(nghttp3_conn * conn,int64_t stream3_id,size_t consumed,void * user_data,void * stream_user_data)926 static int cb_h3_deferred_consume(nghttp3_conn *conn, int64_t stream3_id,
927 size_t consumed, void *user_data,
928 void *stream_user_data)
929 {
930 struct Curl_cfilter *cf = user_data;
931 struct cf_ngtcp2_ctx *ctx = cf->ctx;
932 (void)conn;
933 (void)stream_user_data;
934
935 /* nghttp3 has consumed bytes on the QUIC stream and we need to
936 * tell the QUIC connection to increase its flow control */
937 ngtcp2_conn_extend_max_stream_offset(ctx->qconn, stream3_id, consumed);
938 ngtcp2_conn_extend_max_offset(ctx->qconn, consumed);
939 return 0;
940 }
941
cb_h3_end_headers(nghttp3_conn * conn,int64_t sid,int fin,void * user_data,void * stream_user_data)942 static int cb_h3_end_headers(nghttp3_conn *conn, int64_t sid,
943 int fin, void *user_data, void *stream_user_data)
944 {
945 struct Curl_cfilter *cf = user_data;
946 struct cf_ngtcp2_ctx *ctx = cf->ctx;
947 struct Curl_easy *data = stream_user_data;
948 curl_int64_t stream_id = (curl_int64_t)sid;
949 struct h3_stream_ctx *stream = H3_STREAM_CTX(ctx, data);
950 (void)conn;
951 (void)stream_id;
952 (void)fin;
953 (void)cf;
954
955 if(!stream)
956 return 0;
957 /* add a CRLF only if we've received some headers */
958 h3_xfer_write_resp_hd(cf, data, stream, STRCONST("\r\n"), stream->closed);
959
960 CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] end_headers, status=%d",
961 stream_id, stream->status_code);
962 if(stream->status_code / 100 != 1) {
963 stream->resp_hds_complete = TRUE;
964 }
965 h3_drain_stream(cf, data);
966 return 0;
967 }
968
cb_h3_recv_header(nghttp3_conn * conn,int64_t sid,int32_t token,nghttp3_rcbuf * name,nghttp3_rcbuf * value,uint8_t flags,void * user_data,void * stream_user_data)969 static int cb_h3_recv_header(nghttp3_conn *conn, int64_t sid,
970 int32_t token, nghttp3_rcbuf *name,
971 nghttp3_rcbuf *value, uint8_t flags,
972 void *user_data, void *stream_user_data)
973 {
974 struct Curl_cfilter *cf = user_data;
975 struct cf_ngtcp2_ctx *ctx = cf->ctx;
976 curl_int64_t stream_id = (curl_int64_t)sid;
977 nghttp3_vec h3name = nghttp3_rcbuf_get_buf(name);
978 nghttp3_vec h3val = nghttp3_rcbuf_get_buf(value);
979 struct Curl_easy *data = stream_user_data;
980 struct h3_stream_ctx *stream = H3_STREAM_CTX(ctx, data);
981 CURLcode result = CURLE_OK;
982 (void)conn;
983 (void)stream_id;
984 (void)token;
985 (void)flags;
986 (void)cf;
987
988 /* we might have cleaned up this transfer already */
989 if(!stream)
990 return 0;
991
992 if(token == NGHTTP3_QPACK_TOKEN__STATUS) {
993
994 result = Curl_http_decode_status(&stream->status_code,
995 (const char *)h3val.base, h3val.len);
996 if(result)
997 return -1;
998 Curl_dyn_reset(&ctx->scratch);
999 result = Curl_dyn_addn(&ctx->scratch, STRCONST("HTTP/3 "));
1000 if(!result)
1001 result = Curl_dyn_addn(&ctx->scratch,
1002 (const char *)h3val.base, h3val.len);
1003 if(!result)
1004 result = Curl_dyn_addn(&ctx->scratch, STRCONST(" \r\n"));
1005 if(!result)
1006 h3_xfer_write_resp_hd(cf, data, stream, Curl_dyn_ptr(&ctx->scratch),
1007 Curl_dyn_len(&ctx->scratch), FALSE);
1008 CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] status: %s",
1009 stream_id, Curl_dyn_ptr(&ctx->scratch));
1010 if(result) {
1011 return -1;
1012 }
1013 }
1014 else {
1015 /* store as an HTTP1-style header */
1016 CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] header: %.*s: %.*s",
1017 stream_id, (int)h3name.len, h3name.base,
1018 (int)h3val.len, h3val.base);
1019 Curl_dyn_reset(&ctx->scratch);
1020 result = Curl_dyn_addn(&ctx->scratch,
1021 (const char *)h3name.base, h3name.len);
1022 if(!result)
1023 result = Curl_dyn_addn(&ctx->scratch, STRCONST(": "));
1024 if(!result)
1025 result = Curl_dyn_addn(&ctx->scratch,
1026 (const char *)h3val.base, h3val.len);
1027 if(!result)
1028 result = Curl_dyn_addn(&ctx->scratch, STRCONST("\r\n"));
1029 if(!result)
1030 h3_xfer_write_resp_hd(cf, data, stream, Curl_dyn_ptr(&ctx->scratch),
1031 Curl_dyn_len(&ctx->scratch), FALSE);
1032 }
1033 return 0;
1034 }
1035
cb_h3_stop_sending(nghttp3_conn * conn,int64_t stream_id,uint64_t app_error_code,void * user_data,void * stream_user_data)1036 static int cb_h3_stop_sending(nghttp3_conn *conn, int64_t stream_id,
1037 uint64_t app_error_code, void *user_data,
1038 void *stream_user_data)
1039 {
1040 struct Curl_cfilter *cf = user_data;
1041 struct cf_ngtcp2_ctx *ctx = cf->ctx;
1042 int rv;
1043 (void)conn;
1044 (void)stream_user_data;
1045
1046 rv = ngtcp2_conn_shutdown_stream_read(ctx->qconn, 0, stream_id,
1047 app_error_code);
1048 if(rv && rv != NGTCP2_ERR_STREAM_NOT_FOUND) {
1049 return NGTCP2_ERR_CALLBACK_FAILURE;
1050 }
1051
1052 return 0;
1053 }
1054
cb_h3_reset_stream(nghttp3_conn * conn,int64_t sid,uint64_t app_error_code,void * user_data,void * stream_user_data)1055 static int cb_h3_reset_stream(nghttp3_conn *conn, int64_t sid,
1056 uint64_t app_error_code, void *user_data,
1057 void *stream_user_data) {
1058 struct Curl_cfilter *cf = user_data;
1059 struct cf_ngtcp2_ctx *ctx = cf->ctx;
1060 curl_int64_t stream_id = (curl_int64_t)sid;
1061 struct Curl_easy *data = stream_user_data;
1062 int rv;
1063 (void)conn;
1064 (void)data;
1065
1066 rv = ngtcp2_conn_shutdown_stream_write(ctx->qconn, 0, stream_id,
1067 app_error_code);
1068 CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] reset -> %d", stream_id, rv);
1069 if(rv && rv != NGTCP2_ERR_STREAM_NOT_FOUND) {
1070 return NGTCP2_ERR_CALLBACK_FAILURE;
1071 }
1072
1073 return 0;
1074 }
1075
1076 static nghttp3_callbacks ngh3_callbacks = {
1077 cb_h3_acked_req_body, /* acked_stream_data */
1078 cb_h3_stream_close,
1079 cb_h3_recv_data,
1080 cb_h3_deferred_consume,
1081 NULL, /* begin_headers */
1082 cb_h3_recv_header,
1083 cb_h3_end_headers,
1084 NULL, /* begin_trailers */
1085 cb_h3_recv_header,
1086 NULL, /* end_trailers */
1087 cb_h3_stop_sending,
1088 NULL, /* end_stream */
1089 cb_h3_reset_stream,
1090 NULL, /* shutdown */
1091 NULL /* recv_settings */
1092 };
1093
init_ngh3_conn(struct Curl_cfilter * cf)1094 static int init_ngh3_conn(struct Curl_cfilter *cf)
1095 {
1096 struct cf_ngtcp2_ctx *ctx = cf->ctx;
1097 CURLcode result;
1098 int rc;
1099 int64_t ctrl_stream_id, qpack_enc_stream_id, qpack_dec_stream_id;
1100
1101 if(ngtcp2_conn_get_streams_uni_left(ctx->qconn) < 3) {
1102 return CURLE_QUIC_CONNECT_ERROR;
1103 }
1104
1105 nghttp3_settings_default(&ctx->h3settings);
1106
1107 rc = nghttp3_conn_client_new(&ctx->h3conn,
1108 &ngh3_callbacks,
1109 &ctx->h3settings,
1110 nghttp3_mem_default(),
1111 cf);
1112 if(rc) {
1113 result = CURLE_OUT_OF_MEMORY;
1114 goto fail;
1115 }
1116
1117 rc = ngtcp2_conn_open_uni_stream(ctx->qconn, &ctrl_stream_id, NULL);
1118 if(rc) {
1119 result = CURLE_QUIC_CONNECT_ERROR;
1120 goto fail;
1121 }
1122
1123 rc = nghttp3_conn_bind_control_stream(ctx->h3conn, ctrl_stream_id);
1124 if(rc) {
1125 result = CURLE_QUIC_CONNECT_ERROR;
1126 goto fail;
1127 }
1128
1129 rc = ngtcp2_conn_open_uni_stream(ctx->qconn, &qpack_enc_stream_id, NULL);
1130 if(rc) {
1131 result = CURLE_QUIC_CONNECT_ERROR;
1132 goto fail;
1133 }
1134
1135 rc = ngtcp2_conn_open_uni_stream(ctx->qconn, &qpack_dec_stream_id, NULL);
1136 if(rc) {
1137 result = CURLE_QUIC_CONNECT_ERROR;
1138 goto fail;
1139 }
1140
1141 rc = nghttp3_conn_bind_qpack_streams(ctx->h3conn, qpack_enc_stream_id,
1142 qpack_dec_stream_id);
1143 if(rc) {
1144 result = CURLE_QUIC_CONNECT_ERROR;
1145 goto fail;
1146 }
1147
1148 return CURLE_OK;
1149 fail:
1150
1151 return result;
1152 }
1153
recv_closed_stream(struct Curl_cfilter * cf,struct Curl_easy * data,struct h3_stream_ctx * stream,CURLcode * err)1154 static ssize_t recv_closed_stream(struct Curl_cfilter *cf,
1155 struct Curl_easy *data,
1156 struct h3_stream_ctx *stream,
1157 CURLcode *err)
1158 {
1159 ssize_t nread = -1;
1160
1161 (void)cf;
1162 if(stream->reset) {
1163 failf(data,
1164 "HTTP/3 stream %" CURL_PRId64 " reset by server", stream->id);
1165 *err = data->req.bytecount? CURLE_PARTIAL_FILE : CURLE_HTTP3;
1166 goto out;
1167 }
1168 else if(!stream->resp_hds_complete) {
1169 failf(data,
1170 "HTTP/3 stream %" CURL_PRId64 " was closed cleanly, but before "
1171 "getting all response header fields, treated as error",
1172 stream->id);
1173 *err = CURLE_HTTP3;
1174 goto out;
1175 }
1176 *err = CURLE_OK;
1177 nread = 0;
1178
1179 out:
1180 return nread;
1181 }
1182
1183 /* incoming data frames on the h3 stream */
cf_ngtcp2_recv(struct Curl_cfilter * cf,struct Curl_easy * data,char * buf,size_t blen,CURLcode * err)1184 static ssize_t cf_ngtcp2_recv(struct Curl_cfilter *cf, struct Curl_easy *data,
1185 char *buf, size_t blen, CURLcode *err)
1186 {
1187 struct cf_ngtcp2_ctx *ctx = cf->ctx;
1188 struct h3_stream_ctx *stream = H3_STREAM_CTX(ctx, data);
1189 ssize_t nread = -1;
1190 struct cf_call_data save;
1191 struct pkt_io_ctx pktx;
1192
1193 (void)ctx;
1194 (void)buf;
1195
1196 CF_DATA_SAVE(save, cf, data);
1197 DEBUGASSERT(cf->connected);
1198 DEBUGASSERT(ctx);
1199 DEBUGASSERT(ctx->qconn);
1200 DEBUGASSERT(ctx->h3conn);
1201 *err = CURLE_OK;
1202
1203 pktx_init(&pktx, cf, data);
1204
1205 if(!stream || ctx->conn_closed) {
1206 *err = CURLE_RECV_ERROR;
1207 goto out;
1208 }
1209
1210 if(cf_progress_ingress(cf, data, &pktx)) {
1211 *err = CURLE_RECV_ERROR;
1212 nread = -1;
1213 goto out;
1214 }
1215
1216 if(stream->xfer_result) {
1217 CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] xfer write failed", stream->id);
1218 cf_ngtcp2_stream_close(cf, data, stream);
1219 *err = stream->xfer_result;
1220 nread = -1;
1221 goto out;
1222 }
1223 else if(stream->closed) {
1224 nread = recv_closed_stream(cf, data, stream, err);
1225 goto out;
1226 }
1227 *err = CURLE_AGAIN;
1228 nread = -1;
1229
1230 out:
1231 if(cf_progress_egress(cf, data, &pktx)) {
1232 *err = CURLE_SEND_ERROR;
1233 nread = -1;
1234 }
1235 else {
1236 CURLcode result2 = check_and_set_expiry(cf, data, &pktx);
1237 if(result2) {
1238 *err = result2;
1239 nread = -1;
1240 }
1241 }
1242 CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] cf_recv(blen=%zu) -> %zd, %d",
1243 stream? stream->id : -1, blen, nread, *err);
1244 CF_DATA_RESTORE(cf, save);
1245 return nread;
1246 }
1247
cb_h3_acked_req_body(nghttp3_conn * conn,int64_t stream_id,uint64_t datalen,void * user_data,void * stream_user_data)1248 static int cb_h3_acked_req_body(nghttp3_conn *conn, int64_t stream_id,
1249 uint64_t datalen, void *user_data,
1250 void *stream_user_data)
1251 {
1252 struct Curl_cfilter *cf = user_data;
1253 struct cf_ngtcp2_ctx *ctx = cf->ctx;
1254 struct Curl_easy *data = stream_user_data;
1255 struct h3_stream_ctx *stream = H3_STREAM_CTX(ctx, data);
1256 size_t skiplen;
1257
1258 (void)cf;
1259 if(!stream)
1260 return 0;
1261 /* The server acknowledged `datalen` of bytes from our request body.
1262 * This is a delta. We have kept this data in `sendbuf` for
1263 * re-transmissions and can free it now. */
1264 if(datalen >= (uint64_t)stream->sendbuf_len_in_flight)
1265 skiplen = stream->sendbuf_len_in_flight;
1266 else
1267 skiplen = (size_t)datalen;
1268 Curl_bufq_skip(&stream->sendbuf, skiplen);
1269 stream->sendbuf_len_in_flight -= skiplen;
1270
1271 /* Everything ACKed, we resume upload processing */
1272 if(!stream->sendbuf_len_in_flight) {
1273 int rv = nghttp3_conn_resume_stream(conn, stream_id);
1274 if(rv && rv != NGHTTP3_ERR_STREAM_NOT_FOUND) {
1275 return NGTCP2_ERR_CALLBACK_FAILURE;
1276 }
1277 }
1278 return 0;
1279 }
1280
1281 static nghttp3_ssize
cb_h3_read_req_body(nghttp3_conn * conn,int64_t stream_id,nghttp3_vec * vec,size_t veccnt,uint32_t * pflags,void * user_data,void * stream_user_data)1282 cb_h3_read_req_body(nghttp3_conn *conn, int64_t stream_id,
1283 nghttp3_vec *vec, size_t veccnt,
1284 uint32_t *pflags, void *user_data,
1285 void *stream_user_data)
1286 {
1287 struct Curl_cfilter *cf = user_data;
1288 struct cf_ngtcp2_ctx *ctx = cf->ctx;
1289 struct Curl_easy *data = stream_user_data;
1290 struct h3_stream_ctx *stream = H3_STREAM_CTX(ctx, data);
1291 ssize_t nwritten = 0;
1292 size_t nvecs = 0;
1293 (void)cf;
1294 (void)conn;
1295 (void)stream_id;
1296 (void)user_data;
1297 (void)veccnt;
1298
1299 if(!stream)
1300 return NGHTTP3_ERR_CALLBACK_FAILURE;
1301 /* nghttp3 keeps references to the sendbuf data until it is ACKed
1302 * by the server (see `cb_h3_acked_req_body()` for updates).
1303 * `sendbuf_len_in_flight` is the amount of bytes in `sendbuf`
1304 * that we have already passed to nghttp3, but which have not been
1305 * ACKed yet.
1306 * Any amount beyond `sendbuf_len_in_flight` we need still to pass
1307 * to nghttp3. Do that now, if we can. */
1308 if(stream->sendbuf_len_in_flight < Curl_bufq_len(&stream->sendbuf)) {
1309 nvecs = 0;
1310 while(nvecs < veccnt &&
1311 Curl_bufq_peek_at(&stream->sendbuf,
1312 stream->sendbuf_len_in_flight,
1313 (const unsigned char **)&vec[nvecs].base,
1314 &vec[nvecs].len)) {
1315 stream->sendbuf_len_in_flight += vec[nvecs].len;
1316 nwritten += vec[nvecs].len;
1317 ++nvecs;
1318 }
1319 DEBUGASSERT(nvecs > 0); /* we SHOULD have been be able to peek */
1320 }
1321
1322 if(nwritten > 0 && stream->upload_left != -1)
1323 stream->upload_left -= nwritten;
1324
1325 /* When we stopped sending and everything in `sendbuf` is "in flight",
1326 * we are at the end of the request body. */
1327 if(stream->upload_left == 0) {
1328 *pflags = NGHTTP3_DATA_FLAG_EOF;
1329 stream->send_closed = TRUE;
1330 }
1331 else if(!nwritten) {
1332 /* Not EOF, and nothing to give, we signal WOULDBLOCK. */
1333 CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] read req body -> AGAIN",
1334 stream->id);
1335 return NGHTTP3_ERR_WOULDBLOCK;
1336 }
1337
1338 CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] read req body -> "
1339 "%d vecs%s with %zu (buffered=%zu, left=%"
1340 CURL_FORMAT_CURL_OFF_T ")",
1341 stream->id, (int)nvecs,
1342 *pflags == NGHTTP3_DATA_FLAG_EOF?" EOF":"",
1343 nwritten, Curl_bufq_len(&stream->sendbuf),
1344 stream->upload_left);
1345 return (nghttp3_ssize)nvecs;
1346 }
1347
1348 /* Index where :authority header field will appear in request header
1349 field list. */
1350 #define AUTHORITY_DST_IDX 3
1351
h3_stream_open(struct Curl_cfilter * cf,struct Curl_easy * data,const void * buf,size_t len,CURLcode * err)1352 static ssize_t h3_stream_open(struct Curl_cfilter *cf,
1353 struct Curl_easy *data,
1354 const void *buf, size_t len,
1355 CURLcode *err)
1356 {
1357 struct cf_ngtcp2_ctx *ctx = cf->ctx;
1358 struct h3_stream_ctx *stream = NULL;
1359 int64_t sid;
1360 struct dynhds h2_headers;
1361 size_t nheader;
1362 nghttp3_nv *nva = NULL;
1363 int rc = 0;
1364 unsigned int i;
1365 ssize_t nwritten = -1;
1366 nghttp3_data_reader reader;
1367 nghttp3_data_reader *preader = NULL;
1368
1369 Curl_dynhds_init(&h2_headers, 0, DYN_HTTP_REQUEST);
1370
1371 *err = h3_data_setup(cf, data);
1372 if(*err)
1373 goto out;
1374 stream = H3_STREAM_CTX(ctx, data);
1375 DEBUGASSERT(stream);
1376 if(!stream) {
1377 *err = CURLE_FAILED_INIT;
1378 goto out;
1379 }
1380
1381 nwritten = Curl_h1_req_parse_read(&stream->h1, buf, len, NULL, 0, err);
1382 if(nwritten < 0)
1383 goto out;
1384 if(!stream->h1.done) {
1385 /* need more data */
1386 goto out;
1387 }
1388 DEBUGASSERT(stream->h1.req);
1389
1390 *err = Curl_http_req_to_h2(&h2_headers, stream->h1.req, data);
1391 if(*err) {
1392 nwritten = -1;
1393 goto out;
1394 }
1395 /* no longer needed */
1396 Curl_h1_req_parse_free(&stream->h1);
1397
1398 nheader = Curl_dynhds_count(&h2_headers);
1399 nva = malloc(sizeof(nghttp3_nv) * nheader);
1400 if(!nva) {
1401 *err = CURLE_OUT_OF_MEMORY;
1402 nwritten = -1;
1403 goto out;
1404 }
1405
1406 for(i = 0; i < nheader; ++i) {
1407 struct dynhds_entry *e = Curl_dynhds_getn(&h2_headers, i);
1408 nva[i].name = (unsigned char *)e->name;
1409 nva[i].namelen = e->namelen;
1410 nva[i].value = (unsigned char *)e->value;
1411 nva[i].valuelen = e->valuelen;
1412 nva[i].flags = NGHTTP3_NV_FLAG_NONE;
1413 }
1414
1415 rc = ngtcp2_conn_open_bidi_stream(ctx->qconn, &sid, data);
1416 if(rc) {
1417 failf(data, "can get bidi streams");
1418 *err = CURLE_SEND_ERROR;
1419 nwritten = -1;
1420 goto out;
1421 }
1422 stream->id = (curl_int64_t)sid;
1423 ++ctx->used_bidi_streams;
1424
1425 switch(data->state.httpreq) {
1426 case HTTPREQ_POST:
1427 case HTTPREQ_POST_FORM:
1428 case HTTPREQ_POST_MIME:
1429 case HTTPREQ_PUT:
1430 /* known request body size or -1 */
1431 if(data->state.infilesize != -1)
1432 stream->upload_left = data->state.infilesize;
1433 else
1434 /* data sending without specifying the data amount up front */
1435 stream->upload_left = -1; /* unknown */
1436 break;
1437 default:
1438 /* there is not request body */
1439 stream->upload_left = 0; /* no request body */
1440 break;
1441 }
1442
1443 stream->send_closed = (stream->upload_left == 0);
1444 if(!stream->send_closed) {
1445 reader.read_data = cb_h3_read_req_body;
1446 preader = &reader;
1447 }
1448
1449 rc = nghttp3_conn_submit_request(ctx->h3conn, stream->id,
1450 nva, nheader, preader, data);
1451 if(rc) {
1452 switch(rc) {
1453 case NGHTTP3_ERR_CONN_CLOSING:
1454 CURL_TRC_CF(data, cf, "h3sid[%" CURL_PRId64 "] failed to send, "
1455 "connection is closing", stream->id);
1456 break;
1457 default:
1458 CURL_TRC_CF(data, cf, "h3sid[%" CURL_PRId64 "] failed to send -> "
1459 "%d (%s)", stream->id, rc, ngtcp2_strerror(rc));
1460 break;
1461 }
1462 *err = CURLE_SEND_ERROR;
1463 nwritten = -1;
1464 goto out;
1465 }
1466
1467 if(Curl_trc_is_verbose(data)) {
1468 infof(data, "[HTTP/3] [%" CURL_PRId64 "] OPENED stream for %s",
1469 stream->id, data->state.url);
1470 for(i = 0; i < nheader; ++i) {
1471 infof(data, "[HTTP/3] [%" CURL_PRId64 "] [%.*s: %.*s]", stream->id,
1472 (int)nva[i].namelen, nva[i].name,
1473 (int)nva[i].valuelen, nva[i].value);
1474 }
1475 }
1476
1477 out:
1478 free(nva);
1479 Curl_dynhds_free(&h2_headers);
1480 return nwritten;
1481 }
1482
cf_ngtcp2_send(struct Curl_cfilter * cf,struct Curl_easy * data,const void * buf,size_t len,CURLcode * err)1483 static ssize_t cf_ngtcp2_send(struct Curl_cfilter *cf, struct Curl_easy *data,
1484 const void *buf, size_t len, CURLcode *err)
1485 {
1486 struct cf_ngtcp2_ctx *ctx = cf->ctx;
1487 struct h3_stream_ctx *stream = H3_STREAM_CTX(ctx, data);
1488 ssize_t sent = 0;
1489 struct cf_call_data save;
1490 struct pkt_io_ctx pktx;
1491 CURLcode result;
1492
1493 CF_DATA_SAVE(save, cf, data);
1494 DEBUGASSERT(cf->connected);
1495 DEBUGASSERT(ctx->qconn);
1496 DEBUGASSERT(ctx->h3conn);
1497 pktx_init(&pktx, cf, data);
1498 *err = CURLE_OK;
1499
1500 result = cf_progress_ingress(cf, data, &pktx);
1501 if(result) {
1502 *err = result;
1503 sent = -1;
1504 }
1505
1506 if(!stream || stream->id < 0) {
1507 if(ctx->conn_closed) {
1508 CURL_TRC_CF(data, cf, "cannot open stream on closed connection");
1509 *err = CURLE_SEND_ERROR;
1510 sent = -1;
1511 goto out;
1512 }
1513 sent = h3_stream_open(cf, data, buf, len, err);
1514 if(sent < 0) {
1515 CURL_TRC_CF(data, cf, "failed to open stream -> %d", *err);
1516 goto out;
1517 }
1518 stream = H3_STREAM_CTX(ctx, data);
1519 }
1520 else if(stream->xfer_result) {
1521 CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] xfer write failed", stream->id);
1522 cf_ngtcp2_stream_close(cf, data, stream);
1523 *err = stream->xfer_result;
1524 sent = -1;
1525 goto out;
1526 }
1527 else if(stream->upload_blocked_len) {
1528 /* the data in `buf` has already been submitted or added to the
1529 * buffers, but have been EAGAINed on the last invocation. */
1530 DEBUGASSERT(len >= stream->upload_blocked_len);
1531 if(len < stream->upload_blocked_len) {
1532 /* Did we get called again with a smaller `len`? This should not
1533 * happen. We are not prepared to handle that. */
1534 failf(data, "HTTP/3 send again with decreased length");
1535 *err = CURLE_HTTP3;
1536 sent = -1;
1537 goto out;
1538 }
1539 sent = (ssize_t)stream->upload_blocked_len;
1540 stream->upload_blocked_len = 0;
1541 }
1542 else if(stream->closed) {
1543 if(stream->resp_hds_complete) {
1544 /* Server decided to close the stream after having sent us a final
1545 * response. This is valid if it is not interested in the request
1546 * body. This happens on 30x or 40x responses.
1547 * We silently discard the data sent, since this is not a transport
1548 * error situation. */
1549 CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] discarding data"
1550 "on closed stream with response", stream->id);
1551 *err = CURLE_OK;
1552 sent = (ssize_t)len;
1553 goto out;
1554 }
1555 CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] send_body(len=%zu) "
1556 "-> stream closed", stream->id, len);
1557 *err = CURLE_HTTP3;
1558 sent = -1;
1559 goto out;
1560 }
1561 else if(ctx->conn_closed) {
1562 CURL_TRC_CF(data, cf, "cannot send on closed connection");
1563 *err = CURLE_SEND_ERROR;
1564 sent = -1;
1565 goto out;
1566 }
1567 else {
1568 sent = Curl_bufq_write(&stream->sendbuf, buf, len, err);
1569 CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] cf_send, add to "
1570 "sendbuf(len=%zu) -> %zd, %d",
1571 stream->id, len, sent, *err);
1572 if(sent < 0) {
1573 goto out;
1574 }
1575
1576 (void)nghttp3_conn_resume_stream(ctx->h3conn, stream->id);
1577 }
1578
1579 result = cf_progress_egress(cf, data, &pktx);
1580 if(result) {
1581 *err = result;
1582 sent = -1;
1583 }
1584
1585 if(stream && sent > 0 && stream->sendbuf_len_in_flight) {
1586 /* We have unacknowledged DATA and cannot report success to our
1587 * caller. Instead we EAGAIN and remember how much we have already
1588 * "written" into our various internal connection buffers. */
1589 stream->upload_blocked_len = sent;
1590 CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] cf_send(len=%zu), "
1591 "%zu bytes in flight -> EGAIN", stream->id, len,
1592 stream->sendbuf_len_in_flight);
1593 *err = CURLE_AGAIN;
1594 sent = -1;
1595 }
1596
1597 out:
1598 result = check_and_set_expiry(cf, data, &pktx);
1599 if(result) {
1600 *err = result;
1601 sent = -1;
1602 }
1603 CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] cf_send(len=%zu) -> %zd, %d",
1604 stream? stream->id : -1, len, sent, *err);
1605 CF_DATA_RESTORE(cf, save);
1606 return sent;
1607 }
1608
qng_verify_peer(struct Curl_cfilter * cf,struct Curl_easy * data)1609 static CURLcode qng_verify_peer(struct Curl_cfilter *cf,
1610 struct Curl_easy *data)
1611 {
1612 struct cf_ngtcp2_ctx *ctx = cf->ctx;
1613
1614 cf->conn->bits.multiplex = TRUE; /* at least potentially multiplexed */
1615 cf->conn->httpversion = 30;
1616 cf->conn->bundle->multiuse = BUNDLE_MULTIPLEX;
1617
1618 return Curl_vquic_tls_verify_peer(&ctx->tls, cf, data, &ctx->peer);
1619 }
1620
recv_pkt(const unsigned char * pkt,size_t pktlen,struct sockaddr_storage * remote_addr,socklen_t remote_addrlen,int ecn,void * userp)1621 static CURLcode recv_pkt(const unsigned char *pkt, size_t pktlen,
1622 struct sockaddr_storage *remote_addr,
1623 socklen_t remote_addrlen, int ecn,
1624 void *userp)
1625 {
1626 struct pkt_io_ctx *pktx = userp;
1627 struct cf_ngtcp2_ctx *ctx = pktx->cf->ctx;
1628 ngtcp2_pkt_info pi;
1629 ngtcp2_path path;
1630 int rv;
1631
1632 ++pktx->pkt_count;
1633 ngtcp2_addr_init(&path.local, (struct sockaddr *)&ctx->q.local_addr,
1634 ctx->q.local_addrlen);
1635 ngtcp2_addr_init(&path.remote, (struct sockaddr *)remote_addr,
1636 remote_addrlen);
1637 pi.ecn = (uint8_t)ecn;
1638
1639 rv = ngtcp2_conn_read_pkt(ctx->qconn, &path, &pi, pkt, pktlen, pktx->ts);
1640 if(rv) {
1641 CURL_TRC_CF(pktx->data, pktx->cf, "ingress, read_pkt -> %s (%d)",
1642 ngtcp2_strerror(rv), rv);
1643 cf_ngtcp2_err_set(pktx->cf, pktx->data, rv);
1644
1645 if(rv == NGTCP2_ERR_CRYPTO)
1646 /* this is a "TLS problem", but a failed certificate verification
1647 is a common reason for this */
1648 return CURLE_PEER_FAILED_VERIFICATION;
1649 return CURLE_RECV_ERROR;
1650 }
1651
1652 return CURLE_OK;
1653 }
1654
cf_progress_ingress(struct Curl_cfilter * cf,struct Curl_easy * data,struct pkt_io_ctx * pktx)1655 static CURLcode cf_progress_ingress(struct Curl_cfilter *cf,
1656 struct Curl_easy *data,
1657 struct pkt_io_ctx *pktx)
1658 {
1659 struct cf_ngtcp2_ctx *ctx = cf->ctx;
1660 struct pkt_io_ctx local_pktx;
1661 size_t pkts_chunk = 128, i;
1662 CURLcode result = CURLE_OK;
1663
1664 if(!pktx) {
1665 pktx_init(&local_pktx, cf, data);
1666 pktx = &local_pktx;
1667 }
1668 else {
1669 pktx_update_time(pktx, cf);
1670 }
1671
1672 result = Curl_vquic_tls_before_recv(&ctx->tls, cf, data);
1673 if(result)
1674 return result;
1675
1676 for(i = 0; i < 4; ++i) {
1677 if(i)
1678 pktx_update_time(pktx, cf);
1679 pktx->pkt_count = 0;
1680 result = vquic_recv_packets(cf, data, &ctx->q, pkts_chunk,
1681 recv_pkt, pktx);
1682 if(result || !pktx->pkt_count) /* error or got nothing */
1683 break;
1684 }
1685 return result;
1686 }
1687
1688 /**
1689 * Read a network packet to send from ngtcp2 into `buf`.
1690 * Return number of bytes written or -1 with *err set.
1691 */
read_pkt_to_send(void * userp,unsigned char * buf,size_t buflen,CURLcode * err)1692 static ssize_t read_pkt_to_send(void *userp,
1693 unsigned char *buf, size_t buflen,
1694 CURLcode *err)
1695 {
1696 struct pkt_io_ctx *x = userp;
1697 struct cf_ngtcp2_ctx *ctx = x->cf->ctx;
1698 nghttp3_vec vec[16];
1699 nghttp3_ssize veccnt;
1700 ngtcp2_ssize ndatalen;
1701 uint32_t flags;
1702 int64_t stream_id;
1703 int fin;
1704 ssize_t nwritten, n;
1705 veccnt = 0;
1706 stream_id = -1;
1707 fin = 0;
1708
1709 /* ngtcp2 may want to put several frames from different streams into
1710 * this packet. `NGTCP2_WRITE_STREAM_FLAG_MORE` tells it to do so.
1711 * When `NGTCP2_ERR_WRITE_MORE` is returned, we *need* to make
1712 * another iteration.
1713 * When ngtcp2 is happy (because it has no other frame that would fit
1714 * or it has nothing more to send), it returns the total length
1715 * of the assembled packet. This may be 0 if there was nothing to send. */
1716 nwritten = 0;
1717 *err = CURLE_OK;
1718 for(;;) {
1719
1720 if(ctx->h3conn && ngtcp2_conn_get_max_data_left(ctx->qconn)) {
1721 veccnt = nghttp3_conn_writev_stream(ctx->h3conn, &stream_id, &fin, vec,
1722 sizeof(vec) / sizeof(vec[0]));
1723 if(veccnt < 0) {
1724 failf(x->data, "nghttp3_conn_writev_stream returned error: %s",
1725 nghttp3_strerror((int)veccnt));
1726 cf_ngtcp2_h3_err_set(x->cf, x->data, (int)veccnt);
1727 *err = CURLE_SEND_ERROR;
1728 return -1;
1729 }
1730 }
1731
1732 flags = NGTCP2_WRITE_STREAM_FLAG_MORE |
1733 (fin ? NGTCP2_WRITE_STREAM_FLAG_FIN : 0);
1734 n = ngtcp2_conn_writev_stream(ctx->qconn, &x->ps.path,
1735 NULL, buf, buflen,
1736 &ndatalen, flags, stream_id,
1737 (const ngtcp2_vec *)vec, veccnt, x->ts);
1738 if(n == 0) {
1739 /* nothing to send */
1740 *err = CURLE_AGAIN;
1741 nwritten = -1;
1742 goto out;
1743 }
1744 else if(n < 0) {
1745 switch(n) {
1746 case NGTCP2_ERR_STREAM_DATA_BLOCKED: {
1747 struct h3_stream_ctx *stream = H3_STREAM_CTX(ctx, x->data);
1748 DEBUGASSERT(ndatalen == -1);
1749 nghttp3_conn_block_stream(ctx->h3conn, stream_id);
1750 CURL_TRC_CF(x->data, x->cf, "[%" CURL_PRId64 "] block quic flow",
1751 (curl_int64_t)stream_id);
1752 DEBUGASSERT(stream);
1753 if(stream)
1754 stream->quic_flow_blocked = TRUE;
1755 n = 0;
1756 break;
1757 }
1758 case NGTCP2_ERR_STREAM_SHUT_WR:
1759 DEBUGASSERT(ndatalen == -1);
1760 nghttp3_conn_shutdown_stream_write(ctx->h3conn, stream_id);
1761 n = 0;
1762 break;
1763 case NGTCP2_ERR_WRITE_MORE:
1764 /* ngtcp2 wants to send more. update the flow of the stream whose data
1765 * is in the buffer and continue */
1766 DEBUGASSERT(ndatalen >= 0);
1767 n = 0;
1768 break;
1769 default:
1770 DEBUGASSERT(ndatalen == -1);
1771 failf(x->data, "ngtcp2_conn_writev_stream returned error: %s",
1772 ngtcp2_strerror((int)n));
1773 cf_ngtcp2_err_set(x->cf, x->data, (int)n);
1774 *err = CURLE_SEND_ERROR;
1775 nwritten = -1;
1776 goto out;
1777 }
1778 }
1779
1780 if(ndatalen >= 0) {
1781 /* we add the amount of data bytes to the flow windows */
1782 int rv = nghttp3_conn_add_write_offset(ctx->h3conn, stream_id, ndatalen);
1783 if(rv) {
1784 failf(x->data, "nghttp3_conn_add_write_offset returned error: %s\n",
1785 nghttp3_strerror(rv));
1786 return CURLE_SEND_ERROR;
1787 }
1788 }
1789
1790 if(n > 0) {
1791 /* packet assembled, leave */
1792 nwritten = n;
1793 goto out;
1794 }
1795 }
1796 out:
1797 return nwritten;
1798 }
1799
cf_progress_egress(struct Curl_cfilter * cf,struct Curl_easy * data,struct pkt_io_ctx * pktx)1800 static CURLcode cf_progress_egress(struct Curl_cfilter *cf,
1801 struct Curl_easy *data,
1802 struct pkt_io_ctx *pktx)
1803 {
1804 struct cf_ngtcp2_ctx *ctx = cf->ctx;
1805 ssize_t nread;
1806 size_t max_payload_size, path_max_payload_size, max_pktcnt;
1807 size_t pktcnt = 0;
1808 size_t gsolen = 0; /* this disables gso until we have a clue */
1809 CURLcode curlcode;
1810 struct pkt_io_ctx local_pktx;
1811
1812 if(!pktx) {
1813 pktx_init(&local_pktx, cf, data);
1814 pktx = &local_pktx;
1815 }
1816 else {
1817 pktx_update_time(pktx, cf);
1818 ngtcp2_path_storage_zero(&pktx->ps);
1819 }
1820
1821 curlcode = vquic_flush(cf, data, &ctx->q);
1822 if(curlcode) {
1823 if(curlcode == CURLE_AGAIN) {
1824 Curl_expire(data, 1, EXPIRE_QUIC);
1825 return CURLE_OK;
1826 }
1827 return curlcode;
1828 }
1829
1830 /* In UDP, there is a maximum theoretical packet paload length and
1831 * a minimum payload length that is "guaranteed" to work.
1832 * To detect if this minimum payload can be increased, ngtcp2 sends
1833 * now and then a packet payload larger than the minimum. It that
1834 * is ACKed by the peer, both parties know that it works and
1835 * the subsequent packets can use a larger one.
1836 * This is called PMTUD (Path Maximum Transmission Unit Discovery).
1837 * Since a PMTUD might be rejected right on send, we do not want it
1838 * be followed by other packets of lesser size. Because those would
1839 * also fail then. So, if we detect a PMTUD while buffering, we flush.
1840 */
1841 max_payload_size = ngtcp2_conn_get_max_tx_udp_payload_size(ctx->qconn);
1842 path_max_payload_size =
1843 ngtcp2_conn_get_path_max_tx_udp_payload_size(ctx->qconn);
1844 /* maximum number of packets buffered before we flush to the socket */
1845 max_pktcnt = CURLMIN(MAX_PKT_BURST,
1846 ctx->q.sendbuf.chunk_size / max_payload_size);
1847
1848 for(;;) {
1849 /* add the next packet to send, if any, to our buffer */
1850 nread = Curl_bufq_sipn(&ctx->q.sendbuf, max_payload_size,
1851 read_pkt_to_send, pktx, &curlcode);
1852 if(nread < 0) {
1853 if(curlcode != CURLE_AGAIN)
1854 return curlcode;
1855 /* Nothing more to add, flush and leave */
1856 curlcode = vquic_send(cf, data, &ctx->q, gsolen);
1857 if(curlcode) {
1858 if(curlcode == CURLE_AGAIN) {
1859 Curl_expire(data, 1, EXPIRE_QUIC);
1860 return CURLE_OK;
1861 }
1862 return curlcode;
1863 }
1864 goto out;
1865 }
1866
1867 DEBUGASSERT(nread > 0);
1868 if(pktcnt == 0) {
1869 /* first packet in buffer. This is either of a known, "good"
1870 * payload size or it is a PMTUD. We'll see. */
1871 gsolen = (size_t)nread;
1872 }
1873 else if((size_t)nread > gsolen ||
1874 (gsolen > path_max_payload_size && (size_t)nread != gsolen)) {
1875 /* The just added packet is a PMTUD *or* the one(s) before the
1876 * just added were PMTUD and the last one is smaller.
1877 * Flush the buffer before the last add. */
1878 curlcode = vquic_send_tail_split(cf, data, &ctx->q,
1879 gsolen, nread, nread);
1880 if(curlcode) {
1881 if(curlcode == CURLE_AGAIN) {
1882 Curl_expire(data, 1, EXPIRE_QUIC);
1883 return CURLE_OK;
1884 }
1885 return curlcode;
1886 }
1887 pktcnt = 0;
1888 continue;
1889 }
1890
1891 if(++pktcnt >= max_pktcnt || (size_t)nread < gsolen) {
1892 /* Reached MAX_PKT_BURST *or*
1893 * the capacity of our buffer *or*
1894 * last add was shorter than the previous ones, flush */
1895 curlcode = vquic_send(cf, data, &ctx->q, gsolen);
1896 if(curlcode) {
1897 if(curlcode == CURLE_AGAIN) {
1898 Curl_expire(data, 1, EXPIRE_QUIC);
1899 return CURLE_OK;
1900 }
1901 return curlcode;
1902 }
1903 /* pktbuf has been completely sent */
1904 pktcnt = 0;
1905 }
1906 }
1907
1908 out:
1909 return CURLE_OK;
1910 }
1911
1912 /*
1913 * Called from transfer.c:data_pending to know if we should keep looping
1914 * to receive more data from the connection.
1915 */
cf_ngtcp2_data_pending(struct Curl_cfilter * cf,const struct Curl_easy * data)1916 static bool cf_ngtcp2_data_pending(struct Curl_cfilter *cf,
1917 const struct Curl_easy *data)
1918 {
1919 (void)cf;
1920 (void)data;
1921 return FALSE;
1922 }
1923
h3_data_pause(struct Curl_cfilter * cf,struct Curl_easy * data,bool pause)1924 static CURLcode h3_data_pause(struct Curl_cfilter *cf,
1925 struct Curl_easy *data,
1926 bool pause)
1927 {
1928 /* TODO: there seems right now no API in ngtcp2 to shrink/enlarge
1929 * the streams windows. As we do in HTTP/2. */
1930 if(!pause) {
1931 h3_drain_stream(cf, data);
1932 Curl_expire(data, 0, EXPIRE_RUN_NOW);
1933 }
1934 return CURLE_OK;
1935 }
1936
cf_ngtcp2_data_event(struct Curl_cfilter * cf,struct Curl_easy * data,int event,int arg1,void * arg2)1937 static CURLcode cf_ngtcp2_data_event(struct Curl_cfilter *cf,
1938 struct Curl_easy *data,
1939 int event, int arg1, void *arg2)
1940 {
1941 struct cf_ngtcp2_ctx *ctx = cf->ctx;
1942 CURLcode result = CURLE_OK;
1943 struct cf_call_data save;
1944
1945 CF_DATA_SAVE(save, cf, data);
1946 (void)arg1;
1947 (void)arg2;
1948 switch(event) {
1949 case CF_CTRL_DATA_SETUP:
1950 break;
1951 case CF_CTRL_DATA_PAUSE:
1952 result = h3_data_pause(cf, data, (arg1 != 0));
1953 break;
1954 case CF_CTRL_DATA_DETACH:
1955 h3_data_done(cf, data);
1956 break;
1957 case CF_CTRL_DATA_DONE:
1958 h3_data_done(cf, data);
1959 break;
1960 case CF_CTRL_DATA_DONE_SEND: {
1961 struct h3_stream_ctx *stream = H3_STREAM_CTX(ctx, data);
1962 if(stream && !stream->send_closed) {
1963 stream->send_closed = TRUE;
1964 stream->upload_left = Curl_bufq_len(&stream->sendbuf);
1965 (void)nghttp3_conn_resume_stream(ctx->h3conn, stream->id);
1966 }
1967 break;
1968 }
1969 case CF_CTRL_DATA_IDLE: {
1970 struct h3_stream_ctx *stream = H3_STREAM_CTX(ctx, data);
1971 CURL_TRC_CF(data, cf, "data idle");
1972 if(stream && !stream->closed) {
1973 result = check_and_set_expiry(cf, data, NULL);
1974 if(result)
1975 CURL_TRC_CF(data, cf, "data idle, check_and_set_expiry -> %d", result);
1976 }
1977 break;
1978 }
1979 default:
1980 break;
1981 }
1982 CF_DATA_RESTORE(cf, save);
1983 return result;
1984 }
1985
cf_ngtcp2_ctx_clear(struct cf_ngtcp2_ctx * ctx)1986 static void cf_ngtcp2_ctx_clear(struct cf_ngtcp2_ctx *ctx)
1987 {
1988 struct cf_call_data save = ctx->call_data;
1989
1990 if(ctx->qlogfd != -1) {
1991 close(ctx->qlogfd);
1992 }
1993 Curl_vquic_tls_cleanup(&ctx->tls);
1994 vquic_ctx_free(&ctx->q);
1995 if(ctx->h3conn)
1996 nghttp3_conn_del(ctx->h3conn);
1997 if(ctx->qconn)
1998 ngtcp2_conn_del(ctx->qconn);
1999 Curl_bufcp_free(&ctx->stream_bufcp);
2000 Curl_dyn_free(&ctx->scratch);
2001 Curl_hash_clean(&ctx->streams);
2002 Curl_hash_destroy(&ctx->streams);
2003 Curl_ssl_peer_cleanup(&ctx->peer);
2004
2005 memset(ctx, 0, sizeof(*ctx));
2006 ctx->qlogfd = -1;
2007 ctx->call_data = save;
2008 }
2009
cf_ngtcp2_conn_close(struct Curl_cfilter * cf,struct Curl_easy * data)2010 static void cf_ngtcp2_conn_close(struct Curl_cfilter *cf,
2011 struct Curl_easy *data)
2012 {
2013 struct cf_ngtcp2_ctx *ctx = cf->ctx;
2014 if(ctx && ctx->qconn && !ctx->conn_closed) {
2015 char buffer[NGTCP2_MAX_UDP_PAYLOAD_SIZE];
2016 struct pkt_io_ctx pktx;
2017 ngtcp2_ssize rc;
2018
2019 ctx->conn_closed = TRUE;
2020 pktx_init(&pktx, cf, data);
2021 rc = ngtcp2_conn_write_connection_close(ctx->qconn, NULL, /* path */
2022 NULL, /* pkt_info */
2023 (uint8_t *)buffer, sizeof(buffer),
2024 &ctx->last_error, pktx.ts);
2025 CURL_TRC_CF(data, cf, "closing connection(err_type=%d, err_code=%"
2026 CURL_PRIu64 ") -> %d", ctx->last_error.type,
2027 (curl_uint64_t)ctx->last_error.error_code, (int)rc);
2028 if(rc > 0) {
2029 while((send(ctx->q.sockfd, buffer, (SEND_TYPE_ARG3)rc, 0) == -1) &&
2030 SOCKERRNO == EINTR);
2031 }
2032 }
2033 }
2034
cf_ngtcp2_close(struct Curl_cfilter * cf,struct Curl_easy * data)2035 static void cf_ngtcp2_close(struct Curl_cfilter *cf, struct Curl_easy *data)
2036 {
2037 struct cf_ngtcp2_ctx *ctx = cf->ctx;
2038 struct cf_call_data save;
2039
2040 CF_DATA_SAVE(save, cf, data);
2041 if(ctx && ctx->qconn) {
2042 cf_ngtcp2_conn_close(cf, data);
2043 cf_ngtcp2_ctx_clear(ctx);
2044 CURL_TRC_CF(data, cf, "close");
2045 }
2046 cf->connected = FALSE;
2047 CF_DATA_RESTORE(cf, save);
2048 }
2049
cf_ngtcp2_destroy(struct Curl_cfilter * cf,struct Curl_easy * data)2050 static void cf_ngtcp2_destroy(struct Curl_cfilter *cf, struct Curl_easy *data)
2051 {
2052 struct cf_ngtcp2_ctx *ctx = cf->ctx;
2053 struct cf_call_data save;
2054
2055 CF_DATA_SAVE(save, cf, data);
2056 CURL_TRC_CF(data, cf, "destroy");
2057 if(ctx) {
2058 cf_ngtcp2_ctx_clear(ctx);
2059 free(ctx);
2060 }
2061 cf->ctx = NULL;
2062 /* No CF_DATA_RESTORE(cf, save) possible */
2063 (void)save;
2064 }
2065
2066 #ifdef USE_OPENSSL
2067 /* The "new session" callback must return zero if the session can be removed
2068 * or non-zero if the session has been put into the session cache.
2069 */
quic_ossl_new_session_cb(SSL * ssl,SSL_SESSION * ssl_sessionid)2070 static int quic_ossl_new_session_cb(SSL *ssl, SSL_SESSION *ssl_sessionid)
2071 {
2072 struct Curl_cfilter *cf;
2073 struct cf_ngtcp2_ctx *ctx;
2074 struct Curl_easy *data;
2075 ngtcp2_crypto_conn_ref *cref;
2076
2077 cref = (ngtcp2_crypto_conn_ref *)SSL_get_app_data(ssl);
2078 cf = cref? cref->user_data : NULL;
2079 ctx = cf? cf->ctx : NULL;
2080 data = cf? CF_DATA_CURRENT(cf) : NULL;
2081 if(cf && data && ctx) {
2082 Curl_ossl_add_session(cf, data, &ctx->peer, ssl_sessionid);
2083 return 1;
2084 }
2085 return 0;
2086 }
2087 #endif /* USE_OPENSSL */
2088
tls_ctx_setup(struct Curl_cfilter * cf,struct Curl_easy * data,void * user_data)2089 static CURLcode tls_ctx_setup(struct Curl_cfilter *cf,
2090 struct Curl_easy *data,
2091 void *user_data)
2092 {
2093 struct curl_tls_ctx *ctx = user_data;
2094 (void)cf;
2095 #ifdef USE_OPENSSL
2096 #if defined(OPENSSL_IS_BORINGSSL) || defined(OPENSSL_IS_AWSLC)
2097 if(ngtcp2_crypto_boringssl_configure_client_context(ctx->ossl.ssl_ctx)
2098 != 0) {
2099 failf(data, "ngtcp2_crypto_boringssl_configure_client_context failed");
2100 return CURLE_FAILED_INIT;
2101 }
2102 #else
2103 if(ngtcp2_crypto_quictls_configure_client_context(ctx->ossl.ssl_ctx) != 0) {
2104 failf(data, "ngtcp2_crypto_quictls_configure_client_context failed");
2105 return CURLE_FAILED_INIT;
2106 }
2107 #endif /* !OPENSSL_IS_BORINGSSL && !OPENSSL_IS_AWSLC */
2108 /* Enable the session cache because it's a prerequisite for the
2109 * "new session" callback. Use the "external storage" mode to prevent
2110 * OpenSSL from creating an internal session cache.
2111 */
2112 SSL_CTX_set_session_cache_mode(ctx->ossl.ssl_ctx,
2113 SSL_SESS_CACHE_CLIENT |
2114 SSL_SESS_CACHE_NO_INTERNAL);
2115 SSL_CTX_sess_set_new_cb(ctx->ossl.ssl_ctx, quic_ossl_new_session_cb);
2116
2117 #elif defined(USE_GNUTLS)
2118 if(ngtcp2_crypto_gnutls_configure_client_session(ctx->gtls.session) != 0) {
2119 failf(data, "ngtcp2_crypto_gnutls_configure_client_session failed");
2120 return CURLE_FAILED_INIT;
2121 }
2122 #elif defined(USE_WOLFSSL)
2123 if(ngtcp2_crypto_wolfssl_configure_client_context(ctx->ssl_ctx) != 0) {
2124 failf(data, "ngtcp2_crypto_wolfssl_configure_client_context failed");
2125 return CURLE_FAILED_INIT;
2126 }
2127 #endif
2128 return CURLE_OK;
2129 }
2130
2131 /*
2132 * Might be called twice for happy eyeballs.
2133 */
cf_connect_start(struct Curl_cfilter * cf,struct Curl_easy * data,struct pkt_io_ctx * pktx)2134 static CURLcode cf_connect_start(struct Curl_cfilter *cf,
2135 struct Curl_easy *data,
2136 struct pkt_io_ctx *pktx)
2137 {
2138 struct cf_ngtcp2_ctx *ctx = cf->ctx;
2139 int rc;
2140 int rv;
2141 CURLcode result;
2142 const struct Curl_sockaddr_ex *sockaddr = NULL;
2143 int qfd;
2144
2145 ctx->version = NGTCP2_PROTO_VER_MAX;
2146 ctx->max_stream_window = H3_STREAM_WINDOW_SIZE;
2147 ctx->max_idle_ms = CURL_QUIC_MAX_IDLE_MS;
2148 Curl_bufcp_init(&ctx->stream_bufcp, H3_STREAM_CHUNK_SIZE,
2149 H3_STREAM_POOL_SPARES);
2150 Curl_dyn_init(&ctx->scratch, CURL_MAX_HTTP_HEADER);
2151 Curl_hash_offt_init(&ctx->streams, 63, h3_stream_hash_free);
2152
2153 result = Curl_ssl_peer_init(&ctx->peer, cf, TRNSPRT_QUIC);
2154 if(result)
2155 return result;
2156
2157 #define H3_ALPN "\x2h3\x5h3-29"
2158 result = Curl_vquic_tls_init(&ctx->tls, cf, data, &ctx->peer,
2159 H3_ALPN, sizeof(H3_ALPN) - 1,
2160 tls_ctx_setup, &ctx->tls, &ctx->conn_ref);
2161 if(result)
2162 return result;
2163
2164 #ifdef USE_OPENSSL
2165 SSL_set_quic_use_legacy_codepoint(ctx->tls.ossl.ssl, 0);
2166 #endif
2167
2168 ctx->dcid.datalen = NGTCP2_MAX_CIDLEN;
2169 result = Curl_rand(data, ctx->dcid.data, NGTCP2_MAX_CIDLEN);
2170 if(result)
2171 return result;
2172
2173 ctx->scid.datalen = NGTCP2_MAX_CIDLEN;
2174 result = Curl_rand(data, ctx->scid.data, NGTCP2_MAX_CIDLEN);
2175 if(result)
2176 return result;
2177
2178 (void)Curl_qlogdir(data, ctx->scid.data, NGTCP2_MAX_CIDLEN, &qfd);
2179 ctx->qlogfd = qfd; /* -1 if failure above */
2180 quic_settings(ctx, data, pktx);
2181
2182 result = vquic_ctx_init(&ctx->q);
2183 if(result)
2184 return result;
2185
2186 Curl_cf_socket_peek(cf->next, data, &ctx->q.sockfd, &sockaddr, NULL);
2187 if(!sockaddr)
2188 return CURLE_QUIC_CONNECT_ERROR;
2189 ctx->q.local_addrlen = sizeof(ctx->q.local_addr);
2190 rv = getsockname(ctx->q.sockfd, (struct sockaddr *)&ctx->q.local_addr,
2191 &ctx->q.local_addrlen);
2192 if(rv == -1)
2193 return CURLE_QUIC_CONNECT_ERROR;
2194
2195 ngtcp2_addr_init(&ctx->connected_path.local,
2196 (struct sockaddr *)&ctx->q.local_addr,
2197 ctx->q.local_addrlen);
2198 ngtcp2_addr_init(&ctx->connected_path.remote,
2199 &sockaddr->sa_addr, sockaddr->addrlen);
2200
2201 rc = ngtcp2_conn_client_new(&ctx->qconn, &ctx->dcid, &ctx->scid,
2202 &ctx->connected_path,
2203 NGTCP2_PROTO_VER_V1, &ng_callbacks,
2204 &ctx->settings, &ctx->transport_params,
2205 NULL, cf);
2206 if(rc)
2207 return CURLE_QUIC_CONNECT_ERROR;
2208
2209 #ifdef USE_OPENSSL
2210 ngtcp2_conn_set_tls_native_handle(ctx->qconn, ctx->tls.ossl.ssl);
2211 #elif defined(USE_GNUTLS)
2212 ngtcp2_conn_set_tls_native_handle(ctx->qconn, ctx->tls.gtls.session);
2213 #else
2214 ngtcp2_conn_set_tls_native_handle(ctx->qconn, ctx->tls.ssl);
2215 #endif
2216
2217 ngtcp2_ccerr_default(&ctx->last_error);
2218
2219 ctx->conn_ref.get_conn = get_conn;
2220 ctx->conn_ref.user_data = cf;
2221
2222 return CURLE_OK;
2223 }
2224
cf_ngtcp2_connect(struct Curl_cfilter * cf,struct Curl_easy * data,bool blocking,bool * done)2225 static CURLcode cf_ngtcp2_connect(struct Curl_cfilter *cf,
2226 struct Curl_easy *data,
2227 bool blocking, bool *done)
2228 {
2229 struct cf_ngtcp2_ctx *ctx = cf->ctx;
2230 CURLcode result = CURLE_OK;
2231 struct cf_call_data save;
2232 struct curltime now;
2233 struct pkt_io_ctx pktx;
2234
2235 if(cf->connected) {
2236 *done = TRUE;
2237 return CURLE_OK;
2238 }
2239
2240 /* Connect the UDP filter first */
2241 if(!cf->next->connected) {
2242 result = Curl_conn_cf_connect(cf->next, data, blocking, done);
2243 if(result || !*done)
2244 return result;
2245 }
2246
2247 *done = FALSE;
2248 now = Curl_now();
2249 pktx_init(&pktx, cf, data);
2250
2251 CF_DATA_SAVE(save, cf, data);
2252
2253 if(ctx->reconnect_at.tv_sec && Curl_timediff(now, ctx->reconnect_at) < 0) {
2254 /* Not time yet to attempt the next connect */
2255 CURL_TRC_CF(data, cf, "waiting for reconnect time");
2256 goto out;
2257 }
2258
2259 if(!ctx->qconn) {
2260 ctx->started_at = now;
2261 result = cf_connect_start(cf, data, &pktx);
2262 if(result)
2263 goto out;
2264 result = cf_progress_egress(cf, data, &pktx);
2265 /* we do not expect to be able to recv anything yet */
2266 goto out;
2267 }
2268
2269 result = cf_progress_ingress(cf, data, &pktx);
2270 if(result)
2271 goto out;
2272
2273 result = cf_progress_egress(cf, data, &pktx);
2274 if(result)
2275 goto out;
2276
2277 if(ngtcp2_conn_get_handshake_completed(ctx->qconn)) {
2278 ctx->handshake_at = now;
2279 CURL_TRC_CF(data, cf, "handshake complete after %dms",
2280 (int)Curl_timediff(now, ctx->started_at));
2281 result = qng_verify_peer(cf, data);
2282 if(!result) {
2283 CURL_TRC_CF(data, cf, "peer verified");
2284 cf->connected = TRUE;
2285 cf->conn->alpn = CURL_HTTP_VERSION_3;
2286 *done = TRUE;
2287 connkeep(cf->conn, "HTTP/3 default");
2288 }
2289 }
2290
2291 out:
2292 if(result == CURLE_RECV_ERROR && ctx->qconn &&
2293 ngtcp2_conn_in_draining_period(ctx->qconn)) {
2294 /* When a QUIC server instance is shutting down, it may send us a
2295 * CONNECTION_CLOSE right away. Our connection then enters the DRAINING
2296 * state. The CONNECT may work in the near future again. Indicate
2297 * that as a "weird" reply. */
2298 result = CURLE_WEIRD_SERVER_REPLY;
2299 }
2300
2301 #ifndef CURL_DISABLE_VERBOSE_STRINGS
2302 if(result) {
2303 struct ip_quadruple ip;
2304
2305 Curl_cf_socket_peek(cf->next, data, NULL, NULL, &ip);
2306 infof(data, "QUIC connect to %s port %u failed: %s",
2307 ip.remote_ip, ip.remote_port, curl_easy_strerror(result));
2308 }
2309 #endif
2310 if(!result && ctx->qconn) {
2311 result = check_and_set_expiry(cf, data, &pktx);
2312 }
2313 if(result || *done)
2314 CURL_TRC_CF(data, cf, "connect -> %d, done=%d", result, *done);
2315 CF_DATA_RESTORE(cf, save);
2316 return result;
2317 }
2318
cf_ngtcp2_query(struct Curl_cfilter * cf,struct Curl_easy * data,int query,int * pres1,void * pres2)2319 static CURLcode cf_ngtcp2_query(struct Curl_cfilter *cf,
2320 struct Curl_easy *data,
2321 int query, int *pres1, void *pres2)
2322 {
2323 struct cf_ngtcp2_ctx *ctx = cf->ctx;
2324 struct cf_call_data save;
2325
2326 switch(query) {
2327 case CF_QUERY_MAX_CONCURRENT: {
2328 DEBUGASSERT(pres1);
2329 CF_DATA_SAVE(save, cf, data);
2330 /* Set after transport params arrived and continually updated
2331 * by callback. QUIC counts the number over the lifetime of the
2332 * connection, ever increasing.
2333 * We count the *open* transfers plus the budget for new ones. */
2334 if(!ctx->qconn || ctx->conn_closed) {
2335 *pres1 = 0;
2336 }
2337 else if(ctx->max_bidi_streams) {
2338 uint64_t avail_bidi_streams = 0;
2339 uint64_t max_streams = CONN_INUSE(cf->conn);
2340 if(ctx->max_bidi_streams > ctx->used_bidi_streams)
2341 avail_bidi_streams = ctx->max_bidi_streams - ctx->used_bidi_streams;
2342 max_streams += avail_bidi_streams;
2343 *pres1 = (max_streams > INT_MAX)? INT_MAX : (int)max_streams;
2344 }
2345 else /* transport params not arrived yet? take our default. */
2346 *pres1 = Curl_multi_max_concurrent_streams(data->multi);
2347 CURL_TRC_CF(data, cf, "query conn[%" CURL_FORMAT_CURL_OFF_T "]: "
2348 "MAX_CONCURRENT -> %d (%zu in use)",
2349 cf->conn->connection_id, *pres1, CONN_INUSE(cf->conn));
2350 CF_DATA_RESTORE(cf, save);
2351 return CURLE_OK;
2352 }
2353 case CF_QUERY_CONNECT_REPLY_MS:
2354 if(ctx->q.got_first_byte) {
2355 timediff_t ms = Curl_timediff(ctx->q.first_byte_at, ctx->started_at);
2356 *pres1 = (ms < INT_MAX)? (int)ms : INT_MAX;
2357 }
2358 else
2359 *pres1 = -1;
2360 return CURLE_OK;
2361 case CF_QUERY_TIMER_CONNECT: {
2362 struct curltime *when = pres2;
2363 if(ctx->q.got_first_byte)
2364 *when = ctx->q.first_byte_at;
2365 return CURLE_OK;
2366 }
2367 case CF_QUERY_TIMER_APPCONNECT: {
2368 struct curltime *when = pres2;
2369 if(cf->connected)
2370 *when = ctx->handshake_at;
2371 return CURLE_OK;
2372 }
2373 default:
2374 break;
2375 }
2376 return cf->next?
2377 cf->next->cft->query(cf->next, data, query, pres1, pres2) :
2378 CURLE_UNKNOWN_OPTION;
2379 }
2380
cf_ngtcp2_conn_is_alive(struct Curl_cfilter * cf,struct Curl_easy * data,bool * input_pending)2381 static bool cf_ngtcp2_conn_is_alive(struct Curl_cfilter *cf,
2382 struct Curl_easy *data,
2383 bool *input_pending)
2384 {
2385 struct cf_ngtcp2_ctx *ctx = cf->ctx;
2386 bool alive = FALSE;
2387 const ngtcp2_transport_params *rp;
2388 struct cf_call_data save;
2389
2390 CF_DATA_SAVE(save, cf, data);
2391 *input_pending = FALSE;
2392 if(!ctx->qconn || ctx->conn_closed)
2393 goto out;
2394
2395 /* Both sides of the QUIC connection announce they max idle times in
2396 * the transport parameters. Look at the minimum of both and if
2397 * we exceed this, regard the connection as dead. The other side
2398 * may have completely purged it and will no longer respond
2399 * to any packets from us. */
2400 rp = ngtcp2_conn_get_remote_transport_params(ctx->qconn);
2401 if(rp) {
2402 timediff_t idletime;
2403 uint64_t idle_ms = ctx->max_idle_ms;
2404
2405 if(rp->max_idle_timeout &&
2406 (rp->max_idle_timeout / NGTCP2_MILLISECONDS) < idle_ms)
2407 idle_ms = (rp->max_idle_timeout / NGTCP2_MILLISECONDS);
2408 idletime = Curl_timediff(Curl_now(), ctx->q.last_io);
2409 if(idletime > 0 && (uint64_t)idletime > idle_ms)
2410 goto out;
2411 }
2412
2413 if(!cf->next || !cf->next->cft->is_alive(cf->next, data, input_pending))
2414 goto out;
2415
2416 alive = TRUE;
2417 if(*input_pending) {
2418 CURLcode result;
2419 /* This happens before we've sent off a request and the connection is
2420 not in use by any other transfer, there shouldn't be any data here,
2421 only "protocol frames" */
2422 *input_pending = FALSE;
2423 result = cf_progress_ingress(cf, data, NULL);
2424 CURL_TRC_CF(data, cf, "is_alive, progress ingress -> %d", result);
2425 alive = result? FALSE : TRUE;
2426 }
2427
2428 out:
2429 CF_DATA_RESTORE(cf, save);
2430 return alive;
2431 }
2432
2433 struct Curl_cftype Curl_cft_http3 = {
2434 "HTTP/3",
2435 CF_TYPE_IP_CONNECT | CF_TYPE_SSL | CF_TYPE_MULTIPLEX,
2436 0,
2437 cf_ngtcp2_destroy,
2438 cf_ngtcp2_connect,
2439 cf_ngtcp2_close,
2440 Curl_cf_def_get_host,
2441 cf_ngtcp2_adjust_pollset,
2442 cf_ngtcp2_data_pending,
2443 cf_ngtcp2_send,
2444 cf_ngtcp2_recv,
2445 cf_ngtcp2_data_event,
2446 cf_ngtcp2_conn_is_alive,
2447 Curl_cf_def_conn_keep_alive,
2448 cf_ngtcp2_query,
2449 };
2450
Curl_cf_ngtcp2_create(struct Curl_cfilter ** pcf,struct Curl_easy * data,struct connectdata * conn,const struct Curl_addrinfo * ai)2451 CURLcode Curl_cf_ngtcp2_create(struct Curl_cfilter **pcf,
2452 struct Curl_easy *data,
2453 struct connectdata *conn,
2454 const struct Curl_addrinfo *ai)
2455 {
2456 struct cf_ngtcp2_ctx *ctx = NULL;
2457 struct Curl_cfilter *cf = NULL, *udp_cf = NULL;
2458 CURLcode result;
2459
2460 (void)data;
2461 ctx = calloc(1, sizeof(*ctx));
2462 if(!ctx) {
2463 result = CURLE_OUT_OF_MEMORY;
2464 goto out;
2465 }
2466 ctx->qlogfd = -1;
2467 cf_ngtcp2_ctx_clear(ctx);
2468
2469 result = Curl_cf_create(&cf, &Curl_cft_http3, ctx);
2470 if(result)
2471 goto out;
2472
2473 result = Curl_cf_udp_create(&udp_cf, data, conn, ai, TRNSPRT_QUIC);
2474 if(result)
2475 goto out;
2476
2477 cf->conn = conn;
2478 udp_cf->conn = cf->conn;
2479 udp_cf->sockindex = cf->sockindex;
2480 cf->next = udp_cf;
2481
2482 out:
2483 *pcf = (!result)? cf : NULL;
2484 if(result) {
2485 if(udp_cf)
2486 Curl_conn_cf_discard_sub(cf, udp_cf, data, TRUE);
2487 Curl_safefree(cf);
2488 Curl_safefree(ctx);
2489 }
2490 return result;
2491 }
2492
Curl_conn_is_ngtcp2(const struct Curl_easy * data,const struct connectdata * conn,int sockindex)2493 bool Curl_conn_is_ngtcp2(const struct Curl_easy *data,
2494 const struct connectdata *conn,
2495 int sockindex)
2496 {
2497 struct Curl_cfilter *cf = conn? conn->cfilter[sockindex] : NULL;
2498
2499 (void)data;
2500 for(; cf; cf = cf->next) {
2501 if(cf->cft == &Curl_cft_http3)
2502 return TRUE;
2503 if(cf->cft->flags & CF_TYPE_IP_CONNECT)
2504 return FALSE;
2505 }
2506 return FALSE;
2507 }
2508
2509 #endif
2510