Commit cfb0aa2a84 for asterisk.org
commit cfb0aa2a84a2c78a64c8764b213bfd0623975596
Author: Mike Bradeen <mbradeen@sangoma.com>
Date: Fri Nov 13 13:42:01 2026 -0700
res_rtp_asterisk: Assign unique ICE host candidate priorities.
Add local interfaces above additional mappings, making sure that
each gets a unique priority as they are added.
Fixes: #2298
UpgradeNote: ICE host candidates now receive distinct priorities.
When include_local_address is enabled in rtp.conf, local interface
candidates have higher priority than additional mapped candidates.
This can change ICE media path selection. No configuration changes
are required.
diff --git a/configs/samples/rtp.conf.sample b/configs/samples/rtp.conf.sample
index d1429dc136..a85df3525b 100644
--- a/configs/samples/rtp.conf.sample
+++ b/configs/samples/rtp.conf.sample
@@ -174,7 +174,9 @@ rtpend=20000
;
; The following will include BOTH 192.168.1.10 and 1.2.3.4 during ICE
; negotiation instead of replacing 192.168.1.10. This can make it easier
-; to serve both local and remote clients.
+; to serve both local and remote clients. When both are present, local
+; interface candidates have higher ICE priority than the additional
+; advertised address.
;
;192.168.1.10 => 1.2.3.4,include_local_address
;
diff --git a/res/res_rtp_asterisk.c b/res/res_rtp_asterisk.c
index e3b05f4786..4ac6d28593 100644
--- a/res/res_rtp_asterisk.c
+++ b/res/res_rtp_asterisk.c
@@ -1297,8 +1297,13 @@ static void ast_rtp_ice_set_role(struct ast_rtp_instance *instance, enum ast_rtp
}
}
-/*! \pre instance is locked */
-static void ast_rtp_ice_add_cand(struct ast_rtp_instance *instance, struct ast_rtp *rtp,
+/*! \pre instance is locked
+ *
+ * \retval 1 if the candidate was added
+ * \retval 0 if the candidate was not added
+ *
+ */
+static int ast_rtp_ice_add_cand(struct ast_rtp_instance *instance, struct ast_rtp *rtp,
unsigned comp_id, unsigned transport_id, pj_ice_cand_type type, pj_uint16_t local_pref,
const pj_sockaddr_t *addr, const pj_sockaddr_t *base_addr, const pj_sockaddr_t *rel_addr,
int addr_len)
@@ -1310,7 +1315,7 @@ static void ast_rtp_ice_add_cand(struct ast_rtp_instance *instance, struct ast_r
pj_status_t status;
if (!rtp->ice) {
- return;
+ return 0;
}
pj_thread_register_check();
@@ -1321,12 +1326,12 @@ static void ast_rtp_ice_add_cand(struct ast_rtp_instance *instance, struct ast_r
rtp->ice_local_candidates = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_MUTEX, 0,
NULL, ice_candidate_cmp);
if (!rtp->ice_local_candidates) {
- return;
+ return 0;
}
}
if (!(candidate = ao2_alloc(sizeof(*candidate), ast_rtp_ice_candidate_destroy))) {
- return;
+ return 0;
}
candidate->foundation = ast_strndup(pj_strbuf(&foundation), pj_strlen(&foundation));
@@ -1352,7 +1357,7 @@ static void ast_rtp_ice_add_cand(struct ast_rtp_instance *instance, struct ast_r
if ((existing = ao2_find(rtp->ice_local_candidates, candidate, OBJ_POINTER))) {
ao2_ref(existing, -1);
ao2_ref(candidate, -1);
- return;
+ return 0;
}
/* Release the instance lock to avoid deadlock with PJPROJECT group lock */
@@ -1367,7 +1372,7 @@ static void ast_rtp_ice_add_cand(struct ast_rtp_instance *instance, struct ast_r
ast_debug_ice(2, "(%p) ICE unable to add candidate: %s, %d\n", instance, ast_sockaddr_stringify(
&candidate->address), candidate->priority);
ao2_ref(candidate, -1);
- return;
+ return 0;
}
/* By placing the candidate into the ICE session it will have produced the priority, so update the local candidate with it */
@@ -1378,6 +1383,8 @@ static void ast_rtp_ice_add_cand(struct ast_rtp_instance *instance, struct ast_r
ao2_link(rtp->ice_local_candidates, candidate);
ao2_ref(candidate, -1);
+
+ return 1;
}
/* PJPROJECT TURN callback */
@@ -3763,11 +3770,71 @@ static int stun_address_is_blacklisted(const struct ast_sockaddr *addr)
return result;
}
+/*!
+ * \brief Check whether an interface address can be used as an ICE host candidate.
+ *
+ * The interface must be up, have an assigned IPv4 or IPv6 address, and use an
+ * address family compatible with the RTP bind address. Loopback IPv4 and IPv6
+ * addresses are rejected, as is the IPv4 0.0.0.0/8 range. If the address is
+ * eligible, it is copied into \a address for use by the caller.
+ *
+ * \param ia Interface address to check.
+ * \param address Output for the converted interface address.
+ * \param af_inet_ok Whether IPv4 candidates are allowed.
+ * \param af_inet6_ok Whether IPv6 candidates are allowed.
+ *
+ * \retval 1 The interface address is eligible and was copied into \a address.
+ * \retval 0 The interface address should be skipped.
+ */
+static int rtp_ice_check_host_address(const struct ifaddrs *ia, struct ast_sockaddr *address,
+ int af_inet_ok, int af_inet6_ok)
+{
+ /* Interface is either not UP or doesn't have an address assigned,
+ * eg, a ppp that just completed LCP but no IPCP yet */
+ if (!ia->ifa_addr || (ia->ifa_flags & IFF_UP) == 0) {
+ return 0;
+ }
+
+ /* Filter out non-IPvX addresses, eg, link-layer */
+ if (ia->ifa_addr->sa_family != AF_INET && ia->ifa_addr->sa_family != AF_INET6) {
+ return 0;
+ }
+
+ ast_sockaddr_from_sockaddr(address, ia->ifa_addr);
+
+ if (ia->ifa_addr->sa_family == AF_INET) {
+ const struct sockaddr_in *sa_in = (struct sockaddr_in *) ia->ifa_addr;
+
+ if (!af_inet_ok) {
+ return 0;
+ }
+
+ /* Skip 127.0.0.0/8 (loopback). Don't use IFF_LOOPBACK since one
+ * could assign usable publics to loopback. */
+ if ((sa_in->sin_addr.s_addr & htonl(0xFF000000)) == htonl(0x7F000000)) {
+ return 0;
+ }
+
+ /* Skip 0.0.0.0/8 based on RFC1122 and pjproject. */
+ if ((sa_in->sin_addr.s_addr & htonl(0xFF000000)) == 0) {
+ return 0;
+ }
+ } else {
+ if (!af_inet6_ok || !ast_sockaddr_cmp_addr(&lo6, address)) {
+ return 0;
+ }
+ }
+
+ return 1;
+}
+
/*! \pre instance is locked */
static void rtp_add_candidates_to_ice(struct ast_rtp_instance *instance, struct ast_rtp *rtp, struct ast_sockaddr *addr, int port, int component,
- int transport)
+ int transport)
{
unsigned int count = 0;
+ unsigned int maximum;
+ unsigned int preferred_index = 65535;
struct ifaddrs *ifa, *ia;
struct ast_sockaddr tmp;
pj_sockaddr pjtmp;
@@ -3777,6 +3844,13 @@ static void rtp_add_candidates_to_ice(struct ast_rtp_instance *instance, struct
int stunaddr_ttl_copy = 0;
char *stun_hostname_copy = NULL;
+ /* If we have reached the maximum number of ICE candidates, return as pjnath
+ * will reject additional ones. */
+ if (rtp->ice->real_ice->lcand_cnt >= PJ_ICE_MAX_CAND) {
+ return;
+ }
+ maximum = PJ_ICE_MAX_CAND - rtp->ice->real_ice->lcand_cnt;
+
if (ast_sockaddr_is_ipv4(addr)) {
af_inet_ok = 1;
} else if (ast_sockaddr_is_any(addr)) {
@@ -3791,49 +3865,46 @@ static void rtp_add_candidates_to_ice(struct ast_rtp_instance *instance, struct
instance, strerror(errno));
} else {
ast_debug_ice(2, "(%p) ICE add system candidates\n", instance);
- /* Iterate through the list of addresses obtained from the system,
- * until we've iterated through all of them, or accepted
- * PJ_ICE_MAX_CAND candidates */
- for (ia = ifa; ia && count < PJ_ICE_MAX_CAND; ia = ia->ifa_next) {
- /* Interface is either not UP or doesn't have an address assigned,
- * eg, a ppp that just completed LCP but no IPCP yet */
- if (!ia->ifa_addr || (ia->ifa_flags & IFF_UP) == 0) {
- continue;
- }
+ /* Add local addresses first so a matching mapped address does not lower its priority. */
+ for (ia = ifa; ia && count < maximum; ia = ia->ifa_next) {
+ /* should we include this address */
+ unsigned int include_local_address = 1;
- /* Filter out non-IPvX addresses, eg, link-layer */
- if (ia->ifa_addr->sa_family != AF_INET && ia->ifa_addr->sa_family != AF_INET6) {
+ if (!rtp_ice_check_host_address(ia, &tmp, af_inet_ok, af_inet6_ok)) {
continue;
}
- ast_sockaddr_from_sockaddr(&tmp, ia->ifa_addr);
-
- if (ia->ifa_addr->sa_family == AF_INET) {
- const struct sockaddr_in *sa_in = (struct sockaddr_in*)ia->ifa_addr;
- if (!af_inet_ok) {
- continue;
+ /* Pull in the host candidates from [ice_host_candidates] */
+ AST_RWLIST_RDLOCK(&host_candidates);
+ AST_LIST_TRAVERSE(&host_candidates, candidate, next) {
+ if (!ast_sockaddr_cmp(&candidate->local, &tmp)) {
+ include_local_address = candidate->include_local;
+ break;
}
+ }
+ AST_RWLIST_UNLOCK(&host_candidates);
- /* Skip 127.0.0.0/8 (loopback) */
- /* Don't use IFF_LOOPBACK check since one could assign usable
- * publics to the loopback */
- if ((sa_in->sin_addr.s_addr & htonl(0xFF000000)) == htonl(0x7F000000)) {
- continue;
- }
+ if (!include_local_address || rtp_address_is_ice_blacklisted(&tmp)) {
+ continue;
+ }
- /* Skip 0.0.0.0/8 based on RFC1122, and from pjproject */
- if ((sa_in->sin_addr.s_addr & htonl(0xFF000000)) == 0) {
- continue;
- }
- } else { /* ia->ifa_addr->sa_family == AF_INET6 */
- if (!af_inet6_ok) {
- continue;
- }
+ ast_sockaddr_to_pj_sockaddr(&tmp, &pjtmp);
+ pj_sockaddr_set_port(&pjtmp, port);
+ if (ast_rtp_ice_add_cand(instance, rtp, component, transport,
+ PJ_ICE_CAND_TYPE_HOST, preferred_index, &pjtmp, &pjtmp, NULL,
+ pj_sockaddr_get_len(&pjtmp))) {
+ --preferred_index;
+ ++count;
+ }
+ }
- /* Filter ::1 */
- if (!ast_sockaddr_cmp_addr(&lo6, &tmp)) {
- continue;
- }
+ /* Add mapped addresses using the remaining, lower preference values. */
+ for (ia = ifa; ia && count < maximum; ia = ia->ifa_next) {
+ unsigned int add_mapped = 0;
+ struct ast_sockaddr mapped_address;
+
+ if (!rtp_ice_check_host_address(ia, &tmp, af_inet_ok, af_inet6_ok)) {
+ continue;
}
/* Pull in the host candidates from [ice_host_candidates] */
@@ -3846,17 +3917,8 @@ static void rtp_add_candidates_to_ice(struct ast_rtp_instance *instance, struct
* a local address to a blacklisted address, but honour it
* anyway. */
if (!rtp_address_is_ice_blacklisted(&candidate->advertised)) {
- ast_sockaddr_to_pj_sockaddr(&candidate->advertised, &pjtmp);
- pj_sockaddr_set_port(&pjtmp, port);
- ast_rtp_ice_add_cand(instance, rtp, component, transport,
- PJ_ICE_CAND_TYPE_HOST, 65535, &pjtmp, &pjtmp, NULL,
- pj_sockaddr_get_len(&pjtmp));
- ++count;
- }
-
- if (!candidate->include_local) {
- /* We don't want to advertise the actual address */
- ast_sockaddr_setnull(&tmp);
+ ast_sockaddr_copy(&mapped_address, &candidate->advertised);
+ add_mapped = 1;
}
break;
@@ -3864,23 +3926,16 @@ static void rtp_add_candidates_to_ice(struct ast_rtp_instance *instance, struct
}
AST_RWLIST_UNLOCK(&host_candidates);
- /* we had an entry in [ice_host_candidates] that matched, and
- * didn't have include_local_address set. Alternatively, adding
- * that match resulted in us going to PJ_ICE_MAX_CAND */
- if (ast_sockaddr_isnull(&tmp) || count == PJ_ICE_MAX_CAND) {
- continue;
- }
-
- if (rtp_address_is_ice_blacklisted(&tmp)) {
- continue;
+ if (add_mapped) {
+ ast_sockaddr_to_pj_sockaddr(&mapped_address, &pjtmp);
+ pj_sockaddr_set_port(&pjtmp, port);
+ if (ast_rtp_ice_add_cand(instance, rtp, component, transport,
+ PJ_ICE_CAND_TYPE_HOST, preferred_index, &pjtmp, &pjtmp, NULL,
+ pj_sockaddr_get_len(&pjtmp))) {
+ --preferred_index;
+ ++count;
+ }
}
-
- ast_sockaddr_to_pj_sockaddr(&tmp, &pjtmp);
- pj_sockaddr_set_port(&pjtmp, port);
- ast_rtp_ice_add_cand(instance, rtp, component, transport,
- PJ_ICE_CAND_TYPE_HOST, 65535, &pjtmp, &pjtmp, NULL,
- pj_sockaddr_get_len(&pjtmp));
- ++count;
}
freeifaddrs(ifa);
}
@@ -3899,7 +3954,7 @@ static void rtp_add_candidates_to_ice(struct ast_rtp_instance *instance, struct
if ( (!ast_strlen_zero(stun_hostname_copy) || stunaddr_copy.sin_addr.s_addr)
&& !stun_address_is_blacklisted(addr) &&
(ast_sockaddr_is_ipv4(addr) || ast_sockaddr_is_any(addr)) &&
- count < PJ_ICE_MAX_CAND) {
+ count < maximum) {
struct sockaddr_in answer;
int rsp;