Preserving caller ID of forwarded calls

I have an SIP trunk registered to a “PBX in a Flash” asterisk PBX, running Asterisk

I want to configure Asterisk so if a call comes in and I don’t answer my desk phone, it goes back out the trunk and to my cell.

I have configured this and it is working, however I am not getting the caller ID of the inbound caller, but rather the caller ID of the DID of my line registered to the PBX.

I need to preserve the original caller ID when the call hits my cell so I can see who’s calling – especially if I miss the forwarded call I am not able to see who called me unless I log into the web interface and run a CDR report which is a little tedious.

I’ve checked with my provider and they have indicated this is possible. They have said it will need to be done via SIP 302 redirects, as per RFC5806 (

Unfortunately my knowledge of how all this work isn’t quite up to scratch to be able to work out what they’re saying and implement it – can someone here assist?


To get a 302 redirect, DO NOT answer the, either explicitly, or implicitly (e.g. by using Playback), and then invoke the Transfer application. This must all be done on the original channel; no Local channels. The transfer application takes a SIP URI with “sip:” replaced by “SIP/”. It does not use sip.conf.

If you answer, the transfer is done in a different way, which the ITSP might not support. If there is a Local channel in the way, the Transfer request will simply fail.

Transfer is rarely used and is therefore less well tested. There is no proper call detail logging and the call will terminate completely within Asterisk when the transfer succeeds.

Although it is unusual for ITSPs to support 302 redirects, and they will charge them as a new call, the reason it can work is that the ITSP knows about the incoming call, and can therefore trust the associated caller ID. If you set the caller ID on an outgoing call, it is “unscreened” and reputable ITSPs will not forward it because it could be used fraudulently, although some offer the option for you to prove that you control the number in advance (not applicable here).

Hi David, thanks for the reply.

So what you are saying is that if I want to preserve the caller ID, I will have to bounce it out as soon as it comes (i.e. without answering it, without sending it through my IVR etc) and that there is no way to send the call back out with the original caller ID once my pbx ‘answers’ the call?

It will normally be sent out with the original caller ID, but responsible ITSPs will not use that because it is too easy to use it to mislead people. Judging by the number of phone spammers that generate bogus caller IDs, there are a lot of irresponsible ITSPs out there.

Responsible ITSPs will require that you either validate the caller ID in advance, probably by accepting a call from them on that number, to prove that you control the number, or will require you to be an official network operator, which may require a licence in some countries (e.g. the UK).

ISDN, and some modes of sending SIP caller IDs, allow you to flag a number as “unscreened”. That might allow an ITSP to forward the number so that it is available to ISDN and suitable VoIP callees, but it should still get suppressed before being sent to ordinary phone subscribers, who cannot distinguish between screened and unscreened numbers.

Transfer fan be used after an IVR, but will send a REFER request. Your ITSP is much less likely to support that. IVRs can be run before answer, but ITSPs are unlikely to allow that because of the possibility of making free calls.

Basically, this is an ITSP issue, not an Asterisk one.

Hi David

I understand that – and as always it’s easier to hack my end to work properly rather than trying to school my trunk provider on how to do things.

I’ve been back and forth with them and they’ve said the following:

[quote]First declare the following variable (you will see where other variables are being declared):

$didname = $AGI->request['agi_dnid'];
Then you need to locate the call forward section and enter in the below, the new line is the $AGI etc:

$count = 0; foreach( $ext as $k) { $cf = $AGI->database_get('CF',$k); $cf = $cf['data']; if (strlen($cf)) { // append a hash sign so we can send out on chan_local below. $ext[$count] = $cf.'#'; debug("Extension $k has call forward set to $cf", 1); $AGI->set_variable("__SIPADDHEADER","Diversion: <sip:$didname@{IPADDRESS}>;reason=unconditional;counter=1;privacy=off"); debug("$AGI->set_variable"); } else { debug("Extension $k cf is disabled", 3); } $count++; }

But this was for asterisk a few versions ago… can you make sense of it and see what this code is trying to accomplish?