Harald Welte | d8c8e6f | 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 | 0e3e88e | 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 | d8c8e6f | 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 | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 14 | * GNU Affero General Public License for more details. |
Harald Welte | d8c8e6f | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 15 | * |
Harald Welte | 0e3e88e | 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 | d8c8e6f | 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 | dd5fff4 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 25 | #include <osmocom/core/talloc.h> |
Harald Welte | d8c8e6f | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 26 | #include <openbsc/gsm_subscriber.h> |
| 27 | #include <openbsc/gsm_04_08.h> |
| 28 | #include <openbsc/mncc.h> |
| 29 | #include <openbsc/paging.h> |
Holger Hans Peter Freyther | 3a0ec17 | 2010-06-30 12:44:07 +0800 | [diff] [blame] | 30 | #include <openbsc/osmo_msc.h> |
Harald Welte | d8c8e6f | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 31 | |
Harald Welte (local) | 8751ee9 | 2009-08-15 02:30:58 +0200 | [diff] [blame] | 32 | void *tall_trans_ctx; |
Harald Welte | d8c8e6f | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 33 | |
Holger Hans Peter Freyther | 735cd9e | 2009-08-10 07:54:02 +0200 | [diff] [blame] | 34 | void _gsm48_cc_trans_free(struct gsm_trans *trans); |
| 35 | |
Jacob Erlbeck | e2d8014 | 2014-12-02 14:22:53 +0100 | [diff] [blame] | 36 | struct gsm_trans *trans_find_by_id(struct gsm_subscriber_connection *conn, |
Holger Hans Peter Freyther | 7eb8a9a | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 37 | uint8_t proto, uint8_t trans_id) |
Harald Welte | d8c8e6f | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 38 | { |
| 39 | struct gsm_trans *trans; |
Jacob Erlbeck | e2d8014 | 2014-12-02 14:22:53 +0100 | [diff] [blame] | 40 | struct gsm_network *net = conn->bts->network; |
| 41 | struct gsm_subscriber *subscr = conn->subscr; |
Harald Welte | d8c8e6f | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 42 | |
| 43 | llist_for_each_entry(trans, &net->trans_list, entry) { |
Harald Welte | 0d82416 | 2009-07-23 21:58:40 +0200 | [diff] [blame] | 44 | if (trans->subscr == subscr && |
| 45 | trans->protocol == proto && |
| 46 | trans->transaction_id == trans_id) |
Harald Welte | d8c8e6f | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 47 | return trans; |
| 48 | } |
| 49 | return NULL; |
| 50 | } |
| 51 | |
| 52 | struct gsm_trans *trans_find_by_callref(struct gsm_network *net, |
Holger Hans Peter Freyther | 7eb8a9a | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 53 | uint32_t callref) |
Harald Welte | d8c8e6f | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 54 | { |
| 55 | struct gsm_trans *trans; |
| 56 | |
| 57 | llist_for_each_entry(trans, &net->trans_list, entry) { |
| 58 | if (trans->callref == callref) |
| 59 | return trans; |
| 60 | } |
| 61 | return NULL; |
| 62 | } |
| 63 | |
| 64 | struct gsm_trans *trans_alloc(struct gsm_subscriber *subscr, |
Holger Hans Peter Freyther | 7eb8a9a | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 65 | uint8_t protocol, uint8_t trans_id, |
| 66 | uint32_t callref) |
Harald Welte | d8c8e6f | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 67 | { |
| 68 | struct gsm_trans *trans; |
Jacob Erlbeck | da8770b | 2014-12-03 09:28:24 +0100 | [diff] [blame] | 69 | struct gsm_network *net = subscr->group->net; |
Harald Welte | d8c8e6f | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 70 | |
Jacob Erlbeck | e0b37e1 | 2014-12-02 11:58:00 +0100 | [diff] [blame] | 71 | DEBUGP(DCC, "subscr=%p, net=%p\n", subscr, net); |
Harald Welte | d8c8e6f | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 72 | |
| 73 | trans = talloc_zero(tall_trans_ctx, struct gsm_trans); |
| 74 | if (!trans) |
| 75 | return NULL; |
| 76 | |
| 77 | trans->subscr = subscr; |
| 78 | subscr_get(trans->subscr); |
| 79 | |
| 80 | trans->protocol = protocol; |
| 81 | trans->transaction_id = trans_id; |
| 82 | trans->callref = callref; |
| 83 | |
Jacob Erlbeck | e0b37e1 | 2014-12-02 11:58:00 +0100 | [diff] [blame] | 84 | trans->net = net; |
| 85 | llist_add_tail(&trans->entry, &net->trans_list); |
Harald Welte | d8c8e6f | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 86 | |
| 87 | return trans; |
| 88 | } |
| 89 | |
| 90 | void trans_free(struct gsm_trans *trans) |
| 91 | { |
Harald Welte | d8c8e6f | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 92 | switch (trans->protocol) { |
| 93 | case GSM48_PDISC_CC: |
| 94 | _gsm48_cc_trans_free(trans); |
| 95 | break; |
Harald Welte (local) | 9d0bd79 | 2009-08-14 14:52:17 +0200 | [diff] [blame] | 96 | case GSM48_PDISC_SMS: |
| 97 | _gsm411_sms_trans_free(trans); |
| 98 | break; |
Harald Welte | d8c8e6f | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 99 | } |
| 100 | |
Holger Hans Peter Freyther | 02539b1 | 2010-12-29 17:09:07 +0100 | [diff] [blame] | 101 | /* FIXME: implement a sane way to stop this. */ |
| 102 | if (!trans->conn && trans->paging_request) { |
| 103 | LOGP(DNM, LOGL_ERROR, |
| 104 | "Transaction freed while paging for sub: %llu\n", |
| 105 | trans->subscr->id); |
| 106 | trans->paging_request = NULL; |
Harald Welte | d8c8e6f | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 107 | } |
| 108 | |
Holger Hans Peter Freyther | dd4709c | 2012-12-22 18:16:47 +0100 | [diff] [blame] | 109 | if (trans->subscr) { |
Harald Welte | d8c8e6f | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 110 | subscr_put(trans->subscr); |
Holger Hans Peter Freyther | dd4709c | 2012-12-22 18:16:47 +0100 | [diff] [blame] | 111 | trans->subscr = NULL; |
| 112 | } |
Harald Welte | d8c8e6f | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 113 | |
| 114 | llist_del(&trans->entry); |
| 115 | |
Holger Hans Peter Freyther | 6e5c50f | 2010-06-28 17:09:29 +0800 | [diff] [blame] | 116 | if (trans->conn) |
| 117 | msc_release_connection(trans->conn); |
| 118 | |
Holger Hans Peter Freyther | 7a1d6bb | 2013-12-27 18:07:23 +0100 | [diff] [blame] | 119 | trans->conn = NULL; |
Harald Welte | d8c8e6f | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 120 | talloc_free(trans); |
| 121 | } |
| 122 | |
Harald Welte | 165fc33 | 2009-07-23 21:36:44 +0200 | [diff] [blame] | 123 | /* allocate an unused transaction ID for the given subscriber |
| 124 | * in the given protocol using the ti_flag specified */ |
Jacob Erlbeck | e2d8014 | 2014-12-02 14:22:53 +0100 | [diff] [blame] | 125 | int trans_assign_trans_id(struct gsm_network *net, struct gsm_subscriber *subscr, |
Holger Hans Peter Freyther | 7eb8a9a | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 126 | uint8_t protocol, uint8_t ti_flag) |
Harald Welte | d8c8e6f | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 127 | { |
Harald Welte | d8c8e6f | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 128 | struct gsm_trans *trans; |
| 129 | unsigned int used_tid_bitmask = 0; |
Sylvain Munaut | a36dbca | 2009-12-24 13:26:17 +0100 | [diff] [blame] | 130 | int i, j, h; |
Harald Welte | 165fc33 | 2009-07-23 21:36:44 +0200 | [diff] [blame] | 131 | |
| 132 | if (ti_flag) |
| 133 | ti_flag = 0x8; |
Harald Welte | d8c8e6f | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 134 | |
| 135 | /* generate bitmask of already-used TIDs for this (subscr,proto) */ |
| 136 | llist_for_each_entry(trans, &net->trans_list, entry) { |
| 137 | if (trans->subscr != subscr || |
| 138 | trans->protocol != protocol || |
| 139 | trans->transaction_id == 0xff) |
| 140 | continue; |
| 141 | used_tid_bitmask |= (1 << trans->transaction_id); |
| 142 | } |
| 143 | |
Sylvain Munaut | a36dbca | 2009-12-24 13:26:17 +0100 | [diff] [blame] | 144 | /* find a new one, trying to go in a 'circular' pattern */ |
| 145 | for (h = 6; h > 0; h--) |
| 146 | if (used_tid_bitmask & (1 << (h | ti_flag))) |
| 147 | break; |
Sylvain Munaut | 7a50ca8 | 2009-12-18 18:28:11 +0100 | [diff] [blame] | 148 | for (i = 0; i < 7; i++) { |
Sylvain Munaut | a36dbca | 2009-12-24 13:26:17 +0100 | [diff] [blame] | 149 | j = ((h + i) % 7) | ti_flag; |
| 150 | if ((used_tid_bitmask & (1 << j)) == 0) |
| 151 | return j; |
Harald Welte | 165fc33 | 2009-07-23 21:36:44 +0200 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | return -1; |
Harald Welte | d8c8e6f | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 155 | } |
Holger Hans Peter Freyther | f8faba7 | 2012-11-23 21:33:15 +0100 | [diff] [blame] | 156 | |
| 157 | int trans_has_conn(const struct gsm_subscriber_connection *conn) |
| 158 | { |
| 159 | struct gsm_trans *trans; |
| 160 | |
| 161 | llist_for_each_entry(trans, &conn->bts->network->trans_list, entry) |
| 162 | if (trans->conn == conn) |
| 163 | return 1; |
| 164 | |
| 165 | return 0; |
| 166 | } |