diff --git a/AGENTS.md b/AGENTS.md index a0504b2..1f99bc7 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -44,12 +44,12 @@ 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`), 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, `MinSe` (the interval a `Min-SE` names), `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-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, `MinSe` (the interval a `Min-SE` names), `WithoutHeader` (a message less every line on which one header is found, under its long name or its compact form), `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. | | `interfaces/Imsd-Aka.cppm` | `imsd::aka` — the card's side of a registration: the AKAv1-MD5 digest (RFC 3310), IMPI/IMPU/realm from IMSI + MCC/MNC, parsers for `qmicli --uim-get-card-status` and `mmcli` output, the AUTHENTICATE APDU and its response; since 1.2.3 the ISIM: the application of the slot in use out of the card status (`ParseIsimApp`, in whatever state the modem lists it), the SELECT and the two READ commands its identity files are read with, a card's answer (`ApduAnswer`: `90 00`, `91 XX` and `92 XX` are its normal endings; `HoldsRead` takes a read's data under `62 82` as well) and the command it asks for next (`ApduFollowUp`, for an answer without data: GET RESPONSE for `61 XX`, a command without data again for `6C XX`), the value a file holds (`ParseIsimValue`: the data object with tag `80`, the `FF` bytes at the end of its length cut, no blank and no byte outside printable ASCII) and `IsimIdentity`, the check of the three values — an `@` between two parts, a domain of labels of letters, digits and inner hyphens, 63 characters a label and 253 in all at most, a `sip:` at the start and more after it, the two identities 255 characters at most, no blank, no byte outside printable ASCII and no `"`, `\`, `<` or `>` — before they replace the derived names. | -| `interfaces/Imsd-Ipsec.cppm` | `imsd::ipsec` — the two ESP SA pairs (TS 33.203) as `ip xfrm` argv lists, the ESP ciphers offered (`ParseEalgOffer`, `DefaultEalgOffer`), the Security-Server mechanism selection, and the reader that reconstructs SPIs/ports from `ip xfrm state`/`policy` text for a warm resume, and (1.0.0) `SaPeer`, the P-CSCF the kernel's pair is to, read from the `dir out` policies that carry imsd's fingerprint, `OwnXfrmDeleteCommands`, the deletes by exact key of what two rules select — the policies carrying imsd's fingerprint and the reqid-1/2 transport-mode ESP states in their direction or on the UE's ims-PDN address, whoever installed them (§12), and `FileNamesAnotherPair`, whether a state file's `spi_uc`/`spi_us` name the kernel's pair (unknown on either side decides nothing). | -| `interfaces/Imsd-Messages.cppm` | `imsd::msg` — every SIP request and response imsd sends, built from an explicit `Context` and per-request variables so the builders are pure functions; `tests/Messages` is their suite. Also `IsEmergency`. Since 1.2.0 the SIP MESSAGE of SMS over IP (`BuildMessageSms`: an RP-DATA to the service centre's PSI, or an RP-ACK/RP-ERROR to the IP-SM-GW with In-Reply-To — TS 24.341 5.3.1.2 and 5.3.2.4), the `+g.3gpp.smsip` Contact tag behind `Context::smsOverIp`, `BuildResponse`, the final for an inbound MESSAGE outside a call's dialog (the 200, the 480, the 488): each Via row echoed in order and the To echoed with a tag added unless it already holds `tag=` (RFC 3261 8.2.6.2); since 1.2.1 `BuildBadRequest`, the 400 to a request cut short of the Content-Length read from it — any but an ACK or a CANCEL, in a call's dialog or not —, in that shape, a To tag it adds made from the request's header section and a secret of the run (RFC 3261 8.2.7); of requests that came whole, in-dialog requests, a MESSAGE among them, and the shell's answers to any other request outside a call — a NOTIFY, an OPTIONS, a method it does not handle; not an INVITE, whose 486 while a call exists is `BuildInviteResponse`'s — keep `BuildResponse200`'s hop-by-hop shape; and `ScPsi`, the service centre's PSI from its international number — never a dialled-number guess. | +| `interfaces/Imsd-Ipsec.cppm` | `imsd::ipsec` — the two ESP SA pairs (TS 33.203) as `ip xfrm` argv lists, the ESP ciphers offered (`ParseEalgOffer`, `DefaultEalgOffer`), the Security-Server mechanism selection, and the reader that reconstructs SPIs/ports from `ip xfrm state`/`policy` text for a warm resume, and (1.0.0) `SaPeer`, the P-CSCF the kernel's pair is to, read from the `dir out` policies that carry imsd's fingerprint, `OwnXfrmDeleteCommands`, the deletes by exact key of what two rules select — the policies carrying imsd's fingerprint and the reqid-1/2 transport-mode ESP states in their direction or on the UE's ims-PDN address, whoever installed them (§12), and `FileNamesAnotherPair`, whether a state file's `spi_uc`/`spi_us` name the kernel's pair (unknown on either side decides nothing); `SaPaths` and `OnSaPath`, whether a packet runs on one of the two paths the outbound policies of an SA pair select. | +| `interfaces/Imsd-Messages.cppm` | `imsd::msg` — every SIP request and response imsd sends, built from an explicit `Context` (an empty one for `BuildBadRequest`'s 400) and per-request variables so the builders are pure functions (the shell takes one line, `P-Access-Network-Info`, out of an answer its server ports send on neither path of the SA pair on record: §12); `tests/Messages` is their suite. Also `IsEmergency`. Since 1.2.0 the SIP MESSAGE of SMS over IP (`BuildMessageSms`: an RP-DATA to the service centre's PSI, or an RP-ACK/RP-ERROR to the IP-SM-GW with In-Reply-To — TS 24.341 5.3.1.2 and 5.3.2.4), the `+g.3gpp.smsip` Contact tag behind `Context::smsOverIp`, `BuildResponse`, the final for an inbound MESSAGE outside a call's dialog (the 200, the 480, the 488): each Via row echoed in order and the To echoed with a tag added unless it already holds `tag=` (RFC 3261 8.2.6.2); since 1.2.1 `BuildBadRequest`, the 400 to a request cut short of the Content-Length read from it — any but an ACK or a CANCEL, in a call's dialog or not —, in that shape, a To tag it adds made from the request's header section and a secret of the run (RFC 3261 8.2.7); of requests that came whole, in-dialog requests, a MESSAGE among them, and the shell's answers to any other request outside a call — a NOTIFY, an OPTIONS, a method it does not handle; not an INVITE, whose 486 while a call exists is `BuildInviteResponse`'s — keep `BuildResponse200`'s hop-by-hop shape; and `ScPsi`, the service centre's PSI from its international number — never a dialled-number guess. | | `interfaces/Imsd-Engine.cppm` | `imsd::engine` — the decisions as pure reducers: `CallMachine` (one call's dialog, returning `Action`s for the shell), `RegRecovery` (the registration recovery policy, §7.4) and, since 1.2.0, SMS over IP (§7.9): `SmsOutbound` (one short message of ours on its way to the service centre, segment by segment, since 1.2.1 a segment that gets no answer, or an RP-ERROR with cause 41, sent once more with TP-RD set — TS 23.040 9.2.3.6 —, returning `SmsAction`s) and `OnSmsMessage` (what an inbound MESSAGE outside a call's dialog leads to: its final, the message or status report it delivered, the delivery report to send), and `RecentTransactions` (an inbound request's retransmission — the same Call-ID, CSeq and Via branch within 32 s (`TimerJ`), the branch being the value of the first `branch=` of its first Via row and a request with none or an empty one never matched — answered with the same final and not handled again) and, since 1.2.1, `RecentSubmissions` (the Call-IDs of the MESSAGEs our submissions went out in, an hour each, by which a submit report for one that is no longer in flight is known for ours — TS 24.341 5.3.1.2); `SameRecipient` and `StatusReportBook`, a status report's match to the message that asked for one — TP-MR and recipient — and its word for the whole message, with `SmsWordOrder`, the order of a message's words: the submission's first, a report's held until then (§7.9); `RunToken`, `SentSmsId` and `ReceivedSmsId`, the ids (§8.4); `StoredSms` with `FormatStoredSms`/`ParseStoredSms`, the record codec of the received-message store, and `StoredSmsBefore`, `List`'s order. Since 1.2.5 also `WakeAt`: when the phone has to be awake for imsd (§7.10). | | `interfaces/Imsd-Qmi.cppm` | `imsd::qmi` — the QMI WDS request bytes `imsd-pcscf` sends (Bind Mux Data Port, Set IP Family, Get Packet Service Status, Get Current Settings, Get Profile Settings), the message/TLV parser, the P-CSCF list readers (0x22 flag, 0x23 IPv4, 0x24 FQDN, 0x2E IPv6 — the last one libqmi does not model). | | `interfaces/ImsdBus.cppm` | `imsd::bus` — the polkit gate in front of call control and messaging (`Gated`'s `Action` parameter names the action), shared by imsd and imsd-ofonod. GLib, so outside `imsd-core`. | @@ -184,7 +184,14 @@ stand-ins on the workstation and what on the dev phone. Where an answer goes that the server ports send (§12: the connection found by its port and its peer, an answer that is not sent, the socket a datagram leaves from) is the shell's (`ServerPorts`); §5 has what of it ran against a -stand-in network on the workstation and what on the dev phone. SMS over IP +stand-in network on the workstation and what on the dev phone. What such +an answer keeps of `P-Access-Network-Info` (§12) is decided by +`imsd::ipsec::OnSaPath` and done by `imsd::sip::WithoutHeader`, both with +tests; the shell's part is the record of the SA pair (`saPaths_`: when a +pair goes on it and when it is emptied), the two ends it has an answer +judged by (`Leaving`'s callers) and the journal's line for a header taken +out (`Leaving`), and §5 has what of it ran against that network and what +on the dev phone. 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 @@ -345,7 +352,14 @@ What the suites pin: untouched, another transport-mode user's pair on other ports and addresses untouched — its reqid-1 ESP state on the UE's address deleted, its AH state left —, a policy-less state on the UE's address deleted, one between - other addresses left). + other addresses left), and `OnSaPath` (our client port to the P-CSCF's + server port and our server port to its client port are the pair's + paths; our client port to its client port, our server port to its + server port, another port or another host for the P-CSCF, another + address or another port of ours are not, nor is anything while no pair + is on record; an IPv6 address in another spelling, the packet's or the + record's, is the same address; over IPv4 a path of the pair, and not + another host on its port). - `Qmi` — the request bytes `imsd-pcscf` sends (four of the five the probe's, byte for byte; Get Profile Settings is the helper's own, answered by the modem on the dev phone in the probe capture's `--profile 2 -v` run and on the dev phone's bring-up @@ -377,11 +391,25 @@ What the suites pin: (every Via row echoed; the To tag sixteen hex digits of the MD5 of a secret and the header section, the expected one from Python's hashlib; the same request, the same 400; another request, another tag; another - secret, another tag; a To's own tag kept). + secret, another tag; a To's own tag kept); `P-Access-Network-Info` + where the cell is known — after the RAck of a PRACK and the CSeq of a + BYE, in the 200 of `BuildResponse200` (before the Contact of one with an + answer), in the 488 of `BuildResponse`, in a 180 plain and reliable, a + 486 and a 200 of `BuildInviteResponse` — and in none of these where it + is not (the 488 and the plain 180 are pinned whole in both forms); where + the cell is known, in no 200 to a CANCEL, no 100 Trying, neither ACK and + no CANCEL; in no 400 of `BuildBadRequest`, which is built without the + context; and the 180 to an INVITE whose Allow names CANCEL is the 180 + to any other. - `Sip` — the text layer's edge cases (empty header values, line anchoring, keepalive discards, a header read that stops at the first empty line — a body line spelling a header name picks nothing, leading empty lines are - skipped), the Content-Length the two framers read (zeros before its + skipped), `WithoutHeader` (a message less every line on which + one header is found: one such line, two, the last before the + empty line, a last line without its line end, the name in + another letter case, a compact form; the body, a message + without the header and a name that is part of a longer one left + as they are), the Content-Length the two framers read (zeros before its number no part of it; a number past any that fits not read as zero; a value that begins with no digit read as none), `CutShortRequest` (the header section, unfolded, of a request with fewer body bytes than the @@ -470,7 +498,11 @@ What the suites pin: string past `DtmfMaxTones`; the receive payload type: our offer's on a renumbered answer, the answer's when the audio took ours, the echoed offer's on an incoming call; an incoming call from an INVITE, and an - outgoing call from responses, whose header names come in compact form) and + outgoing call from responses, whose header names come in compact form; + `P-Access-Network-Info` through a call where the cell is known: in the + 180, the 200s, the 486 and the 487 of a call that comes in, in the PRACK + of a call imsd places and in a BYE either way, in no 100 Trying, no 200 + to a CANCEL, no ACK and no CANCEL) and `RegRecovery` (thresholds, backoffs, the throttle schedule, Retry-After, which waits a Dial may cut short, when the old registration counts as gone, the holds a restart takes over, a binding the @@ -1353,6 +1385,46 @@ Rules: address; a peer on a link-local or an IPv4 address; a connection that ends in an error and is made again within one turn of the daemon's loop. +- What an answer of the server ports keeps of `P-Access-Network-Info` + (§7.6, §12): the rule of the two paths is pinned in `tests/Ipsec` and + the line's removal in `tests/Sip`; the shell's part (the record of the + SA pair, the two ends an answer is judged by, the journal's line) ran + on the workstation against the same stand-in network (the same capture + and script; the bench installs no SA, its `ip` being a stand-in, and + the network tells the paths by the addresses and the ports, as the + record does). Eight OPTIONS requests (five over UDP, three over TCP) + were answered with the header to the P-CSCF's protected client port + from 45062, over UDP and over TCP, and to its protected server port + from 45061 by the request's Via, and without it to another port of the + P-CSCF's host, to another host and to a Via port that is not the + protected server port, with one line in the journal; the same after a + restart that resumed the registration with another P-CSCF configured + than the pair's; none of the eight answers carried it where one policy + add of the SA setup had failed, nor where the modem named no cell. Of + an incoming call, over TCP and over UDP by the Via, the reliable 180, + the 200s to its PRACK and its UPDATE, the 200 that answered it and the + 200 to its BYE carried the header and the 100 Trying did not; over TCP + a 486 and a 487 carried it and the 200 to the CANCEL did not; a call + from another host got its 180 and its 486 without it. The daemon's own + PRACK and BYE carried it, its ACKs and its CANCEL did not, and a + request on the client flow was answered there with it. On the dev + phone on KPN (2026-12-04, the capture of the entry above), with the + record of the pair made by a resume, these left with the header + inside the SA pair: the 200s to the two NOTIFYs that follow the + reg-event SUBSCRIBE and to a text's two requests; of a call that came + in the 180, the 200 that answered it and the 200 to its UPDATE; and + the daemon's own BYE, of that call and of one it placed. The call's + 100 Trying and the daemon's ACK left without it. After a boot, where + the record is made by the fresh registration's setup: the 200s to the + NOTIFY and to a text's two requests, a call's 180, its 200 and the + 200 to its BYE, and the BYE of a call the daemon placed, with the + header; the 100 Trying and the ACK without. The journal has no line + of an answer that went without it. Of what ran against the stand-in + network nothing else has been seen on a real one (an answer off the + pair's two paths, a PRACK either way, a 486, a 487 and a CANCEL among + it). Not run: a fresh registration whose setup fails while a pair is + on record; a bring-up that finds a dead pair; a fresh attempt to + another P-CSCF that fails before its SA setup; an address move. - `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, @@ -2142,6 +2214,29 @@ sent, emit this D-Bus state change, arm this deadline). States and reasons are t call it places imsd asks to be the refresher and sends no session refresh of its own (the call machine sends no UPDATE and no re-INVITE; one from the far side is answered). +- Access information: `P-Access-Network-Info` names the serving cell + (`ParsePani` of `mmcli --location-get`: no header before a read has + named a cell, and a read that names none leaves the last one). TS 24.229 + 5.1.2A.1.1 has a UE insert it "into any request for a dialog, any + subsequent request (except CANCEL requests) or response (except CANCEL + responses) within a dialog or any request for a standalone method", the + ACK at its choice, and 5.1.2A.2 into "any response to a request for a + dialog" and "any response to a standalone method" as well; RFC 7315 5.7 + has it appear in no ACK and no CANCEL. imsd writes it into its + REGISTERs, the reg-event SUBSCRIBE, an INVITE, a MESSAGE, a PRACK and a + BYE, and into every response it builds but three, the last two by its + own choice: the 200 to a CANCEL; a 100 Trying, which a stateful proxy + never forwards (RFC 3261 21.1.1) and for which the log of the stock + modem's capture has no value either (§13); and the 400 to a request cut + short (§7.9), which the transport answers without the registration's + context. The value is the one + `RefreshPani` last took — it reads at the bring-up and after a wait + there, before a refresh's, a reconnect's and a recovery's REGISTER and + before a dial — and not the cell of the moment a later message leaves at + (NOTE 13 of 5.1.2A.1.1 has the current one in a request or response + within a dialog): an incoming call's answers name the cell of the last + of those reads. What the server ports send in answer keeps the header on + the two paths of the SA pair on record and loses it anywhere else (§12). - DTMF (`SendDtmf`): the tones go out as RFC 4733 telephone-event packets on the call's RTP stream, at the payload type the SDP negotiated for the codec's clock (`MediaLeg::dtmfPt`: the answer's for an outgoing call, the @@ -2342,6 +2437,9 @@ Lines the operator (and the scripts) look for, all prefixed `imsd: `: - `an answer from port N to [address]:port is not sent: no connection from that peer to that port is held (said once)` the first time in a run that an answer is given to the channel of a request that came in over TCP and `ServerPorts` holds no connection from that request's peer to that port (§12). +- `an answer from port N to [address]:port is on neither path of the SA pair on record: it goes without + P-Access-Network-Info, as every such answer does (said once)` the first time in a run that the header is + taken out of an answer the server ports send (§12). - `sleep is held off while the call lasts (logind's inhibitor)` when the calls table's first call has its lock and `sleep is no longer held off (no call)` when that lock is released, the table's last call gone @@ -3405,6 +3503,28 @@ only the imsd side of its bus choice. has one. A channel keeps no descriptor: the kernel gives a closed descriptor's number out again, to a pipe, a file or the connection of whoever connects next. + `P-Access-Network-Info` names the serving cell, so what `ServerPorts` + sends in answer keeps it only on the two paths of the SA pair on record + — from the client port to the P-CSCF's protected server port, from the + server port to its protected client port, between the pair's two + addresses (`imsd::ipsec::OnSaPath`) — and loses it anywhere else + (`Leaving`, `imsd::sip::WithoutHeader`), the journal saying so once in a + run (§7.8). The record is the shell's (`saPaths_`): the pair this run + set up without an error from `ip`, or read back from the kernel's + tables at a resume, and none from the moment the shell takes that pair + out of the kernel; the shell does not ask the kernel again. The client + flow is not judged by the record: what imsd sends there, its own + requests and its answers to what comes in on that flow, leaves as it + was built. It is the first of the two paths; where a command of the SA + setup failed and the registration went on all the same, it may run in + the clear. The first REGISTER of a fresh registration goes from the + init port to the P-CSCF's unprotected port and, where a cell has been + read, carries the header in the clear: RFC 7315 6.4 keeps it out of an + initial request that is unauthenticated and unprotected, as this one + is, and TS 24.229 5.1.1.2.1 has it in an unprotected REGISTER only + where a security association or TLS session exists (§13 has what the + stock modem's log shows of the REGISTER that begins its own + registration). - Short messages (§7.9): `Send` is behind `net.catcrafts.IMS1.Messaging` (an ungated `Send` would let any local account text from the subscriber's number); the journal names a delivered message's sender, size and @@ -3758,3 +3878,21 @@ the related code. `folded-401`); the `Security-Server` of both was one line. imsd finds the nonce by a search of the whole message, and unfolds every received message where it is framed (§7.9). +- **The stock modem's `P-Access-Network-Info`** — on KPN the stock + Fairphone 6's own IMS client (2026-07-21, a de-registration, a + registration and an incoming call it took: the fp6 journal's + `2026-12-04-pani-in-dialog` capture, `evidence/`) puts the header into + its reg-event SUBSCRIBE and + into its 200s to a NOTIFY, to a PRACK, to an INVITE it answers after + the call's ACK and to a BYE: its log has the value for each. It has + the value for each of the capture's three REGISTERs too; for the one + that begins the registration, unprotected, it also has, after the + value, the stack's line that a `P-Access-Network-Info` "to be Removed" + was "NOT Found", the only line of the log with the header's name: it + does not say whether that REGISTER carried the header. + The log has no value for its two 100 Tryings nor for the 200 with + which it answers the call's INVITE, and too little of its 180 to say; + it is a diagnostic stream that loses lines, so a value it lacks was + not added or its line was lost. imsd leaves a 100 Trying without the + header by its own choice (§7.6); into the 200 that answers an INVITE + it writes it, as TS 24.229 5.1.2A.2 has it. diff --git a/implementations/main.cpp b/implementations/main.cpp index 95e8e0d..4f2be52 100644 --- a/implementations/main.cpp +++ b/implementations/main.cpp @@ -753,6 +753,15 @@ struct Inbound { class ServerPorts { public: + // Whether a packet from `own` to `peer` runs on a path of the SA pair: + // the shell answers, from its record of the pair (Engine::InsideSa). + // These ports take a request from any source (AGENTS.md §12); an + // answer they send that runs on neither path goes without + // P-Access-Network-Info, which names the serving cell (RFC 7315 4.4.2: + // a UA inserts it into what "is going to be securely sent" to its + // proxy). + void SetInsideSa(std::function f) { insideSa_ = std::move(f); } + void Open(const std::string& local) { int fam = Is6(local) ? AF_INET6 : AF_INET; for (int port : {PortUc, PortUs}) { @@ -859,7 +868,8 @@ public: auto reply = [this, uc = rs == udpUc_, s, sll](const std::string& r) mutable { const int from = uc ? udpUc_ : udpUs_; if (from < 0) return; - sendto(from, r.data(), r.size(), 0, reinterpret_cast(&s), sll); + const std::string o = Leaving(r, NameOf(from), s); + sendto(from, o.data(), o.size(), 0, reinterpret_cast(&s), sll); }; if (cutShort) { Log(std::format("UDP: {} on server socket {} — answered 400", why, us == udpUc_ ? PortUc : PortUs)); @@ -892,7 +902,8 @@ public: out.push_back({*m, [this, port = it->port, peer = it->peer](const std::string& r) { const Conn* c = Find(port, peer); if (!c) { Gone(port, peer); return; } - (void)!write(c->fd, r.data(), r.size()); + const std::string o = Leaving(r, NameOf(c->fd), c->peer); + (void)!write(c->fd, o.data(), o.size()); }}); } } @@ -923,6 +934,18 @@ private: return found; } + // `r` as it leaves from `own` to `peer` (SetInsideSa). + std::string Leaving(const std::string& r, const sockaddr_storage& own, const sockaddr_storage& peer) { + if (insideSa_ && insideSa_(own, peer)) return r; + std::string bare = imsd::sip::WithoutHeader(r, "P-Access-Network-Info"); + if (bare.size() != r.size() && !saidOutside_) { + saidOutside_ = true; + Log(std::format("an answer from port {} to {} is on neither path of the SA pair on record: it goes without P-Access-Network-Info, as every such answer does (said once)", PortOf(own), + imsd::util::HostPort(HostOf(peer), PortOf(peer)))); + } + return bare; + } + // An answer with no connection to go out on. void Gone(int port, const sockaddr_storage& peer) { if (!saidClosed_) Log(std::format("an answer from port {} to {} is not sent: no connection from that peer to that port is held (said once)", port, @@ -930,6 +953,8 @@ private: saidClosed_ = true; } + std::function insideSa_; + bool saidOutside_ = false; bool saidClosed_ = false; int udpUc_ = -1, udpUs_ = -1; std::vector listeners_, udp_; @@ -1817,6 +1842,7 @@ public: return; } } + server_.SetInsideSa([this](const sockaddr_storage& own, const sockaddr_storage& peer) { return InsideSa(own, peer); }); try { server_.Open(local_); } catch (...) { Log("server ports unavailable; remote hangup/incoming not seen"); } OnRegistered(); @@ -2019,6 +2045,7 @@ private: double subRenewAt_ = 0; // the live subscription is subscribed anew at this time on the boot clock, ahead of its granted expiry (0: none) double subRetryDelay_ = 60; // 60 s doubling to 10 min between failed SUBSCRIBEs int portPs_ = 0; // P-CSCF protected server port (reconnect target) + imsd::ipsec::SaPaths saPaths_; // the SA pair this run set up without an error or resumed, until it takes that pair out of the kernel: what InsideSa judges by imsd::engine::RegRecovery recovery_; // what to do when the registration stops working bool saReplaced_ = false; // the running fresh attempt got past the SA setup (the old registration's SAs are gone) @@ -2220,6 +2247,14 @@ private: return "/usr/libexec/imsd-media"; } + // Whether a packet from `own` to `peer` runs on one of the two paths of + // the SA pair on record (imsd::ipsec::OnSaPath). The record is the + // shell's own: of the pair it set up without an error or read back from + // the kernel's tables at a resume. It does not ask the kernel again. + bool InsideSa(const sockaddr_storage& own, const sockaddr_storage& peer) const { + return imsd::ipsec::OnSaPath(saPaths_, HostOf(own), PortOf(own), HostOf(peer), PortOf(peer)); + } + // Refresh P-Access-Network-Info from the live serving cell (mmcli 3GPP // location). Serving cell changes as the phone moves, so this runs // before every REGISTER/INVITE-carrying step; on failure the previous @@ -2355,7 +2390,7 @@ private: // P-CSCF and nothing is configured, a fact until a boot or a // start. A discovery that FAILED (the reader did not answer) is // no fact: the fatal, and the restart runs it again. - if (outcome == ResumeOutcome::Dead) { recovery_.OldGone(); RemoveDeadPair(pdnLocal_); } + if (outcome == ResumeOutcome::Dead) { recovery_.OldGone(); RemoveDeadPair(pdnLocal_); saPaths_ = {}; } KeepHolds(); if (outcome == ResumeOutcome::Failed || DiscoveryFailed()) throw std::runtime_error(std::string(NoFreshPcscf)); RequestIdle(std::string(NoPcscfIdleText)); throw Stopping{}; @@ -2501,6 +2536,7 @@ private: Log(std::format("RESUME via existing SA (cseq {})", ps->cseq.value_or(1))); portPs_ = sa->portPs; + saPaths_ = {local_, pcscf_, sa->portPs, sa->portPc}; SetTransport(t); // Local refusals (TIME_WAIT after a restart) keep retrying; two // silences mean the flow is not coming back — fresh instead. @@ -2668,16 +2704,19 @@ private: sp.ealg = ealg; // imsd's previous pair out first, each entry by its key — not `ip xfrm … // flush`: another IPsec user's entries (a policy-based VPN's) stay (§12) + saPaths_ = {}; { auto state = RunCapture({"ip", "xfrm", "state"}); auto policy = RunCapture({"ip", "xfrm", "policy"}); RunOwnXfrmDeletes(imsd::ipsec::OwnXfrmDeleteCommands(state.out, policy.out, local_)); } saReplaced_ = true; // from here on the old registration's SAs are gone + bool installed = true; for (auto& cmd : imsd::ipsec::BuildSetupCommands(sp)) { int rc = RunCmd(cmd); - if (rc != 0) Log(std::format("{} -> rc {}", RedactKeys(cmd), rc)); + if (rc != 0) { Log(std::format("{} -> rc {}", RedactKeys(cmd), rc)); installed = false; } } + if (installed) saPaths_ = {local_, pcscf_, portPs, portPc}; ctx_.securityServer = ss_; portPs_ = portPs; diff --git a/interfaces/Imsd-Aka.cppm b/interfaces/Imsd-Aka.cppm index e470a07..935e736 100644 --- a/interfaces/Imsd-Aka.cppm +++ b/interfaces/Imsd-Aka.cppm @@ -283,8 +283,7 @@ export namespace imsd::aka { // "3GPP-E-UTRAN-FDD;utran-cell-id-3gpp=" // (TS 24.229 7.2A.4: E-UTRAN cell id = MCC + MNC as broadcast + 16-bit // TAC + 28-bit ECI; mmcli prints both hex fields zero-padded wider, so - // take the low digits). The stock modem sends exactly this shape in - // every REGISTER/INVITE — the TAS reads it to see which access the UE + // take the low digits). The TAS reads it to see which access the UE // is reachable on. FDD is a constant: every KPN LTE band in use is FDD // and MM does not expose the duplex mode. nullopt when the fields are // missing or zero (no serving cell) — callers then omit the header diff --git a/interfaces/Imsd-Ipsec.cppm b/interfaces/Imsd-Ipsec.cppm index 2960187..e4f8903 100644 --- a/interfaces/Imsd-Ipsec.cppm +++ b/interfaces/Imsd-Ipsec.cppm @@ -25,7 +25,8 @@ rules select — the policies carrying imsd's fingerprint and the reqid-1/2 transport-mode ESP states in their direction or on the UE's ims-PDN address, whoever installed them (§12); and `FileNamesAnotherPair`, whether a state file's spi_uc/spi_us name the pair the kernel holds (unknown on either side -decides nothing). +decides nothing). And `SaPaths` with `OnSaPath`: whether a packet runs on one +of the two paths the outbound policies of an SA pair select (§12). Pure std C++ (the shell runs the commands and captures the text). Command shapes and the parser are pinned by tests/Ipsec. @@ -180,6 +181,29 @@ export namespace imsd::ipsec { return cmds; } + // The two paths the outbound policies of an SA pair select + // (BuildSetupCommands: reqid 1 from the UE's protected client port to + // the P-CSCF's server port, reqid 2 from its protected server port to the + // P-CSCF's client port), between the pair's two addresses. The shell + // keeps one of the pair it set up without an error or resumed; with no + // ports, none. + struct SaPaths { + std::string local; // the UE's address the pair is on + std::string pcscf; // the P-CSCF's + int portPs = 0; + int portPc = 0; + }; + + // Whether a packet from [fromHost]:fromPort to [toHost]:toPort runs on + // one of the two paths of `p`. Addresses are compared as + // imsd::util::CanonicalAddr writes them (an IPv6 address in its RFC 5952 + // form). + inline bool OnSaPath(const SaPaths& p, std::string_view fromHost, int fromPort, std::string_view toHost, int toPort) { + const int theirs = fromPort == imsd::util::PortUc ? p.portPs : fromPort == imsd::util::PortUs ? p.portPc : 0; + return theirs != 0 && toPort == theirs && imsd::util::CanonicalAddr(fromHost) == imsd::util::CanonicalAddr(p.local) && + imsd::util::CanonicalAddr(toHost) == imsd::util::CanonicalAddr(p.pcscf); + } + // ---- Security-Server (RFC 3329 §2.2) ----------------------------------- // // The P-CSCF answers the challenge with the mechanisms it supports, each diff --git a/interfaces/Imsd-Messages.cppm b/interfaces/Imsd-Messages.cppm index a9d1285..92ef390 100644 --- a/interfaces/Imsd-Messages.cppm +++ b/interfaces/Imsd-Messages.cppm @@ -7,7 +7,9 @@ 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 context and the per-request variables so the builders are pure functions with no hidden state: the engine owns the dialog bookkeeping -and the transport owns the socket, this module only formats bytes. That makes +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 each message byte-pinnable — tests/Messages compares a REGISTER pair and an INVITE with strings cross-generated from the Python reference stack, header for header but for the divergences that suite's header names. @@ -77,7 +79,13 @@ export namespace imsd::msg { // 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 - // the UE is reachable on (stock sends it in every REGISTER/INVITE). + // 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. std::string panInfo; // Advertise SMS over IP in the REGISTER Contact (+g.3gpp.smsip, TS // 24.341 5.3.2.2): the network then delivers terminating short @@ -224,6 +232,18 @@ export namespace imsd::msg { } return s; } + + // 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& 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)); + } } // ---- REGISTER ------------------------------------------------------- @@ -423,6 +443,9 @@ export namespace imsd::msg { // An in-dialog request (PRACK, BYE, UPDATE...) over the dialog route set. // `extra` lines go after CSeq; an optional body sets Content-Type/Length. + // 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). std::string BuildInDialog(const Context& c, const Dialog& d, std::string_view method, int cseq, std::string_view viaBranch, std::span 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)); @@ -438,6 +461,7 @@ export namespace imsd::msg { }; for (const std::string& e : extra) l.push_back(e); + if (!c.panInfo.empty()) l.push_back(std::format("P-Access-Network-Info: {}", c.panInfo)); if (!body.empty()) { l.push_back(std::format("Content-Type: {}", ctype)); l.push_back(std::format("Content-Length: {}", body.size())); @@ -508,7 +532,8 @@ export namespace imsd::msg { // 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...); - // `sdp` turns a 200 into the answer. + // P-Access-Network-Info follows them where the cell is known, in all + // but a 100; `sdp` turns a 200 into the answer. std::string BuildInviteResponse(const Context& c, std::string_view invite, int code, std::string_view text, std::string_view toTag, std::optional sdp = std::nullopt, std::span extra = {}) { std::vector l = {std::format("SIP/2.0 {} {}", code, text)}; for (auto v : imsd::sip::Headers(invite, "Via")) @@ -530,6 +555,7 @@ export namespace imsd::msg { } for (const std::string& e : extra) l.push_back(e); + detail::AccessInfo(l, c, invite, code); if (sdp) { l.push_back("Content-Type: application/sdp"); l.push_back(std::format("Content-Length: {}", sdp->size())); @@ -544,10 +570,12 @@ export namespace imsd::msg { // 488 to one we did not: each Via row echoed in order (RFC 3261 // 8.2.6.2), From, To with // `toTag` added unless the request's To holds `tag=` (8.2.6.2 again), - // Call-ID and CSeq; no body. The shell's answers to any other request + // Call-ID and CSeq, then P-Access-Network-Info where the cell is known; + // no body. The shell's answers to any other request // outside a call but an INVITE (an OPTIONS, a NOTIFY, a method it does - // not handle) keep BuildResponse200's hop-by-hop shape, as before. - std::string BuildResponse(const Context&, std::string_view request, int code, std::string_view text, std::string_view toTag) { + // 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) { std::vector 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)); @@ -557,6 +585,7 @@ export namespace imsd::msg { } 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)); + detail::AccessInfo(l, c, request, code); l.push_back("Content-Length: 0"); return std::format("{}\r\n", detail::Join(l)); } @@ -564,25 +593,30 @@ export namespace imsd::msg { // 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 - // keeps nothing of such a request, so the To tag 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 + // 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 // 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 - // header section. + // header section. The transport answers with it, without the + // registration's context: it carries no P-Access-Network-Info. 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)); } // 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. - // `extra` appends headers (Session-Expires echo for refreshes...). + // `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. std::string BuildResponse200(const Context& c, std::string_view request, std::optional sdp = std::nullopt, std::span extra = {}) { std::vector 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); + detail::AccessInfo(l, c, request, 200); if (sdp) { l.push_back(std::format("Contact: ", ContactUser(c), imsd::util::HostPort(c.local, c.portUs))); l.push_back("Content-Type: application/sdp"); diff --git a/interfaces/Imsd-Sip.cppm b/interfaces/Imsd-Sip.cppm index c672693..18ca65f 100644 --- a/interfaces/Imsd-Sip.cppm +++ b/interfaces/Imsd-Sip.cppm @@ -193,6 +193,26 @@ export namespace imsd::sip { return out; } + // `msg` without its `name:` header lines: every line of the header + // section on which Headers would find the name, long or compact + // (MatchHeaderLine), is left out, and the other lines and the body stay + // as they are. For P-Access-Network-Info as the builders write it, one + // line with its value, that is the header whole. + std::string WithoutHeader(std::string_view msg, std::string_view name) { + std::string out; + std::size_t kept = 0; + ForEachLine(msg, [&](std::string_view line) { + if (MatchHeaderLine(line, name)) { + const auto at = static_cast(line.data() - msg.data()); + out += msg.substr(kept, at - kept); + kept = std::min(at + line.size() + 1, msg.size()); + } + return true; + }); + out += msg.substr(kept); + return out; + } + // The 3-digit code of a "SIP/2.0 NNN ..." response start-line; nullopt // for requests / anything else. Only matches at the start of the message. std::optional Status(std::string_view msg) { diff --git a/tests/Aka/main.cpp b/tests/Aka/main.cpp index 7cdad57..f75c92c 100644 --- a/tests/Aka/main.cpp +++ b/tests/Aka/main.cpp @@ -215,8 +215,7 @@ int main() { { // P-Access-Network-Info from `mmcli --location-get` (real-device // output shape; TAC/CI zero-padded wider than the 4/7 hex digits - // PANI uses). The mcc+mnc+TAC+ECI concatenation was validated - // against a stock modem's on-wire PANI; values here are synthetic. + // PANI uses). Values here are synthetic. constexpr std::string_view Loc = " --------------------------\n" " 3GPP | operator mcc: 001\n" diff --git a/tests/Engine/main.cpp b/tests/Engine/main.cpp index c98ee31..4a7c514 100644 --- a/tests/Engine/main.cpp +++ b/tests/Engine/main.cpp @@ -14,7 +14,11 @@ // ring and are answered at the offer's own payload types, ring timeout -> // 480, BYE both ways; an incoming call from an INVITE, and an outgoing // call from responses, whose header names come in compact form (RFC 3261 -// 7.3.3). DTMF: the tones reach the media leg on an active +// 7.3.3); which of a call's messages carry P-Access-Network-Info where +// the cell is known (its 180, its 200s but the one to a CANCEL, a 486 +// and a 487, PRACK and BYE) and which do not (the 100 Trying, the 200 to +// a CANCEL, the ACKs, the CANCEL). DTMF: the tones reach the media leg +// on an active // call that negotiated telephone-event, and are dropped — with the reason // — before the answer, after the call, without the negotiation or with a // character that is no tone. Then RegRecovery, the registration recovery policy: @@ -917,6 +921,67 @@ int main() { Check(m.Terminated(), "terminated"); } + // ---- P-Access-Network-Info through a call's life, where the cell is + // known: in the 180, the 200s but a CANCEL's, a 486 and a 487, and + // in PRACK and BYE; in no 100 Trying, no ACK and no CANCEL + { + Context k = MakeCtx(); + k.panInfo = "3GPP-E-UTRAN-FDD;utran-cell-id-3gpp=0010112340ABCDE0"; + const auto with = [](const Action* a) { return a && a->text.contains("\r\nP-Access-Network-Info: 3GPP-E-UTRAN-FDD;utran-cell-id-3gpp=0010112340ABCDE0\r\n"); }; + const auto without = [](const Action* a) { return a && !a->text.contains("P-Access-Network-Info"); }; + const auto inDialog = [](std::string_view method, std::string_view callid, std::string_view extra = "") { + return std::format( + "{} sip:001010123456789@[2001:db8::db43]:45061 SIP/2.0\r\n" + "Via: SIP/2.0/UDP [2001:db8::105]:5100;branch=z9hG4bKpcscf9\r\n" + "From: ;tag=caller1\r\n" + "To: ;tag=ours\r\n" + "Call-ID: {}\r\nCSeq: 2 {}\r\n{}Content-Length: 0\r\n\r\n", method, callid, method, extra); + }; + { + // an incoming call its caller cancels + imsd::util::Rng rng(71); + CallMachine m(k, rng, "ims-call-80", IncomingInvite{MtInvite("Require: 100rel\r\n")}, 50004); + auto ai = m.OnInvite(); + Check(with(Responded(ai, "SIP/2.0 180 Ringing")) && without(Responded(ai, "SIP/2.0 100 Trying")), "a cell known: the 180 to an incoming INVITE carries P-Access-Network-Info, its 100 Trying does not"); + Check(with(Responded(m.OnRequest(inDialog("PRACK", m.CallId(), "RAck: 1 1 INVITE\r\n")), "SIP/2.0 200 OK")), "... and the 200 to the PRACK of that 180"); + auto ac = m.OnRequest(inDialog("CANCEL", m.CallId())); + Check(without(Responded(ac, "SIP/2.0 200 OK")) && with(Responded(ac, "SIP/2.0 487 Request Terminated")), "... the 200 to a CANCEL does not, the INVITE's 487 does"); + } + { + // an incoming call answered, then refreshed and ended from the far side + imsd::util::Rng rng(72); + CallMachine m(k, rng, "ims-call-81", IncomingInvite{MtInvite()}, 50004); + m.OnInvite(); + Check(with(Responded(m.OnAccept(), "SIP/2.0 200 OK")), "... the 200 that answers the call"); + Check(with(Responded(m.OnRequest(inDialog("UPDATE", m.CallId(), "Session-Expires: 1800;refresher=uac\r\n")), "SIP/2.0 200 OK")), "... the 200 to a session refresh"); + Check(with(Responded(m.OnRequest(inDialog("BYE", m.CallId())), "SIP/2.0 200 OK")), "... and the 200 to the far side's BYE"); + } + { + // an incoming call refused, and one answered and ended here + imsd::util::Rng rng(73); + CallMachine m(k, rng, "ims-call-82", IncomingInvite{MtInvite()}, 50004); + m.OnInvite(); + Check(with(Responded(m.OnHangup(), "SIP/2.0 486 Busy Here")), "... a 486"); + CallMachine n(k, rng, "ims-call-83", IncomingInvite{MtInvite()}, 50004); + n.OnInvite(); + n.OnAccept(); + Check(with(Sent(n.OnHangup(), "BYE ")), "... our BYE on a call that came in"); + } + { + // a call we place: its PRACK and its BYE carry it, its ACKs and its CANCEL do not + imsd::util::Rng rng(74); + CallMachine m(k, rng, "ims-call-84", "1233", 50004, false); + m.Start(); + Check(with(Sent(m.OnInviteResponse(R183(m.CallId())), "PRACK ")), "... the PRACK of a call we place"); + Check(without(Sent(m.OnInviteResponse(R200(m.CallId())), "ACK ")), "... not the ACK of its 200"); + Check(with(Sent(m.OnHangup(), "BYE ")), "... its BYE"); + CallMachine n(k, rng, "ims-call-85", "1233", 50004, false); + n.Start(); + Check(without(Sent(n.OnHangup(), "CANCEL ")), "... not a CANCEL"); + Check(without(Sent(n.OnInviteResponse(Final(n.CallId(), 487, "Request Terminated")), "ACK ")), "... and not the ACK of a refusal"); + } + } + // ================= emergency (stage 1) ================================ // ---- emergency dial: the INVITE targets the sos URN over the normal diff --git a/tests/Ipsec/main.cpp b/tests/Ipsec/main.cpp index 5bdf223..6512087 100644 --- a/tests/Ipsec/main.cpp +++ b/tests/Ipsec/main.cpp @@ -8,7 +8,8 @@ // prints, with its line for each algorithm imsd installs, and // the Security-Server parser, fed the operator lines as captured (O2 UK's // six-mechanism Mavenir shape verbatim, under a null and under an aes-cbc -// offer; KPN's single mechanism; Odido's), and the EALG list. +// offer; KPN's single mechanism; Odido's), the EALG list, and the two +// paths an SA pair's outbound policies select (OnSaPath). import std; import Imsd; @@ -476,6 +477,22 @@ int main() { Check(NormalizeEalg("") == "null" && NormalizeEalg("3des") == "des-ede3-cbc" && NormalizeEalg("aes-cbc") == "aes-cbc", "NormalizeEalg"); } + // ---- OnSaPath: the two paths an SA pair's outbound policies select + { + const SaPaths p{"2001:db8::db43", "2001:db8::105", 6000, 33421}; + Check(OnSaPath(p, "2001:db8::db43", 45061, "2001:db8::105", 6000), "OnSaPath: the client port to the P-CSCF's server port"); + Check(OnSaPath(p, "2001:db8::db43", 45062, "2001:db8::105", 33421), "OnSaPath: the server port to the P-CSCF's client port"); + Check(!OnSaPath(p, "2001:db8::db43", 45061, "2001:db8::105", 33421) && !OnSaPath(p, "2001:db8::db43", 45062, "2001:db8::105", 6000), "OnSaPath: not the client port to its client port, nor the server port to its server port"); + Check(!OnSaPath(p, "2001:db8::db43", 45062, "2001:db8::105", 33422), "OnSaPath: not another port of the P-CSCF"); + Check(!OnSaPath(p, "2001:db8::db43", 45062, "2001:db8::106", 33421), "OnSaPath: not another host on the P-CSCF's port"); + Check(!OnSaPath(p, "2001:db8::db44", 45062, "2001:db8::105", 33421), "OnSaPath: not from another address of ours"); + Check(!OnSaPath(p, "2001:db8::db43", 5060, "2001:db8::105", 6000) && !OnSaPath(p, "2001:db8::db43", 5060, "2001:db8::105", 33421), "OnSaPath: not from a port that is neither protected port"); + Check(OnSaPath(p, "2001:DB8:0:0:0:0:0:DB43", 45061, "2001:0db8::0105", 6000) && OnSaPath(SaPaths{"2001:DB8:0:0:0:0:0:DB43", "2001:0db8::0105", 6000, 33421}, "2001:db8::db43", 45061, "2001:db8::105", 6000), "OnSaPath: an IPv6 address in another spelling is the same address, the packet's or the record's"); + Check(!OnSaPath(SaPaths{}, "2001:db8::db43", 45061, "2001:db8::105", 6000) && !OnSaPath(SaPaths{}, "", 45061, "", 0), "OnSaPath: no pair on record, no path"); + const SaPaths four{"192.0.2.2", "192.0.2.1", 6000, 33421}; + Check(OnSaPath(four, "192.0.2.2", 45062, "192.0.2.1", 33421) && !OnSaPath(four, "192.0.2.2", 45062, "192.0.2.3", 33421), "OnSaPath: over IPv4 a path of the pair, and not another host on its port"); + } + if (Failures == 0) std::println("Ipsec: 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 } diff --git a/tests/Messages/main.cpp b/tests/Messages/main.cpp index 2916c7a..ec2994e 100644 --- a/tests/Messages/main.cpp +++ b/tests/Messages/main.cpp @@ -21,7 +21,10 @@ // one cipher. The two REGISTERs once more on an identity an ISIM holds: the // pinned ones with the card's names in place of the derived ones. The INVITE, // its CANCEL and its ACKs once more for a dialog with another sequence number, -// session interval and Min-SE than the pinned 1, 1800 and 90. Then the +// session interval and Min-SE than the pinned 1, 1800 and 90. Which +// messages carry P-Access-Network-Info where the cell is known, and that the +// PRACK, the BYE and the responses that carry it carry none where it is not. +// Then the // SMS over IP shapes: the // +g.3gpp.smsip Contact tag behind Context::smsOverIp, the MESSAGE carrying // an RP-DATA to the service centre and the one carrying an RP-ACK to the @@ -423,11 +426,11 @@ int main() { Check(prack.contains("To: ;tag=remote99\r\n"), "PRACK To has remote tag"); Check(prack.contains("CSeq: 2 PRACK\r\n"), "PRACK CSeq"); Check(prack.contains("RAck: 42 1 INVITE\r\n"), "PRACK RAck"); - Check(prack.ends_with("Content-Length: 0\r\n\r\n"), "PRACK empty body"); + Check(prack.ends_with("RAck: 42 1 INVITE\r\nP-Access-Network-Info: 3GPP-E-UTRAN-FDD;utran-cell-id-3gpp=0010112340ABCDE0\r\nContent-Length: 0\r\n\r\n"), "PRACK: P-Access-Network-Info after its RAck, an empty body"); std::string bye = BuildInDialog(c, d, "BYE", 3, "byebranch"); Check(bye.starts_with("BYE sip:termgw@2001:db8::9 SIP/2.0\r\n"), "BYE line"); - Check(bye.contains("CSeq: 3 BYE\r\n"), "BYE CSeq"); + Check(bye.ends_with("CSeq: 3 BYE\r\nP-Access-Network-Info: 3GPP-E-UTRAN-FDD;utran-cell-id-3gpp=0010112340ABCDE0\r\nContent-Length: 0\r\n\r\n"), "BYE: its CSeq, then P-Access-Network-Info"); std::string ack = BuildAck2xx(c, d, "ackbranch", ""); Check(ack.starts_with("ACK sip:termgw@2001:db8::9 SIP/2.0\r\n"), "ACK 2xx target"); @@ -438,6 +441,12 @@ int main() { Check(cancel.starts_with(std::format("CANCEL {} SIP/2.0\r\n", d.ruri)), "CANCEL request-URI = ruri"); Check(cancel.contains("branch=z9hG4bKib;rport\r\n"), "CANCEL reuses INVITE branch"); Check(cancel.contains("CSeq: 1 CANCEL\r\n"), "CANCEL CSeq copies INVITE"); + // RFC 7315 5.7: the header appears in no ACK and no CANCEL + Check(!ack.contains("P-Access-Network-Info") && !cancel.contains("P-Access-Network-Info") && !BuildAckNon2xx(c, d, ";tag=z").contains("P-Access-Network-Info"), "neither ACK nor the CANCEL carries P-Access-Network-Info"); + // where the cell is not known the in-dialog requests carry none either + Context n = MakeCtx(); + n.panInfo.clear(); + Check(BuildInDialog(n, d, "PRACK", 2, "prbranch", rack).ends_with("RAck: 42 1 INVITE\r\nContent-Length: 0\r\n\r\n") && BuildInDialog(n, d, "BYE", 3, "byebranch").ends_with("CSeq: 3 BYE\r\nContent-Length: 0\r\n\r\n"), "no cell known: PRACK and BYE without the header"); } { // non-2xx ACK uses the INVITE branch + registration route + resp To @@ -478,12 +487,21 @@ int main() { Check(r.contains("Via: SIP/2.0/TCP [a::1]:45061;branch=z9hG4bKother\r\n"), "200 echoes Via"); Check(r.contains("Call-ID: xyz@far\r\n"), "200 echoes Call-ID"); Check(r.contains("CSeq: 7 BYE\r\n"), "200 echoes CSeq"); - Check(r.ends_with("Content-Length: 0\r\n\r\n"), "200 empty body"); + Check(r.ends_with("CSeq: 7 BYE\r\nP-Access-Network-Info: 3GPP-E-UTRAN-FDD;utran-cell-id-3gpp=0010112340ABCDE0\r\nContent-Length: 0\r\n\r\n"), "200 to a BYE: P-Access-Network-Info after the echoed five, an empty body"); // with an SDP answer: our Contact + application/sdp appear std::string r2 = BuildResponse200(c, req, Sdp); Check(r2.contains("Content-Type: application/sdp\r\n"), "200 w/ SDP content-type"); Check(r2.contains("Contact: \r\n"), "200 w/ SDP includes our Contact"); Check(r2.ends_with(Sdp), "200 w/ SDP body"); + Check(r2.contains("CSeq: 7 BYE\r\nP-Access-Network-Info: 3GPP-E-UTRAN-FDD;utran-cell-id-3gpp=0010112340ABCDE0\r\nContact: "), "200 w/ SDP: P-Access-Network-Info before our Contact"); + // TS 24.229 5.1.2A.1.1, 5.1.2A.2: "(except CANCEL responses)" + std::string cancelReq = req; + cancelReq.replace(0, 3, "CANCEL"); + cancelReq.replace(cancelReq.find("CSeq: 7 BYE"), 11, "CSeq: 7 CANCEL"); + Check(BuildResponse200(c, cancelReq).ends_with("CSeq: 7 CANCEL\r\nContent-Length: 0\r\n\r\n"), "200 to a CANCEL: no P-Access-Network-Info"); + Context n = MakeCtx(); + n.panInfo.clear(); + Check(BuildResponse200(n, req).ends_with("CSeq: 7 BYE\r\nContent-Length: 0\r\n\r\n") && !BuildResponse200(n, req, Sdp).contains("P-Access-Network-Info"), "no cell known: the 200 without the header"); } // ---- s53 oracle diff: UUID Contact user-part + User-Agent ------------ @@ -569,7 +587,9 @@ int main() { "Call-ID: in1@net\r\n" "CSeq: 9 MESSAGE\r\n" "Content-Length: 0\r\n\r\n"; - Eq(BuildResponse(c, inbound, 488, "Not Acceptable Here", "T488"), + Context noCell = MakeCtx(); + noCell.panInfo.clear(); + Eq(BuildResponse(noCell, inbound, 488, "Not Acceptable Here", "T488"), "SIP/2.0 488 Not Acceptable Here\r\n" "Via: SIP/2.0/UDP [2001:db8::105]:6000;branch=z9hG4bKnet1\r\n" "Via: SIP/2.0/UDP [2001:db8::77]:5060;branch=z9hG4bKgw0\r\n" @@ -578,6 +598,16 @@ int main() { "Call-ID: in1@net\r\n" "CSeq: 9 MESSAGE\r\n" "Content-Length: 0\r\n\r\n", "a 488 for an inbound MESSAGE echoes every Via and tags To"); + Eq(BuildResponse(c, inbound, 488, "Not Acceptable Here", "T488"), + "SIP/2.0 488 Not Acceptable Here\r\n" + "Via: SIP/2.0/UDP [2001:db8::105]:6000;branch=z9hG4bKnet1\r\n" + "Via: SIP/2.0/UDP [2001:db8::77]:5060;branch=z9hG4bKgw0\r\n" + "From: ;tag=N1\r\n" + "To: ;tag=T488\r\n" + "Call-ID: in1@net\r\n" + "CSeq: 9 MESSAGE\r\n" + "P-Access-Network-Info: 3GPP-E-UTRAN-FDD;utran-cell-id-3gpp=0010112340ABCDE0\r\n" + "Content-Length: 0\r\n\r\n", "and where the cell is known, P-Access-Network-Info after its CSeq"); const std::string tagged = "MESSAGE sip:me@ims SIP/2.0\r\nVia: SIP/2.0/UDP [2001:db8::105]:6000;branch=z9hG4bKnet2\r\nFrom: ;tag=N2\r\nTo: ;tag=had\r\nCall-ID: in2@net\r\nCSeq: 1 MESSAGE\r\nContent-Length: 0\r\n\r\n"; Check(BuildResponse(c, tagged, 200, "OK", "unused").contains("\r\nTo: ;tag=had\r\n"), "a To that already carries a tag is echoed as it is"); // the 400 to a request whose datagram ended before its body did (RFC 3261 18.3), built from what imsd::sip::CutShortRequest gives of it @@ -598,6 +628,7 @@ int main() { "CSeq: 9 MESSAGE\r\n" "Content-Length: 0\r\n\r\n", "its 400 echoes every Via and tags To with sixteen hex digits of the MD5 of the secret and the header section"); Eq(BuildBadRequest(imsd::sip::CutShortRequest(cut + "s").value_or(""), secret), BuildBadRequest(head, secret), "the same request cut short elsewhere draws the same 400, tag and all"); + Check(!BuildBadRequest(head, secret).contains("P-Access-Network-Info"), "the 400 is built without the registration's context: no P-Access-Network-Info"); const auto toTag = [](std::string_view r) { const std::size_t at = r.find(";tag=", r.find("\r\nTo: ")); return r.substr(at, r.find("\r\n", at) - at); }; const std::string r1 = BuildBadRequest(head, secret); std::string other = cut; @@ -644,6 +675,18 @@ int main() { "i: call3@net\r\n" "CSeq: 1 INVITE\r\n" "l: 0\r\n\r\n"; + Eq(BuildInviteResponse(noCell, invite, 180, "Ringing", "T180"), + "SIP/2.0 180 Ringing\r\n" + "Via: SIP/2.0/TCP [2001:db8::105]:6000;branch=z9hG4bKnet3\r\n" + "Via: SIP/2.0/UDP [2001:db8::77]:5060;branch=z9hG4bKgw3\r\n" + "Record-Route: \r\n" + "From: ;tag=N3\r\n" + "To: ;tag=T180\r\n" + "Call-ID: call3@net\r\n" + "CSeq: 1 INVITE\r\n" + "Contact: \r\n" + "Allow: INVITE, ACK, BYE, CANCEL, OPTIONS, PRACK, UPDATE, MESSAGE, REFER\r\n" + "Content-Length: 0\r\n\r\n", "a 180 for an INVITE echoes every Via and the Record-Route, tags To and carries our Contact and Allow"); const std::string ringing = BuildInviteResponse(c, invite, 180, "Ringing", "T180"); Eq(ringing, "SIP/2.0 180 Ringing\r\n" @@ -656,7 +699,21 @@ int main() { "CSeq: 1 INVITE\r\n" "Contact: \r\n" "Allow: INVITE, ACK, BYE, CANCEL, OPTIONS, PRACK, UPDATE, MESSAGE, REFER\r\n" - "Content-Length: 0\r\n\r\n", "a 180 for an INVITE echoes every Via and the Record-Route, tags To and carries our Contact and Allow"); + "P-Access-Network-Info: 3GPP-E-UTRAN-FDD;utran-cell-id-3gpp=0010112340ABCDE0\r\n" + "Content-Length: 0\r\n\r\n", "and where the cell is known, P-Access-Network-Info after them"); + { + const std::array reliable = {"Require: 100rel", "RSeq: 1"}; + Check(BuildInviteResponse(c, invite, 180, "Ringing", "T180", std::nullopt, reliable).ends_with("Require: 100rel\r\nRSeq: 1\r\nP-Access-Network-Info: 3GPP-E-UTRAN-FDD;utran-cell-id-3gpp=0010112340ABCDE0\r\nContent-Length: 0\r\n\r\n"), "a reliable 180: P-Access-Network-Info after its Require and RSeq"); + Check(BuildInviteResponse(c, invite, 100, "Trying", "unused").ends_with("CSeq: 1 INVITE\r\nContent-Length: 0\r\n\r\n"), "a 100 Trying carries none"); + Check(BuildInviteResponse(c, invite, 486, "Busy Here", "T486").ends_with("CSeq: 1 INVITE\r\nP-Access-Network-Info: 3GPP-E-UTRAN-FDD;utran-cell-id-3gpp=0010112340ABCDE0\r\nContent-Length: 0\r\n\r\n"), "and a 486"); + Check(BuildInviteResponse(c, invite, 200, "OK", "T200", "v=0\r\n").ends_with("P-Access-Network-Info: 3GPP-E-UTRAN-FDD;utran-cell-id-3gpp=0010112340ABCDE0\r\nContent-Type: application/sdp\r\nContent-Length: 5\r\n\r\nv=0\r\n"), "and a 200 with its answer, before the Content-Type"); + Check(!BuildInviteResponse(noCell, invite, 100, "Trying", "unused").contains("P-Access-Network-Info") && !BuildInviteResponse(noCell, invite, 200, "OK", "T200", "v=0\r\n").contains("P-Access-Network-Info"), "no cell known: a 100 and a 200 without the header"); + Check(BuildInviteResponse(noCell, invite, 180, "Ringing", "T180", std::nullopt, reliable).ends_with("Require: 100rel\r\nRSeq: 1\r\nContent-Length: 0\r\n\r\n") && BuildInviteResponse(noCell, invite, 486, "Busy Here", "T486").ends_with("CSeq: 1 INVITE\r\nContent-Length: 0\r\n\r\n"), "no cell known: a reliable 180 and a 486 without it"); + // the exception is for a response to a CANCEL, not for one to a request that names the method + std::string allowing = invite; + allowing.insert(allowing.find("Content-Length:"), "Allow: INVITE, ACK, CANCEL, BYE\r\n"); + Check(BuildInviteResponse(c, allowing, 180, "Ringing", "T180") == ringing, "an INVITE whose Allow names CANCEL is answered like any other"); + } Eq(BuildInviteResponse(c, compactInvite, 180, "Ringing", "T180"), ringing, "a 180 for an INVITE in compact form is the one for its long form"); Eq(BuildInviteResponse(c, compactInvite, 486, "Busy Here", "T486"), BuildInviteResponse(c, invite, 486, "Busy Here", "T486"), "and so is a 486"); Check(BuildInviteResponse(c, invite, 486, "Busy Here", "T486").contains("\r\nTo: ;tag=T486\r\n") && !BuildInviteResponse(c, invite, 486, "Busy Here", "T486").contains("Record-Route"), "a 486 tags To and returns no Record-Route"); diff --git a/tests/Sip/main.cpp b/tests/Sip/main.cpp index 9df0cee..4d3784a 100644 --- a/tests/Sip/main.cpp +++ b/tests/Sip/main.cpp @@ -11,8 +11,10 @@ // (CutShortRequest), a refused final's Retry-After and summary (RetryAfter, // RefusalSummary), the interval a 422's Min-SE names (MinSe), transaction // matching (ResponseOf), our binding in a -// reg-event document (RegInfoContactState), and HeaderUri and Body. These pin -// the module's edge-case behavior; see the module header before "fixing" one. +// reg-event document (RegInfoContactState), HeaderUri and Body, and a +// message less every line on which one header is found (WithoutHeader). +// These pin the module's edge-case behavior; see the module header before +// "fixing" one. import std; import Imsd; @@ -569,6 +571,34 @@ int main() { Check(imsd::sip::Body("OPTIONS sip:x SIP/2.0\r\nCSeq: 1 OPTIONS\r\n").empty(), "Body: none without a terminator"); } + // ---- WithoutHeader: a message less every line on which one header is + // found + { + const std::string r = + "SIP/2.0 200 OK\r\n" + "Via: SIP/2.0/UDP [2001:db8::105]:6000;branch=z9hG4bKnet1\r\n" + "P-Access-Network-Info: 3GPP-E-UTRAN-FDD;utran-cell-id-3gpp=0010112340ABCDE0\r\n" + "Content-Type: text/plain\r\n" + "Content-Length: 31\r\n" + "\r\n" + "P-Access-Network-Info: a body\r\n"; + const std::string bare = + "SIP/2.0 200 OK\r\n" + "Via: SIP/2.0/UDP [2001:db8::105]:6000;branch=z9hG4bKnet1\r\n" + "Content-Type: text/plain\r\n" + "Content-Length: 31\r\n" + "\r\n" + "P-Access-Network-Info: a body\r\n"; + Check(WithoutHeader(r, "P-Access-Network-Info") == bare, "WithoutHeader: the header's line is gone, the other lines and a body line that spells its name stay"); + Check(WithoutHeader(bare, "P-Access-Network-Info") == bare, "WithoutHeader: a message without the header comes back as it is"); + Check(WithoutHeader(r, "p-access-network-info") == bare, "WithoutHeader: the name in another letter case"); + Check(WithoutHeader(r, "P-Access-Network") == r && WithoutHeader(r, "Access-Network-Info") == r, "WithoutHeader: a name that is only part of a header's name takes nothing"); + const std::string twice = "BYE sip:a@b SIP/2.0\r\nP-Access-Network-Info: one\r\nCSeq: 2 BYE\r\nP-Access-Network-Info: two\r\n\r\n"; + Check(WithoutHeader(twice, "P-Access-Network-Info") == "BYE sip:a@b SIP/2.0\r\nCSeq: 2 BYE\r\n\r\n", "WithoutHeader: every line of the name, the last before the empty line too"); + Check(WithoutHeader("SIP/2.0 200 OK\r\nVia: x\r\nP-Access-Network-Info: v", "P-Access-Network-Info") == "SIP/2.0 200 OK\r\nVia: x\r\n", "WithoutHeader: a last line without its line end"); + Check(WithoutHeader("SIP/2.0 200 OK\r\nv: x\r\nTo: y\r\nVia: z\r\n\r\n", "Via") == "SIP/2.0 200 OK\r\nTo: y\r\n\r\n", "WithoutHeader: a header under its compact form goes as under its long one"); + } + if (Failures == 0) std::println("Sip: all tests passed"); return Failures != 0; // not the count: `crafter-build test` takes an exit status of 77 for a skipped suite, and this suite has more checks than that }