Counting Diameter per subscriber when the answers carry no IMSI
A Wireshark Lua tap that got the counts wrong twice, and the field that fixed it.
The problem the field layout creates
In S6a, a request carries the subscriber identity — e212.imsi sits in the Authentication-Information-Request (command 318, application 16777251). The answer omits it. So "how many requests did this IMSI make, and how many did it get back" cannot be answered by reading responses, because a response does not know whose it is.
That one asymmetry is the whole engineering problem, and both of my first two attempts walked into it.
Two versions that counted wrong
diameterv2.lua listened on diameter.flags.request == 1 and keyed its table on the IMSI. It produced request counts per subscriber, correctly, and no response counts at all; the filter excluded them.
diameterreqres.lua widened the filter to both directions (request == 1 || request == 0) and kept the IMSI as the key. That is worse than the first version, because now responses arrive, have no IMSI, and get attributed to whatever the key lookup happens to find. The README of the version that replaced both says it without ceremony: fixed counting issues that previously caused incorrect request/response counts.
Key on the end-to-end ID, not on the IMSI
Diameter already carries the thing needed here. diameter.endtoendid is set by the originator and copied into the answer (a 32-bit value, unique per originator for long enough), so it is the one label both halves of a transaction share. The working version keys on it:
local tap_diameter = Listener.new(nil, "diameter")
-- one entry per end-to-end id
endtoend_table[id] = { imsi, request_count, response_count,
request_codes, response_results }
One Listener on the whole diameter filter, one entry per end-to-end ID. A request sets the entry's IMSI and bumps request_count; a response bumps response_count and never touches the IMSI field. At draw() time the table is folded by IMSI, merging every end-to-end ID that shared one. Single pass over the capture; the attribution holds because it never depends on a response knowing anything about itself.
The second problem: several messages in one packet
A TCP segment can carry more than one Diameter message, so a field extractor returns every occurrence, not one value. The script pulls each field as a list ({endtoend_f()}), splits any comma-joined string form, and then zips the per-message lists positionally:
local max_messages = math.max(#endtoend_values,
#request_flag_values,
#cmd_code_values)
IMSI is the exception: it falls back to imsi_values[1] when there is no value at position i, because one subscriber's identity is often shared across the messages in a packet. That fallback is a guess, and it is the weakest line in the script.
What it found
A 38 MB capture (38,330 Diameter packets), 3,456 distinct IMSIs, 19,930 requests in total. Sorted by request count, the first row is one IMSI with 1,345 requests, all command 318, all answered 5001:
IMSI | Req | Resp | Mismatch | Request Codes | Response Codes
311210393865041 | 1345 | 1345 | NO | 318(1345) | 318:5001(1345)
One subscriber asking for authentication vectors 1,345 times in one capture window (requests and responses matched, so nothing timed out) and being refused every time. The Mismatch column (YES when requests and responses differ for an IMSI) is the column people actually read, because an unanswered request is a timeout somewhere and a timeout is the thing being hunted.
Two limits worth stating
The response key is cmd:result, built from diameter.Result-Code when present and diameter.Experimental-Result-Code otherwise — and the key does not record which of the two it came from. 318:5001 is therefore ambiguous: 5001 as an experimental result is DIAMETER_ERROR_USER_UNKNOWN, while 5001 in the base protocol is DIAMETER_AVP_UNSUPPORTED. The tool prints a number and leaves the disambiguation to the reader — which is fine for one IMSI and poor for a report.
The summary also drops any end-to-end ID whose request was outside the capture, because the fold only keeps entries that have an IMSI. The debug output shows them (IMSI: nil, Req: 0, Resp: 1), so an orphaned answer is visible and uncounted.
The Go part is a wrapper
Worth saying plainly, since the directory contains Go files: main.go is about 40 lines (one flag.Parse, one embed, one call). It //go:embeds the Lua script, parses one argument, and hands both to tshark. There is no Go analysis, no streaming processor, no parallel chunking; the Go exists to make a binary that carries its own Lua. The entire tool is a Lua tap plus a flag parser, and that is why it is 300 lines instead of 3,000.