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/vlr.h> |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 29 | |
Harald Welte (local) | d19e58b | 2009-08-15 02:30:58 +0200 | [diff] [blame] | 30 | void *tall_trans_ctx; |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 31 | |
Holger Hans Peter Freyther | 34e9749 | 2009-08-10 07:54:02 +0200 | [diff] [blame] | 32 | void _gsm48_cc_trans_free(struct gsm_trans *trans); |
Vadim Yanitskiy | c350c09 | 2018-11-21 20:51:51 +0700 | [diff] [blame] | 33 | void _gsm411_sms_trans_free(struct gsm_trans *trans); |
Vadim Yanitskiy | f2f83b0 | 2018-06-17 21:09:28 +0700 | [diff] [blame] | 34 | void _gsm911_nc_ss_trans_free(struct gsm_trans *trans); |
Holger Hans Peter Freyther | 34e9749 | 2009-08-10 07:54:02 +0200 | [diff] [blame] | 35 | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 36 | /*! Find a transaction in connection for given protocol + transaction ID |
Vadim Yanitskiy | 380b219 | 2018-11-27 15:14:31 +0700 | [diff] [blame] | 37 | * \param[in] conn Connection in which we want to find transaction |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 38 | * \param[in] proto Protocol of transaction |
| 39 | * \param[in] trans_id Transaction ID of transaction |
| 40 | * \returns Matching transaction, if any |
| 41 | */ |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 42 | struct gsm_trans *trans_find_by_id(struct ran_conn *conn, |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 43 | uint8_t proto, uint8_t trans_id) |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 44 | { |
| 45 | struct gsm_trans *trans; |
Neels Hofmeyr | a9f2bb5 | 2016-05-09 21:09:47 +0200 | [diff] [blame] | 46 | struct gsm_network *net = conn->network; |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 47 | struct vlr_subscr *vsub = conn->vsub; |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 48 | |
| 49 | llist_for_each_entry(trans, &net->trans_list, entry) { |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 50 | if (trans->vsub == vsub && |
Harald Welte | b8b4073 | 2009-07-23 21:58:40 +0200 | [diff] [blame] | 51 | trans->protocol == proto && |
| 52 | trans->transaction_id == trans_id) |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 53 | return trans; |
| 54 | } |
| 55 | return NULL; |
| 56 | } |
| 57 | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 58 | /*! 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 Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 63 | 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] | 64 | uint32_t callref) |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 65 | { |
| 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 | |
Ivan Kluchnikov | 9bd4fd6 | 2015-12-21 12:05:56 +0300 | [diff] [blame] | 75 | /*! Find a transaction by SM-RP-MR (RP Message Reference) |
| 76 | * \param[in] conn Connection in which we want to find transaction |
| 77 | * \param[in] sm_rp_mr RP Message Reference (see GSM TS 04.11, section 8.2.3) |
| 78 | * \returns Matching transaction, NULL otherwise |
| 79 | */ |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 80 | struct gsm_trans *trans_find_by_sm_rp_mr(struct ran_conn *conn, |
Ivan Kluchnikov | 9bd4fd6 | 2015-12-21 12:05:56 +0300 | [diff] [blame] | 81 | uint8_t sm_rp_mr) |
| 82 | { |
| 83 | struct gsm_network *net = conn->network; |
| 84 | struct vlr_subscr *vsub = conn->vsub; |
| 85 | struct gsm_trans *trans; |
| 86 | |
| 87 | llist_for_each_entry(trans, &net->trans_list, entry) { |
| 88 | if (trans->vsub == vsub && |
| 89 | trans->protocol == GSM48_PDISC_SMS && |
| 90 | trans->sms.sm_rp_mr == sm_rp_mr) |
| 91 | return trans; |
| 92 | } |
| 93 | |
| 94 | return NULL; |
| 95 | } |
| 96 | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 97 | /*! Allocate a new transaction and add it to network list |
| 98 | * \param[in] net Netwokr in which we allocate transaction |
| 99 | * \param[in] subscr Subscriber for which we allocate transaction |
| 100 | * \param[in] protocol Protocol (CC/SMS/...) |
| 101 | * \param[in] callref Call Reference |
| 102 | * \returns Transaction |
| 103 | */ |
Jacob Erlbeck | af792d6 | 2014-12-02 14:22:53 +0100 | [diff] [blame] | 104 | struct gsm_trans *trans_alloc(struct gsm_network *net, |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 105 | struct vlr_subscr *vsub, |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 106 | uint8_t protocol, uint8_t trans_id, |
| 107 | uint32_t callref) |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 108 | { |
| 109 | struct gsm_trans *trans; |
| 110 | |
Benoit Bolsee | e7cf5e7 | 2017-07-05 12:34:18 +0200 | [diff] [blame] | 111 | /* a valid subscriber is indispensable */ |
| 112 | if (vsub == NULL) { |
| 113 | LOGP(DCC, LOGL_NOTICE, |
| 114 | "unable to alloc transaction, invalid subscriber (NULL)\n"); |
| 115 | return NULL; |
| 116 | } |
| 117 | |
Neels Hofmeyr | d1ec111 | 2017-11-22 01:58:00 +0100 | [diff] [blame] | 118 | DEBUGP(DCC, "(ti %02x sub %s callref %x) New transaction\n", |
| 119 | trans_id, vlr_subscr_name(vsub), callref); |
| 120 | |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 121 | trans = talloc_zero(tall_trans_ctx, struct gsm_trans); |
| 122 | if (!trans) |
| 123 | return NULL; |
| 124 | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 125 | trans->vsub = vlr_subscr_get(vsub); |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 126 | |
| 127 | trans->protocol = protocol; |
| 128 | trans->transaction_id = trans_id; |
| 129 | trans->callref = callref; |
| 130 | |
Jacob Erlbeck | f07c605 | 2014-12-02 11:58:00 +0100 | [diff] [blame] | 131 | trans->net = net; |
| 132 | llist_add_tail(&trans->entry, &net->trans_list); |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 133 | |
| 134 | return trans; |
| 135 | } |
| 136 | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 137 | /*! Release a transaction |
| 138 | * \param[in] trans Transaction to be released |
| 139 | */ |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 140 | void trans_free(struct gsm_trans *trans) |
| 141 | { |
Neels Hofmeyr | 3c20a5e | 2018-11-30 01:08:36 +0100 | [diff] [blame] | 142 | enum ran_conn_use conn_usage_token = RAN_CONN_USE_UNTRACKED; |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 143 | struct ran_conn *conn; |
Neels Hofmeyr | 6166f29 | 2017-11-22 14:33:12 +0100 | [diff] [blame] | 144 | |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 145 | switch (trans->protocol) { |
| 146 | case GSM48_PDISC_CC: |
| 147 | _gsm48_cc_trans_free(trans); |
Neels Hofmeyr | 3c20a5e | 2018-11-30 01:08:36 +0100 | [diff] [blame] | 148 | conn_usage_token = RAN_CONN_USE_TRANS_CC; |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 149 | break; |
Harald Welte (local) | 86b1717 | 2009-08-14 14:52:17 +0200 | [diff] [blame] | 150 | case GSM48_PDISC_SMS: |
| 151 | _gsm411_sms_trans_free(trans); |
Neels Hofmeyr | 3c20a5e | 2018-11-30 01:08:36 +0100 | [diff] [blame] | 152 | conn_usage_token = RAN_CONN_USE_TRANS_SMS; |
Harald Welte (local) | 86b1717 | 2009-08-14 14:52:17 +0200 | [diff] [blame] | 153 | break; |
Vadim Yanitskiy | 10c6419 | 2018-04-17 19:17:11 +0700 | [diff] [blame] | 154 | case GSM48_PDISC_NC_SS: |
Vadim Yanitskiy | f2f83b0 | 2018-06-17 21:09:28 +0700 | [diff] [blame] | 155 | _gsm911_nc_ss_trans_free(trans); |
Neels Hofmeyr | 3c20a5e | 2018-11-30 01:08:36 +0100 | [diff] [blame] | 156 | conn_usage_token = RAN_CONN_USE_TRANS_NC_SS; |
Vadim Yanitskiy | 10c6419 | 2018-04-17 19:17:11 +0700 | [diff] [blame] | 157 | break; |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 158 | } |
| 159 | |
Holger Hans Peter Freyther | d6d7aff | 2015-04-06 12:03:45 +0200 | [diff] [blame] | 160 | if (trans->paging_request) { |
| 161 | subscr_remove_request(trans->paging_request); |
Holger Hans Peter Freyther | 49b3ed2 | 2010-12-29 17:09:07 +0100 | [diff] [blame] | 162 | trans->paging_request = NULL; |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 163 | } |
| 164 | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 165 | if (trans->vsub) { |
| 166 | vlr_subscr_put(trans->vsub); |
| 167 | trans->vsub = NULL; |
Holger Hans Peter Freyther | 405824c | 2012-12-22 18:16:47 +0100 | [diff] [blame] | 168 | } |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 169 | |
Neels Hofmeyr | d6a769b | 2018-03-12 23:59:07 +0100 | [diff] [blame] | 170 | conn = trans->conn; |
Holger Hans Peter Freyther | 8effcb7 | 2013-12-27 18:07:23 +0100 | [diff] [blame] | 171 | trans->conn = NULL; |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 172 | llist_del(&trans->entry); |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 173 | talloc_free(trans); |
Neels Hofmeyr | d6a769b | 2018-03-12 23:59:07 +0100 | [diff] [blame] | 174 | |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 175 | if (conn) |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 176 | ran_conn_put(conn, conn_usage_token); |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 177 | } |
| 178 | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 179 | /*! allocate an unused transaction ID for the given subscriber |
| 180 | * in the given protocol using the ti_flag specified |
| 181 | * \param[in] net GSM network |
| 182 | * \param[in] subscr Subscriber for which to find ID |
| 183 | * \param[in] protocol Protocol for whihc to find ID |
| 184 | * \param[in] ti_flag FIXME |
| 185 | */ |
| 186 | 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] | 187 | uint8_t protocol, uint8_t ti_flag) |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 188 | { |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 189 | struct gsm_trans *trans; |
| 190 | unsigned int used_tid_bitmask = 0; |
Sylvain Munaut | 926fcec | 2009-12-24 13:26:17 +0100 | [diff] [blame] | 191 | int i, j, h; |
Harald Welte | 78283ef | 2009-07-23 21:36:44 +0200 | [diff] [blame] | 192 | |
| 193 | if (ti_flag) |
| 194 | ti_flag = 0x8; |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 195 | |
| 196 | /* generate bitmask of already-used TIDs for this (subscr,proto) */ |
| 197 | llist_for_each_entry(trans, &net->trans_list, entry) { |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 198 | if (trans->vsub != vsub || |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 199 | trans->protocol != protocol || |
| 200 | trans->transaction_id == 0xff) |
| 201 | continue; |
| 202 | used_tid_bitmask |= (1 << trans->transaction_id); |
| 203 | } |
| 204 | |
Sylvain Munaut | 926fcec | 2009-12-24 13:26:17 +0100 | [diff] [blame] | 205 | /* find a new one, trying to go in a 'circular' pattern */ |
| 206 | for (h = 6; h > 0; h--) |
| 207 | if (used_tid_bitmask & (1 << (h | ti_flag))) |
| 208 | break; |
Sylvain Munaut | b9f3dce | 2009-12-18 18:28:11 +0100 | [diff] [blame] | 209 | for (i = 0; i < 7; i++) { |
Sylvain Munaut | 926fcec | 2009-12-24 13:26:17 +0100 | [diff] [blame] | 210 | j = ((h + i) % 7) | ti_flag; |
| 211 | if ((used_tid_bitmask & (1 << j)) == 0) |
| 212 | return j; |
Harald Welte | 78283ef | 2009-07-23 21:36:44 +0200 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | return -1; |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 216 | } |
Holger Hans Peter Freyther | 70ae5d3 | 2012-11-23 21:33:15 +0100 | [diff] [blame] | 217 | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 218 | /*! Check if we have any transaction for given connection |
| 219 | * \param[in] conn Connection to check |
| 220 | * \returns 1 in case there is a transaction, 0 otherwise |
| 221 | */ |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 222 | struct gsm_trans *trans_has_conn(const struct ran_conn *conn) |
Holger Hans Peter Freyther | 70ae5d3 | 2012-11-23 21:33:15 +0100 | [diff] [blame] | 223 | { |
| 224 | struct gsm_trans *trans; |
| 225 | |
Neels Hofmeyr | a9f2bb5 | 2016-05-09 21:09:47 +0200 | [diff] [blame] | 226 | llist_for_each_entry(trans, &conn->network->trans_list, entry) |
Holger Hans Peter Freyther | 70ae5d3 | 2012-11-23 21:33:15 +0100 | [diff] [blame] | 227 | if (trans->conn == conn) |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 228 | return trans; |
Holger Hans Peter Freyther | 70ae5d3 | 2012-11-23 21:33:15 +0100 | [diff] [blame] | 229 | |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 230 | return NULL; |
Holger Hans Peter Freyther | 70ae5d3 | 2012-11-23 21:33:15 +0100 | [diff] [blame] | 231 | } |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 232 | |
| 233 | /*! Free all transactions associated with a connection, presumably when the |
| 234 | * conn is being closed. The transaction code will inform the CC or SMS |
| 235 | * facilities, which will then send the necessary release indications. |
| 236 | * \param[in] conn Connection that is going to be closed. |
| 237 | */ |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 238 | void trans_conn_closed(struct ran_conn *conn) |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 239 | { |
| 240 | struct gsm_trans *trans; |
| 241 | |
| 242 | /* As part of the CC REL_IND the remote leg might be released and this |
| 243 | * will trigger another call to trans_free. This is something the llist |
| 244 | * macro can not handle and we need to re-iterate the list every time. |
| 245 | */ |
| 246 | restart: |
| 247 | llist_for_each_entry(trans, &conn->network->trans_list, entry) { |
| 248 | if (trans->conn == conn) { |
| 249 | trans_free(trans); |
| 250 | goto restart; |
| 251 | } |
| 252 | } |
| 253 | } |