res_pjsip_nat: Add endpoint option "restore_orig_contact_host"

With rewrite_contact enabled, the Contact received from a device behind
NAT is changed to the source address and port.  Requests built from that
contact (INVITE to a registered contact, ACK/BYE/re-INVITE to the dialog
remote target, NOTIFY, OPTIONS) then carry the NAT address in the
Request-URI and To header.  Some devices refuse them because the request
is not addressed to them (RFC 3261 8.2.2.1): a Cisco CP-8941 answers
404 Not Found to the INVITE and ignores the ACK of an answered call.

When the new endpoint option is enabled together with rewrite_contact,
the host and port announced in the Contact are saved in an internal
"x-ast-restore-host" URI parameter before the rewrite, both for REGISTER
and for dialog messages.  On transmit, the Request-URI and To header get
that host and port back and the internal parameters are removed.  The
destination was already resolved from the rewritten URI, so requests
are still delivered to the source address and port.  This follows
RFC 3261 10.3, 16.5 and 12.2.1.1, as chan_sip did with
nat=force_rport.  WebSocket transports are not affected.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Resolves: #2312

UserNote: The new "restore_orig_contact_host" endpoint option, used with
rewrite_contact, keeps the host and port the device announced in its
Contact in the Request-URI and To header of requests sent to it, while
still delivering them to the source address.  This helps devices behind
NAT that refuse requests addressed to the NAT address.

UpgradeNote: The alembic script adds the "restore_orig_contact_host"
column to the ps_endpoints table of the realtime database schema.
pull/2313/head
Hugo Castro de Deco 2 weeks ago
parent 35be64cbed
commit 5b5de8b52b

@ -1038,6 +1038,20 @@
; NOTE: This doesn't just apply to 183 responses. MOH will
; be suppressed when the attribute appears in any SDP received
; including INVITEs, re-INVITES, and other responses.
;
; restore_orig_contact_host = no
; Only used when rewrite_contact is enabled. Requests sent to a
; rewritten contact (calls, in-dialog requests such as ACK and
; BYE, NOTIFY, OPTIONS) are still delivered to the source address
; and port of the device, but the Request-URI and To header carry
; the host and port the device announced in its Contact, as
; RFC 3261 sections 12.2.1.1 and 16.5 describe. Some devices behind
; NAT (for example Cisco CP-89xx phones) answer 404 Not Found or
; ignore requests whose Request-URI carries the NAT address.
; Contacts registered before the option changes keep the previous
; behavior until they register again. WebSocket transports are
; not affected.
; (default: no)
; (default: no)
;==========================AUTH SECTION OPTIONS=========================

@ -0,0 +1,30 @@
"""Add restore_orig_contact_host to ps_endpoints
Revision ID: 7c3f9a1e2b6d
Revises: 2b45fd748a4f
Create Date: 2026-11-18 20:00:00.000000
"""
# revision identifiers, used by Alembic.
revision = '7c3f9a1e2b6d'
down_revision = '2b45fd748a4f'
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects.postgresql import ENUM
YESNO_NAME = 'yesno_values'
YESNO_VALUES = ['yes', 'no']
def upgrade():
yesno_values = ENUM(*YESNO_VALUES, name=YESNO_NAME, create_type=False)
op.add_column('ps_endpoints', sa.Column('restore_orig_contact_host', yesno_values))
def downgrade():
if op.get_context().bind.dialect.name == 'mssql':
op.drop_constraint('ck_ps_endpoints_restore_orig_contact_host_yesno_values', 'ps_endpoints')
op.drop_column('ps_endpoints', 'restore_orig_contact_host')

@ -1182,6 +1182,8 @@ struct ast_sip_endpoint {
AST_STRING_FIELD_EXTENDED(tenantid);
/*! Ignore remote hold requests */
int suppress_moh_on_sendonly;
/*! Keep the device's own Contact host in requests sent to a rewritten contact */
unsigned int restore_orig_contact_host;
};
/*! URI parameter for symmetric transport */

@ -2087,6 +2087,32 @@
</note>
</description>
</configOption>
<configOption name="restore_orig_contact_host" default="no">
<synopsis>Keep the device's own Contact host in requests sent to a rewritten contact</synopsis>
<description><para>
When <replaceable>rewrite_contact</replaceable> is enabled, the Contact
received from this endpoint is changed to the source address and port so
requests can reach a device behind NAT. Requests built from that contact
(calls, in-dialog requests such as ACK and BYE, NOTIFY, OPTIONS) then carry
the NAT address in the Request-URI and To header, and some devices
(for example Cisco CP-89xx phones) answer 404 Not Found or ignore them
because the request is not addressed to them.
</para>
<para>
If you set this option to "yes", the requests are still delivered to the
source address and port, but the Request-URI and To header carry the host
and port the device announced in its Contact, as RFC 3261 sections 12.2.1.1
and 16.5 describe. This covers contacts saved by REGISTER and the remote
target of dialogs.
</para>
<note><para>
This option has no effect unless <replaceable>rewrite_contact</replaceable>
is enabled. Contacts registered before the option changes keep the previous
behavior until they register again. WebSocket transports are not affected.
</para>
</note>
</description>
</configOption>
</configObject>
<configObject name="auth">
<since>

@ -2433,6 +2433,8 @@ int ast_res_pjsip_initialize_configuration(void)
ast_sorcery_object_field_register(sip_sorcery, "endpoint", "tenantid", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, tenantid));
ast_sorcery_object_field_register(sip_sorcery, "endpoint", "suppress_moh_on_sendonly",
"no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, suppress_moh_on_sendonly));
ast_sorcery_object_field_register(sip_sorcery, "endpoint", "restore_orig_contact_host",
"no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, restore_orig_contact_host));
if (ast_sip_initialize_sorcery_transport()) {
ast_log(LOG_ERROR, "Failed to register SIP transport support with sorcery\n");

@ -38,6 +38,10 @@
#define AST_SIP_X_AST_ORIG_HOST "x-ast-orig-host"
#define AST_SIP_X_AST_ORIG_HOST_LEN 15
/*! URI parameter for the host/port restored in outgoing requests (restore_orig_contact_host) */
#define AST_SIP_X_AST_RESTORE_HOST "x-ast-restore-host"
#define AST_SIP_X_AST_RESTORE_HOST_LEN 18
#define is_sip_uri(uri) \
(PJSIP_URI_SCHEME_IS_SIP(uri) || PJSIP_URI_SCHEME_IS_SIPS(uri))
@ -67,6 +71,48 @@ static void save_orig_contact_host(pjsip_rx_data *rdata, pjsip_sip_uri *uri)
return;
}
/*!
* \internal
* \brief Save the Contact host and port announced by an endpoint with restore_orig_contact_host.
*
* Unlike x-ast-orig-host, which is only saved for REGISTER, this is saved for any message
* whose Contact is rewritten (REGISTER and the remote target of dialogs), so requests sent
* to the rewritten contact can carry the device's own host and port in the Request-URI.
*/
static void save_restore_contact_host(pjsip_rx_data *rdata, pjsip_sip_uri *uri)
{
pj_str_t x_name = { AST_SIP_X_AST_RESTORE_HOST, AST_SIP_X_AST_RESTORE_HOST_LEN };
pjsip_param *x_restore_host;
pj_str_t p_value;
if (!strncasecmp("WS", rdata->tp_info.transport->type_name, 2)) {
/* WebSocket contacts are handled by the transport */
return;
}
if (pj_strcmp2(&uri->host, rdata->pkt_info.src_name) == 0
&& uri->port == rdata->pkt_info.src_port) {
/* Nothing to restore (no NAT, or already rewritten) */
return;
}
/* Never trust a value received from the network */
while ((x_restore_host = pjsip_param_find(&uri->other_param, &x_name))) {
pj_list_erase(x_restore_host);
}
ast_debug(1, "Saving contact '%.*s:%d' to restore in requests\n",
(int)uri->host.slen, uri->host.ptr, uri->port);
x_restore_host = PJ_POOL_ALLOC_T(rdata->tp_info.pool, pjsip_param);
x_restore_host->name = pj_strdup3(rdata->tp_info.pool, AST_SIP_X_AST_RESTORE_HOST);
p_value.slen = pj_strlen(&uri->host) + COLON_LEN + MAX_PORT_LEN;
p_value.ptr = (char*)pj_pool_alloc(rdata->tp_info.pool, p_value.slen + 1);
p_value.slen = snprintf(p_value.ptr, p_value.slen + 1, "%.*s:%d", (int)uri->host.slen, uri->host.ptr, uri->port);
pj_strassign(&x_restore_host->value, &p_value);
pj_list_insert_before(&uri->other_param, x_restore_host);
}
static void rewrite_uri(pjsip_rx_data *rdata, pjsip_sip_uri *uri, pj_pool_t *pool)
{
@ -176,7 +222,7 @@ static int rewrite_route_set(pjsip_rx_data *rdata, pjsip_dialog *dlg)
return res;
}
static int rewrite_contact(pjsip_rx_data *rdata, pjsip_dialog *dlg)
static int rewrite_contact(struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata, pjsip_dialog *dlg)
{
pjsip_contact_hdr *contact;
@ -184,6 +230,9 @@ static int rewrite_contact(pjsip_rx_data *rdata, pjsip_dialog *dlg)
if (contact && !contact->star && (PJSIP_URI_SCHEME_IS_SIP(contact->uri) || PJSIP_URI_SCHEME_IS_SIPS(contact->uri))) {
pjsip_sip_uri *uri = pjsip_uri_get_uri(contact->uri);
if (endpoint->restore_orig_contact_host) {
save_restore_contact_host(rdata, uri);
}
rewrite_uri(rdata, uri, rdata->tp_info.pool);
if (dlg && pj_list_empty(&dlg->route_set) && (!dlg->remote.contact
@ -214,7 +263,7 @@ static pj_bool_t handle_rx_message(struct ast_sip_endpoint *endpoint, pjsip_rx_d
* none are present, we fall back to rewriting the Contact header instead.
*/
if (rewrite_route_set(rdata, dlg)) {
rewrite_contact(rdata, dlg);
rewrite_contact(endpoint, rdata, dlg);
}
}
@ -257,28 +306,61 @@ static int nat_invoke_hook(void *obj, void *arg, int flags)
return 0;
}
/*!
* \internal
* \brief Restore the host and port saved by save_restore_contact_host and remove the
* internal parameters from a URI of an outgoing request.
*
* The destination of the request was already resolved from the rewritten URI, so
* only the encoded header changes.
*/
static void restore_request_uri_host(pj_pool_t *pool, pjsip_sip_uri *uri)
{
pj_str_t x_orig_name = { AST_SIP_X_AST_ORIG_HOST, AST_SIP_X_AST_ORIG_HOST_LEN };
pj_str_t x_restore_name = { AST_SIP_X_AST_RESTORE_HOST, AST_SIP_X_AST_RESTORE_HOST_LEN };
pjsip_param *param;
param = pjsip_param_find(&uri->other_param, &x_restore_name);
if (param && param->value.slen) {
char host_port[param->value.slen + 1];
char *sep;
ast_copy_pj_str(host_port, &param->value, sizeof(host_port));
/* The saved value always ends with ":<port>", the host may be IPv6 */
sep = strrchr(host_port, ':');
if (sep && sep != host_port) {
*sep++ = '\0';
ast_debug(1, "Restoring request URI host %.*s:%d to %s:%s\n",
(int)uri->host.slen, uri->host.ptr, uri->port, host_port, sep);
pj_strdup2(pool, &uri->host, host_port);
uri->port = strtol(sep, NULL, 10);
}
}
while ((param = pjsip_param_find(&uri->other_param, &x_restore_name))) {
pj_list_erase(param);
}
while ((param = pjsip_param_find(&uri->other_param, &x_orig_name))) {
pj_list_erase(param);
}
}
static void restore_orig_contact_host(pjsip_tx_data *tdata)
{
pjsip_contact_hdr *contact;
pj_str_t x_name = { AST_SIP_X_AST_ORIG_HOST, AST_SIP_X_AST_ORIG_HOST_LEN };
pj_str_t x_restore_name = { AST_SIP_X_AST_RESTORE_HOST, AST_SIP_X_AST_RESTORE_HOST_LEN };
pjsip_param *x_orig_host;
pjsip_sip_uri *uri;
pjsip_hdr *hdr;
if (tdata->msg->type == PJSIP_REQUEST_MSG) {
if (is_sip_uri(tdata->msg->line.req.uri)) {
uri = pjsip_uri_get_uri(tdata->msg->line.req.uri);
while ((x_orig_host = pjsip_param_find(&uri->other_param, &x_name))) {
pj_list_erase(x_orig_host);
}
restore_request_uri_host(tdata->pool, pjsip_uri_get_uri(tdata->msg->line.req.uri));
}
for (hdr = tdata->msg->hdr.next; hdr != &tdata->msg->hdr; hdr = hdr->next) {
if (hdr->type == PJSIP_H_TO) {
if (is_sip_uri(((pjsip_fromto_hdr *) hdr)->uri)) {
uri = pjsip_uri_get_uri(((pjsip_fromto_hdr *) hdr)->uri);
while ((x_orig_host = pjsip_param_find(&uri->other_param, &x_name))) {
pj_list_erase(x_orig_host);
}
restore_request_uri_host(tdata->pool, pjsip_uri_get_uri(((pjsip_fromto_hdr *) hdr)->uri));
}
}
}
@ -312,6 +394,9 @@ static void restore_orig_contact_host(pjsip_tx_data *tdata)
}
pj_list_erase(x_orig_host);
}
while ((x_orig_host = pjsip_param_find(&contact_uri->other_param, &x_restore_name))) {
pj_list_erase(x_orig_host);
}
contact = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_CONTACT, contact->next);
}
}

Loading…
Cancel
Save