]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
res_rtp_asterisk: Fix regression issues with DTLS client check
authorGeorge Joseph <gjoseph@sangoma.com>
Fri, 15 Dec 2023 16:37:54 +0000 (09:37 -0700)
committerGeorge Joseph <gjoseph@sangoma.com>
Wed, 20 Dec 2023 14:02:25 +0000 (14:02 +0000)
* Since ICE candidates are used for the check and pjproject is
  required to use ICE, res_rtp_asterisk was failing to compile
  when pjproject wasn't available.  The check is now wrapped
  with an #ifdef HAVE_PJPROJECT.

* The rtp->ice_active_remote_candidates container was being
  used to check the address on incoming packets but that
  container doesn't contain peer reflexive candidates discovered
  during negotiation. This was causing the check to fail
  where it shouldn't.  We now check against pjproject's
  real_ice->rcand array which will contain those candidates.

* Also fixed a bug in ast_sockaddr_from_pj_sockaddr() where
  we weren't zeroing out sin->sin_zero before returning.  This
  was causing ast_sockaddr_cmp() to always return false when
  one of the inputs was converted from a pj_sockaddr, even
  if both inputs had the same address and port.

Resolves: #500
Resolves: #503
Resolves: #505

include/asterisk/res_pjproject.h
res/res_pjproject.c
res/res_rtp_asterisk.c

index eee8c4eee81f75608c5ea5ce8739c3d84bce02ce..823f5cc54583b39e18de85ea3bf989f7e33e46ac 100644 (file)
@@ -115,4 +115,17 @@ int ast_sockaddr_to_pj_sockaddr(const struct ast_sockaddr *addr, pj_sockaddr *pj
  */
 int ast_sockaddr_from_pj_sockaddr(struct ast_sockaddr *addr, const pj_sockaddr *pjaddr);
 
+/*!
+ * \brief Compare an ast_sockaddr to a pj_sockaddr
+ *
+ * \param addr pointer to ast_sockaddr structure
+ * \param pjaddr pointer to pj_sockaddr structure
+ *
+ * \retval -1 \a addr is lexicographically smaller than \a pjaddr
+ * \retval 0 \a addr is equal to \a pjaddr
+ * \retval 1 \a pjaddr is lexicographically smaller than \a addr
+*/
+int ast_sockaddr_pj_sockaddr_cmp(const struct ast_sockaddr *addr,
+       const pj_sockaddr *pjaddr);
+
 #endif /* _RES_PJPROJECT_H */
index 8eadaee82f659e124215550a966f2579dd16e20b..865f06278fcedb8281ad7def484dd057688f687a 100644 (file)
@@ -522,6 +522,7 @@ int ast_sockaddr_from_pj_sockaddr(struct ast_sockaddr *addr, const pj_sockaddr *
                sin->sin_addr.s_addr = pjaddr->ipv4.sin_addr.s_addr;
 #endif
                sin->sin_port   = pjaddr->ipv4.sin_port;
+               memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
                addr->len = sizeof(struct sockaddr_in);
        } else if (pjaddr->addr.sa_family == pj_AF_INET6()) {
                struct sockaddr_in6 *sin = (struct sockaddr_in6 *) &addr->ss;
@@ -538,6 +539,27 @@ int ast_sockaddr_from_pj_sockaddr(struct ast_sockaddr *addr, const pj_sockaddr *
        return 0;
 }
 
+int ast_sockaddr_pj_sockaddr_cmp(const struct ast_sockaddr *addr,
+       const pj_sockaddr *pjaddr)
+{
+       struct ast_sockaddr temp_pjaddr;
+       int rc = 0;
+
+       rc = ast_sockaddr_from_pj_sockaddr(&temp_pjaddr, pjaddr);
+       if (rc != 0) {
+               return -1;
+       }
+
+       rc = ast_sockaddr_cmp(addr, &temp_pjaddr);
+       if (DEBUG_ATLEAST(4)) {
+               char *a_str = ast_strdupa(ast_sockaddr_stringify(addr));
+               char *pj_str = ast_strdupa(ast_sockaddr_stringify(&temp_pjaddr));
+               ast_debug(4, "Comparing %s -> %s  rc: %d\n", a_str, pj_str, rc);
+       }
+
+       return rc;
+}
+
 #ifdef TEST_FRAMEWORK
 static void fill_with_garbage(void *x, ssize_t len)
 {
index 2341c846ff2e6a4e14f1095511d57e390bb470ed..c8af3c376350f6525fd6990f4d2b37ce2f4a7936 100644 (file)
@@ -3214,11 +3214,10 @@ static int __rtp_recvfrom(struct ast_rtp_instance *instance, void *buf, size_t s
                 * candidates list.
                 */
 
+#ifdef HAVE_PJPROJECT
                if (rtp->ice) {
                        int pass_src_check = 0;
-                       struct ao2_iterator i;
-                       struct ast_rtp_engine_ice_candidate *candidate;
-                       int cand_cnt = 0;
+                       int ix = 0;
 
                        /*
                         * You'd think that this check would cause a "deadlock"
@@ -3239,20 +3238,18 @@ static int __rtp_recvfrom(struct ast_rtp_instance *instance, void *buf, size_t s
                        }
 
                        /*
-                        * If we got this far, then ice_active_remote_candidates
-                        * can't be NULL.
+                        * If we got this far, then there have to be candidates.
+                        * We have to use pjproject's rcands because they may have
+                        * peer reflexive candidates that our ice_active_remote_candidates
+                        * won't.
                         */
-                       i = ao2_iterator_init(rtp->ice_active_remote_candidates, 0);
-                       while ((candidate = ao2_iterator_next(&i)) && (cand_cnt < PJ_ICE_MAX_CAND)) {
-                               res = ast_sockaddr_cmp_addr(&candidate->address, sa);
-                               ao2_ref(candidate, -1);
-                               if (res == 0) {
+                       for (ix = 0; ix < rtp->ice->real_ice->rcand_cnt; ix++) {
+                               pj_ice_sess_cand *rcand = &rtp->ice->real_ice->rcand[ix];
+                               if (ast_sockaddr_pj_sockaddr_cmp(sa, &rcand->addr) == 0) {
                                        pass_src_check = 1;
                                        break;
                                }
-                               cand_cnt++;
                        }
-                       ao2_iterator_destroy(&i);
 
                        if (!pass_src_check) {
                                ast_log(LOG_WARNING, "%s: DTLS packet from %s dropped. Source not in ICE active candidate list.\n",
@@ -3261,6 +3258,7 @@ static int __rtp_recvfrom(struct ast_rtp_instance *instance, void *buf, size_t s
                                return 0;
                        }
                }
+#endif
 
                /*
                 * A race condition is prevented between dtls_perform_handshake()