2026-07-22 22:53:28 +02:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
|
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
Imsd:Messages — the SIP request/response text the engine puts on the wire.
|
|
|
|
|
|
|
|
|
|
Every VoLTE message the daemon sends is assembled here from an explicit
|
2026-12-01 15:51:21 +01:00
|
|
|
context and the per-request variables so the builders
|
2026-07-22 22:53:28 +02:00
|
|
|
are pure functions with no hidden state: the engine owns the dialog bookkeeping
|
messages, imsd: P-Access-Network-Info in responses and in requests within a dialog
imsd named the serving cell in its REGISTERs, the reg-event SUBSCRIBE,
an INVITE and a MESSAGE. TS 24.229 5.1.2A.1.1 and 5.1.2A.2 have a UE
insert the header into a request within a dialog as well, and into a
response within a dialog, to a request for a dialog and to a standalone
method: not into a CANCEL nor into a response to one, and into an ACK
at its choice. A PRACK and a BYE carry it now, and every response imsd
builds but a 100 Trying, the 200 to a CANCEL and the transport's 400 to
a request cut short.
The server ports (the protected ports' TCP listeners and UDP sockets)
take a request from any source (AGENTS.md §12), and the header is the
phone's cell. What they send in answer keeps it only on the two paths
of the SA pair the shell has on record, and loses it anywhere else.
Tested: the suites, and the daemon of this commit on the workstation
against a stand-in network that asks it from both sides of those paths
(the fp6 journal's 2026-12-04-pani-in-dialog capture, bench/), and on
the dev phone on KPN, where its answers to a text's two requests and a
call's 180 and 200 carried the header inside the SA pair (the
journal's 2026-12-04-imsd-1.2.7-range-on-dev-phone capture).
2026-12-04 23:20:38 +01:00
|
|
|
and the transport owns the socket (and takes the P-Access-Network-Info line
|
|
|
|
|
out of an answer the server ports send on neither path of the SA pair on
|
|
|
|
|
record), this module only formats bytes. That makes
|
messages: the SIP MESSAGE of SMS over IP, +g.3gpp.smsip behind a context flag
TS 24.341 5.3.1.2 has a submission travel as a SIP MESSAGE to the
service centre's PSI with the RP-DATA as an application/vnd.3gpp.sms
body, and 5.3.2.4 the delivery report as one to the IP-SM-GW with
In-Reply-To naming the delivered MESSAGE: BuildMessageSms builds both,
out of dialog over the registration's Route with the same
P-Preferred-Identity, P-Access-Network-Info and sec-agree headers as the
INVITE and the reg-event SUBSCRIBE, and no Contact (RFC 3428 4). The REGISTER Contact carries +g.3gpp.smsip only
when Context::smsOverIp says so — the tag routes terminating short
messages to imsd (the 2026-10-17 bench's mt-last-message-in.raw), which
at this commit still answers them 200 and logs them — after the ICSI.
BuildResponse builds a final for an inbound request outside a call (the
488 of 5.3.1.2 for a report that is not ours, or a 200); the shell does
not call it in this commit — it still answers a MESSAGE with
BuildResponse200.
tests/Messages pins the Contact with the tag, the MESSAGE's headers and
body bytes both ways, the absence of Contact, and the 488.
BuildResponse echoes each Via: line and the To, with a tag added unless
the To already holds `tag=` (RFC 3261 8.2.6.2 has a UAS add a tag to a
To that carries none); tests/Messages pins the 488 and a 200.
ScPsi gives the service centre's PSI from its international number
(5.3.1.2 NOTE 1: the SC's E.164 number as a SIP URI with user=phone, or
a tel URI; the home domain is imsd's choice); RuriFor's dialled-number guess, whose trunk 0 is Dutch, has no
place there. Pinned in tests/Messages.
2026-10-16 23:13:39 +02:00
|
|
|
each message byte-pinnable — tests/Messages compares a REGISTER pair and an INVITE
|
ipsec: the default offer names the null cipher beside aes-cbc
The UE is to announce the IPsec algorithms it supports (TS 24.229
5.1.1.2.2 d), and TS 33.203 7.2 has its first REGISTER carry "a list of
identifiers for the integrity and encryption algorithms, which the UE
supports". The null cipher is one a UE must support (7.1 by TS 33.210
5.3.3 and RFC 8221; GSMA IR.92 5.3), and imsd can install it, but its
default offer named aes-cbc alone.
Ice Norway has answered imsd's first REGISTER 403 (2026-11-01, -02),
where the phone's own modem had carried a call with the CS domain
detached (2026-09-02). On Odido it drew an answer that the imsd of that
day did not log, and the Python stack's REGISTER minutes later a 403
(2026-09-16), with a SIM that another Fairphone 6's own IMS client had
registered with (2026-07-15). That client's four mechanisms were
answered with one naming ealg=null, as the Python stack's offer of the
null cipher was (2026-07-13). Whether the offer is the reason for either
answer is not measured.
The default offer is aes-cbc, then null. A P-CSCF that selects the null
cipher from it gets SAs that protect the signalling's integrity and do
not encrypt it; EALG=aes-cbc gives 1.2.1's offer.
Tested: the suites; the daemon of this commit against a stand-in P-CSCF
on the workstation that refuses an offer without the null cipher (imsd
1.2.1 is answered 403 there, this commit's is challenged and selects the
null mechanism); on the FP6 on KPN, in an earlier build of this change,
on 1.2.0, a reregistration and a fresh registration with the default
offer: KPN named aes-cbc, and answered the reregistration, the fresh
registration's protected REGISTER and the reg-event SUBSCRIBEs 200.
2026-11-23 09:32:26 +01:00
|
|
|
with strings cross-generated from the Python reference stack, header for header
|
|
|
|
|
but for the divergences that suite's header names.
|
2026-07-22 22:53:28 +02:00
|
|
|
|
|
|
|
|
Covers: the unprotected + protected REGISTER and the refresh re-REGISTER
|
|
|
|
|
(sec-agree, Security-Client/Verify, AKAv1-MD5 Authorization); the INVITE with
|
|
|
|
|
an AMR-WB SDP offer and IMS MMTEL feature tags; the in-dialog PRACK/BYE and
|
|
|
|
|
2xx/non-2xx ACK; CANCEL (reusing the INVITE branch); the 200 OK that echoes
|
messages: the SIP MESSAGE of SMS over IP, +g.3gpp.smsip behind a context flag
TS 24.341 5.3.1.2 has a submission travel as a SIP MESSAGE to the
service centre's PSI with the RP-DATA as an application/vnd.3gpp.sms
body, and 5.3.2.4 the delivery report as one to the IP-SM-GW with
In-Reply-To naming the delivered MESSAGE: BuildMessageSms builds both,
out of dialog over the registration's Route with the same
P-Preferred-Identity, P-Access-Network-Info and sec-agree headers as the
INVITE and the reg-event SUBSCRIBE, and no Contact (RFC 3428 4). The REGISTER Contact carries +g.3gpp.smsip only
when Context::smsOverIp says so — the tag routes terminating short
messages to imsd (the 2026-10-17 bench's mt-last-message-in.raw), which
at this commit still answers them 200 and logs them — after the ICSI.
BuildResponse builds a final for an inbound request outside a call (the
488 of 5.3.1.2 for a report that is not ours, or a 200); the shell does
not call it in this commit — it still answers a MESSAGE with
BuildResponse200.
tests/Messages pins the Contact with the tag, the MESSAGE's headers and
body bytes both ways, the absence of Contact, and the 488.
BuildResponse echoes each Via: line and the To, with a tag added unless
the To already holds `tag=` (RFC 3261 8.2.6.2 has a UAS add a tag to a
To that carries none); tests/Messages pins the 488 and a 200.
ScPsi gives the service centre's PSI from its international number
(5.3.1.2 NOTE 1: the SC's E.164 number as a SIP URI with user=phone, or
a tel URI; the home domain is imsd's choice); RuriFor's dialled-number guess, whose trunk 0 is Dutch, has no
place there. Pinned in tests/Messages.
2026-10-16 23:13:39 +02:00
|
|
|
an inbound request's dialog headers; the finals of the inbound MESSAGE
|
sip: a header is found under its compact form, and with white space before the colon
RFC 3261 7.3.3 lets a sender write a header name in its one-letter form
at any time and has every implementation accept both forms. The text
layer matched the long name only, so to every reader a header sent in
compact form was absent: the TCP framer took no body length from `l:`
and read the body as the start of the next message, a response with
`i:` matched no transaction and a request no call, the finals imsd
builds left out a Via, From, To or Call-ID that came in compact form,
and a short message whose content type came as `c:` was answered 200
and logged instead of taken (issue #2).
MatchHeaderLine now takes a line under the name it is asked for or
under that name's compact form: the ten RFC 3261 defines, and `o` for
Event (RFC 6665) and `x` for Session-Expires (RFC 4028), the two
extension headers imsd reads that have one. Every reader goes through
it — Header, Headers, the framer, DatagramMessage — so no reader
changes for it. It also takes white space between the name and the
colon, which the same grammar allows (25.1's HCOLON; 7.3.1's
"Subject : lunch") and the matcher refused. A value folded
onto further lines is still read by its first line.
One reader is changed for the rows it is now handed. ViaResponsePort
gives the port the top Via names, to which the answer to a request that
came in a datagram on a server port goes (RFC 3261 18.2.2), and none
where that Via carries `rport` (RFC 3581): the answer then goes back to
the port the request came from. It took `rport` for present where
those five letters, in lower case, stood anywhere behind the row's
first semicolon. Parameter names are case-insensitive (7.3.1), and a
request whose top Via came as `v: …;RPORT`, a row 1.2.0 passed over,
would have been answered to the port that Via names, its `rport` not
seen. ViaResponsePort now reads the Via's parameters one by one, each
to the next semicolon outside a quoted string, and `rport` is one by
its name, in any case: not those letters in a value or in a longer
name. Read so, the last parameter of the top value would run on into
the next value where a row holds a list — `rport, SIP/2.0/UDP …` is no
`rport`, and 1.2.0's search of the letters found it —, so the top value
is cut from the row first, at the first comma outside a quoted string.
With that cut a lower value's `rport` no longer counts for the top
one's, as it did in 1.2.0 for a list on one line.
Tested by the suites: tests/Sip pins each of the twelve forms through
Header, in lower and in upper case, and through Headers, `l:` through
the framer and DatagramMessage, 7.3.1's four spellings of the colon, and
ViaResponsePort's reading of a Via row, `rport` by its name and of a
list on one line the top value alone; tests/Engine drives an incoming
call from an INVITE in compact form, an outgoing call from responses in
compact form and a delivered MESSAGE in compact form through the
reducers; tests/Messages builds BuildResponse, BuildResponse200 and
BuildInviteResponse for a request in compact form. On the workstation's
loopback (the fp6 journal's 2026-11-20-issues-2-5 capture, server-ports,
at this commit and at the one before) a request on a server port whose
Via row is a list on one line with `rport` on its lower value is
answered from the client-port socket, the way to the port its top value
names, where the commit before answered it from the socket it had come
on. On the dev phone the binary of this range's last commit answered a
MESSAGE the phone sent to its own protected server port from the
client-port socket where its Via row was such a list, and from the
socket it had come on where its one Via came as `v: …;RPORT` (the
capture's phone-form6, inject-bench.log).
2026-11-22 17:35:11 +01:00
|
|
|
path, each Via row echoed and the To tagged unless it holds `tag=`
|
2026-11-23 08:39:47 +01:00
|
|
|
(RFC 3261 8.2.6.2); the 400 to a request whose datagram ended before its
|
|
|
|
|
body did (18.3); the UAS responses to an
|
messages: the SIP MESSAGE of SMS over IP, +g.3gpp.smsip behind a context flag
TS 24.341 5.3.1.2 has a submission travel as a SIP MESSAGE to the
service centre's PSI with the RP-DATA as an application/vnd.3gpp.sms
body, and 5.3.2.4 the delivery report as one to the IP-SM-GW with
In-Reply-To naming the delivered MESSAGE: BuildMessageSms builds both,
out of dialog over the registration's Route with the same
P-Preferred-Identity, P-Access-Network-Info and sec-agree headers as the
INVITE and the reg-event SUBSCRIBE, and no Contact (RFC 3428 4). The REGISTER Contact carries +g.3gpp.smsip only
when Context::smsOverIp says so — the tag routes terminating short
messages to imsd (the 2026-10-17 bench's mt-last-message-in.raw), which
at this commit still answers them 200 and logs them — after the ICSI.
BuildResponse builds a final for an inbound request outside a call (the
488 of 5.3.1.2 for a report that is not ours, or a 200); the shell does
not call it in this commit — it still answers a MESSAGE with
BuildResponse200.
tests/Messages pins the Contact with the tag, the MESSAGE's headers and
body bytes both ways, the absence of Contact, and the 488.
BuildResponse echoes each Via: line and the To, with a tag added unless
the To already holds `tag=` (RFC 3261 8.2.6.2 has a UAS add a tag to a
To that carries none); tests/Messages pins the 488 and a 200.
ScPsi gives the service centre's PSI from its international number
(5.3.1.2 NOTE 1: the SC's E.164 number as a SIP URI with user=phone, or
a tel URI; the home domain is imsd's choice); RuriFor's dialled-number guess, whose trunk 0 is Dutch, has no
place there. Pinned in tests/Messages.
2026-10-16 23:13:39 +02:00
|
|
|
inbound INVITE (100/180/200
|
|
|
|
|
and rejection finals); and the SIP MESSAGE that carries a short message's
|
|
|
|
|
RP-DATA to the service centre, or its RP-ACK/RP-ERROR back to the IP-SM-GW
|
|
|
|
|
(TS 24.341). Pure std C++.
|
2026-07-22 22:53:28 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
export module Imsd:Messages;
|
|
|
|
|
import std;
|
|
|
|
|
import :Util;
|
|
|
|
|
import :Aka;
|
ipsec, messages, imsd: the offered ESP ciphers are a list
imsd's Security-Client held one mechanism, its cipher the value of EALG,
aes-cbc when unset. TS 33.203 7.2 has the UE's first REGISTER carry "a
list of identifiers for the integrity and encryption algorithms, which
the UE supports", and imsd can install three ciphers: an offer of one
could not say so.
EALG is now a comma-separated list, offered in the order written: one
mechanism per cipher in a single Security-Client, each with the
REGISTER's SPIs and ports (7.2 NOTE 1, NOTE 2). The default stays
aes-cbc alone. A name imsd cannot install is ignored with a journal
line where it used to go out as written, `3des` is offered as
des-ede3-cbc, RFC 3329's name for it, a setting that names no cipher
gives the default, and the bring-up logs what is offered (`ESP ciphers
offered: …`).
An offer of more than one cipher needs a further rule for the 401's
list. O2 UK's P-CSCF lists six mechanisms and puts the SPIs on one: the
sha1 entry without a cipher for a null offer, the sha1 entry with
aes-cbc for an aes-cbc offer (2026-09-17, 2026-09-20). The second has
the higher q, so with both ciphers offered the highest-q offered entry
and the entry carrying the SPIs can be two entries. The entry carrying
the SPIs is therefore taken when it is the only such entry and one of
the offer; otherwise the highest-q offered entry, as before. With one
cipher offered the two name the same algorithms; they can differ in mod
or prot, and there the marked entry's are now taken, where 1.2.1 took
the highest-q entry's.
Tested: tests/Ipsec (the setting; the selection against two offered
ciphers) and tests/Messages (a Security-Client of two mechanisms); the
daemon of this commit against a stand-in P-CSCF on the workstation, with
one cipher offered and with two.
2026-11-23 10:08:04 +01:00
|
|
|
import :Ipsec;
|
2026-07-22 22:53:28 +02:00
|
|
|
import :Sip;
|
|
|
|
|
|
|
|
|
|
export namespace imsd::msg {
|
|
|
|
|
|
|
|
|
|
// Everything the builders need that is stable for the life of a
|
imsd: a resumed registration echoes the 401's Security-Server list from the state file
A warm resume rebuilt its Security-Verify from the kernel's SA: one
mechanism with hmac-sha-1-96 and aes-cbc written in, where RFC 3329
§2.3.1 has the client mirror the server's whole list. The fresh
registration kept the 401's list in memory, so a registration this
process made fresh echoed it; after a restart, the resumed
registration's re-REGISTER, its keepalives, its reg-event SUBSCRIBE and
its INVITEs carried the rebuilt entry until the next fresh registration.
KPN's one-entry answer to the default offer is the rebuilt entry byte
for byte; against a six-entry list (O2's) the mirror is broken, which
the RFC has the server answer with a 494, and a refused resume
registers fresh.
The fresh registration now stores the joined Security-Server value in the
state file (security_server, next to the challenge answer) and the resume
takes it from there, logging the mechanism count, when the entry
carrying its SPIs names the SA pair in the kernel: a file an attempt the
process did not live to record left behind (its SAs installed, its 200
not written) names another. A file from before this change holds none.
In both cases the resume falls back to the rebuilt entry, says which,
and the registration's next state write stores that entry, which is what
it echoes; the choice is Imsd:Ipsec's, pinned in tests/Ipsec with O2's
recorded list and KPN's. A value with a quote, a backslash or a newline in it is
written empty, which the resume treats as absent; RFC 3329's token
syntax produces none of these, a quoted-string extension parameter
could carry the first two.
Tested on the dev phone (KPN) with the build of this tree: a resume from
a state file the 0.3.11 daemon wrote logs the fallback and succeeds, so
do one from a file whose list names another SA pair and one whose list
is empty; two keepalives at a 120-s interval; a fresh registration
stores KPN's list byte-for-byte as the journal shows it received; a
restart resumes with the list from the file; an outgoing call.
2026-10-10 23:42:01 +02:00
|
|
|
// registration. Ports default to the fixed protected values; route and
|
|
|
|
|
// ppi are filled in from the REGISTER 200 OK, securityServer from the
|
|
|
|
|
// 401 (at a resume, from the state file).
|
2026-07-22 22:53:28 +02:00
|
|
|
struct Context {
|
|
|
|
|
imsd::aka::Identity id;
|
|
|
|
|
std::string local;
|
|
|
|
|
int portUc = imsd::util::PortUc;
|
|
|
|
|
int portUs = imsd::util::PortUs;
|
|
|
|
|
int initPort = 5060;
|
sip: UDP for the protected leg, TCP first with a fallback
imsd's protected leg (the second REGISTER and everything after it) was
TCP only. A P-CSCF that never answers the TCP connect on its protected
server port (O2 UK: the SYNs leave ESP-protected, nothing comes back)
left the unit looping with no way forward, although the SAs, the
listener sockets and the firewall rule already covered UDP.
The client flow now carries a transport. UDP binds the same protected
client port and connect()s the datagram socket to the P-CSCF's protected
server port, so the kernel delivers that peer's datagrams to it ahead of
the unconnected listener on the same port. One datagram is one message
(RFC 3261 18.3: a Content-Length that fits truncates, one that does not
fit discards the datagram, none means the rest of the datagram); a
receive error is logged and marks the flow dead; a message over the
single-packet ESP budget at the ims PDN's MTU is logged once per flow,
since the kernel fragments it and a P-CSCF may drop the fragments.
Every protected request's Via follows the transport; the challenge
stays UDP.
SIP_TRANSPORT selects the policy: auto (default) registers over TCP and,
after two unanswered connects, registers again over UDP (new challenge,
new SA pair) - but only on a phone whose state file does not record a
successful TCP registration, so an outage on a TCP carrier never becomes
a second initial REGISTER; a phone whose last registration was UDP goes
straight to UDP. tcp and udp force one. A connect refused locally
(EADDRNOTAVAIL: the 4-tuple still in TIME_WAIT from the previous flow)
keeps retrying and is not counted as silence; SO_ERROR results are
logged by name. The registration's transport is persisted so a warm
resume reconnects the same way, and the resume gives up after two
silences.
Two things the change exposed and fixes: an engine-fatal event exited 0
("exiting for systemd restart" with Restart=on-failure never firing),
and a fresh registration refused with the P-CSCF's fresh-SA throttle
(Security-Server spi-s=0) must not be retried by a 120-s restart loop,
since every attempt re-arms the ~20-min window - it is now waited out
in-process, growing on repeats, with D-Bus commands still served. A
resume the network refuses falls through to a fresh registration. A
retransmitted 200 OK to our INVITE is ACKed again and no longer starts
a second media leg.
Verified on KPN: resume over TCP after a binary swap, MT and MO calls
with media both ways, the throttle deferral and its self-recovery, the
UDP client path up to KPN dropping the datagram. The UDP success path
is a field test; SIP retransmission timers over UDP are not in this
change.
2026-09-17 14:58:46 +02:00
|
|
|
// Transport of the protected leg (REGISTER 2 onwards): the Via token
|
|
|
|
|
// of every request we send over it. TCP unless the engine fell back
|
|
|
|
|
// to, or was told to use, UDP.
|
|
|
|
|
std::string transport = "TCP";
|
ipsec, messages, imsd: the offered ESP ciphers are a list
imsd's Security-Client held one mechanism, its cipher the value of EALG,
aes-cbc when unset. TS 33.203 7.2 has the UE's first REGISTER carry "a
list of identifiers for the integrity and encryption algorithms, which
the UE supports", and imsd can install three ciphers: an offer of one
could not say so.
EALG is now a comma-separated list, offered in the order written: one
mechanism per cipher in a single Security-Client, each with the
REGISTER's SPIs and ports (7.2 NOTE 1, NOTE 2). The default stays
aes-cbc alone. A name imsd cannot install is ignored with a journal
line where it used to go out as written, `3des` is offered as
des-ede3-cbc, RFC 3329's name for it, a setting that names no cipher
gives the default, and the bring-up logs what is offered (`ESP ciphers
offered: …`).
An offer of more than one cipher needs a further rule for the 401's
list. O2 UK's P-CSCF lists six mechanisms and puts the SPIs on one: the
sha1 entry without a cipher for a null offer, the sha1 entry with
aes-cbc for an aes-cbc offer (2026-09-17, 2026-09-20). The second has
the higher q, so with both ciphers offered the highest-q offered entry
and the entry carrying the SPIs can be two entries. The entry carrying
the SPIs is therefore taken when it is the only such entry and one of
the offer; otherwise the highest-q offered entry, as before. With one
cipher offered the two name the same algorithms; they can differ in mod
or prot, and there the marked entry's are now taken, where 1.2.1 took
the highest-q entry's.
Tested: tests/Ipsec (the setting; the selection against two offered
ciphers) and tests/Messages (a Security-Client of two mechanisms); the
daemon of this commit against a stand-in P-CSCF on the workstation, with
one cipher offered and with two.
2026-11-23 10:08:04 +01:00
|
|
|
// The ESP ciphers Security-Client offers, in order (canonical names:
|
|
|
|
|
// imsd::ipsec::ParseEalgOffer).
|
|
|
|
|
std::vector<std::string> ealgOffer = imsd::ipsec::DefaultEalgOffer();
|
2026-07-22 22:53:28 +02:00
|
|
|
// RFC 7254 IMEI URN for +sip.instance (e.g. urn:gsma:imei:35999999-
|
|
|
|
|
// 000000-1); empty omits the parameter.
|
|
|
|
|
std::string instanceId;
|
|
|
|
|
// User-part of the registered-binding Contact URI. The stock modem
|
|
|
|
|
// registers a fresh random UUID here, NOT the IMSI (oracle capture
|
|
|
|
|
// 2026-07-21, journal/ims.md s53) — one of the two residual diffs vs
|
|
|
|
|
// our REGISTER while MT calls still CSFB. Empty falls back to the
|
|
|
|
|
// IMSI: the pre-oracle shape, which old state files resumed from and
|
|
|
|
|
// the byte-pinned tests encode.
|
|
|
|
|
std::string contactUser;
|
|
|
|
|
// User-Agent header value for REGISTER; the stock modem sends its
|
|
|
|
|
// build string (the other residual oracle diff). Empty omits.
|
|
|
|
|
std::string userAgent;
|
|
|
|
|
// From the successful REGISTER:
|
|
|
|
|
std::string route; // Route header value (Service-Route or fallback)
|
|
|
|
|
std::string ppi; // P-Preferred-Identity value
|
|
|
|
|
std::string aor; // default public identity (P-Associated-URI[0],
|
|
|
|
|
// sip: entry preferred) — the identity for
|
|
|
|
|
// self-targeted requests like reg-event
|
|
|
|
|
// SUBSCRIBE; the temporary (IMSI-derived)
|
|
|
|
|
// IMPU is barred for anything but REGISTER
|
imsd: a resumed registration echoes the 401's Security-Server list from the state file
A warm resume rebuilt its Security-Verify from the kernel's SA: one
mechanism with hmac-sha-1-96 and aes-cbc written in, where RFC 3329
§2.3.1 has the client mirror the server's whole list. The fresh
registration kept the 401's list in memory, so a registration this
process made fresh echoed it; after a restart, the resumed
registration's re-REGISTER, its keepalives, its reg-event SUBSCRIBE and
its INVITEs carried the rebuilt entry until the next fresh registration.
KPN's one-entry answer to the default offer is the rebuilt entry byte
for byte; against a six-entry list (O2's) the mirror is broken, which
the RFC has the server answer with a 494, and a refused resume
registers fresh.
The fresh registration now stores the joined Security-Server value in the
state file (security_server, next to the challenge answer) and the resume
takes it from there, logging the mechanism count, when the entry
carrying its SPIs names the SA pair in the kernel: a file an attempt the
process did not live to record left behind (its SAs installed, its 200
not written) names another. A file from before this change holds none.
In both cases the resume falls back to the rebuilt entry, says which,
and the registration's next state write stores that entry, which is what
it echoes; the choice is Imsd:Ipsec's, pinned in tests/Ipsec with O2's
recorded list and KPN's. A value with a quote, a backslash or a newline in it is
written empty, which the resume treats as absent; RFC 3329's token
syntax produces none of these, a quoted-string extension parameter
could carry the first two.
Tested on the dev phone (KPN) with the build of this tree: a resume from
a state file the 0.3.11 daemon wrote logs the fallback and succeeds, so
do one from a file whose list names another SA pair and one whose list
is empty; two keepalives at a 120-s interval; a fresh registration
stores KPN's list byte-for-byte as the journal shows it received; a
restart resumes with the list from the file; an outgoing call.
2026-10-10 23:42:01 +02:00
|
|
|
std::string securityServer; // Security-Verify value: the 401's Security-Server list (from the state file on a resume)
|
2026-07-22 22:53:28 +02:00
|
|
|
// P-Access-Network-Info value (imsd::aka::ParsePani from live mmcli
|
|
|
|
|
// location: real MCC/MNC/TAC/ECI, FDD). Empty omits the header —
|
|
|
|
|
// never fabricate one: the network reads it to learn which access
|
messages, imsd: P-Access-Network-Info in responses and in requests within a dialog
imsd named the serving cell in its REGISTERs, the reg-event SUBSCRIBE,
an INVITE and a MESSAGE. TS 24.229 5.1.2A.1.1 and 5.1.2A.2 have a UE
insert the header into a request within a dialog as well, and into a
response within a dialog, to a request for a dialog and to a standalone
method: not into a CANCEL nor into a response to one, and into an ACK
at its choice. A PRACK and a BYE carry it now, and every response imsd
builds but a 100 Trying, the 200 to a CANCEL and the transport's 400 to
a request cut short.
The server ports (the protected ports' TCP listeners and UDP sockets)
take a request from any source (AGENTS.md §12), and the header is the
phone's cell. What they send in answer keeps it only on the two paths
of the SA pair the shell has on record, and loses it anywhere else.
Tested: the suites, and the daemon of this commit on the workstation
against a stand-in network that asks it from both sides of those paths
(the fp6 journal's 2026-12-04-pani-in-dialog capture, bench/), and on
the dev phone on KPN, where its answers to a text's two requests and a
call's 180 and 200 carried the header inside the SA pair (the
journal's 2026-12-04-imsd-1.2.7-range-on-dev-phone capture).
2026-12-04 23:20:38 +01:00
|
|
|
// the UE is reachable on.
|
|
|
|
|
// It rides in every request but the ACKs and the CANCEL and in every
|
|
|
|
|
// response but a 100 Trying, a CANCEL's and BuildBadRequest's 400:
|
|
|
|
|
// by TS 24.229 5.1.2A.1.1 and 5.1.2A.2 for a request within a
|
|
|
|
|
// dialog and for the three kinds of response detail::AccessInfo
|
|
|
|
|
// names (the 100 and the 400 are left out by imsd's choice), by RFC
|
|
|
|
|
// 7315 5.7 for the ACKs and the CANCEL.
|
2026-07-22 22:53:28 +02:00
|
|
|
std::string panInfo;
|
messages: the SIP MESSAGE of SMS over IP, +g.3gpp.smsip behind a context flag
TS 24.341 5.3.1.2 has a submission travel as a SIP MESSAGE to the
service centre's PSI with the RP-DATA as an application/vnd.3gpp.sms
body, and 5.3.2.4 the delivery report as one to the IP-SM-GW with
In-Reply-To naming the delivered MESSAGE: BuildMessageSms builds both,
out of dialog over the registration's Route with the same
P-Preferred-Identity, P-Access-Network-Info and sec-agree headers as the
INVITE and the reg-event SUBSCRIBE, and no Contact (RFC 3428 4). The REGISTER Contact carries +g.3gpp.smsip only
when Context::smsOverIp says so — the tag routes terminating short
messages to imsd (the 2026-10-17 bench's mt-last-message-in.raw), which
at this commit still answers them 200 and logs them — after the ICSI.
BuildResponse builds a final for an inbound request outside a call (the
488 of 5.3.1.2 for a report that is not ours, or a 200); the shell does
not call it in this commit — it still answers a MESSAGE with
BuildResponse200.
tests/Messages pins the Contact with the tag, the MESSAGE's headers and
body bytes both ways, the absence of Contact, and the 488.
BuildResponse echoes each Via: line and the To, with a tag added unless
the To already holds `tag=` (RFC 3261 8.2.6.2 has a UAS add a tag to a
To that carries none); tests/Messages pins the 488 and a 200.
ScPsi gives the service centre's PSI from its international number
(5.3.1.2 NOTE 1: the SC's E.164 number as a SIP URI with user=phone, or
a tel URI; the home domain is imsd's choice); RuriFor's dialled-number guess, whose trunk 0 is Dutch, has no
place there. Pinned in tests/Messages.
2026-10-16 23:13:39 +02:00
|
|
|
// Advertise SMS over IP in the REGISTER Contact (+g.3gpp.smsip, TS
|
|
|
|
|
// 24.341 5.3.2.2): the network then delivers terminating short
|
|
|
|
|
// messages and submit reports to imsd as SIP MESSAGEs (a network that
|
|
|
|
|
// also has a NAS SMS path chooses by operator policy, 5.3.3.4.2 NOTE
|
|
|
|
|
// 2). Off unless the shell has SMS over IP handling to offer and turns
|
|
|
|
|
// it on from its configuration.
|
|
|
|
|
bool smsOverIp = false;
|
2026-07-22 22:53:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// A Via line: "Via: SIP/2.0/<transport> [local]:<port>;branch=z9hG4bK<b>;rport".
|
|
|
|
|
//
|
|
|
|
|
// Port rule (TS 24.229 5.1.1.4 / 33.203): every PROTECTED request carries
|
|
|
|
|
// the protected SERVER port (portUs) in Via and Contact — that is the
|
|
|
|
|
// port the network delivers terminating requests to (MT INVITE, NOTIFY,
|
|
|
|
|
// in-dialog requests toward us). The protected CLIENT port (portUc) is
|
|
|
|
|
// only the source port of the TCP connection we open; it never appears
|
|
|
|
|
// in headers. Getting this wrong is invisible on the originating path
|
|
|
|
|
// (TCP responses ride our own connection regardless) but silently kills
|
|
|
|
|
// ALL terminating delivery: the P-CSCF has no deliverable flow toward a
|
|
|
|
|
// Contact naming portUc, so MT calls fall back to CS and reg-event
|
|
|
|
|
// NOTIFYs vanish (found live 2026-07-20; the stock modem puts its
|
|
|
|
|
// port_us in both Via and Contact).
|
|
|
|
|
inline std::string ViaLine(const Context& c, std::string_view transport, int port, std::string_view branch) {
|
|
|
|
|
return std::format("Via: SIP/2.0/{} {};branch=z9hG4bK{};rport", transport, imsd::util::HostPort(c.local, port), branch);
|
|
|
|
|
}
|
|
|
|
|
|
ipsec, messages, imsd: the offered ESP ciphers are a list
imsd's Security-Client held one mechanism, its cipher the value of EALG,
aes-cbc when unset. TS 33.203 7.2 has the UE's first REGISTER carry "a
list of identifiers for the integrity and encryption algorithms, which
the UE supports", and imsd can install three ciphers: an offer of one
could not say so.
EALG is now a comma-separated list, offered in the order written: one
mechanism per cipher in a single Security-Client, each with the
REGISTER's SPIs and ports (7.2 NOTE 1, NOTE 2). The default stays
aes-cbc alone. A name imsd cannot install is ignored with a journal
line where it used to go out as written, `3des` is offered as
des-ede3-cbc, RFC 3329's name for it, a setting that names no cipher
gives the default, and the bring-up logs what is offered (`ESP ciphers
offered: …`).
An offer of more than one cipher needs a further rule for the 401's
list. O2 UK's P-CSCF lists six mechanisms and puts the SPIs on one: the
sha1 entry without a cipher for a null offer, the sha1 entry with
aes-cbc for an aes-cbc offer (2026-09-17, 2026-09-20). The second has
the higher q, so with both ciphers offered the highest-q offered entry
and the entry carrying the SPIs can be two entries. The entry carrying
the SPIs is therefore taken when it is the only such entry and one of
the offer; otherwise the highest-q offered entry, as before. With one
cipher offered the two name the same algorithms; they can differ in mod
or prot, and there the marked entry's are now taken, where 1.2.1 took
the highest-q entry's.
Tested: tests/Ipsec (the setting; the selection against two offered
ciphers) and tests/Messages (a Security-Client of two mechanisms); the
daemon of this commit against a stand-in P-CSCF on the workstation, with
one cipher offered and with two.
2026-11-23 10:08:04 +01:00
|
|
|
// Security-Client value: one mechanism per offered cipher, each with the
|
|
|
|
|
// SPIs and protected ports offered for the SA pairs this REGISTER may
|
|
|
|
|
// set up — the in-use ones on an initial registration, new SPIs and a
|
|
|
|
|
// new client port on a reregistration (TS 24.229 5.1.1.4.2 d; TS 33.203
|
|
|
|
|
// 7.4.0: the server port stays). One pair of SPIs and one of ports for
|
|
|
|
|
// the whole list (TS 33.203 7.2 NOTE 1, NOTE 2), the mechanisms
|
|
|
|
|
// comma-separated in one header field (Annex H).
|
engine, messages, util, imsd: the reregistration carries the last challenge answer and new SA parameters
The keepalive refresh and the resume's re-REGISTER carried an initial
registration's first-REGISTER Authorization — an empty nonce and
response — in the second REGISTER's frame, with a Security-Client
naming the SPIs and ports in use. TS 24.229 5.1.1.4.2 has a
reregistration carry the Authorization
of the last successful authentication — the last received nonce and
the last calculated response — and a Security-Client with the new
parameter values for the two SA pairs a re-authentication would set
up; TS 33.203 7.4.0 keeps the server ports and changes the client
ports. O2 UK refused the refresh of the old shape with 503 on every
occasion on record (alyx, 2026-09-24 to 10-08, a stationary phone
included) and accepted the fresh registrations that followed; KPN
accepted the old shape without a challenge.
The fresh registration now records the 401's nonce, our cnonce and
the response in the registration state (auth_nonce, auth_cnonce,
auth_response in the state file); a reregistration sends them
verbatim, mints new SPIs and proposes PortUcNext (45063) as the
client port with the server port unchanged. A registration resumed from a
state file written before this change has no answer: its
reregistrations carry the empty digest, as before, until its next
fresh registration, and the resume says so. A 401 to a reregistration asks
for re-authentication over temporary SAs on the proposed ports (TS
24.229 5.1.1.5.1), which imsd does not implement: the refresh throws
it as a refusal and RegRecovery makes the next step the fresh
registration, replacing the in-place answer over the old SAs.
Tested with the Messages and Engine suites and on the dev phone (KPN)
with a build of this commit's tree: a resume from the previous state
file, a fresh registration, a resume that sent the stored answer,
three refreshes at a 120-s interval, an outgoing call to a bench line
and an incoming call's ring.
2026-10-10 21:00:28 +02:00
|
|
|
inline std::string SecurityClient(const Context& c, std::uint32_t spiUc, std::uint32_t spiUs, int portUc, int portUs) {
|
ipsec, messages, imsd: the offered ESP ciphers are a list
imsd's Security-Client held one mechanism, its cipher the value of EALG,
aes-cbc when unset. TS 33.203 7.2 has the UE's first REGISTER carry "a
list of identifiers for the integrity and encryption algorithms, which
the UE supports", and imsd can install three ciphers: an offer of one
could not say so.
EALG is now a comma-separated list, offered in the order written: one
mechanism per cipher in a single Security-Client, each with the
REGISTER's SPIs and ports (7.2 NOTE 1, NOTE 2). The default stays
aes-cbc alone. A name imsd cannot install is ignored with a journal
line where it used to go out as written, `3des` is offered as
des-ede3-cbc, RFC 3329's name for it, a setting that names no cipher
gives the default, and the bring-up logs what is offered (`ESP ciphers
offered: …`).
An offer of more than one cipher needs a further rule for the 401's
list. O2 UK's P-CSCF lists six mechanisms and puts the SPIs on one: the
sha1 entry without a cipher for a null offer, the sha1 entry with
aes-cbc for an aes-cbc offer (2026-09-17, 2026-09-20). The second has
the higher q, so with both ciphers offered the highest-q offered entry
and the entry carrying the SPIs can be two entries. The entry carrying
the SPIs is therefore taken when it is the only such entry and one of
the offer; otherwise the highest-q offered entry, as before. With one
cipher offered the two name the same algorithms; they can differ in mod
or prot, and there the marked entry's are now taken, where 1.2.1 took
the highest-q entry's.
Tested: tests/Ipsec (the setting; the selection against two offered
ciphers) and tests/Messages (a Security-Client of two mechanisms); the
daemon of this commit against a stand-in P-CSCF on the workstation, with
one cipher offered and with two.
2026-11-23 10:08:04 +01:00
|
|
|
std::string v;
|
|
|
|
|
for (const std::string& ealg : c.ealgOffer)
|
|
|
|
|
v += std::format("{}ipsec-3gpp; alg=hmac-sha-1-96; ealg={}; " "spi-c={}; spi-s={}; port-c={}; port-s={}", v.empty() ? "" : ", ", ealg, spiUc, spiUs, portUc, portUs);
|
|
|
|
|
return std::format("Security-Client: {}", v);
|
engine, messages, util, imsd: the reregistration carries the last challenge answer and new SA parameters
The keepalive refresh and the resume's re-REGISTER carried an initial
registration's first-REGISTER Authorization — an empty nonce and
response — in the second REGISTER's frame, with a Security-Client
naming the SPIs and ports in use. TS 24.229 5.1.1.4.2 has a
reregistration carry the Authorization
of the last successful authentication — the last received nonce and
the last calculated response — and a Security-Client with the new
parameter values for the two SA pairs a re-authentication would set
up; TS 33.203 7.4.0 keeps the server ports and changes the client
ports. O2 UK refused the refresh of the old shape with 503 on every
occasion on record (alyx, 2026-09-24 to 10-08, a stationary phone
included) and accepted the fresh registrations that followed; KPN
accepted the old shape without a challenge.
The fresh registration now records the 401's nonce, our cnonce and
the response in the registration state (auth_nonce, auth_cnonce,
auth_response in the state file); a reregistration sends them
verbatim, mints new SPIs and proposes PortUcNext (45063) as the
client port with the server port unchanged. A registration resumed from a
state file written before this change has no answer: its
reregistrations carry the empty digest, as before, until its next
fresh registration, and the resume says so. A 401 to a reregistration asks
for re-authentication over temporary SAs on the proposed ports (TS
24.229 5.1.1.5.1), which imsd does not implement: the refresh throws
it as a refusal and RegRecovery makes the next step the fresh
registration, replacing the in-place answer over the old SAs.
Tested with the Messages and Engine suites and on the dev phone (KPN)
with a build of this commit's tree: a resume from the previous state
file, a fresh registration, a resume that sent the stored answer,
three refreshes at a 120-s interval, an outgoing call to a bench line
and an incoming call's ring.
2026-10-10 21:00:28 +02:00
|
|
|
}
|
2026-07-22 22:53:28 +02:00
|
|
|
inline std::string SecurityClient(const Context& c, std::uint32_t spiUc, std::uint32_t spiUs) {
|
engine, messages, util, imsd: the reregistration carries the last challenge answer and new SA parameters
The keepalive refresh and the resume's re-REGISTER carried an initial
registration's first-REGISTER Authorization — an empty nonce and
response — in the second REGISTER's frame, with a Security-Client
naming the SPIs and ports in use. TS 24.229 5.1.1.4.2 has a
reregistration carry the Authorization
of the last successful authentication — the last received nonce and
the last calculated response — and a Security-Client with the new
parameter values for the two SA pairs a re-authentication would set
up; TS 33.203 7.4.0 keeps the server ports and changes the client
ports. O2 UK refused the refresh of the old shape with 503 on every
occasion on record (alyx, 2026-09-24 to 10-08, a stationary phone
included) and accepted the fresh registrations that followed; KPN
accepted the old shape without a challenge.
The fresh registration now records the 401's nonce, our cnonce and
the response in the registration state (auth_nonce, auth_cnonce,
auth_response in the state file); a reregistration sends them
verbatim, mints new SPIs and proposes PortUcNext (45063) as the
client port with the server port unchanged. A registration resumed from a
state file written before this change has no answer: its
reregistrations carry the empty digest, as before, until its next
fresh registration, and the resume says so. A 401 to a reregistration asks
for re-authentication over temporary SAs on the proposed ports (TS
24.229 5.1.1.5.1), which imsd does not implement: the refresh throws
it as a refusal and RegRecovery makes the next step the fresh
registration, replacing the in-place answer over the old SAs.
Tested with the Messages and Engine suites and on the dev phone (KPN)
with a build of this commit's tree: a resume from the previous state
file, a fresh registration, a resume that sent the stored answer,
three refreshes at a 120-s interval, an outgoing call to a bench line
and an incoming call's ring.
2026-10-10 21:00:28 +02:00
|
|
|
return SecurityClient(c, spiUc, spiUs, c.portUc, c.portUs);
|
2026-07-22 22:53:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Authorization line with an empty (unchallenged) digest — the first
|
|
|
|
|
// REGISTER, before the 401 nonce is known.
|
|
|
|
|
inline std::string AuthEmpty(const Context& c) {
|
|
|
|
|
return std::format("Authorization: Digest username=\"{}\", realm=\"{}\", uri=\"{}\", " "nonce=\"\", response=\"\"", c.id.impi, c.id.domain, c.id.regUri);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Authorization line with a computed AKAv1-MD5 response.
|
|
|
|
|
inline std::string AuthAka(const Context& c, std::string_view nonce, std::string_view cnonce, std::string_view response) {
|
|
|
|
|
return std::format(
|
|
|
|
|
"Authorization: Digest username=\"{}\", realm=\"{}\", uri=\"{}\", "
|
|
|
|
|
"nonce=\"{}\", qop=auth, nc=00000001, cnonce=\"{}\", response=\"{}\", "
|
|
|
|
|
"algorithm=AKAv1-MD5",
|
|
|
|
|
c.id.impi, c.id.domain, c.id.regUri, nonce, cnonce, response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// REGISTER Contact media-feature tags. The MMTEL ICSI declares the
|
|
|
|
|
// binding VOICE-capable: terminating access-domain selection only routes
|
|
|
|
|
// incoming calls to contacts registered with it — without the tag the
|
|
|
|
|
// network diverts straight to voicemail (KPN, observed live 2026-07-20;
|
|
|
|
|
// the stock modem registers with exactly this ICSI). The URN colons are
|
|
|
|
|
// percent-encoded (TS 24.229 7.2A.8.2 feature-tag coding; RFC 3841
|
|
|
|
|
// matching compares the value as a string, so the encoding must match
|
|
|
|
|
// what the network selects on — the stock modem sends exactly
|
|
|
|
|
// urn%3Aurn-7%3A..., diag capture 2026-07-18). +sip.instance carries the
|
messages: the SIP MESSAGE of SMS over IP, +g.3gpp.smsip behind a context flag
TS 24.341 5.3.1.2 has a submission travel as a SIP MESSAGE to the
service centre's PSI with the RP-DATA as an application/vnd.3gpp.sms
body, and 5.3.2.4 the delivery report as one to the IP-SM-GW with
In-Reply-To naming the delivered MESSAGE: BuildMessageSms builds both,
out of dialog over the registration's Route with the same
P-Preferred-Identity, P-Access-Network-Info and sec-agree headers as the
INVITE and the reg-event SUBSCRIBE, and no Contact (RFC 3428 4). The REGISTER Contact carries +g.3gpp.smsip only
when Context::smsOverIp says so — the tag routes terminating short
messages to imsd (the 2026-10-17 bench's mt-last-message-in.raw), which
at this commit still answers them 200 and logs them — after the ICSI.
BuildResponse builds a final for an inbound request outside a call (the
488 of 5.3.1.2 for a report that is not ours, or a 200); the shell does
not call it in this commit — it still answers a MESSAGE with
BuildResponse200.
tests/Messages pins the Contact with the tag, the MESSAGE's headers and
body bytes both ways, the absence of Contact, and the 488.
BuildResponse echoes each Via: line and the To, with a tag added unless
the To already holds `tag=` (RFC 3261 8.2.6.2 has a UAS add a tag to a
To that carries none); tests/Messages pins the 488 and a 200.
ScPsi gives the service centre's PSI from its international number
(5.3.1.2 NOTE 1: the SC's E.164 number as a SIP URI with user=phone, or
a tel URI; the home domain is imsd's choice); RuriFor's dialled-number guess, whose trunk 0 is Dutch, has no
place there. Pinned in tests/Messages.
2026-10-16 23:13:39 +02:00
|
|
|
// IMEI URN when known. +g.3gpp.smsip only when the shell asks
|
|
|
|
|
// (Context::smsOverIp): it routes terminating SMS over IMS to imsd,
|
|
|
|
|
// which loses texts unless the daemon handles them — and exact tag
|
|
|
|
|
// parity including smsip + nw-init-ussi was tested live (2026-07-20,
|
|
|
|
|
// stored binding echo-verified) and did NOT change terminating
|
|
|
|
|
// access-domain selection for calls, so for calls the tags buy nothing.
|
|
|
|
|
// The tag follows the ICSI.
|
2026-07-22 22:53:28 +02:00
|
|
|
// The binding user-part every Contact we emit carries (see
|
|
|
|
|
// Context::contactUser).
|
|
|
|
|
inline std::string_view ContactUser(const Context& c) {
|
|
|
|
|
return c.contactUser.empty() ? std::string_view(c.id.imsi)
|
|
|
|
|
: std::string_view(c.contactUser);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline std::string ContactFeatures(const Context& c) {
|
|
|
|
|
std::string s;
|
|
|
|
|
if (!c.instanceId.empty()) s += std::format(";+sip.instance=\"<{}>\"", c.instanceId);
|
|
|
|
|
s += ";+g.3gpp.icsi-ref=\"urn%3Aurn-7%3A3gpp-service.ims.icsi.mmtel\"";
|
messages: the SIP MESSAGE of SMS over IP, +g.3gpp.smsip behind a context flag
TS 24.341 5.3.1.2 has a submission travel as a SIP MESSAGE to the
service centre's PSI with the RP-DATA as an application/vnd.3gpp.sms
body, and 5.3.2.4 the delivery report as one to the IP-SM-GW with
In-Reply-To naming the delivered MESSAGE: BuildMessageSms builds both,
out of dialog over the registration's Route with the same
P-Preferred-Identity, P-Access-Network-Info and sec-agree headers as the
INVITE and the reg-event SUBSCRIBE, and no Contact (RFC 3428 4). The REGISTER Contact carries +g.3gpp.smsip only
when Context::smsOverIp says so — the tag routes terminating short
messages to imsd (the 2026-10-17 bench's mt-last-message-in.raw), which
at this commit still answers them 200 and logs them — after the ICSI.
BuildResponse builds a final for an inbound request outside a call (the
488 of 5.3.1.2 for a report that is not ours, or a 200); the shell does
not call it in this commit — it still answers a MESSAGE with
BuildResponse200.
tests/Messages pins the Contact with the tag, the MESSAGE's headers and
body bytes both ways, the absence of Contact, and the 488.
BuildResponse echoes each Via: line and the To, with a tag added unless
the To already holds `tag=` (RFC 3261 8.2.6.2 has a UAS add a tag to a
To that carries none); tests/Messages pins the 488 and a 200.
ScPsi gives the service centre's PSI from its international number
(5.3.1.2 NOTE 1: the SC's E.164 number as a SIP URI with user=phone, or
a tel URI; the home domain is imsd's choice); RuriFor's dialled-number guess, whose trunk 0 is Dutch, has no
place there. Pinned in tests/Messages.
2026-10-16 23:13:39 +02:00
|
|
|
if (c.smsOverIp) s += ";+g.3gpp.smsip";
|
2026-07-22 22:53:28 +02:00
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
Emergency calling, stage 1: urn:service:sos with a digits fallback
Classify 112/911 (plus EMERGENCY_NUMBERS) at Dial. A classified call
INVITEs urn:service:sos over the existing registration and, on any
non-2xx final not caused by the user hanging up (including the
setup-timeout CANCEL paths), retries once as a plain INVITE of the
dialled digits — the pre-0.3.0 behavior, so classification can never
place a call worse than the status quo. The reverse edge: a 380
Alternative Service whose body carries the emergency indication
upgrades an unclassified call to the sos URN; a bare 380 stays an
error, since promoting an arbitrary redirect would put a
non-emergency call through to a PSAP. An emergency dial preempts an
in-progress call, and an answer racing a deadline-initiated CANCEL is
taken instead of BYE'd (user-initiated CANCEL races still BYE).
No emergency registration, no emergency PDN, no CS fallback, no
SIM-less calling, no AML — and no carrier has confirmed the sos path
end-to-end. The README warning states exactly that.
Assisted-by: Claude:claude-fable-5
2026-08-01 22:29:12 +02:00
|
|
|
// The service URN an emergency INVITE targets (RFC 5031); Request-URI and
|
|
|
|
|
// To both carry it (TS 24.229 5.1.6). No sos subtype: a dialled string
|
|
|
|
|
// does not say police/ambulance/fire.
|
|
|
|
|
inline constexpr std::string_view SosUrn = "urn:service:sos";
|
|
|
|
|
|
|
|
|
|
// Emergency-number classification. 112 and 911 are emergency numbers on
|
|
|
|
|
// every network (TS 22.101 §10.1.1), so they are compiled in; `extra`
|
|
|
|
|
// adds carrier- or lab-specific numbers (the EMERGENCY_NUMBERS list —
|
|
|
|
|
// e.g. a private test core's advertised short code). Exact match after
|
|
|
|
|
// stripping "1 1 2"-style separators; prefixed/suffixed strings ("0112",
|
|
|
|
|
// "1120") are ordinary numbers. SIM EF_ECC is not read yet.
|
|
|
|
|
inline bool IsEmergency(std::string_view target, std::span<const std::string> extra = {}) {
|
|
|
|
|
std::string t;
|
|
|
|
|
for (char ch : target)
|
|
|
|
|
if (ch != ' ' && ch != '-' && ch != '.') t.push_back(ch);
|
|
|
|
|
if (t == "112" || t == "911") return true;
|
|
|
|
|
for (const std::string& e : extra)
|
|
|
|
|
if (!e.empty() && t == e) return true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-22 22:53:28 +02:00
|
|
|
// Map a dialled string to a Request-URI (imscall.ruri_for): E.164/national
|
|
|
|
|
// numbers become user=phone SIP URIs at the home domain; a bare short code
|
|
|
|
|
// gets phone-context.
|
|
|
|
|
inline std::string RuriFor(std::string_view target, std::string_view domain) {
|
|
|
|
|
std::string t;
|
|
|
|
|
for (char ch : target)
|
|
|
|
|
if (ch != ' ') t.push_back(ch);
|
|
|
|
|
if (t.starts_with("+")) return std::format("sip:{}@{};user=phone", t, domain);
|
|
|
|
|
if (t.starts_with("00")) return std::format("sip:+{}@{};user=phone", t.substr(2), domain);
|
|
|
|
|
if (t.starts_with("0") && t.size() >= 9) return std::format("sip:+31{}@{};user=phone", t.substr(1), domain);
|
|
|
|
|
return std::format("sip:{};phone-context={}@{};user=phone", t, domain, domain);
|
|
|
|
|
}
|
|
|
|
|
|
messages: the SIP MESSAGE of SMS over IP, +g.3gpp.smsip behind a context flag
TS 24.341 5.3.1.2 has a submission travel as a SIP MESSAGE to the
service centre's PSI with the RP-DATA as an application/vnd.3gpp.sms
body, and 5.3.2.4 the delivery report as one to the IP-SM-GW with
In-Reply-To naming the delivered MESSAGE: BuildMessageSms builds both,
out of dialog over the registration's Route with the same
P-Preferred-Identity, P-Access-Network-Info and sec-agree headers as the
INVITE and the reg-event SUBSCRIBE, and no Contact (RFC 3428 4). The REGISTER Contact carries +g.3gpp.smsip only
when Context::smsOverIp says so — the tag routes terminating short
messages to imsd (the 2026-10-17 bench's mt-last-message-in.raw), which
at this commit still answers them 200 and logs them — after the ICSI.
BuildResponse builds a final for an inbound request outside a call (the
488 of 5.3.1.2 for a report that is not ours, or a 200); the shell does
not call it in this commit — it still answers a MESSAGE with
BuildResponse200.
tests/Messages pins the Contact with the tag, the MESSAGE's headers and
body bytes both ways, the absence of Contact, and the 488.
BuildResponse echoes each Via: line and the To, with a tag added unless
the To already holds `tag=` (RFC 3261 8.2.6.2 has a UAS add a tag to a
To that carries none); tests/Messages pins the 488 and a 200.
ScPsi gives the service centre's PSI from its international number
(5.3.1.2 NOTE 1: the SC's E.164 number as a SIP URI with user=phone, or
a tel URI; the home domain is imsd's choice); RuriFor's dialled-number guess, whose trunk 0 is Dutch, has no
place there. Pinned in tests/Messages.
2026-10-16 23:13:39 +02:00
|
|
|
// The service centre's PSI a submission is addressed to (TS 24.341
|
|
|
|
|
// 5.3.1.2 NOTE 1: the SC's E.164 number as a SIP URI with user=phone,
|
|
|
|
|
// or a tel URI; the home domain as its host is imsd's choice). Only an
|
|
|
|
|
// international number makes one — RuriFor's
|
|
|
|
|
// dialled-number guess (a trunk 0 is Dutch) has no place here; a
|
|
|
|
|
// service centre in another form is nullopt, and the caller says so.
|
|
|
|
|
inline std::optional<std::string> ScPsi(std::string_view e164, std::string_view domain) {
|
|
|
|
|
if (e164.size() < 2 || e164[0] != '+') return std::nullopt;
|
|
|
|
|
for (char ch : e164.substr(1))
|
|
|
|
|
if (ch < '0' || ch > '9') return std::nullopt;
|
|
|
|
|
return std::format("sip:{}@{};user=phone", e164, domain);
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-22 22:53:28 +02:00
|
|
|
namespace detail {
|
|
|
|
|
inline std::string Join(std::span<const std::string> lines) {
|
|
|
|
|
std::string s;
|
|
|
|
|
for (std::size_t i = 0; i < lines.size(); i++) {
|
|
|
|
|
s += lines[i];
|
|
|
|
|
s += "\r\n";
|
|
|
|
|
}
|
|
|
|
|
return s;
|
|
|
|
|
}
|
messages, imsd: P-Access-Network-Info in responses and in requests within a dialog
imsd named the serving cell in its REGISTERs, the reg-event SUBSCRIBE,
an INVITE and a MESSAGE. TS 24.229 5.1.2A.1.1 and 5.1.2A.2 have a UE
insert the header into a request within a dialog as well, and into a
response within a dialog, to a request for a dialog and to a standalone
method: not into a CANCEL nor into a response to one, and into an ACK
at its choice. A PRACK and a BYE carry it now, and every response imsd
builds but a 100 Trying, the 200 to a CANCEL and the transport's 400 to
a request cut short.
The server ports (the protected ports' TCP listeners and UDP sockets)
take a request from any source (AGENTS.md §12), and the header is the
phone's cell. What they send in answer keeps it only on the two paths
of the SA pair the shell has on record, and loses it anywhere else.
Tested: the suites, and the daemon of this commit on the workstation
against a stand-in network that asks it from both sides of those paths
(the fp6 journal's 2026-12-04-pani-in-dialog capture, bench/), and on
the dev phone on KPN, where its answers to a text's two requests and a
call's 180 and 200 carried the header inside the SA pair (the
journal's 2026-12-04-imsd-1.2.7-range-on-dev-phone capture).
2026-12-04 23:20:38 +01:00
|
|
|
|
|
|
|
|
// The P-Access-Network-Info line of a response with status `code`
|
|
|
|
|
// to `request`, where the serving cell is known: TS 24.229
|
|
|
|
|
// 5.1.2A.1.1 and 5.1.2A.2 have a UE insert the header into every
|
|
|
|
|
// response it sends within a dialog, to a request for a dialog and
|
|
|
|
|
// to a standalone method, those to a CANCEL excepted. A 100 Trying
|
|
|
|
|
// gets none, by imsd's choice: a stateful proxy never forwards it
|
|
|
|
|
// (RFC 3261 21.1.1; AGENTS.md §7.6).
|
|
|
|
|
inline void AccessInfo(std::vector<std::string>& l, const Context& c, std::string_view request, int code) {
|
|
|
|
|
if (c.panInfo.empty() || code == 100 || request.starts_with("CANCEL ")) return;
|
|
|
|
|
l.push_back(std::format("P-Access-Network-Info: {}", c.panInfo));
|
|
|
|
|
}
|
2026-07-22 22:53:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ---- REGISTER -------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
// The first, unprotected REGISTER (UDP from the init port, CSeq 1, empty
|
|
|
|
|
// digest, Supported: path). Triggers the 401 that carries the AKA nonce.
|
|
|
|
|
std::string BuildRegisterInitial(const Context& c, std::string_view callid, std::string_view ftag, std::string_view branch, std::uint32_t spiUc, std::uint32_t spiUs) {
|
|
|
|
|
std::vector<std::string> l = {
|
|
|
|
|
std::format("REGISTER {} SIP/2.0", c.id.regUri),
|
|
|
|
|
ViaLine(c, "UDP", c.initPort, branch),
|
|
|
|
|
"Max-Forwards: 70",
|
|
|
|
|
std::format("From: <{}>;tag={}", c.id.impu, ftag),
|
|
|
|
|
std::format("To: <{}>", c.id.impu),
|
|
|
|
|
std::format("Call-ID: {}", callid),
|
|
|
|
|
"CSeq: 1 REGISTER",
|
|
|
|
|
std::format("Contact: <sip:{}@{}>{};expires=600000", ContactUser(c), imsd::util::HostPort(c.local, c.initPort), ContactFeatures(c)),
|
|
|
|
|
"Allow: INVITE, ACK, BYE, CANCEL, OPTIONS, NOTIFY, PRACK, UPDATE, MESSAGE, REFER",
|
|
|
|
|
AuthEmpty(c),
|
|
|
|
|
"Require: sec-agree",
|
|
|
|
|
"Proxy-Require: sec-agree",
|
|
|
|
|
SecurityClient(c, spiUc, spiUs),
|
|
|
|
|
"Supported: sec-agree, path",
|
|
|
|
|
"Expires: 600000",
|
|
|
|
|
};
|
|
|
|
|
if (!c.panInfo.empty()) l.push_back(std::format("P-Access-Network-Info: {}", c.panInfo));
|
|
|
|
|
if (!c.userAgent.empty()) l.push_back(std::format("User-Agent: {}", c.userAgent));
|
|
|
|
|
l.push_back("Content-Length: 0");
|
|
|
|
|
return std::format("{}\r\n", detail::Join(l));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// A protected REGISTER over the IPsec/TCP flow. Always advertises
|
|
|
|
|
// Supported: sec-agree, path — RFC 3327: 'path' here is the UA's
|
|
|
|
|
// declaration that it is reachable via the Path the P-CSCF inserted,
|
|
|
|
|
// i.e. the route every terminating request (NOTIFY, MT INVITE) takes.
|
|
|
|
|
// The stock oracle sends it in BOTH REGISTERs (2026-07-21 capture,
|
|
|
|
|
// reg2 build at 19:30:44); an earlier reading that reg2 omits it was
|
|
|
|
|
// wrong and cost us all terminating delivery. `authLine` is a full
|
engine, messages, util, imsd: the reregistration carries the last challenge answer and new SA parameters
The keepalive refresh and the resume's re-REGISTER carried an initial
registration's first-REGISTER Authorization — an empty nonce and
response — in the second REGISTER's frame, with a Security-Client
naming the SPIs and ports in use. TS 24.229 5.1.1.4.2 has a
reregistration carry the Authorization
of the last successful authentication — the last received nonce and
the last calculated response — and a Security-Client with the new
parameter values for the two SA pairs a re-authentication would set
up; TS 33.203 7.4.0 keeps the server ports and changes the client
ports. O2 UK refused the refresh of the old shape with 503 on every
occasion on record (alyx, 2026-09-24 to 10-08, a stationary phone
included) and accepted the fresh registrations that followed; KPN
accepted the old shape without a challenge.
The fresh registration now records the 401's nonce, our cnonce and
the response in the registration state (auth_nonce, auth_cnonce,
auth_response in the state file); a reregistration sends them
verbatim, mints new SPIs and proposes PortUcNext (45063) as the
client port with the server port unchanged. A registration resumed from a
state file written before this change has no answer: its
reregistrations carry the empty digest, as before, until its next
fresh registration, and the resume says so. A 401 to a reregistration asks
for re-authentication over temporary SAs on the proposed ports (TS
24.229 5.1.1.5.1), which imsd does not implement: the refresh throws
it as a refusal and RegRecovery makes the next step the fresh
registration, replacing the in-place answer over the old SAs.
Tested with the Messages and Engine suites and on the dev phone (KPN)
with a build of this commit's tree: a resume from the previous state
file, a fresh registration, a resume that sent the stored answer,
three refreshes at a 120-s interval, an outgoing call to a bench line
and an incoming call's ring.
2026-10-10 21:00:28 +02:00
|
|
|
// Authorization line: AuthAka with the challenge's answer for the
|
|
|
|
|
// initial registration's second REGISTER and, verbatim, for every
|
|
|
|
|
// reregistration after it (TS 24.229 5.1.1.4.2 a: the last received
|
|
|
|
|
// nonce, the last calculated response); AuthEmpty for a registration
|
|
|
|
|
// resumed from a pre-0.3.11 state file, on every reregistration until
|
|
|
|
|
// its next fresh registration. A
|
|
|
|
|
// reregistration also passes NEW SPIs and the client port it proposes
|
|
|
|
|
// for a re-authentication's SA pairs (`portUc`, TS 33.203 7.4.0: the
|
|
|
|
|
// server port stays); 0 offers the in-use client port, the initial
|
|
|
|
|
// registration's case.
|
|
|
|
|
std::string BuildRegisterProtected(const Context& c, std::string_view callid, std::string_view ftag, std::string_view branch, int cseq, std::uint32_t spiUc, std::uint32_t spiUs, std::string_view authLine, int portUc = 0) {
|
2026-07-22 22:53:28 +02:00
|
|
|
std::vector<std::string> l = {
|
|
|
|
|
std::format("REGISTER {} SIP/2.0", c.id.regUri),
|
sip: UDP for the protected leg, TCP first with a fallback
imsd's protected leg (the second REGISTER and everything after it) was
TCP only. A P-CSCF that never answers the TCP connect on its protected
server port (O2 UK: the SYNs leave ESP-protected, nothing comes back)
left the unit looping with no way forward, although the SAs, the
listener sockets and the firewall rule already covered UDP.
The client flow now carries a transport. UDP binds the same protected
client port and connect()s the datagram socket to the P-CSCF's protected
server port, so the kernel delivers that peer's datagrams to it ahead of
the unconnected listener on the same port. One datagram is one message
(RFC 3261 18.3: a Content-Length that fits truncates, one that does not
fit discards the datagram, none means the rest of the datagram); a
receive error is logged and marks the flow dead; a message over the
single-packet ESP budget at the ims PDN's MTU is logged once per flow,
since the kernel fragments it and a P-CSCF may drop the fragments.
Every protected request's Via follows the transport; the challenge
stays UDP.
SIP_TRANSPORT selects the policy: auto (default) registers over TCP and,
after two unanswered connects, registers again over UDP (new challenge,
new SA pair) - but only on a phone whose state file does not record a
successful TCP registration, so an outage on a TCP carrier never becomes
a second initial REGISTER; a phone whose last registration was UDP goes
straight to UDP. tcp and udp force one. A connect refused locally
(EADDRNOTAVAIL: the 4-tuple still in TIME_WAIT from the previous flow)
keeps retrying and is not counted as silence; SO_ERROR results are
logged by name. The registration's transport is persisted so a warm
resume reconnects the same way, and the resume gives up after two
silences.
Two things the change exposed and fixes: an engine-fatal event exited 0
("exiting for systemd restart" with Restart=on-failure never firing),
and a fresh registration refused with the P-CSCF's fresh-SA throttle
(Security-Server spi-s=0) must not be retried by a 120-s restart loop,
since every attempt re-arms the ~20-min window - it is now waited out
in-process, growing on repeats, with D-Bus commands still served. A
resume the network refuses falls through to a fresh registration. A
retransmitted 200 OK to our INVITE is ACKed again and no longer starts
a second media leg.
Verified on KPN: resume over TCP after a binary swap, MT and MO calls
with media both ways, the throttle deferral and its self-recovery, the
UDP client path up to KPN dropping the datagram. The UDP success path
is a field test; SIP retransmission timers over UDP are not in this
change.
2026-09-17 14:58:46 +02:00
|
|
|
ViaLine(c, c.transport, c.portUs, branch),
|
2026-07-22 22:53:28 +02:00
|
|
|
"Max-Forwards: 70",
|
|
|
|
|
std::format("From: <{}>;tag={}", c.id.impu, ftag),
|
|
|
|
|
std::format("To: <{}>", c.id.impu),
|
|
|
|
|
std::format("Call-ID: {}", callid),
|
|
|
|
|
std::format("CSeq: {} REGISTER", cseq),
|
|
|
|
|
std::format("Contact: <sip:{}@{}>{};expires=600000", ContactUser(c), imsd::util::HostPort(c.local, c.portUs), ContactFeatures(c)),
|
|
|
|
|
"Allow: INVITE, ACK, BYE, CANCEL, OPTIONS, NOTIFY, PRACK, UPDATE, MESSAGE, REFER",
|
|
|
|
|
std::string(authLine),
|
|
|
|
|
"Require: sec-agree",
|
|
|
|
|
"Proxy-Require: sec-agree",
|
engine, messages, util, imsd: the reregistration carries the last challenge answer and new SA parameters
The keepalive refresh and the resume's re-REGISTER carried an initial
registration's first-REGISTER Authorization — an empty nonce and
response — in the second REGISTER's frame, with a Security-Client
naming the SPIs and ports in use. TS 24.229 5.1.1.4.2 has a
reregistration carry the Authorization
of the last successful authentication — the last received nonce and
the last calculated response — and a Security-Client with the new
parameter values for the two SA pairs a re-authentication would set
up; TS 33.203 7.4.0 keeps the server ports and changes the client
ports. O2 UK refused the refresh of the old shape with 503 on every
occasion on record (alyx, 2026-09-24 to 10-08, a stationary phone
included) and accepted the fresh registrations that followed; KPN
accepted the old shape without a challenge.
The fresh registration now records the 401's nonce, our cnonce and
the response in the registration state (auth_nonce, auth_cnonce,
auth_response in the state file); a reregistration sends them
verbatim, mints new SPIs and proposes PortUcNext (45063) as the
client port with the server port unchanged. A registration resumed from a
state file written before this change has no answer: its
reregistrations carry the empty digest, as before, until its next
fresh registration, and the resume says so. A 401 to a reregistration asks
for re-authentication over temporary SAs on the proposed ports (TS
24.229 5.1.1.5.1), which imsd does not implement: the refresh throws
it as a refusal and RegRecovery makes the next step the fresh
registration, replacing the in-place answer over the old SAs.
Tested with the Messages and Engine suites and on the dev phone (KPN)
with a build of this commit's tree: a resume from the previous state
file, a fresh registration, a resume that sent the stored answer,
three refreshes at a 120-s interval, an outgoing call to a bench line
and an incoming call's ring.
2026-10-10 21:00:28 +02:00
|
|
|
SecurityClient(c, spiUc, spiUs, portUc ? portUc : c.portUc, c.portUs),
|
2026-07-22 22:53:28 +02:00
|
|
|
std::format("Security-Verify: {}", c.securityServer),
|
|
|
|
|
};
|
|
|
|
|
l.push_back("Supported: sec-agree, path");
|
|
|
|
|
l.push_back("Expires: 600000");
|
|
|
|
|
if (!c.panInfo.empty()) l.push_back(std::format("P-Access-Network-Info: {}", c.panInfo));
|
|
|
|
|
if (!c.userAgent.empty()) l.push_back(std::format("User-Agent: {}", c.userAgent));
|
|
|
|
|
l.push_back("Content-Length: 0");
|
|
|
|
|
return std::format("{}\r\n", detail::Join(l));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SUBSCRIBE to the registration event package (RFC 3680) for our own
|
|
|
|
|
// AOR — TS 24.229 5.1.1.3 requires it after registration. The immediate
|
|
|
|
|
// NOTIFY carries application/reginfo+xml with every binding the S-CSCF
|
|
|
|
|
// holds for the implicit registration set, feature tags as stored — the
|
|
|
|
|
// network's own view of what terminating routing selects on. Same
|
|
|
|
|
// sec-agree/route shape as the other protected in-flow requests; a new
|
|
|
|
|
// dialog (fresh Call-ID, CSeq 1) per subscription.
|
|
|
|
|
std::string BuildSubscribeReg(const Context& c, std::string_view callid, std::string_view ftag, std::string_view branch) {
|
|
|
|
|
std::string_view aor = c.aor.empty() ? std::string_view(c.id.impu)
|
|
|
|
|
: std::string_view(c.aor);
|
|
|
|
|
std::vector<std::string> l = {
|
|
|
|
|
std::format("SUBSCRIBE {} SIP/2.0", aor),
|
sip: UDP for the protected leg, TCP first with a fallback
imsd's protected leg (the second REGISTER and everything after it) was
TCP only. A P-CSCF that never answers the TCP connect on its protected
server port (O2 UK: the SYNs leave ESP-protected, nothing comes back)
left the unit looping with no way forward, although the SAs, the
listener sockets and the firewall rule already covered UDP.
The client flow now carries a transport. UDP binds the same protected
client port and connect()s the datagram socket to the P-CSCF's protected
server port, so the kernel delivers that peer's datagrams to it ahead of
the unconnected listener on the same port. One datagram is one message
(RFC 3261 18.3: a Content-Length that fits truncates, one that does not
fit discards the datagram, none means the rest of the datagram); a
receive error is logged and marks the flow dead; a message over the
single-packet ESP budget at the ims PDN's MTU is logged once per flow,
since the kernel fragments it and a P-CSCF may drop the fragments.
Every protected request's Via follows the transport; the challenge
stays UDP.
SIP_TRANSPORT selects the policy: auto (default) registers over TCP and,
after two unanswered connects, registers again over UDP (new challenge,
new SA pair) - but only on a phone whose state file does not record a
successful TCP registration, so an outage on a TCP carrier never becomes
a second initial REGISTER; a phone whose last registration was UDP goes
straight to UDP. tcp and udp force one. A connect refused locally
(EADDRNOTAVAIL: the 4-tuple still in TIME_WAIT from the previous flow)
keeps retrying and is not counted as silence; SO_ERROR results are
logged by name. The registration's transport is persisted so a warm
resume reconnects the same way, and the resume gives up after two
silences.
Two things the change exposed and fixes: an engine-fatal event exited 0
("exiting for systemd restart" with Restart=on-failure never firing),
and a fresh registration refused with the P-CSCF's fresh-SA throttle
(Security-Server spi-s=0) must not be retried by a 120-s restart loop,
since every attempt re-arms the ~20-min window - it is now waited out
in-process, growing on repeats, with D-Bus commands still served. A
resume the network refuses falls through to a fresh registration. A
retransmitted 200 OK to our INVITE is ACKed again and no longer starts
a second media leg.
Verified on KPN: resume over TCP after a binary swap, MT and MO calls
with media both ways, the throttle deferral and its self-recovery, the
UDP client path up to KPN dropping the datagram. The UDP success path
is a field test; SIP retransmission timers over UDP are not in this
change.
2026-09-17 14:58:46 +02:00
|
|
|
ViaLine(c, c.transport, c.portUs, branch),
|
2026-07-22 22:53:28 +02:00
|
|
|
"Max-Forwards: 70",
|
|
|
|
|
std::format("Route: {}", c.route),
|
|
|
|
|
std::format("From: <{}>;tag={}", aor, ftag),
|
|
|
|
|
std::format("To: <{}>", aor),
|
|
|
|
|
std::format("Call-ID: {}", callid),
|
|
|
|
|
"CSeq: 1 SUBSCRIBE",
|
|
|
|
|
std::format("Contact: <sip:{}@{}>", ContactUser(c), imsd::util::HostPort(c.local, c.portUs)),
|
|
|
|
|
"Event: reg",
|
|
|
|
|
"Accept: application/reginfo+xml",
|
|
|
|
|
"Expires: 600000",
|
|
|
|
|
};
|
|
|
|
|
// P-Preferred-Identity is a hint (RFC 3325) — omitted when no public
|
|
|
|
|
// identity has been learned yet; the network asserts one regardless.
|
|
|
|
|
if (!c.ppi.empty()) l.push_back(std::format("P-Preferred-Identity: <{}>", c.ppi));
|
|
|
|
|
if (!c.panInfo.empty()) l.push_back(std::format("P-Access-Network-Info: {}", c.panInfo));
|
|
|
|
|
l.push_back(std::format("Security-Verify: {}", c.securityServer));
|
|
|
|
|
l.push_back("Proxy-Require: sec-agree");
|
|
|
|
|
l.push_back("Require: sec-agree");
|
|
|
|
|
l.push_back("Content-Length: 0");
|
|
|
|
|
return std::format("{}\r\n", detail::Join(l));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ---- call dialog ----------------------------------------------------
|
|
|
|
|
|
2026-12-01 15:51:21 +01:00
|
|
|
// A dialog as the engine keeps it.
|
2026-07-22 22:53:28 +02:00
|
|
|
// For an outgoing call itag is our From tag and dialogTo is learned from
|
|
|
|
|
// the responses; for an incoming call itag is the To tag we mint,
|
|
|
|
|
// dialogTo is the caller's From, and both feed BuildInDialog identically
|
|
|
|
|
// (our requests always put OUR tag in From and the remote's in To).
|
|
|
|
|
struct Dialog {
|
|
|
|
|
std::string ruri;
|
|
|
|
|
std::string callid;
|
|
|
|
|
std::string itag; // our (local) dialog tag
|
|
|
|
|
std::string invBranch; // INVITE Via branch (CANCEL reuses it)
|
|
|
|
|
std::optional<std::string> dialogTo; // remote To/From incl. tag, once known
|
|
|
|
|
std::vector<std::string> recordRoute; // route set, UAC order (see DialogRoute)
|
|
|
|
|
std::optional<std::string> remoteTarget; // remote Contact URI
|
messages: sign our requests with the public identity, not the IMSI IMPU
Every request we originated — INVITE, CANCEL, both ACKs, in-dialog BYE and
friends — put the IMSI-derived temporary IMPU in From. 3GPP allows that
identity in REGISTER only; the same lesson was learned for the reg-event
SUBSCRIBE (480) and never carried to calls. Most P-CSCFs overwrite From
and hid it; a Telia node did not, and a reporter's IMSI appeared on the
callee's screen (field report 2026-09-01). A strict P-CSCF may reject the
INVITE outright.
CallerId(): the registered sip: public identity (P-Associated-URI), else
the tel: one, and the temporary IMPU only before either is learned. One
helper feeds all five builders, so a dialog's From never drifts. As UAS the
dialog's local URI is the INVITE's To (RFC 3261 12.2.1.1), stored on the
Dialog, so an incoming call's BYE is signed the way the network addressed
us. The identity is persisted in the state file and restored on warm
resume, so a call placed before the refresh 200 re-learns it cannot fall
back. DUMP_SIP now also writes the last outgoing INVITE
(imsd-invite-out.raw): the one request a field log could never show.
The byte-pinned INVITE fixture moves to the tel: identity its test context
knows; new scenarios cover the sip: identity, the tel: fallback, the
pre-learning case and the UAS BYE.
Bench-verified on KPN 2026-09-08: outgoing INVITE From is the registered
sip: identity, call accepted and carried; caller ID at the far end
unchanged.
2026-09-08 20:37:47 +02:00
|
|
|
std::string localUri; // our URI in this dialog: the UAS takes the
|
|
|
|
|
// INVITE's To URI; empty = CallerId(c) (UAC)
|
2026-12-01 15:51:21 +01:00
|
|
|
int invCseq = 1; // our INVITE's sequence number: its CANCEL
|
|
|
|
|
// and its ACK carry it, a PRACK's RAck
|
|
|
|
|
// names it
|
|
|
|
|
int sessionExpires = 1800; // the session interval our INVITE asks for
|
|
|
|
|
// (RFC 4028 4), in seconds
|
|
|
|
|
int minSe = 90; // the Min-SE our INVITE carries (RFC 4028 5)
|
2026-07-22 22:53:28 +02:00
|
|
|
};
|
|
|
|
|
|
messages: sign our requests with the public identity, not the IMSI IMPU
Every request we originated — INVITE, CANCEL, both ACKs, in-dialog BYE and
friends — put the IMSI-derived temporary IMPU in From. 3GPP allows that
identity in REGISTER only; the same lesson was learned for the reg-event
SUBSCRIBE (480) and never carried to calls. Most P-CSCFs overwrite From
and hid it; a Telia node did not, and a reporter's IMSI appeared on the
callee's screen (field report 2026-09-01). A strict P-CSCF may reject the
INVITE outright.
CallerId(): the registered sip: public identity (P-Associated-URI), else
the tel: one, and the temporary IMPU only before either is learned. One
helper feeds all five builders, so a dialog's From never drifts. As UAS the
dialog's local URI is the INVITE's To (RFC 3261 12.2.1.1), stored on the
Dialog, so an incoming call's BYE is signed the way the network addressed
us. The identity is persisted in the state file and restored on warm
resume, so a call placed before the refresh 200 re-learns it cannot fall
back. DUMP_SIP now also writes the last outgoing INVITE
(imsd-invite-out.raw): the one request a field log could never show.
The byte-pinned INVITE fixture moves to the tel: identity its test context
knows; new scenarios cover the sip: identity, the tel: fallback, the
pre-learning case and the UAS BYE.
Bench-verified on KPN 2026-09-08: outgoing INVITE From is the registered
sip: identity, call accepted and carried; caller ID at the far end
unchanged.
2026-09-08 20:37:47 +02:00
|
|
|
// The identity we sign our own requests with: the registered public
|
|
|
|
|
// identity (P-Associated-URI sip: entry), else the tel: one, and the
|
imsd: with an identity read from the card's ISIM, a fresh registration is made under it and the ISIM is asked for the answer to the challenge
imsd registered the identity TS 23.003 derives from the IMSI on every
card and authenticated in the USIM. TS 23.003 has a UE derive its home
domain and a temporary public identity "if there is no ISIM
application" (13.2, 13.4B); TS 24.229 5.1.1.1A names the ISIM as
holding the parameters a registration starts from and says "The ISIM
shall always be used for authentication to the IM CN subsystem, if it
is present"; GSMA IR.92 2.2.1 has the UE register the public identity
of EF_IMPU's first record.
A US card on AT&T's network (operator name "Dark Star") holds names of
the operator's own: <IMSI>@private.att.net, one.att.net and
sip:<IMSI>@one.att.net (a read-only probe of the card, 2026-11-23).
imsd 1.1.0's first REGISTER there, with the derived names, was
answered 480 Temporarily Unavailable (2026-11-09); TS 24.229 5.3.1.3
has an I-CSCF answer 480 when the HSS lookup "cannot be completed".
This daemon has not sent that network the card's own names.
Unless ISIM=0 is set, a bring-up reads the ISIM application of the
USIM's slot, in whatever state the modem lists it, over a logical
channel: EF_IMPI, EF_DOMAIN and record 1 of EF_IMPU. When the three
make an identity it is the card's, and the ISIM is asked for the answer
to the challenge; an ISIM that gives no result is followed by the same
challenge to the card's USIM, where 1.2.2 sent every challenge.
Without an ISIM, with ISIM=0, or when the read or the check of the
three fails, the derived names and the USIM stand as before. An
`identity:` line in the journal says which and why. So with ISIM=0,
with an ISIM that gives no identity and behind one that gives no result
imsd asks the USIM where 5.1.1.1A has the ISIM used.
A fresh registration is made under the card's identity. A resumed one
keeps the identity it was made under until its next fresh registration,
as it keeps its P-CSCF and its address: the state file records the
three names, and a file without them is taken for one made under the
derived ones. Otherwise a card whose ISIM holds other names, on a
network that took the derived ones as well, would have its standing
registration refreshed under new names at the first start after the
upgrade. An ISIM that gave no identity at the bring-up
is read again at each recovery by a fresh registration later in the
same run, until it gives one: such a registration would else go out
under the derived names. A state file whose three names are no identity
imsd can register is no registration to resume, whatever transport the
file names.
Calls and texts follow the registration's identity. Its home domain is
what RuriFor takes for the address of a dialled number, an outgoing
call's Request-URI and To (the host, and the phone-context where it
writes one), and what ScPsi takes for the service centre's address of
an SMS submission; while no P-Associated-URI has named another, its
public identity is the one the reg-event SUBSCRIBE names and CallerId
gives. Under an identity read from an ISIM these are the ISIM's.
The read and the AKA run share one channel helper (UimChannel), which
closes its channel on every way out: 1.2.2 left the AKA's channel open
when the card's answer was the single byte 61.
Tested: the suites; the daemon of this commit on the workstation, its
registration against a stand-in for qmicli and a stand-in network that
answers a first REGISTER of any names but its subscriber's 480 and
tells the two applications' answers to the challenge apart (the fp6
journal's 2026-11-23-isim-identity capture): 46 scenarios, among them
an ISIM with names of the operator's own, one with the derived names,
ISIM=0 on the first (480, as on 2026-11-09), an ISIM that cannot be
read or does not answer the challenge, the resume by this daemon of a
registration imsd 1.2.2 made under the derived names on a card whose
ISIM holds others, and a fresh registration in a run that resumed. Not
run on a phone yet, and on no network.
2026-11-23 19:32:58 +01:00
|
|
|
// identity's own (an ISIM's first, or the IMSI-derived temporary IMPU)
|
|
|
|
|
// only before either is learned. 3GPP allows
|
messages: sign our requests with the public identity, not the IMSI IMPU
Every request we originated — INVITE, CANCEL, both ACKs, in-dialog BYE and
friends — put the IMSI-derived temporary IMPU in From. 3GPP allows that
identity in REGISTER only; the same lesson was learned for the reg-event
SUBSCRIBE (480) and never carried to calls. Most P-CSCFs overwrite From
and hid it; a Telia node did not, and a reporter's IMSI appeared on the
callee's screen (field report 2026-09-01). A strict P-CSCF may reject the
INVITE outright.
CallerId(): the registered sip: public identity (P-Associated-URI), else
the tel: one, and the temporary IMPU only before either is learned. One
helper feeds all five builders, so a dialog's From never drifts. As UAS the
dialog's local URI is the INVITE's To (RFC 3261 12.2.1.1), stored on the
Dialog, so an incoming call's BYE is signed the way the network addressed
us. The identity is persisted in the state file and restored on warm
resume, so a call placed before the refresh 200 re-learns it cannot fall
back. DUMP_SIP now also writes the last outgoing INVITE
(imsd-invite-out.raw): the one request a field log could never show.
The byte-pinned INVITE fixture moves to the tel: identity its test context
knows; new scenarios cover the sip: identity, the tel: fallback, the
pre-learning case and the UAS BYE.
Bench-verified on KPN 2026-09-08: outgoing INVITE From is the registered
sip: identity, call accepted and carried; caller ID at the far end
unchanged.
2026-09-08 20:37:47 +02:00
|
|
|
// the temporary IMPU in REGISTER alone; a P-CSCF that does not overwrite
|
|
|
|
|
// From shows it to the callee (Telia, 2026-09-01: the reporter's IMSI on
|
|
|
|
|
// the far phone) and a strict one may reject the request.
|
|
|
|
|
inline std::string_view CallerId(const Context& c) {
|
|
|
|
|
if (!c.aor.empty()) return c.aor;
|
|
|
|
|
if (!c.ppi.empty()) return c.ppi;
|
|
|
|
|
return c.id.impu;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// From URI of a request inside a dialog (RFC 3261 12.2.1.1: the dialog's
|
|
|
|
|
// local URI — as UAS that is the INVITE's To, as UAC our caller identity).
|
|
|
|
|
inline std::string_view FromUri(const Context& c, const Dialog& d) {
|
|
|
|
|
return d.localUri.empty() ? CallerId(c) : std::string_view(d.localUri);
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-22 22:53:28 +02:00
|
|
|
// Dialog route set: the reversed Record-Route, or the registration Route
|
|
|
|
|
// when none was recorded (imsd._dialog_route).
|
|
|
|
|
inline std::string DialogRoute(const Context& c, const Dialog& d) {
|
|
|
|
|
if (d.recordRoute.empty()) return c.route;
|
|
|
|
|
std::string s;
|
|
|
|
|
for (std::size_t i = d.recordRoute.size(); i-- > 0;) {
|
|
|
|
|
s += d.recordRoute[i];
|
|
|
|
|
if (i != 0) s += ", ";
|
|
|
|
|
}
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// INVITE with an AMR-WB SDP offer (passed in, built by Imsd:Sdp). MMTEL
|
2026-12-01 15:51:21 +01:00
|
|
|
// ICSI feature tags + sec-agree + session timers, as imsd._send_invite; the
|
|
|
|
|
// sequence number, the session interval and its minimum are the dialog's.
|
2026-07-22 22:53:28 +02:00
|
|
|
std::string BuildInvite(const Context& c, const Dialog& d, std::string_view sdp, bool precond) {
|
|
|
|
|
std::string supported = precond ? "100rel, timer, precondition"
|
|
|
|
|
: "100rel, timer";
|
|
|
|
|
std::vector<std::string> l = {
|
|
|
|
|
std::format("INVITE {} SIP/2.0", d.ruri),
|
sip: UDP for the protected leg, TCP first with a fallback
imsd's protected leg (the second REGISTER and everything after it) was
TCP only. A P-CSCF that never answers the TCP connect on its protected
server port (O2 UK: the SYNs leave ESP-protected, nothing comes back)
left the unit looping with no way forward, although the SAs, the
listener sockets and the firewall rule already covered UDP.
The client flow now carries a transport. UDP binds the same protected
client port and connect()s the datagram socket to the P-CSCF's protected
server port, so the kernel delivers that peer's datagrams to it ahead of
the unconnected listener on the same port. One datagram is one message
(RFC 3261 18.3: a Content-Length that fits truncates, one that does not
fit discards the datagram, none means the rest of the datagram); a
receive error is logged and marks the flow dead; a message over the
single-packet ESP budget at the ims PDN's MTU is logged once per flow,
since the kernel fragments it and a P-CSCF may drop the fragments.
Every protected request's Via follows the transport; the challenge
stays UDP.
SIP_TRANSPORT selects the policy: auto (default) registers over TCP and,
after two unanswered connects, registers again over UDP (new challenge,
new SA pair) - but only on a phone whose state file does not record a
successful TCP registration, so an outage on a TCP carrier never becomes
a second initial REGISTER; a phone whose last registration was UDP goes
straight to UDP. tcp and udp force one. A connect refused locally
(EADDRNOTAVAIL: the 4-tuple still in TIME_WAIT from the previous flow)
keeps retrying and is not counted as silence; SO_ERROR results are
logged by name. The registration's transport is persisted so a warm
resume reconnects the same way, and the resume gives up after two
silences.
Two things the change exposed and fixes: an engine-fatal event exited 0
("exiting for systemd restart" with Restart=on-failure never firing),
and a fresh registration refused with the P-CSCF's fresh-SA throttle
(Security-Server spi-s=0) must not be retried by a 120-s restart loop,
since every attempt re-arms the ~20-min window - it is now waited out
in-process, growing on repeats, with D-Bus commands still served. A
resume the network refuses falls through to a fresh registration. A
retransmitted 200 OK to our INVITE is ACKed again and no longer starts
a second media leg.
Verified on KPN: resume over TCP after a binary swap, MT and MO calls
with media both ways, the throttle deferral and its self-recovery, the
UDP client path up to KPN dropping the datagram. The UDP success path
is a field test; SIP retransmission timers over UDP are not in this
change.
2026-09-17 14:58:46 +02:00
|
|
|
ViaLine(c, c.transport, c.portUs, d.invBranch),
|
2026-07-22 22:53:28 +02:00
|
|
|
"Max-Forwards: 70",
|
|
|
|
|
std::format("Route: {}", c.route),
|
messages: sign our requests with the public identity, not the IMSI IMPU
Every request we originated — INVITE, CANCEL, both ACKs, in-dialog BYE and
friends — put the IMSI-derived temporary IMPU in From. 3GPP allows that
identity in REGISTER only; the same lesson was learned for the reg-event
SUBSCRIBE (480) and never carried to calls. Most P-CSCFs overwrite From
and hid it; a Telia node did not, and a reporter's IMSI appeared on the
callee's screen (field report 2026-09-01). A strict P-CSCF may reject the
INVITE outright.
CallerId(): the registered sip: public identity (P-Associated-URI), else
the tel: one, and the temporary IMPU only before either is learned. One
helper feeds all five builders, so a dialog's From never drifts. As UAS the
dialog's local URI is the INVITE's To (RFC 3261 12.2.1.1), stored on the
Dialog, so an incoming call's BYE is signed the way the network addressed
us. The identity is persisted in the state file and restored on warm
resume, so a call placed before the refresh 200 re-learns it cannot fall
back. DUMP_SIP now also writes the last outgoing INVITE
(imsd-invite-out.raw): the one request a field log could never show.
The byte-pinned INVITE fixture moves to the tel: identity its test context
knows; new scenarios cover the sip: identity, the tel: fallback, the
pre-learning case and the UAS BYE.
Bench-verified on KPN 2026-09-08: outgoing INVITE From is the registered
sip: identity, call accepted and carried; caller ID at the far end
unchanged.
2026-09-08 20:37:47 +02:00
|
|
|
std::format("From: <{}>;tag={}", FromUri(c, d), d.itag),
|
2026-07-22 22:53:28 +02:00
|
|
|
std::format("To: <{}>", d.ruri),
|
|
|
|
|
std::format("Call-ID: {}", d.callid),
|
2026-12-01 15:51:21 +01:00
|
|
|
std::format("CSeq: {} INVITE", d.invCseq),
|
2026-07-22 22:53:28 +02:00
|
|
|
std::format("Contact: <sip:{}@{}>;+g.3gpp.icsi-ref=\"urn%3Aurn-7%3A3gpp-service.ims.icsi.mmtel\"", ContactUser(c), imsd::util::HostPort(c.local, c.portUs)),
|
|
|
|
|
"Allow: INVITE, ACK, BYE, CANCEL, OPTIONS, PRACK, UPDATE, MESSAGE, REFER",
|
|
|
|
|
std::format("Supported: {}", supported),
|
|
|
|
|
"Accept-Contact: *;+g.3gpp.icsi-ref=\"urn%3Aurn-7%3A3gpp-service.ims.icsi.mmtel\"",
|
|
|
|
|
};
|
|
|
|
|
// see BuildSubscribeReg: P-Preferred-Identity only when known
|
|
|
|
|
if (!c.ppi.empty()) l.push_back(std::format("P-Preferred-Identity: <{}>", c.ppi));
|
|
|
|
|
if (!c.panInfo.empty()) l.push_back(std::format("P-Access-Network-Info: {}", c.panInfo));
|
|
|
|
|
l.push_back(std::format("Security-Verify: {}", c.securityServer));
|
|
|
|
|
l.push_back("Proxy-Require: sec-agree");
|
|
|
|
|
l.push_back("Require: sec-agree");
|
2026-12-01 15:51:21 +01:00
|
|
|
l.push_back(std::format("Session-Expires: {};refresher=uac", d.sessionExpires));
|
|
|
|
|
l.push_back(std::format("Min-SE: {}", d.minSe));
|
2026-07-22 22:53:28 +02:00
|
|
|
l.push_back("Content-Type: application/sdp");
|
|
|
|
|
l.push_back(std::format("Content-Length: {}", sdp.size()));
|
|
|
|
|
l.push_back("");
|
|
|
|
|
return detail::Join(l) + std::string(sdp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// An in-dialog request (PRACK, BYE, UPDATE...) over the dialog route set.
|
|
|
|
|
// `extra` lines go after CSeq; an optional body sets Content-Type/Length.
|
messages, imsd: P-Access-Network-Info in responses and in requests within a dialog
imsd named the serving cell in its REGISTERs, the reg-event SUBSCRIBE,
an INVITE and a MESSAGE. TS 24.229 5.1.2A.1.1 and 5.1.2A.2 have a UE
insert the header into a request within a dialog as well, and into a
response within a dialog, to a request for a dialog and to a standalone
method: not into a CANCEL nor into a response to one, and into an ACK
at its choice. A PRACK and a BYE carry it now, and every response imsd
builds but a 100 Trying, the 200 to a CANCEL and the transport's 400 to
a request cut short.
The server ports (the protected ports' TCP listeners and UDP sockets)
take a request from any source (AGENTS.md §12), and the header is the
phone's cell. What they send in answer keeps it only on the two paths
of the SA pair the shell has on record, and loses it anywhere else.
Tested: the suites, and the daemon of this commit on the workstation
against a stand-in network that asks it from both sides of those paths
(the fp6 journal's 2026-12-04-pani-in-dialog capture, bench/), and on
the dev phone on KPN, where its answers to a text's two requests and a
call's 180 and 200 carried the header inside the SA pair (the
journal's 2026-12-04-imsd-1.2.7-range-on-dev-phone capture).
2026-12-04 23:20:38 +01:00
|
|
|
// P-Access-Network-Info as in the INVITE (TS 24.229 5.1.2A.1.1,
|
|
|
|
|
// 5.1.2A.2); the ACKs and the CANCEL, built below, carry none (RFC 7315
|
|
|
|
|
// 5.7: the header appears in no ACK and no CANCEL).
|
2026-07-22 22:53:28 +02:00
|
|
|
std::string BuildInDialog(const Context& c, const Dialog& d, std::string_view method, int cseq, std::string_view viaBranch, std::span<const std::string> extra = {}, std::string_view body = {}, std::string_view ctype = {}) {
|
|
|
|
|
std::string target = d.remoteTarget.value_or(d.ruri);
|
|
|
|
|
std::string to = d.dialogTo.value_or(std::format("<{}>", d.ruri));
|
|
|
|
|
std::vector<std::string> l = {
|
|
|
|
|
std::format("{} {} SIP/2.0", method, target),
|
sip: UDP for the protected leg, TCP first with a fallback
imsd's protected leg (the second REGISTER and everything after it) was
TCP only. A P-CSCF that never answers the TCP connect on its protected
server port (O2 UK: the SYNs leave ESP-protected, nothing comes back)
left the unit looping with no way forward, although the SAs, the
listener sockets and the firewall rule already covered UDP.
The client flow now carries a transport. UDP binds the same protected
client port and connect()s the datagram socket to the P-CSCF's protected
server port, so the kernel delivers that peer's datagrams to it ahead of
the unconnected listener on the same port. One datagram is one message
(RFC 3261 18.3: a Content-Length that fits truncates, one that does not
fit discards the datagram, none means the rest of the datagram); a
receive error is logged and marks the flow dead; a message over the
single-packet ESP budget at the ims PDN's MTU is logged once per flow,
since the kernel fragments it and a P-CSCF may drop the fragments.
Every protected request's Via follows the transport; the challenge
stays UDP.
SIP_TRANSPORT selects the policy: auto (default) registers over TCP and,
after two unanswered connects, registers again over UDP (new challenge,
new SA pair) - but only on a phone whose state file does not record a
successful TCP registration, so an outage on a TCP carrier never becomes
a second initial REGISTER; a phone whose last registration was UDP goes
straight to UDP. tcp and udp force one. A connect refused locally
(EADDRNOTAVAIL: the 4-tuple still in TIME_WAIT from the previous flow)
keeps retrying and is not counted as silence; SO_ERROR results are
logged by name. The registration's transport is persisted so a warm
resume reconnects the same way, and the resume gives up after two
silences.
Two things the change exposed and fixes: an engine-fatal event exited 0
("exiting for systemd restart" with Restart=on-failure never firing),
and a fresh registration refused with the P-CSCF's fresh-SA throttle
(Security-Server spi-s=0) must not be retried by a 120-s restart loop,
since every attempt re-arms the ~20-min window - it is now waited out
in-process, growing on repeats, with D-Bus commands still served. A
resume the network refuses falls through to a fresh registration. A
retransmitted 200 OK to our INVITE is ACKed again and no longer starts
a second media leg.
Verified on KPN: resume over TCP after a binary swap, MT and MO calls
with media both ways, the throttle deferral and its self-recovery, the
UDP client path up to KPN dropping the datagram. The UDP success path
is a field test; SIP retransmission timers over UDP are not in this
change.
2026-09-17 14:58:46 +02:00
|
|
|
ViaLine(c, c.transport, c.portUs, viaBranch),
|
2026-07-22 22:53:28 +02:00
|
|
|
"Max-Forwards: 70",
|
|
|
|
|
std::format("Route: {}", DialogRoute(c, d)),
|
messages: sign our requests with the public identity, not the IMSI IMPU
Every request we originated — INVITE, CANCEL, both ACKs, in-dialog BYE and
friends — put the IMSI-derived temporary IMPU in From. 3GPP allows that
identity in REGISTER only; the same lesson was learned for the reg-event
SUBSCRIBE (480) and never carried to calls. Most P-CSCFs overwrite From
and hid it; a Telia node did not, and a reporter's IMSI appeared on the
callee's screen (field report 2026-09-01). A strict P-CSCF may reject the
INVITE outright.
CallerId(): the registered sip: public identity (P-Associated-URI), else
the tel: one, and the temporary IMPU only before either is learned. One
helper feeds all five builders, so a dialog's From never drifts. As UAS the
dialog's local URI is the INVITE's To (RFC 3261 12.2.1.1), stored on the
Dialog, so an incoming call's BYE is signed the way the network addressed
us. The identity is persisted in the state file and restored on warm
resume, so a call placed before the refresh 200 re-learns it cannot fall
back. DUMP_SIP now also writes the last outgoing INVITE
(imsd-invite-out.raw): the one request a field log could never show.
The byte-pinned INVITE fixture moves to the tel: identity its test context
knows; new scenarios cover the sip: identity, the tel: fallback, the
pre-learning case and the UAS BYE.
Bench-verified on KPN 2026-09-08: outgoing INVITE From is the registered
sip: identity, call accepted and carried; caller ID at the far end
unchanged.
2026-09-08 20:37:47 +02:00
|
|
|
std::format("From: <{}>;tag={}", FromUri(c, d), d.itag),
|
2026-07-22 22:53:28 +02:00
|
|
|
std::format("To: {}", to),
|
|
|
|
|
std::format("Call-ID: {}", d.callid),
|
|
|
|
|
std::format("CSeq: {} {}", cseq, method),
|
|
|
|
|
};
|
|
|
|
|
for (const std::string& e : extra)
|
|
|
|
|
l.push_back(e);
|
messages, imsd: P-Access-Network-Info in responses and in requests within a dialog
imsd named the serving cell in its REGISTERs, the reg-event SUBSCRIBE,
an INVITE and a MESSAGE. TS 24.229 5.1.2A.1.1 and 5.1.2A.2 have a UE
insert the header into a request within a dialog as well, and into a
response within a dialog, to a request for a dialog and to a standalone
method: not into a CANCEL nor into a response to one, and into an ACK
at its choice. A PRACK and a BYE carry it now, and every response imsd
builds but a 100 Trying, the 200 to a CANCEL and the transport's 400 to
a request cut short.
The server ports (the protected ports' TCP listeners and UDP sockets)
take a request from any source (AGENTS.md §12), and the header is the
phone's cell. What they send in answer keeps it only on the two paths
of the SA pair the shell has on record, and loses it anywhere else.
Tested: the suites, and the daemon of this commit on the workstation
against a stand-in network that asks it from both sides of those paths
(the fp6 journal's 2026-12-04-pani-in-dialog capture, bench/), and on
the dev phone on KPN, where its answers to a text's two requests and a
call's 180 and 200 carried the header inside the SA pair (the
journal's 2026-12-04-imsd-1.2.7-range-on-dev-phone capture).
2026-12-04 23:20:38 +01:00
|
|
|
if (!c.panInfo.empty()) l.push_back(std::format("P-Access-Network-Info: {}", c.panInfo));
|
2026-07-22 22:53:28 +02:00
|
|
|
if (!body.empty()) {
|
|
|
|
|
l.push_back(std::format("Content-Type: {}", ctype));
|
|
|
|
|
l.push_back(std::format("Content-Length: {}", body.size()));
|
|
|
|
|
l.push_back("");
|
|
|
|
|
return detail::Join(l) + std::string(body);
|
|
|
|
|
}
|
|
|
|
|
l.push_back("Content-Length: 0");
|
|
|
|
|
return std::format("{}\r\n", detail::Join(l));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ACK for a 2xx final: a new transaction over the dialog route to the
|
|
|
|
|
// remote target, echoing the dialog To (with the remote tag).
|
|
|
|
|
std::string BuildAck2xx(const Context& c, const Dialog& d, std::string_view viaBranch, std::string_view toHeader) {
|
|
|
|
|
std::string target = d.remoteTarget.value_or(d.ruri);
|
|
|
|
|
std::string to = d.dialogTo.value_or(std::string(toHeader));
|
|
|
|
|
std::vector<std::string> l = {
|
|
|
|
|
std::format("ACK {} SIP/2.0", target),
|
sip: UDP for the protected leg, TCP first with a fallback
imsd's protected leg (the second REGISTER and everything after it) was
TCP only. A P-CSCF that never answers the TCP connect on its protected
server port (O2 UK: the SYNs leave ESP-protected, nothing comes back)
left the unit looping with no way forward, although the SAs, the
listener sockets and the firewall rule already covered UDP.
The client flow now carries a transport. UDP binds the same protected
client port and connect()s the datagram socket to the P-CSCF's protected
server port, so the kernel delivers that peer's datagrams to it ahead of
the unconnected listener on the same port. One datagram is one message
(RFC 3261 18.3: a Content-Length that fits truncates, one that does not
fit discards the datagram, none means the rest of the datagram); a
receive error is logged and marks the flow dead; a message over the
single-packet ESP budget at the ims PDN's MTU is logged once per flow,
since the kernel fragments it and a P-CSCF may drop the fragments.
Every protected request's Via follows the transport; the challenge
stays UDP.
SIP_TRANSPORT selects the policy: auto (default) registers over TCP and,
after two unanswered connects, registers again over UDP (new challenge,
new SA pair) - but only on a phone whose state file does not record a
successful TCP registration, so an outage on a TCP carrier never becomes
a second initial REGISTER; a phone whose last registration was UDP goes
straight to UDP. tcp and udp force one. A connect refused locally
(EADDRNOTAVAIL: the 4-tuple still in TIME_WAIT from the previous flow)
keeps retrying and is not counted as silence; SO_ERROR results are
logged by name. The registration's transport is persisted so a warm
resume reconnects the same way, and the resume gives up after two
silences.
Two things the change exposed and fixes: an engine-fatal event exited 0
("exiting for systemd restart" with Restart=on-failure never firing),
and a fresh registration refused with the P-CSCF's fresh-SA throttle
(Security-Server spi-s=0) must not be retried by a 120-s restart loop,
since every attempt re-arms the ~20-min window - it is now waited out
in-process, growing on repeats, with D-Bus commands still served. A
resume the network refuses falls through to a fresh registration. A
retransmitted 200 OK to our INVITE is ACKed again and no longer starts
a second media leg.
Verified on KPN: resume over TCP after a binary swap, MT and MO calls
with media both ways, the throttle deferral and its self-recovery, the
UDP client path up to KPN dropping the datagram. The UDP success path
is a field test; SIP retransmission timers over UDP are not in this
change.
2026-09-17 14:58:46 +02:00
|
|
|
ViaLine(c, c.transport, c.portUs, viaBranch),
|
2026-07-22 22:53:28 +02:00
|
|
|
"Max-Forwards: 70",
|
|
|
|
|
std::format("Route: {}", DialogRoute(c, d)),
|
messages: sign our requests with the public identity, not the IMSI IMPU
Every request we originated — INVITE, CANCEL, both ACKs, in-dialog BYE and
friends — put the IMSI-derived temporary IMPU in From. 3GPP allows that
identity in REGISTER only; the same lesson was learned for the reg-event
SUBSCRIBE (480) and never carried to calls. Most P-CSCFs overwrite From
and hid it; a Telia node did not, and a reporter's IMSI appeared on the
callee's screen (field report 2026-09-01). A strict P-CSCF may reject the
INVITE outright.
CallerId(): the registered sip: public identity (P-Associated-URI), else
the tel: one, and the temporary IMPU only before either is learned. One
helper feeds all five builders, so a dialog's From never drifts. As UAS the
dialog's local URI is the INVITE's To (RFC 3261 12.2.1.1), stored on the
Dialog, so an incoming call's BYE is signed the way the network addressed
us. The identity is persisted in the state file and restored on warm
resume, so a call placed before the refresh 200 re-learns it cannot fall
back. DUMP_SIP now also writes the last outgoing INVITE
(imsd-invite-out.raw): the one request a field log could never show.
The byte-pinned INVITE fixture moves to the tel: identity its test context
knows; new scenarios cover the sip: identity, the tel: fallback, the
pre-learning case and the UAS BYE.
Bench-verified on KPN 2026-09-08: outgoing INVITE From is the registered
sip: identity, call accepted and carried; caller ID at the far end
unchanged.
2026-09-08 20:37:47 +02:00
|
|
|
std::format("From: <{}>;tag={}", FromUri(c, d), d.itag),
|
2026-07-22 22:53:28 +02:00
|
|
|
std::format("To: {}", to),
|
|
|
|
|
std::format("Call-ID: {}", d.callid),
|
2026-12-01 15:51:21 +01:00
|
|
|
std::format("CSeq: {} ACK", d.invCseq),
|
2026-07-22 22:53:28 +02:00
|
|
|
"Content-Length: 0",
|
|
|
|
|
};
|
|
|
|
|
return std::format("{}\r\n", detail::Join(l));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ACK for a non-2xx final: hop-by-hop over the INVITE's own branch and the
|
|
|
|
|
// registration Route, echoing the response's To.
|
|
|
|
|
std::string BuildAckNon2xx(const Context& c, const Dialog& d, std::string_view toHeader) {
|
|
|
|
|
std::vector<std::string> l = {
|
|
|
|
|
std::format("ACK {} SIP/2.0", d.ruri),
|
sip: UDP for the protected leg, TCP first with a fallback
imsd's protected leg (the second REGISTER and everything after it) was
TCP only. A P-CSCF that never answers the TCP connect on its protected
server port (O2 UK: the SYNs leave ESP-protected, nothing comes back)
left the unit looping with no way forward, although the SAs, the
listener sockets and the firewall rule already covered UDP.
The client flow now carries a transport. UDP binds the same protected
client port and connect()s the datagram socket to the P-CSCF's protected
server port, so the kernel delivers that peer's datagrams to it ahead of
the unconnected listener on the same port. One datagram is one message
(RFC 3261 18.3: a Content-Length that fits truncates, one that does not
fit discards the datagram, none means the rest of the datagram); a
receive error is logged and marks the flow dead; a message over the
single-packet ESP budget at the ims PDN's MTU is logged once per flow,
since the kernel fragments it and a P-CSCF may drop the fragments.
Every protected request's Via follows the transport; the challenge
stays UDP.
SIP_TRANSPORT selects the policy: auto (default) registers over TCP and,
after two unanswered connects, registers again over UDP (new challenge,
new SA pair) - but only on a phone whose state file does not record a
successful TCP registration, so an outage on a TCP carrier never becomes
a second initial REGISTER; a phone whose last registration was UDP goes
straight to UDP. tcp and udp force one. A connect refused locally
(EADDRNOTAVAIL: the 4-tuple still in TIME_WAIT from the previous flow)
keeps retrying and is not counted as silence; SO_ERROR results are
logged by name. The registration's transport is persisted so a warm
resume reconnects the same way, and the resume gives up after two
silences.
Two things the change exposed and fixes: an engine-fatal event exited 0
("exiting for systemd restart" with Restart=on-failure never firing),
and a fresh registration refused with the P-CSCF's fresh-SA throttle
(Security-Server spi-s=0) must not be retried by a 120-s restart loop,
since every attempt re-arms the ~20-min window - it is now waited out
in-process, growing on repeats, with D-Bus commands still served. A
resume the network refuses falls through to a fresh registration. A
retransmitted 200 OK to our INVITE is ACKed again and no longer starts
a second media leg.
Verified on KPN: resume over TCP after a binary swap, MT and MO calls
with media both ways, the throttle deferral and its self-recovery, the
UDP client path up to KPN dropping the datagram. The UDP success path
is a field test; SIP retransmission timers over UDP are not in this
change.
2026-09-17 14:58:46 +02:00
|
|
|
ViaLine(c, c.transport, c.portUs, d.invBranch),
|
2026-07-22 22:53:28 +02:00
|
|
|
"Max-Forwards: 70",
|
|
|
|
|
std::format("Route: {}", c.route),
|
messages: sign our requests with the public identity, not the IMSI IMPU
Every request we originated — INVITE, CANCEL, both ACKs, in-dialog BYE and
friends — put the IMSI-derived temporary IMPU in From. 3GPP allows that
identity in REGISTER only; the same lesson was learned for the reg-event
SUBSCRIBE (480) and never carried to calls. Most P-CSCFs overwrite From
and hid it; a Telia node did not, and a reporter's IMSI appeared on the
callee's screen (field report 2026-09-01). A strict P-CSCF may reject the
INVITE outright.
CallerId(): the registered sip: public identity (P-Associated-URI), else
the tel: one, and the temporary IMPU only before either is learned. One
helper feeds all five builders, so a dialog's From never drifts. As UAS the
dialog's local URI is the INVITE's To (RFC 3261 12.2.1.1), stored on the
Dialog, so an incoming call's BYE is signed the way the network addressed
us. The identity is persisted in the state file and restored on warm
resume, so a call placed before the refresh 200 re-learns it cannot fall
back. DUMP_SIP now also writes the last outgoing INVITE
(imsd-invite-out.raw): the one request a field log could never show.
The byte-pinned INVITE fixture moves to the tel: identity its test context
knows; new scenarios cover the sip: identity, the tel: fallback, the
pre-learning case and the UAS BYE.
Bench-verified on KPN 2026-09-08: outgoing INVITE From is the registered
sip: identity, call accepted and carried; caller ID at the far end
unchanged.
2026-09-08 20:37:47 +02:00
|
|
|
std::format("From: <{}>;tag={}", FromUri(c, d), d.itag),
|
2026-07-22 22:53:28 +02:00
|
|
|
std::format("To: {}", toHeader),
|
|
|
|
|
std::format("Call-ID: {}", d.callid),
|
2026-12-01 15:51:21 +01:00
|
|
|
std::format("CSeq: {} ACK", d.invCseq),
|
2026-07-22 22:53:28 +02:00
|
|
|
"Content-Length: 0",
|
|
|
|
|
};
|
|
|
|
|
return std::format("{}\r\n", detail::Join(l));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CANCEL: RFC 3261 requires it copy the INVITE's branch + CSeq number.
|
|
|
|
|
std::string BuildCancel(const Context& c, const Dialog& d) {
|
|
|
|
|
std::vector<std::string> l = {
|
|
|
|
|
std::format("CANCEL {} SIP/2.0", d.ruri),
|
sip: UDP for the protected leg, TCP first with a fallback
imsd's protected leg (the second REGISTER and everything after it) was
TCP only. A P-CSCF that never answers the TCP connect on its protected
server port (O2 UK: the SYNs leave ESP-protected, nothing comes back)
left the unit looping with no way forward, although the SAs, the
listener sockets and the firewall rule already covered UDP.
The client flow now carries a transport. UDP binds the same protected
client port and connect()s the datagram socket to the P-CSCF's protected
server port, so the kernel delivers that peer's datagrams to it ahead of
the unconnected listener on the same port. One datagram is one message
(RFC 3261 18.3: a Content-Length that fits truncates, one that does not
fit discards the datagram, none means the rest of the datagram); a
receive error is logged and marks the flow dead; a message over the
single-packet ESP budget at the ims PDN's MTU is logged once per flow,
since the kernel fragments it and a P-CSCF may drop the fragments.
Every protected request's Via follows the transport; the challenge
stays UDP.
SIP_TRANSPORT selects the policy: auto (default) registers over TCP and,
after two unanswered connects, registers again over UDP (new challenge,
new SA pair) - but only on a phone whose state file does not record a
successful TCP registration, so an outage on a TCP carrier never becomes
a second initial REGISTER; a phone whose last registration was UDP goes
straight to UDP. tcp and udp force one. A connect refused locally
(EADDRNOTAVAIL: the 4-tuple still in TIME_WAIT from the previous flow)
keeps retrying and is not counted as silence; SO_ERROR results are
logged by name. The registration's transport is persisted so a warm
resume reconnects the same way, and the resume gives up after two
silences.
Two things the change exposed and fixes: an engine-fatal event exited 0
("exiting for systemd restart" with Restart=on-failure never firing),
and a fresh registration refused with the P-CSCF's fresh-SA throttle
(Security-Server spi-s=0) must not be retried by a 120-s restart loop,
since every attempt re-arms the ~20-min window - it is now waited out
in-process, growing on repeats, with D-Bus commands still served. A
resume the network refuses falls through to a fresh registration. A
retransmitted 200 OK to our INVITE is ACKed again and no longer starts
a second media leg.
Verified on KPN: resume over TCP after a binary swap, MT and MO calls
with media both ways, the throttle deferral and its self-recovery, the
UDP client path up to KPN dropping the datagram. The UDP success path
is a field test; SIP retransmission timers over UDP are not in this
change.
2026-09-17 14:58:46 +02:00
|
|
|
ViaLine(c, c.transport, c.portUs, d.invBranch),
|
2026-07-22 22:53:28 +02:00
|
|
|
"Max-Forwards: 70",
|
|
|
|
|
std::format("Route: {}", c.route),
|
messages: sign our requests with the public identity, not the IMSI IMPU
Every request we originated — INVITE, CANCEL, both ACKs, in-dialog BYE and
friends — put the IMSI-derived temporary IMPU in From. 3GPP allows that
identity in REGISTER only; the same lesson was learned for the reg-event
SUBSCRIBE (480) and never carried to calls. Most P-CSCFs overwrite From
and hid it; a Telia node did not, and a reporter's IMSI appeared on the
callee's screen (field report 2026-09-01). A strict P-CSCF may reject the
INVITE outright.
CallerId(): the registered sip: public identity (P-Associated-URI), else
the tel: one, and the temporary IMPU only before either is learned. One
helper feeds all five builders, so a dialog's From never drifts. As UAS the
dialog's local URI is the INVITE's To (RFC 3261 12.2.1.1), stored on the
Dialog, so an incoming call's BYE is signed the way the network addressed
us. The identity is persisted in the state file and restored on warm
resume, so a call placed before the refresh 200 re-learns it cannot fall
back. DUMP_SIP now also writes the last outgoing INVITE
(imsd-invite-out.raw): the one request a field log could never show.
The byte-pinned INVITE fixture moves to the tel: identity its test context
knows; new scenarios cover the sip: identity, the tel: fallback, the
pre-learning case and the UAS BYE.
Bench-verified on KPN 2026-09-08: outgoing INVITE From is the registered
sip: identity, call accepted and carried; caller ID at the far end
unchanged.
2026-09-08 20:37:47 +02:00
|
|
|
std::format("From: <{}>;tag={}", FromUri(c, d), d.itag),
|
2026-07-22 22:53:28 +02:00
|
|
|
std::format("To: <{}>", d.ruri),
|
|
|
|
|
std::format("Call-ID: {}", d.callid),
|
2026-12-01 15:51:21 +01:00
|
|
|
std::format("CSeq: {} CANCEL", d.invCseq),
|
2026-07-22 22:53:28 +02:00
|
|
|
"Content-Length: 0",
|
|
|
|
|
};
|
|
|
|
|
return std::format("{}\r\n", detail::Join(l));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// A response to an inbound INVITE — the UAS side of call setup. Unlike
|
|
|
|
|
// BuildResponse200 (first Via only, fine for hop-by-hop CANCEL/BYE
|
|
|
|
|
// answers), an INVITE response travels the whole proxy chain back to the
|
|
|
|
|
// caller: it must echo EVERY Via in order, return the Record-Route set on
|
|
|
|
|
// dialog-establishing (1xx/2xx) responses, and mint the dialog's local
|
|
|
|
|
// half by tagging To (all but 100 Trying, which is hop-by-hop and
|
|
|
|
|
// tagless). 1xx/2xx carry our Contact (the dialog's remote target for the
|
|
|
|
|
// caller) and Allow; `extra` appends headers (RSeq, Session-Expires...);
|
messages, imsd: P-Access-Network-Info in responses and in requests within a dialog
imsd named the serving cell in its REGISTERs, the reg-event SUBSCRIBE,
an INVITE and a MESSAGE. TS 24.229 5.1.2A.1.1 and 5.1.2A.2 have a UE
insert the header into a request within a dialog as well, and into a
response within a dialog, to a request for a dialog and to a standalone
method: not into a CANCEL nor into a response to one, and into an ACK
at its choice. A PRACK and a BYE carry it now, and every response imsd
builds but a 100 Trying, the 200 to a CANCEL and the transport's 400 to
a request cut short.
The server ports (the protected ports' TCP listeners and UDP sockets)
take a request from any source (AGENTS.md §12), and the header is the
phone's cell. What they send in answer keeps it only on the two paths
of the SA pair the shell has on record, and loses it anywhere else.
Tested: the suites, and the daemon of this commit on the workstation
against a stand-in network that asks it from both sides of those paths
(the fp6 journal's 2026-12-04-pani-in-dialog capture, bench/), and on
the dev phone on KPN, where its answers to a text's two requests and a
call's 180 and 200 carried the header inside the SA pair (the
journal's 2026-12-04-imsd-1.2.7-range-on-dev-phone capture).
2026-12-04 23:20:38 +01:00
|
|
|
// P-Access-Network-Info follows them where the cell is known, in all
|
|
|
|
|
// but a 100; `sdp` turns a 200 into the answer.
|
2026-07-22 22:53:28 +02:00
|
|
|
std::string BuildInviteResponse(const Context& c, std::string_view invite, int code, std::string_view text, std::string_view toTag, std::optional<std::string_view> sdp = std::nullopt, std::span<const std::string> extra = {}) {
|
|
|
|
|
std::vector<std::string> l = {std::format("SIP/2.0 {} {}", code, text)};
|
|
|
|
|
for (auto v : imsd::sip::Headers(invite, "Via"))
|
|
|
|
|
l.push_back(std::format("Via: {}", v));
|
|
|
|
|
if (code < 300)
|
|
|
|
|
for (auto v : imsd::sip::Headers(invite, "Record-Route"))
|
|
|
|
|
l.push_back(std::format("Record-Route: {}", v));
|
|
|
|
|
if (auto f = imsd::sip::Header(invite, "From")) l.push_back(std::format("From: {}", *f));
|
|
|
|
|
if (auto t = imsd::sip::Header(invite, "To")) {
|
|
|
|
|
if (code == 100 || t->contains("tag=")) l.push_back(std::format("To: {}", *t));
|
|
|
|
|
else
|
|
|
|
|
l.push_back(std::format("To: {};tag={}", *t, toTag));
|
|
|
|
|
}
|
|
|
|
|
if (auto ci = imsd::sip::Header(invite, "Call-ID")) l.push_back(std::format("Call-ID: {}", *ci));
|
|
|
|
|
if (auto cs = imsd::sip::Header(invite, "CSeq")) l.push_back(std::format("CSeq: {}", *cs));
|
|
|
|
|
if (code != 100 && code < 300) {
|
|
|
|
|
l.push_back(std::format("Contact: <sip:{}@{}>", ContactUser(c), imsd::util::HostPort(c.local, c.portUs)));
|
|
|
|
|
l.push_back("Allow: INVITE, ACK, BYE, CANCEL, OPTIONS, PRACK, UPDATE, MESSAGE, REFER");
|
|
|
|
|
}
|
|
|
|
|
for (const std::string& e : extra)
|
|
|
|
|
l.push_back(e);
|
messages, imsd: P-Access-Network-Info in responses and in requests within a dialog
imsd named the serving cell in its REGISTERs, the reg-event SUBSCRIBE,
an INVITE and a MESSAGE. TS 24.229 5.1.2A.1.1 and 5.1.2A.2 have a UE
insert the header into a request within a dialog as well, and into a
response within a dialog, to a request for a dialog and to a standalone
method: not into a CANCEL nor into a response to one, and into an ACK
at its choice. A PRACK and a BYE carry it now, and every response imsd
builds but a 100 Trying, the 200 to a CANCEL and the transport's 400 to
a request cut short.
The server ports (the protected ports' TCP listeners and UDP sockets)
take a request from any source (AGENTS.md §12), and the header is the
phone's cell. What they send in answer keeps it only on the two paths
of the SA pair the shell has on record, and loses it anywhere else.
Tested: the suites, and the daemon of this commit on the workstation
against a stand-in network that asks it from both sides of those paths
(the fp6 journal's 2026-12-04-pani-in-dialog capture, bench/), and on
the dev phone on KPN, where its answers to a text's two requests and a
call's 180 and 200 carried the header inside the SA pair (the
journal's 2026-12-04-imsd-1.2.7-range-on-dev-phone capture).
2026-12-04 23:20:38 +01:00
|
|
|
detail::AccessInfo(l, c, invite, code);
|
2026-07-22 22:53:28 +02:00
|
|
|
if (sdp) {
|
|
|
|
|
l.push_back("Content-Type: application/sdp");
|
|
|
|
|
l.push_back(std::format("Content-Length: {}", sdp->size()));
|
|
|
|
|
l.push_back("");
|
|
|
|
|
return detail::Join(l) + std::string(*sdp);
|
|
|
|
|
}
|
|
|
|
|
l.push_back("Content-Length: 0");
|
|
|
|
|
return std::format("{}\r\n", detail::Join(l));
|
|
|
|
|
}
|
|
|
|
|
|
imsd, engine, ImsdBus, packaging, README: SMS over IP behind SMS_OVER_IMS=1, net.catcrafts.IMS1.Messaging
With the modem's own IMS client off, a carrier that carries SMS over IMS
only leaves the phone no path for texts but imsd; on one with SMS over
NAS the +g.3gpp.smsip tag may take them off a path that works (the
order is operator policy, TS 24.341 5.3.3.4.2 NOTE 2). So the knob is the user's and off by default: SMS_OVER_IMS=1
advertises the tag (Context::smsOverIp), reads the service centre from
EF_SMSP record 1 at bring-up — SMSC= instead — and opens Send on a
second interface, net.catcrafts.IMS1.Messaging, beside the frozen one,
gated by a polkit action of its own (Gated's Action parameter; the Voice
action's callers are unchanged). The shell keeps one message in flight
on the flow and queues the rest, routes the in-flight MESSAGE's final
to it (OnSmsResponse; a submit report reaches it through OnSmsMessage),
seeds and advances the TP-MR, RP-MR and concatenation counters, answers an inbound
MESSAGE outside a call through OnSmsMessage (one inside the call's
dialog stays the CallMachine's, and is not the MESSAGE of any sentence
below) — the message to the bus as
Received, the delivery report on the flow with its final logged, a
status report matched by TP-MR and recipient to the message that asked
for one — and reports a message's fate as StateChanged. A Send is judged
when the engine takes its command up: unregistered or with the flow down
then, it fails with that reason.
The journal names a delivered message's sender, size and time — a text
reaches it only as the body of a MESSAGE whose Content-Type does not
contain application/vnd.3gpp.sms or as the hex of a body that is no RP
message or of an unusable TPDU; DUMP_SIP adds the two MESSAGE dumps.
While the knob is off a network may deliver a short message over IP
regardless; answering it 200 and acknowledging it would count it
delivered to nobody, so it is refused with 480 Temporarily Unavailable,
which lets the IP-SM-GW try the modem's path where the network has one
(TS 24.341 5.3.3.4.2 NOTE 2); a MESSAGE outside a call's dialog whose
Content-Type does not contain application/vnd.3gpp.sms (letter case
ignored) is still answered 200 and logged with its body — the 200 in
BuildResponse's shape now, the line with the content type. A
retransmitted MESSAGE — the same
Call-ID, CSeq and Via branch (the value of the first `branch=` of its
first Via line; a MESSAGE with none or an empty one is never taken for a
copy) within 32 s of our final, in the same run of the daemon — is
answered again and not handled again (the engine's RecentTransactions, a
table in memory), where a repeated RP-DATA would otherwise,
with SMS over IP on, be handled again: a second delivery report where
the first got one and, of a text in one segment, a second text.
The Messaging signals carry a text's content, and a D-Bus broadcast
reaches every account the bus policy lets receive from imsd — which
polkit cannot narrow — so they are never broadcast: Subscribe, behind the
same action as Send, records the caller's unique name, and Received and
StateChanged go unicast to each subscriber (the submission's sent or
failed also to the Send caller), which the bus delivers to those
connections — root's busctl monitor sees them as well. A subscription
ends with its connection; a consumer
subscribes at start and again when imsd's name reappears. GetStatus
answers `enabled` from the daemon's configuration, not from the
registration snapshot, so an asker before the first registration reads
the truth.
A message's id holds the run's token, eight hex digits drawn from
std::random_device at the daemon's start, ahead of a second and a
count: `ims-sms-<run>-<start>-<N>` for
a sent text, with the start second and a counter, and
`ims-sms-in-<run>-<epoch>-<N>` for a received one, with its second. A
counter starts over with the daemon, and the phone's wall clock reads
1970 until the first time sync, so a second repeats across runs: the
token is what tells one run's ids from another's, and a client keys by id
(RunToken, SentSmsId and ReceivedSmsId, pinned by tests/Engine).
Tested on the dev phone on KPN (2026-10-17; the fp6 journal's
2026-10-17-sms-over-ip-bench capture) on sms-branch builds ahead of this
history (195e91b, then c57765a with the retransmission absorption): the
tag on a resumed and on a fresh registration, Sends to 202 and their
submit reports in one and two segments and in UCS-2, texts received in
one and two segments and in UCS-2, every delivery report answered 202
but one of the two for the 01:23 text, refused 488 on the build before
the absorption, and a same-second retransmission absorbed under c57765a
(01:44) — AGENTS.md §5 has the record and §13 the network facts,
among them that the modem was CS-detached that night. On the host's
session bus in dev mode:
two Sends answered with ids and failed "imsd is not registered"
(2026-10-16), and three connections — a subscriber, a match-rule-only
listener, an unsubscribed sender — where only the subscriber and the
sender heard the StateChanged and the subscription ended with its
connection (2026-10-17); with the knob off, GetStatus's `enabled` false,
Send refused NotSupported and Subscribe answered (a private session bus,
this commit's host build, 2026-11-19; its sha256 begins 98e30a9522409d99).
The SMSC= override ran on 2026-10-18 from 02:17
(smsc-override-2026-10-18-0217):
the EF_SMSP number as an override and a Send through it to sent, and
SMSC=abc stopped at bring-up with "is not a number" (ForNumber dials
digits, * and # only; the bench's first run, on a build outside this
history that dialled the BCD table's a, b and c, submitted with them and
got an RP-ERROR back, RP cause 28).
A status report is matched to the message that asked for one by its TP-MR
and the recipient's number — among this run's messages: the book is in
memory — and the message gets a word for the whole (StatusReportBook,
pinned), by TS 23.040 9.2.3.15's classes of TP-ST: delivery-failed at the
first report in which the SC is not making any more transfer attempts (a
permanent error or a temporary one; a reserved value too, read as
"Service Rejected" as the clause has the MS read it), delivered when the
last of its segments is reported in the completed class (the SC's word
in the reason) — never a later segment's word over an earlier failure.
The next TP-MR is kept in
<state dir>/sms-tpmr across restarts, so the TP-MRs continue instead of
starting over at random: a reuse needs a full 256-segment wrap, not a
restart (a report can be days late) — as long as the file's last write
reached the disk: it is neither checked nor synced.
One message's StateChanged words come in the order they mean, which is
not the order imsd learns them: the service centre can deliver and report
on segment 1 while segment 2 is still on its way. SmsWordOrder
(Imsd:Engine, pure) gives the order §8.4 states: the submission's word
first — `sent`, or `failed`, after which nothing follows — and a
report's only after `sent`. A report's word on the message in flight is
held until the submission ends (the latest word; one message is in
flight at a time) and posted after `sent`. A submission that fails after
the centre reported every segment delivered posts `sent`, the failure in
its reason, and then `delivered`. A `failed` otherwise drops the held
word and StatusReportBook::ForgetMessage forgets the message's TP-MRs, so
a later report on a part that went matches nothing and is logged.
tests/Engine pins the book's forgetting and each of these orders.
BuildResponse (the messages commit) is the final of the inbound MESSAGE
path from here on — the 200, the 480 and the 488; BuildResponse200's hop-by-hop
shape stays for the other answers that had it.
The reassembler's 30-minute age-out is judged in the run loop beside the
SMS deadline, whether or not another MESSAGE ever comes.
The MESSAGE's target is the service centre's own international number
(ScPsi), not RuriFor's dialled-number guess, whose trunk-0 rule is
Dutch: a national SMSC= of nine or more characters starting with 0 would
have become a +31 number, a 00-prefixed one +<the rest>, any other
non-international value a phone-context URI at the home domain; a service
centre ScPsi makes nothing of (national, 00-prefixed, or a '*' or '#'
among the digits, which ForNumber dials) is refused at bring-up on that same
predicate with a journal line, never announced, and Send then fails
with no service centre.
A failed submission's StateChanged reason can hold the network's text (a
refused final's, RefusalSummary); a D-Bus string must be valid UTF-8, so
the emission makes it so (g_utf8_make_valid: bytes that are not become
U+FFFD, and a NUL byte ends the string).
2026-10-16 23:30:25 +02:00
|
|
|
// The final for an inbound MESSAGE outside a call's dialog — the 200 to one we took, the 480 or
|
sip: a header is found under its compact form, and with white space before the colon
RFC 3261 7.3.3 lets a sender write a header name in its one-letter form
at any time and has every implementation accept both forms. The text
layer matched the long name only, so to every reader a header sent in
compact form was absent: the TCP framer took no body length from `l:`
and read the body as the start of the next message, a response with
`i:` matched no transaction and a request no call, the finals imsd
builds left out a Via, From, To or Call-ID that came in compact form,
and a short message whose content type came as `c:` was answered 200
and logged instead of taken (issue #2).
MatchHeaderLine now takes a line under the name it is asked for or
under that name's compact form: the ten RFC 3261 defines, and `o` for
Event (RFC 6665) and `x` for Session-Expires (RFC 4028), the two
extension headers imsd reads that have one. Every reader goes through
it — Header, Headers, the framer, DatagramMessage — so no reader
changes for it. It also takes white space between the name and the
colon, which the same grammar allows (25.1's HCOLON; 7.3.1's
"Subject : lunch") and the matcher refused. A value folded
onto further lines is still read by its first line.
One reader is changed for the rows it is now handed. ViaResponsePort
gives the port the top Via names, to which the answer to a request that
came in a datagram on a server port goes (RFC 3261 18.2.2), and none
where that Via carries `rport` (RFC 3581): the answer then goes back to
the port the request came from. It took `rport` for present where
those five letters, in lower case, stood anywhere behind the row's
first semicolon. Parameter names are case-insensitive (7.3.1), and a
request whose top Via came as `v: …;RPORT`, a row 1.2.0 passed over,
would have been answered to the port that Via names, its `rport` not
seen. ViaResponsePort now reads the Via's parameters one by one, each
to the next semicolon outside a quoted string, and `rport` is one by
its name, in any case: not those letters in a value or in a longer
name. Read so, the last parameter of the top value would run on into
the next value where a row holds a list — `rport, SIP/2.0/UDP …` is no
`rport`, and 1.2.0's search of the letters found it —, so the top value
is cut from the row first, at the first comma outside a quoted string.
With that cut a lower value's `rport` no longer counts for the top
one's, as it did in 1.2.0 for a list on one line.
Tested by the suites: tests/Sip pins each of the twelve forms through
Header, in lower and in upper case, and through Headers, `l:` through
the framer and DatagramMessage, 7.3.1's four spellings of the colon, and
ViaResponsePort's reading of a Via row, `rport` by its name and of a
list on one line the top value alone; tests/Engine drives an incoming
call from an INVITE in compact form, an outgoing call from responses in
compact form and a delivered MESSAGE in compact form through the
reducers; tests/Messages builds BuildResponse, BuildResponse200 and
BuildInviteResponse for a request in compact form. On the workstation's
loopback (the fp6 journal's 2026-11-20-issues-2-5 capture, server-ports,
at this commit and at the one before) a request on a server port whose
Via row is a list on one line with `rport` on its lower value is
answered from the client-port socket, the way to the port its top value
names, where the commit before answered it from the socket it had come
on. On the dev phone the binary of this range's last commit answered a
MESSAGE the phone sent to its own protected server port from the
client-port socket where its Via row was such a list, and from the
socket it had come on where its one Via came as `v: …;RPORT` (the
capture's phone-form6, inject-bench.log).
2026-11-22 17:35:11 +01:00
|
|
|
// 488 to one we did not: each Via row echoed in order (RFC 3261
|
|
|
|
|
// 8.2.6.2), From, To with
|
messages: the SIP MESSAGE of SMS over IP, +g.3gpp.smsip behind a context flag
TS 24.341 5.3.1.2 has a submission travel as a SIP MESSAGE to the
service centre's PSI with the RP-DATA as an application/vnd.3gpp.sms
body, and 5.3.2.4 the delivery report as one to the IP-SM-GW with
In-Reply-To naming the delivered MESSAGE: BuildMessageSms builds both,
out of dialog over the registration's Route with the same
P-Preferred-Identity, P-Access-Network-Info and sec-agree headers as the
INVITE and the reg-event SUBSCRIBE, and no Contact (RFC 3428 4). The REGISTER Contact carries +g.3gpp.smsip only
when Context::smsOverIp says so — the tag routes terminating short
messages to imsd (the 2026-10-17 bench's mt-last-message-in.raw), which
at this commit still answers them 200 and logs them — after the ICSI.
BuildResponse builds a final for an inbound request outside a call (the
488 of 5.3.1.2 for a report that is not ours, or a 200); the shell does
not call it in this commit — it still answers a MESSAGE with
BuildResponse200.
tests/Messages pins the Contact with the tag, the MESSAGE's headers and
body bytes both ways, the absence of Contact, and the 488.
BuildResponse echoes each Via: line and the To, with a tag added unless
the To already holds `tag=` (RFC 3261 8.2.6.2 has a UAS add a tag to a
To that carries none); tests/Messages pins the 488 and a 200.
ScPsi gives the service centre's PSI from its international number
(5.3.1.2 NOTE 1: the SC's E.164 number as a SIP URI with user=phone, or
a tel URI; the home domain is imsd's choice); RuriFor's dialled-number guess, whose trunk 0 is Dutch, has no
place there. Pinned in tests/Messages.
2026-10-16 23:13:39 +02:00
|
|
|
// `toTag` added unless the request's To holds `tag=` (8.2.6.2 again),
|
messages, imsd: P-Access-Network-Info in responses and in requests within a dialog
imsd named the serving cell in its REGISTERs, the reg-event SUBSCRIBE,
an INVITE and a MESSAGE. TS 24.229 5.1.2A.1.1 and 5.1.2A.2 have a UE
insert the header into a request within a dialog as well, and into a
response within a dialog, to a request for a dialog and to a standalone
method: not into a CANCEL nor into a response to one, and into an ACK
at its choice. A PRACK and a BYE carry it now, and every response imsd
builds but a 100 Trying, the 200 to a CANCEL and the transport's 400 to
a request cut short.
The server ports (the protected ports' TCP listeners and UDP sockets)
take a request from any source (AGENTS.md §12), and the header is the
phone's cell. What they send in answer keeps it only on the two paths
of the SA pair the shell has on record, and loses it anywhere else.
Tested: the suites, and the daemon of this commit on the workstation
against a stand-in network that asks it from both sides of those paths
(the fp6 journal's 2026-12-04-pani-in-dialog capture, bench/), and on
the dev phone on KPN, where its answers to a text's two requests and a
call's 180 and 200 carried the header inside the SA pair (the
journal's 2026-12-04-imsd-1.2.7-range-on-dev-phone capture).
2026-12-04 23:20:38 +01:00
|
|
|
// Call-ID and CSeq, then P-Access-Network-Info where the cell is known;
|
|
|
|
|
// no body. The shell's answers to any other request
|
messages: the SIP MESSAGE of SMS over IP, +g.3gpp.smsip behind a context flag
TS 24.341 5.3.1.2 has a submission travel as a SIP MESSAGE to the
service centre's PSI with the RP-DATA as an application/vnd.3gpp.sms
body, and 5.3.2.4 the delivery report as one to the IP-SM-GW with
In-Reply-To naming the delivered MESSAGE: BuildMessageSms builds both,
out of dialog over the registration's Route with the same
P-Preferred-Identity, P-Access-Network-Info and sec-agree headers as the
INVITE and the reg-event SUBSCRIBE, and no Contact (RFC 3428 4). The REGISTER Contact carries +g.3gpp.smsip only
when Context::smsOverIp says so — the tag routes terminating short
messages to imsd (the 2026-10-17 bench's mt-last-message-in.raw), which
at this commit still answers them 200 and logs them — after the ICSI.
BuildResponse builds a final for an inbound request outside a call (the
488 of 5.3.1.2 for a report that is not ours, or a 200); the shell does
not call it in this commit — it still answers a MESSAGE with
BuildResponse200.
tests/Messages pins the Contact with the tag, the MESSAGE's headers and
body bytes both ways, the absence of Contact, and the 488.
BuildResponse echoes each Via: line and the To, with a tag added unless
the To already holds `tag=` (RFC 3261 8.2.6.2 has a UAS add a tag to a
To that carries none); tests/Messages pins the 488 and a 200.
ScPsi gives the service centre's PSI from its international number
(5.3.1.2 NOTE 1: the SC's E.164 number as a SIP URI with user=phone, or
a tel URI; the home domain is imsd's choice); RuriFor's dialled-number guess, whose trunk 0 is Dutch, has no
place there. Pinned in tests/Messages.
2026-10-16 23:13:39 +02:00
|
|
|
// outside a call but an INVITE (an OPTIONS, a NOTIFY, a method it does
|
messages, imsd: P-Access-Network-Info in responses and in requests within a dialog
imsd named the serving cell in its REGISTERs, the reg-event SUBSCRIBE,
an INVITE and a MESSAGE. TS 24.229 5.1.2A.1.1 and 5.1.2A.2 have a UE
insert the header into a request within a dialog as well, and into a
response within a dialog, to a request for a dialog and to a standalone
method: not into a CANCEL nor into a response to one, and into an ACK
at its choice. A PRACK and a BYE carry it now, and every response imsd
builds but a 100 Trying, the 200 to a CANCEL and the transport's 400 to
a request cut short.
The server ports (the protected ports' TCP listeners and UDP sockets)
take a request from any source (AGENTS.md §12), and the header is the
phone's cell. What they send in answer keeps it only on the two paths
of the SA pair the shell has on record, and loses it anywhere else.
Tested: the suites, and the daemon of this commit on the workstation
against a stand-in network that asks it from both sides of those paths
(the fp6 journal's 2026-12-04-pani-in-dialog capture, bench/), and on
the dev phone on KPN, where its answers to a text's two requests and a
call's 180 and 200 carried the header inside the SA pair (the
journal's 2026-12-04-imsd-1.2.7-range-on-dev-phone capture).
2026-12-04 23:20:38 +01:00
|
|
|
// not handle) keep BuildResponse200's hop-by-hop shape, as before,
|
|
|
|
|
// unless the request is cut short: BuildBadRequest's 400 has this one.
|
|
|
|
|
std::string BuildResponse(const Context& c, std::string_view request, int code, std::string_view text, std::string_view toTag) {
|
messages: the SIP MESSAGE of SMS over IP, +g.3gpp.smsip behind a context flag
TS 24.341 5.3.1.2 has a submission travel as a SIP MESSAGE to the
service centre's PSI with the RP-DATA as an application/vnd.3gpp.sms
body, and 5.3.2.4 the delivery report as one to the IP-SM-GW with
In-Reply-To naming the delivered MESSAGE: BuildMessageSms builds both,
out of dialog over the registration's Route with the same
P-Preferred-Identity, P-Access-Network-Info and sec-agree headers as the
INVITE and the reg-event SUBSCRIBE, and no Contact (RFC 3428 4). The REGISTER Contact carries +g.3gpp.smsip only
when Context::smsOverIp says so — the tag routes terminating short
messages to imsd (the 2026-10-17 bench's mt-last-message-in.raw), which
at this commit still answers them 200 and logs them — after the ICSI.
BuildResponse builds a final for an inbound request outside a call (the
488 of 5.3.1.2 for a report that is not ours, or a 200); the shell does
not call it in this commit — it still answers a MESSAGE with
BuildResponse200.
tests/Messages pins the Contact with the tag, the MESSAGE's headers and
body bytes both ways, the absence of Contact, and the 488.
BuildResponse echoes each Via: line and the To, with a tag added unless
the To already holds `tag=` (RFC 3261 8.2.6.2 has a UAS add a tag to a
To that carries none); tests/Messages pins the 488 and a 200.
ScPsi gives the service centre's PSI from its international number
(5.3.1.2 NOTE 1: the SC's E.164 number as a SIP URI with user=phone, or
a tel URI; the home domain is imsd's choice); RuriFor's dialled-number guess, whose trunk 0 is Dutch, has no
place there. Pinned in tests/Messages.
2026-10-16 23:13:39 +02:00
|
|
|
std::vector<std::string> l = {std::format("SIP/2.0 {} {}", code, text)};
|
|
|
|
|
for (auto v : imsd::sip::Headers(request, "Via")) l.push_back(std::format("Via: {}", v));
|
|
|
|
|
if (auto f = imsd::sip::Header(request, "From")) l.push_back(std::format("From: {}", *f));
|
|
|
|
|
if (auto t = imsd::sip::Header(request, "To")) {
|
|
|
|
|
if (t->contains("tag=")) l.push_back(std::format("To: {}", *t));
|
|
|
|
|
else l.push_back(std::format("To: {};tag={}", *t, toTag));
|
|
|
|
|
}
|
|
|
|
|
if (auto ci = imsd::sip::Header(request, "Call-ID")) l.push_back(std::format("Call-ID: {}", *ci));
|
|
|
|
|
if (auto cs = imsd::sip::Header(request, "CSeq")) l.push_back(std::format("CSeq: {}", *cs));
|
messages, imsd: P-Access-Network-Info in responses and in requests within a dialog
imsd named the serving cell in its REGISTERs, the reg-event SUBSCRIBE,
an INVITE and a MESSAGE. TS 24.229 5.1.2A.1.1 and 5.1.2A.2 have a UE
insert the header into a request within a dialog as well, and into a
response within a dialog, to a request for a dialog and to a standalone
method: not into a CANCEL nor into a response to one, and into an ACK
at its choice. A PRACK and a BYE carry it now, and every response imsd
builds but a 100 Trying, the 200 to a CANCEL and the transport's 400 to
a request cut short.
The server ports (the protected ports' TCP listeners and UDP sockets)
take a request from any source (AGENTS.md §12), and the header is the
phone's cell. What they send in answer keeps it only on the two paths
of the SA pair the shell has on record, and loses it anywhere else.
Tested: the suites, and the daemon of this commit on the workstation
against a stand-in network that asks it from both sides of those paths
(the fp6 journal's 2026-12-04-pani-in-dialog capture, bench/), and on
the dev phone on KPN, where its answers to a text's two requests and a
call's 180 and 200 carried the header inside the SA pair (the
journal's 2026-12-04-imsd-1.2.7-range-on-dev-phone capture).
2026-12-04 23:20:38 +01:00
|
|
|
detail::AccessInfo(l, c, request, code);
|
messages: the SIP MESSAGE of SMS over IP, +g.3gpp.smsip behind a context flag
TS 24.341 5.3.1.2 has a submission travel as a SIP MESSAGE to the
service centre's PSI with the RP-DATA as an application/vnd.3gpp.sms
body, and 5.3.2.4 the delivery report as one to the IP-SM-GW with
In-Reply-To naming the delivered MESSAGE: BuildMessageSms builds both,
out of dialog over the registration's Route with the same
P-Preferred-Identity, P-Access-Network-Info and sec-agree headers as the
INVITE and the reg-event SUBSCRIBE, and no Contact (RFC 3428 4). The REGISTER Contact carries +g.3gpp.smsip only
when Context::smsOverIp says so — the tag routes terminating short
messages to imsd (the 2026-10-17 bench's mt-last-message-in.raw), which
at this commit still answers them 200 and logs them — after the ICSI.
BuildResponse builds a final for an inbound request outside a call (the
488 of 5.3.1.2 for a report that is not ours, or a 200); the shell does
not call it in this commit — it still answers a MESSAGE with
BuildResponse200.
tests/Messages pins the Contact with the tag, the MESSAGE's headers and
body bytes both ways, the absence of Contact, and the 488.
BuildResponse echoes each Via: line and the To, with a tag added unless
the To already holds `tag=` (RFC 3261 8.2.6.2 has a UAS add a tag to a
To that carries none); tests/Messages pins the 488 and a 200.
ScPsi gives the service centre's PSI from its international number
(5.3.1.2 NOTE 1: the SC's E.164 number as a SIP URI with user=phone, or
a tel URI; the home domain is imsd's choice); RuriFor's dialled-number guess, whose trunk 0 is Dutch, has no
place there. Pinned in tests/Messages.
2026-10-16 23:13:39 +02:00
|
|
|
l.push_back("Content-Length: 0");
|
|
|
|
|
return std::format("{}\r\n", detail::Join(l));
|
|
|
|
|
}
|
|
|
|
|
|
sip, messages, imsd: the client flow answers a request datagram cut short of the Content-Length it reads with a 400, unless an ACK or a CANCEL
RFC 3261 18.3 on a datagram that ends before its message's body does:
"If the message is a response, it MUST be discarded. If the message is
a request, the element SHOULD generate a 400 (Bad Request) response."
The client flow's datagram read ignored either, where the
Content-Length it read said more bytes than came, with one line, `UDP:
datagram truncated (…) — ignored`.
CutShortRequest gives the header section of a request whose datagram
has fewer body bytes than the Content-Length read from it says, and
BuildBadRequest the 400 for it, in the shape of BuildResponse's finals:
every Via row echoed, the To tagged unless it holds a tag. The client
flow sends that 400 and its line ends `— answered 400`. Such a request
is known by its first line, which ends with the SIP version where a
status line begins with it (7.1, 7.2), in upper or in lower case: a
response cut short so, bytes that are no SIP cut short so and a
datagram without an end of headers are ignored as before. imsd keeps
nothing of a request it answers so, and follows 8.2.7 for an element
that answers without state: a To tag it adds is made from the
request's header section and a secret drawn once for the run, "in a
manner that will generate the same tag for the same request
consistently", and an ACK or a CANCEL cut short so gets no answer. The
server ports and the init port hand a datagram on as it came, as
before this commit.
Tested by tests/Sip (CutShortRequest: the header section of a request
cut short, with line breaks before it, under a compact Content-Length
and with its version in lower case; nothing for a body that is all
there, no Content-Length, no end of headers, a response in either
letter case, bytes that are no SIP, an ACK, a CANCEL) and by
tests/Messages (the 400 by its lines, its tag against Python's
hashlib; the same request, the same 400; another request or another
secret, another tag). On the workstation's loopback, SipFlow built
around a bench main (the fp6 journal's 2026-11-20-issues-2-5 capture,
cut-short, at this commit and at the one before): a MESSAGE with five
body bytes of nine draws the 400, and the same one when it is sent
again, where the commit before ignored it; a response cut short the
same way, in upper and in lower case, an ACK, a CANCEL, bytes that are
no SIP and a request without an end of headers draw nothing at either
commit; the MESSAGE with its Content-Length folded is handed on at
either commit, a folded value being read by its first line; a whole
request sent after them is returned by the flow.
2026-11-22 17:35:11 +01:00
|
|
|
// The 400 to a request whose datagram ended before its body did (RFC
|
|
|
|
|
// 3261 18.3), from the request's header section
|
|
|
|
|
// (imsd::sip::CutShortRequest), as BuildResponse makes a final. imsd
|
messages, imsd: P-Access-Network-Info in responses and in requests within a dialog
imsd named the serving cell in its REGISTERs, the reg-event SUBSCRIBE,
an INVITE and a MESSAGE. TS 24.229 5.1.2A.1.1 and 5.1.2A.2 have a UE
insert the header into a request within a dialog as well, and into a
response within a dialog, to a request for a dialog and to a standalone
method: not into a CANCEL nor into a response to one, and into an ACK
at its choice. A PRACK and a BYE carry it now, and every response imsd
builds but a 100 Trying, the 200 to a CANCEL and the transport's 400 to
a request cut short.
The server ports (the protected ports' TCP listeners and UDP sockets)
take a request from any source (AGENTS.md §12), and the header is the
phone's cell. What they send in answer keeps it only on the two paths
of the SA pair the shell has on record, and loses it anywhere else.
Tested: the suites, and the daemon of this commit on the workstation
against a stand-in network that asks it from both sides of those paths
(the fp6 journal's 2026-12-04-pani-in-dialog capture, bench/), and on
the dev phone on KPN, where its answers to a text's two requests and a
call's 180 and 200 carried the header inside the SA pair (the
journal's 2026-12-04-imsd-1.2.7-range-on-dev-phone capture).
2026-12-04 23:20:38 +01:00
|
|
|
// keeps nothing of such a request, so the tag it adds to a To that holds
|
|
|
|
|
// none is made from the request itself, "in a manner that will generate
|
|
|
|
|
// the same tag for the same request consistently" (8.2.7), and from
|
|
|
|
|
// `secret`, which the
|
sip, messages, imsd: the client flow answers a request datagram cut short of the Content-Length it reads with a 400, unless an ACK or a CANCEL
RFC 3261 18.3 on a datagram that ends before its message's body does:
"If the message is a response, it MUST be discarded. If the message is
a request, the element SHOULD generate a 400 (Bad Request) response."
The client flow's datagram read ignored either, where the
Content-Length it read said more bytes than came, with one line, `UDP:
datagram truncated (…) — ignored`.
CutShortRequest gives the header section of a request whose datagram
has fewer body bytes than the Content-Length read from it says, and
BuildBadRequest the 400 for it, in the shape of BuildResponse's finals:
every Via row echoed, the To tagged unless it holds a tag. The client
flow sends that 400 and its line ends `— answered 400`. Such a request
is known by its first line, which ends with the SIP version where a
status line begins with it (7.1, 7.2), in upper or in lower case: a
response cut short so, bytes that are no SIP cut short so and a
datagram without an end of headers are ignored as before. imsd keeps
nothing of a request it answers so, and follows 8.2.7 for an element
that answers without state: a To tag it adds is made from the
request's header section and a secret drawn once for the run, "in a
manner that will generate the same tag for the same request
consistently", and an ACK or a CANCEL cut short so gets no answer. The
server ports and the init port hand a datagram on as it came, as
before this commit.
Tested by tests/Sip (CutShortRequest: the header section of a request
cut short, with line breaks before it, under a compact Content-Length
and with its version in lower case; nothing for a body that is all
there, no Content-Length, no end of headers, a response in either
letter case, bytes that are no SIP, an ACK, a CANCEL) and by
tests/Messages (the 400 by its lines, its tag against Python's
hashlib; the same request, the same 400; another request or another
secret, another tag). On the workstation's loopback, SipFlow built
around a bench main (the fp6 journal's 2026-11-20-issues-2-5 capture,
cut-short, at this commit and at the one before): a MESSAGE with five
body bytes of nine draws the 400, and the same one when it is sent
again, where the commit before ignored it; a response cut short the
same way, in upper and in lower case, an ACK, a CANCEL, bytes that are
no SIP and a request without an end of headers draw nothing at either
commit; the MESSAGE with its Content-Length folded is handed on at
either commit, a folded value being read by its first line; a whole
request sent after them is returned by the flow.
2026-11-22 17:35:11 +01:00
|
|
|
// caller draws once for the run: the randomness 19.3 asks of a tag. It
|
|
|
|
|
// is the first sixteen hex digits of the MD5 of the secret and that
|
messages, imsd: P-Access-Network-Info in responses and in requests within a dialog
imsd named the serving cell in its REGISTERs, the reg-event SUBSCRIBE,
an INVITE and a MESSAGE. TS 24.229 5.1.2A.1.1 and 5.1.2A.2 have a UE
insert the header into a request within a dialog as well, and into a
response within a dialog, to a request for a dialog and to a standalone
method: not into a CANCEL nor into a response to one, and into an ACK
at its choice. A PRACK and a BYE carry it now, and every response imsd
builds but a 100 Trying, the 200 to a CANCEL and the transport's 400 to
a request cut short.
The server ports (the protected ports' TCP listeners and UDP sockets)
take a request from any source (AGENTS.md §12), and the header is the
phone's cell. What they send in answer keeps it only on the two paths
of the SA pair the shell has on record, and loses it anywhere else.
Tested: the suites, and the daemon of this commit on the workstation
against a stand-in network that asks it from both sides of those paths
(the fp6 journal's 2026-12-04-pani-in-dialog capture, bench/), and on
the dev phone on KPN, where its answers to a text's two requests and a
call's 180 and 200 carried the header inside the SA pair (the
journal's 2026-12-04-imsd-1.2.7-range-on-dev-phone capture).
2026-12-04 23:20:38 +01:00
|
|
|
// header section. The transport answers with it, without the
|
|
|
|
|
// registration's context: it carries no P-Access-Network-Info.
|
sip, messages, imsd: the client flow answers a request datagram cut short of the Content-Length it reads with a 400, unless an ACK or a CANCEL
RFC 3261 18.3 on a datagram that ends before its message's body does:
"If the message is a response, it MUST be discarded. If the message is
a request, the element SHOULD generate a 400 (Bad Request) response."
The client flow's datagram read ignored either, where the
Content-Length it read said more bytes than came, with one line, `UDP:
datagram truncated (…) — ignored`.
CutShortRequest gives the header section of a request whose datagram
has fewer body bytes than the Content-Length read from it says, and
BuildBadRequest the 400 for it, in the shape of BuildResponse's finals:
every Via row echoed, the To tagged unless it holds a tag. The client
flow sends that 400 and its line ends `— answered 400`. Such a request
is known by its first line, which ends with the SIP version where a
status line begins with it (7.1, 7.2), in upper or in lower case: a
response cut short so, bytes that are no SIP cut short so and a
datagram without an end of headers are ignored as before. imsd keeps
nothing of a request it answers so, and follows 8.2.7 for an element
that answers without state: a To tag it adds is made from the
request's header section and a secret drawn once for the run, "in a
manner that will generate the same tag for the same request
consistently", and an ACK or a CANCEL cut short so gets no answer. The
server ports and the init port hand a datagram on as it came, as
before this commit.
Tested by tests/Sip (CutShortRequest: the header section of a request
cut short, with line breaks before it, under a compact Content-Length
and with its version in lower case; nothing for a body that is all
there, no Content-Length, no end of headers, a response in either
letter case, bytes that are no SIP, an ACK, a CANCEL) and by
tests/Messages (the 400 by its lines, its tag against Python's
hashlib; the same request, the same 400; another request or another
secret, another tag). On the workstation's loopback, SipFlow built
around a bench main (the fp6 journal's 2026-11-20-issues-2-5 capture,
cut-short, at this commit and at the one before): a MESSAGE with five
body bytes of nine draws the 400, and the same one when it is sent
again, where the commit before ignored it; a response cut short the
same way, in upper and in lower case, an ACK, a CANCEL, bytes that are
no SIP and a request without an end of headers draw nothing at either
commit; the MESSAGE with its Content-Length folded is handed on at
either commit, a folded value being read by its first line; a whole
request sent after them is returned by the flow.
2026-11-22 17:35:11 +01:00
|
|
|
std::string BuildBadRequest(std::string_view head, std::string_view secret) {
|
|
|
|
|
return BuildResponse({}, head, 400, "Bad Request", imsd::util::Md5::HexDigest(std::format("{}{}", secret, head)).substr(0, 16));
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-22 22:53:28 +02:00
|
|
|
// A 200 OK for an inbound request: echo Via/From/To/Call-ID/CSeq in order,
|
|
|
|
|
// then either an SDP answer (with our Contact) or an empty body.
|
messages, imsd: P-Access-Network-Info in responses and in requests within a dialog
imsd named the serving cell in its REGISTERs, the reg-event SUBSCRIBE,
an INVITE and a MESSAGE. TS 24.229 5.1.2A.1.1 and 5.1.2A.2 have a UE
insert the header into a request within a dialog as well, and into a
response within a dialog, to a request for a dialog and to a standalone
method: not into a CANCEL nor into a response to one, and into an ACK
at its choice. A PRACK and a BYE carry it now, and every response imsd
builds but a 100 Trying, the 200 to a CANCEL and the transport's 400 to
a request cut short.
The server ports (the protected ports' TCP listeners and UDP sockets)
take a request from any source (AGENTS.md §12), and the header is the
phone's cell. What they send in answer keeps it only on the two paths
of the SA pair the shell has on record, and loses it anywhere else.
Tested: the suites, and the daemon of this commit on the workstation
against a stand-in network that asks it from both sides of those paths
(the fp6 journal's 2026-12-04-pani-in-dialog capture, bench/), and on
the dev phone on KPN, where its answers to a text's two requests and a
call's 180 and 200 carried the header inside the SA pair (the
journal's 2026-12-04-imsd-1.2.7-range-on-dev-phone capture).
2026-12-04 23:20:38 +01:00
|
|
|
// `extra` appends headers (Session-Expires echo for refreshes...), and
|
|
|
|
|
// P-Access-Network-Info follows them where the cell is known, unless
|
|
|
|
|
// the request is a CANCEL.
|
2026-07-22 22:53:28 +02:00
|
|
|
std::string BuildResponse200(const Context& c, std::string_view request, std::optional<std::string_view> sdp = std::nullopt, std::span<const std::string> extra = {}) {
|
|
|
|
|
std::vector<std::string> l = {"SIP/2.0 200 OK"};
|
|
|
|
|
for (std::string_view h : {"Via", "From", "To", "Call-ID", "CSeq"})
|
|
|
|
|
if (auto v = imsd::sip::Header(request, h)) l.push_back(std::format("{}: {}", h, *v));
|
|
|
|
|
for (const auto& e : extra)
|
|
|
|
|
l.push_back(e);
|
messages, imsd: P-Access-Network-Info in responses and in requests within a dialog
imsd named the serving cell in its REGISTERs, the reg-event SUBSCRIBE,
an INVITE and a MESSAGE. TS 24.229 5.1.2A.1.1 and 5.1.2A.2 have a UE
insert the header into a request within a dialog as well, and into a
response within a dialog, to a request for a dialog and to a standalone
method: not into a CANCEL nor into a response to one, and into an ACK
at its choice. A PRACK and a BYE carry it now, and every response imsd
builds but a 100 Trying, the 200 to a CANCEL and the transport's 400 to
a request cut short.
The server ports (the protected ports' TCP listeners and UDP sockets)
take a request from any source (AGENTS.md §12), and the header is the
phone's cell. What they send in answer keeps it only on the two paths
of the SA pair the shell has on record, and loses it anywhere else.
Tested: the suites, and the daemon of this commit on the workstation
against a stand-in network that asks it from both sides of those paths
(the fp6 journal's 2026-12-04-pani-in-dialog capture, bench/), and on
the dev phone on KPN, where its answers to a text's two requests and a
call's 180 and 200 carried the header inside the SA pair (the
journal's 2026-12-04-imsd-1.2.7-range-on-dev-phone capture).
2026-12-04 23:20:38 +01:00
|
|
|
detail::AccessInfo(l, c, request, 200);
|
2026-07-22 22:53:28 +02:00
|
|
|
if (sdp) {
|
|
|
|
|
l.push_back(std::format("Contact: <sip:{}@{}>", ContactUser(c), imsd::util::HostPort(c.local, c.portUs)));
|
|
|
|
|
l.push_back("Content-Type: application/sdp");
|
|
|
|
|
l.push_back(std::format("Content-Length: {}", sdp->size()));
|
|
|
|
|
l.push_back("");
|
|
|
|
|
return detail::Join(l) + std::string(*sdp);
|
|
|
|
|
}
|
|
|
|
|
l.push_back("Content-Length: 0");
|
|
|
|
|
return std::format("{}\r\n", detail::Join(l));
|
|
|
|
|
}
|
messages: the SIP MESSAGE of SMS over IP, +g.3gpp.smsip behind a context flag
TS 24.341 5.3.1.2 has a submission travel as a SIP MESSAGE to the
service centre's PSI with the RP-DATA as an application/vnd.3gpp.sms
body, and 5.3.2.4 the delivery report as one to the IP-SM-GW with
In-Reply-To naming the delivered MESSAGE: BuildMessageSms builds both,
out of dialog over the registration's Route with the same
P-Preferred-Identity, P-Access-Network-Info and sec-agree headers as the
INVITE and the reg-event SUBSCRIBE, and no Contact (RFC 3428 4). The REGISTER Contact carries +g.3gpp.smsip only
when Context::smsOverIp says so — the tag routes terminating short
messages to imsd (the 2026-10-17 bench's mt-last-message-in.raw), which
at this commit still answers them 200 and logs them — after the ICSI.
BuildResponse builds a final for an inbound request outside a call (the
488 of 5.3.1.2 for a report that is not ours, or a 200); the shell does
not call it in this commit — it still answers a MESSAGE with
BuildResponse200.
tests/Messages pins the Contact with the tag, the MESSAGE's headers and
body bytes both ways, the absence of Contact, and the 488.
BuildResponse echoes each Via: line and the To, with a tag added unless
the To already holds `tag=` (RFC 3261 8.2.6.2 has a UAS add a tag to a
To that carries none); tests/Messages pins the 488 and a 200.
ScPsi gives the service centre's PSI from its international number
(5.3.1.2 NOTE 1: the SC's E.164 number as a SIP URI with user=phone, or
a tel URI; the home domain is imsd's choice); RuriFor's dialled-number guess, whose trunk 0 is Dutch, has no
place there. Pinned in tests/Messages.
2026-10-16 23:13:39 +02:00
|
|
|
|
|
|
|
|
// ---- SMS over IP ----------------------------------------------------
|
|
|
|
|
|
|
|
|
|
constexpr std::string_view SmsContentType = "application/vnd.3gpp.sms";
|
|
|
|
|
|
|
|
|
|
// A SIP MESSAGE carrying an SM-RL message (TS 24.011) as its body — the
|
|
|
|
|
// RP-DATA of a submission to the service centre's PSI (TS 24.341
|
|
|
|
|
// 5.3.1.2: `target` is the Request-URI and the To — `ScPsi`),
|
|
|
|
|
// or the RP-ACK/RP-ERROR of a delivery report to the IP-SM-GW (5.3.2.4:
|
|
|
|
|
// `target` is the IP-SM-GW, `inReplyTo` the delivered MESSAGE's
|
|
|
|
|
// Call-ID). Out of dialog: a Call-ID of its own, CSeq 1,
|
|
|
|
|
// the registration's Route, our public identity in From, and no Contact
|
|
|
|
|
// (RFC 3428 §4). The same P-Preferred-Identity, P-Access-Network-Info
|
|
|
|
|
// and sec-agree headers as the INVITE and the reg-event SUBSCRIBE.
|
|
|
|
|
std::string BuildMessageSms(const Context& c, std::string_view target, std::string_view callid, std::string_view ftag, std::string_view branch, std::span<const std::uint8_t> rp, std::string_view inReplyTo = {}) {
|
|
|
|
|
std::vector<std::string> l = {
|
|
|
|
|
std::format("MESSAGE {} SIP/2.0", target),
|
|
|
|
|
ViaLine(c, c.transport, c.portUs, branch),
|
|
|
|
|
"Max-Forwards: 70",
|
|
|
|
|
std::format("Route: {}", c.route),
|
|
|
|
|
std::format("From: <{}>;tag={}", CallerId(c), ftag),
|
|
|
|
|
std::format("To: <{}>", target),
|
|
|
|
|
std::format("Call-ID: {}", callid),
|
|
|
|
|
"CSeq: 1 MESSAGE",
|
|
|
|
|
};
|
|
|
|
|
if (!inReplyTo.empty()) l.push_back(std::format("In-Reply-To: {}", inReplyTo));
|
|
|
|
|
if (!c.ppi.empty()) l.push_back(std::format("P-Preferred-Identity: <{}>", c.ppi));
|
|
|
|
|
if (!c.panInfo.empty()) l.push_back(std::format("P-Access-Network-Info: {}", c.panInfo));
|
|
|
|
|
l.push_back(std::format("Security-Verify: {}", c.securityServer));
|
|
|
|
|
l.push_back("Proxy-Require: sec-agree");
|
|
|
|
|
l.push_back("Require: sec-agree");
|
|
|
|
|
l.push_back(std::format("Content-Type: {}", SmsContentType));
|
|
|
|
|
l.push_back(std::format("Content-Length: {}", rp.size()));
|
|
|
|
|
l.push_back("");
|
|
|
|
|
std::string out = detail::Join(l);
|
project, docs: the linter goes
The Catcrafts house-style rules (Crafter.LintRules) rode in project.cpp
as a dependency crafter-build cloned once and built on every load; the tree
never passed them (111 findings on the 1.1.0 tree, 156 on an early freeze
of this range (2e87e26), 100 of the 156 the shared naming rule's, each
for a variable whose name begins with a capital — the
fp6 journal's lint captures, lint-tip-2e87e26), CI never ran them,
and AGENTS.md kept a census nobody acted on. Out: the dependency, the
import and the five AddRules calls, the lint-disable comments in 25
files and on 4 lines, §4.2 and the census of §6 — now "Code style", the
three rules that were never the linter's; the cross-compiling section
becomes §4.2 and its six citations — §4.1's sysroot sentence, §9.1, the CI
workflow, the APKBUILD, build-package.sh, make-sysroot.sh — follow. Jorijn, 2026-10-17: "just
remove the linter, we are ignoring it anyway".
2026-10-18 00:05:06 +02:00
|
|
|
out.append(reinterpret_cast<const char*>(rp.data()), rp.size());
|
messages: the SIP MESSAGE of SMS over IP, +g.3gpp.smsip behind a context flag
TS 24.341 5.3.1.2 has a submission travel as a SIP MESSAGE to the
service centre's PSI with the RP-DATA as an application/vnd.3gpp.sms
body, and 5.3.2.4 the delivery report as one to the IP-SM-GW with
In-Reply-To naming the delivered MESSAGE: BuildMessageSms builds both,
out of dialog over the registration's Route with the same
P-Preferred-Identity, P-Access-Network-Info and sec-agree headers as the
INVITE and the reg-event SUBSCRIBE, and no Contact (RFC 3428 4). The REGISTER Contact carries +g.3gpp.smsip only
when Context::smsOverIp says so — the tag routes terminating short
messages to imsd (the 2026-10-17 bench's mt-last-message-in.raw), which
at this commit still answers them 200 and logs them — after the ICSI.
BuildResponse builds a final for an inbound request outside a call (the
488 of 5.3.1.2 for a report that is not ours, or a 200); the shell does
not call it in this commit — it still answers a MESSAGE with
BuildResponse200.
tests/Messages pins the Contact with the tag, the MESSAGE's headers and
body bytes both ways, the absence of Contact, and the 488.
BuildResponse echoes each Via: line and the To, with a tag added unless
the To already holds `tag=` (RFC 3261 8.2.6.2 has a UAS add a tag to a
To that carries none); tests/Messages pins the 488 and a 200.
ScPsi gives the service centre's PSI from its international number
(5.3.1.2 NOTE 1: the SC's E.164 number as a SIP URI with user=phone, or
a tel URI; the home domain is imsd's choice); RuriFor's dialled-number guess, whose trunk 0 is Dutch, has no
place there. Pinned in tests/Messages.
2026-10-16 23:13:39 +02:00
|
|
|
return out;
|
|
|
|
|
}
|
2026-07-22 22:53:28 +02:00
|
|
|
}
|