Q: SIP Client Responding 481 on CANCEL I am trying to write a SIP client for mobile devices. I use PJSIP for android, and Linphone for iOS. Sometimes the clients respond a 481 for a legitimate CANCEL.
I read RFC 3261 Section 9.1 and tried to see why the client is not matching the dialog. but as far as I see it looks good, the Request URI matches, as does the Call-ID header, the To header including the tag, and the numeric part of the Cseq
INVITE sip:#######@127.0.0.0 SIP/2.0
Via: SIP/2.0/UDP 127.0.0.1:5060;rport;branch=z9hG4bK-197747523180-702327557205
From: "WIRELESS CALLER" <sip:##########@my.sipserver.com>;tag=a410144978468667
To: <sip:#######@127.0.0.0>
Max-Forwards: 68
Call-ID: 0ce8e8201a70540029d0ec3402005633@127.0.0.2:5060-b4-3551-277
CSeq: 204936 INVITE
Contact: <sip:##########@127.0.0.1>
User-Agent: Server UAS
Allow: ACK,BYE,CANCEL,INVITE,REGISTER,UPDATE,MESSAGE,INFO,OPTIONS,SUBSCRIBE,NOTIFY,REFER,COMET,PUBLISH,PING,DO
Allow-Events: presence,refer,telephone-event,keep-alive,dialog,message-summary
Supported: privacy,replaces
Accept: application/sdp,application/dtmf-relay,audio/telephone-event,message/sipfrag,text/plain,text/html
Content-Disposition: session;handling=required
Content-Type: application/sdp
Content-Length: 521

v=0
o=root 1954582096 1954582096 IN IP4 127.0.0.2
s=Sonus_UAC
c=IN IP4 127.0.0.1
t=0 0
m=audio 10852 RTP/AVP 0 9 8 18 10 3 111 5 7 110 97 101
a=rtpmap:0 PCMU/8000
a=rtpmap:9 G722/8000
a=rtpmap:8 PCMA/8000
a=rtpmap:18 G729/8000
a=fmtp:18 annexb=no
a=rtpmap:10 L16/8000
a=rtpmap:3 GSM/8000
a=rtpmap:111 G726-32/8000
a=rtpmap:5 DVI4/8000
a=rtpmap:7 LPC/8000
a=rtpmap:110 speex/8000
a=rtpmap:97 iLBC/8000
a=fmtp:97 mode=30
a=rtpmap:101 telephone-event/8000
a=fmtp:101 0-16
a=ptime:20
a=sendrecv

SIP/2.0 100 Trying
Via: SIP/2.0/UDP 127.0.0.1:5060;rport;branch=z9hG4bK-197747523180-702327557205
From: "WIRELESS CALLER" <sip:##########@my.sipserver.com>;tag=a410144978468667
To: sip:#######@127.0.0.0
Call-ID: 0ce8e8201a70540029d0ec3402005633@127.0.0.2:5060-b4-3551-277
CSeq: 204936 INVITE

SIP/2.0 180 Ringing
Via: SIP/2.0/UDP 127.0.0.1:5060;rport;branch=z9hG4bK-197747523180-702327557205
From: "WIRELESS CALLER" <sip:##########@my.sipserver.com>;tag=a410144978468667
To: <sip:#######@127.0.0.0>;tag=43ye8Yn
Call-ID: 0ce8e8201a70540029d0ec3402005633@127.0.0.2:5060-b4-3551-277
CSeq: 204936 INVITE
User-Agent: belle-sip/1.6.3
Supported: replaces, outbound

CANCEL sip:#######@127.0.0.0 SIP/2.0
Via: SIP/2.0/UDP 127.0.0.1:5060;rport;branch=z9hG4bK-171352412518-702327557205
From: "WIRELESS CALLER" <sip:##########@my.sipserver.com>;tag=a410144978468667
To: <sip:#######@127.0.0.0>
Max-Forwards: 68
Call-ID: 0ce8e8201a70540029d0ec3402005633@127.0.0.2:5060-b4-3551-277
CSeq: 204936 CANCEL
User-Agent: Server UAS
Warning: 399 127.0.0.0 "Cancel received (0x15) (4007)"
M-Stat: s:0 r:0 l:0 es:0 er:0 el:0
Content-Length: 0

BYE sip:#######@127.0.0.0 SIP/2.0
Via: SIP/2.0/UDP 127.0.0.1:5060;rport;branch=z9hG4bK-151544386511-702327557205
From: "WIRELESS CALLER" <sip:##########@my.sipserver.com>;tag=a410144978468667
To: <sip:#######@127.0.0.0>;tag=43ye8Yn
Max-Forwards: 68
Call-ID: 0ce8e8201a70540029d0ec3402005633@127.0.0.2:5060-b4-3551-277
CSeq: 204937 BYE
User-Agent: Server UAS
Warning: 399 127.0.0.0 "Cancel received (0x15) (4007)"
M-Stat: s:0 r:0 l:0 es:0 er:0 el:0
Content-Length: 0

SIP/2.0 481 Call/transaction does not exist
Via: SIP/2.0/UDP 127.0.0.1:5060;rport;branch=z9hG4bK-171352412518-702327557205
From: "WIRELESS CALLER" <sip:##########@my.sipserver.com>;tag=a410144978468667
To: <sip:#######@127.0.0.0>;tag=K4PcWbb
Call-ID: 0ce8e8201a70540029d0ec3402005633@127.0.0.2:5060-b4-3551-277
CSeq: 204936 CANCEL

SIP/2.0 501 Not implemented
Via: SIP/2.0/UDP 127.0.0.1:5060;rport;branch=z9hG4bK-151544386511-702327557205
From: "WIRELESS CALLER" <sip:##########@my.sipserver.com>;tag=a410144978468667
To: <sip:#######@127.0.0.0>;tag=43ye8Yn
Call-ID: 0ce8e8201a70540029d0ec3402005633@127.0.0.2:5060-b4-3551-277
CSeq: 204937 BYE

CANCEL sip:#######@127.0.0.0 SIP/2.0
Via: SIP/2.0/UDP 127.0.0.1:5060;rport;branch=z9hG4bK-171352412518-702327557205
From: "WIRELESS CALLER" <sip:##########@my.sipserver.com>;tag=a410144978468667
To: <sip:#######@127.0.0.0>
Max-Forwards: 68
Call-ID: 0ce8e8201a70540029d0ec3402005633@127.0.0.2:5060-b4-3551-277
CSeq: 204937 CANCEL
User-Agent: Server UAS
Warning: 399 127.0.0.0 "Cancel received (0x15) (4007)"
M-Stat: s:0 r:0 l:0 es:0 er:0 el:0
Content-Length: 0

SIP/2.0 481 Call/transaction does not exist
Via: SIP/2.0/UDP 127.0.0.1:5060;rport;branch=z9hG4bK-171352412518-702327557205
From: "WIRELESS CALLER" <sip:##########@my.sipserver.com>;tag=a410144978468667
To: <sip:#######@127.0.0.0>;tag=azviy
Call-ID: 0ce8e8201a70540029d0ec3402005633@127.0.0.2:5060-b4-3551-277
CSeq: 204937 CANCEL

I have for privacy reasons removed identifying details.

A: There is one extra requirement to the CANCEL request (RFC3261 chapter 9.1):

A CANCEL constructed by a client MUST have only a single Via header
  field value matching the top Via value in the request being
  cancelled.  Using the same values for these header fields allows
  the CANCEL to be matched with the request it cancels

When the UAS receives the CANCEL request it has to find the corresponding INVITE server transaction. RFC3261 chapter 9.2:

The TU determines the transaction to be cancelled by taking the
  CANCEL request, and then assuming that the request method is
  anything but CANCEL or ACK and applying the transaction matching
  procedures of Section 17.2.3. The matching transaction is the one
  to be cancelled.

One of the matching criteria is the branch parameter in the top Via header of the CANCEL request: it must be identical to the one in the INVITE request that created the server transaction. Since the branch parameters in your INVITE and CANCEL requests differ, the UAS can't find the server transaction and cancel the INVITE.
