blob: 28da9f37e03ecc3cb893f9f06910fcb096267949 [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);
34
Harald Welte2483f1b2016-06-19 18:06:02 +020035/*! Find a transaction in connection for given protocol + transaction ID
36 * \param[in] conn Connection in whihc we want to find transaction
37 * \param[in] proto Protocol of transaction
38 * \param[in] trans_id Transaction ID of transaction
39 * \returns Matching transaction, if any
40 */
Jacob Erlbeckdae1f642014-12-02 14:22:53 +010041struct gsm_trans *trans_find_by_id(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020042 uint8_t proto, uint8_t trans_id)
Harald Welte9ee48252009-07-23 21:25:08 +020043{
44 struct gsm_trans *trans;
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +020045 struct gsm_network *net = conn->network;
Harald Welte2483f1b2016-06-19 18:06:02 +020046 struct vlr_subscr *vsub = conn->vsub;
Harald Welte9ee48252009-07-23 21:25:08 +020047
48 llist_for_each_entry(trans, &net->trans_list, entry) {
Harald Welte2483f1b2016-06-19 18:06:02 +020049 if (trans->vsub == vsub &&
Harald Welteb8b40732009-07-23 21:58:40 +020050 trans->protocol == proto &&
51 trans->transaction_id == trans_id)
Harald Welte9ee48252009-07-23 21:25:08 +020052 return trans;
53 }
54 return NULL;
55}
56
Harald Welte2483f1b2016-06-19 18:06:02 +020057/*! Find a transaction by call reference
58 * \param[in] net Network in which we should search
59 * \param[in] callref Call Reference of transaction
60 * \returns Matching transaction, if any
61 */
Harald Welte9ee48252009-07-23 21:25:08 +020062struct gsm_trans *trans_find_by_callref(struct gsm_network *net,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020063 uint32_t callref)
Harald Welte9ee48252009-07-23 21:25:08 +020064{
65 struct gsm_trans *trans;
66
67 llist_for_each_entry(trans, &net->trans_list, entry) {
68 if (trans->callref == callref)
69 return trans;
70 }
71 return NULL;
72}
73
Harald Welte2483f1b2016-06-19 18:06:02 +020074/*! Allocate a new transaction and add it to network list
75 * \param[in] net Netwokr in which we allocate transaction
76 * \param[in] subscr Subscriber for which we allocate transaction
77 * \param[in] protocol Protocol (CC/SMS/...)
78 * \param[in] callref Call Reference
79 * \returns Transaction
80 */
Jacob Erlbeckaf792d62014-12-02 14:22:53 +010081struct gsm_trans *trans_alloc(struct gsm_network *net,
Harald Welte2483f1b2016-06-19 18:06:02 +020082 struct vlr_subscr *vsub,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020083 uint8_t protocol, uint8_t trans_id,
84 uint32_t callref)
Harald Welte9ee48252009-07-23 21:25:08 +020085{
86 struct gsm_trans *trans;
87
Benoit Bolseee7cf5e72017-07-05 12:34:18 +020088 /* a valid subscriber is indispensable */
89 if (vsub == NULL) {
90 LOGP(DCC, LOGL_NOTICE,
91 "unable to alloc transaction, invalid subscriber (NULL)\n");
92 return NULL;
93 }
94
Neels Hofmeyrd1ec1112017-11-22 01:58:00 +010095 DEBUGP(DCC, "(ti %02x sub %s callref %x) New transaction\n",
96 trans_id, vlr_subscr_name(vsub), callref);
97
Harald Welte9ee48252009-07-23 21:25:08 +020098 trans = talloc_zero(tall_trans_ctx, struct gsm_trans);
99 if (!trans)
100 return NULL;
101
Harald Welte2483f1b2016-06-19 18:06:02 +0200102 trans->vsub = vlr_subscr_get(vsub);
Harald Welte9ee48252009-07-23 21:25:08 +0200103
104 trans->protocol = protocol;
105 trans->transaction_id = trans_id;
106 trans->callref = callref;
107
Jacob Erlbeckf07c6052014-12-02 11:58:00 +0100108 trans->net = net;
109 llist_add_tail(&trans->entry, &net->trans_list);
Harald Welte9ee48252009-07-23 21:25:08 +0200110
111 return trans;
112}
113
Harald Welte2483f1b2016-06-19 18:06:02 +0200114/*! Release a transaction
115 * \param[in] trans Transaction to be released
116 */
Harald Welte9ee48252009-07-23 21:25:08 +0200117void trans_free(struct gsm_trans *trans)
118{
Neels Hofmeyr6166f292017-11-22 14:33:12 +0100119 enum msc_subscr_conn_use conn_usage_token = MSC_CONN_USE_UNTRACKED;
Neels Hofmeyrd6a769b2018-03-12 23:59:07 +0100120 struct gsm_subscriber_connection *conn;
Neels Hofmeyr6166f292017-11-22 14:33:12 +0100121
Harald Welte9ee48252009-07-23 21:25:08 +0200122 switch (trans->protocol) {
123 case GSM48_PDISC_CC:
124 _gsm48_cc_trans_free(trans);
Neels Hofmeyr6166f292017-11-22 14:33:12 +0100125 conn_usage_token = MSC_CONN_USE_TRANS_CC;
Harald Welte9ee48252009-07-23 21:25:08 +0200126 break;
Harald Welte (local)86b17172009-08-14 14:52:17 +0200127 case GSM48_PDISC_SMS:
128 _gsm411_sms_trans_free(trans);
Neels Hofmeyr6166f292017-11-22 14:33:12 +0100129 conn_usage_token = MSC_CONN_USE_TRANS_SMS;
Harald Welte (local)86b17172009-08-14 14:52:17 +0200130 break;
Harald Welte9ee48252009-07-23 21:25:08 +0200131 }
132
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +0200133 if (trans->paging_request) {
134 subscr_remove_request(trans->paging_request);
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +0100135 trans->paging_request = NULL;
Harald Welte9ee48252009-07-23 21:25:08 +0200136 }
137
Harald Welte2483f1b2016-06-19 18:06:02 +0200138 if (trans->vsub) {
139 vlr_subscr_put(trans->vsub);
140 trans->vsub = NULL;
Holger Hans Peter Freyther405824c2012-12-22 18:16:47 +0100141 }
Harald Welte9ee48252009-07-23 21:25:08 +0200142
Neels Hofmeyrd6a769b2018-03-12 23:59:07 +0100143 conn = trans->conn;
Holger Hans Peter Freyther8effcb72013-12-27 18:07:23 +0100144 trans->conn = NULL;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200145 llist_del(&trans->entry);
Harald Welte9ee48252009-07-23 21:25:08 +0200146 talloc_free(trans);
Neels Hofmeyrd6a769b2018-03-12 23:59:07 +0100147
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200148 if (conn)
149 msc_subscr_conn_put(conn, conn_usage_token);
Harald Welte9ee48252009-07-23 21:25:08 +0200150}
151
Harald Welte2483f1b2016-06-19 18:06:02 +0200152/*! allocate an unused transaction ID for the given subscriber
153 * in the given protocol using the ti_flag specified
154 * \param[in] net GSM network
155 * \param[in] subscr Subscriber for which to find ID
156 * \param[in] protocol Protocol for whihc to find ID
157 * \param[in] ti_flag FIXME
158 */
159int trans_assign_trans_id(struct gsm_network *net, struct vlr_subscr *vsub,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200160 uint8_t protocol, uint8_t ti_flag)
Harald Welte9ee48252009-07-23 21:25:08 +0200161{
Harald Welte9ee48252009-07-23 21:25:08 +0200162 struct gsm_trans *trans;
163 unsigned int used_tid_bitmask = 0;
Sylvain Munaut926fcec2009-12-24 13:26:17 +0100164 int i, j, h;
Harald Welte78283ef2009-07-23 21:36:44 +0200165
166 if (ti_flag)
167 ti_flag = 0x8;
Harald Welte9ee48252009-07-23 21:25:08 +0200168
169 /* generate bitmask of already-used TIDs for this (subscr,proto) */
170 llist_for_each_entry(trans, &net->trans_list, entry) {
Harald Welte2483f1b2016-06-19 18:06:02 +0200171 if (trans->vsub != vsub ||
Harald Welte9ee48252009-07-23 21:25:08 +0200172 trans->protocol != protocol ||
173 trans->transaction_id == 0xff)
174 continue;
175 used_tid_bitmask |= (1 << trans->transaction_id);
176 }
177
Sylvain Munaut926fcec2009-12-24 13:26:17 +0100178 /* find a new one, trying to go in a 'circular' pattern */
179 for (h = 6; h > 0; h--)
180 if (used_tid_bitmask & (1 << (h | ti_flag)))
181 break;
Sylvain Munautb9f3dce2009-12-18 18:28:11 +0100182 for (i = 0; i < 7; i++) {
Sylvain Munaut926fcec2009-12-24 13:26:17 +0100183 j = ((h + i) % 7) | ti_flag;
184 if ((used_tid_bitmask & (1 << j)) == 0)
185 return j;
Harald Welte78283ef2009-07-23 21:36:44 +0200186 }
187
188 return -1;
Harald Welte9ee48252009-07-23 21:25:08 +0200189}
Holger Hans Peter Freyther70ae5d32012-11-23 21:33:15 +0100190
Harald Welte2483f1b2016-06-19 18:06:02 +0200191/*! Check if we have any transaction for given connection
192 * \param[in] conn Connection to check
193 * \returns 1 in case there is a transaction, 0 otherwise
194 */
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200195struct gsm_trans *trans_has_conn(const struct gsm_subscriber_connection *conn)
Holger Hans Peter Freyther70ae5d32012-11-23 21:33:15 +0100196{
197 struct gsm_trans *trans;
198
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200199 llist_for_each_entry(trans, &conn->network->trans_list, entry)
Holger Hans Peter Freyther70ae5d32012-11-23 21:33:15 +0100200 if (trans->conn == conn)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200201 return trans;
Holger Hans Peter Freyther70ae5d32012-11-23 21:33:15 +0100202
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200203 return NULL;
Holger Hans Peter Freyther70ae5d32012-11-23 21:33:15 +0100204}
Harald Welte2483f1b2016-06-19 18:06:02 +0200205
206/*! Free all transactions associated with a connection, presumably when the
207 * conn is being closed. The transaction code will inform the CC or SMS
208 * facilities, which will then send the necessary release indications.
209 * \param[in] conn Connection that is going to be closed.
210 */
211void trans_conn_closed(struct gsm_subscriber_connection *conn)
212{
213 struct gsm_trans *trans;
214
215 /* As part of the CC REL_IND the remote leg might be released and this
216 * will trigger another call to trans_free. This is something the llist
217 * macro can not handle and we need to re-iterate the list every time.
218 */
219restart:
220 llist_for_each_entry(trans, &conn->network->trans_list, entry) {
221 if (trans->conn == conn) {
222 trans_free(trans);
223 goto restart;
224 }
225 }
226}