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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264// 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) on this path. §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. sent_ect0: 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. pub fn codepoint(v: *const Validator) Codepoint { return switch (v.state) { .testing, .capable => .ect0, .unknown, .failed => .not_ect, }; }
/// Notes that a packet went out, and whether it was marked. pub fn onSent(v: *Validator, marked: bool) void { if (!marked) return; v.sent_ect0 +|= 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; }
/// §13.4.2.1, on an ACK frame that moved the largest acknowledged. /// /// `newly_acked_marked` is how many of the packets this frame newly /// acknowledged had been sent with ECT(0). The caller counts them because /// only the caller knows which packets the frame covered. /// /// 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." pub fn onAck(v: *Validator, reported: ?Counts, newly_acked_marked: u64) bool { 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(); }
v.last = counts; if (newly_acked_marked > 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) v.state = .capable; return true; }
/// §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." pub fn onMarkedLost(v: *Validator, marked_outstanding: bool) void { if (v.state == .failed) return; if (!marked_outstanding and v.sent_ect0 > 0 and !v.acked_marked) _ = v.fail(); }
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. pub fn restart(v: *Validator) void { v.* = .{}; }};
const t = std.testing;
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)); 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)); 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)); // Two more marked packets acknowledged, and the counts moved by one. try t.expect(!v.onAck(.{ .ect0 = 3, .ect1 = 0, .ce = 0 }, 2)); 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)); try t.expect(v.onAck(.{ .ect0 = 1, .ect1 = 0, .ce = 3 }, 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));
// 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));}
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)); try t.expectEqual(State.unknown, v.state); try t.expect(v.onAck(.{ .ect0 = 4, .ect1 = 0, .ce = 0 }, 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)); try t.expectEqual(State.failed, v.state); v.restart(); try t.expectEqual(State.testing, v.state); try t.expectEqual(Codepoint.ect0, v.codepoint());}