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