Rewriting the DIVERSION SIP header

Hi FreePBX Pros!

My system:

NAME=“Sangoma Linux”
VERSION=“7 (Core)”
ID_LIKE=“centos rhel fedora”
PRETTY_NAME=“Sangoma Linux 7 (Core)”


The issue:
I need to rewrite the DIVERSION SIP header and I don’t know in which config file (Admin > Config edit > ??) and how exactly I have to perform that.

Currently external incoming calls being forwarded to an external participant (e.g. Mobile > PBX >> forwarding to Mobile) will not succeed because the DIVERSION SIP header on the INVITE to the forwarded number looks like this:

Diversion: sip:[email protected];reason=unconditional

My SIP provider told me that the following is expected:

Diversion: sip:[email protected];reason=unconditional


  • 123 is the extension number
  • +49 is Germany’s country code
  • 89 is Munich preselection
  • 9876 is the PBX’ trunk number

My plan is to create a rewrite rule so that the +49899876 is prefixed to every externally forwarded call.

Who knows which is the relevant config file and has an idea how the rewrite rule looks like?

Thank you in advance!

Do not close.

Do you have the context [sub-diversion-header] in /etc/asterisk/extensions_additional.conf ?
If so, and you confirm from the log of a failing forwarded call that this subroutine gets executed, you should be able to put a modified version of it in /etc/asterisk/extensions_override_freepbx.conf .

Yes, this is the context [sub-diversion-header]:

include => sub-diversion-header-custom
exten => s,1,Set(DIVERSION_REASON=${IF($[${LEN(${DIVERSION_REASON})}=0]?no-answer:${DIVERSION_REASON})})
exten => s,n,Gosub(func-set-sipheader,s,1(Diversion,tel:${FROM_DID};reason=${DIVERSION_REASON};screen=no;privacy=off))
exten => s,n,Return()

;–== end of [sub-diversion-header] ==–;`

I assume that this would be the edited version:

include => sub-diversion-header-custom
exten => s,1,Set(DIVERSION_REASON=${IF($[${LEN(${DIVERSION_REASON})}=0]?no-answer:${DIVERSION_REASON})})
exten => s,n,Gosub(func-set-sipheader,s,1(Diversion,tel:+49899876${FROM_DID};reason=${DIVERSION_REASON};screen=no;privacy=off))
exten => s,n,Return()

;–== end of [sub-diversion-header] ==–;`

Is that correct?

Do I have to add this entire context to ?

Can I add it directly to the config file by editor or do I have to use the Web GUI?

Do I have to “recompile” sth. after the file manipulation or just restart the freepbx daemon?

Assuming that failing calls are actually executing this context, your change looks correct. After adding the context to
fwconsole reload
should be sufficient; it requests Asterisk to reread the config files.

fwconsole restart
is a bigger club, restarting Asterisk and FreePBX; it will drop any calls currently in progress, and incoming calls won’t work until the phones reregister. Try just the reload first and test.

Tried the trunk number hardcoding mentioned above within extensions_override_freepbx.conf with no success. Neither “fwconsole reload” nor rebooting the whole system made the changes take effect.

The SIP trace still shows the extension number without the trunk in the diversion sip header:

sip:[email protected];reason=unconditional

instead of

sip:[email protected];reason=unconditional

Manipulated also the file extensions_additional.conf directly accordingly (what you shouldn’t do; just for checking) but also this change does not take effect.

Searching all the files in directory /etc/asterisk for the string occurrences “diversion” I got the following result:

extensions_additional.conf:exten => _X.,n,GosubIf($[${LEN(${FROM_DID})}>0 & "${FROM_DID}"!="s"]?sub-diversion-header,s,1())
extensions_additional.conf:exten => _+49X.,n,GosubIf($[${LEN(${FROM_DID})}>0 & "${FROM_DID}"!="s"]?sub-diversion-header,s,1())
extensions_additional.conf:include => sub-diversion-header-custom
extensions_additional.conf:exten => s,1,Set(DIVERSION_REASON=${IF($[${LEN(${DIVERSION_REASON})}=0]?no-answer:${DIVERSION_REASON})})
extensions_additional.conf:exten => s,n,Gosub(func-set-sipheader,s,1(Diversion,<tel:${FROM_DID}>\;reason=${DIVERSION_REASON}\;screen=no\;privacy=off))
extensions_additional.conf:;--== end of [sub-diversion-header] ==--;
extensions_additional.conf:exten => docfu,1,Set(__DIVERSION_REASON=unavailable)
extensions_additional.conf:exten => docfu,n,Set(__DIVERSION_REASON=)
extensions_additional.conf:exten => docfb,1,Set(__DIVERSION_REASON=user-busy)
extensions_additional.conf:exten => docfb,n,Set(__DIVERSION_REASON=)
extensions_additional.conf:exten => cf,n,Set(__DIVERSION_REASON=${IF($["${DEXTEN}"!=""]?"unconditional": )})
extensions_additional.conf.180901:exten => _X.,n,GosubIf($[${LEN(${FROM_DID})}>0 & "${FROM_DID}"!="s"]?sub-diversion-header,s,1())
extensions_additional.conf.180901:exten => _+49X.,n,GosubIf($[${LEN(${FROM_DID})}>0 & "${FROM_DID}"!="s"]?sub-diversion-header,s,1())
extensions_additional.conf.180901:include => sub-diversion-header-custom
extensions_additional.conf.180901:exten => s,1,Set(DIVERSION_REASON=${IF($[${LEN(${DIVERSION_REASON})}=0]?no-answer:${DIVERSION_REASON})})
extensions_additional.conf.180901:exten => s,n,Gosub(func-set-sipheader,s,1(Diversion,<tel:${FROM_DID}>\;reason=${DIVERSION_REASON}\;screen=no\;privacy=off))
extensions_additional.conf.180901:;--== end of [sub-diversion-header] ==--;
extensions_additional.conf.180901:exten => docfu,1,Set(__DIVERSION_REASON=unavailable)
extensions_additional.conf.180901:exten => docfu,n,Set(__DIVERSION_REASON=)
extensions_additional.conf.180901:exten => docfb,1,Set(__DIVERSION_REASON=user-busy)
extensions_additional.conf.180901:exten => docfb,n,Set(__DIVERSION_REASON=)
extensions_additional.conf.180901:exten => cf,n,Set(__DIVERSION_REASON=${IF($["${DEXTEN}"!=""]?"unconditional": )})
extensions_override_freepbx.conf:include => sub-diversion-header-custom
extensions_override_freepbx.conf:exten => s,1,Set(DIVERSION_REASON=${IF($[${LEN(${DIVERSION_REASON})}=0]?no-answer:${DIVERSION_REASON})})
extensions_override_freepbx.conf:exten => s,n,Gosub(func-set-sipheader,s,1(Diversion,tel:+49899876${FROM_DID};reason=${DIVERSION_REASON};screen=no;privacy=off))
extensions_override_freepbx.conf:;–== end of [sub-diversion-header] ==–;

Assuming that the file extensions_additional.conf.180901 is only a backup file, there’s only the files extensions_additional.conf and extensions_override_freepbx.conf where you can manipulate the diversion settings.

Did I miss sth.?

Looking on the [sub-diversion-header] section I just noticed the line

include => sub-diversion-header-custom

I grepped for that context on all files within /etc/asterisk but couldn’t find the file where you can confiugre this context.

Could it be, that you have to create a file in order to configure this context?

Opened another thread:

This one can be closed. Thank you!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.