/src/curl/lib/vquic/vquic.c
Line | Count | Source |
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 | | #include "curl_setup.h" |
25 | | #include "urldata.h" |
26 | | #include "vquic/vquic.h" |
27 | | |
28 | | #include "curl_trc.h" |
29 | | |
30 | | #if !defined(CURL_DISABLE_HTTP) && defined(USE_HTTP3) |
31 | | |
32 | | #ifdef HAVE_NETINET_UDP_H |
33 | | #include <netinet/udp.h> |
34 | | #endif |
35 | | |
36 | | #ifdef USE_NGHTTP3 |
37 | | #include <nghttp3/nghttp3.h> |
38 | | #endif |
39 | | |
40 | | #include "bufq.h" |
41 | | #include "curlx/dynbuf.h" |
42 | | #include "curlx/fopen.h" |
43 | | #include "cfilters.h" |
44 | | #include "vquic/curl_ngtcp2.h" |
45 | | #include "vquic/curl_quiche.h" |
46 | | #include "multiif.h" |
47 | | #include "progress.h" |
48 | | #include "rand.h" |
49 | | #include "vquic/vquic_int.h" |
50 | | #include "curlx/strerr.h" |
51 | | #include "curlx/strparse.h" |
52 | | |
53 | | |
54 | | #define NW_CHUNK_SIZE (64 * 1024) |
55 | | #define NW_SEND_CHUNKS 1 |
56 | | |
57 | | int Curl_vquic_init(void) |
58 | | { |
59 | | #if defined(USE_NGTCP2) && defined(OPENSSL_QUIC_API2) |
60 | | if(ngtcp2_crypto_ossl_init()) |
61 | | return 0; |
62 | | #endif |
63 | | |
64 | | return 1; |
65 | | } |
66 | | |
67 | | void Curl_quic_ver(char *p, size_t len) |
68 | | { |
69 | | #if defined(USE_NGTCP2) && defined(USE_NGHTTP3) |
70 | | Curl_ngtcp2_ver(p, len); |
71 | | #elif defined(USE_QUICHE) |
72 | | Curl_quiche_ver(p, len); |
73 | | #endif |
74 | | } |
75 | | |
76 | | CURLcode vquic_ctx_init(struct Curl_easy *data, |
77 | | struct cf_quic_ctx *qctx) |
78 | | { |
79 | | Curl_bufq_init2(&qctx->sendbuf, NW_CHUNK_SIZE, NW_SEND_CHUNKS, |
80 | | BUFQ_OPT_SOFT_LIMIT); |
81 | | #if defined(__linux__) && defined(UDP_SEGMENT) && defined(HAVE_SENDMSG) |
82 | | qctx->no_gso = FALSE; |
83 | | #else |
84 | | qctx->no_gso = TRUE; |
85 | | #endif |
86 | | #ifdef DEBUGBUILD |
87 | | { |
88 | | const char *p = getenv("CURL_DBG_QUIC_WBLOCK"); |
89 | | if(p) { |
90 | | curl_off_t l; |
91 | | if(!curlx_str_number(&p, &l, 100)) |
92 | | qctx->wblock_percent = (int)l; |
93 | | } |
94 | | } |
95 | | #endif |
96 | | vquic_ctx_set_time(qctx, Curl_pgrs_now(data)); |
97 | | |
98 | | return CURLE_OK; |
99 | | } |
100 | | |
101 | | void vquic_ctx_free(struct cf_quic_ctx *qctx) |
102 | | { |
103 | | Curl_bufq_free(&qctx->sendbuf); |
104 | | } |
105 | | |
106 | | void vquic_ctx_set_time(struct cf_quic_ctx *qctx, |
107 | | const struct curltime *pnow) |
108 | | { |
109 | | qctx->last_op = *pnow; |
110 | | } |
111 | | |
112 | | void vquic_ctx_update_time(struct cf_quic_ctx *qctx, |
113 | | const struct curltime *pnow) |
114 | | { |
115 | | qctx->last_op = *pnow; |
116 | | } |
117 | | |
118 | | static CURLcode send_packet_no_gso(struct Curl_cfilter *cf, |
119 | | struct Curl_easy *data, |
120 | | struct cf_quic_ctx *qctx, |
121 | | const uint8_t *pkt, size_t pktlen, |
122 | | size_t gsolen, size_t *psent); |
123 | | |
124 | | static CURLcode do_sendmsg(struct Curl_cfilter *cf, |
125 | | struct Curl_easy *data, |
126 | | struct cf_quic_ctx *qctx, |
127 | | const uint8_t *pkt, size_t pktlen, size_t gsolen, |
128 | | size_t *psent) |
129 | | { |
130 | | CURLcode result = CURLE_OK; |
131 | | #ifdef HAVE_SENDMSG |
132 | | struct iovec msg_iov; |
133 | | struct msghdr msg = { 0 }; |
134 | | ssize_t rv; |
135 | | #if defined(__linux__) && defined(UDP_SEGMENT) |
136 | | uint8_t msg_ctrl[32]; |
137 | | struct cmsghdr *cm; |
138 | | #endif |
139 | | |
140 | | *psent = 0; |
141 | | msg_iov.iov_base = (uint8_t *)CURL_UNCONST(pkt); |
142 | | msg_iov.iov_len = pktlen; |
143 | | msg.msg_iov = &msg_iov; |
144 | | msg.msg_iovlen = 1; |
145 | | |
146 | | #if defined(__linux__) && defined(UDP_SEGMENT) |
147 | | if(pktlen > gsolen) { |
148 | | /* Only set this, when we need it. macOS, for example, |
149 | | * does not seem to like a msg_control of length 0. */ |
150 | | memset(msg_ctrl, 0, sizeof(msg_ctrl)); |
151 | | msg.msg_control = msg_ctrl; |
152 | | assert(sizeof(msg_ctrl) >= CMSG_SPACE(sizeof(int))); |
153 | | msg.msg_controllen = CMSG_SPACE(sizeof(int)); |
154 | | cm = CMSG_FIRSTHDR(&msg); |
155 | | cm->cmsg_level = SOL_UDP; |
156 | | cm->cmsg_type = UDP_SEGMENT; |
157 | | cm->cmsg_len = CMSG_LEN(sizeof(uint16_t)); |
158 | | *(uint16_t *)(void *)CMSG_DATA(cm) = gsolen & 0xffff; |
159 | | } |
160 | | #endif |
161 | | |
162 | | while((rv = sendmsg(qctx->sockfd, &msg, 0)) == -1 && SOCKERRNO == SOCKEINTR) |
163 | | ; |
164 | | |
165 | | if(!curlx_sztouz(rv, psent)) { |
166 | | switch(SOCKERRNO) { |
167 | | case EAGAIN: |
168 | | #if EAGAIN != SOCKEWOULDBLOCK |
169 | | case SOCKEWOULDBLOCK: |
170 | | #endif |
171 | | return CURLE_AGAIN; |
172 | | case SOCKEMSGSIZE: |
173 | | /* UDP datagram is too large; caused by PMTUD. Let it be lost. */ |
174 | | *psent = pktlen; |
175 | | break; |
176 | | case EIO: |
177 | | if(pktlen > gsolen) { |
178 | | /* GSO failure */ |
179 | | infof(data, "sendmsg() returned %zd (errno %d); disable GSO", rv, |
180 | | SOCKERRNO); |
181 | | qctx->no_gso = TRUE; |
182 | | return send_packet_no_gso(cf, data, qctx, pkt, pktlen, gsolen, psent); |
183 | | } |
184 | | FALLTHROUGH(); |
185 | | default: |
186 | | failf(data, "sendmsg() returned %zd (errno %d)", rv, SOCKERRNO); |
187 | | result = CURLE_SEND_ERROR; |
188 | | goto out; |
189 | | } |
190 | | } |
191 | | else if(pktlen != *psent) { |
192 | | failf(data, "sendmsg() sent only %zu/%zu bytes", *psent, pktlen); |
193 | | result = CURLE_SEND_ERROR; |
194 | | goto out; |
195 | | } |
196 | | #else |
197 | | ssize_t rv; |
198 | | (void)gsolen; |
199 | | |
200 | | *psent = 0; |
201 | | |
202 | | while((rv = swrite(qctx->sockfd, pkt, pktlen)) == -1 && |
203 | | SOCKERRNO == SOCKEINTR) |
204 | | ; |
205 | | |
206 | | if(!curlx_sztouz(rv, psent)) { |
207 | | if(SOCKERRNO == EAGAIN || SOCKERRNO == SOCKEWOULDBLOCK) { |
208 | | result = CURLE_AGAIN; |
209 | | goto out; |
210 | | } |
211 | | else { |
212 | | if(SOCKERRNO != SOCKEMSGSIZE) { |
213 | | failf(data, "send() returned %zd (errno %d)", rv, SOCKERRNO); |
214 | | result = CURLE_SEND_ERROR; |
215 | | goto out; |
216 | | } |
217 | | /* UDP datagram is too large; caused by PMTUD. Let it be lost. */ |
218 | | *psent = pktlen; |
219 | | } |
220 | | } |
221 | | #endif |
222 | | (void)cf; |
223 | | |
224 | | out: |
225 | | return result; |
226 | | } |
227 | | |
228 | | #ifdef CURLVERBOSE |
229 | | #ifdef HAVE_SENDMSG |
230 | | #define VQUIC_SEND_METHOD "sendmsg" |
231 | | #else |
232 | | #define VQUIC_SEND_METHOD "send" |
233 | | #endif |
234 | | #endif |
235 | | |
236 | | static CURLcode send_packet_no_gso(struct Curl_cfilter *cf, |
237 | | struct Curl_easy *data, |
238 | | struct cf_quic_ctx *qctx, |
239 | | const uint8_t *pkt, size_t pktlen, |
240 | | size_t gsolen, size_t *psent) |
241 | | { |
242 | | const uint8_t *p, *end = pkt + pktlen; |
243 | | size_t sent, len; |
244 | | CURLcode result = CURLE_OK; |
245 | | VERBOSE(size_t calls = 0); |
246 | | |
247 | | *psent = 0; |
248 | | |
249 | | for(p = pkt; p < end; p += gsolen) { |
250 | | len = CURLMIN(gsolen, (size_t)(end - p)); |
251 | | result = do_sendmsg(cf, data, qctx, p, len, len, &sent); |
252 | | if(result) |
253 | | goto out; |
254 | | *psent += sent; |
255 | | VERBOSE(++calls); |
256 | | } |
257 | | out: |
258 | | CURL_TRC_CF(data, cf, |
259 | | "vquic_%s(len=%zu, gso=%zu, calls=%zu) -> %d, sent=%zu", |
260 | | VQUIC_SEND_METHOD, pktlen, gsolen, calls, result, *psent); |
261 | | return result; |
262 | | } |
263 | | |
264 | | /* Split QUIC payload by datagram (gso) boundaries when sending over a |
265 | | * non-UDP lower filter (for example CONNECT-UDP proxy tunnel). */ |
266 | | static CURLcode send_packet_no_gso_cf(struct Curl_cfilter *cf, |
267 | | struct Curl_easy *data, |
268 | | const uint8_t *pkt, size_t pktlen, |
269 | | size_t gsolen, size_t *psent) |
270 | | { |
271 | | const uint8_t *p, *end = pkt + pktlen; |
272 | | size_t sent, len; |
273 | | CURLcode result = CURLE_OK; |
274 | | VERBOSE(size_t calls = 0); |
275 | | |
276 | | *psent = 0; |
277 | | |
278 | | /* Send one datagram-sized chunk per call into the lower filter. */ |
279 | | for(p = pkt; p < end; p += len) { |
280 | | len = CURLMIN(gsolen, (size_t)(end - p)); |
281 | | result = Curl_conn_cf_send(cf->next, data, p, len, FALSE, &sent); |
282 | | /* Report forward progress even if we return CURLE_AGAIN later. */ |
283 | | *psent += sent; |
284 | | VERBOSE(++calls); |
285 | | /* Preserve lower-filter errors (including CURLE_AGAIN). */ |
286 | | if(result) |
287 | | goto out; |
288 | | if(sent < len) { |
289 | | /* We need whole datagrams here. Partial accept means blocked. */ |
290 | | result = CURLE_AGAIN; |
291 | | goto out; |
292 | | } |
293 | | } |
294 | | |
295 | | out: |
296 | | CURL_TRC_CF(data, cf, |
297 | | "vquic_cf_send(len=%zu, gso=%zu, calls=%zu) -> %d, sent=%zu", |
298 | | pktlen, gsolen, calls, result, *psent); |
299 | | return result; |
300 | | } |
301 | | |
302 | | static CURLcode vquic_send_packets(struct Curl_cfilter *cf, |
303 | | struct Curl_easy *data, |
304 | | struct cf_quic_ctx *qctx, |
305 | | const uint8_t *pkt, size_t pktlen, |
306 | | size_t gsolen, size_t *psent) |
307 | | { |
308 | | CURLcode result; |
309 | | #ifdef DEBUGBUILD |
310 | | /* simulate network blocking/partial writes */ |
311 | | if(qctx->wblock_percent > 0) { |
312 | | unsigned char c; |
313 | | *psent = 0; |
314 | | Curl_rand(data, &c, 1); |
315 | | if(c >= ((100 - qctx->wblock_percent) * 256 / 100)) { |
316 | | CURL_TRC_CF(data, cf, "vquic_flush() simulate EWOULDBLOCK"); |
317 | | return CURLE_AGAIN; |
318 | | } |
319 | | } |
320 | | #endif |
321 | | if(qctx->no_gso && pktlen > gsolen) { |
322 | | result = send_packet_no_gso(cf, data, qctx, pkt, pktlen, gsolen, psent); |
323 | | } |
324 | | else { |
325 | | result = do_sendmsg(cf, data, qctx, pkt, pktlen, gsolen, psent); |
326 | | CURL_TRC_CF(data, cf, |
327 | | "vquic_%s(len=%zu, gso=%zu, calls=1) -> %d, sent=%zu", |
328 | | VQUIC_SEND_METHOD, pktlen, gsolen, result, *psent); |
329 | | } |
330 | | if(!result) |
331 | | qctx->last_io = qctx->last_op; |
332 | | return result; |
333 | | } |
334 | | |
335 | | CURLcode vquic_flush(struct Curl_cfilter *cf, struct Curl_easy *data, |
336 | | struct cf_quic_ctx *qctx) |
337 | | { |
338 | | const unsigned char *buf; |
339 | | size_t blen, sent; |
340 | | CURLcode result; |
341 | | size_t gsolen; |
342 | | |
343 | | while(Curl_bufq_peek(&qctx->sendbuf, &buf, &blen)) { |
344 | | gsolen = qctx->gsolen; |
345 | | if(qctx->split_len) { |
346 | | gsolen = qctx->split_gsolen; |
347 | | if(blen > qctx->split_len) |
348 | | blen = qctx->split_len; |
349 | | } |
350 | | |
351 | | if(qctx->sockfd != CURL_SOCKET_BAD) { |
352 | | /* Direct UDP socket (via happy eyeballs) */ |
353 | | result = vquic_send_packets(cf, data, qctx, buf, blen, gsolen, &sent); |
354 | | } |
355 | | else { |
356 | | /* Tunneled QUIC (CONNECT-UDP through proxy) */ |
357 | | if(gsolen && (blen > gsolen)) { |
358 | | /* Send one datagram at a time to preserve packet boundaries. */ |
359 | | result = send_packet_no_gso_cf(cf, data, buf, blen, gsolen, &sent); |
360 | | } |
361 | | else { |
362 | | /* No GSO aggregate to split, regular lower-filter send is enough. */ |
363 | | result = Curl_conn_cf_send(cf->next, data, buf, blen, FALSE, &sent); |
364 | | } |
365 | | } |
366 | | |
367 | | if(result) { |
368 | | if(result == CURLE_AGAIN) { |
369 | | Curl_bufq_skip(&qctx->sendbuf, sent); |
370 | | if(qctx->split_len) |
371 | | qctx->split_len -= sent; |
372 | | } |
373 | | return result; |
374 | | } |
375 | | Curl_bufq_skip(&qctx->sendbuf, sent); |
376 | | if(qctx->split_len) |
377 | | qctx->split_len -= sent; |
378 | | } |
379 | | return CURLE_OK; |
380 | | } |
381 | | |
382 | | CURLcode vquic_send(struct Curl_cfilter *cf, struct Curl_easy *data, |
383 | | struct cf_quic_ctx *qctx, size_t gsolen) |
384 | | { |
385 | | qctx->gsolen = gsolen; |
386 | | return vquic_flush(cf, data, qctx); |
387 | | } |
388 | | |
389 | | CURLcode vquic_send_tail_split(struct Curl_cfilter *cf, struct Curl_easy *data, |
390 | | struct cf_quic_ctx *qctx, size_t gsolen, |
391 | | size_t tail_len, size_t tail_gsolen) |
392 | | { |
393 | | DEBUGASSERT(Curl_bufq_len(&qctx->sendbuf) > tail_len); |
394 | | qctx->split_len = Curl_bufq_len(&qctx->sendbuf) - tail_len; |
395 | | qctx->split_gsolen = gsolen; |
396 | | qctx->gsolen = tail_gsolen; |
397 | | CURL_TRC_CF(data, cf, "vquic_send_tail_split: [%zu gso=%zu][%zu gso=%zu]", |
398 | | qctx->split_len, qctx->split_gsolen, tail_len, qctx->gsolen); |
399 | | return vquic_flush(cf, data, qctx); |
400 | | } |
401 | | |
402 | | #if defined(HAVE_SENDMMSG) || defined(HAVE_SENDMSG) |
403 | | static size_t vquic_msghdr_get_udp_gro(struct msghdr *msg) |
404 | | { |
405 | | int gso_size = 0; |
406 | | #if defined(__linux__) && defined(UDP_GRO) |
407 | | struct cmsghdr *cmsg; |
408 | | |
409 | | /* Workaround musl CMSG_NXTHDR issue */ |
410 | | #if defined(__clang__) && !defined(__GLIBC__) |
411 | | #pragma clang diagnostic push |
412 | | #pragma clang diagnostic ignored "-Wsign-compare" |
413 | | #pragma clang diagnostic ignored "-Wcast-align" |
414 | | #endif |
415 | | for(cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) { |
416 | | #if defined(__clang__) && !defined(__GLIBC__) |
417 | | #pragma clang diagnostic pop |
418 | | #endif |
419 | | if(cmsg->cmsg_level == SOL_UDP && cmsg->cmsg_type == UDP_GRO) { |
420 | | memcpy(&gso_size, CMSG_DATA(cmsg), sizeof(gso_size)); |
421 | | |
422 | | break; |
423 | | } |
424 | | } |
425 | | #endif |
426 | | (void)msg; |
427 | | |
428 | | return (size_t)gso_size; |
429 | | } |
430 | | #endif |
431 | | |
432 | | #ifdef HAVE_SENDMMSG |
433 | | static CURLcode recvmmsg_packets(struct Curl_cfilter *cf, |
434 | | struct Curl_easy *data, |
435 | | struct cf_quic_ctx *qctx, |
436 | | size_t max_pkts, |
437 | | vquic_recv_pkts_cb *recv_cb, void *userp) |
438 | | { |
439 | | #if defined(__linux__) && defined(UDP_GRO) |
440 | | #define MMSG_NUM 16 |
441 | | #define UDP_GRO_CNT_MAX 64 |
442 | | #else |
443 | | #define MMSG_NUM 64 |
444 | | #define UDP_GRO_CNT_MAX 1 |
445 | | #endif |
446 | | #define MSG_BUF_SIZE (UDP_GRO_CNT_MAX * 1500) |
447 | | struct iovec msg_iov[MMSG_NUM]; |
448 | | struct mmsghdr mmsg[MMSG_NUM]; |
449 | | uint8_t msg_ctrl[MMSG_NUM * CMSG_SPACE(sizeof(int))]; |
450 | | struct sockaddr_storage remote_addr[MMSG_NUM]; |
451 | | size_t total_nread = 0, pkts = 0; |
452 | | #ifdef CURLVERBOSE |
453 | | size_t calls = 0; |
454 | | #endif |
455 | | int mcount, i, n; |
456 | | char errstr[STRERROR_LEN]; |
457 | | CURLcode result = CURLE_OK; |
458 | | size_t gso_size; |
459 | | char *sockbuf = NULL; |
460 | | uint8_t (*bufs)[MSG_BUF_SIZE] = NULL; |
461 | | |
462 | | DEBUGASSERT(max_pkts > 0); |
463 | | result = Curl_multi_xfer_sockbuf_borrow(data, MMSG_NUM * MSG_BUF_SIZE, |
464 | | &sockbuf); |
465 | | if(result) |
466 | | goto out; |
467 | | bufs = (uint8_t (*)[MSG_BUF_SIZE])sockbuf; |
468 | | |
469 | | total_nread = 0; |
470 | | while(pkts < max_pkts) { |
471 | | n = (int)CURLMIN(CURLMIN(MMSG_NUM, IOV_MAX), max_pkts); |
472 | | memset(&mmsg, 0, sizeof(mmsg)); |
473 | | for(i = 0; i < n; ++i) { |
474 | | msg_iov[i].iov_base = bufs[i]; |
475 | | msg_iov[i].iov_len = (int)sizeof(bufs[i]); |
476 | | mmsg[i].msg_hdr.msg_iov = &msg_iov[i]; |
477 | | mmsg[i].msg_hdr.msg_iovlen = 1; |
478 | | mmsg[i].msg_hdr.msg_name = &remote_addr[i]; |
479 | | mmsg[i].msg_hdr.msg_namelen = sizeof(remote_addr[i]); |
480 | | mmsg[i].msg_hdr.msg_control = &msg_ctrl[i * CMSG_SPACE(sizeof(int))]; |
481 | | mmsg[i].msg_hdr.msg_controllen = CMSG_SPACE(sizeof(int)); |
482 | | } |
483 | | |
484 | | while((mcount = recvmmsg(qctx->sockfd, mmsg, n, 0, NULL)) == -1 && |
485 | | (SOCKERRNO == SOCKEINTR || SOCKERRNO == SOCKEMSGSIZE)) |
486 | | ; |
487 | | if(mcount == -1) { |
488 | | if(SOCKERRNO == EAGAIN || SOCKERRNO == SOCKEWOULDBLOCK) { |
489 | | CURL_TRC_CF(data, cf, "ingress, recvmmsg -> EAGAIN"); |
490 | | goto out; |
491 | | } |
492 | | if(!cf->connected && SOCKERRNO == SOCKECONNREFUSED) { |
493 | | struct ip_quadruple ip; |
494 | | if(!Curl_cf_socket_peek(cf->next, data, NULL, NULL, &ip)) |
495 | | failf(data, "QUIC: connection to %s port %u refused", |
496 | | ip.remote_ip, ip.remote_port); |
497 | | result = CURLE_COULDNT_CONNECT; |
498 | | goto out; |
499 | | } |
500 | | curlx_strerror(SOCKERRNO, errstr, sizeof(errstr)); |
501 | | failf(data, "QUIC: recvmmsg() unexpectedly returned %d (errno=%d; %s)", |
502 | | mcount, SOCKERRNO, errstr); |
503 | | result = CURLE_RECV_ERROR; |
504 | | goto out; |
505 | | } |
506 | | |
507 | | VERBOSE(++calls); |
508 | | for(i = 0; i < mcount; ++i) { |
509 | | /* A zero-length UDP packet is no QUIC packet. Ignore. */ |
510 | | if(!mmsg[i].msg_len) |
511 | | continue; |
512 | | total_nread += mmsg[i].msg_len; |
513 | | |
514 | | gso_size = vquic_msghdr_get_udp_gro(&mmsg[i].msg_hdr); |
515 | | if(gso_size == 0) |
516 | | gso_size = mmsg[i].msg_len; |
517 | | |
518 | | result = recv_cb(bufs[i], mmsg[i].msg_len, gso_size, |
519 | | mmsg[i].msg_hdr.msg_name, |
520 | | mmsg[i].msg_hdr.msg_namelen, 0, userp); |
521 | | if(result) |
522 | | goto out; |
523 | | pkts += (mmsg[i].msg_len + gso_size - 1) / gso_size; |
524 | | } |
525 | | } |
526 | | |
527 | | out: |
528 | | if(total_nread || result) |
529 | | CURL_TRC_CF(data, cf, |
530 | | "vquic_recvmmsg(len=%zu, packets=%zu, calls=%zu) -> %d", |
531 | | total_nread, pkts, calls, result); |
532 | | Curl_multi_xfer_sockbuf_release(data, sockbuf); |
533 | | return result; |
534 | | } |
535 | | |
536 | | #elif defined(HAVE_SENDMSG) |
537 | | static CURLcode recvmsg_packets(struct Curl_cfilter *cf, |
538 | | struct Curl_easy *data, |
539 | | struct cf_quic_ctx *qctx, |
540 | | size_t max_pkts, |
541 | | vquic_recv_pkts_cb *recv_cb, void *userp) |
542 | | { |
543 | | struct iovec msg_iov; |
544 | | struct msghdr msg; |
545 | | uint8_t buf[64 * 1024]; |
546 | | struct sockaddr_storage remote_addr; |
547 | | size_t total_nread, pkts, calls; |
548 | | ssize_t rc; |
549 | | size_t nread; |
550 | | char errstr[STRERROR_LEN]; |
551 | | CURLcode result = CURLE_OK; |
552 | | uint8_t msg_ctrl[CMSG_SPACE(sizeof(int))]; |
553 | | size_t gso_size; |
554 | | |
555 | | DEBUGASSERT(max_pkts > 0); |
556 | | for(pkts = 0, total_nread = 0, calls = 0; pkts < max_pkts;) { |
557 | | /* fully initialize this on each call to `recvmsg()`. There seem to |
558 | | * operating systems out there that mess with `msg_iov.iov_len`. */ |
559 | | memset(&msg, 0, sizeof(msg)); |
560 | | msg_iov.iov_base = buf; |
561 | | msg_iov.iov_len = (int)sizeof(buf); |
562 | | msg.msg_iov = &msg_iov; |
563 | | msg.msg_iovlen = 1; |
564 | | msg.msg_control = msg_ctrl; |
565 | | msg.msg_name = &remote_addr; |
566 | | msg.msg_namelen = sizeof(remote_addr); |
567 | | msg.msg_controllen = sizeof(msg_ctrl); |
568 | | |
569 | | while((rc = recvmsg(qctx->sockfd, &msg, 0)) == -1 && |
570 | | (SOCKERRNO == SOCKEINTR || SOCKERRNO == SOCKEMSGSIZE)) |
571 | | ; |
572 | | if(!curlx_sztouz(rc, &nread)) { |
573 | | if(SOCKERRNO == EAGAIN || SOCKERRNO == SOCKEWOULDBLOCK) { |
574 | | goto out; |
575 | | } |
576 | | if(!cf->connected && SOCKERRNO == SOCKECONNREFUSED) { |
577 | | struct ip_quadruple ip; |
578 | | if(!Curl_cf_socket_peek(cf->next, data, NULL, NULL, &ip)) |
579 | | failf(data, "QUIC: connection to %s port %u refused", |
580 | | ip.remote_ip, ip.remote_port); |
581 | | result = CURLE_COULDNT_CONNECT; |
582 | | goto out; |
583 | | } |
584 | | curlx_strerror(SOCKERRNO, errstr, sizeof(errstr)); |
585 | | failf(data, "QUIC: recvmsg() unexpectedly returned %zd (errno=%d; %s)", |
586 | | rc, SOCKERRNO, errstr); |
587 | | result = CURLE_RECV_ERROR; |
588 | | goto out; |
589 | | } |
590 | | |
591 | | total_nread += nread; |
592 | | ++calls; |
593 | | |
594 | | /* A 0-length UDP packet is no QUIC packet */ |
595 | | if(!nread) |
596 | | continue; |
597 | | |
598 | | gso_size = vquic_msghdr_get_udp_gro(&msg); |
599 | | if(gso_size == 0) |
600 | | gso_size = nread; |
601 | | |
602 | | result = recv_cb(buf, nread, gso_size, |
603 | | msg.msg_name, msg.msg_namelen, 0, userp); |
604 | | if(result) |
605 | | goto out; |
606 | | pkts += (nread + gso_size - 1) / gso_size; |
607 | | } |
608 | | |
609 | | out: |
610 | | if(total_nread || result) |
611 | | CURL_TRC_CF(data, cf, |
612 | | "vquic_recvmsg(len=%zu, packets=%zu, calls=%zu) -> %d", |
613 | | total_nread, pkts, calls, result); |
614 | | return result; |
615 | | } |
616 | | |
617 | | #else /* HAVE_SENDMMSG || HAVE_SENDMSG */ |
618 | | static CURLcode recvfrom_packets(struct Curl_cfilter *cf, |
619 | | struct Curl_easy *data, |
620 | | struct cf_quic_ctx *qctx, |
621 | | size_t max_pkts, |
622 | | vquic_recv_pkts_cb *recv_cb, void *userp) |
623 | | { |
624 | | uint8_t buf[64 * 1024]; |
625 | | int bufsize = (int)sizeof(buf); |
626 | | struct sockaddr_storage remote_addr; |
627 | | socklen_t remote_addrlen = sizeof(remote_addr); |
628 | | size_t total_nread, pkts, calls = 0, nread; |
629 | | ssize_t rv; |
630 | | char errstr[STRERROR_LEN]; |
631 | | CURLcode result = CURLE_OK; |
632 | | |
633 | | DEBUGASSERT(max_pkts > 0); |
634 | | for(pkts = 0, total_nread = 0; pkts < max_pkts;) { |
635 | | while((rv = recvfrom(qctx->sockfd, (char *)buf, bufsize, 0, |
636 | | (struct sockaddr *)&remote_addr, |
637 | | &remote_addrlen)) == -1 && |
638 | | (SOCKERRNO == SOCKEINTR || SOCKERRNO == SOCKEMSGSIZE)) |
639 | | ; |
640 | | if(!curlx_sztouz(rv, &nread)) { |
641 | | if(SOCKERRNO == EAGAIN || SOCKERRNO == SOCKEWOULDBLOCK) { |
642 | | CURL_TRC_CF(data, cf, "ingress, recvfrom -> EAGAIN"); |
643 | | goto out; |
644 | | } |
645 | | if(!cf->connected && SOCKERRNO == SOCKECONNREFUSED) { |
646 | | struct ip_quadruple ip; |
647 | | if(!Curl_cf_socket_peek(cf->next, data, NULL, NULL, &ip)) |
648 | | failf(data, "QUIC: connection to %s port %u refused", |
649 | | ip.remote_ip, ip.remote_port); |
650 | | result = CURLE_COULDNT_CONNECT; |
651 | | goto out; |
652 | | } |
653 | | curlx_strerror(SOCKERRNO, errstr, sizeof(errstr)); |
654 | | failf(data, "QUIC: recvfrom() unexpectedly returned %zd (errno=%d; %s)", |
655 | | rv, SOCKERRNO, errstr); |
656 | | result = CURLE_RECV_ERROR; |
657 | | goto out; |
658 | | } |
659 | | |
660 | | ++pkts; |
661 | | ++calls; |
662 | | |
663 | | /* A 0-length UDP packet is no QUIC packet */ |
664 | | if(!nread) |
665 | | continue; |
666 | | |
667 | | total_nread += nread; |
668 | | result = recv_cb(buf, nread, nread, &remote_addr, remote_addrlen, |
669 | | 0, userp); |
670 | | if(result) |
671 | | goto out; |
672 | | } |
673 | | |
674 | | out: |
675 | | if(total_nread || result) |
676 | | CURL_TRC_CF(data, cf, |
677 | | "vquic_recvfrom(len=%zu, packets=%zu, calls=%zu) -> %d", |
678 | | total_nread, pkts, calls, result); |
679 | | return result; |
680 | | } |
681 | | #endif /* !HAVE_SENDMMSG && !HAVE_SENDMSG */ |
682 | | |
683 | | CURLcode vquic_recv_packets(struct Curl_cfilter *cf, |
684 | | struct Curl_easy *data, |
685 | | struct cf_quic_ctx *qctx, |
686 | | size_t max_pkts, |
687 | | vquic_recv_pkts_cb *recv_cb, void *userp) |
688 | | { |
689 | | CURLcode result; |
690 | | #ifdef HAVE_SENDMMSG |
691 | | result = recvmmsg_packets(cf, data, qctx, max_pkts, recv_cb, userp); |
692 | | #elif defined(HAVE_SENDMSG) |
693 | | result = recvmsg_packets(cf, data, qctx, max_pkts, recv_cb, userp); |
694 | | #else |
695 | | result = recvfrom_packets(cf, data, qctx, max_pkts, recv_cb, userp); |
696 | | #endif |
697 | | if(!result) { |
698 | | if(!qctx->got_first_byte) { |
699 | | qctx->got_first_byte = TRUE; |
700 | | qctx->first_byte_at = qctx->last_op; |
701 | | } |
702 | | qctx->last_io = qctx->last_op; |
703 | | } |
704 | | return result; |
705 | | } |
706 | | |
707 | | /* |
708 | | * If the QLOGDIR environment variable is set, open and return a file |
709 | | * descriptor to write the log to. |
710 | | * |
711 | | * This function returns error if something failed outside of failing to |
712 | | * create the file. Open file success is deemed by seeing if the returned fd |
713 | | * is != -1. |
714 | | */ |
715 | | CURLcode Curl_qlogdir(struct Curl_easy *data, |
716 | | unsigned char *scid, |
717 | | size_t scidlen, |
718 | | int *qlogfdp) |
719 | | { |
720 | | char *qlog_dir = curl_getenv("QLOGDIR"); |
721 | | *qlogfdp = -1; |
722 | | if(qlog_dir) { |
723 | | struct dynbuf fname; |
724 | | CURLcode result; |
725 | | unsigned int i; |
726 | | curlx_dyn_init(&fname, DYN_QLOG_NAME); |
727 | | result = curlx_dyn_add(&fname, qlog_dir); |
728 | | if(!result) |
729 | | result = curlx_dyn_add(&fname, "/"); |
730 | | for(i = 0; (i < scidlen) && !result; i++) { |
731 | | char hex[3]; |
732 | | curl_msnprintf(hex, 3, "%02x", scid[i]); |
733 | | result = curlx_dyn_add(&fname, hex); |
734 | | } |
735 | | if(!result) |
736 | | result = curlx_dyn_add(&fname, ".sqlog"); |
737 | | |
738 | | if(!result) { |
739 | | int qlogfd = curlx_open(curlx_dyn_ptr(&fname), |
740 | | O_WRONLY | O_CREAT | CURL_O_BINARY, |
741 | | data->set.new_file_perms |
742 | | #ifdef _WIN32 |
743 | | & (_S_IREAD | _S_IWRITE) |
744 | | #endif |
745 | | ); |
746 | | if(qlogfd != -1) |
747 | | *qlogfdp = qlogfd; |
748 | | } |
749 | | curlx_dyn_free(&fname); |
750 | | curlx_free(qlog_dir); |
751 | | if(result) |
752 | | return result; |
753 | | } |
754 | | |
755 | | return CURLE_OK; |
756 | | } |
757 | | |
758 | | CURLcode Curl_cf_quic_insert_after(struct Curl_cfilter *cf_at) |
759 | | { |
760 | | #if defined(USE_NGTCP2) && defined(USE_NGHTTP3) |
761 | | return Curl_cf_ngtcp2_insert_after(cf_at); |
762 | | #else |
763 | | (void)cf_at; |
764 | | return CURLE_NOT_BUILT_IN; |
765 | | #endif |
766 | | } |
767 | | |
768 | | CURLcode Curl_cf_quic_create(struct Curl_cfilter **pcf, |
769 | | struct Curl_easy *data, |
770 | | struct connectdata *conn, |
771 | | struct Curl_sockaddr_ex *addr, |
772 | | uint8_t transport_in, |
773 | | uint8_t transport_out) |
774 | | { |
775 | | (void)transport_in; |
776 | | (void)transport_out; |
777 | | DEBUGASSERT(transport_out == TRNSPRT_QUIC); |
778 | | #if defined(USE_NGTCP2) && defined(USE_NGHTTP3) |
779 | | return Curl_cf_ngtcp2_create(pcf, data, conn, addr); |
780 | | #elif defined(USE_QUICHE) |
781 | | return Curl_cf_quiche_create(pcf, data, conn, addr); |
782 | | #else |
783 | | *pcf = NULL; |
784 | | (void)data; |
785 | | (void)conn; |
786 | | (void)addr; |
787 | | return CURLE_NOT_BUILT_IN; |
788 | | #endif |
789 | | } |
790 | | |
791 | | CURLcode Curl_conn_may_http3(struct Curl_easy *data, |
792 | | const struct connectdata *conn, |
793 | | unsigned char transport) |
794 | | { |
795 | | if(transport == TRNSPRT_UNIX) { |
796 | | failf(data, "HTTP/3 cannot be used over UNIX domain sockets"); |
797 | | return CURLE_QUIC_CONNECT_ERROR; |
798 | | } |
799 | | if(!(conn->scheme->flags & PROTOPT_SSL)) { |
800 | | failf(data, "HTTP/3 requested for non-HTTPS URL"); |
801 | | return CURLE_URL_MALFORMAT; |
802 | | } |
803 | | #ifndef CURL_DISABLE_PROXY |
804 | | if(conn->bits.socksproxy) { |
805 | | failf(data, "HTTP/3 is not supported over a SOCKS proxy"); |
806 | | return CURLE_URL_MALFORMAT; |
807 | | } |
808 | | #endif |
809 | | |
810 | | return CURLE_OK; |
811 | | } |
812 | | |
813 | | #ifdef CURLVERBOSE |
814 | | const char *vquic_h3_err_str(uint64_t error_code) |
815 | | { |
816 | | if(error_code <= UINT_MAX) { |
817 | | switch((unsigned int)error_code) { |
818 | | case CURL_H3_ERR_NO_ERROR: |
819 | | return "NO_ERROR"; |
820 | | case CURL_H3_ERR_GENERAL_PROTOCOL_ERROR: |
821 | | return "GENERAL_PROTOCOL_ERROR"; |
822 | | case CURL_H3_ERR_INTERNAL_ERROR: |
823 | | return "INTERNAL_ERROR"; |
824 | | case CURL_H3_ERR_STREAM_CREATION_ERROR: |
825 | | return "STREAM_CREATION_ERROR"; |
826 | | case CURL_H3_ERR_CLOSED_CRITICAL_STREAM: |
827 | | return "CLOSED_CRITICAL_STREAM"; |
828 | | case CURL_H3_ERR_FRAME_UNEXPECTED: |
829 | | return "FRAME_UNEXPECTED"; |
830 | | case CURL_H3_ERR_FRAME_ERROR: |
831 | | return "FRAME_ERROR"; |
832 | | case CURL_H3_ERR_EXCESSIVE_LOAD: |
833 | | return "EXCESSIVE_LOAD"; |
834 | | case CURL_H3_ERR_ID_ERROR: |
835 | | return "ID_ERROR"; |
836 | | case CURL_H3_ERR_SETTINGS_ERROR: |
837 | | return "SETTINGS_ERROR"; |
838 | | case CURL_H3_ERR_MISSING_SETTINGS: |
839 | | return "MISSING_SETTINGS"; |
840 | | case CURL_H3_ERR_REQUEST_REJECTED: |
841 | | return "REQUEST_REJECTED"; |
842 | | case CURL_H3_ERR_REQUEST_CANCELLED: |
843 | | return "REQUEST_CANCELLED"; |
844 | | case CURL_H3_ERR_REQUEST_INCOMPLETE: |
845 | | return "REQUEST_INCOMPLETE"; |
846 | | case CURL_H3_ERR_MESSAGE_ERROR: |
847 | | return "MESSAGE_ERROR"; |
848 | | case CURL_H3_ERR_CONNECT_ERROR: |
849 | | return "CONNECT_ERROR"; |
850 | | case CURL_H3_ERR_VERSION_FALLBACK: |
851 | | return "VERSION_FALLBACK"; |
852 | | default: |
853 | | break; |
854 | | } |
855 | | } |
856 | | /* RFC 9114 ch. 8.1 + 9, reserved future error codes that are NO_ERROR */ |
857 | | if((error_code >= 0x21) && !((error_code - 0x21) % 0x1f)) |
858 | | return "NO_ERROR"; |
859 | | return "unknown"; |
860 | | } |
861 | | #endif /* CURLVERBOSE */ |
862 | | |
863 | | #if defined(USE_NGTCP2) || defined(USE_NGHTTP3) |
864 | | |
865 | | static void *vquic_ngtcp2_malloc(size_t size, void *user_data) |
866 | | { |
867 | | (void)user_data; |
868 | | return Curl_cmalloc(size); |
869 | | } |
870 | | |
871 | | static void vquic_ngtcp2_free(void *ptr, void *user_data) |
872 | | { |
873 | | (void)user_data; |
874 | | Curl_cfree(ptr); |
875 | | } |
876 | | |
877 | | static void *vquic_ngtcp2_calloc(size_t nmemb, size_t size, void *user_data) |
878 | | { |
879 | | (void)user_data; |
880 | | return Curl_ccalloc(nmemb, size); |
881 | | } |
882 | | |
883 | | static void *vquic_ngtcp2_realloc(void *ptr, size_t size, void *user_data) |
884 | | { |
885 | | (void)user_data; |
886 | | return Curl_crealloc(ptr, size); |
887 | | } |
888 | | |
889 | | #ifdef USE_NGTCP2 |
890 | | static struct ngtcp2_mem vquic_ngtcp2_mem = { |
891 | | NULL, |
892 | | vquic_ngtcp2_malloc, |
893 | | vquic_ngtcp2_free, |
894 | | vquic_ngtcp2_calloc, |
895 | | vquic_ngtcp2_realloc |
896 | | }; |
897 | | struct ngtcp2_mem *Curl_ngtcp2_mem(void) |
898 | | { |
899 | | return &vquic_ngtcp2_mem; |
900 | | } |
901 | | #endif |
902 | | |
903 | | #ifdef USE_NGHTTP3 |
904 | | static struct nghttp3_mem vquic_nghttp3_mem = { |
905 | | NULL, |
906 | | vquic_ngtcp2_malloc, |
907 | | vquic_ngtcp2_free, |
908 | | vquic_ngtcp2_calloc, |
909 | | vquic_ngtcp2_realloc |
910 | | }; |
911 | | struct nghttp3_mem *Curl_nghttp3_mem(void) |
912 | | { |
913 | | return &vquic_nghttp3_mem; |
914 | | } |
915 | | #endif |
916 | | |
917 | | #endif /* USE_NGTCP2 || USE_NGHTTP3 */ |
918 | | |
919 | | #else /* CURL_DISABLE_HTTP || !USE_HTTP3 */ |
920 | | |
921 | | CURLcode Curl_conn_may_http3(struct Curl_easy *data, |
922 | | const struct connectdata *conn, |
923 | | unsigned char transport) |
924 | 0 | { |
925 | 0 | (void)data; |
926 | 0 | (void)conn; |
927 | 0 | (void)transport; |
928 | 0 | DEBUGF(infof(data, "QUIC is not supported in this build")); |
929 | 0 | return CURLE_NOT_BUILT_IN; |
930 | 0 | } |
931 | | |
932 | | #endif /* !CURL_DISABLE_HTTP && USE_HTTP3 */ |