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.
This commit is contained in:
Jorijn van der Graaf 2026-11-23 19:32:58 +01:00
commit 610d8e1300
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
4 changed files with 401 additions and 51 deletions

177
AGENTS.md
View file

@ -15,8 +15,10 @@ bug — fix it in the same change.
imsd is a userspace IMS/VoLTE client for mainline Linux phones. The modem
firmware on such phones brings up the IMS PDN but cannot carry call audio, so
imsd does the whole IMS client itself: SIP registration with USIM AKA
authentication over kernel IPsec (ESP), call signalling for outgoing and
imsd does the whole IMS client itself: SIP registration — by default under
the identity the card's ISIM holds, else the one derived from the IMSI, with
AKA authentication in that ISIM, else in the USIM (§7.2) — over
kernel IPsec (ESP), call signalling for outgoing and
incoming calls, and the RTP media plane (AMR-WB, AMR, G.711) wired into
PipeWire, and since 1.2.0 short messages over IMS (TS 24.341: the SIP MESSAGE
carrying an RP-DATA, a store, the Messaging interface —
@ -25,7 +27,8 @@ and GNOME Mobile — places and receives real VoLTE calls on a commercial
network through it.
It talks to the modem through ModemManager (`mmcli`) and `qmicli` over QRTR
(the USIM AKA run goes through a UIM logical channel) and, for the P-CSCF list
(the AKA run and the read of an ISIM's identity files go through UIM logical
channels) and, for the P-CSCF list
the network put in the ims PDN's PCO, through its own QMI WDS client
(`imsd-pcscf`, a raw QRTR socket bound to the bearer's mux data port — §13),
installs its security
@ -139,7 +142,23 @@ both sides (1.0.0) is unbenched: no capture stages a holds file whose `pdn` is
spelled unlike `LOCAL`). For the schedule, a reducer next to
`RegRecovery`, with a Subscription-State parser in `Imsd:Sip` pinned by
`tests/Sip`, is the follow-up that moves it; a pure format/parse pair in
`Imsd:Engine` is the holds file's. SMS over IP (1.2.0, §7.9) keeps in the
`Imsd:Engine` is the holds file's. The card's side (1.2.3, §7.2 step 2)
keeps in the shell the choice of the identity (`BringUp`: `ISIM=0`, no ISIM
application in the USIM's slot, an ISIM that gave none, and `FreshRecover`'s
asking of such an ISIM again), the read itself
(`ReadIsimIdentity`: the three files and their order, record 1 of EF_IMPU,
a SELECT's `61 XX` taken for the file selected, what its `why` says), the
channel (`UimChannel`: the open, the exchanges, the two follow-ups at most,
the close), the application the challenge goes to with its
fall-back to the USIM and the journal's words for an answer that is no
result (`Authenticate`, `AkaIn`), and which identity a
registration carries: the card's at a fresh registration, the one it was
made under at a resume (`TryResume`: the state file's three names, the
derived ones for a file that has none; `FreshRegister`'s restore after a
failed attempt) — the commands, the
answers' reading, the follow-up, the value of a file and the check of the
identity are `Imsd:Aka`'s, with tests; the shell's part ran against
stand-ins on the workstation (§5). SMS over IP (1.2.0, §7.9) keeps in the
shell, among others, the service centre's source (`SMSC=` over EF_SMSP, `LoadSmsParameters`),
the three counters (TP-MR, RP-MR, the concatenation reference), their
seeding and the TP-MR file, the routing of an inbound MESSAGE
@ -527,7 +546,44 @@ Rules:
selection of a null entry is pinned in `tests/Ipsec` with Odido's answer
of 2026-07-13 to the Python stack's offer of the null cipher and ran
against a stand-in P-CSCF on the workstation as far as the `SA:` line;
no capture holds a registration of imsd's over a null SA pair); a
no capture holds a registration of imsd's over a null SA pair); an
ISIM's identity and an AKA in an ISIM (1.2.3; the dev phone's card has no
ISIM: the daemon ran its registration on the workstation against a
stand-in for qmicli and a stand-in network that answers a first REGISTER
of any other names than 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: a card without an
ISIM, an ISIM that holds names of the operator's own and one that holds
the derived names, `ISIM=0`, another slot's ISIM, an ISIM listed `ready`
or with no state; an ISIM that gives no identity (no channel, no answer
to a SELECT, to a read or to the GET RESPONSE after one, no EF_DOMAIN,
a refused read, an
unused or a `tel:` first record of EF_IMPU, a private identity without
a realm); reads answered `6C XX` or `61 XX` first, one after the other,
or `61 XX` to every GET RESPONSE (two are sent, then the read counts as
failed), reads that end `91 XX` or `62 82`, and a length that counts
the padding in; channels 4 and 5; an ISIM that refuses the AUTHENTICATE
or answers a
synchronisation failure, the USIM asked after it; no application giving
a result (both refusing; a USIM with no channel, with an answer of one
byte, of bytes that are no result, with no answer to the AUTHENTICATE
or to the GET RESPONSE after it); the registration
made again over UDP; a second bring-up in one run at which the ISIM
gives none; resumes — of a registration of a card without an ISIM, of
one made under the ISIM's names, with and without `ISIM=0` at the
restart, of one 1.2.2 made under the derived names on a card whose ISIM
holds others (kept under the derived names; refused by a network that
knows the card's names only, and registered fresh under those), of a
state file whose identity fails the check, with a fresh target and
without (also under a forced `SIP_TRANSPORT` that is not the file's), or
lacks one of the three names — and, in a run that resumed, a
fresh registration the network refuses (the reconnect after it goes out
under the standing registration's names) and fresh registrations before
which an ISIM that gave none at the bring-up is read again: giving its
identity at the first, or none again at the first and its identity at
the second. The reads' commands ran on two reporters' cards in the
journal's probe, §13; this daemon has sent no network a REGISTER with
names it read from an ISIM, and no real ISIM an AUTHENTICATE); a
Security-Server list `PersistState` writes empty (one with
a quote, a backslash or a newline — neither KPN's nor O2's list has
one; the resume of such a file ran with a hand-edited empty key,
@ -1031,7 +1087,7 @@ The daemon's own bring-up (`BringUp()` in `main.cpp`):
registration is due or a binding a NOTIFY terminated under the attempt
write the registration off — either way the pair counts as one nothing
can resume. A pair nothing can resume (its UE address left the link, no
state file, a file another attempt left behind, written off before this start or
state file, a file another attempt left behind or whose identity fails the check, written off before this start or
given up by the engine during the attempt) is written off (`old_gone` in the holds — written as
well for a pair `RESUME=0` leaves untried) and taken
out of the kernel first (`the SA pair to … is nothing to resume: removing
@ -1040,7 +1096,37 @@ The daemon's own bring-up (`BringUp()` in `main.cpp`):
2. `qmicli -d qrtr://0 --uim-get-card-status` finds the ready USIM (slot, AID);
`mmcli -m any` gives the primary SIM path and the IMEI (for the
`+sip.instance` URN); `mmcli -i <sim>` gives IMSI and MCC/MNC, from which
the IMPI/IMPU/realm are derived; with SMS over IP on and `SMSC=` unset,
the IMPI/IMPU/realm are derived (TS 23.003 13.2 to 13.4B) — the names
of a UE without an ISIM (TS 24.229 5.1.1.1A). Since 1.2.3, unless
`ISIM=0` is set, a card with an ISIM application in the USIM's slot
(`ParseIsimApp`, in whatever state the modem lists it) is asked for the
identity that ISIM holds: over a logical channel on the application imsd
reads EF_IMPI (the private identity), EF_DOMAIN (the home domain) and
record 1 of EF_IMPU (the public identity to register, GSMA IR.92 2.2.1),
each with a SELECT by file identifier and a read, and takes the three
when `IsimIdentity` accepts them (`identity: the ISIM's (application
state …): …`, with whether they are the names the IMSI gives). Then the
ISIM is asked for the answer to the challenge (step 4). When the card
has no ISIM, when `ISIM=0` is set, or when the ISIM gave no identity,
the derived identity and the USIM stand, as in 1.2.2,
and the `identity: derived from the IMSI …` line says which of the three
and, for the last, what was missing (§7.8). The read is made at every
bring-up of such a card, a resume's too, and at each recovery by a
fresh registration later in the same run (`FreshRecover`) while no read
since the bring-up has given one. That is the card's identity, and a fresh
registration is made under it. A resumed registration keeps the one it
was made under until its next fresh registration, as it keeps its
P-CSCF and its address (step 3): the state file records the three names
since 1.2.3, and a file without them is taken for one made under the
derived ones (`identity: the registration to
resume was made under …; it keeps them until the next fresh
registration, which takes the card's` when they are not the card's;
`identity: the fresh registration takes the card's: …` when that
registration comes under other names, and after an attempt that fails
the names are back that stood before it). The registration's home
domain is also what `RuriFor` and `ScPsi` take, for an outgoing call's
Request-URI and for the service centre's address of an SMS submission
(§12). With SMS over IP on and `SMSC=` unset,
`qmicli --uim-read-record` of EF_SMSP record 1 names the service centre
(§7.9). `mmcli --location-get` fills
`P-Access-Network-Info`.
@ -1098,7 +1184,15 @@ The daemon's own bring-up (`BringUp()` in `main.cpp`):
route (imsd's own `ip -6 route replace`; step 3 says which target), an
unprotected REGISTER on `PCSCF_PORT` (UDP, always), the 401 with the AKA challenge and
the `Security-Server` list, the AKA run over a UIM logical channel
(`--uim-open-logical-channel`, `--uim-send-apdu`), the SA pair installed
(`--uim-open-logical-channel`, `--uim-send-apdu`) — in the ISIM when the
card's identity was read from it, else in the USIM; the command is the
same (AUTHENTICATE with P2 `81`: the 3G context of TS 31.102 7.1.2, the
IMS AKA context of TS 31.103 7.1.2), and an ISIM that gives no result
is followed by the same challenge to the card's USIM (`AKA: the ISIM
application gave no result (…); asking the USIM`). TS 24.229 5.1.1.1A
has the ISIM used for authentication whenever the card has one: with
`ISIM=0`, with an ISIM that gave no identity and behind one that gives
no result imsd asks the USIM —, the SA pair installed
with `ip xfrm`, the protected REGISTER over the negotiated transport
(§10.1, `SIP_TRANSPORT`), `REGISTERED (fresh)`. Then the reg-event
SUBSCRIBE (§7.4).
@ -1135,7 +1229,12 @@ The daemon's own bring-up (`BringUp()` in `main.cpp`):
in-use SPIs) was refused with 503 (§13).
The state file is JSON: `callid`, `ftag`, `cseq`, `spi_uc`, `spi_us`,
`expiry`, `route`, `ppi`, `aor`, `contact_user`, `transport`, since 1.0.0
`expiry`, `route`, `ppi`, `aor`, `contact_user`, `transport`, since 1.2.3
`impi`, `domain` and `impu` (the identity the registration was made under,
which a resume keeps — step 2; all three absent in an older file: the
derived identity; a file that holds one or two of them, or three that are
no identity `IsimIdentity` accepts, is not resumed: `the state file's
identity is not one imsd can register; …`), since 1.0.0
`pcscf` (the P-CSCF the registration was made to; named in the line that
reports a file another attempt left behind — the check is the SPIs',
`FileNamesAnotherPair`), since 0.3.12
@ -1330,7 +1429,7 @@ The policy, with its constants:
| A silent fresh attempt | Backs off 30 s → 10 min (`FreshDelayMin`/`Max`). |
| `Retry-After` | Always wins over a shorter wait, honoured up to an hour (`RetryAfterMax`, the schedule's longest wait); a longer one is cut to an hour and the refusal heard again then. On a refused fresh attempt it replaces the schedule's wait, shorter or longer, never below 30 s (`FreshDelayMin`). |
| A 403 | A refusal like the others: never hammered (the schedule), never abandoned (a SIM that is unbarred registers within the hour). |
| The old registration is gone | A 403 once a fresh registration is due, a fresh attempt that got past the SA setup, a binding the network terminated (reg-event), an older address that left the netdev, or a bring-up whose resume failed while a fresh registration follows (with no fresh target a resumable pair whose attempt failed is kept for the restart; a pair nothing can resume — given up or written off by the engine, its address off the link, no state file, a file another attempt left behind, left untried by `RESUME=0` — is written off and taken out of the kernel, §7.2 step 1): no reconnect over the old SA from then on. Otherwise the existing SA is still retried on the reconnect back-off while a fresh registration is deferred. |
| The old registration is gone | A 403 once a fresh registration is due, a fresh attempt that got past the SA setup, a binding the network terminated (reg-event), an older address that left the netdev, or a bring-up whose resume failed while a fresh registration follows (with no fresh target a resumable pair whose attempt failed is kept for the restart; a pair nothing can resume — given up or written off by the engine, its address off the link, no state file, a file another attempt left behind or whose identity fails the check, left untried by `RESUME=0` — is written off and taken out of the kernel, §7.2 step 1): no reconnect over the old SA from then on. Otherwise the existing SA is still retried on the reconnect back-off while a fresh registration is deferred. |
| A `Dial` while unregistered | Brings the next recovery step forward (the reconnect, or the fresh attempt once it is due) — never a wait the network imposed (a `Retry-After`, or the schedule after a refused or throttled fresh attempt). imsd-ofonod refuses a `Dial` then and imsd-dialerd lists no modem. |
| Success | Resets everything. |
@ -1344,7 +1443,8 @@ the SA setup, a binding the network terminated, an older address that left
the netdev, and every bring-up that goes on to register fresh — its resume
failed or there was nothing to resume; a bring-up with no fresh target
writes `old_gone` when nothing can be resumed — a pair nothing can resume: its UE
address left the link, no state file, a file another attempt left behind, written
address left the link, no state file, a file another attempt left behind or whose
identity fails the check, written
off before this start, given up or written off by the engine during the
attempt, left untried by `RESUME=0` — never for a pair kept for the restart,
§7.2 step 1); the reducer's refusal count travels too (`refusals`): a resume
@ -1592,8 +1692,33 @@ account `user`, its old default.
Lines the operator (and the scripts) look for, all prefixed `imsd: `:
- `LOCAL=… P-CSCF=…`, `SIP transport: …`, `ESP ciphers offered: …`, `SIM slot=… IMSI=… domain=…` at start;
- `LOCAL=… P-CSCF=…`, `SIP transport: …`, `ESP ciphers offered: …`, `SIM slot=… IMSI=… domain=…` at start
(the home domain of the card's identity, which the line before names);
`EALG entry '…' is not a cipher imsd can install; ignored` for a name in `EALG` that imsd cannot install.
- Before the `SIM slot=…` line, at every bring-up, where the identity is from (§7.2 step 2): `identity: the
ISIM's (application state S): private identity P, home domain D, public identity U; the names the IMSI gives`
or `…; not the names the IMSI gives (home domain D')`, `identity: derived from the IMSI (the card has no
ISIM application)`, `identity: derived from the IMSI (ISIM=0)`, `identity: derived from the IMSI: the ISIM
application (state S) gave none — …` with what was missing: `no logical channel opened on it`, `EF_X: the
SELECT answered <status word>`, `EF_X: the read answered <status word>`, `EF_X: no answer with a status
word to the SELECT`, `EF_X: an exchange of the read brought no answer with a status word` (the read or a
command the card asked for after it), `EF_X: no value in the N bytes read`, or `EF_IMPI '…', EF_DOMAIN
'…' and EF_IMPU record 1 '…' are no identity imsd can register`. At a resume whose
registration was made under other names than the card gives now: `identity: the registration to resume was
made under private identity P, home domain D, public identity U; it keeps them until the next fresh
registration, which takes the card's`; at a fresh registration under other names than the ones in use
before it: `identity: the fresh registration takes the card's: private identity P, home domain D, public
identity U`;
`the state file's identity is not one imsd can register; resume falls back to fresh` (with no fresh target
`…; nothing can resume it: the pair is removed`) for a file whose three names fail the check or that
holds one or two of them. At a recovery by a fresh registration, while the ISIM has given none since the
bring-up, it is read again and the line of that read follows: `identity: the ISIM's …` or
`… gave none — …`.
- `AKA: the ISIM application gave no result (…); asking the USIM` and, when no application gave one, `AKA: no
result (…)` before `USIM AKA failed`: `no logical channel opened`, `an exchange brought no answer with a
status word` (the AUTHENTICATE or a GET RESPONSE after it),
`status word <status word>`, `a synchronisation failure, tag DC`, or `N bytes that are no AKA result,
status word <status word>`.
- `RESUME via existing SA (cseq N)`, `REGISTERED (resumed + true-refreshed)`,
`FRESH registration`, `Security-Server: …`, `REGISTERED (fresh)`.
- `bring-up deferred: …; next: fresh registration in N s` — one line; `in N
@ -2513,6 +2638,25 @@ only the imsd side of its bus choice.
authorization is polkit's, not the policy file's. Keep it that way: a
policy that names an account breaks the moment the account is called
something else.
- What an ISIM's identity files hold goes into the requests of a
registration made under it. Its REGISTERs: the home domain as the
Request-URI's host and in the digest's quoted `realm` and `uri`, the
private identity inside the quoted `username`, the public identity
between the angle brackets of From and To. Beyond them: the home domain
in the address `RuriFor` makes of a dialled number, an outgoing call's
Request-URI and To (its host, and its phone-context where `RuriFor`
writes one), and as the host of the service centre's address of an SMS
submission (`ScPsi`: the MESSAGE's Request-URI and To); the public
identity, while no
P-Associated-URI has named another, as the reg-event SUBSCRIBE's
Request-URI, From and To (`BuildSubscribeReg`) and as the identity of
imsd's own requests (`CallerId`). `ParseIsimValue` takes no blank and no
byte outside printable ASCII and `IsimIdentity` no quote, backslash or
angle bracket, and in the domain nothing but letters, digits, hyphens
and dots, so a card cannot end a header field or a request line or add
one; a card whose values fail the check registers the derived names. The
journal prints the three as it prints the IMSI, and the state file holds
them.
- SIP dumps and the state file carry the IMSI, the MSISDN, SPIs and
addresses, and the state file (since 0.3.11) the last challenge answer —
nonce, cnonce and response — and (since 0.3.12) the P-CSCF's
@ -2723,7 +2867,14 @@ the related code.
USIM (a KPN eSIM, 2026-11-21, by hand) that SELECT answered `61 19` for
one file and `61 1C` for another, the
two reads data and `90 00` at once, and the SELECT of a file the
application does not have `6A 82`.
application does not have `6A 82`. The US card's network answered imsd
1.1.0's first REGISTER, which carried the derived names
(`ims.mnc280.mcc310.3gppnetwork.org`), `480 Temporarily Unavailable`
(2026-11-09: a journal has the answer in the second the REGISTER was
sent, a dump of such an answer has no Reason, Warning or Retry-After) —
what TS 24.229 5.3.1.3 has an I-CSCF answer
when the HSS lookup "cannot be completed"; this daemon has not sent
that network the card's own names.
- **A folded `WWW-Authenticate`** — the 401 to the unprotected REGISTER
came with its `WWW-Authenticate` over four lines on KPN (2026-07-18)
and on Odido (2026-07-13), the nonce on the second (RFC 3261 7.3.1; one