sip, engine: the retry of an INVITE refused 422

imsd asks for a session interval of 1800 s in a call's first INVITE. A
422 (Session Interval Too Small) is a server's answer to an interval
below its minimum and carries that minimum in a Min-SE (RFC 4028 6);
TS 24.229 5.1.2A.1.1 has a UE that supports RFC 4028 retry an INVITE so
refused in accordance with RFC 4028 7.4. imsd announces "timer" and
took a 422 for any other refusal: on a reporter's phone on AT&T's
network each of the five calls of one boot ended "INVITE final 422"
(AGENTS.md §13).

The call machine now sends the INVITE again in the same call: a new
transaction under the Call-ID, with the next sequence number, the 422's
Min-SE and a session interval no smaller. AGENTS.md §7.6 has when it
does not, and what becomes of an answer to an INVITE it has replaced.

Tested: the suites, and the daemon of this commit on the workstation
against a stand-in network that answers 422 (the fp6 journal's
2026-12-01-min-se-retry capture, bench/).
This commit is contained in:
Jorijn van der Graaf 2026-12-01 15:51:21 +01:00
commit 0026213073
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
6 changed files with 459 additions and 39 deletions

View file

@ -17,7 +17,8 @@ That keeps the whole call FSM — PRACK on reliable 18x, ACK of 2xx vs non-2xx
finals, CANCEL/answer races, remote BYE, media-plane far-end-hangup, the
emergency attempt chain (urn:service:sos first for a classified number, one
fallback to a plain INVITE of the digits; a 380 Alternative Service upgrade
the other way), the terminating (UAS) side: 100/180 on an inbound INVITE,
the other way), an INVITE refused 422 sent again with the Min-SE the 422
names (RFC 4028 7.3, 7.4), the terminating (UAS) side: 100/180 on an inbound INVITE,
200-with-answer on Accept, 486 reject, the remote-CANCEL 200+487 pair — and
DTMF, which reaches the media leg only on an active call whose SDP
negotiated telephone-event, as the tones a keypad has —
@ -417,6 +418,10 @@ export namespace imsd::engine {
// imsd.py INVITE_TIMEOUT / CANCEL_487_TIMEOUT constants.
inline constexpr double InviteTimeout = 90.0;
inline constexpr double Cancel487Timeout = 10.0;
// How often an INVITE goes out again for a 422 under one Call-ID
// (CallMachine::OnInviteResponse). RFC 4028 sets no number: this is
// imsd's bound for a path that names a larger minimum every time.
inline constexpr int MinSeAgainMax = 3;
// How long an unanswered incoming call may ring before we 480 it — a
// safety net behind the network's own CANCEL, so generous.
inline constexpr double IncomingRingTimeout = 120.0;
@ -494,22 +499,23 @@ export namespace imsd::engine {
return a;
}
// The current outgoing attempt has sent its INVITE and has had no
// response to it, not a 100 either, and no CANCEL has followed it.
// An outgoing call's current INVITE has been sent and has had no
// response, not a 100 either, and no CANCEL has followed it.
// The shell asks when it finds the client flow dead under a call: a
// flow the network had forgotten is reset at the first bytes sent
// on it, and those may be this INVITE.
bool Unanswered() const {
return state_ == CallState::Dialing && !inviteOut_.empty() && !answered_ && !cancelled_;
}
// … and the attempt has not been sent again yet.
// … and that INVITE has not gone out a second time over a reopened
// flow.
bool ResendDue() const { return Unanswered() && !resent_; }
// The shell reopened the flow under an attempt whose resend is due:
// The shell reopened the flow under an INVITE whose resend is due:
// the same INVITE again, byte for byte (the same branch: RFC 3261
// 17.2.3 has a server that did receive the first match this one to
// its transaction), and the setup deadline from now. Once per
// attempt.
// INVITE: one that AgainWithMinSe sends has a resend of its own.
std::vector<Action> OnFlowReopened() {
std::vector<Action> a;
if (!ResendDue()) return a;
@ -520,7 +526,7 @@ export namespace imsd::engine {
return a;
}
// The flow under an unanswered attempt is dead and stays so (it
// The flow under an unanswered INVITE is dead and stays so (it
// could not be reopened, or died again after the resend): the call
// ends here, an emergency call's first attempt too, with nothing to
// send a CANCEL on and no flow for a fallback attempt.
@ -597,11 +603,17 @@ export namespace imsd::engine {
return a;
}
// A response to our INVITE (the shell already matched Call-ID + CSeq).
// A response to an INVITE of ours (the shell has matched the Call-ID
// and the method).
std::vector<Action> OnInviteResponse(std::string_view msg) {
std::vector<Action> a;
if (direction_ == Direction::Incoming)
return a; // a UAS has no INVITE transaction of its own
// An answer to an INVITE this call has since sent again under
// its Call-ID (AgainWithMinSe) is the replaced transaction's:
// the shell matches the Call-ID and the method, the sequence
// number is told apart here.
if (!imsd::sip::ResponseOf(msg, d_.callid, std::format("{} INVITE", d_.invCseq))) return a;
AbsorbDialog(msg);
auto st = imsd::sip::Status(msg);
if (!st) return a;
@ -650,7 +662,24 @@ export namespace imsd::engine {
}
// non-2xx final
a.push_back(Send(imsd::msg::BuildAckNon2xx(ctx_, d_, std::string(imsd::sip::Header(msg, "To").value_or("")))));
std::string fin = WithReason(std::format("INVITE final {}", code), msg);
std::string fin = std::format("INVITE final {}", code);
if (code == 422) {
// Session Interval Too Small: by RFC 4028 6 it carries the
// server's minimum in a Min-SE, and TS 24.229 5.1.2A.1.1 has
// a UE retry an INVITE so refused by RFC 4028 7.4. Only an
// INVITE that awaits its final (no CANCEL out, no answer
// taken), not for a Min-SE no larger than the one the
// refused INVITE carried, and not more than MinSeAgainMax
// times under one Call-ID.
const auto minSe = imsd::sip::MinSe(msg);
fin += minSe ? std::format(", Min-SE {}", *minSe) : std::string(", no Min-SE read");
const bool awaited = !cancelled_ && (state_ == CallState::Dialing || state_ == CallState::Ringing);
if (minSe && awaited && *minSe > d_.minSe && minSeAgain_ < MinSeAgainMax) {
Append(a, AgainWithMinSe(*minSe, WithReason(fin, msg)));
return a;
}
}
fin = WithReason(fin, msg);
if (auto sos = FallbackTo(code, msg)) {
Append(a, Retry(*sos, fin));
return a;
@ -868,12 +897,13 @@ export namespace imsd::engine {
bool sosTried_ = false;
bool plainTried_ = false;
bool userCancelled_ = false; // CANCEL came from OnHangup, not a deadline
// The current outgoing attempt's INVITE as sent, whether any response
// to it has come, and whether it went out a second time over a
// reopened flow (OnFlowReopened).
// The current INVITE as sent, whether any response to it has come,
// and whether it went out a second time over a reopened flow
// (OnFlowReopened).
std::string inviteOut_;
bool answered_ = false;
bool resent_ = false;
int minSeAgain_ = 0; // INVITEs sent again for a 422 under this Call-ID
std::set<std::string> pracked_;
std::string answerSdp_;
std::optional<int> dtmfPt_; // the running leg's telephone-event pt, set at media start
@ -889,23 +919,65 @@ export namespace imsd::engine {
d_.callid = std::format("{}@{}", rng_.Token(20), ctx_.local);
d_.itag = rng_.Token(10);
d_.invBranch = rng_.Token(20);
nextCseq_ = 2;
pracked_.clear();
answerSdp_.clear();
NewTransaction();
cancelled_ = false;
userCancelled_ = false;
minSeAgain_ = 0;
}
// What each INVITE of an outgoing call starts from, a dialog's first
// (ResetDialog) and one sent again in it (AgainWithMinSe): the
// requests within the dialog numbered after it, no 18x PRACKed, no
// answer, no response to it and no resend of it.
void NewTransaction() {
nextCseq_ = d_.invCseq + 1;
pracked_.clear();
answerSdp_.clear();
answered_ = false;
resent_ = false;
}
// The INVITE a 422 refused, again (RFC 4028 7.3, 7.4): a new
// transaction of the same call. Its dialog keeps the request-URI,
// the Call-ID and our tag, takes the next sequence number the
// Call-ID has not carried (7.3 has "one higher than the previous":
// the one after the refused INVITE's, or, which is ours, the one
// after the last PRACK's where a PRACK followed that INVITE), a
// branch of its own and `minSe` as its Min-SE. Its session interval
// is the refused INVITE's, or `minSe` where that was smaller. What
// the refused transaction left (the refusal's To tag, a route, a
// target, and what NewTransaction clears) is not the new one's.
// Whether it is sent is the caller's (OnInviteResponse).
std::vector<Action> AgainWithMinSe(int minSe, std::string_view why) {
imsd::msg::Dialog next;
next.ruri = d_.ruri;
next.callid = d_.callid;
next.itag = d_.itag;
next.invBranch = rng_.Token(20);
next.invCseq = nextCseq_;
next.minSe = minSe;
next.sessionExpires = std::max(d_.sessionExpires, minSe);
d_ = std::move(next);
NewTransaction();
minSeAgain_++;
std::vector<Action> a;
a.push_back(LogAct(std::format("call {}: {} — the INVITE again with Session-Expires {}", uni_, why, d_.sessionExpires)));
if (state_ != CallState::Dialing) {
state_ = CallState::Dialing;
a.push_back(StateAct("outgoing"));
}
Append(a, Start());
return a;
}
// The emergency fallback chain — where the next attempt (if any)
// points. `msg` may be empty (deadline paths).
// - a sos INVITE that fails for any reason other than the user
// hanging up, its own send failing at the Dial (Fail) or the
// client flow lost under it (OnFlowLost: there is nothing to send
// on) is re-attempted as a plain INVITE of the dialled digits: the
// - asked after a sos attempt, it answers the dialled digits as a
// plain INVITE, unless the user hung up or the digits were tried
// before that attempt (the 380's upgrade below): the
// pre-emergency-support behavior, so classification can only improve
// on the status quo, never block a call the old code would have placed.
// An end of a sos attempt that does not ask here brings no fallback.
// - a plain INVITE answered 380 Alternative Service whose body
// names emergency (TS 24.229 5.1.6: the network classified a
// number we didn't) is re-attempted as urn:service:sos. A 380

View file

@ -6,8 +6,8 @@ Imsd:Sip — SIP message text layer + TCP framing.
Deliberately a text layer, not a full RFC 3261 parser: the engine works on
whole messages as strings and pulls out exactly what it needs (headers,
status codes, the granted registration lifetime), which keeps every message
loggable and testable as text. A header is asked for by its long name and
status codes, the granted registration lifetime, the interval a Min-SE
names), which keeps every message loggable and testable as text. A header is asked for by its long name and
found under that or its compact form (RFC 3261 7.3.3). Edge-case behavior
(empty header values, line anchoring, white space before the colon,
keepalive discards) is pinned by tests/Sip — a change that flips one of
@ -228,6 +228,18 @@ export namespace imsd::sip {
return LeadingDigits(*v);
}
// The interval a message's Min-SE names (RFC 4028 5: delta-seconds,
// then parameters), in seconds: the number its value's leading digits
// spell, zeros before it aside. nullopt when the header is absent, its
// value begins with no digit, or the number has more than nine digits.
std::optional<int> MinSe(std::string_view msg) {
auto v = Header(msg, "Min-SE");
if (!v) return std::nullopt;
const auto n = LeadingDigits(*v);
if (!n || *n > 999'999'999) return std::nullopt;
return static_cast<int>(*n);
}
// A refused final for the journal: its status line, then the headers
// that say who refused and for how long — Retry-After (RFC 3261 20.33),
// Warning (20.43: the node's own words), Reason (RFC 3326), Server