xref: /curl/lib/vquic/curl_quiche.c (revision 2036bebb)
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 #ifdef USE_QUICHE
28 #include <quiche.h>
29 #include <openssl/err.h>
30 #include <openssl/ssl.h>
31 #include "bufq.h"
32 #include "hash.h"
33 #include "urldata.h"
34 #include "cfilters.h"
35 #include "cf-socket.h"
36 #include "sendf.h"
37 #include "strdup.h"
38 #include "rand.h"
39 #include "strcase.h"
40 #include "multiif.h"
41 #include "connect.h"
42 #include "progress.h"
43 #include "strerror.h"
44 #include "http1.h"
45 #include "vquic.h"
46 #include "vquic_int.h"
47 #include "vquic-tls.h"
48 #include "curl_quiche.h"
49 #include "transfer.h"
50 #include "inet_pton.h"
51 #include "vtls/openssl.h"
52 #include "vtls/keylog.h"
53 #include "vtls/vtls.h"
54 
55 /* The last 3 #include files should be in this order */
56 #include "curl_printf.h"
57 #include "curl_memory.h"
58 #include "memdebug.h"
59 
60 /* HTTP/3 error values defined in RFC 9114, ch. 8.1 */
61 #define CURL_H3_NO_ERROR  (0x0100)
62 
63 #define QUIC_MAX_STREAMS              (100)
64 
65 #define H3_STREAM_WINDOW_SIZE  (128 * 1024)
66 #define H3_STREAM_CHUNK_SIZE    (16 * 1024)
67 /* The pool keeps spares around and half of a full stream windows
68  * seems good. More does not seem to improve performance.
69  * The benefit of the pool is that stream buffer to not keep
70  * spares. So memory consumption goes down when streams run empty,
71  * have a large upload done, etc. */
72 #define H3_STREAM_POOL_SPARES \
73           (H3_STREAM_WINDOW_SIZE / H3_STREAM_CHUNK_SIZE ) / 2
74 /* Receive and Send max number of chunks just follows from the
75  * chunk size and window size */
76 #define H3_STREAM_RECV_CHUNKS \
77           (H3_STREAM_WINDOW_SIZE / H3_STREAM_CHUNK_SIZE)
78 #define H3_STREAM_SEND_CHUNKS \
79           (H3_STREAM_WINDOW_SIZE / H3_STREAM_CHUNK_SIZE)
80 
81 /*
82  * Store quiche version info in this buffer.
83  */
Curl_quiche_ver(char * p,size_t len)84 void Curl_quiche_ver(char *p, size_t len)
85 {
86   (void)msnprintf(p, len, "quiche/%s", quiche_version());
87 }
88 
89 struct cf_quiche_ctx {
90   struct cf_quic_ctx q;
91   struct ssl_peer peer;
92   struct curl_tls_ctx tls;
93   quiche_conn *qconn;
94   quiche_config *cfg;
95   quiche_h3_conn *h3c;
96   quiche_h3_config *h3config;
97   uint8_t scid[QUICHE_MAX_CONN_ID_LEN];
98   struct curltime started_at;        /* time the current attempt started */
99   struct curltime handshake_at;      /* time connect handshake finished */
100   struct curltime reconnect_at;      /* time the next attempt should start */
101   struct bufc_pool stream_bufcp;     /* chunk pool for streams */
102   struct Curl_hash streams;          /* hash `data->id` to `stream_ctx` */
103   curl_off_t data_recvd;
104   BIT(goaway);                       /* got GOAWAY from server */
105   BIT(x509_store_setup);             /* if x509 store has been set up */
106 };
107 
108 #ifdef DEBUG_QUICHE
quiche_debug_log(const char * line,void * argp)109 static void quiche_debug_log(const char *line, void *argp)
110 {
111   (void)argp;
112   fprintf(stderr, "%s\n", line);
113 }
114 #endif
115 
cf_quiche_ctx_clear(struct cf_quiche_ctx * ctx)116 static void cf_quiche_ctx_clear(struct cf_quiche_ctx *ctx)
117 {
118   if(ctx) {
119     if(ctx->h3c)
120       quiche_h3_conn_free(ctx->h3c);
121     if(ctx->h3config)
122       quiche_h3_config_free(ctx->h3config);
123     if(ctx->qconn)
124       quiche_conn_free(ctx->qconn);
125     if(ctx->cfg)
126       quiche_config_free(ctx->cfg);
127     /* quiche just freed it */
128     ctx->tls.ossl.ssl = NULL;
129     Curl_vquic_tls_cleanup(&ctx->tls);
130     Curl_ssl_peer_cleanup(&ctx->peer);
131     vquic_ctx_free(&ctx->q);
132     Curl_bufcp_free(&ctx->stream_bufcp);
133     Curl_hash_clean(&ctx->streams);
134     Curl_hash_destroy(&ctx->streams);
135 
136     memset(ctx, 0, sizeof(*ctx));
137   }
138 }
139 
140 static CURLcode cf_flush_egress(struct Curl_cfilter *cf,
141                                 struct Curl_easy *data);
142 
143 /**
144  * All about the H3 internals of a stream
145  */
146 struct stream_ctx {
147   curl_uint64_t id; /* HTTP/3 protocol stream identifier */
148   struct bufq recvbuf; /* h3 response */
149   struct h1_req_parser h1; /* h1 request parsing */
150   curl_uint64_t error3; /* HTTP/3 stream error code */
151   curl_off_t upload_left; /* number of request bytes left to upload */
152   BIT(opened); /* TRUE after stream has been opened */
153   BIT(closed); /* TRUE on stream close */
154   BIT(reset);  /* TRUE on stream reset */
155   BIT(send_closed); /* stream is locally closed */
156   BIT(resp_hds_complete);  /* final response has been received */
157   BIT(resp_got_header); /* TRUE when h3 stream has recvd some HEADER */
158   BIT(quic_flow_blocked); /* stream is blocked by QUIC flow control */
159 };
160 
161 #define H3_STREAM_CTX(ctx,data)   ((struct stream_ctx *)(\
162             data? Curl_hash_offt_get(&(ctx)->streams, (data)->id) : NULL))
163 
h3_stream_ctx_free(struct stream_ctx * stream)164 static void h3_stream_ctx_free(struct stream_ctx *stream)
165 {
166   Curl_bufq_free(&stream->recvbuf);
167   Curl_h1_req_parse_free(&stream->h1);
168   free(stream);
169 }
170 
h3_stream_hash_free(void * stream)171 static void h3_stream_hash_free(void *stream)
172 {
173   DEBUGASSERT(stream);
174   h3_stream_ctx_free((struct stream_ctx *)stream);
175 }
176 
check_resumes(struct Curl_cfilter * cf,struct Curl_easy * data)177 static void check_resumes(struct Curl_cfilter *cf,
178                           struct Curl_easy *data)
179 {
180   struct cf_quiche_ctx *ctx = cf->ctx;
181   struct Curl_easy *sdata;
182   struct stream_ctx *stream;
183 
184   DEBUGASSERT(data->multi);
185   for(sdata = data->multi->easyp; sdata; sdata = sdata->next) {
186     if(sdata->conn == data->conn) {
187       stream = H3_STREAM_CTX(ctx, sdata);
188       if(stream && stream->quic_flow_blocked) {
189         stream->quic_flow_blocked = FALSE;
190         Curl_expire(data, 0, EXPIRE_RUN_NOW);
191         CURL_TRC_CF(data, cf, "[%"CURL_PRIu64"] unblock", stream->id);
192       }
193     }
194   }
195 }
196 
h3_data_setup(struct Curl_cfilter * cf,struct Curl_easy * data)197 static CURLcode h3_data_setup(struct Curl_cfilter *cf,
198                               struct Curl_easy *data)
199 {
200   struct cf_quiche_ctx *ctx = cf->ctx;
201   struct stream_ctx *stream = H3_STREAM_CTX(ctx, data);
202 
203   if(stream)
204     return CURLE_OK;
205 
206   stream = calloc(1, sizeof(*stream));
207   if(!stream)
208     return CURLE_OUT_OF_MEMORY;
209 
210   stream->id = -1;
211   Curl_bufq_initp(&stream->recvbuf, &ctx->stream_bufcp,
212                   H3_STREAM_RECV_CHUNKS, BUFQ_OPT_SOFT_LIMIT);
213   Curl_h1_req_parse_init(&stream->h1, H1_PARSE_DEFAULT_MAX_LINE_LEN);
214 
215   if(!Curl_hash_offt_set(&ctx->streams, data->id, stream)) {
216     h3_stream_ctx_free(stream);
217     return CURLE_OUT_OF_MEMORY;
218   }
219 
220   return CURLE_OK;
221 }
222 
h3_data_done(struct Curl_cfilter * cf,struct Curl_easy * data)223 static void h3_data_done(struct Curl_cfilter *cf, struct Curl_easy *data)
224 {
225   struct cf_quiche_ctx *ctx = cf->ctx;
226   struct stream_ctx *stream = H3_STREAM_CTX(ctx, data);
227   CURLcode result;
228 
229   (void)cf;
230   if(stream) {
231     CURL_TRC_CF(data, cf, "[%"CURL_PRIu64"] easy handle is done", stream->id);
232     if(ctx->qconn && !stream->closed) {
233       quiche_conn_stream_shutdown(ctx->qconn, stream->id,
234                                   QUICHE_SHUTDOWN_READ, CURL_H3_NO_ERROR);
235       if(!stream->send_closed) {
236         quiche_conn_stream_shutdown(ctx->qconn, stream->id,
237                                     QUICHE_SHUTDOWN_WRITE, CURL_H3_NO_ERROR);
238         stream->send_closed = TRUE;
239       }
240       stream->closed = TRUE;
241       result = cf_flush_egress(cf, data);
242       if(result)
243         CURL_TRC_CF(data, cf, "data_done, flush egress -> %d", result);
244     }
245     Curl_hash_offt_remove(&ctx->streams, data->id);
246   }
247 }
248 
drain_stream(struct Curl_cfilter * cf,struct Curl_easy * data)249 static void drain_stream(struct Curl_cfilter *cf,
250                          struct Curl_easy *data)
251 {
252   struct cf_quiche_ctx *ctx = cf->ctx;
253   struct stream_ctx *stream = H3_STREAM_CTX(ctx, data);
254   unsigned char bits;
255 
256   (void)cf;
257   bits = CURL_CSELECT_IN;
258   if(stream && !stream->send_closed && stream->upload_left)
259     bits |= CURL_CSELECT_OUT;
260   if(data->state.select_bits != bits) {
261     data->state.select_bits = bits;
262     Curl_expire(data, 0, EXPIRE_RUN_NOW);
263   }
264 }
265 
get_stream_easy(struct Curl_cfilter * cf,struct Curl_easy * data,curl_uint64_t stream_id,struct stream_ctx ** pstream)266 static struct Curl_easy *get_stream_easy(struct Curl_cfilter *cf,
267                                          struct Curl_easy *data,
268                                          curl_uint64_t stream_id,
269                                          struct stream_ctx **pstream)
270 {
271   struct cf_quiche_ctx *ctx = cf->ctx;
272   struct Curl_easy *sdata;
273   struct stream_ctx *stream;
274 
275   (void)cf;
276   stream = H3_STREAM_CTX(ctx, data);
277   if(stream && stream->id == stream_id) {
278     *pstream = stream;
279     return data;
280   }
281   else {
282     DEBUGASSERT(data->multi);
283     for(sdata = data->multi->easyp; sdata; sdata = sdata->next) {
284       if(sdata->conn != data->conn)
285         continue;
286       stream = H3_STREAM_CTX(ctx, sdata);
287       if(stream && stream->id == stream_id) {
288         *pstream = stream;
289         return sdata;
290       }
291     }
292   }
293   *pstream = NULL;
294   return NULL;
295 }
296 
cf_quiche_expire_conn_closed(struct Curl_cfilter * cf,struct Curl_easy * data)297 static void cf_quiche_expire_conn_closed(struct Curl_cfilter *cf,
298                                          struct Curl_easy *data)
299 {
300   struct Curl_easy *sdata;
301 
302   DEBUGASSERT(data->multi);
303   CURL_TRC_CF(data, cf, "conn closed, expire all transfers");
304   for(sdata = data->multi->easyp; sdata; sdata = sdata->next) {
305     if(sdata == data || sdata->conn != data->conn)
306       continue;
307     CURL_TRC_CF(sdata, cf, "conn closed, expire transfer");
308     Curl_expire(sdata, 0, EXPIRE_RUN_NOW);
309   }
310 }
311 
312 /*
313  * write_resp_raw() copies response data in raw format to the `data`'s
314   * receive buffer. If not enough space is available, it appends to the
315  * `data`'s overflow buffer.
316  */
write_resp_raw(struct Curl_cfilter * cf,struct Curl_easy * data,const void * mem,size_t memlen)317 static CURLcode write_resp_raw(struct Curl_cfilter *cf,
318                                struct Curl_easy *data,
319                                const void *mem, size_t memlen)
320 {
321   struct cf_quiche_ctx *ctx = cf->ctx;
322   struct stream_ctx *stream = H3_STREAM_CTX(ctx, data);
323   CURLcode result = CURLE_OK;
324   ssize_t nwritten;
325 
326   (void)cf;
327   if(!stream)
328     return CURLE_RECV_ERROR;
329   nwritten = Curl_bufq_write(&stream->recvbuf, mem, memlen, &result);
330   if(nwritten < 0)
331     return result;
332 
333   if((size_t)nwritten < memlen) {
334     /* This MUST not happen. Our recbuf is dimensioned to hold the
335      * full max_stream_window and then some for this very reason. */
336     DEBUGASSERT(0);
337     return CURLE_RECV_ERROR;
338   }
339   return result;
340 }
341 
342 struct cb_ctx {
343   struct Curl_cfilter *cf;
344   struct Curl_easy *data;
345 };
346 
cb_each_header(uint8_t * name,size_t name_len,uint8_t * value,size_t value_len,void * argp)347 static int cb_each_header(uint8_t *name, size_t name_len,
348                           uint8_t *value, size_t value_len,
349                           void *argp)
350 {
351   struct cb_ctx *x = argp;
352   struct cf_quiche_ctx *ctx = x->cf->ctx;
353   struct stream_ctx *stream = H3_STREAM_CTX(ctx, x->data);
354   CURLcode result;
355 
356   if(!stream)
357     return CURLE_OK;
358 
359   if((name_len == 7) && !strncmp(HTTP_PSEUDO_STATUS, (char *)name, 7)) {
360     CURL_TRC_CF(x->data, x->cf, "[%" CURL_PRIu64 "] status: %.*s",
361                 stream->id, (int)value_len, value);
362     result = write_resp_raw(x->cf, x->data, "HTTP/3 ", sizeof("HTTP/3 ") - 1);
363     if(!result)
364       result = write_resp_raw(x->cf, x->data, value, value_len);
365     if(!result)
366       result = write_resp_raw(x->cf, x->data, " \r\n", 3);
367   }
368   else {
369     CURL_TRC_CF(x->data, x->cf, "[%" CURL_PRIu64 "] header: %.*s: %.*s",
370                 stream->id, (int)name_len, name,
371                 (int)value_len, value);
372     result = write_resp_raw(x->cf, x->data, name, name_len);
373     if(!result)
374       result = write_resp_raw(x->cf, x->data, ": ", 2);
375     if(!result)
376       result = write_resp_raw(x->cf, x->data, value, value_len);
377     if(!result)
378       result = write_resp_raw(x->cf, x->data, "\r\n", 2);
379   }
380   if(result) {
381     CURL_TRC_CF(x->data, x->cf, "[%"CURL_PRIu64"] on header error %d",
382                 stream->id, result);
383   }
384   return result;
385 }
386 
stream_resp_read(void * reader_ctx,unsigned char * buf,size_t len,CURLcode * err)387 static ssize_t stream_resp_read(void *reader_ctx,
388                                 unsigned char *buf, size_t len,
389                                 CURLcode *err)
390 {
391   struct cb_ctx *x = reader_ctx;
392   struct cf_quiche_ctx *ctx = x->cf->ctx;
393   struct stream_ctx *stream = H3_STREAM_CTX(ctx, x->data);
394   ssize_t nread;
395 
396   if(!stream) {
397     *err = CURLE_RECV_ERROR;
398     return -1;
399   }
400 
401   nread = quiche_h3_recv_body(ctx->h3c, ctx->qconn, stream->id,
402                               buf, len);
403   if(nread >= 0) {
404     *err = CURLE_OK;
405     return nread;
406   }
407   else {
408     *err = CURLE_AGAIN;
409     return -1;
410   }
411 }
412 
cf_recv_body(struct Curl_cfilter * cf,struct Curl_easy * data)413 static CURLcode cf_recv_body(struct Curl_cfilter *cf,
414                              struct Curl_easy *data)
415 {
416   struct cf_quiche_ctx *ctx = cf->ctx;
417   struct stream_ctx *stream = H3_STREAM_CTX(ctx, data);
418   ssize_t nwritten;
419   struct cb_ctx cb_ctx;
420   CURLcode result = CURLE_OK;
421 
422   if(!stream)
423     return CURLE_RECV_ERROR;
424 
425   if(!stream->resp_hds_complete) {
426     result = write_resp_raw(cf, data, "\r\n", 2);
427     if(result)
428       return result;
429     stream->resp_hds_complete = TRUE;
430   }
431 
432   cb_ctx.cf = cf;
433   cb_ctx.data = data;
434   nwritten = Curl_bufq_slurp(&stream->recvbuf,
435                              stream_resp_read, &cb_ctx, &result);
436 
437   if(nwritten < 0 && result != CURLE_AGAIN) {
438     CURL_TRC_CF(data, cf, "[%"CURL_PRIu64"] recv_body error %zd",
439                 stream->id, nwritten);
440     failf(data, "Error %d in HTTP/3 response body for stream[%"CURL_PRIu64"]",
441           result, stream->id);
442     stream->closed = TRUE;
443     stream->reset = TRUE;
444     stream->send_closed = TRUE;
445     streamclose(cf->conn, "Reset of stream");
446     return result;
447   }
448   return CURLE_OK;
449 }
450 
451 #ifdef DEBUGBUILD
cf_ev_name(quiche_h3_event * ev)452 static const char *cf_ev_name(quiche_h3_event *ev)
453 {
454   switch(quiche_h3_event_type(ev)) {
455   case QUICHE_H3_EVENT_HEADERS:
456     return "HEADERS";
457   case QUICHE_H3_EVENT_DATA:
458     return "DATA";
459   case QUICHE_H3_EVENT_RESET:
460     return "RESET";
461   case QUICHE_H3_EVENT_FINISHED:
462     return "FINISHED";
463   case QUICHE_H3_EVENT_GOAWAY:
464     return "GOAWAY";
465   default:
466     return "Unknown";
467   }
468 }
469 #else
470 #define cf_ev_name(x)   ""
471 #endif
472 
h3_process_event(struct Curl_cfilter * cf,struct Curl_easy * data,struct stream_ctx * stream,quiche_h3_event * ev)473 static CURLcode h3_process_event(struct Curl_cfilter *cf,
474                                  struct Curl_easy *data,
475                                  struct stream_ctx *stream,
476                                  quiche_h3_event *ev)
477 {
478   struct cb_ctx cb_ctx;
479   CURLcode result = CURLE_OK;
480   int rc;
481 
482   if(!stream)
483     return CURLE_OK;
484   switch(quiche_h3_event_type(ev)) {
485   case QUICHE_H3_EVENT_HEADERS:
486     stream->resp_got_header = TRUE;
487     cb_ctx.cf = cf;
488     cb_ctx.data = data;
489     rc = quiche_h3_event_for_each_header(ev, cb_each_header, &cb_ctx);
490     if(rc) {
491       failf(data, "Error %d in HTTP/3 response header for stream[%"
492             CURL_PRIu64"]", rc, stream->id);
493       return CURLE_RECV_ERROR;
494     }
495     CURL_TRC_CF(data, cf, "[%"CURL_PRIu64"] <- [HEADERS]", stream->id);
496     break;
497 
498   case QUICHE_H3_EVENT_DATA:
499     if(!stream->closed) {
500       result = cf_recv_body(cf, data);
501     }
502     break;
503 
504   case QUICHE_H3_EVENT_RESET:
505     CURL_TRC_CF(data, cf, "[%"CURL_PRIu64"] RESET", stream->id);
506     stream->closed = TRUE;
507     stream->reset = TRUE;
508     stream->send_closed = TRUE;
509     streamclose(cf->conn, "Reset of stream");
510     break;
511 
512   case QUICHE_H3_EVENT_FINISHED:
513     CURL_TRC_CF(data, cf, "[%"CURL_PRIu64"] CLOSED", stream->id);
514     if(!stream->resp_hds_complete) {
515       result = write_resp_raw(cf, data, "\r\n", 2);
516       if(result)
517         return result;
518       stream->resp_hds_complete = TRUE;
519     }
520     stream->closed = TRUE;
521     streamclose(cf->conn, "End of stream");
522     break;
523 
524   case QUICHE_H3_EVENT_GOAWAY:
525     CURL_TRC_CF(data, cf, "[%"CURL_PRIu64"] <- [GOAWAY]", stream->id);
526     break;
527 
528   default:
529     CURL_TRC_CF(data, cf, "[%"CURL_PRIu64"] recv, unhandled event %d",
530                 stream->id, quiche_h3_event_type(ev));
531     break;
532   }
533   return result;
534 }
535 
cf_poll_events(struct Curl_cfilter * cf,struct Curl_easy * data)536 static CURLcode cf_poll_events(struct Curl_cfilter *cf,
537                                struct Curl_easy *data)
538 {
539   struct cf_quiche_ctx *ctx = cf->ctx;
540   struct stream_ctx *stream = NULL;
541   struct Curl_easy *sdata;
542   quiche_h3_event *ev;
543   CURLcode result;
544 
545   /* Take in the events and distribute them to the transfers. */
546   while(ctx->h3c) {
547     curl_int64_t stream3_id = quiche_h3_conn_poll(ctx->h3c, ctx->qconn, &ev);
548     if(stream3_id == QUICHE_H3_ERR_DONE) {
549       break;
550     }
551     else if(stream3_id < 0) {
552       CURL_TRC_CF(data, cf, "error poll: %"CURL_PRId64, stream3_id);
553       return CURLE_HTTP3;
554     }
555 
556     sdata = get_stream_easy(cf, data, stream3_id, &stream);
557     if(!sdata || !stream) {
558       CURL_TRC_CF(data, cf, "discard event %s for unknown [%"CURL_PRId64"]",
559                   cf_ev_name(ev), stream3_id);
560     }
561     else {
562       result = h3_process_event(cf, sdata, stream, ev);
563       drain_stream(cf, sdata);
564       if(result) {
565         CURL_TRC_CF(data, cf, "error processing event %s "
566                     "for [%"CURL_PRIu64"] -> %d", cf_ev_name(ev),
567                     stream3_id, result);
568         if(data == sdata) {
569           /* Only report this error to the caller if it is about the
570            * transfer we were called with. Otherwise we fail a transfer
571            * due to a problem in another one. */
572           quiche_h3_event_free(ev);
573           return result;
574         }
575       }
576       quiche_h3_event_free(ev);
577     }
578   }
579   return CURLE_OK;
580 }
581 
582 struct recv_ctx {
583   struct Curl_cfilter *cf;
584   struct Curl_easy *data;
585   int pkts;
586 };
587 
recv_pkt(const unsigned char * pkt,size_t pktlen,struct sockaddr_storage * remote_addr,socklen_t remote_addrlen,int ecn,void * userp)588 static CURLcode recv_pkt(const unsigned char *pkt, size_t pktlen,
589                          struct sockaddr_storage *remote_addr,
590                          socklen_t remote_addrlen, int ecn,
591                          void *userp)
592 {
593   struct recv_ctx *r = userp;
594   struct cf_quiche_ctx *ctx = r->cf->ctx;
595   quiche_recv_info recv_info;
596   ssize_t nread;
597 
598   (void)ecn;
599   ++r->pkts;
600 
601   recv_info.to = (struct sockaddr *)&ctx->q.local_addr;
602   recv_info.to_len = ctx->q.local_addrlen;
603   recv_info.from = (struct sockaddr *)remote_addr;
604   recv_info.from_len = remote_addrlen;
605 
606   nread = quiche_conn_recv(ctx->qconn, (unsigned char *)pkt, pktlen,
607                            &recv_info);
608   if(nread < 0) {
609     if(QUICHE_ERR_DONE == nread) {
610       if(quiche_conn_is_draining(ctx->qconn)) {
611         CURL_TRC_CF(r->data, r->cf, "ingress, connection is draining");
612         return CURLE_RECV_ERROR;
613       }
614       if(quiche_conn_is_closed(ctx->qconn)) {
615         CURL_TRC_CF(r->data, r->cf, "ingress, connection is closed");
616         return CURLE_RECV_ERROR;
617       }
618       CURL_TRC_CF(r->data, r->cf, "ingress, quiche is DONE");
619       return CURLE_OK;
620     }
621     else if(QUICHE_ERR_TLS_FAIL == nread) {
622       long verify_ok = SSL_get_verify_result(ctx->tls.ossl.ssl);
623       if(verify_ok != X509_V_OK) {
624         failf(r->data, "SSL certificate problem: %s",
625               X509_verify_cert_error_string(verify_ok));
626         return CURLE_PEER_FAILED_VERIFICATION;
627       }
628     }
629     else {
630       failf(r->data, "quiche_conn_recv() == %zd", nread);
631       return CURLE_RECV_ERROR;
632     }
633   }
634   else if((size_t)nread < pktlen) {
635     CURL_TRC_CF(r->data, r->cf, "ingress, quiche only read %zd/%zu bytes",
636                 nread, pktlen);
637   }
638 
639   return CURLE_OK;
640 }
641 
cf_process_ingress(struct Curl_cfilter * cf,struct Curl_easy * data)642 static CURLcode cf_process_ingress(struct Curl_cfilter *cf,
643                                    struct Curl_easy *data)
644 {
645   struct cf_quiche_ctx *ctx = cf->ctx;
646   struct recv_ctx rctx;
647   CURLcode result;
648 
649   DEBUGASSERT(ctx->qconn);
650   result = Curl_vquic_tls_before_recv(&ctx->tls, cf, data);
651   if(result)
652     return result;
653 
654   rctx.cf = cf;
655   rctx.data = data;
656   rctx.pkts = 0;
657 
658   result = vquic_recv_packets(cf, data, &ctx->q, 1000, recv_pkt, &rctx);
659   if(result)
660     return result;
661 
662   if(rctx.pkts > 0) {
663     /* quiche digested ingress packets. It might have opened flow control
664      * windows again. */
665     check_resumes(cf, data);
666   }
667   return cf_poll_events(cf, data);
668 }
669 
670 struct read_ctx {
671   struct Curl_cfilter *cf;
672   struct Curl_easy *data;
673   quiche_send_info send_info;
674 };
675 
read_pkt_to_send(void * userp,unsigned char * buf,size_t buflen,CURLcode * err)676 static ssize_t read_pkt_to_send(void *userp,
677                                 unsigned char *buf, size_t buflen,
678                                 CURLcode *err)
679 {
680   struct read_ctx *x = userp;
681   struct cf_quiche_ctx *ctx = x->cf->ctx;
682   ssize_t nwritten;
683 
684   nwritten = quiche_conn_send(ctx->qconn, buf, buflen, &x->send_info);
685   if(nwritten == QUICHE_ERR_DONE) {
686     *err = CURLE_AGAIN;
687     return -1;
688   }
689 
690   if(nwritten < 0) {
691     failf(x->data, "quiche_conn_send returned %zd", nwritten);
692     *err = CURLE_SEND_ERROR;
693     return -1;
694   }
695   *err = CURLE_OK;
696   return nwritten;
697 }
698 
699 /*
700  * flush_egress drains the buffers and sends off data.
701  * Calls failf() on errors.
702  */
cf_flush_egress(struct Curl_cfilter * cf,struct Curl_easy * data)703 static CURLcode cf_flush_egress(struct Curl_cfilter *cf,
704                                 struct Curl_easy *data)
705 {
706   struct cf_quiche_ctx *ctx = cf->ctx;
707   ssize_t nread;
708   CURLcode result;
709   curl_int64_t expiry_ns;
710   curl_int64_t timeout_ns;
711   struct read_ctx readx;
712   size_t pkt_count, gsolen;
713 
714   expiry_ns = quiche_conn_timeout_as_nanos(ctx->qconn);
715   if(!expiry_ns) {
716     quiche_conn_on_timeout(ctx->qconn);
717     if(quiche_conn_is_closed(ctx->qconn)) {
718       if(quiche_conn_is_timed_out(ctx->qconn))
719         failf(data, "connection closed by idle timeout");
720       else
721         failf(data, "connection closed by server");
722       /* Connection timed out, expire all transfers belonging to it
723        * as will not get any more POLL events here. */
724       cf_quiche_expire_conn_closed(cf, data);
725       return CURLE_SEND_ERROR;
726     }
727   }
728 
729   result = vquic_flush(cf, data, &ctx->q);
730   if(result) {
731     if(result == CURLE_AGAIN) {
732       Curl_expire(data, 1, EXPIRE_QUIC);
733       return CURLE_OK;
734     }
735     return result;
736   }
737 
738   readx.cf = cf;
739   readx.data = data;
740   memset(&readx.send_info, 0, sizeof(readx.send_info));
741   pkt_count = 0;
742   gsolen = quiche_conn_max_send_udp_payload_size(ctx->qconn);
743   for(;;) {
744     /* add the next packet to send, if any, to our buffer */
745     nread = Curl_bufq_sipn(&ctx->q.sendbuf, 0,
746                            read_pkt_to_send, &readx, &result);
747     if(nread < 0) {
748       if(result != CURLE_AGAIN)
749         return result;
750       /* Nothing more to add, flush and leave */
751       result = vquic_send(cf, data, &ctx->q, gsolen);
752       if(result) {
753         if(result == CURLE_AGAIN) {
754           Curl_expire(data, 1, EXPIRE_QUIC);
755           return CURLE_OK;
756         }
757         return result;
758       }
759       goto out;
760     }
761 
762     ++pkt_count;
763     if((size_t)nread < gsolen || pkt_count >= MAX_PKT_BURST) {
764       result = vquic_send(cf, data, &ctx->q, gsolen);
765       if(result) {
766         if(result == CURLE_AGAIN) {
767           Curl_expire(data, 1, EXPIRE_QUIC);
768           return CURLE_OK;
769         }
770         goto out;
771       }
772       pkt_count = 0;
773     }
774   }
775 
776 out:
777   timeout_ns = quiche_conn_timeout_as_nanos(ctx->qconn);
778   if(timeout_ns % 1000000)
779     timeout_ns += 1000000;
780     /* expire resolution is milliseconds */
781   Curl_expire(data, (timeout_ns / 1000000), EXPIRE_QUIC);
782   return result;
783 }
784 
recv_closed_stream(struct Curl_cfilter * cf,struct Curl_easy * data,CURLcode * err)785 static ssize_t recv_closed_stream(struct Curl_cfilter *cf,
786                                   struct Curl_easy *data,
787                                   CURLcode *err)
788 {
789   struct cf_quiche_ctx *ctx = cf->ctx;
790   struct stream_ctx *stream = H3_STREAM_CTX(ctx, data);
791   ssize_t nread = -1;
792 
793   DEBUGASSERT(stream);
794   if(stream->reset) {
795     failf(data,
796           "HTTP/3 stream %" CURL_PRIu64 " reset by server", stream->id);
797     *err = data->req.bytecount? CURLE_PARTIAL_FILE : CURLE_HTTP3;
798     CURL_TRC_CF(data, cf, "[%" CURL_PRIu64 "] cf_recv, was reset -> %d",
799                 stream->id, *err);
800   }
801   else if(!stream->resp_got_header) {
802     failf(data,
803           "HTTP/3 stream %" CURL_PRIu64 " was closed cleanly, but before "
804           "getting all response header fields, treated as error",
805           stream->id);
806     /* *err = CURLE_PARTIAL_FILE; */
807     *err = CURLE_HTTP3;
808     CURL_TRC_CF(data, cf, "[%" CURL_PRIu64 "] cf_recv, closed incomplete"
809                 " -> %d", stream->id, *err);
810   }
811   else {
812     *err = CURLE_OK;
813     nread = 0;
814   }
815   return nread;
816 }
817 
cf_quiche_recv(struct Curl_cfilter * cf,struct Curl_easy * data,char * buf,size_t len,CURLcode * err)818 static ssize_t cf_quiche_recv(struct Curl_cfilter *cf, struct Curl_easy *data,
819                               char *buf, size_t len, CURLcode *err)
820 {
821   struct cf_quiche_ctx *ctx = cf->ctx;
822   struct stream_ctx *stream = H3_STREAM_CTX(ctx, data);
823   ssize_t nread = -1;
824   CURLcode result;
825 
826   vquic_ctx_update_time(&ctx->q);
827 
828   if(!stream) {
829     *err = CURLE_RECV_ERROR;
830     return -1;
831   }
832 
833   if(!Curl_bufq_is_empty(&stream->recvbuf)) {
834     nread = Curl_bufq_read(&stream->recvbuf,
835                            (unsigned char *)buf, len, err);
836     CURL_TRC_CF(data, cf, "[%" CURL_PRIu64 "] read recvbuf(len=%zu) "
837                 "-> %zd, %d", stream->id, len, nread, *err);
838     if(nread < 0)
839       goto out;
840   }
841 
842   if(cf_process_ingress(cf, data)) {
843     CURL_TRC_CF(data, cf, "cf_recv, error on ingress");
844     *err = CURLE_RECV_ERROR;
845     nread = -1;
846     goto out;
847   }
848 
849   /* recvbuf had nothing before, maybe after progressing ingress? */
850   if(nread < 0 && !Curl_bufq_is_empty(&stream->recvbuf)) {
851     nread = Curl_bufq_read(&stream->recvbuf,
852                            (unsigned char *)buf, len, err);
853     CURL_TRC_CF(data, cf, "[%" CURL_PRIu64 "] read recvbuf(len=%zu) "
854                 "-> %zd, %d", stream->id, len, nread, *err);
855     if(nread < 0)
856       goto out;
857   }
858 
859   if(nread > 0) {
860     if(stream->closed)
861       drain_stream(cf, data);
862   }
863   else {
864     if(stream->closed) {
865       nread = recv_closed_stream(cf, data, err);
866       goto out;
867     }
868     else if(quiche_conn_is_draining(ctx->qconn)) {
869       failf(data, "QUIC connection is draining");
870       *err = CURLE_HTTP3;
871       nread = -1;
872       goto out;
873     }
874     *err = CURLE_AGAIN;
875     nread = -1;
876   }
877 
878 out:
879   result = cf_flush_egress(cf, data);
880   if(result) {
881     CURL_TRC_CF(data, cf, "cf_recv, flush egress failed");
882     *err = result;
883     nread = -1;
884   }
885   if(nread > 0)
886     ctx->data_recvd += nread;
887   CURL_TRC_CF(data, cf, "[%"CURL_PRIu64"] cf_recv(total=%"
888               CURL_FORMAT_CURL_OFF_T ") -> %zd, %d",
889               stream->id, ctx->data_recvd, nread, *err);
890   return nread;
891 }
892 
893 /* Index where :authority header field will appear in request header
894    field list. */
895 #define AUTHORITY_DST_IDX 3
896 
h3_open_stream(struct Curl_cfilter * cf,struct Curl_easy * data,const void * buf,size_t len,CURLcode * err)897 static ssize_t h3_open_stream(struct Curl_cfilter *cf,
898                               struct Curl_easy *data,
899                               const void *buf, size_t len,
900                               CURLcode *err)
901 {
902   struct cf_quiche_ctx *ctx = cf->ctx;
903   struct stream_ctx *stream = H3_STREAM_CTX(ctx, data);
904   size_t nheader, i;
905   curl_int64_t stream3_id;
906   struct dynhds h2_headers;
907   quiche_h3_header *nva = NULL;
908   ssize_t nwritten;
909 
910   if(!stream) {
911     *err = h3_data_setup(cf, data);
912     if(*err) {
913       return -1;
914     }
915     stream = H3_STREAM_CTX(ctx, data);
916     DEBUGASSERT(stream);
917   }
918 
919   Curl_dynhds_init(&h2_headers, 0, DYN_HTTP_REQUEST);
920 
921   DEBUGASSERT(stream);
922   nwritten = Curl_h1_req_parse_read(&stream->h1, buf, len, NULL, 0, err);
923   if(nwritten < 0)
924     goto out;
925   if(!stream->h1.done) {
926     /* need more data */
927     goto out;
928   }
929   DEBUGASSERT(stream->h1.req);
930 
931   *err = Curl_http_req_to_h2(&h2_headers, stream->h1.req, data);
932   if(*err) {
933     nwritten = -1;
934     goto out;
935   }
936   /* no longer needed */
937   Curl_h1_req_parse_free(&stream->h1);
938 
939   nheader = Curl_dynhds_count(&h2_headers);
940   nva = malloc(sizeof(quiche_h3_header) * nheader);
941   if(!nva) {
942     *err = CURLE_OUT_OF_MEMORY;
943     nwritten = -1;
944     goto out;
945   }
946 
947   for(i = 0; i < nheader; ++i) {
948     struct dynhds_entry *e = Curl_dynhds_getn(&h2_headers, i);
949     nva[i].name = (unsigned char *)e->name;
950     nva[i].name_len = e->namelen;
951     nva[i].value = (unsigned char *)e->value;
952     nva[i].value_len = e->valuelen;
953   }
954 
955   switch(data->state.httpreq) {
956   case HTTPREQ_POST:
957   case HTTPREQ_POST_FORM:
958   case HTTPREQ_POST_MIME:
959   case HTTPREQ_PUT:
960     if(data->state.infilesize != -1)
961       stream->upload_left = data->state.infilesize;
962     else
963       /* data sending without specifying the data amount up front */
964       stream->upload_left = -1; /* unknown */
965     break;
966   default:
967     stream->upload_left = 0; /* no request body */
968     break;
969   }
970 
971   if(stream->upload_left == 0)
972     stream->send_closed = TRUE;
973 
974   stream3_id = quiche_h3_send_request(ctx->h3c, ctx->qconn, nva, nheader,
975                                       stream->send_closed);
976   if(stream3_id < 0) {
977     if(QUICHE_H3_ERR_STREAM_BLOCKED == stream3_id) {
978       /* quiche seems to report this error if the connection window is
979        * exhausted. Which happens frequently and intermittent. */
980       CURL_TRC_CF(data, cf, "[%"CURL_PRIu64"] blocked", stream->id);
981       stream->quic_flow_blocked = TRUE;
982       *err = CURLE_AGAIN;
983       nwritten = -1;
984       goto out;
985     }
986     else {
987       CURL_TRC_CF(data, cf, "send_request(%s) -> %" CURL_PRIu64,
988                   data->state.url, stream3_id);
989     }
990     *err = CURLE_SEND_ERROR;
991     nwritten = -1;
992     goto out;
993   }
994 
995   DEBUGASSERT(!stream->opened);
996   *err = CURLE_OK;
997   stream->id = stream3_id;
998   stream->opened = TRUE;
999   stream->closed = FALSE;
1000   stream->reset = FALSE;
1001 
1002   if(Curl_trc_is_verbose(data)) {
1003     infof(data, "[HTTP/3] [%" CURL_PRIu64 "] OPENED stream for %s",
1004           stream->id, data->state.url);
1005     for(i = 0; i < nheader; ++i) {
1006       infof(data, "[HTTP/3] [%" CURL_PRIu64 "] [%.*s: %.*s]", stream->id,
1007             (int)nva[i].name_len, nva[i].name,
1008             (int)nva[i].value_len, nva[i].value);
1009     }
1010   }
1011 
1012 out:
1013   free(nva);
1014   Curl_dynhds_free(&h2_headers);
1015   return nwritten;
1016 }
1017 
cf_quiche_send(struct Curl_cfilter * cf,struct Curl_easy * data,const void * buf,size_t len,CURLcode * err)1018 static ssize_t cf_quiche_send(struct Curl_cfilter *cf, struct Curl_easy *data,
1019                               const void *buf, size_t len, CURLcode *err)
1020 {
1021   struct cf_quiche_ctx *ctx = cf->ctx;
1022   struct stream_ctx *stream = H3_STREAM_CTX(ctx, data);
1023   CURLcode result;
1024   ssize_t nwritten;
1025 
1026   vquic_ctx_update_time(&ctx->q);
1027 
1028   *err = cf_process_ingress(cf, data);
1029   if(*err) {
1030     nwritten = -1;
1031     goto out;
1032   }
1033 
1034   if(!stream || !stream->opened) {
1035     nwritten = h3_open_stream(cf, data, buf, len, err);
1036     if(nwritten < 0)
1037       goto out;
1038     stream = H3_STREAM_CTX(ctx, data);
1039   }
1040   else if(stream->closed) {
1041     if(stream->resp_hds_complete) {
1042       /* sending request body on a stream that has been closed by the
1043        * server. If the server has send us a final response, we should
1044        * silently discard the send data.
1045        * This happens for example on redirects where the server, instead
1046        * of reading the full request body just closed the stream after
1047        * sending the 30x response.
1048        * This is sort of a race: had the transfer loop called recv first,
1049        * it would see the response and stop/discard sending on its own- */
1050       CURL_TRC_CF(data, cf, "[%" CURL_PRIu64 "] discarding data"
1051                   "on closed stream with response", stream->id);
1052       *err = CURLE_OK;
1053       nwritten = (ssize_t)len;
1054       goto out;
1055     }
1056     CURL_TRC_CF(data, cf, "[%" CURL_PRIu64 "] send_body(len=%zu) "
1057                 "-> stream closed", stream->id, len);
1058     *err = CURLE_HTTP3;
1059     nwritten = -1;
1060     goto out;
1061   }
1062   else {
1063     bool eof = (stream->upload_left >= 0 &&
1064                 (curl_off_t)len >= stream->upload_left);
1065     nwritten = quiche_h3_send_body(ctx->h3c, ctx->qconn, stream->id,
1066                                    (uint8_t *)buf, len, eof);
1067     if(nwritten == QUICHE_H3_ERR_DONE || (nwritten == 0 && len > 0)) {
1068       /* TODO: we seem to be blocked on flow control and should HOLD
1069        * sending. But when do we open again? */
1070       if(!quiche_conn_stream_writable(ctx->qconn, stream->id, len)) {
1071         CURL_TRC_CF(data, cf, "[%" CURL_PRIu64 "] send_body(len=%zu) "
1072                     "-> window exhausted", stream->id, len);
1073         stream->quic_flow_blocked = TRUE;
1074       }
1075       *err = CURLE_AGAIN;
1076       nwritten = -1;
1077       goto out;
1078     }
1079     else if(nwritten == QUICHE_H3_TRANSPORT_ERR_INVALID_STREAM_STATE) {
1080       CURL_TRC_CF(data, cf, "[%" CURL_PRIu64 "] send_body(len=%zu) "
1081                   "-> invalid stream state", stream->id, len);
1082       *err = CURLE_HTTP3;
1083       nwritten = -1;
1084       goto out;
1085     }
1086     else if(nwritten == QUICHE_H3_TRANSPORT_ERR_FINAL_SIZE) {
1087       CURL_TRC_CF(data, cf, "[%" CURL_PRIu64 "] send_body(len=%zu) "
1088                   "-> exceeds size", stream->id, len);
1089       *err = CURLE_SEND_ERROR;
1090       nwritten = -1;
1091       goto out;
1092     }
1093     else if(nwritten < 0) {
1094       CURL_TRC_CF(data, cf, "[%" CURL_PRIu64 "] send_body(len=%zu) "
1095                   "-> quiche err %zd", stream->id, len, nwritten);
1096       *err = CURLE_SEND_ERROR;
1097       nwritten = -1;
1098       goto out;
1099     }
1100     else {
1101       /* quiche accepted all or at least a part of the buf */
1102       if(stream->upload_left > 0) {
1103         stream->upload_left = (nwritten < stream->upload_left)?
1104                               (stream->upload_left - nwritten) : 0;
1105       }
1106       if(stream->upload_left == 0)
1107         stream->send_closed = TRUE;
1108 
1109       CURL_TRC_CF(data, cf, "[%" CURL_PRIu64 "] send body(len=%zu, "
1110                   "left=%" CURL_FORMAT_CURL_OFF_T ") -> %zd",
1111                   stream->id, len, stream->upload_left, nwritten);
1112       *err = CURLE_OK;
1113     }
1114   }
1115 
1116 out:
1117   result = cf_flush_egress(cf, data);
1118   if(result) {
1119     *err = result;
1120     nwritten = -1;
1121   }
1122   CURL_TRC_CF(data, cf, "[%" CURL_PRIu64 "] cf_send(len=%zu) -> %zd, %d",
1123               stream? stream->id : -1, len, nwritten, *err);
1124   return nwritten;
1125 }
1126 
stream_is_writeable(struct Curl_cfilter * cf,struct Curl_easy * data)1127 static bool stream_is_writeable(struct Curl_cfilter *cf,
1128                                 struct Curl_easy *data)
1129 {
1130   struct cf_quiche_ctx *ctx = cf->ctx;
1131   struct stream_ctx *stream = H3_STREAM_CTX(ctx, data);
1132 
1133   return stream && (quiche_conn_stream_writable(
1134     ctx->qconn, (curl_uint64_t)stream->id, 1) > 0);
1135 }
1136 
cf_quiche_adjust_pollset(struct Curl_cfilter * cf,struct Curl_easy * data,struct easy_pollset * ps)1137 static void cf_quiche_adjust_pollset(struct Curl_cfilter *cf,
1138                                      struct Curl_easy *data,
1139                                      struct easy_pollset *ps)
1140 {
1141   struct cf_quiche_ctx *ctx = cf->ctx;
1142   bool want_recv, want_send;
1143 
1144   if(!ctx->qconn)
1145     return;
1146 
1147   Curl_pollset_check(data, ps, ctx->q.sockfd, &want_recv, &want_send);
1148   if(want_recv || want_send) {
1149     struct stream_ctx *stream = H3_STREAM_CTX(ctx, data);
1150     bool c_exhaust, s_exhaust;
1151 
1152     c_exhaust = FALSE; /* Have not found any call in quiche that tells
1153                           us if the connection itself is blocked */
1154     s_exhaust = want_send && stream && stream->opened &&
1155                 (stream->quic_flow_blocked || !stream_is_writeable(cf, data));
1156     want_recv = (want_recv || c_exhaust || s_exhaust);
1157     want_send = (!s_exhaust && want_send) ||
1158                  !Curl_bufq_is_empty(&ctx->q.sendbuf);
1159 
1160     Curl_pollset_set(data, ps, ctx->q.sockfd, want_recv, want_send);
1161   }
1162 }
1163 
1164 /*
1165  * Called from transfer.c:data_pending to know if we should keep looping
1166  * to receive more data from the connection.
1167  */
cf_quiche_data_pending(struct Curl_cfilter * cf,const struct Curl_easy * data)1168 static bool cf_quiche_data_pending(struct Curl_cfilter *cf,
1169                                    const struct Curl_easy *data)
1170 {
1171   struct cf_quiche_ctx *ctx = cf->ctx;
1172   const struct stream_ctx *stream = H3_STREAM_CTX(ctx, data);
1173   (void)cf;
1174   return stream && !Curl_bufq_is_empty(&stream->recvbuf);
1175 }
1176 
h3_data_pause(struct Curl_cfilter * cf,struct Curl_easy * data,bool pause)1177 static CURLcode h3_data_pause(struct Curl_cfilter *cf,
1178                               struct Curl_easy *data,
1179                               bool pause)
1180 {
1181   /* TODO: there seems right now no API in quiche to shrink/enlarge
1182    * the streams windows. As we do in HTTP/2. */
1183   if(!pause) {
1184     drain_stream(cf, data);
1185     Curl_expire(data, 0, EXPIRE_RUN_NOW);
1186   }
1187   return CURLE_OK;
1188 }
1189 
cf_quiche_data_event(struct Curl_cfilter * cf,struct Curl_easy * data,int event,int arg1,void * arg2)1190 static CURLcode cf_quiche_data_event(struct Curl_cfilter *cf,
1191                                      struct Curl_easy *data,
1192                                      int event, int arg1, void *arg2)
1193 {
1194   struct cf_quiche_ctx *ctx = cf->ctx;
1195   CURLcode result = CURLE_OK;
1196 
1197   (void)arg1;
1198   (void)arg2;
1199   switch(event) {
1200   case CF_CTRL_DATA_SETUP:
1201     break;
1202   case CF_CTRL_DATA_PAUSE:
1203     result = h3_data_pause(cf, data, (arg1 != 0));
1204     break;
1205   case CF_CTRL_DATA_DETACH:
1206     h3_data_done(cf, data);
1207     break;
1208   case CF_CTRL_DATA_DONE:
1209     h3_data_done(cf, data);
1210     break;
1211   case CF_CTRL_DATA_DONE_SEND: {
1212     struct stream_ctx *stream = H3_STREAM_CTX(ctx, data);
1213     if(stream && !stream->send_closed) {
1214       unsigned char body[1];
1215       ssize_t sent;
1216 
1217       stream->send_closed = TRUE;
1218       stream->upload_left = 0;
1219       body[0] = 'X';
1220       sent = cf_quiche_send(cf, data, body, 0, &result);
1221       CURL_TRC_CF(data, cf, "[%"CURL_PRIu64"] DONE_SEND -> %zd, %d",
1222                   stream->id, sent, result);
1223     }
1224     break;
1225   }
1226   case CF_CTRL_DATA_IDLE: {
1227     struct stream_ctx *stream = H3_STREAM_CTX(ctx, data);
1228     if(stream && !stream->closed) {
1229       result = cf_flush_egress(cf, data);
1230       if(result)
1231         CURL_TRC_CF(data, cf, "data idle, flush egress -> %d", result);
1232     }
1233     break;
1234   }
1235   default:
1236     break;
1237   }
1238   return result;
1239 }
1240 
cf_connect_start(struct Curl_cfilter * cf,struct Curl_easy * data)1241 static CURLcode cf_connect_start(struct Curl_cfilter *cf,
1242                                  struct Curl_easy *data)
1243 {
1244   struct cf_quiche_ctx *ctx = cf->ctx;
1245   int rv;
1246   CURLcode result;
1247   const struct Curl_sockaddr_ex *sockaddr;
1248 
1249   DEBUGASSERT(ctx->q.sockfd != CURL_SOCKET_BAD);
1250 
1251 #ifdef DEBUG_QUICHE
1252   /* initialize debug log callback only once */
1253   static int debug_log_init = 0;
1254   if(!debug_log_init) {
1255     quiche_enable_debug_logging(quiche_debug_log, NULL);
1256     debug_log_init = 1;
1257   }
1258 #endif
1259   Curl_bufcp_init(&ctx->stream_bufcp, H3_STREAM_CHUNK_SIZE,
1260                   H3_STREAM_POOL_SPARES);
1261   Curl_hash_offt_init(&ctx->streams, 63, h3_stream_hash_free);
1262   ctx->data_recvd = 0;
1263 
1264   result = vquic_ctx_init(&ctx->q);
1265   if(result)
1266     return result;
1267 
1268   result = Curl_ssl_peer_init(&ctx->peer, cf, TRNSPRT_QUIC);
1269   if(result)
1270     return result;
1271 
1272   ctx->cfg = quiche_config_new(QUICHE_PROTOCOL_VERSION);
1273   if(!ctx->cfg) {
1274     failf(data, "can't create quiche config");
1275     return CURLE_FAILED_INIT;
1276   }
1277   quiche_config_enable_pacing(ctx->cfg, false);
1278   quiche_config_set_max_idle_timeout(ctx->cfg, CURL_QUIC_MAX_IDLE_MS);
1279   quiche_config_set_initial_max_data(ctx->cfg, (1 * 1024 * 1024)
1280     /* (QUIC_MAX_STREAMS/2) * H3_STREAM_WINDOW_SIZE */);
1281   quiche_config_set_initial_max_streams_bidi(ctx->cfg, QUIC_MAX_STREAMS);
1282   quiche_config_set_initial_max_streams_uni(ctx->cfg, QUIC_MAX_STREAMS);
1283   quiche_config_set_initial_max_stream_data_bidi_local(ctx->cfg,
1284     H3_STREAM_WINDOW_SIZE);
1285   quiche_config_set_initial_max_stream_data_bidi_remote(ctx->cfg,
1286     H3_STREAM_WINDOW_SIZE);
1287   quiche_config_set_initial_max_stream_data_uni(ctx->cfg,
1288     H3_STREAM_WINDOW_SIZE);
1289   quiche_config_set_disable_active_migration(ctx->cfg, TRUE);
1290 
1291   quiche_config_set_max_connection_window(ctx->cfg,
1292     10 * QUIC_MAX_STREAMS * H3_STREAM_WINDOW_SIZE);
1293   quiche_config_set_max_stream_window(ctx->cfg, 10 * H3_STREAM_WINDOW_SIZE);
1294   quiche_config_set_application_protos(ctx->cfg,
1295                                        (uint8_t *)
1296                                        QUICHE_H3_APPLICATION_PROTOCOL,
1297                                        sizeof(QUICHE_H3_APPLICATION_PROTOCOL)
1298                                        - 1);
1299 
1300   result = Curl_vquic_tls_init(&ctx->tls, cf, data, &ctx->peer,
1301                                QUICHE_H3_APPLICATION_PROTOCOL,
1302                                sizeof(QUICHE_H3_APPLICATION_PROTOCOL) - 1,
1303                                NULL, NULL, cf);
1304   if(result)
1305     return result;
1306 
1307   result = Curl_rand(data, ctx->scid, sizeof(ctx->scid));
1308   if(result)
1309     return result;
1310 
1311   Curl_cf_socket_peek(cf->next, data, &ctx->q.sockfd, &sockaddr, NULL);
1312   ctx->q.local_addrlen = sizeof(ctx->q.local_addr);
1313   rv = getsockname(ctx->q.sockfd, (struct sockaddr *)&ctx->q.local_addr,
1314                    &ctx->q.local_addrlen);
1315   if(rv == -1)
1316     return CURLE_QUIC_CONNECT_ERROR;
1317 
1318   ctx->qconn = quiche_conn_new_with_tls((const uint8_t *)ctx->scid,
1319                                       sizeof(ctx->scid), NULL, 0,
1320                                       (struct sockaddr *)&ctx->q.local_addr,
1321                                       ctx->q.local_addrlen,
1322                                       &sockaddr->sa_addr, sockaddr->addrlen,
1323                                       ctx->cfg, ctx->tls.ossl.ssl, false);
1324   if(!ctx->qconn) {
1325     failf(data, "can't create quiche connection");
1326     return CURLE_OUT_OF_MEMORY;
1327   }
1328 
1329   /* Known to not work on Windows */
1330 #if !defined(_WIN32) && defined(HAVE_QUICHE_CONN_SET_QLOG_FD)
1331   {
1332     int qfd;
1333     (void)Curl_qlogdir(data, ctx->scid, sizeof(ctx->scid), &qfd);
1334     if(qfd != -1)
1335       quiche_conn_set_qlog_fd(ctx->qconn, qfd,
1336                               "qlog title", "curl qlog");
1337   }
1338 #endif
1339 
1340   result = cf_flush_egress(cf, data);
1341   if(result)
1342     return result;
1343 
1344   {
1345     unsigned char alpn_protocols[] = QUICHE_H3_APPLICATION_PROTOCOL;
1346     unsigned alpn_len, offset = 0;
1347 
1348     /* Replace each ALPN length prefix by a comma. */
1349     while(offset < sizeof(alpn_protocols) - 1) {
1350       alpn_len = alpn_protocols[offset];
1351       alpn_protocols[offset] = ',';
1352       offset += 1 + alpn_len;
1353     }
1354 
1355     CURL_TRC_CF(data, cf, "Sent QUIC client Initial, ALPN: %s",
1356                 alpn_protocols + 1);
1357   }
1358 
1359   return CURLE_OK;
1360 }
1361 
cf_quiche_verify_peer(struct Curl_cfilter * cf,struct Curl_easy * data)1362 static CURLcode cf_quiche_verify_peer(struct Curl_cfilter *cf,
1363                                       struct Curl_easy *data)
1364 {
1365   struct cf_quiche_ctx *ctx = cf->ctx;
1366 
1367   cf->conn->bits.multiplex = TRUE; /* at least potentially multiplexed */
1368   cf->conn->httpversion = 30;
1369   cf->conn->bundle->multiuse = BUNDLE_MULTIPLEX;
1370 
1371   return Curl_vquic_tls_verify_peer(&ctx->tls, cf, data, &ctx->peer);
1372 }
1373 
cf_quiche_connect(struct Curl_cfilter * cf,struct Curl_easy * data,bool blocking,bool * done)1374 static CURLcode cf_quiche_connect(struct Curl_cfilter *cf,
1375                                   struct Curl_easy *data,
1376                                   bool blocking, bool *done)
1377 {
1378   struct cf_quiche_ctx *ctx = cf->ctx;
1379   CURLcode result = CURLE_OK;
1380 
1381   if(cf->connected) {
1382     *done = TRUE;
1383     return CURLE_OK;
1384   }
1385 
1386   /* Connect the UDP filter first */
1387   if(!cf->next->connected) {
1388     result = Curl_conn_cf_connect(cf->next, data, blocking, done);
1389     if(result || !*done)
1390       return result;
1391   }
1392 
1393   *done = FALSE;
1394   vquic_ctx_update_time(&ctx->q);
1395 
1396   if(ctx->reconnect_at.tv_sec &&
1397      Curl_timediff(ctx->q.last_op, ctx->reconnect_at) < 0) {
1398     /* Not time yet to attempt the next connect */
1399     CURL_TRC_CF(data, cf, "waiting for reconnect time");
1400     goto out;
1401   }
1402 
1403   if(!ctx->qconn) {
1404     result = cf_connect_start(cf, data);
1405     if(result)
1406       goto out;
1407     ctx->started_at = ctx->q.last_op;
1408     result = cf_flush_egress(cf, data);
1409     /* we do not expect to be able to recv anything yet */
1410     goto out;
1411   }
1412 
1413   result = cf_process_ingress(cf, data);
1414   if(result)
1415     goto out;
1416 
1417   result = cf_flush_egress(cf, data);
1418   if(result)
1419     goto out;
1420 
1421   if(quiche_conn_is_established(ctx->qconn)) {
1422     ctx->handshake_at = ctx->q.last_op;
1423     CURL_TRC_CF(data, cf, "handshake complete after %dms",
1424                 (int)Curl_timediff(ctx->handshake_at, ctx->started_at));
1425     result = cf_quiche_verify_peer(cf, data);
1426     if(!result) {
1427       CURL_TRC_CF(data, cf, "peer verified");
1428       ctx->h3config = quiche_h3_config_new();
1429       if(!ctx->h3config) {
1430         result = CURLE_OUT_OF_MEMORY;
1431         goto out;
1432       }
1433 
1434       /* Create a new HTTP/3 connection on the QUIC connection. */
1435       ctx->h3c = quiche_h3_conn_new_with_transport(ctx->qconn, ctx->h3config);
1436       if(!ctx->h3c) {
1437         result = CURLE_OUT_OF_MEMORY;
1438         goto out;
1439       }
1440       cf->connected = TRUE;
1441       cf->conn->alpn = CURL_HTTP_VERSION_3;
1442       *done = TRUE;
1443       connkeep(cf->conn, "HTTP/3 default");
1444     }
1445   }
1446   else if(quiche_conn_is_draining(ctx->qconn)) {
1447     /* When a QUIC server instance is shutting down, it may send us a
1448      * CONNECTION_CLOSE right away. Our connection then enters the DRAINING
1449      * state. The CONNECT may work in the near future again. Indicate
1450      * that as a "weird" reply. */
1451     result = CURLE_WEIRD_SERVER_REPLY;
1452   }
1453 
1454 out:
1455 #ifndef CURL_DISABLE_VERBOSE_STRINGS
1456   if(result && result != CURLE_AGAIN) {
1457     struct ip_quadruple ip;
1458 
1459     Curl_cf_socket_peek(cf->next, data, NULL, NULL, &ip);
1460     infof(data, "connect to %s port %u failed: %s",
1461           ip.remote_ip, ip.remote_port, curl_easy_strerror(result));
1462   }
1463 #endif
1464   return result;
1465 }
1466 
cf_quiche_close(struct Curl_cfilter * cf,struct Curl_easy * data)1467 static void cf_quiche_close(struct Curl_cfilter *cf, struct Curl_easy *data)
1468 {
1469   struct cf_quiche_ctx *ctx = cf->ctx;
1470 
1471   if(ctx) {
1472     if(ctx->qconn) {
1473       vquic_ctx_update_time(&ctx->q);
1474       (void)quiche_conn_close(ctx->qconn, TRUE, 0, NULL, 0);
1475       /* flushing the egress is not a failsafe way to deliver all the
1476          outstanding packets, but we also don't want to get stuck here... */
1477       (void)cf_flush_egress(cf, data);
1478     }
1479     cf_quiche_ctx_clear(ctx);
1480   }
1481 }
1482 
cf_quiche_destroy(struct Curl_cfilter * cf,struct Curl_easy * data)1483 static void cf_quiche_destroy(struct Curl_cfilter *cf, struct Curl_easy *data)
1484 {
1485   struct cf_quiche_ctx *ctx = cf->ctx;
1486 
1487   (void)data;
1488   cf_quiche_ctx_clear(ctx);
1489   free(ctx);
1490   cf->ctx = NULL;
1491 }
1492 
cf_quiche_query(struct Curl_cfilter * cf,struct Curl_easy * data,int query,int * pres1,void * pres2)1493 static CURLcode cf_quiche_query(struct Curl_cfilter *cf,
1494                                 struct Curl_easy *data,
1495                                 int query, int *pres1, void *pres2)
1496 {
1497   struct cf_quiche_ctx *ctx = cf->ctx;
1498 
1499   switch(query) {
1500   case CF_QUERY_MAX_CONCURRENT: {
1501     curl_uint64_t max_streams = CONN_INUSE(cf->conn);
1502     if(!ctx->goaway) {
1503       max_streams += quiche_conn_peer_streams_left_bidi(ctx->qconn);
1504     }
1505     *pres1 = (max_streams > INT_MAX)? INT_MAX : (int)max_streams;
1506     CURL_TRC_CF(data, cf, "query conn[%" CURL_FORMAT_CURL_OFF_T "]: "
1507                 "MAX_CONCURRENT -> %d (%zu in use)",
1508                 cf->conn->connection_id, *pres1, CONN_INUSE(cf->conn));
1509     return CURLE_OK;
1510   }
1511   case CF_QUERY_CONNECT_REPLY_MS:
1512     if(ctx->q.got_first_byte) {
1513       timediff_t ms = Curl_timediff(ctx->q.first_byte_at, ctx->started_at);
1514       *pres1 = (ms < INT_MAX)? (int)ms : INT_MAX;
1515     }
1516     else
1517       *pres1 = -1;
1518     return CURLE_OK;
1519   case CF_QUERY_TIMER_CONNECT: {
1520     struct curltime *when = pres2;
1521     if(ctx->q.got_first_byte)
1522       *when = ctx->q.first_byte_at;
1523     return CURLE_OK;
1524   }
1525   case CF_QUERY_TIMER_APPCONNECT: {
1526     struct curltime *when = pres2;
1527     if(cf->connected)
1528       *when = ctx->handshake_at;
1529     return CURLE_OK;
1530   }
1531   default:
1532     break;
1533   }
1534   return cf->next?
1535     cf->next->cft->query(cf->next, data, query, pres1, pres2) :
1536     CURLE_UNKNOWN_OPTION;
1537 }
1538 
cf_quiche_conn_is_alive(struct Curl_cfilter * cf,struct Curl_easy * data,bool * input_pending)1539 static bool cf_quiche_conn_is_alive(struct Curl_cfilter *cf,
1540                                     struct Curl_easy *data,
1541                                     bool *input_pending)
1542 {
1543   struct cf_quiche_ctx *ctx = cf->ctx;
1544   bool alive = TRUE;
1545 
1546   *input_pending = FALSE;
1547   if(!ctx->qconn)
1548     return FALSE;
1549 
1550   if(quiche_conn_is_closed(ctx->qconn)) {
1551     if(quiche_conn_is_timed_out(ctx->qconn))
1552       CURL_TRC_CF(data, cf, "connection was closed due to idle timeout");
1553     else
1554       CURL_TRC_CF(data, cf, "connection is closed");
1555     return FALSE;
1556   }
1557 
1558   if(!cf->next || !cf->next->cft->is_alive(cf->next, data, input_pending))
1559     return FALSE;
1560 
1561   if(*input_pending) {
1562     /* This happens before we've sent off a request and the connection is
1563        not in use by any other transfer, there shouldn't be any data here,
1564        only "protocol frames" */
1565     *input_pending = FALSE;
1566     if(cf_process_ingress(cf, data))
1567       alive = FALSE;
1568     else {
1569       alive = TRUE;
1570     }
1571   }
1572 
1573   return alive;
1574 }
1575 
1576 struct Curl_cftype Curl_cft_http3 = {
1577   "HTTP/3",
1578   CF_TYPE_IP_CONNECT | CF_TYPE_SSL | CF_TYPE_MULTIPLEX,
1579   0,
1580   cf_quiche_destroy,
1581   cf_quiche_connect,
1582   cf_quiche_close,
1583   Curl_cf_def_get_host,
1584   cf_quiche_adjust_pollset,
1585   cf_quiche_data_pending,
1586   cf_quiche_send,
1587   cf_quiche_recv,
1588   cf_quiche_data_event,
1589   cf_quiche_conn_is_alive,
1590   Curl_cf_def_conn_keep_alive,
1591   cf_quiche_query,
1592 };
1593 
Curl_cf_quiche_create(struct Curl_cfilter ** pcf,struct Curl_easy * data,struct connectdata * conn,const struct Curl_addrinfo * ai)1594 CURLcode Curl_cf_quiche_create(struct Curl_cfilter **pcf,
1595                                struct Curl_easy *data,
1596                                struct connectdata *conn,
1597                                const struct Curl_addrinfo *ai)
1598 {
1599   struct cf_quiche_ctx *ctx = NULL;
1600   struct Curl_cfilter *cf = NULL, *udp_cf = NULL;
1601   CURLcode result;
1602 
1603   (void)data;
1604   (void)conn;
1605   ctx = calloc(1, sizeof(*ctx));
1606   if(!ctx) {
1607     result = CURLE_OUT_OF_MEMORY;
1608     goto out;
1609   }
1610 
1611   result = Curl_cf_create(&cf, &Curl_cft_http3, ctx);
1612   if(result)
1613     goto out;
1614 
1615   result = Curl_cf_udp_create(&udp_cf, data, conn, ai, TRNSPRT_QUIC);
1616   if(result)
1617     goto out;
1618 
1619   udp_cf->conn = cf->conn;
1620   udp_cf->sockindex = cf->sockindex;
1621   cf->next = udp_cf;
1622 
1623 out:
1624   *pcf = (!result)? cf : NULL;
1625   if(result) {
1626     if(udp_cf)
1627       Curl_conn_cf_discard_sub(cf, udp_cf, data, TRUE);
1628     Curl_safefree(cf);
1629     Curl_safefree(ctx);
1630   }
1631 
1632   return result;
1633 }
1634 
Curl_conn_is_quiche(const struct Curl_easy * data,const struct connectdata * conn,int sockindex)1635 bool Curl_conn_is_quiche(const struct Curl_easy *data,
1636                          const struct connectdata *conn,
1637                          int sockindex)
1638 {
1639   struct Curl_cfilter *cf = conn? conn->cfilter[sockindex] : NULL;
1640 
1641   (void)data;
1642   for(; cf; cf = cf->next) {
1643     if(cf->cft == &Curl_cft_http3)
1644       return TRUE;
1645     if(cf->cft->flags & CF_TYPE_IP_CONNECT)
1646       return FALSE;
1647   }
1648   return FALSE;
1649 }
1650 
1651 #endif
1652