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 | |
| 21 | #include <openbsc/transaction.h> |
| 22 | #include <openbsc/gsm_data.h> |
| 23 | #include <openbsc/mncc.h> |
| 24 | #include <openbsc/debug.h> |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 25 | #include <osmocom/core/talloc.h> |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 26 | #include <openbsc/gsm_04_08.h> |
| 27 | #include <openbsc/mncc.h> |
| 28 | #include <openbsc/paging.h> |
Holger Hans Peter Freyther | 88519ea | 2010-06-30 12:44:07 +0800 | [diff] [blame] | 29 | #include <openbsc/osmo_msc.h> |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 30 | #include <openbsc/vlr.h> |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 31 | |
Harald Welte (local) | d19e58b | 2009-08-15 02:30:58 +0200 | [diff] [blame] | 32 | void *tall_trans_ctx; |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 33 | |
Holger Hans Peter Freyther | 34e9749 | 2009-08-10 07:54:02 +0200 | [diff] [blame] | 34 | void _gsm48_cc_trans_free(struct gsm_trans *trans); |
| 35 | |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 36 | /*! 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 Erlbeck | dae1f64 | 2014-12-02 14:22:53 +0100 | [diff] [blame] | 42 | 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] | 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 | 7b423ed | 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 | 7b423ed | 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 | 7b423ed | 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 | |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 75 | /*! 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 Erlbeck | af792d6 | 2014-12-02 14:22:53 +0100 | [diff] [blame] | 82 | struct gsm_trans *trans_alloc(struct gsm_network *net, |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 83 | struct vlr_subscr *vsub, |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 84 | uint8_t protocol, uint8_t trans_id, |
| 85 | uint32_t callref) |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 86 | { |
| 87 | struct gsm_trans *trans; |
| 88 | |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 89 | DEBUGP(DCC, "subscr=%p, net=%p\n", vsub, net); |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 90 | |
| 91 | trans = talloc_zero(tall_trans_ctx, struct gsm_trans); |
| 92 | if (!trans) |
| 93 | return NULL; |
| 94 | |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 95 | trans->vsub = vlr_subscr_get(vsub); |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 96 | |
| 97 | trans->protocol = protocol; |
| 98 | trans->transaction_id = trans_id; |
| 99 | trans->callref = callref; |
| 100 | |
Jacob Erlbeck | f07c605 | 2014-12-02 11:58:00 +0100 | [diff] [blame] | 101 | trans->net = net; |
| 102 | llist_add_tail(&trans->entry, &net->trans_list); |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 103 | |
| 104 | return trans; |
| 105 | } |
| 106 | |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 107 | /*! Release a transaction |
| 108 | * \param[in] trans Transaction to be released |
| 109 | */ |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 110 | void trans_free(struct gsm_trans *trans) |
| 111 | { |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 112 | switch (trans->protocol) { |
| 113 | case GSM48_PDISC_CC: |
| 114 | _gsm48_cc_trans_free(trans); |
| 115 | break; |
Harald Welte (local) | 86b1717 | 2009-08-14 14:52:17 +0200 | [diff] [blame] | 116 | case GSM48_PDISC_SMS: |
| 117 | _gsm411_sms_trans_free(trans); |
| 118 | break; |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 119 | } |
| 120 | |
Holger Hans Peter Freyther | d6d7aff | 2015-04-06 12:03:45 +0200 | [diff] [blame] | 121 | if (trans->paging_request) { |
| 122 | subscr_remove_request(trans->paging_request); |
Holger Hans Peter Freyther | 49b3ed2 | 2010-12-29 17:09:07 +0100 | [diff] [blame] | 123 | trans->paging_request = NULL; |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 124 | } |
| 125 | |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 126 | if (trans->vsub) { |
| 127 | vlr_subscr_put(trans->vsub); |
| 128 | trans->vsub = NULL; |
Holger Hans Peter Freyther | 405824c | 2012-12-22 18:16:47 +0100 | [diff] [blame] | 129 | } |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 130 | |
| 131 | llist_del(&trans->entry); |
| 132 | |
Holger Hans Peter Freyther | 4049455 | 2010-06-28 17:09:29 +0800 | [diff] [blame] | 133 | if (trans->conn) |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 134 | msc_subscr_conn_put(trans->conn); |
Holger Hans Peter Freyther | 4049455 | 2010-06-28 17:09:29 +0800 | [diff] [blame] | 135 | |
Holger Hans Peter Freyther | 8effcb7 | 2013-12-27 18:07:23 +0100 | [diff] [blame] | 136 | trans->conn = NULL; |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 137 | talloc_free(trans); |
| 138 | } |
| 139 | |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 140 | /*! allocate an unused transaction ID for the given subscriber |
| 141 | * in the given protocol using the ti_flag specified |
| 142 | * \param[in] net GSM network |
| 143 | * \param[in] subscr Subscriber for which to find ID |
| 144 | * \param[in] protocol Protocol for whihc to find ID |
| 145 | * \param[in] ti_flag FIXME |
| 146 | */ |
| 147 | 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] | 148 | uint8_t protocol, uint8_t ti_flag) |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 149 | { |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 150 | struct gsm_trans *trans; |
| 151 | unsigned int used_tid_bitmask = 0; |
Sylvain Munaut | 926fcec | 2009-12-24 13:26:17 +0100 | [diff] [blame] | 152 | int i, j, h; |
Harald Welte | 78283ef | 2009-07-23 21:36:44 +0200 | [diff] [blame] | 153 | |
| 154 | if (ti_flag) |
| 155 | ti_flag = 0x8; |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 156 | |
| 157 | /* generate bitmask of already-used TIDs for this (subscr,proto) */ |
| 158 | llist_for_each_entry(trans, &net->trans_list, entry) { |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 159 | if (trans->vsub != vsub || |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 160 | trans->protocol != protocol || |
| 161 | trans->transaction_id == 0xff) |
| 162 | continue; |
| 163 | used_tid_bitmask |= (1 << trans->transaction_id); |
| 164 | } |
| 165 | |
Sylvain Munaut | 926fcec | 2009-12-24 13:26:17 +0100 | [diff] [blame] | 166 | /* find a new one, trying to go in a 'circular' pattern */ |
| 167 | for (h = 6; h > 0; h--) |
| 168 | if (used_tid_bitmask & (1 << (h | ti_flag))) |
| 169 | break; |
Sylvain Munaut | b9f3dce | 2009-12-18 18:28:11 +0100 | [diff] [blame] | 170 | for (i = 0; i < 7; i++) { |
Sylvain Munaut | 926fcec | 2009-12-24 13:26:17 +0100 | [diff] [blame] | 171 | j = ((h + i) % 7) | ti_flag; |
| 172 | if ((used_tid_bitmask & (1 << j)) == 0) |
| 173 | return j; |
Harald Welte | 78283ef | 2009-07-23 21:36:44 +0200 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | return -1; |
Harald Welte | 9ee4825 | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 177 | } |
Holger Hans Peter Freyther | 70ae5d3 | 2012-11-23 21:33:15 +0100 | [diff] [blame] | 178 | |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 179 | /*! Check if we have any transaction for given connection |
| 180 | * \param[in] conn Connection to check |
| 181 | * \returns 1 in case there is a transaction, 0 otherwise |
| 182 | */ |
Holger Hans Peter Freyther | 70ae5d3 | 2012-11-23 21:33:15 +0100 | [diff] [blame] | 183 | int trans_has_conn(const struct gsm_subscriber_connection *conn) |
| 184 | { |
| 185 | struct gsm_trans *trans; |
| 186 | |
Neels Hofmeyr | a9f2bb5 | 2016-05-09 21:09:47 +0200 | [diff] [blame] | 187 | llist_for_each_entry(trans, &conn->network->trans_list, entry) |
Holger Hans Peter Freyther | 70ae5d3 | 2012-11-23 21:33:15 +0100 | [diff] [blame] | 188 | if (trans->conn == conn) |
| 189 | return 1; |
| 190 | |
| 191 | return 0; |
| 192 | } |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 193 | |
| 194 | /*! Free all transactions associated with a connection, presumably when the |
| 195 | * conn is being closed. The transaction code will inform the CC or SMS |
| 196 | * facilities, which will then send the necessary release indications. |
| 197 | * \param[in] conn Connection that is going to be closed. |
| 198 | */ |
| 199 | void trans_conn_closed(struct gsm_subscriber_connection *conn) |
| 200 | { |
| 201 | struct gsm_trans *trans; |
| 202 | |
| 203 | /* As part of the CC REL_IND the remote leg might be released and this |
| 204 | * will trigger another call to trans_free. This is something the llist |
| 205 | * macro can not handle and we need to re-iterate the list every time. |
| 206 | */ |
| 207 | restart: |
| 208 | llist_for_each_entry(trans, &conn->network->trans_list, entry) { |
| 209 | if (trans->conn == conn) { |
| 210 | trans_free(trans); |
| 211 | goto restart; |
| 212 | } |
| 213 | } |
| 214 | } |