sms, engine: a class 2 message is refused, not acknowledged, unless it is a short message type 0

OnSmsMessage acknowledged every SMS-DELIVER it could parse with an
RP-ACK and handed it to the bus, whatever its class. For a class 2
message the acknowledgement said more than was done: TS 23.038 4 has the
MS "ensure that the message has been transferred to the SMS data field
in the (U)SIM before sending an acknowledgement to the SC", and TS
23.040 9.2.3.9 has a (U)SIM data download — TP-PID 0x7F with a class 2
coding — passed to the (U)SIM. imsd writes nothing to the card, so the
network had an acknowledgement for something that had not happened
(issue #4).

imsd has no way to the card for such a message yet. TS 23.038 4 names
the answer of an MS that cannot store a class 2 message in the (U)SIM
while other storage is free, "protocol error, unspecified", and excepts
only a download on an MS that supports the (U)SIM Application Toolkit,
which imsd does not. The delivery report of a class 2 message, a
segment of a set included, is now an RP-ERROR with that cause, 111, and
the TP-FCS 0xFF, unspecified error cause; the message is neither stored
nor handed to the bus, and the journal has its sender, service centre
and time. A short message type 0 (TP-PID 0x40) with a class 2 coding is
no part of this: 9.2.3.9 has it received "irrespective of whether there
is memory available in the (U)SIM or ME or not". TP-PID 0x7F without a
class 2 coding stays a message like any other, and so do classes 0, 1
and 3.

What this changes for a consumer: no `Received` comes for a refused
class 2 message, a text of that class among them, which 1.2.0 stored
and handed over (Jorijn, 2026-11-21: the refusal is kept). The
interface's members, keys and words are as they were.

Still open of issue #4: the way to the card itself — TS 31.111 7.1.1's
ENVELOPE (SMS-PP DOWNLOAD) for a download, an EF_SMS record for any
other class 2 message — and with it an acknowledgement made from the
card's answer. Read from the dev phone for it (the fp6 journal's
2026-11-20-issues-2-5 capture, phone): the KPN USIM's service table
lists 28 (data download via SMS-PP) and 10 (short message storage),
EF_SMS holds ten records of 176 bytes, the modem's route table has a
class 2 message it receives itself stored on the card, and qmicli has
the APDU route AKA takes and no toolkit command. Whether the modem lets
an ENVELOPE through that route has not been tried.

Tested by tests/Engine. On the dev phone the daemon, the binary of this
range's last commit, refused five class 2 deliveries that the phone sent
to its own protected server port — answered 200, the journal's line,
nothing stored, and no report, since they named no gateway — and took
six deliveries from KPN as before (the capture's phone-form6).
This commit is contained in:
Jorijn van der Graaf 2026-11-22 17:35:13 +01:00
commit d6d7ba100a
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
5 changed files with 126 additions and 19 deletions

View file

@ -47,7 +47,8 @@
// message, a page-mode text 200'd and logged, a short message while SMS
// over IP is off refused with 480, a delivered MESSAGE and a final whose
// header names come in compact form, a delivered MESSAGE whose
// Content-Type came folded), and the store's record codec
// Content-Type came folded, a class 2 message refused with an
// RP-ERROR and kept from the bus), and the store's record codec
// (FormatStoredSms/ParseStoredSms: a full record round trip, defaults
// for absent keys, a value that does not parse or a missing id refused) and order
// (StoredSmsBefore: the arrival second, then within a run the sequence
@ -1852,6 +1853,44 @@ int main() {
const std::string final202 = Compact(final(callid, 202, "Accepted"));
Check(final202.contains(std::format("\r\ni: {}\r\n", callid)) && !final202.contains("Call-ID:") && m.OwnsResponse(final202), "outbound: a final whose Call-ID comes as `i:` is the message in flight's");
}
// a class 2 message is (U)SIM-specific and imsd has no way to the card: refused with an RP-ERROR of the cause TS 23.038 4 names, not stored, not handed over
{
imsd::util::Rng rng(95);
Reassembler re;
// a (U)SIM data download: TP-PID 0x7F, class 2 8-bit data (DCS 0xF6)
auto dl = OnSmsMessage(c, rng, inbound("012107911356131313F30017040B911346610089F67FF6208062917314080401020304"), re, nullptr, sub, 10.0);
const auto* rep = findSms(dl.actions, SmsAction::Type::SendClient);
Check(dl.response.starts_with("SIP/2.0 200 OK\r\n") && !hasSms(dl.actions, SmsAction::Type::Received) && rep && imsd::sip::Body(rep->text) == std::string("\x04\x21\x01\x6F\x41\x03\x00\xFF\x00", 9) && rep->text.starts_with("MESSAGE sip:ipsmgw@ims.mnc001.mcc001.3gppnetwork.org SIP/2.0\r\n") && rep->text.contains("\r\nIn-Reply-To: in-012107@net\r\n"), "inbound: a (U)SIM data download is refused with an RP-ERROR — cause 111, TP-FCS 0xFF — to the IP-SM-GW, nothing to the bus");
Check(RpCauseProtocolError == 111 && FcsUnspecified == 0xFF && PidSimDataDownload == 0x7F && PidShortMessageType0 == 0x40, "the refusal's cause is 111, its TP-FCS 0xFF; the download's TP-PID 0x7F, a type 0's 0x40");
Check(logged(dl.actions, "short message from +31641600986 via +31653131313 at 2002-08-26T19:37:41-00:00: class 2, a (U)SIM data download (23 bytes), which imsd cannot pass to the (U)SIM: refused") && logged(dl.actions, "delivery report RP-ERROR (RP-MR 33) -> sip:ipsmgw@ims.mnc001.mcc001.3gppnetwork.org"), "inbound: the refusal's two lines");
// a class 2 text that is no download (TP-PID 0, DCS 0x12): the same answer
auto text = OnSmsMessage(c, rng, inbound("012207911356131313F3001E040B911346610089F60012208062917314080CC8F71D14969741F977FD07"), re, nullptr, sub, 11.0);
rep = findSms(text.actions, SmsAction::Type::SendClient);
Check(!hasSms(text.actions, SmsAction::Type::Received) && rep && imsd::sip::Body(rep->text) == std::string("\x04\x22\x01\x6F\x41\x03\x00\xFF\x00", 9) && logged(text.actions, "short message from +31641600986 via +31653131313 at 2002-08-26T19:37:41-00:00: class 2 (30 bytes), which imsd cannot pass to the (U)SIM: refused"), "inbound: a class 2 text that is no download is refused the same way");
// a segment of a class 2 set: refused on its own, nothing kept for a reassembly
auto seg = OnSmsMessage(c, rng, inbound("012307911356131313F3001E440B911316325476F80012620161220500800C0500037B02028442A15008"), re, nullptr, sub, 12.0);
rep = findSms(seg.actions, SmsAction::Type::SendClient);
Check(!hasSms(seg.actions, SmsAction::Type::Received) && rep && imsd::sip::Body(rep->text) == std::string("\x04\x23\x01\x6F\x41\x03\x00\xFF\x00", 9) && re.Pending() == 0 && logged(seg.actions, "short message from +31612345678 via +31653131313 at 2026-10-16T22:50:00+02:00, segment 2/2 of set 123: class 2 (30 bytes), which imsd cannot pass to the (U)SIM: refused"), "inbound: a segment of a class 2 set is refused on its own, none of it held");
// the other classes, and TP-PID 0x7F without class 2 ("handled in the normal way", TS 23.040 9.2.3.9): received and acknowledged
for (std::string_view tpdu : {"040B911346610089F60010208062917314080CC8F71D14969741F977FD07", "040B911346610089F60011208062917314080CC8F71D14969741F977FD07", "040B911346610089F60013208062917314080CC8F71D14969741F977FD07", "040B911346610089F67FF5208062917314080401020304", "040B911346610089F67F04208062917314080401020304"}) {
auto ok = OnSmsMessage(c, rng, inbound(std::format("012407911356131313F300{:02X}{}", tpdu.size() / 2, tpdu)), re, nullptr, sub, 13.0);
rep = findSms(ok.actions, SmsAction::Type::SendClient);
const auto* rec = findSms(ok.actions, SmsAction::Type::Received);
Check(rec && rec->received.msgClass != 2 && rep && imsd::sip::Body(rep->text) == std::string("\x02\x24\x41\x02\x00\x00", 6), std::format("inbound: {} is received and acknowledged", tpdu.substr(18, 4) == "0010" ? "class 0" : tpdu.substr(18, 4) == "0011" ? "class 1" : tpdu.substr(18, 4) == "0013" ? "class 3" : tpdu.substr(18, 4) == "7FF5" ? "TP-PID 0x7F with class 1" : "TP-PID 0x7F with no class"));
}
// a short message type 0 (TP-PID 0x40) with a class 2 coding (DCS 0x12) is no message for the card (TS 23.040 9.2.3.9): acknowledged, and handed over as any type 0
auto type0 = OnSmsMessage(c, rng, inbound("012607911356131313F3001E040B911346610089F64012208062917314080CC8F71D14969741F977FD07"), re, nullptr, sub, 13.5);
rep = findSms(type0.actions, SmsAction::Type::SendClient);
const auto* rec0 = findSms(type0.actions, SmsAction::Type::Received);
Check(rec0 && rec0->received.pid == 0x40 && rec0->received.msgClass == 2 && rec0->received.text == "How are you?" && rep && imsd::sip::Body(rep->text) == std::string("\x02\x26\x41\x02\x00\x00", 6), "inbound: a type 0 message with a class 2 coding is acknowledged and handed over, not refused");
// with no gateway named, the refusal cannot go anywhere either: logged, nothing handed over
std::string anon = inbound("012507911356131313F30017040B911346610089F67FF6208062917314080401020304");
const std::string pai = "P-Asserted-Identity: <sip:ipsmgw@ims.mnc001.mcc001.3gppnetwork.org>\r\n", from = "From: <sip:ipsmgw@ims.mnc001.mcc001.3gppnetwork.org>;tag=N1";
anon.erase(anon.find(pai), pai.size());
anon.replace(anon.find(from), from.size(), "From: anonymous;tag=N1");
auto none = OnSmsMessage(c, rng, anon, re, nullptr, sub, 14.0);
Check(none.response.starts_with("SIP/2.0 200 OK\r\n") && !hasSms(none.actions, SmsAction::Type::Received) && !hasSms(none.actions, SmsAction::Type::SendClient) && logged(none.actions, "no P-Asserted-Identity or From URI on the delivered MESSAGE: no delivery report RP-ERROR sent"), "inbound: a class 2 message that names no gateway: neither handed over nor refused on the wire");
}
// a Call-ID the delivery report cannot carry — a byte up to 0x20 in it (but an LF, which ends the line) or a DEL: no In-Reply-To, the message and its report as ever
for (int b = 0; b <= 0x7F; b++) {
if ((b > 0x20 && b != 0x7F) || b == 0x0A) continue;