Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 1 | /* 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 Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 7 | * 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 Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 9 | * (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 Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 14 | * GNU Affero General Public License for more details. |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 15 | * |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 16 | * 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 Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 18 | * |
| 19 | */ |
| 20 | |
Neels Hofmeyr | 9084396 | 2017-09-04 15:04:35 +0200 | [diff] [blame] | 21 | #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 Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 25 | #include <osmocom/core/talloc.h> |
Neels Hofmeyr | 9084396 | 2017-09-04 15:04:35 +0200 | [diff] [blame] | 26 | #include <osmocom/msc/gsm_04_08.h> |
| 27 | #include <osmocom/msc/mncc.h> |
Neels Hofmeyr | 9084396 | 2017-09-04 15:04:35 +0200 | [diff] [blame] | 28 | #include <osmocom/msc/osmo_msc.h> |
| 29 | #include <osmocom/msc/vlr.h> |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 30 | |
Harald Welte (local) | d19e58b | 2009-08-15 02:30:58 +0200 | [diff] [blame] | 31 | void *tall_trans_ctx; |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 32 | |
Holger Hans Peter Freyther | 34e9749 | 2009-08-10 07:54:02 +0200 | [diff] [blame] | 33 | void _gsm48_cc_trans_free(struct gsm_trans *trans); |
| 34 | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 35 | /*! 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 Erlbeck | dae1f64 | 2014-12-02 14:22:53 +0100 | [diff] [blame] | 41 | struct gsm_trans *trans_find_by_id(struct gsm_subscriber_connection *conn, |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 42 | uint8_t proto, uint8_t trans_id) |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 43 | { |
| 44 | struct gsm_trans *trans; |
Neels Hofmeyr | a9f2bb5 | 2016-05-09 21:09:47 +0200 | [diff] [blame] | 45 | struct gsm_network *net = conn->network; |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 46 | struct vlr_subscr *vsub = conn->vsub; |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 47 | |
| 48 | llist_for_each_entry(trans, &net->trans_list, entry) { |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 49 | if (trans->vsub == vsub && |
Harald Welte | b8b4073 | 2009-07-23 21:58:40 +0200 | [diff] [blame] | 50 | trans->protocol == proto && |
| 51 | trans->transaction_id == trans_id) |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 52 | return trans; |
| 53 | } |
| 54 | return NULL; |
| 55 | } |
| 56 | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 57 | /*! 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 Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 62 | struct gsm_trans *trans_find_by_callref(struct gsm_network *net, |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 63 | uint32_t callref) |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 64 | { |
| 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 Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 74 | /*! 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 Erlbeck | af792d6 | 2014-12-02 14:22:53 +0100 | [diff] [blame] | 81 | struct gsm_trans *trans_alloc(struct gsm_network *net, |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 82 | struct vlr_subscr *vsub, |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 83 | uint8_t protocol, uint8_t trans_id, |
| 84 | uint32_t callref) |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 85 | { |
| 86 | struct gsm_trans *trans; |
| 87 | |
Benoit Bolsee | e7cf5e7 | 2017-07-05 12:34:18 +0200 | [diff] [blame] | 88 | /* 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 Hofmeyr | d1ec111 | 2017-11-22 01:58:00 +0100 | [diff] [blame] | 95 | DEBUGP(DCC, "(ti %02x sub %s callref %x) New transaction\n", |
| 96 | trans_id, vlr_subscr_name(vsub), callref); |
| 97 | |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 98 | trans = talloc_zero(tall_trans_ctx, struct gsm_trans); |
| 99 | if (!trans) |
| 100 | return NULL; |
| 101 | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 102 | trans->vsub = vlr_subscr_get(vsub); |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 103 | |
| 104 | trans->protocol = protocol; |
| 105 | trans->transaction_id = trans_id; |
| 106 | trans->callref = callref; |
| 107 | |
Jacob Erlbeck | f07c605 | 2014-12-02 11:58:00 +0100 | [diff] [blame] | 108 | trans->net = net; |
| 109 | llist_add_tail(&trans->entry, &net->trans_list); |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 110 | |
| 111 | return trans; |
| 112 | } |
| 113 | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 114 | /*! Release a transaction |
| 115 | * \param[in] trans Transaction to be released |
| 116 | */ |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 117 | void trans_free(struct gsm_trans *trans) |
| 118 | { |
Neels Hofmeyr | 6166f29 | 2017-11-22 14:33:12 +0100 | [diff] [blame] | 119 | enum msc_subscr_conn_use conn_usage_token = MSC_CONN_USE_UNTRACKED; |
| 120 | |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 121 | switch (trans->protocol) { |
| 122 | case GSM48_PDISC_CC: |
| 123 | _gsm48_cc_trans_free(trans); |
Neels Hofmeyr | 5c56bc5 | 2018-03-13 18:03:15 +0100 | [diff] [blame] | 124 | if (osmo_timer_pending(&trans->cc.timer)) { |
| 125 | LOGP(DCC, LOGL_ERROR, |
| 126 | "%s Timer 0x%x is still running while discarding transaction" |
| 127 | " -- this is a bug: we were still expecting a response but" |
| 128 | " are freeing the transaction anyway\n", |
| 129 | vlr_subscr_name(trans->conn->vsub), trans->cc.Tcurrent); |
| 130 | osmo_timer_del(&trans->cc.timer); |
| 131 | trans->cc.Tcurrent = 0; |
| 132 | } |
Neels Hofmeyr | 6166f29 | 2017-11-22 14:33:12 +0100 | [diff] [blame] | 133 | conn_usage_token = MSC_CONN_USE_TRANS_CC; |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 134 | break; |
Harald Welte (local) | 86b1717 | 2009-08-14 14:52:17 +0200 | [diff] [blame] | 135 | case GSM48_PDISC_SMS: |
| 136 | _gsm411_sms_trans_free(trans); |
Neels Hofmeyr | 6166f29 | 2017-11-22 14:33:12 +0100 | [diff] [blame] | 137 | conn_usage_token = MSC_CONN_USE_TRANS_SMS; |
Harald Welte (local) | 86b1717 | 2009-08-14 14:52:17 +0200 | [diff] [blame] | 138 | break; |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 139 | } |
| 140 | |
Holger Hans Peter Freyther | d6d7aff | 2015-04-06 12:03:45 +0200 | [diff] [blame] | 141 | if (trans->paging_request) { |
| 142 | subscr_remove_request(trans->paging_request); |
Holger Hans Peter Freyther | 49b3ed2 | 2010-12-29 17:09:07 +0100 | [diff] [blame] | 143 | trans->paging_request = NULL; |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 144 | } |
| 145 | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 146 | if (trans->vsub) { |
| 147 | vlr_subscr_put(trans->vsub); |
| 148 | trans->vsub = NULL; |
Holger Hans Peter Freyther | 405824c | 2012-12-22 18:16:47 +0100 | [diff] [blame] | 149 | } |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 150 | |
| 151 | llist_del(&trans->entry); |
| 152 | |
Holger Hans Peter Freyther | 4049455 | 2010-06-28 17:09:29 +0800 | [diff] [blame] | 153 | if (trans->conn) |
Neels Hofmeyr | 6166f29 | 2017-11-22 14:33:12 +0100 | [diff] [blame] | 154 | msc_subscr_conn_put(trans->conn, conn_usage_token); |
Holger Hans Peter Freyther | 4049455 | 2010-06-28 17:09:29 +0800 | [diff] [blame] | 155 | |
Holger Hans Peter Freyther | 8effcb7 | 2013-12-27 18:07:23 +0100 | [diff] [blame] | 156 | trans->conn = NULL; |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 157 | talloc_free(trans); |
| 158 | } |
| 159 | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 160 | /*! allocate an unused transaction ID for the given subscriber |
| 161 | * in the given protocol using the ti_flag specified |
| 162 | * \param[in] net GSM network |
| 163 | * \param[in] subscr Subscriber for which to find ID |
| 164 | * \param[in] protocol Protocol for whihc to find ID |
| 165 | * \param[in] ti_flag FIXME |
| 166 | */ |
| 167 | int trans_assign_trans_id(struct gsm_network *net, struct vlr_subscr *vsub, |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 168 | uint8_t protocol, uint8_t ti_flag) |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 169 | { |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 170 | struct gsm_trans *trans; |
| 171 | unsigned int used_tid_bitmask = 0; |
Sylvain Munaut | 926fcec | 2009-12-24 13:26:17 +0100 | [diff] [blame] | 172 | int i, j, h; |
Harald Welte | 78283ef | 2009-07-23 21:36:44 +0200 | [diff] [blame] | 173 | |
| 174 | if (ti_flag) |
| 175 | ti_flag = 0x8; |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 176 | |
| 177 | /* generate bitmask of already-used TIDs for this (subscr,proto) */ |
| 178 | llist_for_each_entry(trans, &net->trans_list, entry) { |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 179 | if (trans->vsub != vsub || |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 180 | trans->protocol != protocol || |
| 181 | trans->transaction_id == 0xff) |
| 182 | continue; |
| 183 | used_tid_bitmask |= (1 << trans->transaction_id); |
| 184 | } |
| 185 | |
Sylvain Munaut | 926fcec | 2009-12-24 13:26:17 +0100 | [diff] [blame] | 186 | /* find a new one, trying to go in a 'circular' pattern */ |
| 187 | for (h = 6; h > 0; h--) |
| 188 | if (used_tid_bitmask & (1 << (h | ti_flag))) |
| 189 | break; |
Sylvain Munaut | b9f3dce | 2009-12-18 18:28:11 +0100 | [diff] [blame] | 190 | for (i = 0; i < 7; i++) { |
Sylvain Munaut | 926fcec | 2009-12-24 13:26:17 +0100 | [diff] [blame] | 191 | j = ((h + i) % 7) | ti_flag; |
| 192 | if ((used_tid_bitmask & (1 << j)) == 0) |
| 193 | return j; |
Harald Welte | 78283ef | 2009-07-23 21:36:44 +0200 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | return -1; |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 197 | } |
Holger Hans Peter Freyther | 70ae5d3 | 2012-11-23 21:33:15 +0100 | [diff] [blame] | 198 | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 199 | /*! Check if we have any transaction for given connection |
| 200 | * \param[in] conn Connection to check |
| 201 | * \returns 1 in case there is a transaction, 0 otherwise |
| 202 | */ |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 203 | struct gsm_trans *trans_has_conn(const struct gsm_subscriber_connection *conn) |
Holger Hans Peter Freyther | 70ae5d3 | 2012-11-23 21:33:15 +0100 | [diff] [blame] | 204 | { |
| 205 | struct gsm_trans *trans; |
| 206 | |
Neels Hofmeyr | a9f2bb5 | 2016-05-09 21:09:47 +0200 | [diff] [blame] | 207 | llist_for_each_entry(trans, &conn->network->trans_list, entry) |
Holger Hans Peter Freyther | 70ae5d3 | 2012-11-23 21:33:15 +0100 | [diff] [blame] | 208 | if (trans->conn == conn) |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 209 | return trans; |
Holger Hans Peter Freyther | 70ae5d3 | 2012-11-23 21:33:15 +0100 | [diff] [blame] | 210 | |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 211 | return NULL; |
Holger Hans Peter Freyther | 70ae5d3 | 2012-11-23 21:33:15 +0100 | [diff] [blame] | 212 | } |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 213 | |
| 214 | /*! Free all transactions associated with a connection, presumably when the |
| 215 | * conn is being closed. The transaction code will inform the CC or SMS |
| 216 | * facilities, which will then send the necessary release indications. |
| 217 | * \param[in] conn Connection that is going to be closed. |
| 218 | */ |
| 219 | void trans_conn_closed(struct gsm_subscriber_connection *conn) |
| 220 | { |
| 221 | struct gsm_trans *trans; |
| 222 | |
| 223 | /* As part of the CC REL_IND the remote leg might be released and this |
| 224 | * will trigger another call to trans_free. This is something the llist |
| 225 | * macro can not handle and we need to re-iterate the list every time. |
| 226 | */ |
| 227 | restart: |
| 228 | llist_for_each_entry(trans, &conn->network->trans_list, entry) { |
| 229 | if (trans->conn == conn) { |
| 230 | trans_free(trans); |
| 231 | goto restart; |
| 232 | } |
| 233 | } |
| 234 | } |