Holger Hans Peter Freyther | 3485feb | 2010-11-09 23:28:33 +0100 | [diff] [blame] | 1 | /* GSM 08.08 BSSMAP handling */ |
Holger Hans Peter Freyther | d2361d9 | 2012-12-03 15:32:54 +0100 | [diff] [blame] | 2 | /* (C) 2009-2012 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 3 | * (C) 2009-2012 by On-Waves |
Holger Hans Peter Freyther | 3485feb | 2010-11-09 23:28:33 +0100 | [diff] [blame] | 4 | * All Rights Reserved |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 9af6ddf | 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 |
Holger Hans Peter Freyther | 3485feb | 2010-11-09 23:28:33 +0100 | [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 | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 14 | * GNU Affero General Public License for more details. |
Holger Hans Peter Freyther | 3485feb | 2010-11-09 23:28:33 +0100 | [diff] [blame] | 15 | * |
Harald Welte | 9af6ddf | 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/>. |
Holger Hans Peter Freyther | 3485feb | 2010-11-09 23:28:33 +0100 | [diff] [blame] | 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include <openbsc/osmo_bsc.h> |
Holger Hans Peter Freyther | 890dfc5 | 2010-11-10 09:24:37 +0100 | [diff] [blame] | 22 | #include <openbsc/osmo_bsc_grace.h> |
Holger Hans Peter Freyther | 50c579b | 2010-11-10 10:07:30 +0100 | [diff] [blame] | 23 | #include <openbsc/osmo_msc_data.h> |
Holger Hans Peter Freyther | 3485feb | 2010-11-09 23:28:33 +0100 | [diff] [blame] | 24 | #include <openbsc/debug.h> |
Holger Hans Peter Freyther | 9c838ae | 2010-11-15 09:16:09 +0100 | [diff] [blame] | 25 | #include <openbsc/gsm_subscriber.h> |
Holger Hans Peter Freyther | 50c579b | 2010-11-10 10:07:30 +0100 | [diff] [blame] | 26 | #include <openbsc/mgcp.h> |
Holger Hans Peter Freyther | 9c838ae | 2010-11-15 09:16:09 +0100 | [diff] [blame] | 27 | #include <openbsc/paging.h> |
Holger Hans Peter Freyther | 3485feb | 2010-11-09 23:28:33 +0100 | [diff] [blame] | 28 | |
Harald Welte | d36ff76 | 2011-03-23 18:26:56 +0100 | [diff] [blame] | 29 | #include <osmocom/gsm/protocol/gsm_08_08.h> |
| 30 | #include <osmocom/gsm/gsm0808.h> |
Holger Hans Peter Freyther | 3485feb | 2010-11-09 23:28:33 +0100 | [diff] [blame] | 31 | |
Holger Hans Peter Freyther | 890dfc5 | 2010-11-10 09:24:37 +0100 | [diff] [blame] | 32 | #include <arpa/inet.h> |
| 33 | |
| 34 | static uint16_t read_data16(const uint8_t *data) |
| 35 | { |
| 36 | uint16_t res; |
| 37 | |
| 38 | memcpy(&res, data, sizeof(res)); |
| 39 | return res; |
| 40 | } |
| 41 | |
Holger Hans Peter Freyther | 50c579b | 2010-11-10 10:07:30 +0100 | [diff] [blame] | 42 | /* |
| 43 | * helpers for the assignment command |
| 44 | */ |
| 45 | enum gsm0808_permitted_speech audio_support_to_gsm88(struct gsm_audio_support *audio) |
| 46 | { |
| 47 | if (audio->hr) { |
| 48 | switch (audio->ver) { |
| 49 | case 1: |
| 50 | return GSM0808_PERM_HR1; |
| 51 | break; |
| 52 | case 2: |
| 53 | return GSM0808_PERM_HR2; |
| 54 | break; |
| 55 | case 3: |
| 56 | return GSM0808_PERM_HR3; |
| 57 | break; |
| 58 | default: |
| 59 | LOGP(DMSC, LOGL_ERROR, "Wrong speech mode: %d\n", audio->ver); |
| 60 | return GSM0808_PERM_FR1; |
| 61 | } |
| 62 | } else { |
| 63 | switch (audio->ver) { |
| 64 | case 1: |
| 65 | return GSM0808_PERM_FR1; |
| 66 | break; |
| 67 | case 2: |
| 68 | return GSM0808_PERM_FR2; |
| 69 | break; |
| 70 | case 3: |
| 71 | return GSM0808_PERM_FR3; |
| 72 | break; |
| 73 | default: |
| 74 | LOGP(DMSC, LOGL_ERROR, "Wrong speech mode: %d\n", audio->ver); |
| 75 | return GSM0808_PERM_HR1; |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | enum gsm48_chan_mode gsm88_to_chan_mode(enum gsm0808_permitted_speech speech) |
| 81 | { |
| 82 | switch (speech) { |
| 83 | case GSM0808_PERM_HR1: |
| 84 | case GSM0808_PERM_FR1: |
| 85 | return GSM48_CMODE_SPEECH_V1; |
| 86 | break; |
| 87 | case GSM0808_PERM_HR2: |
| 88 | case GSM0808_PERM_FR2: |
| 89 | return GSM48_CMODE_SPEECH_EFR; |
| 90 | break; |
| 91 | case GSM0808_PERM_HR3: |
| 92 | case GSM0808_PERM_FR3: |
| 93 | return GSM48_CMODE_SPEECH_AMR; |
| 94 | break; |
| 95 | } |
| 96 | |
| 97 | LOGP(DMSC, LOGL_FATAL, "Should not be reached.\n"); |
| 98 | return GSM48_CMODE_SPEECH_AMR; |
| 99 | } |
| 100 | |
Holger Hans Peter Freyther | 644b0bf | 2011-06-08 15:52:07 +0200 | [diff] [blame] | 101 | static int bssmap_handle_reset_ack(struct osmo_msc_data *msc, |
Holger Hans Peter Freyther | 890dfc5 | 2010-11-10 09:24:37 +0100 | [diff] [blame] | 102 | struct msgb *msg, unsigned int length) |
| 103 | { |
| 104 | LOGP(DMSC, LOGL_NOTICE, "Reset ACK from MSC\n"); |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | /* GSM 08.08 ยง 3.2.1.19 */ |
Holger Hans Peter Freyther | 644b0bf | 2011-06-08 15:52:07 +0200 | [diff] [blame] | 109 | static int bssmap_handle_paging(struct osmo_msc_data *msc, |
Holger Hans Peter Freyther | 890dfc5 | 2010-11-10 09:24:37 +0100 | [diff] [blame] | 110 | struct msgb *msg, unsigned int payload_length) |
| 111 | { |
Holger Hans Peter Freyther | 9c838ae | 2010-11-15 09:16:09 +0100 | [diff] [blame] | 112 | struct gsm_subscriber *subscr; |
Holger Hans Peter Freyther | 890dfc5 | 2010-11-10 09:24:37 +0100 | [diff] [blame] | 113 | struct tlv_parsed tp; |
| 114 | char mi_string[GSM48_MI_SIZE]; |
| 115 | uint32_t tmsi = GSM_RESERVED_TMSI; |
| 116 | unsigned int lac = GSM_LAC_RESERVED_ALL_BTS; |
| 117 | uint8_t data_length; |
| 118 | const uint8_t *data; |
| 119 | uint8_t chan_needed = RSL_CHANNEED_ANY; |
| 120 | |
| 121 | tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l4h + 1, payload_length - 1, 0, 0); |
| 122 | |
| 123 | if (!TLVP_PRESENT(&tp, GSM0808_IE_IMSI)) { |
Holger Hans Peter Freyther | 59043d8 | 2012-08-03 11:05:29 +0200 | [diff] [blame] | 124 | LOGP(DMSC, LOGL_ERROR, "Mandatory IMSI not present.\n"); |
Holger Hans Peter Freyther | 890dfc5 | 2010-11-10 09:24:37 +0100 | [diff] [blame] | 125 | return -1; |
| 126 | } else if ((TLVP_VAL(&tp, GSM0808_IE_IMSI)[0] & GSM_MI_TYPE_MASK) != GSM_MI_TYPE_IMSI) { |
| 127 | LOGP(DMSC, LOGL_ERROR, "Wrong content in the IMSI\n"); |
| 128 | return -1; |
| 129 | } |
| 130 | |
| 131 | if (!TLVP_PRESENT(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST)) { |
Holger Hans Peter Freyther | 59043d8 | 2012-08-03 11:05:29 +0200 | [diff] [blame] | 132 | LOGP(DMSC, LOGL_ERROR, "Mandatory CELL IDENTIFIER LIST not present.\n"); |
Holger Hans Peter Freyther | 890dfc5 | 2010-11-10 09:24:37 +0100 | [diff] [blame] | 133 | return -1; |
| 134 | } |
| 135 | |
Harald Welte | d55cef9 | 2012-01-20 17:20:51 +0100 | [diff] [blame] | 136 | if (TLVP_PRESENT(&tp, GSM0808_IE_TMSI) && |
| 137 | TLVP_LEN(&tp, GSM0808_IE_TMSI) == 4) { |
| 138 | tmsi = ntohl(*(uint32_t *) TLVP_VAL(&tp, GSM0808_IE_TMSI)); |
Holger Hans Peter Freyther | 890dfc5 | 2010-11-10 09:24:37 +0100 | [diff] [blame] | 139 | } |
| 140 | |
Holger Hans Peter Freyther | 890dfc5 | 2010-11-10 09:24:37 +0100 | [diff] [blame] | 141 | /* |
| 142 | * parse the IMSI |
| 143 | */ |
| 144 | gsm48_mi_to_string(mi_string, sizeof(mi_string), |
| 145 | TLVP_VAL(&tp, GSM0808_IE_IMSI), TLVP_LEN(&tp, GSM0808_IE_IMSI)); |
| 146 | |
| 147 | /* |
| 148 | * parse the cell identifier list |
| 149 | */ |
| 150 | data_length = TLVP_LEN(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST); |
| 151 | data = TLVP_VAL(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST); |
| 152 | |
| 153 | /* |
| 154 | * Support paging to all network or one BTS at one LAC |
| 155 | */ |
| 156 | if (data_length == 3 && data[0] == CELL_IDENT_LAC) { |
| 157 | lac = ntohs(read_data16(&data[1])); |
| 158 | } else if (data_length > 1 || (data[0] & 0x0f) != CELL_IDENT_BSS) { |
Pablo Neira Ayuso | c0d17f2 | 2011-05-07 12:12:48 +0200 | [diff] [blame] | 159 | LOGP(DMSC, LOGL_ERROR, "Unsupported Cell Identifier List: %s\n", osmo_hexdump(data, data_length)); |
Holger Hans Peter Freyther | 890dfc5 | 2010-11-10 09:24:37 +0100 | [diff] [blame] | 160 | return -1; |
| 161 | } |
| 162 | |
| 163 | if (TLVP_PRESENT(&tp, GSM0808_IE_CHANNEL_NEEDED) && TLVP_LEN(&tp, GSM0808_IE_CHANNEL_NEEDED) == 1) |
| 164 | chan_needed = TLVP_VAL(&tp, GSM0808_IE_CHANNEL_NEEDED)[0] & 0x03; |
| 165 | |
| 166 | if (TLVP_PRESENT(&tp, GSM0808_IE_EMLPP_PRIORITY)) { |
| 167 | LOGP(DMSC, LOGL_ERROR, "eMLPP is not handled\n"); |
| 168 | } |
| 169 | |
Holger Hans Peter Freyther | 644b0bf | 2011-06-08 15:52:07 +0200 | [diff] [blame] | 170 | subscr = subscr_get_or_create(msc->network, mi_string); |
Holger Hans Peter Freyther | 9c838ae | 2010-11-15 09:16:09 +0100 | [diff] [blame] | 171 | if (!subscr) { |
| 172 | LOGP(DMSC, LOGL_ERROR, "Failed to allocate a subscriber for %s\n", mi_string); |
| 173 | return -1; |
| 174 | } |
| 175 | |
| 176 | subscr->lac = lac; |
| 177 | subscr->tmsi = tmsi; |
| 178 | |
Harald Welte | cccd301 | 2011-07-11 18:18:35 +0200 | [diff] [blame] | 179 | LOGP(DMSC, LOGL_INFO, "Paging request from MSC IMSI: '%s' TMSI: '0x%x/%u' LAC: 0x%x\n", mi_string, tmsi, tmsi, lac); |
Holger Hans Peter Freyther | 644b0bf | 2011-06-08 15:52:07 +0200 | [diff] [blame] | 180 | paging_request(msc->network, subscr, chan_needed, NULL, msc); |
Holger Hans Peter Freyther | 9c838ae | 2010-11-15 09:16:09 +0100 | [diff] [blame] | 181 | return 0; |
Holger Hans Peter Freyther | 890dfc5 | 2010-11-10 09:24:37 +0100 | [diff] [blame] | 182 | } |
| 183 | |
Holger Hans Peter Freyther | f1f57a8 | 2010-11-10 09:34:47 +0100 | [diff] [blame] | 184 | /* |
| 185 | * GSM 08.08 ยง 3.1.9.1 and 3.2.1.21... |
| 186 | * release our gsm_subscriber_connection and send message |
| 187 | */ |
| 188 | static int bssmap_handle_clear_command(struct osmo_bsc_sccp_con *conn, |
| 189 | struct msgb *msg, unsigned int payload_length) |
| 190 | { |
| 191 | struct msgb *resp; |
| 192 | |
| 193 | /* TODO: handle the cause of this package */ |
| 194 | |
| 195 | if (conn->conn) { |
Harald Welte | cccd301 | 2011-07-11 18:18:35 +0200 | [diff] [blame] | 196 | LOGP(DMSC, LOGL_INFO, "Releasing all transactions on %p\n", conn); |
Holger Hans Peter Freyther | f1f57a8 | 2010-11-10 09:34:47 +0100 | [diff] [blame] | 197 | gsm0808_clear(conn->conn); |
| 198 | subscr_con_free(conn->conn); |
| 199 | conn->conn = NULL; |
| 200 | } |
| 201 | |
| 202 | /* send the clear complete message */ |
| 203 | resp = gsm0808_create_clear_complete(); |
| 204 | if (!resp) { |
| 205 | LOGP(DMSC, LOGL_ERROR, "Sending clear complete failed.\n"); |
| 206 | return -1; |
| 207 | } |
| 208 | |
| 209 | bsc_queue_for_msc(conn, resp); |
| 210 | return 0; |
| 211 | } |
| 212 | |
Holger Hans Peter Freyther | fae3c65 | 2010-11-10 09:44:34 +0100 | [diff] [blame] | 213 | /* |
| 214 | * GSM 08.08 ยง 3.4.7 cipher mode handling. We will have to pick |
| 215 | * the cipher to be used for this. In case we are already using |
| 216 | * a cipher we will have to send cipher mode reject to the MSC, |
| 217 | * otherwise we will have to pick something that we and the MS |
| 218 | * is supporting. Currently we are doing it in a rather static |
| 219 | * way by picking one ecnryption or no encrytpion. |
| 220 | */ |
| 221 | static int bssmap_handle_cipher_mode(struct osmo_bsc_sccp_con *conn, |
| 222 | struct msgb *msg, unsigned int payload_length) |
| 223 | { |
| 224 | uint16_t len; |
| 225 | struct gsm_network *network = NULL; |
| 226 | const uint8_t *data; |
| 227 | struct tlv_parsed tp; |
| 228 | struct msgb *resp; |
| 229 | int reject_cause = -1; |
| 230 | int include_imeisv = 1; |
| 231 | |
| 232 | if (!conn->conn) { |
| 233 | LOGP(DMSC, LOGL_ERROR, "No lchan/msc_data in cipher mode command.\n"); |
| 234 | goto reject; |
| 235 | } |
| 236 | |
| 237 | if (conn->ciphering_handled) { |
| 238 | LOGP(DMSC, LOGL_ERROR, "Already seen ciphering command. Protocol Error.\n"); |
| 239 | goto reject; |
| 240 | } |
| 241 | |
| 242 | conn->ciphering_handled = 1; |
| 243 | |
| 244 | tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l4h + 1, payload_length - 1, 0, 0); |
| 245 | if (!TLVP_PRESENT(&tp, GSM0808_IE_ENCRYPTION_INFORMATION)) { |
| 246 | LOGP(DMSC, LOGL_ERROR, "IE Encryption Information missing.\n"); |
| 247 | goto reject; |
| 248 | } |
| 249 | |
| 250 | /* |
| 251 | * check if our global setting is allowed |
| 252 | * - Currently we check for A5/0 and A5/1 |
| 253 | * - Copy the key if that is necessary |
| 254 | * - Otherwise reject |
| 255 | */ |
| 256 | len = TLVP_LEN(&tp, GSM0808_IE_ENCRYPTION_INFORMATION); |
| 257 | if (len < 1) { |
| 258 | LOGP(DMSC, LOGL_ERROR, "IE Encryption Information is too short.\n"); |
| 259 | goto reject; |
| 260 | } |
| 261 | |
| 262 | network = conn->conn->bts->network; |
| 263 | data = TLVP_VAL(&tp, GSM0808_IE_ENCRYPTION_INFORMATION); |
| 264 | |
| 265 | if (TLVP_PRESENT(&tp, GSM0808_IE_CIPHER_RESPONSE_MODE)) |
| 266 | include_imeisv = TLVP_VAL(&tp, GSM0808_IE_CIPHER_RESPONSE_MODE)[0] & 0x1; |
| 267 | |
| 268 | if (network->a5_encryption == 0 && (data[0] & 0x1) == 0x1) { |
| 269 | gsm0808_cipher_mode(conn->conn, 0, NULL, 0, include_imeisv); |
| 270 | } else if (network->a5_encryption != 0 && (data[0] & 0x2) == 0x2) { |
| 271 | gsm0808_cipher_mode(conn->conn, 1, &data[1], len - 1, include_imeisv); |
| 272 | } else { |
| 273 | LOGP(DMSC, LOGL_ERROR, "Can not select encryption...\n"); |
| 274 | goto reject; |
| 275 | } |
| 276 | |
Harald Welte | 75413c4 | 2011-07-12 00:03:43 +0200 | [diff] [blame] | 277 | return 0; |
| 278 | |
Holger Hans Peter Freyther | fae3c65 | 2010-11-10 09:44:34 +0100 | [diff] [blame] | 279 | reject: |
| 280 | resp = gsm0808_create_cipher_reject(reject_cause); |
| 281 | if (!resp) { |
| 282 | LOGP(DMSC, LOGL_ERROR, "Sending the cipher reject failed.\n"); |
| 283 | return -1; |
| 284 | } |
| 285 | |
| 286 | bsc_queue_for_msc(conn, resp); |
| 287 | return -1; |
| 288 | } |
| 289 | |
Holger Hans Peter Freyther | 50c579b | 2010-11-10 10:07:30 +0100 | [diff] [blame] | 290 | /* |
| 291 | * Handle the assignment request message. |
| 292 | * |
| 293 | * See ยง3.2.1.1 for the message type |
| 294 | */ |
| 295 | static int bssmap_handle_assignm_req(struct osmo_bsc_sccp_con *conn, |
| 296 | struct msgb *msg, unsigned int length) |
| 297 | { |
| 298 | struct msgb *resp; |
Holger Hans Peter Freyther | 8ec4952 | 2011-08-15 15:53:00 +0200 | [diff] [blame] | 299 | struct osmo_msc_data *msc; |
Holger Hans Peter Freyther | 50c579b | 2010-11-10 10:07:30 +0100 | [diff] [blame] | 300 | struct tlv_parsed tp; |
| 301 | uint8_t *data; |
| 302 | uint16_t cic; |
| 303 | uint8_t timeslot; |
| 304 | uint8_t multiplex; |
| 305 | enum gsm48_chan_mode chan_mode = GSM48_CMODE_SIGN; |
| 306 | int i, supported, port, full_rate = -1; |
| 307 | |
| 308 | if (!conn->conn) { |
| 309 | LOGP(DMSC, LOGL_ERROR, "No lchan/msc_data in cipher mode command.\n"); |
| 310 | return -1; |
| 311 | } |
| 312 | |
Holger Hans Peter Freyther | 50c579b | 2010-11-10 10:07:30 +0100 | [diff] [blame] | 313 | tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l4h + 1, length - 1, 0, 0); |
| 314 | |
| 315 | if (!TLVP_PRESENT(&tp, GSM0808_IE_CHANNEL_TYPE)) { |
Holger Hans Peter Freyther | 59043d8 | 2012-08-03 11:05:29 +0200 | [diff] [blame] | 316 | LOGP(DMSC, LOGL_ERROR, "Mandatory channel type not present.\n"); |
Holger Hans Peter Freyther | 50c579b | 2010-11-10 10:07:30 +0100 | [diff] [blame] | 317 | goto reject; |
| 318 | } |
| 319 | |
| 320 | if (!TLVP_PRESENT(&tp, GSM0808_IE_CIRCUIT_IDENTITY_CODE)) { |
| 321 | LOGP(DMSC, LOGL_ERROR, "Identity code missing. Audio routing will not work.\n"); |
| 322 | goto reject; |
| 323 | } |
| 324 | |
| 325 | cic = ntohs(read_data16(TLVP_VAL(&tp, GSM0808_IE_CIRCUIT_IDENTITY_CODE))); |
| 326 | timeslot = cic & 0x1f; |
| 327 | multiplex = (cic & ~0x1f) >> 5; |
| 328 | |
| 329 | /* |
| 330 | * Currently we only support a limited subset of all |
| 331 | * possible channel types. The limitation ends by not using |
| 332 | * multi-slot, limiting the channel coding, speech... |
| 333 | */ |
| 334 | if (TLVP_LEN(&tp, GSM0808_IE_CHANNEL_TYPE) < 3) { |
| 335 | LOGP(DMSC, LOGL_ERROR, "ChannelType len !=3 not supported: %d\n", |
| 336 | TLVP_LEN(&tp, GSM0808_IE_CHANNEL_TYPE)); |
| 337 | goto reject; |
| 338 | } |
| 339 | |
| 340 | /* |
| 341 | * Try to figure out if we support the proposed speech codecs. For |
| 342 | * now we will always pick the full rate codecs. |
| 343 | */ |
| 344 | |
| 345 | data = (uint8_t *) TLVP_VAL(&tp, GSM0808_IE_CHANNEL_TYPE); |
| 346 | if ((data[0] & 0xf) != 0x1) { |
| 347 | LOGP(DMSC, LOGL_ERROR, "ChannelType != speech: %d\n", data[0]); |
| 348 | goto reject; |
| 349 | } |
| 350 | |
Holger Hans Peter Freyther | 50c579b | 2010-11-10 10:07:30 +0100 | [diff] [blame] | 351 | /* |
| 352 | * go through the list of preferred codecs of our gsm network |
| 353 | * and try to find it among the permitted codecs. If we found |
| 354 | * it we will send chan_mode to the right mode and break the |
| 355 | * inner loop. The outer loop will exit due chan_mode having |
| 356 | * the correct value. |
| 357 | */ |
| 358 | full_rate = 0; |
Holger Hans Peter Freyther | f936fb4 | 2011-06-04 15:12:57 +0200 | [diff] [blame] | 359 | msc = conn->msc; |
Holger Hans Peter Freyther | 50c579b | 2010-11-10 10:07:30 +0100 | [diff] [blame] | 360 | for (supported = 0; |
Holger Hans Peter Freyther | 8ec4952 | 2011-08-15 15:53:00 +0200 | [diff] [blame] | 361 | chan_mode == GSM48_CMODE_SIGN && supported < msc->audio_length; |
Holger Hans Peter Freyther | 50c579b | 2010-11-10 10:07:30 +0100 | [diff] [blame] | 362 | ++supported) { |
| 363 | |
Holger Hans Peter Freyther | 8ec4952 | 2011-08-15 15:53:00 +0200 | [diff] [blame] | 364 | int perm_val = audio_support_to_gsm88(msc->audio_support[supported]); |
Holger Hans Peter Freyther | 50c579b | 2010-11-10 10:07:30 +0100 | [diff] [blame] | 365 | for (i = 2; i < TLVP_LEN(&tp, GSM0808_IE_CHANNEL_TYPE); ++i) { |
| 366 | if ((data[i] & 0x7f) == perm_val) { |
| 367 | chan_mode = gsm88_to_chan_mode(perm_val); |
| 368 | full_rate = (data[i] & 0x4) == 0; |
| 369 | break; |
| 370 | } else if ((data[i] & 0x80) == 0x00) { |
| 371 | break; |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | if (chan_mode == GSM48_CMODE_SIGN) { |
| 377 | LOGP(DMSC, LOGL_ERROR, "No supported audio type found.\n"); |
| 378 | goto reject; |
| 379 | } |
| 380 | |
| 381 | /* map it to a MGCP Endpoint and a RTP port */ |
| 382 | port = mgcp_timeslot_to_endpoint(multiplex, timeslot); |
Holger Hans Peter Freyther | 8ec4952 | 2011-08-15 15:53:00 +0200 | [diff] [blame] | 383 | conn->rtp_port = rtp_calculate_port(port, msc->rtp_base); |
Holger Hans Peter Freyther | 50c579b | 2010-11-10 10:07:30 +0100 | [diff] [blame] | 384 | |
| 385 | return gsm0808_assign_req(conn->conn, chan_mode, full_rate); |
| 386 | |
| 387 | reject: |
| 388 | resp = gsm0808_create_assignment_failure(GSM0808_CAUSE_NO_RADIO_RESOURCE_AVAILABLE, NULL); |
| 389 | if (!resp) { |
| 390 | LOGP(DMSC, LOGL_ERROR, "Channel allocation failure.\n"); |
| 391 | return -1; |
| 392 | } |
| 393 | |
| 394 | bsc_queue_for_msc(conn, resp); |
| 395 | return -1; |
| 396 | } |
| 397 | |
Holger Hans Peter Freyther | 644b0bf | 2011-06-08 15:52:07 +0200 | [diff] [blame] | 398 | static int bssmap_rcvmsg_udt(struct osmo_msc_data *msc, |
Holger Hans Peter Freyther | 890dfc5 | 2010-11-10 09:24:37 +0100 | [diff] [blame] | 399 | struct msgb *msg, unsigned int length) |
| 400 | { |
| 401 | int ret = 0; |
| 402 | |
| 403 | if (length < 1) { |
| 404 | LOGP(DMSC, LOGL_ERROR, "Not enough room: %d\n", length); |
| 405 | return -1; |
| 406 | } |
| 407 | |
Harald Welte | cccd301 | 2011-07-11 18:18:35 +0200 | [diff] [blame] | 408 | LOGP(DMSC, LOGL_INFO, "Rx MSC UDT BSSMAP %s\n", |
| 409 | gsm0808_bssmap_name(msg->l4h[0])); |
| 410 | |
Holger Hans Peter Freyther | 890dfc5 | 2010-11-10 09:24:37 +0100 | [diff] [blame] | 411 | switch (msg->l4h[0]) { |
| 412 | case BSS_MAP_MSG_RESET_ACKNOWLEDGE: |
Holger Hans Peter Freyther | 644b0bf | 2011-06-08 15:52:07 +0200 | [diff] [blame] | 413 | ret = bssmap_handle_reset_ack(msc, msg, length); |
Holger Hans Peter Freyther | 890dfc5 | 2010-11-10 09:24:37 +0100 | [diff] [blame] | 414 | break; |
| 415 | case BSS_MAP_MSG_PAGING: |
Holger Hans Peter Freyther | 3d119f1 | 2012-08-30 16:43:28 +0200 | [diff] [blame^] | 416 | if (bsc_grace_allow_new_connection(msc->network, NULL)) |
Holger Hans Peter Freyther | 644b0bf | 2011-06-08 15:52:07 +0200 | [diff] [blame] | 417 | ret = bssmap_handle_paging(msc, msg, length); |
Holger Hans Peter Freyther | 890dfc5 | 2010-11-10 09:24:37 +0100 | [diff] [blame] | 418 | break; |
| 419 | } |
| 420 | |
| 421 | return ret; |
| 422 | } |
| 423 | |
| 424 | static int bssmap_rcvmsg_dt1(struct osmo_bsc_sccp_con *conn, |
| 425 | struct msgb *msg, unsigned int length) |
| 426 | { |
Holger Hans Peter Freyther | f1f57a8 | 2010-11-10 09:34:47 +0100 | [diff] [blame] | 427 | int ret = 0; |
| 428 | |
| 429 | if (length < 1) { |
| 430 | LOGP(DMSC, LOGL_ERROR, "Not enough room: %d\n", length); |
| 431 | return -1; |
| 432 | } |
| 433 | |
Harald Welte | cccd301 | 2011-07-11 18:18:35 +0200 | [diff] [blame] | 434 | LOGP(DMSC, LOGL_INFO, "Rx MSC DT1 BSSMAP %s\n", |
| 435 | gsm0808_bssmap_name(msg->l4h[0])); |
| 436 | |
Holger Hans Peter Freyther | f1f57a8 | 2010-11-10 09:34:47 +0100 | [diff] [blame] | 437 | switch (msg->l4h[0]) { |
| 438 | case BSS_MAP_MSG_CLEAR_CMD: |
| 439 | ret = bssmap_handle_clear_command(conn, msg, length); |
| 440 | break; |
Holger Hans Peter Freyther | fae3c65 | 2010-11-10 09:44:34 +0100 | [diff] [blame] | 441 | case BSS_MAP_MSG_CIPHER_MODE_CMD: |
| 442 | ret = bssmap_handle_cipher_mode(conn, msg, length); |
| 443 | break; |
Holger Hans Peter Freyther | 50c579b | 2010-11-10 10:07:30 +0100 | [diff] [blame] | 444 | case BSS_MAP_MSG_ASSIGMENT_RQST: |
| 445 | ret = bssmap_handle_assignm_req(conn, msg, length); |
| 446 | break; |
Holger Hans Peter Freyther | f1f57a8 | 2010-11-10 09:34:47 +0100 | [diff] [blame] | 447 | default: |
Harald Welte | 9f32a8a | 2011-07-11 17:56:34 +0200 | [diff] [blame] | 448 | LOGP(DMSC, LOGL_NOTICE, "Unimplemented msg type: %s\n", |
| 449 | gsm0808_bssmap_name(msg->l4h[0])); |
Holger Hans Peter Freyther | f1f57a8 | 2010-11-10 09:34:47 +0100 | [diff] [blame] | 450 | break; |
| 451 | } |
| 452 | |
| 453 | return ret; |
Holger Hans Peter Freyther | 890dfc5 | 2010-11-10 09:24:37 +0100 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | static int dtap_rcvmsg(struct osmo_bsc_sccp_con *conn, |
| 457 | struct msgb *msg, unsigned int length) |
| 458 | { |
Holger Hans Peter Freyther | dbc698a | 2010-11-10 10:17:05 +0100 | [diff] [blame] | 459 | struct dtap_header *header; |
| 460 | struct msgb *gsm48; |
| 461 | uint8_t *data; |
Holger Hans Peter Freyther | 3d4d79d | 2012-12-03 15:32:54 +0100 | [diff] [blame] | 462 | int rc, dtap_rc; |
Holger Hans Peter Freyther | dbc698a | 2010-11-10 10:17:05 +0100 | [diff] [blame] | 463 | |
Harald Welte | cccd301 | 2011-07-11 18:18:35 +0200 | [diff] [blame] | 464 | LOGP(DMSC, LOGL_DEBUG, "Rx MSC DTAP: %s\n", |
| 465 | osmo_hexdump(msg->l3h, length)); |
| 466 | |
Holger Hans Peter Freyther | dbc698a | 2010-11-10 10:17:05 +0100 | [diff] [blame] | 467 | if (!conn->conn) { |
| 468 | LOGP(DMSC, LOGL_ERROR, "No subscriber connection available\n"); |
| 469 | return -1; |
| 470 | } |
| 471 | |
| 472 | header = (struct dtap_header *) msg->l3h; |
| 473 | if (sizeof(*header) >= length) { |
Holger Hans Peter Freyther | 2f2be57 | 2012-03-16 12:18:39 +0100 | [diff] [blame] | 474 | LOGP(DMSC, LOGL_ERROR, "The DTAP header does not fit. Wanted: %zu got: %u\n", sizeof(*header), length); |
Pablo Neira Ayuso | c0d17f2 | 2011-05-07 12:12:48 +0200 | [diff] [blame] | 475 | LOGP(DMSC, LOGL_ERROR, "hex: %s\n", osmo_hexdump(msg->l3h, length)); |
Holger Hans Peter Freyther | dbc698a | 2010-11-10 10:17:05 +0100 | [diff] [blame] | 476 | return -1; |
| 477 | } |
| 478 | |
| 479 | if (header->length > length - sizeof(*header)) { |
| 480 | LOGP(DMSC, LOGL_ERROR, "The DTAP l4 information does not fit: header: %u length: %u\n", header->length, length); |
Pablo Neira Ayuso | c0d17f2 | 2011-05-07 12:12:48 +0200 | [diff] [blame] | 481 | LOGP(DMSC, LOGL_ERROR, "hex: %s\n", osmo_hexdump(msg->l3h, length)); |
Holger Hans Peter Freyther | dbc698a | 2010-11-10 10:17:05 +0100 | [diff] [blame] | 482 | return -1; |
| 483 | } |
| 484 | |
Harald Welte | cccd301 | 2011-07-11 18:18:35 +0200 | [diff] [blame] | 485 | LOGP(DMSC, LOGL_INFO, "Rx MSC DTAP, SAPI: %u CHAN: %u\n", header->link_id & 0x07, header->link_id & 0xC0); |
Holger Hans Peter Freyther | dbc698a | 2010-11-10 10:17:05 +0100 | [diff] [blame] | 486 | |
| 487 | /* forward the data */ |
| 488 | gsm48 = gsm48_msgb_alloc(); |
| 489 | if (!gsm48) { |
| 490 | LOGP(DMSC, LOGL_ERROR, "Allocation of the message failed.\n"); |
| 491 | return -1; |
| 492 | } |
| 493 | |
| 494 | gsm48->l3h = gsm48->data; |
| 495 | data = msgb_put(gsm48, length - sizeof(*header)); |
| 496 | memcpy(data, msg->l3h + sizeof(*header), length - sizeof(*header)); |
| 497 | |
| 498 | /* pass it to the filter for extra actions */ |
Holger Hans Peter Freyther | d2361d9 | 2012-12-03 15:32:54 +0100 | [diff] [blame] | 499 | rc = bsc_scan_msc_msg(conn->conn, gsm48); |
Holger Hans Peter Freyther | 3d4d79d | 2012-12-03 15:32:54 +0100 | [diff] [blame] | 500 | dtap_rc = gsm0808_submit_dtap(conn->conn, gsm48, header->link_id, 1); |
Holger Hans Peter Freyther | d2361d9 | 2012-12-03 15:32:54 +0100 | [diff] [blame] | 501 | if (rc == BSS_SEND_USSD) |
| 502 | bsc_send_welcome_ussd(conn->conn); |
Holger Hans Peter Freyther | 3d4d79d | 2012-12-03 15:32:54 +0100 | [diff] [blame] | 503 | return dtap_rc; |
Holger Hans Peter Freyther | 890dfc5 | 2010-11-10 09:24:37 +0100 | [diff] [blame] | 504 | } |
| 505 | |
Holger Hans Peter Freyther | 644b0bf | 2011-06-08 15:52:07 +0200 | [diff] [blame] | 506 | int bsc_handle_udt(struct osmo_msc_data *msc, |
Holger Hans Peter Freyther | 3485feb | 2010-11-09 23:28:33 +0100 | [diff] [blame] | 507 | struct msgb *msgb, unsigned int length) |
| 508 | { |
| 509 | struct bssmap_header *bs; |
| 510 | |
Harald Welte | cccd301 | 2011-07-11 18:18:35 +0200 | [diff] [blame] | 511 | LOGP(DMSC, LOGL_DEBUG, "Rx MSC UDT: %s\n", |
Pablo Neira Ayuso | c0d17f2 | 2011-05-07 12:12:48 +0200 | [diff] [blame] | 512 | osmo_hexdump(msgb->l3h, length)); |
Holger Hans Peter Freyther | 3485feb | 2010-11-09 23:28:33 +0100 | [diff] [blame] | 513 | |
| 514 | if (length < sizeof(*bs)) { |
| 515 | LOGP(DMSC, LOGL_ERROR, "The header is too short.\n"); |
| 516 | return -1; |
| 517 | } |
| 518 | |
| 519 | bs = (struct bssmap_header *) msgb->l3h; |
| 520 | if (bs->length < length - sizeof(*bs)) |
| 521 | return -1; |
| 522 | |
| 523 | switch (bs->type) { |
| 524 | case BSSAP_MSG_BSS_MANAGEMENT: |
Holger Hans Peter Freyther | 890dfc5 | 2010-11-10 09:24:37 +0100 | [diff] [blame] | 525 | msgb->l4h = &msgb->l3h[sizeof(*bs)]; |
Holger Hans Peter Freyther | 644b0bf | 2011-06-08 15:52:07 +0200 | [diff] [blame] | 526 | bssmap_rcvmsg_udt(msc, msgb, length - sizeof(*bs)); |
Holger Hans Peter Freyther | 3485feb | 2010-11-09 23:28:33 +0100 | [diff] [blame] | 527 | break; |
| 528 | default: |
Harald Welte | 9f32a8a | 2011-07-11 17:56:34 +0200 | [diff] [blame] | 529 | LOGP(DMSC, LOGL_NOTICE, "Unimplemented msg type: %s\n", |
| 530 | gsm0808_bssmap_name(bs->type)); |
Holger Hans Peter Freyther | 3485feb | 2010-11-09 23:28:33 +0100 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | return 0; |
| 534 | } |
| 535 | |
| 536 | int bsc_handle_dt1(struct osmo_bsc_sccp_con *conn, |
| 537 | struct msgb *msg, unsigned int len) |
| 538 | { |
Holger Hans Peter Freyther | 890dfc5 | 2010-11-10 09:24:37 +0100 | [diff] [blame] | 539 | if (len < sizeof(struct bssmap_header)) { |
| 540 | LOGP(DMSC, LOGL_ERROR, "The header is too short.\n"); |
| 541 | } |
| 542 | |
| 543 | switch (msg->l3h[0]) { |
| 544 | case BSSAP_MSG_BSS_MANAGEMENT: |
| 545 | msg->l4h = &msg->l3h[sizeof(struct bssmap_header)]; |
| 546 | bssmap_rcvmsg_dt1(conn, msg, len - sizeof(struct bssmap_header)); |
| 547 | break; |
| 548 | case BSSAP_MSG_DTAP: |
| 549 | dtap_rcvmsg(conn, msg, len); |
| 550 | break; |
| 551 | default: |
Harald Welte | cccd301 | 2011-07-11 18:18:35 +0200 | [diff] [blame] | 552 | LOGP(DMSC, LOGL_NOTICE, "Unimplemented BSSAP msg type: %s\n", |
| 553 | gsm0808_bssap_name(msg->l3h[0])); |
Holger Hans Peter Freyther | 890dfc5 | 2010-11-10 09:24:37 +0100 | [diff] [blame] | 554 | } |
| 555 | |
Holger Hans Peter Freyther | 3485feb | 2010-11-09 23:28:33 +0100 | [diff] [blame] | 556 | return -1; |
| 557 | } |