diff --git a/AGENTS.md b/AGENTS.md index 736a0b4..5a83fcb 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -43,7 +43,7 @@ PipeWire. The verified device/carrier combinations are in the README. |---|---| | `project.cpp` | The canonical build description, for [Crafter Build](https://forgejo.catcrafts.net/Catcrafts/Crafter.Build). Chooses the product by `--product=`. | | `interfaces/Imsd.cppm` | The primary module interface of `imsd-core`: `export import`s the nine partitions below. | -| `interfaces/Imsd-Util.cppm` | `imsd::util` — SIP tokens, IPv6 host/port formatting, the RFC 5952 §4 IPv6 text (`CanonicalIp6`/`CanonicalAddr`; an IPv4-mapped address spelled in hex stays hex, where §5 and `inet_ntop` write it dotted) and the comma-separated address-list parser (`ParseAddressList`), hex/base64, MD5 (RFC 1321), the fixed protected ports `PortUc`/`PortUs` (45061/45062). | +| `interfaces/Imsd-Util.cppm` | `imsd::util` — SIP tokens, IPv6 host/port formatting, the RFC 5952 §4 IPv6 text (`CanonicalIp6`/`CanonicalAddr`; an IPv4-mapped address spelled in hex stays hex, where §5 and `inet_ntop` write it dotted) and the comma-separated address-list parser (`ParseAddressList`), the count of seconds a setting names (`WholeSeconds`), hex/base64, MD5 (RFC 1321), the fixed protected ports `PortUc`/`PortUs` (45061/45062). | | `interfaces/Imsd-Sip.cppm` | `imsd::sip` — SIP as a text layer plus TCP framing: header extraction (from the header section only: a read ends at the first empty line after the start line, empty lines before it skipped; a header is asked for by its long name and found under that or its compact form — RFC 3261 7.3.3: the RFC's ten, and `o` for Event and `x` for Session-Expires — with or without white space before the colon; `Unfolded`, by which the framers hand a message on with its folded header lines — 7.3.1 — joined; `HeaderUri`: the party a header names; `Body`, moved here out of `CallMachine`), status codes, the granted lifetime, `ResponseOf` transaction matching (the Call-ID, and the CSeq as its number and its method, whatever spaces or tabs part them and whatever zeros stand before the number), `CutShortRequest`: the header section of a request — any but an ACK or a CANCEL — whose datagram has fewer body bytes than the Content-Length read from it says, which its 400 is built from. Not a full RFC 3261 parser, deliberately. | | `interfaces/Imsd-Sdp.cppm` | `imsd::sdp` — SDP offer/answer: the AMR-WB + AMR + telephone-event offer (optional IR.92 preconditions; `OfferedTelephoneEventPt` names its telephone-event numbers), the parser the media plane needs (the codec, its payload type and mode, and the telephone-event payload type the m-line lists at that codec's clock), `BuildAnswer` for the terminating side, the `CODECS` override. | | `interfaces/Imsd-Sms.cppm` | `imsd::sms` — the short message codecs SMS over IP rides on (TS 24.341 carries the bytes a 2G/3G phone puts in an RP-DATA, in a SIP MESSAGE body): the GSM 7-bit default alphabet with its extension table and the septet packing, UCS-2 (TS 23.038); the SM-TL PDUs (TS 23.040: SMS-SUBMIT built, one per segment with the concatenation header when the text needs more than one; SMS-DELIVER, SMS-STATUS-REPORT and SMS-SUBMIT-REPORT parsed; the SMS-DELIVER-REPORT that acknowledges a delivery; the user-data header — concatenation, application ports — and the `Reassembler` of a concatenated message); the SM-RL messages (TS 24.011: RP-DATA, RP-ACK, RP-ERROR in both directions, the cause values) and `RpCauseIsTemporary`, the causes of a submission after which imsd sends the segment again; EF_SMSP (TS 31.102 §4.2.27), where the (U)SIM names the service centre, and qmicli's hex dump of it. Byte-level only: no SIP, no clock, no I/O. | @@ -276,7 +276,10 @@ What the suites pin: - `Util` — tokens, IPv6 detection and host/port formatting, the RFC 5952 §4 form (the RFC's §4.1–4.2.3 examples, idempotence on `inet_ntop`-shaped - text, what is refused), the address-list parser, hex/base64, MD5. + text, what is refused), the address-list parser, the count of seconds + a setting names (`WholeSeconds`: digits and nothing else; a sign, a + unit, a fraction, a blank, `0x1e`, `1e2`, `nan`, `inf` and a number too + large are refused), hex/base64, MD5. - `Aka` — the AKAv1-MD5 digest, identity derivation, the `qmicli`/`mmcli` text parsers, the AUTHENTICATE APDU and its response parser, the class byte of a command on a logical channel (the channel itself for 0 to 3, @@ -1021,17 +1024,81 @@ Rules: `bench/bench-esp-pinhole.sh` there has the gap without the script: the network's connection accepted a second after the phone's own exchange and under a packet of the phone's every 3 s, and unanswered 11 s after - the last packet. On the dev phone (KPN, the capture's `phone/`, - 2026-11-25) the script, run at imsd's start, inserted the rule above the - two port rules it found, and the daemon resumed its registration. Then a - text was sent to the phone's own number with the tracker's timeout at 0 - for the test, so that every ESP packet from the network came without an - entry: with the rule, six such packets came, and the answer to the - MESSAGE, the submit report and the text's delivery reached the daemon - within a second; with the rule taken out, 23 came in 20 s and none of - the three reached the daemon in those 20 s. Not met: a request the - network starts after the entry has lapsed of itself, 600 s after the - phone's last ESP packet (a call to the phone from another line). + the last packet. On the dev phone (KPN, the capture's `phone/`), under a + daemon that sends no ping (the binary of 1.2.4, no `CRLF_KEEPALIVE`): + the tracker's entry for the P-CSCF's ESP was read counting down after a + refresh (575 s left 23 s after it, 15 s left 9 min 43 s after it) and + was gone 10 min 29 s after it (2026-11-24); the script, run at imsd's + start, inserted the rule above the two port rules it found, and the + daemon resumed its registration (2026-11-25). Then a text was sent to + the phone's own number with the tracker's timeout at 0 for the test, so + that every ESP packet from the network came without an entry: with the + rule, six such packets came, and the answer to the MESSAGE, the submit + report and the text's delivery reached the daemon within a second; with + the rule taken out, 23 came in 20 s and none of the three reached the + daemon in those 20 s. Not met: a request the network starts after the + entry has lapsed of itself, 600 s after the phone's last ESP packet (a + call to the phone from another line). +- The idle flow (§7.2 step 5; no CRLF keepalive unless `CRLF_KEEPALIVE` + names an interval, 1.2.4) is shell code no suite reaches; how the + setting's value is read is `WholeSeconds`, pinned in `tests/Util`. The + loop ran on the workstation against a stand-in network that prints + every ping and can close a flow it has heard nothing on, or forget it + and answer the next bytes on it with a reset (2026-11-24, the fp6 + journal's `2026-11-24-crlf-keepalive` capture, + `bench/bench-keepalive.sh`: the daemon in dev mode, its card and modem + reads answered by stand-ins). Left alone for 100 s after its + registration the daemon sent no ping, 1.2.3's three, and with + `CRLF_KEEPALIVE=10` nine, behind its start line (§7.8); over UDP with + that setting it sent none and no empty datagram; with + `CRLF_KEEPALIVE=0` and with an empty value it sent none and logged + nothing of the setting; with `CRLF_KEEPALIVE=30s` it logged the value + as not understood and sent none. A network that closes a flow after + 45 s of silence closed two in 130 s, each reconnected and re-registered + in the same second; it closed none under 1.2.3's pings or under + `CRLF_KEEPALIVE=30`, and one that closes after 60 s none under a refresh + every 40 s. A network that forgets a flow after 45 s of silence reset + the refresh that came at 60 s, twice in 140 s: `keepalive re-REGISTER + failed: no reply to re-REGISTER`, then the reconnect and the + registration in the same second; under `CRLF_KEEPALIVE=30`, as under + 1.2.3's pings, it forgot none and answered both refreshes; one that + forgets after 20 s reset each of 1.2.3's three pings, and each was + followed by the reconnect. Under a call the daemon had placed and + hung up 30 s later, a network that forgets a flow after 20 s of + silence reset the BYE's bytes, and one that closes it after 20 s had + closed it: in both the BYE was not delivered, the call ended + `local-hangup — HangUp; BYE sent`, and the recovery reconnected as + soon as the call was gone; 1.2.3's BYE was lost the same way on the + forgetting network (the fp6 journal's `2026-11-24-bye-resend` + capture, `bench/bench-hangup.sh`, the scenarios named `-not-resent` + and `hangup-forgotten-before`; §7.6). + On the dev phone (KPN, the same day; its + ModemManager has the WDS event reports off while the phone sleeps, + §13), with the binary of 1.2.4 (the capture's `phone3/`): no packet on + the ims netdev in two idle minutes, where 1.2.3 put one each way every + 30 s (`phone/`); with `CRLF_KEEPALIVE=30` the start line and a packet + each way every 30 s; with `CRLF_KEEPALIVE=30s` the line for a value not + understood and no packet. With builds of this change from before the + call's resend (two bullets above) was put under it (`phone2/`, + `phone/`): the netdev's counters stood still for the 1800 awake seconds + to the refresh, which the P-CSCF answered on the flow, silent until + then (no reconnect), twice; and two loops of deep suspends with a 150-s + alarm had no packet on the netdev, of seven sleeps (five ended by the + alarm, one after 9 s by the ADSP's battery manager, one after 98 s by a + file-system request of the modem's that no packet had preceded) and of + six (five ended by the alarm, one after 99 s by the ADSP's battery + manager). Not met: a real network that closes or forgets an idle flow; + a flow that dies with neither a close nor a reset; a reg-event + SUBSCRIBE as the first bytes on a forgotten flow; an incoming call + whose BYE is the first bytes on such a flow; an incoming text whose + delivery report is the first bytes on such a flow. A text's MESSAGE as + the first bytes on a flow the network had dropped was met once, in the + firewall's test above (2026-11-25): the phone had dropped the P-CSCF's + packets for 20 s, a text's first MESSAGE had gone unanswered for that, + and its repeat, 128 s on, was the first thing imsd sent on the flow + since. The flow was found dead 0.3 s after the repeat went out, the + recovery reconnected and re-registered 0.2 s after that, and the text + ended `failed`, its repeat lost with the flow. - `imsd-pcscf` ran on the dev phone (KPN) on 2026-10-11 against the live ims bearer: the `--mux 1 --profile 2 -v` sequence, a plain run, and `OutOfCall` on an unbound mux (the fp6 journal's `2026-10-11-pcscf-pco-probe` capture, @@ -1281,7 +1348,42 @@ The daemon's own bring-up (`BringUp()` in `main.cpp`): (§10.1, `SIP_TRANSPORT`), `REGISTERED (fresh)`. Then the reg-event SUBSCRIBE (§7.4). 5. A keepalive re-REGISTER refresh at half the granted lifetime, within - 120–1800 s (`REFRESH_INTERVAL`), only while idle. The refresh of a + 120–1800 s (`REFRESH_INTERVAL`), only while idle. Besides it an idle + phone whose registration stands sends what its reg-event subscription + needs (§7.4). The CRLF keepalive (RFC 5626 4.4.1) imsd sent every 30 s + until 1.2.3 is off unless `CRLF_KEEPALIVE` names an interval in whole + seconds (a TCP flow's only; an empty value is as none, any other that + is no whole number is reported and sends none; §13 has what a ping + costs a phone). No registration of imsd's negotiates keep-alives: its + REGISTER has no `outbound` in `Supported` and no `reg-id` (RFC 5626 + 4.2.1), and no `keep` in the Via (RFC 6223), which TS 24.229 5.1.1.2.1 + d) has a UE offer unless it is configured not to send keep-alives or + is directly connected to an IP-CAN for which usage of NAT is not + defined. What a ping does for a flow, outside a call (none is sent in + one): sent more often than a P-CSCF's timer for a flow it hears + nothing on, it keeps that P-CSCF from dropping the flow, and it draws + the reset of a P-CSCF that has lost the flow. On the phone it also + kept the connection tracker's entry for the P-CSCF's ESP, which the + firewall's rule for ESP makes needless (§13). Without the ping: a + close by the network is answered, outside a call, with a reconnect + and a refresh (`client flow dead; reconnecting`, §7.4); the reconnect's + first connect can be refused, the closed flow's socket still in the + kernel's table (`connect failed; retry in 10s (TIME_WAIT?)`: a user's + log on O2, 2026-09-24), and its second follows 10 s later. A flow the + network has dropped without closing, where it answers the next bytes + on it with a reset, is found at those bytes: where they are this + refresh's, it fails (`keepalive re-REGISTER failed: …`) and the same + reconnect follows; where they are a call's INVITE, the shell reopens + the flow for the call (§7.6); where they are a call's BYE, the BYE + is lost (§7.6); where they are a text's MESSAGE, it is lost with the + flow and the text's wait decides (§7.9); where they are the delivery + report of a text that came in, the report is lost with the flow and + not sent again. A flow that died with no close and no + reset reaching the phone is noticed when this refresh's re-REGISTER + gets no answer, or sooner where TCP gives up on bytes imsd sent on it + before that (924.6 s or more after them by the kernel's default + `tcp_retries2` of 15); until 1.2.3 a ping's were among those bytes + every 30 s outside a call. The refresh of a registration this process made fresh is a reregistration as TS 24.229 5.1.1.4.2 has it: the same Call-ID and Contact, CSeq+1, the Authorization of the last successful authentication verbatim (the 401's @@ -1561,7 +1663,7 @@ When nothing is held the file is removed — when it is a regular file; `/dev/null` switches the knob off and anything that is not a regular file is emptied, never unlinked. The recovery policy's waits and the holds are measured on the boot clock (`CLOCK_BOOTTIME`), so a suspend counts; the -keepalive refresh gate, the CRLF keepalive, the reg-event retry and +keepalive refresh gate, the CRLF keepalive (where `CRLF_KEEPALIVE` sets one), the reg-event retry and renewal timers and the times SMS over IP measures (§7.9: its two waits, the 30-minute age-out, the retransmission table's 32 s, the submissions table's hour) run on the steady clock and count awake seconds only. @@ -1694,6 +1796,22 @@ sent, emit this D-Bus state change, arm this deadline). States and reasons are t unusable offer 488 naming what was offered; ring timeout 480. A landline gateway's G.711-only or AMR-NB offer rings and is answered at its own payload types. +- Hang-up: a BYE of imsd's goes out on the client flow, an incoming + call's too, and the call is over for imsd as the BYE is sent: the + media leg is stopped (RFC 3261 15.1.1 has the session terminated as + soon as the BYE is passed to the client transaction) and no response + to the BYE is waited for. The BYE is not kept and not sent again: on a + flow the network has closed or dropped it is lost, and the call's line + in the journal still ends `BYE sent`. A network that closes or drops a + TCP flow it hears nothing on (§7.2 step 5) can have done so by then: + under a call no ping goes out (with `CRLF_KEEPALIVE` neither), the + refresh waits for the call's end and the recovery does not reconnect. + That network's timer runs from the flow's last bytes. A call the + daemon placed restarts it with its own requests; an incoming call + sends none before its BYE, and where until 1.2.3 a ping went out every + 30 s outside a call, without the ping the flow can have been silent, + or dropped without a close, long before the call came. Shown on the + workstation for a call the daemon placed (§5); not met on a network. - Emergency: a number `IsEmergency` classifies (112, 911, plus `EMERGENCY_NUMBERS`; SIM `EF_ECC` is not read yet) is tried as `urn:service:sos` first, with one fallback to a plain INVITE of the digits; @@ -1799,9 +1917,11 @@ 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: …` (behind it `CRLF keepalive: every N s on an idle TCP flow + (CRLF_KEEPALIVE)` where that setting names an interval), `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. + `EALG entry '…' is not a cipher imsd can install; ignored` for a name in `EALG` that imsd cannot install, + `CRLF_KEEPALIVE=… is not a whole number of seconds; no CRLF keepalive` for a value of that setting, other than an empty one, that is no whole number. - 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 @@ -1835,8 +1955,9 @@ Lines the operator (and the scripts) look for, all prefixed `imsd: `: `the refusal schedule stands at N min from before this start: …`. - `…; next: fresh registration in N s` (`in N min` from a minute, `now`), `registering fresh (recovery attempt N)`, - `fresh registration deferred/refused/failed: …`, `reconnected + re-registered`, - `recovered by a fresh registration`. + `fresh registration deferred/refused/failed: …`, `client flow dead; reconnecting` + (a reconnect over the registration's SAs begins, §7.4) and its + `reconnected + re-registered`, `recovered by a fresh registration`. - `the registration's SAs are on X, not the PDN's own address Y: resuming there until the next fresh registration`, `the fresh registration moves from X to the PDN's own address Y`, `back on X: the old registration stands @@ -2902,6 +3023,39 @@ the related code. first off the stream's 20 ms timestamp grid (bench 10's outgoing call) — whether that shape is the phone's or the network's interworking, nothing on disk says. +- **A packet imsd sends on the ims PDN can wake a phone that has gone + back to sleep, up to twice** (the dev phone, KPN, a mainline-based 7.3 + kernel with the fp6 journal's power lane's test patches; + `2026-11-20-modem-echo-wakes` there). The bearer comes + up for it and goes dormant 11 to 13 s later, which the modem reports to + ModemManager (WDS event reports), and 90 s after the dormancy the modem + makes a file-system request to rmtfs (what it reads or writes there is + not known). Each can wake a phone that has + gone back to sleep; a ModemManager patched to switch those reports off + for the sleep (on the dev phone since 2026-11-20, not in the fp6 image) + keeps the first back. Under the CRLF keepalive of imsd up to 1.2.3, one + every 30 s of a clock that stops in a suspend, the modem ended 29 + sleeps of 29 in a loop of suspends with a 150-s alarm, 15 with the + report and 14 with that request, and 5 of 8 with the reports off; with imsd + held still, none of 6 (2026-11-20). Without the ping (two builds of 1.2.4's + change, §5), with the reports off, the same loop ran twice with no + packet on the ims netdev: the modem ended none of six sleeps in one + and one of seven in the other, for a file-system request that no + packet had preceded (2026-11-24). What an idle phone with a standing + registration still sends is the refresh, once in 1800 awake seconds on + KPN (three packets out, the dormancy report 11 to 13 s and the request + 102 to 104 s after them), and what its reg-event subscription needs + (§7.4): on KPN a renewal once in 599400 awake seconds (the grant of + 600000 s less 600). Awake and idle + on a 100-mA USB port the dev phone's battery lost 140 mA under 1.2.3's + ping and 59 mA without it (its own counter over one window of 40 min + and one of 33; 2026-11-24). +- **KPN's P-CSCF leaves a flow open that carries nothing**: 44 min 9 s + lay between a registration's last packet and the refresh, which it + answered on that flow (the dev phone, 2026-11-24, six suspends in + between). Where a network's P-CSCF closes or forgets a flow it hears + nothing on, `CRLF_KEEPALIVE` is that network's setting (§7.2 step 5 + has what imsd does without it). - **pmOS's default nftables** drops inbound on `qmapmux*`; without the protected-port rule every network-initiated request (NOTIFY, MT INVITE) dies after ESP decapsulation and MT calls fall back to CS. The @@ -2912,19 +3066,19 @@ the related code. `nf_conntrack_generic_timeout` seconds after the last packet either way, 600 on the dev phone. A request the network starts later than that after the phone's last ESP packet is then dropped, and its - retransmissions until the phone next sends to that P-CSCF. A CRLF - keepalive every 30 s keeps the entry (over TCP and outside a call - only: `SendKeepalive` sends none over UDP, and the shell's loop calls - it only while no call is up); the bring-up script's rule for ESP - (§7.3, 1.2.4) makes the entry needless. Shown on the workstation with - the dev phone's ruleset, and on the dev phone for a text with the - tracker's timeout at 0 (§5). A firewall without an `inet filter` table - with an `input` chain gets no rule from the script and needs its own. - And the script's rules are in the running ruleset only: a reload of - the ruleset from its files (the nftables unit's `flush ruleset`, and - the one in `/etc/nftables.nft`) removes them until imsd next starts, - so a ruleset whose files carry the port rules (fp6-img's - `01_imsd.nft`) has to carry the rule for ESP in them too. + retransmissions until the phone next sends to that P-CSCF. Until 1.2.3 + imsd's CRLF keepalive every 30 s kept the entry (over TCP and outside + a call only; since 1.2.4 none goes out unless `CRLF_KEEPALIVE` names + an interval, §7.2 step 5); the bring-up script's rule for ESP (§7.3, + 1.2.4) makes the entry needless. Shown on the workstation with the dev + phone's ruleset, and on the dev phone for a text with the tracker's + timeout at 0 (§5). A firewall without an `inet filter` table with an + `input` chain gets no rule from the script and needs its own. And the + script's rules are in the running ruleset only: a reload of the + ruleset from its files (the nftables unit's `flush ruleset`, and the + one in `/etc/nftables.nft`) removes them until imsd next starts, so a + ruleset whose files carry the port rules (fp6-img's `01_imsd.nft`) has + to carry the rule for ESP in them too. - **The kernel's temporary SLAAC address** on the ims netdev is listed first and retired after about two days; a registration bound to it dies with it. - **systemd reads `EnvironmentFile=` per spawned command** (systemd 262~rc3 diff --git a/README.md b/README.md index 8fffcd9..26180d7 100644 --- a/README.md +++ b/README.md @@ -95,6 +95,7 @@ Configuration is environment variables in `/etc/imsd.env`, none are required. | `HOLDS_FILE` | `/run/imsd.holds` (root) / `$XDG_RUNTIME_DIR/imsd.holds` (`/tmp/imsd.holds` without a runtime dir) | the waits the network imposed (a `Retry-After`, the refusal schedule), where that schedule stands whether the old registration is gone (`old_gone`) and the refusal count (`refusals`: a resume refused without a Retry-After at a start with no fresh target is one, the restart's the second), kept across a restart of imsd for the PDN they were set on — not across a reboot; `/dev/null` switches the file off (only a regular file is ever removed; anything else is emptied). `imsd-handover.sh` honours it too | | `RESUME` | `auto` | `0` forces a fresh registration (ignores a warm SA) where there is a fresh target; with none the untried pair is removed and the bring-up ends as AGENTS §7.2 (the daemon's own bring-up) step 1 says | | `REFRESH_INTERVAL` | *(auto)* | keepalive re-REGISTER period in s; default = half the granted expiry, clamped to [120, 1800] | +| `CRLF_KEEPALIVE` | `0` | whole seconds between CRLF keepalives (RFC 5626 §4.4.1) on an idle TCP flow to the P-CSCF; `0` or an empty value sends none (until 1.2.3 imsd sent one every 30 s; AGENTS.md §13 has what that costs a phone). For a network that drops a flow it hears nothing on (AGENTS.md §7.2, the daemon's bring-up, step 5). Any other value that is no whole number of seconds is reported in the journal and sends none | | `EALG` | `aes-cbc,null` | the ESP ciphers offered, in order: a comma-separated list of `aes-cbc`, `null` (no encryption) and `des-ede3-cbc`; a name imsd cannot install is ignored with a journal line. From the 401's `Security-Server` list imsd selects the `ipsec-3gpp` mechanism the P-CSCF put the SPIs on when it is the only such entry and one of the offer, else the mechanism of the offer with the highest preference (the `q` of RFC 3329 §2.3.1), and takes the SPIs/ports from the entry carrying them (some cores list every mechanism they support and put the SPIs on the one they applied); when no entry matches, the P-CSCF's SPI-carrying entry is taken as listed (`hmac-md5-96`, an absent `ealg` = null) where imsd can install it, and the journal says so. The offer is logged at bring-up beside the SIP transport, the line, the selection and the SA parameters at every fresh registration | | `SIP_TRANSPORT` | `auto` | transport of the protected leg (the second REGISTER and everything after it): `tcp`, `udp`, or `auto` = TCP, falling back to a fresh registration over UDP when the P-CSCF leaves two TCP connects unanswered — but only on a phone that has never registered over TCP: once the state file records a TCP registration, a silent connect is treated as an outage, not a policy (delete the file or set `udp` to override, e.g. after a SIM change). The challenge (first REGISTER) is always UDP. Over UDP a REGISTER or INVITE larger than ~1.2 KB leaves as IPv6 fragments at the ims PDN's 1280-byte MTU (the journal says so once per flow); there are no SIP retransmission timers over UDP yet, so a lost datagram costs a timeout. The SAs, the listener and the firewall rule cover both transports | | `SMS_OVER_IMS` | `0` | `1` takes short messages over IMS (AGENTS.md §7.9): the REGISTER Contact advertises `+g.3gpp.smsip` and `net.catcrafts.IMS1.Messaging.Send` submits one; off, imsd does not advertise it and refuses `Send` (AGENTS.md §7.9 says how imsd answers a text the network sends anyway) | diff --git a/implementations/main.cpp b/implementations/main.cpp index 34a8198..20ff7f0 100644 --- a/implementations/main.cpp +++ b/implementations/main.cpp @@ -1512,6 +1512,16 @@ public: sipPolicy_ = "auto"; } transport_ = sipPolicy_ == "udp" ? Transport::Udp : Transport::Tcp; + // CRLF_KEEPALIVE=: an RFC 5626 4.4.1 ping that often on the + // idle TCP flow, for a P-CSCF that drops a flow it hears nothing on. + // None by default (until 1.2.3: one every 30 s): no registration of + // imsd's negotiates them, and a ping can wake a suspended phone + // afterwards (AGENTS.md §13). An empty value is as none; any other + // that is no whole number of seconds is reported and sends none. + if (std::string v = EnvOr("CRLF_KEEPALIVE", ""); !v.empty()) { + if (auto s = imsd::util::WholeSeconds(v)) crlfKeepalive_ = *s; + else Log(std::format("CRLF_KEEPALIVE={} is not a whole number of seconds; no CRLF keepalive", v)); + } mediaBin_ = ResolveMediaBin(); // CODECS: restrict + reorder the codecs we offer and accept (bench // knob — a gateway that transcodes every caller up to AMR-WB never @@ -1670,7 +1680,7 @@ public: MaybeReconnect(); MaybeResubscribe(); MaybeResendInvite(); - if (!call_ && sip_.Alive() && Mono() - lastKa_ > 30) { sip_.SendKeepalive(); lastKa_ = Mono(); } + if (crlfKeepalive_ > 0 && !call_ && sip_.Alive() && Mono() - lastKa_ > crlfKeepalive_) { sip_.SendKeepalive(); lastKa_ = Mono(); } ReapMedia(); if (call_ && call_->State() != imsd::engine::CallState::Active && Mono() > deadline_) { Execute(call_->OnDeadline()); @@ -1773,6 +1783,7 @@ private: // config std::string pcscf_, dev_, outDir_, local_, mediaBin_; + long crlfKeepalive_ = 0; // CRLF_KEEPALIVE=: seconds between pings on the idle TCP flow; 0: none std::vector ealgOffer_; // the ESP ciphers offered, in order // The P-CSCF a fresh registration goes to (PCSCF=, else the first of // PCSCF_DISCOVERED=) and the whole discovered list. pcscf_ is the one in @@ -1935,6 +1946,7 @@ private: ctx_.ealgOffer = ealgOffer_; SetTransport(transport_); Log(std::format("SIP transport: {} ({})", TransportName(transport_), sipPolicy_)); + if (crlfKeepalive_ > 0) Log(std::format("CRLF keepalive: every {} s on an idle TCP flow (CRLF_KEEPALIVE)", crlfKeepalive_)); { std::string list; for (const std::string& e : ealgOffer_) list += (list.empty() ? "" : ", ") + e; @@ -2660,8 +2672,8 @@ private: } // The protected client flow can die under us while the SA and the - // registration stay valid — a CSFB excursion resets it, the P-CSCF drops - // idle flows — which is exactly the state a daemon restart resumes from + // registration stay valid — a CSFB excursion resets it, a P-CSCF may drop + // an idle flow — which is exactly the state a daemon restart resumes from // (s51: "restart-to-recover"). Do what the restart does, in place: // reconnect the TCP leg over the existing SA and true-refresh. When that // keeps failing — the network refuses the refresh, or nothing answers — diff --git a/interfaces/Imsd-Util.cppm b/interfaces/Imsd-Util.cppm index 4cc50a9..3578ba9 100644 --- a/interfaces/Imsd-Util.cppm +++ b/interfaces/Imsd-Util.cppm @@ -3,7 +3,8 @@ /* Imsd:Util — small self-contained primitives the rest of the engine builds on: -random SIP tokens, IPv6 detection + host/port formatting, the RFC 5952 +random SIP tokens, the whole seconds a setting names, IPv6 detection + +host/port formatting, the RFC 5952 canonical IPv6 text and the address-list parser, hex/base64 decoding, and MD5 (RFC 1321). None of the standard library ships these last three, and the IMS AKAv1-MD5 digest (Imsd:Aka) needs MD5 over raw bytes plus base64 to @@ -148,6 +149,18 @@ export namespace imsd::util { return out; } + // A setting that counts whole seconds (CRLF_KEEPALIVE=): decimal digits + // and nothing else. nullopt for anything else — a sign, a unit, a + // fraction, a blank, an empty value, a number too large for a long — so + // the caller can say the setting was not understood rather than take it + // for a number it is not. + inline std::optional WholeSeconds(std::string_view s) { + if (s.empty() || !std::all_of(s.begin(), s.end(), [](char c) { return c >= '0' && c <= '9'; })) return std::nullopt; + long v = 0; + if (std::from_chars(s.data(), s.data() + s.size(), v).ec != std::errc{}) return std::nullopt; + return v; + } + // A random token of `n` chars over [a-z0-9] — SIP branch/tag/Call-ID // material (the Python rand()). The generator is a parameter so tests can // pin output; the default constructor seeds from random_device. diff --git a/tests/Util/main.cpp b/tests/Util/main.cpp index ae86762..15b1925 100644 --- a/tests/Util/main.cpp +++ b/tests/Util/main.cpp @@ -3,7 +3,7 @@ // Imsd:Util unit tests — MD5 against the RFC 1321 test suite, base64/hex // decoding, host/port formatting, the RFC 5952 canonical IPv6 text and the -// address-list parser, and the deterministic-seed RNG. MD5 and +// address-list parser, a setting's count of seconds, and the deterministic-seed RNG. MD5 and // base64 are our own (the standard library ships neither) and the AKAv1-MD5 // digest depends on them exactly, so these are pinned hard. import std; @@ -136,6 +136,16 @@ int main() { Check(ParseAddressList("2001:db8::1").size() == 1, "list: single entry, no rejected sink"); } + // ---- WholeSeconds: a setting that counts seconds (CRLF_KEEPALIVE=) + { + Check(WholeSeconds("30") == 30, "seconds: 30"); + Check(WholeSeconds("0") == 0, "seconds: 0"); + Check(WholeSeconds("0840") == 840, "seconds: leading zero"); + Check(WholeSeconds("9876543210") == 9876543210L, "seconds: every digit, past an int"); + for (std::string_view s : {"", " 30", "30 ", "30s", "-30", "+30", "30.5", "0x1e", "1e2", "nan", "inf", "thirty", "99999999999999999999"}) + Check(!WholeSeconds(s), std::format("seconds: '{}' is no count of seconds", s)); + } + if (Failures == 0) std::println("Util: all tests passed"); return Failures != 0; // not the count: `crafter-build test` takes an exit status of 77 for a skipped suite, and an exit status keeps 8 bits }