blob: cffbe0dac3e2f4a7ef8cced71b96295457936961 [file] [log] [blame]
Harald Welte9ee48252009-07-23 21:25:08 +02001/* GSM 04.07 Transaction handling */
2
3/* (C) 2009 by Harald Welte <laforge@gnumonks.org>
4 * All Rights Reserved
5 *
6 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +01007 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
Harald Welte9ee48252009-07-23 21:25:08 +02009 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte9af6ddf2011-01-01 15:25:50 +010014 * GNU Affero General Public License for more details.
Harald Welte9ee48252009-07-23 21:25:08 +020015 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010016 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte9ee48252009-07-23 21:25:08 +020018 *
19 */
20
Neels Hofmeyr90843962017-09-04 15:04:35 +020021#include <osmocom/msc/transaction.h>
22#include <osmocom/msc/gsm_data.h>
23#include <osmocom/msc/mncc.h>
24#include <osmocom/msc/debug.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010025#include <osmocom/core/talloc.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020026#include <osmocom/msc/gsm_04_08.h>
27#include <osmocom/msc/mncc.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020028#include <osmocom/msc/osmo_msc.h>
29#include <osmocom/msc/vlr.h>
Harald Welte9ee48252009-07-23 21:25:08 +020030
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020031void *tall_trans_ctx;
Harald Welte9ee48252009-07-23 21:25:08 +020032
Holger Hans Peter Freyther34e97492009-08-10 07:54:02 +020033void _gsm48_cc_trans_free(struct gsm_trans *trans);
Vadim Yanitskiyc350c092018-11-21 20:51:51 +070034void _gsm411_sms_trans_free(struct gsm_trans *trans);
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +070035void _gsm911_nc_ss_trans_free(struct gsm_trans *trans);
Holger Hans Peter Freyther34e97492009-08-10 07:54:02 +020036
Harald Welte2483f1b2016-06-19 18:06:02 +020037/*! Find a transaction in connection for given protocol + transaction ID
Vadim Yanitskiy380b2192018-11-27 15:14:31 +070038 * \param[in] conn Connection in which we want to find transaction
Harald Welte2483f1b2016-06-19 18:06:02 +020039 * \param[in] proto Protocol of transaction
40 * \param[in] trans_id Transaction ID of transaction
41 * \returns Matching transaction, if any
42 */
Jacob Erlbeckdae1f642014-12-02 14:22:53 +010043struct gsm_trans *trans_find_by_id(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020044 uint8_t proto, uint8_t trans_id)
Harald Welte9ee48252009-07-23 21:25:08 +020045{
46 struct gsm_trans *trans;
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +020047 struct gsm_network *net = conn->network;
Harald Welte2483f1b2016-06-19 18:06:02 +020048 struct vlr_subscr *vsub = conn->vsub;
Harald Welte9ee48252009-07-23 21:25:08 +020049
50 llist_for_each_entry(trans, &net->trans_list, entry) {
Harald Welte2483f1b2016-06-19 18:06:02 +020051 if (trans->vsub == vsub &&
Harald Welteb8b40732009-07-23 21:58:40 +020052 trans->protocol == proto &&
53 trans->transaction_id == trans_id)
Harald Welte9ee48252009-07-23 21:25:08 +020054 return trans;
55 }
56 return NULL;
57}
58
Harald Welte2483f1b2016-06-19 18:06:02 +020059/*! Find a transaction by call reference
60 * \param[in] net Network in which we should search
61 * \param[in] callref Call Reference of transaction
62 * \returns Matching transaction, if any
63 */
Harald Welte9ee48252009-07-23 21:25:08 +020064struct gsm_trans *trans_find_by_callref(struct gsm_network *net,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020065 uint32_t callref)
Harald Welte9ee48252009-07-23 21:25:08 +020066{
67 struct gsm_trans *trans;
68
69 llist_for_each_entry(trans, &net->trans_list, entry) {
70 if (trans->callref == callref)
71 return trans;
72 }
73 return NULL;
74}
75
Ivan Kluchnikov9bd4fd62015-12-21 12:05:56 +030076/*! Find a transaction by SM-RP-MR (RP Message Reference)
77 * \param[in] conn Connection in which we want to find transaction
78 * \param[in] sm_rp_mr RP Message Reference (see GSM TS 04.11, section 8.2.3)
79 * \returns Matching transaction, NULL otherwise
80 */
81struct gsm_trans *trans_find_by_sm_rp_mr(struct gsm_subscriber_connection *conn,
82 uint8_t sm_rp_mr)
83{
84 struct gsm_network *net = conn->network;
85 struct vlr_subscr *vsub = conn->vsub;
86 struct gsm_trans *trans;
87
88 llist_for_each_entry(trans, &net->trans_list, entry) {
89 if (trans->vsub == vsub &&
90 trans->protocol == GSM48_PDISC_SMS &&
91 trans->sms.sm_rp_mr == sm_rp_mr)
92 return trans;
93 }
94
95 return NULL;
96}
97
Harald Welte2483f1b2016-06-19 18:06:02 +020098/*! Allocate a new transaction and add it to network list
99 * \param[in] net Netwokr in which we allocate transaction
100 * \param[in] subscr Subscriber for which we allocate transaction
101 * \param[in] protocol Protocol (CC/SMS/...)
102 * \param[in] callref Call Reference
103 * \returns Transaction
104 */
Jacob Erlbeckaf792d62014-12-02 14:22:53 +0100105struct gsm_trans *trans_alloc(struct gsm_network *net,
Harald Welte2483f1b2016-06-19 18:06:02 +0200106 struct vlr_subscr *vsub,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200107 uint8_t protocol, uint8_t trans_id,
108 uint32_t callref)
Harald Welte9ee48252009-07-23 21:25:08 +0200109{
110 struct gsm_trans *trans;
111
Benoit Bolseee7cf5e72017-07-05 12:34:18 +0200112 /* a valid subscriber is indispensable */
113 if (vsub == NULL) {
114 LOGP(DCC, LOGL_NOTICE,
115 "unable to alloc transaction, invalid subscriber (NULL)\n");
116 return NULL;
117 }
118
Neels Hofmeyrd1ec1112017-11-22 01:58:00 +0100119 DEBUGP(DCC, "(ti %02x sub %s callref %x) New transaction\n",
120 trans_id, vlr_subscr_name(vsub), callref);
121
Harald Welte9ee48252009-07-23 21:25:08 +0200122 trans = talloc_zero(tall_trans_ctx, struct gsm_trans);
123 if (!trans)
124 return NULL;
125
Harald Welte2483f1b2016-06-19 18:06:02 +0200126 trans->vsub = vlr_subscr_get(vsub);
Harald Welte9ee48252009-07-23 21:25:08 +0200127
128 trans->protocol = protocol;
129 trans->transaction_id = trans_id;
130 trans->callref = callref;
131
Jacob Erlbeckf07c6052014-12-02 11:58:00 +0100132 trans->net = net;
133 llist_add_tail(&trans->entry, &net->trans_list);
Harald Welte9ee48252009-07-23 21:25:08 +0200134
135 return trans;
136}
137
Harald Welte2483f1b2016-06-19 18:06:02 +0200138/*! Release a transaction
139 * \param[in] trans Transaction to be released
140 */
Harald Welte9ee48252009-07-23 21:25:08 +0200141void trans_free(struct gsm_trans *trans)
142{
Neels Hofmeyr6166f292017-11-22 14:33:12 +0100143 enum msc_subscr_conn_use conn_usage_token = MSC_CONN_USE_UNTRACKED;
Neels Hofmeyrd6a769b2018-03-12 23:59:07 +0100144 struct gsm_subscriber_connection *conn;
Neels Hofmeyr6166f292017-11-22 14:33:12 +0100145
Harald Welte9ee48252009-07-23 21:25:08 +0200146 switch (trans->protocol) {
147 case GSM48_PDISC_CC:
148 _gsm48_cc_trans_free(trans);
Neels Hofmeyr6166f292017-11-22 14:33:12 +0100149 conn_usage_token = MSC_CONN_USE_TRANS_CC;
Harald Welte9ee48252009-07-23 21:25:08 +0200150 break;
Harald Welte (local)86b17172009-08-14 14:52:17 +0200151 case GSM48_PDISC_SMS:
152 _gsm411_sms_trans_free(trans);
Neels Hofmeyr6166f292017-11-22 14:33:12 +0100153 conn_usage_token = MSC_CONN_USE_TRANS_SMS;
Harald Welte (local)86b17172009-08-14 14:52:17 +0200154 break;
Vadim Yanitskiy10c64192018-04-17 19:17:11 +0700155 case GSM48_PDISC_NC_SS:
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700156 _gsm911_nc_ss_trans_free(trans);
Vadim Yanitskiy10c64192018-04-17 19:17:11 +0700157 conn_usage_token = MSC_CONN_USE_TRANS_NC_SS;
158 break;
Harald Welte9ee48252009-07-23 21:25:08 +0200159 }
160
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +0200161 if (trans->paging_request) {
162 subscr_remove_request(trans->paging_request);
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +0100163 trans->paging_request = NULL;
Harald Welte9ee48252009-07-23 21:25:08 +0200164 }
165
Harald Welte2483f1b2016-06-19 18:06:02 +0200166 if (trans->vsub) {
167 vlr_subscr_put(trans->vsub);
168 trans->vsub = NULL;
Holger Hans Peter Freyther405824c2012-12-22 18:16:47 +0100169 }
Harald Welte9ee48252009-07-23 21:25:08 +0200170
Neels Hofmeyrd6a769b2018-03-12 23:59:07 +0100171 conn = trans->conn;
Holger Hans Peter Freyther8effcb72013-12-27 18:07:23 +0100172 trans->conn = NULL;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200173 llist_del(&trans->entry);
Harald Welte9ee48252009-07-23 21:25:08 +0200174 talloc_free(trans);
Neels Hofmeyrd6a769b2018-03-12 23:59:07 +0100175
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200176 if (conn)
177 msc_subscr_conn_put(conn, conn_usage_token);
Harald Welte9ee48252009-07-23 21:25:08 +0200178}
179
Harald Welte2483f1b2016-06-19 18:06:02 +0200180/*! allocate an unused transaction ID for the given subscriber
181 * in the given protocol using the ti_flag specified
182 * \param[in] net GSM network
183 * \param[in] subscr Subscriber for which to find ID
184 * \param[in] protocol Protocol for whihc to find ID
185 * \param[in] ti_flag FIXME
186 */
187int trans_assign_trans_id(struct gsm_network *net, struct vlr_subscr *vsub,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200188 uint8_t protocol, uint8_t ti_flag)
Harald Welte9ee48252009-07-23 21:25:08 +0200189{
Harald Welte9ee48252009-07-23 21:25:08 +0200190 struct gsm_trans *trans;
191 unsigned int used_tid_bitmask = 0;
Sylvain Munaut926fcec2009-12-24 13:26:17 +0100192 int i, j, h;
Harald Welte78283ef2009-07-23 21:36:44 +0200193
194 if (ti_flag)
195 ti_flag = 0x8;
Harald Welte9ee48252009-07-23 21:25:08 +0200196
197 /* generate bitmask of already-used TIDs for this (subscr,proto) */
198 llist_for_each_entry(trans, &net->trans_list, entry) {
Harald Welte2483f1b2016-06-19 18:06:02 +0200199 if (trans->vsub != vsub ||
Harald Welte9ee48252009-07-23 21:25:08 +0200200 trans->protocol != protocol ||
201 trans->transaction_id == 0xff)
202 continue;
203 used_tid_bitmask |= (1 << trans->transaction_id);
204 }
205
Sylvain Munaut926fcec2009-12-24 13:26:17 +0100206 /* find a new one, trying to go in a 'circular' pattern */
207 for (h = 6; h > 0; h--)
208 if (used_tid_bitmask & (1 << (h | ti_flag)))
209 break;
Sylvain Munautb9f3dce2009-12-18 18:28:11 +0100210 for (i = 0; i < 7; i++) {
Sylvain Munaut926fcec2009-12-24 13:26:17 +0100211 j = ((h + i) % 7) | ti_flag;
212 if ((used_tid_bitmask & (1 << j)) == 0)
213 return j;
Harald Welte78283ef2009-07-23 21:36:44 +0200214 }
215
216 return -1;
Harald Welte9ee48252009-07-23 21:25:08 +0200217}
Holger Hans Peter Freyther70ae5d32012-11-23 21:33:15 +0100218
Harald Welte2483f1b2016-06-19 18:06:02 +0200219/*! Check if we have any transaction for given connection
220 * \param[in] conn Connection to check
221 * \returns 1 in case there is a transaction, 0 otherwise
222 */
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200223struct gsm_trans *trans_has_conn(const struct gsm_subscriber_connection *conn)
Holger Hans Peter Freyther70ae5d32012-11-23 21:33:15 +0100224{
225 struct gsm_trans *trans;
226
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200227 llist_for_each_entry(trans, &conn->network->trans_list, entry)
Holger Hans Peter Freyther70ae5d32012-11-23 21:33:15 +0100228 if (trans->conn == conn)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200229 return trans;
Holger Hans Peter Freyther70ae5d32012-11-23 21:33:15 +0100230
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200231 return NULL;
Holger Hans Peter Freyther70ae5d32012-11-23 21:33:15 +0100232}
Harald Welte2483f1b2016-06-19 18:06:02 +0200233
234/*! Free all transactions associated with a connection, presumably when the
235 * conn is being closed. The transaction code will inform the CC or SMS
236 * facilities, which will then send the necessary release indications.
237 * \param[in] conn Connection that is going to be closed.
238 */
239void trans_conn_closed(struct gsm_subscriber_connection *conn)
240{
241 struct gsm_trans *trans;
242
243 /* As part of the CC REL_IND the remote leg might be released and this
244 * will trigger another call to trans_free. This is something the llist
245 * macro can not handle and we need to re-iterate the list every time.
246 */
247restart:
248 llist_for_each_entry(trans, &conn->network->trans_list, entry) {
249 if (trans->conn == conn) {
250 trans_free(trans);
251 goto restart;
252 }
253 }
254}