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 |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 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 |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along |
| 17 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include <openbsc/transaction.h> |
| 23 | #include <openbsc/gsm_data.h> |
| 24 | #include <openbsc/mncc.h> |
| 25 | #include <openbsc/debug.h> |
Harald Welte | f4625b1 | 2010-02-20 16:24:02 +0100 | [diff] [blame] | 26 | #include <osmocore/talloc.h> |
Harald Welte | d8c8e6f | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 27 | #include <openbsc/gsm_subscriber.h> |
| 28 | #include <openbsc/gsm_04_08.h> |
| 29 | #include <openbsc/mncc.h> |
| 30 | #include <openbsc/paging.h> |
| 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, |
| 37 | u_int8_t proto, u_int8_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, |
| 52 | u_int32_t callref) |
| 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, |
| 64 | u_int8_t protocol, u_int8_t trans_id, |
| 65 | u_int32_t callref) |
| 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 | 4009da4 | 2010-03-23 07:00:22 +0100 | [diff] [blame] | 98 | if (trans->conn) |
| 99 | put_subscr_con(trans->conn); |
Harald Welte | d8c8e6f | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 100 | |
Holger Hans Peter Freyther | 4009da4 | 2010-03-23 07:00:22 +0100 | [diff] [blame] | 101 | if (!trans->conn && trans->subscr && trans->subscr->net) { |
Harald Welte | d8c8e6f | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 102 | /* Stop paging on all bts' */ |
Holger Hans Peter Freyther | b78507a | 2009-08-21 05:43:44 +0200 | [diff] [blame] | 103 | paging_request_stop(NULL, trans->subscr, NULL); |
Harald Welte | d8c8e6f | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | if (trans->subscr) |
| 107 | subscr_put(trans->subscr); |
| 108 | |
| 109 | llist_del(&trans->entry); |
| 110 | |
| 111 | talloc_free(trans); |
| 112 | } |
| 113 | |
Harald Welte | 165fc33 | 2009-07-23 21:36:44 +0200 | [diff] [blame] | 114 | /* allocate an unused transaction ID for the given subscriber |
| 115 | * in the given protocol using the ti_flag specified */ |
Harald Welte | d8c8e6f | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 116 | int trans_assign_trans_id(struct gsm_subscriber *subscr, |
| 117 | u_int8_t protocol, u_int8_t ti_flag) |
| 118 | { |
| 119 | struct gsm_network *net = subscr->net; |
| 120 | struct gsm_trans *trans; |
| 121 | unsigned int used_tid_bitmask = 0; |
Sylvain Munaut | a36dbca | 2009-12-24 13:26:17 +0100 | [diff] [blame] | 122 | int i, j, h; |
Harald Welte | 165fc33 | 2009-07-23 21:36:44 +0200 | [diff] [blame] | 123 | |
| 124 | if (ti_flag) |
| 125 | ti_flag = 0x8; |
Harald Welte | d8c8e6f | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 126 | |
| 127 | /* generate bitmask of already-used TIDs for this (subscr,proto) */ |
| 128 | llist_for_each_entry(trans, &net->trans_list, entry) { |
| 129 | if (trans->subscr != subscr || |
| 130 | trans->protocol != protocol || |
| 131 | trans->transaction_id == 0xff) |
| 132 | continue; |
| 133 | used_tid_bitmask |= (1 << trans->transaction_id); |
| 134 | } |
| 135 | |
Sylvain Munaut | a36dbca | 2009-12-24 13:26:17 +0100 | [diff] [blame] | 136 | /* find a new one, trying to go in a 'circular' pattern */ |
| 137 | for (h = 6; h > 0; h--) |
| 138 | if (used_tid_bitmask & (1 << (h | ti_flag))) |
| 139 | break; |
Sylvain Munaut | 7a50ca8 | 2009-12-18 18:28:11 +0100 | [diff] [blame] | 140 | for (i = 0; i < 7; i++) { |
Sylvain Munaut | a36dbca | 2009-12-24 13:26:17 +0100 | [diff] [blame] | 141 | j = ((h + i) % 7) | ti_flag; |
| 142 | if ((used_tid_bitmask & (1 << j)) == 0) |
| 143 | return j; |
Harald Welte | 165fc33 | 2009-07-23 21:36:44 +0200 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | return -1; |
Harald Welte | d8c8e6f | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 147 | } |
Harald Welte | 2c99807 | 2009-12-17 17:13:28 +0100 | [diff] [blame] | 148 | |
| 149 | /* update all transactions to use a different LCHAN, e.g. |
| 150 | * after handover has succeeded */ |
Holger Hans Peter Freyther | 4009da4 | 2010-03-23 07:00:22 +0100 | [diff] [blame] | 151 | int trans_lchan_change(struct gsm_subscriber_connection *conn_old, |
| 152 | struct gsm_subscriber_connection *conn_new) |
Harald Welte | 2c99807 | 2009-12-17 17:13:28 +0100 | [diff] [blame] | 153 | { |
Holger Hans Peter Freyther | 541aaa4 | 2010-06-21 15:35:29 +0800 | [diff] [blame^] | 154 | struct gsm_network *net = conn_old->bts->network; |
Harald Welte | 2c99807 | 2009-12-17 17:13:28 +0100 | [diff] [blame] | 155 | struct gsm_trans *trans; |
| 156 | int num = 0; |
| 157 | |
| 158 | llist_for_each_entry(trans, &net->trans_list, entry) { |
Holger Hans Peter Freyther | 4009da4 | 2010-03-23 07:00:22 +0100 | [diff] [blame] | 159 | if (trans->conn == conn_old) { |
Holger Hans Peter Freyther | 065b811 | 2010-03-23 06:41:45 +0100 | [diff] [blame] | 160 | |
| 161 | /* drop old channel use count */ |
Holger Hans Peter Freyther | 4009da4 | 2010-03-23 07:00:22 +0100 | [diff] [blame] | 162 | put_subscr_con(conn_old); |
Harald Welte | b4290e0 | 2009-12-18 14:51:48 +0100 | [diff] [blame] | 163 | /* assign new channel */ |
Holger Hans Peter Freyther | 4009da4 | 2010-03-23 07:00:22 +0100 | [diff] [blame] | 164 | trans->conn = conn_new; |
Harald Welte | b4290e0 | 2009-12-18 14:51:48 +0100 | [diff] [blame] | 165 | /* bump new channel use count */ |
Holger Hans Peter Freyther | 4009da4 | 2010-03-23 07:00:22 +0100 | [diff] [blame] | 166 | use_subscr_con(conn_new); |
Harald Welte | 2c99807 | 2009-12-17 17:13:28 +0100 | [diff] [blame] | 167 | num++; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | return num; |
| 172 | } |