blob: 01d69c24e6150bed2c60448abd4897af1f5b6ae9 [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>
28#include <osmocom/msc/paging.h>
29#include <osmocom/msc/osmo_msc.h>
30#include <osmocom/msc/vlr.h>
Harald Welte9ee48252009-07-23 21:25:08 +020031
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020032void *tall_trans_ctx;
Harald Welte9ee48252009-07-23 21:25:08 +020033
Holger Hans Peter Freyther34e97492009-08-10 07:54:02 +020034void _gsm48_cc_trans_free(struct gsm_trans *trans);
35
Harald Welte2483f1b2016-06-19 18:06:02 +020036/*! Find a transaction in connection for given protocol + transaction ID
37 * \param[in] conn Connection in whihc we want to find transaction
38 * \param[in] proto Protocol of transaction
39 * \param[in] trans_id Transaction ID of transaction
40 * \returns Matching transaction, if any
41 */
Jacob Erlbeckdae1f642014-12-02 14:22:53 +010042struct gsm_trans *trans_find_by_id(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020043 uint8_t proto, uint8_t trans_id)
Harald Welte9ee48252009-07-23 21:25:08 +020044{
45 struct gsm_trans *trans;
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +020046 struct gsm_network *net = conn->network;
Harald Welte2483f1b2016-06-19 18:06:02 +020047 struct vlr_subscr *vsub = conn->vsub;
Harald Welte9ee48252009-07-23 21:25:08 +020048
49 llist_for_each_entry(trans, &net->trans_list, entry) {
Harald Welte2483f1b2016-06-19 18:06:02 +020050 if (trans->vsub == vsub &&
Harald Welteb8b40732009-07-23 21:58:40 +020051 trans->protocol == proto &&
52 trans->transaction_id == trans_id)
Harald Welte9ee48252009-07-23 21:25:08 +020053 return trans;
54 }
55 return NULL;
56}
57
Harald Welte2483f1b2016-06-19 18:06:02 +020058/*! Find a transaction by call reference
59 * \param[in] net Network in which we should search
60 * \param[in] callref Call Reference of transaction
61 * \returns Matching transaction, if any
62 */
Harald Welte9ee48252009-07-23 21:25:08 +020063struct gsm_trans *trans_find_by_callref(struct gsm_network *net,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020064 uint32_t callref)
Harald Welte9ee48252009-07-23 21:25:08 +020065{
66 struct gsm_trans *trans;
67
68 llist_for_each_entry(trans, &net->trans_list, entry) {
69 if (trans->callref == callref)
70 return trans;
71 }
72 return NULL;
73}
74
Harald Welte2483f1b2016-06-19 18:06:02 +020075/*! Allocate a new transaction and add it to network list
76 * \param[in] net Netwokr in which we allocate transaction
77 * \param[in] subscr Subscriber for which we allocate transaction
78 * \param[in] protocol Protocol (CC/SMS/...)
79 * \param[in] callref Call Reference
80 * \returns Transaction
81 */
Jacob Erlbeckaf792d62014-12-02 14:22:53 +010082struct gsm_trans *trans_alloc(struct gsm_network *net,
Harald Welte2483f1b2016-06-19 18:06:02 +020083 struct vlr_subscr *vsub,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020084 uint8_t protocol, uint8_t trans_id,
85 uint32_t callref)
Harald Welte9ee48252009-07-23 21:25:08 +020086{
87 struct gsm_trans *trans;
88
Benoit Bolseee7cf5e72017-07-05 12:34:18 +020089 /* a valid subscriber is indispensable */
90 if (vsub == NULL) {
91 LOGP(DCC, LOGL_NOTICE,
92 "unable to alloc transaction, invalid subscriber (NULL)\n");
93 return NULL;
94 }
95
Neels Hofmeyrd1ec1112017-11-22 01:58:00 +010096 DEBUGP(DCC, "(ti %02x sub %s callref %x) New transaction\n",
97 trans_id, vlr_subscr_name(vsub), callref);
98
Harald Welte9ee48252009-07-23 21:25:08 +020099 trans = talloc_zero(tall_trans_ctx, struct gsm_trans);
100 if (!trans)
101 return NULL;
102
Harald Welte2483f1b2016-06-19 18:06:02 +0200103 trans->vsub = vlr_subscr_get(vsub);
Harald Welte9ee48252009-07-23 21:25:08 +0200104
105 trans->protocol = protocol;
106 trans->transaction_id = trans_id;
107 trans->callref = callref;
108
Jacob Erlbeckf07c6052014-12-02 11:58:00 +0100109 trans->net = net;
110 llist_add_tail(&trans->entry, &net->trans_list);
Harald Welte9ee48252009-07-23 21:25:08 +0200111
112 return trans;
113}
114
Harald Welte2483f1b2016-06-19 18:06:02 +0200115/*! Release a transaction
116 * \param[in] trans Transaction to be released
117 */
Harald Welte9ee48252009-07-23 21:25:08 +0200118void trans_free(struct gsm_trans *trans)
119{
Harald Welte9ee48252009-07-23 21:25:08 +0200120 switch (trans->protocol) {
121 case GSM48_PDISC_CC:
122 _gsm48_cc_trans_free(trans);
123 break;
Harald Welte (local)86b17172009-08-14 14:52:17 +0200124 case GSM48_PDISC_SMS:
125 _gsm411_sms_trans_free(trans);
126 break;
Harald Welte9ee48252009-07-23 21:25:08 +0200127 }
128
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +0200129 if (trans->paging_request) {
130 subscr_remove_request(trans->paging_request);
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +0100131 trans->paging_request = NULL;
Harald Welte9ee48252009-07-23 21:25:08 +0200132 }
133
Harald Welte2483f1b2016-06-19 18:06:02 +0200134 if (trans->vsub) {
135 vlr_subscr_put(trans->vsub);
136 trans->vsub = NULL;
Holger Hans Peter Freyther405824c2012-12-22 18:16:47 +0100137 }
Harald Welte9ee48252009-07-23 21:25:08 +0200138
139 llist_del(&trans->entry);
140
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800141 if (trans->conn)
Harald Welte2483f1b2016-06-19 18:06:02 +0200142 msc_subscr_conn_put(trans->conn);
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800143
Holger Hans Peter Freyther8effcb72013-12-27 18:07:23 +0100144 trans->conn = NULL;
Harald Welte9ee48252009-07-23 21:25:08 +0200145 talloc_free(trans);
146}
147
Harald Welte2483f1b2016-06-19 18:06:02 +0200148/*! allocate an unused transaction ID for the given subscriber
149 * in the given protocol using the ti_flag specified
150 * \param[in] net GSM network
151 * \param[in] subscr Subscriber for which to find ID
152 * \param[in] protocol Protocol for whihc to find ID
153 * \param[in] ti_flag FIXME
154 */
155int trans_assign_trans_id(struct gsm_network *net, struct vlr_subscr *vsub,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200156 uint8_t protocol, uint8_t ti_flag)
Harald Welte9ee48252009-07-23 21:25:08 +0200157{
Harald Welte9ee48252009-07-23 21:25:08 +0200158 struct gsm_trans *trans;
159 unsigned int used_tid_bitmask = 0;
Sylvain Munaut926fcec2009-12-24 13:26:17 +0100160 int i, j, h;
Harald Welte78283ef2009-07-23 21:36:44 +0200161
162 if (ti_flag)
163 ti_flag = 0x8;
Harald Welte9ee48252009-07-23 21:25:08 +0200164
165 /* generate bitmask of already-used TIDs for this (subscr,proto) */
166 llist_for_each_entry(trans, &net->trans_list, entry) {
Harald Welte2483f1b2016-06-19 18:06:02 +0200167 if (trans->vsub != vsub ||
Harald Welte9ee48252009-07-23 21:25:08 +0200168 trans->protocol != protocol ||
169 trans->transaction_id == 0xff)
170 continue;
171 used_tid_bitmask |= (1 << trans->transaction_id);
172 }
173
Sylvain Munaut926fcec2009-12-24 13:26:17 +0100174 /* find a new one, trying to go in a 'circular' pattern */
175 for (h = 6; h > 0; h--)
176 if (used_tid_bitmask & (1 << (h | ti_flag)))
177 break;
Sylvain Munautb9f3dce2009-12-18 18:28:11 +0100178 for (i = 0; i < 7; i++) {
Sylvain Munaut926fcec2009-12-24 13:26:17 +0100179 j = ((h + i) % 7) | ti_flag;
180 if ((used_tid_bitmask & (1 << j)) == 0)
181 return j;
Harald Welte78283ef2009-07-23 21:36:44 +0200182 }
183
184 return -1;
Harald Welte9ee48252009-07-23 21:25:08 +0200185}
Holger Hans Peter Freyther70ae5d32012-11-23 21:33:15 +0100186
Harald Welte2483f1b2016-06-19 18:06:02 +0200187/*! Check if we have any transaction for given connection
188 * \param[in] conn Connection to check
189 * \returns 1 in case there is a transaction, 0 otherwise
190 */
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200191struct gsm_trans *trans_has_conn(const struct gsm_subscriber_connection *conn)
Holger Hans Peter Freyther70ae5d32012-11-23 21:33:15 +0100192{
193 struct gsm_trans *trans;
194
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200195 llist_for_each_entry(trans, &conn->network->trans_list, entry)
Holger Hans Peter Freyther70ae5d32012-11-23 21:33:15 +0100196 if (trans->conn == conn)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200197 return trans;
Holger Hans Peter Freyther70ae5d32012-11-23 21:33:15 +0100198
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200199 return NULL;
Holger Hans Peter Freyther70ae5d32012-11-23 21:33:15 +0100200}
Harald Welte2483f1b2016-06-19 18:06:02 +0200201
202/*! Free all transactions associated with a connection, presumably when the
203 * conn is being closed. The transaction code will inform the CC or SMS
204 * facilities, which will then send the necessary release indications.
205 * \param[in] conn Connection that is going to be closed.
206 */
207void trans_conn_closed(struct gsm_subscriber_connection *conn)
208{
209 struct gsm_trans *trans;
210
211 /* As part of the CC REL_IND the remote leg might be released and this
212 * will trigger another call to trans_free. This is something the llist
213 * macro can not handle and we need to re-iterate the list every time.
214 */
215restart:
216 llist_for_each_entry(trans, &conn->network->trans_list, entry) {
217 if (trans->conn == conn) {
218 trans_free(trans);
219 goto restart;
220 }
221 }
222}