pjsip_distributor.c 11 KB

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