Holger Hans Peter Freyther | ec32b58 | 2010-03-23 07:40:46 +0100 | [diff] [blame] | 1 | /* GSM 08.08 like API for OpenBSC. The bridge from MSC to BSC */ |
| 2 | |
Holger Hans Peter Freyther | 594b322 | 2011-08-06 06:42:00 +0200 | [diff] [blame] | 3 | /* (C) 2010-2011 by Holger Hans Peter Freyther |
| 4 | * (C) 2010-2011 by On-Waves |
Holger Hans Peter Freyther | 2a9285c | 2010-06-14 18:03:06 +0800 | [diff] [blame] | 5 | * (C) 2009 by Harald Welte <laforge@gnumonks.org> |
Holger Hans Peter Freyther | ec32b58 | 2010-03-23 07:40:46 +0100 | [diff] [blame] | 6 | * |
| 7 | * All Rights Reserved |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 10 | * it under the terms of the GNU Affero General Public License as published by |
| 11 | * the Free Software Foundation; either version 3 of the License, or |
Holger Hans Peter Freyther | ec32b58 | 2010-03-23 07:40:46 +0100 | [diff] [blame] | 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 17 | * GNU Affero General Public License for more details. |
Holger Hans Peter Freyther | ec32b58 | 2010-03-23 07:40:46 +0100 | [diff] [blame] | 18 | * |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 19 | * You should have received a copy of the GNU Affero General Public License |
| 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Holger Hans Peter Freyther | ec32b58 | 2010-03-23 07:40:46 +0100 | [diff] [blame] | 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <openbsc/bsc_api.h> |
Holger Hans Peter Freyther | 9c595b7 | 2010-06-15 11:20:52 +0800 | [diff] [blame] | 25 | #include <openbsc/bsc_rll.h> |
Holger Hans Peter Freyther | abcddf1 | 2010-06-14 18:20:15 +0800 | [diff] [blame] | 26 | #include <openbsc/gsm_data.h> |
Holger Hans Peter Freyther | e2fee35 | 2010-12-22 12:32:56 +0100 | [diff] [blame] | 27 | #include <openbsc/gsm_subscriber.h> |
Holger Hans Peter Freyther | abcddf1 | 2010-06-14 18:20:15 +0800 | [diff] [blame] | 28 | #include <openbsc/signal.h> |
Holger Hans Peter Freyther | ec32b58 | 2010-03-23 07:40:46 +0100 | [diff] [blame] | 29 | #include <openbsc/abis_rsl.h> |
Holger Hans Peter Freyther | 9764331 | 2010-06-17 16:41:25 +0800 | [diff] [blame] | 30 | #include <openbsc/chan_alloc.h> |
Holger Hans Peter Freyther | f2553a6 | 2010-06-30 12:58:14 +0800 | [diff] [blame] | 31 | #include <openbsc/handover.h> |
Holger Hans Peter Freyther | 49ab278 | 2010-07-05 11:38:37 +0800 | [diff] [blame] | 32 | #include <openbsc/debug.h> |
Holger Hans Peter Freyther | 77cd95d | 2010-11-14 16:19:48 +0100 | [diff] [blame] | 33 | #include <openbsc/gsm_04_08.h> |
Holger Hans Peter Freyther | ec32b58 | 2010-03-23 07:40:46 +0100 | [diff] [blame] | 34 | |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 35 | #include <osmocom/gsm/protocol/gsm_08_08.h> |
Holger Hans Peter Freyther | 6c199e7 | 2010-11-04 11:18:03 +0100 | [diff] [blame] | 36 | |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 37 | #include <osmocom/core/talloc.h> |
Holger Hans Peter Freyther | abcddf1 | 2010-06-14 18:20:15 +0800 | [diff] [blame] | 38 | |
Holger Hans Peter Freyther | 77cd95d | 2010-11-14 16:19:48 +0100 | [diff] [blame] | 39 | #define GSM0808_T10_VALUE 6, 0 |
| 40 | |
Holger Hans Peter Freyther | 79f763f | 2010-09-16 00:53:37 +0800 | [diff] [blame] | 41 | static LLIST_HEAD(sub_connections); |
| 42 | |
Holger Hans Peter Freyther | 9c595b7 | 2010-06-15 11:20:52 +0800 | [diff] [blame] | 43 | static void rll_ind_cb(struct gsm_lchan *, uint8_t, void *, enum bsc_rllr_ind); |
| 44 | static void send_sapi_reject(struct gsm_subscriber_connection *conn, int link_id); |
Holger Hans Peter Freyther | f05750c | 2010-11-14 19:29:42 +0100 | [diff] [blame] | 45 | static void handle_release(struct gsm_subscriber_connection *conn, struct bsc_api *bsc, struct gsm_lchan *lchan); |
Holger Hans Peter Freyther | 77cd95d | 2010-11-14 16:19:48 +0100 | [diff] [blame] | 46 | static void handle_chan_ack(struct gsm_subscriber_connection *conn, struct bsc_api *bsc, struct gsm_lchan *lchan); |
| 47 | static void handle_chan_nack(struct gsm_subscriber_connection *conn, struct bsc_api *bsc, struct gsm_lchan *lchan); |
Holger Hans Peter Freyther | 9c595b7 | 2010-06-15 11:20:52 +0800 | [diff] [blame] | 48 | |
Holger Hans Peter Freyther | b094171 | 2010-11-04 12:06:57 +0100 | [diff] [blame] | 49 | /* GSM 08.08 3.2.2.33 */ |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 50 | static uint8_t lchan_to_chosen_channel(struct gsm_lchan *lchan) |
Holger Hans Peter Freyther | b094171 | 2010-11-04 12:06:57 +0100 | [diff] [blame] | 51 | { |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 52 | uint8_t channel_mode = 0, channel = 0; |
Holger Hans Peter Freyther | b094171 | 2010-11-04 12:06:57 +0100 | [diff] [blame] | 53 | |
| 54 | switch (lchan->tch_mode) { |
| 55 | case GSM48_CMODE_SPEECH_V1: |
| 56 | case GSM48_CMODE_SPEECH_EFR: |
| 57 | case GSM48_CMODE_SPEECH_AMR: |
| 58 | channel_mode = 0x9; |
| 59 | break; |
| 60 | case GSM48_CMODE_SIGN: |
| 61 | channel_mode = 0x8; |
| 62 | break; |
| 63 | case GSM48_CMODE_DATA_14k5: |
| 64 | channel_mode = 0xe; |
| 65 | break; |
| 66 | case GSM48_CMODE_DATA_12k0: |
| 67 | channel_mode = 0xb; |
| 68 | break; |
| 69 | case GSM48_CMODE_DATA_6k0: |
| 70 | channel_mode = 0xc; |
| 71 | break; |
| 72 | case GSM48_CMODE_DATA_3k6: |
| 73 | channel_mode = 0xd; |
| 74 | break; |
| 75 | } |
| 76 | |
| 77 | switch (lchan->type) { |
| 78 | case GSM_LCHAN_NONE: |
| 79 | channel = 0x0; |
| 80 | break; |
| 81 | case GSM_LCHAN_SDCCH: |
| 82 | channel = 0x1; |
| 83 | break; |
| 84 | case GSM_LCHAN_TCH_F: |
| 85 | channel = 0x8; |
| 86 | break; |
| 87 | case GSM_LCHAN_TCH_H: |
| 88 | channel = 0x9; |
| 89 | break; |
| 90 | case GSM_LCHAN_UNKNOWN: |
| 91 | LOGP(DMSC, LOGL_ERROR, "Unknown lchan type: %p\n", lchan); |
| 92 | break; |
| 93 | } |
| 94 | |
| 95 | return channel_mode << 4 | channel; |
| 96 | } |
| 97 | |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 98 | static uint8_t chan_mode_to_speech(struct gsm_lchan *lchan) |
Holger Hans Peter Freyther | b094171 | 2010-11-04 12:06:57 +0100 | [diff] [blame] | 99 | { |
| 100 | int mode = 0; |
| 101 | |
| 102 | switch (lchan->tch_mode) { |
| 103 | case GSM48_CMODE_SPEECH_V1: |
| 104 | mode = 1; |
| 105 | break; |
| 106 | case GSM48_CMODE_SPEECH_EFR: |
| 107 | mode = 0x11; |
| 108 | break; |
| 109 | case GSM48_CMODE_SPEECH_AMR: |
| 110 | mode = 0x21; |
| 111 | break; |
| 112 | case GSM48_CMODE_SIGN: |
| 113 | case GSM48_CMODE_DATA_14k5: |
| 114 | case GSM48_CMODE_DATA_12k0: |
| 115 | case GSM48_CMODE_DATA_6k0: |
| 116 | case GSM48_CMODE_DATA_3k6: |
| 117 | default: |
| 118 | LOGP(DMSC, LOGL_ERROR, "Using non speech mode: %d\n", mode); |
| 119 | return 0; |
| 120 | break; |
| 121 | } |
| 122 | |
| 123 | /* assume to always do AMR HR on any TCH type */ |
| 124 | if (lchan->type == GSM_LCHAN_TCH_H || |
| 125 | lchan->tch_mode == GSM48_CMODE_SPEECH_AMR) |
| 126 | mode |= 0x4; |
| 127 | |
| 128 | return mode; |
| 129 | } |
| 130 | |
Holger Hans Peter Freyther | 77cd95d | 2010-11-14 16:19:48 +0100 | [diff] [blame] | 131 | static void assignment_t10_timeout(void *_conn) |
| 132 | { |
| 133 | struct bsc_api *api; |
| 134 | struct gsm_subscriber_connection *conn = |
| 135 | (struct gsm_subscriber_connection *) _conn; |
| 136 | |
| 137 | LOGP(DMSC, LOGL_ERROR, "Assigment T10 timeout on %p\n", conn); |
| 138 | |
Holger Hans Peter Freyther | cab0d7b | 2011-08-30 19:07:58 +0200 | [diff] [blame] | 139 | /* |
| 140 | * normal release on the secondary channel but only if the |
| 141 | * secondary_channel has not been released by the handle_chan_nack. |
| 142 | */ |
| 143 | if (conn->secondary_lchan) |
Daniel Willmann | 28d9ccb | 2011-08-10 11:40:54 +0200 | [diff] [blame] | 144 | lchan_release(conn->secondary_lchan, 0, 1); |
Holger Hans Peter Freyther | 77cd95d | 2010-11-14 16:19:48 +0100 | [diff] [blame] | 145 | conn->secondary_lchan = NULL; |
| 146 | |
| 147 | /* inform them about the failure */ |
| 148 | api = conn->bts->network->bsc_api; |
| 149 | api->assign_fail(conn, GSM0808_CAUSE_NO_RADIO_RESOURCE_AVAILABLE, NULL); |
| 150 | } |
| 151 | |
Holger Hans Peter Freyther | 594b322 | 2011-08-06 06:42:00 +0200 | [diff] [blame] | 152 | /** |
| 153 | * Handle the multirate config |
| 154 | */ |
| 155 | static void handle_mr_config(struct gsm_subscriber_connection *conn, |
| 156 | struct gsm_lchan *lchan) |
| 157 | { |
| 158 | lchan->mr_conf.ver = 1; |
| 159 | lchan->mr_conf.icmi = 1; |
| 160 | lchan->mr_conf.m5_90 = 1; |
| 161 | } |
| 162 | |
Holger Hans Peter Freyther | 77cd95d | 2010-11-14 16:19:48 +0100 | [diff] [blame] | 163 | /* |
| 164 | * Start a new assignment and make sure that it is completed within T10 either |
| 165 | * positively, negatively or by the timeout. |
| 166 | * |
| 167 | * 1.) allocate a new lchan |
| 168 | * 2.) copy the encryption key and other data from the |
| 169 | * old to the new channel. |
| 170 | * 3.) RSL Channel Activate this channel and wait |
| 171 | * |
| 172 | * -> Signal handler for the LCHAN |
| 173 | * 4.) Send GSM 04.08 assignment command to the MS |
| 174 | * |
| 175 | * -> Assignment Complete/Assignment Failure |
| 176 | * 5.) Release the SDCCH, continue signalling on the new link |
| 177 | */ |
| 178 | static int handle_new_assignment(struct gsm_subscriber_connection *conn, int chan_mode, int full_rate) |
| 179 | { |
| 180 | struct gsm_lchan *new_lchan; |
| 181 | int chan_type; |
| 182 | |
| 183 | chan_type = full_rate ? GSM_LCHAN_TCH_F : GSM_LCHAN_TCH_H; |
| 184 | |
| 185 | new_lchan = lchan_alloc(conn->bts, chan_type, 0); |
| 186 | |
| 187 | if (!new_lchan) { |
| 188 | LOGP(DMSC, LOGL_NOTICE, "No free channel.\n"); |
| 189 | return -1; |
| 190 | } |
| 191 | |
| 192 | /* copy old data to the new channel */ |
| 193 | memcpy(&new_lchan->encr, &conn->lchan->encr, sizeof(new_lchan->encr)); |
| 194 | new_lchan->ms_power = conn->lchan->ms_power; |
| 195 | new_lchan->bs_power = conn->lchan->bs_power; |
| 196 | |
| 197 | /* copy new data to it */ |
| 198 | new_lchan->tch_mode = chan_mode; |
| 199 | new_lchan->rsl_cmode = RSL_CMOD_SPD_SPEECH; |
| 200 | |
| 201 | /* handle AMR correctly */ |
Holger Hans Peter Freyther | 594b322 | 2011-08-06 06:42:00 +0200 | [diff] [blame] | 202 | if (chan_mode == GSM48_CMODE_SPEECH_AMR) |
| 203 | handle_mr_config(conn, new_lchan); |
Holger Hans Peter Freyther | 77cd95d | 2010-11-14 16:19:48 +0100 | [diff] [blame] | 204 | |
| 205 | if (rsl_chan_activate_lchan(new_lchan, 0x1, 0, 0) < 0) { |
| 206 | LOGP(DHO, LOGL_ERROR, "could not activate channel\n"); |
| 207 | lchan_free(new_lchan); |
| 208 | return -1; |
| 209 | } |
| 210 | |
| 211 | /* remember that we have the channel */ |
| 212 | conn->secondary_lchan = new_lchan; |
| 213 | new_lchan->conn = conn; |
| 214 | |
| 215 | rsl_lchan_set_state(new_lchan, LCHAN_S_ACT_REQ); |
| 216 | return 0; |
| 217 | } |
Holger Hans Peter Freyther | b094171 | 2010-11-04 12:06:57 +0100 | [diff] [blame] | 218 | |
Holger Hans Peter Freyther | a2aedad | 2010-09-16 00:47:32 +0800 | [diff] [blame] | 219 | struct gsm_subscriber_connection *subscr_con_allocate(struct gsm_lchan *lchan) |
| 220 | { |
| 221 | struct gsm_subscriber_connection *conn; |
| 222 | |
| 223 | conn = talloc_zero(lchan->ts->trx->bts->network, struct gsm_subscriber_connection); |
| 224 | if (!conn) |
| 225 | return NULL; |
| 226 | |
| 227 | /* Configure the time and start it so it will be closed */ |
| 228 | conn->lchan = lchan; |
| 229 | conn->bts = lchan->ts->trx->bts; |
| 230 | lchan->conn = conn; |
Holger Hans Peter Freyther | 79f763f | 2010-09-16 00:53:37 +0800 | [diff] [blame] | 231 | llist_add_tail(&conn->entry, &sub_connections); |
Holger Hans Peter Freyther | a2aedad | 2010-09-16 00:47:32 +0800 | [diff] [blame] | 232 | return conn; |
| 233 | } |
| 234 | |
| 235 | /* TODO: move subscriber put here... */ |
| 236 | void subscr_con_free(struct gsm_subscriber_connection *conn) |
| 237 | { |
Holger Hans Peter Freyther | a2aedad | 2010-09-16 00:47:32 +0800 | [diff] [blame] | 238 | if (!conn) |
| 239 | return; |
| 240 | |
| 241 | |
| 242 | if (conn->subscr) { |
| 243 | subscr_put(conn->subscr); |
| 244 | conn->subscr = NULL; |
| 245 | } |
| 246 | |
| 247 | |
Holger Hans Peter Freyther | a2828fa | 2010-11-14 15:50:42 +0100 | [diff] [blame] | 248 | if (conn->ho_lchan) { |
Holger Hans Peter Freyther | a2aedad | 2010-09-16 00:47:32 +0800 | [diff] [blame] | 249 | LOGP(DNM, LOGL_ERROR, "The ho_lchan should have been cleared.\n"); |
Holger Hans Peter Freyther | a2828fa | 2010-11-14 15:50:42 +0100 | [diff] [blame] | 250 | conn->ho_lchan->conn = NULL; |
| 251 | } |
| 252 | |
| 253 | if (conn->lchan) { |
| 254 | LOGP(DNM, LOGL_ERROR, "The lchan should have been cleared.\n"); |
| 255 | conn->lchan->conn = NULL; |
| 256 | } |
Holger Hans Peter Freyther | a2aedad | 2010-09-16 00:47:32 +0800 | [diff] [blame] | 257 | |
Holger Hans Peter Freyther | 77cd95d | 2010-11-14 16:19:48 +0100 | [diff] [blame] | 258 | if (conn->secondary_lchan) { |
| 259 | LOGP(DNM, LOGL_ERROR, "The secondary_lchan should have been cleared.\n"); |
| 260 | conn->secondary_lchan->conn = NULL; |
| 261 | } |
| 262 | |
Holger Hans Peter Freyther | 79f763f | 2010-09-16 00:53:37 +0800 | [diff] [blame] | 263 | llist_del(&conn->entry); |
Holger Hans Peter Freyther | a2aedad | 2010-09-16 00:47:32 +0800 | [diff] [blame] | 264 | talloc_free(conn); |
Holger Hans Peter Freyther | a2aedad | 2010-09-16 00:47:32 +0800 | [diff] [blame] | 265 | } |
| 266 | |
Holger Hans Peter Freyther | abcddf1 | 2010-06-14 18:20:15 +0800 | [diff] [blame] | 267 | int bsc_api_init(struct gsm_network *network, struct bsc_api *api) |
| 268 | { |
| 269 | network->bsc_api = api; |
| 270 | return 0; |
| 271 | } |
Holger Hans Peter Freyther | ec32b58 | 2010-03-23 07:40:46 +0100 | [diff] [blame] | 272 | |
Harald Welte | b70bd52 | 2011-09-13 00:02:09 +0100 | [diff] [blame] | 273 | /*! \brief process incoming 08.08 DTAP from MSC (send via BTS to MS) */ |
Holger Hans Peter Freyther | ec32b58 | 2010-03-23 07:40:46 +0100 | [diff] [blame] | 274 | int gsm0808_submit_dtap(struct gsm_subscriber_connection *conn, |
Holger Hans Peter Freyther | 8d380dc | 2010-11-10 10:16:02 +0100 | [diff] [blame] | 275 | struct msgb *msg, int link_id, int allow_sach) |
Holger Hans Peter Freyther | ec32b58 | 2010-03-23 07:40:46 +0100 | [diff] [blame] | 276 | { |
Holger Hans Peter Freyther | 8ce8d3a | 2010-12-27 10:55:45 +0100 | [diff] [blame] | 277 | uint8_t sapi; |
| 278 | |
| 279 | |
| 280 | if (!conn->lchan) { |
| 281 | LOGP(DMSC, LOGL_ERROR, |
| 282 | "Called submit dtap without an lchan.\n"); |
| 283 | msgb_free(msg); |
| 284 | return -1; |
| 285 | } |
| 286 | |
| 287 | sapi = link_id & 0x7; |
Holger Hans Peter Freyther | ec32b58 | 2010-03-23 07:40:46 +0100 | [diff] [blame] | 288 | msg->lchan = conn->lchan; |
Pablo Neira Ayuso | 7abecfc | 2011-08-17 22:43:54 +0200 | [diff] [blame] | 289 | msg->dst = msg->lchan->ts->trx->rsl_link; |
Holger Hans Peter Freyther | 9c595b7 | 2010-06-15 11:20:52 +0800 | [diff] [blame] | 290 | |
Holger Hans Peter Freyther | 8d380dc | 2010-11-10 10:16:02 +0100 | [diff] [blame] | 291 | /* If we are on a TCH and need to submit a SMS (on SAPI=3) we need to use the SACH */ |
| 292 | if (allow_sach && sapi != 0) { |
| 293 | if (conn->lchan->type == GSM_LCHAN_TCH_F || conn->lchan->type == GSM_LCHAN_TCH_H) |
| 294 | link_id |= 0x40; |
| 295 | } |
| 296 | |
Holger Hans Peter Freyther | f6d0e06 | 2010-06-17 17:48:13 +0800 | [diff] [blame] | 297 | msg->l3h = msg->data; |
Harald Welte | b70bd52 | 2011-09-13 00:02:09 +0100 | [diff] [blame] | 298 | /* is requested SAPI already up? */ |
Holger Hans Peter Freyther | 9c595b7 | 2010-06-15 11:20:52 +0800 | [diff] [blame] | 299 | if (conn->lchan->sapis[sapi] == LCHAN_SAPI_UNUSED) { |
Harald Welte | b70bd52 | 2011-09-13 00:02:09 +0100 | [diff] [blame] | 300 | /* Establish L2 for additional SAPI */ |
Holger Hans Peter Freyther | 9c595b7 | 2010-06-15 11:20:52 +0800 | [diff] [blame] | 301 | OBSC_LINKID_CB(msg) = link_id; |
| 302 | if (rll_establish(msg->lchan, sapi, rll_ind_cb, msg) != 0) { |
| 303 | msgb_free(msg); |
| 304 | send_sapi_reject(conn, link_id); |
| 305 | return -1; |
| 306 | } |
| 307 | return 0; |
| 308 | } else { |
Harald Welte | b70bd52 | 2011-09-13 00:02:09 +0100 | [diff] [blame] | 309 | /* Directly forward via RLL/RSL to BTS */ |
Holger Hans Peter Freyther | 9c595b7 | 2010-06-15 11:20:52 +0800 | [diff] [blame] | 310 | return rsl_data_request(msg, link_id); |
| 311 | } |
Holger Hans Peter Freyther | ec32b58 | 2010-03-23 07:40:46 +0100 | [diff] [blame] | 312 | } |
Holger Hans Peter Freyther | 2a9285c | 2010-06-14 18:03:06 +0800 | [diff] [blame] | 313 | |
Holger Hans Peter Freyther | a5dcdd9 | 2012-04-07 13:25:47 +0200 | [diff] [blame] | 314 | /* |
| 315 | * \brief Check if the given channel is compatible with the mode/fullrate |
| 316 | * |
| 317 | * NOTE: This code is only written with TCH/F and TCH/H in mind. This means |
| 318 | * that it will not work for CSD, handover, etc. This also assumes that the |
| 319 | * type of the lchan is either a TCH or a SDCCH. |
| 320 | */ |
| 321 | static int chan_compat_with_mode(struct gsm_lchan *lchan, int chan_mode, int full_rate) |
| 322 | { |
| 323 | if (lchan->type == GSM_LCHAN_SDCCH) |
| 324 | return 1; |
| 325 | if (full_rate && lchan->type != GSM_LCHAN_TCH_F) |
| 326 | return 1; |
| 327 | |
| 328 | return 0; |
| 329 | } |
| 330 | |
Holger Hans Peter Freyther | c354de8 | 2010-07-23 16:49:38 +0800 | [diff] [blame] | 331 | /** |
| 332 | * Send a GSM08.08 Assignment Request. Right now this does not contain the |
Holger Hans Peter Freyther | 088dde8 | 2010-11-10 11:07:12 +0100 | [diff] [blame] | 333 | * audio codec type or the allowed rates for the config. It is assumed that |
Holger Hans Peter Freyther | a5dcdd9 | 2012-04-07 13:25:47 +0200 | [diff] [blame] | 334 | * this is for audio handling only. In case the current channel does not allow |
| 335 | * the selected mode a new one will be allocated. |
| 336 | * |
| 337 | * TODO: Add multirate configuration, make it work for more than audio. |
Holger Hans Peter Freyther | c354de8 | 2010-07-23 16:49:38 +0800 | [diff] [blame] | 338 | */ |
Holger Hans Peter Freyther | 2603c1f | 2010-11-10 10:01:20 +0100 | [diff] [blame] | 339 | int gsm0808_assign_req(struct gsm_subscriber_connection *conn, int chan_mode, int full_rate) |
Holger Hans Peter Freyther | c354de8 | 2010-07-23 16:49:38 +0800 | [diff] [blame] | 340 | { |
| 341 | struct bsc_api *api; |
| 342 | api = conn->bts->network->bsc_api; |
| 343 | |
Holger Hans Peter Freyther | a5dcdd9 | 2012-04-07 13:25:47 +0200 | [diff] [blame] | 344 | if (chan_compat_with_mode(conn->lchan, chan_mode, full_rate) != 0) { |
Holger Hans Peter Freyther | 77cd95d | 2010-11-14 16:19:48 +0100 | [diff] [blame] | 345 | if (handle_new_assignment(conn, chan_mode, full_rate) != 0) |
| 346 | goto error; |
Holger Hans Peter Freyther | 088dde8 | 2010-11-10 11:07:12 +0100 | [diff] [blame] | 347 | } else { |
| 348 | LOGP(DMSC, LOGL_NOTICE, |
| 349 | "Sending ChanModify for speech %d %d\n", chan_mode, full_rate); |
Holger Hans Peter Freyther | 594b322 | 2011-08-06 06:42:00 +0200 | [diff] [blame] | 350 | if (chan_mode == GSM48_CMODE_SPEECH_AMR) |
| 351 | handle_mr_config(conn, conn->lchan); |
Holger Hans Peter Freyther | 088dde8 | 2010-11-10 11:07:12 +0100 | [diff] [blame] | 352 | |
Holger Hans Peter Freyther | 77cd95d | 2010-11-14 16:19:48 +0100 | [diff] [blame] | 353 | gsm48_lchan_modify(conn->lchan, chan_mode); |
Holger Hans Peter Freyther | 088dde8 | 2010-11-10 11:07:12 +0100 | [diff] [blame] | 354 | } |
| 355 | |
Holger Hans Peter Freyther | 77cd95d | 2010-11-14 16:19:48 +0100 | [diff] [blame] | 356 | /* we will now start the timer to complete the assignment */ |
| 357 | conn->T10.cb = assignment_t10_timeout; |
| 358 | conn->T10.data = conn; |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 359 | osmo_timer_schedule(&conn->T10, GSM0808_T10_VALUE); |
Holger Hans Peter Freyther | c354de8 | 2010-07-23 16:49:38 +0800 | [diff] [blame] | 360 | return 0; |
Holger Hans Peter Freyther | 77cd95d | 2010-11-14 16:19:48 +0100 | [diff] [blame] | 361 | |
| 362 | error: |
| 363 | api->assign_fail(conn, 0, NULL); |
| 364 | return -1; |
Holger Hans Peter Freyther | c354de8 | 2010-07-23 16:49:38 +0800 | [diff] [blame] | 365 | } |
| 366 | |
Holger Hans Peter Freyther | 7700846 | 2010-06-16 14:10:45 +0800 | [diff] [blame] | 367 | int gsm0808_page(struct gsm_bts *bts, unsigned int page_group, unsigned int mi_len, |
| 368 | uint8_t *mi, int chan_type) |
| 369 | { |
| 370 | return rsl_paging_cmd(bts, page_group, mi_len, mi, chan_type); |
| 371 | } |
| 372 | |
Holger Hans Peter Freyther | 77cd95d | 2010-11-14 16:19:48 +0100 | [diff] [blame] | 373 | static void handle_ass_compl(struct gsm_subscriber_connection *conn, |
| 374 | struct msgb *msg) |
| 375 | { |
| 376 | struct gsm48_hdr *gh; |
| 377 | struct bsc_api *api = conn->bts->network->bsc_api; |
| 378 | |
| 379 | if (conn->secondary_lchan != msg->lchan) { |
| 380 | LOGP(DMSC, LOGL_ERROR, "Assignment Compl should occur on second lchan.\n"); |
| 381 | return; |
| 382 | } |
| 383 | |
| 384 | gh = msgb_l3(msg); |
| 385 | if (msgb_l3len(msg) - sizeof(*gh) != 1) { |
Harald Welte | 4d54d0b | 2011-02-19 16:48:17 +0100 | [diff] [blame] | 386 | LOGP(DMSC, LOGL_ERROR, "Assignment Compl invalid: %lu\n", |
Holger Hans Peter Freyther | 77cd95d | 2010-11-14 16:19:48 +0100 | [diff] [blame] | 387 | msgb_l3len(msg) - sizeof(*gh)); |
| 388 | return; |
| 389 | } |
| 390 | |
| 391 | /* swap channels */ |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 392 | osmo_timer_del(&conn->T10); |
Holger Hans Peter Freyther | 77cd95d | 2010-11-14 16:19:48 +0100 | [diff] [blame] | 393 | |
| 394 | lchan_release(conn->lchan, 0, 1); |
| 395 | conn->lchan = conn->secondary_lchan; |
| 396 | conn->secondary_lchan = NULL; |
| 397 | |
| 398 | if (is_ipaccess_bts(conn->bts) && conn->lchan->tch_mode != GSM48_CMODE_SIGN) |
| 399 | rsl_ipacc_crcx(conn->lchan); |
| 400 | |
Holger Hans Peter Freyther | 40aac3f | 2011-12-27 12:31:02 +0100 | [diff] [blame] | 401 | api->assign_compl(conn, gh->data[0], |
Holger Hans Peter Freyther | 77cd95d | 2010-11-14 16:19:48 +0100 | [diff] [blame] | 402 | lchan_to_chosen_channel(conn->lchan), |
| 403 | conn->lchan->encr.alg_id, |
| 404 | chan_mode_to_speech(conn->lchan)); |
| 405 | } |
| 406 | |
| 407 | static void handle_ass_fail(struct gsm_subscriber_connection *conn, |
| 408 | struct msgb *msg) |
| 409 | { |
| 410 | struct bsc_api *api = conn->bts->network->bsc_api; |
| 411 | uint8_t *rr_failure; |
| 412 | struct gsm48_hdr *gh; |
| 413 | |
| 414 | |
| 415 | if (conn->lchan != msg->lchan) { |
| 416 | LOGP(DMSC, LOGL_ERROR, "Assignment failure should occur on primary lchan.\n"); |
| 417 | return; |
| 418 | } |
| 419 | |
| 420 | /* stop the timer and release it */ |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 421 | osmo_timer_del(&conn->T10); |
Holger Hans Peter Freyther | 77cd95d | 2010-11-14 16:19:48 +0100 | [diff] [blame] | 422 | lchan_release(conn->secondary_lchan, 0, 1); |
| 423 | conn->secondary_lchan = NULL; |
| 424 | |
| 425 | gh = msgb_l3(msg); |
| 426 | if (msgb_l3len(msg) - sizeof(*gh) != 1) { |
Harald Welte | 4d54d0b | 2011-02-19 16:48:17 +0100 | [diff] [blame] | 427 | LOGP(DMSC, LOGL_ERROR, "assignemnt failure unhandled: %lu\n", |
Holger Hans Peter Freyther | 77cd95d | 2010-11-14 16:19:48 +0100 | [diff] [blame] | 428 | msgb_l3len(msg) - sizeof(*gh)); |
| 429 | rr_failure = NULL; |
| 430 | } else { |
| 431 | rr_failure = &gh->data[0]; |
| 432 | } |
| 433 | |
| 434 | api->assign_fail(conn, |
| 435 | GSM0808_CAUSE_RADIO_INTERFACE_MESSAGE_FAILURE, |
| 436 | rr_failure); |
| 437 | } |
| 438 | |
Harald Welte | 95e862c | 2012-01-23 10:28:35 +0100 | [diff] [blame] | 439 | static void handle_classmark_chg(struct gsm_subscriber_connection *conn, |
| 440 | struct msgb *msg) |
| 441 | { |
| 442 | struct bsc_api *api = msg->lchan->ts->trx->bts->network->bsc_api; |
| 443 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 444 | unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh); |
| 445 | uint8_t cm2_len, cm3_len = 0; |
| 446 | uint8_t *cm2, *cm3 = NULL; |
| 447 | |
| 448 | DEBUGP(DRR, "CLASSMARK CHANGE "); |
| 449 | |
| 450 | /* classmark 2 */ |
| 451 | cm2_len = gh->data[0]; |
| 452 | cm2 = &gh->data[1]; |
| 453 | DEBUGPC(DRR, "CM2(len=%u) ", cm2_len); |
| 454 | |
| 455 | if (payload_len > cm2_len + 1) { |
| 456 | /* we must have a classmark3 */ |
| 457 | if (gh->data[cm2_len+1] != 0x20) { |
| 458 | DEBUGPC(DRR, "ERR CM3 TAG\n"); |
Harald Welte | cf149ee | 2012-01-23 16:40:24 +0100 | [diff] [blame] | 459 | return; |
Harald Welte | 95e862c | 2012-01-23 10:28:35 +0100 | [diff] [blame] | 460 | } |
| 461 | if (cm2_len > 3) { |
| 462 | DEBUGPC(DRR, "CM2 too long!\n"); |
Harald Welte | cf149ee | 2012-01-23 16:40:24 +0100 | [diff] [blame] | 463 | return; |
Harald Welte | 95e862c | 2012-01-23 10:28:35 +0100 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | cm3_len = gh->data[cm2_len+2]; |
| 467 | cm3 = &gh->data[cm2_len+3]; |
| 468 | if (cm3_len > 14) { |
| 469 | DEBUGPC(DRR, "CM3 len %u too long!\n", cm3_len); |
Harald Welte | cf149ee | 2012-01-23 16:40:24 +0100 | [diff] [blame] | 470 | return; |
Harald Welte | 95e862c | 2012-01-23 10:28:35 +0100 | [diff] [blame] | 471 | } |
| 472 | DEBUGPC(DRR, "CM3(len=%u)\n", cm3_len); |
| 473 | } |
| 474 | api->classmark_chg(conn, cm2, cm2_len, cm3, cm3_len); |
| 475 | } |
| 476 | |
Harald Welte | cf149ee | 2012-01-23 16:40:24 +0100 | [diff] [blame] | 477 | /* Chapter 9.1.16 Handover complete */ |
| 478 | static void handle_rr_ho_compl(struct msgb *msg) |
| 479 | { |
| 480 | struct lchan_signal_data sig; |
| 481 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 482 | |
| 483 | DEBUGP(DRR, "HANDOVER COMPLETE cause = %s\n", |
| 484 | rr_cause_name(gh->data[0])); |
| 485 | |
| 486 | sig.lchan = msg->lchan; |
| 487 | sig.mr = NULL; |
| 488 | osmo_signal_dispatch(SS_LCHAN, S_LCHAN_HANDOVER_COMPL, &sig); |
| 489 | /* FIXME: release old channel */ |
| 490 | } |
| 491 | |
| 492 | /* Chapter 9.1.17 Handover Failure */ |
| 493 | static void handle_rr_ho_fail(struct msgb *msg) |
| 494 | { |
| 495 | struct lchan_signal_data sig; |
| 496 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 497 | |
| 498 | DEBUGP(DRR, "HANDOVER FAILED cause = %s\n", |
| 499 | rr_cause_name(gh->data[0])); |
| 500 | |
| 501 | sig.lchan = msg->lchan; |
| 502 | sig.mr = NULL; |
| 503 | osmo_signal_dispatch(SS_LCHAN, S_LCHAN_HANDOVER_FAIL, &sig); |
| 504 | /* FIXME: release allocated new channel */ |
| 505 | } |
| 506 | |
| 507 | |
Holger Hans Peter Freyther | 46caa30 | 2010-11-04 12:18:00 +0100 | [diff] [blame] | 508 | static void dispatch_dtap(struct gsm_subscriber_connection *conn, |
| 509 | uint8_t link_id, struct msgb *msg) |
Holger Hans Peter Freyther | 6c199e7 | 2010-11-04 11:18:03 +0100 | [diff] [blame] | 510 | { |
| 511 | struct bsc_api *api = msg->lchan->ts->trx->bts->network->bsc_api; |
| 512 | struct gsm48_hdr *gh; |
| 513 | uint8_t pdisc; |
| 514 | int rc; |
| 515 | |
| 516 | if (msgb_l3len(msg) < sizeof(*gh)) { |
| 517 | LOGP(DMSC, LOGL_ERROR, "Message too short for a GSM48 header.\n"); |
| 518 | return; |
| 519 | } |
| 520 | |
| 521 | gh = msgb_l3(msg); |
| 522 | pdisc = gh->proto_discr & 0x0f; |
Harald Welte | cf149ee | 2012-01-23 16:40:24 +0100 | [diff] [blame] | 523 | |
| 524 | /* the idea is to handle all RR messages here, and only hand |
| 525 | * MM/CC/SMS-CP/LCS up to the MSC. Some messages like PAGING |
| 526 | * RESPONSE or CM SERVICE REQUEST will not be covered here, as |
| 527 | * they are only possible in the first L3 message of each L2 |
| 528 | * channel, i.e. 'conn' will not exist and gsm0408_rcvmsg() |
| 529 | * will call api->compl_l3() for it */ |
Holger Hans Peter Freyther | 6c199e7 | 2010-11-04 11:18:03 +0100 | [diff] [blame] | 530 | switch (pdisc) { |
| 531 | case GSM48_PDISC_RR: |
| 532 | switch (gh->msg_type) { |
Harald Welte | cf149ee | 2012-01-23 16:40:24 +0100 | [diff] [blame] | 533 | case GSM48_MT_RR_GPRS_SUSP_REQ: |
| 534 | DEBUGP(DRR, "GRPS SUSPEND REQUEST\n"); |
| 535 | break; |
| 536 | case GSM48_MT_RR_STATUS: |
| 537 | DEBUGP(DRR, "RR STATUS (cause: %s)\n", |
| 538 | rr_cause_name(gh->data[0])); |
| 539 | break; |
| 540 | case GSM48_MT_RR_MEAS_REP: |
| 541 | /* This shouldn't actually end up here, as RSL treats |
| 542 | * L3 Info of 08.58 MEASUREMENT REPORT different by calling |
| 543 | * directly into gsm48_parse_meas_rep */ |
| 544 | LOGP(LOGL_ERROR, DMEAS, "DIRECT GSM48 MEASUREMENT REPORT ?!? "); |
| 545 | break; |
| 546 | case GSM48_MT_RR_HANDO_COMPL: |
| 547 | handle_rr_ho_compl(msg); |
| 548 | break; |
| 549 | case GSM48_MT_RR_HANDO_FAIL: |
| 550 | handle_rr_ho_fail(msg); |
| 551 | break; |
Holger Hans Peter Freyther | 6c199e7 | 2010-11-04 11:18:03 +0100 | [diff] [blame] | 552 | case GSM48_MT_RR_CIPH_M_COMPL: |
| 553 | if (api->cipher_mode_compl) |
Harald Welte | cf149ee | 2012-01-23 16:40:24 +0100 | [diff] [blame] | 554 | api->cipher_mode_compl(conn, msg, |
Holger Hans Peter Freyther | 6c199e7 | 2010-11-04 11:18:03 +0100 | [diff] [blame] | 555 | conn->lchan->encr.alg_id); |
| 556 | break; |
| 557 | case GSM48_MT_RR_ASS_COMPL: |
Holger Hans Peter Freyther | 77cd95d | 2010-11-14 16:19:48 +0100 | [diff] [blame] | 558 | handle_ass_compl(conn, msg); |
Holger Hans Peter Freyther | 6c199e7 | 2010-11-04 11:18:03 +0100 | [diff] [blame] | 559 | break; |
| 560 | case GSM48_MT_RR_ASS_FAIL: |
Holger Hans Peter Freyther | 77cd95d | 2010-11-14 16:19:48 +0100 | [diff] [blame] | 561 | handle_ass_fail(conn, msg); |
Holger Hans Peter Freyther | 6c199e7 | 2010-11-04 11:18:03 +0100 | [diff] [blame] | 562 | break; |
| 563 | case GSM48_MT_RR_CHAN_MODE_MODIF_ACK: |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 564 | osmo_timer_del(&conn->T10); |
Holger Hans Peter Freyther | 6c199e7 | 2010-11-04 11:18:03 +0100 | [diff] [blame] | 565 | rc = gsm48_rx_rr_modif_ack(msg); |
Holger Hans Peter Freyther | 40aac3f | 2011-12-27 12:31:02 +0100 | [diff] [blame] | 566 | if (rc < 0) { |
Holger Hans Peter Freyther | 6c199e7 | 2010-11-04 11:18:03 +0100 | [diff] [blame] | 567 | api->assign_fail(conn, |
Holger Hans Peter Freyther | 45b575f | 2010-11-05 10:37:17 +0100 | [diff] [blame] | 568 | GSM0808_CAUSE_NO_RADIO_RESOURCE_AVAILABLE, |
| 569 | NULL); |
Holger Hans Peter Freyther | 40aac3f | 2011-12-27 12:31:02 +0100 | [diff] [blame] | 570 | } else if (rc >= 0) { |
Holger Hans Peter Freyther | b094171 | 2010-11-04 12:06:57 +0100 | [diff] [blame] | 571 | api->assign_compl(conn, 0, |
| 572 | lchan_to_chosen_channel(conn->lchan), |
| 573 | conn->lchan->encr.alg_id, |
| 574 | chan_mode_to_speech(conn->lchan)); |
Holger Hans Peter Freyther | 40aac3f | 2011-12-27 12:31:02 +0100 | [diff] [blame] | 575 | } |
Holger Hans Peter Freyther | 6c199e7 | 2010-11-04 11:18:03 +0100 | [diff] [blame] | 576 | break; |
Harald Welte | 95e862c | 2012-01-23 10:28:35 +0100 | [diff] [blame] | 577 | case GSM48_MT_RR_CLSM_CHG: |
| 578 | handle_classmark_chg(conn, msg); |
Harald Welte | 95e862c | 2012-01-23 10:28:35 +0100 | [diff] [blame] | 579 | break; |
Harald Welte | cf149ee | 2012-01-23 16:40:24 +0100 | [diff] [blame] | 580 | default: |
| 581 | /* Normally, a MSC should never receive RR |
| 582 | * messages, but we'd rather forward what we |
| 583 | * don't know than drop it... */ |
| 584 | LOGP(DRR, LOGL_NOTICE, "BSC: Passing unknown 04.08 " |
| 585 | "RR message type 0x%02x to MSC\n", gh->msg_type); |
| 586 | if (api->dtap) |
| 587 | api->dtap(conn, link_id, msg); |
Holger Hans Peter Freyther | 6c199e7 | 2010-11-04 11:18:03 +0100 | [diff] [blame] | 588 | } |
| 589 | break; |
Harald Welte | cf149ee | 2012-01-23 16:40:24 +0100 | [diff] [blame] | 590 | default: |
| 591 | if (api->dtap) |
| 592 | api->dtap(conn, link_id, msg); |
Holger Hans Peter Freyther | 6c199e7 | 2010-11-04 11:18:03 +0100 | [diff] [blame] | 593 | break; |
| 594 | } |
Holger Hans Peter Freyther | 6c199e7 | 2010-11-04 11:18:03 +0100 | [diff] [blame] | 595 | } |
| 596 | |
Harald Welte | b70bd52 | 2011-09-13 00:02:09 +0100 | [diff] [blame] | 597 | /*! \brief RSL has received a DATA INDICATION with L3 from MS */ |
Holger Hans Peter Freyther | 9764331 | 2010-06-17 16:41:25 +0800 | [diff] [blame] | 598 | int gsm0408_rcvmsg(struct msgb *msg, uint8_t link_id) |
| 599 | { |
| 600 | int rc; |
Holger Hans Peter Freyther | 9764331 | 2010-06-17 16:41:25 +0800 | [diff] [blame] | 601 | struct bsc_api *api = msg->lchan->ts->trx->bts->network->bsc_api; |
Holger Hans Peter Freyther | 2412a07 | 2010-06-28 15:47:12 +0800 | [diff] [blame] | 602 | struct gsm_lchan *lchan; |
Holger Hans Peter Freyther | 9764331 | 2010-06-17 16:41:25 +0800 | [diff] [blame] | 603 | |
Holger Hans Peter Freyther | 2412a07 | 2010-06-28 15:47:12 +0800 | [diff] [blame] | 604 | lchan = msg->lchan; |
Holger Hans Peter Freyther | 49ab278 | 2010-07-05 11:38:37 +0800 | [diff] [blame] | 605 | if (lchan->state != LCHAN_S_ACTIVE) { |
Harald Welte | 2cafc4b | 2010-12-24 13:06:11 +0100 | [diff] [blame] | 606 | LOGP(DRSL, LOGL_INFO, "Got data in non active state(%s), " |
| 607 | "discarding.\n", gsm_lchans_name(lchan->state)); |
Holger Hans Peter Freyther | 49ab278 | 2010-07-05 11:38:37 +0800 | [diff] [blame] | 608 | return -1; |
| 609 | } |
| 610 | |
| 611 | |
Holger Hans Peter Freyther | 2412a07 | 2010-06-28 15:47:12 +0800 | [diff] [blame] | 612 | if (lchan->conn) { |
Harald Welte | b70bd52 | 2011-09-13 00:02:09 +0100 | [diff] [blame] | 613 | /* if we already have a connection, forward via DTAP to |
| 614 | * MSC */ |
Holger Hans Peter Freyther | 46caa30 | 2010-11-04 12:18:00 +0100 | [diff] [blame] | 615 | dispatch_dtap(lchan->conn, link_id, msg); |
Holger Hans Peter Freyther | 9764331 | 2010-06-17 16:41:25 +0800 | [diff] [blame] | 616 | } else { |
Harald Welte | b70bd52 | 2011-09-13 00:02:09 +0100 | [diff] [blame] | 617 | /* allocate a new connection */ |
Holger Hans Peter Freyther | 2412a07 | 2010-06-28 15:47:12 +0800 | [diff] [blame] | 618 | rc = BSC_API_CONN_POL_REJECT; |
| 619 | lchan->conn = subscr_con_allocate(msg->lchan); |
Holger Hans Peter Freyther | 05bbec7 | 2011-01-16 18:15:26 +0100 | [diff] [blame] | 620 | if (!lchan->conn) { |
| 621 | lchan_release(lchan, 0, 0); |
| 622 | return -1; |
| 623 | } |
Holger Hans Peter Freyther | 2412a07 | 2010-06-28 15:47:12 +0800 | [diff] [blame] | 624 | |
Harald Welte | b70bd52 | 2011-09-13 00:02:09 +0100 | [diff] [blame] | 625 | /* fwd via bsc_api to send COMPLETE L3 INFO to MSC */ |
Holger Hans Peter Freyther | 05bbec7 | 2011-01-16 18:15:26 +0100 | [diff] [blame] | 626 | rc = api->compl_l3(lchan->conn, msg, 0); |
Holger Hans Peter Freyther | 2412a07 | 2010-06-28 15:47:12 +0800 | [diff] [blame] | 627 | |
| 628 | if (rc != BSC_API_CONN_POL_ACCEPT) { |
Holger Hans Peter Freyther | 8d216c7 | 2010-11-15 10:33:59 +0100 | [diff] [blame] | 629 | lchan->conn->lchan = NULL; |
Holger Hans Peter Freyther | 2412a07 | 2010-06-28 15:47:12 +0800 | [diff] [blame] | 630 | subscr_con_free(lchan->conn); |
Holger Hans Peter Freyther | 4049455 | 2010-06-28 17:09:29 +0800 | [diff] [blame] | 631 | lchan_release(lchan, 0, 0); |
Holger Hans Peter Freyther | 2412a07 | 2010-06-28 15:47:12 +0800 | [diff] [blame] | 632 | } |
Holger Hans Peter Freyther | 9764331 | 2010-06-17 16:41:25 +0800 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | return 0; |
| 636 | } |
| 637 | |
Harald Welte | b70bd52 | 2011-09-13 00:02:09 +0100 | [diff] [blame] | 638 | /*! \brief We received a GSM 08.08 CIPHER MODE from the MSC */ |
Holger Hans Peter Freyther | dbc62db | 2010-07-23 16:55:24 +0800 | [diff] [blame] | 639 | int gsm0808_cipher_mode(struct gsm_subscriber_connection *conn, int cipher, |
Holger Hans Peter Freyther | 2f169d6 | 2010-11-10 09:42:50 +0100 | [diff] [blame] | 640 | const uint8_t *key, int len, int include_imeisv) |
Holger Hans Peter Freyther | dbc62db | 2010-07-23 16:55:24 +0800 | [diff] [blame] | 641 | { |
Holger Hans Peter Freyther | 4be7d41 | 2010-11-10 10:25:34 +0100 | [diff] [blame] | 642 | if (cipher > 0 && key == NULL) { |
| 643 | LOGP(DRSL, LOGL_ERROR, "Need to have an encrytpion key.\n"); |
| 644 | return -1; |
| 645 | } |
| 646 | |
| 647 | if (len > MAX_A5_KEY_LEN) { |
| 648 | LOGP(DRSL, LOGL_ERROR, "The key is too long: %d\n", len); |
| 649 | return -1; |
| 650 | } |
| 651 | |
| 652 | conn->lchan->encr.alg_id = RSL_ENC_ALG_A5(cipher); |
| 653 | if (key) { |
| 654 | conn->lchan->encr.key_len = len; |
| 655 | memcpy(conn->lchan->encr.key, key, len); |
| 656 | } |
| 657 | |
| 658 | return gsm48_send_rr_ciph_mode(conn->lchan, include_imeisv); |
Holger Hans Peter Freyther | dbc62db | 2010-07-23 16:55:24 +0800 | [diff] [blame] | 659 | } |
| 660 | |
Holger Hans Peter Freyther | aaa40b8 | 2010-09-16 20:48:15 +0800 | [diff] [blame] | 661 | /* |
| 662 | * Release all occupied RF Channels but stay around for more. |
| 663 | */ |
Holger Hans Peter Freyther | adb6e1c | 2010-09-18 06:44:24 +0800 | [diff] [blame] | 664 | int gsm0808_clear(struct gsm_subscriber_connection *conn) |
Holger Hans Peter Freyther | 4049455 | 2010-06-28 17:09:29 +0800 | [diff] [blame] | 665 | { |
Holger Hans Peter Freyther | 1f69b46 | 2010-09-16 22:49:37 +0800 | [diff] [blame] | 666 | if (conn->ho_lchan) |
Holger Hans Peter Freyther | ebd50a6 | 2010-12-27 13:46:48 +0100 | [diff] [blame] | 667 | bsc_clear_handover(conn, 1); |
Holger Hans Peter Freyther | d065165 | 2010-06-30 12:17:35 +0800 | [diff] [blame] | 668 | |
Holger Hans Peter Freyther | 77cd95d | 2010-11-14 16:19:48 +0100 | [diff] [blame] | 669 | if (conn->secondary_lchan) |
| 670 | lchan_release(conn->secondary_lchan, 0, 1); |
| 671 | |
Holger Hans Peter Freyther | abf962b | 2010-11-14 16:04:46 +0100 | [diff] [blame] | 672 | if (conn->lchan) |
Holger Hans Peter Freyther | 1f69b46 | 2010-09-16 22:49:37 +0800 | [diff] [blame] | 673 | lchan_release(conn->lchan, 1, 0); |
Holger Hans Peter Freyther | 1f69b46 | 2010-09-16 22:49:37 +0800 | [diff] [blame] | 674 | |
Holger Hans Peter Freyther | aaa40b8 | 2010-09-16 20:48:15 +0800 | [diff] [blame] | 675 | conn->lchan = NULL; |
Holger Hans Peter Freyther | 77cd95d | 2010-11-14 16:19:48 +0100 | [diff] [blame] | 676 | conn->secondary_lchan = NULL; |
Holger Hans Peter Freyther | aaa40b8 | 2010-09-16 20:48:15 +0800 | [diff] [blame] | 677 | conn->ho_lchan = NULL; |
| 678 | conn->bts = NULL; |
| 679 | |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 680 | osmo_timer_del(&conn->T10); |
Holger Hans Peter Freyther | 77cd95d | 2010-11-14 16:19:48 +0100 | [diff] [blame] | 681 | |
Holger Hans Peter Freyther | 4049455 | 2010-06-28 17:09:29 +0800 | [diff] [blame] | 682 | return 0; |
| 683 | } |
| 684 | |
Holger Hans Peter Freyther | 9c595b7 | 2010-06-15 11:20:52 +0800 | [diff] [blame] | 685 | static void send_sapi_reject(struct gsm_subscriber_connection *conn, int link_id) |
| 686 | { |
| 687 | struct bsc_api *api; |
| 688 | |
Holger Hans Peter Freyther | 2412a07 | 2010-06-28 15:47:12 +0800 | [diff] [blame] | 689 | if (!conn) |
| 690 | return; |
| 691 | |
Holger Hans Peter Freyther | 9c595b7 | 2010-06-15 11:20:52 +0800 | [diff] [blame] | 692 | api = conn->bts->network->bsc_api; |
| 693 | if (!api || !api->sapi_n_reject) |
| 694 | return; |
| 695 | |
| 696 | api->sapi_n_reject(conn, link_id); |
| 697 | } |
| 698 | |
| 699 | static void rll_ind_cb(struct gsm_lchan *lchan, uint8_t link_id, void *_data, enum bsc_rllr_ind rllr_ind) |
| 700 | { |
| 701 | struct msgb *msg = _data; |
| 702 | |
Holger Hans Peter Freyther | 7e50c81 | 2010-11-10 10:59:05 +0100 | [diff] [blame] | 703 | /* |
| 704 | * There seems to be a small window that the RLL timer can |
| 705 | * fire after a lchan_release call and before the S_CHALLOC_FREED |
| 706 | * is called. Check if a conn is set before proceeding. |
| 707 | */ |
| 708 | if (!lchan->conn) |
| 709 | return; |
| 710 | |
Holger Hans Peter Freyther | 9c595b7 | 2010-06-15 11:20:52 +0800 | [diff] [blame] | 711 | switch (rllr_ind) { |
| 712 | case BSC_RLLR_IND_EST_CONF: |
| 713 | rsl_data_request(msg, OBSC_LINKID_CB(msg)); |
| 714 | break; |
| 715 | case BSC_RLLR_IND_REL_IND: |
| 716 | case BSC_RLLR_IND_ERR_IND: |
| 717 | case BSC_RLLR_IND_TIMEOUT: |
Holger Hans Peter Freyther | 2412a07 | 2010-06-28 15:47:12 +0800 | [diff] [blame] | 718 | send_sapi_reject(lchan->conn, OBSC_LINKID_CB(msg)); |
Holger Hans Peter Freyther | 9c595b7 | 2010-06-15 11:20:52 +0800 | [diff] [blame] | 719 | msgb_free(msg); |
| 720 | break; |
| 721 | } |
| 722 | } |
| 723 | |
Holger Hans Peter Freyther | abcddf1 | 2010-06-14 18:20:15 +0800 | [diff] [blame] | 724 | static int bsc_handle_lchan_signal(unsigned int subsys, unsigned int signal, |
| 725 | void *handler_data, void *signal_data) |
| 726 | { |
| 727 | struct bsc_api *bsc; |
| 728 | struct gsm_lchan *lchan; |
Holger Hans Peter Freyther | 08eebd5 | 2010-12-27 13:28:20 +0100 | [diff] [blame] | 729 | struct lchan_signal_data *lchan_data; |
Holger Hans Peter Freyther | abcddf1 | 2010-06-14 18:20:15 +0800 | [diff] [blame] | 730 | |
Holger Hans Peter Freyther | f05750c | 2010-11-14 19:29:42 +0100 | [diff] [blame] | 731 | if (subsys != SS_LCHAN) |
Holger Hans Peter Freyther | abcddf1 | 2010-06-14 18:20:15 +0800 | [diff] [blame] | 732 | return 0; |
| 733 | |
Holger Hans Peter Freyther | 9ae7b29 | 2010-12-27 12:30:12 +0100 | [diff] [blame] | 734 | |
Holger Hans Peter Freyther | 08eebd5 | 2010-12-27 13:28:20 +0100 | [diff] [blame] | 735 | lchan_data = signal_data; |
| 736 | if (!lchan_data->lchan || !lchan_data->lchan->conn) |
Holger Hans Peter Freyther | abcddf1 | 2010-06-14 18:20:15 +0800 | [diff] [blame] | 737 | return 0; |
| 738 | |
Holger Hans Peter Freyther | 08eebd5 | 2010-12-27 13:28:20 +0100 | [diff] [blame] | 739 | lchan = lchan_data->lchan; |
Holger Hans Peter Freyther | abcddf1 | 2010-06-14 18:20:15 +0800 | [diff] [blame] | 740 | bsc = lchan->ts->trx->bts->network->bsc_api; |
Holger Hans Peter Freyther | aaa40b8 | 2010-09-16 20:48:15 +0800 | [diff] [blame] | 741 | if (!bsc) |
Holger Hans Peter Freyther | abcddf1 | 2010-06-14 18:20:15 +0800 | [diff] [blame] | 742 | return 0; |
| 743 | |
Holger Hans Peter Freyther | f05750c | 2010-11-14 19:29:42 +0100 | [diff] [blame] | 744 | switch (signal) { |
| 745 | case S_LCHAN_UNEXPECTED_RELEASE: |
| 746 | handle_release(lchan->conn, bsc, lchan); |
| 747 | break; |
Holger Hans Peter Freyther | 77cd95d | 2010-11-14 16:19:48 +0100 | [diff] [blame] | 748 | case S_LCHAN_ACTIVATE_ACK: |
| 749 | handle_chan_ack(lchan->conn, bsc, lchan); |
| 750 | break; |
| 751 | case S_LCHAN_ACTIVATE_NACK: |
| 752 | handle_chan_nack(lchan->conn, bsc, lchan); |
| 753 | break; |
Holger Hans Peter Freyther | f05750c | 2010-11-14 19:29:42 +0100 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | return 0; |
| 757 | } |
| 758 | |
| 759 | static void handle_release(struct gsm_subscriber_connection *conn, |
| 760 | struct bsc_api *bsc, struct gsm_lchan *lchan) |
| 761 | { |
| 762 | int destruct = 1; |
| 763 | |
Holger Hans Peter Freyther | 77cd95d | 2010-11-14 16:19:48 +0100 | [diff] [blame] | 764 | if (conn->secondary_lchan == lchan) { |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 765 | osmo_timer_del(&conn->T10); |
Holger Hans Peter Freyther | 77cd95d | 2010-11-14 16:19:48 +0100 | [diff] [blame] | 766 | conn->secondary_lchan = NULL; |
| 767 | |
| 768 | bsc->assign_fail(conn, |
| 769 | GSM0808_CAUSE_RADIO_INTERFACE_FAILURE, |
| 770 | NULL); |
| 771 | } |
| 772 | |
Holger Hans Peter Freyther | 77cd95d | 2010-11-14 16:19:48 +0100 | [diff] [blame] | 773 | /* clear the connection now */ |
| 774 | if (bsc->clear_request) |
| 775 | destruct = bsc->clear_request(conn, 0); |
| 776 | |
Harald Welte | 6349b7f | 2010-12-26 22:12:53 +0100 | [diff] [blame] | 777 | /* now give up all channels */ |
| 778 | if (conn->lchan == lchan) |
| 779 | conn->lchan = NULL; |
Holger Hans Peter Freyther | ebd50a6 | 2010-12-27 13:46:48 +0100 | [diff] [blame] | 780 | if (conn->ho_lchan == lchan) { |
| 781 | bsc_clear_handover(conn, 0); |
Harald Welte | 6349b7f | 2010-12-26 22:12:53 +0100 | [diff] [blame] | 782 | conn->ho_lchan = NULL; |
Holger Hans Peter Freyther | ebd50a6 | 2010-12-27 13:46:48 +0100 | [diff] [blame] | 783 | } |
Harald Welte | 6349b7f | 2010-12-26 22:12:53 +0100 | [diff] [blame] | 784 | lchan->conn = NULL; |
Holger Hans Peter Freyther | 77cd95d | 2010-11-14 16:19:48 +0100 | [diff] [blame] | 785 | |
Holger Hans Peter Freyther | aaa40b8 | 2010-09-16 20:48:15 +0800 | [diff] [blame] | 786 | gsm0808_clear(conn); |
| 787 | |
Holger Hans Peter Freyther | 05c6884 | 2010-11-03 19:01:58 +0100 | [diff] [blame] | 788 | if (destruct) |
| 789 | subscr_con_free(conn); |
Holger Hans Peter Freyther | abcddf1 | 2010-06-14 18:20:15 +0800 | [diff] [blame] | 790 | } |
| 791 | |
Holger Hans Peter Freyther | 77cd95d | 2010-11-14 16:19:48 +0100 | [diff] [blame] | 792 | static void handle_chan_ack(struct gsm_subscriber_connection *conn, |
| 793 | struct bsc_api *api, struct gsm_lchan *lchan) |
| 794 | { |
| 795 | if (conn->secondary_lchan != lchan) |
| 796 | return; |
| 797 | |
| 798 | LOGP(DMSC, LOGL_NOTICE, "Sending assignment on chan: %p\n", lchan); |
Dieter Spaar | 6b35377 | 2011-09-03 14:11:24 +0200 | [diff] [blame] | 799 | gsm48_send_rr_ass_cmd(conn->lchan, lchan, lchan->ms_power); |
Holger Hans Peter Freyther | 77cd95d | 2010-11-14 16:19:48 +0100 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | static void handle_chan_nack(struct gsm_subscriber_connection *conn, |
| 803 | struct bsc_api *api, struct gsm_lchan *lchan) |
| 804 | { |
| 805 | if (conn->secondary_lchan != lchan) |
| 806 | return; |
| 807 | |
| 808 | LOGP(DMSC, LOGL_ERROR, "Channel activation failed. Waiting for timeout now\n"); |
| 809 | conn->secondary_lchan->conn = NULL; |
| 810 | conn->secondary_lchan = NULL; |
| 811 | } |
| 812 | |
Holger Hans Peter Freyther | abcddf1 | 2010-06-14 18:20:15 +0800 | [diff] [blame] | 813 | static __attribute__((constructor)) void on_dso_load_bsc(void) |
| 814 | { |
Pablo Neira Ayuso | bbc5b99 | 2011-05-06 12:12:31 +0200 | [diff] [blame] | 815 | osmo_signal_register_handler(SS_LCHAN, bsc_handle_lchan_signal, NULL); |
Holger Hans Peter Freyther | 0cfbe26 | 2010-06-15 11:20:24 +0800 | [diff] [blame] | 816 | } |