res_pjsip: Add support for building against pjproject with SIP transaction group...
[asterisk/asterisk.git] / res / res_pjsip / pjsip_distributor.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * Mark Michelson <mmichelson@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 #include "asterisk.h"
20
21 #include <pjsip.h>
22
23 #include "asterisk/res_pjsip.h"
24
25 static int distribute(void *data);
26 static pj_bool_t distributor(pjsip_rx_data *rdata);
27
28 static pjsip_module distributor_mod = {
29         .name = {"Request Distributor", 19},
30         .priority = PJSIP_MOD_PRIORITY_TSX_LAYER - 6,
31         .on_rx_request = distributor,
32         .on_rx_response = distributor,
33 };
34
35 /*! Dialog-specific information the distributor uses */
36 struct distributor_dialog_data {
37         /* Serializer to distribute tasks to for this dialog */
38         struct ast_taskprocessor *serializer;
39         /* Endpoint associated with this dialog */
40         struct ast_sip_endpoint *endpoint;
41 };
42
43 /*!
44  * \internal
45  *
46  * \note Call this with the dialog locked
47  */
48 static struct distributor_dialog_data *distributor_dialog_data_alloc(pjsip_dialog *dlg)
49 {
50         struct distributor_dialog_data *dist;
51
52         dist = PJ_POOL_ZALLOC_T(dlg->pool, struct distributor_dialog_data);
53         pjsip_dlg_set_mod_data(dlg, distributor_mod.id, dist);
54
55         return dist;
56 }
57
58 void ast_sip_dialog_set_serializer(pjsip_dialog *dlg, struct ast_taskprocessor *serializer)
59 {
60         struct distributor_dialog_data *dist;
61         SCOPED_LOCK(lock, dlg, pjsip_dlg_inc_lock, pjsip_dlg_dec_lock);
62
63         dist = pjsip_dlg_get_mod_data(dlg, distributor_mod.id);
64         if (!dist) {
65                 dist = distributor_dialog_data_alloc(dlg);
66         }
67         dist->serializer = serializer;
68 }
69
70 void ast_sip_dialog_set_endpoint(pjsip_dialog *dlg, struct ast_sip_endpoint *endpoint)
71 {
72         struct distributor_dialog_data *dist;
73         SCOPED_LOCK(lock, dlg, pjsip_dlg_inc_lock, pjsip_dlg_dec_lock);
74
75         dist = pjsip_dlg_get_mod_data(dlg, distributor_mod.id);
76         if (!dist) {
77                 dist = distributor_dialog_data_alloc(dlg);
78         }
79         dist->endpoint = endpoint;
80 }
81
82 struct ast_sip_endpoint *ast_sip_dialog_get_endpoint(pjsip_dialog *dlg)
83 {
84         struct distributor_dialog_data *dist;
85         SCOPED_LOCK(lock, dlg, pjsip_dlg_inc_lock, pjsip_dlg_dec_lock);
86
87         dist = pjsip_dlg_get_mod_data(dlg, distributor_mod.id);
88         if (!dist || !dist->endpoint) {
89                 return NULL;
90         }
91         ao2_ref(dist->endpoint, +1);
92         return dist->endpoint;
93 }
94
95 static pjsip_dialog *find_dialog(pjsip_rx_data *rdata)
96 {
97         pj_str_t tsx_key;
98         pjsip_transaction *tsx;
99         pjsip_dialog *dlg;
100         pj_str_t *local_tag;
101         pj_str_t *remote_tag;
102
103         if (rdata->msg_info.msg->type == PJSIP_REQUEST_MSG) {
104                 local_tag = &rdata->msg_info.to->tag;
105                 remote_tag = &rdata->msg_info.from->tag;
106         } else {
107                 local_tag = &rdata->msg_info.from->tag;
108                 remote_tag = &rdata->msg_info.to->tag;
109         }
110
111         /* We can only call the convenient method for
112          *  1) responses
113          *  2) non-CANCEL requests
114          *  3) CANCEL requests with a to-tag
115          */
116         if (rdata->msg_info.msg->type == PJSIP_RESPONSE_MSG ||
117                         pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, &pjsip_cancel_method) ||
118                         rdata->msg_info.to->tag.slen != 0) {
119                 return pjsip_ua_find_dialog(&rdata->msg_info.cid->id, local_tag,
120                                 remote_tag, PJ_TRUE);
121         }
122
123         /* Incoming CANCEL without a to-tag can't use same method for finding the
124          * dialog. Instead, we have to find the matching INVITE transaction and
125          * then get the dialog from the transaction
126          */
127         pjsip_tsx_create_key(rdata->tp_info.pool, &tsx_key, PJSIP_ROLE_UAS,
128                         pjsip_get_invite_method(), rdata);
129
130         tsx = pjsip_tsx_layer_find_tsx(&tsx_key, PJ_TRUE);
131         if (!tsx) {
132                 ast_log(LOG_ERROR, "Could not find matching INVITE transaction for CANCEL request\n");
133                 return NULL;
134         }
135
136         dlg = pjsip_tsx_get_dlg(tsx);
137
138 #ifdef HAVE_PJ_TRANSACTION_GRP_LOCK
139         pj_grp_lock_release(tsx->grp_lock);
140 #else
141         pj_mutex_unlock(tsx->mutex);
142 #endif
143
144         if (!dlg) {
145                 return NULL;
146         }
147
148         pjsip_dlg_inc_lock(dlg);
149         return dlg;
150 }
151
152 static pj_bool_t distributor(pjsip_rx_data *rdata)
153 {
154         pjsip_dialog *dlg = find_dialog(rdata);
155         struct distributor_dialog_data *dist = NULL;
156         struct ast_taskprocessor *serializer = NULL;
157         pjsip_rx_data *clone;
158
159         if (dlg) {
160                 dist = pjsip_dlg_get_mod_data(dlg, distributor_mod.id);
161                 if (dist) {
162                         serializer = dist->serializer;
163                 }
164         }
165
166         if (rdata->msg_info.msg->type == PJSIP_REQUEST_MSG && (
167                 !pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, &pjsip_cancel_method) || 
168                 !pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, &pjsip_bye_method)) &&
169                 !serializer) {
170                 pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 481, NULL, NULL, NULL);
171                 goto end;
172         }
173
174         pjsip_rx_data_clone(rdata, 0, &clone);
175
176         if (dist) {
177                 clone->endpt_info.mod_data[distributor_mod.id] = dist->endpoint;
178         }
179
180         ast_sip_push_task(serializer, distribute, clone);
181
182 end:
183         if (dlg) {
184                 pjsip_dlg_dec_lock(dlg);
185         }
186
187         return PJ_TRUE;
188 }
189
190 static pj_bool_t endpoint_lookup(pjsip_rx_data *rdata);
191
192 static pjsip_module endpoint_mod = {
193         .name = {"Endpoint Identifier", 19},
194         .priority = PJSIP_MOD_PRIORITY_TSX_LAYER - 3,
195         .on_rx_request = endpoint_lookup,
196 };
197
198 static struct ast_sip_auth *artificial_auth;
199
200 static int create_artificial_auth(void)
201 {
202         if (!(artificial_auth = ast_sorcery_alloc(
203                       ast_sip_get_sorcery(), SIP_SORCERY_AUTH_TYPE, "artificial"))) {
204                 ast_log(LOG_ERROR, "Unable to create artificial auth\n");
205                 return -1;
206         }
207
208         ast_string_field_set(artificial_auth, realm, "asterisk");
209         ast_string_field_set(artificial_auth, auth_user, "");
210         ast_string_field_set(artificial_auth, auth_pass, "");
211         artificial_auth->type = AST_SIP_AUTH_TYPE_ARTIFICIAL;
212         return 0;
213 }
214
215 struct ast_sip_auth *ast_sip_get_artificial_auth(void)
216 {
217         ao2_ref(artificial_auth, +1);
218         return artificial_auth;
219 }
220
221 static struct ast_sip_endpoint *artificial_endpoint;
222
223 static int create_artificial_endpoint(void)
224 {
225         if (!(artificial_endpoint = ast_sorcery_alloc(
226                       ast_sip_get_sorcery(), "endpoint", NULL))) {
227                 return -1;
228         }
229
230         artificial_endpoint->inbound_auths.num = 1;
231         return 0;
232 }
233
234 struct ast_sip_endpoint *ast_sip_get_artificial_endpoint(void)
235 {
236         ao2_ref(artificial_endpoint, +1);
237         return artificial_endpoint;
238 }
239
240 static void log_unidentified_request(pjsip_rx_data *rdata)
241 {
242         char from_buf[PJSIP_MAX_URL_SIZE];
243         char callid_buf[PJSIP_MAX_URL_SIZE];
244         pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, rdata->msg_info.from->uri, from_buf, PJSIP_MAX_URL_SIZE);
245         ast_copy_pj_str(callid_buf, &rdata->msg_info.cid->id, PJSIP_MAX_URL_SIZE);
246         ast_log(LOG_NOTICE, "Request from '%s' failed for '%s:%d' (callid: %s) - No matching endpoint found\n",
247                 from_buf, rdata->pkt_info.src_name, rdata->pkt_info.src_port, callid_buf);
248 }
249
250 static pj_bool_t endpoint_lookup(pjsip_rx_data *rdata)
251 {
252         struct ast_sip_endpoint *endpoint;
253         int is_ack = rdata->msg_info.msg->line.req.method.id == PJSIP_ACK_METHOD;
254
255         endpoint = rdata->endpt_info.mod_data[distributor_mod.id];
256         if (endpoint) {
257                 /* Bumping the refcount makes refcounting consistent whether an endpoint
258                  * is looked up or not */
259                 ao2_ref(endpoint, +1);
260         } else {
261                 endpoint = ast_sip_identify_endpoint(rdata);
262         }
263
264         if (!endpoint && !is_ack) {
265                 char name[AST_UUID_STR_LEN] = "";
266                 pjsip_uri *from = rdata->msg_info.from->uri;
267
268                 /* always use an artificial endpoint - per discussion no reason
269                    to have "alwaysauthreject" as an option.  It is felt using it
270                    was a bug fix and it is not needed since we are not worried about
271                    breaking old stuff and we really don't want to enable the discovery
272                    of SIP accounts */
273                 endpoint = ast_sip_get_artificial_endpoint();
274
275                 if (PJSIP_URI_SCHEME_IS_SIP(from) || PJSIP_URI_SCHEME_IS_SIPS(from)) {
276                         pjsip_sip_uri *sip_from = pjsip_uri_get_uri(from);
277                         ast_copy_pj_str(name, &sip_from->user, sizeof(name));
278                 }
279
280                 log_unidentified_request(rdata);
281                 ast_sip_report_invalid_endpoint(name, rdata);
282         }
283         rdata->endpt_info.mod_data[endpoint_mod.id] = endpoint;
284         return PJ_FALSE;
285 }
286
287 static pj_bool_t authenticate(pjsip_rx_data *rdata)
288 {
289         RAII_VAR(struct ast_sip_endpoint *, endpoint, ast_pjsip_rdata_get_endpoint(rdata), ao2_cleanup);
290         int is_ack = rdata->msg_info.msg->line.req.method.id == PJSIP_ACK_METHOD;
291
292         ast_assert(endpoint != NULL);
293
294         if (!is_ack && ast_sip_requires_authentication(endpoint, rdata)) {
295                 pjsip_tx_data *tdata;
296                 pjsip_endpt_create_response(ast_sip_get_pjsip_endpoint(), rdata, 401, NULL, &tdata);
297                 switch (ast_sip_check_authentication(endpoint, rdata, tdata)) {
298                 case AST_SIP_AUTHENTICATION_CHALLENGE:
299                         /* Send the 401 we created for them */
300                         ast_sip_report_auth_challenge_sent(endpoint, rdata, tdata);
301                         pjsip_endpt_send_response2(ast_sip_get_pjsip_endpoint(), rdata, tdata, NULL, NULL);
302                         return PJ_TRUE;
303                 case AST_SIP_AUTHENTICATION_SUCCESS:
304                         ast_sip_report_auth_success(endpoint, rdata);
305                         pjsip_tx_data_dec_ref(tdata);
306                         return PJ_FALSE;
307                 case AST_SIP_AUTHENTICATION_FAILED:
308                         ast_sip_report_auth_failed_challenge_response(endpoint, rdata);
309                         pjsip_endpt_send_response2(ast_sip_get_pjsip_endpoint(), rdata, tdata, NULL, NULL);
310                         return PJ_TRUE;
311                 case AST_SIP_AUTHENTICATION_ERROR:
312                         ast_sip_report_auth_failed_challenge_response(endpoint, rdata);
313                         pjsip_tx_data_dec_ref(tdata);
314                         pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 500, NULL, NULL, NULL);
315                         return PJ_TRUE;
316                 }
317         }
318
319         return PJ_FALSE;
320 }
321
322 static pjsip_module auth_mod = {
323         .name = {"Request Authenticator", 21},
324         .priority = PJSIP_MOD_PRIORITY_APPLICATION - 1,
325         .on_rx_request = authenticate,
326 };
327
328 static int distribute(void *data)
329 {
330         static pjsip_process_rdata_param param = {
331                 .start_mod = &distributor_mod,
332                 .idx_after_start = 1,
333         };
334         pj_bool_t handled;
335         pjsip_rx_data *rdata = data;
336         int is_request = rdata->msg_info.msg->type == PJSIP_REQUEST_MSG;
337         int is_ack = is_request ? rdata->msg_info.msg->line.req.method.id == PJSIP_ACK_METHOD : 0;
338         struct ast_sip_endpoint *endpoint;
339
340         pjsip_endpt_process_rx_data(ast_sip_get_pjsip_endpoint(), rdata, &param, &handled);
341         if (!handled && is_request && !is_ack) {
342                 pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 501, NULL, NULL, NULL);
343         }
344
345         /* The endpoint_mod stores an endpoint reference in the mod_data of rdata. This
346          * is the only appropriate spot to actually decrement the reference.
347          */
348         endpoint = rdata->endpt_info.mod_data[endpoint_mod.id];
349         ao2_cleanup(endpoint);
350         pjsip_rx_data_free_cloned(rdata);
351         return 0;
352 }
353
354 struct ast_sip_endpoint *ast_pjsip_rdata_get_endpoint(pjsip_rx_data *rdata)
355 {
356         struct ast_sip_endpoint *endpoint = rdata->endpt_info.mod_data[endpoint_mod.id];
357         if (endpoint) {
358                 ao2_ref(endpoint, +1);
359         }
360         return endpoint;
361 }
362
363 int ast_sip_initialize_distributor(void)
364 {
365         if (create_artificial_endpoint() || create_artificial_auth()) {
366                 return -1;
367         }
368
369         if (ast_sip_register_service(&distributor_mod)) {
370                 return -1;
371         }
372         if (ast_sip_register_service(&endpoint_mod)) {
373                 return -1;
374         }
375         if (ast_sip_register_service(&auth_mod)) {
376                 return -1;
377         }
378
379         return 0;
380 }
381
382 void ast_sip_destroy_distributor(void)
383 {
384         ast_sip_unregister_service(&distributor_mod);
385         ast_sip_unregister_service(&endpoint_mod);
386         ast_sip_unregister_service(&auth_mod);
387
388         ao2_cleanup(artificial_auth);
389         ao2_cleanup(artificial_endpoint);
390 }