Something went wrong. Try again.
QUIC for Zig 0.17: RFC 9000-9002 with TLS 1.3, as a sans-I/O core plus a socket layer, with datagrams, multipath and raw public keys.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! §13.4 of RFC 9000: deciding whether a path carries ECN markings honestly,//! and giving up on it when it does not.//!//! The marking itself is two bits of the IP header, which this library never//! touches -- `Connection.send` says which codepoint a datagram wants and the//! caller's socket applies it, the same division as everywhere else here.//! What lives in this file is the arithmetic that decides what to ask for.//!//! Almost all of it is distrust, and §13.4.2 says why: "It is possible for//! faulty network devices to corrupt or erroneously drop packets that carry a//! non-zero ECN codepoint." A middlebox that zeroes the field makes a peer//! report nothing; one that rewrites everything to CE makes a sender throttle//! itself to nothing. Both are indistinguishable from a working path except//! by counting, which is what this does.
const std = @import("std");const assert = std.debug.assert;
/// The two-bit ECN field of an IP header, as RFC 3168 numbers it.pub const Codepoint = enum(u2) { not_ect = 0b00, ect1 = 0b01, ect0 = 0b10, ce = 0b11,};
/// The counts a peer reports, which §19.3 puts in an ACK frame.pub const Counts = struct { ect0: u64, ect1: u64, ce: u64,};
/// Appendix A.4's four states, and the transitions between them.////// "On paths with a 'testing' or 'capable' state, the endpoint sends packets/// with an ECT marking -- ECT(0) by default; otherwise, the endpoint sends/// unmarked packets."pub const State = enum { testing, unknown, failed, capable };
/// §13.4.2 suggests "limiting this to ten packets or three times the PTO"./// Ten packets, because a count is something this side can hold and a/// duration is something it would have to be told.pub const testing_packets = 10;
pub const Validator = struct { state: State = .testing, /// How many more packets to mark before the testing period ends. remaining: u32 = testing_packets, /// Every packet this endpoint has marked ECT(0) in the packet number /// space being validated. §13.4.2.1 fails validation "if the received /// total count for either ECT(0) or ECT(1) exceeds the total number of /// packets sent with each corresponding ECT codepoint", and this is that /// total. /// /// Kept across `restart`, as `last` is: §13.4.1 has a peer count per /// packet number space, not per path, so its totals run on through a /// migration and have to be compared with totals that do too. sent_ect0: u64 = 0, /// The packets marked since the path being tested became the one in /// use, which is what `onMarkedLost` asks about. marked_on_path: u64 = 0, /// The number of the first packet sent on the path being tested. The /// packet number space runs on through a migration, so a packet numbered /// below this went out on a path before it -- and its fate, whether /// acknowledged or lost, says nothing about this one. `onPath` asks. first_on_path: u64 = 0, /// How far the peer's ECT(0) and CE counts have grown since then, which /// is what Appendix A.4's "if they are all ECN-CE marked" asks about. /// Growth rather than totals, since the totals include whatever the /// paths before this one were marked with. ect0_on_path: u64 = 0, ce_on_path: u64 = 0, /// The counts from the last ACK that moved the largest acknowledged, which /// is what an increase is measured against. last: ?Counts = null, /// Whether any packet sent with a marking has been acknowledged, which /// Appendix A.4 requires before "unknown" may become "capable" -- without /// it, a path that has carried no marked packet at all would be declared /// good on the strength of having said nothing. acked_marked: bool = false,
/// What to mark the next outgoing datagram with: ECT(0) while the path /// is being tested or has proved capable, and nothing otherwise, which is /// Appendix A.4's rule. Never ECT(1) and never CE -- the first is not this /// endpoint's to use, and the second is the network's to set. pub fn codepoint(v: *const Validator) Codepoint { return switch (v.state) { .testing, .capable => .ect0, .unknown, .failed => .not_ect, }; }
test codepoint { var v: Validator = .{}; // A new path is tested, so the first datagram is marked. try std.testing.expectEqual(Codepoint.ect0, v.codepoint()); // And a path that failed validation is not marked again. v.onSent(true); _ = v.onAck(null, 1, 1); try std.testing.expectEqual(Codepoint.not_ect, v.codepoint()); }
/// Notes that a packet went out, and whether it was marked ECT(0) -- /// that is, whether it was sent with what `codepoint` answered. /// /// Called once per QUIC packet rather than per datagram, since that is /// what a peer counts. A marked packet adds to the total §13.4.2.1 /// checks reports against, and while the path is being tested it uses up /// one of the `testing_packets`; the last one moves the path to /// "unknown". An unmarked packet changes nothing. pub fn onSent(v: *Validator, marked: bool) void { if (!marked) return; v.sent_ect0 +|= 1; v.marked_on_path +|= 1; if (v.state != .testing) return; // "The testing period runs for a number of packets or a limited time, // as determined by the endpoint... After the testing period ends, the // ECN state for the path becomes 'unknown'." Unknown is unmarked: the // question has been asked and the answer is not back yet, so asking // it louder would only cost more packets if the path is hostile. v.remaining -|= 1; if (v.remaining == 0) v.state = .unknown; }
test onSent { var v: Validator = .{}; for (0..testing_packets) |_| v.onSent(v.codepoint() == .ect0); try std.testing.expectEqual(@as(u64, testing_packets), v.sent_ect0); // The testing period is over, and the answer is not back yet. try std.testing.expectEqual(State.unknown, v.state); try std.testing.expectEqual(Codepoint.not_ect, v.codepoint()); }
/// §13.4.2.1, on an ACK frame that moved the largest acknowledged. /// /// `reported` is the frame's ECN counts, or null for an ACK frame without /// them. `newly_acked_marked` is how many of the packets this frame newly /// acknowledged had been sent with ECT(0), and `on_path` how many of those /// were sent on the path being tested (see `onPath`). The caller counts /// them because only the caller knows which packets the frame covered. /// The first is what the peer's counts are held to, since it counts /// every packet in the space; the second is what shows this path carries /// a marked packet at all. /// /// Answers whether validation still holds. A false answer is permanent /// for this path: §13.4.2.2, "If validation fails, then the endpoint MUST /// disable ECN. It stops setting the ECT codepoint in IP packets that it /// sends." Once failed, every later call answers false without looking. /// A true answer keeps `reported` as what the next increase is measured /// from, and moves an "unknown" path to "capable" once any marked packet /// has been acknowledged -- or to "failed", answering false, when every /// mark the path has reported since its testing began came back CE. pub fn onAck(v: *Validator, reported: ?Counts, newly_acked_marked: u64, on_path: u64) bool { assert(on_path <= newly_acked_marked); if (v.state == .failed) return false;
// "If an ACK frame newly acknowledges a packet that the endpoint sent // with either the ECT(0) or ECT(1) codepoint set, ECN validation // fails if the corresponding ECN counts are not present in the ACK // frame. This check detects a network element that zeroes the ECN // field or a peer that does not report ECN markings." const counts = reported orelse { if (newly_acked_marked > 0) return v.fail(); return true; };
// "ECN validation can fail if the received total count for either // ECT(0) or ECT(1) exceeds the total number of packets sent with each // corresponding ECT codepoint. In particular, validation will fail // when an endpoint receives a non-zero ECN count corresponding to an // ECT codepoint that it never applied." This endpoint never sends // ECT(1), so any count of it at all is the network remarking. if (counts.ect1 > 0) return v.fail(); if (counts.ect0 > v.sent_ect0) return v.fail();
if (v.last) |previous| { // Counts only ever grow; a peer that reports fewer than it did is // not reporting the same connection's history. if (counts.ect0 < previous.ect0 or counts.ce < previous.ce) return v.fail();
// "ECN validation also fails if the sum of the increase in ECT(0) // and ECN-CE counts is less than the number of newly acknowledged // packets that were originally sent with an ECT(0) marking... // These checks can detect remarking of ECN-CE markings by the // network." const grew = (counts.ect0 - previous.ect0) + (counts.ce - previous.ce); if (grew < newly_acked_marked) return v.fail(); }
const before = v.last orelse Counts{ .ect0 = 0, .ect1 = 0, .ce = 0 }; v.ect0_on_path +|= counts.ect0 - before.ect0; v.ce_on_path +|= counts.ce - before.ce; v.last = counts; if (on_path > 0) v.acked_marked = true;
// "From the 'unknown' state, successful validation of the ECN counts // in an ACK frame causes the ECN state for the path to become // 'capable', unless no marked packet has been acknowledged." if (v.state == .unknown and v.acked_marked) { // "An endpoint can also mark the ECN state for a path as // 'failed'... if they are all ECN-CE marked." Asked once, as the // testing period's verdict, and not of a capable path afterwards: // by then CE is the path reporting congestion, which is what it // is for, and a link that is busy for a while would otherwise // lose ECN just when it is most useful. A path congested from // its very first packet is failed by this too -- far less likely // than a middlebox rewriting the field, and costing only the // markings, not the connection. if (v.ect0_on_path == 0 and v.ce_on_path > 0) return v.fail(); v.state = .capable; } return true; }
test onAck { var v: Validator = .{}; for (0..testing_packets) |_| v.onSent(true); // Three of the marked packets acknowledged, two arriving as ECT(0) // and one with a router's congestion mark: the counts add up. try std.testing.expect(v.onAck(.{ .ect0 = 2, .ect1 = 0, .ce = 1 }, 3, 3)); try std.testing.expectEqual(State.capable, v.state); // Two more, and the counts did not move: something is erasing marks. try std.testing.expect(!v.onAck(.{ .ect0 = 2, .ect1 = 0, .ce = 1 }, 2, 2)); try std.testing.expectEqual(State.failed, v.state); }
/// §13.4.2: "An endpoint can also mark the ECN state for a path as /// 'failed' if marked packets are all declared lost or if they are all /// ECN-CE marked." /// /// The first half of that, for the caller to call after declaring a /// marked packet lost. `marked_outstanding` says whether any marked /// packet is still in flight. When none is, at least one was sent on /// this path, and none has been acknowledged since it became the one in /// use, then every marked packet was lost and the path is failed. The /// all-CE half is `onAck`'s, since only an ACK reports CE. pub fn onMarkedLost(v: *Validator, marked_outstanding: bool) void { if (v.state == .failed) return; if (!marked_outstanding and v.marked_on_path > 0 and !v.acked_marked) _ = v.fail(); }
test onMarkedLost { var v: Validator = .{}; v.onSent(true); v.onSent(true); // One of two declared lost: the other may yet arrive. v.onMarkedLost(true); try std.testing.expectEqual(State.testing, v.state); // Both lost, and neither ever acknowledged. v.onMarkedLost(false); try std.testing.expectEqual(State.failed, v.state); }
/// Whether the packet numbered `number` was sent on the path being /// tested, rather than on one before it. pub fn onPath(v: *const Validator, number: u64) bool { return number >= v.first_on_path; }
test onPath { var v: Validator = .{}; try std.testing.expect(v.onPath(0)); v.restart(7); try std.testing.expect(!v.onPath(6)); try std.testing.expect(v.onPath(7)); }
fn fail(v: *Validator) bool { v.state = .failed; return false; }
/// Starts again on a path this endpoint has not tested. /// /// §13.4.2: "An endpoint thus attempts to use ECN and validates this for /// each new connection, when switching to a server's preferred address, /// and on active connection migration to a new path." A path is a /// different set of network elements, so what was learned about the last /// one says nothing about this one -- in either direction. /// /// What is not about the path survives: the totals sent and last /// reported, which belong to the packet number space (see `sent_ect0`). /// `next_number` is the number the space's next packet will carry, the /// first on the new path. pub fn restart(v: *Validator, next_number: u64) void { v.* = .{ .sent_ect0 = v.sent_ect0, .last = v.last, .first_on_path = next_number }; }
test restart { var v: Validator = .{}; v.onSent(true); try std.testing.expect(!v.onAck(null, 1, 1)); // The connection moved to a new path: what failed was the old one. v.restart(1); try std.testing.expectEqual(State.testing, v.state); try std.testing.expectEqual(Codepoint.ect0, v.codepoint()); // The space's total is still the one the peer's counts are held to. try std.testing.expectEqual(@as(u64, 1), v.sent_ect0); }};
const t = std.testing;
test "RFC 9000 13.4.2: a new path is tested against counts the old one ran up" { // §13.4.1: counts are kept per packet number space, not per path, so the // peer's totals carry straight across a migration. A validator that // forgot what it had sent would read them as more marks than it ever // applied, and fail a path it had not yet sent a packet on. var v: Validator = .{}; for (0..testing_packets) |_| v.onSent(true); try t.expect(v.onAck(.{ .ect0 = 10, .ect1 = 0, .ce = 0 }, 10, 10)); try t.expectEqual(State.capable, v.state);
v.restart(1); try t.expectEqual(State.testing, v.state); v.onSent(true); try t.expect(v.onAck(.{ .ect0 = 11, .ect1 = 0, .ce = 0 }, 1, 1));}
test "RFC 9000 13.4.2: a new path is not proved by the old path's packets" { // The old path's marked packets can still be acknowledged after the // move. Their counts are the space's and are checked as such, but that // the old path carried them says nothing about whether the new one // does -- so they cannot make it capable, and cannot stop its own // packets all being lost from failing it. var v: Validator = .{}; for (0..testing_packets) |_| v.onSent(true); v.restart(testing_packets); for (0..testing_packets) |_| v.onSent(true); try t.expectEqual(State.unknown, v.state); try t.expect(v.onAck(.{ .ect0 = testing_packets, .ect1 = 0, .ce = 0 }, testing_packets, 0)); try t.expectEqual(State.unknown, v.state); v.onMarkedLost(false); try t.expectEqual(State.failed, v.state);}
test "RFC 9000 13.4.2: a new path is not failed for losses it never had" { // "If marked packets are all declared lost" is a statement about the // path being tested, and the new one has not marked anything yet. var v: Validator = .{}; v.onSent(true); v.restart(1); v.onMarkedLost(false); try t.expectEqual(State.testing, v.state);
// Once it has, and lost all of them, it is. v.onSent(true); v.onMarkedLost(false); try t.expectEqual(State.failed, v.state);}
test "RFC 9000 13.4.2: a path that reports nothing for a marked packet fails" { // "ECN validation fails if the corresponding ECN counts are not present // in the ACK frame. This check detects a network element that zeroes the // ECN field or a peer that does not report ECN markings." var v: Validator = .{}; try t.expectEqual(Codepoint.ect0, v.codepoint()); v.onSent(true); try t.expect(!v.onAck(null, 1, 1)); try t.expectEqual(State.failed, v.state); // §13.4.2.2: "It stops setting the ECT codepoint in IP packets that it // sends." try t.expectEqual(Codepoint.not_ect, v.codepoint());}
test "RFC 9000 13.4.2: an unmarked packet acknowledged without counts is fine" { // The same absence, with nothing marked to explain: a peer that reports // no counts because there was nothing to report has said nothing wrong. var v: Validator = .{}; try t.expect(v.onAck(null, 0, 0)); try t.expectEqual(State.testing, v.state);}
test "RFC 9000 13.4.2.1: counts that grow by less than what was marked fail" { // "ECN validation also fails if the sum of the increase in ECT(0) and // ECN-CE counts is less than the number of newly acknowledged packets // that were originally sent with an ECT(0) marking." var v: Validator = .{}; for (0..4) |_| v.onSent(true); try t.expect(v.onAck(.{ .ect0 = 2, .ect1 = 0, .ce = 0 }, 2, 2)); // Two more marked packets acknowledged, and the counts moved by one. try t.expect(!v.onAck(.{ .ect0 = 3, .ect1 = 0, .ce = 0 }, 2, 2)); try t.expectEqual(State.failed, v.state);}
test "RFC 9000 A.4: a path that marks every tested packet CE fails" { // "An endpoint can also mark the ECN state for a path as 'failed' if // marked packets are all declared lost or if they are all ECN-CE // marked." Every count adds up, so §13.4.2.1 finds nothing wrong -- but // a path that reports congestion on every packet it was ever given is // far more likely to be a middlebox rewriting the field than a link that // is congested from the first packet, and believing it would hold the // sender at its minimum window for as long as the connection lasts. var v: Validator = .{}; for (0..testing_packets) |_| v.onSent(true); try t.expectEqual(State.unknown, v.state); try t.expect(!v.onAck(.{ .ect0 = 0, .ect1 = 0, .ce = testing_packets }, testing_packets, testing_packets)); try t.expectEqual(State.failed, v.state); try t.expectEqual(Codepoint.not_ect, v.codepoint());}
test "RFC 9000 A.4: a path that marks some tested packets CE is capable" { // Congestion is what ECN is for: one ECT(0) coming back intact is // enough to show the field is carried, and the CE beside it is real. var v: Validator = .{}; for (0..testing_packets) |_| v.onSent(true); try t.expect(v.onAck(.{ .ect0 = 1, .ect1 = 0, .ce = testing_packets - 1 }, testing_packets, testing_packets)); try t.expectEqual(State.capable, v.state);}
test "RFC 9000 A.4: a new path is judged on its own CE marks" { // The counts run on through a migration (§13.4.1), so the old path's // ECT(0) total must not vouch for a new path whose every packet comes // back CE. var v: Validator = .{}; for (0..testing_packets) |_| v.onSent(true); try t.expect(v.onAck(.{ .ect0 = testing_packets, .ect1 = 0, .ce = 0 }, testing_packets, testing_packets)); try t.expectEqual(State.capable, v.state);
v.restart(testing_packets); for (0..testing_packets) |_| v.onSent(true); try t.expect(!v.onAck(.{ .ect0 = testing_packets, .ect1 = 0, .ce = testing_packets }, testing_packets, testing_packets)); try t.expectEqual(State.failed, v.state);}
test "RFC 9000 13.4.2.1: CE counts toward the increase rather than against it" { // A router marking congestion is the path working, not failing: the sum // of ECT(0) and CE is what has to keep up. var v: Validator = .{}; for (0..4) |_| v.onSent(true); try t.expect(v.onAck(.{ .ect0 = 1, .ect1 = 0, .ce = 1 }, 2, 2)); try t.expect(v.onAck(.{ .ect0 = 1, .ect1 = 0, .ce = 3 }, 2, 2)); try t.expect(v.state != .failed);}
test "RFC 9000 13.4.2.1: a count of a codepoint never applied fails" { // "In particular, validation will fail when an endpoint receives a // non-zero ECN count corresponding to an ECT codepoint that it never // applied." Nothing here ever sends ECT(1). var v: Validator = .{}; v.onSent(true); try t.expect(!v.onAck(.{ .ect0 = 1, .ect1 = 1, .ce = 0 }, 1, 1));
// And more ECT(0) than were ever sent, which is the same remarking seen // from the other side. var w: Validator = .{}; w.onSent(true); try t.expect(!w.onAck(.{ .ect0 = 2, .ect1 = 0, .ce = 0 }, 1, 1));}
test "RFC 9000 A.4: testing gives way to unknown, and an acknowledgement to capable" { var v: Validator = .{}; try t.expectEqual(State.testing, v.state); for (0..testing_packets) |_| { try t.expectEqual(Codepoint.ect0, v.codepoint()); v.onSent(true); } // "After the testing period ends, the ECN state for the path becomes // 'unknown'", and an unknown path is not marked. try t.expectEqual(State.unknown, v.state); try t.expectEqual(Codepoint.not_ect, v.codepoint());
// "successful validation of the ECN counts in an ACK frame... causes the // ECN state for the path to become 'capable', unless no marked packet has // been acknowledged." try t.expect(v.onAck(.{ .ect0 = 0, .ect1 = 0, .ce = 0 }, 0, 0)); try t.expectEqual(State.unknown, v.state); try t.expect(v.onAck(.{ .ect0 = 4, .ect1 = 0, .ce = 0 }, 4, 4)); try t.expectEqual(State.capable, v.state); try t.expectEqual(Codepoint.ect0, v.codepoint());}
test "a new path is tested again from nothing" { // §13.4.2 validates "for each network path", and a failure belongs to the // elements that caused it rather than to the connection. var v: Validator = .{}; v.onSent(true); try t.expect(!v.onAck(null, 1, 1)); try t.expectEqual(State.failed, v.state); v.restart(1); try t.expectEqual(State.testing, v.state); try t.expectEqual(Codepoint.ect0, v.codepoint());}