Asterisk and Twilio's Elastic SIP Trunking (inbound troubleshooting)

I’ve been at this for a few days and can’t seem to route incoming calls through to user extensions. Outgoing calls and internal SIP extension dialing both work however, when placing a call to the number associated with a Twilio Elastic SIP trunk I have setup and configured for a domain, I get an “All circuits are busy” message from my carrier.

The system is a fresh install of FreePBX 12.0.68 running on Ubuntu 14.04 with internal SIP extention dialing and outbound calls on the trunk working.

type=peer
secret=xxxxxxxxxxxxxxxxxxx
username=xxxxxxxxxxxxxxx
host=xxxxxxxxx.pstn.twilio.com
dtmfmode=rfc2833
canreinvite=no
disallow=all
allow=ulaw
insecure=port,invite
fromuser=xxxxxxxxxxx
fromdomain=xxxxxxxxx.pstn.twilio.com
context=incoming

Here’s the TCP/UDP traffic between Twilio and the server

Source          Destination     Protocal  Info
10x.xxx.xx.xxx  10x.xxx.xxx.xx  UDP       Source port: 5060  Destination port: 5060
54.172.60.2     10x.xxx.xxx.xx  SIP/SDP   Request: INVITE sip:[email protected] 
54.172.60.2     10x.xxx.xxx.xx  SIP/SDP   Request: INVITE sip:[email protected] 
54.172.60.2     10x.xxx.xxx.xx  SIP/SDP   Request: INVITE sip:[email protected] 
54.172.60.2     10x.xxx.xxx.xx  SIP/SDP   Request: INVITE sip:[email protected] 
54.172.60.2     10x.xxx.xxx.xx  SIP/SDP   Request: INVITE sip:[email protected] 
54.172.60.2     10x.xxx.xxx.xx  SIP/SDP   Request: INVITE sip:[email protected] 
54.172.60.2     10x.xxx.xxx.xx  SIP/SDP   Request: INVITE sip:[email protected] 
54.172.60.2     10x.xxx.xxx.xx  SIP/SDP   Request: INVITE sip:[email protected] 
54.172.60.3     10x.xxx.xxx.xx  SIP/SDP   Request: INVITE sip:[email protected] 
54.172.60.3     10x.xxx.xxx.xx  SIP/SDP   Request: INVITE sip:[email protected] 
54.172.60.3     10x.xxx.xxx.xx  SIP/SDP   Request: INVITE sip:[email protected] 
54.172.60.3     10x.xxx.xxx.xx  SIP/SDP   Request: INVITE sip:[email protected] 
54.172.60.3     10x.xxx.xxx.xx  SIP/SDP   Request: INVITE sip:[email protected] 
54.172.60.3     10x.xxx.xxx.xx  SIP/SDP   Request: INVITE sip:[email protected] 
54.172.60.3     10x.xxx.xxx.xx  SIP/SDP   Request: INVITE sip:[email protected] 
54.172.60.3     10x.xxx.xxx.xx  SIP/SDP   Request: INVITE sip:[email protected] 
54.172.60.0     10x.xxx.xxx.xx  SIP/SDP   Request: INVITE sip:[email protected] 
54.172.60.0     10x.xxx.xxx.xx  SIP/SDP   Request: INVITE sip:[email protected] 
54.172.60.0     10x.xxx.xxx.xx  SIP/SDP   Request: INVITE sip:[email protected] 
10x.xxx.xx.xxx  10x.xxx.xxx.xx  UDP       Source port: 5060  Destination port: 5060

And here’s the INVITE UDP stream

INVITE sip:[email protected] SIP/2.0
Record-Route: <sip:54.172.60.0:5060;lr;ftag=11540065_6772d868_144031e0-db91-45e9-ae85-6de18ed14b19>
From: <sip:[email protected];pstn-params=808481808882;cpc=ordinary>;tag=11540065_6772d868_144031e0-db91-45e9-ae85-6de18ed14b19
To: <sip:[email protected];user=phone>
CSeq: 25149 INVITE
Max-Forwards: 132
Accept: application/sdp,application/isup,application/dtmf,application/dtmf-relay,multipart/mixed
Session-Expires: 1800
Min-SE: 90
Content-Disposition: session;handling=required
Diversion: sip:[email protected];reason=unconditional
Call-ID: [email protected]
Via: SIP/2.0/UDP 54.172.60.0:5060;branch=z9hG4bKdf6c.854803a7.0
Via: SIP/2.0/UDP 172.18.18.39:5060;branch=z9hG4bK144031e0-db91-45e9-ae85-6de18ed14b19_6772d868_287964010429808
Contact: <sip:[email protected]:5060;transport=udp>
Allow: INVITE,ACK,CANCEL,OPTIONS,BYE
User-Agent: Twilio Gateway
X-Twilio-AccountSid: ACaa6e5a9a0d40b2b12751f33b612ebf6e
X-Twilio-ApiVersion: 2010-04-01
Content-Type: application/sdp
X-Twilio-CallSid: CAcc7d0e0603fea476fdaa1c94d9243104
Content-Length: 233

v=0
o=- 412164138 412164138 IN IP4 54.172.60.23
s=SIP Media Capabilities
c=IN IP4 54.172.60.23
t=0 0
m=audio 11590 RTP/AVP 0 101
a=rtpmap:0 PCMU/8000
a=rtpmap:101 telephone-event/8000
a=fmtp:101 0-15
a=sendrecv
a=ptime:20
INVITE sip:[email protected] SIP/2.0
Record-Route: <sip:54.172.60.0:5060;lr;ftag=11540065_6772d868_144031e0-db91-45e9-ae85-6de18ed14b19>
From: <sip:[email protected];pstn-params=808481808882;cpc=ordinary>;tag=11540065_6772d868_144031e0-db91-45e9-ae85-6de18ed14b19
To: <sip:[email protected];user=phone>
CSeq: 25149 INVITE
Max-Forwards: 132
Accept: application/sdp,application/isup,application/dtmf,application/dtmf-relay,multipart/mixed
Session-Expires: 1800
Min-SE: 90
Content-Disposition: session;handling=required
Diversion: sip:[email protected];reason=unconditional
Call-ID: [email protected]
Via: SIP/2.0/UDP 54.172.60.0:5060;branch=z9hG4bKdf6c.854803a7.0
Via: SIP/2.0/UDP 172.18.18.39:5060;branch=z9hG4bK144031e0-db91-45e9-ae85-6de18ed14b19_6772d868_287964010429808
Contact: <sip:[email protected]:5060;transport=udp>
Allow: INVITE,ACK,CANCEL,OPTIONS,BYE
User-Agent: Twilio Gateway
X-Twilio-AccountSid: ACaa6e5a9a0d40b2b12751f33b612ebf6e
X-Twilio-ApiVersion: 2010-04-01
Content-Type: application/sdp
X-Twilio-CallSid: CAcc7d0e0603fea476fdaa1c94d9243104
Content-Length: 233

v=0
o=- 412164138 412164138 IN IP4 54.172.60.23
s=SIP Media Capabilities
c=IN IP4 54.172.60.23
t=0 0
m=audio 11590 RTP/AVP 0 101
a=rtpmap:0 PCMU/8000
a=rtpmap:101 telephone-event/8000
a=fmtp:101 0-15
a=sendrecv
a=ptime:20
INVITE sip:[email protected] SIP/2.0
Record-Route: <sip:54.172.60.0:5060;lr;ftag=11540065_6772d868_144031e0-db91-45e9-ae85-6de18ed14b19>
From: <sip:[email protected];pstn-params=808481808882;cpc=ordinary>;tag=11540065_6772d868_144031e0-db91-45e9-ae85-6de18ed14b19
To: <sip:[email protected];user=phone>
CSeq: 25149 INVITE
Max-Forwards: 132
Accept: application/sdp,application/isup,application/dtmf,application/dtmf-relay,multipart/mixed
Session-Expires: 1800
Min-SE: 90
Content-Disposition: session;handling=required
Diversion: sip:[email protected];reason=unconditional
Call-ID: [email protected]
Via: SIP/2.0/UDP 54.172.60.0:5060;branch=z9hG4bKdf6c.854803a7.0
Via: SIP/2.0/UDP 172.18.18.39:5060;branch=z9hG4bK144031e0-db91-45e9-ae85-6de18ed14b19_6772d868_287964010429808
Contact: <sip:[email protected]:5060;transport=udp>
Allow: INVITE,ACK,CANCEL,OPTIONS,BYE
User-Agent: Twilio Gateway
X-Twilio-AccountSid: ACaa6e5a9a0d40b2b12751f33b612ebf6e
X-Twilio-ApiVersion: 2010-04-01
Content-Type: application/sdp
X-Twilio-CallSid: CAcc7d0e0603fea476fdaa1c94d9243104
Content-Length: 233

v=0
o=- 412164138 412164138 IN IP4 54.172.60.23
s=SIP Media Capabilities
c=IN IP4 54.172.60.23
t=0 0
m=audio 11590 RTP/AVP 0 101
a=rtpmap:0 PCMU/8000
a=rtpmap:101 telephone-event/8000
a=fmtp:101 0-15
a=sendrecv
a=ptime:20

I also have an incoming route configured within the FreePBX interface with the DID Number set to my Twilio number and Destination set directly to a user’s SIP extension with a corresponding client running and ready to receive calls. I’ve used both netstat and tcpdump which to me it looks like an INVITE request is sent from Twilio and FreePBX just isn’t routing it properly?

Thanks for your help!

Is NAT set to “YES” on the “Asterisk SIP Settings” page?

Thanks for the reply, I did have it set to No/Dynamic IP with corresponding iptables to support it. Upon changing it to static, I did get a few more packets in my buffer

Source          Destination     Protocal  Info

54.172.60.0     10x.xxx.xxx.xx  SIP/SDP   Request: INVITE sip:[email protected]
10x.xxx.xxx.xx  10x.xxx.xx.xxx  SIP 535   Request: OPTIONS sip:[email protected]:5060
10x.xxx.xx.xxx  10x.xxx.xxx.xx  SIP 371   Status: 200 Ok
10x.xxx.xx.xxx  10x.xxx.xxx.xx  UDP 60    Source port: 5060  Destination port: 5060

So it looks like the server is sending an OPTIONS request to the client I have set up after receiving an INVITE request from Twilio, in which the client responds with status code 200 OK and attempts to open a UDP socket. No failures afterwards, just the “All circuits are busy” message from my carrier.

May be worth noting, the trunk is set up using chan_sip and my extention/device uses pjsip, this shouldn’t be a problem though, or am I wrong in that assumption?

By default there is no context named [incoming] in FreePBX

hah, I managed to fix this temporarily by removing a few entries in my prerouting chain that supported dynamic IP routing after switching to NAT

-A PREROUTING -i eth+ -p tcp --dport 5060:5082 -m string --string "sip:xxx.xxxxx.com" --algo bm --icase -j NEWSIP
-A PREROUTING -i eth+ -p udp --dport 5060:5082 -m string --string "sip:xxx.xxxxx.com" --algo bm --to 1500 --icase -j NEWSIP

Thanks @Overkill , had to switch to NAT