diff --git a/configs/samples/pjsip.conf.sample b/configs/samples/pjsip.conf.sample index e01f3f756c..646d6fd985 100644 --- a/configs/samples/pjsip.conf.sample +++ b/configs/samples/pjsip.conf.sample @@ -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========================= diff --git a/contrib/ast-db-manage/config/versions/7c3f9a1e2b6d_add_restore_orig_contact_host.py b/contrib/ast-db-manage/config/versions/7c3f9a1e2b6d_add_restore_orig_contact_host.py new file mode 100644 index 0000000000..bd2a4d002c --- /dev/null +++ b/contrib/ast-db-manage/config/versions/7c3f9a1e2b6d_add_restore_orig_contact_host.py @@ -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') diff --git a/include/asterisk/res_pjsip.h b/include/asterisk/res_pjsip.h index 17cbf4d981..37f87b15fc 100644 --- a/include/asterisk/res_pjsip.h +++ b/include/asterisk/res_pjsip.h @@ -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 */ diff --git a/res/res_pjsip/pjsip_config.xml b/res/res_pjsip/pjsip_config.xml index 86a606b649..bebc77d1f6 100644 --- a/res/res_pjsip/pjsip_config.xml +++ b/res/res_pjsip/pjsip_config.xml @@ -2087,6 +2087,32 @@ + + Keep the device's own Contact host in requests sent to a rewritten contact + + When rewrite_contact 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. + + + 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. + + + This option has no effect unless rewrite_contact + is enabled. Contacts registered before the option changes keep the previous + behavior until they register again. WebSocket transports are not affected. + + + + diff --git a/res/res_pjsip/pjsip_configuration.c b/res/res_pjsip/pjsip_configuration.c index 7056b7a2a3..c6ac81b580 100644 --- a/res/res_pjsip/pjsip_configuration.c +++ b/res/res_pjsip/pjsip_configuration.c @@ -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"); diff --git a/res/res_pjsip_nat.c b/res/res_pjsip_nat.c index 5749a3d610..f2d4833dda 100644 --- a/res/res_pjsip_nat.c +++ b/res/res_pjsip_nat.c @@ -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, ¶m->value, sizeof(host_port)); + /* The saved value always ends with ":", 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); } }