©Code by Samnoon
Blog
← All posts

isdn

Reading ISDN signalling: Q.921 and Q.931, byte by byte

How LAPD frames carry call control, what's inside a SETUP, and how cause codes explain a failed call.

12 min read

ISDN sounds like something from a museum, but its signalling is still around. Primary rate lines still connect PBXs to phone networks in plenty of places, and the ideas behind Q.931 outlived a lot of the hardware: SIP can even carry Q.931-style cause codes in its Reason header. If you work anywhere near telephony, sooner or later someone will hand you a hex dump of D-channel traffic and ask what went wrong.

This post reads both layers by hand. Q.921 gets frames across the link, and Q.931 uses those frames to set up and clear calls. Everything here comes from the ITU-T recommendations, which are free to download, and the sources are listed at the end.

Two layers, two jobs

An ISDN interface splits its capacity into B-channels, which carry the audio at 64 kbit/s each, and a D-channel, which carries signalling. A basic rate interface has two B-channels and a 16 kbit/s D-channel. A primary rate interface on an E1 line has 30 B-channels and a 64 kbit/s D-channel (on a North American T1 it's 23 plus one).

The D-channel has a small protocol stack of its own:

LayerProtocolJob
2, data linkQ.921 (LAPD)Delivers frames in order and retransmits lost ones
3, networkQ.931Sets up, tracks and clears calls

That split is the first tool for debugging. If Q.921 keeps re-establishing the link, the problem is below call control. If Q.921 stays up and calls still fail, read the Q.931 messages.

Q.921: frames on the D-channel

Q.921, also called LAPD (Link Access Procedure on the D-channel), belongs to the HDLC family, so the framing will look familiar if you've met PPP or LAPB:

| Flag | Address | Control   | Information | FCS     | Flag |
| 7E   | 2 bytes | 1-2 bytes | 0-260 bytes | 2 bytes | 7E   |

Flags mark where a frame starts and ends. To keep 7E from turning up inside a frame by accident, the sender inserts a 0 bit after any five consecutive 1 bits and the receiver strips it out again (bit stuffing). The FCS is a 16-bit CRC, the same one HDLC and PPP use. In practice an HDLC controller handles flags, stuffing and the FCS in hardware, so what software and most trace tools show starts at the address and ends at the last byte of information. The examples below are all in that form.

The address field

byte 1:  SAPI (6 bits) | C/R | EA = 0
byte 2:  TEI  (7 bits)       | EA = 1

The SAPI says which layer-3 protocol the frame is for. SAPI 0 is call control, meaning Q.931, and SAPI 63 is layer-2 management, used to assign TEIs. The TEI identifies the terminal: 0 to 63 are fixed, 64 to 126 are assigned automatically, and 127 is broadcast. Point-to-point links such as a primary rate interface use a fixed TEI, normally 0. EA marks the last byte of the address.

C/R marks a frame as a command or a response, and its meaning flips with direction:

Sent byCommandResponse
User sideC/R = 0C/R = 1
Network sideC/R = 1C/R = 0

So you can't decode C/R without knowing who sent the frame. For SAPI 0 and TEI 0, the address is 00 01 for user commands and network responses, and 02 01 for network commands and user responses.

The control field

I-frames carry the Q.931 messages. Their two-byte control field holds N(S), this frame's sequence number, and N(R), the number of the next frame the sender expects to receive. N(R) is how acknowledgement works: an N(R) of 5 means "I have everything up to 4".

S-frames acknowledge and control flow without carrying data. RR (01) means "keep going", RNR (05) means "pause", and REJ (09) means "resend from N(R)". Their second byte holds N(R) and the P/F bit.

U-frames bring the link up and down. They're a single byte, and the value depends on the P/F bit:

FrameP/F = 0P/F = 1Meaning
SABME6F7F"Let's start"
UA6373"OK"
DISC4353"Let's stop"
DM0F1F"I'm not up"
UI0313Unnumbered information, no acknowledgement

The P/F bit is called Poll in a command and Final in a response. A command with P=1 demands an immediate answer, and the answer comes back with F=1.

One side sends SABME with P=1, the other answers UA with F=1, both reset their sequence numbers to zero, and I-frames can start to flow. After that, a handful of timers and counters keep the link honest:

NameDefaultWhat it controls
T2001 sHow long to wait for an acknowledgement before retransmitting
N2003How many retries before giving up
T20310 sHow long the link may sit idle before one side checks on it
N201260Maximum bytes in an information field
k7Unacknowledged I-frames in flight (1 for SAPI 0 on basic rate)

When the link has been idle for T203, a side polls with an RR command, P=1, and the other has to answer with an RR response, F=1. When T200 expires without an acknowledgement, the sender polls and retransmits. After N200 failures it gives up and re-establishes the link with a new SABME, which usually takes any calls being set up at that moment down with it.

That gives you a quick way to read a misbehaving link. One that drops every ten-odd seconds like clockwork usually has a side that isn't answering the T203 polls. One that only drops under load points at acknowledgements arriving late or not at all.

Q.931: what the frames say

Every Q.931 message starts the same way:

| Protocol discriminator | Call reference             | Message type | Information elements |
| 1 byte, 08             | length byte + 0 to 2 bytes | 1 byte       | the rest             |

The protocol discriminator is 08 for Q.931. The call reference says which call the message belongs to, since many calls share one D-channel. Its first byte gives the length (1 on basic rate, 2 on primary rate), and the top bit of the value is the call reference flag: 0 in messages from the side that started the call, 1 in messages from the other side. Call reference 1 from the caller is 00 01. The same call, in a message coming back, is 80 01.

These are the message types you'll see most:

TypeMessageTypeMessage
05SETUP45DISCONNECT
02CALL PROCEEDING4DRELEASE
01ALERTING5ARELEASE COMPLETE
07CONNECT7DSTATUS
0FCONNECT ACKNOWLEDGE75STATUS ENQUIRY

Information elements come in two shapes. Single-byte IEs have the top bit set and pack identifier and value into that one byte: Sending complete is A1, and Shift is 9x. Variable-length IEs have the top bit clear and work like a small TLV, with an identifier, a length and the contents. The variable-length ones you'll read most:

IDIEWhat it tells you
04Bearer capabilitySpeech, 3.1 kHz audio or data, and the codec law
08CauseWhy a call was cleared or rejected
18Channel identificationWhich B-channel carries the audio
1EProgress indicatorTones are available in-band, or the call left ISDN
6CCalling party numberWho's calling
70Called party numberWho's being called

One call, both layers

Here's a primary rate link coming up and the first two messages of a call, in hex. U→N is the user side sending, N→U is the network side.

1  U→N  00 01 7F
2  N→U  00 01 73
3  U→N  00 01 00 00  08 02 00 01 05 04 03 80 90 A3 18 03 A9 83 81 70 0B 81 35 35 35 35 35 35 30 31 30 30
4  N→U  00 01 01 02
5  N→U  02 01 00 02  08 02 80 01 02 18 03 A9 83 81
6  U→N  02 01 01 02

Lines 1 and 2 are the handshake. 00 01 is SAPI 0, TEI 0, C/R 0: from the user side that's a command, and 7F is SABME with P=1. The same address bytes from the network side are a response, and 73 is UA with F=1.

Line 3 is an I-frame from the user. Control 00 00 means N(S)=0 and N(R)=0. Everything after it is a Q.931 SETUP, taken apart below.

Line 4 is the network's RR with N(R)=1: "got your frame 0, send frame 1 next".

Line 5 is the network's first I-frame. Address 02 01 makes it a network command, N(S) is 0 and N(R) is 1, and the payload is a CALL PROCEEDING for call reference 80 01.

Line 6 is the user's RR acknowledging it.

The SETUP from line 3, byte by byte:

BytesMeaning
08Q.931
02 00 01Call reference: 2 bytes, value 1, flag 0 (from the caller)
05SETUP
04 03 80 90 A3Bearer capability: speech, 64 kbit/s circuit mode, G.711 A-law
18 03 A9 83 81Channel identification: primary rate, exclusive, B-channel 1
70 0B 81 35 … 30Called party number: unknown type, ISDN numbering plan, digits 5555550100

In the bearer capability, A3 means A-law; µ-law would be A2, and a mismatch between the two ends gets you either a rejection or some truly awful audio. In the channel identification, exclusive means "this channel and no other", so if B-channel 1 is busy the call fails instead of moving to another one. The called number is plain ASCII after a one-byte header.

From there, a normal call runs like this:

User side                                          Network side
    |--- SETUP (00 01) ------------------------------->|
    |<------------------------ CALL PROCEEDING (80 01) |
    |<------------------------------- ALERTING (80 01) |
    |<-------------------------------- CONNECT (80 01) |
    |--- CONNECT ACKNOWLEDGE (00 01) ------------------>|
    |                  ... conversation ...            |
    |--- DISCONNECT (00 01), cause 16 ----------------->|
    |<-------------------------------- RELEASE (80 01) |
    |--- RELEASE COMPLETE (00 01) --------------------->|

ALERTING means the far end is ringing, and CONNECT means someone answered. Clearing takes three messages on purpose: DISCONNECT asks to end the call, RELEASE frees the channel and the call reference, and RELEASE COMPLETE confirms. The cause in the first clearing message is the one that explains why the call ended. Everything after it is bookkeeping.

When calls fail

Cause values are defined in a separate recommendation, ITU-T Q.850. The Cause IE 08 02 80 90 decodes as: Cause, 2 bytes long; 80 is ITU-T coding with location "user"; 90 is cause 16 with the top bit set. These are the ones that come up again and again:

CauseMeaningWhat it usually tells you
16Normal call clearingSomeone hung up. Not an error.
17User busyThe called party is busy.
18No user respondingNothing answered the SETUP in time.
19No answer from user (user alerted)It rang and nobody picked up.
21Call rejectedThe far end refused it on purpose.
28Invalid number formatThe number doesn't fit the expected type or plan.
34No circuit/channel availableNo free B-channel.
41Temporary failureSomething failed briefly; a retry might work.
96Mandatory information element is missingA protocol bug on the sending side.
100Invalid information element contentsSame, but a bad value instead of a missing one.
101Message not compatible with call stateThe two sides disagree about the call.
102Recovery on timer expiryA timer ran out waiting for the other side.

Causes from 96 to 111 are protocol errors. They don't mean a call was refused. They mean two implementations disagree about Q.931 itself.

Timers explain most of the rest. T303 (4 s) guards the SETUP: if nothing comes back, the SETUP goes out once more, and then the call is cleared. T310 (30 to 120 s) starts when CALL PROCEEDING arrives and clears the call with cause 102 if nothing follows. T305, T308 and T313 guard DISCONNECT, RELEASE and CONNECT in the same way.

A few patterns cover most failed calls:

  • SETUP, the same SETUP four seconds later, then nothing. T303 expired twice and the far end's layer 3 never answered. Check that Q.921 is actually up before anything else.
  • SETUP, CALL PROCEEDING, a long silence, then DISCONNECT with cause 102. T310 ran out. The far end accepted the call, but nothing beyond it ever alerted.
  • SETUP answered by RELEASE COMPLETE with cause 34. No free channel. Count the calls in progress before you call it a bug.
  • SETUP answered by RELEASE COMPLETE with cause 96 or 100. The far end rejected the SETUP itself. The diagnostic bytes after the cause value often name the IE it didn't like.
  • A STATUS with cause 101 in the middle of a call. A state mismatch. Walk back to the message one side missed.

One more thing that trips decoders up. Q.931's standard IEs live in codeset 0, and networks can define their own in codesets 5 to 7, switching to them with the Shift IE. So a run of unknown IEs right after a 9x byte usually means you're reading network-specific elements, not garbage.

Let Wireshark do it

Decoding by hand is good practice exactly once. After that, wrap the hex in a capture file with text2pcap, one frame per line, each starting with an offset:

trace.txt
0000  00 01 7f
0000  00 01 73
0000  00 01 01 02
text2pcap -l 147 trace.txt trace.pcap

There's no standard link type for raw LAPD, so this borrows 147, the first of the link types reserved for private use. In Wireshark, open Edit → Preferences → Protocols → DLT_USER, add an entry for User 0 (DLT=147) with lapd as the payload protocol, and the LAPD and Q.931 dissectors take it from there.

References

#isdn #q921 #q931 #telephony