| 1 | #ifndef HM_QUIC_H | ||
| 2 | #define HM_QUIC_H | ||
| 3 | |||
| 4 | /* QUIC transport (ngtcp2 + ngtcp2_crypto_ossl). Enabled by Makefile.PL when | ||
| 5 | * all of ngtcp2, ngtcp2_crypto_ossl, nghttp3 and an OpenSSL that can hand | ||
| 6 | * them its handshake are found (-DHM_HAVE_HTTP3); otherwise everything here | ||
| 7 | * compiles to stubs and `http3 => 1` errors at runtime. | ||
| 8 | * | ||
| 9 | * This file owns the transport: the UDP listener, the datagram read loop, | ||
| 10 | * the Destination Connection ID map, hm_qconn, and the expiry heap. HTTP/3 | ||
| 11 | * semantics live next door in hm_http3.h. Included from hm_core.h after | ||
| 12 | * hm_tls.h (QUIC's handshake is a TLS handshake) and hm_http2.h. | ||
| 13 | * | ||
| 14 | * Two shapes here deliberately do NOT follow the HTTP/2 template: | ||
| 15 | * | ||
| 16 | * - QUIC keeps NO backend timer. ngtcp2 wants its expiry rearmed after | ||
| 17 | * essentially every packet, and every backend's timer is too expensive | ||
| 18 | * for that: epoll burns an fd per timer and deletes with a linear scan | ||
| 19 | * over 65536 entries, kqueue floors at 1ms (too coarse for a | ||
| 20 | * sub-millisecond PTO), poll appends and linear-deletes, io_uring leaks | ||
| 21 | * until completion, and hm_dispatch's HM_TW_C branch frees an hm_tw that | ||
| 22 | * the arm just allocated. So there is a min-heap on the loop and the | ||
| 23 | * rearm is a sift: no syscall, no allocation. | ||
| 24 | * | ||
| 25 | * - QUIC PULLS. hm_h2_cb_send pushes into hm_wb_put and then hm_flush; | ||
| 26 | * nghttp3 has no send callback at all, and bytes leave through | ||
| 27 | * nghttp3_conn_writev_stream -> ngtcp2_conn_writev_stream -> sendto. So | ||
| 28 | * the h3 equivalent of hm_h2_flush_send lives HERE rather than in | ||
| 29 | * hm_http3.h, and a QUIC connection has no wbuf, no hm_flush and no | ||
| 30 | * write watcher. | ||
| 31 | * | ||
| 32 | * Deliberately deferred, decided rather than missed: connection migration | ||
| 33 | * (path_validation is accepted, the initial path is kept), 0-RTT (refused | ||
| 34 | * explicitly rather than mis-handled), key update beyond | ||
| 35 | * ngtcp2_crypto_update_key_cb, ECN, GSO/GRO/sendmmsg, the datagram | ||
| 36 | * extension (RFC 9221), CBPF/eBPF reuseport steering, and IPv6 - the whole | ||
| 37 | * dist is IPv4-only at the listener, and h3 inherits that. */ | ||
| 38 | |||
| 39 | #ifdef HM_HAVE_HTTP3 | ||
| 40 | |||
| 41 | #include <ngtcp2/ngtcp2.h> | ||
| 42 | #include <ngtcp2/ngtcp2_crypto.h> | ||
| 43 | #include <ngtcp2/ngtcp2_crypto_ossl.h> | ||
| 44 | #include <nghttp3/nghttp3.h> | ||
| 45 | #include <openssl/rand.h> | ||
| 46 | |||
| 47 | /* A QUIC packet must fit the path MTU, so a real one is never near this. | ||
| 48 | * The buffer is generous anyway because a datagram larger than it is | ||
| 49 | * TRUNCATED by recvfrom rather than refused, and a truncated packet fails | ||
| 50 | * its AEAD and is dropped - correct, but a silent drop is a bad thing to | ||
| 51 | * debug. At this size nothing legitimate can reach that path. */ | ||
| 52 | #define HM_QUIC_MAX_UDP 65536 | ||
| 53 | |||
| 54 | static int hm_quic_available(void) { return 1; } | ||
| 55 | |||
| 56 | /* ---- Connection IDs ------------------------------------------------------ | ||
| 57 | * | ||
| 58 | * Everything from here down to and including the lookup is dTHX-FREE, the | ||
| 59 | * same constraint that lets hm_parse.h be fuzzed standalone. It faces | ||
| 60 | * unauthenticated UDP, which is a worse position than the HTTP/1 parser is | ||
| 61 | * in: a packet reaches this code before anything has been decrypted or | ||
| 62 | * authenticated, and a mistake here misroutes one client's packets into | ||
| 63 | * another client's connection - a data disclosure, not a crash. | ||
| 64 | * | ||
| 65 | * The length we mint. 16 leaves plenty of room against collision while | ||
| 66 | * keeping the header small; the wire can hand us up to 20 (and, for an | ||
| 67 | * unsupported version, up to 255 - see hm_quic_triage). */ | ||
| 68 | #define HM_QUIC_CIDLEN 16 | ||
| 69 | #define HM_QUIC_CIDBUCKETS 1024 /* power of two */ | ||
| 70 | #define HM_QUIC_SRC_SLOTS 256 /* power of two: per-source half-open table */ | ||
| 71 | |||
| 72 | /* The worker index lives in the FIRST byte of every CID we mint. That is a | ||
| 73 | * fixed offset from the start of a short-header packet, which is what a | ||
| 74 | * reuseport-steering BPF program can read. The steering itself is deferred, | ||
| 75 | * but where the index lives is not: retrofitting it means changing every | ||
| 76 | * CID already in flight, so the room is reserved from the first packet. */ | ||
| 77 | #define HM_QUIC_CID_WORKER(cid) ((cid)->data[0]) | ||
| 78 | |||
| 79 | typedef struct hm_quic_srv hm_quic_srv; | ||
| 80 | |||
| 81 | typedef struct hm_qcid { | ||
| 82 | struct hm_qcid *hnext; /* bucket chain */ | ||
| 83 | struct hm_qcid *cnext; /* the owning connection's list */ | ||
| 84 | hm_qconn *qc; | ||
| 85 | uint8_t len; | ||
| 86 | uint8_t data[NGTCP2_MAX_CIDLEN]; | ||
| 87 | } hm_qcid; | ||
| 88 | |||
| 89 | /* A QUIC connection. A PARALLEL struct to hm_conn rather than an extension | ||
| 90 | * of it, and the reasons are load-bearing: | ||
| 91 | * | ||
| 92 | * - loop->conns is indexed by fd and N QUIC connections share ONE fd, so | ||
| 93 | * putting these there means rewriting every loop->conns[fd] site - | ||
| 94 | * hm_dispatch, hm_run_again, hm_deliver, the writers, hm_detach, the LRU | ||
| 95 | * walk in hm_sweep, the pooling in hm_close. | ||
| 96 | * - rbuf, wbuf, writing, keepalive, bsrc, spill_fd and tls_hs are all | ||
| 97 | * meaningless here. ngtcp2 owns framing, retransmission and flow | ||
| 98 | * control, and there is no write buffer because QUIC pulls. | ||
| 99 | * | ||
| 100 | * `id` comes from the same hm_id_counter the TCP side uses, so the existing | ||
| 101 | * generation-guard idiom carries over unchanged: the pair a Writer or a | ||
| 102 | * parked closure holds becomes (udp_fd, id) instead of (fd, id), a stale | ||
| 103 | * ticket is a no-op, and it can never be a wrong-connection hit. */ | ||
| 104 | struct hm_qconn { | ||
| 105 | hm_quic_srv *srv; | ||
| 106 | UV id; /* generation, from hm_id_counter */ | ||
| 107 | hm_qcid *cids; /* every CID routing to this conn */ | ||
| 108 | uint64_t expiry; /* ngtcp2_conn_get_expiry() */ | ||
| 109 | int heap_idx; /* position in loop->qheap, or -1 */ | ||
| 110 | hm_qconn *next; /* srv->conns */ | ||
| 111 | hm_qconn *idnext; /* srv->ids bucket chain */ | ||
| 112 | |||
| 113 | ngtcp2_conn *conn; | ||
| 114 | /* What ngtcp2_crypto hangs off the SSL as application data, and the ONLY | ||
| 115 | * thing that may go there: its callbacks read it back as this type and | ||
| 116 | * call get_conn through it, so anything else is a call through a garbage | ||
| 117 | * pointer. */ | ||
| 118 | ngtcp2_crypto_conn_ref conn_ref; | ||
| 119 | ngtcp2_crypto_ossl_ctx *ossl; | ||
| 120 | void *ssl; /* SSL *, owned by ossl once set */ | ||
| 121 | void *h3; /* nghttp3_conn *, or NULL */ | ||
| 122 | void *h3_streams; /* hm_h3_stream list (hm_http3.h) */ | ||
| 123 | ngtcp2_path_storage path; | ||
| 124 | struct sockaddr_storage peer; | ||
| 125 | socklen_t peerlen; | ||
| 126 | char peer_str[INET6_ADDRSTRLEN]; /* REMOTE_ADDR */ | ||
| 127 | int peer_port; | ||
| 128 | |||
| 129 | /* Mirrors hm_conn's, so $env reports TLS the same way on either | ||
| 130 | * transport. OpenSSL owns the strings for the life of the session. */ | ||
| 131 | const char *tls_proto; | ||
| 132 | const char *tls_cipher; | ||
| 133 | void *tls_peer; /* hm_tls_peer *, if a client cert */ | ||
| 134 | |||
| 135 | unsigned char handshake_done; /* off srv->handshaking once true */ | ||
| 136 | unsigned char closing; /* CONNECTION_CLOSE sent */ | ||
| 137 | unsigned char draining; | ||
| 138 | unsigned char in_read; /* inside ngtcp2_conn_read_pkt: the | ||
| 139 | * write half waits for it to return | ||
| 140 | * (hm_quic_write) */ | ||
| 141 | /* An application call for one of this connection's streams is on the | ||
| 142 | * stack (hm_h3_run_ready). Its ->get may pump the loop, and the loop | ||
| 143 | * may then want this connection gone - an expiry, a read error, the | ||
| 144 | * idle timeout - while that frame still holds qc and the stream: the | ||
| 145 | * free is deferred to the frame's return instead. Out of the expiry | ||
| 146 | * heap for the duration (hm_quic_rearm), as the h2 half is out of its | ||
| 147 | * read watcher. */ | ||
| 148 | unsigned char in_app; | ||
| 149 | unsigned char close_deferred; | ||
| 150 | unsigned char dispatching; /* one stream's handler at a time */ | ||
| 151 | unsigned char src_counted; /* in the per-source table: an inc | ||
| 152 | * the table refused (full) must not | ||
| 153 | * be matched by a dec against some | ||
| 154 | * other address's slot */ | ||
| 155 | }; | ||
| 156 | |||
| 157 | /* One per listener, not per loop: a CID minted on listener A must not be | ||
| 158 | * resolvable on listener B, and scoping the table this way makes teardown | ||
| 159 | * obviously bounded rather than a search. */ | ||
| 160 | struct hm_quic_srv { | ||
| 161 | hm_listener *lst; | ||
| 162 | hm_loop *loop; | ||
| 163 | int worker_idx; | ||
| 164 | hm_qcid *cids[HM_QUIC_CIDBUCKETS]; /* by connection id */ | ||
| 165 | hm_qconn *ids[HM_QUIC_CIDBUCKETS]; /* by qconn->id, for the shim */ | ||
| 166 | hm_qconn *conns; /* every live connection */ | ||
| 167 | int nconns; | ||
| 168 | int handshaking; /* half-open, drives Retry */ | ||
| 169 | /* Half-open handshakes BY SOURCE ADDRESS. The global threshold above | ||
| 170 | * can be spent by one source; this holds each to HM_QUIC_PER_SOURCE. | ||
| 171 | * Open addressing over the address bytes, a slot freed when its count | ||
| 172 | * reaches zero; with the table full the global rule applies alone. */ | ||
| 173 | struct { uint8_t len; uint8_t addr[16]; uint16_t count; } src[HM_QUIC_SRC_SLOTS]; | ||
| 174 | void *ssl_ctx; /* SSL_CTX *, QUIC-side */ | ||
| 175 | /* The per-process secret behind stateless reset and Retry tokens. | ||
| 176 | * Generated ONCE, before the fork, for the same reason the TLS | ||
| 177 | * ticket key is: under SO_REUSEPORT a client's token can land on a | ||
| 178 | * different worker, and a per-worker secret makes every such token | ||
| 179 | * unverifiable. */ | ||
| 180 | uint8_t secret[32]; | ||
| 181 | }; | ||
| 182 | |||
| 183 | /* Half-open handshakes one source address may hold at once. A NAT puts many | ||
| 184 | * clients behind one address, so this is generous; a flood from one address | ||
| 185 | * is what it stops. */ | ||
| 186 | #define HM_QUIC_PER_SOURCE 32 | ||
| 187 | |||
| 188 | /* The address bytes of a peer, without the port. */ | ||
| 189 | static int hm_quic_src_bytes(const struct sockaddr *sa, uint8_t *out) { | ||
| 190 | if (sa->sa_family == AF_INET) { | ||
| 191 | memcpy(out, &((const struct sockaddr_in *)sa)->sin_addr, 4); | ||
| 192 | return 4; | ||
| 193 | } | ||
| 194 | if (sa->sa_family == AF_INET6) { /* # uncoverable branch false */ | ||
| 195 | memcpy(out, &((const struct sockaddr_in6 *)sa)->sin6_addr, 16); | ||
| 196 | return 16; | ||
| 197 | } | ||
| 198 | return 0; /* # uncoverable statement */ | ||
| 199 | } | ||
| 200 | |||
| 201 | static uint32_t hm_quic_hash(const uint8_t *d, size_t n); | ||
| 202 | |||
| 203 | /* The slot for an address: its own, or the first empty one on the probe | ||
| 204 | * walk, or -1 when the table is full. */ | ||
| 205 | /* KEYED: the per-process secret seeds the hash, so an attacker holding a | ||
| 206 | * /64 cannot pick addresses that land in one slot and walk the probe | ||
| 207 | * sequence to make the table look full. */ | ||
| 208 | static uint32_t hm_quic_src_home(const hm_quic_srv *srv, const uint8_t *a, int len) { | ||
| 209 | uint32_t h = 2166136261u ^ ((uint32_t)srv->secret[0] | ||
| 210 | | ((uint32_t)srv->secret[1] << 8) | ||
| 211 | | ((uint32_t)srv->secret[2] << 16) | ||
| 212 | | ((uint32_t)srv->secret[3] << 24)); | ||
| 213 | int i; | ||
| 214 | for (i = 0; i < len; i++) { h ^= a[i]; h *= 16777619u; } | ||
| 215 | return h & (HM_QUIC_SRC_SLOTS - 1); | ||
| 216 | } | ||
| 217 | |||
| 218 | static int hm_quic_src_slot(hm_quic_srv *srv, const uint8_t *a, int len, int create) { | ||
| 219 | uint32_t h = hm_quic_src_home(srv, a, len); | ||
| 220 | int i; | ||
| 221 | for (i = 0; i < HM_QUIC_SRC_SLOTS; i++) { | ||
| 222 | int k = (int)((h + (uint32_t)i) & (HM_QUIC_SRC_SLOTS - 1)); | ||
| 223 | if (srv->src[k].len == (uint8_t)len && memcmp(srv->src[k].addr, a, (size_t)len) == 0) | ||
| 224 | return k; | ||
| 225 | if (srv->src[k].len == 0) { | ||
| 226 | if (!create) return -1; | ||
| 227 | srv->src[k].len = (uint8_t)len; | ||
| 228 | memcpy(srv->src[k].addr, a, (size_t)len); | ||
| 229 | srv->src[k].count = 0; | ||
| 230 | return k; | ||
| 231 | } | ||
| 232 | } | ||
| 233 | return -1; | ||
| 234 | } | ||
| 235 | |||
| 236 | /* Empty slot k and close the probe chain behind it (backward-shift | ||
| 237 | * deletion). Linear probing finds an address by walking from its home to | ||
| 238 | * the first EMPTY slot, so simply emptying k hid every address stored past | ||
| 239 | * it: those read as absent, were re-created as duplicates, and their | ||
| 240 | * counts never drained - ghosts filled the table and the cap switched off. | ||
| 241 | * Each later entry whose home is not inside (k, j] is moved back into k. */ | ||
| 242 | static void hm_quic_src_erase(hm_quic_srv *srv, int k) { | ||
| 243 | int j = k; | ||
| 244 | srv->src[k].len = 0; | ||
| 245 | for (;;) { | ||
| 246 | uint32_t home; | ||
| 247 | int in_gap; | ||
| 248 | j = (j + 1) & (HM_QUIC_SRC_SLOTS - 1); | ||
| 249 | if (srv->src[j].len == 0) return; | ||
| 250 | home = hm_quic_src_home(srv, srv->src[j].addr, srv->src[j].len); | ||
| 251 | /* is home cyclically in (k, j]? then j may stay where it is */ | ||
| 252 | in_gap = (k < j) ? ((int)home > k && (int)home <= j) | ||
| 253 | : ((int)home > k || (int)home <= j); /* # uncoverable statement */ | ||
| 254 | if (in_gap) continue; /* # uncoverable branch true */ | ||
| 255 | srv->src[k] = srv->src[j]; | ||
| 256 | srv->src[j].len = 0; | ||
| 257 | k = j; | ||
| 258 | } | ||
| 259 | } | ||
| 260 | |||
| 261 | /* Count a half-open handshake against its source: the new count, or -1 for | ||
| 262 | * an address the table cannot hold (then only the global rule applies). */ | ||
| 263 | static int hm_quic_src_inc(hm_quic_srv *srv, const struct sockaddr *sa) { | ||
| 264 | uint8_t a[16]; | ||
| 265 | int len = hm_quic_src_bytes(sa, a), k; | ||
| 266 | if (!len) return -1; /* # uncoverable branch true */ | ||
| 267 | k = hm_quic_src_slot(srv, a, len, 1); | ||
| 268 | if (k < 0) return -1; | ||
| 269 | return (int)++srv->src[k].count; | ||
| 270 | } | ||
| 271 | static int hm_quic_src_count(hm_quic_srv *srv, const struct sockaddr *sa) { | ||
| 272 | uint8_t a[16]; | ||
| 273 | int len = hm_quic_src_bytes(sa, a), k; | ||
| 274 | if (!len) return 0; /* # uncoverable branch true */ | ||
| 275 | k = hm_quic_src_slot(srv, a, len, 0); | ||
| 276 | return k < 0 ? 0 : (int)srv->src[k].count; | ||
| 277 | } | ||
| 278 | /* The handshake completed or the connection went: one fewer for its source, | ||
| 279 | * and the slot freed at zero so the table does not fill with ghosts. */ | ||
| 280 | static void hm_quic_src_dec(hm_quic_srv *srv, const struct sockaddr *sa) { | ||
| 281 | uint8_t a[16]; | ||
| 282 | int len = hm_quic_src_bytes(sa, a), k; | ||
| 283 | if (!len) return; /* # uncoverable branch true */ | ||
| 284 | k = hm_quic_src_slot(srv, a, len, 0); | ||
| 285 | if (k < 0) return; /* # uncoverable branch true */ | ||
| 286 | if (srv->src[k].count > 0) srv->src[k].count--; | ||
| 287 | if (srv->src[k].count == 0) hm_quic_src_erase(srv, k); | ||
| 288 | } | ||
| 289 | |||
| 290 | /* Filled before any worker forks; every srv copies it. */ | ||
| 291 | static uint8_t hm_quic_secret[32]; | ||
| 292 | static int hm_quic_secret_ready = 0; | ||
| 293 | |||
| 294 | static void hm_quic_secret_init(void); | ||
| 295 | |||
| 296 | static uint32_t hm_quic_hash(const uint8_t *d, size_t n) { | ||
| 297 | uint32_t h = 2166136261u; /* FNV-1a */ | ||
| 298 | size_t i; | ||
| 299 | for (i = 0; i < n; i++) { h ^= d[i]; h *= 16777619u; } | ||
| 300 | return h; | ||
| 301 | } | ||
| 302 | |||
| 303 | /* A hit compares the FULL length with memcmp. Never a prefix: two CIDs | ||
| 304 | * sharing a prefix are two different connections, and a prefix match would | ||
| 305 | * hand one client's packets to the other. */ | ||
| 306 | static hm_qconn *hm_quic_cid_find(hm_quic_srv *srv, const uint8_t *d, size_t n) { | ||
| 307 | hm_qcid *e; | ||
| 308 | if (!srv || n == 0 || n > NGTCP2_MAX_CIDLEN) return NULL; | ||
| 309 | e = srv->cids[hm_quic_hash(d, n) & (HM_QUIC_CIDBUCKETS - 1)]; | ||
| 310 | for (; e; e = e->hnext) | ||
| 311 | if (e->len == n && memcmp(e->data, d, n) == 0) return e->qc; | ||
| 312 | return NULL; | ||
| 313 | } | ||
| 314 | |||
| 315 | /* Add one CID to a connection. ngtcp2 mints extras through | ||
| 316 | * NEW_CONNECTION_ID, which is why a connection owns a LIST rather than one. | ||
| 317 | * 1 on success, 0 if the CID is unusable or already mapped. */ | ||
| 318 | static int hm_quic_cid_add(hm_quic_srv *srv, hm_qconn *qc, | ||
| 319 | const uint8_t *d, size_t n) { | ||
| 320 | hm_qcid *e; | ||
| 321 | uint32_t b; | ||
| 322 | if (!srv || !qc || n == 0 || n > NGTCP2_MAX_CIDLEN) return 0; | ||
| 323 | if (hm_quic_cid_find(srv, d, n)) return 0; /* # uncoverable branch true */ | ||
| 324 | b = hm_quic_hash(d, n) & (HM_QUIC_CIDBUCKETS - 1); | ||
| 325 | e = (hm_qcid *)hm_xcalloc(1, sizeof(hm_qcid)); | ||
| 326 | e->qc = qc; | ||
| 327 | e->len = (uint8_t)n; | ||
| 328 | memcpy(e->data, d, n); | ||
| 329 | e->hnext = srv->cids[b]; | ||
| 330 | srv->cids[b] = e; | ||
| 331 | e->cnext = qc->cids; | ||
| 332 | qc->cids = e; | ||
| 333 | return 1; | ||
| 334 | } | ||
| 335 | |||
| 336 | /* Retire one CID (ngtcp2's remove_connection_id callback). */ | ||
| 337 | static void hm_quic_cid_remove(hm_quic_srv *srv, const uint8_t *d, size_t n) { | ||
| 338 | hm_qcid **pp, *e = NULL; | ||
| 339 | uint32_t b; | ||
| 340 | if (!srv || n == 0 || n > NGTCP2_MAX_CIDLEN) return; | ||
| 341 | b = hm_quic_hash(d, n) & (HM_QUIC_CIDBUCKETS - 1); | ||
| 342 | for (pp = &srv->cids[b]; *pp; pp = &(*pp)->hnext) { | ||
| 343 | if ((*pp)->len == n && memcmp((*pp)->data, d, n) == 0) { | ||
| 344 | e = *pp; *pp = e->hnext; break; | ||
| 345 | } | ||
| 346 | } | ||
| 347 | if (!e) return; /* # uncoverable branch true */ | ||
| 348 | { /* and off its connection's list */ | ||
| 349 | hm_qcid **cp; | ||
| 350 | for (cp = &e->qc->cids; *cp; cp = &(*cp)->cnext) | ||
| 351 | if (*cp == e) { *cp = e->cnext; break; } | ||
| 352 | } | ||
| 353 | free(e); | ||
| 354 | } | ||
| 355 | |||
| 356 | /* Every CID this connection answers to, at close. Walks the CONNECTION'S | ||
| 357 | * list - never a scan of the buckets, which would be 1024 chain walks to | ||
| 358 | * retire a handful of entries. */ | ||
| 359 | static void hm_quic_cid_drop_conn(hm_quic_srv *srv, hm_qconn *qc) { | ||
| 360 | hm_qcid *e = qc->cids; | ||
| 361 | while (e) { | ||
| 362 | hm_qcid *next = e->cnext; | ||
| 363 | hm_qcid **pp; | ||
| 364 | uint32_t b = hm_quic_hash(e->data, e->len) & (HM_QUIC_CIDBUCKETS - 1); | ||
| 365 | for (pp = &srv->cids[b]; *pp; pp = &(*pp)->hnext) | ||
| 366 | if (*pp == e) { *pp = e->hnext; break; } | ||
| 367 | free(e); | ||
| 368 | e = next; | ||
| 369 | } | ||
| 370 | qc->cids = NULL; | ||
| 371 | } | ||
| 372 | |||
| 373 | /* The (udp_fd, id) shim: the generation-guard idiom the TCP side uses, over | ||
| 374 | * a transport with no fd per connection. Hashed rather than walked, because | ||
| 375 | * this resolves once per response and a worker can hold thousands. */ | ||
| 376 | static hm_qconn *hm_quic_conn_by_id(hm_quic_srv *srv, UV id) { | ||
| 377 | hm_qconn *qc; | ||
| 378 | uint32_t b; | ||
| 379 | if (!srv) return NULL; /* # uncoverable branch true */ | ||
| 380 | b = hm_quic_hash((const uint8_t *)&id, sizeof(id)) & (HM_QUIC_CIDBUCKETS - 1); | ||
| 381 | for (qc = srv->ids[b]; qc; qc = qc->idnext) | ||
| 382 | if (qc->id == id) return qc; | ||
| 383 | return NULL; | ||
| 384 | } | ||
| 385 | |||
| 386 | static void hm_quic_id_add(hm_quic_srv *srv, hm_qconn *qc) { | ||
| 387 | uint32_t b = hm_quic_hash((const uint8_t *)&qc->id, sizeof(qc->id)) | ||
| 388 | & (HM_QUIC_CIDBUCKETS - 1); | ||
| 389 | qc->idnext = srv->ids[b]; | ||
| 390 | srv->ids[b] = qc; | ||
| 391 | } | ||
| 392 | |||
| 393 | static void hm_quic_id_remove(hm_quic_srv *srv, hm_qconn *qc) { | ||
| 394 | uint32_t b = hm_quic_hash((const uint8_t *)&qc->id, sizeof(qc->id)) | ||
| 395 | & (HM_QUIC_CIDBUCKETS - 1); | ||
| 396 | hm_qconn **pp; | ||
| 397 | for (pp = &srv->ids[b]; *pp; pp = &(*pp)->idnext) | ||
| 398 | if (*pp == qc) { *pp = qc->idnext; break; } | ||
| 399 | qc->idnext = NULL; | ||
| 400 | } | ||
| 401 | |||
| 402 | /* ---- the expiry heap ----------------------------------------------------- | ||
| 403 | * | ||
| 404 | * A min-heap keyed on each connection's ngtcp2_conn_get_expiry(), with the | ||
| 405 | * position cached on the connection so a rearm is a sift: no syscall, no | ||
| 406 | * allocation, O(log n). The backend timer vtables cannot carry this - epoll | ||
| 407 | * burns an fd per timer and deletes with a linear scan over 65536 entries, | ||
| 408 | * kqueue floors at 1ms, poll appends and linear-deletes, io_uring leaks | ||
| 409 | * until completion - and ngtcp2 rearms after essentially every packet. */ | ||
| 410 | |||
| 411 | static void hm_qheap_swap(hm_qheap *h, int i, int j) { | ||
| 412 | hm_qconn *t = h->a[i]; | ||
| 413 | h->a[i] = h->a[j]; | ||
| 414 | h->a[j] = t; | ||
| 415 | h->a[i]->heap_idx = i; | ||
| 416 | h->a[j]->heap_idx = j; | ||
| 417 | } | ||
| 418 | |||
| 419 | static void hm_qheap_up(hm_qheap *h, int i) { | ||
| 420 | while (i > 0) { | ||
| 421 | int p = (i - 1) / 2; | ||
| 422 | if (h->a[p]->expiry <= h->a[i]->expiry) break; | ||
| 423 | hm_qheap_swap(h, i, p); | ||
| 424 | i = p; | ||
| 425 | } | ||
| 426 | } | ||
| 427 | |||
| 428 | static void hm_qheap_down(hm_qheap *h, int i) { | ||
| 429 | for (;;) { | ||
| 430 | int l = 2 * i + 1, r = l + 1, m = i; | ||
| 431 | if (l < h->n && h->a[l]->expiry < h->a[m]->expiry) m = l; | ||
| 432 | if (r < h->n && h->a[r]->expiry < h->a[m]->expiry) m = r; | ||
| 433 | if (m == i) break; | ||
| 434 | hm_qheap_swap(h, i, m); | ||
| 435 | i = m; | ||
| 436 | } | ||
| 437 | } | ||
| 438 | |||
| 439 | /* Insert, or move an existing entry. Both directions: ngtcp2 moves a | ||
| 440 | * deadline EARLIER as often as later (a PTO arms sooner than the idle | ||
| 441 | * timeout it replaces), so sifting only one way would leave the heap | ||
| 442 | * unordered and the loop sleeping past a deadline. */ | ||
| 443 | static void hm_qheap_set(hm_qheap *h, hm_qconn *qc, uint64_t expiry) { | ||
| 444 | if (qc->heap_idx >= 0) { | ||
| 445 | uint64_t old = qc->expiry; | ||
| 446 | qc->expiry = expiry; | ||
| 447 | if (expiry < old) hm_qheap_up(h, qc->heap_idx); | ||
| 448 | else hm_qheap_down(h, qc->heap_idx); | ||
| 449 | return; | ||
| 450 | } | ||
| 451 | if (h->n == h->cap) { | ||
| 452 | int cap = h->cap ? h->cap * 2 : 16; | ||
| 453 | h->a = (hm_qconn **)hm_xrealloc(h->a, (size_t)cap * sizeof(hm_qconn *)); | ||
| 454 | h->cap = cap; | ||
| 455 | } | ||
| 456 | qc->expiry = expiry; | ||
| 457 | qc->heap_idx = h->n; | ||
| 458 | h->a[h->n++] = qc; | ||
| 459 | hm_qheap_up(h, qc->heap_idx); | ||
| 460 | } | ||
| 461 | |||
| 462 | static void hm_qheap_remove(hm_qheap *h, hm_qconn *qc) { | ||
| 463 | int i = qc->heap_idx; | ||
| 464 | if (i < 0) return; | ||
| 465 | qc->heap_idx = -1; | ||
| 466 | h->n--; | ||
| 467 | if (i == h->n) return; /* it was the last slot */ | ||
| 468 | h->a[i] = h->a[h->n]; | ||
| 469 | h->a[i]->heap_idx = i; | ||
| 470 | /* The replacement came from the bottom, so it can belong in either | ||
| 471 | * direction relative to where it landed. */ | ||
| 472 | hm_qheap_up(h, i); | ||
| 473 | hm_qheap_down(h, i); | ||
| 474 | } | ||
| 475 | |||
| 476 | /* ---- packet triage ------------------------------------------------------- | ||
| 477 | * | ||
| 478 | * The first thing that touches an attacker-controlled datagram. Kept | ||
| 479 | * separate, and dTHX-free, so it can be fuzzed on its own. | ||
| 480 | * | ||
| 481 | * Returns 0 and fills *out when the packet names a connection we can look | ||
| 482 | * up, 1 when it is well-formed but names no connection we hold (a new | ||
| 483 | * handshake, or a stray), NGTCP2_ERR_VERSION_NEGOTIATION when the client | ||
| 484 | * asked for a version we do not speak, and -1 when it is not decodable. */ | ||
| 485 | static int hm_quic_triage(hm_quic_srv *srv, const uint8_t *pkt, size_t len, | ||
| 486 | ngtcp2_version_cid *vc, hm_qconn **out) { | ||
| 487 | int rv; | ||
| 488 | size_t n; | ||
| 489 | *out = NULL; | ||
| 490 | /* ngtcp2 ASSERTS datalen is non-zero, and this library is built with | ||
| 491 | * assertions live, so an empty datagram would abort the worker rather | ||
| 492 | * than be rejected. hm_quic_readable skips those already; the guard is | ||
| 493 | * repeated here because this function is the fuzz target and must hold | ||
| 494 | * on its own. */ | ||
| 495 | if (len == 0) return -1; /* # uncoverable branch true */ | ||
| 496 | rv = ngtcp2_pkt_decode_version_cid(vc, pkt, len, HM_QUIC_CIDLEN); | ||
| 497 | if (rv == NGTCP2_ERR_VERSION_NEGOTIATION) return rv; /* vc is filled */ /* # uncoverable branch true */ | ||
| 498 | if (rv != 0) return -1; | ||
| 499 | /* dcidlen is NOT bounded by NGTCP2_MAX_CIDLEN here. The decoder handles | ||
| 500 | * Connection IDs up to 255 bytes, because a longer one is legal in a | ||
| 501 | * packet naming a version we do not support - and this is the length | ||
| 502 | * that would otherwise be handed to memcmp against a 20-byte buffer. */ | ||
| 503 | n = vc->dcidlen; | ||
| 504 | if (n == 0 || n > NGTCP2_MAX_CIDLEN) return 1; | ||
| 505 | *out = hm_quic_cid_find(srv, vc->dcid, n); | ||
| 506 | return *out ? 0 : 1; | ||
| 507 | } | ||
| 508 | |||
| 509 | /* Defined below, beside the ngtcp2 wiring; declared here because the packet | ||
| 510 | * path is written in the order a datagram travels rather than in dependency | ||
| 511 | * order, and reading it that way is worth two forward declarations. */ | ||
| 512 | static void hm_quic_rand_bytes(uint8_t *d, size_t n); | ||
| 513 | static void hm_quic_conn_free(pTHX_ hm_qconn *qc); | ||
| 514 | static int hm_quic_write(pTHX_ hm_qconn *qc); | ||
| 515 | |||
| 516 | /* HTTP/3 lives next door in hm_http3.h, included AFTER this file because it | ||
| 517 | * is built on hm_qconn. These are the places the transport reaches up into | ||
| 518 | * it: a QUIC connection owns an nghttp3 session, and stream events have to | ||
| 519 | * cross from one to the other. */ | ||
| 520 | static int hm_h3_session_start(pTHX_ hm_qconn *qc); | ||
| 521 | static void hm_h3_session_free(pTHX_ hm_qconn *qc); | ||
| 522 | static int hm_h3_run_ready(pTHX_ hm_qconn *qc); | ||
| 523 | static int hm_h3_recv_stream(pTHX_ hm_qconn *qc, int64_t sid, | ||
| 524 | const uint8_t *data, size_t datalen, | ||
| 525 | int fin); | ||
| 526 | static int hm_h3_stream_gone(pTHX_ hm_qconn *qc, int64_t sid, | ||
| 527 | uint64_t app_error_code); | ||
| 528 | static int hm_h3_acked(pTHX_ hm_qconn *qc, int64_t sid, uint64_t n); | ||
| 529 | static nghttp3_ssize hm_h3_pull(pTHX_ hm_qconn *qc, int64_t *psid, int *pfin, | ||
| 530 | nghttp3_vec *vec, size_t veccnt); | ||
| 531 | static int hm_h3_wrote(pTHX_ hm_qconn *qc, int64_t sid, size_t n); | ||
| 532 | static int hm_h3_unblock(pTHX_ hm_qconn *qc, int64_t sid); | ||
| 533 | static hm_qconn *hm_quic_conn_new(pTHX_ hm_quic_srv *srv, | ||
| 534 | const ngtcp2_version_cid *vc, | ||
| 535 | const ngtcp2_pkt_hd *hd, | ||
| 536 | const ngtcp2_cid *retry_odcid, | ||
| 537 | const struct sockaddr *peer, | ||
| 538 | socklen_t peerlen); | ||
| 539 | |||
| 540 | /* ---- address validation -------------------------------------------------- | ||
| 541 | * | ||
| 542 | * A QUIC server sends its first flight - certificate and all - to an address | ||
| 543 | * it has not heard back from, so a spoofed source address turns it into an | ||
| 544 | * amplifier. Two answers, and both are in the library rather than written | ||
| 545 | * here: no token in this codebase is hand-rolled crypto. | ||
| 546 | * | ||
| 547 | * Retry the server refuses to continue until the client echoes a token | ||
| 548 | * back from the address it claims. One extra round trip, so it | ||
| 549 | * is NOT unconditional - it is what the server does once it is | ||
| 550 | * carrying more half-open handshakes than the threshold below. | ||
| 551 | * That is the posture that matters: cheap when idle, defensive | ||
| 552 | * under load. | ||
| 553 | * NEW_TOKEN issued after every completed handshake, so a client that comes | ||
| 554 | * back skips validation entirely and pays no round trip even | ||
| 555 | * while the server is retrying everyone else. | ||
| 556 | * | ||
| 557 | * Both are verified against the ONE pre-fork secret, for the reason the TLS | ||
| 558 | * ticket key carries the same rule: under SO_REUSEPORT a client's token can | ||
| 559 | * land on a different worker than the one that minted it, and a per-worker | ||
| 560 | * secret would make every such token unverifiable. */ | ||
| 561 | |||
| 562 | /* Half-open handshakes tolerated before Retry is demanded of new clients. */ | ||
| 563 | #define HM_QUIC_RETRY_THRESHOLD 64 | ||
| 564 | |||
| 565 | /* How long a token stays good. Long enough for a client to come back on the | ||
| 566 | * same address, short enough that a stolen one is not useful for long. */ | ||
| 567 | #define HM_QUIC_RETRY_TIMEOUT (10 * NGTCP2_SECONDS) | ||
| 568 | #define HM_QUIC_TOKEN_TIMEOUT (3600 * NGTCP2_SECONDS) | ||
| 569 | |||
| 570 | /* Demand a Retry: the client must come back echoing this token. */ | ||
| 571 | /* | ||
| 572 | # uncoverable statement | ||
| 573 | # uncoverable subroutine */ | ||
| 574 | static void hm_quic_send_retry(hm_quic_srv *srv, const ngtcp2_pkt_hd *hd, | ||
| 575 | const struct sockaddr *peer, socklen_t peerlen) { | ||
| 576 | uint8_t token[NGTCP2_CRYPTO_MAX_RETRY_TOKENLEN2]; | ||
| 577 | uint8_t buf[512]; | ||
| 578 | ngtcp2_cid scid = {0}; /* # uncoverable statement */ | ||
| 579 | ngtcp2_ssize tlen, n; | ||
| 580 | |||
| 581 | /* A fresh Connection ID for the Retry, which the client will then use as | ||
| 582 | * its destination - it is not ours to keep, so it is not put in the map. */ | ||
| 583 | hm_quic_rand_bytes(scid.data, HM_QUIC_CIDLEN); /* # uncoverable statement */ | ||
| 584 | scid.datalen = HM_QUIC_CIDLEN; /* # uncoverable statement */ | ||
| 585 | HM_QUIC_CID_WORKER(&scid) = (uint8_t)srv->worker_idx; /* # uncoverable statement */ | ||
| 586 | |||
| 587 | tlen = ngtcp2_crypto_generate_retry_token2( /* # uncoverable statement */ | ||
| 588 | token, srv->secret, sizeof(srv->secret), hd->version, /* # uncoverable statement */ | ||
| 589 | (const ngtcp2_sockaddr *)peer, (ngtcp2_socklen)peerlen, /* # uncoverable statement */ | ||
| 590 | &scid, &hd->dcid, hm_now_ns()); /* # uncoverable statement */ | ||
| 591 | /* | ||
| 592 | # uncoverable statement | ||
| 593 | # uncoverable branch true | ||
| 594 | # uncoverable branch false */ | ||
| 595 | if (tlen < 0) return; | ||
| 596 | |||
| 597 | n = ngtcp2_crypto_write_retry(buf, sizeof(buf), hd->version, /* # uncoverable statement */ | ||
| 598 | &hd->scid, &scid, &hd->dcid, /* # uncoverable statement */ | ||
| 599 | token, (size_t)tlen); /* # uncoverable statement */ | ||
| 600 | if (n > 0) (void)hm_os_sendto(srv->lst->udp_fd, buf, (size_t)n, /* # uncoverable statement */ | ||
| 601 | peer, peerlen); | ||
| 602 | } | ||
| 603 | |||
| 604 | /* A short-header packet naming a Connection ID we have never heard of. The | ||
| 605 | * usual cause is a client still talking to a server that has restarted, and | ||
| 606 | * without an answer it retries until its idle timeout. A Stateless Reset | ||
| 607 | * tells it to give up now. | ||
| 608 | * | ||
| 609 | * Only for packets big enough to be worth answering, and the reply is always | ||
| 610 | * SMALLER than what arrived: this path is unauthenticated by definition, so | ||
| 611 | * it must never be an amplifier. */ | ||
| 612 | static void hm_quic_send_stateless_reset(hm_quic_srv *srv, | ||
| 613 | const ngtcp2_version_cid *vc, | ||
| 614 | size_t inlen, | ||
| 615 | const struct sockaddr *peer, | ||
| 616 | socklen_t peerlen) { | ||
| 617 | uint8_t token[NGTCP2_STATELESS_RESET_TOKENLEN]; | ||
| 618 | uint8_t rnd[64], buf[256]; | ||
| 619 | ngtcp2_cid cid = {0}; | ||
| 620 | ngtcp2_ssize n; | ||
| 621 | if (inlen < 41) return; /* too small to be worth a reply */ | ||
| 622 | if (vc->dcidlen == 0 || vc->dcidlen > NGTCP2_MAX_CIDLEN) return; | ||
| 623 | memcpy(cid.data, vc->dcid, vc->dcidlen); | ||
| 624 | cid.datalen = vc->dcidlen; | ||
| 625 | if (ngtcp2_crypto_generate_stateless_reset_token( /* # uncoverable branch true */ | ||
| 626 | token, srv->secret, sizeof(srv->secret), &cid) != 0) return; | ||
| 627 | hm_quic_rand_bytes(rnd, sizeof(rnd)); | ||
| 628 | n = ngtcp2_pkt_write_stateless_reset(buf, sizeof(buf) < inlen - 1 | ||
| 629 | ? sizeof(buf) : inlen - 1, | ||
| 630 | token, rnd, sizeof(rnd)); | ||
| 631 | if (n > 0) (void)hm_os_sendto(srv->lst->udp_fd, buf, (size_t)n, | ||
| 632 | peer, peerlen); | ||
| 633 | } | ||
| 634 | |||
| 635 | /* Answer a client asking for a QUIC version we do not speak. Cheap, and | ||
| 636 | * without it such a client gets silence and retries until it gives up. */ | ||
| 637 | /* | ||
| 638 | # uncoverable statement | ||
| 639 | # uncoverable subroutine */ | ||
| 640 | static void hm_quic_send_vn(hm_quic_srv *srv, const ngtcp2_version_cid *vc, | ||
| 641 | const struct sockaddr *peer, socklen_t peerlen) { | ||
| 642 | uint8_t buf[256]; | ||
| 643 | uint32_t versions[1]; | ||
| 644 | ngtcp2_ssize n; | ||
| 645 | uint8_t seed; | ||
| 646 | versions[0] = NGTCP2_PROTO_VER_V1; /* # uncoverable statement */ | ||
| 647 | hm_quic_rand_bytes(&seed, 1); /* # uncoverable statement */ | ||
| 648 | /* The reply swaps the connection ids, which is what tells the client | ||
| 649 | * this answers its packet. */ | ||
| 650 | n = ngtcp2_pkt_write_version_negotiation(buf, sizeof(buf), seed, /* # uncoverable statement */ | ||
| 651 | vc->scid, vc->scidlen, /* # uncoverable statement */ | ||
| 652 | vc->dcid, vc->dcidlen, /* # uncoverable statement */ | ||
| 653 | versions, 1); /* # uncoverable statement */ | ||
| 654 | if (n > 0) (void)hm_os_sendto(srv->lst->udp_fd, buf, (size_t)n, /* # uncoverable statement */ | ||
| 655 | peer, peerlen); | ||
| 656 | } | ||
| 657 | |||
| 658 | /* One datagram, start to finish: triage, then either an existing connection | ||
| 659 | * or a new one, then read it and write whatever that produced. | ||
| 660 | * | ||
| 661 | * A bad packet from an unauthenticated peer is not an error, so nothing here | ||
| 662 | * has a failure a caller could act on - it returns 0 either way and the | ||
| 663 | * packet is dropped. */ | ||
| 664 | static int hm_quic_packet(pTHX_ hm_loop *loop, hm_listener *lst, | ||
| 665 | const unsigned char *pkt, size_t len, | ||
| 666 | const struct sockaddr *peer, socklen_t peerlen) { | ||
| 667 | hm_quic_srv *srv = (hm_quic_srv *)lst->quic; | ||
| 668 | ngtcp2_version_cid vc; | ||
| 669 | ngtcp2_path path; | ||
| 670 | ngtcp2_pkt_info pi; | ||
| 671 | hm_qconn *qc = NULL; | ||
| 672 | int rv; | ||
| 673 | |||
| 674 | loop->datagrams++; | ||
| 675 | if (!srv) return 0; /* # uncoverable branch true */ | ||
| 676 | |||
| 677 | rv = hm_quic_triage(srv, pkt, len, &vc, &qc); | ||
| 678 | if (rv == NGTCP2_ERR_VERSION_NEGOTIATION) { /* # uncoverable branch true */ | ||
| 679 | hm_quic_send_vn(srv, &vc, peer, peerlen); /* # uncoverable statement */ | ||
| 680 | return 0; /* # uncoverable statement */ | ||
| 681 | } | ||
| 682 | if (rv < 0) return 0; /* not decodable */ | ||
| 683 | if (qc && qc->close_deferred) return 0; /* freed when its handler returns */ | ||
| 684 | |||
| 685 | if (!qc) { | ||
| 686 | ngtcp2_pkt_hd hd; | ||
| 687 | ngtcp2_cid odcid = {0}; | ||
| 688 | const ngtcp2_cid *odcidp = NULL; /* set only when Retry validated */ | ||
| 689 | |||
| 690 | if (loop->stopping) return 0; /* # uncoverable branch true */ | ||
| 691 | |||
| 692 | /* Not an Initial and no connection: this names a Connection ID that | ||
| 693 | * is gone - almost always a client still talking to a server that | ||
| 694 | * has restarted - so tell it to stop rather than let it retry until | ||
| 695 | * its idle timeout. */ | ||
| 696 | if (ngtcp2_accept(&hd, pkt, len) != 0) { | ||
| 697 | if (vc.version == 0) /* short header */ | ||
| 698 | hm_quic_send_stateless_reset(srv, &vc, len, peer, peerlen); | ||
| 699 | return 0; | ||
| 700 | } | ||
| 701 | |||
| 702 | if (loop->http3_max_conns && loop->h3_live >= loop->http3_max_conns) | ||
| 703 | return 0; /* # uncoverable statement */ | ||
| 704 | |||
| 705 | if (hd.tokenlen == 0) { /* # uncoverable branch false */ | ||
| 706 | /* No token. Cheap while the server is idle; once it is carrying | ||
| 707 | * more half-open handshakes than the threshold, make the client | ||
| 708 | * prove its address first. */ | ||
| 709 | if (srv->handshaking >= HM_QUIC_RETRY_THRESHOLD) { /* # uncoverable branch true */ | ||
| 710 | hm_quic_send_retry(srv, &hd, peer, peerlen); /* # uncoverable statement */ | ||
| 711 | return 0; /* # uncoverable statement */ | ||
| 712 | } | ||
| 713 | /* | ||
| 714 | # uncoverable branch true | ||
| 715 | # uncoverable branch false */ | ||
| 716 | } else if (hd.token[0] == NGTCP2_CRYPTO_TOKEN_MAGIC_RETRY2) { | ||
| 717 | /* | ||
| 718 | # uncoverable statement | ||
| 719 | # uncoverable branch true | ||
| 720 | # uncoverable branch false */ | ||
| 721 | if (ngtcp2_crypto_verify_retry_token2( | ||
| 722 | &odcid, hd.token, hd.tokenlen, /* # uncoverable statement */ | ||
| 723 | srv->secret, sizeof(srv->secret), hd.version, /* # uncoverable statement */ | ||
| 724 | (const ngtcp2_sockaddr *)peer, (ngtcp2_socklen)peerlen, /* # uncoverable statement */ | ||
| 725 | &hd.dcid, HM_QUIC_RETRY_TIMEOUT, hm_now_ns()) != 0) /* # uncoverable statement */ | ||
| 726 | return 0; /* forged or stale: say nothing */ /* # uncoverable statement */ | ||
| 727 | odcidp = &odcid; /* # uncoverable statement */ | ||
| 728 | /* | ||
| 729 | # uncoverable statement | ||
| 730 | # uncoverable branch true | ||
| 731 | # uncoverable branch false */ | ||
| 732 | } else if (hd.token[0] == NGTCP2_CRYPTO_TOKEN_MAGIC_REGULAR) { | ||
| 733 | /* A NEW_TOKEN from a previous visit. Good, or ignored - a bad one | ||
| 734 | * is not grounds to refuse, only to stop trusting the address. */ | ||
| 735 | if (ngtcp2_crypto_verify_regular_token( /* # uncoverable statement */ | ||
| 736 | hd.token, hd.tokenlen, srv->secret, sizeof(srv->secret), /* # uncoverable statement */ | ||
| 737 | (const ngtcp2_sockaddr *)peer, (ngtcp2_socklen)peerlen, /* # uncoverable statement */ | ||
| 738 | HM_QUIC_TOKEN_TIMEOUT, hm_now_ns()) != 0 /* # uncoverable statement */ | ||
| 739 | && srv->handshaking >= HM_QUIC_RETRY_THRESHOLD) { /* # uncoverable statement */ | ||
| 740 | hm_quic_send_retry(srv, &hd, peer, peerlen); /* # uncoverable statement */ | ||
| 741 | return 0; /* # uncoverable statement */ | ||
| 742 | } | ||
| 743 | /* | ||
| 744 | # uncoverable statement | ||
| 745 | # uncoverable branch true | ||
| 746 | # uncoverable branch false */ | ||
| 747 | } else if (srv->handshaking >= HM_QUIC_RETRY_THRESHOLD) { | ||
| 748 | hm_quic_send_retry(srv, &hd, peer, peerlen); /* # uncoverable statement */ | ||
| 749 | return 0; /* # uncoverable statement */ | ||
| 750 | } | ||
| 751 | |||
| 752 | /* One source may not hold more than its share of half-open | ||
| 753 | * handshakes, whatever the global count says: dropped, not Retried, | ||
| 754 | * because a Retry only costs the sender one round trip and it has | ||
| 755 | * already shown it will spend them. */ | ||
| 756 | if (hm_quic_src_count(srv, peer) >= HM_QUIC_PER_SOURCE) return 0; /* # uncoverable branch true */ | ||
| 757 | |||
| 758 | qc = hm_quic_conn_new(aTHX_ srv, &vc, &hd, odcidp, peer, peerlen); | ||
| 759 | if (!qc) return 0; /* # uncoverable branch true */ | ||
| 760 | } | ||
| 761 | |||
| 762 | memset(&pi, 0, sizeof(pi)); | ||
| 763 | path = qc->path.path; | ||
| 764 | path.remote.addr = (ngtcp2_sockaddr *)peer; | ||
| 765 | path.remote.addrlen = peerlen; | ||
| 766 | |||
| 767 | qc->in_read = 1; | ||
| 768 | rv = ngtcp2_conn_read_pkt(qc->conn, &path, &pi, pkt, len, hm_now_ns()); | ||
| 769 | qc->in_read = 0; | ||
| 770 | if (rv != 0) { | ||
| 771 | hm_quic_conn_free(aTHX_ qc); | ||
| 772 | return 0; | ||
| 773 | } | ||
| 774 | /* Requests completed by this packet go to the application HERE, once | ||
| 775 | * ngtcp2 has returned - never from inside its callbacks, where a handler | ||
| 776 | * that waited pumped a loop that could free this very connection. The | ||
| 777 | * h2 half made the same move in 0.60. */ | ||
| 778 | if (hm_h3_run_ready(aTHX_ qc) < 0) return 0; /* # uncoverable branch true */ | ||
| 779 | hm_quic_write(aTHX_ qc); | ||
| 780 | return 0; | ||
| 781 | } | ||
| 782 | |||
| 783 | /* ---- selftests ----------------------------------------------------------- | ||
| 784 | * | ||
| 785 | * The CID one is the highest-value unit test in the whole feature: a map bug | ||
| 786 | * misroutes one client's packets into another client's connection, which is | ||
| 787 | * a data disclosure, and no black-box test against a real client will catch | ||
| 788 | * it. Both drive the real structures, with bare hm_qconns - the map and the | ||
| 789 | * heap touch nothing else, so a real ngtcp2 connection would only make the | ||
| 790 | * test harder to run without making it prove more. | ||
| 791 | * | ||
| 792 | * Deterministic pseudo-random, so a failure is reproducible. */ | ||
| 793 | static uint32_t hm_quic_st_rand(uint32_t *s) { | ||
| 794 | *s ^= *s << 13; *s ^= *s >> 17; *s ^= *s << 5; | ||
| 795 | return *s; | ||
| 796 | } | ||
| 797 | |||
| 798 | /* The per-source table: counts climb per address and not across addresses, | ||
| 799 | * fall on completion, free their slot at zero, and a full table says -1 | ||
| 800 | * rather than lying. 1 = every check passed. */ | ||
| 801 | static int hm_quic_src_selftest(void) { | ||
| 802 | hm_quic_srv *srv = (hm_quic_srv *)hm_xcalloc(1, sizeof(hm_quic_srv)); | ||
| 803 | struct sockaddr_in a, b; | ||
| 804 | struct sockaddr_in6 c6; | ||
| 805 | int i, ok = 1; | ||
| 806 | memset(&a, 0, sizeof a); a.sin_family = AF_INET; a.sin_addr.s_addr = htonl(0xc0000201); | ||
| 807 | b = a; b.sin_addr.s_addr = htonl(0xc0000202); | ||
| 808 | b.sin_port = htons(9); /* a different port is the SAME source */ | ||
| 809 | memset(&c6, 0, sizeof c6); c6.sin6_family = AF_INET6; c6.sin6_addr.s6_addr[15] = 1; | ||
| 810 | for (i = 1; i <= HM_QUIC_PER_SOURCE; i++) | ||
| 811 | if (hm_quic_src_inc(srv, (struct sockaddr *)&a) != i) ok = 0; | ||
| 812 | if (hm_quic_src_count(srv, (struct sockaddr *)&a) != HM_QUIC_PER_SOURCE) ok = 0; | ||
| 813 | if (hm_quic_src_count(srv, (struct sockaddr *)&b) != 0) ok = 0; /* its own count */ /* # uncoverable statement */ | ||
| 814 | a.sin_port = htons(4433); /* # uncoverable statement */ | ||
| 815 | if (hm_quic_src_count(srv, (struct sockaddr *)&a) != HM_QUIC_PER_SOURCE) ok = 0; /* port ignored */ /* # uncoverable statement */ | ||
| 816 | if (hm_quic_src_inc(srv, (struct sockaddr *)&c6) != 1) ok = 0; /* # uncoverable statement */ | ||
| 817 | for (i = 0; i < HM_QUIC_PER_SOURCE; i++) hm_quic_src_dec(srv, (struct sockaddr *)&a); | ||
| 818 | if (hm_quic_src_count(srv, (struct sockaddr *)&a) != 0) ok = 0; | ||
| 819 | { /* the slot was freed: the table holds exactly its capacity of others */ | ||
| 820 | int n = 0; | ||
| 821 | for (i = 0; i < HM_QUIC_SRC_SLOTS; i++) if (srv->src[i].len) n++; | ||
| 822 | if (n != 1) ok = 0; /* only the v6 one */ | ||
| 823 | } | ||
| 824 | { /* fill it: the slot after capacity is -1, never a wrong count */ | ||
| 825 | struct sockaddr_in f; int k; | ||
| 826 | memset(&f, 0, sizeof f); f.sin_family = AF_INET; | ||
| 827 | for (k = 0; k < HM_QUIC_SRC_SLOTS - 1; k++) { | ||
| 828 | f.sin_addr.s_addr = htonl(0x0a000000u + (uint32_t)k); | ||
| 829 | if (hm_quic_src_inc(srv, (struct sockaddr *)&f) != 1) { ok = 0; break; } /* # uncoverable branch true */ | ||
| 830 | } | ||
| 831 | f.sin_addr.s_addr = htonl(0x0b000000u); /* # uncoverable statement */ | ||
| 832 | if (hm_quic_src_inc(srv, (struct sockaddr *)&f) != -1) ok = 0; /* # uncoverable statement */ | ||
| 833 | if (hm_quic_src_count(srv, (struct sockaddr *)&f) != 0) ok = 0; /* # uncoverable statement */ | ||
| 834 | } | ||
| 835 | { /* Deletion closes the probe chain. Two addresses with the same home | ||
| 836 | * (found by walking addresses until one collides), the first | ||
| 837 | * stored at home and the second one past it; freeing the first | ||
| 838 | * must leave the second FINDABLE - before backward-shift deletion | ||
| 839 | * the empty slot ended every lookup walk, the second read as | ||
| 840 | * absent, and a fresh increment created a duplicate whose count | ||
| 841 | * never drained. */ | ||
| 842 | struct sockaddr_in p, q; | ||
| 843 | uint8_t pa[4], qa[4]; | ||
| 844 | uint32_t home; | ||
| 845 | uint32_t cand; | ||
| 846 | int found = 0; | ||
| 847 | memset(srv->src, 0, sizeof(srv->src)); | ||
| 848 | memset(&p, 0, sizeof p); p.sin_family = AF_INET; | ||
| 849 | p.sin_addr.s_addr = htonl(0x0a010101u); | ||
| 850 | hm_quic_src_bytes((struct sockaddr *)&p, pa); | ||
| 851 | home = hm_quic_src_home(srv, pa, 4); | ||
| 852 | q = p; | ||
| 853 | for (cand = 0x0a010102u; cand < 0x0a01ffffu; cand++) { | ||
| 854 | q.sin_addr.s_addr = htonl(cand); | ||
| 855 | hm_quic_src_bytes((struct sockaddr *)&q, qa); | ||
| 856 | if (hm_quic_src_home(srv, qa, 4) == home) { found = 1; break; } | ||
| 857 | } | ||
| 858 | if (!found) ok = 0; /* # uncoverable branch true */ | ||
| 859 | else { | ||
| 860 | if (hm_quic_src_inc(srv, (struct sockaddr *)&p) != 1) ok = 0; | ||
| 861 | if (hm_quic_src_inc(srv, (struct sockaddr *)&q) != 1) ok = 0; /* # uncoverable statement */ | ||
| 862 | hm_quic_src_dec(srv, (struct sockaddr *)&p); /* # uncoverable statement */ | ||
| 863 | if (hm_quic_src_count(srv, (struct sockaddr *)&q) != 1) ok = 0; /* # uncoverable statement */ | ||
| 864 | if (hm_quic_src_inc(srv, (struct sockaddr *)&q) != 2) ok = 0; /* # uncoverable statement */ | ||
| 865 | hm_quic_src_dec(srv, (struct sockaddr *)&q); /* # uncoverable statement */ | ||
| 866 | hm_quic_src_dec(srv, (struct sockaddr *)&q); /* # uncoverable statement */ | ||
| 867 | if (hm_quic_src_count(srv, (struct sockaddr *)&q) != 0) ok = 0; /* # uncoverable statement */ | ||
| 868 | { | ||
| 869 | int n = 0; | ||
| 870 | for (found = 0; found < HM_QUIC_SRC_SLOTS; found++) | ||
| 871 | if (srv->src[found].len) n++; | ||
| 872 | if (n != 0) ok = 0; /* no ghosts left behind */ | ||
| 873 | } | ||
| 874 | } | ||
| 875 | } | ||
| 876 | free(srv); | ||
| 877 | return ok; | ||
| 878 | } | ||
| 879 | |||
| 880 | #define HM_QUIC_ST_CONNS 64 | ||
| 881 | #define HM_QUIC_ST_PER 8 | ||
| 882 | |||
| 883 | static int hm_quic_cid_selftest(void) { | ||
| 884 | hm_quic_srv *srv = (hm_quic_srv *)hm_xcalloc(1, sizeof(hm_quic_srv)); | ||
| 885 | hm_qconn *qcs = (hm_qconn *)hm_xcalloc(HM_QUIC_ST_CONNS, sizeof(hm_qconn)); | ||
| 886 | uint8_t (*cid)[NGTCP2_MAX_CIDLEN] = | ||
| 887 | (uint8_t (*)[NGTCP2_MAX_CIDLEN]) | ||
| 888 | hm_xcalloc(HM_QUIC_ST_CONNS * HM_QUIC_ST_PER, NGTCP2_MAX_CIDLEN); | ||
| 889 | uint32_t seed = 0x9e3779b9u; | ||
| 890 | int i, j, ok = 1; | ||
| 891 | |||
| 892 | for (i = 0; i < HM_QUIC_ST_CONNS; i++) { | ||
| 893 | qcs[i].id = (UV)(1000 + i); | ||
| 894 | qcs[i].heap_idx = -1; | ||
| 895 | hm_quic_id_add(srv, &qcs[i]); | ||
| 896 | for (j = 0; j < HM_QUIC_ST_PER; j++) { | ||
| 897 | int k = i * HM_QUIC_ST_PER + j, b; | ||
| 898 | for (b = 0; b < HM_QUIC_CIDLEN; b++) | ||
| 899 | cid[k][b] = (uint8_t)hm_quic_st_rand(&seed); | ||
| 900 | /* the worker index lives in byte 0 and is not random */ | ||
| 901 | cid[k][0] = 0; | ||
| 902 | /* ... which means byte 0 collides across every connection, so | ||
| 903 | * this also proves the lookup is not keyed on a prefix */ | ||
| 904 | if (!hm_quic_cid_add(srv, &qcs[i], cid[k], HM_QUIC_CIDLEN)) ok = 0; | ||
| 905 | } | ||
| 906 | } | ||
| 907 | |||
| 908 | /* every CID resolves, and to the RIGHT connection */ | ||
| 909 | for (i = 0; i < HM_QUIC_ST_CONNS * HM_QUIC_ST_PER; i++) | ||
| 910 | if (hm_quic_cid_find(srv, cid[i], HM_QUIC_CIDLEN) | ||
| 911 | != &qcs[i / HM_QUIC_ST_PER]) ok = 0; | ||
| 912 | |||
| 913 | /* the id table resolves too - the (udp_fd, id) shim */ | ||
| 914 | for (i = 0; i < HM_QUIC_ST_CONNS; i++) | ||
| 915 | if (hm_quic_conn_by_id(srv, (UV)(1000 + i)) != &qcs[i]) ok = 0; | ||
| 916 | if (hm_quic_conn_by_id(srv, 999)) ok = 0; | ||
| 917 | |||
| 918 | if (hm_quic_cid_find(srv, cid[0], 0)) ok = 0; /* # uncoverable statement */ | ||
| 919 | if (hm_quic_cid_find(srv, cid[0], NGTCP2_MAX_CIDLEN + 1)) ok = 0; /* # uncoverable statement */ | ||
| 920 | |||
| 921 | /* A prefix must not resolve to the connection it is a prefix OF. | ||
| 922 | * | ||
| 923 | * Testing that with a bare short lookup proves nothing, and this test | ||
| 924 | * asserted exactly that until it was mutation-checked: FNV over n bytes | ||
| 925 | * puts a 15-byte query in a different bucket from the 16-byte entry, so | ||
| 926 | * the query misses on the bucket and the length comparison inside the | ||
| 927 | * chain never runs at all. Removing `e->len == n` entirely left the | ||
| 928 | * suite green. | ||
| 929 | * | ||
| 930 | * So the collision is FORCED. Two CIDs of different lengths, one a | ||
| 931 | * strict prefix of the other, searched until they land in the same | ||
| 932 | * bucket - which is the only arrangement in which the length check is | ||
| 933 | * load-bearing, and the arrangement a different hash function would | ||
| 934 | * make common. */ | ||
| 935 | { | ||
| 936 | uint8_t longer[NGTCP2_MAX_CIDLEN]; | ||
| 937 | uint32_t want, tries; | ||
| 938 | int placed = 0; | ||
| 939 | for (i = 0; i < NGTCP2_MAX_CIDLEN; i++) | ||
| 940 | longer[i] = (uint8_t)hm_quic_st_rand(&seed); | ||
| 941 | longer[0] = 0; | ||
| 942 | /* The short form's bucket is fixed - it reads only the first | ||
| 943 | * HM_QUIC_CIDLEN bytes - so the search varies the TAIL those bytes | ||
| 944 | * do not cover until the long form lands in it. | ||
| 945 | * | ||
| 946 | * All four tail bytes, not just the last: FNV's final step is | ||
| 947 | * `h ^= b; h *= prime`, so varying one byte reaches at most 256 of | ||
| 948 | * the 1024 buckets and the target is usually not among them. That | ||
| 949 | * mistake made this loop fail three times in four. */ | ||
| 950 | want = hm_quic_hash(longer, HM_QUIC_CIDLEN) & (HM_QUIC_CIDBUCKETS - 1); | ||
| 951 | for (tries = 0; tries < 200000 && !placed; tries++) { | ||
| 952 | if ((hm_quic_hash(longer, NGTCP2_MAX_CIDLEN) | ||
| 953 | & (HM_QUIC_CIDBUCKETS - 1)) == want) { placed = 1; break; } | ||
| 954 | for (i = HM_QUIC_CIDLEN; i < NGTCP2_MAX_CIDLEN; i++) | ||
| 955 | longer[i] = (uint8_t)hm_quic_st_rand(&seed); | ||
| 956 | } | ||
| 957 | if (!placed) ok = 0; /* never happens; not silently skipped */ /* # uncoverable branch true */ | ||
| 958 | else { | ||
| 959 | if (!hm_quic_cid_add(srv, &qcs[1], longer, NGTCP2_MAX_CIDLEN)) ok = 0; | ||
| 960 | if (!hm_quic_cid_add(srv, &qcs[2], longer, HM_QUIC_CIDLEN)) ok = 0; /* # uncoverable statement */ | ||
| 961 | if (hm_quic_cid_find(srv, longer, NGTCP2_MAX_CIDLEN) != &qcs[1]) ok = 0; /* # uncoverable statement */ | ||
| 962 | if (hm_quic_cid_find(srv, longer, HM_QUIC_CIDLEN) != &qcs[2]) ok = 0; /* # uncoverable statement */ | ||
| 963 | hm_quic_cid_remove(srv, longer, NGTCP2_MAX_CIDLEN); | ||
| 964 | hm_quic_cid_remove(srv, longer, HM_QUIC_CIDLEN); | ||
| 965 | } | ||
| 966 | } | ||
| 967 | |||
| 968 | /* drop one connection: its CIDs go, every other one still resolves */ | ||
| 969 | hm_quic_cid_drop_conn(srv, &qcs[7]); | ||
| 970 | hm_quic_id_remove(srv, &qcs[7]); | ||
| 971 | for (i = 0; i < HM_QUIC_ST_CONNS * HM_QUIC_ST_PER; i++) { | ||
| 972 | hm_qconn *got = hm_quic_cid_find(srv, cid[i], HM_QUIC_CIDLEN); | ||
| 973 | if (i / HM_QUIC_ST_PER == 7) { if (got) ok = 0; } | ||
| 974 | else if (got != &qcs[i / HM_QUIC_ST_PER]) ok = 0; | ||
| 975 | } | ||
| 976 | if (hm_quic_conn_by_id(srv, 1007)) ok = 0; | ||
| 977 | |||
| 978 | /* retiring a single CID leaves its connection's others alone */ | ||
| 979 | hm_quic_cid_remove(srv, cid[0], HM_QUIC_CIDLEN); /* # uncoverable statement */ | ||
| 980 | if (hm_quic_cid_find(srv, cid[0], HM_QUIC_CIDLEN)) ok = 0; /* # uncoverable statement */ | ||
| 981 | if (hm_quic_cid_find(srv, cid[1], HM_QUIC_CIDLEN) != &qcs[0]) ok = 0; /* # uncoverable statement */ | ||
| 982 | |||
| 983 | for (i = 0; i < HM_QUIC_ST_CONNS; i++) | ||
| 984 | if (i != 7) hm_quic_cid_drop_conn(srv, &qcs[i]); | ||
| 985 | free(cid); free(qcs); free(srv); | ||
| 986 | return ok; | ||
| 987 | } | ||
| 988 | |||
| 989 | /* Address-validation tokens. The property that matters is not that a token | ||
| 990 | * verifies - it is that it verifies for the address it was minted for and | ||
| 991 | * for NO OTHER, because a token that travels is one an attacker can replay | ||
| 992 | * from a spoofed source. The crypto is all the library's; what is under test | ||
| 993 | * is that it was called with the right arguments. */ | ||
| 994 | static int hm_quic_token_selftest(void) { | ||
| 995 | uint8_t secret[32]; | ||
| 996 | uint8_t token[NGTCP2_CRYPTO_MAX_RETRY_TOKENLEN2]; | ||
| 997 | uint8_t reg[NGTCP2_CRYPTO_MAX_REGULAR_TOKENLEN]; | ||
| 998 | struct sockaddr_in a, b; | ||
| 999 | ngtcp2_cid scid = {0}, odcid = {0}, got = {0}; | ||
| 1000 | ngtcp2_tstamp now = hm_now_ns(); | ||
| 1001 | ngtcp2_ssize tlen, rlen; | ||
| 1002 | int ok = 1; | ||
| 1003 | |||
| 1004 | hm_quic_rand_bytes(secret, sizeof(secret)); | ||
| 1005 | memset(&a, 0, sizeof(a)); | ||
| 1006 | a.sin_family = AF_INET; | ||
| 1007 | a.sin_port = htons(4433); | ||
| 1008 | a.sin_addr.s_addr = htonl(0x7f000001); /* 127.0.0.1 */ | ||
| 1009 | b = a; | ||
| 1010 | b.sin_addr.s_addr = htonl(0x7f000002); /* 127.0.0.2 */ | ||
| 1011 | |||
| 1012 | hm_quic_rand_bytes(scid.data, HM_QUIC_CIDLEN); scid.datalen = HM_QUIC_CIDLEN; | ||
| 1013 | hm_quic_rand_bytes(odcid.data, HM_QUIC_CIDLEN); odcid.datalen = HM_QUIC_CIDLEN; | ||
| 1014 | |||
| 1015 | tlen = ngtcp2_crypto_generate_retry_token2( | ||
| 1016 | token, secret, sizeof(secret), NGTCP2_PROTO_VER_V1, | ||
| 1017 | (const ngtcp2_sockaddr *)&a, sizeof(a), &scid, &odcid, now); | ||
| 1018 | if (tlen <= 0) return 0; /* # uncoverable branch true */ | ||
| 1019 | if (token[0] != NGTCP2_CRYPTO_TOKEN_MAGIC_RETRY2) ok = 0; | ||
| 1020 | |||
| 1021 | /* the address it was minted for */ | ||
| 1022 | if (ngtcp2_crypto_verify_retry_token2( /* # uncoverable statement */ | ||
| 1023 | &got, token, (size_t)tlen, secret, sizeof(secret), /* # uncoverable statement */ | ||
| 1024 | NGTCP2_PROTO_VER_V1, (const ngtcp2_sockaddr *)&a, sizeof(a), | ||
| 1025 | &scid, HM_QUIC_RETRY_TIMEOUT, now) != 0) ok = 0; /* # uncoverable statement */ | ||
| 1026 | /* ...and it hands back the id the client used BEFORE the Retry, which is | ||
| 1027 | * what the transport parameters then have to echo */ | ||
| 1028 | if (got.datalen != odcid.datalen | ||
| 1029 | || memcmp(got.data, odcid.data, got.datalen) != 0) ok = 0; | ||
| 1030 | |||
| 1031 | /* a DIFFERENT address must not verify: that is the whole point */ | ||
| 1032 | if (ngtcp2_crypto_verify_retry_token2( /* # uncoverable statement */ | ||
| 1033 | &got, token, (size_t)tlen, secret, sizeof(secret), /* # uncoverable statement */ | ||
| 1034 | NGTCP2_PROTO_VER_V1, (const ngtcp2_sockaddr *)&b, sizeof(b), | ||
| 1035 | &scid, HM_QUIC_RETRY_TIMEOUT, now) == 0) ok = 0; /* # uncoverable statement */ | ||
| 1036 | |||
| 1037 | /* nor a different Connection ID */ | ||
| 1038 | { | ||
| 1039 | ngtcp2_cid other = scid; /* # uncoverable statement */ | ||
| 1040 | other.data[0] ^= 0xff; /* # uncoverable statement */ | ||
| 1041 | if (ngtcp2_crypto_verify_retry_token2( /* # uncoverable statement */ | ||
| 1042 | &got, token, (size_t)tlen, secret, sizeof(secret), /* # uncoverable statement */ | ||
| 1043 | NGTCP2_PROTO_VER_V1, (const ngtcp2_sockaddr *)&a, sizeof(a), | ||
| 1044 | &other, HM_QUIC_RETRY_TIMEOUT, now) == 0) ok = 0; /* # uncoverable statement */ | ||
| 1045 | } | ||
| 1046 | |||
| 1047 | /* nor a different secret - which is why one is generated before the fork | ||
| 1048 | * and shared, rather than per worker */ | ||
| 1049 | { | ||
| 1050 | uint8_t other[32]; | ||
| 1051 | hm_quic_rand_bytes(other, sizeof(other)); /* # uncoverable statement */ | ||
| 1052 | if (ngtcp2_crypto_verify_retry_token2( /* # uncoverable statement */ | ||
| 1053 | &got, token, (size_t)tlen, other, sizeof(other), /* # uncoverable statement */ | ||
| 1054 | NGTCP2_PROTO_VER_V1, (const ngtcp2_sockaddr *)&a, sizeof(a), | ||
| 1055 | &scid, HM_QUIC_RETRY_TIMEOUT, now) == 0) ok = 0; /* # uncoverable statement */ | ||
| 1056 | } | ||
| 1057 | |||
| 1058 | /* and the address binding holds over IPv6 the same way: a token for | ||
| 1059 | * [::1] verifies for [::1] and not for [::2] */ | ||
| 1060 | { | ||
| 1061 | struct sockaddr_in6 a6, b6; | ||
| 1062 | uint8_t t6[NGTCP2_CRYPTO_MAX_RETRY_TOKENLEN2]; | ||
| 1063 | ngtcp2_ssize t6len; | ||
| 1064 | memset(&a6, 0, sizeof(a6)); | ||
| 1065 | a6.sin6_family = AF_INET6; | ||
| 1066 | a6.sin6_port = htons(4433); | ||
| 1067 | a6.sin6_addr.s6_addr[15] = 1; /* ::1 */ | ||
| 1068 | b6 = a6; | ||
| 1069 | b6.sin6_addr.s6_addr[15] = 2; /* ::2 */ | ||
| 1070 | t6len = ngtcp2_crypto_generate_retry_token2( | ||
| 1071 | t6, secret, sizeof(secret), NGTCP2_PROTO_VER_V1, | ||
| 1072 | (const ngtcp2_sockaddr *)&a6, sizeof(a6), &scid, &odcid, now); | ||
| 1073 | if (t6len <= 0) ok = 0; /* # uncoverable branch true */ | ||
| 1074 | else { | ||
| 1075 | if (ngtcp2_crypto_verify_retry_token2( | ||
| 1076 | &got, t6, (size_t)t6len, secret, sizeof(secret), | ||
| 1077 | NGTCP2_PROTO_VER_V1, (const ngtcp2_sockaddr *)&a6, sizeof(a6), | ||
| 1078 | &scid, HM_QUIC_RETRY_TIMEOUT, now) != 0) ok = 0; | ||
| 1079 | if (ngtcp2_crypto_verify_retry_token2( /* # uncoverable statement */ | ||
| 1080 | &got, t6, (size_t)t6len, secret, sizeof(secret), /* # uncoverable statement */ | ||
| 1081 | NGTCP2_PROTO_VER_V1, (const ngtcp2_sockaddr *)&b6, sizeof(b6), | ||
| 1082 | &scid, HM_QUIC_RETRY_TIMEOUT, now) == 0) ok = 0; /* # uncoverable statement */ | ||
| 1083 | } | ||
| 1084 | } | ||
| 1085 | |||
| 1086 | /* nor a tampered one */ | ||
| 1087 | token[tlen / 2] ^= 0x01; | ||
| 1088 | if (ngtcp2_crypto_verify_retry_token2( | ||
| 1089 | &got, token, (size_t)tlen, secret, sizeof(secret), | ||
| 1090 | NGTCP2_PROTO_VER_V1, (const ngtcp2_sockaddr *)&a, sizeof(a), | ||
| 1091 | &scid, HM_QUIC_RETRY_TIMEOUT, now) == 0) ok = 0; | ||
| 1092 | token[tlen / 2] ^= 0x01; /* # uncoverable statement */ | ||
| 1093 | |||
| 1094 | /* nor an expired one */ | ||
| 1095 | if (ngtcp2_crypto_verify_retry_token2( /* # uncoverable statement */ | ||
| 1096 | &got, token, (size_t)tlen, secret, sizeof(secret), /* # uncoverable statement */ | ||
| 1097 | NGTCP2_PROTO_VER_V1, (const ngtcp2_sockaddr *)&a, sizeof(a), | ||
| 1098 | &scid, HM_QUIC_RETRY_TIMEOUT, | ||
| 1099 | now + HM_QUIC_RETRY_TIMEOUT + NGTCP2_SECONDS) == 0) ok = 0; /* # uncoverable statement */ | ||
| 1100 | |||
| 1101 | /* NEW_TOKEN: the same address rule, and a DIFFERENT magic byte - the | ||
| 1102 | * receive path tells the two kinds apart by that before it tries to | ||
| 1103 | * verify either, so they must not collide. */ | ||
| 1104 | rlen = ngtcp2_crypto_generate_regular_token( | ||
| 1105 | reg, secret, sizeof(secret), | ||
| 1106 | (const ngtcp2_sockaddr *)&a, sizeof(a), now); | ||
| 1107 | if (rlen <= 0) return 0; /* # uncoverable branch true */ | ||
| 1108 | if (reg[0] != NGTCP2_CRYPTO_TOKEN_MAGIC_REGULAR) ok = 0; | ||
| 1109 | if (reg[0] == NGTCP2_CRYPTO_TOKEN_MAGIC_RETRY2) ok = 0; /* # uncoverable statement */ | ||
| 1110 | if (ngtcp2_crypto_verify_regular_token( /* # uncoverable statement */ | ||
| 1111 | reg, (size_t)rlen, secret, sizeof(secret), /* # uncoverable statement */ | ||
| 1112 | (const ngtcp2_sockaddr *)&a, sizeof(a), | ||
| 1113 | HM_QUIC_TOKEN_TIMEOUT, now) != 0) ok = 0; /* # uncoverable statement */ | ||
| 1114 | if (ngtcp2_crypto_verify_regular_token( /* # uncoverable statement */ | ||
| 1115 | reg, (size_t)rlen, secret, sizeof(secret), /* # uncoverable statement */ | ||
| 1116 | (const ngtcp2_sockaddr *)&b, sizeof(b), | ||
| 1117 | HM_QUIC_TOKEN_TIMEOUT, now) == 0) ok = 0; /* # uncoverable statement */ | ||
| 1118 | |||
| 1119 | /* a stateless reset token is a pure function of secret and id: the same | ||
| 1120 | * pair must give the same token after a restart, or a client that has | ||
| 1121 | * one cannot recognise the reset it is sent */ | ||
| 1122 | { | ||
| 1123 | uint8_t t1[NGTCP2_STATELESS_RESET_TOKENLEN]; | ||
| 1124 | uint8_t t2[NGTCP2_STATELESS_RESET_TOKENLEN]; | ||
| 1125 | ngtcp2_cid other = scid; /* # uncoverable statement */ | ||
| 1126 | if (ngtcp2_crypto_generate_stateless_reset_token( /* # uncoverable statement */ | ||
| 1127 | t1, secret, sizeof(secret), &scid) != 0) ok = 0; /* # uncoverable statement */ | ||
| 1128 | if (ngtcp2_crypto_generate_stateless_reset_token( /* # uncoverable statement */ | ||
| 1129 | t2, secret, sizeof(secret), &scid) != 0) ok = 0; /* # uncoverable statement */ | ||
| 1130 | if (memcmp(t1, t2, sizeof(t1)) != 0) ok = 0; /* reproducible */ /* # uncoverable statement */ | ||
| 1131 | other.data[0] ^= 0xff; /* # uncoverable statement */ | ||
| 1132 | if (ngtcp2_crypto_generate_stateless_reset_token( /* # uncoverable statement */ | ||
| 1133 | t2, secret, sizeof(secret), &other) != 0) ok = 0; /* # uncoverable statement */ | ||
| 1134 | if (memcmp(t1, t2, sizeof(t1)) == 0) ok = 0; /* ...but per id */ /* # uncoverable statement */ | ||
| 1135 | } | ||
| 1136 | return ok; | ||
| 1137 | } | ||
| 1138 | |||
| 1139 | static int hm_quic_heap_selftest(void) { | ||
| 1140 | hm_qheap h; | ||
| 1141 | hm_qconn *qcs = (hm_qconn *)hm_xcalloc(256, sizeof(hm_qconn)); | ||
| 1142 | uint32_t seed = 0x243f6a88u; | ||
| 1143 | uint64_t last; | ||
| 1144 | int i, ok = 1; | ||
| 1145 | memset(&h, 0, sizeof(h)); | ||
| 1146 | for (i = 0; i < 256; i++) qcs[i].heap_idx = -1; | ||
| 1147 | |||
| 1148 | /* push in random order */ | ||
| 1149 | for (i = 0; i < 256; i++) | ||
| 1150 | hm_qheap_set(&h, &qcs[i], (uint64_t)hm_quic_st_rand(&seed)); | ||
| 1151 | if (h.n != 256) ok = 0; | ||
| 1152 | |||
| 1153 | /* every position agrees with the entry that holds it */ | ||
| 1154 | for (i = 0; i < h.n; i++) if (h.a[i]->heap_idx != i) ok = 0; | ||
| 1155 | |||
| 1156 | /* move deadlines BOTH ways - the rearm does both, and a sift that only | ||
| 1157 | * goes one way leaves the loop sleeping past a deadline */ | ||
| 1158 | for (i = 0; i < 256; i += 2) | ||
| 1159 | hm_qheap_set(&h, &qcs[i], (uint64_t)hm_quic_st_rand(&seed)); | ||
| 1160 | for (i = 1; i < 256; i += 2) | ||
| 1161 | hm_qheap_set(&h, &qcs[i], (uint64_t)hm_quic_st_rand(&seed) >> 4); | ||
| 1162 | for (i = 0; i < h.n; i++) if (h.a[i]->heap_idx != i) ok = 0; | ||
| 1163 | |||
| 1164 | /* removing from the middle keeps it a heap */ | ||
| 1165 | for (i = 0; i < 64; i++) hm_qheap_remove(&h, &qcs[i * 3]); | ||
| 1166 | for (i = 0; i < h.n; i++) if (h.a[i]->heap_idx != i) ok = 0; | ||
| 1167 | |||
| 1168 | /* draining the minimum is monotonic, which is the whole contract */ | ||
| 1169 | last = 0; | ||
| 1170 | while (h.n) { | ||
| 1171 | hm_qconn *m = h.a[0]; | ||
| 1172 | if (m->expiry < last) ok = 0; | ||
| 1173 | last = m->expiry; | ||
| 1174 | hm_qheap_remove(&h, m); | ||
| 1175 | } | ||
| 1176 | if (h.n != 0) ok = 0; | ||
| 1177 | |||
| 1178 | if (h.a) free(h.a); | ||
| 1179 | free(qcs); | ||
| 1180 | return ok; | ||
| 1181 | } | ||
| 1182 | |||
| 1183 | /* The UDP listener is readable. Mirrors hm_accept's fairness cap: take at | ||
| 1184 | * most a batch in one wakeup and let the next iteration resume, since the | ||
| 1185 | * socket stays level-readable. Without the cap one busy client's flight | ||
| 1186 | * starves every other connection this worker holds. | ||
| 1187 | * | ||
| 1188 | * The single hm_os_recvfrom call site. When recvmsg arrives - for the local | ||
| 1189 | * address and ECN - or recvmmsg for batching, it is this loop that changes | ||
| 1190 | * and nothing else. */ | ||
| 1191 | /* ---- ngtcp2 wiring ------------------------------------------------------- | ||
| 1192 | * | ||
| 1193 | * Eleven of the callbacks come ready-made from ngtcp2_crypto; the handful | ||
| 1194 | * below are the real glue. The connection is created on the first Initial | ||
| 1195 | * that names no CID we hold, and torn down by the idle timeout or a close. | ||
| 1196 | * | ||
| 1197 | * Everything from here on may allocate and may touch the interpreter, so | ||
| 1198 | * the dTHX-free rule stops at the CID lookup above. */ | ||
| 1199 | |||
| 1200 | /* Unpredictable bytes, from the OpenSSL that is already linked - HTTP/3 | ||
| 1201 | * cannot be built without TLS, so this needs no platform shim of its own. | ||
| 1202 | * A guessable Connection ID is a routing oracle, so a failure here zeroes | ||
| 1203 | * rather than falling back to something weaker; the connection that gets a | ||
| 1204 | * zero CID fails to route, which is the safe direction. */ | ||
| 1205 | static void hm_quic_rand_bytes(uint8_t *d, size_t n) { | ||
| 1206 | if (RAND_bytes(d, (int)n) != 1) memset(d, 0, n); | ||
| 1207 | } | ||
| 1208 | |||
| 1209 | static ngtcp2_conn *hm_quic_get_conn(ngtcp2_crypto_conn_ref *ref) { | ||
| 1210 | return ((hm_qconn *)ref->user_data)->conn; | ||
| 1211 | } | ||
| 1212 | |||
| 1213 | static void hm_quic_cb_rand(uint8_t *dest, size_t destlen, | ||
| 1214 | const ngtcp2_rand_ctx *rand_ctx) { | ||
| 1215 | (void)rand_ctx; | ||
| 1216 | hm_quic_rand_bytes(dest, destlen); | ||
| 1217 | } | ||
| 1218 | |||
| 1219 | /* ngtcp2 mints extra Connection IDs through NEW_CONNECTION_ID; each one has | ||
| 1220 | * to be routable to this connection, which is what the map is for. The | ||
| 1221 | * worker index goes in byte 0 - a fixed offset a steering program can read - | ||
| 1222 | * and the rest is random. */ | ||
| 1223 | static int hm_quic_cb_get_new_cid(ngtcp2_conn *conn, ngtcp2_cid *cid, | ||
| 1224 | uint8_t *token, size_t cidlen, | ||
| 1225 | void *user_data) { | ||
| 1226 | hm_qconn *qc = (hm_qconn *)user_data; | ||
| 1227 | (void)conn; | ||
| 1228 | if (cidlen > NGTCP2_MAX_CIDLEN) return NGTCP2_ERR_CALLBACK_FAILURE; /* # uncoverable branch true */ | ||
| 1229 | hm_quic_rand_bytes(cid->data, cidlen); | ||
| 1230 | cid->datalen = cidlen; | ||
| 1231 | HM_QUIC_CID_WORKER(cid) = (uint8_t)qc->srv->worker_idx; | ||
| 1232 | if (ngtcp2_crypto_generate_stateless_reset_token( /* # uncoverable branch true */ | ||
| 1233 | token, qc->srv->secret, sizeof(qc->srv->secret), cid) != 0) | ||
| 1234 | return NGTCP2_ERR_CALLBACK_FAILURE; /* # uncoverable statement */ | ||
| 1235 | if (!hm_quic_cid_add(qc->srv, qc, cid->data, cid->datalen)) /* # uncoverable branch true */ | ||
| 1236 | return NGTCP2_ERR_CALLBACK_FAILURE; /* # uncoverable statement */ | ||
| 1237 | return 0; | ||
| 1238 | } | ||
| 1239 | |||
| 1240 | /* | ||
| 1241 | # uncoverable statement | ||
| 1242 | # uncoverable subroutine */ | ||
| 1243 | static int hm_quic_cb_remove_cid(ngtcp2_conn *conn, const ngtcp2_cid *cid, | ||
| 1244 | void *user_data) { | ||
| 1245 | hm_qconn *qc = (hm_qconn *)user_data; /* # uncoverable statement */ | ||
| 1246 | (void)conn; /* # uncoverable statement */ | ||
| 1247 | hm_quic_cid_remove(qc->srv, cid->data, cid->datalen); /* # uncoverable statement */ | ||
| 1248 | return 0; /* # uncoverable statement */ | ||
| 1249 | } | ||
| 1250 | |||
| 1251 | static int hm_quic_cb_handshake_completed(ngtcp2_conn *conn, void *user_data) { | ||
| 1252 | dTHX; | ||
| 1253 | hm_qconn *qc = (hm_qconn *)user_data; | ||
| 1254 | SSL *ssl = (SSL *)qc->ssl; | ||
| 1255 | (void)conn; | ||
| 1256 | /* The same capture the TCP side does, through the same function: a QUIC | ||
| 1257 | * handshake IS a TLS handshake, so a client certificate presented over | ||
| 1258 | * HTTP/3 is reported exactly as one over HTTP/1.1 or HTTP/2. */ | ||
| 1259 | if (ssl) hm_tls_capture_ssl(ssl, &qc->tls_proto, &qc->tls_cipher, | ||
| 1260 | &qc->tls_peer); | ||
| 1261 | /* Counted so a test can see it: a completed QUIC handshake is entirely | ||
| 1262 | * C-side and leaves no trace an application could report on. */ | ||
| 1263 | qc->srv->loop->h3_conns++; | ||
| 1264 | /* The control and QPACK streams cannot be opened before there is a | ||
| 1265 | * connection to open them on, so the HTTP/3 session starts here and | ||
| 1266 | * not at connection creation. */ | ||
| 1267 | if (!qc->h3 && hm_h3_session_start(aTHX_ qc) != 0) | ||
| 1268 | return NGTCP2_ERR_CALLBACK_FAILURE; /* # uncoverable statement */ | ||
| 1269 | if (!qc->handshake_done) { | ||
| 1270 | qc->handshake_done = 1; | ||
| 1271 | if (qc->srv->handshaking > 0) qc->srv->handshaking--; | ||
| 1272 | if (qc->src_counted) { qc->src_counted = 0; hm_quic_src_dec(qc->srv, (const struct sockaddr *)&qc->peer); } | ||
| 1273 | } | ||
| 1274 | /* NEW_TOKEN, so this client's next visit skips address validation | ||
| 1275 | * entirely - no Retry round trip even while the server is demanding one | ||
| 1276 | * of everybody else. It is bound to the address it was issued to, which | ||
| 1277 | * is what makes it safe to trust on the way back in. */ | ||
| 1278 | { | ||
| 1279 | uint8_t token[NGTCP2_CRYPTO_MAX_REGULAR_TOKENLEN]; | ||
| 1280 | ngtcp2_ssize n = ngtcp2_crypto_generate_regular_token( | ||
| 1281 | token, qc->srv->secret, sizeof(qc->srv->secret), | ||
| 1282 | (const ngtcp2_sockaddr *)&qc->peer, (ngtcp2_socklen)qc->peerlen, | ||
| 1283 | hm_now_ns()); | ||
| 1284 | if (n > 0) (void)ngtcp2_conn_submit_new_token(conn, token, (size_t)n); | ||
| 1285 | } | ||
| 1286 | return 0; | ||
| 1287 | } | ||
| 1288 | |||
| 1289 | /* Streams are HTTP/3's business (hm_http3.h). Until that lands the transport | ||
| 1290 | * accepts and discards them, which is what lets a handshake be tested on its | ||
| 1291 | * own. */ | ||
| 1292 | static int hm_quic_cb_recv_stream_data(ngtcp2_conn *conn, uint32_t flags, | ||
| 1293 | int64_t stream_id, uint64_t offset, | ||
| 1294 | const uint8_t *data, size_t datalen, | ||
| 1295 | void *user_data, | ||
| 1296 | void *stream_user_data) { | ||
| 1297 | dTHX; | ||
| 1298 | hm_qconn *qc = (hm_qconn *)user_data; | ||
| 1299 | (void)conn; (void)offset; (void)stream_user_data; | ||
| 1300 | /* Stream bytes belong to HTTP/3. nghttp3 credits the flow-control window | ||
| 1301 | * back through recv_data and deferred_consume, so it is NOT credited | ||
| 1302 | * here as well - doing both opens the window twice and lets a peer send | ||
| 1303 | * past what was advertised. */ | ||
| 1304 | if (hm_h3_recv_stream(aTHX_ qc, stream_id, data, datalen, /* # uncoverable branch true */ | ||
| 1305 | (flags & NGTCP2_STREAM_DATA_FLAG_FIN) ? 1 : 0) != 0) | ||
| 1306 | return NGTCP2_ERR_CALLBACK_FAILURE; /* # uncoverable statement */ | ||
| 1307 | return 0; | ||
| 1308 | } | ||
| 1309 | |||
| 1310 | /* The peer opened this stream's send window again. nghttp3 has to be told, | ||
| 1311 | * or a stream it stopped offering when the window closed is never offered | ||
| 1312 | * again and the response simply stops. */ | ||
| 1313 | static int hm_quic_cb_extend_max_stream_data(ngtcp2_conn *conn, | ||
| 1314 | int64_t stream_id, | ||
| 1315 | uint64_t max_data, | ||
| 1316 | void *user_data, | ||
| 1317 | void *stream_user_data) { | ||
| 1318 | dTHX; | ||
| 1319 | hm_qconn *qc = (hm_qconn *)user_data; | ||
| 1320 | (void)conn; (void)max_data; (void)stream_user_data; | ||
| 1321 | if (hm_h3_unblock(aTHX_ qc, stream_id) != 0) /* # uncoverable branch true */ | ||
| 1322 | return NGTCP2_ERR_CALLBACK_FAILURE; /* # uncoverable statement */ | ||
| 1323 | return 0; | ||
| 1324 | } | ||
| 1325 | |||
| 1326 | static int hm_quic_cb_stream_close(ngtcp2_conn *conn, uint32_t flags, | ||
| 1327 | int64_t stream_id, uint64_t app_error_code, | ||
| 1328 | void *user_data, void *stream_user_data) { | ||
| 1329 | dTHX; | ||
| 1330 | hm_qconn *qc = (hm_qconn *)user_data; | ||
| 1331 | (void)conn; (void)stream_user_data; | ||
| 1332 | if (!(flags & NGTCP2_STREAM_CLOSE_FLAG_APP_ERROR_CODE_SET)) | ||
| 1333 | app_error_code = NGHTTP3_H3_NO_ERROR; | ||
| 1334 | if (hm_h3_stream_gone(aTHX_ qc, stream_id, app_error_code) != 0) /* # uncoverable branch true */ | ||
| 1335 | return NGTCP2_ERR_CALLBACK_FAILURE; /* # uncoverable statement */ | ||
| 1336 | return 0; | ||
| 1337 | } | ||
| 1338 | |||
| 1339 | /* Bytes the peer acknowledged. nghttp3 hands back POINTERS into our own | ||
| 1340 | * buffers, so this is what says they may be reused - the half nghttp2 never | ||
| 1341 | * needed, because there the body was copied into a window it owned. */ | ||
| 1342 | static int hm_quic_cb_acked_stream_data(ngtcp2_conn *conn, int64_t stream_id, | ||
| 1343 | uint64_t offset, uint64_t datalen, | ||
| 1344 | void *user_data, | ||
| 1345 | void *stream_user_data) { | ||
| 1346 | dTHX; | ||
| 1347 | hm_qconn *qc = (hm_qconn *)user_data; | ||
| 1348 | (void)conn; (void)offset; (void)stream_user_data; | ||
| 1349 | if (hm_h3_acked(aTHX_ qc, stream_id, datalen) != 0) /* # uncoverable branch true */ | ||
| 1350 | return NGTCP2_ERR_CALLBACK_FAILURE; /* # uncoverable statement */ | ||
| 1351 | return 0; | ||
| 1352 | } | ||
| 1353 | |||
| 1354 | static void hm_quic_settings(hm_qconn *qc, ngtcp2_settings *s, | ||
| 1355 | ngtcp2_transport_params *p) { | ||
| 1356 | ngtcp2_settings_default(s); | ||
| 1357 | ngtcp2_transport_params_default(p); | ||
| 1358 | s->initial_ts = hm_now_ns(); | ||
| 1359 | s->rand_ctx.native_handle = NULL; | ||
| 1360 | /* A server must not send more than three times what it received before | ||
| 1361 | * the client's address is validated. ngtcp2 enforces that itself, but | ||
| 1362 | * only if it is fed honest byte counts and paths - which is why nothing | ||
| 1363 | * here writes around it. */ | ||
| 1364 | p->initial_max_stream_data_bidi_local = 256 * 1024; | ||
| 1365 | p->initial_max_stream_data_bidi_remote = 256 * 1024; | ||
| 1366 | p->initial_max_stream_data_uni = 256 * 1024; | ||
| 1367 | p->initial_max_data = 1024 * 1024; | ||
| 1368 | p->initial_max_streams_bidi = 128; | ||
| 1369 | p->initial_max_streams_uni = 8; | ||
| 1370 | p->max_idle_timeout = 30 * NGTCP2_SECONDS; | ||
| 1371 | p->stateless_reset_token_present = 0; | ||
| 1372 | (void)qc; | ||
| 1373 | } | ||
| 1374 | |||
| 1375 | static void hm_quic_secret_init(void) { | ||
| 1376 | if (hm_quic_secret_ready) return; | ||
| 1377 | hm_quic_rand_bytes(hm_quic_secret, sizeof(hm_quic_secret)); | ||
| 1378 | hm_quic_secret_ready = 1; | ||
| 1379 | } | ||
| 1380 | |||
| 1381 | /* The QUIC context is built by hm_tls.h through hm_tls_ctx_one with | ||
| 1382 | * HM_TLSCTX_QUIC, not here. That is what gets it the same certificate, the | ||
| 1383 | * same client-cert policy and the same SESSION TICKET KEY as the listener's | ||
| 1384 | * TCP context - a QUIC context minting its own key silently costs every | ||
| 1385 | * cross-protocol resumption a full handshake. The ALPN callback, the TLS 1.3 | ||
| 1386 | * pin and the 0-RTT refusal live there too, beside their TCP counterparts. | ||
| 1387 | */ | ||
| 1388 | |||
| 1389 | static hm_quic_srv *hm_quic_srv_new(hm_loop *loop, hm_listener *lst, int widx) { | ||
| 1390 | hm_quic_srv *srv; | ||
| 1391 | unsigned char kbuf[HM_TLS_TKEY_MAX]; | ||
| 1392 | size_t klen = 0; | ||
| 1393 | const unsigned char *tkey = NULL; | ||
| 1394 | void *ctx; | ||
| 1395 | /* The key the TCP context of this same listener is already using. */ | ||
| 1396 | if (lst->tls_ctx && hm_tls_ctx_tkey(lst->tls_ctx, kbuf, &klen)) tkey = kbuf; | ||
| 1397 | ctx = hm_tls_quic_ctx_build(lst->tls_cert, lst->tls_key, lst->tls_ca, | ||
| 1398 | lst->tls_verify, tkey, klen); | ||
| 1399 | if (!ctx) return NULL; /* # uncoverable branch true */ | ||
| 1400 | srv = (hm_quic_srv *)hm_xcalloc(1, sizeof(hm_quic_srv)); | ||
| 1401 | srv->lst = lst; | ||
| 1402 | srv->loop = loop; | ||
| 1403 | srv->worker_idx = widx; | ||
| 1404 | srv->ssl_ctx = ctx; | ||
| 1405 | hm_quic_secret_init(); | ||
| 1406 | memcpy(srv->secret, hm_quic_secret, sizeof(srv->secret)); | ||
| 1407 | return srv; | ||
| 1408 | } | ||
| 1409 | |||
| 1410 | /* ---- one connection ------------------------------------------------------ */ | ||
| 1411 | |||
| 1412 | static void hm_quic_conn_free(pTHX_ hm_qconn *qc) { | ||
| 1413 | hm_quic_srv *srv = qc->srv; | ||
| 1414 | hm_qconn **pp; | ||
| 1415 | /* A handler for one of its streams is on the stack: only MARK it, and | ||
| 1416 | * hm_h3_run_ready frees it when that handler returns. Freeing here | ||
| 1417 | * handed the frame a freed qc and a freed stream to write its response | ||
| 1418 | * into (the expiry sweep did exactly that under any ->get longer than | ||
| 1419 | * the ack delay). */ | ||
| 1420 | if (qc->in_app) { qc->close_deferred = 1; return; } /* # uncoverable branch true */ | ||
| 1421 | hm_qheap_remove(&srv->loop->qheap, qc); | ||
| 1422 | hm_quic_cid_drop_conn(srv, qc); | ||
| 1423 | hm_quic_id_remove(srv, qc); | ||
| 1424 | for (pp = &srv->conns; *pp; pp = &(*pp)->next) | ||
| 1425 | if (*pp == qc) { *pp = qc->next; break; } | ||
| 1426 | srv->nconns--; | ||
| 1427 | if (!qc->handshake_done) { | ||
| 1428 | if (srv->handshaking > 0) srv->handshaking--; | ||
| 1429 | if (qc->src_counted) { qc->src_counted = 0; hm_quic_src_dec(srv, (const struct sockaddr *)&qc->peer); } | ||
| 1430 | } | ||
| 1431 | if (srv->loop->h3_live > 0) srv->loop->h3_live--; | ||
| 1432 | hm_h3_session_free(aTHX_ qc); | ||
| 1433 | if (qc->conn) ngtcp2_conn_del(qc->conn); | ||
| 1434 | /* The ossl ctx owns the SSL once it has been handed over, so the SSL is | ||
| 1435 | * freed through it and never directly - freeing both is a double free. */ | ||
| 1436 | if (qc->ossl) ngtcp2_crypto_ossl_ctx_del(qc->ossl); /* # uncoverable branch false */ | ||
| 1437 | else if (qc->ssl) SSL_free((SSL *)qc->ssl); /* # uncoverable statement */ | ||
| 1438 | if (qc->tls_peer) { | ||
| 1439 | hm_tls_peer *tp = (hm_tls_peer *)qc->tls_peer; | ||
| 1440 | if (tp->subject) free(tp->subject); | ||
| 1441 | if (tp->issuer) free(tp->issuer); | ||
| 1442 | free(tp); | ||
| 1443 | qc->tls_peer = NULL; | ||
| 1444 | } | ||
| 1445 | free(qc); | ||
| 1446 | } | ||
| 1447 | |||
| 1448 | /* Rearm this connection's place in the loop's expiry heap. Called after | ||
| 1449 | * every read and every write, because that is how often ngtcp2 moves it. */ | ||
| 1450 | static void hm_quic_rearm(hm_qconn *qc) { | ||
| 1451 | ngtcp2_tstamp t = ngtcp2_conn_get_expiry(qc->conn); | ||
| 1452 | /* Out of the heap while a handler runs: hm_quic_expire would otherwise | ||
| 1453 | * find it due, be unable to free it, and spin. Re-armed by | ||
| 1454 | * hm_h3_run_ready when the handler returns; ngtcp2 copes with a late | ||
| 1455 | * expiry. */ | ||
| 1456 | if (qc->in_app) { hm_qheap_remove(&qc->srv->loop->qheap, qc); return; } | ||
| 1457 | if (t == UINT64_MAX) hm_qheap_remove(&qc->srv->loop->qheap, qc); /* # uncoverable branch true */ | ||
| 1458 | else hm_qheap_set(&qc->srv->loop->qheap, qc, (uint64_t)t); | ||
| 1459 | } | ||
| 1460 | |||
| 1461 | /* The write half. QUIC PULLS: there is no send callback and no write | ||
| 1462 | * buffer - bytes are asked for and handed straight to sendto. This is the | ||
| 1463 | * counterpart of hm_h2_flush_send, and it lives here rather than in | ||
| 1464 | * hm_http3.h for exactly that reason. 0 to keep the connection, -1 if it | ||
| 1465 | * was freed. */ | ||
| 1466 | static int hm_quic_write(pTHX_ hm_qconn *qc) { | ||
| 1467 | uint8_t buf[1452]; /* a conservative path MTU */ | ||
| 1468 | ngtcp2_path_storage ps; | ||
| 1469 | ngtcp2_pkt_info pi; | ||
| 1470 | /* Called from inside a read_pkt callback (the application answered a | ||
| 1471 | * stream synchronously): ngtcp2 forbids writev_stream there, and the | ||
| 1472 | * error paths below would free the connection read_pkt is still | ||
| 1473 | * executing on. hm_quic_packet writes once read_pkt has returned. */ | ||
| 1474 | if (qc->in_read) return 0; /* # uncoverable branch true */ | ||
| 1475 | if (qc->close_deferred) return -1; /* as good as gone */ /* # uncoverable branch true */ | ||
| 1476 | for (;;) { | ||
| 1477 | nghttp3_vec vec[16]; | ||
| 1478 | ngtcp2_ssize n, wrote = 0; | ||
| 1479 | nghttp3_ssize nvec = 0; | ||
| 1480 | int64_t sid = -1; | ||
| 1481 | int fin = 0; | ||
| 1482 | uint32_t flags = NGTCP2_WRITE_STREAM_FLAG_MORE; | ||
| 1483 | |||
| 1484 | /* Ask HTTP/3 what it wants sent, then hand that to the transport. | ||
| 1485 | * This is the direction the h2 template does not have: nghttp2 | ||
| 1486 | * PUSHED through a send callback, nghttp3 is PULLED from here. */ | ||
| 1487 | if (qc->h3) { | ||
| 1488 | nvec = hm_h3_pull(aTHX_ qc, &sid, &fin, vec, | ||
| 1489 | sizeof(vec) / sizeof(vec[0])); | ||
| 1490 | if (nvec < 0) { hm_quic_conn_free(aTHX_ qc); return -1; } /* # uncoverable branch true */ | ||
| 1491 | } | ||
| 1492 | if (sid < 0) flags = NGTCP2_WRITE_STREAM_FLAG_NONE; | ||
| 1493 | else if (fin) flags |= NGTCP2_WRITE_STREAM_FLAG_FIN; | ||
| 1494 | |||
| 1495 | ngtcp2_path_storage_zero(&ps); | ||
| 1496 | n = ngtcp2_conn_writev_stream(qc->conn, &ps.path, &pi, buf, sizeof(buf), | ||
| 1497 | &wrote, flags, sid, | ||
| 1498 | (const ngtcp2_vec *)vec, (size_t)nvec, | ||
| 1499 | hm_now_ns()); | ||
| 1500 | if (n < 0) { | ||
| 1501 | if (n == NGTCP2_ERR_WRITE_MORE) { /* # uncoverable branch false class:ignore_covered_err */ | ||
| 1502 | /* Accepted into the packet being built but not yet framed | ||
| 1503 | * out; tell nghttp3 how much it may release and go round. */ | ||
| 1504 | if (wrote > 0 && hm_h3_wrote(aTHX_ qc, sid, (size_t)wrote) != 0) { | ||
| 1505 | hm_quic_conn_free(aTHX_ qc); /* # uncoverable statement */ | ||
| 1506 | return -1; /* # uncoverable statement */ | ||
| 1507 | } | ||
| 1508 | continue; | ||
| 1509 | } | ||
| 1510 | if (n == NGTCP2_ERR_STREAM_DATA_BLOCKED /* # uncoverable statement class:ignore_covered_err */ | ||
| 1511 | || n == NGTCP2_ERR_STREAM_SHUT_WR) { /* # uncoverable statement class:ignore_covered_err */ | ||
| 1512 | /* This stream cannot progress; others on the connection | ||
| 1513 | * still can, so block it rather than the whole session. */ | ||
| 1514 | if (qc->h3 && sid >= 0) /* # uncoverable statement class:ignore_covered_err */ | ||
| 1515 | nghttp3_conn_block_stream((nghttp3_conn *)qc->h3, sid); /* # uncoverable statement class:ignore_covered_err */ | ||
| 1516 | continue; /* # uncoverable statement class:ignore_covered_err */ | ||
| 1517 | } | ||
| 1518 | hm_quic_conn_free(aTHX_ qc); /* # uncoverable statement */ | ||
| 1519 | return -1; /* # uncoverable statement */ | ||
| 1520 | } | ||
| 1521 | if (wrote > 0 && hm_h3_wrote(aTHX_ qc, sid, (size_t)wrote) != 0) { | ||
| 1522 | hm_quic_conn_free(aTHX_ qc); /* # uncoverable statement */ | ||
| 1523 | return -1; /* # uncoverable statement */ | ||
| 1524 | } | ||
| 1525 | if (n == 0) break; /* nothing more to send */ | ||
| 1526 | if (hm_os_sendto(qc->srv->lst->udp_fd, buf, (size_t)n, /* # uncoverable branch true */ | ||
| 1527 | (struct sockaddr *)&qc->peer, qc->peerlen) < 0) { | ||
| 1528 | if (errno == EAGAIN || errno == EWOULDBLOCK) break; /* # uncoverable statement */ | ||
| 1529 | hm_quic_conn_free(aTHX_ qc); /* # uncoverable statement */ | ||
| 1530 | return -1; /* # uncoverable statement */ | ||
| 1531 | } | ||
| 1532 | qc->srv->loop->bytes_out += (UV)n; | ||
| 1533 | qc->srv->loop->datagrams_out++; | ||
| 1534 | } | ||
| 1535 | hm_quic_rearm(qc); | ||
| 1536 | return 0; | ||
| 1537 | } | ||
| 1538 | |||
| 1539 | /* A new connection, from an Initial naming no CID we hold. */ | ||
| 1540 | static hm_qconn *hm_quic_conn_new(pTHX_ hm_quic_srv *srv, | ||
| 1541 | const ngtcp2_version_cid *vc, | ||
| 1542 | const ngtcp2_pkt_hd *hd, | ||
| 1543 | const ngtcp2_cid *retry_odcid, | ||
| 1544 | const struct sockaddr *peer, | ||
| 1545 | socklen_t peerlen) { | ||
| 1546 | ngtcp2_callbacks cbs; | ||
| 1547 | ngtcp2_settings settings; | ||
| 1548 | ngtcp2_transport_params params; | ||
| 1549 | ngtcp2_cid dcid = {0}, scid = {0}, ocid = {0}; | ||
| 1550 | hm_qconn *qc; | ||
| 1551 | SSL *ssl; | ||
| 1552 | struct sockaddr_storage local; | ||
| 1553 | socklen_t locallen = sizeof(local); | ||
| 1554 | |||
| 1555 | qc = (hm_qconn *)hm_xcalloc(1, sizeof(hm_qconn)); | ||
| 1556 | qc->srv = srv; | ||
| 1557 | qc->id = ++hm_id_counter; | ||
| 1558 | qc->heap_idx = -1; | ||
| 1559 | memcpy(&qc->peer, peer, peerlen); | ||
| 1560 | qc->peerlen = peerlen; | ||
| 1561 | /* Formatted once here rather than per request: REMOTE_ADDR is wanted on | ||
| 1562 | * every request this connection ever carries, and the address does not | ||
| 1563 | * change (migration is deferred). */ | ||
| 1564 | hm_fmt_peer(qc->peer_str, sizeof(qc->peer_str), &qc->peer_port, | ||
| 1565 | (struct sockaddr *)&qc->peer); | ||
| 1566 | |||
| 1567 | /* Our own Connection ID, with the worker index where a steering program | ||
| 1568 | * can find it. The client's SCID becomes our DCID. */ | ||
| 1569 | hm_quic_rand_bytes(scid.data, HM_QUIC_CIDLEN); | ||
| 1570 | scid.datalen = HM_QUIC_CIDLEN; | ||
| 1571 | HM_QUIC_CID_WORKER(&scid) = (uint8_t)srv->worker_idx; | ||
| 1572 | memcpy(dcid.data, vc->scid, vc->scidlen); | ||
| 1573 | dcid.datalen = vc->scidlen; | ||
| 1574 | memcpy(ocid.data, vc->dcid, vc->dcidlen); | ||
| 1575 | ocid.datalen = vc->dcidlen; | ||
| 1576 | |||
| 1577 | memset(&cbs, 0, sizeof(cbs)); | ||
| 1578 | /* The eleven ngtcp2_crypto supplies ready-made. */ | ||
| 1579 | cbs.recv_client_initial = ngtcp2_crypto_recv_client_initial_cb; | ||
| 1580 | cbs.recv_crypto_data = ngtcp2_crypto_recv_crypto_data_cb; | ||
| 1581 | cbs.encrypt = ngtcp2_crypto_encrypt_cb; | ||
| 1582 | cbs.decrypt = ngtcp2_crypto_decrypt_cb; | ||
| 1583 | cbs.hp_mask = ngtcp2_crypto_hp_mask_cb; | ||
| 1584 | cbs.update_key = ngtcp2_crypto_update_key_cb; | ||
| 1585 | cbs.delete_crypto_aead_ctx = ngtcp2_crypto_delete_crypto_aead_ctx_cb; | ||
| 1586 | cbs.delete_crypto_cipher_ctx = ngtcp2_crypto_delete_crypto_cipher_ctx_cb; | ||
| 1587 | cbs.get_path_challenge_data = ngtcp2_crypto_get_path_challenge_data_cb; | ||
| 1588 | cbs.version_negotiation = ngtcp2_crypto_version_negotiation_cb; | ||
| 1589 | /* ...and the glue that is actually ours. */ | ||
| 1590 | cbs.rand = hm_quic_cb_rand; | ||
| 1591 | cbs.get_new_connection_id = hm_quic_cb_get_new_cid; | ||
| 1592 | cbs.remove_connection_id = hm_quic_cb_remove_cid; | ||
| 1593 | cbs.handshake_completed = hm_quic_cb_handshake_completed; | ||
| 1594 | cbs.recv_stream_data = hm_quic_cb_recv_stream_data; | ||
| 1595 | cbs.stream_close = hm_quic_cb_stream_close; | ||
| 1596 | cbs.acked_stream_data_offset = hm_quic_cb_acked_stream_data; | ||
| 1597 | cbs.extend_max_stream_data = hm_quic_cb_extend_max_stream_data; | ||
| 1598 | |||
| 1599 | hm_quic_settings(qc, &settings, ¶ms); | ||
| 1600 | if (retry_odcid) { /* # uncoverable branch true */ | ||
| 1601 | /* Validated by Retry. The transport parameters must then name the id | ||
| 1602 | * the client used BEFORE the Retry as the original, and the one it | ||
| 1603 | * used after as retry_scid - the client checks both, and getting | ||
| 1604 | * them the wrong way round fails the handshake with no useful error. */ | ||
| 1605 | params.original_dcid = *retry_odcid; /* # uncoverable statement */ | ||
| 1606 | params.retry_scid = hd->dcid; /* # uncoverable statement */ | ||
| 1607 | params.retry_scid_present = 1; /* # uncoverable statement */ | ||
| 1608 | } else { | ||
| 1609 | params.original_dcid = ocid; | ||
| 1610 | } | ||
| 1611 | params.original_dcid_present = 1; | ||
| 1612 | if (hd->tokenlen) { | ||
| 1613 | settings.token = hd->token; /* # uncoverable statement */ | ||
| 1614 | settings.tokenlen = hd->tokenlen; /* # uncoverable statement */ | ||
| 1615 | settings.token_type = retry_odcid ? NGTCP2_TOKEN_TYPE_RETRY /* # uncoverable statement */ | ||
| 1616 | : NGTCP2_TOKEN_TYPE_NEW_TOKEN; | ||
| 1617 | } | ||
| 1618 | |||
| 1619 | if (getsockname(srv->lst->udp_fd, (struct sockaddr *)&local, &locallen) < 0) /* # uncoverable statement */ | ||
| 1620 | locallen = 0; /* # uncoverable statement */ | ||
| 1621 | ngtcp2_path_storage_init(&qc->path, | ||
| 1622 | (struct sockaddr *)&local, locallen, | ||
| 1623 | (struct sockaddr *)&qc->peer, peerlen, NULL); | ||
| 1624 | |||
| 1625 | if (ngtcp2_conn_server_new(&qc->conn, &dcid, &scid, &qc->path.path, /* # uncoverable branch true */ | ||
| 1626 | vc->version, &cbs, &settings, ¶ms, | ||
| 1627 | NULL, qc) != 0) { | ||
| 1628 | free(qc); /* # uncoverable statement */ | ||
| 1629 | return NULL; /* # uncoverable statement */ | ||
| 1630 | } | ||
| 1631 | |||
| 1632 | ssl = SSL_new((SSL_CTX *)srv->ssl_ctx); | ||
| 1633 | if (!ssl || ngtcp2_crypto_ossl_ctx_new(&qc->ossl, ssl) != 0 | ||
| 1634 | || ngtcp2_crypto_ossl_configure_server_session(ssl) != 0) { | ||
| 1635 | if (ssl && !qc->ossl) SSL_free(ssl); /* # uncoverable statement */ | ||
| 1636 | ngtcp2_conn_del(qc->conn); /* # uncoverable statement */ | ||
| 1637 | free(qc); /* # uncoverable statement */ | ||
| 1638 | return NULL; /* # uncoverable statement */ | ||
| 1639 | } | ||
| 1640 | qc->ssl = ssl; | ||
| 1641 | qc->conn_ref.get_conn = hm_quic_get_conn; | ||
| 1642 | qc->conn_ref.user_data = qc; | ||
| 1643 | SSL_set_app_data(ssl, &qc->conn_ref); | ||
| 1644 | /* Without this the SSL has no side and the first Initial comes back as | ||
| 1645 | * NGTCP2_ERR_CRYPTO with nothing in the error queue to say why. The TLS | ||
| 1646 | * paths elsewhere in this dist get their accept state from SSL_accept; | ||
| 1647 | * QUIC never calls it, because ngtcp2 drives the handshake itself. */ | ||
| 1648 | SSL_set_accept_state(ssl); | ||
| 1649 | ngtcp2_conn_set_tls_native_handle(qc->conn, qc->ossl); | ||
| 1650 | |||
| 1651 | if (!hm_quic_cid_add(srv, qc, scid.data, scid.datalen)) { /* # uncoverable branch true */ | ||
| 1652 | hm_quic_conn_free(aTHX_ qc); /* # uncoverable statement */ | ||
| 1653 | return NULL; /* # uncoverable statement */ | ||
| 1654 | } | ||
| 1655 | /* The client's ORIGINAL destination id, the random one it invented for | ||
| 1656 | * its first Initial, is routed here too. Until the client has seen our | ||
| 1657 | * reply it keeps addressing that, so without this a retransmitted | ||
| 1658 | * Initial - which is every handshake with any loss, and was every second | ||
| 1659 | * packet in testing - looks like a brand new connection and builds a | ||
| 1660 | * second one that then fails. Retired by ngtcp2 through | ||
| 1661 | * remove_connection_id once the handshake has moved on. */ | ||
| 1662 | if (ocid.datalen && ocid.datalen <= NGTCP2_MAX_CIDLEN) | ||
| 1663 | (void)hm_quic_cid_add(srv, qc, ocid.data, ocid.datalen); | ||
| 1664 | hm_quic_id_add(srv, qc); | ||
| 1665 | qc->next = srv->conns; | ||
| 1666 | srv->conns = qc; | ||
| 1667 | srv->nconns++; | ||
| 1668 | srv->handshaking++; | ||
| 1669 | qc->src_counted = hm_quic_src_inc(srv, peer) >= 0; | ||
| 1670 | srv->loop->h3_live++; | ||
| 1671 | return qc; | ||
| 1672 | } | ||
| 1673 | |||
| 1674 | /* The soonest QUIC deadline, or UINT64_MAX when there is none. */ | ||
| 1675 | static uint64_t hm_quic_next_deadline(hm_loop *loop) { | ||
| 1676 | return loop->qheap.n ? loop->qheap.a[0]->expiry : (uint64_t)-1; | ||
| 1677 | } | ||
| 1678 | |||
| 1679 | /* Every connection whose deadline has passed. Called once per loop turn, | ||
| 1680 | * after the dispatch, and gated on the heap being non-empty - which is one | ||
| 1681 | * compare on a server with no QUIC listener. */ | ||
| 1682 | static void hm_quic_expire(pTHX_ hm_loop *loop) { | ||
| 1683 | uint64_t now = hm_now_ns(); | ||
| 1684 | while (loop->qheap.n && loop->qheap.a[0]->expiry <= now) { | ||
| 1685 | hm_qconn *qc = loop->qheap.a[0]; | ||
| 1686 | /* Its handler is on the stack (hm_quic_rearm keeps such a | ||
| 1687 | * connection out of the heap; this is the belt to that brace). */ | ||
| 1688 | if (qc->in_app) { hm_qheap_remove(&loop->qheap, qc); continue; } /* # uncoverable branch true */ | ||
| 1689 | if (ngtcp2_conn_handle_expiry(qc->conn, now) != 0) { /* # uncoverable branch true */ | ||
| 1690 | hm_quic_conn_free(aTHX_ qc); /* # uncoverable statement */ | ||
| 1691 | continue; /* # uncoverable statement */ | ||
| 1692 | } | ||
| 1693 | if (hm_quic_write(aTHX_ qc) < 0) continue; /* # uncoverable branch true */ | ||
| 1694 | /* hm_quic_write rearmed it; if the deadline did not move we would | ||
| 1695 | * spin, so a connection that stays due is dropped rather than | ||
| 1696 | * looped on. */ | ||
| 1697 | if (loop->qheap.n && loop->qheap.a[0] == qc && qc->expiry <= now) | ||
| 1698 | hm_quic_conn_free(aTHX_ qc); /* # uncoverable statement */ | ||
| 1699 | } | ||
| 1700 | } | ||
| 1701 | |||
| 1702 | /* Build this worker's QUIC server for a listener, and tear it down. Called | ||
| 1703 | * from hm_attach_server and hm_loop_free beside the TCP half. */ | ||
| 1704 | static void hm_quic_srv_attach(pTHX_ hm_loop *loop, hm_listener *lst, int widx) { | ||
| 1705 | PERL_UNUSED_CONTEXT; | ||
| 1706 | if (lst->udp_fd < 0 || lst->quic) return; | ||
| 1707 | lst->quic = (void *)hm_quic_srv_new(loop, lst, widx); | ||
| 1708 | if (lst->quic) lst->quic_ctx = ((hm_quic_srv *)lst->quic)->ssl_ctx; | ||
| 1709 | } | ||
| 1710 | |||
| 1711 | /* Tell every live connection the server is going away. | ||
| 1712 | * | ||
| 1713 | * A QUIC connection is not reachable through anything fd-indexed, so none of | ||
| 1714 | * the TCP shutdown path touches it: without this a client waits out its idle | ||
| 1715 | * timeout instead of being told, and a restart looks like a network fault. | ||
| 1716 | * CONNECTION_CLOSE is written directly rather than through the pull loop, | ||
| 1717 | * because the connection is being torn down and there is nothing after it. */ | ||
| 1718 | static void hm_quic_shutdown(pTHX_ hm_loop *loop) { | ||
| 1719 | int i; | ||
| 1720 | for (i = 0; i < loop->nlisteners; i++) { | ||
| 1721 | hm_quic_srv *srv = (hm_quic_srv *)loop->listeners[i].quic; | ||
| 1722 | hm_qconn *qc; | ||
| 1723 | if (!srv) continue; | ||
| 1724 | for (qc = srv->conns; qc; qc = qc->next) { | ||
| 1725 | uint8_t buf[1452]; | ||
| 1726 | ngtcp2_path_storage ps; | ||
| 1727 | ngtcp2_pkt_info pi; | ||
| 1728 | ngtcp2_ccerr err; | ||
| 1729 | ngtcp2_ssize n; | ||
| 1730 | if (qc->closing || qc->draining) continue; | ||
| 1731 | ngtcp2_ccerr_default(&err); | ||
| 1732 | ngtcp2_path_storage_zero(&ps); | ||
| 1733 | n = ngtcp2_conn_write_connection_close(qc->conn, &ps.path, &pi, | ||
| 1734 | buf, sizeof(buf), &err, | ||
| 1735 | hm_now_ns()); | ||
| 1736 | if (n > 0) (void)hm_os_sendto(srv->lst->udp_fd, buf, (size_t)n, | ||
| 1737 | (struct sockaddr *)&qc->peer, | ||
| 1738 | qc->peerlen); | ||
| 1739 | qc->closing = 1; | ||
| 1740 | } | ||
| 1741 | } | ||
| 1742 | } | ||
| 1743 | |||
| 1744 | /* Install a rebuilt QUIC context on a listener. | ||
| 1745 | * | ||
| 1746 | * Connections already up keep the SSL they hold - their handshake is long | ||
| 1747 | * finished and the certificate is not consulted again - so nothing in flight | ||
| 1748 | * is disturbed. The next handshake this worker accepts uses the new one. | ||
| 1749 | * Exactly the rule the TCP side follows. */ | ||
| 1750 | /* | ||
| 1751 | # uncoverable statement | ||
| 1752 | # uncoverable subroutine */ | ||
| 1753 | static void hm_quic_ctx_swap(pTHX_ hm_listener *lst, void *fresh) { | ||
| 1754 | hm_quic_srv *srv = (hm_quic_srv *)lst->quic; /* # uncoverable statement */ | ||
| 1755 | PERL_UNUSED_CONTEXT; | ||
| 1756 | if (!srv || !fresh) return; /* # uncoverable statement */ | ||
| 1757 | if (srv->ssl_ctx) SSL_CTX_free((SSL_CTX *)srv->ssl_ctx); /* # uncoverable statement */ | ||
| 1758 | srv->ssl_ctx = fresh; /* # uncoverable statement */ | ||
| 1759 | lst->quic_ctx = fresh; /* # uncoverable statement */ | ||
| 1760 | } | ||
| 1761 | |||
| 1762 | static void hm_quic_srv_free(pTHX_ hm_listener *lst) { | ||
| 1763 | hm_quic_srv *srv = (hm_quic_srv *)lst->quic; | ||
| 1764 | if (!srv) return; | ||
| 1765 | while (srv->conns) { | ||
| 1766 | srv->conns->in_app = 0; /* teardown: no frame to defer to */ | ||
| 1767 | hm_quic_conn_free(aTHX_ srv->conns); | ||
| 1768 | } | ||
| 1769 | if (srv->ssl_ctx) SSL_CTX_free((SSL_CTX *)srv->ssl_ctx); | ||
| 1770 | free(srv); | ||
| 1771 | lst->quic = NULL; | ||
| 1772 | lst->quic_ctx = NULL; | ||
| 1773 | } | ||
| 1774 | |||
| 1775 | /* Run the decoder over bytes a test hands in. The first thing that touches | ||
| 1776 | * an attacker-controlled datagram, reachable from Perl so the hand-built | ||
| 1777 | * cases - a valid Initial, a truncated one, garbage, an unsupported version | ||
| 1778 | * with an over-long Connection ID - can be written as byte strings. */ | ||
| 1779 | static void hm_quic_decode_probe(const unsigned char *p, size_t n, | ||
| 1780 | int *rv, uint32_t *ver, | ||
| 1781 | size_t *dlen, size_t *slen) { | ||
| 1782 | ngtcp2_version_cid vc; | ||
| 1783 | memset(&vc, 0, sizeof(vc)); | ||
| 1784 | if (n == 0) { *rv = -1; *ver = 0; *dlen = 0; *slen = 0; return; } | ||
| 1785 | *rv = ngtcp2_pkt_decode_version_cid(&vc, p, n, HM_QUIC_CIDLEN); | ||
| 1786 | *ver = vc.version; | ||
| 1787 | *dlen = vc.dcidlen; | ||
| 1788 | *slen = vc.scidlen; | ||
| 1789 | } | ||
| 1790 | |||
| 1791 | static void hm_quic_readable(pTHX_ hm_loop *loop, hm_listener *lst) { | ||
| 1792 | static unsigned char buf[HM_QUIC_MAX_UDP]; | ||
| 1793 | int batch = 0; | ||
| 1794 | if (loop->stopping) return; /* # uncoverable branch true */ | ||
| 1795 | for (;;) { | ||
| 1796 | struct sockaddr_storage ss; | ||
| 1797 | socklen_t slen = sizeof(ss); | ||
| 1798 | ssize_t n = hm_os_recvfrom(lst->udp_fd, buf, sizeof(buf), | ||
| 1799 | (struct sockaddr *)&ss, &slen); | ||
| 1800 | if (n < 0) break; /* EAGAIN: drained */ | ||
| 1801 | if (n == 0) continue; /* an empty datagram is not QUIC */ /* # uncoverable branch true */ | ||
| 1802 | hm_quic_packet(aTHX_ loop, lst, buf, (size_t)n, | ||
| 1803 | (struct sockaddr *)&ss, slen); | ||
| 1804 | if (++batch >= 64) break; | ||
| 1805 | } | ||
| 1806 | } | ||
| 1807 | |||
| 1808 | /* The runtime QUIC stack banner, the counterpart to hm_tls_library. Both | ||
| 1809 | * library versions, because a QUIC problem is as often nghttp3's as | ||
| 1810 | * ngtcp2's and a bug report naming one of them is half a report. Built once | ||
| 1811 | * and cached: it is a diagnostic, not a hot path. */ | ||
| 1812 | static const char *hm_quic_library(void) { | ||
| 1813 | static char buf[96]; | ||
| 1814 | if (!buf[0]) { | ||
| 1815 | const ngtcp2_info *q = ngtcp2_version(0); | ||
| 1816 | const nghttp3_info *h = nghttp3_version(0); | ||
| 1817 | snprintf(buf, sizeof(buf), "ngtcp2 %s, nghttp3 %s", | ||
| 1818 | (q && q->version_str) ? q->version_str : "?", | ||
| 1819 | (h && h->version_str) ? h->version_str : "?"); | ||
| 1820 | } | ||
| 1821 | return buf; | ||
| 1822 | } | ||
| 1823 | |||
| 1824 | #else /* !HM_HAVE_HTTP3 */ | ||
| 1825 | |||
| 1826 | /* | ||
| 1827 | # uncoverable statement | ||
| 1828 | # uncoverable subroutine */ | ||
| 1829 | 0 | static int hm_quic_available(void) { return 0; } | |
| 1830 | 1 | static const char *hm_quic_library(void) { return 0; } | |
| 1831 | /* | ||
| 1832 | # uncoverable statement | ||
| 1833 | # uncoverable subroutine */ | ||
| 1834 | 0 | static int hm_quic_cid_selftest(void) { return 0; } | |
| 1835 | /* | ||
| 1836 | # uncoverable statement | ||
| 1837 | # uncoverable subroutine */ | ||
| 1838 | 0 | static int hm_quic_heap_selftest(void) { return 0; } | |
| 1839 | /* | ||
| 1840 | # uncoverable statement | ||
| 1841 | # uncoverable subroutine */ | ||
| 1842 | 0 | static int hm_quic_src_selftest(void) { return 0; } | |
| 1843 | /* | ||
| 1844 | # uncoverable statement | ||
| 1845 | # uncoverable subroutine */ | ||
| 1846 | 0 | static int hm_quic_token_selftest(void) { return 0; } | |
| 1847 | /* | ||
| 1848 | # uncoverable statement | ||
| 1849 | # uncoverable subroutine */ | ||
| 1850 | 0 | static void hm_quic_expire(pTHX_ hm_loop *loop) { | |
| 1851 | PERL_UNUSED_CONTEXT; (void)loop; | ||
| 1852 | 0 | } | |
| 1853 | /* | ||
| 1854 | # uncoverable statement | ||
| 1855 | # uncoverable subroutine */ | ||
| 1856 | 0 | static void hm_quic_srv_attach(pTHX_ hm_loop *loop, hm_listener *lst, int widx) { | |
| 1857 | PERL_UNUSED_CONTEXT; (void)loop; (void)lst; (void)widx; | ||
| 1858 | 0 | } | |
| 1859 | 145 | static void hm_quic_srv_free(pTHX_ hm_listener *lst) { | |
| 1860 | PERL_UNUSED_CONTEXT; (void)lst; | ||
| 1861 | 145 | } | |
| 1862 | 134 | static void hm_quic_shutdown(pTHX_ hm_loop *loop) { | |
| 1863 | PERL_UNUSED_CONTEXT; (void)loop; | ||
| 1864 | 134 | } | |
| 1865 | /* | ||
| 1866 | # uncoverable statement | ||
| 1867 | # uncoverable subroutine */ | ||
| 1868 | 0 | static void hm_quic_ctx_swap(pTHX_ hm_listener *lst, void *fresh) { | |
| 1869 | PERL_UNUSED_CONTEXT; (void)lst; (void)fresh; | ||
| 1870 | 0 | } | |
| 1871 | /* | ||
| 1872 | # uncoverable statement | ||
| 1873 | # uncoverable subroutine */ | ||
| 1874 | 0 | static uint64_t hm_quic_next_deadline(hm_loop *loop) { | |
| 1875 | 0 | (void)loop; return (uint64_t)-1; /* # uncoverable statement */ | |
| 1876 | } | ||
| 1877 | 96 | static void hm_quic_secret_init(void) { } | |
| 1878 | /* | ||
| 1879 | # uncoverable statement | ||
| 1880 | # uncoverable subroutine */ | ||
| 1881 | 0 | static void hm_quic_decode_probe(const unsigned char *p, size_t n, | |
| 1882 | int *rv, uint32_t *ver, | ||
| 1883 | size_t *dlen, size_t *slen) { | ||
| 1884 | (void)p; (void)n; | ||
| 1885 | 0 | *rv = -1; *ver = 0; *dlen = 0; *slen = 0; /* # uncoverable statement */ | |
| 1886 | 0 | } | |
| 1887 | |||
| 1888 | /* Never reached - a listener only gets a udp_fd when http3 is on, and that | ||
| 1889 | * is refused at boot on this build - but it has to compile, because the | ||
| 1890 | * routing in hm_dispatch is not itself conditional. */ | ||
| 1891 | /* | ||
| 1892 | # uncoverable statement | ||
| 1893 | # uncoverable subroutine */ | ||
| 1894 | 0 | static void hm_quic_readable(pTHX_ hm_loop *loop, hm_listener *lst) { | |
| 1895 | PERL_UNUSED_CONTEXT; | ||
| 1896 | (void)loop; (void)lst; | ||
| 1897 | 0 | } | |
| 1898 | |||
| 1899 | #endif /* HM_HAVE_HTTP3 */ | ||
| 1900 | |||
| 1901 | #endif /* HM_QUIC_H */ |