@amritk/nish-aarch64-darwin 0.34.0 → 0.35.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/std/net/quic.ts CHANGED
@@ -97,15 +97,18 @@
97
97
  *
98
98
  * **Time.** The connection has no clock: `receive`, `takeDatagram` and
99
99
  * `handleTimer` take the caller's monotonic time in milliseconds, and
100
- * `deadline()` says when `handleTimer` is next due. Four things run on it.
100
+ * `deadline()` says when `handleTimer` is next due. Five things run on it.
101
101
  * The idle timeout (RFC 9000 §10.1) is the smaller of the two sides'
102
102
  * `max_idle_timeout`, never under `QUIC_CONN_IDLE_FLOOR` or three probe
103
103
  * timeouts; once it passes with nothing received, the connection closes
104
104
  * silently (`QUIC_STATE_TIMED_OUT`) and wipes its keys. Loss recovery's
105
105
  * timer declares packets lost by the time threshold or fires the probe
106
- * timeout. And after a key update the previous read keys are kept for a
106
+ * timeout. After a key update the previous read keys are kept for a
107
107
  * probe timeout (`recovery.probeTimeout()`), for packets the network
108
- * reordered, before the next generation's are derived (RFC 9001 §6.5). Once
108
+ * reordered, before the next generation's are derived (RFC 9001 §6.5). And
109
+ * while a new path is validated, its PATH_CHALLENGE goes again a probe
110
+ * timeout after the last, and validation gives up max(3 PTO, 6 kInitialRtt)
111
+ * after the move (RFC 9000 §8.2.4, §9.3.2). Once
109
112
  * the connection has closed, by either side, its keys are kept through the
110
113
  * closing or draining period, three probe timeouts (RFC 9000 §10.2), and
111
114
  * wiped at its end; `deadline()` is that end, then -1. A close this side
@@ -138,6 +141,32 @@
138
141
  * token from the configuration's static key (RFC 9000 §10.3.2), and every
139
142
  * ACK says a delay of zero. So a recorded exchange replays byte for byte.
140
143
  *
144
+ * **Paths** (RFC 9000 §8.2, §9). `receiveWindowFrom` takes the 18-byte
145
+ * `nish:net` address each datagram came from, and `sendAddress()` says where
146
+ * the connection's datagrams go: the first Initial's address, validated by
147
+ * the handshake, until the client's address changes. Before the handshake
148
+ * is confirmed a datagram from any other address is dropped unread, so no
149
+ * forged source moves a handshake or takes its flight. After it, a datagram
150
+ * from a new address whose newest non-probing packet authenticates moves
151
+ * the connection there (§9.3) — a later one from the old address, with a
152
+ * lower packet number, does not move it back — and a PATH_CHALLENGE, eight
153
+ * bytes from the ID seed's HMAC, padded to 1,200 bytes, validates the new
154
+ * path. Until a PATH_RESPONSE with any of the last three challenges'
155
+ * data arrives, on any path, the server sends there at most three times
156
+ * what came from it (§9.3.1); a lost challenge is drawn again, fresh, a
157
+ * probe timeout later; past max(3 PTO, 6 kInitialRtt) validation has failed
158
+ * and the connection goes back to the last validated address, where the
159
+ * client — or, for a forged source, the real client's next packet — finds
160
+ * it. Validated, a new address starts congestion control and the RTT
161
+ * estimate again, a port-only change keeps them (§9.4), and ECN is tested
162
+ * again (§13.4.2). A packet that carries only probing frames moves
163
+ * nothing: its PATH_CHALLENGE is answered on its own path, alone in a
164
+ * datagram `takeProbeInto` writes for `probeAddress`, under that path's own
165
+ * 3x limit. The client's connection ID is kept across a move (§9.5): a NAT
166
+ * rebinding gives the client no reason to change it, and this server never
167
+ * migrates itself. Every address, challenge and counter is a field made by
168
+ * the constructor and cleared by `reset`.
169
+ *
141
170
  * **What a peer cannot do.** Nothing it sends makes this module panic. A
142
171
  * datagram that does not parse, a packet that does not authenticate, a
143
172
  * duplicate packet number, a packet for keys already discarded and a 1-RTT
@@ -231,6 +260,7 @@ import {
231
260
  QUIC_FRAME_MAX_STREAMS_UNI,
232
261
  QUIC_FRAME_NEW_CONNECTION_ID,
233
262
  QUIC_FRAME_NEW_TOKEN,
263
+ QUIC_FRAME_PADDING,
234
264
  QUIC_FRAME_PATH_CHALLENGE,
235
265
  QUIC_FRAME_PATH_RESPONSE,
236
266
  QUIC_FRAME_PING,
@@ -406,6 +436,24 @@ export const QUIC_CONN_SKIP_SPREAD: i64 = 897
406
436
  * both without either telling anything about the other.
407
437
  */
408
438
  const QUIC_CONN_SKIP_SEQUENCE: i64 = QUIC_MAX_VARINT + 1
439
+ /**
440
+ * The HMAC counter PATH_CHALLENGE data is drawn under: past every connection
441
+ * ID's sequence and every skip's (`QUIC_CONN_SKIP_SEQUENCE` plus fewer than
442
+ * 2^61 draws), so no challenge repeats an ID or a skip distance.
443
+ */
444
+ const QUIC_CONN_CHALLENGE_SEQUENCE: i64 = QUIC_CONN_SKIP_SEQUENCE + QUIC_CONN_SKIP_SEQUENCE / 2
445
+ /** A `nish:net` address: sixteen bytes of IPv6 (IPv4 mapped), then the port, big-endian. */
446
+ export const QUIC_CONN_ADDRESS_SIZE: i32 = 18
447
+ /** PATH_CHALLENGE data kept to match a PATH_RESPONSE against: the latest and the two sent before it. */
448
+ const QUIC_CONN_CHALLENGES: i32 = 3
449
+ /**
450
+ * The smallest datagram sent on a path being validated: below it, what the
451
+ * path's 3x limit leaves is not worth a packet, and the server waits for the
452
+ * client to send more (RFC 9000 §8.1, §9.3.1).
453
+ */
454
+ const QUIC_CONN_PATH_MIN: i64 = 64
455
+ /** RFC 9000 §8.2.4's floor on path validation: six times the initial RTT, in milliseconds. */
456
+ const QUIC_CONN_VALIDATION_FLOOR: i64 = 1998
409
457
  /**
410
458
  * How many ack-eliciting datagrams a probe timeout sends in the Initial and
411
459
  * Handshake spaces (RFC 9002 §6.2.4 allows up to two). The server's first
@@ -1080,6 +1128,44 @@ const quicConnRefill = (to: u8[], from: u8[], at: i32, length: i32): void => {
1080
1128
  }
1081
1129
  }
1082
1130
 
1131
+ /** Copies `length` bytes from `from[fromAt]` to `to[toAt]`, as far as both arrays reach. */
1132
+ const quicConnCopyAt = (to: u8[], toAt: i32, from: u8[], fromAt: i32, length: i32): void => {
1133
+ for (let k: i32 = 0; k < length; k += 1) {
1134
+ const i: i32 = toAt + k
1135
+ const j: i32 = fromAt + k
1136
+ if (i >= 0 && i < toI32(to.length) && j >= 0 && j < toI32(from.length)) {
1137
+ to[i] = from[j]
1138
+ }
1139
+ }
1140
+ }
1141
+
1142
+ /** Sets every byte of `bytes` to 0. */
1143
+ const quicConnZeroAt = (bytes: u8[]): void => {
1144
+ for (let k: i32 = 0; k < toI32(bytes.length); k += 1) {
1145
+ bytes[k] = 0
1146
+ }
1147
+ }
1148
+
1149
+ /** Whether `a` and `b` hold the same `nish:net` address; `portOnly` compares the sixteen address bytes alone. */
1150
+ const quicConnSameAddress = (a: u8[], b: u8[], portOnly: boolean): boolean => {
1151
+ const n: i32 = portOnly ? QUIC_CONN_ADDRESS_SIZE - 2 : QUIC_CONN_ADDRESS_SIZE
1152
+ if (toI32(a.length) < n || toI32(b.length) < n) {
1153
+ return false
1154
+ }
1155
+ let same: boolean = true
1156
+ for (let k: i32 = 0; k < n && k < toI32(a.length) && k < toI32(b.length); k += 1) {
1157
+ same = same && a[k] === b[k]
1158
+ }
1159
+ return same
1160
+ }
1161
+
1162
+ /** Whether a frame of `type` is a probing frame (RFC 9000 §9.1), which a packet may carry without moving the connection. */
1163
+ const quicConnProbing = (type: i32): boolean =>
1164
+ type === QUIC_FRAME_PATH_CHALLENGE ||
1165
+ type === QUIC_FRAME_PATH_RESPONSE ||
1166
+ type === QUIC_FRAME_NEW_CONNECTION_ID ||
1167
+ type === QUIC_FRAME_PADDING
1168
+
1083
1169
  /** Zeroes the bytes a `Secret` field holds, for `exposeWith` to run outside the connection's `wipe()`; answers `answer`. */
1084
1170
  const quicConnZeroHeld = (held: u8[], answer: i32): i32 => {
1085
1171
  wipe(held)
@@ -1202,6 +1288,42 @@ export class QuicConnection {
1202
1288
  cryptoIn: QuicConnReassembly
1203
1289
  /** PATH_CHALLENGE data to answer: a ring of four, eight bytes each, `pathCount` from `pathHead`. */
1204
1290
  pathData: u8[]
1291
+ /**
1292
+ * The client's address (RFC 9000 §9), each in `nish:net`'s 18-byte form,
1293
+ * for a carrier that passes one to `receiveWindowFrom`: `pathCurrent` is
1294
+ * where this connection's datagrams go now (`sendAddress`), and
1295
+ * `pathValidated` the last address the client proved it receives at — the
1296
+ * handshake's, or one a PATH_RESPONSE answered. `pathArrival` is the
1297
+ * datagram being read's. Made once and overwritten in place.
1298
+ */
1299
+ pathCurrent: u8[]
1300
+ pathValidated: u8[]
1301
+ pathArrival: u8[]
1302
+ /**
1303
+ * A PATH_CHALLENGE that came from another address in a datagram that
1304
+ * moved nothing (§9.1): answered there by `takeProbeInto`, from
1305
+ * `probeData`, within `probeBudget` bytes, three times what came from
1306
+ * `probeAddress` (§8.1). Only the latest such challenge is kept. A
1307
+ * response owed on `pathCurrent` when the connection moves goes back
1308
+ * the same way (`sendResponsesBack`).
1309
+ */
1310
+ probeAddress: u8[]
1311
+ probeData: u8[]
1312
+ /**
1313
+ * The last `QUIC_CONN_CHALLENGES` PATH_CHALLENGE data sent on the path
1314
+ * being validated, eight bytes each, `challengeCount` of them, the latest
1315
+ * at `(challengesDrawn - 1) % QUIC_CONN_CHALLENGES`: each drawn from the
1316
+ * ID seed's HMAC under `QUIC_CONN_CHALLENGE_SEQUENCE`, so unguessable.
1317
+ */
1318
+ challengeData: u8[]
1319
+ /** Bytes received from and sent to the path being validated, for its own 3x limit (§9.3, §8.1). */
1320
+ pathReceived: i64 = 0
1321
+ pathSent: i64 = 0
1322
+ probeBudget: i64 = 0
1323
+ /** When the PATH_CHALLENGE goes again with fresh data, and when validation gives up (§8.2.4); -1 when not validating. */
1324
+ challengeResendAt: i64 = -1
1325
+ challengeUntil: i64 = -1
1326
+ challengesDrawn: i64 = 0
1205
1327
  frame: QuicFrame
1206
1328
  /** The header and the packet numbers of the packet being read, reused for every one. */
1207
1329
  header: QuicHeader
@@ -1251,17 +1373,6 @@ export class QuicConnection {
1251
1373
  /** Packets sealed under the current 1-RTT write key, and packets that failed to authenticate, against those limits. */
1252
1374
  sealedUnderKey: i64 = 0
1253
1375
  authFailures: i64 = 0
1254
- /**
1255
- * ECN (RFC 9000 §13.4): `ecnMarking` is the carrier's to set when it sends
1256
- * this connection's datagrams with the ECN bits of `datagramEcn`, which
1257
- * each `takeDatagramInto` sets for the datagram it wrote: ECT(0) while
1258
- * loss recovery's `ecnCodepoint()` asks for it, 0 otherwise and always
1259
- * while `ecnMarking` is false. Every packet of a marked datagram is counted
1260
- * as marked, and an ACK_ECN's counts are validated against them. `reset`
1261
- * keeps `ecnMarking`, which is the carrier's, and clears the rest.
1262
- */
1263
- ecnMarking: boolean = false
1264
- datagramEcn: i32 = 0
1265
1376
  /**
1266
1377
  * The next Application Data packet number skipped (RFC 9000 §21.4), or -1
1267
1378
  * before the 1-RTT keys; a test may set it to force one. `skipped` holds
@@ -1271,6 +1382,8 @@ export class QuicConnection {
1271
1382
  nextSkip: i64 = -1
1272
1383
  skipped: i64[]
1273
1384
  skipsDrawn: i64 = 0
1385
+ /** How many of `challengeData`'s slots hold a challenge sent on the path being validated. */
1386
+ challengeCount: i32 = 0
1274
1387
  originalDcidLength: i32 = 0
1275
1388
  peerScidLength: i32 = 0
1276
1389
  pathHead: i32 = 0
@@ -1289,6 +1402,17 @@ export class QuicConnection {
1289
1402
  earlyResends: i32 = 0
1290
1403
  /** The level at which the packet just read showed the client lacks the server's handshake data, or -1. */
1291
1404
  resendLevel: i32 = -1
1405
+ /**
1406
+ * ECN (RFC 9000 §13.4): `ecnMarking` is the carrier's to set when it sends
1407
+ * this connection's datagrams with the ECN bits of `datagramEcn`, which
1408
+ * each `takeDatagramInto` sets for the datagram it wrote: ECT(0) while
1409
+ * loss recovery's `ecnCodepoint()` asks for it, 0 otherwise and always
1410
+ * while `ecnMarking` is false. Every packet of a marked datagram is counted
1411
+ * as marked, and an ACK_ECN's counts are validated against them. `reset`
1412
+ * keeps `ecnMarking`, which is the carrier's, and clears the rest.
1413
+ */
1414
+ datagramEcn: i32 = 0
1415
+ ecnMarking: boolean = false
1292
1416
  /** Whether `error` is an application code (CONNECTION_CLOSE 0x1d) rather than a transport error. */
1293
1417
  errorIsApplication: boolean = false
1294
1418
  /** Whether the one CONNECTION_CLOSE this side owes has gone out. */
@@ -1316,6 +1440,21 @@ export class QuicConnection {
1316
1440
  /** Whether the packet just read brought CRYPTO bytes for TLS, or retired an ID that wants replacing. */
1317
1441
  cryptoArrived: boolean = false
1318
1442
  topUpOwed: boolean = false
1443
+ /** Whether `pathCurrent` holds the client's address: the carrier passes addresses, and a first Initial gave one. */
1444
+ pathKnown: boolean = false
1445
+ /** Whether the datagram being read came from an address other than `pathCurrent`. */
1446
+ arrivalOther: boolean = false
1447
+ /** Whether `pathCurrent` is not validated yet: a PATH_CHALLENGE is out on it. */
1448
+ pathValidating: boolean = false
1449
+ /** Whether a PATH_CHALLENGE is due in the next datagram, and whether the one being built carries a PATH_CHALLENGE or PATH_RESPONSE. */
1450
+ challengeOwed: boolean = false
1451
+ pathFrameStaged: boolean = false
1452
+ /** Whether a PATH_RESPONSE is owed on `probeAddress`, and whether the datagram being read carried a challenge for it. */
1453
+ probeOwed: boolean = false
1454
+ probeStaged: boolean = false
1455
+ /** Whether the packet being read carries a non-probing frame (§9.1), and whether a packet of the datagram moves the connection (§9.3). */
1456
+ nonProbing: boolean = false
1457
+ migrateOwed: boolean = false
1319
1458
 
1320
1459
  /**
1321
1460
  * A connection under `config`, with `entropy` its `QUIC_CONN_ENTROPY_SIZE`
@@ -1362,6 +1501,12 @@ export class QuicConnection {
1362
1501
  )
1363
1502
  this.cryptoIn = new QuicConnReassembly(QUIC_CONN_CRYPTO_WINDOW)
1364
1503
  this.pathData = new Array<u8>(QUIC_PATH_DATA_SIZE * 4)
1504
+ this.pathCurrent = new Array<u8>(QUIC_CONN_ADDRESS_SIZE)
1505
+ this.pathValidated = new Array<u8>(QUIC_CONN_ADDRESS_SIZE)
1506
+ this.pathArrival = new Array<u8>(QUIC_CONN_ADDRESS_SIZE)
1507
+ this.probeAddress = new Array<u8>(QUIC_CONN_ADDRESS_SIZE)
1508
+ this.probeData = new Array<u8>(QUIC_PATH_DATA_SIZE)
1509
+ this.challengeData = new Array<u8>(QUIC_PATH_DATA_SIZE * QUIC_CONN_CHALLENGES)
1365
1510
  this.frame = new QuicFrame()
1366
1511
  this.header = new QuicHeader()
1367
1512
  this.packet = new QuicPacket()
@@ -1445,6 +1590,7 @@ export class QuicConnection {
1445
1590
  this.peerScidLength = 0
1446
1591
  this.pathHead = 0
1447
1592
  this.pathCount = 0
1593
+ this.forgetPath()
1448
1594
  this.state = QUIC_STATE_WAIT_INITIAL
1449
1595
  this.dropped = 0
1450
1596
  this.keyUpdates = 0
@@ -1631,6 +1777,16 @@ export class QuicConnection {
1631
1777
  * datagram is ignored.
1632
1778
  */
1633
1779
  receiveWindow(buf: u8[], off: i32, len: i32, now: i64): i64 {
1780
+ return this.receiveWindowFrom(buf, off, len, this.none, now)
1781
+ }
1782
+
1783
+ /**
1784
+ * `receiveWindow` of a datagram that came from `address`, an 18-byte
1785
+ * `nish:net` address: the connection follows the client from one address
1786
+ * to another (RFC 9000 §9) — see "Paths" in the module header. An
1787
+ * address of any other length is no address, as `receiveWindow` gives.
1788
+ */
1789
+ receiveWindowFrom(buf: u8[], off: i32, len: i32, address: u8[], now: i64): i64 {
1634
1790
  this.runClocks(now)
1635
1791
  if (this.closed()) {
1636
1792
  return this.error
@@ -1644,6 +1800,10 @@ export class QuicConnection {
1644
1800
  this.drop()
1645
1801
  return 0
1646
1802
  }
1803
+ if (!this.notePath(address)) {
1804
+ this.drop()
1805
+ return 0
1806
+ }
1647
1807
  const end: i32 = off + len
1648
1808
  let at: i32 = off
1649
1809
  let counted: boolean = false
@@ -1665,6 +1825,11 @@ export class QuicConnection {
1665
1825
  if (counted) {
1666
1826
  this.bytesReceived = this.bytesReceived + toI64(len)
1667
1827
  }
1828
+ if (!this.closed()) {
1829
+ this.afterPath(counted, len)
1830
+ }
1831
+ // A challenge staged from another address belongs to this datagram only.
1832
+ this.probeStaged = false
1668
1833
  // Loss recovery's timer runs after the datagram, so an acknowledgement
1669
1834
  // it carries settles what it acknowledges before a probe is owed.
1670
1835
  this.runRecoveryTimer()
@@ -1783,8 +1948,9 @@ export class QuicConnection {
1783
1948
  params.initialMaxStreamDataUni = this.config.maxStreamsUni > 0 ? this.config.maxStreamData : 0
1784
1949
  params.maxDatagramFrameSize = this.config.maxDatagramFrameSize
1785
1950
  params.activeConnectionIdLimit = this.config.activeConnectionIdLimit
1786
- // The server never migrates and asks the client not to (§9): path
1787
- // validation beyond answering PATH_CHALLENGE is not here.
1951
+ // The server never migrates and asks the client not to (§9). A client
1952
+ // whose address changes all the same, as a NAT rebinding changes it, is
1953
+ // followed with path validation (`migrate`), as §9 allows.
1788
1954
  params.disableActiveMigration = true
1789
1955
  quicEncodeTransportParametersInto(params, this.localEncoded)
1790
1956
  // One `TlsServer` a slot, made by its first handshake (so its flight is
@@ -1837,6 +2003,7 @@ export class QuicConnection {
1837
2003
  this.initial.reset()
1838
2004
  this.originalDcidLength = 0
1839
2005
  this.peerScidLength = 0
2006
+ this.pathKnown = false
1840
2007
  }
1841
2008
 
1842
2009
  /**
@@ -1957,10 +2124,17 @@ export class QuicConnection {
1957
2124
  if (type === QUIC_PACKET_SHORT && !this.notePhase(pn, other)) {
1958
2125
  return true
1959
2126
  }
2127
+ const largest: i64 = space.received.largest
2128
+ this.nonProbing = false
1960
2129
  const eliciting: boolean = this.receiveFrames(space, type, payload, buf, header)
1961
2130
  if (this.closed()) {
1962
2131
  return true
1963
2132
  }
2133
+ // RFC 9000 §9.3: the client's newest non-probing packet, from another
2134
+ // address, moves the connection there; an older one, or a probe, does not.
2135
+ if (type === QUIC_PACKET_SHORT && this.arrivalOther && this.nonProbing && pn > largest) {
2136
+ this.migrateOwed = true
2137
+ }
1964
2138
  // RFC 9002 §6.2.3: a client that sends an ack-eliciting Handshake packet
1965
2139
  // while the server's Handshake data is in flight may not have it all.
1966
2140
  if (eliciting && type === QUIC_PACKET_HANDSHAKE) {
@@ -2202,6 +2376,7 @@ export class QuicConnection {
2202
2376
  return eliciting
2203
2377
  }
2204
2378
  eliciting = eliciting || quicFrameAckEliciting(frame.type)
2379
+ this.nonProbing = this.nonProbing || !quicConnProbing(frame.type)
2205
2380
  this.receiveFrame(space, frame, payload, at, buf, header)
2206
2381
  if (this.closed() || frame.end <= at) {
2207
2382
  return eliciting
@@ -2302,12 +2477,17 @@ export class QuicConnection {
2302
2477
  break
2303
2478
  }
2304
2479
  case QUIC_FRAME_PATH_CHALLENGE:
2305
- this.receivePathChallenge(frame, payload)
2480
+ if (this.arrivalOther) {
2481
+ this.stageProbe(frame, payload)
2482
+ } else {
2483
+ this.receivePathChallenge(frame, payload)
2484
+ }
2306
2485
  break
2307
- // A PATH_RESPONSE answers no PATH_CHALLENGE, since the server sends none
2308
- // (§19.18); NEW_TOKEN and HANDSHAKE_DONE go only to a client (§19.7,
2309
- // §19.20). Each is a PROTOCOL_VIOLATION here.
2310
2486
  case QUIC_FRAME_PATH_RESPONSE:
2487
+ this.receivePathResponse(frame, payload)
2488
+ break
2489
+ // NEW_TOKEN and HANDSHAKE_DONE go only to a client (§19.7, §19.20): a
2490
+ // PROTOCOL_VIOLATION here.
2311
2491
  case QUIC_FRAME_NEW_TOKEN:
2312
2492
  case QUIC_FRAME_HANDSHAKE_DONE:
2313
2493
  this.fail(QUIC_ERROR_PROTOCOL_VIOLATION, type)
@@ -2631,29 +2811,77 @@ export class QuicConnection {
2631
2811
  }
2632
2812
 
2633
2813
  /**
2634
- * A PATH_CHALLENGE (§8.2.2): answered with the same eight bytes. Only the
2635
- * latest four are kept, so a burst of challenges cannot grow anything.
2814
+ * A PATH_CHALLENGE (§8.2.2) on the current path: answered with the same
2815
+ * eight bytes. Only the latest four are kept, so a burst of challenges
2816
+ * cannot grow anything.
2636
2817
  */
2637
2818
  receivePathChallenge(frame: QuicFrame, payload: u8[]): void {
2819
+ this.queuePathResponse(payload, frame.dataStart)
2820
+ }
2821
+
2822
+ /** Queues a PATH_RESPONSE of the eight bytes at `data[from]` on the current path. */
2823
+ queuePathResponse(data: u8[], from: i32): void {
2638
2824
  if (this.pathCount >= 4) {
2639
2825
  this.pathHead = 0
2640
2826
  this.pathCount = 0
2641
2827
  }
2642
2828
  const base: i32 = ((this.pathHead + this.pathCount) % 4) * QUIC_PATH_DATA_SIZE
2643
2829
  for (let k: i32 = 0; k < QUIC_PATH_DATA_SIZE; k += 1) {
2644
- const from: i32 = frame.dataStart + k
2645
- if (
2646
- base + k >= 0 &&
2647
- base + k < toI32(this.pathData.length) &&
2648
- from >= 0 &&
2649
- from < toI32(payload.length)
2650
- ) {
2651
- this.pathData[base + k] = payload[from]
2830
+ const at: i32 = from + k
2831
+ if (base + k >= 0 && base + k < toI32(this.pathData.length) && at >= 0 && at < toI32(data.length)) {
2832
+ this.pathData[base + k] = data[at]
2652
2833
  }
2653
2834
  }
2654
2835
  this.pathCount = this.pathCount + 1
2655
2836
  }
2656
2837
 
2838
+ /**
2839
+ * A PATH_CHALLENGE from another address: kept for `afterPath`, which
2840
+ * answers it on the current path when the datagram moved the connection
2841
+ * there, and on its own path (`takeProbeInto`) otherwise (§8.2.2, §9.1).
2842
+ * Only the latest such challenge is kept, so a response still owed on
2843
+ * `probeAddress` is dropped, as a response may be: its data is about to
2844
+ * be overwritten.
2845
+ */
2846
+ stageProbe(frame: QuicFrame, payload: u8[]): void {
2847
+ quicConnCopyAt(this.probeData, 0, payload, frame.dataStart, QUIC_PATH_DATA_SIZE)
2848
+ this.probeOwed = false
2849
+ this.probeStaged = true
2850
+ }
2851
+
2852
+ /**
2853
+ * A PATH_RESPONSE (§8.2.3): one matching a PATH_CHALLENGE still out
2854
+ * validates the path, whichever path it came on. Once the server has sent
2855
+ * a challenge, one matching none — late, or for a path given up — is
2856
+ * ignored; before that, none can be expected, and it is a
2857
+ * PROTOCOL_VIOLATION (§19.18).
2858
+ */
2859
+ receivePathResponse(frame: QuicFrame, payload: u8[]): void {
2860
+ if (this.challengesDrawn === 0) {
2861
+ this.fail(QUIC_ERROR_PROTOCOL_VIOLATION, toI64(QUIC_FRAME_PATH_RESPONSE))
2862
+ return
2863
+ }
2864
+ if (!this.pathValidating) {
2865
+ return
2866
+ }
2867
+ let matched: boolean = false
2868
+ for (let m: i32 = 0; m < this.challengeCount; m += 1) {
2869
+ const slot: i32 = toI32((this.challengesDrawn - 1 - toI64(m)) % toI64(QUIC_CONN_CHALLENGES))
2870
+ // Not secret once sent, but compared without branching on the bytes all the same.
2871
+ matched =
2872
+ timingSafeEqualAt(
2873
+ payload,
2874
+ frame.dataStart,
2875
+ this.challengeData,
2876
+ slot * QUIC_PATH_DATA_SIZE,
2877
+ QUIC_PATH_DATA_SIZE
2878
+ ) || matched
2879
+ }
2880
+ if (matched) {
2881
+ this.pathConfirmed()
2882
+ }
2883
+ }
2884
+
2657
2885
  /**
2658
2886
  * Whatever TLS's last step made due: its output moved into each level's
2659
2887
  * CRYPTO stream, the keys of each level it now has secrets for, the
@@ -2941,12 +3169,14 @@ export class QuicConnection {
2941
3169
  /**
2942
3170
  * When `handleTimer` is next due, in the caller's milliseconds: the idle
2943
3171
  * timeout, loss recovery's timer (a time-threshold loss or the probe
2944
- * timeout), or the end of a key update's probe timeout, whichever comes
2945
- * first; once the connection has closed, the end of its closing or
3172
+ * timeout), the end of a key update's probe timeout, or, while a path is
3173
+ * validated, its PATH_CHALLENGE's resend or the end of its validation,
3174
+ * whichever comes first; once the connection has closed, the end of its closing or
2946
3175
  * draining period, when its keys are wiped. -1 when nothing is timed:
2947
3176
  * before the first packet, while a closed connection's own close has not
2948
3177
  * gone out (`takeDatagram` sends it), once a closed connection's keys are
2949
- * wiped, and with no idle timeout, nothing in flight and no update pending.
3178
+ * wiped, and with no idle timeout, nothing in flight, no update pending
3179
+ * and no path being validated.
2950
3180
  */
2951
3181
  deadline(): i64 {
2952
3182
  if (this.closed()) {
@@ -2963,6 +3193,13 @@ export class QuicConnection {
2963
3193
  if (recovery >= 0 && (due < 0 || recovery < due)) {
2964
3194
  due = recovery
2965
3195
  }
3196
+ if (this.pathValidating) {
3197
+ const path: i64 =
3198
+ this.challengeResendAt < this.challengeUntil ? this.challengeResendAt : this.challengeUntil
3199
+ if (due < 0 || path < due) {
3200
+ due = path
3201
+ }
3202
+ }
2966
3203
  return due
2967
3204
  }
2968
3205
 
@@ -2972,7 +3209,19 @@ export class QuicConnection {
2972
3209
  * sent leaves no room for a whole datagram.
2973
3210
  */
2974
3211
  amplificationBlocked(): boolean {
2975
- return !this.addressValidated && this.bytesReceived * 3 - this.bytesSent < toI64(QUIC_CONN_DATAGRAM_SIZE)
3212
+ return (
3213
+ (!this.addressValidated && this.bytesReceived * 3 - this.bytesSent < toI64(QUIC_CONN_DATAGRAM_SIZE)) ||
3214
+ this.pathBudget() < QUIC_CONN_PATH_MIN
3215
+ )
3216
+ }
3217
+
3218
+ /**
3219
+ * What the path being validated still lets the server send: three times
3220
+ * what came from it since the connection moved there, less what went
3221
+ * (RFC 9000 §9.3.1, §8.1). Unlimited, as `QUIC_MAX_VARINT`, on a validated path.
3222
+ */
3223
+ pathBudget(): i64 {
3224
+ return this.pathValidating ? this.pathReceived * 3 - this.pathSent : QUIC_MAX_VARINT
2976
3225
  }
2977
3226
 
2978
3227
  /**
@@ -3022,11 +3271,309 @@ export class QuicConnection {
3022
3271
  this.wipeAll()
3023
3272
  return
3024
3273
  }
3274
+ this.runPathClock()
3025
3275
  if (this.keyRetainUntil >= 0 && this.now >= this.keyRetainUntil) {
3026
3276
  this.prepareNextReadKeys()
3027
3277
  }
3028
3278
  }
3029
3279
 
3280
+ /**
3281
+ * The path being validated's timers (RFC 9000 §8.2.4, §9.3.2): past
3282
+ * `challengeUntil`, max(3 PTO, 6 kInitialRtt) after the move, validation
3283
+ * has failed and the connection goes back to the last validated address;
3284
+ * past `challengeResendAt`, a probe timeout after the last challenge, a
3285
+ * fresh one is drawn and sent. A server's handshake validates the
3286
+ * client's address and the handshake confirms the connection before any
3287
+ * move, so there is always an address to go back to: the case where
3288
+ * there is none, which RFC 9000 §9.3.2 closes silently, cannot arise.
3289
+ */
3290
+ runPathClock(): void {
3291
+ if (!this.pathValidating) {
3292
+ return
3293
+ }
3294
+ if (this.now >= this.challengeUntil) {
3295
+ this.sendResponsesBack()
3296
+ quicConnCopyAt(this.pathCurrent, 0, this.pathValidated, 0, QUIC_CONN_ADDRESS_SIZE)
3297
+ this.endValidation()
3298
+ return
3299
+ }
3300
+ if (this.now >= this.challengeResendAt) {
3301
+ this.drawChallenge()
3302
+ }
3303
+ }
3304
+
3305
+ /**
3306
+ * Notes where the datagram about to be read came from, before any of it
3307
+ * is: answers false for one to drop unread. The first Initial's address
3308
+ * is the connection's, and validated by the handshake. Until the handshake
3309
+ * is confirmed the client may not move (RFC 9000 §9), and a datagram from
3310
+ * another address is dropped — so a forged Initial or Handshake packet
3311
+ * from elsewhere cannot move the connection, nor take the server's
3312
+ * flight. A carrier that passes no address leaves every path rule off.
3313
+ */
3314
+ notePath(address: u8[]): boolean {
3315
+ this.arrivalOther = false
3316
+ this.migrateOwed = false
3317
+ this.probeStaged = false
3318
+ if (toI32(address.length) !== QUIC_CONN_ADDRESS_SIZE) {
3319
+ return true
3320
+ }
3321
+ if (this.state === QUIC_STATE_WAIT_INITIAL) {
3322
+ quicConnCopyAt(this.pathCurrent, 0, address, 0, QUIC_CONN_ADDRESS_SIZE)
3323
+ quicConnCopyAt(this.pathValidated, 0, address, 0, QUIC_CONN_ADDRESS_SIZE)
3324
+ this.pathKnown = true
3325
+ return true
3326
+ }
3327
+ if (!this.pathKnown || quicConnSameAddress(address, this.pathCurrent, false)) {
3328
+ return true
3329
+ }
3330
+ if (!this.handshakeComplete) {
3331
+ return false
3332
+ }
3333
+ quicConnCopyAt(this.pathArrival, 0, address, 0, QUIC_CONN_ADDRESS_SIZE)
3334
+ this.arrivalOther = true
3335
+ return true
3336
+ }
3337
+
3338
+ /**
3339
+ * What a datagram of `len` bytes, now read, does to the paths: one from
3340
+ * another address whose newest non-probing packet moved the connection
3341
+ * moves it (§9.3), and a challenge it carried is answered on the new
3342
+ * path; one that moved nothing has its challenge answered on its own path
3343
+ * (`takeProbeInto`). A datagram that authenticated nothing does neither.
3344
+ * What came from the path being validated adds to its limit.
3345
+ */
3346
+ afterPath(counted: boolean, len: i32): void {
3347
+ if (!counted) {
3348
+ return
3349
+ }
3350
+ if (this.arrivalOther) {
3351
+ if (!this.migrateOwed) {
3352
+ if (this.probeStaged) {
3353
+ quicConnCopyAt(this.probeAddress, 0, this.pathArrival, 0, QUIC_CONN_ADDRESS_SIZE)
3354
+ this.probeBudget = toI64(len) * 3
3355
+ this.probeOwed = true
3356
+ }
3357
+ return
3358
+ }
3359
+ const swapped: boolean = this.migrate(this.pathArrival)
3360
+ if (this.probeStaged && !swapped) {
3361
+ this.queuePathResponse(this.probeData, toI32(0))
3362
+ }
3363
+ }
3364
+ if (this.pathValidating) {
3365
+ this.pathReceived = this.pathReceived + toI64(len)
3366
+ }
3367
+ }
3368
+
3369
+ /**
3370
+ * Moves the connection to `address` (RFC 9000 §9.3): its datagrams go
3371
+ * there from now. Back to the last validated address, nothing more is
3372
+ * needed, and a validation under way stops — which is how a forged
3373
+ * source, a copy of the client's packet sent on from elsewhere, loses the
3374
+ * connection to the client's next packet (§9.3.3). Anywhere else, the
3375
+ * path is validated with a PATH_CHALLENGE, and until it is the server
3376
+ * sends there only three times what it received from it (§9.3.1). The
3377
+ * peer's connection ID is kept: the client chose not to change it, and a
3378
+ * NAT rebinding never does (§9.5). Answers whether the challenge staged
3379
+ * in `probeData` is already queued on the new path (`sendResponsesBack`).
3380
+ */
3381
+ migrate(address: u8[]): boolean {
3382
+ const swapped: boolean = this.sendResponsesBack()
3383
+ quicConnCopyAt(this.pathCurrent, 0, address, 0, QUIC_CONN_ADDRESS_SIZE)
3384
+ if (quicConnSameAddress(address, this.pathValidated, false)) {
3385
+ this.endValidation()
3386
+ return swapped
3387
+ }
3388
+ this.endValidation()
3389
+ this.pathValidating = true
3390
+ const span: i64 = this.recovery.probeTimeout() * 3
3391
+ this.challengeUntil = this.now + (span > QUIC_CONN_VALIDATION_FLOOR ? span : QUIC_CONN_VALIDATION_FLOOR)
3392
+ this.drawChallenge()
3393
+ return swapped
3394
+ }
3395
+
3396
+ /**
3397
+ * Before `pathCurrent` changes: a PATH_RESPONSE still owed there answers
3398
+ * a challenge that came from there, so it goes there, not to the new
3399
+ * address (RFC 9000 §8.2.2) — the latest one, on the probe route
3400
+ * (`takeProbeInto`), within what that path may still be sent: no limit
3401
+ * once validated, else its own 3x (§8.1). The others are dropped, as a
3402
+ * response may be. A challenge staged in `probeData` by the datagram
3403
+ * being read swaps into the ring in its place, owed on the new address,
3404
+ * and the answer is true; otherwise false. A response still owed on the
3405
+ * probe route is never overwritten: the ring's are dropped instead.
3406
+ */
3407
+ sendResponsesBack(): boolean {
3408
+ if (this.pathCount <= 0) {
3409
+ return false
3410
+ }
3411
+ if (this.probeOwed) {
3412
+ this.pathCount = 0
3413
+ return false
3414
+ }
3415
+ const base: i32 = ((this.pathHead + this.pathCount - 1) % 4) * QUIC_PATH_DATA_SIZE
3416
+ const swapped: boolean = this.probeStaged
3417
+ for (let k: i32 = 0; k < QUIC_PATH_DATA_SIZE; k += 1) {
3418
+ const kept: u8 = this.probeData[k]
3419
+ this.probeData[k] = this.pathData[base + k]
3420
+ this.pathData[base + k] = kept
3421
+ }
3422
+ this.pathHead = base / QUIC_PATH_DATA_SIZE
3423
+ this.pathCount = swapped ? 1 : 0
3424
+ quicConnCopyAt(this.probeAddress, 0, this.pathCurrent, 0, QUIC_CONN_ADDRESS_SIZE)
3425
+ if (!this.pathValidating) {
3426
+ this.probeBudget = toI64(QUIC_CONN_DATAGRAM_SIZE)
3427
+ } else {
3428
+ const left: i64 = this.pathReceived * 3 - this.pathSent
3429
+ this.probeBudget = left > 0 ? left : 0
3430
+ }
3431
+ this.probeOwed = true
3432
+ return swapped
3433
+ }
3434
+
3435
+ /**
3436
+ * Draws the next PATH_CHALLENGE's eight bytes from the ID seed's HMAC
3437
+ * under `QUIC_CONN_CHALLENGE_SEQUENCE` (§8.2.1: unpredictable), keeps
3438
+ * them with the two before, and owes the challenge; its resend is due a
3439
+ * probe timeout later.
3440
+ */
3441
+ drawChallenge(): void {
3442
+ this.deriveConnectionId(QUIC_CONN_CHALLENGE_SEQUENCE + this.challengesDrawn)
3443
+ const slot: i32 = toI32(this.challengesDrawn % toI64(QUIC_CONN_CHALLENGES))
3444
+ quicConnCopyAt(this.challengeData, slot * QUIC_PATH_DATA_SIZE, this.cidScratch, 0, QUIC_PATH_DATA_SIZE)
3445
+ secureZero(this.cidScratch)
3446
+ this.challengesDrawn = this.challengesDrawn + 1
3447
+ if (this.challengeCount < QUIC_CONN_CHALLENGES) {
3448
+ this.challengeCount = this.challengeCount + 1
3449
+ }
3450
+ this.challengeOwed = true
3451
+ this.challengeResendAt = this.now + this.recovery.probeTimeout()
3452
+ }
3453
+
3454
+ /**
3455
+ * The path being validated answered (§8.2.3): it is the validated
3456
+ * address now. Congestion control and the RTT estimate start again on a
3457
+ * new address, but not when only the port changed, which is the same
3458
+ * network path behind a rebinding NAT (§9.4); ECN is tested again either
3459
+ * way (§13.4.2).
3460
+ */
3461
+ pathConfirmed(): void {
3462
+ const portOnly: boolean = quicConnSameAddress(this.pathCurrent, this.pathValidated, true)
3463
+ quicConnCopyAt(this.pathValidated, 0, this.pathCurrent, 0, QUIC_CONN_ADDRESS_SIZE)
3464
+ this.endValidation()
3465
+ if (!portOnly) {
3466
+ this.recovery.resetPath()
3467
+ }
3468
+ this.recovery.restartEcn()
3469
+ }
3470
+
3471
+ /** Stops validating a path: no challenge is owed, kept or timed. */
3472
+ endValidation(): void {
3473
+ this.pathValidating = false
3474
+ this.challengeOwed = false
3475
+ this.challengeCount = 0
3476
+ this.challengeResendAt = -1
3477
+ this.challengeUntil = -1
3478
+ this.pathReceived = 0
3479
+ this.pathSent = 0
3480
+ }
3481
+
3482
+ /** Adds `n` bytes sent to the path being validated's count. */
3483
+ notePathSent(n: i32): void {
3484
+ if (this.pathValidating) {
3485
+ this.pathSent = this.pathSent + toI64(n)
3486
+ }
3487
+ }
3488
+
3489
+ /** Clears every path field for a new connection, in the arrays the slot keeps. */
3490
+ forgetPath(): void {
3491
+ quicConnZeroAt(this.pathCurrent)
3492
+ quicConnZeroAt(this.pathValidated)
3493
+ quicConnZeroAt(this.pathArrival)
3494
+ quicConnZeroAt(this.probeAddress)
3495
+ quicConnZeroAt(this.probeData)
3496
+ quicConnZeroAt(this.challengeData)
3497
+ this.endValidation()
3498
+ this.probeBudget = 0
3499
+ this.challengesDrawn = 0
3500
+ this.pathKnown = false
3501
+ this.arrivalOther = false
3502
+ this.pathFrameStaged = false
3503
+ this.probeOwed = false
3504
+ this.probeStaged = false
3505
+ this.nonProbing = false
3506
+ this.migrateOwed = false
3507
+ }
3508
+
3509
+ /**
3510
+ * Where this connection's datagrams go now: the 18-byte `nish:net`
3511
+ * address `receiveWindowFrom` last moved it to, or the first Initial's.
3512
+ * A carrier sends every datagram of `takeDatagramInto` here; it is empty
3513
+ * (all zero) for a connection fed by `receiveWindow` alone.
3514
+ */
3515
+ sendAddress(): u8[] {
3516
+ return this.pathCurrent
3517
+ }
3518
+
3519
+ /**
3520
+ * Writes the PATH_RESPONSE owed on another path (§8.2.2, §9.1) into `out`
3521
+ * at `at`, which needs `QUIC_CONN_DATAGRAM_SIZE` bytes of room, and
3522
+ * answers its length, or 0 when none is owed: a 1-RTT packet with that
3523
+ * frame alone, padded to 1200 bytes or to three times what came from the
3524
+ * path (§8.1), whichever is less. The carrier sends it to
3525
+ * `probeAddress`, before `takeDatagramInto`'s datagrams. It is answered
3526
+ * once and never sent again (§13.3).
3527
+ */
3528
+ takeProbeInto(out: u8[], at: i32, now: i64): i32 {
3529
+ this.handleTimer(now)
3530
+ if (
3531
+ !this.probeOwed ||
3532
+ this.closed() ||
3533
+ !this.handshakeComplete ||
3534
+ at < 0 ||
3535
+ at > toI32(out.length) - QUIC_CONN_DATAGRAM_SIZE
3536
+ ) {
3537
+ return 0
3538
+ }
3539
+ this.probeOwed = false
3540
+ const size: i32 =
3541
+ this.probeBudget < toI64(QUIC_CONN_DATAGRAM_SIZE) ? toI32(this.probeBudget) : QUIC_CONN_DATAGRAM_SIZE
3542
+ this.beforeDatagram()
3543
+ this.datagramEcn = 0
3544
+ let n: i32 = 0
3545
+ {
3546
+ using _scope = arena()
3547
+ n = this.buildProbe(out, at, size)
3548
+ }
3549
+ this.probeBudget = this.probeBudget - toI64(n)
3550
+ return n
3551
+ }
3552
+
3553
+ /** `takeProbeInto`'s packet, of `size` bytes at `out[at]`, built and sealed; 0 when it does not fit. */
3554
+ buildProbe(out: u8[], at: i32, size: i32): i32 {
3555
+ const space: QuicConnSpace = this.application
3556
+ const overhead: i32 = this.overhead(space)
3557
+ if (overhead === 0 || size < overhead + 1 + QUIC_PATH_DATA_SIZE) {
3558
+ return 0
3559
+ }
3560
+ space.clearStaged()
3561
+ const payloadStart: i32 = at + overhead - QUIC_AEAD_TAG_SIZE
3562
+ const payloadEnd: i32 = at + size - QUIC_AEAD_TAG_SIZE
3563
+ const next: i32 = quicPutPathData(
3564
+ out,
3565
+ payloadStart,
3566
+ payloadEnd,
3567
+ QUIC_FRAME_PATH_RESPONSE,
3568
+ this.probeData,
3569
+ 0
3570
+ )
3571
+ if (next < 0) {
3572
+ return 0
3573
+ }
3574
+ return this.sealAt(space, out, at, quicPutPadding(out, next, payloadEnd - next))
3575
+ }
3576
+
3030
3577
  /** Loss recovery's timer, if it is due (RFC 9002 §6.2, A.9). */
3031
3578
  runRecoveryTimer(): void {
3032
3579
  if (this.closed() || this.state === QUIC_STATE_WAIT_INITIAL) {
@@ -3386,6 +3933,13 @@ export class QuicConnection {
3386
3933
  */
3387
3934
  buildDatagram(out: u8[], at: i32): i32 {
3388
3935
  let remaining: i32 = QUIC_CONN_DATAGRAM_SIZE
3936
+ // A path being validated has its own 3x limit (§9.3.1), which the
3937
+ // datagram may not pass; `amplificationBlocked` kept it above the floor.
3938
+ const budget: i64 = this.pathBudget()
3939
+ if (budget < toI64(remaining)) {
3940
+ remaining = toI32(budget)
3941
+ }
3942
+ this.pathFrameStaged = false
3389
3943
  let position: i32 = at
3390
3944
  let paddedInitial: boolean = false
3391
3945
  let eliciting: boolean = false
@@ -3415,7 +3969,8 @@ export class QuicConnection {
3415
3969
  out,
3416
3970
  payloadStart,
3417
3971
  payloadStart + remaining - overhead,
3418
- elicit
3972
+ elicit,
3973
+ !sent.full()
3419
3974
  )
3420
3975
  if (payloadEnd <= payloadStart) {
3421
3976
  continue
@@ -3440,7 +3995,10 @@ export class QuicConnection {
3440
3995
  if (last < 0) {
3441
3996
  return 0
3442
3997
  }
3443
- if (paddedInitial && remaining > 0) {
3998
+ // RFC 9000 §14.1: a datagram with an ack-eliciting Initial is padded;
3999
+ // §8.2.1, §8.2.2: so is one with a PATH_CHALLENGE or PATH_RESPONSE, as
4000
+ // far as the path's limit lets it, which shows the path carries 1200.
4001
+ if ((paddedInitial || this.pathFrameStaged) && remaining > 0) {
3444
4002
  const padded: QuicConnSpace = this.spaceAt(last)
3445
4003
  padded.builtEnd = quicPutPadding(out, padded.builtEnd, remaining)
3446
4004
  }
@@ -3464,6 +4022,7 @@ export class QuicConnection {
3464
4022
  return 0
3465
4023
  }
3466
4024
  this.bytesSent = this.bytesSent + toI64(total)
4025
+ this.notePathSent(total)
3467
4026
  return total
3468
4027
  }
3469
4028
 
@@ -3495,14 +4054,22 @@ export class QuicConnection {
3495
4054
  * The frames of one packet of `space`, written into `out[start .. end)`:
3496
4055
  * an ACK when one is due, and, when `elicit` allows frames that elicit an
3497
4056
  * acknowledgement, CRYPTO data (lost bytes first), at the application
3498
- * level HANDSHAKE_DONE, RETIRE_CONNECTION_ID, NEW_CONNECTION_ID,
3499
- * PATH_RESPONSE, the streams' control frames, DATAGRAMs and stream data,
3500
- * and for a probe with nothing else to carry a PING. Answers the offset
4057
+ * level HANDSHAKE_DONE, RETIRE_CONNECTION_ID, NEW_CONNECTION_ID, the path
4058
+ * frames, the streams' control frames, DATAGRAMs and stream data, and for
4059
+ * a probe with nothing else to carry a PING. Without `elicit`, the path
4060
+ * frames still go when `pathFrames` says the packet can be recorded. Answers the offset
3501
4061
  * past the last frame, `start` when nothing is due. What it carries is
3502
4062
  * written to the space's staging row, and `stagedEliciting` says whether
3503
4063
  * anything elicits an acknowledgement.
3504
4064
  */
3505
- buildPayloadInto(space: QuicConnSpace, out: u8[], start: i32, end: i32, elicit: boolean): i32 {
4065
+ buildPayloadInto(
4066
+ space: QuicConnSpace,
4067
+ out: u8[],
4068
+ start: i32,
4069
+ end: i32,
4070
+ elicit: boolean,
4071
+ pathFrames: boolean
4072
+ ): i32 {
3506
4073
  let at: i32 = start
3507
4074
  space.clearStaged()
3508
4075
  if (space.received.ackPending) {
@@ -3513,6 +4080,9 @@ export class QuicConnection {
3513
4080
  }
3514
4081
  }
3515
4082
  if (!elicit) {
4083
+ if (pathFrames && space.level === TLS_LEVEL_APPLICATION) {
4084
+ at = this.buildPathFrames(space, out, at, end)
4085
+ }
3516
4086
  return at
3517
4087
  }
3518
4088
  at = this.buildCrypto(space, out, at, end)
@@ -3579,6 +4149,57 @@ export class QuicConnection {
3579
4149
  return next
3580
4150
  }
3581
4151
 
4152
+ /**
4153
+ * The path frames of a 1-RTT packet into `out[at .. end)`: every
4154
+ * PATH_RESPONSE owed and the PATH_CHALLENGE on a path being validated.
4155
+ * They go even when the congestion window holds everything else back
4156
+ * (`buildPayloadInto`), so the first packet on a new path validates it
4157
+ * (RFC 9000 §9.3); the path's own 3x limit still bounds them, and a
4158
+ * challenge is drawn at most once a probe timeout.
4159
+ */
4160
+ buildPathFrames(space: QuicConnSpace, out: u8[], start: i32, end: i32): i32 {
4161
+ let at: i32 = start
4162
+ while (this.pathCount > 0 && end - at >= 9) {
4163
+ const next: i32 = quicPutPathData(
4164
+ out,
4165
+ at,
4166
+ end,
4167
+ QUIC_FRAME_PATH_RESPONSE,
4168
+ this.pathData,
4169
+ (this.pathHead % 4) * QUIC_PATH_DATA_SIZE
4170
+ )
4171
+ if (next < 0) {
4172
+ break
4173
+ }
4174
+ at = next
4175
+ this.pathHead = (this.pathHead + 1) % 4
4176
+ this.pathCount = this.pathCount - 1
4177
+ // Ack-eliciting, but never sent again (RFC 9000 §13.3).
4178
+ space.stagedEliciting = true
4179
+ this.pathFrameStaged = true
4180
+ }
4181
+ // The PATH_CHALLENGE on a path being validated; one lost is not sent
4182
+ // again, but `runPathClock` draws fresh data a probe timeout later.
4183
+ if (this.challengeOwed && this.challengesDrawn > 0 && end - at >= 9) {
4184
+ const slot: i32 = toI32((this.challengesDrawn - 1) % toI64(QUIC_CONN_CHALLENGES))
4185
+ const next: i32 = quicPutPathData(
4186
+ out,
4187
+ at,
4188
+ end,
4189
+ QUIC_FRAME_PATH_CHALLENGE,
4190
+ this.challengeData,
4191
+ slot * QUIC_PATH_DATA_SIZE
4192
+ )
4193
+ if (next >= 0) {
4194
+ at = next
4195
+ this.challengeOwed = false
4196
+ space.stagedEliciting = true
4197
+ this.pathFrameStaged = true
4198
+ }
4199
+ }
4200
+ return at
4201
+ }
4202
+
3582
4203
  /**
3583
4204
  * The 1-RTT frames beyond ACK, as many as fit in `out[at .. end)` and in
3584
4205
  * the packet's record: `QUIC_CONN_PACKET_CONTROL` control frames and
@@ -3593,7 +4214,7 @@ export class QuicConnection {
3593
4214
  space.stagedEliciting = true
3594
4215
  }
3595
4216
  // RETIRE_CONNECTION_ID is at most 9 bytes, NEW_CONNECTION_ID with an
3596
- // 8-byte ID at most 42, PATH_RESPONSE 9.
4217
+ // 8-byte ID at most 42.
3597
4218
  while (
3598
4219
  this.cids.retireCount > 0 &&
3599
4220
  end - at >= 9 &&
@@ -3623,24 +4244,7 @@ export class QuicConnection {
3623
4244
  space.stageControl(QUIC_CONN_CONTROL_NEW_CID, entry.sequence)
3624
4245
  entry = this.cids.nextUnannounced()
3625
4246
  }
3626
- while (this.pathCount > 0 && end - at >= 9) {
3627
- const next: i32 = quicPutPathData(
3628
- out,
3629
- at,
3630
- end,
3631
- QUIC_FRAME_PATH_RESPONSE,
3632
- this.pathData,
3633
- (this.pathHead % 4) * QUIC_PATH_DATA_SIZE
3634
- )
3635
- if (next < 0) {
3636
- break
3637
- }
3638
- at = next
3639
- this.pathHead = (this.pathHead + 1) % 4
3640
- this.pathCount = this.pathCount - 1
3641
- // Ack-eliciting, but never sent again (RFC 9000 §13.3).
3642
- space.stagedEliciting = true
3643
- }
4247
+ at = this.buildPathFrames(space, out, at, end)
3644
4248
  const streams: QuicStreams = this.streams
3645
4249
  while (space.controlCount(space.staging) < QUIC_CONN_PACKET_CONTROL) {
3646
4250
  const next: i32 = streams.putNextControl(out, at, end)
@@ -3851,6 +4455,7 @@ export class QuicConnection {
3851
4455
  position = position + this.sealAt(space, out, position, payloadEnd)
3852
4456
  }
3853
4457
  this.bytesSent = this.bytesSent + toI64(position - at)
4458
+ this.notePathSent(position - at)
3854
4459
  return position - at
3855
4460
  }
3856
4461