How to set up a SIP trunk using FreePBX

Setting up a SIP trunk can be a confusing and aggravating task, but FreePBX makes things much easier.
In this guide, we'll go through the steps to set up a SIP trunk using FreePBX.

Contents

  1. Prerequisites
  2. Set up the trunk
  3. Set up the inbound route
  4. Set up the outbound route
  5. Finishing up

1. Prerequisites

Ok, for this guide we'll need to cover some basic requirements:
  1. A functioning Asterisk server with FreePBX. We'll be using trixbox 2.8
  2. SIP trunk info from a SIP provider. We'll be using Broadvoice.
  3. An extension assigned to an IP Phone. We'll be using extension 2000.
  4. Water. To keep you hydrated and thinking clearly.

2. Set up the SIP trunk

Now, with the above covered, let's get started!

Open up a web browser and go to your Asterisk server web interface. In our case we'll go to http://192.168.4.131/maint/index.php?freepbx. Now, on the left, under Basic click Trunks.
FreePBX menu
You should see a selection of trunk types, like this:
Trunk selections
Click Add SIP Trunk. We are now greeted with a page that we must fill in with our trunk info.

In the Outbound Caller ID field, you can enter a caller ID, but it may not do anything. So, we'll skip this field. We'll also leave the Never Override CallerID unchecked.
For the Maximum Channels field, we'll put in 1. This is because the plan we are using in this guide only allows 1 incoming call at a time.
Leave the Disable Trunk and Monitor Trunk Failures at their defaults and go down to Dial Rules under Outgoing Dial Rules
This is where the phone number gets "conditioned" before it gets sent to the SIP servers. For this guide, we'll use dialing rules to condition numbers for US 10-digit dialing.
Example are these dial rules:
1+NXXNXXXXXX
1912+NXXXXXX
Let's examine what these mean:
We'll start with the first one. (1+NXXNXXXXXX)

1+ means prepends "1" to the number.
N means match any number between 2 and 9.
X means match any number between 0 and 9.
This would match a number like, say 912-555-1234 and turn it into 1-912-555-1234 before sending it to the SIP servers.

So, the next one (1912+NXXXXXX) goes like this:
1912+ prepends "1912" to the number.
N matches any number between 2 and 9.
X matches any number between 0 and 9.
So then this one would match a number like, 555-1234 and turn it into 1-912-555-1234 before sending it to the SIP servers.

See, so far it isn't so bad. But, then again, we're not finished yet either. :-)

The Outbound Dial Prefix field prefixes a number to all numbers dialed through this trunk. For what we're doing, we will leave it blank. Although, like the help says, if this is a trunk to another Asterisk server or a Centrex line, you many need to put "9" in this box to access an outside line.

Under Outgoing Settings, we see the field Trunk Name. This is for, well, the trunk name. We'll put "Broadvoice" in this box.
Now, here comes one of the trickiest parts of setting up a SIP trunk, the peer details(settings). These are to tell Asterisk how to connect to the SIP provider.

Here is an example set of settings with descriptions of each one:
disallow=all
This should always come before any allow directives
allow=ulaw
This allows use of the G.711 u-law codec. Most SIP providers support this codec.
context=from-trunk
This is the context that Asterisk will dump calls coming from the trunk into this dialplan context. Without this set to a proper context, incoming calls will not work.
dtmfmode=auto
This tells Asterisk how to interpert DTMF tones. It can be auto(auto-negotiates what mode to use - recommended), inband(sends DTMF as sounds in the audio stream), rfc2833(recommended if your SIP provider supports it), or info(DTMF info is sent inside the SIP header)
fromdomain=sip.broadvoice.com
This tells the SIP provider what domain the call comes from. Some SIP providers require this for authentication.
fromuser=<Username>
This is the username to authenticate to the SIP provider with.
host=sip.broadvoice.com
This is the host to connect with to send calls
insecure=very
This determines if Asterisk should authenticate calls coming in. Your SIP provider should tell what to set this to. Common settings are "invite", "invite,port", or "very".
qualify=yes
This tells Asterisk whether or not to send SIP NOTIFY messages to the peer to check if it's still avalible the latency between it and Asterisk.
secret=<Password>
This is the password to authenticate to the SIP provider
type=peer
This sets the type to peer. This is required.
username=<Username>
This is the username to connect to the SIP provider with.
authname<Username>
This is the authentication username to connect to the SIP provider with. This isn't normally required, but some providers like Broadvoice require it to register.
canreinvite=no
This tells Asterisk if it should try to set up a call between the SIP provider and the destination phone directly. This is typicly set to no. If you're behind a NAT, this should be set to "no".
We do not need anything under Incoming Settings, so just make sure they're blank.

One of the most important settings in a SIP trunk, is the register string. You will find the field under Registration.
Some SIP providers use a slightly different register string format than others. The formats go as below:

Some, like Broadvoice, use this format:
<Username>@<SIP proxy>:<Password>:<AuthID>@<SIP proxy>/<DID>

Which translates into:
<Phone number>@<SIP proxy>:<Password>:<Phone number>@<SIP proxy>/<DID>

While others use this format:
<Username>:<Password>@<SIP proxy>/<DID>

Which translates into:
<Phone number>:<Password>@<SIP proxy>/<DID>

The /<DID> is important because it tells Asterisk how to route incoming calls from this trunk. It is a good idea to set it to your phone number/username.
So, for this guide, we'll use a register string like this:
9125555555@sip.broadvoice.com:ATenCharPw:9125555555@sip.broadvoice.com/9125555555

Finally we can click the Submit Changes button. Woo!
Now we can move on to setting up the inbound route.

3. Set up the inbound route

Now that we have the SIP trunk set up, it's time to set up the inbound route so that we can receive calls.
On the left menu, under Inbound Call Control click Inbound Routes.
FreePBX menu
We will be presented with the Add Incoming Route page.

Under the Add Incoming Route sub-heading, in the Description field, put a meaningful name for the route. We'll use "FromBroadvoice".
Remember that DID number we put on the trunk register string? Put that number in the DID Number field.
You can make this route match a certain Call ID number by putting that number in the Caller ID Number field. But for this guide, we'll leave this blank to route calls with any or no CID info.
Now we move down to Set Destination. This is where we choose where to route calls that come through this route. We'll choose Extensions: <2000> Test Phone as the destination.
You can set up the other options, like Alert Info, Fax handling, etc. but that isn't covered in this guide.

Click the Submit button so we can go on to setting up the outbound route.

4. Set up the outbound route

The last thing we need to set up for the SIP trunk is the outbound route. This will allow us to make outbound calls through the trunk.
On the left menu, under Basic click Outbound Routes.
FreePBX menu
We are now presented with the Add Route page.

In the Route Name field, put a meaningful name. We'll put "ToBroadvoice" in this box.
The Route Password field allows us to password protect this route. It would be useful if we wanted to restrict international or Toll-Free numbers with a password.
The next field we'll use is the Dial Patterns box. It works similar to the SIP trunk's dial rules.
Example are these dial patterns:
9|NXXNXXXXXX
9|NXXXXXX
Let's examine what these mean:
We'll start with the first one. (9|NXXNXXXXXX)

9| means match 9 at the beginning of the number, but don't pass it to the trunk.
N and X work the same as in the trunk dial rules.
This would mean that if you dialed a number like 9912-555-1234, it would route the call through this route but only pass 912-555-1234 to the trunk.

So, the next one (9|NXXXXXX) does like the above one, except it matches a 7-digit number instead of a 10-digit one.
So it would match a number like, 9555-1234 but only pass 555-1234 to the trunk.

The last thing we need to set for the route is the Trunk Sequence. This tells Asterisk in what order to try using trunks to send calls through. If one trunk fails (busy, down, or something else), it will try the next one in the sequence.
We'll select SIP/Broadvoice as the first trunk and since we don't have any other trunks, we'll leave the other drop-down blank.

Now click Submit Changes to create the route and meet me in the next section.

5. Finishing up

Now that we've set up the trunk and the routes, it's time take a small break and drink your water, it will replenish any water lost while setting up the trunk (like sweat or tears).
After your done with your break, we need to apply the changes and give the new trunk a test run!
At the top of the page, you should see an Apply Configurations Changes button. It looks like this: Apply button
Click the button and you should see an orange dialog box(like the one below) asking you if you wish apply the changes we've made. Click Continue with reload.
dialog box
Now, if you're using trixbox like in this guide, go up to the top menu and under PBX, click PBX Status. If you aren't using trixbox, go to the Asterisk console and type sip show registry and press enter.
you should see something like this:
SIP registry
If you don't see any entries, you may need to run sip reload and dialplan reload then sip show registry again.
If it shows Registered, we can test the trunk! Pick up one of your SIP phones and dial 9+ and a telephone number (eg. 95551234). If you are using a Polycom phone, you can dial the number then pick up the handset to override the phone's digitmap.
I recommend calling your cell-phone or house phone for testing. Unless your SIP provider has any other special parameters for the SIP peer, the call should go through.
If the outbound calling works, now try inbound calling. When you call the number assigned to the trunk, the extension we set for the inbound route should start ringing.