80339d3aa44260909544c08a92547fe88f48bff7
[feed/telephony.git] / net / asterisk-13.x / patches / 180-AST-2020-002-13.diff
1 From 01b7ac0d590b0ad2e3e856d1a81fc87154ae68a0 Mon Sep 17 00:00:00 2001
2 From: Ben Ford <bford@digium.com>
3 Date: Mon, 02 Nov 2020 10:29:31 -0600
4 Subject: [PATCH] AST-2020-002 - res_pjsip: Stop sending INVITEs after challenge limit.
5
6 If Asterisk sends out an INVITE and receives a challenge with a
7 different nonce value each time, it will continuously send out INVITEs,
8 even if the call is hung up. The endpoint must be configured for
9 outbound authentication for this to occur. A limit has been set on
10 outbound INVITEs so that, once reached, Asterisk will stop sending
11 INVITEs and the transaction will terminate.
12
13 ASTERISK-29013
14
15 Change-Id: I2d001ca745b00ca8aa12030f2240cd72363b46f7
16 ---
17
18 --- a/include/asterisk/res_pjsip.h
19 +++ b/include/asterisk/res_pjsip.h
20 @@ -64,6 +64,9 @@ struct pjsip_tpselector;
21 /*! \brief Maximum number of ciphers supported for a TLS transport */
22 #define SIP_TLS_MAX_CIPHERS 64
23
24 +/*! Maximum number of challenges before assuming that we are in a loop */
25 +#define MAX_RX_CHALLENGES 10
26 +
27 /*!
28 * \brief Structure for SIP transport information
29 */
30 --- a/include/asterisk/res_pjsip_session.h
31 +++ b/include/asterisk/res_pjsip_session.h
32 @@ -161,6 +161,8 @@ struct ast_sip_session {
33 enum ast_sip_dtmf_mode dtmf;
34 /*! Initial incoming INVITE Request-URI. NULL otherwise. */
35 pjsip_uri *request_uri;
36 + /*! Number of challenges received during outgoing requests to determine if we are in a loop */
37 + unsigned int authentication_challenge_count:4;
38 };
39
40 typedef int (*ast_sip_session_request_creation_cb)(struct ast_sip_session *session, pjsip_tx_data *tdata);
41 --- a/res/res_pjsip.c
42 +++ b/res/res_pjsip.c
43 @@ -3693,8 +3693,6 @@ static pj_bool_t does_method_match(const
44 return pj_stristr(&method, message_method) ? PJ_TRUE : PJ_FALSE;
45 }
46
47 -/*! Maximum number of challenges before assuming that we are in a loop */
48 -#define MAX_RX_CHALLENGES 10
49 #define TIMER_INACTIVE 0
50 #define TIMEOUT_TIMER2 5
51
52 --- a/res/res_pjsip_session.c
53 +++ b/res/res_pjsip_session.c
54 @@ -1184,7 +1184,6 @@ static pjsip_module session_reinvite_mod
55 .on_rx_request = session_reinvite_on_rx_request,
56 };
57
58 -
59 void ast_sip_session_send_request_with_cb(struct ast_sip_session *session, pjsip_tx_data *tdata,
60 ast_sip_session_response_cb on_response)
61 {
62 @@ -1470,12 +1469,17 @@ struct ast_sip_session *ast_sip_session_
63 ao2_ref(session, -1);
64 return NULL;
65 }
66 +
67 + /* Track the number of challenges received on outbound requests */
68 + session->authentication_challenge_count = 0;
69 +
70 AST_LIST_TRAVERSE(&session->supplements, iter, next) {
71 if (iter->session_begin) {
72 iter->session_begin(session);
73 }
74 }
75
76 +
77 /* Avoid unnecessary ref manipulation to return a session */
78 ret_session = session;
79 session = NULL;
80 @@ -1642,6 +1646,11 @@ static pj_bool_t outbound_invite_auth(pj
81
82 session = inv->mod_data[session_module.id];
83
84 + if (++session->authentication_challenge_count > MAX_RX_CHALLENGES) {
85 + ast_debug(3, "Initial INVITE reached maximum number of auth attempts.\n");
86 + return PJ_FALSE;
87 + }
88 +
89 if (ast_sip_create_request_with_auth(&session->endpoint->outbound_auths, rdata, tsx,
90 &tdata)) {
91 return PJ_FALSE;
92 @@ -2888,6 +2897,7 @@ static void session_inv_on_tsx_state_cha
93 ast_debug(1, "reINVITE received final response code %d\n",
94 tsx->status_code);
95 if ((tsx->status_code == 401 || tsx->status_code == 407)
96 + && ++session->authentication_challenge_count < MAX_RX_CHALLENGES
97 && !ast_sip_create_request_with_auth(
98 &session->endpoint->outbound_auths,
99 e->body.tsx_state.src.rdata, tsx, &tdata)) {
100 @@ -2962,6 +2972,7 @@ static void session_inv_on_tsx_state_cha
101 (int) pj_strlen(&tsx->method.name), pj_strbuf(&tsx->method.name),
102 tsx->status_code);
103 if ((tsx->status_code == 401 || tsx->status_code == 407)
104 + && ++session->authentication_challenge_count < MAX_RX_CHALLENGES
105 && !ast_sip_create_request_with_auth(
106 &session->endpoint->outbound_auths,
107 e->body.tsx_state.src.rdata, tsx, &tdata)) {