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> |
Holger Hans Peter Freyther | 3a0ec17 | 2010-06-30 12:44:07 +0800 | [diff] [blame] | 31 | #include <openbsc/osmo_msc.h> |
Harald Welte | d8c8e6f | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 32 | |
Harald Welte (local) | 8751ee9 | 2009-08-15 02:30:58 +0200 | [diff] [blame] | 33 | void *tall_trans_ctx; |
Harald Welte | d8c8e6f | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 34 | |
Holger Hans Peter Freyther | 735cd9e | 2009-08-10 07:54:02 +0200 | [diff] [blame] | 35 | void _gsm48_cc_trans_free(struct gsm_trans *trans); |
| 36 | |
Harald Welte | 0d82416 | 2009-07-23 21:58:40 +0200 | [diff] [blame] | 37 | struct gsm_trans *trans_find_by_id(struct gsm_subscriber *subscr, |
| 38 | u_int8_t proto, u_int8_t trans_id) |
Harald Welte | d8c8e6f | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 39 | { |
| 40 | struct gsm_trans *trans; |
Harald Welte | 0d82416 | 2009-07-23 21:58:40 +0200 | [diff] [blame] | 41 | struct gsm_network *net = subscr->net; |
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, |
| 53 | u_int32_t callref) |
| 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, |
| 65 | u_int8_t protocol, u_int8_t trans_id, |
| 66 | u_int32_t callref) |
| 67 | { |
| 68 | struct gsm_trans *trans; |
| 69 | |
| 70 | DEBUGP(DCC, "subscr=%p, subscr->net=%p\n", subscr, subscr->net); |
| 71 | |
| 72 | trans = talloc_zero(tall_trans_ctx, struct gsm_trans); |
| 73 | if (!trans) |
| 74 | return NULL; |
| 75 | |
| 76 | trans->subscr = subscr; |
| 77 | subscr_get(trans->subscr); |
| 78 | |
| 79 | trans->protocol = protocol; |
| 80 | trans->transaction_id = trans_id; |
| 81 | trans->callref = callref; |
| 82 | |
| 83 | llist_add_tail(&trans->entry, &subscr->net->trans_list); |
| 84 | |
| 85 | return trans; |
| 86 | } |
| 87 | |
| 88 | void trans_free(struct gsm_trans *trans) |
| 89 | { |
Harald Welte | d8c8e6f | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 90 | switch (trans->protocol) { |
| 91 | case GSM48_PDISC_CC: |
| 92 | _gsm48_cc_trans_free(trans); |
| 93 | break; |
Harald Welte (local) | 9d0bd79 | 2009-08-14 14:52:17 +0200 | [diff] [blame] | 94 | case GSM48_PDISC_SMS: |
| 95 | _gsm411_sms_trans_free(trans); |
| 96 | break; |
Harald Welte | d8c8e6f | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 97 | } |
| 98 | |
Holger Hans Peter Freyther | 4009da4 | 2010-03-23 07:00:22 +0100 | [diff] [blame] | 99 | if (!trans->conn && trans->subscr && trans->subscr->net) { |
Harald Welte | d8c8e6f | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 100 | /* Stop paging on all bts' */ |
Holger Hans Peter Freyther | b78507a | 2009-08-21 05:43:44 +0200 | [diff] [blame] | 101 | paging_request_stop(NULL, trans->subscr, NULL); |
Harald Welte | d8c8e6f | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | if (trans->subscr) |
| 105 | subscr_put(trans->subscr); |
| 106 | |
| 107 | llist_del(&trans->entry); |
| 108 | |
Holger Hans Peter Freyther | 6e5c50f | 2010-06-28 17:09:29 +0800 | [diff] [blame] | 109 | if (trans->conn) |
| 110 | msc_release_connection(trans->conn); |
| 111 | |
| 112 | |
Harald Welte | d8c8e6f | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 113 | talloc_free(trans); |
| 114 | } |
| 115 | |
Harald Welte | 165fc33 | 2009-07-23 21:36:44 +0200 | [diff] [blame] | 116 | /* allocate an unused transaction ID for the given subscriber |
| 117 | * in the given protocol using the ti_flag specified */ |
Harald Welte | d8c8e6f | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 118 | int trans_assign_trans_id(struct gsm_subscriber *subscr, |
| 119 | u_int8_t protocol, u_int8_t ti_flag) |
| 120 | { |
| 121 | struct gsm_network *net = subscr->net; |
| 122 | struct gsm_trans *trans; |
| 123 | unsigned int used_tid_bitmask = 0; |
Sylvain Munaut | a36dbca | 2009-12-24 13:26:17 +0100 | [diff] [blame] | 124 | int i, j, h; |
Harald Welte | 165fc33 | 2009-07-23 21:36:44 +0200 | [diff] [blame] | 125 | |
| 126 | if (ti_flag) |
| 127 | ti_flag = 0x8; |
Harald Welte | d8c8e6f | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 128 | |
| 129 | /* generate bitmask of already-used TIDs for this (subscr,proto) */ |
| 130 | llist_for_each_entry(trans, &net->trans_list, entry) { |
| 131 | if (trans->subscr != subscr || |
| 132 | trans->protocol != protocol || |
| 133 | trans->transaction_id == 0xff) |
| 134 | continue; |
| 135 | used_tid_bitmask |= (1 << trans->transaction_id); |
| 136 | } |
| 137 | |
Sylvain Munaut | a36dbca | 2009-12-24 13:26:17 +0100 | [diff] [blame] | 138 | /* find a new one, trying to go in a 'circular' pattern */ |
| 139 | for (h = 6; h > 0; h--) |
| 140 | if (used_tid_bitmask & (1 << (h | ti_flag))) |
| 141 | break; |
Sylvain Munaut | 7a50ca8 | 2009-12-18 18:28:11 +0100 | [diff] [blame] | 142 | for (i = 0; i < 7; i++) { |
Sylvain Munaut | a36dbca | 2009-12-24 13:26:17 +0100 | [diff] [blame] | 143 | j = ((h + i) % 7) | ti_flag; |
| 144 | if ((used_tid_bitmask & (1 << j)) == 0) |
| 145 | return j; |
Harald Welte | 165fc33 | 2009-07-23 21:36:44 +0200 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | return -1; |
Harald Welte | d8c8e6f | 2009-07-23 21:25:08 +0200 | [diff] [blame] | 149 | } |
Harald Welte | 2c99807 | 2009-12-17 17:13:28 +0100 | [diff] [blame] | 150 | |